1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016 MediaTek Inc.
8 #include <linux/interrupt.h>
10 #include <linux/pm_runtime.h>
12 #include "mtk_vcodec_dec_hw.h"
13 #include "mtk_vcodec_dec_pm.h"
15 int mtk_vcodec_init_dec_clk(struct platform_device *pdev, struct mtk_vcodec_pm *pm)
17 struct mtk_vcodec_clk *dec_clk;
18 struct mtk_vcodec_clk_info *clk_info;
21 dec_clk = &pm->vdec_clk;
25 of_property_count_strings(pdev->dev.of_node, "clock-names");
26 if (dec_clk->clk_num > 0) {
27 dec_clk->clk_info = devm_kcalloc(&pdev->dev,
28 dec_clk->clk_num, sizeof(*clk_info),
30 if (!dec_clk->clk_info)
33 dev_err(&pdev->dev, "Failed to get vdec clock count");
37 for (i = 0; i < dec_clk->clk_num; i++) {
38 clk_info = &dec_clk->clk_info[i];
39 ret = of_property_read_string_index(pdev->dev.of_node,
40 "clock-names", i, &clk_info->clk_name);
42 dev_err(&pdev->dev, "Failed to get clock name id = %d", i);
45 clk_info->vcodec_clk = devm_clk_get(&pdev->dev,
47 if (IS_ERR(clk_info->vcodec_clk)) {
48 dev_err(&pdev->dev, "devm_clk_get (%d)%s fail", i, clk_info->clk_name);
49 return PTR_ERR(clk_info->vcodec_clk);
55 EXPORT_SYMBOL_GPL(mtk_vcodec_init_dec_clk);
57 static int mtk_vcodec_dec_pw_on(struct mtk_vcodec_pm *pm)
61 ret = pm_runtime_resume_and_get(pm->dev);
63 dev_err(pm->dev, "pm_runtime_resume_and_get fail %d", ret);
68 static void mtk_vcodec_dec_pw_off(struct mtk_vcodec_pm *pm)
72 ret = pm_runtime_put(pm->dev);
73 if (ret && ret != -EAGAIN)
74 dev_err(pm->dev, "pm_runtime_put fail %d", ret);
77 static void mtk_vcodec_dec_clock_on(struct mtk_vcodec_pm *pm)
79 struct mtk_vcodec_clk *dec_clk;
82 dec_clk = &pm->vdec_clk;
83 for (i = 0; i < dec_clk->clk_num; i++) {
84 ret = clk_prepare_enable(dec_clk->clk_info[i].vcodec_clk);
86 dev_err(pm->dev, "clk_prepare_enable %d %s fail %d", i,
87 dec_clk->clk_info[i].clk_name, ret);
94 for (i -= 1; i >= 0; i--)
95 clk_disable_unprepare(dec_clk->clk_info[i].vcodec_clk);
98 static void mtk_vcodec_dec_clock_off(struct mtk_vcodec_pm *pm)
100 struct mtk_vcodec_clk *dec_clk;
103 dec_clk = &pm->vdec_clk;
104 for (i = dec_clk->clk_num - 1; i >= 0; i--)
105 clk_disable_unprepare(dec_clk->clk_info[i].vcodec_clk);
108 static void mtk_vcodec_dec_enable_irq(struct mtk_vcodec_dec_dev *vdec_dev, int hw_idx)
110 struct mtk_vdec_hw_dev *subdev_dev;
112 if (!test_bit(hw_idx, vdec_dev->subdev_bitmap))
115 if (vdec_dev->vdec_pdata->is_subdev_supported) {
116 subdev_dev = mtk_vcodec_get_hw_dev(vdec_dev, hw_idx);
118 enable_irq(subdev_dev->dec_irq);
120 dev_err(&vdec_dev->plat_dev->dev, "Failed to get hw dev\n");
122 enable_irq(vdec_dev->dec_irq);
126 static void mtk_vcodec_dec_disable_irq(struct mtk_vcodec_dec_dev *vdec_dev, int hw_idx)
128 struct mtk_vdec_hw_dev *subdev_dev;
130 if (!test_bit(hw_idx, vdec_dev->subdev_bitmap))
133 if (vdec_dev->vdec_pdata->is_subdev_supported) {
134 subdev_dev = mtk_vcodec_get_hw_dev(vdec_dev, hw_idx);
136 disable_irq(subdev_dev->dec_irq);
138 dev_err(&vdec_dev->plat_dev->dev, "Failed to get hw dev\n");
140 disable_irq(vdec_dev->dec_irq);
144 static void mtk_vcodec_load_racing_info(struct mtk_vcodec_dec_ctx *ctx)
146 void __iomem *vdec_racing_addr;
149 mutex_lock(&ctx->dev->dec_racing_info_mutex);
150 if (atomic_inc_return(&ctx->dev->dec_active_cnt) == 1) {
151 vdec_racing_addr = ctx->dev->reg_base[VDEC_MISC] + 0x100;
152 for (j = 0; j < 132; j++)
153 writel(ctx->dev->vdec_racing_info[j], vdec_racing_addr + j * 4);
155 mutex_unlock(&ctx->dev->dec_racing_info_mutex);
158 static void mtk_vcodec_record_racing_info(struct mtk_vcodec_dec_ctx *ctx)
160 void __iomem *vdec_racing_addr;
163 mutex_lock(&ctx->dev->dec_racing_info_mutex);
164 if (atomic_dec_and_test(&ctx->dev->dec_active_cnt)) {
165 vdec_racing_addr = ctx->dev->reg_base[VDEC_MISC] + 0x100;
166 for (j = 0; j < 132; j++)
167 ctx->dev->vdec_racing_info[j] = readl(vdec_racing_addr + j * 4);
169 mutex_unlock(&ctx->dev->dec_racing_info_mutex);
172 static struct mtk_vcodec_pm *mtk_vcodec_dec_get_pm(struct mtk_vcodec_dec_dev *vdec_dev,
175 struct mtk_vdec_hw_dev *subdev_dev;
177 if (!test_bit(hw_idx, vdec_dev->subdev_bitmap))
180 if (vdec_dev->vdec_pdata->is_subdev_supported) {
181 subdev_dev = mtk_vcodec_get_hw_dev(vdec_dev, hw_idx);
183 return &subdev_dev->pm;
185 dev_err(&vdec_dev->plat_dev->dev, "Failed to get hw dev\n");
189 return &vdec_dev->pm;
192 static void mtk_vcodec_dec_child_dev_on(struct mtk_vcodec_dec_dev *vdec_dev,
195 struct mtk_vcodec_pm *pm;
197 pm = mtk_vcodec_dec_get_pm(vdec_dev, hw_idx);
199 mtk_vcodec_dec_pw_on(pm);
200 mtk_vcodec_dec_clock_on(pm);
203 if (hw_idx == MTK_VDEC_LAT0) {
204 pm = mtk_vcodec_dec_get_pm(vdec_dev, MTK_VDEC_LAT_SOC);
206 mtk_vcodec_dec_pw_on(pm);
207 mtk_vcodec_dec_clock_on(pm);
212 static void mtk_vcodec_dec_child_dev_off(struct mtk_vcodec_dec_dev *vdec_dev,
215 struct mtk_vcodec_pm *pm;
217 pm = mtk_vcodec_dec_get_pm(vdec_dev, hw_idx);
219 mtk_vcodec_dec_clock_off(pm);
220 mtk_vcodec_dec_pw_off(pm);
223 if (hw_idx == MTK_VDEC_LAT0) {
224 pm = mtk_vcodec_dec_get_pm(vdec_dev, MTK_VDEC_LAT_SOC);
226 mtk_vcodec_dec_clock_off(pm);
227 mtk_vcodec_dec_pw_off(pm);
232 void mtk_vcodec_dec_enable_hardware(struct mtk_vcodec_dec_ctx *ctx, int hw_idx)
234 mutex_lock(&ctx->dev->dec_mutex[hw_idx]);
236 if (IS_VDEC_LAT_ARCH(ctx->dev->vdec_pdata->hw_arch) &&
237 hw_idx == MTK_VDEC_CORE)
238 mtk_vcodec_dec_child_dev_on(ctx->dev, MTK_VDEC_LAT0);
239 mtk_vcodec_dec_child_dev_on(ctx->dev, hw_idx);
241 mtk_vcodec_dec_enable_irq(ctx->dev, hw_idx);
243 if (IS_VDEC_INNER_RACING(ctx->dev->dec_capability))
244 mtk_vcodec_load_racing_info(ctx);
246 EXPORT_SYMBOL_GPL(mtk_vcodec_dec_enable_hardware);
248 void mtk_vcodec_dec_disable_hardware(struct mtk_vcodec_dec_ctx *ctx, int hw_idx)
250 if (IS_VDEC_INNER_RACING(ctx->dev->dec_capability))
251 mtk_vcodec_record_racing_info(ctx);
253 mtk_vcodec_dec_disable_irq(ctx->dev, hw_idx);
255 mtk_vcodec_dec_child_dev_off(ctx->dev, hw_idx);
256 if (IS_VDEC_LAT_ARCH(ctx->dev->vdec_pdata->hw_arch) &&
257 hw_idx == MTK_VDEC_CORE)
258 mtk_vcodec_dec_child_dev_off(ctx->dev, MTK_VDEC_LAT0);
260 mutex_unlock(&ctx->dev->dec_mutex[hw_idx]);
262 EXPORT_SYMBOL_GPL(mtk_vcodec_dec_disable_hardware);