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(top, PREFIX, adata);
88 asoc_simple_parse_convert(node, PREFIX, adata);
89 asoc_simple_parse_convert(node, NULL, adata);
90 asoc_simple_parse_convert(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 = asoc_link_to_cpu(dai_link, 0);
126 struct snd_soc_dai_link_component *codecs = asoc_link_to_codec(dai_link, 0);
127 struct snd_soc_dai_link_component *platforms = asoc_link_to_platform(dai_link, 0);
128 struct device_node *top = dev->of_node;
129 struct device_node *node = of_get_parent(np);
133 dev_dbg(dev, "link_of DPCM (%pOF)\n", np);
137 /* For single DAI link & old style of DT node */
142 int is_single_links = 0;
147 dai_link->dynamic = 1;
148 dai_link->dpcm_merged_format = 1;
150 dai = simple_props_to_dai_cpu(dai_props, 0);
152 ret = asoc_simple_parse_dai(np, cpus, &is_single_links);
156 ret = asoc_simple_parse_clk(dev, np, dai, cpus);
160 ret = asoc_simple_set_dailink_name(dev, dai_link,
166 asoc_simple_canonicalize_cpu(cpus, is_single_links);
167 asoc_simple_canonicalize_platform(platforms, cpus);
169 struct snd_soc_codec_conf *cconf;
174 dai_link->no_pcm = 1;
175 dai_link->be_hw_params_fixup = asoc_simple_be_hw_params_fixup;
177 dai = simple_props_to_dai_codec(dai_props, 0);
178 cconf = simple_props_to_codec_conf(dai_props, 0);
180 ret = asoc_simple_parse_dai(np, codecs, NULL);
184 ret = asoc_simple_parse_clk(dev, np, dai, codecs);
188 ret = asoc_simple_set_dailink_name(dev, dai_link,
194 /* check "prefix" from top node */
195 snd_soc_of_parse_node_prefix(top, cconf, codecs->of_node,
197 snd_soc_of_parse_node_prefix(node, cconf, codecs->of_node,
199 snd_soc_of_parse_node_prefix(np, cconf, codecs->of_node,
203 simple_parse_convert(dev, np, &dai_props->adata);
204 simple_parse_mclk_fs(top, np, codec, dai_props, prefix);
206 ret = asoc_simple_parse_tdm(np, dai);
210 ret = asoc_simple_parse_daifmt(dev, node, codec,
211 prefix, &dai_link->dai_fmt);
215 snd_soc_dai_link_set_capabilities(dai_link);
217 dai_link->ops = &simple_ops;
218 dai_link->init = asoc_simple_dai_init;
225 static int simple_dai_link_of(struct asoc_simple_priv *priv,
226 struct device_node *np,
227 struct device_node *codec,
228 struct link_info *li,
231 struct device *dev = simple_priv_to_dev(priv);
232 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
233 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
234 struct asoc_simple_dai *cpu_dai = simple_props_to_dai_cpu(dai_props, 0);
235 struct asoc_simple_dai *codec_dai = simple_props_to_dai_codec(dai_props, 0);
236 struct snd_soc_dai_link_component *cpus = asoc_link_to_cpu(dai_link, 0);
237 struct snd_soc_dai_link_component *codecs = asoc_link_to_codec(dai_link, 0);
238 struct snd_soc_dai_link_component *platforms = asoc_link_to_platform(dai_link, 0);
239 struct device_node *top = dev->of_node;
240 struct device_node *cpu = NULL;
241 struct device_node *node = NULL;
242 struct device_node *plat = NULL;
245 int ret, single_cpu = 0;
248 node = of_get_parent(np);
251 dev_dbg(dev, "link_of (%pOF)\n", node);
253 /* For single DAI link & old style of DT node */
257 snprintf(prop, sizeof(prop), "%splat", prefix);
258 plat = of_get_child_by_name(node, prop);
260 ret = asoc_simple_parse_daifmt(dev, node, codec,
261 prefix, &dai_link->dai_fmt);
263 goto dai_link_of_err;
265 simple_parse_mclk_fs(top, cpu, codec, dai_props, prefix);
267 ret = asoc_simple_parse_dai(cpu, cpus, &single_cpu);
269 goto dai_link_of_err;
271 ret = asoc_simple_parse_dai(codec, codecs, NULL);
273 goto dai_link_of_err;
275 ret = asoc_simple_parse_dai(plat, platforms, NULL);
277 goto dai_link_of_err;
279 ret = asoc_simple_parse_tdm(cpu, cpu_dai);
281 goto dai_link_of_err;
283 ret = asoc_simple_parse_tdm(codec, codec_dai);
285 goto dai_link_of_err;
287 ret = asoc_simple_parse_clk(dev, cpu, cpu_dai, cpus);
289 goto dai_link_of_err;
291 ret = asoc_simple_parse_clk(dev, codec, codec_dai, codecs);
293 goto dai_link_of_err;
295 ret = asoc_simple_set_dailink_name(dev, dai_link,
300 goto dai_link_of_err;
302 dai_link->ops = &simple_ops;
303 dai_link->init = asoc_simple_dai_init;
305 asoc_simple_canonicalize_cpu(cpus, single_cpu);
306 asoc_simple_canonicalize_platform(platforms, cpus);
315 static int __simple_for_each_link(struct asoc_simple_priv *priv,
316 struct link_info *li,
317 int (*func_noml)(struct asoc_simple_priv *priv,
318 struct device_node *np,
319 struct device_node *codec,
320 struct link_info *li, bool is_top),
321 int (*func_dpcm)(struct asoc_simple_priv *priv,
322 struct device_node *np,
323 struct device_node *codec,
324 struct link_info *li, bool is_top))
326 struct device *dev = simple_priv_to_dev(priv);
327 struct device_node *top = dev->of_node;
328 struct device_node *node;
329 uintptr_t dpcm_selectable = (uintptr_t)of_device_get_match_data(dev);
333 /* Check if it has dai-link */
334 node = of_get_child_by_name(top, PREFIX "dai-link");
336 node = of_node_get(top);
340 /* loop for all dai-link */
342 struct asoc_simple_data adata;
343 struct device_node *codec;
344 struct device_node *plat;
345 struct device_node *np;
346 int num = of_get_child_count(node);
349 codec = of_get_child_by_name(node, is_top ?
350 PREFIX "codec" : "codec");
356 plat = of_get_child_by_name(node, is_top ?
357 PREFIX "plat" : "plat");
359 /* get convert-xxx property */
360 memset(&adata, 0, sizeof(adata));
361 for_each_child_of_node(node, np)
362 simple_parse_convert(dev, np, &adata);
364 /* loop for all CPU/Codec node */
365 for_each_child_of_node(node, np) {
370 * if it has many CPUs,
371 * or has convert-xxx property
373 if (dpcm_selectable &&
375 adata.convert_rate || adata.convert_channels)) {
378 * |1(CPU)|0(Codec) li->cpu
382 if (li->cpu != (np == codec))
383 ret = func_dpcm(priv, np, codec, li, is_top);
384 /* else normal sound */
388 * |1(CPU)|0(Codec) li->cpu
390 * Codec |return|return
392 if (li->cpu && (np != codec))
393 ret = func_noml(priv, np, codec, li, is_top);
404 node = of_get_next_child(top, node);
405 } while (!is_top && node);
412 static int simple_for_each_link(struct asoc_simple_priv *priv,
413 struct link_info *li,
414 int (*func_noml)(struct asoc_simple_priv *priv,
415 struct device_node *np,
416 struct device_node *codec,
417 struct link_info *li, bool is_top),
418 int (*func_dpcm)(struct asoc_simple_priv *priv,
419 struct device_node *np,
420 struct device_node *codec,
421 struct link_info *li, bool is_top))
425 * Detect all CPU first, and Detect all Codec 2nd.
427 * In Normal sound case, all DAIs are detected
430 * In DPCM sound case,
431 * all CPUs are detected as "CPU-dummy", and
432 * all Codecs are detected as "dummy-Codec".
433 * To avoid random sub-device numbering,
434 * detect "dummy-Codec" in last;
436 for (li->cpu = 1; li->cpu >= 0; li->cpu--) {
437 ret = __simple_for_each_link(priv, li, func_noml, func_dpcm);
445 static int simple_parse_of(struct asoc_simple_priv *priv, struct link_info *li)
447 struct snd_soc_card *card = simple_priv_to_card(priv);
450 ret = asoc_simple_parse_widgets(card, PREFIX);
454 ret = asoc_simple_parse_routing(card, PREFIX);
458 ret = asoc_simple_parse_pin_switches(card, PREFIX);
462 /* Single/Muti DAI link(s) & New style of DT node */
463 memset(li, 0, sizeof(*li));
464 ret = simple_for_each_link(priv, li,
466 simple_dai_link_of_dpcm);
470 ret = asoc_simple_parse_card_name(card, PREFIX);
474 ret = snd_soc_of_parse_aux_devs(card, PREFIX "aux-devs");
479 static int simple_count_noml(struct asoc_simple_priv *priv,
480 struct device_node *np,
481 struct device_node *codec,
482 struct link_info *li, bool is_top)
484 if (li->link >= SNDRV_MAX_LINKS) {
485 struct device *dev = simple_priv_to_dev(priv);
487 dev_err(dev, "too many links\n");
491 li->num[li->link].cpus = 1;
492 li->num[li->link].codecs = 1;
493 li->num[li->link].platforms = 1;
500 static int simple_count_dpcm(struct asoc_simple_priv *priv,
501 struct device_node *np,
502 struct device_node *codec,
503 struct link_info *li, bool is_top)
505 if (li->link >= SNDRV_MAX_LINKS) {
506 struct device *dev = simple_priv_to_dev(priv);
508 dev_err(dev, "too many links\n");
513 li->num[li->link].cpus = 1;
514 li->num[li->link].platforms = 1;
516 li->link++; /* CPU-dummy */
518 li->num[li->link].codecs = 1;
520 li->link++; /* dummy-Codec */
526 static int simple_get_dais_count(struct asoc_simple_priv *priv,
527 struct link_info *li)
529 struct device *dev = simple_priv_to_dev(priv);
530 struct device_node *top = dev->of_node;
533 * link_num : number of links.
534 * CPU-Codec / CPU-dummy / dummy-Codec
535 * dais_num : number of DAIs
536 * ccnf_num : number of codec_conf
537 * same number for "dummy-Codec"
540 * CPU0 --- Codec0 link : 5
541 * CPU1 --- Codec1 dais : 7
545 * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec
546 * => 7 DAIs = 4xCPU + 3xCodec
547 * => 1 ccnf = 1xdummy-Codec
550 * CPU0 --- Codec0 link : 5
551 * CPU1 --- Codec1 dais : 6
555 * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec
556 * => 6 DAIs = 4xCPU + 2xCodec
557 * => 1 ccnf = 1xdummy-Codec
560 * CPU0 --- Codec0 link : 6
562 * CPU2 --- Codec1 ccnf : 2
565 * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec
566 * => 6 DAIs = 4xCPU + 2xCodec
567 * => 2 ccnf = 2xdummy-Codec
570 * CPU0 --- Codec0 (convert-rate) link : 3
571 * CPU1 --- Codec1 dais : 4
574 * => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec
575 * => 4 DAIs = 2xCPU + 2xCodec
576 * => 1 ccnf = 1xdummy-Codec
580 li->num[0].codecs = 1;
581 li->num[0].platforms = 1;
587 return simple_for_each_link(priv, li,
592 static int simple_soc_probe(struct snd_soc_card *card)
594 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(card);
597 ret = asoc_simple_init_hp(card, &priv->hp_jack, PREFIX);
601 ret = asoc_simple_init_mic(card, &priv->mic_jack, PREFIX);
608 static int asoc_simple_probe(struct platform_device *pdev)
610 struct asoc_simple_priv *priv;
611 struct device *dev = &pdev->dev;
612 struct device_node *np = dev->of_node;
613 struct snd_soc_card *card;
614 struct link_info *li;
617 /* Allocate the private data and the DAI link array */
618 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
622 card = simple_priv_to_card(priv);
623 card->owner = THIS_MODULE;
625 card->probe = simple_soc_probe;
627 li = devm_kzalloc(dev, sizeof(*li), GFP_KERNEL);
631 ret = simple_get_dais_count(priv, li);
638 ret = asoc_simple_init_priv(priv, li);
642 if (np && of_device_is_available(np)) {
644 ret = simple_parse_of(priv, li);
646 if (ret != -EPROBE_DEFER)
647 dev_err(dev, "parse error %d\n", ret);
652 struct asoc_simple_card_info *cinfo;
653 struct snd_soc_dai_link_component *cpus;
654 struct snd_soc_dai_link_component *codecs;
655 struct snd_soc_dai_link_component *platform;
656 struct snd_soc_dai_link *dai_link = priv->dai_link;
657 struct simple_dai_props *dai_props = priv->dai_props;
659 cinfo = dev->platform_data;
661 dev_err(dev, "no info for asoc-simple-card\n");
666 !cinfo->codec_dai.name ||
669 !cinfo->cpu_dai.name) {
670 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
674 cpus = dai_link->cpus;
675 cpus->dai_name = cinfo->cpu_dai.name;
677 codecs = dai_link->codecs;
678 codecs->name = cinfo->codec;
679 codecs->dai_name = cinfo->codec_dai.name;
681 platform = dai_link->platforms;
682 platform->name = cinfo->platform;
684 card->name = (cinfo->card) ? cinfo->card : cinfo->name;
685 dai_link->name = cinfo->name;
686 dai_link->stream_name = cinfo->name;
687 dai_link->dai_fmt = cinfo->daifmt;
688 dai_link->init = asoc_simple_dai_init;
689 memcpy(dai_props->cpu_dai, &cinfo->cpu_dai,
690 sizeof(*dai_props->cpu_dai));
691 memcpy(dai_props->codec_dai, &cinfo->codec_dai,
692 sizeof(*dai_props->codec_dai));
695 snd_soc_card_set_drvdata(card, priv);
697 asoc_simple_debug_info(priv);
699 ret = devm_snd_soc_register_card(dev, card);
706 asoc_simple_clean_reference(card);
711 static const struct of_device_id simple_of_match[] = {
712 { .compatible = "simple-audio-card", },
713 { .compatible = "simple-scu-audio-card",
714 .data = (void *)DPCM_SELECTABLE },
717 MODULE_DEVICE_TABLE(of, simple_of_match);
719 static struct platform_driver asoc_simple_card = {
721 .name = "asoc-simple-card",
722 .pm = &snd_soc_pm_ops,
723 .of_match_table = simple_of_match,
725 .probe = asoc_simple_probe,
726 .remove = asoc_simple_remove,
729 module_platform_driver(asoc_simple_card);
731 MODULE_ALIAS("platform:asoc-simple-card");
732 MODULE_LICENSE("GPL v2");
733 MODULE_DESCRIPTION("ASoC Simple Sound Card");
734 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");