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->num_codecs ? 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 int codec_list_seq_show(struct seq_file *m, void *v)
354 struct snd_soc_codec *codec;
356 mutex_lock(&client_mutex);
358 list_for_each_entry(codec, &codec_list, list)
359 seq_printf(m, "%s\n", codec->component.name);
361 mutex_unlock(&client_mutex);
366 static int codec_list_seq_open(struct inode *inode, struct file *file)
368 return single_open(file, codec_list_seq_show, NULL);
371 static const struct file_operations codec_list_fops = {
372 .open = codec_list_seq_open,
375 .release = single_release,
378 static int dai_list_seq_show(struct seq_file *m, void *v)
380 struct snd_soc_component *component;
381 struct snd_soc_dai *dai;
383 mutex_lock(&client_mutex);
385 list_for_each_entry(component, &component_list, list)
386 list_for_each_entry(dai, &component->dai_list, list)
387 seq_printf(m, "%s\n", dai->name);
389 mutex_unlock(&client_mutex);
394 static int dai_list_seq_open(struct inode *inode, struct file *file)
396 return single_open(file, dai_list_seq_show, NULL);
399 static const struct file_operations dai_list_fops = {
400 .open = dai_list_seq_open,
403 .release = single_release,
406 static int platform_list_seq_show(struct seq_file *m, void *v)
408 struct snd_soc_platform *platform;
410 mutex_lock(&client_mutex);
412 list_for_each_entry(platform, &platform_list, list)
413 seq_printf(m, "%s\n", platform->component.name);
415 mutex_unlock(&client_mutex);
420 static int platform_list_seq_open(struct inode *inode, struct file *file)
422 return single_open(file, platform_list_seq_show, NULL);
425 static const struct file_operations platform_list_fops = {
426 .open = platform_list_seq_open,
429 .release = single_release,
432 static void soc_init_card_debugfs(struct snd_soc_card *card)
434 if (!snd_soc_debugfs_root)
437 card->debugfs_card_root = debugfs_create_dir(card->name,
438 snd_soc_debugfs_root);
439 if (!card->debugfs_card_root) {
441 "ASoC: Failed to create card debugfs directory\n");
445 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
446 card->debugfs_card_root,
448 if (!card->debugfs_pop_time)
450 "ASoC: Failed to create pop time debugfs file\n");
453 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
455 debugfs_remove_recursive(card->debugfs_card_root);
458 static void snd_soc_debugfs_init(void)
460 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
461 if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) {
462 pr_warn("ASoC: Failed to create debugfs directory\n");
463 snd_soc_debugfs_root = NULL;
467 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
469 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
471 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
473 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
475 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
476 &platform_list_fops))
477 pr_warn("ASoC: Failed to create platform list debugfs file\n");
480 static void snd_soc_debugfs_exit(void)
482 debugfs_remove_recursive(snd_soc_debugfs_root);
487 #define soc_init_codec_debugfs NULL
489 static inline void soc_init_component_debugfs(
490 struct snd_soc_component *component)
494 static inline void soc_cleanup_component_debugfs(
495 struct snd_soc_component *component)
499 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
503 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
507 static inline void snd_soc_debugfs_init(void)
511 static inline void snd_soc_debugfs_exit(void)
517 static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
518 struct snd_soc_component *component)
520 struct snd_soc_rtdcom_list *rtdcom;
521 struct snd_soc_rtdcom_list *new_rtdcom;
523 for_each_rtdcom(rtd, rtdcom) {
524 /* already connected */
525 if (rtdcom->component == component)
529 new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
533 new_rtdcom->component = component;
534 INIT_LIST_HEAD(&new_rtdcom->list);
536 list_add_tail(&new_rtdcom->list, &rtd->component_list);
541 static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
543 struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
545 for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
548 INIT_LIST_HEAD(&rtd->component_list);
551 struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
552 const char *driver_name)
554 struct snd_soc_rtdcom_list *rtdcom;
556 for_each_rtdcom(rtd, rtdcom) {
557 if ((rtdcom->component->driver->name == driver_name) ||
558 strcmp(rtdcom->component->driver->name, driver_name) == 0)
559 return rtdcom->component;
564 EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
566 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
567 const char *dai_link, int stream)
569 struct snd_soc_pcm_runtime *rtd;
571 list_for_each_entry(rtd, &card->rtd_list, list) {
572 if (rtd->dai_link->no_pcm &&
573 !strcmp(rtd->dai_link->name, dai_link))
574 return rtd->pcm->streams[stream].substream;
576 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
579 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
581 static const struct snd_soc_ops null_snd_soc_ops;
583 static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
584 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
586 struct snd_soc_pcm_runtime *rtd;
588 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
592 INIT_LIST_HEAD(&rtd->component_list);
594 rtd->dai_link = dai_link;
595 if (!rtd->dai_link->ops)
596 rtd->dai_link->ops = &null_snd_soc_ops;
598 rtd->codec_dais = kzalloc(sizeof(struct snd_soc_dai *) *
599 dai_link->num_codecs,
601 if (!rtd->codec_dais) {
609 static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
611 kfree(rtd->codec_dais);
612 snd_soc_rtdcom_del_all(rtd);
616 static void soc_add_pcm_runtime(struct snd_soc_card *card,
617 struct snd_soc_pcm_runtime *rtd)
619 list_add_tail(&rtd->list, &card->rtd_list);
620 rtd->num = card->num_rtd;
624 static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
626 struct snd_soc_pcm_runtime *rtd, *_rtd;
628 list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) {
629 list_del(&rtd->list);
630 soc_free_pcm_runtime(rtd);
636 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
637 const char *dai_link)
639 struct snd_soc_pcm_runtime *rtd;
641 list_for_each_entry(rtd, &card->rtd_list, list) {
642 if (!strcmp(rtd->dai_link->name, dai_link))
645 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
648 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
650 static void codec2codec_close_delayed_work(struct work_struct *work)
652 /* Currently nothing to do for c2c links
653 * Since c2c links are internal nodes in the DAPM graph and
654 * don't interface with the outside world or application layer
655 * we don't have to do any special handling on close.
659 #ifdef CONFIG_PM_SLEEP
660 /* powers down audio subsystem for suspend */
661 int snd_soc_suspend(struct device *dev)
663 struct snd_soc_card *card = dev_get_drvdata(dev);
664 struct snd_soc_component *component;
665 struct snd_soc_pcm_runtime *rtd;
668 /* If the card is not initialized yet there is nothing to do */
669 if (!card->instantiated)
672 /* Due to the resume being scheduled into a workqueue we could
673 * suspend before that's finished - wait for it to complete.
675 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
677 /* we're going to block userspace touching us until resume completes */
678 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
680 /* mute any active DACs */
681 list_for_each_entry(rtd, &card->rtd_list, list) {
683 if (rtd->dai_link->ignore_suspend)
686 for (i = 0; i < rtd->num_codecs; i++) {
687 struct snd_soc_dai *dai = rtd->codec_dais[i];
688 struct snd_soc_dai_driver *drv = dai->driver;
690 if (drv->ops->digital_mute && dai->playback_active)
691 drv->ops->digital_mute(dai, 1);
695 /* suspend all pcms */
696 list_for_each_entry(rtd, &card->rtd_list, list) {
697 if (rtd->dai_link->ignore_suspend)
700 snd_pcm_suspend_all(rtd->pcm);
703 if (card->suspend_pre)
704 card->suspend_pre(card);
706 list_for_each_entry(rtd, &card->rtd_list, list) {
707 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
709 if (rtd->dai_link->ignore_suspend)
712 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
713 cpu_dai->driver->suspend(cpu_dai);
716 /* close any waiting streams */
717 list_for_each_entry(rtd, &card->rtd_list, list)
718 flush_delayed_work(&rtd->delayed_work);
720 list_for_each_entry(rtd, &card->rtd_list, list) {
722 if (rtd->dai_link->ignore_suspend)
725 snd_soc_dapm_stream_event(rtd,
726 SNDRV_PCM_STREAM_PLAYBACK,
727 SND_SOC_DAPM_STREAM_SUSPEND);
729 snd_soc_dapm_stream_event(rtd,
730 SNDRV_PCM_STREAM_CAPTURE,
731 SND_SOC_DAPM_STREAM_SUSPEND);
734 /* Recheck all endpoints too, their state is affected by suspend */
735 dapm_mark_endpoints_dirty(card);
736 snd_soc_dapm_sync(&card->dapm);
738 /* suspend all COMPONENTs */
739 list_for_each_entry(component, &card->component_dev_list, card_list) {
740 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
742 /* If there are paths active then the COMPONENT will be held with
743 * bias _ON and should not be suspended. */
744 if (!component->suspended) {
745 switch (snd_soc_dapm_get_bias_level(dapm)) {
746 case SND_SOC_BIAS_STANDBY:
748 * If the COMPONENT is capable of idle
749 * bias off then being in STANDBY
750 * means it's doing something,
751 * otherwise fall through.
753 if (dapm->idle_bias_off) {
754 dev_dbg(component->dev,
755 "ASoC: idle_bias_off CODEC on over suspend\n");
759 case SND_SOC_BIAS_OFF:
760 if (component->suspend)
761 component->suspend(component);
762 component->suspended = 1;
763 if (component->regmap)
764 regcache_mark_dirty(component->regmap);
765 /* deactivate pins to sleep state */
766 pinctrl_pm_select_sleep_state(component->dev);
769 dev_dbg(component->dev,
770 "ASoC: COMPONENT is on over suspend\n");
776 list_for_each_entry(rtd, &card->rtd_list, list) {
777 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
779 if (rtd->dai_link->ignore_suspend)
782 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
783 cpu_dai->driver->suspend(cpu_dai);
785 /* deactivate pins to sleep state */
786 pinctrl_pm_select_sleep_state(cpu_dai->dev);
789 if (card->suspend_post)
790 card->suspend_post(card);
794 EXPORT_SYMBOL_GPL(snd_soc_suspend);
796 /* deferred resume work, so resume can complete before we finished
797 * setting our codec back up, which can be very slow on I2C
799 static void soc_resume_deferred(struct work_struct *work)
801 struct snd_soc_card *card =
802 container_of(work, struct snd_soc_card, deferred_resume_work);
803 struct snd_soc_pcm_runtime *rtd;
804 struct snd_soc_component *component;
807 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
808 * so userspace apps are blocked from touching us
811 dev_dbg(card->dev, "ASoC: starting resume work\n");
813 /* Bring us up into D2 so that DAPM starts enabling things */
814 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
816 if (card->resume_pre)
817 card->resume_pre(card);
819 /* resume control bus DAIs */
820 list_for_each_entry(rtd, &card->rtd_list, list) {
821 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
823 if (rtd->dai_link->ignore_suspend)
826 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
827 cpu_dai->driver->resume(cpu_dai);
830 list_for_each_entry(component, &card->component_dev_list, card_list) {
831 if (component->suspended) {
832 if (component->resume)
833 component->resume(component);
834 component->suspended = 0;
838 list_for_each_entry(rtd, &card->rtd_list, list) {
840 if (rtd->dai_link->ignore_suspend)
843 snd_soc_dapm_stream_event(rtd,
844 SNDRV_PCM_STREAM_PLAYBACK,
845 SND_SOC_DAPM_STREAM_RESUME);
847 snd_soc_dapm_stream_event(rtd,
848 SNDRV_PCM_STREAM_CAPTURE,
849 SND_SOC_DAPM_STREAM_RESUME);
852 /* unmute any active DACs */
853 list_for_each_entry(rtd, &card->rtd_list, list) {
855 if (rtd->dai_link->ignore_suspend)
858 for (i = 0; i < rtd->num_codecs; i++) {
859 struct snd_soc_dai *dai = rtd->codec_dais[i];
860 struct snd_soc_dai_driver *drv = dai->driver;
862 if (drv->ops->digital_mute && dai->playback_active)
863 drv->ops->digital_mute(dai, 0);
867 list_for_each_entry(rtd, &card->rtd_list, list) {
868 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
870 if (rtd->dai_link->ignore_suspend)
873 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
874 cpu_dai->driver->resume(cpu_dai);
877 if (card->resume_post)
878 card->resume_post(card);
880 dev_dbg(card->dev, "ASoC: resume work completed\n");
882 /* Recheck all endpoints too, their state is affected by suspend */
883 dapm_mark_endpoints_dirty(card);
884 snd_soc_dapm_sync(&card->dapm);
886 /* userspace can access us now we are back as we were before */
887 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
890 /* powers up audio subsystem after a suspend */
891 int snd_soc_resume(struct device *dev)
893 struct snd_soc_card *card = dev_get_drvdata(dev);
894 bool bus_control = false;
895 struct snd_soc_pcm_runtime *rtd;
897 /* If the card is not initialized yet there is nothing to do */
898 if (!card->instantiated)
901 /* activate pins from sleep state */
902 list_for_each_entry(rtd, &card->rtd_list, list) {
903 struct snd_soc_dai **codec_dais = rtd->codec_dais;
904 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
908 pinctrl_pm_select_default_state(cpu_dai->dev);
910 for (j = 0; j < rtd->num_codecs; j++) {
911 struct snd_soc_dai *codec_dai = codec_dais[j];
912 if (codec_dai->active)
913 pinctrl_pm_select_default_state(codec_dai->dev);
918 * DAIs that also act as the control bus master might have other drivers
919 * hanging off them so need to resume immediately. Other drivers don't
920 * have that problem and may take a substantial amount of time to resume
921 * due to I/O costs and anti-pop so handle them out of line.
923 list_for_each_entry(rtd, &card->rtd_list, list) {
924 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
925 bus_control |= cpu_dai->driver->bus_control;
928 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
929 soc_resume_deferred(&card->deferred_resume_work);
931 dev_dbg(dev, "ASoC: Scheduling resume work\n");
932 if (!schedule_work(&card->deferred_resume_work))
933 dev_err(dev, "ASoC: resume work item may be lost\n");
938 EXPORT_SYMBOL_GPL(snd_soc_resume);
940 #define snd_soc_suspend NULL
941 #define snd_soc_resume NULL
944 static const struct snd_soc_dai_ops null_dai_ops = {
947 static struct snd_soc_component *soc_find_component(
948 const struct device_node *of_node, const char *name)
950 struct snd_soc_component *component;
952 lockdep_assert_held(&client_mutex);
954 list_for_each_entry(component, &component_list, list) {
956 if (component->dev->of_node == of_node)
958 } else if (strcmp(component->name, name) == 0) {
967 * snd_soc_find_dai - Find a registered DAI
969 * @dlc: name of the DAI or the DAI driver and optional component info to match
971 * This function will search all registered components and their DAIs to
972 * find the DAI of the same name. The component's of_node and name
973 * should also match if being specified.
975 * Return: pointer of DAI, or NULL if not found.
977 struct snd_soc_dai *snd_soc_find_dai(
978 const struct snd_soc_dai_link_component *dlc)
980 struct snd_soc_component *component;
981 struct snd_soc_dai *dai;
982 struct device_node *component_of_node;
984 lockdep_assert_held(&client_mutex);
986 /* Find CPU DAI from registered DAIs*/
987 list_for_each_entry(component, &component_list, list) {
988 component_of_node = component->dev->of_node;
989 if (!component_of_node && component->dev->parent)
990 component_of_node = component->dev->parent->of_node;
992 if (dlc->of_node && component_of_node != dlc->of_node)
994 if (dlc->name && strcmp(component->name, dlc->name))
996 list_for_each_entry(dai, &component->dai_list, list) {
997 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
998 && (!dai->driver->name
999 || strcmp(dai->driver->name, dlc->dai_name)))
1008 EXPORT_SYMBOL_GPL(snd_soc_find_dai);
1012 * snd_soc_find_dai_link - Find a DAI link
1015 * @id: DAI link ID to match
1016 * @name: DAI link name to match, optional
1017 * @stream_name: DAI link stream name to match, optional
1019 * This function will search all existing DAI links of the soc card to
1020 * find the link of the same ID. Since DAI links may not have their
1021 * unique ID, so name and stream name should also match if being
1024 * Return: pointer of DAI link, or NULL if not found.
1026 struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
1027 int id, const char *name,
1028 const char *stream_name)
1030 struct snd_soc_dai_link *link, *_link;
1032 lockdep_assert_held(&client_mutex);
1034 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1038 if (name && (!link->name || strcmp(name, link->name)))
1041 if (stream_name && (!link->stream_name
1042 || strcmp(stream_name, link->stream_name)))
1050 EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
1052 static bool soc_is_dai_link_bound(struct snd_soc_card *card,
1053 struct snd_soc_dai_link *dai_link)
1055 struct snd_soc_pcm_runtime *rtd;
1057 list_for_each_entry(rtd, &card->rtd_list, list) {
1058 if (rtd->dai_link == dai_link)
1065 static int soc_bind_dai_link(struct snd_soc_card *card,
1066 struct snd_soc_dai_link *dai_link)
1068 struct snd_soc_pcm_runtime *rtd;
1069 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
1070 struct snd_soc_dai_link_component cpu_dai_component;
1071 struct snd_soc_component *component;
1072 struct snd_soc_dai **codec_dais;
1073 struct snd_soc_platform *platform;
1074 struct device_node *platform_of_node;
1075 const char *platform_name;
1078 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
1080 if (soc_is_dai_link_bound(card, dai_link)) {
1081 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
1086 rtd = soc_new_pcm_runtime(card, dai_link);
1090 cpu_dai_component.name = dai_link->cpu_name;
1091 cpu_dai_component.of_node = dai_link->cpu_of_node;
1092 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
1093 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
1094 if (!rtd->cpu_dai) {
1095 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
1096 dai_link->cpu_dai_name);
1099 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
1101 rtd->num_codecs = dai_link->num_codecs;
1103 /* Find CODEC from registered CODECs */
1104 codec_dais = rtd->codec_dais;
1105 for (i = 0; i < rtd->num_codecs; i++) {
1106 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
1107 if (!codec_dais[i]) {
1108 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
1109 codecs[i].dai_name);
1112 snd_soc_rtdcom_add(rtd, codec_dais[i]->component);
1115 /* Single codec links expect codec and codec_dai in runtime data */
1116 rtd->codec_dai = codec_dais[0];
1117 rtd->codec = rtd->codec_dai->codec;
1119 /* if there's no platform we match on the empty platform */
1120 platform_name = dai_link->platform_name;
1121 if (!platform_name && !dai_link->platform_of_node)
1122 platform_name = "snd-soc-dummy";
1124 /* find one from the set of registered platforms */
1125 list_for_each_entry(component, &component_list, list) {
1126 platform_of_node = component->dev->of_node;
1127 if (!platform_of_node && component->dev->parent->of_node)
1128 platform_of_node = component->dev->parent->of_node;
1130 if (dai_link->platform_of_node) {
1131 if (platform_of_node != dai_link->platform_of_node)
1134 if (strcmp(component->name, platform_name))
1138 snd_soc_rtdcom_add(rtd, component);
1141 /* find one from the set of registered platforms */
1142 list_for_each_entry(platform, &platform_list, list) {
1143 platform_of_node = platform->dev->of_node;
1144 if (!platform_of_node && platform->dev->parent->of_node)
1145 platform_of_node = platform->dev->parent->of_node;
1147 if (dai_link->platform_of_node) {
1148 if (platform_of_node != dai_link->platform_of_node)
1151 if (strcmp(platform->component.name, platform_name))
1155 rtd->platform = platform;
1157 if (!rtd->platform) {
1158 dev_err(card->dev, "ASoC: platform %s not registered\n",
1159 dai_link->platform_name);
1163 soc_add_pcm_runtime(card, rtd);
1167 soc_free_pcm_runtime(rtd);
1168 return -EPROBE_DEFER;
1171 static void soc_remove_component(struct snd_soc_component *component)
1173 if (!component->card)
1176 list_del(&component->card_list);
1178 if (component->remove)
1179 component->remove(component);
1181 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1183 soc_cleanup_component_debugfs(component);
1184 component->card = NULL;
1185 module_put(component->dev->driver->owner);
1188 static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1192 if (dai && dai->probed &&
1193 dai->driver->remove_order == order) {
1194 if (dai->driver->remove) {
1195 err = dai->driver->remove(dai);
1198 "ASoC: failed to remove %s: %d\n",
1205 static void soc_remove_link_dais(struct snd_soc_card *card,
1206 struct snd_soc_pcm_runtime *rtd, int order)
1210 /* unregister the rtd device */
1211 if (rtd->dev_registered) {
1212 device_unregister(rtd->dev);
1213 rtd->dev_registered = 0;
1216 /* remove the CODEC DAI */
1217 for (i = 0; i < rtd->num_codecs; i++)
1218 soc_remove_dai(rtd->codec_dais[i], order);
1220 soc_remove_dai(rtd->cpu_dai, order);
1223 static void soc_remove_link_components(struct snd_soc_card *card,
1224 struct snd_soc_pcm_runtime *rtd, int order)
1226 struct snd_soc_component *component;
1227 struct snd_soc_rtdcom_list *rtdcom;
1229 for_each_rtdcom(rtd, rtdcom) {
1230 component = rtdcom->component;
1232 if (component->driver->remove_order == order)
1233 soc_remove_component(component);
1237 static void soc_remove_dai_links(struct snd_soc_card *card)
1240 struct snd_soc_pcm_runtime *rtd;
1241 struct snd_soc_dai_link *link, *_link;
1243 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1245 list_for_each_entry(rtd, &card->rtd_list, list)
1246 soc_remove_link_dais(card, rtd, order);
1249 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1251 list_for_each_entry(rtd, &card->rtd_list, list)
1252 soc_remove_link_components(card, rtd, order);
1255 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1256 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1257 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1260 list_del(&link->list);
1261 card->num_dai_links--;
1265 static int snd_soc_init_multicodec(struct snd_soc_card *card,
1266 struct snd_soc_dai_link *dai_link)
1268 /* Legacy codec/codec_dai link is a single entry in multicodec */
1269 if (dai_link->codec_name || dai_link->codec_of_node ||
1270 dai_link->codec_dai_name) {
1271 dai_link->num_codecs = 1;
1273 dai_link->codecs = devm_kzalloc(card->dev,
1274 sizeof(struct snd_soc_dai_link_component),
1276 if (!dai_link->codecs)
1279 dai_link->codecs[0].name = dai_link->codec_name;
1280 dai_link->codecs[0].of_node = dai_link->codec_of_node;
1281 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1284 if (!dai_link->codecs) {
1285 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1292 static int soc_init_dai_link(struct snd_soc_card *card,
1293 struct snd_soc_dai_link *link)
1297 ret = snd_soc_init_multicodec(card, link);
1299 dev_err(card->dev, "ASoC: failed to init multicodec\n");
1303 for (i = 0; i < link->num_codecs; i++) {
1305 * Codec must be specified by 1 of name or OF node,
1306 * not both or neither.
1308 if (!!link->codecs[i].name ==
1309 !!link->codecs[i].of_node) {
1310 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1314 /* Codec DAI name must be specified */
1315 if (!link->codecs[i].dai_name) {
1316 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1323 * Platform may be specified by either name or OF node, but
1324 * can be left unspecified, and a dummy platform will be used.
1326 if (link->platform_name && link->platform_of_node) {
1328 "ASoC: Both platform name/of_node are set for %s\n",
1334 * CPU device may be specified by either name or OF node, but
1335 * can be left unspecified, and will be matched based on DAI
1338 if (link->cpu_name && link->cpu_of_node) {
1340 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1345 * At least one of CPU DAI name or CPU device name/node must be
1348 if (!link->cpu_dai_name &&
1349 !(link->cpu_name || link->cpu_of_node)) {
1351 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1359 void snd_soc_disconnect_sync(struct device *dev)
1361 struct snd_soc_component *component = snd_soc_lookup_component(dev, NULL);
1363 if (!component || !component->card)
1366 snd_card_disconnect_sync(component->card->snd_card);
1368 EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
1371 * snd_soc_add_dai_link - Add a DAI link dynamically
1372 * @card: The ASoC card to which the DAI link is added
1373 * @dai_link: The new DAI link to add
1375 * This function adds a DAI link to the ASoC card's link list.
1377 * Note: Topology can use this API to add DAI links when probing the
1378 * topology component. And machine drivers can still define static
1379 * DAI links in dai_link array.
1381 int snd_soc_add_dai_link(struct snd_soc_card *card,
1382 struct snd_soc_dai_link *dai_link)
1384 if (dai_link->dobj.type
1385 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1386 dev_err(card->dev, "Invalid dai link type %d\n",
1387 dai_link->dobj.type);
1391 lockdep_assert_held(&client_mutex);
1392 /* Notify the machine driver for extra initialization
1393 * on the link created by topology.
1395 if (dai_link->dobj.type && card->add_dai_link)
1396 card->add_dai_link(card, dai_link);
1398 list_add_tail(&dai_link->list, &card->dai_link_list);
1399 card->num_dai_links++;
1403 EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1406 * snd_soc_remove_dai_link - Remove a DAI link from the list
1407 * @card: The ASoC card that owns the link
1408 * @dai_link: The DAI link to remove
1410 * This function removes a DAI link from the ASoC card's link list.
1412 * For DAI links previously added by topology, topology should
1413 * remove them by using the dobj embedded in the link.
1415 void snd_soc_remove_dai_link(struct snd_soc_card *card,
1416 struct snd_soc_dai_link *dai_link)
1418 struct snd_soc_dai_link *link, *_link;
1420 if (dai_link->dobj.type
1421 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1422 dev_err(card->dev, "Invalid dai link type %d\n",
1423 dai_link->dobj.type);
1427 lockdep_assert_held(&client_mutex);
1428 /* Notify the machine driver for extra destruction
1429 * on the link created by topology.
1431 if (dai_link->dobj.type && card->remove_dai_link)
1432 card->remove_dai_link(card, dai_link);
1434 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1435 if (link == dai_link) {
1436 list_del(&link->list);
1437 card->num_dai_links--;
1442 EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1444 static void soc_set_name_prefix(struct snd_soc_card *card,
1445 struct snd_soc_component *component)
1449 if (card->codec_conf == NULL)
1452 for (i = 0; i < card->num_configs; i++) {
1453 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1454 if (map->of_node && component->dev->of_node != map->of_node)
1456 if (map->dev_name && strcmp(component->name, map->dev_name))
1458 component->name_prefix = map->name_prefix;
1463 static int soc_probe_component(struct snd_soc_card *card,
1464 struct snd_soc_component *component)
1466 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1467 struct snd_soc_dai *dai;
1470 if (!strcmp(component->name, "snd-soc-dummy"))
1473 if (component->card) {
1474 if (component->card != card) {
1475 dev_err(component->dev,
1476 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1477 card->name, component->card->name);
1483 if (!try_module_get(component->dev->driver->owner))
1486 component->card = card;
1488 soc_set_name_prefix(card, component);
1490 soc_init_component_debugfs(component);
1492 if (component->driver->dapm_widgets) {
1493 ret = snd_soc_dapm_new_controls(dapm,
1494 component->driver->dapm_widgets,
1495 component->driver->num_dapm_widgets);
1498 dev_err(component->dev,
1499 "Failed to create new controls %d\n", ret);
1504 list_for_each_entry(dai, &component->dai_list, list) {
1505 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1507 dev_err(component->dev,
1508 "Failed to create DAI widgets %d\n", ret);
1513 if (component->probe) {
1514 ret = component->probe(component);
1516 dev_err(component->dev,
1517 "ASoC: failed to probe component %d\n", ret);
1521 WARN(dapm->idle_bias_off &&
1522 dapm->bias_level != SND_SOC_BIAS_OFF,
1523 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1527 /* machine specific init */
1528 if (component->init) {
1529 ret = component->init(component);
1531 dev_err(component->dev,
1532 "Failed to do machine specific init %d\n", ret);
1537 if (component->driver->controls)
1538 snd_soc_add_component_controls(component,
1539 component->driver->controls,
1540 component->driver->num_controls);
1541 if (component->driver->dapm_routes)
1542 snd_soc_dapm_add_routes(dapm,
1543 component->driver->dapm_routes,
1544 component->driver->num_dapm_routes);
1546 list_add(&dapm->list, &card->dapm_list);
1547 list_add(&component->card_list, &card->component_dev_list);
1552 soc_cleanup_component_debugfs(component);
1553 component->card = NULL;
1554 module_put(component->dev->driver->owner);
1559 static void rtd_release(struct device *dev)
1564 static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1569 /* register the rtd device */
1570 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1573 device_initialize(rtd->dev);
1574 rtd->dev->parent = rtd->card->dev;
1575 rtd->dev->release = rtd_release;
1576 rtd->dev->groups = soc_dev_attr_groups;
1577 dev_set_name(rtd->dev, "%s", name);
1578 dev_set_drvdata(rtd->dev, rtd);
1579 mutex_init(&rtd->pcm_mutex);
1580 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1581 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1582 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1583 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1584 ret = device_add(rtd->dev);
1586 /* calling put_device() here to free the rtd->dev */
1587 put_device(rtd->dev);
1588 dev_err(rtd->card->dev,
1589 "ASoC: failed to register runtime device: %d\n", ret);
1592 rtd->dev_registered = 1;
1596 static int soc_probe_link_components(struct snd_soc_card *card,
1597 struct snd_soc_pcm_runtime *rtd,
1600 struct snd_soc_component *component;
1601 struct snd_soc_rtdcom_list *rtdcom;
1604 for_each_rtdcom(rtd, rtdcom) {
1605 component = rtdcom->component;
1607 if (component->driver->probe_order == order) {
1608 ret = soc_probe_component(card, component);
1617 static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1621 if (!dai->probed && dai->driver->probe_order == order) {
1622 if (dai->driver->probe) {
1623 ret = dai->driver->probe(dai);
1626 "ASoC: failed to probe DAI %s: %d\n",
1638 static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1639 struct snd_soc_pcm_runtime *rtd)
1643 for (i = 0; i < num_dais; ++i) {
1644 struct snd_soc_dai_driver *drv = dais[i]->driver;
1646 if (!rtd->dai_link->no_pcm && drv->pcm_new)
1647 ret = drv->pcm_new(rtd, dais[i]);
1649 dev_err(dais[i]->dev,
1650 "ASoC: Failed to bind %s with pcm device\n",
1659 static int soc_link_dai_widgets(struct snd_soc_card *card,
1660 struct snd_soc_dai_link *dai_link,
1661 struct snd_soc_pcm_runtime *rtd)
1663 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1664 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1665 struct snd_soc_dapm_widget *sink, *source;
1668 if (rtd->num_codecs > 1)
1669 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1671 /* link the DAI widgets */
1672 sink = codec_dai->playback_widget;
1673 source = cpu_dai->capture_widget;
1674 if (sink && source) {
1675 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1676 dai_link->num_params,
1679 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1680 sink->name, source->name, ret);
1685 sink = cpu_dai->playback_widget;
1686 source = codec_dai->capture_widget;
1687 if (sink && source) {
1688 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1689 dai_link->num_params,
1692 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1693 sink->name, source->name, ret);
1701 static int soc_probe_link_dais(struct snd_soc_card *card,
1702 struct snd_soc_pcm_runtime *rtd, int order)
1704 struct snd_soc_dai_link *dai_link = rtd->dai_link;
1705 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1708 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1709 card->name, rtd->num, order);
1711 /* set default power off timeout */
1712 rtd->pmdown_time = pmdown_time;
1714 ret = soc_probe_dai(cpu_dai, order);
1718 /* probe the CODEC DAI */
1719 for (i = 0; i < rtd->num_codecs; i++) {
1720 ret = soc_probe_dai(rtd->codec_dais[i], order);
1725 /* complete DAI probe during last probe */
1726 if (order != SND_SOC_COMP_ORDER_LAST)
1729 /* do machine specific initialization */
1730 if (dai_link->init) {
1731 ret = dai_link->init(rtd);
1733 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1734 dai_link->name, ret);
1739 if (dai_link->dai_fmt)
1740 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1742 ret = soc_post_component_init(rtd, dai_link->name);
1746 #ifdef CONFIG_DEBUG_FS
1747 /* add DPCM sysfs entries */
1748 if (dai_link->dynamic)
1749 soc_dpcm_debugfs_add(rtd);
1752 if (cpu_dai->driver->compress_new) {
1753 /*create compress_device"*/
1754 ret = cpu_dai->driver->compress_new(rtd, rtd->num);
1756 dev_err(card->dev, "ASoC: can't create compress %s\n",
1757 dai_link->stream_name);
1762 if (!dai_link->params) {
1763 /* create the pcm */
1764 ret = soc_new_pcm(rtd, rtd->num);
1766 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1767 dai_link->stream_name, ret);
1770 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1773 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1774 rtd->num_codecs, rtd);
1778 INIT_DELAYED_WORK(&rtd->delayed_work,
1779 codec2codec_close_delayed_work);
1781 /* link the DAI widgets */
1782 ret = soc_link_dai_widgets(card, dai_link, rtd);
1791 static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1793 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1794 struct snd_soc_component *component;
1796 struct device_node *codec_of_node;
1798 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1799 /* codecs, usually analog devices */
1800 name = aux_dev->codec_name;
1801 codec_of_node = aux_dev->codec_of_node;
1802 component = soc_find_component(codec_of_node, name);
1805 name = of_node_full_name(codec_of_node);
1808 } else if (aux_dev->name) {
1809 /* generic components */
1810 name = aux_dev->name;
1811 component = soc_find_component(NULL, name);
1815 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1819 component->init = aux_dev->init;
1820 list_add(&component->card_aux_list, &card->aux_comp_list);
1825 dev_err(card->dev, "ASoC: %s not registered\n", name);
1826 return -EPROBE_DEFER;
1829 static int soc_probe_aux_devices(struct snd_soc_card *card)
1831 struct snd_soc_component *comp;
1835 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1837 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
1838 if (comp->driver->probe_order == order) {
1839 ret = soc_probe_component(card, comp);
1842 "ASoC: failed to probe aux component %s %d\n",
1853 static void soc_remove_aux_devices(struct snd_soc_card *card)
1855 struct snd_soc_component *comp, *_comp;
1858 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1860 list_for_each_entry_safe(comp, _comp,
1861 &card->aux_comp_list, card_aux_list) {
1863 if (comp->driver->remove_order == order) {
1864 soc_remove_component(comp);
1865 /* remove it from the card's aux_comp_list */
1866 list_del(&comp->card_aux_list);
1872 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1876 if (codec->cache_init)
1879 ret = snd_soc_cache_init(codec);
1882 "ASoC: Failed to set cache compression type: %d\n",
1886 codec->cache_init = 1;
1891 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1892 * @rtd: The runtime for which the DAI link format should be changed
1893 * @dai_fmt: The new DAI link format
1895 * This function updates the DAI link format for all DAIs connected to the DAI
1896 * link for the specified runtime.
1898 * Note: For setups with a static format set the dai_fmt field in the
1899 * corresponding snd_dai_link struct instead of using this function.
1901 * Returns 0 on success, otherwise a negative error code.
1903 int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1904 unsigned int dai_fmt)
1906 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1907 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1911 for (i = 0; i < rtd->num_codecs; i++) {
1912 struct snd_soc_dai *codec_dai = codec_dais[i];
1914 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1915 if (ret != 0 && ret != -ENOTSUPP) {
1916 dev_warn(codec_dai->dev,
1917 "ASoC: Failed to set DAI format: %d\n", ret);
1922 /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1923 /* the component which has non_legacy_dai_naming is Codec */
1924 if (cpu_dai->codec ||
1925 cpu_dai->component->driver->non_legacy_dai_naming) {
1926 unsigned int inv_dai_fmt;
1928 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1929 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1930 case SND_SOC_DAIFMT_CBM_CFM:
1931 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1933 case SND_SOC_DAIFMT_CBM_CFS:
1934 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1936 case SND_SOC_DAIFMT_CBS_CFM:
1937 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1939 case SND_SOC_DAIFMT_CBS_CFS:
1940 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1944 dai_fmt = inv_dai_fmt;
1947 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1948 if (ret != 0 && ret != -ENOTSUPP) {
1949 dev_warn(cpu_dai->dev,
1950 "ASoC: Failed to set DAI format: %d\n", ret);
1956 EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1960 /* Trim special characters, and replace '-' with '_' since '-' is used to
1961 * separate different DMI fields in the card long name. Only number and
1962 * alphabet characters and a few separator characters are kept.
1964 static void cleanup_dmi_name(char *name)
1968 for (i = 0; name[i]; i++) {
1969 if (isalnum(name[i]) || (name[i] == '.')
1970 || (name[i] == '_'))
1971 name[j++] = name[i];
1972 else if (name[i] == '-')
1979 /* Check if a DMI field is valid, i.e. not containing any string
1980 * in the black list.
1982 static int is_dmi_valid(const char *field)
1986 while (dmi_blacklist[i]) {
1987 if (strstr(field, dmi_blacklist[i]))
1996 * snd_soc_set_dmi_name() - Register DMI names to card
1997 * @card: The card to register DMI names
1998 * @flavour: The flavour "differentiator" for the card amongst its peers.
2000 * An Intel machine driver may be used by many different devices but are
2001 * difficult for userspace to differentiate, since machine drivers ususally
2002 * use their own name as the card short name and leave the card long name
2003 * blank. To differentiate such devices and fix bugs due to lack of
2004 * device-specific configurations, this function allows DMI info to be used
2005 * as the sound card long name, in the format of
2006 * "vendor-product-version-board"
2007 * (Character '-' is used to separate different DMI fields here).
2008 * This will help the user space to load the device-specific Use Case Manager
2009 * (UCM) configurations for the card.
2011 * Possible card long names may be:
2012 * DellInc.-XPS139343-01-0310JH
2013 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
2014 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
2016 * This function also supports flavoring the card longname to provide
2017 * the extra differentiation, like "vendor-product-version-board-flavor".
2019 * We only keep number and alphabet characters and a few separator characters
2020 * in the card long name since UCM in the user space uses the card long names
2021 * as card configuration directory names and AudoConf cannot support special
2022 * charactors like SPACE.
2024 * Returns 0 on success, otherwise a negative error code.
2026 int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
2028 const char *vendor, *product, *product_version, *board;
2029 size_t longname_buf_size = sizeof(card->snd_card->longname);
2032 if (card->long_name)
2033 return 0; /* long name already set by driver or from DMI */
2035 /* make up dmi long name as: vendor.product.version.board */
2036 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2037 if (!vendor || !is_dmi_valid(vendor)) {
2038 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
2043 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
2045 cleanup_dmi_name(card->dmi_longname);
2047 product = dmi_get_system_info(DMI_PRODUCT_NAME);
2048 if (product && is_dmi_valid(product)) {
2049 len = strlen(card->dmi_longname);
2050 snprintf(card->dmi_longname + len,
2051 longname_buf_size - len,
2054 len++; /* skip the separator "-" */
2055 if (len < longname_buf_size)
2056 cleanup_dmi_name(card->dmi_longname + len);
2058 /* some vendors like Lenovo may only put a self-explanatory
2059 * name in the product version field
2061 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
2062 if (product_version && is_dmi_valid(product_version)) {
2063 len = strlen(card->dmi_longname);
2064 snprintf(card->dmi_longname + len,
2065 longname_buf_size - len,
2066 "-%s", product_version);
2069 if (len < longname_buf_size)
2070 cleanup_dmi_name(card->dmi_longname + len);
2074 board = dmi_get_system_info(DMI_BOARD_NAME);
2075 if (board && is_dmi_valid(board)) {
2076 len = strlen(card->dmi_longname);
2077 snprintf(card->dmi_longname + len,
2078 longname_buf_size - len,
2082 if (len < longname_buf_size)
2083 cleanup_dmi_name(card->dmi_longname + len);
2084 } else if (!product) {
2085 /* fall back to using legacy name */
2086 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
2090 /* Add flavour to dmi long name */
2092 len = strlen(card->dmi_longname);
2093 snprintf(card->dmi_longname + len,
2094 longname_buf_size - len,
2098 if (len < longname_buf_size)
2099 cleanup_dmi_name(card->dmi_longname + len);
2102 /* set the card long name */
2103 card->long_name = card->dmi_longname;
2107 EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
2108 #endif /* CONFIG_DMI */
2110 static int snd_soc_instantiate_card(struct snd_soc_card *card)
2112 struct snd_soc_codec *codec;
2113 struct snd_soc_pcm_runtime *rtd;
2114 struct snd_soc_dai_link *dai_link;
2117 mutex_lock(&client_mutex);
2118 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
2121 for (i = 0; i < card->num_links; i++) {
2122 ret = soc_bind_dai_link(card, &card->dai_link[i]);
2127 /* bind aux_devs too */
2128 for (i = 0; i < card->num_aux_devs; i++) {
2129 ret = soc_bind_aux_dev(card, i);
2134 /* add predefined DAI links to the list */
2135 for (i = 0; i < card->num_links; i++)
2136 snd_soc_add_dai_link(card, card->dai_link+i);
2138 /* initialize the register cache for each available codec */
2139 list_for_each_entry(codec, &codec_list, list) {
2140 if (codec->cache_init)
2142 ret = snd_soc_init_codec_cache(codec);
2147 /* card bind complete so register a sound card */
2148 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2149 card->owner, 0, &card->snd_card);
2152 "ASoC: can't create sound card for card %s: %d\n",
2157 soc_init_card_debugfs(card);
2159 card->dapm.bias_level = SND_SOC_BIAS_OFF;
2160 card->dapm.dev = card->dev;
2161 card->dapm.card = card;
2162 list_add(&card->dapm.list, &card->dapm_list);
2164 #ifdef CONFIG_DEBUG_FS
2165 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2168 #ifdef CONFIG_PM_SLEEP
2169 /* deferred resume work */
2170 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
2173 if (card->dapm_widgets)
2174 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2175 card->num_dapm_widgets);
2177 if (card->of_dapm_widgets)
2178 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2179 card->num_of_dapm_widgets);
2181 /* initialise the sound card only once */
2183 ret = card->probe(card);
2185 goto card_probe_error;
2188 /* probe all components used by DAI links on this card */
2189 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2191 list_for_each_entry(rtd, &card->rtd_list, list) {
2192 ret = soc_probe_link_components(card, rtd, order);
2195 "ASoC: failed to instantiate card %d\n",
2202 /* probe auxiliary components */
2203 ret = soc_probe_aux_devices(card);
2207 /* Find new DAI links added during probing components and bind them.
2208 * Components with topology may bring new DAIs and DAI links.
2210 list_for_each_entry(dai_link, &card->dai_link_list, list) {
2211 if (soc_is_dai_link_bound(card, dai_link))
2214 ret = soc_init_dai_link(card, dai_link);
2217 ret = soc_bind_dai_link(card, dai_link);
2222 /* probe all DAI links on this card */
2223 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2225 list_for_each_entry(rtd, &card->rtd_list, list) {
2226 ret = soc_probe_link_dais(card, rtd, order);
2229 "ASoC: failed to instantiate card %d\n",
2236 snd_soc_dapm_link_dai_widgets(card);
2237 snd_soc_dapm_connect_dai_link_widgets(card);
2240 snd_soc_add_card_controls(card, card->controls, card->num_controls);
2242 if (card->dapm_routes)
2243 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2244 card->num_dapm_routes);
2246 if (card->of_dapm_routes)
2247 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2248 card->num_of_dapm_routes);
2250 /* try to set some sane longname if DMI is available */
2251 snd_soc_set_dmi_name(card, NULL);
2253 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
2255 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2256 "%s", card->long_name ? card->long_name : card->name);
2257 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2258 "%s", card->driver_name ? card->driver_name : card->name);
2259 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2260 switch (card->snd_card->driver[i]) {
2266 if (!isalnum(card->snd_card->driver[i]))
2267 card->snd_card->driver[i] = '_';
2272 if (card->late_probe) {
2273 ret = card->late_probe(card);
2275 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
2277 goto probe_aux_dev_err;
2281 snd_soc_dapm_new_widgets(card);
2283 ret = snd_card_register(card->snd_card);
2285 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2287 goto probe_aux_dev_err;
2290 card->instantiated = 1;
2291 snd_soc_dapm_sync(&card->dapm);
2292 mutex_unlock(&card->mutex);
2293 mutex_unlock(&client_mutex);
2298 soc_remove_aux_devices(card);
2301 soc_remove_dai_links(card);
2307 snd_soc_dapm_free(&card->dapm);
2308 soc_cleanup_card_debugfs(card);
2309 snd_card_free(card->snd_card);
2312 soc_remove_pcm_runtimes(card);
2313 mutex_unlock(&card->mutex);
2314 mutex_unlock(&client_mutex);
2319 /* probes a new socdev */
2320 static int soc_probe(struct platform_device *pdev)
2322 struct snd_soc_card *card = platform_get_drvdata(pdev);
2325 * no card, so machine driver should be registering card
2326 * we should not be here in that case so ret error
2331 dev_warn(&pdev->dev,
2332 "ASoC: machine %s should use snd_soc_register_card()\n",
2335 /* Bodge while we unpick instantiation */
2336 card->dev = &pdev->dev;
2338 return snd_soc_register_card(card);
2341 static int soc_cleanup_card_resources(struct snd_soc_card *card)
2343 struct snd_soc_pcm_runtime *rtd;
2345 /* make sure any delayed work runs */
2346 list_for_each_entry(rtd, &card->rtd_list, list)
2347 flush_delayed_work(&rtd->delayed_work);
2349 /* free the ALSA card at first; this syncs with pending operations */
2350 snd_card_free(card->snd_card);
2352 /* remove and free each DAI */
2353 soc_remove_dai_links(card);
2354 soc_remove_pcm_runtimes(card);
2356 /* remove auxiliary devices */
2357 soc_remove_aux_devices(card);
2359 snd_soc_dapm_free(&card->dapm);
2360 soc_cleanup_card_debugfs(card);
2362 /* remove the card */
2369 /* removes a socdev */
2370 static int soc_remove(struct platform_device *pdev)
2372 struct snd_soc_card *card = platform_get_drvdata(pdev);
2374 snd_soc_unregister_card(card);
2378 int snd_soc_poweroff(struct device *dev)
2380 struct snd_soc_card *card = dev_get_drvdata(dev);
2381 struct snd_soc_pcm_runtime *rtd;
2383 if (!card->instantiated)
2386 /* Flush out pmdown_time work - we actually do want to run it
2387 * now, we're shutting down so no imminent restart. */
2388 list_for_each_entry(rtd, &card->rtd_list, list)
2389 flush_delayed_work(&rtd->delayed_work);
2391 snd_soc_dapm_shutdown(card);
2393 /* deactivate pins to sleep state */
2394 list_for_each_entry(rtd, &card->rtd_list, list) {
2395 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2398 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2399 for (i = 0; i < rtd->num_codecs; i++) {
2400 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
2401 pinctrl_pm_select_sleep_state(codec_dai->dev);
2407 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2409 const struct dev_pm_ops snd_soc_pm_ops = {
2410 .suspend = snd_soc_suspend,
2411 .resume = snd_soc_resume,
2412 .freeze = snd_soc_suspend,
2413 .thaw = snd_soc_resume,
2414 .poweroff = snd_soc_poweroff,
2415 .restore = snd_soc_resume,
2417 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2419 /* ASoC platform driver */
2420 static struct platform_driver soc_driver = {
2422 .name = "soc-audio",
2423 .pm = &snd_soc_pm_ops,
2426 .remove = soc_remove,
2430 * snd_soc_cnew - create new control
2431 * @_template: control template
2432 * @data: control private data
2433 * @long_name: control long name
2434 * @prefix: control name prefix
2436 * Create a new mixer control from a template control.
2438 * Returns 0 for success, else error.
2440 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2441 void *data, const char *long_name,
2444 struct snd_kcontrol_new template;
2445 struct snd_kcontrol *kcontrol;
2448 memcpy(&template, _template, sizeof(template));
2452 long_name = template.name;
2455 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2459 template.name = name;
2461 template.name = long_name;
2464 kcontrol = snd_ctl_new1(&template, data);
2470 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2472 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2473 const struct snd_kcontrol_new *controls, int num_controls,
2474 const char *prefix, void *data)
2478 for (i = 0; i < num_controls; i++) {
2479 const struct snd_kcontrol_new *control = &controls[i];
2480 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2481 control->name, prefix));
2483 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2484 control->name, err);
2492 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2495 struct snd_card *card = soc_card->snd_card;
2496 struct snd_kcontrol *kctl;
2498 if (unlikely(!name))
2501 list_for_each_entry(kctl, &card->controls, list)
2502 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2506 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2509 * snd_soc_add_component_controls - Add an array of controls to a component.
2511 * @component: Component to add controls to
2512 * @controls: Array of controls to add
2513 * @num_controls: Number of elements in the array
2515 * Return: 0 for success, else error.
2517 int snd_soc_add_component_controls(struct snd_soc_component *component,
2518 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2520 struct snd_card *card = component->card->snd_card;
2522 return snd_soc_add_controls(card, component->dev, controls,
2523 num_controls, component->name_prefix, component);
2525 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2528 * snd_soc_add_codec_controls - add an array of controls to a codec.
2529 * Convenience function to add a list of controls. Many codecs were
2530 * duplicating this code.
2532 * @codec: codec to add controls to
2533 * @controls: array of controls to add
2534 * @num_controls: number of elements in the array
2536 * Return 0 for success, else error.
2538 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2539 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2541 return snd_soc_add_component_controls(&codec->component, controls,
2544 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2547 * snd_soc_add_platform_controls - add an array of controls to a platform.
2548 * Convenience function to add a list of controls.
2550 * @platform: platform to add controls to
2551 * @controls: array of controls to add
2552 * @num_controls: number of elements in the array
2554 * Return 0 for success, else error.
2556 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2557 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2559 return snd_soc_add_component_controls(&platform->component, controls,
2562 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2565 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2566 * Convenience function to add a list of controls.
2568 * @soc_card: SoC card to add controls to
2569 * @controls: array of controls to add
2570 * @num_controls: number of elements in the array
2572 * Return 0 for success, else error.
2574 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2575 const struct snd_kcontrol_new *controls, int num_controls)
2577 struct snd_card *card = soc_card->snd_card;
2579 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2582 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2585 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2586 * Convienience function to add a list of controls.
2588 * @dai: DAI to add controls to
2589 * @controls: array of controls to add
2590 * @num_controls: number of elements in the array
2592 * Return 0 for success, else error.
2594 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2595 const struct snd_kcontrol_new *controls, int num_controls)
2597 struct snd_card *card = dai->component->card->snd_card;
2599 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2602 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2605 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2607 * @clk_id: DAI specific clock ID
2608 * @freq: new clock frequency in Hz
2609 * @dir: new clock direction - input/output.
2611 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2613 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2614 unsigned int freq, int dir)
2616 if (dai->driver->ops->set_sysclk)
2617 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2619 return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2622 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2625 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2627 * @clk_id: DAI specific clock ID
2628 * @source: Source for the clock
2629 * @freq: new clock frequency in Hz
2630 * @dir: new clock direction - input/output.
2632 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2634 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2635 int source, unsigned int freq, int dir)
2637 if (codec->driver->set_sysclk)
2638 return codec->driver->set_sysclk(codec, clk_id, source,
2643 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2646 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2647 * @component: COMPONENT
2648 * @clk_id: DAI specific clock ID
2649 * @source: Source for the clock
2650 * @freq: new clock frequency in Hz
2651 * @dir: new clock direction - input/output.
2653 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2655 int snd_soc_component_set_sysclk(struct snd_soc_component *component, int clk_id,
2656 int source, unsigned int freq, int dir)
2658 /* will be removed */
2659 if (component->set_sysclk)
2660 return component->set_sysclk(component, clk_id, source,
2663 if (component->driver->set_sysclk)
2664 return component->driver->set_sysclk(component, clk_id, source,
2669 EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2672 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2674 * @div_id: DAI specific clock divider ID
2675 * @div: new clock divisor.
2677 * Configures the clock dividers. This is used to derive the best DAI bit and
2678 * frame clocks from the system or master clock. It's best to set the DAI bit
2679 * and frame clocks as low as possible to save system power.
2681 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2682 int div_id, int div)
2684 if (dai->driver->ops->set_clkdiv)
2685 return dai->driver->ops->set_clkdiv(dai, div_id, div);
2689 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2692 * snd_soc_dai_set_pll - configure DAI PLL.
2694 * @pll_id: DAI specific PLL ID
2695 * @source: DAI specific source for the PLL
2696 * @freq_in: PLL input clock frequency in Hz
2697 * @freq_out: requested PLL output clock frequency in Hz
2699 * Configures and enables PLL to generate output clock based on input clock.
2701 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2702 unsigned int freq_in, unsigned int freq_out)
2704 if (dai->driver->ops->set_pll)
2705 return dai->driver->ops->set_pll(dai, pll_id, source,
2708 return snd_soc_component_set_pll(dai->component, pll_id, source,
2711 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2714 * snd_soc_codec_set_pll - configure codec PLL.
2716 * @pll_id: DAI specific PLL ID
2717 * @source: DAI specific source for the PLL
2718 * @freq_in: PLL input clock frequency in Hz
2719 * @freq_out: requested PLL output clock frequency in Hz
2721 * Configures and enables PLL to generate output clock based on input clock.
2723 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2724 unsigned int freq_in, unsigned int freq_out)
2726 if (codec->driver->set_pll)
2727 return codec->driver->set_pll(codec, pll_id, source,
2732 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2735 * snd_soc_component_set_pll - configure component PLL.
2736 * @component: COMPONENT
2737 * @pll_id: DAI specific PLL ID
2738 * @source: DAI specific source for the PLL
2739 * @freq_in: PLL input clock frequency in Hz
2740 * @freq_out: requested PLL output clock frequency in Hz
2742 * Configures and enables PLL to generate output clock based on input clock.
2744 int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2745 int source, unsigned int freq_in,
2746 unsigned int freq_out)
2748 /* will be removed */
2749 if (component->set_pll)
2750 return component->set_pll(component, pll_id, source,
2753 if (component->driver->set_pll)
2754 return component->driver->set_pll(component, pll_id, source,
2759 EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2762 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2764 * @ratio: Ratio of BCLK to Sample rate.
2766 * Configures the DAI for a preset BCLK to sample rate ratio.
2768 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2770 if (dai->driver->ops->set_bclk_ratio)
2771 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2775 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2778 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2780 * @fmt: SND_SOC_DAIFMT_* format value.
2782 * Configures the DAI hardware format and clocking.
2784 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2786 if (dai->driver == NULL)
2788 if (dai->driver->ops->set_fmt == NULL)
2790 return dai->driver->ops->set_fmt(dai, fmt);
2792 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2795 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
2796 * @slots: Number of slots in use.
2797 * @tx_mask: bitmask representing active TX slots.
2798 * @rx_mask: bitmask representing active RX slots.
2800 * Generates the TDM tx and rx slot default masks for DAI.
2802 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
2803 unsigned int *tx_mask,
2804 unsigned int *rx_mask)
2806 if (*tx_mask || *rx_mask)
2812 *tx_mask = (1 << slots) - 1;
2813 *rx_mask = (1 << slots) - 1;
2819 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2820 * @dai: The DAI to configure
2821 * @tx_mask: bitmask representing active TX slots.
2822 * @rx_mask: bitmask representing active RX slots.
2823 * @slots: Number of slots in use.
2824 * @slot_width: Width in bits for each slot.
2826 * This function configures the specified DAI for TDM operation. @slot contains
2827 * the total number of slots of the TDM stream and @slot_with the width of each
2828 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2829 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2830 * DAI should write to or read from. If a bit is set the corresponding slot is
2831 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2832 * the first slot, bit 1 to the second slot and so on. The first active slot
2833 * maps to the first channel of the DAI, the second active slot to the second
2834 * channel and so on.
2836 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2837 * @rx_mask and @slot_width will be ignored.
2839 * Returns 0 on success, a negative error code otherwise.
2841 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2842 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2844 if (dai->driver->ops->xlate_tdm_slot_mask)
2845 dai->driver->ops->xlate_tdm_slot_mask(slots,
2846 &tx_mask, &rx_mask);
2848 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
2850 dai->tx_mask = tx_mask;
2851 dai->rx_mask = rx_mask;
2853 if (dai->driver->ops->set_tdm_slot)
2854 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2859 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2862 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2864 * @tx_num: how many TX channels
2865 * @tx_slot: pointer to an array which imply the TX slot number channel
2867 * @rx_num: how many RX channels
2868 * @rx_slot: pointer to an array which imply the RX slot number channel
2871 * configure the relationship between channel number and TDM slot number.
2873 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2874 unsigned int tx_num, unsigned int *tx_slot,
2875 unsigned int rx_num, unsigned int *rx_slot)
2877 if (dai->driver->ops->set_channel_map)
2878 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2883 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2886 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2888 * @tristate: tristate enable
2890 * Tristates the DAI so that others can use it.
2892 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2894 if (dai->driver->ops->set_tristate)
2895 return dai->driver->ops->set_tristate(dai, tristate);
2899 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2902 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2904 * @mute: mute enable
2905 * @direction: stream to mute
2907 * Mutes the DAI DAC.
2909 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2915 if (dai->driver->ops->mute_stream)
2916 return dai->driver->ops->mute_stream(dai, mute, direction);
2917 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2918 dai->driver->ops->digital_mute)
2919 return dai->driver->ops->digital_mute(dai, mute);
2923 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2926 * snd_soc_register_card - Register a card with the ASoC core
2928 * @card: Card to register
2931 int snd_soc_register_card(struct snd_soc_card *card)
2934 struct snd_soc_pcm_runtime *rtd;
2936 if (!card->name || !card->dev)
2939 for (i = 0; i < card->num_links; i++) {
2940 struct snd_soc_dai_link *link = &card->dai_link[i];
2942 ret = soc_init_dai_link(card, link);
2944 dev_err(card->dev, "ASoC: failed to init link %s\n",
2950 dev_set_drvdata(card->dev, card);
2952 snd_soc_initialize_card_lists(card);
2954 INIT_LIST_HEAD(&card->dai_link_list);
2955 card->num_dai_links = 0;
2957 INIT_LIST_HEAD(&card->rtd_list);
2960 INIT_LIST_HEAD(&card->dapm_dirty);
2961 INIT_LIST_HEAD(&card->dobj_list);
2962 card->instantiated = 0;
2963 mutex_init(&card->mutex);
2964 mutex_init(&card->dapm_mutex);
2966 ret = snd_soc_instantiate_card(card);
2970 /* deactivate pins to sleep state */
2971 list_for_each_entry(rtd, &card->rtd_list, list) {
2972 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2975 for (j = 0; j < rtd->num_codecs; j++) {
2976 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2977 if (!codec_dai->active)
2978 pinctrl_pm_select_sleep_state(codec_dai->dev);
2981 if (!cpu_dai->active)
2982 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2987 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2990 * snd_soc_unregister_card - Unregister a card with the ASoC core
2992 * @card: Card to unregister
2995 int snd_soc_unregister_card(struct snd_soc_card *card)
2997 if (card->instantiated) {
2998 card->instantiated = false;
2999 snd_soc_dapm_shutdown(card);
3000 soc_cleanup_card_resources(card);
3001 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
3006 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3009 * Simplify DAI link configuration by removing ".-1" from device names
3010 * and sanitizing names.
3012 static char *fmt_single_name(struct device *dev, int *id)
3014 char *found, name[NAME_SIZE];
3017 if (dev_name(dev) == NULL)
3020 strlcpy(name, dev_name(dev), NAME_SIZE);
3022 /* are we a "%s.%d" name (platform and SPI components) */
3023 found = strstr(name, dev->driver->name);
3026 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3028 /* discard ID from name if ID == -1 */
3030 found[strlen(dev->driver->name)] = '\0';
3034 /* I2C component devices are named "bus-addr" */
3035 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3036 char tmp[NAME_SIZE];
3038 /* create unique ID number from I2C addr and bus */
3039 *id = ((id1 & 0xffff) << 16) + id2;
3041 /* sanitize component name for DAI link creation */
3042 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3043 strlcpy(name, tmp, NAME_SIZE);
3048 return kstrdup(name, GFP_KERNEL);
3052 * Simplify DAI link naming for single devices with multiple DAIs by removing
3053 * any ".-1" and using the DAI name (instead of device name).
3055 static inline char *fmt_multiple_name(struct device *dev,
3056 struct snd_soc_dai_driver *dai_drv)
3058 if (dai_drv->name == NULL) {
3060 "ASoC: error - multiple DAI %s registered with no name\n",
3065 return kstrdup(dai_drv->name, GFP_KERNEL);
3069 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
3071 * @component: The component for which the DAIs should be unregistered
3073 static void snd_soc_unregister_dais(struct snd_soc_component *component)
3075 struct snd_soc_dai *dai, *_dai;
3077 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
3078 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3080 list_del(&dai->list);
3086 /* Create a DAI and add it to the component's DAI list */
3087 static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
3088 struct snd_soc_dai_driver *dai_drv,
3089 bool legacy_dai_naming)
3091 struct device *dev = component->dev;
3092 struct snd_soc_dai *dai;
3094 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
3096 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3101 * Back in the old days when we still had component-less DAIs,
3102 * instead of having a static name, component-less DAIs would
3103 * inherit the name of the parent device so it is possible to
3104 * register multiple instances of the DAI. We still need to keep
3105 * the same naming style even though those DAIs are not
3106 * component-less anymore.
3108 if (legacy_dai_naming &&
3109 (dai_drv->id == 0 || dai_drv->name == NULL)) {
3110 dai->name = fmt_single_name(dev, &dai->id);
3112 dai->name = fmt_multiple_name(dev, dai_drv);
3114 dai->id = dai_drv->id;
3116 dai->id = component->num_dai;
3118 if (dai->name == NULL) {
3123 dai->component = component;
3125 dai->driver = dai_drv;
3126 if (!dai->driver->ops)
3127 dai->driver->ops = &null_dai_ops;
3129 list_add_tail(&dai->list, &component->dai_list);
3130 component->num_dai++;
3132 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3137 * snd_soc_register_dais - Register a DAI with the ASoC core
3139 * @component: The component the DAIs are registered for
3140 * @dai_drv: DAI driver to use for the DAIs
3141 * @count: Number of DAIs
3142 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3145 static int snd_soc_register_dais(struct snd_soc_component *component,
3146 struct snd_soc_dai_driver *dai_drv, size_t count,
3147 bool legacy_dai_naming)
3149 struct device *dev = component->dev;
3150 struct snd_soc_dai *dai;
3154 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
3156 for (i = 0; i < count; i++) {
3158 dai = soc_add_dai(component, dai_drv + i,
3159 count == 1 && legacy_dai_naming);
3169 snd_soc_unregister_dais(component);
3175 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
3177 * @component: The component the DAIs are registered for
3178 * @dai_drv: DAI driver to use for the DAI
3180 * Topology can use this API to register DAIs when probing a component.
3181 * These DAIs's widgets will be freed in the card cleanup and the DAIs
3182 * will be freed in the component cleanup.
3184 int snd_soc_register_dai(struct snd_soc_component *component,
3185 struct snd_soc_dai_driver *dai_drv)
3187 struct snd_soc_dapm_context *dapm =
3188 snd_soc_component_get_dapm(component);
3189 struct snd_soc_dai *dai;
3192 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
3193 dev_err(component->dev, "Invalid dai type %d\n",
3194 dai_drv->dobj.type);
3198 lockdep_assert_held(&client_mutex);
3199 dai = soc_add_dai(component, dai_drv, false);
3203 /* Create the DAI widgets here. After adding DAIs, topology may
3204 * also add routes that need these widgets as source or sink.
3206 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
3208 dev_err(component->dev,
3209 "Failed to create DAI widgets %d\n", ret);
3214 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3216 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3217 enum snd_soc_dapm_type type, int subseq)
3219 struct snd_soc_component *component = dapm->component;
3221 component->driver->seq_notifier(component, type, subseq);
3224 static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3227 struct snd_soc_component *component = dapm->component;
3229 return component->driver->stream_event(component, event);
3232 static int snd_soc_component_drv_pcm_new(struct snd_soc_component *component,
3233 struct snd_soc_pcm_runtime *rtd)
3235 if (component->driver->pcm_new)
3236 return component->driver->pcm_new(rtd);
3241 static void snd_soc_component_drv_pcm_free(struct snd_soc_component *component,
3242 struct snd_pcm *pcm)
3244 if (component->driver->pcm_free)
3245 component->driver->pcm_free(pcm);
3248 static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
3249 enum snd_soc_bias_level level)
3251 struct snd_soc_component *component = dapm->component;
3253 return component->driver->set_bias_level(component, level);
3256 static int snd_soc_component_initialize(struct snd_soc_component *component,
3257 const struct snd_soc_component_driver *driver, struct device *dev)
3259 struct snd_soc_dapm_context *dapm;
3261 component->name = fmt_single_name(dev, &component->id);
3262 if (!component->name) {
3263 dev_err(dev, "ASoC: Failed to allocate name\n");
3267 component->dev = dev;
3268 component->driver = driver;
3269 component->probe = component->driver->probe;
3270 component->remove = component->driver->remove;
3271 component->suspend = component->driver->suspend;
3272 component->resume = component->driver->resume;
3273 component->set_sysclk = component->driver->set_sysclk;
3274 component->set_pll = component->driver->set_pll;
3275 component->set_jack = component->driver->set_jack;
3276 component->pcm_new = snd_soc_component_drv_pcm_new;
3277 component->pcm_free = snd_soc_component_drv_pcm_free;
3279 dapm = snd_soc_component_get_dapm(component);
3281 dapm->component = component;
3282 dapm->bias_level = SND_SOC_BIAS_OFF;
3283 dapm->idle_bias_off = !driver->idle_bias_on;
3284 dapm->suspend_bias_off = driver->suspend_bias_off;
3285 if (driver->seq_notifier)
3286 dapm->seq_notifier = snd_soc_component_seq_notifier;
3287 if (driver->stream_event)
3288 dapm->stream_event = snd_soc_component_stream_event;
3289 if (driver->set_bias_level)
3290 dapm->set_bias_level = snd_soc_component_set_bias_level;
3292 INIT_LIST_HEAD(&component->dai_list);
3293 mutex_init(&component->io_mutex);
3298 static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
3300 int val_bytes = regmap_get_val_bytes(component->regmap);
3302 /* Errors are legitimate for non-integer byte multiples */
3304 component->val_bytes = val_bytes;
3307 #ifdef CONFIG_REGMAP
3310 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
3311 * @component: The component for which to initialize the regmap instance
3312 * @regmap: The regmap instance that should be used by the component
3314 * This function allows deferred assignment of the regmap instance that is
3315 * associated with the component. Only use this if the regmap instance is not
3316 * yet ready when the component is registered. The function must also be called
3317 * before the first IO attempt of the component.
3319 void snd_soc_component_init_regmap(struct snd_soc_component *component,
3320 struct regmap *regmap)
3322 component->regmap = regmap;
3323 snd_soc_component_setup_regmap(component);
3325 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3328 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
3329 * @component: The component for which to de-initialize the regmap instance
3331 * Calls regmap_exit() on the regmap instance associated to the component and
3332 * removes the regmap instance from the component.
3334 * This function should only be used if snd_soc_component_init_regmap() was used
3335 * to initialize the regmap instance.
3337 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3339 regmap_exit(component->regmap);
3340 component->regmap = NULL;
3342 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3346 static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
3348 if (!component->write && !component->read) {
3349 if (!component->regmap)
3350 component->regmap = dev_get_regmap(component->dev, NULL);
3351 if (component->regmap)
3352 snd_soc_component_setup_regmap(component);
3355 list_add(&component->list, &component_list);
3356 INIT_LIST_HEAD(&component->dobj_list);
3359 static void snd_soc_component_add(struct snd_soc_component *component)
3361 mutex_lock(&client_mutex);
3362 snd_soc_component_add_unlocked(component);
3363 mutex_unlock(&client_mutex);
3366 static void snd_soc_component_cleanup(struct snd_soc_component *component)
3368 snd_soc_unregister_dais(component);
3369 kfree(component->name);
3372 static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3374 struct snd_soc_card *card = component->card;
3377 snd_soc_unregister_card(card);
3379 list_del(&component->list);
3382 #define ENDIANNESS_MAP(name) \
3383 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3384 static u64 endianness_format_map[] = {
3385 ENDIANNESS_MAP(S16_),
3386 ENDIANNESS_MAP(U16_),
3387 ENDIANNESS_MAP(S24_),
3388 ENDIANNESS_MAP(U24_),
3389 ENDIANNESS_MAP(S32_),
3390 ENDIANNESS_MAP(U32_),
3391 ENDIANNESS_MAP(S24_3),
3392 ENDIANNESS_MAP(U24_3),
3393 ENDIANNESS_MAP(S20_3),
3394 ENDIANNESS_MAP(U20_3),
3395 ENDIANNESS_MAP(S18_3),
3396 ENDIANNESS_MAP(U18_3),
3397 ENDIANNESS_MAP(FLOAT_),
3398 ENDIANNESS_MAP(FLOAT64_),
3399 ENDIANNESS_MAP(IEC958_SUBFRAME_),
3403 * Fix up the DAI formats for endianness: codecs don't actually see
3404 * the endianness of the data but we're using the CPU format
3405 * definitions which do need to include endianness so we ensure that
3406 * codec DAIs always have both big and little endian variants set.
3408 static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3412 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3413 if (stream->formats & endianness_format_map[i])
3414 stream->formats |= endianness_format_map[i];
3417 int snd_soc_add_component(struct device *dev,
3418 struct snd_soc_component *component,
3419 const struct snd_soc_component_driver *component_driver,
3420 struct snd_soc_dai_driver *dai_drv,
3426 ret = snd_soc_component_initialize(component, component_driver, dev);
3430 component->ignore_pmdown_time = true;
3431 component->registered_as_component = true;
3433 if (component_driver->endianness) {
3434 for (i = 0; i < num_dai; i++) {
3435 convert_endianness_formats(&dai_drv[i].playback);
3436 convert_endianness_formats(&dai_drv[i].capture);
3440 ret = snd_soc_register_dais(component, dai_drv, num_dai,
3441 !component_driver->non_legacy_dai_naming);
3443 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3447 snd_soc_component_add(component);
3452 snd_soc_component_cleanup(component);
3457 EXPORT_SYMBOL_GPL(snd_soc_add_component);
3459 int snd_soc_register_component(struct device *dev,
3460 const struct snd_soc_component_driver *component_driver,
3461 struct snd_soc_dai_driver *dai_drv,
3464 struct snd_soc_component *component;
3466 component = kzalloc(sizeof(*component), GFP_KERNEL);
3470 return snd_soc_add_component(dev, component, component_driver,
3473 EXPORT_SYMBOL_GPL(snd_soc_register_component);
3476 * snd_soc_unregister_component - Unregister all related component
3477 * from the ASoC core
3479 * @dev: The device to unregister
3481 static int __snd_soc_unregister_component(struct device *dev)
3483 struct snd_soc_component *component;
3486 mutex_lock(&client_mutex);
3487 list_for_each_entry(component, &component_list, list) {
3488 if (dev != component->dev ||
3489 !component->registered_as_component)
3492 snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);
3493 snd_soc_component_del_unlocked(component);
3497 mutex_unlock(&client_mutex);
3500 snd_soc_component_cleanup(component);
3507 void snd_soc_unregister_component(struct device *dev)
3509 while (__snd_soc_unregister_component(dev));
3511 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3513 struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
3514 const char *driver_name)
3516 struct snd_soc_component *component;
3517 struct snd_soc_component *ret;
3520 mutex_lock(&client_mutex);
3521 list_for_each_entry(component, &component_list, list) {
3522 if (dev != component->dev)
3526 (driver_name != component->driver->name) &&
3527 (strcmp(component->driver->name, driver_name) != 0))
3533 mutex_unlock(&client_mutex);
3537 EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
3539 static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
3541 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3543 return platform->driver->probe(platform);
3546 static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
3548 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3550 platform->driver->remove(platform);
3553 static int snd_soc_platform_drv_pcm_new(struct snd_soc_component *component,
3554 struct snd_soc_pcm_runtime *rtd)
3556 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3558 if (platform->driver->pcm_new)
3559 return platform->driver->pcm_new(rtd);
3564 static void snd_soc_platform_drv_pcm_free(struct snd_soc_component *component,
3565 struct snd_pcm *pcm)
3567 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3569 if (platform->driver->pcm_free)
3570 platform->driver->pcm_free(pcm);
3574 * snd_soc_add_platform - Add a platform to the ASoC core
3575 * @dev: The parent device for the platform
3576 * @platform: The platform to add
3577 * @platform_drv: The driver for the platform
3579 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
3580 const struct snd_soc_platform_driver *platform_drv)
3584 ret = snd_soc_component_initialize(&platform->component,
3585 &platform_drv->component_driver, dev);
3589 platform->dev = dev;
3590 platform->driver = platform_drv;
3592 if (platform_drv->probe)
3593 platform->component.probe = snd_soc_platform_drv_probe;
3594 if (platform_drv->remove)
3595 platform->component.remove = snd_soc_platform_drv_remove;
3596 if (platform_drv->pcm_new)
3597 platform->component.pcm_new = snd_soc_platform_drv_pcm_new;
3598 if (platform_drv->pcm_free)
3599 platform->component.pcm_free = snd_soc_platform_drv_pcm_free;
3601 #ifdef CONFIG_DEBUG_FS
3602 platform->component.debugfs_prefix = "platform";
3605 mutex_lock(&client_mutex);
3606 snd_soc_component_add_unlocked(&platform->component);
3607 list_add(&platform->list, &platform_list);
3608 mutex_unlock(&client_mutex);
3610 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
3611 platform->component.name);
3615 EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3618 * snd_soc_register_platform - Register a platform with the ASoC core
3620 * @dev: The device for the platform
3621 * @platform_drv: The driver for the platform
3623 int snd_soc_register_platform(struct device *dev,
3624 const struct snd_soc_platform_driver *platform_drv)
3626 struct snd_soc_platform *platform;
3629 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3631 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3632 if (platform == NULL)
3635 ret = snd_soc_add_platform(dev, platform, platform_drv);
3641 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3644 * snd_soc_remove_platform - Remove a platform from the ASoC core
3645 * @platform: the platform to remove
3647 void snd_soc_remove_platform(struct snd_soc_platform *platform)
3650 mutex_lock(&client_mutex);
3651 list_del(&platform->list);
3652 snd_soc_component_del_unlocked(&platform->component);
3653 mutex_unlock(&client_mutex);
3655 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3656 platform->component.name);
3658 snd_soc_component_cleanup(&platform->component);
3660 EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
3662 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
3664 struct snd_soc_platform *platform;
3666 mutex_lock(&client_mutex);
3667 list_for_each_entry(platform, &platform_list, list) {
3668 if (dev == platform->dev) {
3669 mutex_unlock(&client_mutex);
3673 mutex_unlock(&client_mutex);
3677 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
3680 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3682 * @dev: platform to unregister
3684 void snd_soc_unregister_platform(struct device *dev)
3686 struct snd_soc_platform *platform;
3688 platform = snd_soc_lookup_platform(dev);
3692 snd_soc_remove_platform(platform);
3695 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3697 static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3699 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3701 return codec->driver->probe(codec);
3704 static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3706 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3708 codec->driver->remove(codec);
3711 static int snd_soc_codec_drv_suspend(struct snd_soc_component *component)
3713 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3715 return codec->driver->suspend(codec);
3718 static int snd_soc_codec_drv_resume(struct snd_soc_component *component)
3720 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3722 return codec->driver->resume(codec);
3725 static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3726 unsigned int reg, unsigned int val)
3728 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3730 return codec->driver->write(codec, reg, val);
3733 static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3734 unsigned int reg, unsigned int *val)
3736 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3738 *val = codec->driver->read(codec, reg);
3743 static int snd_soc_codec_set_sysclk_(struct snd_soc_component *component,
3744 int clk_id, int source, unsigned int freq, int dir)
3746 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3748 return snd_soc_codec_set_sysclk(codec, clk_id, source, freq, dir);
3751 static int snd_soc_codec_set_pll_(struct snd_soc_component *component,
3752 int pll_id, int source, unsigned int freq_in,
3753 unsigned int freq_out)
3755 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3757 return snd_soc_codec_set_pll(codec, pll_id, source, freq_in, freq_out);
3760 static int snd_soc_codec_set_jack_(struct snd_soc_component *component,
3761 struct snd_soc_jack *jack, void *data)
3763 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3765 return snd_soc_codec_set_jack(codec, jack, data);
3768 static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
3769 enum snd_soc_bias_level level)
3771 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
3773 return codec->driver->set_bias_level(codec, level);
3777 * snd_soc_register_codec - Register a codec with the ASoC core
3779 * @dev: The parent device for this codec
3780 * @codec_drv: Codec driver
3781 * @dai_drv: The associated DAI driver
3782 * @num_dai: Number of DAIs
3784 int snd_soc_register_codec(struct device *dev,
3785 const struct snd_soc_codec_driver *codec_drv,
3786 struct snd_soc_dai_driver *dai_drv,
3789 struct snd_soc_dapm_context *dapm;
3790 struct snd_soc_codec *codec;
3791 struct snd_soc_dai *dai;
3794 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3796 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3800 codec->component.codec = codec;
3802 ret = snd_soc_component_initialize(&codec->component,
3803 &codec_drv->component_driver, dev);
3807 if (codec_drv->probe)
3808 codec->component.probe = snd_soc_codec_drv_probe;
3809 if (codec_drv->remove)
3810 codec->component.remove = snd_soc_codec_drv_remove;
3811 if (codec_drv->suspend)
3812 codec->component.suspend = snd_soc_codec_drv_suspend;
3813 if (codec_drv->resume)
3814 codec->component.resume = snd_soc_codec_drv_resume;
3815 if (codec_drv->write)
3816 codec->component.write = snd_soc_codec_drv_write;
3817 if (codec_drv->read)
3818 codec->component.read = snd_soc_codec_drv_read;
3819 if (codec_drv->set_sysclk)
3820 codec->component.set_sysclk = snd_soc_codec_set_sysclk_;
3821 if (codec_drv->set_pll)
3822 codec->component.set_pll = snd_soc_codec_set_pll_;
3823 if (codec_drv->set_jack)
3824 codec->component.set_jack = snd_soc_codec_set_jack_;
3825 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
3827 dapm = snd_soc_codec_get_dapm(codec);
3828 dapm->idle_bias_off = codec_drv->idle_bias_off;
3829 dapm->suspend_bias_off = codec_drv->suspend_bias_off;
3830 if (codec_drv->seq_notifier)
3831 dapm->seq_notifier = codec_drv->seq_notifier;
3832 if (codec_drv->set_bias_level)
3833 dapm->set_bias_level = snd_soc_codec_set_bias_level;
3835 codec->driver = codec_drv;
3836 codec->component.val_bytes = codec_drv->reg_word_size;
3838 #ifdef CONFIG_DEBUG_FS
3839 codec->component.init_debugfs = soc_init_codec_debugfs;
3840 codec->component.debugfs_prefix = "codec";
3843 if (codec_drv->get_regmap)
3844 codec->component.regmap = codec_drv->get_regmap(dev);
3846 for (i = 0; i < num_dai; i++) {
3847 convert_endianness_formats(&dai_drv[i].playback);
3848 convert_endianness_formats(&dai_drv[i].capture);
3851 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3853 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3857 list_for_each_entry(dai, &codec->component.dai_list, list)
3860 mutex_lock(&client_mutex);
3861 snd_soc_component_add_unlocked(&codec->component);
3862 list_add(&codec->list, &codec_list);
3863 mutex_unlock(&client_mutex);
3865 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3866 codec->component.name);
3870 snd_soc_component_cleanup(&codec->component);
3875 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3878 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3880 * @dev: codec to unregister
3882 void snd_soc_unregister_codec(struct device *dev)
3884 struct snd_soc_codec *codec;
3886 mutex_lock(&client_mutex);
3887 list_for_each_entry(codec, &codec_list, list) {
3888 if (dev == codec->dev)
3891 mutex_unlock(&client_mutex);
3895 list_del(&codec->list);
3896 snd_soc_component_del_unlocked(&codec->component);
3897 mutex_unlock(&client_mutex);
3899 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3900 codec->component.name);
3902 snd_soc_component_cleanup(&codec->component);
3903 snd_soc_cache_exit(codec);
3906 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3908 /* Retrieve a card's name from device tree */
3909 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3910 const char *propname)
3912 struct device_node *np;
3916 pr_err("card->dev is not set before calling %s\n", __func__);
3920 np = card->dev->of_node;
3922 ret = of_property_read_string_index(np, propname, 0, &card->name);
3924 * EINVAL means the property does not exist. This is fine providing
3925 * card->name was previously set, which is checked later in
3926 * snd_soc_register_card.
3928 if (ret < 0 && ret != -EINVAL) {
3930 "ASoC: Property '%s' could not be read: %d\n",
3937 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3939 static const struct snd_soc_dapm_widget simple_widgets[] = {
3940 SND_SOC_DAPM_MIC("Microphone", NULL),
3941 SND_SOC_DAPM_LINE("Line", NULL),
3942 SND_SOC_DAPM_HP("Headphone", NULL),
3943 SND_SOC_DAPM_SPK("Speaker", NULL),
3946 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
3947 const char *propname)
3949 struct device_node *np = card->dev->of_node;
3950 struct snd_soc_dapm_widget *widgets;
3951 const char *template, *wname;
3952 int i, j, num_widgets, ret;
3954 num_widgets = of_property_count_strings(np, propname);
3955 if (num_widgets < 0) {
3957 "ASoC: Property '%s' does not exist\n", propname);
3960 if (num_widgets & 1) {
3962 "ASoC: Property '%s' length is not even\n", propname);
3968 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3973 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3977 "ASoC: Could not allocate memory for widgets\n");
3981 for (i = 0; i < num_widgets; i++) {
3982 ret = of_property_read_string_index(np, propname,
3986 "ASoC: Property '%s' index %d read error:%d\n",
3987 propname, 2 * i, ret);
3991 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3992 if (!strncmp(template, simple_widgets[j].name,
3993 strlen(simple_widgets[j].name))) {
3994 widgets[i] = simple_widgets[j];
3999 if (j >= ARRAY_SIZE(simple_widgets)) {
4001 "ASoC: DAPM widget '%s' is not supported\n",
4006 ret = of_property_read_string_index(np, propname,
4011 "ASoC: Property '%s' index %d read error:%d\n",
4012 propname, (2 * i) + 1, ret);
4016 widgets[i].name = wname;
4019 card->of_dapm_widgets = widgets;
4020 card->num_of_dapm_widgets = num_widgets;
4024 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4026 static int snd_soc_of_get_slot_mask(struct device_node *np,
4027 const char *prop_name,
4031 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
4037 for (i = 0; i < val; i++)
4038 if (be32_to_cpup(&of_slot_mask[i]))
4044 int snd_soc_of_parse_tdm_slot(struct device_node *np,
4045 unsigned int *tx_mask,
4046 unsigned int *rx_mask,
4047 unsigned int *slots,
4048 unsigned int *slot_width)
4054 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
4056 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
4058 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4059 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4067 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4068 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4078 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4080 void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
4081 struct snd_soc_codec_conf *codec_conf,
4082 struct device_node *of_node,
4083 const char *propname)
4085 struct device_node *np = card->dev->of_node;
4089 ret = of_property_read_string(np, propname, &str);
4091 /* no prefix is not error */
4095 codec_conf->of_node = of_node;
4096 codec_conf->name_prefix = str;
4098 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
4100 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4101 const char *propname)
4103 struct device_node *np = card->dev->of_node;
4105 struct snd_soc_dapm_route *routes;
4108 num_routes = of_property_count_strings(np, propname);
4109 if (num_routes < 0 || num_routes & 1) {
4111 "ASoC: Property '%s' does not exist or its length is not even\n",
4117 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4122 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4126 "ASoC: Could not allocate DAPM route table\n");
4130 for (i = 0; i < num_routes; i++) {
4131 ret = of_property_read_string_index(np, propname,
4132 2 * i, &routes[i].sink);
4135 "ASoC: Property '%s' index %d could not be read: %d\n",
4136 propname, 2 * i, ret);
4139 ret = of_property_read_string_index(np, propname,
4140 (2 * i) + 1, &routes[i].source);
4143 "ASoC: Property '%s' index %d could not be read: %d\n",
4144 propname, (2 * i) + 1, ret);
4149 card->num_of_dapm_routes = num_routes;
4150 card->of_dapm_routes = routes;
4154 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4156 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4158 struct device_node **bitclkmaster,
4159 struct device_node **framemaster)
4163 unsigned int format = 0;
4169 } of_fmt_table[] = {
4170 { "i2s", SND_SOC_DAIFMT_I2S },
4171 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4172 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4173 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4174 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4175 { "ac97", SND_SOC_DAIFMT_AC97 },
4176 { "pdm", SND_SOC_DAIFMT_PDM},
4177 { "msb", SND_SOC_DAIFMT_MSB },
4178 { "lsb", SND_SOC_DAIFMT_LSB },
4185 * check "dai-format = xxx"
4186 * or "[prefix]format = xxx"
4187 * SND_SOC_DAIFMT_FORMAT_MASK area
4189 ret = of_property_read_string(np, "dai-format", &str);
4191 snprintf(prop, sizeof(prop), "%sformat", prefix);
4192 ret = of_property_read_string(np, prop, &str);
4195 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4196 if (strcmp(str, of_fmt_table[i].name) == 0) {
4197 format |= of_fmt_table[i].val;
4204 * check "[prefix]continuous-clock"
4205 * SND_SOC_DAIFMT_CLOCK_MASK area
4207 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4208 if (of_property_read_bool(np, prop))
4209 format |= SND_SOC_DAIFMT_CONT;
4211 format |= SND_SOC_DAIFMT_GATED;
4214 * check "[prefix]bitclock-inversion"
4215 * check "[prefix]frame-inversion"
4216 * SND_SOC_DAIFMT_INV_MASK area
4218 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4219 bit = !!of_get_property(np, prop, NULL);
4221 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4222 frame = !!of_get_property(np, prop, NULL);
4224 switch ((bit << 4) + frame) {
4226 format |= SND_SOC_DAIFMT_IB_IF;
4229 format |= SND_SOC_DAIFMT_IB_NF;
4232 format |= SND_SOC_DAIFMT_NB_IF;
4235 /* SND_SOC_DAIFMT_NB_NF is default */
4240 * check "[prefix]bitclock-master"
4241 * check "[prefix]frame-master"
4242 * SND_SOC_DAIFMT_MASTER_MASK area
4244 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4245 bit = !!of_get_property(np, prop, NULL);
4246 if (bit && bitclkmaster)
4247 *bitclkmaster = of_parse_phandle(np, prop, 0);
4249 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4250 frame = !!of_get_property(np, prop, NULL);
4251 if (frame && framemaster)
4252 *framemaster = of_parse_phandle(np, prop, 0);
4254 switch ((bit << 4) + frame) {
4256 format |= SND_SOC_DAIFMT_CBM_CFM;
4259 format |= SND_SOC_DAIFMT_CBM_CFS;
4262 format |= SND_SOC_DAIFMT_CBS_CFM;
4265 format |= SND_SOC_DAIFMT_CBS_CFS;
4271 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4273 int snd_soc_get_dai_id(struct device_node *ep)
4275 struct snd_soc_component *pos;
4276 struct device_node *node;
4279 node = of_graph_get_port_parent(ep);
4282 * For example HDMI case, HDMI has video/sound port,
4283 * but ALSA SoC needs sound port number only.
4284 * Thus counting HDMI DT port/endpoint doesn't work.
4285 * Then, it should have .of_xlate_dai_id
4288 mutex_lock(&client_mutex);
4289 list_for_each_entry(pos, &component_list, list) {
4290 struct device_node *component_of_node = pos->dev->of_node;
4292 if (!component_of_node && pos->dev->parent)
4293 component_of_node = pos->dev->parent->of_node;
4295 if (component_of_node != node)
4298 if (pos->driver->of_xlate_dai_id)
4299 ret = pos->driver->of_xlate_dai_id(pos, ep);
4303 mutex_unlock(&client_mutex);
4309 EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
4311 int snd_soc_get_dai_name(struct of_phandle_args *args,
4312 const char **dai_name)
4314 struct snd_soc_component *pos;
4315 struct device_node *component_of_node;
4316 int ret = -EPROBE_DEFER;
4318 mutex_lock(&client_mutex);
4319 list_for_each_entry(pos, &component_list, list) {
4320 component_of_node = pos->dev->of_node;
4321 if (!component_of_node && pos->dev->parent)
4322 component_of_node = pos->dev->parent->of_node;
4324 if (component_of_node != args->np)
4327 if (pos->driver->of_xlate_dai_name) {
4328 ret = pos->driver->of_xlate_dai_name(pos,
4332 struct snd_soc_dai *dai;
4335 switch (args->args_count) {
4337 id = 0; /* same as dai_drv[0] */
4347 if (id < 0 || id >= pos->num_dai) {
4354 /* find target DAI */
4355 list_for_each_entry(dai, &pos->dai_list, list) {
4361 *dai_name = dai->driver->name;
4363 *dai_name = pos->name;
4368 mutex_unlock(&client_mutex);
4371 EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
4373 int snd_soc_of_get_dai_name(struct device_node *of_node,
4374 const char **dai_name)
4376 struct of_phandle_args args;
4379 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4380 "#sound-dai-cells", 0, &args);
4384 ret = snd_soc_get_dai_name(&args, dai_name);
4386 of_node_put(args.np);
4390 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4393 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
4395 * @of_node: Device node
4396 * @dai_link: DAI link
4398 * Builds an array of CODEC DAI components from the DAI link property
4400 * The array is set in the DAI link and the number of DAIs is set accordingly.
4401 * The device nodes in the array (of_node) must be dereferenced by the caller.
4403 * Returns 0 for success
4405 int snd_soc_of_get_dai_link_codecs(struct device *dev,
4406 struct device_node *of_node,
4407 struct snd_soc_dai_link *dai_link)
4409 struct of_phandle_args args;
4410 struct snd_soc_dai_link_component *component;
4412 int index, num_codecs, ret;
4414 /* Count the number of CODECs */
4416 num_codecs = of_count_phandle_with_args(of_node, name,
4417 "#sound-dai-cells");
4418 if (num_codecs <= 0) {
4419 if (num_codecs == -ENOENT)
4420 dev_err(dev, "No 'sound-dai' property\n");
4422 dev_err(dev, "Bad phandle in 'sound-dai'\n");
4425 component = devm_kzalloc(dev,
4426 sizeof *component * num_codecs,
4430 dai_link->codecs = component;
4431 dai_link->num_codecs = num_codecs;
4433 /* Parse the list */
4434 for (index = 0, component = dai_link->codecs;
4435 index < dai_link->num_codecs;
4436 index++, component++) {
4437 ret = of_parse_phandle_with_args(of_node, name,
4442 component->of_node = args.np;
4443 ret = snd_soc_get_dai_name(&args, &component->dai_name);
4449 for (index = 0, component = dai_link->codecs;
4450 index < dai_link->num_codecs;
4451 index++, component++) {
4452 if (!component->of_node)
4454 of_node_put(component->of_node);
4455 component->of_node = NULL;
4457 dai_link->codecs = NULL;
4458 dai_link->num_codecs = 0;
4461 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
4463 static int __init snd_soc_init(void)
4465 snd_soc_debugfs_init();
4466 snd_soc_util_init();
4468 return platform_driver_register(&soc_driver);
4470 module_init(snd_soc_init);
4472 static void __exit snd_soc_exit(void)
4474 snd_soc_util_exit();
4475 snd_soc_debugfs_exit();
4477 platform_driver_unregister(&soc_driver);
4479 module_exit(snd_soc_exit);
4481 /* Module information */
4482 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4483 MODULE_DESCRIPTION("ALSA SoC Core");
4484 MODULE_LICENSE("GPL");
4485 MODULE_ALIAS("platform:soc-audio");