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 struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
360 const char *driver_name)
362 struct snd_soc_component *component;
363 struct snd_soc_component *found_component;
365 found_component = NULL;
366 mutex_lock(&client_mutex);
367 for_each_component(component) {
368 if ((dev == component->dev) &&
370 (driver_name == component->driver->name) ||
371 (strcmp(component->driver->name, driver_name) == 0))) {
372 found_component = component;
376 mutex_unlock(&client_mutex);
378 return found_component;
380 EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
382 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
383 const char *dai_link, int stream)
385 struct snd_soc_pcm_runtime *rtd;
387 for_each_card_rtds(card, rtd) {
388 if (rtd->dai_link->no_pcm &&
389 !strcmp(rtd->dai_link->name, dai_link))
390 return rtd->pcm->streams[stream].substream;
392 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
395 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
397 static const struct snd_soc_ops null_snd_soc_ops;
399 static void soc_release_rtd_dev(struct device *dev)
401 /* "dev" means "rtd->dev" */
405 static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
410 list_del(&rtd->list);
413 * we don't need to call kfree() for rtd->dev
415 * soc_release_rtd_dev()
417 * We don't need rtd->dev NULL check, because
418 * it is alloced *before* rtd.
420 * soc_new_pcm_runtime()
422 device_unregister(rtd->dev);
425 static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
426 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
428 struct snd_soc_pcm_runtime *rtd;
435 dev = kzalloc(sizeof(struct device), GFP_KERNEL);
439 dev->parent = card->dev;
440 dev->release = soc_release_rtd_dev;
441 dev->groups = soc_dev_attr_groups;
443 dev_set_name(dev, "%s", dai_link->name);
445 ret = device_register(dev);
447 put_device(dev); /* soc_release_rtd_dev */
454 rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL);
459 dev_set_drvdata(dev, rtd);
462 * for rtd->codec_dais
464 rtd->codec_dais = devm_kcalloc(dev, dai_link->num_codecs,
465 sizeof(struct snd_soc_dai *),
467 if (!rtd->codec_dais)
471 * rtd remaining settings
473 INIT_LIST_HEAD(&rtd->component_list);
474 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
475 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
476 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
477 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
480 rtd->dai_link = dai_link;
481 if (!rtd->dai_link->ops)
482 rtd->dai_link->ops = &null_snd_soc_ops;
484 /* see for_each_card_rtds */
485 list_add_tail(&rtd->list, &card->rtd_list);
486 rtd->num = card->num_rtd;
492 soc_free_pcm_runtime(rtd);
496 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
497 const char *dai_link)
499 struct snd_soc_pcm_runtime *rtd;
501 for_each_card_rtds(card, rtd) {
502 if (!strcmp(rtd->dai_link->name, dai_link))
505 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
508 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
510 static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
512 struct snd_soc_pcm_runtime *rtd;
514 for_each_card_rtds(card, rtd)
515 flush_delayed_work(&rtd->delayed_work);
518 #ifdef CONFIG_PM_SLEEP
519 /* powers down audio subsystem for suspend */
520 int snd_soc_suspend(struct device *dev)
522 struct snd_soc_card *card = dev_get_drvdata(dev);
523 struct snd_soc_component *component;
524 struct snd_soc_pcm_runtime *rtd;
527 /* If the card is not initialized yet there is nothing to do */
528 if (!card->instantiated)
532 * Due to the resume being scheduled into a workqueue we could
533 * suspend before that's finished - wait for it to complete.
535 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
537 /* we're going to block userspace touching us until resume completes */
538 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
540 /* mute any active DACs */
541 for_each_card_rtds(card, rtd) {
542 struct snd_soc_dai *dai;
544 if (rtd->dai_link->ignore_suspend)
547 for_each_rtd_codec_dai(rtd, i, dai) {
548 if (dai->playback_active)
549 snd_soc_dai_digital_mute(dai, 1,
550 SNDRV_PCM_STREAM_PLAYBACK);
554 /* suspend all pcms */
555 for_each_card_rtds(card, rtd) {
556 if (rtd->dai_link->ignore_suspend)
559 snd_pcm_suspend_all(rtd->pcm);
562 if (card->suspend_pre)
563 card->suspend_pre(card);
565 for_each_card_rtds(card, rtd) {
566 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
568 if (rtd->dai_link->ignore_suspend)
571 if (!cpu_dai->driver->bus_control)
572 snd_soc_dai_suspend(cpu_dai);
575 /* close any waiting streams */
576 snd_soc_flush_all_delayed_work(card);
578 for_each_card_rtds(card, rtd) {
580 if (rtd->dai_link->ignore_suspend)
583 snd_soc_dapm_stream_event(rtd,
584 SNDRV_PCM_STREAM_PLAYBACK,
585 SND_SOC_DAPM_STREAM_SUSPEND);
587 snd_soc_dapm_stream_event(rtd,
588 SNDRV_PCM_STREAM_CAPTURE,
589 SND_SOC_DAPM_STREAM_SUSPEND);
592 /* Recheck all endpoints too, their state is affected by suspend */
593 dapm_mark_endpoints_dirty(card);
594 snd_soc_dapm_sync(&card->dapm);
596 /* suspend all COMPONENTs */
597 for_each_card_components(card, component) {
598 struct snd_soc_dapm_context *dapm =
599 snd_soc_component_get_dapm(component);
602 * If there are paths active then the COMPONENT will be held
603 * with bias _ON and should not be suspended.
605 if (!snd_soc_component_is_suspended(component)) {
606 switch (snd_soc_dapm_get_bias_level(dapm)) {
607 case SND_SOC_BIAS_STANDBY:
609 * If the COMPONENT is capable of idle
610 * bias off then being in STANDBY
611 * means it's doing something,
612 * otherwise fall through.
614 if (dapm->idle_bias_off) {
615 dev_dbg(component->dev,
616 "ASoC: idle_bias_off CODEC on over suspend\n");
621 case SND_SOC_BIAS_OFF:
622 snd_soc_component_suspend(component);
623 if (component->regmap)
624 regcache_mark_dirty(component->regmap);
625 /* deactivate pins to sleep state */
626 pinctrl_pm_select_sleep_state(component->dev);
629 dev_dbg(component->dev,
630 "ASoC: COMPONENT is on over suspend\n");
636 for_each_card_rtds(card, rtd) {
637 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
639 if (rtd->dai_link->ignore_suspend)
642 if (cpu_dai->driver->bus_control)
643 snd_soc_dai_suspend(cpu_dai);
645 /* deactivate pins to sleep state */
646 pinctrl_pm_select_sleep_state(cpu_dai->dev);
649 if (card->suspend_post)
650 card->suspend_post(card);
654 EXPORT_SYMBOL_GPL(snd_soc_suspend);
657 * deferred resume work, so resume can complete before we finished
658 * setting our codec back up, which can be very slow on I2C
660 static void soc_resume_deferred(struct work_struct *work)
662 struct snd_soc_card *card =
663 container_of(work, struct snd_soc_card,
664 deferred_resume_work);
665 struct snd_soc_pcm_runtime *rtd;
666 struct snd_soc_component *component;
670 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
671 * so userspace apps are blocked from touching us
674 dev_dbg(card->dev, "ASoC: starting resume work\n");
676 /* Bring us up into D2 so that DAPM starts enabling things */
677 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
679 if (card->resume_pre)
680 card->resume_pre(card);
682 /* resume control bus DAIs */
683 for_each_card_rtds(card, rtd) {
684 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
686 if (rtd->dai_link->ignore_suspend)
689 if (cpu_dai->driver->bus_control)
690 snd_soc_dai_resume(cpu_dai);
693 for_each_card_components(card, component) {
694 if (snd_soc_component_is_suspended(component))
695 snd_soc_component_resume(component);
698 for_each_card_rtds(card, rtd) {
700 if (rtd->dai_link->ignore_suspend)
703 snd_soc_dapm_stream_event(rtd,
704 SNDRV_PCM_STREAM_PLAYBACK,
705 SND_SOC_DAPM_STREAM_RESUME);
707 snd_soc_dapm_stream_event(rtd,
708 SNDRV_PCM_STREAM_CAPTURE,
709 SND_SOC_DAPM_STREAM_RESUME);
712 /* unmute any active DACs */
713 for_each_card_rtds(card, rtd) {
714 struct snd_soc_dai *dai;
716 if (rtd->dai_link->ignore_suspend)
719 for_each_rtd_codec_dai(rtd, i, dai) {
720 if (dai->playback_active)
721 snd_soc_dai_digital_mute(dai, 0,
722 SNDRV_PCM_STREAM_PLAYBACK);
726 for_each_card_rtds(card, rtd) {
727 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
729 if (rtd->dai_link->ignore_suspend)
732 if (!cpu_dai->driver->bus_control)
733 snd_soc_dai_resume(cpu_dai);
736 if (card->resume_post)
737 card->resume_post(card);
739 dev_dbg(card->dev, "ASoC: resume work completed\n");
741 /* Recheck all endpoints too, their state is affected by suspend */
742 dapm_mark_endpoints_dirty(card);
743 snd_soc_dapm_sync(&card->dapm);
745 /* userspace can access us now we are back as we were before */
746 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
749 /* powers up audio subsystem after a suspend */
750 int snd_soc_resume(struct device *dev)
752 struct snd_soc_card *card = dev_get_drvdata(dev);
753 bool bus_control = false;
754 struct snd_soc_pcm_runtime *rtd;
755 struct snd_soc_dai *codec_dai;
758 /* If the card is not initialized yet there is nothing to do */
759 if (!card->instantiated)
762 /* activate pins from sleep state */
763 for_each_card_rtds(card, rtd) {
764 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
767 pinctrl_pm_select_default_state(cpu_dai->dev);
769 for_each_rtd_codec_dai(rtd, i, codec_dai) {
770 if (codec_dai->active)
771 pinctrl_pm_select_default_state(codec_dai->dev);
776 * DAIs that also act as the control bus master might have other drivers
777 * hanging off them so need to resume immediately. Other drivers don't
778 * have that problem and may take a substantial amount of time to resume
779 * due to I/O costs and anti-pop so handle them out of line.
781 for_each_card_rtds(card, rtd) {
782 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
784 bus_control |= cpu_dai->driver->bus_control;
787 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
788 soc_resume_deferred(&card->deferred_resume_work);
790 dev_dbg(dev, "ASoC: Scheduling resume work\n");
791 if (!schedule_work(&card->deferred_resume_work))
792 dev_err(dev, "ASoC: resume work item may be lost\n");
797 EXPORT_SYMBOL_GPL(snd_soc_resume);
799 static void soc_resume_init(struct snd_soc_card *card)
801 /* deferred resume work */
802 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
805 #define snd_soc_suspend NULL
806 #define snd_soc_resume NULL
807 static inline void soc_resume_init(struct snd_soc_card *card)
812 static const struct snd_soc_dai_ops null_dai_ops = {
815 static struct device_node
816 *soc_component_to_node(struct snd_soc_component *component)
818 struct device_node *of_node;
820 of_node = component->dev->of_node;
821 if (!of_node && component->dev->parent)
822 of_node = component->dev->parent->of_node;
827 static int snd_soc_is_matching_component(
828 const struct snd_soc_dai_link_component *dlc,
829 struct snd_soc_component *component)
831 struct device_node *component_of_node;
836 component_of_node = soc_component_to_node(component);
838 if (dlc->of_node && component_of_node != dlc->of_node)
840 if (dlc->name && strcmp(component->name, dlc->name))
846 static struct snd_soc_component *soc_find_component(
847 const struct snd_soc_dai_link_component *dlc)
849 struct snd_soc_component *component;
851 lockdep_assert_held(&client_mutex);
856 * It returns *1st* found component, but some driver
857 * has few components by same of_node/name
859 * CPU component and generic DMAEngine component
861 for_each_component(component)
862 if (snd_soc_is_matching_component(dlc, component))
869 * snd_soc_find_dai - Find a registered DAI
871 * @dlc: name of the DAI or the DAI driver and optional component info to match
873 * This function will search all registered components and their DAIs to
874 * find the DAI of the same name. The component's of_node and name
875 * should also match if being specified.
877 * Return: pointer of DAI, or NULL if not found.
879 struct snd_soc_dai *snd_soc_find_dai(
880 const struct snd_soc_dai_link_component *dlc)
882 struct snd_soc_component *component;
883 struct snd_soc_dai *dai;
885 lockdep_assert_held(&client_mutex);
887 /* Find CPU DAI from registered DAIs */
888 for_each_component(component) {
889 if (!snd_soc_is_matching_component(dlc, component))
891 for_each_component_dais(component, dai) {
892 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
893 && (!dai->driver->name
894 || strcmp(dai->driver->name, dlc->dai_name)))
903 EXPORT_SYMBOL_GPL(snd_soc_find_dai);
906 * snd_soc_find_dai_link - Find a DAI link
909 * @id: DAI link ID to match
910 * @name: DAI link name to match, optional
911 * @stream_name: DAI link stream name to match, optional
913 * This function will search all existing DAI links of the soc card to
914 * find the link of the same ID. Since DAI links may not have their
915 * unique ID, so name and stream name should also match if being
918 * Return: pointer of DAI link, or NULL if not found.
920 struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
921 int id, const char *name,
922 const char *stream_name)
924 struct snd_soc_dai_link *link;
926 lockdep_assert_held(&client_mutex);
928 for_each_card_links(card, link) {
932 if (name && (!link->name || strcmp(name, link->name)))
935 if (stream_name && (!link->stream_name
936 || strcmp(stream_name, link->stream_name)))
944 EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
946 static bool soc_is_dai_link_bound(struct snd_soc_card *card,
947 struct snd_soc_dai_link *dai_link)
949 struct snd_soc_pcm_runtime *rtd;
951 for_each_card_rtds(card, rtd) {
952 if (rtd->dai_link == dai_link)
959 static int soc_dai_link_sanity_check(struct snd_soc_card *card,
960 struct snd_soc_dai_link *link)
963 struct snd_soc_dai_link_component *codec, *platform;
965 for_each_link_codecs(link, i, codec) {
967 * Codec must be specified by 1 of name or OF node,
968 * not both or neither.
970 if (!!codec->name == !!codec->of_node) {
971 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
976 /* Codec DAI name must be specified */
977 if (!codec->dai_name) {
978 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
984 * Defer card registration if codec component is not added to
987 if (!soc_find_component(codec))
988 return -EPROBE_DEFER;
991 for_each_link_platforms(link, i, platform) {
993 * Platform may be specified by either name or OF node, but it
994 * can be left unspecified, then no components will be inserted
997 if (!!platform->name == !!platform->of_node) {
999 "ASoC: Neither/both platform name/of_node are set for %s\n",
1005 * Defer card registration if platform component is not added to
1008 if (!soc_find_component(platform))
1009 return -EPROBE_DEFER;
1013 if (link->num_cpus > 1) {
1015 "ASoC: multi cpu is not yet supported %s\n",
1021 * CPU device may be specified by either name or OF node, but
1022 * can be left unspecified, and will be matched based on DAI
1025 if (link->cpus->name && link->cpus->of_node) {
1027 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1033 * Defer card registration if cpu dai component is not added to
1036 if ((link->cpus->of_node || link->cpus->name) &&
1037 !soc_find_component(link->cpus))
1038 return -EPROBE_DEFER;
1041 * At least one of CPU DAI name or CPU device name/node must be
1044 if (!link->cpus->dai_name &&
1045 !(link->cpus->name || link->cpus->of_node)) {
1047 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1055 static void soc_unbind_dai_link(struct snd_soc_card *card,
1056 struct snd_soc_dai_link *dai_link)
1058 struct snd_soc_pcm_runtime *rtd;
1060 rtd = snd_soc_get_pcm_runtime(card, dai_link->name);
1062 soc_free_pcm_runtime(rtd);
1065 static int soc_bind_dai_link(struct snd_soc_card *card,
1066 struct snd_soc_dai_link *dai_link)
1068 struct snd_soc_pcm_runtime *rtd;
1069 struct snd_soc_dai_link_component *codec, *platform;
1070 struct snd_soc_component *component;
1073 if (dai_link->ignore)
1076 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
1078 if (soc_is_dai_link_bound(card, dai_link)) {
1079 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
1084 ret = soc_dai_link_sanity_check(card, dai_link);
1088 rtd = soc_new_pcm_runtime(card, dai_link);
1092 /* FIXME: we need multi CPU support in the future */
1093 rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
1094 if (!rtd->cpu_dai) {
1095 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
1096 dai_link->cpus->dai_name);
1099 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
1101 /* Find CODEC from registered CODECs */
1102 rtd->num_codecs = dai_link->num_codecs;
1103 for_each_link_codecs(dai_link, i, codec) {
1104 rtd->codec_dais[i] = snd_soc_find_dai(codec);
1105 if (!rtd->codec_dais[i]) {
1106 dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
1111 snd_soc_rtdcom_add(rtd, rtd->codec_dais[i]->component);
1114 /* Single codec links expect codec and codec_dai in runtime data */
1115 rtd->codec_dai = rtd->codec_dais[0];
1117 /* Find PLATFORM from registered PLATFORMs */
1118 for_each_link_platforms(dai_link, i, platform) {
1119 for_each_component(component) {
1120 if (!snd_soc_is_matching_component(platform, component))
1123 snd_soc_rtdcom_add(rtd, component);
1130 soc_free_pcm_runtime(rtd);
1131 return -EPROBE_DEFER;
1134 static void soc_set_of_name_prefix(struct snd_soc_component *component)
1136 struct device_node *of_node = soc_component_to_node(component);
1140 ret = of_property_read_string(of_node, "sound-name-prefix", &str);
1142 component->name_prefix = str;
1145 static void soc_set_name_prefix(struct snd_soc_card *card,
1146 struct snd_soc_component *component)
1150 for (i = 0; i < card->num_configs && card->codec_conf; i++) {
1151 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1152 struct device_node *of_node = soc_component_to_node(component);
1154 if (map->of_node && of_node != map->of_node)
1156 if (map->dev_name && strcmp(component->name, map->dev_name))
1158 component->name_prefix = map->name_prefix;
1163 * If there is no configuration table or no match in the table,
1164 * check if a prefix is provided in the node
1166 soc_set_of_name_prefix(component);
1169 static void soc_cleanup_component(struct snd_soc_component *component)
1171 /* For framework level robustness */
1172 snd_soc_component_set_jack(component, NULL, NULL);
1174 list_del_init(&component->card_list);
1175 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1176 soc_cleanup_component_debugfs(component);
1177 component->card = NULL;
1178 snd_soc_component_module_put_when_remove(component);
1181 static void soc_remove_component(struct snd_soc_component *component)
1183 if (!component->card)
1186 snd_soc_component_remove(component);
1188 soc_cleanup_component(component);
1191 static int soc_probe_component(struct snd_soc_card *card,
1192 struct snd_soc_component *component)
1194 struct snd_soc_dapm_context *dapm =
1195 snd_soc_component_get_dapm(component);
1196 struct snd_soc_dai *dai;
1199 if (!strcmp(component->name, "snd-soc-dummy"))
1202 if (component->card) {
1203 if (component->card != card) {
1204 dev_err(component->dev,
1205 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1206 card->name, component->card->name);
1212 ret = snd_soc_component_module_get_when_probe(component);
1216 component->card = card;
1217 soc_set_name_prefix(card, component);
1219 soc_init_component_debugfs(component);
1221 snd_soc_dapm_init(dapm, card, component);
1223 ret = snd_soc_dapm_new_controls(dapm,
1224 component->driver->dapm_widgets,
1225 component->driver->num_dapm_widgets);
1228 dev_err(component->dev,
1229 "Failed to create new controls %d\n", ret);
1233 for_each_component_dais(component, dai) {
1234 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1236 dev_err(component->dev,
1237 "Failed to create DAI widgets %d\n", ret);
1242 ret = snd_soc_component_probe(component);
1244 dev_err(component->dev,
1245 "ASoC: failed to probe component %d\n", ret);
1248 WARN(dapm->idle_bias_off &&
1249 dapm->bias_level != SND_SOC_BIAS_OFF,
1250 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1253 /* machine specific init */
1254 if (component->init) {
1255 ret = component->init(component);
1257 dev_err(component->dev,
1258 "Failed to do machine specific init %d\n", ret);
1263 ret = snd_soc_add_component_controls(component,
1264 component->driver->controls,
1265 component->driver->num_controls);
1269 ret = snd_soc_dapm_add_routes(dapm,
1270 component->driver->dapm_routes,
1271 component->driver->num_dapm_routes);
1275 /* see for_each_card_components */
1276 list_add(&component->card_list, &card->component_dev_list);
1280 soc_cleanup_component(component);
1285 static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1289 if (!dai || !dai->probed || !dai->driver ||
1290 dai->driver->remove_order != order)
1293 err = snd_soc_dai_remove(dai);
1296 "ASoC: failed to remove %s: %d\n",
1302 static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1307 dai->driver->probe_order != order)
1310 ret = snd_soc_dai_probe(dai);
1312 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1322 static void soc_remove_link_dais(struct snd_soc_card *card)
1325 struct snd_soc_dai *codec_dai;
1326 struct snd_soc_pcm_runtime *rtd;
1329 for_each_comp_order(order) {
1330 for_each_card_rtds(card, rtd) {
1331 /* remove the CODEC DAI */
1332 for_each_rtd_codec_dai(rtd, i, codec_dai)
1333 soc_remove_dai(codec_dai, order);
1335 soc_remove_dai(rtd->cpu_dai, order);
1340 static int soc_probe_link_dais(struct snd_soc_card *card)
1342 struct snd_soc_dai *codec_dai;
1343 struct snd_soc_pcm_runtime *rtd;
1346 for_each_comp_order(order) {
1347 for_each_card_rtds(card, rtd) {
1350 "ASoC: probe %s dai link %d late %d\n",
1351 card->name, rtd->num, order);
1353 ret = soc_probe_dai(rtd->cpu_dai, order);
1357 /* probe the CODEC DAI */
1358 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1359 ret = soc_probe_dai(codec_dai, order);
1369 static void soc_remove_link_components(struct snd_soc_card *card)
1371 struct snd_soc_component *component;
1372 struct snd_soc_pcm_runtime *rtd;
1373 struct snd_soc_rtdcom_list *rtdcom;
1376 for_each_comp_order(order) {
1377 for_each_card_rtds(card, rtd) {
1378 for_each_rtd_components(rtd, rtdcom, component) {
1379 if (component->driver->remove_order != order)
1382 soc_remove_component(component);
1388 static int soc_probe_link_components(struct snd_soc_card *card)
1390 struct snd_soc_component *component;
1391 struct snd_soc_pcm_runtime *rtd;
1392 struct snd_soc_rtdcom_list *rtdcom;
1395 for_each_comp_order(order) {
1396 for_each_card_rtds(card, rtd) {
1397 for_each_rtd_components(rtd, rtdcom, component) {
1398 if (component->driver->probe_order != order)
1401 ret = soc_probe_component(card, component);
1411 void snd_soc_disconnect_sync(struct device *dev)
1413 struct snd_soc_component *component =
1414 snd_soc_lookup_component(dev, NULL);
1416 if (!component || !component->card)
1419 snd_card_disconnect_sync(component->card->snd_card);
1421 EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
1424 * snd_soc_add_dai_link - Add a DAI link dynamically
1425 * @card: The ASoC card to which the DAI link is added
1426 * @dai_link: The new DAI link to add
1428 * This function adds a DAI link to the ASoC card's link list.
1430 * Note: Topology can use this API to add DAI links when probing the
1431 * topology component. And machine drivers can still define static
1432 * DAI links in dai_link array.
1434 int snd_soc_add_dai_link(struct snd_soc_card *card,
1435 struct snd_soc_dai_link *dai_link)
1439 if (dai_link->dobj.type
1440 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1441 dev_err(card->dev, "Invalid dai link type %d\n",
1442 dai_link->dobj.type);
1446 lockdep_assert_held(&client_mutex);
1448 * Notify the machine driver for extra initialization
1449 * on the link created by topology.
1451 if (dai_link->dobj.type && card->add_dai_link)
1452 card->add_dai_link(card, dai_link);
1454 ret = soc_bind_dai_link(card, dai_link);
1458 /* see for_each_card_links */
1459 list_add_tail(&dai_link->list, &card->dai_link_list);
1463 EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1466 * snd_soc_remove_dai_link - Remove a DAI link from the list
1467 * @card: The ASoC card that owns the link
1468 * @dai_link: The DAI link to remove
1470 * This function removes a DAI link from the ASoC card's link list.
1472 * For DAI links previously added by topology, topology should
1473 * remove them by using the dobj embedded in the link.
1475 void snd_soc_remove_dai_link(struct snd_soc_card *card,
1476 struct snd_soc_dai_link *dai_link)
1478 if (dai_link->dobj.type
1479 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1480 dev_err(card->dev, "Invalid dai link type %d\n",
1481 dai_link->dobj.type);
1485 lockdep_assert_held(&client_mutex);
1487 * Notify the machine driver for extra destruction
1488 * on the link created by topology.
1490 if (dai_link->dobj.type && card->remove_dai_link)
1491 card->remove_dai_link(card, dai_link);
1493 list_del(&dai_link->list);
1495 soc_unbind_dai_link(card, dai_link);
1497 EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1499 static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1500 struct snd_soc_pcm_runtime *rtd)
1504 for (i = 0; i < num_dais; ++i) {
1505 struct snd_soc_dai_driver *drv = dais[i]->driver;
1508 ret = drv->pcm_new(rtd, dais[i]);
1510 dev_err(dais[i]->dev,
1511 "ASoC: Failed to bind %s with pcm device\n",
1520 static int soc_link_init(struct snd_soc_card *card,
1521 struct snd_soc_pcm_runtime *rtd)
1523 struct snd_soc_dai_link *dai_link = rtd->dai_link;
1524 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1525 struct snd_soc_rtdcom_list *rtdcom;
1526 struct snd_soc_component *component;
1529 /* set default power off timeout */
1530 rtd->pmdown_time = pmdown_time;
1532 /* do machine specific initialization */
1533 if (dai_link->init) {
1534 ret = dai_link->init(rtd);
1536 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1537 dai_link->name, ret);
1542 if (dai_link->dai_fmt) {
1543 ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1548 /* add DPCM sysfs entries */
1549 soc_dpcm_debugfs_add(rtd);
1554 * most drivers will register their PCMs using DAI link ordering but
1555 * topology based drivers can use the DAI link id field to set PCM
1556 * device number and then use rtd + a base offset of the BEs.
1558 for_each_rtd_components(rtd, rtdcom, component) {
1559 if (!component->driver->use_dai_pcm_id)
1562 if (rtd->dai_link->no_pcm)
1563 num += component->driver->be_pcm_base;
1565 num = rtd->dai_link->id;
1568 /* create compress_device if possible */
1569 ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1570 if (ret != -ENOTSUPP) {
1572 dev_err(card->dev, "ASoC: can't create compress %s\n",
1573 dai_link->stream_name);
1577 /* create the pcm */
1578 ret = soc_new_pcm(rtd, num);
1580 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1581 dai_link->stream_name, ret);
1584 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1587 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1588 rtd->num_codecs, rtd);
1592 static void soc_unbind_aux_dev(struct snd_soc_card *card)
1594 struct snd_soc_component *component, *_component;
1596 for_each_card_auxs_safe(card, component, _component) {
1597 component->init = NULL;
1598 list_del(&component->card_aux_list);
1602 static int soc_bind_aux_dev(struct snd_soc_card *card)
1604 struct snd_soc_component *component;
1605 struct snd_soc_aux_dev *aux;
1608 for_each_card_pre_auxs(card, i, aux) {
1609 /* codecs, usually analog devices */
1610 component = soc_find_component(&aux->dlc);
1612 return -EPROBE_DEFER;
1614 component->init = aux->init;
1615 /* see for_each_card_auxs */
1616 list_add(&component->card_aux_list, &card->aux_comp_list);
1621 static int soc_probe_aux_devices(struct snd_soc_card *card)
1623 struct snd_soc_component *comp;
1627 for_each_comp_order(order) {
1628 for_each_card_auxs(card, comp) {
1629 if (comp->driver->probe_order == order) {
1630 ret = soc_probe_component(card, comp);
1633 "ASoC: failed to probe aux component %s %d\n",
1644 static void soc_remove_aux_devices(struct snd_soc_card *card)
1646 struct snd_soc_component *comp, *_comp;
1649 for_each_comp_order(order) {
1650 for_each_card_auxs_safe(card, comp, _comp) {
1651 if (comp->driver->remove_order == order)
1652 soc_remove_component(comp);
1658 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1659 * @rtd: The runtime for which the DAI link format should be changed
1660 * @dai_fmt: The new DAI link format
1662 * This function updates the DAI link format for all DAIs connected to the DAI
1663 * link for the specified runtime.
1665 * Note: For setups with a static format set the dai_fmt field in the
1666 * corresponding snd_dai_link struct instead of using this function.
1668 * Returns 0 on success, otherwise a negative error code.
1670 int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1671 unsigned int dai_fmt)
1673 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1674 struct snd_soc_dai *codec_dai;
1678 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1679 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1680 if (ret != 0 && ret != -ENOTSUPP) {
1681 dev_warn(codec_dai->dev,
1682 "ASoC: Failed to set DAI format: %d\n", ret);
1688 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1689 * the component which has non_legacy_dai_naming is Codec
1691 if (cpu_dai->component->driver->non_legacy_dai_naming) {
1692 unsigned int inv_dai_fmt;
1694 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1695 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1696 case SND_SOC_DAIFMT_CBM_CFM:
1697 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1699 case SND_SOC_DAIFMT_CBM_CFS:
1700 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1702 case SND_SOC_DAIFMT_CBS_CFM:
1703 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1705 case SND_SOC_DAIFMT_CBS_CFS:
1706 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1710 dai_fmt = inv_dai_fmt;
1713 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1714 if (ret != 0 && ret != -ENOTSUPP) {
1715 dev_warn(cpu_dai->dev,
1716 "ASoC: Failed to set DAI format: %d\n", ret);
1722 EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1726 * Trim special characters, and replace '-' with '_' since '-' is used to
1727 * separate different DMI fields in the card long name. Only number and
1728 * alphabet characters and a few separator characters are kept.
1730 static void cleanup_dmi_name(char *name)
1734 for (i = 0; name[i]; i++) {
1735 if (isalnum(name[i]) || (name[i] == '.')
1736 || (name[i] == '_'))
1737 name[j++] = name[i];
1738 else if (name[i] == '-')
1746 * Check if a DMI field is valid, i.e. not containing any string
1747 * in the black list.
1749 static int is_dmi_valid(const char *field)
1753 while (dmi_blacklist[i]) {
1754 if (strstr(field, dmi_blacklist[i]))
1763 * snd_soc_set_dmi_name() - Register DMI names to card
1764 * @card: The card to register DMI names
1765 * @flavour: The flavour "differentiator" for the card amongst its peers.
1767 * An Intel machine driver may be used by many different devices but are
1768 * difficult for userspace to differentiate, since machine drivers ususally
1769 * use their own name as the card short name and leave the card long name
1770 * blank. To differentiate such devices and fix bugs due to lack of
1771 * device-specific configurations, this function allows DMI info to be used
1772 * as the sound card long name, in the format of
1773 * "vendor-product-version-board"
1774 * (Character '-' is used to separate different DMI fields here).
1775 * This will help the user space to load the device-specific Use Case Manager
1776 * (UCM) configurations for the card.
1778 * Possible card long names may be:
1779 * DellInc.-XPS139343-01-0310JH
1780 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1781 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1783 * This function also supports flavoring the card longname to provide
1784 * the extra differentiation, like "vendor-product-version-board-flavor".
1786 * We only keep number and alphabet characters and a few separator characters
1787 * in the card long name since UCM in the user space uses the card long names
1788 * as card configuration directory names and AudoConf cannot support special
1789 * charactors like SPACE.
1791 * Returns 0 on success, otherwise a negative error code.
1793 int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1795 const char *vendor, *product, *product_version, *board;
1796 size_t longname_buf_size = sizeof(card->snd_card->longname);
1799 if (card->long_name)
1800 return 0; /* long name already set by driver or from DMI */
1802 /* make up dmi long name as: vendor.product.version.board */
1803 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1804 if (!vendor || !is_dmi_valid(vendor)) {
1805 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1809 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1811 cleanup_dmi_name(card->dmi_longname);
1813 product = dmi_get_system_info(DMI_PRODUCT_NAME);
1814 if (product && is_dmi_valid(product)) {
1815 len = strlen(card->dmi_longname);
1816 snprintf(card->dmi_longname + len,
1817 longname_buf_size - len,
1820 len++; /* skip the separator "-" */
1821 if (len < longname_buf_size)
1822 cleanup_dmi_name(card->dmi_longname + len);
1825 * some vendors like Lenovo may only put a self-explanatory
1826 * name in the product version field
1828 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
1829 if (product_version && is_dmi_valid(product_version)) {
1830 len = strlen(card->dmi_longname);
1831 snprintf(card->dmi_longname + len,
1832 longname_buf_size - len,
1833 "-%s", product_version);
1836 if (len < longname_buf_size)
1837 cleanup_dmi_name(card->dmi_longname + len);
1841 board = dmi_get_system_info(DMI_BOARD_NAME);
1842 if (board && is_dmi_valid(board)) {
1843 len = strlen(card->dmi_longname);
1844 snprintf(card->dmi_longname + len,
1845 longname_buf_size - len,
1849 if (len < longname_buf_size)
1850 cleanup_dmi_name(card->dmi_longname + len);
1851 } else if (!product) {
1852 /* fall back to using legacy name */
1853 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1857 /* Add flavour to dmi long name */
1859 len = strlen(card->dmi_longname);
1860 snprintf(card->dmi_longname + len,
1861 longname_buf_size - len,
1865 if (len < longname_buf_size)
1866 cleanup_dmi_name(card->dmi_longname + len);
1869 /* set the card long name */
1870 card->long_name = card->dmi_longname;
1874 EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
1875 #endif /* CONFIG_DMI */
1877 static void soc_check_tplg_fes(struct snd_soc_card *card)
1879 struct snd_soc_component *component;
1880 const struct snd_soc_component_driver *comp_drv;
1881 struct snd_soc_dai_link *dai_link;
1884 for_each_component(component) {
1886 /* does this component override BEs ? */
1887 if (!component->driver->ignore_machine)
1890 /* for this machine ? */
1891 if (!strcmp(component->driver->ignore_machine,
1892 card->dev->driver->name))
1894 if (strcmp(component->driver->ignore_machine,
1895 dev_name(card->dev)))
1898 /* machine matches, so override the rtd data */
1899 for_each_card_prelinks(card, i, dai_link) {
1901 /* ignore this FE */
1902 if (dai_link->dynamic) {
1903 dai_link->ignore = true;
1907 dev_info(card->dev, "info: override BE DAI link %s\n",
1908 card->dai_link[i].name);
1910 /* override platform component */
1911 if (!dai_link->platforms) {
1912 dev_err(card->dev, "init platform error");
1915 dai_link->platforms->name = component->name;
1917 /* convert non BE into BE */
1918 dai_link->no_pcm = 1;
1920 /* override any BE fixups */
1921 dai_link->be_hw_params_fixup =
1922 component->driver->be_hw_params_fixup;
1925 * most BE links don't set stream name, so set it to
1926 * dai link name if it's NULL to help bind widgets.
1928 if (!dai_link->stream_name)
1929 dai_link->stream_name = dai_link->name;
1932 /* Inform userspace we are using alternate topology */
1933 if (component->driver->topology_name_prefix) {
1935 /* topology shortname created? */
1936 if (!card->topology_shortname_created) {
1937 comp_drv = component->driver;
1939 snprintf(card->topology_shortname, 32, "%s-%s",
1940 comp_drv->topology_name_prefix,
1942 card->topology_shortname_created = true;
1945 /* use topology shortname */
1946 card->name = card->topology_shortname;
1951 #define soc_setup_card_name(name, name1, name2, norm) \
1952 __soc_setup_card_name(name, sizeof(name), name1, name2, norm)
1953 static void __soc_setup_card_name(char *name, int len,
1954 const char *name1, const char *name2,
1959 snprintf(name, len, "%s", name1 ? name1 : name2);
1965 * Name normalization
1967 * The driver name is somewhat special, as it's used as a key for
1968 * searches in the user-space.
1971 * "abcd??efg" -> "abcd__efg"
1973 for (i = 0; i < len; i++) {
1980 if (!isalnum(name[i]))
1987 static void soc_cleanup_card_resources(struct snd_soc_card *card)
1989 struct snd_soc_dai_link *link, *_link;
1991 /* free the ALSA card at first; this syncs with pending operations */
1992 if (card->snd_card) {
1993 snd_card_free(card->snd_card);
1994 card->snd_card = NULL;
1997 /* remove and free each DAI */
1998 soc_remove_link_dais(card);
1999 soc_remove_link_components(card);
2001 for_each_card_links_safe(card, link, _link)
2002 snd_soc_remove_dai_link(card, link);
2004 /* remove auxiliary devices */
2005 soc_remove_aux_devices(card);
2006 soc_unbind_aux_dev(card);
2008 snd_soc_dapm_free(&card->dapm);
2009 soc_cleanup_card_debugfs(card);
2011 /* remove the card */
2016 static int snd_soc_instantiate_card(struct snd_soc_card *card)
2018 struct snd_soc_pcm_runtime *rtd;
2019 struct snd_soc_dai_link *dai_link;
2022 mutex_lock(&client_mutex);
2023 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
2025 snd_soc_dapm_init(&card->dapm, card, NULL);
2027 /* check whether any platform is ignore machine FE and using topology */
2028 soc_check_tplg_fes(card);
2030 /* bind aux_devs too */
2031 ret = soc_bind_aux_dev(card);
2035 /* add predefined DAI links to the list */
2037 for_each_card_prelinks(card, i, dai_link) {
2038 ret = snd_soc_add_dai_link(card, dai_link);
2043 /* card bind complete so register a sound card */
2044 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2045 card->owner, 0, &card->snd_card);
2048 "ASoC: can't create sound card for card %s: %d\n",
2053 soc_init_card_debugfs(card);
2055 soc_resume_init(card);
2057 ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2058 card->num_dapm_widgets);
2062 ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2063 card->num_of_dapm_widgets);
2067 /* initialise the sound card only once */
2069 ret = card->probe(card);
2074 /* probe all components used by DAI links on this card */
2075 ret = soc_probe_link_components(card);
2078 "ASoC: failed to instantiate card %d\n", ret);
2082 /* probe auxiliary components */
2083 ret = soc_probe_aux_devices(card);
2087 /* probe all DAI links on this card */
2088 ret = soc_probe_link_dais(card);
2091 "ASoC: failed to instantiate card %d\n", ret);
2095 for_each_card_rtds(card, rtd)
2096 soc_link_init(card, rtd);
2098 snd_soc_dapm_link_dai_widgets(card);
2099 snd_soc_dapm_connect_dai_link_widgets(card);
2101 ret = snd_soc_add_card_controls(card, card->controls,
2102 card->num_controls);
2106 ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2107 card->num_dapm_routes);
2111 ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2112 card->num_of_dapm_routes);
2116 /* try to set some sane longname if DMI is available */
2117 snd_soc_set_dmi_name(card, NULL);
2119 soc_setup_card_name(card->snd_card->shortname,
2120 card->name, NULL, 0);
2121 soc_setup_card_name(card->snd_card->longname,
2122 card->long_name, card->name, 0);
2123 soc_setup_card_name(card->snd_card->driver,
2124 card->driver_name, card->name, 1);
2126 if (card->late_probe) {
2127 ret = card->late_probe(card);
2129 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
2135 snd_soc_dapm_new_widgets(card);
2137 ret = snd_card_register(card->snd_card);
2139 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2144 card->instantiated = 1;
2145 dapm_mark_endpoints_dirty(card);
2146 snd_soc_dapm_sync(&card->dapm);
2150 soc_cleanup_card_resources(card);
2152 mutex_unlock(&card->mutex);
2153 mutex_unlock(&client_mutex);
2158 /* probes a new socdev */
2159 static int soc_probe(struct platform_device *pdev)
2161 struct snd_soc_card *card = platform_get_drvdata(pdev);
2164 * no card, so machine driver should be registering card
2165 * we should not be here in that case so ret error
2170 dev_warn(&pdev->dev,
2171 "ASoC: machine %s should use snd_soc_register_card()\n",
2174 /* Bodge while we unpick instantiation */
2175 card->dev = &pdev->dev;
2177 return snd_soc_register_card(card);
2180 /* removes a socdev */
2181 static int soc_remove(struct platform_device *pdev)
2183 struct snd_soc_card *card = platform_get_drvdata(pdev);
2185 snd_soc_unregister_card(card);
2189 int snd_soc_poweroff(struct device *dev)
2191 struct snd_soc_card *card = dev_get_drvdata(dev);
2192 struct snd_soc_pcm_runtime *rtd;
2194 if (!card->instantiated)
2198 * Flush out pmdown_time work - we actually do want to run it
2199 * now, we're shutting down so no imminent restart.
2201 snd_soc_flush_all_delayed_work(card);
2203 snd_soc_dapm_shutdown(card);
2205 /* deactivate pins to sleep state */
2206 for_each_card_rtds(card, rtd) {
2207 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2208 struct snd_soc_dai *codec_dai;
2211 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2212 for_each_rtd_codec_dai(rtd, i, codec_dai) {
2213 pinctrl_pm_select_sleep_state(codec_dai->dev);
2219 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2221 const struct dev_pm_ops snd_soc_pm_ops = {
2222 .suspend = snd_soc_suspend,
2223 .resume = snd_soc_resume,
2224 .freeze = snd_soc_suspend,
2225 .thaw = snd_soc_resume,
2226 .poweroff = snd_soc_poweroff,
2227 .restore = snd_soc_resume,
2229 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2231 /* ASoC platform driver */
2232 static struct platform_driver soc_driver = {
2234 .name = "soc-audio",
2235 .pm = &snd_soc_pm_ops,
2238 .remove = soc_remove,
2242 * snd_soc_cnew - create new control
2243 * @_template: control template
2244 * @data: control private data
2245 * @long_name: control long name
2246 * @prefix: control name prefix
2248 * Create a new mixer control from a template control.
2250 * Returns 0 for success, else error.
2252 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2253 void *data, const char *long_name,
2256 struct snd_kcontrol_new template;
2257 struct snd_kcontrol *kcontrol;
2260 memcpy(&template, _template, sizeof(template));
2264 long_name = template.name;
2267 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2271 template.name = name;
2273 template.name = long_name;
2276 kcontrol = snd_ctl_new1(&template, data);
2282 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2284 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2285 const struct snd_kcontrol_new *controls, int num_controls,
2286 const char *prefix, void *data)
2290 for (i = 0; i < num_controls; i++) {
2291 const struct snd_kcontrol_new *control = &controls[i];
2293 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2294 control->name, prefix));
2296 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2297 control->name, err);
2305 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2308 struct snd_card *card = soc_card->snd_card;
2309 struct snd_kcontrol *kctl;
2311 if (unlikely(!name))
2314 list_for_each_entry(kctl, &card->controls, list)
2315 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2319 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2322 * snd_soc_add_component_controls - Add an array of controls to a component.
2324 * @component: Component to add controls to
2325 * @controls: Array of controls to add
2326 * @num_controls: Number of elements in the array
2328 * Return: 0 for success, else error.
2330 int snd_soc_add_component_controls(struct snd_soc_component *component,
2331 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2333 struct snd_card *card = component->card->snd_card;
2335 return snd_soc_add_controls(card, component->dev, controls,
2336 num_controls, component->name_prefix, component);
2338 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2341 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2342 * Convenience function to add a list of controls.
2344 * @soc_card: SoC card to add controls to
2345 * @controls: array of controls to add
2346 * @num_controls: number of elements in the array
2348 * Return 0 for success, else error.
2350 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2351 const struct snd_kcontrol_new *controls, int num_controls)
2353 struct snd_card *card = soc_card->snd_card;
2355 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2358 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2361 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2362 * Convienience function to add a list of controls.
2364 * @dai: DAI to add controls to
2365 * @controls: array of controls to add
2366 * @num_controls: number of elements in the array
2368 * Return 0 for success, else error.
2370 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2371 const struct snd_kcontrol_new *controls, int num_controls)
2373 struct snd_card *card = dai->component->card->snd_card;
2375 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2378 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2380 static int snd_soc_bind_card(struct snd_soc_card *card)
2382 struct snd_soc_pcm_runtime *rtd;
2385 ret = snd_soc_instantiate_card(card);
2389 /* deactivate pins to sleep state */
2390 for_each_card_rtds(card, rtd) {
2391 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2392 struct snd_soc_dai *codec_dai;
2395 for_each_rtd_codec_dai(rtd, j, codec_dai) {
2396 if (!codec_dai->active)
2397 pinctrl_pm_select_sleep_state(codec_dai->dev);
2400 if (!cpu_dai->active)
2401 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2408 * snd_soc_register_card - Register a card with the ASoC core
2410 * @card: Card to register
2413 int snd_soc_register_card(struct snd_soc_card *card)
2415 if (!card->name || !card->dev)
2418 dev_set_drvdata(card->dev, card);
2420 INIT_LIST_HEAD(&card->widgets);
2421 INIT_LIST_HEAD(&card->paths);
2422 INIT_LIST_HEAD(&card->dapm_list);
2423 INIT_LIST_HEAD(&card->aux_comp_list);
2424 INIT_LIST_HEAD(&card->component_dev_list);
2425 INIT_LIST_HEAD(&card->list);
2426 INIT_LIST_HEAD(&card->dai_link_list);
2427 INIT_LIST_HEAD(&card->rtd_list);
2428 INIT_LIST_HEAD(&card->dapm_dirty);
2429 INIT_LIST_HEAD(&card->dobj_list);
2431 card->instantiated = 0;
2432 mutex_init(&card->mutex);
2433 mutex_init(&card->dapm_mutex);
2434 mutex_init(&card->pcm_mutex);
2435 spin_lock_init(&card->dpcm_lock);
2437 return snd_soc_bind_card(card);
2439 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2441 static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2443 if (card->instantiated) {
2444 card->instantiated = false;
2445 snd_soc_dapm_shutdown(card);
2446 snd_soc_flush_all_delayed_work(card);
2448 soc_cleanup_card_resources(card);
2450 list_add(&card->list, &unbind_card_list);
2453 list_del(&card->list);
2458 * snd_soc_unregister_card - Unregister a card with the ASoC core
2460 * @card: Card to unregister
2463 int snd_soc_unregister_card(struct snd_soc_card *card)
2465 mutex_lock(&client_mutex);
2466 snd_soc_unbind_card(card, true);
2467 mutex_unlock(&client_mutex);
2468 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2472 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2475 * Simplify DAI link configuration by removing ".-1" from device names
2476 * and sanitizing names.
2478 static char *fmt_single_name(struct device *dev, int *id)
2480 char *found, name[NAME_SIZE];
2483 if (dev_name(dev) == NULL)
2486 strlcpy(name, dev_name(dev), NAME_SIZE);
2488 /* are we a "%s.%d" name (platform and SPI components) */
2489 found = strstr(name, dev->driver->name);
2492 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2494 /* discard ID from name if ID == -1 */
2496 found[strlen(dev->driver->name)] = '\0';
2500 /* I2C component devices are named "bus-addr" */
2501 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2502 char tmp[NAME_SIZE];
2504 /* create unique ID number from I2C addr and bus */
2505 *id = ((id1 & 0xffff) << 16) + id2;
2507 /* sanitize component name for DAI link creation */
2508 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2510 strlcpy(name, tmp, NAME_SIZE);
2515 return devm_kstrdup(dev, name, GFP_KERNEL);
2519 * Simplify DAI link naming for single devices with multiple DAIs by removing
2520 * any ".-1" and using the DAI name (instead of device name).
2522 static inline char *fmt_multiple_name(struct device *dev,
2523 struct snd_soc_dai_driver *dai_drv)
2525 if (dai_drv->name == NULL) {
2527 "ASoC: error - multiple DAI %s registered with no name\n",
2532 return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2535 /* Create a DAI and add it to the component's DAI list */
2536 static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2537 struct snd_soc_dai_driver *dai_drv,
2538 bool legacy_dai_naming)
2540 struct device *dev = component->dev;
2541 struct snd_soc_dai *dai;
2543 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2545 dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
2550 * Back in the old days when we still had component-less DAIs,
2551 * instead of having a static name, component-less DAIs would
2552 * inherit the name of the parent device so it is possible to
2553 * register multiple instances of the DAI. We still need to keep
2554 * the same naming style even though those DAIs are not
2555 * component-less anymore.
2557 if (legacy_dai_naming &&
2558 (dai_drv->id == 0 || dai_drv->name == NULL)) {
2559 dai->name = fmt_single_name(dev, &dai->id);
2561 dai->name = fmt_multiple_name(dev, dai_drv);
2563 dai->id = dai_drv->id;
2565 dai->id = component->num_dai;
2570 dai->component = component;
2572 dai->driver = dai_drv;
2573 if (!dai->driver->ops)
2574 dai->driver->ops = &null_dai_ops;
2576 /* see for_each_component_dais */
2577 list_add_tail(&dai->list, &component->dai_list);
2578 component->num_dai++;
2580 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2585 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2587 * @component: The component the DAIs are registered for
2588 * @dai_drv: DAI driver to use for the DAI
2590 * Topology can use this API to register DAIs when probing a component.
2591 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2592 * will be freed in the component cleanup.
2594 int snd_soc_register_dai(struct snd_soc_component *component,
2595 struct snd_soc_dai_driver *dai_drv)
2597 struct snd_soc_dapm_context *dapm =
2598 snd_soc_component_get_dapm(component);
2599 struct snd_soc_dai *dai;
2602 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
2603 dev_err(component->dev, "Invalid dai type %d\n",
2604 dai_drv->dobj.type);
2608 lockdep_assert_held(&client_mutex);
2609 dai = soc_add_dai(component, dai_drv, false);
2614 * Create the DAI widgets here. After adding DAIs, topology may
2615 * also add routes that need these widgets as source or sink.
2617 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
2619 dev_err(component->dev,
2620 "Failed to create DAI widgets %d\n", ret);
2625 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2628 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2630 * @component: The component for which the DAIs should be unregistered
2632 static void snd_soc_unregister_dais(struct snd_soc_component *component)
2634 struct snd_soc_dai *dai, *_dai;
2636 for_each_component_dais_safe(component, dai, _dai) {
2637 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2639 list_del(&dai->list);
2644 * snd_soc_register_dais - Register a DAI with the ASoC core
2646 * @component: The component the DAIs are registered for
2647 * @dai_drv: DAI driver to use for the DAIs
2648 * @count: Number of DAIs
2650 static int snd_soc_register_dais(struct snd_soc_component *component,
2651 struct snd_soc_dai_driver *dai_drv,
2654 struct device *dev = component->dev;
2655 struct snd_soc_dai *dai;
2659 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
2661 for (i = 0; i < count; i++) {
2663 dai = soc_add_dai(component, dai_drv + i, count == 1 &&
2664 !component->driver->non_legacy_dai_naming);
2674 snd_soc_unregister_dais(component);
2679 static int snd_soc_component_initialize(struct snd_soc_component *component,
2680 const struct snd_soc_component_driver *driver, struct device *dev)
2682 INIT_LIST_HEAD(&component->dai_list);
2683 INIT_LIST_HEAD(&component->dobj_list);
2684 INIT_LIST_HEAD(&component->card_list);
2685 mutex_init(&component->io_mutex);
2687 component->name = fmt_single_name(dev, &component->id);
2688 if (!component->name) {
2689 dev_err(dev, "ASoC: Failed to allocate name\n");
2693 component->dev = dev;
2694 component->driver = driver;
2699 static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2701 int val_bytes = regmap_get_val_bytes(component->regmap);
2703 /* Errors are legitimate for non-integer byte multiples */
2705 component->val_bytes = val_bytes;
2708 #ifdef CONFIG_REGMAP
2711 * snd_soc_component_init_regmap() - Initialize regmap instance for the
2713 * @component: The component for which to initialize the regmap instance
2714 * @regmap: The regmap instance that should be used by the component
2716 * This function allows deferred assignment of the regmap instance that is
2717 * associated with the component. Only use this if the regmap instance is not
2718 * yet ready when the component is registered. The function must also be called
2719 * before the first IO attempt of the component.
2721 void snd_soc_component_init_regmap(struct snd_soc_component *component,
2722 struct regmap *regmap)
2724 component->regmap = regmap;
2725 snd_soc_component_setup_regmap(component);
2727 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
2730 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
2732 * @component: The component for which to de-initialize the regmap instance
2734 * Calls regmap_exit() on the regmap instance associated to the component and
2735 * removes the regmap instance from the component.
2737 * This function should only be used if snd_soc_component_init_regmap() was used
2738 * to initialize the regmap instance.
2740 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
2742 regmap_exit(component->regmap);
2743 component->regmap = NULL;
2745 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2749 #define ENDIANNESS_MAP(name) \
2750 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2751 static u64 endianness_format_map[] = {
2752 ENDIANNESS_MAP(S16_),
2753 ENDIANNESS_MAP(U16_),
2754 ENDIANNESS_MAP(S24_),
2755 ENDIANNESS_MAP(U24_),
2756 ENDIANNESS_MAP(S32_),
2757 ENDIANNESS_MAP(U32_),
2758 ENDIANNESS_MAP(S24_3),
2759 ENDIANNESS_MAP(U24_3),
2760 ENDIANNESS_MAP(S20_3),
2761 ENDIANNESS_MAP(U20_3),
2762 ENDIANNESS_MAP(S18_3),
2763 ENDIANNESS_MAP(U18_3),
2764 ENDIANNESS_MAP(FLOAT_),
2765 ENDIANNESS_MAP(FLOAT64_),
2766 ENDIANNESS_MAP(IEC958_SUBFRAME_),
2770 * Fix up the DAI formats for endianness: codecs don't actually see
2771 * the endianness of the data but we're using the CPU format
2772 * definitions which do need to include endianness so we ensure that
2773 * codec DAIs always have both big and little endian variants set.
2775 static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2779 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2780 if (stream->formats & endianness_format_map[i])
2781 stream->formats |= endianness_format_map[i];
2784 static void snd_soc_try_rebind_card(void)
2786 struct snd_soc_card *card, *c;
2788 list_for_each_entry_safe(card, c, &unbind_card_list, list)
2789 if (!snd_soc_bind_card(card))
2790 list_del(&card->list);
2793 static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2795 struct snd_soc_card *card = component->card;
2797 snd_soc_unregister_dais(component);
2800 snd_soc_unbind_card(card, false);
2802 list_del(&component->list);
2805 int snd_soc_add_component(struct device *dev,
2806 struct snd_soc_component *component,
2807 const struct snd_soc_component_driver *component_driver,
2808 struct snd_soc_dai_driver *dai_drv,
2814 mutex_lock(&client_mutex);
2816 ret = snd_soc_component_initialize(component, component_driver, dev);
2820 if (component_driver->endianness) {
2821 for (i = 0; i < num_dai; i++) {
2822 convert_endianness_formats(&dai_drv[i].playback);
2823 convert_endianness_formats(&dai_drv[i].capture);
2827 ret = snd_soc_register_dais(component, dai_drv, num_dai);
2829 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
2833 if (!component->driver->write && !component->driver->read) {
2834 if (!component->regmap)
2835 component->regmap = dev_get_regmap(component->dev,
2837 if (component->regmap)
2838 snd_soc_component_setup_regmap(component);
2841 /* see for_each_component */
2842 list_add(&component->list, &component_list);
2846 snd_soc_del_component_unlocked(component);
2848 mutex_unlock(&client_mutex);
2851 snd_soc_try_rebind_card();
2855 EXPORT_SYMBOL_GPL(snd_soc_add_component);
2857 int snd_soc_register_component(struct device *dev,
2858 const struct snd_soc_component_driver *component_driver,
2859 struct snd_soc_dai_driver *dai_drv,
2862 struct snd_soc_component *component;
2864 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
2868 return snd_soc_add_component(dev, component, component_driver,
2871 EXPORT_SYMBOL_GPL(snd_soc_register_component);
2874 * snd_soc_unregister_component - Unregister all related component
2875 * from the ASoC core
2877 * @dev: The device to unregister
2879 void snd_soc_unregister_component(struct device *dev)
2881 struct snd_soc_component *component;
2883 mutex_lock(&client_mutex);
2885 component = snd_soc_lookup_component(dev, NULL);
2889 snd_soc_del_component_unlocked(component);
2891 mutex_unlock(&client_mutex);
2893 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2895 /* Retrieve a card's name from device tree */
2896 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2897 const char *propname)
2899 struct device_node *np;
2903 pr_err("card->dev is not set before calling %s\n", __func__);
2907 np = card->dev->of_node;
2909 ret = of_property_read_string_index(np, propname, 0, &card->name);
2911 * EINVAL means the property does not exist. This is fine providing
2912 * card->name was previously set, which is checked later in
2913 * snd_soc_register_card.
2915 if (ret < 0 && ret != -EINVAL) {
2917 "ASoC: Property '%s' could not be read: %d\n",
2924 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2926 static const struct snd_soc_dapm_widget simple_widgets[] = {
2927 SND_SOC_DAPM_MIC("Microphone", NULL),
2928 SND_SOC_DAPM_LINE("Line", NULL),
2929 SND_SOC_DAPM_HP("Headphone", NULL),
2930 SND_SOC_DAPM_SPK("Speaker", NULL),
2933 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
2934 const char *propname)
2936 struct device_node *np = card->dev->of_node;
2937 struct snd_soc_dapm_widget *widgets;
2938 const char *template, *wname;
2939 int i, j, num_widgets, ret;
2941 num_widgets = of_property_count_strings(np, propname);
2942 if (num_widgets < 0) {
2944 "ASoC: Property '%s' does not exist\n", propname);
2947 if (num_widgets & 1) {
2949 "ASoC: Property '%s' length is not even\n", propname);
2955 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
2960 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
2964 "ASoC: Could not allocate memory for widgets\n");
2968 for (i = 0; i < num_widgets; i++) {
2969 ret = of_property_read_string_index(np, propname,
2973 "ASoC: Property '%s' index %d read error:%d\n",
2974 propname, 2 * i, ret);
2978 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
2979 if (!strncmp(template, simple_widgets[j].name,
2980 strlen(simple_widgets[j].name))) {
2981 widgets[i] = simple_widgets[j];
2986 if (j >= ARRAY_SIZE(simple_widgets)) {
2988 "ASoC: DAPM widget '%s' is not supported\n",
2993 ret = of_property_read_string_index(np, propname,
2998 "ASoC: Property '%s' index %d read error:%d\n",
2999 propname, (2 * i) + 1, ret);
3003 widgets[i].name = wname;
3006 card->of_dapm_widgets = widgets;
3007 card->num_of_dapm_widgets = num_widgets;
3011 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
3013 int snd_soc_of_get_slot_mask(struct device_node *np,
3014 const char *prop_name,
3018 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
3024 for (i = 0; i < val; i++)
3025 if (be32_to_cpup(&of_slot_mask[i]))
3030 EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
3032 int snd_soc_of_parse_tdm_slot(struct device_node *np,
3033 unsigned int *tx_mask,
3034 unsigned int *rx_mask,
3035 unsigned int *slots,
3036 unsigned int *slot_width)
3042 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3044 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3046 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3047 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3055 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3056 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3066 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3068 void snd_soc_of_parse_node_prefix(struct device_node *np,
3069 struct snd_soc_codec_conf *codec_conf,
3070 struct device_node *of_node,
3071 const char *propname)
3076 ret = of_property_read_string(np, propname, &str);
3078 /* no prefix is not error */
3082 codec_conf->of_node = of_node;
3083 codec_conf->name_prefix = str;
3085 EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
3087 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3088 const char *propname)
3090 struct device_node *np = card->dev->of_node;
3092 struct snd_soc_dapm_route *routes;
3095 num_routes = of_property_count_strings(np, propname);
3096 if (num_routes < 0 || num_routes & 1) {
3098 "ASoC: Property '%s' does not exist or its length is not even\n",
3104 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3109 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
3113 "ASoC: Could not allocate DAPM route table\n");
3117 for (i = 0; i < num_routes; i++) {
3118 ret = of_property_read_string_index(np, propname,
3119 2 * i, &routes[i].sink);
3122 "ASoC: Property '%s' index %d could not be read: %d\n",
3123 propname, 2 * i, ret);
3126 ret = of_property_read_string_index(np, propname,
3127 (2 * i) + 1, &routes[i].source);
3130 "ASoC: Property '%s' index %d could not be read: %d\n",
3131 propname, (2 * i) + 1, ret);
3136 card->num_of_dapm_routes = num_routes;
3137 card->of_dapm_routes = routes;
3141 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3143 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3145 struct device_node **bitclkmaster,
3146 struct device_node **framemaster)
3150 unsigned int format = 0;
3156 } of_fmt_table[] = {
3157 { "i2s", SND_SOC_DAIFMT_I2S },
3158 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3159 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3160 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3161 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3162 { "ac97", SND_SOC_DAIFMT_AC97 },
3163 { "pdm", SND_SOC_DAIFMT_PDM},
3164 { "msb", SND_SOC_DAIFMT_MSB },
3165 { "lsb", SND_SOC_DAIFMT_LSB },
3172 * check "dai-format = xxx"
3173 * or "[prefix]format = xxx"
3174 * SND_SOC_DAIFMT_FORMAT_MASK area
3176 ret = of_property_read_string(np, "dai-format", &str);
3178 snprintf(prop, sizeof(prop), "%sformat", prefix);
3179 ret = of_property_read_string(np, prop, &str);
3182 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3183 if (strcmp(str, of_fmt_table[i].name) == 0) {
3184 format |= of_fmt_table[i].val;
3191 * check "[prefix]continuous-clock"
3192 * SND_SOC_DAIFMT_CLOCK_MASK area
3194 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
3195 if (of_property_read_bool(np, prop))
3196 format |= SND_SOC_DAIFMT_CONT;
3198 format |= SND_SOC_DAIFMT_GATED;
3201 * check "[prefix]bitclock-inversion"
3202 * check "[prefix]frame-inversion"
3203 * SND_SOC_DAIFMT_INV_MASK area
3205 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3206 bit = !!of_get_property(np, prop, NULL);
3208 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3209 frame = !!of_get_property(np, prop, NULL);
3211 switch ((bit << 4) + frame) {
3213 format |= SND_SOC_DAIFMT_IB_IF;
3216 format |= SND_SOC_DAIFMT_IB_NF;
3219 format |= SND_SOC_DAIFMT_NB_IF;
3222 /* SND_SOC_DAIFMT_NB_NF is default */
3227 * check "[prefix]bitclock-master"
3228 * check "[prefix]frame-master"
3229 * SND_SOC_DAIFMT_MASTER_MASK area
3231 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3232 bit = !!of_get_property(np, prop, NULL);
3233 if (bit && bitclkmaster)
3234 *bitclkmaster = of_parse_phandle(np, prop, 0);
3236 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3237 frame = !!of_get_property(np, prop, NULL);
3238 if (frame && framemaster)
3239 *framemaster = of_parse_phandle(np, prop, 0);
3241 switch ((bit << 4) + frame) {
3243 format |= SND_SOC_DAIFMT_CBM_CFM;
3246 format |= SND_SOC_DAIFMT_CBM_CFS;
3249 format |= SND_SOC_DAIFMT_CBS_CFM;
3252 format |= SND_SOC_DAIFMT_CBS_CFS;
3258 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3260 int snd_soc_get_dai_id(struct device_node *ep)
3262 struct snd_soc_component *component;
3263 struct snd_soc_dai_link_component dlc;
3266 dlc.of_node = of_graph_get_port_parent(ep);
3269 * For example HDMI case, HDMI has video/sound port,
3270 * but ALSA SoC needs sound port number only.
3271 * Thus counting HDMI DT port/endpoint doesn't work.
3272 * Then, it should have .of_xlate_dai_id
3275 mutex_lock(&client_mutex);
3276 component = soc_find_component(&dlc);
3278 ret = snd_soc_component_of_xlate_dai_id(component, ep);
3279 mutex_unlock(&client_mutex);
3281 of_node_put(dlc.of_node);
3285 EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3287 int snd_soc_get_dai_name(struct of_phandle_args *args,
3288 const char **dai_name)
3290 struct snd_soc_component *pos;
3291 struct device_node *component_of_node;
3292 int ret = -EPROBE_DEFER;
3294 mutex_lock(&client_mutex);
3295 for_each_component(pos) {
3296 component_of_node = soc_component_to_node(pos);
3298 if (component_of_node != args->np)
3301 ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3302 if (ret == -ENOTSUPP) {
3303 struct snd_soc_dai *dai;
3306 switch (args->args_count) {
3308 id = 0; /* same as dai_drv[0] */
3318 if (id < 0 || id >= pos->num_dai) {
3325 /* find target DAI */
3326 for_each_component_dais(pos, dai) {
3332 *dai_name = dai->driver->name;
3334 *dai_name = pos->name;
3339 mutex_unlock(&client_mutex);
3342 EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
3344 int snd_soc_of_get_dai_name(struct device_node *of_node,
3345 const char **dai_name)
3347 struct of_phandle_args args;
3350 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3351 "#sound-dai-cells", 0, &args);
3355 ret = snd_soc_get_dai_name(&args, dai_name);
3357 of_node_put(args.np);
3361 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3364 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3365 * @dai_link: DAI link
3367 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3369 void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3371 struct snd_soc_dai_link_component *component;
3374 for_each_link_codecs(dai_link, index, component) {
3375 if (!component->of_node)
3377 of_node_put(component->of_node);
3378 component->of_node = NULL;
3381 EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3384 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3386 * @of_node: Device node
3387 * @dai_link: DAI link
3389 * Builds an array of CODEC DAI components from the DAI link property
3391 * The array is set in the DAI link and the number of DAIs is set accordingly.
3392 * The device nodes in the array (of_node) must be dereferenced by calling
3393 * snd_soc_of_put_dai_link_codecs() on @dai_link.
3395 * Returns 0 for success
3397 int snd_soc_of_get_dai_link_codecs(struct device *dev,
3398 struct device_node *of_node,
3399 struct snd_soc_dai_link *dai_link)
3401 struct of_phandle_args args;
3402 struct snd_soc_dai_link_component *component;
3404 int index, num_codecs, ret;
3406 /* Count the number of CODECs */
3408 num_codecs = of_count_phandle_with_args(of_node, name,
3409 "#sound-dai-cells");
3410 if (num_codecs <= 0) {
3411 if (num_codecs == -ENOENT)
3412 dev_err(dev, "No 'sound-dai' property\n");
3414 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3417 component = devm_kcalloc(dev,
3418 num_codecs, sizeof(*component),
3422 dai_link->codecs = component;
3423 dai_link->num_codecs = num_codecs;
3425 /* Parse the list */
3426 for_each_link_codecs(dai_link, index, component) {
3427 ret = of_parse_phandle_with_args(of_node, name,
3432 component->of_node = args.np;
3433 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3439 snd_soc_of_put_dai_link_codecs(dai_link);
3440 dai_link->codecs = NULL;
3441 dai_link->num_codecs = 0;
3444 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3446 static int __init snd_soc_init(void)
3448 snd_soc_debugfs_init();
3449 snd_soc_util_init();
3451 return platform_driver_register(&soc_driver);
3453 module_init(snd_soc_init);
3455 static void __exit snd_soc_exit(void)
3457 snd_soc_util_exit();
3458 snd_soc_debugfs_exit();
3460 platform_driver_unregister(&soc_driver);
3462 module_exit(snd_soc_exit);
3464 /* Module information */
3465 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3466 MODULE_DESCRIPTION("ALSA SoC Core");
3467 MODULE_LICENSE("GPL");
3468 MODULE_ALIAS("platform:soc-audio");