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