1 // SPDX-License-Identifier: GPL-2.0+
3 // soc-core.c -- ALSA SoC Audio Layer
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Copyright 2005 Openedhand Ltd.
7 // Copyright (C) 2010 Slimlogic Ltd.
8 // Copyright (C) 2010 Texas Instruments Inc.
10 // Author: Liam Girdwood <lrg@slimlogic.co.uk>
11 // with code, comments and ideas from :-
12 // Richard Purdie <richard@openedhand.com>
15 // o Add hw rules to enforce rates, etc.
16 // o More testing with other codecs/machines.
17 // o Add more codecs and platforms to ensure good API coverage.
18 // o Support TDM on PCM and I2S
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
25 #include <linux/bitops.h>
26 #include <linux/debugfs.h>
27 #include <linux/platform_device.h>
28 #include <linux/pinctrl/consumer.h>
29 #include <linux/ctype.h>
30 #include <linux/slab.h>
32 #include <linux/of_graph.h>
33 #include <linux/dmi.h>
34 #include <sound/core.h>
35 #include <sound/jack.h>
36 #include <sound/pcm.h>
37 #include <sound/pcm_params.h>
38 #include <sound/soc.h>
39 #include <sound/soc-dpcm.h>
40 #include <sound/soc-topology.h>
41 #include <sound/initval.h>
43 #define CREATE_TRACE_POINTS
44 #include <trace/events/asoc.h>
48 #ifdef CONFIG_DEBUG_FS
49 struct dentry *snd_soc_debugfs_root;
50 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
53 static DEFINE_MUTEX(client_mutex);
54 static LIST_HEAD(component_list);
55 static LIST_HEAD(unbind_card_list);
57 #define for_each_component(component) \
58 list_for_each_entry(component, &component_list, list)
61 * This is used if driver don't need to have CPU/Codec/Platform
64 struct snd_soc_dai_link_component null_dailink_component[0];
65 EXPORT_SYMBOL_GPL(null_dailink_component);
68 * This is a timeout to do a DAPM powerdown after a stream is closed().
69 * It can be used to eliminate pops between different playback streams, e.g.
70 * between two audio tracks.
72 static int pmdown_time = 5000;
73 module_param(pmdown_time, int, 0);
74 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
78 * If a DMI filed contain strings in this blacklist (e.g.
79 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
80 * as invalid and dropped when setting the card long name from DMI info.
82 static const char * const dmi_blacklist[] = {
83 "To be filled by OEM",
89 NULL, /* terminator */
93 static ssize_t pmdown_time_show(struct device *dev,
94 struct device_attribute *attr, char *buf)
96 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
98 return sprintf(buf, "%ld\n", rtd->pmdown_time);
101 static ssize_t pmdown_time_set(struct device *dev,
102 struct device_attribute *attr,
103 const char *buf, size_t count)
105 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
108 ret = kstrtol(buf, 10, &rtd->pmdown_time);
115 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
117 static struct attribute *soc_dev_attrs[] = {
118 &dev_attr_pmdown_time.attr,
122 static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
123 struct attribute *attr, int idx)
125 struct device *dev = kobj_to_dev(kobj);
126 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
128 if (attr == &dev_attr_pmdown_time.attr)
129 return attr->mode; /* always visible */
130 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
133 static const struct attribute_group soc_dapm_dev_group = {
134 .attrs = soc_dapm_dev_attrs,
135 .is_visible = soc_dev_attr_is_visible,
138 static const struct attribute_group soc_dev_group = {
139 .attrs = soc_dev_attrs,
140 .is_visible = soc_dev_attr_is_visible,
143 static const struct attribute_group *soc_dev_attr_groups[] = {
149 #ifdef CONFIG_DEBUG_FS
150 static void soc_init_component_debugfs(struct snd_soc_component *component)
152 if (!component->card->debugfs_card_root)
155 if (component->debugfs_prefix) {
158 name = kasprintf(GFP_KERNEL, "%s:%s",
159 component->debugfs_prefix, component->name);
161 component->debugfs_root = debugfs_create_dir(name,
162 component->card->debugfs_card_root);
166 component->debugfs_root = debugfs_create_dir(component->name,
167 component->card->debugfs_card_root);
170 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
171 component->debugfs_root);
174 static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
176 if (!component->debugfs_root)
178 debugfs_remove_recursive(component->debugfs_root);
179 component->debugfs_root = NULL;
182 static int dai_list_show(struct seq_file *m, void *v)
184 struct snd_soc_component *component;
185 struct snd_soc_dai *dai;
187 mutex_lock(&client_mutex);
189 for_each_component(component)
190 for_each_component_dais(component, dai)
191 seq_printf(m, "%s\n", dai->name);
193 mutex_unlock(&client_mutex);
197 DEFINE_SHOW_ATTRIBUTE(dai_list);
199 static int component_list_show(struct seq_file *m, void *v)
201 struct snd_soc_component *component;
203 mutex_lock(&client_mutex);
205 for_each_component(component)
206 seq_printf(m, "%s\n", component->name);
208 mutex_unlock(&client_mutex);
212 DEFINE_SHOW_ATTRIBUTE(component_list);
214 static void soc_init_card_debugfs(struct snd_soc_card *card)
216 card->debugfs_card_root = debugfs_create_dir(card->name,
217 snd_soc_debugfs_root);
219 debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root,
222 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
225 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
227 debugfs_remove_recursive(card->debugfs_card_root);
228 card->debugfs_card_root = NULL;
231 static void snd_soc_debugfs_init(void)
233 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
235 debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
238 debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
239 &component_list_fops);
242 static void snd_soc_debugfs_exit(void)
244 debugfs_remove_recursive(snd_soc_debugfs_root);
249 static inline void soc_init_component_debugfs(
250 struct snd_soc_component *component)
254 static inline void soc_cleanup_component_debugfs(
255 struct snd_soc_component *component)
259 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
263 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
267 static inline void snd_soc_debugfs_init(void)
271 static inline void snd_soc_debugfs_exit(void)
277 static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
278 struct snd_soc_component *component)
280 struct snd_soc_rtdcom_list *rtdcom;
282 for_each_rtdcom(rtd, rtdcom) {
283 /* already connected */
284 if (rtdcom->component == component)
288 rtdcom = kmalloc(sizeof(*rtdcom), GFP_KERNEL);
292 rtdcom->component = component;
293 INIT_LIST_HEAD(&rtdcom->list);
295 list_add_tail(&rtdcom->list, &rtd->component_list);
300 static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
302 struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
304 for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
307 INIT_LIST_HEAD(&rtd->component_list);
310 struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
311 const char *driver_name)
313 struct snd_soc_rtdcom_list *rtdcom;
318 for_each_rtdcom(rtd, rtdcom) {
319 const char *component_name = rtdcom->component->driver->name;
324 if ((component_name == driver_name) ||
325 strcmp(component_name, driver_name) == 0)
326 return rtdcom->component;
331 EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
333 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
334 const char *dai_link, int stream)
336 struct snd_soc_pcm_runtime *rtd;
338 for_each_card_rtds(card, rtd) {
339 if (rtd->dai_link->no_pcm &&
340 !strcmp(rtd->dai_link->name, dai_link))
341 return rtd->pcm->streams[stream].substream;
343 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
346 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
348 static const struct snd_soc_ops null_snd_soc_ops;
350 static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
351 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
353 struct snd_soc_pcm_runtime *rtd;
355 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
359 INIT_LIST_HEAD(&rtd->component_list);
361 rtd->dai_link = dai_link;
362 if (!rtd->dai_link->ops)
363 rtd->dai_link->ops = &null_snd_soc_ops;
365 rtd->codec_dais = kcalloc(dai_link->num_codecs,
366 sizeof(struct snd_soc_dai *),
368 if (!rtd->codec_dais) {
376 static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
378 kfree(rtd->codec_dais);
379 snd_soc_rtdcom_del_all(rtd);
383 static void soc_add_pcm_runtime(struct snd_soc_card *card,
384 struct snd_soc_pcm_runtime *rtd)
386 /* see for_each_card_rtds */
387 list_add_tail(&rtd->list, &card->rtd_list);
388 rtd->num = card->num_rtd;
392 static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
394 struct snd_soc_pcm_runtime *rtd, *_rtd;
396 for_each_card_rtds_safe(card, rtd, _rtd) {
397 list_del(&rtd->list);
398 soc_free_pcm_runtime(rtd);
404 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
405 const char *dai_link)
407 struct snd_soc_pcm_runtime *rtd;
409 for_each_card_rtds(card, rtd) {
410 if (!strcmp(rtd->dai_link->name, dai_link))
413 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
416 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
418 static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
420 struct snd_soc_pcm_runtime *rtd;
422 for_each_card_rtds(card, rtd)
423 flush_delayed_work(&rtd->delayed_work);
426 #ifdef CONFIG_PM_SLEEP
427 /* powers down audio subsystem for suspend */
428 int snd_soc_suspend(struct device *dev)
430 struct snd_soc_card *card = dev_get_drvdata(dev);
431 struct snd_soc_component *component;
432 struct snd_soc_pcm_runtime *rtd;
435 /* If the card is not initialized yet there is nothing to do */
436 if (!card->instantiated)
440 * Due to the resume being scheduled into a workqueue we could
441 * suspend before that's finished - wait for it to complete.
443 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
445 /* we're going to block userspace touching us until resume completes */
446 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
448 /* mute any active DACs */
449 for_each_card_rtds(card, rtd) {
450 struct snd_soc_dai *dai;
452 if (rtd->dai_link->ignore_suspend)
455 for_each_rtd_codec_dai(rtd, i, dai) {
456 if (dai->playback_active)
457 snd_soc_dai_digital_mute(dai, 1,
458 SNDRV_PCM_STREAM_PLAYBACK);
462 /* suspend all pcms */
463 for_each_card_rtds(card, rtd) {
464 if (rtd->dai_link->ignore_suspend)
467 snd_pcm_suspend_all(rtd->pcm);
470 if (card->suspend_pre)
471 card->suspend_pre(card);
473 for_each_card_rtds(card, rtd) {
474 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
476 if (rtd->dai_link->ignore_suspend)
479 if (!cpu_dai->driver->bus_control)
480 snd_soc_dai_suspend(cpu_dai);
483 /* close any waiting streams */
484 snd_soc_flush_all_delayed_work(card);
486 for_each_card_rtds(card, rtd) {
488 if (rtd->dai_link->ignore_suspend)
491 snd_soc_dapm_stream_event(rtd,
492 SNDRV_PCM_STREAM_PLAYBACK,
493 SND_SOC_DAPM_STREAM_SUSPEND);
495 snd_soc_dapm_stream_event(rtd,
496 SNDRV_PCM_STREAM_CAPTURE,
497 SND_SOC_DAPM_STREAM_SUSPEND);
500 /* Recheck all endpoints too, their state is affected by suspend */
501 dapm_mark_endpoints_dirty(card);
502 snd_soc_dapm_sync(&card->dapm);
504 /* suspend all COMPONENTs */
505 for_each_card_components(card, component) {
506 struct snd_soc_dapm_context *dapm =
507 snd_soc_component_get_dapm(component);
510 * If there are paths active then the COMPONENT will be held
511 * with bias _ON and should not be suspended.
513 if (!snd_soc_component_is_suspended(component)) {
514 switch (snd_soc_dapm_get_bias_level(dapm)) {
515 case SND_SOC_BIAS_STANDBY:
517 * If the COMPONENT is capable of idle
518 * bias off then being in STANDBY
519 * means it's doing something,
520 * otherwise fall through.
522 if (dapm->idle_bias_off) {
523 dev_dbg(component->dev,
524 "ASoC: idle_bias_off CODEC on over suspend\n");
529 case SND_SOC_BIAS_OFF:
530 snd_soc_component_suspend(component);
531 if (component->regmap)
532 regcache_mark_dirty(component->regmap);
533 /* deactivate pins to sleep state */
534 pinctrl_pm_select_sleep_state(component->dev);
537 dev_dbg(component->dev,
538 "ASoC: COMPONENT is on over suspend\n");
544 for_each_card_rtds(card, rtd) {
545 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
547 if (rtd->dai_link->ignore_suspend)
550 if (cpu_dai->driver->bus_control)
551 snd_soc_dai_suspend(cpu_dai);
553 /* deactivate pins to sleep state */
554 pinctrl_pm_select_sleep_state(cpu_dai->dev);
557 if (card->suspend_post)
558 card->suspend_post(card);
562 EXPORT_SYMBOL_GPL(snd_soc_suspend);
565 * deferred resume work, so resume can complete before we finished
566 * setting our codec back up, which can be very slow on I2C
568 static void soc_resume_deferred(struct work_struct *work)
570 struct snd_soc_card *card =
571 container_of(work, struct snd_soc_card,
572 deferred_resume_work);
573 struct snd_soc_pcm_runtime *rtd;
574 struct snd_soc_component *component;
578 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
579 * so userspace apps are blocked from touching us
582 dev_dbg(card->dev, "ASoC: starting resume work\n");
584 /* Bring us up into D2 so that DAPM starts enabling things */
585 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
587 if (card->resume_pre)
588 card->resume_pre(card);
590 /* resume control bus DAIs */
591 for_each_card_rtds(card, rtd) {
592 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
594 if (rtd->dai_link->ignore_suspend)
597 if (cpu_dai->driver->bus_control)
598 snd_soc_dai_resume(cpu_dai);
601 for_each_card_components(card, component) {
602 if (snd_soc_component_is_suspended(component))
603 snd_soc_component_resume(component);
606 for_each_card_rtds(card, rtd) {
608 if (rtd->dai_link->ignore_suspend)
611 snd_soc_dapm_stream_event(rtd,
612 SNDRV_PCM_STREAM_PLAYBACK,
613 SND_SOC_DAPM_STREAM_RESUME);
615 snd_soc_dapm_stream_event(rtd,
616 SNDRV_PCM_STREAM_CAPTURE,
617 SND_SOC_DAPM_STREAM_RESUME);
620 /* unmute any active DACs */
621 for_each_card_rtds(card, rtd) {
622 struct snd_soc_dai *dai;
624 if (rtd->dai_link->ignore_suspend)
627 for_each_rtd_codec_dai(rtd, i, dai) {
628 if (dai->playback_active)
629 snd_soc_dai_digital_mute(dai, 0,
630 SNDRV_PCM_STREAM_PLAYBACK);
634 for_each_card_rtds(card, rtd) {
635 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
637 if (rtd->dai_link->ignore_suspend)
640 if (!cpu_dai->driver->bus_control)
641 snd_soc_dai_resume(cpu_dai);
644 if (card->resume_post)
645 card->resume_post(card);
647 dev_dbg(card->dev, "ASoC: resume work completed\n");
649 /* Recheck all endpoints too, their state is affected by suspend */
650 dapm_mark_endpoints_dirty(card);
651 snd_soc_dapm_sync(&card->dapm);
653 /* userspace can access us now we are back as we were before */
654 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
657 /* powers up audio subsystem after a suspend */
658 int snd_soc_resume(struct device *dev)
660 struct snd_soc_card *card = dev_get_drvdata(dev);
661 bool bus_control = false;
662 struct snd_soc_pcm_runtime *rtd;
663 struct snd_soc_dai *codec_dai;
666 /* If the card is not initialized yet there is nothing to do */
667 if (!card->instantiated)
670 /* activate pins from sleep state */
671 for_each_card_rtds(card, rtd) {
672 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
675 pinctrl_pm_select_default_state(cpu_dai->dev);
677 for_each_rtd_codec_dai(rtd, i, codec_dai) {
678 if (codec_dai->active)
679 pinctrl_pm_select_default_state(codec_dai->dev);
684 * DAIs that also act as the control bus master might have other drivers
685 * hanging off them so need to resume immediately. Other drivers don't
686 * have that problem and may take a substantial amount of time to resume
687 * due to I/O costs and anti-pop so handle them out of line.
689 for_each_card_rtds(card, rtd) {
690 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
692 bus_control |= cpu_dai->driver->bus_control;
695 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
696 soc_resume_deferred(&card->deferred_resume_work);
698 dev_dbg(dev, "ASoC: Scheduling resume work\n");
699 if (!schedule_work(&card->deferred_resume_work))
700 dev_err(dev, "ASoC: resume work item may be lost\n");
705 EXPORT_SYMBOL_GPL(snd_soc_resume);
707 static void soc_resume_init(struct snd_soc_card *card)
709 /* deferred resume work */
710 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
713 #define snd_soc_suspend NULL
714 #define snd_soc_resume NULL
715 static inline void soc_resume_init(struct snd_soc_card *card)
720 static const struct snd_soc_dai_ops null_dai_ops = {
723 static struct device_node
724 *soc_component_to_node(struct snd_soc_component *component)
726 struct device_node *of_node;
728 of_node = component->dev->of_node;
729 if (!of_node && component->dev->parent)
730 of_node = component->dev->parent->of_node;
735 static int snd_soc_is_matching_component(
736 const struct snd_soc_dai_link_component *dlc,
737 struct snd_soc_component *component)
739 struct device_node *component_of_node;
744 component_of_node = soc_component_to_node(component);
746 if (dlc->of_node && component_of_node != dlc->of_node)
748 if (dlc->name && strcmp(component->name, dlc->name))
754 static struct snd_soc_component *soc_find_component(
755 const struct snd_soc_dai_link_component *dlc)
757 struct snd_soc_component *component;
759 lockdep_assert_held(&client_mutex);
764 * It returns *1st* found component, but some driver
765 * has few components by same of_node/name
767 * CPU component and generic DMAEngine component
769 for_each_component(component)
770 if (snd_soc_is_matching_component(dlc, component))
777 * snd_soc_find_dai - Find a registered DAI
779 * @dlc: name of the DAI or the DAI driver and optional component info to match
781 * This function will search all registered components and their DAIs to
782 * find the DAI of the same name. The component's of_node and name
783 * should also match if being specified.
785 * Return: pointer of DAI, or NULL if not found.
787 struct snd_soc_dai *snd_soc_find_dai(
788 const struct snd_soc_dai_link_component *dlc)
790 struct snd_soc_component *component;
791 struct snd_soc_dai *dai;
793 lockdep_assert_held(&client_mutex);
795 /* Find CPU DAI from registered DAIs */
796 for_each_component(component) {
797 if (!snd_soc_is_matching_component(dlc, component))
799 for_each_component_dais(component, dai) {
800 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
801 && (!dai->driver->name
802 || strcmp(dai->driver->name, dlc->dai_name)))
811 EXPORT_SYMBOL_GPL(snd_soc_find_dai);
814 * snd_soc_find_dai_link - Find a DAI link
817 * @id: DAI link ID to match
818 * @name: DAI link name to match, optional
819 * @stream_name: DAI link stream name to match, optional
821 * This function will search all existing DAI links of the soc card to
822 * find the link of the same ID. Since DAI links may not have their
823 * unique ID, so name and stream name should also match if being
826 * Return: pointer of DAI link, or NULL if not found.
828 struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
829 int id, const char *name,
830 const char *stream_name)
832 struct snd_soc_dai_link *link;
834 lockdep_assert_held(&client_mutex);
836 for_each_card_links(card, link) {
840 if (name && (!link->name || strcmp(name, link->name)))
843 if (stream_name && (!link->stream_name
844 || strcmp(stream_name, link->stream_name)))
852 EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
854 static bool soc_is_dai_link_bound(struct snd_soc_card *card,
855 struct snd_soc_dai_link *dai_link)
857 struct snd_soc_pcm_runtime *rtd;
859 for_each_card_rtds(card, rtd) {
860 if (rtd->dai_link == dai_link)
867 static int soc_bind_dai_link(struct snd_soc_card *card,
868 struct snd_soc_dai_link *dai_link)
870 struct snd_soc_pcm_runtime *rtd;
871 struct snd_soc_dai_link_component *codec, *platform;
872 struct snd_soc_component *component;
875 if (dai_link->ignore)
878 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
880 if (soc_is_dai_link_bound(card, dai_link)) {
881 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
886 rtd = soc_new_pcm_runtime(card, dai_link);
890 /* FIXME: we need multi CPU support in the future */
891 rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
893 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
894 dai_link->cpus->dai_name);
897 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
899 /* Find CODEC from registered CODECs */
900 rtd->num_codecs = dai_link->num_codecs;
901 for_each_link_codecs(dai_link, i, codec) {
902 rtd->codec_dais[i] = snd_soc_find_dai(codec);
903 if (!rtd->codec_dais[i]) {
904 dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
909 snd_soc_rtdcom_add(rtd, rtd->codec_dais[i]->component);
912 /* Single codec links expect codec and codec_dai in runtime data */
913 rtd->codec_dai = rtd->codec_dais[0];
915 /* Find PLATFORM from registered PLATFORMs */
916 for_each_link_platforms(dai_link, i, platform) {
917 for_each_component(component) {
918 if (!snd_soc_is_matching_component(platform, component))
921 snd_soc_rtdcom_add(rtd, component);
925 soc_add_pcm_runtime(card, rtd);
929 soc_free_pcm_runtime(rtd);
930 return -EPROBE_DEFER;
933 static void soc_cleanup_component(struct snd_soc_component *component)
935 snd_soc_component_set_jack(component, NULL, NULL);
936 list_del(&component->card_list);
937 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
938 soc_cleanup_component_debugfs(component);
939 component->card = NULL;
940 snd_soc_component_module_put_when_remove(component);
943 static void soc_remove_component(struct snd_soc_component *component)
945 if (!component->card)
948 snd_soc_component_remove(component);
950 soc_cleanup_component(component);
953 static void soc_remove_dai(struct snd_soc_dai *dai, int order)
957 if (!dai || !dai->probed || !dai->driver ||
958 dai->driver->remove_order != order)
961 err = snd_soc_dai_remove(dai);
964 "ASoC: failed to remove %s: %d\n",
970 static void soc_remove_link_dais(struct snd_soc_card *card,
971 struct snd_soc_pcm_runtime *rtd, int order)
974 struct snd_soc_dai *codec_dai;
976 /* unregister the rtd device */
977 if (rtd->dev_registered) {
978 device_unregister(rtd->dev);
979 rtd->dev_registered = 0;
982 /* remove the CODEC DAI */
983 for_each_rtd_codec_dai(rtd, i, codec_dai)
984 soc_remove_dai(codec_dai, order);
986 soc_remove_dai(rtd->cpu_dai, order);
989 static void soc_remove_link_components(struct snd_soc_card *card,
990 struct snd_soc_pcm_runtime *rtd, int order)
992 struct snd_soc_component *component;
993 struct snd_soc_rtdcom_list *rtdcom;
995 for_each_rtdcom(rtd, rtdcom) {
996 component = rtdcom->component;
998 if (component->driver->remove_order == order)
999 soc_remove_component(component);
1003 static void soc_remove_dai_links(struct snd_soc_card *card)
1006 struct snd_soc_pcm_runtime *rtd;
1007 struct snd_soc_dai_link *link, *_link;
1009 for_each_comp_order(order) {
1010 for_each_card_rtds(card, rtd)
1011 soc_remove_link_dais(card, rtd, order);
1014 for_each_comp_order(order) {
1015 for_each_card_rtds(card, rtd)
1016 soc_remove_link_components(card, rtd, order);
1019 for_each_card_links_safe(card, link, _link) {
1020 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1021 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1024 list_del(&link->list);
1028 static int soc_init_dai_link(struct snd_soc_card *card,
1029 struct snd_soc_dai_link *link)
1032 struct snd_soc_dai_link_component *codec, *platform;
1034 for_each_link_codecs(link, i, codec) {
1036 * Codec must be specified by 1 of name or OF node,
1037 * not both or neither.
1039 if (!!codec->name == !!codec->of_node) {
1040 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1045 /* Codec DAI name must be specified */
1046 if (!codec->dai_name) {
1047 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1053 * Defer card registration if codec component is not added to
1056 if (!soc_find_component(codec))
1057 return -EPROBE_DEFER;
1060 for_each_link_platforms(link, i, platform) {
1062 * Platform may be specified by either name or OF node, but it
1063 * can be left unspecified, then no components will be inserted
1064 * in the rtdcom list
1066 if (!!platform->name == !!platform->of_node) {
1068 "ASoC: Neither/both platform name/of_node are set for %s\n",
1074 * Defer card registration if platform component is not added to
1077 if (!soc_find_component(platform))
1078 return -EPROBE_DEFER;
1082 if (link->num_cpus > 1) {
1084 "ASoC: multi cpu is not yet supported %s\n",
1090 * CPU device may be specified by either name or OF node, but
1091 * can be left unspecified, and will be matched based on DAI
1094 if (link->cpus->name && link->cpus->of_node) {
1096 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1102 * Defer card registartion if cpu dai component is not added to
1105 if ((link->cpus->of_node || link->cpus->name) &&
1106 !soc_find_component(link->cpus))
1107 return -EPROBE_DEFER;
1110 * At least one of CPU DAI name or CPU device name/node must be
1113 if (!link->cpus->dai_name &&
1114 !(link->cpus->name || link->cpus->of_node)) {
1116 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1124 void snd_soc_disconnect_sync(struct device *dev)
1126 struct snd_soc_component *component =
1127 snd_soc_lookup_component(dev, NULL);
1129 if (!component || !component->card)
1132 snd_card_disconnect_sync(component->card->snd_card);
1134 EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
1137 * snd_soc_add_dai_link - Add a DAI link dynamically
1138 * @card: The ASoC card to which the DAI link is added
1139 * @dai_link: The new DAI link to add
1141 * This function adds a DAI link to the ASoC card's link list.
1143 * Note: Topology can use this API to add DAI links when probing the
1144 * topology component. And machine drivers can still define static
1145 * DAI links in dai_link array.
1147 int snd_soc_add_dai_link(struct snd_soc_card *card,
1148 struct snd_soc_dai_link *dai_link)
1150 if (dai_link->dobj.type
1151 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1152 dev_err(card->dev, "Invalid dai link type %d\n",
1153 dai_link->dobj.type);
1157 lockdep_assert_held(&client_mutex);
1159 * Notify the machine driver for extra initialization
1160 * on the link created by topology.
1162 if (dai_link->dobj.type && card->add_dai_link)
1163 card->add_dai_link(card, dai_link);
1165 /* see for_each_card_links */
1166 list_add_tail(&dai_link->list, &card->dai_link_list);
1170 EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1173 * snd_soc_remove_dai_link - Remove a DAI link from the list
1174 * @card: The ASoC card that owns the link
1175 * @dai_link: The DAI link to remove
1177 * This function removes a DAI link from the ASoC card's link list.
1179 * For DAI links previously added by topology, topology should
1180 * remove them by using the dobj embedded in the link.
1182 void snd_soc_remove_dai_link(struct snd_soc_card *card,
1183 struct snd_soc_dai_link *dai_link)
1185 if (dai_link->dobj.type
1186 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1187 dev_err(card->dev, "Invalid dai link type %d\n",
1188 dai_link->dobj.type);
1192 lockdep_assert_held(&client_mutex);
1194 * Notify the machine driver for extra destruction
1195 * on the link created by topology.
1197 if (dai_link->dobj.type && card->remove_dai_link)
1198 card->remove_dai_link(card, dai_link);
1200 list_del(&dai_link->list);
1202 EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1204 static void soc_set_of_name_prefix(struct snd_soc_component *component)
1206 struct device_node *component_of_node = soc_component_to_node(component);
1210 ret = of_property_read_string(component_of_node, "sound-name-prefix",
1213 component->name_prefix = str;
1216 static void soc_set_name_prefix(struct snd_soc_card *card,
1217 struct snd_soc_component *component)
1221 for (i = 0; i < card->num_configs && card->codec_conf; i++) {
1222 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1223 struct device_node *component_of_node = soc_component_to_node(component);
1225 if (map->of_node && component_of_node != map->of_node)
1227 if (map->dev_name && strcmp(component->name, map->dev_name))
1229 component->name_prefix = map->name_prefix;
1234 * If there is no configuration table or no match in the table,
1235 * check if a prefix is provided in the node
1237 soc_set_of_name_prefix(component);
1240 static int soc_probe_component(struct snd_soc_card *card,
1241 struct snd_soc_component *component)
1243 struct snd_soc_dapm_context *dapm =
1244 snd_soc_component_get_dapm(component);
1245 struct snd_soc_dai *dai;
1248 if (!strcmp(component->name, "snd-soc-dummy"))
1251 if (component->card) {
1252 if (component->card != card) {
1253 dev_err(component->dev,
1254 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1255 card->name, component->card->name);
1261 ret = snd_soc_component_module_get_when_probe(component);
1265 component->card = card;
1267 INIT_LIST_HEAD(&component->card_list);
1268 INIT_LIST_HEAD(&dapm->list);
1269 soc_set_name_prefix(card, component);
1271 soc_init_component_debugfs(component);
1273 ret = snd_soc_dapm_new_controls(dapm,
1274 component->driver->dapm_widgets,
1275 component->driver->num_dapm_widgets);
1278 dev_err(component->dev,
1279 "Failed to create new controls %d\n", ret);
1283 for_each_component_dais(component, dai) {
1284 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1286 dev_err(component->dev,
1287 "Failed to create DAI widgets %d\n", ret);
1292 ret = snd_soc_component_probe(component);
1294 dev_err(component->dev,
1295 "ASoC: failed to probe component %d\n", ret);
1298 WARN(dapm->idle_bias_off &&
1299 dapm->bias_level != SND_SOC_BIAS_OFF,
1300 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1303 /* machine specific init */
1304 if (component->init) {
1305 ret = component->init(component);
1307 dev_err(component->dev,
1308 "Failed to do machine specific init %d\n", ret);
1313 ret = snd_soc_add_component_controls(component,
1314 component->driver->controls,
1315 component->driver->num_controls);
1319 ret = snd_soc_dapm_add_routes(dapm,
1320 component->driver->dapm_routes,
1321 component->driver->num_dapm_routes);
1325 list_add(&dapm->list, &card->dapm_list);
1326 /* see for_each_card_components */
1327 list_add(&component->card_list, &card->component_dev_list);
1331 soc_cleanup_component(component);
1336 static void rtd_release(struct device *dev)
1341 static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1346 /* register the rtd device */
1347 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1350 rtd->dev->parent = rtd->card->dev;
1351 rtd->dev->release = rtd_release;
1352 rtd->dev->groups = soc_dev_attr_groups;
1353 dev_set_name(rtd->dev, "%s", name);
1354 dev_set_drvdata(rtd->dev, rtd);
1355 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1356 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1357 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1358 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1359 ret = device_register(rtd->dev);
1361 /* calling put_device() here to free the rtd->dev */
1362 put_device(rtd->dev);
1363 dev_err(rtd->card->dev,
1364 "ASoC: failed to register runtime device: %d\n", ret);
1367 rtd->dev_registered = 1;
1371 static int soc_probe_link_components(struct snd_soc_card *card,
1372 struct snd_soc_pcm_runtime *rtd, int order)
1374 struct snd_soc_component *component;
1375 struct snd_soc_rtdcom_list *rtdcom;
1378 for_each_rtdcom(rtd, rtdcom) {
1379 component = rtdcom->component;
1381 if (component->driver->probe_order == order) {
1382 ret = soc_probe_component(card, component);
1391 static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1396 dai->driver->probe_order != order)
1399 ret = snd_soc_dai_probe(dai);
1401 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1411 static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1412 struct snd_soc_pcm_runtime *rtd)
1416 for (i = 0; i < num_dais; ++i) {
1417 struct snd_soc_dai_driver *drv = dais[i]->driver;
1420 ret = drv->pcm_new(rtd, dais[i]);
1422 dev_err(dais[i]->dev,
1423 "ASoC: Failed to bind %s with pcm device\n",
1432 static int soc_probe_link_dais(struct snd_soc_card *card,
1433 struct snd_soc_pcm_runtime *rtd, int order)
1435 struct snd_soc_dai_link *dai_link = rtd->dai_link;
1436 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1437 struct snd_soc_rtdcom_list *rtdcom;
1438 struct snd_soc_component *component;
1439 struct snd_soc_dai *codec_dai;
1442 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1443 card->name, rtd->num, order);
1445 /* set default power off timeout */
1446 rtd->pmdown_time = pmdown_time;
1448 ret = soc_probe_dai(cpu_dai, order);
1452 /* probe the CODEC DAI */
1453 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1454 ret = soc_probe_dai(codec_dai, order);
1459 /* complete DAI probe during last probe */
1460 if (order != SND_SOC_COMP_ORDER_LAST)
1463 /* do machine specific initialization */
1464 if (dai_link->init) {
1465 ret = dai_link->init(rtd);
1467 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1468 dai_link->name, ret);
1473 if (dai_link->dai_fmt) {
1474 ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1479 ret = soc_post_component_init(rtd, dai_link->name);
1483 /* add DPCM sysfs entries */
1484 soc_dpcm_debugfs_add(rtd);
1489 * most drivers will register their PCMs using DAI link ordering but
1490 * topology based drivers can use the DAI link id field to set PCM
1491 * device number and then use rtd + a base offset of the BEs.
1493 for_each_rtdcom(rtd, rtdcom) {
1494 component = rtdcom->component;
1496 if (!component->driver->use_dai_pcm_id)
1499 if (rtd->dai_link->no_pcm)
1500 num += component->driver->be_pcm_base;
1502 num = rtd->dai_link->id;
1505 /* create compress_device if possible */
1506 ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1507 if (ret != -ENOTSUPP) {
1509 dev_err(card->dev, "ASoC: can't create compress %s\n",
1510 dai_link->stream_name);
1514 /* create the pcm */
1515 ret = soc_new_pcm(rtd, num);
1517 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1518 dai_link->stream_name, ret);
1521 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1524 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1525 rtd->num_codecs, rtd);
1529 static int soc_bind_aux_dev(struct snd_soc_card *card,
1530 struct snd_soc_aux_dev *aux_dev)
1532 struct snd_soc_component *component;
1534 /* codecs, usually analog devices */
1535 component = soc_find_component(&aux_dev->dlc);
1537 return -EPROBE_DEFER;
1539 component->init = aux_dev->init;
1540 /* see for_each_card_auxs */
1541 list_add(&component->card_aux_list, &card->aux_comp_list);
1546 static int soc_probe_aux_devices(struct snd_soc_card *card)
1548 struct snd_soc_component *comp;
1552 for_each_comp_order(order) {
1553 for_each_card_auxs(card, comp) {
1554 if (comp->driver->probe_order == order) {
1555 ret = soc_probe_component(card, comp);
1558 "ASoC: failed to probe aux component %s %d\n",
1569 static void soc_remove_aux_devices(struct snd_soc_card *card)
1571 struct snd_soc_component *comp, *_comp;
1574 for_each_comp_order(order) {
1575 for_each_card_auxs_safe(card, comp, _comp) {
1577 if (comp->driver->remove_order == order) {
1578 soc_remove_component(comp);
1579 /* remove it from the card's aux_comp_list */
1580 list_del(&comp->card_aux_list);
1587 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1588 * @rtd: The runtime for which the DAI link format should be changed
1589 * @dai_fmt: The new DAI link format
1591 * This function updates the DAI link format for all DAIs connected to the DAI
1592 * link for the specified runtime.
1594 * Note: For setups with a static format set the dai_fmt field in the
1595 * corresponding snd_dai_link struct instead of using this function.
1597 * Returns 0 on success, otherwise a negative error code.
1599 int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1600 unsigned int dai_fmt)
1602 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1603 struct snd_soc_dai *codec_dai;
1607 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1608 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1609 if (ret != 0 && ret != -ENOTSUPP) {
1610 dev_warn(codec_dai->dev,
1611 "ASoC: Failed to set DAI format: %d\n", ret);
1617 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1618 * the component which has non_legacy_dai_naming is Codec
1620 if (cpu_dai->component->driver->non_legacy_dai_naming) {
1621 unsigned int inv_dai_fmt;
1623 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1624 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1625 case SND_SOC_DAIFMT_CBM_CFM:
1626 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1628 case SND_SOC_DAIFMT_CBM_CFS:
1629 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1631 case SND_SOC_DAIFMT_CBS_CFM:
1632 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1634 case SND_SOC_DAIFMT_CBS_CFS:
1635 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1639 dai_fmt = inv_dai_fmt;
1642 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1643 if (ret != 0 && ret != -ENOTSUPP) {
1644 dev_warn(cpu_dai->dev,
1645 "ASoC: Failed to set DAI format: %d\n", ret);
1651 EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1655 * Trim special characters, and replace '-' with '_' since '-' is used to
1656 * separate different DMI fields in the card long name. Only number and
1657 * alphabet characters and a few separator characters are kept.
1659 static void cleanup_dmi_name(char *name)
1663 for (i = 0; name[i]; i++) {
1664 if (isalnum(name[i]) || (name[i] == '.')
1665 || (name[i] == '_'))
1666 name[j++] = name[i];
1667 else if (name[i] == '-')
1675 * Check if a DMI field is valid, i.e. not containing any string
1676 * in the black list.
1678 static int is_dmi_valid(const char *field)
1682 while (dmi_blacklist[i]) {
1683 if (strstr(field, dmi_blacklist[i]))
1692 * snd_soc_set_dmi_name() - Register DMI names to card
1693 * @card: The card to register DMI names
1694 * @flavour: The flavour "differentiator" for the card amongst its peers.
1696 * An Intel machine driver may be used by many different devices but are
1697 * difficult for userspace to differentiate, since machine drivers ususally
1698 * use their own name as the card short name and leave the card long name
1699 * blank. To differentiate such devices and fix bugs due to lack of
1700 * device-specific configurations, this function allows DMI info to be used
1701 * as the sound card long name, in the format of
1702 * "vendor-product-version-board"
1703 * (Character '-' is used to separate different DMI fields here).
1704 * This will help the user space to load the device-specific Use Case Manager
1705 * (UCM) configurations for the card.
1707 * Possible card long names may be:
1708 * DellInc.-XPS139343-01-0310JH
1709 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1710 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1712 * This function also supports flavoring the card longname to provide
1713 * the extra differentiation, like "vendor-product-version-board-flavor".
1715 * We only keep number and alphabet characters and a few separator characters
1716 * in the card long name since UCM in the user space uses the card long names
1717 * as card configuration directory names and AudoConf cannot support special
1718 * charactors like SPACE.
1720 * Returns 0 on success, otherwise a negative error code.
1722 int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1724 const char *vendor, *product, *product_version, *board;
1725 size_t longname_buf_size = sizeof(card->snd_card->longname);
1728 if (card->long_name)
1729 return 0; /* long name already set by driver or from DMI */
1731 /* make up dmi long name as: vendor.product.version.board */
1732 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1733 if (!vendor || !is_dmi_valid(vendor)) {
1734 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1738 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1740 cleanup_dmi_name(card->dmi_longname);
1742 product = dmi_get_system_info(DMI_PRODUCT_NAME);
1743 if (product && is_dmi_valid(product)) {
1744 len = strlen(card->dmi_longname);
1745 snprintf(card->dmi_longname + len,
1746 longname_buf_size - len,
1749 len++; /* skip the separator "-" */
1750 if (len < longname_buf_size)
1751 cleanup_dmi_name(card->dmi_longname + len);
1754 * some vendors like Lenovo may only put a self-explanatory
1755 * name in the product version field
1757 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
1758 if (product_version && is_dmi_valid(product_version)) {
1759 len = strlen(card->dmi_longname);
1760 snprintf(card->dmi_longname + len,
1761 longname_buf_size - len,
1762 "-%s", product_version);
1765 if (len < longname_buf_size)
1766 cleanup_dmi_name(card->dmi_longname + len);
1770 board = dmi_get_system_info(DMI_BOARD_NAME);
1771 if (board && is_dmi_valid(board)) {
1772 len = strlen(card->dmi_longname);
1773 snprintf(card->dmi_longname + len,
1774 longname_buf_size - len,
1778 if (len < longname_buf_size)
1779 cleanup_dmi_name(card->dmi_longname + len);
1780 } else if (!product) {
1781 /* fall back to using legacy name */
1782 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1786 /* Add flavour to dmi long name */
1788 len = strlen(card->dmi_longname);
1789 snprintf(card->dmi_longname + len,
1790 longname_buf_size - len,
1794 if (len < longname_buf_size)
1795 cleanup_dmi_name(card->dmi_longname + len);
1798 /* set the card long name */
1799 card->long_name = card->dmi_longname;
1803 EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
1804 #endif /* CONFIG_DMI */
1806 static void soc_check_tplg_fes(struct snd_soc_card *card)
1808 struct snd_soc_component *component;
1809 const struct snd_soc_component_driver *comp_drv;
1810 struct snd_soc_dai_link *dai_link;
1813 for_each_component(component) {
1815 /* does this component override FEs ? */
1816 if (!component->driver->ignore_machine)
1819 /* for this machine ? */
1820 if (!strcmp(component->driver->ignore_machine,
1821 card->dev->driver->name))
1823 if (strcmp(component->driver->ignore_machine,
1824 dev_name(card->dev)))
1827 /* machine matches, so override the rtd data */
1828 for_each_card_prelinks(card, i, dai_link) {
1830 /* ignore this FE */
1831 if (dai_link->dynamic) {
1832 dai_link->ignore = true;
1836 dev_info(card->dev, "info: override FE DAI link %s\n",
1837 card->dai_link[i].name);
1839 /* override platform component */
1840 if (!dai_link->platforms) {
1841 dev_err(card->dev, "init platform error");
1844 dai_link->platforms->name = component->name;
1846 /* convert non BE into BE */
1847 dai_link->no_pcm = 1;
1849 /* override any BE fixups */
1850 dai_link->be_hw_params_fixup =
1851 component->driver->be_hw_params_fixup;
1854 * most BE links don't set stream name, so set it to
1855 * dai link name if it's NULL to help bind widgets.
1857 if (!dai_link->stream_name)
1858 dai_link->stream_name = dai_link->name;
1861 /* Inform userspace we are using alternate topology */
1862 if (component->driver->topology_name_prefix) {
1864 /* topology shortname created? */
1865 if (!card->topology_shortname_created) {
1866 comp_drv = component->driver;
1868 snprintf(card->topology_shortname, 32, "%s-%s",
1869 comp_drv->topology_name_prefix,
1871 card->topology_shortname_created = true;
1874 /* use topology shortname */
1875 card->name = card->topology_shortname;
1880 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1882 /* free the ALSA card at first; this syncs with pending operations */
1883 if (card->snd_card) {
1884 snd_card_free(card->snd_card);
1885 card->snd_card = NULL;
1888 /* remove and free each DAI */
1889 soc_remove_dai_links(card);
1890 soc_remove_pcm_runtimes(card);
1892 /* remove auxiliary devices */
1893 soc_remove_aux_devices(card);
1895 snd_soc_dapm_free(&card->dapm);
1896 soc_cleanup_card_debugfs(card);
1898 /* remove the card */
1905 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1907 struct snd_soc_pcm_runtime *rtd;
1908 struct snd_soc_dai_link *dai_link;
1909 struct snd_soc_aux_dev *aux;
1912 mutex_lock(&client_mutex);
1913 for_each_card_prelinks(card, i, dai_link) {
1914 ret = soc_init_dai_link(card, dai_link);
1916 dev_err(card->dev, "ASoC: failed to init link %s: %d\n",
1917 dai_link->name, ret);
1918 mutex_unlock(&client_mutex);
1922 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1924 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1925 card->dapm.dev = card->dev;
1926 card->dapm.card = card;
1927 list_add(&card->dapm.list, &card->dapm_list);
1929 /* check whether any platform is ignore machine FE and using topology */
1930 soc_check_tplg_fes(card);
1933 for_each_card_prelinks(card, i, dai_link) {
1934 ret = soc_bind_dai_link(card, dai_link);
1939 /* bind aux_devs too */
1940 for_each_card_pre_auxs(card, i, aux) {
1941 ret = soc_bind_aux_dev(card, aux);
1946 /* add predefined DAI links to the list */
1947 for_each_card_prelinks(card, i, dai_link) {
1948 ret = snd_soc_add_dai_link(card, dai_link);
1953 /* card bind complete so register a sound card */
1954 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1955 card->owner, 0, &card->snd_card);
1958 "ASoC: can't create sound card for card %s: %d\n",
1963 soc_init_card_debugfs(card);
1965 soc_resume_init(card);
1967 ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1968 card->num_dapm_widgets);
1972 ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1973 card->num_of_dapm_widgets);
1977 /* initialise the sound card only once */
1979 ret = card->probe(card);
1984 /* probe all components used by DAI links on this card */
1985 for_each_comp_order(order) {
1986 for_each_card_rtds(card, rtd) {
1987 ret = soc_probe_link_components(card, rtd, order);
1990 "ASoC: failed to instantiate card %d\n",
1997 /* probe auxiliary components */
1998 ret = soc_probe_aux_devices(card);
2003 * Find new DAI links added during probing components and bind them.
2004 * Components with topology may bring new DAIs and DAI links.
2006 for_each_card_links(card, dai_link) {
2007 if (soc_is_dai_link_bound(card, dai_link))
2010 ret = soc_init_dai_link(card, dai_link);
2013 ret = soc_bind_dai_link(card, dai_link);
2018 /* probe all DAI links on this card */
2019 for_each_comp_order(order) {
2020 for_each_card_rtds(card, rtd) {
2021 ret = soc_probe_link_dais(card, rtd, order);
2024 "ASoC: failed to instantiate card %d\n",
2031 snd_soc_dapm_link_dai_widgets(card);
2032 snd_soc_dapm_connect_dai_link_widgets(card);
2034 ret = snd_soc_add_card_controls(card, card->controls,
2035 card->num_controls);
2039 ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2040 card->num_dapm_routes);
2044 ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2045 card->num_of_dapm_routes);
2049 /* try to set some sane longname if DMI is available */
2050 snd_soc_set_dmi_name(card, NULL);
2052 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
2054 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2055 "%s", card->long_name ? card->long_name : card->name);
2056 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2057 "%s", card->driver_name ? card->driver_name : card->name);
2058 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2059 switch (card->snd_card->driver[i]) {
2065 if (!isalnum(card->snd_card->driver[i]))
2066 card->snd_card->driver[i] = '_';
2071 if (card->late_probe) {
2072 ret = card->late_probe(card);
2074 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
2080 snd_soc_dapm_new_widgets(card);
2082 ret = snd_card_register(card->snd_card);
2084 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2089 card->instantiated = 1;
2090 dapm_mark_endpoints_dirty(card);
2091 snd_soc_dapm_sync(&card->dapm);
2095 soc_cleanup_card_resources(card);
2097 mutex_unlock(&card->mutex);
2098 mutex_unlock(&client_mutex);
2103 /* probes a new socdev */
2104 static int soc_probe(struct platform_device *pdev)
2106 struct snd_soc_card *card = platform_get_drvdata(pdev);
2109 * no card, so machine driver should be registering card
2110 * we should not be here in that case so ret error
2115 dev_warn(&pdev->dev,
2116 "ASoC: machine %s should use snd_soc_register_card()\n",
2119 /* Bodge while we unpick instantiation */
2120 card->dev = &pdev->dev;
2122 return snd_soc_register_card(card);
2125 /* removes a socdev */
2126 static int soc_remove(struct platform_device *pdev)
2128 struct snd_soc_card *card = platform_get_drvdata(pdev);
2130 snd_soc_unregister_card(card);
2134 int snd_soc_poweroff(struct device *dev)
2136 struct snd_soc_card *card = dev_get_drvdata(dev);
2137 struct snd_soc_pcm_runtime *rtd;
2139 if (!card->instantiated)
2143 * Flush out pmdown_time work - we actually do want to run it
2144 * now, we're shutting down so no imminent restart.
2146 snd_soc_flush_all_delayed_work(card);
2148 snd_soc_dapm_shutdown(card);
2150 /* deactivate pins to sleep state */
2151 for_each_card_rtds(card, rtd) {
2152 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2153 struct snd_soc_dai *codec_dai;
2156 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2157 for_each_rtd_codec_dai(rtd, i, codec_dai) {
2158 pinctrl_pm_select_sleep_state(codec_dai->dev);
2164 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2166 const struct dev_pm_ops snd_soc_pm_ops = {
2167 .suspend = snd_soc_suspend,
2168 .resume = snd_soc_resume,
2169 .freeze = snd_soc_suspend,
2170 .thaw = snd_soc_resume,
2171 .poweroff = snd_soc_poweroff,
2172 .restore = snd_soc_resume,
2174 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2176 /* ASoC platform driver */
2177 static struct platform_driver soc_driver = {
2179 .name = "soc-audio",
2180 .pm = &snd_soc_pm_ops,
2183 .remove = soc_remove,
2187 * snd_soc_cnew - create new control
2188 * @_template: control template
2189 * @data: control private data
2190 * @long_name: control long name
2191 * @prefix: control name prefix
2193 * Create a new mixer control from a template control.
2195 * Returns 0 for success, else error.
2197 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2198 void *data, const char *long_name,
2201 struct snd_kcontrol_new template;
2202 struct snd_kcontrol *kcontrol;
2205 memcpy(&template, _template, sizeof(template));
2209 long_name = template.name;
2212 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2216 template.name = name;
2218 template.name = long_name;
2221 kcontrol = snd_ctl_new1(&template, data);
2227 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2229 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2230 const struct snd_kcontrol_new *controls, int num_controls,
2231 const char *prefix, void *data)
2235 for (i = 0; i < num_controls; i++) {
2236 const struct snd_kcontrol_new *control = &controls[i];
2238 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2239 control->name, prefix));
2241 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2242 control->name, err);
2250 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2253 struct snd_card *card = soc_card->snd_card;
2254 struct snd_kcontrol *kctl;
2256 if (unlikely(!name))
2259 list_for_each_entry(kctl, &card->controls, list)
2260 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2264 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2267 * snd_soc_add_component_controls - Add an array of controls to a component.
2269 * @component: Component to add controls to
2270 * @controls: Array of controls to add
2271 * @num_controls: Number of elements in the array
2273 * Return: 0 for success, else error.
2275 int snd_soc_add_component_controls(struct snd_soc_component *component,
2276 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2278 struct snd_card *card = component->card->snd_card;
2280 return snd_soc_add_controls(card, component->dev, controls,
2281 num_controls, component->name_prefix, component);
2283 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2286 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2287 * Convenience function to add a list of controls.
2289 * @soc_card: SoC card to add controls to
2290 * @controls: array of controls to add
2291 * @num_controls: number of elements in the array
2293 * Return 0 for success, else error.
2295 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2296 const struct snd_kcontrol_new *controls, int num_controls)
2298 struct snd_card *card = soc_card->snd_card;
2300 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2303 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2306 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2307 * Convienience function to add a list of controls.
2309 * @dai: DAI to add controls to
2310 * @controls: array of controls to add
2311 * @num_controls: number of elements in the array
2313 * Return 0 for success, else error.
2315 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2316 const struct snd_kcontrol_new *controls, int num_controls)
2318 struct snd_card *card = dai->component->card->snd_card;
2320 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2323 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2325 static int snd_soc_bind_card(struct snd_soc_card *card)
2327 struct snd_soc_pcm_runtime *rtd;
2330 ret = snd_soc_instantiate_card(card);
2334 /* deactivate pins to sleep state */
2335 for_each_card_rtds(card, rtd) {
2336 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2337 struct snd_soc_dai *codec_dai;
2340 for_each_rtd_codec_dai(rtd, j, codec_dai) {
2341 if (!codec_dai->active)
2342 pinctrl_pm_select_sleep_state(codec_dai->dev);
2345 if (!cpu_dai->active)
2346 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2353 * snd_soc_register_card - Register a card with the ASoC core
2355 * @card: Card to register
2358 int snd_soc_register_card(struct snd_soc_card *card)
2360 if (!card->name || !card->dev)
2363 dev_set_drvdata(card->dev, card);
2365 INIT_LIST_HEAD(&card->widgets);
2366 INIT_LIST_HEAD(&card->paths);
2367 INIT_LIST_HEAD(&card->dapm_list);
2368 INIT_LIST_HEAD(&card->aux_comp_list);
2369 INIT_LIST_HEAD(&card->component_dev_list);
2370 INIT_LIST_HEAD(&card->list);
2371 INIT_LIST_HEAD(&card->dai_link_list);
2372 INIT_LIST_HEAD(&card->rtd_list);
2373 INIT_LIST_HEAD(&card->dapm_dirty);
2374 INIT_LIST_HEAD(&card->dobj_list);
2377 card->instantiated = 0;
2378 mutex_init(&card->mutex);
2379 mutex_init(&card->dapm_mutex);
2380 mutex_init(&card->pcm_mutex);
2381 spin_lock_init(&card->dpcm_lock);
2383 return snd_soc_bind_card(card);
2385 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2387 static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2389 struct snd_soc_pcm_runtime *rtd;
2392 if (card->instantiated) {
2393 card->instantiated = false;
2394 snd_soc_dapm_shutdown(card);
2395 snd_soc_flush_all_delayed_work(card);
2397 /* remove all components used by DAI links on this card */
2398 for_each_comp_order(order) {
2399 for_each_card_rtds(card, rtd) {
2400 soc_remove_link_components(card, rtd, order);
2404 soc_cleanup_card_resources(card);
2406 list_add(&card->list, &unbind_card_list);
2409 list_del(&card->list);
2414 * snd_soc_unregister_card - Unregister a card with the ASoC core
2416 * @card: Card to unregister
2419 int snd_soc_unregister_card(struct snd_soc_card *card)
2421 mutex_lock(&client_mutex);
2422 snd_soc_unbind_card(card, true);
2423 mutex_unlock(&client_mutex);
2424 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2428 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2431 * Simplify DAI link configuration by removing ".-1" from device names
2432 * and sanitizing names.
2434 static char *fmt_single_name(struct device *dev, int *id)
2436 char *found, name[NAME_SIZE];
2439 if (dev_name(dev) == NULL)
2442 strlcpy(name, dev_name(dev), NAME_SIZE);
2444 /* are we a "%s.%d" name (platform and SPI components) */
2445 found = strstr(name, dev->driver->name);
2448 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2450 /* discard ID from name if ID == -1 */
2452 found[strlen(dev->driver->name)] = '\0';
2456 /* I2C component devices are named "bus-addr" */
2457 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2458 char tmp[NAME_SIZE];
2460 /* create unique ID number from I2C addr and bus */
2461 *id = ((id1 & 0xffff) << 16) + id2;
2463 /* sanitize component name for DAI link creation */
2464 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2466 strlcpy(name, tmp, NAME_SIZE);
2471 return kstrdup(name, GFP_KERNEL);
2475 * Simplify DAI link naming for single devices with multiple DAIs by removing
2476 * any ".-1" and using the DAI name (instead of device name).
2478 static inline char *fmt_multiple_name(struct device *dev,
2479 struct snd_soc_dai_driver *dai_drv)
2481 if (dai_drv->name == NULL) {
2483 "ASoC: error - multiple DAI %s registered with no name\n",
2488 return kstrdup(dai_drv->name, GFP_KERNEL);
2492 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2494 * @component: The component for which the DAIs should be unregistered
2496 static void snd_soc_unregister_dais(struct snd_soc_component *component)
2498 struct snd_soc_dai *dai, *_dai;
2500 for_each_component_dais_safe(component, dai, _dai) {
2501 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2503 list_del(&dai->list);
2509 /* Create a DAI and add it to the component's DAI list */
2510 static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2511 struct snd_soc_dai_driver *dai_drv,
2512 bool legacy_dai_naming)
2514 struct device *dev = component->dev;
2515 struct snd_soc_dai *dai;
2517 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2519 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2524 * Back in the old days when we still had component-less DAIs,
2525 * instead of having a static name, component-less DAIs would
2526 * inherit the name of the parent device so it is possible to
2527 * register multiple instances of the DAI. We still need to keep
2528 * the same naming style even though those DAIs are not
2529 * component-less anymore.
2531 if (legacy_dai_naming &&
2532 (dai_drv->id == 0 || dai_drv->name == NULL)) {
2533 dai->name = fmt_single_name(dev, &dai->id);
2535 dai->name = fmt_multiple_name(dev, dai_drv);
2537 dai->id = dai_drv->id;
2539 dai->id = component->num_dai;
2541 if (dai->name == NULL) {
2546 dai->component = component;
2548 dai->driver = dai_drv;
2549 if (!dai->driver->ops)
2550 dai->driver->ops = &null_dai_ops;
2552 /* see for_each_component_dais */
2553 list_add_tail(&dai->list, &component->dai_list);
2554 component->num_dai++;
2556 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2561 * snd_soc_register_dais - Register a DAI with the ASoC core
2563 * @component: The component the DAIs are registered for
2564 * @dai_drv: DAI driver to use for the DAIs
2565 * @count: Number of DAIs
2567 static int snd_soc_register_dais(struct snd_soc_component *component,
2568 struct snd_soc_dai_driver *dai_drv,
2571 struct device *dev = component->dev;
2572 struct snd_soc_dai *dai;
2576 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
2578 for (i = 0; i < count; i++) {
2580 dai = soc_add_dai(component, dai_drv + i, count == 1 &&
2581 !component->driver->non_legacy_dai_naming);
2591 snd_soc_unregister_dais(component);
2597 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2599 * @component: The component the DAIs are registered for
2600 * @dai_drv: DAI driver to use for the DAI
2602 * Topology can use this API to register DAIs when probing a component.
2603 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2604 * will be freed in the component cleanup.
2606 int snd_soc_register_dai(struct snd_soc_component *component,
2607 struct snd_soc_dai_driver *dai_drv)
2609 struct snd_soc_dapm_context *dapm =
2610 snd_soc_component_get_dapm(component);
2611 struct snd_soc_dai *dai;
2614 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
2615 dev_err(component->dev, "Invalid dai type %d\n",
2616 dai_drv->dobj.type);
2620 lockdep_assert_held(&client_mutex);
2621 dai = soc_add_dai(component, dai_drv, false);
2626 * Create the DAI widgets here. After adding DAIs, topology may
2627 * also add routes that need these widgets as source or sink.
2629 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
2631 dev_err(component->dev,
2632 "Failed to create DAI widgets %d\n", ret);
2637 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2639 static int snd_soc_component_initialize(struct snd_soc_component *component,
2640 const struct snd_soc_component_driver *driver, struct device *dev)
2642 struct snd_soc_dapm_context *dapm;
2644 component->name = fmt_single_name(dev, &component->id);
2645 if (!component->name) {
2646 dev_err(dev, "ASoC: Failed to allocate name\n");
2650 component->dev = dev;
2651 component->driver = driver;
2653 dapm = snd_soc_component_get_dapm(component);
2655 dapm->component = component;
2656 dapm->bias_level = SND_SOC_BIAS_OFF;
2657 dapm->idle_bias_off = !driver->idle_bias_on;
2658 dapm->suspend_bias_off = driver->suspend_bias_off;
2660 INIT_LIST_HEAD(&component->dai_list);
2661 mutex_init(&component->io_mutex);
2666 static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2668 int val_bytes = regmap_get_val_bytes(component->regmap);
2670 /* Errors are legitimate for non-integer byte multiples */
2672 component->val_bytes = val_bytes;
2675 #ifdef CONFIG_REGMAP
2678 * snd_soc_component_init_regmap() - Initialize regmap instance for the
2680 * @component: The component for which to initialize the regmap instance
2681 * @regmap: The regmap instance that should be used by the component
2683 * This function allows deferred assignment of the regmap instance that is
2684 * associated with the component. Only use this if the regmap instance is not
2685 * yet ready when the component is registered. The function must also be called
2686 * before the first IO attempt of the component.
2688 void snd_soc_component_init_regmap(struct snd_soc_component *component,
2689 struct regmap *regmap)
2691 component->regmap = regmap;
2692 snd_soc_component_setup_regmap(component);
2694 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
2697 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
2699 * @component: The component for which to de-initialize the regmap instance
2701 * Calls regmap_exit() on the regmap instance associated to the component and
2702 * removes the regmap instance from the component.
2704 * This function should only be used if snd_soc_component_init_regmap() was used
2705 * to initialize the regmap instance.
2707 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
2709 regmap_exit(component->regmap);
2710 component->regmap = NULL;
2712 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2716 static void snd_soc_component_add(struct snd_soc_component *component)
2718 mutex_lock(&client_mutex);
2720 if (!component->driver->write && !component->driver->read) {
2721 if (!component->regmap)
2722 component->regmap = dev_get_regmap(component->dev,
2724 if (component->regmap)
2725 snd_soc_component_setup_regmap(component);
2728 /* see for_each_component */
2729 list_add(&component->list, &component_list);
2730 INIT_LIST_HEAD(&component->dobj_list);
2732 mutex_unlock(&client_mutex);
2735 static void snd_soc_component_cleanup(struct snd_soc_component *component)
2737 snd_soc_unregister_dais(component);
2738 kfree(component->name);
2741 static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2743 struct snd_soc_card *card = component->card;
2746 snd_soc_unbind_card(card, false);
2748 list_del(&component->list);
2751 #define ENDIANNESS_MAP(name) \
2752 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2753 static u64 endianness_format_map[] = {
2754 ENDIANNESS_MAP(S16_),
2755 ENDIANNESS_MAP(U16_),
2756 ENDIANNESS_MAP(S24_),
2757 ENDIANNESS_MAP(U24_),
2758 ENDIANNESS_MAP(S32_),
2759 ENDIANNESS_MAP(U32_),
2760 ENDIANNESS_MAP(S24_3),
2761 ENDIANNESS_MAP(U24_3),
2762 ENDIANNESS_MAP(S20_3),
2763 ENDIANNESS_MAP(U20_3),
2764 ENDIANNESS_MAP(S18_3),
2765 ENDIANNESS_MAP(U18_3),
2766 ENDIANNESS_MAP(FLOAT_),
2767 ENDIANNESS_MAP(FLOAT64_),
2768 ENDIANNESS_MAP(IEC958_SUBFRAME_),
2772 * Fix up the DAI formats for endianness: codecs don't actually see
2773 * the endianness of the data but we're using the CPU format
2774 * definitions which do need to include endianness so we ensure that
2775 * codec DAIs always have both big and little endian variants set.
2777 static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2781 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2782 if (stream->formats & endianness_format_map[i])
2783 stream->formats |= endianness_format_map[i];
2786 static void snd_soc_try_rebind_card(void)
2788 struct snd_soc_card *card, *c;
2790 list_for_each_entry_safe(card, c, &unbind_card_list, list)
2791 if (!snd_soc_bind_card(card))
2792 list_del(&card->list);
2795 int snd_soc_add_component(struct device *dev,
2796 struct snd_soc_component *component,
2797 const struct snd_soc_component_driver *component_driver,
2798 struct snd_soc_dai_driver *dai_drv,
2804 ret = snd_soc_component_initialize(component, component_driver, dev);
2808 if (component_driver->endianness) {
2809 for (i = 0; i < num_dai; i++) {
2810 convert_endianness_formats(&dai_drv[i].playback);
2811 convert_endianness_formats(&dai_drv[i].capture);
2815 ret = snd_soc_register_dais(component, dai_drv, num_dai);
2817 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
2821 snd_soc_component_add(component);
2822 snd_soc_try_rebind_card();
2827 snd_soc_component_cleanup(component);
2831 EXPORT_SYMBOL_GPL(snd_soc_add_component);
2833 int snd_soc_register_component(struct device *dev,
2834 const struct snd_soc_component_driver *component_driver,
2835 struct snd_soc_dai_driver *dai_drv,
2838 struct snd_soc_component *component;
2840 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
2844 return snd_soc_add_component(dev, component, component_driver,
2847 EXPORT_SYMBOL_GPL(snd_soc_register_component);
2850 * snd_soc_unregister_component - Unregister all related component
2851 * from the ASoC core
2853 * @dev: The device to unregister
2855 static int __snd_soc_unregister_component(struct device *dev)
2857 struct snd_soc_component *component;
2860 mutex_lock(&client_mutex);
2861 for_each_component(component) {
2862 if (dev != component->dev)
2865 snd_soc_tplg_component_remove(component,
2866 SND_SOC_TPLG_INDEX_ALL);
2867 snd_soc_component_del_unlocked(component);
2871 mutex_unlock(&client_mutex);
2874 snd_soc_component_cleanup(component);
2879 void snd_soc_unregister_component(struct device *dev)
2881 while (__snd_soc_unregister_component(dev))
2884 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2886 struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
2887 const char *driver_name)
2889 struct snd_soc_component *component;
2890 struct snd_soc_component *ret;
2893 mutex_lock(&client_mutex);
2894 for_each_component(component) {
2895 if (dev != component->dev)
2899 (driver_name != component->driver->name) &&
2900 (strcmp(component->driver->name, driver_name) != 0))
2906 mutex_unlock(&client_mutex);
2910 EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
2912 /* Retrieve a card's name from device tree */
2913 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2914 const char *propname)
2916 struct device_node *np;
2920 pr_err("card->dev is not set before calling %s\n", __func__);
2924 np = card->dev->of_node;
2926 ret = of_property_read_string_index(np, propname, 0, &card->name);
2928 * EINVAL means the property does not exist. This is fine providing
2929 * card->name was previously set, which is checked later in
2930 * snd_soc_register_card.
2932 if (ret < 0 && ret != -EINVAL) {
2934 "ASoC: Property '%s' could not be read: %d\n",
2941 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2943 static const struct snd_soc_dapm_widget simple_widgets[] = {
2944 SND_SOC_DAPM_MIC("Microphone", NULL),
2945 SND_SOC_DAPM_LINE("Line", NULL),
2946 SND_SOC_DAPM_HP("Headphone", NULL),
2947 SND_SOC_DAPM_SPK("Speaker", NULL),
2950 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
2951 const char *propname)
2953 struct device_node *np = card->dev->of_node;
2954 struct snd_soc_dapm_widget *widgets;
2955 const char *template, *wname;
2956 int i, j, num_widgets, ret;
2958 num_widgets = of_property_count_strings(np, propname);
2959 if (num_widgets < 0) {
2961 "ASoC: Property '%s' does not exist\n", propname);
2964 if (num_widgets & 1) {
2966 "ASoC: Property '%s' length is not even\n", propname);
2972 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
2977 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
2981 "ASoC: Could not allocate memory for widgets\n");
2985 for (i = 0; i < num_widgets; i++) {
2986 ret = of_property_read_string_index(np, propname,
2990 "ASoC: Property '%s' index %d read error:%d\n",
2991 propname, 2 * i, ret);
2995 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
2996 if (!strncmp(template, simple_widgets[j].name,
2997 strlen(simple_widgets[j].name))) {
2998 widgets[i] = simple_widgets[j];
3003 if (j >= ARRAY_SIZE(simple_widgets)) {
3005 "ASoC: DAPM widget '%s' is not supported\n",
3010 ret = of_property_read_string_index(np, propname,
3015 "ASoC: Property '%s' index %d read error:%d\n",
3016 propname, (2 * i) + 1, ret);
3020 widgets[i].name = wname;
3023 card->of_dapm_widgets = widgets;
3024 card->num_of_dapm_widgets = num_widgets;
3028 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
3030 int snd_soc_of_get_slot_mask(struct device_node *np,
3031 const char *prop_name,
3035 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
3041 for (i = 0; i < val; i++)
3042 if (be32_to_cpup(&of_slot_mask[i]))
3047 EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
3049 int snd_soc_of_parse_tdm_slot(struct device_node *np,
3050 unsigned int *tx_mask,
3051 unsigned int *rx_mask,
3052 unsigned int *slots,
3053 unsigned int *slot_width)
3059 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3061 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3063 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3064 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3072 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3073 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3083 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3085 void snd_soc_of_parse_node_prefix(struct device_node *np,
3086 struct snd_soc_codec_conf *codec_conf,
3087 struct device_node *of_node,
3088 const char *propname)
3093 ret = of_property_read_string(np, propname, &str);
3095 /* no prefix is not error */
3099 codec_conf->of_node = of_node;
3100 codec_conf->name_prefix = str;
3102 EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
3104 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3105 const char *propname)
3107 struct device_node *np = card->dev->of_node;
3109 struct snd_soc_dapm_route *routes;
3112 num_routes = of_property_count_strings(np, propname);
3113 if (num_routes < 0 || num_routes & 1) {
3115 "ASoC: Property '%s' does not exist or its length is not even\n",
3121 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3126 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
3130 "ASoC: Could not allocate DAPM route table\n");
3134 for (i = 0; i < num_routes; i++) {
3135 ret = of_property_read_string_index(np, propname,
3136 2 * i, &routes[i].sink);
3139 "ASoC: Property '%s' index %d could not be read: %d\n",
3140 propname, 2 * i, ret);
3143 ret = of_property_read_string_index(np, propname,
3144 (2 * i) + 1, &routes[i].source);
3147 "ASoC: Property '%s' index %d could not be read: %d\n",
3148 propname, (2 * i) + 1, ret);
3153 card->num_of_dapm_routes = num_routes;
3154 card->of_dapm_routes = routes;
3158 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3160 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3162 struct device_node **bitclkmaster,
3163 struct device_node **framemaster)
3167 unsigned int format = 0;
3173 } of_fmt_table[] = {
3174 { "i2s", SND_SOC_DAIFMT_I2S },
3175 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3176 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3177 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3178 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3179 { "ac97", SND_SOC_DAIFMT_AC97 },
3180 { "pdm", SND_SOC_DAIFMT_PDM},
3181 { "msb", SND_SOC_DAIFMT_MSB },
3182 { "lsb", SND_SOC_DAIFMT_LSB },
3189 * check "dai-format = xxx"
3190 * or "[prefix]format = xxx"
3191 * SND_SOC_DAIFMT_FORMAT_MASK area
3193 ret = of_property_read_string(np, "dai-format", &str);
3195 snprintf(prop, sizeof(prop), "%sformat", prefix);
3196 ret = of_property_read_string(np, prop, &str);
3199 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3200 if (strcmp(str, of_fmt_table[i].name) == 0) {
3201 format |= of_fmt_table[i].val;
3208 * check "[prefix]continuous-clock"
3209 * SND_SOC_DAIFMT_CLOCK_MASK area
3211 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
3212 if (of_property_read_bool(np, prop))
3213 format |= SND_SOC_DAIFMT_CONT;
3215 format |= SND_SOC_DAIFMT_GATED;
3218 * check "[prefix]bitclock-inversion"
3219 * check "[prefix]frame-inversion"
3220 * SND_SOC_DAIFMT_INV_MASK area
3222 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3223 bit = !!of_get_property(np, prop, NULL);
3225 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3226 frame = !!of_get_property(np, prop, NULL);
3228 switch ((bit << 4) + frame) {
3230 format |= SND_SOC_DAIFMT_IB_IF;
3233 format |= SND_SOC_DAIFMT_IB_NF;
3236 format |= SND_SOC_DAIFMT_NB_IF;
3239 /* SND_SOC_DAIFMT_NB_NF is default */
3244 * check "[prefix]bitclock-master"
3245 * check "[prefix]frame-master"
3246 * SND_SOC_DAIFMT_MASTER_MASK area
3248 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3249 bit = !!of_get_property(np, prop, NULL);
3250 if (bit && bitclkmaster)
3251 *bitclkmaster = of_parse_phandle(np, prop, 0);
3253 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3254 frame = !!of_get_property(np, prop, NULL);
3255 if (frame && framemaster)
3256 *framemaster = of_parse_phandle(np, prop, 0);
3258 switch ((bit << 4) + frame) {
3260 format |= SND_SOC_DAIFMT_CBM_CFM;
3263 format |= SND_SOC_DAIFMT_CBM_CFS;
3266 format |= SND_SOC_DAIFMT_CBS_CFM;
3269 format |= SND_SOC_DAIFMT_CBS_CFS;
3275 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3277 int snd_soc_get_dai_id(struct device_node *ep)
3279 struct snd_soc_component *component;
3280 struct snd_soc_dai_link_component dlc;
3283 dlc.of_node = of_graph_get_port_parent(ep);
3286 * For example HDMI case, HDMI has video/sound port,
3287 * but ALSA SoC needs sound port number only.
3288 * Thus counting HDMI DT port/endpoint doesn't work.
3289 * Then, it should have .of_xlate_dai_id
3292 mutex_lock(&client_mutex);
3293 component = soc_find_component(&dlc);
3295 ret = snd_soc_component_of_xlate_dai_id(component, ep);
3296 mutex_unlock(&client_mutex);
3298 of_node_put(dlc.of_node);
3302 EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3304 int snd_soc_get_dai_name(struct of_phandle_args *args,
3305 const char **dai_name)
3307 struct snd_soc_component *pos;
3308 struct device_node *component_of_node;
3309 int ret = -EPROBE_DEFER;
3311 mutex_lock(&client_mutex);
3312 for_each_component(pos) {
3313 component_of_node = soc_component_to_node(pos);
3315 if (component_of_node != args->np)
3318 ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3319 if (ret == -ENOTSUPP) {
3320 struct snd_soc_dai *dai;
3323 switch (args->args_count) {
3325 id = 0; /* same as dai_drv[0] */
3335 if (id < 0 || id >= pos->num_dai) {
3342 /* find target DAI */
3343 for_each_component_dais(pos, dai) {
3349 *dai_name = dai->driver->name;
3351 *dai_name = pos->name;
3356 mutex_unlock(&client_mutex);
3359 EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
3361 int snd_soc_of_get_dai_name(struct device_node *of_node,
3362 const char **dai_name)
3364 struct of_phandle_args args;
3367 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3368 "#sound-dai-cells", 0, &args);
3372 ret = snd_soc_get_dai_name(&args, dai_name);
3374 of_node_put(args.np);
3378 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3381 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3382 * @dai_link: DAI link
3384 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3386 void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3388 struct snd_soc_dai_link_component *component;
3391 for_each_link_codecs(dai_link, index, component) {
3392 if (!component->of_node)
3394 of_node_put(component->of_node);
3395 component->of_node = NULL;
3398 EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3401 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3403 * @of_node: Device node
3404 * @dai_link: DAI link
3406 * Builds an array of CODEC DAI components from the DAI link property
3408 * The array is set in the DAI link and the number of DAIs is set accordingly.
3409 * The device nodes in the array (of_node) must be dereferenced by calling
3410 * snd_soc_of_put_dai_link_codecs() on @dai_link.
3412 * Returns 0 for success
3414 int snd_soc_of_get_dai_link_codecs(struct device *dev,
3415 struct device_node *of_node,
3416 struct snd_soc_dai_link *dai_link)
3418 struct of_phandle_args args;
3419 struct snd_soc_dai_link_component *component;
3421 int index, num_codecs, ret;
3423 /* Count the number of CODECs */
3425 num_codecs = of_count_phandle_with_args(of_node, name,
3426 "#sound-dai-cells");
3427 if (num_codecs <= 0) {
3428 if (num_codecs == -ENOENT)
3429 dev_err(dev, "No 'sound-dai' property\n");
3431 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3434 component = devm_kcalloc(dev,
3435 num_codecs, sizeof(*component),
3439 dai_link->codecs = component;
3440 dai_link->num_codecs = num_codecs;
3442 /* Parse the list */
3443 for_each_link_codecs(dai_link, index, component) {
3444 ret = of_parse_phandle_with_args(of_node, name,
3449 component->of_node = args.np;
3450 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3456 snd_soc_of_put_dai_link_codecs(dai_link);
3457 dai_link->codecs = NULL;
3458 dai_link->num_codecs = 0;
3461 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3463 static int __init snd_soc_init(void)
3465 snd_soc_debugfs_init();
3466 snd_soc_util_init();
3468 return platform_driver_register(&soc_driver);
3470 module_init(snd_soc_init);
3472 static void __exit snd_soc_exit(void)
3474 snd_soc_util_exit();
3475 snd_soc_debugfs_exit();
3477 platform_driver_unregister(&soc_driver);
3479 module_exit(snd_soc_exit);
3481 /* Module information */
3482 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3483 MODULE_DESCRIPTION("ALSA SoC Core");
3484 MODULE_LICENSE("GPL");
3485 MODULE_ALIAS("platform:soc-audio");