ASoC: audio-graph-card: fix refcount leak of cpu_ep in __graph_for_each_link()
[platform/kernel/linux-rpi.git] / sound / soc / generic / audio-graph-card.c
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // ASoC audio graph sound card support
4 //
5 // Copyright (C) 2016 Renesas Solutions Corp.
6 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 //
8 // based on ${LINUX}/sound/soc/generic/simple-card.c
9
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>
15 #include <linux/of.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>
22
23 #define DPCM_SELECTABLE 1
24
25 static int graph_outdrv_event(struct snd_soc_dapm_widget *w,
26                               struct snd_kcontrol *kcontrol,
27                               int event)
28 {
29         struct snd_soc_dapm_context *dapm = w->dapm;
30         struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(dapm->card);
31
32         switch (event) {
33         case SND_SOC_DAPM_POST_PMU:
34                 gpiod_set_value_cansleep(priv->pa_gpio, 1);
35                 break;
36         case SND_SOC_DAPM_PRE_PMD:
37                 gpiod_set_value_cansleep(priv->pa_gpio, 0);
38                 break;
39         default:
40                 return -EINVAL;
41         }
42
43         return 0;
44 }
45
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),
50 };
51
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,
56 };
57
58 static int graph_get_dai_id(struct device_node *ep)
59 {
60         struct device_node *node;
61         struct device_node *endpoint;
62         struct of_endpoint info;
63         int i, id;
64         const u32 *reg;
65         int ret;
66
67         /* use driver specified DAI ID if exist */
68         ret = snd_soc_get_dai_id(ep);
69         if (ret != -ENOTSUPP)
70                 return ret;
71
72         /* use endpoint/port reg if exist */
73         ret = of_graph_parse_endpoint(ep, &info);
74         if (ret == 0) {
75                 /*
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
80                  */
81                 if (of_get_property(ep,   "reg", NULL))
82                         return info.id;
83
84                 node = of_get_parent(ep);
85                 reg = of_get_property(node, "reg", NULL);
86                 of_node_put(node);
87                 if (reg)
88                         return info.port;
89         }
90         node = of_graph_get_port_parent(ep);
91
92         /*
93          * Non HDMI sound case, counting port/endpoint on its DT
94          * is enough. Let's count it.
95          */
96         i = 0;
97         id = -1;
98         for_each_endpoint_of_node(node, endpoint) {
99                 if (endpoint == ep)
100                         id = i;
101                 i++;
102         }
103
104         of_node_put(node);
105
106         if (id < 0)
107                 return -ENODEV;
108
109         return id;
110 }
111
112 static bool soc_component_is_pcm(struct snd_soc_dai_link_component *dlc)
113 {
114         struct snd_soc_dai *dai = snd_soc_find_dai_with_mutex(dlc);
115
116         if (dai && (dai->component->driver->pcm_construct ||
117                     dai->driver->pcm_new))
118                 return true;
119
120         return false;
121 }
122
123 static int asoc_simple_parse_dai(struct device_node *ep,
124                                  struct snd_soc_dai_link_component *dlc,
125                                  int *is_single_link)
126 {
127         struct device_node *node;
128         struct of_phandle_args args;
129         int ret;
130
131         if (!ep)
132                 return 0;
133
134         node = of_graph_get_port_parent(ep);
135
136         /* Get dai->name */
137         args.np         = node;
138         args.args[0]    = graph_get_dai_id(ep);
139         args.args_count = (of_graph_get_endpoint_count(node) > 1);
140
141         /*
142          * FIXME
143          *
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
147          * driver's pointer.
148          *
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.
153          *
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.
159          */
160         ret = snd_soc_get_dai_name(&args, &dlc->dai_name);
161         if (ret < 0) {
162                 of_node_put(node);
163                 return ret;
164         }
165
166         dlc->of_node = node;
167
168         if (is_single_link)
169                 *is_single_link = of_graph_get_endpoint_count(node) == 1;
170
171         return 0;
172 }
173
174 static void graph_parse_convert(struct device *dev,
175                                 struct device_node *ep,
176                                 struct asoc_simple_data *adata)
177 {
178         struct device_node *top = dev->of_node;
179         struct device_node *port = of_get_parent(ep);
180         struct device_node *ports = of_get_parent(port);
181         struct device_node *node = of_graph_get_port_parent(ep);
182
183         asoc_simple_parse_convert(top,   NULL,   adata);
184         if (of_node_name_eq(ports, "ports"))
185                 asoc_simple_parse_convert(ports, NULL, adata);
186         asoc_simple_parse_convert(port,  NULL,   adata);
187         asoc_simple_parse_convert(ep,    NULL,   adata);
188
189         of_node_put(port);
190         of_node_put(ports);
191         of_node_put(node);
192 }
193
194 static void graph_parse_mclk_fs(struct device_node *top,
195                                 struct device_node *ep,
196                                 struct simple_dai_props *props)
197 {
198         struct device_node *port        = of_get_parent(ep);
199         struct device_node *ports       = of_get_parent(port);
200
201         of_property_read_u32(top,       "mclk-fs", &props->mclk_fs);
202         if (of_node_name_eq(ports, "ports"))
203                 of_property_read_u32(ports, "mclk-fs", &props->mclk_fs);
204         of_property_read_u32(port,      "mclk-fs", &props->mclk_fs);
205         of_property_read_u32(ep,        "mclk-fs", &props->mclk_fs);
206
207         of_node_put(port);
208         of_node_put(ports);
209 }
210
211 static int graph_parse_node(struct asoc_simple_priv *priv,
212                             struct device_node *ep,
213                             struct link_info *li,
214                             int *cpu)
215 {
216         struct device *dev = simple_priv_to_dev(priv);
217         struct device_node *top = dev->of_node;
218         struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
219         struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
220         struct snd_soc_dai_link_component *dlc;
221         struct asoc_simple_dai *dai;
222         int ret;
223
224         if (cpu) {
225                 dlc = asoc_link_to_cpu(dai_link, 0);
226                 dai = simple_props_to_dai_cpu(dai_props, 0);
227         } else {
228                 dlc = asoc_link_to_codec(dai_link, 0);
229                 dai = simple_props_to_dai_codec(dai_props, 0);
230         }
231
232         graph_parse_mclk_fs(top, ep, dai_props);
233
234         ret = asoc_simple_parse_dai(ep, dlc, cpu);
235         if (ret < 0)
236                 return ret;
237
238         ret = asoc_simple_parse_tdm(ep, dai);
239         if (ret < 0)
240                 return ret;
241
242         ret = asoc_simple_parse_clk(dev, ep, dai, dlc);
243         if (ret < 0)
244                 return ret;
245
246         return 0;
247 }
248
249 static int graph_link_init(struct asoc_simple_priv *priv,
250                            struct device_node *cpu_ep,
251                            struct device_node *codec_ep,
252                            struct link_info *li,
253                            char *name)
254 {
255         struct device *dev = simple_priv_to_dev(priv);
256         struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
257         int ret;
258
259         ret = asoc_simple_parse_daifmt(dev, cpu_ep, codec_ep,
260                                        NULL, &dai_link->dai_fmt);
261         if (ret < 0)
262                 return ret;
263
264         dai_link->init          = asoc_simple_dai_init;
265         dai_link->ops           = &graph_ops;
266         if (priv->ops)
267                 dai_link->ops   = priv->ops;
268
269         return asoc_simple_set_dailink_name(dev, dai_link, name);
270 }
271
272 static int graph_dai_link_of_dpcm(struct asoc_simple_priv *priv,
273                                   struct device_node *cpu_ep,
274                                   struct device_node *codec_ep,
275                                   struct link_info *li)
276 {
277         struct device *dev = simple_priv_to_dev(priv);
278         struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
279         struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
280         struct device_node *top = dev->of_node;
281         struct device_node *ep = li->cpu ? cpu_ep : codec_ep;
282         char dai_name[64];
283         int ret;
284
285         dev_dbg(dev, "link_of DPCM (%pOF)\n", ep);
286
287         if (li->cpu) {
288                 struct snd_soc_card *card = simple_priv_to_card(priv);
289                 struct snd_soc_dai_link_component *cpus = asoc_link_to_cpu(dai_link, 0);
290                 struct snd_soc_dai_link_component *platforms = asoc_link_to_platform(dai_link, 0);
291                 int is_single_links = 0;
292
293                 /* Codec is dummy */
294
295                 /* FE settings */
296                 dai_link->dynamic               = 1;
297                 dai_link->dpcm_merged_format    = 1;
298
299                 ret = graph_parse_node(priv, cpu_ep, li, &is_single_links);
300                 if (ret)
301                         return ret;
302
303                 snprintf(dai_name, sizeof(dai_name),
304                          "fe.%pOFP.%s", cpus->of_node, cpus->dai_name);
305                 /*
306                  * In BE<->BE connections it is not required to create
307                  * PCM devices at CPU end of the dai link and thus 'no_pcm'
308                  * flag needs to be set. It is useful when there are many
309                  * BE components and some of these have to be connected to
310                  * form a valid audio path.
311                  *
312                  * For example: FE <-> BE1 <-> BE2 <-> ... <-> BEn where
313                  * there are 'n' BE components in the path.
314                  */
315                 if (card->component_chaining && !soc_component_is_pcm(cpus))
316                         dai_link->no_pcm = 1;
317
318                 asoc_simple_canonicalize_cpu(cpus, is_single_links);
319                 asoc_simple_canonicalize_platform(platforms, cpus);
320         } else {
321                 struct snd_soc_codec_conf *cconf = simple_props_to_codec_conf(dai_props, 0);
322                 struct snd_soc_dai_link_component *codecs = asoc_link_to_codec(dai_link, 0);
323                 struct device_node *port;
324                 struct device_node *ports;
325
326                 /* CPU is dummy */
327
328                 /* BE settings */
329                 dai_link->no_pcm                = 1;
330                 dai_link->be_hw_params_fixup    = asoc_simple_be_hw_params_fixup;
331
332                 ret = graph_parse_node(priv, codec_ep, li, NULL);
333                 if (ret < 0)
334                         return ret;
335
336                 snprintf(dai_name, sizeof(dai_name),
337                          "be.%pOFP.%s", codecs->of_node, codecs->dai_name);
338
339                 /* check "prefix" from top node */
340                 port = of_get_parent(ep);
341                 ports = of_get_parent(port);
342                 snd_soc_of_parse_node_prefix(top, cconf, codecs->of_node,
343                                               "prefix");
344                 if (of_node_name_eq(ports, "ports"))
345                         snd_soc_of_parse_node_prefix(ports, cconf, codecs->of_node, "prefix");
346                 snd_soc_of_parse_node_prefix(port, cconf, codecs->of_node,
347                                              "prefix");
348
349                 of_node_put(ports);
350                 of_node_put(port);
351         }
352
353         graph_parse_convert(dev, ep, &dai_props->adata);
354
355         snd_soc_dai_link_set_capabilities(dai_link);
356
357         ret = graph_link_init(priv, cpu_ep, codec_ep, li, dai_name);
358
359         li->link++;
360
361         return ret;
362 }
363
364 static int graph_dai_link_of(struct asoc_simple_priv *priv,
365                              struct device_node *cpu_ep,
366                              struct device_node *codec_ep,
367                              struct link_info *li)
368 {
369         struct device *dev = simple_priv_to_dev(priv);
370         struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
371         struct snd_soc_dai_link_component *cpus = asoc_link_to_cpu(dai_link, 0);
372         struct snd_soc_dai_link_component *codecs = asoc_link_to_codec(dai_link, 0);
373         struct snd_soc_dai_link_component *platforms = asoc_link_to_platform(dai_link, 0);
374         char dai_name[64];
375         int ret, is_single_links = 0;
376
377         dev_dbg(dev, "link_of (%pOF)\n", cpu_ep);
378
379         ret = graph_parse_node(priv, cpu_ep, li, &is_single_links);
380         if (ret < 0)
381                 return ret;
382
383         ret = graph_parse_node(priv, codec_ep, li, NULL);
384         if (ret < 0)
385                 return ret;
386
387         snprintf(dai_name, sizeof(dai_name),
388                  "%s-%s", cpus->dai_name, codecs->dai_name);
389
390         asoc_simple_canonicalize_cpu(cpus, is_single_links);
391         asoc_simple_canonicalize_platform(platforms, cpus);
392
393         ret = graph_link_init(priv, cpu_ep, codec_ep, li, dai_name);
394         if (ret < 0)
395                 return ret;
396
397         li->link++;
398
399         return 0;
400 }
401
402 static inline bool parse_as_dpcm_link(struct asoc_simple_priv *priv,
403                                       struct device_node *codec_port,
404                                       struct asoc_simple_data *adata)
405 {
406         if (priv->force_dpcm)
407                 return true;
408
409         if (!priv->dpcm_selectable)
410                 return false;
411
412         /*
413          * It is DPCM
414          * if Codec port has many endpoints,
415          * or has convert-xxx property
416          */
417         if ((of_get_child_count(codec_port) > 1) ||
418             (adata->convert_rate || adata->convert_channels))
419                 return true;
420
421         return false;
422 }
423
424 static int __graph_for_each_link(struct asoc_simple_priv *priv,
425                         struct link_info *li,
426                         int (*func_noml)(struct asoc_simple_priv *priv,
427                                          struct device_node *cpu_ep,
428                                          struct device_node *codec_ep,
429                                          struct link_info *li),
430                         int (*func_dpcm)(struct asoc_simple_priv *priv,
431                                          struct device_node *cpu_ep,
432                                          struct device_node *codec_ep,
433                                          struct link_info *li))
434 {
435         struct of_phandle_iterator it;
436         struct device *dev = simple_priv_to_dev(priv);
437         struct device_node *node = dev->of_node;
438         struct device_node *cpu_port;
439         struct device_node *cpu_ep;
440         struct device_node *codec_ep;
441         struct device_node *codec_port;
442         struct device_node *codec_port_old = NULL;
443         struct asoc_simple_data adata;
444         int rc, ret = 0;
445
446         /* loop for all listed CPU port */
447         of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
448                 cpu_port = it.node;
449                 cpu_ep   = NULL;
450
451                 /* loop for all CPU endpoint */
452                 while (1) {
453                         cpu_ep = of_get_next_child(cpu_port, cpu_ep);
454                         if (!cpu_ep)
455                                 break;
456
457                         /* get codec */
458                         codec_ep = of_graph_get_remote_endpoint(cpu_ep);
459                         codec_port = of_get_parent(codec_ep);
460
461                         /* get convert-xxx property */
462                         memset(&adata, 0, sizeof(adata));
463                         graph_parse_convert(dev, codec_ep, &adata);
464                         graph_parse_convert(dev, cpu_ep,   &adata);
465
466                         /* check if link requires DPCM parsing */
467                         if (parse_as_dpcm_link(priv, codec_port, &adata)) {
468                                 /*
469                                  * Codec endpoint can be NULL for pluggable audio HW.
470                                  * Platform DT can populate the Codec endpoint depending on the
471                                  * plugged HW.
472                                  */
473                                 /* Do it all CPU endpoint, and 1st Codec endpoint */
474                                 if (li->cpu ||
475                                     ((codec_port_old != codec_port) && codec_ep))
476                                         ret = func_dpcm(priv, cpu_ep, codec_ep, li);
477                         /* else normal sound */
478                         } else {
479                                 if (li->cpu)
480                                         ret = func_noml(priv, cpu_ep, codec_ep, li);
481                         }
482
483                         of_node_put(codec_ep);
484                         of_node_put(codec_port);
485
486                         if (ret < 0) {
487                                 of_node_put(cpu_ep);
488                                 return ret;
489                         }
490
491                         codec_port_old = codec_port;
492                 }
493         }
494
495         return 0;
496 }
497
498 static int graph_for_each_link(struct asoc_simple_priv *priv,
499                                struct link_info *li,
500                                int (*func_noml)(struct asoc_simple_priv *priv,
501                                                 struct device_node *cpu_ep,
502                                                 struct device_node *codec_ep,
503                                                 struct link_info *li),
504                                int (*func_dpcm)(struct asoc_simple_priv *priv,
505                                                 struct device_node *cpu_ep,
506                                                 struct device_node *codec_ep,
507                                                 struct link_info *li))
508 {
509         int ret;
510         /*
511          * Detect all CPU first, and Detect all Codec 2nd.
512          *
513          * In Normal sound case, all DAIs are detected
514          * as "CPU-Codec".
515          *
516          * In DPCM sound case,
517          * all CPUs   are detected as "CPU-dummy", and
518          * all Codecs are detected as "dummy-Codec".
519          * To avoid random sub-device numbering,
520          * detect "dummy-Codec" in last;
521          */
522         for (li->cpu = 1; li->cpu >= 0; li->cpu--) {
523                 ret = __graph_for_each_link(priv, li, func_noml, func_dpcm);
524                 if (ret < 0)
525                         break;
526         }
527
528         return ret;
529 }
530
531 static int graph_get_dais_count(struct asoc_simple_priv *priv,
532                                 struct link_info *li);
533
534 int audio_graph_parse_of(struct asoc_simple_priv *priv, struct device *dev)
535 {
536         struct snd_soc_card *card = simple_priv_to_card(priv);
537         struct link_info *li;
538         int ret;
539
540         li = devm_kzalloc(dev, sizeof(*li), GFP_KERNEL);
541         if (!li)
542                 return -ENOMEM;
543
544         card->owner = THIS_MODULE;
545         card->dev = dev;
546
547         ret = graph_get_dais_count(priv, li);
548         if (ret < 0)
549                 return ret;
550
551         if (!li->link)
552                 return -EINVAL;
553
554         ret = asoc_simple_init_priv(priv, li);
555         if (ret < 0)
556                 return ret;
557
558         priv->pa_gpio = devm_gpiod_get_optional(dev, "pa", GPIOD_OUT_LOW);
559         if (IS_ERR(priv->pa_gpio)) {
560                 ret = PTR_ERR(priv->pa_gpio);
561                 dev_err(dev, "failed to get amplifier gpio: %d\n", ret);
562                 return ret;
563         }
564
565         ret = asoc_simple_parse_widgets(card, NULL);
566         if (ret < 0)
567                 return ret;
568
569         ret = asoc_simple_parse_routing(card, NULL);
570         if (ret < 0)
571                 return ret;
572
573         memset(li, 0, sizeof(*li));
574         ret = graph_for_each_link(priv, li,
575                                   graph_dai_link_of,
576                                   graph_dai_link_of_dpcm);
577         if (ret < 0)
578                 goto err;
579
580         ret = asoc_simple_parse_card_name(card, NULL);
581         if (ret < 0)
582                 goto err;
583
584         snd_soc_card_set_drvdata(card, priv);
585
586         asoc_simple_debug_info(priv);
587
588         ret = devm_snd_soc_register_card(dev, card);
589         if (ret < 0)
590                 goto err;
591
592         devm_kfree(dev, li);
593         return 0;
594
595 err:
596         asoc_simple_clean_reference(card);
597
598         if (ret != -EPROBE_DEFER)
599                 dev_err(dev, "parse error %d\n", ret);
600
601         return ret;
602 }
603 EXPORT_SYMBOL_GPL(audio_graph_parse_of);
604
605 static int graph_count_noml(struct asoc_simple_priv *priv,
606                             struct device_node *cpu_ep,
607                             struct device_node *codec_ep,
608                             struct link_info *li)
609 {
610         struct device *dev = simple_priv_to_dev(priv);
611
612         if (li->link >= SNDRV_MAX_LINKS) {
613                 dev_err(dev, "too many links\n");
614                 return -EINVAL;
615         }
616
617         li->num[li->link].cpus          = 1;
618         li->num[li->link].codecs        = 1;
619         li->num[li->link].platforms     = 1;
620
621         li->link += 1; /* 1xCPU-Codec */
622
623         dev_dbg(dev, "Count As Normal\n");
624
625         return 0;
626 }
627
628 static int graph_count_dpcm(struct asoc_simple_priv *priv,
629                             struct device_node *cpu_ep,
630                             struct device_node *codec_ep,
631                             struct link_info *li)
632 {
633         struct device *dev = simple_priv_to_dev(priv);
634
635         if (li->link >= SNDRV_MAX_LINKS) {
636                 dev_err(dev, "too many links\n");
637                 return -EINVAL;
638         }
639
640         if (li->cpu) {
641                 li->num[li->link].cpus          = 1;
642                 li->num[li->link].platforms     = 1;
643
644                 li->link++; /* 1xCPU-dummy */
645         } else {
646                 li->num[li->link].codecs        = 1;
647
648                 li->link++; /* 1xdummy-Codec */
649         }
650
651         dev_dbg(dev, "Count As DPCM\n");
652
653         return 0;
654 }
655
656 static int graph_get_dais_count(struct asoc_simple_priv *priv,
657                                 struct link_info *li)
658 {
659         /*
660          * link_num :   number of links.
661          *              CPU-Codec / CPU-dummy / dummy-Codec
662          * dais_num :   number of DAIs
663          * ccnf_num :   number of codec_conf
664          *              same number for "dummy-Codec"
665          *
666          * ex1)
667          * CPU0 --- Codec0      link : 5
668          * CPU1 --- Codec1      dais : 7
669          * CPU2 -/              ccnf : 1
670          * CPU3 --- Codec2
671          *
672          *      => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec
673          *      => 7 DAIs  = 4xCPU + 3xCodec
674          *      => 1 ccnf  = 1xdummy-Codec
675          *
676          * ex2)
677          * CPU0 --- Codec0      link : 5
678          * CPU1 --- Codec1      dais : 6
679          * CPU2 -/              ccnf : 1
680          * CPU3 -/
681          *
682          *      => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec
683          *      => 6 DAIs  = 4xCPU + 2xCodec
684          *      => 1 ccnf  = 1xdummy-Codec
685          *
686          * ex3)
687          * CPU0 --- Codec0      link : 6
688          * CPU1 -/              dais : 6
689          * CPU2 --- Codec1      ccnf : 2
690          * CPU3 -/
691          *
692          *      => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec
693          *      => 6 DAIs  = 4xCPU + 2xCodec
694          *      => 2 ccnf  = 2xdummy-Codec
695          *
696          * ex4)
697          * CPU0 --- Codec0 (convert-rate)       link : 3
698          * CPU1 --- Codec1                      dais : 4
699          *                                      ccnf : 1
700          *
701          *      => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec
702          *      => 4 DAIs  = 2xCPU + 2xCodec
703          *      => 1 ccnf  = 1xdummy-Codec
704          */
705         return graph_for_each_link(priv, li,
706                                    graph_count_noml,
707                                    graph_count_dpcm);
708 }
709
710 static int graph_probe(struct platform_device *pdev)
711 {
712         struct asoc_simple_priv *priv;
713         struct device *dev = &pdev->dev;
714         struct snd_soc_card *card;
715
716         /* Allocate the private data and the DAI link array */
717         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
718         if (!priv)
719                 return -ENOMEM;
720
721         card = simple_priv_to_card(priv);
722         card->dapm_widgets      = graph_dapm_widgets;
723         card->num_dapm_widgets  = ARRAY_SIZE(graph_dapm_widgets);
724         card->probe             = asoc_graph_card_probe;
725
726         if (of_device_get_match_data(dev))
727                 priv->dpcm_selectable = 1;
728
729         return audio_graph_parse_of(priv, dev);
730 }
731
732 static const struct of_device_id graph_of_match[] = {
733         { .compatible = "audio-graph-card", },
734         { .compatible = "audio-graph-scu-card",
735           .data = (void *)DPCM_SELECTABLE },
736         {},
737 };
738 MODULE_DEVICE_TABLE(of, graph_of_match);
739
740 static struct platform_driver graph_card = {
741         .driver = {
742                 .name = "asoc-audio-graph-card",
743                 .pm = &snd_soc_pm_ops,
744                 .of_match_table = graph_of_match,
745         },
746         .probe = graph_probe,
747         .remove = asoc_simple_remove,
748 };
749 module_platform_driver(graph_card);
750
751 MODULE_ALIAS("platform:asoc-audio-graph-card");
752 MODULE_LICENSE("GPL v2");
753 MODULE_DESCRIPTION("ASoC Audio Graph Sound Card");
754 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");