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_enc_pm.h"
14 #include "mtk_vcodec_util.h"
15
mtk_vcodec_init_enc_pm(struct mtk_vcodec_dev * mtkdev)16 int mtk_vcodec_init_enc_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 *enc_clk;
22 struct mtk_vcodec_clk_info *clk_info;
23 int ret = 0, i = 0;
24 struct device *dev;
25
26 pdev = mtkdev->plat_dev;
27 pm = &mtkdev->pm;
28 memset(pm, 0, sizeof(struct mtk_vcodec_pm));
29 pm->mtkdev = mtkdev;
30 pm->dev = &pdev->dev;
31 dev = &pdev->dev;
32 enc_clk = &pm->venc_clk;
33
34 node = of_parse_phandle(dev->of_node, "mediatek,larb", 0);
35 if (!node) {
36 mtk_v4l2_err("no mediatek,larb found");
37 return -ENODEV;
38 }
39 pdev = of_find_device_by_node(node);
40 of_node_put(node);
41 if (!pdev) {
42 mtk_v4l2_err("no mediatek,larb device found");
43 return -ENODEV;
44 }
45 pm->larbvenc = &pdev->dev;
46
47 node = of_parse_phandle(dev->of_node, "mediatek,larb", 1);
48 if (!node) {
49 mtk_v4l2_err("no mediatek,larb found");
50 return -ENODEV;
51 }
52
53 pdev = of_find_device_by_node(node);
54 of_node_put(node);
55 if (!pdev) {
56 mtk_v4l2_err("no mediatek,larb device found");
57 return -ENODEV;
58 }
59
60 pm->larbvenclt = &pdev->dev;
61 pdev = mtkdev->plat_dev;
62 pm->dev = &pdev->dev;
63
64 enc_clk->clk_num = of_property_count_strings(pdev->dev.of_node,
65 "clock-names");
66 if (enc_clk->clk_num > 0) {
67 enc_clk->clk_info = devm_kcalloc(&pdev->dev,
68 enc_clk->clk_num, sizeof(*clk_info),
69 GFP_KERNEL);
70 if (!enc_clk->clk_info)
71 return -ENOMEM;
72 } else {
73 mtk_v4l2_err("Failed to get venc clock count");
74 return -EINVAL;
75 }
76
77 for (i = 0; i < enc_clk->clk_num; i++) {
78 clk_info = &enc_clk->clk_info[i];
79 ret = of_property_read_string_index(pdev->dev.of_node,
80 "clock-names", i, &clk_info->clk_name);
81 if (ret) {
82 mtk_v4l2_err("venc failed to get clk name %d", i);
83 return ret;
84 }
85 clk_info->vcodec_clk = devm_clk_get(&pdev->dev,
86 clk_info->clk_name);
87 if (IS_ERR(clk_info->vcodec_clk)) {
88 mtk_v4l2_err("venc devm_clk_get (%d)%s fail", i,
89 clk_info->clk_name);
90 return PTR_ERR(clk_info->vcodec_clk);
91 }
92 }
93
94 return ret;
95 }
96
mtk_vcodec_release_enc_pm(struct mtk_vcodec_dev * mtkdev)97 void mtk_vcodec_release_enc_pm(struct mtk_vcodec_dev *mtkdev)
98 {
99 }
100
101
mtk_vcodec_enc_clock_on(struct mtk_vcodec_pm * pm)102 void mtk_vcodec_enc_clock_on(struct mtk_vcodec_pm *pm)
103 {
104 struct mtk_vcodec_clk *enc_clk = &pm->venc_clk;
105 int ret, i = 0;
106
107 for (i = 0; i < enc_clk->clk_num; i++) {
108 ret = clk_prepare_enable(enc_clk->clk_info[i].vcodec_clk);
109 if (ret) {
110 mtk_v4l2_err("venc clk_prepare_enable %d %s fail %d", i,
111 enc_clk->clk_info[i].clk_name, ret);
112 goto clkerr;
113 }
114 }
115
116 ret = mtk_smi_larb_get(pm->larbvenc);
117 if (ret) {
118 mtk_v4l2_err("mtk_smi_larb_get larb3 fail %d", ret);
119 goto larbvencerr;
120 }
121 ret = mtk_smi_larb_get(pm->larbvenclt);
122 if (ret) {
123 mtk_v4l2_err("mtk_smi_larb_get larb4 fail %d", ret);
124 goto larbvenclterr;
125 }
126 return;
127
128 larbvenclterr:
129 mtk_smi_larb_put(pm->larbvenc);
130 larbvencerr:
131 clkerr:
132 for (i -= 1; i >= 0; i--)
133 clk_disable_unprepare(enc_clk->clk_info[i].vcodec_clk);
134 }
135
mtk_vcodec_enc_clock_off(struct mtk_vcodec_pm * pm)136 void mtk_vcodec_enc_clock_off(struct mtk_vcodec_pm *pm)
137 {
138 struct mtk_vcodec_clk *enc_clk = &pm->venc_clk;
139 int i = 0;
140
141 mtk_smi_larb_put(pm->larbvenc);
142 mtk_smi_larb_put(pm->larbvenclt);
143 for (i = enc_clk->clk_num - 1; i >= 0; i--)
144 clk_disable_unprepare(enc_clk->clk_info[i].vcodec_clk);
145 }
146