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