1 // SPDX-License-Identifier: GPL-2.0-only
2 // SPDX-FileCopyrightText: Copyright (c) 2020-2025 NVIDIA CORPORATION. All rights reserved.
3 //
4 // tegra_audio_graph_card.c - Audio Graph based Tegra Machine Driver
5
6 #include <linux/math64.h>
7 #include <linux/module.h>
8 #include <linux/of.h>
9 #include <linux/platform_device.h>
10 #include <sound/graph_card.h>
11 #include <sound/pcm_params.h>
12 #include <sound/soc-dai.h>
13
14 #define MAX_PLLA_OUT0_DIV 128
15
16 #define simple_to_tegra_priv(simple) \
17 container_of(simple, struct tegra_audio_priv, simple)
18
19 enum srate_type {
20 /*
21 * Sample rates multiple of 8000 Hz and below are supported:
22 * ( 8000, 16000, 32000, 48000, 96000, 192000 Hz )
23 */
24 x8_RATE,
25
26 /*
27 * Sample rates multiple of 11025 Hz and below are supported:
28 * ( 11025, 22050, 44100, 88200, 176400 Hz )
29 */
30 x11_RATE,
31
32 NUM_RATE_TYPE,
33 };
34
35 struct tegra_audio_priv {
36 struct simple_util_priv simple;
37 struct clk *clk_plla_out0;
38 struct clk *clk_plla;
39 };
40
41 /* Tegra audio chip data */
42 struct tegra_audio_cdata {
43 unsigned int plla_rates[NUM_RATE_TYPE];
44 unsigned int plla_out0_rates[NUM_RATE_TYPE];
45 };
46
need_clk_update(struct snd_soc_dai * dai)47 static bool need_clk_update(struct snd_soc_dai *dai)
48 {
49 if (snd_soc_dai_is_dummy(dai) ||
50 !dai->driver->ops ||
51 !dai->driver->name)
52 return false;
53
54 if (strstr(dai->driver->name, "I2S") ||
55 strstr(dai->driver->name, "DMIC") ||
56 strstr(dai->driver->name, "DSPK"))
57 return true;
58
59 return false;
60 }
61
62 /* Setup PLL clock as per the given sample rate */
tegra_audio_graph_update_pll(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)63 static int tegra_audio_graph_update_pll(struct snd_pcm_substream *substream,
64 struct snd_pcm_hw_params *params)
65 {
66 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
67 struct simple_util_priv *simple = snd_soc_card_get_drvdata(rtd->card);
68 struct tegra_audio_priv *priv = simple_to_tegra_priv(simple);
69 struct device *dev = rtd->card->dev;
70 const struct tegra_audio_cdata *data = of_device_get_match_data(dev);
71 unsigned int plla_rate, plla_out0_rate, bclk;
72 unsigned int srate = params_rate(params);
73 int err;
74
75 switch (srate) {
76 case 11025:
77 case 22050:
78 case 44100:
79 case 88200:
80 case 176400:
81 plla_out0_rate = data->plla_out0_rates[x11_RATE];
82 plla_rate = data->plla_rates[x11_RATE];
83 break;
84 case 8000:
85 case 16000:
86 case 32000:
87 case 48000:
88 case 96000:
89 case 192000:
90 plla_out0_rate = data->plla_out0_rates[x8_RATE];
91 plla_rate = data->plla_rates[x8_RATE];
92 break;
93 default:
94 dev_err(rtd->card->dev, "Unsupported sample rate %u\n",
95 srate);
96 return -EINVAL;
97 }
98
99 /*
100 * Below is the clock relation:
101 *
102 * PLLA
103 * |
104 * |--> PLLA_OUT0
105 * |
106 * |---> I2S modules
107 * |
108 * |---> DMIC modules
109 * |
110 * |---> DSPK modules
111 *
112 *
113 * Default PLLA_OUT0 rate might be too high when I/O is running
114 * at minimum PCM configurations. This may result in incorrect
115 * clock rates and glitchy audio. The maximum divider is 128
116 * and any thing higher than that won't work. Thus reduce PLLA_OUT0
117 * to work for lower configurations.
118 *
119 * This problem is seen for I2S only, as DMIC and DSPK minimum
120 * clock requirements are under allowed divider limits.
121 */
122 bclk = srate * params_channels(params) * params_width(params);
123 if (div_u64(plla_out0_rate, bclk) > MAX_PLLA_OUT0_DIV)
124 plla_out0_rate >>= 1;
125
126 dev_dbg(rtd->card->dev,
127 "Update clock rates: PLLA(= %u Hz) and PLLA_OUT0(= %u Hz)\n",
128 plla_rate, plla_out0_rate);
129
130 /* Set PLLA rate */
131 err = clk_set_rate(priv->clk_plla, plla_rate);
132 if (err) {
133 dev_err(rtd->card->dev,
134 "Can't set plla rate for %u, err: %d\n",
135 plla_rate, err);
136 return err;
137 }
138
139 /* Set PLLA_OUT0 rate */
140 err = clk_set_rate(priv->clk_plla_out0, plla_out0_rate);
141 if (err) {
142 dev_err(rtd->card->dev,
143 "Can't set plla_out0 rate %u, err: %d\n",
144 plla_out0_rate, err);
145 return err;
146 }
147
148 return err;
149 }
150
tegra_audio_graph_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)151 static int tegra_audio_graph_hw_params(struct snd_pcm_substream *substream,
152 struct snd_pcm_hw_params *params)
153 {
154 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
155 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
156 int err;
157
158 if (need_clk_update(cpu_dai)) {
159 err = tegra_audio_graph_update_pll(substream, params);
160 if (err)
161 return err;
162 }
163
164 return simple_util_hw_params(substream, params);
165 }
166
167 static const struct snd_soc_ops tegra_audio_graph_ops = {
168 .startup = simple_util_startup,
169 .shutdown = simple_util_shutdown,
170 .hw_params = tegra_audio_graph_hw_params,
171 };
172
tegra_audio_graph_card_probe(struct snd_soc_card * card)173 static int tegra_audio_graph_card_probe(struct snd_soc_card *card)
174 {
175 struct simple_util_priv *simple = snd_soc_card_get_drvdata(card);
176 struct tegra_audio_priv *priv = simple_to_tegra_priv(simple);
177 int ret;
178
179 priv->clk_plla = devm_clk_get(card->dev, "pll_a");
180 if (IS_ERR(priv->clk_plla))
181 return dev_err_probe(card->dev, PTR_ERR(priv->clk_plla),
182 "can't retrieve clk pll_a\n");
183
184 priv->clk_plla_out0 = devm_clk_get(card->dev, "plla_out0");
185 if (IS_ERR(priv->clk_plla_out0))
186 return dev_err_probe(card->dev, PTR_ERR(priv->clk_plla_out0),
187 "can't retrieve clk plla_out0\n");
188
189 ret = graph_util_card_probe(card);
190 if (ret < 0)
191 return dev_err_probe(card->dev, ret, "graph_util_card_probe failed\n");
192
193 return ret;
194 }
195
tegra_audio_graph_probe(struct platform_device * pdev)196 static int tegra_audio_graph_probe(struct platform_device *pdev)
197 {
198 struct tegra_audio_priv *priv;
199 struct device *dev = &pdev->dev;
200 struct snd_soc_card *card;
201
202 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
203 if (!priv)
204 return -ENOMEM;
205
206 card = simple_priv_to_card(&priv->simple);
207 card->driver_name = "tegra-ape";
208
209 card->probe = tegra_audio_graph_card_probe;
210
211 /* audio_graph_parse_of() depends on below */
212 card->component_chaining = 1;
213 priv->simple.ops = &tegra_audio_graph_ops;
214 priv->simple.force_dpcm = 1;
215
216 return audio_graph_parse_of(&priv->simple, dev);
217 }
218
219 static const struct tegra_audio_cdata tegra210_data = {
220 /* PLLA */
221 .plla_rates[x8_RATE] = 368640000,
222 .plla_rates[x11_RATE] = 338688000,
223 /* PLLA_OUT0 */
224 .plla_out0_rates[x8_RATE] = 49152000,
225 .plla_out0_rates[x11_RATE] = 45158400,
226 };
227
228 static const struct tegra_audio_cdata tegra186_data = {
229 /* PLLA */
230 .plla_rates[x8_RATE] = 245760000,
231 .plla_rates[x11_RATE] = 270950400,
232 /* PLLA_OUT0 */
233 .plla_out0_rates[x8_RATE] = 49152000,
234 .plla_out0_rates[x11_RATE] = 45158400,
235 };
236
237 static const struct tegra_audio_cdata tegra238_data = {
238 /* PLLA */
239 .plla_rates[x8_RATE] = 1277952000,
240 .plla_rates[x11_RATE] = 1264435200,
241 /* PLLA_OUT0 */
242 .plla_out0_rates[x8_RATE] = 49152000,
243 .plla_out0_rates[x11_RATE] = 45158400,
244 };
245
246 static const struct tegra_audio_cdata tegra264_data = {
247 /* PLLA1 */
248 .plla_rates[x8_RATE] = 983040000,
249 .plla_rates[x11_RATE] = 993484800,
250 /* PLLA1_OUT1 */
251 .plla_out0_rates[x8_RATE] = 49152000,
252 .plla_out0_rates[x11_RATE] = 45158400,
253 };
254
255 static const struct of_device_id graph_of_tegra_match[] = {
256 { .compatible = "nvidia,tegra210-audio-graph-card",
257 .data = &tegra210_data },
258 { .compatible = "nvidia,tegra186-audio-graph-card",
259 .data = &tegra186_data },
260 { .compatible = "nvidia,tegra238-audio-graph-card",
261 .data = &tegra238_data },
262 { .compatible = "nvidia,tegra264-audio-graph-card",
263 .data = &tegra264_data },
264 {},
265 };
266 MODULE_DEVICE_TABLE(of, graph_of_tegra_match);
267
268 static struct platform_driver tegra_audio_graph_card = {
269 .driver = {
270 .name = "tegra-audio-graph-card",
271 .pm = &snd_soc_pm_ops,
272 .of_match_table = graph_of_tegra_match,
273 },
274 .probe = tegra_audio_graph_probe,
275 .remove = simple_util_remove,
276 };
277 module_platform_driver(tegra_audio_graph_card);
278
279 MODULE_LICENSE("GPL v2");
280 MODULE_DESCRIPTION("ASoC Tegra Audio Graph Sound Card");
281 MODULE_AUTHOR("Sameer Pujar <spujar@nvidia.com>");
282