Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / media / platform / mtk-vcodec / mtk_vcodec_util.c
blobac5973b6735f169321608aa4bcc5dd517156ac29
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: PC Chen <pc.chen@mediatek.com>
5 * Tiffany Lin <tiffany.lin@mediatek.com>
6 */
8 #include <linux/module.h>
10 #include "mtk_vcodec_drv.h"
11 #include "mtk_vcodec_util.h"
13 /* For encoder, this will enable logs in venc/*/
14 bool mtk_vcodec_dbg;
15 EXPORT_SYMBOL(mtk_vcodec_dbg);
17 /* The log level of v4l2 encoder or decoder driver.
18 * That is, files under mtk-vcodec/.
20 int mtk_v4l2_dbg_level;
21 EXPORT_SYMBOL(mtk_v4l2_dbg_level);
23 void __iomem *mtk_vcodec_get_reg_addr(struct mtk_vcodec_ctx *data,
24 unsigned int reg_idx)
26 struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)data;
28 if (!data || reg_idx >= NUM_MAX_VCODEC_REG_BASE) {
29 mtk_v4l2_err("Invalid arguments, reg_idx=%d", reg_idx);
30 return NULL;
32 return ctx->dev->reg_base[reg_idx];
34 EXPORT_SYMBOL(mtk_vcodec_get_reg_addr);
36 int mtk_vcodec_mem_alloc(struct mtk_vcodec_ctx *data,
37 struct mtk_vcodec_mem *mem)
39 unsigned long size = mem->size;
40 struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)data;
41 struct device *dev = &ctx->dev->plat_dev->dev;
43 mem->va = dma_alloc_coherent(dev, size, &mem->dma_addr, GFP_KERNEL);
44 if (!mem->va) {
45 mtk_v4l2_err("%s dma_alloc size=%ld failed!", dev_name(dev),
46 size);
47 return -ENOMEM;
50 mtk_v4l2_debug(3, "[%d] - va = %p", ctx->id, mem->va);
51 mtk_v4l2_debug(3, "[%d] - dma = 0x%lx", ctx->id,
52 (unsigned long)mem->dma_addr);
53 mtk_v4l2_debug(3, "[%d] size = 0x%lx", ctx->id, size);
55 return 0;
57 EXPORT_SYMBOL(mtk_vcodec_mem_alloc);
59 void mtk_vcodec_mem_free(struct mtk_vcodec_ctx *data,
60 struct mtk_vcodec_mem *mem)
62 unsigned long size = mem->size;
63 struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)data;
64 struct device *dev = &ctx->dev->plat_dev->dev;
66 if (!mem->va) {
67 mtk_v4l2_err("%s dma_free size=%ld failed!", dev_name(dev),
68 size);
69 return;
72 mtk_v4l2_debug(3, "[%d] - va = %p", ctx->id, mem->va);
73 mtk_v4l2_debug(3, "[%d] - dma = 0x%lx", ctx->id,
74 (unsigned long)mem->dma_addr);
75 mtk_v4l2_debug(3, "[%d] size = 0x%lx", ctx->id, size);
77 dma_free_coherent(dev, size, mem->va, mem->dma_addr);
78 mem->va = NULL;
79 mem->dma_addr = 0;
80 mem->size = 0;
82 EXPORT_SYMBOL(mtk_vcodec_mem_free);
84 void mtk_vcodec_set_curr_ctx(struct mtk_vcodec_dev *dev,
85 struct mtk_vcodec_ctx *ctx)
87 unsigned long flags;
89 spin_lock_irqsave(&dev->irqlock, flags);
90 dev->curr_ctx = ctx;
91 spin_unlock_irqrestore(&dev->irqlock, flags);
93 EXPORT_SYMBOL(mtk_vcodec_set_curr_ctx);
95 struct mtk_vcodec_ctx *mtk_vcodec_get_curr_ctx(struct mtk_vcodec_dev *dev)
97 unsigned long flags;
98 struct mtk_vcodec_ctx *ctx;
100 spin_lock_irqsave(&dev->irqlock, flags);
101 ctx = dev->curr_ctx;
102 spin_unlock_irqrestore(&dev->irqlock, flags);
103 return ctx;
105 EXPORT_SYMBOL(mtk_vcodec_get_curr_ctx);
107 MODULE_LICENSE("GPL v2");
108 MODULE_DESCRIPTION("Mediatek video codec driver");