1// SPDX-License-Identifier: GPL-2.0
2//
3// ASoC audio graph sound card support
4//
5// Copyright (C) 2016 Renesas Solutions Corp.
6// Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7//
8// based on ${LINUX}/sound/soc/generic/simple-card.c
9
10#include <linux/clk.h>
11#include <linux/device.h>
12#include <linux/gpio.h>
13#include <linux/gpio/consumer.h>
14#include <linux/module.h>
15#include <linux/of.h>
16#include <linux/of_device.h>
17#include <linux/of_gpio.h>
18#include <linux/of_graph.h>
19#include <linux/platform_device.h>
20#include <linux/string.h>
21#include <sound/graph_card.h>
22
23#define DPCM_SELECTABLE 1
24
25static int graph_outdrv_event(struct snd_soc_dapm_widget *w,
26			      struct snd_kcontrol *kcontrol,
27			      int event)
28{
29	struct snd_soc_dapm_context *dapm = w->dapm;
30	struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(dapm->card);
31
32	switch (event) {
33	case SND_SOC_DAPM_POST_PMU:
34		gpiod_set_value_cansleep(priv->pa_gpio, 1);
35		break;
36	case SND_SOC_DAPM_PRE_PMD:
37		gpiod_set_value_cansleep(priv->pa_gpio, 0);
38		break;
39	default:
40		return -EINVAL;
41	}
42
43	return 0;
44}
45
46static const struct snd_soc_dapm_widget graph_dapm_widgets[] = {
47	SND_SOC_DAPM_OUT_DRV_E("Amplifier", SND_SOC_NOPM,
48			       0, 0, NULL, 0, graph_outdrv_event,
49			       SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
50};
51
52static const struct snd_soc_ops graph_ops = {
53	.startup	= asoc_simple_startup,
54	.shutdown	= asoc_simple_shutdown,
55	.hw_params	= asoc_simple_hw_params,
56};
57
58static bool soc_component_is_pcm(struct snd_soc_dai_link_component *dlc)
59{
60	struct snd_soc_dai *dai = snd_soc_find_dai_with_mutex(dlc);
61
62	if (dai && (dai->component->driver->pcm_construct ||
63		    (dai->driver->ops && dai->driver->ops->pcm_new)))
64		return true;
65
66	return false;
67}
68
69static void graph_parse_convert(struct device *dev,
70				struct device_node *ep,
71				struct asoc_simple_data *adata)
72{
73	struct device_node *top = dev->of_node;
74	struct device_node *port = of_get_parent(ep);
75	struct device_node *ports = of_get_parent(port);
76	struct device_node *node = of_graph_get_port_parent(ep);
77
78	asoc_simple_parse_convert(top,   NULL,   adata);
79	if (of_node_name_eq(ports, "ports"))
80		asoc_simple_parse_convert(ports, NULL, adata);
81	asoc_simple_parse_convert(port,  NULL,   adata);
82	asoc_simple_parse_convert(ep,    NULL,   adata);
83
84	of_node_put(port);
85	of_node_put(ports);
86	of_node_put(node);
87}
88
89static void graph_parse_mclk_fs(struct device_node *top,
90				struct device_node *ep,
91				struct simple_dai_props *props)
92{
93	struct device_node *port	= of_get_parent(ep);
94	struct device_node *ports	= of_get_parent(port);
95
96	of_property_read_u32(top,	"mclk-fs", &props->mclk_fs);
97	if (of_node_name_eq(ports, "ports"))
98		of_property_read_u32(ports, "mclk-fs", &props->mclk_fs);
99	of_property_read_u32(port,	"mclk-fs", &props->mclk_fs);
100	of_property_read_u32(ep,	"mclk-fs", &props->mclk_fs);
101
102	of_node_put(port);
103	of_node_put(ports);
104}
105
106static int graph_parse_node(struct asoc_simple_priv *priv,
107			    struct device_node *ep,
108			    struct link_info *li,
109			    int *cpu)
110{
111	struct device *dev = simple_priv_to_dev(priv);
112	struct device_node *top = dev->of_node;
113	struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
114	struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
115	struct snd_soc_dai_link_component *dlc;
116	struct asoc_simple_dai *dai;
117	int ret;
118
119	if (cpu) {
120		dlc = asoc_link_to_cpu(dai_link, 0);
121		dai = simple_props_to_dai_cpu(dai_props, 0);
122	} else {
123		dlc = asoc_link_to_codec(dai_link, 0);
124		dai = simple_props_to_dai_codec(dai_props, 0);
125	}
126
127	graph_parse_mclk_fs(top, ep, dai_props);
128
129	ret = asoc_graph_parse_dai(dev, ep, dlc, cpu);
130	if (ret < 0)
131		return ret;
132
133	ret = asoc_simple_parse_tdm(ep, dai);
134	if (ret < 0)
135		return ret;
136
137	ret = asoc_simple_parse_clk(dev, ep, dai, dlc);
138	if (ret < 0)
139		return ret;
140
141	return 0;
142}
143
144static int graph_link_init(struct asoc_simple_priv *priv,
145			   struct device_node *cpu_ep,
146			   struct device_node *codec_ep,
147			   struct link_info *li,
148			   char *name)
149{
150	struct device *dev = simple_priv_to_dev(priv);
151	struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
152	int ret;
153
154	ret = asoc_simple_parse_daifmt(dev, cpu_ep, codec_ep,
155				       NULL, &dai_link->dai_fmt);
156	if (ret < 0)
157		return ret;
158
159	dai_link->init		= asoc_simple_dai_init;
160	dai_link->ops		= &graph_ops;
161	if (priv->ops)
162		dai_link->ops	= priv->ops;
163
164	return asoc_simple_set_dailink_name(dev, dai_link, name);
165}
166
167static int graph_dai_link_of_dpcm(struct asoc_simple_priv *priv,
168				  struct device_node *cpu_ep,
169				  struct device_node *codec_ep,
170				  struct link_info *li)
171{
172	struct device *dev = simple_priv_to_dev(priv);
173	struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
174	struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
175	struct device_node *top = dev->of_node;
176	struct device_node *ep = li->cpu ? cpu_ep : codec_ep;
177	char dai_name[64];
178	int ret;
179
180	dev_dbg(dev, "link_of DPCM (%pOF)\n", ep);
181
182	if (li->cpu) {
183		struct snd_soc_card *card = simple_priv_to_card(priv);
184		struct snd_soc_dai_link_component *cpus = asoc_link_to_cpu(dai_link, 0);
185		struct snd_soc_dai_link_component *platforms = asoc_link_to_platform(dai_link, 0);
186		int is_single_links = 0;
187
188		/* Codec is dummy */
189
190		/* FE settings */
191		dai_link->dynamic		= 1;
192		dai_link->dpcm_merged_format	= 1;
193
194		ret = graph_parse_node(priv, cpu_ep, li, &is_single_links);
195		if (ret)
196			return ret;
197
198		snprintf(dai_name, sizeof(dai_name),
199			 "fe.%pOFP.%s", cpus->of_node, cpus->dai_name);
200		/*
201		 * In BE<->BE connections it is not required to create
202		 * PCM devices at CPU end of the dai link and thus 'no_pcm'
203		 * flag needs to be set. It is useful when there are many
204		 * BE components and some of these have to be connected to
205		 * form a valid audio path.
206		 *
207		 * For example: FE <-> BE1 <-> BE2 <-> ... <-> BEn where
208		 * there are 'n' BE components in the path.
209		 */
210		if (card->component_chaining && !soc_component_is_pcm(cpus)) {
211			dai_link->no_pcm = 1;
212			dai_link->be_hw_params_fixup = asoc_simple_be_hw_params_fixup;
213		}
214
215		asoc_simple_canonicalize_cpu(cpus, is_single_links);
216		asoc_simple_canonicalize_platform(platforms, cpus);
217	} else {
218		struct snd_soc_codec_conf *cconf = simple_props_to_codec_conf(dai_props, 0);
219		struct snd_soc_dai_link_component *codecs = asoc_link_to_codec(dai_link, 0);
220		struct device_node *port;
221		struct device_node *ports;
222
223		/* CPU is dummy */
224
225		/* BE settings */
226		dai_link->no_pcm		= 1;
227		dai_link->be_hw_params_fixup	= asoc_simple_be_hw_params_fixup;
228
229		ret = graph_parse_node(priv, codec_ep, li, NULL);
230		if (ret < 0)
231			return ret;
232
233		snprintf(dai_name, sizeof(dai_name),
234			 "be.%pOFP.%s", codecs->of_node, codecs->dai_name);
235
236		/* check "prefix" from top node */
237		port = of_get_parent(ep);
238		ports = of_get_parent(port);
239		snd_soc_of_parse_node_prefix(top, cconf, codecs->of_node,
240					      "prefix");
241		if (of_node_name_eq(ports, "ports"))
242			snd_soc_of_parse_node_prefix(ports, cconf, codecs->of_node, "prefix");
243		snd_soc_of_parse_node_prefix(port, cconf, codecs->of_node,
244					     "prefix");
245
246		of_node_put(ports);
247		of_node_put(port);
248	}
249
250	graph_parse_convert(dev, ep, &dai_props->adata);
251
252	snd_soc_dai_link_set_capabilities(dai_link);
253
254	ret = graph_link_init(priv, cpu_ep, codec_ep, li, dai_name);
255
256	li->link++;
257
258	return ret;
259}
260
261static int graph_dai_link_of(struct asoc_simple_priv *priv,
262			     struct device_node *cpu_ep,
263			     struct device_node *codec_ep,
264			     struct link_info *li)
265{
266	struct device *dev = simple_priv_to_dev(priv);
267	struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
268	struct snd_soc_dai_link_component *cpus = asoc_link_to_cpu(dai_link, 0);
269	struct snd_soc_dai_link_component *codecs = asoc_link_to_codec(dai_link, 0);
270	struct snd_soc_dai_link_component *platforms = asoc_link_to_platform(dai_link, 0);
271	char dai_name[64];
272	int ret, is_single_links = 0;
273
274	dev_dbg(dev, "link_of (%pOF)\n", cpu_ep);
275
276	ret = graph_parse_node(priv, cpu_ep, li, &is_single_links);
277	if (ret < 0)
278		return ret;
279
280	ret = graph_parse_node(priv, codec_ep, li, NULL);
281	if (ret < 0)
282		return ret;
283
284	snprintf(dai_name, sizeof(dai_name),
285		 "%s-%s", cpus->dai_name, codecs->dai_name);
286
287	asoc_simple_canonicalize_cpu(cpus, is_single_links);
288	asoc_simple_canonicalize_platform(platforms, cpus);
289
290	ret = graph_link_init(priv, cpu_ep, codec_ep, li, dai_name);
291	if (ret < 0)
292		return ret;
293
294	li->link++;
295
296	return 0;
297}
298
299static inline bool parse_as_dpcm_link(struct asoc_simple_priv *priv,
300				      struct device_node *codec_port,
301				      struct asoc_simple_data *adata)
302{
303	if (priv->force_dpcm)
304		return true;
305
306	if (!priv->dpcm_selectable)
307		return false;
308
309	/*
310	 * It is DPCM
311	 * if Codec port has many endpoints,
312	 * or has convert-xxx property
313	 */
314	if ((of_get_child_count(codec_port) > 1) ||
315	    asoc_simple_is_convert_required(adata))
316		return true;
317
318	return false;
319}
320
321static int __graph_for_each_link(struct asoc_simple_priv *priv,
322			struct link_info *li,
323			int (*func_noml)(struct asoc_simple_priv *priv,
324					 struct device_node *cpu_ep,
325					 struct device_node *codec_ep,
326					 struct link_info *li),
327			int (*func_dpcm)(struct asoc_simple_priv *priv,
328					 struct device_node *cpu_ep,
329					 struct device_node *codec_ep,
330					 struct link_info *li))
331{
332	struct of_phandle_iterator it;
333	struct device *dev = simple_priv_to_dev(priv);
334	struct device_node *node = dev->of_node;
335	struct device_node *cpu_port;
336	struct device_node *cpu_ep;
337	struct device_node *codec_ep;
338	struct device_node *codec_port;
339	struct device_node *codec_port_old = NULL;
340	struct asoc_simple_data adata;
341	int rc, ret = 0;
342
343	/* loop for all listed CPU port */
344	of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
345		cpu_port = it.node;
346		cpu_ep	 = NULL;
347
348		/* loop for all CPU endpoint */
349		while (1) {
350			cpu_ep = of_get_next_child(cpu_port, cpu_ep);
351			if (!cpu_ep)
352				break;
353
354			/* get codec */
355			codec_ep = of_graph_get_remote_endpoint(cpu_ep);
356			codec_port = of_get_parent(codec_ep);
357
358			/* get convert-xxx property */
359			memset(&adata, 0, sizeof(adata));
360			graph_parse_convert(dev, codec_ep, &adata);
361			graph_parse_convert(dev, cpu_ep,   &adata);
362
363			/* check if link requires DPCM parsing */
364			if (parse_as_dpcm_link(priv, codec_port, &adata)) {
365				/*
366				 * Codec endpoint can be NULL for pluggable audio HW.
367				 * Platform DT can populate the Codec endpoint depending on the
368				 * plugged HW.
369				 */
370				/* Do it all CPU endpoint, and 1st Codec endpoint */
371				if (li->cpu ||
372				    ((codec_port_old != codec_port) && codec_ep))
373					ret = func_dpcm(priv, cpu_ep, codec_ep, li);
374			/* else normal sound */
375			} else {
376				if (li->cpu)
377					ret = func_noml(priv, cpu_ep, codec_ep, li);
378			}
379
380			of_node_put(codec_ep);
381			of_node_put(codec_port);
382
383			if (ret < 0) {
384				of_node_put(cpu_ep);
385				return ret;
386			}
387
388			codec_port_old = codec_port;
389		}
390	}
391
392	return 0;
393}
394
395static int graph_for_each_link(struct asoc_simple_priv *priv,
396			       struct link_info *li,
397			       int (*func_noml)(struct asoc_simple_priv *priv,
398						struct device_node *cpu_ep,
399						struct device_node *codec_ep,
400						struct link_info *li),
401			       int (*func_dpcm)(struct asoc_simple_priv *priv,
402						struct device_node *cpu_ep,
403						struct device_node *codec_ep,
404						struct link_info *li))
405{
406	int ret;
407	/*
408	 * Detect all CPU first, and Detect all Codec 2nd.
409	 *
410	 * In Normal sound case, all DAIs are detected
411	 * as "CPU-Codec".
412	 *
413	 * In DPCM sound case,
414	 * all CPUs   are detected as "CPU-dummy", and
415	 * all Codecs are detected as "dummy-Codec".
416	 * To avoid random sub-device numbering,
417	 * detect "dummy-Codec" in last;
418	 */
419	for (li->cpu = 1; li->cpu >= 0; li->cpu--) {
420		ret = __graph_for_each_link(priv, li, func_noml, func_dpcm);
421		if (ret < 0)
422			break;
423	}
424
425	return ret;
426}
427
428static int graph_count_noml(struct asoc_simple_priv *priv,
429			    struct device_node *cpu_ep,
430			    struct device_node *codec_ep,
431			    struct link_info *li)
432{
433	struct device *dev = simple_priv_to_dev(priv);
434
435	if (li->link >= SNDRV_MAX_LINKS) {
436		dev_err(dev, "too many links\n");
437		return -EINVAL;
438	}
439
440	/*
441	 * DON'T REMOVE platforms
442	 * see
443	 *	simple-card.c :: simple_count_noml()
444	 */
445	li->num[li->link].cpus		= 1;
446	li->num[li->link].platforms     = 1;
447
448	li->num[li->link].codecs	= 1;
449
450	li->link += 1; /* 1xCPU-Codec */
451
452	dev_dbg(dev, "Count As Normal\n");
453
454	return 0;
455}
456
457static int graph_count_dpcm(struct asoc_simple_priv *priv,
458			    struct device_node *cpu_ep,
459			    struct device_node *codec_ep,
460			    struct link_info *li)
461{
462	struct device *dev = simple_priv_to_dev(priv);
463
464	if (li->link >= SNDRV_MAX_LINKS) {
465		dev_err(dev, "too many links\n");
466		return -EINVAL;
467	}
468
469	if (li->cpu) {
470		/*
471		 * DON'T REMOVE platforms
472		 * see
473		 *	simple-card.c :: simple_count_noml()
474		 */
475		li->num[li->link].cpus		= 1;
476		li->num[li->link].platforms     = 1;
477
478		li->link++; /* 1xCPU-dummy */
479	} else {
480		li->num[li->link].codecs	= 1;
481
482		li->link++; /* 1xdummy-Codec */
483	}
484
485	dev_dbg(dev, "Count As DPCM\n");
486
487	return 0;
488}
489
490static int graph_get_dais_count(struct asoc_simple_priv *priv,
491				struct link_info *li)
492{
493	/*
494	 * link_num :	number of links.
495	 *		CPU-Codec / CPU-dummy / dummy-Codec
496	 * dais_num :	number of DAIs
497	 * ccnf_num :	number of codec_conf
498	 *		same number for "dummy-Codec"
499	 *
500	 * ex1)
501	 * CPU0 --- Codec0	link : 5
502	 * CPU1 --- Codec1	dais : 7
503	 * CPU2 -/		ccnf : 1
504	 * CPU3 --- Codec2
505	 *
506	 *	=> 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec
507	 *	=> 7 DAIs  = 4xCPU + 3xCodec
508	 *	=> 1 ccnf  = 1xdummy-Codec
509	 *
510	 * ex2)
511	 * CPU0 --- Codec0	link : 5
512	 * CPU1 --- Codec1	dais : 6
513	 * CPU2 -/		ccnf : 1
514	 * CPU3 -/
515	 *
516	 *	=> 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec
517	 *	=> 6 DAIs  = 4xCPU + 2xCodec
518	 *	=> 1 ccnf  = 1xdummy-Codec
519	 *
520	 * ex3)
521	 * CPU0 --- Codec0	link : 6
522	 * CPU1 -/		dais : 6
523	 * CPU2 --- Codec1	ccnf : 2
524	 * CPU3 -/
525	 *
526	 *	=> 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec
527	 *	=> 6 DAIs  = 4xCPU + 2xCodec
528	 *	=> 2 ccnf  = 2xdummy-Codec
529	 *
530	 * ex4)
531	 * CPU0 --- Codec0 (convert-rate)	link : 3
532	 * CPU1 --- Codec1			dais : 4
533	 *					ccnf : 1
534	 *
535	 *	=> 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec
536	 *	=> 4 DAIs  = 2xCPU + 2xCodec
537	 *	=> 1 ccnf  = 1xdummy-Codec
538	 */
539	return graph_for_each_link(priv, li,
540				   graph_count_noml,
541				   graph_count_dpcm);
542}
543
544int audio_graph_parse_of(struct asoc_simple_priv *priv, struct device *dev)
545{
546	struct snd_soc_card *card = simple_priv_to_card(priv);
547	struct link_info *li;
548	int ret;
549
550	li = devm_kzalloc(dev, sizeof(*li), GFP_KERNEL);
551	if (!li)
552		return -ENOMEM;
553
554	card->owner = THIS_MODULE;
555	card->dev = dev;
556
557	ret = graph_get_dais_count(priv, li);
558	if (ret < 0)
559		return ret;
560
561	if (!li->link)
562		return -EINVAL;
563
564	ret = asoc_simple_init_priv(priv, li);
565	if (ret < 0)
566		return ret;
567
568	priv->pa_gpio = devm_gpiod_get_optional(dev, "pa", GPIOD_OUT_LOW);
569	if (IS_ERR(priv->pa_gpio)) {
570		ret = PTR_ERR(priv->pa_gpio);
571		dev_err(dev, "failed to get amplifier gpio: %d\n", ret);
572		return ret;
573	}
574
575	ret = asoc_simple_parse_widgets(card, NULL);
576	if (ret < 0)
577		return ret;
578
579	ret = asoc_simple_parse_routing(card, NULL);
580	if (ret < 0)
581		return ret;
582
583	memset(li, 0, sizeof(*li));
584	ret = graph_for_each_link(priv, li,
585				  graph_dai_link_of,
586				  graph_dai_link_of_dpcm);
587	if (ret < 0)
588		goto err;
589
590	ret = asoc_simple_parse_card_name(card, NULL);
591	if (ret < 0)
592		goto err;
593
594	snd_soc_card_set_drvdata(card, priv);
595
596	asoc_simple_debug_info(priv);
597
598	ret = devm_snd_soc_register_card(dev, card);
599	if (ret < 0)
600		goto err;
601
602	devm_kfree(dev, li);
603	return 0;
604
605err:
606	asoc_simple_clean_reference(card);
607
608	return dev_err_probe(dev, ret, "parse error\n");
609}
610EXPORT_SYMBOL_GPL(audio_graph_parse_of);
611
612static int graph_probe(struct platform_device *pdev)
613{
614	struct asoc_simple_priv *priv;
615	struct device *dev = &pdev->dev;
616	struct snd_soc_card *card;
617
618	/* Allocate the private data and the DAI link array */
619	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
620	if (!priv)
621		return -ENOMEM;
622
623	card = simple_priv_to_card(priv);
624	card->dapm_widgets	= graph_dapm_widgets;
625	card->num_dapm_widgets	= ARRAY_SIZE(graph_dapm_widgets);
626	card->probe		= asoc_graph_card_probe;
627
628	if (of_device_get_match_data(dev))
629		priv->dpcm_selectable = 1;
630
631	return audio_graph_parse_of(priv, dev);
632}
633
634static const struct of_device_id graph_of_match[] = {
635	{ .compatible = "audio-graph-card", },
636	{ .compatible = "audio-graph-scu-card",
637	  .data = (void *)DPCM_SELECTABLE },
638	{},
639};
640MODULE_DEVICE_TABLE(of, graph_of_match);
641
642static struct platform_driver graph_card = {
643	.driver = {
644		.name = "asoc-audio-graph-card",
645		.pm = &snd_soc_pm_ops,
646		.of_match_table = graph_of_match,
647	},
648	.probe = graph_probe,
649	.remove = asoc_simple_remove,
650};
651module_platform_driver(graph_card);
652
653MODULE_ALIAS("platform:asoc-audio-graph-card");
654MODULE_LICENSE("GPL v2");
655MODULE_DESCRIPTION("ASoC Audio Graph Sound Card");
656MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
657