1 // SPDX-License-Identifier: GPL-2.0
3 // ASoC Audio Graph Card2 support
5 // Copyright (C) 2020 Renesas Electronics Corp.
6 // Copyright (C) 2020 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
8 // based on ${LINUX}/sound/soc/generic/audio-graph-card.c
10 #include <linux/device.h>
11 #include <linux/gpio.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/module.h>
15 #include <linux/of_device.h>
16 #include <linux/of_gpio.h>
17 #include <linux/of_graph.h>
18 #include <linux/platform_device.h>
19 #include <linux/string.h>
20 #include <sound/graph_card.h>
22 /************************************
24 ************************************
39 You can set daifmt at ports/port/endpoint.
40 It uses *latest* format, and *share* master settings.
42 sample0: left_j, bitclock-master, frame-master
43 sample1: i2s, bitclock-master
45 If there was no settings, *Codec* will be
46 bitclock/frame provider as default.
50 ************************************
52 ************************************
57 compatible = "audio-graph-card2";
65 cpu_ep: endpoint { remote-endpoint = <&codec_ep>; }; };
69 port { codec_ep: endpoint { remote-endpoint = <&cpu_ep>; }; };
72 ************************************
74 ************************************
76 It has connection part (= X) and list part (= y).
77 links indicates connection part of CPU side (= A).
80 CPU1 --(y) | | <-(X)--(X)-> | | (y)-- Codec1
81 CPU2 --(y) | | | | (y)-- Codec2
85 compatible = "audio-graph-card2";
91 (X) (A) mcpu: port@0 { mcpu0_ep: endpoint { remote-endpoint = <&mcodec0_ep>; }; };
92 (y) port@1 { mcpu1_ep: endpoint { remote-endpoint = <&cpu1_ep>; }; };
93 (y) port@1 { mcpu2_ep: endpoint { remote-endpoint = <&cpu2_ep>; }; };
96 (X) port@0 { mcodec0_ep: endpoint { remote-endpoint = <&mcpu0_ep>; }; };
97 (y) port@0 { mcodec1_ep: endpoint { remote-endpoint = <&codec1_ep>; }; };
98 (y) port@1 { mcodec2_ep: endpoint { remote-endpoint = <&codec2_ep>; }; };
107 port@0 { cpu1_ep: endpoint { remote-endpoint = <&mcpu1_ep>; }; };
108 port@1 { cpu2_ep: endpoint { remote-endpoint = <&mcpu2_ep>; }; };
114 port@0 { codec1_ep: endpoint { remote-endpoint = <&mcodec1_ep>; }; };
115 port@1 { codec2_ep: endpoint { remote-endpoint = <&mcodec2_ep>; }; };
119 ************************************
121 ************************************
125 PCM0 <--> * fe0 be0 * <--> DAI0: Codec Headset
126 PCM1 <--> * fe1 be1 * <--> DAI1: Codec Speakers
127 PCM2 <--> * fe2 be2 * <--> DAI2: MODEM
128 PCM3 <--> * fe3 be3 * <--> DAI3: BT
129 * be4 * <--> DAI4: DMIC
130 * be5 * <--> DAI5: FM
134 compatible = "audio-graph-card2";
137 routing = "xxx Playback", "xxx Playback",
138 "xxx Playback", "xxx Playback",
139 "xxx Playback", "xxx Playback";
141 // indicate all Front-End, Back-End
142 links = <&fe0, &fe1, ...,
148 fe0: port@0 { fe0_ep: endpoint { remote-endpoint = <&pcm0_ep>; }; };
149 fe1: port@1 { fe1_ep: endpoint { remote-endpoint = <&pcm1_ep>; }; };
154 be0: port@0 { be0_ep: endpoint { remote-endpoint = <&dai0_ep>; }; };
155 be1: port@1 { be1_ep: endpoint { remote-endpoint = <&dai1_ep>; }; };
165 port@0 { pcm0_ep: endpoint { remote-endpoint = <&fe0_ep>; }; };
166 port@1 { pcm1_ep: endpoint { remote-endpoint = <&fe1_ep>; }; };
173 port@0 { dai0_ep: endpoint { remote-endpoint = <&be0_ep>; }; };
174 port@1 { dai1_ep: endpoint { remote-endpoint = <&be1_ep>; }; };
179 ************************************
181 ************************************
189 compatible = "audio-graph-card2";
191 routing = "OUT" ,"DAI1 Playback",
192 "DAI0 Capture", "IN";
199 c2c: port@0 { c2cf_ep: endpoint { remote-endpoint = <&codec0_ep>; }; };
200 port@1 { c2cb_ep: endpoint { remote-endpoint = <&codec1_ep>; }; };
209 codec0_ep: endpoint { remote-endpoint = <&c2cf_ep>; }; };
210 port@1 { codec1_ep: endpoint { remote-endpoint = <&c2cb_ep>; }; };
221 GRAPH_MULTI, /* don't use ! Use this only in __graph_get_type() */
224 #define GRAPH_NODENAME_MULTI "multi"
225 #define GRAPH_NODENAME_DPCM "dpcm"
226 #define GRAPH_NODENAME_C2C "codec2codec"
228 #define port_to_endpoint(port) of_get_child_by_name(port, "endpoint")
230 static enum graph_type __graph_get_type(struct device_node *lnk)
232 struct device_node *np;
237 * => lnk: port@0 { ... };
242 np = of_get_parent(lnk);
243 if (of_node_name_eq(np, "ports"))
244 np = of_get_parent(np);
246 if (of_node_name_eq(np, GRAPH_NODENAME_MULTI))
249 if (of_node_name_eq(np, GRAPH_NODENAME_DPCM))
252 if (of_node_name_eq(np, GRAPH_NODENAME_C2C))
258 static enum graph_type graph_get_type(struct asoc_simple_priv *priv,
259 struct device_node *lnk)
261 enum graph_type type = __graph_get_type(lnk);
263 /* GRAPH_MULTI here means GRAPH_NORMAL */
264 if (type == GRAPH_MULTI)
269 struct device *dev = simple_priv_to_dev(priv);
270 const char *str = "Normal";
274 if (asoc_graph_is_ports0(lnk))
275 str = "DPCM Front-End";
277 str = "DPCM Back-End";
286 dev_dbg(dev, "%pOF (%s)", lnk, str);
292 static int graph_lnk_is_multi(struct device_node *lnk)
294 return __graph_get_type(lnk) == GRAPH_MULTI;
297 static struct device_node *graph_get_next_multi_ep(struct device_node **port)
299 struct device_node *ports = of_get_parent(*port);
300 struct device_node *ep = NULL;
301 struct device_node *rep = NULL;
306 * => lnk: port@0 { ... };
307 * port@1 { ep { ... = rep0 } };
308 * port@2 { ep { ... = rep1 } };
319 *port = of_get_next_child(ports, *port);
322 } while (!of_node_name_eq(*port, "port"));
325 ep = port_to_endpoint(*port);
326 rep = of_graph_get_remote_endpoint(ep);
335 static const struct snd_soc_ops graph_ops = {
336 .startup = asoc_simple_startup,
337 .shutdown = asoc_simple_shutdown,
338 .hw_params = asoc_simple_hw_params,
341 static int graph_get_dai_id(struct device_node *ep)
343 struct device_node *node;
344 struct device_node *endpoint;
345 struct of_endpoint info;
350 /* use driver specified DAI ID if exist */
351 ret = snd_soc_get_dai_id(ep);
352 if (ret != -ENOTSUPP)
355 /* use endpoint/port reg if exist */
356 ret = of_graph_parse_endpoint(ep, &info);
359 * Because it will count port/endpoint if it doesn't have "reg".
360 * But, we can't judge whether it has "no reg", or "reg = <0>"
361 * only of_graph_parse_endpoint().
362 * We need to check "reg" property
364 if (of_get_property(ep, "reg", NULL))
367 node = of_get_parent(ep);
368 reg = of_get_property(node, "reg", NULL);
373 node = of_graph_get_port_parent(ep);
376 * Non HDMI sound case, counting port/endpoint on its DT
377 * is enough. Let's count it.
381 for_each_endpoint_of_node(node, endpoint) {
395 static int asoc_simple_parse_dai(struct device_node *ep,
396 struct snd_soc_dai_link_component *dlc,
399 struct device_node *node;
400 struct of_phandle_args args;
406 node = of_graph_get_port_parent(ep);
410 args.args[0] = graph_get_dai_id(ep);
411 args.args_count = (of_graph_get_endpoint_count(node) > 1);
416 * Here, dlc->dai_name is pointer to CPU/Codec DAI name.
417 * If user unbinded CPU or Codec driver, but not for Sound Card,
418 * dlc->dai_name is keeping unbinded CPU or Codec
421 * If user re-bind CPU or Codec driver again, ALSA SoC will try
422 * to rebind Card via snd_soc_try_rebind_card(), but because of
423 * above reason, it might can't bind Sound Card.
424 * Because Sound Card is pointing to released dai_name pointer.
426 * To avoid this rebind Card issue,
427 * 1) It needs to alloc memory to keep dai_name eventhough
428 * CPU or Codec driver was unbinded, or
429 * 2) user need to rebind Sound Card everytime
430 * if he unbinded CPU or Codec.
432 ret = snd_soc_get_dai_name(&args, &dlc->dai_name);
439 *is_single_link = of_graph_get_endpoint_count(node) == 1;
444 static void graph_parse_convert(struct device_node *ep,
445 struct simple_dai_props *props)
447 struct device_node *port = of_get_parent(ep);
448 struct device_node *ports = of_get_parent(port);
449 struct asoc_simple_data *adata = &props->adata;
451 if (of_node_name_eq(ports, "ports"))
452 asoc_simple_parse_convert(ports, NULL, adata);
453 asoc_simple_parse_convert(port, NULL, adata);
454 asoc_simple_parse_convert(ep, NULL, adata);
460 static void graph_parse_mclk_fs(struct device_node *ep,
461 struct simple_dai_props *props)
463 struct device_node *port = of_get_parent(ep);
464 struct device_node *ports = of_get_parent(port);
466 if (of_node_name_eq(ports, "ports"))
467 of_property_read_u32(ports, "mclk-fs", &props->mclk_fs);
468 of_property_read_u32(port, "mclk-fs", &props->mclk_fs);
469 of_property_read_u32(ep, "mclk-fs", &props->mclk_fs);
475 static int __graph_parse_node(struct asoc_simple_priv *priv,
476 enum graph_type gtype,
477 struct device_node *ep,
478 struct link_info *li,
481 struct device *dev = simple_priv_to_dev(priv);
482 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
483 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
484 struct snd_soc_dai_link_component *dlc;
485 struct asoc_simple_dai *dai;
486 int ret, is_single_links = 0;
489 dlc = asoc_link_to_cpu(dai_link, idx);
490 dai = simple_props_to_dai_cpu(dai_props, idx);
492 dlc = asoc_link_to_codec(dai_link, idx);
493 dai = simple_props_to_dai_codec(dai_props, idx);
496 graph_parse_mclk_fs(ep, dai_props);
498 ret = asoc_simple_parse_dai(ep, dlc, &is_single_links);
502 ret = asoc_simple_parse_tdm(ep, dai);
506 ret = asoc_simple_parse_tdm_width_map(dev, ep, dai);
510 ret = asoc_simple_parse_clk(dev, ep, dai, dlc);
517 if (!dai_link->name) {
518 struct snd_soc_dai_link_component *cpus = dlc;
519 struct snd_soc_dai_link_component *codecs = asoc_link_to_codec(dai_link, idx);
520 char *cpu_multi = "";
521 char *codec_multi = "";
523 if (dai_link->num_cpus > 1)
524 cpu_multi = "_multi";
525 if (dai_link->num_codecs > 1)
526 codec_multi = "_multi";
530 /* run is_cpu only. see audio_graph2_link_normal() */
532 asoc_simple_set_dailink_name(dev, dai_link, "%s%s-%s%s",
533 cpus->dai_name, cpu_multi,
534 codecs->dai_name, codec_multi);
538 asoc_simple_set_dailink_name(dev, dai_link, "fe.%pOFP.%s%s",
539 cpus->of_node, cpus->dai_name, cpu_multi);
541 asoc_simple_set_dailink_name(dev, dai_link, "be.%pOFP.%s%s",
542 codecs->of_node, codecs->dai_name, codec_multi);
545 /* run is_cpu only. see audio_graph2_link_c2c() */
547 asoc_simple_set_dailink_name(dev, dai_link, "c2c.%s%s-%s%s",
548 cpus->dai_name, cpu_multi,
549 codecs->dai_name, codec_multi);
557 * Check "prefix" from top node
560 if (!is_cpu && gtype == GRAPH_DPCM) {
561 struct snd_soc_dai_link_component *codecs = asoc_link_to_codec(dai_link, idx);
562 struct snd_soc_codec_conf *cconf = simple_props_to_codec_conf(dai_props, idx);
563 struct device_node *rport = of_get_parent(ep);
564 struct device_node *rports = of_get_parent(rport);
566 if (of_node_name_eq(rports, "ports"))
567 snd_soc_of_parse_node_prefix(rports, cconf, codecs->of_node, "prefix");
568 snd_soc_of_parse_node_prefix(rport, cconf, codecs->of_node, "prefix");
575 struct snd_soc_dai_link_component *cpus = dlc;
576 struct snd_soc_dai_link_component *platforms = asoc_link_to_platform(dai_link, idx);
578 asoc_simple_canonicalize_cpu(cpus, is_single_links);
579 asoc_simple_canonicalize_platform(platforms, cpus);
585 static int graph_parse_node(struct asoc_simple_priv *priv,
586 enum graph_type gtype,
587 struct device_node *port,
588 struct link_info *li, int is_cpu)
590 struct device_node *ep;
593 if (graph_lnk_is_multi(port)) {
598 for (idx = 0;; idx++) {
599 ep = graph_get_next_multi_ep(&port);
603 ret = __graph_parse_node(priv, gtype, ep,
610 /* Single CPU / Codec */
611 ep = port_to_endpoint(port);
612 ret = __graph_parse_node(priv, gtype, ep, li, is_cpu, 0);
619 static void graph_parse_daifmt(struct device_node *node,
620 unsigned int *daifmt, unsigned int *bit_frame)
625 * see also above "daifmt" explanation
645 * It can be judged it is provider
646 * if (A) or (B) or (C) has bitclock-master / frame-master flag.
650 *bit_frame |= snd_soc_daifmt_parse_clock_provider_as_bitmap(node, NULL);
652 #define update_daifmt(name) \
653 if (!(*daifmt & SND_SOC_DAIFMT_##name##_MASK) && \
654 (fmt & SND_SOC_DAIFMT_##name##_MASK)) \
655 *daifmt |= fmt & SND_SOC_DAIFMT_##name##_MASK
660 * This function is called by (C) -> (B) -> (A) order.
661 * Set if applicable part was not yet set.
663 fmt = snd_soc_daifmt_parse_format(node, NULL);
664 update_daifmt(FORMAT);
665 update_daifmt(CLOCK);
669 static void graph_link_init(struct asoc_simple_priv *priv,
670 struct device_node *port,
671 struct link_info *li,
674 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
675 struct device_node *ep;
676 struct device_node *ports;
677 unsigned int daifmt = 0, daiclk = 0;
678 unsigned int bit_frame = 0;
680 if (graph_lnk_is_multi(port)) {
682 ep = graph_get_next_multi_ep(&port);
683 port = of_get_parent(ep);
685 ep = port_to_endpoint(port);
688 ports = of_get_parent(port);
702 graph_parse_daifmt(ep, &daifmt, &bit_frame); /* (C) */
703 graph_parse_daifmt(port, &daifmt, &bit_frame); /* (B) */
704 if (of_node_name_eq(ports, "ports"))
705 graph_parse_daifmt(ports, &daifmt, &bit_frame); /* (A) */
709 * We need to flip clock_provider if it was CPU node,
710 * because it is Codec base.
712 daiclk = snd_soc_daifmt_clock_provider_from_bitmap(bit_frame);
714 daiclk = snd_soc_daifmt_clock_provider_flipped(daiclk);
716 dai_link->dai_fmt = daifmt | daiclk;
717 dai_link->init = asoc_simple_dai_init;
718 dai_link->ops = &graph_ops;
720 dai_link->ops = priv->ops;
723 int audio_graph2_link_normal(struct asoc_simple_priv *priv,
724 struct device_node *lnk,
725 struct link_info *li)
727 struct device_node *cpu_port = lnk;
728 struct device_node *cpu_ep = port_to_endpoint(cpu_port);
729 struct device_node *codec_port = of_graph_get_remote_port(cpu_ep);
735 * __graph_parse_node() :: DAI Naming
737 ret = graph_parse_node(priv, GRAPH_NORMAL, codec_port, li, 0);
742 * call CPU, and set DAI Name
744 ret = graph_parse_node(priv, GRAPH_NORMAL, cpu_port, li, 1);
748 graph_link_init(priv, cpu_port, li, 1);
750 of_node_put(codec_port);
755 EXPORT_SYMBOL_GPL(audio_graph2_link_normal);
757 int audio_graph2_link_dpcm(struct asoc_simple_priv *priv,
758 struct device_node *lnk,
759 struct link_info *li)
761 struct device_node *ep = port_to_endpoint(lnk);
762 struct device_node *rep = of_graph_get_remote_endpoint(ep);
763 struct device_node *rport = of_graph_get_remote_port(ep);
764 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
765 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
766 int is_cpu = asoc_graph_is_ports0(lnk);
774 * => lnk: port@0 { ep: { ... = rep }; };
785 * rport: port@0 { rep: { ... = ep } };
790 * setup CPU here, Codec is already set as dummy.
792 * asoc_simple_init_priv()
794 dai_link->dynamic = 1;
795 dai_link->dpcm_merged_format = 1;
797 ret = graph_parse_node(priv, GRAPH_DPCM, rport, li, 1);
809 * => lnk: port@0 { ep: { ... = rep; }; };
816 * rport: port@0 { rep: { ... = ep; }; };
821 * setup Codec here, CPU is already set as dummy.
823 * asoc_simple_init_priv()
827 dai_link->no_pcm = 1;
828 dai_link->be_hw_params_fixup = asoc_simple_be_hw_params_fixup;
830 ret = graph_parse_node(priv, GRAPH_DPCM, rport, li, 0);
835 graph_parse_convert(rep, dai_props);
837 snd_soc_dai_link_set_capabilities(dai_link);
839 graph_link_init(priv, rport, li, is_cpu);
847 EXPORT_SYMBOL_GPL(audio_graph2_link_dpcm);
849 int audio_graph2_link_c2c(struct asoc_simple_priv *priv,
850 struct device_node *lnk,
851 struct link_info *li)
853 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
854 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
855 struct snd_soc_pcm_stream *c2c_conf = dai_props->c2c_conf;
856 struct device_node *port0, *port1, *ports;
857 struct device_node *codec0_port, *codec1_port;
858 struct device_node *ep0, *ep1;
866 * => lnk: port@0 { c2c0_ep: { ... = codec0_ep; }; };
867 * port@1 { c2c1_ep: { ... = codec1_ep; }; };
873 * port@0 { codec0_ep: ... }; };
874 * port@1 { codec1_ep: ... }; };
880 ports = of_get_parent(port0);
881 port1 = of_get_next_child(ports, lnk);
883 if (!of_get_property(ports, "rate", &val)) {
884 struct device *dev = simple_priv_to_dev(priv);
886 dev_err(dev, "Codec2Codec needs rate settings\n");
890 c2c_conf->formats = SNDRV_PCM_FMTBIT_S32_LE; /* update ME */
892 c2c_conf->rate_max = val;
893 c2c_conf->channels_min =
894 c2c_conf->channels_max = 2; /* update ME */
895 dai_link->params = c2c_conf;
897 ep0 = port_to_endpoint(port0);
898 ep1 = port_to_endpoint(port1);
900 codec0_port = of_graph_get_remote_port(ep0);
901 codec1_port = of_graph_get_remote_port(ep1);
906 * __graph_parse_node() :: DAI Naming
908 ret = graph_parse_node(priv, GRAPH_C2C, codec1_port, li, 0);
913 * call CPU, and set DAI Name
915 ret = graph_parse_node(priv, GRAPH_C2C, codec0_port, li, 1);
919 graph_link_init(priv, codec0_port, li, 1);
923 of_node_put(codec0_port);
924 of_node_put(codec1_port);
932 EXPORT_SYMBOL_GPL(audio_graph2_link_c2c);
934 static int graph_link(struct asoc_simple_priv *priv,
935 struct graph2_custom_hooks *hooks,
936 enum graph_type gtype,
937 struct device_node *lnk,
938 struct link_info *li)
940 struct device *dev = simple_priv_to_dev(priv);
941 GRAPH2_CUSTOM func = NULL;
946 if (hooks && hooks->custom_normal)
947 func = hooks->custom_normal;
949 func = audio_graph2_link_normal;
952 if (hooks && hooks->custom_dpcm)
953 func = hooks->custom_dpcm;
955 func = audio_graph2_link_dpcm;
958 if (hooks && hooks->custom_c2c)
959 func = hooks->custom_c2c;
961 func = audio_graph2_link_c2c;
968 dev_err(dev, "non supported gtype (%d)\n", gtype);
972 ret = func(priv, lnk, li);
981 static int graph_counter(struct device_node *lnk)
988 * => lnk: port@0 { ... };
995 * ignore first lnk part
997 if (graph_lnk_is_multi(lnk))
998 return of_graph_get_endpoint_count(of_get_parent(lnk)) - 1;
1000 * Single CPU / Codec
1006 static int graph_count_normal(struct asoc_simple_priv *priv,
1007 struct device_node *lnk,
1008 struct link_info *li)
1010 struct device_node *cpu_port = lnk;
1011 struct device_node *cpu_ep = port_to_endpoint(cpu_port);
1012 struct device_node *codec_port = of_graph_get_remote_port(cpu_ep);
1016 * => lnk: port { endpoint { .. }; };
1019 li->num[li->link].cpus =
1020 li->num[li->link].platforms = graph_counter(cpu_port);
1021 li->num[li->link].codecs = graph_counter(codec_port);
1023 of_node_put(cpu_ep);
1024 of_node_put(codec_port);
1029 static int graph_count_dpcm(struct asoc_simple_priv *priv,
1030 struct device_node *lnk,
1031 struct link_info *li)
1033 struct device_node *ep = port_to_endpoint(lnk);
1034 struct device_node *rport = of_graph_get_remote_port(ep);
1040 * => lnk: port@0 { endpoint { ... }; };
1045 * => lnk: port@0 { endpoint { ... }; };
1051 if (asoc_graph_is_ports0(lnk)) {
1052 li->num[li->link].cpus = graph_counter(rport); /* FE */
1053 li->num[li->link].platforms = graph_counter(rport);
1055 li->num[li->link].codecs = graph_counter(rport); /* BE */
1064 static int graph_count_c2c(struct asoc_simple_priv *priv,
1065 struct device_node *lnk,
1066 struct link_info *li)
1068 struct device_node *ports = of_get_parent(lnk);
1069 struct device_node *port0 = lnk;
1070 struct device_node *port1 = of_get_next_child(ports, lnk);
1071 struct device_node *ep0 = port_to_endpoint(port0);
1072 struct device_node *ep1 = port_to_endpoint(port1);
1073 struct device_node *codec0 = of_graph_get_remote_port(ep0);
1074 struct device_node *codec1 = of_graph_get_remote_port(ep1);
1081 * => lnk: port@0 { endpoint { ... }; };
1082 * port@1 { endpoint { ... }; };
1086 li->num[li->link].cpus =
1087 li->num[li->link].platforms = graph_counter(codec0);
1088 li->num[li->link].codecs = graph_counter(codec1);
1089 li->num[li->link].c2c = 1;
1095 of_node_put(codec0);
1096 of_node_put(codec1);
1101 static int graph_count(struct asoc_simple_priv *priv,
1102 struct graph2_custom_hooks *hooks,
1103 enum graph_type gtype,
1104 struct device_node *lnk,
1105 struct link_info *li)
1107 struct device *dev = simple_priv_to_dev(priv);
1108 GRAPH2_CUSTOM func = NULL;
1111 if (li->link >= SNDRV_MAX_LINKS) {
1112 dev_err(dev, "too many links\n");
1118 func = graph_count_normal;
1121 func = graph_count_dpcm;
1124 func = graph_count_c2c;
1131 dev_err(dev, "non supported gtype (%d)\n", gtype);
1135 ret = func(priv, lnk, li);
1144 static int graph_for_each_link(struct asoc_simple_priv *priv,
1145 struct graph2_custom_hooks *hooks,
1146 struct link_info *li,
1147 int (*func)(struct asoc_simple_priv *priv,
1148 struct graph2_custom_hooks *hooks,
1149 enum graph_type gtype,
1150 struct device_node *lnk,
1151 struct link_info *li))
1153 struct of_phandle_iterator it;
1154 struct device *dev = simple_priv_to_dev(priv);
1155 struct device_node *node = dev->of_node;
1156 struct device_node *lnk;
1157 enum graph_type gtype;
1160 /* loop for all listed CPU port */
1161 of_for_each_phandle(&it, rc, node, "links", NULL, 0) {
1164 gtype = graph_get_type(priv, lnk);
1166 ret = func(priv, hooks, gtype, lnk, li);
1174 int audio_graph2_parse_of(struct asoc_simple_priv *priv, struct device *dev,
1175 struct graph2_custom_hooks *hooks)
1177 struct snd_soc_card *card = simple_priv_to_card(priv);
1178 struct link_info *li;
1181 li = devm_kzalloc(dev, sizeof(*li), GFP_KERNEL);
1185 card->probe = asoc_graph_card_probe;
1186 card->owner = THIS_MODULE;
1189 if ((hooks) && (hooks)->hook_pre) {
1190 ret = (hooks)->hook_pre(priv);
1195 ret = graph_for_each_link(priv, hooks, li, graph_count);
1201 ret = asoc_simple_init_priv(priv, li);
1205 priv->pa_gpio = devm_gpiod_get_optional(dev, "pa", GPIOD_OUT_LOW);
1206 if (IS_ERR(priv->pa_gpio)) {
1207 ret = PTR_ERR(priv->pa_gpio);
1208 dev_err(dev, "failed to get amplifier gpio: %d\n", ret);
1212 ret = asoc_simple_parse_widgets(card, NULL);
1216 ret = asoc_simple_parse_routing(card, NULL);
1220 memset(li, 0, sizeof(*li));
1221 ret = graph_for_each_link(priv, hooks, li, graph_link);
1225 ret = asoc_simple_parse_card_name(card, NULL);
1229 snd_soc_card_set_drvdata(card, priv);
1231 if ((hooks) && (hooks)->hook_post) {
1232 ret = (hooks)->hook_post(priv);
1237 asoc_simple_debug_info(priv);
1239 ret = devm_snd_soc_register_card(dev, card);
1241 devm_kfree(dev, li);
1244 dev_err_probe(dev, ret, "parse error\n");
1247 dev_warn(dev, "Audio Graph Card2 is still under Experimental stage\n");
1251 EXPORT_SYMBOL_GPL(audio_graph2_parse_of);
1253 static int graph_probe(struct platform_device *pdev)
1255 struct asoc_simple_priv *priv;
1256 struct device *dev = &pdev->dev;
1258 /* Allocate the private data and the DAI link array */
1259 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1263 return audio_graph2_parse_of(priv, dev, NULL);
1266 static const struct of_device_id graph_of_match[] = {
1267 { .compatible = "audio-graph-card2", },
1270 MODULE_DEVICE_TABLE(of, graph_of_match);
1272 static struct platform_driver graph_card = {
1274 .name = "asoc-audio-graph-card2",
1275 .pm = &snd_soc_pm_ops,
1276 .of_match_table = graph_of_match,
1278 .probe = graph_probe,
1279 .remove = asoc_simple_remove,
1281 module_platform_driver(graph_card);
1283 MODULE_ALIAS("platform:asoc-audio-graph-card2");
1284 MODULE_LICENSE("GPL v2");
1285 MODULE_DESCRIPTION("ASoC Audio Graph Card2");
1286 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");