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 <linux/acpi.h>
35 #include <sound/core.h>
36 #include <sound/jack.h>
37 #include <sound/pcm.h>
38 #include <sound/pcm_params.h>
39 #include <sound/soc.h>
40 #include <sound/soc-dpcm.h>
41 #include <sound/soc-topology.h>
42 #include <sound/soc-link.h>
43 #include <sound/initval.h>
45 #define CREATE_TRACE_POINTS
46 #include <trace/events/asoc.h>
48 static DEFINE_MUTEX(client_mutex);
49 static LIST_HEAD(component_list);
50 static LIST_HEAD(unbind_card_list);
52 #define for_each_component(component) \
53 list_for_each_entry(component, &component_list, list)
56 * This is used if driver don't need to have CPU/Codec/Platform
59 struct snd_soc_dai_link_component null_dailink_component[0];
60 EXPORT_SYMBOL_GPL(null_dailink_component);
63 * This is a timeout to do a DAPM powerdown after a stream is closed().
64 * It can be used to eliminate pops between different playback streams, e.g.
65 * between two audio tracks.
67 static int pmdown_time = 5000;
68 module_param(pmdown_time, int, 0);
69 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
71 static ssize_t pmdown_time_show(struct device *dev,
72 struct device_attribute *attr, char *buf)
74 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
76 return sprintf(buf, "%ld\n", rtd->pmdown_time);
79 static ssize_t pmdown_time_set(struct device *dev,
80 struct device_attribute *attr,
81 const char *buf, size_t count)
83 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
86 ret = kstrtol(buf, 10, &rtd->pmdown_time);
93 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
95 static struct attribute *soc_dev_attrs[] = {
96 &dev_attr_pmdown_time.attr,
100 static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
101 struct attribute *attr, int idx)
103 struct device *dev = kobj_to_dev(kobj);
104 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
109 if (attr == &dev_attr_pmdown_time.attr)
110 return attr->mode; /* always visible */
111 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
114 static const struct attribute_group soc_dapm_dev_group = {
115 .attrs = soc_dapm_dev_attrs,
116 .is_visible = soc_dev_attr_is_visible,
119 static const struct attribute_group soc_dev_group = {
120 .attrs = soc_dev_attrs,
121 .is_visible = soc_dev_attr_is_visible,
124 static const struct attribute_group *soc_dev_attr_groups[] = {
130 #ifdef CONFIG_DEBUG_FS
131 struct dentry *snd_soc_debugfs_root;
132 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
134 static void soc_init_component_debugfs(struct snd_soc_component *component)
136 if (!component->card->debugfs_card_root)
139 if (component->debugfs_prefix) {
142 name = kasprintf(GFP_KERNEL, "%s:%s",
143 component->debugfs_prefix, component->name);
145 component->debugfs_root = debugfs_create_dir(name,
146 component->card->debugfs_card_root);
150 component->debugfs_root = debugfs_create_dir(component->name,
151 component->card->debugfs_card_root);
154 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
155 component->debugfs_root);
158 static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
160 if (!component->debugfs_root)
162 debugfs_remove_recursive(component->debugfs_root);
163 component->debugfs_root = NULL;
166 static int dai_list_show(struct seq_file *m, void *v)
168 struct snd_soc_component *component;
169 struct snd_soc_dai *dai;
171 mutex_lock(&client_mutex);
173 for_each_component(component)
174 for_each_component_dais(component, dai)
175 seq_printf(m, "%s\n", dai->name);
177 mutex_unlock(&client_mutex);
181 DEFINE_SHOW_ATTRIBUTE(dai_list);
183 static int component_list_show(struct seq_file *m, void *v)
185 struct snd_soc_component *component;
187 mutex_lock(&client_mutex);
189 for_each_component(component)
190 seq_printf(m, "%s\n", component->name);
192 mutex_unlock(&client_mutex);
196 DEFINE_SHOW_ATTRIBUTE(component_list);
198 static void soc_init_card_debugfs(struct snd_soc_card *card)
200 card->debugfs_card_root = debugfs_create_dir(card->name,
201 snd_soc_debugfs_root);
203 debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root,
206 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
209 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
211 debugfs_remove_recursive(card->debugfs_card_root);
212 card->debugfs_card_root = NULL;
215 static void snd_soc_debugfs_init(void)
217 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
219 debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
222 debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
223 &component_list_fops);
226 static void snd_soc_debugfs_exit(void)
228 debugfs_remove_recursive(snd_soc_debugfs_root);
233 static inline void soc_init_component_debugfs(
234 struct snd_soc_component *component)
238 static inline void soc_cleanup_component_debugfs(
239 struct snd_soc_component *component)
243 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
247 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
251 static inline void snd_soc_debugfs_init(void)
255 static inline void snd_soc_debugfs_exit(void)
261 static int snd_soc_rtd_add_component(struct snd_soc_pcm_runtime *rtd,
262 struct snd_soc_component *component)
264 struct snd_soc_component *comp;
267 for_each_rtd_components(rtd, i, comp) {
268 /* already connected */
269 if (comp == component)
273 /* see for_each_rtd_components */
274 rtd->components[rtd->num_components] = component;
275 rtd->num_components++;
280 struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
281 const char *driver_name)
283 struct snd_soc_component *component;
292 * snd_soc_rtdcom_lookup() will find component from rtd by using
293 * specified driver name.
294 * But, if many components which have same driver name are connected
295 * to 1 rtd, this function will return 1st found component.
297 for_each_rtd_components(rtd, i, component) {
298 const char *component_name = component->driver->name;
303 if ((component_name == driver_name) ||
304 strcmp(component_name, driver_name) == 0)
310 EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
312 struct snd_soc_component
313 *snd_soc_lookup_component_nolocked(struct device *dev, const char *driver_name)
315 struct snd_soc_component *component;
316 struct snd_soc_component *found_component;
318 found_component = NULL;
319 for_each_component(component) {
320 if ((dev == component->dev) &&
322 (driver_name == component->driver->name) ||
323 (strcmp(component->driver->name, driver_name) == 0))) {
324 found_component = component;
329 return found_component;
331 EXPORT_SYMBOL_GPL(snd_soc_lookup_component_nolocked);
333 struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
334 const char *driver_name)
336 struct snd_soc_component *component;
338 mutex_lock(&client_mutex);
339 component = snd_soc_lookup_component_nolocked(dev, driver_name);
340 mutex_unlock(&client_mutex);
344 EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
346 struct snd_soc_pcm_runtime
347 *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
348 struct snd_soc_dai_link *dai_link)
350 struct snd_soc_pcm_runtime *rtd;
352 for_each_card_rtds(card, rtd) {
353 if (rtd->dai_link == dai_link)
356 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link->name);
359 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
362 * Power down the audio subsystem pmdown_time msecs after close is called.
363 * This is to ensure there are no pops or clicks in between any music tracks
364 * due to DAPM power cycling.
366 void snd_soc_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
368 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
369 int playback = SNDRV_PCM_STREAM_PLAYBACK;
371 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
374 "ASoC: pop wq checking: %s status: %s waiting: %s\n",
375 codec_dai->driver->playback.stream_name,
376 snd_soc_dai_stream_active(codec_dai, playback) ?
377 "active" : "inactive",
378 rtd->pop_wait ? "yes" : "no");
380 /* are we waiting on this codec DAI stream */
381 if (rtd->pop_wait == 1) {
383 snd_soc_dapm_stream_event(rtd, playback,
384 SND_SOC_DAPM_STREAM_STOP);
387 mutex_unlock(&rtd->card->pcm_mutex);
389 EXPORT_SYMBOL_GPL(snd_soc_close_delayed_work);
391 static void soc_release_rtd_dev(struct device *dev)
393 /* "dev" means "rtd->dev" */
397 static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
402 list_del(&rtd->list);
404 if (delayed_work_pending(&rtd->delayed_work))
405 flush_delayed_work(&rtd->delayed_work);
406 snd_soc_pcm_component_free(rtd);
409 * we don't need to call kfree() for rtd->dev
411 * soc_release_rtd_dev()
413 * We don't need rtd->dev NULL check, because
414 * it is alloced *before* rtd.
416 * soc_new_pcm_runtime()
418 device_unregister(rtd->dev);
421 static void close_delayed_work(struct work_struct *work) {
422 struct snd_soc_pcm_runtime *rtd =
423 container_of(work, struct snd_soc_pcm_runtime,
426 if (rtd->close_delayed_work_func)
427 rtd->close_delayed_work_func(rtd);
430 static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
431 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
433 struct snd_soc_pcm_runtime *rtd;
434 struct snd_soc_component *component;
442 dev = kzalloc(sizeof(struct device), GFP_KERNEL);
446 dev->parent = card->dev;
447 dev->release = soc_release_rtd_dev;
449 dev_set_name(dev, "%s", dai_link->name);
451 ret = device_register(dev);
453 put_device(dev); /* soc_release_rtd_dev */
460 rtd = devm_kzalloc(dev,
462 sizeof(*component) * (dai_link->num_cpus +
463 dai_link->num_codecs +
464 dai_link->num_platforms),
470 INIT_LIST_HEAD(&rtd->list);
471 for_each_pcm_streams(stream) {
472 INIT_LIST_HEAD(&rtd->dpcm[stream].be_clients);
473 INIT_LIST_HEAD(&rtd->dpcm[stream].fe_clients);
475 dev_set_drvdata(dev, rtd);
476 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
481 rtd->dais = devm_kcalloc(dev, dai_link->num_cpus + dai_link->num_codecs,
482 sizeof(struct snd_soc_dai *),
488 * dais = [][][][][][][][][][][][][][][][][][]
489 * ^cpu_dais ^codec_dais
490 * |--- num_cpus ---|--- num_codecs --|
493 * asoc_rtd_to_codec()
495 rtd->num_cpus = dai_link->num_cpus;
496 rtd->num_codecs = dai_link->num_codecs;
498 rtd->dai_link = dai_link;
499 rtd->num = card->num_rtd++;
501 /* see for_each_card_rtds */
502 list_add_tail(&rtd->list, &card->rtd_list);
504 ret = device_add_groups(dev, soc_dev_attr_groups);
511 soc_free_pcm_runtime(rtd);
515 static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
517 struct snd_soc_pcm_runtime *rtd;
519 for_each_card_rtds(card, rtd)
520 flush_delayed_work(&rtd->delayed_work);
523 #ifdef CONFIG_PM_SLEEP
524 /* powers down audio subsystem for suspend */
525 int snd_soc_suspend(struct device *dev)
527 struct snd_soc_card *card = dev_get_drvdata(dev);
528 struct snd_soc_component *component;
529 struct snd_soc_pcm_runtime *rtd;
530 int playback = SNDRV_PCM_STREAM_PLAYBACK;
533 /* If the card is not initialized yet there is nothing to do */
534 if (!card->instantiated)
538 * Due to the resume being scheduled into a workqueue we could
539 * suspend before that's finished - wait for it to complete.
541 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
543 /* we're going to block userspace touching us until resume completes */
544 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
546 /* mute any active DACs */
547 for_each_card_rtds(card, rtd) {
548 struct snd_soc_dai *dai;
550 if (rtd->dai_link->ignore_suspend)
553 for_each_rtd_dais(rtd, i, dai) {
554 if (snd_soc_dai_stream_active(dai, playback))
555 snd_soc_dai_digital_mute(dai, 1, playback);
559 /* suspend all pcms */
560 for_each_card_rtds(card, rtd) {
561 if (rtd->dai_link->ignore_suspend)
564 snd_pcm_suspend_all(rtd->pcm);
567 snd_soc_card_suspend_pre(card);
569 /* close any waiting streams */
570 snd_soc_flush_all_delayed_work(card);
572 for_each_card_rtds(card, rtd) {
575 if (rtd->dai_link->ignore_suspend)
578 for_each_pcm_streams(stream)
579 snd_soc_dapm_stream_event(rtd, stream,
580 SND_SOC_DAPM_STREAM_SUSPEND);
583 /* Recheck all endpoints too, their state is affected by suspend */
584 dapm_mark_endpoints_dirty(card);
585 snd_soc_dapm_sync(&card->dapm);
587 /* suspend all COMPONENTs */
588 for_each_card_rtds(card, rtd) {
590 if (rtd->dai_link->ignore_suspend)
593 for_each_rtd_components(rtd, i, component) {
594 struct snd_soc_dapm_context *dapm =
595 snd_soc_component_get_dapm(component);
598 * ignore if component was already suspended
600 if (snd_soc_component_is_suspended(component))
604 * If there are paths active then the COMPONENT will be
605 * held with bias _ON and should not be suspended.
607 switch (snd_soc_dapm_get_bias_level(dapm)) {
608 case SND_SOC_BIAS_STANDBY:
610 * If the COMPONENT is capable of idle
611 * bias off then being in STANDBY
612 * means it's doing something,
613 * otherwise fall through.
615 if (dapm->idle_bias_off) {
616 dev_dbg(component->dev,
617 "ASoC: idle_bias_off CODEC on over suspend\n");
622 case SND_SOC_BIAS_OFF:
623 snd_soc_component_suspend(component);
624 if (component->regmap)
625 regcache_mark_dirty(component->regmap);
626 /* deactivate pins to sleep state */
627 pinctrl_pm_select_sleep_state(component->dev);
630 dev_dbg(component->dev,
631 "ASoC: COMPONENT is on over suspend\n");
637 snd_soc_card_suspend_post(card);
641 EXPORT_SYMBOL_GPL(snd_soc_suspend);
644 * deferred resume work, so resume can complete before we finished
645 * setting our codec back up, which can be very slow on I2C
647 static void soc_resume_deferred(struct work_struct *work)
649 struct snd_soc_card *card =
650 container_of(work, struct snd_soc_card,
651 deferred_resume_work);
652 struct snd_soc_pcm_runtime *rtd;
653 struct snd_soc_component *component;
657 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
658 * so userspace apps are blocked from touching us
661 dev_dbg(card->dev, "ASoC: starting resume work\n");
663 /* Bring us up into D2 so that DAPM starts enabling things */
664 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
666 snd_soc_card_resume_pre(card);
668 for_each_card_components(card, component) {
669 if (snd_soc_component_is_suspended(component))
670 snd_soc_component_resume(component);
673 for_each_card_rtds(card, rtd) {
676 if (rtd->dai_link->ignore_suspend)
679 for_each_pcm_streams(stream)
680 snd_soc_dapm_stream_event(rtd, stream,
681 SND_SOC_DAPM_STREAM_RESUME);
684 /* unmute any active DACs */
685 for_each_card_rtds(card, rtd) {
686 struct snd_soc_dai *dai;
687 int playback = SNDRV_PCM_STREAM_PLAYBACK;
689 if (rtd->dai_link->ignore_suspend)
692 for_each_rtd_dais(rtd, i, dai) {
693 if (snd_soc_dai_stream_active(dai, playback))
694 snd_soc_dai_digital_mute(dai, 0, playback);
698 snd_soc_card_resume_post(card);
700 dev_dbg(card->dev, "ASoC: resume work completed\n");
702 /* Recheck all endpoints too, their state is affected by suspend */
703 dapm_mark_endpoints_dirty(card);
704 snd_soc_dapm_sync(&card->dapm);
706 /* userspace can access us now we are back as we were before */
707 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
710 /* powers up audio subsystem after a suspend */
711 int snd_soc_resume(struct device *dev)
713 struct snd_soc_card *card = dev_get_drvdata(dev);
714 struct snd_soc_component *component;
716 /* If the card is not initialized yet there is nothing to do */
717 if (!card->instantiated)
720 /* activate pins from sleep state */
721 for_each_card_components(card, component)
722 if (snd_soc_component_active(component))
723 pinctrl_pm_select_default_state(component->dev);
725 dev_dbg(dev, "ASoC: Scheduling resume work\n");
726 if (!schedule_work(&card->deferred_resume_work))
727 dev_err(dev, "ASoC: resume work item may be lost\n");
731 EXPORT_SYMBOL_GPL(snd_soc_resume);
733 static void soc_resume_init(struct snd_soc_card *card)
735 /* deferred resume work */
736 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
739 #define snd_soc_suspend NULL
740 #define snd_soc_resume NULL
741 static inline void soc_resume_init(struct snd_soc_card *card)
746 static struct device_node
747 *soc_component_to_node(struct snd_soc_component *component)
749 struct device_node *of_node;
751 of_node = component->dev->of_node;
752 if (!of_node && component->dev->parent)
753 of_node = component->dev->parent->of_node;
758 static int snd_soc_is_matching_component(
759 const struct snd_soc_dai_link_component *dlc,
760 struct snd_soc_component *component)
762 struct device_node *component_of_node;
767 component_of_node = soc_component_to_node(component);
769 if (dlc->of_node && component_of_node != dlc->of_node)
771 if (dlc->name && strcmp(component->name, dlc->name))
777 static struct snd_soc_component *soc_find_component(
778 const struct snd_soc_dai_link_component *dlc)
780 struct snd_soc_component *component;
782 lockdep_assert_held(&client_mutex);
787 * It returns *1st* found component, but some driver
788 * has few components by same of_node/name
790 * CPU component and generic DMAEngine component
792 for_each_component(component)
793 if (snd_soc_is_matching_component(dlc, component))
800 * snd_soc_find_dai - Find a registered DAI
802 * @dlc: name of the DAI or the DAI driver and optional component info to match
804 * This function will search all registered components and their DAIs to
805 * find the DAI of the same name. The component's of_node and name
806 * should also match if being specified.
808 * Return: pointer of DAI, or NULL if not found.
810 struct snd_soc_dai *snd_soc_find_dai(
811 const struct snd_soc_dai_link_component *dlc)
813 struct snd_soc_component *component;
814 struct snd_soc_dai *dai;
816 lockdep_assert_held(&client_mutex);
818 /* Find CPU DAI from registered DAIs */
819 for_each_component(component) {
820 if (!snd_soc_is_matching_component(dlc, component))
822 for_each_component_dais(component, dai) {
823 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
824 && (!dai->driver->name
825 || strcmp(dai->driver->name, dlc->dai_name)))
834 EXPORT_SYMBOL_GPL(snd_soc_find_dai);
836 struct snd_soc_dai *snd_soc_find_dai_with_mutex(
837 const struct snd_soc_dai_link_component *dlc)
839 struct snd_soc_dai *dai;
841 mutex_lock(&client_mutex);
842 dai = snd_soc_find_dai(dlc);
843 mutex_unlock(&client_mutex);
847 EXPORT_SYMBOL_GPL(snd_soc_find_dai_with_mutex);
849 static int soc_dai_link_sanity_check(struct snd_soc_card *card,
850 struct snd_soc_dai_link *link)
853 struct snd_soc_dai_link_component *cpu, *codec, *platform;
855 for_each_link_codecs(link, i, codec) {
857 * Codec must be specified by 1 of name or OF node,
858 * not both or neither.
860 if (!!codec->name == !!codec->of_node) {
861 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
866 /* Codec DAI name must be specified */
867 if (!codec->dai_name) {
868 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
874 * Defer card registration if codec component is not added to
877 if (!soc_find_component(codec)) {
879 "ASoC: codec component %s not found for link %s\n",
880 codec->name, link->name);
881 return -EPROBE_DEFER;
885 for_each_link_platforms(link, i, platform) {
887 * Platform may be specified by either name or OF node, but it
888 * can be left unspecified, then no components will be inserted
891 if (!!platform->name == !!platform->of_node) {
893 "ASoC: Neither/both platform name/of_node are set for %s\n",
899 * Defer card registration if platform component is not added to
902 if (!soc_find_component(platform)) {
904 "ASoC: platform component %s not found for link %s\n",
905 platform->name, link->name);
906 return -EPROBE_DEFER;
910 for_each_link_cpus(link, i, cpu) {
912 * CPU device may be specified by either name or OF node, but
913 * can be left unspecified, and will be matched based on DAI
916 if (cpu->name && cpu->of_node) {
918 "ASoC: Neither/both cpu name/of_node are set for %s\n",
924 * Defer card registration if cpu dai component is not added to
927 if ((cpu->of_node || cpu->name) &&
928 !soc_find_component(cpu)) {
930 "ASoC: cpu component %s not found for link %s\n",
931 cpu->name, link->name);
932 return -EPROBE_DEFER;
936 * At least one of CPU DAI name or CPU device name/node must be
939 if (!cpu->dai_name &&
940 !(cpu->name || cpu->of_node)) {
942 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
952 * snd_soc_remove_pcm_runtime - Remove a pcm_runtime from card
953 * @card: The ASoC card to which the pcm_runtime has
954 * @rtd: The pcm_runtime to remove
956 * This function removes a pcm_runtime from the ASoC card.
958 void snd_soc_remove_pcm_runtime(struct snd_soc_card *card,
959 struct snd_soc_pcm_runtime *rtd)
961 lockdep_assert_held(&client_mutex);
963 /* release machine specific resources */
964 snd_soc_link_exit(rtd);
967 * Notify the machine driver for extra destruction
969 snd_soc_card_remove_dai_link(card, rtd->dai_link);
971 soc_free_pcm_runtime(rtd);
973 EXPORT_SYMBOL_GPL(snd_soc_remove_pcm_runtime);
976 * snd_soc_add_pcm_runtime - Add a pcm_runtime dynamically via dai_link
977 * @card: The ASoC card to which the pcm_runtime is added
978 * @dai_link: The DAI link to find pcm_runtime
980 * This function adds a pcm_runtime ASoC card by using dai_link.
982 * Note: Topology can use this API to add pcm_runtime when probing the
983 * topology component. And machine drivers can still define static
984 * DAI links in dai_link array.
986 int snd_soc_add_pcm_runtime(struct snd_soc_card *card,
987 struct snd_soc_dai_link *dai_link)
989 struct snd_soc_pcm_runtime *rtd;
990 struct snd_soc_dai_link_component *codec, *platform, *cpu;
991 struct snd_soc_component *component;
994 lockdep_assert_held(&client_mutex);
997 * Notify the machine driver for extra initialization
999 ret = snd_soc_card_add_dai_link(card, dai_link);
1003 if (dai_link->ignore)
1006 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
1008 ret = soc_dai_link_sanity_check(card, dai_link);
1012 rtd = soc_new_pcm_runtime(card, dai_link);
1016 for_each_link_cpus(dai_link, i, cpu) {
1017 asoc_rtd_to_cpu(rtd, i) = snd_soc_find_dai(cpu);
1018 if (!asoc_rtd_to_cpu(rtd, i)) {
1019 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
1023 snd_soc_rtd_add_component(rtd, asoc_rtd_to_cpu(rtd, i)->component);
1026 /* Find CODEC from registered CODECs */
1027 for_each_link_codecs(dai_link, i, codec) {
1028 asoc_rtd_to_codec(rtd, i) = snd_soc_find_dai(codec);
1029 if (!asoc_rtd_to_codec(rtd, i)) {
1030 dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
1035 snd_soc_rtd_add_component(rtd, asoc_rtd_to_codec(rtd, i)->component);
1038 /* Find PLATFORM from registered PLATFORMs */
1039 for_each_link_platforms(dai_link, i, platform) {
1040 for_each_component(component) {
1041 if (!snd_soc_is_matching_component(platform, component))
1044 snd_soc_rtd_add_component(rtd, component);
1051 snd_soc_remove_pcm_runtime(card, rtd);
1052 return -EPROBE_DEFER;
1054 EXPORT_SYMBOL_GPL(snd_soc_add_pcm_runtime);
1056 static int soc_init_pcm_runtime(struct snd_soc_card *card,
1057 struct snd_soc_pcm_runtime *rtd)
1059 struct snd_soc_dai_link *dai_link = rtd->dai_link;
1060 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
1061 struct snd_soc_component *component;
1064 /* set default power off timeout */
1065 rtd->pmdown_time = pmdown_time;
1067 /* do machine specific initialization */
1068 ret = snd_soc_link_init(rtd);
1072 if (dai_link->dai_fmt) {
1073 ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1078 /* add DPCM sysfs entries */
1079 soc_dpcm_debugfs_add(rtd);
1084 * most drivers will register their PCMs using DAI link ordering but
1085 * topology based drivers can use the DAI link id field to set PCM
1086 * device number and then use rtd + a base offset of the BEs.
1088 for_each_rtd_components(rtd, i, component) {
1089 if (!component->driver->use_dai_pcm_id)
1092 if (rtd->dai_link->no_pcm)
1093 num += component->driver->be_pcm_base;
1095 num = rtd->dai_link->id;
1098 /* create compress_device if possible */
1099 ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1100 if (ret != -ENOTSUPP) {
1102 dev_err(card->dev, "ASoC: can't create compress %s\n",
1103 dai_link->stream_name);
1107 /* create the pcm */
1108 ret = soc_new_pcm(rtd, num);
1110 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1111 dai_link->stream_name, ret);
1115 return snd_soc_pcm_dai_new(rtd);
1118 static void soc_set_name_prefix(struct snd_soc_card *card,
1119 struct snd_soc_component *component)
1121 struct device_node *of_node = soc_component_to_node(component);
1125 for (i = 0; i < card->num_configs; i++) {
1126 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1128 if (snd_soc_is_matching_component(&map->dlc, component)) {
1129 component->name_prefix = map->name_prefix;
1135 * If there is no configuration table or no match in the table,
1136 * check if a prefix is provided in the node
1138 ret = of_property_read_string(of_node, "sound-name-prefix", &str);
1142 component->name_prefix = str;
1145 static void soc_remove_component(struct snd_soc_component *component,
1149 if (!component->card)
1153 snd_soc_component_remove(component);
1155 /* For framework level robustness */
1156 snd_soc_component_set_jack(component, NULL, NULL);
1158 list_del_init(&component->card_list);
1159 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1160 soc_cleanup_component_debugfs(component);
1161 component->card = NULL;
1162 snd_soc_component_module_put_when_remove(component);
1165 static int soc_probe_component(struct snd_soc_card *card,
1166 struct snd_soc_component *component)
1168 struct snd_soc_dapm_context *dapm =
1169 snd_soc_component_get_dapm(component);
1170 struct snd_soc_dai *dai;
1174 if (!strcmp(component->name, "snd-soc-dummy"))
1177 if (component->card) {
1178 if (component->card != card) {
1179 dev_err(component->dev,
1180 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1181 card->name, component->card->name);
1187 ret = snd_soc_component_module_get_when_probe(component);
1191 component->card = card;
1192 soc_set_name_prefix(card, component);
1194 soc_init_component_debugfs(component);
1196 snd_soc_dapm_init(dapm, card, component);
1198 ret = snd_soc_dapm_new_controls(dapm,
1199 component->driver->dapm_widgets,
1200 component->driver->num_dapm_widgets);
1203 dev_err(component->dev,
1204 "Failed to create new controls %d\n", ret);
1208 for_each_component_dais(component, dai) {
1209 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1211 dev_err(component->dev,
1212 "Failed to create DAI widgets %d\n", ret);
1217 ret = snd_soc_component_probe(component);
1219 dev_err(component->dev,
1220 "ASoC: failed to probe component %d\n", ret);
1223 WARN(dapm->idle_bias_off &&
1224 dapm->bias_level != SND_SOC_BIAS_OFF,
1225 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1230 * machine specific init
1232 * snd_soc_component_set_aux()
1234 ret = snd_soc_component_init(component);
1238 ret = snd_soc_add_component_controls(component,
1239 component->driver->controls,
1240 component->driver->num_controls);
1244 ret = snd_soc_dapm_add_routes(dapm,
1245 component->driver->dapm_routes,
1246 component->driver->num_dapm_routes);
1248 if (card->disable_route_checks) {
1250 "%s: disable_route_checks set, ignoring errors on add_routes\n",
1254 "%s: snd_soc_dapm_add_routes failed: %d\n",
1260 /* see for_each_card_components */
1261 list_add(&component->card_list, &card->component_dev_list);
1265 soc_remove_component(component, probed);
1270 static void soc_remove_link_dais(struct snd_soc_card *card)
1272 struct snd_soc_pcm_runtime *rtd;
1275 for_each_comp_order(order) {
1276 for_each_card_rtds(card, rtd) {
1277 /* remove all rtd connected DAIs in good order */
1278 snd_soc_pcm_dai_remove(rtd, order);
1283 static int soc_probe_link_dais(struct snd_soc_card *card)
1285 struct snd_soc_pcm_runtime *rtd;
1288 for_each_comp_order(order) {
1289 for_each_card_rtds(card, rtd) {
1292 "ASoC: probe %s dai link %d late %d\n",
1293 card->name, rtd->num, order);
1295 /* probe all rtd connected DAIs in good order */
1296 ret = snd_soc_pcm_dai_probe(rtd, order);
1305 static void soc_remove_link_components(struct snd_soc_card *card)
1307 struct snd_soc_component *component;
1308 struct snd_soc_pcm_runtime *rtd;
1311 for_each_comp_order(order) {
1312 for_each_card_rtds(card, rtd) {
1313 for_each_rtd_components(rtd, i, component) {
1314 if (component->driver->remove_order != order)
1317 soc_remove_component(component, 1);
1323 static int soc_probe_link_components(struct snd_soc_card *card)
1325 struct snd_soc_component *component;
1326 struct snd_soc_pcm_runtime *rtd;
1329 for_each_comp_order(order) {
1330 for_each_card_rtds(card, rtd) {
1331 for_each_rtd_components(rtd, i, component) {
1332 if (component->driver->probe_order != order)
1335 ret = soc_probe_component(card, component);
1345 static void soc_unbind_aux_dev(struct snd_soc_card *card)
1347 struct snd_soc_component *component, *_component;
1349 for_each_card_auxs_safe(card, component, _component) {
1350 /* for snd_soc_component_init() */
1351 snd_soc_component_set_aux(component, NULL);
1352 list_del(&component->card_aux_list);
1356 static int soc_bind_aux_dev(struct snd_soc_card *card)
1358 struct snd_soc_component *component;
1359 struct snd_soc_aux_dev *aux;
1362 for_each_card_pre_auxs(card, i, aux) {
1363 /* codecs, usually analog devices */
1364 component = soc_find_component(&aux->dlc);
1366 return -EPROBE_DEFER;
1368 /* for snd_soc_component_init() */
1369 snd_soc_component_set_aux(component, aux);
1370 /* see for_each_card_auxs */
1371 list_add(&component->card_aux_list, &card->aux_comp_list);
1376 static int soc_probe_aux_devices(struct snd_soc_card *card)
1378 struct snd_soc_component *component;
1382 for_each_comp_order(order) {
1383 for_each_card_auxs(card, component) {
1384 if (component->driver->probe_order != order)
1387 ret = soc_probe_component(card, component);
1396 static void soc_remove_aux_devices(struct snd_soc_card *card)
1398 struct snd_soc_component *comp, *_comp;
1401 for_each_comp_order(order) {
1402 for_each_card_auxs_safe(card, comp, _comp) {
1403 if (comp->driver->remove_order == order)
1404 soc_remove_component(comp, 1);
1410 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1411 * @rtd: The runtime for which the DAI link format should be changed
1412 * @dai_fmt: The new DAI link format
1414 * This function updates the DAI link format for all DAIs connected to the DAI
1415 * link for the specified runtime.
1417 * Note: For setups with a static format set the dai_fmt field in the
1418 * corresponding snd_dai_link struct instead of using this function.
1420 * Returns 0 on success, otherwise a negative error code.
1422 int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1423 unsigned int dai_fmt)
1425 struct snd_soc_dai *cpu_dai;
1426 struct snd_soc_dai *codec_dai;
1427 unsigned int inv_dai_fmt;
1431 for_each_rtd_codec_dais(rtd, i, codec_dai) {
1432 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1433 if (ret != 0 && ret != -ENOTSUPP) {
1434 dev_warn(codec_dai->dev,
1435 "ASoC: Failed to set DAI format: %d\n", ret);
1441 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1442 * the component which has non_legacy_dai_naming is Codec
1444 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1445 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1446 case SND_SOC_DAIFMT_CBM_CFM:
1447 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1449 case SND_SOC_DAIFMT_CBM_CFS:
1450 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1452 case SND_SOC_DAIFMT_CBS_CFM:
1453 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1455 case SND_SOC_DAIFMT_CBS_CFS:
1456 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1459 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1460 unsigned int fmt = dai_fmt;
1462 if (cpu_dai->component->driver->non_legacy_dai_naming)
1465 ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
1466 if (ret != 0 && ret != -ENOTSUPP) {
1467 dev_warn(cpu_dai->dev,
1468 "ASoC: Failed to set DAI format: %d\n", ret);
1475 EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1479 * If a DMI filed contain strings in this blacklist (e.g.
1480 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
1481 * as invalid and dropped when setting the card long name from DMI info.
1483 static const char * const dmi_blacklist[] = {
1484 "To be filled by OEM",
1487 "Board Manufacturer",
1488 "Board Vendor Name",
1489 "Board Product Name",
1490 NULL, /* terminator */
1494 * Trim special characters, and replace '-' with '_' since '-' is used to
1495 * separate different DMI fields in the card long name. Only number and
1496 * alphabet characters and a few separator characters are kept.
1498 static void cleanup_dmi_name(char *name)
1502 for (i = 0; name[i]; i++) {
1503 if (isalnum(name[i]) || (name[i] == '.')
1504 || (name[i] == '_'))
1505 name[j++] = name[i];
1506 else if (name[i] == '-')
1514 * Check if a DMI field is valid, i.e. not containing any string
1515 * in the black list.
1517 static int is_dmi_valid(const char *field)
1521 while (dmi_blacklist[i]) {
1522 if (strstr(field, dmi_blacklist[i]))
1531 * Append a string to card->dmi_longname with character cleanups.
1533 static void append_dmi_string(struct snd_soc_card *card, const char *str)
1535 char *dst = card->dmi_longname;
1536 size_t dst_len = sizeof(card->dmi_longname);
1540 snprintf(dst + len, dst_len - len, "-%s", str);
1542 len++; /* skip the separator "-" */
1544 cleanup_dmi_name(dst + len);
1548 * snd_soc_set_dmi_name() - Register DMI names to card
1549 * @card: The card to register DMI names
1550 * @flavour: The flavour "differentiator" for the card amongst its peers.
1552 * An Intel machine driver may be used by many different devices but are
1553 * difficult for userspace to differentiate, since machine drivers ususally
1554 * use their own name as the card short name and leave the card long name
1555 * blank. To differentiate such devices and fix bugs due to lack of
1556 * device-specific configurations, this function allows DMI info to be used
1557 * as the sound card long name, in the format of
1558 * "vendor-product-version-board"
1559 * (Character '-' is used to separate different DMI fields here).
1560 * This will help the user space to load the device-specific Use Case Manager
1561 * (UCM) configurations for the card.
1563 * Possible card long names may be:
1564 * DellInc.-XPS139343-01-0310JH
1565 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1566 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1568 * This function also supports flavoring the card longname to provide
1569 * the extra differentiation, like "vendor-product-version-board-flavor".
1571 * We only keep number and alphabet characters and a few separator characters
1572 * in the card long name since UCM in the user space uses the card long names
1573 * as card configuration directory names and AudoConf cannot support special
1574 * charactors like SPACE.
1576 * Returns 0 on success, otherwise a negative error code.
1578 int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1580 const char *vendor, *product, *product_version, *board;
1582 if (card->long_name)
1583 return 0; /* long name already set by driver or from DMI */
1585 if (!is_acpi_device_node(card->dev->fwnode))
1588 /* make up dmi long name as: vendor-product-version-board */
1589 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1590 if (!vendor || !is_dmi_valid(vendor)) {
1591 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1595 snprintf(card->dmi_longname, sizeof(card->dmi_longname), "%s", vendor);
1596 cleanup_dmi_name(card->dmi_longname);
1598 product = dmi_get_system_info(DMI_PRODUCT_NAME);
1599 if (product && is_dmi_valid(product)) {
1600 append_dmi_string(card, product);
1603 * some vendors like Lenovo may only put a self-explanatory
1604 * name in the product version field
1606 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
1607 if (product_version && is_dmi_valid(product_version))
1608 append_dmi_string(card, product_version);
1611 board = dmi_get_system_info(DMI_BOARD_NAME);
1612 if (board && is_dmi_valid(board)) {
1613 if (!product || strcasecmp(board, product))
1614 append_dmi_string(card, board);
1615 } else if (!product) {
1616 /* fall back to using legacy name */
1617 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1621 /* Add flavour to dmi long name */
1623 append_dmi_string(card, flavour);
1625 /* set the card long name */
1626 card->long_name = card->dmi_longname;
1630 EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
1631 #endif /* CONFIG_DMI */
1633 static void soc_check_tplg_fes(struct snd_soc_card *card)
1635 struct snd_soc_component *component;
1636 const struct snd_soc_component_driver *comp_drv;
1637 struct snd_soc_dai_link *dai_link;
1640 for_each_component(component) {
1642 /* does this component override BEs ? */
1643 if (!component->driver->ignore_machine)
1646 /* for this machine ? */
1647 if (!strcmp(component->driver->ignore_machine,
1648 card->dev->driver->name))
1650 if (strcmp(component->driver->ignore_machine,
1651 dev_name(card->dev)))
1654 /* machine matches, so override the rtd data */
1655 for_each_card_prelinks(card, i, dai_link) {
1657 /* ignore this FE */
1658 if (dai_link->dynamic) {
1659 dai_link->ignore = true;
1663 dev_dbg(card->dev, "info: override BE DAI link %s\n",
1664 card->dai_link[i].name);
1666 /* override platform component */
1667 if (!dai_link->platforms) {
1668 dev_err(card->dev, "init platform error");
1671 dai_link->platforms->name = component->name;
1673 /* convert non BE into BE */
1674 if (!dai_link->no_pcm) {
1675 dai_link->no_pcm = 1;
1677 if (dai_link->dpcm_playback)
1679 "invalid configuration, dailink %s has flags no_pcm=0 and dpcm_playback=1\n",
1681 if (dai_link->dpcm_capture)
1683 "invalid configuration, dailink %s has flags no_pcm=0 and dpcm_capture=1\n",
1686 /* convert normal link into DPCM one */
1687 if (!(dai_link->dpcm_playback ||
1688 dai_link->dpcm_capture)) {
1689 dai_link->dpcm_playback = !dai_link->capture_only;
1690 dai_link->dpcm_capture = !dai_link->playback_only;
1695 * override any BE fixups
1697 * snd_soc_link_be_hw_params_fixup()
1699 dai_link->be_hw_params_fixup =
1700 component->driver->be_hw_params_fixup;
1703 * most BE links don't set stream name, so set it to
1704 * dai link name if it's NULL to help bind widgets.
1706 if (!dai_link->stream_name)
1707 dai_link->stream_name = dai_link->name;
1710 /* Inform userspace we are using alternate topology */
1711 if (component->driver->topology_name_prefix) {
1713 /* topology shortname created? */
1714 if (!card->topology_shortname_created) {
1715 comp_drv = component->driver;
1717 snprintf(card->topology_shortname, 32, "%s-%s",
1718 comp_drv->topology_name_prefix,
1720 card->topology_shortname_created = true;
1723 /* use topology shortname */
1724 card->name = card->topology_shortname;
1729 #define soc_setup_card_name(name, name1, name2, norm) \
1730 __soc_setup_card_name(name, sizeof(name), name1, name2, norm)
1731 static void __soc_setup_card_name(char *name, int len,
1732 const char *name1, const char *name2,
1737 snprintf(name, len, "%s", name1 ? name1 : name2);
1743 * Name normalization
1745 * The driver name is somewhat special, as it's used as a key for
1746 * searches in the user-space.
1749 * "abcd??efg" -> "abcd__efg"
1751 for (i = 0; i < len; i++) {
1758 if (!isalnum(name[i]))
1765 static void soc_cleanup_card_resources(struct snd_soc_card *card)
1767 struct snd_soc_pcm_runtime *rtd, *n;
1770 snd_card_disconnect_sync(card->snd_card);
1772 snd_soc_dapm_shutdown(card);
1774 /* remove and free each DAI */
1775 soc_remove_link_dais(card);
1776 soc_remove_link_components(card);
1778 for_each_card_rtds_safe(card, rtd, n)
1779 snd_soc_remove_pcm_runtime(card, rtd);
1781 /* remove auxiliary devices */
1782 soc_remove_aux_devices(card);
1783 soc_unbind_aux_dev(card);
1785 snd_soc_dapm_free(&card->dapm);
1786 soc_cleanup_card_debugfs(card);
1788 /* remove the card */
1789 snd_soc_card_remove(card);
1791 if (card->snd_card) {
1792 snd_card_free(card->snd_card);
1793 card->snd_card = NULL;
1797 static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
1799 if (card->instantiated) {
1800 card->instantiated = false;
1801 snd_soc_flush_all_delayed_work(card);
1803 soc_cleanup_card_resources(card);
1805 list_add(&card->list, &unbind_card_list);
1808 list_del(&card->list);
1812 static int snd_soc_bind_card(struct snd_soc_card *card)
1814 struct snd_soc_pcm_runtime *rtd;
1815 struct snd_soc_component *component;
1816 struct snd_soc_dai_link *dai_link;
1819 mutex_lock(&client_mutex);
1820 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1822 snd_soc_dapm_init(&card->dapm, card, NULL);
1824 /* check whether any platform is ignore machine FE and using topology */
1825 soc_check_tplg_fes(card);
1827 /* bind aux_devs too */
1828 ret = soc_bind_aux_dev(card);
1832 /* add predefined DAI links to the list */
1834 for_each_card_prelinks(card, i, dai_link) {
1835 ret = snd_soc_add_pcm_runtime(card, dai_link);
1840 /* card bind complete so register a sound card */
1841 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1842 card->owner, 0, &card->snd_card);
1845 "ASoC: can't create sound card for card %s: %d\n",
1850 soc_init_card_debugfs(card);
1852 soc_resume_init(card);
1854 ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1855 card->num_dapm_widgets);
1859 ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1860 card->num_of_dapm_widgets);
1864 /* initialise the sound card only once */
1865 ret = snd_soc_card_probe(card);
1869 /* probe all components used by DAI links on this card */
1870 ret = soc_probe_link_components(card);
1873 "ASoC: failed to instantiate card %d\n", ret);
1877 /* probe auxiliary components */
1878 ret = soc_probe_aux_devices(card);
1881 "ASoC: failed to probe aux component %d\n", ret);
1885 /* probe all DAI links on this card */
1886 ret = soc_probe_link_dais(card);
1889 "ASoC: failed to instantiate card %d\n", ret);
1893 for_each_card_rtds(card, rtd) {
1894 ret = soc_init_pcm_runtime(card, rtd);
1899 snd_soc_dapm_link_dai_widgets(card);
1900 snd_soc_dapm_connect_dai_link_widgets(card);
1902 ret = snd_soc_add_card_controls(card, card->controls,
1903 card->num_controls);
1907 ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1908 card->num_dapm_routes);
1910 if (card->disable_route_checks) {
1912 "%s: disable_route_checks set, ignoring errors on add_routes\n",
1916 "%s: snd_soc_dapm_add_routes failed: %d\n",
1922 ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
1923 card->num_of_dapm_routes);
1927 /* try to set some sane longname if DMI is available */
1928 snd_soc_set_dmi_name(card, NULL);
1930 soc_setup_card_name(card->snd_card->shortname,
1931 card->name, NULL, 0);
1932 soc_setup_card_name(card->snd_card->longname,
1933 card->long_name, card->name, 0);
1934 soc_setup_card_name(card->snd_card->driver,
1935 card->driver_name, card->name, 1);
1937 if (card->components) {
1938 /* the current implementation of snd_component_add() accepts */
1939 /* multiple components in the string separated by space, */
1940 /* but the string collision (identical string) check might */
1941 /* not work correctly */
1942 ret = snd_component_add(card->snd_card, card->components);
1944 dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n",
1950 ret = snd_soc_card_late_probe(card);
1954 snd_soc_dapm_new_widgets(card);
1956 ret = snd_card_register(card->snd_card);
1958 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1963 card->instantiated = 1;
1964 dapm_mark_endpoints_dirty(card);
1965 snd_soc_dapm_sync(&card->dapm);
1967 /* deactivate pins to sleep state */
1968 for_each_card_components(card, component)
1969 if (!snd_soc_component_active(component))
1970 pinctrl_pm_select_sleep_state(component->dev);
1974 soc_cleanup_card_resources(card);
1976 mutex_unlock(&card->mutex);
1977 mutex_unlock(&client_mutex);
1982 /* probes a new socdev */
1983 static int soc_probe(struct platform_device *pdev)
1985 struct snd_soc_card *card = platform_get_drvdata(pdev);
1988 * no card, so machine driver should be registering card
1989 * we should not be here in that case so ret error
1994 dev_warn(&pdev->dev,
1995 "ASoC: machine %s should use snd_soc_register_card()\n",
1998 /* Bodge while we unpick instantiation */
1999 card->dev = &pdev->dev;
2001 return devm_snd_soc_register_card(&pdev->dev, card);
2004 int snd_soc_poweroff(struct device *dev)
2006 struct snd_soc_card *card = dev_get_drvdata(dev);
2007 struct snd_soc_component *component;
2009 if (!card->instantiated)
2013 * Flush out pmdown_time work - we actually do want to run it
2014 * now, we're shutting down so no imminent restart.
2016 snd_soc_flush_all_delayed_work(card);
2018 snd_soc_dapm_shutdown(card);
2020 /* deactivate pins to sleep state */
2021 for_each_card_components(card, component)
2022 pinctrl_pm_select_sleep_state(component->dev);
2026 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2028 const struct dev_pm_ops snd_soc_pm_ops = {
2029 .suspend = snd_soc_suspend,
2030 .resume = snd_soc_resume,
2031 .freeze = snd_soc_suspend,
2032 .thaw = snd_soc_resume,
2033 .poweroff = snd_soc_poweroff,
2034 .restore = snd_soc_resume,
2036 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2038 /* ASoC platform driver */
2039 static struct platform_driver soc_driver = {
2041 .name = "soc-audio",
2042 .pm = &snd_soc_pm_ops,
2048 * snd_soc_cnew - create new control
2049 * @_template: control template
2050 * @data: control private data
2051 * @long_name: control long name
2052 * @prefix: control name prefix
2054 * Create a new mixer control from a template control.
2056 * Returns 0 for success, else error.
2058 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2059 void *data, const char *long_name,
2062 struct snd_kcontrol_new template;
2063 struct snd_kcontrol *kcontrol;
2066 memcpy(&template, _template, sizeof(template));
2070 long_name = template.name;
2073 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2077 template.name = name;
2079 template.name = long_name;
2082 kcontrol = snd_ctl_new1(&template, data);
2088 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2090 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2091 const struct snd_kcontrol_new *controls, int num_controls,
2092 const char *prefix, void *data)
2096 for (i = 0; i < num_controls; i++) {
2097 const struct snd_kcontrol_new *control = &controls[i];
2099 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2100 control->name, prefix));
2102 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2103 control->name, err);
2112 * snd_soc_add_component_controls - Add an array of controls to a component.
2114 * @component: Component to add controls to
2115 * @controls: Array of controls to add
2116 * @num_controls: Number of elements in the array
2118 * Return: 0 for success, else error.
2120 int snd_soc_add_component_controls(struct snd_soc_component *component,
2121 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2123 struct snd_card *card = component->card->snd_card;
2125 return snd_soc_add_controls(card, component->dev, controls,
2126 num_controls, component->name_prefix, component);
2128 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2131 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2132 * Convenience function to add a list of controls.
2134 * @soc_card: SoC card to add controls to
2135 * @controls: array of controls to add
2136 * @num_controls: number of elements in the array
2138 * Return 0 for success, else error.
2140 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2141 const struct snd_kcontrol_new *controls, int num_controls)
2143 struct snd_card *card = soc_card->snd_card;
2145 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2148 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2151 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2152 * Convienience function to add a list of controls.
2154 * @dai: DAI to add controls to
2155 * @controls: array of controls to add
2156 * @num_controls: number of elements in the array
2158 * Return 0 for success, else error.
2160 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2161 const struct snd_kcontrol_new *controls, int num_controls)
2163 struct snd_card *card = dai->component->card->snd_card;
2165 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2168 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2171 * snd_soc_register_card - Register a card with the ASoC core
2173 * @card: Card to register
2176 int snd_soc_register_card(struct snd_soc_card *card)
2178 if (!card->name || !card->dev)
2181 dev_set_drvdata(card->dev, card);
2183 INIT_LIST_HEAD(&card->widgets);
2184 INIT_LIST_HEAD(&card->paths);
2185 INIT_LIST_HEAD(&card->dapm_list);
2186 INIT_LIST_HEAD(&card->aux_comp_list);
2187 INIT_LIST_HEAD(&card->component_dev_list);
2188 INIT_LIST_HEAD(&card->list);
2189 INIT_LIST_HEAD(&card->rtd_list);
2190 INIT_LIST_HEAD(&card->dapm_dirty);
2191 INIT_LIST_HEAD(&card->dobj_list);
2193 card->instantiated = 0;
2194 mutex_init(&card->mutex);
2195 mutex_init(&card->dapm_mutex);
2196 mutex_init(&card->pcm_mutex);
2197 spin_lock_init(&card->dpcm_lock);
2199 return snd_soc_bind_card(card);
2201 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2204 * snd_soc_unregister_card - Unregister a card with the ASoC core
2206 * @card: Card to unregister
2209 int snd_soc_unregister_card(struct snd_soc_card *card)
2211 mutex_lock(&client_mutex);
2212 snd_soc_unbind_card(card, true);
2213 mutex_unlock(&client_mutex);
2214 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2218 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2221 * Simplify DAI link configuration by removing ".-1" from device names
2222 * and sanitizing names.
2224 static char *fmt_single_name(struct device *dev, int *id)
2226 const char *devname = dev_name(dev);
2230 if (devname == NULL)
2233 name = devm_kstrdup(dev, devname, GFP_KERNEL);
2237 /* are we a "%s.%d" name (platform and SPI components) */
2238 found = strstr(name, dev->driver->name);
2241 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2243 /* discard ID from name if ID == -1 */
2245 found[strlen(dev->driver->name)] = '\0';
2248 /* I2C component devices are named "bus-addr" */
2249 } else if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2251 /* create unique ID number from I2C addr and bus */
2252 *id = ((id1 & 0xffff) << 16) + id2;
2254 devm_kfree(dev, name);
2256 /* sanitize component name for DAI link creation */
2257 name = devm_kasprintf(dev, GFP_KERNEL, "%s.%s", dev->driver->name, devname);
2266 * Simplify DAI link naming for single devices with multiple DAIs by removing
2267 * any ".-1" and using the DAI name (instead of device name).
2269 static inline char *fmt_multiple_name(struct device *dev,
2270 struct snd_soc_dai_driver *dai_drv)
2272 if (dai_drv->name == NULL) {
2274 "ASoC: error - multiple DAI %s registered with no name\n",
2279 return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2282 void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2284 dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2285 list_del(&dai->list);
2287 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2290 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2292 * @component: The component the DAIs are registered for
2293 * @dai_drv: DAI driver to use for the DAI
2294 * @legacy_dai_naming: if %true, use legacy single-name format;
2295 * if %false, use multiple-name format;
2297 * Topology can use this API to register DAIs when probing a component.
2298 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2299 * will be freed in the component cleanup.
2301 struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
2302 struct snd_soc_dai_driver *dai_drv,
2303 bool legacy_dai_naming)
2305 struct device *dev = component->dev;
2306 struct snd_soc_dai *dai;
2308 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2310 lockdep_assert_held(&client_mutex);
2312 dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
2317 * Back in the old days when we still had component-less DAIs,
2318 * instead of having a static name, component-less DAIs would
2319 * inherit the name of the parent device so it is possible to
2320 * register multiple instances of the DAI. We still need to keep
2321 * the same naming style even though those DAIs are not
2322 * component-less anymore.
2324 if (legacy_dai_naming &&
2325 (dai_drv->id == 0 || dai_drv->name == NULL)) {
2326 dai->name = fmt_single_name(dev, &dai->id);
2328 dai->name = fmt_multiple_name(dev, dai_drv);
2330 dai->id = dai_drv->id;
2332 dai->id = component->num_dai;
2337 dai->component = component;
2339 dai->driver = dai_drv;
2341 /* see for_each_component_dais */
2342 list_add_tail(&dai->list, &component->dai_list);
2343 component->num_dai++;
2345 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2350 * snd_soc_unregister_dais - Unregister DAIs from the ASoC core
2352 * @component: The component for which the DAIs should be unregistered
2354 static void snd_soc_unregister_dais(struct snd_soc_component *component)
2356 struct snd_soc_dai *dai, *_dai;
2358 for_each_component_dais_safe(component, dai, _dai)
2359 snd_soc_unregister_dai(dai);
2363 * snd_soc_register_dais - Register a DAI with the ASoC core
2365 * @component: The component the DAIs are registered for
2366 * @dai_drv: DAI driver to use for the DAIs
2367 * @count: Number of DAIs
2369 static int snd_soc_register_dais(struct snd_soc_component *component,
2370 struct snd_soc_dai_driver *dai_drv,
2373 struct snd_soc_dai *dai;
2377 for (i = 0; i < count; i++) {
2378 dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
2379 !component->driver->non_legacy_dai_naming);
2389 snd_soc_unregister_dais(component);
2394 #define ENDIANNESS_MAP(name) \
2395 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2396 static u64 endianness_format_map[] = {
2397 ENDIANNESS_MAP(S16_),
2398 ENDIANNESS_MAP(U16_),
2399 ENDIANNESS_MAP(S24_),
2400 ENDIANNESS_MAP(U24_),
2401 ENDIANNESS_MAP(S32_),
2402 ENDIANNESS_MAP(U32_),
2403 ENDIANNESS_MAP(S24_3),
2404 ENDIANNESS_MAP(U24_3),
2405 ENDIANNESS_MAP(S20_3),
2406 ENDIANNESS_MAP(U20_3),
2407 ENDIANNESS_MAP(S18_3),
2408 ENDIANNESS_MAP(U18_3),
2409 ENDIANNESS_MAP(FLOAT_),
2410 ENDIANNESS_MAP(FLOAT64_),
2411 ENDIANNESS_MAP(IEC958_SUBFRAME_),
2415 * Fix up the DAI formats for endianness: codecs don't actually see
2416 * the endianness of the data but we're using the CPU format
2417 * definitions which do need to include endianness so we ensure that
2418 * codec DAIs always have both big and little endian variants set.
2420 static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2424 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2425 if (stream->formats & endianness_format_map[i])
2426 stream->formats |= endianness_format_map[i];
2429 static void snd_soc_try_rebind_card(void)
2431 struct snd_soc_card *card, *c;
2433 list_for_each_entry_safe(card, c, &unbind_card_list, list)
2434 if (!snd_soc_bind_card(card))
2435 list_del(&card->list);
2438 static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2440 struct snd_soc_card *card = component->card;
2442 snd_soc_unregister_dais(component);
2445 snd_soc_unbind_card(card, false);
2447 list_del(&component->list);
2450 int snd_soc_component_initialize(struct snd_soc_component *component,
2451 const struct snd_soc_component_driver *driver,
2454 INIT_LIST_HEAD(&component->dai_list);
2455 INIT_LIST_HEAD(&component->dobj_list);
2456 INIT_LIST_HEAD(&component->card_list);
2457 INIT_LIST_HEAD(&component->list);
2458 mutex_init(&component->io_mutex);
2460 component->name = fmt_single_name(dev, &component->id);
2461 if (!component->name) {
2462 dev_err(dev, "ASoC: Failed to allocate name\n");
2466 component->dev = dev;
2467 component->driver = driver;
2471 EXPORT_SYMBOL_GPL(snd_soc_component_initialize);
2473 int snd_soc_add_component(struct snd_soc_component *component,
2474 struct snd_soc_dai_driver *dai_drv,
2480 mutex_lock(&client_mutex);
2482 if (component->driver->endianness) {
2483 for (i = 0; i < num_dai; i++) {
2484 convert_endianness_formats(&dai_drv[i].playback);
2485 convert_endianness_formats(&dai_drv[i].capture);
2489 ret = snd_soc_register_dais(component, dai_drv, num_dai);
2491 dev_err(component->dev, "ASoC: Failed to register DAIs: %d\n",
2496 if (!component->driver->write && !component->driver->read) {
2497 if (!component->regmap)
2498 component->regmap = dev_get_regmap(component->dev,
2500 if (component->regmap)
2501 snd_soc_component_setup_regmap(component);
2504 /* see for_each_component */
2505 list_add(&component->list, &component_list);
2509 snd_soc_del_component_unlocked(component);
2511 mutex_unlock(&client_mutex);
2514 snd_soc_try_rebind_card();
2518 EXPORT_SYMBOL_GPL(snd_soc_add_component);
2520 int snd_soc_register_component(struct device *dev,
2521 const struct snd_soc_component_driver *component_driver,
2522 struct snd_soc_dai_driver *dai_drv,
2525 struct snd_soc_component *component;
2528 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
2532 ret = snd_soc_component_initialize(component, component_driver, dev);
2536 return snd_soc_add_component(component, dai_drv, num_dai);
2538 EXPORT_SYMBOL_GPL(snd_soc_register_component);
2541 * snd_soc_unregister_component_by_driver - Unregister component using a given driver
2542 * from the ASoC core
2544 * @dev: The device to unregister
2545 * @component_driver: The component driver to unregister
2547 void snd_soc_unregister_component_by_driver(struct device *dev,
2548 const struct snd_soc_component_driver *component_driver)
2550 struct snd_soc_component *component;
2552 if (!component_driver)
2555 mutex_lock(&client_mutex);
2556 component = snd_soc_lookup_component_nolocked(dev, component_driver->name);
2560 snd_soc_del_component_unlocked(component);
2563 mutex_unlock(&client_mutex);
2565 EXPORT_SYMBOL_GPL(snd_soc_unregister_component_by_driver);
2568 * snd_soc_unregister_component - Unregister all related component
2569 * from the ASoC core
2571 * @dev: The device to unregister
2573 void snd_soc_unregister_component(struct device *dev)
2575 struct snd_soc_component *component;
2577 mutex_lock(&client_mutex);
2579 component = snd_soc_lookup_component_nolocked(dev, NULL);
2583 snd_soc_del_component_unlocked(component);
2585 mutex_unlock(&client_mutex);
2587 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2589 /* Retrieve a card's name from device tree */
2590 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2591 const char *propname)
2593 struct device_node *np;
2597 pr_err("card->dev is not set before calling %s\n", __func__);
2601 np = card->dev->of_node;
2603 ret = of_property_read_string_index(np, propname, 0, &card->name);
2605 * EINVAL means the property does not exist. This is fine providing
2606 * card->name was previously set, which is checked later in
2607 * snd_soc_register_card.
2609 if (ret < 0 && ret != -EINVAL) {
2611 "ASoC: Property '%s' could not be read: %d\n",
2618 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2620 static const struct snd_soc_dapm_widget simple_widgets[] = {
2621 SND_SOC_DAPM_MIC("Microphone", NULL),
2622 SND_SOC_DAPM_LINE("Line", NULL),
2623 SND_SOC_DAPM_HP("Headphone", NULL),
2624 SND_SOC_DAPM_SPK("Speaker", NULL),
2627 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
2628 const char *propname)
2630 struct device_node *np = card->dev->of_node;
2631 struct snd_soc_dapm_widget *widgets;
2632 const char *template, *wname;
2633 int i, j, num_widgets, ret;
2635 num_widgets = of_property_count_strings(np, propname);
2636 if (num_widgets < 0) {
2638 "ASoC: Property '%s' does not exist\n", propname);
2641 if (num_widgets & 1) {
2643 "ASoC: Property '%s' length is not even\n", propname);
2649 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
2654 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
2658 "ASoC: Could not allocate memory for widgets\n");
2662 for (i = 0; i < num_widgets; i++) {
2663 ret = of_property_read_string_index(np, propname,
2667 "ASoC: Property '%s' index %d read error:%d\n",
2668 propname, 2 * i, ret);
2672 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
2673 if (!strncmp(template, simple_widgets[j].name,
2674 strlen(simple_widgets[j].name))) {
2675 widgets[i] = simple_widgets[j];
2680 if (j >= ARRAY_SIZE(simple_widgets)) {
2682 "ASoC: DAPM widget '%s' is not supported\n",
2687 ret = of_property_read_string_index(np, propname,
2692 "ASoC: Property '%s' index %d read error:%d\n",
2693 propname, (2 * i) + 1, ret);
2697 widgets[i].name = wname;
2700 card->of_dapm_widgets = widgets;
2701 card->num_of_dapm_widgets = num_widgets;
2705 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
2707 int snd_soc_of_get_slot_mask(struct device_node *np,
2708 const char *prop_name,
2712 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
2718 for (i = 0; i < val; i++)
2719 if (be32_to_cpup(&of_slot_mask[i]))
2724 EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
2726 int snd_soc_of_parse_tdm_slot(struct device_node *np,
2727 unsigned int *tx_mask,
2728 unsigned int *rx_mask,
2729 unsigned int *slots,
2730 unsigned int *slot_width)
2736 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
2738 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
2740 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
2741 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
2749 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
2750 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
2760 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
2762 void snd_soc_of_parse_node_prefix(struct device_node *np,
2763 struct snd_soc_codec_conf *codec_conf,
2764 struct device_node *of_node,
2765 const char *propname)
2770 ret = of_property_read_string(np, propname, &str);
2772 /* no prefix is not error */
2776 codec_conf->dlc.of_node = of_node;
2777 codec_conf->name_prefix = str;
2779 EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
2781 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
2782 const char *propname)
2784 struct device_node *np = card->dev->of_node;
2786 struct snd_soc_dapm_route *routes;
2789 num_routes = of_property_count_strings(np, propname);
2790 if (num_routes < 0 || num_routes & 1) {
2792 "ASoC: Property '%s' does not exist or its length is not even\n",
2798 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
2803 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
2807 "ASoC: Could not allocate DAPM route table\n");
2811 for (i = 0; i < num_routes; i++) {
2812 ret = of_property_read_string_index(np, propname,
2813 2 * i, &routes[i].sink);
2816 "ASoC: Property '%s' index %d could not be read: %d\n",
2817 propname, 2 * i, ret);
2820 ret = of_property_read_string_index(np, propname,
2821 (2 * i) + 1, &routes[i].source);
2824 "ASoC: Property '%s' index %d could not be read: %d\n",
2825 propname, (2 * i) + 1, ret);
2830 card->num_of_dapm_routes = num_routes;
2831 card->of_dapm_routes = routes;
2835 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
2837 int snd_soc_of_parse_aux_devs(struct snd_soc_card *card, const char *propname)
2839 struct device_node *node = card->dev->of_node;
2840 struct snd_soc_aux_dev *aux;
2843 num = of_count_phandle_with_args(node, propname, NULL);
2844 if (num == -ENOENT) {
2846 } else if (num < 0) {
2847 dev_err(card->dev, "ASOC: Property '%s' could not be read: %d\n",
2852 aux = devm_kcalloc(card->dev, num, sizeof(*aux), GFP_KERNEL);
2855 card->aux_dev = aux;
2856 card->num_aux_devs = num;
2858 for_each_card_pre_auxs(card, i, aux) {
2859 aux->dlc.of_node = of_parse_phandle(node, propname, i);
2860 if (!aux->dlc.of_node)
2866 EXPORT_SYMBOL_GPL(snd_soc_of_parse_aux_devs);
2868 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
2870 struct device_node **bitclkmaster,
2871 struct device_node **framemaster)
2875 unsigned int format = 0;
2881 } of_fmt_table[] = {
2882 { "i2s", SND_SOC_DAIFMT_I2S },
2883 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
2884 { "left_j", SND_SOC_DAIFMT_LEFT_J },
2885 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
2886 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
2887 { "ac97", SND_SOC_DAIFMT_AC97 },
2888 { "pdm", SND_SOC_DAIFMT_PDM},
2889 { "msb", SND_SOC_DAIFMT_MSB },
2890 { "lsb", SND_SOC_DAIFMT_LSB },
2897 * check "dai-format = xxx"
2898 * or "[prefix]format = xxx"
2899 * SND_SOC_DAIFMT_FORMAT_MASK area
2901 ret = of_property_read_string(np, "dai-format", &str);
2903 snprintf(prop, sizeof(prop), "%sformat", prefix);
2904 ret = of_property_read_string(np, prop, &str);
2907 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
2908 if (strcmp(str, of_fmt_table[i].name) == 0) {
2909 format |= of_fmt_table[i].val;
2916 * check "[prefix]continuous-clock"
2917 * SND_SOC_DAIFMT_CLOCK_MASK area
2919 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
2920 if (of_property_read_bool(np, prop))
2921 format |= SND_SOC_DAIFMT_CONT;
2923 format |= SND_SOC_DAIFMT_GATED;
2926 * check "[prefix]bitclock-inversion"
2927 * check "[prefix]frame-inversion"
2928 * SND_SOC_DAIFMT_INV_MASK area
2930 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
2931 bit = !!of_get_property(np, prop, NULL);
2933 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
2934 frame = !!of_get_property(np, prop, NULL);
2936 switch ((bit << 4) + frame) {
2938 format |= SND_SOC_DAIFMT_IB_IF;
2941 format |= SND_SOC_DAIFMT_IB_NF;
2944 format |= SND_SOC_DAIFMT_NB_IF;
2947 /* SND_SOC_DAIFMT_NB_NF is default */
2952 * check "[prefix]bitclock-master"
2953 * check "[prefix]frame-master"
2954 * SND_SOC_DAIFMT_MASTER_MASK area
2956 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
2957 bit = !!of_get_property(np, prop, NULL);
2958 if (bit && bitclkmaster)
2959 *bitclkmaster = of_parse_phandle(np, prop, 0);
2961 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
2962 frame = !!of_get_property(np, prop, NULL);
2963 if (frame && framemaster)
2964 *framemaster = of_parse_phandle(np, prop, 0);
2966 switch ((bit << 4) + frame) {
2968 format |= SND_SOC_DAIFMT_CBM_CFM;
2971 format |= SND_SOC_DAIFMT_CBM_CFS;
2974 format |= SND_SOC_DAIFMT_CBS_CFM;
2977 format |= SND_SOC_DAIFMT_CBS_CFS;
2983 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
2985 int snd_soc_get_dai_id(struct device_node *ep)
2987 struct snd_soc_component *component;
2988 struct snd_soc_dai_link_component dlc;
2991 dlc.of_node = of_graph_get_port_parent(ep);
2994 * For example HDMI case, HDMI has video/sound port,
2995 * but ALSA SoC needs sound port number only.
2996 * Thus counting HDMI DT port/endpoint doesn't work.
2997 * Then, it should have .of_xlate_dai_id
3000 mutex_lock(&client_mutex);
3001 component = soc_find_component(&dlc);
3003 ret = snd_soc_component_of_xlate_dai_id(component, ep);
3004 mutex_unlock(&client_mutex);
3006 of_node_put(dlc.of_node);
3010 EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3012 int snd_soc_get_dai_name(struct of_phandle_args *args,
3013 const char **dai_name)
3015 struct snd_soc_component *pos;
3016 struct device_node *component_of_node;
3017 int ret = -EPROBE_DEFER;
3019 mutex_lock(&client_mutex);
3020 for_each_component(pos) {
3021 component_of_node = soc_component_to_node(pos);
3023 if (component_of_node != args->np || !pos->num_dai)
3026 ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3027 if (ret == -ENOTSUPP) {
3028 struct snd_soc_dai *dai;
3031 switch (args->args_count) {
3033 id = 0; /* same as dai_drv[0] */
3043 if (id < 0 || id >= pos->num_dai) {
3050 /* find target DAI */
3051 for_each_component_dais(pos, dai) {
3057 *dai_name = dai->driver->name;
3059 *dai_name = pos->name;
3062 * if another error than ENOTSUPP is returned go on and
3063 * check if another component is provided with the same
3064 * node. This may happen if a device provides several
3072 mutex_unlock(&client_mutex);
3075 EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
3077 int snd_soc_of_get_dai_name(struct device_node *of_node,
3078 const char **dai_name)
3080 struct of_phandle_args args;
3083 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3084 "#sound-dai-cells", 0, &args);
3088 ret = snd_soc_get_dai_name(&args, dai_name);
3090 of_node_put(args.np);
3094 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3097 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3098 * @dai_link: DAI link
3100 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3102 void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3104 struct snd_soc_dai_link_component *component;
3107 for_each_link_codecs(dai_link, index, component) {
3108 if (!component->of_node)
3110 of_node_put(component->of_node);
3111 component->of_node = NULL;
3114 EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3117 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3119 * @of_node: Device node
3120 * @dai_link: DAI link
3122 * Builds an array of CODEC DAI components from the DAI link property
3124 * The array is set in the DAI link and the number of DAIs is set accordingly.
3125 * The device nodes in the array (of_node) must be dereferenced by calling
3126 * snd_soc_of_put_dai_link_codecs() on @dai_link.
3128 * Returns 0 for success
3130 int snd_soc_of_get_dai_link_codecs(struct device *dev,
3131 struct device_node *of_node,
3132 struct snd_soc_dai_link *dai_link)
3134 struct of_phandle_args args;
3135 struct snd_soc_dai_link_component *component;
3137 int index, num_codecs, ret;
3139 /* Count the number of CODECs */
3141 num_codecs = of_count_phandle_with_args(of_node, name,
3142 "#sound-dai-cells");
3143 if (num_codecs <= 0) {
3144 if (num_codecs == -ENOENT)
3145 dev_err(dev, "No 'sound-dai' property\n");
3147 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3150 component = devm_kcalloc(dev,
3151 num_codecs, sizeof(*component),
3155 dai_link->codecs = component;
3156 dai_link->num_codecs = num_codecs;
3158 /* Parse the list */
3159 for_each_link_codecs(dai_link, index, component) {
3160 ret = of_parse_phandle_with_args(of_node, name,
3165 component->of_node = args.np;
3166 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3172 snd_soc_of_put_dai_link_codecs(dai_link);
3173 dai_link->codecs = NULL;
3174 dai_link->num_codecs = 0;
3177 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3179 static int __init snd_soc_init(void)
3183 snd_soc_debugfs_init();
3184 ret = snd_soc_util_init();
3188 ret = platform_driver_register(&soc_driver);
3194 snd_soc_util_exit();
3196 snd_soc_debugfs_exit();
3199 module_init(snd_soc_init);
3201 static void __exit snd_soc_exit(void)
3203 snd_soc_util_exit();
3204 snd_soc_debugfs_exit();
3206 platform_driver_unregister(&soc_driver);
3208 module_exit(snd_soc_exit);
3210 /* Module information */
3211 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3212 MODULE_DESCRIPTION("ALSA SoC Core");
3213 MODULE_LICENSE("GPL");
3214 MODULE_ALIAS("platform:soc-audio");