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