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