xref: /linux/sound/soc/generic/audio-graph-card2.c (revision 4359a011e259a4608afc7fb3635370c9d4ba5943)
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 
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 
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 
307 static int graph_lnk_is_multi(struct device_node *lnk)
308 {
309 	return __graph_get_type(lnk) == GRAPH_MULTI;
310 }
311 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 device_node *port0, *port1, *ports;
872 	struct device_node *codec0_port, *codec1_port;
873 	struct device_node *ep0, *ep1;
874 	u32 val = 0;
875 	int ret = -EINVAL;
876 
877 	/*
878 	 * codec2codec {
879 	 *	ports {
880 	 *		rate = <48000>;
881 	 * =>	lnk:	port@0 { c2c0_ep: { ... = codec0_ep; }; };
882 	 *		port@1 { c2c1_ep: { ... = codec1_ep; }; };
883 	 *	};
884 	 * };
885 	 *
886 	 * Codec {
887 	 *	ports {
888 	 *		port@0 { codec0_ep: ... }; };
889 	 *		port@1 { codec1_ep: ... }; };
890 	 *	};
891 	 * };
892 	 */
893 	of_node_get(lnk);
894 	port0 = lnk;
895 	ports = of_get_parent(port0);
896 	port1 = of_get_next_child(ports, lnk);
897 
898 	/*
899 	 * Card2 can use original Codec2Codec settings if DT has.
900 	 * It will use default settings if no settings on DT.
901 	 * see
902 	 *	asoc_simple_init_for_codec2codec()
903 	 *
904 	 * Add more settings here if needed
905 	 */
906 	of_property_read_u32(ports, "rate", &val);
907 	if (val) {
908 		struct device *dev = simple_priv_to_dev(priv);
909 		struct snd_soc_pcm_stream *c2c_conf;
910 
911 		c2c_conf = devm_kzalloc(dev, sizeof(*c2c_conf), GFP_KERNEL);
912 		if (!c2c_conf)
913 			goto err1;
914 
915 		c2c_conf->formats	= SNDRV_PCM_FMTBIT_S32_LE; /* update ME */
916 		c2c_conf->rates		= SNDRV_PCM_RATE_8000_384000;
917 		c2c_conf->rate_min	=
918 		c2c_conf->rate_max	= val;
919 		c2c_conf->channels_min	=
920 		c2c_conf->channels_max	= 2; /* update ME */
921 
922 		dai_link->params	= c2c_conf;
923 		dai_link->num_params	= 1;
924 	}
925 
926 	ep0 = port_to_endpoint(port0);
927 	ep1 = port_to_endpoint(port1);
928 
929 	codec0_port = of_graph_get_remote_port(ep0);
930 	codec1_port = of_graph_get_remote_port(ep1);
931 
932 	/*
933 	 * call Codec first.
934 	 * see
935 	 *	__graph_parse_node() :: DAI Naming
936 	 */
937 	ret = graph_parse_node(priv, GRAPH_C2C, codec1_port, li, 0);
938 	if (ret < 0)
939 		goto err2;
940 
941 	/*
942 	 * call CPU, and set DAI Name
943 	 */
944 	ret = graph_parse_node(priv, GRAPH_C2C, codec0_port, li, 1);
945 	if (ret < 0)
946 		goto err2;
947 
948 	graph_link_init(priv, codec0_port, li, 1);
949 err2:
950 	of_node_put(ep0);
951 	of_node_put(ep1);
952 	of_node_put(codec0_port);
953 	of_node_put(codec1_port);
954 err1:
955 	of_node_put(ports);
956 	of_node_put(port0);
957 	of_node_put(port1);
958 
959 	return ret;
960 }
961 EXPORT_SYMBOL_GPL(audio_graph2_link_c2c);
962 
963 static int graph_link(struct asoc_simple_priv *priv,
964 		      struct graph2_custom_hooks *hooks,
965 		      enum graph_type gtype,
966 		      struct device_node *lnk,
967 		      struct link_info *li)
968 {
969 	struct device *dev = simple_priv_to_dev(priv);
970 	GRAPH2_CUSTOM func = NULL;
971 	int ret = -EINVAL;
972 
973 	switch (gtype) {
974 	case GRAPH_NORMAL:
975 		if (hooks && hooks->custom_normal)
976 			func = hooks->custom_normal;
977 		else
978 			func = audio_graph2_link_normal;
979 		break;
980 	case GRAPH_DPCM:
981 		if (hooks && hooks->custom_dpcm)
982 			func = hooks->custom_dpcm;
983 		else
984 			func = audio_graph2_link_dpcm;
985 		break;
986 	case GRAPH_C2C:
987 		if (hooks && hooks->custom_c2c)
988 			func = hooks->custom_c2c;
989 		else
990 			func = audio_graph2_link_c2c;
991 		break;
992 	default:
993 		break;
994 	}
995 
996 	if (!func) {
997 		dev_err(dev, "non supported gtype (%d)\n", gtype);
998 		goto err;
999 	}
1000 
1001 	ret = func(priv, lnk, li);
1002 	if (ret < 0)
1003 		goto err;
1004 
1005 	li->link++;
1006 err:
1007 	return ret;
1008 }
1009 
1010 static int graph_counter(struct device_node *lnk)
1011 {
1012 	/*
1013 	 * Multi CPU / Codec
1014 	 *
1015 	 * multi {
1016 	 *	ports {
1017 	 * =>		lnk:	port@0 { ... };
1018 	 *			port@1 { ... };
1019 	 *			port@2 { ... };
1020 	 *			...
1021 	 *	};
1022 	 * };
1023 	 *
1024 	 * ignore first lnk part
1025 	 */
1026 	if (graph_lnk_is_multi(lnk))
1027 		return of_graph_get_endpoint_count(of_get_parent(lnk)) - 1;
1028 	/*
1029 	 * Single CPU / Codec
1030 	 */
1031 	else
1032 		return 1;
1033 }
1034 
1035 static int graph_count_normal(struct asoc_simple_priv *priv,
1036 			      struct device_node *lnk,
1037 			      struct link_info *li)
1038 {
1039 	struct device_node *cpu_port = lnk;
1040 	struct device_node *cpu_ep = port_to_endpoint(cpu_port);
1041 	struct device_node *codec_port = of_graph_get_remote_port(cpu_ep);
1042 
1043 	/*
1044 	 *	CPU {
1045 	 * =>		lnk: port { endpoint { .. }; };
1046 	 *	};
1047 	 */
1048 	li->num[li->link].cpus		=
1049 	li->num[li->link].platforms	= graph_counter(cpu_port);
1050 	li->num[li->link].codecs	= graph_counter(codec_port);
1051 
1052 	of_node_put(cpu_ep);
1053 	of_node_put(codec_port);
1054 
1055 	return 0;
1056 }
1057 
1058 static int graph_count_dpcm(struct asoc_simple_priv *priv,
1059 			    struct device_node *lnk,
1060 			    struct link_info *li)
1061 {
1062 	struct device_node *ep = port_to_endpoint(lnk);
1063 	struct device_node *rport = of_graph_get_remote_port(ep);
1064 
1065 	/*
1066 	 * dpcm {
1067 	 *	// Front-End
1068 	 *	ports@0 {
1069 	 * =>		lnk: port@0 { endpoint { ... }; };
1070 	 *		 ...
1071 	 *	};
1072 	 *	// Back-End
1073 	 *	ports@1 {
1074 	 * =>		lnk: port@0 { endpoint { ... }; };
1075 	 *		 ...
1076 	 *	};
1077 	 * };
1078 	 */
1079 
1080 	if (asoc_graph_is_ports0(lnk)) {
1081 		li->num[li->link].cpus		= graph_counter(rport); /* FE */
1082 		li->num[li->link].platforms	= graph_counter(rport);
1083 	} else {
1084 		li->num[li->link].codecs	= graph_counter(rport); /* BE */
1085 	}
1086 
1087 	of_node_put(ep);
1088 	of_node_put(rport);
1089 
1090 	return 0;
1091 }
1092 
1093 static int graph_count_c2c(struct asoc_simple_priv *priv,
1094 			   struct device_node *lnk,
1095 			   struct link_info *li)
1096 {
1097 	struct device_node *ports = of_get_parent(lnk);
1098 	struct device_node *port0 = lnk;
1099 	struct device_node *port1 = of_get_next_child(ports, lnk);
1100 	struct device_node *ep0 = port_to_endpoint(port0);
1101 	struct device_node *ep1 = port_to_endpoint(port1);
1102 	struct device_node *codec0 = of_graph_get_remote_port(ep0);
1103 	struct device_node *codec1 = of_graph_get_remote_port(ep1);
1104 
1105 	of_node_get(lnk);
1106 
1107 	/*
1108 	 * codec2codec {
1109 	 *	ports {
1110 	 * =>	lnk:	port@0 { endpoint { ... }; };
1111 	 *		port@1 { endpoint { ... }; };
1112 	 *	};
1113 	 * };
1114 	 */
1115 	li->num[li->link].cpus		=
1116 	li->num[li->link].platforms	= graph_counter(codec0);
1117 	li->num[li->link].codecs	= graph_counter(codec1);
1118 
1119 	of_node_put(ports);
1120 	of_node_put(port1);
1121 	of_node_put(ep0);
1122 	of_node_put(ep1);
1123 	of_node_put(codec0);
1124 	of_node_put(codec1);
1125 
1126 	return 0;
1127 }
1128 
1129 static int graph_count(struct asoc_simple_priv *priv,
1130 		       struct graph2_custom_hooks *hooks,
1131 		       enum graph_type gtype,
1132 		       struct device_node *lnk,
1133 		       struct link_info *li)
1134 {
1135 	struct device *dev = simple_priv_to_dev(priv);
1136 	GRAPH2_CUSTOM func = NULL;
1137 	int ret = -EINVAL;
1138 
1139 	if (li->link >= SNDRV_MAX_LINKS) {
1140 		dev_err(dev, "too many links\n");
1141 		return ret;
1142 	}
1143 
1144 	switch (gtype) {
1145 	case GRAPH_NORMAL:
1146 		func = graph_count_normal;
1147 		break;
1148 	case GRAPH_DPCM:
1149 		func = graph_count_dpcm;
1150 		break;
1151 	case GRAPH_C2C:
1152 		func = graph_count_c2c;
1153 		break;
1154 	default:
1155 		break;
1156 	}
1157 
1158 	if (!func) {
1159 		dev_err(dev, "non supported gtype (%d)\n", gtype);
1160 		goto err;
1161 	}
1162 
1163 	ret = func(priv, lnk, li);
1164 	if (ret < 0)
1165 		goto err;
1166 
1167 	li->link++;
1168 err:
1169 	return ret;
1170 }
1171 
1172 static int graph_for_each_link(struct asoc_simple_priv *priv,
1173 			       struct graph2_custom_hooks *hooks,
1174 			       struct link_info *li,
1175 			       int (*func)(struct asoc_simple_priv *priv,
1176 					   struct graph2_custom_hooks *hooks,
1177 					   enum graph_type gtype,
1178 					   struct device_node *lnk,
1179 					   struct link_info *li))
1180 {
1181 	struct of_phandle_iterator it;
1182 	struct device *dev = simple_priv_to_dev(priv);
1183 	struct device_node *node = dev->of_node;
1184 	struct device_node *lnk;
1185 	enum graph_type gtype;
1186 	int rc, ret;
1187 
1188 	/* loop for all listed CPU port */
1189 	of_for_each_phandle(&it, rc, node, "links", NULL, 0) {
1190 		lnk = it.node;
1191 
1192 		gtype = graph_get_type(priv, lnk);
1193 
1194 		ret = func(priv, hooks, gtype, lnk, li);
1195 		if (ret < 0)
1196 			return ret;
1197 	}
1198 
1199 	return 0;
1200 }
1201 
1202 int audio_graph2_parse_of(struct asoc_simple_priv *priv, struct device *dev,
1203 			  struct graph2_custom_hooks *hooks)
1204 {
1205 	struct snd_soc_card *card = simple_priv_to_card(priv);
1206 	struct link_info *li;
1207 	int ret;
1208 
1209 	li = devm_kzalloc(dev, sizeof(*li), GFP_KERNEL);
1210 	if (!li)
1211 		return -ENOMEM;
1212 
1213 	card->probe	= asoc_graph_card_probe;
1214 	card->owner	= THIS_MODULE;
1215 	card->dev	= dev;
1216 
1217 	if ((hooks) && (hooks)->hook_pre) {
1218 		ret = (hooks)->hook_pre(priv);
1219 		if (ret < 0)
1220 			goto err;
1221 	}
1222 
1223 	ret = graph_for_each_link(priv, hooks, li, graph_count);
1224 	if (!li->link)
1225 		ret = -EINVAL;
1226 	if (ret < 0)
1227 		goto err;
1228 
1229 	ret = asoc_simple_init_priv(priv, li);
1230 	if (ret < 0)
1231 		goto err;
1232 
1233 	priv->pa_gpio = devm_gpiod_get_optional(dev, "pa", GPIOD_OUT_LOW);
1234 	if (IS_ERR(priv->pa_gpio)) {
1235 		ret = PTR_ERR(priv->pa_gpio);
1236 		dev_err(dev, "failed to get amplifier gpio: %d\n", ret);
1237 		goto err;
1238 	}
1239 
1240 	ret = asoc_simple_parse_widgets(card, NULL);
1241 	if (ret < 0)
1242 		goto err;
1243 
1244 	ret = asoc_simple_parse_routing(card, NULL);
1245 	if (ret < 0)
1246 		goto err;
1247 
1248 	memset(li, 0, sizeof(*li));
1249 	ret = graph_for_each_link(priv, hooks, li, graph_link);
1250 	if (ret < 0)
1251 		goto err;
1252 
1253 	ret = asoc_simple_parse_card_name(card, NULL);
1254 	if (ret < 0)
1255 		goto err;
1256 
1257 	snd_soc_card_set_drvdata(card, priv);
1258 
1259 	if ((hooks) && (hooks)->hook_post) {
1260 		ret = (hooks)->hook_post(priv);
1261 		if (ret < 0)
1262 			goto err;
1263 	}
1264 
1265 	asoc_simple_debug_info(priv);
1266 
1267 	ret = devm_snd_soc_register_card(dev, card);
1268 err:
1269 	devm_kfree(dev, li);
1270 
1271 	if (ret < 0)
1272 		dev_err_probe(dev, ret, "parse error\n");
1273 
1274 	if (ret == 0)
1275 		dev_warn(dev, "Audio Graph Card2 is still under Experimental stage\n");
1276 
1277 	return ret;
1278 }
1279 EXPORT_SYMBOL_GPL(audio_graph2_parse_of);
1280 
1281 static int graph_probe(struct platform_device *pdev)
1282 {
1283 	struct asoc_simple_priv *priv;
1284 	struct device *dev = &pdev->dev;
1285 
1286 	/* Allocate the private data and the DAI link array */
1287 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1288 	if (!priv)
1289 		return -ENOMEM;
1290 
1291 	return audio_graph2_parse_of(priv, dev, NULL);
1292 }
1293 
1294 static const struct of_device_id graph_of_match[] = {
1295 	{ .compatible = "audio-graph-card2", },
1296 	{},
1297 };
1298 MODULE_DEVICE_TABLE(of, graph_of_match);
1299 
1300 static struct platform_driver graph_card = {
1301 	.driver = {
1302 		.name = "asoc-audio-graph-card2",
1303 		.pm = &snd_soc_pm_ops,
1304 		.of_match_table = graph_of_match,
1305 	},
1306 	.probe	= graph_probe,
1307 	.remove	= asoc_simple_remove,
1308 };
1309 module_platform_driver(graph_card);
1310 
1311 MODULE_ALIAS("platform:asoc-audio-graph-card2");
1312 MODULE_LICENSE("GPL v2");
1313 MODULE_DESCRIPTION("ASoC Audio Graph Card2");
1314 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1315