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