treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 174
[sfrench/cifs-2.6.git] / drivers / media / platform / mtk-vcodec / mtk_vcodec_util.c
1 // SPDX-License-Identifier: GPL-2.0-only
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 #include "mtk_vpu.h"
13
14 /* For encoder, this will enable logs in venc/*/
15 bool mtk_vcodec_dbg;
16 EXPORT_SYMBOL(mtk_vcodec_dbg);
17
18 /* The log level of v4l2 encoder or decoder driver.
19  * That is, files under mtk-vcodec/.
20  */
21 int mtk_v4l2_dbg_level;
22 EXPORT_SYMBOL(mtk_v4l2_dbg_level);
23
24 void __iomem *mtk_vcodec_get_reg_addr(struct mtk_vcodec_ctx *data,
25                                         unsigned int reg_idx)
26 {
27         struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)data;
28
29         if (!data || reg_idx >= NUM_MAX_VCODEC_REG_BASE) {
30                 mtk_v4l2_err("Invalid arguments, reg_idx=%d", reg_idx);
31                 return NULL;
32         }
33         return ctx->dev->reg_base[reg_idx];
34 }
35 EXPORT_SYMBOL(mtk_vcodec_get_reg_addr);
36
37 int mtk_vcodec_mem_alloc(struct mtk_vcodec_ctx *data,
38                         struct mtk_vcodec_mem *mem)
39 {
40         unsigned long size = mem->size;
41         struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)data;
42         struct device *dev = &ctx->dev->plat_dev->dev;
43
44         mem->va = dma_alloc_coherent(dev, size, &mem->dma_addr, GFP_KERNEL);
45         if (!mem->va) {
46                 mtk_v4l2_err("%s dma_alloc size=%ld failed!", dev_name(dev),
47                              size);
48                 return -ENOMEM;
49         }
50
51         mtk_v4l2_debug(3, "[%d]  - va      = %p", ctx->id, mem->va);
52         mtk_v4l2_debug(3, "[%d]  - dma     = 0x%lx", ctx->id,
53                        (unsigned long)mem->dma_addr);
54         mtk_v4l2_debug(3, "[%d]    size = 0x%lx", ctx->id, size);
55
56         return 0;
57 }
58 EXPORT_SYMBOL(mtk_vcodec_mem_alloc);
59
60 void mtk_vcodec_mem_free(struct mtk_vcodec_ctx *data,
61                         struct mtk_vcodec_mem *mem)
62 {
63         unsigned long size = mem->size;
64         struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)data;
65         struct device *dev = &ctx->dev->plat_dev->dev;
66
67         if (!mem->va) {
68                 mtk_v4l2_err("%s dma_free size=%ld failed!", dev_name(dev),
69                              size);
70                 return;
71         }
72
73         mtk_v4l2_debug(3, "[%d]  - va      = %p", ctx->id, mem->va);
74         mtk_v4l2_debug(3, "[%d]  - dma     = 0x%lx", ctx->id,
75                        (unsigned long)mem->dma_addr);
76         mtk_v4l2_debug(3, "[%d]    size = 0x%lx", ctx->id, size);
77
78         dma_free_coherent(dev, size, mem->va, mem->dma_addr);
79         mem->va = NULL;
80         mem->dma_addr = 0;
81         mem->size = 0;
82 }
83 EXPORT_SYMBOL(mtk_vcodec_mem_free);
84
85 void mtk_vcodec_set_curr_ctx(struct mtk_vcodec_dev *dev,
86         struct mtk_vcodec_ctx *ctx)
87 {
88         unsigned long flags;
89
90         spin_lock_irqsave(&dev->irqlock, flags);
91         dev->curr_ctx = ctx;
92         spin_unlock_irqrestore(&dev->irqlock, flags);
93 }
94 EXPORT_SYMBOL(mtk_vcodec_set_curr_ctx);
95
96 struct mtk_vcodec_ctx *mtk_vcodec_get_curr_ctx(struct mtk_vcodec_dev *dev)
97 {
98         unsigned long flags;
99         struct mtk_vcodec_ctx *ctx;
100
101         spin_lock_irqsave(&dev->irqlock, flags);
102         ctx = dev->curr_ctx;
103         spin_unlock_irqrestore(&dev->irqlock, flags);
104         return ctx;
105 }
106 EXPORT_SYMBOL(mtk_vcodec_get_curr_ctx);
107
108 MODULE_LICENSE("GPL v2");
109 MODULE_DESCRIPTION("Mediatek video codec driver");