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/simple_card_utils.h> 22 23 #define DPCM_SELECTABLE 1 24 25 #define PREFIX "audio-graph-card," 26 27 static int graph_outdrv_event(struct snd_soc_dapm_widget *w, 28 struct snd_kcontrol *kcontrol, 29 int event) 30 { 31 struct snd_soc_dapm_context *dapm = w->dapm; 32 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(dapm->card); 33 34 switch (event) { 35 case SND_SOC_DAPM_POST_PMU: 36 gpiod_set_value_cansleep(priv->pa_gpio, 1); 37 break; 38 case SND_SOC_DAPM_PRE_PMD: 39 gpiod_set_value_cansleep(priv->pa_gpio, 0); 40 break; 41 default: 42 return -EINVAL; 43 } 44 45 return 0; 46 } 47 48 static const struct snd_soc_dapm_widget graph_dapm_widgets[] = { 49 SND_SOC_DAPM_OUT_DRV_E("Amplifier", SND_SOC_NOPM, 50 0, 0, NULL, 0, graph_outdrv_event, 51 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 52 }; 53 54 static const struct snd_soc_ops graph_ops = { 55 .startup = asoc_simple_startup, 56 .shutdown = asoc_simple_shutdown, 57 .hw_params = asoc_simple_hw_params, 58 }; 59 60 static int graph_get_dai_id(struct device_node *ep) 61 { 62 struct device_node *node; 63 struct device_node *endpoint; 64 struct of_endpoint info; 65 int i, id; 66 int ret; 67 68 /* use driver specified DAI ID if exist */ 69 ret = snd_soc_get_dai_id(ep); 70 if (ret != -ENOTSUPP) 71 return ret; 72 73 /* use endpoint/port reg if exist */ 74 ret = of_graph_parse_endpoint(ep, &info); 75 if (ret == 0) { 76 /* 77 * Because it will count port/endpoint if it doesn't have "reg". 78 * But, we can't judge whether it has "no reg", or "reg = <0>" 79 * only of_graph_parse_endpoint(). 80 * We need to check "reg" property 81 */ 82 if (of_get_property(ep, "reg", NULL)) 83 return info.id; 84 85 node = of_get_parent(ep); 86 of_node_put(node); 87 if (of_get_property(node, "reg", NULL)) 88 return info.port; 89 } 90 node = of_graph_get_port_parent(ep); 91 92 /* 93 * Non HDMI sound case, counting port/endpoint on its DT 94 * is enough. Let's count it. 95 */ 96 i = 0; 97 id = -1; 98 for_each_endpoint_of_node(node, endpoint) { 99 if (endpoint == ep) 100 id = i; 101 i++; 102 } 103 104 of_node_put(node); 105 106 if (id < 0) 107 return -ENODEV; 108 109 return id; 110 } 111 112 static int asoc_simple_parse_dai(struct device_node *ep, 113 struct snd_soc_dai_link_component *dlc, 114 struct device_node **dai_of_node, 115 const char **dai_name, 116 int *is_single_link) 117 { 118 struct device_node *node; 119 struct of_phandle_args args; 120 int ret; 121 122 /* 123 * Use snd_soc_dai_link_component instead of legacy style. 124 * It is only for codec, but cpu will be supported in the future. 125 * see 126 * soc-core.c :: snd_soc_init_multicodec() 127 */ 128 if (dlc) { 129 dai_name = &dlc->dai_name; 130 dai_of_node = &dlc->of_node; 131 } 132 133 if (!ep) 134 return 0; 135 if (!dai_name) 136 return 0; 137 138 node = of_graph_get_port_parent(ep); 139 140 /* Get dai->name */ 141 args.np = node; 142 args.args[0] = graph_get_dai_id(ep); 143 args.args_count = (of_graph_get_endpoint_count(node) > 1); 144 145 ret = snd_soc_get_dai_name(&args, dai_name); 146 if (ret < 0) 147 return ret; 148 149 *dai_of_node = node; 150 151 if (is_single_link) 152 *is_single_link = of_graph_get_endpoint_count(node) == 1; 153 154 return 0; 155 } 156 157 static void graph_parse_convert(struct device *dev, 158 struct device_node *ep, 159 struct asoc_simple_data *adata) 160 { 161 struct device_node *top = dev->of_node; 162 struct device_node *port = of_get_parent(ep); 163 struct device_node *ports = of_get_parent(port); 164 struct device_node *node = of_graph_get_port_parent(ep); 165 166 asoc_simple_parse_convert(dev, top, NULL, adata); 167 asoc_simple_parse_convert(dev, node, PREFIX, adata); 168 asoc_simple_parse_convert(dev, ports, NULL, adata); 169 asoc_simple_parse_convert(dev, port, NULL, adata); 170 asoc_simple_parse_convert(dev, ep, NULL, adata); 171 172 of_node_put(port); 173 of_node_put(ports); 174 of_node_put(node); 175 } 176 177 static void graph_parse_mclk_fs(struct device_node *top, 178 struct device_node *ep, 179 struct simple_dai_props *props) 180 { 181 struct device_node *port = of_get_parent(ep); 182 struct device_node *ports = of_get_parent(port); 183 struct device_node *node = of_graph_get_port_parent(ep); 184 185 of_property_read_u32(top, "mclk-fs", &props->mclk_fs); 186 of_property_read_u32(ports, "mclk-fs", &props->mclk_fs); 187 of_property_read_u32(port, "mclk-fs", &props->mclk_fs); 188 of_property_read_u32(ep, "mclk-fs", &props->mclk_fs); 189 190 of_node_put(port); 191 of_node_put(ports); 192 of_node_put(node); 193 } 194 195 static int graph_dai_link_of_dpcm(struct asoc_simple_priv *priv, 196 struct device_node *cpu_ep, 197 struct device_node *codec_ep, 198 struct link_info *li, 199 int dup_codec) 200 { 201 struct device *dev = simple_priv_to_dev(priv); 202 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link); 203 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link); 204 struct device_node *top = dev->of_node; 205 struct device_node *ep = li->cpu ? cpu_ep : codec_ep; 206 struct device_node *port; 207 struct device_node *ports; 208 struct device_node *node; 209 struct asoc_simple_dai *dai; 210 struct snd_soc_dai_link_component *codecs = dai_link->codecs; 211 int ret; 212 213 /* Do it all CPU endpoint, and 1st Codec endpoint */ 214 if (!li->cpu && dup_codec) 215 return 0; 216 217 port = of_get_parent(ep); 218 ports = of_get_parent(port); 219 node = of_graph_get_port_parent(ep); 220 221 li->link++; 222 223 dev_dbg(dev, "link_of DPCM (%pOF)\n", ep); 224 225 of_node_put(ports); 226 of_node_put(port); 227 of_node_put(node); 228 229 if (li->cpu) { 230 int is_single_links = 0; 231 232 /* BE is dummy */ 233 codecs->of_node = NULL; 234 codecs->dai_name = "snd-soc-dummy-dai"; 235 codecs->name = "snd-soc-dummy"; 236 237 /* FE settings */ 238 dai_link->dynamic = 1; 239 dai_link->dpcm_merged_format = 1; 240 241 dai = 242 dai_props->cpu_dai = &priv->dais[li->dais++]; 243 244 ret = asoc_simple_parse_cpu(ep, dai_link, &is_single_links); 245 if (ret) 246 return ret; 247 248 ret = asoc_simple_parse_clk_cpu(dev, ep, dai_link, dai); 249 if (ret < 0) 250 return ret; 251 252 ret = asoc_simple_set_dailink_name(dev, dai_link, 253 "fe.%s", 254 dai_link->cpu_dai_name); 255 if (ret < 0) 256 return ret; 257 258 /* card->num_links includes Codec */ 259 asoc_simple_canonicalize_cpu(dai_link, is_single_links); 260 } else { 261 struct snd_soc_codec_conf *cconf; 262 263 /* FE is dummy */ 264 dai_link->cpu_of_node = NULL; 265 dai_link->cpu_dai_name = "snd-soc-dummy-dai"; 266 dai_link->cpu_name = "snd-soc-dummy"; 267 268 /* BE settings */ 269 dai_link->no_pcm = 1; 270 dai_link->be_hw_params_fixup = asoc_simple_be_hw_params_fixup; 271 272 dai = 273 dai_props->codec_dai = &priv->dais[li->dais++]; 274 275 cconf = 276 dai_props->codec_conf = &priv->codec_conf[li->conf++]; 277 278 ret = asoc_simple_parse_codec(ep, dai_link); 279 if (ret < 0) 280 return ret; 281 282 ret = asoc_simple_parse_clk_codec(dev, ep, dai_link, dai); 283 if (ret < 0) 284 return ret; 285 286 ret = asoc_simple_set_dailink_name(dev, dai_link, 287 "be.%s", 288 codecs->dai_name); 289 if (ret < 0) 290 return ret; 291 292 /* check "prefix" from top node */ 293 snd_soc_of_parse_node_prefix(top, cconf, codecs->of_node, 294 "prefix"); 295 snd_soc_of_parse_node_prefix(node, cconf, codecs->of_node, 296 PREFIX "prefix"); 297 snd_soc_of_parse_node_prefix(ports, cconf, codecs->of_node, 298 "prefix"); 299 snd_soc_of_parse_node_prefix(port, cconf, codecs->of_node, 300 "prefix"); 301 } 302 303 graph_parse_convert(dev, ep, &dai_props->adata); 304 graph_parse_mclk_fs(top, ep, dai_props); 305 306 asoc_simple_canonicalize_platform(dai_link); 307 308 ret = asoc_simple_parse_tdm(ep, dai); 309 if (ret) 310 return ret; 311 312 ret = asoc_simple_parse_daifmt(dev, cpu_ep, codec_ep, 313 NULL, &dai_link->dai_fmt); 314 if (ret < 0) 315 return ret; 316 317 dai_link->dpcm_playback = 1; 318 dai_link->dpcm_capture = 1; 319 dai_link->ops = &graph_ops; 320 dai_link->init = asoc_simple_dai_init; 321 322 return 0; 323 } 324 325 static int graph_dai_link_of(struct asoc_simple_priv *priv, 326 struct device_node *cpu_ep, 327 struct device_node *codec_ep, 328 struct link_info *li) 329 { 330 struct device *dev = simple_priv_to_dev(priv); 331 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link); 332 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link); 333 struct device_node *top = dev->of_node; 334 struct asoc_simple_dai *cpu_dai; 335 struct asoc_simple_dai *codec_dai; 336 int ret, single_cpu; 337 338 /* Do it only CPU turn */ 339 if (!li->cpu) 340 return 0; 341 342 dev_dbg(dev, "link_of (%pOF)\n", cpu_ep); 343 344 li->link++; 345 346 cpu_dai = 347 dai_props->cpu_dai = &priv->dais[li->dais++]; 348 codec_dai = 349 dai_props->codec_dai = &priv->dais[li->dais++]; 350 351 /* Factor to mclk, used in hw_params() */ 352 graph_parse_mclk_fs(top, cpu_ep, dai_props); 353 graph_parse_mclk_fs(top, codec_ep, dai_props); 354 355 ret = asoc_simple_parse_daifmt(dev, cpu_ep, codec_ep, 356 NULL, &dai_link->dai_fmt); 357 if (ret < 0) 358 return ret; 359 360 ret = asoc_simple_parse_cpu(cpu_ep, dai_link, &single_cpu); 361 if (ret < 0) 362 return ret; 363 364 ret = asoc_simple_parse_codec(codec_ep, dai_link); 365 if (ret < 0) 366 return ret; 367 368 ret = asoc_simple_parse_tdm(cpu_ep, cpu_dai); 369 if (ret < 0) 370 return ret; 371 372 ret = asoc_simple_parse_tdm(codec_ep, codec_dai); 373 if (ret < 0) 374 return ret; 375 376 ret = asoc_simple_parse_clk_cpu(dev, cpu_ep, dai_link, cpu_dai); 377 if (ret < 0) 378 return ret; 379 380 ret = asoc_simple_parse_clk_codec(dev, codec_ep, dai_link, codec_dai); 381 if (ret < 0) 382 return ret; 383 384 ret = asoc_simple_set_dailink_name(dev, dai_link, 385 "%s-%s", 386 dai_link->cpu_dai_name, 387 dai_link->codecs->dai_name); 388 if (ret < 0) 389 return ret; 390 391 dai_link->ops = &graph_ops; 392 dai_link->init = asoc_simple_dai_init; 393 394 asoc_simple_canonicalize_cpu(dai_link, single_cpu); 395 asoc_simple_canonicalize_platform(dai_link); 396 397 return 0; 398 } 399 400 static int graph_for_each_link(struct asoc_simple_priv *priv, 401 struct link_info *li, 402 int (*func_noml)(struct asoc_simple_priv *priv, 403 struct device_node *cpu_ep, 404 struct device_node *codec_ep, 405 struct link_info *li), 406 int (*func_dpcm)(struct asoc_simple_priv *priv, 407 struct device_node *cpu_ep, 408 struct device_node *codec_ep, 409 struct link_info *li, int dup_codec)) 410 { 411 struct of_phandle_iterator it; 412 struct device *dev = simple_priv_to_dev(priv); 413 struct device_node *node = dev->of_node; 414 struct device_node *cpu_port; 415 struct device_node *cpu_ep; 416 struct device_node *codec_ep; 417 struct device_node *codec_port; 418 struct device_node *codec_port_old = NULL; 419 struct asoc_simple_data adata; 420 uintptr_t dpcm_selectable = (uintptr_t)of_device_get_match_data(dev); 421 int rc, ret; 422 423 /* loop for all listed CPU port */ 424 of_for_each_phandle(&it, rc, node, "dais", NULL, 0) { 425 cpu_port = it.node; 426 cpu_ep = NULL; 427 428 /* loop for all CPU endpoint */ 429 while (1) { 430 cpu_ep = of_get_next_child(cpu_port, cpu_ep); 431 if (!cpu_ep) 432 break; 433 434 /* get codec */ 435 codec_ep = of_graph_get_remote_endpoint(cpu_ep); 436 codec_port = of_get_parent(codec_ep); 437 438 of_node_put(codec_ep); 439 of_node_put(codec_port); 440 441 /* get convert-xxx property */ 442 memset(&adata, 0, sizeof(adata)); 443 graph_parse_convert(dev, codec_ep, &adata); 444 graph_parse_convert(dev, cpu_ep, &adata); 445 446 /* 447 * It is DPCM 448 * if Codec port has many endpoints, 449 * or has convert-xxx property 450 */ 451 if (dpcm_selectable && 452 ((of_get_child_count(codec_port) > 1) || 453 adata.convert_rate || adata.convert_channels)) 454 ret = func_dpcm(priv, cpu_ep, codec_ep, li, 455 (codec_port_old == codec_port)); 456 /* else normal sound */ 457 else 458 ret = func_noml(priv, cpu_ep, codec_ep, li); 459 460 if (ret < 0) 461 return ret; 462 463 codec_port_old = codec_port; 464 } 465 } 466 467 return 0; 468 } 469 470 static int graph_parse_of(struct asoc_simple_priv *priv) 471 { 472 struct snd_soc_card *card = simple_priv_to_card(priv); 473 struct link_info li; 474 int ret; 475 476 ret = asoc_simple_parse_widgets(card, NULL); 477 if (ret < 0) 478 return ret; 479 480 ret = asoc_simple_parse_routing(card, NULL); 481 if (ret < 0) 482 return ret; 483 484 memset(&li, 0, sizeof(li)); 485 for (li.cpu = 1; li.cpu >= 0; li.cpu--) { 486 /* 487 * Detect all CPU first, and Detect all Codec 2nd. 488 * 489 * In Normal sound case, all DAIs are detected 490 * as "CPU-Codec". 491 * 492 * In DPCM sound case, 493 * all CPUs are detected as "CPU-dummy", and 494 * all Codecs are detected as "dummy-Codec". 495 * To avoid random sub-device numbering, 496 * detect "dummy-Codec" in last; 497 */ 498 ret = graph_for_each_link(priv, &li, 499 graph_dai_link_of, 500 graph_dai_link_of_dpcm); 501 if (ret < 0) 502 return ret; 503 } 504 505 return asoc_simple_parse_card_name(card, NULL); 506 } 507 508 static int graph_count_noml(struct asoc_simple_priv *priv, 509 struct device_node *cpu_ep, 510 struct device_node *codec_ep, 511 struct link_info *li) 512 { 513 struct device *dev = simple_priv_to_dev(priv); 514 515 li->link += 1; /* 1xCPU-Codec */ 516 li->dais += 2; /* 1xCPU + 1xCodec */ 517 518 dev_dbg(dev, "Count As Normal\n"); 519 520 return 0; 521 } 522 523 static int graph_count_dpcm(struct asoc_simple_priv *priv, 524 struct device_node *cpu_ep, 525 struct device_node *codec_ep, 526 struct link_info *li, 527 int dup_codec) 528 { 529 struct device *dev = simple_priv_to_dev(priv); 530 531 li->link++; /* 1xCPU-dummy */ 532 li->dais++; /* 1xCPU */ 533 534 if (!dup_codec) { 535 li->link++; /* 1xdummy-Codec */ 536 li->conf++; /* 1xdummy-Codec */ 537 li->dais++; /* 1xCodec */ 538 } 539 540 dev_dbg(dev, "Count As DPCM\n"); 541 542 return 0; 543 } 544 545 static void graph_get_dais_count(struct asoc_simple_priv *priv, 546 struct link_info *li) 547 { 548 struct device *dev = simple_priv_to_dev(priv); 549 550 /* 551 * link_num : number of links. 552 * CPU-Codec / CPU-dummy / dummy-Codec 553 * dais_num : number of DAIs 554 * ccnf_num : number of codec_conf 555 * same number for "dummy-Codec" 556 * 557 * ex1) 558 * CPU0 --- Codec0 link : 5 559 * CPU1 --- Codec1 dais : 7 560 * CPU2 -/ ccnf : 1 561 * CPU3 --- Codec2 562 * 563 * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec 564 * => 7 DAIs = 4xCPU + 3xCodec 565 * => 1 ccnf = 1xdummy-Codec 566 * 567 * ex2) 568 * CPU0 --- Codec0 link : 5 569 * CPU1 --- Codec1 dais : 6 570 * CPU2 -/ ccnf : 1 571 * CPU3 -/ 572 * 573 * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec 574 * => 6 DAIs = 4xCPU + 2xCodec 575 * => 1 ccnf = 1xdummy-Codec 576 * 577 * ex3) 578 * CPU0 --- Codec0 link : 6 579 * CPU1 -/ dais : 6 580 * CPU2 --- Codec1 ccnf : 2 581 * CPU3 -/ 582 * 583 * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec 584 * => 6 DAIs = 4xCPU + 2xCodec 585 * => 2 ccnf = 2xdummy-Codec 586 * 587 * ex4) 588 * CPU0 --- Codec0 (convert-rate) link : 3 589 * CPU1 --- Codec1 dais : 4 590 * ccnf : 1 591 * 592 * => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec 593 * => 4 DAIs = 2xCPU + 2xCodec 594 * => 1 ccnf = 1xdummy-Codec 595 */ 596 graph_for_each_link(priv, li, 597 graph_count_noml, 598 graph_count_dpcm); 599 dev_dbg(dev, "link %d, dais %d, ccnf %d\n", 600 li->link, li->dais, li->conf); 601 } 602 603 static int graph_card_probe(struct snd_soc_card *card) 604 { 605 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(card); 606 int ret; 607 608 ret = asoc_simple_init_hp(card, &priv->hp_jack, NULL); 609 if (ret < 0) 610 return ret; 611 612 ret = asoc_simple_init_mic(card, &priv->mic_jack, NULL); 613 if (ret < 0) 614 return ret; 615 616 return 0; 617 } 618 619 static int graph_probe(struct platform_device *pdev) 620 { 621 struct asoc_simple_priv *priv; 622 struct device *dev = &pdev->dev; 623 struct snd_soc_card *card; 624 struct link_info li; 625 int ret; 626 627 /* Allocate the private data and the DAI link array */ 628 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 629 if (!priv) 630 return -ENOMEM; 631 632 card = simple_priv_to_card(priv); 633 card->owner = THIS_MODULE; 634 card->dev = dev; 635 card->dapm_widgets = graph_dapm_widgets; 636 card->num_dapm_widgets = ARRAY_SIZE(graph_dapm_widgets); 637 card->probe = graph_card_probe; 638 639 memset(&li, 0, sizeof(li)); 640 graph_get_dais_count(priv, &li); 641 if (!li.link || !li.dais) 642 return -EINVAL; 643 644 ret = asoc_simple_init_priv(priv, &li); 645 if (ret < 0) 646 return ret; 647 648 priv->pa_gpio = devm_gpiod_get_optional(dev, "pa", GPIOD_OUT_LOW); 649 if (IS_ERR(priv->pa_gpio)) { 650 ret = PTR_ERR(priv->pa_gpio); 651 dev_err(dev, "failed to get amplifier gpio: %d\n", ret); 652 return ret; 653 } 654 655 ret = graph_parse_of(priv); 656 if (ret < 0) { 657 if (ret != -EPROBE_DEFER) 658 dev_err(dev, "parse error %d\n", ret); 659 goto err; 660 } 661 662 snd_soc_card_set_drvdata(card, priv); 663 664 asoc_simple_debug_info(priv); 665 666 ret = devm_snd_soc_register_card(dev, card); 667 if (ret < 0) 668 goto err; 669 670 return 0; 671 err: 672 asoc_simple_clean_reference(card); 673 674 return ret; 675 } 676 677 static int graph_remove(struct platform_device *pdev) 678 { 679 struct snd_soc_card *card = platform_get_drvdata(pdev); 680 681 return asoc_simple_clean_reference(card); 682 } 683 684 static const struct of_device_id graph_of_match[] = { 685 { .compatible = "audio-graph-card", }, 686 { .compatible = "audio-graph-scu-card", 687 .data = (void *)DPCM_SELECTABLE }, 688 {}, 689 }; 690 MODULE_DEVICE_TABLE(of, graph_of_match); 691 692 static struct platform_driver graph_card = { 693 .driver = { 694 .name = "asoc-audio-graph-card", 695 .pm = &snd_soc_pm_ops, 696 .of_match_table = graph_of_match, 697 }, 698 .probe = graph_probe, 699 .remove = graph_remove, 700 }; 701 module_platform_driver(graph_card); 702 703 MODULE_ALIAS("platform:asoc-audio-graph-card"); 704 MODULE_LICENSE("GPL v2"); 705 MODULE_DESCRIPTION("ASoC Audio Graph Sound Card"); 706 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); 707