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