1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // ASoC simple sound card support
4 //
5 // Copyright (C) 2012 Renesas Solutions Corp.
6 // Kuninori Morimoto <[email protected]>
7
8 #include <linux/cleanup.h>
9 #include <linux/clk.h>
10 #include <linux/device.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/of_platform.h>
14 #include <linux/platform_device.h>
15 #include <linux/string.h>
16 #include <sound/simple_card.h>
17 #include <sound/soc-dai.h>
18 #include <sound/soc.h>
19
20 #define DPCM_SELECTABLE 1
21
22 #define DAI "sound-dai"
23 #define CELL "#sound-dai-cells"
24 #define PREFIX "simple-audio-card,"
25
26 static const struct snd_soc_ops simple_ops = {
27 .startup = simple_util_startup,
28 .shutdown = simple_util_shutdown,
29 .hw_params = simple_util_hw_params,
30 };
31
simple_parse_platform(struct device_node * node,struct snd_soc_dai_link_component * dlc)32 static int simple_parse_platform(struct device_node *node, struct snd_soc_dai_link_component *dlc)
33 {
34 struct of_phandle_args args;
35 int ret;
36
37 if (!node)
38 return 0;
39
40 /*
41 * Get node via "sound-dai = <&phandle port>"
42 * it will be used as xxx_of_node on soc_bind_dai_link()
43 */
44 ret = of_parse_phandle_with_args(node, DAI, CELL, 0, &args);
45 if (ret)
46 return ret;
47
48 /* dai_name is not required and may not exist for plat component */
49
50 dlc->of_node = args.np;
51
52 return 0;
53 }
54
simple_parse_dai(struct device * dev,struct device_node * node,struct snd_soc_dai_link_component * dlc,int * is_single_link)55 static int simple_parse_dai(struct device *dev,
56 struct device_node *node,
57 struct snd_soc_dai_link_component *dlc,
58 int *is_single_link)
59 {
60 struct of_phandle_args args;
61 struct snd_soc_dai *dai;
62 int ret;
63
64 if (!node)
65 return 0;
66
67 /*
68 * Get node via "sound-dai = <&phandle port>"
69 * it will be used as xxx_of_node on soc_bind_dai_link()
70 */
71 ret = of_parse_phandle_with_args(node, DAI, CELL, 0, &args);
72 if (ret)
73 return ret;
74
75 /*
76 * Try to find from DAI args
77 */
78 dai = snd_soc_get_dai_via_args(&args);
79 if (dai) {
80 dlc->dai_name = snd_soc_dai_name_get(dai);
81 dlc->dai_args = snd_soc_copy_dai_args(dev, &args);
82 if (!dlc->dai_args)
83 return -ENOMEM;
84
85 goto parse_dai_end;
86 }
87
88 /*
89 * FIXME
90 *
91 * Here, dlc->dai_name is pointer to CPU/Codec DAI name.
92 * If user unbinded CPU or Codec driver, but not for Sound Card,
93 * dlc->dai_name is keeping unbinded CPU or Codec
94 * driver's pointer.
95 *
96 * If user re-bind CPU or Codec driver again, ALSA SoC will try
97 * to rebind Card via snd_soc_try_rebind_card(), but because of
98 * above reason, it might can't bind Sound Card.
99 * Because Sound Card is pointing to released dai_name pointer.
100 *
101 * To avoid this rebind Card issue,
102 * 1) It needs to alloc memory to keep dai_name eventhough
103 * CPU or Codec driver was unbinded, or
104 * 2) user need to rebind Sound Card everytime
105 * if he unbinded CPU or Codec.
106 */
107 ret = snd_soc_get_dlc(&args, dlc);
108 if (ret < 0)
109 return ret;
110
111 parse_dai_end:
112 if (is_single_link)
113 *is_single_link = !args.args_count;
114
115 return 0;
116 }
117
simple_parse_convert(struct device * dev,struct device_node * np,struct simple_util_data * adata)118 static void simple_parse_convert(struct device *dev,
119 struct device_node *np,
120 struct simple_util_data *adata)
121 {
122 struct device_node *top = dev->of_node;
123 struct device_node *node __free(device_node) = of_get_parent(np);
124
125 simple_util_parse_convert(top, PREFIX, adata);
126 simple_util_parse_convert(node, PREFIX, adata);
127 simple_util_parse_convert(node, NULL, adata);
128 simple_util_parse_convert(np, NULL, adata);
129 }
130
simple_parse_node(struct simple_util_priv * priv,struct device_node * np,struct link_info * li,char * prefix,int * cpu)131 static int simple_parse_node(struct simple_util_priv *priv,
132 struct device_node *np,
133 struct link_info *li,
134 char *prefix,
135 int *cpu)
136 {
137 struct device *dev = simple_priv_to_dev(priv);
138 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
139 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
140 struct snd_soc_dai_link_component *dlc;
141 struct simple_util_dai *dai;
142 int ret;
143
144 if (cpu) {
145 dlc = snd_soc_link_to_cpu(dai_link, 0);
146 dai = simple_props_to_dai_cpu(dai_props, 0);
147 } else {
148 dlc = snd_soc_link_to_codec(dai_link, 0);
149 dai = simple_props_to_dai_codec(dai_props, 0);
150 }
151
152 ret = simple_parse_dai(dev, np, dlc, cpu);
153 if (ret)
154 return ret;
155
156 ret = simple_util_parse_clk(dev, np, dai, dlc);
157 if (ret)
158 return ret;
159
160 ret = simple_util_parse_tdm(np, dai);
161 if (ret)
162 return ret;
163
164 return 0;
165 }
166
simple_link_init(struct simple_util_priv * priv,struct device_node * cpu,struct device_node * codec,struct link_info * li,char * prefix,char * name)167 static int simple_link_init(struct simple_util_priv *priv,
168 struct device_node *cpu,
169 struct device_node *codec,
170 struct link_info *li,
171 char *prefix, char *name)
172 {
173 struct device *dev = simple_priv_to_dev(priv);
174 struct device_node *top = dev->of_node;
175 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
176 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
177 struct device_node *node __free(device_node) = of_get_parent(cpu);
178 enum snd_soc_trigger_order trigger_start = SND_SOC_TRIGGER_ORDER_DEFAULT;
179 enum snd_soc_trigger_order trigger_stop = SND_SOC_TRIGGER_ORDER_DEFAULT;
180 bool playback_only = 0, capture_only = 0;
181 int ret;
182
183 ret = simple_util_parse_daifmt(dev, node, codec,
184 prefix, &dai_link->dai_fmt);
185 if (ret < 0)
186 return ret;
187
188 graph_util_parse_link_direction(top, &playback_only, &capture_only);
189 graph_util_parse_link_direction(node, &playback_only, &capture_only);
190 graph_util_parse_link_direction(cpu, &playback_only, &capture_only);
191 graph_util_parse_link_direction(codec, &playback_only, &capture_only);
192
193 of_property_read_u32(top, "mclk-fs", &dai_props->mclk_fs);
194 of_property_read_u32(top, PREFIX "mclk-fs", &dai_props->mclk_fs);
195 of_property_read_u32(node, "mclk-fs", &dai_props->mclk_fs);
196 of_property_read_u32(node, PREFIX "mclk-fs", &dai_props->mclk_fs);
197 of_property_read_u32(cpu, "mclk-fs", &dai_props->mclk_fs);
198 of_property_read_u32(cpu, PREFIX "mclk-fs", &dai_props->mclk_fs);
199 of_property_read_u32(codec, "mclk-fs", &dai_props->mclk_fs);
200 of_property_read_u32(codec, PREFIX "mclk-fs", &dai_props->mclk_fs);
201
202 graph_util_parse_trigger_order(priv, top, &trigger_start, &trigger_stop);
203 graph_util_parse_trigger_order(priv, node, &trigger_start, &trigger_stop);
204 graph_util_parse_trigger_order(priv, cpu, &trigger_start, &trigger_stop);
205 graph_util_parse_trigger_order(priv, codec, &trigger_start, &trigger_stop);
206
207 dai_link->playback_only = playback_only;
208 dai_link->capture_only = capture_only;
209
210 dai_link->trigger_start = trigger_start;
211 dai_link->trigger_stop = trigger_stop;
212
213 dai_link->init = simple_util_dai_init;
214 dai_link->ops = &simple_ops;
215
216 return simple_util_set_dailink_name(dev, dai_link, name);
217 }
218
simple_dai_link_of_dpcm(struct simple_util_priv * priv,struct device_node * np,struct device_node * codec,struct link_info * li,bool is_top)219 static int simple_dai_link_of_dpcm(struct simple_util_priv *priv,
220 struct device_node *np,
221 struct device_node *codec,
222 struct link_info *li,
223 bool is_top)
224 {
225 struct device *dev = simple_priv_to_dev(priv);
226 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
227 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
228 struct device_node *top = dev->of_node;
229 struct device_node *node __free(device_node) = of_get_parent(np);
230 char *prefix = "";
231 char dai_name[64];
232 int ret;
233
234 dev_dbg(dev, "link_of DPCM (%pOF)\n", np);
235
236 /* For single DAI link & old style of DT node */
237 if (is_top)
238 prefix = PREFIX;
239
240 if (li->cpu) {
241 struct snd_soc_dai_link_component *cpus = snd_soc_link_to_cpu(dai_link, 0);
242 struct snd_soc_dai_link_component *platforms = snd_soc_link_to_platform(dai_link, 0);
243 int is_single_links = 0;
244
245 /* Codec is dummy */
246
247 /* FE settings */
248 dai_link->dynamic = 1;
249 dai_link->dpcm_merged_format = 1;
250
251 ret = simple_parse_node(priv, np, li, prefix, &is_single_links);
252 if (ret < 0)
253 goto out_put_node;
254
255 snprintf(dai_name, sizeof(dai_name), "fe.%s", cpus->dai_name);
256
257 simple_util_canonicalize_cpu(cpus, is_single_links);
258 simple_util_canonicalize_platform(platforms, cpus);
259 } else {
260 struct snd_soc_dai_link_component *codecs = snd_soc_link_to_codec(dai_link, 0);
261 struct snd_soc_codec_conf *cconf;
262
263 /* CPU is dummy */
264
265 /* BE settings */
266 dai_link->no_pcm = 1;
267 dai_link->be_hw_params_fixup = simple_util_be_hw_params_fixup;
268
269 cconf = simple_props_to_codec_conf(dai_props, 0);
270
271 ret = simple_parse_node(priv, np, li, prefix, NULL);
272 if (ret < 0)
273 goto out_put_node;
274
275 snprintf(dai_name, sizeof(dai_name), "be.%s", codecs->dai_name);
276
277 /* check "prefix" from top node */
278 snd_soc_of_parse_node_prefix(top, cconf, codecs->of_node,
279 PREFIX "prefix");
280 snd_soc_of_parse_node_prefix(node, cconf, codecs->of_node,
281 "prefix");
282 snd_soc_of_parse_node_prefix(np, cconf, codecs->of_node,
283 "prefix");
284 }
285
286 simple_parse_convert(dev, np, &dai_props->adata);
287
288 ret = simple_link_init(priv, np, codec, li, prefix, dai_name);
289
290 out_put_node:
291 li->link++;
292
293 return ret;
294 }
295
simple_dai_link_of(struct simple_util_priv * priv,struct device_node * np,struct device_node * codec,struct link_info * li,bool is_top)296 static int simple_dai_link_of(struct simple_util_priv *priv,
297 struct device_node *np,
298 struct device_node *codec,
299 struct link_info *li,
300 bool is_top)
301 {
302 struct device *dev = simple_priv_to_dev(priv);
303 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
304 struct snd_soc_dai_link_component *cpus = snd_soc_link_to_cpu(dai_link, 0);
305 struct snd_soc_dai_link_component *codecs = snd_soc_link_to_codec(dai_link, 0);
306 struct snd_soc_dai_link_component *platforms = snd_soc_link_to_platform(dai_link, 0);
307 struct device_node *cpu = NULL;
308 char dai_name[64];
309 char prop[128];
310 char *prefix = "";
311 int ret, single_cpu = 0;
312
313 cpu = np;
314 struct device_node *node __free(device_node) = of_get_parent(np);
315
316 dev_dbg(dev, "link_of (%pOF)\n", node);
317
318 /* For single DAI link & old style of DT node */
319 if (is_top)
320 prefix = PREFIX;
321
322 snprintf(prop, sizeof(prop), "%splat", prefix);
323 struct device_node *plat __free(device_node) = of_get_child_by_name(node, prop);
324
325 ret = simple_parse_node(priv, cpu, li, prefix, &single_cpu);
326 if (ret < 0)
327 goto dai_link_of_err;
328
329 ret = simple_parse_node(priv, codec, li, prefix, NULL);
330 if (ret < 0)
331 goto dai_link_of_err;
332
333 ret = simple_parse_platform(plat, platforms);
334 if (ret < 0)
335 goto dai_link_of_err;
336
337 snprintf(dai_name, sizeof(dai_name),
338 "%s-%s", cpus->dai_name, codecs->dai_name);
339
340 simple_util_canonicalize_cpu(cpus, single_cpu);
341 simple_util_canonicalize_platform(platforms, cpus);
342
343 ret = simple_link_init(priv, cpu, codec, li, prefix, dai_name);
344
345 dai_link_of_err:
346 li->link++;
347
348 return ret;
349 }
350
__simple_for_each_link(struct simple_util_priv * priv,struct link_info * li,int (* func_noml)(struct simple_util_priv * priv,struct device_node * np,struct device_node * codec,struct link_info * li,bool is_top),int (* func_dpcm)(struct simple_util_priv * priv,struct device_node * np,struct device_node * codec,struct link_info * li,bool is_top))351 static int __simple_for_each_link(struct simple_util_priv *priv,
352 struct link_info *li,
353 int (*func_noml)(struct simple_util_priv *priv,
354 struct device_node *np,
355 struct device_node *codec,
356 struct link_info *li, bool is_top),
357 int (*func_dpcm)(struct simple_util_priv *priv,
358 struct device_node *np,
359 struct device_node *codec,
360 struct link_info *li, bool is_top))
361 {
362 struct device *dev = simple_priv_to_dev(priv);
363 struct device_node *top = dev->of_node;
364 struct device_node *node;
365 uintptr_t dpcm_selectable = (uintptr_t)of_device_get_match_data(dev);
366 bool is_top = 0;
367 int ret = 0;
368
369 /* Check if it has dai-link */
370 node = of_get_child_by_name(top, PREFIX "dai-link");
371 if (!node) {
372 node = of_node_get(top);
373 is_top = 1;
374 }
375
376 struct device_node *add_devs __free(device_node) = of_get_child_by_name(top, PREFIX "additional-devs");
377
378 /* loop for all dai-link */
379 do {
380 struct simple_util_data adata;
381 int num = of_get_child_count(node);
382
383 /* Skip additional-devs node */
384 if (node == add_devs) {
385 node = of_get_next_child(top, node);
386 continue;
387 }
388
389 /* get codec */
390 struct device_node *codec __free(device_node) =
391 of_get_child_by_name(node, is_top ? PREFIX "codec" : "codec");
392 if (!codec) {
393 ret = -ENODEV;
394 goto error;
395 }
396 /* get platform */
397 struct device_node *plat __free(device_node) =
398 of_get_child_by_name(node, is_top ? PREFIX "plat" : "plat");
399
400 /* get convert-xxx property */
401 memset(&adata, 0, sizeof(adata));
402 for_each_child_of_node_scoped(node, np) {
403 if (np == add_devs)
404 continue;
405 simple_parse_convert(dev, np, &adata);
406 }
407
408 /* loop for all CPU/Codec node */
409 for_each_child_of_node_scoped(node, np) {
410 if (plat == np || add_devs == np)
411 continue;
412 /*
413 * It is DPCM
414 * if it has many CPUs,
415 * or has convert-xxx property
416 */
417 if (dpcm_selectable &&
418 (num > 2 || simple_util_is_convert_required(&adata))) {
419 /*
420 * np
421 * |1(CPU)|0(Codec) li->cpu
422 * CPU |Pass |return
423 * Codec |return|Pass
424 */
425 if (li->cpu != (np == codec))
426 ret = func_dpcm(priv, np, codec, li, is_top);
427 /* else normal sound */
428 } else {
429 /*
430 * np
431 * |1(CPU)|0(Codec) li->cpu
432 * CPU |Pass |return
433 * Codec |return|return
434 */
435 if (li->cpu && (np != codec))
436 ret = func_noml(priv, np, codec, li, is_top);
437 }
438
439 if (ret < 0)
440 goto error;
441 }
442
443 node = of_get_next_child(top, node);
444 } while (!is_top && node);
445
446 error:
447 of_node_put(node);
448
449 return ret;
450 }
451
simple_for_each_link(struct simple_util_priv * priv,struct link_info * li,int (* func_noml)(struct simple_util_priv * priv,struct device_node * np,struct device_node * codec,struct link_info * li,bool is_top),int (* func_dpcm)(struct simple_util_priv * priv,struct device_node * np,struct device_node * codec,struct link_info * li,bool is_top))452 static int simple_for_each_link(struct simple_util_priv *priv,
453 struct link_info *li,
454 int (*func_noml)(struct simple_util_priv *priv,
455 struct device_node *np,
456 struct device_node *codec,
457 struct link_info *li, bool is_top),
458 int (*func_dpcm)(struct simple_util_priv *priv,
459 struct device_node *np,
460 struct device_node *codec,
461 struct link_info *li, bool is_top))
462 {
463 int ret;
464 /*
465 * Detect all CPU first, and Detect all Codec 2nd.
466 *
467 * In Normal sound case, all DAIs are detected
468 * as "CPU-Codec".
469 *
470 * In DPCM sound case,
471 * all CPUs are detected as "CPU-dummy", and
472 * all Codecs are detected as "dummy-Codec".
473 * To avoid random sub-device numbering,
474 * detect "dummy-Codec" in last;
475 */
476 for (li->cpu = 1; li->cpu >= 0; li->cpu--) {
477 ret = __simple_for_each_link(priv, li, func_noml, func_dpcm);
478 if (ret < 0)
479 break;
480 }
481
482 return ret;
483 }
484
simple_depopulate_aux(void * data)485 static void simple_depopulate_aux(void *data)
486 {
487 struct simple_util_priv *priv = data;
488
489 of_platform_depopulate(simple_priv_to_dev(priv));
490 }
491
simple_populate_aux(struct simple_util_priv * priv)492 static int simple_populate_aux(struct simple_util_priv *priv)
493 {
494 struct device *dev = simple_priv_to_dev(priv);
495 struct device_node *node __free(device_node) = of_get_child_by_name(dev->of_node, PREFIX "additional-devs");
496 int ret;
497
498 if (!node)
499 return 0;
500
501 ret = of_platform_populate(node, NULL, NULL, dev);
502 if (ret)
503 return ret;
504
505 return devm_add_action_or_reset(dev, simple_depopulate_aux, priv);
506 }
507
simple_parse_of(struct simple_util_priv * priv,struct link_info * li)508 static int simple_parse_of(struct simple_util_priv *priv, struct link_info *li)
509 {
510 struct snd_soc_card *card = simple_priv_to_card(priv);
511 int ret;
512
513 ret = simple_util_parse_widgets(card, PREFIX);
514 if (ret < 0)
515 return ret;
516
517 ret = simple_util_parse_routing(card, PREFIX);
518 if (ret < 0)
519 return ret;
520
521 ret = simple_util_parse_pin_switches(card, PREFIX);
522 if (ret < 0)
523 return ret;
524
525 /* Single/Muti DAI link(s) & New style of DT node */
526 memset(li, 0, sizeof(*li));
527 ret = simple_for_each_link(priv, li,
528 simple_dai_link_of,
529 simple_dai_link_of_dpcm);
530 if (ret < 0)
531 return ret;
532
533 ret = simple_util_parse_card_name(card, PREFIX);
534 if (ret < 0)
535 return ret;
536
537 ret = simple_populate_aux(priv);
538 if (ret < 0)
539 return ret;
540
541 ret = snd_soc_of_parse_aux_devs(card, PREFIX "aux-devs");
542
543 return ret;
544 }
545
simple_count_noml(struct simple_util_priv * priv,struct device_node * np,struct device_node * codec,struct link_info * li,bool is_top)546 static int simple_count_noml(struct simple_util_priv *priv,
547 struct device_node *np,
548 struct device_node *codec,
549 struct link_info *li, bool is_top)
550 {
551 if (li->link >= SNDRV_MAX_LINKS) {
552 struct device *dev = simple_priv_to_dev(priv);
553
554 dev_err(dev, "too many links\n");
555 return -EINVAL;
556 }
557
558 /*
559 * DON'T REMOVE platforms
560 *
561 * Some CPU might be using soc-generic-dmaengine-pcm. This means CPU and Platform
562 * are different Component, but are sharing same component->dev.
563 * Simple Card had been supported it without special Platform selection.
564 * We need platforms here.
565 *
566 * In case of no Platform, it will be Platform == CPU, but Platform will be
567 * ignored by snd_soc_rtd_add_component().
568 *
569 * see
570 * simple-card-utils.c :: simple_util_canonicalize_platform()
571 */
572 li->num[li->link].cpus = 1;
573 li->num[li->link].platforms = 1;
574
575 li->num[li->link].codecs = 1;
576
577 li->link += 1;
578
579 return 0;
580 }
581
simple_count_dpcm(struct simple_util_priv * priv,struct device_node * np,struct device_node * codec,struct link_info * li,bool is_top)582 static int simple_count_dpcm(struct simple_util_priv *priv,
583 struct device_node *np,
584 struct device_node *codec,
585 struct link_info *li, bool is_top)
586 {
587 if (li->link >= SNDRV_MAX_LINKS) {
588 struct device *dev = simple_priv_to_dev(priv);
589
590 dev_err(dev, "too many links\n");
591 return -EINVAL;
592 }
593
594 if (li->cpu) {
595 /*
596 * DON'T REMOVE platforms
597 * see
598 * simple_count_noml()
599 */
600 li->num[li->link].cpus = 1;
601 li->num[li->link].platforms = 1;
602
603 li->link++; /* CPU-dummy */
604 } else {
605 li->num[li->link].codecs = 1;
606
607 li->link++; /* dummy-Codec */
608 }
609
610 return 0;
611 }
612
simple_get_dais_count(struct simple_util_priv * priv,struct link_info * li)613 static int simple_get_dais_count(struct simple_util_priv *priv,
614 struct link_info *li)
615 {
616 struct device *dev = simple_priv_to_dev(priv);
617 struct device_node *top = dev->of_node;
618
619 /*
620 * link_num : number of links.
621 * CPU-Codec / CPU-dummy / dummy-Codec
622 * dais_num : number of DAIs
623 * ccnf_num : number of codec_conf
624 * same number for "dummy-Codec"
625 *
626 * ex1)
627 * CPU0 --- Codec0 link : 5
628 * CPU1 --- Codec1 dais : 7
629 * CPU2 -/ ccnf : 1
630 * CPU3 --- Codec2
631 *
632 * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec
633 * => 7 DAIs = 4xCPU + 3xCodec
634 * => 1 ccnf = 1xdummy-Codec
635 *
636 * ex2)
637 * CPU0 --- Codec0 link : 5
638 * CPU1 --- Codec1 dais : 6
639 * CPU2 -/ ccnf : 1
640 * CPU3 -/
641 *
642 * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec
643 * => 6 DAIs = 4xCPU + 2xCodec
644 * => 1 ccnf = 1xdummy-Codec
645 *
646 * ex3)
647 * CPU0 --- Codec0 link : 6
648 * CPU1 -/ dais : 6
649 * CPU2 --- Codec1 ccnf : 2
650 * CPU3 -/
651 *
652 * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec
653 * => 6 DAIs = 4xCPU + 2xCodec
654 * => 2 ccnf = 2xdummy-Codec
655 *
656 * ex4)
657 * CPU0 --- Codec0 (convert-rate) link : 3
658 * CPU1 --- Codec1 dais : 4
659 * ccnf : 1
660 *
661 * => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec
662 * => 4 DAIs = 2xCPU + 2xCodec
663 * => 1 ccnf = 1xdummy-Codec
664 */
665 if (!top) {
666 li->num[0].cpus = 1;
667 li->num[0].codecs = 1;
668 li->num[0].platforms = 1;
669
670 li->link = 1;
671 return 0;
672 }
673
674 return simple_for_each_link(priv, li,
675 simple_count_noml,
676 simple_count_dpcm);
677 }
678
simple_soc_probe(struct snd_soc_card * card)679 static int simple_soc_probe(struct snd_soc_card *card)
680 {
681 struct simple_util_priv *priv = snd_soc_card_get_drvdata(card);
682 int ret;
683
684 ret = simple_util_init_hp(card, &priv->hp_jack, PREFIX);
685 if (ret < 0)
686 return ret;
687
688 ret = simple_util_init_mic(card, &priv->mic_jack, PREFIX);
689 if (ret < 0)
690 return ret;
691
692 ret = simple_util_init_aux_jacks(priv, PREFIX);
693 if (ret < 0)
694 return ret;
695
696 return 0;
697 }
698
simple_probe(struct platform_device * pdev)699 static int simple_probe(struct platform_device *pdev)
700 {
701 struct simple_util_priv *priv;
702 struct device *dev = &pdev->dev;
703 struct device_node *np = dev->of_node;
704 struct snd_soc_card *card;
705 int ret;
706
707 /* Allocate the private data and the DAI link array */
708 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
709 if (!priv)
710 return -ENOMEM;
711
712 card = simple_priv_to_card(priv);
713 card->owner = THIS_MODULE;
714 card->dev = dev;
715 card->probe = simple_soc_probe;
716 card->driver_name = "simple-card";
717
718 struct link_info *li __free(kfree) = kzalloc(sizeof(*li), GFP_KERNEL);
719 if (!li)
720 return -ENOMEM;
721
722 ret = simple_get_dais_count(priv, li);
723 if (ret < 0)
724 return ret;
725
726 if (!li->link)
727 return -EINVAL;
728
729 ret = simple_util_init_priv(priv, li);
730 if (ret < 0)
731 return ret;
732
733 if (np && of_device_is_available(np)) {
734
735 ret = simple_parse_of(priv, li);
736 if (ret < 0) {
737 dev_err_probe(dev, ret, "parse error\n");
738 goto err;
739 }
740
741 } else {
742 struct simple_util_info *cinfo;
743 struct snd_soc_dai_link_component *cpus;
744 struct snd_soc_dai_link_component *codecs;
745 struct snd_soc_dai_link_component *platform;
746 struct snd_soc_dai_link *dai_link = priv->dai_link;
747 struct simple_dai_props *dai_props = priv->dai_props;
748
749 ret = -EINVAL;
750
751 cinfo = dev->platform_data;
752 if (!cinfo) {
753 dev_err(dev, "no info for asoc-simple-card\n");
754 goto err;
755 }
756
757 if (!cinfo->name ||
758 !cinfo->codec_dai.name ||
759 !cinfo->codec ||
760 !cinfo->platform ||
761 !cinfo->cpu_dai.name) {
762 dev_err(dev, "insufficient simple_util_info settings\n");
763 goto err;
764 }
765
766 cpus = dai_link->cpus;
767 cpus->dai_name = cinfo->cpu_dai.name;
768
769 codecs = dai_link->codecs;
770 codecs->name = cinfo->codec;
771 codecs->dai_name = cinfo->codec_dai.name;
772
773 platform = dai_link->platforms;
774 platform->name = cinfo->platform;
775
776 card->name = (cinfo->card) ? cinfo->card : cinfo->name;
777 dai_link->name = cinfo->name;
778 dai_link->stream_name = cinfo->name;
779 dai_link->dai_fmt = cinfo->daifmt;
780 dai_link->init = simple_util_dai_init;
781 memcpy(dai_props->cpu_dai, &cinfo->cpu_dai,
782 sizeof(*dai_props->cpu_dai));
783 memcpy(dai_props->codec_dai, &cinfo->codec_dai,
784 sizeof(*dai_props->codec_dai));
785 }
786
787 snd_soc_card_set_drvdata(card, priv);
788
789 simple_util_debug_info(priv);
790
791 ret = devm_snd_soc_register_card(dev, card);
792 if (ret < 0)
793 goto err;
794
795 return 0;
796 err:
797 simple_util_clean_reference(card);
798
799 return ret;
800 }
801
802 static const struct of_device_id simple_of_match[] = {
803 { .compatible = "simple-audio-card", },
804 { .compatible = "simple-scu-audio-card",
805 .data = (void *)DPCM_SELECTABLE },
806 {},
807 };
808 MODULE_DEVICE_TABLE(of, simple_of_match);
809
810 static struct platform_driver simple_card = {
811 .driver = {
812 .name = "asoc-simple-card",
813 .pm = &snd_soc_pm_ops,
814 .of_match_table = simple_of_match,
815 },
816 .probe = simple_probe,
817 .remove = simple_util_remove,
818 };
819
820 module_platform_driver(simple_card);
821
822 MODULE_ALIAS("platform:asoc-simple-card");
823 MODULE_LICENSE("GPL v2");
824 MODULE_DESCRIPTION("ASoC Simple Sound Card");
825 MODULE_AUTHOR("Kuninori Morimoto <[email protected]>");
826