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