1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: PC Chen <[email protected]>
5 *	Tiffany Lin <[email protected]>
6 */
7 
8 #include <linux/module.h>
9 #include <linux/of.h>
10 #include <linux/regmap.h>
11 
12 #include "../decoder/mtk_vcodec_dec_drv.h"
13 #include "../encoder/mtk_vcodec_enc_drv.h"
14 #include "../decoder/mtk_vcodec_dec_hw.h"
15 
16 #if defined(CONFIG_DEBUG_FS)
17 int mtk_vcodec_dbg;
18 EXPORT_SYMBOL(mtk_vcodec_dbg);
19 
20 int mtk_v4l2_dbg_level;
21 EXPORT_SYMBOL(mtk_v4l2_dbg_level);
22 #endif
23 
24 void __iomem *mtk_vcodec_get_reg_addr(void __iomem **reg_base, unsigned int reg_idx)
25 {
26 	if (reg_idx >= NUM_MAX_VCODEC_REG_BASE) {
27 		pr_err(MTK_DBG_V4L2_STR "Invalid arguments, reg_idx=%d", reg_idx);
28 		return NULL;
29 	}
30 	return reg_base[reg_idx];
31 }
32 EXPORT_SYMBOL(mtk_vcodec_get_reg_addr);
33 
34 int mtk_vcodec_write_vdecsys(struct mtk_vcodec_dec_ctx *ctx, unsigned int reg,
35 			     unsigned int val)
36 {
37 	struct mtk_vcodec_dec_dev *dev = ctx->dev;
38 
39 	if (dev->vdecsys_regmap)
40 		return regmap_write(dev->vdecsys_regmap, reg, val);
41 
42 	writel(val, dev->reg_base[VDEC_SYS] + reg);
43 
44 	return 0;
45 }
46 EXPORT_SYMBOL(mtk_vcodec_write_vdecsys);
47 
48 int mtk_vcodec_mem_alloc(void *priv, struct mtk_vcodec_mem *mem)
49 {
50 	enum mtk_instance_type inst_type = *((unsigned int *)priv);
51 	struct platform_device *plat_dev;
52 	int id;
53 
54 	if (inst_type == MTK_INST_ENCODER) {
55 		struct mtk_vcodec_enc_ctx *enc_ctx = priv;
56 
57 		plat_dev = enc_ctx->dev->plat_dev;
58 		id = enc_ctx->id;
59 	} else {
60 		struct mtk_vcodec_dec_ctx *dec_ctx = priv;
61 
62 		plat_dev = dec_ctx->dev->plat_dev;
63 		id = dec_ctx->id;
64 	}
65 
66 	mem->va = dma_alloc_coherent(&plat_dev->dev, mem->size, &mem->dma_addr, GFP_KERNEL);
67 	if (!mem->va) {
68 		mtk_v4l2_err(plat_dev, "%s dma_alloc size=0x%zx failed!",
69 			     __func__, mem->size);
70 		return -ENOMEM;
71 	}
72 
73 	mtk_v4l2_debug(plat_dev, 3, "[%d] - va = %p dma = 0x%lx size = 0x%zx", id, mem->va,
74 		       (unsigned long)mem->dma_addr, mem->size);
75 
76 	return 0;
77 }
78 EXPORT_SYMBOL(mtk_vcodec_mem_alloc);
79 
80 void mtk_vcodec_mem_free(void *priv, struct mtk_vcodec_mem *mem)
81 {
82 	enum mtk_instance_type inst_type = *((unsigned int *)priv);
83 	struct platform_device *plat_dev;
84 	int id;
85 
86 	if (inst_type == MTK_INST_ENCODER) {
87 		struct mtk_vcodec_enc_ctx *enc_ctx = priv;
88 
89 		plat_dev = enc_ctx->dev->plat_dev;
90 		id = enc_ctx->id;
91 	} else {
92 		struct mtk_vcodec_dec_ctx *dec_ctx = priv;
93 
94 		plat_dev = dec_ctx->dev->plat_dev;
95 		id = dec_ctx->id;
96 	}
97 
98 	if (!mem->va) {
99 		mtk_v4l2_err(plat_dev, "%s: Tried to free a NULL VA", __func__);
100 		if (mem->size)
101 			mtk_v4l2_err(plat_dev, "Failed to free %zu bytes", mem->size);
102 		return;
103 	}
104 
105 	mtk_v4l2_debug(plat_dev, 3, "[%d] - va = %p dma = 0x%lx size = 0x%zx", id, mem->va,
106 		       (unsigned long)mem->dma_addr, mem->size);
107 
108 	dma_free_coherent(&plat_dev->dev, mem->size, mem->va, mem->dma_addr);
109 	mem->va = NULL;
110 	mem->dma_addr = 0;
111 	mem->size = 0;
112 }
113 EXPORT_SYMBOL(mtk_vcodec_mem_free);
114 
115 void *mtk_vcodec_get_hw_dev(struct mtk_vcodec_dec_dev *dev, int hw_idx)
116 {
117 	if (hw_idx >= MTK_VDEC_HW_MAX || hw_idx < 0 || !dev->subdev_dev[hw_idx]) {
118 		dev_err(&dev->plat_dev->dev, "hw idx is out of range:%d", hw_idx);
119 		return NULL;
120 	}
121 
122 	return dev->subdev_dev[hw_idx];
123 }
124 EXPORT_SYMBOL(mtk_vcodec_get_hw_dev);
125 
126 void mtk_vcodec_set_curr_ctx(struct mtk_vcodec_dec_dev *vdec_dev,
127 			     struct mtk_vcodec_dec_ctx *ctx, int hw_idx)
128 {
129 	unsigned long flags;
130 	struct mtk_vdec_hw_dev *subdev_dev;
131 
132 	spin_lock_irqsave(&vdec_dev->irqlock, flags);
133 	if (vdec_dev->vdec_pdata->is_subdev_supported) {
134 		subdev_dev = mtk_vcodec_get_hw_dev(vdec_dev, hw_idx);
135 		if (!subdev_dev) {
136 			dev_err(&vdec_dev->plat_dev->dev, "Failed to get hw dev");
137 			spin_unlock_irqrestore(&vdec_dev->irqlock, flags);
138 			return;
139 		}
140 		subdev_dev->curr_ctx = ctx;
141 	} else {
142 		vdec_dev->curr_ctx = ctx;
143 	}
144 	spin_unlock_irqrestore(&vdec_dev->irqlock, flags);
145 }
146 EXPORT_SYMBOL(mtk_vcodec_set_curr_ctx);
147 
148 struct mtk_vcodec_dec_ctx *mtk_vcodec_get_curr_ctx(struct mtk_vcodec_dec_dev *vdec_dev,
149 						   unsigned int hw_idx)
150 {
151 	unsigned long flags;
152 	struct mtk_vcodec_dec_ctx *ctx;
153 	struct mtk_vdec_hw_dev *subdev_dev;
154 
155 	spin_lock_irqsave(&vdec_dev->irqlock, flags);
156 	if (vdec_dev->vdec_pdata->is_subdev_supported) {
157 		subdev_dev = mtk_vcodec_get_hw_dev(vdec_dev, hw_idx);
158 		if (!subdev_dev) {
159 			dev_err(&vdec_dev->plat_dev->dev, "Failed to get hw dev");
160 			spin_unlock_irqrestore(&vdec_dev->irqlock, flags);
161 			return NULL;
162 		}
163 		ctx = subdev_dev->curr_ctx;
164 	} else {
165 		ctx = vdec_dev->curr_ctx;
166 	}
167 	spin_unlock_irqrestore(&vdec_dev->irqlock, flags);
168 	return ctx;
169 }
170 EXPORT_SYMBOL(mtk_vcodec_get_curr_ctx);
171 
172 MODULE_LICENSE("GPL v2");
173 MODULE_DESCRIPTION("Mediatek video codec driver");
174