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