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/simple_card_utils.h>
23 #define DPCM_SELECTABLE 1
26 struct snd_soc_card snd_card;
27 struct graph_dai_props {
28 struct asoc_simple_dai *cpu_dai;
29 struct asoc_simple_dai *codec_dai;
30 struct snd_soc_dai_link_component codecs; /* single codec */
31 struct snd_soc_dai_link_component platforms;
32 struct asoc_simple_card_data adata;
33 struct snd_soc_codec_conf *codec_conf;
36 struct asoc_simple_jack hp_jack;
37 struct asoc_simple_jack mic_jack;
38 struct snd_soc_dai_link *dai_link;
39 struct asoc_simple_dai *dais;
40 struct snd_soc_codec_conf *codec_conf;
41 struct gpio_desc *pa_gpio;
45 int dais; /* number of dai */
46 int link; /* number of link */
47 int conf; /* number of codec_conf */
48 int cpu; /* turn for CPU / Codec */
51 #define graph_priv_to_card(priv) (&(priv)->snd_card)
52 #define graph_priv_to_props(priv, i) ((priv)->dai_props + (i))
53 #define graph_priv_to_dev(priv) (graph_priv_to_card(priv)->dev)
54 #define graph_priv_to_link(priv, i) (graph_priv_to_card(priv)->dai_link + (i))
56 #define PREFIX "audio-graph-card,"
58 static int graph_outdrv_event(struct snd_soc_dapm_widget *w,
59 struct snd_kcontrol *kcontrol,
62 struct snd_soc_dapm_context *dapm = w->dapm;
63 struct graph_priv *priv = snd_soc_card_get_drvdata(dapm->card);
66 case SND_SOC_DAPM_POST_PMU:
67 gpiod_set_value_cansleep(priv->pa_gpio, 1);
69 case SND_SOC_DAPM_PRE_PMD:
70 gpiod_set_value_cansleep(priv->pa_gpio, 0);
79 static const struct snd_soc_dapm_widget graph_dapm_widgets[] = {
80 SND_SOC_DAPM_OUT_DRV_E("Amplifier", SND_SOC_NOPM,
81 0, 0, NULL, 0, graph_outdrv_event,
82 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
85 static int graph_startup(struct snd_pcm_substream *substream)
87 struct snd_soc_pcm_runtime *rtd = substream->private_data;
88 struct graph_priv *priv = snd_soc_card_get_drvdata(rtd->card);
89 struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
92 ret = asoc_simple_card_clk_enable(dai_props->cpu_dai);
96 ret = asoc_simple_card_clk_enable(dai_props->codec_dai);
98 asoc_simple_card_clk_disable(dai_props->cpu_dai);
103 static void graph_shutdown(struct snd_pcm_substream *substream)
105 struct snd_soc_pcm_runtime *rtd = substream->private_data;
106 struct graph_priv *priv = snd_soc_card_get_drvdata(rtd->card);
107 struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
109 asoc_simple_card_clk_disable(dai_props->cpu_dai);
111 asoc_simple_card_clk_disable(dai_props->codec_dai);
114 static int graph_hw_params(struct snd_pcm_substream *substream,
115 struct snd_pcm_hw_params *params)
117 struct snd_soc_pcm_runtime *rtd = substream->private_data;
118 struct snd_soc_dai *codec_dai = rtd->codec_dai;
119 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
120 struct graph_priv *priv = snd_soc_card_get_drvdata(rtd->card);
121 struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
122 unsigned int mclk, mclk_fs = 0;
125 if (dai_props->mclk_fs)
126 mclk_fs = dai_props->mclk_fs;
129 mclk = params_rate(params) * mclk_fs;
130 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
132 if (ret && ret != -ENOTSUPP)
135 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
137 if (ret && ret != -ENOTSUPP)
145 static const struct snd_soc_ops graph_ops = {
146 .startup = graph_startup,
147 .shutdown = graph_shutdown,
148 .hw_params = graph_hw_params,
151 static int graph_dai_init(struct snd_soc_pcm_runtime *rtd)
153 struct graph_priv *priv = snd_soc_card_get_drvdata(rtd->card);
154 struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
157 ret = asoc_simple_card_init_dai(rtd->codec_dai,
158 dai_props->codec_dai);
162 ret = asoc_simple_card_init_dai(rtd->cpu_dai,
170 static int graph_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
171 struct snd_pcm_hw_params *params)
173 struct graph_priv *priv = snd_soc_card_get_drvdata(rtd->card);
174 struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
176 asoc_simple_card_convert_fixup(&dai_props->adata, params);
181 static void graph_get_conversion(struct device *dev,
182 struct device_node *ep,
183 struct asoc_simple_card_data *adata)
185 struct device_node *top = dev->of_node;
186 struct device_node *port = of_get_parent(ep);
187 struct device_node *ports = of_get_parent(port);
188 struct device_node *node = of_graph_get_port_parent(ep);
190 asoc_simple_card_parse_convert(dev, top, NULL, adata);
191 asoc_simple_card_parse_convert(dev, node, PREFIX, adata);
192 asoc_simple_card_parse_convert(dev, ports, NULL, adata);
193 asoc_simple_card_parse_convert(dev, port, NULL, adata);
194 asoc_simple_card_parse_convert(dev, ep, NULL, adata);
197 static int graph_dai_link_of_dpcm(struct graph_priv *priv,
198 struct device_node *cpu_ep,
199 struct device_node *codec_ep,
200 struct link_info *li,
203 struct device *dev = graph_priv_to_dev(priv);
204 struct snd_soc_dai_link *dai_link = graph_priv_to_link(priv, li->link);
205 struct graph_dai_props *dai_props = graph_priv_to_props(priv, li->link);
206 struct device_node *top = dev->of_node;
207 struct device_node *ep = li->cpu ? cpu_ep : codec_ep;
208 struct device_node *port;
209 struct device_node *ports;
210 struct device_node *node;
211 struct asoc_simple_dai *dai;
212 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
215 /* Do it all CPU endpoint, and 1st Codec endpoint */
216 if (!li->cpu && dup_codec)
219 port = of_get_parent(ep);
220 ports = of_get_parent(port);
221 node = of_graph_get_port_parent(ep);
225 dev_dbg(dev, "link_of DPCM (%pOF)\n", ep);
227 of_property_read_u32(top, "mclk-fs", &dai_props->mclk_fs);
228 of_property_read_u32(ports, "mclk-fs", &dai_props->mclk_fs);
229 of_property_read_u32(port, "mclk-fs", &dai_props->mclk_fs);
230 of_property_read_u32(ep, "mclk-fs", &dai_props->mclk_fs);
232 graph_get_conversion(dev, ep, &dai_props->adata);
241 codecs->of_node = NULL;
242 codecs->dai_name = "snd-soc-dummy-dai";
243 codecs->name = "snd-soc-dummy";
246 dai_link->dynamic = 1;
247 dai_link->dpcm_merged_format = 1;
250 dai_props->cpu_dai = &priv->dais[li->dais++];
252 ret = asoc_simple_card_parse_graph_cpu(ep, dai_link);
256 ret = asoc_simple_card_parse_clk_cpu(dev, ep, dai_link, dai);
260 ret = asoc_simple_card_set_dailink_name(dev, dai_link,
262 dai_link->cpu_dai_name);
266 /* card->num_links includes Codec */
267 asoc_simple_card_canonicalize_cpu(dai_link,
268 of_graph_get_endpoint_count(dai_link->cpu_of_node) == 1);
270 struct snd_soc_codec_conf *cconf;
273 dai_link->cpu_of_node = NULL;
274 dai_link->cpu_dai_name = "snd-soc-dummy-dai";
275 dai_link->cpu_name = "snd-soc-dummy";
278 dai_link->no_pcm = 1;
279 dai_link->be_hw_params_fixup = graph_be_hw_params_fixup;
282 dai_props->codec_dai = &priv->dais[li->dais++];
285 dai_props->codec_conf = &priv->codec_conf[li->conf++];
287 ret = asoc_simple_card_parse_graph_codec(ep, dai_link);
291 ret = asoc_simple_card_parse_clk_codec(dev, ep, dai_link, dai);
295 ret = asoc_simple_card_set_dailink_name(dev, dai_link,
301 /* check "prefix" from top node */
302 snd_soc_of_parse_node_prefix(top, cconf, codecs->of_node,
304 snd_soc_of_parse_node_prefix(node, cconf, codecs->of_node,
306 snd_soc_of_parse_node_prefix(ports, cconf, codecs->of_node,
308 snd_soc_of_parse_node_prefix(port, cconf, codecs->of_node,
312 asoc_simple_card_canonicalize_platform(dai_link);
314 ret = asoc_simple_card_of_parse_tdm(ep, dai);
318 ret = asoc_simple_card_parse_daifmt(dev, cpu_ep, codec_ep,
319 NULL, &dai_link->dai_fmt);
323 dai_link->dpcm_playback = 1;
324 dai_link->dpcm_capture = 1;
325 dai_link->ops = &graph_ops;
326 dai_link->init = graph_dai_init;
331 static int graph_dai_link_of(struct graph_priv *priv,
332 struct device_node *cpu_ep,
333 struct device_node *codec_ep,
334 struct link_info *li)
336 struct device *dev = graph_priv_to_dev(priv);
337 struct snd_soc_dai_link *dai_link = graph_priv_to_link(priv, li->link);
338 struct graph_dai_props *dai_props = graph_priv_to_props(priv, li->link);
339 struct device_node *top = dev->of_node;
340 struct device_node *cpu_port;
341 struct device_node *cpu_ports;
342 struct device_node *codec_port;
343 struct device_node *codec_ports;
344 struct asoc_simple_dai *cpu_dai;
345 struct asoc_simple_dai *codec_dai;
348 /* Do it only CPU turn */
352 cpu_port = of_get_parent(cpu_ep);
353 cpu_ports = of_get_parent(cpu_port);
354 codec_port = of_get_parent(codec_ep);
355 codec_ports = of_get_parent(codec_port);
357 dev_dbg(dev, "link_of (%pOF)\n", cpu_ep);
362 dai_props->cpu_dai = &priv->dais[li->dais++];
364 dai_props->codec_dai = &priv->dais[li->dais++];
366 /* Factor to mclk, used in hw_params() */
367 of_property_read_u32(top, "mclk-fs", &dai_props->mclk_fs);
368 of_property_read_u32(cpu_ports, "mclk-fs", &dai_props->mclk_fs);
369 of_property_read_u32(codec_ports, "mclk-fs", &dai_props->mclk_fs);
370 of_property_read_u32(cpu_port, "mclk-fs", &dai_props->mclk_fs);
371 of_property_read_u32(codec_port, "mclk-fs", &dai_props->mclk_fs);
372 of_property_read_u32(cpu_ep, "mclk-fs", &dai_props->mclk_fs);
373 of_property_read_u32(codec_ep, "mclk-fs", &dai_props->mclk_fs);
374 of_node_put(cpu_port);
375 of_node_put(cpu_ports);
376 of_node_put(codec_port);
377 of_node_put(codec_ports);
379 ret = asoc_simple_card_parse_daifmt(dev, cpu_ep, codec_ep,
380 NULL, &dai_link->dai_fmt);
384 ret = asoc_simple_card_parse_graph_cpu(cpu_ep, dai_link);
388 ret = asoc_simple_card_parse_graph_codec(codec_ep, dai_link);
392 ret = asoc_simple_card_of_parse_tdm(cpu_ep, cpu_dai);
396 ret = asoc_simple_card_of_parse_tdm(codec_ep, codec_dai);
400 ret = asoc_simple_card_parse_clk_cpu(dev, cpu_ep, dai_link, cpu_dai);
404 ret = asoc_simple_card_parse_clk_codec(dev, codec_ep, dai_link, codec_dai);
408 ret = asoc_simple_card_set_dailink_name(dev, dai_link,
410 dai_link->cpu_dai_name,
411 dai_link->codecs->dai_name);
415 dai_link->ops = &graph_ops;
416 dai_link->init = graph_dai_init;
418 asoc_simple_card_canonicalize_platform(dai_link);
419 asoc_simple_card_canonicalize_cpu(dai_link,
420 of_graph_get_endpoint_count(dai_link->cpu_of_node) == 1);
425 static int graph_for_each_link(struct graph_priv *priv,
426 struct link_info *li,
427 int (*func_noml)(struct graph_priv *priv,
428 struct device_node *cpu_ep,
429 struct device_node *codec_ep,
430 struct link_info *li),
431 int (*func_dpcm)(struct graph_priv *priv,
432 struct device_node *cpu_ep,
433 struct device_node *codec_ep,
434 struct link_info *li, int dup_codec))
436 struct of_phandle_iterator it;
437 struct device *dev = graph_priv_to_dev(priv);
438 struct device_node *node = dev->of_node;
439 struct device_node *cpu_port;
440 struct device_node *cpu_ep;
441 struct device_node *codec_ep;
442 struct device_node *codec_port;
443 struct device_node *codec_port_old = NULL;
444 struct asoc_simple_card_data adata;
445 uintptr_t dpcm_selectable = (uintptr_t)of_device_get_match_data(dev);
448 /* loop for all listed CPU port */
449 of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
453 /* loop for all CPU endpoint */
455 cpu_ep = of_get_next_child(cpu_port, cpu_ep);
460 codec_ep = of_graph_get_remote_endpoint(cpu_ep);
461 codec_port = of_get_parent(codec_ep);
463 of_node_put(codec_ep);
464 of_node_put(codec_port);
466 /* get convert-xxx property */
467 memset(&adata, 0, sizeof(adata));
468 graph_get_conversion(dev, codec_ep, &adata);
469 graph_get_conversion(dev, cpu_ep, &adata);
473 * if Codec port has many endpoints,
474 * or has convert-xxx property
476 if (dpcm_selectable &&
477 ((of_get_child_count(codec_port) > 1) ||
478 adata.convert_rate || adata.convert_channels))
479 ret = func_dpcm(priv, cpu_ep, codec_ep, li,
480 (codec_port_old == codec_port));
481 /* else normal sound */
483 ret = func_noml(priv, cpu_ep, codec_ep, li);
488 codec_port_old = codec_port;
495 static int graph_parse_of(struct graph_priv *priv)
497 struct snd_soc_card *card = graph_priv_to_card(priv);
501 ret = asoc_simple_card_of_parse_widgets(card, NULL);
505 ret = asoc_simple_card_of_parse_routing(card, NULL);
509 memset(&li, 0, sizeof(li));
510 for (li.cpu = 1; li.cpu >= 0; li.cpu--) {
512 * Detect all CPU first, and Detect all Codec 2nd.
514 * In Normal sound case, all DAIs are detected
517 * In DPCM sound case,
518 * all CPUs are detected as "CPU-dummy", and
519 * all Codecs are detected as "dummy-Codec".
520 * To avoid random sub-device numbering,
521 * detect "dummy-Codec" in last;
523 ret = graph_for_each_link(priv, &li,
525 graph_dai_link_of_dpcm);
530 return asoc_simple_card_parse_card_name(card, NULL);
533 static int graph_count_noml(struct graph_priv *priv,
534 struct device_node *cpu_ep,
535 struct device_node *codec_ep,
536 struct link_info *li)
538 struct device *dev = graph_priv_to_dev(priv);
540 li->link += 1; /* 1xCPU-Codec */
541 li->dais += 2; /* 1xCPU + 1xCodec */
543 dev_dbg(dev, "Count As Normal\n");
548 static int graph_count_dpcm(struct graph_priv *priv,
549 struct device_node *cpu_ep,
550 struct device_node *codec_ep,
551 struct link_info *li,
554 struct device *dev = graph_priv_to_dev(priv);
556 li->link++; /* 1xCPU-dummy */
557 li->dais++; /* 1xCPU */
560 li->link++; /* 1xdummy-Codec */
561 li->conf++; /* 1xdummy-Codec */
562 li->dais++; /* 1xCodec */
565 dev_dbg(dev, "Count As DPCM\n");
570 static void graph_get_dais_count(struct graph_priv *priv,
571 struct link_info *li)
573 struct device *dev = graph_priv_to_dev(priv);
576 * link_num : number of links.
577 * CPU-Codec / CPU-dummy / dummy-Codec
578 * dais_num : number of DAIs
579 * ccnf_num : number of codec_conf
580 * same number for "dummy-Codec"
583 * CPU0 --- Codec0 link : 5
584 * CPU1 --- Codec1 dais : 7
588 * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec
589 * => 7 DAIs = 4xCPU + 3xCodec
590 * => 1 ccnf = 1xdummy-Codec
593 * CPU0 --- Codec0 link : 5
594 * CPU1 --- Codec1 dais : 6
598 * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec
599 * => 6 DAIs = 4xCPU + 2xCodec
600 * => 1 ccnf = 1xdummy-Codec
603 * CPU0 --- Codec0 link : 6
605 * CPU2 --- Codec1 ccnf : 2
608 * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec
609 * => 6 DAIs = 4xCPU + 2xCodec
610 * => 2 ccnf = 2xdummy-Codec
613 * CPU0 --- Codec0 (convert-rate) link : 3
614 * CPU1 --- Codec1 dais : 4
617 * => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec
618 * => 4 DAIs = 2xCPU + 2xCodec
619 * => 1 ccnf = 1xdummy-Codec
621 graph_for_each_link(priv, li,
624 dev_dbg(dev, "link %d, dais %d, ccnf %d\n",
625 li->link, li->dais, li->conf);
628 static int graph_card_probe(struct snd_soc_card *card)
630 struct graph_priv *priv = snd_soc_card_get_drvdata(card);
633 ret = asoc_simple_card_init_hp(card, &priv->hp_jack, NULL);
637 ret = asoc_simple_card_init_mic(card, &priv->mic_jack, NULL);
644 static int graph_probe(struct platform_device *pdev)
646 struct graph_priv *priv;
647 struct snd_soc_dai_link *dai_link;
648 struct graph_dai_props *dai_props;
649 struct asoc_simple_dai *dais;
650 struct device *dev = &pdev->dev;
651 struct snd_soc_card *card;
652 struct snd_soc_codec_conf *cconf;
656 /* Allocate the private data and the DAI link array */
657 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
661 card = graph_priv_to_card(priv);
662 card->owner = THIS_MODULE;
664 card->dapm_widgets = graph_dapm_widgets;
665 card->num_dapm_widgets = ARRAY_SIZE(graph_dapm_widgets);
666 card->probe = graph_card_probe;
668 memset(&li, 0, sizeof(li));
669 graph_get_dais_count(priv, &li);
670 if (!li.link || !li.dais)
673 dai_props = devm_kcalloc(dev, li.link, sizeof(*dai_props), GFP_KERNEL);
674 dai_link = devm_kcalloc(dev, li.link, sizeof(*dai_link), GFP_KERNEL);
675 dais = devm_kcalloc(dev, li.dais, sizeof(*dais), GFP_KERNEL);
676 cconf = devm_kcalloc(dev, li.conf, sizeof(*cconf), GFP_KERNEL);
677 if (!dai_props || !dai_link || !dais)
681 * Use snd_soc_dai_link_component instead of legacy style
682 * It is codec only. but cpu/platform will be supported in the future.
684 * soc-core.c :: snd_soc_init_multicodec()
686 for (i = 0; i < li.link; i++) {
687 dai_link[i].codecs = &dai_props[i].codecs;
688 dai_link[i].num_codecs = 1;
689 dai_link[i].platforms = &dai_props[i].platforms;
690 dai_link[i].num_platforms = 1;
693 priv->pa_gpio = devm_gpiod_get_optional(dev, "pa", GPIOD_OUT_LOW);
694 if (IS_ERR(priv->pa_gpio)) {
695 ret = PTR_ERR(priv->pa_gpio);
696 dev_err(dev, "failed to get amplifier gpio: %d\n", ret);
700 priv->dai_props = dai_props;
701 priv->dai_link = dai_link;
703 priv->codec_conf = cconf;
705 card->dai_link = dai_link;
706 card->num_links = li.link;
707 card->codec_conf = cconf;
708 card->num_configs = li.conf;
710 ret = graph_parse_of(priv);
712 if (ret != -EPROBE_DEFER)
713 dev_err(dev, "parse error %d\n", ret);
717 snd_soc_card_set_drvdata(card, priv);
719 ret = devm_snd_soc_register_card(dev, card);
725 asoc_simple_card_clean_reference(card);
730 static int graph_remove(struct platform_device *pdev)
732 struct snd_soc_card *card = platform_get_drvdata(pdev);
734 return asoc_simple_card_clean_reference(card);
737 static const struct of_device_id graph_of_match[] = {
738 { .compatible = "audio-graph-card", },
739 { .compatible = "audio-graph-scu-card",
740 .data = (void *)DPCM_SELECTABLE },
743 MODULE_DEVICE_TABLE(of, graph_of_match);
745 static struct platform_driver graph_card = {
747 .name = "asoc-audio-graph-card",
748 .pm = &snd_soc_pm_ops,
749 .of_match_table = graph_of_match,
751 .probe = graph_probe,
752 .remove = graph_remove,
754 module_platform_driver(graph_card);
756 MODULE_ALIAS("platform:asoc-audio-graph-card");
757 MODULE_LICENSE("GPL v2");
758 MODULE_DESCRIPTION("ASoC Audio Graph Sound Card");
759 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");