2 * soc-core.c -- ALSA SoC Audio Layer
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
30 #include <linux/bitops.h>
31 #include <linux/debugfs.h>
32 #include <linux/platform_device.h>
33 #include <linux/pinctrl/consumer.h>
34 #include <linux/ctype.h>
35 #include <linux/slab.h>
37 #include <linux/of_graph.h>
38 #include <linux/dmi.h>
39 #include <sound/core.h>
40 #include <sound/jack.h>
41 #include <sound/pcm.h>
42 #include <sound/pcm_params.h>
43 #include <sound/soc.h>
44 #include <sound/soc-dpcm.h>
45 #include <sound/soc-topology.h>
46 #include <sound/initval.h>
48 #define CREATE_TRACE_POINTS
49 #include <trace/events/asoc.h>
53 #ifdef CONFIG_DEBUG_FS
54 struct dentry *snd_soc_debugfs_root;
55 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
58 static DEFINE_MUTEX(client_mutex);
59 static LIST_HEAD(codec_list);
60 static LIST_HEAD(component_list);
63 * This is a timeout to do a DAPM powerdown after a stream is closed().
64 * It can be used to eliminate pops between different playback streams, e.g.
65 * between two audio tracks.
67 static int pmdown_time = 5000;
68 module_param(pmdown_time, int, 0);
69 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
71 /* If a DMI filed contain strings in this blacklist (e.g.
72 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
73 * as invalid and dropped when setting the card long name from DMI info.
75 static const char * const dmi_blacklist[] = {
76 "To be filled by OEM",
82 NULL, /* terminator */
85 static ssize_t pmdown_time_show(struct device *dev,
86 struct device_attribute *attr, char *buf)
88 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
90 return sprintf(buf, "%ld\n", rtd->pmdown_time);
93 static ssize_t pmdown_time_set(struct device *dev,
94 struct device_attribute *attr,
95 const char *buf, size_t count)
97 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
100 ret = kstrtol(buf, 10, &rtd->pmdown_time);
107 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
109 static struct attribute *soc_dev_attrs[] = {
110 &dev_attr_pmdown_time.attr,
114 static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
115 struct attribute *attr, int idx)
117 struct device *dev = kobj_to_dev(kobj);
118 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
120 if (attr == &dev_attr_pmdown_time.attr)
121 return attr->mode; /* always visible */
122 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
125 static const struct attribute_group soc_dapm_dev_group = {
126 .attrs = soc_dapm_dev_attrs,
127 .is_visible = soc_dev_attr_is_visible,
130 static const struct attribute_group soc_dev_group = {
131 .attrs = soc_dev_attrs,
132 .is_visible = soc_dev_attr_is_visible,
135 static const struct attribute_group *soc_dev_attr_groups[] = {
141 #ifdef CONFIG_DEBUG_FS
142 static void soc_init_component_debugfs(struct snd_soc_component *component)
144 if (!component->card->debugfs_card_root)
147 if (component->debugfs_prefix) {
150 name = kasprintf(GFP_KERNEL, "%s:%s",
151 component->debugfs_prefix, component->name);
153 component->debugfs_root = debugfs_create_dir(name,
154 component->card->debugfs_card_root);
158 component->debugfs_root = debugfs_create_dir(component->name,
159 component->card->debugfs_card_root);
162 if (!component->debugfs_root) {
163 dev_warn(component->dev,
164 "ASoC: Failed to create component debugfs directory\n");
168 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
169 component->debugfs_root);
172 static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
174 debugfs_remove_recursive(component->debugfs_root);
177 static int codec_list_show(struct seq_file *m, void *v)
179 struct snd_soc_codec *codec;
181 mutex_lock(&client_mutex);
183 list_for_each_entry(codec, &codec_list, list)
184 seq_printf(m, "%s\n", codec->component.name);
186 mutex_unlock(&client_mutex);
190 DEFINE_SHOW_ATTRIBUTE(codec_list);
192 static int dai_list_show(struct seq_file *m, void *v)
194 struct snd_soc_component *component;
195 struct snd_soc_dai *dai;
197 mutex_lock(&client_mutex);
199 list_for_each_entry(component, &component_list, list)
200 list_for_each_entry(dai, &component->dai_list, list)
201 seq_printf(m, "%s\n", dai->name);
203 mutex_unlock(&client_mutex);
207 DEFINE_SHOW_ATTRIBUTE(dai_list);
209 static void soc_init_card_debugfs(struct snd_soc_card *card)
211 if (!snd_soc_debugfs_root)
214 card->debugfs_card_root = debugfs_create_dir(card->name,
215 snd_soc_debugfs_root);
216 if (!card->debugfs_card_root) {
218 "ASoC: Failed to create card debugfs directory\n");
222 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
223 card->debugfs_card_root,
225 if (!card->debugfs_pop_time)
227 "ASoC: Failed to create pop time debugfs file\n");
230 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
232 debugfs_remove_recursive(card->debugfs_card_root);
235 static void snd_soc_debugfs_init(void)
237 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
238 if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) {
239 pr_warn("ASoC: Failed to create debugfs directory\n");
240 snd_soc_debugfs_root = NULL;
244 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
246 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
248 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
250 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
253 static void snd_soc_debugfs_exit(void)
255 debugfs_remove_recursive(snd_soc_debugfs_root);
260 static inline void soc_init_component_debugfs(
261 struct snd_soc_component *component)
265 static inline void soc_cleanup_component_debugfs(
266 struct snd_soc_component *component)
270 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
274 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
278 static inline void snd_soc_debugfs_init(void)
282 static inline void snd_soc_debugfs_exit(void)
288 static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
289 struct snd_soc_component *component)
291 struct snd_soc_rtdcom_list *rtdcom;
292 struct snd_soc_rtdcom_list *new_rtdcom;
294 for_each_rtdcom(rtd, rtdcom) {
295 /* already connected */
296 if (rtdcom->component == component)
300 new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
304 new_rtdcom->component = component;
305 INIT_LIST_HEAD(&new_rtdcom->list);
307 list_add_tail(&new_rtdcom->list, &rtd->component_list);
312 static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
314 struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
316 for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
319 INIT_LIST_HEAD(&rtd->component_list);
322 struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
323 const char *driver_name)
325 struct snd_soc_rtdcom_list *rtdcom;
330 for_each_rtdcom(rtd, rtdcom) {
331 const char *component_name = rtdcom->component->driver->name;
336 if ((component_name == driver_name) ||
337 strcmp(component_name, driver_name) == 0)
338 return rtdcom->component;
343 EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
345 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
346 const char *dai_link, int stream)
348 struct snd_soc_pcm_runtime *rtd;
350 list_for_each_entry(rtd, &card->rtd_list, list) {
351 if (rtd->dai_link->no_pcm &&
352 !strcmp(rtd->dai_link->name, dai_link))
353 return rtd->pcm->streams[stream].substream;
355 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
358 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
360 static const struct snd_soc_ops null_snd_soc_ops;
362 static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
363 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
365 struct snd_soc_pcm_runtime *rtd;
367 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
371 INIT_LIST_HEAD(&rtd->component_list);
373 rtd->dai_link = dai_link;
374 if (!rtd->dai_link->ops)
375 rtd->dai_link->ops = &null_snd_soc_ops;
377 rtd->codec_dais = kzalloc(sizeof(struct snd_soc_dai *) *
378 dai_link->num_codecs,
380 if (!rtd->codec_dais) {
388 static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
390 kfree(rtd->codec_dais);
391 snd_soc_rtdcom_del_all(rtd);
395 static void soc_add_pcm_runtime(struct snd_soc_card *card,
396 struct snd_soc_pcm_runtime *rtd)
398 list_add_tail(&rtd->list, &card->rtd_list);
399 rtd->num = card->num_rtd;
403 static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
405 struct snd_soc_pcm_runtime *rtd, *_rtd;
407 list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) {
408 list_del(&rtd->list);
409 soc_free_pcm_runtime(rtd);
415 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
416 const char *dai_link)
418 struct snd_soc_pcm_runtime *rtd;
420 list_for_each_entry(rtd, &card->rtd_list, list) {
421 if (!strcmp(rtd->dai_link->name, dai_link))
424 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
427 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
429 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)
451 /* Due to the resume being scheduled into a workqueue we could
452 * suspend before that's finished - wait for it to complete.
454 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
456 /* we're going to block userspace touching us until resume completes */
457 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
459 /* mute any active DACs */
460 list_for_each_entry(rtd, &card->rtd_list, list) {
462 if (rtd->dai_link->ignore_suspend)
465 for (i = 0; i < rtd->num_codecs; i++) {
466 struct snd_soc_dai *dai = rtd->codec_dais[i];
467 struct snd_soc_dai_driver *drv = dai->driver;
469 if (drv->ops->digital_mute && dai->playback_active)
470 drv->ops->digital_mute(dai, 1);
474 /* suspend all pcms */
475 list_for_each_entry(rtd, &card->rtd_list, list) {
476 if (rtd->dai_link->ignore_suspend)
479 snd_pcm_suspend_all(rtd->pcm);
482 if (card->suspend_pre)
483 card->suspend_pre(card);
485 list_for_each_entry(rtd, &card->rtd_list, list) {
486 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
488 if (rtd->dai_link->ignore_suspend)
491 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
492 cpu_dai->driver->suspend(cpu_dai);
495 /* close any waiting streams */
496 list_for_each_entry(rtd, &card->rtd_list, list)
497 flush_delayed_work(&rtd->delayed_work);
499 list_for_each_entry(rtd, &card->rtd_list, list) {
501 if (rtd->dai_link->ignore_suspend)
504 snd_soc_dapm_stream_event(rtd,
505 SNDRV_PCM_STREAM_PLAYBACK,
506 SND_SOC_DAPM_STREAM_SUSPEND);
508 snd_soc_dapm_stream_event(rtd,
509 SNDRV_PCM_STREAM_CAPTURE,
510 SND_SOC_DAPM_STREAM_SUSPEND);
513 /* Recheck all endpoints too, their state is affected by suspend */
514 dapm_mark_endpoints_dirty(card);
515 snd_soc_dapm_sync(&card->dapm);
517 /* suspend all COMPONENTs */
518 list_for_each_entry(component, &card->component_dev_list, card_list) {
519 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
521 /* If there are paths active then the COMPONENT will be held with
522 * bias _ON and should not be suspended. */
523 if (!component->suspended) {
524 switch (snd_soc_dapm_get_bias_level(dapm)) {
525 case SND_SOC_BIAS_STANDBY:
527 * If the COMPONENT is capable of idle
528 * bias off then being in STANDBY
529 * means it's doing something,
530 * otherwise fall through.
532 if (dapm->idle_bias_off) {
533 dev_dbg(component->dev,
534 "ASoC: idle_bias_off CODEC on over suspend\n");
538 case SND_SOC_BIAS_OFF:
539 if (component->suspend)
540 component->suspend(component);
541 component->suspended = 1;
542 if (component->regmap)
543 regcache_mark_dirty(component->regmap);
544 /* deactivate pins to sleep state */
545 pinctrl_pm_select_sleep_state(component->dev);
548 dev_dbg(component->dev,
549 "ASoC: COMPONENT is on over suspend\n");
555 list_for_each_entry(rtd, &card->rtd_list, list) {
556 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
558 if (rtd->dai_link->ignore_suspend)
561 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
562 cpu_dai->driver->suspend(cpu_dai);
564 /* deactivate pins to sleep state */
565 pinctrl_pm_select_sleep_state(cpu_dai->dev);
568 if (card->suspend_post)
569 card->suspend_post(card);
573 EXPORT_SYMBOL_GPL(snd_soc_suspend);
575 /* deferred resume work, so resume can complete before we finished
576 * setting our codec back up, which can be very slow on I2C
578 static void soc_resume_deferred(struct work_struct *work)
580 struct snd_soc_card *card =
581 container_of(work, struct snd_soc_card, deferred_resume_work);
582 struct snd_soc_pcm_runtime *rtd;
583 struct snd_soc_component *component;
586 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
587 * so userspace apps are blocked from touching us
590 dev_dbg(card->dev, "ASoC: starting resume work\n");
592 /* Bring us up into D2 so that DAPM starts enabling things */
593 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
595 if (card->resume_pre)
596 card->resume_pre(card);
598 /* resume control bus DAIs */
599 list_for_each_entry(rtd, &card->rtd_list, list) {
600 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
602 if (rtd->dai_link->ignore_suspend)
605 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
606 cpu_dai->driver->resume(cpu_dai);
609 list_for_each_entry(component, &card->component_dev_list, card_list) {
610 if (component->suspended) {
611 if (component->resume)
612 component->resume(component);
613 component->suspended = 0;
617 list_for_each_entry(rtd, &card->rtd_list, list) {
619 if (rtd->dai_link->ignore_suspend)
622 snd_soc_dapm_stream_event(rtd,
623 SNDRV_PCM_STREAM_PLAYBACK,
624 SND_SOC_DAPM_STREAM_RESUME);
626 snd_soc_dapm_stream_event(rtd,
627 SNDRV_PCM_STREAM_CAPTURE,
628 SND_SOC_DAPM_STREAM_RESUME);
631 /* unmute any active DACs */
632 list_for_each_entry(rtd, &card->rtd_list, list) {
634 if (rtd->dai_link->ignore_suspend)
637 for (i = 0; i < rtd->num_codecs; i++) {
638 struct snd_soc_dai *dai = rtd->codec_dais[i];
639 struct snd_soc_dai_driver *drv = dai->driver;
641 if (drv->ops->digital_mute && dai->playback_active)
642 drv->ops->digital_mute(dai, 0);
646 list_for_each_entry(rtd, &card->rtd_list, list) {
647 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
649 if (rtd->dai_link->ignore_suspend)
652 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
653 cpu_dai->driver->resume(cpu_dai);
656 if (card->resume_post)
657 card->resume_post(card);
659 dev_dbg(card->dev, "ASoC: resume work completed\n");
661 /* Recheck all endpoints too, their state is affected by suspend */
662 dapm_mark_endpoints_dirty(card);
663 snd_soc_dapm_sync(&card->dapm);
665 /* userspace can access us now we are back as we were before */
666 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
669 /* powers up audio subsystem after a suspend */
670 int snd_soc_resume(struct device *dev)
672 struct snd_soc_card *card = dev_get_drvdata(dev);
673 bool bus_control = false;
674 struct snd_soc_pcm_runtime *rtd;
676 /* If the card is not initialized yet there is nothing to do */
677 if (!card->instantiated)
680 /* activate pins from sleep state */
681 list_for_each_entry(rtd, &card->rtd_list, list) {
682 struct snd_soc_dai **codec_dais = rtd->codec_dais;
683 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
687 pinctrl_pm_select_default_state(cpu_dai->dev);
689 for (j = 0; j < rtd->num_codecs; j++) {
690 struct snd_soc_dai *codec_dai = codec_dais[j];
691 if (codec_dai->active)
692 pinctrl_pm_select_default_state(codec_dai->dev);
697 * DAIs that also act as the control bus master might have other drivers
698 * hanging off them so need to resume immediately. Other drivers don't
699 * have that problem and may take a substantial amount of time to resume
700 * due to I/O costs and anti-pop so handle them out of line.
702 list_for_each_entry(rtd, &card->rtd_list, list) {
703 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
704 bus_control |= cpu_dai->driver->bus_control;
707 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
708 soc_resume_deferred(&card->deferred_resume_work);
710 dev_dbg(dev, "ASoC: Scheduling resume work\n");
711 if (!schedule_work(&card->deferred_resume_work))
712 dev_err(dev, "ASoC: resume work item may be lost\n");
717 EXPORT_SYMBOL_GPL(snd_soc_resume);
719 #define snd_soc_suspend NULL
720 #define snd_soc_resume NULL
723 static const struct snd_soc_dai_ops null_dai_ops = {
726 static struct snd_soc_component *soc_find_component(
727 const struct device_node *of_node, const char *name)
729 struct snd_soc_component *component;
731 lockdep_assert_held(&client_mutex);
733 list_for_each_entry(component, &component_list, list) {
735 if (component->dev->of_node == of_node)
737 } else if (strcmp(component->name, name) == 0) {
746 * snd_soc_find_dai - Find a registered DAI
748 * @dlc: name of the DAI or the DAI driver and optional component info to match
750 * This function will search all registered components and their DAIs to
751 * find the DAI of the same name. The component's of_node and name
752 * should also match if being specified.
754 * Return: pointer of DAI, or NULL if not found.
756 struct snd_soc_dai *snd_soc_find_dai(
757 const struct snd_soc_dai_link_component *dlc)
759 struct snd_soc_component *component;
760 struct snd_soc_dai *dai;
761 struct device_node *component_of_node;
763 lockdep_assert_held(&client_mutex);
765 /* Find CPU DAI from registered DAIs*/
766 list_for_each_entry(component, &component_list, list) {
767 component_of_node = component->dev->of_node;
768 if (!component_of_node && component->dev->parent)
769 component_of_node = component->dev->parent->of_node;
771 if (dlc->of_node && component_of_node != dlc->of_node)
773 if (dlc->name && strcmp(component->name, dlc->name))
775 list_for_each_entry(dai, &component->dai_list, list) {
776 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
777 && (!dai->driver->name
778 || strcmp(dai->driver->name, dlc->dai_name)))
787 EXPORT_SYMBOL_GPL(snd_soc_find_dai);
791 * snd_soc_find_dai_link - Find a DAI link
794 * @id: DAI link ID to match
795 * @name: DAI link name to match, optional
796 * @stream_name: DAI link stream name to match, optional
798 * This function will search all existing DAI links of the soc card to
799 * find the link of the same ID. Since DAI links may not have their
800 * unique ID, so name and stream name should also match if being
803 * Return: pointer of DAI link, or NULL if not found.
805 struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
806 int id, const char *name,
807 const char *stream_name)
809 struct snd_soc_dai_link *link, *_link;
811 lockdep_assert_held(&client_mutex);
813 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
817 if (name && (!link->name || strcmp(name, link->name)))
820 if (stream_name && (!link->stream_name
821 || strcmp(stream_name, link->stream_name)))
829 EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
831 static bool soc_is_dai_link_bound(struct snd_soc_card *card,
832 struct snd_soc_dai_link *dai_link)
834 struct snd_soc_pcm_runtime *rtd;
836 list_for_each_entry(rtd, &card->rtd_list, list) {
837 if (rtd->dai_link == dai_link)
844 static int soc_bind_dai_link(struct snd_soc_card *card,
845 struct snd_soc_dai_link *dai_link)
847 struct snd_soc_pcm_runtime *rtd;
848 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
849 struct snd_soc_dai_link_component cpu_dai_component;
850 struct snd_soc_component *component;
851 struct snd_soc_dai **codec_dais;
852 struct device_node *platform_of_node;
853 const char *platform_name;
856 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
858 if (soc_is_dai_link_bound(card, dai_link)) {
859 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
864 rtd = soc_new_pcm_runtime(card, dai_link);
868 cpu_dai_component.name = dai_link->cpu_name;
869 cpu_dai_component.of_node = dai_link->cpu_of_node;
870 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
871 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
873 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
874 dai_link->cpu_dai_name);
877 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
879 rtd->num_codecs = dai_link->num_codecs;
881 /* Find CODEC from registered CODECs */
882 codec_dais = rtd->codec_dais;
883 for (i = 0; i < rtd->num_codecs; i++) {
884 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
885 if (!codec_dais[i]) {
886 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
890 snd_soc_rtdcom_add(rtd, codec_dais[i]->component);
893 /* Single codec links expect codec and codec_dai in runtime data */
894 rtd->codec_dai = codec_dais[0];
895 rtd->codec = rtd->codec_dai->codec;
897 /* if there's no platform we match on the empty platform */
898 platform_name = dai_link->platform_name;
899 if (!platform_name && !dai_link->platform_of_node)
900 platform_name = "snd-soc-dummy";
902 /* find one from the set of registered platforms */
903 list_for_each_entry(component, &component_list, list) {
904 platform_of_node = component->dev->of_node;
905 if (!platform_of_node && component->dev->parent->of_node)
906 platform_of_node = component->dev->parent->of_node;
908 if (dai_link->platform_of_node) {
909 if (platform_of_node != dai_link->platform_of_node)
912 if (strcmp(component->name, platform_name))
916 snd_soc_rtdcom_add(rtd, component);
919 soc_add_pcm_runtime(card, rtd);
923 soc_free_pcm_runtime(rtd);
924 return -EPROBE_DEFER;
927 static void soc_remove_component(struct snd_soc_component *component)
929 if (!component->card)
932 list_del(&component->card_list);
934 if (component->remove)
935 component->remove(component);
937 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
939 soc_cleanup_component_debugfs(component);
940 component->card = NULL;
941 module_put(component->dev->driver->owner);
944 static void soc_remove_dai(struct snd_soc_dai *dai, int order)
948 if (dai && dai->probed &&
949 dai->driver->remove_order == order) {
950 if (dai->driver->remove) {
951 err = dai->driver->remove(dai);
954 "ASoC: failed to remove %s: %d\n",
961 static void soc_remove_link_dais(struct snd_soc_card *card,
962 struct snd_soc_pcm_runtime *rtd, int order)
966 /* unregister the rtd device */
967 if (rtd->dev_registered) {
968 device_unregister(rtd->dev);
969 rtd->dev_registered = 0;
972 /* remove the CODEC DAI */
973 for (i = 0; i < rtd->num_codecs; i++)
974 soc_remove_dai(rtd->codec_dais[i], order);
976 soc_remove_dai(rtd->cpu_dai, order);
979 static void soc_remove_link_components(struct snd_soc_card *card,
980 struct snd_soc_pcm_runtime *rtd, int order)
982 struct snd_soc_component *component;
983 struct snd_soc_rtdcom_list *rtdcom;
985 for_each_rtdcom(rtd, rtdcom) {
986 component = rtdcom->component;
988 if (component->driver->remove_order == order)
989 soc_remove_component(component);
993 static void soc_remove_dai_links(struct snd_soc_card *card)
996 struct snd_soc_pcm_runtime *rtd;
997 struct snd_soc_dai_link *link, *_link;
999 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1001 list_for_each_entry(rtd, &card->rtd_list, list)
1002 soc_remove_link_dais(card, rtd, order);
1005 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1007 list_for_each_entry(rtd, &card->rtd_list, list)
1008 soc_remove_link_components(card, rtd, order);
1011 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1012 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1013 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1016 list_del(&link->list);
1017 card->num_dai_links--;
1021 static int snd_soc_init_multicodec(struct snd_soc_card *card,
1022 struct snd_soc_dai_link *dai_link)
1024 /* Legacy codec/codec_dai link is a single entry in multicodec */
1025 if (dai_link->codec_name || dai_link->codec_of_node ||
1026 dai_link->codec_dai_name) {
1027 dai_link->num_codecs = 1;
1029 dai_link->codecs = devm_kzalloc(card->dev,
1030 sizeof(struct snd_soc_dai_link_component),
1032 if (!dai_link->codecs)
1035 dai_link->codecs[0].name = dai_link->codec_name;
1036 dai_link->codecs[0].of_node = dai_link->codec_of_node;
1037 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1040 if (!dai_link->codecs) {
1041 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1048 static int soc_init_dai_link(struct snd_soc_card *card,
1049 struct snd_soc_dai_link *link)
1053 ret = snd_soc_init_multicodec(card, link);
1055 dev_err(card->dev, "ASoC: failed to init multicodec\n");
1059 for (i = 0; i < link->num_codecs; i++) {
1061 * Codec must be specified by 1 of name or OF node,
1062 * not both or neither.
1064 if (!!link->codecs[i].name ==
1065 !!link->codecs[i].of_node) {
1066 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1070 /* Codec DAI name must be specified */
1071 if (!link->codecs[i].dai_name) {
1072 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1079 * Platform may be specified by either name or OF node, but
1080 * can be left unspecified, and a dummy platform will be used.
1082 if (link->platform_name && link->platform_of_node) {
1084 "ASoC: Both platform name/of_node are set for %s\n",
1090 * CPU device may be specified by either name or OF node, but
1091 * can be left unspecified, and will be matched based on DAI
1094 if (link->cpu_name && link->cpu_of_node) {
1096 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1101 * At least one of CPU DAI name or CPU device name/node must be
1104 if (!link->cpu_dai_name &&
1105 !(link->cpu_name || link->cpu_of_node)) {
1107 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1115 void snd_soc_disconnect_sync(struct device *dev)
1117 struct snd_soc_component *component = snd_soc_lookup_component(dev, NULL);
1119 if (!component || !component->card)
1122 snd_card_disconnect_sync(component->card->snd_card);
1124 EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
1127 * snd_soc_add_dai_link - Add a DAI link dynamically
1128 * @card: The ASoC card to which the DAI link is added
1129 * @dai_link: The new DAI link to add
1131 * This function adds a DAI link to the ASoC card's link list.
1133 * Note: Topology can use this API to add DAI links when probing the
1134 * topology component. And machine drivers can still define static
1135 * DAI links in dai_link array.
1137 int snd_soc_add_dai_link(struct snd_soc_card *card,
1138 struct snd_soc_dai_link *dai_link)
1140 if (dai_link->dobj.type
1141 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1142 dev_err(card->dev, "Invalid dai link type %d\n",
1143 dai_link->dobj.type);
1147 lockdep_assert_held(&client_mutex);
1148 /* Notify the machine driver for extra initialization
1149 * on the link created by topology.
1151 if (dai_link->dobj.type && card->add_dai_link)
1152 card->add_dai_link(card, dai_link);
1154 list_add_tail(&dai_link->list, &card->dai_link_list);
1155 card->num_dai_links++;
1159 EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1162 * snd_soc_remove_dai_link - Remove a DAI link from the list
1163 * @card: The ASoC card that owns the link
1164 * @dai_link: The DAI link to remove
1166 * This function removes a DAI link from the ASoC card's link list.
1168 * For DAI links previously added by topology, topology should
1169 * remove them by using the dobj embedded in the link.
1171 void snd_soc_remove_dai_link(struct snd_soc_card *card,
1172 struct snd_soc_dai_link *dai_link)
1174 struct snd_soc_dai_link *link, *_link;
1176 if (dai_link->dobj.type
1177 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1178 dev_err(card->dev, "Invalid dai link type %d\n",
1179 dai_link->dobj.type);
1183 lockdep_assert_held(&client_mutex);
1184 /* Notify the machine driver for extra destruction
1185 * on the link created by topology.
1187 if (dai_link->dobj.type && card->remove_dai_link)
1188 card->remove_dai_link(card, dai_link);
1190 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1191 if (link == dai_link) {
1192 list_del(&link->list);
1193 card->num_dai_links--;
1198 EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1200 static void soc_set_name_prefix(struct snd_soc_card *card,
1201 struct snd_soc_component *component)
1205 if (card->codec_conf == NULL)
1208 for (i = 0; i < card->num_configs; i++) {
1209 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1210 struct device_node *component_of_node = component->dev->of_node;
1212 if (!component_of_node && component->dev->parent)
1213 component_of_node = component->dev->parent->of_node;
1215 if (map->of_node && component_of_node != map->of_node)
1217 if (map->dev_name && strcmp(component->name, map->dev_name))
1219 component->name_prefix = map->name_prefix;
1224 static int soc_probe_component(struct snd_soc_card *card,
1225 struct snd_soc_component *component)
1227 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1228 struct snd_soc_dai *dai;
1231 if (!strcmp(component->name, "snd-soc-dummy"))
1234 if (component->card) {
1235 if (component->card != card) {
1236 dev_err(component->dev,
1237 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1238 card->name, component->card->name);
1244 if (!try_module_get(component->dev->driver->owner))
1247 component->card = card;
1249 soc_set_name_prefix(card, component);
1251 soc_init_component_debugfs(component);
1253 if (component->driver->dapm_widgets) {
1254 ret = snd_soc_dapm_new_controls(dapm,
1255 component->driver->dapm_widgets,
1256 component->driver->num_dapm_widgets);
1259 dev_err(component->dev,
1260 "Failed to create new controls %d\n", ret);
1265 list_for_each_entry(dai, &component->dai_list, list) {
1266 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1268 dev_err(component->dev,
1269 "Failed to create DAI widgets %d\n", ret);
1274 if (component->probe) {
1275 ret = component->probe(component);
1277 dev_err(component->dev,
1278 "ASoC: failed to probe component %d\n", ret);
1282 WARN(dapm->idle_bias_off &&
1283 dapm->bias_level != SND_SOC_BIAS_OFF,
1284 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1288 /* machine specific init */
1289 if (component->init) {
1290 ret = component->init(component);
1292 dev_err(component->dev,
1293 "Failed to do machine specific init %d\n", ret);
1298 if (component->driver->controls)
1299 snd_soc_add_component_controls(component,
1300 component->driver->controls,
1301 component->driver->num_controls);
1302 if (component->driver->dapm_routes)
1303 snd_soc_dapm_add_routes(dapm,
1304 component->driver->dapm_routes,
1305 component->driver->num_dapm_routes);
1307 list_add(&dapm->list, &card->dapm_list);
1308 list_add(&component->card_list, &card->component_dev_list);
1313 soc_cleanup_component_debugfs(component);
1314 component->card = NULL;
1315 module_put(component->dev->driver->owner);
1320 static void rtd_release(struct device *dev)
1325 static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1330 /* register the rtd device */
1331 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1334 device_initialize(rtd->dev);
1335 rtd->dev->parent = rtd->card->dev;
1336 rtd->dev->release = rtd_release;
1337 rtd->dev->groups = soc_dev_attr_groups;
1338 dev_set_name(rtd->dev, "%s", name);
1339 dev_set_drvdata(rtd->dev, rtd);
1340 mutex_init(&rtd->pcm_mutex);
1341 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1342 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1343 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1344 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1345 ret = device_add(rtd->dev);
1347 /* calling put_device() here to free the rtd->dev */
1348 put_device(rtd->dev);
1349 dev_err(rtd->card->dev,
1350 "ASoC: failed to register runtime device: %d\n", ret);
1353 rtd->dev_registered = 1;
1357 static int soc_probe_link_components(struct snd_soc_card *card,
1358 struct snd_soc_pcm_runtime *rtd,
1361 struct snd_soc_component *component;
1362 struct snd_soc_rtdcom_list *rtdcom;
1365 for_each_rtdcom(rtd, rtdcom) {
1366 component = rtdcom->component;
1368 if (component->driver->probe_order == order) {
1369 ret = soc_probe_component(card, component);
1378 static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1381 dai->driver->probe_order != order)
1384 if (dai->driver->probe) {
1385 int ret = dai->driver->probe(dai);
1387 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1398 static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1399 struct snd_soc_pcm_runtime *rtd)
1403 for (i = 0; i < num_dais; ++i) {
1404 struct snd_soc_dai_driver *drv = dais[i]->driver;
1406 if (!rtd->dai_link->no_pcm && drv->pcm_new)
1407 ret = drv->pcm_new(rtd, dais[i]);
1409 dev_err(dais[i]->dev,
1410 "ASoC: Failed to bind %s with pcm device\n",
1419 static int soc_link_dai_widgets(struct snd_soc_card *card,
1420 struct snd_soc_dai_link *dai_link,
1421 struct snd_soc_pcm_runtime *rtd)
1423 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1424 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1425 struct snd_soc_dapm_widget *sink, *source;
1428 if (rtd->num_codecs > 1)
1429 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1431 /* link the DAI widgets */
1432 sink = codec_dai->playback_widget;
1433 source = cpu_dai->capture_widget;
1434 if (sink && source) {
1435 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1436 dai_link->num_params,
1439 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1440 sink->name, source->name, ret);
1445 sink = cpu_dai->playback_widget;
1446 source = codec_dai->capture_widget;
1447 if (sink && source) {
1448 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1449 dai_link->num_params,
1452 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1453 sink->name, source->name, ret);
1461 static int soc_probe_link_dais(struct snd_soc_card *card,
1462 struct snd_soc_pcm_runtime *rtd, int order)
1464 struct snd_soc_dai_link *dai_link = rtd->dai_link;
1465 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1468 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1469 card->name, rtd->num, order);
1471 /* set default power off timeout */
1472 rtd->pmdown_time = pmdown_time;
1474 ret = soc_probe_dai(cpu_dai, order);
1478 /* probe the CODEC DAI */
1479 for (i = 0; i < rtd->num_codecs; i++) {
1480 ret = soc_probe_dai(rtd->codec_dais[i], order);
1485 /* complete DAI probe during last probe */
1486 if (order != SND_SOC_COMP_ORDER_LAST)
1489 /* do machine specific initialization */
1490 if (dai_link->init) {
1491 ret = dai_link->init(rtd);
1493 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1494 dai_link->name, ret);
1499 if (dai_link->dai_fmt)
1500 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1502 ret = soc_post_component_init(rtd, dai_link->name);
1506 #ifdef CONFIG_DEBUG_FS
1507 /* add DPCM sysfs entries */
1508 if (dai_link->dynamic)
1509 soc_dpcm_debugfs_add(rtd);
1512 if (cpu_dai->driver->compress_new) {
1513 /*create compress_device"*/
1514 ret = cpu_dai->driver->compress_new(rtd, rtd->num);
1516 dev_err(card->dev, "ASoC: can't create compress %s\n",
1517 dai_link->stream_name);
1522 if (!dai_link->params) {
1523 /* create the pcm */
1524 ret = soc_new_pcm(rtd, rtd->num);
1526 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1527 dai_link->stream_name, ret);
1530 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1533 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1534 rtd->num_codecs, rtd);
1538 INIT_DELAYED_WORK(&rtd->delayed_work,
1539 codec2codec_close_delayed_work);
1541 /* link the DAI widgets */
1542 ret = soc_link_dai_widgets(card, dai_link, rtd);
1551 static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1553 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1554 struct snd_soc_component *component;
1556 struct device_node *codec_of_node;
1558 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1559 /* codecs, usually analog devices */
1560 name = aux_dev->codec_name;
1561 codec_of_node = aux_dev->codec_of_node;
1562 component = soc_find_component(codec_of_node, name);
1565 name = of_node_full_name(codec_of_node);
1568 } else if (aux_dev->name) {
1569 /* generic components */
1570 name = aux_dev->name;
1571 component = soc_find_component(NULL, name);
1575 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1579 component->init = aux_dev->init;
1580 list_add(&component->card_aux_list, &card->aux_comp_list);
1585 dev_err(card->dev, "ASoC: %s not registered\n", name);
1586 return -EPROBE_DEFER;
1589 static int soc_probe_aux_devices(struct snd_soc_card *card)
1591 struct snd_soc_component *comp;
1595 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1597 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
1598 if (comp->driver->probe_order == order) {
1599 ret = soc_probe_component(card, comp);
1602 "ASoC: failed to probe aux component %s %d\n",
1613 static void soc_remove_aux_devices(struct snd_soc_card *card)
1615 struct snd_soc_component *comp, *_comp;
1618 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1620 list_for_each_entry_safe(comp, _comp,
1621 &card->aux_comp_list, card_aux_list) {
1623 if (comp->driver->remove_order == order) {
1624 soc_remove_component(comp);
1625 /* remove it from the card's aux_comp_list */
1626 list_del(&comp->card_aux_list);
1633 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1634 * @rtd: The runtime for which the DAI link format should be changed
1635 * @dai_fmt: The new DAI link format
1637 * This function updates the DAI link format for all DAIs connected to the DAI
1638 * link for the specified runtime.
1640 * Note: For setups with a static format set the dai_fmt field in the
1641 * corresponding snd_dai_link struct instead of using this function.
1643 * Returns 0 on success, otherwise a negative error code.
1645 int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1646 unsigned int dai_fmt)
1648 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1649 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1653 for (i = 0; i < rtd->num_codecs; i++) {
1654 struct snd_soc_dai *codec_dai = codec_dais[i];
1656 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1657 if (ret != 0 && ret != -ENOTSUPP) {
1658 dev_warn(codec_dai->dev,
1659 "ASoC: Failed to set DAI format: %d\n", ret);
1664 /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1665 /* the component which has non_legacy_dai_naming is Codec */
1666 if (cpu_dai->codec ||
1667 cpu_dai->component->driver->non_legacy_dai_naming) {
1668 unsigned int inv_dai_fmt;
1670 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1671 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1672 case SND_SOC_DAIFMT_CBM_CFM:
1673 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1675 case SND_SOC_DAIFMT_CBM_CFS:
1676 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1678 case SND_SOC_DAIFMT_CBS_CFM:
1679 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1681 case SND_SOC_DAIFMT_CBS_CFS:
1682 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1686 dai_fmt = inv_dai_fmt;
1689 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1690 if (ret != 0 && ret != -ENOTSUPP) {
1691 dev_warn(cpu_dai->dev,
1692 "ASoC: Failed to set DAI format: %d\n", ret);
1698 EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1702 /* Trim special characters, and replace '-' with '_' since '-' is used to
1703 * separate different DMI fields in the card long name. Only number and
1704 * alphabet characters and a few separator characters are kept.
1706 static void cleanup_dmi_name(char *name)
1710 for (i = 0; name[i]; i++) {
1711 if (isalnum(name[i]) || (name[i] == '.')
1712 || (name[i] == '_'))
1713 name[j++] = name[i];
1714 else if (name[i] == '-')
1721 /* Check if a DMI field is valid, i.e. not containing any string
1722 * in the black list.
1724 static int is_dmi_valid(const char *field)
1728 while (dmi_blacklist[i]) {
1729 if (strstr(field, dmi_blacklist[i]))
1738 * snd_soc_set_dmi_name() - Register DMI names to card
1739 * @card: The card to register DMI names
1740 * @flavour: The flavour "differentiator" for the card amongst its peers.
1742 * An Intel machine driver may be used by many different devices but are
1743 * difficult for userspace to differentiate, since machine drivers ususally
1744 * use their own name as the card short name and leave the card long name
1745 * blank. To differentiate such devices and fix bugs due to lack of
1746 * device-specific configurations, this function allows DMI info to be used
1747 * as the sound card long name, in the format of
1748 * "vendor-product-version-board"
1749 * (Character '-' is used to separate different DMI fields here).
1750 * This will help the user space to load the device-specific Use Case Manager
1751 * (UCM) configurations for the card.
1753 * Possible card long names may be:
1754 * DellInc.-XPS139343-01-0310JH
1755 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1756 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1758 * This function also supports flavoring the card longname to provide
1759 * the extra differentiation, like "vendor-product-version-board-flavor".
1761 * We only keep number and alphabet characters and a few separator characters
1762 * in the card long name since UCM in the user space uses the card long names
1763 * as card configuration directory names and AudoConf cannot support special
1764 * charactors like SPACE.
1766 * Returns 0 on success, otherwise a negative error code.
1768 int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1770 const char *vendor, *product, *product_version, *board;
1771 size_t longname_buf_size = sizeof(card->snd_card->longname);
1774 if (card->long_name)
1775 return 0; /* long name already set by driver or from DMI */
1777 /* make up dmi long name as: vendor.product.version.board */
1778 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1779 if (!vendor || !is_dmi_valid(vendor)) {
1780 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1785 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1787 cleanup_dmi_name(card->dmi_longname);
1789 product = dmi_get_system_info(DMI_PRODUCT_NAME);
1790 if (product && is_dmi_valid(product)) {
1791 len = strlen(card->dmi_longname);
1792 snprintf(card->dmi_longname + len,
1793 longname_buf_size - len,
1796 len++; /* skip the separator "-" */
1797 if (len < longname_buf_size)
1798 cleanup_dmi_name(card->dmi_longname + len);
1800 /* some vendors like Lenovo may only put a self-explanatory
1801 * name in the product version field
1803 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
1804 if (product_version && is_dmi_valid(product_version)) {
1805 len = strlen(card->dmi_longname);
1806 snprintf(card->dmi_longname + len,
1807 longname_buf_size - len,
1808 "-%s", product_version);
1811 if (len < longname_buf_size)
1812 cleanup_dmi_name(card->dmi_longname + len);
1816 board = dmi_get_system_info(DMI_BOARD_NAME);
1817 if (board && is_dmi_valid(board)) {
1818 len = strlen(card->dmi_longname);
1819 snprintf(card->dmi_longname + len,
1820 longname_buf_size - len,
1824 if (len < longname_buf_size)
1825 cleanup_dmi_name(card->dmi_longname + len);
1826 } else if (!product) {
1827 /* fall back to using legacy name */
1828 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1832 /* Add flavour to dmi long name */
1834 len = strlen(card->dmi_longname);
1835 snprintf(card->dmi_longname + len,
1836 longname_buf_size - len,
1840 if (len < longname_buf_size)
1841 cleanup_dmi_name(card->dmi_longname + len);
1844 /* set the card long name */
1845 card->long_name = card->dmi_longname;
1849 EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
1850 #endif /* CONFIG_DMI */
1852 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1854 struct snd_soc_pcm_runtime *rtd;
1855 struct snd_soc_dai_link *dai_link;
1858 mutex_lock(&client_mutex);
1859 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1862 for (i = 0; i < card->num_links; i++) {
1863 ret = soc_bind_dai_link(card, &card->dai_link[i]);
1868 /* bind aux_devs too */
1869 for (i = 0; i < card->num_aux_devs; i++) {
1870 ret = soc_bind_aux_dev(card, i);
1875 /* add predefined DAI links to the list */
1876 for (i = 0; i < card->num_links; i++)
1877 snd_soc_add_dai_link(card, card->dai_link+i);
1879 /* card bind complete so register a sound card */
1880 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1881 card->owner, 0, &card->snd_card);
1884 "ASoC: can't create sound card for card %s: %d\n",
1889 soc_init_card_debugfs(card);
1891 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1892 card->dapm.dev = card->dev;
1893 card->dapm.card = card;
1894 list_add(&card->dapm.list, &card->dapm_list);
1896 #ifdef CONFIG_DEBUG_FS
1897 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1900 #ifdef CONFIG_PM_SLEEP
1901 /* deferred resume work */
1902 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1905 if (card->dapm_widgets)
1906 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1907 card->num_dapm_widgets);
1909 if (card->of_dapm_widgets)
1910 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1911 card->num_of_dapm_widgets);
1913 /* initialise the sound card only once */
1915 ret = card->probe(card);
1917 goto card_probe_error;
1920 /* probe all components used by DAI links on this card */
1921 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1923 list_for_each_entry(rtd, &card->rtd_list, list) {
1924 ret = soc_probe_link_components(card, rtd, order);
1927 "ASoC: failed to instantiate card %d\n",
1934 /* probe auxiliary components */
1935 ret = soc_probe_aux_devices(card);
1939 /* Find new DAI links added during probing components and bind them.
1940 * Components with topology may bring new DAIs and DAI links.
1942 list_for_each_entry(dai_link, &card->dai_link_list, list) {
1943 if (soc_is_dai_link_bound(card, dai_link))
1946 ret = soc_init_dai_link(card, dai_link);
1949 ret = soc_bind_dai_link(card, dai_link);
1954 /* probe all DAI links on this card */
1955 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1957 list_for_each_entry(rtd, &card->rtd_list, list) {
1958 ret = soc_probe_link_dais(card, rtd, order);
1961 "ASoC: failed to instantiate card %d\n",
1968 snd_soc_dapm_link_dai_widgets(card);
1969 snd_soc_dapm_connect_dai_link_widgets(card);
1972 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1974 if (card->dapm_routes)
1975 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1976 card->num_dapm_routes);
1978 if (card->of_dapm_routes)
1979 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
1980 card->num_of_dapm_routes);
1982 /* try to set some sane longname if DMI is available */
1983 snd_soc_set_dmi_name(card, NULL);
1985 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1987 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1988 "%s", card->long_name ? card->long_name : card->name);
1989 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1990 "%s", card->driver_name ? card->driver_name : card->name);
1991 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1992 switch (card->snd_card->driver[i]) {
1998 if (!isalnum(card->snd_card->driver[i]))
1999 card->snd_card->driver[i] = '_';
2004 if (card->late_probe) {
2005 ret = card->late_probe(card);
2007 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
2009 goto probe_aux_dev_err;
2013 snd_soc_dapm_new_widgets(card);
2015 ret = snd_card_register(card->snd_card);
2017 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2019 goto probe_aux_dev_err;
2022 card->instantiated = 1;
2023 snd_soc_dapm_sync(&card->dapm);
2024 mutex_unlock(&card->mutex);
2025 mutex_unlock(&client_mutex);
2030 soc_remove_aux_devices(card);
2033 soc_remove_dai_links(card);
2039 snd_soc_dapm_free(&card->dapm);
2040 soc_cleanup_card_debugfs(card);
2041 snd_card_free(card->snd_card);
2044 soc_remove_pcm_runtimes(card);
2045 mutex_unlock(&card->mutex);
2046 mutex_unlock(&client_mutex);
2051 /* probes a new socdev */
2052 static int soc_probe(struct platform_device *pdev)
2054 struct snd_soc_card *card = platform_get_drvdata(pdev);
2057 * no card, so machine driver should be registering card
2058 * we should not be here in that case so ret error
2063 dev_warn(&pdev->dev,
2064 "ASoC: machine %s should use snd_soc_register_card()\n",
2067 /* Bodge while we unpick instantiation */
2068 card->dev = &pdev->dev;
2070 return snd_soc_register_card(card);
2073 static int soc_cleanup_card_resources(struct snd_soc_card *card)
2075 struct snd_soc_pcm_runtime *rtd;
2077 /* make sure any delayed work runs */
2078 list_for_each_entry(rtd, &card->rtd_list, list)
2079 flush_delayed_work(&rtd->delayed_work);
2081 /* free the ALSA card at first; this syncs with pending operations */
2082 snd_card_free(card->snd_card);
2084 /* remove and free each DAI */
2085 soc_remove_dai_links(card);
2086 soc_remove_pcm_runtimes(card);
2088 /* remove auxiliary devices */
2089 soc_remove_aux_devices(card);
2091 snd_soc_dapm_free(&card->dapm);
2092 soc_cleanup_card_debugfs(card);
2094 /* remove the card */
2101 /* removes a socdev */
2102 static int soc_remove(struct platform_device *pdev)
2104 struct snd_soc_card *card = platform_get_drvdata(pdev);
2106 snd_soc_unregister_card(card);
2110 int snd_soc_poweroff(struct device *dev)
2112 struct snd_soc_card *card = dev_get_drvdata(dev);
2113 struct snd_soc_pcm_runtime *rtd;
2115 if (!card->instantiated)
2118 /* Flush out pmdown_time work - we actually do want to run it
2119 * now, we're shutting down so no imminent restart. */
2120 list_for_each_entry(rtd, &card->rtd_list, list)
2121 flush_delayed_work(&rtd->delayed_work);
2123 snd_soc_dapm_shutdown(card);
2125 /* deactivate pins to sleep state */
2126 list_for_each_entry(rtd, &card->rtd_list, list) {
2127 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2130 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2131 for (i = 0; i < rtd->num_codecs; i++) {
2132 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
2133 pinctrl_pm_select_sleep_state(codec_dai->dev);
2139 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2141 const struct dev_pm_ops snd_soc_pm_ops = {
2142 .suspend = snd_soc_suspend,
2143 .resume = snd_soc_resume,
2144 .freeze = snd_soc_suspend,
2145 .thaw = snd_soc_resume,
2146 .poweroff = snd_soc_poweroff,
2147 .restore = snd_soc_resume,
2149 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2151 /* ASoC platform driver */
2152 static struct platform_driver soc_driver = {
2154 .name = "soc-audio",
2155 .pm = &snd_soc_pm_ops,
2158 .remove = soc_remove,
2162 * snd_soc_cnew - create new control
2163 * @_template: control template
2164 * @data: control private data
2165 * @long_name: control long name
2166 * @prefix: control name prefix
2168 * Create a new mixer control from a template control.
2170 * Returns 0 for success, else error.
2172 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2173 void *data, const char *long_name,
2176 struct snd_kcontrol_new template;
2177 struct snd_kcontrol *kcontrol;
2180 memcpy(&template, _template, sizeof(template));
2184 long_name = template.name;
2187 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2191 template.name = name;
2193 template.name = long_name;
2196 kcontrol = snd_ctl_new1(&template, data);
2202 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2204 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2205 const struct snd_kcontrol_new *controls, int num_controls,
2206 const char *prefix, void *data)
2210 for (i = 0; i < num_controls; i++) {
2211 const struct snd_kcontrol_new *control = &controls[i];
2212 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2213 control->name, prefix));
2215 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2216 control->name, err);
2224 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2227 struct snd_card *card = soc_card->snd_card;
2228 struct snd_kcontrol *kctl;
2230 if (unlikely(!name))
2233 list_for_each_entry(kctl, &card->controls, list)
2234 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2238 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2241 * snd_soc_add_component_controls - Add an array of controls to a component.
2243 * @component: Component to add controls to
2244 * @controls: Array of controls to add
2245 * @num_controls: Number of elements in the array
2247 * Return: 0 for success, else error.
2249 int snd_soc_add_component_controls(struct snd_soc_component *component,
2250 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2252 struct snd_card *card = component->card->snd_card;
2254 return snd_soc_add_controls(card, component->dev, controls,
2255 num_controls, component->name_prefix, component);
2257 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2260 * snd_soc_add_codec_controls - add an array of controls to a codec.
2261 * Convenience function to add a list of controls. Many codecs were
2262 * duplicating this code.
2264 * @codec: codec to add controls to
2265 * @controls: array of controls to add
2266 * @num_controls: number of elements in the array
2268 * Return 0 for success, else error.
2270 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2271 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2273 return snd_soc_add_component_controls(&codec->component, controls,
2276 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2279 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2280 * Convenience function to add a list of controls.
2282 * @soc_card: SoC card to add controls to
2283 * @controls: array of controls to add
2284 * @num_controls: number of elements in the array
2286 * Return 0 for success, else error.
2288 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2289 const struct snd_kcontrol_new *controls, int num_controls)
2291 struct snd_card *card = soc_card->snd_card;
2293 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2296 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2299 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2300 * Convienience function to add a list of controls.
2302 * @dai: DAI to add controls to
2303 * @controls: array of controls to add
2304 * @num_controls: number of elements in the array
2306 * Return 0 for success, else error.
2308 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2309 const struct snd_kcontrol_new *controls, int num_controls)
2311 struct snd_card *card = dai->component->card->snd_card;
2313 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2316 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2319 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2321 * @clk_id: DAI specific clock ID
2322 * @freq: new clock frequency in Hz
2323 * @dir: new clock direction - input/output.
2325 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2327 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2328 unsigned int freq, int dir)
2330 if (dai->driver->ops->set_sysclk)
2331 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2333 return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2336 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2339 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2341 * @clk_id: DAI specific clock ID
2342 * @source: Source for the clock
2343 * @freq: new clock frequency in Hz
2344 * @dir: new clock direction - input/output.
2346 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2348 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2349 int source, unsigned int freq, int dir)
2351 if (codec->driver->set_sysclk)
2352 return codec->driver->set_sysclk(codec, clk_id, source,
2357 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2360 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2361 * @component: COMPONENT
2362 * @clk_id: DAI specific clock ID
2363 * @source: Source for the clock
2364 * @freq: new clock frequency in Hz
2365 * @dir: new clock direction - input/output.
2367 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2369 int snd_soc_component_set_sysclk(struct snd_soc_component *component, int clk_id,
2370 int source, unsigned int freq, int dir)
2372 /* will be removed */
2373 if (component->set_sysclk)
2374 return component->set_sysclk(component, clk_id, source,
2377 if (component->driver->set_sysclk)
2378 return component->driver->set_sysclk(component, clk_id, source,
2383 EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2386 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2388 * @div_id: DAI specific clock divider ID
2389 * @div: new clock divisor.
2391 * Configures the clock dividers. This is used to derive the best DAI bit and
2392 * frame clocks from the system or master clock. It's best to set the DAI bit
2393 * and frame clocks as low as possible to save system power.
2395 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2396 int div_id, int div)
2398 if (dai->driver->ops->set_clkdiv)
2399 return dai->driver->ops->set_clkdiv(dai, div_id, div);
2403 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2406 * snd_soc_dai_set_pll - configure DAI PLL.
2408 * @pll_id: DAI specific PLL ID
2409 * @source: DAI specific source for the PLL
2410 * @freq_in: PLL input clock frequency in Hz
2411 * @freq_out: requested PLL output clock frequency in Hz
2413 * Configures and enables PLL to generate output clock based on input clock.
2415 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2416 unsigned int freq_in, unsigned int freq_out)
2418 if (dai->driver->ops->set_pll)
2419 return dai->driver->ops->set_pll(dai, pll_id, source,
2422 return snd_soc_component_set_pll(dai->component, pll_id, source,
2425 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2428 * snd_soc_codec_set_pll - configure codec PLL.
2430 * @pll_id: DAI specific PLL ID
2431 * @source: DAI specific source for the PLL
2432 * @freq_in: PLL input clock frequency in Hz
2433 * @freq_out: requested PLL output clock frequency in Hz
2435 * Configures and enables PLL to generate output clock based on input clock.
2437 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2438 unsigned int freq_in, unsigned int freq_out)
2440 if (codec->driver->set_pll)
2441 return codec->driver->set_pll(codec, pll_id, source,
2446 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2449 * snd_soc_component_set_pll - configure component PLL.
2450 * @component: COMPONENT
2451 * @pll_id: DAI specific PLL ID
2452 * @source: DAI specific source for the PLL
2453 * @freq_in: PLL input clock frequency in Hz
2454 * @freq_out: requested PLL output clock frequency in Hz
2456 * Configures and enables PLL to generate output clock based on input clock.
2458 int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2459 int source, unsigned int freq_in,
2460 unsigned int freq_out)
2462 /* will be removed */
2463 if (component->set_pll)
2464 return component->set_pll(component, pll_id, source,
2467 if (component->driver->set_pll)
2468 return component->driver->set_pll(component, pll_id, source,
2473 EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2476 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2478 * @ratio: Ratio of BCLK to Sample rate.
2480 * Configures the DAI for a preset BCLK to sample rate ratio.
2482 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2484 if (dai->driver->ops->set_bclk_ratio)
2485 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2489 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2492 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2494 * @fmt: SND_SOC_DAIFMT_* format value.
2496 * Configures the DAI hardware format and clocking.
2498 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2500 if (dai->driver == NULL)
2502 if (dai->driver->ops->set_fmt == NULL)
2504 return dai->driver->ops->set_fmt(dai, fmt);
2506 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2509 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
2510 * @slots: Number of slots in use.
2511 * @tx_mask: bitmask representing active TX slots.
2512 * @rx_mask: bitmask representing active RX slots.
2514 * Generates the TDM tx and rx slot default masks for DAI.
2516 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
2517 unsigned int *tx_mask,
2518 unsigned int *rx_mask)
2520 if (*tx_mask || *rx_mask)
2526 *tx_mask = (1 << slots) - 1;
2527 *rx_mask = (1 << slots) - 1;
2533 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2534 * @dai: The DAI to configure
2535 * @tx_mask: bitmask representing active TX slots.
2536 * @rx_mask: bitmask representing active RX slots.
2537 * @slots: Number of slots in use.
2538 * @slot_width: Width in bits for each slot.
2540 * This function configures the specified DAI for TDM operation. @slot contains
2541 * the total number of slots of the TDM stream and @slot_with the width of each
2542 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2543 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2544 * DAI should write to or read from. If a bit is set the corresponding slot is
2545 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2546 * the first slot, bit 1 to the second slot and so on. The first active slot
2547 * maps to the first channel of the DAI, the second active slot to the second
2548 * channel and so on.
2550 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2551 * @rx_mask and @slot_width will be ignored.
2553 * Returns 0 on success, a negative error code otherwise.
2555 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2556 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2558 if (dai->driver->ops->xlate_tdm_slot_mask)
2559 dai->driver->ops->xlate_tdm_slot_mask(slots,
2560 &tx_mask, &rx_mask);
2562 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
2564 dai->tx_mask = tx_mask;
2565 dai->rx_mask = rx_mask;
2567 if (dai->driver->ops->set_tdm_slot)
2568 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2573 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2576 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2578 * @tx_num: how many TX channels
2579 * @tx_slot: pointer to an array which imply the TX slot number channel
2581 * @rx_num: how many RX channels
2582 * @rx_slot: pointer to an array which imply the RX slot number channel
2585 * configure the relationship between channel number and TDM slot number.
2587 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2588 unsigned int tx_num, unsigned int *tx_slot,
2589 unsigned int rx_num, unsigned int *rx_slot)
2591 if (dai->driver->ops->set_channel_map)
2592 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2597 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2600 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2602 * @tristate: tristate enable
2604 * Tristates the DAI so that others can use it.
2606 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2608 if (dai->driver->ops->set_tristate)
2609 return dai->driver->ops->set_tristate(dai, tristate);
2613 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2616 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2618 * @mute: mute enable
2619 * @direction: stream to mute
2621 * Mutes the DAI DAC.
2623 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2629 if (dai->driver->ops->mute_stream)
2630 return dai->driver->ops->mute_stream(dai, mute, direction);
2631 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2632 dai->driver->ops->digital_mute)
2633 return dai->driver->ops->digital_mute(dai, mute);
2637 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2640 * snd_soc_register_card - Register a card with the ASoC core
2642 * @card: Card to register
2645 int snd_soc_register_card(struct snd_soc_card *card)
2648 struct snd_soc_pcm_runtime *rtd;
2650 if (!card->name || !card->dev)
2653 for (i = 0; i < card->num_links; i++) {
2654 struct snd_soc_dai_link *link = &card->dai_link[i];
2656 ret = soc_init_dai_link(card, link);
2658 dev_err(card->dev, "ASoC: failed to init link %s\n",
2664 dev_set_drvdata(card->dev, card);
2666 snd_soc_initialize_card_lists(card);
2668 INIT_LIST_HEAD(&card->dai_link_list);
2669 card->num_dai_links = 0;
2671 INIT_LIST_HEAD(&card->rtd_list);
2674 INIT_LIST_HEAD(&card->dapm_dirty);
2675 INIT_LIST_HEAD(&card->dobj_list);
2676 card->instantiated = 0;
2677 mutex_init(&card->mutex);
2678 mutex_init(&card->dapm_mutex);
2680 ret = snd_soc_instantiate_card(card);
2684 /* deactivate pins to sleep state */
2685 list_for_each_entry(rtd, &card->rtd_list, list) {
2686 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2689 for (j = 0; j < rtd->num_codecs; j++) {
2690 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2691 if (!codec_dai->active)
2692 pinctrl_pm_select_sleep_state(codec_dai->dev);
2695 if (!cpu_dai->active)
2696 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2701 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2704 * snd_soc_unregister_card - Unregister a card with the ASoC core
2706 * @card: Card to unregister
2709 int snd_soc_unregister_card(struct snd_soc_card *card)
2711 if (card->instantiated) {
2712 card->instantiated = false;
2713 snd_soc_dapm_shutdown(card);
2714 soc_cleanup_card_resources(card);
2715 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2720 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2723 * Simplify DAI link configuration by removing ".-1" from device names
2724 * and sanitizing names.
2726 static char *fmt_single_name(struct device *dev, int *id)
2728 char *found, name[NAME_SIZE];
2731 if (dev_name(dev) == NULL)
2734 strlcpy(name, dev_name(dev), NAME_SIZE);
2736 /* are we a "%s.%d" name (platform and SPI components) */
2737 found = strstr(name, dev->driver->name);
2740 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2742 /* discard ID from name if ID == -1 */
2744 found[strlen(dev->driver->name)] = '\0';
2748 /* I2C component devices are named "bus-addr" */
2749 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2750 char tmp[NAME_SIZE];
2752 /* create unique ID number from I2C addr and bus */
2753 *id = ((id1 & 0xffff) << 16) + id2;
2755 /* sanitize component name for DAI link creation */
2756 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2757 strlcpy(name, tmp, NAME_SIZE);
2762 return kstrdup(name, GFP_KERNEL);
2766 * Simplify DAI link naming for single devices with multiple DAIs by removing
2767 * any ".-1" and using the DAI name (instead of device name).
2769 static inline char *fmt_multiple_name(struct device *dev,
2770 struct snd_soc_dai_driver *dai_drv)
2772 if (dai_drv->name == NULL) {
2774 "ASoC: error - multiple DAI %s registered with no name\n",
2779 return kstrdup(dai_drv->name, GFP_KERNEL);
2783 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2785 * @component: The component for which the DAIs should be unregistered
2787 static void snd_soc_unregister_dais(struct snd_soc_component *component)
2789 struct snd_soc_dai *dai, *_dai;
2791 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
2792 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2794 list_del(&dai->list);
2800 /* Create a DAI and add it to the component's DAI list */
2801 static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2802 struct snd_soc_dai_driver *dai_drv,
2803 bool legacy_dai_naming)
2805 struct device *dev = component->dev;
2806 struct snd_soc_dai *dai;
2808 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2810 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2815 * Back in the old days when we still had component-less DAIs,
2816 * instead of having a static name, component-less DAIs would
2817 * inherit the name of the parent device so it is possible to
2818 * register multiple instances of the DAI. We still need to keep
2819 * the same naming style even though those DAIs are not
2820 * component-less anymore.
2822 if (legacy_dai_naming &&
2823 (dai_drv->id == 0 || dai_drv->name == NULL)) {
2824 dai->name = fmt_single_name(dev, &dai->id);
2826 dai->name = fmt_multiple_name(dev, dai_drv);
2828 dai->id = dai_drv->id;
2830 dai->id = component->num_dai;
2832 if (dai->name == NULL) {
2837 dai->component = component;
2839 dai->driver = dai_drv;
2840 if (!dai->driver->ops)
2841 dai->driver->ops = &null_dai_ops;
2843 list_add_tail(&dai->list, &component->dai_list);
2844 component->num_dai++;
2846 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2851 * snd_soc_register_dais - Register a DAI with the ASoC core
2853 * @component: The component the DAIs are registered for
2854 * @dai_drv: DAI driver to use for the DAIs
2855 * @count: Number of DAIs
2856 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
2859 static int snd_soc_register_dais(struct snd_soc_component *component,
2860 struct snd_soc_dai_driver *dai_drv, size_t count,
2861 bool legacy_dai_naming)
2863 struct device *dev = component->dev;
2864 struct snd_soc_dai *dai;
2868 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
2870 for (i = 0; i < count; i++) {
2872 dai = soc_add_dai(component, dai_drv + i,
2873 count == 1 && legacy_dai_naming);
2883 snd_soc_unregister_dais(component);
2889 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2891 * @component: The component the DAIs are registered for
2892 * @dai_drv: DAI driver to use for the DAI
2894 * Topology can use this API to register DAIs when probing a component.
2895 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2896 * will be freed in the component cleanup.
2898 int snd_soc_register_dai(struct snd_soc_component *component,
2899 struct snd_soc_dai_driver *dai_drv)
2901 struct snd_soc_dapm_context *dapm =
2902 snd_soc_component_get_dapm(component);
2903 struct snd_soc_dai *dai;
2906 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
2907 dev_err(component->dev, "Invalid dai type %d\n",
2908 dai_drv->dobj.type);
2912 lockdep_assert_held(&client_mutex);
2913 dai = soc_add_dai(component, dai_drv, false);
2917 /* Create the DAI widgets here. After adding DAIs, topology may
2918 * also add routes that need these widgets as source or sink.
2920 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
2922 dev_err(component->dev,
2923 "Failed to create DAI widgets %d\n", ret);
2928 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2930 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
2931 enum snd_soc_dapm_type type, int subseq)
2933 struct snd_soc_component *component = dapm->component;
2935 component->driver->seq_notifier(component, type, subseq);
2938 static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
2941 struct snd_soc_component *component = dapm->component;
2943 return component->driver->stream_event(component, event);
2946 static int snd_soc_component_drv_pcm_new(struct snd_soc_component *component,
2947 struct snd_soc_pcm_runtime *rtd)
2949 if (component->driver->pcm_new)
2950 return component->driver->pcm_new(rtd);
2955 static void snd_soc_component_drv_pcm_free(struct snd_soc_component *component,
2956 struct snd_pcm *pcm)
2958 if (component->driver->pcm_free)
2959 component->driver->pcm_free(pcm);
2962 static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
2963 enum snd_soc_bias_level level)
2965 struct snd_soc_component *component = dapm->component;
2967 return component->driver->set_bias_level(component, level);
2970 static int snd_soc_component_initialize(struct snd_soc_component *component,
2971 const struct snd_soc_component_driver *driver, struct device *dev)
2973 struct snd_soc_dapm_context *dapm;
2975 component->name = fmt_single_name(dev, &component->id);
2976 if (!component->name) {
2977 dev_err(dev, "ASoC: Failed to allocate name\n");
2981 component->dev = dev;
2982 component->driver = driver;
2983 component->probe = component->driver->probe;
2984 component->remove = component->driver->remove;
2985 component->suspend = component->driver->suspend;
2986 component->resume = component->driver->resume;
2987 component->set_sysclk = component->driver->set_sysclk;
2988 component->set_pll = component->driver->set_pll;
2989 component->set_jack = component->driver->set_jack;
2990 component->pcm_new = snd_soc_component_drv_pcm_new;
2991 component->pcm_free = snd_soc_component_drv_pcm_free;
2993 dapm = snd_soc_component_get_dapm(component);
2995 dapm->component = component;
2996 dapm->bias_level = SND_SOC_BIAS_OFF;
2997 dapm->idle_bias_off = !driver->idle_bias_on;
2998 dapm->suspend_bias_off = driver->suspend_bias_off;
2999 if (driver->seq_notifier)
3000 dapm->seq_notifier = snd_soc_component_seq_notifier;
3001 if (driver->stream_event)
3002 dapm->stream_event = snd_soc_component_stream_event;
3003 if (driver->set_bias_level)
3004 dapm->set_bias_level = snd_soc_component_set_bias_level;
3006 INIT_LIST_HEAD(&component->dai_list);
3007 mutex_init(&component->io_mutex);
3012 static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
3014 int val_bytes = regmap_get_val_bytes(component->regmap);
3016 /* Errors are legitimate for non-integer byte multiples */
3018 component->val_bytes = val_bytes;
3021 #ifdef CONFIG_REGMAP
3024 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
3025 * @component: The component for which to initialize the regmap instance
3026 * @regmap: The regmap instance that should be used by the component
3028 * This function allows deferred assignment of the regmap instance that is
3029 * associated with the component. Only use this if the regmap instance is not
3030 * yet ready when the component is registered. The function must also be called
3031 * before the first IO attempt of the component.
3033 void snd_soc_component_init_regmap(struct snd_soc_component *component,
3034 struct regmap *regmap)
3036 component->regmap = regmap;
3037 snd_soc_component_setup_regmap(component);
3039 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3042 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
3043 * @component: The component for which to de-initialize the regmap instance
3045 * Calls regmap_exit() on the regmap instance associated to the component and
3046 * removes the regmap instance from the component.
3048 * This function should only be used if snd_soc_component_init_regmap() was used
3049 * to initialize the regmap instance.
3051 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3053 regmap_exit(component->regmap);
3054 component->regmap = NULL;
3056 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3060 static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
3062 if (!component->write && !component->read) {
3063 if (!component->regmap)
3064 component->regmap = dev_get_regmap(component->dev, NULL);
3065 if (component->regmap)
3066 snd_soc_component_setup_regmap(component);
3069 list_add(&component->list, &component_list);
3070 INIT_LIST_HEAD(&component->dobj_list);
3073 static void snd_soc_component_add(struct snd_soc_component *component)
3075 mutex_lock(&client_mutex);
3076 snd_soc_component_add_unlocked(component);
3077 mutex_unlock(&client_mutex);
3080 static void snd_soc_component_cleanup(struct snd_soc_component *component)
3082 snd_soc_unregister_dais(component);
3083 kfree(component->name);
3086 static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3088 struct snd_soc_card *card = component->card;
3091 snd_soc_unregister_card(card);
3093 list_del(&component->list);
3096 #define ENDIANNESS_MAP(name) \
3097 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3098 static u64 endianness_format_map[] = {
3099 ENDIANNESS_MAP(S16_),
3100 ENDIANNESS_MAP(U16_),
3101 ENDIANNESS_MAP(S24_),
3102 ENDIANNESS_MAP(U24_),
3103 ENDIANNESS_MAP(S32_),
3104 ENDIANNESS_MAP(U32_),
3105 ENDIANNESS_MAP(S24_3),
3106 ENDIANNESS_MAP(U24_3),
3107 ENDIANNESS_MAP(S20_3),
3108 ENDIANNESS_MAP(U20_3),
3109 ENDIANNESS_MAP(S18_3),
3110 ENDIANNESS_MAP(U18_3),
3111 ENDIANNESS_MAP(FLOAT_),
3112 ENDIANNESS_MAP(FLOAT64_),
3113 ENDIANNESS_MAP(IEC958_SUBFRAME_),
3117 * Fix up the DAI formats for endianness: codecs don't actually see
3118 * the endianness of the data but we're using the CPU format
3119 * definitions which do need to include endianness so we ensure that
3120 * codec DAIs always have both big and little endian variants set.
3122 static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3126 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3127 if (stream->formats & endianness_format_map[i])
3128 stream->formats |= endianness_format_map[i];
3131 int snd_soc_add_component(struct device *dev,
3132 struct snd_soc_component *component,
3133 const struct snd_soc_component_driver *component_driver,
3134 struct snd_soc_dai_driver *dai_drv,
3140 ret = snd_soc_component_initialize(component, component_driver, dev);
3144 component->ignore_pmdown_time = true;
3145 component->registered_as_component = true;
3147 if (component_driver->endianness) {
3148 for (i = 0; i < num_dai; i++) {
3149 convert_endianness_formats(&dai_drv[i].playback);
3150 convert_endianness_formats(&dai_drv[i].capture);
3154 ret = snd_soc_register_dais(component, dai_drv, num_dai,
3155 !component_driver->non_legacy_dai_naming);
3157 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3161 snd_soc_component_add(component);
3166 snd_soc_component_cleanup(component);
3170 EXPORT_SYMBOL_GPL(snd_soc_add_component);
3172 int snd_soc_register_component(struct device *dev,
3173 const struct snd_soc_component_driver *component_driver,
3174 struct snd_soc_dai_driver *dai_drv,
3177 struct snd_soc_component *component;
3179 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
3183 return snd_soc_add_component(dev, component, component_driver,
3186 EXPORT_SYMBOL_GPL(snd_soc_register_component);
3189 * snd_soc_unregister_component - Unregister all related component
3190 * from the ASoC core
3192 * @dev: The device to unregister
3194 static int __snd_soc_unregister_component(struct device *dev)
3196 struct snd_soc_component *component;
3199 mutex_lock(&client_mutex);
3200 list_for_each_entry(component, &component_list, list) {
3201 if (dev != component->dev ||
3202 !component->registered_as_component)
3205 snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);
3206 snd_soc_component_del_unlocked(component);
3210 mutex_unlock(&client_mutex);
3213 snd_soc_component_cleanup(component);
3219 void snd_soc_unregister_component(struct device *dev)
3221 while (__snd_soc_unregister_component(dev));
3223 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3225 struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
3226 const char *driver_name)
3228 struct snd_soc_component *component;
3229 struct snd_soc_component *ret;
3232 mutex_lock(&client_mutex);
3233 list_for_each_entry(component, &component_list, list) {
3234 if (dev != component->dev)
3238 (driver_name != component->driver->name) &&
3239 (strcmp(component->driver->name, driver_name) != 0))
3245 mutex_unlock(&client_mutex);
3249 EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
3251 static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3253 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3255 return codec->driver->probe(codec);
3258 static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3260 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3262 codec->driver->remove(codec);
3265 static int snd_soc_codec_drv_suspend(struct snd_soc_component *component)
3267 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3269 return codec->driver->suspend(codec);
3272 static int snd_soc_codec_drv_resume(struct snd_soc_component *component)
3274 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3276 return codec->driver->resume(codec);
3279 static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3280 unsigned int reg, unsigned int val)
3282 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3284 return codec->driver->write(codec, reg, val);
3287 static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3288 unsigned int reg, unsigned int *val)
3290 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3292 *val = codec->driver->read(codec, reg);
3297 static int snd_soc_codec_set_sysclk_(struct snd_soc_component *component,
3298 int clk_id, int source, unsigned int freq, int dir)
3300 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3302 return snd_soc_codec_set_sysclk(codec, clk_id, source, freq, dir);
3305 static int snd_soc_codec_set_pll_(struct snd_soc_component *component,
3306 int pll_id, int source, unsigned int freq_in,
3307 unsigned int freq_out)
3309 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3311 return snd_soc_codec_set_pll(codec, pll_id, source, freq_in, freq_out);
3314 static int snd_soc_codec_set_jack_(struct snd_soc_component *component,
3315 struct snd_soc_jack *jack, void *data)
3317 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3319 return snd_soc_codec_set_jack(codec, jack, data);
3322 static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
3323 enum snd_soc_bias_level level)
3325 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
3327 return codec->driver->set_bias_level(codec, level);
3331 * snd_soc_register_codec - Register a codec with the ASoC core
3333 * @dev: The parent device for this codec
3334 * @codec_drv: Codec driver
3335 * @dai_drv: The associated DAI driver
3336 * @num_dai: Number of DAIs
3338 int snd_soc_register_codec(struct device *dev,
3339 const struct snd_soc_codec_driver *codec_drv,
3340 struct snd_soc_dai_driver *dai_drv,
3343 struct snd_soc_dapm_context *dapm;
3344 struct snd_soc_codec *codec;
3345 struct snd_soc_dai *dai;
3348 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3350 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3354 codec->component.codec = codec;
3356 ret = snd_soc_component_initialize(&codec->component,
3357 &codec_drv->component_driver, dev);
3361 if (codec_drv->probe)
3362 codec->component.probe = snd_soc_codec_drv_probe;
3363 if (codec_drv->remove)
3364 codec->component.remove = snd_soc_codec_drv_remove;
3365 if (codec_drv->suspend)
3366 codec->component.suspend = snd_soc_codec_drv_suspend;
3367 if (codec_drv->resume)
3368 codec->component.resume = snd_soc_codec_drv_resume;
3369 if (codec_drv->write)
3370 codec->component.write = snd_soc_codec_drv_write;
3371 if (codec_drv->read)
3372 codec->component.read = snd_soc_codec_drv_read;
3373 if (codec_drv->set_sysclk)
3374 codec->component.set_sysclk = snd_soc_codec_set_sysclk_;
3375 if (codec_drv->set_pll)
3376 codec->component.set_pll = snd_soc_codec_set_pll_;
3377 if (codec_drv->set_jack)
3378 codec->component.set_jack = snd_soc_codec_set_jack_;
3379 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
3381 dapm = snd_soc_codec_get_dapm(codec);
3382 dapm->idle_bias_off = codec_drv->idle_bias_off;
3383 dapm->suspend_bias_off = codec_drv->suspend_bias_off;
3384 if (codec_drv->seq_notifier)
3385 dapm->seq_notifier = codec_drv->seq_notifier;
3386 if (codec_drv->set_bias_level)
3387 dapm->set_bias_level = snd_soc_codec_set_bias_level;
3389 codec->driver = codec_drv;
3391 #ifdef CONFIG_DEBUG_FS
3392 codec->component.debugfs_prefix = "codec";
3395 for (i = 0; i < num_dai; i++) {
3396 convert_endianness_formats(&dai_drv[i].playback);
3397 convert_endianness_formats(&dai_drv[i].capture);
3400 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3402 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3406 list_for_each_entry(dai, &codec->component.dai_list, list)
3409 mutex_lock(&client_mutex);
3410 snd_soc_component_add_unlocked(&codec->component);
3411 list_add(&codec->list, &codec_list);
3412 mutex_unlock(&client_mutex);
3414 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3415 codec->component.name);
3419 snd_soc_component_cleanup(&codec->component);
3424 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3427 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3429 * @dev: codec to unregister
3431 void snd_soc_unregister_codec(struct device *dev)
3433 struct snd_soc_codec *codec;
3435 mutex_lock(&client_mutex);
3436 list_for_each_entry(codec, &codec_list, list) {
3437 if (dev == codec->dev)
3440 mutex_unlock(&client_mutex);
3444 list_del(&codec->list);
3445 snd_soc_component_del_unlocked(&codec->component);
3446 mutex_unlock(&client_mutex);
3448 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3449 codec->component.name);
3451 snd_soc_component_cleanup(&codec->component);
3454 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3456 /* Retrieve a card's name from device tree */
3457 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3458 const char *propname)
3460 struct device_node *np;
3464 pr_err("card->dev is not set before calling %s\n", __func__);
3468 np = card->dev->of_node;
3470 ret = of_property_read_string_index(np, propname, 0, &card->name);
3472 * EINVAL means the property does not exist. This is fine providing
3473 * card->name was previously set, which is checked later in
3474 * snd_soc_register_card.
3476 if (ret < 0 && ret != -EINVAL) {
3478 "ASoC: Property '%s' could not be read: %d\n",
3485 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3487 static const struct snd_soc_dapm_widget simple_widgets[] = {
3488 SND_SOC_DAPM_MIC("Microphone", NULL),
3489 SND_SOC_DAPM_LINE("Line", NULL),
3490 SND_SOC_DAPM_HP("Headphone", NULL),
3491 SND_SOC_DAPM_SPK("Speaker", NULL),
3494 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
3495 const char *propname)
3497 struct device_node *np = card->dev->of_node;
3498 struct snd_soc_dapm_widget *widgets;
3499 const char *template, *wname;
3500 int i, j, num_widgets, ret;
3502 num_widgets = of_property_count_strings(np, propname);
3503 if (num_widgets < 0) {
3505 "ASoC: Property '%s' does not exist\n", propname);
3508 if (num_widgets & 1) {
3510 "ASoC: Property '%s' length is not even\n", propname);
3516 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3521 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3525 "ASoC: Could not allocate memory for widgets\n");
3529 for (i = 0; i < num_widgets; i++) {
3530 ret = of_property_read_string_index(np, propname,
3534 "ASoC: Property '%s' index %d read error:%d\n",
3535 propname, 2 * i, ret);
3539 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3540 if (!strncmp(template, simple_widgets[j].name,
3541 strlen(simple_widgets[j].name))) {
3542 widgets[i] = simple_widgets[j];
3547 if (j >= ARRAY_SIZE(simple_widgets)) {
3549 "ASoC: DAPM widget '%s' is not supported\n",
3554 ret = of_property_read_string_index(np, propname,
3559 "ASoC: Property '%s' index %d read error:%d\n",
3560 propname, (2 * i) + 1, ret);
3564 widgets[i].name = wname;
3567 card->of_dapm_widgets = widgets;
3568 card->num_of_dapm_widgets = num_widgets;
3572 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
3574 static int snd_soc_of_get_slot_mask(struct device_node *np,
3575 const char *prop_name,
3579 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
3585 for (i = 0; i < val; i++)
3586 if (be32_to_cpup(&of_slot_mask[i]))
3592 int snd_soc_of_parse_tdm_slot(struct device_node *np,
3593 unsigned int *tx_mask,
3594 unsigned int *rx_mask,
3595 unsigned int *slots,
3596 unsigned int *slot_width)
3602 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3604 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3606 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3607 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3615 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3616 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3626 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3628 void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
3629 struct snd_soc_codec_conf *codec_conf,
3630 struct device_node *of_node,
3631 const char *propname)
3633 struct device_node *np = card->dev->of_node;
3637 ret = of_property_read_string(np, propname, &str);
3639 /* no prefix is not error */
3643 codec_conf->of_node = of_node;
3644 codec_conf->name_prefix = str;
3646 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
3648 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3649 const char *propname)
3651 struct device_node *np = card->dev->of_node;
3653 struct snd_soc_dapm_route *routes;
3656 num_routes = of_property_count_strings(np, propname);
3657 if (num_routes < 0 || num_routes & 1) {
3659 "ASoC: Property '%s' does not exist or its length is not even\n",
3665 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3670 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3674 "ASoC: Could not allocate DAPM route table\n");
3678 for (i = 0; i < num_routes; i++) {
3679 ret = of_property_read_string_index(np, propname,
3680 2 * i, &routes[i].sink);
3683 "ASoC: Property '%s' index %d could not be read: %d\n",
3684 propname, 2 * i, ret);
3687 ret = of_property_read_string_index(np, propname,
3688 (2 * i) + 1, &routes[i].source);
3691 "ASoC: Property '%s' index %d could not be read: %d\n",
3692 propname, (2 * i) + 1, ret);
3697 card->num_of_dapm_routes = num_routes;
3698 card->of_dapm_routes = routes;
3702 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3704 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3706 struct device_node **bitclkmaster,
3707 struct device_node **framemaster)
3711 unsigned int format = 0;
3717 } of_fmt_table[] = {
3718 { "i2s", SND_SOC_DAIFMT_I2S },
3719 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3720 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3721 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3722 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3723 { "ac97", SND_SOC_DAIFMT_AC97 },
3724 { "pdm", SND_SOC_DAIFMT_PDM},
3725 { "msb", SND_SOC_DAIFMT_MSB },
3726 { "lsb", SND_SOC_DAIFMT_LSB },
3733 * check "dai-format = xxx"
3734 * or "[prefix]format = xxx"
3735 * SND_SOC_DAIFMT_FORMAT_MASK area
3737 ret = of_property_read_string(np, "dai-format", &str);
3739 snprintf(prop, sizeof(prop), "%sformat", prefix);
3740 ret = of_property_read_string(np, prop, &str);
3743 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3744 if (strcmp(str, of_fmt_table[i].name) == 0) {
3745 format |= of_fmt_table[i].val;
3752 * check "[prefix]continuous-clock"
3753 * SND_SOC_DAIFMT_CLOCK_MASK area
3755 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
3756 if (of_property_read_bool(np, prop))
3757 format |= SND_SOC_DAIFMT_CONT;
3759 format |= SND_SOC_DAIFMT_GATED;
3762 * check "[prefix]bitclock-inversion"
3763 * check "[prefix]frame-inversion"
3764 * SND_SOC_DAIFMT_INV_MASK area
3766 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3767 bit = !!of_get_property(np, prop, NULL);
3769 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3770 frame = !!of_get_property(np, prop, NULL);
3772 switch ((bit << 4) + frame) {
3774 format |= SND_SOC_DAIFMT_IB_IF;
3777 format |= SND_SOC_DAIFMT_IB_NF;
3780 format |= SND_SOC_DAIFMT_NB_IF;
3783 /* SND_SOC_DAIFMT_NB_NF is default */
3788 * check "[prefix]bitclock-master"
3789 * check "[prefix]frame-master"
3790 * SND_SOC_DAIFMT_MASTER_MASK area
3792 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3793 bit = !!of_get_property(np, prop, NULL);
3794 if (bit && bitclkmaster)
3795 *bitclkmaster = of_parse_phandle(np, prop, 0);
3797 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3798 frame = !!of_get_property(np, prop, NULL);
3799 if (frame && framemaster)
3800 *framemaster = of_parse_phandle(np, prop, 0);
3802 switch ((bit << 4) + frame) {
3804 format |= SND_SOC_DAIFMT_CBM_CFM;
3807 format |= SND_SOC_DAIFMT_CBM_CFS;
3810 format |= SND_SOC_DAIFMT_CBS_CFM;
3813 format |= SND_SOC_DAIFMT_CBS_CFS;
3819 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3821 int snd_soc_get_dai_id(struct device_node *ep)
3823 struct snd_soc_component *pos;
3824 struct device_node *node;
3827 node = of_graph_get_port_parent(ep);
3830 * For example HDMI case, HDMI has video/sound port,
3831 * but ALSA SoC needs sound port number only.
3832 * Thus counting HDMI DT port/endpoint doesn't work.
3833 * Then, it should have .of_xlate_dai_id
3836 mutex_lock(&client_mutex);
3837 list_for_each_entry(pos, &component_list, list) {
3838 struct device_node *component_of_node = pos->dev->of_node;
3840 if (!component_of_node && pos->dev->parent)
3841 component_of_node = pos->dev->parent->of_node;
3843 if (component_of_node != node)
3846 if (pos->driver->of_xlate_dai_id)
3847 ret = pos->driver->of_xlate_dai_id(pos, ep);
3851 mutex_unlock(&client_mutex);
3857 EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3859 int snd_soc_get_dai_name(struct of_phandle_args *args,
3860 const char **dai_name)
3862 struct snd_soc_component *pos;
3863 struct device_node *component_of_node;
3864 int ret = -EPROBE_DEFER;
3866 mutex_lock(&client_mutex);
3867 list_for_each_entry(pos, &component_list, list) {
3868 component_of_node = pos->dev->of_node;
3869 if (!component_of_node && pos->dev->parent)
3870 component_of_node = pos->dev->parent->of_node;
3872 if (component_of_node != args->np)
3875 if (pos->driver->of_xlate_dai_name) {
3876 ret = pos->driver->of_xlate_dai_name(pos,
3880 struct snd_soc_dai *dai;
3883 switch (args->args_count) {
3885 id = 0; /* same as dai_drv[0] */
3895 if (id < 0 || id >= pos->num_dai) {
3902 /* find target DAI */
3903 list_for_each_entry(dai, &pos->dai_list, list) {
3909 *dai_name = dai->driver->name;
3911 *dai_name = pos->name;
3916 mutex_unlock(&client_mutex);
3919 EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
3921 int snd_soc_of_get_dai_name(struct device_node *of_node,
3922 const char **dai_name)
3924 struct of_phandle_args args;
3927 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3928 "#sound-dai-cells", 0, &args);
3932 ret = snd_soc_get_dai_name(&args, dai_name);
3934 of_node_put(args.np);
3938 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3941 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3942 * @dai_link: DAI link
3944 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3946 void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3948 struct snd_soc_dai_link_component *component = dai_link->codecs;
3951 for (index = 0; index < dai_link->num_codecs; index++, component++) {
3952 if (!component->of_node)
3954 of_node_put(component->of_node);
3955 component->of_node = NULL;
3958 EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3961 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3963 * @of_node: Device node
3964 * @dai_link: DAI link
3966 * Builds an array of CODEC DAI components from the DAI link property
3968 * The array is set in the DAI link and the number of DAIs is set accordingly.
3969 * The device nodes in the array (of_node) must be dereferenced by calling
3970 * snd_soc_of_put_dai_link_codecs() on @dai_link.
3972 * Returns 0 for success
3974 int snd_soc_of_get_dai_link_codecs(struct device *dev,
3975 struct device_node *of_node,
3976 struct snd_soc_dai_link *dai_link)
3978 struct of_phandle_args args;
3979 struct snd_soc_dai_link_component *component;
3981 int index, num_codecs, ret;
3983 /* Count the number of CODECs */
3985 num_codecs = of_count_phandle_with_args(of_node, name,
3986 "#sound-dai-cells");
3987 if (num_codecs <= 0) {
3988 if (num_codecs == -ENOENT)
3989 dev_err(dev, "No 'sound-dai' property\n");
3991 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3994 component = devm_kzalloc(dev,
3995 sizeof *component * num_codecs,
3999 dai_link->codecs = component;
4000 dai_link->num_codecs = num_codecs;
4002 /* Parse the list */
4003 for (index = 0, component = dai_link->codecs;
4004 index < dai_link->num_codecs;
4005 index++, component++) {
4006 ret = of_parse_phandle_with_args(of_node, name,
4011 component->of_node = args.np;
4012 ret = snd_soc_get_dai_name(&args, &component->dai_name);
4018 snd_soc_of_put_dai_link_codecs(dai_link);
4019 dai_link->codecs = NULL;
4020 dai_link->num_codecs = 0;
4023 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
4025 static int __init snd_soc_init(void)
4027 snd_soc_debugfs_init();
4028 snd_soc_util_init();
4030 return platform_driver_register(&soc_driver);
4032 module_init(snd_soc_init);
4034 static void __exit snd_soc_exit(void)
4036 snd_soc_util_exit();
4037 snd_soc_debugfs_exit();
4039 platform_driver_unregister(&soc_driver);
4041 module_exit(snd_soc_exit);
4043 /* Module information */
4044 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4045 MODULE_DESCRIPTION("ALSA SoC Core");
4046 MODULE_LICENSE("GPL");
4047 MODULE_ALIAS("platform:soc-audio");