1 // SPDX-License-Identifier: GPL-2.0-only
3 * i.MX6 Video Data Order Adapter (VDOA)
5 * Copyright (C) 2014 Philipp Zabel
6 * Copyright (C) 2016 Pengutronix, Michael Tretter <kernel@pengutronix.de>
10 #include <linux/device.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/platform_device.h>
16 #include <linux/videodev2.h>
17 #include <linux/slab.h>
21 #define VDOA_NAME "imx-vdoa"
28 #define VDOAIEBA00 0x14
29 #define VDOAIEBA01 0x18
30 #define VDOAIEBA02 0x1c
31 #define VDOAIEBA10 0x20
32 #define VDOAIEBA11 0x24
33 #define VDOAIEBA12 0x28
36 #define VDOAVEBA0 0x34
37 #define VDOAVEBA1 0x38
38 #define VDOAVEBA2 0x3c
42 #define VDOAC_ISEL BIT(6)
43 #define VDOAC_PFS BIT(5)
44 #define VDOAC_SO BIT(4)
45 #define VDOAC_SYNC BIT(3)
46 #define VDOAC_NF BIT(2)
47 #define VDOAC_BNDM_MASK 0x3
48 #define VDOAC_BAND_HEIGHT_8 0x0
49 #define VDOAC_BAND_HEIGHT_16 0x1
50 #define VDOAC_BAND_HEIGHT_32 0x2
52 #define VDOASRR_START BIT(1)
53 #define VDOASRR_SWRST BIT(0)
55 #define VDOAIE_EITERR BIT(1)
56 #define VDOAIE_EIEOT BIT(0)
58 #define VDOAIST_TERR BIT(1)
59 #define VDOAIST_EOT BIT(0)
61 #define VDOAFP_FH_MASK (0x1fff << 16)
62 #define VDOAFP_FW_MASK (0x3fff)
64 #define VDOASL_VSLY_MASK (0x3fff << 16)
65 #define VDOASL_ISLY_MASK (0x7fff)
67 #define VDOASR_ERRW BIT(4)
68 #define VDOASR_EOB BIT(3)
69 #define VDOASR_CURRENT_FRAME (0x3 << 1)
70 #define VDOASR_CURRENT_BUFFER BIT(1)
78 struct vdoa_ctx *curr_ctx;
87 unsigned int bytesperline;
88 unsigned int sizeimage;
93 struct vdoa_data *vdoa;
94 struct completion completion;
95 struct vdoa_q_data q_data[2];
96 unsigned int submitted_job;
97 unsigned int completed_job;
100 static irqreturn_t vdoa_irq_handler(int irq, void *data)
102 struct vdoa_data *vdoa = data;
103 struct vdoa_ctx *curr_ctx;
106 /* Disable interrupts */
107 writel(0, vdoa->regs + VDOAIE);
109 curr_ctx = vdoa->curr_ctx;
112 "Instance released before the end of transaction\n");
116 val = readl(vdoa->regs + VDOAIST);
117 writel(val, vdoa->regs + VDOAIST);
118 if (val & VDOAIST_TERR) {
119 val = readl(vdoa->regs + VDOASR) & VDOASR_ERRW;
120 dev_err(vdoa->dev, "AXI %s error\n", val ? "write" : "read");
121 } else if (!(val & VDOAIST_EOT)) {
122 dev_warn(vdoa->dev, "Spurious interrupt\n");
124 curr_ctx->completed_job++;
125 complete(&curr_ctx->completion);
130 int vdoa_wait_for_completion(struct vdoa_ctx *ctx)
132 struct vdoa_data *vdoa = ctx->vdoa;
134 if (ctx->submitted_job == ctx->completed_job)
137 if (!wait_for_completion_timeout(&ctx->completion,
138 msecs_to_jiffies(300))) {
140 "Timeout waiting for transfer result\n");
146 EXPORT_SYMBOL(vdoa_wait_for_completion);
148 void vdoa_device_run(struct vdoa_ctx *ctx, dma_addr_t dst, dma_addr_t src)
150 struct vdoa_q_data *src_q_data, *dst_q_data;
151 struct vdoa_data *vdoa = ctx->vdoa;
155 vdoa_wait_for_completion(vdoa->curr_ctx);
157 vdoa->curr_ctx = ctx;
159 reinit_completion(&ctx->completion);
160 ctx->submitted_job++;
162 src_q_data = &ctx->q_data[V4L2_M2M_SRC];
163 dst_q_data = &ctx->q_data[V4L2_M2M_DST];
165 /* Progressive, no sync, 1 frame per run */
166 if (dst_q_data->pixelformat == V4L2_PIX_FMT_YUYV)
170 writel(val, vdoa->regs + VDOAC);
172 writel(dst_q_data->height << 16 | dst_q_data->width,
173 vdoa->regs + VDOAFP);
176 writel(val, vdoa->regs + VDOAIEBA00);
178 writel(src_q_data->bytesperline << 16 | dst_q_data->bytesperline,
179 vdoa->regs + VDOASL);
181 if (dst_q_data->pixelformat == V4L2_PIX_FMT_NV12 ||
182 dst_q_data->pixelformat == V4L2_PIX_FMT_NV21)
183 val = dst_q_data->bytesperline * dst_q_data->height;
186 writel(val, vdoa->regs + VDOAIUBO);
189 writel(val, vdoa->regs + VDOAVEBA0);
190 val = round_up(src_q_data->bytesperline * src_q_data->height, 4096);
191 writel(val, vdoa->regs + VDOAVUBO);
193 /* Enable interrupts and start transfer */
194 writel(VDOAIE_EITERR | VDOAIE_EIEOT, vdoa->regs + VDOAIE);
195 writel(VDOASRR_START, vdoa->regs + VDOASRR);
197 EXPORT_SYMBOL(vdoa_device_run);
199 struct vdoa_ctx *vdoa_context_create(struct vdoa_data *vdoa)
201 struct vdoa_ctx *ctx;
204 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
208 err = clk_prepare_enable(vdoa->vdoa_clk);
214 init_completion(&ctx->completion);
219 EXPORT_SYMBOL(vdoa_context_create);
221 void vdoa_context_destroy(struct vdoa_ctx *ctx)
223 struct vdoa_data *vdoa = ctx->vdoa;
225 if (vdoa->curr_ctx == ctx) {
226 vdoa_wait_for_completion(vdoa->curr_ctx);
227 vdoa->curr_ctx = NULL;
230 clk_disable_unprepare(vdoa->vdoa_clk);
233 EXPORT_SYMBOL(vdoa_context_destroy);
235 int vdoa_context_configure(struct vdoa_ctx *ctx,
236 unsigned int width, unsigned int height,
239 struct vdoa_q_data *src_q_data;
240 struct vdoa_q_data *dst_q_data;
242 if (width < 16 || width > 8192 || width % 16 != 0 ||
243 height < 16 || height > 4096 || height % 16 != 0)
246 if (pixelformat != V4L2_PIX_FMT_YUYV &&
247 pixelformat != V4L2_PIX_FMT_NV12)
250 /* If no context is passed, only check if the format is valid */
254 src_q_data = &ctx->q_data[V4L2_M2M_SRC];
255 dst_q_data = &ctx->q_data[V4L2_M2M_DST];
257 src_q_data->width = width;
258 src_q_data->height = height;
259 src_q_data->bytesperline = width;
260 src_q_data->sizeimage =
261 round_up(src_q_data->bytesperline * height, 4096) +
262 src_q_data->bytesperline * height / 2;
264 dst_q_data->width = width;
265 dst_q_data->height = height;
266 dst_q_data->pixelformat = pixelformat;
267 switch (pixelformat) {
268 case V4L2_PIX_FMT_YUYV:
269 dst_q_data->bytesperline = width * 2;
270 dst_q_data->sizeimage = dst_q_data->bytesperline * height;
272 case V4L2_PIX_FMT_NV12:
274 dst_q_data->bytesperline = width;
275 dst_q_data->sizeimage =
276 dst_q_data->bytesperline * height * 3 / 2;
282 EXPORT_SYMBOL(vdoa_context_configure);
284 static int vdoa_probe(struct platform_device *pdev)
286 struct vdoa_data *vdoa;
287 struct resource *res;
290 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
292 dev_err(&pdev->dev, "DMA enable failed\n");
296 vdoa = devm_kzalloc(&pdev->dev, sizeof(*vdoa), GFP_KERNEL);
300 vdoa->dev = &pdev->dev;
302 vdoa->vdoa_clk = devm_clk_get(vdoa->dev, NULL);
303 if (IS_ERR(vdoa->vdoa_clk)) {
304 dev_err(vdoa->dev, "Failed to get clock\n");
305 return PTR_ERR(vdoa->vdoa_clk);
308 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
309 vdoa->regs = devm_ioremap_resource(vdoa->dev, res);
310 if (IS_ERR(vdoa->regs))
311 return PTR_ERR(vdoa->regs);
313 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
316 ret = devm_request_threaded_irq(&pdev->dev, res->start, NULL,
317 vdoa_irq_handler, IRQF_ONESHOT,
320 dev_err(vdoa->dev, "Failed to get irq\n");
324 platform_set_drvdata(pdev, vdoa);
329 static int vdoa_remove(struct platform_device *pdev)
334 static const struct of_device_id vdoa_dt_ids[] = {
335 { .compatible = "fsl,imx6q-vdoa" },
338 MODULE_DEVICE_TABLE(of, vdoa_dt_ids);
340 static struct platform_driver vdoa_driver = {
342 .remove = vdoa_remove,
345 .of_match_table = vdoa_dt_ids,
349 module_platform_driver(vdoa_driver);
351 MODULE_DESCRIPTION("Video Data Order Adapter");
352 MODULE_AUTHOR("Philipp Zabel <philipp.zabel@gmail.com>");
353 MODULE_ALIAS("platform:imx-vdoa");
354 MODULE_LICENSE("GPL");