5fe53c5efcbf6bc4b6ba9cf0ae204b8c455c17e1
[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(dev, top,   NULL,   adata);
184         asoc_simple_parse_convert(dev, node,  PREFIX, adata);
185         asoc_simple_parse_convert(dev, ports, NULL,   adata);
186         asoc_simple_parse_convert(dev, port,  NULL,   adata);
187         asoc_simple_parse_convert(dev, 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 = dai_link->cpus;
228         struct snd_soc_dai_link_component *codecs = dai_link->codecs;
229         int ret;
230
231         port    = of_get_parent(ep);
232         ports   = of_get_parent(port);
233         node    = of_graph_get_port_parent(ep);
234
235         li->link++;
236
237         dev_dbg(dev, "link_of DPCM (%pOF)\n", ep);
238
239         if (li->cpu) {
240                 int is_single_links = 0;
241
242                 /* Codec is dummy */
243                 codecs->of_node         = NULL;
244                 codecs->dai_name        = "snd-soc-dummy-dai";
245                 codecs->name            = "snd-soc-dummy";
246
247                 /* FE settings */
248                 dai_link->dynamic               = 1;
249                 dai_link->dpcm_merged_format    = 1;
250
251                 dai =
252                 dai_props->cpu_dai      = &priv->dais[li->dais++];
253
254                 ret = asoc_simple_parse_cpu(ep, dai_link, &is_single_links);
255                 if (ret)
256                         goto out_put_node;
257
258                 ret = asoc_simple_parse_clk_cpu(dev, ep, dai_link, dai);
259                 if (ret < 0)
260                         goto out_put_node;
261
262                 ret = asoc_simple_set_dailink_name(dev, dai_link,
263                                                    "fe.%pOFP.%s",
264                                                    cpus->of_node,
265                                                    cpus->dai_name);
266                 if (ret < 0)
267                         goto out_put_node;
268
269                 /*
270                  * In BE<->BE connections it is not required to create
271                  * PCM devices at CPU end of the dai link and thus 'no_pcm'
272                  * flag needs to be set. It is useful when there are many
273                  * BE components and some of these have to be connected to
274                  * form a valid audio path.
275                  *
276                  * For example: FE <-> BE1 <-> BE2 <-> ... <-> BEn where
277                  * there are 'n' BE components in the path.
278                  */
279                 if (card->component_chaining && !soc_component_is_pcm(cpus))
280                         dai_link->no_pcm = 1;
281
282                 /* card->num_links includes Codec */
283                 asoc_simple_canonicalize_cpu(dai_link, is_single_links);
284         } else {
285                 struct snd_soc_codec_conf *cconf;
286
287                 /* CPU is dummy */
288                 cpus->of_node           = NULL;
289                 cpus->dai_name          = "snd-soc-dummy-dai";
290                 cpus->name              = "snd-soc-dummy";
291
292                 /* BE settings */
293                 dai_link->no_pcm                = 1;
294                 dai_link->be_hw_params_fixup    = asoc_simple_be_hw_params_fixup;
295
296                 dai =
297                 dai_props->codec_dai    = &priv->dais[li->dais++];
298
299                 cconf =
300                 dai_props->codec_conf   = &priv->codec_conf[li->conf++];
301
302                 ret = asoc_simple_parse_codec(ep, dai_link);
303                 if (ret < 0)
304                         goto out_put_node;
305
306                 ret = asoc_simple_parse_clk_codec(dev, ep, dai_link, dai);
307                 if (ret < 0)
308                         goto out_put_node;
309
310                 ret = asoc_simple_set_dailink_name(dev, dai_link,
311                                                    "be.%pOFP.%s",
312                                                    codecs->of_node,
313                                                    codecs->dai_name);
314                 if (ret < 0)
315                         goto out_put_node;
316
317                 /* check "prefix" from top node */
318                 snd_soc_of_parse_node_prefix(top, cconf, codecs->of_node,
319                                               "prefix");
320                 snd_soc_of_parse_node_prefix(node, cconf, codecs->of_node,
321                                              PREFIX "prefix");
322                 snd_soc_of_parse_node_prefix(ports, cconf, codecs->of_node,
323                                              "prefix");
324                 snd_soc_of_parse_node_prefix(port, cconf, codecs->of_node,
325                                              "prefix");
326         }
327
328         graph_parse_convert(dev, ep, &dai_props->adata);
329         graph_parse_mclk_fs(top, ep, dai_props);
330
331         asoc_simple_canonicalize_platform(dai_link);
332
333         ret = asoc_simple_parse_tdm(ep, dai);
334         if (ret)
335                 goto out_put_node;
336
337         ret = asoc_simple_parse_daifmt(dev, cpu_ep, codec_ep,
338                                        NULL, &dai_link->dai_fmt);
339         if (ret < 0)
340                 goto out_put_node;
341
342         snd_soc_dai_link_set_capabilities(dai_link);
343
344         dai_link->ops                   = &graph_ops;
345
346         /* Use custom snd_soc_ops callbacks if available */
347         if (priv->ops)
348                 dai_link->ops = priv->ops;
349
350         dai_link->init                  = asoc_simple_dai_init;
351
352 out_put_node:
353         of_node_put(ports);
354         of_node_put(port);
355         of_node_put(node);
356         return ret;
357 }
358
359 static int graph_dai_link_of(struct asoc_simple_priv *priv,
360                              struct device_node *cpu_ep,
361                              struct device_node *codec_ep,
362                              struct link_info *li)
363 {
364         struct device *dev = simple_priv_to_dev(priv);
365         struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
366         struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
367         struct device_node *top = dev->of_node;
368         struct asoc_simple_dai *cpu_dai;
369         struct asoc_simple_dai *codec_dai;
370         int ret, single_cpu = 0;
371
372         dev_dbg(dev, "link_of (%pOF)\n", cpu_ep);
373
374         li->link++;
375
376         cpu_dai                 =
377         dai_props->cpu_dai      = &priv->dais[li->dais++];
378         codec_dai               =
379         dai_props->codec_dai    = &priv->dais[li->dais++];
380
381         /* Factor to mclk, used in hw_params() */
382         graph_parse_mclk_fs(top, cpu_ep,   dai_props);
383         graph_parse_mclk_fs(top, codec_ep, dai_props);
384
385         ret = asoc_simple_parse_daifmt(dev, cpu_ep, codec_ep,
386                                        NULL, &dai_link->dai_fmt);
387         if (ret < 0)
388                 return ret;
389
390         ret = asoc_simple_parse_cpu(cpu_ep, dai_link, &single_cpu);
391         if (ret < 0)
392                 return ret;
393
394         ret = asoc_simple_parse_codec(codec_ep, dai_link);
395         if (ret < 0)
396                 return ret;
397
398         ret = asoc_simple_parse_tdm(cpu_ep, cpu_dai);
399         if (ret < 0)
400                 return ret;
401
402         ret = asoc_simple_parse_tdm(codec_ep, codec_dai);
403         if (ret < 0)
404                 return ret;
405
406         ret = asoc_simple_parse_clk_cpu(dev, cpu_ep, dai_link, cpu_dai);
407         if (ret < 0)
408                 return ret;
409
410         ret = asoc_simple_parse_clk_codec(dev, codec_ep, dai_link, codec_dai);
411         if (ret < 0)
412                 return ret;
413
414         ret = asoc_simple_set_dailink_name(dev, dai_link,
415                                            "%s-%s",
416                                            dai_link->cpus->dai_name,
417                                            dai_link->codecs->dai_name);
418         if (ret < 0)
419                 return ret;
420
421         dai_link->ops = &graph_ops;
422         dai_link->init = asoc_simple_dai_init;
423
424         asoc_simple_canonicalize_cpu(dai_link, single_cpu);
425         asoc_simple_canonicalize_platform(dai_link);
426
427         return 0;
428 }
429
430 static inline bool parse_as_dpcm_link(struct asoc_simple_priv *priv,
431                                       struct device_node *codec_port,
432                                       struct asoc_simple_data *adata)
433 {
434         if (priv->force_dpcm)
435                 return true;
436
437         if (!priv->dpcm_selectable)
438                 return false;
439
440         /*
441          * It is DPCM
442          * if Codec port has many endpoints,
443          * or has convert-xxx property
444          */
445         if ((of_get_child_count(codec_port) > 1) ||
446             (adata->convert_rate || adata->convert_channels))
447                 return true;
448
449         return false;
450 }
451
452 static int __graph_for_each_link(struct asoc_simple_priv *priv,
453                         struct link_info *li,
454                         int (*func_noml)(struct asoc_simple_priv *priv,
455                                          struct device_node *cpu_ep,
456                                          struct device_node *codec_ep,
457                                          struct link_info *li),
458                         int (*func_dpcm)(struct asoc_simple_priv *priv,
459                                          struct device_node *cpu_ep,
460                                          struct device_node *codec_ep,
461                                          struct link_info *li))
462 {
463         struct of_phandle_iterator it;
464         struct device *dev = simple_priv_to_dev(priv);
465         struct device_node *node = dev->of_node;
466         struct device_node *cpu_port;
467         struct device_node *cpu_ep;
468         struct device_node *codec_ep;
469         struct device_node *codec_port;
470         struct device_node *codec_port_old = NULL;
471         struct asoc_simple_data adata;
472         int rc, ret = 0;
473
474         /* loop for all listed CPU port */
475         of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
476                 cpu_port = it.node;
477                 cpu_ep   = NULL;
478
479                 /* loop for all CPU endpoint */
480                 while (1) {
481                         cpu_ep = of_get_next_child(cpu_port, cpu_ep);
482                         if (!cpu_ep)
483                                 break;
484
485                         /* get codec */
486                         codec_ep = of_graph_get_remote_endpoint(cpu_ep);
487                         codec_port = of_get_parent(codec_ep);
488
489                         /* get convert-xxx property */
490                         memset(&adata, 0, sizeof(adata));
491                         graph_parse_convert(dev, codec_ep, &adata);
492                         graph_parse_convert(dev, cpu_ep,   &adata);
493
494                         /* check if link requires DPCM parsing */
495                         if (parse_as_dpcm_link(priv, codec_port, &adata)) {
496                                 /*
497                                  * Codec endpoint can be NULL for pluggable audio HW.
498                                  * Platform DT can populate the Codec endpoint depending on the
499                                  * plugged HW.
500                                  */
501                                 /* Do it all CPU endpoint, and 1st Codec endpoint */
502                                 if (li->cpu ||
503                                     ((codec_port_old != codec_port) && codec_ep))
504                                         ret = func_dpcm(priv, cpu_ep, codec_ep, li);
505                         /* else normal sound */
506                         } else {
507                                 if (li->cpu)
508                                         ret = func_noml(priv, cpu_ep, codec_ep, li);
509                         }
510
511                         of_node_put(codec_ep);
512                         of_node_put(codec_port);
513
514                         if (ret < 0)
515                                 return ret;
516
517                         codec_port_old = codec_port;
518                 }
519         }
520
521         return 0;
522 }
523
524 static int graph_for_each_link(struct asoc_simple_priv *priv,
525                                struct link_info *li,
526                                int (*func_noml)(struct asoc_simple_priv *priv,
527                                                 struct device_node *cpu_ep,
528                                                 struct device_node *codec_ep,
529                                                 struct link_info *li),
530                                int (*func_dpcm)(struct asoc_simple_priv *priv,
531                                                 struct device_node *cpu_ep,
532                                                 struct device_node *codec_ep,
533                                                 struct link_info *li))
534 {
535         int ret;
536         /*
537          * Detect all CPU first, and Detect all Codec 2nd.
538          *
539          * In Normal sound case, all DAIs are detected
540          * as "CPU-Codec".
541          *
542          * In DPCM sound case,
543          * all CPUs   are detected as "CPU-dummy", and
544          * all Codecs are detected as "dummy-Codec".
545          * To avoid random sub-device numbering,
546          * detect "dummy-Codec" in last;
547          */
548         for (li->cpu = 1; li->cpu >= 0; li->cpu--) {
549                 ret = __graph_for_each_link(priv, li, func_noml, func_dpcm);
550                 if (ret < 0)
551                         break;
552         }
553
554         return ret;
555 }
556
557 static void graph_get_dais_count(struct asoc_simple_priv *priv,
558                                  struct link_info *li);
559
560 int audio_graph_parse_of(struct asoc_simple_priv *priv, struct device *dev)
561 {
562         struct snd_soc_card *card = simple_priv_to_card(priv);
563         struct link_info li;
564         int ret;
565
566         card->owner = THIS_MODULE;
567         card->dev = dev;
568
569         memset(&li, 0, sizeof(li));
570         graph_get_dais_count(priv, &li);
571         if (!li.link || !li.dais)
572                 return -EINVAL;
573
574         ret = asoc_simple_init_priv(priv, &li);
575         if (ret < 0)
576                 return ret;
577
578         priv->pa_gpio = devm_gpiod_get_optional(dev, "pa", GPIOD_OUT_LOW);
579         if (IS_ERR(priv->pa_gpio)) {
580                 ret = PTR_ERR(priv->pa_gpio);
581                 dev_err(dev, "failed to get amplifier gpio: %d\n", ret);
582                 return ret;
583         }
584
585         ret = asoc_simple_parse_widgets(card, NULL);
586         if (ret < 0)
587                 return ret;
588
589         ret = asoc_simple_parse_routing(card, NULL);
590         if (ret < 0)
591                 return ret;
592
593         memset(&li, 0, sizeof(li));
594         ret = graph_for_each_link(priv, &li,
595                                   graph_dai_link_of,
596                                   graph_dai_link_of_dpcm);
597         if (ret < 0)
598                 goto err;
599
600         ret = asoc_simple_parse_card_name(card, NULL);
601         if (ret < 0)
602                 goto err;
603
604         snd_soc_card_set_drvdata(card, priv);
605
606         asoc_simple_debug_info(priv);
607
608         ret = devm_snd_soc_register_card(dev, card);
609         if (ret < 0)
610                 goto err;
611
612         return 0;
613
614 err:
615         asoc_simple_clean_reference(card);
616
617         if (ret != -EPROBE_DEFER)
618                 dev_err(dev, "parse error %d\n", ret);
619
620         return ret;
621 }
622 EXPORT_SYMBOL_GPL(audio_graph_parse_of);
623
624 static int graph_count_noml(struct asoc_simple_priv *priv,
625                             struct device_node *cpu_ep,
626                             struct device_node *codec_ep,
627                             struct link_info *li)
628 {
629         struct device *dev = simple_priv_to_dev(priv);
630
631         if (li->link >= SNDRV_MINOR_DEVICES) {
632                 dev_err(dev, "too many links\n");
633                 return -EINVAL;
634         }
635
636         li->num[li->link].cpus          = 1;
637         li->num[li->link].codecs        = 1;
638         li->num[li->link].platforms     = 1;
639
640         li->link += 1; /* 1xCPU-Codec */
641         li->dais += 2; /* 1xCPU + 1xCodec */
642
643         dev_dbg(dev, "Count As Normal\n");
644
645         return 0;
646 }
647
648 static int graph_count_dpcm(struct asoc_simple_priv *priv,
649                             struct device_node *cpu_ep,
650                             struct device_node *codec_ep,
651                             struct link_info *li)
652 {
653         struct device *dev = simple_priv_to_dev(priv);
654
655         if (li->link >= SNDRV_MINOR_DEVICES) {
656                 dev_err(dev, "too many links\n");
657                 return -EINVAL;
658         }
659
660         if (li->cpu) {
661                 li->num[li->link].cpus          = 1;
662                 li->num[li->link].codecs        = 1;
663                 li->num[li->link].platforms     = 1;
664
665                 li->link++; /* 1xCPU-dummy */
666                 li->dais++; /* 1xCPU */
667         } else {
668                 li->num[li->link].cpus          = 1;
669                 li->num[li->link].codecs        = 1;
670                 li->num[li->link].platforms     = 1;
671
672                 li->link++; /* 1xdummy-Codec */
673                 li->conf++; /* 1xdummy-Codec */
674                 li->dais++; /* 1xCodec */
675         }
676
677         dev_dbg(dev, "Count As DPCM\n");
678
679         return 0;
680 }
681
682 static void graph_get_dais_count(struct asoc_simple_priv *priv,
683                                  struct link_info *li)
684 {
685         struct device *dev = simple_priv_to_dev(priv);
686
687         /*
688          * link_num :   number of links.
689          *              CPU-Codec / CPU-dummy / dummy-Codec
690          * dais_num :   number of DAIs
691          * ccnf_num :   number of codec_conf
692          *              same number for "dummy-Codec"
693          *
694          * ex1)
695          * CPU0 --- Codec0      link : 5
696          * CPU1 --- Codec1      dais : 7
697          * CPU2 -/              ccnf : 1
698          * CPU3 --- Codec2
699          *
700          *      => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec
701          *      => 7 DAIs  = 4xCPU + 3xCodec
702          *      => 1 ccnf  = 1xdummy-Codec
703          *
704          * ex2)
705          * CPU0 --- Codec0      link : 5
706          * CPU1 --- Codec1      dais : 6
707          * CPU2 -/              ccnf : 1
708          * CPU3 -/
709          *
710          *      => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec
711          *      => 6 DAIs  = 4xCPU + 2xCodec
712          *      => 1 ccnf  = 1xdummy-Codec
713          *
714          * ex3)
715          * CPU0 --- Codec0      link : 6
716          * CPU1 -/              dais : 6
717          * CPU2 --- Codec1      ccnf : 2
718          * CPU3 -/
719          *
720          *      => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec
721          *      => 6 DAIs  = 4xCPU + 2xCodec
722          *      => 2 ccnf  = 2xdummy-Codec
723          *
724          * ex4)
725          * CPU0 --- Codec0 (convert-rate)       link : 3
726          * CPU1 --- Codec1                      dais : 4
727          *                                      ccnf : 1
728          *
729          *      => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec
730          *      => 4 DAIs  = 2xCPU + 2xCodec
731          *      => 1 ccnf  = 1xdummy-Codec
732          */
733         graph_for_each_link(priv, li,
734                             graph_count_noml,
735                             graph_count_dpcm);
736         dev_dbg(dev, "link %d, dais %d, ccnf %d\n",
737                 li->link, li->dais, li->conf);
738 }
739
740 int audio_graph_card_probe(struct snd_soc_card *card)
741 {
742         struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(card);
743         int ret;
744
745         ret = asoc_simple_init_hp(card, &priv->hp_jack, NULL);
746         if (ret < 0)
747                 return ret;
748
749         ret = asoc_simple_init_mic(card, &priv->mic_jack, NULL);
750         if (ret < 0)
751                 return ret;
752
753         return 0;
754 }
755 EXPORT_SYMBOL_GPL(audio_graph_card_probe);
756
757 static int graph_probe(struct platform_device *pdev)
758 {
759         struct asoc_simple_priv *priv;
760         struct device *dev = &pdev->dev;
761         struct snd_soc_card *card;
762
763         /* Allocate the private data and the DAI link array */
764         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
765         if (!priv)
766                 return -ENOMEM;
767
768         card = simple_priv_to_card(priv);
769         card->dapm_widgets      = graph_dapm_widgets;
770         card->num_dapm_widgets  = ARRAY_SIZE(graph_dapm_widgets);
771         card->probe             = audio_graph_card_probe;
772
773         if (of_device_get_match_data(dev))
774                 priv->dpcm_selectable = 1;
775
776         return audio_graph_parse_of(priv, dev);
777 }
778
779 int audio_graph_remove(struct platform_device *pdev)
780 {
781         struct snd_soc_card *card = platform_get_drvdata(pdev);
782
783         return asoc_simple_clean_reference(card);
784 }
785 EXPORT_SYMBOL_GPL(audio_graph_remove);
786
787 static const struct of_device_id graph_of_match[] = {
788         { .compatible = "audio-graph-card", },
789         { .compatible = "audio-graph-scu-card",
790           .data = (void *)DPCM_SELECTABLE },
791         {},
792 };
793 MODULE_DEVICE_TABLE(of, graph_of_match);
794
795 static struct platform_driver graph_card = {
796         .driver = {
797                 .name = "asoc-audio-graph-card",
798                 .pm = &snd_soc_pm_ops,
799                 .of_match_table = graph_of_match,
800         },
801         .probe = graph_probe,
802         .remove = audio_graph_remove,
803 };
804 module_platform_driver(graph_card);
805
806 MODULE_ALIAS("platform:asoc-audio-graph-card");
807 MODULE_LICENSE("GPL v2");
808 MODULE_DESCRIPTION("ASoC Audio Graph Sound Card");
809 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");