]> rtime.felk.cvut.cz Git - zynq/linux.git/blob - drivers/media/platform/xilinx/xilinx-dma.c
v4l: xilinx: dma: Fix sizeimage calculation
[zynq/linux.git] / drivers / media / platform / xilinx / xilinx-dma.c
1 /*
2  * Xilinx Video DMA
3  *
4  * Copyright (C) 2013-2015 Ideas on Board
5  * Copyright (C) 2013-2015 Xilinx, Inc.
6  *
7  * Contacts: Hyun Kwon <hyun.kwon@xilinx.com>
8  *           Laurent Pinchart <laurent.pinchart@ideasonboard.com>
9  *
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.
13  */
14
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>
20 #include <linux/of.h>
21 #include <linux/slab.h>
22
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>
28
29 #include "xilinx-dma.h"
30 #include "xilinx-vip.h"
31 #include "xilinx-vipp.h"
32
33 #define XVIP_DMA_DEF_FORMAT             V4L2_PIX_FMT_YUYV
34 #define XVIP_DMA_DEF_WIDTH              1920
35 #define XVIP_DMA_DEF_HEIGHT             1080
36
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
42
43 /* -----------------------------------------------------------------------------
44  * Helper functions
45  */
46
47 static struct v4l2_subdev *
48 xvip_dma_remote_subdev(struct media_pad *local, u32 *pad)
49 {
50         struct media_pad *remote;
51
52         remote = media_entity_remote_pad(local);
53         if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
54                 return NULL;
55
56         if (pad)
57                 *pad = remote->index;
58
59         return media_entity_to_v4l2_subdev(remote->entity);
60 }
61
62 static int xvip_dma_verify_format(struct xvip_dma *dma)
63 {
64         struct v4l2_subdev_format fmt;
65         struct v4l2_subdev *subdev;
66         int ret;
67         int width, height;
68
69         subdev = xvip_dma_remote_subdev(&dma->pad, &fmt.pad);
70         if (subdev == NULL)
71                 return -EPIPE;
72
73         fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
74         ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
75         if (ret < 0)
76                 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
77
78         if (dma->fmtinfo->code != fmt.format.code)
79                 return -EINVAL;
80
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;
84         } else {
85                 width = dma->format.fmt.pix.width;
86                 height = dma->format.fmt.pix.height;
87         }
88
89         if (width != fmt.format.width || height != fmt.format.height)
90                 return -EINVAL;
91
92         return 0;
93 }
94
95 /* -----------------------------------------------------------------------------
96  * Pipeline Stream Management
97  */
98
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)
102 {
103         unsigned int i;
104
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.
107          */
108         if (source == NULL)
109                 return &entity->pads[0];
110
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];
114
115                 if (!(sink->flags & MEDIA_PAD_FL_SINK))
116                         continue;
117
118                 if (sink == source)
119                         continue;
120
121                 if (media_entity_has_route(entity, sink->index, source->index))
122                         return sink;
123         }
124
125         return NULL;
126 }
127
128 /**
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
132  *
133  * Walk the entities chain starting at the pipeline output video node and start
134  * or stop all of them.
135  *
136  * Return: 0 if successful, or the return value of the failed video::s_stream
137  * operation otherwise.
138  */
139 static int xvip_pipeline_start_stop(struct xvip_pipeline *pipe, bool start)
140 {
141         struct xvip_dma *dma = pipe->output;
142         struct media_entity *entity;
143         struct media_pad *pad;
144         struct v4l2_subdev *subdev;
145         int ret;
146
147         entity = &dma->video.entity;
148         pad = NULL;
149
150         while (1) {
151                 pad = xvip_get_entity_sink(entity, pad);
152                 if (pad == NULL)
153                         break;
154
155                 if (!(pad->flags & MEDIA_PAD_FL_SINK))
156                         break;
157
158                 pad = media_entity_remote_pad(pad);
159                 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
160                         break;
161
162                 entity = pad->entity;
163                 subdev = media_entity_to_v4l2_subdev(entity);
164
165                 ret = v4l2_subdev_call(subdev, video, s_stream, start);
166                 if (start && ret < 0 && ret != -ENOIOCTLCMD)
167                         return ret;
168         }
169
170         return 0;
171 }
172
173 /**
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
177  *
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
183  * to be enabled.
184  *
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
188  * pipeline.
189  *
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.
193  *
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.
197  */
198 static int xvip_pipeline_set_stream(struct xvip_pipeline *pipe, bool on)
199 {
200         int ret = 0;
201
202         mutex_lock(&pipe->lock);
203
204         if (on) {
205                 if (pipe->stream_count == pipe->num_dmas - 1) {
206                         ret = xvip_pipeline_start_stop(pipe, true);
207                         if (ret < 0)
208                                 goto done;
209                 }
210                 pipe->stream_count++;
211         } else {
212                 if (--pipe->stream_count == 0)
213                         xvip_pipeline_start_stop(pipe, false);
214         }
215
216 done:
217         mutex_unlock(&pipe->lock);
218         return ret;
219 }
220
221 static int xvip_pipeline_validate(struct xvip_pipeline *pipe,
222                                   struct xvip_dma *start)
223 {
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;
229         int ret;
230
231         mutex_lock(&mdev->graph_mutex);
232
233         /* Walk the graph to locate the video nodes. */
234         ret = media_graph_walk_init(&graph, mdev);
235         if (ret) {
236                 mutex_unlock(&mdev->graph_mutex);
237                 return ret;
238         }
239
240         media_graph_walk_start(&graph, entity);
241
242         while ((entity = media_graph_walk_next(&graph))) {
243                 struct xvip_dma *dma;
244
245                 if (entity->function != MEDIA_ENT_F_IO_V4L)
246                         continue;
247
248                 dma = to_xvip_dma(media_entity_to_video_device(entity));
249
250                 if (dma->pad.flags & MEDIA_PAD_FL_SINK) {
251                         pipe->output = dma;
252                         num_outputs++;
253                 } else {
254                         num_inputs++;
255                 }
256         }
257
258         mutex_unlock(&mdev->graph_mutex);
259
260         media_graph_walk_cleanup(&graph);
261
262         /* We need exactly one output and zero or one input. */
263         if (num_outputs != 1 || num_inputs > 1)
264                 return -EPIPE;
265
266         pipe->num_dmas = num_inputs + num_outputs;
267
268         return 0;
269 }
270
271 static void __xvip_pipeline_cleanup(struct xvip_pipeline *pipe)
272 {
273         pipe->num_dmas = 0;
274         pipe->output = NULL;
275 }
276
277 /**
278  * xvip_pipeline_cleanup - Cleanup the pipeline after streaming
279  * @pipe: the pipeline
280  *
281  * Decrease the pipeline use count and clean it up if we were the last user.
282  */
283 static void xvip_pipeline_cleanup(struct xvip_pipeline *pipe)
284 {
285         mutex_lock(&pipe->lock);
286
287         /* If we're the last user clean up the pipeline. */
288         if (--pipe->use_count == 0)
289                 __xvip_pipeline_cleanup(pipe);
290
291         mutex_unlock(&pipe->lock);
292 }
293
294 /**
295  * xvip_pipeline_prepare - Prepare the pipeline for streaming
296  * @pipe: the pipeline
297  * @dma: DMA engine at one end of the pipeline
298  *
299  * Validate the pipeline if no user exists yet, otherwise just increase the use
300  * count.
301  *
302  * Return: 0 if successful or -EPIPE if the pipeline is not valid.
303  */
304 static int xvip_pipeline_prepare(struct xvip_pipeline *pipe,
305                                  struct xvip_dma *dma)
306 {
307         int ret;
308
309         mutex_lock(&pipe->lock);
310
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);
314                 if (ret < 0) {
315                         __xvip_pipeline_cleanup(pipe);
316                         goto done;
317                 }
318         }
319
320         pipe->use_count++;
321         ret = 0;
322
323 done:
324         mutex_unlock(&pipe->lock);
325         return ret;
326 }
327
328 /* -----------------------------------------------------------------------------
329  * videobuf2 queue operations
330  */
331
332 /**
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
337  */
338 struct xvip_dma_buffer {
339         struct vb2_v4l2_buffer buf;
340         struct list_head queue;
341         struct xvip_dma *dma;
342 };
343
344 #define to_xvip_dma_buffer(vb)  container_of(vb, struct xvip_dma_buffer, buf)
345
346 static void xvip_dma_complete(void *param)
347 {
348         struct xvip_dma_buffer *buf = param;
349         struct xvip_dma *dma = buf->dma;
350         int i, sizeimage;
351
352         spin_lock(&dma->queued_lock);
353         list_del(&buf->queue);
354         spin_unlock(&dma->queued_lock);
355
356         buf->buf.field = V4L2_FIELD_NONE;
357         buf->buf.sequence = dma->sequence++;
358         buf->buf.vb2_buf.timestamp = ktime_get_ns();
359
360         if (V4L2_TYPE_IS_MULTIPLANAR(dma->format.type)) {
361                 for (i = 0; i < dma->fmtinfo->buffers; i++) {
362                         sizeimage =
363                                 dma->format.fmt.pix_mp.plane_fmt[i].sizeimage;
364                         vb2_set_plane_payload(&buf->buf.vb2_buf, i, sizeimage);
365                 }
366         } else {
367                 sizeimage = dma->format.fmt.pix.sizeimage;
368                 vb2_set_plane_payload(&buf->buf.vb2_buf, 0, sizeimage);
369         }
370
371         vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_DONE);
372 }
373
374 static int
375 xvip_dma_queue_setup(struct vb2_queue *vq,
376                      unsigned int *nbuffers, unsigned int *nplanes,
377                      unsigned int sizes[], struct device *alloc_devs[])
378 {
379         struct xvip_dma *dma = vb2_get_drv_priv(vq);
380         u8 i;
381         int sizeimage;
382
383         /* Multi planar case: Make sure the image size is large enough */
384         if (V4L2_TYPE_IS_MULTIPLANAR(dma->format.type)) {
385                 if (*nplanes) {
386                         if (*nplanes != dma->format.fmt.pix_mp.num_planes)
387                                 return -EINVAL;
388
389                         for (i = 0; i < *nplanes; i++) {
390                                 sizeimage =
391                                   dma->format.fmt.pix_mp.plane_fmt[i].sizeimage;
392                                 if (sizes[i] < sizeimage)
393                                         return -EINVAL;
394                         }
395                 } else {
396                         *nplanes = dma->fmtinfo->buffers;
397                         for (i = 0; i < dma->fmtinfo->buffers; i++) {
398                                 sizeimage =
399                                   dma->format.fmt.pix_mp.plane_fmt[i].sizeimage;
400                                 sizes[i] = sizeimage;
401                         }
402                 }
403                 return 0;
404         }
405
406         /* Single planar case: Make sure the image size is large enough */
407         sizeimage = dma->format.fmt.pix.sizeimage;
408         if (*nplanes == 1)
409                 return sizes[0] < sizeimage ? -EINVAL : 0;
410
411         *nplanes = 1;
412         sizes[0] = sizeimage;
413
414         return 0;
415 }
416
417 static int xvip_dma_buffer_prepare(struct vb2_buffer *vb)
418 {
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);
422
423         buf->dma = dma;
424
425         return 0;
426 }
427
428 static void xvip_dma_buffer_queue(struct vb2_buffer *vb)
429 {
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);
435         u32 flags;
436         u32 luma_size;
437         u32 padding_factor_nume, padding_factor_deno, bpl_nume, bpl_deno;
438
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;
453         }
454
455         /*
456          * DMA IP supports only 2 planes, so one datachunk is sufficient
457          * to get start address of 2nd plane
458          */
459         if (V4L2_TYPE_IS_MULTIPLANAR(dma->format.type)) {
460                 struct v4l2_pix_format_mplane *pix_mp;
461
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,
468                                         &bpl_deno);
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 -
474                                                         dma->sgl[0].size;
475                 dma->xt.numf = pix_mp->height;
476
477                 /*
478                  * dst_icg is the number of bytes to jump after last luma addr
479                  * and before first chroma addr
480                  */
481
482                 /* Handling contiguous data with mplanes */
483                 if (dma->fmtinfo->buffers == 1) {
484                         dma->sgl[0].dst_icg = 0;
485                 } else {
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 *
491                                             dma->xt.numf;
492                                 if (chroma_addr > addr)
493                                         dma->sgl[0].dst_icg = chroma_addr -
494                                                               addr - luma_size;
495                                 }
496                 }
497         } else {
498                 struct v4l2_pix_format *pix;
499
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,
505                                         &bpl_deno);
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;
513         }
514
515         desc = dmaengine_prep_interleaved_dma(dma->dma, &dma->xt, flags);
516         if (!desc) {
517                 dev_err(dma->xdev->dev, "Failed to prepare DMA transfer\n");
518                 vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_ERROR);
519                 return;
520         }
521         desc->callback = xvip_dma_complete;
522         desc->callback_param = buf;
523
524         spin_lock_irq(&dma->queued_lock);
525         list_add_tail(&buf->queue, &dma->queued_bufs);
526         spin_unlock_irq(&dma->queued_lock);
527
528         dmaengine_submit(desc);
529
530         if (vb2_is_streaming(&dma->queue))
531                 dma_async_issue_pending(dma->dma);
532 }
533
534 static int xvip_dma_start_streaming(struct vb2_queue *vq, unsigned int count)
535 {
536         struct xvip_dma *dma = vb2_get_drv_priv(vq);
537         struct xvip_dma_buffer *buf, *nbuf;
538         struct xvip_pipeline *pipe;
539         int ret;
540
541         dma->sequence = 0;
542
543         /*
544          * Start streaming on the pipeline. No link touching an entity in the
545          * pipeline can be activated or deactivated once streaming is started.
546          *
547          * Use the pipeline object embedded in the first DMA object that starts
548          * streaming.
549          */
550         pipe = dma->video.entity.pipe
551              ? to_xvip_pipeline(&dma->video.entity) : &dma->pipe;
552
553         ret = media_pipeline_start(&dma->video.entity, &pipe->pipe);
554         if (ret < 0)
555                 goto error;
556
557         /* Verify that the configured format matches the output of the
558          * connected subdev.
559          */
560         ret = xvip_dma_verify_format(dma);
561         if (ret < 0)
562                 goto error_stop;
563
564         ret = xvip_pipeline_prepare(pipe, dma);
565         if (ret < 0)
566                 goto error_stop;
567
568         /* Start the DMA engine. This must be done before starting the blocks
569          * in the pipeline to avoid DMA synchronization issues.
570          */
571         dma_async_issue_pending(dma->dma);
572
573         /* Start the pipeline. */
574         xvip_pipeline_set_stream(pipe, true);
575
576         return 0;
577
578 error_stop:
579         dmaengine_terminate_all(dma->dma);
580         media_pipeline_stop(&dma->video.entity);
581
582 error:
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);
588         }
589         spin_unlock_irq(&dma->queued_lock);
590
591         return ret;
592 }
593
594 static void xvip_dma_stop_streaming(struct vb2_queue *vq)
595 {
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;
599
600         /* Stop the pipeline. */
601         xvip_pipeline_set_stream(pipe, false);
602
603         /* Stop and reset the DMA engine. */
604         dmaengine_terminate_all(dma->dma);
605
606         /* Cleanup the pipeline and mark it as being stopped. */
607         xvip_pipeline_cleanup(pipe);
608         media_pipeline_stop(&dma->video.entity);
609
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);
615         }
616         spin_unlock_irq(&dma->queued_lock);
617 }
618
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,
627 };
628
629 /* -----------------------------------------------------------------------------
630  * V4L2 ioctls
631  */
632
633 static int
634 xvip_dma_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
635 {
636         struct v4l2_fh *vfh = file->private_data;
637         struct xvip_dma *dma = to_xvip_dma(vfh->vdev);
638
639         cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
640                           | dma->xdev->v4l2_caps;
641
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;
646                 break;
647         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
648                 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
649                 break;
650         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
651                 cap->device_caps |= V4L2_CAP_VIDEO_OUTPUT_MPLANE;
652                 break;
653         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
654                 cap->device_caps |= V4L2_CAP_VIDEO_OUTPUT;
655                 break;
656         }
657
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);
662
663         return 0;
664 }
665
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.
670  */
671 static int
672 xvip_dma_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f)
673 {
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;
678         int err, ret;
679         const struct xvip_video_format *fmt;
680
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);
684                 if (!subdev)
685                         return -EPIPE;
686
687                 v4l_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
688                 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &v4l_fmt);
689                 if (ret < 0)
690                         return ret == -ENOIOCTLCMD ? -EINVAL : ret;
691
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,
700                                                             &fmts);
701                         if (err)
702                                 return err;
703                         if (!dma->poss_v4l2_fmts) {
704                                 dma->poss_v4l2_fmts =
705                                         devm_kzalloc(&dma->video.dev,
706                                                      sizeof(u32) * fmt_cnt,
707                                                      GFP_KERNEL);
708                                 if (!dma->poss_v4l2_fmts)
709                                         return -ENOMEM;
710                         }
711                         for (i = 0; i < fmt_cnt; i++) {
712                                 fmt = xvip_get_format_by_fourcc(fmts[i]);
713                                 if (IS_ERR(fmt))
714                                         return PTR_ERR(fmt);
715
716                                 if (fmt->code != dma->remote_subdev_med_bus)
717                                         continue;
718
719                                 dma->poss_v4l2_fmts[dma->poss_v4l2_fmt_cnt++] =
720                                                                         fmts[i];
721                         }
722                 }
723
724                 /* Return err if index is greater than count of legal values */
725                 if (f->index >= dma->poss_v4l2_fmt_cnt)
726                         return -EINVAL;
727
728                 /* Else return pix format in table */
729                 fmt = xvip_get_format_by_fourcc(dma->poss_v4l2_fmts[f->index]);
730                 if (IS_ERR(fmt))
731                         return PTR_ERR(fmt);
732
733                 f->pixelformat = fmt->fourcc;
734                 strlcpy(f->description, fmt->description,
735                         sizeof(f->description));
736         }
737
738         /* Single plane formats */
739         if (f->index > 0)
740                 return -EINVAL;
741
742         f->pixelformat = dma->format.fmt.pix.pixelformat;
743         strlcpy(f->description, dma->fmtinfo->description,
744                 sizeof(f->description));
745         return 0;
746 }
747
748 static int
749 xvip_dma_get_format(struct file *file, void *fh, struct v4l2_format *format)
750 {
751         struct v4l2_fh *vfh = file->private_data;
752         struct xvip_dma *dma = to_xvip_dma(vfh->vdev);
753
754         if (V4L2_TYPE_IS_MULTIPLANAR(dma->format.type))
755                 format->fmt.pix_mp = dma->format.fmt.pix_mp;
756         else
757                 format->fmt.pix = dma->format.fmt.pix;
758
759         return 0;
760 }
761
762 static void
763 __xvip_dma_try_format(struct xvip_dma *dma,
764                       struct v4l2_format *format,
765                       const struct xvip_video_format **fmtinfo)
766 {
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;
772         unsigned int width;
773         unsigned int align;
774         unsigned int bpl;
775         unsigned int i, hsub, vsub, plane_width, plane_height;
776         unsigned int fourcc;
777         unsigned int padding_factor_nume, padding_factor_deno;
778         unsigned int bpl_nume, bpl_deno;
779
780         /* Retrieve format information and select the default format if the
781          * requested format isn't supported.
782          */
783         if (V4L2_TYPE_IS_MULTIPLANAR(dma->format.type))
784                 fourcc = format->fmt.pix_mp.pixelformat;
785         else
786                 fourcc = format->fmt.pix.pixelformat;
787
788         info = xvip_get_format_by_fourcc(fourcc);
789
790         if (IS_ERR(info))
791                 info = xvip_get_format_by_fourcc(XVIP_DMA_DEF_FORMAT);
792
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);
796
797         /* The transfer alignment requirements are expressed in bytes. Compute
798          * the minimum and maximum values, clamp the requested width and convert
799          * it back to pixels.
800          */
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);
804
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;
808
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) /
814                                 info->bpl_factor;
815                 pix_mp->height = clamp(pix_mp->height, XVIP_DMA_MIN_HEIGHT,
816                                        XVIP_DMA_MAX_HEIGHT);
817
818                 /*
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.
823                  */
824
825                 max_bpl = rounddown(XVIP_DMA_MAX_WIDTH, dma->align);
826
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,
835                                                           max_bpl);
836
837                         if (info->num_planes == 1) {
838                                 /* Single plane formats */
839                                 plane_fmt[0].sizeimage =
840                                                 plane_fmt[0].bytesperline *
841                                                 pix_mp->height;
842                         } else {
843                                 /* Multi plane formats */
844                                 plane_fmt[0].sizeimage =
845                                         DIV_ROUND_UP(plane_fmt[0].bytesperline *
846                                                      pix_mp->height *
847                                                      info->bpp, 8);
848                         }
849                 } else {
850                         /* Handling non-contiguous data with mplanes */
851                         hsub = info->hsub;
852                         vsub = info->vsub;
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,
860                                                 dma->align);
861                                 plane_fmt[i].bytesperline =
862                                                 clamp(bpl, min_bpl, max_bpl);
863                                 plane_fmt[i].sizeimage =
864                                                 plane_fmt[i].bytesperline *
865                                                 plane_height;
866                         }
867                 }
868         } else {
869                 struct v4l2_pix_format *pix;
870
871                 pix = &format->fmt.pix;
872                 pix->field = V4L2_FIELD_NONE;
873
874                 width = rounddown(pix->width * info->bpl_factor, align);
875                 pix->width = clamp(width, min_width, max_width) /
876                              info->bpl_factor;
877                 pix->height = clamp(pix->height, XVIP_DMA_MIN_HEIGHT,
878                                     XVIP_DMA_MAX_HEIGHT);
879
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;
885         }
886
887         if (fmtinfo)
888                 *fmtinfo = info;
889 }
890
891 static int
892 xvip_dma_try_format(struct file *file, void *fh, struct v4l2_format *format)
893 {
894         struct v4l2_fh *vfh = file->private_data;
895         struct xvip_dma *dma = to_xvip_dma(vfh->vdev);
896
897         __xvip_dma_try_format(dma, format, NULL);
898         return 0;
899 }
900
901 static int
902 xvip_dma_set_format(struct file *file, void *fh, struct v4l2_format *format)
903 {
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;
907
908         __xvip_dma_try_format(dma, format, &info);
909
910         if (vb2_is_busy(&dma->queue))
911                 return -EBUSY;
912
913         if (V4L2_TYPE_IS_MULTIPLANAR(dma->format.type))
914                 dma->format.fmt.pix_mp = format->fmt.pix_mp;
915         else
916                 dma->format.fmt.pix = format->fmt.pix;
917
918         dma->fmtinfo = info;
919
920         return 0;
921 }
922
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,
944 };
945
946 /* -----------------------------------------------------------------------------
947  * V4L2 file operations
948  */
949
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,
957 };
958
959 /* -----------------------------------------------------------------------------
960  * Xilinx Video DMA Core
961  */
962
963 int xvip_dma_init(struct xvip_composite_device *xdev, struct xvip_dma *dma,
964                   enum v4l2_buf_type type, unsigned int port)
965 {
966         char name[16];
967         int ret;
968         u32 i, hsub, vsub, width, height;
969
970         dma->xdev = xdev;
971         dma->port = port;
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);
976
977         dma->fmtinfo = xvip_get_format_by_fourcc(XVIP_DMA_DEF_FORMAT);
978         dma->format.type = type;
979
980         if (V4L2_TYPE_IS_MULTIPLANAR(type)) {
981                 struct v4l2_pix_format_mplane *pix_mp;
982
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;
988
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;
996                 } else {
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;
1006                         }
1007                 }
1008         } else {
1009                 struct v4l2_pix_format *pix;
1010
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;
1018                 pix->sizeimage =
1019                         pix->width * pix->height * dma->fmtinfo->bpp / 8;
1020         }
1021
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;
1026         else
1027                 dma->pad.flags = MEDIA_PAD_FL_SOURCE;
1028
1029         ret = media_entity_pads_init(&dma->video.entity, 1, &dma->pad);
1030         if (ret < 0)
1031                 goto error;
1032
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",
1042                  port);
1043
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;
1048         else
1049                 dma->video.vfl_dir = VFL_DIR_TX;
1050
1051         dma->video.release = video_device_release_empty;
1052         dma->video.ioctl_ops = &xvip_dma_ioctl_ops;
1053         dma->video.lock = &dma->lock;
1054
1055         video_set_drvdata(&dma->video, dma);
1056
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.
1064          */
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);
1076         if (ret < 0) {
1077                 dev_err(dma->xdev->dev, "failed to initialize VB2 queue\n");
1078                 goto error;
1079         }
1080
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");
1089                 goto error;
1090         }
1091
1092         dma->align = 1 << dma->dma->device->copy_align;
1093
1094         ret = video_register_device(&dma->video, VFL_TYPE_GRABBER, -1);
1095         if (ret < 0) {
1096                 dev_err(dma->xdev->dev, "failed to register video device\n");
1097                 goto error;
1098         }
1099
1100         return 0;
1101
1102 error:
1103         xvip_dma_cleanup(dma);
1104         return ret;
1105 }
1106
1107 void xvip_dma_cleanup(struct xvip_dma *dma)
1108 {
1109         if (video_is_registered(&dma->video))
1110                 video_unregister_device(&dma->video);
1111
1112         if (!IS_ERR(dma->dma))
1113                 dma_release_channel(dma->dma);
1114
1115         media_entity_cleanup(&dma->video.entity);
1116
1117         mutex_destroy(&dma->lock);
1118         mutex_destroy(&dma->pipe.lock);
1119 }