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