xref: /linux/drivers/media/platform/mediatek/vcodec/encoder/mtk_vcodec_enc_pm.c (revision 6fd600d742744dc7ef7fc65ca26daa2b1163158a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: Tiffany Lin <tiffany.lin@mediatek.com>
5 */
6 
7 #include <linux/clk.h>
8 #include <linux/of.h>
9 #include <linux/pm_runtime.h>
10 
11 #include "mtk_vcodec_enc_drv.h"
12 #include "mtk_vcodec_enc_pm.h"
13 
mtk_vcodec_init_enc_clk(struct mtk_vcodec_enc_dev * mtkdev)14 int mtk_vcodec_init_enc_clk(struct mtk_vcodec_enc_dev *mtkdev)
15 {
16 	struct platform_device *pdev;
17 	struct mtk_vcodec_pm *pm;
18 	struct mtk_vcodec_clk *enc_clk;
19 	struct mtk_vcodec_clk_info *clk_info;
20 	int ret, i;
21 
22 	pdev = mtkdev->plat_dev;
23 	pm = &mtkdev->pm;
24 	memset(pm, 0, sizeof(struct mtk_vcodec_pm));
25 	pm->dev = &pdev->dev;
26 	enc_clk = &pm->venc_clk;
27 
28 	enc_clk->clk_num = of_property_count_strings(pdev->dev.of_node,
29 		"clock-names");
30 	if (enc_clk->clk_num > 0) {
31 		enc_clk->clk_info = devm_kcalloc(&pdev->dev,
32 			enc_clk->clk_num, sizeof(*clk_info),
33 			GFP_KERNEL);
34 		if (!enc_clk->clk_info)
35 			return -ENOMEM;
36 	} else {
37 		dev_err(pm->dev, "[MTK VCODEC] Failed to get venc clock count");
38 		return -EINVAL;
39 	}
40 
41 	for (i = 0; i < enc_clk->clk_num; i++) {
42 		clk_info = &enc_clk->clk_info[i];
43 		ret = of_property_read_string_index(pdev->dev.of_node,
44 			"clock-names", i, &clk_info->clk_name);
45 		if (ret) {
46 			dev_err(pm->dev, "[MTK VCODEC] venc failed to get clk name %d", i);
47 			return ret;
48 		}
49 		clk_info->vcodec_clk = devm_clk_get(&pdev->dev,
50 			clk_info->clk_name);
51 		if (IS_ERR(clk_info->vcodec_clk)) {
52 			dev_err(pm->dev, "[MTK VCODEC] venc devm_clk_get (%d)%s fail", i,
53 				clk_info->clk_name);
54 			return PTR_ERR(clk_info->vcodec_clk);
55 		}
56 	}
57 
58 	return 0;
59 }
60 
mtk_vcodec_enc_pw_on(struct mtk_vcodec_pm * pm)61 int mtk_vcodec_enc_pw_on(struct mtk_vcodec_pm *pm)
62 {
63 	int ret;
64 
65 	ret = pm_runtime_resume_and_get(pm->dev);
66 	if (ret)
67 		dev_err(pm->dev, "pm_runtime_resume_and_get fail: %d", ret);
68 
69 	return ret;
70 }
71 
mtk_vcodec_enc_pw_off(struct mtk_vcodec_pm * pm)72 void mtk_vcodec_enc_pw_off(struct mtk_vcodec_pm *pm)
73 {
74 	int ret;
75 
76 	ret = pm_runtime_put(pm->dev);
77 	if (ret && ret != -EAGAIN)
78 		dev_err(pm->dev, "pm_runtime_put fail %d", ret);
79 }
80 
mtk_vcodec_enc_clock_on(struct mtk_vcodec_pm * pm)81 void mtk_vcodec_enc_clock_on(struct mtk_vcodec_pm *pm)
82 {
83 	struct mtk_vcodec_clk *enc_clk = &pm->venc_clk;
84 	int ret, i = 0;
85 
86 	for (i = 0; i < enc_clk->clk_num; i++) {
87 		ret = clk_prepare_enable(enc_clk->clk_info[i].vcodec_clk);
88 		if (ret) {
89 			dev_err(pm->dev, "[MTK VCODEC] venc clk_prepare_enable %d %s fail %d", i,
90 				enc_clk->clk_info[i].clk_name, ret);
91 			goto clkerr;
92 		}
93 	}
94 
95 	return;
96 
97 clkerr:
98 	for (i -= 1; i >= 0; i--)
99 		clk_disable_unprepare(enc_clk->clk_info[i].vcodec_clk);
100 }
101 
mtk_vcodec_enc_clock_off(struct mtk_vcodec_pm * pm)102 void mtk_vcodec_enc_clock_off(struct mtk_vcodec_pm *pm)
103 {
104 	struct mtk_vcodec_clk *enc_clk = &pm->venc_clk;
105 	int i = 0;
106 
107 	for (i = enc_clk->clk_num - 1; i >= 0; i--)
108 		clk_disable_unprepare(enc_clk->clk_info[i].vcodec_clk);
109 }
110