1 // SPDX-License-Identifier: GPL-2.0+
3 // soc-core.c -- ALSA SoC Audio Layer
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Copyright 2005 Openedhand Ltd.
7 // Copyright (C) 2010 Slimlogic Ltd.
8 // Copyright (C) 2010 Texas Instruments Inc.
10 // Author: Liam Girdwood <lrg@slimlogic.co.uk>
11 // with code, comments and ideas from :-
12 // Richard Purdie <richard@openedhand.com>
15 // o Add hw rules to enforce rates, etc.
16 // o More testing with other codecs/machines.
17 // o Add more codecs and platforms to ensure good API coverage.
18 // o Support TDM on PCM and I2S
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
25 #include <linux/bitops.h>
26 #include <linux/debugfs.h>
27 #include <linux/platform_device.h>
28 #include <linux/pinctrl/consumer.h>
29 #include <linux/ctype.h>
30 #include <linux/slab.h>
32 #include <linux/of_graph.h>
33 #include <linux/dmi.h>
34 #include <sound/core.h>
35 #include <sound/jack.h>
36 #include <sound/pcm.h>
37 #include <sound/pcm_params.h>
38 #include <sound/soc.h>
39 #include <sound/soc-dpcm.h>
40 #include <sound/soc-topology.h>
41 #include <sound/initval.h>
43 #define CREATE_TRACE_POINTS
44 #include <trace/events/asoc.h>
48 #ifdef CONFIG_DEBUG_FS
49 struct dentry *snd_soc_debugfs_root;
50 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
53 static DEFINE_MUTEX(client_mutex);
54 static LIST_HEAD(component_list);
55 static LIST_HEAD(unbind_card_list);
57 #define for_each_component(component) \
58 list_for_each_entry(component, &component_list, list)
61 * This is used if driver don't need to have CPU/Codec/Platform
64 struct snd_soc_dai_link_component null_dailink_component[0];
65 EXPORT_SYMBOL_GPL(null_dailink_component);
68 * This is a timeout to do a DAPM powerdown after a stream is closed().
69 * It can be used to eliminate pops between different playback streams, e.g.
70 * between two audio tracks.
72 static int pmdown_time = 5000;
73 module_param(pmdown_time, int, 0);
74 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
78 * If a DMI filed contain strings in this blacklist (e.g.
79 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
80 * as invalid and dropped when setting the card long name from DMI info.
82 static const char * const dmi_blacklist[] = {
83 "To be filled by OEM",
89 NULL, /* terminator */
93 static ssize_t pmdown_time_show(struct device *dev,
94 struct device_attribute *attr, char *buf)
96 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
98 return sprintf(buf, "%ld\n", rtd->pmdown_time);
101 static ssize_t pmdown_time_set(struct device *dev,
102 struct device_attribute *attr,
103 const char *buf, size_t count)
105 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
108 ret = kstrtol(buf, 10, &rtd->pmdown_time);
115 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
117 static struct attribute *soc_dev_attrs[] = {
118 &dev_attr_pmdown_time.attr,
122 static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
123 struct attribute *attr, int idx)
125 struct device *dev = kobj_to_dev(kobj);
126 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
131 if (attr == &dev_attr_pmdown_time.attr)
132 return attr->mode; /* always visible */
133 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
136 static const struct attribute_group soc_dapm_dev_group = {
137 .attrs = soc_dapm_dev_attrs,
138 .is_visible = soc_dev_attr_is_visible,
141 static const struct attribute_group soc_dev_group = {
142 .attrs = soc_dev_attrs,
143 .is_visible = soc_dev_attr_is_visible,
146 static const struct attribute_group *soc_dev_attr_groups[] = {
152 #ifdef CONFIG_DEBUG_FS
153 static void soc_init_component_debugfs(struct snd_soc_component *component)
155 if (!component->card->debugfs_card_root)
158 if (component->debugfs_prefix) {
161 name = kasprintf(GFP_KERNEL, "%s:%s",
162 component->debugfs_prefix, component->name);
164 component->debugfs_root = debugfs_create_dir(name,
165 component->card->debugfs_card_root);
169 component->debugfs_root = debugfs_create_dir(component->name,
170 component->card->debugfs_card_root);
173 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
174 component->debugfs_root);
177 static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
179 if (!component->debugfs_root)
181 debugfs_remove_recursive(component->debugfs_root);
182 component->debugfs_root = NULL;
185 static int dai_list_show(struct seq_file *m, void *v)
187 struct snd_soc_component *component;
188 struct snd_soc_dai *dai;
190 mutex_lock(&client_mutex);
192 for_each_component(component)
193 for_each_component_dais(component, dai)
194 seq_printf(m, "%s\n", dai->name);
196 mutex_unlock(&client_mutex);
200 DEFINE_SHOW_ATTRIBUTE(dai_list);
202 static int component_list_show(struct seq_file *m, void *v)
204 struct snd_soc_component *component;
206 mutex_lock(&client_mutex);
208 for_each_component(component)
209 seq_printf(m, "%s\n", component->name);
211 mutex_unlock(&client_mutex);
215 DEFINE_SHOW_ATTRIBUTE(component_list);
217 static void soc_init_card_debugfs(struct snd_soc_card *card)
219 card->debugfs_card_root = debugfs_create_dir(card->name,
220 snd_soc_debugfs_root);
222 debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root,
225 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
228 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
230 debugfs_remove_recursive(card->debugfs_card_root);
231 card->debugfs_card_root = NULL;
234 static void snd_soc_debugfs_init(void)
236 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
238 debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
241 debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
242 &component_list_fops);
245 static void snd_soc_debugfs_exit(void)
247 debugfs_remove_recursive(snd_soc_debugfs_root);
252 static inline void soc_init_component_debugfs(
253 struct snd_soc_component *component)
257 static inline void soc_cleanup_component_debugfs(
258 struct snd_soc_component *component)
262 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
266 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
270 static inline void snd_soc_debugfs_init(void)
274 static inline void snd_soc_debugfs_exit(void)
281 * This is glue code between snd_pcm_lib_ioctl() and
282 * snd_soc_component_driver :: ioctl
284 int snd_soc_pcm_lib_ioctl(struct snd_soc_component *component,
285 struct snd_pcm_substream *substream,
286 unsigned int cmd, void *arg)
288 return snd_pcm_lib_ioctl(substream, cmd, arg);
290 EXPORT_SYMBOL_GPL(snd_soc_pcm_lib_ioctl);
292 static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
293 struct snd_soc_component *component)
295 struct snd_soc_rtdcom_list *rtdcom;
296 struct snd_soc_component *comp;
298 for_each_rtd_components(rtd, rtdcom, comp) {
299 /* already connected */
300 if (comp == component)
305 * created rtdcom here will be freed when rtd->dev was freed.
307 * soc_free_pcm_runtime() :: device_unregister(rtd->dev)
309 rtdcom = devm_kzalloc(rtd->dev, sizeof(*rtdcom), GFP_KERNEL);
313 rtdcom->component = component;
314 INIT_LIST_HEAD(&rtdcom->list);
317 * When rtd was freed, created rtdcom here will be
319 * And we don't need to call list_del(&rtdcom->list)
320 * when freed, because rtd is also freed.
322 list_add_tail(&rtdcom->list, &rtd->component_list);
327 struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
328 const char *driver_name)
330 struct snd_soc_rtdcom_list *rtdcom;
331 struct snd_soc_component *component;
339 * snd_soc_rtdcom_lookup() will find component from rtd by using
340 * specified driver name.
341 * But, if many components which have same driver name are connected
342 * to 1 rtd, this function will return 1st found component.
344 for_each_rtd_components(rtd, rtdcom, component) {
345 const char *component_name = component->driver->name;
350 if ((component_name == driver_name) ||
351 strcmp(component_name, driver_name) == 0)
352 return rtdcom->component;
357 EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
359 static struct snd_soc_component
360 *snd_soc_lookup_component_nolocked(struct device *dev, const char *driver_name)
362 struct snd_soc_component *component;
363 struct snd_soc_component *found_component;
365 found_component = NULL;
366 for_each_component(component) {
367 if ((dev == component->dev) &&
369 (driver_name == component->driver->name) ||
370 (strcmp(component->driver->name, driver_name) == 0))) {
371 found_component = component;
376 return found_component;
379 struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
380 const char *driver_name)
382 struct snd_soc_component *component;
384 mutex_lock(&client_mutex);
385 component = snd_soc_lookup_component_nolocked(dev, driver_name);
386 mutex_unlock(&client_mutex);
390 EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
392 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
393 const char *dai_link, int stream)
395 struct snd_soc_pcm_runtime *rtd;
397 for_each_card_rtds(card, rtd) {
398 if (rtd->dai_link->no_pcm &&
399 !strcmp(rtd->dai_link->name, dai_link))
400 return rtd->pcm->streams[stream].substream;
402 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
405 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
407 static const struct snd_soc_ops null_snd_soc_ops;
409 static void soc_release_rtd_dev(struct device *dev)
411 /* "dev" means "rtd->dev" */
415 static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
420 list_del(&rtd->list);
423 * we don't need to call kfree() for rtd->dev
425 * soc_release_rtd_dev()
427 * We don't need rtd->dev NULL check, because
428 * it is alloced *before* rtd.
430 * soc_new_pcm_runtime()
432 device_unregister(rtd->dev);
435 static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
436 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
438 struct snd_soc_pcm_runtime *rtd;
445 dev = kzalloc(sizeof(struct device), GFP_KERNEL);
449 dev->parent = card->dev;
450 dev->release = soc_release_rtd_dev;
451 dev->groups = soc_dev_attr_groups;
453 dev_set_name(dev, "%s", dai_link->name);
455 ret = device_register(dev);
457 put_device(dev); /* soc_release_rtd_dev */
464 rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL);
469 dev_set_drvdata(dev, rtd);
472 * for rtd->codec_dais
474 rtd->codec_dais = devm_kcalloc(dev, dai_link->num_codecs,
475 sizeof(struct snd_soc_dai *),
477 if (!rtd->codec_dais)
481 * rtd remaining settings
483 INIT_LIST_HEAD(&rtd->component_list);
484 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
485 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
486 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
487 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
490 rtd->dai_link = dai_link;
491 if (!rtd->dai_link->ops)
492 rtd->dai_link->ops = &null_snd_soc_ops;
494 /* see for_each_card_rtds */
495 list_add_tail(&rtd->list, &card->rtd_list);
496 rtd->num = card->num_rtd;
502 soc_free_pcm_runtime(rtd);
506 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
507 const char *dai_link)
509 struct snd_soc_pcm_runtime *rtd;
511 for_each_card_rtds(card, rtd) {
512 if (!strcmp(rtd->dai_link->name, dai_link))
515 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
518 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
520 static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
522 struct snd_soc_pcm_runtime *rtd;
524 for_each_card_rtds(card, rtd)
525 flush_delayed_work(&rtd->delayed_work);
528 #ifdef CONFIG_PM_SLEEP
529 /* powers down audio subsystem for suspend */
530 int snd_soc_suspend(struct device *dev)
532 struct snd_soc_card *card = dev_get_drvdata(dev);
533 struct snd_soc_component *component;
534 struct snd_soc_pcm_runtime *rtd;
537 /* If the card is not initialized yet there is nothing to do */
538 if (!card->instantiated)
542 * Due to the resume being scheduled into a workqueue we could
543 * suspend before that's finished - wait for it to complete.
545 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
547 /* we're going to block userspace touching us until resume completes */
548 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
550 /* mute any active DACs */
551 for_each_card_rtds(card, rtd) {
552 struct snd_soc_dai *dai;
554 if (rtd->dai_link->ignore_suspend)
557 for_each_rtd_codec_dai(rtd, i, dai) {
558 if (dai->playback_active)
559 snd_soc_dai_digital_mute(dai, 1,
560 SNDRV_PCM_STREAM_PLAYBACK);
564 /* suspend all pcms */
565 for_each_card_rtds(card, rtd) {
566 if (rtd->dai_link->ignore_suspend)
569 snd_pcm_suspend_all(rtd->pcm);
572 if (card->suspend_pre)
573 card->suspend_pre(card);
575 for_each_card_rtds(card, rtd) {
576 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
578 if (rtd->dai_link->ignore_suspend)
581 if (!cpu_dai->driver->bus_control)
582 snd_soc_dai_suspend(cpu_dai);
585 /* close any waiting streams */
586 snd_soc_flush_all_delayed_work(card);
588 for_each_card_rtds(card, rtd) {
590 if (rtd->dai_link->ignore_suspend)
593 snd_soc_dapm_stream_event(rtd,
594 SNDRV_PCM_STREAM_PLAYBACK,
595 SND_SOC_DAPM_STREAM_SUSPEND);
597 snd_soc_dapm_stream_event(rtd,
598 SNDRV_PCM_STREAM_CAPTURE,
599 SND_SOC_DAPM_STREAM_SUSPEND);
602 /* Recheck all endpoints too, their state is affected by suspend */
603 dapm_mark_endpoints_dirty(card);
604 snd_soc_dapm_sync(&card->dapm);
606 /* suspend all COMPONENTs */
607 for_each_card_components(card, component) {
608 struct snd_soc_dapm_context *dapm =
609 snd_soc_component_get_dapm(component);
612 * If there are paths active then the COMPONENT will be held
613 * with bias _ON and should not be suspended.
615 if (!snd_soc_component_is_suspended(component)) {
616 switch (snd_soc_dapm_get_bias_level(dapm)) {
617 case SND_SOC_BIAS_STANDBY:
619 * If the COMPONENT is capable of idle
620 * bias off then being in STANDBY
621 * means it's doing something,
622 * otherwise fall through.
624 if (dapm->idle_bias_off) {
625 dev_dbg(component->dev,
626 "ASoC: idle_bias_off CODEC on over suspend\n");
631 case SND_SOC_BIAS_OFF:
632 snd_soc_component_suspend(component);
633 if (component->regmap)
634 regcache_mark_dirty(component->regmap);
635 /* deactivate pins to sleep state */
636 pinctrl_pm_select_sleep_state(component->dev);
639 dev_dbg(component->dev,
640 "ASoC: COMPONENT is on over suspend\n");
646 for_each_card_rtds(card, rtd) {
647 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
649 if (rtd->dai_link->ignore_suspend)
652 if (cpu_dai->driver->bus_control)
653 snd_soc_dai_suspend(cpu_dai);
655 /* deactivate pins to sleep state */
656 pinctrl_pm_select_sleep_state(cpu_dai->dev);
659 if (card->suspend_post)
660 card->suspend_post(card);
664 EXPORT_SYMBOL_GPL(snd_soc_suspend);
667 * deferred resume work, so resume can complete before we finished
668 * setting our codec back up, which can be very slow on I2C
670 static void soc_resume_deferred(struct work_struct *work)
672 struct snd_soc_card *card =
673 container_of(work, struct snd_soc_card,
674 deferred_resume_work);
675 struct snd_soc_pcm_runtime *rtd;
676 struct snd_soc_component *component;
680 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
681 * so userspace apps are blocked from touching us
684 dev_dbg(card->dev, "ASoC: starting resume work\n");
686 /* Bring us up into D2 so that DAPM starts enabling things */
687 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
689 if (card->resume_pre)
690 card->resume_pre(card);
692 /* resume control bus DAIs */
693 for_each_card_rtds(card, rtd) {
694 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
696 if (rtd->dai_link->ignore_suspend)
699 if (cpu_dai->driver->bus_control)
700 snd_soc_dai_resume(cpu_dai);
703 for_each_card_components(card, component) {
704 if (snd_soc_component_is_suspended(component))
705 snd_soc_component_resume(component);
708 for_each_card_rtds(card, rtd) {
710 if (rtd->dai_link->ignore_suspend)
713 snd_soc_dapm_stream_event(rtd,
714 SNDRV_PCM_STREAM_PLAYBACK,
715 SND_SOC_DAPM_STREAM_RESUME);
717 snd_soc_dapm_stream_event(rtd,
718 SNDRV_PCM_STREAM_CAPTURE,
719 SND_SOC_DAPM_STREAM_RESUME);
722 /* unmute any active DACs */
723 for_each_card_rtds(card, rtd) {
724 struct snd_soc_dai *dai;
726 if (rtd->dai_link->ignore_suspend)
729 for_each_rtd_codec_dai(rtd, i, dai) {
730 if (dai->playback_active)
731 snd_soc_dai_digital_mute(dai, 0,
732 SNDRV_PCM_STREAM_PLAYBACK);
736 for_each_card_rtds(card, rtd) {
737 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
739 if (rtd->dai_link->ignore_suspend)
742 if (!cpu_dai->driver->bus_control)
743 snd_soc_dai_resume(cpu_dai);
746 if (card->resume_post)
747 card->resume_post(card);
749 dev_dbg(card->dev, "ASoC: resume work completed\n");
751 /* Recheck all endpoints too, their state is affected by suspend */
752 dapm_mark_endpoints_dirty(card);
753 snd_soc_dapm_sync(&card->dapm);
755 /* userspace can access us now we are back as we were before */
756 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
759 /* powers up audio subsystem after a suspend */
760 int snd_soc_resume(struct device *dev)
762 struct snd_soc_card *card = dev_get_drvdata(dev);
763 bool bus_control = false;
764 struct snd_soc_pcm_runtime *rtd;
765 struct snd_soc_dai *codec_dai;
768 /* If the card is not initialized yet there is nothing to do */
769 if (!card->instantiated)
772 /* activate pins from sleep state */
773 for_each_card_rtds(card, rtd) {
774 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
777 pinctrl_pm_select_default_state(cpu_dai->dev);
779 for_each_rtd_codec_dai(rtd, i, codec_dai) {
780 if (codec_dai->active)
781 pinctrl_pm_select_default_state(codec_dai->dev);
786 * DAIs that also act as the control bus master might have other drivers
787 * hanging off them so need to resume immediately. Other drivers don't
788 * have that problem and may take a substantial amount of time to resume
789 * due to I/O costs and anti-pop so handle them out of line.
791 for_each_card_rtds(card, rtd) {
792 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
794 bus_control |= cpu_dai->driver->bus_control;
797 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
798 soc_resume_deferred(&card->deferred_resume_work);
800 dev_dbg(dev, "ASoC: Scheduling resume work\n");
801 if (!schedule_work(&card->deferred_resume_work))
802 dev_err(dev, "ASoC: resume work item may be lost\n");
807 EXPORT_SYMBOL_GPL(snd_soc_resume);
809 static void soc_resume_init(struct snd_soc_card *card)
811 /* deferred resume work */
812 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
815 #define snd_soc_suspend NULL
816 #define snd_soc_resume NULL
817 static inline void soc_resume_init(struct snd_soc_card *card)
822 static const struct snd_soc_dai_ops null_dai_ops = {
825 static struct device_node
826 *soc_component_to_node(struct snd_soc_component *component)
828 struct device_node *of_node;
830 of_node = component->dev->of_node;
831 if (!of_node && component->dev->parent)
832 of_node = component->dev->parent->of_node;
837 static int snd_soc_is_matching_component(
838 const struct snd_soc_dai_link_component *dlc,
839 struct snd_soc_component *component)
841 struct device_node *component_of_node;
846 component_of_node = soc_component_to_node(component);
848 if (dlc->of_node && component_of_node != dlc->of_node)
850 if (dlc->name && strcmp(component->name, dlc->name))
856 static struct snd_soc_component *soc_find_component(
857 const struct snd_soc_dai_link_component *dlc)
859 struct snd_soc_component *component;
861 lockdep_assert_held(&client_mutex);
866 * It returns *1st* found component, but some driver
867 * has few components by same of_node/name
869 * CPU component and generic DMAEngine component
871 for_each_component(component)
872 if (snd_soc_is_matching_component(dlc, component))
879 * snd_soc_find_dai - Find a registered DAI
881 * @dlc: name of the DAI or the DAI driver and optional component info to match
883 * This function will search all registered components and their DAIs to
884 * find the DAI of the same name. The component's of_node and name
885 * should also match if being specified.
887 * Return: pointer of DAI, or NULL if not found.
889 struct snd_soc_dai *snd_soc_find_dai(
890 const struct snd_soc_dai_link_component *dlc)
892 struct snd_soc_component *component;
893 struct snd_soc_dai *dai;
895 lockdep_assert_held(&client_mutex);
897 /* Find CPU DAI from registered DAIs */
898 for_each_component(component) {
899 if (!snd_soc_is_matching_component(dlc, component))
901 for_each_component_dais(component, dai) {
902 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
903 && (!dai->driver->name
904 || strcmp(dai->driver->name, dlc->dai_name)))
913 EXPORT_SYMBOL_GPL(snd_soc_find_dai);
916 * snd_soc_find_dai_link - Find a DAI link
919 * @id: DAI link ID to match
920 * @name: DAI link name to match, optional
921 * @stream_name: DAI link stream name to match, optional
923 * This function will search all existing DAI links of the soc card to
924 * find the link of the same ID. Since DAI links may not have their
925 * unique ID, so name and stream name should also match if being
928 * Return: pointer of DAI link, or NULL if not found.
930 struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
931 int id, const char *name,
932 const char *stream_name)
934 struct snd_soc_dai_link *link;
936 lockdep_assert_held(&client_mutex);
938 for_each_card_links(card, link) {
942 if (name && (!link->name || strcmp(name, link->name)))
945 if (stream_name && (!link->stream_name
946 || strcmp(stream_name, link->stream_name)))
954 EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
956 static bool soc_is_dai_link_bound(struct snd_soc_card *card,
957 struct snd_soc_dai_link *dai_link)
959 struct snd_soc_pcm_runtime *rtd;
961 for_each_card_rtds(card, rtd) {
962 if (rtd->dai_link == dai_link)
969 static int soc_dai_link_sanity_check(struct snd_soc_card *card,
970 struct snd_soc_dai_link *link)
973 struct snd_soc_dai_link_component *codec, *platform;
975 for_each_link_codecs(link, i, codec) {
977 * Codec must be specified by 1 of name or OF node,
978 * not both or neither.
980 if (!!codec->name == !!codec->of_node) {
981 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
986 /* Codec DAI name must be specified */
987 if (!codec->dai_name) {
988 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
994 * Defer card registration if codec component is not added to
997 if (!soc_find_component(codec))
998 return -EPROBE_DEFER;
1001 for_each_link_platforms(link, i, platform) {
1003 * Platform may be specified by either name or OF node, but it
1004 * can be left unspecified, then no components will be inserted
1005 * in the rtdcom list
1007 if (!!platform->name == !!platform->of_node) {
1009 "ASoC: Neither/both platform name/of_node are set for %s\n",
1015 * Defer card registration if platform component is not added to
1018 if (!soc_find_component(platform))
1019 return -EPROBE_DEFER;
1023 if (link->num_cpus > 1) {
1025 "ASoC: multi cpu is not yet supported %s\n",
1031 * CPU device may be specified by either name or OF node, but
1032 * can be left unspecified, and will be matched based on DAI
1035 if (link->cpus->name && link->cpus->of_node) {
1037 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1043 * Defer card registration if cpu dai component is not added to
1046 if ((link->cpus->of_node || link->cpus->name) &&
1047 !soc_find_component(link->cpus))
1048 return -EPROBE_DEFER;
1051 * At least one of CPU DAI name or CPU device name/node must be
1054 if (!link->cpus->dai_name &&
1055 !(link->cpus->name || link->cpus->of_node)) {
1057 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1065 static void soc_unbind_dai_link(struct snd_soc_card *card,
1066 struct snd_soc_dai_link *dai_link)
1068 struct snd_soc_pcm_runtime *rtd;
1070 rtd = snd_soc_get_pcm_runtime(card, dai_link->name);
1072 soc_free_pcm_runtime(rtd);
1075 static int soc_bind_dai_link(struct snd_soc_card *card,
1076 struct snd_soc_dai_link *dai_link)
1078 struct snd_soc_pcm_runtime *rtd;
1079 struct snd_soc_dai_link_component *codec, *platform;
1080 struct snd_soc_component *component;
1083 if (dai_link->ignore)
1086 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
1088 if (soc_is_dai_link_bound(card, dai_link)) {
1089 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
1094 ret = soc_dai_link_sanity_check(card, dai_link);
1098 rtd = soc_new_pcm_runtime(card, dai_link);
1102 /* FIXME: we need multi CPU support in the future */
1103 rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
1104 if (!rtd->cpu_dai) {
1105 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
1106 dai_link->cpus->dai_name);
1109 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
1111 /* Find CODEC from registered CODECs */
1112 rtd->num_codecs = dai_link->num_codecs;
1113 for_each_link_codecs(dai_link, i, codec) {
1114 rtd->codec_dais[i] = snd_soc_find_dai(codec);
1115 if (!rtd->codec_dais[i]) {
1116 dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
1121 snd_soc_rtdcom_add(rtd, rtd->codec_dais[i]->component);
1124 /* Single codec links expect codec and codec_dai in runtime data */
1125 rtd->codec_dai = rtd->codec_dais[0];
1127 /* Find PLATFORM from registered PLATFORMs */
1128 for_each_link_platforms(dai_link, i, platform) {
1129 for_each_component(component) {
1130 if (!snd_soc_is_matching_component(platform, component))
1133 snd_soc_rtdcom_add(rtd, component);
1140 soc_free_pcm_runtime(rtd);
1141 return -EPROBE_DEFER;
1144 static void soc_set_of_name_prefix(struct snd_soc_component *component)
1146 struct device_node *of_node = soc_component_to_node(component);
1150 ret = of_property_read_string(of_node, "sound-name-prefix", &str);
1152 component->name_prefix = str;
1155 static void soc_set_name_prefix(struct snd_soc_card *card,
1156 struct snd_soc_component *component)
1160 for (i = 0; i < card->num_configs && card->codec_conf; i++) {
1161 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1162 struct device_node *of_node = soc_component_to_node(component);
1164 if (map->of_node && of_node != map->of_node)
1166 if (map->dev_name && strcmp(component->name, map->dev_name))
1168 component->name_prefix = map->name_prefix;
1173 * If there is no configuration table or no match in the table,
1174 * check if a prefix is provided in the node
1176 soc_set_of_name_prefix(component);
1179 static void soc_cleanup_component(struct snd_soc_component *component)
1181 /* For framework level robustness */
1182 snd_soc_component_set_jack(component, NULL, NULL);
1184 list_del_init(&component->card_list);
1185 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1186 soc_cleanup_component_debugfs(component);
1187 component->card = NULL;
1188 snd_soc_component_module_put_when_remove(component);
1191 static void soc_remove_component(struct snd_soc_component *component)
1193 if (!component->card)
1196 snd_soc_component_remove(component);
1198 soc_cleanup_component(component);
1201 static int soc_probe_component(struct snd_soc_card *card,
1202 struct snd_soc_component *component)
1204 struct snd_soc_dapm_context *dapm =
1205 snd_soc_component_get_dapm(component);
1206 struct snd_soc_dai *dai;
1209 if (!strcmp(component->name, "snd-soc-dummy"))
1212 if (component->card) {
1213 if (component->card != card) {
1214 dev_err(component->dev,
1215 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1216 card->name, component->card->name);
1222 ret = snd_soc_component_module_get_when_probe(component);
1226 component->card = card;
1227 soc_set_name_prefix(card, component);
1229 soc_init_component_debugfs(component);
1231 snd_soc_dapm_init(dapm, card, component);
1233 ret = snd_soc_dapm_new_controls(dapm,
1234 component->driver->dapm_widgets,
1235 component->driver->num_dapm_widgets);
1238 dev_err(component->dev,
1239 "Failed to create new controls %d\n", ret);
1243 for_each_component_dais(component, dai) {
1244 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1246 dev_err(component->dev,
1247 "Failed to create DAI widgets %d\n", ret);
1252 ret = snd_soc_component_probe(component);
1254 dev_err(component->dev,
1255 "ASoC: failed to probe component %d\n", ret);
1258 WARN(dapm->idle_bias_off &&
1259 dapm->bias_level != SND_SOC_BIAS_OFF,
1260 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1263 /* machine specific init */
1264 if (component->init) {
1265 ret = component->init(component);
1267 dev_err(component->dev,
1268 "Failed to do machine specific init %d\n", ret);
1273 ret = snd_soc_add_component_controls(component,
1274 component->driver->controls,
1275 component->driver->num_controls);
1279 ret = snd_soc_dapm_add_routes(dapm,
1280 component->driver->dapm_routes,
1281 component->driver->num_dapm_routes);
1285 /* see for_each_card_components */
1286 list_add(&component->card_list, &card->component_dev_list);
1290 soc_cleanup_component(component);
1295 static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1299 if (!dai || !dai->probed || !dai->driver ||
1300 dai->driver->remove_order != order)
1303 err = snd_soc_dai_remove(dai);
1306 "ASoC: failed to remove %s: %d\n",
1312 static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1317 dai->driver->probe_order != order)
1320 ret = snd_soc_dai_probe(dai);
1322 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1332 static void soc_remove_link_dais(struct snd_soc_card *card)
1335 struct snd_soc_dai *codec_dai;
1336 struct snd_soc_pcm_runtime *rtd;
1339 for_each_comp_order(order) {
1340 for_each_card_rtds(card, rtd) {
1341 /* remove the CODEC DAI */
1342 for_each_rtd_codec_dai(rtd, i, codec_dai)
1343 soc_remove_dai(codec_dai, order);
1345 soc_remove_dai(rtd->cpu_dai, order);
1350 static int soc_probe_link_dais(struct snd_soc_card *card)
1352 struct snd_soc_dai *codec_dai;
1353 struct snd_soc_pcm_runtime *rtd;
1356 for_each_comp_order(order) {
1357 for_each_card_rtds(card, rtd) {
1360 "ASoC: probe %s dai link %d late %d\n",
1361 card->name, rtd->num, order);
1363 ret = soc_probe_dai(rtd->cpu_dai, order);
1367 /* probe the CODEC DAI */
1368 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1369 ret = soc_probe_dai(codec_dai, order);
1379 static void soc_remove_link_components(struct snd_soc_card *card)
1381 struct snd_soc_component *component;
1382 struct snd_soc_pcm_runtime *rtd;
1383 struct snd_soc_rtdcom_list *rtdcom;
1386 for_each_comp_order(order) {
1387 for_each_card_rtds(card, rtd) {
1388 for_each_rtd_components(rtd, rtdcom, component) {
1389 if (component->driver->remove_order != order)
1392 soc_remove_component(component);
1398 static int soc_probe_link_components(struct snd_soc_card *card)
1400 struct snd_soc_component *component;
1401 struct snd_soc_pcm_runtime *rtd;
1402 struct snd_soc_rtdcom_list *rtdcom;
1405 for_each_comp_order(order) {
1406 for_each_card_rtds(card, rtd) {
1407 for_each_rtd_components(rtd, rtdcom, component) {
1408 if (component->driver->probe_order != order)
1411 ret = soc_probe_component(card, component);
1421 void snd_soc_disconnect_sync(struct device *dev)
1423 struct snd_soc_component *component =
1424 snd_soc_lookup_component(dev, NULL);
1426 if (!component || !component->card)
1429 snd_card_disconnect_sync(component->card->snd_card);
1431 EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
1434 * snd_soc_add_dai_link - Add a DAI link dynamically
1435 * @card: The ASoC card to which the DAI link is added
1436 * @dai_link: The new DAI link to add
1438 * This function adds a DAI link to the ASoC card's link list.
1440 * Note: Topology can use this API to add DAI links when probing the
1441 * topology component. And machine drivers can still define static
1442 * DAI links in dai_link array.
1444 int snd_soc_add_dai_link(struct snd_soc_card *card,
1445 struct snd_soc_dai_link *dai_link)
1449 lockdep_assert_held(&client_mutex);
1452 * Notify the machine driver for extra initialization
1454 if (card->add_dai_link)
1455 card->add_dai_link(card, dai_link);
1457 ret = soc_bind_dai_link(card, dai_link);
1461 /* see for_each_card_links */
1462 list_add_tail(&dai_link->list, &card->dai_link_list);
1466 EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1469 * snd_soc_remove_dai_link - Remove a DAI link from the list
1470 * @card: The ASoC card that owns the link
1471 * @dai_link: The DAI link to remove
1473 * This function removes a DAI link from the ASoC card's link list.
1475 * For DAI links previously added by topology, topology should
1476 * remove them by using the dobj embedded in the link.
1478 void snd_soc_remove_dai_link(struct snd_soc_card *card,
1479 struct snd_soc_dai_link *dai_link)
1481 lockdep_assert_held(&client_mutex);
1484 * Notify the machine driver for extra destruction
1486 if (card->remove_dai_link)
1487 card->remove_dai_link(card, dai_link);
1489 list_del(&dai_link->list);
1491 soc_unbind_dai_link(card, dai_link);
1493 EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1495 static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1496 struct snd_soc_pcm_runtime *rtd)
1500 for (i = 0; i < num_dais; ++i) {
1501 struct snd_soc_dai_driver *drv = dais[i]->driver;
1504 ret = drv->pcm_new(rtd, dais[i]);
1506 dev_err(dais[i]->dev,
1507 "ASoC: Failed to bind %s with pcm device\n",
1516 static int soc_link_init(struct snd_soc_card *card,
1517 struct snd_soc_pcm_runtime *rtd)
1519 struct snd_soc_dai_link *dai_link = rtd->dai_link;
1520 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1521 struct snd_soc_rtdcom_list *rtdcom;
1522 struct snd_soc_component *component;
1525 /* set default power off timeout */
1526 rtd->pmdown_time = pmdown_time;
1528 /* do machine specific initialization */
1529 if (dai_link->init) {
1530 ret = dai_link->init(rtd);
1532 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1533 dai_link->name, ret);
1538 if (dai_link->dai_fmt) {
1539 ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1544 /* add DPCM sysfs entries */
1545 soc_dpcm_debugfs_add(rtd);
1550 * most drivers will register their PCMs using DAI link ordering but
1551 * topology based drivers can use the DAI link id field to set PCM
1552 * device number and then use rtd + a base offset of the BEs.
1554 for_each_rtd_components(rtd, rtdcom, component) {
1555 if (!component->driver->use_dai_pcm_id)
1558 if (rtd->dai_link->no_pcm)
1559 num += component->driver->be_pcm_base;
1561 num = rtd->dai_link->id;
1564 /* create compress_device if possible */
1565 ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1566 if (ret != -ENOTSUPP) {
1568 dev_err(card->dev, "ASoC: can't create compress %s\n",
1569 dai_link->stream_name);
1573 /* create the pcm */
1574 ret = soc_new_pcm(rtd, num);
1576 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1577 dai_link->stream_name, ret);
1580 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1583 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1584 rtd->num_codecs, rtd);
1588 static void soc_unbind_aux_dev(struct snd_soc_card *card)
1590 struct snd_soc_component *component, *_component;
1592 for_each_card_auxs_safe(card, component, _component) {
1593 component->init = NULL;
1594 list_del(&component->card_aux_list);
1598 static int soc_bind_aux_dev(struct snd_soc_card *card)
1600 struct snd_soc_component *component;
1601 struct snd_soc_aux_dev *aux;
1604 for_each_card_pre_auxs(card, i, aux) {
1605 /* codecs, usually analog devices */
1606 component = soc_find_component(&aux->dlc);
1608 return -EPROBE_DEFER;
1610 component->init = aux->init;
1611 /* see for_each_card_auxs */
1612 list_add(&component->card_aux_list, &card->aux_comp_list);
1617 static int soc_probe_aux_devices(struct snd_soc_card *card)
1619 struct snd_soc_component *comp;
1623 for_each_comp_order(order) {
1624 for_each_card_auxs(card, comp) {
1625 if (comp->driver->probe_order == order) {
1626 ret = soc_probe_component(card, comp);
1629 "ASoC: failed to probe aux component %s %d\n",
1640 static void soc_remove_aux_devices(struct snd_soc_card *card)
1642 struct snd_soc_component *comp, *_comp;
1645 for_each_comp_order(order) {
1646 for_each_card_auxs_safe(card, comp, _comp) {
1647 if (comp->driver->remove_order == order)
1648 soc_remove_component(comp);
1654 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1655 * @rtd: The runtime for which the DAI link format should be changed
1656 * @dai_fmt: The new DAI link format
1658 * This function updates the DAI link format for all DAIs connected to the DAI
1659 * link for the specified runtime.
1661 * Note: For setups with a static format set the dai_fmt field in the
1662 * corresponding snd_dai_link struct instead of using this function.
1664 * Returns 0 on success, otherwise a negative error code.
1666 int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1667 unsigned int dai_fmt)
1669 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1670 struct snd_soc_dai *codec_dai;
1674 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1675 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1676 if (ret != 0 && ret != -ENOTSUPP) {
1677 dev_warn(codec_dai->dev,
1678 "ASoC: Failed to set DAI format: %d\n", ret);
1684 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1685 * the component which has non_legacy_dai_naming is Codec
1687 if (cpu_dai->component->driver->non_legacy_dai_naming) {
1688 unsigned int inv_dai_fmt;
1690 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1691 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1692 case SND_SOC_DAIFMT_CBM_CFM:
1693 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1695 case SND_SOC_DAIFMT_CBM_CFS:
1696 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1698 case SND_SOC_DAIFMT_CBS_CFM:
1699 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1701 case SND_SOC_DAIFMT_CBS_CFS:
1702 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1706 dai_fmt = inv_dai_fmt;
1709 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1710 if (ret != 0 && ret != -ENOTSUPP) {
1711 dev_warn(cpu_dai->dev,
1712 "ASoC: Failed to set DAI format: %d\n", ret);
1718 EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1722 * Trim special characters, and replace '-' with '_' since '-' is used to
1723 * separate different DMI fields in the card long name. Only number and
1724 * alphabet characters and a few separator characters are kept.
1726 static void cleanup_dmi_name(char *name)
1730 for (i = 0; name[i]; i++) {
1731 if (isalnum(name[i]) || (name[i] == '.')
1732 || (name[i] == '_'))
1733 name[j++] = name[i];
1734 else if (name[i] == '-')
1742 * Check if a DMI field is valid, i.e. not containing any string
1743 * in the black list.
1745 static int is_dmi_valid(const char *field)
1749 while (dmi_blacklist[i]) {
1750 if (strstr(field, dmi_blacklist[i]))
1759 * snd_soc_set_dmi_name() - Register DMI names to card
1760 * @card: The card to register DMI names
1761 * @flavour: The flavour "differentiator" for the card amongst its peers.
1763 * An Intel machine driver may be used by many different devices but are
1764 * difficult for userspace to differentiate, since machine drivers ususally
1765 * use their own name as the card short name and leave the card long name
1766 * blank. To differentiate such devices and fix bugs due to lack of
1767 * device-specific configurations, this function allows DMI info to be used
1768 * as the sound card long name, in the format of
1769 * "vendor-product-version-board"
1770 * (Character '-' is used to separate different DMI fields here).
1771 * This will help the user space to load the device-specific Use Case Manager
1772 * (UCM) configurations for the card.
1774 * Possible card long names may be:
1775 * DellInc.-XPS139343-01-0310JH
1776 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1777 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1779 * This function also supports flavoring the card longname to provide
1780 * the extra differentiation, like "vendor-product-version-board-flavor".
1782 * We only keep number and alphabet characters and a few separator characters
1783 * in the card long name since UCM in the user space uses the card long names
1784 * as card configuration directory names and AudoConf cannot support special
1785 * charactors like SPACE.
1787 * Returns 0 on success, otherwise a negative error code.
1789 int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1791 const char *vendor, *product, *product_version, *board;
1792 size_t longname_buf_size = sizeof(card->snd_card->longname);
1795 if (card->long_name)
1796 return 0; /* long name already set by driver or from DMI */
1798 /* make up dmi long name as: vendor.product.version.board */
1799 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1800 if (!vendor || !is_dmi_valid(vendor)) {
1801 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1805 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1807 cleanup_dmi_name(card->dmi_longname);
1809 product = dmi_get_system_info(DMI_PRODUCT_NAME);
1810 if (product && is_dmi_valid(product)) {
1811 len = strlen(card->dmi_longname);
1812 snprintf(card->dmi_longname + len,
1813 longname_buf_size - len,
1816 len++; /* skip the separator "-" */
1817 if (len < longname_buf_size)
1818 cleanup_dmi_name(card->dmi_longname + len);
1821 * some vendors like Lenovo may only put a self-explanatory
1822 * name in the product version field
1824 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
1825 if (product_version && is_dmi_valid(product_version)) {
1826 len = strlen(card->dmi_longname);
1827 snprintf(card->dmi_longname + len,
1828 longname_buf_size - len,
1829 "-%s", product_version);
1832 if (len < longname_buf_size)
1833 cleanup_dmi_name(card->dmi_longname + len);
1837 board = dmi_get_system_info(DMI_BOARD_NAME);
1838 if (board && is_dmi_valid(board)) {
1839 len = strlen(card->dmi_longname);
1840 snprintf(card->dmi_longname + len,
1841 longname_buf_size - len,
1845 if (len < longname_buf_size)
1846 cleanup_dmi_name(card->dmi_longname + len);
1847 } else if (!product) {
1848 /* fall back to using legacy name */
1849 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1853 /* Add flavour to dmi long name */
1855 len = strlen(card->dmi_longname);
1856 snprintf(card->dmi_longname + len,
1857 longname_buf_size - len,
1861 if (len < longname_buf_size)
1862 cleanup_dmi_name(card->dmi_longname + len);
1865 /* set the card long name */
1866 card->long_name = card->dmi_longname;
1870 EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
1871 #endif /* CONFIG_DMI */
1873 static void soc_check_tplg_fes(struct snd_soc_card *card)
1875 struct snd_soc_component *component;
1876 const struct snd_soc_component_driver *comp_drv;
1877 struct snd_soc_dai_link *dai_link;
1880 for_each_component(component) {
1882 /* does this component override BEs ? */
1883 if (!component->driver->ignore_machine)
1886 /* for this machine ? */
1887 if (!strcmp(component->driver->ignore_machine,
1888 card->dev->driver->name))
1890 if (strcmp(component->driver->ignore_machine,
1891 dev_name(card->dev)))
1894 /* machine matches, so override the rtd data */
1895 for_each_card_prelinks(card, i, dai_link) {
1897 /* ignore this FE */
1898 if (dai_link->dynamic) {
1899 dai_link->ignore = true;
1903 dev_info(card->dev, "info: override BE DAI link %s\n",
1904 card->dai_link[i].name);
1906 /* override platform component */
1907 if (!dai_link->platforms) {
1908 dev_err(card->dev, "init platform error");
1911 dai_link->platforms->name = component->name;
1913 /* convert non BE into BE */
1914 dai_link->no_pcm = 1;
1916 /* override any BE fixups */
1917 dai_link->be_hw_params_fixup =
1918 component->driver->be_hw_params_fixup;
1921 * most BE links don't set stream name, so set it to
1922 * dai link name if it's NULL to help bind widgets.
1924 if (!dai_link->stream_name)
1925 dai_link->stream_name = dai_link->name;
1928 /* Inform userspace we are using alternate topology */
1929 if (component->driver->topology_name_prefix) {
1931 /* topology shortname created? */
1932 if (!card->topology_shortname_created) {
1933 comp_drv = component->driver;
1935 snprintf(card->topology_shortname, 32, "%s-%s",
1936 comp_drv->topology_name_prefix,
1938 card->topology_shortname_created = true;
1941 /* use topology shortname */
1942 card->name = card->topology_shortname;
1947 #define soc_setup_card_name(name, name1, name2, norm) \
1948 __soc_setup_card_name(name, sizeof(name), name1, name2, norm)
1949 static void __soc_setup_card_name(char *name, int len,
1950 const char *name1, const char *name2,
1955 snprintf(name, len, "%s", name1 ? name1 : name2);
1961 * Name normalization
1963 * The driver name is somewhat special, as it's used as a key for
1964 * searches in the user-space.
1967 * "abcd??efg" -> "abcd__efg"
1969 for (i = 0; i < len; i++) {
1976 if (!isalnum(name[i]))
1983 static void soc_cleanup_card_resources(struct snd_soc_card *card)
1985 struct snd_soc_dai_link *link, *_link;
1987 /* This should be called before snd_card_free() */
1988 soc_remove_link_components(card);
1990 /* free the ALSA card at first; this syncs with pending operations */
1991 if (card->snd_card) {
1992 snd_card_free(card->snd_card);
1993 card->snd_card = NULL;
1996 /* remove and free each DAI */
1997 soc_remove_link_dais(card);
1999 for_each_card_links_safe(card, link, _link)
2000 snd_soc_remove_dai_link(card, link);
2002 /* remove auxiliary devices */
2003 soc_remove_aux_devices(card);
2004 soc_unbind_aux_dev(card);
2006 snd_soc_dapm_free(&card->dapm);
2007 soc_cleanup_card_debugfs(card);
2009 /* remove the card */
2014 static int snd_soc_instantiate_card(struct snd_soc_card *card)
2016 struct snd_soc_pcm_runtime *rtd;
2017 struct snd_soc_dai_link *dai_link;
2020 mutex_lock(&client_mutex);
2021 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
2023 snd_soc_dapm_init(&card->dapm, card, NULL);
2025 /* check whether any platform is ignore machine FE and using topology */
2026 soc_check_tplg_fes(card);
2028 /* bind aux_devs too */
2029 ret = soc_bind_aux_dev(card);
2033 /* add predefined DAI links to the list */
2035 for_each_card_prelinks(card, i, dai_link) {
2036 ret = snd_soc_add_dai_link(card, dai_link);
2041 /* card bind complete so register a sound card */
2042 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2043 card->owner, 0, &card->snd_card);
2046 "ASoC: can't create sound card for card %s: %d\n",
2051 soc_init_card_debugfs(card);
2053 soc_resume_init(card);
2055 ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2056 card->num_dapm_widgets);
2060 ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2061 card->num_of_dapm_widgets);
2065 /* initialise the sound card only once */
2067 ret = card->probe(card);
2072 /* probe all components used by DAI links on this card */
2073 ret = soc_probe_link_components(card);
2076 "ASoC: failed to instantiate card %d\n", ret);
2080 /* probe auxiliary components */
2081 ret = soc_probe_aux_devices(card);
2085 /* probe all DAI links on this card */
2086 ret = soc_probe_link_dais(card);
2089 "ASoC: failed to instantiate card %d\n", ret);
2093 for_each_card_rtds(card, rtd)
2094 soc_link_init(card, rtd);
2096 snd_soc_dapm_link_dai_widgets(card);
2097 snd_soc_dapm_connect_dai_link_widgets(card);
2099 ret = snd_soc_add_card_controls(card, card->controls,
2100 card->num_controls);
2104 ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2105 card->num_dapm_routes);
2109 ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2110 card->num_of_dapm_routes);
2114 /* try to set some sane longname if DMI is available */
2115 snd_soc_set_dmi_name(card, NULL);
2117 soc_setup_card_name(card->snd_card->shortname,
2118 card->name, NULL, 0);
2119 soc_setup_card_name(card->snd_card->longname,
2120 card->long_name, card->name, 0);
2121 soc_setup_card_name(card->snd_card->driver,
2122 card->driver_name, card->name, 1);
2124 if (card->late_probe) {
2125 ret = card->late_probe(card);
2127 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
2133 snd_soc_dapm_new_widgets(card);
2135 ret = snd_card_register(card->snd_card);
2137 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2142 card->instantiated = 1;
2143 dapm_mark_endpoints_dirty(card);
2144 snd_soc_dapm_sync(&card->dapm);
2148 soc_cleanup_card_resources(card);
2150 mutex_unlock(&card->mutex);
2151 mutex_unlock(&client_mutex);
2156 /* probes a new socdev */
2157 static int soc_probe(struct platform_device *pdev)
2159 struct snd_soc_card *card = platform_get_drvdata(pdev);
2162 * no card, so machine driver should be registering card
2163 * we should not be here in that case so ret error
2168 dev_warn(&pdev->dev,
2169 "ASoC: machine %s should use snd_soc_register_card()\n",
2172 /* Bodge while we unpick instantiation */
2173 card->dev = &pdev->dev;
2175 return snd_soc_register_card(card);
2178 /* removes a socdev */
2179 static int soc_remove(struct platform_device *pdev)
2181 struct snd_soc_card *card = platform_get_drvdata(pdev);
2183 snd_soc_unregister_card(card);
2187 int snd_soc_poweroff(struct device *dev)
2189 struct snd_soc_card *card = dev_get_drvdata(dev);
2190 struct snd_soc_pcm_runtime *rtd;
2192 if (!card->instantiated)
2196 * Flush out pmdown_time work - we actually do want to run it
2197 * now, we're shutting down so no imminent restart.
2199 snd_soc_flush_all_delayed_work(card);
2201 snd_soc_dapm_shutdown(card);
2203 /* deactivate pins to sleep state */
2204 for_each_card_rtds(card, rtd) {
2205 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2206 struct snd_soc_dai *codec_dai;
2209 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2210 for_each_rtd_codec_dai(rtd, i, codec_dai) {
2211 pinctrl_pm_select_sleep_state(codec_dai->dev);
2217 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2219 const struct dev_pm_ops snd_soc_pm_ops = {
2220 .suspend = snd_soc_suspend,
2221 .resume = snd_soc_resume,
2222 .freeze = snd_soc_suspend,
2223 .thaw = snd_soc_resume,
2224 .poweroff = snd_soc_poweroff,
2225 .restore = snd_soc_resume,
2227 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2229 /* ASoC platform driver */
2230 static struct platform_driver soc_driver = {
2232 .name = "soc-audio",
2233 .pm = &snd_soc_pm_ops,
2236 .remove = soc_remove,
2240 * snd_soc_cnew - create new control
2241 * @_template: control template
2242 * @data: control private data
2243 * @long_name: control long name
2244 * @prefix: control name prefix
2246 * Create a new mixer control from a template control.
2248 * Returns 0 for success, else error.
2250 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2251 void *data, const char *long_name,
2254 struct snd_kcontrol_new template;
2255 struct snd_kcontrol *kcontrol;
2258 memcpy(&template, _template, sizeof(template));
2262 long_name = template.name;
2265 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2269 template.name = name;
2271 template.name = long_name;
2274 kcontrol = snd_ctl_new1(&template, data);
2280 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2282 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2283 const struct snd_kcontrol_new *controls, int num_controls,
2284 const char *prefix, void *data)
2288 for (i = 0; i < num_controls; i++) {
2289 const struct snd_kcontrol_new *control = &controls[i];
2291 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2292 control->name, prefix));
2294 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2295 control->name, err);
2303 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2306 struct snd_card *card = soc_card->snd_card;
2307 struct snd_kcontrol *kctl;
2309 if (unlikely(!name))
2312 list_for_each_entry(kctl, &card->controls, list)
2313 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2317 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2320 * snd_soc_add_component_controls - Add an array of controls to a component.
2322 * @component: Component to add controls to
2323 * @controls: Array of controls to add
2324 * @num_controls: Number of elements in the array
2326 * Return: 0 for success, else error.
2328 int snd_soc_add_component_controls(struct snd_soc_component *component,
2329 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2331 struct snd_card *card = component->card->snd_card;
2333 return snd_soc_add_controls(card, component->dev, controls,
2334 num_controls, component->name_prefix, component);
2336 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2339 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2340 * Convenience function to add a list of controls.
2342 * @soc_card: SoC card to add controls to
2343 * @controls: array of controls to add
2344 * @num_controls: number of elements in the array
2346 * Return 0 for success, else error.
2348 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2349 const struct snd_kcontrol_new *controls, int num_controls)
2351 struct snd_card *card = soc_card->snd_card;
2353 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2356 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2359 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2360 * Convienience function to add a list of controls.
2362 * @dai: DAI to add controls to
2363 * @controls: array of controls to add
2364 * @num_controls: number of elements in the array
2366 * Return 0 for success, else error.
2368 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2369 const struct snd_kcontrol_new *controls, int num_controls)
2371 struct snd_card *card = dai->component->card->snd_card;
2373 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2376 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2378 static int snd_soc_bind_card(struct snd_soc_card *card)
2380 struct snd_soc_pcm_runtime *rtd;
2383 ret = snd_soc_instantiate_card(card);
2387 /* deactivate pins to sleep state */
2388 for_each_card_rtds(card, rtd) {
2389 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2390 struct snd_soc_dai *codec_dai;
2393 for_each_rtd_codec_dai(rtd, j, codec_dai) {
2394 if (!codec_dai->active)
2395 pinctrl_pm_select_sleep_state(codec_dai->dev);
2398 if (!cpu_dai->active)
2399 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2406 * snd_soc_register_card - Register a card with the ASoC core
2408 * @card: Card to register
2411 int snd_soc_register_card(struct snd_soc_card *card)
2413 if (!card->name || !card->dev)
2416 dev_set_drvdata(card->dev, card);
2418 INIT_LIST_HEAD(&card->widgets);
2419 INIT_LIST_HEAD(&card->paths);
2420 INIT_LIST_HEAD(&card->dapm_list);
2421 INIT_LIST_HEAD(&card->aux_comp_list);
2422 INIT_LIST_HEAD(&card->component_dev_list);
2423 INIT_LIST_HEAD(&card->list);
2424 INIT_LIST_HEAD(&card->dai_link_list);
2425 INIT_LIST_HEAD(&card->rtd_list);
2426 INIT_LIST_HEAD(&card->dapm_dirty);
2427 INIT_LIST_HEAD(&card->dobj_list);
2429 card->instantiated = 0;
2430 mutex_init(&card->mutex);
2431 mutex_init(&card->dapm_mutex);
2432 mutex_init(&card->pcm_mutex);
2433 spin_lock_init(&card->dpcm_lock);
2435 return snd_soc_bind_card(card);
2437 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2439 static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2441 if (card->instantiated) {
2442 card->instantiated = false;
2443 snd_soc_dapm_shutdown(card);
2444 snd_soc_flush_all_delayed_work(card);
2446 soc_cleanup_card_resources(card);
2448 list_add(&card->list, &unbind_card_list);
2451 list_del(&card->list);
2456 * snd_soc_unregister_card - Unregister a card with the ASoC core
2458 * @card: Card to unregister
2461 int snd_soc_unregister_card(struct snd_soc_card *card)
2463 mutex_lock(&client_mutex);
2464 snd_soc_unbind_card(card, true);
2465 mutex_unlock(&client_mutex);
2466 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2470 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2473 * Simplify DAI link configuration by removing ".-1" from device names
2474 * and sanitizing names.
2476 static char *fmt_single_name(struct device *dev, int *id)
2478 char *found, name[NAME_SIZE];
2481 if (dev_name(dev) == NULL)
2484 strlcpy(name, dev_name(dev), NAME_SIZE);
2486 /* are we a "%s.%d" name (platform and SPI components) */
2487 found = strstr(name, dev->driver->name);
2490 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2492 /* discard ID from name if ID == -1 */
2494 found[strlen(dev->driver->name)] = '\0';
2498 /* I2C component devices are named "bus-addr" */
2499 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2500 char tmp[NAME_SIZE];
2502 /* create unique ID number from I2C addr and bus */
2503 *id = ((id1 & 0xffff) << 16) + id2;
2505 /* sanitize component name for DAI link creation */
2506 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2508 strlcpy(name, tmp, NAME_SIZE);
2513 return devm_kstrdup(dev, name, GFP_KERNEL);
2517 * Simplify DAI link naming for single devices with multiple DAIs by removing
2518 * any ".-1" and using the DAI name (instead of device name).
2520 static inline char *fmt_multiple_name(struct device *dev,
2521 struct snd_soc_dai_driver *dai_drv)
2523 if (dai_drv->name == NULL) {
2525 "ASoC: error - multiple DAI %s registered with no name\n",
2530 return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2533 static void soc_del_dai(struct snd_soc_dai *dai)
2535 dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2536 list_del(&dai->list);
2539 /* Create a DAI and add it to the component's DAI list */
2540 static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2541 struct snd_soc_dai_driver *dai_drv,
2542 bool legacy_dai_naming)
2544 struct device *dev = component->dev;
2545 struct snd_soc_dai *dai;
2547 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2549 dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
2554 * Back in the old days when we still had component-less DAIs,
2555 * instead of having a static name, component-less DAIs would
2556 * inherit the name of the parent device so it is possible to
2557 * register multiple instances of the DAI. We still need to keep
2558 * the same naming style even though those DAIs are not
2559 * component-less anymore.
2561 if (legacy_dai_naming &&
2562 (dai_drv->id == 0 || dai_drv->name == NULL)) {
2563 dai->name = fmt_single_name(dev, &dai->id);
2565 dai->name = fmt_multiple_name(dev, dai_drv);
2567 dai->id = dai_drv->id;
2569 dai->id = component->num_dai;
2574 dai->component = component;
2576 dai->driver = dai_drv;
2577 if (!dai->driver->ops)
2578 dai->driver->ops = &null_dai_ops;
2580 /* see for_each_component_dais */
2581 list_add_tail(&dai->list, &component->dai_list);
2582 component->num_dai++;
2584 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2588 void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2592 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2595 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2597 * @component: The component the DAIs are registered for
2598 * @dai_drv: DAI driver to use for the DAI
2600 * Topology can use this API to register DAIs when probing a component.
2601 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2602 * will be freed in the component cleanup.
2604 struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
2605 struct snd_soc_dai_driver *dai_drv,
2606 bool legacy_dai_naming)
2608 struct device *dev = component->dev;
2610 dev_dbg(dev, "ASoC: dai register %s\n", dai_drv->name);
2612 lockdep_assert_held(&client_mutex);
2613 return soc_add_dai(component, dai_drv, legacy_dai_naming);
2615 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2618 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2620 * @component: The component for which the DAIs should be unregistered
2622 static void snd_soc_unregister_dais(struct snd_soc_component *component)
2624 struct snd_soc_dai *dai, *_dai;
2626 for_each_component_dais_safe(component, dai, _dai)
2627 snd_soc_unregister_dai(dai);
2631 * snd_soc_register_dais - Register a DAI with the ASoC core
2633 * @component: The component the DAIs are registered for
2634 * @dai_drv: DAI driver to use for the DAIs
2635 * @count: Number of DAIs
2637 static int snd_soc_register_dais(struct snd_soc_component *component,
2638 struct snd_soc_dai_driver *dai_drv,
2641 struct snd_soc_dai *dai;
2645 for (i = 0; i < count; i++) {
2646 dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
2647 !component->driver->non_legacy_dai_naming);
2657 snd_soc_unregister_dais(component);
2662 static int snd_soc_component_initialize(struct snd_soc_component *component,
2663 const struct snd_soc_component_driver *driver, struct device *dev)
2665 INIT_LIST_HEAD(&component->dai_list);
2666 INIT_LIST_HEAD(&component->dobj_list);
2667 INIT_LIST_HEAD(&component->card_list);
2668 mutex_init(&component->io_mutex);
2670 component->name = fmt_single_name(dev, &component->id);
2671 if (!component->name) {
2672 dev_err(dev, "ASoC: Failed to allocate name\n");
2676 component->dev = dev;
2677 component->driver = driver;
2682 static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2684 int val_bytes = regmap_get_val_bytes(component->regmap);
2686 /* Errors are legitimate for non-integer byte multiples */
2688 component->val_bytes = val_bytes;
2691 #ifdef CONFIG_REGMAP
2694 * snd_soc_component_init_regmap() - Initialize regmap instance for the
2696 * @component: The component for which to initialize the regmap instance
2697 * @regmap: The regmap instance that should be used by the component
2699 * This function allows deferred assignment of the regmap instance that is
2700 * associated with the component. Only use this if the regmap instance is not
2701 * yet ready when the component is registered. The function must also be called
2702 * before the first IO attempt of the component.
2704 void snd_soc_component_init_regmap(struct snd_soc_component *component,
2705 struct regmap *regmap)
2707 component->regmap = regmap;
2708 snd_soc_component_setup_regmap(component);
2710 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
2713 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
2715 * @component: The component for which to de-initialize the regmap instance
2717 * Calls regmap_exit() on the regmap instance associated to the component and
2718 * removes the regmap instance from the component.
2720 * This function should only be used if snd_soc_component_init_regmap() was used
2721 * to initialize the regmap instance.
2723 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
2725 regmap_exit(component->regmap);
2726 component->regmap = NULL;
2728 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2732 #define ENDIANNESS_MAP(name) \
2733 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2734 static u64 endianness_format_map[] = {
2735 ENDIANNESS_MAP(S16_),
2736 ENDIANNESS_MAP(U16_),
2737 ENDIANNESS_MAP(S24_),
2738 ENDIANNESS_MAP(U24_),
2739 ENDIANNESS_MAP(S32_),
2740 ENDIANNESS_MAP(U32_),
2741 ENDIANNESS_MAP(S24_3),
2742 ENDIANNESS_MAP(U24_3),
2743 ENDIANNESS_MAP(S20_3),
2744 ENDIANNESS_MAP(U20_3),
2745 ENDIANNESS_MAP(S18_3),
2746 ENDIANNESS_MAP(U18_3),
2747 ENDIANNESS_MAP(FLOAT_),
2748 ENDIANNESS_MAP(FLOAT64_),
2749 ENDIANNESS_MAP(IEC958_SUBFRAME_),
2753 * Fix up the DAI formats for endianness: codecs don't actually see
2754 * the endianness of the data but we're using the CPU format
2755 * definitions which do need to include endianness so we ensure that
2756 * codec DAIs always have both big and little endian variants set.
2758 static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2762 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2763 if (stream->formats & endianness_format_map[i])
2764 stream->formats |= endianness_format_map[i];
2767 static void snd_soc_try_rebind_card(void)
2769 struct snd_soc_card *card, *c;
2771 list_for_each_entry_safe(card, c, &unbind_card_list, list)
2772 if (!snd_soc_bind_card(card))
2773 list_del(&card->list);
2776 static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2778 struct snd_soc_card *card = component->card;
2780 snd_soc_unregister_dais(component);
2783 snd_soc_unbind_card(card, false);
2785 list_del(&component->list);
2788 int snd_soc_add_component(struct device *dev,
2789 struct snd_soc_component *component,
2790 const struct snd_soc_component_driver *component_driver,
2791 struct snd_soc_dai_driver *dai_drv,
2797 mutex_lock(&client_mutex);
2799 ret = snd_soc_component_initialize(component, component_driver, dev);
2803 if (component_driver->endianness) {
2804 for (i = 0; i < num_dai; i++) {
2805 convert_endianness_formats(&dai_drv[i].playback);
2806 convert_endianness_formats(&dai_drv[i].capture);
2810 ret = snd_soc_register_dais(component, dai_drv, num_dai);
2812 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
2816 if (!component->driver->write && !component->driver->read) {
2817 if (!component->regmap)
2818 component->regmap = dev_get_regmap(component->dev,
2820 if (component->regmap)
2821 snd_soc_component_setup_regmap(component);
2824 /* see for_each_component */
2825 list_add(&component->list, &component_list);
2829 snd_soc_del_component_unlocked(component);
2831 mutex_unlock(&client_mutex);
2834 snd_soc_try_rebind_card();
2838 EXPORT_SYMBOL_GPL(snd_soc_add_component);
2840 int snd_soc_register_component(struct device *dev,
2841 const struct snd_soc_component_driver *component_driver,
2842 struct snd_soc_dai_driver *dai_drv,
2845 struct snd_soc_component *component;
2847 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
2851 return snd_soc_add_component(dev, component, component_driver,
2854 EXPORT_SYMBOL_GPL(snd_soc_register_component);
2857 * snd_soc_unregister_component - Unregister all related component
2858 * from the ASoC core
2860 * @dev: The device to unregister
2862 void snd_soc_unregister_component(struct device *dev)
2864 struct snd_soc_component *component;
2866 mutex_lock(&client_mutex);
2868 component = snd_soc_lookup_component_nolocked(dev, NULL);
2872 snd_soc_del_component_unlocked(component);
2874 mutex_unlock(&client_mutex);
2876 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2878 /* Retrieve a card's name from device tree */
2879 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2880 const char *propname)
2882 struct device_node *np;
2886 pr_err("card->dev is not set before calling %s\n", __func__);
2890 np = card->dev->of_node;
2892 ret = of_property_read_string_index(np, propname, 0, &card->name);
2894 * EINVAL means the property does not exist. This is fine providing
2895 * card->name was previously set, which is checked later in
2896 * snd_soc_register_card.
2898 if (ret < 0 && ret != -EINVAL) {
2900 "ASoC: Property '%s' could not be read: %d\n",
2907 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2909 static const struct snd_soc_dapm_widget simple_widgets[] = {
2910 SND_SOC_DAPM_MIC("Microphone", NULL),
2911 SND_SOC_DAPM_LINE("Line", NULL),
2912 SND_SOC_DAPM_HP("Headphone", NULL),
2913 SND_SOC_DAPM_SPK("Speaker", NULL),
2916 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
2917 const char *propname)
2919 struct device_node *np = card->dev->of_node;
2920 struct snd_soc_dapm_widget *widgets;
2921 const char *template, *wname;
2922 int i, j, num_widgets, ret;
2924 num_widgets = of_property_count_strings(np, propname);
2925 if (num_widgets < 0) {
2927 "ASoC: Property '%s' does not exist\n", propname);
2930 if (num_widgets & 1) {
2932 "ASoC: Property '%s' length is not even\n", propname);
2938 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
2943 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
2947 "ASoC: Could not allocate memory for widgets\n");
2951 for (i = 0; i < num_widgets; i++) {
2952 ret = of_property_read_string_index(np, propname,
2956 "ASoC: Property '%s' index %d read error:%d\n",
2957 propname, 2 * i, ret);
2961 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
2962 if (!strncmp(template, simple_widgets[j].name,
2963 strlen(simple_widgets[j].name))) {
2964 widgets[i] = simple_widgets[j];
2969 if (j >= ARRAY_SIZE(simple_widgets)) {
2971 "ASoC: DAPM widget '%s' is not supported\n",
2976 ret = of_property_read_string_index(np, propname,
2981 "ASoC: Property '%s' index %d read error:%d\n",
2982 propname, (2 * i) + 1, ret);
2986 widgets[i].name = wname;
2989 card->of_dapm_widgets = widgets;
2990 card->num_of_dapm_widgets = num_widgets;
2994 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
2996 int snd_soc_of_get_slot_mask(struct device_node *np,
2997 const char *prop_name,
3001 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
3007 for (i = 0; i < val; i++)
3008 if (be32_to_cpup(&of_slot_mask[i]))
3013 EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
3015 int snd_soc_of_parse_tdm_slot(struct device_node *np,
3016 unsigned int *tx_mask,
3017 unsigned int *rx_mask,
3018 unsigned int *slots,
3019 unsigned int *slot_width)
3025 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3027 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3029 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3030 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3038 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3039 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3049 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3051 void snd_soc_of_parse_node_prefix(struct device_node *np,
3052 struct snd_soc_codec_conf *codec_conf,
3053 struct device_node *of_node,
3054 const char *propname)
3059 ret = of_property_read_string(np, propname, &str);
3061 /* no prefix is not error */
3065 codec_conf->of_node = of_node;
3066 codec_conf->name_prefix = str;
3068 EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
3070 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3071 const char *propname)
3073 struct device_node *np = card->dev->of_node;
3075 struct snd_soc_dapm_route *routes;
3078 num_routes = of_property_count_strings(np, propname);
3079 if (num_routes < 0 || num_routes & 1) {
3081 "ASoC: Property '%s' does not exist or its length is not even\n",
3087 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3092 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
3096 "ASoC: Could not allocate DAPM route table\n");
3100 for (i = 0; i < num_routes; i++) {
3101 ret = of_property_read_string_index(np, propname,
3102 2 * i, &routes[i].sink);
3105 "ASoC: Property '%s' index %d could not be read: %d\n",
3106 propname, 2 * i, ret);
3109 ret = of_property_read_string_index(np, propname,
3110 (2 * i) + 1, &routes[i].source);
3113 "ASoC: Property '%s' index %d could not be read: %d\n",
3114 propname, (2 * i) + 1, ret);
3119 card->num_of_dapm_routes = num_routes;
3120 card->of_dapm_routes = routes;
3124 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3126 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3128 struct device_node **bitclkmaster,
3129 struct device_node **framemaster)
3133 unsigned int format = 0;
3139 } of_fmt_table[] = {
3140 { "i2s", SND_SOC_DAIFMT_I2S },
3141 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3142 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3143 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3144 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3145 { "ac97", SND_SOC_DAIFMT_AC97 },
3146 { "pdm", SND_SOC_DAIFMT_PDM},
3147 { "msb", SND_SOC_DAIFMT_MSB },
3148 { "lsb", SND_SOC_DAIFMT_LSB },
3155 * check "dai-format = xxx"
3156 * or "[prefix]format = xxx"
3157 * SND_SOC_DAIFMT_FORMAT_MASK area
3159 ret = of_property_read_string(np, "dai-format", &str);
3161 snprintf(prop, sizeof(prop), "%sformat", prefix);
3162 ret = of_property_read_string(np, prop, &str);
3165 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3166 if (strcmp(str, of_fmt_table[i].name) == 0) {
3167 format |= of_fmt_table[i].val;
3174 * check "[prefix]continuous-clock"
3175 * SND_SOC_DAIFMT_CLOCK_MASK area
3177 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
3178 if (of_property_read_bool(np, prop))
3179 format |= SND_SOC_DAIFMT_CONT;
3181 format |= SND_SOC_DAIFMT_GATED;
3184 * check "[prefix]bitclock-inversion"
3185 * check "[prefix]frame-inversion"
3186 * SND_SOC_DAIFMT_INV_MASK area
3188 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3189 bit = !!of_get_property(np, prop, NULL);
3191 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3192 frame = !!of_get_property(np, prop, NULL);
3194 switch ((bit << 4) + frame) {
3196 format |= SND_SOC_DAIFMT_IB_IF;
3199 format |= SND_SOC_DAIFMT_IB_NF;
3202 format |= SND_SOC_DAIFMT_NB_IF;
3205 /* SND_SOC_DAIFMT_NB_NF is default */
3210 * check "[prefix]bitclock-master"
3211 * check "[prefix]frame-master"
3212 * SND_SOC_DAIFMT_MASTER_MASK area
3214 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3215 bit = !!of_get_property(np, prop, NULL);
3216 if (bit && bitclkmaster)
3217 *bitclkmaster = of_parse_phandle(np, prop, 0);
3219 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3220 frame = !!of_get_property(np, prop, NULL);
3221 if (frame && framemaster)
3222 *framemaster = of_parse_phandle(np, prop, 0);
3224 switch ((bit << 4) + frame) {
3226 format |= SND_SOC_DAIFMT_CBM_CFM;
3229 format |= SND_SOC_DAIFMT_CBM_CFS;
3232 format |= SND_SOC_DAIFMT_CBS_CFM;
3235 format |= SND_SOC_DAIFMT_CBS_CFS;
3241 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3243 int snd_soc_get_dai_id(struct device_node *ep)
3245 struct snd_soc_component *component;
3246 struct snd_soc_dai_link_component dlc;
3249 dlc.of_node = of_graph_get_port_parent(ep);
3252 * For example HDMI case, HDMI has video/sound port,
3253 * but ALSA SoC needs sound port number only.
3254 * Thus counting HDMI DT port/endpoint doesn't work.
3255 * Then, it should have .of_xlate_dai_id
3258 mutex_lock(&client_mutex);
3259 component = soc_find_component(&dlc);
3261 ret = snd_soc_component_of_xlate_dai_id(component, ep);
3262 mutex_unlock(&client_mutex);
3264 of_node_put(dlc.of_node);
3268 EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3270 int snd_soc_get_dai_name(struct of_phandle_args *args,
3271 const char **dai_name)
3273 struct snd_soc_component *pos;
3274 struct device_node *component_of_node;
3275 int ret = -EPROBE_DEFER;
3277 mutex_lock(&client_mutex);
3278 for_each_component(pos) {
3279 component_of_node = soc_component_to_node(pos);
3281 if (component_of_node != args->np)
3284 ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3285 if (ret == -ENOTSUPP) {
3286 struct snd_soc_dai *dai;
3289 switch (args->args_count) {
3291 id = 0; /* same as dai_drv[0] */
3301 if (id < 0 || id >= pos->num_dai) {
3308 /* find target DAI */
3309 for_each_component_dais(pos, dai) {
3315 *dai_name = dai->driver->name;
3317 *dai_name = pos->name;
3322 mutex_unlock(&client_mutex);
3325 EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
3327 int snd_soc_of_get_dai_name(struct device_node *of_node,
3328 const char **dai_name)
3330 struct of_phandle_args args;
3333 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3334 "#sound-dai-cells", 0, &args);
3338 ret = snd_soc_get_dai_name(&args, dai_name);
3340 of_node_put(args.np);
3344 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3347 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3348 * @dai_link: DAI link
3350 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3352 void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3354 struct snd_soc_dai_link_component *component;
3357 for_each_link_codecs(dai_link, index, component) {
3358 if (!component->of_node)
3360 of_node_put(component->of_node);
3361 component->of_node = NULL;
3364 EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3367 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3369 * @of_node: Device node
3370 * @dai_link: DAI link
3372 * Builds an array of CODEC DAI components from the DAI link property
3374 * The array is set in the DAI link and the number of DAIs is set accordingly.
3375 * The device nodes in the array (of_node) must be dereferenced by calling
3376 * snd_soc_of_put_dai_link_codecs() on @dai_link.
3378 * Returns 0 for success
3380 int snd_soc_of_get_dai_link_codecs(struct device *dev,
3381 struct device_node *of_node,
3382 struct snd_soc_dai_link *dai_link)
3384 struct of_phandle_args args;
3385 struct snd_soc_dai_link_component *component;
3387 int index, num_codecs, ret;
3389 /* Count the number of CODECs */
3391 num_codecs = of_count_phandle_with_args(of_node, name,
3392 "#sound-dai-cells");
3393 if (num_codecs <= 0) {
3394 if (num_codecs == -ENOENT)
3395 dev_err(dev, "No 'sound-dai' property\n");
3397 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3400 component = devm_kcalloc(dev,
3401 num_codecs, sizeof(*component),
3405 dai_link->codecs = component;
3406 dai_link->num_codecs = num_codecs;
3408 /* Parse the list */
3409 for_each_link_codecs(dai_link, index, component) {
3410 ret = of_parse_phandle_with_args(of_node, name,
3415 component->of_node = args.np;
3416 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3422 snd_soc_of_put_dai_link_codecs(dai_link);
3423 dai_link->codecs = NULL;
3424 dai_link->num_codecs = 0;
3427 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3429 static int __init snd_soc_init(void)
3431 snd_soc_debugfs_init();
3432 snd_soc_util_init();
3434 return platform_driver_register(&soc_driver);
3436 module_init(snd_soc_init);
3438 static void __exit snd_soc_exit(void)
3440 snd_soc_util_exit();
3441 snd_soc_debugfs_exit();
3443 platform_driver_unregister(&soc_driver);
3445 module_exit(snd_soc_exit);
3447 /* Module information */
3448 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3449 MODULE_DESCRIPTION("ALSA SoC Core");
3450 MODULE_LICENSE("GPL");
3451 MODULE_ALIAS("platform:soc-audio");