2 * vsp1_video.c -- R-Car VSP1 Video Node
4 * Copyright (C) 2013-2015 Renesas Electronics Corporation
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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.
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>
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>
33 #include "vsp1_entity.h"
35 #include "vsp1_pipe.h"
36 #include "vsp1_rwpf.h"
38 #include "vsp1_video.h"
40 #define VSP1_VIDEO_DEF_FORMAT V4L2_PIX_FMT_YUYV
41 #define VSP1_VIDEO_DEF_WIDTH 1024
42 #define VSP1_VIDEO_DEF_HEIGHT 768
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
49 /* -----------------------------------------------------------------------------
53 static struct v4l2_subdev *
54 vsp1_video_remote_subdev(struct media_pad *local, u32 *pad)
56 struct media_pad *remote;
58 remote = media_entity_remote_pad(local);
59 if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
65 return media_entity_to_v4l2_subdev(remote->entity);
68 static int vsp1_video_verify_format(struct vsp1_video *video)
70 struct v4l2_subdev_format fmt;
71 struct v4l2_subdev *subdev;
74 subdev = vsp1_video_remote_subdev(&video->pad, &fmt.pad);
78 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
79 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
81 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
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)
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)
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 },
102 const struct vsp1_format_info *info;
103 unsigned int width = pix->width;
104 unsigned int height = pix->height;
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.
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];
120 * Retrieve format information and select the default format if the
121 * requested format isn't supported.
123 info = vsp1_get_format_info(video->vsp1, pix->pixelformat);
125 info = vsp1_get_format_info(video->vsp1, VSP1_VIDEO_DEF_FORMAT);
127 pix->pixelformat = info->fourcc;
128 pix->colorspace = V4L2_COLORSPACE_SRGB;
129 pix->field = V4L2_FIELD_NONE;
131 if (info->fourcc == V4L2_PIX_FMT_HSV24 ||
132 info->fourcc == V4L2_PIX_FMT_HSV32)
133 pix->hsv_enc = V4L2_HSV_ENC_256;
135 memset(pix->reserved, 0, sizeof(pix->reserved));
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);
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);
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.
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;
157 bpl = clamp_t(unsigned int, pix->plane_fmt[i].bytesperline,
158 pix->width / hsub * info->bpp[i] / 8,
159 round_down(65535U, align));
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;
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;
172 pix->num_planes = info->planes;
180 /* -----------------------------------------------------------------------------
181 * VSP1 Partition Algorithm support
184 static void vsp1_video_pipeline_setup_partitions(struct vsp1_pipeline *pipe)
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;
192 * Partitions are computed on the size before rotation, use the format
195 format = vsp1_entity_get_pad_format(&pipe->output->entity,
196 pipe->output->entity.config,
198 div_size = format->width;
200 /* Gen2 hardware doesn't require image partitioning. */
201 if (vsp1->info->gen == 2) {
202 pipe->div_size = div_size;
203 pipe->partitions = 1;
207 list_for_each_entry(entity, &pipe->entities, list_pipe) {
208 unsigned int entity_max = VSP1_VIDEO_MAX_WIDTH;
210 if (entity->ops->max_width) {
211 entity_max = entity->ops->max_width(entity, pipe);
213 div_size = min(div_size, entity_max);
217 pipe->div_size = div_size;
218 pipe->partitions = DIV_ROUND_UP(format->width, div_size);
222 * vsp1_video_partition - Calculate the active partition output window
224 * @div_size: pre-determined maximum partition division size
225 * @index: partition index
227 * Returns a v4l2_rect describing the partition window.
229 static struct v4l2_rect vsp1_video_partition(struct vsp1_pipeline *pipe,
230 unsigned int div_size,
233 const struct v4l2_mbus_framefmt *format;
234 struct v4l2_rect partition;
235 unsigned int modulus;
238 * Partitions are computed on the size before rotation, use the format
241 format = vsp1_entity_get_pad_format(&pipe->output->entity,
242 pipe->output->entity.config,
245 /* A single partition simply processes the output size in full. */
246 if (pipe->partitions <= 1) {
249 partition.width = format->width;
250 partition.height = format->height;
254 /* Initialise the partition with sane starting conditions. */
255 partition.left = index * div_size;
257 partition.width = div_size;
258 partition.height = format->height;
260 modulus = format->width % div_size;
263 * We need to prevent the last partition from being smaller than the
264 * *minimum* width of the hardware capabilities.
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|.
273 * pipe->partitions is 1 based, whilst index is a 0 based index.
274 * Normalise this locally.
276 unsigned int partitions = pipe->partitions - 1;
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;
287 } else if (index == partitions) {
288 partition.width = modulus;
295 /* -----------------------------------------------------------------------------
296 * Pipeline Management
300 * vsp1_video_complete_buffer - Complete the current buffer
301 * @video: the video node
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.
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.
311 * Return the next queued buffer or NULL if the queue is empty.
313 static struct vsp1_vb2_buffer *
314 vsp1_video_complete_buffer(struct vsp1_video *video)
316 struct vsp1_pipeline *pipe = video->rwpf->pipe;
317 struct vsp1_vb2_buffer *next = NULL;
318 struct vsp1_vb2_buffer *done;
322 spin_lock_irqsave(&video->irqlock, flags);
324 if (list_empty(&video->irqqueue)) {
325 spin_unlock_irqrestore(&video->irqlock, flags);
329 done = list_first_entry(&video->irqqueue,
330 struct vsp1_vb2_buffer, queue);
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);
338 list_del(&done->queue);
340 if (!list_empty(&video->irqqueue))
341 next = list_first_entry(&video->irqqueue,
342 struct vsp1_vb2_buffer, queue);
344 spin_unlock_irqrestore(&video->irqlock, flags);
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);
356 static void vsp1_video_frame_end(struct vsp1_pipeline *pipe,
357 struct vsp1_rwpf *rwpf)
359 struct vsp1_video *video = rwpf->video;
360 struct vsp1_vb2_buffer *buf;
362 buf = vsp1_video_complete_buffer(video);
366 video->rwpf->mem = buf->mem;
367 pipe->buffers_ready |= 1 << video->pipe_index;
370 static void vsp1_video_pipeline_run_partition(struct vsp1_pipeline *pipe,
371 struct vsp1_dl_list *dl)
373 struct vsp1_entity *entity;
375 pipe->partition = vsp1_video_partition(pipe, pipe->div_size,
376 pipe->current_partition);
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);
385 static void vsp1_video_pipeline_run(struct vsp1_pipeline *pipe)
387 struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
388 struct vsp1_entity *entity;
391 pipe->dl = vsp1_dl_list_get(pipe->output->dlm);
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.
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);
404 /* Run the first partition */
405 pipe->current_partition = 0;
406 vsp1_video_pipeline_run_partition(pipe, pipe->dl);
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;
415 * Partition configuration operations will utilise
416 * the pipe->current_partition variable to determine
417 * the work they should complete.
419 dl = vsp1_dl_list_get(pipe->output->dlm);
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.
427 dev_err(vsp1->dev, "Failed to obtain a dl list. Frame will be incomplete\n");
431 vsp1_video_pipeline_run_partition(pipe, dl);
432 vsp1_dl_list_add_chain(pipe->dl, dl);
435 /* Complete, and commit the head display list. */
436 vsp1_dl_list_commit(pipe->dl);
439 vsp1_pipeline_run(pipe);
442 static void vsp1_video_pipeline_frame_end(struct vsp1_pipeline *pipe)
444 struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
445 enum vsp1_pipeline_state state;
449 spin_lock_irqsave(&pipe->irqlock, flags);
451 /* Complete buffers on all video nodes. */
452 for (i = 0; i < vsp1->info->rpf_count; ++i) {
453 if (!pipe->inputs[i])
456 vsp1_video_frame_end(pipe, pipe->inputs[i]);
459 vsp1_video_frame_end(pipe, pipe->output);
462 pipe->state = VSP1_PIPELINE_STOPPED;
465 * If a stop has been requested, mark the pipeline as stopped and
466 * return. Otherwise restart the pipeline if ready.
468 if (state == VSP1_PIPELINE_STOPPING)
470 else if (vsp1_pipeline_ready(pipe))
471 vsp1_video_pipeline_run(pipe);
473 spin_unlock_irqrestore(&pipe->irqlock, flags);
476 static int vsp1_video_pipeline_build_branch(struct vsp1_pipeline *pipe,
477 struct vsp1_rwpf *input,
478 struct vsp1_rwpf *output)
480 struct media_entity_enum ent_enum;
481 struct vsp1_entity *entity;
482 struct media_pad *pad;
483 bool bru_found = false;
486 ret = media_entity_enum_init(&ent_enum, &input->entity.vsp1->media_dev);
491 * The main data path doesn't include the HGO or HGT, use
492 * vsp1_entity_remote_pad() to traverse the graph.
495 pad = vsp1_entity_remote_pad(&input->entity.pads[RWPF_PAD_SOURCE]);
503 /* We've reached a video node, that shouldn't have happened. */
504 if (!is_media_entity_v4l2_subdev(pad->entity)) {
509 entity = to_vsp1_entity(
510 media_entity_to_v4l2_subdev(pad->entity));
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.
516 if (entity->type == VSP1_ENTITY_BRU) {
517 struct vsp1_bru *bru = to_bru(&entity->subdev);
519 bru->inputs[pad->index].rpf = input;
520 input->bru_input = pad->index;
525 /* We've reached the WPF, we're done. */
526 if (entity->type == VSP1_ENTITY_WPF)
529 /* Ensure the branch has no loop. */
530 if (media_entity_enum_test_and_set(&ent_enum,
531 &entity->subdev.entity)) {
536 /* UDS can't be chained. */
537 if (entity->type == VSP1_ENTITY_UDS) {
544 pipe->uds_input = bru_found ? pipe->bru
548 /* Follow the source link, ignoring any HGO or HGT. */
549 pad = &entity->pads[entity->source_pad];
550 pad = vsp1_entity_remote_pad(pad);
553 /* The last entity must be the output WPF. */
554 if (entity != &output->entity)
558 media_entity_enum_cleanup(&ent_enum);
563 static int vsp1_video_pipeline_build(struct vsp1_pipeline *pipe,
564 struct vsp1_video *video)
566 struct media_graph graph;
567 struct media_entity *entity = &video->video.entity;
568 struct media_device *mdev = entity->graph_obj.mdev;
572 /* Walk the graph to locate the entities and video nodes. */
573 ret = media_graph_walk_init(&graph, mdev);
577 media_graph_walk_start(&graph, entity);
579 while ((entity = media_graph_walk_next(&graph))) {
580 struct v4l2_subdev *subdev;
581 struct vsp1_rwpf *rwpf;
582 struct vsp1_entity *e;
584 if (!is_media_entity_v4l2_subdev(entity))
587 subdev = media_entity_to_v4l2_subdev(entity);
588 e = to_vsp1_entity(subdev);
589 list_add_tail(&e->list_pipe, &pipe->entities);
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;
596 } else if (e->type == VSP1_ENTITY_WPF) {
597 rwpf = to_rwpf(subdev);
599 rwpf->video->pipe_index = 0;
601 } else if (e->type == VSP1_ENTITY_LIF) {
603 } else if (e->type == VSP1_ENTITY_BRU) {
605 } else if (e->type == VSP1_ENTITY_HGO) {
606 struct vsp1_hgo *hgo = to_hgo(subdev);
609 hgo->histo.pipe = pipe;
613 media_graph_walk_cleanup(&graph);
615 /* We need one output and at least one input. */
616 if (pipe->num_inputs == 0 || !pipe->output)
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.
623 for (i = 0; i < video->vsp1->info->rpf_count; ++i) {
624 if (!pipe->inputs[i])
627 ret = vsp1_video_pipeline_build_branch(pipe, pipe->inputs[i],
636 static int vsp1_video_pipeline_init(struct vsp1_pipeline *pipe,
637 struct vsp1_video *video)
639 vsp1_pipeline_init(pipe);
641 pipe->frame_end = vsp1_video_pipeline_frame_end;
643 return vsp1_video_pipeline_build(pipe, video);
646 static struct vsp1_pipeline *vsp1_video_pipeline_get(struct vsp1_video *video)
648 struct vsp1_pipeline *pipe;
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.
657 if (!video->rwpf->pipe) {
658 pipe = kzalloc(sizeof(*pipe), GFP_KERNEL);
660 return ERR_PTR(-ENOMEM);
662 ret = vsp1_video_pipeline_init(pipe, video);
664 vsp1_pipeline_reset(pipe);
669 pipe = video->rwpf->pipe;
670 kref_get(&pipe->kref);
676 static void vsp1_video_pipeline_release(struct kref *kref)
678 struct vsp1_pipeline *pipe = container_of(kref, typeof(*pipe), kref);
680 vsp1_pipeline_reset(pipe);
684 static void vsp1_video_pipeline_put(struct vsp1_pipeline *pipe)
686 struct media_device *mdev = &pipe->output->entity.vsp1->media_dev;
688 mutex_lock(&mdev->graph_mutex);
689 kref_put(&pipe->kref, vsp1_video_pipeline_release);
690 mutex_unlock(&mdev->graph_mutex);
693 /* -----------------------------------------------------------------------------
694 * videobuf2 Queue Operations
698 vsp1_video_queue_setup(struct vb2_queue *vq,
699 unsigned int *nbuffers, unsigned int *nplanes,
700 unsigned int sizes[], struct device *alloc_devs[])
702 struct vsp1_video *video = vb2_get_drv_priv(vq);
703 const struct v4l2_pix_format_mplane *format = &video->rwpf->format;
707 if (*nplanes != format->num_planes)
710 for (i = 0; i < *nplanes; i++)
711 if (sizes[i] < format->plane_fmt[i].sizeimage)
716 *nplanes = format->num_planes;
718 for (i = 0; i < format->num_planes; ++i)
719 sizes[i] = format->plane_fmt[i].sizeimage;
724 static int vsp1_video_buffer_prepare(struct vb2_buffer *vb)
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;
732 if (vb->num_planes < format->num_planes)
735 for (i = 0; i < vb->num_planes; ++i) {
736 buf->mem.addr[i] = vb2_dma_contig_plane_dma_addr(vb, i);
738 if (vb2_plane_size(vb, i) < format->plane_fmt[i].sizeimage)
743 buf->mem.addr[i] = 0;
748 static void vsp1_video_buffer_queue(struct vb2_buffer *vb)
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);
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);
765 spin_lock_irqsave(&pipe->irqlock, flags);
767 video->rwpf->mem = buf->mem;
768 pipe->buffers_ready |= 1 << video->pipe_index;
770 if (vb2_is_streaming(&video->queue) &&
771 vsp1_pipeline_ready(pipe))
772 vsp1_video_pipeline_run(pipe);
774 spin_unlock_irqrestore(&pipe->irqlock, flags);
777 static int vsp1_video_setup_pipeline(struct vsp1_pipeline *pipe)
779 struct vsp1_entity *entity;
781 /* Determine this pipelines sizes for image partitioning support. */
782 vsp1_video_pipeline_setup_partitions(pipe);
784 /* Prepare the display list. */
785 pipe->dl = vsp1_dl_list_get(pipe->output->dlm);
790 struct vsp1_uds *uds = to_uds(&pipe->uds->subdev);
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.
798 if (pipe->uds_input->type == VSP1_ENTITY_BRU) {
799 uds->scale_alpha = false;
801 struct vsp1_rwpf *rpf =
802 to_rwpf(&pipe->uds_input->subdev);
804 uds->scale_alpha = rpf->fmtinfo->alpha;
808 list_for_each_entry(entity, &pipe->entities, list_pipe) {
809 vsp1_entity_route_setup(entity, pipe, pipe->dl);
811 if (entity->ops->configure)
812 entity->ops->configure(entity, pipe, pipe->dl,
813 VSP1_ENTITY_PARAMS_INIT);
819 static int vsp1_video_start_streaming(struct vb2_queue *vq, unsigned int count)
821 struct vsp1_video *video = vb2_get_drv_priv(vq);
822 struct vsp1_pipeline *pipe = video->rwpf->pipe;
823 bool start_pipeline = false;
827 mutex_lock(&pipe->lock);
828 if (pipe->stream_count == pipe->num_inputs) {
829 ret = vsp1_video_setup_pipeline(pipe);
831 mutex_unlock(&pipe->lock);
835 start_pipeline = true;
838 pipe->stream_count++;
839 mutex_unlock(&pipe->lock);
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.
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);
859 static void vsp1_video_stop_streaming(struct vb2_queue *vq)
861 struct vsp1_video *video = vb2_get_drv_priv(vq);
862 struct vsp1_pipeline *pipe = video->rwpf->pipe;
863 struct vsp1_vb2_buffer *buffer;
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.
871 spin_lock_irqsave(&video->irqlock, flags);
872 pipe->buffers_ready &= ~(1 << video->pipe_index);
873 spin_unlock_irqrestore(&video->irqlock, flags);
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");
882 vsp1_dl_list_put(pipe->dl);
885 mutex_unlock(&pipe->lock);
887 media_pipeline_stop(&video->video.entity);
888 vsp1_video_pipeline_put(pipe);
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);
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,
908 /* -----------------------------------------------------------------------------
913 vsp1_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
915 struct v4l2_fh *vfh = file->private_data;
916 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
918 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
919 | V4L2_CAP_VIDEO_CAPTURE_MPLANE
920 | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
922 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
923 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE
924 | V4L2_CAP_STREAMING;
926 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT_MPLANE
927 | V4L2_CAP_STREAMING;
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));
938 vsp1_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
940 struct v4l2_fh *vfh = file->private_data;
941 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
943 if (format->type != video->queue.type)
946 mutex_lock(&video->lock);
947 format->fmt.pix_mp = video->rwpf->format;
948 mutex_unlock(&video->lock);
954 vsp1_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
956 struct v4l2_fh *vfh = file->private_data;
957 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
959 if (format->type != video->queue.type)
962 return __vsp1_video_try_format(video, &format->fmt.pix_mp, NULL);
966 vsp1_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
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;
973 if (format->type != video->queue.type)
976 ret = __vsp1_video_try_format(video, &format->fmt.pix_mp, &info);
980 mutex_lock(&video->lock);
982 if (vb2_is_busy(&video->queue)) {
987 video->rwpf->format = format->fmt.pix_mp;
988 video->rwpf->fmtinfo = info;
991 mutex_unlock(&video->lock);
996 vsp1_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
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;
1004 if (video->queue.owner && video->queue.owner != file->private_data)
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.
1012 mutex_lock(&mdev->graph_mutex);
1014 pipe = vsp1_video_pipeline_get(video);
1016 mutex_unlock(&mdev->graph_mutex);
1017 return PTR_ERR(pipe);
1020 ret = __media_pipeline_start(&video->video.entity, &pipe->pipe);
1022 mutex_unlock(&mdev->graph_mutex);
1026 mutex_unlock(&mdev->graph_mutex);
1029 * Verify that the configured format matches the output of the connected
1032 ret = vsp1_video_verify_format(video);
1036 /* Start the queue. */
1037 ret = vb2_streamon(&video->queue, type);
1044 media_pipeline_stop(&video->video.entity);
1046 vsp1_video_pipeline_put(pipe);
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,
1069 /* -----------------------------------------------------------------------------
1070 * V4L2 File Operations
1073 static int vsp1_video_open(struct file *file)
1075 struct vsp1_video *video = video_drvdata(file);
1076 struct v4l2_fh *vfh;
1079 vfh = kzalloc(sizeof(*vfh), GFP_KERNEL);
1083 v4l2_fh_init(vfh, &video->video);
1086 file->private_data = vfh;
1088 ret = vsp1_device_get(video->vsp1);
1098 static int vsp1_video_release(struct file *file)
1100 struct vsp1_video *video = video_drvdata(file);
1101 struct v4l2_fh *vfh = file->private_data;
1103 mutex_lock(&video->lock);
1104 if (video->queue.owner == vfh) {
1105 vb2_queue_release(&video->queue);
1106 video->queue.owner = NULL;
1108 mutex_unlock(&video->lock);
1110 vsp1_device_put(video->vsp1);
1112 v4l2_fh_release(file);
1114 file->private_data = NULL;
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,
1128 /* -----------------------------------------------------------------------------
1129 * Initialization and Cleanup
1132 struct vsp1_video *vsp1_video_create(struct vsp1_device *vsp1,
1133 struct vsp1_rwpf *rwpf)
1135 struct vsp1_video *video;
1136 const char *direction;
1139 video = devm_kzalloc(vsp1->dev, sizeof(*video), GFP_KERNEL);
1141 return ERR_PTR(-ENOMEM);
1143 rwpf->video = video;
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;
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;
1160 mutex_init(&video->lock);
1161 spin_lock_init(&video->irqlock);
1162 INIT_LIST_HEAD(&video->irqqueue);
1164 /* Initialize the media entity... */
1165 ret = media_entity_pads_init(&video->video.entity, 1, &video->pad);
1167 return ERR_PTR(ret);
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);
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;
1184 video_set_drvdata(&video->video, video);
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);
1197 dev_err(video->vsp1->dev, "failed to initialize vb2 queue\n");
1201 /* ... and register the video device. */
1202 video->video.queue = &video->queue;
1203 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1205 dev_err(video->vsp1->dev, "failed to register video device\n");
1212 vsp1_video_cleanup(video);
1213 return ERR_PTR(ret);
1216 void vsp1_video_cleanup(struct vsp1_video *video)
1218 if (video_is_registered(&video->video))
1219 video_unregister_device(&video->video);
1221 media_entity_cleanup(&video->video.entity);