1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: PC Chen <pc.chen@mediatek.com>
5 * Tiffany Lin <tiffany.lin@mediatek.com>
8 #include <linux/module.h>
10 #include <linux/of_device.h>
12 #include "mtk_vcodec_dec_hw.h"
13 #include "mtk_vcodec_drv.h"
14 #include "mtk_vcodec_util.h"
16 void __iomem *mtk_vcodec_get_reg_addr(struct mtk_vcodec_ctx *data,
19 struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)data;
21 if (!data || reg_idx >= NUM_MAX_VCODEC_REG_BASE) {
22 mtk_v4l2_err("Invalid arguments, reg_idx=%d", reg_idx);
25 return ctx->dev->reg_base[reg_idx];
27 EXPORT_SYMBOL(mtk_vcodec_get_reg_addr);
29 int mtk_vcodec_mem_alloc(struct mtk_vcodec_ctx *data,
30 struct mtk_vcodec_mem *mem)
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;
36 mem->va = dma_alloc_coherent(dev, size, &mem->dma_addr, GFP_KERNEL);
38 mtk_v4l2_err("%s dma_alloc size=%ld failed!", dev_name(dev),
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);
50 EXPORT_SYMBOL(mtk_vcodec_mem_alloc);
52 void mtk_vcodec_mem_free(struct mtk_vcodec_ctx *data,
53 struct mtk_vcodec_mem *mem)
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;
60 mtk_v4l2_err("%s dma_free size=%ld failed!", dev_name(dev),
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);
70 dma_free_coherent(dev, size, mem->va, mem->dma_addr);
75 EXPORT_SYMBOL(mtk_vcodec_mem_free);
77 void *mtk_vcodec_get_hw_dev(struct mtk_vcodec_dev *dev, int hw_idx)
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);
84 return dev->subdev_dev[hw_idx];
86 EXPORT_SYMBOL(mtk_vcodec_get_hw_dev);
88 void mtk_vcodec_set_curr_ctx(struct mtk_vcodec_dev *vdec_dev,
89 struct mtk_vcodec_ctx *ctx, int hw_idx)
92 struct mtk_vdec_hw_dev *subdev_dev;
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);
98 mtk_v4l2_err("Failed to get hw dev");
99 spin_unlock_irqrestore(&vdec_dev->irqlock, flags);
102 subdev_dev->curr_ctx = ctx;
104 vdec_dev->curr_ctx = ctx;
106 spin_unlock_irqrestore(&vdec_dev->irqlock, flags);
108 EXPORT_SYMBOL(mtk_vcodec_set_curr_ctx);
110 struct mtk_vcodec_ctx *mtk_vcodec_get_curr_ctx(struct mtk_vcodec_dev *vdec_dev,
114 struct mtk_vcodec_ctx *ctx;
115 struct mtk_vdec_hw_dev *subdev_dev;
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);
121 mtk_v4l2_err("Failed to get hw dev");
122 spin_unlock_irqrestore(&vdec_dev->irqlock, flags);
125 ctx = subdev_dev->curr_ctx;
127 ctx = vdec_dev->curr_ctx;
129 spin_unlock_irqrestore(&vdec_dev->irqlock, flags);
132 EXPORT_SYMBOL(mtk_vcodec_get_curr_ctx);
134 MODULE_LICENSE("GPL v2");
135 MODULE_DESCRIPTION("Mediatek video codec driver");