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 25 static 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 46 static 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 52 static 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 58 static 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->pcm_new)) 64 return true; 65 66 return false; 67 } 68 69 static 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 89 static 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 106 static 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(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 144 static 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 167 static 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 261 static 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 299 static 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 321 static 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 395 static 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 428 static int graph_get_dais_count(struct asoc_simple_priv *priv, 429 struct link_info *li); 430 431 int audio_graph_parse_of(struct asoc_simple_priv *priv, struct device *dev) 432 { 433 struct snd_soc_card *card = simple_priv_to_card(priv); 434 struct link_info *li; 435 int ret; 436 437 li = devm_kzalloc(dev, sizeof(*li), GFP_KERNEL); 438 if (!li) 439 return -ENOMEM; 440 441 card->owner = THIS_MODULE; 442 card->dev = dev; 443 444 ret = graph_get_dais_count(priv, li); 445 if (ret < 0) 446 return ret; 447 448 if (!li->link) 449 return -EINVAL; 450 451 ret = asoc_simple_init_priv(priv, li); 452 if (ret < 0) 453 return ret; 454 455 priv->pa_gpio = devm_gpiod_get_optional(dev, "pa", GPIOD_OUT_LOW); 456 if (IS_ERR(priv->pa_gpio)) { 457 ret = PTR_ERR(priv->pa_gpio); 458 dev_err(dev, "failed to get amplifier gpio: %d\n", ret); 459 return ret; 460 } 461 462 ret = asoc_simple_parse_widgets(card, NULL); 463 if (ret < 0) 464 return ret; 465 466 ret = asoc_simple_parse_routing(card, NULL); 467 if (ret < 0) 468 return ret; 469 470 memset(li, 0, sizeof(*li)); 471 ret = graph_for_each_link(priv, li, 472 graph_dai_link_of, 473 graph_dai_link_of_dpcm); 474 if (ret < 0) 475 goto err; 476 477 ret = asoc_simple_parse_card_name(card, NULL); 478 if (ret < 0) 479 goto err; 480 481 snd_soc_card_set_drvdata(card, priv); 482 483 asoc_simple_debug_info(priv); 484 485 ret = devm_snd_soc_register_card(dev, card); 486 if (ret < 0) 487 goto err; 488 489 devm_kfree(dev, li); 490 return 0; 491 492 err: 493 asoc_simple_clean_reference(card); 494 495 return dev_err_probe(dev, ret, "parse error\n"); 496 } 497 EXPORT_SYMBOL_GPL(audio_graph_parse_of); 498 499 static int graph_count_noml(struct asoc_simple_priv *priv, 500 struct device_node *cpu_ep, 501 struct device_node *codec_ep, 502 struct link_info *li) 503 { 504 struct device *dev = simple_priv_to_dev(priv); 505 506 if (li->link >= SNDRV_MAX_LINKS) { 507 dev_err(dev, "too many links\n"); 508 return -EINVAL; 509 } 510 511 /* 512 * DON'T REMOVE platforms 513 * see 514 * simple-card.c :: simple_count_noml() 515 */ 516 li->num[li->link].cpus = 1; 517 li->num[li->link].platforms = 1; 518 519 li->num[li->link].codecs = 1; 520 521 li->link += 1; /* 1xCPU-Codec */ 522 523 dev_dbg(dev, "Count As Normal\n"); 524 525 return 0; 526 } 527 528 static int graph_count_dpcm(struct asoc_simple_priv *priv, 529 struct device_node *cpu_ep, 530 struct device_node *codec_ep, 531 struct link_info *li) 532 { 533 struct device *dev = simple_priv_to_dev(priv); 534 535 if (li->link >= SNDRV_MAX_LINKS) { 536 dev_err(dev, "too many links\n"); 537 return -EINVAL; 538 } 539 540 if (li->cpu) { 541 /* 542 * DON'T REMOVE platforms 543 * see 544 * simple-card.c :: simple_count_noml() 545 */ 546 li->num[li->link].cpus = 1; 547 li->num[li->link].platforms = 1; 548 549 li->link++; /* 1xCPU-dummy */ 550 } else { 551 li->num[li->link].codecs = 1; 552 553 li->link++; /* 1xdummy-Codec */ 554 } 555 556 dev_dbg(dev, "Count As DPCM\n"); 557 558 return 0; 559 } 560 561 static int graph_get_dais_count(struct asoc_simple_priv *priv, 562 struct link_info *li) 563 { 564 /* 565 * link_num : number of links. 566 * CPU-Codec / CPU-dummy / dummy-Codec 567 * dais_num : number of DAIs 568 * ccnf_num : number of codec_conf 569 * same number for "dummy-Codec" 570 * 571 * ex1) 572 * CPU0 --- Codec0 link : 5 573 * CPU1 --- Codec1 dais : 7 574 * CPU2 -/ ccnf : 1 575 * CPU3 --- Codec2 576 * 577 * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec 578 * => 7 DAIs = 4xCPU + 3xCodec 579 * => 1 ccnf = 1xdummy-Codec 580 * 581 * ex2) 582 * CPU0 --- Codec0 link : 5 583 * CPU1 --- Codec1 dais : 6 584 * CPU2 -/ ccnf : 1 585 * CPU3 -/ 586 * 587 * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec 588 * => 6 DAIs = 4xCPU + 2xCodec 589 * => 1 ccnf = 1xdummy-Codec 590 * 591 * ex3) 592 * CPU0 --- Codec0 link : 6 593 * CPU1 -/ dais : 6 594 * CPU2 --- Codec1 ccnf : 2 595 * CPU3 -/ 596 * 597 * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec 598 * => 6 DAIs = 4xCPU + 2xCodec 599 * => 2 ccnf = 2xdummy-Codec 600 * 601 * ex4) 602 * CPU0 --- Codec0 (convert-rate) link : 3 603 * CPU1 --- Codec1 dais : 4 604 * ccnf : 1 605 * 606 * => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec 607 * => 4 DAIs = 2xCPU + 2xCodec 608 * => 1 ccnf = 1xdummy-Codec 609 */ 610 return graph_for_each_link(priv, li, 611 graph_count_noml, 612 graph_count_dpcm); 613 } 614 615 static int graph_probe(struct platform_device *pdev) 616 { 617 struct asoc_simple_priv *priv; 618 struct device *dev = &pdev->dev; 619 struct snd_soc_card *card; 620 621 /* Allocate the private data and the DAI link array */ 622 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 623 if (!priv) 624 return -ENOMEM; 625 626 card = simple_priv_to_card(priv); 627 card->dapm_widgets = graph_dapm_widgets; 628 card->num_dapm_widgets = ARRAY_SIZE(graph_dapm_widgets); 629 card->probe = asoc_graph_card_probe; 630 631 if (of_device_get_match_data(dev)) 632 priv->dpcm_selectable = 1; 633 634 return audio_graph_parse_of(priv, dev); 635 } 636 637 static const struct of_device_id graph_of_match[] = { 638 { .compatible = "audio-graph-card", }, 639 { .compatible = "audio-graph-scu-card", 640 .data = (void *)DPCM_SELECTABLE }, 641 {}, 642 }; 643 MODULE_DEVICE_TABLE(of, graph_of_match); 644 645 static struct platform_driver graph_card = { 646 .driver = { 647 .name = "asoc-audio-graph-card", 648 .pm = &snd_soc_pm_ops, 649 .of_match_table = graph_of_match, 650 }, 651 .probe = graph_probe, 652 .remove = asoc_simple_remove, 653 }; 654 module_platform_driver(graph_card); 655 656 MODULE_ALIAS("platform:asoc-audio-graph-card"); 657 MODULE_LICENSE("GPL v2"); 658 MODULE_DESCRIPTION("ASoC Audio Graph Sound Card"); 659 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); 660