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 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;
332 EXPORT_SYMBOL_GPL(snd_soc_lookup_component_nolocked);
334 struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
335 const char *driver_name)
337 struct snd_soc_component *component;
339 mutex_lock(&client_mutex);
340 component = snd_soc_lookup_component_nolocked(dev, driver_name);
341 mutex_unlock(&client_mutex);
345 EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
347 struct snd_soc_pcm_runtime
348 *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
349 struct snd_soc_dai_link *dai_link)
351 struct snd_soc_pcm_runtime *rtd;
353 for_each_card_rtds(card, rtd) {
354 if (rtd->dai_link == dai_link)
357 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link->name);
360 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
363 * Power down the audio subsystem pmdown_time msecs after close is called.
364 * This is to ensure there are no pops or clicks in between any music tracks
365 * due to DAPM power cycling.
367 void snd_soc_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
369 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
370 int playback = SNDRV_PCM_STREAM_PLAYBACK;
372 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
375 "ASoC: pop wq checking: %s status: %s waiting: %s\n",
376 codec_dai->driver->playback.stream_name,
377 snd_soc_dai_stream_active(codec_dai, playback) ?
378 "active" : "inactive",
379 rtd->pop_wait ? "yes" : "no");
381 /* are we waiting on this codec DAI stream */
382 if (rtd->pop_wait == 1) {
384 snd_soc_dapm_stream_event(rtd, playback,
385 SND_SOC_DAPM_STREAM_STOP);
388 mutex_unlock(&rtd->card->pcm_mutex);
390 EXPORT_SYMBOL_GPL(snd_soc_close_delayed_work);
392 static void soc_release_rtd_dev(struct device *dev)
394 /* "dev" means "rtd->dev" */
398 static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
403 list_del(&rtd->list);
405 if (delayed_work_pending(&rtd->delayed_work))
406 flush_delayed_work(&rtd->delayed_work);
407 snd_soc_pcm_component_free(rtd);
410 * we don't need to call kfree() for rtd->dev
412 * soc_release_rtd_dev()
414 * We don't need rtd->dev NULL check, because
415 * it is alloced *before* rtd.
417 * soc_new_pcm_runtime()
419 device_unregister(rtd->dev);
422 static void close_delayed_work(struct work_struct *work) {
423 struct snd_soc_pcm_runtime *rtd =
424 container_of(work, struct snd_soc_pcm_runtime,
427 if (rtd->close_delayed_work_func)
428 rtd->close_delayed_work_func(rtd);
431 static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
432 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
434 struct snd_soc_pcm_runtime *rtd;
435 struct snd_soc_component *component;
443 dev = kzalloc(sizeof(struct device), GFP_KERNEL);
447 dev->parent = card->dev;
448 dev->release = soc_release_rtd_dev;
449 dev->groups = soc_dev_attr_groups;
451 dev_set_name(dev, "%s", dai_link->name);
453 ret = device_register(dev);
455 put_device(dev); /* soc_release_rtd_dev */
462 rtd = devm_kzalloc(dev,
464 sizeof(*component) * (dai_link->num_cpus +
465 dai_link->num_codecs +
466 dai_link->num_platforms),
472 INIT_LIST_HEAD(&rtd->list);
473 for_each_pcm_streams(stream) {
474 INIT_LIST_HEAD(&rtd->dpcm[stream].be_clients);
475 INIT_LIST_HEAD(&rtd->dpcm[stream].fe_clients);
477 dev_set_drvdata(dev, rtd);
478 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
483 rtd->dais = devm_kcalloc(dev, dai_link->num_cpus + dai_link->num_codecs,
484 sizeof(struct snd_soc_dai *),
490 * dais = [][][][][][][][][][][][][][][][][][]
491 * ^cpu_dais ^codec_dais
492 * |--- num_cpus ---|--- num_codecs --|
495 * asoc_rtd_to_codec()
497 rtd->num_cpus = dai_link->num_cpus;
498 rtd->num_codecs = dai_link->num_codecs;
500 rtd->dai_link = dai_link;
501 rtd->num = card->num_rtd++;
503 /* see for_each_card_rtds */
504 list_add_tail(&rtd->list, &card->rtd_list);
509 soc_free_pcm_runtime(rtd);
513 static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
515 struct snd_soc_pcm_runtime *rtd;
517 for_each_card_rtds(card, rtd)
518 flush_delayed_work(&rtd->delayed_work);
521 #ifdef CONFIG_PM_SLEEP
522 /* powers down audio subsystem for suspend */
523 int snd_soc_suspend(struct device *dev)
525 struct snd_soc_card *card = dev_get_drvdata(dev);
526 struct snd_soc_component *component;
527 struct snd_soc_pcm_runtime *rtd;
528 int playback = SNDRV_PCM_STREAM_PLAYBACK;
531 /* If the card is not initialized yet there is nothing to do */
532 if (!card->instantiated)
536 * Due to the resume being scheduled into a workqueue we could
537 * suspend before that's finished - wait for it to complete.
539 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
541 /* we're going to block userspace touching us until resume completes */
542 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
544 /* mute any active DACs */
545 for_each_card_rtds(card, rtd) {
546 struct snd_soc_dai *dai;
548 if (rtd->dai_link->ignore_suspend)
551 for_each_rtd_codec_dais(rtd, i, dai) {
552 if (snd_soc_dai_stream_active(dai, playback))
553 snd_soc_dai_digital_mute(dai, 1, playback);
557 /* suspend all pcms */
558 for_each_card_rtds(card, rtd) {
559 if (rtd->dai_link->ignore_suspend)
562 snd_pcm_suspend_all(rtd->pcm);
565 snd_soc_card_suspend_pre(card);
567 /* close any waiting streams */
568 snd_soc_flush_all_delayed_work(card);
570 for_each_card_rtds(card, rtd) {
573 if (rtd->dai_link->ignore_suspend)
576 for_each_pcm_streams(stream)
577 snd_soc_dapm_stream_event(rtd, stream,
578 SND_SOC_DAPM_STREAM_SUSPEND);
581 /* Recheck all endpoints too, their state is affected by suspend */
582 dapm_mark_endpoints_dirty(card);
583 snd_soc_dapm_sync(&card->dapm);
585 /* suspend all COMPONENTs */
586 for_each_card_rtds(card, rtd) {
588 if (rtd->dai_link->ignore_suspend)
591 for_each_rtd_components(rtd, i, component) {
592 struct snd_soc_dapm_context *dapm =
593 snd_soc_component_get_dapm(component);
596 * ignore if component was already suspended
598 if (snd_soc_component_is_suspended(component))
602 * If there are paths active then the COMPONENT will be
603 * held with bias _ON and should not be suspended.
605 switch (snd_soc_dapm_get_bias_level(dapm)) {
606 case SND_SOC_BIAS_STANDBY:
608 * If the COMPONENT is capable of idle
609 * bias off then being in STANDBY
610 * means it's doing something,
611 * otherwise fall through.
613 if (dapm->idle_bias_off) {
614 dev_dbg(component->dev,
615 "ASoC: idle_bias_off CODEC on over suspend\n");
620 case SND_SOC_BIAS_OFF:
621 snd_soc_component_suspend(component);
622 if (component->regmap)
623 regcache_mark_dirty(component->regmap);
624 /* deactivate pins to sleep state */
625 pinctrl_pm_select_sleep_state(component->dev);
628 dev_dbg(component->dev,
629 "ASoC: COMPONENT is on over suspend\n");
635 snd_soc_card_suspend_post(card);
639 EXPORT_SYMBOL_GPL(snd_soc_suspend);
642 * deferred resume work, so resume can complete before we finished
643 * setting our codec back up, which can be very slow on I2C
645 static void soc_resume_deferred(struct work_struct *work)
647 struct snd_soc_card *card =
648 container_of(work, struct snd_soc_card,
649 deferred_resume_work);
650 struct snd_soc_pcm_runtime *rtd;
651 struct snd_soc_component *component;
655 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
656 * so userspace apps are blocked from touching us
659 dev_dbg(card->dev, "ASoC: starting resume work\n");
661 /* Bring us up into D2 so that DAPM starts enabling things */
662 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
664 snd_soc_card_resume_pre(card);
666 for_each_card_components(card, component) {
667 if (snd_soc_component_is_suspended(component))
668 snd_soc_component_resume(component);
671 for_each_card_rtds(card, rtd) {
674 if (rtd->dai_link->ignore_suspend)
677 for_each_pcm_streams(stream)
678 snd_soc_dapm_stream_event(rtd, stream,
679 SND_SOC_DAPM_STREAM_RESUME);
682 /* unmute any active DACs */
683 for_each_card_rtds(card, rtd) {
684 struct snd_soc_dai *dai;
685 int playback = SNDRV_PCM_STREAM_PLAYBACK;
687 if (rtd->dai_link->ignore_suspend)
690 for_each_rtd_codec_dais(rtd, i, dai) {
691 if (snd_soc_dai_stream_active(dai, playback))
692 snd_soc_dai_digital_mute(dai, 0, playback);
696 snd_soc_card_resume_post(card);
698 dev_dbg(card->dev, "ASoC: resume work completed\n");
700 /* Recheck all endpoints too, their state is affected by suspend */
701 dapm_mark_endpoints_dirty(card);
702 snd_soc_dapm_sync(&card->dapm);
704 /* userspace can access us now we are back as we were before */
705 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
708 /* powers up audio subsystem after a suspend */
709 int snd_soc_resume(struct device *dev)
711 struct snd_soc_card *card = dev_get_drvdata(dev);
712 struct snd_soc_component *component;
714 /* If the card is not initialized yet there is nothing to do */
715 if (!card->instantiated)
718 /* activate pins from sleep state */
719 for_each_card_components(card, component)
720 if (snd_soc_component_active(component))
721 pinctrl_pm_select_default_state(component->dev);
723 dev_dbg(dev, "ASoC: Scheduling resume work\n");
724 if (!schedule_work(&card->deferred_resume_work))
725 dev_err(dev, "ASoC: resume work item may be lost\n");
729 EXPORT_SYMBOL_GPL(snd_soc_resume);
731 static void soc_resume_init(struct snd_soc_card *card)
733 /* deferred resume work */
734 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
737 #define snd_soc_suspend NULL
738 #define snd_soc_resume NULL
739 static inline void soc_resume_init(struct snd_soc_card *card)
744 static struct device_node
745 *soc_component_to_node(struct snd_soc_component *component)
747 struct device_node *of_node;
749 of_node = component->dev->of_node;
750 if (!of_node && component->dev->parent)
751 of_node = component->dev->parent->of_node;
756 static int snd_soc_is_matching_component(
757 const struct snd_soc_dai_link_component *dlc,
758 struct snd_soc_component *component)
760 struct device_node *component_of_node;
765 component_of_node = soc_component_to_node(component);
767 if (dlc->of_node && component_of_node != dlc->of_node)
769 if (dlc->name && strcmp(component->name, dlc->name))
775 static struct snd_soc_component *soc_find_component(
776 const struct snd_soc_dai_link_component *dlc)
778 struct snd_soc_component *component;
780 lockdep_assert_held(&client_mutex);
785 * It returns *1st* found component, but some driver
786 * has few components by same of_node/name
788 * CPU component and generic DMAEngine component
790 for_each_component(component)
791 if (snd_soc_is_matching_component(dlc, component))
798 * snd_soc_find_dai - Find a registered DAI
800 * @dlc: name of the DAI or the DAI driver and optional component info to match
802 * This function will search all registered components and their DAIs to
803 * find the DAI of the same name. The component's of_node and name
804 * should also match if being specified.
806 * Return: pointer of DAI, or NULL if not found.
808 struct snd_soc_dai *snd_soc_find_dai(
809 const struct snd_soc_dai_link_component *dlc)
811 struct snd_soc_component *component;
812 struct snd_soc_dai *dai;
814 lockdep_assert_held(&client_mutex);
816 /* Find CPU DAI from registered DAIs */
817 for_each_component(component) {
818 if (!snd_soc_is_matching_component(dlc, component))
820 for_each_component_dais(component, dai) {
821 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
822 && (!dai->driver->name
823 || strcmp(dai->driver->name, dlc->dai_name)))
832 EXPORT_SYMBOL_GPL(snd_soc_find_dai);
834 static int soc_dai_link_sanity_check(struct snd_soc_card *card,
835 struct snd_soc_dai_link *link)
838 struct snd_soc_dai_link_component *cpu, *codec, *platform;
840 for_each_link_codecs(link, i, codec) {
842 * Codec must be specified by 1 of name or OF node,
843 * not both or neither.
845 if (!!codec->name == !!codec->of_node) {
846 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
851 /* Codec DAI name must be specified */
852 if (!codec->dai_name) {
853 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
859 * Defer card registration if codec component is not added to
862 if (!soc_find_component(codec)) {
864 "ASoC: codec component %s not found for link %s\n",
865 codec->name, link->name);
866 return -EPROBE_DEFER;
870 for_each_link_platforms(link, i, platform) {
872 * Platform may be specified by either name or OF node, but it
873 * can be left unspecified, then no components will be inserted
876 if (!!platform->name == !!platform->of_node) {
878 "ASoC: Neither/both platform name/of_node are set for %s\n",
884 * Defer card registration if platform component is not added to
887 if (!soc_find_component(platform)) {
889 "ASoC: platform component %s not found for link %s\n",
890 platform->name, link->name);
891 return -EPROBE_DEFER;
895 for_each_link_cpus(link, i, cpu) {
897 * CPU device may be specified by either name or OF node, but
898 * can be left unspecified, and will be matched based on DAI
901 if (cpu->name && cpu->of_node) {
903 "ASoC: Neither/both cpu name/of_node are set for %s\n",
909 * Defer card registration if cpu dai component is not added to
912 if ((cpu->of_node || cpu->name) &&
913 !soc_find_component(cpu)) {
915 "ASoC: cpu component %s not found for link %s\n",
916 cpu->name, link->name);
917 return -EPROBE_DEFER;
921 * At least one of CPU DAI name or CPU device name/node must be
924 if (!cpu->dai_name &&
925 !(cpu->name || cpu->of_node)) {
927 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
937 * snd_soc_remove_pcm_runtime - Remove a pcm_runtime from card
938 * @card: The ASoC card to which the pcm_runtime has
939 * @rtd: The pcm_runtime to remove
941 * This function removes a pcm_runtime from the ASoC card.
943 void snd_soc_remove_pcm_runtime(struct snd_soc_card *card,
944 struct snd_soc_pcm_runtime *rtd)
946 lockdep_assert_held(&client_mutex);
949 * Notify the machine driver for extra destruction
951 snd_soc_card_remove_dai_link(card, rtd->dai_link);
953 soc_free_pcm_runtime(rtd);
955 EXPORT_SYMBOL_GPL(snd_soc_remove_pcm_runtime);
958 * snd_soc_add_pcm_runtime - Add a pcm_runtime dynamically via dai_link
959 * @card: The ASoC card to which the pcm_runtime is added
960 * @dai_link: The DAI link to find pcm_runtime
962 * This function adds a pcm_runtime ASoC card by using dai_link.
964 * Note: Topology can use this API to add pcm_runtime when probing the
965 * topology component. And machine drivers can still define static
966 * DAI links in dai_link array.
968 int snd_soc_add_pcm_runtime(struct snd_soc_card *card,
969 struct snd_soc_dai_link *dai_link)
971 struct snd_soc_pcm_runtime *rtd;
972 struct snd_soc_dai_link_component *codec, *platform, *cpu;
973 struct snd_soc_component *component;
976 lockdep_assert_held(&client_mutex);
979 * Notify the machine driver for extra initialization
981 ret = snd_soc_card_add_dai_link(card, dai_link);
985 if (dai_link->ignore)
988 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
990 ret = soc_dai_link_sanity_check(card, dai_link);
994 rtd = soc_new_pcm_runtime(card, dai_link);
998 for_each_link_cpus(dai_link, i, cpu) {
999 asoc_rtd_to_cpu(rtd, i) = snd_soc_find_dai(cpu);
1000 if (!asoc_rtd_to_cpu(rtd, i)) {
1001 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
1005 snd_soc_rtd_add_component(rtd, asoc_rtd_to_cpu(rtd, i)->component);
1008 /* Find CODEC from registered CODECs */
1009 for_each_link_codecs(dai_link, i, codec) {
1010 asoc_rtd_to_codec(rtd, i) = snd_soc_find_dai(codec);
1011 if (!asoc_rtd_to_codec(rtd, i)) {
1012 dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
1017 snd_soc_rtd_add_component(rtd, asoc_rtd_to_codec(rtd, i)->component);
1020 /* Find PLATFORM from registered PLATFORMs */
1021 for_each_link_platforms(dai_link, i, platform) {
1022 for_each_component(component) {
1023 if (!snd_soc_is_matching_component(platform, component))
1026 snd_soc_rtd_add_component(rtd, component);
1033 snd_soc_remove_pcm_runtime(card, rtd);
1034 return -EPROBE_DEFER;
1036 EXPORT_SYMBOL_GPL(snd_soc_add_pcm_runtime);
1038 static int soc_init_pcm_runtime(struct snd_soc_card *card,
1039 struct snd_soc_pcm_runtime *rtd)
1041 struct snd_soc_dai_link *dai_link = rtd->dai_link;
1042 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
1043 struct snd_soc_component *component;
1046 /* set default power off timeout */
1047 rtd->pmdown_time = pmdown_time;
1049 /* do machine specific initialization */
1050 ret = snd_soc_link_init(rtd);
1054 if (dai_link->dai_fmt) {
1055 ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1060 /* add DPCM sysfs entries */
1061 soc_dpcm_debugfs_add(rtd);
1066 * most drivers will register their PCMs using DAI link ordering but
1067 * topology based drivers can use the DAI link id field to set PCM
1068 * device number and then use rtd + a base offset of the BEs.
1070 for_each_rtd_components(rtd, i, component) {
1071 if (!component->driver->use_dai_pcm_id)
1074 if (rtd->dai_link->no_pcm)
1075 num += component->driver->be_pcm_base;
1077 num = rtd->dai_link->id;
1080 /* create compress_device if possible */
1081 ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1082 if (ret != -ENOTSUPP) {
1084 dev_err(card->dev, "ASoC: can't create compress %s\n",
1085 dai_link->stream_name);
1089 /* create the pcm */
1090 ret = soc_new_pcm(rtd, num);
1092 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1093 dai_link->stream_name, ret);
1097 return snd_soc_pcm_dai_new(rtd);
1100 static void soc_set_name_prefix(struct snd_soc_card *card,
1101 struct snd_soc_component *component)
1103 struct device_node *of_node = soc_component_to_node(component);
1107 for (i = 0; i < card->num_configs; i++) {
1108 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1110 if (snd_soc_is_matching_component(&map->dlc, component)) {
1111 component->name_prefix = map->name_prefix;
1117 * If there is no configuration table or no match in the table,
1118 * check if a prefix is provided in the node
1120 ret = of_property_read_string(of_node, "sound-name-prefix", &str);
1124 component->name_prefix = str;
1127 static void soc_remove_component(struct snd_soc_component *component,
1131 if (!component->card)
1135 snd_soc_component_remove(component);
1137 /* For framework level robustness */
1138 snd_soc_component_set_jack(component, NULL, NULL);
1140 list_del_init(&component->card_list);
1141 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1142 soc_cleanup_component_debugfs(component);
1143 component->card = NULL;
1144 snd_soc_component_module_put_when_remove(component);
1147 static int soc_probe_component(struct snd_soc_card *card,
1148 struct snd_soc_component *component)
1150 struct snd_soc_dapm_context *dapm =
1151 snd_soc_component_get_dapm(component);
1152 struct snd_soc_dai *dai;
1156 if (!strcmp(component->name, "snd-soc-dummy"))
1159 if (component->card) {
1160 if (component->card != card) {
1161 dev_err(component->dev,
1162 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1163 card->name, component->card->name);
1169 ret = snd_soc_component_module_get_when_probe(component);
1173 component->card = card;
1174 soc_set_name_prefix(card, component);
1176 soc_init_component_debugfs(component);
1178 snd_soc_dapm_init(dapm, card, component);
1180 ret = snd_soc_dapm_new_controls(dapm,
1181 component->driver->dapm_widgets,
1182 component->driver->num_dapm_widgets);
1185 dev_err(component->dev,
1186 "Failed to create new controls %d\n", ret);
1190 for_each_component_dais(component, dai) {
1191 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1193 dev_err(component->dev,
1194 "Failed to create DAI widgets %d\n", ret);
1199 ret = snd_soc_component_probe(component);
1201 dev_err(component->dev,
1202 "ASoC: failed to probe component %d\n", ret);
1205 WARN(dapm->idle_bias_off &&
1206 dapm->bias_level != SND_SOC_BIAS_OFF,
1207 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1211 /* machine specific init */
1212 if (component->init) {
1213 ret = component->init(component);
1215 dev_err(component->dev,
1216 "Failed to do machine specific init %d\n", ret);
1221 ret = snd_soc_add_component_controls(component,
1222 component->driver->controls,
1223 component->driver->num_controls);
1227 ret = snd_soc_dapm_add_routes(dapm,
1228 component->driver->dapm_routes,
1229 component->driver->num_dapm_routes);
1231 if (card->disable_route_checks) {
1233 "%s: disable_route_checks set, ignoring errors on add_routes\n",
1237 "%s: snd_soc_dapm_add_routes failed: %d\n",
1243 /* see for_each_card_components */
1244 list_add(&component->card_list, &card->component_dev_list);
1248 soc_remove_component(component, probed);
1253 static void soc_remove_link_dais(struct snd_soc_card *card)
1255 struct snd_soc_pcm_runtime *rtd;
1258 for_each_comp_order(order) {
1259 for_each_card_rtds(card, rtd) {
1260 /* remove all rtd connected DAIs in good order */
1261 snd_soc_pcm_dai_remove(rtd, order);
1266 static int soc_probe_link_dais(struct snd_soc_card *card)
1268 struct snd_soc_pcm_runtime *rtd;
1271 for_each_comp_order(order) {
1272 for_each_card_rtds(card, rtd) {
1275 "ASoC: probe %s dai link %d late %d\n",
1276 card->name, rtd->num, order);
1278 /* probe all rtd connected DAIs in good order */
1279 ret = snd_soc_pcm_dai_probe(rtd, order);
1288 static void soc_remove_link_components(struct snd_soc_card *card)
1290 struct snd_soc_component *component;
1291 struct snd_soc_pcm_runtime *rtd;
1294 for_each_comp_order(order) {
1295 for_each_card_rtds(card, rtd) {
1296 for_each_rtd_components(rtd, i, component) {
1297 if (component->driver->remove_order != order)
1300 soc_remove_component(component, 1);
1306 static int soc_probe_link_components(struct snd_soc_card *card)
1308 struct snd_soc_component *component;
1309 struct snd_soc_pcm_runtime *rtd;
1312 for_each_comp_order(order) {
1313 for_each_card_rtds(card, rtd) {
1314 for_each_rtd_components(rtd, i, component) {
1315 if (component->driver->probe_order != order)
1318 ret = soc_probe_component(card, component);
1328 static void soc_unbind_aux_dev(struct snd_soc_card *card)
1330 struct snd_soc_component *component, *_component;
1332 for_each_card_auxs_safe(card, component, _component) {
1333 component->init = NULL;
1334 list_del(&component->card_aux_list);
1338 static int soc_bind_aux_dev(struct snd_soc_card *card)
1340 struct snd_soc_component *component;
1341 struct snd_soc_aux_dev *aux;
1344 for_each_card_pre_auxs(card, i, aux) {
1345 /* codecs, usually analog devices */
1346 component = soc_find_component(&aux->dlc);
1348 return -EPROBE_DEFER;
1350 component->init = aux->init;
1351 /* see for_each_card_auxs */
1352 list_add(&component->card_aux_list, &card->aux_comp_list);
1357 static int soc_probe_aux_devices(struct snd_soc_card *card)
1359 struct snd_soc_component *component;
1363 for_each_comp_order(order) {
1364 for_each_card_auxs(card, component) {
1365 if (component->driver->probe_order != order)
1368 ret = soc_probe_component(card, component);
1377 static void soc_remove_aux_devices(struct snd_soc_card *card)
1379 struct snd_soc_component *comp, *_comp;
1382 for_each_comp_order(order) {
1383 for_each_card_auxs_safe(card, comp, _comp) {
1384 if (comp->driver->remove_order == order)
1385 soc_remove_component(comp, 1);
1391 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1392 * @rtd: The runtime for which the DAI link format should be changed
1393 * @dai_fmt: The new DAI link format
1395 * This function updates the DAI link format for all DAIs connected to the DAI
1396 * link for the specified runtime.
1398 * Note: For setups with a static format set the dai_fmt field in the
1399 * corresponding snd_dai_link struct instead of using this function.
1401 * Returns 0 on success, otherwise a negative error code.
1403 int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1404 unsigned int dai_fmt)
1406 struct snd_soc_dai *cpu_dai;
1407 struct snd_soc_dai *codec_dai;
1408 unsigned int inv_dai_fmt;
1412 for_each_rtd_codec_dais(rtd, i, codec_dai) {
1413 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1414 if (ret != 0 && ret != -ENOTSUPP) {
1415 dev_warn(codec_dai->dev,
1416 "ASoC: Failed to set DAI format: %d\n", ret);
1422 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1423 * the component which has non_legacy_dai_naming is Codec
1425 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1426 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1427 case SND_SOC_DAIFMT_CBM_CFM:
1428 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1430 case SND_SOC_DAIFMT_CBM_CFS:
1431 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1433 case SND_SOC_DAIFMT_CBS_CFM:
1434 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1436 case SND_SOC_DAIFMT_CBS_CFS:
1437 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1440 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1441 unsigned int fmt = dai_fmt;
1443 if (cpu_dai->component->driver->non_legacy_dai_naming)
1446 ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
1447 if (ret != 0 && ret != -ENOTSUPP) {
1448 dev_warn(cpu_dai->dev,
1449 "ASoC: Failed to set DAI format: %d\n", ret);
1456 EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1460 * If a DMI filed contain strings in this blacklist (e.g.
1461 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
1462 * as invalid and dropped when setting the card long name from DMI info.
1464 static const char * const dmi_blacklist[] = {
1465 "To be filled by OEM",
1468 "Board Manufacturer",
1469 "Board Vendor Name",
1470 "Board Product Name",
1471 NULL, /* terminator */
1475 * Trim special characters, and replace '-' with '_' since '-' is used to
1476 * separate different DMI fields in the card long name. Only number and
1477 * alphabet characters and a few separator characters are kept.
1479 static void cleanup_dmi_name(char *name)
1483 for (i = 0; name[i]; i++) {
1484 if (isalnum(name[i]) || (name[i] == '.')
1485 || (name[i] == '_'))
1486 name[j++] = name[i];
1487 else if (name[i] == '-')
1495 * Check if a DMI field is valid, i.e. not containing any string
1496 * in the black list.
1498 static int is_dmi_valid(const char *field)
1502 while (dmi_blacklist[i]) {
1503 if (strstr(field, dmi_blacklist[i]))
1512 * Append a string to card->dmi_longname with character cleanups.
1514 static void append_dmi_string(struct snd_soc_card *card, const char *str)
1516 char *dst = card->dmi_longname;
1517 size_t dst_len = sizeof(card->dmi_longname);
1521 snprintf(dst + len, dst_len - len, "-%s", str);
1523 len++; /* skip the separator "-" */
1525 cleanup_dmi_name(dst + len);
1529 * snd_soc_set_dmi_name() - Register DMI names to card
1530 * @card: The card to register DMI names
1531 * @flavour: The flavour "differentiator" for the card amongst its peers.
1533 * An Intel machine driver may be used by many different devices but are
1534 * difficult for userspace to differentiate, since machine drivers ususally
1535 * use their own name as the card short name and leave the card long name
1536 * blank. To differentiate such devices and fix bugs due to lack of
1537 * device-specific configurations, this function allows DMI info to be used
1538 * as the sound card long name, in the format of
1539 * "vendor-product-version-board"
1540 * (Character '-' is used to separate different DMI fields here).
1541 * This will help the user space to load the device-specific Use Case Manager
1542 * (UCM) configurations for the card.
1544 * Possible card long names may be:
1545 * DellInc.-XPS139343-01-0310JH
1546 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1547 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1549 * This function also supports flavoring the card longname to provide
1550 * the extra differentiation, like "vendor-product-version-board-flavor".
1552 * We only keep number and alphabet characters and a few separator characters
1553 * in the card long name since UCM in the user space uses the card long names
1554 * as card configuration directory names and AudoConf cannot support special
1555 * charactors like SPACE.
1557 * Returns 0 on success, otherwise a negative error code.
1559 int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1561 const char *vendor, *product, *product_version, *board;
1563 if (card->long_name)
1564 return 0; /* long name already set by driver or from DMI */
1566 /* make up dmi long name as: vendor-product-version-board */
1567 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1568 if (!vendor || !is_dmi_valid(vendor)) {
1569 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1573 snprintf(card->dmi_longname, sizeof(card->dmi_longname), "%s", vendor);
1574 cleanup_dmi_name(card->dmi_longname);
1576 product = dmi_get_system_info(DMI_PRODUCT_NAME);
1577 if (product && is_dmi_valid(product)) {
1578 append_dmi_string(card, product);
1581 * some vendors like Lenovo may only put a self-explanatory
1582 * name in the product version field
1584 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
1585 if (product_version && is_dmi_valid(product_version))
1586 append_dmi_string(card, product_version);
1589 board = dmi_get_system_info(DMI_BOARD_NAME);
1590 if (board && is_dmi_valid(board)) {
1591 if (!product || strcasecmp(board, product))
1592 append_dmi_string(card, board);
1593 } else if (!product) {
1594 /* fall back to using legacy name */
1595 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1599 /* Add flavour to dmi long name */
1601 append_dmi_string(card, flavour);
1603 /* set the card long name */
1604 card->long_name = card->dmi_longname;
1608 EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
1609 #endif /* CONFIG_DMI */
1611 static void soc_check_tplg_fes(struct snd_soc_card *card)
1613 struct snd_soc_component *component;
1614 const struct snd_soc_component_driver *comp_drv;
1615 struct snd_soc_dai_link *dai_link;
1618 for_each_component(component) {
1620 /* does this component override BEs ? */
1621 if (!component->driver->ignore_machine)
1624 /* for this machine ? */
1625 if (!strcmp(component->driver->ignore_machine,
1626 card->dev->driver->name))
1628 if (strcmp(component->driver->ignore_machine,
1629 dev_name(card->dev)))
1632 /* machine matches, so override the rtd data */
1633 for_each_card_prelinks(card, i, dai_link) {
1635 /* ignore this FE */
1636 if (dai_link->dynamic) {
1637 dai_link->ignore = true;
1641 dev_info(card->dev, "info: override BE DAI link %s\n",
1642 card->dai_link[i].name);
1644 /* override platform component */
1645 if (!dai_link->platforms) {
1646 dev_err(card->dev, "init platform error");
1649 dai_link->platforms->name = component->name;
1651 /* convert non BE into BE */
1652 if (!dai_link->no_pcm) {
1653 dai_link->no_pcm = 1;
1655 if (dai_link->dpcm_playback)
1657 "invalid configuration, dailink %s has flags no_pcm=0 and dpcm_playback=1\n",
1659 if (dai_link->dpcm_capture)
1661 "invalid configuration, dailink %s has flags no_pcm=0 and dpcm_capture=1\n",
1664 /* convert normal link into DPCM one */
1665 if (!(dai_link->dpcm_playback ||
1666 dai_link->dpcm_capture)) {
1667 dai_link->dpcm_playback = !dai_link->capture_only;
1668 dai_link->dpcm_capture = !dai_link->playback_only;
1673 * override any BE fixups
1675 * snd_soc_link_be_hw_params_fixup()
1677 dai_link->be_hw_params_fixup =
1678 component->driver->be_hw_params_fixup;
1681 * most BE links don't set stream name, so set it to
1682 * dai link name if it's NULL to help bind widgets.
1684 if (!dai_link->stream_name)
1685 dai_link->stream_name = dai_link->name;
1688 /* Inform userspace we are using alternate topology */
1689 if (component->driver->topology_name_prefix) {
1691 /* topology shortname created? */
1692 if (!card->topology_shortname_created) {
1693 comp_drv = component->driver;
1695 snprintf(card->topology_shortname, 32, "%s-%s",
1696 comp_drv->topology_name_prefix,
1698 card->topology_shortname_created = true;
1701 /* use topology shortname */
1702 card->name = card->topology_shortname;
1707 #define soc_setup_card_name(name, name1, name2, norm) \
1708 __soc_setup_card_name(name, sizeof(name), name1, name2, norm)
1709 static void __soc_setup_card_name(char *name, int len,
1710 const char *name1, const char *name2,
1715 snprintf(name, len, "%s", name1 ? name1 : name2);
1721 * Name normalization
1723 * The driver name is somewhat special, as it's used as a key for
1724 * searches in the user-space.
1727 * "abcd??efg" -> "abcd__efg"
1729 for (i = 0; i < len; i++) {
1736 if (!isalnum(name[i]))
1743 static void soc_cleanup_card_resources(struct snd_soc_card *card)
1745 struct snd_soc_pcm_runtime *rtd, *n;
1748 snd_card_disconnect_sync(card->snd_card);
1750 snd_soc_dapm_shutdown(card);
1752 /* remove and free each DAI */
1753 soc_remove_link_dais(card);
1754 soc_remove_link_components(card);
1756 for_each_card_rtds_safe(card, rtd, n)
1757 snd_soc_remove_pcm_runtime(card, rtd);
1759 /* remove auxiliary devices */
1760 soc_remove_aux_devices(card);
1761 soc_unbind_aux_dev(card);
1763 snd_soc_dapm_free(&card->dapm);
1764 soc_cleanup_card_debugfs(card);
1766 /* remove the card */
1767 snd_soc_card_remove(card);
1769 if (card->snd_card) {
1770 snd_card_free(card->snd_card);
1771 card->snd_card = NULL;
1775 static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
1777 if (card->instantiated) {
1778 card->instantiated = false;
1779 snd_soc_flush_all_delayed_work(card);
1781 soc_cleanup_card_resources(card);
1783 list_add(&card->list, &unbind_card_list);
1786 list_del(&card->list);
1790 static int snd_soc_bind_card(struct snd_soc_card *card)
1792 struct snd_soc_pcm_runtime *rtd;
1793 struct snd_soc_component *component;
1794 struct snd_soc_dai_link *dai_link;
1797 mutex_lock(&client_mutex);
1798 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1800 snd_soc_dapm_init(&card->dapm, card, NULL);
1802 /* check whether any platform is ignore machine FE and using topology */
1803 soc_check_tplg_fes(card);
1805 /* bind aux_devs too */
1806 ret = soc_bind_aux_dev(card);
1810 /* add predefined DAI links to the list */
1812 for_each_card_prelinks(card, i, dai_link) {
1813 ret = snd_soc_add_pcm_runtime(card, dai_link);
1818 /* card bind complete so register a sound card */
1819 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1820 card->owner, 0, &card->snd_card);
1823 "ASoC: can't create sound card for card %s: %d\n",
1828 soc_init_card_debugfs(card);
1830 soc_resume_init(card);
1832 ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1833 card->num_dapm_widgets);
1837 ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1838 card->num_of_dapm_widgets);
1842 /* initialise the sound card only once */
1843 ret = snd_soc_card_probe(card);
1847 /* probe all components used by DAI links on this card */
1848 ret = soc_probe_link_components(card);
1851 "ASoC: failed to instantiate card %d\n", ret);
1855 /* probe auxiliary components */
1856 ret = soc_probe_aux_devices(card);
1859 "ASoC: failed to probe aux component %d\n", ret);
1863 /* probe all DAI links on this card */
1864 ret = soc_probe_link_dais(card);
1867 "ASoC: failed to instantiate card %d\n", ret);
1871 for_each_card_rtds(card, rtd) {
1872 ret = soc_init_pcm_runtime(card, rtd);
1877 snd_soc_dapm_link_dai_widgets(card);
1878 snd_soc_dapm_connect_dai_link_widgets(card);
1880 ret = snd_soc_add_card_controls(card, card->controls,
1881 card->num_controls);
1885 ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1886 card->num_dapm_routes);
1888 if (card->disable_route_checks) {
1890 "%s: disable_route_checks set, ignoring errors on add_routes\n",
1894 "%s: snd_soc_dapm_add_routes failed: %d\n",
1900 ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
1901 card->num_of_dapm_routes);
1905 /* try to set some sane longname if DMI is available */
1906 snd_soc_set_dmi_name(card, NULL);
1908 soc_setup_card_name(card->snd_card->shortname,
1909 card->name, NULL, 0);
1910 soc_setup_card_name(card->snd_card->longname,
1911 card->long_name, card->name, 0);
1912 soc_setup_card_name(card->snd_card->driver,
1913 card->driver_name, card->name, 1);
1915 if (card->components) {
1916 /* the current implementation of snd_component_add() accepts */
1917 /* multiple components in the string separated by space, */
1918 /* but the string collision (identical string) check might */
1919 /* not work correctly */
1920 ret = snd_component_add(card->snd_card, card->components);
1922 dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n",
1928 ret = snd_soc_card_late_probe(card);
1932 snd_soc_dapm_new_widgets(card);
1934 ret = snd_card_register(card->snd_card);
1936 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1941 card->instantiated = 1;
1942 dapm_mark_endpoints_dirty(card);
1943 snd_soc_dapm_sync(&card->dapm);
1945 /* deactivate pins to sleep state */
1946 for_each_card_components(card, component)
1947 if (!snd_soc_component_active(component))
1948 pinctrl_pm_select_sleep_state(component->dev);
1952 soc_cleanup_card_resources(card);
1954 mutex_unlock(&card->mutex);
1955 mutex_unlock(&client_mutex);
1960 /* probes a new socdev */
1961 static int soc_probe(struct platform_device *pdev)
1963 struct snd_soc_card *card = platform_get_drvdata(pdev);
1966 * no card, so machine driver should be registering card
1967 * we should not be here in that case so ret error
1972 dev_warn(&pdev->dev,
1973 "ASoC: machine %s should use snd_soc_register_card()\n",
1976 /* Bodge while we unpick instantiation */
1977 card->dev = &pdev->dev;
1979 return snd_soc_register_card(card);
1982 /* removes a socdev */
1983 static int soc_remove(struct platform_device *pdev)
1985 struct snd_soc_card *card = platform_get_drvdata(pdev);
1987 snd_soc_unregister_card(card);
1991 int snd_soc_poweroff(struct device *dev)
1993 struct snd_soc_card *card = dev_get_drvdata(dev);
1994 struct snd_soc_component *component;
1996 if (!card->instantiated)
2000 * Flush out pmdown_time work - we actually do want to run it
2001 * now, we're shutting down so no imminent restart.
2003 snd_soc_flush_all_delayed_work(card);
2005 snd_soc_dapm_shutdown(card);
2007 /* deactivate pins to sleep state */
2008 for_each_card_components(card, component)
2009 pinctrl_pm_select_sleep_state(component->dev);
2013 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2015 const struct dev_pm_ops snd_soc_pm_ops = {
2016 .suspend = snd_soc_suspend,
2017 .resume = snd_soc_resume,
2018 .freeze = snd_soc_suspend,
2019 .thaw = snd_soc_resume,
2020 .poweroff = snd_soc_poweroff,
2021 .restore = snd_soc_resume,
2023 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2025 /* ASoC platform driver */
2026 static struct platform_driver soc_driver = {
2028 .name = "soc-audio",
2029 .pm = &snd_soc_pm_ops,
2032 .remove = soc_remove,
2036 * snd_soc_cnew - create new control
2037 * @_template: control template
2038 * @data: control private data
2039 * @long_name: control long name
2040 * @prefix: control name prefix
2042 * Create a new mixer control from a template control.
2044 * Returns 0 for success, else error.
2046 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2047 void *data, const char *long_name,
2050 struct snd_kcontrol_new template;
2051 struct snd_kcontrol *kcontrol;
2054 memcpy(&template, _template, sizeof(template));
2058 long_name = template.name;
2061 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2065 template.name = name;
2067 template.name = long_name;
2070 kcontrol = snd_ctl_new1(&template, data);
2076 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2078 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2079 const struct snd_kcontrol_new *controls, int num_controls,
2080 const char *prefix, void *data)
2084 for (i = 0; i < num_controls; i++) {
2085 const struct snd_kcontrol_new *control = &controls[i];
2087 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2088 control->name, prefix));
2090 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2091 control->name, err);
2100 * snd_soc_add_component_controls - Add an array of controls to a component.
2102 * @component: Component to add controls to
2103 * @controls: Array of controls to add
2104 * @num_controls: Number of elements in the array
2106 * Return: 0 for success, else error.
2108 int snd_soc_add_component_controls(struct snd_soc_component *component,
2109 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2111 struct snd_card *card = component->card->snd_card;
2113 return snd_soc_add_controls(card, component->dev, controls,
2114 num_controls, component->name_prefix, component);
2116 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2119 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2120 * Convenience function to add a list of controls.
2122 * @soc_card: SoC card to add controls to
2123 * @controls: array of controls to add
2124 * @num_controls: number of elements in the array
2126 * Return 0 for success, else error.
2128 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2129 const struct snd_kcontrol_new *controls, int num_controls)
2131 struct snd_card *card = soc_card->snd_card;
2133 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2136 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2139 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2140 * Convienience function to add a list of controls.
2142 * @dai: DAI to add controls to
2143 * @controls: array of controls to add
2144 * @num_controls: number of elements in the array
2146 * Return 0 for success, else error.
2148 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2149 const struct snd_kcontrol_new *controls, int num_controls)
2151 struct snd_card *card = dai->component->card->snd_card;
2153 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2156 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2159 * snd_soc_register_card - Register a card with the ASoC core
2161 * @card: Card to register
2164 int snd_soc_register_card(struct snd_soc_card *card)
2166 if (!card->name || !card->dev)
2169 dev_set_drvdata(card->dev, card);
2171 INIT_LIST_HEAD(&card->widgets);
2172 INIT_LIST_HEAD(&card->paths);
2173 INIT_LIST_HEAD(&card->dapm_list);
2174 INIT_LIST_HEAD(&card->aux_comp_list);
2175 INIT_LIST_HEAD(&card->component_dev_list);
2176 INIT_LIST_HEAD(&card->list);
2177 INIT_LIST_HEAD(&card->rtd_list);
2178 INIT_LIST_HEAD(&card->dapm_dirty);
2179 INIT_LIST_HEAD(&card->dobj_list);
2181 card->instantiated = 0;
2182 mutex_init(&card->mutex);
2183 mutex_init(&card->dapm_mutex);
2184 mutex_init(&card->pcm_mutex);
2185 spin_lock_init(&card->dpcm_lock);
2187 return snd_soc_bind_card(card);
2189 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2192 * snd_soc_unregister_card - Unregister a card with the ASoC core
2194 * @card: Card to unregister
2197 int snd_soc_unregister_card(struct snd_soc_card *card)
2199 mutex_lock(&client_mutex);
2200 snd_soc_unbind_card(card, true);
2201 mutex_unlock(&client_mutex);
2202 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2206 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2209 * Simplify DAI link configuration by removing ".-1" from device names
2210 * and sanitizing names.
2212 static char *fmt_single_name(struct device *dev, int *id)
2214 char *found, name[NAME_SIZE];
2217 if (dev_name(dev) == NULL)
2220 strlcpy(name, dev_name(dev), NAME_SIZE);
2222 /* are we a "%s.%d" name (platform and SPI components) */
2223 found = strstr(name, dev->driver->name);
2226 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2228 /* discard ID from name if ID == -1 */
2230 found[strlen(dev->driver->name)] = '\0';
2234 /* I2C component devices are named "bus-addr" */
2235 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2236 char tmp[NAME_SIZE];
2238 /* create unique ID number from I2C addr and bus */
2239 *id = ((id1 & 0xffff) << 16) + id2;
2241 /* sanitize component name for DAI link creation */
2242 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2244 strlcpy(name, tmp, NAME_SIZE);
2249 return devm_kstrdup(dev, name, GFP_KERNEL);
2253 * Simplify DAI link naming for single devices with multiple DAIs by removing
2254 * any ".-1" and using the DAI name (instead of device name).
2256 static inline char *fmt_multiple_name(struct device *dev,
2257 struct snd_soc_dai_driver *dai_drv)
2259 if (dai_drv->name == NULL) {
2261 "ASoC: error - multiple DAI %s registered with no name\n",
2266 return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2269 void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2271 dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2272 list_del(&dai->list);
2274 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2277 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2279 * @component: The component the DAIs are registered for
2280 * @dai_drv: DAI driver to use for the DAI
2281 * @legacy_dai_naming: if %true, use legacy single-name format;
2282 * if %false, use multiple-name format;
2284 * Topology can use this API to register DAIs when probing a component.
2285 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2286 * will be freed in the component cleanup.
2288 struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
2289 struct snd_soc_dai_driver *dai_drv,
2290 bool legacy_dai_naming)
2292 struct device *dev = component->dev;
2293 struct snd_soc_dai *dai;
2295 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2297 lockdep_assert_held(&client_mutex);
2299 dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
2304 * Back in the old days when we still had component-less DAIs,
2305 * instead of having a static name, component-less DAIs would
2306 * inherit the name of the parent device so it is possible to
2307 * register multiple instances of the DAI. We still need to keep
2308 * the same naming style even though those DAIs are not
2309 * component-less anymore.
2311 if (legacy_dai_naming &&
2312 (dai_drv->id == 0 || dai_drv->name == NULL)) {
2313 dai->name = fmt_single_name(dev, &dai->id);
2315 dai->name = fmt_multiple_name(dev, dai_drv);
2317 dai->id = dai_drv->id;
2319 dai->id = component->num_dai;
2324 dai->component = component;
2326 dai->driver = dai_drv;
2328 /* see for_each_component_dais */
2329 list_add_tail(&dai->list, &component->dai_list);
2330 component->num_dai++;
2332 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2337 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2339 * @component: The component for which the DAIs should be unregistered
2341 static void snd_soc_unregister_dais(struct snd_soc_component *component)
2343 struct snd_soc_dai *dai, *_dai;
2345 for_each_component_dais_safe(component, dai, _dai)
2346 snd_soc_unregister_dai(dai);
2350 * snd_soc_register_dais - Register a DAI with the ASoC core
2352 * @component: The component the DAIs are registered for
2353 * @dai_drv: DAI driver to use for the DAIs
2354 * @count: Number of DAIs
2356 static int snd_soc_register_dais(struct snd_soc_component *component,
2357 struct snd_soc_dai_driver *dai_drv,
2360 struct snd_soc_dai *dai;
2364 for (i = 0; i < count; i++) {
2365 dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
2366 !component->driver->non_legacy_dai_naming);
2376 snd_soc_unregister_dais(component);
2381 static int snd_soc_component_initialize(struct snd_soc_component *component,
2382 const struct snd_soc_component_driver *driver, struct device *dev)
2384 INIT_LIST_HEAD(&component->dai_list);
2385 INIT_LIST_HEAD(&component->dobj_list);
2386 INIT_LIST_HEAD(&component->card_list);
2387 mutex_init(&component->io_mutex);
2389 component->name = fmt_single_name(dev, &component->id);
2390 if (!component->name) {
2391 dev_err(dev, "ASoC: Failed to allocate name\n");
2395 component->dev = dev;
2396 component->driver = driver;
2401 static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2403 int val_bytes = regmap_get_val_bytes(component->regmap);
2405 /* Errors are legitimate for non-integer byte multiples */
2407 component->val_bytes = val_bytes;
2410 #ifdef CONFIG_REGMAP
2413 * snd_soc_component_init_regmap() - Initialize regmap instance for the
2415 * @component: The component for which to initialize the regmap instance
2416 * @regmap: The regmap instance that should be used by the component
2418 * This function allows deferred assignment of the regmap instance that is
2419 * associated with the component. Only use this if the regmap instance is not
2420 * yet ready when the component is registered. The function must also be called
2421 * before the first IO attempt of the component.
2423 void snd_soc_component_init_regmap(struct snd_soc_component *component,
2424 struct regmap *regmap)
2426 component->regmap = regmap;
2427 snd_soc_component_setup_regmap(component);
2429 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
2432 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
2434 * @component: The component for which to de-initialize the regmap instance
2436 * Calls regmap_exit() on the regmap instance associated to the component and
2437 * removes the regmap instance from the component.
2439 * This function should only be used if snd_soc_component_init_regmap() was used
2440 * to initialize the regmap instance.
2442 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
2444 regmap_exit(component->regmap);
2445 component->regmap = NULL;
2447 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2451 #define ENDIANNESS_MAP(name) \
2452 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2453 static u64 endianness_format_map[] = {
2454 ENDIANNESS_MAP(S16_),
2455 ENDIANNESS_MAP(U16_),
2456 ENDIANNESS_MAP(S24_),
2457 ENDIANNESS_MAP(U24_),
2458 ENDIANNESS_MAP(S32_),
2459 ENDIANNESS_MAP(U32_),
2460 ENDIANNESS_MAP(S24_3),
2461 ENDIANNESS_MAP(U24_3),
2462 ENDIANNESS_MAP(S20_3),
2463 ENDIANNESS_MAP(U20_3),
2464 ENDIANNESS_MAP(S18_3),
2465 ENDIANNESS_MAP(U18_3),
2466 ENDIANNESS_MAP(FLOAT_),
2467 ENDIANNESS_MAP(FLOAT64_),
2468 ENDIANNESS_MAP(IEC958_SUBFRAME_),
2472 * Fix up the DAI formats for endianness: codecs don't actually see
2473 * the endianness of the data but we're using the CPU format
2474 * definitions which do need to include endianness so we ensure that
2475 * codec DAIs always have both big and little endian variants set.
2477 static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2481 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2482 if (stream->formats & endianness_format_map[i])
2483 stream->formats |= endianness_format_map[i];
2486 static void snd_soc_try_rebind_card(void)
2488 struct snd_soc_card *card, *c;
2490 list_for_each_entry_safe(card, c, &unbind_card_list, list)
2491 if (!snd_soc_bind_card(card))
2492 list_del(&card->list);
2495 static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2497 struct snd_soc_card *card = component->card;
2499 snd_soc_unregister_dais(component);
2502 snd_soc_unbind_card(card, false);
2504 list_del(&component->list);
2507 int snd_soc_add_component(struct device *dev,
2508 struct snd_soc_component *component,
2509 const struct snd_soc_component_driver *component_driver,
2510 struct snd_soc_dai_driver *dai_drv,
2516 mutex_lock(&client_mutex);
2518 ret = snd_soc_component_initialize(component, component_driver, dev);
2522 if (component_driver->endianness) {
2523 for (i = 0; i < num_dai; i++) {
2524 convert_endianness_formats(&dai_drv[i].playback);
2525 convert_endianness_formats(&dai_drv[i].capture);
2529 ret = snd_soc_register_dais(component, dai_drv, num_dai);
2531 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
2535 if (!component->driver->write && !component->driver->read) {
2536 if (!component->regmap)
2537 component->regmap = dev_get_regmap(component->dev,
2539 if (component->regmap)
2540 snd_soc_component_setup_regmap(component);
2543 /* see for_each_component */
2544 list_add(&component->list, &component_list);
2548 snd_soc_del_component_unlocked(component);
2550 mutex_unlock(&client_mutex);
2553 snd_soc_try_rebind_card();
2557 EXPORT_SYMBOL_GPL(snd_soc_add_component);
2559 int snd_soc_register_component(struct device *dev,
2560 const struct snd_soc_component_driver *component_driver,
2561 struct snd_soc_dai_driver *dai_drv,
2564 struct snd_soc_component *component;
2566 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
2570 return snd_soc_add_component(dev, component, component_driver,
2573 EXPORT_SYMBOL_GPL(snd_soc_register_component);
2576 * snd_soc_unregister_component - Unregister all related component
2577 * from the ASoC core
2579 * @dev: The device to unregister
2581 void snd_soc_unregister_component(struct device *dev)
2583 struct snd_soc_component *component;
2585 mutex_lock(&client_mutex);
2587 component = snd_soc_lookup_component_nolocked(dev, NULL);
2591 snd_soc_del_component_unlocked(component);
2593 mutex_unlock(&client_mutex);
2595 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2597 /* Retrieve a card's name from device tree */
2598 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2599 const char *propname)
2601 struct device_node *np;
2605 pr_err("card->dev is not set before calling %s\n", __func__);
2609 np = card->dev->of_node;
2611 ret = of_property_read_string_index(np, propname, 0, &card->name);
2613 * EINVAL means the property does not exist. This is fine providing
2614 * card->name was previously set, which is checked later in
2615 * snd_soc_register_card.
2617 if (ret < 0 && ret != -EINVAL) {
2619 "ASoC: Property '%s' could not be read: %d\n",
2626 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2628 static const struct snd_soc_dapm_widget simple_widgets[] = {
2629 SND_SOC_DAPM_MIC("Microphone", NULL),
2630 SND_SOC_DAPM_LINE("Line", NULL),
2631 SND_SOC_DAPM_HP("Headphone", NULL),
2632 SND_SOC_DAPM_SPK("Speaker", NULL),
2635 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
2636 const char *propname)
2638 struct device_node *np = card->dev->of_node;
2639 struct snd_soc_dapm_widget *widgets;
2640 const char *template, *wname;
2641 int i, j, num_widgets, ret;
2643 num_widgets = of_property_count_strings(np, propname);
2644 if (num_widgets < 0) {
2646 "ASoC: Property '%s' does not exist\n", propname);
2649 if (num_widgets & 1) {
2651 "ASoC: Property '%s' length is not even\n", propname);
2657 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
2662 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
2666 "ASoC: Could not allocate memory for widgets\n");
2670 for (i = 0; i < num_widgets; i++) {
2671 ret = of_property_read_string_index(np, propname,
2675 "ASoC: Property '%s' index %d read error:%d\n",
2676 propname, 2 * i, ret);
2680 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
2681 if (!strncmp(template, simple_widgets[j].name,
2682 strlen(simple_widgets[j].name))) {
2683 widgets[i] = simple_widgets[j];
2688 if (j >= ARRAY_SIZE(simple_widgets)) {
2690 "ASoC: DAPM widget '%s' is not supported\n",
2695 ret = of_property_read_string_index(np, propname,
2700 "ASoC: Property '%s' index %d read error:%d\n",
2701 propname, (2 * i) + 1, ret);
2705 widgets[i].name = wname;
2708 card->of_dapm_widgets = widgets;
2709 card->num_of_dapm_widgets = num_widgets;
2713 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
2715 int snd_soc_of_get_slot_mask(struct device_node *np,
2716 const char *prop_name,
2720 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
2726 for (i = 0; i < val; i++)
2727 if (be32_to_cpup(&of_slot_mask[i]))
2732 EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
2734 int snd_soc_of_parse_tdm_slot(struct device_node *np,
2735 unsigned int *tx_mask,
2736 unsigned int *rx_mask,
2737 unsigned int *slots,
2738 unsigned int *slot_width)
2744 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
2746 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
2748 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
2749 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
2757 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
2758 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
2768 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
2770 void snd_soc_of_parse_node_prefix(struct device_node *np,
2771 struct snd_soc_codec_conf *codec_conf,
2772 struct device_node *of_node,
2773 const char *propname)
2778 ret = of_property_read_string(np, propname, &str);
2780 /* no prefix is not error */
2784 codec_conf->dlc.of_node = of_node;
2785 codec_conf->name_prefix = str;
2787 EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
2789 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
2790 const char *propname)
2792 struct device_node *np = card->dev->of_node;
2794 struct snd_soc_dapm_route *routes;
2797 num_routes = of_property_count_strings(np, propname);
2798 if (num_routes < 0 || num_routes & 1) {
2800 "ASoC: Property '%s' does not exist or its length is not even\n",
2806 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
2811 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
2815 "ASoC: Could not allocate DAPM route table\n");
2819 for (i = 0; i < num_routes; i++) {
2820 ret = of_property_read_string_index(np, propname,
2821 2 * i, &routes[i].sink);
2824 "ASoC: Property '%s' index %d could not be read: %d\n",
2825 propname, 2 * i, ret);
2828 ret = of_property_read_string_index(np, propname,
2829 (2 * i) + 1, &routes[i].source);
2832 "ASoC: Property '%s' index %d could not be read: %d\n",
2833 propname, (2 * i) + 1, ret);
2838 card->num_of_dapm_routes = num_routes;
2839 card->of_dapm_routes = routes;
2843 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
2845 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
2847 struct device_node **bitclkmaster,
2848 struct device_node **framemaster)
2852 unsigned int format = 0;
2858 } of_fmt_table[] = {
2859 { "i2s", SND_SOC_DAIFMT_I2S },
2860 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
2861 { "left_j", SND_SOC_DAIFMT_LEFT_J },
2862 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
2863 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
2864 { "ac97", SND_SOC_DAIFMT_AC97 },
2865 { "pdm", SND_SOC_DAIFMT_PDM},
2866 { "msb", SND_SOC_DAIFMT_MSB },
2867 { "lsb", SND_SOC_DAIFMT_LSB },
2874 * check "dai-format = xxx"
2875 * or "[prefix]format = xxx"
2876 * SND_SOC_DAIFMT_FORMAT_MASK area
2878 ret = of_property_read_string(np, "dai-format", &str);
2880 snprintf(prop, sizeof(prop), "%sformat", prefix);
2881 ret = of_property_read_string(np, prop, &str);
2884 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
2885 if (strcmp(str, of_fmt_table[i].name) == 0) {
2886 format |= of_fmt_table[i].val;
2893 * check "[prefix]continuous-clock"
2894 * SND_SOC_DAIFMT_CLOCK_MASK area
2896 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
2897 if (of_property_read_bool(np, prop))
2898 format |= SND_SOC_DAIFMT_CONT;
2900 format |= SND_SOC_DAIFMT_GATED;
2903 * check "[prefix]bitclock-inversion"
2904 * check "[prefix]frame-inversion"
2905 * SND_SOC_DAIFMT_INV_MASK area
2907 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
2908 bit = !!of_get_property(np, prop, NULL);
2910 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
2911 frame = !!of_get_property(np, prop, NULL);
2913 switch ((bit << 4) + frame) {
2915 format |= SND_SOC_DAIFMT_IB_IF;
2918 format |= SND_SOC_DAIFMT_IB_NF;
2921 format |= SND_SOC_DAIFMT_NB_IF;
2924 /* SND_SOC_DAIFMT_NB_NF is default */
2929 * check "[prefix]bitclock-master"
2930 * check "[prefix]frame-master"
2931 * SND_SOC_DAIFMT_MASTER_MASK area
2933 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
2934 bit = !!of_get_property(np, prop, NULL);
2935 if (bit && bitclkmaster)
2936 *bitclkmaster = of_parse_phandle(np, prop, 0);
2938 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
2939 frame = !!of_get_property(np, prop, NULL);
2940 if (frame && framemaster)
2941 *framemaster = of_parse_phandle(np, prop, 0);
2943 switch ((bit << 4) + frame) {
2945 format |= SND_SOC_DAIFMT_CBM_CFM;
2948 format |= SND_SOC_DAIFMT_CBM_CFS;
2951 format |= SND_SOC_DAIFMT_CBS_CFM;
2954 format |= SND_SOC_DAIFMT_CBS_CFS;
2960 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
2962 int snd_soc_get_dai_id(struct device_node *ep)
2964 struct snd_soc_component *component;
2965 struct snd_soc_dai_link_component dlc;
2968 dlc.of_node = of_graph_get_port_parent(ep);
2971 * For example HDMI case, HDMI has video/sound port,
2972 * but ALSA SoC needs sound port number only.
2973 * Thus counting HDMI DT port/endpoint doesn't work.
2974 * Then, it should have .of_xlate_dai_id
2977 mutex_lock(&client_mutex);
2978 component = soc_find_component(&dlc);
2980 ret = snd_soc_component_of_xlate_dai_id(component, ep);
2981 mutex_unlock(&client_mutex);
2983 of_node_put(dlc.of_node);
2987 EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
2989 int snd_soc_get_dai_name(struct of_phandle_args *args,
2990 const char **dai_name)
2992 struct snd_soc_component *pos;
2993 struct device_node *component_of_node;
2994 int ret = -EPROBE_DEFER;
2996 mutex_lock(&client_mutex);
2997 for_each_component(pos) {
2998 component_of_node = soc_component_to_node(pos);
3000 if (component_of_node != args->np)
3003 ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3004 if (ret == -ENOTSUPP) {
3005 struct snd_soc_dai *dai;
3008 switch (args->args_count) {
3010 id = 0; /* same as dai_drv[0] */
3020 if (id < 0 || id >= pos->num_dai) {
3027 /* find target DAI */
3028 for_each_component_dais(pos, dai) {
3034 *dai_name = dai->driver->name;
3036 *dai_name = pos->name;
3039 * if another error than ENOTSUPP is returned go on and
3040 * check if another component is provided with the same
3041 * node. This may happen if a device provides several
3049 mutex_unlock(&client_mutex);
3052 EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
3054 int snd_soc_of_get_dai_name(struct device_node *of_node,
3055 const char **dai_name)
3057 struct of_phandle_args args;
3060 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3061 "#sound-dai-cells", 0, &args);
3065 ret = snd_soc_get_dai_name(&args, dai_name);
3067 of_node_put(args.np);
3071 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3074 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3075 * @dai_link: DAI link
3077 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3079 void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3081 struct snd_soc_dai_link_component *component;
3084 for_each_link_codecs(dai_link, index, component) {
3085 if (!component->of_node)
3087 of_node_put(component->of_node);
3088 component->of_node = NULL;
3091 EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3094 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3096 * @of_node: Device node
3097 * @dai_link: DAI link
3099 * Builds an array of CODEC DAI components from the DAI link property
3101 * The array is set in the DAI link and the number of DAIs is set accordingly.
3102 * The device nodes in the array (of_node) must be dereferenced by calling
3103 * snd_soc_of_put_dai_link_codecs() on @dai_link.
3105 * Returns 0 for success
3107 int snd_soc_of_get_dai_link_codecs(struct device *dev,
3108 struct device_node *of_node,
3109 struct snd_soc_dai_link *dai_link)
3111 struct of_phandle_args args;
3112 struct snd_soc_dai_link_component *component;
3114 int index, num_codecs, ret;
3116 /* Count the number of CODECs */
3118 num_codecs = of_count_phandle_with_args(of_node, name,
3119 "#sound-dai-cells");
3120 if (num_codecs <= 0) {
3121 if (num_codecs == -ENOENT)
3122 dev_err(dev, "No 'sound-dai' property\n");
3124 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3127 component = devm_kcalloc(dev,
3128 num_codecs, sizeof(*component),
3132 dai_link->codecs = component;
3133 dai_link->num_codecs = num_codecs;
3135 /* Parse the list */
3136 for_each_link_codecs(dai_link, index, component) {
3137 ret = of_parse_phandle_with_args(of_node, name,
3142 component->of_node = args.np;
3143 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3149 snd_soc_of_put_dai_link_codecs(dai_link);
3150 dai_link->codecs = NULL;
3151 dai_link->num_codecs = 0;
3154 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3156 static int __init snd_soc_init(void)
3158 snd_soc_debugfs_init();
3159 snd_soc_util_init();
3161 return platform_driver_register(&soc_driver);
3163 module_init(snd_soc_init);
3165 static void __exit snd_soc_exit(void)
3167 snd_soc_util_exit();
3168 snd_soc_debugfs_exit();
3170 platform_driver_unregister(&soc_driver);
3172 module_exit(snd_soc_exit);
3174 /* Module information */
3175 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3176 MODULE_DESCRIPTION("ALSA SoC Core");
3177 MODULE_LICENSE("GPL");
3178 MODULE_ALIAS("platform:soc-audio");