1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // ASoC Audio Graph Card2 support
4 //
5 // Copyright (C) 2020 Renesas Electronics Corp.
6 // Copyright (C) 2020 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 //
8 // based on ${LINUX}/sound/soc/generic/audio-graph-card.c
9 #include <linux/clk.h>
10 #include <linux/device.h>
11 #include <linux/gpio.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/of_device.h>
16 #include <linux/of_gpio.h>
17 #include <linux/of_graph.h>
18 #include <linux/platform_device.h>
19 #include <linux/string.h>
20 #include <sound/graph_card.h>
21
22 /************************************
23 daifmt
24 ************************************
25 ports {
26 format = "left_j";
27 port@0 {
28 bitclock-master;
29 sample0: endpoint@0 {
30 frame-master;
31 };
32 sample1: endpoint@1 {
33 format = "i2s";
34 };
35 };
36 ...
37 };
38
39 You can set daifmt at ports/port/endpoint.
40 It uses *latest* format, and *share* master settings.
41 In above case,
42 sample0: left_j, bitclock-master, frame-master
43 sample1: i2s, bitclock-master
44
45 If there was no settings, *Codec* will be
46 bitclock/frame provider as default.
47 see
48 graph_parse_daifmt().
49
50 ************************************
51 Normal Audio-Graph
52 ************************************
53
54 CPU <---> Codec
55
56 sound {
57 compatible = "audio-graph-card2";
58 links = <&cpu>;
59 };
60
61 CPU {
62 cpu: port {
63 bitclock-master;
64 frame-master;
65 cpu_ep: endpoint { remote-endpoint = <&codec_ep>; }; };
66 };
67
68 Codec {
69 port { codec_ep: endpoint { remote-endpoint = <&cpu_ep>; }; };
70 };
71
72 ************************************
73 Multi-CPU/Codec
74 ************************************
75
76 It has connection part (= X) and list part (= y).
77 links indicates connection part of CPU side (= A).
78
79 +-+ (A) +-+
80 CPU1 --(y) | | <-(X)--(X)-> | | (y)-- Codec1
81 CPU2 --(y) | | | | (y)-- Codec2
82 +-+ +-+
83
84 sound {
85 compatible = "audio-graph-card2";
86
87 (A) links = <&mcpu>;
88
89 multi {
90 ports@0 {
91 (X) (A) mcpu: port@0 { mcpu0_ep: endpoint { remote-endpoint = <&mcodec0_ep>; }; };
92 (y) port@1 { mcpu1_ep: endpoint { remote-endpoint = <&cpu1_ep>; }; };
93 (y) port@2 { mcpu2_ep: endpoint { remote-endpoint = <&cpu2_ep>; }; };
94 };
95 ports@1 {
96 (X) port@0 { mcodec0_ep: endpoint { remote-endpoint = <&mcpu0_ep>; }; };
97 (y) port@1 { mcodec1_ep: endpoint { remote-endpoint = <&codec1_ep>; }; };
98 (y) port@2 { mcodec2_ep: endpoint { remote-endpoint = <&codec2_ep>; }; };
99 };
100 };
101 };
102
103 CPU {
104 ports {
105 bitclock-master;
106 frame-master;
107 port@0 { cpu1_ep: endpoint { remote-endpoint = <&mcpu1_ep>; }; };
108 port@1 { cpu2_ep: endpoint { remote-endpoint = <&mcpu2_ep>; }; };
109 };
110 };
111
112 Codec {
113 ports {
114 port@0 { codec1_ep: endpoint { remote-endpoint = <&mcodec1_ep>; }; };
115 port@1 { codec2_ep: endpoint { remote-endpoint = <&mcodec2_ep>; }; };
116 };
117 };
118
119 ************************************
120 DPCM
121 ************************************
122
123 DSP
124 ************
125 PCM0 <--> * fe0 be0 * <--> DAI0: Codec Headset
126 PCM1 <--> * fe1 be1 * <--> DAI1: Codec Speakers
127 PCM2 <--> * fe2 be2 * <--> DAI2: MODEM
128 PCM3 <--> * fe3 be3 * <--> DAI3: BT
129 * be4 * <--> DAI4: DMIC
130 * be5 * <--> DAI5: FM
131 ************
132
133 sound {
134 compatible = "audio-graph-card2";
135
136 // indicate routing
137 routing = "xxx Playback", "xxx Playback",
138 "xxx Playback", "xxx Playback",
139 "xxx Playback", "xxx Playback";
140
141 // indicate all Front-End, Back-End
142 links = <&fe0, &fe1, ...,
143 &be0, &be1, ...>;
144
145 dpcm {
146 // Front-End
147 ports@0 {
148 fe0: port@0 { fe0_ep: endpoint { remote-endpoint = <&pcm0_ep>; }; };
149 fe1: port@1 { fe1_ep: endpoint { remote-endpoint = <&pcm1_ep>; }; };
150 ...
151 };
152 // Back-End
153 ports@1 {
154 be0: port@0 { be0_ep: endpoint { remote-endpoint = <&dai0_ep>; }; };
155 be1: port@1 { be1_ep: endpoint { remote-endpoint = <&dai1_ep>; }; };
156 ...
157 };
158 };
159 };
160
161 CPU {
162 ports {
163 bitclock-master;
164 frame-master;
165 port@0 { pcm0_ep: endpoint { remote-endpoint = <&fe0_ep>; }; };
166 port@1 { pcm1_ep: endpoint { remote-endpoint = <&fe1_ep>; }; };
167 ...
168 };
169 };
170
171 Codec {
172 ports {
173 port@0 { dai0_ep: endpoint { remote-endpoint = <&be0_ep>; }; };
174 port@1 { dai1_ep: endpoint { remote-endpoint = <&be1_ep>; }; };
175 ...
176 };
177 };
178
179 ************************************
180 Codec to Codec
181 ************************************
182
183 +--+
184 | |<-- Codec0 <- IN
185 | |--> Codec1 -> OUT
186 +--+
187
188 sound {
189 compatible = "audio-graph-card2";
190
191 routing = "OUT" ,"DAI1 Playback",
192 "DAI0 Capture", "IN";
193
194 links = <&c2c>;
195
196 codec2codec {
197 ports {
198 rate = <48000>;
199 c2c: port@0 { c2cf_ep: endpoint { remote-endpoint = <&codec0_ep>; }; };
200 port@1 { c2cb_ep: endpoint { remote-endpoint = <&codec1_ep>; }; };
201 };
202 };
203
204 Codec {
205 ports {
206 port@0 {
207 bitclock-master;
208 frame-master;
209 codec0_ep: endpoint { remote-endpoint = <&c2cf_ep>; }; };
210 port@1 { codec1_ep: endpoint { remote-endpoint = <&c2cb_ep>; }; };
211 };
212 };
213
214 */
215
216 enum graph_type {
217 GRAPH_NORMAL,
218 GRAPH_DPCM,
219 GRAPH_C2C,
220
221 GRAPH_MULTI, /* don't use ! Use this only in __graph_get_type() */
222 };
223
224 #define GRAPH_NODENAME_MULTI "multi"
225 #define GRAPH_NODENAME_DPCM "dpcm"
226 #define GRAPH_NODENAME_C2C "codec2codec"
227
228 #define port_to_endpoint(port) of_get_child_by_name(port, "endpoint")
229
__graph_get_type(struct device_node * lnk)230 static enum graph_type __graph_get_type(struct device_node *lnk)
231 {
232 struct device_node *np, *parent_np;
233 enum graph_type ret;
234
235 /*
236 * target {
237 * ports {
238 * => lnk: port@0 { ... };
239 * port@1 { ... };
240 * };
241 * };
242 */
243 np = of_get_parent(lnk);
244 if (of_node_name_eq(np, "ports")) {
245 parent_np = of_get_parent(np);
246 of_node_put(np);
247 np = parent_np;
248 }
249
250 if (of_node_name_eq(np, GRAPH_NODENAME_MULTI)) {
251 ret = GRAPH_MULTI;
252 goto out_put;
253 }
254
255 if (of_node_name_eq(np, GRAPH_NODENAME_DPCM)) {
256 ret = GRAPH_DPCM;
257 goto out_put;
258 }
259
260 if (of_node_name_eq(np, GRAPH_NODENAME_C2C)) {
261 ret = GRAPH_C2C;
262 goto out_put;
263 }
264
265 ret = GRAPH_NORMAL;
266
267 out_put:
268 of_node_put(np);
269 return ret;
270
271 }
272
graph_get_type(struct asoc_simple_priv * priv,struct device_node * lnk)273 static enum graph_type graph_get_type(struct asoc_simple_priv *priv,
274 struct device_node *lnk)
275 {
276 enum graph_type type = __graph_get_type(lnk);
277
278 /* GRAPH_MULTI here means GRAPH_NORMAL */
279 if (type == GRAPH_MULTI)
280 type = GRAPH_NORMAL;
281
282 #ifdef DEBUG
283 {
284 struct device *dev = simple_priv_to_dev(priv);
285 const char *str = "Normal";
286
287 switch (type) {
288 case GRAPH_DPCM:
289 if (asoc_graph_is_ports0(lnk))
290 str = "DPCM Front-End";
291 else
292 str = "DPCM Back-End";
293 break;
294 case GRAPH_C2C:
295 str = "Codec2Codec";
296 break;
297 default:
298 break;
299 }
300
301 dev_dbg(dev, "%pOF (%s)", lnk, str);
302 }
303 #endif
304 return type;
305 }
306
graph_lnk_is_multi(struct device_node * lnk)307 static int graph_lnk_is_multi(struct device_node *lnk)
308 {
309 return __graph_get_type(lnk) == GRAPH_MULTI;
310 }
311
graph_get_next_multi_ep(struct device_node ** port)312 static struct device_node *graph_get_next_multi_ep(struct device_node **port)
313 {
314 struct device_node *ports = of_get_parent(*port);
315 struct device_node *ep = NULL;
316 struct device_node *rep = NULL;
317
318 /*
319 * multi {
320 * ports {
321 * => lnk: port@0 { ... };
322 * port@1 { ep { ... = rep0 } };
323 * port@2 { ep { ... = rep1 } };
324 * ...
325 * };
326 * };
327 *
328 * xxx {
329 * port@0 { rep0 };
330 * port@1 { rep1 };
331 * };
332 */
333 do {
334 *port = of_get_next_child(ports, *port);
335 if (!*port)
336 break;
337 } while (!of_node_name_eq(*port, "port"));
338
339 if (*port) {
340 ep = port_to_endpoint(*port);
341 rep = of_graph_get_remote_endpoint(ep);
342 }
343
344 of_node_put(ep);
345 of_node_put(ports);
346
347 return rep;
348 }
349
350 static const struct snd_soc_ops graph_ops = {
351 .startup = asoc_simple_startup,
352 .shutdown = asoc_simple_shutdown,
353 .hw_params = asoc_simple_hw_params,
354 };
355
graph_get_dai_id(struct device_node * ep)356 static int graph_get_dai_id(struct device_node *ep)
357 {
358 struct device_node *node;
359 struct device_node *endpoint;
360 struct of_endpoint info;
361 int i, id;
362 const u32 *reg;
363 int ret;
364
365 /* use driver specified DAI ID if exist */
366 ret = snd_soc_get_dai_id(ep);
367 if (ret != -ENOTSUPP)
368 return ret;
369
370 /* use endpoint/port reg if exist */
371 ret = of_graph_parse_endpoint(ep, &info);
372 if (ret == 0) {
373 /*
374 * Because it will count port/endpoint if it doesn't have "reg".
375 * But, we can't judge whether it has "no reg", or "reg = <0>"
376 * only of_graph_parse_endpoint().
377 * We need to check "reg" property
378 */
379 if (of_get_property(ep, "reg", NULL))
380 return info.id;
381
382 node = of_get_parent(ep);
383 reg = of_get_property(node, "reg", NULL);
384 of_node_put(node);
385 if (reg)
386 return info.port;
387 }
388 node = of_graph_get_port_parent(ep);
389
390 /*
391 * Non HDMI sound case, counting port/endpoint on its DT
392 * is enough. Let's count it.
393 */
394 i = 0;
395 id = -1;
396 for_each_endpoint_of_node(node, endpoint) {
397 if (endpoint == ep)
398 id = i;
399 i++;
400 }
401
402 of_node_put(node);
403
404 if (id < 0)
405 return -ENODEV;
406
407 return id;
408 }
409
asoc_simple_parse_dai(struct device_node * ep,struct snd_soc_dai_link_component * dlc,int * is_single_link)410 static int asoc_simple_parse_dai(struct device_node *ep,
411 struct snd_soc_dai_link_component *dlc,
412 int *is_single_link)
413 {
414 struct device_node *node;
415 struct of_phandle_args args;
416 int ret;
417
418 if (!ep)
419 return 0;
420
421 node = of_graph_get_port_parent(ep);
422
423 /* Get dai->name */
424 args.np = node;
425 args.args[0] = graph_get_dai_id(ep);
426 args.args_count = (of_graph_get_endpoint_count(node) > 1);
427
428 /*
429 * FIXME
430 *
431 * Here, dlc->dai_name is pointer to CPU/Codec DAI name.
432 * If user unbinded CPU or Codec driver, but not for Sound Card,
433 * dlc->dai_name is keeping unbinded CPU or Codec
434 * driver's pointer.
435 *
436 * If user re-bind CPU or Codec driver again, ALSA SoC will try
437 * to rebind Card via snd_soc_try_rebind_card(), but because of
438 * above reason, it might can't bind Sound Card.
439 * Because Sound Card is pointing to released dai_name pointer.
440 *
441 * To avoid this rebind Card issue,
442 * 1) It needs to alloc memory to keep dai_name eventhough
443 * CPU or Codec driver was unbinded, or
444 * 2) user need to rebind Sound Card everytime
445 * if he unbinded CPU or Codec.
446 */
447 ret = snd_soc_get_dai_name(&args, &dlc->dai_name);
448 if (ret < 0) {
449 of_node_put(node);
450 return ret;
451 }
452
453 dlc->of_node = node;
454
455 if (is_single_link)
456 *is_single_link = of_graph_get_endpoint_count(node) == 1;
457
458 return 0;
459 }
460
graph_parse_convert(struct device_node * ep,struct simple_dai_props * props)461 static void graph_parse_convert(struct device_node *ep,
462 struct simple_dai_props *props)
463 {
464 struct device_node *port = of_get_parent(ep);
465 struct device_node *ports = of_get_parent(port);
466 struct asoc_simple_data *adata = &props->adata;
467
468 if (of_node_name_eq(ports, "ports"))
469 asoc_simple_parse_convert(ports, NULL, adata);
470 asoc_simple_parse_convert(port, NULL, adata);
471 asoc_simple_parse_convert(ep, NULL, adata);
472
473 of_node_put(port);
474 of_node_put(ports);
475 }
476
graph_parse_mclk_fs(struct device_node * ep,struct simple_dai_props * props)477 static void graph_parse_mclk_fs(struct device_node *ep,
478 struct simple_dai_props *props)
479 {
480 struct device_node *port = of_get_parent(ep);
481 struct device_node *ports = of_get_parent(port);
482
483 if (of_node_name_eq(ports, "ports"))
484 of_property_read_u32(ports, "mclk-fs", &props->mclk_fs);
485 of_property_read_u32(port, "mclk-fs", &props->mclk_fs);
486 of_property_read_u32(ep, "mclk-fs", &props->mclk_fs);
487
488 of_node_put(port);
489 of_node_put(ports);
490 }
491
__graph_parse_node(struct asoc_simple_priv * priv,enum graph_type gtype,struct device_node * ep,struct link_info * li,int is_cpu,int idx)492 static int __graph_parse_node(struct asoc_simple_priv *priv,
493 enum graph_type gtype,
494 struct device_node *ep,
495 struct link_info *li,
496 int is_cpu, int idx)
497 {
498 struct device *dev = simple_priv_to_dev(priv);
499 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
500 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
501 struct snd_soc_dai_link_component *dlc;
502 struct asoc_simple_dai *dai;
503 int ret, is_single_links = 0;
504
505 if (is_cpu) {
506 dlc = asoc_link_to_cpu(dai_link, idx);
507 dai = simple_props_to_dai_cpu(dai_props, idx);
508 } else {
509 dlc = asoc_link_to_codec(dai_link, idx);
510 dai = simple_props_to_dai_codec(dai_props, idx);
511 }
512
513 graph_parse_mclk_fs(ep, dai_props);
514
515 ret = asoc_simple_parse_dai(ep, dlc, &is_single_links);
516 if (ret < 0)
517 return ret;
518
519 ret = asoc_simple_parse_tdm(ep, dai);
520 if (ret < 0)
521 return ret;
522
523 ret = asoc_simple_parse_tdm_width_map(dev, ep, dai);
524 if (ret < 0)
525 return ret;
526
527 ret = asoc_simple_parse_clk(dev, ep, dai, dlc);
528 if (ret < 0)
529 return ret;
530
531 /*
532 * set DAI Name
533 */
534 if (!dai_link->name) {
535 struct snd_soc_dai_link_component *cpus = dlc;
536 struct snd_soc_dai_link_component *codecs = asoc_link_to_codec(dai_link, idx);
537 char *cpu_multi = "";
538 char *codec_multi = "";
539
540 if (dai_link->num_cpus > 1)
541 cpu_multi = "_multi";
542 if (dai_link->num_codecs > 1)
543 codec_multi = "_multi";
544
545 switch (gtype) {
546 case GRAPH_NORMAL:
547 /* run is_cpu only. see audio_graph2_link_normal() */
548 if (is_cpu)
549 asoc_simple_set_dailink_name(dev, dai_link, "%s%s-%s%s",
550 cpus->dai_name, cpu_multi,
551 codecs->dai_name, codec_multi);
552 break;
553 case GRAPH_DPCM:
554 if (is_cpu)
555 asoc_simple_set_dailink_name(dev, dai_link, "fe.%pOFP.%s%s",
556 cpus->of_node, cpus->dai_name, cpu_multi);
557 else
558 asoc_simple_set_dailink_name(dev, dai_link, "be.%pOFP.%s%s",
559 codecs->of_node, codecs->dai_name, codec_multi);
560 break;
561 case GRAPH_C2C:
562 /* run is_cpu only. see audio_graph2_link_c2c() */
563 if (is_cpu)
564 asoc_simple_set_dailink_name(dev, dai_link, "c2c.%s%s-%s%s",
565 cpus->dai_name, cpu_multi,
566 codecs->dai_name, codec_multi);
567 break;
568 default:
569 break;
570 }
571 }
572
573 /*
574 * Check "prefix" from top node
575 * if DPCM-BE case
576 */
577 if (!is_cpu && gtype == GRAPH_DPCM) {
578 struct snd_soc_dai_link_component *codecs = asoc_link_to_codec(dai_link, idx);
579 struct snd_soc_codec_conf *cconf = simple_props_to_codec_conf(dai_props, idx);
580 struct device_node *rport = of_get_parent(ep);
581 struct device_node *rports = of_get_parent(rport);
582
583 if (of_node_name_eq(rports, "ports"))
584 snd_soc_of_parse_node_prefix(rports, cconf, codecs->of_node, "prefix");
585 snd_soc_of_parse_node_prefix(rport, cconf, codecs->of_node, "prefix");
586
587 of_node_put(rport);
588 of_node_put(rports);
589 }
590
591 if (is_cpu) {
592 struct snd_soc_dai_link_component *cpus = dlc;
593 struct snd_soc_dai_link_component *platforms = asoc_link_to_platform(dai_link, idx);
594
595 asoc_simple_canonicalize_cpu(cpus, is_single_links);
596 asoc_simple_canonicalize_platform(platforms, cpus);
597 }
598
599 return 0;
600 }
601
graph_parse_node(struct asoc_simple_priv * priv,enum graph_type gtype,struct device_node * port,struct link_info * li,int is_cpu)602 static int graph_parse_node(struct asoc_simple_priv *priv,
603 enum graph_type gtype,
604 struct device_node *port,
605 struct link_info *li, int is_cpu)
606 {
607 struct device_node *ep;
608 int ret = 0;
609
610 if (graph_lnk_is_multi(port)) {
611 int idx;
612
613 of_node_get(port);
614
615 for (idx = 0;; idx++) {
616 ep = graph_get_next_multi_ep(&port);
617 if (!ep)
618 break;
619
620 ret = __graph_parse_node(priv, gtype, ep,
621 li, is_cpu, idx);
622 of_node_put(ep);
623 if (ret < 0)
624 break;
625 }
626 } else {
627 /* Single CPU / Codec */
628 ep = port_to_endpoint(port);
629 ret = __graph_parse_node(priv, gtype, ep, li, is_cpu, 0);
630 of_node_put(ep);
631 }
632
633 return ret;
634 }
635
graph_parse_daifmt(struct device_node * node,unsigned int * daifmt,unsigned int * bit_frame)636 static void graph_parse_daifmt(struct device_node *node,
637 unsigned int *daifmt, unsigned int *bit_frame)
638 {
639 unsigned int fmt;
640
641 /*
642 * see also above "daifmt" explanation
643 * and samples.
644 */
645
646 /*
647 * ports {
648 * (A)
649 * port {
650 * (B)
651 * endpoint {
652 * (C)
653 * };
654 * };
655 * };
656 * };
657 */
658
659 /*
660 * clock_provider:
661 *
662 * It can be judged it is provider
663 * if (A) or (B) or (C) has bitclock-master / frame-master flag.
664 *
665 * use "or"
666 */
667 *bit_frame |= snd_soc_daifmt_parse_clock_provider_as_bitmap(node, NULL);
668
669 #define update_daifmt(name) \
670 if (!(*daifmt & SND_SOC_DAIFMT_##name##_MASK) && \
671 (fmt & SND_SOC_DAIFMT_##name##_MASK)) \
672 *daifmt |= fmt & SND_SOC_DAIFMT_##name##_MASK
673
674 /*
675 * format
676 *
677 * This function is called by (C) -> (B) -> (A) order.
678 * Set if applicable part was not yet set.
679 */
680 fmt = snd_soc_daifmt_parse_format(node, NULL);
681 update_daifmt(FORMAT);
682 update_daifmt(CLOCK);
683 update_daifmt(INV);
684 }
685
graph_link_init(struct asoc_simple_priv * priv,struct device_node * port,struct link_info * li,int is_cpu_node)686 static void graph_link_init(struct asoc_simple_priv *priv,
687 struct device_node *port,
688 struct link_info *li,
689 int is_cpu_node)
690 {
691 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
692 struct device_node *ep;
693 struct device_node *ports;
694 unsigned int daifmt = 0, daiclk = 0;
695 unsigned int bit_frame = 0;
696
697 if (graph_lnk_is_multi(port)) {
698 of_node_get(port);
699 ep = graph_get_next_multi_ep(&port);
700 port = of_get_parent(ep);
701 } else {
702 ep = port_to_endpoint(port);
703 }
704
705 ports = of_get_parent(port);
706
707 /*
708 * ports {
709 * (A)
710 * port {
711 * (B)
712 * endpoint {
713 * (C)
714 * };
715 * };
716 * };
717 * };
718 */
719 graph_parse_daifmt(ep, &daifmt, &bit_frame); /* (C) */
720 graph_parse_daifmt(port, &daifmt, &bit_frame); /* (B) */
721 if (of_node_name_eq(ports, "ports"))
722 graph_parse_daifmt(ports, &daifmt, &bit_frame); /* (A) */
723
724 /*
725 * convert bit_frame
726 * We need to flip clock_provider if it was CPU node,
727 * because it is Codec base.
728 */
729 daiclk = snd_soc_daifmt_clock_provider_from_bitmap(bit_frame);
730 if (is_cpu_node)
731 daiclk = snd_soc_daifmt_clock_provider_flipped(daiclk);
732
733 dai_link->dai_fmt = daifmt | daiclk;
734 dai_link->init = asoc_simple_dai_init;
735 dai_link->ops = &graph_ops;
736 if (priv->ops)
737 dai_link->ops = priv->ops;
738 }
739
audio_graph2_link_normal(struct asoc_simple_priv * priv,struct device_node * lnk,struct link_info * li)740 int audio_graph2_link_normal(struct asoc_simple_priv *priv,
741 struct device_node *lnk,
742 struct link_info *li)
743 {
744 struct device_node *cpu_port = lnk;
745 struct device_node *cpu_ep = port_to_endpoint(cpu_port);
746 struct device_node *codec_port = of_graph_get_remote_port(cpu_ep);
747 int ret;
748
749 /*
750 * call Codec first.
751 * see
752 * __graph_parse_node() :: DAI Naming
753 */
754 ret = graph_parse_node(priv, GRAPH_NORMAL, codec_port, li, 0);
755 if (ret < 0)
756 goto err;
757
758 /*
759 * call CPU, and set DAI Name
760 */
761 ret = graph_parse_node(priv, GRAPH_NORMAL, cpu_port, li, 1);
762 if (ret < 0)
763 goto err;
764
765 graph_link_init(priv, cpu_port, li, 1);
766 err:
767 of_node_put(codec_port);
768 of_node_put(cpu_ep);
769
770 return ret;
771 }
772 EXPORT_SYMBOL_GPL(audio_graph2_link_normal);
773
audio_graph2_link_dpcm(struct asoc_simple_priv * priv,struct device_node * lnk,struct link_info * li)774 int audio_graph2_link_dpcm(struct asoc_simple_priv *priv,
775 struct device_node *lnk,
776 struct link_info *li)
777 {
778 struct device_node *ep = port_to_endpoint(lnk);
779 struct device_node *rep = of_graph_get_remote_endpoint(ep);
780 struct device_node *rport = of_graph_get_remote_port(ep);
781 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
782 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
783 int is_cpu = asoc_graph_is_ports0(lnk);
784 int ret;
785
786 if (is_cpu) {
787 /*
788 * dpcm {
789 * // Front-End
790 * ports@0 {
791 * => lnk: port@0 { ep: { ... = rep }; };
792 * ...
793 * };
794 * // Back-End
795 * ports@0 {
796 * ...
797 * };
798 * };
799 *
800 * CPU {
801 * rports: ports {
802 * rport: port@0 { rep: { ... = ep } };
803 * }
804 * }
805 */
806 /*
807 * setup CPU here, Codec is already set as dummy.
808 * see
809 * asoc_simple_init_priv()
810 */
811 dai_link->dynamic = 1;
812 dai_link->dpcm_merged_format = 1;
813
814 ret = graph_parse_node(priv, GRAPH_DPCM, rport, li, 1);
815 if (ret)
816 goto err;
817 } else {
818 /*
819 * dpcm {
820 * // Front-End
821 * ports@0 {
822 * ...
823 * };
824 * // Back-End
825 * ports@0 {
826 * => lnk: port@0 { ep: { ... = rep; }; };
827 * ...
828 * };
829 * };
830 *
831 * Codec {
832 * rports: ports {
833 * rport: port@0 { rep: { ... = ep; }; };
834 * }
835 * }
836 */
837 /*
838 * setup Codec here, CPU is already set as dummy.
839 * see
840 * asoc_simple_init_priv()
841 */
842
843 /* BE settings */
844 dai_link->no_pcm = 1;
845 dai_link->be_hw_params_fixup = asoc_simple_be_hw_params_fixup;
846
847 ret = graph_parse_node(priv, GRAPH_DPCM, rport, li, 0);
848 if (ret < 0)
849 goto err;
850 }
851
852 graph_parse_convert(rep, dai_props);
853
854 snd_soc_dai_link_set_capabilities(dai_link);
855
856 graph_link_init(priv, rport, li, is_cpu);
857 err:
858 of_node_put(ep);
859 of_node_put(rep);
860 of_node_put(rport);
861
862 return ret;
863 }
864 EXPORT_SYMBOL_GPL(audio_graph2_link_dpcm);
865
audio_graph2_link_c2c(struct asoc_simple_priv * priv,struct device_node * lnk,struct link_info * li)866 int audio_graph2_link_c2c(struct asoc_simple_priv *priv,
867 struct device_node *lnk,
868 struct link_info *li)
869 {
870 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
871 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
872 struct snd_soc_pcm_stream *c2c_conf = dai_props->c2c_conf;
873 struct device_node *port0, *port1, *ports;
874 struct device_node *codec0_port, *codec1_port;
875 struct device_node *ep0, *ep1;
876 u32 val = 0;
877 int ret = -EINVAL;
878
879 /*
880 * codec2codec {
881 * ports {
882 * rate = <48000>;
883 * => lnk: port@0 { c2c0_ep: { ... = codec0_ep; }; };
884 * port@1 { c2c1_ep: { ... = codec1_ep; }; };
885 * };
886 * };
887 *
888 * Codec {
889 * ports {
890 * port@0 { codec0_ep: ... }; };
891 * port@1 { codec1_ep: ... }; };
892 * };
893 * };
894 */
895 of_node_get(lnk);
896 port0 = lnk;
897 ports = of_get_parent(port0);
898 port1 = of_get_next_child(ports, lnk);
899
900 of_property_read_u32(ports, "rate", &val);
901 if (!val) {
902 struct device *dev = simple_priv_to_dev(priv);
903
904 dev_err(dev, "Codec2Codec needs rate settings\n");
905 goto err1;
906 }
907
908 c2c_conf->formats = SNDRV_PCM_FMTBIT_S32_LE; /* update ME */
909 c2c_conf->rate_min =
910 c2c_conf->rate_max = val;
911 c2c_conf->channels_min =
912 c2c_conf->channels_max = 2; /* update ME */
913 dai_link->params = c2c_conf;
914
915 ep0 = port_to_endpoint(port0);
916 ep1 = port_to_endpoint(port1);
917
918 codec0_port = of_graph_get_remote_port(ep0);
919 codec1_port = of_graph_get_remote_port(ep1);
920
921 /*
922 * call Codec first.
923 * see
924 * __graph_parse_node() :: DAI Naming
925 */
926 ret = graph_parse_node(priv, GRAPH_C2C, codec1_port, li, 0);
927 if (ret < 0)
928 goto err2;
929
930 /*
931 * call CPU, and set DAI Name
932 */
933 ret = graph_parse_node(priv, GRAPH_C2C, codec0_port, li, 1);
934 if (ret < 0)
935 goto err2;
936
937 graph_link_init(priv, codec0_port, li, 1);
938 err2:
939 of_node_put(ep0);
940 of_node_put(ep1);
941 of_node_put(codec0_port);
942 of_node_put(codec1_port);
943 err1:
944 of_node_put(ports);
945 of_node_put(port0);
946 of_node_put(port1);
947
948 return ret;
949 }
950 EXPORT_SYMBOL_GPL(audio_graph2_link_c2c);
951
graph_link(struct asoc_simple_priv * priv,struct graph2_custom_hooks * hooks,enum graph_type gtype,struct device_node * lnk,struct link_info * li)952 static int graph_link(struct asoc_simple_priv *priv,
953 struct graph2_custom_hooks *hooks,
954 enum graph_type gtype,
955 struct device_node *lnk,
956 struct link_info *li)
957 {
958 struct device *dev = simple_priv_to_dev(priv);
959 GRAPH2_CUSTOM func = NULL;
960 int ret = -EINVAL;
961
962 switch (gtype) {
963 case GRAPH_NORMAL:
964 if (hooks && hooks->custom_normal)
965 func = hooks->custom_normal;
966 else
967 func = audio_graph2_link_normal;
968 break;
969 case GRAPH_DPCM:
970 if (hooks && hooks->custom_dpcm)
971 func = hooks->custom_dpcm;
972 else
973 func = audio_graph2_link_dpcm;
974 break;
975 case GRAPH_C2C:
976 if (hooks && hooks->custom_c2c)
977 func = hooks->custom_c2c;
978 else
979 func = audio_graph2_link_c2c;
980 break;
981 default:
982 break;
983 }
984
985 if (!func) {
986 dev_err(dev, "non supported gtype (%d)\n", gtype);
987 goto err;
988 }
989
990 ret = func(priv, lnk, li);
991 if (ret < 0)
992 goto err;
993
994 li->link++;
995 err:
996 return ret;
997 }
998
graph_counter(struct device_node * lnk)999 static int graph_counter(struct device_node *lnk)
1000 {
1001 /*
1002 * Multi CPU / Codec
1003 *
1004 * multi {
1005 * ports {
1006 * => lnk: port@0 { ... };
1007 * port@1 { ... };
1008 * port@2 { ... };
1009 * ...
1010 * };
1011 * };
1012 *
1013 * ignore first lnk part
1014 */
1015 if (graph_lnk_is_multi(lnk))
1016 return of_graph_get_endpoint_count(of_get_parent(lnk)) - 1;
1017 /*
1018 * Single CPU / Codec
1019 */
1020 else
1021 return 1;
1022 }
1023
graph_count_normal(struct asoc_simple_priv * priv,struct device_node * lnk,struct link_info * li)1024 static int graph_count_normal(struct asoc_simple_priv *priv,
1025 struct device_node *lnk,
1026 struct link_info *li)
1027 {
1028 struct device_node *cpu_port = lnk;
1029 struct device_node *cpu_ep = port_to_endpoint(cpu_port);
1030 struct device_node *codec_port = of_graph_get_remote_port(cpu_ep);
1031
1032 /*
1033 * CPU {
1034 * => lnk: port { endpoint { .. }; };
1035 * };
1036 */
1037 li->num[li->link].cpus =
1038 li->num[li->link].platforms = graph_counter(cpu_port);
1039 li->num[li->link].codecs = graph_counter(codec_port);
1040
1041 of_node_put(cpu_ep);
1042 of_node_put(codec_port);
1043
1044 return 0;
1045 }
1046
graph_count_dpcm(struct asoc_simple_priv * priv,struct device_node * lnk,struct link_info * li)1047 static int graph_count_dpcm(struct asoc_simple_priv *priv,
1048 struct device_node *lnk,
1049 struct link_info *li)
1050 {
1051 struct device_node *ep = port_to_endpoint(lnk);
1052 struct device_node *rport = of_graph_get_remote_port(ep);
1053
1054 /*
1055 * dpcm {
1056 * // Front-End
1057 * ports@0 {
1058 * => lnk: port@0 { endpoint { ... }; };
1059 * ...
1060 * };
1061 * // Back-End
1062 * ports@1 {
1063 * => lnk: port@0 { endpoint { ... }; };
1064 * ...
1065 * };
1066 * };
1067 */
1068
1069 if (asoc_graph_is_ports0(lnk)) {
1070 li->num[li->link].cpus = graph_counter(rport); /* FE */
1071 li->num[li->link].platforms = graph_counter(rport);
1072 } else {
1073 li->num[li->link].codecs = graph_counter(rport); /* BE */
1074 }
1075
1076 of_node_put(ep);
1077 of_node_put(rport);
1078
1079 return 0;
1080 }
1081
graph_count_c2c(struct asoc_simple_priv * priv,struct device_node * lnk,struct link_info * li)1082 static int graph_count_c2c(struct asoc_simple_priv *priv,
1083 struct device_node *lnk,
1084 struct link_info *li)
1085 {
1086 struct device_node *ports = of_get_parent(lnk);
1087 struct device_node *port0 = lnk;
1088 struct device_node *port1 = of_get_next_child(ports, lnk);
1089 struct device_node *ep0 = port_to_endpoint(port0);
1090 struct device_node *ep1 = port_to_endpoint(port1);
1091 struct device_node *codec0 = of_graph_get_remote_port(ep0);
1092 struct device_node *codec1 = of_graph_get_remote_port(ep1);
1093
1094 of_node_get(lnk);
1095
1096 /*
1097 * codec2codec {
1098 * ports {
1099 * => lnk: port@0 { endpoint { ... }; };
1100 * port@1 { endpoint { ... }; };
1101 * };
1102 * };
1103 */
1104 li->num[li->link].cpus =
1105 li->num[li->link].platforms = graph_counter(codec0);
1106 li->num[li->link].codecs = graph_counter(codec1);
1107 li->num[li->link].c2c = 1;
1108
1109 of_node_put(ports);
1110 of_node_put(port1);
1111 of_node_put(ep0);
1112 of_node_put(ep1);
1113 of_node_put(codec0);
1114 of_node_put(codec1);
1115
1116 return 0;
1117 }
1118
graph_count(struct asoc_simple_priv * priv,struct graph2_custom_hooks * hooks,enum graph_type gtype,struct device_node * lnk,struct link_info * li)1119 static int graph_count(struct asoc_simple_priv *priv,
1120 struct graph2_custom_hooks *hooks,
1121 enum graph_type gtype,
1122 struct device_node *lnk,
1123 struct link_info *li)
1124 {
1125 struct device *dev = simple_priv_to_dev(priv);
1126 GRAPH2_CUSTOM func = NULL;
1127 int ret = -EINVAL;
1128
1129 if (li->link >= SNDRV_MAX_LINKS) {
1130 dev_err(dev, "too many links\n");
1131 return ret;
1132 }
1133
1134 switch (gtype) {
1135 case GRAPH_NORMAL:
1136 func = graph_count_normal;
1137 break;
1138 case GRAPH_DPCM:
1139 func = graph_count_dpcm;
1140 break;
1141 case GRAPH_C2C:
1142 func = graph_count_c2c;
1143 break;
1144 default:
1145 break;
1146 }
1147
1148 if (!func) {
1149 dev_err(dev, "non supported gtype (%d)\n", gtype);
1150 goto err;
1151 }
1152
1153 ret = func(priv, lnk, li);
1154 if (ret < 0)
1155 goto err;
1156
1157 li->link++;
1158 err:
1159 return ret;
1160 }
1161
graph_for_each_link(struct asoc_simple_priv * priv,struct graph2_custom_hooks * hooks,struct link_info * li,int (* func)(struct asoc_simple_priv * priv,struct graph2_custom_hooks * hooks,enum graph_type gtype,struct device_node * lnk,struct link_info * li))1162 static int graph_for_each_link(struct asoc_simple_priv *priv,
1163 struct graph2_custom_hooks *hooks,
1164 struct link_info *li,
1165 int (*func)(struct asoc_simple_priv *priv,
1166 struct graph2_custom_hooks *hooks,
1167 enum graph_type gtype,
1168 struct device_node *lnk,
1169 struct link_info *li))
1170 {
1171 struct of_phandle_iterator it;
1172 struct device *dev = simple_priv_to_dev(priv);
1173 struct device_node *node = dev->of_node;
1174 struct device_node *lnk;
1175 enum graph_type gtype;
1176 int rc, ret;
1177
1178 /* loop for all listed CPU port */
1179 of_for_each_phandle(&it, rc, node, "links", NULL, 0) {
1180 lnk = it.node;
1181
1182 gtype = graph_get_type(priv, lnk);
1183
1184 ret = func(priv, hooks, gtype, lnk, li);
1185 if (ret < 0)
1186 return ret;
1187 }
1188
1189 return 0;
1190 }
1191
audio_graph2_parse_of(struct asoc_simple_priv * priv,struct device * dev,struct graph2_custom_hooks * hooks)1192 int audio_graph2_parse_of(struct asoc_simple_priv *priv, struct device *dev,
1193 struct graph2_custom_hooks *hooks)
1194 {
1195 struct snd_soc_card *card = simple_priv_to_card(priv);
1196 struct link_info *li;
1197 int ret;
1198
1199 li = devm_kzalloc(dev, sizeof(*li), GFP_KERNEL);
1200 if (!li)
1201 return -ENOMEM;
1202
1203 card->probe = asoc_graph_card_probe;
1204 card->owner = THIS_MODULE;
1205 card->dev = dev;
1206
1207 if ((hooks) && (hooks)->hook_pre) {
1208 ret = (hooks)->hook_pre(priv);
1209 if (ret < 0)
1210 goto err;
1211 }
1212
1213 ret = graph_for_each_link(priv, hooks, li, graph_count);
1214 if (!li->link)
1215 ret = -EINVAL;
1216 if (ret < 0)
1217 goto err;
1218
1219 ret = asoc_simple_init_priv(priv, li);
1220 if (ret < 0)
1221 goto err;
1222
1223 priv->pa_gpio = devm_gpiod_get_optional(dev, "pa", GPIOD_OUT_LOW);
1224 if (IS_ERR(priv->pa_gpio)) {
1225 ret = PTR_ERR(priv->pa_gpio);
1226 dev_err(dev, "failed to get amplifier gpio: %d\n", ret);
1227 goto err;
1228 }
1229
1230 ret = asoc_simple_parse_widgets(card, NULL);
1231 if (ret < 0)
1232 goto err;
1233
1234 ret = asoc_simple_parse_routing(card, NULL);
1235 if (ret < 0)
1236 goto err;
1237
1238 memset(li, 0, sizeof(*li));
1239 ret = graph_for_each_link(priv, hooks, li, graph_link);
1240 if (ret < 0)
1241 goto err;
1242
1243 ret = asoc_simple_parse_card_name(card, NULL);
1244 if (ret < 0)
1245 goto err;
1246
1247 snd_soc_card_set_drvdata(card, priv);
1248
1249 if ((hooks) && (hooks)->hook_post) {
1250 ret = (hooks)->hook_post(priv);
1251 if (ret < 0)
1252 goto err;
1253 }
1254
1255 asoc_simple_debug_info(priv);
1256
1257 ret = devm_snd_soc_register_card(dev, card);
1258 err:
1259 devm_kfree(dev, li);
1260
1261 if (ret < 0)
1262 dev_err_probe(dev, ret, "parse error\n");
1263
1264 if (ret == 0)
1265 dev_warn(dev, "Audio Graph Card2 is still under Experimental stage\n");
1266
1267 return ret;
1268 }
1269 EXPORT_SYMBOL_GPL(audio_graph2_parse_of);
1270
graph_probe(struct platform_device * pdev)1271 static int graph_probe(struct platform_device *pdev)
1272 {
1273 struct asoc_simple_priv *priv;
1274 struct device *dev = &pdev->dev;
1275
1276 /* Allocate the private data and the DAI link array */
1277 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1278 if (!priv)
1279 return -ENOMEM;
1280
1281 return audio_graph2_parse_of(priv, dev, NULL);
1282 }
1283
1284 static const struct of_device_id graph_of_match[] = {
1285 { .compatible = "audio-graph-card2", },
1286 {},
1287 };
1288 MODULE_DEVICE_TABLE(of, graph_of_match);
1289
1290 static struct platform_driver graph_card = {
1291 .driver = {
1292 .name = "asoc-audio-graph-card2",
1293 .pm = &snd_soc_pm_ops,
1294 .of_match_table = graph_of_match,
1295 },
1296 .probe = graph_probe,
1297 .remove = asoc_simple_remove,
1298 };
1299 module_platform_driver(graph_card);
1300
1301 MODULE_ALIAS("platform:asoc-audio-graph-card2");
1302 MODULE_LICENSE("GPL v2");
1303 MODULE_DESCRIPTION("ASoC Audio Graph Card2");
1304 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1305