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