2 * Copyright (C) 2017 Samsung Electronics Co., Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
10 #include <linux/clk-provider.h>
12 #include <linux/of_device.h>
13 #include <linux/module.h>
14 #include <sound/soc.h>
15 #include <sound/pcm_params.h>
20 struct snd_soc_card card;
21 struct clk *clk_i2s_bus;
24 /* Spinlock protecting fields below */
26 unsigned int be_sample_rate;
30 static int odroid_card_fe_startup(struct snd_pcm_substream *substream)
32 struct snd_pcm_runtime *runtime = substream->runtime;
34 snd_pcm_hw_constraint_single(runtime, SNDRV_PCM_HW_PARAM_CHANNELS, 2);
39 static int odroid_card_fe_hw_params(struct snd_pcm_substream *substream,
40 struct snd_pcm_hw_params *params)
42 struct snd_soc_pcm_runtime *rtd = substream->private_data;
43 struct odroid_priv *priv = snd_soc_card_get_drvdata(rtd->card);
47 spin_lock_irqsave(&priv->lock, flags);
48 if (priv->be_active && priv->be_sample_rate != params_rate(params))
50 spin_unlock_irqrestore(&priv->lock, flags);
55 static const struct snd_soc_ops odroid_card_fe_ops = {
56 .startup = odroid_card_fe_startup,
57 .hw_params = odroid_card_fe_hw_params,
60 static int odroid_card_be_hw_params(struct snd_pcm_substream *substream,
61 struct snd_pcm_hw_params *params)
63 struct snd_soc_pcm_runtime *rtd = substream->private_data;
64 struct odroid_priv *priv = snd_soc_card_get_drvdata(rtd->card);
65 unsigned int pll_freq, rclk_freq, rfs;
69 switch (params_rate(params)) {
71 pll_freq = 196608001U;
76 pll_freq = 180633609U;
82 pll_freq = 196608001U;
89 ret = clk_set_rate(priv->clk_i2s_bus, pll_freq / 2 + 1);
94 * We add 2 to the rclk_freq value in order to avoid too low clock
95 * frequency values due to the EPLL output frequency not being exact
96 * multiple of the audio sampling rate.
98 rclk_freq = params_rate(params) * rfs + 2;
100 ret = clk_set_rate(priv->sclk_i2s, rclk_freq);
104 if (rtd->num_codecs > 1) {
105 struct snd_soc_dai *codec_dai = rtd->codec_dais[1];
107 ret = snd_soc_dai_set_sysclk(codec_dai, 0, rclk_freq,
113 spin_lock_irqsave(&priv->lock, flags);
114 priv->be_sample_rate = params_rate(params);
115 spin_unlock_irqrestore(&priv->lock, flags);
120 static int odroid_card_be_trigger(struct snd_pcm_substream *substream, int cmd)
122 struct snd_soc_pcm_runtime *rtd = substream->private_data;
123 struct odroid_priv *priv = snd_soc_card_get_drvdata(rtd->card);
126 spin_lock_irqsave(&priv->lock, flags);
129 case SNDRV_PCM_TRIGGER_START:
130 case SNDRV_PCM_TRIGGER_RESUME:
131 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
132 priv->be_active = true;
135 case SNDRV_PCM_TRIGGER_STOP:
136 case SNDRV_PCM_TRIGGER_SUSPEND:
137 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
138 priv->be_active = false;
142 spin_unlock_irqrestore(&priv->lock, flags);
147 static const struct snd_soc_ops odroid_card_be_ops = {
148 .hw_params = odroid_card_be_hw_params,
149 .trigger = odroid_card_be_trigger,
152 /* DAPM routes for backward compatibility with old DTS */
153 static const struct snd_soc_dapm_route odroid_dapm_routes[] = {
154 { "I2S Playback", NULL, "Mixer DAI TX" },
155 { "HiFi Playback", NULL, "Mixer DAI TX" },
158 static struct snd_soc_dai_link odroid_card_dais[] = {
160 /* Primary FE <-> BE link */
161 .codec_name = "snd-soc-dummy",
162 .codec_dai_name = "snd-soc-dummy-dai",
163 .ops = &odroid_card_fe_ops,
165 .stream_name = "Primary",
166 .platform_name = "3830000.i2s",
170 /* BE <-> CODECs link */
172 .cpu_name = "snd-soc-dummy",
173 .cpu_dai_name = "snd-soc-dummy-dai",
174 .platform_name = "snd-soc-dummy",
175 .ops = &odroid_card_be_ops,
178 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
179 SND_SOC_DAIFMT_CBS_CFS,
181 /* Secondary FE <-> BE link */
183 .codec_name = "snd-soc-dummy",
184 .codec_dai_name = "snd-soc-dummy-dai",
185 .ops = &odroid_card_fe_ops,
187 .stream_name = "Secondary",
188 .platform_name = "3830000.i2s-sec",
194 static int odroid_audio_probe(struct platform_device *pdev)
196 struct device *dev = &pdev->dev;
197 struct device_node *cpu_dai = NULL;
198 struct device_node *cpu, *codec;
199 struct odroid_priv *priv;
200 struct snd_soc_card *card;
201 struct snd_soc_dai_link *link, *codec_link;
202 int num_pcms, ret, i;
203 struct of_phandle_args args = {};
205 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
212 card->owner = THIS_MODULE;
213 card->fully_routed = true;
215 spin_lock_init(&priv->lock);
216 snd_soc_card_set_drvdata(card, priv);
218 ret = snd_soc_of_parse_card_name(card, "model");
222 if (of_property_read_bool(dev->of_node, "samsung,audio-widgets")) {
223 ret = snd_soc_of_parse_audio_simple_widgets(card,
224 "samsung,audio-widgets");
229 if (of_property_read_bool(dev->of_node, "samsung,audio-routing")) {
230 ret = snd_soc_of_parse_audio_routing(card,
231 "samsung,audio-routing");
236 card->dai_link = odroid_card_dais;
237 card->num_links = ARRAY_SIZE(odroid_card_dais);
239 cpu = of_get_child_by_name(dev->of_node, "cpu");
240 codec = of_get_child_by_name(dev->of_node, "codec");
241 link = card->dai_link;
242 codec_link = &card->dai_link[1];
245 * For backwards compatibility create the secondary CPU DAI link only
246 * if there are 2 CPU DAI entries in the cpu sound-dai property in DT.
247 * Also add required DAPM routes not available in old DTS.
249 num_pcms = of_count_phandle_with_args(cpu, "sound-dai",
252 card->dapm_routes = odroid_dapm_routes;
253 card->num_dapm_routes = ARRAY_SIZE(odroid_dapm_routes);
257 for (i = 0; i < num_pcms; i++, link += 2) {
258 ret = of_parse_phandle_with_args(cpu, "sound-dai",
259 "#sound-dai-cells", i, &args);
264 dev_err(dev, "sound-dai property parse error: %d\n", ret);
269 ret = snd_soc_get_dai_name(&args, &link->cpu_dai_name);
270 of_node_put(args.np);
276 cpu_dai = of_parse_phandle(cpu, "sound-dai", 0);
286 ret = snd_soc_of_get_dai_link_codecs(dev, codec, codec_link);
288 goto err_put_cpu_dai;
290 /* Set capture capability only for boards with the MAX98090 CODEC */
291 if (codec_link->num_codecs > 1) {
292 card->dai_link[0].dpcm_capture = 1;
293 card->dai_link[1].dpcm_capture = 1;
296 priv->sclk_i2s = of_clk_get_by_name(cpu_dai, "i2s_opclk1");
297 if (IS_ERR(priv->sclk_i2s)) {
298 ret = PTR_ERR(priv->sclk_i2s);
299 goto err_put_cpu_dai;
302 priv->clk_i2s_bus = of_clk_get_by_name(cpu_dai, "iis");
303 if (IS_ERR(priv->clk_i2s_bus)) {
304 ret = PTR_ERR(priv->clk_i2s_bus);
307 of_node_put(cpu_dai);
309 ret = devm_snd_soc_register_card(dev, card);
311 dev_err(dev, "snd_soc_register_card() failed: %d\n", ret);
312 goto err_put_clk_i2s;
318 clk_put(priv->clk_i2s_bus);
320 clk_put(priv->sclk_i2s);
322 of_node_put(cpu_dai);
323 snd_soc_of_put_dai_link_codecs(codec_link);
327 static int odroid_audio_remove(struct platform_device *pdev)
329 struct odroid_priv *priv = platform_get_drvdata(pdev);
331 snd_soc_of_put_dai_link_codecs(&priv->card.dai_link[1]);
332 clk_put(priv->sclk_i2s);
333 clk_put(priv->clk_i2s_bus);
338 static const struct of_device_id odroid_audio_of_match[] = {
339 { .compatible = "hardkernel,odroid-xu3-audio" },
340 { .compatible = "hardkernel,odroid-xu4-audio" },
341 { .compatible = "samsung,odroid-xu3-audio" },
342 { .compatible = "samsung,odroid-xu4-audio" },
345 MODULE_DEVICE_TABLE(of, odroid_audio_of_match);
347 static struct platform_driver odroid_audio_driver = {
349 .name = "odroid-audio",
350 .of_match_table = odroid_audio_of_match,
351 .pm = &snd_soc_pm_ops,
353 .probe = odroid_audio_probe,
354 .remove = odroid_audio_remove,
356 module_platform_driver(odroid_audio_driver);
358 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
359 MODULE_DESCRIPTION("Odroid XU3/XU4 audio support");
360 MODULE_LICENSE("GPL v2");