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/of_device.h>
13 #include <linux/platform_device.h>
14 #include <linux/string.h>
15 #include <sound/simple_card.h>
16 #include <sound/soc-dai.h>
17 #include <sound/soc.h>
19 #define DPCM_SELECTABLE 1
21 #define DAI "sound-dai"
22 #define CELL "#sound-dai-cells"
23 #define PREFIX "simple-audio-card,"
25 static const struct snd_soc_ops simple_ops = {
26 .startup = asoc_simple_startup,
27 .shutdown = asoc_simple_shutdown,
28 .hw_params = asoc_simple_hw_params,
31 static int asoc_simple_parse_dai(struct device_node *node,
32 struct snd_soc_dai_link_component *dlc,
35 struct of_phandle_args args;
42 * Get node via "sound-dai = <&phandle port>"
43 * it will be used as xxx_of_node on soc_bind_dai_link()
45 ret = of_parse_phandle_with_args(node, DAI, CELL, 0, &args);
50 ret = snd_soc_of_get_dai_name(node, &dlc->dai_name);
54 dlc->of_node = args.np;
57 *is_single_link = !args.args_count;
62 static void simple_parse_convert(struct device *dev,
63 struct device_node *np,
64 struct asoc_simple_data *adata)
66 struct device_node *top = dev->of_node;
67 struct device_node *node = of_get_parent(np);
69 asoc_simple_parse_convert(dev, top, PREFIX, adata);
70 asoc_simple_parse_convert(dev, node, PREFIX, adata);
71 asoc_simple_parse_convert(dev, node, NULL, adata);
72 asoc_simple_parse_convert(dev, np, NULL, adata);
77 static void simple_parse_mclk_fs(struct device_node *top,
78 struct device_node *cpu,
79 struct device_node *codec,
80 struct simple_dai_props *props,
83 struct device_node *node = of_get_parent(cpu);
86 snprintf(prop, sizeof(prop), "%smclk-fs", PREFIX);
87 of_property_read_u32(top, prop, &props->mclk_fs);
89 snprintf(prop, sizeof(prop), "%smclk-fs", prefix);
90 of_property_read_u32(node, prop, &props->mclk_fs);
91 of_property_read_u32(cpu, prop, &props->mclk_fs);
92 of_property_read_u32(codec, prop, &props->mclk_fs);
97 static int simple_dai_link_of_dpcm(struct asoc_simple_priv *priv,
98 struct device_node *np,
99 struct device_node *codec,
100 struct link_info *li,
103 struct device *dev = simple_priv_to_dev(priv);
104 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
105 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
106 struct asoc_simple_dai *dai;
107 struct snd_soc_dai_link_component *cpus = dai_link->cpus;
108 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
109 struct device_node *top = dev->of_node;
110 struct device_node *node = of_get_parent(np);
120 if (li->cpu == (np == codec))
123 dev_dbg(dev, "link_of DPCM (%pOF)\n", np);
127 /* For single DAI link & old style of DT node */
132 int is_single_links = 0;
135 codecs->of_node = NULL;
136 codecs->dai_name = "snd-soc-dummy-dai";
137 codecs->name = "snd-soc-dummy";
140 dai_link->dynamic = 1;
141 dai_link->dpcm_merged_format = 1;
144 dai_props->cpu_dai = &priv->dais[li->dais++];
146 ret = asoc_simple_parse_cpu(np, dai_link, &is_single_links);
150 ret = asoc_simple_parse_clk_cpu(dev, np, dai_link, dai);
154 ret = asoc_simple_set_dailink_name(dev, dai_link,
160 asoc_simple_canonicalize_cpu(dai_link, is_single_links);
162 struct snd_soc_codec_conf *cconf;
165 cpus->of_node = NULL;
166 cpus->dai_name = "snd-soc-dummy-dai";
167 cpus->name = "snd-soc-dummy";
170 dai_link->no_pcm = 1;
171 dai_link->be_hw_params_fixup = asoc_simple_be_hw_params_fixup;
174 dai_props->codec_dai = &priv->dais[li->dais++];
177 dai_props->codec_conf = &priv->codec_conf[li->conf++];
179 ret = asoc_simple_parse_codec(np, dai_link);
183 ret = asoc_simple_parse_clk_codec(dev, np, dai_link, dai);
187 ret = asoc_simple_set_dailink_name(dev, dai_link,
193 /* check "prefix" from top node */
194 snd_soc_of_parse_node_prefix(top, cconf, codecs->of_node,
196 snd_soc_of_parse_node_prefix(node, cconf, codecs->of_node,
198 snd_soc_of_parse_node_prefix(np, cconf, codecs->of_node,
202 simple_parse_convert(dev, np, &dai_props->adata);
203 simple_parse_mclk_fs(top, np, codec, dai_props, prefix);
205 asoc_simple_canonicalize_platform(dai_link);
207 ret = asoc_simple_parse_tdm(np, dai);
211 ret = asoc_simple_parse_daifmt(dev, node, codec,
212 prefix, &dai_link->dai_fmt);
216 dai_link->dpcm_playback = 1;
217 dai_link->dpcm_capture = 1;
218 dai_link->ops = &simple_ops;
219 dai_link->init = asoc_simple_dai_init;
226 static int simple_dai_link_of(struct asoc_simple_priv *priv,
227 struct device_node *np,
228 struct device_node *codec,
229 struct link_info *li,
232 struct device *dev = simple_priv_to_dev(priv);
233 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
234 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
235 struct asoc_simple_dai *cpu_dai;
236 struct asoc_simple_dai *codec_dai;
237 struct device_node *top = dev->of_node;
238 struct device_node *cpu = NULL;
239 struct device_node *node = NULL;
240 struct device_node *plat = NULL;
248 * Codec |return|return
251 if (!li->cpu || np == codec)
255 node = of_get_parent(np);
258 dev_dbg(dev, "link_of (%pOF)\n", node);
260 /* For single DAI link & old style of DT node */
264 snprintf(prop, sizeof(prop), "%splat", prefix);
265 plat = of_get_child_by_name(node, prop);
268 dai_props->cpu_dai = &priv->dais[li->dais++];
270 dai_props->codec_dai = &priv->dais[li->dais++];
272 ret = asoc_simple_parse_daifmt(dev, node, codec,
273 prefix, &dai_link->dai_fmt);
275 goto dai_link_of_err;
277 simple_parse_mclk_fs(top, cpu, codec, dai_props, prefix);
279 ret = asoc_simple_parse_cpu(cpu, dai_link, &single_cpu);
281 goto dai_link_of_err;
283 ret = asoc_simple_parse_codec(codec, dai_link);
285 goto dai_link_of_err;
287 ret = asoc_simple_parse_platform(plat, dai_link);
289 goto dai_link_of_err;
291 ret = asoc_simple_parse_tdm(cpu, cpu_dai);
293 goto dai_link_of_err;
295 ret = asoc_simple_parse_tdm(codec, codec_dai);
297 goto dai_link_of_err;
299 ret = asoc_simple_parse_clk_cpu(dev, cpu, dai_link, cpu_dai);
301 goto dai_link_of_err;
303 ret = asoc_simple_parse_clk_codec(dev, codec, dai_link, codec_dai);
305 goto dai_link_of_err;
307 ret = asoc_simple_set_dailink_name(dev, dai_link,
309 dai_link->cpus->dai_name,
310 dai_link->codecs->dai_name);
312 goto dai_link_of_err;
314 dai_link->ops = &simple_ops;
315 dai_link->init = asoc_simple_dai_init;
317 asoc_simple_canonicalize_cpu(dai_link, single_cpu);
318 asoc_simple_canonicalize_platform(dai_link);
327 static int simple_for_each_link(struct asoc_simple_priv *priv,
328 struct link_info *li,
329 int (*func_noml)(struct asoc_simple_priv *priv,
330 struct device_node *np,
331 struct device_node *codec,
332 struct link_info *li, bool is_top),
333 int (*func_dpcm)(struct asoc_simple_priv *priv,
334 struct device_node *np,
335 struct device_node *codec,
336 struct link_info *li, bool is_top))
338 struct device *dev = simple_priv_to_dev(priv);
339 struct device_node *top = dev->of_node;
340 struct device_node *node;
341 uintptr_t dpcm_selectable = (uintptr_t)of_device_get_match_data(dev);
345 /* Check if it has dai-link */
346 node = of_get_child_by_name(top, PREFIX "dai-link");
348 node = of_node_get(top);
352 /* loop for all dai-link */
354 struct asoc_simple_data adata;
355 struct device_node *codec;
356 struct device_node *np;
357 int num = of_get_child_count(node);
360 codec = of_get_child_by_name(node, is_top ?
361 PREFIX "codec" : "codec");
367 /* get convert-xxx property */
368 memset(&adata, 0, sizeof(adata));
369 for_each_child_of_node(node, np)
370 simple_parse_convert(dev, np, &adata);
372 /* loop for all CPU/Codec node */
373 for_each_child_of_node(node, np) {
376 * if it has many CPUs,
377 * or has convert-xxx property
379 if (dpcm_selectable &&
381 adata.convert_rate || adata.convert_channels))
382 ret = func_dpcm(priv, np, codec, li, is_top);
383 /* else normal sound */
385 ret = func_noml(priv, np, codec, li, is_top);
395 node = of_get_next_child(top, node);
396 } while (!is_top && node);
403 static int simple_parse_aux_devs(struct device_node *node,
404 struct asoc_simple_priv *priv)
406 struct device *dev = simple_priv_to_dev(priv);
407 struct device_node *aux_node;
408 struct snd_soc_card *card = simple_priv_to_card(priv);
411 if (!of_find_property(node, PREFIX "aux-devs", &len))
412 return 0; /* Ok to have no aux-devs */
414 n = len / sizeof(__be32);
418 card->aux_dev = devm_kcalloc(dev,
419 n, sizeof(*card->aux_dev), GFP_KERNEL);
423 for (i = 0; i < n; i++) {
424 aux_node = of_parse_phandle(node, PREFIX "aux-devs", i);
427 card->aux_dev[i].codec_of_node = aux_node;
430 card->num_aux_devs = n;
434 static int simple_parse_of(struct asoc_simple_priv *priv)
436 struct device *dev = simple_priv_to_dev(priv);
437 struct device_node *top = dev->of_node;
438 struct snd_soc_card *card = simple_priv_to_card(priv);
445 ret = asoc_simple_parse_widgets(card, PREFIX);
449 ret = asoc_simple_parse_routing(card, PREFIX);
453 ret = asoc_simple_parse_pin_switches(card, PREFIX);
457 /* Single/Muti DAI link(s) & New style of DT node */
458 memset(&li, 0, sizeof(li));
459 for (li.cpu = 1; li.cpu >= 0; li.cpu--) {
461 * Detect all CPU first, and Detect all Codec 2nd.
463 * In Normal sound case, all DAIs are detected
466 * In DPCM sound case,
467 * all CPUs are detected as "CPU-dummy", and
468 * all Codecs are detected as "dummy-Codec".
469 * To avoid random sub-device numbering,
470 * detect "dummy-Codec" in last;
472 ret = simple_for_each_link(priv, &li,
474 simple_dai_link_of_dpcm);
479 ret = asoc_simple_parse_card_name(card, PREFIX);
483 ret = simple_parse_aux_devs(top, priv);
488 static int simple_count_noml(struct asoc_simple_priv *priv,
489 struct device_node *np,
490 struct device_node *codec,
491 struct link_info *li, bool is_top)
493 li->dais++; /* CPU or Codec */
495 li->link++; /* CPU-Codec */
500 static int simple_count_dpcm(struct asoc_simple_priv *priv,
501 struct device_node *np,
502 struct device_node *codec,
503 struct link_info *li, bool is_top)
505 li->dais++; /* CPU or Codec */
506 li->link++; /* CPU-dummy or dummy-Codec */
513 static void simple_get_dais_count(struct asoc_simple_priv *priv,
514 struct link_info *li)
516 struct device *dev = simple_priv_to_dev(priv);
517 struct device_node *top = dev->of_node;
520 * link_num : number of links.
521 * CPU-Codec / CPU-dummy / dummy-Codec
522 * dais_num : number of DAIs
523 * ccnf_num : number of codec_conf
524 * same number for "dummy-Codec"
527 * CPU0 --- Codec0 link : 5
528 * CPU1 --- Codec1 dais : 7
532 * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec
533 * => 7 DAIs = 4xCPU + 3xCodec
534 * => 1 ccnf = 1xdummy-Codec
537 * CPU0 --- Codec0 link : 5
538 * CPU1 --- Codec1 dais : 6
542 * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec
543 * => 6 DAIs = 4xCPU + 2xCodec
544 * => 1 ccnf = 1xdummy-Codec
547 * CPU0 --- Codec0 link : 6
549 * CPU2 --- Codec1 ccnf : 2
552 * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec
553 * => 6 DAIs = 4xCPU + 2xCodec
554 * => 2 ccnf = 2xdummy-Codec
557 * CPU0 --- Codec0 (convert-rate) link : 3
558 * CPU1 --- Codec1 dais : 4
561 * => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec
562 * => 4 DAIs = 2xCPU + 2xCodec
563 * => 1 ccnf = 1xdummy-Codec
572 simple_for_each_link(priv, li,
576 dev_dbg(dev, "link %d, dais %d, ccnf %d\n",
577 li->link, li->dais, li->conf);
580 static int simple_soc_probe(struct snd_soc_card *card)
582 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(card);
585 ret = asoc_simple_init_hp(card, &priv->hp_jack, PREFIX);
589 ret = asoc_simple_init_mic(card, &priv->mic_jack, PREFIX);
596 static int asoc_simple_probe(struct platform_device *pdev)
598 struct asoc_simple_priv *priv;
599 struct device *dev = &pdev->dev;
600 struct device_node *np = dev->of_node;
601 struct snd_soc_card *card;
605 /* Allocate the private data and the DAI link array */
606 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
610 card = simple_priv_to_card(priv);
611 card->owner = THIS_MODULE;
613 card->probe = simple_soc_probe;
615 memset(&li, 0, sizeof(li));
616 simple_get_dais_count(priv, &li);
617 if (!li.link || !li.dais)
620 ret = asoc_simple_init_priv(priv, &li);
624 if (np && of_device_is_available(np)) {
626 ret = simple_parse_of(priv);
628 if (ret != -EPROBE_DEFER)
629 dev_err(dev, "parse error %d\n", ret);
634 struct asoc_simple_card_info *cinfo;
635 struct snd_soc_dai_link_component *cpus;
636 struct snd_soc_dai_link_component *codecs;
637 struct snd_soc_dai_link_component *platform;
638 struct snd_soc_dai_link *dai_link = priv->dai_link;
639 struct simple_dai_props *dai_props = priv->dai_props;
643 cinfo = dev->platform_data;
645 dev_err(dev, "no info for asoc-simple-card\n");
650 !cinfo->codec_dai.name ||
653 !cinfo->cpu_dai.name) {
654 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
658 dai_props->cpu_dai = &priv->dais[dai_idx++];
659 dai_props->codec_dai = &priv->dais[dai_idx++];
661 cpus = dai_link->cpus;
662 cpus->dai_name = cinfo->cpu_dai.name;
664 codecs = dai_link->codecs;
665 codecs->name = cinfo->codec;
666 codecs->dai_name = cinfo->codec_dai.name;
668 platform = dai_link->platforms;
669 platform->name = cinfo->platform;
671 card->name = (cinfo->card) ? cinfo->card : cinfo->name;
672 dai_link->name = cinfo->name;
673 dai_link->stream_name = cinfo->name;
674 dai_link->dai_fmt = cinfo->daifmt;
675 dai_link->init = asoc_simple_dai_init;
676 memcpy(dai_props->cpu_dai, &cinfo->cpu_dai,
677 sizeof(*dai_props->cpu_dai));
678 memcpy(dai_props->codec_dai, &cinfo->codec_dai,
679 sizeof(*dai_props->codec_dai));
682 snd_soc_card_set_drvdata(card, priv);
684 asoc_simple_debug_info(priv);
686 ret = devm_snd_soc_register_card(dev, card);
692 asoc_simple_clean_reference(card);
697 static int asoc_simple_remove(struct platform_device *pdev)
699 struct snd_soc_card *card = platform_get_drvdata(pdev);
701 return asoc_simple_clean_reference(card);
704 static const struct of_device_id simple_of_match[] = {
705 { .compatible = "simple-audio-card", },
706 { .compatible = "simple-scu-audio-card",
707 .data = (void *)DPCM_SELECTABLE },
710 MODULE_DEVICE_TABLE(of, simple_of_match);
712 static struct platform_driver asoc_simple_card = {
714 .name = "asoc-simple-card",
715 .pm = &snd_soc_pm_ops,
716 .of_match_table = simple_of_match,
718 .probe = asoc_simple_probe,
719 .remove = asoc_simple_remove,
722 module_platform_driver(asoc_simple_card);
724 MODULE_ALIAS("platform:asoc-simple-card");
725 MODULE_LICENSE("GPL v2");
726 MODULE_DESCRIPTION("ASoC Simple Sound Card");
727 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");