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);
57 * This is a timeout to do a DAPM powerdown after a stream is closed().
58 * It can be used to eliminate pops between different playback streams, e.g.
59 * between two audio tracks.
61 static int pmdown_time = 5000;
62 module_param(pmdown_time, int, 0);
63 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
65 /* If a DMI filed contain strings in this blacklist (e.g.
66 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
67 * as invalid and dropped when setting the card long name from DMI info.
69 static const char * const dmi_blacklist[] = {
70 "To be filled by OEM",
76 NULL, /* terminator */
79 static ssize_t pmdown_time_show(struct device *dev,
80 struct device_attribute *attr, char *buf)
82 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
84 return sprintf(buf, "%ld\n", rtd->pmdown_time);
87 static ssize_t pmdown_time_set(struct device *dev,
88 struct device_attribute *attr,
89 const char *buf, size_t count)
91 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
94 ret = kstrtol(buf, 10, &rtd->pmdown_time);
101 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
103 static struct attribute *soc_dev_attrs[] = {
104 &dev_attr_pmdown_time.attr,
108 static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
109 struct attribute *attr, int idx)
111 struct device *dev = kobj_to_dev(kobj);
112 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
114 if (attr == &dev_attr_pmdown_time.attr)
115 return attr->mode; /* always visible */
116 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
119 static const struct attribute_group soc_dapm_dev_group = {
120 .attrs = soc_dapm_dev_attrs,
121 .is_visible = soc_dev_attr_is_visible,
124 static const struct attribute_group soc_dev_group = {
125 .attrs = soc_dev_attrs,
126 .is_visible = soc_dev_attr_is_visible,
129 static const struct attribute_group *soc_dev_attr_groups[] = {
135 #ifdef CONFIG_DEBUG_FS
136 static void soc_init_component_debugfs(struct snd_soc_component *component)
138 if (!component->card->debugfs_card_root)
141 if (component->debugfs_prefix) {
144 name = kasprintf(GFP_KERNEL, "%s:%s",
145 component->debugfs_prefix, component->name);
147 component->debugfs_root = debugfs_create_dir(name,
148 component->card->debugfs_card_root);
152 component->debugfs_root = debugfs_create_dir(component->name,
153 component->card->debugfs_card_root);
156 if (!component->debugfs_root) {
157 dev_warn(component->dev,
158 "ASoC: Failed to create component debugfs directory\n");
162 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
163 component->debugfs_root);
166 static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
168 debugfs_remove_recursive(component->debugfs_root);
171 static int dai_list_show(struct seq_file *m, void *v)
173 struct snd_soc_component *component;
174 struct snd_soc_dai *dai;
176 mutex_lock(&client_mutex);
178 list_for_each_entry(component, &component_list, list)
179 list_for_each_entry(dai, &component->dai_list, list)
180 seq_printf(m, "%s\n", dai->name);
182 mutex_unlock(&client_mutex);
186 DEFINE_SHOW_ATTRIBUTE(dai_list);
188 static int component_list_show(struct seq_file *m, void *v)
190 struct snd_soc_component *component;
192 mutex_lock(&client_mutex);
194 list_for_each_entry(component, &component_list, list)
195 seq_printf(m, "%s\n", component->name);
197 mutex_unlock(&client_mutex);
201 DEFINE_SHOW_ATTRIBUTE(component_list);
203 static void soc_init_card_debugfs(struct snd_soc_card *card)
205 if (!snd_soc_debugfs_root)
208 card->debugfs_card_root = debugfs_create_dir(card->name,
209 snd_soc_debugfs_root);
210 if (!card->debugfs_card_root) {
212 "ASoC: Failed to create card debugfs directory\n");
216 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
217 card->debugfs_card_root,
219 if (!card->debugfs_pop_time)
221 "ASoC: Failed to create pop time debugfs file\n");
224 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
226 debugfs_remove_recursive(card->debugfs_card_root);
229 static void snd_soc_debugfs_init(void)
231 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
232 if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) {
233 pr_warn("ASoC: Failed to create debugfs directory\n");
234 snd_soc_debugfs_root = NULL;
238 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
240 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
242 if (!debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
243 &component_list_fops))
244 pr_warn("ASoC: Failed to create component list debugfs file\n");
247 static void snd_soc_debugfs_exit(void)
249 debugfs_remove_recursive(snd_soc_debugfs_root);
254 static inline void soc_init_component_debugfs(
255 struct snd_soc_component *component)
259 static inline void soc_cleanup_component_debugfs(
260 struct snd_soc_component *component)
264 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
268 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
272 static inline void snd_soc_debugfs_init(void)
276 static inline void snd_soc_debugfs_exit(void)
282 static int snd_soc_card_comp_compare(struct device *dev, void *data)
284 struct snd_soc_component *component;
286 lockdep_assert_held(&client_mutex);
287 list_for_each_entry(component, &component_list, list) {
288 if (dev == component->dev) {
289 if (!strcmp(component->name, data))
298 static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
299 struct snd_soc_component *component)
301 struct snd_soc_rtdcom_list *rtdcom;
302 struct snd_soc_rtdcom_list *new_rtdcom;
305 for_each_rtdcom(rtd, rtdcom) {
306 /* already connected */
307 if (rtdcom->component == component)
311 new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
315 new_rtdcom->component = component;
316 INIT_LIST_HEAD(&new_rtdcom->list);
318 list_add_tail(&new_rtdcom->list, &rtd->component_list);
320 if (rtd->card->auto_bind && !rtd->card->components_added) {
321 cname = devm_kasprintf(rtd->card->dev, GFP_KERNEL,
322 "%s", component->name);
323 component_match_add(rtd->card->dev, &rtd->card->match,
324 snd_soc_card_comp_compare, cname);
330 static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
332 struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
334 for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
337 INIT_LIST_HEAD(&rtd->component_list);
340 struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
341 const char *driver_name)
343 struct snd_soc_rtdcom_list *rtdcom;
348 for_each_rtdcom(rtd, rtdcom) {
349 const char *component_name = rtdcom->component->driver->name;
354 if ((component_name == driver_name) ||
355 strcmp(component_name, driver_name) == 0)
356 return rtdcom->component;
361 EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
363 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
364 const char *dai_link, int stream)
366 struct snd_soc_pcm_runtime *rtd;
368 list_for_each_entry(rtd, &card->rtd_list, list) {
369 if (rtd->dai_link->no_pcm &&
370 !strcmp(rtd->dai_link->name, dai_link))
371 return rtd->pcm->streams[stream].substream;
373 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
376 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
378 static const struct snd_soc_ops null_snd_soc_ops;
380 static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
381 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
383 struct snd_soc_pcm_runtime *rtd;
385 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
389 INIT_LIST_HEAD(&rtd->component_list);
391 rtd->dai_link = dai_link;
392 if (!rtd->dai_link->ops)
393 rtd->dai_link->ops = &null_snd_soc_ops;
395 rtd->codec_dais = kcalloc(dai_link->num_codecs,
396 sizeof(struct snd_soc_dai *),
398 if (!rtd->codec_dais) {
406 static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
408 kfree(rtd->codec_dais);
409 snd_soc_rtdcom_del_all(rtd);
413 static void soc_add_pcm_runtime(struct snd_soc_card *card,
414 struct snd_soc_pcm_runtime *rtd)
416 list_add_tail(&rtd->list, &card->rtd_list);
417 rtd->num = card->num_rtd;
421 static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
423 struct snd_soc_pcm_runtime *rtd, *_rtd;
425 list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) {
426 list_del(&rtd->list);
427 soc_free_pcm_runtime(rtd);
433 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
434 const char *dai_link)
436 struct snd_soc_pcm_runtime *rtd;
438 list_for_each_entry(rtd, &card->rtd_list, list) {
439 if (!strcmp(rtd->dai_link->name, dai_link))
442 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
445 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
447 static void codec2codec_close_delayed_work(struct work_struct *work)
449 /* Currently nothing to do for c2c links
450 * Since c2c links are internal nodes in the DAPM graph and
451 * don't interface with the outside world or application layer
452 * we don't have to do any special handling on close.
456 #ifdef CONFIG_PM_SLEEP
457 /* powers down audio subsystem for suspend */
458 int snd_soc_suspend(struct device *dev)
460 struct snd_soc_card *card = dev_get_drvdata(dev);
461 struct snd_soc_component *component;
462 struct snd_soc_pcm_runtime *rtd;
465 /* If the card is not initialized yet there is nothing to do */
466 if (!card->instantiated)
469 /* Due to the resume being scheduled into a workqueue we could
470 * suspend before that's finished - wait for it to complete.
472 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
474 /* we're going to block userspace touching us until resume completes */
475 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
477 /* mute any active DACs */
478 list_for_each_entry(rtd, &card->rtd_list, list) {
480 if (rtd->dai_link->ignore_suspend)
483 for (i = 0; i < rtd->num_codecs; i++) {
484 struct snd_soc_dai *dai = rtd->codec_dais[i];
485 struct snd_soc_dai_driver *drv = dai->driver;
487 if (drv->ops->digital_mute && dai->playback_active)
488 drv->ops->digital_mute(dai, 1);
492 /* suspend all pcms */
493 list_for_each_entry(rtd, &card->rtd_list, list) {
494 if (rtd->dai_link->ignore_suspend)
497 snd_pcm_suspend_all(rtd->pcm);
500 if (card->suspend_pre)
501 card->suspend_pre(card);
503 list_for_each_entry(rtd, &card->rtd_list, list) {
504 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
506 if (rtd->dai_link->ignore_suspend)
509 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
510 cpu_dai->driver->suspend(cpu_dai);
513 /* close any waiting streams */
514 list_for_each_entry(rtd, &card->rtd_list, list)
515 flush_delayed_work(&rtd->delayed_work);
517 list_for_each_entry(rtd, &card->rtd_list, list) {
519 if (rtd->dai_link->ignore_suspend)
522 snd_soc_dapm_stream_event(rtd,
523 SNDRV_PCM_STREAM_PLAYBACK,
524 SND_SOC_DAPM_STREAM_SUSPEND);
526 snd_soc_dapm_stream_event(rtd,
527 SNDRV_PCM_STREAM_CAPTURE,
528 SND_SOC_DAPM_STREAM_SUSPEND);
531 /* Recheck all endpoints too, their state is affected by suspend */
532 dapm_mark_endpoints_dirty(card);
533 snd_soc_dapm_sync(&card->dapm);
535 /* suspend all COMPONENTs */
536 list_for_each_entry(component, &card->component_dev_list, card_list) {
537 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
539 /* If there are paths active then the COMPONENT will be held with
540 * bias _ON and should not be suspended. */
541 if (!component->suspended) {
542 switch (snd_soc_dapm_get_bias_level(dapm)) {
543 case SND_SOC_BIAS_STANDBY:
545 * If the COMPONENT is capable of idle
546 * bias off then being in STANDBY
547 * means it's doing something,
548 * otherwise fall through.
550 if (dapm->idle_bias_off) {
551 dev_dbg(component->dev,
552 "ASoC: idle_bias_off CODEC on over suspend\n");
556 case SND_SOC_BIAS_OFF:
557 if (component->driver->suspend)
558 component->driver->suspend(component);
559 component->suspended = 1;
560 if (component->regmap)
561 regcache_mark_dirty(component->regmap);
562 /* deactivate pins to sleep state */
563 pinctrl_pm_select_sleep_state(component->dev);
566 dev_dbg(component->dev,
567 "ASoC: COMPONENT is on over suspend\n");
573 list_for_each_entry(rtd, &card->rtd_list, list) {
574 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
576 if (rtd->dai_link->ignore_suspend)
579 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
580 cpu_dai->driver->suspend(cpu_dai);
582 /* deactivate pins to sleep state */
583 pinctrl_pm_select_sleep_state(cpu_dai->dev);
586 if (card->suspend_post)
587 card->suspend_post(card);
591 EXPORT_SYMBOL_GPL(snd_soc_suspend);
593 /* deferred resume work, so resume can complete before we finished
594 * setting our codec back up, which can be very slow on I2C
596 static void soc_resume_deferred(struct work_struct *work)
598 struct snd_soc_card *card =
599 container_of(work, struct snd_soc_card, deferred_resume_work);
600 struct snd_soc_pcm_runtime *rtd;
601 struct snd_soc_component *component;
604 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
605 * so userspace apps are blocked from touching us
608 dev_dbg(card->dev, "ASoC: starting resume work\n");
610 /* Bring us up into D2 so that DAPM starts enabling things */
611 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
613 if (card->resume_pre)
614 card->resume_pre(card);
616 /* resume control bus DAIs */
617 list_for_each_entry(rtd, &card->rtd_list, list) {
618 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
620 if (rtd->dai_link->ignore_suspend)
623 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
624 cpu_dai->driver->resume(cpu_dai);
627 list_for_each_entry(component, &card->component_dev_list, card_list) {
628 if (component->suspended) {
629 if (component->driver->resume)
630 component->driver->resume(component);
631 component->suspended = 0;
635 list_for_each_entry(rtd, &card->rtd_list, list) {
637 if (rtd->dai_link->ignore_suspend)
640 snd_soc_dapm_stream_event(rtd,
641 SNDRV_PCM_STREAM_PLAYBACK,
642 SND_SOC_DAPM_STREAM_RESUME);
644 snd_soc_dapm_stream_event(rtd,
645 SNDRV_PCM_STREAM_CAPTURE,
646 SND_SOC_DAPM_STREAM_RESUME);
649 /* unmute any active DACs */
650 list_for_each_entry(rtd, &card->rtd_list, list) {
652 if (rtd->dai_link->ignore_suspend)
655 for (i = 0; i < rtd->num_codecs; i++) {
656 struct snd_soc_dai *dai = rtd->codec_dais[i];
657 struct snd_soc_dai_driver *drv = dai->driver;
659 if (drv->ops->digital_mute && dai->playback_active)
660 drv->ops->digital_mute(dai, 0);
664 list_for_each_entry(rtd, &card->rtd_list, list) {
665 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
667 if (rtd->dai_link->ignore_suspend)
670 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
671 cpu_dai->driver->resume(cpu_dai);
674 if (card->resume_post)
675 card->resume_post(card);
677 dev_dbg(card->dev, "ASoC: resume work completed\n");
679 /* Recheck all endpoints too, their state is affected by suspend */
680 dapm_mark_endpoints_dirty(card);
681 snd_soc_dapm_sync(&card->dapm);
683 /* userspace can access us now we are back as we were before */
684 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
687 /* powers up audio subsystem after a suspend */
688 int snd_soc_resume(struct device *dev)
690 struct snd_soc_card *card = dev_get_drvdata(dev);
691 bool bus_control = false;
692 struct snd_soc_pcm_runtime *rtd;
694 /* If the card is not initialized yet there is nothing to do */
695 if (!card->instantiated)
698 /* activate pins from sleep state */
699 list_for_each_entry(rtd, &card->rtd_list, list) {
700 struct snd_soc_dai **codec_dais = rtd->codec_dais;
701 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
705 pinctrl_pm_select_default_state(cpu_dai->dev);
707 for (j = 0; j < rtd->num_codecs; j++) {
708 struct snd_soc_dai *codec_dai = codec_dais[j];
709 if (codec_dai->active)
710 pinctrl_pm_select_default_state(codec_dai->dev);
715 * DAIs that also act as the control bus master might have other drivers
716 * hanging off them so need to resume immediately. Other drivers don't
717 * have that problem and may take a substantial amount of time to resume
718 * due to I/O costs and anti-pop so handle them out of line.
720 list_for_each_entry(rtd, &card->rtd_list, list) {
721 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
722 bus_control |= cpu_dai->driver->bus_control;
725 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
726 soc_resume_deferred(&card->deferred_resume_work);
728 dev_dbg(dev, "ASoC: Scheduling resume work\n");
729 if (!schedule_work(&card->deferred_resume_work))
730 dev_err(dev, "ASoC: resume work item may be lost\n");
735 EXPORT_SYMBOL_GPL(snd_soc_resume);
737 #define snd_soc_suspend NULL
738 #define snd_soc_resume NULL
741 static const struct snd_soc_dai_ops null_dai_ops = {
744 static struct snd_soc_component *soc_find_component(
745 const struct device_node *of_node, const char *name)
747 struct snd_soc_component *component;
749 lockdep_assert_held(&client_mutex);
751 list_for_each_entry(component, &component_list, list) {
753 if (component->dev->of_node == of_node)
755 } else if (strcmp(component->name, name) == 0) {
764 * snd_soc_find_dai - Find a registered DAI
766 * @dlc: name of the DAI or the DAI driver and optional component info to match
768 * This function will search all registered components and their DAIs to
769 * find the DAI of the same name. The component's of_node and name
770 * should also match if being specified.
772 * Return: pointer of DAI, or NULL if not found.
774 struct snd_soc_dai *snd_soc_find_dai(
775 const struct snd_soc_dai_link_component *dlc)
777 struct snd_soc_component *component;
778 struct snd_soc_dai *dai;
779 struct device_node *component_of_node;
781 lockdep_assert_held(&client_mutex);
783 /* Find CPU DAI from registered DAIs*/
784 list_for_each_entry(component, &component_list, list) {
785 component_of_node = component->dev->of_node;
786 if (!component_of_node && component->dev->parent)
787 component_of_node = component->dev->parent->of_node;
789 if (dlc->of_node && component_of_node != dlc->of_node)
791 if (dlc->name && strcmp(component->name, dlc->name))
793 list_for_each_entry(dai, &component->dai_list, list) {
794 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
795 && (!dai->driver->name
796 || strcmp(dai->driver->name, dlc->dai_name)))
805 EXPORT_SYMBOL_GPL(snd_soc_find_dai);
809 * snd_soc_find_dai_link - Find a DAI link
812 * @id: DAI link ID to match
813 * @name: DAI link name to match, optional
814 * @stream_name: DAI link stream name to match, optional
816 * This function will search all existing DAI links of the soc card to
817 * find the link of the same ID. Since DAI links may not have their
818 * unique ID, so name and stream name should also match if being
821 * Return: pointer of DAI link, or NULL if not found.
823 struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
824 int id, const char *name,
825 const char *stream_name)
827 struct snd_soc_dai_link *link, *_link;
829 lockdep_assert_held(&client_mutex);
831 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
835 if (name && (!link->name || strcmp(name, link->name)))
838 if (stream_name && (!link->stream_name
839 || strcmp(stream_name, link->stream_name)))
847 EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
849 static bool soc_is_dai_link_bound(struct snd_soc_card *card,
850 struct snd_soc_dai_link *dai_link)
852 struct snd_soc_pcm_runtime *rtd;
854 list_for_each_entry(rtd, &card->rtd_list, list) {
855 if (rtd->dai_link == dai_link)
862 static int snd_soc_card_comp_bind(struct device *dev)
864 struct snd_soc_card *card = dev_get_drvdata(dev);
866 if (card->instantiated)
869 return snd_soc_register_card(card);
872 static void snd_soc_card_comp_unbind(struct device *dev)
876 static const struct component_master_ops snd_soc_card_comp_ops = {
877 .bind = snd_soc_card_comp_bind,
878 .unbind = snd_soc_card_comp_unbind,
881 static int soc_bind_dai_link(struct snd_soc_card *card,
882 struct snd_soc_dai_link *dai_link)
884 struct snd_soc_pcm_runtime *rtd;
885 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
886 struct snd_soc_dai_link_component cpu_dai_component;
887 struct snd_soc_component *component;
888 struct snd_soc_dai **codec_dais;
889 struct device_node *platform_of_node;
890 const char *platform_name;
893 if (dai_link->ignore)
896 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
898 if (soc_is_dai_link_bound(card, dai_link)) {
899 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
904 rtd = soc_new_pcm_runtime(card, dai_link);
908 cpu_dai_component.name = dai_link->cpu_name;
909 cpu_dai_component.of_node = dai_link->cpu_of_node;
910 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
911 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
913 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
914 dai_link->cpu_dai_name);
917 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
919 rtd->num_codecs = dai_link->num_codecs;
921 /* Find CODEC from registered CODECs */
922 codec_dais = rtd->codec_dais;
923 for (i = 0; i < rtd->num_codecs; i++) {
924 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
925 if (!codec_dais[i]) {
926 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
930 snd_soc_rtdcom_add(rtd, codec_dais[i]->component);
933 /* Single codec links expect codec and codec_dai in runtime data */
934 rtd->codec_dai = codec_dais[0];
936 /* if there's no platform we match on the empty platform */
937 platform_name = dai_link->platform_name;
938 if (!platform_name && !dai_link->platform_of_node)
939 platform_name = "snd-soc-dummy";
941 /* find one from the set of registered platforms */
942 list_for_each_entry(component, &component_list, list) {
943 platform_of_node = component->dev->of_node;
944 if (!platform_of_node && component->dev->parent->of_node)
945 platform_of_node = component->dev->parent->of_node;
947 if (dai_link->platform_of_node) {
948 if (platform_of_node != dai_link->platform_of_node)
951 if (strcmp(component->name, platform_name))
955 snd_soc_rtdcom_add(rtd, component);
958 soc_add_pcm_runtime(card, rtd);
962 soc_free_pcm_runtime(rtd);
963 return -EPROBE_DEFER;
966 static void soc_remove_component(struct snd_soc_component *component)
968 if (!component->card)
971 list_del(&component->card_list);
973 if (component->driver->remove)
974 component->driver->remove(component);
976 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
978 soc_cleanup_component_debugfs(component);
979 component->card = NULL;
980 module_put(component->dev->driver->owner);
983 static void soc_remove_dai(struct snd_soc_dai *dai, int order)
987 if (dai && dai->probed &&
988 dai->driver->remove_order == order) {
989 if (dai->driver->remove) {
990 err = dai->driver->remove(dai);
993 "ASoC: failed to remove %s: %d\n",
1000 static void soc_remove_link_dais(struct snd_soc_card *card,
1001 struct snd_soc_pcm_runtime *rtd, int order)
1005 /* unregister the rtd device */
1006 if (rtd->dev_registered) {
1007 device_unregister(rtd->dev);
1008 rtd->dev_registered = 0;
1011 /* remove the CODEC DAI */
1012 for (i = 0; i < rtd->num_codecs; i++)
1013 soc_remove_dai(rtd->codec_dais[i], order);
1015 soc_remove_dai(rtd->cpu_dai, order);
1018 static void soc_remove_link_components(struct snd_soc_card *card,
1019 struct snd_soc_pcm_runtime *rtd, int order)
1021 struct snd_soc_component *component;
1022 struct snd_soc_rtdcom_list *rtdcom;
1024 for_each_rtdcom(rtd, rtdcom) {
1025 component = rtdcom->component;
1027 if (component->driver->remove_order == order)
1028 soc_remove_component(component);
1032 static void soc_remove_dai_links(struct snd_soc_card *card)
1035 struct snd_soc_pcm_runtime *rtd;
1036 struct snd_soc_dai_link *link, *_link;
1038 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1040 list_for_each_entry(rtd, &card->rtd_list, list)
1041 soc_remove_link_dais(card, rtd, order);
1044 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1046 list_for_each_entry(rtd, &card->rtd_list, list)
1047 soc_remove_link_components(card, rtd, order);
1050 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1051 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1052 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1055 list_del(&link->list);
1056 card->num_dai_links--;
1060 static int snd_soc_init_multicodec(struct snd_soc_card *card,
1061 struct snd_soc_dai_link *dai_link)
1063 /* Legacy codec/codec_dai link is a single entry in multicodec */
1064 if (dai_link->codec_name || dai_link->codec_of_node ||
1065 dai_link->codec_dai_name) {
1066 dai_link->num_codecs = 1;
1068 dai_link->codecs = devm_kzalloc(card->dev,
1069 sizeof(struct snd_soc_dai_link_component),
1071 if (!dai_link->codecs)
1074 dai_link->codecs[0].name = dai_link->codec_name;
1075 dai_link->codecs[0].of_node = dai_link->codec_of_node;
1076 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1079 if (!dai_link->codecs) {
1080 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1087 static int soc_init_dai_link(struct snd_soc_card *card,
1088 struct snd_soc_dai_link *link)
1092 ret = snd_soc_init_multicodec(card, link);
1094 dev_err(card->dev, "ASoC: failed to init multicodec\n");
1098 for (i = 0; i < link->num_codecs; i++) {
1100 * Codec must be specified by 1 of name or OF node,
1101 * not both or neither.
1103 if (!!link->codecs[i].name ==
1104 !!link->codecs[i].of_node) {
1105 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1109 /* Codec DAI name must be specified */
1110 if (!link->codecs[i].dai_name) {
1111 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1118 * Platform may be specified by either name or OF node, but
1119 * can be left unspecified, and a dummy platform will be used.
1121 if (link->platform_name && link->platform_of_node) {
1123 "ASoC: Both platform name/of_node are set for %s\n",
1129 * CPU device may be specified by either name or OF node, but
1130 * can be left unspecified, and will be matched based on DAI
1133 if (link->cpu_name && link->cpu_of_node) {
1135 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1140 * At least one of CPU DAI name or CPU device name/node must be
1143 if (!link->cpu_dai_name &&
1144 !(link->cpu_name || link->cpu_of_node)) {
1146 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1154 void snd_soc_disconnect_sync(struct device *dev)
1156 struct snd_soc_component *component = snd_soc_lookup_component(dev, NULL);
1158 if (!component || !component->card)
1161 snd_card_disconnect_sync(component->card->snd_card);
1163 EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
1166 * snd_soc_add_dai_link - Add a DAI link dynamically
1167 * @card: The ASoC card to which the DAI link is added
1168 * @dai_link: The new DAI link to add
1170 * This function adds a DAI link to the ASoC card's link list.
1172 * Note: Topology can use this API to add DAI links when probing the
1173 * topology component. And machine drivers can still define static
1174 * DAI links in dai_link array.
1176 int snd_soc_add_dai_link(struct snd_soc_card *card,
1177 struct snd_soc_dai_link *dai_link)
1179 if (dai_link->dobj.type
1180 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1181 dev_err(card->dev, "Invalid dai link type %d\n",
1182 dai_link->dobj.type);
1186 lockdep_assert_held(&client_mutex);
1187 /* Notify the machine driver for extra initialization
1188 * on the link created by topology.
1190 if (dai_link->dobj.type && card->add_dai_link)
1191 card->add_dai_link(card, dai_link);
1193 list_add_tail(&dai_link->list, &card->dai_link_list);
1194 card->num_dai_links++;
1198 EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1201 * snd_soc_remove_dai_link - Remove a DAI link from the list
1202 * @card: The ASoC card that owns the link
1203 * @dai_link: The DAI link to remove
1205 * This function removes a DAI link from the ASoC card's link list.
1207 * For DAI links previously added by topology, topology should
1208 * remove them by using the dobj embedded in the link.
1210 void snd_soc_remove_dai_link(struct snd_soc_card *card,
1211 struct snd_soc_dai_link *dai_link)
1213 struct snd_soc_dai_link *link, *_link;
1215 if (dai_link->dobj.type
1216 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1217 dev_err(card->dev, "Invalid dai link type %d\n",
1218 dai_link->dobj.type);
1222 lockdep_assert_held(&client_mutex);
1223 /* Notify the machine driver for extra destruction
1224 * on the link created by topology.
1226 if (dai_link->dobj.type && card->remove_dai_link)
1227 card->remove_dai_link(card, dai_link);
1229 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1230 if (link == dai_link) {
1231 list_del(&link->list);
1232 card->num_dai_links--;
1237 EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1239 static void soc_set_of_name_prefix(struct snd_soc_component *component)
1241 struct device_node *component_of_node = component->dev->of_node;
1245 if (!component_of_node && component->dev->parent)
1246 component_of_node = component->dev->parent->of_node;
1248 ret = of_property_read_string(component_of_node, "sound-name-prefix",
1251 component->name_prefix = str;
1254 static void soc_set_name_prefix(struct snd_soc_card *card,
1255 struct snd_soc_component *component)
1259 for (i = 0; i < card->num_configs && card->codec_conf; i++) {
1260 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1261 struct device_node *component_of_node = component->dev->of_node;
1263 if (!component_of_node && component->dev->parent)
1264 component_of_node = component->dev->parent->of_node;
1266 if (map->of_node && component_of_node != map->of_node)
1268 if (map->dev_name && strcmp(component->name, map->dev_name))
1270 component->name_prefix = map->name_prefix;
1275 * If there is no configuration table or no match in the table,
1276 * check if a prefix is provided in the node
1278 soc_set_of_name_prefix(component);
1281 static int soc_probe_component(struct snd_soc_card *card,
1282 struct snd_soc_component *component)
1284 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1285 struct snd_soc_dai *dai;
1288 if (!strcmp(component->name, "snd-soc-dummy"))
1291 if (component->card) {
1292 if (component->card != card) {
1293 dev_err(component->dev,
1294 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1295 card->name, component->card->name);
1301 if (!try_module_get(component->dev->driver->owner))
1304 component->card = card;
1306 soc_set_name_prefix(card, component);
1308 soc_init_component_debugfs(component);
1310 if (component->driver->dapm_widgets) {
1311 ret = snd_soc_dapm_new_controls(dapm,
1312 component->driver->dapm_widgets,
1313 component->driver->num_dapm_widgets);
1316 dev_err(component->dev,
1317 "Failed to create new controls %d\n", ret);
1322 list_for_each_entry(dai, &component->dai_list, list) {
1323 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1325 dev_err(component->dev,
1326 "Failed to create DAI widgets %d\n", ret);
1331 if (component->driver->probe) {
1332 ret = component->driver->probe(component);
1334 dev_err(component->dev,
1335 "ASoC: failed to probe component %d\n", ret);
1339 WARN(dapm->idle_bias_off &&
1340 dapm->bias_level != SND_SOC_BIAS_OFF,
1341 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1345 /* machine specific init */
1346 if (component->init) {
1347 ret = component->init(component);
1349 dev_err(component->dev,
1350 "Failed to do machine specific init %d\n", ret);
1355 if (component->driver->controls)
1356 snd_soc_add_component_controls(component,
1357 component->driver->controls,
1358 component->driver->num_controls);
1359 if (component->driver->dapm_routes)
1360 snd_soc_dapm_add_routes(dapm,
1361 component->driver->dapm_routes,
1362 component->driver->num_dapm_routes);
1364 list_add(&dapm->list, &card->dapm_list);
1365 list_add(&component->card_list, &card->component_dev_list);
1370 soc_cleanup_component_debugfs(component);
1371 component->card = NULL;
1372 module_put(component->dev->driver->owner);
1377 static void rtd_release(struct device *dev)
1382 static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1387 /* register the rtd device */
1388 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1391 device_initialize(rtd->dev);
1392 rtd->dev->parent = rtd->card->dev;
1393 rtd->dev->release = rtd_release;
1394 rtd->dev->groups = soc_dev_attr_groups;
1395 dev_set_name(rtd->dev, "%s", name);
1396 dev_set_drvdata(rtd->dev, rtd);
1397 mutex_init(&rtd->pcm_mutex);
1398 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1399 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1400 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1401 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1402 ret = device_add(rtd->dev);
1404 /* calling put_device() here to free the rtd->dev */
1405 put_device(rtd->dev);
1406 dev_err(rtd->card->dev,
1407 "ASoC: failed to register runtime device: %d\n", ret);
1410 rtd->dev_registered = 1;
1414 static int soc_probe_link_components(struct snd_soc_card *card,
1415 struct snd_soc_pcm_runtime *rtd,
1418 struct snd_soc_component *component;
1419 struct snd_soc_rtdcom_list *rtdcom;
1422 for_each_rtdcom(rtd, rtdcom) {
1423 component = rtdcom->component;
1425 if (component->driver->probe_order == order) {
1426 ret = soc_probe_component(card, component);
1435 static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1438 dai->driver->probe_order != order)
1441 if (dai->driver->probe) {
1442 int ret = dai->driver->probe(dai);
1444 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1455 static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1456 struct snd_soc_pcm_runtime *rtd)
1460 for (i = 0; i < num_dais; ++i) {
1461 struct snd_soc_dai_driver *drv = dais[i]->driver;
1463 if (!rtd->dai_link->no_pcm && drv->pcm_new)
1464 ret = drv->pcm_new(rtd, dais[i]);
1466 dev_err(dais[i]->dev,
1467 "ASoC: Failed to bind %s with pcm device\n",
1476 static int soc_link_dai_widgets(struct snd_soc_card *card,
1477 struct snd_soc_dai_link *dai_link,
1478 struct snd_soc_pcm_runtime *rtd)
1480 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1481 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1482 struct snd_soc_dapm_widget *sink, *source;
1485 if (rtd->num_codecs > 1)
1486 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1488 /* link the DAI widgets */
1489 sink = codec_dai->playback_widget;
1490 source = cpu_dai->capture_widget;
1491 if (sink && source) {
1492 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1493 dai_link->num_params,
1496 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1497 sink->name, source->name, ret);
1502 sink = cpu_dai->playback_widget;
1503 source = codec_dai->capture_widget;
1504 if (sink && source) {
1505 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1506 dai_link->num_params,
1509 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1510 sink->name, source->name, ret);
1518 static int soc_probe_link_dais(struct snd_soc_card *card,
1519 struct snd_soc_pcm_runtime *rtd, int order)
1521 struct snd_soc_dai_link *dai_link = rtd->dai_link;
1522 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1523 struct snd_soc_rtdcom_list *rtdcom;
1524 struct snd_soc_component *component;
1527 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1528 card->name, rtd->num, order);
1530 /* set default power off timeout */
1531 rtd->pmdown_time = pmdown_time;
1533 ret = soc_probe_dai(cpu_dai, order);
1537 /* probe the CODEC DAI */
1538 for (i = 0; i < rtd->num_codecs; i++) {
1539 ret = soc_probe_dai(rtd->codec_dais[i], order);
1544 /* complete DAI probe during last probe */
1545 if (order != SND_SOC_COMP_ORDER_LAST)
1548 /* do machine specific initialization */
1549 if (dai_link->init) {
1550 ret = dai_link->init(rtd);
1552 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1553 dai_link->name, ret);
1558 if (dai_link->dai_fmt)
1559 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1561 ret = soc_post_component_init(rtd, dai_link->name);
1565 #ifdef CONFIG_DEBUG_FS
1566 /* add DPCM sysfs entries */
1567 if (dai_link->dynamic)
1568 soc_dpcm_debugfs_add(rtd);
1574 * most drivers will register their PCMs using DAI link ordering but
1575 * topology based drivers can use the DAI link id field to set PCM
1576 * device number and then use rtd + a base offset of the BEs.
1578 for_each_rtdcom(rtd, rtdcom) {
1579 component = rtdcom->component;
1581 if (!component->driver->use_dai_pcm_id)
1584 if (rtd->dai_link->no_pcm)
1585 num += component->driver->be_pcm_base;
1587 num = rtd->dai_link->id;
1590 if (cpu_dai->driver->compress_new) {
1591 /*create compress_device"*/
1592 ret = cpu_dai->driver->compress_new(rtd, num);
1594 dev_err(card->dev, "ASoC: can't create compress %s\n",
1595 dai_link->stream_name);
1600 if (!dai_link->params) {
1601 /* create the pcm */
1602 ret = soc_new_pcm(rtd, num);
1604 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1605 dai_link->stream_name, ret);
1608 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1611 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1612 rtd->num_codecs, rtd);
1616 INIT_DELAYED_WORK(&rtd->delayed_work,
1617 codec2codec_close_delayed_work);
1619 /* link the DAI widgets */
1620 ret = soc_link_dai_widgets(card, dai_link, rtd);
1629 static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1631 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1632 struct snd_soc_component *component;
1634 struct device_node *codec_of_node;
1636 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1637 /* codecs, usually analog devices */
1638 name = aux_dev->codec_name;
1639 codec_of_node = aux_dev->codec_of_node;
1640 component = soc_find_component(codec_of_node, name);
1643 name = of_node_full_name(codec_of_node);
1646 } else if (aux_dev->name) {
1647 /* generic components */
1648 name = aux_dev->name;
1649 component = soc_find_component(NULL, name);
1653 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1657 component->init = aux_dev->init;
1658 list_add(&component->card_aux_list, &card->aux_comp_list);
1663 dev_err(card->dev, "ASoC: %s not registered\n", name);
1664 return -EPROBE_DEFER;
1667 static int soc_probe_aux_devices(struct snd_soc_card *card)
1669 struct snd_soc_component *comp;
1673 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1675 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
1676 if (comp->driver->probe_order == order) {
1677 ret = soc_probe_component(card, comp);
1680 "ASoC: failed to probe aux component %s %d\n",
1691 static void soc_remove_aux_devices(struct snd_soc_card *card)
1693 struct snd_soc_component *comp, *_comp;
1696 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1698 list_for_each_entry_safe(comp, _comp,
1699 &card->aux_comp_list, card_aux_list) {
1701 if (comp->driver->remove_order == order) {
1702 soc_remove_component(comp);
1703 /* remove it from the card's aux_comp_list */
1704 list_del(&comp->card_aux_list);
1711 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1712 * @rtd: The runtime for which the DAI link format should be changed
1713 * @dai_fmt: The new DAI link format
1715 * This function updates the DAI link format for all DAIs connected to the DAI
1716 * link for the specified runtime.
1718 * Note: For setups with a static format set the dai_fmt field in the
1719 * corresponding snd_dai_link struct instead of using this function.
1721 * Returns 0 on success, otherwise a negative error code.
1723 int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1724 unsigned int dai_fmt)
1726 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1727 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1731 for (i = 0; i < rtd->num_codecs; i++) {
1732 struct snd_soc_dai *codec_dai = codec_dais[i];
1734 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1735 if (ret != 0 && ret != -ENOTSUPP) {
1736 dev_warn(codec_dai->dev,
1737 "ASoC: Failed to set DAI format: %d\n", ret);
1742 /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1743 /* the component which has non_legacy_dai_naming is Codec */
1744 if (cpu_dai->component->driver->non_legacy_dai_naming) {
1745 unsigned int inv_dai_fmt;
1747 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1748 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1749 case SND_SOC_DAIFMT_CBM_CFM:
1750 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1752 case SND_SOC_DAIFMT_CBM_CFS:
1753 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1755 case SND_SOC_DAIFMT_CBS_CFM:
1756 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1758 case SND_SOC_DAIFMT_CBS_CFS:
1759 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1763 dai_fmt = inv_dai_fmt;
1766 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1767 if (ret != 0 && ret != -ENOTSUPP) {
1768 dev_warn(cpu_dai->dev,
1769 "ASoC: Failed to set DAI format: %d\n", ret);
1775 EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1779 /* Trim special characters, and replace '-' with '_' since '-' is used to
1780 * separate different DMI fields in the card long name. Only number and
1781 * alphabet characters and a few separator characters are kept.
1783 static void cleanup_dmi_name(char *name)
1787 for (i = 0; name[i]; i++) {
1788 if (isalnum(name[i]) || (name[i] == '.')
1789 || (name[i] == '_'))
1790 name[j++] = name[i];
1791 else if (name[i] == '-')
1798 /* Check if a DMI field is valid, i.e. not containing any string
1799 * in the black list.
1801 static int is_dmi_valid(const char *field)
1805 while (dmi_blacklist[i]) {
1806 if (strstr(field, dmi_blacklist[i]))
1815 * snd_soc_set_dmi_name() - Register DMI names to card
1816 * @card: The card to register DMI names
1817 * @flavour: The flavour "differentiator" for the card amongst its peers.
1819 * An Intel machine driver may be used by many different devices but are
1820 * difficult for userspace to differentiate, since machine drivers ususally
1821 * use their own name as the card short name and leave the card long name
1822 * blank. To differentiate such devices and fix bugs due to lack of
1823 * device-specific configurations, this function allows DMI info to be used
1824 * as the sound card long name, in the format of
1825 * "vendor-product-version-board"
1826 * (Character '-' is used to separate different DMI fields here).
1827 * This will help the user space to load the device-specific Use Case Manager
1828 * (UCM) configurations for the card.
1830 * Possible card long names may be:
1831 * DellInc.-XPS139343-01-0310JH
1832 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1833 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1835 * This function also supports flavoring the card longname to provide
1836 * the extra differentiation, like "vendor-product-version-board-flavor".
1838 * We only keep number and alphabet characters and a few separator characters
1839 * in the card long name since UCM in the user space uses the card long names
1840 * as card configuration directory names and AudoConf cannot support special
1841 * charactors like SPACE.
1843 * Returns 0 on success, otherwise a negative error code.
1845 int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1847 const char *vendor, *product, *product_version, *board;
1848 size_t longname_buf_size = sizeof(card->snd_card->longname);
1851 if (card->long_name)
1852 return 0; /* long name already set by driver or from DMI */
1854 /* make up dmi long name as: vendor.product.version.board */
1855 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1856 if (!vendor || !is_dmi_valid(vendor)) {
1857 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1862 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1864 cleanup_dmi_name(card->dmi_longname);
1866 product = dmi_get_system_info(DMI_PRODUCT_NAME);
1867 if (product && is_dmi_valid(product)) {
1868 len = strlen(card->dmi_longname);
1869 snprintf(card->dmi_longname + len,
1870 longname_buf_size - len,
1873 len++; /* skip the separator "-" */
1874 if (len < longname_buf_size)
1875 cleanup_dmi_name(card->dmi_longname + len);
1877 /* some vendors like Lenovo may only put a self-explanatory
1878 * name in the product version field
1880 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
1881 if (product_version && is_dmi_valid(product_version)) {
1882 len = strlen(card->dmi_longname);
1883 snprintf(card->dmi_longname + len,
1884 longname_buf_size - len,
1885 "-%s", product_version);
1888 if (len < longname_buf_size)
1889 cleanup_dmi_name(card->dmi_longname + len);
1893 board = dmi_get_system_info(DMI_BOARD_NAME);
1894 if (board && is_dmi_valid(board)) {
1895 len = strlen(card->dmi_longname);
1896 snprintf(card->dmi_longname + len,
1897 longname_buf_size - len,
1901 if (len < longname_buf_size)
1902 cleanup_dmi_name(card->dmi_longname + len);
1903 } else if (!product) {
1904 /* fall back to using legacy name */
1905 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1909 /* Add flavour to dmi long name */
1911 len = strlen(card->dmi_longname);
1912 snprintf(card->dmi_longname + len,
1913 longname_buf_size - len,
1917 if (len < longname_buf_size)
1918 cleanup_dmi_name(card->dmi_longname + len);
1921 /* set the card long name */
1922 card->long_name = card->dmi_longname;
1926 EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
1927 #endif /* CONFIG_DMI */
1929 static void soc_check_tplg_fes(struct snd_soc_card *card)
1931 struct snd_soc_component *component;
1932 const struct snd_soc_component_driver *comp_drv;
1933 struct snd_soc_dai_link *dai_link;
1936 list_for_each_entry(component, &component_list, list) {
1938 /* does this component override FEs ? */
1939 if (!component->driver->ignore_machine)
1942 /* for this machine ? */
1943 if (strcmp(component->driver->ignore_machine,
1944 card->dev->driver->name))
1947 /* machine matches, so override the rtd data */
1948 for (i = 0; i < card->num_links; i++) {
1950 dai_link = &card->dai_link[i];
1952 /* ignore this FE */
1953 if (dai_link->dynamic) {
1954 dai_link->ignore = true;
1958 dev_info(card->dev, "info: override FE DAI link %s\n",
1959 card->dai_link[i].name);
1961 /* override platform component */
1962 dai_link->platform_name = component->name;
1964 /* convert non BE into BE */
1965 dai_link->no_pcm = 1;
1967 /* override any BE fixups */
1968 dai_link->be_hw_params_fixup =
1969 component->driver->be_hw_params_fixup;
1971 /* most BE links don't set stream name, so set it to
1972 * dai link name if it's NULL to help bind widgets.
1974 if (!dai_link->stream_name)
1975 dai_link->stream_name = dai_link->name;
1978 /* Inform userspace we are using alternate topology */
1979 if (component->driver->topology_name_prefix) {
1981 /* topology shortname created ? */
1982 if (!card->topology_shortname_created) {
1983 comp_drv = component->driver;
1985 snprintf(card->topology_shortname, 32, "%s-%s",
1986 comp_drv->topology_name_prefix,
1988 card->topology_shortname_created = true;
1991 /* use topology shortname */
1992 card->name = card->topology_shortname;
1997 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1999 struct snd_soc_pcm_runtime *rtd;
2000 struct snd_soc_dai_link *dai_link;
2003 mutex_lock(&client_mutex);
2004 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
2006 /* check whether any platform is ignore machine FE and using topology */
2007 soc_check_tplg_fes(card);
2010 for (i = 0; i < card->num_links; i++) {
2011 ret = soc_bind_dai_link(card, &card->dai_link[i]);
2016 /* bind aux_devs too */
2017 for (i = 0; i < card->num_aux_devs; i++) {
2018 ret = soc_bind_aux_dev(card, i);
2023 /* add predefined DAI links to the list */
2024 for (i = 0; i < card->num_links; i++)
2025 snd_soc_add_dai_link(card, card->dai_link+i);
2027 /* card bind complete so register a sound card */
2028 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2029 card->owner, 0, &card->snd_card);
2032 "ASoC: can't create sound card for card %s: %d\n",
2037 soc_init_card_debugfs(card);
2039 card->dapm.bias_level = SND_SOC_BIAS_OFF;
2040 card->dapm.dev = card->dev;
2041 card->dapm.card = card;
2042 list_add(&card->dapm.list, &card->dapm_list);
2044 #ifdef CONFIG_DEBUG_FS
2045 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2048 #ifdef CONFIG_PM_SLEEP
2049 /* deferred resume work */
2050 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
2053 if (card->dapm_widgets)
2054 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2055 card->num_dapm_widgets);
2057 if (card->of_dapm_widgets)
2058 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2059 card->num_of_dapm_widgets);
2061 /* initialise the sound card only once */
2063 ret = card->probe(card);
2065 goto card_probe_error;
2068 /* probe all components used by DAI links on this card */
2069 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2071 list_for_each_entry(rtd, &card->rtd_list, list) {
2072 ret = soc_probe_link_components(card, rtd, order);
2075 "ASoC: failed to instantiate card %d\n",
2082 /* probe auxiliary components */
2083 ret = soc_probe_aux_devices(card);
2087 /* Find new DAI links added during probing components and bind them.
2088 * Components with topology may bring new DAIs and DAI links.
2090 list_for_each_entry(dai_link, &card->dai_link_list, list) {
2091 if (soc_is_dai_link_bound(card, dai_link))
2094 ret = soc_init_dai_link(card, dai_link);
2097 ret = soc_bind_dai_link(card, dai_link);
2102 /* probe all DAI links on this card */
2103 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2105 list_for_each_entry(rtd, &card->rtd_list, list) {
2106 ret = soc_probe_link_dais(card, rtd, order);
2109 "ASoC: failed to instantiate card %d\n",
2116 snd_soc_dapm_link_dai_widgets(card);
2117 snd_soc_dapm_connect_dai_link_widgets(card);
2120 snd_soc_add_card_controls(card, card->controls, card->num_controls);
2122 if (card->dapm_routes)
2123 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2124 card->num_dapm_routes);
2126 if (card->of_dapm_routes)
2127 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2128 card->num_of_dapm_routes);
2130 /* try to set some sane longname if DMI is available */
2131 snd_soc_set_dmi_name(card, NULL);
2133 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
2135 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2136 "%s", card->long_name ? card->long_name : card->name);
2137 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2138 "%s", card->driver_name ? card->driver_name : card->name);
2139 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2140 switch (card->snd_card->driver[i]) {
2146 if (!isalnum(card->snd_card->driver[i]))
2147 card->snd_card->driver[i] = '_';
2152 if (card->late_probe) {
2153 ret = card->late_probe(card);
2155 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
2157 goto probe_aux_dev_err;
2161 snd_soc_dapm_new_widgets(card);
2163 ret = snd_card_register(card->snd_card);
2165 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2167 goto probe_aux_dev_err;
2170 card->instantiated = 1;
2171 snd_soc_dapm_sync(&card->dapm);
2172 if (card->auto_bind && !card->components_added) {
2173 component_master_add_with_match(card->dev,
2174 &snd_soc_card_comp_ops,
2176 card->components_added = true;
2178 mutex_unlock(&card->mutex);
2179 mutex_unlock(&client_mutex);
2184 soc_remove_aux_devices(card);
2187 soc_remove_dai_links(card);
2193 snd_soc_dapm_free(&card->dapm);
2194 soc_cleanup_card_debugfs(card);
2195 snd_card_free(card->snd_card);
2198 soc_remove_pcm_runtimes(card);
2199 mutex_unlock(&card->mutex);
2200 mutex_unlock(&client_mutex);
2205 /* probes a new socdev */
2206 static int soc_probe(struct platform_device *pdev)
2208 struct snd_soc_card *card = platform_get_drvdata(pdev);
2211 * no card, so machine driver should be registering card
2212 * we should not be here in that case so ret error
2217 dev_warn(&pdev->dev,
2218 "ASoC: machine %s should use snd_soc_register_card()\n",
2221 /* Bodge while we unpick instantiation */
2222 card->dev = &pdev->dev;
2224 return snd_soc_register_card(card);
2227 static int soc_cleanup_card_resources(struct snd_soc_card *card)
2229 struct snd_soc_pcm_runtime *rtd;
2231 /* make sure any delayed work runs */
2232 list_for_each_entry(rtd, &card->rtd_list, list)
2233 flush_delayed_work(&rtd->delayed_work);
2235 /* free the ALSA card at first; this syncs with pending operations */
2236 snd_card_free(card->snd_card);
2238 /* remove and free each DAI */
2239 soc_remove_dai_links(card);
2240 soc_remove_pcm_runtimes(card);
2242 /* remove auxiliary devices */
2243 soc_remove_aux_devices(card);
2245 snd_soc_dapm_free(&card->dapm);
2246 soc_cleanup_card_debugfs(card);
2248 /* remove the card */
2255 /* removes a socdev */
2256 static int soc_remove(struct platform_device *pdev)
2258 struct snd_soc_card *card = platform_get_drvdata(pdev);
2260 snd_soc_unregister_card(card);
2264 int snd_soc_poweroff(struct device *dev)
2266 struct snd_soc_card *card = dev_get_drvdata(dev);
2267 struct snd_soc_pcm_runtime *rtd;
2269 if (!card->instantiated)
2272 /* Flush out pmdown_time work - we actually do want to run it
2273 * now, we're shutting down so no imminent restart. */
2274 list_for_each_entry(rtd, &card->rtd_list, list)
2275 flush_delayed_work(&rtd->delayed_work);
2277 snd_soc_dapm_shutdown(card);
2279 /* deactivate pins to sleep state */
2280 list_for_each_entry(rtd, &card->rtd_list, list) {
2281 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2284 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2285 for (i = 0; i < rtd->num_codecs; i++) {
2286 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
2287 pinctrl_pm_select_sleep_state(codec_dai->dev);
2293 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2295 const struct dev_pm_ops snd_soc_pm_ops = {
2296 .suspend = snd_soc_suspend,
2297 .resume = snd_soc_resume,
2298 .freeze = snd_soc_suspend,
2299 .thaw = snd_soc_resume,
2300 .poweroff = snd_soc_poweroff,
2301 .restore = snd_soc_resume,
2303 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2305 /* ASoC platform driver */
2306 static struct platform_driver soc_driver = {
2308 .name = "soc-audio",
2309 .pm = &snd_soc_pm_ops,
2312 .remove = soc_remove,
2316 * snd_soc_cnew - create new control
2317 * @_template: control template
2318 * @data: control private data
2319 * @long_name: control long name
2320 * @prefix: control name prefix
2322 * Create a new mixer control from a template control.
2324 * Returns 0 for success, else error.
2326 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2327 void *data, const char *long_name,
2330 struct snd_kcontrol_new template;
2331 struct snd_kcontrol *kcontrol;
2334 memcpy(&template, _template, sizeof(template));
2338 long_name = template.name;
2341 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2345 template.name = name;
2347 template.name = long_name;
2350 kcontrol = snd_ctl_new1(&template, data);
2356 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2358 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2359 const struct snd_kcontrol_new *controls, int num_controls,
2360 const char *prefix, void *data)
2364 for (i = 0; i < num_controls; i++) {
2365 const struct snd_kcontrol_new *control = &controls[i];
2366 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2367 control->name, prefix));
2369 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2370 control->name, err);
2378 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2381 struct snd_card *card = soc_card->snd_card;
2382 struct snd_kcontrol *kctl;
2384 if (unlikely(!name))
2387 list_for_each_entry(kctl, &card->controls, list)
2388 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2392 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2395 * snd_soc_add_component_controls - Add an array of controls to a component.
2397 * @component: Component to add controls to
2398 * @controls: Array of controls to add
2399 * @num_controls: Number of elements in the array
2401 * Return: 0 for success, else error.
2403 int snd_soc_add_component_controls(struct snd_soc_component *component,
2404 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2406 struct snd_card *card = component->card->snd_card;
2408 return snd_soc_add_controls(card, component->dev, controls,
2409 num_controls, component->name_prefix, component);
2411 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2414 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2415 * Convenience function to add a list of controls.
2417 * @soc_card: SoC card to add controls to
2418 * @controls: array of controls to add
2419 * @num_controls: number of elements in the array
2421 * Return 0 for success, else error.
2423 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2424 const struct snd_kcontrol_new *controls, int num_controls)
2426 struct snd_card *card = soc_card->snd_card;
2428 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2431 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2434 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2435 * Convienience function to add a list of controls.
2437 * @dai: DAI to add controls to
2438 * @controls: array of controls to add
2439 * @num_controls: number of elements in the array
2441 * Return 0 for success, else error.
2443 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2444 const struct snd_kcontrol_new *controls, int num_controls)
2446 struct snd_card *card = dai->component->card->snd_card;
2448 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2451 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2454 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2456 * @clk_id: DAI specific clock ID
2457 * @freq: new clock frequency in Hz
2458 * @dir: new clock direction - input/output.
2460 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2462 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2463 unsigned int freq, int dir)
2465 if (dai->driver->ops->set_sysclk)
2466 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2468 return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2471 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2474 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2475 * @component: COMPONENT
2476 * @clk_id: DAI specific clock ID
2477 * @source: Source for the clock
2478 * @freq: new clock frequency in Hz
2479 * @dir: new clock direction - input/output.
2481 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2483 int snd_soc_component_set_sysclk(struct snd_soc_component *component, int clk_id,
2484 int source, unsigned int freq, int dir)
2486 if (component->driver->set_sysclk)
2487 return component->driver->set_sysclk(component, clk_id, source,
2492 EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2495 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2497 * @div_id: DAI specific clock divider ID
2498 * @div: new clock divisor.
2500 * Configures the clock dividers. This is used to derive the best DAI bit and
2501 * frame clocks from the system or master clock. It's best to set the DAI bit
2502 * and frame clocks as low as possible to save system power.
2504 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2505 int div_id, int div)
2507 if (dai->driver->ops->set_clkdiv)
2508 return dai->driver->ops->set_clkdiv(dai, div_id, div);
2512 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2515 * snd_soc_dai_set_pll - configure DAI PLL.
2517 * @pll_id: DAI specific PLL ID
2518 * @source: DAI specific source for the PLL
2519 * @freq_in: PLL input clock frequency in Hz
2520 * @freq_out: requested PLL output clock frequency in Hz
2522 * Configures and enables PLL to generate output clock based on input clock.
2524 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2525 unsigned int freq_in, unsigned int freq_out)
2527 if (dai->driver->ops->set_pll)
2528 return dai->driver->ops->set_pll(dai, pll_id, source,
2531 return snd_soc_component_set_pll(dai->component, pll_id, source,
2534 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2537 * snd_soc_component_set_pll - configure component PLL.
2538 * @component: COMPONENT
2539 * @pll_id: DAI specific PLL ID
2540 * @source: DAI specific source for the PLL
2541 * @freq_in: PLL input clock frequency in Hz
2542 * @freq_out: requested PLL output clock frequency in Hz
2544 * Configures and enables PLL to generate output clock based on input clock.
2546 int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2547 int source, unsigned int freq_in,
2548 unsigned int freq_out)
2550 if (component->driver->set_pll)
2551 return component->driver->set_pll(component, pll_id, source,
2556 EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2559 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2561 * @ratio: Ratio of BCLK to Sample rate.
2563 * Configures the DAI for a preset BCLK to sample rate ratio.
2565 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2567 if (dai->driver->ops->set_bclk_ratio)
2568 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2572 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2575 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2577 * @fmt: SND_SOC_DAIFMT_* format value.
2579 * Configures the DAI hardware format and clocking.
2581 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2583 if (dai->driver == NULL)
2585 if (dai->driver->ops->set_fmt == NULL)
2587 return dai->driver->ops->set_fmt(dai, fmt);
2589 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2592 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
2593 * @slots: Number of slots in use.
2594 * @tx_mask: bitmask representing active TX slots.
2595 * @rx_mask: bitmask representing active RX slots.
2597 * Generates the TDM tx and rx slot default masks for DAI.
2599 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
2600 unsigned int *tx_mask,
2601 unsigned int *rx_mask)
2603 if (*tx_mask || *rx_mask)
2609 *tx_mask = (1 << slots) - 1;
2610 *rx_mask = (1 << slots) - 1;
2616 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2617 * @dai: The DAI to configure
2618 * @tx_mask: bitmask representing active TX slots.
2619 * @rx_mask: bitmask representing active RX slots.
2620 * @slots: Number of slots in use.
2621 * @slot_width: Width in bits for each slot.
2623 * This function configures the specified DAI for TDM operation. @slot contains
2624 * the total number of slots of the TDM stream and @slot_with the width of each
2625 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2626 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2627 * DAI should write to or read from. If a bit is set the corresponding slot is
2628 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2629 * the first slot, bit 1 to the second slot and so on. The first active slot
2630 * maps to the first channel of the DAI, the second active slot to the second
2631 * channel and so on.
2633 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2634 * @rx_mask and @slot_width will be ignored.
2636 * Returns 0 on success, a negative error code otherwise.
2638 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2639 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2641 if (dai->driver->ops->xlate_tdm_slot_mask)
2642 dai->driver->ops->xlate_tdm_slot_mask(slots,
2643 &tx_mask, &rx_mask);
2645 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
2647 dai->tx_mask = tx_mask;
2648 dai->rx_mask = rx_mask;
2650 if (dai->driver->ops->set_tdm_slot)
2651 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2656 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2659 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2661 * @tx_num: how many TX channels
2662 * @tx_slot: pointer to an array which imply the TX slot number channel
2664 * @rx_num: how many RX channels
2665 * @rx_slot: pointer to an array which imply the RX slot number channel
2668 * configure the relationship between channel number and TDM slot number.
2670 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2671 unsigned int tx_num, unsigned int *tx_slot,
2672 unsigned int rx_num, unsigned int *rx_slot)
2674 if (dai->driver->ops->set_channel_map)
2675 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2680 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2683 * snd_soc_dai_get_channel_map - Get DAI audio channel map
2685 * @tx_num: how many TX channels
2686 * @tx_slot: pointer to an array which imply the TX slot number channel
2688 * @rx_num: how many RX channels
2689 * @rx_slot: pointer to an array which imply the RX slot number channel
2692 int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
2693 unsigned int *tx_num, unsigned int *tx_slot,
2694 unsigned int *rx_num, unsigned int *rx_slot)
2696 if (dai->driver->ops->get_channel_map)
2697 return dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
2702 EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
2705 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2707 * @tristate: tristate enable
2709 * Tristates the DAI so that others can use it.
2711 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2713 if (dai->driver->ops->set_tristate)
2714 return dai->driver->ops->set_tristate(dai, tristate);
2718 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2721 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2723 * @mute: mute enable
2724 * @direction: stream to mute
2726 * Mutes the DAI DAC.
2728 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2734 if (dai->driver->ops->mute_stream)
2735 return dai->driver->ops->mute_stream(dai, mute, direction);
2736 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2737 dai->driver->ops->digital_mute)
2738 return dai->driver->ops->digital_mute(dai, mute);
2742 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2745 * snd_soc_register_card - Register a card with the ASoC core
2747 * @card: Card to register
2750 int snd_soc_register_card(struct snd_soc_card *card)
2753 struct snd_soc_pcm_runtime *rtd;
2755 if (!card->name || !card->dev)
2758 for (i = 0; i < card->num_links; i++) {
2759 struct snd_soc_dai_link *link = &card->dai_link[i];
2761 ret = soc_init_dai_link(card, link);
2763 dev_err(card->dev, "ASoC: failed to init link %s\n",
2769 dev_set_drvdata(card->dev, card);
2771 snd_soc_initialize_card_lists(card);
2773 INIT_LIST_HEAD(&card->dai_link_list);
2774 card->num_dai_links = 0;
2776 INIT_LIST_HEAD(&card->rtd_list);
2779 INIT_LIST_HEAD(&card->dapm_dirty);
2780 INIT_LIST_HEAD(&card->dobj_list);
2781 card->instantiated = 0;
2782 mutex_init(&card->mutex);
2783 mutex_init(&card->dapm_mutex);
2785 ret = snd_soc_instantiate_card(card);
2789 /* deactivate pins to sleep state */
2790 list_for_each_entry(rtd, &card->rtd_list, list) {
2791 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2794 for (j = 0; j < rtd->num_codecs; j++) {
2795 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2796 if (!codec_dai->active)
2797 pinctrl_pm_select_sleep_state(codec_dai->dev);
2800 if (!cpu_dai->active)
2801 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2806 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2809 * snd_soc_unregister_card - Unregister a card with the ASoC core
2811 * @card: Card to unregister
2814 int snd_soc_unregister_card(struct snd_soc_card *card)
2816 if (card->instantiated) {
2817 card->instantiated = false;
2818 snd_soc_dapm_shutdown(card);
2819 soc_cleanup_card_resources(card);
2820 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2823 if (!card->auto_bind && card->components_added)
2824 component_master_del(card->dev, &snd_soc_card_comp_ops);
2828 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2831 * Simplify DAI link configuration by removing ".-1" from device names
2832 * and sanitizing names.
2834 static char *fmt_single_name(struct device *dev, int *id)
2836 char *found, name[NAME_SIZE];
2839 if (dev_name(dev) == NULL)
2842 strlcpy(name, dev_name(dev), NAME_SIZE);
2844 /* are we a "%s.%d" name (platform and SPI components) */
2845 found = strstr(name, dev->driver->name);
2848 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2850 /* discard ID from name if ID == -1 */
2852 found[strlen(dev->driver->name)] = '\0';
2856 /* I2C component devices are named "bus-addr" */
2857 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2858 char tmp[NAME_SIZE];
2860 /* create unique ID number from I2C addr and bus */
2861 *id = ((id1 & 0xffff) << 16) + id2;
2863 /* sanitize component name for DAI link creation */
2864 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2865 strlcpy(name, tmp, NAME_SIZE);
2870 return kstrdup(name, GFP_KERNEL);
2874 * Simplify DAI link naming for single devices with multiple DAIs by removing
2875 * any ".-1" and using the DAI name (instead of device name).
2877 static inline char *fmt_multiple_name(struct device *dev,
2878 struct snd_soc_dai_driver *dai_drv)
2880 if (dai_drv->name == NULL) {
2882 "ASoC: error - multiple DAI %s registered with no name\n",
2887 return kstrdup(dai_drv->name, GFP_KERNEL);
2891 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2893 * @component: The component for which the DAIs should be unregistered
2895 static void snd_soc_unregister_dais(struct snd_soc_component *component)
2897 struct snd_soc_dai *dai, *_dai;
2899 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
2900 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2902 list_del(&dai->list);
2908 /* Create a DAI and add it to the component's DAI list */
2909 static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2910 struct snd_soc_dai_driver *dai_drv,
2911 bool legacy_dai_naming)
2913 struct device *dev = component->dev;
2914 struct snd_soc_dai *dai;
2916 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2918 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2923 * Back in the old days when we still had component-less DAIs,
2924 * instead of having a static name, component-less DAIs would
2925 * inherit the name of the parent device so it is possible to
2926 * register multiple instances of the DAI. We still need to keep
2927 * the same naming style even though those DAIs are not
2928 * component-less anymore.
2930 if (legacy_dai_naming &&
2931 (dai_drv->id == 0 || dai_drv->name == NULL)) {
2932 dai->name = fmt_single_name(dev, &dai->id);
2934 dai->name = fmt_multiple_name(dev, dai_drv);
2936 dai->id = dai_drv->id;
2938 dai->id = component->num_dai;
2940 if (dai->name == NULL) {
2945 dai->component = component;
2947 dai->driver = dai_drv;
2948 if (!dai->driver->ops)
2949 dai->driver->ops = &null_dai_ops;
2951 list_add_tail(&dai->list, &component->dai_list);
2952 component->num_dai++;
2954 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2959 * snd_soc_register_dais - Register a DAI with the ASoC core
2961 * @component: The component the DAIs are registered for
2962 * @dai_drv: DAI driver to use for the DAIs
2963 * @count: Number of DAIs
2964 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
2967 static int snd_soc_register_dais(struct snd_soc_component *component,
2968 struct snd_soc_dai_driver *dai_drv, size_t count)
2970 struct device *dev = component->dev;
2971 struct snd_soc_dai *dai;
2975 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
2977 for (i = 0; i < count; i++) {
2979 dai = soc_add_dai(component, dai_drv + i,
2980 count == 1 && !component->driver->non_legacy_dai_naming);
2990 snd_soc_unregister_dais(component);
2996 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2998 * @component: The component the DAIs are registered for
2999 * @dai_drv: DAI driver to use for the DAI
3001 * Topology can use this API to register DAIs when probing a component.
3002 * These DAIs's widgets will be freed in the card cleanup and the DAIs
3003 * will be freed in the component cleanup.
3005 int snd_soc_register_dai(struct snd_soc_component *component,
3006 struct snd_soc_dai_driver *dai_drv)
3008 struct snd_soc_dapm_context *dapm =
3009 snd_soc_component_get_dapm(component);
3010 struct snd_soc_dai *dai;
3013 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
3014 dev_err(component->dev, "Invalid dai type %d\n",
3015 dai_drv->dobj.type);
3019 lockdep_assert_held(&client_mutex);
3020 dai = soc_add_dai(component, dai_drv, false);
3024 /* Create the DAI widgets here. After adding DAIs, topology may
3025 * also add routes that need these widgets as source or sink.
3027 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
3029 dev_err(component->dev,
3030 "Failed to create DAI widgets %d\n", ret);
3035 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3037 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3038 enum snd_soc_dapm_type type, int subseq)
3040 struct snd_soc_component *component = dapm->component;
3042 component->driver->seq_notifier(component, type, subseq);
3045 static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3048 struct snd_soc_component *component = dapm->component;
3050 return component->driver->stream_event(component, event);
3053 static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
3054 enum snd_soc_bias_level level)
3056 struct snd_soc_component *component = dapm->component;
3058 return component->driver->set_bias_level(component, level);
3061 static int snd_soc_component_initialize(struct snd_soc_component *component,
3062 const struct snd_soc_component_driver *driver, struct device *dev)
3064 struct snd_soc_dapm_context *dapm;
3066 component->name = fmt_single_name(dev, &component->id);
3067 if (!component->name) {
3068 dev_err(dev, "ASoC: Failed to allocate name\n");
3072 component->dev = dev;
3073 component->driver = driver;
3075 dapm = snd_soc_component_get_dapm(component);
3077 dapm->component = component;
3078 dapm->bias_level = SND_SOC_BIAS_OFF;
3079 dapm->idle_bias_off = !driver->idle_bias_on;
3080 dapm->suspend_bias_off = driver->suspend_bias_off;
3081 if (driver->seq_notifier)
3082 dapm->seq_notifier = snd_soc_component_seq_notifier;
3083 if (driver->stream_event)
3084 dapm->stream_event = snd_soc_component_stream_event;
3085 if (driver->set_bias_level)
3086 dapm->set_bias_level = snd_soc_component_set_bias_level;
3088 INIT_LIST_HEAD(&component->dai_list);
3089 mutex_init(&component->io_mutex);
3094 static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
3096 int val_bytes = regmap_get_val_bytes(component->regmap);
3098 /* Errors are legitimate for non-integer byte multiples */
3100 component->val_bytes = val_bytes;
3103 #ifdef CONFIG_REGMAP
3106 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
3107 * @component: The component for which to initialize the regmap instance
3108 * @regmap: The regmap instance that should be used by the component
3110 * This function allows deferred assignment of the regmap instance that is
3111 * associated with the component. Only use this if the regmap instance is not
3112 * yet ready when the component is registered. The function must also be called
3113 * before the first IO attempt of the component.
3115 void snd_soc_component_init_regmap(struct snd_soc_component *component,
3116 struct regmap *regmap)
3118 component->regmap = regmap;
3119 snd_soc_component_setup_regmap(component);
3121 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3124 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
3125 * @component: The component for which to de-initialize the regmap instance
3127 * Calls regmap_exit() on the regmap instance associated to the component and
3128 * removes the regmap instance from the component.
3130 * This function should only be used if snd_soc_component_init_regmap() was used
3131 * to initialize the regmap instance.
3133 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3135 regmap_exit(component->regmap);
3136 component->regmap = NULL;
3138 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3142 static void snd_soc_component_add(struct snd_soc_component *component)
3144 mutex_lock(&client_mutex);
3146 if (!component->driver->write && !component->driver->read) {
3147 if (!component->regmap)
3148 component->regmap = dev_get_regmap(component->dev, NULL);
3149 if (component->regmap)
3150 snd_soc_component_setup_regmap(component);
3153 list_add(&component->list, &component_list);
3154 INIT_LIST_HEAD(&component->dobj_list);
3156 mutex_unlock(&client_mutex);
3159 static void snd_soc_component_cleanup(struct snd_soc_component *component)
3161 snd_soc_unregister_dais(component);
3162 kfree(component->name);
3165 static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3167 struct snd_soc_card *card = component->card;
3170 snd_soc_unregister_card(card);
3172 list_del(&component->list);
3175 #define ENDIANNESS_MAP(name) \
3176 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3177 static u64 endianness_format_map[] = {
3178 ENDIANNESS_MAP(S16_),
3179 ENDIANNESS_MAP(U16_),
3180 ENDIANNESS_MAP(S24_),
3181 ENDIANNESS_MAP(U24_),
3182 ENDIANNESS_MAP(S32_),
3183 ENDIANNESS_MAP(U32_),
3184 ENDIANNESS_MAP(S24_3),
3185 ENDIANNESS_MAP(U24_3),
3186 ENDIANNESS_MAP(S20_3),
3187 ENDIANNESS_MAP(U20_3),
3188 ENDIANNESS_MAP(S18_3),
3189 ENDIANNESS_MAP(U18_3),
3190 ENDIANNESS_MAP(FLOAT_),
3191 ENDIANNESS_MAP(FLOAT64_),
3192 ENDIANNESS_MAP(IEC958_SUBFRAME_),
3196 * Fix up the DAI formats for endianness: codecs don't actually see
3197 * the endianness of the data but we're using the CPU format
3198 * definitions which do need to include endianness so we ensure that
3199 * codec DAIs always have both big and little endian variants set.
3201 static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3205 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3206 if (stream->formats & endianness_format_map[i])
3207 stream->formats |= endianness_format_map[i];
3210 int snd_soc_add_component(struct device *dev,
3211 struct snd_soc_component *component,
3212 const struct snd_soc_component_driver *component_driver,
3213 struct snd_soc_dai_driver *dai_drv,
3219 ret = snd_soc_component_initialize(component, component_driver, dev);
3223 if (component_driver->endianness) {
3224 for (i = 0; i < num_dai; i++) {
3225 convert_endianness_formats(&dai_drv[i].playback);
3226 convert_endianness_formats(&dai_drv[i].capture);
3230 ret = snd_soc_register_dais(component, dai_drv, num_dai);
3232 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3236 snd_soc_component_add(component);
3238 ret = component_add(dev, NULL);
3240 dev_err(dev, "ASoC: Failed to add Component: %d\n", ret);
3247 soc_remove_component(component);
3248 snd_soc_unregister_dais(component);
3250 snd_soc_component_cleanup(component);
3254 EXPORT_SYMBOL_GPL(snd_soc_add_component);
3256 int snd_soc_register_component(struct device *dev,
3257 const struct snd_soc_component_driver *component_driver,
3258 struct snd_soc_dai_driver *dai_drv,
3261 struct snd_soc_component *component;
3263 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
3267 return snd_soc_add_component(dev, component, component_driver,
3270 EXPORT_SYMBOL_GPL(snd_soc_register_component);
3273 * snd_soc_unregister_component - Unregister all related component
3274 * from the ASoC core
3276 * @dev: The device to unregister
3278 static int __snd_soc_unregister_component(struct device *dev)
3280 struct snd_soc_component *component;
3283 mutex_lock(&client_mutex);
3284 list_for_each_entry(component, &component_list, list) {
3285 if (dev != component->dev)
3288 snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);
3289 snd_soc_component_del_unlocked(component);
3293 mutex_unlock(&client_mutex);
3296 component_del(dev, NULL);
3297 snd_soc_component_cleanup(component);
3303 void snd_soc_unregister_component(struct device *dev)
3305 while (__snd_soc_unregister_component(dev));
3307 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3309 struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
3310 const char *driver_name)
3312 struct snd_soc_component *component;
3313 struct snd_soc_component *ret;
3316 mutex_lock(&client_mutex);
3317 list_for_each_entry(component, &component_list, list) {
3318 if (dev != component->dev)
3322 (driver_name != component->driver->name) &&
3323 (strcmp(component->driver->name, driver_name) != 0))
3329 mutex_unlock(&client_mutex);
3333 EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
3335 /* Retrieve a card's name from device tree */
3336 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3337 const char *propname)
3339 struct device_node *np;
3343 pr_err("card->dev is not set before calling %s\n", __func__);
3347 np = card->dev->of_node;
3349 ret = of_property_read_string_index(np, propname, 0, &card->name);
3351 * EINVAL means the property does not exist. This is fine providing
3352 * card->name was previously set, which is checked later in
3353 * snd_soc_register_card.
3355 if (ret < 0 && ret != -EINVAL) {
3357 "ASoC: Property '%s' could not be read: %d\n",
3364 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3366 static const struct snd_soc_dapm_widget simple_widgets[] = {
3367 SND_SOC_DAPM_MIC("Microphone", NULL),
3368 SND_SOC_DAPM_LINE("Line", NULL),
3369 SND_SOC_DAPM_HP("Headphone", NULL),
3370 SND_SOC_DAPM_SPK("Speaker", NULL),
3373 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
3374 const char *propname)
3376 struct device_node *np = card->dev->of_node;
3377 struct snd_soc_dapm_widget *widgets;
3378 const char *template, *wname;
3379 int i, j, num_widgets, ret;
3381 num_widgets = of_property_count_strings(np, propname);
3382 if (num_widgets < 0) {
3384 "ASoC: Property '%s' does not exist\n", propname);
3387 if (num_widgets & 1) {
3389 "ASoC: Property '%s' length is not even\n", propname);
3395 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3400 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3404 "ASoC: Could not allocate memory for widgets\n");
3408 for (i = 0; i < num_widgets; i++) {
3409 ret = of_property_read_string_index(np, propname,
3413 "ASoC: Property '%s' index %d read error:%d\n",
3414 propname, 2 * i, ret);
3418 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3419 if (!strncmp(template, simple_widgets[j].name,
3420 strlen(simple_widgets[j].name))) {
3421 widgets[i] = simple_widgets[j];
3426 if (j >= ARRAY_SIZE(simple_widgets)) {
3428 "ASoC: DAPM widget '%s' is not supported\n",
3433 ret = of_property_read_string_index(np, propname,
3438 "ASoC: Property '%s' index %d read error:%d\n",
3439 propname, (2 * i) + 1, ret);
3443 widgets[i].name = wname;
3446 card->of_dapm_widgets = widgets;
3447 card->num_of_dapm_widgets = num_widgets;
3451 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
3453 int snd_soc_of_get_slot_mask(struct device_node *np,
3454 const char *prop_name,
3458 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
3464 for (i = 0; i < val; i++)
3465 if (be32_to_cpup(&of_slot_mask[i]))
3470 EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
3472 int snd_soc_of_parse_tdm_slot(struct device_node *np,
3473 unsigned int *tx_mask,
3474 unsigned int *rx_mask,
3475 unsigned int *slots,
3476 unsigned int *slot_width)
3482 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3484 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3486 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3487 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3495 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3496 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3506 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3508 void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
3509 struct snd_soc_codec_conf *codec_conf,
3510 struct device_node *of_node,
3511 const char *propname)
3513 struct device_node *np = card->dev->of_node;
3517 ret = of_property_read_string(np, propname, &str);
3519 /* no prefix is not error */
3523 codec_conf->of_node = of_node;
3524 codec_conf->name_prefix = str;
3526 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
3528 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3529 const char *propname)
3531 struct device_node *np = card->dev->of_node;
3533 struct snd_soc_dapm_route *routes;
3536 num_routes = of_property_count_strings(np, propname);
3537 if (num_routes < 0 || num_routes & 1) {
3539 "ASoC: Property '%s' does not exist or its length is not even\n",
3545 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3550 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
3554 "ASoC: Could not allocate DAPM route table\n");
3558 for (i = 0; i < num_routes; i++) {
3559 ret = of_property_read_string_index(np, propname,
3560 2 * i, &routes[i].sink);
3563 "ASoC: Property '%s' index %d could not be read: %d\n",
3564 propname, 2 * i, ret);
3567 ret = of_property_read_string_index(np, propname,
3568 (2 * i) + 1, &routes[i].source);
3571 "ASoC: Property '%s' index %d could not be read: %d\n",
3572 propname, (2 * i) + 1, ret);
3577 card->num_of_dapm_routes = num_routes;
3578 card->of_dapm_routes = routes;
3582 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3584 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3586 struct device_node **bitclkmaster,
3587 struct device_node **framemaster)
3591 unsigned int format = 0;
3597 } of_fmt_table[] = {
3598 { "i2s", SND_SOC_DAIFMT_I2S },
3599 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3600 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3601 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3602 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3603 { "ac97", SND_SOC_DAIFMT_AC97 },
3604 { "pdm", SND_SOC_DAIFMT_PDM},
3605 { "msb", SND_SOC_DAIFMT_MSB },
3606 { "lsb", SND_SOC_DAIFMT_LSB },
3613 * check "dai-format = xxx"
3614 * or "[prefix]format = xxx"
3615 * SND_SOC_DAIFMT_FORMAT_MASK area
3617 ret = of_property_read_string(np, "dai-format", &str);
3619 snprintf(prop, sizeof(prop), "%sformat", prefix);
3620 ret = of_property_read_string(np, prop, &str);
3623 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3624 if (strcmp(str, of_fmt_table[i].name) == 0) {
3625 format |= of_fmt_table[i].val;
3632 * check "[prefix]continuous-clock"
3633 * SND_SOC_DAIFMT_CLOCK_MASK area
3635 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
3636 if (of_property_read_bool(np, prop))
3637 format |= SND_SOC_DAIFMT_CONT;
3639 format |= SND_SOC_DAIFMT_GATED;
3642 * check "[prefix]bitclock-inversion"
3643 * check "[prefix]frame-inversion"
3644 * SND_SOC_DAIFMT_INV_MASK area
3646 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3647 bit = !!of_get_property(np, prop, NULL);
3649 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3650 frame = !!of_get_property(np, prop, NULL);
3652 switch ((bit << 4) + frame) {
3654 format |= SND_SOC_DAIFMT_IB_IF;
3657 format |= SND_SOC_DAIFMT_IB_NF;
3660 format |= SND_SOC_DAIFMT_NB_IF;
3663 /* SND_SOC_DAIFMT_NB_NF is default */
3668 * check "[prefix]bitclock-master"
3669 * check "[prefix]frame-master"
3670 * SND_SOC_DAIFMT_MASTER_MASK area
3672 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3673 bit = !!of_get_property(np, prop, NULL);
3674 if (bit && bitclkmaster)
3675 *bitclkmaster = of_parse_phandle(np, prop, 0);
3677 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3678 frame = !!of_get_property(np, prop, NULL);
3679 if (frame && framemaster)
3680 *framemaster = of_parse_phandle(np, prop, 0);
3682 switch ((bit << 4) + frame) {
3684 format |= SND_SOC_DAIFMT_CBM_CFM;
3687 format |= SND_SOC_DAIFMT_CBM_CFS;
3690 format |= SND_SOC_DAIFMT_CBS_CFM;
3693 format |= SND_SOC_DAIFMT_CBS_CFS;
3699 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3701 int snd_soc_get_dai_id(struct device_node *ep)
3703 struct snd_soc_component *pos;
3704 struct device_node *node;
3707 node = of_graph_get_port_parent(ep);
3710 * For example HDMI case, HDMI has video/sound port,
3711 * but ALSA SoC needs sound port number only.
3712 * Thus counting HDMI DT port/endpoint doesn't work.
3713 * Then, it should have .of_xlate_dai_id
3716 mutex_lock(&client_mutex);
3717 list_for_each_entry(pos, &component_list, list) {
3718 struct device_node *component_of_node = pos->dev->of_node;
3720 if (!component_of_node && pos->dev->parent)
3721 component_of_node = pos->dev->parent->of_node;
3723 if (component_of_node != node)
3726 if (pos->driver->of_xlate_dai_id)
3727 ret = pos->driver->of_xlate_dai_id(pos, ep);
3731 mutex_unlock(&client_mutex);
3737 EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3739 int snd_soc_get_dai_name(struct of_phandle_args *args,
3740 const char **dai_name)
3742 struct snd_soc_component *pos;
3743 struct device_node *component_of_node;
3744 int ret = -EPROBE_DEFER;
3746 mutex_lock(&client_mutex);
3747 list_for_each_entry(pos, &component_list, list) {
3748 component_of_node = pos->dev->of_node;
3749 if (!component_of_node && pos->dev->parent)
3750 component_of_node = pos->dev->parent->of_node;
3752 if (component_of_node != args->np)
3755 if (pos->driver->of_xlate_dai_name) {
3756 ret = pos->driver->of_xlate_dai_name(pos,
3760 struct snd_soc_dai *dai;
3763 switch (args->args_count) {
3765 id = 0; /* same as dai_drv[0] */
3775 if (id < 0 || id >= pos->num_dai) {
3782 /* find target DAI */
3783 list_for_each_entry(dai, &pos->dai_list, list) {
3789 *dai_name = dai->driver->name;
3791 *dai_name = pos->name;
3796 mutex_unlock(&client_mutex);
3799 EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
3801 int snd_soc_of_get_dai_name(struct device_node *of_node,
3802 const char **dai_name)
3804 struct of_phandle_args args;
3807 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3808 "#sound-dai-cells", 0, &args);
3812 ret = snd_soc_get_dai_name(&args, dai_name);
3814 of_node_put(args.np);
3818 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3821 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3822 * @dai_link: DAI link
3824 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3826 void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3828 struct snd_soc_dai_link_component *component = dai_link->codecs;
3831 for (index = 0; index < dai_link->num_codecs; index++, component++) {
3832 if (!component->of_node)
3834 of_node_put(component->of_node);
3835 component->of_node = NULL;
3838 EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3841 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3843 * @of_node: Device node
3844 * @dai_link: DAI link
3846 * Builds an array of CODEC DAI components from the DAI link property
3848 * The array is set in the DAI link and the number of DAIs is set accordingly.
3849 * The device nodes in the array (of_node) must be dereferenced by calling
3850 * snd_soc_of_put_dai_link_codecs() on @dai_link.
3852 * Returns 0 for success
3854 int snd_soc_of_get_dai_link_codecs(struct device *dev,
3855 struct device_node *of_node,
3856 struct snd_soc_dai_link *dai_link)
3858 struct of_phandle_args args;
3859 struct snd_soc_dai_link_component *component;
3861 int index, num_codecs, ret;
3863 /* Count the number of CODECs */
3865 num_codecs = of_count_phandle_with_args(of_node, name,
3866 "#sound-dai-cells");
3867 if (num_codecs <= 0) {
3868 if (num_codecs == -ENOENT)
3869 dev_err(dev, "No 'sound-dai' property\n");
3871 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3874 component = devm_kcalloc(dev,
3875 num_codecs, sizeof(*component),
3879 dai_link->codecs = component;
3880 dai_link->num_codecs = num_codecs;
3882 /* Parse the list */
3883 for (index = 0, component = dai_link->codecs;
3884 index < dai_link->num_codecs;
3885 index++, component++) {
3886 ret = of_parse_phandle_with_args(of_node, name,
3891 component->of_node = args.np;
3892 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3898 snd_soc_of_put_dai_link_codecs(dai_link);
3899 dai_link->codecs = NULL;
3900 dai_link->num_codecs = 0;
3903 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3905 static int __init snd_soc_init(void)
3907 snd_soc_debugfs_init();
3908 snd_soc_util_init();
3910 return platform_driver_register(&soc_driver);
3912 module_init(snd_soc_init);
3914 static void __exit snd_soc_exit(void)
3916 snd_soc_util_exit();
3917 snd_soc_debugfs_exit();
3919 platform_driver_unregister(&soc_driver);
3921 module_exit(snd_soc_exit);
3923 /* Module information */
3924 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3925 MODULE_DESCRIPTION("ALSA SoC Core");
3926 MODULE_LICENSE("GPL");
3927 MODULE_ALIAS("platform:soc-audio");