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