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 #include <linux/of.h>
10 #include <linux/of_device.h>
11 
12 #include "mtk_vcodec_dec_hw.h"
13 #include "mtk_vcodec_drv.h"
14 #include "mtk_vcodec_util.h"
15 
mtk_vcodec_get_reg_addr(struct mtk_vcodec_ctx * data,unsigned int reg_idx)16 void __iomem *mtk_vcodec_get_reg_addr(struct mtk_vcodec_ctx *data,
17 					unsigned int reg_idx)
18 {
19 	struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)data;
20 
21 	if (!data || reg_idx >= NUM_MAX_VCODEC_REG_BASE) {
22 		mtk_v4l2_err("Invalid arguments, reg_idx=%d", reg_idx);
23 		return NULL;
24 	}
25 	return ctx->dev->reg_base[reg_idx];
26 }
27 EXPORT_SYMBOL(mtk_vcodec_get_reg_addr);
28 
mtk_vcodec_mem_alloc(struct mtk_vcodec_ctx * data,struct mtk_vcodec_mem * mem)29 int mtk_vcodec_mem_alloc(struct mtk_vcodec_ctx *data,
30 			struct mtk_vcodec_mem *mem)
31 {
32 	unsigned long size = mem->size;
33 	struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)data;
34 	struct device *dev = &ctx->dev->plat_dev->dev;
35 
36 	mem->va = dma_alloc_coherent(dev, size, &mem->dma_addr, GFP_KERNEL);
37 	if (!mem->va) {
38 		mtk_v4l2_err("%s dma_alloc size=%ld failed!", dev_name(dev),
39 			     size);
40 		return -ENOMEM;
41 	}
42 
43 	mtk_v4l2_debug(3, "[%d]  - va      = %p", ctx->id, mem->va);
44 	mtk_v4l2_debug(3, "[%d]  - dma     = 0x%lx", ctx->id,
45 		       (unsigned long)mem->dma_addr);
46 	mtk_v4l2_debug(3, "[%d]    size = 0x%lx", ctx->id, size);
47 
48 	return 0;
49 }
50 EXPORT_SYMBOL(mtk_vcodec_mem_alloc);
51 
mtk_vcodec_mem_free(struct mtk_vcodec_ctx * data,struct mtk_vcodec_mem * mem)52 void mtk_vcodec_mem_free(struct mtk_vcodec_ctx *data,
53 			struct mtk_vcodec_mem *mem)
54 {
55 	unsigned long size = mem->size;
56 	struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)data;
57 	struct device *dev = &ctx->dev->plat_dev->dev;
58 
59 	if (!mem->va) {
60 		mtk_v4l2_err("%s dma_free size=%ld failed!", dev_name(dev),
61 			     size);
62 		return;
63 	}
64 
65 	mtk_v4l2_debug(3, "[%d]  - va      = %p", ctx->id, mem->va);
66 	mtk_v4l2_debug(3, "[%d]  - dma     = 0x%lx", ctx->id,
67 		       (unsigned long)mem->dma_addr);
68 	mtk_v4l2_debug(3, "[%d]    size = 0x%lx", ctx->id, size);
69 
70 	dma_free_coherent(dev, size, mem->va, mem->dma_addr);
71 	mem->va = NULL;
72 	mem->dma_addr = 0;
73 	mem->size = 0;
74 }
75 EXPORT_SYMBOL(mtk_vcodec_mem_free);
76 
mtk_vcodec_get_hw_dev(struct mtk_vcodec_dev * dev,int hw_idx)77 void *mtk_vcodec_get_hw_dev(struct mtk_vcodec_dev *dev, int hw_idx)
78 {
79 	if (hw_idx >= MTK_VDEC_HW_MAX || hw_idx < 0 || !dev->subdev_dev[hw_idx]) {
80 		mtk_v4l2_err("hw idx is out of range:%d", hw_idx);
81 		return NULL;
82 	}
83 
84 	return dev->subdev_dev[hw_idx];
85 }
86 EXPORT_SYMBOL(mtk_vcodec_get_hw_dev);
87 
mtk_vcodec_set_curr_ctx(struct mtk_vcodec_dev * vdec_dev,struct mtk_vcodec_ctx * ctx,int hw_idx)88 void mtk_vcodec_set_curr_ctx(struct mtk_vcodec_dev *vdec_dev,
89 			     struct mtk_vcodec_ctx *ctx, int hw_idx)
90 {
91 	unsigned long flags;
92 	struct mtk_vdec_hw_dev *subdev_dev;
93 
94 	spin_lock_irqsave(&vdec_dev->irqlock, flags);
95 	if (vdec_dev->vdec_pdata->is_subdev_supported) {
96 		subdev_dev = mtk_vcodec_get_hw_dev(vdec_dev, hw_idx);
97 		if (!subdev_dev) {
98 			mtk_v4l2_err("Failed to get hw dev");
99 			spin_unlock_irqrestore(&vdec_dev->irqlock, flags);
100 			return;
101 		}
102 		subdev_dev->curr_ctx = ctx;
103 	} else {
104 		vdec_dev->curr_ctx = ctx;
105 	}
106 	spin_unlock_irqrestore(&vdec_dev->irqlock, flags);
107 }
108 EXPORT_SYMBOL(mtk_vcodec_set_curr_ctx);
109 
mtk_vcodec_get_curr_ctx(struct mtk_vcodec_dev * vdec_dev,unsigned int hw_idx)110 struct mtk_vcodec_ctx *mtk_vcodec_get_curr_ctx(struct mtk_vcodec_dev *vdec_dev,
111 					       unsigned int hw_idx)
112 {
113 	unsigned long flags;
114 	struct mtk_vcodec_ctx *ctx;
115 	struct mtk_vdec_hw_dev *subdev_dev;
116 
117 	spin_lock_irqsave(&vdec_dev->irqlock, flags);
118 	if (vdec_dev->vdec_pdata->is_subdev_supported) {
119 		subdev_dev = mtk_vcodec_get_hw_dev(vdec_dev, hw_idx);
120 		if (!subdev_dev) {
121 			mtk_v4l2_err("Failed to get hw dev");
122 			spin_unlock_irqrestore(&vdec_dev->irqlock, flags);
123 			return NULL;
124 		}
125 		ctx = subdev_dev->curr_ctx;
126 	} else {
127 		ctx = vdec_dev->curr_ctx;
128 	}
129 	spin_unlock_irqrestore(&vdec_dev->irqlock, flags);
130 	return ctx;
131 }
132 EXPORT_SYMBOL(mtk_vcodec_get_curr_ctx);
133 
134 MODULE_LICENSE("GPL v2");
135 MODULE_DESCRIPTION("Mediatek video codec driver");
136