treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / media / platform / mtk-vcodec / mtk_vcodec_util.c
blobd48f542db1a914dc7c4ee321b368df9f9febc364
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"
12 #include "mtk_vpu.h"
14 /* For encoder, this will enable logs in venc/*/
15 bool mtk_vcodec_dbg;
16 EXPORT_SYMBOL(mtk_vcodec_dbg);
18 /* The log level of v4l2 encoder or decoder driver.
19 * That is, files under mtk-vcodec/.
21 int mtk_v4l2_dbg_level;
22 EXPORT_SYMBOL(mtk_v4l2_dbg_level);
24 void __iomem *mtk_vcodec_get_reg_addr(struct mtk_vcodec_ctx *data,
25 unsigned int reg_idx)
27 struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)data;
29 if (!data || reg_idx >= NUM_MAX_VCODEC_REG_BASE) {
30 mtk_v4l2_err("Invalid arguments, reg_idx=%d", reg_idx);
31 return NULL;
33 return ctx->dev->reg_base[reg_idx];
35 EXPORT_SYMBOL(mtk_vcodec_get_reg_addr);
37 int mtk_vcodec_mem_alloc(struct mtk_vcodec_ctx *data,
38 struct mtk_vcodec_mem *mem)
40 unsigned long size = mem->size;
41 struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)data;
42 struct device *dev = &ctx->dev->plat_dev->dev;
44 mem->va = dma_alloc_coherent(dev, size, &mem->dma_addr, GFP_KERNEL);
45 if (!mem->va) {
46 mtk_v4l2_err("%s dma_alloc size=%ld failed!", dev_name(dev),
47 size);
48 return -ENOMEM;
51 mtk_v4l2_debug(3, "[%d] - va = %p", ctx->id, mem->va);
52 mtk_v4l2_debug(3, "[%d] - dma = 0x%lx", ctx->id,
53 (unsigned long)mem->dma_addr);
54 mtk_v4l2_debug(3, "[%d] size = 0x%lx", ctx->id, size);
56 return 0;
58 EXPORT_SYMBOL(mtk_vcodec_mem_alloc);
60 void mtk_vcodec_mem_free(struct mtk_vcodec_ctx *data,
61 struct mtk_vcodec_mem *mem)
63 unsigned long size = mem->size;
64 struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)data;
65 struct device *dev = &ctx->dev->plat_dev->dev;
67 if (!mem->va) {
68 mtk_v4l2_err("%s dma_free size=%ld failed!", dev_name(dev),
69 size);
70 return;
73 mtk_v4l2_debug(3, "[%d] - va = %p", ctx->id, mem->va);
74 mtk_v4l2_debug(3, "[%d] - dma = 0x%lx", ctx->id,
75 (unsigned long)mem->dma_addr);
76 mtk_v4l2_debug(3, "[%d] size = 0x%lx", ctx->id, size);
78 dma_free_coherent(dev, size, mem->va, mem->dma_addr);
79 mem->va = NULL;
80 mem->dma_addr = 0;
81 mem->size = 0;
83 EXPORT_SYMBOL(mtk_vcodec_mem_free);
85 void mtk_vcodec_set_curr_ctx(struct mtk_vcodec_dev *dev,
86 struct mtk_vcodec_ctx *ctx)
88 unsigned long flags;
90 spin_lock_irqsave(&dev->irqlock, flags);
91 dev->curr_ctx = ctx;
92 spin_unlock_irqrestore(&dev->irqlock, flags);
94 EXPORT_SYMBOL(mtk_vcodec_set_curr_ctx);
96 struct mtk_vcodec_ctx *mtk_vcodec_get_curr_ctx(struct mtk_vcodec_dev *dev)
98 unsigned long flags;
99 struct mtk_vcodec_ctx *ctx;
101 spin_lock_irqsave(&dev->irqlock, flags);
102 ctx = dev->curr_ctx;
103 spin_unlock_irqrestore(&dev->irqlock, flags);
104 return ctx;
106 EXPORT_SYMBOL(mtk_vcodec_get_curr_ctx);
108 MODULE_LICENSE("GPL v2");
109 MODULE_DESCRIPTION("Mediatek video codec driver");