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 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))
1040 EXPORT_SYMBOL_GPL(snd_soc_find_dai);
1044 * snd_soc_find_dai_link - Find a DAI link
1047 * @id: DAI link ID to match
1048 * @name: DAI link name to match, optional
1049 * @stream_name: DAI link stream name to match, optional
1051 * This function will search all existing DAI links of the soc card to
1052 * find the link of the same ID. Since DAI links may not have their
1053 * unique ID, so name and stream name should also match if being
1056 * Return: pointer of DAI link, or NULL if not found.
1058 struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
1059 int id, const char *name,
1060 const char *stream_name)
1062 struct snd_soc_dai_link *link, *_link;
1064 lockdep_assert_held(&client_mutex);
1066 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1070 if (name && (!link->name || strcmp(name, link->name)))
1073 if (stream_name && (!link->stream_name
1074 || strcmp(stream_name, link->stream_name)))
1082 EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
1084 static bool soc_is_dai_link_bound(struct snd_soc_card *card,
1085 struct snd_soc_dai_link *dai_link)
1087 struct snd_soc_pcm_runtime *rtd;
1089 list_for_each_entry(rtd, &card->rtd_list, list) {
1090 if (rtd->dai_link == dai_link)
1097 static int soc_bind_dai_link(struct snd_soc_card *card,
1098 struct snd_soc_dai_link *dai_link)
1100 struct snd_soc_pcm_runtime *rtd;
1101 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
1102 struct snd_soc_dai_link_component cpu_dai_component;
1103 struct snd_soc_dai **codec_dais;
1104 struct snd_soc_platform *platform;
1105 struct device_node *platform_of_node;
1106 const char *platform_name;
1109 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
1111 if (soc_is_dai_link_bound(card, dai_link)) {
1112 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
1117 rtd = soc_new_pcm_runtime(card, dai_link);
1121 cpu_dai_component.name = dai_link->cpu_name;
1122 cpu_dai_component.of_node = dai_link->cpu_of_node;
1123 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
1124 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
1125 if (!rtd->cpu_dai) {
1126 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
1127 dai_link->cpu_dai_name);
1131 rtd->num_codecs = dai_link->num_codecs;
1133 /* Find CODEC from registered CODECs */
1134 codec_dais = rtd->codec_dais;
1135 for (i = 0; i < rtd->num_codecs; i++) {
1136 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
1137 if (!codec_dais[i]) {
1138 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
1139 codecs[i].dai_name);
1144 /* Single codec links expect codec and codec_dai in runtime data */
1145 rtd->codec_dai = codec_dais[0];
1146 rtd->codec = rtd->codec_dai->codec;
1148 /* if there's no platform we match on the empty platform */
1149 platform_name = dai_link->platform_name;
1150 if (!platform_name && !dai_link->platform_of_node)
1151 platform_name = "snd-soc-dummy";
1153 /* find one from the set of registered platforms */
1154 list_for_each_entry(platform, &platform_list, list) {
1155 platform_of_node = platform->dev->of_node;
1156 if (!platform_of_node && platform->dev->parent->of_node)
1157 platform_of_node = platform->dev->parent->of_node;
1159 if (dai_link->platform_of_node) {
1160 if (platform_of_node != dai_link->platform_of_node)
1163 if (strcmp(platform->component.name, platform_name))
1167 rtd->platform = platform;
1169 if (!rtd->platform) {
1170 dev_err(card->dev, "ASoC: platform %s not registered\n",
1171 dai_link->platform_name);
1175 soc_add_pcm_runtime(card, rtd);
1179 soc_free_pcm_runtime(rtd);
1180 return -EPROBE_DEFER;
1183 static void soc_remove_component(struct snd_soc_component *component)
1185 if (!component->card)
1188 list_del(&component->card_list);
1190 if (component->remove)
1191 component->remove(component);
1193 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1195 soc_cleanup_component_debugfs(component);
1196 component->card = NULL;
1197 module_put(component->dev->driver->owner);
1200 static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1204 if (dai && dai->probed &&
1205 dai->driver->remove_order == order) {
1206 if (dai->driver->remove) {
1207 err = dai->driver->remove(dai);
1210 "ASoC: failed to remove %s: %d\n",
1217 static void soc_remove_link_dais(struct snd_soc_card *card,
1218 struct snd_soc_pcm_runtime *rtd, int order)
1222 /* unregister the rtd device */
1223 if (rtd->dev_registered) {
1224 device_unregister(rtd->dev);
1225 rtd->dev_registered = 0;
1228 /* remove the CODEC DAI */
1229 for (i = 0; i < rtd->num_codecs; i++)
1230 soc_remove_dai(rtd->codec_dais[i], order);
1232 soc_remove_dai(rtd->cpu_dai, order);
1235 static void soc_remove_link_components(struct snd_soc_card *card,
1236 struct snd_soc_pcm_runtime *rtd, int order)
1238 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1239 struct snd_soc_platform *platform = rtd->platform;
1240 struct snd_soc_component *component;
1243 /* remove the platform */
1244 if (platform && platform->component.driver->remove_order == order)
1245 soc_remove_component(&platform->component);
1247 /* remove the CODEC-side CODEC */
1248 for (i = 0; i < rtd->num_codecs; i++) {
1249 component = rtd->codec_dais[i]->component;
1250 if (component->driver->remove_order == order)
1251 soc_remove_component(component);
1254 /* remove any CPU-side CODEC */
1256 if (cpu_dai->component->driver->remove_order == order)
1257 soc_remove_component(cpu_dai->component);
1261 static void soc_remove_dai_links(struct snd_soc_card *card)
1264 struct snd_soc_pcm_runtime *rtd;
1265 struct snd_soc_dai_link *link, *_link;
1267 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1269 list_for_each_entry(rtd, &card->rtd_list, list)
1270 soc_remove_link_dais(card, rtd, order);
1273 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1275 list_for_each_entry(rtd, &card->rtd_list, list)
1276 soc_remove_link_components(card, rtd, order);
1279 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1280 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1281 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1284 list_del(&link->list);
1285 card->num_dai_links--;
1289 static int snd_soc_init_multicodec(struct snd_soc_card *card,
1290 struct snd_soc_dai_link *dai_link)
1292 /* Legacy codec/codec_dai link is a single entry in multicodec */
1293 if (dai_link->codec_name || dai_link->codec_of_node ||
1294 dai_link->codec_dai_name) {
1295 dai_link->num_codecs = 1;
1297 dai_link->codecs = devm_kzalloc(card->dev,
1298 sizeof(struct snd_soc_dai_link_component),
1300 if (!dai_link->codecs)
1303 dai_link->codecs[0].name = dai_link->codec_name;
1304 dai_link->codecs[0].of_node = dai_link->codec_of_node;
1305 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1308 if (!dai_link->codecs) {
1309 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1316 static int soc_init_dai_link(struct snd_soc_card *card,
1317 struct snd_soc_dai_link *link)
1321 ret = snd_soc_init_multicodec(card, link);
1323 dev_err(card->dev, "ASoC: failed to init multicodec\n");
1327 for (i = 0; i < link->num_codecs; i++) {
1329 * Codec must be specified by 1 of name or OF node,
1330 * not both or neither.
1332 if (!!link->codecs[i].name ==
1333 !!link->codecs[i].of_node) {
1334 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1338 /* Codec DAI name must be specified */
1339 if (!link->codecs[i].dai_name) {
1340 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1347 * Platform may be specified by either name or OF node, but
1348 * can be left unspecified, and a dummy platform will be used.
1350 if (link->platform_name && link->platform_of_node) {
1352 "ASoC: Both platform name/of_node are set for %s\n",
1358 * CPU device may be specified by either name or OF node, but
1359 * can be left unspecified, and will be matched based on DAI
1362 if (link->cpu_name && link->cpu_of_node) {
1364 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1369 * At least one of CPU DAI name or CPU device name/node must be
1372 if (!link->cpu_dai_name &&
1373 !(link->cpu_name || link->cpu_of_node)) {
1375 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1384 * snd_soc_add_dai_link - Add a DAI link dynamically
1385 * @card: The ASoC card to which the DAI link is added
1386 * @dai_link: The new DAI link to add
1388 * This function adds a DAI link to the ASoC card's link list.
1390 * Note: Topology can use this API to add DAI links when probing the
1391 * topology component. And machine drivers can still define static
1392 * DAI links in dai_link array.
1394 int snd_soc_add_dai_link(struct snd_soc_card *card,
1395 struct snd_soc_dai_link *dai_link)
1397 if (dai_link->dobj.type
1398 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1399 dev_err(card->dev, "Invalid dai link type %d\n",
1400 dai_link->dobj.type);
1404 lockdep_assert_held(&client_mutex);
1405 /* Notify the machine driver for extra initialization
1406 * on the link created by topology.
1408 if (dai_link->dobj.type && card->add_dai_link)
1409 card->add_dai_link(card, dai_link);
1411 list_add_tail(&dai_link->list, &card->dai_link_list);
1412 card->num_dai_links++;
1416 EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1419 * snd_soc_remove_dai_link - Remove a DAI link from the list
1420 * @card: The ASoC card that owns the link
1421 * @dai_link: The DAI link to remove
1423 * This function removes a DAI link from the ASoC card's link list.
1425 * For DAI links previously added by topology, topology should
1426 * remove them by using the dobj embedded in the link.
1428 void snd_soc_remove_dai_link(struct snd_soc_card *card,
1429 struct snd_soc_dai_link *dai_link)
1431 struct snd_soc_dai_link *link, *_link;
1433 if (dai_link->dobj.type
1434 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1435 dev_err(card->dev, "Invalid dai link type %d\n",
1436 dai_link->dobj.type);
1440 lockdep_assert_held(&client_mutex);
1441 /* Notify the machine driver for extra destruction
1442 * on the link created by topology.
1444 if (dai_link->dobj.type && card->remove_dai_link)
1445 card->remove_dai_link(card, dai_link);
1447 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1448 if (link == dai_link) {
1449 list_del(&link->list);
1450 card->num_dai_links--;
1455 EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1457 static void soc_set_name_prefix(struct snd_soc_card *card,
1458 struct snd_soc_component *component)
1462 if (card->codec_conf == NULL)
1465 for (i = 0; i < card->num_configs; i++) {
1466 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1467 if (map->of_node && component->dev->of_node != map->of_node)
1469 if (map->dev_name && strcmp(component->name, map->dev_name))
1471 component->name_prefix = map->name_prefix;
1476 static int soc_probe_component(struct snd_soc_card *card,
1477 struct snd_soc_component *component)
1479 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1480 struct snd_soc_dai *dai;
1483 if (!strcmp(component->name, "snd-soc-dummy"))
1486 if (component->card) {
1487 if (component->card != card) {
1488 dev_err(component->dev,
1489 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1490 card->name, component->card->name);
1496 if (!try_module_get(component->dev->driver->owner))
1499 component->card = card;
1501 soc_set_name_prefix(card, component);
1503 soc_init_component_debugfs(component);
1505 if (component->dapm_widgets) {
1506 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1507 component->num_dapm_widgets);
1510 dev_err(component->dev,
1511 "Failed to create new controls %d\n", ret);
1516 list_for_each_entry(dai, &component->dai_list, list) {
1517 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1519 dev_err(component->dev,
1520 "Failed to create DAI widgets %d\n", ret);
1525 if (component->probe) {
1526 ret = component->probe(component);
1528 dev_err(component->dev,
1529 "ASoC: failed to probe component %d\n", ret);
1533 WARN(dapm->idle_bias_off &&
1534 dapm->bias_level != SND_SOC_BIAS_OFF,
1535 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1539 /* machine specific init */
1540 if (component->init) {
1541 ret = component->init(component);
1543 dev_err(component->dev,
1544 "Failed to do machine specific init %d\n", ret);
1549 if (component->controls)
1550 snd_soc_add_component_controls(component, component->controls,
1551 component->num_controls);
1552 if (component->dapm_routes)
1553 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1554 component->num_dapm_routes);
1556 list_add(&dapm->list, &card->dapm_list);
1557 list_add(&component->card_list, &card->component_dev_list);
1562 soc_cleanup_component_debugfs(component);
1563 component->card = NULL;
1564 module_put(component->dev->driver->owner);
1569 static void rtd_release(struct device *dev)
1574 static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1579 /* register the rtd device */
1580 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1583 device_initialize(rtd->dev);
1584 rtd->dev->parent = rtd->card->dev;
1585 rtd->dev->release = rtd_release;
1586 rtd->dev->groups = soc_dev_attr_groups;
1587 dev_set_name(rtd->dev, "%s", name);
1588 dev_set_drvdata(rtd->dev, rtd);
1589 mutex_init(&rtd->pcm_mutex);
1590 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1591 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1592 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1593 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1594 ret = device_add(rtd->dev);
1596 /* calling put_device() here to free the rtd->dev */
1597 put_device(rtd->dev);
1598 dev_err(rtd->card->dev,
1599 "ASoC: failed to register runtime device: %d\n", ret);
1602 rtd->dev_registered = 1;
1606 static int soc_probe_link_components(struct snd_soc_card *card,
1607 struct snd_soc_pcm_runtime *rtd,
1610 struct snd_soc_platform *platform = rtd->platform;
1611 struct snd_soc_component *component;
1614 /* probe the CPU-side component, if it is a CODEC */
1615 component = rtd->cpu_dai->component;
1616 if (component->driver->probe_order == order) {
1617 ret = soc_probe_component(card, component);
1622 /* probe the CODEC-side components */
1623 for (i = 0; i < rtd->num_codecs; i++) {
1624 component = rtd->codec_dais[i]->component;
1625 if (component->driver->probe_order == order) {
1626 ret = soc_probe_component(card, component);
1632 /* probe the platform */
1633 if (platform->component.driver->probe_order == order) {
1634 ret = soc_probe_component(card, &platform->component);
1642 static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1646 if (!dai->probed && dai->driver->probe_order == order) {
1647 if (dai->driver->probe) {
1648 ret = dai->driver->probe(dai);
1651 "ASoC: failed to probe DAI %s: %d\n",
1663 static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1664 struct snd_soc_pcm_runtime *rtd)
1668 for (i = 0; i < num_dais; ++i) {
1669 struct snd_soc_dai_driver *drv = dais[i]->driver;
1671 if (!rtd->dai_link->no_pcm && drv->pcm_new)
1672 ret = drv->pcm_new(rtd, dais[i]);
1674 dev_err(dais[i]->dev,
1675 "ASoC: Failed to bind %s with pcm device\n",
1684 static int soc_link_dai_widgets(struct snd_soc_card *card,
1685 struct snd_soc_dai_link *dai_link,
1686 struct snd_soc_pcm_runtime *rtd)
1688 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1689 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1690 struct snd_soc_dapm_widget *sink, *source;
1693 if (rtd->num_codecs > 1)
1694 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1696 /* link the DAI widgets */
1697 sink = codec_dai->playback_widget;
1698 source = cpu_dai->capture_widget;
1699 if (sink && source) {
1700 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1701 dai_link->num_params,
1704 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1705 sink->name, source->name, ret);
1710 sink = cpu_dai->playback_widget;
1711 source = codec_dai->capture_widget;
1712 if (sink && source) {
1713 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1714 dai_link->num_params,
1717 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1718 sink->name, source->name, ret);
1726 static int soc_probe_link_dais(struct snd_soc_card *card,
1727 struct snd_soc_pcm_runtime *rtd, int order)
1729 struct snd_soc_dai_link *dai_link = rtd->dai_link;
1730 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1733 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1734 card->name, rtd->num, order);
1736 /* set default power off timeout */
1737 rtd->pmdown_time = pmdown_time;
1739 ret = soc_probe_dai(cpu_dai, order);
1743 /* probe the CODEC DAI */
1744 for (i = 0; i < rtd->num_codecs; i++) {
1745 ret = soc_probe_dai(rtd->codec_dais[i], order);
1750 /* complete DAI probe during last probe */
1751 if (order != SND_SOC_COMP_ORDER_LAST)
1754 /* do machine specific initialization */
1755 if (dai_link->init) {
1756 ret = dai_link->init(rtd);
1758 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1759 dai_link->name, ret);
1764 if (dai_link->dai_fmt)
1765 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1767 ret = soc_post_component_init(rtd, dai_link->name);
1771 #ifdef CONFIG_DEBUG_FS
1772 /* add DPCM sysfs entries */
1773 if (dai_link->dynamic)
1774 soc_dpcm_debugfs_add(rtd);
1777 if (cpu_dai->driver->compress_new) {
1778 /*create compress_device"*/
1779 ret = cpu_dai->driver->compress_new(rtd, rtd->num);
1781 dev_err(card->dev, "ASoC: can't create compress %s\n",
1782 dai_link->stream_name);
1787 if (!dai_link->params) {
1788 /* create the pcm */
1789 ret = soc_new_pcm(rtd, rtd->num);
1791 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1792 dai_link->stream_name, ret);
1795 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1798 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1799 rtd->num_codecs, rtd);
1803 INIT_DELAYED_WORK(&rtd->delayed_work,
1804 codec2codec_close_delayed_work);
1806 /* link the DAI widgets */
1807 ret = soc_link_dai_widgets(card, dai_link, rtd);
1816 static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1818 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1819 struct snd_soc_component *component;
1821 struct device_node *codec_of_node;
1823 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1824 /* codecs, usually analog devices */
1825 name = aux_dev->codec_name;
1826 codec_of_node = aux_dev->codec_of_node;
1827 component = soc_find_component(codec_of_node, name);
1830 name = of_node_full_name(codec_of_node);
1833 } else if (aux_dev->name) {
1834 /* generic components */
1835 name = aux_dev->name;
1836 component = soc_find_component(NULL, name);
1840 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1844 component->init = aux_dev->init;
1845 list_add(&component->card_aux_list, &card->aux_comp_list);
1850 dev_err(card->dev, "ASoC: %s not registered\n", name);
1851 return -EPROBE_DEFER;
1854 static int soc_probe_aux_devices(struct snd_soc_card *card)
1856 struct snd_soc_component *comp;
1860 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1862 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
1863 if (comp->driver->probe_order == order) {
1864 ret = soc_probe_component(card, comp);
1867 "ASoC: failed to probe aux component %s %d\n",
1878 static void soc_remove_aux_devices(struct snd_soc_card *card)
1880 struct snd_soc_component *comp, *_comp;
1883 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1885 list_for_each_entry_safe(comp, _comp,
1886 &card->aux_comp_list, card_aux_list) {
1888 if (comp->driver->remove_order == order) {
1889 soc_remove_component(comp);
1890 /* remove it from the card's aux_comp_list */
1891 list_del(&comp->card_aux_list);
1897 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1901 if (codec->cache_init)
1904 ret = snd_soc_cache_init(codec);
1907 "ASoC: Failed to set cache compression type: %d\n",
1911 codec->cache_init = 1;
1916 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1917 * @rtd: The runtime for which the DAI link format should be changed
1918 * @dai_fmt: The new DAI link format
1920 * This function updates the DAI link format for all DAIs connected to the DAI
1921 * link for the specified runtime.
1923 * Note: For setups with a static format set the dai_fmt field in the
1924 * corresponding snd_dai_link struct instead of using this function.
1926 * Returns 0 on success, otherwise a negative error code.
1928 int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1929 unsigned int dai_fmt)
1931 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1932 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1936 for (i = 0; i < rtd->num_codecs; i++) {
1937 struct snd_soc_dai *codec_dai = codec_dais[i];
1939 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1940 if (ret != 0 && ret != -ENOTSUPP) {
1941 dev_warn(codec_dai->dev,
1942 "ASoC: Failed to set DAI format: %d\n", ret);
1947 /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1948 if (cpu_dai->codec) {
1949 unsigned int inv_dai_fmt;
1951 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1952 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1953 case SND_SOC_DAIFMT_CBM_CFM:
1954 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1956 case SND_SOC_DAIFMT_CBM_CFS:
1957 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1959 case SND_SOC_DAIFMT_CBS_CFM:
1960 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1962 case SND_SOC_DAIFMT_CBS_CFS:
1963 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1967 dai_fmt = inv_dai_fmt;
1970 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1971 if (ret != 0 && ret != -ENOTSUPP) {
1972 dev_warn(cpu_dai->dev,
1973 "ASoC: Failed to set DAI format: %d\n", ret);
1979 EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1983 /* Trim special characters, and replace '-' with '_' since '-' is used to
1984 * separate different DMI fields in the card long name. Only number and
1985 * alphabet characters and a few separator characters are kept.
1987 static void cleanup_dmi_name(char *name)
1991 for (i = 0; name[i]; i++) {
1992 if (isalnum(name[i]) || (name[i] == '.')
1993 || (name[i] == '_'))
1994 name[j++] = name[i];
1995 else if (name[i] == '-')
2002 /* Check if a DMI field is valid, i.e. not containing any string
2003 * in the black list.
2005 static int is_dmi_valid(const char *field)
2009 while (dmi_blacklist[i]) {
2010 if (strstr(field, dmi_blacklist[i]))
2019 * snd_soc_set_dmi_name() - Register DMI names to card
2020 * @card: The card to register DMI names
2021 * @flavour: The flavour "differentiator" for the card amongst its peers.
2023 * An Intel machine driver may be used by many different devices but are
2024 * difficult for userspace to differentiate, since machine drivers ususally
2025 * use their own name as the card short name and leave the card long name
2026 * blank. To differentiate such devices and fix bugs due to lack of
2027 * device-specific configurations, this function allows DMI info to be used
2028 * as the sound card long name, in the format of
2029 * "vendor-product-version-board"
2030 * (Character '-' is used to separate different DMI fields here).
2031 * This will help the user space to load the device-specific Use Case Manager
2032 * (UCM) configurations for the card.
2034 * Possible card long names may be:
2035 * DellInc.-XPS139343-01-0310JH
2036 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
2037 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
2039 * This function also supports flavoring the card longname to provide
2040 * the extra differentiation, like "vendor-product-version-board-flavor".
2042 * We only keep number and alphabet characters and a few separator characters
2043 * in the card long name since UCM in the user space uses the card long names
2044 * as card configuration directory names and AudoConf cannot support special
2045 * charactors like SPACE.
2047 * Returns 0 on success, otherwise a negative error code.
2049 int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
2051 const char *vendor, *product, *product_version, *board;
2052 size_t longname_buf_size = sizeof(card->snd_card->longname);
2055 if (card->long_name)
2056 return 0; /* long name already set by driver or from DMI */
2058 /* make up dmi long name as: vendor.product.version.board */
2059 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2060 if (!vendor || !is_dmi_valid(vendor)) {
2061 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
2066 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
2068 cleanup_dmi_name(card->dmi_longname);
2070 product = dmi_get_system_info(DMI_PRODUCT_NAME);
2071 if (product && is_dmi_valid(product)) {
2072 len = strlen(card->dmi_longname);
2073 snprintf(card->dmi_longname + len,
2074 longname_buf_size - len,
2077 len++; /* skip the separator "-" */
2078 if (len < longname_buf_size)
2079 cleanup_dmi_name(card->dmi_longname + len);
2081 /* some vendors like Lenovo may only put a self-explanatory
2082 * name in the product version field
2084 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
2085 if (product_version && is_dmi_valid(product_version)) {
2086 len = strlen(card->dmi_longname);
2087 snprintf(card->dmi_longname + len,
2088 longname_buf_size - len,
2089 "-%s", product_version);
2092 if (len < longname_buf_size)
2093 cleanup_dmi_name(card->dmi_longname + len);
2097 board = dmi_get_system_info(DMI_BOARD_NAME);
2098 if (board && is_dmi_valid(board)) {
2099 len = strlen(card->dmi_longname);
2100 snprintf(card->dmi_longname + len,
2101 longname_buf_size - len,
2105 if (len < longname_buf_size)
2106 cleanup_dmi_name(card->dmi_longname + len);
2107 } else if (!product) {
2108 /* fall back to using legacy name */
2109 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
2113 /* Add flavour to dmi long name */
2115 len = strlen(card->dmi_longname);
2116 snprintf(card->dmi_longname + len,
2117 longname_buf_size - len,
2121 if (len < longname_buf_size)
2122 cleanup_dmi_name(card->dmi_longname + len);
2125 /* set the card long name */
2126 card->long_name = card->dmi_longname;
2130 EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
2131 #endif /* CONFIG_DMI */
2133 static int snd_soc_instantiate_card(struct snd_soc_card *card)
2135 struct snd_soc_codec *codec;
2136 struct snd_soc_pcm_runtime *rtd;
2137 struct snd_soc_dai_link *dai_link;
2140 mutex_lock(&client_mutex);
2141 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
2144 for (i = 0; i < card->num_links; i++) {
2145 ret = soc_bind_dai_link(card, &card->dai_link[i]);
2150 /* bind aux_devs too */
2151 for (i = 0; i < card->num_aux_devs; i++) {
2152 ret = soc_bind_aux_dev(card, i);
2157 /* add predefined DAI links to the list */
2158 for (i = 0; i < card->num_links; i++)
2159 snd_soc_add_dai_link(card, card->dai_link+i);
2161 /* initialize the register cache for each available codec */
2162 list_for_each_entry(codec, &codec_list, list) {
2163 if (codec->cache_init)
2165 ret = snd_soc_init_codec_cache(codec);
2170 /* card bind complete so register a sound card */
2171 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2172 card->owner, 0, &card->snd_card);
2175 "ASoC: can't create sound card for card %s: %d\n",
2180 soc_init_card_debugfs(card);
2182 card->dapm.bias_level = SND_SOC_BIAS_OFF;
2183 card->dapm.dev = card->dev;
2184 card->dapm.card = card;
2185 list_add(&card->dapm.list, &card->dapm_list);
2187 #ifdef CONFIG_DEBUG_FS
2188 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2191 #ifdef CONFIG_PM_SLEEP
2192 /* deferred resume work */
2193 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
2196 if (card->dapm_widgets)
2197 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2198 card->num_dapm_widgets);
2200 if (card->of_dapm_widgets)
2201 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2202 card->num_of_dapm_widgets);
2204 /* initialise the sound card only once */
2206 ret = card->probe(card);
2208 goto card_probe_error;
2211 /* probe all components used by DAI links on this card */
2212 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2214 list_for_each_entry(rtd, &card->rtd_list, list) {
2215 ret = soc_probe_link_components(card, rtd, order);
2218 "ASoC: failed to instantiate card %d\n",
2225 /* probe auxiliary components */
2226 ret = soc_probe_aux_devices(card);
2230 /* Find new DAI links added during probing components and bind them.
2231 * Components with topology may bring new DAIs and DAI links.
2233 list_for_each_entry(dai_link, &card->dai_link_list, list) {
2234 if (soc_is_dai_link_bound(card, dai_link))
2237 ret = soc_init_dai_link(card, dai_link);
2240 ret = soc_bind_dai_link(card, dai_link);
2245 /* probe all DAI links on this card */
2246 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2248 list_for_each_entry(rtd, &card->rtd_list, list) {
2249 ret = soc_probe_link_dais(card, rtd, order);
2252 "ASoC: failed to instantiate card %d\n",
2259 snd_soc_dapm_link_dai_widgets(card);
2260 snd_soc_dapm_connect_dai_link_widgets(card);
2263 snd_soc_add_card_controls(card, card->controls, card->num_controls);
2265 if (card->dapm_routes)
2266 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2267 card->num_dapm_routes);
2269 if (card->of_dapm_routes)
2270 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2271 card->num_of_dapm_routes);
2273 /* try to set some sane longname if DMI is available */
2274 snd_soc_set_dmi_name(card, NULL);
2276 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
2278 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2279 "%s", card->long_name ? card->long_name : card->name);
2280 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2281 "%s", card->driver_name ? card->driver_name : card->name);
2282 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2283 switch (card->snd_card->driver[i]) {
2289 if (!isalnum(card->snd_card->driver[i]))
2290 card->snd_card->driver[i] = '_';
2295 if (card->late_probe) {
2296 ret = card->late_probe(card);
2298 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
2300 goto probe_aux_dev_err;
2304 snd_soc_dapm_new_widgets(card);
2306 ret = snd_card_register(card->snd_card);
2308 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2310 goto probe_aux_dev_err;
2313 card->instantiated = 1;
2314 snd_soc_dapm_sync(&card->dapm);
2315 mutex_unlock(&card->mutex);
2316 mutex_unlock(&client_mutex);
2321 soc_remove_aux_devices(card);
2324 soc_remove_dai_links(card);
2330 snd_soc_dapm_free(&card->dapm);
2331 soc_cleanup_card_debugfs(card);
2332 snd_card_free(card->snd_card);
2335 soc_remove_pcm_runtimes(card);
2336 mutex_unlock(&card->mutex);
2337 mutex_unlock(&client_mutex);
2342 /* probes a new socdev */
2343 static int soc_probe(struct platform_device *pdev)
2345 struct snd_soc_card *card = platform_get_drvdata(pdev);
2348 * no card, so machine driver should be registering card
2349 * we should not be here in that case so ret error
2354 dev_warn(&pdev->dev,
2355 "ASoC: machine %s should use snd_soc_register_card()\n",
2358 /* Bodge while we unpick instantiation */
2359 card->dev = &pdev->dev;
2361 return snd_soc_register_card(card);
2364 static int soc_cleanup_card_resources(struct snd_soc_card *card)
2366 struct snd_soc_pcm_runtime *rtd;
2368 /* make sure any delayed work runs */
2369 list_for_each_entry(rtd, &card->rtd_list, list)
2370 flush_delayed_work(&rtd->delayed_work);
2372 /* free the ALSA card at first; this syncs with pending operations */
2373 snd_card_free(card->snd_card);
2375 /* remove and free each DAI */
2376 soc_remove_dai_links(card);
2377 soc_remove_pcm_runtimes(card);
2379 /* remove auxiliary devices */
2380 soc_remove_aux_devices(card);
2382 snd_soc_dapm_free(&card->dapm);
2383 soc_cleanup_card_debugfs(card);
2385 /* remove the card */
2392 /* removes a socdev */
2393 static int soc_remove(struct platform_device *pdev)
2395 struct snd_soc_card *card = platform_get_drvdata(pdev);
2397 snd_soc_unregister_card(card);
2401 int snd_soc_poweroff(struct device *dev)
2403 struct snd_soc_card *card = dev_get_drvdata(dev);
2404 struct snd_soc_pcm_runtime *rtd;
2406 if (!card->instantiated)
2409 /* Flush out pmdown_time work - we actually do want to run it
2410 * now, we're shutting down so no imminent restart. */
2411 list_for_each_entry(rtd, &card->rtd_list, list)
2412 flush_delayed_work(&rtd->delayed_work);
2414 snd_soc_dapm_shutdown(card);
2416 /* deactivate pins to sleep state */
2417 list_for_each_entry(rtd, &card->rtd_list, list) {
2418 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2421 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2422 for (i = 0; i < rtd->num_codecs; i++) {
2423 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
2424 pinctrl_pm_select_sleep_state(codec_dai->dev);
2430 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2432 const struct dev_pm_ops snd_soc_pm_ops = {
2433 .suspend = snd_soc_suspend,
2434 .resume = snd_soc_resume,
2435 .freeze = snd_soc_suspend,
2436 .thaw = snd_soc_resume,
2437 .poweroff = snd_soc_poweroff,
2438 .restore = snd_soc_resume,
2440 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2442 /* ASoC platform driver */
2443 static struct platform_driver soc_driver = {
2445 .name = "soc-audio",
2446 .pm = &snd_soc_pm_ops,
2449 .remove = soc_remove,
2453 * snd_soc_cnew - create new control
2454 * @_template: control template
2455 * @data: control private data
2456 * @long_name: control long name
2457 * @prefix: control name prefix
2459 * Create a new mixer control from a template control.
2461 * Returns 0 for success, else error.
2463 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2464 void *data, const char *long_name,
2467 struct snd_kcontrol_new template;
2468 struct snd_kcontrol *kcontrol;
2471 memcpy(&template, _template, sizeof(template));
2475 long_name = template.name;
2478 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2482 template.name = name;
2484 template.name = long_name;
2487 kcontrol = snd_ctl_new1(&template, data);
2493 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2495 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2496 const struct snd_kcontrol_new *controls, int num_controls,
2497 const char *prefix, void *data)
2501 for (i = 0; i < num_controls; i++) {
2502 const struct snd_kcontrol_new *control = &controls[i];
2503 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2504 control->name, prefix));
2506 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2507 control->name, err);
2515 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2518 struct snd_card *card = soc_card->snd_card;
2519 struct snd_kcontrol *kctl;
2521 if (unlikely(!name))
2524 list_for_each_entry(kctl, &card->controls, list)
2525 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2529 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2532 * snd_soc_add_component_controls - Add an array of controls to a component.
2534 * @component: Component to add controls to
2535 * @controls: Array of controls to add
2536 * @num_controls: Number of elements in the array
2538 * Return: 0 for success, else error.
2540 int snd_soc_add_component_controls(struct snd_soc_component *component,
2541 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2543 struct snd_card *card = component->card->snd_card;
2545 return snd_soc_add_controls(card, component->dev, controls,
2546 num_controls, component->name_prefix, component);
2548 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2551 * snd_soc_add_codec_controls - add an array of controls to a codec.
2552 * Convenience function to add a list of controls. Many codecs were
2553 * duplicating this code.
2555 * @codec: codec to add controls to
2556 * @controls: array of controls to add
2557 * @num_controls: number of elements in the array
2559 * Return 0 for success, else error.
2561 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2562 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2564 return snd_soc_add_component_controls(&codec->component, controls,
2567 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2570 * snd_soc_add_platform_controls - add an array of controls to a platform.
2571 * Convenience function to add a list of controls.
2573 * @platform: platform to add controls to
2574 * @controls: array of controls to add
2575 * @num_controls: number of elements in the array
2577 * Return 0 for success, else error.
2579 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2580 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2582 return snd_soc_add_component_controls(&platform->component, controls,
2585 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2588 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2589 * Convenience function to add a list of controls.
2591 * @soc_card: SoC card to add controls to
2592 * @controls: array of controls to add
2593 * @num_controls: number of elements in the array
2595 * Return 0 for success, else error.
2597 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2598 const struct snd_kcontrol_new *controls, int num_controls)
2600 struct snd_card *card = soc_card->snd_card;
2602 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2605 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2608 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2609 * Convienience function to add a list of controls.
2611 * @dai: DAI to add controls to
2612 * @controls: array of controls to add
2613 * @num_controls: number of elements in the array
2615 * Return 0 for success, else error.
2617 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2618 const struct snd_kcontrol_new *controls, int num_controls)
2620 struct snd_card *card = dai->component->card->snd_card;
2622 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2625 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2628 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2630 * @clk_id: DAI specific clock ID
2631 * @freq: new clock frequency in Hz
2632 * @dir: new clock direction - input/output.
2634 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2636 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2637 unsigned int freq, int dir)
2639 if (dai->driver && dai->driver->ops->set_sysclk)
2640 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2641 else if (dai->codec && dai->codec->driver->set_sysclk)
2642 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
2647 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2650 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2652 * @clk_id: DAI specific clock ID
2653 * @source: Source for the clock
2654 * @freq: new clock frequency in Hz
2655 * @dir: new clock direction - input/output.
2657 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2659 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2660 int source, unsigned int freq, int dir)
2662 if (codec->driver->set_sysclk)
2663 return codec->driver->set_sysclk(codec, clk_id, source,
2668 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2671 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2673 * @div_id: DAI specific clock divider ID
2674 * @div: new clock divisor.
2676 * Configures the clock dividers. This is used to derive the best DAI bit and
2677 * frame clocks from the system or master clock. It's best to set the DAI bit
2678 * and frame clocks as low as possible to save system power.
2680 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2681 int div_id, int div)
2683 if (dai->driver && dai->driver->ops->set_clkdiv)
2684 return dai->driver->ops->set_clkdiv(dai, div_id, div);
2688 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2691 * snd_soc_dai_set_pll - configure DAI PLL.
2693 * @pll_id: DAI specific PLL ID
2694 * @source: DAI specific source for the PLL
2695 * @freq_in: PLL input clock frequency in Hz
2696 * @freq_out: requested PLL output clock frequency in Hz
2698 * Configures and enables PLL to generate output clock based on input clock.
2700 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2701 unsigned int freq_in, unsigned int freq_out)
2703 if (dai->driver && dai->driver->ops->set_pll)
2704 return dai->driver->ops->set_pll(dai, pll_id, source,
2706 else if (dai->codec && dai->codec->driver->set_pll)
2707 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2712 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2715 * snd_soc_codec_set_pll - configure codec PLL.
2717 * @pll_id: DAI specific PLL ID
2718 * @source: DAI specific source for the PLL
2719 * @freq_in: PLL input clock frequency in Hz
2720 * @freq_out: requested PLL output clock frequency in Hz
2722 * Configures and enables PLL to generate output clock based on input clock.
2724 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2725 unsigned int freq_in, unsigned int freq_out)
2727 if (codec->driver->set_pll)
2728 return codec->driver->set_pll(codec, pll_id, source,
2733 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2736 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2738 * @ratio: Ratio of BCLK to Sample rate.
2740 * Configures the DAI for a preset BCLK to sample rate ratio.
2742 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2744 if (dai->driver && dai->driver->ops->set_bclk_ratio)
2745 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2749 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2752 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2754 * @fmt: SND_SOC_DAIFMT_ format value.
2756 * Configures the DAI hardware format and clocking.
2758 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2760 if (dai->driver == NULL)
2762 if (dai->driver->ops->set_fmt == NULL)
2764 return dai->driver->ops->set_fmt(dai, fmt);
2766 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2769 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
2770 * @slots: Number of slots in use.
2771 * @tx_mask: bitmask representing active TX slots.
2772 * @rx_mask: bitmask representing active RX slots.
2774 * Generates the TDM tx and rx slot default masks for DAI.
2776 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
2777 unsigned int *tx_mask,
2778 unsigned int *rx_mask)
2780 if (*tx_mask || *rx_mask)
2786 *tx_mask = (1 << slots) - 1;
2787 *rx_mask = (1 << slots) - 1;
2793 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2794 * @dai: The DAI to configure
2795 * @tx_mask: bitmask representing active TX slots.
2796 * @rx_mask: bitmask representing active RX slots.
2797 * @slots: Number of slots in use.
2798 * @slot_width: Width in bits for each slot.
2800 * This function configures the specified DAI for TDM operation. @slot contains
2801 * the total number of slots of the TDM stream and @slot_with the width of each
2802 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2803 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2804 * DAI should write to or read from. If a bit is set the corresponding slot is
2805 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2806 * the first slot, bit 1 to the second slot and so on. The first active slot
2807 * maps to the first channel of the DAI, the second active slot to the second
2808 * channel and so on.
2810 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2811 * @rx_mask and @slot_width will be ignored.
2813 * Returns 0 on success, a negative error code otherwise.
2815 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2816 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2818 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2819 dai->driver->ops->xlate_tdm_slot_mask(slots,
2820 &tx_mask, &rx_mask);
2822 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
2824 dai->tx_mask = tx_mask;
2825 dai->rx_mask = rx_mask;
2827 if (dai->driver && dai->driver->ops->set_tdm_slot)
2828 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2833 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2836 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2838 * @tx_num: how many TX channels
2839 * @tx_slot: pointer to an array which imply the TX slot number channel
2841 * @rx_num: how many RX channels
2842 * @rx_slot: pointer to an array which imply the RX slot number channel
2845 * configure the relationship between channel number and TDM slot number.
2847 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2848 unsigned int tx_num, unsigned int *tx_slot,
2849 unsigned int rx_num, unsigned int *rx_slot)
2851 if (dai->driver && dai->driver->ops->set_channel_map)
2852 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2857 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2860 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2862 * @tristate: tristate enable
2864 * Tristates the DAI so that others can use it.
2866 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2868 if (dai->driver && dai->driver->ops->set_tristate)
2869 return dai->driver->ops->set_tristate(dai, tristate);
2873 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2876 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2878 * @mute: mute enable
2879 * @direction: stream to mute
2881 * Mutes the DAI DAC.
2883 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2889 if (dai->driver->ops->mute_stream)
2890 return dai->driver->ops->mute_stream(dai, mute, direction);
2891 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2892 dai->driver->ops->digital_mute)
2893 return dai->driver->ops->digital_mute(dai, mute);
2897 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2900 * snd_soc_register_card - Register a card with the ASoC core
2902 * @card: Card to register
2905 int snd_soc_register_card(struct snd_soc_card *card)
2908 struct snd_soc_pcm_runtime *rtd;
2910 if (!card->name || !card->dev)
2913 for (i = 0; i < card->num_links; i++) {
2914 struct snd_soc_dai_link *link = &card->dai_link[i];
2916 ret = soc_init_dai_link(card, link);
2918 dev_err(card->dev, "ASoC: failed to init link %s\n",
2924 dev_set_drvdata(card->dev, card);
2926 snd_soc_initialize_card_lists(card);
2928 INIT_LIST_HEAD(&card->dai_link_list);
2929 card->num_dai_links = 0;
2931 INIT_LIST_HEAD(&card->rtd_list);
2934 INIT_LIST_HEAD(&card->dapm_dirty);
2935 INIT_LIST_HEAD(&card->dobj_list);
2936 card->instantiated = 0;
2937 mutex_init(&card->mutex);
2938 mutex_init(&card->dapm_mutex);
2940 ret = snd_soc_instantiate_card(card);
2944 /* deactivate pins to sleep state */
2945 list_for_each_entry(rtd, &card->rtd_list, list) {
2946 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2949 for (j = 0; j < rtd->num_codecs; j++) {
2950 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2951 if (!codec_dai->active)
2952 pinctrl_pm_select_sleep_state(codec_dai->dev);
2955 if (!cpu_dai->active)
2956 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2961 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2964 * snd_soc_unregister_card - Unregister a card with the ASoC core
2966 * @card: Card to unregister
2969 int snd_soc_unregister_card(struct snd_soc_card *card)
2971 if (card->instantiated) {
2972 card->instantiated = false;
2973 snd_soc_dapm_shutdown(card);
2974 soc_cleanup_card_resources(card);
2975 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2980 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2983 * Simplify DAI link configuration by removing ".-1" from device names
2984 * and sanitizing names.
2986 static char *fmt_single_name(struct device *dev, int *id)
2988 char *found, name[NAME_SIZE];
2991 if (dev_name(dev) == NULL)
2994 strlcpy(name, dev_name(dev), NAME_SIZE);
2996 /* are we a "%s.%d" name (platform and SPI components) */
2997 found = strstr(name, dev->driver->name);
3000 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3002 /* discard ID from name if ID == -1 */
3004 found[strlen(dev->driver->name)] = '\0';
3008 /* I2C component devices are named "bus-addr" */
3009 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3010 char tmp[NAME_SIZE];
3012 /* create unique ID number from I2C addr and bus */
3013 *id = ((id1 & 0xffff) << 16) + id2;
3015 /* sanitize component name for DAI link creation */
3016 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3017 strlcpy(name, tmp, NAME_SIZE);
3022 return kstrdup(name, GFP_KERNEL);
3026 * Simplify DAI link naming for single devices with multiple DAIs by removing
3027 * any ".-1" and using the DAI name (instead of device name).
3029 static inline char *fmt_multiple_name(struct device *dev,
3030 struct snd_soc_dai_driver *dai_drv)
3032 if (dai_drv->name == NULL) {
3034 "ASoC: error - multiple DAI %s registered with no name\n",
3039 return kstrdup(dai_drv->name, GFP_KERNEL);
3043 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
3045 * @component: The component for which the DAIs should be unregistered
3047 static void snd_soc_unregister_dais(struct snd_soc_component *component)
3049 struct snd_soc_dai *dai, *_dai;
3051 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
3052 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3054 list_del(&dai->list);
3060 /* Create a DAI and add it to the component's DAI list */
3061 static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
3062 struct snd_soc_dai_driver *dai_drv,
3063 bool legacy_dai_naming)
3065 struct device *dev = component->dev;
3066 struct snd_soc_dai *dai;
3068 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
3070 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3075 * Back in the old days when we still had component-less DAIs,
3076 * instead of having a static name, component-less DAIs would
3077 * inherit the name of the parent device so it is possible to
3078 * register multiple instances of the DAI. We still need to keep
3079 * the same naming style even though those DAIs are not
3080 * component-less anymore.
3082 if (legacy_dai_naming &&
3083 (dai_drv->id == 0 || dai_drv->name == NULL)) {
3084 dai->name = fmt_single_name(dev, &dai->id);
3086 dai->name = fmt_multiple_name(dev, dai_drv);
3088 dai->id = dai_drv->id;
3090 dai->id = component->num_dai;
3092 if (dai->name == NULL) {
3097 dai->component = component;
3099 dai->driver = dai_drv;
3100 if (!dai->driver->ops)
3101 dai->driver->ops = &null_dai_ops;
3103 list_add(&dai->list, &component->dai_list);
3104 component->num_dai++;
3106 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3111 * snd_soc_register_dais - Register a DAI with the ASoC core
3113 * @component: The component the DAIs are registered for
3114 * @dai_drv: DAI driver to use for the DAIs
3115 * @count: Number of DAIs
3116 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3119 static int snd_soc_register_dais(struct snd_soc_component *component,
3120 struct snd_soc_dai_driver *dai_drv, size_t count,
3121 bool legacy_dai_naming)
3123 struct device *dev = component->dev;
3124 struct snd_soc_dai *dai;
3128 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
3130 component->dai_drv = dai_drv;
3132 for (i = 0; i < count; i++) {
3134 dai = soc_add_dai(component, dai_drv + i,
3135 count == 1 && legacy_dai_naming);
3145 snd_soc_unregister_dais(component);
3151 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
3153 * @component: The component the DAIs are registered for
3154 * @dai_drv: DAI driver to use for the DAI
3156 * Topology can use this API to register DAIs when probing a component.
3157 * These DAIs's widgets will be freed in the card cleanup and the DAIs
3158 * will be freed in the component cleanup.
3160 int snd_soc_register_dai(struct snd_soc_component *component,
3161 struct snd_soc_dai_driver *dai_drv)
3163 struct snd_soc_dapm_context *dapm =
3164 snd_soc_component_get_dapm(component);
3165 struct snd_soc_dai *dai;
3168 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
3169 dev_err(component->dev, "Invalid dai type %d\n",
3170 dai_drv->dobj.type);
3174 lockdep_assert_held(&client_mutex);
3175 dai = soc_add_dai(component, dai_drv, false);
3179 /* Create the DAI widgets here. After adding DAIs, topology may
3180 * also add routes that need these widgets as source or sink.
3182 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
3184 dev_err(component->dev,
3185 "Failed to create DAI widgets %d\n", ret);
3190 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3192 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3193 enum snd_soc_dapm_type type, int subseq)
3195 struct snd_soc_component *component = dapm->component;
3197 component->driver->seq_notifier(component, type, subseq);
3200 static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3203 struct snd_soc_component *component = dapm->component;
3205 return component->driver->stream_event(component, event);
3208 static int snd_soc_component_initialize(struct snd_soc_component *component,
3209 const struct snd_soc_component_driver *driver, struct device *dev)
3211 struct snd_soc_dapm_context *dapm;
3213 component->name = fmt_single_name(dev, &component->id);
3214 if (!component->name) {
3215 dev_err(dev, "ASoC: Failed to allocate name\n");
3219 component->dev = dev;
3220 component->driver = driver;
3221 component->probe = component->driver->probe;
3222 component->remove = component->driver->remove;
3223 component->suspend = component->driver->suspend;
3224 component->resume = component->driver->resume;
3225 component->pcm_new = component->driver->pcm_new;
3226 component->pcm_free = component->driver->pcm_free;
3228 dapm = &component->dapm;
3230 dapm->component = component;
3231 dapm->bias_level = SND_SOC_BIAS_OFF;
3232 dapm->idle_bias_off = true;
3233 if (driver->seq_notifier)
3234 dapm->seq_notifier = snd_soc_component_seq_notifier;
3235 if (driver->stream_event)
3236 dapm->stream_event = snd_soc_component_stream_event;
3238 component->controls = driver->controls;
3239 component->num_controls = driver->num_controls;
3240 component->dapm_widgets = driver->dapm_widgets;
3241 component->num_dapm_widgets = driver->num_dapm_widgets;
3242 component->dapm_routes = driver->dapm_routes;
3243 component->num_dapm_routes = driver->num_dapm_routes;
3245 INIT_LIST_HEAD(&component->dai_list);
3246 mutex_init(&component->io_mutex);
3251 static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
3253 int val_bytes = regmap_get_val_bytes(component->regmap);
3255 /* Errors are legitimate for non-integer byte multiples */
3257 component->val_bytes = val_bytes;
3260 #ifdef CONFIG_REGMAP
3263 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
3264 * @component: The component for which to initialize the regmap instance
3265 * @regmap: The regmap instance that should be used by the component
3267 * This function allows deferred assignment of the regmap instance that is
3268 * associated with the component. Only use this if the regmap instance is not
3269 * yet ready when the component is registered. The function must also be called
3270 * before the first IO attempt of the component.
3272 void snd_soc_component_init_regmap(struct snd_soc_component *component,
3273 struct regmap *regmap)
3275 component->regmap = regmap;
3276 snd_soc_component_setup_regmap(component);
3278 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3281 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
3282 * @component: The component for which to de-initialize the regmap instance
3284 * Calls regmap_exit() on the regmap instance associated to the component and
3285 * removes the regmap instance from the component.
3287 * This function should only be used if snd_soc_component_init_regmap() was used
3288 * to initialize the regmap instance.
3290 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3292 regmap_exit(component->regmap);
3293 component->regmap = NULL;
3295 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3299 static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
3301 if (!component->write && !component->read) {
3302 if (!component->regmap)
3303 component->regmap = dev_get_regmap(component->dev, NULL);
3304 if (component->regmap)
3305 snd_soc_component_setup_regmap(component);
3308 list_add(&component->list, &component_list);
3309 INIT_LIST_HEAD(&component->dobj_list);
3312 static void snd_soc_component_add(struct snd_soc_component *component)
3314 mutex_lock(&client_mutex);
3315 snd_soc_component_add_unlocked(component);
3316 mutex_unlock(&client_mutex);
3319 static void snd_soc_component_cleanup(struct snd_soc_component *component)
3321 snd_soc_unregister_dais(component);
3322 kfree(component->name);
3325 static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3327 struct snd_soc_card *card = component->card;
3330 snd_soc_unregister_card(card);
3332 list_del(&component->list);
3335 int snd_soc_register_component(struct device *dev,
3336 const struct snd_soc_component_driver *cmpnt_drv,
3337 struct snd_soc_dai_driver *dai_drv,
3340 struct snd_soc_component *cmpnt;
3343 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
3345 dev_err(dev, "ASoC: Failed to allocate memory\n");
3349 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
3353 cmpnt->ignore_pmdown_time = true;
3354 cmpnt->registered_as_component = true;
3356 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
3358 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3362 snd_soc_component_add(cmpnt);
3367 snd_soc_component_cleanup(cmpnt);
3372 EXPORT_SYMBOL_GPL(snd_soc_register_component);
3375 * snd_soc_unregister_component - Unregister a component from the ASoC core
3377 * @dev: The device to unregister
3379 void snd_soc_unregister_component(struct device *dev)
3381 struct snd_soc_component *cmpnt;
3383 mutex_lock(&client_mutex);
3384 list_for_each_entry(cmpnt, &component_list, list) {
3385 if (dev == cmpnt->dev && cmpnt->registered_as_component)
3388 mutex_unlock(&client_mutex);
3392 snd_soc_tplg_component_remove(cmpnt, SND_SOC_TPLG_INDEX_ALL);
3393 snd_soc_component_del_unlocked(cmpnt);
3394 mutex_unlock(&client_mutex);
3395 snd_soc_component_cleanup(cmpnt);
3398 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3400 static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
3402 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3404 return platform->driver->probe(platform);
3407 static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
3409 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3411 platform->driver->remove(platform);
3414 static int snd_soc_platform_drv_pcm_new(struct snd_soc_pcm_runtime *rtd)
3416 struct snd_soc_platform *platform = rtd->platform;
3418 if (platform->driver->pcm_new)
3419 return platform->driver->pcm_new(rtd);
3424 static void snd_soc_platform_drv_pcm_free(struct snd_pcm *pcm)
3426 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
3427 struct snd_soc_platform *platform = rtd->platform;
3429 if (platform->driver->pcm_free)
3430 platform->driver->pcm_free(pcm);
3434 * snd_soc_add_platform - Add a platform to the ASoC core
3435 * @dev: The parent device for the platform
3436 * @platform: The platform to add
3437 * @platform_drv: The driver for the platform
3439 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
3440 const struct snd_soc_platform_driver *platform_drv)
3444 ret = snd_soc_component_initialize(&platform->component,
3445 &platform_drv->component_driver, dev);
3449 platform->dev = dev;
3450 platform->driver = platform_drv;
3452 if (platform_drv->probe)
3453 platform->component.probe = snd_soc_platform_drv_probe;
3454 if (platform_drv->remove)
3455 platform->component.remove = snd_soc_platform_drv_remove;
3456 if (platform_drv->pcm_new)
3457 platform->component.pcm_new = snd_soc_platform_drv_pcm_new;
3458 if (platform_drv->pcm_free)
3459 platform->component.pcm_free = snd_soc_platform_drv_pcm_free;
3461 #ifdef CONFIG_DEBUG_FS
3462 platform->component.debugfs_prefix = "platform";
3465 mutex_lock(&client_mutex);
3466 snd_soc_component_add_unlocked(&platform->component);
3467 list_add(&platform->list, &platform_list);
3468 mutex_unlock(&client_mutex);
3470 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
3471 platform->component.name);
3475 EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3478 * snd_soc_register_platform - Register a platform with the ASoC core
3480 * @dev: The device for the platform
3481 * @platform_drv: The driver for the platform
3483 int snd_soc_register_platform(struct device *dev,
3484 const struct snd_soc_platform_driver *platform_drv)
3486 struct snd_soc_platform *platform;
3489 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3491 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3492 if (platform == NULL)
3495 ret = snd_soc_add_platform(dev, platform, platform_drv);
3501 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3504 * snd_soc_remove_platform - Remove a platform from the ASoC core
3505 * @platform: the platform to remove
3507 void snd_soc_remove_platform(struct snd_soc_platform *platform)
3510 mutex_lock(&client_mutex);
3511 list_del(&platform->list);
3512 snd_soc_component_del_unlocked(&platform->component);
3513 mutex_unlock(&client_mutex);
3515 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3516 platform->component.name);
3518 snd_soc_component_cleanup(&platform->component);
3520 EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
3522 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
3524 struct snd_soc_platform *platform;
3526 mutex_lock(&client_mutex);
3527 list_for_each_entry(platform, &platform_list, list) {
3528 if (dev == platform->dev) {
3529 mutex_unlock(&client_mutex);
3533 mutex_unlock(&client_mutex);
3537 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
3540 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3542 * @dev: platform to unregister
3544 void snd_soc_unregister_platform(struct device *dev)
3546 struct snd_soc_platform *platform;
3548 platform = snd_soc_lookup_platform(dev);
3552 snd_soc_remove_platform(platform);
3555 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3557 static u64 codec_format_map[] = {
3558 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3559 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3560 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3561 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3562 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3563 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3564 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3565 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3566 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3567 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3568 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3569 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3570 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3571 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3572 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3573 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3576 /* Fix up the DAI formats for endianness: codecs don't actually see
3577 * the endianness of the data but we're using the CPU format
3578 * definitions which do need to include endianness so we ensure that
3579 * codec DAIs always have both big and little endian variants set.
3581 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3585 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3586 if (stream->formats & codec_format_map[i])
3587 stream->formats |= codec_format_map[i];
3590 static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3592 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3594 return codec->driver->probe(codec);
3597 static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3599 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3601 codec->driver->remove(codec);
3604 static int snd_soc_codec_drv_suspend(struct snd_soc_component *component)
3606 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3608 return codec->driver->suspend(codec);
3611 static int snd_soc_codec_drv_resume(struct snd_soc_component *component)
3613 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3615 return codec->driver->resume(codec);
3618 static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3619 unsigned int reg, unsigned int val)
3621 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3623 return codec->driver->write(codec, reg, val);
3626 static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3627 unsigned int reg, unsigned int *val)
3629 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3631 *val = codec->driver->read(codec, reg);
3636 static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
3637 enum snd_soc_bias_level level)
3639 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
3641 return codec->driver->set_bias_level(codec, level);
3645 * snd_soc_register_codec - Register a codec with the ASoC core
3647 * @dev: The parent device for this codec
3648 * @codec_drv: Codec driver
3649 * @dai_drv: The associated DAI driver
3650 * @num_dai: Number of DAIs
3652 int snd_soc_register_codec(struct device *dev,
3653 const struct snd_soc_codec_driver *codec_drv,
3654 struct snd_soc_dai_driver *dai_drv,
3657 struct snd_soc_dapm_context *dapm;
3658 struct snd_soc_codec *codec;
3659 struct snd_soc_dai *dai;
3662 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3664 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3668 codec->component.codec = codec;
3670 ret = snd_soc_component_initialize(&codec->component,
3671 &codec_drv->component_driver, dev);
3675 if (codec_drv->probe)
3676 codec->component.probe = snd_soc_codec_drv_probe;
3677 if (codec_drv->remove)
3678 codec->component.remove = snd_soc_codec_drv_remove;
3679 if (codec_drv->suspend)
3680 codec->component.suspend = snd_soc_codec_drv_suspend;
3681 if (codec_drv->resume)
3682 codec->component.resume = snd_soc_codec_drv_resume;
3683 if (codec_drv->write)
3684 codec->component.write = snd_soc_codec_drv_write;
3685 if (codec_drv->read)
3686 codec->component.read = snd_soc_codec_drv_read;
3687 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
3689 dapm = snd_soc_codec_get_dapm(codec);
3690 dapm->idle_bias_off = codec_drv->idle_bias_off;
3691 dapm->suspend_bias_off = codec_drv->suspend_bias_off;
3692 if (codec_drv->seq_notifier)
3693 dapm->seq_notifier = codec_drv->seq_notifier;
3694 if (codec_drv->set_bias_level)
3695 dapm->set_bias_level = snd_soc_codec_set_bias_level;
3697 codec->driver = codec_drv;
3698 codec->component.val_bytes = codec_drv->reg_word_size;
3700 #ifdef CONFIG_DEBUG_FS
3701 codec->component.init_debugfs = soc_init_codec_debugfs;
3702 codec->component.debugfs_prefix = "codec";
3705 if (codec_drv->get_regmap)
3706 codec->component.regmap = codec_drv->get_regmap(dev);
3708 for (i = 0; i < num_dai; i++) {
3709 fixup_codec_formats(&dai_drv[i].playback);
3710 fixup_codec_formats(&dai_drv[i].capture);
3713 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3715 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3719 list_for_each_entry(dai, &codec->component.dai_list, list)
3722 mutex_lock(&client_mutex);
3723 snd_soc_component_add_unlocked(&codec->component);
3724 list_add(&codec->list, &codec_list);
3725 mutex_unlock(&client_mutex);
3727 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3728 codec->component.name);
3732 snd_soc_component_cleanup(&codec->component);
3737 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3740 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3742 * @dev: codec to unregister
3744 void snd_soc_unregister_codec(struct device *dev)
3746 struct snd_soc_codec *codec;
3748 mutex_lock(&client_mutex);
3749 list_for_each_entry(codec, &codec_list, list) {
3750 if (dev == codec->dev)
3753 mutex_unlock(&client_mutex);
3757 list_del(&codec->list);
3758 snd_soc_component_del_unlocked(&codec->component);
3759 mutex_unlock(&client_mutex);
3761 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3762 codec->component.name);
3764 snd_soc_component_cleanup(&codec->component);
3765 snd_soc_cache_exit(codec);
3768 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3770 /* Retrieve a card's name from device tree */
3771 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3772 const char *propname)
3774 struct device_node *np;
3778 pr_err("card->dev is not set before calling %s\n", __func__);
3782 np = card->dev->of_node;
3784 ret = of_property_read_string_index(np, propname, 0, &card->name);
3786 * EINVAL means the property does not exist. This is fine providing
3787 * card->name was previously set, which is checked later in
3788 * snd_soc_register_card.
3790 if (ret < 0 && ret != -EINVAL) {
3792 "ASoC: Property '%s' could not be read: %d\n",
3799 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3801 static const struct snd_soc_dapm_widget simple_widgets[] = {
3802 SND_SOC_DAPM_MIC("Microphone", NULL),
3803 SND_SOC_DAPM_LINE("Line", NULL),
3804 SND_SOC_DAPM_HP("Headphone", NULL),
3805 SND_SOC_DAPM_SPK("Speaker", NULL),
3808 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
3809 const char *propname)
3811 struct device_node *np = card->dev->of_node;
3812 struct snd_soc_dapm_widget *widgets;
3813 const char *template, *wname;
3814 int i, j, num_widgets, ret;
3816 num_widgets = of_property_count_strings(np, propname);
3817 if (num_widgets < 0) {
3819 "ASoC: Property '%s' does not exist\n", propname);
3822 if (num_widgets & 1) {
3824 "ASoC: Property '%s' length is not even\n", propname);
3830 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3835 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3839 "ASoC: Could not allocate memory for widgets\n");
3843 for (i = 0; i < num_widgets; i++) {
3844 ret = of_property_read_string_index(np, propname,
3848 "ASoC: Property '%s' index %d read error:%d\n",
3849 propname, 2 * i, ret);
3853 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3854 if (!strncmp(template, simple_widgets[j].name,
3855 strlen(simple_widgets[j].name))) {
3856 widgets[i] = simple_widgets[j];
3861 if (j >= ARRAY_SIZE(simple_widgets)) {
3863 "ASoC: DAPM widget '%s' is not supported\n",
3868 ret = of_property_read_string_index(np, propname,
3873 "ASoC: Property '%s' index %d read error:%d\n",
3874 propname, (2 * i) + 1, ret);
3878 widgets[i].name = wname;
3881 card->of_dapm_widgets = widgets;
3882 card->num_of_dapm_widgets = num_widgets;
3886 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
3888 static int snd_soc_of_get_slot_mask(struct device_node *np,
3889 const char *prop_name,
3893 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
3899 for (i = 0; i < val; i++)
3900 if (be32_to_cpup(&of_slot_mask[i]))
3906 int snd_soc_of_parse_tdm_slot(struct device_node *np,
3907 unsigned int *tx_mask,
3908 unsigned int *rx_mask,
3909 unsigned int *slots,
3910 unsigned int *slot_width)
3916 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3918 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3920 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3921 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3929 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3930 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3940 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3942 void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
3943 struct snd_soc_codec_conf *codec_conf,
3944 struct device_node *of_node,
3945 const char *propname)
3947 struct device_node *np = card->dev->of_node;
3951 ret = of_property_read_string(np, propname, &str);
3953 /* no prefix is not error */
3957 codec_conf->of_node = of_node;
3958 codec_conf->name_prefix = str;
3960 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
3962 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3963 const char *propname)
3965 struct device_node *np = card->dev->of_node;
3967 struct snd_soc_dapm_route *routes;
3970 num_routes = of_property_count_strings(np, propname);
3971 if (num_routes < 0 || num_routes & 1) {
3973 "ASoC: Property '%s' does not exist or its length is not even\n",
3979 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3984 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3988 "ASoC: Could not allocate DAPM route table\n");
3992 for (i = 0; i < num_routes; i++) {
3993 ret = of_property_read_string_index(np, propname,
3994 2 * i, &routes[i].sink);
3997 "ASoC: Property '%s' index %d could not be read: %d\n",
3998 propname, 2 * i, ret);
4001 ret = of_property_read_string_index(np, propname,
4002 (2 * i) + 1, &routes[i].source);
4005 "ASoC: Property '%s' index %d could not be read: %d\n",
4006 propname, (2 * i) + 1, ret);
4011 card->num_of_dapm_routes = num_routes;
4012 card->of_dapm_routes = routes;
4016 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4018 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4020 struct device_node **bitclkmaster,
4021 struct device_node **framemaster)
4025 unsigned int format = 0;
4031 } of_fmt_table[] = {
4032 { "i2s", SND_SOC_DAIFMT_I2S },
4033 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4034 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4035 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4036 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4037 { "ac97", SND_SOC_DAIFMT_AC97 },
4038 { "pdm", SND_SOC_DAIFMT_PDM},
4039 { "msb", SND_SOC_DAIFMT_MSB },
4040 { "lsb", SND_SOC_DAIFMT_LSB },
4047 * check "dai-format = xxx"
4048 * or "[prefix]format = xxx"
4049 * SND_SOC_DAIFMT_FORMAT_MASK area
4051 ret = of_property_read_string(np, "dai-format", &str);
4053 snprintf(prop, sizeof(prop), "%sformat", prefix);
4054 ret = of_property_read_string(np, prop, &str);
4057 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4058 if (strcmp(str, of_fmt_table[i].name) == 0) {
4059 format |= of_fmt_table[i].val;
4066 * check "[prefix]continuous-clock"
4067 * SND_SOC_DAIFMT_CLOCK_MASK area
4069 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4070 if (of_property_read_bool(np, prop))
4071 format |= SND_SOC_DAIFMT_CONT;
4073 format |= SND_SOC_DAIFMT_GATED;
4076 * check "[prefix]bitclock-inversion"
4077 * check "[prefix]frame-inversion"
4078 * SND_SOC_DAIFMT_INV_MASK area
4080 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4081 bit = !!of_get_property(np, prop, NULL);
4083 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4084 frame = !!of_get_property(np, prop, NULL);
4086 switch ((bit << 4) + frame) {
4088 format |= SND_SOC_DAIFMT_IB_IF;
4091 format |= SND_SOC_DAIFMT_IB_NF;
4094 format |= SND_SOC_DAIFMT_NB_IF;
4097 /* SND_SOC_DAIFMT_NB_NF is default */
4102 * check "[prefix]bitclock-master"
4103 * check "[prefix]frame-master"
4104 * SND_SOC_DAIFMT_MASTER_MASK area
4106 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4107 bit = !!of_get_property(np, prop, NULL);
4108 if (bit && bitclkmaster)
4109 *bitclkmaster = of_parse_phandle(np, prop, 0);
4111 snprintf(prop, sizeof(prop), "%sframe-master", prefix);