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/of_device.h>
13 #include <linux/platform_device.h>
14 #include <linux/string.h>
15 #include <sound/simple_card.h>
16 #include <sound/soc-dai.h>
17 #include <sound/soc.h>
19 #define DPCM_SELECTABLE 1
21 #define DAI "sound-dai"
22 #define CELL "#sound-dai-cells"
23 #define PREFIX "simple-audio-card,"
25 static const struct snd_soc_ops simple_ops = {
26 .startup = asoc_simple_startup,
27 .shutdown = asoc_simple_shutdown,
28 .hw_params = asoc_simple_hw_params,
31 static int asoc_simple_parse_dai(struct device_node *node,
32 struct snd_soc_dai_link_component *dlc,
35 struct of_phandle_args args;
42 * Get node via "sound-dai = <&phandle port>"
43 * it will be used as xxx_of_node on soc_bind_dai_link()
45 ret = of_parse_phandle_with_args(node, DAI, CELL, 0, &args);
52 * Here, dlc->dai_name is pointer to CPU/Codec DAI name.
53 * If user unbinded CPU or Codec driver, but not for Sound Card,
54 * dlc->dai_name is keeping unbinded CPU or Codec
57 * If user re-bind CPU or Codec driver again, ALSA SoC will try
58 * to rebind Card via snd_soc_try_rebind_card(), but because of
59 * above reason, it might can't bind Sound Card.
60 * Because Sound Card is pointing to released dai_name pointer.
62 * To avoid this rebind Card issue,
63 * 1) It needs to alloc memory to keep dai_name eventhough
64 * CPU or Codec driver was unbinded, or
65 * 2) user need to rebind Sound Card everytime
66 * if he unbinded CPU or Codec.
68 ret = snd_soc_of_get_dai_name(node, &dlc->dai_name);
72 dlc->of_node = args.np;
75 *is_single_link = !args.args_count;
80 static void simple_parse_convert(struct device *dev,
81 struct device_node *np,
82 struct asoc_simple_data *adata)
84 struct device_node *top = dev->of_node;
85 struct device_node *node = of_get_parent(np);
87 asoc_simple_parse_convert(dev, top, PREFIX, adata);
88 asoc_simple_parse_convert(dev, node, PREFIX, adata);
89 asoc_simple_parse_convert(dev, node, NULL, adata);
90 asoc_simple_parse_convert(dev, np, NULL, adata);
95 static void simple_parse_mclk_fs(struct device_node *top,
96 struct device_node *cpu,
97 struct device_node *codec,
98 struct simple_dai_props *props,
101 struct device_node *node = of_get_parent(cpu);
104 snprintf(prop, sizeof(prop), "%smclk-fs", PREFIX);
105 of_property_read_u32(top, prop, &props->mclk_fs);
107 snprintf(prop, sizeof(prop), "%smclk-fs", prefix);
108 of_property_read_u32(node, prop, &props->mclk_fs);
109 of_property_read_u32(cpu, prop, &props->mclk_fs);
110 of_property_read_u32(codec, prop, &props->mclk_fs);
115 static int simple_dai_link_of_dpcm(struct asoc_simple_priv *priv,
116 struct device_node *np,
117 struct device_node *codec,
118 struct link_info *li,
121 struct device *dev = simple_priv_to_dev(priv);
122 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
123 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
124 struct asoc_simple_dai *dai;
125 struct snd_soc_dai_link_component *cpus = dai_link->cpus;
126 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
127 struct device_node *top = dev->of_node;
128 struct device_node *node = of_get_parent(np);
132 dev_dbg(dev, "link_of DPCM (%pOF)\n", np);
136 /* For single DAI link & old style of DT node */
141 int is_single_links = 0;
146 dai_link->dynamic = 1;
147 dai_link->dpcm_merged_format = 1;
149 dai = dai_props->cpu_dai;
151 ret = asoc_simple_parse_cpu(np, dai_link, &is_single_links);
155 ret = asoc_simple_parse_clk_cpu(dev, np, dai_link, dai);
159 ret = asoc_simple_set_dailink_name(dev, dai_link,
165 asoc_simple_canonicalize_cpu(dai_link, is_single_links);
166 asoc_simple_canonicalize_platform(dai_link);
168 struct snd_soc_codec_conf *cconf;
173 dai_link->no_pcm = 1;
174 dai_link->be_hw_params_fixup = asoc_simple_be_hw_params_fixup;
176 dai = dai_props->codec_dai;
177 cconf = dai_props->codec_conf;
179 ret = asoc_simple_parse_codec(np, dai_link);
183 ret = asoc_simple_parse_clk_codec(dev, np, dai_link, dai);
187 ret = asoc_simple_set_dailink_name(dev, dai_link,
193 /* check "prefix" from top node */
194 snd_soc_of_parse_node_prefix(top, cconf, codecs->of_node,
196 snd_soc_of_parse_node_prefix(node, cconf, codecs->of_node,
198 snd_soc_of_parse_node_prefix(np, cconf, codecs->of_node,
202 simple_parse_convert(dev, np, &dai_props->adata);
203 simple_parse_mclk_fs(top, np, codec, dai_props, prefix);
205 ret = asoc_simple_parse_tdm(np, dai);
209 ret = asoc_simple_parse_daifmt(dev, node, codec,
210 prefix, &dai_link->dai_fmt);
214 snd_soc_dai_link_set_capabilities(dai_link);
216 dai_link->ops = &simple_ops;
217 dai_link->init = asoc_simple_dai_init;
224 static int simple_dai_link_of(struct asoc_simple_priv *priv,
225 struct device_node *np,
226 struct device_node *codec,
227 struct link_info *li,
230 struct device *dev = simple_priv_to_dev(priv);
231 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
232 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
233 struct asoc_simple_dai *cpu_dai = dai_props->cpu_dai;
234 struct asoc_simple_dai *codec_dai = dai_props->codec_dai;
235 struct device_node *top = dev->of_node;
236 struct device_node *cpu = NULL;
237 struct device_node *node = NULL;
238 struct device_node *plat = NULL;
241 int ret, single_cpu = 0;
244 node = of_get_parent(np);
247 dev_dbg(dev, "link_of (%pOF)\n", node);
249 /* For single DAI link & old style of DT node */
253 snprintf(prop, sizeof(prop), "%splat", prefix);
254 plat = of_get_child_by_name(node, prop);
256 ret = asoc_simple_parse_daifmt(dev, node, codec,
257 prefix, &dai_link->dai_fmt);
259 goto dai_link_of_err;
261 simple_parse_mclk_fs(top, cpu, codec, dai_props, prefix);
263 ret = asoc_simple_parse_cpu(cpu, dai_link, &single_cpu);
265 goto dai_link_of_err;
267 ret = asoc_simple_parse_codec(codec, dai_link);
269 goto dai_link_of_err;
271 ret = asoc_simple_parse_platform(plat, dai_link);
273 goto dai_link_of_err;
275 ret = asoc_simple_parse_tdm(cpu, cpu_dai);
277 goto dai_link_of_err;
279 ret = asoc_simple_parse_tdm(codec, codec_dai);
281 goto dai_link_of_err;
283 ret = asoc_simple_parse_clk_cpu(dev, cpu, dai_link, cpu_dai);
285 goto dai_link_of_err;
287 ret = asoc_simple_parse_clk_codec(dev, codec, dai_link, codec_dai);
289 goto dai_link_of_err;
291 ret = asoc_simple_set_dailink_name(dev, dai_link,
293 dai_link->cpus->dai_name,
294 dai_link->codecs->dai_name);
296 goto dai_link_of_err;
298 dai_link->ops = &simple_ops;
299 dai_link->init = asoc_simple_dai_init;
301 asoc_simple_canonicalize_cpu(dai_link, single_cpu);
302 asoc_simple_canonicalize_platform(dai_link);
311 static int __simple_for_each_link(struct asoc_simple_priv *priv,
312 struct link_info *li,
313 int (*func_noml)(struct asoc_simple_priv *priv,
314 struct device_node *np,
315 struct device_node *codec,
316 struct link_info *li, bool is_top),
317 int (*func_dpcm)(struct asoc_simple_priv *priv,
318 struct device_node *np,
319 struct device_node *codec,
320 struct link_info *li, bool is_top))
322 struct device *dev = simple_priv_to_dev(priv);
323 struct device_node *top = dev->of_node;
324 struct device_node *node;
325 uintptr_t dpcm_selectable = (uintptr_t)of_device_get_match_data(dev);
329 /* Check if it has dai-link */
330 node = of_get_child_by_name(top, PREFIX "dai-link");
332 node = of_node_get(top);
336 /* loop for all dai-link */
338 struct asoc_simple_data adata;
339 struct device_node *codec;
340 struct device_node *plat;
341 struct device_node *np;
342 int num = of_get_child_count(node);
345 codec = of_get_child_by_name(node, is_top ?
346 PREFIX "codec" : "codec");
352 plat = of_get_child_by_name(node, is_top ?
353 PREFIX "plat" : "plat");
355 /* get convert-xxx property */
356 memset(&adata, 0, sizeof(adata));
357 for_each_child_of_node(node, np)
358 simple_parse_convert(dev, np, &adata);
360 /* loop for all CPU/Codec node */
361 for_each_child_of_node(node, np) {
366 * if it has many CPUs,
367 * or has convert-xxx property
369 if (dpcm_selectable &&
371 adata.convert_rate || adata.convert_channels)) {
374 * |1(CPU)|0(Codec) li->cpu
378 if (li->cpu != (np == codec))
379 ret = func_dpcm(priv, np, codec, li, is_top);
380 /* else normal sound */
384 * |1(CPU)|0(Codec) li->cpu
386 * Codec |return|return
388 if (li->cpu && (np != codec))
389 ret = func_noml(priv, np, codec, li, is_top);
400 node = of_get_next_child(top, node);
401 } while (!is_top && node);
408 static int simple_for_each_link(struct asoc_simple_priv *priv,
409 struct link_info *li,
410 int (*func_noml)(struct asoc_simple_priv *priv,
411 struct device_node *np,
412 struct device_node *codec,
413 struct link_info *li, bool is_top),
414 int (*func_dpcm)(struct asoc_simple_priv *priv,
415 struct device_node *np,
416 struct device_node *codec,
417 struct link_info *li, bool is_top))
421 * Detect all CPU first, and Detect all Codec 2nd.
423 * In Normal sound case, all DAIs are detected
426 * In DPCM sound case,
427 * all CPUs are detected as "CPU-dummy", and
428 * all Codecs are detected as "dummy-Codec".
429 * To avoid random sub-device numbering,
430 * detect "dummy-Codec" in last;
432 for (li->cpu = 1; li->cpu >= 0; li->cpu--) {
433 ret = __simple_for_each_link(priv, li, func_noml, func_dpcm);
441 static int simple_parse_of(struct asoc_simple_priv *priv)
443 struct device *dev = simple_priv_to_dev(priv);
444 struct device_node *top = dev->of_node;
445 struct snd_soc_card *card = simple_priv_to_card(priv);
452 ret = asoc_simple_parse_widgets(card, PREFIX);
456 ret = asoc_simple_parse_routing(card, PREFIX);
460 ret = asoc_simple_parse_pin_switches(card, PREFIX);
464 /* Single/Muti DAI link(s) & New style of DT node */
465 memset(&li, 0, sizeof(li));
466 ret = simple_for_each_link(priv, &li,
468 simple_dai_link_of_dpcm);
472 ret = asoc_simple_parse_card_name(card, PREFIX);
476 ret = snd_soc_of_parse_aux_devs(card, PREFIX "aux-devs");
481 static int simple_count_noml(struct asoc_simple_priv *priv,
482 struct device_node *np,
483 struct device_node *codec,
484 struct link_info *li, bool is_top)
486 if (li->link >= SNDRV_MINOR_DEVICES) {
487 struct device *dev = simple_priv_to_dev(priv);
489 dev_err(dev, "too many links\n");
493 li->num[li->link].cpus = 1;
494 li->num[li->link].codecs = 1;
495 li->num[li->link].platforms = 1;
503 static int simple_count_dpcm(struct asoc_simple_priv *priv,
504 struct device_node *np,
505 struct device_node *codec,
506 struct link_info *li, bool is_top)
508 if (li->link >= SNDRV_MINOR_DEVICES) {
509 struct device *dev = simple_priv_to_dev(priv);
511 dev_err(dev, "too many links\n");
516 li->num[li->link].cpus = 1;
517 li->num[li->link].platforms = 1;
519 li->link++; /* CPU-dummy */
522 li->num[li->link].codecs = 1;
524 li->link++; /* dummy-Codec */
532 static void simple_get_dais_count(struct asoc_simple_priv *priv,
533 struct link_info *li)
535 struct device *dev = simple_priv_to_dev(priv);
536 struct device_node *top = dev->of_node;
539 * link_num : number of links.
540 * CPU-Codec / CPU-dummy / dummy-Codec
541 * dais_num : number of DAIs
542 * ccnf_num : number of codec_conf
543 * same number for "dummy-Codec"
546 * CPU0 --- Codec0 link : 5
547 * CPU1 --- Codec1 dais : 7
551 * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec
552 * => 7 DAIs = 4xCPU + 3xCodec
553 * => 1 ccnf = 1xdummy-Codec
556 * CPU0 --- Codec0 link : 5
557 * CPU1 --- Codec1 dais : 6
561 * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec
562 * => 6 DAIs = 4xCPU + 2xCodec
563 * => 1 ccnf = 1xdummy-Codec
566 * CPU0 --- Codec0 link : 6
568 * CPU2 --- Codec1 ccnf : 2
571 * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec
572 * => 6 DAIs = 4xCPU + 2xCodec
573 * => 2 ccnf = 2xdummy-Codec
576 * CPU0 --- Codec0 (convert-rate) link : 3
577 * CPU1 --- Codec1 dais : 4
580 * => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec
581 * => 4 DAIs = 2xCPU + 2xCodec
582 * => 1 ccnf = 1xdummy-Codec
586 li->num[0].codecs = 1;
587 li->num[0].platforms = 1;
595 simple_for_each_link(priv, li,
599 dev_dbg(dev, "link %d, dais %d, ccnf %d\n",
600 li->link, li->dais, li->conf);
603 static int simple_soc_probe(struct snd_soc_card *card)
605 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(card);
608 ret = asoc_simple_init_hp(card, &priv->hp_jack, PREFIX);
612 ret = asoc_simple_init_mic(card, &priv->mic_jack, PREFIX);
619 static int asoc_simple_probe(struct platform_device *pdev)
621 struct asoc_simple_priv *priv;
622 struct device *dev = &pdev->dev;
623 struct device_node *np = dev->of_node;
624 struct snd_soc_card *card;
628 /* Allocate the private data and the DAI link array */
629 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
633 card = simple_priv_to_card(priv);
634 card->owner = THIS_MODULE;
636 card->probe = simple_soc_probe;
638 memset(&li, 0, sizeof(li));
639 simple_get_dais_count(priv, &li);
640 if (!li.link || !li.dais)
643 ret = asoc_simple_init_priv(priv, &li);
647 if (np && of_device_is_available(np)) {
649 ret = simple_parse_of(priv);
651 if (ret != -EPROBE_DEFER)
652 dev_err(dev, "parse error %d\n", ret);
657 struct asoc_simple_card_info *cinfo;
658 struct snd_soc_dai_link_component *cpus;
659 struct snd_soc_dai_link_component *codecs;
660 struct snd_soc_dai_link_component *platform;
661 struct snd_soc_dai_link *dai_link = priv->dai_link;
662 struct simple_dai_props *dai_props = priv->dai_props;
664 cinfo = dev->platform_data;
666 dev_err(dev, "no info for asoc-simple-card\n");
671 !cinfo->codec_dai.name ||
674 !cinfo->cpu_dai.name) {
675 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
679 cpus = dai_link->cpus;
680 cpus->dai_name = cinfo->cpu_dai.name;
682 codecs = dai_link->codecs;
683 codecs->name = cinfo->codec;
684 codecs->dai_name = cinfo->codec_dai.name;
686 platform = dai_link->platforms;
687 platform->name = cinfo->platform;
689 card->name = (cinfo->card) ? cinfo->card : cinfo->name;
690 dai_link->name = cinfo->name;
691 dai_link->stream_name = cinfo->name;
692 dai_link->dai_fmt = cinfo->daifmt;
693 dai_link->init = asoc_simple_dai_init;
694 memcpy(dai_props->cpu_dai, &cinfo->cpu_dai,
695 sizeof(*dai_props->cpu_dai));
696 memcpy(dai_props->codec_dai, &cinfo->codec_dai,
697 sizeof(*dai_props->codec_dai));
700 snd_soc_card_set_drvdata(card, priv);
702 asoc_simple_debug_info(priv);
704 ret = devm_snd_soc_register_card(dev, card);
710 asoc_simple_clean_reference(card);
715 static int asoc_simple_remove(struct platform_device *pdev)
717 struct snd_soc_card *card = platform_get_drvdata(pdev);
719 return asoc_simple_clean_reference(card);
722 static const struct of_device_id simple_of_match[] = {
723 { .compatible = "simple-audio-card", },
724 { .compatible = "simple-scu-audio-card",
725 .data = (void *)DPCM_SELECTABLE },
728 MODULE_DEVICE_TABLE(of, simple_of_match);
730 static struct platform_driver asoc_simple_card = {
732 .name = "asoc-simple-card",
733 .pm = &snd_soc_pm_ops,
734 .of_match_table = simple_of_match,
736 .probe = asoc_simple_probe,
737 .remove = asoc_simple_remove,
740 module_platform_driver(asoc_simple_card);
742 MODULE_ALIAS("platform:asoc-simple-card");
743 MODULE_LICENSE("GPL v2");
744 MODULE_DESCRIPTION("ASoC Simple Sound Card");
745 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");