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