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