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/graph_card.h>
23 #define DPCM_SELECTABLE 1
25 static int graph_outdrv_event(struct snd_soc_dapm_widget
*w
,
26 struct snd_kcontrol
*kcontrol
,
29 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
30 struct asoc_simple_priv
*priv
= snd_soc_card_get_drvdata(dapm
->card
);
33 case SND_SOC_DAPM_POST_PMU
:
34 gpiod_set_value_cansleep(priv
->pa_gpio
, 1);
36 case SND_SOC_DAPM_PRE_PMD
:
37 gpiod_set_value_cansleep(priv
->pa_gpio
, 0);
46 static const struct snd_soc_dapm_widget graph_dapm_widgets
[] = {
47 SND_SOC_DAPM_OUT_DRV_E("Amplifier", SND_SOC_NOPM
,
48 0, 0, NULL
, 0, graph_outdrv_event
,
49 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
52 static const struct snd_soc_ops graph_ops
= {
53 .startup
= asoc_simple_startup
,
54 .shutdown
= asoc_simple_shutdown
,
55 .hw_params
= asoc_simple_hw_params
,
58 static int graph_get_dai_id(struct device_node
*ep
)
60 struct device_node
*node
;
61 struct device_node
*endpoint
;
62 struct of_endpoint info
;
67 /* use driver specified DAI ID if exist */
68 ret
= snd_soc_get_dai_id(ep
);
72 /* use endpoint/port reg if exist */
73 ret
= of_graph_parse_endpoint(ep
, &info
);
76 * Because it will count port/endpoint if it doesn't have "reg".
77 * But, we can't judge whether it has "no reg", or "reg = <0>"
78 * only of_graph_parse_endpoint().
79 * We need to check "reg" property
81 if (of_get_property(ep
, "reg", NULL
))
84 node
= of_get_parent(ep
);
85 reg
= of_get_property(node
, "reg", NULL
);
90 node
= of_graph_get_port_parent(ep
);
93 * Non HDMI sound case, counting port/endpoint on its DT
94 * is enough. Let's count it.
98 for_each_endpoint_of_node(node
, endpoint
) {
112 static bool soc_component_is_pcm(struct snd_soc_dai_link_component
*dlc
)
114 struct snd_soc_dai
*dai
= snd_soc_find_dai_with_mutex(dlc
);
116 if (dai
&& (dai
->component
->driver
->pcm_construct
||
117 dai
->driver
->pcm_new
))
123 static int asoc_simple_parse_dai(struct device_node
*ep
,
124 struct snd_soc_dai_link_component
*dlc
,
127 struct device_node
*node
;
128 struct of_phandle_args args
;
134 node
= of_graph_get_port_parent(ep
);
138 args
.args
[0] = graph_get_dai_id(ep
);
139 args
.args_count
= (of_graph_get_endpoint_count(node
) > 1);
144 * Here, dlc->dai_name is pointer to CPU/Codec DAI name.
145 * If user unbinded CPU or Codec driver, but not for Sound Card,
146 * dlc->dai_name is keeping unbinded CPU or Codec
149 * If user re-bind CPU or Codec driver again, ALSA SoC will try
150 * to rebind Card via snd_soc_try_rebind_card(), but because of
151 * above reason, it might can't bind Sound Card.
152 * Because Sound Card is pointing to released dai_name pointer.
154 * To avoid this rebind Card issue,
155 * 1) It needs to alloc memory to keep dai_name eventhough
156 * CPU or Codec driver was unbinded, or
157 * 2) user need to rebind Sound Card everytime
158 * if he unbinded CPU or Codec.
160 ret
= snd_soc_get_dai_name(&args
, &dlc
->dai_name
);
167 *is_single_link
= of_graph_get_endpoint_count(node
) == 1;
172 static void graph_parse_convert(struct device
*dev
,
173 struct device_node
*ep
,
174 struct asoc_simple_data
*adata
)
176 struct device_node
*top
= dev
->of_node
;
177 struct device_node
*port
= of_get_parent(ep
);
178 struct device_node
*ports
= of_get_parent(port
);
179 struct device_node
*node
= of_graph_get_port_parent(ep
);
181 asoc_simple_parse_convert(top
, NULL
, adata
);
182 if (of_node_name_eq(ports
, "ports"))
183 asoc_simple_parse_convert(ports
, NULL
, adata
);
184 asoc_simple_parse_convert(port
, NULL
, adata
);
185 asoc_simple_parse_convert(ep
, NULL
, adata
);
192 static void graph_parse_mclk_fs(struct device_node
*top
,
193 struct device_node
*ep
,
194 struct simple_dai_props
*props
)
196 struct device_node
*port
= of_get_parent(ep
);
197 struct device_node
*ports
= of_get_parent(port
);
199 of_property_read_u32(top
, "mclk-fs", &props
->mclk_fs
);
200 if (of_node_name_eq(ports
, "ports"))
201 of_property_read_u32(ports
, "mclk-fs", &props
->mclk_fs
);
202 of_property_read_u32(port
, "mclk-fs", &props
->mclk_fs
);
203 of_property_read_u32(ep
, "mclk-fs", &props
->mclk_fs
);
209 static int graph_parse_node(struct asoc_simple_priv
*priv
,
210 struct device_node
*ep
,
211 struct link_info
*li
,
214 struct device
*dev
= simple_priv_to_dev(priv
);
215 struct device_node
*top
= dev
->of_node
;
216 struct snd_soc_dai_link
*dai_link
= simple_priv_to_link(priv
, li
->link
);
217 struct simple_dai_props
*dai_props
= simple_priv_to_props(priv
, li
->link
);
218 struct snd_soc_dai_link_component
*dlc
;
219 struct asoc_simple_dai
*dai
;
223 dlc
= asoc_link_to_cpu(dai_link
, 0);
224 dai
= simple_props_to_dai_cpu(dai_props
, 0);
226 dlc
= asoc_link_to_codec(dai_link
, 0);
227 dai
= simple_props_to_dai_codec(dai_props
, 0);
230 graph_parse_mclk_fs(top
, ep
, dai_props
);
232 ret
= asoc_simple_parse_dai(ep
, dlc
, &single
);
236 ret
= asoc_simple_parse_tdm(ep
, dai
);
240 ret
= asoc_simple_parse_clk(dev
, ep
, dai
, dlc
);
245 asoc_simple_canonicalize_cpu(dlc
, single
);
250 static int graph_link_init(struct asoc_simple_priv
*priv
,
251 struct device_node
*cpu_ep
,
252 struct device_node
*codec_ep
,
253 struct link_info
*li
,
256 struct device
*dev
= simple_priv_to_dev(priv
);
257 struct snd_soc_dai_link
*dai_link
= simple_priv_to_link(priv
, li
->link
);
260 ret
= asoc_simple_parse_daifmt(dev
, cpu_ep
, codec_ep
,
261 NULL
, &dai_link
->dai_fmt
);
265 dai_link
->init
= asoc_simple_dai_init
;
266 dai_link
->ops
= &graph_ops
;
268 dai_link
->ops
= priv
->ops
;
270 return asoc_simple_set_dailink_name(dev
, dai_link
, name
);
273 static int graph_dai_link_of_dpcm(struct asoc_simple_priv
*priv
,
274 struct device_node
*cpu_ep
,
275 struct device_node
*codec_ep
,
276 struct link_info
*li
)
278 struct device
*dev
= simple_priv_to_dev(priv
);
279 struct snd_soc_dai_link
*dai_link
= simple_priv_to_link(priv
, li
->link
);
280 struct simple_dai_props
*dai_props
= simple_priv_to_props(priv
, li
->link
);
281 struct device_node
*top
= dev
->of_node
;
282 struct device_node
*ep
= li
->cpu
? cpu_ep
: codec_ep
;
286 dev_dbg(dev
, "link_of DPCM (%pOF)\n", ep
);
289 struct snd_soc_card
*card
= simple_priv_to_card(priv
);
290 struct snd_soc_dai_link_component
*cpus
= asoc_link_to_cpu(dai_link
, 0);
295 dai_link
->dynamic
= 1;
296 dai_link
->dpcm_merged_format
= 1;
298 ret
= graph_parse_node(priv
, cpu_ep
, li
, 1);
302 snprintf(dai_name
, sizeof(dai_name
),
303 "fe.%pOFP.%s", cpus
->of_node
, cpus
->dai_name
);
305 * In BE<->BE connections it is not required to create
306 * PCM devices at CPU end of the dai link and thus 'no_pcm'
307 * flag needs to be set. It is useful when there are many
308 * BE components and some of these have to be connected to
309 * form a valid audio path.
311 * For example: FE <-> BE1 <-> BE2 <-> ... <-> BEn where
312 * there are 'n' BE components in the path.
314 if (card
->component_chaining
&& !soc_component_is_pcm(cpus
))
315 dai_link
->no_pcm
= 1;
317 struct snd_soc_codec_conf
*cconf
= simple_props_to_codec_conf(dai_props
, 0);
318 struct snd_soc_dai_link_component
*codecs
= asoc_link_to_codec(dai_link
, 0);
319 struct device_node
*port
;
320 struct device_node
*ports
;
325 dai_link
->no_pcm
= 1;
326 dai_link
->be_hw_params_fixup
= asoc_simple_be_hw_params_fixup
;
328 ret
= graph_parse_node(priv
, codec_ep
, li
, 0);
332 snprintf(dai_name
, sizeof(dai_name
),
333 "be.%pOFP.%s", codecs
->of_node
, codecs
->dai_name
);
335 /* check "prefix" from top node */
336 port
= of_get_parent(ep
);
337 ports
= of_get_parent(port
);
338 snd_soc_of_parse_node_prefix(top
, cconf
, codecs
->of_node
,
340 if (of_node_name_eq(ports
, "ports"))
341 snd_soc_of_parse_node_prefix(ports
, cconf
, codecs
->of_node
, "prefix");
342 snd_soc_of_parse_node_prefix(port
, cconf
, codecs
->of_node
,
349 graph_parse_convert(dev
, ep
, &dai_props
->adata
);
351 snd_soc_dai_link_set_capabilities(dai_link
);
353 ret
= graph_link_init(priv
, cpu_ep
, codec_ep
, li
, dai_name
);
360 static int graph_dai_link_of(struct asoc_simple_priv
*priv
,
361 struct device_node
*cpu_ep
,
362 struct device_node
*codec_ep
,
363 struct link_info
*li
)
365 struct device
*dev
= simple_priv_to_dev(priv
);
366 struct snd_soc_dai_link
*dai_link
= simple_priv_to_link(priv
, li
->link
);
367 struct snd_soc_dai_link_component
*cpus
= asoc_link_to_cpu(dai_link
, 0);
368 struct snd_soc_dai_link_component
*codecs
= asoc_link_to_codec(dai_link
, 0);
372 dev_dbg(dev
, "link_of (%pOF)\n", cpu_ep
);
374 ret
= graph_parse_node(priv
, cpu_ep
, li
, 1);
378 ret
= graph_parse_node(priv
, codec_ep
, li
, 0);
382 snprintf(dai_name
, sizeof(dai_name
),
383 "%s-%s", cpus
->dai_name
, codecs
->dai_name
);
384 ret
= graph_link_init(priv
, cpu_ep
, codec_ep
, li
, dai_name
);
393 static inline bool parse_as_dpcm_link(struct asoc_simple_priv
*priv
,
394 struct device_node
*codec_port
,
395 struct asoc_simple_data
*adata
)
397 if (priv
->force_dpcm
)
400 if (!priv
->dpcm_selectable
)
405 * if Codec port has many endpoints,
406 * or has convert-xxx property
408 if ((of_get_child_count(codec_port
) > 1) ||
409 (adata
->convert_rate
|| adata
->convert_channels
))
415 static int __graph_for_each_link(struct asoc_simple_priv
*priv
,
416 struct link_info
*li
,
417 int (*func_noml
)(struct asoc_simple_priv
*priv
,
418 struct device_node
*cpu_ep
,
419 struct device_node
*codec_ep
,
420 struct link_info
*li
),
421 int (*func_dpcm
)(struct asoc_simple_priv
*priv
,
422 struct device_node
*cpu_ep
,
423 struct device_node
*codec_ep
,
424 struct link_info
*li
))
426 struct of_phandle_iterator it
;
427 struct device
*dev
= simple_priv_to_dev(priv
);
428 struct device_node
*node
= dev
->of_node
;
429 struct device_node
*cpu_port
;
430 struct device_node
*cpu_ep
;
431 struct device_node
*codec_ep
;
432 struct device_node
*codec_port
;
433 struct device_node
*codec_port_old
= NULL
;
434 struct asoc_simple_data adata
;
437 /* loop for all listed CPU port */
438 of_for_each_phandle(&it
, rc
, node
, "dais", NULL
, 0) {
442 /* loop for all CPU endpoint */
444 cpu_ep
= of_get_next_child(cpu_port
, cpu_ep
);
449 codec_ep
= of_graph_get_remote_endpoint(cpu_ep
);
450 codec_port
= of_get_parent(codec_ep
);
452 /* get convert-xxx property */
453 memset(&adata
, 0, sizeof(adata
));
454 graph_parse_convert(dev
, codec_ep
, &adata
);
455 graph_parse_convert(dev
, cpu_ep
, &adata
);
457 /* check if link requires DPCM parsing */
458 if (parse_as_dpcm_link(priv
, codec_port
, &adata
)) {
460 * Codec endpoint can be NULL for pluggable audio HW.
461 * Platform DT can populate the Codec endpoint depending on the
464 /* Do it all CPU endpoint, and 1st Codec endpoint */
466 ((codec_port_old
!= codec_port
) && codec_ep
))
467 ret
= func_dpcm(priv
, cpu_ep
, codec_ep
, li
);
468 /* else normal sound */
471 ret
= func_noml(priv
, cpu_ep
, codec_ep
, li
);
474 of_node_put(codec_ep
);
475 of_node_put(codec_port
);
480 codec_port_old
= codec_port
;
487 static int graph_for_each_link(struct asoc_simple_priv
*priv
,
488 struct link_info
*li
,
489 int (*func_noml
)(struct asoc_simple_priv
*priv
,
490 struct device_node
*cpu_ep
,
491 struct device_node
*codec_ep
,
492 struct link_info
*li
),
493 int (*func_dpcm
)(struct asoc_simple_priv
*priv
,
494 struct device_node
*cpu_ep
,
495 struct device_node
*codec_ep
,
496 struct link_info
*li
))
500 * Detect all CPU first, and Detect all Codec 2nd.
502 * In Normal sound case, all DAIs are detected
505 * In DPCM sound case,
506 * all CPUs are detected as "CPU-dummy", and
507 * all Codecs are detected as "dummy-Codec".
508 * To avoid random sub-device numbering,
509 * detect "dummy-Codec" in last;
511 for (li
->cpu
= 1; li
->cpu
>= 0; li
->cpu
--) {
512 ret
= __graph_for_each_link(priv
, li
, func_noml
, func_dpcm
);
520 static int graph_get_dais_count(struct asoc_simple_priv
*priv
,
521 struct link_info
*li
);
523 int audio_graph_parse_of(struct asoc_simple_priv
*priv
, struct device
*dev
)
525 struct snd_soc_card
*card
= simple_priv_to_card(priv
);
526 struct link_info
*li
;
529 li
= devm_kzalloc(dev
, sizeof(*li
), GFP_KERNEL
);
533 card
->owner
= THIS_MODULE
;
536 ret
= graph_get_dais_count(priv
, li
);
543 ret
= asoc_simple_init_priv(priv
, li
);
547 priv
->pa_gpio
= devm_gpiod_get_optional(dev
, "pa", GPIOD_OUT_LOW
);
548 if (IS_ERR(priv
->pa_gpio
)) {
549 ret
= PTR_ERR(priv
->pa_gpio
);
550 dev_err(dev
, "failed to get amplifier gpio: %d\n", ret
);
554 ret
= asoc_simple_parse_widgets(card
, NULL
);
558 ret
= asoc_simple_parse_routing(card
, NULL
);
562 memset(li
, 0, sizeof(*li
));
563 ret
= graph_for_each_link(priv
, li
,
565 graph_dai_link_of_dpcm
);
569 ret
= asoc_simple_parse_card_name(card
, NULL
);
573 snd_soc_card_set_drvdata(card
, priv
);
575 asoc_simple_debug_info(priv
);
577 ret
= devm_snd_soc_register_card(dev
, card
);
585 asoc_simple_clean_reference(card
);
587 if (ret
!= -EPROBE_DEFER
)
588 dev_err(dev
, "parse error %d\n", ret
);
592 EXPORT_SYMBOL_GPL(audio_graph_parse_of
);
594 static int graph_count_noml(struct asoc_simple_priv
*priv
,
595 struct device_node
*cpu_ep
,
596 struct device_node
*codec_ep
,
597 struct link_info
*li
)
599 struct device
*dev
= simple_priv_to_dev(priv
);
601 if (li
->link
>= SNDRV_MAX_LINKS
) {
602 dev_err(dev
, "too many links\n");
606 li
->num
[li
->link
].cpus
= 1;
607 li
->num
[li
->link
].codecs
= 1;
609 li
->link
+= 1; /* 1xCPU-Codec */
611 dev_dbg(dev
, "Count As Normal\n");
616 static int graph_count_dpcm(struct asoc_simple_priv
*priv
,
617 struct device_node
*cpu_ep
,
618 struct device_node
*codec_ep
,
619 struct link_info
*li
)
621 struct device
*dev
= simple_priv_to_dev(priv
);
623 if (li
->link
>= SNDRV_MAX_LINKS
) {
624 dev_err(dev
, "too many links\n");
629 li
->num
[li
->link
].cpus
= 1;
631 li
->link
++; /* 1xCPU-dummy */
633 li
->num
[li
->link
].codecs
= 1;
635 li
->link
++; /* 1xdummy-Codec */
638 dev_dbg(dev
, "Count As DPCM\n");
643 static int graph_get_dais_count(struct asoc_simple_priv
*priv
,
644 struct link_info
*li
)
647 * link_num : number of links.
648 * CPU-Codec / CPU-dummy / dummy-Codec
649 * dais_num : number of DAIs
650 * ccnf_num : number of codec_conf
651 * same number for "dummy-Codec"
654 * CPU0 --- Codec0 link : 5
655 * CPU1 --- Codec1 dais : 7
659 * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec
660 * => 7 DAIs = 4xCPU + 3xCodec
661 * => 1 ccnf = 1xdummy-Codec
664 * CPU0 --- Codec0 link : 5
665 * CPU1 --- Codec1 dais : 6
669 * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec
670 * => 6 DAIs = 4xCPU + 2xCodec
671 * => 1 ccnf = 1xdummy-Codec
674 * CPU0 --- Codec0 link : 6
676 * CPU2 --- Codec1 ccnf : 2
679 * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec
680 * => 6 DAIs = 4xCPU + 2xCodec
681 * => 2 ccnf = 2xdummy-Codec
684 * CPU0 --- Codec0 (convert-rate) link : 3
685 * CPU1 --- Codec1 dais : 4
688 * => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec
689 * => 4 DAIs = 2xCPU + 2xCodec
690 * => 1 ccnf = 1xdummy-Codec
692 return graph_for_each_link(priv
, li
,
697 static int graph_probe(struct platform_device
*pdev
)
699 struct asoc_simple_priv
*priv
;
700 struct device
*dev
= &pdev
->dev
;
701 struct snd_soc_card
*card
;
703 /* Allocate the private data and the DAI link array */
704 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
708 card
= simple_priv_to_card(priv
);
709 card
->dapm_widgets
= graph_dapm_widgets
;
710 card
->num_dapm_widgets
= ARRAY_SIZE(graph_dapm_widgets
);
711 card
->probe
= asoc_graph_card_probe
;
713 if (of_device_get_match_data(dev
))
714 priv
->dpcm_selectable
= 1;
716 return audio_graph_parse_of(priv
, dev
);
719 static const struct of_device_id graph_of_match
[] = {
720 { .compatible
= "audio-graph-card", },
721 { .compatible
= "audio-graph-scu-card",
722 .data
= (void *)DPCM_SELECTABLE
},
725 MODULE_DEVICE_TABLE(of
, graph_of_match
);
727 static struct platform_driver graph_card
= {
729 .name
= "asoc-audio-graph-card",
730 .pm
= &snd_soc_pm_ops
,
731 .of_match_table
= graph_of_match
,
733 .probe
= graph_probe
,
734 .remove
= asoc_simple_remove
,
736 module_platform_driver(graph_card
);
738 MODULE_ALIAS("platform:asoc-audio-graph-card");
739 MODULE_LICENSE("GPL v2");
740 MODULE_DESCRIPTION("ASoC Audio Graph Sound Card");
741 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");