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 static int graph_outdrv_event(struct snd_soc_dapm_widget *w,
26 struct snd_kcontrol *kcontrol,
29 struct snd_soc_dapm_context *dapm = w->dapm;
30 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(dapm->card);
33 case SND_SOC_DAPM_POST_PMU:
34 gpiod_set_value_cansleep(priv->pa_gpio, 1);
36 case SND_SOC_DAPM_PRE_PMD:
37 gpiod_set_value_cansleep(priv->pa_gpio, 0);
46 static const struct snd_soc_dapm_widget graph_dapm_widgets[] = {
47 SND_SOC_DAPM_OUT_DRV_E("Amplifier", SND_SOC_NOPM,
48 0, 0, NULL, 0, graph_outdrv_event,
49 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
52 static const struct snd_soc_ops graph_ops = {
53 .startup = asoc_simple_startup,
54 .shutdown = asoc_simple_shutdown,
55 .hw_params = asoc_simple_hw_params,
58 static bool soc_component_is_pcm(struct snd_soc_dai_link_component *dlc)
60 struct snd_soc_dai *dai = snd_soc_find_dai_with_mutex(dlc);
62 if (dai && (dai->component->driver->pcm_construct ||
63 (dai->driver->ops && dai->driver->ops->pcm_new)))
69 static void graph_parse_convert(struct device *dev,
70 struct device_node *ep,
71 struct asoc_simple_data *adata)
73 struct device_node *top = dev->of_node;
74 struct device_node *port = of_get_parent(ep);
75 struct device_node *ports = of_get_parent(port);
76 struct device_node *node = of_graph_get_port_parent(ep);
78 asoc_simple_parse_convert(top, NULL, adata);
79 if (of_node_name_eq(ports, "ports"))
80 asoc_simple_parse_convert(ports, NULL, adata);
81 asoc_simple_parse_convert(port, NULL, adata);
82 asoc_simple_parse_convert(ep, NULL, adata);
89 static void graph_parse_mclk_fs(struct device_node *top,
90 struct device_node *ep,
91 struct simple_dai_props *props)
93 struct device_node *port = of_get_parent(ep);
94 struct device_node *ports = of_get_parent(port);
96 of_property_read_u32(top, "mclk-fs", &props->mclk_fs);
97 if (of_node_name_eq(ports, "ports"))
98 of_property_read_u32(ports, "mclk-fs", &props->mclk_fs);
99 of_property_read_u32(port, "mclk-fs", &props->mclk_fs);
100 of_property_read_u32(ep, "mclk-fs", &props->mclk_fs);
106 static int graph_parse_node(struct asoc_simple_priv *priv,
107 struct device_node *ep,
108 struct link_info *li,
111 struct device *dev = simple_priv_to_dev(priv);
112 struct device_node *top = dev->of_node;
113 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
114 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
115 struct snd_soc_dai_link_component *dlc;
116 struct asoc_simple_dai *dai;
120 dlc = asoc_link_to_cpu(dai_link, 0);
121 dai = simple_props_to_dai_cpu(dai_props, 0);
123 dlc = asoc_link_to_codec(dai_link, 0);
124 dai = simple_props_to_dai_codec(dai_props, 0);
127 graph_parse_mclk_fs(top, ep, dai_props);
129 ret = asoc_graph_parse_dai(dev, ep, dlc, cpu);
133 ret = asoc_simple_parse_tdm(ep, dai);
137 ret = asoc_simple_parse_clk(dev, ep, dai, dlc);
144 static int graph_link_init(struct asoc_simple_priv *priv,
145 struct device_node *cpu_ep,
146 struct device_node *codec_ep,
147 struct link_info *li,
150 struct device *dev = simple_priv_to_dev(priv);
151 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
154 ret = asoc_simple_parse_daifmt(dev, cpu_ep, codec_ep,
155 NULL, &dai_link->dai_fmt);
159 dai_link->init = asoc_simple_dai_init;
160 dai_link->ops = &graph_ops;
162 dai_link->ops = priv->ops;
164 return asoc_simple_set_dailink_name(dev, dai_link, name);
167 static int graph_dai_link_of_dpcm(struct asoc_simple_priv *priv,
168 struct device_node *cpu_ep,
169 struct device_node *codec_ep,
170 struct link_info *li)
172 struct device *dev = simple_priv_to_dev(priv);
173 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
174 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
175 struct device_node *top = dev->of_node;
176 struct device_node *ep = li->cpu ? cpu_ep : codec_ep;
180 dev_dbg(dev, "link_of DPCM (%pOF)\n", ep);
183 struct snd_soc_card *card = simple_priv_to_card(priv);
184 struct snd_soc_dai_link_component *cpus = asoc_link_to_cpu(dai_link, 0);
185 struct snd_soc_dai_link_component *platforms = asoc_link_to_platform(dai_link, 0);
186 int is_single_links = 0;
191 dai_link->dynamic = 1;
192 dai_link->dpcm_merged_format = 1;
194 ret = graph_parse_node(priv, cpu_ep, li, &is_single_links);
198 snprintf(dai_name, sizeof(dai_name),
199 "fe.%pOFP.%s", cpus->of_node, cpus->dai_name);
201 * In BE<->BE connections it is not required to create
202 * PCM devices at CPU end of the dai link and thus 'no_pcm'
203 * flag needs to be set. It is useful when there are many
204 * BE components and some of these have to be connected to
205 * form a valid audio path.
207 * For example: FE <-> BE1 <-> BE2 <-> ... <-> BEn where
208 * there are 'n' BE components in the path.
210 if (card->component_chaining && !soc_component_is_pcm(cpus)) {
211 dai_link->no_pcm = 1;
212 dai_link->be_hw_params_fixup = asoc_simple_be_hw_params_fixup;
215 asoc_simple_canonicalize_cpu(cpus, is_single_links);
216 asoc_simple_canonicalize_platform(platforms, cpus);
218 struct snd_soc_codec_conf *cconf = simple_props_to_codec_conf(dai_props, 0);
219 struct snd_soc_dai_link_component *codecs = asoc_link_to_codec(dai_link, 0);
220 struct device_node *port;
221 struct device_node *ports;
226 dai_link->no_pcm = 1;
227 dai_link->be_hw_params_fixup = asoc_simple_be_hw_params_fixup;
229 ret = graph_parse_node(priv, codec_ep, li, NULL);
233 snprintf(dai_name, sizeof(dai_name),
234 "be.%pOFP.%s", codecs->of_node, codecs->dai_name);
236 /* check "prefix" from top node */
237 port = of_get_parent(ep);
238 ports = of_get_parent(port);
239 snd_soc_of_parse_node_prefix(top, cconf, codecs->of_node,
241 if (of_node_name_eq(ports, "ports"))
242 snd_soc_of_parse_node_prefix(ports, cconf, codecs->of_node, "prefix");
243 snd_soc_of_parse_node_prefix(port, cconf, codecs->of_node,
250 graph_parse_convert(dev, ep, &dai_props->adata);
252 snd_soc_dai_link_set_capabilities(dai_link);
254 ret = graph_link_init(priv, cpu_ep, codec_ep, li, dai_name);
261 static int graph_dai_link_of(struct asoc_simple_priv *priv,
262 struct device_node *cpu_ep,
263 struct device_node *codec_ep,
264 struct link_info *li)
266 struct device *dev = simple_priv_to_dev(priv);
267 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
268 struct snd_soc_dai_link_component *cpus = asoc_link_to_cpu(dai_link, 0);
269 struct snd_soc_dai_link_component *codecs = asoc_link_to_codec(dai_link, 0);
270 struct snd_soc_dai_link_component *platforms = asoc_link_to_platform(dai_link, 0);
272 int ret, is_single_links = 0;
274 dev_dbg(dev, "link_of (%pOF)\n", cpu_ep);
276 ret = graph_parse_node(priv, cpu_ep, li, &is_single_links);
280 ret = graph_parse_node(priv, codec_ep, li, NULL);
284 snprintf(dai_name, sizeof(dai_name),
285 "%s-%s", cpus->dai_name, codecs->dai_name);
287 asoc_simple_canonicalize_cpu(cpus, is_single_links);
288 asoc_simple_canonicalize_platform(platforms, cpus);
290 ret = graph_link_init(priv, cpu_ep, codec_ep, li, dai_name);
299 static inline bool parse_as_dpcm_link(struct asoc_simple_priv *priv,
300 struct device_node *codec_port,
301 struct asoc_simple_data *adata)
303 if (priv->force_dpcm)
306 if (!priv->dpcm_selectable)
311 * if Codec port has many endpoints,
312 * or has convert-xxx property
314 if ((of_get_child_count(codec_port) > 1) ||
315 asoc_simple_is_convert_required(adata))
321 static int __graph_for_each_link(struct asoc_simple_priv *priv,
322 struct link_info *li,
323 int (*func_noml)(struct asoc_simple_priv *priv,
324 struct device_node *cpu_ep,
325 struct device_node *codec_ep,
326 struct link_info *li),
327 int (*func_dpcm)(struct asoc_simple_priv *priv,
328 struct device_node *cpu_ep,
329 struct device_node *codec_ep,
330 struct link_info *li))
332 struct of_phandle_iterator it;
333 struct device *dev = simple_priv_to_dev(priv);
334 struct device_node *node = dev->of_node;
335 struct device_node *cpu_port;
336 struct device_node *cpu_ep;
337 struct device_node *codec_ep;
338 struct device_node *codec_port;
339 struct device_node *codec_port_old = NULL;
340 struct asoc_simple_data adata;
343 /* loop for all listed CPU port */
344 of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
348 /* loop for all CPU endpoint */
350 cpu_ep = of_get_next_child(cpu_port, cpu_ep);
355 codec_ep = of_graph_get_remote_endpoint(cpu_ep);
356 codec_port = of_get_parent(codec_ep);
358 /* get convert-xxx property */
359 memset(&adata, 0, sizeof(adata));
360 graph_parse_convert(dev, codec_ep, &adata);
361 graph_parse_convert(dev, cpu_ep, &adata);
363 /* check if link requires DPCM parsing */
364 if (parse_as_dpcm_link(priv, codec_port, &adata)) {
366 * Codec endpoint can be NULL for pluggable audio HW.
367 * Platform DT can populate the Codec endpoint depending on the
370 /* Do it all CPU endpoint, and 1st Codec endpoint */
372 ((codec_port_old != codec_port) && codec_ep))
373 ret = func_dpcm(priv, cpu_ep, codec_ep, li);
374 /* else normal sound */
377 ret = func_noml(priv, cpu_ep, codec_ep, li);
380 of_node_put(codec_ep);
381 of_node_put(codec_port);
388 codec_port_old = codec_port;
395 static int graph_for_each_link(struct asoc_simple_priv *priv,
396 struct link_info *li,
397 int (*func_noml)(struct asoc_simple_priv *priv,
398 struct device_node *cpu_ep,
399 struct device_node *codec_ep,
400 struct link_info *li),
401 int (*func_dpcm)(struct asoc_simple_priv *priv,
402 struct device_node *cpu_ep,
403 struct device_node *codec_ep,
404 struct link_info *li))
408 * Detect all CPU first, and Detect all Codec 2nd.
410 * In Normal sound case, all DAIs are detected
413 * In DPCM sound case,
414 * all CPUs are detected as "CPU-dummy", and
415 * all Codecs are detected as "dummy-Codec".
416 * To avoid random sub-device numbering,
417 * detect "dummy-Codec" in last;
419 for (li->cpu = 1; li->cpu >= 0; li->cpu--) {
420 ret = __graph_for_each_link(priv, li, func_noml, func_dpcm);
428 static int graph_count_noml(struct asoc_simple_priv *priv,
429 struct device_node *cpu_ep,
430 struct device_node *codec_ep,
431 struct link_info *li)
433 struct device *dev = simple_priv_to_dev(priv);
435 if (li->link >= SNDRV_MAX_LINKS) {
436 dev_err(dev, "too many links\n");
441 * DON'T REMOVE platforms
443 * simple-card.c :: simple_count_noml()
445 li->num[li->link].cpus = 1;
446 li->num[li->link].platforms = 1;
448 li->num[li->link].codecs = 1;
450 li->link += 1; /* 1xCPU-Codec */
452 dev_dbg(dev, "Count As Normal\n");
457 static int graph_count_dpcm(struct asoc_simple_priv *priv,
458 struct device_node *cpu_ep,
459 struct device_node *codec_ep,
460 struct link_info *li)
462 struct device *dev = simple_priv_to_dev(priv);
464 if (li->link >= SNDRV_MAX_LINKS) {
465 dev_err(dev, "too many links\n");
471 * DON'T REMOVE platforms
473 * simple-card.c :: simple_count_noml()
475 li->num[li->link].cpus = 1;
476 li->num[li->link].platforms = 1;
478 li->link++; /* 1xCPU-dummy */
480 li->num[li->link].codecs = 1;
482 li->link++; /* 1xdummy-Codec */
485 dev_dbg(dev, "Count As DPCM\n");
490 static int graph_get_dais_count(struct asoc_simple_priv *priv,
491 struct link_info *li)
494 * link_num : number of links.
495 * CPU-Codec / CPU-dummy / dummy-Codec
496 * dais_num : number of DAIs
497 * ccnf_num : number of codec_conf
498 * same number for "dummy-Codec"
501 * CPU0 --- Codec0 link : 5
502 * CPU1 --- Codec1 dais : 7
506 * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec
507 * => 7 DAIs = 4xCPU + 3xCodec
508 * => 1 ccnf = 1xdummy-Codec
511 * CPU0 --- Codec0 link : 5
512 * CPU1 --- Codec1 dais : 6
516 * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec
517 * => 6 DAIs = 4xCPU + 2xCodec
518 * => 1 ccnf = 1xdummy-Codec
521 * CPU0 --- Codec0 link : 6
523 * CPU2 --- Codec1 ccnf : 2
526 * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec
527 * => 6 DAIs = 4xCPU + 2xCodec
528 * => 2 ccnf = 2xdummy-Codec
531 * CPU0 --- Codec0 (convert-rate) link : 3
532 * CPU1 --- Codec1 dais : 4
535 * => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec
536 * => 4 DAIs = 2xCPU + 2xCodec
537 * => 1 ccnf = 1xdummy-Codec
539 return graph_for_each_link(priv, li,
544 int audio_graph_parse_of(struct asoc_simple_priv *priv, struct device *dev)
546 struct snd_soc_card *card = simple_priv_to_card(priv);
547 struct link_info *li;
550 li = devm_kzalloc(dev, sizeof(*li), GFP_KERNEL);
554 card->owner = THIS_MODULE;
557 ret = graph_get_dais_count(priv, li);
564 ret = asoc_simple_init_priv(priv, li);
568 priv->pa_gpio = devm_gpiod_get_optional(dev, "pa", GPIOD_OUT_LOW);
569 if (IS_ERR(priv->pa_gpio)) {
570 ret = PTR_ERR(priv->pa_gpio);
571 dev_err(dev, "failed to get amplifier gpio: %d\n", ret);
575 ret = asoc_simple_parse_widgets(card, NULL);
579 ret = asoc_simple_parse_routing(card, NULL);
583 memset(li, 0, sizeof(*li));
584 ret = graph_for_each_link(priv, li,
586 graph_dai_link_of_dpcm);
590 ret = asoc_simple_parse_card_name(card, NULL);
594 snd_soc_card_set_drvdata(card, priv);
596 asoc_simple_debug_info(priv);
598 ret = devm_snd_soc_register_card(dev, card);
606 asoc_simple_clean_reference(card);
608 return dev_err_probe(dev, ret, "parse error\n");
610 EXPORT_SYMBOL_GPL(audio_graph_parse_of);
612 static int graph_probe(struct platform_device *pdev)
614 struct asoc_simple_priv *priv;
615 struct device *dev = &pdev->dev;
616 struct snd_soc_card *card;
618 /* Allocate the private data and the DAI link array */
619 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
623 card = simple_priv_to_card(priv);
624 card->dapm_widgets = graph_dapm_widgets;
625 card->num_dapm_widgets = ARRAY_SIZE(graph_dapm_widgets);
626 card->probe = asoc_graph_card_probe;
628 if (of_device_get_match_data(dev))
629 priv->dpcm_selectable = 1;
631 return audio_graph_parse_of(priv, dev);
634 static const struct of_device_id graph_of_match[] = {
635 { .compatible = "audio-graph-card", },
636 { .compatible = "audio-graph-scu-card",
637 .data = (void *)DPCM_SELECTABLE },
640 MODULE_DEVICE_TABLE(of, graph_of_match);
642 static struct platform_driver graph_card = {
644 .name = "asoc-audio-graph-card",
645 .pm = &snd_soc_pm_ops,
646 .of_match_table = graph_of_match,
648 .probe = graph_probe,
649 .remove = asoc_simple_remove,
651 module_platform_driver(graph_card);
653 MODULE_ALIAS("platform:asoc-audio-graph-card");
654 MODULE_LICENSE("GPL v2");
655 MODULE_DESCRIPTION("ASoC Audio Graph Sound Card");
656 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");