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