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_platform(struct device_node *node,
32 struct snd_soc_dai_link_component *dlc)
34 struct of_phandle_args args;
41 * Get node via "sound-dai = <&phandle port>"
42 * it will be used as xxx_of_node on soc_bind_dai_link()
44 ret = of_parse_phandle_with_args(node, DAI, CELL, 0, &args);
48 /* dai_name is not required and may not exist for plat component */
50 dlc->of_node = args.np;
55 static int asoc_simple_parse_dai(struct device *dev,
56 struct device_node *node,
57 struct snd_soc_dai_link_component *dlc,
60 struct of_phandle_args args;
61 struct snd_soc_dai *dai;
68 * Get node via "sound-dai = <&phandle port>"
69 * it will be used as xxx_of_node on soc_bind_dai_link()
71 ret = of_parse_phandle_with_args(node, DAI, CELL, 0, &args);
76 * Try to find from DAI args
78 dai = snd_soc_get_dai_via_args(&args);
80 dlc->dai_name = snd_soc_dai_name_get(dai);
81 dlc->dai_args = snd_soc_copy_dai_args(dev, &args);
91 * Here, dlc->dai_name is pointer to CPU/Codec DAI name.
92 * If user unbinded CPU or Codec driver, but not for Sound Card,
93 * dlc->dai_name is keeping unbinded CPU or Codec
96 * If user re-bind CPU or Codec driver again, ALSA SoC will try
97 * to rebind Card via snd_soc_try_rebind_card(), but because of
98 * above reason, it might can't bind Sound Card.
99 * Because Sound Card is pointing to released dai_name pointer.
101 * To avoid this rebind Card issue,
102 * 1) It needs to alloc memory to keep dai_name eventhough
103 * CPU or Codec driver was unbinded, or
104 * 2) user need to rebind Sound Card everytime
105 * if he unbinded CPU or Codec.
107 ret = snd_soc_get_dlc(&args, dlc);
113 *is_single_link = !args.args_count;
118 static void simple_parse_convert(struct device *dev,
119 struct device_node *np,
120 struct asoc_simple_data *adata)
122 struct device_node *top = dev->of_node;
123 struct device_node *node = of_get_parent(np);
125 asoc_simple_parse_convert(top, PREFIX, adata);
126 asoc_simple_parse_convert(node, PREFIX, adata);
127 asoc_simple_parse_convert(node, NULL, adata);
128 asoc_simple_parse_convert(np, NULL, adata);
133 static void simple_parse_mclk_fs(struct device_node *top,
134 struct device_node *np,
135 struct simple_dai_props *props,
138 struct device_node *node = of_get_parent(np);
141 snprintf(prop, sizeof(prop), "%smclk-fs", PREFIX);
142 of_property_read_u32(top, prop, &props->mclk_fs);
144 snprintf(prop, sizeof(prop), "%smclk-fs", prefix);
145 of_property_read_u32(node, prop, &props->mclk_fs);
146 of_property_read_u32(np, prop, &props->mclk_fs);
151 static int simple_parse_node(struct asoc_simple_priv *priv,
152 struct device_node *np,
153 struct link_info *li,
157 struct device *dev = simple_priv_to_dev(priv);
158 struct device_node *top = dev->of_node;
159 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
160 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
161 struct snd_soc_dai_link_component *dlc;
162 struct asoc_simple_dai *dai;
166 dlc = asoc_link_to_cpu(dai_link, 0);
167 dai = simple_props_to_dai_cpu(dai_props, 0);
169 dlc = asoc_link_to_codec(dai_link, 0);
170 dai = simple_props_to_dai_codec(dai_props, 0);
173 simple_parse_mclk_fs(top, np, dai_props, prefix);
175 ret = asoc_simple_parse_dai(dev, np, dlc, cpu);
179 ret = asoc_simple_parse_clk(dev, np, dai, dlc);
183 ret = asoc_simple_parse_tdm(np, dai);
190 static int simple_link_init(struct asoc_simple_priv *priv,
191 struct device_node *node,
192 struct device_node *codec,
193 struct link_info *li,
194 char *prefix, char *name)
196 struct device *dev = simple_priv_to_dev(priv);
197 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
200 ret = asoc_simple_parse_daifmt(dev, node, codec,
201 prefix, &dai_link->dai_fmt);
205 dai_link->init = asoc_simple_dai_init;
206 dai_link->ops = &simple_ops;
208 return asoc_simple_set_dailink_name(dev, dai_link, name);
211 static int simple_dai_link_of_dpcm(struct asoc_simple_priv *priv,
212 struct device_node *np,
213 struct device_node *codec,
214 struct link_info *li,
217 struct device *dev = simple_priv_to_dev(priv);
218 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
219 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
220 struct device_node *top = dev->of_node;
221 struct device_node *node = of_get_parent(np);
226 dev_dbg(dev, "link_of DPCM (%pOF)\n", np);
228 /* For single DAI link & old style of DT node */
233 struct snd_soc_dai_link_component *cpus = asoc_link_to_cpu(dai_link, 0);
234 struct snd_soc_dai_link_component *platforms = asoc_link_to_platform(dai_link, 0);
235 int is_single_links = 0;
240 dai_link->dynamic = 1;
241 dai_link->dpcm_merged_format = 1;
243 ret = simple_parse_node(priv, np, li, prefix, &is_single_links);
247 snprintf(dai_name, sizeof(dai_name), "fe.%s", cpus->dai_name);
249 asoc_simple_canonicalize_cpu(cpus, is_single_links);
250 asoc_simple_canonicalize_platform(platforms, cpus);
252 struct snd_soc_dai_link_component *codecs = asoc_link_to_codec(dai_link, 0);
253 struct snd_soc_codec_conf *cconf;
258 dai_link->no_pcm = 1;
259 dai_link->be_hw_params_fixup = asoc_simple_be_hw_params_fixup;
261 cconf = simple_props_to_codec_conf(dai_props, 0);
263 ret = simple_parse_node(priv, np, li, prefix, NULL);
267 snprintf(dai_name, sizeof(dai_name), "be.%s", codecs->dai_name);
269 /* check "prefix" from top node */
270 snd_soc_of_parse_node_prefix(top, cconf, codecs->of_node,
272 snd_soc_of_parse_node_prefix(node, cconf, codecs->of_node,
274 snd_soc_of_parse_node_prefix(np, cconf, codecs->of_node,
278 simple_parse_convert(dev, np, &dai_props->adata);
280 snd_soc_dai_link_set_capabilities(dai_link);
282 ret = simple_link_init(priv, node, codec, li, prefix, dai_name);
291 static int simple_dai_link_of(struct asoc_simple_priv *priv,
292 struct device_node *np,
293 struct device_node *codec,
294 struct link_info *li,
297 struct device *dev = simple_priv_to_dev(priv);
298 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
299 struct snd_soc_dai_link_component *cpus = asoc_link_to_cpu(dai_link, 0);
300 struct snd_soc_dai_link_component *codecs = asoc_link_to_codec(dai_link, 0);
301 struct snd_soc_dai_link_component *platforms = asoc_link_to_platform(dai_link, 0);
302 struct device_node *cpu = NULL;
303 struct device_node *node = NULL;
304 struct device_node *plat = NULL;
308 int ret, single_cpu = 0;
311 node = of_get_parent(np);
313 dev_dbg(dev, "link_of (%pOF)\n", node);
315 /* For single DAI link & old style of DT node */
319 snprintf(prop, sizeof(prop), "%splat", prefix);
320 plat = of_get_child_by_name(node, prop);
322 ret = simple_parse_node(priv, cpu, li, prefix, &single_cpu);
324 goto dai_link_of_err;
326 ret = simple_parse_node(priv, codec, li, prefix, NULL);
328 goto dai_link_of_err;
330 ret = asoc_simple_parse_platform(plat, platforms);
332 goto dai_link_of_err;
334 snprintf(dai_name, sizeof(dai_name),
335 "%s-%s", cpus->dai_name, codecs->dai_name);
337 asoc_simple_canonicalize_cpu(cpus, single_cpu);
338 asoc_simple_canonicalize_platform(platforms, cpus);
340 ret = simple_link_init(priv, node, codec, li, prefix, dai_name);
351 static int __simple_for_each_link(struct asoc_simple_priv *priv,
352 struct link_info *li,
353 int (*func_noml)(struct asoc_simple_priv *priv,
354 struct device_node *np,
355 struct device_node *codec,
356 struct link_info *li, bool is_top),
357 int (*func_dpcm)(struct asoc_simple_priv *priv,
358 struct device_node *np,
359 struct device_node *codec,
360 struct link_info *li, bool is_top))
362 struct device *dev = simple_priv_to_dev(priv);
363 struct device_node *top = dev->of_node;
364 struct device_node *node;
365 struct device_node *add_devs;
366 uintptr_t dpcm_selectable = (uintptr_t)of_device_get_match_data(dev);
370 /* Check if it has dai-link */
371 node = of_get_child_by_name(top, PREFIX "dai-link");
373 node = of_node_get(top);
377 add_devs = of_get_child_by_name(top, PREFIX "additional-devs");
379 /* loop for all dai-link */
381 struct asoc_simple_data adata;
382 struct device_node *codec;
383 struct device_node *plat;
384 struct device_node *np;
385 int num = of_get_child_count(node);
387 /* Skip additional-devs node */
388 if (node == add_devs) {
389 node = of_get_next_child(top, node);
394 codec = of_get_child_by_name(node, is_top ?
395 PREFIX "codec" : "codec");
401 plat = of_get_child_by_name(node, is_top ?
402 PREFIX "plat" : "plat");
404 /* get convert-xxx property */
405 memset(&adata, 0, sizeof(adata));
406 for_each_child_of_node(node, np) {
409 simple_parse_convert(dev, np, &adata);
412 /* loop for all CPU/Codec node */
413 for_each_child_of_node(node, np) {
414 if (plat == np || add_devs == np)
418 * if it has many CPUs,
419 * or has convert-xxx property
421 if (dpcm_selectable &&
422 (num > 2 || asoc_simple_is_convert_required(&adata))) {
425 * |1(CPU)|0(Codec) li->cpu
429 if (li->cpu != (np == codec))
430 ret = func_dpcm(priv, np, codec, li, is_top);
431 /* else normal sound */
435 * |1(CPU)|0(Codec) li->cpu
437 * Codec |return|return
439 if (li->cpu && (np != codec))
440 ret = func_noml(priv, np, codec, li, is_top);
453 node = of_get_next_child(top, node);
454 } while (!is_top && node);
457 of_node_put(add_devs);
462 static int simple_for_each_link(struct asoc_simple_priv *priv,
463 struct link_info *li,
464 int (*func_noml)(struct asoc_simple_priv *priv,
465 struct device_node *np,
466 struct device_node *codec,
467 struct link_info *li, bool is_top),
468 int (*func_dpcm)(struct asoc_simple_priv *priv,
469 struct device_node *np,
470 struct device_node *codec,
471 struct link_info *li, bool is_top))
475 * Detect all CPU first, and Detect all Codec 2nd.
477 * In Normal sound case, all DAIs are detected
480 * In DPCM sound case,
481 * all CPUs are detected as "CPU-dummy", and
482 * all Codecs are detected as "dummy-Codec".
483 * To avoid random sub-device numbering,
484 * detect "dummy-Codec" in last;
486 for (li->cpu = 1; li->cpu >= 0; li->cpu--) {
487 ret = __simple_for_each_link(priv, li, func_noml, func_dpcm);
495 static void simple_depopulate_aux(void *data)
497 struct asoc_simple_priv *priv = data;
499 of_platform_depopulate(simple_priv_to_dev(priv));
502 static int simple_populate_aux(struct asoc_simple_priv *priv)
504 struct device *dev = simple_priv_to_dev(priv);
505 struct device_node *node;
508 node = of_get_child_by_name(dev->of_node, PREFIX "additional-devs");
512 ret = of_platform_populate(node, NULL, NULL, dev);
517 return devm_add_action_or_reset(dev, simple_depopulate_aux, priv);
520 static int simple_parse_of(struct asoc_simple_priv *priv, struct link_info *li)
522 struct snd_soc_card *card = simple_priv_to_card(priv);
525 ret = asoc_simple_parse_widgets(card, PREFIX);
529 ret = asoc_simple_parse_routing(card, PREFIX);
533 ret = asoc_simple_parse_pin_switches(card, PREFIX);
537 /* Single/Muti DAI link(s) & New style of DT node */
538 memset(li, 0, sizeof(*li));
539 ret = simple_for_each_link(priv, li,
541 simple_dai_link_of_dpcm);
545 ret = asoc_simple_parse_card_name(card, PREFIX);
549 ret = simple_populate_aux(priv);
553 ret = snd_soc_of_parse_aux_devs(card, PREFIX "aux-devs");
558 static int simple_count_noml(struct asoc_simple_priv *priv,
559 struct device_node *np,
560 struct device_node *codec,
561 struct link_info *li, bool is_top)
563 if (li->link >= SNDRV_MAX_LINKS) {
564 struct device *dev = simple_priv_to_dev(priv);
566 dev_err(dev, "too many links\n");
571 * DON'T REMOVE platforms
573 * Some CPU might be using soc-generic-dmaengine-pcm. This means CPU and Platform
574 * are different Component, but are sharing same component->dev.
575 * Simple Card had been supported it without special Platform selection.
576 * We need platforms here.
578 * In case of no Platform, it will be Platform == CPU, but Platform will be
579 * ignored by snd_soc_rtd_add_component().
582 * simple-card-utils.c :: asoc_simple_canonicalize_platform()
584 li->num[li->link].cpus = 1;
585 li->num[li->link].platforms = 1;
587 li->num[li->link].codecs = 1;
594 static int simple_count_dpcm(struct asoc_simple_priv *priv,
595 struct device_node *np,
596 struct device_node *codec,
597 struct link_info *li, bool is_top)
599 if (li->link >= SNDRV_MAX_LINKS) {
600 struct device *dev = simple_priv_to_dev(priv);
602 dev_err(dev, "too many links\n");
608 * DON'T REMOVE platforms
610 * simple_count_noml()
612 li->num[li->link].cpus = 1;
613 li->num[li->link].platforms = 1;
615 li->link++; /* CPU-dummy */
617 li->num[li->link].codecs = 1;
619 li->link++; /* dummy-Codec */
625 static int simple_get_dais_count(struct asoc_simple_priv *priv,
626 struct link_info *li)
628 struct device *dev = simple_priv_to_dev(priv);
629 struct device_node *top = dev->of_node;
632 * link_num : number of links.
633 * CPU-Codec / CPU-dummy / dummy-Codec
634 * dais_num : number of DAIs
635 * ccnf_num : number of codec_conf
636 * same number for "dummy-Codec"
639 * CPU0 --- Codec0 link : 5
640 * CPU1 --- Codec1 dais : 7
644 * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec
645 * => 7 DAIs = 4xCPU + 3xCodec
646 * => 1 ccnf = 1xdummy-Codec
649 * CPU0 --- Codec0 link : 5
650 * CPU1 --- Codec1 dais : 6
654 * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec
655 * => 6 DAIs = 4xCPU + 2xCodec
656 * => 1 ccnf = 1xdummy-Codec
659 * CPU0 --- Codec0 link : 6
661 * CPU2 --- Codec1 ccnf : 2
664 * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec
665 * => 6 DAIs = 4xCPU + 2xCodec
666 * => 2 ccnf = 2xdummy-Codec
669 * CPU0 --- Codec0 (convert-rate) link : 3
670 * CPU1 --- Codec1 dais : 4
673 * => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec
674 * => 4 DAIs = 2xCPU + 2xCodec
675 * => 1 ccnf = 1xdummy-Codec
679 li->num[0].codecs = 1;
680 li->num[0].platforms = 1;
686 return simple_for_each_link(priv, li,
691 static int simple_soc_probe(struct snd_soc_card *card)
693 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(card);
696 ret = asoc_simple_init_hp(card, &priv->hp_jack, PREFIX);
700 ret = asoc_simple_init_mic(card, &priv->mic_jack, PREFIX);
704 ret = asoc_simple_init_aux_jacks(priv, PREFIX);
711 static int asoc_simple_probe(struct platform_device *pdev)
713 struct asoc_simple_priv *priv;
714 struct device *dev = &pdev->dev;
715 struct device_node *np = dev->of_node;
716 struct snd_soc_card *card;
717 struct link_info *li;
720 /* Allocate the private data and the DAI link array */
721 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
725 card = simple_priv_to_card(priv);
726 card->owner = THIS_MODULE;
728 card->probe = simple_soc_probe;
729 card->driver_name = "simple-card";
731 li = devm_kzalloc(dev, sizeof(*li), GFP_KERNEL);
735 ret = simple_get_dais_count(priv, li);
742 ret = asoc_simple_init_priv(priv, li);
746 if (np && of_device_is_available(np)) {
748 ret = simple_parse_of(priv, li);
750 dev_err_probe(dev, ret, "parse error\n");
755 struct asoc_simple_card_info *cinfo;
756 struct snd_soc_dai_link_component *cpus;
757 struct snd_soc_dai_link_component *codecs;
758 struct snd_soc_dai_link_component *platform;
759 struct snd_soc_dai_link *dai_link = priv->dai_link;
760 struct simple_dai_props *dai_props = priv->dai_props;
764 cinfo = dev->platform_data;
766 dev_err(dev, "no info for asoc-simple-card\n");
771 !cinfo->codec_dai.name ||
774 !cinfo->cpu_dai.name) {
775 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
779 cpus = dai_link->cpus;
780 cpus->dai_name = cinfo->cpu_dai.name;
782 codecs = dai_link->codecs;
783 codecs->name = cinfo->codec;
784 codecs->dai_name = cinfo->codec_dai.name;
786 platform = dai_link->platforms;
787 platform->name = cinfo->platform;
789 card->name = (cinfo->card) ? cinfo->card : cinfo->name;
790 dai_link->name = cinfo->name;
791 dai_link->stream_name = cinfo->name;
792 dai_link->dai_fmt = cinfo->daifmt;
793 dai_link->init = asoc_simple_dai_init;
794 memcpy(dai_props->cpu_dai, &cinfo->cpu_dai,
795 sizeof(*dai_props->cpu_dai));
796 memcpy(dai_props->codec_dai, &cinfo->codec_dai,
797 sizeof(*dai_props->codec_dai));
800 snd_soc_card_set_drvdata(card, priv);
802 asoc_simple_debug_info(priv);
804 ret = devm_snd_soc_register_card(dev, card);
811 asoc_simple_clean_reference(card);
816 static const struct of_device_id simple_of_match[] = {
817 { .compatible = "simple-audio-card", },
818 { .compatible = "simple-scu-audio-card",
819 .data = (void *)DPCM_SELECTABLE },
822 MODULE_DEVICE_TABLE(of, simple_of_match);
824 static struct platform_driver asoc_simple_card = {
826 .name = "asoc-simple-card",
827 .pm = &snd_soc_pm_ops,
828 .of_match_table = simple_of_match,
830 .probe = asoc_simple_probe,
831 .remove = asoc_simple_remove,
834 module_platform_driver(asoc_simple_card);
836 MODULE_ALIAS("platform:asoc-simple-card");
837 MODULE_LICENSE("GPL v2");
838 MODULE_DESCRIPTION("ASoC Simple Sound Card");
839 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");