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 */
7 
8 #include <linux/module.h>
9 
10 #include "mtk_vcodec_drv.h"
11 #include "mtk_vcodec_util.h"
12 
13 /* For encoder, this will enable logs in venc/*/
14 bool mtk_vcodec_dbg;
15 EXPORT_SYMBOL(mtk_vcodec_dbg);
16 
17 /* The log level of v4l2 encoder or decoder driver.
18  * That is, files under mtk-vcodec/.
19  */
20 int mtk_v4l2_dbg_level;
21 EXPORT_SYMBOL(mtk_v4l2_dbg_level);
22 
mtk_vcodec_get_reg_addr(struct mtk_vcodec_ctx * data,unsigned int reg_idx)23 void __iomem *mtk_vcodec_get_reg_addr(struct mtk_vcodec_ctx *data,
24 					unsigned int reg_idx)
25 {
26 	struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)data;
27 
28 	if (!data || reg_idx >= NUM_MAX_VCODEC_REG_BASE) {
29 		mtk_v4l2_err("Invalid arguments, reg_idx=%d", reg_idx);
30 		return NULL;
31 	}
32 	return ctx->dev->reg_base[reg_idx];
33 }
34 EXPORT_SYMBOL(mtk_vcodec_get_reg_addr);
35 
mtk_vcodec_mem_alloc(struct mtk_vcodec_ctx * data,struct mtk_vcodec_mem * mem)36 int mtk_vcodec_mem_alloc(struct mtk_vcodec_ctx *data,
37 			struct mtk_vcodec_mem *mem)
38 {
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;
42 
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;
48 	}
49 
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);
54 
55 	return 0;
56 }
57 EXPORT_SYMBOL(mtk_vcodec_mem_alloc);
58 
mtk_vcodec_mem_free(struct mtk_vcodec_ctx * data,struct mtk_vcodec_mem * mem)59 void mtk_vcodec_mem_free(struct mtk_vcodec_ctx *data,
60 			struct mtk_vcodec_mem *mem)
61 {
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;
65 
66 	if (!mem->va) {
67 		mtk_v4l2_err("%s dma_free size=%ld failed!", dev_name(dev),
68 			     size);
69 		return;
70 	}
71 
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);
76 
77 	dma_free_coherent(dev, size, mem->va, mem->dma_addr);
78 	mem->va = NULL;
79 	mem->dma_addr = 0;
80 	mem->size = 0;
81 }
82 EXPORT_SYMBOL(mtk_vcodec_mem_free);
83 
mtk_vcodec_set_curr_ctx(struct mtk_vcodec_dev * dev,struct mtk_vcodec_ctx * ctx)84 void mtk_vcodec_set_curr_ctx(struct mtk_vcodec_dev *dev,
85 	struct mtk_vcodec_ctx *ctx)
86 {
87 	unsigned long flags;
88 
89 	spin_lock_irqsave(&dev->irqlock, flags);
90 	dev->curr_ctx = ctx;
91 	spin_unlock_irqrestore(&dev->irqlock, flags);
92 }
93 EXPORT_SYMBOL(mtk_vcodec_set_curr_ctx);
94 
mtk_vcodec_get_curr_ctx(struct mtk_vcodec_dev * dev)95 struct mtk_vcodec_ctx *mtk_vcodec_get_curr_ctx(struct mtk_vcodec_dev *dev)
96 {
97 	unsigned long flags;
98 	struct mtk_vcodec_ctx *ctx;
99 
100 	spin_lock_irqsave(&dev->irqlock, flags);
101 	ctx = dev->curr_ctx;
102 	spin_unlock_irqrestore(&dev->irqlock, flags);
103 	return ctx;
104 }
105 EXPORT_SYMBOL(mtk_vcodec_get_curr_ctx);
106 
107 MODULE_LICENSE("GPL v2");
108 MODULE_DESCRIPTION("Mediatek video codec driver");
109