1 // SPDX-License-Identifier: GPL-2.0
3 // ASoC audio graph sound card support
5 // Copyright (C) 2016 Renesas Solutions Corp.
8 // based on ${LINUX}/sound/soc/generic/simple-card.c
10 #include <linux/clk.h>
11 #include <linux/device.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/module.h>
15 #include <linux/of_graph.h>
16 #include <linux/platform_device.h>
17 #include <linux/string.h>
18 #include <sound/graph_card.h>
20 #define DPCM_SELECTABLE 1
22 static int graph_outdrv_event(struct snd_soc_dapm_widget *w,
23 struct snd_kcontrol *kcontrol,
26 struct snd_soc_dapm_context *dapm = w->dapm;
27 struct simple_util_priv *priv = snd_soc_card_get_drvdata(dapm->card);
30 case SND_SOC_DAPM_POST_PMU:
31 gpiod_set_value_cansleep(priv->pa_gpio, 1);
33 case SND_SOC_DAPM_PRE_PMD:
34 gpiod_set_value_cansleep(priv->pa_gpio, 0);
43 static const struct snd_soc_dapm_widget graph_dapm_widgets[] = {
44 SND_SOC_DAPM_OUT_DRV_E("Amplifier", SND_SOC_NOPM,
45 0, 0, NULL, 0, graph_outdrv_event,
46 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
49 static const struct snd_soc_ops graph_ops = {
50 .startup = simple_util_startup,
51 .shutdown = simple_util_shutdown,
52 .hw_params = simple_util_hw_params,
55 static bool soc_component_is_pcm(struct snd_soc_dai_link_component *dlc)
57 struct snd_soc_dai *dai = snd_soc_find_dai_with_mutex(dlc);
59 if (dai && (dai->component->driver->pcm_construct ||
60 (dai->driver->ops && dai->driver->ops->pcm_new)))
66 static void graph_parse_convert(struct device *dev,
67 struct device_node *ep,
68 struct simple_util_data *adata)
70 struct device_node *top = dev->of_node;
71 struct device_node *port = of_get_parent(ep);
72 struct device_node *ports = of_get_parent(port);
73 struct device_node *node = of_graph_get_port_parent(ep);
75 simple_util_parse_convert(top, NULL, adata);
76 if (of_node_name_eq(ports, "ports"))
77 simple_util_parse_convert(ports, NULL, adata);
78 simple_util_parse_convert(port, NULL, adata);
79 simple_util_parse_convert(ep, NULL, adata);
86 static void graph_parse_mclk_fs(struct device_node *top,
87 struct device_node *ep,
88 struct simple_dai_props *props)
90 struct device_node *port = of_get_parent(ep);
91 struct device_node *ports = of_get_parent(port);
93 of_property_read_u32(top, "mclk-fs", &props->mclk_fs);
94 if (of_node_name_eq(ports, "ports"))
95 of_property_read_u32(ports, "mclk-fs", &props->mclk_fs);
96 of_property_read_u32(port, "mclk-fs", &props->mclk_fs);
97 of_property_read_u32(ep, "mclk-fs", &props->mclk_fs);
103 static int graph_parse_node(struct simple_util_priv *priv,
104 struct device_node *ep,
105 struct link_info *li,
108 struct device *dev = simple_priv_to_dev(priv);
109 struct device_node *top = dev->of_node;
110 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
111 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
112 struct snd_soc_dai_link_component *dlc;
113 struct simple_util_dai *dai;
117 dlc = snd_soc_link_to_cpu(dai_link, 0);
118 dai = simple_props_to_dai_cpu(dai_props, 0);
120 dlc = snd_soc_link_to_codec(dai_link, 0);
121 dai = simple_props_to_dai_codec(dai_props, 0);
124 graph_parse_mclk_fs(top, ep, dai_props);
126 ret = graph_util_parse_dai(dev, ep, dlc, cpu);
130 ret = simple_util_parse_tdm(ep, dai);
134 ret = simple_util_parse_clk(dev, ep, dai, dlc);
141 static int graph_link_init(struct simple_util_priv *priv,
142 struct device_node *cpu_ep,
143 struct device_node *codec_ep,
144 struct link_info *li,
147 struct device *dev = simple_priv_to_dev(priv);
148 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
151 ret = simple_util_parse_daifmt(dev, cpu_ep, codec_ep,
152 NULL, &dai_link->dai_fmt);
156 dai_link->init = simple_util_dai_init;
157 dai_link->ops = &graph_ops;
159 dai_link->ops = priv->ops;
161 return simple_util_set_dailink_name(dev, dai_link, name);
164 static int graph_dai_link_of_dpcm(struct simple_util_priv *priv,
165 struct device_node *cpu_ep,
166 struct device_node *codec_ep,
167 struct link_info *li)
169 struct device *dev = simple_priv_to_dev(priv);
170 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
171 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
172 struct device_node *top = dev->of_node;
173 struct device_node *ep = li->cpu ? cpu_ep : codec_ep;
177 dev_dbg(dev, "link_of DPCM (%pOF)\n", ep);
180 struct snd_soc_card *card = simple_priv_to_card(priv);
181 struct snd_soc_dai_link_component *cpus = snd_soc_link_to_cpu(dai_link, 0);
182 struct snd_soc_dai_link_component *platforms = snd_soc_link_to_platform(dai_link, 0);
183 int is_single_links = 0;
188 dai_link->dynamic = 1;
189 dai_link->dpcm_merged_format = 1;
191 ret = graph_parse_node(priv, cpu_ep, li, &is_single_links);
195 snprintf(dai_name, sizeof(dai_name),
196 "fe.%pOFP.%s", cpus->of_node, cpus->dai_name);
198 * In BE<->BE connections it is not required to create
199 * PCM devices at CPU end of the dai link and thus 'no_pcm'
200 * flag needs to be set. It is useful when there are many
201 * BE components and some of these have to be connected to
202 * form a valid audio path.
204 * For example: FE <-> BE1 <-> BE2 <-> ... <-> BEn where
205 * there are 'n' BE components in the path.
207 if (card->component_chaining && !soc_component_is_pcm(cpus)) {
208 dai_link->no_pcm = 1;
209 dai_link->be_hw_params_fixup = simple_util_be_hw_params_fixup;
212 simple_util_canonicalize_cpu(cpus, is_single_links);
213 simple_util_canonicalize_platform(platforms, cpus);
215 struct snd_soc_codec_conf *cconf = simple_props_to_codec_conf(dai_props, 0);
216 struct snd_soc_dai_link_component *codecs = snd_soc_link_to_codec(dai_link, 0);
217 struct device_node *port;
218 struct device_node *ports;
223 dai_link->no_pcm = 1;
224 dai_link->be_hw_params_fixup = simple_util_be_hw_params_fixup;
226 ret = graph_parse_node(priv, codec_ep, li, NULL);
230 snprintf(dai_name, sizeof(dai_name),
231 "be.%pOFP.%s", codecs->of_node, codecs->dai_name);
233 /* check "prefix" from top node */
234 port = of_get_parent(ep);
235 ports = of_get_parent(port);
236 snd_soc_of_parse_node_prefix(top, cconf, codecs->of_node,
238 if (of_node_name_eq(ports, "ports"))
239 snd_soc_of_parse_node_prefix(ports, cconf, codecs->of_node, "prefix");
240 snd_soc_of_parse_node_prefix(port, cconf, codecs->of_node,
247 graph_parse_convert(dev, ep, &dai_props->adata);
249 snd_soc_dai_link_set_capabilities(dai_link);
251 ret = graph_link_init(priv, cpu_ep, codec_ep, li, dai_name);
258 static int graph_dai_link_of(struct simple_util_priv *priv,
259 struct device_node *cpu_ep,
260 struct device_node *codec_ep,
261 struct link_info *li)
263 struct device *dev = simple_priv_to_dev(priv);
264 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
265 struct snd_soc_dai_link_component *cpus = snd_soc_link_to_cpu(dai_link, 0);
266 struct snd_soc_dai_link_component *codecs = snd_soc_link_to_codec(dai_link, 0);
267 struct snd_soc_dai_link_component *platforms = snd_soc_link_to_platform(dai_link, 0);
269 int ret, is_single_links = 0;
271 dev_dbg(dev, "link_of (%pOF)\n", cpu_ep);
273 ret = graph_parse_node(priv, cpu_ep, li, &is_single_links);
277 ret = graph_parse_node(priv, codec_ep, li, NULL);
281 snprintf(dai_name, sizeof(dai_name),
282 "%s-%s", cpus->dai_name, codecs->dai_name);
284 simple_util_canonicalize_cpu(cpus, is_single_links);
285 simple_util_canonicalize_platform(platforms, cpus);
287 ret = graph_link_init(priv, cpu_ep, codec_ep, li, dai_name);
296 static inline bool parse_as_dpcm_link(struct simple_util_priv *priv,
297 struct device_node *codec_port,
298 struct simple_util_data *adata)
300 if (priv->force_dpcm)
303 if (!priv->dpcm_selectable)
308 * if Codec port has many endpoints,
309 * or has convert-xxx property
311 if ((of_get_child_count(codec_port) > 1) ||
312 simple_util_is_convert_required(adata))
318 static int __graph_for_each_link(struct simple_util_priv *priv,
319 struct link_info *li,
320 int (*func_noml)(struct simple_util_priv *priv,
321 struct device_node *cpu_ep,
322 struct device_node *codec_ep,
323 struct link_info *li),
324 int (*func_dpcm)(struct simple_util_priv *priv,
325 struct device_node *cpu_ep,
326 struct device_node *codec_ep,
327 struct link_info *li))
329 struct of_phandle_iterator it;
330 struct device *dev = simple_priv_to_dev(priv);
331 struct device_node *node = dev->of_node;
332 struct device_node *cpu_port;
333 struct device_node *cpu_ep;
334 struct device_node *codec_ep;
335 struct device_node *codec_port;
336 struct device_node *codec_port_old = NULL;
337 struct simple_util_data adata;
340 /* loop for all listed CPU port */
341 of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
345 /* loop for all CPU endpoint */
347 cpu_ep = of_get_next_child(cpu_port, cpu_ep);
352 codec_ep = of_graph_get_remote_endpoint(cpu_ep);
353 codec_port = of_get_parent(codec_ep);
355 /* get convert-xxx property */
356 memset(&adata, 0, sizeof(adata));
357 graph_parse_convert(dev, codec_ep, &adata);
358 graph_parse_convert(dev, cpu_ep, &adata);
360 /* check if link requires DPCM parsing */
361 if (parse_as_dpcm_link(priv, codec_port, &adata)) {
363 * Codec endpoint can be NULL for pluggable audio HW.
364 * Platform DT can populate the Codec endpoint depending on the
367 /* Do it all CPU endpoint, and 1st Codec endpoint */
369 ((codec_port_old != codec_port) && codec_ep))
370 ret = func_dpcm(priv, cpu_ep, codec_ep, li);
371 /* else normal sound */
374 ret = func_noml(priv, cpu_ep, codec_ep, li);
377 of_node_put(codec_ep);
378 of_node_put(codec_port);
385 codec_port_old = codec_port;
392 static int graph_for_each_link(struct simple_util_priv *priv,
393 struct link_info *li,
394 int (*func_noml)(struct simple_util_priv *priv,
395 struct device_node *cpu_ep,
396 struct device_node *codec_ep,
397 struct link_info *li),
398 int (*func_dpcm)(struct simple_util_priv *priv,
399 struct device_node *cpu_ep,
400 struct device_node *codec_ep,
401 struct link_info *li))
405 * Detect all CPU first, and Detect all Codec 2nd.
407 * In Normal sound case, all DAIs are detected
410 * In DPCM sound case,
411 * all CPUs are detected as "CPU-dummy", and
412 * all Codecs are detected as "dummy-Codec".
413 * To avoid random sub-device numbering,
414 * detect "dummy-Codec" in last;
416 for (li->cpu = 1; li->cpu >= 0; li->cpu--) {
417 ret = __graph_for_each_link(priv, li, func_noml, func_dpcm);
425 static int graph_count_noml(struct simple_util_priv *priv,
426 struct device_node *cpu_ep,
427 struct device_node *codec_ep,
428 struct link_info *li)
430 struct device *dev = simple_priv_to_dev(priv);
432 if (li->link >= SNDRV_MAX_LINKS) {
433 dev_err(dev, "too many links\n");
438 * DON'T REMOVE platforms
440 * simple-card.c :: simple_count_noml()
442 li->num[li->link].cpus = 1;
443 li->num[li->link].platforms = 1;
445 li->num[li->link].codecs = 1;
447 li->link += 1; /* 1xCPU-Codec */
449 dev_dbg(dev, "Count As Normal\n");
454 static int graph_count_dpcm(struct simple_util_priv *priv,
455 struct device_node *cpu_ep,
456 struct device_node *codec_ep,
457 struct link_info *li)
459 struct device *dev = simple_priv_to_dev(priv);
461 if (li->link >= SNDRV_MAX_LINKS) {
462 dev_err(dev, "too many links\n");
468 * DON'T REMOVE platforms
470 * simple-card.c :: simple_count_noml()
472 li->num[li->link].cpus = 1;
473 li->num[li->link].platforms = 1;
475 li->link++; /* 1xCPU-dummy */
477 li->num[li->link].codecs = 1;
479 li->link++; /* 1xdummy-Codec */
482 dev_dbg(dev, "Count As DPCM\n");
487 static int graph_get_dais_count(struct simple_util_priv *priv,
488 struct link_info *li)
491 * link_num : number of links.
492 * CPU-Codec / CPU-dummy / dummy-Codec
493 * dais_num : number of DAIs
494 * ccnf_num : number of codec_conf
495 * same number for "dummy-Codec"
498 * CPU0 --- Codec0 link : 5
499 * CPU1 --- Codec1 dais : 7
503 * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec
504 * => 7 DAIs = 4xCPU + 3xCodec
505 * => 1 ccnf = 1xdummy-Codec
508 * CPU0 --- Codec0 link : 5
509 * CPU1 --- Codec1 dais : 6
513 * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec
514 * => 6 DAIs = 4xCPU + 2xCodec
515 * => 1 ccnf = 1xdummy-Codec
518 * CPU0 --- Codec0 link : 6
520 * CPU2 --- Codec1 ccnf : 2
523 * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec
524 * => 6 DAIs = 4xCPU + 2xCodec
525 * => 2 ccnf = 2xdummy-Codec
528 * CPU0 --- Codec0 (convert-rate) link : 3
529 * CPU1 --- Codec1 dais : 4
532 * => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec
533 * => 4 DAIs = 2xCPU + 2xCodec
534 * => 1 ccnf = 1xdummy-Codec
536 return graph_for_each_link(priv, li,
541 int audio_graph_parse_of(struct simple_util_priv *priv, struct device *dev)
543 struct snd_soc_card *card = simple_priv_to_card(priv);
544 struct link_info *li;
547 li = devm_kzalloc(dev, sizeof(*li), GFP_KERNEL);
551 card->owner = THIS_MODULE;
554 ret = graph_get_dais_count(priv, li);
561 ret = simple_util_init_priv(priv, li);
565 priv->pa_gpio = devm_gpiod_get_optional(dev, "pa", GPIOD_OUT_LOW);
566 if (IS_ERR(priv->pa_gpio)) {
567 ret = PTR_ERR(priv->pa_gpio);
568 dev_err(dev, "failed to get amplifier gpio: %d\n", ret);
572 ret = simple_util_parse_widgets(card, NULL);
576 ret = simple_util_parse_routing(card, NULL);
580 memset(li, 0, sizeof(*li));
581 ret = graph_for_each_link(priv, li,
583 graph_dai_link_of_dpcm);
587 ret = simple_util_parse_card_name(card, NULL);
591 snd_soc_card_set_drvdata(card, priv);
593 simple_util_debug_info(priv);
595 ret = devm_snd_soc_register_card(dev, card);
603 simple_util_clean_reference(card);
605 return dev_err_probe(dev, ret, "parse error\n");
607 EXPORT_SYMBOL_GPL(audio_graph_parse_of);
609 static int graph_probe(struct platform_device *pdev)
611 struct simple_util_priv *priv;
612 struct device *dev = &pdev->dev;
613 struct snd_soc_card *card;
615 /* Allocate the private data and the DAI link array */
616 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
620 card = simple_priv_to_card(priv);
621 card->dapm_widgets = graph_dapm_widgets;
622 card->num_dapm_widgets = ARRAY_SIZE(graph_dapm_widgets);
623 card->probe = graph_util_card_probe;
625 if (of_device_get_match_data(dev))
626 priv->dpcm_selectable = 1;
628 return audio_graph_parse_of(priv, dev);
631 static const struct of_device_id graph_of_match[] = {
632 { .compatible = "audio-graph-card", },
633 { .compatible = "audio-graph-scu-card",
634 .data = (void *)DPCM_SELECTABLE },
637 MODULE_DEVICE_TABLE(of, graph_of_match);
639 static struct platform_driver graph_card = {
641 .name = "asoc-audio-graph-card",
642 .pm = &snd_soc_pm_ops,
643 .of_match_table = graph_of_match,
645 .probe = graph_probe,
646 .remove_new = simple_util_remove,
648 module_platform_driver(graph_card);
650 MODULE_ALIAS("platform:asoc-audio-graph-card");
651 MODULE_LICENSE("GPL v2");
652 MODULE_DESCRIPTION("ASoC Audio Graph Sound Card");