4 * Copyright (C) 2013-2015 Ideas on Board
5 * Copyright (C) 2013-2015 Xilinx, Inc.
7 * Contacts: Hyun Kwon <hyun.kwon@xilinx.com>
8 * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/dma/xilinx_dma.h>
16 #include <linux/dma/xilinx_frmbuf.h>
17 #include <linux/lcm.h>
18 #include <linux/list.h>
19 #include <linux/module.h>
21 #include <linux/slab.h>
23 #include <media/v4l2-dev.h>
24 #include <media/v4l2-fh.h>
25 #include <media/v4l2-ioctl.h>
26 #include <media/videobuf2-v4l2.h>
27 #include <media/videobuf2-dma-contig.h>
29 #include "xilinx-dma.h"
30 #include "xilinx-vip.h"
31 #include "xilinx-vipp.h"
33 #define XVIP_DMA_DEF_FORMAT V4L2_PIX_FMT_YUYV
34 #define XVIP_DMA_DEF_WIDTH 1920
35 #define XVIP_DMA_DEF_HEIGHT 1080
37 /* Minimum and maximum widths are expressed in bytes */
38 #define XVIP_DMA_MIN_WIDTH 1U
39 #define XVIP_DMA_MAX_WIDTH 65535U
40 #define XVIP_DMA_MIN_HEIGHT 1U
41 #define XVIP_DMA_MAX_HEIGHT 8191U
43 /* -----------------------------------------------------------------------------
47 static struct v4l2_subdev *
48 xvip_dma_remote_subdev(struct media_pad *local, u32 *pad)
50 struct media_pad *remote;
52 remote = media_entity_remote_pad(local);
53 if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
59 return media_entity_to_v4l2_subdev(remote->entity);
62 static int xvip_dma_verify_format(struct xvip_dma *dma)
64 struct v4l2_subdev_format fmt;
65 struct v4l2_subdev *subdev;
69 subdev = xvip_dma_remote_subdev(&dma->pad, &fmt.pad);
73 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
74 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
76 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
78 if (dma->fmtinfo->code != fmt.format.code)
81 if (V4L2_TYPE_IS_MULTIPLANAR(dma->format.type)) {
82 width = dma->format.fmt.pix_mp.width;
83 height = dma->format.fmt.pix_mp.height;
85 width = dma->format.fmt.pix.width;
86 height = dma->format.fmt.pix.height;
89 if (width != fmt.format.width || height != fmt.format.height)
95 /* -----------------------------------------------------------------------------
96 * Pipeline Stream Management
99 /* Get the sink pad internally connected to a source pad in the given entity. */
100 static struct media_pad *xvip_get_entity_sink(struct media_entity *entity,
101 struct media_pad *source)
105 /* The source pad can be NULL when the entity has no source pad. Return
106 * the first pad in that case, guaranteed to be a sink pad.
109 return &entity->pads[0];
111 /* Iterates through the pads to find a connected sink pad. */
112 for (i = 0; i < entity->num_pads; ++i) {
113 struct media_pad *sink = &entity->pads[i];
115 if (!(sink->flags & MEDIA_PAD_FL_SINK))
121 if (media_entity_has_route(entity, sink->index, source->index))
129 * xvip_pipeline_start_stop - Start ot stop streaming on a pipeline
130 * @pipe: The pipeline
131 * @start: Start (when true) or stop (when false) the pipeline
133 * Walk the entities chain starting at the pipeline output video node and start
134 * or stop all of them.
136 * Return: 0 if successful, or the return value of the failed video::s_stream
137 * operation otherwise.
139 static int xvip_pipeline_start_stop(struct xvip_pipeline *pipe, bool start)
141 struct xvip_dma *dma = pipe->output;
142 struct media_entity *entity;
143 struct media_pad *pad;
144 struct v4l2_subdev *subdev;
147 entity = &dma->video.entity;
151 pad = xvip_get_entity_sink(entity, pad);
155 if (!(pad->flags & MEDIA_PAD_FL_SINK))
158 pad = media_entity_remote_pad(pad);
159 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
162 entity = pad->entity;
163 subdev = media_entity_to_v4l2_subdev(entity);
165 ret = v4l2_subdev_call(subdev, video, s_stream, start);
166 if (start && ret < 0 && ret != -ENOIOCTLCMD)
174 * xvip_pipeline_set_stream - Enable/disable streaming on a pipeline
175 * @pipe: The pipeline
176 * @on: Turn the stream on when true or off when false
178 * The pipeline is shared between all DMA engines connect at its input and
179 * output. While the stream state of DMA engines can be controlled
180 * independently, pipelines have a shared stream state that enable or disable
181 * all entities in the pipeline. For this reason the pipeline uses a streaming
182 * counter that tracks the number of DMA engines that have requested the stream
185 * When called with the @on argument set to true, this function will increment
186 * the pipeline streaming count. If the streaming count reaches the number of
187 * DMA engines in the pipeline it will enable all entities that belong to the
190 * Similarly, when called with the @on argument set to false, this function will
191 * decrement the pipeline streaming count and disable all entities in the
192 * pipeline when the streaming count reaches zero.
194 * Return: 0 if successful, or the return value of the failed video::s_stream
195 * operation otherwise. Stopping the pipeline never fails. The pipeline state is
196 * not updated when the operation fails.
198 static int xvip_pipeline_set_stream(struct xvip_pipeline *pipe, bool on)
202 mutex_lock(&pipe->lock);
205 if (pipe->stream_count == pipe->num_dmas - 1) {
206 ret = xvip_pipeline_start_stop(pipe, true);
210 pipe->stream_count++;
212 if (--pipe->stream_count == 0)
213 xvip_pipeline_start_stop(pipe, false);
217 mutex_unlock(&pipe->lock);
221 static int xvip_pipeline_validate(struct xvip_pipeline *pipe,
222 struct xvip_dma *start)
224 struct media_graph graph;
225 struct media_entity *entity = &start->video.entity;
226 struct media_device *mdev = entity->graph_obj.mdev;
227 unsigned int num_inputs = 0;
228 unsigned int num_outputs = 0;
231 mutex_lock(&mdev->graph_mutex);
233 /* Walk the graph to locate the video nodes. */
234 ret = media_graph_walk_init(&graph, mdev);
236 mutex_unlock(&mdev->graph_mutex);
240 media_graph_walk_start(&graph, entity);
242 while ((entity = media_graph_walk_next(&graph))) {
243 struct xvip_dma *dma;
245 if (entity->function != MEDIA_ENT_F_IO_V4L)
248 dma = to_xvip_dma(media_entity_to_video_device(entity));
250 if (dma->pad.flags & MEDIA_PAD_FL_SINK) {
258 mutex_unlock(&mdev->graph_mutex);
260 media_graph_walk_cleanup(&graph);
262 /* We need exactly one output and zero or one input. */
263 if (num_outputs != 1 || num_inputs > 1)
266 pipe->num_dmas = num_inputs + num_outputs;
271 static void __xvip_pipeline_cleanup(struct xvip_pipeline *pipe)
278 * xvip_pipeline_cleanup - Cleanup the pipeline after streaming
279 * @pipe: the pipeline
281 * Decrease the pipeline use count and clean it up if we were the last user.
283 static void xvip_pipeline_cleanup(struct xvip_pipeline *pipe)
285 mutex_lock(&pipe->lock);
287 /* If we're the last user clean up the pipeline. */
288 if (--pipe->use_count == 0)
289 __xvip_pipeline_cleanup(pipe);
291 mutex_unlock(&pipe->lock);
295 * xvip_pipeline_prepare - Prepare the pipeline for streaming
296 * @pipe: the pipeline
297 * @dma: DMA engine at one end of the pipeline
299 * Validate the pipeline if no user exists yet, otherwise just increase the use
302 * Return: 0 if successful or -EPIPE if the pipeline is not valid.
304 static int xvip_pipeline_prepare(struct xvip_pipeline *pipe,
305 struct xvip_dma *dma)
309 mutex_lock(&pipe->lock);
311 /* If we're the first user validate and initialize the pipeline. */
312 if (pipe->use_count == 0) {
313 ret = xvip_pipeline_validate(pipe, dma);
315 __xvip_pipeline_cleanup(pipe);
324 mutex_unlock(&pipe->lock);
328 /* -----------------------------------------------------------------------------
329 * videobuf2 queue operations
333 * struct xvip_dma_buffer - Video DMA buffer
334 * @buf: vb2 buffer base object
335 * @queue: buffer list entry in the DMA engine queued buffers list
336 * @dma: DMA channel that uses the buffer
338 struct xvip_dma_buffer {
339 struct vb2_v4l2_buffer buf;
340 struct list_head queue;
341 struct xvip_dma *dma;
344 #define to_xvip_dma_buffer(vb) container_of(vb, struct xvip_dma_buffer, buf)
346 static void xvip_dma_complete(void *param)
348 struct xvip_dma_buffer *buf = param;
349 struct xvip_dma *dma = buf->dma;
352 spin_lock(&dma->queued_lock);
353 list_del(&buf->queue);
354 spin_unlock(&dma->queued_lock);
356 buf->buf.field = V4L2_FIELD_NONE;
357 buf->buf.sequence = dma->sequence++;
358 buf->buf.vb2_buf.timestamp = ktime_get_ns();
360 if (V4L2_TYPE_IS_MULTIPLANAR(dma->format.type)) {
361 for (i = 0; i < dma->fmtinfo->buffers; i++) {
363 dma->format.fmt.pix_mp.plane_fmt[i].sizeimage;
364 vb2_set_plane_payload(&buf->buf.vb2_buf, i, sizeimage);
367 sizeimage = dma->format.fmt.pix.sizeimage;
368 vb2_set_plane_payload(&buf->buf.vb2_buf, 0, sizeimage);
371 vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_DONE);
375 xvip_dma_queue_setup(struct vb2_queue *vq,
376 unsigned int *nbuffers, unsigned int *nplanes,
377 unsigned int sizes[], struct device *alloc_devs[])
379 struct xvip_dma *dma = vb2_get_drv_priv(vq);
383 /* Multi planar case: Make sure the image size is large enough */
384 if (V4L2_TYPE_IS_MULTIPLANAR(dma->format.type)) {
386 if (*nplanes != dma->format.fmt.pix_mp.num_planes)
389 for (i = 0; i < *nplanes; i++) {
391 dma->format.fmt.pix_mp.plane_fmt[i].sizeimage;
392 if (sizes[i] < sizeimage)
396 *nplanes = dma->fmtinfo->buffers;
397 for (i = 0; i < dma->fmtinfo->buffers; i++) {
399 dma->format.fmt.pix_mp.plane_fmt[i].sizeimage;
400 sizes[i] = sizeimage;
406 /* Single planar case: Make sure the image size is large enough */
407 sizeimage = dma->format.fmt.pix.sizeimage;
409 return sizes[0] < sizeimage ? -EINVAL : 0;
412 sizes[0] = sizeimage;
417 static int xvip_dma_buffer_prepare(struct vb2_buffer *vb)
419 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
420 struct xvip_dma *dma = vb2_get_drv_priv(vb->vb2_queue);
421 struct xvip_dma_buffer *buf = to_xvip_dma_buffer(vbuf);
428 static void xvip_dma_buffer_queue(struct vb2_buffer *vb)
430 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
431 struct xvip_dma *dma = vb2_get_drv_priv(vb->vb2_queue);
432 struct xvip_dma_buffer *buf = to_xvip_dma_buffer(vbuf);
433 struct dma_async_tx_descriptor *desc;
434 dma_addr_t addr = vb2_dma_contig_plane_dma_addr(vb, 0);
437 u32 padding_factor_nume, padding_factor_deno, bpl_nume, bpl_deno;
439 if (dma->queue.type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
440 dma->queue.type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
441 flags = DMA_PREP_INTERRUPT | DMA_CTRL_ACK;
442 dma->xt.dir = DMA_DEV_TO_MEM;
443 dma->xt.src_sgl = false;
444 dma->xt.dst_sgl = true;
445 dma->xt.dst_start = addr;
446 } else if (dma->queue.type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
447 dma->queue.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
448 flags = DMA_PREP_INTERRUPT | DMA_CTRL_ACK;
449 dma->xt.dir = DMA_MEM_TO_DEV;
450 dma->xt.src_sgl = true;
451 dma->xt.dst_sgl = false;
452 dma->xt.src_start = addr;
456 * DMA IP supports only 2 planes, so one datachunk is sufficient
457 * to get start address of 2nd plane
459 if (V4L2_TYPE_IS_MULTIPLANAR(dma->format.type)) {
460 struct v4l2_pix_format_mplane *pix_mp;
462 pix_mp = &dma->format.fmt.pix_mp;
463 xilinx_xdma_v4l2_config(dma->dma, pix_mp->pixelformat);
464 xvip_width_padding_factor(pix_mp->pixelformat,
465 &padding_factor_nume,
466 &padding_factor_deno);
467 xvip_bpl_scaling_factor(pix_mp->pixelformat, &bpl_nume,
469 dma->xt.frame_size = dma->fmtinfo->num_planes;
470 dma->sgl[0].size = (pix_mp->width * dma->fmtinfo->bpl_factor *
471 padding_factor_nume * bpl_nume) /
472 (padding_factor_deno * bpl_deno);
473 dma->sgl[0].icg = pix_mp->plane_fmt[0].bytesperline -
475 dma->xt.numf = pix_mp->height;
478 * dst_icg is the number of bytes to jump after last luma addr
479 * and before first chroma addr
482 /* Handling contiguous data with mplanes */
483 if (dma->fmtinfo->buffers == 1) {
484 dma->sgl[0].dst_icg = 0;
486 /* Handling non-contiguous data with mplanes */
487 if (dma->fmtinfo->buffers == 2) {
488 dma_addr_t chroma_addr =
489 vb2_dma_contig_plane_dma_addr(vb, 1);
490 luma_size = pix_mp->plane_fmt[0].bytesperline *
492 if (chroma_addr > addr)
493 dma->sgl[0].dst_icg = chroma_addr -
498 struct v4l2_pix_format *pix;
500 pix = &dma->format.fmt.pix;
501 xvip_width_padding_factor(pix->pixelformat,
502 &padding_factor_nume,
503 &padding_factor_deno);
504 xvip_bpl_scaling_factor(pix->pixelformat, &bpl_nume,
506 dma->xt.frame_size = dma->fmtinfo->num_planes;
507 dma->sgl[0].size = (pix->width * dma->fmtinfo->bpl_factor *
508 padding_factor_nume * bpl_nume) /
509 (padding_factor_deno * bpl_deno);
510 dma->sgl[0].icg = pix->bytesperline - dma->sgl[0].size;
511 dma->xt.numf = pix->height;
512 dma->sgl[0].dst_icg = dma->sgl[0].size;
515 desc = dmaengine_prep_interleaved_dma(dma->dma, &dma->xt, flags);
517 dev_err(dma->xdev->dev, "Failed to prepare DMA transfer\n");
518 vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_ERROR);
521 desc->callback = xvip_dma_complete;
522 desc->callback_param = buf;
524 spin_lock_irq(&dma->queued_lock);
525 list_add_tail(&buf->queue, &dma->queued_bufs);
526 spin_unlock_irq(&dma->queued_lock);
528 dmaengine_submit(desc);
530 if (vb2_is_streaming(&dma->queue))
531 dma_async_issue_pending(dma->dma);
534 static int xvip_dma_start_streaming(struct vb2_queue *vq, unsigned int count)
536 struct xvip_dma *dma = vb2_get_drv_priv(vq);
537 struct xvip_dma_buffer *buf, *nbuf;
538 struct xvip_pipeline *pipe;
544 * Start streaming on the pipeline. No link touching an entity in the
545 * pipeline can be activated or deactivated once streaming is started.
547 * Use the pipeline object embedded in the first DMA object that starts
550 pipe = dma->video.entity.pipe
551 ? to_xvip_pipeline(&dma->video.entity) : &dma->pipe;
553 ret = media_pipeline_start(&dma->video.entity, &pipe->pipe);
557 /* Verify that the configured format matches the output of the
560 ret = xvip_dma_verify_format(dma);
564 ret = xvip_pipeline_prepare(pipe, dma);
568 /* Start the DMA engine. This must be done before starting the blocks
569 * in the pipeline to avoid DMA synchronization issues.
571 dma_async_issue_pending(dma->dma);
573 /* Start the pipeline. */
574 xvip_pipeline_set_stream(pipe, true);
579 dmaengine_terminate_all(dma->dma);
580 media_pipeline_stop(&dma->video.entity);
583 /* Give back all queued buffers to videobuf2. */
584 spin_lock_irq(&dma->queued_lock);
585 list_for_each_entry_safe(buf, nbuf, &dma->queued_bufs, queue) {
586 vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_QUEUED);
587 list_del(&buf->queue);
589 spin_unlock_irq(&dma->queued_lock);
594 static void xvip_dma_stop_streaming(struct vb2_queue *vq)
596 struct xvip_dma *dma = vb2_get_drv_priv(vq);
597 struct xvip_pipeline *pipe = to_xvip_pipeline(&dma->video.entity);
598 struct xvip_dma_buffer *buf, *nbuf;
600 /* Stop the pipeline. */
601 xvip_pipeline_set_stream(pipe, false);
603 /* Stop and reset the DMA engine. */
604 dmaengine_terminate_all(dma->dma);
606 /* Cleanup the pipeline and mark it as being stopped. */
607 xvip_pipeline_cleanup(pipe);
608 media_pipeline_stop(&dma->video.entity);
610 /* Give back all queued buffers to videobuf2. */
611 spin_lock_irq(&dma->queued_lock);
612 list_for_each_entry_safe(buf, nbuf, &dma->queued_bufs, queue) {
613 vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_ERROR);
614 list_del(&buf->queue);
616 spin_unlock_irq(&dma->queued_lock);
619 static const struct vb2_ops xvip_dma_queue_qops = {
620 .queue_setup = xvip_dma_queue_setup,
621 .buf_prepare = xvip_dma_buffer_prepare,
622 .buf_queue = xvip_dma_buffer_queue,
623 .wait_prepare = vb2_ops_wait_prepare,
624 .wait_finish = vb2_ops_wait_finish,
625 .start_streaming = xvip_dma_start_streaming,
626 .stop_streaming = xvip_dma_stop_streaming,
629 /* -----------------------------------------------------------------------------
634 xvip_dma_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
636 struct v4l2_fh *vfh = file->private_data;
637 struct xvip_dma *dma = to_xvip_dma(vfh->vdev);
639 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
640 | dma->xdev->v4l2_caps;
642 cap->device_caps = V4L2_CAP_STREAMING;
643 switch (dma->queue.type) {
644 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
645 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE_MPLANE;
647 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
648 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
650 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
651 cap->device_caps |= V4L2_CAP_VIDEO_OUTPUT_MPLANE;
653 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
654 cap->device_caps |= V4L2_CAP_VIDEO_OUTPUT;
658 strlcpy(cap->driver, "xilinx-vipp", sizeof(cap->driver));
659 strlcpy(cap->card, dma->video.name, sizeof(cap->card));
660 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s:%u",
661 dma->xdev->dev->of_node->name, dma->port);
666 /* FIXME: without this callback function, some applications are not configured
667 * with correct formats, and it results in frames in wrong format. Whether this
668 * callback needs to be required is not clearly defined, so it should be
669 * clarified through the mailing list.
672 xvip_dma_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f)
674 struct v4l2_fh *vfh = file->private_data;
675 struct xvip_dma *dma = to_xvip_dma(vfh->vdev);
676 struct v4l2_subdev *subdev;
677 struct v4l2_subdev_format v4l_fmt;
679 const struct xvip_video_format *fmt;
681 if (V4L2_TYPE_IS_MULTIPLANAR(dma->format.type)) {
682 /* establish media pad format */
683 subdev = xvip_dma_remote_subdev(&dma->pad, &v4l_fmt.pad);
687 v4l_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
688 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &v4l_fmt);
690 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
692 /* has media pad value changed? */
693 if (v4l_fmt.format.code != dma->remote_subdev_med_bus ||
694 !dma->remote_subdev_med_bus) {
695 u32 i, fmt_cnt, *fmts;
696 /* re-generate legal list of fourcc codes */
697 dma->poss_v4l2_fmt_cnt = 0;
698 dma->remote_subdev_med_bus = v4l_fmt.format.code;
699 err = xilinx_xdma_get_v4l2_vid_fmts(dma->dma, &fmt_cnt,
703 if (!dma->poss_v4l2_fmts) {
704 dma->poss_v4l2_fmts =
705 devm_kzalloc(&dma->video.dev,
706 sizeof(u32) * fmt_cnt,
708 if (!dma->poss_v4l2_fmts)
711 for (i = 0; i < fmt_cnt; i++) {
712 fmt = xvip_get_format_by_fourcc(fmts[i]);
716 if (fmt->code != dma->remote_subdev_med_bus)
719 dma->poss_v4l2_fmts[dma->poss_v4l2_fmt_cnt++] =
724 /* Return err if index is greater than count of legal values */
725 if (f->index >= dma->poss_v4l2_fmt_cnt)
728 /* Else return pix format in table */
729 fmt = xvip_get_format_by_fourcc(dma->poss_v4l2_fmts[f->index]);
733 f->pixelformat = fmt->fourcc;
734 strlcpy(f->description, fmt->description,
735 sizeof(f->description));
738 /* Single plane formats */
742 f->pixelformat = dma->format.fmt.pix.pixelformat;
743 strlcpy(f->description, dma->fmtinfo->description,
744 sizeof(f->description));
749 xvip_dma_get_format(struct file *file, void *fh, struct v4l2_format *format)
751 struct v4l2_fh *vfh = file->private_data;
752 struct xvip_dma *dma = to_xvip_dma(vfh->vdev);
754 if (V4L2_TYPE_IS_MULTIPLANAR(dma->format.type))
755 format->fmt.pix_mp = dma->format.fmt.pix_mp;
757 format->fmt.pix = dma->format.fmt.pix;
763 __xvip_dma_try_format(struct xvip_dma *dma,
764 struct v4l2_format *format,
765 const struct xvip_video_format **fmtinfo)
767 const struct xvip_video_format *info;
768 unsigned int min_width;
769 unsigned int max_width;
770 unsigned int min_bpl;
771 unsigned int max_bpl;
775 unsigned int i, hsub, vsub, plane_width, plane_height;
777 unsigned int padding_factor_nume, padding_factor_deno;
778 unsigned int bpl_nume, bpl_deno;
780 /* Retrieve format information and select the default format if the
781 * requested format isn't supported.
783 if (V4L2_TYPE_IS_MULTIPLANAR(dma->format.type))
784 fourcc = format->fmt.pix_mp.pixelformat;
786 fourcc = format->fmt.pix.pixelformat;
788 info = xvip_get_format_by_fourcc(fourcc);
791 info = xvip_get_format_by_fourcc(XVIP_DMA_DEF_FORMAT);
793 xvip_width_padding_factor(info->fourcc, &padding_factor_nume,
794 &padding_factor_deno);
795 xvip_bpl_scaling_factor(info->fourcc, &bpl_nume, &bpl_deno);
797 /* The transfer alignment requirements are expressed in bytes. Compute
798 * the minimum and maximum values, clamp the requested width and convert
801 align = lcm(dma->align, info->bpp >> 3);
802 min_width = roundup(XVIP_DMA_MIN_WIDTH, align);
803 max_width = rounddown(XVIP_DMA_MAX_WIDTH, align);
805 if (V4L2_TYPE_IS_MULTIPLANAR(dma->format.type)) {
806 struct v4l2_pix_format_mplane *pix_mp;
807 struct v4l2_plane_pix_format *plane_fmt;
809 pix_mp = &format->fmt.pix_mp;
810 plane_fmt = pix_mp->plane_fmt;
811 pix_mp->field = V4L2_FIELD_NONE;
812 width = rounddown(pix_mp->width * info->bpl_factor, align);
813 pix_mp->width = clamp(width, min_width, max_width) /
815 pix_mp->height = clamp(pix_mp->height, XVIP_DMA_MIN_HEIGHT,
816 XVIP_DMA_MAX_HEIGHT);
819 * Clamp the requested bytes per line value. If the maximum
820 * bytes per line value is zero, the module doesn't support
821 * user configurable line sizes. Override the requested value
822 * with the minimum in that case.
825 max_bpl = rounddown(XVIP_DMA_MAX_WIDTH, dma->align);
827 /* Handling contiguous data with mplanes */
828 if (info->buffers == 1) {
829 min_bpl = (pix_mp->width * info->bpl_factor *
830 padding_factor_nume * bpl_nume) /
831 (padding_factor_deno * bpl_deno);
832 min_bpl = roundup(min_bpl, dma->align);
833 bpl = roundup(plane_fmt[0].bytesperline, dma->align);
834 plane_fmt[0].bytesperline = clamp(bpl, min_bpl,
837 if (info->num_planes == 1) {
838 /* Single plane formats */
839 plane_fmt[0].sizeimage =
840 plane_fmt[0].bytesperline *
843 /* Multi plane formats */
844 plane_fmt[0].sizeimage =
845 DIV_ROUND_UP(plane_fmt[0].bytesperline *
850 /* Handling non-contiguous data with mplanes */
853 for (i = 0; i < info->num_planes; i++) {
854 plane_width = pix_mp->width / (i ? hsub : 1);
855 plane_height = pix_mp->height / (i ? vsub : 1);
856 min_bpl = (plane_width * info->bpl_factor *
857 padding_factor_nume * bpl_nume) /
858 (padding_factor_deno * bpl_deno);
859 bpl = rounddown(plane_fmt[i].bytesperline,
861 plane_fmt[i].bytesperline =
862 clamp(bpl, min_bpl, max_bpl);
863 plane_fmt[i].sizeimage =
864 plane_fmt[i].bytesperline *
869 struct v4l2_pix_format *pix;
871 pix = &format->fmt.pix;
872 pix->field = V4L2_FIELD_NONE;
874 width = rounddown(pix->width * info->bpl_factor, align);
875 pix->width = clamp(width, min_width, max_width) /
877 pix->height = clamp(pix->height, XVIP_DMA_MIN_HEIGHT,
878 XVIP_DMA_MAX_HEIGHT);
880 min_bpl = pix->width * info->bpl_factor;
881 max_bpl = rounddown(XVIP_DMA_MAX_WIDTH, dma->align);
882 bpl = rounddown(pix->bytesperline, dma->align);
883 pix->bytesperline = clamp(bpl, min_bpl, max_bpl);
884 pix->sizeimage = pix->width * pix->height * info->bpp / 8;
892 xvip_dma_try_format(struct file *file, void *fh, struct v4l2_format *format)
894 struct v4l2_fh *vfh = file->private_data;
895 struct xvip_dma *dma = to_xvip_dma(vfh->vdev);
897 __xvip_dma_try_format(dma, format, NULL);
902 xvip_dma_set_format(struct file *file, void *fh, struct v4l2_format *format)
904 struct v4l2_fh *vfh = file->private_data;
905 struct xvip_dma *dma = to_xvip_dma(vfh->vdev);
906 const struct xvip_video_format *info;
908 __xvip_dma_try_format(dma, format, &info);
910 if (vb2_is_busy(&dma->queue))
913 if (V4L2_TYPE_IS_MULTIPLANAR(dma->format.type))
914 dma->format.fmt.pix_mp = format->fmt.pix_mp;
916 dma->format.fmt.pix = format->fmt.pix;
923 static const struct v4l2_ioctl_ops xvip_dma_ioctl_ops = {
924 .vidioc_querycap = xvip_dma_querycap,
925 .vidioc_enum_fmt_vid_cap = xvip_dma_enum_format,
926 .vidioc_enum_fmt_vid_cap_mplane = xvip_dma_enum_format,
927 .vidioc_g_fmt_vid_cap = xvip_dma_get_format,
928 .vidioc_g_fmt_vid_cap_mplane = xvip_dma_get_format,
929 .vidioc_g_fmt_vid_out = xvip_dma_get_format,
930 .vidioc_s_fmt_vid_cap = xvip_dma_set_format,
931 .vidioc_s_fmt_vid_cap_mplane = xvip_dma_set_format,
932 .vidioc_s_fmt_vid_out = xvip_dma_set_format,
933 .vidioc_try_fmt_vid_cap = xvip_dma_try_format,
934 .vidioc_try_fmt_vid_cap_mplane = xvip_dma_try_format,
935 .vidioc_try_fmt_vid_out = xvip_dma_try_format,
936 .vidioc_reqbufs = vb2_ioctl_reqbufs,
937 .vidioc_querybuf = vb2_ioctl_querybuf,
938 .vidioc_qbuf = vb2_ioctl_qbuf,
939 .vidioc_dqbuf = vb2_ioctl_dqbuf,
940 .vidioc_create_bufs = vb2_ioctl_create_bufs,
941 .vidioc_expbuf = vb2_ioctl_expbuf,
942 .vidioc_streamon = vb2_ioctl_streamon,
943 .vidioc_streamoff = vb2_ioctl_streamoff,
946 /* -----------------------------------------------------------------------------
947 * V4L2 file operations
950 static const struct v4l2_file_operations xvip_dma_fops = {
951 .owner = THIS_MODULE,
952 .unlocked_ioctl = video_ioctl2,
953 .open = v4l2_fh_open,
954 .release = vb2_fop_release,
955 .poll = vb2_fop_poll,
956 .mmap = vb2_fop_mmap,
959 /* -----------------------------------------------------------------------------
960 * Xilinx Video DMA Core
963 int xvip_dma_init(struct xvip_composite_device *xdev, struct xvip_dma *dma,
964 enum v4l2_buf_type type, unsigned int port)
968 u32 i, hsub, vsub, width, height;
972 mutex_init(&dma->lock);
973 mutex_init(&dma->pipe.lock);
974 INIT_LIST_HEAD(&dma->queued_bufs);
975 spin_lock_init(&dma->queued_lock);
977 dma->fmtinfo = xvip_get_format_by_fourcc(XVIP_DMA_DEF_FORMAT);
978 dma->format.type = type;
980 if (V4L2_TYPE_IS_MULTIPLANAR(type)) {
981 struct v4l2_pix_format_mplane *pix_mp;
983 pix_mp = &dma->format.fmt.pix_mp;
984 pix_mp->pixelformat = dma->fmtinfo->fourcc;
985 pix_mp->colorspace = V4L2_COLORSPACE_SRGB;
986 pix_mp->field = V4L2_FIELD_NONE;
987 pix_mp->width = XVIP_DMA_DEF_WIDTH;
989 /* Handling contiguous data with mplanes */
990 if (dma->fmtinfo->buffers == 1) {
991 pix_mp->plane_fmt[0].bytesperline =
992 pix_mp->width * dma->fmtinfo->bpl_factor;
993 pix_mp->plane_fmt[0].sizeimage =
994 pix_mp->width * pix_mp->height *
995 dma->fmtinfo->bpp / 8;
997 /* Handling non-contiguous data with mplanes */
998 hsub = dma->fmtinfo->hsub;
999 vsub = dma->fmtinfo->vsub;
1000 for (i = 0; i < dma->fmtinfo->buffers; i++) {
1001 width = pix_mp->width / (i ? hsub : 1);
1002 height = pix_mp->height / (i ? vsub : 1);
1003 pix_mp->plane_fmt[i].bytesperline =
1004 width * dma->fmtinfo->bpl_factor;
1005 pix_mp->plane_fmt[i].sizeimage = width * height;
1009 struct v4l2_pix_format *pix;
1011 pix = &dma->format.fmt.pix;
1012 pix->pixelformat = dma->fmtinfo->fourcc;
1013 pix->colorspace = V4L2_COLORSPACE_SRGB;
1014 pix->field = V4L2_FIELD_NONE;
1015 pix->width = XVIP_DMA_DEF_WIDTH;
1016 pix->height = XVIP_DMA_DEF_HEIGHT;
1017 pix->bytesperline = pix->width * dma->fmtinfo->bpl_factor;
1019 pix->width * pix->height * dma->fmtinfo->bpp / 8;
1022 /* Initialize the media entity... */
1023 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1024 type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1025 dma->pad.flags = MEDIA_PAD_FL_SINK;
1027 dma->pad.flags = MEDIA_PAD_FL_SOURCE;
1029 ret = media_entity_pads_init(&dma->video.entity, 1, &dma->pad);
1033 /* ... and the video node... */
1034 dma->video.fops = &xvip_dma_fops;
1035 dma->video.v4l2_dev = &xdev->v4l2_dev;
1036 dma->video.queue = &dma->queue;
1037 snprintf(dma->video.name, sizeof(dma->video.name), "%s %s %u",
1038 xdev->dev->of_node->name,
1039 (type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1040 type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1041 ? "output" : "input",
1044 dma->video.vfl_type = VFL_TYPE_GRABBER;
1045 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1046 type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1047 dma->video.vfl_dir = VFL_DIR_RX;
1049 dma->video.vfl_dir = VFL_DIR_TX;
1051 dma->video.release = video_device_release_empty;
1052 dma->video.ioctl_ops = &xvip_dma_ioctl_ops;
1053 dma->video.lock = &dma->lock;
1055 video_set_drvdata(&dma->video, dma);
1057 /* ... and the buffers queue... */
1058 /* Don't enable VB2_READ and VB2_WRITE, as using the read() and write()
1059 * V4L2 APIs would be inefficient. Testing on the command line with a
1060 * 'cat /dev/video?' thus won't be possible, but given that the driver
1061 * anyway requires a test tool to setup the pipeline before any video
1062 * stream can be started, requiring a specific V4L2 test tool as well
1063 * instead of 'cat' isn't really a drawback.
1065 dma->queue.type = type;
1066 dma->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1067 dma->queue.lock = &dma->lock;
1068 dma->queue.drv_priv = dma;
1069 dma->queue.buf_struct_size = sizeof(struct xvip_dma_buffer);
1070 dma->queue.ops = &xvip_dma_queue_qops;
1071 dma->queue.mem_ops = &vb2_dma_contig_memops;
1072 dma->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
1073 | V4L2_BUF_FLAG_TSTAMP_SRC_EOF;
1074 dma->queue.dev = dma->xdev->dev;
1075 ret = vb2_queue_init(&dma->queue);
1077 dev_err(dma->xdev->dev, "failed to initialize VB2 queue\n");
1081 /* ... and the DMA channel. */
1082 snprintf(name, sizeof(name), "port%u", port);
1083 dma->dma = dma_request_chan(dma->xdev->dev, name);
1084 if (IS_ERR(dma->dma)) {
1085 ret = PTR_ERR(dma->dma);
1086 if (ret != -EPROBE_DEFER)
1087 dev_err(dma->xdev->dev,
1088 "No Video DMA channel found");
1092 dma->align = 1 << dma->dma->device->copy_align;
1094 ret = video_register_device(&dma->video, VFL_TYPE_GRABBER, -1);
1096 dev_err(dma->xdev->dev, "failed to register video device\n");
1103 xvip_dma_cleanup(dma);
1107 void xvip_dma_cleanup(struct xvip_dma *dma)
1109 if (video_is_registered(&dma->video))
1110 video_unregister_device(&dma->video);
1112 if (!IS_ERR(dma->dma))
1113 dma_release_channel(dma->dma);
1115 media_entity_cleanup(&dma->video.entity);
1117 mutex_destroy(&dma->lock);
1118 mutex_destroy(&dma->pipe.lock);