1 // SPDX-License-Identifier: GPL-2.0
3 // ASoC Audio Graph Card2 support
5 // Copyright (C) 2020 Renesas Electronics Corp.
8 // based on ${LINUX}/sound/soc/generic/audio-graph-card.c
10 #include <linux/device.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/module.h>
14 #include <linux/of_graph.h>
15 #include <linux/platform_device.h>
16 #include <linux/string.h>
17 #include <sound/graph_card.h>
19 /************************************
21 ************************************
36 You can set daifmt at ports/port/endpoint.
37 It uses *latest* format, and *share* master settings.
39 sample0: left_j, bitclock-master, frame-master
40 sample1: i2s, bitclock-master
42 If there was no settings, *Codec* will be
43 bitclock/frame provider as default.
47 "format" property is no longer needed on DT if both CPU/Codec drivers are
48 supporting snd_soc_dai_ops :: .auto_selectable_formats.
50 snd_soc_runtime_get_dai_fmt()
53 linux/sound/soc/sh/rcar/core.c
54 linux/sound/soc/codecs/ak4613.c
55 linux/sound/soc/codecs/pcm3168a.c
56 linux/sound/soc/soc-utils.c
57 linux/sound/soc/generic/test-component.c
59 ************************************
61 ************************************
66 compatible = "audio-graph-card2";
74 cpu_ep: endpoint { remote-endpoint = <&codec_ep>; }; };
78 port { codec_ep: endpoint { remote-endpoint = <&cpu_ep>; }; };
81 ************************************
83 ************************************
85 It has link connection part (= X,x) and list part (= A,B,a,b).
86 "links" is connection part of CPU side (= @).
89 CPU1 --|A X| <-@----> |x a|-- Codec1
90 CPU2 --|B | | b|-- Codec2
94 compatible = "audio-graph-card2";
100 (@) mcpu: port@0 { mcpu0_ep: endpoint { remote-endpoint = <&mcodec0_ep>; }; }; // (X) to pair
101 port@1 { mcpu1_ep: endpoint { remote-endpoint = <&cpu1_ep>; }; }; // (A) Multi Element
102 port@2 { mcpu2_ep: endpoint { remote-endpoint = <&cpu2_ep>; }; }; // (B) Multi Element
105 port@0 { mcodec0_ep: endpoint { remote-endpoint = <&mcpu0_ep>; }; }; // (x) to pair
106 port@1 { mcodec1_ep: endpoint { remote-endpoint = <&codec1_ep>; }; }; // (a) Multi Element
107 port@2 { mcodec2_ep: endpoint { remote-endpoint = <&codec2_ep>; }; }; // (b) Multi Element
116 port@0 { cpu1_ep: endpoint { remote-endpoint = <&mcpu1_ep>; }; };
117 port@1 { cpu2_ep: endpoint { remote-endpoint = <&mcpu2_ep>; }; };
123 port@0 { codec1_ep: endpoint { remote-endpoint = <&mcodec1_ep>; }; };
124 port@1 { codec2_ep: endpoint { remote-endpoint = <&mcodec2_ep>; }; };
128 ************************************
130 ************************************
134 PCM0 <--> * fe0 be0 * <--> DAI0: Codec Headset
135 PCM1 <--> * fe1 be1 * <--> DAI1: Codec Speakers
136 PCM2 <--> * fe2 be2 * <--> DAI2: MODEM
137 PCM3 <--> * fe3 be3 * <--> DAI3: BT
138 * be4 * <--> DAI4: DMIC
139 * be5 * <--> DAI5: FM
143 compatible = "audio-graph-card2";
146 routing = "xxx Playback", "xxx Playback",
147 "xxx Playback", "xxx Playback",
148 "xxx Playback", "xxx Playback";
150 // indicate all Front-End, Back-End
151 links = <&fe0, &fe1, ...,
157 fe0: port@0 { fe0_ep: endpoint { remote-endpoint = <&pcm0_ep>; }; };
158 fe1: port@1 { fe1_ep: endpoint { remote-endpoint = <&pcm1_ep>; }; };
163 be0: port@0 { be0_ep: endpoint { remote-endpoint = <&dai0_ep>; }; };
164 be1: port@1 { be1_ep: endpoint { remote-endpoint = <&dai1_ep>; }; };
174 port@0 { pcm0_ep: endpoint { remote-endpoint = <&fe0_ep>; }; };
175 port@1 { pcm1_ep: endpoint { remote-endpoint = <&fe1_ep>; }; };
182 port@0 { dai0_ep: endpoint { remote-endpoint = <&be0_ep>; }; };
183 port@1 { dai1_ep: endpoint { remote-endpoint = <&be1_ep>; }; };
188 ************************************
190 ************************************
198 compatible = "audio-graph-card2";
200 routing = "OUT" ,"DAI1 Playback",
201 "DAI0 Capture", "IN";
208 c2c: port@0 { c2cf_ep: endpoint { remote-endpoint = <&codec0_ep>; }; };
209 port@1 { c2cb_ep: endpoint { remote-endpoint = <&codec1_ep>; }; };
218 codec0_ep: endpoint { remote-endpoint = <&c2cf_ep>; }; };
219 port@1 { codec1_ep: endpoint { remote-endpoint = <&c2cb_ep>; }; };
230 GRAPH_MULTI, /* don't use ! Use this only in __graph_get_type() */
233 #define GRAPH_NODENAME_MULTI "multi"
234 #define GRAPH_NODENAME_DPCM "dpcm"
235 #define GRAPH_NODENAME_C2C "codec2codec"
237 #define port_to_endpoint(port) of_get_child_by_name(port, "endpoint")
239 static enum graph_type __graph_get_type(struct device_node *lnk)
241 struct device_node *np, *parent_np;
247 * => lnk: port@0 { ... };
252 np = of_get_parent(lnk);
253 if (of_node_name_eq(np, "ports")) {
254 parent_np = of_get_parent(np);
259 if (of_node_name_eq(np, GRAPH_NODENAME_MULTI)) {
264 if (of_node_name_eq(np, GRAPH_NODENAME_DPCM)) {
269 if (of_node_name_eq(np, GRAPH_NODENAME_C2C)) {
282 static enum graph_type graph_get_type(struct simple_util_priv *priv,
283 struct device_node *lnk)
285 enum graph_type type = __graph_get_type(lnk);
287 /* GRAPH_MULTI here means GRAPH_NORMAL */
288 if (type == GRAPH_MULTI)
293 struct device *dev = simple_priv_to_dev(priv);
294 const char *str = "Normal";
298 if (graph_util_is_ports0(lnk))
299 str = "DPCM Front-End";
301 str = "DPCM Back-End";
310 dev_dbg(dev, "%pOF (%s)", lnk, str);
316 static int graph_lnk_is_multi(struct device_node *lnk)
318 return __graph_get_type(lnk) == GRAPH_MULTI;
321 static struct device_node *graph_get_next_multi_ep(struct device_node **port)
323 struct device_node *ports = of_get_parent(*port);
324 struct device_node *ep = NULL;
325 struct device_node *rep = NULL;
330 * => lnk: port@0 { ... }; // to pair
331 * port@1 { ep { ... = rep0 } }; // Multi Element
332 * port@2 { ep { ... = rep1 } }; // Multi Element
343 *port = of_get_next_child(ports, *port);
346 } while (!of_node_name_eq(*port, "port"));
349 ep = port_to_endpoint(*port);
350 rep = of_graph_get_remote_endpoint(ep);
359 static const struct snd_soc_ops graph_ops = {
360 .startup = simple_util_startup,
361 .shutdown = simple_util_shutdown,
362 .hw_params = simple_util_hw_params,
365 static void graph_parse_convert(struct device_node *ep,
366 struct simple_dai_props *props)
368 struct device_node *port = of_get_parent(ep);
369 struct device_node *ports = of_get_parent(port);
370 struct simple_util_data *adata = &props->adata;
372 if (of_node_name_eq(ports, "ports"))
373 simple_util_parse_convert(ports, NULL, adata);
374 simple_util_parse_convert(port, NULL, adata);
375 simple_util_parse_convert(ep, NULL, adata);
381 static void graph_parse_mclk_fs(struct device_node *ep,
382 struct simple_dai_props *props)
384 struct device_node *port = of_get_parent(ep);
385 struct device_node *ports = of_get_parent(port);
387 if (of_node_name_eq(ports, "ports"))
388 of_property_read_u32(ports, "mclk-fs", &props->mclk_fs);
389 of_property_read_u32(port, "mclk-fs", &props->mclk_fs);
390 of_property_read_u32(ep, "mclk-fs", &props->mclk_fs);
396 static int __graph_parse_node(struct simple_util_priv *priv,
397 enum graph_type gtype,
398 struct device_node *ep,
399 struct link_info *li,
402 struct device *dev = simple_priv_to_dev(priv);
403 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
404 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
405 struct snd_soc_dai_link_component *dlc;
406 struct simple_util_dai *dai;
407 int ret, is_single_links = 0;
410 dlc = snd_soc_link_to_cpu(dai_link, idx);
411 dai = simple_props_to_dai_cpu(dai_props, idx);
413 dlc = snd_soc_link_to_codec(dai_link, idx);
414 dai = simple_props_to_dai_codec(dai_props, idx);
417 graph_parse_mclk_fs(ep, dai_props);
419 ret = graph_util_parse_dai(dev, ep, dlc, &is_single_links);
423 ret = simple_util_parse_tdm(ep, dai);
427 ret = simple_util_parse_tdm_width_map(dev, ep, dai);
431 ret = simple_util_parse_clk(dev, ep, dai, dlc);
438 if (!dai_link->name) {
439 struct snd_soc_dai_link_component *cpus = dlc;
440 struct snd_soc_dai_link_component *codecs = snd_soc_link_to_codec(dai_link, idx);
441 char *cpu_multi = "";
442 char *codec_multi = "";
444 if (dai_link->num_cpus > 1)
445 cpu_multi = "_multi";
446 if (dai_link->num_codecs > 1)
447 codec_multi = "_multi";
451 /* run is_cpu only. see audio_graph2_link_normal() */
453 simple_util_set_dailink_name(dev, dai_link, "%s%s-%s%s",
454 cpus->dai_name, cpu_multi,
455 codecs->dai_name, codec_multi);
459 simple_util_set_dailink_name(dev, dai_link, "fe.%pOFP.%s%s",
460 cpus->of_node, cpus->dai_name, cpu_multi);
462 simple_util_set_dailink_name(dev, dai_link, "be.%pOFP.%s%s",
463 codecs->of_node, codecs->dai_name, codec_multi);
466 /* run is_cpu only. see audio_graph2_link_c2c() */
468 simple_util_set_dailink_name(dev, dai_link, "c2c.%s%s-%s%s",
469 cpus->dai_name, cpu_multi,
470 codecs->dai_name, codec_multi);
478 * Check "prefix" from top node
481 if (!is_cpu && gtype == GRAPH_DPCM) {
482 struct snd_soc_dai_link_component *codecs = snd_soc_link_to_codec(dai_link, idx);
483 struct snd_soc_codec_conf *cconf = simple_props_to_codec_conf(dai_props, idx);
484 struct device_node *rport = of_get_parent(ep);
485 struct device_node *rports = of_get_parent(rport);
487 if (of_node_name_eq(rports, "ports"))
488 snd_soc_of_parse_node_prefix(rports, cconf, codecs->of_node, "prefix");
489 snd_soc_of_parse_node_prefix(rport, cconf, codecs->of_node, "prefix");
496 struct snd_soc_dai_link_component *cpus = dlc;
497 struct snd_soc_dai_link_component *platforms = snd_soc_link_to_platform(dai_link, idx);
499 simple_util_canonicalize_cpu(cpus, is_single_links);
500 simple_util_canonicalize_platform(platforms, cpus);
506 static int graph_parse_node_multi_nm(struct snd_soc_dai_link *dai_link,
507 int *nm_idx, int cpu_idx,
508 struct device_node *mcpu_port)
512 * | X|<-@------->|x |
514 * cpu0 <--|A 1|<--------->|4 a|-> codec0
515 * cpu1 <--|B 2|<-----+--->|5 b|-> codec1
516 * cpu2 <--|C 3|<----/ +---+
521 * port@0 { mcpu_top_ep {... = mcodec_ep; }; }; // (X) to pair
522 * <mcpu_port> port@1 { mcpu0_ep { ... = cpu0_ep; }; // (A) Multi Element
523 * mcpu0_ep_0 { ... = mcodec0_ep_0; }; }; // (1) connected Codec
524 * port@2 { mcpu1_ep { ... = cpu1_ep; }; // (B) Multi Element
525 * mcpu1_ep_0 { ... = mcodec1_ep_0; }; }; // (2) connected Codec
526 * port@3 { mcpu2_ep { ... = cpu2_ep; }; // (C) Multi Element
527 * mcpu2_ep_0 { ... = mcodec1_ep_1; }; }; // (3) connected Codec
531 * port@0 { mcodec_top_ep {... = mcpu_ep; }; }; // (x) to pair
532 * <mcodec_port>port@1 { mcodec0_ep { ... = codec0_ep; }; // (a) Multi Element
533 * mcodec0_ep_0 { ... = mcpu0_ep_0; }; }; // (4) connected CPU
534 * port@2 { mcodec1_ep { ... = codec1_ep; }; // (b) Multi Element
535 * mcodec1_ep_0 { ... = mcpu1_ep_0; }; // (5) connected CPU
536 * mcodec1_ep_1 { ... = mcpu2_ep_0; }; }; // (5) connected CPU
540 struct device_node *mcpu_ep = port_to_endpoint(mcpu_port);
541 struct device_node *mcpu_ep_n = mcpu_ep;
542 struct device_node *mcpu_port_top = of_get_next_child(of_get_parent(mcpu_port), NULL);
543 struct device_node *mcpu_ep_top = port_to_endpoint(mcpu_port_top);
544 struct device_node *mcodec_ep_top = of_graph_get_remote_endpoint(mcpu_ep_top);
545 struct device_node *mcodec_port_top = of_get_parent(mcodec_ep_top);
546 struct device_node *mcodec_ports = of_get_parent(mcodec_port_top);
547 int nm_max = max(dai_link->num_cpus, dai_link->num_codecs);
550 if (cpu_idx > dai_link->num_cpus)
554 struct device_node *mcodec_ep_n;
555 struct device_node *mcodec_port_i;
556 struct device_node *mcodec_port;
559 if (*nm_idx > nm_max)
562 mcpu_ep_n = of_get_next_child(mcpu_port, mcpu_ep_n);
568 mcodec_ep_n = of_graph_get_remote_endpoint(mcpu_ep_n);
569 mcodec_port = of_get_parent(mcodec_ep_n);
571 if (mcodec_ports != of_get_parent(mcodec_port))
575 mcodec_port_i = of_get_next_child(mcodec_ports, NULL);
577 if (codec_idx > dai_link->num_codecs)
580 mcodec_port_i = of_get_next_child(mcodec_ports, mcodec_port_i);
585 if (mcodec_port_i == mcodec_port)
591 dai_link->ch_maps[*nm_idx].cpu = cpu_idx;
592 dai_link->ch_maps[*nm_idx].codec = codec_idx;
596 of_node_put(mcodec_port_i);
598 of_node_put(mcodec_port);
599 of_node_put(mcpu_ep_n);
600 of_node_put(mcodec_ep_n);
603 of_node_put(mcpu_ep);
604 of_node_put(mcpu_port_top);
605 of_node_put(mcpu_ep_top);
606 of_node_put(mcodec_ep_top);
607 of_node_put(mcodec_port_top);
608 of_node_put(mcodec_ports);
613 static int graph_parse_node_multi(struct simple_util_priv *priv,
614 enum graph_type gtype,
615 struct device_node *port,
616 struct link_info *li, int is_cpu)
618 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
619 struct device *dev = simple_priv_to_dev(priv);
620 struct device_node *ep;
623 int nm_max = max(dai_link->num_cpus, dai_link->num_codecs);
626 * create ch_maps if CPU:Codec = N:M
627 * DPCM is out of scope
629 if (gtype != GRAPH_DPCM && !dai_link->ch_maps &&
630 dai_link->num_cpus > 1 && dai_link->num_codecs > 1 &&
631 dai_link->num_cpus != dai_link->num_codecs) {
633 dai_link->ch_maps = devm_kcalloc(dev, nm_max,
634 sizeof(struct snd_soc_dai_link_ch_map), GFP_KERNEL);
635 if (!dai_link->ch_maps)
639 for (int idx = 0;; idx++) {
643 * <port> port@0 { ... }; // to pair
644 * port@1 { mcpu1_ep { ... = cpu1_ep };}; // Multi Element
645 * port@2 { mcpu2_ep { ... = cpu2_ep };}; // Multi Element
651 * <ep> port@0 { cpu1_ep { ... = mcpu1_ep };};
655 ep = graph_get_next_multi_ep(&port);
659 ret = __graph_parse_node(priv, gtype, ep, li, is_cpu, idx);
664 /* CPU:Codec = N:M */
665 if (is_cpu && dai_link->ch_maps) {
666 ret = graph_parse_node_multi_nm(dai_link, &nm_idx, idx, port);
672 if (is_cpu && dai_link->ch_maps && (nm_idx != nm_max))
679 static int graph_parse_node_single(struct simple_util_priv *priv,
680 enum graph_type gtype,
681 struct device_node *port,
682 struct link_info *li, int is_cpu)
684 struct device_node *ep = port_to_endpoint(port);
685 int ret = __graph_parse_node(priv, gtype, ep, li, is_cpu, 0);
692 static int graph_parse_node(struct simple_util_priv *priv,
693 enum graph_type gtype,
694 struct device_node *port,
695 struct link_info *li, int is_cpu)
697 if (graph_lnk_is_multi(port))
698 return graph_parse_node_multi(priv, gtype, port, li, is_cpu);
700 return graph_parse_node_single(priv, gtype, port, li, is_cpu);
703 static void graph_parse_daifmt(struct device_node *node,
704 unsigned int *daifmt, unsigned int *bit_frame)
709 * see also above "daifmt" explanation
729 * It can be judged it is provider
730 * if (A) or (B) or (C) has bitclock-master / frame-master flag.
734 *bit_frame |= snd_soc_daifmt_parse_clock_provider_as_bitmap(node, NULL);
736 #define update_daifmt(name) \
737 if (!(*daifmt & SND_SOC_DAIFMT_##name##_MASK) && \
738 (fmt & SND_SOC_DAIFMT_##name##_MASK)) \
739 *daifmt |= fmt & SND_SOC_DAIFMT_##name##_MASK
744 * This function is called by (C) -> (B) -> (A) order.
745 * Set if applicable part was not yet set.
747 fmt = snd_soc_daifmt_parse_format(node, NULL);
748 update_daifmt(FORMAT);
749 update_daifmt(CLOCK);
753 static void graph_link_init(struct simple_util_priv *priv,
754 struct device_node *port,
755 struct link_info *li,
758 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
759 struct device_node *ep;
760 struct device_node *ports;
761 unsigned int daifmt = 0, daiclk = 0;
762 bool playback_only = 0, capture_only = 0;
763 unsigned int bit_frame = 0;
765 if (graph_lnk_is_multi(port)) {
767 ep = graph_get_next_multi_ep(&port);
768 port = of_get_parent(ep);
770 ep = port_to_endpoint(port);
773 ports = of_get_parent(port);
787 graph_parse_daifmt(ep, &daifmt, &bit_frame); /* (C) */
788 graph_parse_daifmt(port, &daifmt, &bit_frame); /* (B) */
789 if (of_node_name_eq(ports, "ports"))
790 graph_parse_daifmt(ports, &daifmt, &bit_frame); /* (A) */
794 * We need to flip clock_provider if it was CPU node,
795 * because it is Codec base.
797 daiclk = snd_soc_daifmt_clock_provider_from_bitmap(bit_frame);
799 daiclk = snd_soc_daifmt_clock_provider_flipped(daiclk);
801 graph_util_parse_link_direction(port, &playback_only, &capture_only);
803 dai_link->playback_only = playback_only;
804 dai_link->capture_only = capture_only;
806 dai_link->dai_fmt = daifmt | daiclk;
807 dai_link->init = simple_util_dai_init;
808 dai_link->ops = &graph_ops;
810 dai_link->ops = priv->ops;
813 int audio_graph2_link_normal(struct simple_util_priv *priv,
814 struct device_node *lnk,
815 struct link_info *li)
817 struct device_node *cpu_port = lnk;
818 struct device_node *cpu_ep = port_to_endpoint(cpu_port);
819 struct device_node *codec_port = of_graph_get_remote_port(cpu_ep);
825 * __graph_parse_node() :: DAI Naming
827 ret = graph_parse_node(priv, GRAPH_NORMAL, codec_port, li, 0);
832 * call CPU, and set DAI Name
834 ret = graph_parse_node(priv, GRAPH_NORMAL, cpu_port, li, 1);
838 graph_link_init(priv, cpu_port, li, 1);
840 of_node_put(codec_port);
845 EXPORT_SYMBOL_GPL(audio_graph2_link_normal);
847 int audio_graph2_link_dpcm(struct simple_util_priv *priv,
848 struct device_node *lnk,
849 struct link_info *li)
851 struct device_node *ep = port_to_endpoint(lnk);
852 struct device_node *rep = of_graph_get_remote_endpoint(ep);
853 struct device_node *rport = of_graph_get_remote_port(ep);
854 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
855 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
856 int is_cpu = graph_util_is_ports0(lnk);
864 * => lnk: port@0 { ep: { ... = rep }; };
875 * rport: port@0 { rep: { ... = ep } };
880 * setup CPU here, Codec is already set as dummy.
882 * simple_util_init_priv()
884 dai_link->dynamic = 1;
885 dai_link->dpcm_merged_format = 1;
887 ret = graph_parse_node(priv, GRAPH_DPCM, rport, li, 1);
899 * => lnk: port@0 { ep: { ... = rep; }; };
906 * rport: port@0 { rep: { ... = ep; }; };
911 * setup Codec here, CPU is already set as dummy.
913 * simple_util_init_priv()
917 dai_link->no_pcm = 1;
918 dai_link->be_hw_params_fixup = simple_util_be_hw_params_fixup;
920 ret = graph_parse_node(priv, GRAPH_DPCM, rport, li, 0);
925 graph_parse_convert(ep, dai_props); /* at node of <dpcm> */
926 graph_parse_convert(rep, dai_props); /* at node of <CPU/Codec> */
928 snd_soc_dai_link_set_capabilities(dai_link);
930 graph_link_init(priv, rport, li, is_cpu);
938 EXPORT_SYMBOL_GPL(audio_graph2_link_dpcm);
940 int audio_graph2_link_c2c(struct simple_util_priv *priv,
941 struct device_node *lnk,
942 struct link_info *li)
944 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
945 struct device_node *port0, *port1, *ports;
946 struct device_node *codec0_port, *codec1_port;
947 struct device_node *ep0, *ep1;
955 * => lnk: port@0 { c2c0_ep: { ... = codec0_ep; }; };
956 * port@1 { c2c1_ep: { ... = codec1_ep; }; };
962 * port@0 { codec0_ep: ... }; };
963 * port@1 { codec1_ep: ... }; };
969 ports = of_get_parent(port0);
970 port1 = of_get_next_child(ports, lnk);
973 * Card2 can use original Codec2Codec settings if DT has.
974 * It will use default settings if no settings on DT.
976 * simple_util_init_for_codec2codec()
978 * Add more settings here if needed
980 of_property_read_u32(ports, "rate", &val);
982 struct device *dev = simple_priv_to_dev(priv);
983 struct snd_soc_pcm_stream *c2c_conf;
985 c2c_conf = devm_kzalloc(dev, sizeof(*c2c_conf), GFP_KERNEL);
989 c2c_conf->formats = SNDRV_PCM_FMTBIT_S32_LE; /* update ME */
990 c2c_conf->rates = SNDRV_PCM_RATE_8000_384000;
992 c2c_conf->rate_max = val;
993 c2c_conf->channels_min =
994 c2c_conf->channels_max = 2; /* update ME */
996 dai_link->c2c_params = c2c_conf;
997 dai_link->num_c2c_params = 1;
1000 ep0 = port_to_endpoint(port0);
1001 ep1 = port_to_endpoint(port1);
1003 codec0_port = of_graph_get_remote_port(ep0);
1004 codec1_port = of_graph_get_remote_port(ep1);
1009 * __graph_parse_node() :: DAI Naming
1011 ret = graph_parse_node(priv, GRAPH_C2C, codec1_port, li, 0);
1016 * call CPU, and set DAI Name
1018 ret = graph_parse_node(priv, GRAPH_C2C, codec0_port, li, 1);
1022 graph_link_init(priv, codec0_port, li, 1);
1026 of_node_put(codec0_port);
1027 of_node_put(codec1_port);
1035 EXPORT_SYMBOL_GPL(audio_graph2_link_c2c);
1037 static int graph_link(struct simple_util_priv *priv,
1038 struct graph2_custom_hooks *hooks,
1039 enum graph_type gtype,
1040 struct device_node *lnk,
1041 struct link_info *li)
1043 struct device *dev = simple_priv_to_dev(priv);
1044 GRAPH2_CUSTOM func = NULL;
1049 if (hooks && hooks->custom_normal)
1050 func = hooks->custom_normal;
1052 func = audio_graph2_link_normal;
1055 if (hooks && hooks->custom_dpcm)
1056 func = hooks->custom_dpcm;
1058 func = audio_graph2_link_dpcm;
1061 if (hooks && hooks->custom_c2c)
1062 func = hooks->custom_c2c;
1064 func = audio_graph2_link_c2c;
1071 dev_err(dev, "non supported gtype (%d)\n", gtype);
1075 ret = func(priv, lnk, li);
1084 static int graph_counter(struct device_node *lnk)
1091 * => lnk: port@0 { ... }; // to pair
1092 * port@1 { ... }; // Multi Element
1093 * port@2 { ... }; // Multi Element
1098 * ignore first lnk part
1100 if (graph_lnk_is_multi(lnk)) {
1101 struct device_node *ports = of_get_parent(lnk);
1102 struct device_node *port = NULL;
1106 * CPU/Codec = N:M case has many endpoints.
1107 * We can't use of_graph_get_endpoint_count() here
1110 port = of_get_next_child(ports, port);
1119 * Single CPU / Codec
1125 static int graph_count_normal(struct simple_util_priv *priv,
1126 struct device_node *lnk,
1127 struct link_info *li)
1129 struct device_node *cpu_port = lnk;
1130 struct device_node *cpu_ep = port_to_endpoint(cpu_port);
1131 struct device_node *codec_port = of_graph_get_remote_port(cpu_ep);
1135 * => lnk: port { endpoint { .. }; };
1139 * DON'T REMOVE platforms
1141 * simple-card.c :: simple_count_noml()
1143 li->num[li->link].cpus =
1144 li->num[li->link].platforms = graph_counter(cpu_port);
1146 li->num[li->link].codecs = graph_counter(codec_port);
1148 of_node_put(cpu_ep);
1149 of_node_put(codec_port);
1154 static int graph_count_dpcm(struct simple_util_priv *priv,
1155 struct device_node *lnk,
1156 struct link_info *li)
1158 struct device_node *ep = port_to_endpoint(lnk);
1159 struct device_node *rport = of_graph_get_remote_port(ep);
1165 * => lnk: port@0 { endpoint { ... }; };
1170 * => lnk: port@0 { endpoint { ... }; };
1176 if (graph_util_is_ports0(lnk)) {
1178 * DON'T REMOVE platforms
1180 * simple-card.c :: simple_count_noml()
1182 li->num[li->link].cpus = graph_counter(rport); /* FE */
1183 li->num[li->link].platforms = graph_counter(rport);
1185 li->num[li->link].codecs = graph_counter(rport); /* BE */
1194 static int graph_count_c2c(struct simple_util_priv *priv,
1195 struct device_node *lnk,
1196 struct link_info *li)
1198 struct device_node *ports = of_get_parent(lnk);
1199 struct device_node *port0 = lnk;
1200 struct device_node *port1 = of_get_next_child(ports, lnk);
1201 struct device_node *ep0 = port_to_endpoint(port0);
1202 struct device_node *ep1 = port_to_endpoint(port1);
1203 struct device_node *codec0 = of_graph_get_remote_port(ep0);
1204 struct device_node *codec1 = of_graph_get_remote_port(ep1);
1211 * => lnk: port@0 { endpoint { ... }; };
1212 * port@1 { endpoint { ... }; };
1217 * DON'T REMOVE platforms
1219 * simple-card.c :: simple_count_noml()
1221 li->num[li->link].cpus =
1222 li->num[li->link].platforms = graph_counter(codec0);
1224 li->num[li->link].codecs = graph_counter(codec1);
1230 of_node_put(codec0);
1231 of_node_put(codec1);
1236 static int graph_count(struct simple_util_priv *priv,
1237 struct graph2_custom_hooks *hooks,
1238 enum graph_type gtype,
1239 struct device_node *lnk,
1240 struct link_info *li)
1242 struct device *dev = simple_priv_to_dev(priv);
1243 GRAPH2_CUSTOM func = NULL;
1246 if (li->link >= SNDRV_MAX_LINKS) {
1247 dev_err(dev, "too many links\n");
1253 func = graph_count_normal;
1256 func = graph_count_dpcm;
1259 func = graph_count_c2c;
1266 dev_err(dev, "non supported gtype (%d)\n", gtype);
1270 ret = func(priv, lnk, li);
1279 static int graph_for_each_link(struct simple_util_priv *priv,
1280 struct graph2_custom_hooks *hooks,
1281 struct link_info *li,
1282 int (*func)(struct simple_util_priv *priv,
1283 struct graph2_custom_hooks *hooks,
1284 enum graph_type gtype,
1285 struct device_node *lnk,
1286 struct link_info *li))
1288 struct of_phandle_iterator it;
1289 struct device *dev = simple_priv_to_dev(priv);
1290 struct device_node *node = dev->of_node;
1291 struct device_node *lnk;
1292 enum graph_type gtype;
1295 /* loop for all listed CPU port */
1296 of_for_each_phandle(&it, rc, node, "links", NULL, 0) {
1299 gtype = graph_get_type(priv, lnk);
1301 ret = func(priv, hooks, gtype, lnk, li);
1309 int audio_graph2_parse_of(struct simple_util_priv *priv, struct device *dev,
1310 struct graph2_custom_hooks *hooks)
1312 struct snd_soc_card *card = simple_priv_to_card(priv);
1313 struct link_info *li;
1316 li = devm_kzalloc(dev, sizeof(*li), GFP_KERNEL);
1320 card->probe = graph_util_card_probe;
1321 card->owner = THIS_MODULE;
1324 if ((hooks) && (hooks)->hook_pre) {
1325 ret = (hooks)->hook_pre(priv);
1330 ret = graph_for_each_link(priv, hooks, li, graph_count);
1336 ret = simple_util_init_priv(priv, li);
1340 priv->pa_gpio = devm_gpiod_get_optional(dev, "pa", GPIOD_OUT_LOW);
1341 if (IS_ERR(priv->pa_gpio)) {
1342 ret = PTR_ERR(priv->pa_gpio);
1343 dev_err(dev, "failed to get amplifier gpio: %d\n", ret);
1347 ret = simple_util_parse_widgets(card, NULL);
1351 ret = simple_util_parse_routing(card, NULL);
1355 memset(li, 0, sizeof(*li));
1356 ret = graph_for_each_link(priv, hooks, li, graph_link);
1360 ret = simple_util_parse_card_name(card, NULL);
1364 snd_soc_card_set_drvdata(card, priv);
1366 if ((hooks) && (hooks)->hook_post) {
1367 ret = (hooks)->hook_post(priv);
1372 simple_util_debug_info(priv);
1374 ret = devm_snd_soc_register_card(dev, card);
1376 devm_kfree(dev, li);
1379 dev_err_probe(dev, ret, "parse error\n");
1383 EXPORT_SYMBOL_GPL(audio_graph2_parse_of);
1385 static int graph_probe(struct platform_device *pdev)
1387 struct simple_util_priv *priv;
1388 struct device *dev = &pdev->dev;
1390 /* Allocate the private data and the DAI link array */
1391 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1395 return audio_graph2_parse_of(priv, dev, NULL);
1398 static const struct of_device_id graph_of_match[] = {
1399 { .compatible = "audio-graph-card2", },
1402 MODULE_DEVICE_TABLE(of, graph_of_match);
1404 static struct platform_driver graph_card = {
1406 .name = "asoc-audio-graph-card2",
1407 .pm = &snd_soc_pm_ops,
1408 .of_match_table = graph_of_match,
1410 .probe = graph_probe,
1411 .remove_new = simple_util_remove,
1413 module_platform_driver(graph_card);
1415 MODULE_ALIAS("platform:asoc-audio-graph-card2");
1416 MODULE_LICENSE("GPL v2");
1417 MODULE_DESCRIPTION("ASoC Audio Graph Card2");