1 // SPDX-License-Identifier: GPL-2.0
3 // ASoC simple sound card support
5 // Copyright (C) 2012 Renesas Solutions Corp.
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_node *node,
56 struct snd_soc_dai_link_component *dlc,
59 struct of_phandle_args args;
66 * Get node via "sound-dai = <&phandle port>"
67 * it will be used as xxx_of_node on soc_bind_dai_link()
69 ret = of_parse_phandle_with_args(node, DAI, CELL, 0, &args);
76 * Here, dlc->dai_name is pointer to CPU/Codec DAI name.
77 * If user unbinded CPU or Codec driver, but not for Sound Card,
78 * dlc->dai_name is keeping unbinded CPU or Codec
81 * If user re-bind CPU or Codec driver again, ALSA SoC will try
82 * to rebind Card via snd_soc_try_rebind_card(), but because of
83 * above reason, it might can't bind Sound Card.
84 * Because Sound Card is pointing to released dai_name pointer.
86 * To avoid this rebind Card issue,
87 * 1) It needs to alloc memory to keep dai_name eventhough
88 * CPU or Codec driver was unbinded, or
89 * 2) user need to rebind Sound Card everytime
90 * if he unbinded CPU or Codec.
92 ret = snd_soc_of_get_dai_name(node, &dlc->dai_name);
96 dlc->of_node = args.np;
99 *is_single_link = !args.args_count;
104 static void simple_parse_convert(struct device *dev,
105 struct device_node *np,
106 struct asoc_simple_data *adata)
108 struct device_node *top = dev->of_node;
109 struct device_node *node = of_get_parent(np);
111 asoc_simple_parse_convert(top, PREFIX, adata);
112 asoc_simple_parse_convert(node, PREFIX, adata);
113 asoc_simple_parse_convert(node, NULL, adata);
114 asoc_simple_parse_convert(np, NULL, adata);
119 static void simple_parse_mclk_fs(struct device_node *top,
120 struct device_node *np,
121 struct simple_dai_props *props,
124 struct device_node *node = of_get_parent(np);
127 snprintf(prop, sizeof(prop), "%smclk-fs", PREFIX);
128 of_property_read_u32(top, prop, &props->mclk_fs);
130 snprintf(prop, sizeof(prop), "%smclk-fs", prefix);
131 of_property_read_u32(node, prop, &props->mclk_fs);
132 of_property_read_u32(np, prop, &props->mclk_fs);
137 static int simple_parse_node(struct asoc_simple_priv *priv,
138 struct device_node *np,
139 struct link_info *li,
143 struct device *dev = simple_priv_to_dev(priv);
144 struct device_node *top = dev->of_node;
145 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
146 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
147 struct snd_soc_dai_link_component *dlc;
148 struct asoc_simple_dai *dai;
152 dlc = asoc_link_to_cpu(dai_link, 0);
153 dai = simple_props_to_dai_cpu(dai_props, 0);
155 dlc = asoc_link_to_codec(dai_link, 0);
156 dai = simple_props_to_dai_codec(dai_props, 0);
159 simple_parse_mclk_fs(top, np, dai_props, prefix);
161 ret = asoc_simple_parse_dai(np, dlc, cpu);
165 ret = asoc_simple_parse_clk(dev, np, dai, dlc);
169 ret = asoc_simple_parse_tdm(np, dai);
176 static int simple_link_init(struct asoc_simple_priv *priv,
177 struct device_node *node,
178 struct device_node *codec,
179 struct link_info *li,
180 char *prefix, char *name)
182 struct device *dev = simple_priv_to_dev(priv);
183 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
186 ret = asoc_simple_parse_daifmt(dev, node, codec,
187 prefix, &dai_link->dai_fmt);
191 dai_link->init = asoc_simple_dai_init;
192 dai_link->ops = &simple_ops;
194 return asoc_simple_set_dailink_name(dev, dai_link, name);
197 static int simple_dai_link_of_dpcm(struct asoc_simple_priv *priv,
198 struct device_node *np,
199 struct device_node *codec,
200 struct link_info *li,
203 struct device *dev = simple_priv_to_dev(priv);
204 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
205 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
206 struct device_node *top = dev->of_node;
207 struct device_node *node = of_get_parent(np);
212 dev_dbg(dev, "link_of DPCM (%pOF)\n", np);
214 /* For single DAI link & old style of DT node */
219 struct snd_soc_dai_link_component *cpus = asoc_link_to_cpu(dai_link, 0);
220 struct snd_soc_dai_link_component *platforms = asoc_link_to_platform(dai_link, 0);
221 int is_single_links = 0;
226 dai_link->dynamic = 1;
227 dai_link->dpcm_merged_format = 1;
229 ret = simple_parse_node(priv, np, li, prefix, &is_single_links);
233 snprintf(dai_name, sizeof(dai_name), "fe.%s", cpus->dai_name);
235 asoc_simple_canonicalize_cpu(cpus, is_single_links);
236 asoc_simple_canonicalize_platform(platforms, cpus);
238 struct snd_soc_dai_link_component *codecs = asoc_link_to_codec(dai_link, 0);
239 struct snd_soc_codec_conf *cconf;
244 dai_link->no_pcm = 1;
245 dai_link->be_hw_params_fixup = asoc_simple_be_hw_params_fixup;
247 cconf = simple_props_to_codec_conf(dai_props, 0);
249 ret = simple_parse_node(priv, np, li, prefix, NULL);
253 snprintf(dai_name, sizeof(dai_name), "be.%s", codecs->dai_name);
255 /* check "prefix" from top node */
256 snd_soc_of_parse_node_prefix(top, cconf, codecs->of_node,
258 snd_soc_of_parse_node_prefix(node, cconf, codecs->of_node,
260 snd_soc_of_parse_node_prefix(np, cconf, codecs->of_node,
264 simple_parse_convert(dev, np, &dai_props->adata);
266 snd_soc_dai_link_set_capabilities(dai_link);
268 ret = simple_link_init(priv, node, codec, li, prefix, dai_name);
277 static int simple_dai_link_of(struct asoc_simple_priv *priv,
278 struct device_node *np,
279 struct device_node *codec,
280 struct link_info *li,
283 struct device *dev = simple_priv_to_dev(priv);
284 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
285 struct snd_soc_dai_link_component *cpus = asoc_link_to_cpu(dai_link, 0);
286 struct snd_soc_dai_link_component *codecs = asoc_link_to_codec(dai_link, 0);
287 struct snd_soc_dai_link_component *platforms = asoc_link_to_platform(dai_link, 0);
288 struct device_node *cpu = NULL;
289 struct device_node *node = NULL;
290 struct device_node *plat = NULL;
294 int ret, single_cpu = 0;
297 node = of_get_parent(np);
299 dev_dbg(dev, "link_of (%pOF)\n", node);
301 /* For single DAI link & old style of DT node */
305 snprintf(prop, sizeof(prop), "%splat", prefix);
306 plat = of_get_child_by_name(node, prop);
308 ret = simple_parse_node(priv, cpu, li, prefix, &single_cpu);
310 goto dai_link_of_err;
312 ret = simple_parse_node(priv, codec, li, prefix, NULL);
314 goto dai_link_of_err;
316 ret = asoc_simple_parse_platform(plat, platforms);
318 goto dai_link_of_err;
320 snprintf(dai_name, sizeof(dai_name),
321 "%s-%s", cpus->dai_name, codecs->dai_name);
323 asoc_simple_canonicalize_cpu(cpus, single_cpu);
324 asoc_simple_canonicalize_platform(platforms, cpus);
326 ret = simple_link_init(priv, node, codec, li, prefix, dai_name);
337 static int __simple_for_each_link(struct asoc_simple_priv *priv,
338 struct link_info *li,
339 int (*func_noml)(struct asoc_simple_priv *priv,
340 struct device_node *np,
341 struct device_node *codec,
342 struct link_info *li, bool is_top),
343 int (*func_dpcm)(struct asoc_simple_priv *priv,
344 struct device_node *np,
345 struct device_node *codec,
346 struct link_info *li, bool is_top))
348 struct device *dev = simple_priv_to_dev(priv);
349 struct device_node *top = dev->of_node;
350 struct device_node *node;
351 uintptr_t dpcm_selectable = (uintptr_t)of_device_get_match_data(dev);
355 /* Check if it has dai-link */
356 node = of_get_child_by_name(top, PREFIX "dai-link");
358 node = of_node_get(top);
362 /* loop for all dai-link */
364 struct asoc_simple_data adata;
365 struct device_node *codec;
366 struct device_node *plat;
367 struct device_node *np;
368 int num = of_get_child_count(node);
371 codec = of_get_child_by_name(node, is_top ?
372 PREFIX "codec" : "codec");
378 plat = of_get_child_by_name(node, is_top ?
379 PREFIX "plat" : "plat");
381 /* get convert-xxx property */
382 memset(&adata, 0, sizeof(adata));
383 for_each_child_of_node(node, np)
384 simple_parse_convert(dev, np, &adata);
386 /* loop for all CPU/Codec node */
387 for_each_child_of_node(node, np) {
392 * if it has many CPUs,
393 * or has convert-xxx property
395 if (dpcm_selectable &&
396 (num > 2 || asoc_simple_is_convert_required(&adata))) {
399 * |1(CPU)|0(Codec) li->cpu
403 if (li->cpu != (np == codec))
404 ret = func_dpcm(priv, np, codec, li, is_top);
405 /* else normal sound */
409 * |1(CPU)|0(Codec) li->cpu
411 * Codec |return|return
413 if (li->cpu && (np != codec))
414 ret = func_noml(priv, np, codec, li, is_top);
426 node = of_get_next_child(top, node);
427 } while (!is_top && node);
434 static int simple_for_each_link(struct asoc_simple_priv *priv,
435 struct link_info *li,
436 int (*func_noml)(struct asoc_simple_priv *priv,
437 struct device_node *np,
438 struct device_node *codec,
439 struct link_info *li, bool is_top),
440 int (*func_dpcm)(struct asoc_simple_priv *priv,
441 struct device_node *np,
442 struct device_node *codec,
443 struct link_info *li, bool is_top))
447 * Detect all CPU first, and Detect all Codec 2nd.
449 * In Normal sound case, all DAIs are detected
452 * In DPCM sound case,
453 * all CPUs are detected as "CPU-dummy", and
454 * all Codecs are detected as "dummy-Codec".
455 * To avoid random sub-device numbering,
456 * detect "dummy-Codec" in last;
458 for (li->cpu = 1; li->cpu >= 0; li->cpu--) {
459 ret = __simple_for_each_link(priv, li, func_noml, func_dpcm);
467 static int simple_parse_of(struct asoc_simple_priv *priv, struct link_info *li)
469 struct snd_soc_card *card = simple_priv_to_card(priv);
472 ret = asoc_simple_parse_widgets(card, PREFIX);
476 ret = asoc_simple_parse_routing(card, PREFIX);
480 ret = asoc_simple_parse_pin_switches(card, PREFIX);
484 /* Single/Muti DAI link(s) & New style of DT node */
485 memset(li, 0, sizeof(*li));
486 ret = simple_for_each_link(priv, li,
488 simple_dai_link_of_dpcm);
492 ret = asoc_simple_parse_card_name(card, PREFIX);
496 ret = snd_soc_of_parse_aux_devs(card, PREFIX "aux-devs");
501 static int simple_count_noml(struct asoc_simple_priv *priv,
502 struct device_node *np,
503 struct device_node *codec,
504 struct link_info *li, bool is_top)
506 if (li->link >= SNDRV_MAX_LINKS) {
507 struct device *dev = simple_priv_to_dev(priv);
509 dev_err(dev, "too many links\n");
514 * DON'T REMOVE platforms
516 * Some CPU might be using soc-generic-dmaengine-pcm. This means CPU and Platform
517 * are different Component, but are sharing same component->dev.
518 * Simple Card had been supported it without special Platform selection.
519 * We need platforms here.
521 * In case of no Platform, it will be Platform == CPU, but Platform will be
522 * ignored by snd_soc_rtd_add_component().
525 * simple-card-utils.c :: asoc_simple_canonicalize_platform()
527 li->num[li->link].cpus = 1;
528 li->num[li->link].platforms = 1;
530 li->num[li->link].codecs = 1;
537 static int simple_count_dpcm(struct asoc_simple_priv *priv,
538 struct device_node *np,
539 struct device_node *codec,
540 struct link_info *li, bool is_top)
542 if (li->link >= SNDRV_MAX_LINKS) {
543 struct device *dev = simple_priv_to_dev(priv);
545 dev_err(dev, "too many links\n");
551 * DON'T REMOVE platforms
553 * simple_count_noml()
555 li->num[li->link].cpus = 1;
556 li->num[li->link].platforms = 1;
558 li->link++; /* CPU-dummy */
560 li->num[li->link].codecs = 1;
562 li->link++; /* dummy-Codec */
568 static int simple_get_dais_count(struct asoc_simple_priv *priv,
569 struct link_info *li)
571 struct device *dev = simple_priv_to_dev(priv);
572 struct device_node *top = dev->of_node;
575 * link_num : number of links.
576 * CPU-Codec / CPU-dummy / dummy-Codec
577 * dais_num : number of DAIs
578 * ccnf_num : number of codec_conf
579 * same number for "dummy-Codec"
582 * CPU0 --- Codec0 link : 5
583 * CPU1 --- Codec1 dais : 7
587 * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec
588 * => 7 DAIs = 4xCPU + 3xCodec
589 * => 1 ccnf = 1xdummy-Codec
592 * CPU0 --- Codec0 link : 5
593 * CPU1 --- Codec1 dais : 6
597 * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec
598 * => 6 DAIs = 4xCPU + 2xCodec
599 * => 1 ccnf = 1xdummy-Codec
602 * CPU0 --- Codec0 link : 6
604 * CPU2 --- Codec1 ccnf : 2
607 * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec
608 * => 6 DAIs = 4xCPU + 2xCodec
609 * => 2 ccnf = 2xdummy-Codec
612 * CPU0 --- Codec0 (convert-rate) link : 3
613 * CPU1 --- Codec1 dais : 4
616 * => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec
617 * => 4 DAIs = 2xCPU + 2xCodec
618 * => 1 ccnf = 1xdummy-Codec
622 li->num[0].codecs = 1;
623 li->num[0].platforms = 1;
629 return simple_for_each_link(priv, li,
634 static int simple_soc_probe(struct snd_soc_card *card)
636 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(card);
639 ret = asoc_simple_init_hp(card, &priv->hp_jack, PREFIX);
643 ret = asoc_simple_init_mic(card, &priv->mic_jack, PREFIX);
647 ret = asoc_simple_init_aux_jacks(priv, PREFIX);
654 static int asoc_simple_probe(struct platform_device *pdev)
656 struct asoc_simple_priv *priv;
657 struct device *dev = &pdev->dev;
658 struct device_node *np = dev->of_node;
659 struct snd_soc_card *card;
660 struct link_info *li;
663 /* Allocate the private data and the DAI link array */
664 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
668 card = simple_priv_to_card(priv);
669 card->owner = THIS_MODULE;
671 card->probe = simple_soc_probe;
672 card->driver_name = "simple-card";
674 li = devm_kzalloc(dev, sizeof(*li), GFP_KERNEL);
678 ret = simple_get_dais_count(priv, li);
685 ret = asoc_simple_init_priv(priv, li);
689 if (np && of_device_is_available(np)) {
691 ret = simple_parse_of(priv, li);
693 dev_err_probe(dev, ret, "parse error\n");
698 struct asoc_simple_card_info *cinfo;
699 struct snd_soc_dai_link_component *cpus;
700 struct snd_soc_dai_link_component *codecs;
701 struct snd_soc_dai_link_component *platform;
702 struct snd_soc_dai_link *dai_link = priv->dai_link;
703 struct simple_dai_props *dai_props = priv->dai_props;
705 cinfo = dev->platform_data;
707 dev_err(dev, "no info for asoc-simple-card\n");
712 !cinfo->codec_dai.name ||
715 !cinfo->cpu_dai.name) {
716 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
720 cpus = dai_link->cpus;
721 cpus->dai_name = cinfo->cpu_dai.name;
723 codecs = dai_link->codecs;
724 codecs->name = cinfo->codec;
725 codecs->dai_name = cinfo->codec_dai.name;
727 platform = dai_link->platforms;
728 platform->name = cinfo->platform;
730 card->name = (cinfo->card) ? cinfo->card : cinfo->name;
731 dai_link->name = cinfo->name;
732 dai_link->stream_name = cinfo->name;
733 dai_link->dai_fmt = cinfo->daifmt;
734 dai_link->init = asoc_simple_dai_init;
735 memcpy(dai_props->cpu_dai, &cinfo->cpu_dai,
736 sizeof(*dai_props->cpu_dai));
737 memcpy(dai_props->codec_dai, &cinfo->codec_dai,
738 sizeof(*dai_props->codec_dai));
741 snd_soc_card_set_drvdata(card, priv);
743 asoc_simple_debug_info(priv);
745 ret = devm_snd_soc_register_card(dev, card);
752 asoc_simple_clean_reference(card);
757 static const struct of_device_id simple_of_match[] = {
758 { .compatible = "simple-audio-card", },
759 { .compatible = "simple-scu-audio-card",
760 .data = (void *)DPCM_SELECTABLE },
763 MODULE_DEVICE_TABLE(of, simple_of_match);
765 static struct platform_driver asoc_simple_card = {
767 .name = "asoc-simple-card",
768 .pm = &snd_soc_pm_ops,
769 .of_match_table = simple_of_match,
771 .probe = asoc_simple_probe,
772 .remove = asoc_simple_remove,
775 module_platform_driver(asoc_simple_card);
777 MODULE_ALIAS("platform:asoc-simple-card");
778 MODULE_LICENSE("GPL v2");
779 MODULE_DESCRIPTION("ASoC Simple Sound Card");