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