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