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 struct graph_card_data {
24 struct snd_soc_card snd_card;
25 struct graph_dai_props {
26 struct asoc_simple_dai *cpu_dai;
27 struct asoc_simple_dai *codec_dai;
28 struct snd_soc_dai_link_component codecs; /* single codec */
29 struct snd_soc_dai_link_component platform;
30 struct asoc_simple_card_data adata;
31 struct snd_soc_codec_conf *codec_conf;
34 struct asoc_simple_jack hp_jack;
35 struct asoc_simple_jack mic_jack;
36 struct snd_soc_dai_link *dai_link;
37 struct asoc_simple_dai *dais;
38 struct snd_soc_codec_conf *codec_conf;
39 struct gpio_desc *pa_gpio;
43 int dais; /* number of dai */
44 int link; /* number of link */
45 int conf; /* number of codec_conf */
46 int cpu; /* turn for CPU / Codec */
49 #define graph_priv_to_card(priv) (&(priv)->snd_card)
50 #define graph_priv_to_props(priv, i) ((priv)->dai_props + (i))
51 #define graph_priv_to_dev(priv) (graph_priv_to_card(priv)->dev)
52 #define graph_priv_to_link(priv, i) (graph_priv_to_card(priv)->dai_link + (i))
54 #define PREFIX "audio-graph-card,"
56 static int asoc_graph_card_outdrv_event(struct snd_soc_dapm_widget *w,
57 struct snd_kcontrol *kcontrol,
60 struct snd_soc_dapm_context *dapm = w->dapm;
61 struct graph_card_data *priv = snd_soc_card_get_drvdata(dapm->card);
64 case SND_SOC_DAPM_POST_PMU:
65 gpiod_set_value_cansleep(priv->pa_gpio, 1);
67 case SND_SOC_DAPM_PRE_PMD:
68 gpiod_set_value_cansleep(priv->pa_gpio, 0);
77 static const struct snd_soc_dapm_widget asoc_graph_card_dapm_widgets[] = {
78 SND_SOC_DAPM_OUT_DRV_E("Amplifier", SND_SOC_NOPM,
79 0, 0, NULL, 0, asoc_graph_card_outdrv_event,
80 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
83 static int asoc_graph_card_startup(struct snd_pcm_substream *substream)
85 struct snd_soc_pcm_runtime *rtd = substream->private_data;
86 struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
87 struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
90 ret = asoc_simple_card_clk_enable(dai_props->cpu_dai);
94 ret = asoc_simple_card_clk_enable(dai_props->codec_dai);
96 asoc_simple_card_clk_disable(dai_props->cpu_dai);
101 static void asoc_graph_card_shutdown(struct snd_pcm_substream *substream)
103 struct snd_soc_pcm_runtime *rtd = substream->private_data;
104 struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
105 struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
107 asoc_simple_card_clk_disable(dai_props->cpu_dai);
109 asoc_simple_card_clk_disable(dai_props->codec_dai);
112 static int asoc_graph_card_hw_params(struct snd_pcm_substream *substream,
113 struct snd_pcm_hw_params *params)
115 struct snd_soc_pcm_runtime *rtd = substream->private_data;
116 struct snd_soc_dai *codec_dai = rtd->codec_dai;
117 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
118 struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
119 struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
120 unsigned int mclk, mclk_fs = 0;
123 if (dai_props->mclk_fs)
124 mclk_fs = dai_props->mclk_fs;
127 mclk = params_rate(params) * mclk_fs;
128 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
130 if (ret && ret != -ENOTSUPP)
133 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
135 if (ret && ret != -ENOTSUPP)
143 static const struct snd_soc_ops asoc_graph_card_ops = {
144 .startup = asoc_graph_card_startup,
145 .shutdown = asoc_graph_card_shutdown,
146 .hw_params = asoc_graph_card_hw_params,
149 static int asoc_graph_card_dai_init(struct snd_soc_pcm_runtime *rtd)
151 struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
152 struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
155 ret = asoc_simple_card_init_dai(rtd->codec_dai,
156 dai_props->codec_dai);
160 ret = asoc_simple_card_init_dai(rtd->cpu_dai,
168 static int asoc_graph_card_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
169 struct snd_pcm_hw_params *params)
171 struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
172 struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
174 asoc_simple_card_convert_fixup(&dai_props->adata, params);
179 static void asoc_graph_card_get_conversion(struct device *dev,
180 struct device_node *ep,
181 struct asoc_simple_card_data *adata)
183 struct device_node *top = dev->of_node;
184 struct device_node *port = of_get_parent(ep);
185 struct device_node *ports = of_get_parent(port);
186 struct device_node *node = of_graph_get_port_parent(ep);
188 asoc_simple_card_parse_convert(dev, top, NULL, adata);
189 asoc_simple_card_parse_convert(dev, node, PREFIX, adata);
190 asoc_simple_card_parse_convert(dev, ports, NULL, adata);
191 asoc_simple_card_parse_convert(dev, port, NULL, adata);
192 asoc_simple_card_parse_convert(dev, ep, NULL, adata);
195 static int asoc_graph_card_dai_link_of_dpcm(struct graph_card_data *priv,
196 struct device_node *cpu_ep,
197 struct device_node *codec_ep,
198 struct link_info *li,
201 struct device *dev = graph_priv_to_dev(priv);
202 struct snd_soc_dai_link *dai_link = graph_priv_to_link(priv, li->link);
203 struct graph_dai_props *dai_props = graph_priv_to_props(priv, li->link);
204 struct device_node *top = dev->of_node;
205 struct device_node *ep = li->cpu ? cpu_ep : codec_ep;
206 struct device_node *port;
207 struct device_node *ports;
208 struct device_node *node;
209 struct asoc_simple_dai *dai;
210 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
213 /* Do it all CPU endpoint, and 1st Codec endpoint */
214 if (!li->cpu && dup_codec)
217 port = of_get_parent(ep);
218 ports = of_get_parent(port);
219 node = of_graph_get_port_parent(ep);
223 dev_dbg(dev, "link_of DPCM (%pOF)\n", ep);
225 of_property_read_u32(top, "mclk-fs", &dai_props->mclk_fs);
226 of_property_read_u32(ports, "mclk-fs", &dai_props->mclk_fs);
227 of_property_read_u32(port, "mclk-fs", &dai_props->mclk_fs);
228 of_property_read_u32(ep, "mclk-fs", &dai_props->mclk_fs);
230 asoc_graph_card_get_conversion(dev, ep, &dai_props->adata);
239 codecs->of_node = NULL;
240 codecs->dai_name = "snd-soc-dummy-dai";
241 codecs->name = "snd-soc-dummy";
244 dai_link->dynamic = 1;
245 dai_link->dpcm_merged_format = 1;
248 dai_props->cpu_dai = &priv->dais[li->dais++];
250 ret = asoc_simple_card_parse_graph_cpu(ep, dai_link);
254 ret = asoc_simple_card_parse_clk_cpu(dev, ep, dai_link, dai);
258 ret = asoc_simple_card_set_dailink_name(dev, dai_link,
260 dai_link->cpu_dai_name);
264 /* card->num_links includes Codec */
265 asoc_simple_card_canonicalize_cpu(dai_link,
266 of_graph_get_endpoint_count(dai_link->cpu_of_node) == 1);
268 struct snd_soc_codec_conf *cconf;
271 dai_link->cpu_of_node = NULL;
272 dai_link->cpu_dai_name = "snd-soc-dummy-dai";
273 dai_link->cpu_name = "snd-soc-dummy";
276 dai_link->no_pcm = 1;
277 dai_link->be_hw_params_fixup = asoc_graph_card_be_hw_params_fixup;
280 dai_props->codec_dai = &priv->dais[li->dais++];
283 dai_props->codec_conf = &priv->codec_conf[li->conf++];
285 ret = asoc_simple_card_parse_graph_codec(ep, dai_link);
289 ret = asoc_simple_card_parse_clk_codec(dev, ep, dai_link, dai);
293 ret = asoc_simple_card_set_dailink_name(dev, dai_link,
299 /* check "prefix" from top node */
300 snd_soc_of_parse_node_prefix(top, cconf, codecs->of_node,
302 snd_soc_of_parse_node_prefix(node, cconf, codecs->of_node,
304 snd_soc_of_parse_node_prefix(ports, cconf, codecs->of_node,
306 snd_soc_of_parse_node_prefix(port, cconf, codecs->of_node,
310 ret = asoc_simple_card_of_parse_tdm(ep, dai);
314 ret = asoc_simple_card_canonicalize_dailink(dai_link);
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 = &asoc_graph_card_ops;
326 dai_link->init = asoc_graph_card_dai_init;
331 static int asoc_graph_card_dai_link_of(struct graph_card_data *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 *codec_port;
342 struct device_node *cpu_ports;
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_canonicalize_dailink(dai_link);
412 ret = asoc_simple_card_set_dailink_name(dev, dai_link,
414 dai_link->cpu_dai_name,
415 dai_link->codecs->dai_name);
419 dai_link->ops = &asoc_graph_card_ops;
420 dai_link->init = asoc_graph_card_dai_init;
422 asoc_simple_card_canonicalize_cpu(dai_link,
423 of_graph_get_endpoint_count(dai_link->cpu_of_node) == 1);
428 static int asoc_graph_card_for_each_link(struct graph_card_data *priv,
429 struct link_info *li,
430 int (*func_noml)(struct graph_card_data *priv,
431 struct device_node *cpu_ep,
432 struct device_node *codec_ep,
433 struct link_info *li),
434 int (*func_dpcm)(struct graph_card_data *priv,
435 struct device_node *cpu_ep,
436 struct device_node *codec_ep,
437 struct link_info *li, int dup_codec))
439 struct of_phandle_iterator it;
440 struct device *dev = graph_priv_to_dev(priv);
441 struct device_node *node = dev->of_node;
442 struct device_node *cpu_port;
443 struct device_node *cpu_ep;
444 struct device_node *codec_ep;
445 struct device_node *codec_port;
446 struct device_node *codec_port_old = NULL;
447 struct asoc_simple_card_data adata;
450 /* loop for all listed CPU port */
451 of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
455 /* loop for all CPU endpoint */
457 cpu_ep = of_get_next_child(cpu_port, cpu_ep);
462 codec_ep = of_graph_get_remote_endpoint(cpu_ep);
463 codec_port = of_get_parent(codec_ep);
465 of_node_put(codec_ep);
466 of_node_put(codec_port);
468 /* get convert-xxx property */
469 memset(&adata, 0, sizeof(adata));
470 asoc_graph_card_get_conversion(dev, codec_ep, &adata);
471 asoc_graph_card_get_conversion(dev, cpu_ep, &adata);
475 * if Codec port has many endpoints,
476 * or has convert-xxx property
478 if ((of_get_child_count(codec_port) > 1) ||
479 adata.convert_rate || adata.convert_channels)
480 ret = func_dpcm(priv, cpu_ep, codec_ep, li,
481 (codec_port_old == codec_port));
482 /* else normal sound */
484 ret = func_noml(priv, cpu_ep, codec_ep, li);
489 codec_port_old = codec_port;
496 static int asoc_graph_card_parse_of(struct graph_card_data *priv)
498 struct snd_soc_card *card = graph_priv_to_card(priv);
502 ret = asoc_simple_card_of_parse_widgets(card, NULL);
506 ret = asoc_simple_card_of_parse_routing(card, NULL);
510 memset(&li, 0, sizeof(li));
511 for (li.cpu = 1; li.cpu >= 0; li.cpu--) {
513 * Detect all CPU first, and Detect all Codec 2nd.
515 * In Normal sound case, all DAIs are detected
518 * In DPCM sound case,
519 * all CPUs are detected as "CPU-dummy", and
520 * all Codecs are detected as "dummy-Codec".
521 * To avoid random sub-device numbering,
522 * detect "dummy-Codec" in last;
524 ret = asoc_graph_card_for_each_link(priv, &li,
525 asoc_graph_card_dai_link_of,
526 asoc_graph_card_dai_link_of_dpcm);
531 return asoc_simple_card_parse_card_name(card, NULL);
534 static int asoc_graph_card_count_noml(struct graph_card_data *priv,
535 struct device_node *cpu_ep,
536 struct device_node *codec_ep,
537 struct link_info *li)
539 struct device *dev = graph_priv_to_dev(priv);
541 li->link += 1; /* 1xCPU-Codec */
542 li->dais += 2; /* 1xCPU + 1xCodec */
544 dev_dbg(dev, "Count As Normal\n");
549 static int asoc_graph_card_count_dpcm(struct graph_card_data *priv,
550 struct device_node *cpu_ep,
551 struct device_node *codec_ep,
552 struct link_info *li,
555 struct device *dev = graph_priv_to_dev(priv);
557 li->link++; /* 1xCPU-dummy */
558 li->dais++; /* 1xCPU */
561 li->link++; /* 1xdummy-Codec */
562 li->conf++; /* 1xdummy-Codec */
563 li->dais++; /* 1xCodec */
566 dev_dbg(dev, "Count As DPCM\n");
571 static void asoc_graph_get_dais_count(struct graph_card_data *priv,
572 struct link_info *li)
574 struct device *dev = graph_priv_to_dev(priv);
577 * link_num : number of links.
578 * CPU-Codec / CPU-dummy / dummy-Codec
579 * dais_num : number of DAIs
580 * ccnf_num : number of codec_conf
581 * same number for "dummy-Codec"
584 * CPU0 --- Codec0 link : 5
585 * CPU1 --- Codec1 dais : 7
589 * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec
590 * => 7 DAIs = 4xCPU + 3xCodec
591 * => 1 ccnf = 1xdummy-Codec
594 * CPU0 --- Codec0 link : 5
595 * CPU1 --- Codec1 dais : 6
599 * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec
600 * => 6 DAIs = 4xCPU + 2xCodec
601 * => 1 ccnf = 1xdummy-Codec
604 * CPU0 --- Codec0 link : 6
606 * CPU2 --- Codec1 ccnf : 2
609 * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec
610 * => 6 DAIs = 4xCPU + 2xCodec
611 * => 2 ccnf = 2xdummy-Codec
614 * CPU0 --- Codec0 (convert-rate) link : 3
615 * CPU1 --- Codec1 dais : 4
618 * => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec
619 * => 4 DAIs = 2xCPU + 2xCodec
620 * => 1 ccnf = 1xdummy-Codec
622 asoc_graph_card_for_each_link(priv, li,
623 asoc_graph_card_count_noml,
624 asoc_graph_card_count_dpcm);
625 dev_dbg(dev, "link %d, dais %d, ccnf %d\n",
626 li->link, li->dais, li->conf);
629 static int asoc_graph_soc_card_probe(struct snd_soc_card *card)
631 struct graph_card_data *priv = snd_soc_card_get_drvdata(card);
634 ret = asoc_simple_card_init_hp(card, &priv->hp_jack, NULL);
638 ret = asoc_simple_card_init_mic(card, &priv->mic_jack, NULL);
645 static int asoc_graph_card_probe(struct platform_device *pdev)
647 struct graph_card_data *priv;
648 struct snd_soc_dai_link *dai_link;
649 struct graph_dai_props *dai_props;
650 struct asoc_simple_dai *dais;
651 struct device *dev = &pdev->dev;
652 struct snd_soc_card *card;
653 struct snd_soc_codec_conf *cconf;
657 /* Allocate the private data and the DAI link array */
658 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
662 card = graph_priv_to_card(priv);
663 card->owner = THIS_MODULE;
665 card->dapm_widgets = asoc_graph_card_dapm_widgets;
666 card->num_dapm_widgets = ARRAY_SIZE(asoc_graph_card_dapm_widgets);
667 card->probe = asoc_graph_soc_card_probe;
669 memset(&li, 0, sizeof(li));
670 asoc_graph_get_dais_count(priv, &li);
671 if (!li.link || !li.dais)
674 dai_props = devm_kcalloc(dev, li.link, sizeof(*dai_props), GFP_KERNEL);
675 dai_link = devm_kcalloc(dev, li.link, sizeof(*dai_link), GFP_KERNEL);
676 dais = devm_kcalloc(dev, li.dais, sizeof(*dais), GFP_KERNEL);
677 cconf = devm_kcalloc(dev, li.conf, sizeof(*cconf), GFP_KERNEL);
678 if (!dai_props || !dai_link || !dais)
682 * Use snd_soc_dai_link_component instead of legacy style
683 * It is codec only. but cpu/platform will be supported in the future.
685 * soc-core.c :: snd_soc_init_multicodec()
687 for (i = 0; i < li.link; i++) {
688 dai_link[i].codecs = &dai_props[i].codecs;
689 dai_link[i].num_codecs = 1;
690 dai_link[i].platform = &dai_props[i].platform;
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 = asoc_graph_card_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 asoc_graph_card_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 asoc_graph_of_match[] = {
738 { .compatible = "audio-graph-card", },
739 { .compatible = "audio-graph-scu-card", },
742 MODULE_DEVICE_TABLE(of, asoc_graph_of_match);
744 static struct platform_driver asoc_graph_card = {
746 .name = "asoc-audio-graph-card",
747 .pm = &snd_soc_pm_ops,
748 .of_match_table = asoc_graph_of_match,
750 .probe = asoc_graph_card_probe,
751 .remove = asoc_graph_card_remove,
753 module_platform_driver(asoc_graph_card);
755 MODULE_ALIAS("platform:asoc-audio-graph-card");
756 MODULE_LICENSE("GPL v2");
757 MODULE_DESCRIPTION("ASoC Audio Graph Sound Card");
758 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");