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/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 if (!dai_link->no_pcm) {
1652 dai_link->no_pcm = 1;
1654 if (dai_link->dpcm_playback)
1656 "invalid configuration, dailink %s has flags no_pcm=0 and dpcm_playback=1\n",
1658 if (dai_link->dpcm_capture)
1660 "invalid configuration, dailink %s has flags no_pcm=0 and dpcm_capture=1\n",
1663 /* convert normal link into DPCM one */
1664 if (!(dai_link->dpcm_playback ||
1665 dai_link->dpcm_capture)) {
1666 dai_link->dpcm_playback = !dai_link->capture_only;
1667 dai_link->dpcm_capture = !dai_link->playback_only;
1672 * override any BE fixups
1674 * snd_soc_link_be_hw_params_fixup()
1676 dai_link->be_hw_params_fixup =
1677 component->driver->be_hw_params_fixup;
1680 * most BE links don't set stream name, so set it to
1681 * dai link name if it's NULL to help bind widgets.
1683 if (!dai_link->stream_name)
1684 dai_link->stream_name = dai_link->name;
1687 /* Inform userspace we are using alternate topology */
1688 if (component->driver->topology_name_prefix) {
1690 /* topology shortname created? */
1691 if (!card->topology_shortname_created) {
1692 comp_drv = component->driver;
1694 snprintf(card->topology_shortname, 32, "%s-%s",
1695 comp_drv->topology_name_prefix,
1697 card->topology_shortname_created = true;
1700 /* use topology shortname */
1701 card->name = card->topology_shortname;
1706 #define soc_setup_card_name(name, name1, name2, norm) \
1707 __soc_setup_card_name(name, sizeof(name), name1, name2, norm)
1708 static void __soc_setup_card_name(char *name, int len,
1709 const char *name1, const char *name2,
1714 snprintf(name, len, "%s", name1 ? name1 : name2);
1720 * Name normalization
1722 * The driver name is somewhat special, as it's used as a key for
1723 * searches in the user-space.
1726 * "abcd??efg" -> "abcd__efg"
1728 for (i = 0; i < len; i++) {
1735 if (!isalnum(name[i]))
1742 static void soc_cleanup_card_resources(struct snd_soc_card *card)
1744 struct snd_soc_pcm_runtime *rtd, *n;
1747 snd_card_disconnect_sync(card->snd_card);
1749 snd_soc_dapm_shutdown(card);
1751 /* remove and free each DAI */
1752 soc_remove_link_dais(card);
1753 soc_remove_link_components(card);
1755 for_each_card_rtds_safe(card, rtd, n)
1756 snd_soc_remove_pcm_runtime(card, rtd);
1758 /* remove auxiliary devices */
1759 soc_remove_aux_devices(card);
1760 soc_unbind_aux_dev(card);
1762 snd_soc_dapm_free(&card->dapm);
1763 soc_cleanup_card_debugfs(card);
1765 /* remove the card */
1766 snd_soc_card_remove(card);
1768 if (card->snd_card) {
1769 snd_card_free(card->snd_card);
1770 card->snd_card = NULL;
1774 static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
1776 if (card->instantiated) {
1777 card->instantiated = false;
1778 snd_soc_flush_all_delayed_work(card);
1780 soc_cleanup_card_resources(card);
1782 list_add(&card->list, &unbind_card_list);
1785 list_del(&card->list);
1789 static int snd_soc_bind_card(struct snd_soc_card *card)
1791 struct snd_soc_pcm_runtime *rtd;
1792 struct snd_soc_component *component;
1793 struct snd_soc_dai_link *dai_link;
1796 mutex_lock(&client_mutex);
1797 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1799 snd_soc_dapm_init(&card->dapm, card, NULL);
1801 /* check whether any platform is ignore machine FE and using topology */
1802 soc_check_tplg_fes(card);
1804 /* bind aux_devs too */
1805 ret = soc_bind_aux_dev(card);
1809 /* add predefined DAI links to the list */
1811 for_each_card_prelinks(card, i, dai_link) {
1812 ret = snd_soc_add_pcm_runtime(card, dai_link);
1817 /* card bind complete so register a sound card */
1818 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1819 card->owner, 0, &card->snd_card);
1822 "ASoC: can't create sound card for card %s: %d\n",
1827 soc_init_card_debugfs(card);
1829 soc_resume_init(card);
1831 ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1832 card->num_dapm_widgets);
1836 ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1837 card->num_of_dapm_widgets);
1841 /* initialise the sound card only once */
1842 ret = snd_soc_card_probe(card);
1846 /* probe all components used by DAI links on this card */
1847 ret = soc_probe_link_components(card);
1850 "ASoC: failed to instantiate card %d\n", ret);
1854 /* probe auxiliary components */
1855 ret = soc_probe_aux_devices(card);
1858 "ASoC: failed to probe aux component %d\n", ret);
1862 /* probe all DAI links on this card */
1863 ret = soc_probe_link_dais(card);
1866 "ASoC: failed to instantiate card %d\n", ret);
1870 for_each_card_rtds(card, rtd) {
1871 ret = soc_init_pcm_runtime(card, rtd);
1876 snd_soc_dapm_link_dai_widgets(card);
1877 snd_soc_dapm_connect_dai_link_widgets(card);
1879 ret = snd_soc_add_card_controls(card, card->controls,
1880 card->num_controls);
1884 ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1885 card->num_dapm_routes);
1887 if (card->disable_route_checks) {
1889 "%s: disable_route_checks set, ignoring errors on add_routes\n",
1893 "%s: snd_soc_dapm_add_routes failed: %d\n",
1899 ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
1900 card->num_of_dapm_routes);
1904 /* try to set some sane longname if DMI is available */
1905 snd_soc_set_dmi_name(card, NULL);
1907 soc_setup_card_name(card->snd_card->shortname,
1908 card->name, NULL, 0);
1909 soc_setup_card_name(card->snd_card->longname,
1910 card->long_name, card->name, 0);
1911 soc_setup_card_name(card->snd_card->driver,
1912 card->driver_name, card->name, 1);
1914 if (card->components) {
1915 /* the current implementation of snd_component_add() accepts */
1916 /* multiple components in the string separated by space, */
1917 /* but the string collision (identical string) check might */
1918 /* not work correctly */
1919 ret = snd_component_add(card->snd_card, card->components);
1921 dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n",
1927 ret = snd_soc_card_late_probe(card);
1931 snd_soc_dapm_new_widgets(card);
1933 ret = snd_card_register(card->snd_card);
1935 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1940 card->instantiated = 1;
1941 dapm_mark_endpoints_dirty(card);
1942 snd_soc_dapm_sync(&card->dapm);
1944 /* deactivate pins to sleep state */
1945 for_each_card_components(card, component)
1946 if (!snd_soc_component_active(component))
1947 pinctrl_pm_select_sleep_state(component->dev);
1951 soc_cleanup_card_resources(card);
1953 mutex_unlock(&card->mutex);
1954 mutex_unlock(&client_mutex);
1959 /* probes a new socdev */
1960 static int soc_probe(struct platform_device *pdev)
1962 struct snd_soc_card *card = platform_get_drvdata(pdev);
1965 * no card, so machine driver should be registering card
1966 * we should not be here in that case so ret error
1971 dev_warn(&pdev->dev,
1972 "ASoC: machine %s should use snd_soc_register_card()\n",
1975 /* Bodge while we unpick instantiation */
1976 card->dev = &pdev->dev;
1978 return snd_soc_register_card(card);
1981 /* removes a socdev */
1982 static int soc_remove(struct platform_device *pdev)
1984 struct snd_soc_card *card = platform_get_drvdata(pdev);
1986 snd_soc_unregister_card(card);
1990 int snd_soc_poweroff(struct device *dev)
1992 struct snd_soc_card *card = dev_get_drvdata(dev);
1993 struct snd_soc_component *component;
1995 if (!card->instantiated)
1999 * Flush out pmdown_time work - we actually do want to run it
2000 * now, we're shutting down so no imminent restart.
2002 snd_soc_flush_all_delayed_work(card);
2004 snd_soc_dapm_shutdown(card);
2006 /* deactivate pins to sleep state */
2007 for_each_card_components(card, component)
2008 pinctrl_pm_select_sleep_state(component->dev);
2012 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2014 const struct dev_pm_ops snd_soc_pm_ops = {
2015 .suspend = snd_soc_suspend,
2016 .resume = snd_soc_resume,
2017 .freeze = snd_soc_suspend,
2018 .thaw = snd_soc_resume,
2019 .poweroff = snd_soc_poweroff,
2020 .restore = snd_soc_resume,
2022 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2024 /* ASoC platform driver */
2025 static struct platform_driver soc_driver = {
2027 .name = "soc-audio",
2028 .pm = &snd_soc_pm_ops,
2031 .remove = soc_remove,
2035 * snd_soc_cnew - create new control
2036 * @_template: control template
2037 * @data: control private data
2038 * @long_name: control long name
2039 * @prefix: control name prefix
2041 * Create a new mixer control from a template control.
2043 * Returns 0 for success, else error.
2045 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2046 void *data, const char *long_name,
2049 struct snd_kcontrol_new template;
2050 struct snd_kcontrol *kcontrol;
2053 memcpy(&template, _template, sizeof(template));
2057 long_name = template.name;
2060 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2064 template.name = name;
2066 template.name = long_name;
2069 kcontrol = snd_ctl_new1(&template, data);
2075 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2077 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2078 const struct snd_kcontrol_new *controls, int num_controls,
2079 const char *prefix, void *data)
2083 for (i = 0; i < num_controls; i++) {
2084 const struct snd_kcontrol_new *control = &controls[i];
2086 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2087 control->name, prefix));
2089 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2090 control->name, err);
2099 * snd_soc_add_component_controls - Add an array of controls to a component.
2101 * @component: Component to add controls to
2102 * @controls: Array of controls to add
2103 * @num_controls: Number of elements in the array
2105 * Return: 0 for success, else error.
2107 int snd_soc_add_component_controls(struct snd_soc_component *component,
2108 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2110 struct snd_card *card = component->card->snd_card;
2112 return snd_soc_add_controls(card, component->dev, controls,
2113 num_controls, component->name_prefix, component);
2115 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2118 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2119 * Convenience function to add a list of controls.
2121 * @soc_card: SoC card to add controls to
2122 * @controls: array of controls to add
2123 * @num_controls: number of elements in the array
2125 * Return 0 for success, else error.
2127 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2128 const struct snd_kcontrol_new *controls, int num_controls)
2130 struct snd_card *card = soc_card->snd_card;
2132 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2135 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2138 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2139 * Convienience function to add a list of controls.
2141 * @dai: DAI to add controls to
2142 * @controls: array of controls to add
2143 * @num_controls: number of elements in the array
2145 * Return 0 for success, else error.
2147 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2148 const struct snd_kcontrol_new *controls, int num_controls)
2150 struct snd_card *card = dai->component->card->snd_card;
2152 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2155 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2158 * snd_soc_register_card - Register a card with the ASoC core
2160 * @card: Card to register
2163 int snd_soc_register_card(struct snd_soc_card *card)
2165 if (!card->name || !card->dev)
2168 dev_set_drvdata(card->dev, card);
2170 INIT_LIST_HEAD(&card->widgets);
2171 INIT_LIST_HEAD(&card->paths);
2172 INIT_LIST_HEAD(&card->dapm_list);
2173 INIT_LIST_HEAD(&card->aux_comp_list);
2174 INIT_LIST_HEAD(&card->component_dev_list);
2175 INIT_LIST_HEAD(&card->list);
2176 INIT_LIST_HEAD(&card->rtd_list);
2177 INIT_LIST_HEAD(&card->dapm_dirty);
2178 INIT_LIST_HEAD(&card->dobj_list);
2180 card->instantiated = 0;
2181 mutex_init(&card->mutex);
2182 mutex_init(&card->dapm_mutex);
2183 mutex_init(&card->pcm_mutex);
2184 spin_lock_init(&card->dpcm_lock);
2186 return snd_soc_bind_card(card);
2188 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2191 * snd_soc_unregister_card - Unregister a card with the ASoC core
2193 * @card: Card to unregister
2196 int snd_soc_unregister_card(struct snd_soc_card *card)
2198 mutex_lock(&client_mutex);
2199 snd_soc_unbind_card(card, true);
2200 mutex_unlock(&client_mutex);
2201 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2205 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2208 * Simplify DAI link configuration by removing ".-1" from device names
2209 * and sanitizing names.
2211 static char *fmt_single_name(struct device *dev, int *id)
2213 char *found, name[NAME_SIZE];
2216 if (dev_name(dev) == NULL)
2219 strlcpy(name, dev_name(dev), NAME_SIZE);
2221 /* are we a "%s.%d" name (platform and SPI components) */
2222 found = strstr(name, dev->driver->name);
2225 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2227 /* discard ID from name if ID == -1 */
2229 found[strlen(dev->driver->name)] = '\0';
2233 /* I2C component devices are named "bus-addr" */
2234 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2235 char tmp[NAME_SIZE];
2237 /* create unique ID number from I2C addr and bus */
2238 *id = ((id1 & 0xffff) << 16) + id2;
2240 /* sanitize component name for DAI link creation */
2241 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2243 strlcpy(name, tmp, NAME_SIZE);
2248 return devm_kstrdup(dev, name, GFP_KERNEL);
2252 * Simplify DAI link naming for single devices with multiple DAIs by removing
2253 * any ".-1" and using the DAI name (instead of device name).
2255 static inline char *fmt_multiple_name(struct device *dev,
2256 struct snd_soc_dai_driver *dai_drv)
2258 if (dai_drv->name == NULL) {
2260 "ASoC: error - multiple DAI %s registered with no name\n",
2265 return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2268 void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2270 dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2271 list_del(&dai->list);
2273 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2276 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2278 * @component: The component the DAIs are registered for
2279 * @dai_drv: DAI driver to use for the DAI
2280 * @legacy_dai_naming: if %true, use legacy single-name format;
2281 * if %false, use multiple-name format;
2283 * Topology can use this API to register DAIs when probing a component.
2284 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2285 * will be freed in the component cleanup.
2287 struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
2288 struct snd_soc_dai_driver *dai_drv,
2289 bool legacy_dai_naming)
2291 struct device *dev = component->dev;
2292 struct snd_soc_dai *dai;
2294 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2296 lockdep_assert_held(&client_mutex);
2298 dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
2303 * Back in the old days when we still had component-less DAIs,
2304 * instead of having a static name, component-less DAIs would
2305 * inherit the name of the parent device so it is possible to
2306 * register multiple instances of the DAI. We still need to keep
2307 * the same naming style even though those DAIs are not
2308 * component-less anymore.
2310 if (legacy_dai_naming &&
2311 (dai_drv->id == 0 || dai_drv->name == NULL)) {
2312 dai->name = fmt_single_name(dev, &dai->id);
2314 dai->name = fmt_multiple_name(dev, dai_drv);
2316 dai->id = dai_drv->id;
2318 dai->id = component->num_dai;
2323 dai->component = component;
2325 dai->driver = dai_drv;
2327 /* see for_each_component_dais */
2328 list_add_tail(&dai->list, &component->dai_list);
2329 component->num_dai++;
2331 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2336 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2338 * @component: The component for which the DAIs should be unregistered
2340 static void snd_soc_unregister_dais(struct snd_soc_component *component)
2342 struct snd_soc_dai *dai, *_dai;
2344 for_each_component_dais_safe(component, dai, _dai)
2345 snd_soc_unregister_dai(dai);
2349 * snd_soc_register_dais - Register a DAI with the ASoC core
2351 * @component: The component the DAIs are registered for
2352 * @dai_drv: DAI driver to use for the DAIs
2353 * @count: Number of DAIs
2355 static int snd_soc_register_dais(struct snd_soc_component *component,
2356 struct snd_soc_dai_driver *dai_drv,
2359 struct snd_soc_dai *dai;
2363 for (i = 0; i < count; i++) {
2364 dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
2365 !component->driver->non_legacy_dai_naming);
2375 snd_soc_unregister_dais(component);
2380 static int snd_soc_component_initialize(struct snd_soc_component *component,
2381 const struct snd_soc_component_driver *driver, struct device *dev)
2383 INIT_LIST_HEAD(&component->dai_list);
2384 INIT_LIST_HEAD(&component->dobj_list);
2385 INIT_LIST_HEAD(&component->card_list);
2386 mutex_init(&component->io_mutex);
2388 component->name = fmt_single_name(dev, &component->id);
2389 if (!component->name) {
2390 dev_err(dev, "ASoC: Failed to allocate name\n");
2394 component->dev = dev;
2395 component->driver = driver;
2400 static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2402 int val_bytes = regmap_get_val_bytes(component->regmap);
2404 /* Errors are legitimate for non-integer byte multiples */
2406 component->val_bytes = val_bytes;
2409 #ifdef CONFIG_REGMAP
2412 * snd_soc_component_init_regmap() - Initialize regmap instance for the
2414 * @component: The component for which to initialize the regmap instance
2415 * @regmap: The regmap instance that should be used by the component
2417 * This function allows deferred assignment of the regmap instance that is
2418 * associated with the component. Only use this if the regmap instance is not
2419 * yet ready when the component is registered. The function must also be called
2420 * before the first IO attempt of the component.
2422 void snd_soc_component_init_regmap(struct snd_soc_component *component,
2423 struct regmap *regmap)
2425 component->regmap = regmap;
2426 snd_soc_component_setup_regmap(component);
2428 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
2431 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
2433 * @component: The component for which to de-initialize the regmap instance
2435 * Calls regmap_exit() on the regmap instance associated to the component and
2436 * removes the regmap instance from the component.
2438 * This function should only be used if snd_soc_component_init_regmap() was used
2439 * to initialize the regmap instance.
2441 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
2443 regmap_exit(component->regmap);
2444 component->regmap = NULL;
2446 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2450 #define ENDIANNESS_MAP(name) \
2451 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2452 static u64 endianness_format_map[] = {
2453 ENDIANNESS_MAP(S16_),
2454 ENDIANNESS_MAP(U16_),
2455 ENDIANNESS_MAP(S24_),
2456 ENDIANNESS_MAP(U24_),
2457 ENDIANNESS_MAP(S32_),
2458 ENDIANNESS_MAP(U32_),
2459 ENDIANNESS_MAP(S24_3),
2460 ENDIANNESS_MAP(U24_3),
2461 ENDIANNESS_MAP(S20_3),
2462 ENDIANNESS_MAP(U20_3),
2463 ENDIANNESS_MAP(S18_3),
2464 ENDIANNESS_MAP(U18_3),
2465 ENDIANNESS_MAP(FLOAT_),
2466 ENDIANNESS_MAP(FLOAT64_),
2467 ENDIANNESS_MAP(IEC958_SUBFRAME_),
2471 * Fix up the DAI formats for endianness: codecs don't actually see
2472 * the endianness of the data but we're using the CPU format
2473 * definitions which do need to include endianness so we ensure that
2474 * codec DAIs always have both big and little endian variants set.
2476 static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2480 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2481 if (stream->formats & endianness_format_map[i])
2482 stream->formats |= endianness_format_map[i];
2485 static void snd_soc_try_rebind_card(void)
2487 struct snd_soc_card *card, *c;
2489 list_for_each_entry_safe(card, c, &unbind_card_list, list)
2490 if (!snd_soc_bind_card(card))
2491 list_del(&card->list);
2494 static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2496 struct snd_soc_card *card = component->card;
2498 snd_soc_unregister_dais(component);
2501 snd_soc_unbind_card(card, false);
2503 list_del(&component->list);
2506 int snd_soc_add_component(struct device *dev,
2507 struct snd_soc_component *component,
2508 const struct snd_soc_component_driver *component_driver,
2509 struct snd_soc_dai_driver *dai_drv,
2515 mutex_lock(&client_mutex);
2517 ret = snd_soc_component_initialize(component, component_driver, dev);
2521 if (component_driver->endianness) {
2522 for (i = 0; i < num_dai; i++) {
2523 convert_endianness_formats(&dai_drv[i].playback);
2524 convert_endianness_formats(&dai_drv[i].capture);
2528 ret = snd_soc_register_dais(component, dai_drv, num_dai);
2530 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
2534 if (!component->driver->write && !component->driver->read) {
2535 if (!component->regmap)
2536 component->regmap = dev_get_regmap(component->dev,
2538 if (component->regmap)
2539 snd_soc_component_setup_regmap(component);
2542 /* see for_each_component */
2543 list_add(&component->list, &component_list);
2547 snd_soc_del_component_unlocked(component);
2549 mutex_unlock(&client_mutex);
2552 snd_soc_try_rebind_card();
2556 EXPORT_SYMBOL_GPL(snd_soc_add_component);
2558 int snd_soc_register_component(struct device *dev,
2559 const struct snd_soc_component_driver *component_driver,
2560 struct snd_soc_dai_driver *dai_drv,
2563 struct snd_soc_component *component;
2565 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
2569 return snd_soc_add_component(dev, component, component_driver,
2572 EXPORT_SYMBOL_GPL(snd_soc_register_component);
2575 * snd_soc_unregister_component - Unregister all related component
2576 * from the ASoC core
2578 * @dev: The device to unregister
2580 void snd_soc_unregister_component(struct device *dev)
2582 struct snd_soc_component *component;
2584 mutex_lock(&client_mutex);
2586 component = snd_soc_lookup_component_nolocked(dev, NULL);
2590 snd_soc_del_component_unlocked(component);
2592 mutex_unlock(&client_mutex);
2594 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2596 /* Retrieve a card's name from device tree */
2597 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2598 const char *propname)
2600 struct device_node *np;
2604 pr_err("card->dev is not set before calling %s\n", __func__);
2608 np = card->dev->of_node;
2610 ret = of_property_read_string_index(np, propname, 0, &card->name);
2612 * EINVAL means the property does not exist. This is fine providing
2613 * card->name was previously set, which is checked later in
2614 * snd_soc_register_card.
2616 if (ret < 0 && ret != -EINVAL) {
2618 "ASoC: Property '%s' could not be read: %d\n",
2625 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2627 static const struct snd_soc_dapm_widget simple_widgets[] = {
2628 SND_SOC_DAPM_MIC("Microphone", NULL),
2629 SND_SOC_DAPM_LINE("Line", NULL),
2630 SND_SOC_DAPM_HP("Headphone", NULL),
2631 SND_SOC_DAPM_SPK("Speaker", NULL),
2634 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
2635 const char *propname)
2637 struct device_node *np = card->dev->of_node;
2638 struct snd_soc_dapm_widget *widgets;
2639 const char *template, *wname;
2640 int i, j, num_widgets, ret;
2642 num_widgets = of_property_count_strings(np, propname);
2643 if (num_widgets < 0) {
2645 "ASoC: Property '%s' does not exist\n", propname);
2648 if (num_widgets & 1) {
2650 "ASoC: Property '%s' length is not even\n", propname);
2656 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
2661 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
2665 "ASoC: Could not allocate memory for widgets\n");
2669 for (i = 0; i < num_widgets; i++) {
2670 ret = of_property_read_string_index(np, propname,
2674 "ASoC: Property '%s' index %d read error:%d\n",
2675 propname, 2 * i, ret);
2679 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
2680 if (!strncmp(template, simple_widgets[j].name,
2681 strlen(simple_widgets[j].name))) {
2682 widgets[i] = simple_widgets[j];
2687 if (j >= ARRAY_SIZE(simple_widgets)) {
2689 "ASoC: DAPM widget '%s' is not supported\n",
2694 ret = of_property_read_string_index(np, propname,
2699 "ASoC: Property '%s' index %d read error:%d\n",
2700 propname, (2 * i) + 1, ret);
2704 widgets[i].name = wname;
2707 card->of_dapm_widgets = widgets;
2708 card->num_of_dapm_widgets = num_widgets;
2712 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
2714 int snd_soc_of_get_slot_mask(struct device_node *np,
2715 const char *prop_name,
2719 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
2725 for (i = 0; i < val; i++)
2726 if (be32_to_cpup(&of_slot_mask[i]))
2731 EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
2733 int snd_soc_of_parse_tdm_slot(struct device_node *np,
2734 unsigned int *tx_mask,
2735 unsigned int *rx_mask,
2736 unsigned int *slots,
2737 unsigned int *slot_width)
2743 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
2745 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
2747 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
2748 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
2756 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
2757 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
2767 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
2769 void snd_soc_of_parse_node_prefix(struct device_node *np,
2770 struct snd_soc_codec_conf *codec_conf,
2771 struct device_node *of_node,
2772 const char *propname)
2777 ret = of_property_read_string(np, propname, &str);
2779 /* no prefix is not error */
2783 codec_conf->dlc.of_node = of_node;
2784 codec_conf->name_prefix = str;
2786 EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
2788 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
2789 const char *propname)
2791 struct device_node *np = card->dev->of_node;
2793 struct snd_soc_dapm_route *routes;
2796 num_routes = of_property_count_strings(np, propname);
2797 if (num_routes < 0 || num_routes & 1) {
2799 "ASoC: Property '%s' does not exist or its length is not even\n",
2805 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
2810 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
2814 "ASoC: Could not allocate DAPM route table\n");
2818 for (i = 0; i < num_routes; i++) {
2819 ret = of_property_read_string_index(np, propname,
2820 2 * i, &routes[i].sink);
2823 "ASoC: Property '%s' index %d could not be read: %d\n",
2824 propname, 2 * i, ret);
2827 ret = of_property_read_string_index(np, propname,
2828 (2 * i) + 1, &routes[i].source);
2831 "ASoC: Property '%s' index %d could not be read: %d\n",
2832 propname, (2 * i) + 1, ret);
2837 card->num_of_dapm_routes = num_routes;
2838 card->of_dapm_routes = routes;
2842 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
2844 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
2846 struct device_node **bitclkmaster,
2847 struct device_node **framemaster)
2851 unsigned int format = 0;
2857 } of_fmt_table[] = {
2858 { "i2s", SND_SOC_DAIFMT_I2S },
2859 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
2860 { "left_j", SND_SOC_DAIFMT_LEFT_J },
2861 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
2862 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
2863 { "ac97", SND_SOC_DAIFMT_AC97 },
2864 { "pdm", SND_SOC_DAIFMT_PDM},
2865 { "msb", SND_SOC_DAIFMT_MSB },
2866 { "lsb", SND_SOC_DAIFMT_LSB },
2873 * check "dai-format = xxx"
2874 * or "[prefix]format = xxx"
2875 * SND_SOC_DAIFMT_FORMAT_MASK area
2877 ret = of_property_read_string(np, "dai-format", &str);
2879 snprintf(prop, sizeof(prop), "%sformat", prefix);
2880 ret = of_property_read_string(np, prop, &str);
2883 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
2884 if (strcmp(str, of_fmt_table[i].name) == 0) {
2885 format |= of_fmt_table[i].val;
2892 * check "[prefix]continuous-clock"
2893 * SND_SOC_DAIFMT_CLOCK_MASK area
2895 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
2896 if (of_property_read_bool(np, prop))
2897 format |= SND_SOC_DAIFMT_CONT;
2899 format |= SND_SOC_DAIFMT_GATED;
2902 * check "[prefix]bitclock-inversion"
2903 * check "[prefix]frame-inversion"
2904 * SND_SOC_DAIFMT_INV_MASK area
2906 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
2907 bit = !!of_get_property(np, prop, NULL);
2909 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
2910 frame = !!of_get_property(np, prop, NULL);
2912 switch ((bit << 4) + frame) {
2914 format |= SND_SOC_DAIFMT_IB_IF;
2917 format |= SND_SOC_DAIFMT_IB_NF;
2920 format |= SND_SOC_DAIFMT_NB_IF;
2923 /* SND_SOC_DAIFMT_NB_NF is default */
2928 * check "[prefix]bitclock-master"
2929 * check "[prefix]frame-master"
2930 * SND_SOC_DAIFMT_MASTER_MASK area
2932 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
2933 bit = !!of_get_property(np, prop, NULL);
2934 if (bit && bitclkmaster)
2935 *bitclkmaster = of_parse_phandle(np, prop, 0);
2937 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
2938 frame = !!of_get_property(np, prop, NULL);
2939 if (frame && framemaster)
2940 *framemaster = of_parse_phandle(np, prop, 0);
2942 switch ((bit << 4) + frame) {
2944 format |= SND_SOC_DAIFMT_CBM_CFM;
2947 format |= SND_SOC_DAIFMT_CBM_CFS;
2950 format |= SND_SOC_DAIFMT_CBS_CFM;
2953 format |= SND_SOC_DAIFMT_CBS_CFS;
2959 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
2961 int snd_soc_get_dai_id(struct device_node *ep)
2963 struct snd_soc_component *component;
2964 struct snd_soc_dai_link_component dlc;
2967 dlc.of_node = of_graph_get_port_parent(ep);
2970 * For example HDMI case, HDMI has video/sound port,
2971 * but ALSA SoC needs sound port number only.
2972 * Thus counting HDMI DT port/endpoint doesn't work.
2973 * Then, it should have .of_xlate_dai_id
2976 mutex_lock(&client_mutex);
2977 component = soc_find_component(&dlc);
2979 ret = snd_soc_component_of_xlate_dai_id(component, ep);
2980 mutex_unlock(&client_mutex);
2982 of_node_put(dlc.of_node);
2986 EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
2988 int snd_soc_get_dai_name(struct of_phandle_args *args,
2989 const char **dai_name)
2991 struct snd_soc_component *pos;
2992 struct device_node *component_of_node;
2993 int ret = -EPROBE_DEFER;
2995 mutex_lock(&client_mutex);
2996 for_each_component(pos) {
2997 component_of_node = soc_component_to_node(pos);
2999 if (component_of_node != args->np)
3002 ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3003 if (ret == -ENOTSUPP) {
3004 struct snd_soc_dai *dai;
3007 switch (args->args_count) {
3009 id = 0; /* same as dai_drv[0] */
3019 if (id < 0 || id >= pos->num_dai) {
3026 /* find target DAI */
3027 for_each_component_dais(pos, dai) {
3033 *dai_name = dai->driver->name;
3035 *dai_name = pos->name;
3038 * if another error than ENOTSUPP is returned go on and
3039 * check if another component is provided with the same
3040 * node. This may happen if a device provides several
3048 mutex_unlock(&client_mutex);
3051 EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
3053 int snd_soc_of_get_dai_name(struct device_node *of_node,
3054 const char **dai_name)
3056 struct of_phandle_args args;
3059 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3060 "#sound-dai-cells", 0, &args);
3064 ret = snd_soc_get_dai_name(&args, dai_name);
3066 of_node_put(args.np);
3070 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3073 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3074 * @dai_link: DAI link
3076 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3078 void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3080 struct snd_soc_dai_link_component *component;
3083 for_each_link_codecs(dai_link, index, component) {
3084 if (!component->of_node)
3086 of_node_put(component->of_node);
3087 component->of_node = NULL;
3090 EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3093 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3095 * @of_node: Device node
3096 * @dai_link: DAI link
3098 * Builds an array of CODEC DAI components from the DAI link property
3100 * The array is set in the DAI link and the number of DAIs is set accordingly.
3101 * The device nodes in the array (of_node) must be dereferenced by calling
3102 * snd_soc_of_put_dai_link_codecs() on @dai_link.
3104 * Returns 0 for success
3106 int snd_soc_of_get_dai_link_codecs(struct device *dev,
3107 struct device_node *of_node,
3108 struct snd_soc_dai_link *dai_link)
3110 struct of_phandle_args args;
3111 struct snd_soc_dai_link_component *component;
3113 int index, num_codecs, ret;
3115 /* Count the number of CODECs */
3117 num_codecs = of_count_phandle_with_args(of_node, name,
3118 "#sound-dai-cells");
3119 if (num_codecs <= 0) {
3120 if (num_codecs == -ENOENT)
3121 dev_err(dev, "No 'sound-dai' property\n");
3123 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3126 component = devm_kcalloc(dev,
3127 num_codecs, sizeof(*component),
3131 dai_link->codecs = component;
3132 dai_link->num_codecs = num_codecs;
3134 /* Parse the list */
3135 for_each_link_codecs(dai_link, index, component) {
3136 ret = of_parse_phandle_with_args(of_node, name,
3141 component->of_node = args.np;
3142 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3148 snd_soc_of_put_dai_link_codecs(dai_link);
3149 dai_link->codecs = NULL;
3150 dai_link->num_codecs = 0;
3153 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3155 static int __init snd_soc_init(void)
3157 snd_soc_debugfs_init();
3158 snd_soc_util_init();
3160 return platform_driver_register(&soc_driver);
3162 module_init(snd_soc_init);
3164 static void __exit snd_soc_exit(void)
3166 snd_soc_util_exit();
3167 snd_soc_debugfs_exit();
3169 platform_driver_unregister(&soc_driver);
3171 module_exit(snd_soc_exit);
3173 /* Module information */
3174 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3175 MODULE_DESCRIPTION("ALSA SoC Core");
3176 MODULE_LICENSE("GPL");
3177 MODULE_ALIAS("platform:soc-audio");