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
);
52 * Here, dlc->dai_name is pointer to CPU/Codec DAI name.
53 * If user unbinded CPU or Codec driver, but not for Sound Card,
54 * dlc->dai_name is keeping unbinded CPU or Codec
57 * If user re-bind CPU or Codec driver again, ALSA SoC will try
58 * to rebind Card via snd_soc_try_rebind_card(), but because of
59 * above reason, it might can't bind Sound Card.
60 * Because Sound Card is pointing to released dai_name pointer.
62 * To avoid this rebind Card issue,
63 * 1) It needs to alloc memory to keep dai_name eventhough
64 * CPU or Codec driver was unbinded, or
65 * 2) user need to rebind Sound Card everytime
66 * if he unbinded CPU or Codec.
68 ret
= snd_soc_of_get_dai_name(node
, &dlc
->dai_name
);
72 dlc
->of_node
= args
.np
;
75 *is_single_link
= !args
.args_count
;
80 static void simple_parse_convert(struct device
*dev
,
81 struct device_node
*np
,
82 struct asoc_simple_data
*adata
)
84 struct device_node
*top
= dev
->of_node
;
85 struct device_node
*node
= of_get_parent(np
);
87 asoc_simple_parse_convert(dev
, top
, PREFIX
, adata
);
88 asoc_simple_parse_convert(dev
, node
, PREFIX
, adata
);
89 asoc_simple_parse_convert(dev
, node
, NULL
, adata
);
90 asoc_simple_parse_convert(dev
, np
, NULL
, adata
);
95 static void simple_parse_mclk_fs(struct device_node
*top
,
96 struct device_node
*cpu
,
97 struct device_node
*codec
,
98 struct simple_dai_props
*props
,
101 struct device_node
*node
= of_get_parent(cpu
);
104 snprintf(prop
, sizeof(prop
), "%smclk-fs", PREFIX
);
105 of_property_read_u32(top
, prop
, &props
->mclk_fs
);
107 snprintf(prop
, sizeof(prop
), "%smclk-fs", prefix
);
108 of_property_read_u32(node
, prop
, &props
->mclk_fs
);
109 of_property_read_u32(cpu
, prop
, &props
->mclk_fs
);
110 of_property_read_u32(codec
, prop
, &props
->mclk_fs
);
115 static int simple_dai_link_of_dpcm(struct asoc_simple_priv
*priv
,
116 struct device_node
*np
,
117 struct device_node
*codec
,
118 struct link_info
*li
,
121 struct device
*dev
= simple_priv_to_dev(priv
);
122 struct snd_soc_dai_link
*dai_link
= simple_priv_to_link(priv
, li
->link
);
123 struct simple_dai_props
*dai_props
= simple_priv_to_props(priv
, li
->link
);
124 struct asoc_simple_dai
*dai
;
125 struct snd_soc_dai_link_component
*cpus
= dai_link
->cpus
;
126 struct snd_soc_dai_link_component
*codecs
= dai_link
->codecs
;
127 struct device_node
*top
= dev
->of_node
;
128 struct device_node
*node
= of_get_parent(np
);
138 if (li
->cpu
== (np
== codec
))
141 dev_dbg(dev
, "link_of DPCM (%pOF)\n", np
);
145 /* For single DAI link & old style of DT node */
150 int is_single_links
= 0;
153 codecs
->of_node
= NULL
;
154 codecs
->dai_name
= "snd-soc-dummy-dai";
155 codecs
->name
= "snd-soc-dummy";
158 dai_link
->dynamic
= 1;
159 dai_link
->dpcm_merged_format
= 1;
162 dai_props
->cpu_dai
= &priv
->dais
[li
->dais
++];
164 ret
= asoc_simple_parse_cpu(np
, dai_link
, &is_single_links
);
168 ret
= asoc_simple_parse_clk_cpu(dev
, np
, dai_link
, dai
);
172 ret
= asoc_simple_set_dailink_name(dev
, dai_link
,
178 asoc_simple_canonicalize_cpu(dai_link
, is_single_links
);
180 struct snd_soc_codec_conf
*cconf
;
183 cpus
->of_node
= NULL
;
184 cpus
->dai_name
= "snd-soc-dummy-dai";
185 cpus
->name
= "snd-soc-dummy";
188 dai_link
->no_pcm
= 1;
189 dai_link
->be_hw_params_fixup
= asoc_simple_be_hw_params_fixup
;
192 dai_props
->codec_dai
= &priv
->dais
[li
->dais
++];
195 dai_props
->codec_conf
= &priv
->codec_conf
[li
->conf
++];
197 ret
= asoc_simple_parse_codec(np
, dai_link
);
201 ret
= asoc_simple_parse_clk_codec(dev
, np
, dai_link
, dai
);
205 ret
= asoc_simple_set_dailink_name(dev
, dai_link
,
211 /* check "prefix" from top node */
212 snd_soc_of_parse_node_prefix(top
, cconf
, codecs
->of_node
,
214 snd_soc_of_parse_node_prefix(node
, cconf
, codecs
->of_node
,
216 snd_soc_of_parse_node_prefix(np
, cconf
, codecs
->of_node
,
220 simple_parse_convert(dev
, np
, &dai_props
->adata
);
221 simple_parse_mclk_fs(top
, np
, codec
, dai_props
, prefix
);
223 asoc_simple_canonicalize_platform(dai_link
);
225 ret
= asoc_simple_parse_tdm(np
, dai
);
229 ret
= asoc_simple_parse_daifmt(dev
, node
, codec
,
230 prefix
, &dai_link
->dai_fmt
);
234 dai_link
->dpcm_playback
= 1;
235 dai_link
->dpcm_capture
= 1;
236 dai_link
->ops
= &simple_ops
;
237 dai_link
->init
= asoc_simple_dai_init
;
244 static int simple_dai_link_of(struct asoc_simple_priv
*priv
,
245 struct device_node
*np
,
246 struct device_node
*codec
,
247 struct link_info
*li
,
250 struct device
*dev
= simple_priv_to_dev(priv
);
251 struct snd_soc_dai_link
*dai_link
= simple_priv_to_link(priv
, li
->link
);
252 struct simple_dai_props
*dai_props
= simple_priv_to_props(priv
, li
->link
);
253 struct asoc_simple_dai
*cpu_dai
;
254 struct asoc_simple_dai
*codec_dai
;
255 struct device_node
*top
= dev
->of_node
;
256 struct device_node
*cpu
= NULL
;
257 struct device_node
*node
= NULL
;
258 struct device_node
*plat
= NULL
;
266 * Codec |return|return
269 if (!li
->cpu
|| np
== codec
)
273 node
= of_get_parent(np
);
276 dev_dbg(dev
, "link_of (%pOF)\n", node
);
278 /* For single DAI link & old style of DT node */
282 snprintf(prop
, sizeof(prop
), "%splat", prefix
);
283 plat
= of_get_child_by_name(node
, prop
);
286 dai_props
->cpu_dai
= &priv
->dais
[li
->dais
++];
288 dai_props
->codec_dai
= &priv
->dais
[li
->dais
++];
290 ret
= asoc_simple_parse_daifmt(dev
, node
, codec
,
291 prefix
, &dai_link
->dai_fmt
);
293 goto dai_link_of_err
;
295 simple_parse_mclk_fs(top
, cpu
, codec
, dai_props
, prefix
);
297 ret
= asoc_simple_parse_cpu(cpu
, dai_link
, &single_cpu
);
299 goto dai_link_of_err
;
301 ret
= asoc_simple_parse_codec(codec
, dai_link
);
303 goto dai_link_of_err
;
305 ret
= asoc_simple_parse_platform(plat
, dai_link
);
307 goto dai_link_of_err
;
309 ret
= asoc_simple_parse_tdm(cpu
, cpu_dai
);
311 goto dai_link_of_err
;
313 ret
= asoc_simple_parse_tdm(codec
, codec_dai
);
315 goto dai_link_of_err
;
317 ret
= asoc_simple_parse_clk_cpu(dev
, cpu
, dai_link
, cpu_dai
);
319 goto dai_link_of_err
;
321 ret
= asoc_simple_parse_clk_codec(dev
, codec
, dai_link
, codec_dai
);
323 goto dai_link_of_err
;
325 ret
= asoc_simple_set_dailink_name(dev
, dai_link
,
327 dai_link
->cpus
->dai_name
,
328 dai_link
->codecs
->dai_name
);
330 goto dai_link_of_err
;
332 dai_link
->ops
= &simple_ops
;
333 dai_link
->init
= asoc_simple_dai_init
;
335 asoc_simple_canonicalize_cpu(dai_link
, single_cpu
);
336 asoc_simple_canonicalize_platform(dai_link
);
345 static int simple_for_each_link(struct asoc_simple_priv
*priv
,
346 struct link_info
*li
,
347 int (*func_noml
)(struct asoc_simple_priv
*priv
,
348 struct device_node
*np
,
349 struct device_node
*codec
,
350 struct link_info
*li
, bool is_top
),
351 int (*func_dpcm
)(struct asoc_simple_priv
*priv
,
352 struct device_node
*np
,
353 struct device_node
*codec
,
354 struct link_info
*li
, bool is_top
))
356 struct device
*dev
= simple_priv_to_dev(priv
);
357 struct device_node
*top
= dev
->of_node
;
358 struct device_node
*node
;
359 uintptr_t dpcm_selectable
= (uintptr_t)of_device_get_match_data(dev
);
363 /* Check if it has dai-link */
364 node
= of_get_child_by_name(top
, PREFIX
"dai-link");
366 node
= of_node_get(top
);
370 /* loop for all dai-link */
372 struct asoc_simple_data adata
;
373 struct device_node
*codec
;
374 struct device_node
*plat
;
375 struct device_node
*np
;
376 int num
= of_get_child_count(node
);
379 codec
= of_get_child_by_name(node
, is_top
?
380 PREFIX
"codec" : "codec");
386 plat
= of_get_child_by_name(node
, is_top
?
387 PREFIX
"plat" : "plat");
389 /* get convert-xxx property */
390 memset(&adata
, 0, sizeof(adata
));
391 for_each_child_of_node(node
, np
)
392 simple_parse_convert(dev
, np
, &adata
);
394 /* loop for all CPU/Codec node */
395 for_each_child_of_node(node
, np
) {
400 * if it has many CPUs,
401 * or has convert-xxx property
403 if (dpcm_selectable
&&
405 adata
.convert_rate
|| adata
.convert_channels
))
406 ret
= func_dpcm(priv
, np
, codec
, li
, is_top
);
407 /* else normal sound */
409 ret
= func_noml(priv
, np
, codec
, li
, is_top
);
419 node
= of_get_next_child(top
, node
);
420 } while (!is_top
&& node
);
427 static int simple_parse_aux_devs(struct device_node
*node
,
428 struct asoc_simple_priv
*priv
)
430 struct device
*dev
= simple_priv_to_dev(priv
);
431 struct device_node
*aux_node
;
432 struct snd_soc_card
*card
= simple_priv_to_card(priv
);
435 if (!of_find_property(node
, PREFIX
"aux-devs", &len
))
436 return 0; /* Ok to have no aux-devs */
438 n
= len
/ sizeof(__be32
);
442 card
->aux_dev
= devm_kcalloc(dev
,
443 n
, sizeof(*card
->aux_dev
), GFP_KERNEL
);
447 for (i
= 0; i
< n
; i
++) {
448 aux_node
= of_parse_phandle(node
, PREFIX
"aux-devs", i
);
451 card
->aux_dev
[i
].dlc
.of_node
= aux_node
;
454 card
->num_aux_devs
= n
;
458 static int simple_parse_of(struct asoc_simple_priv
*priv
)
460 struct device
*dev
= simple_priv_to_dev(priv
);
461 struct device_node
*top
= dev
->of_node
;
462 struct snd_soc_card
*card
= simple_priv_to_card(priv
);
469 ret
= asoc_simple_parse_widgets(card
, PREFIX
);
473 ret
= asoc_simple_parse_routing(card
, PREFIX
);
477 ret
= asoc_simple_parse_pin_switches(card
, PREFIX
);
481 /* Single/Muti DAI link(s) & New style of DT node */
482 memset(&li
, 0, sizeof(li
));
483 for (li
.cpu
= 1; li
.cpu
>= 0; li
.cpu
--) {
485 * Detect all CPU first, and Detect all Codec 2nd.
487 * In Normal sound case, all DAIs are detected
490 * In DPCM sound case,
491 * all CPUs are detected as "CPU-dummy", and
492 * all Codecs are detected as "dummy-Codec".
493 * To avoid random sub-device numbering,
494 * detect "dummy-Codec" in last;
496 ret
= simple_for_each_link(priv
, &li
,
498 simple_dai_link_of_dpcm
);
503 ret
= asoc_simple_parse_card_name(card
, PREFIX
);
507 ret
= simple_parse_aux_devs(top
, priv
);
512 static int simple_count_noml(struct asoc_simple_priv
*priv
,
513 struct device_node
*np
,
514 struct device_node
*codec
,
515 struct link_info
*li
, bool is_top
)
517 li
->dais
++; /* CPU or Codec */
519 li
->link
++; /* CPU-Codec */
524 static int simple_count_dpcm(struct asoc_simple_priv
*priv
,
525 struct device_node
*np
,
526 struct device_node
*codec
,
527 struct link_info
*li
, bool is_top
)
529 li
->dais
++; /* CPU or Codec */
530 li
->link
++; /* CPU-dummy or dummy-Codec */
537 static void simple_get_dais_count(struct asoc_simple_priv
*priv
,
538 struct link_info
*li
)
540 struct device
*dev
= simple_priv_to_dev(priv
);
541 struct device_node
*top
= dev
->of_node
;
544 * link_num : number of links.
545 * CPU-Codec / CPU-dummy / dummy-Codec
546 * dais_num : number of DAIs
547 * ccnf_num : number of codec_conf
548 * same number for "dummy-Codec"
551 * CPU0 --- Codec0 link : 5
552 * CPU1 --- Codec1 dais : 7
556 * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec
557 * => 7 DAIs = 4xCPU + 3xCodec
558 * => 1 ccnf = 1xdummy-Codec
561 * CPU0 --- Codec0 link : 5
562 * CPU1 --- Codec1 dais : 6
566 * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec
567 * => 6 DAIs = 4xCPU + 2xCodec
568 * => 1 ccnf = 1xdummy-Codec
571 * CPU0 --- Codec0 link : 6
573 * CPU2 --- Codec1 ccnf : 2
576 * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec
577 * => 6 DAIs = 4xCPU + 2xCodec
578 * => 2 ccnf = 2xdummy-Codec
581 * CPU0 --- Codec0 (convert-rate) link : 3
582 * CPU1 --- Codec1 dais : 4
585 * => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec
586 * => 4 DAIs = 2xCPU + 2xCodec
587 * => 1 ccnf = 1xdummy-Codec
596 simple_for_each_link(priv
, li
,
600 dev_dbg(dev
, "link %d, dais %d, ccnf %d\n",
601 li
->link
, li
->dais
, li
->conf
);
604 static int simple_soc_probe(struct snd_soc_card
*card
)
606 struct asoc_simple_priv
*priv
= snd_soc_card_get_drvdata(card
);
609 ret
= asoc_simple_init_hp(card
, &priv
->hp_jack
, PREFIX
);
613 ret
= asoc_simple_init_mic(card
, &priv
->mic_jack
, PREFIX
);
620 static int asoc_simple_probe(struct platform_device
*pdev
)
622 struct asoc_simple_priv
*priv
;
623 struct device
*dev
= &pdev
->dev
;
624 struct device_node
*np
= dev
->of_node
;
625 struct snd_soc_card
*card
;
629 /* Allocate the private data and the DAI link array */
630 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
634 card
= simple_priv_to_card(priv
);
635 card
->owner
= THIS_MODULE
;
637 card
->probe
= simple_soc_probe
;
639 memset(&li
, 0, sizeof(li
));
640 simple_get_dais_count(priv
, &li
);
641 if (!li
.link
|| !li
.dais
)
644 ret
= asoc_simple_init_priv(priv
, &li
);
648 if (np
&& of_device_is_available(np
)) {
650 ret
= simple_parse_of(priv
);
652 if (ret
!= -EPROBE_DEFER
)
653 dev_err(dev
, "parse error %d\n", ret
);
658 struct asoc_simple_card_info
*cinfo
;
659 struct snd_soc_dai_link_component
*cpus
;
660 struct snd_soc_dai_link_component
*codecs
;
661 struct snd_soc_dai_link_component
*platform
;
662 struct snd_soc_dai_link
*dai_link
= priv
->dai_link
;
663 struct simple_dai_props
*dai_props
= priv
->dai_props
;
667 cinfo
= dev
->platform_data
;
669 dev_err(dev
, "no info for asoc-simple-card\n");
674 !cinfo
->codec_dai
.name
||
677 !cinfo
->cpu_dai
.name
) {
678 dev_err(dev
, "insufficient asoc_simple_card_info settings\n");
682 dai_props
->cpu_dai
= &priv
->dais
[dai_idx
++];
683 dai_props
->codec_dai
= &priv
->dais
[dai_idx
++];
685 cpus
= dai_link
->cpus
;
686 cpus
->dai_name
= cinfo
->cpu_dai
.name
;
688 codecs
= dai_link
->codecs
;
689 codecs
->name
= cinfo
->codec
;
690 codecs
->dai_name
= cinfo
->codec_dai
.name
;
692 platform
= dai_link
->platforms
;
693 platform
->name
= cinfo
->platform
;
695 card
->name
= (cinfo
->card
) ? cinfo
->card
: cinfo
->name
;
696 dai_link
->name
= cinfo
->name
;
697 dai_link
->stream_name
= cinfo
->name
;
698 dai_link
->dai_fmt
= cinfo
->daifmt
;
699 dai_link
->init
= asoc_simple_dai_init
;
700 memcpy(dai_props
->cpu_dai
, &cinfo
->cpu_dai
,
701 sizeof(*dai_props
->cpu_dai
));
702 memcpy(dai_props
->codec_dai
, &cinfo
->codec_dai
,
703 sizeof(*dai_props
->codec_dai
));
706 snd_soc_card_set_drvdata(card
, priv
);
708 asoc_simple_debug_info(priv
);
710 ret
= devm_snd_soc_register_card(dev
, card
);
716 asoc_simple_clean_reference(card
);
721 static int asoc_simple_remove(struct platform_device
*pdev
)
723 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
725 return asoc_simple_clean_reference(card
);
728 static const struct of_device_id simple_of_match
[] = {
729 { .compatible
= "simple-audio-card", },
730 { .compatible
= "simple-scu-audio-card",
731 .data
= (void *)DPCM_SELECTABLE
},
734 MODULE_DEVICE_TABLE(of
, simple_of_match
);
736 static struct platform_driver asoc_simple_card
= {
738 .name
= "asoc-simple-card",
739 .pm
= &snd_soc_pm_ops
,
740 .of_match_table
= simple_of_match
,
742 .probe
= asoc_simple_probe
,
743 .remove
= asoc_simple_remove
,
746 module_platform_driver(asoc_simple_card
);
748 MODULE_ALIAS("platform:asoc-simple-card");
749 MODULE_LICENSE("GPL v2");
750 MODULE_DESCRIPTION("ASoC Simple Sound Card");
751 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");