1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (C) 2017 Samsung Electronics Co., Ltd.
6 #include <linux/clk-provider.h>
8 #include <linux/of_device.h>
9 #include <linux/module.h>
10 #include <sound/soc.h>
11 #include <sound/pcm_params.h>
16 struct snd_soc_card card;
17 struct clk *clk_i2s_bus;
20 /* Spinlock protecting fields below */
22 unsigned int be_sample_rate;
26 static int odroid_card_fe_startup(struct snd_pcm_substream *substream)
28 struct snd_pcm_runtime *runtime = substream->runtime;
30 snd_pcm_hw_constraint_single(runtime, SNDRV_PCM_HW_PARAM_CHANNELS, 2);
35 static int odroid_card_fe_hw_params(struct snd_pcm_substream *substream,
36 struct snd_pcm_hw_params *params)
38 struct snd_soc_pcm_runtime *rtd = substream->private_data;
39 struct odroid_priv *priv = snd_soc_card_get_drvdata(rtd->card);
43 spin_lock_irqsave(&priv->lock, flags);
44 if (priv->be_active && priv->be_sample_rate != params_rate(params))
46 spin_unlock_irqrestore(&priv->lock, flags);
51 static const struct snd_soc_ops odroid_card_fe_ops = {
52 .startup = odroid_card_fe_startup,
53 .hw_params = odroid_card_fe_hw_params,
56 static int odroid_card_be_hw_params(struct snd_pcm_substream *substream,
57 struct snd_pcm_hw_params *params)
59 struct snd_soc_pcm_runtime *rtd = substream->private_data;
60 struct odroid_priv *priv = snd_soc_card_get_drvdata(rtd->card);
61 unsigned int pll_freq, rclk_freq, rfs;
65 switch (params_rate(params)) {
67 pll_freq = 196608001U;
72 pll_freq = 180633609U;
78 pll_freq = 196608001U;
85 ret = clk_set_rate(priv->clk_i2s_bus, pll_freq / 2 + 1);
90 * We add 2 to the rclk_freq value in order to avoid too low clock
91 * frequency values due to the EPLL output frequency not being exact
92 * multiple of the audio sampling rate.
94 rclk_freq = params_rate(params) * rfs + 2;
96 ret = clk_set_rate(priv->sclk_i2s, rclk_freq);
100 if (rtd->num_codecs > 1) {
101 struct snd_soc_dai *codec_dai = rtd->codec_dais[1];
103 ret = snd_soc_dai_set_sysclk(codec_dai, 0, rclk_freq,
109 spin_lock_irqsave(&priv->lock, flags);
110 priv->be_sample_rate = params_rate(params);
111 spin_unlock_irqrestore(&priv->lock, flags);
116 static int odroid_card_be_trigger(struct snd_pcm_substream *substream, int cmd)
118 struct snd_soc_pcm_runtime *rtd = substream->private_data;
119 struct odroid_priv *priv = snd_soc_card_get_drvdata(rtd->card);
122 spin_lock_irqsave(&priv->lock, flags);
125 case SNDRV_PCM_TRIGGER_START:
126 case SNDRV_PCM_TRIGGER_RESUME:
127 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
128 priv->be_active = true;
131 case SNDRV_PCM_TRIGGER_STOP:
132 case SNDRV_PCM_TRIGGER_SUSPEND:
133 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
134 priv->be_active = false;
138 spin_unlock_irqrestore(&priv->lock, flags);
143 static const struct snd_soc_ops odroid_card_be_ops = {
144 .hw_params = odroid_card_be_hw_params,
145 .trigger = odroid_card_be_trigger,
148 /* DAPM routes for backward compatibility with old DTS */
149 static const struct snd_soc_dapm_route odroid_dapm_routes[] = {
150 { "I2S Playback", NULL, "Mixer DAI TX" },
151 { "HiFi Playback", NULL, "Mixer DAI TX" },
154 static struct snd_soc_dai_link odroid_card_dais[] = {
156 /* Primary FE <-> BE link */
157 .codec_name = "snd-soc-dummy",
158 .codec_dai_name = "snd-soc-dummy-dai",
159 .ops = &odroid_card_fe_ops,
161 .stream_name = "Primary",
162 .platform_name = "3830000.i2s",
166 /* BE <-> CODECs link */
168 .cpu_name = "snd-soc-dummy",
169 .cpu_dai_name = "snd-soc-dummy-dai",
170 .platform_name = "snd-soc-dummy",
171 .ops = &odroid_card_be_ops,
174 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
175 SND_SOC_DAIFMT_CBS_CFS,
177 /* Secondary FE <-> BE link */
179 .codec_name = "snd-soc-dummy",
180 .codec_dai_name = "snd-soc-dummy-dai",
181 .ops = &odroid_card_fe_ops,
183 .stream_name = "Secondary",
184 .platform_name = "3830000.i2s-sec",
190 static int odroid_audio_probe(struct platform_device *pdev)
192 struct device *dev = &pdev->dev;
193 struct device_node *cpu_dai = NULL;
194 struct device_node *cpu, *codec;
195 struct odroid_priv *priv;
196 struct snd_soc_card *card;
197 struct snd_soc_dai_link *link, *codec_link;
198 int num_pcms, ret, i;
199 struct of_phandle_args args = {};
201 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
208 card->owner = THIS_MODULE;
209 card->fully_routed = true;
211 spin_lock_init(&priv->lock);
212 snd_soc_card_set_drvdata(card, priv);
214 ret = snd_soc_of_parse_card_name(card, "model");
218 if (of_property_read_bool(dev->of_node, "samsung,audio-widgets")) {
219 ret = snd_soc_of_parse_audio_simple_widgets(card,
220 "samsung,audio-widgets");
225 if (of_property_read_bool(dev->of_node, "samsung,audio-routing")) {
226 ret = snd_soc_of_parse_audio_routing(card,
227 "samsung,audio-routing");
232 card->dai_link = odroid_card_dais;
233 card->num_links = ARRAY_SIZE(odroid_card_dais);
235 cpu = of_get_child_by_name(dev->of_node, "cpu");
236 codec = of_get_child_by_name(dev->of_node, "codec");
237 link = card->dai_link;
238 codec_link = &card->dai_link[1];
241 * For backwards compatibility create the secondary CPU DAI link only
242 * if there are 2 CPU DAI entries in the cpu sound-dai property in DT.
243 * Also add required DAPM routes not available in old DTS.
245 num_pcms = of_count_phandle_with_args(cpu, "sound-dai",
248 card->dapm_routes = odroid_dapm_routes;
249 card->num_dapm_routes = ARRAY_SIZE(odroid_dapm_routes);
253 for (i = 0; i < num_pcms; i++, link += 2) {
254 ret = of_parse_phandle_with_args(cpu, "sound-dai",
255 "#sound-dai-cells", i, &args);
260 dev_err(dev, "sound-dai property parse error: %d\n", ret);
265 ret = snd_soc_get_dai_name(&args, &link->cpu_dai_name);
266 of_node_put(args.np);
272 cpu_dai = of_parse_phandle(cpu, "sound-dai", 0);
282 ret = snd_soc_of_get_dai_link_codecs(dev, codec, codec_link);
284 goto err_put_cpu_dai;
286 /* Set capture capability only for boards with the MAX98090 CODEC */
287 if (codec_link->num_codecs > 1) {
288 card->dai_link[0].dpcm_capture = 1;
289 card->dai_link[1].dpcm_capture = 1;
292 priv->sclk_i2s = of_clk_get_by_name(cpu_dai, "i2s_opclk1");
293 if (IS_ERR(priv->sclk_i2s)) {
294 ret = PTR_ERR(priv->sclk_i2s);
295 goto err_put_cpu_dai;
298 priv->clk_i2s_bus = of_clk_get_by_name(cpu_dai, "iis");
299 if (IS_ERR(priv->clk_i2s_bus)) {
300 ret = PTR_ERR(priv->clk_i2s_bus);
303 of_node_put(cpu_dai);
305 ret = devm_snd_soc_register_card(dev, card);
307 dev_err(dev, "snd_soc_register_card() failed: %d\n", ret);
308 goto err_put_clk_i2s;
314 clk_put(priv->clk_i2s_bus);
316 clk_put(priv->sclk_i2s);
318 of_node_put(cpu_dai);
319 snd_soc_of_put_dai_link_codecs(codec_link);
323 static int odroid_audio_remove(struct platform_device *pdev)
325 struct odroid_priv *priv = platform_get_drvdata(pdev);
327 snd_soc_of_put_dai_link_codecs(&priv->card.dai_link[1]);
328 clk_put(priv->sclk_i2s);
329 clk_put(priv->clk_i2s_bus);
334 static const struct of_device_id odroid_audio_of_match[] = {
335 { .compatible = "hardkernel,odroid-xu3-audio" },
336 { .compatible = "hardkernel,odroid-xu4-audio" },
337 { .compatible = "samsung,odroid-xu3-audio" },
338 { .compatible = "samsung,odroid-xu4-audio" },
341 MODULE_DEVICE_TABLE(of, odroid_audio_of_match);
343 static struct platform_driver odroid_audio_driver = {
345 .name = "odroid-audio",
346 .of_match_table = odroid_audio_of_match,
347 .pm = &snd_soc_pm_ops,
349 .probe = odroid_audio_probe,
350 .remove = odroid_audio_remove,
352 module_platform_driver(odroid_audio_driver);
354 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
355 MODULE_DESCRIPTION("Odroid XU3/XU4 audio support");
356 MODULE_LICENSE("GPL v2");