2 * soc-core.c -- ALSA SoC Audio Layer
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
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.
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
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/delay.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>
37 #include <linux/gpio.h>
38 #include <linux/of_gpio.h>
39 #include <sound/ac97_codec.h>
40 #include <sound/core.h>
41 #include <sound/jack.h>
42 #include <sound/pcm.h>
43 #include <sound/pcm_params.h>
44 #include <sound/soc.h>
45 #include <sound/soc-dpcm.h>
46 #include <sound/initval.h>
48 #define CREATE_TRACE_POINTS
49 #include <trace/events/asoc.h>
53 #ifdef CONFIG_DEBUG_FS
54 struct dentry *snd_soc_debugfs_root;
55 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
58 static DEFINE_MUTEX(client_mutex);
59 static LIST_HEAD(platform_list);
60 static LIST_HEAD(codec_list);
61 static LIST_HEAD(component_list);
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.
68 static int pmdown_time = 5000;
69 module_param(pmdown_time, int, 0);
70 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
72 struct snd_ac97_reset_cfg {
74 struct pinctrl_state *pstate_reset;
75 struct pinctrl_state *pstate_warm_reset;
76 struct pinctrl_state *pstate_run;
82 /* returns the minimum number of bytes needed to represent
83 * a particular given value */
84 static int min_bytes_needed(unsigned long val)
89 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
92 c = (sizeof val * 8) - c;
100 /* fill buf which is 'len' bytes with a formatted
101 * string of the form 'reg: value\n' */
102 static int format_register_str(struct snd_soc_codec *codec,
103 unsigned int reg, char *buf, size_t len)
105 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
106 int regsize = codec->driver->reg_word_size * 2;
108 char tmpbuf[len + 1];
109 char regbuf[regsize + 1];
111 /* since tmpbuf is allocated on the stack, warn the callers if they
112 * try to abuse this function */
115 /* +2 for ': ' and + 1 for '\n' */
116 if (wordsize + regsize + 2 + 1 != len)
119 ret = snd_soc_read(codec, reg);
121 memset(regbuf, 'X', regsize);
122 regbuf[regsize] = '\0';
124 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
127 /* prepare the buffer */
128 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
129 /* copy it back to the caller without the '\0' */
130 memcpy(buf, tmpbuf, len);
135 /* codec register dump */
136 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
137 size_t count, loff_t pos)
140 int wordsize, regsize;
145 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
146 regsize = codec->driver->reg_word_size * 2;
148 len = wordsize + regsize + 2 + 1;
150 if (!codec->driver->reg_cache_size)
153 if (codec->driver->reg_cache_step)
154 step = codec->driver->reg_cache_step;
156 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
157 /* only support larger than PAGE_SIZE bytes debugfs
158 * entries for the default case */
160 if (total + len >= count - 1)
162 format_register_str(codec, i, buf + total, len);
168 total = min(total, count - 1);
173 static ssize_t codec_reg_show(struct device *dev,
174 struct device_attribute *attr, char *buf)
176 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
178 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
181 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
183 static ssize_t pmdown_time_show(struct device *dev,
184 struct device_attribute *attr, char *buf)
186 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
188 return sprintf(buf, "%ld\n", rtd->pmdown_time);
191 static ssize_t pmdown_time_set(struct device *dev,
192 struct device_attribute *attr,
193 const char *buf, size_t count)
195 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
198 ret = kstrtol(buf, 10, &rtd->pmdown_time);
205 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
207 #ifdef CONFIG_DEBUG_FS
208 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
209 size_t count, loff_t *ppos)
212 struct snd_soc_codec *codec = file->private_data;
215 if (*ppos < 0 || !count)
218 buf = kmalloc(count, GFP_KERNEL);
222 ret = soc_codec_reg_show(codec, buf, count, *ppos);
224 if (copy_to_user(user_buf, buf, ret)) {
235 static ssize_t codec_reg_write_file(struct file *file,
236 const char __user *user_buf, size_t count, loff_t *ppos)
241 unsigned long reg, value;
242 struct snd_soc_codec *codec = file->private_data;
245 buf_size = min(count, (sizeof(buf)-1));
246 if (copy_from_user(buf, user_buf, buf_size))
250 while (*start == ' ')
252 reg = simple_strtoul(start, &start, 16);
253 while (*start == ' ')
255 ret = kstrtoul(start, 16, &value);
259 /* Userspace has been fiddling around behind the kernel's back */
260 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
262 snd_soc_write(codec, reg, value);
266 static const struct file_operations codec_reg_fops = {
268 .read = codec_reg_read_file,
269 .write = codec_reg_write_file,
270 .llseek = default_llseek,
273 static void soc_init_component_debugfs(struct snd_soc_component *component)
275 if (component->debugfs_prefix) {
278 name = kasprintf(GFP_KERNEL, "%s:%s",
279 component->debugfs_prefix, component->name);
281 component->debugfs_root = debugfs_create_dir(name,
282 component->card->debugfs_card_root);
286 component->debugfs_root = debugfs_create_dir(component->name,
287 component->card->debugfs_card_root);
290 if (!component->debugfs_root) {
291 dev_warn(component->dev,
292 "ASoC: Failed to create component debugfs directory\n");
296 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
297 component->debugfs_root);
299 if (component->init_debugfs)
300 component->init_debugfs(component);
303 static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
305 debugfs_remove_recursive(component->debugfs_root);
308 static void soc_init_codec_debugfs(struct snd_soc_component *component)
310 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
312 debugfs_create_bool("cache_sync", 0444, codec->component.debugfs_root,
315 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
316 codec->component.debugfs_root,
317 codec, &codec_reg_fops);
318 if (!codec->debugfs_reg)
320 "ASoC: Failed to create codec register debugfs file\n");
323 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
324 size_t count, loff_t *ppos)
326 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
327 ssize_t len, ret = 0;
328 struct snd_soc_codec *codec;
333 list_for_each_entry(codec, &codec_list, list) {
334 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
335 codec->component.name);
338 if (ret > PAGE_SIZE) {
345 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
352 static const struct file_operations codec_list_fops = {
353 .read = codec_list_read_file,
354 .llseek = default_llseek,/* read accesses f_pos */
357 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
358 size_t count, loff_t *ppos)
360 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
361 ssize_t len, ret = 0;
362 struct snd_soc_component *component;
363 struct snd_soc_dai *dai;
368 list_for_each_entry(component, &component_list, list) {
369 list_for_each_entry(dai, &component->dai_list, list) {
370 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
374 if (ret > PAGE_SIZE) {
381 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
388 static const struct file_operations dai_list_fops = {
389 .read = dai_list_read_file,
390 .llseek = default_llseek,/* read accesses f_pos */
393 static ssize_t platform_list_read_file(struct file *file,
394 char __user *user_buf,
395 size_t count, loff_t *ppos)
397 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
398 ssize_t len, ret = 0;
399 struct snd_soc_platform *platform;
404 list_for_each_entry(platform, &platform_list, list) {
405 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
406 platform->component.name);
409 if (ret > PAGE_SIZE) {
415 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
422 static const struct file_operations platform_list_fops = {
423 .read = platform_list_read_file,
424 .llseek = default_llseek,/* read accesses f_pos */
427 static void soc_init_card_debugfs(struct snd_soc_card *card)
429 card->debugfs_card_root = debugfs_create_dir(card->name,
430 snd_soc_debugfs_root);
431 if (!card->debugfs_card_root) {
433 "ASoC: Failed to create card debugfs directory\n");
437 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
438 card->debugfs_card_root,
440 if (!card->debugfs_pop_time)
442 "ASoC: Failed to create pop time debugfs file\n");
445 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
447 debugfs_remove_recursive(card->debugfs_card_root);
452 #define soc_init_codec_debugfs NULL
454 static inline void soc_init_component_debugfs(
455 struct snd_soc_component *component)
459 static inline void soc_cleanup_component_debugfs(
460 struct snd_soc_component *component)
464 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
468 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
473 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
474 const char *dai_link, int stream)
478 for (i = 0; i < card->num_links; i++) {
479 if (card->rtd[i].dai_link->no_pcm &&
480 !strcmp(card->rtd[i].dai_link->name, dai_link))
481 return card->rtd[i].pcm->streams[stream].substream;
483 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
486 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
488 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
489 const char *dai_link)
493 for (i = 0; i < card->num_links; i++) {
494 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
495 return &card->rtd[i];
497 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
500 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
502 #ifdef CONFIG_SND_SOC_AC97_BUS
503 /* unregister ac97 codec */
504 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
506 if (codec->ac97->dev.bus)
507 device_unregister(&codec->ac97->dev);
511 /* stop no dev release warning */
512 static void soc_ac97_device_release(struct device *dev){}
514 /* register ac97 codec to bus */
515 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
519 codec->ac97->dev.bus = &ac97_bus_type;
520 codec->ac97->dev.parent = codec->component.card->dev;
521 codec->ac97->dev.release = soc_ac97_device_release;
523 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
524 codec->component.card->snd_card->number, 0,
525 codec->component.name);
526 err = device_register(&codec->ac97->dev);
528 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
529 codec->ac97->dev.bus = NULL;
536 static void codec2codec_close_delayed_work(struct work_struct *work)
538 /* Currently nothing to do for c2c links
539 * Since c2c links are internal nodes in the DAPM graph and
540 * don't interface with the outside world or application layer
541 * we don't have to do any special handling on close.
545 #ifdef CONFIG_PM_SLEEP
546 /* powers down audio subsystem for suspend */
547 int snd_soc_suspend(struct device *dev)
549 struct snd_soc_card *card = dev_get_drvdata(dev);
550 struct snd_soc_codec *codec;
553 /* If the card is not initialized yet there is nothing to do */
554 if (!card->instantiated)
557 /* Due to the resume being scheduled into a workqueue we could
558 * suspend before that's finished - wait for it to complete.
560 snd_power_lock(card->snd_card);
561 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
562 snd_power_unlock(card->snd_card);
564 /* we're going to block userspace touching us until resume completes */
565 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
567 /* mute any active DACs */
568 for (i = 0; i < card->num_rtd; i++) {
570 if (card->rtd[i].dai_link->ignore_suspend)
573 for (j = 0; j < card->rtd[i].num_codecs; j++) {
574 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
575 struct snd_soc_dai_driver *drv = dai->driver;
577 if (drv->ops->digital_mute && dai->playback_active)
578 drv->ops->digital_mute(dai, 1);
582 /* suspend all pcms */
583 for (i = 0; i < card->num_rtd; i++) {
584 if (card->rtd[i].dai_link->ignore_suspend)
587 snd_pcm_suspend_all(card->rtd[i].pcm);
590 if (card->suspend_pre)
591 card->suspend_pre(card);
593 for (i = 0; i < card->num_rtd; i++) {
594 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
596 if (card->rtd[i].dai_link->ignore_suspend)
599 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
600 cpu_dai->driver->suspend(cpu_dai);
603 /* close any waiting streams and save state */
604 for (i = 0; i < card->num_rtd; i++) {
605 struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais;
606 flush_delayed_work(&card->rtd[i].delayed_work);
607 for (j = 0; j < card->rtd[i].num_codecs; j++) {
608 codec_dais[j]->codec->dapm.suspend_bias_level =
609 codec_dais[j]->codec->dapm.bias_level;
613 for (i = 0; i < card->num_rtd; i++) {
615 if (card->rtd[i].dai_link->ignore_suspend)
618 snd_soc_dapm_stream_event(&card->rtd[i],
619 SNDRV_PCM_STREAM_PLAYBACK,
620 SND_SOC_DAPM_STREAM_SUSPEND);
622 snd_soc_dapm_stream_event(&card->rtd[i],
623 SNDRV_PCM_STREAM_CAPTURE,
624 SND_SOC_DAPM_STREAM_SUSPEND);
627 /* Recheck all analogue paths too */
628 dapm_mark_io_dirty(&card->dapm);
629 snd_soc_dapm_sync(&card->dapm);
631 /* suspend all CODECs */
632 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
633 /* If there are paths active then the CODEC will be held with
634 * bias _ON and should not be suspended. */
635 if (!codec->suspended) {
636 switch (codec->dapm.bias_level) {
637 case SND_SOC_BIAS_STANDBY:
639 * If the CODEC is capable of idle
640 * bias off then being in STANDBY
641 * means it's doing something,
642 * otherwise fall through.
644 if (codec->dapm.idle_bias_off) {
646 "ASoC: idle_bias_off CODEC on over suspend\n");
650 case SND_SOC_BIAS_OFF:
651 if (codec->driver->suspend)
652 codec->driver->suspend(codec);
653 codec->suspended = 1;
654 codec->cache_sync = 1;
655 if (codec->component.regmap)
656 regcache_mark_dirty(codec->component.regmap);
657 /* deactivate pins to sleep state */
658 pinctrl_pm_select_sleep_state(codec->dev);
662 "ASoC: CODEC is on over suspend\n");
668 for (i = 0; i < card->num_rtd; i++) {
669 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
671 if (card->rtd[i].dai_link->ignore_suspend)
674 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
675 cpu_dai->driver->suspend(cpu_dai);
677 /* deactivate pins to sleep state */
678 pinctrl_pm_select_sleep_state(cpu_dai->dev);
681 if (card->suspend_post)
682 card->suspend_post(card);
686 EXPORT_SYMBOL_GPL(snd_soc_suspend);
688 /* deferred resume work, so resume can complete before we finished
689 * setting our codec back up, which can be very slow on I2C
691 static void soc_resume_deferred(struct work_struct *work)
693 struct snd_soc_card *card =
694 container_of(work, struct snd_soc_card, deferred_resume_work);
695 struct snd_soc_codec *codec;
698 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
699 * so userspace apps are blocked from touching us
702 dev_dbg(card->dev, "ASoC: starting resume work\n");
704 /* Bring us up into D2 so that DAPM starts enabling things */
705 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
707 if (card->resume_pre)
708 card->resume_pre(card);
710 /* resume AC97 DAIs */
711 for (i = 0; i < card->num_rtd; i++) {
712 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
714 if (card->rtd[i].dai_link->ignore_suspend)
717 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
718 cpu_dai->driver->resume(cpu_dai);
721 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
722 /* If the CODEC was idle over suspend then it will have been
723 * left with bias OFF or STANDBY and suspended so we must now
724 * resume. Otherwise the suspend was suppressed.
726 if (codec->suspended) {
727 switch (codec->dapm.bias_level) {
728 case SND_SOC_BIAS_STANDBY:
729 case SND_SOC_BIAS_OFF:
730 if (codec->driver->resume)
731 codec->driver->resume(codec);
732 codec->suspended = 0;
736 "ASoC: CODEC was on over suspend\n");
742 for (i = 0; i < card->num_rtd; i++) {
744 if (card->rtd[i].dai_link->ignore_suspend)
747 snd_soc_dapm_stream_event(&card->rtd[i],
748 SNDRV_PCM_STREAM_PLAYBACK,
749 SND_SOC_DAPM_STREAM_RESUME);
751 snd_soc_dapm_stream_event(&card->rtd[i],
752 SNDRV_PCM_STREAM_CAPTURE,
753 SND_SOC_DAPM_STREAM_RESUME);
756 /* unmute any active DACs */
757 for (i = 0; i < card->num_rtd; i++) {
759 if (card->rtd[i].dai_link->ignore_suspend)
762 for (j = 0; j < card->rtd[i].num_codecs; j++) {
763 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
764 struct snd_soc_dai_driver *drv = dai->driver;
766 if (drv->ops->digital_mute && dai->playback_active)
767 drv->ops->digital_mute(dai, 0);
771 for (i = 0; i < card->num_rtd; i++) {
772 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
774 if (card->rtd[i].dai_link->ignore_suspend)
777 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
778 cpu_dai->driver->resume(cpu_dai);
781 if (card->resume_post)
782 card->resume_post(card);
784 dev_dbg(card->dev, "ASoC: resume work completed\n");
786 /* userspace can access us now we are back as we were before */
787 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
789 /* Recheck all analogue paths too */
790 dapm_mark_io_dirty(&card->dapm);
791 snd_soc_dapm_sync(&card->dapm);
794 /* powers up audio subsystem after a suspend */
795 int snd_soc_resume(struct device *dev)
797 struct snd_soc_card *card = dev_get_drvdata(dev);
798 int i, ac97_control = 0;
800 /* If the card is not initialized yet there is nothing to do */
801 if (!card->instantiated)
804 /* activate pins from sleep state */
805 for (i = 0; i < card->num_rtd; i++) {
806 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
807 struct snd_soc_dai **codec_dais = rtd->codec_dais;
808 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
812 pinctrl_pm_select_default_state(cpu_dai->dev);
814 for (j = 0; j < rtd->num_codecs; j++) {
815 struct snd_soc_dai *codec_dai = codec_dais[j];
816 if (codec_dai->active)
817 pinctrl_pm_select_default_state(codec_dai->dev);
821 /* AC97 devices might have other drivers hanging off them so
822 * need to resume immediately. Other drivers don't have that
823 * problem and may take a substantial amount of time to resume
824 * due to I/O costs and anti-pop so handle them out of line.
826 for (i = 0; i < card->num_rtd; i++) {
827 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
828 ac97_control |= cpu_dai->driver->ac97_control;
831 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
832 soc_resume_deferred(&card->deferred_resume_work);
834 dev_dbg(dev, "ASoC: Scheduling resume work\n");
835 if (!schedule_work(&card->deferred_resume_work))
836 dev_err(dev, "ASoC: resume work item may be lost\n");
841 EXPORT_SYMBOL_GPL(snd_soc_resume);
843 #define snd_soc_suspend NULL
844 #define snd_soc_resume NULL
847 static const struct snd_soc_dai_ops null_dai_ops = {
850 static struct snd_soc_component *soc_find_component(
851 const struct device_node *of_node, const char *name)
853 struct snd_soc_component *component;
855 list_for_each_entry(component, &component_list, list) {
857 if (component->dev->of_node == of_node)
859 } else if (strcmp(component->name, name) == 0) {
867 static struct snd_soc_dai *snd_soc_find_dai(
868 const struct snd_soc_dai_link_component *dlc)
870 struct snd_soc_component *component;
871 struct snd_soc_dai *dai;
873 /* Find CPU DAI from registered DAIs*/
874 list_for_each_entry(component, &component_list, list) {
875 if (dlc->of_node && component->dev->of_node != dlc->of_node)
877 if (dlc->name && strcmp(dev_name(component->dev), dlc->name))
879 list_for_each_entry(dai, &component->dai_list, list) {
880 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
890 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
892 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
893 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
894 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
895 struct snd_soc_dai_link_component cpu_dai_component;
896 struct snd_soc_dai **codec_dais = rtd->codec_dais;
897 struct snd_soc_platform *platform;
898 const char *platform_name;
901 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
903 cpu_dai_component.name = dai_link->cpu_name;
904 cpu_dai_component.of_node = dai_link->cpu_of_node;
905 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
906 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
908 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
909 dai_link->cpu_dai_name);
910 return -EPROBE_DEFER;
913 rtd->num_codecs = dai_link->num_codecs;
915 /* Find CODEC from registered CODECs */
916 for (i = 0; i < rtd->num_codecs; i++) {
917 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
918 if (!codec_dais[i]) {
919 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
921 return -EPROBE_DEFER;
925 /* Single codec links expect codec and codec_dai in runtime data */
926 rtd->codec_dai = codec_dais[0];
927 rtd->codec = rtd->codec_dai->codec;
929 /* if there's no platform we match on the empty platform */
930 platform_name = dai_link->platform_name;
931 if (!platform_name && !dai_link->platform_of_node)
932 platform_name = "snd-soc-dummy";
934 /* find one from the set of registered platforms */
935 list_for_each_entry(platform, &platform_list, list) {
936 if (dai_link->platform_of_node) {
937 if (platform->dev->of_node !=
938 dai_link->platform_of_node)
941 if (strcmp(platform->component.name, platform_name))
945 rtd->platform = platform;
947 if (!rtd->platform) {
948 dev_err(card->dev, "ASoC: platform %s not registered\n",
949 dai_link->platform_name);
950 return -EPROBE_DEFER;
958 static void soc_remove_component(struct snd_soc_component *component)
960 if (!component->probed)
963 /* This is a HACK and will be removed soon */
964 if (component->codec)
965 list_del(&component->codec->card_list);
967 if (component->remove)
968 component->remove(component);
970 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
972 soc_cleanup_component_debugfs(component);
973 component->probed = 0;
974 module_put(component->dev->driver->owner);
977 static void soc_remove_dai(struct snd_soc_dai *dai, int order)
981 if (dai && dai->probed &&
982 dai->driver->remove_order == order) {
983 if (dai->driver->remove) {
984 err = dai->driver->remove(dai);
987 "ASoC: failed to remove %s: %d\n",
994 static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
996 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
999 /* unregister the rtd device */
1000 if (rtd->dev_registered) {
1001 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1002 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1003 device_unregister(rtd->dev);
1004 rtd->dev_registered = 0;
1007 /* remove the CODEC DAI */
1008 for (i = 0; i < rtd->num_codecs; i++)
1009 soc_remove_dai(rtd->codec_dais[i], order);
1011 soc_remove_dai(rtd->cpu_dai, order);
1014 static void soc_remove_link_components(struct snd_soc_card *card, int num,
1017 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1018 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1019 struct snd_soc_platform *platform = rtd->platform;
1020 struct snd_soc_component *component;
1023 /* remove the platform */
1024 if (platform && platform->component.driver->remove_order == order)
1025 soc_remove_component(&platform->component);
1027 /* remove the CODEC-side CODEC */
1028 for (i = 0; i < rtd->num_codecs; i++) {
1029 component = rtd->codec_dais[i]->component;
1030 if (component->driver->remove_order == order)
1031 soc_remove_component(component);
1034 /* remove any CPU-side CODEC */
1036 if (cpu_dai->component->driver->remove_order == order)
1037 soc_remove_component(cpu_dai->component);
1041 static void soc_remove_dai_links(struct snd_soc_card *card)
1045 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1047 for (dai = 0; dai < card->num_rtd; dai++)
1048 soc_remove_link_dais(card, dai, order);
1051 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1053 for (dai = 0; dai < card->num_rtd; dai++)
1054 soc_remove_link_components(card, dai, order);
1060 static void soc_set_name_prefix(struct snd_soc_card *card,
1061 struct snd_soc_component *component)
1065 if (card->codec_conf == NULL)
1068 for (i = 0; i < card->num_configs; i++) {
1069 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1070 if (map->of_node && component->dev->of_node != map->of_node)
1072 if (map->dev_name && strcmp(component->name, map->dev_name))
1074 component->name_prefix = map->name_prefix;
1079 static int soc_probe_component(struct snd_soc_card *card,
1080 struct snd_soc_component *component)
1082 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1083 struct snd_soc_dai *dai;
1086 if (component->probed)
1089 component->card = card;
1091 soc_set_name_prefix(card, component);
1093 if (!try_module_get(component->dev->driver->owner))
1096 soc_init_component_debugfs(component);
1098 if (component->dapm_widgets) {
1099 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1100 component->num_dapm_widgets);
1103 dev_err(component->dev,
1104 "Failed to create new controls %d\n", ret);
1109 list_for_each_entry(dai, &component->dai_list, list) {
1110 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1112 dev_err(component->dev,
1113 "Failed to create DAI widgets %d\n", ret);
1118 if (component->probe) {
1119 ret = component->probe(component);
1121 dev_err(component->dev,
1122 "ASoC: failed to probe component %d\n", ret);
1126 WARN(dapm->idle_bias_off &&
1127 dapm->bias_level != SND_SOC_BIAS_OFF,
1128 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1132 if (component->controls)
1133 snd_soc_add_component_controls(component, component->controls,
1134 component->num_controls);
1135 if (component->dapm_routes)
1136 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1137 component->num_dapm_routes);
1139 component->probed = 1;
1140 list_add(&dapm->list, &card->dapm_list);
1142 /* This is a HACK and will be removed soon */
1143 if (component->codec)
1144 list_add(&component->codec->card_list, &card->codec_dev_list);
1149 soc_cleanup_component_debugfs(component);
1150 module_put(component->dev->driver->owner);
1155 static void rtd_release(struct device *dev)
1160 static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1165 /* register the rtd device */
1166 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1169 device_initialize(rtd->dev);
1170 rtd->dev->parent = rtd->card->dev;
1171 rtd->dev->release = rtd_release;
1172 dev_set_name(rtd->dev, "%s", name);
1173 dev_set_drvdata(rtd->dev, rtd);
1174 mutex_init(&rtd->pcm_mutex);
1175 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1176 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1177 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1178 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1179 ret = device_add(rtd->dev);
1181 /* calling put_device() here to free the rtd->dev */
1182 put_device(rtd->dev);
1183 dev_err(rtd->card->dev,
1184 "ASoC: failed to register runtime device: %d\n", ret);
1187 rtd->dev_registered = 1;
1190 /* add DAPM sysfs entries for this codec */
1191 ret = snd_soc_dapm_sys_add(rtd->dev);
1194 "ASoC: failed to add codec dapm sysfs entries: %d\n",
1197 /* add codec sysfs entries */
1198 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1201 "ASoC: failed to add codec sysfs files: %d\n",
1208 static int soc_probe_link_components(struct snd_soc_card *card, int num,
1211 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1212 struct snd_soc_platform *platform = rtd->platform;
1213 struct snd_soc_component *component;
1216 /* probe the CPU-side component, if it is a CODEC */
1217 component = rtd->cpu_dai->component;
1218 if (component->driver->probe_order == order) {
1219 ret = soc_probe_component(card, component);
1224 /* probe the CODEC-side components */
1225 for (i = 0; i < rtd->num_codecs; i++) {
1226 component = rtd->codec_dais[i]->component;
1227 if (component->driver->probe_order == order) {
1228 ret = soc_probe_component(card, component);
1234 /* probe the platform */
1235 if (platform->component.driver->probe_order == order) {
1236 ret = soc_probe_component(card, &platform->component);
1244 static int soc_probe_codec_dai(struct snd_soc_card *card,
1245 struct snd_soc_dai *codec_dai,
1250 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1251 if (codec_dai->driver->probe) {
1252 ret = codec_dai->driver->probe(codec_dai);
1254 dev_err(codec_dai->dev,
1255 "ASoC: failed to probe CODEC DAI %s: %d\n",
1256 codec_dai->name, ret);
1261 /* mark codec_dai as probed and add to card dai list */
1262 codec_dai->probed = 1;
1268 static int soc_link_dai_widgets(struct snd_soc_card *card,
1269 struct snd_soc_dai_link *dai_link,
1270 struct snd_soc_pcm_runtime *rtd)
1272 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1273 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1274 struct snd_soc_dapm_widget *play_w, *capture_w;
1277 if (rtd->num_codecs > 1)
1278 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1280 /* link the DAI widgets */
1281 play_w = codec_dai->playback_widget;
1282 capture_w = cpu_dai->capture_widget;
1283 if (play_w && capture_w) {
1284 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1287 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1288 play_w->name, capture_w->name, ret);
1293 play_w = cpu_dai->playback_widget;
1294 capture_w = codec_dai->capture_widget;
1295 if (play_w && capture_w) {
1296 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1299 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1300 play_w->name, capture_w->name, ret);
1308 static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1310 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1311 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1312 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1315 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1316 card->name, num, order);
1318 /* config components */
1319 cpu_dai->card = card;
1320 for (i = 0; i < rtd->num_codecs; i++)
1321 rtd->codec_dais[i]->card = card;
1323 /* set default power off timeout */
1324 rtd->pmdown_time = pmdown_time;
1326 /* probe the cpu_dai */
1327 if (!cpu_dai->probed &&
1328 cpu_dai->driver->probe_order == order) {
1329 if (cpu_dai->driver->probe) {
1330 ret = cpu_dai->driver->probe(cpu_dai);
1332 dev_err(cpu_dai->dev,
1333 "ASoC: failed to probe CPU DAI %s: %d\n",
1334 cpu_dai->name, ret);
1338 cpu_dai->probed = 1;
1341 /* probe the CODEC DAI */
1342 for (i = 0; i < rtd->num_codecs; i++) {
1343 ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order);
1348 /* complete DAI probe during last probe */
1349 if (order != SND_SOC_COMP_ORDER_LAST)
1352 /* do machine specific initialization */
1353 if (dai_link->init) {
1354 ret = dai_link->init(rtd);
1356 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1357 dai_link->name, ret);
1362 ret = soc_post_component_init(rtd, dai_link->name);
1366 #ifdef CONFIG_DEBUG_FS
1367 /* add DPCM sysfs entries */
1368 if (dai_link->dynamic) {
1369 ret = soc_dpcm_debugfs_add(rtd);
1372 "ASoC: failed to add dpcm sysfs entries: %d\n",
1379 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1381 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1384 if (cpu_dai->driver->compress_dai) {
1385 /*create compress_device"*/
1386 ret = soc_new_compress(rtd, num);
1388 dev_err(card->dev, "ASoC: can't create compress %s\n",
1389 dai_link->stream_name);
1394 if (!dai_link->params) {
1395 /* create the pcm */
1396 ret = soc_new_pcm(rtd, num);
1398 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1399 dai_link->stream_name, ret);
1403 INIT_DELAYED_WORK(&rtd->delayed_work,
1404 codec2codec_close_delayed_work);
1406 /* link the DAI widgets */
1407 ret = soc_link_dai_widgets(card, dai_link, rtd);
1413 /* add platform data for AC97 devices */
1414 for (i = 0; i < rtd->num_codecs; i++) {
1415 if (rtd->codec_dais[i]->driver->ac97_control)
1416 snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
1417 rtd->cpu_dai->ac97_pdata);
1423 #ifdef CONFIG_SND_SOC_AC97_BUS
1424 static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1425 struct snd_soc_dai *codec_dai)
1429 /* Only instantiate AC97 if not already done by the adaptor
1430 * for the generic AC97 subsystem.
1432 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
1434 * It is possible that the AC97 device is already registered to
1435 * the device subsystem. This happens when the device is created
1436 * via snd_ac97_mixer(). Currently only SoC codec that does so
1437 * is the generic AC97 glue but others migh emerge.
1439 * In those cases we don't try to register the device again.
1441 if (!codec->ac97_created)
1444 ret = soc_ac97_dev_register(codec);
1447 "ASoC: AC97 device register failed: %d\n", ret);
1451 codec->ac97_registered = 1;
1456 static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
1458 if (codec->ac97_registered) {
1459 soc_ac97_dev_unregister(codec);
1460 codec->ac97_registered = 0;
1464 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1468 for (i = 0; i < rtd->num_codecs; i++) {
1469 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1471 ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
1474 soc_unregister_ac97_codec(codec_dai->codec);
1482 static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1486 for (i = 0; i < rtd->num_codecs; i++)
1487 soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
1491 static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1493 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1494 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1495 const char *name = aux_dev->codec_name;
1497 rtd->component = soc_find_component(aux_dev->codec_of_node, name);
1498 if (!rtd->component) {
1499 if (aux_dev->codec_of_node)
1500 name = of_node_full_name(aux_dev->codec_of_node);
1502 dev_err(card->dev, "ASoC: %s not registered\n", name);
1503 return -EPROBE_DEFER;
1507 * Some places still reference rtd->codec, so we have to keep that
1508 * initialized if the component is a CODEC. Once all those references
1509 * have been removed, this code can be removed as well.
1511 rtd->codec = rtd->component->codec;
1516 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1518 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1519 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1522 ret = soc_probe_component(card, rtd->component);
1526 /* do machine specific initialization */
1527 if (aux_dev->init) {
1528 ret = aux_dev->init(rtd->component);
1530 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1531 aux_dev->name, ret);
1536 return soc_post_component_init(rtd, aux_dev->name);
1539 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1541 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1542 struct snd_soc_component *component = rtd->component;
1544 /* unregister the rtd device */
1545 if (rtd->dev_registered) {
1546 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1547 device_unregister(rtd->dev);
1548 rtd->dev_registered = 0;
1551 if (component && component->probed)
1552 soc_remove_component(component);
1555 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1559 if (codec->cache_init)
1562 ret = snd_soc_cache_init(codec);
1565 "ASoC: Failed to set cache compression type: %d\n",
1569 codec->cache_init = 1;
1573 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1575 struct snd_soc_codec *codec;
1576 struct snd_soc_dai_link *dai_link;
1577 int ret, i, order, dai_fmt;
1579 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1582 for (i = 0; i < card->num_links; i++) {
1583 ret = soc_bind_dai_link(card, i);
1588 /* bind aux_devs too */
1589 for (i = 0; i < card->num_aux_devs; i++) {
1590 ret = soc_bind_aux_dev(card, i);
1595 /* initialize the register cache for each available codec */
1596 list_for_each_entry(codec, &codec_list, list) {
1597 if (codec->cache_init)
1599 ret = snd_soc_init_codec_cache(codec);
1604 /* card bind complete so register a sound card */
1605 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1606 card->owner, 0, &card->snd_card);
1609 "ASoC: can't create sound card for card %s: %d\n",
1614 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1615 card->dapm.dev = card->dev;
1616 card->dapm.card = card;
1617 list_add(&card->dapm.list, &card->dapm_list);
1619 #ifdef CONFIG_DEBUG_FS
1620 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1623 #ifdef CONFIG_PM_SLEEP
1624 /* deferred resume work */
1625 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1628 if (card->dapm_widgets)
1629 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1630 card->num_dapm_widgets);
1632 /* initialise the sound card only once */
1634 ret = card->probe(card);
1636 goto card_probe_error;
1639 /* probe all components used by DAI links on this card */
1640 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1642 for (i = 0; i < card->num_links; i++) {
1643 ret = soc_probe_link_components(card, i, order);
1646 "ASoC: failed to instantiate card %d\n",
1653 /* probe all DAI links on this card */
1654 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1656 for (i = 0; i < card->num_links; i++) {
1657 ret = soc_probe_link_dais(card, i, order);
1660 "ASoC: failed to instantiate card %d\n",
1667 for (i = 0; i < card->num_aux_devs; i++) {
1668 ret = soc_probe_aux_dev(card, i);
1671 "ASoC: failed to add auxiliary devices %d\n",
1673 goto probe_aux_dev_err;
1677 snd_soc_dapm_link_dai_widgets(card);
1678 snd_soc_dapm_connect_dai_link_widgets(card);
1681 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1683 if (card->dapm_routes)
1684 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1685 card->num_dapm_routes);
1687 for (i = 0; i < card->num_links; i++) {
1688 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1689 dai_link = &card->dai_link[i];
1690 dai_fmt = dai_link->dai_fmt;
1693 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1696 for (j = 0; j < rtd->num_codecs; j++) {
1697 struct snd_soc_dai *codec_dai = codec_dais[j];
1699 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1700 if (ret != 0 && ret != -ENOTSUPP)
1701 dev_warn(codec_dai->dev,
1702 "ASoC: Failed to set DAI format: %d\n",
1707 /* If this is a regular CPU link there will be a platform */
1709 (dai_link->platform_name || dai_link->platform_of_node)) {
1710 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1712 if (ret != 0 && ret != -ENOTSUPP)
1713 dev_warn(card->rtd[i].cpu_dai->dev,
1714 "ASoC: Failed to set DAI format: %d\n",
1716 } else if (dai_fmt) {
1717 /* Flip the polarity for the "CPU" end */
1718 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1719 switch (dai_link->dai_fmt &
1720 SND_SOC_DAIFMT_MASTER_MASK) {
1721 case SND_SOC_DAIFMT_CBM_CFM:
1722 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1724 case SND_SOC_DAIFMT_CBM_CFS:
1725 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1727 case SND_SOC_DAIFMT_CBS_CFM:
1728 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1730 case SND_SOC_DAIFMT_CBS_CFS:
1731 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1735 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1737 if (ret != 0 && ret != -ENOTSUPP)
1738 dev_warn(card->rtd[i].cpu_dai->dev,
1739 "ASoC: Failed to set DAI format: %d\n",
1744 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1746 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1747 "%s", card->long_name ? card->long_name : card->name);
1748 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1749 "%s", card->driver_name ? card->driver_name : card->name);
1750 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1751 switch (card->snd_card->driver[i]) {
1757 if (!isalnum(card->snd_card->driver[i]))
1758 card->snd_card->driver[i] = '_';
1763 if (card->late_probe) {
1764 ret = card->late_probe(card);
1766 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
1768 goto probe_aux_dev_err;
1772 if (card->fully_routed)
1773 snd_soc_dapm_auto_nc_pins(card);
1775 snd_soc_dapm_new_widgets(card);
1777 ret = snd_card_register(card->snd_card);
1779 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1781 goto probe_aux_dev_err;
1784 #ifdef CONFIG_SND_SOC_AC97_BUS
1785 /* register any AC97 codecs */
1786 for (i = 0; i < card->num_rtd; i++) {
1787 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1790 "ASoC: failed to register AC97: %d\n", ret);
1792 soc_unregister_ac97_dai_link(&card->rtd[i]);
1793 goto probe_aux_dev_err;
1798 card->instantiated = 1;
1799 snd_soc_dapm_sync(&card->dapm);
1800 mutex_unlock(&card->mutex);
1805 for (i = 0; i < card->num_aux_devs; i++)
1806 soc_remove_aux_dev(card, i);
1809 soc_remove_dai_links(card);
1815 snd_card_free(card->snd_card);
1818 mutex_unlock(&card->mutex);
1823 /* probes a new socdev */
1824 static int soc_probe(struct platform_device *pdev)
1826 struct snd_soc_card *card = platform_get_drvdata(pdev);
1829 * no card, so machine driver should be registering card
1830 * we should not be here in that case so ret error
1835 dev_warn(&pdev->dev,
1836 "ASoC: machine %s should use snd_soc_register_card()\n",
1839 /* Bodge while we unpick instantiation */
1840 card->dev = &pdev->dev;
1842 return snd_soc_register_card(card);
1845 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1849 /* make sure any delayed work runs */
1850 for (i = 0; i < card->num_rtd; i++) {
1851 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1852 flush_delayed_work(&rtd->delayed_work);
1855 /* remove auxiliary devices */
1856 for (i = 0; i < card->num_aux_devs; i++)
1857 soc_remove_aux_dev(card, i);
1859 /* remove and free each DAI */
1860 soc_remove_dai_links(card);
1862 soc_cleanup_card_debugfs(card);
1864 /* remove the card */
1868 snd_soc_dapm_free(&card->dapm);
1870 snd_card_free(card->snd_card);
1875 /* removes a socdev */
1876 static int soc_remove(struct platform_device *pdev)
1878 struct snd_soc_card *card = platform_get_drvdata(pdev);
1880 snd_soc_unregister_card(card);
1884 int snd_soc_poweroff(struct device *dev)
1886 struct snd_soc_card *card = dev_get_drvdata(dev);
1889 if (!card->instantiated)
1892 /* Flush out pmdown_time work - we actually do want to run it
1893 * now, we're shutting down so no imminent restart. */
1894 for (i = 0; i < card->num_rtd; i++) {
1895 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1896 flush_delayed_work(&rtd->delayed_work);
1899 snd_soc_dapm_shutdown(card);
1901 /* deactivate pins to sleep state */
1902 for (i = 0; i < card->num_rtd; i++) {
1903 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1904 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1907 pinctrl_pm_select_sleep_state(cpu_dai->dev);
1908 for (j = 0; j < rtd->num_codecs; j++) {
1909 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
1910 pinctrl_pm_select_sleep_state(codec_dai->dev);
1916 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1918 const struct dev_pm_ops snd_soc_pm_ops = {
1919 .suspend = snd_soc_suspend,
1920 .resume = snd_soc_resume,
1921 .freeze = snd_soc_suspend,
1922 .thaw = snd_soc_resume,
1923 .poweroff = snd_soc_poweroff,
1924 .restore = snd_soc_resume,
1926 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1928 /* ASoC platform driver */
1929 static struct platform_driver soc_driver = {
1931 .name = "soc-audio",
1932 .owner = THIS_MODULE,
1933 .pm = &snd_soc_pm_ops,
1936 .remove = soc_remove,
1940 * snd_soc_new_ac97_codec - initailise AC97 device
1941 * @codec: audio codec
1942 * @ops: AC97 bus operations
1943 * @num: AC97 codec number
1945 * Initialises AC97 codec resources for use by ad-hoc devices only.
1947 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1948 struct snd_ac97_bus_ops *ops, int num)
1950 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1951 if (codec->ac97 == NULL)
1954 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1955 if (codec->ac97->bus == NULL) {
1961 codec->ac97->bus->ops = ops;
1962 codec->ac97->num = num;
1965 * Mark the AC97 device to be created by us. This way we ensure that the
1966 * device will be registered with the device subsystem later on.
1968 codec->ac97_created = 1;
1972 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1974 static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
1976 static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
1978 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
1980 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
1982 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
1986 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
1988 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
1992 static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
1994 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
1996 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
1998 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
1999 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2000 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2004 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2006 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2010 static int snd_soc_ac97_parse_pinctl(struct device *dev,
2011 struct snd_ac97_reset_cfg *cfg)
2014 struct pinctrl_state *state;
2018 p = devm_pinctrl_get(dev);
2020 dev_err(dev, "Failed to get pinctrl\n");
2025 state = pinctrl_lookup_state(p, "ac97-reset");
2026 if (IS_ERR(state)) {
2027 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
2028 return PTR_ERR(state);
2030 cfg->pstate_reset = state;
2032 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2033 if (IS_ERR(state)) {
2034 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
2035 return PTR_ERR(state);
2037 cfg->pstate_warm_reset = state;
2039 state = pinctrl_lookup_state(p, "ac97-running");
2040 if (IS_ERR(state)) {
2041 dev_err(dev, "Can't find pinctrl state ac97-running\n");
2042 return PTR_ERR(state);
2044 cfg->pstate_run = state;
2046 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2048 dev_err(dev, "Can't find ac97-sync gpio\n");
2051 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2053 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2056 cfg->gpio_sync = gpio;
2058 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2060 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2063 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2065 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2068 cfg->gpio_sdata = gpio;
2070 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2072 dev_err(dev, "Can't find ac97-reset gpio\n");
2075 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2077 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2080 cfg->gpio_reset = gpio;
2085 struct snd_ac97_bus_ops *soc_ac97_ops;
2086 EXPORT_SYMBOL_GPL(soc_ac97_ops);
2088 int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2090 if (ops == soc_ac97_ops)
2093 if (soc_ac97_ops && ops)
2100 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2103 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2105 * This function sets the reset and warm_reset properties of ops and parses
2106 * the device node of pdev to get pinctrl states and gpio numbers to use.
2108 int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2109 struct platform_device *pdev)
2111 struct device *dev = &pdev->dev;
2112 struct snd_ac97_reset_cfg cfg;
2115 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2119 ret = snd_soc_set_ac97_ops(ops);
2123 ops->warm_reset = snd_soc_ac97_warm_reset;
2124 ops->reset = snd_soc_ac97_reset;
2126 snd_ac97_rst_cfg = cfg;
2129 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2132 * snd_soc_free_ac97_codec - free AC97 codec device
2133 * @codec: audio codec
2135 * Frees AC97 codec device resources.
2137 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2139 #ifdef CONFIG_SND_SOC_AC97_BUS
2140 soc_unregister_ac97_codec(codec);
2142 kfree(codec->ac97->bus);
2145 codec->ac97_created = 0;
2147 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2150 * snd_soc_cnew - create new control
2151 * @_template: control template
2152 * @data: control private data
2153 * @long_name: control long name
2154 * @prefix: control name prefix
2156 * Create a new mixer control from a template control.
2158 * Returns 0 for success, else error.
2160 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2161 void *data, const char *long_name,
2164 struct snd_kcontrol_new template;
2165 struct snd_kcontrol *kcontrol;
2168 memcpy(&template, _template, sizeof(template));
2172 long_name = template.name;
2175 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2179 template.name = name;
2181 template.name = long_name;
2184 kcontrol = snd_ctl_new1(&template, data);
2190 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2192 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2193 const struct snd_kcontrol_new *controls, int num_controls,
2194 const char *prefix, void *data)
2198 for (i = 0; i < num_controls; i++) {
2199 const struct snd_kcontrol_new *control = &controls[i];
2200 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2201 control->name, prefix));
2203 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2204 control->name, err);
2212 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2215 struct snd_card *card = soc_card->snd_card;
2216 struct snd_kcontrol *kctl;
2218 if (unlikely(!name))
2221 list_for_each_entry(kctl, &card->controls, list)
2222 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2226 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2229 * snd_soc_add_component_controls - Add an array of controls to a component.
2231 * @component: Component to add controls to
2232 * @controls: Array of controls to add
2233 * @num_controls: Number of elements in the array
2235 * Return: 0 for success, else error.
2237 int snd_soc_add_component_controls(struct snd_soc_component *component,
2238 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2240 struct snd_card *card = component->card->snd_card;
2242 return snd_soc_add_controls(card, component->dev, controls,
2243 num_controls, component->name_prefix, component);
2245 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2248 * snd_soc_add_codec_controls - add an array of controls to a codec.
2249 * Convenience function to add a list of controls. Many codecs were
2250 * duplicating this code.
2252 * @codec: codec to add controls to
2253 * @controls: array of controls to add
2254 * @num_controls: number of elements in the array
2256 * Return 0 for success, else error.
2258 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2259 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2261 return snd_soc_add_component_controls(&codec->component, controls,
2264 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2267 * snd_soc_add_platform_controls - add an array of controls to a platform.
2268 * Convenience function to add a list of controls.
2270 * @platform: platform to add controls to
2271 * @controls: array of controls to add
2272 * @num_controls: number of elements in the array
2274 * Return 0 for success, else error.
2276 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2277 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2279 return snd_soc_add_component_controls(&platform->component, controls,
2282 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2285 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2286 * Convenience function to add a list of controls.
2288 * @soc_card: SoC card to add controls to
2289 * @controls: array of controls to add
2290 * @num_controls: number of elements in the array
2292 * Return 0 for success, else error.
2294 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2295 const struct snd_kcontrol_new *controls, int num_controls)
2297 struct snd_card *card = soc_card->snd_card;
2299 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2302 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2305 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2306 * Convienience function to add a list of controls.
2308 * @dai: DAI to add controls to
2309 * @controls: array of controls to add
2310 * @num_controls: number of elements in the array
2312 * Return 0 for success, else error.
2314 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2315 const struct snd_kcontrol_new *controls, int num_controls)
2317 struct snd_card *card = dai->card->snd_card;
2319 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2322 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2325 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2327 * @clk_id: DAI specific clock ID
2328 * @freq: new clock frequency in Hz
2329 * @dir: new clock direction - input/output.
2331 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2333 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2334 unsigned int freq, int dir)
2336 if (dai->driver && dai->driver->ops->set_sysclk)
2337 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2338 else if (dai->codec && dai->codec->driver->set_sysclk)
2339 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
2344 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2347 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2349 * @clk_id: DAI specific clock ID
2350 * @source: Source for the clock
2351 * @freq: new clock frequency in Hz
2352 * @dir: new clock direction - input/output.
2354 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2356 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2357 int source, unsigned int freq, int dir)
2359 if (codec->driver->set_sysclk)
2360 return codec->driver->set_sysclk(codec, clk_id, source,
2365 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2368 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2370 * @div_id: DAI specific clock divider ID
2371 * @div: new clock divisor.
2373 * Configures the clock dividers. This is used to derive the best DAI bit and
2374 * frame clocks from the system or master clock. It's best to set the DAI bit
2375 * and frame clocks as low as possible to save system power.
2377 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2378 int div_id, int div)
2380 if (dai->driver && dai->driver->ops->set_clkdiv)
2381 return dai->driver->ops->set_clkdiv(dai, div_id, div);
2385 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2388 * snd_soc_dai_set_pll - configure DAI PLL.
2390 * @pll_id: DAI specific PLL ID
2391 * @source: DAI specific source for the PLL
2392 * @freq_in: PLL input clock frequency in Hz
2393 * @freq_out: requested PLL output clock frequency in Hz
2395 * Configures and enables PLL to generate output clock based on input clock.
2397 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2398 unsigned int freq_in, unsigned int freq_out)
2400 if (dai->driver && dai->driver->ops->set_pll)
2401 return dai->driver->ops->set_pll(dai, pll_id, source,
2403 else if (dai->codec && dai->codec->driver->set_pll)
2404 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2409 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2412 * snd_soc_codec_set_pll - configure codec PLL.
2414 * @pll_id: DAI specific PLL ID
2415 * @source: DAI specific source for the PLL
2416 * @freq_in: PLL input clock frequency in Hz
2417 * @freq_out: requested PLL output clock frequency in Hz
2419 * Configures and enables PLL to generate output clock based on input clock.
2421 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2422 unsigned int freq_in, unsigned int freq_out)
2424 if (codec->driver->set_pll)
2425 return codec->driver->set_pll(codec, pll_id, source,
2430 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2433 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2435 * @ratio Ratio of BCLK to Sample rate.
2437 * Configures the DAI for a preset BCLK to sample rate ratio.
2439 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2441 if (dai->driver && dai->driver->ops->set_bclk_ratio)
2442 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2446 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2449 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2451 * @fmt: SND_SOC_DAIFMT_ format value.
2453 * Configures the DAI hardware format and clocking.
2455 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2457 if (dai->driver == NULL)
2459 if (dai->driver->ops->set_fmt == NULL)
2461 return dai->driver->ops->set_fmt(dai, fmt);
2463 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2466 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
2467 * @slots: Number of slots in use.
2468 * @tx_mask: bitmask representing active TX slots.
2469 * @rx_mask: bitmask representing active RX slots.
2471 * Generates the TDM tx and rx slot default masks for DAI.
2473 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
2474 unsigned int *tx_mask,
2475 unsigned int *rx_mask)
2477 if (*tx_mask || *rx_mask)
2483 *tx_mask = (1 << slots) - 1;
2484 *rx_mask = (1 << slots) - 1;
2490 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2492 * @tx_mask: bitmask representing active TX slots.
2493 * @rx_mask: bitmask representing active RX slots.
2494 * @slots: Number of slots in use.
2495 * @slot_width: Width in bits for each slot.
2497 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2500 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2501 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2503 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2504 dai->driver->ops->xlate_tdm_slot_mask(slots,
2505 &tx_mask, &rx_mask);
2507 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
2509 dai->tx_mask = tx_mask;
2510 dai->rx_mask = rx_mask;
2512 if (dai->driver && dai->driver->ops->set_tdm_slot)
2513 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2518 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2521 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2523 * @tx_num: how many TX channels
2524 * @tx_slot: pointer to an array which imply the TX slot number channel
2526 * @rx_num: how many RX channels
2527 * @rx_slot: pointer to an array which imply the RX slot number channel
2530 * configure the relationship between channel number and TDM slot number.
2532 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2533 unsigned int tx_num, unsigned int *tx_slot,
2534 unsigned int rx_num, unsigned int *rx_slot)
2536 if (dai->driver && dai->driver->ops->set_channel_map)
2537 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2542 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2545 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2547 * @tristate: tristate enable
2549 * Tristates the DAI so that others can use it.
2551 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2553 if (dai->driver && dai->driver->ops->set_tristate)
2554 return dai->driver->ops->set_tristate(dai, tristate);
2558 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2561 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2563 * @mute: mute enable
2564 * @direction: stream to mute
2566 * Mutes the DAI DAC.
2568 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2574 if (dai->driver->ops->mute_stream)
2575 return dai->driver->ops->mute_stream(dai, mute, direction);
2576 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2577 dai->driver->ops->digital_mute)
2578 return dai->driver->ops->digital_mute(dai, mute);
2582 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2584 static int snd_soc_init_multicodec(struct snd_soc_card *card,
2585 struct snd_soc_dai_link *dai_link)
2587 /* Legacy codec/codec_dai link is a single entry in multicodec */
2588 if (dai_link->codec_name || dai_link->codec_of_node ||
2589 dai_link->codec_dai_name) {
2590 dai_link->num_codecs = 1;
2592 dai_link->codecs = devm_kzalloc(card->dev,
2593 sizeof(struct snd_soc_dai_link_component),
2595 if (!dai_link->codecs)
2598 dai_link->codecs[0].name = dai_link->codec_name;
2599 dai_link->codecs[0].of_node = dai_link->codec_of_node;
2600 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
2603 if (!dai_link->codecs) {
2604 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
2612 * snd_soc_register_card - Register a card with the ASoC core
2614 * @card: Card to register
2617 int snd_soc_register_card(struct snd_soc_card *card)
2621 if (!card->name || !card->dev)
2624 for (i = 0; i < card->num_links; i++) {
2625 struct snd_soc_dai_link *link = &card->dai_link[i];
2627 ret = snd_soc_init_multicodec(card, link);
2629 dev_err(card->dev, "ASoC: failed to init multicodec\n");
2633 for (j = 0; j < link->num_codecs; j++) {
2635 * Codec must be specified by 1 of name or OF node,
2636 * not both or neither.
2638 if (!!link->codecs[j].name ==
2639 !!link->codecs[j].of_node) {
2640 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
2644 /* Codec DAI name must be specified */
2645 if (!link->codecs[j].dai_name) {
2646 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
2653 * Platform may be specified by either name or OF node, but
2654 * can be left unspecified, and a dummy platform will be used.
2656 if (link->platform_name && link->platform_of_node) {
2658 "ASoC: Both platform name/of_node are set for %s\n",
2664 * CPU device may be specified by either name or OF node, but
2665 * can be left unspecified, and will be matched based on DAI
2668 if (link->cpu_name && link->cpu_of_node) {
2670 "ASoC: Neither/both cpu name/of_node are set for %s\n",
2675 * At least one of CPU DAI name or CPU device name/node must be
2678 if (!link->cpu_dai_name &&
2679 !(link->cpu_name || link->cpu_of_node)) {
2681 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
2687 dev_set_drvdata(card->dev, card);
2689 snd_soc_initialize_card_lists(card);
2691 soc_init_card_debugfs(card);
2693 card->rtd = devm_kzalloc(card->dev,
2694 sizeof(struct snd_soc_pcm_runtime) *
2695 (card->num_links + card->num_aux_devs),
2697 if (card->rtd == NULL)
2700 card->rtd_aux = &card->rtd[card->num_links];
2702 for (i = 0; i < card->num_links; i++) {
2703 card->rtd[i].card = card;
2704 card->rtd[i].dai_link = &card->dai_link[i];
2705 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
2706 sizeof(struct snd_soc_dai *) *
2707 (card->rtd[i].dai_link->num_codecs),
2709 if (card->rtd[i].codec_dais == NULL)
2713 for (i = 0; i < card->num_aux_devs; i++)
2714 card->rtd_aux[i].card = card;
2716 INIT_LIST_HEAD(&card->dapm_dirty);
2717 card->instantiated = 0;
2718 mutex_init(&card->mutex);
2719 mutex_init(&card->dapm_mutex);
2721 ret = snd_soc_instantiate_card(card);
2723 soc_cleanup_card_debugfs(card);
2725 /* deactivate pins to sleep state */
2726 for (i = 0; i < card->num_rtd; i++) {
2727 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2728 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2731 for (j = 0; j < rtd->num_codecs; j++) {
2732 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2733 if (!codec_dai->active)
2734 pinctrl_pm_select_sleep_state(codec_dai->dev);
2737 if (!cpu_dai->active)
2738 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2743 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2746 * snd_soc_unregister_card - Unregister a card with the ASoC core
2748 * @card: Card to unregister
2751 int snd_soc_unregister_card(struct snd_soc_card *card)
2753 if (card->instantiated) {
2754 card->instantiated = false;
2755 snd_soc_dapm_shutdown(card);
2756 soc_cleanup_card_resources(card);
2758 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2762 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2765 * Simplify DAI link configuration by removing ".-1" from device names
2766 * and sanitizing names.
2768 static char *fmt_single_name(struct device *dev, int *id)
2770 char *found, name[NAME_SIZE];
2773 if (dev_name(dev) == NULL)
2776 strlcpy(name, dev_name(dev), NAME_SIZE);
2778 /* are we a "%s.%d" name (platform and SPI components) */
2779 found = strstr(name, dev->driver->name);
2782 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2784 /* discard ID from name if ID == -1 */
2786 found[strlen(dev->driver->name)] = '\0';
2790 /* I2C component devices are named "bus-addr" */
2791 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2792 char tmp[NAME_SIZE];
2794 /* create unique ID number from I2C addr and bus */
2795 *id = ((id1 & 0xffff) << 16) + id2;
2797 /* sanitize component name for DAI link creation */
2798 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2799 strlcpy(name, tmp, NAME_SIZE);
2804 return kstrdup(name, GFP_KERNEL);
2808 * Simplify DAI link naming for single devices with multiple DAIs by removing
2809 * any ".-1" and using the DAI name (instead of device name).
2811 static inline char *fmt_multiple_name(struct device *dev,
2812 struct snd_soc_dai_driver *dai_drv)
2814 if (dai_drv->name == NULL) {
2816 "ASoC: error - multiple DAI %s registered with no name\n",
2821 return kstrdup(dai_drv->name, GFP_KERNEL);
2825 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2827 * @component: The component for which the DAIs should be unregistered
2829 static void snd_soc_unregister_dais(struct snd_soc_component *component)
2831 struct snd_soc_dai *dai, *_dai;
2833 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
2834 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2836 list_del(&dai->list);
2843 * snd_soc_register_dais - Register a DAI with the ASoC core
2845 * @component: The component the DAIs are registered for
2846 * @dai_drv: DAI driver to use for the DAIs
2847 * @count: Number of DAIs
2848 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
2851 static int snd_soc_register_dais(struct snd_soc_component *component,
2852 struct snd_soc_dai_driver *dai_drv, size_t count,
2853 bool legacy_dai_naming)
2855 struct device *dev = component->dev;
2856 struct snd_soc_dai *dai;
2860 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
2862 component->dai_drv = dai_drv;
2863 component->num_dai = count;
2865 for (i = 0; i < count; i++) {
2867 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2874 * Back in the old days when we still had component-less DAIs,
2875 * instead of having a static name, component-less DAIs would
2876 * inherit the name of the parent device so it is possible to
2877 * register multiple instances of the DAI. We still need to keep
2878 * the same naming style even though those DAIs are not
2879 * component-less anymore.
2881 if (count == 1 && legacy_dai_naming) {
2882 dai->name = fmt_single_name(dev, &dai->id);
2884 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
2886 dai->id = dai_drv[i].id;
2890 if (dai->name == NULL) {
2896 dai->component = component;
2898 dai->driver = &dai_drv[i];
2899 if (!dai->driver->ops)
2900 dai->driver->ops = &null_dai_ops;
2902 list_add(&dai->list, &component->dai_list);
2904 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2910 snd_soc_unregister_dais(component);
2915 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
2916 enum snd_soc_dapm_type type, int subseq)
2918 struct snd_soc_component *component = dapm->component;
2920 component->driver->seq_notifier(component, type, subseq);
2923 static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
2926 struct snd_soc_component *component = dapm->component;
2928 return component->driver->stream_event(component, event);
2931 static int snd_soc_component_initialize(struct snd_soc_component *component,
2932 const struct snd_soc_component_driver *driver, struct device *dev)
2934 struct snd_soc_dapm_context *dapm;
2936 component->name = fmt_single_name(dev, &component->id);
2937 if (!component->name) {
2938 dev_err(dev, "ASoC: Failed to allocate name\n");
2942 component->dev = dev;
2943 component->driver = driver;
2944 component->probe = component->driver->probe;
2945 component->remove = component->driver->remove;
2947 if (!component->dapm_ptr)
2948 component->dapm_ptr = &component->dapm;
2950 dapm = component->dapm_ptr;
2952 dapm->component = component;
2953 dapm->bias_level = SND_SOC_BIAS_OFF;
2954 dapm->idle_bias_off = true;
2955 if (driver->seq_notifier)
2956 dapm->seq_notifier = snd_soc_component_seq_notifier;
2957 if (driver->stream_event)
2958 dapm->stream_event = snd_soc_component_stream_event;
2960 component->controls = driver->controls;
2961 component->num_controls = driver->num_controls;
2962 component->dapm_widgets = driver->dapm_widgets;
2963 component->num_dapm_widgets = driver->num_dapm_widgets;
2964 component->dapm_routes = driver->dapm_routes;
2965 component->num_dapm_routes = driver->num_dapm_routes;
2967 INIT_LIST_HEAD(&component->dai_list);
2968 mutex_init(&component->io_mutex);
2973 static void snd_soc_component_init_regmap(struct snd_soc_component *component)
2975 if (!component->regmap)
2976 component->regmap = dev_get_regmap(component->dev, NULL);
2977 if (component->regmap) {
2978 int val_bytes = regmap_get_val_bytes(component->regmap);
2979 /* Errors are legitimate for non-integer byte multiples */
2981 component->val_bytes = val_bytes;
2985 static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
2987 if (!component->write && !component->read)
2988 snd_soc_component_init_regmap(component);
2990 list_add(&component->list, &component_list);
2993 static void snd_soc_component_add(struct snd_soc_component *component)
2995 mutex_lock(&client_mutex);
2996 snd_soc_component_add_unlocked(component);
2997 mutex_unlock(&client_mutex);
3000 static void snd_soc_component_cleanup(struct snd_soc_component *component)
3002 snd_soc_unregister_dais(component);
3003 kfree(component->name);
3006 static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3008 list_del(&component->list);
3011 static void snd_soc_component_del(struct snd_soc_component *component)
3013 mutex_lock(&client_mutex);
3014 snd_soc_component_del_unlocked(component);
3015 mutex_unlock(&client_mutex);
3018 int snd_soc_register_component(struct device *dev,
3019 const struct snd_soc_component_driver *cmpnt_drv,
3020 struct snd_soc_dai_driver *dai_drv,
3023 struct snd_soc_component *cmpnt;
3026 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
3028 dev_err(dev, "ASoC: Failed to allocate memory\n");
3032 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
3036 cmpnt->ignore_pmdown_time = true;
3037 cmpnt->registered_as_component = true;
3039 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
3041 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
3045 snd_soc_component_add(cmpnt);
3050 snd_soc_component_cleanup(cmpnt);
3055 EXPORT_SYMBOL_GPL(snd_soc_register_component);
3058 * snd_soc_unregister_component - Unregister a component from the ASoC core
3061 void snd_soc_unregister_component(struct device *dev)
3063 struct snd_soc_component *cmpnt;
3065 list_for_each_entry(cmpnt, &component_list, list) {
3066 if (dev == cmpnt->dev && cmpnt->registered_as_component)
3072 snd_soc_component_del(cmpnt);
3073 snd_soc_component_cleanup(cmpnt);
3076 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3078 static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
3080 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3082 return platform->driver->probe(platform);
3085 static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
3087 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3089 platform->driver->remove(platform);
3093 * snd_soc_add_platform - Add a platform to the ASoC core
3094 * @dev: The parent device for the platform
3095 * @platform: The platform to add
3096 * @platform_driver: The driver for the platform
3098 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
3099 const struct snd_soc_platform_driver *platform_drv)
3103 ret = snd_soc_component_initialize(&platform->component,
3104 &platform_drv->component_driver, dev);
3108 platform->dev = dev;
3109 platform->driver = platform_drv;
3111 if (platform_drv->probe)
3112 platform->component.probe = snd_soc_platform_drv_probe;
3113 if (platform_drv->remove)
3114 platform->component.remove = snd_soc_platform_drv_remove;
3116 #ifdef CONFIG_DEBUG_FS
3117 platform->component.debugfs_prefix = "platform";
3120 mutex_lock(&client_mutex);
3121 snd_soc_component_add_unlocked(&platform->component);
3122 list_add(&platform->list, &platform_list);
3123 mutex_unlock(&client_mutex);
3125 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
3126 platform->component.name);
3130 EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3133 * snd_soc_register_platform - Register a platform with the ASoC core
3135 * @platform: platform to register
3137 int snd_soc_register_platform(struct device *dev,
3138 const struct snd_soc_platform_driver *platform_drv)
3140 struct snd_soc_platform *platform;
3143 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3145 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3146 if (platform == NULL)
3149 ret = snd_soc_add_platform(dev, platform, platform_drv);
3155 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3158 * snd_soc_remove_platform - Remove a platform from the ASoC core
3159 * @platform: the platform to remove
3161 void snd_soc_remove_platform(struct snd_soc_platform *platform)
3164 mutex_lock(&client_mutex);
3165 list_del(&platform->list);
3166 snd_soc_component_del_unlocked(&platform->component);
3167 mutex_unlock(&client_mutex);
3169 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3170 platform->component.name);
3172 snd_soc_component_cleanup(&platform->component);
3174 EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
3176 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
3178 struct snd_soc_platform *platform;
3180 list_for_each_entry(platform, &platform_list, list) {
3181 if (dev == platform->dev)
3187 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
3190 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3192 * @platform: platform to unregister
3194 void snd_soc_unregister_platform(struct device *dev)
3196 struct snd_soc_platform *platform;
3198 platform = snd_soc_lookup_platform(dev);
3202 snd_soc_remove_platform(platform);
3205 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3207 static u64 codec_format_map[] = {
3208 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3209 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3210 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3211 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3212 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3213 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3214 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3215 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3216 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3217 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3218 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3219 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3220 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3221 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3222 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3223 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3226 /* Fix up the DAI formats for endianness: codecs don't actually see
3227 * the endianness of the data but we're using the CPU format
3228 * definitions which do need to include endianness so we ensure that
3229 * codec DAIs always have both big and little endian variants set.
3231 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3235 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3236 if (stream->formats & codec_format_map[i])
3237 stream->formats |= codec_format_map[i];
3240 static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3242 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3244 return codec->driver->probe(codec);
3247 static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3249 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3251 codec->driver->remove(codec);
3254 static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3255 unsigned int reg, unsigned int val)
3257 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3259 return codec->driver->write(codec, reg, val);
3262 static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3263 unsigned int reg, unsigned int *val)
3265 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3267 *val = codec->driver->read(codec, reg);
3272 static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
3273 enum snd_soc_bias_level level)
3275 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
3277 return codec->driver->set_bias_level(codec, level);
3281 * snd_soc_register_codec - Register a codec with the ASoC core
3283 * @codec: codec to register
3285 int snd_soc_register_codec(struct device *dev,
3286 const struct snd_soc_codec_driver *codec_drv,
3287 struct snd_soc_dai_driver *dai_drv,
3290 struct snd_soc_codec *codec;
3291 struct snd_soc_dai *dai;
3294 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3296 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3300 codec->component.dapm_ptr = &codec->dapm;
3301 codec->component.codec = codec;
3303 ret = snd_soc_component_initialize(&codec->component,
3304 &codec_drv->component_driver, dev);
3308 if (codec_drv->controls) {
3309 codec->component.controls = codec_drv->controls;
3310 codec->component.num_controls = codec_drv->num_controls;
3312 if (codec_drv->dapm_widgets) {
3313 codec->component.dapm_widgets = codec_drv->dapm_widgets;
3314 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
3316 if (codec_drv->dapm_routes) {
3317 codec->component.dapm_routes = codec_drv->dapm_routes;
3318 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
3321 if (codec_drv->probe)
3322 codec->component.probe = snd_soc_codec_drv_probe;
3323 if (codec_drv->remove)
3324 codec->component.remove = snd_soc_codec_drv_remove;
3325 if (codec_drv->write)
3326 codec->component.write = snd_soc_codec_drv_write;
3327 if (codec_drv->read)
3328 codec->component.read = snd_soc_codec_drv_read;
3329 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
3330 codec->dapm.idle_bias_off = codec_drv->idle_bias_off;
3331 codec->dapm.suspend_bias_off = codec_drv->suspend_bias_off;
3332 if (codec_drv->seq_notifier)
3333 codec->dapm.seq_notifier = codec_drv->seq_notifier;
3334 if (codec_drv->set_bias_level)
3335 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
3337 codec->driver = codec_drv;
3338 codec->component.val_bytes = codec_drv->reg_word_size;
3339 mutex_init(&codec->mutex);
3341 #ifdef CONFIG_DEBUG_FS
3342 codec->component.init_debugfs = soc_init_codec_debugfs;
3343 codec->component.debugfs_prefix = "codec";
3346 if (codec_drv->get_regmap)
3347 codec->component.regmap = codec_drv->get_regmap(dev);
3349 for (i = 0; i < num_dai; i++) {
3350 fixup_codec_formats(&dai_drv[i].playback);
3351 fixup_codec_formats(&dai_drv[i].capture);
3354 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3356 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
3360 list_for_each_entry(dai, &codec->component.dai_list, list)
3363 mutex_lock(&client_mutex);
3364 snd_soc_component_add_unlocked(&codec->component);
3365 list_add(&codec->list, &codec_list);
3366 mutex_unlock(&client_mutex);
3368 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3369 codec->component.name);
3373 snd_soc_component_cleanup(&codec->component);
3378 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3381 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3383 * @codec: codec to unregister
3385 void snd_soc_unregister_codec(struct device *dev)
3387 struct snd_soc_codec *codec;
3389 list_for_each_entry(codec, &codec_list, list) {
3390 if (dev == codec->dev)
3397 mutex_lock(&client_mutex);
3398 list_del(&codec->list);
3399 snd_soc_component_del_unlocked(&codec->component);
3400 mutex_unlock(&client_mutex);
3402 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3403 codec->component.name);
3405 snd_soc_component_cleanup(&codec->component);
3406 snd_soc_cache_exit(codec);
3409 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3411 /* Retrieve a card's name from device tree */
3412 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3413 const char *propname)
3415 struct device_node *np;
3419 pr_err("card->dev is not set before calling %s\n", __func__);
3423 np = card->dev->of_node;
3425 ret = of_property_read_string_index(np, propname, 0, &card->name);
3427 * EINVAL means the property does not exist. This is fine providing
3428 * card->name was previously set, which is checked later in
3429 * snd_soc_register_card.
3431 if (ret < 0 && ret != -EINVAL) {
3433 "ASoC: Property '%s' could not be read: %d\n",
3440 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3442 static const struct snd_soc_dapm_widget simple_widgets[] = {
3443 SND_SOC_DAPM_MIC("Microphone", NULL),
3444 SND_SOC_DAPM_LINE("Line", NULL),
3445 SND_SOC_DAPM_HP("Headphone", NULL),
3446 SND_SOC_DAPM_SPK("Speaker", NULL),
3449 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
3450 const char *propname)
3452 struct device_node *np = card->dev->of_node;
3453 struct snd_soc_dapm_widget *widgets;
3454 const char *template, *wname;
3455 int i, j, num_widgets, ret;
3457 num_widgets = of_property_count_strings(np, propname);
3458 if (num_widgets < 0) {
3460 "ASoC: Property '%s' does not exist\n", propname);
3463 if (num_widgets & 1) {
3465 "ASoC: Property '%s' length is not even\n", propname);
3471 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3476 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3480 "ASoC: Could not allocate memory for widgets\n");
3484 for (i = 0; i < num_widgets; i++) {
3485 ret = of_property_read_string_index(np, propname,
3489 "ASoC: Property '%s' index %d read error:%d\n",
3490 propname, 2 * i, ret);
3494 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3495 if (!strncmp(template, simple_widgets[j].name,
3496 strlen(simple_widgets[j].name))) {
3497 widgets[i] = simple_widgets[j];
3502 if (j >= ARRAY_SIZE(simple_widgets)) {
3504 "ASoC: DAPM widget '%s' is not supported\n",
3509 ret = of_property_read_string_index(np, propname,
3514 "ASoC: Property '%s' index %d read error:%d\n",
3515 propname, (2 * i) + 1, ret);
3519 widgets[i].name = wname;
3522 card->dapm_widgets = widgets;
3523 card->num_dapm_widgets = num_widgets;
3527 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
3529 int snd_soc_of_parse_tdm_slot(struct device_node *np,
3530 unsigned int *slots,
3531 unsigned int *slot_width)
3536 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3537 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3545 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3546 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3556 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3558 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3559 const char *propname)
3561 struct device_node *np = card->dev->of_node;
3563 struct snd_soc_dapm_route *routes;
3566 num_routes = of_property_count_strings(np, propname);
3567 if (num_routes < 0 || num_routes & 1) {
3569 "ASoC: Property '%s' does not exist or its length is not even\n",
3575 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3580 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3584 "ASoC: Could not allocate DAPM route table\n");
3588 for (i = 0; i < num_routes; i++) {
3589 ret = of_property_read_string_index(np, propname,
3590 2 * i, &routes[i].sink);
3593 "ASoC: Property '%s' index %d could not be read: %d\n",
3594 propname, 2 * i, ret);
3597 ret = of_property_read_string_index(np, propname,
3598 (2 * i) + 1, &routes[i].source);
3601 "ASoC: Property '%s' index %d could not be read: %d\n",
3602 propname, (2 * i) + 1, ret);
3607 card->num_dapm_routes = num_routes;
3608 card->dapm_routes = routes;
3612 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3614 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3616 struct device_node **bitclkmaster,
3617 struct device_node **framemaster)
3621 unsigned int format = 0;
3627 } of_fmt_table[] = {
3628 { "i2s", SND_SOC_DAIFMT_I2S },
3629 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3630 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3631 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3632 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3633 { "ac97", SND_SOC_DAIFMT_AC97 },
3634 { "pdm", SND_SOC_DAIFMT_PDM},
3635 { "msb", SND_SOC_DAIFMT_MSB },
3636 { "lsb", SND_SOC_DAIFMT_LSB },
3643 * check "[prefix]format = xxx"
3644 * SND_SOC_DAIFMT_FORMAT_MASK area
3646 snprintf(prop, sizeof(prop), "%sformat", prefix);
3647 ret = of_property_read_string(np, prop, &str);
3649 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3650 if (strcmp(str, of_fmt_table[i].name) == 0) {
3651 format |= of_fmt_table[i].val;
3658 * check "[prefix]continuous-clock"
3659 * SND_SOC_DAIFMT_CLOCK_MASK area
3661 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
3662 if (of_get_property(np, prop, NULL))
3663 format |= SND_SOC_DAIFMT_CONT;
3665 format |= SND_SOC_DAIFMT_GATED;
3668 * check "[prefix]bitclock-inversion"
3669 * check "[prefix]frame-inversion"
3670 * SND_SOC_DAIFMT_INV_MASK area
3672 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3673 bit = !!of_get_property(np, prop, NULL);
3675 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3676 frame = !!of_get_property(np, prop, NULL);
3678 switch ((bit << 4) + frame) {
3680 format |= SND_SOC_DAIFMT_IB_IF;
3683 format |= SND_SOC_DAIFMT_IB_NF;
3686 format |= SND_SOC_DAIFMT_NB_IF;
3689 /* SND_SOC_DAIFMT_NB_NF is default */
3694 * check "[prefix]bitclock-master"
3695 * check "[prefix]frame-master"
3696 * SND_SOC_DAIFMT_MASTER_MASK area
3698 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3699 bit = !!of_get_property(np, prop, NULL);
3700 if (bit && bitclkmaster)
3701 *bitclkmaster = of_parse_phandle(np, prop, 0);
3703 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3704 frame = !!of_get_property(np, prop, NULL);
3705 if (frame && framemaster)
3706 *framemaster = of_parse_phandle(np, prop, 0);
3708 switch ((bit << 4) + frame) {
3710 format |= SND_SOC_DAIFMT_CBM_CFM;
3713 format |= SND_SOC_DAIFMT_CBM_CFS;
3716 format |= SND_SOC_DAIFMT_CBS_CFM;
3719 format |= SND_SOC_DAIFMT_CBS_CFS;
3725 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3727 int snd_soc_of_get_dai_name(struct device_node *of_node,
3728 const char **dai_name)
3730 struct snd_soc_component *pos;
3731 struct of_phandle_args args;
3734 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3735 "#sound-dai-cells", 0, &args);
3739 ret = -EPROBE_DEFER;
3741 mutex_lock(&client_mutex);
3742 list_for_each_entry(pos, &component_list, list) {
3743 if (pos->dev->of_node != args.np)
3746 if (pos->driver->of_xlate_dai_name) {
3747 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
3751 switch (args.args_count) {
3753 id = 0; /* same as dai_drv[0] */
3763 if (id < 0 || id >= pos->num_dai) {
3770 *dai_name = pos->dai_drv[id].name;
3772 *dai_name = pos->name;
3777 mutex_unlock(&client_mutex);
3779 of_node_put(args.np);
3783 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3785 static int __init snd_soc_init(void)
3787 #ifdef CONFIG_DEBUG_FS
3788 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3789 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
3790 pr_warn("ASoC: Failed to create debugfs directory\n");
3791 snd_soc_debugfs_root = NULL;
3794 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
3796 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3798 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
3800 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
3802 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
3803 &platform_list_fops))
3804 pr_warn("ASoC: Failed to create platform list debugfs file\n");
3807 snd_soc_util_init();
3809 return platform_driver_register(&soc_driver);
3811 module_init(snd_soc_init);
3813 static void __exit snd_soc_exit(void)
3815 snd_soc_util_exit();
3817 #ifdef CONFIG_DEBUG_FS
3818 debugfs_remove_recursive(snd_soc_debugfs_root);
3820 platform_driver_unregister(&soc_driver);
3822 module_exit(snd_soc_exit);
3824 /* Module information */
3825 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3826 MODULE_DESCRIPTION("ALSA SoC Core");
3827 MODULE_LICENSE("GPL");
3828 MODULE_ALIAS("platform:soc-audio");