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_platform.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 = simple_util_startup,
27 .shutdown = simple_util_shutdown,
28 .hw_params = simple_util_hw_params,
31 static int simple_parse_platform(struct device_node *node, struct snd_soc_dai_link_component *dlc)
33 struct of_phandle_args args;
40 * Get node via "sound-dai = <&phandle port>"
41 * it will be used as xxx_of_node on soc_bind_dai_link()
43 ret = of_parse_phandle_with_args(node, DAI, CELL, 0, &args);
47 /* dai_name is not required and may not exist for plat component */
49 dlc->of_node = args.np;
54 static int simple_parse_dai(struct device *dev,
55 struct device_node *node,
56 struct snd_soc_dai_link_component *dlc,
59 struct of_phandle_args args;
60 struct snd_soc_dai *dai;
67 * Get node via "sound-dai = <&phandle port>"
68 * it will be used as xxx_of_node on soc_bind_dai_link()
70 ret = of_parse_phandle_with_args(node, DAI, CELL, 0, &args);
75 * Try to find from DAI args
77 dai = snd_soc_get_dai_via_args(&args);
79 dlc->dai_name = snd_soc_dai_name_get(dai);
80 dlc->dai_args = snd_soc_copy_dai_args(dev, &args);
90 * Here, dlc->dai_name is pointer to CPU/Codec DAI name.
91 * If user unbinded CPU or Codec driver, but not for Sound Card,
92 * dlc->dai_name is keeping unbinded CPU or Codec
95 * If user re-bind CPU or Codec driver again, ALSA SoC will try
96 * to rebind Card via snd_soc_try_rebind_card(), but because of
97 * above reason, it might can't bind Sound Card.
98 * Because Sound Card is pointing to released dai_name pointer.
100 * To avoid this rebind Card issue,
101 * 1) It needs to alloc memory to keep dai_name eventhough
102 * CPU or Codec driver was unbinded, or
103 * 2) user need to rebind Sound Card everytime
104 * if he unbinded CPU or Codec.
106 ret = snd_soc_get_dlc(&args, dlc);
112 *is_single_link = !args.args_count;
117 static void simple_parse_convert(struct device *dev,
118 struct device_node *np,
119 struct simple_util_data *adata)
121 struct device_node *top = dev->of_node;
122 struct device_node *node = of_get_parent(np);
124 simple_util_parse_convert(top, PREFIX, adata);
125 simple_util_parse_convert(node, PREFIX, adata);
126 simple_util_parse_convert(node, NULL, adata);
127 simple_util_parse_convert(np, NULL, adata);
132 static void simple_parse_mclk_fs(struct device_node *top,
133 struct device_node *np,
134 struct simple_dai_props *props,
137 struct device_node *node = of_get_parent(np);
140 snprintf(prop, sizeof(prop), "%smclk-fs", PREFIX);
141 of_property_read_u32(top, prop, &props->mclk_fs);
143 snprintf(prop, sizeof(prop), "%smclk-fs", prefix);
144 of_property_read_u32(node, prop, &props->mclk_fs);
145 of_property_read_u32(np, prop, &props->mclk_fs);
150 static int simple_parse_node(struct simple_util_priv *priv,
151 struct device_node *np,
152 struct link_info *li,
156 struct device *dev = simple_priv_to_dev(priv);
157 struct device_node *top = dev->of_node;
158 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
159 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
160 struct snd_soc_dai_link_component *dlc;
161 struct simple_util_dai *dai;
165 dlc = snd_soc_link_to_cpu(dai_link, 0);
166 dai = simple_props_to_dai_cpu(dai_props, 0);
168 dlc = snd_soc_link_to_codec(dai_link, 0);
169 dai = simple_props_to_dai_codec(dai_props, 0);
172 simple_parse_mclk_fs(top, np, dai_props, prefix);
174 ret = simple_parse_dai(dev, np, dlc, cpu);
178 ret = simple_util_parse_clk(dev, np, dai, dlc);
182 ret = simple_util_parse_tdm(np, dai);
189 static int simple_link_init(struct simple_util_priv *priv,
190 struct device_node *node,
191 struct device_node *codec,
192 struct link_info *li,
193 char *prefix, char *name)
195 struct device *dev = simple_priv_to_dev(priv);
196 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
199 ret = simple_util_parse_daifmt(dev, node, codec,
200 prefix, &dai_link->dai_fmt);
204 dai_link->init = simple_util_dai_init;
205 dai_link->ops = &simple_ops;
207 return simple_util_set_dailink_name(dev, dai_link, name);
210 static int simple_dai_link_of_dpcm(struct simple_util_priv *priv,
211 struct device_node *np,
212 struct device_node *codec,
213 struct link_info *li,
216 struct device *dev = simple_priv_to_dev(priv);
217 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
218 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
219 struct device_node *top = dev->of_node;
220 struct device_node *node = of_get_parent(np);
225 dev_dbg(dev, "link_of DPCM (%pOF)\n", np);
227 /* For single DAI link & old style of DT node */
232 struct snd_soc_dai_link_component *cpus = snd_soc_link_to_cpu(dai_link, 0);
233 struct snd_soc_dai_link_component *platforms = snd_soc_link_to_platform(dai_link, 0);
234 int is_single_links = 0;
239 dai_link->dynamic = 1;
240 dai_link->dpcm_merged_format = 1;
242 ret = simple_parse_node(priv, np, li, prefix, &is_single_links);
246 snprintf(dai_name, sizeof(dai_name), "fe.%s", cpus->dai_name);
248 simple_util_canonicalize_cpu(cpus, is_single_links);
249 simple_util_canonicalize_platform(platforms, cpus);
251 struct snd_soc_dai_link_component *codecs = snd_soc_link_to_codec(dai_link, 0);
252 struct snd_soc_codec_conf *cconf;
257 dai_link->no_pcm = 1;
258 dai_link->be_hw_params_fixup = simple_util_be_hw_params_fixup;
260 cconf = simple_props_to_codec_conf(dai_props, 0);
262 ret = simple_parse_node(priv, np, li, prefix, NULL);
266 snprintf(dai_name, sizeof(dai_name), "be.%s", codecs->dai_name);
268 /* check "prefix" from top node */
269 snd_soc_of_parse_node_prefix(top, cconf, codecs->of_node,
271 snd_soc_of_parse_node_prefix(node, cconf, codecs->of_node,
273 snd_soc_of_parse_node_prefix(np, cconf, codecs->of_node,
277 simple_parse_convert(dev, np, &dai_props->adata);
279 snd_soc_dai_link_set_capabilities(dai_link);
281 ret = simple_link_init(priv, node, codec, li, prefix, dai_name);
290 static int simple_dai_link_of(struct simple_util_priv *priv,
291 struct device_node *np,
292 struct device_node *codec,
293 struct link_info *li,
296 struct device *dev = simple_priv_to_dev(priv);
297 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
298 struct snd_soc_dai_link_component *cpus = snd_soc_link_to_cpu(dai_link, 0);
299 struct snd_soc_dai_link_component *codecs = snd_soc_link_to_codec(dai_link, 0);
300 struct snd_soc_dai_link_component *platforms = snd_soc_link_to_platform(dai_link, 0);
301 struct device_node *cpu = NULL;
302 struct device_node *node = NULL;
303 struct device_node *plat = NULL;
307 int ret, single_cpu = 0;
310 node = of_get_parent(np);
312 dev_dbg(dev, "link_of (%pOF)\n", node);
314 /* For single DAI link & old style of DT node */
318 snprintf(prop, sizeof(prop), "%splat", prefix);
319 plat = of_get_child_by_name(node, prop);
321 ret = simple_parse_node(priv, cpu, li, prefix, &single_cpu);
323 goto dai_link_of_err;
325 ret = simple_parse_node(priv, codec, li, prefix, NULL);
327 goto dai_link_of_err;
329 ret = simple_parse_platform(plat, platforms);
331 goto dai_link_of_err;
333 snprintf(dai_name, sizeof(dai_name),
334 "%s-%s", cpus->dai_name, codecs->dai_name);
336 simple_util_canonicalize_cpu(cpus, single_cpu);
337 simple_util_canonicalize_platform(platforms, cpus);
339 ret = simple_link_init(priv, node, codec, li, prefix, dai_name);
350 static int __simple_for_each_link(struct simple_util_priv *priv,
351 struct link_info *li,
352 int (*func_noml)(struct simple_util_priv *priv,
353 struct device_node *np,
354 struct device_node *codec,
355 struct link_info *li, bool is_top),
356 int (*func_dpcm)(struct simple_util_priv *priv,
357 struct device_node *np,
358 struct device_node *codec,
359 struct link_info *li, bool is_top))
361 struct device *dev = simple_priv_to_dev(priv);
362 struct device_node *top = dev->of_node;
363 struct device_node *node;
364 struct device_node *add_devs;
365 uintptr_t dpcm_selectable = (uintptr_t)of_device_get_match_data(dev);
369 /* Check if it has dai-link */
370 node = of_get_child_by_name(top, PREFIX "dai-link");
372 node = of_node_get(top);
376 add_devs = of_get_child_by_name(top, PREFIX "additional-devs");
378 /* loop for all dai-link */
380 struct simple_util_data adata;
381 struct device_node *codec;
382 struct device_node *plat;
383 struct device_node *np;
384 int num = of_get_child_count(node);
386 /* Skip additional-devs node */
387 if (node == add_devs) {
388 node = of_get_next_child(top, node);
393 codec = of_get_child_by_name(node, is_top ?
394 PREFIX "codec" : "codec");
400 plat = of_get_child_by_name(node, is_top ?
401 PREFIX "plat" : "plat");
403 /* get convert-xxx property */
404 memset(&adata, 0, sizeof(adata));
405 for_each_child_of_node(node, np) {
408 simple_parse_convert(dev, np, &adata);
411 /* loop for all CPU/Codec node */
412 for_each_child_of_node(node, np) {
413 if (plat == np || add_devs == np)
417 * if it has many CPUs,
418 * or has convert-xxx property
420 if (dpcm_selectable &&
421 (num > 2 || simple_util_is_convert_required(&adata))) {
424 * |1(CPU)|0(Codec) li->cpu
428 if (li->cpu != (np == codec))
429 ret = func_dpcm(priv, np, codec, li, is_top);
430 /* else normal sound */
434 * |1(CPU)|0(Codec) li->cpu
436 * Codec |return|return
438 if (li->cpu && (np != codec))
439 ret = func_noml(priv, np, codec, li, is_top);
452 node = of_get_next_child(top, node);
453 } while (!is_top && node);
456 of_node_put(add_devs);
461 static int simple_for_each_link(struct simple_util_priv *priv,
462 struct link_info *li,
463 int (*func_noml)(struct simple_util_priv *priv,
464 struct device_node *np,
465 struct device_node *codec,
466 struct link_info *li, bool is_top),
467 int (*func_dpcm)(struct simple_util_priv *priv,
468 struct device_node *np,
469 struct device_node *codec,
470 struct link_info *li, bool is_top))
474 * Detect all CPU first, and Detect all Codec 2nd.
476 * In Normal sound case, all DAIs are detected
479 * In DPCM sound case,
480 * all CPUs are detected as "CPU-dummy", and
481 * all Codecs are detected as "dummy-Codec".
482 * To avoid random sub-device numbering,
483 * detect "dummy-Codec" in last;
485 for (li->cpu = 1; li->cpu >= 0; li->cpu--) {
486 ret = __simple_for_each_link(priv, li, func_noml, func_dpcm);
494 static void simple_depopulate_aux(void *data)
496 struct simple_util_priv *priv = data;
498 of_platform_depopulate(simple_priv_to_dev(priv));
501 static int simple_populate_aux(struct simple_util_priv *priv)
503 struct device *dev = simple_priv_to_dev(priv);
504 struct device_node *node;
507 node = of_get_child_by_name(dev->of_node, PREFIX "additional-devs");
511 ret = of_platform_populate(node, NULL, NULL, dev);
516 return devm_add_action_or_reset(dev, simple_depopulate_aux, priv);
519 static int simple_parse_of(struct simple_util_priv *priv, struct link_info *li)
521 struct snd_soc_card *card = simple_priv_to_card(priv);
524 ret = simple_util_parse_widgets(card, PREFIX);
528 ret = simple_util_parse_routing(card, PREFIX);
532 ret = simple_util_parse_pin_switches(card, PREFIX);
536 /* Single/Muti DAI link(s) & New style of DT node */
537 memset(li, 0, sizeof(*li));
538 ret = simple_for_each_link(priv, li,
540 simple_dai_link_of_dpcm);
544 ret = simple_util_parse_card_name(card, PREFIX);
548 ret = simple_populate_aux(priv);
552 ret = snd_soc_of_parse_aux_devs(card, PREFIX "aux-devs");
557 static int simple_count_noml(struct simple_util_priv *priv,
558 struct device_node *np,
559 struct device_node *codec,
560 struct link_info *li, bool is_top)
562 if (li->link >= SNDRV_MAX_LINKS) {
563 struct device *dev = simple_priv_to_dev(priv);
565 dev_err(dev, "too many links\n");
570 * DON'T REMOVE platforms
572 * Some CPU might be using soc-generic-dmaengine-pcm. This means CPU and Platform
573 * are different Component, but are sharing same component->dev.
574 * Simple Card had been supported it without special Platform selection.
575 * We need platforms here.
577 * In case of no Platform, it will be Platform == CPU, but Platform will be
578 * ignored by snd_soc_rtd_add_component().
581 * simple-card-utils.c :: simple_util_canonicalize_platform()
583 li->num[li->link].cpus = 1;
584 li->num[li->link].platforms = 1;
586 li->num[li->link].codecs = 1;
593 static int simple_count_dpcm(struct simple_util_priv *priv,
594 struct device_node *np,
595 struct device_node *codec,
596 struct link_info *li, bool is_top)
598 if (li->link >= SNDRV_MAX_LINKS) {
599 struct device *dev = simple_priv_to_dev(priv);
601 dev_err(dev, "too many links\n");
607 * DON'T REMOVE platforms
609 * simple_count_noml()
611 li->num[li->link].cpus = 1;
612 li->num[li->link].platforms = 1;
614 li->link++; /* CPU-dummy */
616 li->num[li->link].codecs = 1;
618 li->link++; /* dummy-Codec */
624 static int simple_get_dais_count(struct simple_util_priv *priv,
625 struct link_info *li)
627 struct device *dev = simple_priv_to_dev(priv);
628 struct device_node *top = dev->of_node;
631 * link_num : number of links.
632 * CPU-Codec / CPU-dummy / dummy-Codec
633 * dais_num : number of DAIs
634 * ccnf_num : number of codec_conf
635 * same number for "dummy-Codec"
638 * CPU0 --- Codec0 link : 5
639 * CPU1 --- Codec1 dais : 7
643 * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec
644 * => 7 DAIs = 4xCPU + 3xCodec
645 * => 1 ccnf = 1xdummy-Codec
648 * CPU0 --- Codec0 link : 5
649 * CPU1 --- Codec1 dais : 6
653 * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec
654 * => 6 DAIs = 4xCPU + 2xCodec
655 * => 1 ccnf = 1xdummy-Codec
658 * CPU0 --- Codec0 link : 6
660 * CPU2 --- Codec1 ccnf : 2
663 * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec
664 * => 6 DAIs = 4xCPU + 2xCodec
665 * => 2 ccnf = 2xdummy-Codec
668 * CPU0 --- Codec0 (convert-rate) link : 3
669 * CPU1 --- Codec1 dais : 4
672 * => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec
673 * => 4 DAIs = 2xCPU + 2xCodec
674 * => 1 ccnf = 1xdummy-Codec
678 li->num[0].codecs = 1;
679 li->num[0].platforms = 1;
685 return simple_for_each_link(priv, li,
690 static int simple_soc_probe(struct snd_soc_card *card)
692 struct simple_util_priv *priv = snd_soc_card_get_drvdata(card);
695 ret = simple_util_init_hp(card, &priv->hp_jack, PREFIX);
699 ret = simple_util_init_mic(card, &priv->mic_jack, PREFIX);
703 ret = simple_util_init_aux_jacks(priv, PREFIX);
710 static int simple_probe(struct platform_device *pdev)
712 struct simple_util_priv *priv;
713 struct device *dev = &pdev->dev;
714 struct device_node *np = dev->of_node;
715 struct snd_soc_card *card;
716 struct link_info *li;
719 /* Allocate the private data and the DAI link array */
720 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
724 card = simple_priv_to_card(priv);
725 card->owner = THIS_MODULE;
727 card->probe = simple_soc_probe;
728 card->driver_name = "simple-card";
730 li = devm_kzalloc(dev, sizeof(*li), GFP_KERNEL);
734 ret = simple_get_dais_count(priv, li);
741 ret = simple_util_init_priv(priv, li);
745 if (np && of_device_is_available(np)) {
747 ret = simple_parse_of(priv, li);
749 dev_err_probe(dev, ret, "parse error\n");
754 struct simple_util_info *cinfo;
755 struct snd_soc_dai_link_component *cpus;
756 struct snd_soc_dai_link_component *codecs;
757 struct snd_soc_dai_link_component *platform;
758 struct snd_soc_dai_link *dai_link = priv->dai_link;
759 struct simple_dai_props *dai_props = priv->dai_props;
763 cinfo = dev->platform_data;
765 dev_err(dev, "no info for asoc-simple-card\n");
770 !cinfo->codec_dai.name ||
773 !cinfo->cpu_dai.name) {
774 dev_err(dev, "insufficient simple_util_info settings\n");
778 cpus = dai_link->cpus;
779 cpus->dai_name = cinfo->cpu_dai.name;
781 codecs = dai_link->codecs;
782 codecs->name = cinfo->codec;
783 codecs->dai_name = cinfo->codec_dai.name;
785 platform = dai_link->platforms;
786 platform->name = cinfo->platform;
788 card->name = (cinfo->card) ? cinfo->card : cinfo->name;
789 dai_link->name = cinfo->name;
790 dai_link->stream_name = cinfo->name;
791 dai_link->dai_fmt = cinfo->daifmt;
792 dai_link->init = simple_util_dai_init;
793 memcpy(dai_props->cpu_dai, &cinfo->cpu_dai,
794 sizeof(*dai_props->cpu_dai));
795 memcpy(dai_props->codec_dai, &cinfo->codec_dai,
796 sizeof(*dai_props->codec_dai));
799 snd_soc_card_set_drvdata(card, priv);
801 simple_util_debug_info(priv);
803 ret = devm_snd_soc_register_card(dev, card);
810 simple_util_clean_reference(card);
815 static const struct of_device_id simple_of_match[] = {
816 { .compatible = "simple-audio-card", },
817 { .compatible = "simple-scu-audio-card",
818 .data = (void *)DPCM_SELECTABLE },
821 MODULE_DEVICE_TABLE(of, simple_of_match);
823 static struct platform_driver simple_card = {
825 .name = "asoc-simple-card",
826 .pm = &snd_soc_pm_ops,
827 .of_match_table = simple_of_match,
829 .probe = simple_probe,
830 .remove_new = simple_util_remove,
833 module_platform_driver(simple_card);
835 MODULE_ALIAS("platform:asoc-simple-card");
836 MODULE_LICENSE("GPL v2");
837 MODULE_DESCRIPTION("ASoC Simple Sound Card");
838 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");