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/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>
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 /* 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.
76 static const char * const dmi_blacklist[] = {
77 "To be filled by OEM",
83 NULL, /* terminator */
86 /* returns the minimum number of bytes needed to represent
87 * a particular given value */
88 static int min_bytes_needed(unsigned long val)
93 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
96 c = (sizeof val * 8) - c;
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)
109 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
110 int regsize = codec->driver->reg_word_size * 2;
113 /* +2 for ': ' and + 1 for '\n' */
114 if (wordsize + regsize + 2 + 1 != len)
117 sprintf(buf, "%.*x: ", wordsize, reg);
120 ret = snd_soc_read(codec, reg);
122 memset(buf, 'X', regsize);
124 sprintf(buf, "%.*x", regsize, ret);
126 /* no NUL-termination needed */
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)
135 int wordsize, regsize;
140 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
141 regsize = codec->driver->reg_word_size * 2;
143 len = wordsize + regsize + 2 + 1;
145 if (!codec->driver->reg_cache_size)
148 if (codec->driver->reg_cache_step)
149 step = codec->driver->reg_cache_step;
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 */
155 if (total + len >= count - 1)
157 format_register_str(codec, i, buf + total, len);
163 total = min(total, count - 1);
168 static ssize_t codec_reg_show(struct device *dev,
169 struct device_attribute *attr, char *buf)
171 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
173 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
176 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
178 static ssize_t pmdown_time_show(struct device *dev,
179 struct device_attribute *attr, char *buf)
181 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
183 return sprintf(buf, "%ld\n", rtd->pmdown_time);
186 static ssize_t pmdown_time_set(struct device *dev,
187 struct device_attribute *attr,
188 const char *buf, size_t count)
190 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
193 ret = kstrtol(buf, 10, &rtd->pmdown_time);
200 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
202 static struct attribute *soc_dev_attrs[] = {
203 &dev_attr_codec_reg.attr,
204 &dev_attr_pmdown_time.attr,
208 static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
209 struct attribute *attr, int idx)
211 struct device *dev = kobj_to_dev(kobj);
212 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
214 if (attr == &dev_attr_pmdown_time.attr)
215 return attr->mode; /* always visible */
216 return rtd->codec ? attr->mode : 0; /* enabled only with codec */
219 static const struct attribute_group soc_dapm_dev_group = {
220 .attrs = soc_dapm_dev_attrs,
221 .is_visible = soc_dev_attr_is_visible,
224 static const struct attribute_group soc_dev_roup = {
225 .attrs = soc_dev_attrs,
226 .is_visible = soc_dev_attr_is_visible,
229 static const struct attribute_group *soc_dev_attr_groups[] = {
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)
240 struct snd_soc_codec *codec = file->private_data;
243 if (*ppos < 0 || !count)
246 buf = kmalloc(count, GFP_KERNEL);
250 ret = soc_codec_reg_show(codec, buf, count, *ppos);
252 if (copy_to_user(user_buf, buf, ret)) {
263 static ssize_t codec_reg_write_file(struct file *file,
264 const char __user *user_buf, size_t count, loff_t *ppos)
269 unsigned long reg, value;
270 struct snd_soc_codec *codec = file->private_data;
273 buf_size = min(count, (sizeof(buf)-1));
274 if (copy_from_user(buf, user_buf, buf_size))
278 while (*start == ' ')
280 reg = simple_strtoul(start, &start, 16);
281 while (*start == ' ')
283 ret = kstrtoul(start, 16, &value);
287 /* Userspace has been fiddling around behind the kernel's back */
288 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
290 snd_soc_write(codec, reg, value);
294 static const struct file_operations codec_reg_fops = {
296 .read = codec_reg_read_file,
297 .write = codec_reg_write_file,
298 .llseek = default_llseek,
301 static void soc_init_component_debugfs(struct snd_soc_component *component)
303 if (!component->card->debugfs_card_root)
306 if (component->debugfs_prefix) {
309 name = kasprintf(GFP_KERNEL, "%s:%s",
310 component->debugfs_prefix, component->name);
312 component->debugfs_root = debugfs_create_dir(name,
313 component->card->debugfs_card_root);
317 component->debugfs_root = debugfs_create_dir(component->name,
318 component->card->debugfs_card_root);
321 if (!component->debugfs_root) {
322 dev_warn(component->dev,
323 "ASoC: Failed to create component debugfs directory\n");
327 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
328 component->debugfs_root);
330 if (component->init_debugfs)
331 component->init_debugfs(component);
334 static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
336 debugfs_remove_recursive(component->debugfs_root);
339 static void soc_init_codec_debugfs(struct snd_soc_component *component)
341 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
342 struct dentry *debugfs_reg;
344 debugfs_reg = debugfs_create_file("codec_reg", 0644,
345 codec->component.debugfs_root,
346 codec, &codec_reg_fops);
349 "ASoC: Failed to create codec register debugfs file\n");
352 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
353 size_t count, loff_t *ppos)
355 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
356 ssize_t len, ret = 0;
357 struct snd_soc_codec *codec;
362 mutex_lock(&client_mutex);
364 list_for_each_entry(codec, &codec_list, list) {
365 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
366 codec->component.name);
369 if (ret > PAGE_SIZE) {
375 mutex_unlock(&client_mutex);
378 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
385 static const struct file_operations codec_list_fops = {
386 .read = codec_list_read_file,
387 .llseek = default_llseek,/* read accesses f_pos */
390 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
391 size_t count, loff_t *ppos)
393 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
394 ssize_t len, ret = 0;
395 struct snd_soc_component *component;
396 struct snd_soc_dai *dai;
401 mutex_lock(&client_mutex);
403 list_for_each_entry(component, &component_list, list) {
404 list_for_each_entry(dai, &component->dai_list, list) {
405 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
409 if (ret > PAGE_SIZE) {
416 mutex_unlock(&client_mutex);
418 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
425 static const struct file_operations dai_list_fops = {
426 .read = dai_list_read_file,
427 .llseek = default_llseek,/* read accesses f_pos */
430 static ssize_t platform_list_read_file(struct file *file,
431 char __user *user_buf,
432 size_t count, loff_t *ppos)
434 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
435 ssize_t len, ret = 0;
436 struct snd_soc_platform *platform;
441 mutex_lock(&client_mutex);
443 list_for_each_entry(platform, &platform_list, list) {
444 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
445 platform->component.name);
448 if (ret > PAGE_SIZE) {
454 mutex_unlock(&client_mutex);
456 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
463 static const struct file_operations platform_list_fops = {
464 .read = platform_list_read_file,
465 .llseek = default_llseek,/* read accesses f_pos */
468 static void soc_init_card_debugfs(struct snd_soc_card *card)
470 if (!snd_soc_debugfs_root)
473 card->debugfs_card_root = debugfs_create_dir(card->name,
474 snd_soc_debugfs_root);
475 if (!card->debugfs_card_root) {
477 "ASoC: Failed to create card debugfs directory\n");
481 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
482 card->debugfs_card_root,
484 if (!card->debugfs_pop_time)
486 "ASoC: Failed to create pop time debugfs file\n");
489 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
491 debugfs_remove_recursive(card->debugfs_card_root);
495 static void snd_soc_debugfs_init(void)
497 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
498 if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) {
499 pr_warn("ASoC: Failed to create debugfs directory\n");
500 snd_soc_debugfs_root = NULL;
504 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
506 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
508 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
510 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
512 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
513 &platform_list_fops))
514 pr_warn("ASoC: Failed to create platform list debugfs file\n");
517 static void snd_soc_debugfs_exit(void)
519 debugfs_remove_recursive(snd_soc_debugfs_root);
524 #define soc_init_codec_debugfs NULL
526 static inline void soc_init_component_debugfs(
527 struct snd_soc_component *component)
531 static inline void soc_cleanup_component_debugfs(
532 struct snd_soc_component *component)
536 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
540 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
544 static inline void snd_soc_debugfs_init(void)
548 static inline void snd_soc_debugfs_exit(void)
554 static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
555 struct snd_soc_component *component)
557 struct snd_soc_rtdcom_list *rtdcom;
558 struct snd_soc_rtdcom_list *new_rtdcom;
560 for_each_rtdcom(rtd, rtdcom) {
561 /* already connected */
562 if (rtdcom->component == component)
566 new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
570 new_rtdcom->component = component;
571 INIT_LIST_HEAD(&new_rtdcom->list);
573 list_add_tail(&new_rtdcom->list, &rtd->component_list);
578 static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
580 struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
582 for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
585 INIT_LIST_HEAD(&rtd->component_list);
588 struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
589 const char *driver_name)
591 struct snd_soc_rtdcom_list *rtdcom;
593 for_each_rtdcom(rtd, rtdcom) {
594 if ((rtdcom->component->driver->name == driver_name) ||
595 strcmp(rtdcom->component->driver->name, driver_name) == 0)
596 return rtdcom->component;
602 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
603 const char *dai_link, int stream)
605 struct snd_soc_pcm_runtime *rtd;
607 list_for_each_entry(rtd, &card->rtd_list, list) {
608 if (rtd->dai_link->no_pcm &&
609 !strcmp(rtd->dai_link->name, dai_link))
610 return rtd->pcm->streams[stream].substream;
612 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
615 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
617 static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
618 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
620 struct snd_soc_pcm_runtime *rtd;
622 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
626 INIT_LIST_HEAD(&rtd->component_list);
628 rtd->dai_link = dai_link;
629 rtd->codec_dais = kzalloc(sizeof(struct snd_soc_dai *) *
630 dai_link->num_codecs,
632 if (!rtd->codec_dais) {
640 static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
642 if (rtd && rtd->codec_dais)
643 kfree(rtd->codec_dais);
644 snd_soc_rtdcom_del_all(rtd);
648 static void soc_add_pcm_runtime(struct snd_soc_card *card,
649 struct snd_soc_pcm_runtime *rtd)
651 list_add_tail(&rtd->list, &card->rtd_list);
652 rtd->num = card->num_rtd;
656 static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
658 struct snd_soc_pcm_runtime *rtd, *_rtd;
660 list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) {
661 list_del(&rtd->list);
662 soc_free_pcm_runtime(rtd);
668 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
669 const char *dai_link)
671 struct snd_soc_pcm_runtime *rtd;
673 list_for_each_entry(rtd, &card->rtd_list, list) {
674 if (!strcmp(rtd->dai_link->name, dai_link))
677 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
680 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
682 static void codec2codec_close_delayed_work(struct work_struct *work)
684 /* Currently nothing to do for c2c links
685 * Since c2c links are internal nodes in the DAPM graph and
686 * don't interface with the outside world or application layer
687 * we don't have to do any special handling on close.
691 #ifdef CONFIG_PM_SLEEP
692 /* powers down audio subsystem for suspend */
693 int snd_soc_suspend(struct device *dev)
695 struct snd_soc_card *card = dev_get_drvdata(dev);
696 struct snd_soc_component *component;
697 struct snd_soc_pcm_runtime *rtd;
700 /* If the card is not initialized yet there is nothing to do */
701 if (!card->instantiated)
704 /* Due to the resume being scheduled into a workqueue we could
705 * suspend before that's finished - wait for it to complete.
707 snd_power_lock(card->snd_card);
708 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
709 snd_power_unlock(card->snd_card);
711 /* we're going to block userspace touching us until resume completes */
712 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
714 /* mute any active DACs */
715 list_for_each_entry(rtd, &card->rtd_list, list) {
717 if (rtd->dai_link->ignore_suspend)
720 for (i = 0; i < rtd->num_codecs; i++) {
721 struct snd_soc_dai *dai = rtd->codec_dais[i];
722 struct snd_soc_dai_driver *drv = dai->driver;
724 if (drv->ops->digital_mute && dai->playback_active)
725 drv->ops->digital_mute(dai, 1);
729 /* suspend all pcms */
730 list_for_each_entry(rtd, &card->rtd_list, list) {
731 if (rtd->dai_link->ignore_suspend)
734 snd_pcm_suspend_all(rtd->pcm);
737 if (card->suspend_pre)
738 card->suspend_pre(card);
740 list_for_each_entry(rtd, &card->rtd_list, list) {
741 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
743 if (rtd->dai_link->ignore_suspend)
746 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
747 cpu_dai->driver->suspend(cpu_dai);
750 /* close any waiting streams */
751 list_for_each_entry(rtd, &card->rtd_list, list)
752 flush_delayed_work(&rtd->delayed_work);
754 list_for_each_entry(rtd, &card->rtd_list, list) {
756 if (rtd->dai_link->ignore_suspend)
759 snd_soc_dapm_stream_event(rtd,
760 SNDRV_PCM_STREAM_PLAYBACK,
761 SND_SOC_DAPM_STREAM_SUSPEND);
763 snd_soc_dapm_stream_event(rtd,
764 SNDRV_PCM_STREAM_CAPTURE,
765 SND_SOC_DAPM_STREAM_SUSPEND);
768 /* Recheck all endpoints too, their state is affected by suspend */
769 dapm_mark_endpoints_dirty(card);
770 snd_soc_dapm_sync(&card->dapm);
772 /* suspend all COMPONENTs */
773 list_for_each_entry(component, &card->component_dev_list, card_list) {
774 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
776 /* If there are paths active then the COMPONENT will be held with
777 * bias _ON and should not be suspended. */
778 if (!component->suspended) {
779 switch (snd_soc_dapm_get_bias_level(dapm)) {
780 case SND_SOC_BIAS_STANDBY:
782 * If the COMPONENT is capable of idle
783 * bias off then being in STANDBY
784 * means it's doing something,
785 * otherwise fall through.
787 if (dapm->idle_bias_off) {
788 dev_dbg(component->dev,
789 "ASoC: idle_bias_off CODEC on over suspend\n");
793 case SND_SOC_BIAS_OFF:
794 if (component->suspend)
795 component->suspend(component);
796 component->suspended = 1;
797 if (component->regmap)
798 regcache_mark_dirty(component->regmap);
799 /* deactivate pins to sleep state */
800 pinctrl_pm_select_sleep_state(component->dev);
803 dev_dbg(component->dev,
804 "ASoC: COMPONENT is on over suspend\n");
810 list_for_each_entry(rtd, &card->rtd_list, list) {
811 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
813 if (rtd->dai_link->ignore_suspend)
816 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
817 cpu_dai->driver->suspend(cpu_dai);
819 /* deactivate pins to sleep state */
820 pinctrl_pm_select_sleep_state(cpu_dai->dev);
823 if (card->suspend_post)
824 card->suspend_post(card);
828 EXPORT_SYMBOL_GPL(snd_soc_suspend);
830 /* deferred resume work, so resume can complete before we finished
831 * setting our codec back up, which can be very slow on I2C
833 static void soc_resume_deferred(struct work_struct *work)
835 struct snd_soc_card *card =
836 container_of(work, struct snd_soc_card, deferred_resume_work);
837 struct snd_soc_pcm_runtime *rtd;
838 struct snd_soc_component *component;
841 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
842 * so userspace apps are blocked from touching us
845 dev_dbg(card->dev, "ASoC: starting resume work\n");
847 /* Bring us up into D2 so that DAPM starts enabling things */
848 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
850 if (card->resume_pre)
851 card->resume_pre(card);
853 /* resume control bus DAIs */
854 list_for_each_entry(rtd, &card->rtd_list, list) {
855 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
857 if (rtd->dai_link->ignore_suspend)
860 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
861 cpu_dai->driver->resume(cpu_dai);
864 list_for_each_entry(component, &card->component_dev_list, card_list) {
865 if (component->suspended) {
866 if (component->resume)
867 component->resume(component);
868 component->suspended = 0;
872 list_for_each_entry(rtd, &card->rtd_list, list) {
874 if (rtd->dai_link->ignore_suspend)
877 snd_soc_dapm_stream_event(rtd,
878 SNDRV_PCM_STREAM_PLAYBACK,
879 SND_SOC_DAPM_STREAM_RESUME);
881 snd_soc_dapm_stream_event(rtd,
882 SNDRV_PCM_STREAM_CAPTURE,
883 SND_SOC_DAPM_STREAM_RESUME);
886 /* unmute any active DACs */
887 list_for_each_entry(rtd, &card->rtd_list, list) {
889 if (rtd->dai_link->ignore_suspend)
892 for (i = 0; i < rtd->num_codecs; i++) {
893 struct snd_soc_dai *dai = rtd->codec_dais[i];
894 struct snd_soc_dai_driver *drv = dai->driver;
896 if (drv->ops->digital_mute && dai->playback_active)
897 drv->ops->digital_mute(dai, 0);
901 list_for_each_entry(rtd, &card->rtd_list, list) {
902 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
904 if (rtd->dai_link->ignore_suspend)
907 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
908 cpu_dai->driver->resume(cpu_dai);
911 if (card->resume_post)
912 card->resume_post(card);
914 dev_dbg(card->dev, "ASoC: resume work completed\n");
916 /* Recheck all endpoints too, their state is affected by suspend */
917 dapm_mark_endpoints_dirty(card);
918 snd_soc_dapm_sync(&card->dapm);
920 /* userspace can access us now we are back as we were before */
921 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
924 /* powers up audio subsystem after a suspend */
925 int snd_soc_resume(struct device *dev)
927 struct snd_soc_card *card = dev_get_drvdata(dev);
928 bool bus_control = false;
929 struct snd_soc_pcm_runtime *rtd;
931 /* If the card is not initialized yet there is nothing to do */
932 if (!card->instantiated)
935 /* activate pins from sleep state */
936 list_for_each_entry(rtd, &card->rtd_list, list) {
937 struct snd_soc_dai **codec_dais = rtd->codec_dais;
938 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
942 pinctrl_pm_select_default_state(cpu_dai->dev);
944 for (j = 0; j < rtd->num_codecs; j++) {
945 struct snd_soc_dai *codec_dai = codec_dais[j];
946 if (codec_dai->active)
947 pinctrl_pm_select_default_state(codec_dai->dev);
952 * DAIs that also act as the control bus master might have other drivers
953 * hanging off them so need to resume immediately. Other drivers don't
954 * have that problem and may take a substantial amount of time to resume
955 * due to I/O costs and anti-pop so handle them out of line.
957 list_for_each_entry(rtd, &card->rtd_list, list) {
958 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
959 bus_control |= cpu_dai->driver->bus_control;
962 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
963 soc_resume_deferred(&card->deferred_resume_work);
965 dev_dbg(dev, "ASoC: Scheduling resume work\n");
966 if (!schedule_work(&card->deferred_resume_work))
967 dev_err(dev, "ASoC: resume work item may be lost\n");
972 EXPORT_SYMBOL_GPL(snd_soc_resume);
974 #define snd_soc_suspend NULL
975 #define snd_soc_resume NULL
978 static const struct snd_soc_dai_ops null_dai_ops = {
981 static struct snd_soc_component *soc_find_component(
982 const struct device_node *of_node, const char *name)
984 struct snd_soc_component *component;
986 lockdep_assert_held(&client_mutex);
988 list_for_each_entry(component, &component_list, list) {
990 if (component->dev->of_node == of_node)
992 } else if (strcmp(component->name, name) == 0) {
1001 * snd_soc_find_dai - Find a registered DAI
1003 * @dlc: name of the DAI or the DAI driver and optional component info to match
1005 * This function will search all registered components and their DAIs to
1006 * find the DAI of the same name. The component's of_node and name
1007 * should also match if being specified.
1009 * Return: pointer of DAI, or NULL if not found.
1011 struct snd_soc_dai *snd_soc_find_dai(
1012 const struct snd_soc_dai_link_component *dlc)
1014 struct snd_soc_component *component;
1015 struct snd_soc_dai *dai;
1016 struct device_node *component_of_node;
1018 lockdep_assert_held(&client_mutex);
1020 /* Find CPU DAI from registered DAIs*/
1021 list_for_each_entry(component, &component_list, list) {
1022 component_of_node = component->dev->of_node;
1023 if (!component_of_node && component->dev->parent)
1024 component_of_node = component->dev->parent->of_node;
1026 if (dlc->of_node && component_of_node != dlc->of_node)
1028 if (dlc->name && strcmp(component->name, dlc->name))
1030 list_for_each_entry(dai, &component->dai_list, list) {
1031 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
1032 && (!dai->driver->name
1033 || strcmp(dai->driver->name, dlc->dai_name)))
1042 EXPORT_SYMBOL_GPL(snd_soc_find_dai);
1046 * snd_soc_find_dai_link - Find a DAI link
1049 * @id: DAI link ID to match
1050 * @name: DAI link name to match, optional
1051 * @stream_name: DAI link stream name to match, optional
1053 * This function will search all existing DAI links of the soc card to
1054 * find the link of the same ID. Since DAI links may not have their
1055 * unique ID, so name and stream name should also match if being
1058 * Return: pointer of DAI link, or NULL if not found.
1060 struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
1061 int id, const char *name,
1062 const char *stream_name)
1064 struct snd_soc_dai_link *link, *_link;
1066 lockdep_assert_held(&client_mutex);
1068 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1072 if (name && (!link->name || strcmp(name, link->name)))
1075 if (stream_name && (!link->stream_name
1076 || strcmp(stream_name, link->stream_name)))
1084 EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
1086 static bool soc_is_dai_link_bound(struct snd_soc_card *card,
1087 struct snd_soc_dai_link *dai_link)
1089 struct snd_soc_pcm_runtime *rtd;
1091 list_for_each_entry(rtd, &card->rtd_list, list) {
1092 if (rtd->dai_link == dai_link)
1099 static int soc_bind_dai_link(struct snd_soc_card *card,
1100 struct snd_soc_dai_link *dai_link)
1102 struct snd_soc_pcm_runtime *rtd;
1103 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
1104 struct snd_soc_dai_link_component cpu_dai_component;
1105 struct snd_soc_component *component;
1106 struct snd_soc_dai **codec_dais;
1107 struct snd_soc_platform *platform;
1108 struct device_node *platform_of_node;
1109 const char *platform_name;
1112 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
1114 if (soc_is_dai_link_bound(card, dai_link)) {
1115 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
1120 rtd = soc_new_pcm_runtime(card, dai_link);
1124 cpu_dai_component.name = dai_link->cpu_name;
1125 cpu_dai_component.of_node = dai_link->cpu_of_node;
1126 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
1127 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
1128 if (!rtd->cpu_dai) {
1129 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
1130 dai_link->cpu_dai_name);
1133 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
1135 rtd->num_codecs = dai_link->num_codecs;
1137 /* Find CODEC from registered CODECs */
1138 codec_dais = rtd->codec_dais;
1139 for (i = 0; i < rtd->num_codecs; i++) {
1140 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
1141 if (!codec_dais[i]) {
1142 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
1143 codecs[i].dai_name);
1146 snd_soc_rtdcom_add(rtd, codec_dais[i]->component);
1149 /* Single codec links expect codec and codec_dai in runtime data */
1150 rtd->codec_dai = codec_dais[0];
1151 rtd->codec = rtd->codec_dai->codec;
1153 /* if there's no platform we match on the empty platform */
1154 platform_name = dai_link->platform_name;
1155 if (!platform_name && !dai_link->platform_of_node)
1156 platform_name = "snd-soc-dummy";
1158 /* find one from the set of registered platforms */
1159 list_for_each_entry(component, &component_list, list) {
1160 platform_of_node = component->dev->of_node;
1161 if (!platform_of_node && component->dev->parent->of_node)
1162 platform_of_node = component->dev->parent->of_node;
1164 if (dai_link->platform_of_node) {
1165 if (platform_of_node != dai_link->platform_of_node)
1168 if (strcmp(component->name, platform_name))
1172 snd_soc_rtdcom_add(rtd, component);
1175 /* find one from the set of registered platforms */
1176 list_for_each_entry(platform, &platform_list, list) {
1177 platform_of_node = platform->dev->of_node;
1178 if (!platform_of_node && platform->dev->parent->of_node)
1179 platform_of_node = platform->dev->parent->of_node;
1181 if (dai_link->platform_of_node) {
1182 if (platform_of_node != dai_link->platform_of_node)
1185 if (strcmp(platform->component.name, platform_name))
1189 rtd->platform = platform;
1191 if (!rtd->platform) {
1192 dev_err(card->dev, "ASoC: platform %s not registered\n",
1193 dai_link->platform_name);
1197 soc_add_pcm_runtime(card, rtd);
1201 soc_free_pcm_runtime(rtd);
1202 return -EPROBE_DEFER;
1205 static void soc_remove_component(struct snd_soc_component *component)
1207 if (!component->card)
1210 list_del(&component->card_list);
1212 if (component->remove)
1213 component->remove(component);
1215 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1217 soc_cleanup_component_debugfs(component);
1218 component->card = NULL;
1219 module_put(component->dev->driver->owner);
1222 static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1226 if (dai && dai->probed &&
1227 dai->driver->remove_order == order) {
1228 if (dai->driver->remove) {
1229 err = dai->driver->remove(dai);
1232 "ASoC: failed to remove %s: %d\n",
1239 static void soc_remove_link_dais(struct snd_soc_card *card,
1240 struct snd_soc_pcm_runtime *rtd, int order)
1244 /* unregister the rtd device */
1245 if (rtd->dev_registered) {
1246 device_unregister(rtd->dev);
1247 rtd->dev_registered = 0;
1250 /* remove the CODEC DAI */
1251 for (i = 0; i < rtd->num_codecs; i++)
1252 soc_remove_dai(rtd->codec_dais[i], order);
1254 soc_remove_dai(rtd->cpu_dai, order);
1257 static void soc_remove_link_components(struct snd_soc_card *card,
1258 struct snd_soc_pcm_runtime *rtd, int order)
1260 struct snd_soc_component *component;
1261 struct snd_soc_rtdcom_list *rtdcom;
1263 for_each_rtdcom(rtd, rtdcom) {
1264 component = rtdcom->component;
1266 if (component->driver->remove_order == order)
1267 soc_remove_component(component);
1271 static void soc_remove_dai_links(struct snd_soc_card *card)
1274 struct snd_soc_pcm_runtime *rtd;
1275 struct snd_soc_dai_link *link, *_link;
1277 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1279 list_for_each_entry(rtd, &card->rtd_list, list)
1280 soc_remove_link_dais(card, rtd, order);
1283 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1285 list_for_each_entry(rtd, &card->rtd_list, list)
1286 soc_remove_link_components(card, rtd, order);
1289 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1290 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1291 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1294 list_del(&link->list);
1295 card->num_dai_links--;
1299 static int snd_soc_init_multicodec(struct snd_soc_card *card,
1300 struct snd_soc_dai_link *dai_link)
1302 /* Legacy codec/codec_dai link is a single entry in multicodec */
1303 if (dai_link->codec_name || dai_link->codec_of_node ||
1304 dai_link->codec_dai_name) {
1305 dai_link->num_codecs = 1;
1307 dai_link->codecs = devm_kzalloc(card->dev,
1308 sizeof(struct snd_soc_dai_link_component),
1310 if (!dai_link->codecs)
1313 dai_link->codecs[0].name = dai_link->codec_name;
1314 dai_link->codecs[0].of_node = dai_link->codec_of_node;
1315 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1318 if (!dai_link->codecs) {
1319 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1326 static int soc_init_dai_link(struct snd_soc_card *card,
1327 struct snd_soc_dai_link *link)
1331 ret = snd_soc_init_multicodec(card, link);
1333 dev_err(card->dev, "ASoC: failed to init multicodec\n");
1337 for (i = 0; i < link->num_codecs; i++) {
1339 * Codec must be specified by 1 of name or OF node,
1340 * not both or neither.
1342 if (!!link->codecs[i].name ==
1343 !!link->codecs[i].of_node) {
1344 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1348 /* Codec DAI name must be specified */
1349 if (!link->codecs[i].dai_name) {
1350 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1357 * Platform may be specified by either name or OF node, but
1358 * can be left unspecified, and a dummy platform will be used.
1360 if (link->platform_name && link->platform_of_node) {
1362 "ASoC: Both platform name/of_node are set for %s\n",
1368 * CPU device may be specified by either name or OF node, but
1369 * can be left unspecified, and will be matched based on DAI
1372 if (link->cpu_name && link->cpu_of_node) {
1374 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1379 * At least one of CPU DAI name or CPU device name/node must be
1382 if (!link->cpu_dai_name &&
1383 !(link->cpu_name || link->cpu_of_node)) {
1385 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1394 * snd_soc_add_dai_link - Add a DAI link dynamically
1395 * @card: The ASoC card to which the DAI link is added
1396 * @dai_link: The new DAI link to add
1398 * This function adds a DAI link to the ASoC card's link list.
1400 * Note: Topology can use this API to add DAI links when probing the
1401 * topology component. And machine drivers can still define static
1402 * DAI links in dai_link array.
1404 int snd_soc_add_dai_link(struct snd_soc_card *card,
1405 struct snd_soc_dai_link *dai_link)
1407 if (dai_link->dobj.type
1408 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1409 dev_err(card->dev, "Invalid dai link type %d\n",
1410 dai_link->dobj.type);
1414 lockdep_assert_held(&client_mutex);
1415 /* Notify the machine driver for extra initialization
1416 * on the link created by topology.
1418 if (dai_link->dobj.type && card->add_dai_link)
1419 card->add_dai_link(card, dai_link);
1421 list_add_tail(&dai_link->list, &card->dai_link_list);
1422 card->num_dai_links++;
1426 EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1429 * snd_soc_remove_dai_link - Remove a DAI link from the list
1430 * @card: The ASoC card that owns the link
1431 * @dai_link: The DAI link to remove
1433 * This function removes a DAI link from the ASoC card's link list.
1435 * For DAI links previously added by topology, topology should
1436 * remove them by using the dobj embedded in the link.
1438 void snd_soc_remove_dai_link(struct snd_soc_card *card,
1439 struct snd_soc_dai_link *dai_link)
1441 struct snd_soc_dai_link *link, *_link;
1443 if (dai_link->dobj.type
1444 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1445 dev_err(card->dev, "Invalid dai link type %d\n",
1446 dai_link->dobj.type);
1450 lockdep_assert_held(&client_mutex);
1451 /* Notify the machine driver for extra destruction
1452 * on the link created by topology.
1454 if (dai_link->dobj.type && card->remove_dai_link)
1455 card->remove_dai_link(card, dai_link);
1457 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1458 if (link == dai_link) {
1459 list_del(&link->list);
1460 card->num_dai_links--;
1465 EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1467 static void soc_set_name_prefix(struct snd_soc_card *card,
1468 struct snd_soc_component *component)
1472 if (card->codec_conf == NULL)
1475 for (i = 0; i < card->num_configs; i++) {
1476 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1477 if (map->of_node && component->dev->of_node != map->of_node)
1479 if (map->dev_name && strcmp(component->name, map->dev_name))
1481 component->name_prefix = map->name_prefix;
1486 static int soc_probe_component(struct snd_soc_card *card,
1487 struct snd_soc_component *component)
1489 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1490 struct snd_soc_dai *dai;
1493 if (!strcmp(component->name, "snd-soc-dummy"))
1496 if (component->card) {
1497 if (component->card != card) {
1498 dev_err(component->dev,
1499 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1500 card->name, component->card->name);
1506 if (!try_module_get(component->dev->driver->owner))
1509 component->card = card;
1511 soc_set_name_prefix(card, component);
1513 soc_init_component_debugfs(component);
1515 if (component->driver->dapm_widgets) {
1516 ret = snd_soc_dapm_new_controls(dapm,
1517 component->driver->dapm_widgets,
1518 component->driver->num_dapm_widgets);
1521 dev_err(component->dev,
1522 "Failed to create new controls %d\n", ret);
1527 list_for_each_entry(dai, &component->dai_list, list) {
1528 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1530 dev_err(component->dev,
1531 "Failed to create DAI widgets %d\n", ret);
1536 if (component->probe) {
1537 ret = component->probe(component);
1539 dev_err(component->dev,
1540 "ASoC: failed to probe component %d\n", ret);
1544 WARN(dapm->idle_bias_off &&
1545 dapm->bias_level != SND_SOC_BIAS_OFF,
1546 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1550 /* machine specific init */
1551 if (component->init) {
1552 ret = component->init(component);
1554 dev_err(component->dev,
1555 "Failed to do machine specific init %d\n", ret);
1560 if (component->driver->controls)
1561 snd_soc_add_component_controls(component,
1562 component->driver->controls,
1563 component->driver->num_controls);
1564 if (component->driver->dapm_routes)
1565 snd_soc_dapm_add_routes(dapm,
1566 component->driver->dapm_routes,
1567 component->driver->num_dapm_routes);
1569 list_add(&dapm->list, &card->dapm_list);
1570 list_add(&component->card_list, &card->component_dev_list);
1575 soc_cleanup_component_debugfs(component);
1576 component->card = NULL;
1577 module_put(component->dev->driver->owner);
1582 static void rtd_release(struct device *dev)
1587 static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1592 /* register the rtd device */
1593 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1596 device_initialize(rtd->dev);
1597 rtd->dev->parent = rtd->card->dev;
1598 rtd->dev->release = rtd_release;
1599 rtd->dev->groups = soc_dev_attr_groups;
1600 dev_set_name(rtd->dev, "%s", name);
1601 dev_set_drvdata(rtd->dev, rtd);
1602 mutex_init(&rtd->pcm_mutex);
1603 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1604 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1605 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1606 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1607 ret = device_add(rtd->dev);
1609 /* calling put_device() here to free the rtd->dev */
1610 put_device(rtd->dev);
1611 dev_err(rtd->card->dev,
1612 "ASoC: failed to register runtime device: %d\n", ret);
1615 rtd->dev_registered = 1;
1619 static int soc_probe_link_components(struct snd_soc_card *card,
1620 struct snd_soc_pcm_runtime *rtd,
1623 struct snd_soc_component *component;
1624 struct snd_soc_rtdcom_list *rtdcom;
1627 for_each_rtdcom(rtd, rtdcom) {
1628 component = rtdcom->component;
1630 if (component->driver->probe_order == order) {
1631 ret = soc_probe_component(card, component);
1640 static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1644 if (!dai->probed && dai->driver->probe_order == order) {
1645 if (dai->driver->probe) {
1646 ret = dai->driver->probe(dai);
1649 "ASoC: failed to probe DAI %s: %d\n",
1661 static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1662 struct snd_soc_pcm_runtime *rtd)
1666 for (i = 0; i < num_dais; ++i) {
1667 struct snd_soc_dai_driver *drv = dais[i]->driver;
1669 if (!rtd->dai_link->no_pcm && drv->pcm_new)
1670 ret = drv->pcm_new(rtd, dais[i]);
1672 dev_err(dais[i]->dev,
1673 "ASoC: Failed to bind %s with pcm device\n",
1682 static int soc_link_dai_widgets(struct snd_soc_card *card,
1683 struct snd_soc_dai_link *dai_link,
1684 struct snd_soc_pcm_runtime *rtd)
1686 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1687 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1688 struct snd_soc_dapm_widget *sink, *source;
1691 if (rtd->num_codecs > 1)
1692 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1694 /* link the DAI widgets */
1695 sink = codec_dai->playback_widget;
1696 source = cpu_dai->capture_widget;
1697 if (sink && source) {
1698 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1699 dai_link->num_params,
1702 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1703 sink->name, source->name, ret);
1708 sink = cpu_dai->playback_widget;
1709 source = codec_dai->capture_widget;
1710 if (sink && source) {
1711 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1712 dai_link->num_params,
1715 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1716 sink->name, source->name, ret);
1724 static int soc_probe_link_dais(struct snd_soc_card *card,
1725 struct snd_soc_pcm_runtime *rtd, int order)
1727 struct snd_soc_dai_link *dai_link = rtd->dai_link;
1728 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1731 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1732 card->name, rtd->num, order);
1734 /* set default power off timeout */
1735 rtd->pmdown_time = pmdown_time;
1737 ret = soc_probe_dai(cpu_dai, order);
1741 /* probe the CODEC DAI */
1742 for (i = 0; i < rtd->num_codecs; i++) {
1743 ret = soc_probe_dai(rtd->codec_dais[i], order);
1748 /* complete DAI probe during last probe */
1749 if (order != SND_SOC_COMP_ORDER_LAST)
1752 /* do machine specific initialization */
1753 if (dai_link->init) {
1754 ret = dai_link->init(rtd);
1756 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1757 dai_link->name, ret);
1762 if (dai_link->dai_fmt)
1763 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1765 ret = soc_post_component_init(rtd, dai_link->name);
1769 #ifdef CONFIG_DEBUG_FS
1770 /* add DPCM sysfs entries */
1771 if (dai_link->dynamic)
1772 soc_dpcm_debugfs_add(rtd);
1775 if (cpu_dai->driver->compress_new) {
1776 /*create compress_device"*/
1777 ret = cpu_dai->driver->compress_new(rtd, rtd->num);
1779 dev_err(card->dev, "ASoC: can't create compress %s\n",
1780 dai_link->stream_name);
1785 if (!dai_link->params) {
1786 /* create the pcm */
1787 ret = soc_new_pcm(rtd, rtd->num);
1789 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1790 dai_link->stream_name, ret);
1793 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1796 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1797 rtd->num_codecs, rtd);
1801 INIT_DELAYED_WORK(&rtd->delayed_work,
1802 codec2codec_close_delayed_work);
1804 /* link the DAI widgets */
1805 ret = soc_link_dai_widgets(card, dai_link, rtd);
1814 static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1816 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1817 struct snd_soc_component *component;
1819 struct device_node *codec_of_node;
1821 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1822 /* codecs, usually analog devices */
1823 name = aux_dev->codec_name;
1824 codec_of_node = aux_dev->codec_of_node;
1825 component = soc_find_component(codec_of_node, name);
1828 name = of_node_full_name(codec_of_node);
1831 } else if (aux_dev->name) {
1832 /* generic components */
1833 name = aux_dev->name;
1834 component = soc_find_component(NULL, name);
1838 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1842 component->init = aux_dev->init;
1843 list_add(&component->card_aux_list, &card->aux_comp_list);
1848 dev_err(card->dev, "ASoC: %s not registered\n", name);
1849 return -EPROBE_DEFER;
1852 static int soc_probe_aux_devices(struct snd_soc_card *card)
1854 struct snd_soc_component *comp;
1858 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1860 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
1861 if (comp->driver->probe_order == order) {
1862 ret = soc_probe_component(card, comp);
1865 "ASoC: failed to probe aux component %s %d\n",
1876 static void soc_remove_aux_devices(struct snd_soc_card *card)
1878 struct snd_soc_component *comp, *_comp;
1881 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1883 list_for_each_entry_safe(comp, _comp,
1884 &card->aux_comp_list, card_aux_list) {
1886 if (comp->driver->remove_order == order) {
1887 soc_remove_component(comp);
1888 /* remove it from the card's aux_comp_list */
1889 list_del(&comp->card_aux_list);
1895 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1899 if (codec->cache_init)
1902 ret = snd_soc_cache_init(codec);
1905 "ASoC: Failed to set cache compression type: %d\n",
1909 codec->cache_init = 1;
1914 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1915 * @rtd: The runtime for which the DAI link format should be changed
1916 * @dai_fmt: The new DAI link format
1918 * This function updates the DAI link format for all DAIs connected to the DAI
1919 * link for the specified runtime.
1921 * Note: For setups with a static format set the dai_fmt field in the
1922 * corresponding snd_dai_link struct instead of using this function.
1924 * Returns 0 on success, otherwise a negative error code.
1926 int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1927 unsigned int dai_fmt)
1929 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1930 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1934 for (i = 0; i < rtd->num_codecs; i++) {
1935 struct snd_soc_dai *codec_dai = codec_dais[i];
1937 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1938 if (ret != 0 && ret != -ENOTSUPP) {
1939 dev_warn(codec_dai->dev,
1940 "ASoC: Failed to set DAI format: %d\n", ret);
1945 /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1946 if (cpu_dai->codec) {
1947 unsigned int inv_dai_fmt;
1949 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1950 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1951 case SND_SOC_DAIFMT_CBM_CFM:
1952 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1954 case SND_SOC_DAIFMT_CBM_CFS:
1955 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1957 case SND_SOC_DAIFMT_CBS_CFM:
1958 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1960 case SND_SOC_DAIFMT_CBS_CFS:
1961 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1965 dai_fmt = inv_dai_fmt;
1968 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1969 if (ret != 0 && ret != -ENOTSUPP) {
1970 dev_warn(cpu_dai->dev,
1971 "ASoC: Failed to set DAI format: %d\n", ret);
1977 EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1981 /* Trim special characters, and replace '-' with '_' since '-' is used to
1982 * separate different DMI fields in the card long name. Only number and
1983 * alphabet characters and a few separator characters are kept.
1985 static void cleanup_dmi_name(char *name)
1989 for (i = 0; name[i]; i++) {
1990 if (isalnum(name[i]) || (name[i] == '.')
1991 || (name[i] == '_'))
1992 name[j++] = name[i];
1993 else if (name[i] == '-')
2000 /* Check if a DMI field is valid, i.e. not containing any string
2001 * in the black list.
2003 static int is_dmi_valid(const char *field)
2007 while (dmi_blacklist[i]) {
2008 if (strstr(field, dmi_blacklist[i]))
2017 * snd_soc_set_dmi_name() - Register DMI names to card
2018 * @card: The card to register DMI names
2019 * @flavour: The flavour "differentiator" for the card amongst its peers.
2021 * An Intel machine driver may be used by many different devices but are
2022 * difficult for userspace to differentiate, since machine drivers ususally
2023 * use their own name as the card short name and leave the card long name
2024 * blank. To differentiate such devices and fix bugs due to lack of
2025 * device-specific configurations, this function allows DMI info to be used
2026 * as the sound card long name, in the format of
2027 * "vendor-product-version-board"
2028 * (Character '-' is used to separate different DMI fields here).
2029 * This will help the user space to load the device-specific Use Case Manager
2030 * (UCM) configurations for the card.
2032 * Possible card long names may be:
2033 * DellInc.-XPS139343-01-0310JH
2034 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
2035 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
2037 * This function also supports flavoring the card longname to provide
2038 * the extra differentiation, like "vendor-product-version-board-flavor".
2040 * We only keep number and alphabet characters and a few separator characters
2041 * in the card long name since UCM in the user space uses the card long names
2042 * as card configuration directory names and AudoConf cannot support special
2043 * charactors like SPACE.
2045 * Returns 0 on success, otherwise a negative error code.
2047 int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
2049 const char *vendor, *product, *product_version, *board;
2050 size_t longname_buf_size = sizeof(card->snd_card->longname);
2053 if (card->long_name)
2054 return 0; /* long name already set by driver or from DMI */
2056 /* make up dmi long name as: vendor.product.version.board */
2057 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2058 if (!vendor || !is_dmi_valid(vendor)) {
2059 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
2064 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
2066 cleanup_dmi_name(card->dmi_longname);
2068 product = dmi_get_system_info(DMI_PRODUCT_NAME);
2069 if (product && is_dmi_valid(product)) {
2070 len = strlen(card->dmi_longname);
2071 snprintf(card->dmi_longname + len,
2072 longname_buf_size - len,
2075 len++; /* skip the separator "-" */
2076 if (len < longname_buf_size)
2077 cleanup_dmi_name(card->dmi_longname + len);
2079 /* some vendors like Lenovo may only put a self-explanatory
2080 * name in the product version field
2082 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
2083 if (product_version && is_dmi_valid(product_version)) {
2084 len = strlen(card->dmi_longname);
2085 snprintf(card->dmi_longname + len,
2086 longname_buf_size - len,
2087 "-%s", product_version);
2090 if (len < longname_buf_size)
2091 cleanup_dmi_name(card->dmi_longname + len);
2095 board = dmi_get_system_info(DMI_BOARD_NAME);
2096 if (board && is_dmi_valid(board)) {
2097 len = strlen(card->dmi_longname);
2098 snprintf(card->dmi_longname + len,
2099 longname_buf_size - len,
2103 if (len < longname_buf_size)
2104 cleanup_dmi_name(card->dmi_longname + len);
2105 } else if (!product) {
2106 /* fall back to using legacy name */
2107 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
2111 /* Add flavour to dmi long name */
2113 len = strlen(card->dmi_longname);
2114 snprintf(card->dmi_longname + len,
2115 longname_buf_size - len,
2119 if (len < longname_buf_size)
2120 cleanup_dmi_name(card->dmi_longname + len);
2123 /* set the card long name */
2124 card->long_name = card->dmi_longname;
2128 EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
2129 #endif /* CONFIG_DMI */
2131 static int snd_soc_instantiate_card(struct snd_soc_card *card)
2133 struct snd_soc_codec *codec;
2134 struct snd_soc_pcm_runtime *rtd;
2135 struct snd_soc_dai_link *dai_link;
2138 mutex_lock(&client_mutex);
2139 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
2142 for (i = 0; i < card->num_links; i++) {
2143 ret = soc_bind_dai_link(card, &card->dai_link[i]);
2148 /* bind aux_devs too */
2149 for (i = 0; i < card->num_aux_devs; i++) {
2150 ret = soc_bind_aux_dev(card, i);
2155 /* add predefined DAI links to the list */
2156 for (i = 0; i < card->num_links; i++)
2157 snd_soc_add_dai_link(card, card->dai_link+i);
2159 /* initialize the register cache for each available codec */
2160 list_for_each_entry(codec, &codec_list, list) {
2161 if (codec->cache_init)
2163 ret = snd_soc_init_codec_cache(codec);
2168 /* card bind complete so register a sound card */
2169 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2170 card->owner, 0, &card->snd_card);
2173 "ASoC: can't create sound card for card %s: %d\n",
2178 soc_init_card_debugfs(card);
2180 card->dapm.bias_level = SND_SOC_BIAS_OFF;
2181 card->dapm.dev = card->dev;
2182 card->dapm.card = card;
2183 list_add(&card->dapm.list, &card->dapm_list);
2185 #ifdef CONFIG_DEBUG_FS
2186 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2189 #ifdef CONFIG_PM_SLEEP
2190 /* deferred resume work */
2191 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
2194 if (card->dapm_widgets)
2195 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2196 card->num_dapm_widgets);
2198 if (card->of_dapm_widgets)
2199 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2200 card->num_of_dapm_widgets);
2202 /* initialise the sound card only once */
2204 ret = card->probe(card);
2206 goto card_probe_error;
2209 /* probe all components used by DAI links on this card */
2210 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2212 list_for_each_entry(rtd, &card->rtd_list, list) {
2213 ret = soc_probe_link_components(card, rtd, order);
2216 "ASoC: failed to instantiate card %d\n",
2223 /* probe auxiliary components */
2224 ret = soc_probe_aux_devices(card);
2228 /* Find new DAI links added during probing components and bind them.
2229 * Components with topology may bring new DAIs and DAI links.
2231 list_for_each_entry(dai_link, &card->dai_link_list, list) {
2232 if (soc_is_dai_link_bound(card, dai_link))
2235 ret = soc_init_dai_link(card, dai_link);
2238 ret = soc_bind_dai_link(card, dai_link);
2243 /* probe all DAI links on this card */
2244 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2246 list_for_each_entry(rtd, &card->rtd_list, list) {
2247 ret = soc_probe_link_dais(card, rtd, order);
2250 "ASoC: failed to instantiate card %d\n",
2257 snd_soc_dapm_link_dai_widgets(card);
2258 snd_soc_dapm_connect_dai_link_widgets(card);
2261 snd_soc_add_card_controls(card, card->controls, card->num_controls);
2263 if (card->dapm_routes)
2264 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2265 card->num_dapm_routes);
2267 if (card->of_dapm_routes)
2268 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2269 card->num_of_dapm_routes);
2271 /* try to set some sane longname if DMI is available */
2272 snd_soc_set_dmi_name(card, NULL);
2274 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
2276 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2277 "%s", card->long_name ? card->long_name : card->name);
2278 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2279 "%s", card->driver_name ? card->driver_name : card->name);
2280 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2281 switch (card->snd_card->driver[i]) {
2287 if (!isalnum(card->snd_card->driver[i]))
2288 card->snd_card->driver[i] = '_';
2293 if (card->late_probe) {
2294 ret = card->late_probe(card);
2296 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
2298 goto probe_aux_dev_err;
2302 snd_soc_dapm_new_widgets(card);
2304 ret = snd_card_register(card->snd_card);
2306 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2308 goto probe_aux_dev_err;
2311 card->instantiated = 1;
2312 snd_soc_dapm_sync(&card->dapm);
2313 mutex_unlock(&card->mutex);
2314 mutex_unlock(&client_mutex);
2319 soc_remove_aux_devices(card);
2322 soc_remove_dai_links(card);
2328 snd_soc_dapm_free(&card->dapm);
2329 soc_cleanup_card_debugfs(card);
2330 snd_card_free(card->snd_card);
2333 soc_remove_pcm_runtimes(card);
2334 mutex_unlock(&card->mutex);
2335 mutex_unlock(&client_mutex);
2340 /* probes a new socdev */
2341 static int soc_probe(struct platform_device *pdev)
2343 struct snd_soc_card *card = platform_get_drvdata(pdev);
2346 * no card, so machine driver should be registering card
2347 * we should not be here in that case so ret error
2352 dev_warn(&pdev->dev,
2353 "ASoC: machine %s should use snd_soc_register_card()\n",
2356 /* Bodge while we unpick instantiation */
2357 card->dev = &pdev->dev;
2359 return snd_soc_register_card(card);
2362 static int soc_cleanup_card_resources(struct snd_soc_card *card)
2364 struct snd_soc_pcm_runtime *rtd;
2366 /* make sure any delayed work runs */
2367 list_for_each_entry(rtd, &card->rtd_list, list)
2368 flush_delayed_work(&rtd->delayed_work);
2370 /* free the ALSA card at first; this syncs with pending operations */
2371 snd_card_free(card->snd_card);
2373 /* remove and free each DAI */
2374 soc_remove_dai_links(card);
2375 soc_remove_pcm_runtimes(card);
2377 /* remove auxiliary devices */
2378 soc_remove_aux_devices(card);
2380 snd_soc_dapm_free(&card->dapm);
2381 soc_cleanup_card_debugfs(card);
2383 /* remove the card */
2390 /* removes a socdev */
2391 static int soc_remove(struct platform_device *pdev)
2393 struct snd_soc_card *card = platform_get_drvdata(pdev);
2395 snd_soc_unregister_card(card);
2399 int snd_soc_poweroff(struct device *dev)
2401 struct snd_soc_card *card = dev_get_drvdata(dev);
2402 struct snd_soc_pcm_runtime *rtd;
2404 if (!card->instantiated)
2407 /* Flush out pmdown_time work - we actually do want to run it
2408 * now, we're shutting down so no imminent restart. */
2409 list_for_each_entry(rtd, &card->rtd_list, list)
2410 flush_delayed_work(&rtd->delayed_work);
2412 snd_soc_dapm_shutdown(card);
2414 /* deactivate pins to sleep state */
2415 list_for_each_entry(rtd, &card->rtd_list, list) {
2416 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2419 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2420 for (i = 0; i < rtd->num_codecs; i++) {
2421 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
2422 pinctrl_pm_select_sleep_state(codec_dai->dev);
2428 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2430 const struct dev_pm_ops snd_soc_pm_ops = {
2431 .suspend = snd_soc_suspend,
2432 .resume = snd_soc_resume,
2433 .freeze = snd_soc_suspend,
2434 .thaw = snd_soc_resume,
2435 .poweroff = snd_soc_poweroff,
2436 .restore = snd_soc_resume,
2438 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2440 /* ASoC platform driver */
2441 static struct platform_driver soc_driver = {
2443 .name = "soc-audio",
2444 .pm = &snd_soc_pm_ops,
2447 .remove = soc_remove,
2451 * snd_soc_cnew - create new control
2452 * @_template: control template
2453 * @data: control private data
2454 * @long_name: control long name
2455 * @prefix: control name prefix
2457 * Create a new mixer control from a template control.
2459 * Returns 0 for success, else error.
2461 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2462 void *data, const char *long_name,
2465 struct snd_kcontrol_new template;
2466 struct snd_kcontrol *kcontrol;
2469 memcpy(&template, _template, sizeof(template));
2473 long_name = template.name;
2476 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2480 template.name = name;
2482 template.name = long_name;
2485 kcontrol = snd_ctl_new1(&template, data);
2491 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2493 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2494 const struct snd_kcontrol_new *controls, int num_controls,
2495 const char *prefix, void *data)
2499 for (i = 0; i < num_controls; i++) {
2500 const struct snd_kcontrol_new *control = &controls[i];
2501 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2502 control->name, prefix));
2504 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2505 control->name, err);
2513 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2516 struct snd_card *card = soc_card->snd_card;
2517 struct snd_kcontrol *kctl;
2519 if (unlikely(!name))
2522 list_for_each_entry(kctl, &card->controls, list)
2523 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2527 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2530 * snd_soc_add_component_controls - Add an array of controls to a component.
2532 * @component: Component to add controls to
2533 * @controls: Array of controls to add
2534 * @num_controls: Number of elements in the array
2536 * Return: 0 for success, else error.
2538 int snd_soc_add_component_controls(struct snd_soc_component *component,
2539 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2541 struct snd_card *card = component->card->snd_card;
2543 return snd_soc_add_controls(card, component->dev, controls,
2544 num_controls, component->name_prefix, component);
2546 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2549 * snd_soc_add_codec_controls - add an array of controls to a codec.
2550 * Convenience function to add a list of controls. Many codecs were
2551 * duplicating this code.
2553 * @codec: codec to add controls to
2554 * @controls: array of controls to add
2555 * @num_controls: number of elements in the array
2557 * Return 0 for success, else error.
2559 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2560 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2562 return snd_soc_add_component_controls(&codec->component, controls,
2565 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2568 * snd_soc_add_platform_controls - add an array of controls to a platform.
2569 * Convenience function to add a list of controls.
2571 * @platform: platform to add controls to
2572 * @controls: array of controls to add
2573 * @num_controls: number of elements in the array
2575 * Return 0 for success, else error.
2577 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2578 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2580 return snd_soc_add_component_controls(&platform->component, controls,
2583 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2586 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2587 * Convenience function to add a list of controls.
2589 * @soc_card: SoC card to add controls to
2590 * @controls: array of controls to add
2591 * @num_controls: number of elements in the array
2593 * Return 0 for success, else error.
2595 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2596 const struct snd_kcontrol_new *controls, int num_controls)
2598 struct snd_card *card = soc_card->snd_card;
2600 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2603 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2606 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2607 * Convienience function to add a list of controls.
2609 * @dai: DAI to add controls to
2610 * @controls: array of controls to add
2611 * @num_controls: number of elements in the array
2613 * Return 0 for success, else error.
2615 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2616 const struct snd_kcontrol_new *controls, int num_controls)
2618 struct snd_card *card = dai->component->card->snd_card;
2620 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2623 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2626 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2628 * @clk_id: DAI specific clock ID
2629 * @freq: new clock frequency in Hz
2630 * @dir: new clock direction - input/output.
2632 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2634 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2635 unsigned int freq, int dir)
2637 if (dai->driver && dai->driver->ops->set_sysclk)
2638 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2640 return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2643 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2646 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
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.
2653 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2655 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2656 int source, unsigned int freq, int dir)
2658 if (codec->driver->set_sysclk)
2659 return codec->driver->set_sysclk(codec, clk_id, source,
2664 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2667 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2668 * @component: COMPONENT
2669 * @clk_id: DAI specific clock ID
2670 * @source: Source for the clock
2671 * @freq: new clock frequency in Hz
2672 * @dir: new clock direction - input/output.
2674 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2676 int snd_soc_component_set_sysclk(struct snd_soc_component *component, int clk_id,
2677 int source, unsigned int freq, int dir)
2679 /* will be removed */
2680 if (component->set_sysclk)
2681 return component->set_sysclk(component, clk_id, source,
2684 if (component->driver->set_sysclk)
2685 return component->driver->set_sysclk(component, clk_id, source,
2690 EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2693 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2695 * @div_id: DAI specific clock divider ID
2696 * @div: new clock divisor.
2698 * Configures the clock dividers. This is used to derive the best DAI bit and
2699 * frame clocks from the system or master clock. It's best to set the DAI bit
2700 * and frame clocks as low as possible to save system power.
2702 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2703 int div_id, int div)
2705 if (dai->driver && dai->driver->ops->set_clkdiv)
2706 return dai->driver->ops->set_clkdiv(dai, div_id, div);
2710 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2713 * snd_soc_dai_set_pll - configure DAI PLL.
2715 * @pll_id: DAI specific PLL ID
2716 * @source: DAI specific source for the PLL
2717 * @freq_in: PLL input clock frequency in Hz
2718 * @freq_out: requested PLL output clock frequency in Hz
2720 * Configures and enables PLL to generate output clock based on input clock.
2722 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2723 unsigned int freq_in, unsigned int freq_out)
2725 if (dai->driver && dai->driver->ops->set_pll)
2726 return dai->driver->ops->set_pll(dai, pll_id, source,
2729 return snd_soc_component_set_pll(dai->component, pll_id, source,
2732 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2735 * snd_soc_codec_set_pll - configure codec PLL.
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
2742 * Configures and enables PLL to generate output clock based on input clock.
2744 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2745 unsigned int freq_in, unsigned int freq_out)
2747 if (codec->driver->set_pll)
2748 return codec->driver->set_pll(codec, pll_id, source,
2753 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2756 * snd_soc_component_set_pll - configure component PLL.
2757 * @component: COMPONENT
2758 * @pll_id: DAI specific PLL ID
2759 * @source: DAI specific source for the PLL
2760 * @freq_in: PLL input clock frequency in Hz
2761 * @freq_out: requested PLL output clock frequency in Hz
2763 * Configures and enables PLL to generate output clock based on input clock.
2765 int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2766 int source, unsigned int freq_in,
2767 unsigned int freq_out)
2769 /* will be removed */
2770 if (component->set_pll)
2771 return component->set_pll(component, pll_id, source,
2774 if (component->driver->set_pll)
2775 return component->driver->set_pll(component, pll_id, source,
2780 EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2783 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2785 * @ratio: Ratio of BCLK to Sample rate.
2787 * Configures the DAI for a preset BCLK to sample rate ratio.
2789 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2791 if (dai->driver && dai->driver->ops->set_bclk_ratio)
2792 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2796 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2799 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2801 * @fmt: SND_SOC_DAIFMT_ format value.
2803 * Configures the DAI hardware format and clocking.
2805 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2807 if (dai->driver == NULL)
2809 if (dai->driver->ops->set_fmt == NULL)
2811 return dai->driver->ops->set_fmt(dai, fmt);
2813 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2816 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
2817 * @slots: Number of slots in use.
2818 * @tx_mask: bitmask representing active TX slots.
2819 * @rx_mask: bitmask representing active RX slots.
2821 * Generates the TDM tx and rx slot default masks for DAI.
2823 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
2824 unsigned int *tx_mask,
2825 unsigned int *rx_mask)
2827 if (*tx_mask || *rx_mask)
2833 *tx_mask = (1 << slots) - 1;
2834 *rx_mask = (1 << slots) - 1;
2840 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2841 * @dai: The DAI to configure
2842 * @tx_mask: bitmask representing active TX slots.
2843 * @rx_mask: bitmask representing active RX slots.
2844 * @slots: Number of slots in use.
2845 * @slot_width: Width in bits for each slot.
2847 * This function configures the specified DAI for TDM operation. @slot contains
2848 * the total number of slots of the TDM stream and @slot_with the width of each
2849 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2850 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2851 * DAI should write to or read from. If a bit is set the corresponding slot is
2852 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2853 * the first slot, bit 1 to the second slot and so on. The first active slot
2854 * maps to the first channel of the DAI, the second active slot to the second
2855 * channel and so on.
2857 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2858 * @rx_mask and @slot_width will be ignored.
2860 * Returns 0 on success, a negative error code otherwise.
2862 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2863 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2865 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2866 dai->driver->ops->xlate_tdm_slot_mask(slots,
2867 &tx_mask, &rx_mask);
2869 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
2871 dai->tx_mask = tx_mask;
2872 dai->rx_mask = rx_mask;
2874 if (dai->driver && dai->driver->ops->set_tdm_slot)
2875 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2880 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2883 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2885 * @tx_num: how many TX channels
2886 * @tx_slot: pointer to an array which imply the TX slot number channel
2888 * @rx_num: how many RX channels
2889 * @rx_slot: pointer to an array which imply the RX slot number channel
2892 * configure the relationship between channel number and TDM slot number.
2894 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2895 unsigned int tx_num, unsigned int *tx_slot,
2896 unsigned int rx_num, unsigned int *rx_slot)
2898 if (dai->driver && dai->driver->ops->set_channel_map)
2899 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2904 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2907 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2909 * @tristate: tristate enable
2911 * Tristates the DAI so that others can use it.
2913 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2915 if (dai->driver && dai->driver->ops->set_tristate)
2916 return dai->driver->ops->set_tristate(dai, tristate);
2920 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2923 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2925 * @mute: mute enable
2926 * @direction: stream to mute
2928 * Mutes the DAI DAC.
2930 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2936 if (dai->driver->ops->mute_stream)
2937 return dai->driver->ops->mute_stream(dai, mute, direction);
2938 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2939 dai->driver->ops->digital_mute)
2940 return dai->driver->ops->digital_mute(dai, mute);
2944 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2947 * snd_soc_register_card - Register a card with the ASoC core
2949 * @card: Card to register
2952 int snd_soc_register_card(struct snd_soc_card *card)
2955 struct snd_soc_pcm_runtime *rtd;
2957 if (!card->name || !card->dev)
2960 for (i = 0; i < card->num_links; i++) {
2961 struct snd_soc_dai_link *link = &card->dai_link[i];
2963 ret = soc_init_dai_link(card, link);
2965 dev_err(card->dev, "ASoC: failed to init link %s\n",
2971 dev_set_drvdata(card->dev, card);
2973 snd_soc_initialize_card_lists(card);
2975 INIT_LIST_HEAD(&card->dai_link_list);
2976 card->num_dai_links = 0;
2978 INIT_LIST_HEAD(&card->rtd_list);
2981 INIT_LIST_HEAD(&card->dapm_dirty);
2982 INIT_LIST_HEAD(&card->dobj_list);
2983 card->instantiated = 0;
2984 mutex_init(&card->mutex);
2985 mutex_init(&card->dapm_mutex);
2987 ret = snd_soc_instantiate_card(card);
2991 /* deactivate pins to sleep state */
2992 list_for_each_entry(rtd, &card->rtd_list, list) {
2993 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2996 for (j = 0; j < rtd->num_codecs; j++) {
2997 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2998 if (!codec_dai->active)
2999 pinctrl_pm_select_sleep_state(codec_dai->dev);
3002 if (!cpu_dai->active)
3003 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3008 EXPORT_SYMBOL_GPL(snd_soc_register_card);
3011 * snd_soc_unregister_card - Unregister a card with the ASoC core
3013 * @card: Card to unregister
3016 int snd_soc_unregister_card(struct snd_soc_card *card)
3018 if (card->instantiated) {
3019 card->instantiated = false;
3020 snd_soc_dapm_shutdown(card);
3021 soc_cleanup_card_resources(card);
3022 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
3027 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3030 * Simplify DAI link configuration by removing ".-1" from device names
3031 * and sanitizing names.
3033 static char *fmt_single_name(struct device *dev, int *id)
3035 char *found, name[NAME_SIZE];
3038 if (dev_name(dev) == NULL)
3041 strlcpy(name, dev_name(dev), NAME_SIZE);
3043 /* are we a "%s.%d" name (platform and SPI components) */
3044 found = strstr(name, dev->driver->name);
3047 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3049 /* discard ID from name if ID == -1 */
3051 found[strlen(dev->driver->name)] = '\0';
3055 /* I2C component devices are named "bus-addr" */
3056 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3057 char tmp[NAME_SIZE];
3059 /* create unique ID number from I2C addr and bus */
3060 *id = ((id1 & 0xffff) << 16) + id2;
3062 /* sanitize component name for DAI link creation */
3063 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3064 strlcpy(name, tmp, NAME_SIZE);
3069 return kstrdup(name, GFP_KERNEL);
3073 * Simplify DAI link naming for single devices with multiple DAIs by removing
3074 * any ".-1" and using the DAI name (instead of device name).
3076 static inline char *fmt_multiple_name(struct device *dev,
3077 struct snd_soc_dai_driver *dai_drv)
3079 if (dai_drv->name == NULL) {
3081 "ASoC: error - multiple DAI %s registered with no name\n",
3086 return kstrdup(dai_drv->name, GFP_KERNEL);
3090 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
3092 * @component: The component for which the DAIs should be unregistered
3094 static void snd_soc_unregister_dais(struct snd_soc_component *component)
3096 struct snd_soc_dai *dai, *_dai;
3098 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
3099 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3101 list_del(&dai->list);
3107 /* Create a DAI and add it to the component's DAI list */
3108 static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
3109 struct snd_soc_dai_driver *dai_drv,
3110 bool legacy_dai_naming)
3112 struct device *dev = component->dev;
3113 struct snd_soc_dai *dai;
3115 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
3117 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3122 * Back in the old days when we still had component-less DAIs,
3123 * instead of having a static name, component-less DAIs would
3124 * inherit the name of the parent device so it is possible to
3125 * register multiple instances of the DAI. We still need to keep
3126 * the same naming style even though those DAIs are not
3127 * component-less anymore.
3129 if (legacy_dai_naming &&
3130 (dai_drv->id == 0 || dai_drv->name == NULL)) {
3131 dai->name = fmt_single_name(dev, &dai->id);
3133 dai->name = fmt_multiple_name(dev, dai_drv);
3135 dai->id = dai_drv->id;
3137 dai->id = component->num_dai;
3139 if (dai->name == NULL) {
3144 dai->component = component;
3146 dai->driver = dai_drv;
3147 if (!dai->driver->ops)
3148 dai->driver->ops = &null_dai_ops;
3150 list_add(&dai->list, &component->dai_list);
3151 component->num_dai++;
3153 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3158 * snd_soc_register_dais - Register a DAI with the ASoC core
3160 * @component: The component the DAIs are registered for
3161 * @dai_drv: DAI driver to use for the DAIs
3162 * @count: Number of DAIs
3163 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3166 static int snd_soc_register_dais(struct snd_soc_component *component,
3167 struct snd_soc_dai_driver *dai_drv, size_t count,
3168 bool legacy_dai_naming)
3170 struct device *dev = component->dev;
3171 struct snd_soc_dai *dai;
3175 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
3177 component->dai_drv = dai_drv;
3179 for (i = 0; i < count; i++) {
3181 dai = soc_add_dai(component, dai_drv + i,
3182 count == 1 && legacy_dai_naming);
3192 snd_soc_unregister_dais(component);
3198 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
3200 * @component: The component the DAIs are registered for
3201 * @dai_drv: DAI driver to use for the DAI
3203 * Topology can use this API to register DAIs when probing a component.
3204 * These DAIs's widgets will be freed in the card cleanup and the DAIs
3205 * will be freed in the component cleanup.
3207 int snd_soc_register_dai(struct snd_soc_component *component,
3208 struct snd_soc_dai_driver *dai_drv)
3210 struct snd_soc_dapm_context *dapm =
3211 snd_soc_component_get_dapm(component);
3212 struct snd_soc_dai *dai;
3215 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
3216 dev_err(component->dev, "Invalid dai type %d\n",
3217 dai_drv->dobj.type);
3221 lockdep_assert_held(&client_mutex);
3222 dai = soc_add_dai(component, dai_drv, false);
3226 /* Create the DAI widgets here. After adding DAIs, topology may
3227 * also add routes that need these widgets as source or sink.
3229 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
3231 dev_err(component->dev,
3232 "Failed to create DAI widgets %d\n", ret);
3237 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3239 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3240 enum snd_soc_dapm_type type, int subseq)
3242 struct snd_soc_component *component = dapm->component;
3244 component->driver->seq_notifier(component, type, subseq);
3247 static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3250 struct snd_soc_component *component = dapm->component;
3252 return component->driver->stream_event(component, event);
3255 static int snd_soc_component_initialize(struct snd_soc_component *component,
3256 const struct snd_soc_component_driver *driver, struct device *dev)
3258 struct snd_soc_dapm_context *dapm;
3260 component->name = fmt_single_name(dev, &component->id);
3261 if (!component->name) {
3262 dev_err(dev, "ASoC: Failed to allocate name\n");
3266 component->dev = dev;
3267 component->driver = driver;
3268 component->probe = component->driver->probe;
3269 component->remove = component->driver->remove;
3270 component->suspend = component->driver->suspend;
3271 component->resume = component->driver->resume;
3272 component->set_sysclk = component->driver->set_sysclk;
3273 component->set_pll = component->driver->set_pll;
3274 component->set_jack = component->driver->set_jack;
3276 dapm = snd_soc_component_get_dapm(component);
3278 dapm->component = component;
3279 dapm->bias_level = SND_SOC_BIAS_OFF;
3280 dapm->idle_bias_off = true;
3281 if (driver->seq_notifier)
3282 dapm->seq_notifier = snd_soc_component_seq_notifier;
3283 if (driver->stream_event)
3284 dapm->stream_event = snd_soc_component_stream_event;
3286 INIT_LIST_HEAD(&component->dai_list);
3287 mutex_init(&component->io_mutex);
3292 static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
3294 int val_bytes = regmap_get_val_bytes(component->regmap);
3296 /* Errors are legitimate for non-integer byte multiples */
3298 component->val_bytes = val_bytes;
3301 #ifdef CONFIG_REGMAP
3304 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
3305 * @component: The component for which to initialize the regmap instance
3306 * @regmap: The regmap instance that should be used by the component
3308 * This function allows deferred assignment of the regmap