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_address.h>
9 #include <linux/of_platform.h>
10 #include <linux/pm_runtime.h>
11 #include <soc/mediatek/smi.h>
12 
13 #include "mtk_vcodec_dec_pm.h"
14 #include "mtk_vcodec_util.h"
15 #include "mtk_vpu.h"
16 
mtk_vcodec_init_dec_pm(struct mtk_vcodec_dev * mtkdev)17 int mtk_vcodec_init_dec_pm(struct mtk_vcodec_dev *mtkdev)
18 {
19 	struct device_node *node;
20 	struct platform_device *pdev;
21 	struct mtk_vcodec_pm *pm;
22 	struct mtk_vcodec_clk *dec_clk;
23 	struct mtk_vcodec_clk_info *clk_info;
24 	int i = 0, ret = 0;
25 
26 	pdev = mtkdev->plat_dev;
27 	pm = &mtkdev->pm;
28 	pm->mtkdev = mtkdev;
29 	dec_clk = &pm->vdec_clk;
30 	node = of_parse_phandle(pdev->dev.of_node, "mediatek,larb", 0);
31 	if (!node) {
32 		mtk_v4l2_err("of_parse_phandle mediatek,larb fail!");
33 		return -1;
34 	}
35 
36 	pdev = of_find_device_by_node(node);
37 	of_node_put(node);
38 	if (WARN_ON(!pdev)) {
39 		return -1;
40 	}
41 	pm->larbvdec = &pdev->dev;
42 	pdev = mtkdev->plat_dev;
43 	pm->dev = &pdev->dev;
44 
45 	dec_clk->clk_num =
46 		of_property_count_strings(pdev->dev.of_node, "clock-names");
47 	if (dec_clk->clk_num > 0) {
48 		dec_clk->clk_info = devm_kcalloc(&pdev->dev,
49 			dec_clk->clk_num, sizeof(*clk_info),
50 			GFP_KERNEL);
51 		if (!dec_clk->clk_info)
52 			return -ENOMEM;
53 	} else {
54 		mtk_v4l2_err("Failed to get vdec clock count");
55 		return -EINVAL;
56 	}
57 
58 	for (i = 0; i < dec_clk->clk_num; i++) {
59 		clk_info = &dec_clk->clk_info[i];
60 		ret = of_property_read_string_index(pdev->dev.of_node,
61 			"clock-names", i, &clk_info->clk_name);
62 		if (ret) {
63 			mtk_v4l2_err("Failed to get clock name id = %d", i);
64 			return ret;
65 		}
66 		clk_info->vcodec_clk = devm_clk_get(&pdev->dev,
67 			clk_info->clk_name);
68 		if (IS_ERR(clk_info->vcodec_clk)) {
69 			mtk_v4l2_err("devm_clk_get (%d)%s fail", i,
70 				clk_info->clk_name);
71 			return PTR_ERR(clk_info->vcodec_clk);
72 		}
73 	}
74 
75 	pm_runtime_enable(&pdev->dev);
76 
77 	return ret;
78 }
79 
mtk_vcodec_release_dec_pm(struct mtk_vcodec_dev * dev)80 void mtk_vcodec_release_dec_pm(struct mtk_vcodec_dev *dev)
81 {
82 	pm_runtime_disable(dev->pm.dev);
83 }
84 
mtk_vcodec_dec_pw_on(struct mtk_vcodec_pm * pm)85 void mtk_vcodec_dec_pw_on(struct mtk_vcodec_pm *pm)
86 {
87 	int ret;
88 
89 	ret = pm_runtime_get_sync(pm->dev);
90 	if (ret)
91 		mtk_v4l2_err("pm_runtime_get_sync fail %d", ret);
92 }
93 
mtk_vcodec_dec_pw_off(struct mtk_vcodec_pm * pm)94 void mtk_vcodec_dec_pw_off(struct mtk_vcodec_pm *pm)
95 {
96 	int ret;
97 
98 	ret = pm_runtime_put_sync(pm->dev);
99 	if (ret)
100 		mtk_v4l2_err("pm_runtime_put_sync fail %d", ret);
101 }
102 
mtk_vcodec_dec_clock_on(struct mtk_vcodec_pm * pm)103 void mtk_vcodec_dec_clock_on(struct mtk_vcodec_pm *pm)
104 {
105 	struct mtk_vcodec_clk *dec_clk = &pm->vdec_clk;
106 	int ret, i = 0;
107 
108 	for (i = 0; i < dec_clk->clk_num; i++) {
109 		ret = clk_prepare_enable(dec_clk->clk_info[i].vcodec_clk);
110 		if (ret) {
111 			mtk_v4l2_err("clk_prepare_enable %d %s fail %d", i,
112 				dec_clk->clk_info[i].clk_name, ret);
113 			goto error;
114 		}
115 	}
116 
117 	ret = mtk_smi_larb_get(pm->larbvdec);
118 	if (ret) {
119 		mtk_v4l2_err("mtk_smi_larb_get larbvdec fail %d", ret);
120 		goto error;
121 	}
122 	return;
123 
124 error:
125 	for (i -= 1; i >= 0; i--)
126 		clk_disable_unprepare(dec_clk->clk_info[i].vcodec_clk);
127 }
128 
mtk_vcodec_dec_clock_off(struct mtk_vcodec_pm * pm)129 void mtk_vcodec_dec_clock_off(struct mtk_vcodec_pm *pm)
130 {
131 	struct mtk_vcodec_clk *dec_clk = &pm->vdec_clk;
132 	int i = 0;
133 
134 	mtk_smi_larb_put(pm->larbvdec);
135 	for (i = dec_clk->clk_num - 1; i >= 0; i--)
136 		clk_disable_unprepare(dec_clk->clk_info[i].vcodec_clk);
137 }
138