1424d6d1aSYC Hung // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2424d6d1aSYC Hung //
3424d6d1aSYC Hung // Copyright(c) 2021 Mediatek Corporation. All rights reserved.
4424d6d1aSYC Hung //
5424d6d1aSYC Hung // Author: YC Hung <yc.hung@mediatek.com>
6424d6d1aSYC Hung //
7424d6d1aSYC Hung // Hardware interface for mt8195 DSP clock
8424d6d1aSYC Hung
9424d6d1aSYC Hung #include <linux/clk.h>
10424d6d1aSYC Hung #include <linux/io.h>
11424d6d1aSYC Hung #include "mt8195.h"
12424d6d1aSYC Hung #include "mt8195-clk.h"
13424d6d1aSYC Hung #include "../adsp_helper.h"
14424d6d1aSYC Hung #include "../../sof-audio.h"
15424d6d1aSYC Hung
16424d6d1aSYC Hung static const char *adsp_clks[ADSP_CLK_MAX] = {
17424d6d1aSYC Hung [CLK_TOP_ADSP] = "adsp_sel",
18424d6d1aSYC Hung [CLK_TOP_CLK26M] = "clk26m_ck",
19424d6d1aSYC Hung [CLK_TOP_AUDIO_LOCAL_BUS] = "audio_local_bus",
20424d6d1aSYC Hung [CLK_TOP_MAINPLL_D7_D2] = "mainpll_d7_d2",
21424d6d1aSYC Hung [CLK_SCP_ADSP_AUDIODSP] = "scp_adsp_audiodsp",
22424d6d1aSYC Hung [CLK_TOP_AUDIO_H] = "audio_h",
23424d6d1aSYC Hung };
24424d6d1aSYC Hung
mt8195_adsp_init_clock(struct snd_sof_dev * sdev)25424d6d1aSYC Hung int mt8195_adsp_init_clock(struct snd_sof_dev *sdev)
26424d6d1aSYC Hung {
27424d6d1aSYC Hung struct device *dev = sdev->dev;
28424d6d1aSYC Hung struct adsp_priv *priv = sdev->pdata->hw_pdata;
29424d6d1aSYC Hung int i;
30424d6d1aSYC Hung
31424d6d1aSYC Hung priv->clk = devm_kcalloc(dev, ADSP_CLK_MAX, sizeof(*priv->clk), GFP_KERNEL);
32424d6d1aSYC Hung
33424d6d1aSYC Hung if (!priv->clk)
34424d6d1aSYC Hung return -ENOMEM;
35424d6d1aSYC Hung
36424d6d1aSYC Hung for (i = 0; i < ADSP_CLK_MAX; i++) {
37424d6d1aSYC Hung priv->clk[i] = devm_clk_get(dev, adsp_clks[i]);
38424d6d1aSYC Hung if (IS_ERR(priv->clk[i]))
39424d6d1aSYC Hung return PTR_ERR(priv->clk[i]);
40424d6d1aSYC Hung }
41424d6d1aSYC Hung
42424d6d1aSYC Hung return 0;
43424d6d1aSYC Hung }
44424d6d1aSYC Hung
adsp_enable_all_clock(struct snd_sof_dev * sdev)45424d6d1aSYC Hung static int adsp_enable_all_clock(struct snd_sof_dev *sdev)
46424d6d1aSYC Hung {
47424d6d1aSYC Hung struct device *dev = sdev->dev;
48424d6d1aSYC Hung struct adsp_priv *priv = sdev->pdata->hw_pdata;
49424d6d1aSYC Hung int ret;
50424d6d1aSYC Hung
51424d6d1aSYC Hung ret = clk_prepare_enable(priv->clk[CLK_TOP_MAINPLL_D7_D2]);
52424d6d1aSYC Hung if (ret) {
53424d6d1aSYC Hung dev_err(dev, "%s clk_prepare_enable(mainpll_d7_d2) fail %d\n",
54424d6d1aSYC Hung __func__, ret);
55424d6d1aSYC Hung return ret;
56424d6d1aSYC Hung }
57424d6d1aSYC Hung
58424d6d1aSYC Hung ret = clk_prepare_enable(priv->clk[CLK_TOP_ADSP]);
59424d6d1aSYC Hung if (ret) {
60424d6d1aSYC Hung dev_err(dev, "%s clk_prepare_enable(adsp_sel) fail %d\n",
61424d6d1aSYC Hung __func__, ret);
62424d6d1aSYC Hung goto disable_mainpll_d7_d2_clk;
63424d6d1aSYC Hung }
64424d6d1aSYC Hung
65424d6d1aSYC Hung ret = clk_prepare_enable(priv->clk[CLK_TOP_AUDIO_LOCAL_BUS]);
66424d6d1aSYC Hung if (ret) {
67424d6d1aSYC Hung dev_err(dev, "%s clk_prepare_enable(audio_local_bus) fail %d\n",
68424d6d1aSYC Hung __func__, ret);
69424d6d1aSYC Hung goto disable_dsp_sel_clk;
70424d6d1aSYC Hung }
71424d6d1aSYC Hung
72424d6d1aSYC Hung ret = clk_prepare_enable(priv->clk[CLK_SCP_ADSP_AUDIODSP]);
73424d6d1aSYC Hung if (ret) {
74424d6d1aSYC Hung dev_err(dev, "%s clk_prepare_enable(scp_adsp_audiodsp) fail %d\n",
75424d6d1aSYC Hung __func__, ret);
76424d6d1aSYC Hung goto disable_audio_local_bus_clk;
77424d6d1aSYC Hung }
78424d6d1aSYC Hung
79424d6d1aSYC Hung ret = clk_prepare_enable(priv->clk[CLK_TOP_AUDIO_H]);
80424d6d1aSYC Hung if (ret) {
81424d6d1aSYC Hung dev_err(dev, "%s clk_prepare_enable(audio_h) fail %d\n",
82424d6d1aSYC Hung __func__, ret);
83424d6d1aSYC Hung goto disable_scp_adsp_audiodsp_clk;
84424d6d1aSYC Hung }
85424d6d1aSYC Hung
86424d6d1aSYC Hung return 0;
87424d6d1aSYC Hung
88424d6d1aSYC Hung disable_scp_adsp_audiodsp_clk:
89424d6d1aSYC Hung clk_disable_unprepare(priv->clk[CLK_SCP_ADSP_AUDIODSP]);
90424d6d1aSYC Hung disable_audio_local_bus_clk:
91424d6d1aSYC Hung clk_disable_unprepare(priv->clk[CLK_TOP_AUDIO_LOCAL_BUS]);
92424d6d1aSYC Hung disable_dsp_sel_clk:
93424d6d1aSYC Hung clk_disable_unprepare(priv->clk[CLK_TOP_ADSP]);
94424d6d1aSYC Hung disable_mainpll_d7_d2_clk:
95424d6d1aSYC Hung clk_disable_unprepare(priv->clk[CLK_TOP_MAINPLL_D7_D2]);
96424d6d1aSYC Hung
97424d6d1aSYC Hung return ret;
98424d6d1aSYC Hung }
99424d6d1aSYC Hung
adsp_disable_all_clock(struct snd_sof_dev * sdev)100424d6d1aSYC Hung static void adsp_disable_all_clock(struct snd_sof_dev *sdev)
101424d6d1aSYC Hung {
102424d6d1aSYC Hung struct adsp_priv *priv = sdev->pdata->hw_pdata;
103424d6d1aSYC Hung
104424d6d1aSYC Hung clk_disable_unprepare(priv->clk[CLK_TOP_AUDIO_H]);
105424d6d1aSYC Hung clk_disable_unprepare(priv->clk[CLK_SCP_ADSP_AUDIODSP]);
106424d6d1aSYC Hung clk_disable_unprepare(priv->clk[CLK_TOP_AUDIO_LOCAL_BUS]);
107424d6d1aSYC Hung clk_disable_unprepare(priv->clk[CLK_TOP_ADSP]);
108424d6d1aSYC Hung clk_disable_unprepare(priv->clk[CLK_TOP_MAINPLL_D7_D2]);
109424d6d1aSYC Hung }
110424d6d1aSYC Hung
adsp_default_clk_init(struct snd_sof_dev * sdev,bool enable)111424d6d1aSYC Hung static int adsp_default_clk_init(struct snd_sof_dev *sdev, bool enable)
112424d6d1aSYC Hung {
113424d6d1aSYC Hung struct device *dev = sdev->dev;
114424d6d1aSYC Hung struct adsp_priv *priv = sdev->pdata->hw_pdata;
115424d6d1aSYC Hung int ret;
116424d6d1aSYC Hung
117424d6d1aSYC Hung dev_dbg(dev, "%s: %s\n", __func__, enable ? "on" : "off");
118424d6d1aSYC Hung
119424d6d1aSYC Hung if (enable) {
120424d6d1aSYC Hung ret = clk_set_parent(priv->clk[CLK_TOP_ADSP],
121424d6d1aSYC Hung priv->clk[CLK_TOP_CLK26M]);
122424d6d1aSYC Hung if (ret) {
123424d6d1aSYC Hung dev_err(dev, "failed to set dsp_sel to clk26m: %d\n", ret);
124424d6d1aSYC Hung return ret;
125424d6d1aSYC Hung }
126424d6d1aSYC Hung
127424d6d1aSYC Hung ret = clk_set_parent(priv->clk[CLK_TOP_AUDIO_LOCAL_BUS],
128424d6d1aSYC Hung priv->clk[CLK_TOP_MAINPLL_D7_D2]);
129424d6d1aSYC Hung if (ret) {
130424d6d1aSYC Hung dev_err(dev, "set audio_local_bus failed %d\n", ret);
131424d6d1aSYC Hung return ret;
132424d6d1aSYC Hung }
133424d6d1aSYC Hung
134*e53b2059SYC Hung ret = clk_set_parent(priv->clk[CLK_TOP_AUDIO_H],
135*e53b2059SYC Hung priv->clk[CLK_TOP_CLK26M]);
136*e53b2059SYC Hung if (ret) {
137*e53b2059SYC Hung dev_err(dev, "set audio_h_sel failed %d\n", ret);
138*e53b2059SYC Hung return ret;
139*e53b2059SYC Hung }
140*e53b2059SYC Hung
141424d6d1aSYC Hung ret = adsp_enable_all_clock(sdev);
142424d6d1aSYC Hung if (ret) {
143424d6d1aSYC Hung dev_err(dev, "failed to adsp_enable_clock: %d\n", ret);
144424d6d1aSYC Hung return ret;
145424d6d1aSYC Hung }
146424d6d1aSYC Hung } else {
147424d6d1aSYC Hung adsp_disable_all_clock(sdev);
148424d6d1aSYC Hung }
149424d6d1aSYC Hung
150424d6d1aSYC Hung return 0;
151424d6d1aSYC Hung }
152424d6d1aSYC Hung
adsp_clock_on(struct snd_sof_dev * sdev)153424d6d1aSYC Hung int adsp_clock_on(struct snd_sof_dev *sdev)
154424d6d1aSYC Hung {
155424d6d1aSYC Hung /* Open ADSP clock */
156424d6d1aSYC Hung return adsp_default_clk_init(sdev, 1);
157424d6d1aSYC Hung }
158424d6d1aSYC Hung
adsp_clock_off(struct snd_sof_dev * sdev)159424d6d1aSYC Hung int adsp_clock_off(struct snd_sof_dev *sdev)
160424d6d1aSYC Hung {
161424d6d1aSYC Hung /* Close ADSP clock */
162424d6d1aSYC Hung return adsp_default_clk_init(sdev, 0);
163424d6d1aSYC Hung }
164424d6d1aSYC Hung
165