1 // SPDX-License-Identifier: GPL-2.0+
3 // soc-core.c -- ALSA SoC Audio Layer
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Copyright 2005 Openedhand Ltd.
7 // Copyright (C) 2010 Slimlogic Ltd.
8 // Copyright (C) 2010 Texas Instruments Inc.
11 // with code, comments and ideas from :-
15 // o Add hw rules to enforce rates, etc.
16 // o More testing with other codecs/machines.
17 // o Add more codecs and platforms to ensure good API coverage.
18 // o Support TDM on PCM and I2S
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
25 #include <linux/bitops.h>
26 #include <linux/debugfs.h>
27 #include <linux/platform_device.h>
28 #include <linux/pinctrl/consumer.h>
29 #include <linux/ctype.h>
30 #include <linux/slab.h>
32 #include <linux/of_graph.h>
33 #include <linux/dmi.h>
34 #include <sound/core.h>
35 #include <sound/jack.h>
36 #include <sound/pcm.h>
37 #include <sound/pcm_params.h>
38 #include <sound/soc.h>
39 #include <sound/soc-dpcm.h>
40 #include <sound/soc-topology.h>
41 #include <sound/initval.h>
43 #define CREATE_TRACE_POINTS
44 #include <trace/events/asoc.h>
48 #ifdef CONFIG_DEBUG_FS
49 struct dentry *snd_soc_debugfs_root;
50 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
53 static DEFINE_MUTEX(client_mutex);
54 static LIST_HEAD(component_list);
55 static LIST_HEAD(unbind_card_list);
57 #define for_each_component(component) \
58 list_for_each_entry(component, &component_list, list)
61 * This is a timeout to do a DAPM powerdown after a stream is closed().
62 * It can be used to eliminate pops between different playback streams, e.g.
63 * between two audio tracks.
65 static int pmdown_time = 5000;
66 module_param(pmdown_time, int, 0);
67 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
70 * If a DMI filed contain strings in this blacklist (e.g.
71 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
72 * as invalid and dropped when setting the card long name from DMI info.
74 static const char * const dmi_blacklist[] = {
75 "To be filled by OEM",
81 NULL, /* terminator */
84 static ssize_t pmdown_time_show(struct device *dev,
85 struct device_attribute *attr, char *buf)
87 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
89 return sprintf(buf, "%ld\n", rtd->pmdown_time);
92 static ssize_t pmdown_time_set(struct device *dev,
93 struct device_attribute *attr,
94 const char *buf, size_t count)
96 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
99 ret = kstrtol(buf, 10, &rtd->pmdown_time);
106 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
108 static struct attribute *soc_dev_attrs[] = {
109 &dev_attr_pmdown_time.attr,
113 static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
114 struct attribute *attr, int idx)
116 struct device *dev = kobj_to_dev(kobj);
117 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
119 if (attr == &dev_attr_pmdown_time.attr)
120 return attr->mode; /* always visible */
121 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
124 static const struct attribute_group soc_dapm_dev_group = {
125 .attrs = soc_dapm_dev_attrs,
126 .is_visible = soc_dev_attr_is_visible,
129 static const struct attribute_group soc_dev_group = {
130 .attrs = soc_dev_attrs,
131 .is_visible = soc_dev_attr_is_visible,
134 static const struct attribute_group *soc_dev_attr_groups[] = {
140 #ifdef CONFIG_DEBUG_FS
141 static void soc_init_component_debugfs(struct snd_soc_component *component)
143 if (!component->card->debugfs_card_root)
146 if (component->debugfs_prefix) {
149 name = kasprintf(GFP_KERNEL, "%s:%s",
150 component->debugfs_prefix, component->name);
152 component->debugfs_root = debugfs_create_dir(name,
153 component->card->debugfs_card_root);
157 component->debugfs_root = debugfs_create_dir(component->name,
158 component->card->debugfs_card_root);
161 if (!component->debugfs_root) {
162 dev_warn(component->dev,
163 "ASoC: Failed to create component debugfs directory\n");
167 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
168 component->debugfs_root);
171 static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
173 debugfs_remove_recursive(component->debugfs_root);
176 static int dai_list_show(struct seq_file *m, void *v)
178 struct snd_soc_component *component;
179 struct snd_soc_dai *dai;
181 mutex_lock(&client_mutex);
183 for_each_component(component)
184 for_each_component_dais(component, dai)
185 seq_printf(m, "%s\n", dai->name);
187 mutex_unlock(&client_mutex);
191 DEFINE_SHOW_ATTRIBUTE(dai_list);
193 static int component_list_show(struct seq_file *m, void *v)
195 struct snd_soc_component *component;
197 mutex_lock(&client_mutex);
199 for_each_component(component)
200 seq_printf(m, "%s\n", component->name);
202 mutex_unlock(&client_mutex);
206 DEFINE_SHOW_ATTRIBUTE(component_list);
208 static void soc_init_card_debugfs(struct snd_soc_card *card)
210 if (!snd_soc_debugfs_root)
213 card->debugfs_card_root = debugfs_create_dir(card->name,
214 snd_soc_debugfs_root);
215 if (!card->debugfs_card_root) {
217 "ASoC: Failed to create card debugfs directory\n");
221 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
222 card->debugfs_card_root,
224 if (!card->debugfs_pop_time)
226 "ASoC: Failed to create pop time debugfs file\n");
229 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
231 debugfs_remove_recursive(card->debugfs_card_root);
234 static void snd_soc_debugfs_init(void)
236 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
237 if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) {
238 pr_warn("ASoC: Failed to create debugfs directory\n");
239 snd_soc_debugfs_root = NULL;
243 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
245 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
247 if (!debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
248 &component_list_fops))
249 pr_warn("ASoC: Failed to create component list debugfs file\n");
252 static void snd_soc_debugfs_exit(void)
254 debugfs_remove_recursive(snd_soc_debugfs_root);
259 static inline void soc_init_component_debugfs(
260 struct snd_soc_component *component)
264 static inline void soc_cleanup_component_debugfs(
265 struct snd_soc_component *component)
269 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
273 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
277 static inline void snd_soc_debugfs_init(void)
281 static inline void snd_soc_debugfs_exit(void)
287 static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
288 struct snd_soc_component *component)
290 struct snd_soc_rtdcom_list *rtdcom;
291 struct snd_soc_rtdcom_list *new_rtdcom;
293 for_each_rtdcom(rtd, rtdcom) {
294 /* already connected */
295 if (rtdcom->component == component)
299 new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
303 new_rtdcom->component = component;
304 INIT_LIST_HEAD(&new_rtdcom->list);
306 list_add_tail(&new_rtdcom->list, &rtd->component_list);
311 static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
313 struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
315 for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
318 INIT_LIST_HEAD(&rtd->component_list);
321 struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
322 const char *driver_name)
324 struct snd_soc_rtdcom_list *rtdcom;
329 for_each_rtdcom(rtd, rtdcom) {
330 const char *component_name = rtdcom->component->driver->name;
335 if ((component_name == driver_name) ||
336 strcmp(component_name, driver_name) == 0)
337 return rtdcom->component;
342 EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
344 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
345 const char *dai_link, int stream)
347 struct snd_soc_pcm_runtime *rtd;
349 for_each_card_rtds(card, rtd) {
350 if (rtd->dai_link->no_pcm &&
351 !strcmp(rtd->dai_link->name, dai_link))
352 return rtd->pcm->streams[stream].substream;
354 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
357 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
359 static const struct snd_soc_ops null_snd_soc_ops;
361 static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
362 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
364 struct snd_soc_pcm_runtime *rtd;
366 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
370 INIT_LIST_HEAD(&rtd->component_list);
372 rtd->dai_link = dai_link;
373 if (!rtd->dai_link->ops)
374 rtd->dai_link->ops = &null_snd_soc_ops;
376 rtd->codec_dais = kcalloc(dai_link->num_codecs,
377 sizeof(struct snd_soc_dai *),
379 if (!rtd->codec_dais) {
387 static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
389 kfree(rtd->codec_dais);
390 snd_soc_rtdcom_del_all(rtd);
394 static void soc_add_pcm_runtime(struct snd_soc_card *card,
395 struct snd_soc_pcm_runtime *rtd)
397 list_add_tail(&rtd->list, &card->rtd_list);
398 rtd->num = card->num_rtd;
402 static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
404 struct snd_soc_pcm_runtime *rtd, *_rtd;
406 for_each_card_rtds_safe(card, rtd, _rtd) {
407 list_del(&rtd->list);
408 soc_free_pcm_runtime(rtd);
414 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
415 const char *dai_link)
417 struct snd_soc_pcm_runtime *rtd;
419 for_each_card_rtds(card, rtd) {
420 if (!strcmp(rtd->dai_link->name, dai_link))
423 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
426 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
428 static void codec2codec_close_delayed_work(struct work_struct *work)
431 * Currently nothing to do for c2c links
432 * Since c2c links are internal nodes in the DAPM graph and
433 * don't interface with the outside world or application layer
434 * we don't have to do any special handling on close.
438 #ifdef CONFIG_PM_SLEEP
439 /* powers down audio subsystem for suspend */
440 int snd_soc_suspend(struct device *dev)
442 struct snd_soc_card *card = dev_get_drvdata(dev);
443 struct snd_soc_component *component;
444 struct snd_soc_pcm_runtime *rtd;
447 /* If the card is not initialized yet there is nothing to do */
448 if (!card->instantiated)
452 * Due to the resume being scheduled into a workqueue we could
453 * suspend before that's finished - wait for it to complete.
455 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
457 /* we're going to block userspace touching us until resume completes */
458 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
460 /* mute any active DACs */
461 for_each_card_rtds(card, rtd) {
462 struct snd_soc_dai *dai;
464 if (rtd->dai_link->ignore_suspend)
467 for_each_rtd_codec_dai(rtd, i, dai) {
468 struct snd_soc_dai_driver *drv = dai->driver;
470 if (drv->ops->digital_mute && dai->playback_active)
471 drv->ops->digital_mute(dai, 1);
475 /* suspend all pcms */
476 for_each_card_rtds(card, rtd) {
477 if (rtd->dai_link->ignore_suspend)
480 snd_pcm_suspend_all(rtd->pcm);
483 if (card->suspend_pre)
484 card->suspend_pre(card);
486 for_each_card_rtds(card, rtd) {
487 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
489 if (rtd->dai_link->ignore_suspend)
492 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
493 cpu_dai->driver->suspend(cpu_dai);
496 /* close any waiting streams */
497 for_each_card_rtds(card, rtd)
498 flush_delayed_work(&rtd->delayed_work);
500 for_each_card_rtds(card, rtd) {
502 if (rtd->dai_link->ignore_suspend)
505 snd_soc_dapm_stream_event(rtd,
506 SNDRV_PCM_STREAM_PLAYBACK,
507 SND_SOC_DAPM_STREAM_SUSPEND);
509 snd_soc_dapm_stream_event(rtd,
510 SNDRV_PCM_STREAM_CAPTURE,
511 SND_SOC_DAPM_STREAM_SUSPEND);
514 /* Recheck all endpoints too, their state is affected by suspend */
515 dapm_mark_endpoints_dirty(card);
516 snd_soc_dapm_sync(&card->dapm);
518 /* suspend all COMPONENTs */
519 for_each_card_components(card, component) {
520 struct snd_soc_dapm_context *dapm =
521 snd_soc_component_get_dapm(component);
524 * If there are paths active then the COMPONENT will be held
525 * with bias _ON and should not be suspended.
527 if (!component->suspended) {
528 switch (snd_soc_dapm_get_bias_level(dapm)) {
529 case SND_SOC_BIAS_STANDBY:
531 * If the COMPONENT is capable of idle
532 * bias off then being in STANDBY
533 * means it's doing something,
534 * otherwise fall through.
536 if (dapm->idle_bias_off) {
537 dev_dbg(component->dev,
538 "ASoC: idle_bias_off CODEC on over suspend\n");
543 case SND_SOC_BIAS_OFF:
544 if (component->driver->suspend)
545 component->driver->suspend(component);
546 component->suspended = 1;
547 if (component->regmap)
548 regcache_mark_dirty(component->regmap);
549 /* deactivate pins to sleep state */
550 pinctrl_pm_select_sleep_state(component->dev);
553 dev_dbg(component->dev,
554 "ASoC: COMPONENT is on over suspend\n");
560 for_each_card_rtds(card, rtd) {
561 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
563 if (rtd->dai_link->ignore_suspend)
566 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
567 cpu_dai->driver->suspend(cpu_dai);
569 /* deactivate pins to sleep state */
570 pinctrl_pm_select_sleep_state(cpu_dai->dev);
573 if (card->suspend_post)
574 card->suspend_post(card);
578 EXPORT_SYMBOL_GPL(snd_soc_suspend);
581 * deferred resume work, so resume can complete before we finished
582 * setting our codec back up, which can be very slow on I2C
584 static void soc_resume_deferred(struct work_struct *work)
586 struct snd_soc_card *card =
587 container_of(work, struct snd_soc_card,
588 deferred_resume_work);
589 struct snd_soc_pcm_runtime *rtd;
590 struct snd_soc_component *component;
594 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
595 * so userspace apps are blocked from touching us
598 dev_dbg(card->dev, "ASoC: starting resume work\n");
600 /* Bring us up into D2 so that DAPM starts enabling things */
601 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
603 if (card->resume_pre)
604 card->resume_pre(card);
606 /* resume control bus DAIs */
607 for_each_card_rtds(card, rtd) {
608 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
610 if (rtd->dai_link->ignore_suspend)
613 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
614 cpu_dai->driver->resume(cpu_dai);
617 for_each_card_components(card, component) {
618 if (component->suspended) {
619 if (component->driver->resume)
620 component->driver->resume(component);
621 component->suspended = 0;
625 for_each_card_rtds(card, rtd) {
627 if (rtd->dai_link->ignore_suspend)
630 snd_soc_dapm_stream_event(rtd,
631 SNDRV_PCM_STREAM_PLAYBACK,
632 SND_SOC_DAPM_STREAM_RESUME);
634 snd_soc_dapm_stream_event(rtd,
635 SNDRV_PCM_STREAM_CAPTURE,
636 SND_SOC_DAPM_STREAM_RESUME);
639 /* unmute any active DACs */
640 for_each_card_rtds(card, rtd) {
641 struct snd_soc_dai *dai;
643 if (rtd->dai_link->ignore_suspend)
646 for_each_rtd_codec_dai(rtd, i, dai) {
647 struct snd_soc_dai_driver *drv = dai->driver;
649 if (drv->ops->digital_mute && dai->playback_active)
650 drv->ops->digital_mute(dai, 0);
654 for_each_card_rtds(card, rtd) {
655 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
657 if (rtd->dai_link->ignore_suspend)
660 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
661 cpu_dai->driver->resume(cpu_dai);
664 if (card->resume_post)
665 card->resume_post(card);
667 dev_dbg(card->dev, "ASoC: resume work completed\n");
669 /* Recheck all endpoints too, their state is affected by suspend */
670 dapm_mark_endpoints_dirty(card);
671 snd_soc_dapm_sync(&card->dapm);
673 /* userspace can access us now we are back as we were before */
674 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
677 /* powers up audio subsystem after a suspend */
678 int snd_soc_resume(struct device *dev)
680 struct snd_soc_card *card = dev_get_drvdata(dev);
681 bool bus_control = false;
682 struct snd_soc_pcm_runtime *rtd;
684 /* If the card is not initialized yet there is nothing to do */
685 if (!card->instantiated)
688 /* activate pins from sleep state */
689 for_each_card_rtds(card, rtd) {
690 struct snd_soc_dai *codec_dai;
691 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
695 pinctrl_pm_select_default_state(cpu_dai->dev);
697 for_each_rtd_codec_dai(rtd, j, codec_dai) {
698 if (codec_dai->active)
699 pinctrl_pm_select_default_state(codec_dai->dev);
704 * DAIs that also act as the control bus master might have other drivers
705 * hanging off them so need to resume immediately. Other drivers don't
706 * have that problem and may take a substantial amount of time to resume
707 * due to I/O costs and anti-pop so handle them out of line.
709 for_each_card_rtds(card, rtd) {
710 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
712 bus_control |= cpu_dai->driver->bus_control;
715 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
716 soc_resume_deferred(&card->deferred_resume_work);
718 dev_dbg(dev, "ASoC: Scheduling resume work\n");
719 if (!schedule_work(&card->deferred_resume_work))
720 dev_err(dev, "ASoC: resume work item may be lost\n");
725 EXPORT_SYMBOL_GPL(snd_soc_resume);
727 #define snd_soc_suspend NULL
728 #define snd_soc_resume NULL
731 static const struct snd_soc_dai_ops null_dai_ops = {
734 static struct snd_soc_component *soc_find_component(
735 const struct device_node *of_node, const char *name)
737 struct snd_soc_component *component;
739 lockdep_assert_held(&client_mutex);
741 for_each_component(component) {
743 if (component->dev->of_node == of_node)
745 } else if (strcmp(component->name, name) == 0) {
753 static int snd_soc_is_matching_component(
754 const struct snd_soc_dai_link_component *dlc,
755 struct snd_soc_component *component)
757 struct device_node *component_of_node;
759 component_of_node = component->dev->of_node;
760 if (!component_of_node && component->dev->parent)
761 component_of_node = component->dev->parent->of_node;
763 if (dlc->of_node && component_of_node != dlc->of_node)
765 if (dlc->name && strcmp(component->name, dlc->name))
772 * snd_soc_find_dai - Find a registered DAI
774 * @dlc: name of the DAI or the DAI driver and optional component info to match
776 * This function will search all registered components and their DAIs to
777 * find the DAI of the same name. The component's of_node and name
778 * should also match if being specified.
780 * Return: pointer of DAI, or NULL if not found.
782 struct snd_soc_dai *snd_soc_find_dai(
783 const struct snd_soc_dai_link_component *dlc)
785 struct snd_soc_component *component;
786 struct snd_soc_dai *dai;
788 lockdep_assert_held(&client_mutex);
790 /* Find CPU DAI from registered DAIs */
791 for_each_component(component) {
792 if (!snd_soc_is_matching_component(dlc, component))
794 for_each_component_dais(component, dai) {
795 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
796 && (!dai->driver->name
797 || strcmp(dai->driver->name, dlc->dai_name)))
806 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 for_each_card_links_safe(card, link, _link) {
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 for_each_card_rtds(card, rtd) {
855 if (rtd->dai_link == dai_link)
862 static int soc_bind_dai_link(struct snd_soc_card *card,
863 struct snd_soc_dai_link *dai_link)
865 struct snd_soc_pcm_runtime *rtd;
866 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
867 struct snd_soc_dai_link_component cpu_dai_component;
868 struct snd_soc_component *component;
869 struct snd_soc_dai **codec_dais;
872 if (dai_link->ignore)
875 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
877 if (soc_is_dai_link_bound(card, dai_link)) {
878 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
883 rtd = soc_new_pcm_runtime(card, dai_link);
887 cpu_dai_component.name = dai_link->cpu_name;
888 cpu_dai_component.of_node = dai_link->cpu_of_node;
889 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
890 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
892 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
893 dai_link->cpu_dai_name);
896 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
898 rtd->num_codecs = dai_link->num_codecs;
900 /* Find CODEC from registered CODECs */
901 /* we can use for_each_rtd_codec_dai() after this */
902 codec_dais = rtd->codec_dais;
903 for (i = 0; i < rtd->num_codecs; i++) {
904 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
905 if (!codec_dais[i]) {
906 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
910 snd_soc_rtdcom_add(rtd, codec_dais[i]->component);
913 /* Single codec links expect codec and codec_dai in runtime data */
914 rtd->codec_dai = codec_dais[0];
916 /* find one from the set of registered platforms */
917 for_each_component(component) {
918 if (!snd_soc_is_matching_component(dai_link->platform,
922 snd_soc_rtdcom_add(rtd, component);
925 soc_add_pcm_runtime(card, rtd);
929 soc_free_pcm_runtime(rtd);
930 return -EPROBE_DEFER;
933 static void soc_remove_component(struct snd_soc_component *component)
935 if (!component->card)
938 list_del(&component->card_list);
940 if (component->driver->remove)
941 component->driver->remove(component);
943 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
945 soc_cleanup_component_debugfs(component);
946 component->card = NULL;
947 module_put(component->dev->driver->owner);
950 static void soc_remove_dai(struct snd_soc_dai *dai, int order)
954 if (!dai || !dai->probed ||
955 dai->driver->remove_order != order)
958 if (dai->driver->remove) {
959 err = dai->driver->remove(dai);
962 "ASoC: failed to remove %s: %d\n",
968 static void soc_remove_link_dais(struct snd_soc_card *card,
969 struct snd_soc_pcm_runtime *rtd, int order)
972 struct snd_soc_dai *codec_dai;
974 /* unregister the rtd device */
975 if (rtd->dev_registered) {
976 device_unregister(rtd->dev);
977 rtd->dev_registered = 0;
980 /* remove the CODEC DAI */
981 for_each_rtd_codec_dai(rtd, i, codec_dai)
982 soc_remove_dai(codec_dai, order);
984 soc_remove_dai(rtd->cpu_dai, order);
987 static void soc_remove_link_components(struct snd_soc_card *card,
988 struct snd_soc_pcm_runtime *rtd, int order)
990 struct snd_soc_component *component;
991 struct snd_soc_rtdcom_list *rtdcom;
993 for_each_rtdcom(rtd, rtdcom) {
994 component = rtdcom->component;
996 if (component->driver->remove_order == order)
997 soc_remove_component(component);
1001 static void soc_remove_dai_links(struct snd_soc_card *card)
1004 struct snd_soc_pcm_runtime *rtd;
1005 struct snd_soc_dai_link *link, *_link;
1007 for_each_comp_order(order) {
1008 for_each_card_rtds(card, rtd)
1009 soc_remove_link_dais(card, rtd, order);
1012 for_each_comp_order(order) {
1013 for_each_card_rtds(card, rtd)
1014 soc_remove_link_components(card, rtd, order);
1017 for_each_card_links_safe(card, link, _link) {
1018 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1019 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1022 list_del(&link->list);
1026 static int snd_soc_init_platform(struct snd_soc_card *card,
1027 struct snd_soc_dai_link *dai_link)
1029 struct snd_soc_dai_link_component *platform = dai_link->platform;
1034 * this function should be removed in the future
1036 /* convert Legacy platform link */
1038 platform = devm_kzalloc(card->dev,
1039 sizeof(struct snd_soc_dai_link_component),
1044 dai_link->platform = platform;
1045 platform->name = dai_link->platform_name;
1046 platform->of_node = dai_link->platform_of_node;
1047 platform->dai_name = NULL;
1050 /* if there's no platform we match on the empty platform */
1051 if (!platform->name &&
1053 platform->name = "snd-soc-dummy";
1058 static int snd_soc_init_multicodec(struct snd_soc_card *card,
1059 struct snd_soc_dai_link *dai_link)
1061 /* Legacy codec/codec_dai link is a single entry in multicodec */
1062 if (dai_link->codec_name || dai_link->codec_of_node ||
1063 dai_link->codec_dai_name) {
1064 dai_link->num_codecs = 1;
1066 dai_link->codecs = devm_kzalloc(card->dev,
1067 sizeof(struct snd_soc_dai_link_component),
1069 if (!dai_link->codecs)
1072 dai_link->codecs[0].name = dai_link->codec_name;
1073 dai_link->codecs[0].of_node = dai_link->codec_of_node;
1074 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1077 if (!dai_link->codecs) {
1078 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1085 static int soc_init_dai_link(struct snd_soc_card *card,
1086 struct snd_soc_dai_link *link)
1089 struct snd_soc_dai_link_component *codec;
1091 ret = snd_soc_init_platform(card, link);
1093 dev_err(card->dev, "ASoC: failed to init multiplatform\n");
1097 ret = snd_soc_init_multicodec(card, link);
1099 dev_err(card->dev, "ASoC: failed to init multicodec\n");
1103 for_each_link_codecs(link, i, codec) {
1105 * Codec must be specified by 1 of name or OF node,
1106 * not both or neither.
1108 if (!!codec->name ==
1110 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1114 /* Codec DAI name must be specified */
1115 if (!codec->dai_name) {
1116 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1123 * Platform may be specified by either name or OF node, but
1124 * can be left unspecified, and a dummy platform will be used.
1126 if (link->platform->name && link->platform->of_node) {
1128 "ASoC: Both platform name/of_node are set for %s\n",
1133 * CPU device may be specified by either name or OF node, but
1134 * can be left unspecified, and will be matched based on DAI
1137 if (link->cpu_name && link->cpu_of_node) {
1139 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1144 * At least one of CPU DAI name or CPU device name/node must be
1147 if (!link->cpu_dai_name &&
1148 !(link->cpu_name || link->cpu_of_node)) {
1150 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1158 void snd_soc_disconnect_sync(struct device *dev)
1160 struct snd_soc_component *component =
1161 snd_soc_lookup_component(dev, NULL);
1163 if (!component || !component->card)
1166 snd_card_disconnect_sync(component->card->snd_card);
1168 EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
1171 * snd_soc_add_dai_link - Add a DAI link dynamically
1172 * @card: The ASoC card to which the DAI link is added
1173 * @dai_link: The new DAI link to add
1175 * This function adds a DAI link to the ASoC card's link list.
1177 * Note: Topology can use this API to add DAI links when probing the
1178 * topology component. And machine drivers can still define static
1179 * DAI links in dai_link array.
1181 int snd_soc_add_dai_link(struct snd_soc_card *card,
1182 struct snd_soc_dai_link *dai_link)
1184 if (dai_link->dobj.type
1185 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1186 dev_err(card->dev, "Invalid dai link type %d\n",
1187 dai_link->dobj.type);
1191 lockdep_assert_held(&client_mutex);
1193 * Notify the machine driver for extra initialization
1194 * on the link created by topology.
1196 if (dai_link->dobj.type && card->add_dai_link)
1197 card->add_dai_link(card, dai_link);
1199 list_add_tail(&dai_link->list, &card->dai_link_list);
1203 EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1206 * snd_soc_remove_dai_link - Remove a DAI link from the list
1207 * @card: The ASoC card that owns the link
1208 * @dai_link: The DAI link to remove
1210 * This function removes a DAI link from the ASoC card's link list.
1212 * For DAI links previously added by topology, topology should
1213 * remove them by using the dobj embedded in the link.
1215 void snd_soc_remove_dai_link(struct snd_soc_card *card,
1216 struct snd_soc_dai_link *dai_link)
1218 struct snd_soc_dai_link *link, *_link;
1220 if (dai_link->dobj.type
1221 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1222 dev_err(card->dev, "Invalid dai link type %d\n",
1223 dai_link->dobj.type);
1227 lockdep_assert_held(&client_mutex);
1229 * Notify the machine driver for extra destruction
1230 * on the link created by topology.
1232 if (dai_link->dobj.type && card->remove_dai_link)
1233 card->remove_dai_link(card, dai_link);
1235 for_each_card_links_safe(card, link, _link) {
1236 if (link == dai_link) {
1237 list_del(&link->list);
1242 EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1244 static void soc_set_of_name_prefix(struct snd_soc_component *component)
1246 struct device_node *component_of_node = component->dev->of_node;
1250 if (!component_of_node && component->dev->parent)
1251 component_of_node = component->dev->parent->of_node;
1253 ret = of_property_read_string(component_of_node, "sound-name-prefix",
1256 component->name_prefix = str;
1259 static void soc_set_name_prefix(struct snd_soc_card *card,
1260 struct snd_soc_component *component)
1264 for (i = 0; i < card->num_configs && card->codec_conf; i++) {
1265 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1266 struct device_node *component_of_node = component->dev->of_node;
1268 if (!component_of_node && component->dev->parent)
1269 component_of_node = component->dev->parent->of_node;
1271 if (map->of_node && component_of_node != map->of_node)
1273 if (map->dev_name && strcmp(component->name, map->dev_name))
1275 component->name_prefix = map->name_prefix;
1280 * If there is no configuration table or no match in the table,
1281 * check if a prefix is provided in the node
1283 soc_set_of_name_prefix(component);
1286 static int soc_probe_component(struct snd_soc_card *card,
1287 struct snd_soc_component *component)
1289 struct snd_soc_dapm_context *dapm =
1290 snd_soc_component_get_dapm(component);
1291 struct snd_soc_dai *dai;
1294 if (!strcmp(component->name, "snd-soc-dummy"))
1297 if (component->card) {
1298 if (component->card != card) {
1299 dev_err(component->dev,
1300 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1301 card->name, component->card->name);
1307 if (!try_module_get(component->dev->driver->owner))
1310 component->card = card;
1312 soc_set_name_prefix(card, component);
1314 soc_init_component_debugfs(component);
1316 if (component->driver->dapm_widgets) {
1317 ret = snd_soc_dapm_new_controls(dapm,
1318 component->driver->dapm_widgets,
1319 component->driver->num_dapm_widgets);
1322 dev_err(component->dev,
1323 "Failed to create new controls %d\n", ret);
1328 for_each_component_dais(component, dai) {
1329 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1331 dev_err(component->dev,
1332 "Failed to create DAI widgets %d\n", ret);
1337 if (component->driver->probe) {
1338 ret = component->driver->probe(component);
1340 dev_err(component->dev,
1341 "ASoC: failed to probe component %d\n", ret);
1345 WARN(dapm->idle_bias_off &&
1346 dapm->bias_level != SND_SOC_BIAS_OFF,
1347 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1351 /* machine specific init */
1352 if (component->init) {
1353 ret = component->init(component);
1355 dev_err(component->dev,
1356 "Failed to do machine specific init %d\n", ret);
1361 if (component->driver->controls)
1362 snd_soc_add_component_controls(component,
1363 component->driver->controls,
1364 component->driver->num_controls);
1365 if (component->driver->dapm_routes)
1366 snd_soc_dapm_add_routes(dapm,
1367 component->driver->dapm_routes,
1368 component->driver->num_dapm_routes);
1370 list_add(&dapm->list, &card->dapm_list);
1371 /* see for_each_card_components */
1372 list_add(&component->card_list, &card->component_dev_list);
1377 soc_cleanup_component_debugfs(component);
1378 component->card = NULL;
1379 module_put(component->dev->driver->owner);
1384 static void rtd_release(struct device *dev)
1389 static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1394 /* register the rtd device */
1395 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1398 device_initialize(rtd->dev);
1399 rtd->dev->parent = rtd->card->dev;
1400 rtd->dev->release = rtd_release;
1401 rtd->dev->groups = soc_dev_attr_groups;
1402 dev_set_name(rtd->dev, "%s", name);
1403 dev_set_drvdata(rtd->dev, rtd);
1404 mutex_init(&rtd->pcm_mutex);
1405 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1406 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1407 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1408 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1409 ret = device_add(rtd->dev);
1411 /* calling put_device() here to free the rtd->dev */
1412 put_device(rtd->dev);
1413 dev_err(rtd->card->dev,
1414 "ASoC: failed to register runtime device: %d\n", ret);
1417 rtd->dev_registered = 1;
1421 static int soc_probe_link_components(struct snd_soc_card *card,
1422 struct snd_soc_pcm_runtime *rtd, int order)
1424 struct snd_soc_component *component;
1425 struct snd_soc_rtdcom_list *rtdcom;
1428 for_each_rtdcom(rtd, rtdcom) {
1429 component = rtdcom->component;
1431 if (component->driver->probe_order == order) {
1432 ret = soc_probe_component(card, component);
1441 static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1444 dai->driver->probe_order != order)
1447 if (dai->driver->probe) {
1448 int ret = dai->driver->probe(dai);
1451 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1462 static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1463 struct snd_soc_pcm_runtime *rtd)
1467 for (i = 0; i < num_dais; ++i) {
1468 struct snd_soc_dai_driver *drv = dais[i]->driver;
1470 if (!rtd->dai_link->no_pcm && drv->pcm_new)
1471 ret = drv->pcm_new(rtd, dais[i]);
1473 dev_err(dais[i]->dev,
1474 "ASoC: Failed to bind %s with pcm device\n",
1483 static int soc_probe_link_dais(struct snd_soc_card *card,
1484 struct snd_soc_pcm_runtime *rtd, int order)
1486 struct snd_soc_dai_link *dai_link = rtd->dai_link;
1487 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1488 struct snd_soc_rtdcom_list *rtdcom;
1489 struct snd_soc_component *component;
1490 struct snd_soc_dai *codec_dai;
1493 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1494 card->name, rtd->num, order);
1496 /* set default power off timeout */
1497 rtd->pmdown_time = pmdown_time;
1499 ret = soc_probe_dai(cpu_dai, order);
1503 /* probe the CODEC DAI */
1504 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1505 ret = soc_probe_dai(codec_dai, order);
1510 /* complete DAI probe during last probe */
1511 if (order != SND_SOC_COMP_ORDER_LAST)
1514 /* do machine specific initialization */
1515 if (dai_link->init) {
1516 ret = dai_link->init(rtd);
1518 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1519 dai_link->name, ret);
1524 if (dai_link->dai_fmt)
1525 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1527 ret = soc_post_component_init(rtd, dai_link->name);
1531 #ifdef CONFIG_DEBUG_FS
1532 /* add DPCM sysfs entries */
1533 if (dai_link->dynamic)
1534 soc_dpcm_debugfs_add(rtd);
1540 * most drivers will register their PCMs using DAI link ordering but
1541 * topology based drivers can use the DAI link id field to set PCM
1542 * device number and then use rtd + a base offset of the BEs.
1544 for_each_rtdcom(rtd, rtdcom) {
1545 component = rtdcom->component;
1547 if (!component->driver->use_dai_pcm_id)
1550 if (rtd->dai_link->no_pcm)
1551 num += component->driver->be_pcm_base;
1553 num = rtd->dai_link->id;
1556 if (cpu_dai->driver->compress_new) {
1557 /* create compress_device" */
1558 ret = cpu_dai->driver->compress_new(rtd, num);
1560 dev_err(card->dev, "ASoC: can't create compress %s\n",
1561 dai_link->stream_name);
1566 if (!dai_link->params) {
1567 /* create the pcm */
1568 ret = soc_new_pcm(rtd, num);
1570 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1571 dai_link->stream_name, ret);
1574 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1577 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1578 rtd->num_codecs, rtd);
1582 INIT_DELAYED_WORK(&rtd->delayed_work,
1583 codec2codec_close_delayed_work);
1590 static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1592 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1593 struct snd_soc_component *component;
1595 struct device_node *codec_of_node;
1597 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1598 /* codecs, usually analog devices */
1599 name = aux_dev->codec_name;
1600 codec_of_node = aux_dev->codec_of_node;
1601 component = soc_find_component(codec_of_node, name);
1604 name = of_node_full_name(codec_of_node);
1607 } else if (aux_dev->name) {
1608 /* generic components */
1609 name = aux_dev->name;
1610 component = soc_find_component(NULL, name);
1614 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1618 component->init = aux_dev->init;
1619 list_add(&component->card_aux_list, &card->aux_comp_list);
1624 dev_err(card->dev, "ASoC: %s not registered\n", name);
1625 return -EPROBE_DEFER;
1628 static int soc_probe_aux_devices(struct snd_soc_card *card)
1630 struct snd_soc_component *comp;
1634 for_each_comp_order(order) {
1635 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
1636 if (comp->driver->probe_order == order) {
1637 ret = soc_probe_component(card, comp);
1640 "ASoC: failed to probe aux component %s %d\n",
1651 static void soc_remove_aux_devices(struct snd_soc_card *card)
1653 struct snd_soc_component *comp, *_comp;
1656 for_each_comp_order(order) {
1657 list_for_each_entry_safe(comp, _comp,
1658 &card->aux_comp_list, card_aux_list) {
1660 if (comp->driver->remove_order == order) {
1661 soc_remove_component(comp);
1662 /* remove it from the card's aux_comp_list */
1663 list_del(&comp->card_aux_list);
1670 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1671 * @rtd: The runtime for which the DAI link format should be changed
1672 * @dai_fmt: The new DAI link format
1674 * This function updates the DAI link format for all DAIs connected to the DAI
1675 * link for the specified runtime.
1677 * Note: For setups with a static format set the dai_fmt field in the
1678 * corresponding snd_dai_link struct instead of using this function.
1680 * Returns 0 on success, otherwise a negative error code.
1682 int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1683 unsigned int dai_fmt)
1685 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1686 struct snd_soc_dai *codec_dai;
1690 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1691 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1692 if (ret != 0 && ret != -ENOTSUPP) {
1693 dev_warn(codec_dai->dev,
1694 "ASoC: Failed to set DAI format: %d\n", ret);
1700 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1701 * the component which has non_legacy_dai_naming is Codec
1703 if (cpu_dai->component->driver->non_legacy_dai_naming) {
1704 unsigned int inv_dai_fmt;
1706 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1707 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1708 case SND_SOC_DAIFMT_CBM_CFM:
1709 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1711 case SND_SOC_DAIFMT_CBM_CFS:
1712 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1714 case SND_SOC_DAIFMT_CBS_CFM:
1715 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1717 case SND_SOC_DAIFMT_CBS_CFS:
1718 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1722 dai_fmt = inv_dai_fmt;
1725 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1726 if (ret != 0 && ret != -ENOTSUPP) {
1727 dev_warn(cpu_dai->dev,
1728 "ASoC: Failed to set DAI format: %d\n", ret);
1734 EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1738 * Trim special characters, and replace '-' with '_' since '-' is used to
1739 * separate different DMI fields in the card long name. Only number and
1740 * alphabet characters and a few separator characters are kept.
1742 static void cleanup_dmi_name(char *name)
1746 for (i = 0; name[i]; i++) {
1747 if (isalnum(name[i]) || (name[i] == '.')
1748 || (name[i] == '_'))
1749 name[j++] = name[i];
1750 else if (name[i] == '-')
1758 * Check if a DMI field is valid, i.e. not containing any string
1759 * in the black list.
1761 static int is_dmi_valid(const char *field)
1765 while (dmi_blacklist[i]) {
1766 if (strstr(field, dmi_blacklist[i]))
1775 * snd_soc_set_dmi_name() - Register DMI names to card
1776 * @card: The card to register DMI names
1777 * @flavour: The flavour "differentiator" for the card amongst its peers.
1779 * An Intel machine driver may be used by many different devices but are
1780 * difficult for userspace to differentiate, since machine drivers ususally
1781 * use their own name as the card short name and leave the card long name
1782 * blank. To differentiate such devices and fix bugs due to lack of
1783 * device-specific configurations, this function allows DMI info to be used
1784 * as the sound card long name, in the format of
1785 * "vendor-product-version-board"
1786 * (Character '-' is used to separate different DMI fields here).
1787 * This will help the user space to load the device-specific Use Case Manager
1788 * (UCM) configurations for the card.
1790 * Possible card long names may be:
1791 * DellInc.-XPS139343-01-0310JH
1792 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1793 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1795 * This function also supports flavoring the card longname to provide
1796 * the extra differentiation, like "vendor-product-version-board-flavor".
1798 * We only keep number and alphabet characters and a few separator characters
1799 * in the card long name since UCM in the user space uses the card long names
1800 * as card configuration directory names and AudoConf cannot support special
1801 * charactors like SPACE.
1803 * Returns 0 on success, otherwise a negative error code.
1805 int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1807 const char *vendor, *product, *product_version, *board;
1808 size_t longname_buf_size = sizeof(card->snd_card->longname);
1811 if (card->long_name)
1812 return 0; /* long name already set by driver or from DMI */
1814 /* make up dmi long name as: vendor.product.version.board */
1815 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1816 if (!vendor || !is_dmi_valid(vendor)) {
1817 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1821 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1823 cleanup_dmi_name(card->dmi_longname);
1825 product = dmi_get_system_info(DMI_PRODUCT_NAME);
1826 if (product && is_dmi_valid(product)) {
1827 len = strlen(card->dmi_longname);
1828 snprintf(card->dmi_longname + len,
1829 longname_buf_size - len,
1832 len++; /* skip the separator "-" */
1833 if (len < longname_buf_size)
1834 cleanup_dmi_name(card->dmi_longname + len);
1837 * some vendors like Lenovo may only put a self-explanatory
1838 * name in the product version field
1840 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
1841 if (product_version && is_dmi_valid(product_version)) {
1842 len = strlen(card->dmi_longname);
1843 snprintf(card->dmi_longname + len,
1844 longname_buf_size - len,
1845 "-%s", product_version);
1848 if (len < longname_buf_size)
1849 cleanup_dmi_name(card->dmi_longname + len);
1853 board = dmi_get_system_info(DMI_BOARD_NAME);
1854 if (board && is_dmi_valid(board)) {
1855 len = strlen(card->dmi_longname);
1856 snprintf(card->dmi_longname + len,
1857 longname_buf_size - len,
1861 if (len < longname_buf_size)
1862 cleanup_dmi_name(card->dmi_longname + len);
1863 } else if (!product) {
1864 /* fall back to using legacy name */
1865 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1869 /* Add flavour to dmi long name */
1871 len = strlen(card->dmi_longname);
1872 snprintf(card->dmi_longname + len,
1873 longname_buf_size - len,
1877 if (len < longname_buf_size)
1878 cleanup_dmi_name(card->dmi_longname + len);
1881 /* set the card long name */
1882 card->long_name = card->dmi_longname;
1886 EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
1887 #endif /* CONFIG_DMI */
1889 static void soc_check_tplg_fes(struct snd_soc_card *card)
1891 struct snd_soc_component *component;
1892 const struct snd_soc_component_driver *comp_drv;
1893 struct snd_soc_dai_link *dai_link;
1896 for_each_component(component) {
1898 /* does this component override FEs ? */
1899 if (!component->driver->ignore_machine)
1902 /* for this machine ? */
1903 if (strcmp(component->driver->ignore_machine,
1904 card->dev->driver->name))
1907 /* machine matches, so override the rtd data */
1908 for_each_card_prelinks(card, i, dai_link) {
1910 /* ignore this FE */
1911 if (dai_link->dynamic) {
1912 dai_link->ignore = true;
1916 dev_info(card->dev, "info: override FE DAI link %s\n",
1917 card->dai_link[i].name);
1919 /* override platform component */
1920 if (snd_soc_init_platform(card, dai_link) < 0) {
1921 dev_err(card->dev, "init platform error");
1924 dai_link->platform->name = component->name;
1926 /* convert non BE into BE */
1927 dai_link->no_pcm = 1;
1929 /* override any BE fixups */
1930 dai_link->be_hw_params_fixup =
1931 component->driver->be_hw_params_fixup;
1934 * most BE links don't set stream name, so set it to
1935 * dai link name if it's NULL to help bind widgets.
1937 if (!dai_link->stream_name)
1938 dai_link->stream_name = dai_link->name;
1941 /* Inform userspace we are using alternate topology */
1942 if (component->driver->topology_name_prefix) {
1944 /* topology shortname created? */
1945 if (!card->topology_shortname_created) {
1946 comp_drv = component->driver;
1948 snprintf(card->topology_shortname, 32, "%s-%s",
1949 comp_drv->topology_name_prefix,
1951 card->topology_shortname_created = true;
1954 /* use topology shortname */
1955 card->name = card->topology_shortname;
1960 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1962 struct snd_soc_pcm_runtime *rtd;
1963 struct snd_soc_dai_link *dai_link;
1966 mutex_lock(&client_mutex);
1967 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1969 /* check whether any platform is ignore machine FE and using topology */
1970 soc_check_tplg_fes(card);
1973 for_each_card_prelinks(card, i, dai_link) {
1974 ret = soc_bind_dai_link(card, dai_link);
1979 /* bind aux_devs too */
1980 for (i = 0; i < card->num_aux_devs; i++) {
1981 ret = soc_bind_aux_dev(card, i);
1986 /* add predefined DAI links to the list */
1987 for_each_card_prelinks(card, i, dai_link)
1988 snd_soc_add_dai_link(card, dai_link);
1990 /* card bind complete so register a sound card */
1991 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1992 card->owner, 0, &card->snd_card);
1995 "ASoC: can't create sound card for card %s: %d\n",
2000 soc_init_card_debugfs(card);
2002 card->dapm.bias_level = SND_SOC_BIAS_OFF;
2003 card->dapm.dev = card->dev;
2004 card->dapm.card = card;
2005 list_add(&card->dapm.list, &card->dapm_list);
2007 #ifdef CONFIG_DEBUG_FS
2008 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2011 #ifdef CONFIG_PM_SLEEP
2012 /* deferred resume work */
2013 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
2016 if (card->dapm_widgets)
2017 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2018 card->num_dapm_widgets);
2020 if (card->of_dapm_widgets)
2021 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2022 card->num_of_dapm_widgets);
2024 /* initialise the sound card only once */
2026 ret = card->probe(card);
2028 goto card_probe_error;
2031 /* probe all components used by DAI links on this card */
2032 for_each_comp_order(order) {
2033 for_each_card_rtds(card, rtd) {
2034 ret = soc_probe_link_components(card, rtd, order);
2037 "ASoC: failed to instantiate card %d\n",
2044 /* probe auxiliary components */
2045 ret = soc_probe_aux_devices(card);
2050 * Find new DAI links added during probing components and bind them.
2051 * Components with topology may bring new DAIs and DAI links.
2053 for_each_card_links(card, dai_link) {
2054 if (soc_is_dai_link_bound(card, dai_link))
2057 ret = soc_init_dai_link(card, dai_link);
2060 ret = soc_bind_dai_link(card, dai_link);
2065 /* probe all DAI links on this card */
2066 for_each_comp_order(order) {
2067 for_each_card_rtds(card, rtd) {
2068 ret = soc_probe_link_dais(card, rtd, order);
2071 "ASoC: failed to instantiate card %d\n",
2078 snd_soc_dapm_link_dai_widgets(card);
2079 snd_soc_dapm_connect_dai_link_widgets(card);
2082 snd_soc_add_card_controls(card, card->controls,
2083 card->num_controls);
2085 if (card->dapm_routes)
2086 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2087 card->num_dapm_routes);
2089 if (card->of_dapm_routes)
2090 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2091 card->num_of_dapm_routes);
2093 /* try to set some sane longname if DMI is available */
2094 snd_soc_set_dmi_name(card, NULL);
2096 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
2098 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2099 "%s", card->long_name ? card->long_name : card->name);
2100 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2101 "%s", card->driver_name ? card->driver_name : card->name);
2102 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2103 switch (card->snd_card->driver[i]) {
2109 if (!isalnum(card->snd_card->driver[i]))
2110 card->snd_card->driver[i] = '_';
2115 if (card->late_probe) {
2116 ret = card->late_probe(card);
2118 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
2120 goto probe_aux_dev_err;
2124 snd_soc_dapm_new_widgets(card);
2126 ret = snd_card_register(card->snd_card);
2128 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2130 goto probe_aux_dev_err;
2133 card->instantiated = 1;
2134 dapm_mark_endpoints_dirty(card);
2135 snd_soc_dapm_sync(&card->dapm);
2136 mutex_unlock(&card->mutex);
2137 mutex_unlock(&client_mutex);
2142 soc_remove_aux_devices(card);
2145 soc_remove_dai_links(card);
2151 snd_soc_dapm_free(&card->dapm);
2152 soc_cleanup_card_debugfs(card);
2153 snd_card_free(card->snd_card);
2156 soc_remove_pcm_runtimes(card);
2157 mutex_unlock(&card->mutex);
2158 mutex_unlock(&client_mutex);
2163 /* probes a new socdev */
2164 static int soc_probe(struct platform_device *pdev)
2166 struct snd_soc_card *card = platform_get_drvdata(pdev);
2169 * no card, so machine driver should be registering card
2170 * we should not be here in that case so ret error
2175 dev_warn(&pdev->dev,
2176 "ASoC: machine %s should use snd_soc_register_card()\n",
2179 /* Bodge while we unpick instantiation */
2180 card->dev = &pdev->dev;
2182 return snd_soc_register_card(card);
2185 static int soc_cleanup_card_resources(struct snd_soc_card *card)
2187 struct snd_soc_pcm_runtime *rtd;
2189 /* make sure any delayed work runs */
2190 for_each_card_rtds(card, rtd)
2191 flush_delayed_work(&rtd->delayed_work);
2193 /* free the ALSA card at first; this syncs with pending operations */
2194 snd_card_free(card->snd_card);
2196 /* remove and free each DAI */
2197 soc_remove_dai_links(card);
2198 soc_remove_pcm_runtimes(card);
2200 /* remove auxiliary devices */
2201 soc_remove_aux_devices(card);
2203 snd_soc_dapm_free(&card->dapm);
2204 soc_cleanup_card_debugfs(card);
2206 /* remove the card */
2213 /* removes a socdev */
2214 static int soc_remove(struct platform_device *pdev)
2216 struct snd_soc_card *card = platform_get_drvdata(pdev);
2218 snd_soc_unregister_card(card);
2222 int snd_soc_poweroff(struct device *dev)
2224 struct snd_soc_card *card = dev_get_drvdata(dev);
2225 struct snd_soc_pcm_runtime *rtd;
2227 if (!card->instantiated)
2231 * Flush out pmdown_time work - we actually do want to run it
2232 * now, we're shutting down so no imminent restart.
2234 for_each_card_rtds(card, rtd)
2235 flush_delayed_work(&rtd->delayed_work);
2237 snd_soc_dapm_shutdown(card);
2239 /* deactivate pins to sleep state */
2240 for_each_card_rtds(card, rtd) {
2241 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2242 struct snd_soc_dai *codec_dai;
2245 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2246 for_each_rtd_codec_dai(rtd, i, codec_dai) {
2247 pinctrl_pm_select_sleep_state(codec_dai->dev);
2253 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2255 const struct dev_pm_ops snd_soc_pm_ops = {
2256 .suspend = snd_soc_suspend,
2257 .resume = snd_soc_resume,
2258 .freeze = snd_soc_suspend,
2259 .thaw = snd_soc_resume,
2260 .poweroff = snd_soc_poweroff,
2261 .restore = snd_soc_resume,
2263 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2265 /* ASoC platform driver */
2266 static struct platform_driver soc_driver = {
2268 .name = "soc-audio",
2269 .pm = &snd_soc_pm_ops,
2272 .remove = soc_remove,
2276 * snd_soc_cnew - create new control
2277 * @_template: control template
2278 * @data: control private data
2279 * @long_name: control long name
2280 * @prefix: control name prefix
2282 * Create a new mixer control from a template control.
2284 * Returns 0 for success, else error.
2286 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2287 void *data, const char *long_name,
2290 struct snd_kcontrol_new template;
2291 struct snd_kcontrol *kcontrol;
2294 memcpy(&template, _template, sizeof(template));
2298 long_name = template.name;
2301 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2305 template.name = name;
2307 template.name = long_name;
2310 kcontrol = snd_ctl_new1(&template, data);
2316 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2318 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2319 const struct snd_kcontrol_new *controls, int num_controls,
2320 const char *prefix, void *data)
2324 for (i = 0; i < num_controls; i++) {
2325 const struct snd_kcontrol_new *control = &controls[i];
2327 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2328 control->name, prefix));
2330 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2331 control->name, err);
2339 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2342 struct snd_card *card = soc_card->snd_card;
2343 struct snd_kcontrol *kctl;
2345 if (unlikely(!name))
2348 list_for_each_entry(kctl, &card->controls, list)
2349 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2353 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2356 * snd_soc_add_component_controls - Add an array of controls to a component.
2358 * @component: Component to add controls to
2359 * @controls: Array of controls to add
2360 * @num_controls: Number of elements in the array
2362 * Return: 0 for success, else error.
2364 int snd_soc_add_component_controls(struct snd_soc_component *component,
2365 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2367 struct snd_card *card = component->card->snd_card;
2369 return snd_soc_add_controls(card, component->dev, controls,
2370 num_controls, component->name_prefix, component);
2372 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2375 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2376 * Convenience function to add a list of controls.
2378 * @soc_card: SoC card to add controls to
2379 * @controls: array of controls to add
2380 * @num_controls: number of elements in the array
2382 * Return 0 for success, else error.
2384 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2385 const struct snd_kcontrol_new *controls, int num_controls)
2387 struct snd_card *card = soc_card->snd_card;
2389 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2392 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2395 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2396 * Convienience function to add a list of controls.
2398 * @dai: DAI to add controls to
2399 * @controls: array of controls to add
2400 * @num_controls: number of elements in the array
2402 * Return 0 for success, else error.
2404 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2405 const struct snd_kcontrol_new *controls, int num_controls)
2407 struct snd_card *card = dai->component->card->snd_card;
2409 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2412 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2415 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2417 * @clk_id: DAI specific clock ID
2418 * @freq: new clock frequency in Hz
2419 * @dir: new clock direction - input/output.
2421 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2423 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2424 unsigned int freq, int dir)
2426 if (dai->driver->ops->set_sysclk)
2427 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2429 return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2432 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2435 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2436 * @component: COMPONENT
2437 * @clk_id: DAI specific clock ID
2438 * @source: Source for the clock
2439 * @freq: new clock frequency in Hz
2440 * @dir: new clock direction - input/output.
2442 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2444 int snd_soc_component_set_sysclk(struct snd_soc_component *component,
2445 int clk_id, int source, unsigned int freq,
2448 if (component->driver->set_sysclk)
2449 return component->driver->set_sysclk(component, clk_id, source,
2454 EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2457 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2459 * @div_id: DAI specific clock divider ID
2460 * @div: new clock divisor.
2462 * Configures the clock dividers. This is used to derive the best DAI bit and
2463 * frame clocks from the system or master clock. It's best to set the DAI bit
2464 * and frame clocks as low as possible to save system power.
2466 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2467 int div_id, int div)
2469 if (dai->driver->ops->set_clkdiv)
2470 return dai->driver->ops->set_clkdiv(dai, div_id, div);
2474 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2477 * snd_soc_dai_set_pll - configure DAI PLL.
2479 * @pll_id: DAI specific PLL ID
2480 * @source: DAI specific source for the PLL
2481 * @freq_in: PLL input clock frequency in Hz
2482 * @freq_out: requested PLL output clock frequency in Hz
2484 * Configures and enables PLL to generate output clock based on input clock.
2486 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2487 unsigned int freq_in, unsigned int freq_out)
2489 if (dai->driver->ops->set_pll)
2490 return dai->driver->ops->set_pll(dai, pll_id, source,
2493 return snd_soc_component_set_pll(dai->component, pll_id, source,
2496 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2499 * snd_soc_component_set_pll - configure component PLL.
2500 * @component: COMPONENT
2501 * @pll_id: DAI specific PLL ID
2502 * @source: DAI specific source for the PLL
2503 * @freq_in: PLL input clock frequency in Hz
2504 * @freq_out: requested PLL output clock frequency in Hz
2506 * Configures and enables PLL to generate output clock based on input clock.
2508 int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2509 int source, unsigned int freq_in,
2510 unsigned int freq_out)
2512 if (component->driver->set_pll)
2513 return component->driver->set_pll(component, pll_id, source,
2518 EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2521 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2523 * @ratio: Ratio of BCLK to Sample rate.
2525 * Configures the DAI for a preset BCLK to sample rate ratio.
2527 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2529 if (dai->driver->ops->set_bclk_ratio)
2530 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2534 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2537 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2539 * @fmt: SND_SOC_DAIFMT_* format value.
2541 * Configures the DAI hardware format and clocking.
2543 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2545 if (dai->driver->ops->set_fmt == NULL)
2547 return dai->driver->ops->set_fmt(dai, fmt);
2549 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2552 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
2553 * @slots: Number of slots in use.
2554 * @tx_mask: bitmask representing active TX slots.
2555 * @rx_mask: bitmask representing active RX slots.
2557 * Generates the TDM tx and rx slot default masks for DAI.
2559 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
2560 unsigned int *tx_mask,
2561 unsigned int *rx_mask)
2563 if (*tx_mask || *rx_mask)
2569 *tx_mask = (1 << slots) - 1;
2570 *rx_mask = (1 << slots) - 1;
2576 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2577 * @dai: The DAI to configure
2578 * @tx_mask: bitmask representing active TX slots.
2579 * @rx_mask: bitmask representing active RX slots.
2580 * @slots: Number of slots in use.
2581 * @slot_width: Width in bits for each slot.
2583 * This function configures the specified DAI for TDM operation. @slot contains
2584 * the total number of slots of the TDM stream and @slot_with the width of each
2585 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2586 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2587 * DAI should write to or read from. If a bit is set the corresponding slot is
2588 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2589 * the first slot, bit 1 to the second slot and so on. The first active slot
2590 * maps to the first channel of the DAI, the second active slot to the second
2591 * channel and so on.
2593 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2594 * @rx_mask and @slot_width will be ignored.
2596 * Returns 0 on success, a negative error code otherwise.
2598 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2599 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2601 if (dai->driver->ops->xlate_tdm_slot_mask)
2602 dai->driver->ops->xlate_tdm_slot_mask(slots,
2603 &tx_mask, &rx_mask);
2605 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
2607 dai->tx_mask = tx_mask;
2608 dai->rx_mask = rx_mask;
2610 if (dai->driver->ops->set_tdm_slot)
2611 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2616 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2619 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2621 * @tx_num: how many TX channels
2622 * @tx_slot: pointer to an array which imply the TX slot number channel
2624 * @rx_num: how many RX channels
2625 * @rx_slot: pointer to an array which imply the RX slot number channel
2628 * configure the relationship between channel number and TDM slot number.
2630 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2631 unsigned int tx_num, unsigned int *tx_slot,
2632 unsigned int rx_num, unsigned int *rx_slot)
2634 if (dai->driver->ops->set_channel_map)
2635 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2640 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2643 * snd_soc_dai_get_channel_map - Get DAI audio channel map
2645 * @tx_num: how many TX channels
2646 * @tx_slot: pointer to an array which imply the TX slot number channel
2648 * @rx_num: how many RX channels
2649 * @rx_slot: pointer to an array which imply the RX slot number channel
2652 int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
2653 unsigned int *tx_num, unsigned int *tx_slot,
2654 unsigned int *rx_num, unsigned int *rx_slot)
2656 if (dai->driver->ops->get_channel_map)
2657 return dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
2662 EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
2665 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2667 * @tristate: tristate enable
2669 * Tristates the DAI so that others can use it.
2671 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2673 if (dai->driver->ops->set_tristate)
2674 return dai->driver->ops->set_tristate(dai, tristate);
2678 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2681 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2683 * @mute: mute enable
2684 * @direction: stream to mute
2686 * Mutes the DAI DAC.
2688 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2691 if (dai->driver->ops->mute_stream)
2692 return dai->driver->ops->mute_stream(dai, mute, direction);
2693 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2694 dai->driver->ops->digital_mute)
2695 return dai->driver->ops->digital_mute(dai, mute);
2699 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2701 static int snd_soc_bind_card(struct snd_soc_card *card)
2703 struct snd_soc_pcm_runtime *rtd;
2706 ret = snd_soc_instantiate_card(card);
2710 /* deactivate pins to sleep state */
2711 for_each_card_rtds(card, rtd) {
2712 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2713 struct snd_soc_dai *codec_dai;
2716 for_each_rtd_codec_dai(rtd, j, codec_dai) {
2717 if (!codec_dai->active)
2718 pinctrl_pm_select_sleep_state(codec_dai->dev);
2721 if (!cpu_dai->active)
2722 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2729 * snd_soc_register_card - Register a card with the ASoC core
2731 * @card: Card to register
2734 int snd_soc_register_card(struct snd_soc_card *card)
2737 struct snd_soc_dai_link *link;
2739 if (!card->name || !card->dev)
2742 for_each_card_prelinks(card, i, link) {
2744 ret = soc_init_dai_link(card, link);
2746 dev_err(card->dev, "ASoC: failed to init link %s\n",
2752 dev_set_drvdata(card->dev, card);
2754 snd_soc_initialize_card_lists(card);
2756 INIT_LIST_HEAD(&card->dai_link_list);
2758 INIT_LIST_HEAD(&card->rtd_list);
2761 INIT_LIST_HEAD(&card->dapm_dirty);
2762 INIT_LIST_HEAD(&card->dobj_list);
2763 card->instantiated = 0;
2764 mutex_init(&card->mutex);
2765 mutex_init(&card->dapm_mutex);
2767 return snd_soc_bind_card(card);
2769 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2771 static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2773 if (card->instantiated) {
2774 card->instantiated = false;
2775 snd_soc_dapm_shutdown(card);
2776 soc_cleanup_card_resources(card);
2778 list_add(&card->list, &unbind_card_list);
2781 list_del(&card->list);
2786 * snd_soc_unregister_card - Unregister a card with the ASoC core
2788 * @card: Card to unregister
2791 int snd_soc_unregister_card(struct snd_soc_card *card)
2793 snd_soc_unbind_card(card, true);
2794 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2798 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2801 * Simplify DAI link configuration by removing ".-1" from device names
2802 * and sanitizing names.
2804 static char *fmt_single_name(struct device *dev, int *id)
2806 char *found, name[NAME_SIZE];
2809 if (dev_name(dev) == NULL)
2812 strlcpy(name, dev_name(dev), NAME_SIZE);
2814 /* are we a "%s.%d" name (platform and SPI components) */
2815 found = strstr(name, dev->driver->name);
2818 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2820 /* discard ID from name if ID == -1 */
2822 found[strlen(dev->driver->name)] = '\0';
2826 /* I2C component devices are named "bus-addr" */
2827 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2828 char tmp[NAME_SIZE];
2830 /* create unique ID number from I2C addr and bus */
2831 *id = ((id1 & 0xffff) << 16) + id2;
2833 /* sanitize component name for DAI link creation */
2834 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2836 strlcpy(name, tmp, NAME_SIZE);
2841 return kstrdup(name, GFP_KERNEL);
2845 * Simplify DAI link naming for single devices with multiple DAIs by removing
2846 * any ".-1" and using the DAI name (instead of device name).
2848 static inline char *fmt_multiple_name(struct device *dev,
2849 struct snd_soc_dai_driver *dai_drv)
2851 if (dai_drv->name == NULL) {
2853 "ASoC: error - multiple DAI %s registered with no name\n",
2858 return kstrdup(dai_drv->name, GFP_KERNEL);
2862 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2864 * @component: The component for which the DAIs should be unregistered
2866 static void snd_soc_unregister_dais(struct snd_soc_component *component)
2868 struct snd_soc_dai *dai, *_dai;
2870 for_each_component_dais_safe(component, dai, _dai) {
2871 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2873 list_del(&dai->list);
2879 /* Create a DAI and add it to the component's DAI list */
2880 static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2881 struct snd_soc_dai_driver *dai_drv,
2882 bool legacy_dai_naming)
2884 struct device *dev = component->dev;
2885 struct snd_soc_dai *dai;
2887 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2889 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2894 * Back in the old days when we still had component-less DAIs,
2895 * instead of having a static name, component-less DAIs would
2896 * inherit the name of the parent device so it is possible to
2897 * register multiple instances of the DAI. We still need to keep
2898 * the same naming style even though those DAIs are not
2899 * component-less anymore.
2901 if (legacy_dai_naming &&
2902 (dai_drv->id == 0 || dai_drv->name == NULL)) {
2903 dai->name = fmt_single_name(dev, &dai->id);
2905 dai->name = fmt_multiple_name(dev, dai_drv);
2907 dai->id = dai_drv->id;
2909 dai->id = component->num_dai;
2911 if (dai->name == NULL) {
2916 dai->component = component;
2918 dai->driver = dai_drv;
2919 if (!dai->driver->ops)
2920 dai->driver->ops = &null_dai_ops;
2922 /* see for_each_component_dais */
2923 list_add_tail(&dai->list, &component->dai_list);
2924 component->num_dai++;
2926 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2931 * snd_soc_register_dais - Register a DAI with the ASoC core
2933 * @component: The component the DAIs are registered for
2934 * @dai_drv: DAI driver to use for the DAIs
2935 * @count: Number of DAIs
2937 static int snd_soc_register_dais(struct snd_soc_component *component,
2938 struct snd_soc_dai_driver *dai_drv,
2941 struct device *dev = component->dev;
2942 struct snd_soc_dai *dai;
2946 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
2948 for (i = 0; i < count; i++) {
2950 dai = soc_add_dai(component, dai_drv + i, count == 1 &&
2951 !component->driver->non_legacy_dai_naming);
2961 snd_soc_unregister_dais(component);
2967 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2969 * @component: The component the DAIs are registered for
2970 * @dai_drv: DAI driver to use for the DAI
2972 * Topology can use this API to register DAIs when probing a component.
2973 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2974 * will be freed in the component cleanup.
2976 int snd_soc_register_dai(struct snd_soc_component *component,
2977 struct snd_soc_dai_driver *dai_drv)
2979 struct snd_soc_dapm_context *dapm =
2980 snd_soc_component_get_dapm(component);
2981 struct snd_soc_dai *dai;
2984 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
2985 dev_err(component->dev, "Invalid dai type %d\n",
2986 dai_drv->dobj.type);
2990 lockdep_assert_held(&client_mutex);
2991 dai = soc_add_dai(component, dai_drv, false);
2996 * Create the DAI widgets here. After adding DAIs, topology may
2997 * also add routes that need these widgets as source or sink.
2999 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
3001 dev_err(component->dev,
3002 "Failed to create DAI widgets %d\n", ret);
3007 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3009 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3010 enum snd_soc_dapm_type type, int subseq)
3012 struct snd_soc_component *component = dapm->component;
3014 component->driver->seq_notifier(component, type, subseq);
3017 static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3020 struct snd_soc_component *component = dapm->component;
3022 return component->driver->stream_event(component, event);
3025 static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
3026 enum snd_soc_bias_level level)
3028 struct snd_soc_component *component = dapm->component;
3030 return component->driver->set_bias_level(component, level);
3033 static int snd_soc_component_initialize(struct snd_soc_component *component,
3034 const struct snd_soc_component_driver *driver, struct device *dev)
3036 struct snd_soc_dapm_context *dapm;
3038 component->name = fmt_single_name(dev, &component->id);
3039 if (!component->name) {
3040 dev_err(dev, "ASoC: Failed to allocate name\n");
3044 component->dev = dev;
3045 component->driver = driver;
3047 dapm = snd_soc_component_get_dapm(component);
3049 dapm->component = component;
3050 dapm->bias_level = SND_SOC_BIAS_OFF;
3051 dapm->idle_bias_off = !driver->idle_bias_on;
3052 dapm->suspend_bias_off = driver->suspend_bias_off;
3053 if (driver->seq_notifier)
3054 dapm->seq_notifier = snd_soc_component_seq_notifier;
3055 if (driver->stream_event)
3056 dapm->stream_event = snd_soc_component_stream_event;
3057 if (driver->set_bias_level)
3058 dapm->set_bias_level = snd_soc_component_set_bias_level;
3060 INIT_LIST_HEAD(&component->dai_list);
3061 mutex_init(&component->io_mutex);
3066 static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
3068 int val_bytes = regmap_get_val_bytes(component->regmap);
3070 /* Errors are legitimate for non-integer byte multiples */
3072 component->val_bytes = val_bytes;
3075 #ifdef CONFIG_REGMAP
3078 * snd_soc_component_init_regmap() - Initialize regmap instance for the
3080 * @component: The component for which to initialize the regmap instance
3081 * @regmap: The regmap instance that should be used by the component
3083 * This function allows deferred assignment of the regmap instance that is
3084 * associated with the component. Only use this if the regmap instance is not
3085 * yet ready when the component is registered. The function must also be called
3086 * before the first IO attempt of the component.
3088 void snd_soc_component_init_regmap(struct snd_soc_component *component,
3089 struct regmap *regmap)
3091 component->regmap = regmap;
3092 snd_soc_component_setup_regmap(component);
3094 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3097 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
3099 * @component: The component for which to de-initialize the regmap instance
3101 * Calls regmap_exit() on the regmap instance associated to the component and
3102 * removes the regmap instance from the component.
3104 * This function should only be used if snd_soc_component_init_regmap() was used
3105 * to initialize the regmap instance.
3107 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3109 regmap_exit(component->regmap);
3110 component->regmap = NULL;
3112 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3116 static void snd_soc_component_add(struct snd_soc_component *component)
3118 mutex_lock(&client_mutex);
3120 if (!component->driver->write && !component->driver->read) {
3121 if (!component->regmap)
3122 component->regmap = dev_get_regmap(component->dev,
3124 if (component->regmap)
3125 snd_soc_component_setup_regmap(component);
3128 /* see for_each_component */
3129 list_add(&component->list, &component_list);
3130 INIT_LIST_HEAD(&component->dobj_list);
3132 mutex_unlock(&client_mutex);
3135 static void snd_soc_component_cleanup(struct snd_soc_component *component)
3137 snd_soc_unregister_dais(component);
3138 kfree(component->name);
3141 static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3143 struct snd_soc_card *card = component->card;
3146 snd_soc_unbind_card(card, false);
3148 list_del(&component->list);
3151 #define ENDIANNESS_MAP(name) \
3152 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3153 static u64 endianness_format_map[] = {
3154 ENDIANNESS_MAP(S16_),
3155 ENDIANNESS_MAP(U16_),
3156 ENDIANNESS_MAP(S24_),
3157 ENDIANNESS_MAP(U24_),
3158 ENDIANNESS_MAP(S32_),
3159 ENDIANNESS_MAP(U32_),
3160 ENDIANNESS_MAP(S24_3),
3161 ENDIANNESS_MAP(U24_3),
3162 ENDIANNESS_MAP(S20_3),
3163 ENDIANNESS_MAP(U20_3),
3164 ENDIANNESS_MAP(S18_3),
3165 ENDIANNESS_MAP(U18_3),
3166 ENDIANNESS_MAP(FLOAT_),
3167 ENDIANNESS_MAP(FLOAT64_),
3168 ENDIANNESS_MAP(IEC958_SUBFRAME_),
3172 * Fix up the DAI formats for endianness: codecs don't actually see
3173 * the endianness of the data but we're using the CPU format
3174 * definitions which do need to include endianness so we ensure that
3175 * codec DAIs always have both big and little endian variants set.
3177 static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3181 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3182 if (stream->formats & endianness_format_map[i])
3183 stream->formats |= endianness_format_map[i];
3186 static void snd_soc_try_rebind_card(void)
3188 struct snd_soc_card *card, *c;
3190 if (!list_empty(&unbind_card_list)) {
3191 list_for_each_entry_safe(card, c, &unbind_card_list, list) {
3192 if (!snd_soc_bind_card(card))
3193 list_del(&card->list);
3198 int snd_soc_add_component(struct device *dev,
3199 struct snd_soc_component *component,
3200 const struct snd_soc_component_driver *component_driver,
3201 struct snd_soc_dai_driver *dai_drv,
3207 ret = snd_soc_component_initialize(component, component_driver, dev);
3211 if (component_driver->endianness) {
3212 for (i = 0; i < num_dai; i++) {
3213 convert_endianness_formats(&dai_drv[i].playback);
3214 convert_endianness_formats(&dai_drv[i].capture);
3218 ret = snd_soc_register_dais(component, dai_drv, num_dai);
3220 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3224 snd_soc_component_add(component);
3225 snd_soc_try_rebind_card();
3230 snd_soc_component_cleanup(component);
3234 EXPORT_SYMBOL_GPL(snd_soc_add_component);
3236 int snd_soc_register_component(struct device *dev,
3237 const struct snd_soc_component_driver *component_driver,
3238 struct snd_soc_dai_driver *dai_drv,
3241 struct snd_soc_component *component;
3243 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
3247 return snd_soc_add_component(dev, component, component_driver,
3250 EXPORT_SYMBOL_GPL(snd_soc_register_component);
3253 * snd_soc_unregister_component - Unregister all related component
3254 * from the ASoC core
3256 * @dev: The device to unregister
3258 static int __snd_soc_unregister_component(struct device *dev)
3260 struct snd_soc_component *component;
3263 mutex_lock(&client_mutex);
3264 for_each_component(component) {
3265 if (dev != component->dev)
3268 snd_soc_tplg_component_remove(component,
3269 SND_SOC_TPLG_INDEX_ALL);
3270 snd_soc_component_del_unlocked(component);
3274 mutex_unlock(&client_mutex);
3277 snd_soc_component_cleanup(component);
3282 void snd_soc_unregister_component(struct device *dev)
3284 while (__snd_soc_unregister_component(dev))
3287 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3289 struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
3290 const char *driver_name)
3292 struct snd_soc_component *component;
3293 struct snd_soc_component *ret;
3296 mutex_lock(&client_mutex);
3297 for_each_component(component) {
3298 if (dev != component->dev)
3302 (driver_name != component->driver->name) &&
3303 (strcmp(component->driver->name, driver_name) != 0))
3309 mutex_unlock(&client_mutex);
3313 EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
3315 /* Retrieve a card's name from device tree */
3316 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3317 const char *propname)
3319 struct device_node *np;
3323 pr_err("card->dev is not set before calling %s\n", __func__);
3327 np = card->dev->of_node;
3329 ret = of_property_read_string_index(np, propname, 0, &card->name);
3331 * EINVAL means the property does not exist. This is fine providing
3332 * card->name was previously set, which is checked later in
3333 * snd_soc_register_card.
3335 if (ret < 0 && ret != -EINVAL) {
3337 "ASoC: Property '%s' could not be read: %d\n",
3344 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3346 static const struct snd_soc_dapm_widget simple_widgets[] = {
3347 SND_SOC_DAPM_MIC("Microphone", NULL),
3348 SND_SOC_DAPM_LINE("Line", NULL),
3349 SND_SOC_DAPM_HP("Headphone", NULL),
3350 SND_SOC_DAPM_SPK("Speaker", NULL),
3353 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
3354 const char *propname)
3356 struct device_node *np = card->dev->of_node;
3357 struct snd_soc_dapm_widget *widgets;
3358 const char *template, *wname;
3359 int i, j, num_widgets, ret;
3361 num_widgets = of_property_count_strings(np, propname);
3362 if (num_widgets < 0) {
3364 "ASoC: Property '%s' does not exist\n", propname);
3367 if (num_widgets & 1) {
3369 "ASoC: Property '%s' length is not even\n", propname);
3375 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3380 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3384 "ASoC: Could not allocate memory for widgets\n");
3388 for (i = 0; i < num_widgets; i++) {
3389 ret = of_property_read_string_index(np, propname,
3393 "ASoC: Property '%s' index %d read error:%d\n",
3394 propname, 2 * i, ret);
3398 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3399 if (!strncmp(template, simple_widgets[j].name,
3400 strlen(simple_widgets[j].name))) {
3401 widgets[i] = simple_widgets[j];
3406 if (j >= ARRAY_SIZE(simple_widgets)) {
3408 "ASoC: DAPM widget '%s' is not supported\n",
3413 ret = of_property_read_string_index(np, propname,
3418 "ASoC: Property '%s' index %d read error:%d\n",
3419 propname, (2 * i) + 1, ret);
3423 widgets[i].name = wname;
3426 card->of_dapm_widgets = widgets;
3427 card->num_of_dapm_widgets = num_widgets;
3431 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
3433 int snd_soc_of_get_slot_mask(struct device_node *np,
3434 const char *prop_name,
3438 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
3444 for (i = 0; i < val; i++)
3445 if (be32_to_cpup(&of_slot_mask[i]))
3450 EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
3452 int snd_soc_of_parse_tdm_slot(struct device_node *np,
3453 unsigned int *tx_mask,
3454 unsigned int *rx_mask,
3455 unsigned int *slots,
3456 unsigned int *slot_width)
3462 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3464 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3466 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3467 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3475 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3476 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3486 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3488 void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
3489 struct snd_soc_codec_conf *codec_conf,
3490 struct device_node *of_node,
3491 const char *propname)
3493 struct device_node *np = card->dev->of_node;
3497 ret = of_property_read_string(np, propname, &str);
3499 /* no prefix is not error */
3503 codec_conf->of_node = of_node;
3504 codec_conf->name_prefix = str;
3506 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
3508 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3509 const char *propname)
3511 struct device_node *np = card->dev->of_node;
3513 struct snd_soc_dapm_route *routes;
3516 num_routes = of_property_count_strings(np, propname);
3517 if (num_routes < 0 || num_routes & 1) {
3519 "ASoC: Property '%s' does not exist or its length is not even\n",
3525 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3530 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
3534 "ASoC: Could not allocate DAPM route table\n");
3538 for (i = 0; i < num_routes; i++) {
3539 ret = of_property_read_string_index(np, propname,
3540 2 * i, &routes[i].sink);
3543 "ASoC: Property '%s' index %d could not be read: %d\n",
3544 propname, 2 * i, ret);
3547 ret = of_property_read_string_index(np, propname,
3548 (2 * i) + 1, &routes[i].source);
3551 "ASoC: Property '%s' index %d could not be read: %d\n",
3552 propname, (2 * i) + 1, ret);
3557 card->num_of_dapm_routes = num_routes;
3558 card->of_dapm_routes = routes;
3562 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3564 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3566 struct device_node **bitclkmaster,
3567 struct device_node **framemaster)
3571 unsigned int format = 0;
3577 } of_fmt_table[] = {
3578 { "i2s", SND_SOC_DAIFMT_I2S },
3579 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3580 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3581 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3582 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3583 { "ac97", SND_SOC_DAIFMT_AC97 },
3584 { "pdm", SND_SOC_DAIFMT_PDM},
3585 { "msb", SND_SOC_DAIFMT_MSB },
3586 { "lsb", SND_SOC_DAIFMT_LSB },
3593 * check "dai-format = xxx"
3594 * or "[prefix]format = xxx"
3595 * SND_SOC_DAIFMT_FORMAT_MASK area
3597 ret = of_property_read_string(np, "dai-format", &str);
3599 snprintf(prop, sizeof(prop), "%sformat", prefix);
3600 ret = of_property_read_string(np, prop, &str);
3603 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3604 if (strcmp(str, of_fmt_table[i].name) == 0) {
3605 format |= of_fmt_table[i].val;
3612 * check "[prefix]continuous-clock"
3613 * SND_SOC_DAIFMT_CLOCK_MASK area
3615 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
3616 if (of_property_read_bool(np, prop))
3617 format |= SND_SOC_DAIFMT_CONT;
3619 format |= SND_SOC_DAIFMT_GATED;
3622 * check "[prefix]bitclock-inversion"
3623 * check "[prefix]frame-inversion"
3624 * SND_SOC_DAIFMT_INV_MASK area
3626 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3627 bit = !!of_get_property(np, prop, NULL);
3629 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3630 frame = !!of_get_property(np, prop, NULL);
3632 switch ((bit << 4) + frame) {
3634 format |= SND_SOC_DAIFMT_IB_IF;
3637 format |= SND_SOC_DAIFMT_IB_NF;
3640 format |= SND_SOC_DAIFMT_NB_IF;
3643 /* SND_SOC_DAIFMT_NB_NF is default */
3648 * check "[prefix]bitclock-master"
3649 * check "[prefix]frame-master"
3650 * SND_SOC_DAIFMT_MASTER_MASK area
3652 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3653 bit = !!of_get_property(np, prop, NULL);
3654 if (bit && bitclkmaster)
3655 *bitclkmaster = of_parse_phandle(np, prop, 0);
3657 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3658 frame = !!of_get_property(np, prop, NULL);
3659 if (frame && framemaster)
3660 *framemaster = of_parse_phandle(np, prop, 0);
3662 switch ((bit << 4) + frame) {
3664 format |= SND_SOC_DAIFMT_CBM_CFM;
3667 format |= SND_SOC_DAIFMT_CBM_CFS;
3670 format |= SND_SOC_DAIFMT_CBS_CFM;
3673 format |= SND_SOC_DAIFMT_CBS_CFS;
3679 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3681 int snd_soc_get_dai_id(struct device_node *ep)
3683 struct snd_soc_component *pos;
3684 struct device_node *node;
3687 node = of_graph_get_port_parent(ep);
3690 * For example HDMI case, HDMI has video/sound port,
3691 * but ALSA SoC needs sound port number only.
3692 * Thus counting HDMI DT port/endpoint doesn't work.
3693 * Then, it should have .of_xlate_dai_id
3696 mutex_lock(&client_mutex);
3697 for_each_component(pos) {
3698 struct device_node *component_of_node = pos->dev->of_node;
3700 if (!component_of_node && pos->dev->parent)
3701 component_of_node = pos->dev->parent->of_node;
3703 if (component_of_node != node)
3706 if (pos->driver->of_xlate_dai_id)
3707 ret = pos->driver->of_xlate_dai_id(pos, ep);
3711 mutex_unlock(&client_mutex);
3717 EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3719 int snd_soc_get_dai_name(struct of_phandle_args *args,
3720 const char **dai_name)
3722 struct snd_soc_component *pos;
3723 struct device_node *component_of_node;
3724 int ret = -EPROBE_DEFER;
3726 mutex_lock(&client_mutex);
3727 for_each_component(pos) {
3728 component_of_node = pos->dev->of_node;
3729 if (!component_of_node && pos->dev->parent)
3730 component_of_node = pos->dev->parent->of_node;
3732 if (component_of_node != args->np)
3735 if (pos->driver->of_xlate_dai_name) {
3736 ret = pos->driver->of_xlate_dai_name(pos,
3740 struct snd_soc_dai *dai;
3743 switch (args->args_count) {
3745 id = 0; /* same as dai_drv[0] */
3755 if (id < 0 || id >= pos->num_dai) {
3762 /* find target DAI */
3763 for_each_component_dais(pos, dai) {
3769 *dai_name = dai->driver->name;
3771 *dai_name = pos->name;
3776 mutex_unlock(&client_mutex);
3779 EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
3781 int snd_soc_of_get_dai_name(struct device_node *of_node,
3782 const char **dai_name)
3784 struct of_phandle_args args;
3787 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3788 "#sound-dai-cells", 0, &args);
3792 ret = snd_soc_get_dai_name(&args, dai_name);
3794 of_node_put(args.np);
3798 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3801 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3802 * @dai_link: DAI link
3804 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3806 void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3808 struct snd_soc_dai_link_component *component;
3811 for_each_link_codecs(dai_link, index, component) {
3812 if (!component->of_node)
3814 of_node_put(component->of_node);
3815 component->of_node = NULL;
3818 EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3821 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3823 * @of_node: Device node
3824 * @dai_link: DAI link
3826 * Builds an array of CODEC DAI components from the DAI link property
3828 * The array is set in the DAI link and the number of DAIs is set accordingly.
3829 * The device nodes in the array (of_node) must be dereferenced by calling
3830 * snd_soc_of_put_dai_link_codecs() on @dai_link.
3832 * Returns 0 for success
3834 int snd_soc_of_get_dai_link_codecs(struct device *dev,
3835 struct device_node *of_node,
3836 struct snd_soc_dai_link *dai_link)
3838 struct of_phandle_args args;
3839 struct snd_soc_dai_link_component *component;
3841 int index, num_codecs, ret;
3843 /* Count the number of CODECs */
3845 num_codecs = of_count_phandle_with_args(of_node, name,
3846 "#sound-dai-cells");
3847 if (num_codecs <= 0) {
3848 if (num_codecs == -ENOENT)
3849 dev_err(dev, "No 'sound-dai' property\n");
3851 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3854 component = devm_kcalloc(dev,
3855 num_codecs, sizeof(*component),
3859 dai_link->codecs = component;
3860 dai_link->num_codecs = num_codecs;
3862 /* Parse the list */
3863 for_each_link_codecs(dai_link, index, component) {
3864 ret = of_parse_phandle_with_args(of_node, name,
3869 component->of_node = args.np;
3870 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3876 snd_soc_of_put_dai_link_codecs(dai_link);
3877 dai_link->codecs = NULL;
3878 dai_link->num_codecs = 0;
3881 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3883 static int __init snd_soc_init(void)
3885 snd_soc_debugfs_init();
3886 snd_soc_util_init();
3888 return platform_driver_register(&soc_driver);
3890 module_init(snd_soc_init);
3892 static void __exit snd_soc_exit(void)
3894 snd_soc_util_exit();
3895 snd_soc_debugfs_exit();
3897 platform_driver_unregister(&soc_driver);
3899 module_exit(snd_soc_exit);
3901 /* Module information */
3903 MODULE_DESCRIPTION("ALSA SoC Core");
3904 MODULE_LICENSE("GPL");
3905 MODULE_ALIAS("platform:soc-audio");