2 * soc-core.c -- ALSA SoC Audio Layer
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
30 #include <linux/bitops.h>
31 #include <linux/debugfs.h>
32 #include <linux/platform_device.h>
33 #include <linux/pinctrl/consumer.h>
34 #include <linux/ctype.h>
35 #include <linux/slab.h>
37 #include <linux/of_graph.h>
38 #include <linux/dmi.h>
39 #include <sound/core.h>
40 #include <sound/jack.h>
41 #include <sound/pcm.h>
42 #include <sound/pcm_params.h>
43 #include <sound/soc.h>
44 #include <sound/soc-dpcm.h>
45 #include <sound/soc-topology.h>
46 #include <sound/initval.h>
48 #define CREATE_TRACE_POINTS
49 #include <trace/events/asoc.h>
53 #ifdef CONFIG_DEBUG_FS
54 struct dentry *snd_soc_debugfs_root;
55 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
58 static DEFINE_MUTEX(client_mutex);
59 static LIST_HEAD(platform_list);
60 static LIST_HEAD(codec_list);
61 static LIST_HEAD(component_list);
64 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
68 static int pmdown_time = 5000;
69 module_param(pmdown_time, int, 0);
70 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
72 /* If a DMI filed contain strings in this blacklist (e.g.
73 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
74 * as invalid and dropped when setting the card long name from DMI info.
76 static const char * const dmi_blacklist[] = {
77 "To be filled by OEM",
83 NULL, /* terminator */
86 /* returns the minimum number of bytes needed to represent
87 * a particular given value */
88 static int min_bytes_needed(unsigned long val)
93 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
96 c = (sizeof val * 8) - c;
104 /* fill buf which is 'len' bytes with a formatted
105 * string of the form 'reg: value\n' */
106 static int format_register_str(struct snd_soc_codec *codec,
107 unsigned int reg, char *buf, size_t len)
109 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
110 int regsize = codec->driver->reg_word_size * 2;
113 /* +2 for ': ' and + 1 for '\n' */
114 if (wordsize + regsize + 2 + 1 != len)
117 sprintf(buf, "%.*x: ", wordsize, reg);
120 ret = snd_soc_read(codec, reg);
122 memset(buf, 'X', regsize);
124 sprintf(buf, "%.*x", regsize, ret);
126 /* no NUL-termination needed */
130 /* codec register dump */
131 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
132 size_t count, loff_t pos)
135 int wordsize, regsize;
140 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
141 regsize = codec->driver->reg_word_size * 2;
143 len = wordsize + regsize + 2 + 1;
145 if (!codec->driver->reg_cache_size)
148 if (codec->driver->reg_cache_step)
149 step = codec->driver->reg_cache_step;
151 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
152 /* only support larger than PAGE_SIZE bytes debugfs
153 * entries for the default case */
155 if (total + len >= count - 1)
157 format_register_str(codec, i, buf + total, len);
163 total = min(total, count - 1);
168 static ssize_t codec_reg_show(struct device *dev,
169 struct device_attribute *attr, char *buf)
171 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
173 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
176 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
178 static ssize_t pmdown_time_show(struct device *dev,
179 struct device_attribute *attr, char *buf)
181 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
183 return sprintf(buf, "%ld\n", rtd->pmdown_time);
186 static ssize_t pmdown_time_set(struct device *dev,
187 struct device_attribute *attr,
188 const char *buf, size_t count)
190 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
193 ret = kstrtol(buf, 10, &rtd->pmdown_time);
200 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
202 static struct attribute *soc_dev_attrs[] = {
203 &dev_attr_codec_reg.attr,
204 &dev_attr_pmdown_time.attr,
208 static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
209 struct attribute *attr, int idx)
211 struct device *dev = kobj_to_dev(kobj);
212 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
214 if (attr == &dev_attr_pmdown_time.attr)
215 return attr->mode; /* always visible */
216 return rtd->codec ? attr->mode : 0; /* enabled only with codec */
219 static const struct attribute_group soc_dapm_dev_group = {
220 .attrs = soc_dapm_dev_attrs,
221 .is_visible = soc_dev_attr_is_visible,
224 static const struct attribute_group soc_dev_roup = {
225 .attrs = soc_dev_attrs,
226 .is_visible = soc_dev_attr_is_visible,
229 static const struct attribute_group *soc_dev_attr_groups[] = {
235 #ifdef CONFIG_DEBUG_FS
236 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
237 size_t count, loff_t *ppos)
240 struct snd_soc_codec *codec = file->private_data;
243 if (*ppos < 0 || !count)
246 buf = kmalloc(count, GFP_KERNEL);
250 ret = soc_codec_reg_show(codec, buf, count, *ppos);
252 if (copy_to_user(user_buf, buf, ret)) {
263 static ssize_t codec_reg_write_file(struct file *file,
264 const char __user *user_buf, size_t count, loff_t *ppos)
269 unsigned long reg, value;
270 struct snd_soc_codec *codec = file->private_data;
273 buf_size = min(count, (sizeof(buf)-1));
274 if (copy_from_user(buf, user_buf, buf_size))
278 while (*start == ' ')
280 reg = simple_strtoul(start, &start, 16);
281 while (*start == ' ')
283 ret = kstrtoul(start, 16, &value);
287 /* Userspace has been fiddling around behind the kernel's back */
288 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
290 snd_soc_write(codec, reg, value);
294 static const struct file_operations codec_reg_fops = {
296 .read = codec_reg_read_file,
297 .write = codec_reg_write_file,
298 .llseek = default_llseek,
301 static void soc_init_component_debugfs(struct snd_soc_component *component)
303 if (!component->card->debugfs_card_root)
306 if (component->debugfs_prefix) {
309 name = kasprintf(GFP_KERNEL, "%s:%s",
310 component->debugfs_prefix, component->name);
312 component->debugfs_root = debugfs_create_dir(name,
313 component->card->debugfs_card_root);
317 component->debugfs_root = debugfs_create_dir(component->name,
318 component->card->debugfs_card_root);
321 if (!component->debugfs_root) {
322 dev_warn(component->dev,
323 "ASoC: Failed to create component debugfs directory\n");
327 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
328 component->debugfs_root);
330 if (component->init_debugfs)
331 component->init_debugfs(component);
334 static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
336 debugfs_remove_recursive(component->debugfs_root);
339 static void soc_init_codec_debugfs(struct snd_soc_component *component)
341 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
342 struct dentry *debugfs_reg;
344 debugfs_reg = debugfs_create_file("codec_reg", 0644,
345 codec->component.debugfs_root,
346 codec, &codec_reg_fops);
349 "ASoC: Failed to create codec register debugfs file\n");
352 static 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;
565 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
566 const char *dai_link, int stream)
568 struct snd_soc_pcm_runtime *rtd;
570 list_for_each_entry(rtd, &card->rtd_list, list) {
571 if (rtd->dai_link->no_pcm &&
572 !strcmp(rtd->dai_link->name, dai_link))
573 return rtd->pcm->streams[stream].substream;
575 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
578 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
580 static const struct snd_soc_ops null_snd_soc_ops;
582 static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
583 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
585 struct snd_soc_pcm_runtime *rtd;
587 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
591 INIT_LIST_HEAD(&rtd->component_list);
593 rtd->dai_link = dai_link;
594 if (!rtd->dai_link->ops)
595 rtd->dai_link->ops = &null_snd_soc_ops;
597 rtd->codec_dais = kzalloc(sizeof(struct snd_soc_dai *) *
598 dai_link->num_codecs,
600 if (!rtd->codec_dais) {
608 static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
610 kfree(rtd->codec_dais);
611 snd_soc_rtdcom_del_all(rtd);
615 static void soc_add_pcm_runtime(struct snd_soc_card *card,
616 struct snd_soc_pcm_runtime *rtd)
618 list_add_tail(&rtd->list, &card->rtd_list);
619 rtd->num = card->num_rtd;
623 static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
625 struct snd_soc_pcm_runtime *rtd, *_rtd;
627 list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) {
628 list_del(&rtd->list);
629 soc_free_pcm_runtime(rtd);
635 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
636 const char *dai_link)
638 struct snd_soc_pcm_runtime *rtd;
640 list_for_each_entry(rtd, &card->rtd_list, list) {
641 if (!strcmp(rtd->dai_link->name, dai_link))
644 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
647 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
649 static void codec2codec_close_delayed_work(struct work_struct *work)
651 /* Currently nothing to do for c2c links
652 * Since c2c links are internal nodes in the DAPM graph and
653 * don't interface with the outside world or application layer
654 * we don't have to do any special handling on close.
658 #ifdef CONFIG_PM_SLEEP
659 /* powers down audio subsystem for suspend */
660 int snd_soc_suspend(struct device *dev)
662 struct snd_soc_card *card = dev_get_drvdata(dev);
663 struct snd_soc_component *component;
664 struct snd_soc_pcm_runtime *rtd;
667 /* If the card is not initialized yet there is nothing to do */
668 if (!card->instantiated)
671 /* Due to the resume being scheduled into a workqueue we could
672 * suspend before that's finished - wait for it to complete.
674 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
676 /* we're going to block userspace touching us until resume completes */
677 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
679 /* mute any active DACs */
680 list_for_each_entry(rtd, &card->rtd_list, list) {
682 if (rtd->dai_link->ignore_suspend)
685 for (i = 0; i < rtd->num_codecs; i++) {
686 struct snd_soc_dai *dai = rtd->codec_dais[i];
687 struct snd_soc_dai_driver *drv = dai->driver;
689 if (drv->ops->digital_mute && dai->playback_active)
690 drv->ops->digital_mute(dai, 1);
694 /* suspend all pcms */
695 list_for_each_entry(rtd, &card->rtd_list, list) {
696 if (rtd->dai_link->ignore_suspend)
699 snd_pcm_suspend_all(rtd->pcm);
702 if (card->suspend_pre)
703 card->suspend_pre(card);
705 list_for_each_entry(rtd, &card->rtd_list, list) {
706 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
708 if (rtd->dai_link->ignore_suspend)
711 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
712 cpu_dai->driver->suspend(cpu_dai);
715 /* close any waiting streams */
716 list_for_each_entry(rtd, &card->rtd_list, list)
717 flush_delayed_work(&rtd->delayed_work);
719 list_for_each_entry(rtd, &card->rtd_list, list) {
721 if (rtd->dai_link->ignore_suspend)
724 snd_soc_dapm_stream_event(rtd,
725 SNDRV_PCM_STREAM_PLAYBACK,
726 SND_SOC_DAPM_STREAM_SUSPEND);
728 snd_soc_dapm_stream_event(rtd,
729 SNDRV_PCM_STREAM_CAPTURE,
730 SND_SOC_DAPM_STREAM_SUSPEND);
733 /* Recheck all endpoints too, their state is affected by suspend */
734 dapm_mark_endpoints_dirty(card);
735 snd_soc_dapm_sync(&card->dapm);
737 /* suspend all COMPONENTs */
738 list_for_each_entry(component, &card->component_dev_list, card_list) {
739 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
741 /* If there are paths active then the COMPONENT will be held with
742 * bias _ON and should not be suspended. */
743 if (!component->suspended) {
744 switch (snd_soc_dapm_get_bias_level(dapm)) {
745 case SND_SOC_BIAS_STANDBY:
747 * If the COMPONENT is capable of idle
748 * bias off then being in STANDBY
749 * means it's doing something,
750 * otherwise fall through.
752 if (dapm->idle_bias_off) {
753 dev_dbg(component->dev,
754 "ASoC: idle_bias_off CODEC on over suspend\n");
758 case SND_SOC_BIAS_OFF:
759 if (component->suspend)
760 component->suspend(component);
761 component->suspended = 1;
762 if (component->regmap)
763 regcache_mark_dirty(component->regmap);
764 /* deactivate pins to sleep state */
765 pinctrl_pm_select_sleep_state(component->dev);
768 dev_dbg(component->dev,
769 "ASoC: COMPONENT is on over suspend\n");
775 list_for_each_entry(rtd, &card->rtd_list, list) {
776 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
778 if (rtd->dai_link->ignore_suspend)
781 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
782 cpu_dai->driver->suspend(cpu_dai);
784 /* deactivate pins to sleep state */
785 pinctrl_pm_select_sleep_state(cpu_dai->dev);
788 if (card->suspend_post)
789 card->suspend_post(card);
793 EXPORT_SYMBOL_GPL(snd_soc_suspend);
795 /* deferred resume work, so resume can complete before we finished
796 * setting our codec back up, which can be very slow on I2C
798 static void soc_resume_deferred(struct work_struct *work)
800 struct snd_soc_card *card =
801 container_of(work, struct snd_soc_card, deferred_resume_work);
802 struct snd_soc_pcm_runtime *rtd;
803 struct snd_soc_component *component;
806 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
807 * so userspace apps are blocked from touching us
810 dev_dbg(card->dev, "ASoC: starting resume work\n");
812 /* Bring us up into D2 so that DAPM starts enabling things */
813 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
815 if (card->resume_pre)
816 card->resume_pre(card);
818 /* resume control bus DAIs */
819 list_for_each_entry(rtd, &card->rtd_list, list) {
820 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
822 if (rtd->dai_link->ignore_suspend)
825 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
826 cpu_dai->driver->resume(cpu_dai);
829 list_for_each_entry(component, &card->component_dev_list, card_list) {
830 if (component->suspended) {
831 if (component->resume)
832 component->resume(component);
833 component->suspended = 0;
837 list_for_each_entry(rtd, &card->rtd_list, list) {
839 if (rtd->dai_link->ignore_suspend)
842 snd_soc_dapm_stream_event(rtd,
843 SNDRV_PCM_STREAM_PLAYBACK,
844 SND_SOC_DAPM_STREAM_RESUME);
846 snd_soc_dapm_stream_event(rtd,
847 SNDRV_PCM_STREAM_CAPTURE,
848 SND_SOC_DAPM_STREAM_RESUME);
851 /* unmute any active DACs */
852 list_for_each_entry(rtd, &card->rtd_list, list) {
854 if (rtd->dai_link->ignore_suspend)
857 for (i = 0; i < rtd->num_codecs; i++) {
858 struct snd_soc_dai *dai = rtd->codec_dais[i];
859 struct snd_soc_dai_driver *drv = dai->driver;
861 if (drv->ops->digital_mute && dai->playback_active)
862 drv->ops->digital_mute(dai, 0);
866 list_for_each_entry(rtd, &card->rtd_list, list) {
867 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
869 if (rtd->dai_link->ignore_suspend)
872 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
873 cpu_dai->driver->resume(cpu_dai);
876 if (card->resume_post)
877 card->resume_post(card);
879 dev_dbg(card->dev, "ASoC: resume work completed\n");
881 /* Recheck all endpoints too, their state is affected by suspend */
882 dapm_mark_endpoints_dirty(card);
883 snd_soc_dapm_sync(&card->dapm);
885 /* userspace can access us now we are back as we were before */
886 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
889 /* powers up audio subsystem after a suspend */
890 int snd_soc_resume(struct device *dev)
892 struct snd_soc_card *card = dev_get_drvdata(dev);
893 bool bus_control = false;
894 struct snd_soc_pcm_runtime *rtd;
896 /* If the card is not initialized yet there is nothing to do */
897 if (!card->instantiated)
900 /* activate pins from sleep state */
901 list_for_each_entry(rtd, &card->rtd_list, list) {
902 struct snd_soc_dai **codec_dais = rtd->codec_dais;
903 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
907 pinctrl_pm_select_default_state(cpu_dai->dev);
909 for (j = 0; j < rtd->num_codecs; j++) {
910 struct snd_soc_dai *codec_dai = codec_dais[j];
911 if (codec_dai->active)
912 pinctrl_pm_select_default_state(codec_dai->dev);
917 * DAIs that also act as the control bus master might have other drivers
918 * hanging off them so need to resume immediately. Other drivers don't
919 * have that problem and may take a substantial amount of time to resume
920 * due to I/O costs and anti-pop so handle them out of line.
922 list_for_each_entry(rtd, &card->rtd_list, list) {
923 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
924 bus_control |= cpu_dai->driver->bus_control;
927 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
928 soc_resume_deferred(&card->deferred_resume_work);
930 dev_dbg(dev, "ASoC: Scheduling resume work\n");
931 if (!schedule_work(&card->deferred_resume_work))
932 dev_err(dev, "ASoC: resume work item may be lost\n");
937 EXPORT_SYMBOL_GPL(snd_soc_resume);
939 #define snd_soc_suspend NULL
940 #define snd_soc_resume NULL
943 static const struct snd_soc_dai_ops null_dai_ops = {
946 static struct snd_soc_component *soc_find_component(
947 const struct device_node *of_node, const char *name)
949 struct snd_soc_component *component;
951 lockdep_assert_held(&client_mutex);
953 list_for_each_entry(component, &component_list, list) {
955 if (component->dev->of_node == of_node)
957 } else if (strcmp(component->name, name) == 0) {
966 * snd_soc_find_dai - Find a registered DAI
968 * @dlc: name of the DAI or the DAI driver and optional component info to match
970 * This function will search all registered components and their DAIs to
971 * find the DAI of the same name. The component's of_node and name
972 * should also match if being specified.
974 * Return: pointer of DAI, or NULL if not found.
976 struct snd_soc_dai *snd_soc_find_dai(
977 const struct snd_soc_dai_link_component *dlc)
979 struct snd_soc_component *component;
980 struct snd_soc_dai *dai;
981 struct device_node *component_of_node;
983 lockdep_assert_held(&client_mutex);
985 /* Find CPU DAI from registered DAIs*/
986 list_for_each_entry(component, &component_list, list) {
987 component_of_node = component->dev->of_node;
988 if (!component_of_node && component->dev->parent)
989 component_of_node = component->dev->parent->of_node;
991 if (dlc->of_node && component_of_node != dlc->of_node)
993 if (dlc->name && strcmp(component->name, dlc->name))
995 list_for_each_entry(dai, &component->dai_list, list) {
996 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
997 && (!dai->driver->name
998 || strcmp(dai->driver->name, dlc->dai_name)))
1007 EXPORT_SYMBOL_GPL(snd_soc_find_dai);
1011 * snd_soc_find_dai_link - Find a DAI link
1014 * @id: DAI link ID to match
1015 * @name: DAI link name to match, optional
1016 * @stream_name: DAI link stream name to match, optional
1018 * This function will search all existing DAI links of the soc card to
1019 * find the link of the same ID. Since DAI links may not have their
1020 * unique ID, so name and stream name should also match if being
1023 * Return: pointer of DAI link, or NULL if not found.
1025 struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
1026 int id, const char *name,
1027 const char *stream_name)
1029 struct snd_soc_dai_link *link, *_link;
1031 lockdep_assert_held(&client_mutex);
1033 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1037 if (name && (!link->name || strcmp(name, link->name)))
1040 if (stream_name && (!link->stream_name
1041 || strcmp(stream_name, link->stream_name)))
1049 EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
1051 static bool soc_is_dai_link_bound(struct snd_soc_card *card,
1052 struct snd_soc_dai_link *dai_link)
1054 struct snd_soc_pcm_runtime *rtd;
1056 list_for_each_entry(rtd, &card->rtd_list, list) {
1057 if (rtd->dai_link == dai_link)
1064 static int soc_bind_dai_link(struct snd_soc_card *card,
1065 struct snd_soc_dai_link *dai_link)
1067 struct snd_soc_pcm_runtime *rtd;
1068 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
1069 struct snd_soc_dai_link_component cpu_dai_component;
1070 struct snd_soc_component *component;
1071 struct snd_soc_dai **codec_dais;
1072 struct snd_soc_platform *platform;
1073 struct device_node *platform_of_node;
1074 const char *platform_name;
1077 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
1079 if (soc_is_dai_link_bound(card, dai_link)) {
1080 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
1085 rtd = soc_new_pcm_runtime(card, dai_link);
1089 cpu_dai_component.name = dai_link->cpu_name;
1090 cpu_dai_component.of_node = dai_link->cpu_of_node;
1091 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
1092 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
1093 if (!rtd->cpu_dai) {
1094 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
1095 dai_link->cpu_dai_name);
1098 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
1100 rtd->num_codecs = dai_link->num_codecs;
1102 /* Find CODEC from registered CODECs */
1103 codec_dais = rtd->codec_dais;
1104 for (i = 0; i < rtd->num_codecs; i++) {
1105 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
1106 if (!codec_dais[i]) {
1107 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
1108 codecs[i].dai_name);
1111 snd_soc_rtdcom_add(rtd, codec_dais[i]->component);
1114 /* Single codec links expect codec and codec_dai in runtime data */
1115 rtd->codec_dai = codec_dais[0];
1116 rtd->codec = rtd->codec_dai->codec;
1118 /* if there's no platform we match on the empty platform */
1119 platform_name = dai_link->platform_name;
1120 if (!platform_name && !dai_link->platform_of_node)
1121 platform_name = "snd-soc-dummy";
1123 /* find one from the set of registered platforms */
1124 list_for_each_entry(component, &component_list, list) {
1125 platform_of_node = component->dev->of_node;
1126 if (!platform_of_node && component->dev->parent->of_node)
1127 platform_of_node = component->dev->parent->of_node;
1129 if (dai_link->platform_of_node) {
1130 if (platform_of_node != dai_link->platform_of_node)
1133 if (strcmp(component->name, platform_name))
1137 snd_soc_rtdcom_add(rtd, component);
1140 /* find one from the set of registered platforms */
1141 list_for_each_entry(platform, &platform_list, list) {
1142 platform_of_node = platform->dev->of_node;
1143 if (!platform_of_node && platform->dev->parent->of_node)
1144 platform_of_node = platform->dev->parent->of_node;
1146 if (dai_link->platform_of_node) {
1147 if (platform_of_node != dai_link->platform_of_node)
1150 if (strcmp(platform->component.name, platform_name))
1154 rtd->platform = platform;
1156 if (!rtd->platform) {
1157 dev_err(card->dev, "ASoC: platform %s not registered\n",
1158 dai_link->platform_name);
1162 soc_add_pcm_runtime(card, rtd);
1166 soc_free_pcm_runtime(rtd);
1167 return -EPROBE_DEFER;
1170 static void soc_remove_component(struct snd_soc_component *component)
1172 if (!component->card)
1175 list_del(&component->card_list);
1177 if (component->remove)
1178 component->remove(component);
1180 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1182 soc_cleanup_component_debugfs(component);
1183 component->card = NULL;
1184 module_put(component->dev->driver->owner);
1187 static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1191 if (dai && dai->probed &&
1192 dai->driver->remove_order == order) {
1193 if (dai->driver->remove) {
1194 err = dai->driver->remove(dai);
1197 "ASoC: failed to remove %s: %d\n",
1204 static void soc_remove_link_dais(struct snd_soc_card *card,
1205 struct snd_soc_pcm_runtime *rtd, int order)
1209 /* unregister the rtd device */
1210 if (rtd->dev_registered) {
1211 device_unregister(rtd->dev);
1212 rtd->dev_registered = 0;
1215 /* remove the CODEC DAI */
1216 for (i = 0; i < rtd->num_codecs; i++)
1217 soc_remove_dai(rtd->codec_dais[i], order);
1219 soc_remove_dai(rtd->cpu_dai, order);
1222 static void soc_remove_link_components(struct snd_soc_card *card,
1223 struct snd_soc_pcm_runtime *rtd, int order)
1225 struct snd_soc_component *component;
1226 struct snd_soc_rtdcom_list *rtdcom;
1228 for_each_rtdcom(rtd, rtdcom) {
1229 component = rtdcom->component;
1231 if (component->driver->remove_order == order)
1232 soc_remove_component(component);
1236 static void soc_remove_dai_links(struct snd_soc_card *card)
1239 struct snd_soc_pcm_runtime *rtd;
1240 struct snd_soc_dai_link *link, *_link;
1242 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1244 list_for_each_entry(rtd, &card->rtd_list, list)
1245 soc_remove_link_dais(card, rtd, order);
1248 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1250 list_for_each_entry(rtd, &card->rtd_list, list)
1251 soc_remove_link_components(card, rtd, order);
1254 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1255 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1256 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1259 list_del(&link->list);
1260 card->num_dai_links--;
1264 static int snd_soc_init_multicodec(struct snd_soc_card *card,
1265 struct snd_soc_dai_link *dai_link)
1267 /* Legacy codec/codec_dai link is a single entry in multicodec */
1268 if (dai_link->codec_name || dai_link->codec_of_node ||
1269 dai_link->codec_dai_name) {
1270 dai_link->num_codecs = 1;
1272 dai_link->codecs = devm_kzalloc(card->dev,
1273 sizeof(struct snd_soc_dai_link_component),
1275 if (!dai_link->codecs)
1278 dai_link->codecs[0].name = dai_link->codec_name;
1279 dai_link->codecs[0].of_node = dai_link->codec_of_node;
1280 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1283 if (!dai_link->codecs) {
1284 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1291 static int soc_init_dai_link(struct snd_soc_card *card,
1292 struct snd_soc_dai_link *link)
1296 ret = snd_soc_init_multicodec(card, link);
1298 dev_err(card->dev, "ASoC: failed to init multicodec\n");
1302 for (i = 0; i < link->num_codecs; i++) {
1304 * Codec must be specified by 1 of name or OF node,
1305 * not both or neither.
1307 if (!!link->codecs[i].name ==
1308 !!link->codecs[i].of_node) {
1309 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1313 /* Codec DAI name must be specified */
1314 if (!link->codecs[i].dai_name) {
1315 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1322 * Platform may be specified by either name or OF node, but
1323 * can be left unspecified, and a dummy platform will be used.
1325 if (link->platform_name && link->platform_of_node) {
1327 "ASoC: Both platform name/of_node are set for %s\n",
1333 * CPU device may be specified by either name or OF node, but
1334 * can be left unspecified, and will be matched based on DAI
1337 if (link->cpu_name && link->cpu_of_node) {
1339 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1344 * At least one of CPU DAI name or CPU device name/node must be
1347 if (!link->cpu_dai_name &&
1348 !(link->cpu_name || link->cpu_of_node)) {
1350 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1359 * snd_soc_add_dai_link - Add a DAI link dynamically
1360 * @card: The ASoC card to which the DAI link is added
1361 * @dai_link: The new DAI link to add
1363 * This function adds a DAI link to the ASoC card's link list.
1365 * Note: Topology can use this API to add DAI links when probing the
1366 * topology component. And machine drivers can still define static
1367 * DAI links in dai_link array.
1369 int snd_soc_add_dai_link(struct snd_soc_card *card,
1370 struct snd_soc_dai_link *dai_link)
1372 if (dai_link->dobj.type
1373 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1374 dev_err(card->dev, "Invalid dai link type %d\n",
1375 dai_link->dobj.type);
1379 lockdep_assert_held(&client_mutex);
1380 /* Notify the machine driver for extra initialization
1381 * on the link created by topology.
1383 if (dai_link->dobj.type && card->add_dai_link)
1384 card->add_dai_link(card, dai_link);
1386 list_add_tail(&dai_link->list, &card->dai_link_list);
1387 card->num_dai_links++;
1391 EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1394 * snd_soc_remove_dai_link - Remove a DAI link from the list
1395 * @card: The ASoC card that owns the link
1396 * @dai_link: The DAI link to remove
1398 * This function removes a DAI link from the ASoC card's link list.
1400 * For DAI links previously added by topology, topology should
1401 * remove them by using the dobj embedded in the link.
1403 void snd_soc_remove_dai_link(struct snd_soc_card *card,
1404 struct snd_soc_dai_link *dai_link)
1406 struct snd_soc_dai_link *link, *_link;
1408 if (dai_link->dobj.type
1409 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1410 dev_err(card->dev, "Invalid dai link type %d\n",
1411 dai_link->dobj.type);
1415 lockdep_assert_held(&client_mutex);
1416 /* Notify the machine driver for extra destruction
1417 * on the link created by topology.
1419 if (dai_link->dobj.type && card->remove_dai_link)
1420 card->remove_dai_link(card, dai_link);
1422 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1423 if (link == dai_link) {
1424 list_del(&link->list);
1425 card->num_dai_links--;
1430 EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1432 static void soc_set_name_prefix(struct snd_soc_card *card,
1433 struct snd_soc_component *component)
1437 if (card->codec_conf == NULL)
1440 for (i = 0; i < card->num_configs; i++) {
1441 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1442 if (map->of_node && component->dev->of_node != map->of_node)
1444 if (map->dev_name && strcmp(component->name, map->dev_name))
1446 component->name_prefix = map->name_prefix;
1451 static int soc_probe_component(struct snd_soc_card *card,
1452 struct snd_soc_component *component)
1454 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1455 struct snd_soc_dai *dai;
1458 if (!strcmp(component->name, "snd-soc-dummy"))
1461 if (component->card) {
1462 if (component->card != card) {
1463 dev_err(component->dev,
1464 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1465 card->name, component->card->name);
1471 if (!try_module_get(component->dev->driver->owner))
1474 component->card = card;
1476 soc_set_name_prefix(card, component);
1478 soc_init_component_debugfs(component);
1480 if (component->driver->dapm_widgets) {
1481 ret = snd_soc_dapm_new_controls(dapm,
1482 component->driver->dapm_widgets,
1483 component->driver->num_dapm_widgets);
1486 dev_err(component->dev,
1487 "Failed to create new controls %d\n", ret);
1492 list_for_each_entry(dai, &component->dai_list, list) {
1493 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1495 dev_err(component->dev,
1496 "Failed to create DAI widgets %d\n", ret);
1501 if (component->probe) {
1502 ret = component->probe(component);
1504 dev_err(component->dev,
1505 "ASoC: failed to probe component %d\n", ret);
1509 WARN(dapm->idle_bias_off &&
1510 dapm->bias_level != SND_SOC_BIAS_OFF,
1511 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1515 /* machine specific init */
1516 if (component->init) {
1517 ret = component->init(component);
1519 dev_err(component->dev,
1520 "Failed to do machine specific init %d\n", ret);
1525 if (component->driver->controls)
1526 snd_soc_add_component_controls(component,
1527 component->driver->controls,
1528 component->driver->num_controls);
1529 if (component->driver->dapm_routes)
1530 snd_soc_dapm_add_routes(dapm,
1531 component->driver->dapm_routes,
1532 component->driver->num_dapm_routes);
1534 list_add(&dapm->list, &card->dapm_list);
1535 list_add(&component->card_list, &card->component_dev_list);
1540 soc_cleanup_component_debugfs(component);
1541 component->card = NULL;
1542 module_put(component->dev->driver->owner);
1547 static void rtd_release(struct device *dev)
1552 static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1557 /* register the rtd device */
1558 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1561 device_initialize(rtd->dev);
1562 rtd->dev->parent = rtd->card->dev;
1563 rtd->dev->release = rtd_release;
1564 rtd->dev->groups = soc_dev_attr_groups;
1565 dev_set_name(rtd->dev, "%s", name);
1566 dev_set_drvdata(rtd->dev, rtd);
1567 mutex_init(&rtd->pcm_mutex);
1568 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1569 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1570 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1571 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1572 ret = device_add(rtd->dev);
1574 /* calling put_device() here to free the rtd->dev */
1575 put_device(rtd->dev);
1576 dev_err(rtd->card->dev,
1577 "ASoC: failed to register runtime device: %d\n", ret);
1580 rtd->dev_registered = 1;
1584 static int soc_probe_link_components(struct snd_soc_card *card,
1585 struct snd_soc_pcm_runtime *rtd,
1588 struct snd_soc_component *component;
1589 struct snd_soc_rtdcom_list *rtdcom;
1592 for_each_rtdcom(rtd, rtdcom) {
1593 component = rtdcom->component;
1595 if (component->driver->probe_order == order) {
1596 ret = soc_probe_component(card, component);
1605 static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1609 if (!dai->probed && dai->driver->probe_order == order) {
1610 if (dai->driver->probe) {
1611 ret = dai->driver->probe(dai);
1614 "ASoC: failed to probe DAI %s: %d\n",
1626 static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1627 struct snd_soc_pcm_runtime *rtd)
1631 for (i = 0; i < num_dais; ++i) {
1632 struct snd_soc_dai_driver *drv = dais[i]->driver;
1634 if (!rtd->dai_link->no_pcm && drv->pcm_new)
1635 ret = drv->pcm_new(rtd, dais[i]);
1637 dev_err(dais[i]->dev,
1638 "ASoC: Failed to bind %s with pcm device\n",
1647 static int soc_link_dai_widgets(struct snd_soc_card *card,
1648 struct snd_soc_dai_link *dai_link,
1649 struct snd_soc_pcm_runtime *rtd)
1651 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1652 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1653 struct snd_soc_dapm_widget *sink, *source;
1656 if (rtd->num_codecs > 1)
1657 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1659 /* link the DAI widgets */
1660 sink = codec_dai->playback_widget;
1661 source = cpu_dai->capture_widget;
1662 if (sink && source) {
1663 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1664 dai_link->num_params,
1667 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1668 sink->name, source->name, ret);
1673 sink = cpu_dai->playback_widget;
1674 source = codec_dai->capture_widget;
1675 if (sink && source) {
1676 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1677 dai_link->num_params,
1680 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1681 sink->name, source->name, ret);
1689 static int soc_probe_link_dais(struct snd_soc_card *card,
1690 struct snd_soc_pcm_runtime *rtd, int order)
1692 struct snd_soc_dai_link *dai_link = rtd->dai_link;
1693 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1696 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1697 card->name, rtd->num, order);
1699 /* set default power off timeout */
1700 rtd->pmdown_time = pmdown_time;
1702 ret = soc_probe_dai(cpu_dai, order);
1706 /* probe the CODEC DAI */
1707 for (i = 0; i < rtd->num_codecs; i++) {
1708 ret = soc_probe_dai(rtd->codec_dais[i], order);
1713 /* complete DAI probe during last probe */
1714 if (order != SND_SOC_COMP_ORDER_LAST)
1717 /* do machine specific initialization */
1718 if (dai_link->init) {
1719 ret = dai_link->init(rtd);
1721 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1722 dai_link->name, ret);
1727 if (dai_link->dai_fmt)
1728 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1730 ret = soc_post_component_init(rtd, dai_link->name);
1734 #ifdef CONFIG_DEBUG_FS
1735 /* add DPCM sysfs entries */
1736 if (dai_link->dynamic)
1737 soc_dpcm_debugfs_add(rtd);
1740 if (cpu_dai->driver->compress_new) {
1741 /*create compress_device"*/
1742 ret = cpu_dai->driver->compress_new(rtd, rtd->num);
1744 dev_err(card->dev, "ASoC: can't create compress %s\n",
1745 dai_link->stream_name);
1750 if (!dai_link->params) {
1751 /* create the pcm */
1752 ret = soc_new_pcm(rtd, rtd->num);
1754 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1755 dai_link->stream_name, ret);
1758 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1761 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1762 rtd->num_codecs, rtd);
1766 INIT_DELAYED_WORK(&rtd->delayed_work,
1767 codec2codec_close_delayed_work);
1769 /* link the DAI widgets */
1770 ret = soc_link_dai_widgets(card, dai_link, rtd);
1779 static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1781 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1782 struct snd_soc_component *component;
1784 struct device_node *codec_of_node;
1786 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1787 /* codecs, usually analog devices */
1788 name = aux_dev->codec_name;
1789 codec_of_node = aux_dev->codec_of_node;
1790 component = soc_find_component(codec_of_node, name);
1793 name = of_node_full_name(codec_of_node);
1796 } else if (aux_dev->name) {
1797 /* generic components */
1798 name = aux_dev->name;
1799 component = soc_find_component(NULL, name);
1803 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1807 component->init = aux_dev->init;
1808 list_add(&component->card_aux_list, &card->aux_comp_list);
1813 dev_err(card->dev, "ASoC: %s not registered\n", name);
1814 return -EPROBE_DEFER;
1817 static int soc_probe_aux_devices(struct snd_soc_card *card)
1819 struct snd_soc_component *comp;
1823 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1825 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
1826 if (comp->driver->probe_order == order) {
1827 ret = soc_probe_component(card, comp);
1830 "ASoC: failed to probe aux component %s %d\n",
1841 static void soc_remove_aux_devices(struct snd_soc_card *card)
1843 struct snd_soc_component *comp, *_comp;
1846 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1848 list_for_each_entry_safe(comp, _comp,
1849 &card->aux_comp_list, card_aux_list) {
1851 if (comp->driver->remove_order == order) {
1852 soc_remove_component(comp);
1853 /* remove it from the card's aux_comp_list */
1854 list_del(&comp->card_aux_list);
1860 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1864 if (codec->cache_init)
1867 ret = snd_soc_cache_init(codec);
1870 "ASoC: Failed to set cache compression type: %d\n",
1874 codec->cache_init = 1;
1879 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1880 * @rtd: The runtime for which the DAI link format should be changed
1881 * @dai_fmt: The new DAI link format
1883 * This function updates the DAI link format for all DAIs connected to the DAI
1884 * link for the specified runtime.
1886 * Note: For setups with a static format set the dai_fmt field in the
1887 * corresponding snd_dai_link struct instead of using this function.
1889 * Returns 0 on success, otherwise a negative error code.
1891 int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1892 unsigned int dai_fmt)
1894 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1895 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1899 for (i = 0; i < rtd->num_codecs; i++) {
1900 struct snd_soc_dai *codec_dai = codec_dais[i];
1902 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1903 if (ret != 0 && ret != -ENOTSUPP) {
1904 dev_warn(codec_dai->dev,
1905 "ASoC: Failed to set DAI format: %d\n", ret);
1910 /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1911 if (cpu_dai->codec) {
1912 unsigned int inv_dai_fmt;
1914 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1915 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1916 case SND_SOC_DAIFMT_CBM_CFM:
1917 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1919 case SND_SOC_DAIFMT_CBM_CFS:
1920 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1922 case SND_SOC_DAIFMT_CBS_CFM:
1923 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1925 case SND_SOC_DAIFMT_CBS_CFS:
1926 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1930 dai_fmt = inv_dai_fmt;
1933 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1934 if (ret != 0 && ret != -ENOTSUPP) {
1935 dev_warn(cpu_dai->dev,
1936 "ASoC: Failed to set DAI format: %d\n", ret);
1942 EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1946 /* Trim special characters, and replace '-' with '_' since '-' is used to
1947 * separate different DMI fields in the card long name. Only number and
1948 * alphabet characters and a few separator characters are kept.
1950 static void cleanup_dmi_name(char *name)
1954 for (i = 0; name[i]; i++) {
1955 if (isalnum(name[i]) || (name[i] == '.')
1956 || (name[i] == '_'))
1957 name[j++] = name[i];
1958 else if (name[i] == '-')
1965 /* Check if a DMI field is valid, i.e. not containing any string
1966 * in the black list.
1968 static int is_dmi_valid(const char *field)
1972 while (dmi_blacklist[i]) {
1973 if (strstr(field, dmi_blacklist[i]))
1982 * snd_soc_set_dmi_name() - Register DMI names to card
1983 * @card: The card to register DMI names
1984 * @flavour: The flavour "differentiator" for the card amongst its peers.
1986 * An Intel machine driver may be used by many different devices but are
1987 * difficult for userspace to differentiate, since machine drivers ususally
1988 * use their own name as the card short name and leave the card long name
1989 * blank. To differentiate such devices and fix bugs due to lack of
1990 * device-specific configurations, this function allows DMI info to be used
1991 * as the sound card long name, in the format of
1992 * "vendor-product-version-board"
1993 * (Character '-' is used to separate different DMI fields here).
1994 * This will help the user space to load the device-specific Use Case Manager
1995 * (UCM) configurations for the card.
1997 * Possible card long names may be:
1998 * DellInc.-XPS139343-01-0310JH
1999 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
2000 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
2002 * This function also supports flavoring the card longname to provide
2003 * the extra differentiation, like "vendor-product-version-board-flavor".
2005 * We only keep number and alphabet characters and a few separator characters
2006 * in the card long name since UCM in the user space uses the card long names
2007 * as card configuration directory names and AudoConf cannot support special
2008 * charactors like SPACE.
2010 * Returns 0 on success, otherwise a negative error code.
2012 int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
2014 const char *vendor, *product, *product_version, *board;
2015 size_t longname_buf_size = sizeof(card->snd_card->longname);
2018 if (card->long_name)
2019 return 0; /* long name already set by driver or from DMI */
2021 /* make up dmi long name as: vendor.product.version.board */
2022 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2023 if (!vendor || !is_dmi_valid(vendor)) {
2024 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
2029 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
2031 cleanup_dmi_name(card->dmi_longname);
2033 product = dmi_get_system_info(DMI_PRODUCT_NAME);
2034 if (product && is_dmi_valid(product)) {
2035 len = strlen(card->dmi_longname);
2036 snprintf(card->dmi_longname + len,
2037 longname_buf_size - len,
2040 len++; /* skip the separator "-" */
2041 if (len < longname_buf_size)
2042 cleanup_dmi_name(card->dmi_longname + len);
2044 /* some vendors like Lenovo may only put a self-explanatory
2045 * name in the product version field
2047 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
2048 if (product_version && is_dmi_valid(product_version)) {
2049 len = strlen(card->dmi_longname);
2050 snprintf(card->dmi_longname + len,
2051 longname_buf_size - len,
2052 "-%s", product_version);
2055 if (len < longname_buf_size)
2056 cleanup_dmi_name(card->dmi_longname + len);
2060 board = dmi_get_system_info(DMI_BOARD_NAME);
2061 if (board && is_dmi_valid(board)) {
2062 len = strlen(card->dmi_longname);
2063 snprintf(card->dmi_longname + len,
2064 longname_buf_size - len,
2068 if (len < longname_buf_size)
2069 cleanup_dmi_name(card->dmi_longname + len);
2070 } else if (!product) {
2071 /* fall back to using legacy name */
2072 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
2076 /* Add flavour to dmi long name */
2078 len = strlen(card->dmi_longname);
2079 snprintf(card->dmi_longname + len,
2080 longname_buf_size - len,
2084 if (len < longname_buf_size)
2085 cleanup_dmi_name(card->dmi_longname + len);
2088 /* set the card long name */
2089 card->long_name = card->dmi_longname;
2093 EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
2094 #endif /* CONFIG_DMI */
2096 static int snd_soc_instantiate_card(struct snd_soc_card *card)
2098 struct snd_soc_codec *codec;
2099 struct snd_soc_pcm_runtime *rtd;
2100 struct snd_soc_dai_link *dai_link;
2103 mutex_lock(&client_mutex);
2104 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
2107 for (i = 0; i < card->num_links; i++) {
2108 ret = soc_bind_dai_link(card, &card->dai_link[i]);
2113 /* bind aux_devs too */
2114 for (i = 0; i < card->num_aux_devs; i++) {
2115 ret = soc_bind_aux_dev(card, i);
2120 /* add predefined DAI links to the list */
2121 for (i = 0; i < card->num_links; i++)
2122 snd_soc_add_dai_link(card, card->dai_link+i);
2124 /* initialize the register cache for each available codec */
2125 list_for_each_entry(codec, &codec_list, list) {
2126 if (codec->cache_init)
2128 ret = snd_soc_init_codec_cache(codec);
2133 /* card bind complete so register a sound card */
2134 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2135 card->owner, 0, &card->snd_card);
2138 "ASoC: can't create sound card for card %s: %d\n",
2143 soc_init_card_debugfs(card);
2145 card->dapm.bias_level = SND_SOC_BIAS_OFF;
2146 card->dapm.dev = card->dev;
2147 card->dapm.card = card;
2148 list_add(&card->dapm.list, &card->dapm_list);
2150 #ifdef CONFIG_DEBUG_FS
2151 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2154 #ifdef CONFIG_PM_SLEEP
2155 /* deferred resume work */
2156 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
2159 if (card->dapm_widgets)
2160 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2161 card->num_dapm_widgets);
2163 if (card->of_dapm_widgets)
2164 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2165 card->num_of_dapm_widgets);
2167 /* initialise the sound card only once */
2169 ret = card->probe(card);
2171 goto card_probe_error;
2174 /* probe all components used by DAI links on this card */
2175 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2177 list_for_each_entry(rtd, &card->rtd_list, list) {
2178 ret = soc_probe_link_components(card, rtd, order);
2181 "ASoC: failed to instantiate card %d\n",
2188 /* probe auxiliary components */
2189 ret = soc_probe_aux_devices(card);
2193 /* Find new DAI links added during probing components and bind them.
2194 * Components with topology may bring new DAIs and DAI links.
2196 list_for_each_entry(dai_link, &card->dai_link_list, list) {
2197 if (soc_is_dai_link_bound(card, dai_link))
2200 ret = soc_init_dai_link(card, dai_link);
2203 ret = soc_bind_dai_link(card, dai_link);
2208 /* probe all DAI links on this card */
2209 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2211 list_for_each_entry(rtd, &card->rtd_list, list) {
2212 ret = soc_probe_link_dais(card, rtd, order);
2215 "ASoC: failed to instantiate card %d\n",
2222 snd_soc_dapm_link_dai_widgets(card);
2223 snd_soc_dapm_connect_dai_link_widgets(card);
2226 snd_soc_add_card_controls(card, card->controls, card->num_controls);
2228 if (card->dapm_routes)
2229 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2230 card->num_dapm_routes);
2232 if (card->of_dapm_routes)
2233 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2234 card->num_of_dapm_routes);
2236 /* try to set some sane longname if DMI is available */
2237 snd_soc_set_dmi_name(card, NULL);
2239 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
2241 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2242 "%s", card->long_name ? card->long_name : card->name);
2243 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2244 "%s", card->driver_name ? card->driver_name : card->name);
2245 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2246 switch (card->snd_card->driver[i]) {
2252 if (!isalnum(card->snd_card->driver[i]))
2253 card->snd_card->driver[i] = '_';
2258 if (card->late_probe) {
2259 ret = card->late_probe(card);
2261 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
2263 goto probe_aux_dev_err;
2267 snd_soc_dapm_new_widgets(card);
2269 ret = snd_card_register(card->snd_card);
2271 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2273 goto probe_aux_dev_err;
2276 card->instantiated = 1;
2277 snd_soc_dapm_sync(&card->dapm);
2278 mutex_unlock(&card->mutex);
2279 mutex_unlock(&client_mutex);
2284 soc_remove_aux_devices(card);
2287 soc_remove_dai_links(card);
2293 snd_soc_dapm_free(&card->dapm);
2294 soc_cleanup_card_debugfs(card);
2295 snd_card_free(card->snd_card);
2298 soc_remove_pcm_runtimes(card);
2299 mutex_unlock(&card->mutex);
2300 mutex_unlock(&client_mutex);
2305 /* probes a new socdev */
2306 static int soc_probe(struct platform_device *pdev)
2308 struct snd_soc_card *card = platform_get_drvdata(pdev);
2311 * no card, so machine driver should be registering card
2312 * we should not be here in that case so ret error
2317 dev_warn(&pdev->dev,
2318 "ASoC: machine %s should use snd_soc_register_card()\n",
2321 /* Bodge while we unpick instantiation */
2322 card->dev = &pdev->dev;
2324 return snd_soc_register_card(card);
2327 static int soc_cleanup_card_resources(struct snd_soc_card *card)
2329 struct snd_soc_pcm_runtime *rtd;
2331 /* make sure any delayed work runs */
2332 list_for_each_entry(rtd, &card->rtd_list, list)
2333 flush_delayed_work(&rtd->delayed_work);
2335 /* free the ALSA card at first; this syncs with pending operations */
2336 snd_card_free(card->snd_card);
2338 /* remove and free each DAI */
2339 soc_remove_dai_links(card);
2340 soc_remove_pcm_runtimes(card);
2342 /* remove auxiliary devices */
2343 soc_remove_aux_devices(card);
2345 snd_soc_dapm_free(&card->dapm);
2346 soc_cleanup_card_debugfs(card);
2348 /* remove the card */
2355 /* removes a socdev */
2356 static int soc_remove(struct platform_device *pdev)
2358 struct snd_soc_card *card = platform_get_drvdata(pdev);
2360 snd_soc_unregister_card(card);
2364 int snd_soc_poweroff(struct device *dev)
2366 struct snd_soc_card *card = dev_get_drvdata(dev);
2367 struct snd_soc_pcm_runtime *rtd;
2369 if (!card->instantiated)
2372 /* Flush out pmdown_time work - we actually do want to run it
2373 * now, we're shutting down so no imminent restart. */
2374 list_for_each_entry(rtd, &card->rtd_list, list)
2375 flush_delayed_work(&rtd->delayed_work);
2377 snd_soc_dapm_shutdown(card);
2379 /* deactivate pins to sleep state */
2380 list_for_each_entry(rtd, &card->rtd_list, list) {
2381 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2384 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2385 for (i = 0; i < rtd->num_codecs; i++) {
2386 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
2387 pinctrl_pm_select_sleep_state(codec_dai->dev);
2393 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2395 const struct dev_pm_ops snd_soc_pm_ops = {
2396 .suspend = snd_soc_suspend,
2397 .resume = snd_soc_resume,
2398 .freeze = snd_soc_suspend,
2399 .thaw = snd_soc_resume,
2400 .poweroff = snd_soc_poweroff,
2401 .restore = snd_soc_resume,
2403 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2405 /* ASoC platform driver */
2406 static struct platform_driver soc_driver = {
2408 .name = "soc-audio",
2409 .pm = &snd_soc_pm_ops,
2412 .remove = soc_remove,
2416 * snd_soc_cnew - create new control
2417 * @_template: control template
2418 * @data: control private data
2419 * @long_name: control long name
2420 * @prefix: control name prefix
2422 * Create a new mixer control from a template control.
2424 * Returns 0 for success, else error.
2426 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2427 void *data, const char *long_name,
2430 struct snd_kcontrol_new template;
2431 struct snd_kcontrol *kcontrol;
2434 memcpy(&template, _template, sizeof(template));
2438 long_name = template.name;
2441 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2445 template.name = name;
2447 template.name = long_name;
2450 kcontrol = snd_ctl_new1(&template, data);
2456 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2458 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2459 const struct snd_kcontrol_new *controls, int num_controls,
2460 const char *prefix, void *data)
2464 for (i = 0; i < num_controls; i++) {
2465 const struct snd_kcontrol_new *control = &controls[i];
2466 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2467 control->name, prefix));
2469 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2470 control->name, err);
2478 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2481 struct snd_card *card = soc_card->snd_card;
2482 struct snd_kcontrol *kctl;
2484 if (unlikely(!name))
2487 list_for_each_entry(kctl, &card->controls, list)
2488 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2492 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2495 * snd_soc_add_component_controls - Add an array of controls to a component.
2497 * @component: Component to add controls to
2498 * @controls: Array of controls to add
2499 * @num_controls: Number of elements in the array
2501 * Return: 0 for success, else error.
2503 int snd_soc_add_component_controls(struct snd_soc_component *component,
2504 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2506 struct snd_card *card = component->card->snd_card;
2508 return snd_soc_add_controls(card, component->dev, controls,
2509 num_controls, component->name_prefix, component);
2511 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2514 * snd_soc_add_codec_controls - add an array of controls to a codec.
2515 * Convenience function to add a list of controls. Many codecs were
2516 * duplicating this code.
2518 * @codec: codec to add controls to
2519 * @controls: array of controls to add
2520 * @num_controls: number of elements in the array
2522 * Return 0 for success, else error.
2524 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2525 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2527 return snd_soc_add_component_controls(&codec->component, controls,
2530 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2533 * snd_soc_add_platform_controls - add an array of controls to a platform.
2534 * Convenience function to add a list of controls.
2536 * @platform: platform to add controls to
2537 * @controls: array of controls to add
2538 * @num_controls: number of elements in the array
2540 * Return 0 for success, else error.
2542 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2543 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2545 return snd_soc_add_component_controls(&platform->component, controls,
2548 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2551 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2552 * Convenience function to add a list of controls.
2554 * @soc_card: SoC card to add controls to
2555 * @controls: array of controls to add
2556 * @num_controls: number of elements in the array
2558 * Return 0 for success, else error.
2560 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2561 const struct snd_kcontrol_new *controls, int num_controls)
2563 struct snd_card *card = soc_card->snd_card;
2565 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2568 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2571 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2572 * Convienience function to add a list of controls.
2574 * @dai: DAI to add controls to
2575 * @controls: array of controls to add
2576 * @num_controls: number of elements in the array
2578 * Return 0 for success, else error.
2580 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2581 const struct snd_kcontrol_new *controls, int num_controls)
2583 struct snd_card *card = dai->component->card->snd_card;
2585 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2588 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2591 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2593 * @clk_id: DAI specific clock ID
2594 * @freq: new clock frequency in Hz
2595 * @dir: new clock direction - input/output.
2597 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2599 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2600 unsigned int freq, int dir)
2602 if (dai->driver->ops->set_sysclk)
2603 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2605 return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2608 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2611 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2613 * @clk_id: DAI specific clock ID
2614 * @source: Source for the clock
2615 * @freq: new clock frequency in Hz
2616 * @dir: new clock direction - input/output.
2618 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2620 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2621 int source, unsigned int freq, int dir)
2623 if (codec->driver->set_sysclk)
2624 return codec->driver->set_sysclk(codec, clk_id, source,
2629 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2632 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2633 * @component: COMPONENT
2634 * @clk_id: DAI specific clock ID
2635 * @source: Source for the clock
2636 * @freq: new clock frequency in Hz
2637 * @dir: new clock direction - input/output.
2639 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2641 int snd_soc_component_set_sysclk(struct snd_soc_component *component, int clk_id,
2642 int source, unsigned int freq, int dir)
2644 /* will be removed */
2645 if (component->set_sysclk)
2646 return component->set_sysclk(component, clk_id, source,
2649 if (component->driver->set_sysclk)
2650 return component->driver->set_sysclk(component, clk_id, source,
2655 EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2658 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2660 * @div_id: DAI specific clock divider ID
2661 * @div: new clock divisor.
2663 * Configures the clock dividers. This is used to derive the best DAI bit and
2664 * frame clocks from the system or master clock. It's best to set the DAI bit
2665 * and frame clocks as low as possible to save system power.
2667 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2668 int div_id, int div)
2670 if (dai->driver->ops->set_clkdiv)
2671 return dai->driver->ops->set_clkdiv(dai, div_id, div);
2675 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2678 * snd_soc_dai_set_pll - configure DAI PLL.
2680 * @pll_id: DAI specific PLL ID
2681 * @source: DAI specific source for the PLL
2682 * @freq_in: PLL input clock frequency in Hz
2683 * @freq_out: requested PLL output clock frequency in Hz
2685 * Configures and enables PLL to generate output clock based on input clock.
2687 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2688 unsigned int freq_in, unsigned int freq_out)
2690 if (dai->driver->ops->set_pll)
2691 return dai->driver->ops->set_pll(dai, pll_id, source,
2694 return snd_soc_component_set_pll(dai->component, pll_id, source,
2697 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2700 * snd_soc_codec_set_pll - configure codec PLL.
2702 * @pll_id: DAI specific PLL ID
2703 * @source: DAI specific source for the PLL
2704 * @freq_in: PLL input clock frequency in Hz
2705 * @freq_out: requested PLL output clock frequency in Hz
2707 * Configures and enables PLL to generate output clock based on input clock.
2709 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2710 unsigned int freq_in, unsigned int freq_out)
2712 if (codec->driver->set_pll)
2713 return codec->driver->set_pll(codec, pll_id, source,
2718 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2721 * snd_soc_component_set_pll - configure component PLL.
2722 * @component: COMPONENT
2723 * @pll_id: DAI specific PLL ID
2724 * @source: DAI specific source for the PLL
2725 * @freq_in: PLL input clock frequency in Hz
2726 * @freq_out: requested PLL output clock frequency in Hz
2728 * Configures and enables PLL to generate output clock based on input clock.
2730 int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2731 int source, unsigned int freq_in,
2732 unsigned int freq_out)
2734 /* will be removed */
2735 if (component->set_pll)
2736 return component->set_pll(component, pll_id, source,
2739 if (component->driver->set_pll)
2740 return component->driver->set_pll(component, pll_id, source,
2745 EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2748 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2750 * @ratio: Ratio of BCLK to Sample rate.
2752 * Configures the DAI for a preset BCLK to sample rate ratio.
2754 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2756 if (dai->driver->ops->set_bclk_ratio)
2757 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2761 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2764 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2766 * @fmt: SND_SOC_DAIFMT_* format value.
2768 * Configures the DAI hardware format and clocking.
2770 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2772 if (dai->driver == NULL)
2774 if (dai->driver->ops->set_fmt == NULL)
2776 return dai->driver->ops->set_fmt(dai, fmt);
2778 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2781 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
2782 * @slots: Number of slots in use.
2783 * @tx_mask: bitmask representing active TX slots.
2784 * @rx_mask: bitmask representing active RX slots.
2786 * Generates the TDM tx and rx slot default masks for DAI.
2788 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
2789 unsigned int *tx_mask,
2790 unsigned int *rx_mask)
2792 if (*tx_mask || *rx_mask)
2798 *tx_mask = (1 << slots) - 1;
2799 *rx_mask = (1 << slots) - 1;
2805 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2806 * @dai: The DAI to configure
2807 * @tx_mask: bitmask representing active TX slots.
2808 * @rx_mask: bitmask representing active RX slots.
2809 * @slots: Number of slots in use.
2810 * @slot_width: Width in bits for each slot.
2812 * This function configures the specified DAI for TDM operation. @slot contains
2813 * the total number of slots of the TDM stream and @slot_with the width of each
2814 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2815 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2816 * DAI should write to or read from. If a bit is set the corresponding slot is
2817 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2818 * the first slot, bit 1 to the second slot and so on. The first active slot
2819 * maps to the first channel of the DAI, the second active slot to the second
2820 * channel and so on.
2822 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2823 * @rx_mask and @slot_width will be ignored.
2825 * Returns 0 on success, a negative error code otherwise.
2827 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2828 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2830 if (dai->driver->ops->xlate_tdm_slot_mask)
2831 dai->driver->ops->xlate_tdm_slot_mask(slots,
2832 &tx_mask, &rx_mask);
2834 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
2836 dai->tx_mask = tx_mask;
2837 dai->rx_mask = rx_mask;
2839 if (dai->driver->ops->set_tdm_slot)
2840 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2845 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2848 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2850 * @tx_num: how many TX channels
2851 * @tx_slot: pointer to an array which imply the TX slot number channel
2853 * @rx_num: how many RX channels
2854 * @rx_slot: pointer to an array which imply the RX slot number channel
2857 * configure the relationship between channel number and TDM slot number.
2859 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2860 unsigned int tx_num, unsigned int *tx_slot,
2861 unsigned int rx_num, unsigned int *rx_slot)
2863 if (dai->driver->ops->set_channel_map)
2864 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2869 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2872 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2874 * @tristate: tristate enable
2876 * Tristates the DAI so that others can use it.
2878 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2880 if (dai->driver->ops->set_tristate)
2881 return dai->driver->ops->set_tristate(dai, tristate);
2885 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2888 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2890 * @mute: mute enable
2891 * @direction: stream to mute
2893 * Mutes the DAI DAC.
2895 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2901 if (dai->driver->ops->mute_stream)
2902 return dai->driver->ops->mute_stream(dai, mute, direction);
2903 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2904 dai->driver->ops->digital_mute)
2905 return dai->driver->ops->digital_mute(dai, mute);
2909 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2912 * snd_soc_register_card - Register a card with the ASoC core
2914 * @card: Card to register
2917 int snd_soc_register_card(struct snd_soc_card *card)
2920 struct snd_soc_pcm_runtime *rtd;
2922 if (!card->name || !card->dev)
2925 for (i = 0; i < card->num_links; i++) {
2926 struct snd_soc_dai_link *link = &card->dai_link[i];
2928 ret = soc_init_dai_link(card, link);
2930 dev_err(card->dev, "ASoC: failed to init link %s\n",
2936 dev_set_drvdata(card->dev, card);
2938 snd_soc_initialize_card_lists(card);
2940 INIT_LIST_HEAD(&card->dai_link_list);
2941 card->num_dai_links = 0;
2943 INIT_LIST_HEAD(&card->rtd_list);
2946 INIT_LIST_HEAD(&card->dapm_dirty);
2947 INIT_LIST_HEAD(&card->dobj_list);
2948 card->instantiated = 0;
2949 mutex_init(&card->mutex);
2950 mutex_init(&card->dapm_mutex);
2952 ret = snd_soc_instantiate_card(card);
2956 /* deactivate pins to sleep state */
2957 list_for_each_entry(rtd, &card->rtd_list, list) {
2958 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2961 for (j = 0; j < rtd->num_codecs; j++) {
2962 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2963 if (!codec_dai->active)
2964 pinctrl_pm_select_sleep_state(codec_dai->dev);
2967 if (!cpu_dai->active)
2968 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2973 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2976 * snd_soc_unregister_card - Unregister a card with the ASoC core
2978 * @card: Card to unregister
2981 int snd_soc_unregister_card(struct snd_soc_card *card)
2983 if (card->instantiated) {
2984 card->instantiated = false;
2985 snd_soc_dapm_shutdown(card);
2986 soc_cleanup_card_resources(card);
2987 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2992 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2995 * Simplify DAI link configuration by removing ".-1" from device names
2996 * and sanitizing names.
2998 static char *fmt_single_name(struct device *dev, int *id)
3000 char *found, name[NAME_SIZE];
3003 if (dev_name(dev) == NULL)
3006 strlcpy(name, dev_name(dev), NAME_SIZE);
3008 /* are we a "%s.%d" name (platform and SPI components) */
3009 found = strstr(name, dev->driver->name);
3012 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3014 /* discard ID from name if ID == -1 */
3016 found[strlen(dev->driver->name)] = '\0';
3020 /* I2C component devices are named "bus-addr" */
3021 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3022 char tmp[NAME_SIZE];
3024 /* create unique ID number from I2C addr and bus */
3025 *id = ((id1 & 0xffff) << 16) + id2;
3027 /* sanitize component name for DAI link creation */
3028 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3029 strlcpy(name, tmp, NAME_SIZE);
3034 return kstrdup(name, GFP_KERNEL);
3038 * Simplify DAI link naming for single devices with multiple DAIs by removing
3039 * any ".-1" and using the DAI name (instead of device name).
3041 static inline char *fmt_multiple_name(struct device *dev,
3042 struct snd_soc_dai_driver *dai_drv)
3044 if (dai_drv->name == NULL) {
3046 "ASoC: error - multiple DAI %s registered with no name\n",
3051 return kstrdup(dai_drv->name, GFP_KERNEL);
3055 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
3057 * @component: The component for which the DAIs should be unregistered
3059 static void snd_soc_unregister_dais(struct snd_soc_component *component)
3061 struct snd_soc_dai *dai, *_dai;
3063 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
3064 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3066 list_del(&dai->list);
3072 /* Create a DAI and add it to the component's DAI list */
3073 static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
3074 struct snd_soc_dai_driver *dai_drv,
3075 bool legacy_dai_naming)
3077 struct device *dev = component->dev;
3078 struct snd_soc_dai *dai;
3080 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
3082 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3087 * Back in the old days when we still had component-less DAIs,
3088 * instead of having a static name, component-less DAIs would
3089 * inherit the name of the parent device so it is possible to
3090 * register multiple instances of the DAI. We still need to keep
3091 * the same naming style even though those DAIs are not
3092 * component-less anymore.
3094 if (legacy_dai_naming &&
3095 (dai_drv->id == 0 || dai_drv->name == NULL)) {
3096 dai->name = fmt_single_name(dev, &dai->id);
3098 dai->name = fmt_multiple_name(dev, dai_drv);
3100 dai->id = dai_drv->id;
3102 dai->id = component->num_dai;
3104 if (dai->name == NULL) {
3109 dai->component = component;
3111 dai->driver = dai_drv;
3112 if (!dai->driver->ops)
3113 dai->driver->ops = &null_dai_ops;
3115 list_add(&dai->list, &component->dai_list);
3116 component->num_dai++;
3118 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3123 * snd_soc_register_dais - Register a DAI with the ASoC core
3125 * @component: The component the DAIs are registered for
3126 * @dai_drv: DAI driver to use for the DAIs
3127 * @count: Number of DAIs
3128 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3131 static int snd_soc_register_dais(struct snd_soc_component *component,
3132 struct snd_soc_dai_driver *dai_drv, size_t count,
3133 bool legacy_dai_naming)
3135 struct device *dev = component->dev;
3136 struct snd_soc_dai *dai;
3140 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
3142 component->dai_drv = dai_drv;
3144 for (i = 0; i < count; i++) {
3146 dai = soc_add_dai(component, dai_drv + i,
3147 count == 1 && legacy_dai_naming);
3157 snd_soc_unregister_dais(component);
3163 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
3165 * @component: The component the DAIs are registered for
3166 * @dai_drv: DAI driver to use for the DAI
3168 * Topology can use this API to register DAIs when probing a component.
3169 * These DAIs's widgets will be freed in the card cleanup and the DAIs
3170 * will be freed in the component cleanup.
3172 int snd_soc_register_dai(struct snd_soc_component *component,
3173 struct snd_soc_dai_driver *dai_drv)
3175 struct snd_soc_dapm_context *dapm =
3176 snd_soc_component_get_dapm(component);
3177 struct snd_soc_dai *dai;
3180 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
3181 dev_err(component->dev, "Invalid dai type %d\n",
3182 dai_drv->dobj.type);
3186 lockdep_assert_held(&client_mutex);
3187 dai = soc_add_dai(component, dai_drv, false);
3191 /* Create the DAI widgets here. After adding DAIs, topology may
3192 * also add routes that need these widgets as source or sink.
3194 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
3196 dev_err(component->dev,
3197 "Failed to create DAI widgets %d\n", ret);
3202 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3204 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3205 enum snd_soc_dapm_type type, int subseq)
3207 struct snd_soc_component *component = dapm->component;
3209 component->driver->seq_notifier(component, type, subseq);
3212 static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3215 struct snd_soc_component *component = dapm->component;
3217 return component->driver->stream_event(component, event);
3220 static int snd_soc_component_drv_pcm_new(struct snd_soc_component *component,
3221 struct snd_soc_pcm_runtime *rtd)
3223 if (component->driver->pcm_new)
3224 return component->driver->pcm_new(rtd);
3229 static void snd_soc_component_drv_pcm_free(struct snd_soc_component *component,
3230 struct snd_pcm *pcm)
3232 if (component->driver->pcm_free)
3233 component->driver->pcm_free(pcm);
3236 static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
3237 enum snd_soc_bias_level level)
3239 struct snd_soc_component *component = dapm->component;
3241 return component->driver->set_bias_level(component, level);
3244 static int snd_soc_component_initialize(struct snd_soc_component *component,
3245 const struct snd_soc_component_driver *driver, struct device *dev)
3247 struct snd_soc_dapm_context *dapm;
3249 component->name = fmt_single_name(dev, &component->id);
3250 if (!component->name) {
3251 dev_err(dev, "ASoC: Failed to allocate name\n");
3255 component->dev = dev;
3256 component->driver = driver;
3257 component->probe = component->driver->probe;
3258 component->remove = component->driver->remove;
3259 component->suspend = component->driver->suspend;
3260 component->resume = component->driver->resume;
3261 component->set_sysclk = component->driver->set_sysclk;
3262 component->set_pll = component->driver->set_pll;
3263 component->set_jack = component->driver->set_jack;
3264 component->pcm_new = snd_soc_component_drv_pcm_new;
3265 component->pcm_free = snd_soc_component_drv_pcm_free;
3267 dapm = snd_soc_component_get_dapm(component);
3269 dapm->component = component;
3270 dapm->bias_level = SND_SOC_BIAS_OFF;
3271 dapm->idle_bias_off = !driver->idle_bias_on;
3272 dapm->suspend_bias_off = driver->suspend_bias_off;
3273 if (driver->seq_notifier)
3274 dapm->seq_notifier = snd_soc_component_seq_notifier;
3275 if (driver->stream_event)
3276 dapm->stream_event = snd_soc_component_stream_event;
3277 if (driver->set_bias_level)
3278 dapm->set_bias_level = snd_soc_component_set_bias_level;
3280 INIT_LIST_HEAD(&component->dai_list);
3281 mutex_init(&component->io_mutex);
3286 static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
3288 int val_bytes = regmap_get_val_bytes(component->regmap);
3290 /* Errors are legitimate for non-integer byte multiples */
3292 component->val_bytes = val_bytes;
3295 #ifdef CONFIG_REGMAP
3298 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
3299 * @component: The component for which to initialize the regmap instance
3300 * @regmap: The regmap instance that should be used by the component
3302 * This function allows deferred assignment of the regmap instance that is
3303 * associated with the component. Only use this if the regmap instance is not
3304 * yet ready when the component is registered. The function must also be called
3305 * before the first IO attempt of the component.
3307 void snd_soc_component_init_regmap(struct snd_soc_component *component,
3308 struct regmap *regmap)
3310 component->regmap = regmap;
3311 snd_soc_component_setup_regmap(component);
3313 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3316 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
3317 * @component: The component for which to de-initialize the regmap instance
3319 * Calls regmap_exit() on the regmap instance associated to the component and
3320 * removes the regmap instance from the component.
3322 * This function should only be used if snd_soc_component_init_regmap() was used
3323 * to initialize the regmap instance.
3325 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3327 regmap_exit(component->regmap);
3328 component->regmap = NULL;
3330 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3334 static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
3336 if (!component->write && !component->read) {
3337 if (!component->regmap)
3338 component->regmap = dev_get_regmap(component->dev, NULL);
3339 if (component->regmap)
3340 snd_soc_component_setup_regmap(component);
3343 list_add(&component->list, &component_list);
3344 INIT_LIST_HEAD(&component->dobj_list);
3347 static void snd_soc_component_add(struct snd_soc_component *component)
3349 mutex_lock(&client_mutex);
3350 snd_soc_component_add_unlocked(component);
3351 mutex_unlock(&client_mutex);
3354 static void snd_soc_component_cleanup(struct snd_soc_component *component)
3356 snd_soc_unregister_dais(component);
3357 kfree(component->name);
3360 static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3362 struct snd_soc_card *card = component->card;
3365 snd_soc_unregister_card(card);
3367 list_del(&component->list);
3370 #define ENDIANNESS_MAP(name) \
3371 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3372 static u64 endianness_format_map[] = {
3373 ENDIANNESS_MAP(S16_),
3374 ENDIANNESS_MAP(U16_),
3375 ENDIANNESS_MAP(S24_),
3376 ENDIANNESS_MAP(U24_),
3377 ENDIANNESS_MAP(S32_),
3378 ENDIANNESS_MAP(U32_),
3379 ENDIANNESS_MAP(S24_3),
3380 ENDIANNESS_MAP(U24_3),
3381 ENDIANNESS_MAP(S20_3),
3382 ENDIANNESS_MAP(U20_3),
3383 ENDIANNESS_MAP(S18_3),
3384 ENDIANNESS_MAP(U18_3),
3385 ENDIANNESS_MAP(FLOAT_),
3386 ENDIANNESS_MAP(FLOAT64_),
3387 ENDIANNESS_MAP(IEC958_SUBFRAME_),
3391 * Fix up the DAI formats for endianness: codecs don't actually see
3392 * the endianness of the data but we're using the CPU format
3393 * definitions which do need to include endianness so we ensure that
3394 * codec DAIs always have both big and little endian variants set.
3396 static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3400 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3401 if (stream->formats & endianness_format_map[i])
3402 stream->formats |= endianness_format_map[i];
3405 int snd_soc_add_component(struct device *dev,
3406 struct snd_soc_component *component,
3407 const struct snd_soc_component_driver *component_driver,
3408 struct snd_soc_dai_driver *dai_drv,
3414 ret = snd_soc_component_initialize(component, component_driver, dev);
3418 component->ignore_pmdown_time = true;
3419 component->registered_as_component = true;
3421 if (component_driver->endianness) {
3422 for (i = 0; i < num_dai; i++) {
3423 convert_endianness_formats(&dai_drv[i].playback);
3424 convert_endianness_formats(&dai_drv[i].capture);
3428 ret = snd_soc_register_dais(component, dai_drv, num_dai,
3429 !component_driver->non_legacy_dai_naming);
3431 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3435 snd_soc_component_add(component);
3440 snd_soc_component_cleanup(component);
3445 EXPORT_SYMBOL_GPL(snd_soc_add_component);
3447 int snd_soc_register_component(struct device *dev,
3448 const struct snd_soc_component_driver *component_driver,
3449 struct snd_soc_dai_driver *dai_drv,
3452 struct snd_soc_component *component;
3454 component = kzalloc(sizeof(*component), GFP_KERNEL);
3458 return snd_soc_add_component(dev, component, component_driver,
3461 EXPORT_SYMBOL_GPL(snd_soc_register_component);
3464 * snd_soc_unregister_component - Unregister all related component
3465 * from the ASoC core
3467 * @dev: The device to unregister
3469 static int __snd_soc_unregister_component(struct device *dev)
3471 struct snd_soc_component *component;
3474 mutex_lock(&client_mutex);
3475 list_for_each_entry(component, &component_list, list) {
3476 if (dev != component->dev ||
3477 !component->registered_as_component)
3480 snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);
3481 snd_soc_component_del_unlocked(component);
3485 mutex_unlock(&client_mutex);
3488 snd_soc_component_cleanup(component);
3495 void snd_soc_unregister_component(struct device *dev)
3497 while (__snd_soc_unregister_component(dev));
3499 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3501 struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
3502 const char *driver_name)
3504 struct snd_soc_component *component;
3505 struct snd_soc_component *ret;
3508 mutex_lock(&client_mutex);
3509 list_for_each_entry(component, &component_list, list) {
3510 if (dev != component->dev)
3514 (driver_name != component->driver->name) &&
3515 (strcmp(component->driver->name, driver_name) != 0))
3521 mutex_unlock(&client_mutex);
3525 EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
3527 static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
3529 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3531 return platform->driver->probe(platform);
3534 static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
3536 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3538 platform->driver->remove(platform);
3541 static int snd_soc_platform_drv_pcm_new(struct snd_soc_component *component,
3542 struct snd_soc_pcm_runtime *rtd)
3544 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3546 if (platform->driver->pcm_new)
3547 return platform->driver->pcm_new(rtd);
3552 static void snd_soc_platform_drv_pcm_free(struct snd_soc_component *component,
3553 struct snd_pcm *pcm)
3555 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3557 if (platform->driver->pcm_free)
3558 platform->driver->pcm_free(pcm);
3562 * snd_soc_add_platform - Add a platform to the ASoC core
3563 * @dev: The parent device for the platform
3564 * @platform: The platform to add
3565 * @platform_drv: The driver for the platform
3567 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
3568 const struct snd_soc_platform_driver *platform_drv)
3572 ret = snd_soc_component_initialize(&platform->component,
3573 &platform_drv->component_driver, dev);
3577 platform->dev = dev;
3578 platform->driver = platform_drv;
3580 if (platform_drv->probe)
3581 platform->component.probe = snd_soc_platform_drv_probe;
3582 if (platform_drv->remove)
3583 platform->component.remove = snd_soc_platform_drv_remove;
3584 if (platform_drv->pcm_new)
3585 platform->component.pcm_new = snd_soc_platform_drv_pcm_new;
3586 if (platform_drv->pcm_free)
3587 platform->component.pcm_free = snd_soc_platform_drv_pcm_free;
3589 #ifdef CONFIG_DEBUG_FS
3590 platform->component.debugfs_prefix = "platform";
3593 mutex_lock(&client_mutex);
3594 snd_soc_component_add_unlocked(&platform->component);
3595 list_add(&platform->list, &platform_list);
3596 mutex_unlock(&client_mutex);
3598 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
3599 platform->component.name);
3603 EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3606 * snd_soc_register_platform - Register a platform with the ASoC core
3608 * @dev: The device for the platform
3609 * @platform_drv: The driver for the platform
3611 int snd_soc_register_platform(struct device *dev,
3612 const struct snd_soc_platform_driver *platform_drv)
3614 struct snd_soc_platform *platform;
3617 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3619 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3620 if (platform == NULL)
3623 ret = snd_soc_add_platform(dev, platform, platform_drv);
3629 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3632 * snd_soc_remove_platform - Remove a platform from the ASoC core
3633 * @platform: the platform to remove
3635 void snd_soc_remove_platform(struct snd_soc_platform *platform)
3638 mutex_lock(&client_mutex);
3639 list_del(&platform->list);
3640 snd_soc_component_del_unlocked(&platform->component);
3641 mutex_unlock(&client_mutex);
3643 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3644 platform->component.name);
3646 snd_soc_component_cleanup(&platform->component);
3648 EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
3650 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
3652 struct snd_soc_platform *platform;
3654 mutex_lock(&client_mutex);
3655 list_for_each_entry(platform, &platform_list, list) {
3656 if (dev == platform->dev) {
3657 mutex_unlock(&client_mutex);
3661 mutex_unlock(&client_mutex);
3665 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
3668 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3670 * @dev: platform to unregister
3672 void snd_soc_unregister_platform(struct device *dev)
3674 struct snd_soc_platform *platform;
3676 platform = snd_soc_lookup_platform(dev);
3680 snd_soc_remove_platform(platform);
3683 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3685 static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3687 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3689 return codec->driver->probe(codec);
3692 static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3694 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3696 codec->driver->remove(codec);
3699 static int snd_soc_codec_drv_suspend(struct snd_soc_component *component)
3701 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3703 return codec->driver->suspend(codec);
3706 static int snd_soc_codec_drv_resume(struct snd_soc_component *component)
3708 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3710 return codec->driver->resume(codec);
3713 static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3714 unsigned int reg, unsigned int val)
3716 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3718 return codec->driver->write(codec, reg, val);
3721 static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3722 unsigned int reg, unsigned int *val)
3724 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3726 *val = codec->driver->read(codec, reg);
3731 static int snd_soc_codec_set_sysclk_(struct snd_soc_component *component,
3732 int clk_id, int source, unsigned int freq, int dir)
3734 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3736 return snd_soc_codec_set_sysclk(codec, clk_id, source, freq, dir);
3739 static int snd_soc_codec_set_pll_(struct snd_soc_component *component,
3740 int pll_id, int source, unsigned int freq_in,
3741 unsigned int freq_out)
3743 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3745 return snd_soc_codec_set_pll(codec, pll_id, source, freq_in, freq_out);
3748 static int snd_soc_codec_set_jack_(struct snd_soc_component *component,
3749 struct snd_soc_jack *jack, void *data)
3751 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3753 return snd_soc_codec_set_jack(codec, jack, data);
3756 static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
3757 enum snd_soc_bias_level level)
3759 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
3761 return codec->driver->set_bias_level(codec, level);
3765 * snd_soc_register_codec - Register a codec with the ASoC core
3767 * @dev: The parent device for this codec
3768 * @codec_drv: Codec driver
3769 * @dai_drv: The associated DAI driver
3770 * @num_dai: Number of DAIs
3772 int snd_soc_register_codec(struct device *dev,
3773 const struct snd_soc_codec_driver *codec_drv,
3774 struct snd_soc_dai_driver *dai_drv,
3777 struct snd_soc_dapm_context *dapm;
3778 struct snd_soc_codec *codec;
3779 struct snd_soc_dai *dai;
3782 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3784 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3788 codec->component.codec = codec;
3790 ret = snd_soc_component_initialize(&codec->component,
3791 &codec_drv->component_driver, dev);
3795 if (codec_drv->probe)
3796 codec->component.probe = snd_soc_codec_drv_probe;
3797 if (codec_drv->remove)
3798 codec->component.remove = snd_soc_codec_drv_remove;
3799 if (codec_drv->suspend)
3800 codec->component.suspend = snd_soc_codec_drv_suspend;
3801 if (codec_drv->resume)
3802 codec->component.resume = snd_soc_codec_drv_resume;
3803 if (codec_drv->write)
3804 codec->component.write = snd_soc_codec_drv_write;
3805 if (codec_drv->read)
3806 codec->component.read = snd_soc_codec_drv_read;
3807 if (codec_drv->set_sysclk)
3808 codec->component.set_sysclk = snd_soc_codec_set_sysclk_;
3809 if (codec_drv->set_pll)
3810 codec->component.set_pll = snd_soc_codec_set_pll_;
3811 if (codec_drv->set_jack)
3812 codec->component.set_jack = snd_soc_codec_set_jack_;
3813 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
3815 dapm = snd_soc_codec_get_dapm(codec);
3816 dapm->idle_bias_off = codec_drv->idle_bias_off;
3817 dapm->suspend_bias_off = codec_drv->suspend_bias_off;
3818 if (codec_drv->seq_notifier)
3819 dapm->seq_notifier = codec_drv->seq_notifier;
3820 if (codec_drv->set_bias_level)
3821 dapm->set_bias_level = snd_soc_codec_set_bias_level;
3823 codec->driver = codec_drv;
3824 codec->component.val_bytes = codec_drv->reg_word_size;
3826 #ifdef CONFIG_DEBUG_FS
3827 codec->component.init_debugfs = soc_init_codec_debugfs;
3828 codec->component.debugfs_prefix = "codec";
3831 if (codec_drv->get_regmap)
3832 codec->component.regmap = codec_drv->get_regmap(dev);
3834 for (i = 0; i < num_dai; i++) {
3835 convert_endianness_formats(&dai_drv[i].playback);
3836 convert_endianness_formats(&dai_drv[i].capture);
3839 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3841 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3845 list_for_each_entry(dai, &codec->component.dai_list, list)
3848 mutex_lock(&client_mutex);
3849 snd_soc_component_add_unlocked(&codec->component);
3850 list_add(&codec->list, &codec_list);
3851 mutex_unlock(&client_mutex);
3853 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3854 codec->component.name);
3858 snd_soc_component_cleanup(&codec->component);
3863 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3866 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3868 * @dev: codec to unregister
3870 void snd_soc_unregister_codec(struct device *dev)
3872 struct snd_soc_codec *codec;
3874 mutex_lock(&client_mutex);
3875 list_for_each_entry(codec, &codec_list, list) {
3876 if (dev == codec->dev)
3879 mutex_unlock(&client_mutex);
3883 list_del(&codec->list);
3884 snd_soc_component_del_unlocked(&codec->component);
3885 mutex_unlock(&client_mutex);
3887 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3888 codec->component.name);
3890 snd_soc_component_cleanup(&codec->component);
3891 snd_soc_cache_exit(codec);
3894 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3896 /* Retrieve a card's name from device tree */
3897 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3898 const char *propname)
3900 struct device_node *np;
3904 pr_err("card->dev is not set before calling %s\n", __func__);
3908 np = card->dev->of_node;
3910 ret = of_property_read_string_index(np, propname, 0, &card->name);
3912 * EINVAL means the property does not exist. This is fine providing
3913 * card->name was previously set, which is checked later in
3914 * snd_soc_register_card.
3916 if (ret < 0 && ret != -EINVAL) {
3918 "ASoC: Property '%s' could not be read: %d\n",
3925 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3927 static const struct snd_soc_dapm_widget simple_widgets[] = {
3928 SND_SOC_DAPM_MIC("Microphone", NULL),
3929 SND_SOC_DAPM_LINE("Line", NULL),
3930 SND_SOC_DAPM_HP("Headphone", NULL),
3931 SND_SOC_DAPM_SPK("Speaker", NULL),
3934 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
3935 const char *propname)
3937 struct device_node *np = card->dev->of_node;
3938 struct snd_soc_dapm_widget *widgets;
3939 const char *template, *wname;
3940 int i, j, num_widgets, ret;
3942 num_widgets = of_property_count_strings(np, propname);
3943 if (num_widgets < 0) {
3945 "ASoC: Property '%s' does not exist\n", propname);
3948 if (num_widgets & 1) {
3950 "ASoC: Property '%s' length is not even\n", propname);
3956 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3961 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3965 "ASoC: Could not allocate memory for widgets\n");
3969 for (i = 0; i < num_widgets; i++) {
3970 ret = of_property_read_string_index(np, propname,
3974 "ASoC: Property '%s' index %d read error:%d\n",
3975 propname, 2 * i, ret);
3979 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3980 if (!strncmp(template, simple_widgets[j].name,
3981 strlen(simple_widgets[j].name))) {
3982 widgets[i] = simple_widgets[j];
3987 if (j >= ARRAY_SIZE(simple_widgets)) {
3989 "ASoC: DAPM widget '%s' is not supported\n",
3994 ret = of_property_read_string_index(np, propname,
3999 "ASoC: Property '%s' index %d read error:%d\n",
4000 propname, (2 * i) + 1, ret);
4004 widgets[i].name = wname;
4007 card->of_dapm_widgets = widgets;
4008 card->num_of_dapm_widgets = num_widgets;
4012 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4014 static int snd_soc_of_get_slot_mask(struct device_node *np,
4015 const char *prop_name,
4019 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
4025 for (i = 0; i < val; i++)
4026 if (be32_to_cpup(&of_slot_mask[i]))
4032 int snd_soc_of_parse_tdm_slot(struct device_node *np,
4033 unsigned int *tx_mask,
4034 unsigned int *rx_mask,
4035 unsigned int *slots,
4036 unsigned int *slot_width)
4042 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
4044 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
4046 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4047 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4055 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4056 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4066 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4068 void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
4069 struct snd_soc_codec_conf *codec_conf,
4070 struct device_node *of_node,
4071 const char *propname)
4073 struct device_node *np = card->dev->of_node;
4077 ret = of_property_read_string(np, propname, &str);
4079 /* no prefix is not error */
4083 codec_conf->of_node = of_node;
4084 codec_conf->name_prefix = str;
4086 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
4088 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4089 const char *propname)
4091 struct device_node *np = card->dev->of_node;
4093 struct snd_soc_dapm_route *routes;
4096 num_routes = of_property_count_strings(np, propname);
4097 if (num_routes < 0 || num_routes & 1) {
4099 "ASoC: Property '%s' does not exist or its length is not even\n",
4105 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4110 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4114 "ASoC: Could not allocate DAPM route table\n");
4118 for (i = 0; i < num_routes; i++) {
4119 ret = of_property_read_string_index(np, propname,
4120 2 * i, &routes[i].sink);
4123 "ASoC: Property '%s' index %d could not be read: %d\n",
4124 propname, 2 * i, ret);
4127 ret = of_property_read_string_index(np, propname,
4128 (2 * i) + 1, &routes[i].source);
4131 "ASoC: Property '%s' index %d could not be read: %d\n",
4132 propname, (2 * i) + 1, ret);
4137 card->num_of_dapm_routes = num_routes;
4138 card->of_dapm_routes = routes;
4142 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4144 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4146 struct device_node **bitclkmaster,
4147 struct device_node **framemaster)
4151 unsigned int format = 0;
4157 } of_fmt_table[] = {
4158 { "i2s", SND_SOC_DAIFMT_I2S },
4159 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4160 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4161 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4162 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4163 { "ac97", SND_SOC_DAIFMT_AC97 },
4164 { "pdm", SND_SOC_DAIFMT_PDM},
4165 { "msb", SND_SOC_DAIFMT_MSB },
4166 { "lsb", SND_SOC_DAIFMT_LSB },
4173 * check "dai-format = xxx"
4174 * or "[prefix]format = xxx"
4175 * SND_SOC_DAIFMT_FORMAT_MASK area
4177 ret = of_property_read_string(np, "dai-format", &str);
4179 snprintf(prop, sizeof(prop), "%sformat", prefix);
4180 ret = of_property_read_string(np, prop, &str);
4183 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4184 if (strcmp(str, of_fmt_table[i].name) == 0) {
4185 format |= of_fmt_table[i].val;
4192 * check "[prefix]continuous-clock"
4193 * SND_SOC_DAIFMT_CLOCK_MASK area
4195 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4196 if (of_property_read_bool(np, prop))
4197 format |= SND_SOC_DAIFMT_CONT;
4199 format |= SND_SOC_DAIFMT_GATED;
4202 * check "[prefix]bitclock-inversion"
4203 * check "[prefix]frame-inversion"
4204 * SND_SOC_DAIFMT_INV_MASK area
4206 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4207 bit = !!of_get_property(np, prop, NULL);
4209 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4210 frame = !!of_get_property(np, prop, NULL);
4212 switch ((bit << 4) + frame) {
4214 format |= SND_SOC_DAIFMT_IB_IF;
4217 format |= SND_SOC_DAIFMT_IB_NF;
4220 format |= SND_SOC_DAIFMT_NB_IF;
4223 /* SND_SOC_DAIFMT_NB_NF is default */
4228 * check "[prefix]bitclock-master"
4229 * check "[prefix]frame-master"
4230 * SND_SOC_DAIFMT_MASTER_MASK area
4232 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4233 bit = !!of_get_property(np, prop, NULL);
4234 if (bit && bitclkmaster)
4235 *bitclkmaster = of_parse_phandle(np, prop, 0);
4237 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4238 frame = !!of_get_property(np, prop, NULL);
4239 if (frame && framemaster)
4240 *framemaster = of_parse_phandle(np, prop, 0);
4242 switch ((bit << 4) + frame) {
4244 format |= SND_SOC_DAIFMT_CBM_CFM;
4247 format |= SND_SOC_DAIFMT_CBM_CFS;
4250 format |= SND_SOC_DAIFMT_CBS_CFM;
4253 format |= SND_SOC_DAIFMT_CBS_CFS;
4259 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4261 int snd_soc_get_dai_id(struct device_node *ep)
4263 struct snd_soc_component *pos;
4264 struct device_node *node;
4267 node = of_graph_get_port_parent(ep);
4270 * For example HDMI case, HDMI has video/sound port,
4271 * but ALSA SoC needs sound port number only.
4272 * Thus counting HDMI DT port/endpoint doesn't work.
4273 * Then, it should have .of_xlate_dai_id
4276 mutex_lock(&client_mutex);
4277 list_for_each_entry(pos, &component_list, list) {
4278 struct device_node *component_of_node = pos->dev->of_node;
4280 if (!component_of_node && pos->dev->parent)
4281 component_of_node = pos->dev->parent->of_node;
4283 if (component_of_node != node)
4286 if (pos->driver->of_xlate_dai_id)
4287 ret = pos->driver->of_xlate_dai_id(pos, ep);
4291 mutex_unlock(&client_mutex);
4297 EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
4299 int snd_soc_get_dai_name(struct of_phandle_args *args,
4300 const char **dai_name)
4302 struct snd_soc_component *pos;
4303 struct device_node *component_of_node;
4304 int ret = -EPROBE_DEFER;
4306 mutex_lock(&client_mutex);
4307 list_for_each_entry(pos, &component_list, list) {
4308 component_of_node = pos->dev->of_node;
4309 if (!component_of_node && pos->dev->parent)
4310 component_of_node = pos->dev->parent->of_node;
4312 if (component_of_node != args->np)
4315 if (pos->driver->of_xlate_dai_name) {
4316 ret = pos->driver->of_xlate_dai_name(pos,
4322 switch (args->args_count) {
4324 id = 0; /* same as dai_drv[0] */
4334 if (id < 0 || id >= pos->num_dai) {
4341 *dai_name = pos->dai_drv[id].name;
4343 *dai_name = pos->name;
4348 mutex_unlock(&client_mutex);
4351 EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
4353 int snd_soc_of_get_dai_name(struct device_node *of_node,
4354 const char **dai_name)
4356 struct of_phandle_args args;
4359 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4360 "#sound-dai-cells", 0, &args);
4364 ret = snd_soc_get_dai_name(&args, dai_name);
4366 of_node_put(args.np);
4370 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4373 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
4375 * @of_node: Device node
4376 * @dai_link: DAI link
4378 * Builds an array of CODEC DAI components from the DAI link property
4380 * The array is set in the DAI link and the number of DAIs is set accordingly.
4381 * The device nodes in the array (of_node) must be dereferenced by the caller.
4383 * Returns 0 for success
4385 int snd_soc_of_get_dai_link_codecs(struct device *dev,
4386 struct device_node *of_node,
4387 struct snd_soc_dai_link *dai_link)
4389 struct of_phandle_args args;
4390 struct snd_soc_dai_link_component *component;
4392 int index, num_codecs, ret;
4394 /* Count the number of CODECs */
4396 num_codecs = of_count_phandle_with_args(of_node, name,
4397 "#sound-dai-cells");
4398 if (num_codecs <= 0) {
4399 if (num_codecs == -ENOENT)
4400 dev_err(dev, "No 'sound-dai' property\n");
4402 dev_err(dev, "Bad phandle in 'sound-dai'\n");
4405 component = devm_kzalloc(dev,
4406 sizeof *component * num_codecs,
4410 dai_link->codecs = component;
4411 dai_link->num_codecs = num_codecs;
4413 /* Parse the list */
4414 for (index = 0, component = dai_link->codecs;
4415 index < dai_link->num_codecs;
4416 index++, component++) {
4417 ret = of_parse_phandle_with_args(of_node, name,
4422 component->of_node = args.np;
4423 ret = snd_soc_get_dai_name(&args, &component->dai_name);
4429 for (index = 0, component = dai_link->codecs;
4430 index < dai_link->num_codecs;
4431 index++, component++) {
4432 if (!component->of_node)
4434 of_node_put(component->of_node);
4435 component->of_node = NULL;
4437 dai_link->codecs = NULL;
4438 dai_link->num_codecs = 0;
4441 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
4443 static int __init snd_soc_init(void)
4445 snd_soc_debugfs_init();
4446 snd_soc_util_init();
4448 return platform_driver_register(&soc_driver);
4450 module_init(snd_soc_init);
4452 static void __exit snd_soc_exit(void)
4454 snd_soc_util_exit();
4455 snd_soc_debugfs_exit();
4457 platform_driver_unregister(&soc_driver);
4459 module_exit(snd_soc_exit);
4461 /* Module information */
4462 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4463 MODULE_DESCRIPTION("ALSA SoC Core");
4464 MODULE_LICENSE("GPL");
4465 MODULE_ALIAS("platform:soc-audio");