1 // SPDX-License-Identifier: GPL-2.0
3 // ASoC audio graph sound card support
5 // Copyright (C) 2016 Renesas Solutions Corp.
6 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
8 // based on ${LINUX}/sound/soc/generic/simple-card.c
10 #include <linux/clk.h>
11 #include <linux/device.h>
12 #include <linux/gpio.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/of_gpio.h>
18 #include <linux/of_graph.h>
19 #include <linux/platform_device.h>
20 #include <linux/string.h>
21 #include <sound/graph_card.h>
23 #define DPCM_SELECTABLE 1
25 #define PREFIX "audio-graph-card,"
27 static int graph_outdrv_event(struct snd_soc_dapm_widget *w,
28 struct snd_kcontrol *kcontrol,
31 struct snd_soc_dapm_context *dapm = w->dapm;
32 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(dapm->card);
35 case SND_SOC_DAPM_POST_PMU:
36 gpiod_set_value_cansleep(priv->pa_gpio, 1);
38 case SND_SOC_DAPM_PRE_PMD:
39 gpiod_set_value_cansleep(priv->pa_gpio, 0);
48 static const struct snd_soc_dapm_widget graph_dapm_widgets[] = {
49 SND_SOC_DAPM_OUT_DRV_E("Amplifier", SND_SOC_NOPM,
50 0, 0, NULL, 0, graph_outdrv_event,
51 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
54 static const struct snd_soc_ops graph_ops = {
55 .startup = asoc_simple_startup,
56 .shutdown = asoc_simple_shutdown,
57 .hw_params = asoc_simple_hw_params,
60 static int graph_get_dai_id(struct device_node *ep)
62 struct device_node *node;
63 struct device_node *endpoint;
64 struct of_endpoint info;
69 /* use driver specified DAI ID if exist */
70 ret = snd_soc_get_dai_id(ep);
74 /* use endpoint/port reg if exist */
75 ret = of_graph_parse_endpoint(ep, &info);
78 * Because it will count port/endpoint if it doesn't have "reg".
79 * But, we can't judge whether it has "no reg", or "reg = <0>"
80 * only of_graph_parse_endpoint().
81 * We need to check "reg" property
83 if (of_get_property(ep, "reg", NULL))
86 node = of_get_parent(ep);
87 reg = of_get_property(node, "reg", NULL);
92 node = of_graph_get_port_parent(ep);
95 * Non HDMI sound case, counting port/endpoint on its DT
96 * is enough. Let's count it.
100 for_each_endpoint_of_node(node, endpoint) {
114 static bool soc_component_is_pcm(struct snd_soc_dai_link_component *dlc)
116 struct snd_soc_dai *dai = snd_soc_find_dai_with_mutex(dlc);
118 if (dai && (dai->component->driver->pcm_construct ||
119 dai->driver->pcm_new))
125 static int asoc_simple_parse_dai(struct device_node *ep,
126 struct snd_soc_dai_link_component *dlc,
129 struct device_node *node;
130 struct of_phandle_args args;
136 node = of_graph_get_port_parent(ep);
140 args.args[0] = graph_get_dai_id(ep);
141 args.args_count = (of_graph_get_endpoint_count(node) > 1);
146 * Here, dlc->dai_name is pointer to CPU/Codec DAI name.
147 * If user unbinded CPU or Codec driver, but not for Sound Card,
148 * dlc->dai_name is keeping unbinded CPU or Codec
151 * If user re-bind CPU or Codec driver again, ALSA SoC will try
152 * to rebind Card via snd_soc_try_rebind_card(), but because of
153 * above reason, it might can't bind Sound Card.
154 * Because Sound Card is pointing to released dai_name pointer.
156 * To avoid this rebind Card issue,
157 * 1) It needs to alloc memory to keep dai_name eventhough
158 * CPU or Codec driver was unbinded, or
159 * 2) user need to rebind Sound Card everytime
160 * if he unbinded CPU or Codec.
162 ret = snd_soc_get_dai_name(&args, &dlc->dai_name);
169 *is_single_link = of_graph_get_endpoint_count(node) == 1;
174 static void graph_parse_convert(struct device *dev,
175 struct device_node *ep,
176 struct asoc_simple_data *adata)
178 struct device_node *top = dev->of_node;
179 struct device_node *port = of_get_parent(ep);
180 struct device_node *ports = of_get_parent(port);
181 struct device_node *node = of_graph_get_port_parent(ep);
183 asoc_simple_parse_convert(top, NULL, adata);
184 asoc_simple_parse_convert(node, PREFIX, adata);
185 asoc_simple_parse_convert(ports, NULL, adata);
186 asoc_simple_parse_convert(port, NULL, adata);
187 asoc_simple_parse_convert(ep, NULL, adata);
194 static void graph_parse_mclk_fs(struct device_node *top,
195 struct device_node *ep,
196 struct simple_dai_props *props)
198 struct device_node *port = of_get_parent(ep);
199 struct device_node *ports = of_get_parent(port);
200 struct device_node *node = of_graph_get_port_parent(ep);
202 of_property_read_u32(top, "mclk-fs", &props->mclk_fs);
203 of_property_read_u32(ports, "mclk-fs", &props->mclk_fs);
204 of_property_read_u32(port, "mclk-fs", &props->mclk_fs);
205 of_property_read_u32(ep, "mclk-fs", &props->mclk_fs);
212 static int graph_dai_link_of_dpcm(struct asoc_simple_priv *priv,
213 struct device_node *cpu_ep,
214 struct device_node *codec_ep,
215 struct link_info *li)
217 struct device *dev = simple_priv_to_dev(priv);
218 struct snd_soc_card *card = simple_priv_to_card(priv);
219 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
220 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
221 struct device_node *top = dev->of_node;
222 struct device_node *ep = li->cpu ? cpu_ep : codec_ep;
223 struct device_node *port;
224 struct device_node *ports;
225 struct device_node *node;
226 struct asoc_simple_dai *dai;
227 struct snd_soc_dai_link_component *cpus = asoc_link_to_cpu(dai_link, 0);
228 struct snd_soc_dai_link_component *codecs = asoc_link_to_codec(dai_link, 0);
229 struct snd_soc_dai_link_component *platforms = asoc_link_to_platform(dai_link, 0);
232 port = of_get_parent(ep);
233 ports = of_get_parent(port);
234 node = of_graph_get_port_parent(ep);
238 dev_dbg(dev, "link_of DPCM (%pOF)\n", ep);
241 int is_single_links = 0;
246 dai_link->dynamic = 1;
247 dai_link->dpcm_merged_format = 1;
249 dai = simple_props_to_dai_cpu(dai_props, 0);
251 ret = asoc_simple_parse_dai(ep, cpus, &is_single_links);
255 ret = asoc_simple_parse_clk(dev, ep, dai, cpus);
259 ret = asoc_simple_set_dailink_name(dev, dai_link,
267 * In BE<->BE connections it is not required to create
268 * PCM devices at CPU end of the dai link and thus 'no_pcm'
269 * flag needs to be set. It is useful when there are many
270 * BE components and some of these have to be connected to
271 * form a valid audio path.
273 * For example: FE <-> BE1 <-> BE2 <-> ... <-> BEn where
274 * there are 'n' BE components in the path.
276 if (card->component_chaining && !soc_component_is_pcm(cpus))
277 dai_link->no_pcm = 1;
279 /* card->num_links includes Codec */
280 asoc_simple_canonicalize_cpu(cpus, is_single_links);
281 asoc_simple_canonicalize_platform(platforms, cpus);
283 struct snd_soc_codec_conf *cconf;
288 dai_link->no_pcm = 1;
289 dai_link->be_hw_params_fixup = asoc_simple_be_hw_params_fixup;
291 dai = simple_props_to_dai_codec(dai_props, 0);
292 cconf = simple_props_to_codec_conf(dai_props, 0);
294 ret = asoc_simple_parse_dai(ep, codecs, NULL);
298 ret = asoc_simple_parse_clk(dev, ep, dai, codecs);
302 ret = asoc_simple_set_dailink_name(dev, dai_link,
309 /* check "prefix" from top node */
310 snd_soc_of_parse_node_prefix(top, cconf, codecs->of_node,
312 snd_soc_of_parse_node_prefix(node, cconf, codecs->of_node,
314 snd_soc_of_parse_node_prefix(ports, cconf, codecs->of_node,
316 snd_soc_of_parse_node_prefix(port, cconf, codecs->of_node,
320 graph_parse_convert(dev, ep, &dai_props->adata);
321 graph_parse_mclk_fs(top, ep, dai_props);
323 ret = asoc_simple_parse_tdm(ep, dai);
327 ret = asoc_simple_parse_daifmt(dev, cpu_ep, codec_ep,
328 NULL, &dai_link->dai_fmt);
332 snd_soc_dai_link_set_capabilities(dai_link);
334 dai_link->ops = &graph_ops;
336 /* Use custom snd_soc_ops callbacks if available */
338 dai_link->ops = priv->ops;
340 dai_link->init = asoc_simple_dai_init;
349 static int graph_dai_link_of(struct asoc_simple_priv *priv,
350 struct device_node *cpu_ep,
351 struct device_node *codec_ep,
352 struct link_info *li)
354 struct device *dev = simple_priv_to_dev(priv);
355 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
356 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
357 struct device_node *top = dev->of_node;
358 struct asoc_simple_dai *cpu_dai = simple_props_to_dai_cpu(dai_props, 0);
359 struct asoc_simple_dai *codec_dai = simple_props_to_dai_codec(dai_props, 0);
360 struct snd_soc_dai_link_component *cpus = asoc_link_to_cpu(dai_link, 0);
361 struct snd_soc_dai_link_component *codecs = asoc_link_to_codec(dai_link, 0);
362 struct snd_soc_dai_link_component *platforms = asoc_link_to_platform(dai_link, 0);
363 int ret, single_cpu = 0;
365 dev_dbg(dev, "link_of (%pOF)\n", cpu_ep);
369 /* Factor to mclk, used in hw_params() */
370 graph_parse_mclk_fs(top, cpu_ep, dai_props);
371 graph_parse_mclk_fs(top, codec_ep, dai_props);
373 ret = asoc_simple_parse_daifmt(dev, cpu_ep, codec_ep,
374 NULL, &dai_link->dai_fmt);
378 ret = asoc_simple_parse_dai(cpu_ep, cpus, &single_cpu);
382 ret = asoc_simple_parse_dai(codec_ep, codecs, NULL);
386 ret = asoc_simple_parse_tdm(cpu_ep, cpu_dai);
390 ret = asoc_simple_parse_tdm(codec_ep, codec_dai);
394 ret = asoc_simple_parse_clk(dev, cpu_ep, cpu_dai, cpus);
398 ret = asoc_simple_parse_clk(dev, codec_ep, codec_dai, codecs);
402 ret = asoc_simple_set_dailink_name(dev, dai_link,
409 dai_link->ops = &graph_ops;
410 dai_link->init = asoc_simple_dai_init;
412 asoc_simple_canonicalize_cpu(cpus, single_cpu);
413 asoc_simple_canonicalize_platform(platforms, cpus);
418 static inline bool parse_as_dpcm_link(struct asoc_simple_priv *priv,
419 struct device_node *codec_port,
420 struct asoc_simple_data *adata)
422 if (priv->force_dpcm)
425 if (!priv->dpcm_selectable)
430 * if Codec port has many endpoints,
431 * or has convert-xxx property
433 if ((of_get_child_count(codec_port) > 1) ||
434 (adata->convert_rate || adata->convert_channels))
440 static int __graph_for_each_link(struct asoc_simple_priv *priv,
441 struct link_info *li,
442 int (*func_noml)(struct asoc_simple_priv *priv,
443 struct device_node *cpu_ep,
444 struct device_node *codec_ep,
445 struct link_info *li),
446 int (*func_dpcm)(struct asoc_simple_priv *priv,
447 struct device_node *cpu_ep,
448 struct device_node *codec_ep,
449 struct link_info *li))
451 struct of_phandle_iterator it;
452 struct device *dev = simple_priv_to_dev(priv);
453 struct device_node *node = dev->of_node;
454 struct device_node *cpu_port;
455 struct device_node *cpu_ep;
456 struct device_node *codec_ep;
457 struct device_node *codec_port;
458 struct device_node *codec_port_old = NULL;
459 struct asoc_simple_data adata;
462 /* loop for all listed CPU port */
463 of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
467 /* loop for all CPU endpoint */
469 cpu_ep = of_get_next_child(cpu_port, cpu_ep);
474 codec_ep = of_graph_get_remote_endpoint(cpu_ep);
475 codec_port = of_get_parent(codec_ep);
477 /* get convert-xxx property */
478 memset(&adata, 0, sizeof(adata));
479 graph_parse_convert(dev, codec_ep, &adata);
480 graph_parse_convert(dev, cpu_ep, &adata);
482 /* check if link requires DPCM parsing */
483 if (parse_as_dpcm_link(priv, codec_port, &adata)) {
485 * Codec endpoint can be NULL for pluggable audio HW.
486 * Platform DT can populate the Codec endpoint depending on the
489 /* Do it all CPU endpoint, and 1st Codec endpoint */
491 ((codec_port_old != codec_port) && codec_ep))
492 ret = func_dpcm(priv, cpu_ep, codec_ep, li);
493 /* else normal sound */
496 ret = func_noml(priv, cpu_ep, codec_ep, li);
499 of_node_put(codec_ep);
500 of_node_put(codec_port);
505 codec_port_old = codec_port;
512 static int graph_for_each_link(struct asoc_simple_priv *priv,
513 struct link_info *li,
514 int (*func_noml)(struct asoc_simple_priv *priv,
515 struct device_node *cpu_ep,
516 struct device_node *codec_ep,
517 struct link_info *li),
518 int (*func_dpcm)(struct asoc_simple_priv *priv,
519 struct device_node *cpu_ep,
520 struct device_node *codec_ep,
521 struct link_info *li))
525 * Detect all CPU first, and Detect all Codec 2nd.
527 * In Normal sound case, all DAIs are detected
530 * In DPCM sound case,
531 * all CPUs are detected as "CPU-dummy", and
532 * all Codecs are detected as "dummy-Codec".
533 * To avoid random sub-device numbering,
534 * detect "dummy-Codec" in last;
536 for (li->cpu = 1; li->cpu >= 0; li->cpu--) {
537 ret = __graph_for_each_link(priv, li, func_noml, func_dpcm);
545 static int graph_get_dais_count(struct asoc_simple_priv *priv,
546 struct link_info *li);
548 int audio_graph_parse_of(struct asoc_simple_priv *priv, struct device *dev)
550 struct snd_soc_card *card = simple_priv_to_card(priv);
551 struct link_info *li;
554 li = devm_kzalloc(dev, sizeof(*li), GFP_KERNEL);
558 card->owner = THIS_MODULE;
561 ret = graph_get_dais_count(priv, li);
568 ret = asoc_simple_init_priv(priv, li);
572 priv->pa_gpio = devm_gpiod_get_optional(dev, "pa", GPIOD_OUT_LOW);
573 if (IS_ERR(priv->pa_gpio)) {
574 ret = PTR_ERR(priv->pa_gpio);
575 dev_err(dev, "failed to get amplifier gpio: %d\n", ret);
579 ret = asoc_simple_parse_widgets(card, NULL);
583 ret = asoc_simple_parse_routing(card, NULL);
587 memset(li, 0, sizeof(*li));
588 ret = graph_for_each_link(priv, li,
590 graph_dai_link_of_dpcm);
594 ret = asoc_simple_parse_card_name(card, NULL);
598 snd_soc_card_set_drvdata(card, priv);
600 asoc_simple_debug_info(priv);
602 ret = devm_snd_soc_register_card(dev, card);
610 asoc_simple_clean_reference(card);
612 if (ret != -EPROBE_DEFER)
613 dev_err(dev, "parse error %d\n", ret);
617 EXPORT_SYMBOL_GPL(audio_graph_parse_of);
619 static int graph_count_noml(struct asoc_simple_priv *priv,
620 struct device_node *cpu_ep,
621 struct device_node *codec_ep,
622 struct link_info *li)
624 struct device *dev = simple_priv_to_dev(priv);
626 if (li->link >= SNDRV_MAX_LINKS) {
627 dev_err(dev, "too many links\n");
631 li->num[li->link].cpus = 1;
632 li->num[li->link].codecs = 1;
633 li->num[li->link].platforms = 1;
635 li->link += 1; /* 1xCPU-Codec */
637 dev_dbg(dev, "Count As Normal\n");
642 static int graph_count_dpcm(struct asoc_simple_priv *priv,
643 struct device_node *cpu_ep,
644 struct device_node *codec_ep,
645 struct link_info *li)
647 struct device *dev = simple_priv_to_dev(priv);
649 if (li->link >= SNDRV_MAX_LINKS) {
650 dev_err(dev, "too many links\n");
655 li->num[li->link].cpus = 1;
656 li->num[li->link].platforms = 1;
658 li->link++; /* 1xCPU-dummy */
660 li->num[li->link].codecs = 1;
662 li->link++; /* 1xdummy-Codec */
665 dev_dbg(dev, "Count As DPCM\n");
670 static int graph_get_dais_count(struct asoc_simple_priv *priv,
671 struct link_info *li)
674 * link_num : number of links.
675 * CPU-Codec / CPU-dummy / dummy-Codec
676 * dais_num : number of DAIs
677 * ccnf_num : number of codec_conf
678 * same number for "dummy-Codec"
681 * CPU0 --- Codec0 link : 5
682 * CPU1 --- Codec1 dais : 7
686 * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec
687 * => 7 DAIs = 4xCPU + 3xCodec
688 * => 1 ccnf = 1xdummy-Codec
691 * CPU0 --- Codec0 link : 5
692 * CPU1 --- Codec1 dais : 6
696 * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec
697 * => 6 DAIs = 4xCPU + 2xCodec
698 * => 1 ccnf = 1xdummy-Codec
701 * CPU0 --- Codec0 link : 6
703 * CPU2 --- Codec1 ccnf : 2
706 * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec
707 * => 6 DAIs = 4xCPU + 2xCodec
708 * => 2 ccnf = 2xdummy-Codec
711 * CPU0 --- Codec0 (convert-rate) link : 3
712 * CPU1 --- Codec1 dais : 4
715 * => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec
716 * => 4 DAIs = 2xCPU + 2xCodec
717 * => 1 ccnf = 1xdummy-Codec
719 return graph_for_each_link(priv, li,
724 static int graph_probe(struct platform_device *pdev)
726 struct asoc_simple_priv *priv;
727 struct device *dev = &pdev->dev;
728 struct snd_soc_card *card;
730 /* Allocate the private data and the DAI link array */
731 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
735 card = simple_priv_to_card(priv);
736 card->dapm_widgets = graph_dapm_widgets;
737 card->num_dapm_widgets = ARRAY_SIZE(graph_dapm_widgets);
738 card->probe = asoc_graph_card_probe;
740 if (of_device_get_match_data(dev))
741 priv->dpcm_selectable = 1;
743 return audio_graph_parse_of(priv, dev);
746 int audio_graph_remove(struct platform_device *pdev)
748 struct snd_soc_card *card = platform_get_drvdata(pdev);
750 return asoc_simple_clean_reference(card);
752 EXPORT_SYMBOL_GPL(audio_graph_remove);
754 static const struct of_device_id graph_of_match[] = {
755 { .compatible = "audio-graph-card", },
756 { .compatible = "audio-graph-scu-card",
757 .data = (void *)DPCM_SELECTABLE },
760 MODULE_DEVICE_TABLE(of, graph_of_match);
762 static struct platform_driver graph_card = {
764 .name = "asoc-audio-graph-card",
765 .pm = &snd_soc_pm_ops,
766 .of_match_table = graph_of_match,
768 .probe = graph_probe,
769 .remove = audio_graph_remove,
771 module_platform_driver(graph_card);
773 MODULE_ALIAS("platform:asoc-audio-graph-card");
774 MODULE_LICENSE("GPL v2");
775 MODULE_DESCRIPTION("ASoC Audio Graph Sound Card");
776 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");