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