1// SPDX-License-Identifier: GPL-2.0+ 2// 3// soc-core.c -- ALSA SoC Audio Layer 4// 5// Copyright 2005 Wolfson Microelectronics PLC. 6// Copyright 2005 Openedhand Ltd. 7// Copyright (C) 2010 Slimlogic Ltd. 8// Copyright (C) 2010 Texas Instruments Inc. 9// 10// Author: Liam Girdwood <lrg@slimlogic.co.uk> 11// with code, comments and ideas from :- 12// Richard Purdie <richard@openedhand.com> 13// 14// TODO: 15// o Add hw rules to enforce rates, etc. 16// o More testing with other codecs/machines. 17// o Add more codecs and platforms to ensure good API coverage. 18// o Support TDM on PCM and I2S 19 20#include <linux/module.h> 21#include <linux/moduleparam.h> 22#include <linux/init.h> 23#include <linux/delay.h> 24#include <linux/pm.h> 25#include <linux/bitops.h> 26#include <linux/debugfs.h> 27#include <linux/platform_device.h> 28#include <linux/pinctrl/consumer.h> 29#include <linux/ctype.h> 30#include <linux/slab.h> 31#include <linux/of.h> 32#include <linux/of_graph.h> 33#include <linux/dmi.h> 34#include <linux/acpi.h> 35#include <sound/core.h> 36#include <sound/jack.h> 37#include <sound/pcm.h> 38#include <sound/pcm_params.h> 39#include <sound/soc.h> 40#include <sound/soc-dpcm.h> 41#include <sound/soc-topology.h> 42#include <sound/soc-link.h> 43#include <sound/initval.h> 44 45#define CREATE_TRACE_POINTS 46#include <trace/events/asoc.h> 47 48static DEFINE_MUTEX(client_mutex); 49static LIST_HEAD(component_list); 50static LIST_HEAD(unbind_card_list); 51 52#define for_each_component(component) \ 53 list_for_each_entry(component, &component_list, list) 54 55/* 56 * This is used if driver don't need to have CPU/Codec/Platform 57 * dai_link. see soc.h 58 */ 59struct snd_soc_dai_link_component null_dailink_component[0]; 60EXPORT_SYMBOL_GPL(null_dailink_component); 61 62/* 63 * This is a timeout to do a DAPM powerdown after a stream is closed(). 64 * It can be used to eliminate pops between different playback streams, e.g. 65 * between two audio tracks. 66 */ 67static int pmdown_time = 5000; 68module_param(pmdown_time, int, 0); 69MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)"); 70 71static ssize_t pmdown_time_show(struct device *dev, 72 struct device_attribute *attr, char *buf) 73{ 74 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 75 76 return sprintf(buf, "%ld\n", rtd->pmdown_time); 77} 78 79static ssize_t pmdown_time_set(struct device *dev, 80 struct device_attribute *attr, 81 const char *buf, size_t count) 82{ 83 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 84 int ret; 85 86 ret = kstrtol(buf, 10, &rtd->pmdown_time); 87 if (ret) 88 return ret; 89 90 return count; 91} 92 93static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set); 94 95static struct attribute *soc_dev_attrs[] = { 96 &dev_attr_pmdown_time.attr, 97 NULL 98}; 99 100static umode_t soc_dev_attr_is_visible(struct kobject *kobj, 101 struct attribute *attr, int idx) 102{ 103 struct device *dev = kobj_to_dev(kobj); 104 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 105 106 if (!rtd) 107 return 0; 108 109 if (attr == &dev_attr_pmdown_time.attr) 110 return attr->mode; /* always visible */ 111 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */ 112} 113 114static const struct attribute_group soc_dapm_dev_group = { 115 .attrs = soc_dapm_dev_attrs, 116 .is_visible = soc_dev_attr_is_visible, 117}; 118 119static const struct attribute_group soc_dev_group = { 120 .attrs = soc_dev_attrs, 121 .is_visible = soc_dev_attr_is_visible, 122}; 123 124static const struct attribute_group *soc_dev_attr_groups[] = { 125 &soc_dapm_dev_group, 126 &soc_dev_group, 127 NULL 128}; 129 130#ifdef CONFIG_DEBUG_FS 131struct dentry *snd_soc_debugfs_root; 132EXPORT_SYMBOL_GPL(snd_soc_debugfs_root); 133 134static void soc_init_component_debugfs(struct snd_soc_component *component) 135{ 136 if (!component->card->debugfs_card_root) 137 return; 138 139 if (component->debugfs_prefix) { 140 char *name; 141 142 name = kasprintf(GFP_KERNEL, "%s:%s", 143 component->debugfs_prefix, component->name); 144 if (name) { 145 component->debugfs_root = debugfs_create_dir(name, 146 component->card->debugfs_card_root); 147 kfree(name); 148 } 149 } else { 150 component->debugfs_root = debugfs_create_dir(component->name, 151 component->card->debugfs_card_root); 152 } 153 154 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component), 155 component->debugfs_root); 156} 157 158static void soc_cleanup_component_debugfs(struct snd_soc_component *component) 159{ 160 if (!component->debugfs_root) 161 return; 162 debugfs_remove_recursive(component->debugfs_root); 163 component->debugfs_root = NULL; 164} 165 166static int dai_list_show(struct seq_file *m, void *v) 167{ 168 struct snd_soc_component *component; 169 struct snd_soc_dai *dai; 170 171 mutex_lock(&client_mutex); 172 173 for_each_component(component) 174 for_each_component_dais(component, dai) 175 seq_printf(m, "%s\n", dai->name); 176 177 mutex_unlock(&client_mutex); 178 179 return 0; 180} 181DEFINE_SHOW_ATTRIBUTE(dai_list); 182 183static int component_list_show(struct seq_file *m, void *v) 184{ 185 struct snd_soc_component *component; 186 187 mutex_lock(&client_mutex); 188 189 for_each_component(component) 190 seq_printf(m, "%s\n", component->name); 191 192 mutex_unlock(&client_mutex); 193 194 return 0; 195} 196DEFINE_SHOW_ATTRIBUTE(component_list); 197 198static void soc_init_card_debugfs(struct snd_soc_card *card) 199{ 200 card->debugfs_card_root = debugfs_create_dir(card->name, 201 snd_soc_debugfs_root); 202 203 debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root, 204 &card->pop_time); 205 206 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root); 207} 208 209static void soc_cleanup_card_debugfs(struct snd_soc_card *card) 210{ 211 debugfs_remove_recursive(card->debugfs_card_root); 212 card->debugfs_card_root = NULL; 213} 214 215static void snd_soc_debugfs_init(void) 216{ 217 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL); 218 219 debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL, 220 &dai_list_fops); 221 222 debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL, 223 &component_list_fops); 224} 225 226static void snd_soc_debugfs_exit(void) 227{ 228 debugfs_remove_recursive(snd_soc_debugfs_root); 229} 230 231#else 232 233static inline void soc_init_component_debugfs( 234 struct snd_soc_component *component) 235{ 236} 237 238static inline void soc_cleanup_component_debugfs( 239 struct snd_soc_component *component) 240{ 241} 242 243static inline void soc_init_card_debugfs(struct snd_soc_card *card) 244{ 245} 246 247static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card) 248{ 249} 250 251static inline void snd_soc_debugfs_init(void) 252{ 253} 254 255static inline void snd_soc_debugfs_exit(void) 256{ 257} 258 259#endif 260 261static int snd_soc_rtd_add_component(struct snd_soc_pcm_runtime *rtd, 262 struct snd_soc_component *component) 263{ 264 struct snd_soc_component *comp; 265 int i; 266 267 for_each_rtd_components(rtd, i, comp) { 268 /* already connected */ 269 if (comp == component) 270 return 0; 271 } 272 273 /* see for_each_rtd_components */ 274 rtd->components[rtd->num_components] = component; 275 rtd->num_components++; 276 277 return 0; 278} 279 280struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd, 281 const char *driver_name) 282{ 283 struct snd_soc_component *component; 284 int i; 285 286 if (!driver_name) 287 return NULL; 288 289 /* 290 * NOTE 291 * 292 * snd_soc_rtdcom_lookup() will find component from rtd by using 293 * specified driver name. 294 * But, if many components which have same driver name are connected 295 * to 1 rtd, this function will return 1st found component. 296 */ 297 for_each_rtd_components(rtd, i, component) { 298 const char *component_name = component->driver->name; 299 300 if (!component_name) 301 continue; 302 303 if ((component_name == driver_name) || 304 strcmp(component_name, driver_name) == 0) 305 return component; 306 } 307 308 return NULL; 309} 310EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup); 311 312struct snd_soc_component 313*snd_soc_lookup_component_nolocked(struct device *dev, const char *driver_name) 314{ 315 struct snd_soc_component *component; 316 struct snd_soc_component *found_component; 317 318 found_component = NULL; 319 for_each_component(component) { 320 if ((dev == component->dev) && 321 (!driver_name || 322 (driver_name == component->driver->name) || 323 (strcmp(component->driver->name, driver_name) == 0))) { 324 found_component = component; 325 break; 326 } 327 } 328 329 return found_component; 330} 331EXPORT_SYMBOL_GPL(snd_soc_lookup_component_nolocked); 332 333struct snd_soc_component *snd_soc_lookup_component(struct device *dev, 334 const char *driver_name) 335{ 336 struct snd_soc_component *component; 337 338 mutex_lock(&client_mutex); 339 component = snd_soc_lookup_component_nolocked(dev, driver_name); 340 mutex_unlock(&client_mutex); 341 342 return component; 343} 344EXPORT_SYMBOL_GPL(snd_soc_lookup_component); 345 346struct snd_soc_pcm_runtime 347*snd_soc_get_pcm_runtime(struct snd_soc_card *card, 348 struct snd_soc_dai_link *dai_link) 349{ 350 struct snd_soc_pcm_runtime *rtd; 351 352 for_each_card_rtds(card, rtd) { 353 if (rtd->dai_link == dai_link) 354 return rtd; 355 } 356 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link->name); 357 return NULL; 358} 359EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime); 360 361/* 362 * Power down the audio subsystem pmdown_time msecs after close is called. 363 * This is to ensure there are no pops or clicks in between any music tracks 364 * due to DAPM power cycling. 365 */ 366void snd_soc_close_delayed_work(struct snd_soc_pcm_runtime *rtd) 367{ 368 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0); 369 int playback = SNDRV_PCM_STREAM_PLAYBACK; 370 371 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 372 373 dev_dbg(rtd->dev, 374 "ASoC: pop wq checking: %s status: %s waiting: %s\n", 375 codec_dai->driver->playback.stream_name, 376 snd_soc_dai_stream_active(codec_dai, playback) ? 377 "active" : "inactive", 378 rtd->pop_wait ? "yes" : "no"); 379 380 /* are we waiting on this codec DAI stream */ 381 if (rtd->pop_wait == 1) { 382 rtd->pop_wait = 0; 383 snd_soc_dapm_stream_event(rtd, playback, 384 SND_SOC_DAPM_STREAM_STOP); 385 } 386 387 mutex_unlock(&rtd->card->pcm_mutex); 388} 389EXPORT_SYMBOL_GPL(snd_soc_close_delayed_work); 390 391static void soc_release_rtd_dev(struct device *dev) 392{ 393 /* "dev" means "rtd->dev" */ 394 kfree(dev); 395} 396 397static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd) 398{ 399 if (!rtd) 400 return; 401 402 list_del(&rtd->list); 403 404 if (delayed_work_pending(&rtd->delayed_work)) 405 flush_delayed_work(&rtd->delayed_work); 406 snd_soc_pcm_component_free(rtd); 407 408 /* 409 * we don't need to call kfree() for rtd->dev 410 * see 411 * soc_release_rtd_dev() 412 * 413 * We don't need rtd->dev NULL check, because 414 * it is alloced *before* rtd. 415 * see 416 * soc_new_pcm_runtime() 417 */ 418 device_unregister(rtd->dev); 419} 420 421static void close_delayed_work(struct work_struct *work) { 422 struct snd_soc_pcm_runtime *rtd = 423 container_of(work, struct snd_soc_pcm_runtime, 424 delayed_work.work); 425 426 if (rtd->close_delayed_work_func) 427 rtd->close_delayed_work_func(rtd); 428} 429 430static struct snd_soc_pcm_runtime *soc_new_pcm_runtime( 431 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link) 432{ 433 struct snd_soc_pcm_runtime *rtd; 434 struct snd_soc_component *component; 435 struct device *dev; 436 int ret; 437 int stream; 438 439 /* 440 * for rtd->dev 441 */ 442 dev = kzalloc(sizeof(struct device), GFP_KERNEL); 443 if (!dev) 444 return NULL; 445 446 dev->parent = card->dev; 447 dev->release = soc_release_rtd_dev; 448 449 dev_set_name(dev, "%s", dai_link->name); 450 451 ret = device_register(dev); 452 if (ret < 0) { 453 put_device(dev); /* soc_release_rtd_dev */ 454 return NULL; 455 } 456 457 /* 458 * for rtd 459 */ 460 rtd = devm_kzalloc(dev, 461 sizeof(*rtd) + 462 sizeof(*component) * (dai_link->num_cpus + 463 dai_link->num_codecs + 464 dai_link->num_platforms), 465 GFP_KERNEL); 466 if (!rtd) 467 goto free_rtd; 468 469 rtd->dev = dev; 470 INIT_LIST_HEAD(&rtd->list); 471 for_each_pcm_streams(stream) { 472 INIT_LIST_HEAD(&rtd->dpcm[stream].be_clients); 473 INIT_LIST_HEAD(&rtd->dpcm[stream].fe_clients); 474 } 475 dev_set_drvdata(dev, rtd); 476 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work); 477 478 /* 479 * for rtd->dais 480 */ 481 rtd->dais = devm_kcalloc(dev, dai_link->num_cpus + dai_link->num_codecs, 482 sizeof(struct snd_soc_dai *), 483 GFP_KERNEL); 484 if (!rtd->dais) 485 goto free_rtd; 486 487 /* 488 * dais = [][][][][][][][][][][][][][][][][][] 489 * ^cpu_dais ^codec_dais 490 * |--- num_cpus ---|--- num_codecs --| 491 * see 492 * asoc_rtd_to_cpu() 493 * asoc_rtd_to_codec() 494 */ 495 rtd->num_cpus = dai_link->num_cpus; 496 rtd->num_codecs = dai_link->num_codecs; 497 rtd->card = card; 498 rtd->dai_link = dai_link; 499 rtd->num = card->num_rtd++; 500 501 /* see for_each_card_rtds */ 502 list_add_tail(&rtd->list, &card->rtd_list); 503 504 ret = device_add_groups(dev, soc_dev_attr_groups); 505 if (ret < 0) 506 goto free_rtd; 507 508 return rtd; 509 510free_rtd: 511 soc_free_pcm_runtime(rtd); 512 return NULL; 513} 514 515static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card) 516{ 517 struct snd_soc_pcm_runtime *rtd; 518 519 for_each_card_rtds(card, rtd) 520 flush_delayed_work(&rtd->delayed_work); 521} 522 523#ifdef CONFIG_PM_SLEEP 524/* powers down audio subsystem for suspend */ 525int snd_soc_suspend(struct device *dev) 526{ 527 struct snd_soc_card *card = dev_get_drvdata(dev); 528 struct snd_soc_component *component; 529 struct snd_soc_pcm_runtime *rtd; 530 int playback = SNDRV_PCM_STREAM_PLAYBACK; 531 int i; 532 533 /* If the card is not initialized yet there is nothing to do */ 534 if (!card->instantiated) 535 return 0; 536 537 /* 538 * Due to the resume being scheduled into a workqueue we could 539 * suspend before that's finished - wait for it to complete. 540 */ 541 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0); 542 543 /* we're going to block userspace touching us until resume completes */ 544 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot); 545 546 /* mute any active DACs */ 547 for_each_card_rtds(card, rtd) { 548 struct snd_soc_dai *dai; 549 550 if (rtd->dai_link->ignore_suspend) 551 continue; 552 553 for_each_rtd_dais(rtd, i, dai) { 554 if (snd_soc_dai_stream_active(dai, playback)) 555 snd_soc_dai_digital_mute(dai, 1, playback); 556 } 557 } 558 559 /* suspend all pcms */ 560 for_each_card_rtds(card, rtd) { 561 if (rtd->dai_link->ignore_suspend) 562 continue; 563 564 snd_pcm_suspend_all(rtd->pcm); 565 } 566 567 snd_soc_card_suspend_pre(card); 568 569 /* close any waiting streams */ 570 snd_soc_flush_all_delayed_work(card); 571 572 for_each_card_rtds(card, rtd) { 573 int stream; 574 575 if (rtd->dai_link->ignore_suspend) 576 continue; 577 578 for_each_pcm_streams(stream) 579 snd_soc_dapm_stream_event(rtd, stream, 580 SND_SOC_DAPM_STREAM_SUSPEND); 581 } 582 583 /* Recheck all endpoints too, their state is affected by suspend */ 584 dapm_mark_endpoints_dirty(card); 585 snd_soc_dapm_sync(&card->dapm); 586 587 /* suspend all COMPONENTs */ 588 for_each_card_rtds(card, rtd) { 589 590 if (rtd->dai_link->ignore_suspend) 591 continue; 592 593 for_each_rtd_components(rtd, i, component) { 594 struct snd_soc_dapm_context *dapm = 595 snd_soc_component_get_dapm(component); 596 597 /* 598 * ignore if component was already suspended 599 */ 600 if (snd_soc_component_is_suspended(component)) 601 continue; 602 603 /* 604 * If there are paths active then the COMPONENT will be 605 * held with bias _ON and should not be suspended. 606 */ 607 switch (snd_soc_dapm_get_bias_level(dapm)) { 608 case SND_SOC_BIAS_STANDBY: 609 /* 610 * If the COMPONENT is capable of idle 611 * bias off then being in STANDBY 612 * means it's doing something, 613 * otherwise fall through. 614 */ 615 if (dapm->idle_bias_off) { 616 dev_dbg(component->dev, 617 "ASoC: idle_bias_off CODEC on over suspend\n"); 618 break; 619 } 620 fallthrough; 621 622 case SND_SOC_BIAS_OFF: 623 snd_soc_component_suspend(component); 624 if (component->regmap) 625 regcache_mark_dirty(component->regmap); 626 /* deactivate pins to sleep state */ 627 pinctrl_pm_select_sleep_state(component->dev); 628 break; 629 default: 630 dev_dbg(component->dev, 631 "ASoC: COMPONENT is on over suspend\n"); 632 break; 633 } 634 } 635 } 636 637 snd_soc_card_suspend_post(card); 638 639 return 0; 640} 641EXPORT_SYMBOL_GPL(snd_soc_suspend); 642 643/* 644 * deferred resume work, so resume can complete before we finished 645 * setting our codec back up, which can be very slow on I2C 646 */ 647static void soc_resume_deferred(struct work_struct *work) 648{ 649 struct snd_soc_card *card = 650 container_of(work, struct snd_soc_card, 651 deferred_resume_work); 652 struct snd_soc_pcm_runtime *rtd; 653 struct snd_soc_component *component; 654 int i; 655 656 /* 657 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time, 658 * so userspace apps are blocked from touching us 659 */ 660 661 dev_dbg(card->dev, "ASoC: starting resume work\n"); 662 663 /* Bring us up into D2 so that DAPM starts enabling things */ 664 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2); 665 666 snd_soc_card_resume_pre(card); 667 668 for_each_card_components(card, component) { 669 if (snd_soc_component_is_suspended(component)) 670 snd_soc_component_resume(component); 671 } 672 673 for_each_card_rtds(card, rtd) { 674 int stream; 675 676 if (rtd->dai_link->ignore_suspend) 677 continue; 678 679 for_each_pcm_streams(stream) 680 snd_soc_dapm_stream_event(rtd, stream, 681 SND_SOC_DAPM_STREAM_RESUME); 682 } 683 684 /* unmute any active DACs */ 685 for_each_card_rtds(card, rtd) { 686 struct snd_soc_dai *dai; 687 int playback = SNDRV_PCM_STREAM_PLAYBACK; 688 689 if (rtd->dai_link->ignore_suspend) 690 continue; 691 692 for_each_rtd_dais(rtd, i, dai) { 693 if (snd_soc_dai_stream_active(dai, playback)) 694 snd_soc_dai_digital_mute(dai, 0, playback); 695 } 696 } 697 698 snd_soc_card_resume_post(card); 699 700 dev_dbg(card->dev, "ASoC: resume work completed\n"); 701 702 /* Recheck all endpoints too, their state is affected by suspend */ 703 dapm_mark_endpoints_dirty(card); 704 snd_soc_dapm_sync(&card->dapm); 705 706 /* userspace can access us now we are back as we were before */ 707 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0); 708} 709 710/* powers up audio subsystem after a suspend */ 711int snd_soc_resume(struct device *dev) 712{ 713 struct snd_soc_card *card = dev_get_drvdata(dev); 714 struct snd_soc_component *component; 715 716 /* If the card is not initialized yet there is nothing to do */ 717 if (!card->instantiated) 718 return 0; 719 720 /* activate pins from sleep state */ 721 for_each_card_components(card, component) 722 if (snd_soc_component_active(component)) 723 pinctrl_pm_select_default_state(component->dev); 724 725 dev_dbg(dev, "ASoC: Scheduling resume work\n"); 726 if (!schedule_work(&card->deferred_resume_work)) 727 dev_err(dev, "ASoC: resume work item may be lost\n"); 728 729 return 0; 730} 731EXPORT_SYMBOL_GPL(snd_soc_resume); 732 733static void soc_resume_init(struct snd_soc_card *card) 734{ 735 /* deferred resume work */ 736 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred); 737} 738#else 739#define snd_soc_suspend NULL 740#define snd_soc_resume NULL 741static inline void soc_resume_init(struct snd_soc_card *card) 742{ 743} 744#endif 745 746static struct device_node 747*soc_component_to_node(struct snd_soc_component *component) 748{ 749 struct device_node *of_node; 750 751 of_node = component->dev->of_node; 752 if (!of_node && component->dev->parent) 753 of_node = component->dev->parent->of_node; 754 755 return of_node; 756} 757 758static int snd_soc_is_matching_component( 759 const struct snd_soc_dai_link_component *dlc, 760 struct snd_soc_component *component) 761{ 762 struct device_node *component_of_node; 763 764 if (!dlc) 765 return 0; 766 767 component_of_node = soc_component_to_node(component); 768 769 if (dlc->of_node && component_of_node != dlc->of_node) 770 return 0; 771 if (dlc->name && strcmp(component->name, dlc->name)) 772 return 0; 773 774 return 1; 775} 776 777static struct snd_soc_component *soc_find_component( 778 const struct snd_soc_dai_link_component *dlc) 779{ 780 struct snd_soc_component *component; 781 782 lockdep_assert_held(&client_mutex); 783 784 /* 785 * NOTE 786 * 787 * It returns *1st* found component, but some driver 788 * has few components by same of_node/name 789 * ex) 790 * CPU component and generic DMAEngine component 791 */ 792 for_each_component(component) 793 if (snd_soc_is_matching_component(dlc, component)) 794 return component; 795 796 return NULL; 797} 798 799/** 800 * snd_soc_find_dai - Find a registered DAI 801 * 802 * @dlc: name of the DAI or the DAI driver and optional component info to match 803 * 804 * This function will search all registered components and their DAIs to 805 * find the DAI of the same name. The component's of_node and name 806 * should also match if being specified. 807 * 808 * Return: pointer of DAI, or NULL if not found. 809 */ 810struct snd_soc_dai *snd_soc_find_dai( 811 const struct snd_soc_dai_link_component *dlc) 812{ 813 struct snd_soc_component *component; 814 struct snd_soc_dai *dai; 815 816 lockdep_assert_held(&client_mutex); 817 818 /* Find CPU DAI from registered DAIs */ 819 for_each_component(component) { 820 if (!snd_soc_is_matching_component(dlc, component)) 821 continue; 822 for_each_component_dais(component, dai) { 823 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name) 824 && (!dai->driver->name 825 || strcmp(dai->driver->name, dlc->dai_name))) 826 continue; 827 828 return dai; 829 } 830 } 831 832 return NULL; 833} 834EXPORT_SYMBOL_GPL(snd_soc_find_dai); 835 836struct snd_soc_dai *snd_soc_find_dai_with_mutex( 837 const struct snd_soc_dai_link_component *dlc) 838{ 839 struct snd_soc_dai *dai; 840 841 mutex_lock(&client_mutex); 842 dai = snd_soc_find_dai(dlc); 843 mutex_unlock(&client_mutex); 844 845 return dai; 846} 847EXPORT_SYMBOL_GPL(snd_soc_find_dai_with_mutex); 848 849static int soc_dai_link_sanity_check(struct snd_soc_card *card, 850 struct snd_soc_dai_link *link) 851{ 852 int i; 853 struct snd_soc_dai_link_component *cpu, *codec, *platform; 854 855 for_each_link_codecs(link, i, codec) { 856 /* 857 * Codec must be specified by 1 of name or OF node, 858 * not both or neither. 859 */ 860 if (!!codec->name == !!codec->of_node) { 861 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n", 862 link->name); 863 return -EINVAL; 864 } 865 866 /* Codec DAI name must be specified */ 867 if (!codec->dai_name) { 868 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n", 869 link->name); 870 return -EINVAL; 871 } 872 873 /* 874 * Defer card registration if codec component is not added to 875 * component list. 876 */ 877 if (!soc_find_component(codec)) { 878 dev_dbg(card->dev, 879 "ASoC: codec component %s not found for link %s\n", 880 codec->name, link->name); 881 return -EPROBE_DEFER; 882 } 883 } 884 885 for_each_link_platforms(link, i, platform) { 886 /* 887 * Platform may be specified by either name or OF node, but it 888 * can be left unspecified, then no components will be inserted 889 * in the rtdcom list 890 */ 891 if (!!platform->name == !!platform->of_node) { 892 dev_err(card->dev, 893 "ASoC: Neither/both platform name/of_node are set for %s\n", 894 link->name); 895 return -EINVAL; 896 } 897 898 /* 899 * Defer card registration if platform component is not added to 900 * component list. 901 */ 902 if (!soc_find_component(platform)) { 903 dev_dbg(card->dev, 904 "ASoC: platform component %s not found for link %s\n", 905 platform->name, link->name); 906 return -EPROBE_DEFER; 907 } 908 } 909 910 for_each_link_cpus(link, i, cpu) { 911 /* 912 * CPU device may be specified by either name or OF node, but 913 * can be left unspecified, and will be matched based on DAI 914 * name alone.. 915 */ 916 if (cpu->name && cpu->of_node) { 917 dev_err(card->dev, 918 "ASoC: Neither/both cpu name/of_node are set for %s\n", 919 link->name); 920 return -EINVAL; 921 } 922 923 /* 924 * Defer card registration if cpu dai component is not added to 925 * component list. 926 */ 927 if ((cpu->of_node || cpu->name) && 928 !soc_find_component(cpu)) { 929 dev_dbg(card->dev, 930 "ASoC: cpu component %s not found for link %s\n", 931 cpu->name, link->name); 932 return -EPROBE_DEFER; 933 } 934 935 /* 936 * At least one of CPU DAI name or CPU device name/node must be 937 * specified 938 */ 939 if (!cpu->dai_name && 940 !(cpu->name || cpu->of_node)) { 941 dev_err(card->dev, 942 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n", 943 link->name); 944 return -EINVAL; 945 } 946 } 947 948 return 0; 949} 950 951/** 952 * snd_soc_remove_pcm_runtime - Remove a pcm_runtime from card 953 * @card: The ASoC card to which the pcm_runtime has 954 * @rtd: The pcm_runtime to remove 955 * 956 * This function removes a pcm_runtime from the ASoC card. 957 */ 958void snd_soc_remove_pcm_runtime(struct snd_soc_card *card, 959 struct snd_soc_pcm_runtime *rtd) 960{ 961 lockdep_assert_held(&client_mutex); 962 963 /* release machine specific resources */ 964 snd_soc_link_exit(rtd); 965 966 /* 967 * Notify the machine driver for extra destruction 968 */ 969 snd_soc_card_remove_dai_link(card, rtd->dai_link); 970 971 soc_free_pcm_runtime(rtd); 972} 973EXPORT_SYMBOL_GPL(snd_soc_remove_pcm_runtime); 974 975/** 976 * snd_soc_add_pcm_runtime - Add a pcm_runtime dynamically via dai_link 977 * @card: The ASoC card to which the pcm_runtime is added 978 * @dai_link: The DAI link to find pcm_runtime 979 * 980 * This function adds a pcm_runtime ASoC card by using dai_link. 981 * 982 * Note: Topology can use this API to add pcm_runtime when probing the 983 * topology component. And machine drivers can still define static 984 * DAI links in dai_link array. 985 */ 986int snd_soc_add_pcm_runtime(struct snd_soc_card *card, 987 struct snd_soc_dai_link *dai_link) 988{ 989 struct snd_soc_pcm_runtime *rtd; 990 struct snd_soc_dai_link_component *codec, *platform, *cpu; 991 struct snd_soc_component *component; 992 int i, ret; 993 994 lockdep_assert_held(&client_mutex); 995 996 /* 997 * Notify the machine driver for extra initialization 998 */ 999 ret = snd_soc_card_add_dai_link(card, dai_link); 1000 if (ret < 0) 1001 return ret; 1002 1003 if (dai_link->ignore) 1004 return 0; 1005 1006 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name); 1007 1008 ret = soc_dai_link_sanity_check(card, dai_link); 1009 if (ret < 0) 1010 return ret; 1011 1012 rtd = soc_new_pcm_runtime(card, dai_link); 1013 if (!rtd) 1014 return -ENOMEM; 1015 1016 for_each_link_cpus(dai_link, i, cpu) { 1017 asoc_rtd_to_cpu(rtd, i) = snd_soc_find_dai(cpu); 1018 if (!asoc_rtd_to_cpu(rtd, i)) { 1019 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n", 1020 cpu->dai_name); 1021 goto _err_defer; 1022 } 1023 snd_soc_rtd_add_component(rtd, asoc_rtd_to_cpu(rtd, i)->component); 1024 } 1025 1026 /* Find CODEC from registered CODECs */ 1027 for_each_link_codecs(dai_link, i, codec) { 1028 asoc_rtd_to_codec(rtd, i) = snd_soc_find_dai(codec); 1029 if (!asoc_rtd_to_codec(rtd, i)) { 1030 dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n", 1031 codec->dai_name); 1032 goto _err_defer; 1033 } 1034 1035 snd_soc_rtd_add_component(rtd, asoc_rtd_to_codec(rtd, i)->component); 1036 } 1037 1038 /* Find PLATFORM from registered PLATFORMs */ 1039 for_each_link_platforms(dai_link, i, platform) { 1040 for_each_component(component) { 1041 if (!snd_soc_is_matching_component(platform, component)) 1042 continue; 1043 1044 snd_soc_rtd_add_component(rtd, component); 1045 } 1046 } 1047 1048 return 0; 1049 1050_err_defer: 1051 snd_soc_remove_pcm_runtime(card, rtd); 1052 return -EPROBE_DEFER; 1053} 1054EXPORT_SYMBOL_GPL(snd_soc_add_pcm_runtime); 1055 1056static int soc_init_pcm_runtime(struct snd_soc_card *card, 1057 struct snd_soc_pcm_runtime *rtd) 1058{ 1059 struct snd_soc_dai_link *dai_link = rtd->dai_link; 1060 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0); 1061 struct snd_soc_component *component; 1062 int ret, num, i; 1063 1064 /* set default power off timeout */ 1065 rtd->pmdown_time = pmdown_time; 1066 1067 /* do machine specific initialization */ 1068 ret = snd_soc_link_init(rtd); 1069 if (ret < 0) 1070 return ret; 1071 1072 if (dai_link->dai_fmt) { 1073 ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt); 1074 if (ret) 1075 return ret; 1076 } 1077 1078 /* add DPCM sysfs entries */ 1079 soc_dpcm_debugfs_add(rtd); 1080 1081 num = rtd->num; 1082 1083 /* 1084 * most drivers will register their PCMs using DAI link ordering but 1085 * topology based drivers can use the DAI link id field to set PCM 1086 * device number and then use rtd + a base offset of the BEs. 1087 */ 1088 for_each_rtd_components(rtd, i, component) { 1089 if (!component->driver->use_dai_pcm_id) 1090 continue; 1091 1092 if (rtd->dai_link->no_pcm) 1093 num += component->driver->be_pcm_base; 1094 else 1095 num = rtd->dai_link->id; 1096 } 1097 1098 /* create compress_device if possible */ 1099 ret = snd_soc_dai_compress_new(cpu_dai, rtd, num); 1100 if (ret != -ENOTSUPP) { 1101 if (ret < 0) 1102 dev_err(card->dev, "ASoC: can't create compress %s\n", 1103 dai_link->stream_name); 1104 return ret; 1105 } 1106 1107 /* create the pcm */ 1108 ret = soc_new_pcm(rtd, num); 1109 if (ret < 0) { 1110 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n", 1111 dai_link->stream_name, ret); 1112 return ret; 1113 } 1114 1115 return snd_soc_pcm_dai_new(rtd); 1116} 1117 1118static void soc_set_name_prefix(struct snd_soc_card *card, 1119 struct snd_soc_component *component) 1120{ 1121 struct device_node *of_node = soc_component_to_node(component); 1122 const char *str; 1123 int ret, i; 1124 1125 for (i = 0; i < card->num_configs; i++) { 1126 struct snd_soc_codec_conf *map = &card->codec_conf[i]; 1127 1128 if (snd_soc_is_matching_component(&map->dlc, component)) { 1129 component->name_prefix = map->name_prefix; 1130 return; 1131 } 1132 } 1133 1134 /* 1135 * If there is no configuration table or no match in the table, 1136 * check if a prefix is provided in the node 1137 */ 1138 ret = of_property_read_string(of_node, "sound-name-prefix", &str); 1139 if (ret < 0) 1140 return; 1141 1142 component->name_prefix = str; 1143} 1144 1145static void soc_remove_component(struct snd_soc_component *component, 1146 int probed) 1147{ 1148 1149 if (!component->card) 1150 return; 1151 1152 if (probed) 1153 snd_soc_component_remove(component); 1154 1155 /* For framework level robustness */ 1156 snd_soc_component_set_jack(component, NULL, NULL); 1157 1158 list_del_init(&component->card_list); 1159 snd_soc_dapm_free(snd_soc_component_get_dapm(component)); 1160 soc_cleanup_component_debugfs(component); 1161 component->card = NULL; 1162 snd_soc_component_module_put_when_remove(component); 1163} 1164 1165static int soc_probe_component(struct snd_soc_card *card, 1166 struct snd_soc_component *component) 1167{ 1168 struct snd_soc_dapm_context *dapm = 1169 snd_soc_component_get_dapm(component); 1170 struct snd_soc_dai *dai; 1171 int probed = 0; 1172 int ret; 1173 1174 if (!strcmp(component->name, "snd-soc-dummy")) 1175 return 0; 1176 1177 if (component->card) { 1178 if (component->card != card) { 1179 dev_err(component->dev, 1180 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n", 1181 card->name, component->card->name); 1182 return -ENODEV; 1183 } 1184 return 0; 1185 } 1186 1187 ret = snd_soc_component_module_get_when_probe(component); 1188 if (ret < 0) 1189 return ret; 1190 1191 component->card = card; 1192 soc_set_name_prefix(card, component); 1193 1194 soc_init_component_debugfs(component); 1195 1196 snd_soc_dapm_init(dapm, card, component); 1197 1198 ret = snd_soc_dapm_new_controls(dapm, 1199 component->driver->dapm_widgets, 1200 component->driver->num_dapm_widgets); 1201 1202 if (ret != 0) { 1203 dev_err(component->dev, 1204 "Failed to create new controls %d\n", ret); 1205 goto err_probe; 1206 } 1207 1208 for_each_component_dais(component, dai) { 1209 ret = snd_soc_dapm_new_dai_widgets(dapm, dai); 1210 if (ret != 0) { 1211 dev_err(component->dev, 1212 "Failed to create DAI widgets %d\n", ret); 1213 goto err_probe; 1214 } 1215 } 1216 1217 ret = snd_soc_component_probe(component); 1218 if (ret < 0) { 1219 dev_err(component->dev, 1220 "ASoC: failed to probe component %d\n", ret); 1221 goto err_probe; 1222 } 1223 WARN(dapm->idle_bias_off && 1224 dapm->bias_level != SND_SOC_BIAS_OFF, 1225 "codec %s can not start from non-off bias with idle_bias_off==1\n", 1226 component->name); 1227 probed = 1; 1228 1229 /* 1230 * machine specific init 1231 * see 1232 * snd_soc_component_set_aux() 1233 */ 1234 ret = snd_soc_component_init(component); 1235 if (ret < 0) 1236 goto err_probe; 1237 1238 ret = snd_soc_add_component_controls(component, 1239 component->driver->controls, 1240 component->driver->num_controls); 1241 if (ret < 0) 1242 goto err_probe; 1243 1244 ret = snd_soc_dapm_add_routes(dapm, 1245 component->driver->dapm_routes, 1246 component->driver->num_dapm_routes); 1247 if (ret < 0) { 1248 if (card->disable_route_checks) { 1249 dev_info(card->dev, 1250 "%s: disable_route_checks set, ignoring errors on add_routes\n", 1251 __func__); 1252 } else { 1253 dev_err(card->dev, 1254 "%s: snd_soc_dapm_add_routes failed: %d\n", 1255 __func__, ret); 1256 goto err_probe; 1257 } 1258 } 1259 1260 /* see for_each_card_components */ 1261 list_add(&component->card_list, &card->component_dev_list); 1262 1263err_probe: 1264 if (ret < 0) 1265 soc_remove_component(component, probed); 1266 1267 return ret; 1268} 1269 1270static void soc_remove_link_dais(struct snd_soc_card *card) 1271{ 1272 struct snd_soc_pcm_runtime *rtd; 1273 int order; 1274 1275 for_each_comp_order(order) { 1276 for_each_card_rtds(card, rtd) { 1277 /* remove all rtd connected DAIs in good order */ 1278 snd_soc_pcm_dai_remove(rtd, order); 1279 } 1280 } 1281} 1282 1283static int soc_probe_link_dais(struct snd_soc_card *card) 1284{ 1285 struct snd_soc_pcm_runtime *rtd; 1286 int order, ret; 1287 1288 for_each_comp_order(order) { 1289 for_each_card_rtds(card, rtd) { 1290 1291 dev_dbg(card->dev, 1292 "ASoC: probe %s dai link %d late %d\n", 1293 card->name, rtd->num, order); 1294 1295 /* probe all rtd connected DAIs in good order */ 1296 ret = snd_soc_pcm_dai_probe(rtd, order); 1297 if (ret) 1298 return ret; 1299 } 1300 } 1301 1302 return 0; 1303} 1304 1305static void soc_remove_link_components(struct snd_soc_card *card) 1306{ 1307 struct snd_soc_component *component; 1308 struct snd_soc_pcm_runtime *rtd; 1309 int i, order; 1310 1311 for_each_comp_order(order) { 1312 for_each_card_rtds(card, rtd) { 1313 for_each_rtd_components(rtd, i, component) { 1314 if (component->driver->remove_order != order) 1315 continue; 1316 1317 soc_remove_component(component, 1); 1318 } 1319 } 1320 } 1321} 1322 1323static int soc_probe_link_components(struct snd_soc_card *card) 1324{ 1325 struct snd_soc_component *component; 1326 struct snd_soc_pcm_runtime *rtd; 1327 int i, ret, order; 1328 1329 for_each_comp_order(order) { 1330 for_each_card_rtds(card, rtd) { 1331 for_each_rtd_components(rtd, i, component) { 1332 if (component->driver->probe_order != order) 1333 continue; 1334 1335 ret = soc_probe_component(card, component); 1336 if (ret < 0) 1337 return ret; 1338 } 1339 } 1340 } 1341 1342 return 0; 1343} 1344 1345static void soc_unbind_aux_dev(struct snd_soc_card *card) 1346{ 1347 struct snd_soc_component *component, *_component; 1348 1349 for_each_card_auxs_safe(card, component, _component) { 1350 /* for snd_soc_component_init() */ 1351 snd_soc_component_set_aux(component, NULL); 1352 list_del(&component->card_aux_list); 1353 } 1354} 1355 1356static int soc_bind_aux_dev(struct snd_soc_card *card) 1357{ 1358 struct snd_soc_component *component; 1359 struct snd_soc_aux_dev *aux; 1360 int i; 1361 1362 for_each_card_pre_auxs(card, i, aux) { 1363 /* codecs, usually analog devices */ 1364 component = soc_find_component(&aux->dlc); 1365 if (!component) 1366 return -EPROBE_DEFER; 1367 1368 /* for snd_soc_component_init() */ 1369 snd_soc_component_set_aux(component, aux); 1370 /* see for_each_card_auxs */ 1371 list_add(&component->card_aux_list, &card->aux_comp_list); 1372 } 1373 return 0; 1374} 1375 1376static int soc_probe_aux_devices(struct snd_soc_card *card) 1377{ 1378 struct snd_soc_component *component; 1379 int order; 1380 int ret; 1381 1382 for_each_comp_order(order) { 1383 for_each_card_auxs(card, component) { 1384 if (component->driver->probe_order != order) 1385 continue; 1386 1387 ret = soc_probe_component(card, component); 1388 if (ret < 0) 1389 return ret; 1390 } 1391 } 1392 1393 return 0; 1394} 1395 1396static void soc_remove_aux_devices(struct snd_soc_card *card) 1397{ 1398 struct snd_soc_component *comp, *_comp; 1399 int order; 1400 1401 for_each_comp_order(order) { 1402 for_each_card_auxs_safe(card, comp, _comp) { 1403 if (comp->driver->remove_order == order) 1404 soc_remove_component(comp, 1); 1405 } 1406 } 1407} 1408 1409/** 1410 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime 1411 * @rtd: The runtime for which the DAI link format should be changed 1412 * @dai_fmt: The new DAI link format 1413 * 1414 * This function updates the DAI link format for all DAIs connected to the DAI 1415 * link for the specified runtime. 1416 * 1417 * Note: For setups with a static format set the dai_fmt field in the 1418 * corresponding snd_dai_link struct instead of using this function. 1419 * 1420 * Returns 0 on success, otherwise a negative error code. 1421 */ 1422int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd, 1423 unsigned int dai_fmt) 1424{ 1425 struct snd_soc_dai *cpu_dai; 1426 struct snd_soc_dai *codec_dai; 1427 unsigned int inv_dai_fmt; 1428 unsigned int i; 1429 int ret; 1430 1431 for_each_rtd_codec_dais(rtd, i, codec_dai) { 1432 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt); 1433 if (ret != 0 && ret != -ENOTSUPP) { 1434 dev_warn(codec_dai->dev, 1435 "ASoC: Failed to set DAI format: %d\n", ret); 1436 return ret; 1437 } 1438 } 1439 1440 /* 1441 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link 1442 * the component which has non_legacy_dai_naming is Codec 1443 */ 1444 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK; 1445 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) { 1446 case SND_SOC_DAIFMT_CBM_CFM: 1447 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS; 1448 break; 1449 case SND_SOC_DAIFMT_CBM_CFS: 1450 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM; 1451 break; 1452 case SND_SOC_DAIFMT_CBS_CFM: 1453 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS; 1454 break; 1455 case SND_SOC_DAIFMT_CBS_CFS: 1456 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM; 1457 break; 1458 } 1459 for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 1460 unsigned int fmt = dai_fmt; 1461 1462 if (cpu_dai->component->driver->non_legacy_dai_naming) 1463 fmt = inv_dai_fmt; 1464 1465 ret = snd_soc_dai_set_fmt(cpu_dai, fmt); 1466 if (ret != 0 && ret != -ENOTSUPP) { 1467 dev_warn(cpu_dai->dev, 1468 "ASoC: Failed to set DAI format: %d\n", ret); 1469 return ret; 1470 } 1471 } 1472 1473 return 0; 1474} 1475EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt); 1476 1477#ifdef CONFIG_DMI 1478/* 1479 * If a DMI filed contain strings in this blacklist (e.g. 1480 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken 1481 * as invalid and dropped when setting the card long name from DMI info. 1482 */ 1483static const char * const dmi_blacklist[] = { 1484 "To be filled by OEM", 1485 "TBD by OEM", 1486 "Default String", 1487 "Board Manufacturer", 1488 "Board Vendor Name", 1489 "Board Product Name", 1490 NULL, /* terminator */ 1491}; 1492 1493/* 1494 * Trim special characters, and replace '-' with '_' since '-' is used to 1495 * separate different DMI fields in the card long name. Only number and 1496 * alphabet characters and a few separator characters are kept. 1497 */ 1498static void cleanup_dmi_name(char *name) 1499{ 1500 int i, j = 0; 1501 1502 for (i = 0; name[i]; i++) { 1503 if (isalnum(name[i]) || (name[i] == '.') 1504 || (name[i] == '_')) 1505 name[j++] = name[i]; 1506 else if (name[i] == '-') 1507 name[j++] = '_'; 1508 } 1509 1510 name[j] = '\0'; 1511} 1512 1513/* 1514 * Check if a DMI field is valid, i.e. not containing any string 1515 * in the black list. 1516 */ 1517static int is_dmi_valid(const char *field) 1518{ 1519 int i = 0; 1520 1521 while (dmi_blacklist[i]) { 1522 if (strstr(field, dmi_blacklist[i])) 1523 return 0; 1524 i++; 1525 } 1526 1527 return 1; 1528} 1529 1530/* 1531 * Append a string to card->dmi_longname with character cleanups. 1532 */ 1533static void append_dmi_string(struct snd_soc_card *card, const char *str) 1534{ 1535 char *dst = card->dmi_longname; 1536 size_t dst_len = sizeof(card->dmi_longname); 1537 size_t len; 1538 1539 len = strlen(dst); 1540 snprintf(dst + len, dst_len - len, "-%s", str); 1541 1542 len++; /* skip the separator "-" */ 1543 if (len < dst_len) 1544 cleanup_dmi_name(dst + len); 1545} 1546 1547/** 1548 * snd_soc_set_dmi_name() - Register DMI names to card 1549 * @card: The card to register DMI names 1550 * @flavour: The flavour "differentiator" for the card amongst its peers. 1551 * 1552 * An Intel machine driver may be used by many different devices but are 1553 * difficult for userspace to differentiate, since machine drivers ususally 1554 * use their own name as the card short name and leave the card long name 1555 * blank. To differentiate such devices and fix bugs due to lack of 1556 * device-specific configurations, this function allows DMI info to be used 1557 * as the sound card long name, in the format of 1558 * "vendor-product-version-board" 1559 * (Character '-' is used to separate different DMI fields here). 1560 * This will help the user space to load the device-specific Use Case Manager 1561 * (UCM) configurations for the card. 1562 * 1563 * Possible card long names may be: 1564 * DellInc.-XPS139343-01-0310JH 1565 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA 1566 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX 1567 * 1568 * This function also supports flavoring the card longname to provide 1569 * the extra differentiation, like "vendor-product-version-board-flavor". 1570 * 1571 * We only keep number and alphabet characters and a few separator characters 1572 * in the card long name since UCM in the user space uses the card long names 1573 * as card configuration directory names and AudoConf cannot support special 1574 * charactors like SPACE. 1575 * 1576 * Returns 0 on success, otherwise a negative error code. 1577 */ 1578int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour) 1579{ 1580 const char *vendor, *product, *product_version, *board; 1581 1582 if (card->long_name) 1583 return 0; /* long name already set by driver or from DMI */ 1584 1585 if (!is_acpi_device_node(card->dev->fwnode)) 1586 return 0; 1587 1588 /* make up dmi long name as: vendor-product-version-board */ 1589 vendor = dmi_get_system_info(DMI_BOARD_VENDOR); 1590 if (!vendor || !is_dmi_valid(vendor)) { 1591 dev_warn(card->dev, "ASoC: no DMI vendor name!\n"); 1592 return 0; 1593 } 1594 1595 snprintf(card->dmi_longname, sizeof(card->dmi_longname), "%s", vendor); 1596 cleanup_dmi_name(card->dmi_longname); 1597 1598 product = dmi_get_system_info(DMI_PRODUCT_NAME); 1599 if (product && is_dmi_valid(product)) { 1600 append_dmi_string(card, product); 1601 1602 /* 1603 * some vendors like Lenovo may only put a self-explanatory 1604 * name in the product version field 1605 */ 1606 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION); 1607 if (product_version && is_dmi_valid(product_version)) 1608 append_dmi_string(card, product_version); 1609 } 1610 1611 board = dmi_get_system_info(DMI_BOARD_NAME); 1612 if (board && is_dmi_valid(board)) { 1613 if (!product || strcasecmp(board, product)) 1614 append_dmi_string(card, board); 1615 } else if (!product) { 1616 /* fall back to using legacy name */ 1617 dev_warn(card->dev, "ASoC: no DMI board/product name!\n"); 1618 return 0; 1619 } 1620 1621 /* Add flavour to dmi long name */ 1622 if (flavour) 1623 append_dmi_string(card, flavour); 1624 1625 /* set the card long name */ 1626 card->long_name = card->dmi_longname; 1627 1628 return 0; 1629} 1630EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name); 1631#endif /* CONFIG_DMI */ 1632 1633static void soc_check_tplg_fes(struct snd_soc_card *card) 1634{ 1635 struct snd_soc_component *component; 1636 const struct snd_soc_component_driver *comp_drv; 1637 struct snd_soc_dai_link *dai_link; 1638 int i; 1639 1640 for_each_component(component) { 1641 1642 /* does this component override BEs ? */ 1643 if (!component->driver->ignore_machine) 1644 continue; 1645 1646 /* for this machine ? */ 1647 if (!strcmp(component->driver->ignore_machine, 1648 card->dev->driver->name)) 1649 goto match; 1650 if (strcmp(component->driver->ignore_machine, 1651 dev_name(card->dev))) 1652 continue; 1653match: 1654 /* machine matches, so override the rtd data */ 1655 for_each_card_prelinks(card, i, dai_link) { 1656 1657 /* ignore this FE */ 1658 if (dai_link->dynamic) { 1659 dai_link->ignore = true; 1660 continue; 1661 } 1662 1663 dev_dbg(card->dev, "info: override BE DAI link %s\n", 1664 card->dai_link[i].name); 1665 1666 /* override platform component */ 1667 if (!dai_link->platforms) { 1668 dev_err(card->dev, "init platform error"); 1669 continue; 1670 } 1671 dai_link->platforms->name = component->name; 1672 1673 /* convert non BE into BE */ 1674 if (!dai_link->no_pcm) { 1675 dai_link->no_pcm = 1; 1676 1677 if (dai_link->dpcm_playback) 1678 dev_warn(card->dev, 1679 "invalid configuration, dailink %s has flags no_pcm=0 and dpcm_playback=1\n", 1680 dai_link->name); 1681 if (dai_link->dpcm_capture) 1682 dev_warn(card->dev, 1683 "invalid configuration, dailink %s has flags no_pcm=0 and dpcm_capture=1\n", 1684 dai_link->name); 1685 1686 /* convert normal link into DPCM one */ 1687 if (!(dai_link->dpcm_playback || 1688 dai_link->dpcm_capture)) { 1689 dai_link->dpcm_playback = !dai_link->capture_only; 1690 dai_link->dpcm_capture = !dai_link->playback_only; 1691 } 1692 } 1693 1694 /* 1695 * override any BE fixups 1696 * see 1697 * snd_soc_link_be_hw_params_fixup() 1698 */ 1699 dai_link->be_hw_params_fixup = 1700 component->driver->be_hw_params_fixup; 1701 1702 /* 1703 * most BE links don't set stream name, so set it to 1704 * dai link name if it's NULL to help bind widgets. 1705 */ 1706 if (!dai_link->stream_name) 1707 dai_link->stream_name = dai_link->name; 1708 } 1709 1710 /* Inform userspace we are using alternate topology */ 1711 if (component->driver->topology_name_prefix) { 1712 1713 /* topology shortname created? */ 1714 if (!card->topology_shortname_created) { 1715 comp_drv = component->driver; 1716 1717 snprintf(card->topology_shortname, 32, "%s-%s", 1718 comp_drv->topology_name_prefix, 1719 card->name); 1720 card->topology_shortname_created = true; 1721 } 1722 1723 /* use topology shortname */ 1724 card->name = card->topology_shortname; 1725 } 1726 } 1727} 1728 1729#define soc_setup_card_name(name, name1, name2, norm) \ 1730 __soc_setup_card_name(name, sizeof(name), name1, name2, norm) 1731static void __soc_setup_card_name(char *name, int len, 1732 const char *name1, const char *name2, 1733 int normalization) 1734{ 1735 int i; 1736 1737 snprintf(name, len, "%s", name1 ? name1 : name2); 1738 1739 if (!normalization) 1740 return; 1741 1742 /* 1743 * Name normalization 1744 * 1745 * The driver name is somewhat special, as it's used as a key for 1746 * searches in the user-space. 1747 * 1748 * ex) 1749 * "abcd??efg" -> "abcd__efg" 1750 */ 1751 for (i = 0; i < len; i++) { 1752 switch (name[i]) { 1753 case '_': 1754 case '-': 1755 case '\0': 1756 break; 1757 default: 1758 if (!isalnum(name[i])) 1759 name[i] = '_'; 1760 break; 1761 } 1762 } 1763} 1764 1765static void soc_cleanup_card_resources(struct snd_soc_card *card) 1766{ 1767 struct snd_soc_pcm_runtime *rtd, *n; 1768 1769 if (card->snd_card) 1770 snd_card_disconnect_sync(card->snd_card); 1771 1772 snd_soc_dapm_shutdown(card); 1773 1774 /* remove and free each DAI */ 1775 soc_remove_link_dais(card); 1776 soc_remove_link_components(card); 1777 1778 for_each_card_rtds_safe(card, rtd, n) 1779 snd_soc_remove_pcm_runtime(card, rtd); 1780 1781 /* remove auxiliary devices */ 1782 soc_remove_aux_devices(card); 1783 soc_unbind_aux_dev(card); 1784 1785 snd_soc_dapm_free(&card->dapm); 1786 soc_cleanup_card_debugfs(card); 1787 1788 /* remove the card */ 1789 snd_soc_card_remove(card); 1790 1791 if (card->snd_card) { 1792 snd_card_free(card->snd_card); 1793 card->snd_card = NULL; 1794 } 1795} 1796 1797static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister) 1798{ 1799 if (card->instantiated) { 1800 card->instantiated = false; 1801 snd_soc_flush_all_delayed_work(card); 1802 1803 soc_cleanup_card_resources(card); 1804 if (!unregister) 1805 list_add(&card->list, &unbind_card_list); 1806 } else { 1807 if (unregister) 1808 list_del(&card->list); 1809 } 1810} 1811 1812static int snd_soc_bind_card(struct snd_soc_card *card) 1813{ 1814 struct snd_soc_pcm_runtime *rtd; 1815 struct snd_soc_component *component; 1816 struct snd_soc_dai_link *dai_link; 1817 int ret, i; 1818 1819 mutex_lock(&client_mutex); 1820 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT); 1821 1822 snd_soc_dapm_init(&card->dapm, card, NULL); 1823 1824 /* check whether any platform is ignore machine FE and using topology */ 1825 soc_check_tplg_fes(card); 1826 1827 /* bind aux_devs too */ 1828 ret = soc_bind_aux_dev(card); 1829 if (ret < 0) 1830 goto probe_end; 1831 1832 /* add predefined DAI links to the list */ 1833 card->num_rtd = 0; 1834 for_each_card_prelinks(card, i, dai_link) { 1835 ret = snd_soc_add_pcm_runtime(card, dai_link); 1836 if (ret < 0) 1837 goto probe_end; 1838 } 1839 1840 /* card bind complete so register a sound card */ 1841 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, 1842 card->owner, 0, &card->snd_card); 1843 if (ret < 0) { 1844 dev_err(card->dev, 1845 "ASoC: can't create sound card for card %s: %d\n", 1846 card->name, ret); 1847 goto probe_end; 1848 } 1849 1850 soc_init_card_debugfs(card); 1851 1852 soc_resume_init(card); 1853 1854 ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets, 1855 card->num_dapm_widgets); 1856 if (ret < 0) 1857 goto probe_end; 1858 1859 ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets, 1860 card->num_of_dapm_widgets); 1861 if (ret < 0) 1862 goto probe_end; 1863 1864 /* initialise the sound card only once */ 1865 ret = snd_soc_card_probe(card); 1866 if (ret < 0) 1867 goto probe_end; 1868 1869 /* probe all components used by DAI links on this card */ 1870 ret = soc_probe_link_components(card); 1871 if (ret < 0) { 1872 dev_err(card->dev, 1873 "ASoC: failed to instantiate card %d\n", ret); 1874 goto probe_end; 1875 } 1876 1877 /* probe auxiliary components */ 1878 ret = soc_probe_aux_devices(card); 1879 if (ret < 0) { 1880 dev_err(card->dev, 1881 "ASoC: failed to probe aux component %d\n", ret); 1882 goto probe_end; 1883 } 1884 1885 /* probe all DAI links on this card */ 1886 ret = soc_probe_link_dais(card); 1887 if (ret < 0) { 1888 dev_err(card->dev, 1889 "ASoC: failed to instantiate card %d\n", ret); 1890 goto probe_end; 1891 } 1892 1893 for_each_card_rtds(card, rtd) { 1894 ret = soc_init_pcm_runtime(card, rtd); 1895 if (ret < 0) 1896 goto probe_end; 1897 } 1898 1899 snd_soc_dapm_link_dai_widgets(card); 1900 snd_soc_dapm_connect_dai_link_widgets(card); 1901 1902 ret = snd_soc_add_card_controls(card, card->controls, 1903 card->num_controls); 1904 if (ret < 0) 1905 goto probe_end; 1906 1907 ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes, 1908 card->num_dapm_routes); 1909 if (ret < 0) { 1910 if (card->disable_route_checks) { 1911 dev_info(card->dev, 1912 "%s: disable_route_checks set, ignoring errors on add_routes\n", 1913 __func__); 1914 } else { 1915 dev_err(card->dev, 1916 "%s: snd_soc_dapm_add_routes failed: %d\n", 1917 __func__, ret); 1918 goto probe_end; 1919 } 1920 } 1921 1922 ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes, 1923 card->num_of_dapm_routes); 1924 if (ret < 0) 1925 goto probe_end; 1926 1927 /* try to set some sane longname if DMI is available */ 1928 snd_soc_set_dmi_name(card, NULL); 1929 1930 soc_setup_card_name(card->snd_card->shortname, 1931 card->name, NULL, 0); 1932 soc_setup_card_name(card->snd_card->longname, 1933 card->long_name, card->name, 0); 1934 soc_setup_card_name(card->snd_card->driver, 1935 card->driver_name, card->name, 1); 1936 1937 if (card->components) { 1938 /* the current implementation of snd_component_add() accepts */ 1939 /* multiple components in the string separated by space, */ 1940 /* but the string collision (identical string) check might */ 1941 /* not work correctly */ 1942 ret = snd_component_add(card->snd_card, card->components); 1943 if (ret < 0) { 1944 dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n", 1945 card->name, ret); 1946 goto probe_end; 1947 } 1948 } 1949 1950 ret = snd_soc_card_late_probe(card); 1951 if (ret < 0) 1952 goto probe_end; 1953 1954 snd_soc_dapm_new_widgets(card); 1955 1956 ret = snd_card_register(card->snd_card); 1957 if (ret < 0) { 1958 dev_err(card->dev, "ASoC: failed to register soundcard %d\n", 1959 ret); 1960 goto probe_end; 1961 } 1962 1963 card->instantiated = 1; 1964 dapm_mark_endpoints_dirty(card); 1965 snd_soc_dapm_sync(&card->dapm); 1966 1967 /* deactivate pins to sleep state */ 1968 for_each_card_components(card, component) 1969 if (!snd_soc_component_active(component)) 1970 pinctrl_pm_select_sleep_state(component->dev); 1971 1972probe_end: 1973 if (ret < 0) 1974 soc_cleanup_card_resources(card); 1975 1976 mutex_unlock(&card->mutex); 1977 mutex_unlock(&client_mutex); 1978 1979 return ret; 1980} 1981 1982/* probes a new socdev */ 1983static int soc_probe(struct platform_device *pdev) 1984{ 1985 struct snd_soc_card *card = platform_get_drvdata(pdev); 1986 1987 /* 1988 * no card, so machine driver should be registering card 1989 * we should not be here in that case so ret error 1990 */ 1991 if (!card) 1992 return -EINVAL; 1993 1994 dev_warn(&pdev->dev, 1995 "ASoC: machine %s should use snd_soc_register_card()\n", 1996 card->name); 1997 1998 /* Bodge while we unpick instantiation */ 1999 card->dev = &pdev->dev; 2000 2001 return devm_snd_soc_register_card(&pdev->dev, card); 2002} 2003 2004int snd_soc_poweroff(struct device *dev) 2005{ 2006 struct snd_soc_card *card = dev_get_drvdata(dev); 2007 struct snd_soc_component *component; 2008 2009 if (!card->instantiated) 2010 return 0; 2011 2012 /* 2013 * Flush out pmdown_time work - we actually do want to run it 2014 * now, we're shutting down so no imminent restart. 2015 */ 2016 snd_soc_flush_all_delayed_work(card); 2017 2018 snd_soc_dapm_shutdown(card); 2019 2020 /* deactivate pins to sleep state */ 2021 for_each_card_components(card, component) 2022 pinctrl_pm_select_sleep_state(component->dev); 2023 2024 return 0; 2025} 2026EXPORT_SYMBOL_GPL(snd_soc_poweroff); 2027 2028const struct dev_pm_ops snd_soc_pm_ops = { 2029 .suspend = snd_soc_suspend, 2030 .resume = snd_soc_resume, 2031 .freeze = snd_soc_suspend, 2032 .thaw = snd_soc_resume, 2033 .poweroff = snd_soc_poweroff, 2034 .restore = snd_soc_resume, 2035}; 2036EXPORT_SYMBOL_GPL(snd_soc_pm_ops); 2037 2038/* ASoC platform driver */ 2039static struct platform_driver soc_driver = { 2040 .driver = { 2041 .name = "soc-audio", 2042 .pm = &snd_soc_pm_ops, 2043 }, 2044 .probe = soc_probe, 2045}; 2046 2047/** 2048 * snd_soc_cnew - create new control 2049 * @_template: control template 2050 * @data: control private data 2051 * @long_name: control long name 2052 * @prefix: control name prefix 2053 * 2054 * Create a new mixer control from a template control. 2055 * 2056 * Returns 0 for success, else error. 2057 */ 2058struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template, 2059 void *data, const char *long_name, 2060 const char *prefix) 2061{ 2062 struct snd_kcontrol_new template; 2063 struct snd_kcontrol *kcontrol; 2064 char *name = NULL; 2065 2066 memcpy(&template, _template, sizeof(template)); 2067 template.index = 0; 2068 2069 if (!long_name) 2070 long_name = template.name; 2071 2072 if (prefix) { 2073 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name); 2074 if (!name) 2075 return NULL; 2076 2077 template.name = name; 2078 } else { 2079 template.name = long_name; 2080 } 2081 2082 kcontrol = snd_ctl_new1(&template, data); 2083 2084 kfree(name); 2085 2086 return kcontrol; 2087} 2088EXPORT_SYMBOL_GPL(snd_soc_cnew); 2089 2090static int snd_soc_add_controls(struct snd_card *card, struct device *dev, 2091 const struct snd_kcontrol_new *controls, int num_controls, 2092 const char *prefix, void *data) 2093{ 2094 int err, i; 2095 2096 for (i = 0; i < num_controls; i++) { 2097 const struct snd_kcontrol_new *control = &controls[i]; 2098 2099 err = snd_ctl_add(card, snd_soc_cnew(control, data, 2100 control->name, prefix)); 2101 if (err < 0) { 2102 dev_err(dev, "ASoC: Failed to add %s: %d\n", 2103 control->name, err); 2104 return err; 2105 } 2106 } 2107 2108 return 0; 2109} 2110 2111/** 2112 * snd_soc_add_component_controls - Add an array of controls to a component. 2113 * 2114 * @component: Component to add controls to 2115 * @controls: Array of controls to add 2116 * @num_controls: Number of elements in the array 2117 * 2118 * Return: 0 for success, else error. 2119 */ 2120int snd_soc_add_component_controls(struct snd_soc_component *component, 2121 const struct snd_kcontrol_new *controls, unsigned int num_controls) 2122{ 2123 struct snd_card *card = component->card->snd_card; 2124 2125 return snd_soc_add_controls(card, component->dev, controls, 2126 num_controls, component->name_prefix, component); 2127} 2128EXPORT_SYMBOL_GPL(snd_soc_add_component_controls); 2129 2130/** 2131 * snd_soc_add_card_controls - add an array of controls to a SoC card. 2132 * Convenience function to add a list of controls. 2133 * 2134 * @soc_card: SoC card to add controls to 2135 * @controls: array of controls to add 2136 * @num_controls: number of elements in the array 2137 * 2138 * Return 0 for success, else error. 2139 */ 2140int snd_soc_add_card_controls(struct snd_soc_card *soc_card, 2141 const struct snd_kcontrol_new *controls, int num_controls) 2142{ 2143 struct snd_card *card = soc_card->snd_card; 2144 2145 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls, 2146 NULL, soc_card); 2147} 2148EXPORT_SYMBOL_GPL(snd_soc_add_card_controls); 2149 2150/** 2151 * snd_soc_add_dai_controls - add an array of controls to a DAI. 2152 * Convienience function to add a list of controls. 2153 * 2154 * @dai: DAI to add controls to 2155 * @controls: array of controls to add 2156 * @num_controls: number of elements in the array 2157 * 2158 * Return 0 for success, else error. 2159 */ 2160int snd_soc_add_dai_controls(struct snd_soc_dai *dai, 2161 const struct snd_kcontrol_new *controls, int num_controls) 2162{ 2163 struct snd_card *card = dai->component->card->snd_card; 2164 2165 return snd_soc_add_controls(card, dai->dev, controls, num_controls, 2166 NULL, dai); 2167} 2168EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls); 2169 2170/** 2171 * snd_soc_register_card - Register a card with the ASoC core 2172 * 2173 * @card: Card to register 2174 * 2175 */ 2176int snd_soc_register_card(struct snd_soc_card *card) 2177{ 2178 if (!card->name || !card->dev) 2179 return -EINVAL; 2180 2181 dev_set_drvdata(card->dev, card); 2182 2183 INIT_LIST_HEAD(&card->widgets); 2184 INIT_LIST_HEAD(&card->paths); 2185 INIT_LIST_HEAD(&card->dapm_list); 2186 INIT_LIST_HEAD(&card->aux_comp_list); 2187 INIT_LIST_HEAD(&card->component_dev_list); 2188 INIT_LIST_HEAD(&card->list); 2189 INIT_LIST_HEAD(&card->rtd_list); 2190 INIT_LIST_HEAD(&card->dapm_dirty); 2191 INIT_LIST_HEAD(&card->dobj_list); 2192 2193 card->instantiated = 0; 2194 mutex_init(&card->mutex); 2195 mutex_init(&card->dapm_mutex); 2196 mutex_init(&card->pcm_mutex); 2197 spin_lock_init(&card->dpcm_lock); 2198 2199 return snd_soc_bind_card(card); 2200} 2201EXPORT_SYMBOL_GPL(snd_soc_register_card); 2202 2203/** 2204 * snd_soc_unregister_card - Unregister a card with the ASoC core 2205 * 2206 * @card: Card to unregister 2207 * 2208 */ 2209int snd_soc_unregister_card(struct snd_soc_card *card) 2210{ 2211 mutex_lock(&client_mutex); 2212 snd_soc_unbind_card(card, true); 2213 mutex_unlock(&client_mutex); 2214 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name); 2215 2216 return 0; 2217} 2218EXPORT_SYMBOL_GPL(snd_soc_unregister_card); 2219 2220/* 2221 * Simplify DAI link configuration by removing ".-1" from device names 2222 * and sanitizing names. 2223 */ 2224static char *fmt_single_name(struct device *dev, int *id) 2225{ 2226 const char *devname = dev_name(dev); 2227 char *found, *name; 2228 int id1, id2; 2229 2230 if (devname == NULL) 2231 return NULL; 2232 2233 name = devm_kstrdup(dev, devname, GFP_KERNEL); 2234 if (!name) 2235 return NULL; 2236 2237 /* are we a "%s.%d" name (platform and SPI components) */ 2238 found = strstr(name, dev->driver->name); 2239 if (found) { 2240 /* get ID */ 2241 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) { 2242 2243 /* discard ID from name if ID == -1 */ 2244 if (*id == -1) 2245 found[strlen(dev->driver->name)] = '\0'; 2246 } 2247 2248 /* I2C component devices are named "bus-addr" */ 2249 } else if (sscanf(name, "%x-%x", &id1, &id2) == 2) { 2250 2251 /* create unique ID number from I2C addr and bus */ 2252 *id = ((id1 & 0xffff) << 16) + id2; 2253 2254 devm_kfree(dev, name); 2255 2256 /* sanitize component name for DAI link creation */ 2257 name = devm_kasprintf(dev, GFP_KERNEL, "%s.%s", dev->driver->name, devname); 2258 } else { 2259 *id = 0; 2260 } 2261 2262 return name; 2263} 2264 2265/* 2266 * Simplify DAI link naming for single devices with multiple DAIs by removing 2267 * any ".-1" and using the DAI name (instead of device name). 2268 */ 2269static inline char *fmt_multiple_name(struct device *dev, 2270 struct snd_soc_dai_driver *dai_drv) 2271{ 2272 if (dai_drv->name == NULL) { 2273 dev_err(dev, 2274 "ASoC: error - multiple DAI %s registered with no name\n", 2275 dev_name(dev)); 2276 return NULL; 2277 } 2278 2279 return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL); 2280} 2281 2282void snd_soc_unregister_dai(struct snd_soc_dai *dai) 2283{ 2284 dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name); 2285 list_del(&dai->list); 2286} 2287EXPORT_SYMBOL_GPL(snd_soc_unregister_dai); 2288 2289/** 2290 * snd_soc_register_dai - Register a DAI dynamically & create its widgets 2291 * 2292 * @component: The component the DAIs are registered for 2293 * @dai_drv: DAI driver to use for the DAI 2294 * @legacy_dai_naming: if %true, use legacy single-name format; 2295 * if %false, use multiple-name format; 2296 * 2297 * Topology can use this API to register DAIs when probing a component. 2298 * These DAIs's widgets will be freed in the card cleanup and the DAIs 2299 * will be freed in the component cleanup. 2300 */ 2301struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component, 2302 struct snd_soc_dai_driver *dai_drv, 2303 bool legacy_dai_naming) 2304{ 2305 struct device *dev = component->dev; 2306 struct snd_soc_dai *dai; 2307 2308 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev)); 2309 2310 lockdep_assert_held(&client_mutex); 2311 2312 dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL); 2313 if (dai == NULL) 2314 return NULL; 2315 2316 /* 2317 * Back in the old days when we still had component-less DAIs, 2318 * instead of having a static name, component-less DAIs would 2319 * inherit the name of the parent device so it is possible to 2320 * register multiple instances of the DAI. We still need to keep 2321 * the same naming style even though those DAIs are not 2322 * component-less anymore. 2323 */ 2324 if (legacy_dai_naming && 2325 (dai_drv->id == 0 || dai_drv->name == NULL)) { 2326 dai->name = fmt_single_name(dev, &dai->id); 2327 } else { 2328 dai->name = fmt_multiple_name(dev, dai_drv); 2329 if (dai_drv->id) 2330 dai->id = dai_drv->id; 2331 else 2332 dai->id = component->num_dai; 2333 } 2334 if (!dai->name) 2335 return NULL; 2336 2337 dai->component = component; 2338 dai->dev = dev; 2339 dai->driver = dai_drv; 2340 2341 /* see for_each_component_dais */ 2342 list_add_tail(&dai->list, &component->dai_list); 2343 component->num_dai++; 2344 2345 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name); 2346 return dai; 2347} 2348 2349/** 2350 * snd_soc_unregister_dais - Unregister DAIs from the ASoC core 2351 * 2352 * @component: The component for which the DAIs should be unregistered 2353 */ 2354static void snd_soc_unregister_dais(struct snd_soc_component *component) 2355{ 2356 struct snd_soc_dai *dai, *_dai; 2357 2358 for_each_component_dais_safe(component, dai, _dai) 2359 snd_soc_unregister_dai(dai); 2360} 2361 2362/** 2363 * snd_soc_register_dais - Register a DAI with the ASoC core 2364 * 2365 * @component: The component the DAIs are registered for 2366 * @dai_drv: DAI driver to use for the DAIs 2367 * @count: Number of DAIs 2368 */ 2369static int snd_soc_register_dais(struct snd_soc_component *component, 2370 struct snd_soc_dai_driver *dai_drv, 2371 size_t count) 2372{ 2373 struct snd_soc_dai *dai; 2374 unsigned int i; 2375 int ret; 2376 2377 for (i = 0; i < count; i++) { 2378 dai = snd_soc_register_dai(component, dai_drv + i, count == 1 && 2379 !component->driver->non_legacy_dai_naming); 2380 if (dai == NULL) { 2381 ret = -ENOMEM; 2382 goto err; 2383 } 2384 } 2385 2386 return 0; 2387 2388err: 2389 snd_soc_unregister_dais(component); 2390 2391 return ret; 2392} 2393 2394#define ENDIANNESS_MAP(name) \ 2395 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE) 2396static u64 endianness_format_map[] = { 2397 ENDIANNESS_MAP(S16_), 2398 ENDIANNESS_MAP(U16_), 2399 ENDIANNESS_MAP(S24_), 2400 ENDIANNESS_MAP(U24_), 2401 ENDIANNESS_MAP(S32_), 2402 ENDIANNESS_MAP(U32_), 2403 ENDIANNESS_MAP(S24_3), 2404 ENDIANNESS_MAP(U24_3), 2405 ENDIANNESS_MAP(S20_3), 2406 ENDIANNESS_MAP(U20_3), 2407 ENDIANNESS_MAP(S18_3), 2408 ENDIANNESS_MAP(U18_3), 2409 ENDIANNESS_MAP(FLOAT_), 2410 ENDIANNESS_MAP(FLOAT64_), 2411 ENDIANNESS_MAP(IEC958_SUBFRAME_), 2412}; 2413 2414/* 2415 * Fix up the DAI formats for endianness: codecs don't actually see 2416 * the endianness of the data but we're using the CPU format 2417 * definitions which do need to include endianness so we ensure that 2418 * codec DAIs always have both big and little endian variants set. 2419 */ 2420static void convert_endianness_formats(struct snd_soc_pcm_stream *stream) 2421{ 2422 int i; 2423 2424 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++) 2425 if (stream->formats & endianness_format_map[i]) 2426 stream->formats |= endianness_format_map[i]; 2427} 2428 2429static void snd_soc_try_rebind_card(void) 2430{ 2431 struct snd_soc_card *card, *c; 2432 2433 list_for_each_entry_safe(card, c, &unbind_card_list, list) 2434 if (!snd_soc_bind_card(card)) 2435 list_del(&card->list); 2436} 2437 2438static void snd_soc_del_component_unlocked(struct snd_soc_component *component) 2439{ 2440 struct snd_soc_card *card = component->card; 2441 2442 snd_soc_unregister_dais(component); 2443 2444 if (card) 2445 snd_soc_unbind_card(card, false); 2446 2447 list_del(&component->list); 2448} 2449 2450int snd_soc_component_initialize(struct snd_soc_component *component, 2451 const struct snd_soc_component_driver *driver, 2452 struct device *dev) 2453{ 2454 INIT_LIST_HEAD(&component->dai_list); 2455 INIT_LIST_HEAD(&component->dobj_list); 2456 INIT_LIST_HEAD(&component->card_list); 2457 INIT_LIST_HEAD(&component->list); 2458 mutex_init(&component->io_mutex); 2459 2460 component->name = fmt_single_name(dev, &component->id); 2461 if (!component->name) { 2462 dev_err(dev, "ASoC: Failed to allocate name\n"); 2463 return -ENOMEM; 2464 } 2465 2466 component->dev = dev; 2467 component->driver = driver; 2468 2469 return 0; 2470} 2471EXPORT_SYMBOL_GPL(snd_soc_component_initialize); 2472 2473int snd_soc_add_component(struct snd_soc_component *component, 2474 struct snd_soc_dai_driver *dai_drv, 2475 int num_dai) 2476{ 2477 int ret; 2478 int i; 2479 2480 mutex_lock(&client_mutex); 2481 2482 if (component->driver->endianness) { 2483 for (i = 0; i < num_dai; i++) { 2484 convert_endianness_formats(&dai_drv[i].playback); 2485 convert_endianness_formats(&dai_drv[i].capture); 2486 } 2487 } 2488 2489 ret = snd_soc_register_dais(component, dai_drv, num_dai); 2490 if (ret < 0) { 2491 dev_err(component->dev, "ASoC: Failed to register DAIs: %d\n", 2492 ret); 2493 goto err_cleanup; 2494 } 2495 2496 if (!component->driver->write && !component->driver->read) { 2497 if (!component->regmap) 2498 component->regmap = dev_get_regmap(component->dev, 2499 NULL); 2500 if (component->regmap) 2501 snd_soc_component_setup_regmap(component); 2502 } 2503 2504 /* see for_each_component */ 2505 list_add(&component->list, &component_list); 2506 2507err_cleanup: 2508 if (ret < 0) 2509 snd_soc_del_component_unlocked(component); 2510 2511 mutex_unlock(&client_mutex); 2512 2513 if (ret == 0) 2514 snd_soc_try_rebind_card(); 2515 2516 return ret; 2517} 2518EXPORT_SYMBOL_GPL(snd_soc_add_component); 2519 2520int snd_soc_register_component(struct device *dev, 2521 const struct snd_soc_component_driver *component_driver, 2522 struct snd_soc_dai_driver *dai_drv, 2523 int num_dai) 2524{ 2525 struct snd_soc_component *component; 2526 int ret; 2527 2528 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL); 2529 if (!component) 2530 return -ENOMEM; 2531 2532 ret = snd_soc_component_initialize(component, component_driver, dev); 2533 if (ret < 0) 2534 return ret; 2535 2536 return snd_soc_add_component(component, dai_drv, num_dai); 2537} 2538EXPORT_SYMBOL_GPL(snd_soc_register_component); 2539 2540/** 2541 * snd_soc_unregister_component_by_driver - Unregister component using a given driver 2542 * from the ASoC core 2543 * 2544 * @dev: The device to unregister 2545 * @component_driver: The component driver to unregister 2546 */ 2547void snd_soc_unregister_component_by_driver(struct device *dev, 2548 const struct snd_soc_component_driver *component_driver) 2549{ 2550 struct snd_soc_component *component; 2551 2552 if (!component_driver) 2553 return; 2554 2555 mutex_lock(&client_mutex); 2556 component = snd_soc_lookup_component_nolocked(dev, component_driver->name); 2557 if (!component) 2558 goto out; 2559 2560 snd_soc_del_component_unlocked(component); 2561 2562out: 2563 mutex_unlock(&client_mutex); 2564} 2565EXPORT_SYMBOL_GPL(snd_soc_unregister_component_by_driver); 2566 2567/** 2568 * snd_soc_unregister_component - Unregister all related component 2569 * from the ASoC core 2570 * 2571 * @dev: The device to unregister 2572 */ 2573void snd_soc_unregister_component(struct device *dev) 2574{ 2575 struct snd_soc_component *component; 2576 2577 mutex_lock(&client_mutex); 2578 while (1) { 2579 component = snd_soc_lookup_component_nolocked(dev, NULL); 2580 if (!component) 2581 break; 2582 2583 snd_soc_del_component_unlocked(component); 2584 } 2585 mutex_unlock(&client_mutex); 2586} 2587EXPORT_SYMBOL_GPL(snd_soc_unregister_component); 2588 2589/* Retrieve a card's name from device tree */ 2590int snd_soc_of_parse_card_name(struct snd_soc_card *card, 2591 const char *propname) 2592{ 2593 struct device_node *np; 2594 int ret; 2595 2596 if (!card->dev) { 2597 pr_err("card->dev is not set before calling %s\n", __func__); 2598 return -EINVAL; 2599 } 2600 2601 np = card->dev->of_node; 2602 2603 ret = of_property_read_string_index(np, propname, 0, &card->name); 2604 /* 2605 * EINVAL means the property does not exist. This is fine providing 2606 * card->name was previously set, which is checked later in 2607 * snd_soc_register_card. 2608 */ 2609 if (ret < 0 && ret != -EINVAL) { 2610 dev_err(card->dev, 2611 "ASoC: Property '%s' could not be read: %d\n", 2612 propname, ret); 2613 return ret; 2614 } 2615 2616 return 0; 2617} 2618EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name); 2619 2620static const struct snd_soc_dapm_widget simple_widgets[] = { 2621 SND_SOC_DAPM_MIC("Microphone", NULL), 2622 SND_SOC_DAPM_LINE("Line", NULL), 2623 SND_SOC_DAPM_HP("Headphone", NULL), 2624 SND_SOC_DAPM_SPK("Speaker", NULL), 2625}; 2626 2627int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card, 2628 const char *propname) 2629{ 2630 struct device_node *np = card->dev->of_node; 2631 struct snd_soc_dapm_widget *widgets; 2632 const char *template, *wname; 2633 int i, j, num_widgets, ret; 2634 2635 num_widgets = of_property_count_strings(np, propname); 2636 if (num_widgets < 0) { 2637 dev_err(card->dev, 2638 "ASoC: Property '%s' does not exist\n", propname); 2639 return -EINVAL; 2640 } 2641 if (num_widgets & 1) { 2642 dev_err(card->dev, 2643 "ASoC: Property '%s' length is not even\n", propname); 2644 return -EINVAL; 2645 } 2646 2647 num_widgets /= 2; 2648 if (!num_widgets) { 2649 dev_err(card->dev, "ASoC: Property '%s's length is zero\n", 2650 propname); 2651 return -EINVAL; 2652 } 2653 2654 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets), 2655 GFP_KERNEL); 2656 if (!widgets) { 2657 dev_err(card->dev, 2658 "ASoC: Could not allocate memory for widgets\n"); 2659 return -ENOMEM; 2660 } 2661 2662 for (i = 0; i < num_widgets; i++) { 2663 ret = of_property_read_string_index(np, propname, 2664 2 * i, &template); 2665 if (ret) { 2666 dev_err(card->dev, 2667 "ASoC: Property '%s' index %d read error:%d\n", 2668 propname, 2 * i, ret); 2669 return -EINVAL; 2670 } 2671 2672 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) { 2673 if (!strncmp(template, simple_widgets[j].name, 2674 strlen(simple_widgets[j].name))) { 2675 widgets[i] = simple_widgets[j]; 2676 break; 2677 } 2678 } 2679 2680 if (j >= ARRAY_SIZE(simple_widgets)) { 2681 dev_err(card->dev, 2682 "ASoC: DAPM widget '%s' is not supported\n", 2683 template); 2684 return -EINVAL; 2685 } 2686 2687 ret = of_property_read_string_index(np, propname, 2688 (2 * i) + 1, 2689 &wname); 2690 if (ret) { 2691 dev_err(card->dev, 2692 "ASoC: Property '%s' index %d read error:%d\n", 2693 propname, (2 * i) + 1, ret); 2694 return -EINVAL; 2695 } 2696 2697 widgets[i].name = wname; 2698 } 2699 2700 card->of_dapm_widgets = widgets; 2701 card->num_of_dapm_widgets = num_widgets; 2702 2703 return 0; 2704} 2705EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets); 2706 2707int snd_soc_of_get_slot_mask(struct device_node *np, 2708 const char *prop_name, 2709 unsigned int *mask) 2710{ 2711 u32 val; 2712 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val); 2713 int i; 2714 2715 if (!of_slot_mask) 2716 return 0; 2717 val /= sizeof(u32); 2718 for (i = 0; i < val; i++) 2719 if (be32_to_cpup(&of_slot_mask[i])) 2720 *mask |= (1 << i); 2721 2722 return val; 2723} 2724EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask); 2725 2726int snd_soc_of_parse_tdm_slot(struct device_node *np, 2727 unsigned int *tx_mask, 2728 unsigned int *rx_mask, 2729 unsigned int *slots, 2730 unsigned int *slot_width) 2731{ 2732 u32 val; 2733 int ret; 2734 2735 if (tx_mask) 2736 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask); 2737 if (rx_mask) 2738 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask); 2739 2740 if (of_property_read_bool(np, "dai-tdm-slot-num")) { 2741 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val); 2742 if (ret) 2743 return ret; 2744 2745 if (slots) 2746 *slots = val; 2747 } 2748 2749 if (of_property_read_bool(np, "dai-tdm-slot-width")) { 2750 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val); 2751 if (ret) 2752 return ret; 2753 2754 if (slot_width) 2755 *slot_width = val; 2756 } 2757 2758 return 0; 2759} 2760EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot); 2761 2762void snd_soc_of_parse_node_prefix(struct device_node *np, 2763 struct snd_soc_codec_conf *codec_conf, 2764 struct device_node *of_node, 2765 const char *propname) 2766{ 2767 const char *str; 2768 int ret; 2769 2770 ret = of_property_read_string(np, propname, &str); 2771 if (ret < 0) { 2772 /* no prefix is not error */ 2773 return; 2774 } 2775 2776 codec_conf->dlc.of_node = of_node; 2777 codec_conf->name_prefix = str; 2778} 2779EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix); 2780 2781int snd_soc_of_parse_audio_routing(struct snd_soc_card *card, 2782 const char *propname) 2783{ 2784 struct device_node *np = card->dev->of_node; 2785 int num_routes; 2786 struct snd_soc_dapm_route *routes; 2787 int i, ret; 2788 2789 num_routes = of_property_count_strings(np, propname); 2790 if (num_routes < 0 || num_routes & 1) { 2791 dev_err(card->dev, 2792 "ASoC: Property '%s' does not exist or its length is not even\n", 2793 propname); 2794 return -EINVAL; 2795 } 2796 num_routes /= 2; 2797 if (!num_routes) { 2798 dev_err(card->dev, "ASoC: Property '%s's length is zero\n", 2799 propname); 2800 return -EINVAL; 2801 } 2802 2803 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes), 2804 GFP_KERNEL); 2805 if (!routes) { 2806 dev_err(card->dev, 2807 "ASoC: Could not allocate DAPM route table\n"); 2808 return -ENOMEM; 2809 } 2810 2811 for (i = 0; i < num_routes; i++) { 2812 ret = of_property_read_string_index(np, propname, 2813 2 * i, &routes[i].sink); 2814 if (ret) { 2815 dev_err(card->dev, 2816 "ASoC: Property '%s' index %d could not be read: %d\n", 2817 propname, 2 * i, ret); 2818 return -EINVAL; 2819 } 2820 ret = of_property_read_string_index(np, propname, 2821 (2 * i) + 1, &routes[i].source); 2822 if (ret) { 2823 dev_err(card->dev, 2824 "ASoC: Property '%s' index %d could not be read: %d\n", 2825 propname, (2 * i) + 1, ret); 2826 return -EINVAL; 2827 } 2828 } 2829 2830 card->num_of_dapm_routes = num_routes; 2831 card->of_dapm_routes = routes; 2832 2833 return 0; 2834} 2835EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing); 2836 2837int snd_soc_of_parse_aux_devs(struct snd_soc_card *card, const char *propname) 2838{ 2839 struct device_node *node = card->dev->of_node; 2840 struct snd_soc_aux_dev *aux; 2841 int num, i; 2842 2843 num = of_count_phandle_with_args(node, propname, NULL); 2844 if (num == -ENOENT) { 2845 return 0; 2846 } else if (num < 0) { 2847 dev_err(card->dev, "ASOC: Property '%s' could not be read: %d\n", 2848 propname, num); 2849 return num; 2850 } 2851 2852 aux = devm_kcalloc(card->dev, num, sizeof(*aux), GFP_KERNEL); 2853 if (!aux) 2854 return -ENOMEM; 2855 card->aux_dev = aux; 2856 card->num_aux_devs = num; 2857 2858 for_each_card_pre_auxs(card, i, aux) { 2859 aux->dlc.of_node = of_parse_phandle(node, propname, i); 2860 if (!aux->dlc.of_node) 2861 return -EINVAL; 2862 } 2863 2864 return 0; 2865} 2866EXPORT_SYMBOL_GPL(snd_soc_of_parse_aux_devs); 2867 2868unsigned int snd_soc_of_parse_daifmt(struct device_node *np, 2869 const char *prefix, 2870 struct device_node **bitclkmaster, 2871 struct device_node **framemaster) 2872{ 2873 int ret, i; 2874 char prop[128]; 2875 unsigned int format = 0; 2876 int bit, frame; 2877 const char *str; 2878 struct { 2879 char *name; 2880 unsigned int val; 2881 } of_fmt_table[] = { 2882 { "i2s", SND_SOC_DAIFMT_I2S }, 2883 { "right_j", SND_SOC_DAIFMT_RIGHT_J }, 2884 { "left_j", SND_SOC_DAIFMT_LEFT_J }, 2885 { "dsp_a", SND_SOC_DAIFMT_DSP_A }, 2886 { "dsp_b", SND_SOC_DAIFMT_DSP_B }, 2887 { "ac97", SND_SOC_DAIFMT_AC97 }, 2888 { "pdm", SND_SOC_DAIFMT_PDM}, 2889 { "msb", SND_SOC_DAIFMT_MSB }, 2890 { "lsb", SND_SOC_DAIFMT_LSB }, 2891 }; 2892 2893 if (!prefix) 2894 prefix = ""; 2895 2896 /* 2897 * check "dai-format = xxx" 2898 * or "[prefix]format = xxx" 2899 * SND_SOC_DAIFMT_FORMAT_MASK area 2900 */ 2901 ret = of_property_read_string(np, "dai-format", &str); 2902 if (ret < 0) { 2903 snprintf(prop, sizeof(prop), "%sformat", prefix); 2904 ret = of_property_read_string(np, prop, &str); 2905 } 2906 if (ret == 0) { 2907 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) { 2908 if (strcmp(str, of_fmt_table[i].name) == 0) { 2909 format |= of_fmt_table[i].val; 2910 break; 2911 } 2912 } 2913 } 2914 2915 /* 2916 * check "[prefix]continuous-clock" 2917 * SND_SOC_DAIFMT_CLOCK_MASK area 2918 */ 2919 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix); 2920 if (of_property_read_bool(np, prop)) 2921 format |= SND_SOC_DAIFMT_CONT; 2922 else 2923 format |= SND_SOC_DAIFMT_GATED; 2924 2925 /* 2926 * check "[prefix]bitclock-inversion" 2927 * check "[prefix]frame-inversion" 2928 * SND_SOC_DAIFMT_INV_MASK area 2929 */ 2930 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix); 2931 bit = !!of_get_property(np, prop, NULL); 2932 2933 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix); 2934 frame = !!of_get_property(np, prop, NULL); 2935 2936 switch ((bit << 4) + frame) { 2937 case 0x11: 2938 format |= SND_SOC_DAIFMT_IB_IF; 2939 break; 2940 case 0x10: 2941 format |= SND_SOC_DAIFMT_IB_NF; 2942 break; 2943 case 0x01: 2944 format |= SND_SOC_DAIFMT_NB_IF; 2945 break; 2946 default: 2947 /* SND_SOC_DAIFMT_NB_NF is default */ 2948 break; 2949 } 2950 2951 /* 2952 * check "[prefix]bitclock-master" 2953 * check "[prefix]frame-master" 2954 * SND_SOC_DAIFMT_MASTER_MASK area 2955 */ 2956 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix); 2957 bit = !!of_get_property(np, prop, NULL); 2958 if (bit && bitclkmaster) 2959 *bitclkmaster = of_parse_phandle(np, prop, 0); 2960 2961 snprintf(prop, sizeof(prop), "%sframe-master", prefix); 2962 frame = !!of_get_property(np, prop, NULL); 2963 if (frame && framemaster) 2964 *framemaster = of_parse_phandle(np, prop, 0); 2965 2966 switch ((bit << 4) + frame) { 2967 case 0x11: 2968 format |= SND_SOC_DAIFMT_CBM_CFM; 2969 break; 2970 case 0x10: 2971 format |= SND_SOC_DAIFMT_CBM_CFS; 2972 break; 2973 case 0x01: 2974 format |= SND_SOC_DAIFMT_CBS_CFM; 2975 break; 2976 default: 2977 format |= SND_SOC_DAIFMT_CBS_CFS; 2978 break; 2979 } 2980 2981 return format; 2982} 2983EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt); 2984 2985int snd_soc_get_dai_id(struct device_node *ep) 2986{ 2987 struct snd_soc_component *component; 2988 struct snd_soc_dai_link_component dlc; 2989 int ret; 2990 2991 dlc.of_node = of_graph_get_port_parent(ep); 2992 dlc.name = NULL; 2993 /* 2994 * For example HDMI case, HDMI has video/sound port, 2995 * but ALSA SoC needs sound port number only. 2996 * Thus counting HDMI DT port/endpoint doesn't work. 2997 * Then, it should have .of_xlate_dai_id 2998 */ 2999 ret = -ENOTSUPP; 3000 mutex_lock(&client_mutex); 3001 component = soc_find_component(&dlc); 3002 if (component) 3003 ret = snd_soc_component_of_xlate_dai_id(component, ep); 3004 mutex_unlock(&client_mutex); 3005 3006 of_node_put(dlc.of_node); 3007 3008 return ret; 3009} 3010EXPORT_SYMBOL_GPL(snd_soc_get_dai_id); 3011 3012int snd_soc_get_dai_name(struct of_phandle_args *args, 3013 const char **dai_name) 3014{ 3015 struct snd_soc_component *pos; 3016 struct device_node *component_of_node; 3017 int ret = -EPROBE_DEFER; 3018 3019 mutex_lock(&client_mutex); 3020 for_each_component(pos) { 3021 component_of_node = soc_component_to_node(pos); 3022 3023 if (component_of_node != args->np || !pos->num_dai) 3024 continue; 3025 3026 ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name); 3027 if (ret == -ENOTSUPP) { 3028 struct snd_soc_dai *dai; 3029 int id = -1; 3030 3031 switch (args->args_count) { 3032 case 0: 3033 id = 0; /* same as dai_drv[0] */ 3034 break; 3035 case 1: 3036 id = args->args[0]; 3037 break; 3038 default: 3039 /* not supported */ 3040 break; 3041 } 3042 3043 if (id < 0 || id >= pos->num_dai) { 3044 ret = -EINVAL; 3045 continue; 3046 } 3047 3048 ret = 0; 3049 3050 /* find target DAI */ 3051 for_each_component_dais(pos, dai) { 3052 if (id == 0) 3053 break; 3054 id--; 3055 } 3056 3057 *dai_name = dai->driver->name; 3058 if (!*dai_name) 3059 *dai_name = pos->name; 3060 } else if (ret) { 3061 /* 3062 * if another error than ENOTSUPP is returned go on and 3063 * check if another component is provided with the same 3064 * node. This may happen if a device provides several 3065 * components 3066 */ 3067 continue; 3068 } 3069 3070 break; 3071 } 3072 mutex_unlock(&client_mutex); 3073 return ret; 3074} 3075EXPORT_SYMBOL_GPL(snd_soc_get_dai_name); 3076 3077int snd_soc_of_get_dai_name(struct device_node *of_node, 3078 const char **dai_name) 3079{ 3080 struct of_phandle_args args; 3081 int ret; 3082 3083 ret = of_parse_phandle_with_args(of_node, "sound-dai", 3084 "#sound-dai-cells", 0, &args); 3085 if (ret) 3086 return ret; 3087 3088 ret = snd_soc_get_dai_name(&args, dai_name); 3089 3090 of_node_put(args.np); 3091 3092 return ret; 3093} 3094EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name); 3095 3096/* 3097 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array 3098 * @dai_link: DAI link 3099 * 3100 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs(). 3101 */ 3102void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link) 3103{ 3104 struct snd_soc_dai_link_component *component; 3105 int index; 3106 3107 for_each_link_codecs(dai_link, index, component) { 3108 if (!component->of_node) 3109 break; 3110 of_node_put(component->of_node); 3111 component->of_node = NULL; 3112 } 3113} 3114EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs); 3115 3116/* 3117 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree 3118 * @dev: Card device 3119 * @of_node: Device node 3120 * @dai_link: DAI link 3121 * 3122 * Builds an array of CODEC DAI components from the DAI link property 3123 * 'sound-dai'. 3124 * The array is set in the DAI link and the number of DAIs is set accordingly. 3125 * The device nodes in the array (of_node) must be dereferenced by calling 3126 * snd_soc_of_put_dai_link_codecs() on @dai_link. 3127 * 3128 * Returns 0 for success 3129 */ 3130int snd_soc_of_get_dai_link_codecs(struct device *dev, 3131 struct device_node *of_node, 3132 struct snd_soc_dai_link *dai_link) 3133{ 3134 struct of_phandle_args args; 3135 struct snd_soc_dai_link_component *component; 3136 char *name; 3137 int index, num_codecs, ret; 3138 3139 /* Count the number of CODECs */ 3140 name = "sound-dai"; 3141 num_codecs = of_count_phandle_with_args(of_node, name, 3142 "#sound-dai-cells"); 3143 if (num_codecs <= 0) { 3144 if (num_codecs == -ENOENT) 3145 dev_err(dev, "No 'sound-dai' property\n"); 3146 else 3147 dev_err(dev, "Bad phandle in 'sound-dai'\n"); 3148 return num_codecs; 3149 } 3150 component = devm_kcalloc(dev, 3151 num_codecs, sizeof(*component), 3152 GFP_KERNEL); 3153 if (!component) 3154 return -ENOMEM; 3155 dai_link->codecs = component; 3156 dai_link->num_codecs = num_codecs; 3157 3158 /* Parse the list */ 3159 for_each_link_codecs(dai_link, index, component) { 3160 ret = of_parse_phandle_with_args(of_node, name, 3161 "#sound-dai-cells", 3162 index, &args); 3163 if (ret) 3164 goto err; 3165 component->of_node = args.np; 3166 ret = snd_soc_get_dai_name(&args, &component->dai_name); 3167 if (ret < 0) 3168 goto err; 3169 } 3170 return 0; 3171err: 3172 snd_soc_of_put_dai_link_codecs(dai_link); 3173 dai_link->codecs = NULL; 3174 dai_link->num_codecs = 0; 3175 return ret; 3176} 3177EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs); 3178 3179static int __init snd_soc_init(void) 3180{ 3181 int ret; 3182 3183 snd_soc_debugfs_init(); 3184 ret = snd_soc_util_init(); 3185 if (ret) 3186 goto err_util_init; 3187 3188 ret = platform_driver_register(&soc_driver); 3189 if (ret) 3190 goto err_register; 3191 return 0; 3192 3193err_register: 3194 snd_soc_util_exit(); 3195err_util_init: 3196 snd_soc_debugfs_exit(); 3197 return ret; 3198} 3199module_init(snd_soc_init); 3200 3201static void __exit snd_soc_exit(void) 3202{ 3203 snd_soc_util_exit(); 3204 snd_soc_debugfs_exit(); 3205 3206 platform_driver_unregister(&soc_driver); 3207} 3208module_exit(snd_soc_exit); 3209 3210/* Module information */ 3211MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); 3212MODULE_DESCRIPTION("ALSA SoC Core"); 3213MODULE_LICENSE("GPL"); 3214MODULE_ALIAS("platform:soc-audio"); 3215