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.
11 // with code, comments and ideas from :-
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/soc-link.h>
42 #include <sound/initval.h>
44 #define CREATE_TRACE_POINTS
45 #include <trace/events/asoc.h>
49 static DEFINE_MUTEX(client_mutex);
50 static LIST_HEAD(component_list);
51 static LIST_HEAD(unbind_card_list);
53 #define for_each_component(component) \
54 list_for_each_entry(component, &component_list, list)
57 * This is used if driver don't need to have CPU/Codec/Platform
60 struct snd_soc_dai_link_component null_dailink_component[0];
61 EXPORT_SYMBOL_GPL(null_dailink_component);
64 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
68 static int pmdown_time = 5000;
69 module_param(pmdown_time, int, 0);
70 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
72 static ssize_t pmdown_time_show(struct device *dev,
73 struct device_attribute *attr, char *buf)
75 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
77 return sprintf(buf, "%ld\n", rtd->pmdown_time);
80 static ssize_t pmdown_time_set(struct device *dev,
81 struct device_attribute *attr,
82 const char *buf, size_t count)
84 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
87 ret = kstrtol(buf, 10, &rtd->pmdown_time);
94 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
96 static struct attribute *soc_dev_attrs[] = {
97 &dev_attr_pmdown_time.attr,
101 static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
102 struct attribute *attr, int idx)
104 struct device *dev = kobj_to_dev(kobj);
105 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
110 if (attr == &dev_attr_pmdown_time.attr)
111 return attr->mode; /* always visible */
112 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
115 static const struct attribute_group soc_dapm_dev_group = {
116 .attrs = soc_dapm_dev_attrs,
117 .is_visible = soc_dev_attr_is_visible,
120 static const struct attribute_group soc_dev_group = {
121 .attrs = soc_dev_attrs,
122 .is_visible = soc_dev_attr_is_visible,
125 static const struct attribute_group *soc_dev_attr_groups[] = {
131 #ifdef CONFIG_DEBUG_FS
132 struct dentry *snd_soc_debugfs_root;
133 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
135 static void soc_init_component_debugfs(struct snd_soc_component *component)
137 if (!component->card->debugfs_card_root)
140 if (component->debugfs_prefix) {
143 name = kasprintf(GFP_KERNEL, "%s:%s",
144 component->debugfs_prefix, component->name);
146 component->debugfs_root = debugfs_create_dir(name,
147 component->card->debugfs_card_root);
151 component->debugfs_root = debugfs_create_dir(component->name,
152 component->card->debugfs_card_root);
155 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
156 component->debugfs_root);
159 static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
161 if (!component->debugfs_root)
163 debugfs_remove_recursive(component->debugfs_root);
164 component->debugfs_root = NULL;
167 static int dai_list_show(struct seq_file *m, void *v)
169 struct snd_soc_component *component;
170 struct snd_soc_dai *dai;
172 mutex_lock(&client_mutex);
174 for_each_component(component)
175 for_each_component_dais(component, dai)
176 seq_printf(m, "%s\n", dai->name);
178 mutex_unlock(&client_mutex);
182 DEFINE_SHOW_ATTRIBUTE(dai_list);
184 static int component_list_show(struct seq_file *m, void *v)
186 struct snd_soc_component *component;
188 mutex_lock(&client_mutex);
190 for_each_component(component)
191 seq_printf(m, "%s\n", component->name);
193 mutex_unlock(&client_mutex);
197 DEFINE_SHOW_ATTRIBUTE(component_list);
199 static void soc_init_card_debugfs(struct snd_soc_card *card)
201 card->debugfs_card_root = debugfs_create_dir(card->name,
202 snd_soc_debugfs_root);
204 debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root,
207 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
210 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
212 debugfs_remove_recursive(card->debugfs_card_root);
213 card->debugfs_card_root = NULL;
216 static void snd_soc_debugfs_init(void)
218 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
220 debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
223 debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
224 &component_list_fops);
227 static void snd_soc_debugfs_exit(void)
229 debugfs_remove_recursive(snd_soc_debugfs_root);
234 static inline void soc_init_component_debugfs(
235 struct snd_soc_component *component)
239 static inline void soc_cleanup_component_debugfs(
240 struct snd_soc_component *component)
244 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
248 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
252 static inline void snd_soc_debugfs_init(void)
256 static inline void snd_soc_debugfs_exit(void)
262 static int snd_soc_rtd_add_component(struct snd_soc_pcm_runtime *rtd,
263 struct snd_soc_component *component)
265 struct snd_soc_component *comp;
268 for_each_rtd_components(rtd, i, comp) {
269 /* already connected */
270 if (comp == component)
274 /* see for_each_rtd_components */
275 rtd->components[rtd->num_components] = component;
276 rtd->num_components++;
281 struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
282 const char *driver_name)
284 struct snd_soc_component *component;
293 * snd_soc_rtdcom_lookup() will find component from rtd by using
294 * specified driver name.
295 * But, if many components which have same driver name are connected
296 * to 1 rtd, this function will return 1st found component.
298 for_each_rtd_components(rtd, i, component) {
299 const char *component_name = component->driver->name;
304 if ((component_name == driver_name) ||
305 strcmp(component_name, driver_name) == 0)
311 EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
313 static struct snd_soc_component
314 *snd_soc_lookup_component_nolocked(struct device *dev, const char *driver_name)
316 struct snd_soc_component *component;
317 struct snd_soc_component *found_component;
319 found_component = NULL;
320 for_each_component(component) {
321 if ((dev == component->dev) &&
323 (driver_name == component->driver->name) ||
324 (strcmp(component->driver->name, driver_name) == 0))) {
325 found_component = component;
330 return found_component;
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;
448 dev->groups = soc_dev_attr_groups;
450 dev_set_name(dev, "%s", dai_link->name);
452 ret = device_register(dev);
454 put_device(dev); /* soc_release_rtd_dev */
461 rtd = devm_kzalloc(dev,
463 sizeof(*component) * (dai_link->num_cpus +
464 dai_link->num_codecs +
465 dai_link->num_platforms),
471 INIT_LIST_HEAD(&rtd->list);
472 for_each_pcm_streams(stream) {
473 INIT_LIST_HEAD(&rtd->dpcm[stream].be_clients);
474 INIT_LIST_HEAD(&rtd->dpcm[stream].fe_clients);
476 dev_set_drvdata(dev, rtd);
477 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
482 rtd->dais = devm_kcalloc(dev, dai_link->num_cpus + dai_link->num_codecs,
483 sizeof(struct snd_soc_dai *),
489 * dais = [][][][][][][][][][][][][][][][][][]
490 * ^cpu_dais ^codec_dais
491 * |--- num_cpus ---|--- num_codecs --|
494 * asoc_rtd_to_codec()
496 rtd->num_cpus = dai_link->num_cpus;
497 rtd->num_codecs = dai_link->num_codecs;
499 rtd->dai_link = dai_link;
500 rtd->num = card->num_rtd++;
502 /* see for_each_card_rtds */
503 list_add_tail(&rtd->list, &card->rtd_list);
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 (snd_soc_dai_stream_active(dai, 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 snd_soc_card_suspend_pre(card);
566 /* close any waiting streams */
567 snd_soc_flush_all_delayed_work(card);
569 for_each_card_rtds(card, rtd) {
572 if (rtd->dai_link->ignore_suspend)
575 for_each_pcm_streams(stream)
576 snd_soc_dapm_stream_event(rtd, stream,
577 SND_SOC_DAPM_STREAM_SUSPEND);
580 /* Recheck all endpoints too, their state is affected by suspend */
581 dapm_mark_endpoints_dirty(card);
582 snd_soc_dapm_sync(&card->dapm);
584 /* suspend all COMPONENTs */
585 for_each_card_rtds(card, rtd) {
587 if (rtd->dai_link->ignore_suspend)
590 for_each_rtd_components(rtd, i, component) {
591 struct snd_soc_dapm_context *dapm =
592 snd_soc_component_get_dapm(component);
595 * ignore if component was already suspended
597 if (snd_soc_component_is_suspended(component))
601 * If there are paths active then the COMPONENT will be
602 * held with bias _ON and should not be suspended.
604 switch (snd_soc_dapm_get_bias_level(dapm)) {
605 case SND_SOC_BIAS_STANDBY:
607 * If the COMPONENT is capable of idle
608 * bias off then being in STANDBY
609 * means it's doing something,
610 * otherwise fall through.
612 if (dapm->idle_bias_off) {
613 dev_dbg(component->dev,
614 "ASoC: idle_bias_off CODEC on over suspend\n");
619 case SND_SOC_BIAS_OFF:
620 snd_soc_component_suspend(component);
621 if (component->regmap)
622 regcache_mark_dirty(component->regmap);
623 /* deactivate pins to sleep state */
624 pinctrl_pm_select_sleep_state(component->dev);
627 dev_dbg(component->dev,
628 "ASoC: COMPONENT is on over suspend\n");
634 snd_soc_card_suspend_post(card);
638 EXPORT_SYMBOL_GPL(snd_soc_suspend);
641 * deferred resume work, so resume can complete before we finished
642 * setting our codec back up, which can be very slow on I2C
644 static void soc_resume_deferred(struct work_struct *work)
646 struct snd_soc_card *card =
647 container_of(work, struct snd_soc_card,
648 deferred_resume_work);
649 struct snd_soc_pcm_runtime *rtd;
650 struct snd_soc_component *component;
654 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
655 * so userspace apps are blocked from touching us
658 dev_dbg(card->dev, "ASoC: starting resume work\n");
660 /* Bring us up into D2 so that DAPM starts enabling things */
661 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
663 snd_soc_card_resume_pre(card);
665 for_each_card_components(card, component) {
666 if (snd_soc_component_is_suspended(component))
667 snd_soc_component_resume(component);
670 for_each_card_rtds(card, rtd) {
673 if (rtd->dai_link->ignore_suspend)
676 for_each_pcm_streams(stream)
677 snd_soc_dapm_stream_event(rtd, stream,
678 SND_SOC_DAPM_STREAM_RESUME);
681 /* unmute any active DACs */
682 for_each_card_rtds(card, rtd) {
683 struct snd_soc_dai *dai;
684 int playback = SNDRV_PCM_STREAM_PLAYBACK;
686 if (rtd->dai_link->ignore_suspend)
689 for_each_rtd_codec_dais(rtd, i, dai) {
690 if (snd_soc_dai_stream_active(dai, playback))
691 snd_soc_dai_digital_mute(dai, 0, playback);
695 snd_soc_card_resume_post(card);
697 dev_dbg(card->dev, "ASoC: resume work completed\n");
699 /* Recheck all endpoints too, their state is affected by suspend */
700 dapm_mark_endpoints_dirty(card);
701 snd_soc_dapm_sync(&card->dapm);
703 /* userspace can access us now we are back as we were before */
704 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
707 /* powers up audio subsystem after a suspend */
708 int snd_soc_resume(struct device *dev)
710 struct snd_soc_card *card = dev_get_drvdata(dev);
711 struct snd_soc_component *component;
713 /* If the card is not initialized yet there is nothing to do */
714 if (!card->instantiated)
717 /* activate pins from sleep state */
718 for_each_card_components(card, component)
719 if (snd_soc_component_active(component))
720 pinctrl_pm_select_default_state(component->dev);
722 dev_dbg(dev, "ASoC: Scheduling resume work\n");
723 if (!schedule_work(&card->deferred_resume_work))
724 dev_err(dev, "ASoC: resume work item may be lost\n");
728 EXPORT_SYMBOL_GPL(snd_soc_resume);
730 static void soc_resume_init(struct snd_soc_card *card)
732 /* deferred resume work */
733 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
736 #define snd_soc_suspend NULL
737 #define snd_soc_resume NULL
738 static inline void soc_resume_init(struct snd_soc_card *card)
743 static struct device_node
744 *soc_component_to_node(struct snd_soc_component *component)
746 struct device_node *of_node;
748 of_node = component->dev->of_node;
749 if (!of_node && component->dev->parent)
750 of_node = component->dev->parent->of_node;
755 static int snd_soc_is_matching_component(
756 const struct snd_soc_dai_link_component *dlc,
757 struct snd_soc_component *component)
759 struct device_node *component_of_node;
764 component_of_node = soc_component_to_node(component);
766 if (dlc->of_node && component_of_node != dlc->of_node)
768 if (dlc->name && strcmp(component->name, dlc->name))
774 static struct snd_soc_component *soc_find_component(
775 const struct snd_soc_dai_link_component *dlc)
777 struct snd_soc_component *component;
779 lockdep_assert_held(&client_mutex);
784 * It returns *1st* found component, but some driver
785 * has few components by same of_node/name
787 * CPU component and generic DMAEngine component
789 for_each_component(component)
790 if (snd_soc_is_matching_component(dlc, component))
797 * snd_soc_find_dai - Find a registered DAI
799 * @dlc: name of the DAI or the DAI driver and optional component info to match
801 * This function will search all registered components and their DAIs to
802 * find the DAI of the same name. The component's of_node and name
803 * should also match if being specified.
805 * Return: pointer of DAI, or NULL if not found.
807 struct snd_soc_dai *snd_soc_find_dai(
808 const struct snd_soc_dai_link_component *dlc)
810 struct snd_soc_component *component;
811 struct snd_soc_dai *dai;
813 lockdep_assert_held(&client_mutex);
815 /* Find CPU DAI from registered DAIs */
816 for_each_component(component) {
817 if (!snd_soc_is_matching_component(dlc, component))
819 for_each_component_dais(component, dai) {
820 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
821 && (!dai->driver->name
822 || strcmp(dai->driver->name, dlc->dai_name)))
831 EXPORT_SYMBOL_GPL(snd_soc_find_dai);
833 static int soc_dai_link_sanity_check(struct snd_soc_card *card,
834 struct snd_soc_dai_link *link)
837 struct snd_soc_dai_link_component *cpu, *codec, *platform;
839 for_each_link_codecs(link, i, codec) {
841 * Codec must be specified by 1 of name or OF node,
842 * not both or neither.
844 if (!!codec->name == !!codec->of_node) {
845 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
850 /* Codec DAI name must be specified */
851 if (!codec->dai_name) {
852 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
858 * Defer card registration if codec component is not added to
861 if (!soc_find_component(codec)) {
863 "ASoC: codec component %s not found for link %s\n",
864 codec->name, link->name);
865 return -EPROBE_DEFER;
869 for_each_link_platforms(link, i, platform) {
871 * Platform may be specified by either name or OF node, but it
872 * can be left unspecified, then no components will be inserted
875 if (!!platform->name == !!platform->of_node) {
877 "ASoC: Neither/both platform name/of_node are set for %s\n",
883 * Defer card registration if platform component is not added to
886 if (!soc_find_component(platform)) {
888 "ASoC: platform component %s not found for link %s\n",
889 platform->name, link->name);
890 return -EPROBE_DEFER;
894 for_each_link_cpus(link, i, cpu) {
896 * CPU device may be specified by either name or OF node, but
897 * can be left unspecified, and will be matched based on DAI
900 if (cpu->name && cpu->of_node) {
902 "ASoC: Neither/both cpu name/of_node are set for %s\n",
908 * Defer card registration if cpu dai component is not added to
911 if ((cpu->of_node || cpu->name) &&
912 !soc_find_component(cpu)) {
914 "ASoC: cpu component %s not found for link %s\n",
915 cpu->name, link->name);
916 return -EPROBE_DEFER;
920 * At least one of CPU DAI name or CPU device name/node must be
923 if (!cpu->dai_name &&
924 !(cpu->name || cpu->of_node)) {
926 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
936 * snd_soc_remove_pcm_runtime - Remove a pcm_runtime from card
937 * @card: The ASoC card to which the pcm_runtime has
938 * @rtd: The pcm_runtime to remove
940 * This function removes a pcm_runtime from the ASoC card.
942 void snd_soc_remove_pcm_runtime(struct snd_soc_card *card,
943 struct snd_soc_pcm_runtime *rtd)
945 lockdep_assert_held(&client_mutex);
948 * Notify the machine driver for extra destruction
950 snd_soc_card_remove_dai_link(card, rtd->dai_link);
952 soc_free_pcm_runtime(rtd);
954 EXPORT_SYMBOL_GPL(snd_soc_remove_pcm_runtime);
957 * snd_soc_add_pcm_runtime - Add a pcm_runtime dynamically via dai_link
958 * @card: The ASoC card to which the pcm_runtime is added
959 * @dai_link: The DAI link to find pcm_runtime
961 * This function adds a pcm_runtime ASoC card by using dai_link.
963 * Note: Topology can use this API to add pcm_runtime when probing the
964 * topology component. And machine drivers can still define static
965 * DAI links in dai_link array.
967 int snd_soc_add_pcm_runtime(struct snd_soc_card *card,
968 struct snd_soc_dai_link *dai_link)
970 struct snd_soc_pcm_runtime *rtd;
971 struct snd_soc_dai_link_component *codec, *platform, *cpu;
972 struct snd_soc_component *component;
975 lockdep_assert_held(&client_mutex);
978 * Notify the machine driver for extra initialization
980 ret = snd_soc_card_add_dai_link(card, dai_link);
984 if (dai_link->ignore)
987 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
989 ret = soc_dai_link_sanity_check(card, dai_link);
993 rtd = soc_new_pcm_runtime(card, dai_link);
997 for_each_link_cpus(dai_link, i, cpu) {
998 asoc_rtd_to_cpu(rtd, i) = snd_soc_find_dai(cpu);
999 if (!asoc_rtd_to_cpu(rtd, i)) {
1000 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
1004 snd_soc_rtd_add_component(rtd, asoc_rtd_to_cpu(rtd, i)->component);
1007 /* Find CODEC from registered CODECs */
1008 for_each_link_codecs(dai_link, i, codec) {
1009 asoc_rtd_to_codec(rtd, i) = snd_soc_find_dai(codec);
1010 if (!asoc_rtd_to_codec(rtd, i)) {
1011 dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
1016 snd_soc_rtd_add_component(rtd, asoc_rtd_to_codec(rtd, i)->component);
1019 /* Find PLATFORM from registered PLATFORMs */
1020 for_each_link_platforms(dai_link, i, platform) {
1021 for_each_component(component) {
1022 if (!snd_soc_is_matching_component(platform, component))
1025 snd_soc_rtd_add_component(rtd, component);
1032 snd_soc_remove_pcm_runtime(card, rtd);
1033 return -EPROBE_DEFER;
1035 EXPORT_SYMBOL_GPL(snd_soc_add_pcm_runtime);
1037 static int soc_init_pcm_runtime(struct snd_soc_card *card,
1038 struct snd_soc_pcm_runtime *rtd)
1040 struct snd_soc_dai_link *dai_link = rtd->dai_link;
1041 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
1042 struct snd_soc_component *component;
1045 /* set default power off timeout */
1046 rtd->pmdown_time = pmdown_time;
1048 /* do machine specific initialization */
1049 ret = snd_soc_link_init(rtd);
1053 if (dai_link->dai_fmt) {
1054 ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1059 /* add DPCM sysfs entries */
1060 soc_dpcm_debugfs_add(rtd);
1065 * most drivers will register their PCMs using DAI link ordering but
1066 * topology based drivers can use the DAI link id field to set PCM
1067 * device number and then use rtd + a base offset of the BEs.
1069 for_each_rtd_components(rtd, i, component) {
1070 if (!component->driver->use_dai_pcm_id)
1073 if (rtd->dai_link->no_pcm)
1074 num += component->driver->be_pcm_base;
1076 num = rtd->dai_link->id;
1079 /* create compress_device if possible */
1080 ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1081 if (ret != -ENOTSUPP) {
1083 dev_err(card->dev, "ASoC: can't create compress %s\n",
1084 dai_link->stream_name);
1088 /* create the pcm */
1089 ret = soc_new_pcm(rtd, num);
1091 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1092 dai_link->stream_name, ret);
1096 return snd_soc_pcm_dai_new(rtd);
1099 static void soc_set_name_prefix(struct snd_soc_card *card,
1100 struct snd_soc_component *component)
1102 struct device_node *of_node = soc_component_to_node(component);
1106 for (i = 0; i < card->num_configs; i++) {
1107 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1109 if (snd_soc_is_matching_component(&map->dlc, component)) {
1110 component->name_prefix = map->name_prefix;
1116 * If there is no configuration table or no match in the table,
1117 * check if a prefix is provided in the node
1119 ret = of_property_read_string(of_node, "sound-name-prefix", &str);
1123 component->name_prefix = str;
1126 static void soc_remove_component(struct snd_soc_component *component,
1130 if (!component->card)
1134 snd_soc_component_remove(component);
1136 /* For framework level robustness */
1137 snd_soc_component_set_jack(component, NULL, NULL);
1139 list_del_init(&component->card_list);
1140 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1141 soc_cleanup_component_debugfs(component);
1142 component->card = NULL;
1143 snd_soc_component_module_put_when_remove(component);
1146 static int soc_probe_component(struct snd_soc_card *card,
1147 struct snd_soc_component *component)
1149 struct snd_soc_dapm_context *dapm =
1150 snd_soc_component_get_dapm(component);
1151 struct snd_soc_dai *dai;
1155 if (!strcmp(component->name, "snd-soc-dummy"))
1158 if (component->card) {
1159 if (component->card != card) {
1160 dev_err(component->dev,
1161 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1162 card->name, component->card->name);
1168 ret = snd_soc_component_module_get_when_probe(component);
1172 component->card = card;
1173 soc_set_name_prefix(card, component);
1175 soc_init_component_debugfs(component);
1177 snd_soc_dapm_init(dapm, card, component);
1179 ret = snd_soc_dapm_new_controls(dapm,
1180 component->driver->dapm_widgets,
1181 component->driver->num_dapm_widgets);
1184 dev_err(component->dev,
1185 "Failed to create new controls %d\n", ret);
1189 for_each_component_dais(component, dai) {
1190 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1192 dev_err(component->dev,
1193 "Failed to create DAI widgets %d\n", ret);
1198 ret = snd_soc_component_probe(component);
1200 dev_err(component->dev,
1201 "ASoC: failed to probe component %d\n", ret);
1204 WARN(dapm->idle_bias_off &&
1205 dapm->bias_level != SND_SOC_BIAS_OFF,
1206 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1210 /* machine specific init */
1211 if (component->init) {
1212 ret = component->init(component);
1214 dev_err(component->dev,
1215 "Failed to do machine specific init %d\n", ret);
1220 ret = snd_soc_add_component_controls(component,
1221 component->driver->controls,
1222 component->driver->num_controls);
1226 ret = snd_soc_dapm_add_routes(dapm,
1227 component->driver->dapm_routes,
1228 component->driver->num_dapm_routes);
1230 if (card->disable_route_checks) {
1232 "%s: disable_route_checks set, ignoring errors on add_routes\n",
1236 "%s: snd_soc_dapm_add_routes failed: %d\n",
1242 /* see for_each_card_components */
1243 list_add(&component->card_list, &card->component_dev_list);
1247 soc_remove_component(component, probed);
1252 static void soc_remove_link_dais(struct snd_soc_card *card)
1254 struct snd_soc_pcm_runtime *rtd;
1257 for_each_comp_order(order) {
1258 for_each_card_rtds(card, rtd) {
1259 /* remove all rtd connected DAIs in good order */
1260 snd_soc_pcm_dai_remove(rtd, order);
1265 static int soc_probe_link_dais(struct snd_soc_card *card)
1267 struct snd_soc_pcm_runtime *rtd;
1270 for_each_comp_order(order) {
1271 for_each_card_rtds(card, rtd) {
1274 "ASoC: probe %s dai link %d late %d\n",
1275 card->name, rtd->num, order);
1277 /* probe all rtd connected DAIs in good order */
1278 ret = snd_soc_pcm_dai_probe(rtd, order);
1287 static void soc_remove_link_components(struct snd_soc_card *card)
1289 struct snd_soc_component *component;
1290 struct snd_soc_pcm_runtime *rtd;
1293 for_each_comp_order(order) {
1294 for_each_card_rtds(card, rtd) {
1295 for_each_rtd_components(rtd, i, component) {
1296 if (component->driver->remove_order != order)
1299 soc_remove_component(component, 1);
1305 static int soc_probe_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->probe_order != order)
1317 ret = soc_probe_component(card, component);
1327 static void soc_unbind_aux_dev(struct snd_soc_card *card)
1329 struct snd_soc_component *component, *_component;
1331 for_each_card_auxs_safe(card, component, _component) {
1332 component->init = NULL;
1333 list_del(&component->card_aux_list);
1337 static int soc_bind_aux_dev(struct snd_soc_card *card)
1339 struct snd_soc_component *component;
1340 struct snd_soc_aux_dev *aux;
1343 for_each_card_pre_auxs(card, i, aux) {
1344 /* codecs, usually analog devices */
1345 component = soc_find_component(&aux->dlc);
1347 return -EPROBE_DEFER;
1349 component->init = aux->init;
1350 /* see for_each_card_auxs */
1351 list_add(&component->card_aux_list, &card->aux_comp_list);
1356 static int soc_probe_aux_devices(struct snd_soc_card *card)
1358 struct snd_soc_component *component;
1362 for_each_comp_order(order) {
1363 for_each_card_auxs(card, component) {
1364 if (component->driver->probe_order != order)
1367 ret = soc_probe_component(card, component);
1376 static void soc_remove_aux_devices(struct snd_soc_card *card)
1378 struct snd_soc_component *comp, *_comp;
1381 for_each_comp_order(order) {
1382 for_each_card_auxs_safe(card, comp, _comp) {
1383 if (comp->driver->remove_order == order)
1384 soc_remove_component(comp, 1);
1390 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1391 * @rtd: The runtime for which the DAI link format should be changed
1392 * @dai_fmt: The new DAI link format
1394 * This function updates the DAI link format for all DAIs connected to the DAI
1395 * link for the specified runtime.
1397 * Note: For setups with a static format set the dai_fmt field in the
1398 * corresponding snd_dai_link struct instead of using this function.
1400 * Returns 0 on success, otherwise a negative error code.
1402 int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1403 unsigned int dai_fmt)
1405 struct snd_soc_dai *cpu_dai;
1406 struct snd_soc_dai *codec_dai;
1407 unsigned int inv_dai_fmt;
1411 for_each_rtd_codec_dais(rtd, i, codec_dai) {
1412 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1413 if (ret != 0 && ret != -ENOTSUPP) {
1414 dev_warn(codec_dai->dev,
1415 "ASoC: Failed to set DAI format: %d\n", ret);
1421 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1422 * the component which has non_legacy_dai_naming is Codec
1424 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1425 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1426 case SND_SOC_DAIFMT_CBM_CFM:
1427 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1429 case SND_SOC_DAIFMT_CBM_CFS:
1430 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1432 case SND_SOC_DAIFMT_CBS_CFM:
1433 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1435 case SND_SOC_DAIFMT_CBS_CFS:
1436 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1439 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1440 unsigned int fmt = dai_fmt;
1442 if (cpu_dai->component->driver->non_legacy_dai_naming)
1445 ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
1446 if (ret != 0 && ret != -ENOTSUPP) {
1447 dev_warn(cpu_dai->dev,
1448 "ASoC: Failed to set DAI format: %d\n", ret);
1455 EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1459 * If a DMI filed contain strings in this blacklist (e.g.
1460 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
1461 * as invalid and dropped when setting the card long name from DMI info.
1463 static const char * const dmi_blacklist[] = {
1464 "To be filled by OEM",
1467 "Board Manufacturer",
1468 "Board Vendor Name",
1469 "Board Product Name",
1470 NULL, /* terminator */
1474 * Trim special characters, and replace '-' with '_' since '-' is used to
1475 * separate different DMI fields in the card long name. Only number and
1476 * alphabet characters and a few separator characters are kept.
1478 static void cleanup_dmi_name(char *name)
1482 for (i = 0; name[i]; i++) {
1483 if (isalnum(name[i]) || (name[i] == '.')
1484 || (name[i] == '_'))
1485 name[j++] = name[i];
1486 else if (name[i] == '-')
1494 * Check if a DMI field is valid, i.e. not containing any string
1495 * in the black list.
1497 static int is_dmi_valid(const char *field)
1501 while (dmi_blacklist[i]) {
1502 if (strstr(field, dmi_blacklist[i]))
1511 * Append a string to card->dmi_longname with character cleanups.
1513 static void append_dmi_string(struct snd_soc_card *card, const char *str)
1515 char *dst = card->dmi_longname;
1516 size_t dst_len = sizeof(card->dmi_longname);
1520 snprintf(dst + len, dst_len - len, "-%s", str);
1522 len++; /* skip the separator "-" */
1524 cleanup_dmi_name(dst + len);
1528 * snd_soc_set_dmi_name() - Register DMI names to card
1529 * @card: The card to register DMI names
1530 * @flavour: The flavour "differentiator" for the card amongst its peers.
1532 * An Intel machine driver may be used by many different devices but are
1533 * difficult for userspace to differentiate, since machine drivers ususally
1534 * use their own name as the card short name and leave the card long name
1535 * blank. To differentiate such devices and fix bugs due to lack of
1536 * device-specific configurations, this function allows DMI info to be used
1537 * as the sound card long name, in the format of
1538 * "vendor-product-version-board"
1539 * (Character '-' is used to separate different DMI fields here).
1540 * This will help the user space to load the device-specific Use Case Manager
1541 * (UCM) configurations for the card.
1543 * Possible card long names may be:
1544 * DellInc.-XPS139343-01-0310JH
1545 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1546 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1548 * This function also supports flavoring the card longname to provide
1549 * the extra differentiation, like "vendor-product-version-board-flavor".
1551 * We only keep number and alphabet characters and a few separator characters
1552 * in the card long name since UCM in the user space uses the card long names
1553 * as card configuration directory names and AudoConf cannot support special
1554 * charactors like SPACE.
1556 * Returns 0 on success, otherwise a negative error code.
1558 int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1560 const char *vendor, *product, *product_version, *board;
1562 if (card->long_name)
1563 return 0; /* long name already set by driver or from DMI */
1565 /* make up dmi long name as: vendor-product-version-board */
1566 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1567 if (!vendor || !is_dmi_valid(vendor)) {
1568 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1572 snprintf(card->dmi_longname, sizeof(card->dmi_longname), "%s", vendor);
1573 cleanup_dmi_name(card->dmi_longname);
1575 product = dmi_get_system_info(DMI_PRODUCT_NAME);
1576 if (product && is_dmi_valid(product)) {
1577 append_dmi_string(card, product);
1580 * some vendors like Lenovo may only put a self-explanatory
1581 * name in the product version field
1583 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
1584 if (product_version && is_dmi_valid(product_version))
1585 append_dmi_string(card, product_version);
1588 board = dmi_get_system_info(DMI_BOARD_NAME);
1589 if (board && is_dmi_valid(board)) {
1590 if (!product || strcasecmp(board, product))
1591 append_dmi_string(card, board);
1592 } else if (!product) {
1593 /* fall back to using legacy name */
1594 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1598 /* Add flavour to dmi long name */
1600 append_dmi_string(card, flavour);
1602 /* set the card long name */
1603 card->long_name = card->dmi_longname;
1607 EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
1608 #endif /* CONFIG_DMI */
1610 static void soc_check_tplg_fes(struct snd_soc_card *card)
1612 struct snd_soc_component *component;
1613 const struct snd_soc_component_driver *comp_drv;
1614 struct snd_soc_dai_link *dai_link;
1617 for_each_component(component) {
1619 /* does this component override BEs ? */
1620 if (!component->driver->ignore_machine)
1623 /* for this machine ? */
1624 if (!strcmp(component->driver->ignore_machine,
1625 card->dev->driver->name))
1627 if (strcmp(component->driver->ignore_machine,
1628 dev_name(card->dev)))
1631 /* machine matches, so override the rtd data */
1632 for_each_card_prelinks(card, i, dai_link) {
1634 /* ignore this FE */
1635 if (dai_link->dynamic) {
1636 dai_link->ignore = true;
1640 dev_info(card->dev, "info: override BE DAI link %s\n",
1641 card->dai_link[i].name);
1643 /* override platform component */
1644 if (!dai_link->platforms) {
1645 dev_err(card->dev, "init platform error");
1648 dai_link->platforms->name = component->name;
1650 /* convert non BE into BE */
1651 dai_link->no_pcm = 1;
1652 dai_link->dpcm_playback = 1;
1653 dai_link->dpcm_capture = 1;
1656 * override any BE fixups
1658 * snd_soc_link_be_hw_params_fixup()
1660 dai_link->be_hw_params_fixup =
1661 component->driver->be_hw_params_fixup;
1664 * most BE links don't set stream name, so set it to
1665 * dai link name if it's NULL to help bind widgets.
1667 if (!dai_link->stream_name)
1668 dai_link->stream_name = dai_link->name;
1671 /* Inform userspace we are using alternate topology */
1672 if (component->driver->topology_name_prefix) {
1674 /* topology shortname created? */
1675 if (!card->topology_shortname_created) {
1676 comp_drv = component->driver;
1678 snprintf(card->topology_shortname, 32, "%s-%s",
1679 comp_drv->topology_name_prefix,
1681 card->topology_shortname_created = true;
1684 /* use topology shortname */
1685 card->name = card->topology_shortname;
1690 #define soc_setup_card_name(name, name1, name2, norm) \
1691 __soc_setup_card_name(name, sizeof(name), name1, name2, norm)
1692 static void __soc_setup_card_name(char *name, int len,
1693 const char *name1, const char *name2,
1698 snprintf(name, len, "%s", name1 ? name1 : name2);
1704 * Name normalization
1706 * The driver name is somewhat special, as it's used as a key for
1707 * searches in the user-space.
1710 * "abcd??efg" -> "abcd__efg"
1712 for (i = 0; i < len; i++) {
1719 if (!isalnum(name[i]))
1726 static void soc_cleanup_card_resources(struct snd_soc_card *card)
1728 struct snd_soc_pcm_runtime *rtd, *n;
1731 snd_card_disconnect_sync(card->snd_card);
1733 snd_soc_dapm_shutdown(card);
1735 /* remove and free each DAI */
1736 soc_remove_link_dais(card);
1737 soc_remove_link_components(card);
1739 for_each_card_rtds_safe(card, rtd, n)
1740 snd_soc_remove_pcm_runtime(card, rtd);
1742 /* remove auxiliary devices */
1743 soc_remove_aux_devices(card);
1744 soc_unbind_aux_dev(card);
1746 snd_soc_dapm_free(&card->dapm);
1747 soc_cleanup_card_debugfs(card);
1749 /* remove the card */
1750 snd_soc_card_remove(card);
1752 if (card->snd_card) {
1753 snd_card_free(card->snd_card);
1754 card->snd_card = NULL;
1758 static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
1760 if (card->instantiated) {
1761 card->instantiated = false;
1762 snd_soc_flush_all_delayed_work(card);
1764 soc_cleanup_card_resources(card);
1766 list_add(&card->list, &unbind_card_list);
1769 list_del(&card->list);
1773 static int snd_soc_bind_card(struct snd_soc_card *card)
1775 struct snd_soc_pcm_runtime *rtd;
1776 struct snd_soc_component *component;
1777 struct snd_soc_dai_link *dai_link;
1780 mutex_lock(&client_mutex);
1781 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1783 snd_soc_dapm_init(&card->dapm, card, NULL);
1785 /* check whether any platform is ignore machine FE and using topology */
1786 soc_check_tplg_fes(card);
1788 /* bind aux_devs too */
1789 ret = soc_bind_aux_dev(card);
1793 /* add predefined DAI links to the list */
1795 for_each_card_prelinks(card, i, dai_link) {
1796 ret = snd_soc_add_pcm_runtime(card, dai_link);
1801 /* card bind complete so register a sound card */
1802 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1803 card->owner, 0, &card->snd_card);
1806 "ASoC: can't create sound card for card %s: %d\n",
1811 soc_init_card_debugfs(card);
1813 soc_resume_init(card);
1815 ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1816 card->num_dapm_widgets);
1820 ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1821 card->num_of_dapm_widgets);
1825 /* initialise the sound card only once */
1826 ret = snd_soc_card_probe(card);
1830 /* probe all components used by DAI links on this card */
1831 ret = soc_probe_link_components(card);
1834 "ASoC: failed to instantiate card %d\n", ret);
1838 /* probe auxiliary components */
1839 ret = soc_probe_aux_devices(card);
1842 "ASoC: failed to probe aux component %d\n", ret);
1846 /* probe all DAI links on this card */
1847 ret = soc_probe_link_dais(card);
1850 "ASoC: failed to instantiate card %d\n", ret);
1854 for_each_card_rtds(card, rtd) {
1855 ret = soc_init_pcm_runtime(card, rtd);
1860 snd_soc_dapm_link_dai_widgets(card);
1861 snd_soc_dapm_connect_dai_link_widgets(card);
1863 ret = snd_soc_add_card_controls(card, card->controls,
1864 card->num_controls);
1868 ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1869 card->num_dapm_routes);
1871 if (card->disable_route_checks) {
1873 "%s: disable_route_checks set, ignoring errors on add_routes\n",
1877 "%s: snd_soc_dapm_add_routes failed: %d\n",
1883 ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
1884 card->num_of_dapm_routes);
1888 /* try to set some sane longname if DMI is available */
1889 snd_soc_set_dmi_name(card, NULL);
1891 soc_setup_card_name(card->snd_card->shortname,
1892 card->name, NULL, 0);
1893 soc_setup_card_name(card->snd_card->longname,
1894 card->long_name, card->name, 0);
1895 soc_setup_card_name(card->snd_card->driver,
1896 card->driver_name, card->name, 1);
1898 if (card->components) {
1899 /* the current implementation of snd_component_add() accepts */
1900 /* multiple components in the string separated by space, */
1901 /* but the string collision (identical string) check might */
1902 /* not work correctly */
1903 ret = snd_component_add(card->snd_card, card->components);
1905 dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n",
1911 ret = snd_soc_card_late_probe(card);
1915 snd_soc_dapm_new_widgets(card);
1917 ret = snd_card_register(card->snd_card);
1919 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1924 card->instantiated = 1;
1925 dapm_mark_endpoints_dirty(card);
1926 snd_soc_dapm_sync(&card->dapm);
1928 /* deactivate pins to sleep state */
1929 for_each_card_components(card, component)
1930 if (!snd_soc_component_active(component))
1931 pinctrl_pm_select_sleep_state(component->dev);
1935 soc_cleanup_card_resources(card);
1937 mutex_unlock(&card->mutex);
1938 mutex_unlock(&client_mutex);
1943 /* probes a new socdev */
1944 static int soc_probe(struct platform_device *pdev)
1946 struct snd_soc_card *card = platform_get_drvdata(pdev);
1949 * no card, so machine driver should be registering card
1950 * we should not be here in that case so ret error
1955 dev_warn(&pdev->dev,
1956 "ASoC: machine %s should use snd_soc_register_card()\n",
1959 /* Bodge while we unpick instantiation */
1960 card->dev = &pdev->dev;
1962 return snd_soc_register_card(card);
1965 /* removes a socdev */
1966 static int soc_remove(struct platform_device *pdev)
1968 struct snd_soc_card *card = platform_get_drvdata(pdev);
1970 snd_soc_unregister_card(card);
1974 int snd_soc_poweroff(struct device *dev)
1976 struct snd_soc_card *card = dev_get_drvdata(dev);
1977 struct snd_soc_component *component;
1979 if (!card->instantiated)
1983 * Flush out pmdown_time work - we actually do want to run it
1984 * now, we're shutting down so no imminent restart.
1986 snd_soc_flush_all_delayed_work(card);
1988 snd_soc_dapm_shutdown(card);
1990 /* deactivate pins to sleep state */
1991 for_each_card_components(card, component)
1992 pinctrl_pm_select_sleep_state(component->dev);
1996 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1998 const struct dev_pm_ops snd_soc_pm_ops = {
1999 .suspend = snd_soc_suspend,
2000 .resume = snd_soc_resume,
2001 .freeze = snd_soc_suspend,
2002 .thaw = snd_soc_resume,
2003 .poweroff = snd_soc_poweroff,
2004 .restore = snd_soc_resume,
2006 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2008 /* ASoC platform driver */
2009 static struct platform_driver soc_driver = {
2011 .name = "soc-audio",
2012 .pm = &snd_soc_pm_ops,
2015 .remove = soc_remove,
2019 * snd_soc_cnew - create new control
2020 * @_template: control template
2021 * @data: control private data
2022 * @long_name: control long name
2023 * @prefix: control name prefix
2025 * Create a new mixer control from a template control.
2027 * Returns 0 for success, else error.
2029 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2030 void *data, const char *long_name,
2033 struct snd_kcontrol_new template;
2034 struct snd_kcontrol *kcontrol;
2037 memcpy(&template, _template, sizeof(template));
2041 long_name = template.name;
2044 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2048 template.name = name;
2050 template.name = long_name;
2053 kcontrol = snd_ctl_new1(&template, data);
2059 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2061 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2062 const struct snd_kcontrol_new *controls, int num_controls,
2063 const char *prefix, void *data)
2067 for (i = 0; i < num_controls; i++) {
2068 const struct snd_kcontrol_new *control = &controls[i];
2070 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2071 control->name, prefix));
2073 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2074 control->name, err);
2083 * snd_soc_add_component_controls - Add an array of controls to a component.
2085 * @component: Component to add controls to
2086 * @controls: Array of controls to add
2087 * @num_controls: Number of elements in the array
2089 * Return: 0 for success, else error.
2091 int snd_soc_add_component_controls(struct snd_soc_component *component,
2092 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2094 struct snd_card *card = component->card->snd_card;
2096 return snd_soc_add_controls(card, component->dev, controls,
2097 num_controls, component->name_prefix, component);
2099 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2102 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2103 * Convenience function to add a list of controls.
2105 * @soc_card: SoC card to add controls to
2106 * @controls: array of controls to add
2107 * @num_controls: number of elements in the array
2109 * Return 0 for success, else error.
2111 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2112 const struct snd_kcontrol_new *controls, int num_controls)
2114 struct snd_card *card = soc_card->snd_card;
2116 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2119 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2122 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2123 * Convienience function to add a list of controls.
2125 * @dai: DAI to add controls to
2126 * @controls: array of controls to add
2127 * @num_controls: number of elements in the array
2129 * Return 0 for success, else error.
2131 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2132 const struct snd_kcontrol_new *controls, int num_controls)
2134 struct snd_card *card = dai->component->card->snd_card;
2136 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2139 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2142 * snd_soc_register_card - Register a card with the ASoC core
2144 * @card: Card to register
2147 int snd_soc_register_card(struct snd_soc_card *card)
2149 if (!card->name || !card->dev)
2152 dev_set_drvdata(card->dev, card);
2154 INIT_LIST_HEAD(&card->widgets);
2155 INIT_LIST_HEAD(&card->paths);
2156 INIT_LIST_HEAD(&card->dapm_list);
2157 INIT_LIST_HEAD(&card->aux_comp_list);
2158 INIT_LIST_HEAD(&card->component_dev_list);
2159 INIT_LIST_HEAD(&card->list);
2160 INIT_LIST_HEAD(&card->rtd_list);
2161 INIT_LIST_HEAD(&card->dapm_dirty);
2162 INIT_LIST_HEAD(&card->dobj_list);
2164 card->instantiated = 0;
2165 mutex_init(&card->mutex);
2166 mutex_init(&card->dapm_mutex);
2167 mutex_init(&card->pcm_mutex);
2168 spin_lock_init(&card->dpcm_lock);
2170 return snd_soc_bind_card(card);
2172 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2175 * snd_soc_unregister_card - Unregister a card with the ASoC core
2177 * @card: Card to unregister
2180 int snd_soc_unregister_card(struct snd_soc_card *card)
2182 mutex_lock(&client_mutex);
2183 snd_soc_unbind_card(card, true);
2184 mutex_unlock(&client_mutex);
2185 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2189 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2192 * Simplify DAI link configuration by removing ".-1" from device names
2193 * and sanitizing names.
2195 static char *fmt_single_name(struct device *dev, int *id)
2197 char *found, name[NAME_SIZE];
2200 if (dev_name(dev) == NULL)
2203 strlcpy(name, dev_name(dev), NAME_SIZE);
2205 /* are we a "%s.%d" name (platform and SPI components) */
2206 found = strstr(name, dev->driver->name);
2209 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2211 /* discard ID from name if ID == -1 */
2213 found[strlen(dev->driver->name)] = '\0';
2217 /* I2C component devices are named "bus-addr" */
2218 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2219 char tmp[NAME_SIZE];
2221 /* create unique ID number from I2C addr and bus */
2222 *id = ((id1 & 0xffff) << 16) + id2;
2224 /* sanitize component name for DAI link creation */
2225 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2227 strlcpy(name, tmp, NAME_SIZE);
2232 return devm_kstrdup(dev, name, GFP_KERNEL);
2236 * Simplify DAI link naming for single devices with multiple DAIs by removing
2237 * any ".-1" and using the DAI name (instead of device name).
2239 static inline char *fmt_multiple_name(struct device *dev,
2240 struct snd_soc_dai_driver *dai_drv)
2242 if (dai_drv->name == NULL) {
2244 "ASoC: error - multiple DAI %s registered with no name\n",
2249 return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2252 void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2254 dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2255 list_del(&dai->list);
2257 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2260 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2262 * @component: The component the DAIs are registered for
2263 * @dai_drv: DAI driver to use for the DAI
2264 * @legacy_dai_naming: if %true, use legacy single-name format;
2265 * if %false, use multiple-name format;
2267 * Topology can use this API to register DAIs when probing a component.
2268 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2269 * will be freed in the component cleanup.
2271 struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
2272 struct snd_soc_dai_driver *dai_drv,
2273 bool legacy_dai_naming)
2275 struct device *dev = component->dev;
2276 struct snd_soc_dai *dai;
2278 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2280 lockdep_assert_held(&client_mutex);
2282 dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
2287 * Back in the old days when we still had component-less DAIs,
2288 * instead of having a static name, component-less DAIs would
2289 * inherit the name of the parent device so it is possible to
2290 * register multiple instances of the DAI. We still need to keep
2291 * the same naming style even though those DAIs are not
2292 * component-less anymore.
2294 if (legacy_dai_naming &&
2295 (dai_drv->id == 0 || dai_drv->name == NULL)) {
2296 dai->name = fmt_single_name(dev, &dai->id);
2298 dai->name = fmt_multiple_name(dev, dai_drv);
2300 dai->id = dai_drv->id;
2302 dai->id = component->num_dai;
2307 dai->component = component;
2309 dai->driver = dai_drv;
2311 /* see for_each_component_dais */
2312 list_add_tail(&dai->list, &component->dai_list);
2313 component->num_dai++;
2315 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2320 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2322 * @component: The component for which the DAIs should be unregistered
2324 static void snd_soc_unregister_dais(struct snd_soc_component *component)
2326 struct snd_soc_dai *dai, *_dai;
2328 for_each_component_dais_safe(component, dai, _dai)
2329 snd_soc_unregister_dai(dai);
2333 * snd_soc_register_dais - Register a DAI with the ASoC core
2335 * @component: The component the DAIs are registered for
2336 * @dai_drv: DAI driver to use for the DAIs
2337 * @count: Number of DAIs
2339 static int snd_soc_register_dais(struct snd_soc_component *component,
2340 struct snd_soc_dai_driver *dai_drv,
2343 struct snd_soc_dai *dai;
2347 for (i = 0; i < count; i++) {
2348 dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
2349 !component->driver->non_legacy_dai_naming);
2359 snd_soc_unregister_dais(component);
2364 static int snd_soc_component_initialize(struct snd_soc_component *component,
2365 const struct snd_soc_component_driver *driver, struct device *dev)
2367 INIT_LIST_HEAD(&component->dai_list);
2368 INIT_LIST_HEAD(&component->dobj_list);
2369 INIT_LIST_HEAD(&component->card_list);
2370 mutex_init(&component->io_mutex);
2372 component->name = fmt_single_name(dev, &component->id);
2373 if (!component->name) {
2374 dev_err(dev, "ASoC: Failed to allocate name\n");
2378 component->dev = dev;
2379 component->driver = driver;
2384 static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2386 int val_bytes = regmap_get_val_bytes(component->regmap);
2388 /* Errors are legitimate for non-integer byte multiples */
2390 component->val_bytes = val_bytes;
2393 #ifdef CONFIG_REGMAP
2396 * snd_soc_component_init_regmap() - Initialize regmap instance for the
2398 * @component: The component for which to initialize the regmap instance
2399 * @regmap: The regmap instance that should be used by the component
2401 * This function allows deferred assignment of the regmap instance that is
2402 * associated with the component. Only use this if the regmap instance is not
2403 * yet ready when the component is registered. The function must also be called
2404 * before the first IO attempt of the component.
2406 void snd_soc_component_init_regmap(struct snd_soc_component *component,
2407 struct regmap *regmap)
2409 component->regmap = regmap;
2410 snd_soc_component_setup_regmap(component);
2412 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
2415 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
2417 * @component: The component for which to de-initialize the regmap instance
2419 * Calls regmap_exit() on the regmap instance associated to the component and
2420 * removes the regmap instance from the component.
2422 * This function should only be used if snd_soc_component_init_regmap() was used
2423 * to initialize the regmap instance.
2425 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
2427 regmap_exit(component->regmap);
2428 component->regmap = NULL;
2430 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2434 #define ENDIANNESS_MAP(name) \
2435 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2436 static u64 endianness_format_map[] = {
2437 ENDIANNESS_MAP(S16_),
2438 ENDIANNESS_MAP(U16_),
2439 ENDIANNESS_MAP(S24_),
2440 ENDIANNESS_MAP(U24_),
2441 ENDIANNESS_MAP(S32_),
2442 ENDIANNESS_MAP(U32_),
2443 ENDIANNESS_MAP(S24_3),
2444 ENDIANNESS_MAP(U24_3),
2445 ENDIANNESS_MAP(S20_3),
2446 ENDIANNESS_MAP(U20_3),
2447 ENDIANNESS_MAP(S18_3),
2448 ENDIANNESS_MAP(U18_3),
2449 ENDIANNESS_MAP(FLOAT_),
2450 ENDIANNESS_MAP(FLOAT64_),
2451 ENDIANNESS_MAP(IEC958_SUBFRAME_),
2455 * Fix up the DAI formats for endianness: codecs don't actually see
2456 * the endianness of the data but we're using the CPU format
2457 * definitions which do need to include endianness so we ensure that
2458 * codec DAIs always have both big and little endian variants set.
2460 static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2464 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2465 if (stream->formats & endianness_format_map[i])
2466 stream->formats |= endianness_format_map[i];
2469 static void snd_soc_try_rebind_card(void)
2471 struct snd_soc_card *card, *c;
2473 list_for_each_entry_safe(card, c, &unbind_card_list, list)
2474 if (!snd_soc_bind_card(card))
2475 list_del(&card->list);
2478 static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2480 struct snd_soc_card *card = component->card;
2482 snd_soc_unregister_dais(component);
2485 snd_soc_unbind_card(card, false);
2487 list_del(&component->list);
2490 int snd_soc_add_component(struct device *dev,
2491 struct snd_soc_component *component,
2492 const struct snd_soc_component_driver *component_driver,
2493 struct snd_soc_dai_driver *dai_drv,
2499 mutex_lock(&client_mutex);
2501 ret = snd_soc_component_initialize(component, component_driver, dev);
2505 if (component_driver->endianness) {
2506 for (i = 0; i < num_dai; i++) {
2507 convert_endianness_formats(&dai_drv[i].playback);
2508 convert_endianness_formats(&dai_drv[i].capture);
2512 ret = snd_soc_register_dais(component, dai_drv, num_dai);
2514 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
2518 if (!component->driver->write && !component->driver->read) {
2519 if (!component->regmap)
2520 component->regmap = dev_get_regmap(component->dev,
2522 if (component->regmap)
2523 snd_soc_component_setup_regmap(component);
2526 /* see for_each_component */
2527 list_add(&component->list, &component_list);
2531 snd_soc_del_component_unlocked(component);
2533 mutex_unlock(&client_mutex);
2536 snd_soc_try_rebind_card();
2540 EXPORT_SYMBOL_GPL(snd_soc_add_component);
2542 int snd_soc_register_component(struct device *dev,
2543 const struct snd_soc_component_driver *component_driver,
2544 struct snd_soc_dai_driver *dai_drv,
2547 struct snd_soc_component *component;
2549 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
2553 return snd_soc_add_component(dev, component, component_driver,
2556 EXPORT_SYMBOL_GPL(snd_soc_register_component);
2559 * snd_soc_unregister_component - Unregister all related component
2560 * from the ASoC core
2562 * @dev: The device to unregister
2564 void snd_soc_unregister_component(struct device *dev)
2566 struct snd_soc_component *component;
2568 mutex_lock(&client_mutex);
2570 component = snd_soc_lookup_component_nolocked(dev, NULL);
2574 snd_soc_del_component_unlocked(component);
2576 mutex_unlock(&client_mutex);
2578 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2580 /* Retrieve a card's name from device tree */
2581 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2582 const char *propname)
2584 struct device_node *np;
2588 pr_err("card->dev is not set before calling %s\n", __func__);
2592 np = card->dev->of_node;
2594 ret = of_property_read_string_index(np, propname, 0, &card->name);
2596 * EINVAL means the property does not exist. This is fine providing
2597 * card->name was previously set, which is checked later in
2598 * snd_soc_register_card.
2600 if (ret < 0 && ret != -EINVAL) {
2602 "ASoC: Property '%s' could not be read: %d\n",
2609 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2611 static const struct snd_soc_dapm_widget simple_widgets[] = {
2612 SND_SOC_DAPM_MIC("Microphone", NULL),
2613 SND_SOC_DAPM_LINE("Line", NULL),
2614 SND_SOC_DAPM_HP("Headphone", NULL),
2615 SND_SOC_DAPM_SPK("Speaker", NULL),
2618 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
2619 const char *propname)
2621 struct device_node *np = card->dev->of_node;
2622 struct snd_soc_dapm_widget *widgets;
2623 const char *template, *wname;
2624 int i, j, num_widgets, ret;
2626 num_widgets = of_property_count_strings(np, propname);
2627 if (num_widgets < 0) {
2629 "ASoC: Property '%s' does not exist\n", propname);
2632 if (num_widgets & 1) {
2634 "ASoC: Property '%s' length is not even\n", propname);
2640 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
2645 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
2649 "ASoC: Could not allocate memory for widgets\n");
2653 for (i = 0; i < num_widgets; i++) {
2654 ret = of_property_read_string_index(np, propname,
2658 "ASoC: Property '%s' index %d read error:%d\n",
2659 propname, 2 * i, ret);
2663 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
2664 if (!strncmp(template, simple_widgets[j].name,
2665 strlen(simple_widgets[j].name))) {
2666 widgets[i] = simple_widgets[j];
2671 if (j >= ARRAY_SIZE(simple_widgets)) {
2673 "ASoC: DAPM widget '%s' is not supported\n",
2678 ret = of_property_read_string_index(np, propname,
2683 "ASoC: Property '%s' index %d read error:%d\n",
2684 propname, (2 * i) + 1, ret);
2688 widgets[i].name = wname;
2691 card->of_dapm_widgets = widgets;
2692 card->num_of_dapm_widgets = num_widgets;
2696 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
2698 int snd_soc_of_get_slot_mask(struct device_node *np,
2699 const char *prop_name,
2703 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
2709 for (i = 0; i < val; i++)
2710 if (be32_to_cpup(&of_slot_mask[i]))
2715 EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
2717 int snd_soc_of_parse_tdm_slot(struct device_node *np,
2718 unsigned int *tx_mask,
2719 unsigned int *rx_mask,
2720 unsigned int *slots,
2721 unsigned int *slot_width)
2727 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
2729 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
2731 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
2732 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
2740 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
2741 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
2751 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
2753 void snd_soc_of_parse_node_prefix(struct device_node *np,
2754 struct snd_soc_codec_conf *codec_conf,
2755 struct device_node *of_node,
2756 const char *propname)
2761 ret = of_property_read_string(np, propname, &str);
2763 /* no prefix is not error */
2767 codec_conf->dlc.of_node = of_node;
2768 codec_conf->name_prefix = str;
2770 EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
2772 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
2773 const char *propname)
2775 struct device_node *np = card->dev->of_node;
2777 struct snd_soc_dapm_route *routes;
2780 num_routes = of_property_count_strings(np, propname);
2781 if (num_routes < 0 || num_routes & 1) {
2783 "ASoC: Property '%s' does not exist or its length is not even\n",
2789 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
2794 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
2798 "ASoC: Could not allocate DAPM route table\n");
2802 for (i = 0; i < num_routes; i++) {
2803 ret = of_property_read_string_index(np, propname,
2804 2 * i, &routes[i].sink);
2807 "ASoC: Property '%s' index %d could not be read: %d\n",
2808 propname, 2 * i, ret);
2811 ret = of_property_read_string_index(np, propname,
2812 (2 * i) + 1, &routes[i].source);
2815 "ASoC: Property '%s' index %d could not be read: %d\n",
2816 propname, (2 * i) + 1, ret);
2821 card->num_of_dapm_routes = num_routes;
2822 card->of_dapm_routes = routes;
2826 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
2828 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
2830 struct device_node **bitclkmaster,
2831 struct device_node **framemaster)
2835 unsigned int format = 0;
2841 } of_fmt_table[] = {
2842 { "i2s", SND_SOC_DAIFMT_I2S },
2843 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
2844 { "left_j", SND_SOC_DAIFMT_LEFT_J },
2845 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
2846 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
2847 { "ac97", SND_SOC_DAIFMT_AC97 },
2848 { "pdm", SND_SOC_DAIFMT_PDM},
2849 { "msb", SND_SOC_DAIFMT_MSB },
2850 { "lsb", SND_SOC_DAIFMT_LSB },
2857 * check "dai-format = xxx"
2858 * or "[prefix]format = xxx"
2859 * SND_SOC_DAIFMT_FORMAT_MASK area
2861 ret = of_property_read_string(np, "dai-format", &str);
2863 snprintf(prop, sizeof(prop), "%sformat", prefix);
2864 ret = of_property_read_string(np, prop, &str);
2867 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
2868 if (strcmp(str, of_fmt_table[i].name) == 0) {
2869 format |= of_fmt_table[i].val;
2876 * check "[prefix]continuous-clock"
2877 * SND_SOC_DAIFMT_CLOCK_MASK area
2879 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
2880 if (of_property_read_bool(np, prop))
2881 format |= SND_SOC_DAIFMT_CONT;
2883 format |= SND_SOC_DAIFMT_GATED;
2886 * check "[prefix]bitclock-inversion"
2887 * check "[prefix]frame-inversion"
2888 * SND_SOC_DAIFMT_INV_MASK area
2890 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
2891 bit = !!of_get_property(np, prop, NULL);
2893 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
2894 frame = !!of_get_property(np, prop, NULL);
2896 switch ((bit << 4) + frame) {
2898 format |= SND_SOC_DAIFMT_IB_IF;
2901 format |= SND_SOC_DAIFMT_IB_NF;
2904 format |= SND_SOC_DAIFMT_NB_IF;
2907 /* SND_SOC_DAIFMT_NB_NF is default */
2912 * check "[prefix]bitclock-master"
2913 * check "[prefix]frame-master"
2914 * SND_SOC_DAIFMT_MASTER_MASK area
2916 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
2917 bit = !!of_get_property(np, prop, NULL);
2918 if (bit && bitclkmaster)
2919 *bitclkmaster = of_parse_phandle(np, prop, 0);
2921 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
2922 frame = !!of_get_property(np, prop, NULL);
2923 if (frame && framemaster)
2924 *framemaster = of_parse_phandle(np, prop, 0);
2926 switch ((bit << 4) + frame) {
2928 format |= SND_SOC_DAIFMT_CBM_CFM;
2931 format |= SND_SOC_DAIFMT_CBM_CFS;
2934 format |= SND_SOC_DAIFMT_CBS_CFM;
2937 format |= SND_SOC_DAIFMT_CBS_CFS;
2943 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
2945 int snd_soc_get_dai_id(struct device_node *ep)
2947 struct snd_soc_component *component;
2948 struct snd_soc_dai_link_component dlc;
2951 dlc.of_node = of_graph_get_port_parent(ep);
2954 * For example HDMI case, HDMI has video/sound port,
2955 * but ALSA SoC needs sound port number only.
2956 * Thus counting HDMI DT port/endpoint doesn't work.
2957 * Then, it should have .of_xlate_dai_id
2960 mutex_lock(&client_mutex);
2961 component = soc_find_component(&dlc);
2963 ret = snd_soc_component_of_xlate_dai_id(component, ep);
2964 mutex_unlock(&client_mutex);
2966 of_node_put(dlc.of_node);
2970 EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
2972 int snd_soc_get_dai_name(struct of_phandle_args *args,
2973 const char **dai_name)
2975 struct snd_soc_component *pos;
2976 struct device_node *component_of_node;
2977 int ret = -EPROBE_DEFER;
2979 mutex_lock(&client_mutex);
2980 for_each_component(pos) {
2981 component_of_node = soc_component_to_node(pos);
2983 if (component_of_node != args->np)
2986 ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
2987 if (ret == -ENOTSUPP) {
2988 struct snd_soc_dai *dai;
2991 switch (args->args_count) {
2993 id = 0; /* same as dai_drv[0] */
3003 if (id < 0 || id >= pos->num_dai) {
3010 /* find target DAI */
3011 for_each_component_dais(pos, dai) {
3017 *dai_name = dai->driver->name;
3019 *dai_name = pos->name;
3022 * if another error than ENOTSUPP is returned go on and
3023 * check if another component is provided with the same
3024 * node. This may happen if a device provides several
3032 mutex_unlock(&client_mutex);
3035 EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
3037 int snd_soc_of_get_dai_name(struct device_node *of_node,
3038 const char **dai_name)
3040 struct of_phandle_args args;
3043 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3044 "#sound-dai-cells", 0, &args);
3048 ret = snd_soc_get_dai_name(&args, dai_name);
3050 of_node_put(args.np);
3054 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3057 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3058 * @dai_link: DAI link
3060 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3062 void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3064 struct snd_soc_dai_link_component *component;
3067 for_each_link_codecs(dai_link, index, component) {
3068 if (!component->of_node)
3070 of_node_put(component->of_node);
3071 component->of_node = NULL;
3074 EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3077 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3079 * @of_node: Device node
3080 * @dai_link: DAI link
3082 * Builds an array of CODEC DAI components from the DAI link property
3084 * The array is set in the DAI link and the number of DAIs is set accordingly.
3085 * The device nodes in the array (of_node) must be dereferenced by calling
3086 * snd_soc_of_put_dai_link_codecs() on @dai_link.
3088 * Returns 0 for success
3090 int snd_soc_of_get_dai_link_codecs(struct device *dev,
3091 struct device_node *of_node,
3092 struct snd_soc_dai_link *dai_link)
3094 struct of_phandle_args args;
3095 struct snd_soc_dai_link_component *component;
3097 int index, num_codecs, ret;
3099 /* Count the number of CODECs */
3101 num_codecs = of_count_phandle_with_args(of_node, name,
3102 "#sound-dai-cells");
3103 if (num_codecs <= 0) {
3104 if (num_codecs == -ENOENT)
3105 dev_err(dev, "No 'sound-dai' property\n");
3107 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3110 component = devm_kcalloc(dev,
3111 num_codecs, sizeof(*component),
3115 dai_link->codecs = component;
3116 dai_link->num_codecs = num_codecs;
3118 /* Parse the list */
3119 for_each_link_codecs(dai_link, index, component) {
3120 ret = of_parse_phandle_with_args(of_node, name,
3125 component->of_node = args.np;
3126 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3132 snd_soc_of_put_dai_link_codecs(dai_link);
3133 dai_link->codecs = NULL;
3134 dai_link->num_codecs = 0;
3137 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3139 static int __init snd_soc_init(void)
3141 snd_soc_debugfs_init();
3142 snd_soc_util_init();
3144 return platform_driver_register(&soc_driver);
3146 module_init(snd_soc_init);
3148 static void __exit snd_soc_exit(void)
3150 snd_soc_util_exit();
3151 snd_soc_debugfs_exit();
3153 platform_driver_unregister(&soc_driver);
3155 module_exit(snd_soc_exit);
3157 /* Module information */
3159 MODULE_DESCRIPTION("ALSA SoC Core");
3160 MODULE_LICENSE("GPL");
3161 MODULE_ALIAS("platform:soc-audio");