1 // SPDX-License-Identifier: GPL-2.0
3 // ASoC simple sound card support
5 // Copyright (C) 2012 Renesas Solutions Corp.
6 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
9 #include <linux/device.h>
10 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/string.h>
14 #include <sound/simple_card.h>
15 #include <sound/soc-dai.h>
16 #include <sound/soc.h>
18 struct simple_card_data {
19 struct snd_soc_card snd_card;
20 struct simple_dai_props {
21 struct asoc_simple_dai cpu_dai;
22 struct asoc_simple_dai codec_dai;
26 struct asoc_simple_jack hp_jack;
27 struct asoc_simple_jack mic_jack;
28 struct snd_soc_dai_link *dai_link;
31 #define simple_priv_to_card(priv) (&(priv)->snd_card)
32 #define simple_priv_to_props(priv, i) ((priv)->dai_props + (i))
33 #define simple_priv_to_dev(priv) (simple_priv_to_card(priv)->dev)
34 #define simple_priv_to_link(priv, i) (simple_priv_to_card(priv)->dai_link + (i))
36 #define DAI "sound-dai"
37 #define CELL "#sound-dai-cells"
38 #define PREFIX "simple-audio-card,"
40 static int asoc_simple_card_startup(struct snd_pcm_substream *substream)
42 struct snd_soc_pcm_runtime *rtd = substream->private_data;
43 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
44 struct simple_dai_props *dai_props =
45 simple_priv_to_props(priv, rtd->num);
48 ret = asoc_simple_card_clk_enable(&dai_props->cpu_dai);
52 ret = asoc_simple_card_clk_enable(&dai_props->codec_dai);
54 asoc_simple_card_clk_disable(&dai_props->cpu_dai);
59 static void asoc_simple_card_shutdown(struct snd_pcm_substream *substream)
61 struct snd_soc_pcm_runtime *rtd = substream->private_data;
62 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
63 struct simple_dai_props *dai_props =
64 simple_priv_to_props(priv, rtd->num);
66 asoc_simple_card_clk_disable(&dai_props->cpu_dai);
68 asoc_simple_card_clk_disable(&dai_props->codec_dai);
71 static int asoc_simple_set_clk_rate(struct asoc_simple_dai *simple_dai,
77 if (clk_get_rate(simple_dai->clk) == rate)
80 return clk_set_rate(simple_dai->clk, rate);
83 static int asoc_simple_card_hw_params(struct snd_pcm_substream *substream,
84 struct snd_pcm_hw_params *params)
86 struct snd_soc_pcm_runtime *rtd = substream->private_data;
87 struct snd_soc_dai *codec_dai = rtd->codec_dai;
88 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
89 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
90 struct simple_dai_props *dai_props =
91 simple_priv_to_props(priv, rtd->num);
92 unsigned int mclk, mclk_fs = 0;
96 mclk_fs = priv->mclk_fs;
97 else if (dai_props->mclk_fs)
98 mclk_fs = dai_props->mclk_fs;
101 mclk = params_rate(params) * mclk_fs;
103 ret = asoc_simple_set_clk_rate(&dai_props->codec_dai, mclk);
107 ret = asoc_simple_set_clk_rate(&dai_props->cpu_dai, mclk);
111 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
113 if (ret && ret != -ENOTSUPP)
116 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
118 if (ret && ret != -ENOTSUPP)
126 static const struct snd_soc_ops asoc_simple_card_ops = {
127 .startup = asoc_simple_card_startup,
128 .shutdown = asoc_simple_card_shutdown,
129 .hw_params = asoc_simple_card_hw_params,
132 static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd)
134 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
135 struct snd_soc_dai *codec = rtd->codec_dai;
136 struct snd_soc_dai *cpu = rtd->cpu_dai;
137 struct simple_dai_props *dai_props =
138 simple_priv_to_props(priv, rtd->num);
141 ret = asoc_simple_card_init_dai(codec, &dai_props->codec_dai);
145 ret = asoc_simple_card_init_dai(cpu, &dai_props->cpu_dai);
152 static int asoc_simple_card_dai_link_of(struct device_node *node,
153 struct simple_card_data *priv,
155 bool is_top_level_node)
157 struct device *dev = simple_priv_to_dev(priv);
158 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, idx);
159 struct simple_dai_props *dai_props = simple_priv_to_props(priv, idx);
160 struct asoc_simple_dai *cpu_dai = &dai_props->cpu_dai;
161 struct asoc_simple_dai *codec_dai = &dai_props->codec_dai;
162 struct device_node *cpu = NULL;
163 struct device_node *plat = NULL;
164 struct device_node *codec = NULL;
169 /* For single DAI link & old style of DT node */
170 if (is_top_level_node)
173 snprintf(prop, sizeof(prop), "%scpu", prefix);
174 cpu = of_get_child_by_name(node, prop);
178 dev_err(dev, "%s: Can't find %s DT node\n", __func__, prop);
179 goto dai_link_of_err;
182 snprintf(prop, sizeof(prop), "%splat", prefix);
183 plat = of_get_child_by_name(node, prop);
185 snprintf(prop, sizeof(prop), "%scodec", prefix);
186 codec = of_get_child_by_name(node, prop);
190 dev_err(dev, "%s: Can't find %s DT node\n", __func__, prop);
191 goto dai_link_of_err;
194 ret = asoc_simple_card_parse_daifmt(dev, node, codec,
195 prefix, &dai_link->dai_fmt);
197 goto dai_link_of_err;
199 of_property_read_u32(node, "mclk-fs", &dai_props->mclk_fs);
201 ret = asoc_simple_card_parse_cpu(cpu, dai_link,
202 DAI, CELL, &single_cpu);
204 goto dai_link_of_err;
206 ret = asoc_simple_card_parse_codec(codec, dai_link, DAI, CELL);
208 goto dai_link_of_err;
210 ret = asoc_simple_card_parse_platform(plat, dai_link, DAI, CELL);
212 goto dai_link_of_err;
214 ret = asoc_simple_card_of_parse_tdm(cpu, cpu_dai);
216 goto dai_link_of_err;
218 ret = asoc_simple_card_of_parse_tdm(codec, codec_dai);
220 goto dai_link_of_err;
222 ret = asoc_simple_card_parse_clk_cpu(dev, cpu, dai_link, cpu_dai);
224 goto dai_link_of_err;
226 ret = asoc_simple_card_parse_clk_codec(dev, codec, dai_link, codec_dai);
228 goto dai_link_of_err;
230 ret = asoc_simple_card_canonicalize_dailink(dai_link);
232 goto dai_link_of_err;
234 ret = asoc_simple_card_set_dailink_name(dev, dai_link,
236 dai_link->cpu_dai_name,
237 dai_link->codec_dai_name);
239 goto dai_link_of_err;
241 dai_link->ops = &asoc_simple_card_ops;
242 dai_link->init = asoc_simple_card_dai_init;
244 asoc_simple_card_canonicalize_cpu(dai_link, single_cpu);
253 static int asoc_simple_card_parse_aux_devs(struct device_node *node,
254 struct simple_card_data *priv)
256 struct device *dev = simple_priv_to_dev(priv);
257 struct device_node *aux_node;
258 struct snd_soc_card *card = simple_priv_to_card(priv);
261 if (!of_find_property(node, PREFIX "aux-devs", &len))
262 return 0; /* Ok to have no aux-devs */
264 n = len / sizeof(__be32);
268 card->aux_dev = devm_kcalloc(dev,
269 n, sizeof(*card->aux_dev), GFP_KERNEL);
273 for (i = 0; i < n; i++) {
274 aux_node = of_parse_phandle(node, PREFIX "aux-devs", i);
277 card->aux_dev[i].codec_of_node = aux_node;
280 card->num_aux_devs = n;
284 static int asoc_simple_card_parse_of(struct simple_card_data *priv)
286 struct device *dev = simple_priv_to_dev(priv);
287 struct snd_soc_card *card = simple_priv_to_card(priv);
288 struct device_node *dai_link;
289 struct device_node *node = dev->of_node;
295 dai_link = of_get_child_by_name(node, PREFIX "dai-link");
297 ret = asoc_simple_card_of_parse_widgets(card, PREFIX);
301 ret = asoc_simple_card_of_parse_routing(card, PREFIX, 1);
305 /* Factor to mclk, used in hw_params() */
306 of_property_read_u32(node, PREFIX "mclk-fs", &priv->mclk_fs);
308 /* Single/Muti DAI link(s) & New style of DT node */
310 struct device_node *np = NULL;
313 for_each_child_of_node(node, np) {
314 dev_dbg(dev, "\tlink %d:\n", i);
315 ret = asoc_simple_card_dai_link_of(np, priv,
324 /* For single DAI link & old style of DT node */
325 ret = asoc_simple_card_dai_link_of(node, priv, 0, true);
330 ret = asoc_simple_card_parse_card_name(card, PREFIX);
334 ret = asoc_simple_card_parse_aux_devs(node, priv);
337 of_node_put(dai_link);
342 static int asoc_simple_soc_card_probe(struct snd_soc_card *card)
344 struct simple_card_data *priv = snd_soc_card_get_drvdata(card);
347 ret = asoc_simple_card_init_hp(card, &priv->hp_jack, PREFIX);
351 ret = asoc_simple_card_init_mic(card, &priv->mic_jack, PREFIX);
358 static int asoc_simple_card_probe(struct platform_device *pdev)
360 struct simple_card_data *priv;
361 struct snd_soc_dai_link *dai_link;
362 struct simple_dai_props *dai_props;
363 struct device *dev = &pdev->dev;
364 struct device_node *np = dev->of_node;
365 struct snd_soc_card *card;
368 /* Get the number of DAI links */
369 if (np && of_get_child_by_name(np, PREFIX "dai-link"))
370 num = of_get_child_count(np);
374 /* Allocate the private data and the DAI link array */
375 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
379 dai_props = devm_kcalloc(dev, num, sizeof(*dai_props), GFP_KERNEL);
380 dai_link = devm_kcalloc(dev, num, sizeof(*dai_link), GFP_KERNEL);
381 if (!dai_props || !dai_link)
384 priv->dai_props = dai_props;
385 priv->dai_link = dai_link;
387 /* Init snd_soc_card */
388 card = simple_priv_to_card(priv);
389 card->owner = THIS_MODULE;
391 card->dai_link = priv->dai_link;
392 card->num_links = num;
393 card->probe = asoc_simple_soc_card_probe;
395 if (np && of_device_is_available(np)) {
397 ret = asoc_simple_card_parse_of(priv);
399 if (ret != -EPROBE_DEFER)
400 dev_err(dev, "parse error %d\n", ret);
405 struct asoc_simple_card_info *cinfo;
407 cinfo = dev->platform_data;
409 dev_err(dev, "no info for asoc-simple-card\n");
414 !cinfo->codec_dai.name ||
417 !cinfo->cpu_dai.name) {
418 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
422 card->name = (cinfo->card) ? cinfo->card : cinfo->name;
423 dai_link->name = cinfo->name;
424 dai_link->stream_name = cinfo->name;
425 dai_link->platform_name = cinfo->platform;
426 dai_link->codec_name = cinfo->codec;
427 dai_link->cpu_dai_name = cinfo->cpu_dai.name;
428 dai_link->codec_dai_name = cinfo->codec_dai.name;
429 dai_link->dai_fmt = cinfo->daifmt;
430 dai_link->init = asoc_simple_card_dai_init;
431 memcpy(&priv->dai_props->cpu_dai, &cinfo->cpu_dai,
432 sizeof(priv->dai_props->cpu_dai));
433 memcpy(&priv->dai_props->codec_dai, &cinfo->codec_dai,
434 sizeof(priv->dai_props->codec_dai));
437 snd_soc_card_set_drvdata(card, priv);
439 ret = devm_snd_soc_register_card(dev, card);
445 asoc_simple_card_clean_reference(card);
450 static int asoc_simple_card_remove(struct platform_device *pdev)
452 struct snd_soc_card *card = platform_get_drvdata(pdev);
454 return asoc_simple_card_clean_reference(card);
457 static const struct of_device_id asoc_simple_of_match[] = {
458 { .compatible = "simple-audio-card", },
461 MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
463 static struct platform_driver asoc_simple_card = {
465 .name = "asoc-simple-card",
466 .pm = &snd_soc_pm_ops,
467 .of_match_table = asoc_simple_of_match,
469 .probe = asoc_simple_card_probe,
470 .remove = asoc_simple_card_remove,
473 module_platform_driver(asoc_simple_card);
475 MODULE_ALIAS("platform:asoc-simple-card");
476 MODULE_LICENSE("GPL v2");
477 MODULE_DESCRIPTION("ASoC Simple Sound Card");
478 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");