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