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