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 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 static 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;
332 struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
333 const char *driver_name)
335 struct snd_soc_component *component;
337 mutex_lock(&client_mutex);
338 component = snd_soc_lookup_component_nolocked(dev, driver_name);
339 mutex_unlock(&client_mutex);
343 EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
345 struct snd_soc_pcm_runtime
346 *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
347 struct snd_soc_dai_link *dai_link)
349 struct snd_soc_pcm_runtime *rtd;
351 for_each_card_rtds(card, rtd) {
352 if (rtd->dai_link == dai_link)
355 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link->name);
358 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
361 * Power down the audio subsystem pmdown_time msecs after close is called.
362 * This is to ensure there are no pops or clicks in between any music tracks
363 * due to DAPM power cycling.
365 void snd_soc_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
367 struct snd_soc_dai *codec_dai = rtd->codec_dai;
368 int playback = SNDRV_PCM_STREAM_PLAYBACK;
370 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
373 "ASoC: pop wq checking: %s status: %s waiting: %s\n",
374 codec_dai->driver->playback.stream_name,
375 codec_dai->stream_active[playback] ? "active" : "inactive",
376 rtd->pop_wait ? "yes" : "no");
378 /* are we waiting on this codec DAI stream */
379 if (rtd->pop_wait == 1) {
381 snd_soc_dapm_stream_event(rtd, playback,
382 SND_SOC_DAPM_STREAM_STOP);
385 mutex_unlock(&rtd->card->pcm_mutex);
387 EXPORT_SYMBOL_GPL(snd_soc_close_delayed_work);
389 static void soc_release_rtd_dev(struct device *dev)
391 /* "dev" means "rtd->dev" */
395 static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
400 list_del(&rtd->list);
402 if (delayed_work_pending(&rtd->delayed_work))
403 flush_delayed_work(&rtd->delayed_work);
404 snd_soc_pcm_component_free(rtd);
407 * we don't need to call kfree() for rtd->dev
409 * soc_release_rtd_dev()
411 * We don't need rtd->dev NULL check, because
412 * it is alloced *before* rtd.
414 * soc_new_pcm_runtime()
416 device_unregister(rtd->dev);
419 static void close_delayed_work(struct work_struct *work) {
420 struct snd_soc_pcm_runtime *rtd =
421 container_of(work, struct snd_soc_pcm_runtime,
424 if (rtd->close_delayed_work_func)
425 rtd->close_delayed_work_func(rtd);
428 static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
429 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
431 struct snd_soc_pcm_runtime *rtd;
432 struct snd_soc_component *component;
440 dev = kzalloc(sizeof(struct device), GFP_KERNEL);
444 dev->parent = card->dev;
445 dev->release = soc_release_rtd_dev;
446 dev->groups = soc_dev_attr_groups;
448 dev_set_name(dev, "%s", dai_link->name);
450 ret = device_register(dev);
452 put_device(dev); /* soc_release_rtd_dev */
459 rtd = devm_kzalloc(dev,
461 sizeof(*component) * (dai_link->num_cpus +
462 dai_link->num_codecs +
463 dai_link->num_platforms),
469 INIT_LIST_HEAD(&rtd->list);
470 for_each_pcm_streams(stream) {
471 INIT_LIST_HEAD(&rtd->dpcm[stream].be_clients);
472 INIT_LIST_HEAD(&rtd->dpcm[stream].fe_clients);
474 dev_set_drvdata(dev, rtd);
475 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
480 rtd->dais = devm_kcalloc(dev, dai_link->num_cpus + dai_link->num_codecs,
481 sizeof(struct snd_soc_dai *),
487 * dais = [][][][][][][][][][][][][][][][][][]
488 * ^cpu_dais ^codec_dais
489 * |--- num_cpus ---|--- num_codecs --|
491 rtd->cpu_dais = &rtd->dais[0];
492 rtd->codec_dais = &rtd->dais[dai_link->num_cpus];
495 * rtd remaining settings
498 rtd->dai_link = dai_link;
500 /* see for_each_card_rtds */
501 list_add_tail(&rtd->list, &card->rtd_list);
502 rtd->num = card->num_rtd;
508 soc_free_pcm_runtime(rtd);
512 static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
514 struct snd_soc_pcm_runtime *rtd;
516 for_each_card_rtds(card, rtd)
517 flush_delayed_work(&rtd->delayed_work);
520 #ifdef CONFIG_PM_SLEEP
521 /* powers down audio subsystem for suspend */
522 int snd_soc_suspend(struct device *dev)
524 struct snd_soc_card *card = dev_get_drvdata(dev);
525 struct snd_soc_component *component;
526 struct snd_soc_pcm_runtime *rtd;
527 int playback = SNDRV_PCM_STREAM_PLAYBACK;
530 /* If the card is not initialized yet there is nothing to do */
531 if (!card->instantiated)
535 * Due to the resume being scheduled into a workqueue we could
536 * suspend before that's finished - wait for it to complete.
538 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
540 /* we're going to block userspace touching us until resume completes */
541 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
543 /* mute any active DACs */
544 for_each_card_rtds(card, rtd) {
545 struct snd_soc_dai *dai;
547 if (rtd->dai_link->ignore_suspend)
550 for_each_rtd_codec_dais(rtd, i, dai) {
551 if (dai->stream_active[playback])
552 snd_soc_dai_digital_mute(dai, 1, playback);
556 /* suspend all pcms */
557 for_each_card_rtds(card, rtd) {
558 if (rtd->dai_link->ignore_suspend)
561 snd_pcm_suspend_all(rtd->pcm);
564 if (card->suspend_pre)
565 card->suspend_pre(card);
567 /* close any waiting streams */
568 snd_soc_flush_all_delayed_work(card);
570 for_each_card_rtds(card, rtd) {
573 if (rtd->dai_link->ignore_suspend)
576 for_each_pcm_streams(stream)
577 snd_soc_dapm_stream_event(rtd, stream,
578 SND_SOC_DAPM_STREAM_SUSPEND);
581 /* Recheck all endpoints too, their state is affected by suspend */
582 dapm_mark_endpoints_dirty(card);
583 snd_soc_dapm_sync(&card->dapm);
585 /* suspend all COMPONENTs */
586 for_each_card_rtds(card, rtd) {
588 if (rtd->dai_link->ignore_suspend)
591 for_each_rtd_components(rtd, i, component) {
592 struct snd_soc_dapm_context *dapm =
593 snd_soc_component_get_dapm(component);
596 * ignore if component was already suspended
598 if (snd_soc_component_is_suspended(component))
602 * If there are paths active then the COMPONENT will be
603 * held with bias _ON and should not be suspended.
605 switch (snd_soc_dapm_get_bias_level(dapm)) {
606 case SND_SOC_BIAS_STANDBY:
608 * If the COMPONENT is capable of idle
609 * bias off then being in STANDBY
610 * means it's doing something,
611 * otherwise fall through.
613 if (dapm->idle_bias_off) {
614 dev_dbg(component->dev,
615 "ASoC: idle_bias_off CODEC on over suspend\n");
620 case SND_SOC_BIAS_OFF:
621 snd_soc_component_suspend(component);
622 if (component->regmap)
623 regcache_mark_dirty(component->regmap);
624 /* deactivate pins to sleep state */
625 pinctrl_pm_select_sleep_state(component->dev);
628 dev_dbg(component->dev,
629 "ASoC: COMPONENT is on over suspend\n");
635 if (card->suspend_post)
636 card->suspend_post(card);
640 EXPORT_SYMBOL_GPL(snd_soc_suspend);
643 * deferred resume work, so resume can complete before we finished
644 * setting our codec back up, which can be very slow on I2C
646 static void soc_resume_deferred(struct work_struct *work)
648 struct snd_soc_card *card =
649 container_of(work, struct snd_soc_card,
650 deferred_resume_work);
651 struct snd_soc_pcm_runtime *rtd;
652 struct snd_soc_component *component;
656 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
657 * so userspace apps are blocked from touching us
660 dev_dbg(card->dev, "ASoC: starting resume work\n");
662 /* Bring us up into D2 so that DAPM starts enabling things */
663 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
665 if (card->resume_pre)
666 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_codec_dais(rtd, i, dai) {
693 if (dai->stream_active[playback])
694 snd_soc_dai_digital_mute(dai, 0, playback);
698 if (card->resume_post)
699 card->resume_post(card);
701 dev_dbg(card->dev, "ASoC: resume work completed\n");
703 /* Recheck all endpoints too, their state is affected by suspend */
704 dapm_mark_endpoints_dirty(card);
705 snd_soc_dapm_sync(&card->dapm);
707 /* userspace can access us now we are back as we were before */
708 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
711 /* powers up audio subsystem after a suspend */
712 int snd_soc_resume(struct device *dev)
714 struct snd_soc_card *card = dev_get_drvdata(dev);
715 struct snd_soc_component *component;
717 /* If the card is not initialized yet there is nothing to do */
718 if (!card->instantiated)
721 /* activate pins from sleep state */
722 for_each_card_components(card, component)
723 if (component->active)
724 pinctrl_pm_select_default_state(component->dev);
726 dev_dbg(dev, "ASoC: Scheduling resume work\n");
727 if (!schedule_work(&card->deferred_resume_work))
728 dev_err(dev, "ASoC: resume work item may be lost\n");
732 EXPORT_SYMBOL_GPL(snd_soc_resume);
734 static void soc_resume_init(struct snd_soc_card *card)
736 /* deferred resume work */
737 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
740 #define snd_soc_suspend NULL
741 #define snd_soc_resume NULL
742 static inline void soc_resume_init(struct snd_soc_card *card)
747 static const struct snd_soc_dai_ops null_dai_ops = {
750 static struct device_node
751 *soc_component_to_node(struct snd_soc_component *component)
753 struct device_node *of_node;
755 of_node = component->dev->of_node;
756 if (!of_node && component->dev->parent)
757 of_node = component->dev->parent->of_node;
762 static int snd_soc_is_matching_component(
763 const struct snd_soc_dai_link_component *dlc,
764 struct snd_soc_component *component)
766 struct device_node *component_of_node;
771 component_of_node = soc_component_to_node(component);
773 if (dlc->of_node && component_of_node != dlc->of_node)
775 if (dlc->name && strcmp(component->name, dlc->name))
781 static struct snd_soc_component *soc_find_component(
782 const struct snd_soc_dai_link_component *dlc)
784 struct snd_soc_component *component;
786 lockdep_assert_held(&client_mutex);
791 * It returns *1st* found component, but some driver
792 * has few components by same of_node/name
794 * CPU component and generic DMAEngine component
796 for_each_component(component)
797 if (snd_soc_is_matching_component(dlc, component))
804 * snd_soc_find_dai - Find a registered DAI
806 * @dlc: name of the DAI or the DAI driver and optional component info to match
808 * This function will search all registered components and their DAIs to
809 * find the DAI of the same name. The component's of_node and name
810 * should also match if being specified.
812 * Return: pointer of DAI, or NULL if not found.
814 struct snd_soc_dai *snd_soc_find_dai(
815 const struct snd_soc_dai_link_component *dlc)
817 struct snd_soc_component *component;
818 struct snd_soc_dai *dai;
820 lockdep_assert_held(&client_mutex);
822 /* Find CPU DAI from registered DAIs */
823 for_each_component(component) {
824 if (!snd_soc_is_matching_component(dlc, component))
826 for_each_component_dais(component, dai) {
827 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
828 && (!dai->driver->name
829 || strcmp(dai->driver->name, dlc->dai_name)))
838 EXPORT_SYMBOL_GPL(snd_soc_find_dai);
840 static int soc_dai_link_sanity_check(struct snd_soc_card *card,
841 struct snd_soc_dai_link *link)
844 struct snd_soc_dai_link_component *cpu, *codec, *platform;
846 for_each_link_codecs(link, i, codec) {
848 * Codec must be specified by 1 of name or OF node,
849 * not both or neither.
851 if (!!codec->name == !!codec->of_node) {
852 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
857 /* Codec DAI name must be specified */
858 if (!codec->dai_name) {
859 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
865 * Defer card registration if codec component is not added to
868 if (!soc_find_component(codec))
869 return -EPROBE_DEFER;
872 for_each_link_platforms(link, i, platform) {
874 * Platform may be specified by either name or OF node, but it
875 * can be left unspecified, then no components will be inserted
878 if (!!platform->name == !!platform->of_node) {
880 "ASoC: Neither/both platform name/of_node are set for %s\n",
886 * Defer card registration if platform component is not added to
889 if (!soc_find_component(platform))
890 return -EPROBE_DEFER;
893 for_each_link_cpus(link, i, cpu) {
895 * CPU device may be specified by either name or OF node, but
896 * can be left unspecified, and will be matched based on DAI
899 if (cpu->name && cpu->of_node) {
901 "ASoC: Neither/both cpu name/of_node are set for %s\n",
907 * Defer card registration if cpu dai component is not added to
910 if ((cpu->of_node || cpu->name) &&
911 !soc_find_component(cpu))
912 return -EPROBE_DEFER;
915 * At least one of CPU DAI name or CPU device name/node must be
918 if (!cpu->dai_name &&
919 !(cpu->name || cpu->of_node)) {
921 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
931 * snd_soc_remove_pcm_runtime - Remove a pcm_runtime from card
932 * @card: The ASoC card to which the pcm_runtime has
933 * @rtd: The pcm_runtime to remove
935 * This function removes a pcm_runtime from the ASoC card.
937 void snd_soc_remove_pcm_runtime(struct snd_soc_card *card,
938 struct snd_soc_pcm_runtime *rtd)
940 lockdep_assert_held(&client_mutex);
943 * Notify the machine driver for extra destruction
945 if (card->remove_dai_link)
946 card->remove_dai_link(card, rtd->dai_link);
948 soc_free_pcm_runtime(rtd);
950 EXPORT_SYMBOL_GPL(snd_soc_remove_pcm_runtime);
953 * snd_soc_add_pcm_runtime - Add a pcm_runtime dynamically via dai_link
954 * @card: The ASoC card to which the pcm_runtime is added
955 * @dai_link: The DAI link to find pcm_runtime
957 * This function adds a pcm_runtime ASoC card by using dai_link.
959 * Note: Topology can use this API to add pcm_runtime when probing the
960 * topology component. And machine drivers can still define static
961 * DAI links in dai_link array.
963 int snd_soc_add_pcm_runtime(struct snd_soc_card *card,
964 struct snd_soc_dai_link *dai_link)
966 struct snd_soc_pcm_runtime *rtd;
967 struct snd_soc_dai_link_component *codec, *platform, *cpu;
968 struct snd_soc_component *component;
971 lockdep_assert_held(&client_mutex);
974 * Notify the machine driver for extra initialization
976 if (card->add_dai_link)
977 card->add_dai_link(card, dai_link);
979 if (dai_link->ignore)
982 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
984 ret = soc_dai_link_sanity_check(card, dai_link);
988 rtd = soc_new_pcm_runtime(card, dai_link);
992 rtd->num_cpus = dai_link->num_cpus;
993 for_each_link_cpus(dai_link, i, cpu) {
994 rtd->cpu_dais[i] = snd_soc_find_dai(cpu);
995 if (!rtd->cpu_dais[i]) {
996 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
1000 snd_soc_rtd_add_component(rtd, rtd->cpu_dais[i]->component);
1003 /* Single cpu links expect cpu and cpu_dai in runtime data */
1004 rtd->cpu_dai = rtd->cpu_dais[0];
1006 /* Find CODEC from registered CODECs */
1007 rtd->num_codecs = dai_link->num_codecs;
1008 for_each_link_codecs(dai_link, i, codec) {
1009 rtd->codec_dais[i] = snd_soc_find_dai(codec);
1010 if (!rtd->codec_dais[i]) {
1011 dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
1016 snd_soc_rtd_add_component(rtd, rtd->codec_dais[i]->component);
1019 /* Single codec links expect codec and codec_dai in runtime data */
1020 rtd->codec_dai = rtd->codec_dais[0];
1022 /* Find PLATFORM from registered PLATFORMs */
1023 for_each_link_platforms(dai_link, i, platform) {
1024 for_each_component(component) {
1025 if (!snd_soc_is_matching_component(platform, component))
1028 snd_soc_rtd_add_component(rtd, component);
1035 snd_soc_remove_pcm_runtime(card, rtd);
1036 return -EPROBE_DEFER;
1038 EXPORT_SYMBOL_GPL(snd_soc_add_pcm_runtime);
1040 static int soc_dai_pcm_new(struct snd_soc_pcm_runtime *rtd)
1042 struct snd_soc_dai *dai;
1045 for_each_rtd_dais(rtd, i, dai) {
1046 struct snd_soc_dai_driver *drv = dai->driver;
1049 ret = drv->pcm_new(rtd, dai);
1052 "ASoC: Failed to bind %s with pcm device\n",
1061 static int soc_init_pcm_runtime(struct snd_soc_card *card,
1062 struct snd_soc_pcm_runtime *rtd)
1064 struct snd_soc_dai_link *dai_link = rtd->dai_link;
1065 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1066 struct snd_soc_component *component;
1069 /* set default power off timeout */
1070 rtd->pmdown_time = pmdown_time;
1072 /* do machine specific initialization */
1073 if (dai_link->init) {
1074 ret = dai_link->init(rtd);
1076 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1077 dai_link->name, ret);
1082 if (dai_link->dai_fmt) {
1083 ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1088 /* add DPCM sysfs entries */
1089 soc_dpcm_debugfs_add(rtd);
1094 * most drivers will register their PCMs using DAI link ordering but
1095 * topology based drivers can use the DAI link id field to set PCM
1096 * device number and then use rtd + a base offset of the BEs.
1098 for_each_rtd_components(rtd, i, component) {
1099 if (!component->driver->use_dai_pcm_id)
1102 if (rtd->dai_link->no_pcm)
1103 num += component->driver->be_pcm_base;
1105 num = rtd->dai_link->id;
1108 /* create compress_device if possible */
1109 ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1110 if (ret != -ENOTSUPP) {
1112 dev_err(card->dev, "ASoC: can't create compress %s\n",
1113 dai_link->stream_name);
1117 /* create the pcm */
1118 ret = soc_new_pcm(rtd, num);
1120 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1121 dai_link->stream_name, ret);
1125 return soc_dai_pcm_new(rtd);
1128 static void soc_set_name_prefix(struct snd_soc_card *card,
1129 struct snd_soc_component *component)
1131 struct device_node *of_node = soc_component_to_node(component);
1135 for (i = 0; i < card->num_configs; i++) {
1136 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1138 if (snd_soc_is_matching_component(&map->dlc, component)) {
1139 component->name_prefix = map->name_prefix;
1145 * If there is no configuration table or no match in the table,
1146 * check if a prefix is provided in the node
1148 ret = of_property_read_string(of_node, "sound-name-prefix", &str);
1152 component->name_prefix = str;
1155 static void soc_remove_component(struct snd_soc_component *component,
1159 if (!component->card)
1163 snd_soc_component_remove(component);
1165 /* For framework level robustness */
1166 snd_soc_component_set_jack(component, NULL, NULL);
1168 list_del_init(&component->card_list);
1169 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1170 soc_cleanup_component_debugfs(component);
1171 component->card = NULL;
1172 snd_soc_component_module_put_when_remove(component);
1175 static int soc_probe_component(struct snd_soc_card *card,
1176 struct snd_soc_component *component)
1178 struct snd_soc_dapm_context *dapm =
1179 snd_soc_component_get_dapm(component);
1180 struct snd_soc_dai *dai;
1184 if (!strcmp(component->name, "snd-soc-dummy"))
1187 if (component->card) {
1188 if (component->card != card) {
1189 dev_err(component->dev,
1190 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1191 card->name, component->card->name);
1197 ret = snd_soc_component_module_get_when_probe(component);
1201 component->card = card;
1202 soc_set_name_prefix(card, component);
1204 soc_init_component_debugfs(component);
1206 snd_soc_dapm_init(dapm, card, component);
1208 ret = snd_soc_dapm_new_controls(dapm,
1209 component->driver->dapm_widgets,
1210 component->driver->num_dapm_widgets);
1213 dev_err(component->dev,
1214 "Failed to create new controls %d\n", ret);
1218 for_each_component_dais(component, dai) {
1219 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1221 dev_err(component->dev,
1222 "Failed to create DAI widgets %d\n", ret);
1227 ret = snd_soc_component_probe(component);
1229 dev_err(component->dev,
1230 "ASoC: failed to probe component %d\n", ret);
1233 WARN(dapm->idle_bias_off &&
1234 dapm->bias_level != SND_SOC_BIAS_OFF,
1235 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1239 /* machine specific init */
1240 if (component->init) {
1241 ret = component->init(component);
1243 dev_err(component->dev,
1244 "Failed to do machine specific init %d\n", ret);
1249 ret = snd_soc_add_component_controls(component,
1250 component->driver->controls,
1251 component->driver->num_controls);
1255 ret = snd_soc_dapm_add_routes(dapm,
1256 component->driver->dapm_routes,
1257 component->driver->num_dapm_routes);
1259 if (card->disable_route_checks) {
1261 "%s: disable_route_checks set, ignoring errors on add_routes\n",
1265 "%s: snd_soc_dapm_add_routes failed: %d\n",
1271 /* see for_each_card_components */
1272 list_add(&component->card_list, &card->component_dev_list);
1276 soc_remove_component(component, probed);
1281 static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1285 if (!dai || !dai->probed || !dai->driver ||
1286 dai->driver->remove_order != order)
1289 err = snd_soc_dai_remove(dai);
1292 "ASoC: failed to remove %s: %d\n",
1298 static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1303 dai->driver->probe_order != order)
1306 ret = snd_soc_dai_probe(dai);
1308 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1318 static void soc_remove_link_dais(struct snd_soc_card *card)
1321 struct snd_soc_dai *dai;
1322 struct snd_soc_pcm_runtime *rtd;
1325 for_each_comp_order(order) {
1326 for_each_card_rtds(card, rtd) {
1328 for_each_rtd_dais(rtd, i, dai)
1329 soc_remove_dai(dai, order);
1334 static int soc_probe_link_dais(struct snd_soc_card *card)
1336 struct snd_soc_dai *dai;
1337 struct snd_soc_pcm_runtime *rtd;
1340 for_each_comp_order(order) {
1341 for_each_card_rtds(card, rtd) {
1344 "ASoC: probe %s dai link %d late %d\n",
1345 card->name, rtd->num, order);
1347 /* probe the CPU DAI */
1348 for_each_rtd_dais(rtd, i, dai) {
1349 ret = soc_probe_dai(dai, order);
1359 static void soc_remove_link_components(struct snd_soc_card *card)
1361 struct snd_soc_component *component;
1362 struct snd_soc_pcm_runtime *rtd;
1365 for_each_comp_order(order) {
1366 for_each_card_rtds(card, rtd) {
1367 for_each_rtd_components(rtd, i, component) {
1368 if (component->driver->remove_order != order)
1371 soc_remove_component(component, 1);
1377 static int soc_probe_link_components(struct snd_soc_card *card)
1379 struct snd_soc_component *component;
1380 struct snd_soc_pcm_runtime *rtd;
1383 for_each_comp_order(order) {
1384 for_each_card_rtds(card, rtd) {
1385 for_each_rtd_components(rtd, i, component) {
1386 if (component->driver->probe_order != order)
1389 ret = soc_probe_component(card, component);
1399 static void soc_unbind_aux_dev(struct snd_soc_card *card)
1401 struct snd_soc_component *component, *_component;
1403 for_each_card_auxs_safe(card, component, _component) {
1404 component->init = NULL;
1405 list_del(&component->card_aux_list);
1409 static int soc_bind_aux_dev(struct snd_soc_card *card)
1411 struct snd_soc_component *component;
1412 struct snd_soc_aux_dev *aux;
1415 for_each_card_pre_auxs(card, i, aux) {
1416 /* codecs, usually analog devices */
1417 component = soc_find_component(&aux->dlc);
1419 return -EPROBE_DEFER;
1421 component->init = aux->init;
1422 /* see for_each_card_auxs */
1423 list_add(&component->card_aux_list, &card->aux_comp_list);
1428 static int soc_probe_aux_devices(struct snd_soc_card *card)
1430 struct snd_soc_component *component;
1434 for_each_comp_order(order) {
1435 for_each_card_auxs(card, component) {
1436 if (component->driver->probe_order != order)
1439 ret = soc_probe_component(card, component);
1448 static void soc_remove_aux_devices(struct snd_soc_card *card)
1450 struct snd_soc_component *comp, *_comp;
1453 for_each_comp_order(order) {
1454 for_each_card_auxs_safe(card, comp, _comp) {
1455 if (comp->driver->remove_order == order)
1456 soc_remove_component(comp, 1);
1462 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1463 * @rtd: The runtime for which the DAI link format should be changed
1464 * @dai_fmt: The new DAI link format
1466 * This function updates the DAI link format for all DAIs connected to the DAI
1467 * link for the specified runtime.
1469 * Note: For setups with a static format set the dai_fmt field in the
1470 * corresponding snd_dai_link struct instead of using this function.
1472 * Returns 0 on success, otherwise a negative error code.
1474 int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1475 unsigned int dai_fmt)
1477 struct snd_soc_dai *cpu_dai;
1478 struct snd_soc_dai *codec_dai;
1479 unsigned int inv_dai_fmt;
1483 for_each_rtd_codec_dais(rtd, i, codec_dai) {
1484 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1485 if (ret != 0 && ret != -ENOTSUPP) {
1486 dev_warn(codec_dai->dev,
1487 "ASoC: Failed to set DAI format: %d\n", ret);
1493 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1494 * the component which has non_legacy_dai_naming is Codec
1496 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1497 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1498 case SND_SOC_DAIFMT_CBM_CFM:
1499 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1501 case SND_SOC_DAIFMT_CBM_CFS:
1502 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1504 case SND_SOC_DAIFMT_CBS_CFM:
1505 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1507 case SND_SOC_DAIFMT_CBS_CFS:
1508 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1511 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1512 unsigned int fmt = dai_fmt;
1514 if (cpu_dai->component->driver->non_legacy_dai_naming)
1517 ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
1518 if (ret != 0 && ret != -ENOTSUPP) {
1519 dev_warn(cpu_dai->dev,
1520 "ASoC: Failed to set DAI format: %d\n", ret);
1527 EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1531 * If a DMI filed contain strings in this blacklist (e.g.
1532 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
1533 * as invalid and dropped when setting the card long name from DMI info.
1535 static const char * const dmi_blacklist[] = {
1536 "To be filled by OEM",
1539 "Board Manufacturer",
1540 "Board Vendor Name",
1541 "Board Product Name",
1542 NULL, /* terminator */
1546 * Trim special characters, and replace '-' with '_' since '-' is used to
1547 * separate different DMI fields in the card long name. Only number and
1548 * alphabet characters and a few separator characters are kept.
1550 static void cleanup_dmi_name(char *name)
1554 for (i = 0; name[i]; i++) {
1555 if (isalnum(name[i]) || (name[i] == '.')
1556 || (name[i] == '_'))
1557 name[j++] = name[i];
1558 else if (name[i] == '-')
1566 * Check if a DMI field is valid, i.e. not containing any string
1567 * in the black list.
1569 static int is_dmi_valid(const char *field)
1573 while (dmi_blacklist[i]) {
1574 if (strstr(field, dmi_blacklist[i]))
1583 * Append a string to card->dmi_longname with character cleanups.
1585 static void append_dmi_string(struct snd_soc_card *card, const char *str)
1587 char *dst = card->dmi_longname;
1588 size_t dst_len = sizeof(card->dmi_longname);
1592 snprintf(dst + len, dst_len - len, "-%s", str);
1594 len++; /* skip the separator "-" */
1596 cleanup_dmi_name(dst + len);
1600 * snd_soc_set_dmi_name() - Register DMI names to card
1601 * @card: The card to register DMI names
1602 * @flavour: The flavour "differentiator" for the card amongst its peers.
1604 * An Intel machine driver may be used by many different devices but are
1605 * difficult for userspace to differentiate, since machine drivers ususally
1606 * use their own name as the card short name and leave the card long name
1607 * blank. To differentiate such devices and fix bugs due to lack of
1608 * device-specific configurations, this function allows DMI info to be used
1609 * as the sound card long name, in the format of
1610 * "vendor-product-version-board"
1611 * (Character '-' is used to separate different DMI fields here).
1612 * This will help the user space to load the device-specific Use Case Manager
1613 * (UCM) configurations for the card.
1615 * Possible card long names may be:
1616 * DellInc.-XPS139343-01-0310JH
1617 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1618 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1620 * This function also supports flavoring the card longname to provide
1621 * the extra differentiation, like "vendor-product-version-board-flavor".
1623 * We only keep number and alphabet characters and a few separator characters
1624 * in the card long name since UCM in the user space uses the card long names
1625 * as card configuration directory names and AudoConf cannot support special
1626 * charactors like SPACE.
1628 * Returns 0 on success, otherwise a negative error code.
1630 int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1632 const char *vendor, *product, *product_version, *board;
1634 if (card->long_name)
1635 return 0; /* long name already set by driver or from DMI */
1637 /* make up dmi long name as: vendor-product-version-board */
1638 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1639 if (!vendor || !is_dmi_valid(vendor)) {
1640 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1644 snprintf(card->dmi_longname, sizeof(card->dmi_longname), "%s", vendor);
1645 cleanup_dmi_name(card->dmi_longname);
1647 product = dmi_get_system_info(DMI_PRODUCT_NAME);
1648 if (product && is_dmi_valid(product)) {
1649 append_dmi_string(card, product);
1652 * some vendors like Lenovo may only put a self-explanatory
1653 * name in the product version field
1655 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
1656 if (product_version && is_dmi_valid(product_version))
1657 append_dmi_string(card, product_version);
1660 board = dmi_get_system_info(DMI_BOARD_NAME);
1661 if (board && is_dmi_valid(board)) {
1662 if (!product || strcasecmp(board, product))
1663 append_dmi_string(card, board);
1664 } else if (!product) {
1665 /* fall back to using legacy name */
1666 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1670 /* Add flavour to dmi long name */
1672 append_dmi_string(card, flavour);
1674 /* set the card long name */
1675 card->long_name = card->dmi_longname;
1679 EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
1680 #endif /* CONFIG_DMI */
1682 static void soc_check_tplg_fes(struct snd_soc_card *card)
1684 struct snd_soc_component *component;
1685 const struct snd_soc_component_driver *comp_drv;
1686 struct snd_soc_dai_link *dai_link;
1689 for_each_component(component) {
1691 /* does this component override BEs ? */
1692 if (!component->driver->ignore_machine)
1695 /* for this machine ? */
1696 if (!strcmp(component->driver->ignore_machine,
1697 card->dev->driver->name))
1699 if (strcmp(component->driver->ignore_machine,
1700 dev_name(card->dev)))
1703 /* machine matches, so override the rtd data */
1704 for_each_card_prelinks(card, i, dai_link) {
1706 /* ignore this FE */
1707 if (dai_link->dynamic) {
1708 dai_link->ignore = true;
1712 dev_info(card->dev, "info: override BE DAI link %s\n",
1713 card->dai_link[i].name);
1715 /* override platform component */
1716 if (!dai_link->platforms) {
1717 dev_err(card->dev, "init platform error");
1720 dai_link->platforms->name = component->name;
1722 /* convert non BE into BE */
1723 dai_link->no_pcm = 1;
1724 dai_link->dpcm_playback = 1;
1725 dai_link->dpcm_capture = 1;
1727 /* override any BE fixups */
1728 dai_link->be_hw_params_fixup =
1729 component->driver->be_hw_params_fixup;
1732 * most BE links don't set stream name, so set it to
1733 * dai link name if it's NULL to help bind widgets.
1735 if (!dai_link->stream_name)
1736 dai_link->stream_name = dai_link->name;
1739 /* Inform userspace we are using alternate topology */
1740 if (component->driver->topology_name_prefix) {
1742 /* topology shortname created? */
1743 if (!card->topology_shortname_created) {
1744 comp_drv = component->driver;
1746 snprintf(card->topology_shortname, 32, "%s-%s",
1747 comp_drv->topology_name_prefix,
1749 card->topology_shortname_created = true;
1752 /* use topology shortname */
1753 card->name = card->topology_shortname;
1758 #define soc_setup_card_name(name, name1, name2, norm) \
1759 __soc_setup_card_name(name, sizeof(name), name1, name2, norm)
1760 static void __soc_setup_card_name(char *name, int len,
1761 const char *name1, const char *name2,
1766 snprintf(name, len, "%s", name1 ? name1 : name2);
1772 * Name normalization
1774 * The driver name is somewhat special, as it's used as a key for
1775 * searches in the user-space.
1778 * "abcd??efg" -> "abcd__efg"
1780 for (i = 0; i < len; i++) {
1787 if (!isalnum(name[i]))
1794 static void soc_cleanup_card_resources(struct snd_soc_card *card,
1797 struct snd_soc_pcm_runtime *rtd, *n;
1800 snd_card_disconnect_sync(card->snd_card);
1802 snd_soc_dapm_shutdown(card);
1804 /* remove and free each DAI */
1805 soc_remove_link_dais(card);
1806 soc_remove_link_components(card);
1808 for_each_card_rtds_safe(card, rtd, n)
1809 snd_soc_remove_pcm_runtime(card, rtd);
1811 /* remove auxiliary devices */
1812 soc_remove_aux_devices(card);
1813 soc_unbind_aux_dev(card);
1815 snd_soc_dapm_free(&card->dapm);
1816 soc_cleanup_card_debugfs(card);
1818 /* remove the card */
1819 if (card_probed && card->remove)
1822 if (card->snd_card) {
1823 snd_card_free(card->snd_card);
1824 card->snd_card = NULL;
1828 static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
1830 if (card->instantiated) {
1831 int card_probed = 1;
1833 card->instantiated = false;
1834 snd_soc_flush_all_delayed_work(card);
1836 soc_cleanup_card_resources(card, card_probed);
1838 list_add(&card->list, &unbind_card_list);
1841 list_del(&card->list);
1845 static int snd_soc_bind_card(struct snd_soc_card *card)
1847 struct snd_soc_pcm_runtime *rtd;
1848 struct snd_soc_component *component;
1849 struct snd_soc_dai_link *dai_link;
1850 int ret, i, card_probed = 0;
1852 mutex_lock(&client_mutex);
1853 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1855 snd_soc_dapm_init(&card->dapm, card, NULL);
1857 /* check whether any platform is ignore machine FE and using topology */
1858 soc_check_tplg_fes(card);
1860 /* bind aux_devs too */
1861 ret = soc_bind_aux_dev(card);
1865 /* add predefined DAI links to the list */
1867 for_each_card_prelinks(card, i, dai_link) {
1868 ret = snd_soc_add_pcm_runtime(card, dai_link);
1873 /* card bind complete so register a sound card */
1874 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1875 card->owner, 0, &card->snd_card);
1878 "ASoC: can't create sound card for card %s: %d\n",
1883 soc_init_card_debugfs(card);
1885 soc_resume_init(card);
1887 ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1888 card->num_dapm_widgets);
1892 ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1893 card->num_of_dapm_widgets);
1897 /* initialise the sound card only once */
1899 ret = card->probe(card);
1905 /* probe all components used by DAI links on this card */
1906 ret = soc_probe_link_components(card);
1909 "ASoC: failed to instantiate card %d\n", ret);
1913 /* probe auxiliary components */
1914 ret = soc_probe_aux_devices(card);
1917 "ASoC: failed to probe aux component %d\n", ret);
1921 /* probe all DAI links on this card */
1922 ret = soc_probe_link_dais(card);
1925 "ASoC: failed to instantiate card %d\n", ret);
1929 for_each_card_rtds(card, rtd) {
1930 ret = soc_init_pcm_runtime(card, rtd);
1935 snd_soc_dapm_link_dai_widgets(card);
1936 snd_soc_dapm_connect_dai_link_widgets(card);
1938 ret = snd_soc_add_card_controls(card, card->controls,
1939 card->num_controls);
1943 ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1944 card->num_dapm_routes);
1946 if (card->disable_route_checks) {
1948 "%s: disable_route_checks set, ignoring errors on add_routes\n",
1952 "%s: snd_soc_dapm_add_routes failed: %d\n",
1958 ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
1959 card->num_of_dapm_routes);
1963 /* try to set some sane longname if DMI is available */
1964 snd_soc_set_dmi_name(card, NULL);
1966 soc_setup_card_name(card->snd_card->shortname,
1967 card->name, NULL, 0);
1968 soc_setup_card_name(card->snd_card->longname,
1969 card->long_name, card->name, 0);
1970 soc_setup_card_name(card->snd_card->driver,
1971 card->driver_name, card->name, 1);
1973 if (card->components) {
1974 /* the current implementation of snd_component_add() accepts */
1975 /* multiple components in the string separated by space, */
1976 /* but the string collision (identical string) check might */
1977 /* not work correctly */
1978 ret = snd_component_add(card->snd_card, card->components);
1980 dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n",
1986 if (card->late_probe) {
1987 ret = card->late_probe(card);
1989 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
1996 snd_soc_dapm_new_widgets(card);
1998 ret = snd_card_register(card->snd_card);
2000 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2005 card->instantiated = 1;
2006 dapm_mark_endpoints_dirty(card);
2007 snd_soc_dapm_sync(&card->dapm);
2009 /* deactivate pins to sleep state */
2010 for_each_card_components(card, component)
2011 if (!component->active)
2012 pinctrl_pm_select_sleep_state(component->dev);
2016 soc_cleanup_card_resources(card, card_probed);
2018 mutex_unlock(&card->mutex);
2019 mutex_unlock(&client_mutex);
2024 /* probes a new socdev */
2025 static int soc_probe(struct platform_device *pdev)
2027 struct snd_soc_card *card = platform_get_drvdata(pdev);
2030 * no card, so machine driver should be registering card
2031 * we should not be here in that case so ret error
2036 dev_warn(&pdev->dev,
2037 "ASoC: machine %s should use snd_soc_register_card()\n",
2040 /* Bodge while we unpick instantiation */
2041 card->dev = &pdev->dev;
2043 return snd_soc_register_card(card);
2046 /* removes a socdev */
2047 static int soc_remove(struct platform_device *pdev)
2049 struct snd_soc_card *card = platform_get_drvdata(pdev);
2051 snd_soc_unregister_card(card);
2055 int snd_soc_poweroff(struct device *dev)
2057 struct snd_soc_card *card = dev_get_drvdata(dev);
2058 struct snd_soc_component *component;
2060 if (!card->instantiated)
2064 * Flush out pmdown_time work - we actually do want to run it
2065 * now, we're shutting down so no imminent restart.
2067 snd_soc_flush_all_delayed_work(card);
2069 snd_soc_dapm_shutdown(card);
2071 /* deactivate pins to sleep state */
2072 for_each_card_components(card, component)
2073 pinctrl_pm_select_sleep_state(component->dev);
2077 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2079 const struct dev_pm_ops snd_soc_pm_ops = {
2080 .suspend = snd_soc_suspend,
2081 .resume = snd_soc_resume,
2082 .freeze = snd_soc_suspend,
2083 .thaw = snd_soc_resume,
2084 .poweroff = snd_soc_poweroff,
2085 .restore = snd_soc_resume,
2087 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2089 /* ASoC platform driver */
2090 static struct platform_driver soc_driver = {
2092 .name = "soc-audio",
2093 .pm = &snd_soc_pm_ops,
2096 .remove = soc_remove,
2100 * snd_soc_cnew - create new control
2101 * @_template: control template
2102 * @data: control private data
2103 * @long_name: control long name
2104 * @prefix: control name prefix
2106 * Create a new mixer control from a template control.
2108 * Returns 0 for success, else error.
2110 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2111 void *data, const char *long_name,
2114 struct snd_kcontrol_new template;
2115 struct snd_kcontrol *kcontrol;
2118 memcpy(&template, _template, sizeof(template));
2122 long_name = template.name;
2125 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2129 template.name = name;
2131 template.name = long_name;
2134 kcontrol = snd_ctl_new1(&template, data);
2140 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2142 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2143 const struct snd_kcontrol_new *controls, int num_controls,
2144 const char *prefix, void *data)
2148 for (i = 0; i < num_controls; i++) {
2149 const struct snd_kcontrol_new *control = &controls[i];
2151 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2152 control->name, prefix));
2154 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2155 control->name, err);
2163 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2166 struct snd_card *card = soc_card->snd_card;
2167 struct snd_kcontrol *kctl;
2169 if (unlikely(!name))
2172 list_for_each_entry(kctl, &card->controls, list)
2173 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2177 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2180 * snd_soc_add_component_controls - Add an array of controls to a component.
2182 * @component: Component to add controls to
2183 * @controls: Array of controls to add
2184 * @num_controls: Number of elements in the array
2186 * Return: 0 for success, else error.
2188 int snd_soc_add_component_controls(struct snd_soc_component *component,
2189 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2191 struct snd_card *card = component->card->snd_card;
2193 return snd_soc_add_controls(card, component->dev, controls,
2194 num_controls, component->name_prefix, component);
2196 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2199 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2200 * Convenience function to add a list of controls.
2202 * @soc_card: SoC card to add controls to
2203 * @controls: array of controls to add
2204 * @num_controls: number of elements in the array
2206 * Return 0 for success, else error.
2208 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2209 const struct snd_kcontrol_new *controls, int num_controls)
2211 struct snd_card *card = soc_card->snd_card;
2213 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2216 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2219 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2220 * Convienience function to add a list of controls.
2222 * @dai: DAI to add controls to
2223 * @controls: array of controls to add
2224 * @num_controls: number of elements in the array
2226 * Return 0 for success, else error.
2228 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2229 const struct snd_kcontrol_new *controls, int num_controls)
2231 struct snd_card *card = dai->component->card->snd_card;
2233 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2236 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2239 * snd_soc_register_card - Register a card with the ASoC core
2241 * @card: Card to register
2244 int snd_soc_register_card(struct snd_soc_card *card)
2246 if (!card->name || !card->dev)
2249 dev_set_drvdata(card->dev, card);
2251 INIT_LIST_HEAD(&card->widgets);
2252 INIT_LIST_HEAD(&card->paths);
2253 INIT_LIST_HEAD(&card->dapm_list);
2254 INIT_LIST_HEAD(&card->aux_comp_list);
2255 INIT_LIST_HEAD(&card->component_dev_list);
2256 INIT_LIST_HEAD(&card->list);
2257 INIT_LIST_HEAD(&card->rtd_list);
2258 INIT_LIST_HEAD(&card->dapm_dirty);
2259 INIT_LIST_HEAD(&card->dobj_list);
2261 card->instantiated = 0;
2262 mutex_init(&card->mutex);
2263 mutex_init(&card->dapm_mutex);
2264 mutex_init(&card->pcm_mutex);
2265 spin_lock_init(&card->dpcm_lock);
2267 return snd_soc_bind_card(card);
2269 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2272 * snd_soc_unregister_card - Unregister a card with the ASoC core
2274 * @card: Card to unregister
2277 int snd_soc_unregister_card(struct snd_soc_card *card)
2279 mutex_lock(&client_mutex);
2280 snd_soc_unbind_card(card, true);
2281 mutex_unlock(&client_mutex);
2282 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2286 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2289 * Simplify DAI link configuration by removing ".-1" from device names
2290 * and sanitizing names.
2292 static char *fmt_single_name(struct device *dev, int *id)
2294 char *found, name[NAME_SIZE];
2297 if (dev_name(dev) == NULL)
2300 strlcpy(name, dev_name(dev), NAME_SIZE);
2302 /* are we a "%s.%d" name (platform and SPI components) */
2303 found = strstr(name, dev->driver->name);
2306 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2308 /* discard ID from name if ID == -1 */
2310 found[strlen(dev->driver->name)] = '\0';
2314 /* I2C component devices are named "bus-addr" */
2315 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2316 char tmp[NAME_SIZE];
2318 /* create unique ID number from I2C addr and bus */
2319 *id = ((id1 & 0xffff) << 16) + id2;
2321 /* sanitize component name for DAI link creation */
2322 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2324 strlcpy(name, tmp, NAME_SIZE);
2329 return devm_kstrdup(dev, name, GFP_KERNEL);
2333 * Simplify DAI link naming for single devices with multiple DAIs by removing
2334 * any ".-1" and using the DAI name (instead of device name).
2336 static inline char *fmt_multiple_name(struct device *dev,
2337 struct snd_soc_dai_driver *dai_drv)
2339 if (dai_drv->name == NULL) {
2341 "ASoC: error - multiple DAI %s registered with no name\n",
2346 return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2349 void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2351 dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2352 list_del(&dai->list);
2354 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2357 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2359 * @component: The component the DAIs are registered for
2360 * @dai_drv: DAI driver to use for the DAI
2361 * @legacy_dai_naming: if %true, use legacy single-name format;
2362 * if %false, use multiple-name format;
2364 * Topology can use this API to register DAIs when probing a component.
2365 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2366 * will be freed in the component cleanup.
2368 struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
2369 struct snd_soc_dai_driver *dai_drv,
2370 bool legacy_dai_naming)
2372 struct device *dev = component->dev;
2373 struct snd_soc_dai *dai;
2375 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2377 lockdep_assert_held(&client_mutex);
2379 dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
2384 * Back in the old days when we still had component-less DAIs,
2385 * instead of having a static name, component-less DAIs would
2386 * inherit the name of the parent device so it is possible to
2387 * register multiple instances of the DAI. We still need to keep
2388 * the same naming style even though those DAIs are not
2389 * component-less anymore.
2391 if (legacy_dai_naming &&
2392 (dai_drv->id == 0 || dai_drv->name == NULL)) {
2393 dai->name = fmt_single_name(dev, &dai->id);
2395 dai->name = fmt_multiple_name(dev, dai_drv);
2397 dai->id = dai_drv->id;
2399 dai->id = component->num_dai;
2404 dai->component = component;
2406 dai->driver = dai_drv;
2407 if (!dai->driver->ops)
2408 dai->driver->ops = &null_dai_ops;
2410 /* see for_each_component_dais */
2411 list_add_tail(&dai->list, &component->dai_list);
2412 component->num_dai++;
2414 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2419 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2421 * @component: The component for which the DAIs should be unregistered
2423 static void snd_soc_unregister_dais(struct snd_soc_component *component)
2425 struct snd_soc_dai *dai, *_dai;
2427 for_each_component_dais_safe(component, dai, _dai)
2428 snd_soc_unregister_dai(dai);
2432 * snd_soc_register_dais - Register a DAI with the ASoC core
2434 * @component: The component the DAIs are registered for
2435 * @dai_drv: DAI driver to use for the DAIs
2436 * @count: Number of DAIs
2438 static int snd_soc_register_dais(struct snd_soc_component *component,
2439 struct snd_soc_dai_driver *dai_drv,
2442 struct snd_soc_dai *dai;
2446 for (i = 0; i < count; i++) {
2447 dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
2448 !component->driver->non_legacy_dai_naming);
2458 snd_soc_unregister_dais(component);
2463 static int snd_soc_component_initialize(struct snd_soc_component *component,
2464 const struct snd_soc_component_driver *driver, struct device *dev)
2466 INIT_LIST_HEAD(&component->dai_list);
2467 INIT_LIST_HEAD(&component->dobj_list);
2468 INIT_LIST_HEAD(&component->card_list);
2469 mutex_init(&component->io_mutex);
2471 component->name = fmt_single_name(dev, &component->id);
2472 if (!component->name) {
2473 dev_err(dev, "ASoC: Failed to allocate name\n");
2477 component->dev = dev;
2478 component->driver = driver;
2483 static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2485 int val_bytes = regmap_get_val_bytes(component->regmap);
2487 /* Errors are legitimate for non-integer byte multiples */
2489 component->val_bytes = val_bytes;
2492 #ifdef CONFIG_REGMAP
2495 * snd_soc_component_init_regmap() - Initialize regmap instance for the
2497 * @component: The component for which to initialize the regmap instance
2498 * @regmap: The regmap instance that should be used by the component
2500 * This function allows deferred assignment of the regmap instance that is
2501 * associated with the component. Only use this if the regmap instance is not
2502 * yet ready when the component is registered. The function must also be called
2503 * before the first IO attempt of the component.
2505 void snd_soc_component_init_regmap(struct snd_soc_component *component,
2506 struct regmap *regmap)
2508 component->regmap = regmap;
2509 snd_soc_component_setup_regmap(component);
2511 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
2514 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
2516 * @component: The component for which to de-initialize the regmap instance
2518 * Calls regmap_exit() on the regmap instance associated to the component and
2519 * removes the regmap instance from the component.
2521 * This function should only be used if snd_soc_component_init_regmap() was used
2522 * to initialize the regmap instance.
2524 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
2526 regmap_exit(component->regmap);
2527 component->regmap = NULL;
2529 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2533 #define ENDIANNESS_MAP(name) \
2534 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2535 static u64 endianness_format_map[] = {
2536 ENDIANNESS_MAP(S16_),
2537 ENDIANNESS_MAP(U16_),
2538 ENDIANNESS_MAP(S24_),
2539 ENDIANNESS_MAP(U24_),
2540 ENDIANNESS_MAP(S32_),
2541 ENDIANNESS_MAP(U32_),
2542 ENDIANNESS_MAP(S24_3),
2543 ENDIANNESS_MAP(U24_3),
2544 ENDIANNESS_MAP(S20_3),
2545 ENDIANNESS_MAP(U20_3),
2546 ENDIANNESS_MAP(S18_3),
2547 ENDIANNESS_MAP(U18_3),
2548 ENDIANNESS_MAP(FLOAT_),
2549 ENDIANNESS_MAP(FLOAT64_),
2550 ENDIANNESS_MAP(IEC958_SUBFRAME_),
2554 * Fix up the DAI formats for endianness: codecs don't actually see
2555 * the endianness of the data but we're using the CPU format
2556 * definitions which do need to include endianness so we ensure that
2557 * codec DAIs always have both big and little endian variants set.
2559 static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2563 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2564 if (stream->formats & endianness_format_map[i])
2565 stream->formats |= endianness_format_map[i];
2568 static void snd_soc_try_rebind_card(void)
2570 struct snd_soc_card *card, *c;
2572 list_for_each_entry_safe(card, c, &unbind_card_list, list)
2573 if (!snd_soc_bind_card(card))
2574 list_del(&card->list);
2577 static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2579 struct snd_soc_card *card = component->card;
2581 snd_soc_unregister_dais(component);
2584 snd_soc_unbind_card(card, false);
2586 list_del(&component->list);
2589 int snd_soc_add_component(struct device *dev,
2590 struct snd_soc_component *component,
2591 const struct snd_soc_component_driver *component_driver,
2592 struct snd_soc_dai_driver *dai_drv,
2598 mutex_lock(&client_mutex);
2600 ret = snd_soc_component_initialize(component, component_driver, dev);
2604 if (component_driver->endianness) {
2605 for (i = 0; i < num_dai; i++) {
2606 convert_endianness_formats(&dai_drv[i].playback);
2607 convert_endianness_formats(&dai_drv[i].capture);
2611 ret = snd_soc_register_dais(component, dai_drv, num_dai);
2613 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
2617 if (!component->driver->write && !component->driver->read) {
2618 if (!component->regmap)
2619 component->regmap = dev_get_regmap(component->dev,
2621 if (component->regmap)
2622 snd_soc_component_setup_regmap(component);
2625 /* see for_each_component */
2626 list_add(&component->list, &component_list);
2630 snd_soc_del_component_unlocked(component);
2632 mutex_unlock(&client_mutex);
2635 snd_soc_try_rebind_card();
2639 EXPORT_SYMBOL_GPL(snd_soc_add_component);
2641 int snd_soc_register_component(struct device *dev,
2642 const struct snd_soc_component_driver *component_driver,
2643 struct snd_soc_dai_driver *dai_drv,
2646 struct snd_soc_component *component;
2648 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
2652 return snd_soc_add_component(dev, component, component_driver,
2655 EXPORT_SYMBOL_GPL(snd_soc_register_component);
2658 * snd_soc_unregister_component - Unregister all related component
2659 * from the ASoC core
2661 * @dev: The device to unregister
2663 void snd_soc_unregister_component(struct device *dev)
2665 struct snd_soc_component *component;
2667 mutex_lock(&client_mutex);
2669 component = snd_soc_lookup_component_nolocked(dev, NULL);
2673 snd_soc_del_component_unlocked(component);
2675 mutex_unlock(&client_mutex);
2677 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2679 /* Retrieve a card's name from device tree */
2680 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2681 const char *propname)
2683 struct device_node *np;
2687 pr_err("card->dev is not set before calling %s\n", __func__);
2691 np = card->dev->of_node;
2693 ret = of_property_read_string_index(np, propname, 0, &card->name);
2695 * EINVAL means the property does not exist. This is fine providing
2696 * card->name was previously set, which is checked later in
2697 * snd_soc_register_card.
2699 if (ret < 0 && ret != -EINVAL) {
2701 "ASoC: Property '%s' could not be read: %d\n",
2708 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2710 static const struct snd_soc_dapm_widget simple_widgets[] = {
2711 SND_SOC_DAPM_MIC("Microphone", NULL),
2712 SND_SOC_DAPM_LINE("Line", NULL),
2713 SND_SOC_DAPM_HP("Headphone", NULL),
2714 SND_SOC_DAPM_SPK("Speaker", NULL),
2717 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
2718 const char *propname)
2720 struct device_node *np = card->dev->of_node;
2721 struct snd_soc_dapm_widget *widgets;
2722 const char *template, *wname;
2723 int i, j, num_widgets, ret;
2725 num_widgets = of_property_count_strings(np, propname);
2726 if (num_widgets < 0) {
2728 "ASoC: Property '%s' does not exist\n", propname);
2731 if (num_widgets & 1) {
2733 "ASoC: Property '%s' length is not even\n", propname);
2739 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
2744 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
2748 "ASoC: Could not allocate memory for widgets\n");
2752 for (i = 0; i < num_widgets; i++) {
2753 ret = of_property_read_string_index(np, propname,
2757 "ASoC: Property '%s' index %d read error:%d\n",
2758 propname, 2 * i, ret);
2762 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
2763 if (!strncmp(template, simple_widgets[j].name,
2764 strlen(simple_widgets[j].name))) {
2765 widgets[i] = simple_widgets[j];
2770 if (j >= ARRAY_SIZE(simple_widgets)) {
2772 "ASoC: DAPM widget '%s' is not supported\n",
2777 ret = of_property_read_string_index(np, propname,
2782 "ASoC: Property '%s' index %d read error:%d\n",
2783 propname, (2 * i) + 1, ret);
2787 widgets[i].name = wname;
2790 card->of_dapm_widgets = widgets;
2791 card->num_of_dapm_widgets = num_widgets;
2795 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
2797 int snd_soc_of_get_slot_mask(struct device_node *np,
2798 const char *prop_name,
2802 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
2808 for (i = 0; i < val; i++)
2809 if (be32_to_cpup(&of_slot_mask[i]))
2814 EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
2816 int snd_soc_of_parse_tdm_slot(struct device_node *np,
2817 unsigned int *tx_mask,
2818 unsigned int *rx_mask,
2819 unsigned int *slots,
2820 unsigned int *slot_width)
2826 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
2828 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
2830 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
2831 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
2839 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
2840 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
2850 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
2852 void snd_soc_of_parse_node_prefix(struct device_node *np,
2853 struct snd_soc_codec_conf *codec_conf,
2854 struct device_node *of_node,
2855 const char *propname)
2860 ret = of_property_read_string(np, propname, &str);
2862 /* no prefix is not error */
2866 codec_conf->dlc.of_node = of_node;
2867 codec_conf->name_prefix = str;
2869 EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
2871 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
2872 const char *propname)
2874 struct device_node *np = card->dev->of_node;
2876 struct snd_soc_dapm_route *routes;
2879 num_routes = of_property_count_strings(np, propname);
2880 if (num_routes < 0 || num_routes & 1) {
2882 "ASoC: Property '%s' does not exist or its length is not even\n",
2888 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
2893 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
2897 "ASoC: Could not allocate DAPM route table\n");
2901 for (i = 0; i < num_routes; i++) {
2902 ret = of_property_read_string_index(np, propname,
2903 2 * i, &routes[i].sink);
2906 "ASoC: Property '%s' index %d could not be read: %d\n",
2907 propname, 2 * i, ret);
2910 ret = of_property_read_string_index(np, propname,
2911 (2 * i) + 1, &routes[i].source);
2914 "ASoC: Property '%s' index %d could not be read: %d\n",
2915 propname, (2 * i) + 1, ret);
2920 card->num_of_dapm_routes = num_routes;
2921 card->of_dapm_routes = routes;
2925 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
2927 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
2929 struct device_node **bitclkmaster,
2930 struct device_node **framemaster)
2934 unsigned int format = 0;
2940 } of_fmt_table[] = {
2941 { "i2s", SND_SOC_DAIFMT_I2S },
2942 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
2943 { "left_j", SND_SOC_DAIFMT_LEFT_J },
2944 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
2945 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
2946 { "ac97", SND_SOC_DAIFMT_AC97 },
2947 { "pdm", SND_SOC_DAIFMT_PDM},
2948 { "msb", SND_SOC_DAIFMT_MSB },
2949 { "lsb", SND_SOC_DAIFMT_LSB },
2956 * check "dai-format = xxx"
2957 * or "[prefix]format = xxx"
2958 * SND_SOC_DAIFMT_FORMAT_MASK area
2960 ret = of_property_read_string(np, "dai-format", &str);
2962 snprintf(prop, sizeof(prop), "%sformat", prefix);
2963 ret = of_property_read_string(np, prop, &str);
2966 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
2967 if (strcmp(str, of_fmt_table[i].name) == 0) {
2968 format |= of_fmt_table[i].val;
2975 * check "[prefix]continuous-clock"
2976 * SND_SOC_DAIFMT_CLOCK_MASK area
2978 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
2979 if (of_property_read_bool(np, prop))
2980 format |= SND_SOC_DAIFMT_CONT;
2982 format |= SND_SOC_DAIFMT_GATED;
2985 * check "[prefix]bitclock-inversion"
2986 * check "[prefix]frame-inversion"
2987 * SND_SOC_DAIFMT_INV_MASK area
2989 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
2990 bit = !!of_get_property(np, prop, NULL);
2992 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
2993 frame = !!of_get_property(np, prop, NULL);
2995 switch ((bit << 4) + frame) {
2997 format |= SND_SOC_DAIFMT_IB_IF;
3000 format |= SND_SOC_DAIFMT_IB_NF;
3003 format |= SND_SOC_DAIFMT_NB_IF;
3006 /* SND_SOC_DAIFMT_NB_NF is default */
3011 * check "[prefix]bitclock-master"
3012 * check "[prefix]frame-master"
3013 * SND_SOC_DAIFMT_MASTER_MASK area
3015 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3016 bit = !!of_get_property(np, prop, NULL);
3017 if (bit && bitclkmaster)
3018 *bitclkmaster = of_parse_phandle(np, prop, 0);
3020 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3021 frame = !!of_get_property(np, prop, NULL);
3022 if (frame && framemaster)
3023 *framemaster = of_parse_phandle(np, prop, 0);
3025 switch ((bit << 4) + frame) {
3027 format |= SND_SOC_DAIFMT_CBM_CFM;
3030 format |= SND_SOC_DAIFMT_CBM_CFS;
3033 format |= SND_SOC_DAIFMT_CBS_CFM;
3036 format |= SND_SOC_DAIFMT_CBS_CFS;
3042 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3044 int snd_soc_get_dai_id(struct device_node *ep)
3046 struct snd_soc_component *component;
3047 struct snd_soc_dai_link_component dlc;
3050 dlc.of_node = of_graph_get_port_parent(ep);
3053 * For example HDMI case, HDMI has video/sound port,
3054 * but ALSA SoC needs sound port number only.
3055 * Thus counting HDMI DT port/endpoint doesn't work.
3056 * Then, it should have .of_xlate_dai_id
3059 mutex_lock(&client_mutex);
3060 component = soc_find_component(&dlc);
3062 ret = snd_soc_component_of_xlate_dai_id(component, ep);
3063 mutex_unlock(&client_mutex);
3065 of_node_put(dlc.of_node);
3069 EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3071 int snd_soc_get_dai_name(struct of_phandle_args *args,
3072 const char **dai_name)
3074 struct snd_soc_component *pos;
3075 struct device_node *component_of_node;
3076 int ret = -EPROBE_DEFER;
3078 mutex_lock(&client_mutex);
3079 for_each_component(pos) {
3080 component_of_node = soc_component_to_node(pos);
3082 if (component_of_node != args->np)
3085 ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3086 if (ret == -ENOTSUPP) {
3087 struct snd_soc_dai *dai;
3090 switch (args->args_count) {
3092 id = 0; /* same as dai_drv[0] */
3102 if (id < 0 || id >= pos->num_dai) {
3109 /* find target DAI */
3110 for_each_component_dais(pos, dai) {
3116 *dai_name = dai->driver->name;
3118 *dai_name = pos->name;
3121 * if another error than ENOTSUPP is returned go on and
3122 * check if another component is provided with the same
3123 * node. This may happen if a device provides several
3131 mutex_unlock(&client_mutex);
3134 EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
3136 int snd_soc_of_get_dai_name(struct device_node *of_node,
3137 const char **dai_name)
3139 struct of_phandle_args args;
3142 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3143 "#sound-dai-cells", 0, &args);
3147 ret = snd_soc_get_dai_name(&args, dai_name);
3149 of_node_put(args.np);
3153 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3156 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3157 * @dai_link: DAI link
3159 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3161 void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3163 struct snd_soc_dai_link_component *component;
3166 for_each_link_codecs(dai_link, index, component) {
3167 if (!component->of_node)
3169 of_node_put(component->of_node);
3170 component->of_node = NULL;
3173 EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3176 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3178 * @of_node: Device node
3179 * @dai_link: DAI link
3181 * Builds an array of CODEC DAI components from the DAI link property
3183 * The array is set in the DAI link and the number of DAIs is set accordingly.
3184 * The device nodes in the array (of_node) must be dereferenced by calling
3185 * snd_soc_of_put_dai_link_codecs() on @dai_link.
3187 * Returns 0 for success
3189 int snd_soc_of_get_dai_link_codecs(struct device *dev,
3190 struct device_node *of_node,
3191 struct snd_soc_dai_link *dai_link)
3193 struct of_phandle_args args;
3194 struct snd_soc_dai_link_component *component;
3196 int index, num_codecs, ret;
3198 /* Count the number of CODECs */
3200 num_codecs = of_count_phandle_with_args(of_node, name,
3201 "#sound-dai-cells");
3202 if (num_codecs <= 0) {
3203 if (num_codecs == -ENOENT)
3204 dev_err(dev, "No 'sound-dai' property\n");
3206 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3209 component = devm_kcalloc(dev,
3210 num_codecs, sizeof(*component),
3214 dai_link->codecs = component;
3215 dai_link->num_codecs = num_codecs;
3217 /* Parse the list */
3218 for_each_link_codecs(dai_link, index, component) {
3219 ret = of_parse_phandle_with_args(of_node, name,
3224 component->of_node = args.np;
3225 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3231 snd_soc_of_put_dai_link_codecs(dai_link);
3232 dai_link->codecs = NULL;
3233 dai_link->num_codecs = 0;
3236 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3238 static int __init snd_soc_init(void)
3240 snd_soc_debugfs_init();
3241 snd_soc_util_init();
3243 return platform_driver_register(&soc_driver);
3245 module_init(snd_soc_init);
3247 static void __exit snd_soc_exit(void)
3249 snd_soc_util_exit();
3250 snd_soc_debugfs_exit();
3252 platform_driver_unregister(&soc_driver);
3254 module_exit(snd_soc_exit);
3256 /* Module information */
3257 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3258 MODULE_DESCRIPTION("ALSA SoC Core");
3259 MODULE_LICENSE("GPL");
3260 MODULE_ALIAS("platform:soc-audio");