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