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