1 // SPDX-License-Identifier: GPL-2.0
3 // ASoC simple sound card support
5 // Copyright (C) 2012 Renesas Solutions Corp.
6 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
9 #include <linux/device.h>
10 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/string.h>
14 #include <sound/simple_card.h>
15 #include <sound/soc-dai.h>
16 #include <sound/soc.h>
19 int dais; /* number of dai */
20 int link; /* number of link */
21 int conf; /* number of codec_conf */
22 int cpu; /* turn for CPU / Codec */
25 #define DAI "sound-dai"
26 #define CELL "#sound-dai-cells"
27 #define PREFIX "simple-audio-card,"
29 static int simple_set_clk_rate(struct asoc_simple_dai *simple_dai,
38 if (clk_get_rate(simple_dai->clk) == rate)
41 return clk_set_rate(simple_dai->clk, rate);
44 static int simple_hw_params(struct snd_pcm_substream *substream,
45 struct snd_pcm_hw_params *params)
47 struct snd_soc_pcm_runtime *rtd = substream->private_data;
48 struct snd_soc_dai *codec_dai = rtd->codec_dai;
49 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
50 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card);
51 struct simple_dai_props *dai_props =
52 simple_priv_to_props(priv, rtd->num);
53 unsigned int mclk, mclk_fs = 0;
56 if (dai_props->mclk_fs)
57 mclk_fs = dai_props->mclk_fs;
60 mclk = params_rate(params) * mclk_fs;
62 ret = simple_set_clk_rate(dai_props->codec_dai, mclk);
66 ret = simple_set_clk_rate(dai_props->cpu_dai, mclk);
70 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
72 if (ret && ret != -ENOTSUPP)
75 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
77 if (ret && ret != -ENOTSUPP)
85 static const struct snd_soc_ops simple_ops = {
86 .startup = asoc_simple_startup,
87 .shutdown = asoc_simple_shutdown,
88 .hw_params = simple_hw_params,
91 static int simple_dai_init(struct snd_soc_pcm_runtime *rtd)
93 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card);
94 struct simple_dai_props *dai_props = simple_priv_to_props(priv, rtd->num);
97 ret = asoc_simple_card_init_dai(rtd->codec_dai,
98 dai_props->codec_dai);
102 ret = asoc_simple_card_init_dai(rtd->cpu_dai,
110 static int simple_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
111 struct snd_pcm_hw_params *params)
113 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card);
114 struct simple_dai_props *dai_props = simple_priv_to_props(priv, rtd->num);
116 asoc_simple_card_convert_fixup(&dai_props->adata, params);
121 static void simple_parse_convert(struct device *dev,
122 struct device_node *np,
123 struct asoc_simple_card_data *adata)
125 struct device_node *top = dev->of_node;
126 struct device_node *node = of_get_parent(np);
128 asoc_simple_card_parse_convert(dev, top, PREFIX, adata);
129 asoc_simple_card_parse_convert(dev, node, PREFIX, adata);
130 asoc_simple_card_parse_convert(dev, node, NULL, adata);
131 asoc_simple_card_parse_convert(dev, np, NULL, adata);
136 static void simple_parse_mclk_fs(struct device_node *top,
137 struct device_node *cpu,
138 struct device_node *codec,
139 struct simple_dai_props *props,
142 struct device_node *node = of_get_parent(cpu);
145 snprintf(prop, sizeof(prop), "%smclk-fs", PREFIX);
146 of_property_read_u32(top, prop, &props->mclk_fs);
148 snprintf(prop, sizeof(prop), "%smclk-fs", prefix);
149 of_property_read_u32(node, prop, &props->mclk_fs);
150 of_property_read_u32(cpu, prop, &props->mclk_fs);
151 of_property_read_u32(codec, prop, &props->mclk_fs);
156 static int simple_dai_link_of_dpcm(struct asoc_simple_priv *priv,
157 struct device_node *np,
158 struct device_node *codec,
159 struct link_info *li,
162 struct device *dev = simple_priv_to_dev(priv);
163 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
164 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
165 struct asoc_simple_dai *dai;
166 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
167 struct device_node *top = dev->of_node;
168 struct device_node *node = of_get_parent(np);
178 if (li->cpu == (np == codec))
181 dev_dbg(dev, "link_of DPCM (%pOF)\n", np);
187 /* For single DAI link & old style of DT node */
192 int is_single_links = 0;
195 codecs->of_node = NULL;
196 codecs->dai_name = "snd-soc-dummy-dai";
197 codecs->name = "snd-soc-dummy";
200 dai_link->dynamic = 1;
201 dai_link->dpcm_merged_format = 1;
204 dai_props->cpu_dai = &priv->dais[li->dais++];
206 ret = asoc_simple_card_parse_cpu(np, dai_link, DAI, CELL,
211 ret = asoc_simple_card_parse_clk_cpu(dev, np, dai_link, dai);
215 ret = asoc_simple_card_set_dailink_name(dev, dai_link,
217 dai_link->cpu_dai_name);
221 asoc_simple_card_canonicalize_cpu(dai_link, is_single_links);
223 struct snd_soc_codec_conf *cconf;
226 dai_link->cpu_of_node = NULL;
227 dai_link->cpu_dai_name = "snd-soc-dummy-dai";
228 dai_link->cpu_name = "snd-soc-dummy";
231 dai_link->no_pcm = 1;
232 dai_link->be_hw_params_fixup = simple_be_hw_params_fixup;
235 dai_props->codec_dai = &priv->dais[li->dais++];
238 dai_props->codec_conf = &priv->codec_conf[li->conf++];
240 ret = asoc_simple_card_parse_codec(np, dai_link, DAI, CELL);
244 ret = asoc_simple_card_parse_clk_codec(dev, np, dai_link, dai);
248 ret = asoc_simple_card_set_dailink_name(dev, dai_link,
254 /* check "prefix" from top node */
255 snd_soc_of_parse_node_prefix(top, cconf, codecs->of_node,
257 snd_soc_of_parse_node_prefix(node, cconf, codecs->of_node,
259 snd_soc_of_parse_node_prefix(np, cconf, codecs->of_node,
263 simple_parse_convert(dev, np, &dai_props->adata);
264 simple_parse_mclk_fs(top, np, codec, dai_props, prefix);
266 asoc_simple_card_canonicalize_platform(dai_link);
268 ret = asoc_simple_card_of_parse_tdm(np, dai);
272 ret = asoc_simple_card_parse_daifmt(dev, node, codec,
273 prefix, &dai_link->dai_fmt);
277 dai_link->dpcm_playback = 1;
278 dai_link->dpcm_capture = 1;
279 dai_link->ops = &simple_ops;
280 dai_link->init = simple_dai_init;
285 static int simple_dai_link_of(struct asoc_simple_priv *priv,
286 struct device_node *np,
287 struct device_node *codec,
288 struct link_info *li,
291 struct device *dev = simple_priv_to_dev(priv);
292 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
293 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
294 struct asoc_simple_dai *cpu_dai;
295 struct asoc_simple_dai *codec_dai;
296 struct device_node *top = dev->of_node;
297 struct device_node *cpu = NULL;
298 struct device_node *node = NULL;
299 struct device_node *plat = NULL;
307 * Codec |return|return
310 if (!li->cpu || np == codec)
314 node = of_get_parent(np);
317 dev_dbg(dev, "link_of (%pOF)\n", node);
319 /* For single DAI link & old style of DT node */
323 snprintf(prop, sizeof(prop), "%splat", prefix);
324 plat = of_get_child_by_name(node, prop);
327 dai_props->cpu_dai = &priv->dais[li->dais++];
329 dai_props->codec_dai = &priv->dais[li->dais++];
331 ret = asoc_simple_card_parse_daifmt(dev, node, codec,
332 prefix, &dai_link->dai_fmt);
334 goto dai_link_of_err;
336 simple_parse_mclk_fs(top, cpu, codec, dai_props, prefix);
338 ret = asoc_simple_card_parse_cpu(cpu, dai_link,
339 DAI, CELL, &single_cpu);
341 goto dai_link_of_err;
343 ret = asoc_simple_card_parse_codec(codec, dai_link, DAI, CELL);
345 goto dai_link_of_err;
347 ret = asoc_simple_card_parse_platform(plat, dai_link, DAI, CELL);
349 goto dai_link_of_err;
351 ret = asoc_simple_card_of_parse_tdm(cpu, cpu_dai);
353 goto dai_link_of_err;
355 ret = asoc_simple_card_of_parse_tdm(codec, codec_dai);
357 goto dai_link_of_err;
359 ret = asoc_simple_card_parse_clk_cpu(dev, cpu, dai_link, cpu_dai);
361 goto dai_link_of_err;
363 ret = asoc_simple_card_parse_clk_codec(dev, codec, dai_link, codec_dai);
365 goto dai_link_of_err;
367 ret = asoc_simple_card_set_dailink_name(dev, dai_link,
369 dai_link->cpu_dai_name,
370 dai_link->codecs->dai_name);
372 goto dai_link_of_err;
374 dai_link->ops = &simple_ops;
375 dai_link->init = simple_dai_init;
377 asoc_simple_card_canonicalize_cpu(dai_link, single_cpu);
378 asoc_simple_card_canonicalize_platform(dai_link);
387 static int simple_for_each_link(struct asoc_simple_priv *priv,
388 struct link_info *li,
389 int (*func_noml)(struct asoc_simple_priv *priv,
390 struct device_node *np,
391 struct device_node *codec,
392 struct link_info *li, bool is_top),
393 int (*func_dpcm)(struct asoc_simple_priv *priv,
394 struct device_node *np,
395 struct device_node *codec,
396 struct link_info *li, bool is_top))
398 struct device *dev = simple_priv_to_dev(priv);
399 struct device_node *top = dev->of_node;
400 struct device_node *node;
404 /* Check if it has dai-link */
405 node = of_get_child_by_name(top, PREFIX "dai-link");
407 node = of_node_get(top);
411 /* loop for all dai-link */
413 struct asoc_simple_card_data adata;
414 struct device_node *codec;
415 struct device_node *np;
416 int num = of_get_child_count(node);
419 codec = of_get_child_by_name(node, is_top ?
420 PREFIX "codec" : "codec");
428 /* get convert-xxx property */
429 memset(&adata, 0, sizeof(adata));
430 for_each_child_of_node(node, np)
431 simple_parse_convert(dev, np, &adata);
433 /* loop for all CPU/Codec node */
434 for_each_child_of_node(node, np) {
437 * if it has many CPUs,
438 * or has convert-xxx property
441 adata.convert_rate || adata.convert_channels)
442 ret = func_dpcm(priv, np, codec, li, is_top);
443 /* else normal sound */
445 ret = func_noml(priv, np, codec, li, is_top);
453 node = of_get_next_child(top, node);
454 } while (!is_top && node);
461 static int simple_parse_aux_devs(struct device_node *node,
462 struct asoc_simple_priv *priv)
464 struct device *dev = simple_priv_to_dev(priv);
465 struct device_node *aux_node;
466 struct snd_soc_card *card = simple_priv_to_card(priv);
469 if (!of_find_property(node, PREFIX "aux-devs", &len))
470 return 0; /* Ok to have no aux-devs */
472 n = len / sizeof(__be32);
476 card->aux_dev = devm_kcalloc(dev,
477 n, sizeof(*card->aux_dev), GFP_KERNEL);
481 for (i = 0; i < n; i++) {
482 aux_node = of_parse_phandle(node, PREFIX "aux-devs", i);
485 card->aux_dev[i].codec_of_node = aux_node;
488 card->num_aux_devs = n;
492 static int simple_parse_of(struct asoc_simple_priv *priv)
494 struct device *dev = simple_priv_to_dev(priv);
495 struct device_node *top = dev->of_node;
496 struct snd_soc_card *card = simple_priv_to_card(priv);
503 ret = asoc_simple_card_of_parse_widgets(card, PREFIX);
507 ret = asoc_simple_card_of_parse_routing(card, PREFIX);
511 /* Single/Muti DAI link(s) & New style of DT node */
512 memset(&li, 0, sizeof(li));
513 for (li.cpu = 1; li.cpu >= 0; li.cpu--) {
515 * Detect all CPU first, and Detect all Codec 2nd.
517 * In Normal sound case, all DAIs are detected
520 * In DPCM sound case,
521 * all CPUs are detected as "CPU-dummy", and
522 * all Codecs are detected as "dummy-Codec".
523 * To avoid random sub-device numbering,
524 * detect "dummy-Codec" in last;
526 ret = simple_for_each_link(priv, &li,
528 simple_dai_link_of_dpcm);
533 ret = asoc_simple_card_parse_card_name(card, PREFIX);
537 ret = simple_parse_aux_devs(top, priv);
542 static int simple_count_noml(struct asoc_simple_priv *priv,
543 struct device_node *np,
544 struct device_node *codec,
545 struct link_info *li, bool is_top)
547 li->dais++; /* CPU or Codec */
549 li->link++; /* CPU-Codec */
554 static int simple_count_dpcm(struct asoc_simple_priv *priv,
555 struct device_node *np,
556 struct device_node *codec,
557 struct link_info *li, bool is_top)
559 li->dais++; /* CPU or Codec */
560 li->link++; /* CPU-dummy or dummy-Codec */
567 static void simple_get_dais_count(struct asoc_simple_priv *priv,
568 struct link_info *li)
570 struct device *dev = simple_priv_to_dev(priv);
571 struct device_node *top = dev->of_node;
574 * link_num : number of links.
575 * CPU-Codec / CPU-dummy / dummy-Codec
576 * dais_num : number of DAIs
577 * ccnf_num : number of codec_conf
578 * same number for "dummy-Codec"
581 * CPU0 --- Codec0 link : 5
582 * CPU1 --- Codec1 dais : 7
586 * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec
587 * => 7 DAIs = 4xCPU + 3xCodec
588 * => 1 ccnf = 1xdummy-Codec
591 * CPU0 --- Codec0 link : 5
592 * CPU1 --- Codec1 dais : 6
596 * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec
597 * => 6 DAIs = 4xCPU + 2xCodec
598 * => 1 ccnf = 1xdummy-Codec
601 * CPU0 --- Codec0 link : 6
603 * CPU2 --- Codec1 ccnf : 2
606 * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec
607 * => 6 DAIs = 4xCPU + 2xCodec
608 * => 2 ccnf = 2xdummy-Codec
611 * CPU0 --- Codec0 (convert-rate) link : 3
612 * CPU1 --- Codec1 dais : 4
615 * => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec
616 * => 4 DAIs = 2xCPU + 2xCodec
617 * => 1 ccnf = 1xdummy-Codec
626 simple_for_each_link(priv, li,
630 dev_dbg(dev, "link %d, dais %d, ccnf %d\n",
631 li->link, li->dais, li->conf);
634 static int simple_soc_probe(struct snd_soc_card *card)
636 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(card);
639 ret = asoc_simple_card_init_hp(card, &priv->hp_jack, PREFIX);
643 ret = asoc_simple_card_init_mic(card, &priv->mic_jack, PREFIX);
650 static int simple_probe(struct platform_device *pdev)
652 struct asoc_simple_priv *priv;
653 struct snd_soc_dai_link *dai_link;
654 struct simple_dai_props *dai_props;
655 struct asoc_simple_dai *dais;
656 struct device *dev = &pdev->dev;
657 struct device_node *np = dev->of_node;
658 struct snd_soc_card *card;
659 struct snd_soc_codec_conf *cconf;
663 /* Allocate the private data and the DAI link array */
664 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
668 card = simple_priv_to_card(priv);
669 card->owner = THIS_MODULE;
671 card->probe = simple_soc_probe;
673 memset(&li, 0, sizeof(li));
674 simple_get_dais_count(priv, &li);
675 if (!li.link || !li.dais)
678 dai_props = devm_kcalloc(dev, li.link, sizeof(*dai_props), GFP_KERNEL);
679 dai_link = devm_kcalloc(dev, li.link, sizeof(*dai_link), GFP_KERNEL);
680 dais = devm_kcalloc(dev, li.dais, sizeof(*dais), GFP_KERNEL);
681 cconf = devm_kcalloc(dev, li.conf, sizeof(*cconf), GFP_KERNEL);
682 if (!dai_props || !dai_link || !dais)
686 * Use snd_soc_dai_link_component instead of legacy style
687 * It is codec only. but cpu/platform will be supported in the future.
689 * soc-core.c :: snd_soc_init_multicodec()
691 for (i = 0; i < li.link; i++) {
692 dai_link[i].codecs = &dai_props[i].codecs;
693 dai_link[i].num_codecs = 1;
694 dai_link[i].platforms = &dai_props[i].platforms;
695 dai_link[i].num_platforms = 1;
698 priv->dai_props = dai_props;
699 priv->dai_link = dai_link;
701 priv->codec_conf = cconf;
703 card->dai_link = priv->dai_link;
704 card->num_links = li.link;
705 card->codec_conf = cconf;
706 card->num_configs = li.conf;
708 if (np && of_device_is_available(np)) {
710 ret = simple_parse_of(priv);
712 if (ret != -EPROBE_DEFER)
713 dev_err(dev, "parse error %d\n", ret);
718 struct asoc_simple_card_info *cinfo;
719 struct snd_soc_dai_link_component *codecs;
720 struct snd_soc_dai_link_component *platform;
723 cinfo = dev->platform_data;
725 dev_err(dev, "no info for asoc-simple-card\n");
730 !cinfo->codec_dai.name ||
733 !cinfo->cpu_dai.name) {
734 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
738 dai_props->cpu_dai = &priv->dais[dai_idx++];
739 dai_props->codec_dai = &priv->dais[dai_idx++];
741 codecs = dai_link->codecs;
742 codecs->name = cinfo->codec;
743 codecs->dai_name = cinfo->codec_dai.name;
745 platform = dai_link->platforms;
746 platform->name = cinfo->platform;
748 card->name = (cinfo->card) ? cinfo->card : cinfo->name;
749 dai_link->name = cinfo->name;
750 dai_link->stream_name = cinfo->name;
751 dai_link->cpu_dai_name = cinfo->cpu_dai.name;
752 dai_link->dai_fmt = cinfo->daifmt;
753 dai_link->init = simple_dai_init;
754 memcpy(priv->dai_props->cpu_dai, &cinfo->cpu_dai,
755 sizeof(*priv->dai_props->cpu_dai));
756 memcpy(priv->dai_props->codec_dai, &cinfo->codec_dai,
757 sizeof(*priv->dai_props->codec_dai));
760 snd_soc_card_set_drvdata(card, priv);
762 asoc_simple_debug_info(priv);
764 ret = devm_snd_soc_register_card(dev, card);
770 asoc_simple_card_clean_reference(card);
775 static int simple_remove(struct platform_device *pdev)
777 struct snd_soc_card *card = platform_get_drvdata(pdev);
779 return asoc_simple_card_clean_reference(card);
782 static const struct of_device_id simple_of_match[] = {
783 { .compatible = "simple-audio-card", },
784 { .compatible = "simple-scu-audio-card", },
787 MODULE_DEVICE_TABLE(of, simple_of_match);
789 static struct platform_driver asoc_simple_card = {
791 .name = "asoc-simple-card",
792 .pm = &snd_soc_pm_ops,
793 .of_match_table = simple_of_match,
795 .probe = simple_probe,
796 .remove = simple_remove,
799 module_platform_driver(asoc_simple_card);
801 MODULE_ALIAS("platform:asoc-simple-card");
802 MODULE_LICENSE("GPL v2");
803 MODULE_DESCRIPTION("ASoC Simple Sound Card");
804 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");