perf bpf: Move perf_event_output() from stdio.h to bpf.h
[linux/fpc-iii.git] / drivers / media / platform / mtk-vcodec / mtk_vcodec_util.c
blobe80123cba4062119b7c7aa2d0b99d470baacd39c
1 /*
2 * Copyright (c) 2016 MediaTek Inc.
3 * Author: PC Chen <pc.chen@mediatek.com>
4 * Tiffany Lin <tiffany.lin@mediatek.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/module.h>
18 #include "mtk_vcodec_drv.h"
19 #include "mtk_vcodec_util.h"
20 #include "mtk_vpu.h"
22 /* For encoder, this will enable logs in venc/*/
23 bool mtk_vcodec_dbg;
24 EXPORT_SYMBOL(mtk_vcodec_dbg);
26 /* The log level of v4l2 encoder or decoder driver.
27 * That is, files under mtk-vcodec/.
29 int mtk_v4l2_dbg_level;
30 EXPORT_SYMBOL(mtk_v4l2_dbg_level);
32 void __iomem *mtk_vcodec_get_reg_addr(struct mtk_vcodec_ctx *data,
33 unsigned int reg_idx)
35 struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)data;
37 if (!data || reg_idx >= NUM_MAX_VCODEC_REG_BASE) {
38 mtk_v4l2_err("Invalid arguments, reg_idx=%d", reg_idx);
39 return NULL;
41 return ctx->dev->reg_base[reg_idx];
43 EXPORT_SYMBOL(mtk_vcodec_get_reg_addr);
45 int mtk_vcodec_mem_alloc(struct mtk_vcodec_ctx *data,
46 struct mtk_vcodec_mem *mem)
48 unsigned long size = mem->size;
49 struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)data;
50 struct device *dev = &ctx->dev->plat_dev->dev;
52 mem->va = dma_zalloc_coherent(dev, size, &mem->dma_addr, GFP_KERNEL);
53 if (!mem->va) {
54 mtk_v4l2_err("%s dma_alloc size=%ld failed!", dev_name(dev),
55 size);
56 return -ENOMEM;
59 mtk_v4l2_debug(3, "[%d] - va = %p", ctx->id, mem->va);
60 mtk_v4l2_debug(3, "[%d] - dma = 0x%lx", ctx->id,
61 (unsigned long)mem->dma_addr);
62 mtk_v4l2_debug(3, "[%d] size = 0x%lx", ctx->id, size);
64 return 0;
66 EXPORT_SYMBOL(mtk_vcodec_mem_alloc);
68 void mtk_vcodec_mem_free(struct mtk_vcodec_ctx *data,
69 struct mtk_vcodec_mem *mem)
71 unsigned long size = mem->size;
72 struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)data;
73 struct device *dev = &ctx->dev->plat_dev->dev;
75 if (!mem->va) {
76 mtk_v4l2_err("%s dma_free size=%ld failed!", dev_name(dev),
77 size);
78 return;
81 mtk_v4l2_debug(3, "[%d] - va = %p", ctx->id, mem->va);
82 mtk_v4l2_debug(3, "[%d] - dma = 0x%lx", ctx->id,
83 (unsigned long)mem->dma_addr);
84 mtk_v4l2_debug(3, "[%d] size = 0x%lx", ctx->id, size);
86 dma_free_coherent(dev, size, mem->va, mem->dma_addr);
87 mem->va = NULL;
88 mem->dma_addr = 0;
89 mem->size = 0;
91 EXPORT_SYMBOL(mtk_vcodec_mem_free);
93 void mtk_vcodec_set_curr_ctx(struct mtk_vcodec_dev *dev,
94 struct mtk_vcodec_ctx *ctx)
96 unsigned long flags;
98 spin_lock_irqsave(&dev->irqlock, flags);
99 dev->curr_ctx = ctx;
100 spin_unlock_irqrestore(&dev->irqlock, flags);
102 EXPORT_SYMBOL(mtk_vcodec_set_curr_ctx);
104 struct mtk_vcodec_ctx *mtk_vcodec_get_curr_ctx(struct mtk_vcodec_dev *dev)
106 unsigned long flags;
107 struct mtk_vcodec_ctx *ctx;
109 spin_lock_irqsave(&dev->irqlock, flags);
110 ctx = dev->curr_ctx;
111 spin_unlock_irqrestore(&dev->irqlock, flags);
112 return ctx;
114 EXPORT_SYMBOL(mtk_vcodec_get_curr_ctx);
116 MODULE_LICENSE("GPL v2");
117 MODULE_DESCRIPTION("Mediatek video codec driver");