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 <sound/core.h> 38 #include <sound/jack.h> 39 #include <sound/pcm.h> 40 #include <sound/pcm_params.h> 41 #include <sound/soc.h> 42 #include <sound/soc-dpcm.h> 43 #include <sound/initval.h> 44 45 #define CREATE_TRACE_POINTS 46 #include <trace/events/asoc.h> 47 48 #define NAME_SIZE 32 49 50 #ifdef CONFIG_DEBUG_FS 51 struct dentry *snd_soc_debugfs_root; 52 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root); 53 #endif 54 55 static DEFINE_MUTEX(client_mutex); 56 static LIST_HEAD(platform_list); 57 static LIST_HEAD(codec_list); 58 static LIST_HEAD(component_list); 59 60 /* 61 * This is a timeout to do a DAPM powerdown after a stream is closed(). 62 * It can be used to eliminate pops between different playback streams, e.g. 63 * between two audio tracks. 64 */ 65 static int pmdown_time = 5000; 66 module_param(pmdown_time, int, 0); 67 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)"); 68 69 /* returns the minimum number of bytes needed to represent 70 * a particular given value */ 71 static int min_bytes_needed(unsigned long val) 72 { 73 int c = 0; 74 int i; 75 76 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c) 77 if (val & (1UL << i)) 78 break; 79 c = (sizeof val * 8) - c; 80 if (!c || (c % 8)) 81 c = (c + 8) / 8; 82 else 83 c /= 8; 84 return c; 85 } 86 87 /* fill buf which is 'len' bytes with a formatted 88 * string of the form 'reg: value\n' */ 89 static int format_register_str(struct snd_soc_codec *codec, 90 unsigned int reg, char *buf, size_t len) 91 { 92 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2; 93 int regsize = codec->driver->reg_word_size * 2; 94 int ret; 95 char tmpbuf[len + 1]; 96 char regbuf[regsize + 1]; 97 98 /* since tmpbuf is allocated on the stack, warn the callers if they 99 * try to abuse this function */ 100 WARN_ON(len > 63); 101 102 /* +2 for ': ' and + 1 for '\n' */ 103 if (wordsize + regsize + 2 + 1 != len) 104 return -EINVAL; 105 106 ret = snd_soc_read(codec, reg); 107 if (ret < 0) { 108 memset(regbuf, 'X', regsize); 109 regbuf[regsize] = '\0'; 110 } else { 111 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret); 112 } 113 114 /* prepare the buffer */ 115 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf); 116 /* copy it back to the caller without the '\0' */ 117 memcpy(buf, tmpbuf, len); 118 119 return 0; 120 } 121 122 /* codec register dump */ 123 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf, 124 size_t count, loff_t pos) 125 { 126 int i, step = 1; 127 int wordsize, regsize; 128 int len; 129 size_t total = 0; 130 loff_t p = 0; 131 132 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2; 133 regsize = codec->driver->reg_word_size * 2; 134 135 len = wordsize + regsize + 2 + 1; 136 137 if (!codec->driver->reg_cache_size) 138 return 0; 139 140 if (codec->driver->reg_cache_step) 141 step = codec->driver->reg_cache_step; 142 143 for (i = 0; i < codec->driver->reg_cache_size; i += step) { 144 /* only support larger than PAGE_SIZE bytes debugfs 145 * entries for the default case */ 146 if (p >= pos) { 147 if (total + len >= count - 1) 148 break; 149 format_register_str(codec, i, buf + total, len); 150 total += len; 151 } 152 p += len; 153 } 154 155 total = min(total, count - 1); 156 157 return total; 158 } 159 160 static ssize_t codec_reg_show(struct device *dev, 161 struct device_attribute *attr, char *buf) 162 { 163 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 164 165 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0); 166 } 167 168 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL); 169 170 static ssize_t pmdown_time_show(struct device *dev, 171 struct device_attribute *attr, char *buf) 172 { 173 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 174 175 return sprintf(buf, "%ld\n", rtd->pmdown_time); 176 } 177 178 static ssize_t pmdown_time_set(struct device *dev, 179 struct device_attribute *attr, 180 const char *buf, size_t count) 181 { 182 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 183 int ret; 184 185 ret = kstrtol(buf, 10, &rtd->pmdown_time); 186 if (ret) 187 return ret; 188 189 return count; 190 } 191 192 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set); 193 194 #ifdef CONFIG_DEBUG_FS 195 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf, 196 size_t count, loff_t *ppos) 197 { 198 ssize_t ret; 199 struct snd_soc_codec *codec = file->private_data; 200 char *buf; 201 202 if (*ppos < 0 || !count) 203 return -EINVAL; 204 205 buf = kmalloc(count, GFP_KERNEL); 206 if (!buf) 207 return -ENOMEM; 208 209 ret = soc_codec_reg_show(codec, buf, count, *ppos); 210 if (ret >= 0) { 211 if (copy_to_user(user_buf, buf, ret)) { 212 kfree(buf); 213 return -EFAULT; 214 } 215 *ppos += ret; 216 } 217 218 kfree(buf); 219 return ret; 220 } 221 222 static ssize_t codec_reg_write_file(struct file *file, 223 const char __user *user_buf, size_t count, loff_t *ppos) 224 { 225 char buf[32]; 226 size_t buf_size; 227 char *start = buf; 228 unsigned long reg, value; 229 struct snd_soc_codec *codec = file->private_data; 230 int ret; 231 232 buf_size = min(count, (sizeof(buf)-1)); 233 if (copy_from_user(buf, user_buf, buf_size)) 234 return -EFAULT; 235 buf[buf_size] = 0; 236 237 while (*start == ' ') 238 start++; 239 reg = simple_strtoul(start, &start, 16); 240 while (*start == ' ') 241 start++; 242 ret = kstrtoul(start, 16, &value); 243 if (ret) 244 return ret; 245 246 /* Userspace has been fiddling around behind the kernel's back */ 247 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE); 248 249 snd_soc_write(codec, reg, value); 250 return buf_size; 251 } 252 253 static const struct file_operations codec_reg_fops = { 254 .open = simple_open, 255 .read = codec_reg_read_file, 256 .write = codec_reg_write_file, 257 .llseek = default_llseek, 258 }; 259 260 static void soc_init_component_debugfs(struct snd_soc_component *component) 261 { 262 if (component->debugfs_prefix) { 263 char *name; 264 265 name = kasprintf(GFP_KERNEL, "%s:%s", 266 component->debugfs_prefix, component->name); 267 if (name) { 268 component->debugfs_root = debugfs_create_dir(name, 269 component->card->debugfs_card_root); 270 kfree(name); 271 } 272 } else { 273 component->debugfs_root = debugfs_create_dir(component->name, 274 component->card->debugfs_card_root); 275 } 276 277 if (!component->debugfs_root) { 278 dev_warn(component->dev, 279 "ASoC: Failed to create component debugfs directory\n"); 280 return; 281 } 282 283 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component), 284 component->debugfs_root); 285 286 if (component->init_debugfs) 287 component->init_debugfs(component); 288 } 289 290 static void soc_cleanup_component_debugfs(struct snd_soc_component *component) 291 { 292 debugfs_remove_recursive(component->debugfs_root); 293 } 294 295 static void soc_init_codec_debugfs(struct snd_soc_component *component) 296 { 297 struct snd_soc_codec *codec = snd_soc_component_to_codec(component); 298 299 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644, 300 codec->component.debugfs_root, 301 codec, &codec_reg_fops); 302 if (!codec->debugfs_reg) 303 dev_warn(codec->dev, 304 "ASoC: Failed to create codec register debugfs file\n"); 305 } 306 307 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf, 308 size_t count, loff_t *ppos) 309 { 310 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 311 ssize_t len, ret = 0; 312 struct snd_soc_codec *codec; 313 314 if (!buf) 315 return -ENOMEM; 316 317 list_for_each_entry(codec, &codec_list, list) { 318 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", 319 codec->component.name); 320 if (len >= 0) 321 ret += len; 322 if (ret > PAGE_SIZE) { 323 ret = PAGE_SIZE; 324 break; 325 } 326 } 327 328 if (ret >= 0) 329 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 330 331 kfree(buf); 332 333 return ret; 334 } 335 336 static const struct file_operations codec_list_fops = { 337 .read = codec_list_read_file, 338 .llseek = default_llseek,/* read accesses f_pos */ 339 }; 340 341 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf, 342 size_t count, loff_t *ppos) 343 { 344 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 345 ssize_t len, ret = 0; 346 struct snd_soc_component *component; 347 struct snd_soc_dai *dai; 348 349 if (!buf) 350 return -ENOMEM; 351 352 list_for_each_entry(component, &component_list, list) { 353 list_for_each_entry(dai, &component->dai_list, list) { 354 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", 355 dai->name); 356 if (len >= 0) 357 ret += len; 358 if (ret > PAGE_SIZE) { 359 ret = PAGE_SIZE; 360 break; 361 } 362 } 363 } 364 365 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 366 367 kfree(buf); 368 369 return ret; 370 } 371 372 static const struct file_operations dai_list_fops = { 373 .read = dai_list_read_file, 374 .llseek = default_llseek,/* read accesses f_pos */ 375 }; 376 377 static ssize_t platform_list_read_file(struct file *file, 378 char __user *user_buf, 379 size_t count, loff_t *ppos) 380 { 381 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 382 ssize_t len, ret = 0; 383 struct snd_soc_platform *platform; 384 385 if (!buf) 386 return -ENOMEM; 387 388 list_for_each_entry(platform, &platform_list, list) { 389 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", 390 platform->component.name); 391 if (len >= 0) 392 ret += len; 393 if (ret > PAGE_SIZE) { 394 ret = PAGE_SIZE; 395 break; 396 } 397 } 398 399 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 400 401 kfree(buf); 402 403 return ret; 404 } 405 406 static const struct file_operations platform_list_fops = { 407 .read = platform_list_read_file, 408 .llseek = default_llseek,/* read accesses f_pos */ 409 }; 410 411 static void soc_init_card_debugfs(struct snd_soc_card *card) 412 { 413 card->debugfs_card_root = debugfs_create_dir(card->name, 414 snd_soc_debugfs_root); 415 if (!card->debugfs_card_root) { 416 dev_warn(card->dev, 417 "ASoC: Failed to create card debugfs directory\n"); 418 return; 419 } 420 421 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644, 422 card->debugfs_card_root, 423 &card->pop_time); 424 if (!card->debugfs_pop_time) 425 dev_warn(card->dev, 426 "ASoC: Failed to create pop time debugfs file\n"); 427 } 428 429 static void soc_cleanup_card_debugfs(struct snd_soc_card *card) 430 { 431 debugfs_remove_recursive(card->debugfs_card_root); 432 } 433 434 #else 435 436 #define soc_init_codec_debugfs NULL 437 438 static inline void soc_init_component_debugfs( 439 struct snd_soc_component *component) 440 { 441 } 442 443 static inline void soc_cleanup_component_debugfs( 444 struct snd_soc_component *component) 445 { 446 } 447 448 static inline void soc_init_card_debugfs(struct snd_soc_card *card) 449 { 450 } 451 452 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card) 453 { 454 } 455 #endif 456 457 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card, 458 const char *dai_link, int stream) 459 { 460 int i; 461 462 for (i = 0; i < card->num_links; i++) { 463 if (card->rtd[i].dai_link->no_pcm && 464 !strcmp(card->rtd[i].dai_link->name, dai_link)) 465 return card->rtd[i].pcm->streams[stream].substream; 466 } 467 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link); 468 return NULL; 469 } 470 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream); 471 472 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card, 473 const char *dai_link) 474 { 475 int i; 476 477 for (i = 0; i < card->num_links; i++) { 478 if (!strcmp(card->rtd[i].dai_link->name, dai_link)) 479 return &card->rtd[i]; 480 } 481 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link); 482 return NULL; 483 } 484 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime); 485 486 static void codec2codec_close_delayed_work(struct work_struct *work) 487 { 488 /* Currently nothing to do for c2c links 489 * Since c2c links are internal nodes in the DAPM graph and 490 * don't interface with the outside world or application layer 491 * we don't have to do any special handling on close. 492 */ 493 } 494 495 #ifdef CONFIG_PM_SLEEP 496 /* powers down audio subsystem for suspend */ 497 int snd_soc_suspend(struct device *dev) 498 { 499 struct snd_soc_card *card = dev_get_drvdata(dev); 500 struct snd_soc_codec *codec; 501 int i, j; 502 503 /* If the card is not initialized yet there is nothing to do */ 504 if (!card->instantiated) 505 return 0; 506 507 /* Due to the resume being scheduled into a workqueue we could 508 * suspend before that's finished - wait for it to complete. 509 */ 510 snd_power_lock(card->snd_card); 511 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0); 512 snd_power_unlock(card->snd_card); 513 514 /* we're going to block userspace touching us until resume completes */ 515 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot); 516 517 /* mute any active DACs */ 518 for (i = 0; i < card->num_rtd; i++) { 519 520 if (card->rtd[i].dai_link->ignore_suspend) 521 continue; 522 523 for (j = 0; j < card->rtd[i].num_codecs; j++) { 524 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j]; 525 struct snd_soc_dai_driver *drv = dai->driver; 526 527 if (drv->ops->digital_mute && dai->playback_active) 528 drv->ops->digital_mute(dai, 1); 529 } 530 } 531 532 /* suspend all pcms */ 533 for (i = 0; i < card->num_rtd; i++) { 534 if (card->rtd[i].dai_link->ignore_suspend) 535 continue; 536 537 snd_pcm_suspend_all(card->rtd[i].pcm); 538 } 539 540 if (card->suspend_pre) 541 card->suspend_pre(card); 542 543 for (i = 0; i < card->num_rtd; i++) { 544 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 545 546 if (card->rtd[i].dai_link->ignore_suspend) 547 continue; 548 549 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control) 550 cpu_dai->driver->suspend(cpu_dai); 551 } 552 553 /* close any waiting streams and save state */ 554 for (i = 0; i < card->num_rtd; i++) { 555 struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais; 556 flush_delayed_work(&card->rtd[i].delayed_work); 557 for (j = 0; j < card->rtd[i].num_codecs; j++) { 558 codec_dais[j]->codec->dapm.suspend_bias_level = 559 codec_dais[j]->codec->dapm.bias_level; 560 } 561 } 562 563 for (i = 0; i < card->num_rtd; i++) { 564 565 if (card->rtd[i].dai_link->ignore_suspend) 566 continue; 567 568 snd_soc_dapm_stream_event(&card->rtd[i], 569 SNDRV_PCM_STREAM_PLAYBACK, 570 SND_SOC_DAPM_STREAM_SUSPEND); 571 572 snd_soc_dapm_stream_event(&card->rtd[i], 573 SNDRV_PCM_STREAM_CAPTURE, 574 SND_SOC_DAPM_STREAM_SUSPEND); 575 } 576 577 /* Recheck all endpoints too, their state is affected by suspend */ 578 dapm_mark_endpoints_dirty(card); 579 snd_soc_dapm_sync(&card->dapm); 580 581 /* suspend all CODECs */ 582 list_for_each_entry(codec, &card->codec_dev_list, card_list) { 583 /* If there are paths active then the CODEC will be held with 584 * bias _ON and should not be suspended. */ 585 if (!codec->suspended) { 586 switch (codec->dapm.bias_level) { 587 case SND_SOC_BIAS_STANDBY: 588 /* 589 * If the CODEC is capable of idle 590 * bias off then being in STANDBY 591 * means it's doing something, 592 * otherwise fall through. 593 */ 594 if (codec->dapm.idle_bias_off) { 595 dev_dbg(codec->dev, 596 "ASoC: idle_bias_off CODEC on over suspend\n"); 597 break; 598 } 599 600 case SND_SOC_BIAS_OFF: 601 if (codec->driver->suspend) 602 codec->driver->suspend(codec); 603 codec->suspended = 1; 604 if (codec->component.regmap) 605 regcache_mark_dirty(codec->component.regmap); 606 /* deactivate pins to sleep state */ 607 pinctrl_pm_select_sleep_state(codec->dev); 608 break; 609 default: 610 dev_dbg(codec->dev, 611 "ASoC: CODEC is on over suspend\n"); 612 break; 613 } 614 } 615 } 616 617 for (i = 0; i < card->num_rtd; i++) { 618 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 619 620 if (card->rtd[i].dai_link->ignore_suspend) 621 continue; 622 623 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control) 624 cpu_dai->driver->suspend(cpu_dai); 625 626 /* deactivate pins to sleep state */ 627 pinctrl_pm_select_sleep_state(cpu_dai->dev); 628 } 629 630 if (card->suspend_post) 631 card->suspend_post(card); 632 633 return 0; 634 } 635 EXPORT_SYMBOL_GPL(snd_soc_suspend); 636 637 /* deferred resume work, so resume can complete before we finished 638 * setting our codec back up, which can be very slow on I2C 639 */ 640 static void soc_resume_deferred(struct work_struct *work) 641 { 642 struct snd_soc_card *card = 643 container_of(work, struct snd_soc_card, deferred_resume_work); 644 struct snd_soc_codec *codec; 645 int i, j; 646 647 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time, 648 * so userspace apps are blocked from touching us 649 */ 650 651 dev_dbg(card->dev, "ASoC: starting resume work\n"); 652 653 /* Bring us up into D2 so that DAPM starts enabling things */ 654 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2); 655 656 if (card->resume_pre) 657 card->resume_pre(card); 658 659 /* resume control bus DAIs */ 660 for (i = 0; i < card->num_rtd; i++) { 661 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 662 663 if (card->rtd[i].dai_link->ignore_suspend) 664 continue; 665 666 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control) 667 cpu_dai->driver->resume(cpu_dai); 668 } 669 670 list_for_each_entry(codec, &card->codec_dev_list, card_list) { 671 /* If the CODEC was idle over suspend then it will have been 672 * left with bias OFF or STANDBY and suspended so we must now 673 * resume. Otherwise the suspend was suppressed. 674 */ 675 if (codec->suspended) { 676 switch (codec->dapm.bias_level) { 677 case SND_SOC_BIAS_STANDBY: 678 case SND_SOC_BIAS_OFF: 679 if (codec->driver->resume) 680 codec->driver->resume(codec); 681 codec->suspended = 0; 682 break; 683 default: 684 dev_dbg(codec->dev, 685 "ASoC: CODEC was on over suspend\n"); 686 break; 687 } 688 } 689 } 690 691 for (i = 0; i < card->num_rtd; i++) { 692 693 if (card->rtd[i].dai_link->ignore_suspend) 694 continue; 695 696 snd_soc_dapm_stream_event(&card->rtd[i], 697 SNDRV_PCM_STREAM_PLAYBACK, 698 SND_SOC_DAPM_STREAM_RESUME); 699 700 snd_soc_dapm_stream_event(&card->rtd[i], 701 SNDRV_PCM_STREAM_CAPTURE, 702 SND_SOC_DAPM_STREAM_RESUME); 703 } 704 705 /* unmute any active DACs */ 706 for (i = 0; i < card->num_rtd; i++) { 707 708 if (card->rtd[i].dai_link->ignore_suspend) 709 continue; 710 711 for (j = 0; j < card->rtd[i].num_codecs; j++) { 712 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j]; 713 struct snd_soc_dai_driver *drv = dai->driver; 714 715 if (drv->ops->digital_mute && dai->playback_active) 716 drv->ops->digital_mute(dai, 0); 717 } 718 } 719 720 for (i = 0; i < card->num_rtd; i++) { 721 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 722 723 if (card->rtd[i].dai_link->ignore_suspend) 724 continue; 725 726 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control) 727 cpu_dai->driver->resume(cpu_dai); 728 } 729 730 if (card->resume_post) 731 card->resume_post(card); 732 733 dev_dbg(card->dev, "ASoC: resume work completed\n"); 734 735 /* userspace can access us now we are back as we were before */ 736 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0); 737 738 /* Recheck all endpoints too, their state is affected by suspend */ 739 dapm_mark_endpoints_dirty(card); 740 snd_soc_dapm_sync(&card->dapm); 741 } 742 743 /* powers up audio subsystem after a suspend */ 744 int snd_soc_resume(struct device *dev) 745 { 746 struct snd_soc_card *card = dev_get_drvdata(dev); 747 bool bus_control = false; 748 int i; 749 750 /* If the card is not initialized yet there is nothing to do */ 751 if (!card->instantiated) 752 return 0; 753 754 /* activate pins from sleep state */ 755 for (i = 0; i < card->num_rtd; i++) { 756 struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 757 struct snd_soc_dai **codec_dais = rtd->codec_dais; 758 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 759 int j; 760 761 if (cpu_dai->active) 762 pinctrl_pm_select_default_state(cpu_dai->dev); 763 764 for (j = 0; j < rtd->num_codecs; j++) { 765 struct snd_soc_dai *codec_dai = codec_dais[j]; 766 if (codec_dai->active) 767 pinctrl_pm_select_default_state(codec_dai->dev); 768 } 769 } 770 771 /* 772 * DAIs that also act as the control bus master might have other drivers 773 * hanging off them so need to resume immediately. Other drivers don't 774 * have that problem and may take a substantial amount of time to resume 775 * due to I/O costs and anti-pop so handle them out of line. 776 */ 777 for (i = 0; i < card->num_rtd; i++) { 778 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 779 bus_control |= cpu_dai->driver->bus_control; 780 } 781 if (bus_control) { 782 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n"); 783 soc_resume_deferred(&card->deferred_resume_work); 784 } else { 785 dev_dbg(dev, "ASoC: Scheduling resume work\n"); 786 if (!schedule_work(&card->deferred_resume_work)) 787 dev_err(dev, "ASoC: resume work item may be lost\n"); 788 } 789 790 return 0; 791 } 792 EXPORT_SYMBOL_GPL(snd_soc_resume); 793 #else 794 #define snd_soc_suspend NULL 795 #define snd_soc_resume NULL 796 #endif 797 798 static const struct snd_soc_dai_ops null_dai_ops = { 799 }; 800 801 static struct snd_soc_component *soc_find_component( 802 const struct device_node *of_node, const char *name) 803 { 804 struct snd_soc_component *component; 805 806 list_for_each_entry(component, &component_list, list) { 807 if (of_node) { 808 if (component->dev->of_node == of_node) 809 return component; 810 } else if (strcmp(component->name, name) == 0) { 811 return component; 812 } 813 } 814 815 return NULL; 816 } 817 818 static struct snd_soc_dai *snd_soc_find_dai( 819 const struct snd_soc_dai_link_component *dlc) 820 { 821 struct snd_soc_component *component; 822 struct snd_soc_dai *dai; 823 824 /* Find CPU DAI from registered DAIs*/ 825 list_for_each_entry(component, &component_list, list) { 826 if (dlc->of_node && component->dev->of_node != dlc->of_node) 827 continue; 828 if (dlc->name && strcmp(component->name, dlc->name)) 829 continue; 830 list_for_each_entry(dai, &component->dai_list, list) { 831 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)) 832 continue; 833 834 return dai; 835 } 836 } 837 838 return NULL; 839 } 840 841 static int soc_bind_dai_link(struct snd_soc_card *card, int num) 842 { 843 struct snd_soc_dai_link *dai_link = &card->dai_link[num]; 844 struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 845 struct snd_soc_dai_link_component *codecs = dai_link->codecs; 846 struct snd_soc_dai_link_component cpu_dai_component; 847 struct snd_soc_dai **codec_dais = rtd->codec_dais; 848 struct snd_soc_platform *platform; 849 const char *platform_name; 850 int i; 851 852 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num); 853 854 cpu_dai_component.name = dai_link->cpu_name; 855 cpu_dai_component.of_node = dai_link->cpu_of_node; 856 cpu_dai_component.dai_name = dai_link->cpu_dai_name; 857 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component); 858 if (!rtd->cpu_dai) { 859 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n", 860 dai_link->cpu_dai_name); 861 return -EPROBE_DEFER; 862 } 863 864 rtd->num_codecs = dai_link->num_codecs; 865 866 /* Find CODEC from registered CODECs */ 867 for (i = 0; i < rtd->num_codecs; i++) { 868 codec_dais[i] = snd_soc_find_dai(&codecs[i]); 869 if (!codec_dais[i]) { 870 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n", 871 codecs[i].dai_name); 872 return -EPROBE_DEFER; 873 } 874 } 875 876 /* Single codec links expect codec and codec_dai in runtime data */ 877 rtd->codec_dai = codec_dais[0]; 878 rtd->codec = rtd->codec_dai->codec; 879 880 /* if there's no platform we match on the empty platform */ 881 platform_name = dai_link->platform_name; 882 if (!platform_name && !dai_link->platform_of_node) 883 platform_name = "snd-soc-dummy"; 884 885 /* find one from the set of registered platforms */ 886 list_for_each_entry(platform, &platform_list, list) { 887 if (dai_link->platform_of_node) { 888 if (platform->dev->of_node != 889 dai_link->platform_of_node) 890 continue; 891 } else { 892 if (strcmp(platform->component.name, platform_name)) 893 continue; 894 } 895 896 rtd->platform = platform; 897 } 898 if (!rtd->platform) { 899 dev_err(card->dev, "ASoC: platform %s not registered\n", 900 dai_link->platform_name); 901 return -EPROBE_DEFER; 902 } 903 904 card->num_rtd++; 905 906 return 0; 907 } 908 909 static void soc_remove_component(struct snd_soc_component *component) 910 { 911 if (!component->probed) 912 return; 913 914 /* This is a HACK and will be removed soon */ 915 if (component->codec) 916 list_del(&component->codec->card_list); 917 918 if (component->remove) 919 component->remove(component); 920 921 snd_soc_dapm_free(snd_soc_component_get_dapm(component)); 922 923 soc_cleanup_component_debugfs(component); 924 component->probed = 0; 925 module_put(component->dev->driver->owner); 926 } 927 928 static void soc_remove_dai(struct snd_soc_dai *dai, int order) 929 { 930 int err; 931 932 if (dai && dai->probed && 933 dai->driver->remove_order == order) { 934 if (dai->driver->remove) { 935 err = dai->driver->remove(dai); 936 if (err < 0) 937 dev_err(dai->dev, 938 "ASoC: failed to remove %s: %d\n", 939 dai->name, err); 940 } 941 dai->probed = 0; 942 } 943 } 944 945 static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order) 946 { 947 struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 948 int i; 949 950 /* unregister the rtd device */ 951 if (rtd->dev_registered) { 952 device_remove_file(rtd->dev, &dev_attr_pmdown_time); 953 device_remove_file(rtd->dev, &dev_attr_codec_reg); 954 device_unregister(rtd->dev); 955 rtd->dev_registered = 0; 956 } 957 958 /* remove the CODEC DAI */ 959 for (i = 0; i < rtd->num_codecs; i++) 960 soc_remove_dai(rtd->codec_dais[i], order); 961 962 soc_remove_dai(rtd->cpu_dai, order); 963 } 964 965 static void soc_remove_link_components(struct snd_soc_card *card, int num, 966 int order) 967 { 968 struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 969 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 970 struct snd_soc_platform *platform = rtd->platform; 971 struct snd_soc_component *component; 972 int i; 973 974 /* remove the platform */ 975 if (platform && platform->component.driver->remove_order == order) 976 soc_remove_component(&platform->component); 977 978 /* remove the CODEC-side CODEC */ 979 for (i = 0; i < rtd->num_codecs; i++) { 980 component = rtd->codec_dais[i]->component; 981 if (component->driver->remove_order == order) 982 soc_remove_component(component); 983 } 984 985 /* remove any CPU-side CODEC */ 986 if (cpu_dai) { 987 if (cpu_dai->component->driver->remove_order == order) 988 soc_remove_component(cpu_dai->component); 989 } 990 } 991 992 static void soc_remove_dai_links(struct snd_soc_card *card) 993 { 994 int dai, order; 995 996 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; 997 order++) { 998 for (dai = 0; dai < card->num_rtd; dai++) 999 soc_remove_link_dais(card, dai, order); 1000 } 1001 1002 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; 1003 order++) { 1004 for (dai = 0; dai < card->num_rtd; dai++) 1005 soc_remove_link_components(card, dai, order); 1006 } 1007 1008 card->num_rtd = 0; 1009 } 1010 1011 static void soc_set_name_prefix(struct snd_soc_card *card, 1012 struct snd_soc_component *component) 1013 { 1014 int i; 1015 1016 if (card->codec_conf == NULL) 1017 return; 1018 1019 for (i = 0; i < card->num_configs; i++) { 1020 struct snd_soc_codec_conf *map = &card->codec_conf[i]; 1021 if (map->of_node && component->dev->of_node != map->of_node) 1022 continue; 1023 if (map->dev_name && strcmp(component->name, map->dev_name)) 1024 continue; 1025 component->name_prefix = map->name_prefix; 1026 break; 1027 } 1028 } 1029 1030 static int soc_probe_component(struct snd_soc_card *card, 1031 struct snd_soc_component *component) 1032 { 1033 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); 1034 struct snd_soc_dai *dai; 1035 int ret; 1036 1037 if (component->probed) 1038 return 0; 1039 1040 component->card = card; 1041 dapm->card = card; 1042 soc_set_name_prefix(card, component); 1043 1044 if (!try_module_get(component->dev->driver->owner)) 1045 return -ENODEV; 1046 1047 soc_init_component_debugfs(component); 1048 1049 if (component->dapm_widgets) { 1050 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets, 1051 component->num_dapm_widgets); 1052 1053 if (ret != 0) { 1054 dev_err(component->dev, 1055 "Failed to create new controls %d\n", ret); 1056 goto err_probe; 1057 } 1058 } 1059 1060 list_for_each_entry(dai, &component->dai_list, list) { 1061 ret = snd_soc_dapm_new_dai_widgets(dapm, dai); 1062 if (ret != 0) { 1063 dev_err(component->dev, 1064 "Failed to create DAI widgets %d\n", ret); 1065 goto err_probe; 1066 } 1067 } 1068 1069 if (component->probe) { 1070 ret = component->probe(component); 1071 if (ret < 0) { 1072 dev_err(component->dev, 1073 "ASoC: failed to probe component %d\n", ret); 1074 goto err_probe; 1075 } 1076 1077 WARN(dapm->idle_bias_off && 1078 dapm->bias_level != SND_SOC_BIAS_OFF, 1079 "codec %s can not start from non-off bias with idle_bias_off==1\n", 1080 component->name); 1081 } 1082 1083 if (component->controls) 1084 snd_soc_add_component_controls(component, component->controls, 1085 component->num_controls); 1086 if (component->dapm_routes) 1087 snd_soc_dapm_add_routes(dapm, component->dapm_routes, 1088 component->num_dapm_routes); 1089 1090 component->probed = 1; 1091 list_add(&dapm->list, &card->dapm_list); 1092 1093 /* This is a HACK and will be removed soon */ 1094 if (component->codec) 1095 list_add(&component->codec->card_list, &card->codec_dev_list); 1096 1097 return 0; 1098 1099 err_probe: 1100 soc_cleanup_component_debugfs(component); 1101 module_put(component->dev->driver->owner); 1102 1103 return ret; 1104 } 1105 1106 static void rtd_release(struct device *dev) 1107 { 1108 kfree(dev); 1109 } 1110 1111 static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd, 1112 const char *name) 1113 { 1114 int ret = 0; 1115 1116 /* register the rtd device */ 1117 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL); 1118 if (!rtd->dev) 1119 return -ENOMEM; 1120 device_initialize(rtd->dev); 1121 rtd->dev->parent = rtd->card->dev; 1122 rtd->dev->release = rtd_release; 1123 dev_set_name(rtd->dev, "%s", name); 1124 dev_set_drvdata(rtd->dev, rtd); 1125 mutex_init(&rtd->pcm_mutex); 1126 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients); 1127 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients); 1128 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients); 1129 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients); 1130 ret = device_add(rtd->dev); 1131 if (ret < 0) { 1132 /* calling put_device() here to free the rtd->dev */ 1133 put_device(rtd->dev); 1134 dev_err(rtd->card->dev, 1135 "ASoC: failed to register runtime device: %d\n", ret); 1136 return ret; 1137 } 1138 rtd->dev_registered = 1; 1139 1140 if (rtd->codec) { 1141 /* add DAPM sysfs entries for this codec */ 1142 ret = snd_soc_dapm_sys_add(rtd->dev); 1143 if (ret < 0) 1144 dev_err(rtd->dev, 1145 "ASoC: failed to add codec dapm sysfs entries: %d\n", 1146 ret); 1147 1148 /* add codec sysfs entries */ 1149 ret = device_create_file(rtd->dev, &dev_attr_codec_reg); 1150 if (ret < 0) 1151 dev_err(rtd->dev, 1152 "ASoC: failed to add codec sysfs files: %d\n", 1153 ret); 1154 } 1155 1156 return 0; 1157 } 1158 1159 static int soc_probe_link_components(struct snd_soc_card *card, int num, 1160 int order) 1161 { 1162 struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 1163 struct snd_soc_platform *platform = rtd->platform; 1164 struct snd_soc_component *component; 1165 int i, ret; 1166 1167 /* probe the CPU-side component, if it is a CODEC */ 1168 component = rtd->cpu_dai->component; 1169 if (component->driver->probe_order == order) { 1170 ret = soc_probe_component(card, component); 1171 if (ret < 0) 1172 return ret; 1173 } 1174 1175 /* probe the CODEC-side components */ 1176 for (i = 0; i < rtd->num_codecs; i++) { 1177 component = rtd->codec_dais[i]->component; 1178 if (component->driver->probe_order == order) { 1179 ret = soc_probe_component(card, component); 1180 if (ret < 0) 1181 return ret; 1182 } 1183 } 1184 1185 /* probe the platform */ 1186 if (platform->component.driver->probe_order == order) { 1187 ret = soc_probe_component(card, &platform->component); 1188 if (ret < 0) 1189 return ret; 1190 } 1191 1192 return 0; 1193 } 1194 1195 static int soc_probe_dai(struct snd_soc_dai *dai, int order) 1196 { 1197 int ret; 1198 1199 if (!dai->probed && dai->driver->probe_order == order) { 1200 if (dai->driver->probe) { 1201 ret = dai->driver->probe(dai); 1202 if (ret < 0) { 1203 dev_err(dai->dev, 1204 "ASoC: failed to probe DAI %s: %d\n", 1205 dai->name, ret); 1206 return ret; 1207 } 1208 } 1209 1210 dai->probed = 1; 1211 } 1212 1213 return 0; 1214 } 1215 1216 static int soc_link_dai_widgets(struct snd_soc_card *card, 1217 struct snd_soc_dai_link *dai_link, 1218 struct snd_soc_pcm_runtime *rtd) 1219 { 1220 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 1221 struct snd_soc_dai *codec_dai = rtd->codec_dai; 1222 struct snd_soc_dapm_widget *play_w, *capture_w; 1223 int ret; 1224 1225 if (rtd->num_codecs > 1) 1226 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n"); 1227 1228 /* link the DAI widgets */ 1229 play_w = codec_dai->playback_widget; 1230 capture_w = cpu_dai->capture_widget; 1231 if (play_w && capture_w) { 1232 ret = snd_soc_dapm_new_pcm(card, dai_link->params, 1233 capture_w, play_w); 1234 if (ret != 0) { 1235 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n", 1236 play_w->name, capture_w->name, ret); 1237 return ret; 1238 } 1239 } 1240 1241 play_w = cpu_dai->playback_widget; 1242 capture_w = codec_dai->capture_widget; 1243 if (play_w && capture_w) { 1244 ret = snd_soc_dapm_new_pcm(card, dai_link->params, 1245 capture_w, play_w); 1246 if (ret != 0) { 1247 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n", 1248 play_w->name, capture_w->name, ret); 1249 return ret; 1250 } 1251 } 1252 1253 return 0; 1254 } 1255 1256 static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order) 1257 { 1258 struct snd_soc_dai_link *dai_link = &card->dai_link[num]; 1259 struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 1260 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 1261 int i, ret; 1262 1263 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n", 1264 card->name, num, order); 1265 1266 /* set default power off timeout */ 1267 rtd->pmdown_time = pmdown_time; 1268 1269 ret = soc_probe_dai(cpu_dai, order); 1270 if (ret) 1271 return ret; 1272 1273 /* probe the CODEC DAI */ 1274 for (i = 0; i < rtd->num_codecs; i++) { 1275 ret = soc_probe_dai(rtd->codec_dais[i], order); 1276 if (ret) 1277 return ret; 1278 } 1279 1280 /* complete DAI probe during last probe */ 1281 if (order != SND_SOC_COMP_ORDER_LAST) 1282 return 0; 1283 1284 /* do machine specific initialization */ 1285 if (dai_link->init) { 1286 ret = dai_link->init(rtd); 1287 if (ret < 0) { 1288 dev_err(card->dev, "ASoC: failed to init %s: %d\n", 1289 dai_link->name, ret); 1290 return ret; 1291 } 1292 } 1293 1294 ret = soc_post_component_init(rtd, dai_link->name); 1295 if (ret) 1296 return ret; 1297 1298 #ifdef CONFIG_DEBUG_FS 1299 /* add DPCM sysfs entries */ 1300 if (dai_link->dynamic) { 1301 ret = soc_dpcm_debugfs_add(rtd); 1302 if (ret < 0) { 1303 dev_err(rtd->dev, 1304 "ASoC: failed to add dpcm sysfs entries: %d\n", 1305 ret); 1306 return ret; 1307 } 1308 } 1309 #endif 1310 1311 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time); 1312 if (ret < 0) 1313 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n", 1314 ret); 1315 1316 if (cpu_dai->driver->compress_dai) { 1317 /*create compress_device"*/ 1318 ret = soc_new_compress(rtd, num); 1319 if (ret < 0) { 1320 dev_err(card->dev, "ASoC: can't create compress %s\n", 1321 dai_link->stream_name); 1322 return ret; 1323 } 1324 } else { 1325 1326 if (!dai_link->params) { 1327 /* create the pcm */ 1328 ret = soc_new_pcm(rtd, num); 1329 if (ret < 0) { 1330 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n", 1331 dai_link->stream_name, ret); 1332 return ret; 1333 } 1334 } else { 1335 INIT_DELAYED_WORK(&rtd->delayed_work, 1336 codec2codec_close_delayed_work); 1337 1338 /* link the DAI widgets */ 1339 ret = soc_link_dai_widgets(card, dai_link, rtd); 1340 if (ret) 1341 return ret; 1342 } 1343 } 1344 1345 return 0; 1346 } 1347 1348 static int soc_bind_aux_dev(struct snd_soc_card *card, int num) 1349 { 1350 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num]; 1351 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num]; 1352 const char *name = aux_dev->codec_name; 1353 1354 rtd->component = soc_find_component(aux_dev->codec_of_node, name); 1355 if (!rtd->component) { 1356 if (aux_dev->codec_of_node) 1357 name = of_node_full_name(aux_dev->codec_of_node); 1358 1359 dev_err(card->dev, "ASoC: %s not registered\n", name); 1360 return -EPROBE_DEFER; 1361 } 1362 1363 /* 1364 * Some places still reference rtd->codec, so we have to keep that 1365 * initialized if the component is a CODEC. Once all those references 1366 * have been removed, this code can be removed as well. 1367 */ 1368 rtd->codec = rtd->component->codec; 1369 1370 return 0; 1371 } 1372 1373 static int soc_probe_aux_dev(struct snd_soc_card *card, int num) 1374 { 1375 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num]; 1376 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num]; 1377 int ret; 1378 1379 ret = soc_probe_component(card, rtd->component); 1380 if (ret < 0) 1381 return ret; 1382 1383 /* do machine specific initialization */ 1384 if (aux_dev->init) { 1385 ret = aux_dev->init(rtd->component); 1386 if (ret < 0) { 1387 dev_err(card->dev, "ASoC: failed to init %s: %d\n", 1388 aux_dev->name, ret); 1389 return ret; 1390 } 1391 } 1392 1393 return soc_post_component_init(rtd, aux_dev->name); 1394 } 1395 1396 static void soc_remove_aux_dev(struct snd_soc_card *card, int num) 1397 { 1398 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num]; 1399 struct snd_soc_component *component = rtd->component; 1400 1401 /* unregister the rtd device */ 1402 if (rtd->dev_registered) { 1403 device_remove_file(rtd->dev, &dev_attr_codec_reg); 1404 device_unregister(rtd->dev); 1405 rtd->dev_registered = 0; 1406 } 1407 1408 if (component && component->probed) 1409 soc_remove_component(component); 1410 } 1411 1412 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec) 1413 { 1414 int ret; 1415 1416 if (codec->cache_init) 1417 return 0; 1418 1419 ret = snd_soc_cache_init(codec); 1420 if (ret < 0) { 1421 dev_err(codec->dev, 1422 "ASoC: Failed to set cache compression type: %d\n", 1423 ret); 1424 return ret; 1425 } 1426 codec->cache_init = 1; 1427 return 0; 1428 } 1429 1430 static int snd_soc_instantiate_card(struct snd_soc_card *card) 1431 { 1432 struct snd_soc_codec *codec; 1433 struct snd_soc_dai_link *dai_link; 1434 int ret, i, order, dai_fmt; 1435 1436 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT); 1437 1438 /* bind DAIs */ 1439 for (i = 0; i < card->num_links; i++) { 1440 ret = soc_bind_dai_link(card, i); 1441 if (ret != 0) 1442 goto base_error; 1443 } 1444 1445 /* bind aux_devs too */ 1446 for (i = 0; i < card->num_aux_devs; i++) { 1447 ret = soc_bind_aux_dev(card, i); 1448 if (ret != 0) 1449 goto base_error; 1450 } 1451 1452 /* initialize the register cache for each available codec */ 1453 list_for_each_entry(codec, &codec_list, list) { 1454 if (codec->cache_init) 1455 continue; 1456 ret = snd_soc_init_codec_cache(codec); 1457 if (ret < 0) 1458 goto base_error; 1459 } 1460 1461 /* card bind complete so register a sound card */ 1462 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, 1463 card->owner, 0, &card->snd_card); 1464 if (ret < 0) { 1465 dev_err(card->dev, 1466 "ASoC: can't create sound card for card %s: %d\n", 1467 card->name, ret); 1468 goto base_error; 1469 } 1470 1471 card->dapm.bias_level = SND_SOC_BIAS_OFF; 1472 card->dapm.dev = card->dev; 1473 card->dapm.card = card; 1474 list_add(&card->dapm.list, &card->dapm_list); 1475 1476 #ifdef CONFIG_DEBUG_FS 1477 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root); 1478 #endif 1479 1480 #ifdef CONFIG_PM_SLEEP 1481 /* deferred resume work */ 1482 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred); 1483 #endif 1484 1485 if (card->dapm_widgets) 1486 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets, 1487 card->num_dapm_widgets); 1488 1489 /* initialise the sound card only once */ 1490 if (card->probe) { 1491 ret = card->probe(card); 1492 if (ret < 0) 1493 goto card_probe_error; 1494 } 1495 1496 /* probe all components used by DAI links on this card */ 1497 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; 1498 order++) { 1499 for (i = 0; i < card->num_links; i++) { 1500 ret = soc_probe_link_components(card, i, order); 1501 if (ret < 0) { 1502 dev_err(card->dev, 1503 "ASoC: failed to instantiate card %d\n", 1504 ret); 1505 goto probe_dai_err; 1506 } 1507 } 1508 } 1509 1510 /* probe all DAI links on this card */ 1511 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; 1512 order++) { 1513 for (i = 0; i < card->num_links; i++) { 1514 ret = soc_probe_link_dais(card, i, order); 1515 if (ret < 0) { 1516 dev_err(card->dev, 1517 "ASoC: failed to instantiate card %d\n", 1518 ret); 1519 goto probe_dai_err; 1520 } 1521 } 1522 } 1523 1524 for (i = 0; i < card->num_aux_devs; i++) { 1525 ret = soc_probe_aux_dev(card, i); 1526 if (ret < 0) { 1527 dev_err(card->dev, 1528 "ASoC: failed to add auxiliary devices %d\n", 1529 ret); 1530 goto probe_aux_dev_err; 1531 } 1532 } 1533 1534 snd_soc_dapm_link_dai_widgets(card); 1535 snd_soc_dapm_connect_dai_link_widgets(card); 1536 1537 if (card->controls) 1538 snd_soc_add_card_controls(card, card->controls, card->num_controls); 1539 1540 if (card->dapm_routes) 1541 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes, 1542 card->num_dapm_routes); 1543 1544 for (i = 0; i < card->num_links; i++) { 1545 struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 1546 dai_link = &card->dai_link[i]; 1547 dai_fmt = dai_link->dai_fmt; 1548 1549 if (dai_fmt) { 1550 struct snd_soc_dai **codec_dais = rtd->codec_dais; 1551 int j; 1552 1553 for (j = 0; j < rtd->num_codecs; j++) { 1554 struct snd_soc_dai *codec_dai = codec_dais[j]; 1555 1556 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt); 1557 if (ret != 0 && ret != -ENOTSUPP) 1558 dev_warn(codec_dai->dev, 1559 "ASoC: Failed to set DAI format: %d\n", 1560 ret); 1561 } 1562 } 1563 1564 /* If this is a regular CPU link there will be a platform */ 1565 if (dai_fmt && 1566 (dai_link->platform_name || dai_link->platform_of_node)) { 1567 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai, 1568 dai_fmt); 1569 if (ret != 0 && ret != -ENOTSUPP) 1570 dev_warn(card->rtd[i].cpu_dai->dev, 1571 "ASoC: Failed to set DAI format: %d\n", 1572 ret); 1573 } else if (dai_fmt) { 1574 /* Flip the polarity for the "CPU" end */ 1575 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK; 1576 switch (dai_link->dai_fmt & 1577 SND_SOC_DAIFMT_MASTER_MASK) { 1578 case SND_SOC_DAIFMT_CBM_CFM: 1579 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS; 1580 break; 1581 case SND_SOC_DAIFMT_CBM_CFS: 1582 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM; 1583 break; 1584 case SND_SOC_DAIFMT_CBS_CFM: 1585 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS; 1586 break; 1587 case SND_SOC_DAIFMT_CBS_CFS: 1588 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM; 1589 break; 1590 } 1591 1592 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai, 1593 dai_fmt); 1594 if (ret != 0 && ret != -ENOTSUPP) 1595 dev_warn(card->rtd[i].cpu_dai->dev, 1596 "ASoC: Failed to set DAI format: %d\n", 1597 ret); 1598 } 1599 } 1600 1601 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname), 1602 "%s", card->name); 1603 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname), 1604 "%s", card->long_name ? card->long_name : card->name); 1605 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver), 1606 "%s", card->driver_name ? card->driver_name : card->name); 1607 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) { 1608 switch (card->snd_card->driver[i]) { 1609 case '_': 1610 case '-': 1611 case '\0': 1612 break; 1613 default: 1614 if (!isalnum(card->snd_card->driver[i])) 1615 card->snd_card->driver[i] = '_'; 1616 break; 1617 } 1618 } 1619 1620 if (card->late_probe) { 1621 ret = card->late_probe(card); 1622 if (ret < 0) { 1623 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n", 1624 card->name, ret); 1625 goto probe_aux_dev_err; 1626 } 1627 } 1628 1629 snd_soc_dapm_new_widgets(card); 1630 1631 ret = snd_card_register(card->snd_card); 1632 if (ret < 0) { 1633 dev_err(card->dev, "ASoC: failed to register soundcard %d\n", 1634 ret); 1635 goto probe_aux_dev_err; 1636 } 1637 1638 card->instantiated = 1; 1639 snd_soc_dapm_sync(&card->dapm); 1640 mutex_unlock(&card->mutex); 1641 1642 return 0; 1643 1644 probe_aux_dev_err: 1645 for (i = 0; i < card->num_aux_devs; i++) 1646 soc_remove_aux_dev(card, i); 1647 1648 probe_dai_err: 1649 soc_remove_dai_links(card); 1650 1651 card_probe_error: 1652 if (card->remove) 1653 card->remove(card); 1654 1655 snd_card_free(card->snd_card); 1656 1657 base_error: 1658 mutex_unlock(&card->mutex); 1659 1660 return ret; 1661 } 1662 1663 /* probes a new socdev */ 1664 static int soc_probe(struct platform_device *pdev) 1665 { 1666 struct snd_soc_card *card = platform_get_drvdata(pdev); 1667 1668 /* 1669 * no card, so machine driver should be registering card 1670 * we should not be here in that case so ret error 1671 */ 1672 if (!card) 1673 return -EINVAL; 1674 1675 dev_warn(&pdev->dev, 1676 "ASoC: machine %s should use snd_soc_register_card()\n", 1677 card->name); 1678 1679 /* Bodge while we unpick instantiation */ 1680 card->dev = &pdev->dev; 1681 1682 return snd_soc_register_card(card); 1683 } 1684 1685 static int soc_cleanup_card_resources(struct snd_soc_card *card) 1686 { 1687 int i; 1688 1689 /* make sure any delayed work runs */ 1690 for (i = 0; i < card->num_rtd; i++) { 1691 struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 1692 flush_delayed_work(&rtd->delayed_work); 1693 } 1694 1695 /* remove auxiliary devices */ 1696 for (i = 0; i < card->num_aux_devs; i++) 1697 soc_remove_aux_dev(card, i); 1698 1699 /* remove and free each DAI */ 1700 soc_remove_dai_links(card); 1701 1702 soc_cleanup_card_debugfs(card); 1703 1704 /* remove the card */ 1705 if (card->remove) 1706 card->remove(card); 1707 1708 snd_soc_dapm_free(&card->dapm); 1709 1710 snd_card_free(card->snd_card); 1711 return 0; 1712 1713 } 1714 1715 /* removes a socdev */ 1716 static int soc_remove(struct platform_device *pdev) 1717 { 1718 struct snd_soc_card *card = platform_get_drvdata(pdev); 1719 1720 snd_soc_unregister_card(card); 1721 return 0; 1722 } 1723 1724 int snd_soc_poweroff(struct device *dev) 1725 { 1726 struct snd_soc_card *card = dev_get_drvdata(dev); 1727 int i; 1728 1729 if (!card->instantiated) 1730 return 0; 1731 1732 /* Flush out pmdown_time work - we actually do want to run it 1733 * now, we're shutting down so no imminent restart. */ 1734 for (i = 0; i < card->num_rtd; i++) { 1735 struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 1736 flush_delayed_work(&rtd->delayed_work); 1737 } 1738 1739 snd_soc_dapm_shutdown(card); 1740 1741 /* deactivate pins to sleep state */ 1742 for (i = 0; i < card->num_rtd; i++) { 1743 struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 1744 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 1745 int j; 1746 1747 pinctrl_pm_select_sleep_state(cpu_dai->dev); 1748 for (j = 0; j < rtd->num_codecs; j++) { 1749 struct snd_soc_dai *codec_dai = rtd->codec_dais[j]; 1750 pinctrl_pm_select_sleep_state(codec_dai->dev); 1751 } 1752 } 1753 1754 return 0; 1755 } 1756 EXPORT_SYMBOL_GPL(snd_soc_poweroff); 1757 1758 const struct dev_pm_ops snd_soc_pm_ops = { 1759 .suspend = snd_soc_suspend, 1760 .resume = snd_soc_resume, 1761 .freeze = snd_soc_suspend, 1762 .thaw = snd_soc_resume, 1763 .poweroff = snd_soc_poweroff, 1764 .restore = snd_soc_resume, 1765 }; 1766 EXPORT_SYMBOL_GPL(snd_soc_pm_ops); 1767 1768 /* ASoC platform driver */ 1769 static struct platform_driver soc_driver = { 1770 .driver = { 1771 .name = "soc-audio", 1772 .pm = &snd_soc_pm_ops, 1773 }, 1774 .probe = soc_probe, 1775 .remove = soc_remove, 1776 }; 1777 1778 /** 1779 * snd_soc_cnew - create new control 1780 * @_template: control template 1781 * @data: control private data 1782 * @long_name: control long name 1783 * @prefix: control name prefix 1784 * 1785 * Create a new mixer control from a template control. 1786 * 1787 * Returns 0 for success, else error. 1788 */ 1789 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template, 1790 void *data, const char *long_name, 1791 const char *prefix) 1792 { 1793 struct snd_kcontrol_new template; 1794 struct snd_kcontrol *kcontrol; 1795 char *name = NULL; 1796 1797 memcpy(&template, _template, sizeof(template)); 1798 template.index = 0; 1799 1800 if (!long_name) 1801 long_name = template.name; 1802 1803 if (prefix) { 1804 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name); 1805 if (!name) 1806 return NULL; 1807 1808 template.name = name; 1809 } else { 1810 template.name = long_name; 1811 } 1812 1813 kcontrol = snd_ctl_new1(&template, data); 1814 1815 kfree(name); 1816 1817 return kcontrol; 1818 } 1819 EXPORT_SYMBOL_GPL(snd_soc_cnew); 1820 1821 static int snd_soc_add_controls(struct snd_card *card, struct device *dev, 1822 const struct snd_kcontrol_new *controls, int num_controls, 1823 const char *prefix, void *data) 1824 { 1825 int err, i; 1826 1827 for (i = 0; i < num_controls; i++) { 1828 const struct snd_kcontrol_new *control = &controls[i]; 1829 err = snd_ctl_add(card, snd_soc_cnew(control, data, 1830 control->name, prefix)); 1831 if (err < 0) { 1832 dev_err(dev, "ASoC: Failed to add %s: %d\n", 1833 control->name, err); 1834 return err; 1835 } 1836 } 1837 1838 return 0; 1839 } 1840 1841 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card, 1842 const char *name) 1843 { 1844 struct snd_card *card = soc_card->snd_card; 1845 struct snd_kcontrol *kctl; 1846 1847 if (unlikely(!name)) 1848 return NULL; 1849 1850 list_for_each_entry(kctl, &card->controls, list) 1851 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) 1852 return kctl; 1853 return NULL; 1854 } 1855 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol); 1856 1857 /** 1858 * snd_soc_add_component_controls - Add an array of controls to a component. 1859 * 1860 * @component: Component to add controls to 1861 * @controls: Array of controls to add 1862 * @num_controls: Number of elements in the array 1863 * 1864 * Return: 0 for success, else error. 1865 */ 1866 int snd_soc_add_component_controls(struct snd_soc_component *component, 1867 const struct snd_kcontrol_new *controls, unsigned int num_controls) 1868 { 1869 struct snd_card *card = component->card->snd_card; 1870 1871 return snd_soc_add_controls(card, component->dev, controls, 1872 num_controls, component->name_prefix, component); 1873 } 1874 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls); 1875 1876 /** 1877 * snd_soc_add_codec_controls - add an array of controls to a codec. 1878 * Convenience function to add a list of controls. Many codecs were 1879 * duplicating this code. 1880 * 1881 * @codec: codec to add controls to 1882 * @controls: array of controls to add 1883 * @num_controls: number of elements in the array 1884 * 1885 * Return 0 for success, else error. 1886 */ 1887 int snd_soc_add_codec_controls(struct snd_soc_codec *codec, 1888 const struct snd_kcontrol_new *controls, unsigned int num_controls) 1889 { 1890 return snd_soc_add_component_controls(&codec->component, controls, 1891 num_controls); 1892 } 1893 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls); 1894 1895 /** 1896 * snd_soc_add_platform_controls - add an array of controls to a platform. 1897 * Convenience function to add a list of controls. 1898 * 1899 * @platform: platform to add controls to 1900 * @controls: array of controls to add 1901 * @num_controls: number of elements in the array 1902 * 1903 * Return 0 for success, else error. 1904 */ 1905 int snd_soc_add_platform_controls(struct snd_soc_platform *platform, 1906 const struct snd_kcontrol_new *controls, unsigned int num_controls) 1907 { 1908 return snd_soc_add_component_controls(&platform->component, controls, 1909 num_controls); 1910 } 1911 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls); 1912 1913 /** 1914 * snd_soc_add_card_controls - add an array of controls to a SoC card. 1915 * Convenience function to add a list of controls. 1916 * 1917 * @soc_card: SoC card to add controls to 1918 * @controls: array of controls to add 1919 * @num_controls: number of elements in the array 1920 * 1921 * Return 0 for success, else error. 1922 */ 1923 int snd_soc_add_card_controls(struct snd_soc_card *soc_card, 1924 const struct snd_kcontrol_new *controls, int num_controls) 1925 { 1926 struct snd_card *card = soc_card->snd_card; 1927 1928 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls, 1929 NULL, soc_card); 1930 } 1931 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls); 1932 1933 /** 1934 * snd_soc_add_dai_controls - add an array of controls to a DAI. 1935 * Convienience function to add a list of controls. 1936 * 1937 * @dai: DAI to add controls to 1938 * @controls: array of controls to add 1939 * @num_controls: number of elements in the array 1940 * 1941 * Return 0 for success, else error. 1942 */ 1943 int snd_soc_add_dai_controls(struct snd_soc_dai *dai, 1944 const struct snd_kcontrol_new *controls, int num_controls) 1945 { 1946 struct snd_card *card = dai->component->card->snd_card; 1947 1948 return snd_soc_add_controls(card, dai->dev, controls, num_controls, 1949 NULL, dai); 1950 } 1951 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls); 1952 1953 /** 1954 * snd_soc_dai_set_sysclk - configure DAI system or master clock. 1955 * @dai: DAI 1956 * @clk_id: DAI specific clock ID 1957 * @freq: new clock frequency in Hz 1958 * @dir: new clock direction - input/output. 1959 * 1960 * Configures the DAI master (MCLK) or system (SYSCLK) clocking. 1961 */ 1962 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id, 1963 unsigned int freq, int dir) 1964 { 1965 if (dai->driver && dai->driver->ops->set_sysclk) 1966 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir); 1967 else if (dai->codec && dai->codec->driver->set_sysclk) 1968 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0, 1969 freq, dir); 1970 else 1971 return -ENOTSUPP; 1972 } 1973 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk); 1974 1975 /** 1976 * snd_soc_codec_set_sysclk - configure CODEC system or master clock. 1977 * @codec: CODEC 1978 * @clk_id: DAI specific clock ID 1979 * @source: Source for the clock 1980 * @freq: new clock frequency in Hz 1981 * @dir: new clock direction - input/output. 1982 * 1983 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking. 1984 */ 1985 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id, 1986 int source, unsigned int freq, int dir) 1987 { 1988 if (codec->driver->set_sysclk) 1989 return codec->driver->set_sysclk(codec, clk_id, source, 1990 freq, dir); 1991 else 1992 return -ENOTSUPP; 1993 } 1994 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk); 1995 1996 /** 1997 * snd_soc_dai_set_clkdiv - configure DAI clock dividers. 1998 * @dai: DAI 1999 * @div_id: DAI specific clock divider ID 2000 * @div: new clock divisor. 2001 * 2002 * Configures the clock dividers. This is used to derive the best DAI bit and 2003 * frame clocks from the system or master clock. It's best to set the DAI bit 2004 * and frame clocks as low as possible to save system power. 2005 */ 2006 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai, 2007 int div_id, int div) 2008 { 2009 if (dai->driver && dai->driver->ops->set_clkdiv) 2010 return dai->driver->ops->set_clkdiv(dai, div_id, div); 2011 else 2012 return -EINVAL; 2013 } 2014 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv); 2015 2016 /** 2017 * snd_soc_dai_set_pll - configure DAI PLL. 2018 * @dai: DAI 2019 * @pll_id: DAI specific PLL ID 2020 * @source: DAI specific source for the PLL 2021 * @freq_in: PLL input clock frequency in Hz 2022 * @freq_out: requested PLL output clock frequency in Hz 2023 * 2024 * Configures and enables PLL to generate output clock based on input clock. 2025 */ 2026 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source, 2027 unsigned int freq_in, unsigned int freq_out) 2028 { 2029 if (dai->driver && dai->driver->ops->set_pll) 2030 return dai->driver->ops->set_pll(dai, pll_id, source, 2031 freq_in, freq_out); 2032 else if (dai->codec && dai->codec->driver->set_pll) 2033 return dai->codec->driver->set_pll(dai->codec, pll_id, source, 2034 freq_in, freq_out); 2035 else 2036 return -EINVAL; 2037 } 2038 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll); 2039 2040 /* 2041 * snd_soc_codec_set_pll - configure codec PLL. 2042 * @codec: CODEC 2043 * @pll_id: DAI specific PLL ID 2044 * @source: DAI specific source for the PLL 2045 * @freq_in: PLL input clock frequency in Hz 2046 * @freq_out: requested PLL output clock frequency in Hz 2047 * 2048 * Configures and enables PLL to generate output clock based on input clock. 2049 */ 2050 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source, 2051 unsigned int freq_in, unsigned int freq_out) 2052 { 2053 if (codec->driver->set_pll) 2054 return codec->driver->set_pll(codec, pll_id, source, 2055 freq_in, freq_out); 2056 else 2057 return -EINVAL; 2058 } 2059 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll); 2060 2061 /** 2062 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio. 2063 * @dai: DAI 2064 * @ratio Ratio of BCLK to Sample rate. 2065 * 2066 * Configures the DAI for a preset BCLK to sample rate ratio. 2067 */ 2068 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio) 2069 { 2070 if (dai->driver && dai->driver->ops->set_bclk_ratio) 2071 return dai->driver->ops->set_bclk_ratio(dai, ratio); 2072 else 2073 return -EINVAL; 2074 } 2075 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio); 2076 2077 /** 2078 * snd_soc_dai_set_fmt - configure DAI hardware audio format. 2079 * @dai: DAI 2080 * @fmt: SND_SOC_DAIFMT_ format value. 2081 * 2082 * Configures the DAI hardware format and clocking. 2083 */ 2084 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) 2085 { 2086 if (dai->driver == NULL) 2087 return -EINVAL; 2088 if (dai->driver->ops->set_fmt == NULL) 2089 return -ENOTSUPP; 2090 return dai->driver->ops->set_fmt(dai, fmt); 2091 } 2092 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt); 2093 2094 /** 2095 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask. 2096 * @slots: Number of slots in use. 2097 * @tx_mask: bitmask representing active TX slots. 2098 * @rx_mask: bitmask representing active RX slots. 2099 * 2100 * Generates the TDM tx and rx slot default masks for DAI. 2101 */ 2102 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots, 2103 unsigned int *tx_mask, 2104 unsigned int *rx_mask) 2105 { 2106 if (*tx_mask || *rx_mask) 2107 return 0; 2108 2109 if (!slots) 2110 return -EINVAL; 2111 2112 *tx_mask = (1 << slots) - 1; 2113 *rx_mask = (1 << slots) - 1; 2114 2115 return 0; 2116 } 2117 2118 /** 2119 * snd_soc_dai_set_tdm_slot - configure DAI TDM. 2120 * @dai: DAI 2121 * @tx_mask: bitmask representing active TX slots. 2122 * @rx_mask: bitmask representing active RX slots. 2123 * @slots: Number of slots in use. 2124 * @slot_width: Width in bits for each slot. 2125 * 2126 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI 2127 * specific. 2128 */ 2129 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai, 2130 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width) 2131 { 2132 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask) 2133 dai->driver->ops->xlate_tdm_slot_mask(slots, 2134 &tx_mask, &rx_mask); 2135 else 2136 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask); 2137 2138 dai->tx_mask = tx_mask; 2139 dai->rx_mask = rx_mask; 2140 2141 if (dai->driver && dai->driver->ops->set_tdm_slot) 2142 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask, 2143 slots, slot_width); 2144 else 2145 return -ENOTSUPP; 2146 } 2147 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot); 2148 2149 /** 2150 * snd_soc_dai_set_channel_map - configure DAI audio channel map 2151 * @dai: DAI 2152 * @tx_num: how many TX channels 2153 * @tx_slot: pointer to an array which imply the TX slot number channel 2154 * 0~num-1 uses 2155 * @rx_num: how many RX channels 2156 * @rx_slot: pointer to an array which imply the RX slot number channel 2157 * 0~num-1 uses 2158 * 2159 * configure the relationship between channel number and TDM slot number. 2160 */ 2161 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai, 2162 unsigned int tx_num, unsigned int *tx_slot, 2163 unsigned int rx_num, unsigned int *rx_slot) 2164 { 2165 if (dai->driver && dai->driver->ops->set_channel_map) 2166 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot, 2167 rx_num, rx_slot); 2168 else 2169 return -EINVAL; 2170 } 2171 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map); 2172 2173 /** 2174 * snd_soc_dai_set_tristate - configure DAI system or master clock. 2175 * @dai: DAI 2176 * @tristate: tristate enable 2177 * 2178 * Tristates the DAI so that others can use it. 2179 */ 2180 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate) 2181 { 2182 if (dai->driver && dai->driver->ops->set_tristate) 2183 return dai->driver->ops->set_tristate(dai, tristate); 2184 else 2185 return -EINVAL; 2186 } 2187 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate); 2188 2189 /** 2190 * snd_soc_dai_digital_mute - configure DAI system or master clock. 2191 * @dai: DAI 2192 * @mute: mute enable 2193 * @direction: stream to mute 2194 * 2195 * Mutes the DAI DAC. 2196 */ 2197 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute, 2198 int direction) 2199 { 2200 if (!dai->driver) 2201 return -ENOTSUPP; 2202 2203 if (dai->driver->ops->mute_stream) 2204 return dai->driver->ops->mute_stream(dai, mute, direction); 2205 else if (direction == SNDRV_PCM_STREAM_PLAYBACK && 2206 dai->driver->ops->digital_mute) 2207 return dai->driver->ops->digital_mute(dai, mute); 2208 else 2209 return -ENOTSUPP; 2210 } 2211 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute); 2212 2213 static int snd_soc_init_multicodec(struct snd_soc_card *card, 2214 struct snd_soc_dai_link *dai_link) 2215 { 2216 /* Legacy codec/codec_dai link is a single entry in multicodec */ 2217 if (dai_link->codec_name || dai_link->codec_of_node || 2218 dai_link->codec_dai_name) { 2219 dai_link->num_codecs = 1; 2220 2221 dai_link->codecs = devm_kzalloc(card->dev, 2222 sizeof(struct snd_soc_dai_link_component), 2223 GFP_KERNEL); 2224 if (!dai_link->codecs) 2225 return -ENOMEM; 2226 2227 dai_link->codecs[0].name = dai_link->codec_name; 2228 dai_link->codecs[0].of_node = dai_link->codec_of_node; 2229 dai_link->codecs[0].dai_name = dai_link->codec_dai_name; 2230 } 2231 2232 if (!dai_link->codecs) { 2233 dev_err(card->dev, "ASoC: DAI link has no CODECs\n"); 2234 return -EINVAL; 2235 } 2236 2237 return 0; 2238 } 2239 2240 /** 2241 * snd_soc_register_card - Register a card with the ASoC core 2242 * 2243 * @card: Card to register 2244 * 2245 */ 2246 int snd_soc_register_card(struct snd_soc_card *card) 2247 { 2248 int i, j, ret; 2249 2250 if (!card->name || !card->dev) 2251 return -EINVAL; 2252 2253 for (i = 0; i < card->num_links; i++) { 2254 struct snd_soc_dai_link *link = &card->dai_link[i]; 2255 2256 ret = snd_soc_init_multicodec(card, link); 2257 if (ret) { 2258 dev_err(card->dev, "ASoC: failed to init multicodec\n"); 2259 return ret; 2260 } 2261 2262 for (j = 0; j < link->num_codecs; j++) { 2263 /* 2264 * Codec must be specified by 1 of name or OF node, 2265 * not both or neither. 2266 */ 2267 if (!!link->codecs[j].name == 2268 !!link->codecs[j].of_node) { 2269 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n", 2270 link->name); 2271 return -EINVAL; 2272 } 2273 /* Codec DAI name must be specified */ 2274 if (!link->codecs[j].dai_name) { 2275 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n", 2276 link->name); 2277 return -EINVAL; 2278 } 2279 } 2280 2281 /* 2282 * Platform may be specified by either name or OF node, but 2283 * can be left unspecified, and a dummy platform will be used. 2284 */ 2285 if (link->platform_name && link->platform_of_node) { 2286 dev_err(card->dev, 2287 "ASoC: Both platform name/of_node are set for %s\n", 2288 link->name); 2289 return -EINVAL; 2290 } 2291 2292 /* 2293 * CPU device may be specified by either name or OF node, but 2294 * can be left unspecified, and will be matched based on DAI 2295 * name alone.. 2296 */ 2297 if (link->cpu_name && link->cpu_of_node) { 2298 dev_err(card->dev, 2299 "ASoC: Neither/both cpu name/of_node are set for %s\n", 2300 link->name); 2301 return -EINVAL; 2302 } 2303 /* 2304 * At least one of CPU DAI name or CPU device name/node must be 2305 * specified 2306 */ 2307 if (!link->cpu_dai_name && 2308 !(link->cpu_name || link->cpu_of_node)) { 2309 dev_err(card->dev, 2310 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n", 2311 link->name); 2312 return -EINVAL; 2313 } 2314 } 2315 2316 dev_set_drvdata(card->dev, card); 2317 2318 snd_soc_initialize_card_lists(card); 2319 2320 soc_init_card_debugfs(card); 2321 2322 card->rtd = devm_kzalloc(card->dev, 2323 sizeof(struct snd_soc_pcm_runtime) * 2324 (card->num_links + card->num_aux_devs), 2325 GFP_KERNEL); 2326 if (card->rtd == NULL) 2327 return -ENOMEM; 2328 card->num_rtd = 0; 2329 card->rtd_aux = &card->rtd[card->num_links]; 2330 2331 for (i = 0; i < card->num_links; i++) { 2332 card->rtd[i].card = card; 2333 card->rtd[i].dai_link = &card->dai_link[i]; 2334 card->rtd[i].codec_dais = devm_kzalloc(card->dev, 2335 sizeof(struct snd_soc_dai *) * 2336 (card->rtd[i].dai_link->num_codecs), 2337 GFP_KERNEL); 2338 if (card->rtd[i].codec_dais == NULL) 2339 return -ENOMEM; 2340 } 2341 2342 for (i = 0; i < card->num_aux_devs; i++) 2343 card->rtd_aux[i].card = card; 2344 2345 INIT_LIST_HEAD(&card->dapm_dirty); 2346 card->instantiated = 0; 2347 mutex_init(&card->mutex); 2348 mutex_init(&card->dapm_mutex); 2349 2350 ret = snd_soc_instantiate_card(card); 2351 if (ret != 0) 2352 soc_cleanup_card_debugfs(card); 2353 2354 /* deactivate pins to sleep state */ 2355 for (i = 0; i < card->num_rtd; i++) { 2356 struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 2357 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 2358 int j; 2359 2360 for (j = 0; j < rtd->num_codecs; j++) { 2361 struct snd_soc_dai *codec_dai = rtd->codec_dais[j]; 2362 if (!codec_dai->active) 2363 pinctrl_pm_select_sleep_state(codec_dai->dev); 2364 } 2365 2366 if (!cpu_dai->active) 2367 pinctrl_pm_select_sleep_state(cpu_dai->dev); 2368 } 2369 2370 return ret; 2371 } 2372 EXPORT_SYMBOL_GPL(snd_soc_register_card); 2373 2374 /** 2375 * snd_soc_unregister_card - Unregister a card with the ASoC core 2376 * 2377 * @card: Card to unregister 2378 * 2379 */ 2380 int snd_soc_unregister_card(struct snd_soc_card *card) 2381 { 2382 if (card->instantiated) { 2383 card->instantiated = false; 2384 snd_soc_dapm_shutdown(card); 2385 soc_cleanup_card_resources(card); 2386 } 2387 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name); 2388 2389 return 0; 2390 } 2391 EXPORT_SYMBOL_GPL(snd_soc_unregister_card); 2392 2393 /* 2394 * Simplify DAI link configuration by removing ".-1" from device names 2395 * and sanitizing names. 2396 */ 2397 static char *fmt_single_name(struct device *dev, int *id) 2398 { 2399 char *found, name[NAME_SIZE]; 2400 int id1, id2; 2401 2402 if (dev_name(dev) == NULL) 2403 return NULL; 2404 2405 strlcpy(name, dev_name(dev), NAME_SIZE); 2406 2407 /* are we a "%s.%d" name (platform and SPI components) */ 2408 found = strstr(name, dev->driver->name); 2409 if (found) { 2410 /* get ID */ 2411 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) { 2412 2413 /* discard ID from name if ID == -1 */ 2414 if (*id == -1) 2415 found[strlen(dev->driver->name)] = '\0'; 2416 } 2417 2418 } else { 2419 /* I2C component devices are named "bus-addr" */ 2420 if (sscanf(name, "%x-%x", &id1, &id2) == 2) { 2421 char tmp[NAME_SIZE]; 2422 2423 /* create unique ID number from I2C addr and bus */ 2424 *id = ((id1 & 0xffff) << 16) + id2; 2425 2426 /* sanitize component name for DAI link creation */ 2427 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name); 2428 strlcpy(name, tmp, NAME_SIZE); 2429 } else 2430 *id = 0; 2431 } 2432 2433 return kstrdup(name, GFP_KERNEL); 2434 } 2435 2436 /* 2437 * Simplify DAI link naming for single devices with multiple DAIs by removing 2438 * any ".-1" and using the DAI name (instead of device name). 2439 */ 2440 static inline char *fmt_multiple_name(struct device *dev, 2441 struct snd_soc_dai_driver *dai_drv) 2442 { 2443 if (dai_drv->name == NULL) { 2444 dev_err(dev, 2445 "ASoC: error - multiple DAI %s registered with no name\n", 2446 dev_name(dev)); 2447 return NULL; 2448 } 2449 2450 return kstrdup(dai_drv->name, GFP_KERNEL); 2451 } 2452 2453 /** 2454 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core 2455 * 2456 * @component: The component for which the DAIs should be unregistered 2457 */ 2458 static void snd_soc_unregister_dais(struct snd_soc_component *component) 2459 { 2460 struct snd_soc_dai *dai, *_dai; 2461 2462 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) { 2463 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n", 2464 dai->name); 2465 list_del(&dai->list); 2466 kfree(dai->name); 2467 kfree(dai); 2468 } 2469 } 2470 2471 /** 2472 * snd_soc_register_dais - Register a DAI with the ASoC core 2473 * 2474 * @component: The component the DAIs are registered for 2475 * @dai_drv: DAI driver to use for the DAIs 2476 * @count: Number of DAIs 2477 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the 2478 * parent's name. 2479 */ 2480 static int snd_soc_register_dais(struct snd_soc_component *component, 2481 struct snd_soc_dai_driver *dai_drv, size_t count, 2482 bool legacy_dai_naming) 2483 { 2484 struct device *dev = component->dev; 2485 struct snd_soc_dai *dai; 2486 unsigned int i; 2487 int ret; 2488 2489 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count); 2490 2491 component->dai_drv = dai_drv; 2492 component->num_dai = count; 2493 2494 for (i = 0; i < count; i++) { 2495 2496 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL); 2497 if (dai == NULL) { 2498 ret = -ENOMEM; 2499 goto err; 2500 } 2501 2502 /* 2503 * Back in the old days when we still had component-less DAIs, 2504 * instead of having a static name, component-less DAIs would 2505 * inherit the name of the parent device so it is possible to 2506 * register multiple instances of the DAI. We still need to keep 2507 * the same naming style even though those DAIs are not 2508 * component-less anymore. 2509 */ 2510 if (count == 1 && legacy_dai_naming) { 2511 dai->name = fmt_single_name(dev, &dai->id); 2512 } else { 2513 dai->name = fmt_multiple_name(dev, &dai_drv[i]); 2514 if (dai_drv[i].id) 2515 dai->id = dai_drv[i].id; 2516 else 2517 dai->id = i; 2518 } 2519 if (dai->name == NULL) { 2520 kfree(dai); 2521 ret = -ENOMEM; 2522 goto err; 2523 } 2524 2525 dai->component = component; 2526 dai->dev = dev; 2527 dai->driver = &dai_drv[i]; 2528 if (!dai->driver->ops) 2529 dai->driver->ops = &null_dai_ops; 2530 2531 list_add(&dai->list, &component->dai_list); 2532 2533 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name); 2534 } 2535 2536 return 0; 2537 2538 err: 2539 snd_soc_unregister_dais(component); 2540 2541 return ret; 2542 } 2543 2544 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm, 2545 enum snd_soc_dapm_type type, int subseq) 2546 { 2547 struct snd_soc_component *component = dapm->component; 2548 2549 component->driver->seq_notifier(component, type, subseq); 2550 } 2551 2552 static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm, 2553 int event) 2554 { 2555 struct snd_soc_component *component = dapm->component; 2556 2557 return component->driver->stream_event(component, event); 2558 } 2559 2560 static int snd_soc_component_initialize(struct snd_soc_component *component, 2561 const struct snd_soc_component_driver *driver, struct device *dev) 2562 { 2563 struct snd_soc_dapm_context *dapm; 2564 2565 component->name = fmt_single_name(dev, &component->id); 2566 if (!component->name) { 2567 dev_err(dev, "ASoC: Failed to allocate name\n"); 2568 return -ENOMEM; 2569 } 2570 2571 component->dev = dev; 2572 component->driver = driver; 2573 component->probe = component->driver->probe; 2574 component->remove = component->driver->remove; 2575 2576 if (!component->dapm_ptr) 2577 component->dapm_ptr = &component->dapm; 2578 2579 dapm = component->dapm_ptr; 2580 dapm->dev = dev; 2581 dapm->component = component; 2582 dapm->bias_level = SND_SOC_BIAS_OFF; 2583 dapm->idle_bias_off = true; 2584 if (driver->seq_notifier) 2585 dapm->seq_notifier = snd_soc_component_seq_notifier; 2586 if (driver->stream_event) 2587 dapm->stream_event = snd_soc_component_stream_event; 2588 2589 component->controls = driver->controls; 2590 component->num_controls = driver->num_controls; 2591 component->dapm_widgets = driver->dapm_widgets; 2592 component->num_dapm_widgets = driver->num_dapm_widgets; 2593 component->dapm_routes = driver->dapm_routes; 2594 component->num_dapm_routes = driver->num_dapm_routes; 2595 2596 INIT_LIST_HEAD(&component->dai_list); 2597 mutex_init(&component->io_mutex); 2598 2599 return 0; 2600 } 2601 2602 static void snd_soc_component_setup_regmap(struct snd_soc_component *component) 2603 { 2604 int val_bytes = regmap_get_val_bytes(component->regmap); 2605 2606 /* Errors are legitimate for non-integer byte multiples */ 2607 if (val_bytes > 0) 2608 component->val_bytes = val_bytes; 2609 } 2610 2611 #ifdef CONFIG_REGMAP 2612 2613 /** 2614 * snd_soc_component_init_regmap() - Initialize regmap instance for the component 2615 * @component: The component for which to initialize the regmap instance 2616 * @regmap: The regmap instance that should be used by the component 2617 * 2618 * This function allows deferred assignment of the regmap instance that is 2619 * associated with the component. Only use this if the regmap instance is not 2620 * yet ready when the component is registered. The function must also be called 2621 * before the first IO attempt of the component. 2622 */ 2623 void snd_soc_component_init_regmap(struct snd_soc_component *component, 2624 struct regmap *regmap) 2625 { 2626 component->regmap = regmap; 2627 snd_soc_component_setup_regmap(component); 2628 } 2629 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap); 2630 2631 /** 2632 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component 2633 * @component: The component for which to de-initialize the regmap instance 2634 * 2635 * Calls regmap_exit() on the regmap instance associated to the component and 2636 * removes the regmap instance from the component. 2637 * 2638 * This function should only be used if snd_soc_component_init_regmap() was used 2639 * to initialize the regmap instance. 2640 */ 2641 void snd_soc_component_exit_regmap(struct snd_soc_component *component) 2642 { 2643 regmap_exit(component->regmap); 2644 component->regmap = NULL; 2645 } 2646 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap); 2647 2648 #endif 2649 2650 static void snd_soc_component_add_unlocked(struct snd_soc_component *component) 2651 { 2652 if (!component->write && !component->read) { 2653 if (!component->regmap) 2654 component->regmap = dev_get_regmap(component->dev, NULL); 2655 if (component->regmap) 2656 snd_soc_component_setup_regmap(component); 2657 } 2658 2659 list_add(&component->list, &component_list); 2660 } 2661 2662 static void snd_soc_component_add(struct snd_soc_component *component) 2663 { 2664 mutex_lock(&client_mutex); 2665 snd_soc_component_add_unlocked(component); 2666 mutex_unlock(&client_mutex); 2667 } 2668 2669 static void snd_soc_component_cleanup(struct snd_soc_component *component) 2670 { 2671 snd_soc_unregister_dais(component); 2672 kfree(component->name); 2673 } 2674 2675 static void snd_soc_component_del_unlocked(struct snd_soc_component *component) 2676 { 2677 list_del(&component->list); 2678 } 2679 2680 static void snd_soc_component_del(struct snd_soc_component *component) 2681 { 2682 mutex_lock(&client_mutex); 2683 snd_soc_component_del_unlocked(component); 2684 mutex_unlock(&client_mutex); 2685 } 2686 2687 int snd_soc_register_component(struct device *dev, 2688 const struct snd_soc_component_driver *cmpnt_drv, 2689 struct snd_soc_dai_driver *dai_drv, 2690 int num_dai) 2691 { 2692 struct snd_soc_component *cmpnt; 2693 int ret; 2694 2695 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL); 2696 if (!cmpnt) { 2697 dev_err(dev, "ASoC: Failed to allocate memory\n"); 2698 return -ENOMEM; 2699 } 2700 2701 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev); 2702 if (ret) 2703 goto err_free; 2704 2705 cmpnt->ignore_pmdown_time = true; 2706 cmpnt->registered_as_component = true; 2707 2708 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true); 2709 if (ret < 0) { 2710 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret); 2711 goto err_cleanup; 2712 } 2713 2714 snd_soc_component_add(cmpnt); 2715 2716 return 0; 2717 2718 err_cleanup: 2719 snd_soc_component_cleanup(cmpnt); 2720 err_free: 2721 kfree(cmpnt); 2722 return ret; 2723 } 2724 EXPORT_SYMBOL_GPL(snd_soc_register_component); 2725 2726 /** 2727 * snd_soc_unregister_component - Unregister a component from the ASoC core 2728 * 2729 */ 2730 void snd_soc_unregister_component(struct device *dev) 2731 { 2732 struct snd_soc_component *cmpnt; 2733 2734 list_for_each_entry(cmpnt, &component_list, list) { 2735 if (dev == cmpnt->dev && cmpnt->registered_as_component) 2736 goto found; 2737 } 2738 return; 2739 2740 found: 2741 snd_soc_component_del(cmpnt); 2742 snd_soc_component_cleanup(cmpnt); 2743 kfree(cmpnt); 2744 } 2745 EXPORT_SYMBOL_GPL(snd_soc_unregister_component); 2746 2747 static int snd_soc_platform_drv_probe(struct snd_soc_component *component) 2748 { 2749 struct snd_soc_platform *platform = snd_soc_component_to_platform(component); 2750 2751 return platform->driver->probe(platform); 2752 } 2753 2754 static void snd_soc_platform_drv_remove(struct snd_soc_component *component) 2755 { 2756 struct snd_soc_platform *platform = snd_soc_component_to_platform(component); 2757 2758 platform->driver->remove(platform); 2759 } 2760 2761 /** 2762 * snd_soc_add_platform - Add a platform to the ASoC core 2763 * @dev: The parent device for the platform 2764 * @platform: The platform to add 2765 * @platform_driver: The driver for the platform 2766 */ 2767 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform, 2768 const struct snd_soc_platform_driver *platform_drv) 2769 { 2770 int ret; 2771 2772 ret = snd_soc_component_initialize(&platform->component, 2773 &platform_drv->component_driver, dev); 2774 if (ret) 2775 return ret; 2776 2777 platform->dev = dev; 2778 platform->driver = platform_drv; 2779 2780 if (platform_drv->probe) 2781 platform->component.probe = snd_soc_platform_drv_probe; 2782 if (platform_drv->remove) 2783 platform->component.remove = snd_soc_platform_drv_remove; 2784 2785 #ifdef CONFIG_DEBUG_FS 2786 platform->component.debugfs_prefix = "platform"; 2787 #endif 2788 2789 mutex_lock(&client_mutex); 2790 snd_soc_component_add_unlocked(&platform->component); 2791 list_add(&platform->list, &platform_list); 2792 mutex_unlock(&client_mutex); 2793 2794 dev_dbg(dev, "ASoC: Registered platform '%s'\n", 2795 platform->component.name); 2796 2797 return 0; 2798 } 2799 EXPORT_SYMBOL_GPL(snd_soc_add_platform); 2800 2801 /** 2802 * snd_soc_register_platform - Register a platform with the ASoC core 2803 * 2804 * @platform: platform to register 2805 */ 2806 int snd_soc_register_platform(struct device *dev, 2807 const struct snd_soc_platform_driver *platform_drv) 2808 { 2809 struct snd_soc_platform *platform; 2810 int ret; 2811 2812 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev)); 2813 2814 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL); 2815 if (platform == NULL) 2816 return -ENOMEM; 2817 2818 ret = snd_soc_add_platform(dev, platform, platform_drv); 2819 if (ret) 2820 kfree(platform); 2821 2822 return ret; 2823 } 2824 EXPORT_SYMBOL_GPL(snd_soc_register_platform); 2825 2826 /** 2827 * snd_soc_remove_platform - Remove a platform from the ASoC core 2828 * @platform: the platform to remove 2829 */ 2830 void snd_soc_remove_platform(struct snd_soc_platform *platform) 2831 { 2832 2833 mutex_lock(&client_mutex); 2834 list_del(&platform->list); 2835 snd_soc_component_del_unlocked(&platform->component); 2836 mutex_unlock(&client_mutex); 2837 2838 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n", 2839 platform->component.name); 2840 2841 snd_soc_component_cleanup(&platform->component); 2842 } 2843 EXPORT_SYMBOL_GPL(snd_soc_remove_platform); 2844 2845 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev) 2846 { 2847 struct snd_soc_platform *platform; 2848 2849 list_for_each_entry(platform, &platform_list, list) { 2850 if (dev == platform->dev) 2851 return platform; 2852 } 2853 2854 return NULL; 2855 } 2856 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform); 2857 2858 /** 2859 * snd_soc_unregister_platform - Unregister a platform from the ASoC core 2860 * 2861 * @platform: platform to unregister 2862 */ 2863 void snd_soc_unregister_platform(struct device *dev) 2864 { 2865 struct snd_soc_platform *platform; 2866 2867 platform = snd_soc_lookup_platform(dev); 2868 if (!platform) 2869 return; 2870 2871 snd_soc_remove_platform(platform); 2872 kfree(platform); 2873 } 2874 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform); 2875 2876 static u64 codec_format_map[] = { 2877 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE, 2878 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE, 2879 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE, 2880 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE, 2881 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE, 2882 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE, 2883 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, 2884 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, 2885 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE, 2886 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE, 2887 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE, 2888 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE, 2889 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE, 2890 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE, 2891 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE 2892 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE, 2893 }; 2894 2895 /* Fix up the DAI formats for endianness: codecs don't actually see 2896 * the endianness of the data but we're using the CPU format 2897 * definitions which do need to include endianness so we ensure that 2898 * codec DAIs always have both big and little endian variants set. 2899 */ 2900 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream) 2901 { 2902 int i; 2903 2904 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++) 2905 if (stream->formats & codec_format_map[i]) 2906 stream->formats |= codec_format_map[i]; 2907 } 2908 2909 static int snd_soc_codec_drv_probe(struct snd_soc_component *component) 2910 { 2911 struct snd_soc_codec *codec = snd_soc_component_to_codec(component); 2912 2913 return codec->driver->probe(codec); 2914 } 2915 2916 static void snd_soc_codec_drv_remove(struct snd_soc_component *component) 2917 { 2918 struct snd_soc_codec *codec = snd_soc_component_to_codec(component); 2919 2920 codec->driver->remove(codec); 2921 } 2922 2923 static int snd_soc_codec_drv_write(struct snd_soc_component *component, 2924 unsigned int reg, unsigned int val) 2925 { 2926 struct snd_soc_codec *codec = snd_soc_component_to_codec(component); 2927 2928 return codec->driver->write(codec, reg, val); 2929 } 2930 2931 static int snd_soc_codec_drv_read(struct snd_soc_component *component, 2932 unsigned int reg, unsigned int *val) 2933 { 2934 struct snd_soc_codec *codec = snd_soc_component_to_codec(component); 2935 2936 *val = codec->driver->read(codec, reg); 2937 2938 return 0; 2939 } 2940 2941 static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm, 2942 enum snd_soc_bias_level level) 2943 { 2944 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm); 2945 2946 return codec->driver->set_bias_level(codec, level); 2947 } 2948 2949 /** 2950 * snd_soc_register_codec - Register a codec with the ASoC core 2951 * 2952 * @codec: codec to register 2953 */ 2954 int snd_soc_register_codec(struct device *dev, 2955 const struct snd_soc_codec_driver *codec_drv, 2956 struct snd_soc_dai_driver *dai_drv, 2957 int num_dai) 2958 { 2959 struct snd_soc_codec *codec; 2960 struct snd_soc_dai *dai; 2961 int ret, i; 2962 2963 dev_dbg(dev, "codec register %s\n", dev_name(dev)); 2964 2965 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL); 2966 if (codec == NULL) 2967 return -ENOMEM; 2968 2969 codec->component.dapm_ptr = &codec->dapm; 2970 codec->component.codec = codec; 2971 2972 ret = snd_soc_component_initialize(&codec->component, 2973 &codec_drv->component_driver, dev); 2974 if (ret) 2975 goto err_free; 2976 2977 if (codec_drv->controls) { 2978 codec->component.controls = codec_drv->controls; 2979 codec->component.num_controls = codec_drv->num_controls; 2980 } 2981 if (codec_drv->dapm_widgets) { 2982 codec->component.dapm_widgets = codec_drv->dapm_widgets; 2983 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets; 2984 } 2985 if (codec_drv->dapm_routes) { 2986 codec->component.dapm_routes = codec_drv->dapm_routes; 2987 codec->component.num_dapm_routes = codec_drv->num_dapm_routes; 2988 } 2989 2990 if (codec_drv->probe) 2991 codec->component.probe = snd_soc_codec_drv_probe; 2992 if (codec_drv->remove) 2993 codec->component.remove = snd_soc_codec_drv_remove; 2994 if (codec_drv->write) 2995 codec->component.write = snd_soc_codec_drv_write; 2996 if (codec_drv->read) 2997 codec->component.read = snd_soc_codec_drv_read; 2998 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time; 2999 codec->dapm.idle_bias_off = codec_drv->idle_bias_off; 3000 codec->dapm.suspend_bias_off = codec_drv->suspend_bias_off; 3001 if (codec_drv->seq_notifier) 3002 codec->dapm.seq_notifier = codec_drv->seq_notifier; 3003 if (codec_drv->set_bias_level) 3004 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level; 3005 codec->dev = dev; 3006 codec->driver = codec_drv; 3007 codec->component.val_bytes = codec_drv->reg_word_size; 3008 3009 #ifdef CONFIG_DEBUG_FS 3010 codec->component.init_debugfs = soc_init_codec_debugfs; 3011 codec->component.debugfs_prefix = "codec"; 3012 #endif 3013 3014 if (codec_drv->get_regmap) 3015 codec->component.regmap = codec_drv->get_regmap(dev); 3016 3017 for (i = 0; i < num_dai; i++) { 3018 fixup_codec_formats(&dai_drv[i].playback); 3019 fixup_codec_formats(&dai_drv[i].capture); 3020 } 3021 3022 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false); 3023 if (ret < 0) { 3024 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret); 3025 goto err_cleanup; 3026 } 3027 3028 list_for_each_entry(dai, &codec->component.dai_list, list) 3029 dai->codec = codec; 3030 3031 mutex_lock(&client_mutex); 3032 snd_soc_component_add_unlocked(&codec->component); 3033 list_add(&codec->list, &codec_list); 3034 mutex_unlock(&client_mutex); 3035 3036 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", 3037 codec->component.name); 3038 return 0; 3039 3040 err_cleanup: 3041 snd_soc_component_cleanup(&codec->component); 3042 err_free: 3043 kfree(codec); 3044 return ret; 3045 } 3046 EXPORT_SYMBOL_GPL(snd_soc_register_codec); 3047 3048 /** 3049 * snd_soc_unregister_codec - Unregister a codec from the ASoC core 3050 * 3051 * @codec: codec to unregister 3052 */ 3053 void snd_soc_unregister_codec(struct device *dev) 3054 { 3055 struct snd_soc_codec *codec; 3056 3057 list_for_each_entry(codec, &codec_list, list) { 3058 if (dev == codec->dev) 3059 goto found; 3060 } 3061 return; 3062 3063 found: 3064 3065 mutex_lock(&client_mutex); 3066 list_del(&codec->list); 3067 snd_soc_component_del_unlocked(&codec->component); 3068 mutex_unlock(&client_mutex); 3069 3070 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", 3071 codec->component.name); 3072 3073 snd_soc_component_cleanup(&codec->component); 3074 snd_soc_cache_exit(codec); 3075 kfree(codec); 3076 } 3077 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec); 3078 3079 /* Retrieve a card's name from device tree */ 3080 int snd_soc_of_parse_card_name(struct snd_soc_card *card, 3081 const char *propname) 3082 { 3083 struct device_node *np; 3084 int ret; 3085 3086 if (!card->dev) { 3087 pr_err("card->dev is not set before calling %s\n", __func__); 3088 return -EINVAL; 3089 } 3090 3091 np = card->dev->of_node; 3092 3093 ret = of_property_read_string_index(np, propname, 0, &card->name); 3094 /* 3095 * EINVAL means the property does not exist. This is fine providing 3096 * card->name was previously set, which is checked later in 3097 * snd_soc_register_card. 3098 */ 3099 if (ret < 0 && ret != -EINVAL) { 3100 dev_err(card->dev, 3101 "ASoC: Property '%s' could not be read: %d\n", 3102 propname, ret); 3103 return ret; 3104 } 3105 3106 return 0; 3107 } 3108 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name); 3109 3110 static const struct snd_soc_dapm_widget simple_widgets[] = { 3111 SND_SOC_DAPM_MIC("Microphone", NULL), 3112 SND_SOC_DAPM_LINE("Line", NULL), 3113 SND_SOC_DAPM_HP("Headphone", NULL), 3114 SND_SOC_DAPM_SPK("Speaker", NULL), 3115 }; 3116 3117 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card, 3118 const char *propname) 3119 { 3120 struct device_node *np = card->dev->of_node; 3121 struct snd_soc_dapm_widget *widgets; 3122 const char *template, *wname; 3123 int i, j, num_widgets, ret; 3124 3125 num_widgets = of_property_count_strings(np, propname); 3126 if (num_widgets < 0) { 3127 dev_err(card->dev, 3128 "ASoC: Property '%s' does not exist\n", propname); 3129 return -EINVAL; 3130 } 3131 if (num_widgets & 1) { 3132 dev_err(card->dev, 3133 "ASoC: Property '%s' length is not even\n", propname); 3134 return -EINVAL; 3135 } 3136 3137 num_widgets /= 2; 3138 if (!num_widgets) { 3139 dev_err(card->dev, "ASoC: Property '%s's length is zero\n", 3140 propname); 3141 return -EINVAL; 3142 } 3143 3144 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets), 3145 GFP_KERNEL); 3146 if (!widgets) { 3147 dev_err(card->dev, 3148 "ASoC: Could not allocate memory for widgets\n"); 3149 return -ENOMEM; 3150 } 3151 3152 for (i = 0; i < num_widgets; i++) { 3153 ret = of_property_read_string_index(np, propname, 3154 2 * i, &template); 3155 if (ret) { 3156 dev_err(card->dev, 3157 "ASoC: Property '%s' index %d read error:%d\n", 3158 propname, 2 * i, ret); 3159 return -EINVAL; 3160 } 3161 3162 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) { 3163 if (!strncmp(template, simple_widgets[j].name, 3164 strlen(simple_widgets[j].name))) { 3165 widgets[i] = simple_widgets[j]; 3166 break; 3167 } 3168 } 3169 3170 if (j >= ARRAY_SIZE(simple_widgets)) { 3171 dev_err(card->dev, 3172 "ASoC: DAPM widget '%s' is not supported\n", 3173 template); 3174 return -EINVAL; 3175 } 3176 3177 ret = of_property_read_string_index(np, propname, 3178 (2 * i) + 1, 3179 &wname); 3180 if (ret) { 3181 dev_err(card->dev, 3182 "ASoC: Property '%s' index %d read error:%d\n", 3183 propname, (2 * i) + 1, ret); 3184 return -EINVAL; 3185 } 3186 3187 widgets[i].name = wname; 3188 } 3189 3190 card->dapm_widgets = widgets; 3191 card->num_dapm_widgets = num_widgets; 3192 3193 return 0; 3194 } 3195 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets); 3196 3197 int snd_soc_of_parse_tdm_slot(struct device_node *np, 3198 unsigned int *slots, 3199 unsigned int *slot_width) 3200 { 3201 u32 val; 3202 int ret; 3203 3204 if (of_property_read_bool(np, "dai-tdm-slot-num")) { 3205 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val); 3206 if (ret) 3207 return ret; 3208 3209 if (slots) 3210 *slots = val; 3211 } 3212 3213 if (of_property_read_bool(np, "dai-tdm-slot-width")) { 3214 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val); 3215 if (ret) 3216 return ret; 3217 3218 if (slot_width) 3219 *slot_width = val; 3220 } 3221 3222 return 0; 3223 } 3224 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot); 3225 3226 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card, 3227 const char *propname) 3228 { 3229 struct device_node *np = card->dev->of_node; 3230 int num_routes; 3231 struct snd_soc_dapm_route *routes; 3232 int i, ret; 3233 3234 num_routes = of_property_count_strings(np, propname); 3235 if (num_routes < 0 || num_routes & 1) { 3236 dev_err(card->dev, 3237 "ASoC: Property '%s' does not exist or its length is not even\n", 3238 propname); 3239 return -EINVAL; 3240 } 3241 num_routes /= 2; 3242 if (!num_routes) { 3243 dev_err(card->dev, "ASoC: Property '%s's length is zero\n", 3244 propname); 3245 return -EINVAL; 3246 } 3247 3248 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes), 3249 GFP_KERNEL); 3250 if (!routes) { 3251 dev_err(card->dev, 3252 "ASoC: Could not allocate DAPM route table\n"); 3253 return -EINVAL; 3254 } 3255 3256 for (i = 0; i < num_routes; i++) { 3257 ret = of_property_read_string_index(np, propname, 3258 2 * i, &routes[i].sink); 3259 if (ret) { 3260 dev_err(card->dev, 3261 "ASoC: Property '%s' index %d could not be read: %d\n", 3262 propname, 2 * i, ret); 3263 return -EINVAL; 3264 } 3265 ret = of_property_read_string_index(np, propname, 3266 (2 * i) + 1, &routes[i].source); 3267 if (ret) { 3268 dev_err(card->dev, 3269 "ASoC: Property '%s' index %d could not be read: %d\n", 3270 propname, (2 * i) + 1, ret); 3271 return -EINVAL; 3272 } 3273 } 3274 3275 card->num_dapm_routes = num_routes; 3276 card->dapm_routes = routes; 3277 3278 return 0; 3279 } 3280 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing); 3281 3282 unsigned int snd_soc_of_parse_daifmt(struct device_node *np, 3283 const char *prefix, 3284 struct device_node **bitclkmaster, 3285 struct device_node **framemaster) 3286 { 3287 int ret, i; 3288 char prop[128]; 3289 unsigned int format = 0; 3290 int bit, frame; 3291 const char *str; 3292 struct { 3293 char *name; 3294 unsigned int val; 3295 } of_fmt_table[] = { 3296 { "i2s", SND_SOC_DAIFMT_I2S }, 3297 { "right_j", SND_SOC_DAIFMT_RIGHT_J }, 3298 { "left_j", SND_SOC_DAIFMT_LEFT_J }, 3299 { "dsp_a", SND_SOC_DAIFMT_DSP_A }, 3300 { "dsp_b", SND_SOC_DAIFMT_DSP_B }, 3301 { "ac97", SND_SOC_DAIFMT_AC97 }, 3302 { "pdm", SND_SOC_DAIFMT_PDM}, 3303 { "msb", SND_SOC_DAIFMT_MSB }, 3304 { "lsb", SND_SOC_DAIFMT_LSB }, 3305 }; 3306 3307 if (!prefix) 3308 prefix = ""; 3309 3310 /* 3311 * check "[prefix]format = xxx" 3312 * SND_SOC_DAIFMT_FORMAT_MASK area 3313 */ 3314 snprintf(prop, sizeof(prop), "%sformat", prefix); 3315 ret = of_property_read_string(np, prop, &str); 3316 if (ret == 0) { 3317 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) { 3318 if (strcmp(str, of_fmt_table[i].name) == 0) { 3319 format |= of_fmt_table[i].val; 3320 break; 3321 } 3322 } 3323 } 3324 3325 /* 3326 * check "[prefix]continuous-clock" 3327 * SND_SOC_DAIFMT_CLOCK_MASK area 3328 */ 3329 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix); 3330 if (of_get_property(np, prop, NULL)) 3331 format |= SND_SOC_DAIFMT_CONT; 3332 else 3333 format |= SND_SOC_DAIFMT_GATED; 3334 3335 /* 3336 * check "[prefix]bitclock-inversion" 3337 * check "[prefix]frame-inversion" 3338 * SND_SOC_DAIFMT_INV_MASK area 3339 */ 3340 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix); 3341 bit = !!of_get_property(np, prop, NULL); 3342 3343 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix); 3344 frame = !!of_get_property(np, prop, NULL); 3345 3346 switch ((bit << 4) + frame) { 3347 case 0x11: 3348 format |= SND_SOC_DAIFMT_IB_IF; 3349 break; 3350 case 0x10: 3351 format |= SND_SOC_DAIFMT_IB_NF; 3352 break; 3353 case 0x01: 3354 format |= SND_SOC_DAIFMT_NB_IF; 3355 break; 3356 default: 3357 /* SND_SOC_DAIFMT_NB_NF is default */ 3358 break; 3359 } 3360 3361 /* 3362 * check "[prefix]bitclock-master" 3363 * check "[prefix]frame-master" 3364 * SND_SOC_DAIFMT_MASTER_MASK area 3365 */ 3366 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix); 3367 bit = !!of_get_property(np, prop, NULL); 3368 if (bit && bitclkmaster) 3369 *bitclkmaster = of_parse_phandle(np, prop, 0); 3370 3371 snprintf(prop, sizeof(prop), "%sframe-master", prefix); 3372 frame = !!of_get_property(np, prop, NULL); 3373 if (frame && framemaster) 3374 *framemaster = of_parse_phandle(np, prop, 0); 3375 3376 switch ((bit << 4) + frame) { 3377 case 0x11: 3378 format |= SND_SOC_DAIFMT_CBM_CFM; 3379 break; 3380 case 0x10: 3381 format |= SND_SOC_DAIFMT_CBM_CFS; 3382 break; 3383 case 0x01: 3384 format |= SND_SOC_DAIFMT_CBS_CFM; 3385 break; 3386 default: 3387 format |= SND_SOC_DAIFMT_CBS_CFS; 3388 break; 3389 } 3390 3391 return format; 3392 } 3393 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt); 3394 3395 static int snd_soc_get_dai_name(struct of_phandle_args *args, 3396 const char **dai_name) 3397 { 3398 struct snd_soc_component *pos; 3399 int ret = -EPROBE_DEFER; 3400 3401 mutex_lock(&client_mutex); 3402 list_for_each_entry(pos, &component_list, list) { 3403 if (pos->dev->of_node != args->np) 3404 continue; 3405 3406 if (pos->driver->of_xlate_dai_name) { 3407 ret = pos->driver->of_xlate_dai_name(pos, 3408 args, 3409 dai_name); 3410 } else { 3411 int id = -1; 3412 3413 switch (args->args_count) { 3414 case 0: 3415 id = 0; /* same as dai_drv[0] */ 3416 break; 3417 case 1: 3418 id = args->args[0]; 3419 break; 3420 default: 3421 /* not supported */ 3422 break; 3423 } 3424 3425 if (id < 0 || id >= pos->num_dai) { 3426 ret = -EINVAL; 3427 continue; 3428 } 3429 3430 ret = 0; 3431 3432 *dai_name = pos->dai_drv[id].name; 3433 if (!*dai_name) 3434 *dai_name = pos->name; 3435 } 3436 3437 break; 3438 } 3439 mutex_unlock(&client_mutex); 3440 return ret; 3441 } 3442 3443 int snd_soc_of_get_dai_name(struct device_node *of_node, 3444 const char **dai_name) 3445 { 3446 struct of_phandle_args args; 3447 int ret; 3448 3449 ret = of_parse_phandle_with_args(of_node, "sound-dai", 3450 "#sound-dai-cells", 0, &args); 3451 if (ret) 3452 return ret; 3453 3454 ret = snd_soc_get_dai_name(&args, dai_name); 3455 3456 of_node_put(args.np); 3457 3458 return ret; 3459 } 3460 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name); 3461 3462 /* 3463 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree 3464 * @dev: Card device 3465 * @of_node: Device node 3466 * @dai_link: DAI link 3467 * 3468 * Builds an array of CODEC DAI components from the DAI link property 3469 * 'sound-dai'. 3470 * The array is set in the DAI link and the number of DAIs is set accordingly. 3471 * The device nodes in the array (of_node) must be dereferenced by the caller. 3472 * 3473 * Returns 0 for success 3474 */ 3475 int snd_soc_of_get_dai_link_codecs(struct device *dev, 3476 struct device_node *of_node, 3477 struct snd_soc_dai_link *dai_link) 3478 { 3479 struct of_phandle_args args; 3480 struct snd_soc_dai_link_component *component; 3481 char *name; 3482 int index, num_codecs, ret; 3483 3484 /* Count the number of CODECs */ 3485 name = "sound-dai"; 3486 num_codecs = of_count_phandle_with_args(of_node, name, 3487 "#sound-dai-cells"); 3488 if (num_codecs <= 0) { 3489 if (num_codecs == -ENOENT) 3490 dev_err(dev, "No 'sound-dai' property\n"); 3491 else 3492 dev_err(dev, "Bad phandle in 'sound-dai'\n"); 3493 return num_codecs; 3494 } 3495 component = devm_kzalloc(dev, 3496 sizeof *component * num_codecs, 3497 GFP_KERNEL); 3498 if (!component) 3499 return -ENOMEM; 3500 dai_link->codecs = component; 3501 dai_link->num_codecs = num_codecs; 3502 3503 /* Parse the list */ 3504 for (index = 0, component = dai_link->codecs; 3505 index < dai_link->num_codecs; 3506 index++, component++) { 3507 ret = of_parse_phandle_with_args(of_node, name, 3508 "#sound-dai-cells", 3509 index, &args); 3510 if (ret) 3511 goto err; 3512 component->of_node = args.np; 3513 ret = snd_soc_get_dai_name(&args, &component->dai_name); 3514 if (ret < 0) 3515 goto err; 3516 } 3517 return 0; 3518 err: 3519 for (index = 0, component = dai_link->codecs; 3520 index < dai_link->num_codecs; 3521 index++, component++) { 3522 if (!component->of_node) 3523 break; 3524 of_node_put(component->of_node); 3525 component->of_node = NULL; 3526 } 3527 dai_link->codecs = NULL; 3528 dai_link->num_codecs = 0; 3529 return ret; 3530 } 3531 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs); 3532 3533 static int __init snd_soc_init(void) 3534 { 3535 #ifdef CONFIG_DEBUG_FS 3536 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL); 3537 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) { 3538 pr_warn("ASoC: Failed to create debugfs directory\n"); 3539 snd_soc_debugfs_root = NULL; 3540 } 3541 3542 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL, 3543 &codec_list_fops)) 3544 pr_warn("ASoC: Failed to create CODEC list debugfs file\n"); 3545 3546 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL, 3547 &dai_list_fops)) 3548 pr_warn("ASoC: Failed to create DAI list debugfs file\n"); 3549 3550 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL, 3551 &platform_list_fops)) 3552 pr_warn("ASoC: Failed to create platform list debugfs file\n"); 3553 #endif 3554 3555 snd_soc_util_init(); 3556 3557 return platform_driver_register(&soc_driver); 3558 } 3559 module_init(snd_soc_init); 3560 3561 static void __exit snd_soc_exit(void) 3562 { 3563 snd_soc_util_exit(); 3564 3565 #ifdef CONFIG_DEBUG_FS 3566 debugfs_remove_recursive(snd_soc_debugfs_root); 3567 #endif 3568 platform_driver_unregister(&soc_driver); 3569 } 3570 module_exit(snd_soc_exit); 3571 3572 /* Module information */ 3573 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); 3574 MODULE_DESCRIPTION("ALSA SoC Core"); 3575 MODULE_LICENSE("GPL"); 3576 MODULE_ALIAS("platform:soc-audio"); 3577