1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: Tiffany Lin <tiffany.lin@mediatek.com>
8 #include <linux/of_address.h>
9 #include <linux/of_platform.h>
10 #include <linux/pm_runtime.h>
11 #include <soc/mediatek/smi.h>
13 #include "mtk_vcodec_enc_pm.h"
14 #include "mtk_vcodec_util.h"
18 int mtk_vcodec_init_enc_pm(struct mtk_vcodec_dev
*mtkdev
)
20 struct device_node
*node
;
21 struct platform_device
*pdev
;
22 struct mtk_vcodec_pm
*pm
;
23 struct mtk_vcodec_clk
*enc_clk
;
24 struct mtk_vcodec_clk_info
*clk_info
;
28 pdev
= mtkdev
->plat_dev
;
30 memset(pm
, 0, sizeof(struct mtk_vcodec_pm
));
34 enc_clk
= &pm
->venc_clk
;
36 node
= of_parse_phandle(dev
->of_node
, "mediatek,larb", 0);
38 mtk_v4l2_err("no mediatek,larb found");
41 pdev
= of_find_device_by_node(node
);
44 mtk_v4l2_err("no mediatek,larb device found");
47 pm
->larbvenc
= &pdev
->dev
;
49 node
= of_parse_phandle(dev
->of_node
, "mediatek,larb", 1);
51 mtk_v4l2_err("no mediatek,larb found");
55 pdev
= of_find_device_by_node(node
);
58 mtk_v4l2_err("no mediatek,larb device found");
62 pm
->larbvenclt
= &pdev
->dev
;
63 pdev
= mtkdev
->plat_dev
;
66 enc_clk
->clk_num
= of_property_count_strings(pdev
->dev
.of_node
,
68 if (enc_clk
->clk_num
> 0) {
69 enc_clk
->clk_info
= devm_kcalloc(&pdev
->dev
,
70 enc_clk
->clk_num
, sizeof(*clk_info
),
72 if (!enc_clk
->clk_info
)
75 mtk_v4l2_err("Failed to get venc clock count");
79 for (i
= 0; i
< enc_clk
->clk_num
; i
++) {
80 clk_info
= &enc_clk
->clk_info
[i
];
81 ret
= of_property_read_string_index(pdev
->dev
.of_node
,
82 "clock-names", i
, &clk_info
->clk_name
);
84 mtk_v4l2_err("venc failed to get clk name %d", i
);
87 clk_info
->vcodec_clk
= devm_clk_get(&pdev
->dev
,
89 if (IS_ERR(clk_info
->vcodec_clk
)) {
90 mtk_v4l2_err("venc devm_clk_get (%d)%s fail", i
,
92 return PTR_ERR(clk_info
->vcodec_clk
);
99 void mtk_vcodec_release_enc_pm(struct mtk_vcodec_dev
*mtkdev
)
104 void mtk_vcodec_enc_clock_on(struct mtk_vcodec_pm
*pm
)
106 struct mtk_vcodec_clk
*enc_clk
= &pm
->venc_clk
;
109 for (i
= 0; i
< enc_clk
->clk_num
; i
++) {
110 ret
= clk_prepare_enable(enc_clk
->clk_info
[i
].vcodec_clk
);
112 mtk_v4l2_err("venc clk_prepare_enable %d %s fail %d", i
,
113 enc_clk
->clk_info
[i
].clk_name
, ret
);
118 ret
= mtk_smi_larb_get(pm
->larbvenc
);
120 mtk_v4l2_err("mtk_smi_larb_get larb3 fail %d", ret
);
123 ret
= mtk_smi_larb_get(pm
->larbvenclt
);
125 mtk_v4l2_err("mtk_smi_larb_get larb4 fail %d", ret
);
131 mtk_smi_larb_put(pm
->larbvenc
);
134 for (i
-= 1; i
>= 0; i
--)
135 clk_disable_unprepare(enc_clk
->clk_info
[i
].vcodec_clk
);
138 void mtk_vcodec_enc_clock_off(struct mtk_vcodec_pm
*pm
)
140 struct mtk_vcodec_clk
*enc_clk
= &pm
->venc_clk
;
143 mtk_smi_larb_put(pm
->larbvenc
);
144 mtk_smi_larb_put(pm
->larbvenclt
);
145 for (i
= enc_clk
->clk_num
- 1; i
>= 0; i
--)
146 clk_disable_unprepare(enc_clk
->clk_info
[i
].vcodec_clk
);