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