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 /* returns the minimum number of bytes needed to represent
86 * a particular given value */
87 static int min_bytes_needed(unsigned long val)
92 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
95 c = (sizeof val * 8) - c;
103 /* fill buf which is 'len' bytes with a formatted
104 * string of the form 'reg: value\n' */
105 static int format_register_str(struct snd_soc_codec *codec,
106 unsigned int reg, char *buf, size_t len)
108 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
109 int regsize = codec->driver->reg_word_size * 2;
112 /* +2 for ': ' and + 1 for '\n' */
113 if (wordsize + regsize + 2 + 1 != len)
116 sprintf(buf, "%.*x: ", wordsize, reg);
119 ret = snd_soc_read(codec, reg);
121 memset(buf, 'X', regsize);
123 sprintf(buf, "%.*x", regsize, ret);
125 /* no NUL-termination needed */
129 /* codec register dump */
130 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
131 size_t count, loff_t pos)
134 int wordsize, regsize;
139 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
140 regsize = codec->driver->reg_word_size * 2;
142 len = wordsize + regsize + 2 + 1;
144 if (!codec->driver->reg_cache_size)
147 if (codec->driver->reg_cache_step)
148 step = codec->driver->reg_cache_step;
150 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
151 /* only support larger than PAGE_SIZE bytes debugfs
152 * entries for the default case */
154 if (total + len >= count - 1)
156 format_register_str(codec, i, buf + total, len);
162 total = min(total, count - 1);
167 static ssize_t codec_reg_show(struct device *dev,
168 struct device_attribute *attr, char *buf)
170 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
172 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
175 static DEVICE_ATTR_RO(codec_reg);
177 static ssize_t pmdown_time_show(struct device *dev,
178 struct device_attribute *attr, char *buf)
180 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
182 return sprintf(buf, "%ld\n", rtd->pmdown_time);
185 static ssize_t pmdown_time_set(struct device *dev,
186 struct device_attribute *attr,
187 const char *buf, size_t count)
189 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
192 ret = kstrtol(buf, 10, &rtd->pmdown_time);
199 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
201 static struct attribute *soc_dev_attrs[] = {
202 &dev_attr_codec_reg.attr,
203 &dev_attr_pmdown_time.attr,
207 static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
208 struct attribute *attr, int idx)
210 struct device *dev = kobj_to_dev(kobj);
211 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
213 if (attr == &dev_attr_pmdown_time.attr)
214 return attr->mode; /* always visible */
215 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
218 static const struct attribute_group soc_dapm_dev_group = {
219 .attrs = soc_dapm_dev_attrs,
220 .is_visible = soc_dev_attr_is_visible,
223 static const struct attribute_group soc_dev_group = {
224 .attrs = soc_dev_attrs,
225 .is_visible = soc_dev_attr_is_visible,
228 static const struct attribute_group *soc_dev_attr_groups[] = {
234 #ifdef CONFIG_DEBUG_FS
235 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
236 size_t count, loff_t *ppos)
239 struct snd_soc_codec *codec = file->private_data;
242 if (*ppos < 0 || !count)
245 buf = kmalloc(count, GFP_KERNEL);
249 ret = soc_codec_reg_show(codec, buf, count, *ppos);
251 if (copy_to_user(user_buf, buf, ret)) {
262 static ssize_t codec_reg_write_file(struct file *file,
263 const char __user *user_buf, size_t count, loff_t *ppos)
268 unsigned long reg, value;
269 struct snd_soc_codec *codec = file->private_data;
272 buf_size = min(count, (sizeof(buf)-1));
273 if (copy_from_user(buf, user_buf, buf_size))
277 while (*start == ' ')
279 reg = simple_strtoul(start, &start, 16);
280 while (*start == ' ')
282 ret = kstrtoul(start, 16, &value);
286 /* Userspace has been fiddling around behind the kernel's back */
287 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
289 snd_soc_write(codec, reg, value);
293 static const struct file_operations codec_reg_fops = {
295 .read = codec_reg_read_file,
296 .write = codec_reg_write_file,
297 .llseek = default_llseek,
300 static void soc_init_component_debugfs(struct snd_soc_component *component)
302 if (!component->card->debugfs_card_root)
305 if (component->debugfs_prefix) {
308 name = kasprintf(GFP_KERNEL, "%s:%s",
309 component->debugfs_prefix, component->name);
311 component->debugfs_root = debugfs_create_dir(name,
312 component->card->debugfs_card_root);
316 component->debugfs_root = debugfs_create_dir(component->name,
317 component->card->debugfs_card_root);
320 if (!component->debugfs_root) {
321 dev_warn(component->dev,
322 "ASoC: Failed to create component debugfs directory\n");
326 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
327 component->debugfs_root);
329 if (component->init_debugfs)
330 component->init_debugfs(component);
333 static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
335 debugfs_remove_recursive(component->debugfs_root);
338 static void soc_init_codec_debugfs(struct snd_soc_component *component)
340 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
341 struct dentry *debugfs_reg;
343 debugfs_reg = debugfs_create_file("codec_reg", 0644,
344 codec->component.debugfs_root,
345 codec, &codec_reg_fops);
348 "ASoC: Failed to create codec register debugfs file\n");
351 static int codec_list_show(struct seq_file *m, void *v)
353 struct snd_soc_codec *codec;
355 mutex_lock(&client_mutex);
357 list_for_each_entry(codec, &codec_list, list)
358 seq_printf(m, "%s\n", codec->component.name);
360 mutex_unlock(&client_mutex);
364 DEFINE_SHOW_ATTRIBUTE(codec_list);
366 static int dai_list_show(struct seq_file *m, void *v)
368 struct snd_soc_component *component;
369 struct snd_soc_dai *dai;
371 mutex_lock(&client_mutex);
373 list_for_each_entry(component, &component_list, list)
374 list_for_each_entry(dai, &component->dai_list, list)
375 seq_printf(m, "%s\n", dai->name);
377 mutex_unlock(&client_mutex);
381 DEFINE_SHOW_ATTRIBUTE(dai_list);
383 static void soc_init_card_debugfs(struct snd_soc_card *card)
385 if (!snd_soc_debugfs_root)
388 card->debugfs_card_root = debugfs_create_dir(card->name,
389 snd_soc_debugfs_root);
390 if (!card->debugfs_card_root) {
392 "ASoC: Failed to create card debugfs directory\n");
396 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
397 card->debugfs_card_root,
399 if (!card->debugfs_pop_time)
401 "ASoC: Failed to create pop time debugfs file\n");
404 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
406 debugfs_remove_recursive(card->debugfs_card_root);
409 static void snd_soc_debugfs_init(void)
411 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
412 if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) {
413 pr_warn("ASoC: Failed to create debugfs directory\n");
414 snd_soc_debugfs_root = NULL;
418 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
420 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
422 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
424 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
427 static void snd_soc_debugfs_exit(void)
429 debugfs_remove_recursive(snd_soc_debugfs_root);
434 #define soc_init_codec_debugfs NULL
436 static inline void soc_init_component_debugfs(
437 struct snd_soc_component *component)
441 static inline void soc_cleanup_component_debugfs(
442 struct snd_soc_component *component)
446 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
450 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
454 static inline void snd_soc_debugfs_init(void)
458 static inline void snd_soc_debugfs_exit(void)
464 static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
465 struct snd_soc_component *component)
467 struct snd_soc_rtdcom_list *rtdcom;
468 struct snd_soc_rtdcom_list *new_rtdcom;
470 for_each_rtdcom(rtd, rtdcom) {
471 /* already connected */
472 if (rtdcom->component == component)
476 new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
480 new_rtdcom->component = component;
481 INIT_LIST_HEAD(&new_rtdcom->list);
483 list_add_tail(&new_rtdcom->list, &rtd->component_list);
488 static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
490 struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
492 for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
495 INIT_LIST_HEAD(&rtd->component_list);
498 struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
499 const char *driver_name)
501 struct snd_soc_rtdcom_list *rtdcom;
506 for_each_rtdcom(rtd, rtdcom) {
507 const char *component_name = rtdcom->component->driver->name;
512 if ((component_name == driver_name) ||
513 strcmp(component_name, driver_name) == 0)
514 return rtdcom->component;
519 EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
521 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
522 const char *dai_link, int stream)
524 struct snd_soc_pcm_runtime *rtd;
526 list_for_each_entry(rtd, &card->rtd_list, list) {
527 if (rtd->dai_link->no_pcm &&
528 !strcmp(rtd->dai_link->name, dai_link))
529 return rtd->pcm->streams[stream].substream;
531 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
534 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
536 static const struct snd_soc_ops null_snd_soc_ops;
538 static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
539 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
541 struct snd_soc_pcm_runtime *rtd;
543 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
547 INIT_LIST_HEAD(&rtd->component_list);
549 rtd->dai_link = dai_link;
550 if (!rtd->dai_link->ops)
551 rtd->dai_link->ops = &null_snd_soc_ops;
553 rtd->codec_dais = kzalloc(sizeof(struct snd_soc_dai *) *
554 dai_link->num_codecs,
556 if (!rtd->codec_dais) {
564 static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
566 kfree(rtd->codec_dais);
567 snd_soc_rtdcom_del_all(rtd);
571 static void soc_add_pcm_runtime(struct snd_soc_card *card,
572 struct snd_soc_pcm_runtime *rtd)
574 list_add_tail(&rtd->list, &card->rtd_list);
575 rtd->num = card->num_rtd;
579 static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
581 struct snd_soc_pcm_runtime *rtd, *_rtd;
583 list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) {
584 list_del(&rtd->list);
585 soc_free_pcm_runtime(rtd);
591 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
592 const char *dai_link)
594 struct snd_soc_pcm_runtime *rtd;
596 list_for_each_entry(rtd, &card->rtd_list, list) {
597 if (!strcmp(rtd->dai_link->name, dai_link))
600 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
603 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
605 static void codec2codec_close_delayed_work(struct work_struct *work)
607 /* Currently nothing to do for c2c links
608 * Since c2c links are internal nodes in the DAPM graph and
609 * don't interface with the outside world or application layer
610 * we don't have to do any special handling on close.
614 #ifdef CONFIG_PM_SLEEP
615 /* powers down audio subsystem for suspend */
616 int snd_soc_suspend(struct device *dev)
618 struct snd_soc_card *card = dev_get_drvdata(dev);
619 struct snd_soc_component *component;
620 struct snd_soc_pcm_runtime *rtd;
623 /* If the card is not initialized yet there is nothing to do */
624 if (!card->instantiated)
627 /* Due to the resume being scheduled into a workqueue we could
628 * suspend before that's finished - wait for it to complete.
630 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
632 /* we're going to block userspace touching us until resume completes */
633 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
635 /* mute any active DACs */
636 list_for_each_entry(rtd, &card->rtd_list, list) {
638 if (rtd->dai_link->ignore_suspend)
641 for (i = 0; i < rtd->num_codecs; i++) {
642 struct snd_soc_dai *dai = rtd->codec_dais[i];
643 struct snd_soc_dai_driver *drv = dai->driver;
645 if (drv->ops->digital_mute && dai->playback_active)
646 drv->ops->digital_mute(dai, 1);
650 /* suspend all pcms */
651 list_for_each_entry(rtd, &card->rtd_list, list) {
652 if (rtd->dai_link->ignore_suspend)
655 snd_pcm_suspend_all(rtd->pcm);
658 if (card->suspend_pre)
659 card->suspend_pre(card);
661 list_for_each_entry(rtd, &card->rtd_list, list) {
662 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
664 if (rtd->dai_link->ignore_suspend)
667 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
668 cpu_dai->driver->suspend(cpu_dai);
671 /* close any waiting streams */
672 list_for_each_entry(rtd, &card->rtd_list, list)
673 flush_delayed_work(&rtd->delayed_work);
675 list_for_each_entry(rtd, &card->rtd_list, list) {
677 if (rtd->dai_link->ignore_suspend)
680 snd_soc_dapm_stream_event(rtd,
681 SNDRV_PCM_STREAM_PLAYBACK,
682 SND_SOC_DAPM_STREAM_SUSPEND);
684 snd_soc_dapm_stream_event(rtd,
685 SNDRV_PCM_STREAM_CAPTURE,
686 SND_SOC_DAPM_STREAM_SUSPEND);
689 /* Recheck all endpoints too, their state is affected by suspend */
690 dapm_mark_endpoints_dirty(card);
691 snd_soc_dapm_sync(&card->dapm);
693 /* suspend all COMPONENTs */
694 list_for_each_entry(component, &card->component_dev_list, card_list) {
695 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
697 /* If there are paths active then the COMPONENT will be held with
698 * bias _ON and should not be suspended. */
699 if (!component->suspended) {
700 switch (snd_soc_dapm_get_bias_level(dapm)) {
701 case SND_SOC_BIAS_STANDBY:
703 * If the COMPONENT is capable of idle
704 * bias off then being in STANDBY
705 * means it's doing something,
706 * otherwise fall through.
708 if (dapm->idle_bias_off) {
709 dev_dbg(component->dev,
710 "ASoC: idle_bias_off CODEC on over suspend\n");
714 case SND_SOC_BIAS_OFF:
715 if (component->suspend)
716 component->suspend(component);
717 component->suspended = 1;
718 if (component->regmap)
719 regcache_mark_dirty(component->regmap);
720 /* deactivate pins to sleep state */
721 pinctrl_pm_select_sleep_state(component->dev);
724 dev_dbg(component->dev,
725 "ASoC: COMPONENT is on over suspend\n");
731 list_for_each_entry(rtd, &card->rtd_list, list) {
732 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
734 if (rtd->dai_link->ignore_suspend)
737 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
738 cpu_dai->driver->suspend(cpu_dai);
740 /* deactivate pins to sleep state */
741 pinctrl_pm_select_sleep_state(cpu_dai->dev);
744 if (card->suspend_post)
745 card->suspend_post(card);
749 EXPORT_SYMBOL_GPL(snd_soc_suspend);
751 /* deferred resume work, so resume can complete before we finished
752 * setting our codec back up, which can be very slow on I2C
754 static void soc_resume_deferred(struct work_struct *work)
756 struct snd_soc_card *card =
757 container_of(work, struct snd_soc_card, deferred_resume_work);
758 struct snd_soc_pcm_runtime *rtd;
759 struct snd_soc_component *component;
762 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
763 * so userspace apps are blocked from touching us
766 dev_dbg(card->dev, "ASoC: starting resume work\n");
768 /* Bring us up into D2 so that DAPM starts enabling things */
769 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
771 if (card->resume_pre)
772 card->resume_pre(card);
774 /* resume control bus DAIs */
775 list_for_each_entry(rtd, &card->rtd_list, list) {
776 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
778 if (rtd->dai_link->ignore_suspend)
781 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
782 cpu_dai->driver->resume(cpu_dai);
785 list_for_each_entry(component, &card->component_dev_list, card_list) {
786 if (component->suspended) {
787 if (component->resume)
788 component->resume(component);
789 component->suspended = 0;
793 list_for_each_entry(rtd, &card->rtd_list, list) {
795 if (rtd->dai_link->ignore_suspend)
798 snd_soc_dapm_stream_event(rtd,
799 SNDRV_PCM_STREAM_PLAYBACK,
800 SND_SOC_DAPM_STREAM_RESUME);
802 snd_soc_dapm_stream_event(rtd,
803 SNDRV_PCM_STREAM_CAPTURE,
804 SND_SOC_DAPM_STREAM_RESUME);
807 /* unmute any active DACs */
808 list_for_each_entry(rtd, &card->rtd_list, list) {
810 if (rtd->dai_link->ignore_suspend)
813 for (i = 0; i < rtd->num_codecs; i++) {
814 struct snd_soc_dai *dai = rtd->codec_dais[i];
815 struct snd_soc_dai_driver *drv = dai->driver;
817 if (drv->ops->digital_mute && dai->playback_active)
818 drv->ops->digital_mute(dai, 0);
822 list_for_each_entry(rtd, &card->rtd_list, list) {
823 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
825 if (rtd->dai_link->ignore_suspend)
828 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
829 cpu_dai->driver->resume(cpu_dai);
832 if (card->resume_post)
833 card->resume_post(card);
835 dev_dbg(card->dev, "ASoC: resume work completed\n");
837 /* Recheck all endpoints too, their state is affected by suspend */
838 dapm_mark_endpoints_dirty(card);
839 snd_soc_dapm_sync(&card->dapm);
841 /* userspace can access us now we are back as we were before */
842 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
845 /* powers up audio subsystem after a suspend */
846 int snd_soc_resume(struct device *dev)
848 struct snd_soc_card *card = dev_get_drvdata(dev);
849 bool bus_control = false;
850 struct snd_soc_pcm_runtime *rtd;
852 /* If the card is not initialized yet there is nothing to do */
853 if (!card->instantiated)
856 /* activate pins from sleep state */
857 list_for_each_entry(rtd, &card->rtd_list, list) {
858 struct snd_soc_dai **codec_dais = rtd->codec_dais;
859 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
863 pinctrl_pm_select_default_state(cpu_dai->dev);
865 for (j = 0; j < rtd->num_codecs; j++) {
866 struct snd_soc_dai *codec_dai = codec_dais[j];
867 if (codec_dai->active)
868 pinctrl_pm_select_default_state(codec_dai->dev);
873 * DAIs that also act as the control bus master might have other drivers
874 * hanging off them so need to resume immediately. Other drivers don't
875 * have that problem and may take a substantial amount of time to resume
876 * due to I/O costs and anti-pop so handle them out of line.
878 list_for_each_entry(rtd, &card->rtd_list, list) {
879 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
880 bus_control |= cpu_dai->driver->bus_control;
883 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
884 soc_resume_deferred(&card->deferred_resume_work);
886 dev_dbg(dev, "ASoC: Scheduling resume work\n");
887 if (!schedule_work(&card->deferred_resume_work))
888 dev_err(dev, "ASoC: resume work item may be lost\n");
893 EXPORT_SYMBOL_GPL(snd_soc_resume);
895 #define snd_soc_suspend NULL
896 #define snd_soc_resume NULL
899 static const struct snd_soc_dai_ops null_dai_ops = {
902 static struct snd_soc_component *soc_find_component(
903 const struct device_node *of_node, const char *name)
905 struct snd_soc_component *component;
907 lockdep_assert_held(&client_mutex);
909 list_for_each_entry(component, &component_list, list) {
911 if (component->dev->of_node == of_node)
913 } else if (strcmp(component->name, name) == 0) {
922 * snd_soc_find_dai - Find a registered DAI
924 * @dlc: name of the DAI or the DAI driver and optional component info to match
926 * This function will search all registered components and their DAIs to
927 * find the DAI of the same name. The component's of_node and name
928 * should also match if being specified.
930 * Return: pointer of DAI, or NULL if not found.
932 struct snd_soc_dai *snd_soc_find_dai(
933 const struct snd_soc_dai_link_component *dlc)
935 struct snd_soc_component *component;
936 struct snd_soc_dai *dai;
937 struct device_node *component_of_node;
939 lockdep_assert_held(&client_mutex);
941 /* Find CPU DAI from registered DAIs*/
942 list_for_each_entry(component, &component_list, list) {
943 component_of_node = component->dev->of_node;
944 if (!component_of_node && component->dev->parent)
945 component_of_node = component->dev->parent->of_node;
947 if (dlc->of_node && component_of_node != dlc->of_node)
949 if (dlc->name && strcmp(component->name, dlc->name))
951 list_for_each_entry(dai, &component->dai_list, list) {
952 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
953 && (!dai->driver->name
954 || strcmp(dai->driver->name, dlc->dai_name)))
963 EXPORT_SYMBOL_GPL(snd_soc_find_dai);
967 * snd_soc_find_dai_link - Find a DAI link
970 * @id: DAI link ID to match
971 * @name: DAI link name to match, optional
972 * @stream_name: DAI link stream name to match, optional
974 * This function will search all existing DAI links of the soc card to
975 * find the link of the same ID. Since DAI links may not have their
976 * unique ID, so name and stream name should also match if being
979 * Return: pointer of DAI link, or NULL if not found.
981 struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
982 int id, const char *name,
983 const char *stream_name)
985 struct snd_soc_dai_link *link, *_link;
987 lockdep_assert_held(&client_mutex);
989 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
993 if (name && (!link->name || strcmp(name, link->name)))
996 if (stream_name && (!link->stream_name
997 || strcmp(stream_name, link->stream_name)))
1005 EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
1007 static bool soc_is_dai_link_bound(struct snd_soc_card *card,
1008 struct snd_soc_dai_link *dai_link)
1010 struct snd_soc_pcm_runtime *rtd;
1012 list_for_each_entry(rtd, &card->rtd_list, list) {
1013 if (rtd->dai_link == dai_link)
1020 static int soc_bind_dai_link(struct snd_soc_card *card,
1021 struct snd_soc_dai_link *dai_link)
1023 struct snd_soc_pcm_runtime *rtd;
1024 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
1025 struct snd_soc_dai_link_component cpu_dai_component;
1026 struct snd_soc_component *component;
1027 struct snd_soc_dai **codec_dais;
1028 struct device_node *platform_of_node;
1029 const char *platform_name;
1032 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
1034 if (soc_is_dai_link_bound(card, dai_link)) {
1035 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
1040 rtd = soc_new_pcm_runtime(card, dai_link);
1044 cpu_dai_component.name = dai_link->cpu_name;
1045 cpu_dai_component.of_node = dai_link->cpu_of_node;
1046 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
1047 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
1048 if (!rtd->cpu_dai) {
1049 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
1050 dai_link->cpu_dai_name);
1053 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
1055 rtd->num_codecs = dai_link->num_codecs;
1057 /* Find CODEC from registered CODECs */
1058 codec_dais = rtd->codec_dais;
1059 for (i = 0; i < rtd->num_codecs; i++) {
1060 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
1061 if (!codec_dais[i]) {
1062 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
1063 codecs[i].dai_name);
1066 snd_soc_rtdcom_add(rtd, codec_dais[i]->component);
1069 /* Single codec links expect codec and codec_dai in runtime data */
1070 rtd->codec_dai = codec_dais[0];
1071 rtd->codec = rtd->codec_dai->codec;
1073 /* if there's no platform we match on the empty platform */
1074 platform_name = dai_link->platform_name;
1075 if (!platform_name && !dai_link->platform_of_node)
1076 platform_name = "snd-soc-dummy";
1078 /* find one from the set of registered platforms */
1079 list_for_each_entry(component, &component_list, list) {
1080 platform_of_node = component->dev->of_node;
1081 if (!platform_of_node && component->dev->parent->of_node)
1082 platform_of_node = component->dev->parent->of_node;
1084 if (dai_link->platform_of_node) {
1085 if (platform_of_node != dai_link->platform_of_node)
1088 if (strcmp(component->name, platform_name))
1092 snd_soc_rtdcom_add(rtd, component);
1095 soc_add_pcm_runtime(card, rtd);
1099 soc_free_pcm_runtime(rtd);
1100 return -EPROBE_DEFER;
1103 static void soc_remove_component(struct snd_soc_component *component)
1105 if (!component->card)
1108 list_del(&component->card_list);
1110 if (component->remove)
1111 component->remove(component);
1113 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1115 soc_cleanup_component_debugfs(component);
1116 component->card = NULL;
1117 module_put(component->dev->driver->owner);
1120 static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1124 if (dai && dai->probed &&
1125 dai->driver->remove_order == order) {
1126 if (dai->driver->remove) {
1127 err = dai->driver->remove(dai);
1130 "ASoC: failed to remove %s: %d\n",
1137 static void soc_remove_link_dais(struct snd_soc_card *card,
1138 struct snd_soc_pcm_runtime *rtd, int order)
1142 /* unregister the rtd device */
1143 if (rtd->dev_registered) {
1144 device_unregister(rtd->dev);
1145 rtd->dev_registered = 0;
1148 /* remove the CODEC DAI */
1149 for (i = 0; i < rtd->num_codecs; i++)
1150 soc_remove_dai(rtd->codec_dais[i], order);
1152 soc_remove_dai(rtd->cpu_dai, order);
1155 static void soc_remove_link_components(struct snd_soc_card *card,
1156 struct snd_soc_pcm_runtime *rtd, int order)
1158 struct snd_soc_component *component;
1159 struct snd_soc_rtdcom_list *rtdcom;
1161 for_each_rtdcom(rtd, rtdcom) {
1162 component = rtdcom->component;
1164 if (component->driver->remove_order == order)
1165 soc_remove_component(component);
1169 static void soc_remove_dai_links(struct snd_soc_card *card)
1172 struct snd_soc_pcm_runtime *rtd;
1173 struct snd_soc_dai_link *link, *_link;
1175 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1177 list_for_each_entry(rtd, &card->rtd_list, list)
1178 soc_remove_link_dais(card, rtd, order);
1181 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1183 list_for_each_entry(rtd, &card->rtd_list, list)
1184 soc_remove_link_components(card, rtd, order);
1187 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1188 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1189 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1192 list_del(&link->list);
1193 card->num_dai_links--;
1197 static int snd_soc_init_multicodec(struct snd_soc_card *card,
1198 struct snd_soc_dai_link *dai_link)
1200 /* Legacy codec/codec_dai link is a single entry in multicodec */
1201 if (dai_link->codec_name || dai_link->codec_of_node ||
1202 dai_link->codec_dai_name) {
1203 dai_link->num_codecs = 1;
1205 dai_link->codecs = devm_kzalloc(card->dev,
1206 sizeof(struct snd_soc_dai_link_component),
1208 if (!dai_link->codecs)
1211 dai_link->codecs[0].name = dai_link->codec_name;
1212 dai_link->codecs[0].of_node = dai_link->codec_of_node;
1213 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1216 if (!dai_link->codecs) {
1217 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1224 static int soc_init_dai_link(struct snd_soc_card *card,
1225 struct snd_soc_dai_link *link)
1229 ret = snd_soc_init_multicodec(card, link);
1231 dev_err(card->dev, "ASoC: failed to init multicodec\n");
1235 for (i = 0; i < link->num_codecs; i++) {
1237 * Codec must be specified by 1 of name or OF node,
1238 * not both or neither.
1240 if (!!link->codecs[i].name ==
1241 !!link->codecs[i].of_node) {
1242 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1246 /* Codec DAI name must be specified */
1247 if (!link->codecs[i].dai_name) {
1248 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1255 * Platform may be specified by either name or OF node, but
1256 * can be left unspecified, and a dummy platform will be used.
1258 if (link->platform_name && link->platform_of_node) {
1260 "ASoC: Both platform name/of_node are set for %s\n",
1266 * CPU device may be specified by either name or OF node, but
1267 * can be left unspecified, and will be matched based on DAI
1270 if (link->cpu_name && link->cpu_of_node) {
1272 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1277 * At least one of CPU DAI name or CPU device name/node must be
1280 if (!link->cpu_dai_name &&
1281 !(link->cpu_name || link->cpu_of_node)) {
1283 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1291 void snd_soc_disconnect_sync(struct device *dev)
1293 struct snd_soc_component *component = snd_soc_lookup_component(dev, NULL);
1295 if (!component || !component->card)
1298 snd_card_disconnect_sync(component->card->snd_card);
1300 EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
1303 * snd_soc_add_dai_link - Add a DAI link dynamically
1304 * @card: The ASoC card to which the DAI link is added
1305 * @dai_link: The new DAI link to add
1307 * This function adds a DAI link to the ASoC card's link list.
1309 * Note: Topology can use this API to add DAI links when probing the
1310 * topology component. And machine drivers can still define static
1311 * DAI links in dai_link array.
1313 int snd_soc_add_dai_link(struct snd_soc_card *card,
1314 struct snd_soc_dai_link *dai_link)
1316 if (dai_link->dobj.type
1317 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1318 dev_err(card->dev, "Invalid dai link type %d\n",
1319 dai_link->dobj.type);
1323 lockdep_assert_held(&client_mutex);
1324 /* Notify the machine driver for extra initialization
1325 * on the link created by topology.
1327 if (dai_link->dobj.type && card->add_dai_link)
1328 card->add_dai_link(card, dai_link);
1330 list_add_tail(&dai_link->list, &card->dai_link_list);
1331 card->num_dai_links++;
1335 EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1338 * snd_soc_remove_dai_link - Remove a DAI link from the list
1339 * @card: The ASoC card that owns the link
1340 * @dai_link: The DAI link to remove
1342 * This function removes a DAI link from the ASoC card's link list.
1344 * For DAI links previously added by topology, topology should
1345 * remove them by using the dobj embedded in the link.
1347 void snd_soc_remove_dai_link(struct snd_soc_card *card,
1348 struct snd_soc_dai_link *dai_link)
1350 struct snd_soc_dai_link *link, *_link;
1352 if (dai_link->dobj.type
1353 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1354 dev_err(card->dev, "Invalid dai link type %d\n",
1355 dai_link->dobj.type);
1359 lockdep_assert_held(&client_mutex);
1360 /* Notify the machine driver for extra destruction
1361 * on the link created by topology.
1363 if (dai_link->dobj.type && card->remove_dai_link)
1364 card->remove_dai_link(card, dai_link);
1366 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1367 if (link == dai_link) {
1368 list_del(&link->list);
1369 card->num_dai_links--;
1374 EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1376 static void soc_set_name_prefix(struct snd_soc_card *card,
1377 struct snd_soc_component *component)
1381 if (card->codec_conf == NULL)
1384 for (i = 0; i < card->num_configs; i++) {
1385 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1386 struct device_node *component_of_node = component->dev->of_node;
1388 if (!component_of_node && component->dev->parent)
1389 component_of_node = component->dev->parent->of_node;
1391 if (map->of_node && component_of_node != map->of_node)
1393 if (map->dev_name && strcmp(component->name, map->dev_name))
1395 component->name_prefix = map->name_prefix;
1400 static int soc_probe_component(struct snd_soc_card *card,
1401 struct snd_soc_component *component)
1403 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1404 struct snd_soc_dai *dai;
1407 if (!strcmp(component->name, "snd-soc-dummy"))
1410 if (component->card) {
1411 if (component->card != card) {
1412 dev_err(component->dev,
1413 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1414 card->name, component->card->name);
1420 if (!try_module_get(component->dev->driver->owner))
1423 component->card = card;
1425 soc_set_name_prefix(card, component);
1427 soc_init_component_debugfs(component);
1429 if (component->driver->dapm_widgets) {
1430 ret = snd_soc_dapm_new_controls(dapm,
1431 component->driver->dapm_widgets,
1432 component->driver->num_dapm_widgets);
1435 dev_err(component->dev,
1436 "Failed to create new controls %d\n", ret);
1441 list_for_each_entry(dai, &component->dai_list, list) {
1442 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1444 dev_err(component->dev,
1445 "Failed to create DAI widgets %d\n", ret);
1450 if (component->probe) {
1451 ret = component->probe(component);
1453 dev_err(component->dev,
1454 "ASoC: failed to probe component %d\n", ret);
1458 WARN(dapm->idle_bias_off &&
1459 dapm->bias_level != SND_SOC_BIAS_OFF,
1460 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1464 /* machine specific init */
1465 if (component->init) {
1466 ret = component->init(component);
1468 dev_err(component->dev,
1469 "Failed to do machine specific init %d\n", ret);
1474 if (component->driver->controls)
1475 snd_soc_add_component_controls(component,
1476 component->driver->controls,
1477 component->driver->num_controls);
1478 if (component->driver->dapm_routes)
1479 snd_soc_dapm_add_routes(dapm,
1480 component->driver->dapm_routes,
1481 component->driver->num_dapm_routes);
1483 list_add(&dapm->list, &card->dapm_list);
1484 list_add(&component->card_list, &card->component_dev_list);
1489 soc_cleanup_component_debugfs(component);
1490 component->card = NULL;
1491 module_put(component->dev->driver->owner);
1496 static void rtd_release(struct device *dev)
1501 static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1506 /* register the rtd device */
1507 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1510 device_initialize(rtd->dev);
1511 rtd->dev->parent = rtd->card->dev;
1512 rtd->dev->release = rtd_release;
1513 rtd->dev->groups = soc_dev_attr_groups;
1514 dev_set_name(rtd->dev, "%s", name);
1515 dev_set_drvdata(rtd->dev, rtd);
1516 mutex_init(&rtd->pcm_mutex);
1517 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1518 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1519 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1520 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1521 ret = device_add(rtd->dev);
1523 /* calling put_device() here to free the rtd->dev */
1524 put_device(rtd->dev);
1525 dev_err(rtd->card->dev,
1526 "ASoC: failed to register runtime device: %d\n", ret);
1529 rtd->dev_registered = 1;
1533 static int soc_probe_link_components(struct snd_soc_card *card,
1534 struct snd_soc_pcm_runtime *rtd,
1537 struct snd_soc_component *component;
1538 struct snd_soc_rtdcom_list *rtdcom;
1541 for_each_rtdcom(rtd, rtdcom) {
1542 component = rtdcom->component;
1544 if (component->driver->probe_order == order) {
1545 ret = soc_probe_component(card, component);
1554 static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1557 dai->driver->probe_order != order)
1560 if (dai->driver->probe) {
1561 int ret = dai->driver->probe(dai);
1563 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1574 static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1575 struct snd_soc_pcm_runtime *rtd)
1579 for (i = 0; i < num_dais; ++i) {
1580 struct snd_soc_dai_driver *drv = dais[i]->driver;
1582 if (!rtd->dai_link->no_pcm && drv->pcm_new)
1583 ret = drv->pcm_new(rtd, dais[i]);
1585 dev_err(dais[i]->dev,
1586 "ASoC: Failed to bind %s with pcm device\n",
1595 static int soc_link_dai_widgets(struct snd_soc_card *card,
1596 struct snd_soc_dai_link *dai_link,
1597 struct snd_soc_pcm_runtime *rtd)
1599 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1600 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1601 struct snd_soc_dapm_widget *sink, *source;
1604 if (rtd->num_codecs > 1)
1605 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1607 /* link the DAI widgets */
1608 sink = codec_dai->playback_widget;
1609 source = cpu_dai->capture_widget;
1610 if (sink && source) {
1611 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1612 dai_link->num_params,
1615 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1616 sink->name, source->name, ret);
1621 sink = cpu_dai->playback_widget;
1622 source = codec_dai->capture_widget;
1623 if (sink && source) {
1624 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1625 dai_link->num_params,
1628 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1629 sink->name, source->name, ret);
1637 static int soc_probe_link_dais(struct snd_soc_card *card,
1638 struct snd_soc_pcm_runtime *rtd, int order)
1640 struct snd_soc_dai_link *dai_link = rtd->dai_link;
1641 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1644 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1645 card->name, rtd->num, order);
1647 /* set default power off timeout */
1648 rtd->pmdown_time = pmdown_time;
1650 ret = soc_probe_dai(cpu_dai, order);
1654 /* probe the CODEC DAI */
1655 for (i = 0; i < rtd->num_codecs; i++) {
1656 ret = soc_probe_dai(rtd->codec_dais[i], order);
1661 /* complete DAI probe during last probe */
1662 if (order != SND_SOC_COMP_ORDER_LAST)
1665 /* do machine specific initialization */
1666 if (dai_link->init) {
1667 ret = dai_link->init(rtd);
1669 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1670 dai_link->name, ret);
1675 if (dai_link->dai_fmt)
1676 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1678 ret = soc_post_component_init(rtd, dai_link->name);
1682 #ifdef CONFIG_DEBUG_FS
1683 /* add DPCM sysfs entries */
1684 if (dai_link->dynamic)
1685 soc_dpcm_debugfs_add(rtd);
1688 if (cpu_dai->driver->compress_new) {
1689 /*create compress_device"*/
1690 ret = cpu_dai->driver->compress_new(rtd, rtd->num);
1692 dev_err(card->dev, "ASoC: can't create compress %s\n",
1693 dai_link->stream_name);
1698 if (!dai_link->params) {
1699 /* create the pcm */
1700 ret = soc_new_pcm(rtd, rtd->num);
1702 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1703 dai_link->stream_name, ret);
1706 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1709 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1710 rtd->num_codecs, rtd);
1714 INIT_DELAYED_WORK(&rtd->delayed_work,
1715 codec2codec_close_delayed_work);
1717 /* link the DAI widgets */
1718 ret = soc_link_dai_widgets(card, dai_link, rtd);
1727 static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1729 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1730 struct snd_soc_component *component;
1732 struct device_node *codec_of_node;
1734 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1735 /* codecs, usually analog devices */
1736 name = aux_dev->codec_name;
1737 codec_of_node = aux_dev->codec_of_node;
1738 component = soc_find_component(codec_of_node, name);
1741 name = of_node_full_name(codec_of_node);
1744 } else if (aux_dev->name) {
1745 /* generic components */
1746 name = aux_dev->name;
1747 component = soc_find_component(NULL, name);
1751 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1755 component->init = aux_dev->init;
1756 list_add(&component->card_aux_list, &card->aux_comp_list);
1761 dev_err(card->dev, "ASoC: %s not registered\n", name);
1762 return -EPROBE_DEFER;
1765 static int soc_probe_aux_devices(struct snd_soc_card *card)
1767 struct snd_soc_component *comp;
1771 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1773 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
1774 if (comp->driver->probe_order == order) {
1775 ret = soc_probe_component(card, comp);
1778 "ASoC: failed to probe aux component %s %d\n",
1789 static void soc_remove_aux_devices(struct snd_soc_card *card)
1791 struct snd_soc_component *comp, *_comp;
1794 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1796 list_for_each_entry_safe(comp, _comp,
1797 &card->aux_comp_list, card_aux_list) {
1799 if (comp->driver->remove_order == order) {
1800 soc_remove_component(comp);
1801 /* remove it from the card's aux_comp_list */
1802 list_del(&comp->card_aux_list);
1808 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1812 if (codec->cache_init)
1815 ret = snd_soc_cache_init(codec);
1818 "ASoC: Failed to set cache compression type: %d\n",
1822 codec->cache_init = 1;
1827 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1828 * @rtd: The runtime for which the DAI link format should be changed
1829 * @dai_fmt: The new DAI link format
1831 * This function updates the DAI link format for all DAIs connected to the DAI
1832 * link for the specified runtime.
1834 * Note: For setups with a static format set the dai_fmt field in the
1835 * corresponding snd_dai_link struct instead of using this function.
1837 * Returns 0 on success, otherwise a negative error code.
1839 int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1840 unsigned int dai_fmt)
1842 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1843 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1847 for (i = 0; i < rtd->num_codecs; i++) {
1848 struct snd_soc_dai *codec_dai = codec_dais[i];
1850 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1851 if (ret != 0 && ret != -ENOTSUPP) {
1852 dev_warn(codec_dai->dev,
1853 "ASoC: Failed to set DAI format: %d\n", ret);
1858 /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1859 /* the component which has non_legacy_dai_naming is Codec */
1860 if (cpu_dai->codec ||
1861 cpu_dai->component->driver->non_legacy_dai_naming) {
1862 unsigned int inv_dai_fmt;
1864 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1865 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1866 case SND_SOC_DAIFMT_CBM_CFM:
1867 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1869 case SND_SOC_DAIFMT_CBM_CFS:
1870 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1872 case SND_SOC_DAIFMT_CBS_CFM:
1873 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1875 case SND_SOC_DAIFMT_CBS_CFS:
1876 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1880 dai_fmt = inv_dai_fmt;
1883 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1884 if (ret != 0 && ret != -ENOTSUPP) {
1885 dev_warn(cpu_dai->dev,
1886 "ASoC: Failed to set DAI format: %d\n", ret);
1892 EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1896 /* Trim special characters, and replace '-' with '_' since '-' is used to
1897 * separate different DMI fields in the card long name. Only number and
1898 * alphabet characters and a few separator characters are kept.
1900 static void cleanup_dmi_name(char *name)
1904 for (i = 0; name[i]; i++) {
1905 if (isalnum(name[i]) || (name[i] == '.')
1906 || (name[i] == '_'))
1907 name[j++] = name[i];
1908 else if (name[i] == '-')
1915 /* Check if a DMI field is valid, i.e. not containing any string
1916 * in the black list.
1918 static int is_dmi_valid(const char *field)
1922 while (dmi_blacklist[i]) {
1923 if (strstr(field, dmi_blacklist[i]))
1932 * snd_soc_set_dmi_name() - Register DMI names to card
1933 * @card: The card to register DMI names
1934 * @flavour: The flavour "differentiator" for the card amongst its peers.
1936 * An Intel machine driver may be used by many different devices but are
1937 * difficult for userspace to differentiate, since machine drivers ususally
1938 * use their own name as the card short name and leave the card long name
1939 * blank. To differentiate such devices and fix bugs due to lack of
1940 * device-specific configurations, this function allows DMI info to be used
1941 * as the sound card long name, in the format of
1942 * "vendor-product-version-board"
1943 * (Character '-' is used to separate different DMI fields here).
1944 * This will help the user space to load the device-specific Use Case Manager
1945 * (UCM) configurations for the card.
1947 * Possible card long names may be:
1948 * DellInc.-XPS139343-01-0310JH
1949 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1950 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1952 * This function also supports flavoring the card longname to provide
1953 * the extra differentiation, like "vendor-product-version-board-flavor".
1955 * We only keep number and alphabet characters and a few separator characters
1956 * in the card long name since UCM in the user space uses the card long names
1957 * as card configuration directory names and AudoConf cannot support special
1958 * charactors like SPACE.
1960 * Returns 0 on success, otherwise a negative error code.
1962 int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1964 const char *vendor, *product, *product_version, *board;
1965 size_t longname_buf_size = sizeof(card->snd_card->longname);
1968 if (card->long_name)
1969 return 0; /* long name already set by driver or from DMI */
1971 /* make up dmi long name as: vendor.product.version.board */
1972 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1973 if (!vendor || !is_dmi_valid(vendor)) {
1974 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1979 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1981 cleanup_dmi_name(card->dmi_longname);
1983 product = dmi_get_system_info(DMI_PRODUCT_NAME);
1984 if (product && is_dmi_valid(product)) {
1985 len = strlen(card->dmi_longname);
1986 snprintf(card->dmi_longname + len,
1987 longname_buf_size - len,
1990 len++; /* skip the separator "-" */
1991 if (len < longname_buf_size)
1992 cleanup_dmi_name(card->dmi_longname + len);
1994 /* some vendors like Lenovo may only put a self-explanatory
1995 * name in the product version field
1997 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
1998 if (product_version && is_dmi_valid(product_version)) {
1999 len = strlen(card->dmi_longname);
2000 snprintf(card->dmi_longname + len,
2001 longname_buf_size - len,
2002 "-%s", product_version);
2005 if (len < longname_buf_size)
2006 cleanup_dmi_name(card->dmi_longname + len);
2010 board = dmi_get_system_info(DMI_BOARD_NAME);
2011 if (board && is_dmi_valid(board)) {
2012 len = strlen(card->dmi_longname);
2013 snprintf(card->dmi_longname + len,
2014 longname_buf_size - len,
2018 if (len < longname_buf_size)
2019 cleanup_dmi_name(card->dmi_longname + len);
2020 } else if (!product) {
2021 /* fall back to using legacy name */
2022 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
2026 /* Add flavour to dmi long name */
2028 len = strlen(card->dmi_longname);
2029 snprintf(card->dmi_longname + len,
2030 longname_buf_size - len,
2034 if (len < longname_buf_size)
2035 cleanup_dmi_name(card->dmi_longname + len);
2038 /* set the card long name */
2039 card->long_name = card->dmi_longname;
2043 EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
2044 #endif /* CONFIG_DMI */
2046 static int snd_soc_instantiate_card(struct snd_soc_card *card)
2048 struct snd_soc_codec *codec;
2049 struct snd_soc_pcm_runtime *rtd;
2050 struct snd_soc_dai_link *dai_link;
2053 mutex_lock(&client_mutex);
2054 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
2057 for (i = 0; i < card->num_links; i++) {
2058 ret = soc_bind_dai_link(card, &card->dai_link[i]);
2063 /* bind aux_devs too */
2064 for (i = 0; i < card->num_aux_devs; i++) {
2065 ret = soc_bind_aux_dev(card, i);
2070 /* add predefined DAI links to the list */
2071 for (i = 0; i < card->num_links; i++)
2072 snd_soc_add_dai_link(card, card->dai_link+i);
2074 /* initialize the register cache for each available codec */
2075 list_for_each_entry(codec, &codec_list, list) {
2076 if (codec->cache_init)
2078 ret = snd_soc_init_codec_cache(codec);
2083 /* card bind complete so register a sound card */
2084 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2085 card->owner, 0, &card->snd_card);
2088 "ASoC: can't create sound card for card %s: %d\n",
2093 soc_init_card_debugfs(card);
2095 card->dapm.bias_level = SND_SOC_BIAS_OFF;
2096 card->dapm.dev = card->dev;
2097 card->dapm.card = card;
2098 list_add(&card->dapm.list, &card->dapm_list);
2100 #ifdef CONFIG_DEBUG_FS
2101 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2104 #ifdef CONFIG_PM_SLEEP
2105 /* deferred resume work */
2106 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
2109 if (card->dapm_widgets)
2110 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2111 card->num_dapm_widgets);
2113 if (card->of_dapm_widgets)
2114 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2115 card->num_of_dapm_widgets);
2117 /* initialise the sound card only once */
2119 ret = card->probe(card);
2121 goto card_probe_error;
2124 /* probe all components used by DAI links on this card */
2125 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2127 list_for_each_entry(rtd, &card->rtd_list, list) {
2128 ret = soc_probe_link_components(card, rtd, order);
2131 "ASoC: failed to instantiate card %d\n",
2138 /* probe auxiliary components */
2139 ret = soc_probe_aux_devices(card);
2143 /* Find new DAI links added during probing components and bind them.
2144 * Components with topology may bring new DAIs and DAI links.
2146 list_for_each_entry(dai_link, &card->dai_link_list, list) {
2147 if (soc_is_dai_link_bound(card, dai_link))
2150 ret = soc_init_dai_link(card, dai_link);
2153 ret = soc_bind_dai_link(card, dai_link);
2158 /* probe all DAI links on this card */
2159 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2161 list_for_each_entry(rtd, &card->rtd_list, list) {
2162 ret = soc_probe_link_dais(card, rtd, order);
2165 "ASoC: failed to instantiate card %d\n",
2172 snd_soc_dapm_link_dai_widgets(card);
2173 snd_soc_dapm_connect_dai_link_widgets(card);
2176 snd_soc_add_card_controls(card, card->controls, card->num_controls);
2178 if (card->dapm_routes)
2179 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2180 card->num_dapm_routes);
2182 if (card->of_dapm_routes)
2183 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2184 card->num_of_dapm_routes);
2186 /* try to set some sane longname if DMI is available */
2187 snd_soc_set_dmi_name(card, NULL);
2189 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
2191 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2192 "%s", card->long_name ? card->long_name : card->name);
2193 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2194 "%s", card->driver_name ? card->driver_name : card->name);
2195 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2196 switch (card->snd_card->driver[i]) {
2202 if (!isalnum(card->snd_card->driver[i]))
2203 card->snd_card->driver[i] = '_';
2208 if (card->late_probe) {
2209 ret = card->late_probe(card);
2211 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
2213 goto probe_aux_dev_err;
2217 snd_soc_dapm_new_widgets(card);
2219 ret = snd_card_register(card->snd_card);
2221 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2223 goto probe_aux_dev_err;
2226 card->instantiated = 1;
2227 snd_soc_dapm_sync(&card->dapm);
2228 mutex_unlock(&card->mutex);
2229 mutex_unlock(&client_mutex);
2234 soc_remove_aux_devices(card);
2237 soc_remove_dai_links(card);
2243 snd_soc_dapm_free(&card->dapm);
2244 soc_cleanup_card_debugfs(card);
2245 snd_card_free(card->snd_card);
2248 soc_remove_pcm_runtimes(card);
2249 mutex_unlock(&card->mutex);
2250 mutex_unlock(&client_mutex);
2255 /* probes a new socdev */
2256 static int soc_probe(struct platform_device *pdev)
2258 struct snd_soc_card *card = platform_get_drvdata(pdev);
2261 * no card, so machine driver should be registering card
2262 * we should not be here in that case so ret error
2267 dev_warn(&pdev->dev,
2268 "ASoC: machine %s should use snd_soc_register_card()\n",
2271 /* Bodge while we unpick instantiation */
2272 card->dev = &pdev->dev;
2274 return snd_soc_register_card(card);
2277 static int soc_cleanup_card_resources(struct snd_soc_card *card)
2279 struct snd_soc_pcm_runtime *rtd;
2281 /* make sure any delayed work runs */
2282 list_for_each_entry(rtd, &card->rtd_list, list)
2283 flush_delayed_work(&rtd->delayed_work);
2285 /* free the ALSA card at first; this syncs with pending operations */
2286 snd_card_free(card->snd_card);
2288 /* remove and free each DAI */
2289 soc_remove_dai_links(card);
2290 soc_remove_pcm_runtimes(card);
2292 /* remove auxiliary devices */
2293 soc_remove_aux_devices(card);
2295 snd_soc_dapm_free(&card->dapm);
2296 soc_cleanup_card_debugfs(card);
2298 /* remove the card */
2305 /* removes a socdev */
2306 static int soc_remove(struct platform_device *pdev)
2308 struct snd_soc_card *card = platform_get_drvdata(pdev);
2310 snd_soc_unregister_card(card);
2314 int snd_soc_poweroff(struct device *dev)
2316 struct snd_soc_card *card = dev_get_drvdata(dev);
2317 struct snd_soc_pcm_runtime *rtd;
2319 if (!card->instantiated)
2322 /* Flush out pmdown_time work - we actually do want to run it
2323 * now, we're shutting down so no imminent restart. */
2324 list_for_each_entry(rtd, &card->rtd_list, list)
2325 flush_delayed_work(&rtd->delayed_work);
2327 snd_soc_dapm_shutdown(card);
2329 /* deactivate pins to sleep state */
2330 list_for_each_entry(rtd, &card->rtd_list, list) {
2331 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2334 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2335 for (i = 0; i < rtd->num_codecs; i++) {
2336 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
2337 pinctrl_pm_select_sleep_state(codec_dai->dev);
2343 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2345 const struct dev_pm_ops snd_soc_pm_ops = {
2346 .suspend = snd_soc_suspend,
2347 .resume = snd_soc_resume,
2348 .freeze = snd_soc_suspend,
2349 .thaw = snd_soc_resume,
2350 .poweroff = snd_soc_poweroff,
2351 .restore = snd_soc_resume,
2353 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2355 /* ASoC platform driver */
2356 static struct platform_driver soc_driver = {
2358 .name = "soc-audio",
2359 .pm = &snd_soc_pm_ops,
2362 .remove = soc_remove,
2366 * snd_soc_cnew - create new control
2367 * @_template: control template
2368 * @data: control private data
2369 * @long_name: control long name
2370 * @prefix: control name prefix
2372 * Create a new mixer control from a template control.
2374 * Returns 0 for success, else error.
2376 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2377 void *data, const char *long_name,
2380 struct snd_kcontrol_new template;
2381 struct snd_kcontrol *kcontrol;
2384 memcpy(&template, _template, sizeof(template));
2388 long_name = template.name;
2391 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2395 template.name = name;
2397 template.name = long_name;
2400 kcontrol = snd_ctl_new1(&template, data);
2406 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2408 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2409 const struct snd_kcontrol_new *controls, int num_controls,
2410 const char *prefix, void *data)
2414 for (i = 0; i < num_controls; i++) {
2415 const struct snd_kcontrol_new *control = &controls[i];
2416 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2417 control->name, prefix));
2419 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2420 control->name, err);
2428 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2431 struct snd_card *card = soc_card->snd_card;
2432 struct snd_kcontrol *kctl;
2434 if (unlikely(!name))
2437 list_for_each_entry(kctl, &card->controls, list)
2438 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2442 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2445 * snd_soc_add_component_controls - Add an array of controls to a component.
2447 * @component: Component to add controls to
2448 * @controls: Array of controls to add
2449 * @num_controls: Number of elements in the array
2451 * Return: 0 for success, else error.
2453 int snd_soc_add_component_controls(struct snd_soc_component *component,
2454 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2456 struct snd_card *card = component->card->snd_card;
2458 return snd_soc_add_controls(card, component->dev, controls,
2459 num_controls, component->name_prefix, component);
2461 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2464 * snd_soc_add_codec_controls - add an array of controls to a codec.
2465 * Convenience function to add a list of controls. Many codecs were
2466 * duplicating this code.
2468 * @codec: codec to add controls to
2469 * @controls: array of controls to add
2470 * @num_controls: number of elements in the array
2472 * Return 0 for success, else error.
2474 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2475 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2477 return snd_soc_add_component_controls(&codec->component, controls,
2480 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2483 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2484 * Convenience function to add a list of controls.
2486 * @soc_card: SoC card to add controls to
2487 * @controls: array of controls to add
2488 * @num_controls: number of elements in the array
2490 * Return 0 for success, else error.
2492 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2493 const struct snd_kcontrol_new *controls, int num_controls)
2495 struct snd_card *card = soc_card->snd_card;
2497 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2500 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2503 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2504 * Convienience function to add a list of controls.
2506 * @dai: DAI to add controls to
2507 * @controls: array of controls to add
2508 * @num_controls: number of elements in the array
2510 * Return 0 for success, else error.
2512 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2513 const struct snd_kcontrol_new *controls, int num_controls)
2515 struct snd_card *card = dai->component->card->snd_card;
2517 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2520 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2523 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2525 * @clk_id: DAI specific clock ID
2526 * @freq: new clock frequency in Hz
2527 * @dir: new clock direction - input/output.
2529 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2531 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2532 unsigned int freq, int dir)
2534 if (dai->driver->ops->set_sysclk)
2535 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2537 return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2540 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2543 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2545 * @clk_id: DAI specific clock ID
2546 * @source: Source for the clock
2547 * @freq: new clock frequency in Hz
2548 * @dir: new clock direction - input/output.
2550 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2552 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2553 int source, unsigned int freq, int dir)
2555 if (codec->driver->set_sysclk)
2556 return codec->driver->set_sysclk(codec, clk_id, source,
2561 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2564 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2565 * @component: COMPONENT
2566 * @clk_id: DAI specific clock ID
2567 * @source: Source for the clock
2568 * @freq: new clock frequency in Hz
2569 * @dir: new clock direction - input/output.
2571 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2573 int snd_soc_component_set_sysclk(struct snd_soc_component *component, int clk_id,
2574 int source, unsigned int freq, int dir)
2576 /* will be removed */
2577 if (component->set_sysclk)
2578 return component->set_sysclk(component, clk_id, source,
2581 if (component->driver->set_sysclk)
2582 return component->driver->set_sysclk(component, clk_id, source,
2587 EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2590 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2592 * @div_id: DAI specific clock divider ID
2593 * @div: new clock divisor.
2595 * Configures the clock dividers. This is used to derive the best DAI bit and
2596 * frame clocks from the system or master clock. It's best to set the DAI bit
2597 * and frame clocks as low as possible to save system power.
2599 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2600 int div_id, int div)
2602 if (dai->driver->ops->set_clkdiv)
2603 return dai->driver->ops->set_clkdiv(dai, div_id, div);
2607 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2610 * snd_soc_dai_set_pll - configure DAI PLL.
2612 * @pll_id: DAI specific PLL ID
2613 * @source: DAI specific source for the PLL
2614 * @freq_in: PLL input clock frequency in Hz
2615 * @freq_out: requested PLL output clock frequency in Hz
2617 * Configures and enables PLL to generate output clock based on input clock.
2619 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2620 unsigned int freq_in, unsigned int freq_out)
2622 if (dai->driver->ops->set_pll)
2623 return dai->driver->ops->set_pll(dai, pll_id, source,
2626 return snd_soc_component_set_pll(dai->component, pll_id, source,
2629 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2632 * snd_soc_codec_set_pll - configure codec PLL.
2634 * @pll_id: DAI specific PLL ID
2635 * @source: DAI specific source for the PLL
2636 * @freq_in: PLL input clock frequency in Hz
2637 * @freq_out: requested PLL output clock frequency in Hz
2639 * Configures and enables PLL to generate output clock based on input clock.
2641 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2642 unsigned int freq_in, unsigned int freq_out)
2644 if (codec->driver->set_pll)
2645 return codec->driver->set_pll(codec, pll_id, source,
2650 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2653 * snd_soc_component_set_pll - configure component PLL.
2654 * @component: COMPONENT
2655 * @pll_id: DAI specific PLL ID
2656 * @source: DAI specific source for the PLL
2657 * @freq_in: PLL input clock frequency in Hz
2658 * @freq_out: requested PLL output clock frequency in Hz
2660 * Configures and enables PLL to generate output clock based on input clock.
2662 int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2663 int source, unsigned int freq_in,
2664 unsigned int freq_out)
2666 /* will be removed */
2667 if (component->set_pll)
2668 return component->set_pll(component, pll_id, source,
2671 if (component->driver->set_pll)
2672 return component->driver->set_pll(component, pll_id, source,
2677 EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2680 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2682 * @ratio: Ratio of BCLK to Sample rate.
2684 * Configures the DAI for a preset BCLK to sample rate ratio.
2686 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2688 if (dai->driver->ops->set_bclk_ratio)
2689 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2693 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2696 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2698 * @fmt: SND_SOC_DAIFMT_* format value.
2700 * Configures the DAI hardware format and clocking.
2702 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2704 if (dai->driver == NULL)
2706 if (dai->driver->ops->set_fmt == NULL)
2708 return dai->driver->ops->set_fmt(dai, fmt);
2710 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2713 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
2714 * @slots: Number of slots in use.
2715 * @tx_mask: bitmask representing active TX slots.
2716 * @rx_mask: bitmask representing active RX slots.
2718 * Generates the TDM tx and rx slot default masks for DAI.
2720 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
2721 unsigned int *tx_mask,
2722 unsigned int *rx_mask)
2724 if (*tx_mask || *rx_mask)
2730 *tx_mask = (1 << slots) - 1;
2731 *rx_mask = (1 << slots) - 1;
2737 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2738 * @dai: The DAI to configure
2739 * @tx_mask: bitmask representing active TX slots.
2740 * @rx_mask: bitmask representing active RX slots.
2741 * @slots: Number of slots in use.
2742 * @slot_width: Width in bits for each slot.
2744 * This function configures the specified DAI for TDM operation. @slot contains
2745 * the total number of slots of the TDM stream and @slot_with the width of each
2746 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2747 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2748 * DAI should write to or read from. If a bit is set the corresponding slot is
2749 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2750 * the first slot, bit 1 to the second slot and so on. The first active slot
2751 * maps to the first channel of the DAI, the second active slot to the second
2752 * channel and so on.
2754 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2755 * @rx_mask and @slot_width will be ignored.
2757 * Returns 0 on success, a negative error code otherwise.
2759 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2760 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2762 if (dai->driver->ops->xlate_tdm_slot_mask)
2763 dai->driver->ops->xlate_tdm_slot_mask(slots,
2764 &tx_mask, &rx_mask);
2766 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
2768 dai->tx_mask = tx_mask;
2769 dai->rx_mask = rx_mask;
2771 if (dai->driver->ops->set_tdm_slot)
2772 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2777 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2780 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2782 * @tx_num: how many TX channels
2783 * @tx_slot: pointer to an array which imply the TX slot number channel
2785 * @rx_num: how many RX channels
2786 * @rx_slot: pointer to an array which imply the RX slot number channel
2789 * configure the relationship between channel number and TDM slot number.
2791 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2792 unsigned int tx_num, unsigned int *tx_slot,
2793 unsigned int rx_num, unsigned int *rx_slot)
2795 if (dai->driver->ops->set_channel_map)
2796 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2801 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2804 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2806 * @tristate: tristate enable
2808 * Tristates the DAI so that others can use it.
2810 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2812 if (dai->driver->ops->set_tristate)
2813 return dai->driver->ops->set_tristate(dai, tristate);
2817 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2820 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2822 * @mute: mute enable
2823 * @direction: stream to mute
2825 * Mutes the DAI DAC.
2827 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2833 if (dai->driver->ops->mute_stream)
2834 return dai->driver->ops->mute_stream(dai, mute, direction);
2835 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2836 dai->driver->ops->digital_mute)
2837 return dai->driver->ops->digital_mute(dai, mute);
2841 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2844 * snd_soc_register_card - Register a card with the ASoC core
2846 * @card: Card to register
2849 int snd_soc_register_card(struct snd_soc_card *card)
2852 struct snd_soc_pcm_runtime *rtd;
2854 if (!card->name || !card->dev)
2857 for (i = 0; i < card->num_links; i++) {
2858 struct snd_soc_dai_link *link = &card->dai_link[i];
2860 ret = soc_init_dai_link(card, link);
2862 dev_err(card->dev, "ASoC: failed to init link %s\n",
2868 dev_set_drvdata(card->dev, card);
2870 snd_soc_initialize_card_lists(card);
2872 INIT_LIST_HEAD(&card->dai_link_list);
2873 card->num_dai_links = 0;
2875 INIT_LIST_HEAD(&card->rtd_list);
2878 INIT_LIST_HEAD(&card->dapm_dirty);
2879 INIT_LIST_HEAD(&card->dobj_list);
2880 card->instantiated = 0;
2881 mutex_init(&card->mutex);
2882 mutex_init(&card->dapm_mutex);
2884 ret = snd_soc_instantiate_card(card);
2888 /* deactivate pins to sleep state */
2889 list_for_each_entry(rtd, &card->rtd_list, list) {
2890 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2893 for (j = 0; j < rtd->num_codecs; j++) {
2894 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2895 if (!codec_dai->active)
2896 pinctrl_pm_select_sleep_state(codec_dai->dev);
2899 if (!cpu_dai->active)
2900 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2905 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2908 * snd_soc_unregister_card - Unregister a card with the ASoC core
2910 * @card: Card to unregister
2913 int snd_soc_unregister_card(struct snd_soc_card *card)
2915 if (card->instantiated) {
2916 card->instantiated = false;
2917 snd_soc_dapm_shutdown(card);
2918 soc_cleanup_card_resources(card);
2919 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2924 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2927 * Simplify DAI link configuration by removing ".-1" from device names
2928 * and sanitizing names.
2930 static char *fmt_single_name(struct device *dev, int *id)
2932 char *found, name[NAME_SIZE];
2935 if (dev_name(dev) == NULL)
2938 strlcpy(name, dev_name(dev), NAME_SIZE);
2940 /* are we a "%s.%d" name (platform and SPI components) */
2941 found = strstr(name, dev->driver->name);
2944 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2946 /* discard ID from name if ID == -1 */
2948 found[strlen(dev->driver->name)] = '\0';
2952 /* I2C component devices are named "bus-addr" */
2953 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2954 char tmp[NAME_SIZE];
2956 /* create unique ID number from I2C addr and bus */
2957 *id = ((id1 & 0xffff) << 16) + id2;
2959 /* sanitize component name for DAI link creation */
2960 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2961 strlcpy(name, tmp, NAME_SIZE);
2966 return kstrdup(name, GFP_KERNEL);
2970 * Simplify DAI link naming for single devices with multiple DAIs by removing
2971 * any ".-1" and using the DAI name (instead of device name).
2973 static inline char *fmt_multiple_name(struct device *dev,
2974 struct snd_soc_dai_driver *dai_drv)
2976 if (dai_drv->name == NULL) {
2978 "ASoC: error - multiple DAI %s registered with no name\n",
2983 return kstrdup(dai_drv->name, GFP_KERNEL);
2987 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2989 * @component: The component for which the DAIs should be unregistered
2991 static void snd_soc_unregister_dais(struct snd_soc_component *component)
2993 struct snd_soc_dai *dai, *_dai;
2995 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
2996 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2998 list_del(&dai->list);
3004 /* Create a DAI and add it to the component's DAI list */
3005 static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
3006 struct snd_soc_dai_driver *dai_drv,
3007 bool legacy_dai_naming)
3009 struct device *dev = component->dev;
3010 struct snd_soc_dai *dai;
3012 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
3014 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3019 * Back in the old days when we still had component-less DAIs,
3020 * instead of having a static name, component-less DAIs would
3021 * inherit the name of the parent device so it is possible to
3022 * register multiple instances of the DAI. We still need to keep
3023 * the same naming style even though those DAIs are not
3024 * component-less anymore.
3026 if (legacy_dai_naming &&
3027 (dai_drv->id == 0 || dai_drv->name == NULL)) {
3028 dai->name = fmt_single_name(dev, &dai->id);
3030 dai->name = fmt_multiple_name(dev, dai_drv);
3032 dai->id = dai_drv->id;
3034 dai->id = component->num_dai;
3036 if (dai->name == NULL) {
3041 dai->component = component;
3043 dai->driver = dai_drv;
3044 if (!dai->driver->ops)
3045 dai->driver->ops = &null_dai_ops;
3047 list_add_tail(&dai->list, &component->dai_list);
3048 component->num_dai++;
3050 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3055 * snd_soc_register_dais - Register a DAI with the ASoC core
3057 * @component: The component the DAIs are registered for
3058 * @dai_drv: DAI driver to use for the DAIs
3059 * @count: Number of DAIs
3060 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3063 static int snd_soc_register_dais(struct snd_soc_component *component,
3064 struct snd_soc_dai_driver *dai_drv, size_t count,
3065 bool legacy_dai_naming)
3067 struct device *dev = component->dev;
3068 struct snd_soc_dai *dai;
3072 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
3074 for (i = 0; i < count; i++) {
3076 dai = soc_add_dai(component, dai_drv + i,
3077 count == 1 && legacy_dai_naming);
3087 snd_soc_unregister_dais(component);
3093 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
3095 * @component: The component the DAIs are registered for
3096 * @dai_drv: DAI driver to use for the DAI
3098 * Topology can use this API to register DAIs when probing a component.
3099 * These DAIs's widgets will be freed in the card cleanup and the DAIs
3100 * will be freed in the component cleanup.
3102 int snd_soc_register_dai(struct snd_soc_component *component,
3103 struct snd_soc_dai_driver *dai_drv)
3105 struct snd_soc_dapm_context *dapm =
3106 snd_soc_component_get_dapm(component);
3107 struct snd_soc_dai *dai;
3110 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
3111 dev_err(component->dev, "Invalid dai type %d\n",
3112 dai_drv->dobj.type);
3116 lockdep_assert_held(&client_mutex);
3117 dai = soc_add_dai(component, dai_drv, false);
3121 /* Create the DAI widgets here. After adding DAIs, topology may
3122 * also add routes that need these widgets as source or sink.
3124 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
3126 dev_err(component->dev,
3127 "Failed to create DAI widgets %d\n", ret);
3132 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3134 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3135 enum snd_soc_dapm_type type, int subseq)
3137 struct snd_soc_component *component = dapm->component;
3139 component->driver->seq_notifier(component, type, subseq);
3142 static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3145 struct snd_soc_component *component = dapm->component;
3147 return component->driver->stream_event(component, event);
3150 static int snd_soc_component_drv_pcm_new(struct snd_soc_component *component,
3151 struct snd_soc_pcm_runtime *rtd)
3153 if (component->driver->pcm_new)
3154 return component->driver->pcm_new(rtd);
3159 static void snd_soc_component_drv_pcm_free(struct snd_soc_component *component,
3160 struct snd_pcm *pcm)
3162 if (component->driver->pcm_free)
3163 component->driver->pcm_free(pcm);
3166 static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
3167 enum snd_soc_bias_level level)
3169 struct snd_soc_component *component = dapm->component;
3171 return component->driver->set_bias_level(component, level);
3174 static int snd_soc_component_initialize(struct snd_soc_component *component,
3175 const struct snd_soc_component_driver *driver, struct device *dev)
3177 struct snd_soc_dapm_context *dapm;
3179 component->name = fmt_single_name(dev, &component->id);
3180 if (!component->name) {
3181 dev_err(dev, "ASoC: Failed to allocate name\n");
3185 component->dev = dev;
3186 component->driver = driver;
3187 component->probe = component->driver->probe;
3188 component->remove = component->driver->remove;
3189 component->suspend = component->driver->suspend;
3190 component->resume = component->driver->resume;
3191 component->set_sysclk = component->driver->set_sysclk;
3192 component->set_pll = component->driver->set_pll;
3193 component->set_jack = component->driver->set_jack;
3194 component->pcm_new = snd_soc_component_drv_pcm_new;
3195 component->pcm_free = snd_soc_component_drv_pcm_free;
3197 dapm = snd_soc_component_get_dapm(component);
3199 dapm->component = component;
3200 dapm->bias_level = SND_SOC_BIAS_OFF;
3201 dapm->idle_bias_off = !driver->idle_bias_on;
3202 dapm->suspend_bias_off = driver->suspend_bias_off;
3203 if (driver->seq_notifier)
3204 dapm->seq_notifier = snd_soc_component_seq_notifier;
3205 if (driver->stream_event)
3206 dapm->stream_event = snd_soc_component_stream_event;
3207 if (driver->set_bias_level)
3208 dapm->set_bias_level = snd_soc_component_set_bias_level;
3210 INIT_LIST_HEAD(&component->dai_list);
3211 mutex_init(&component->io_mutex);
3216 static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
3218 int val_bytes = regmap_get_val_bytes(component->regmap);
3220 /* Errors are legitimate for non-integer byte multiples */
3222 component->val_bytes = val_bytes;
3225 #ifdef CONFIG_REGMAP
3228 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
3229 * @component: The component for which to initialize the regmap instance
3230 * @regmap: The regmap instance that should be used by the component
3232 * This function allows deferred assignment of the regmap instance that is
3233 * associated with the component. Only use this if the regmap instance is not
3234 * yet ready when the component is registered. The function must also be called
3235 * before the first IO attempt of the component.
3237 void snd_soc_component_init_regmap(struct snd_soc_component *component,
3238 struct regmap *regmap)
3240 component->regmap = regmap;
3241 snd_soc_component_setup_regmap(component);
3243 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3246 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
3247 * @component: The component for which to de-initialize the regmap instance
3249 * Calls regmap_exit() on the regmap instance associated to the component and
3250 * removes the regmap instance from the component.
3252 * This function should only be used if snd_soc_component_init_regmap() was used
3253 * to initialize the regmap instance.
3255 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3257 regmap_exit(component->regmap);
3258 component->regmap = NULL;
3260 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3264 static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
3266 if (!component->write && !component->read) {
3267 if (!component->regmap)
3268 component->regmap = dev_get_regmap(component->dev, NULL);
3269 if (component->regmap)
3270 snd_soc_component_setup_regmap(component);
3273 list_add(&component->list, &component_list);
3274 INIT_LIST_HEAD(&component->dobj_list);
3277 static void snd_soc_component_add(struct snd_soc_component *component)
3279 mutex_lock(&client_mutex);
3280 snd_soc_component_add_unlocked(component);
3281 mutex_unlock(&client_mutex);
3284 static void snd_soc_component_cleanup(struct snd_soc_component *component)
3286 snd_soc_unregister_dais(component);
3287 kfree(component->name);
3290 static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3292 struct snd_soc_card *card = component->card;
3295 snd_soc_unregister_card(card);
3297 list_del(&component->list);
3300 #define ENDIANNESS_MAP(name) \
3301 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3302 static u64 endianness_format_map[] = {
3303 ENDIANNESS_MAP(S16_),
3304 ENDIANNESS_MAP(U16_),
3305 ENDIANNESS_MAP(S24_),
3306 ENDIANNESS_MAP(U24_),
3307 ENDIANNESS_MAP(S32_),
3308 ENDIANNESS_MAP(U32_),
3309 ENDIANNESS_MAP(S24_3),
3310 ENDIANNESS_MAP(U24_3),
3311 ENDIANNESS_MAP(S20_3),
3312 ENDIANNESS_MAP(U20_3),
3313 ENDIANNESS_MAP(S18_3),
3314 ENDIANNESS_MAP(U18_3),
3315 ENDIANNESS_MAP(FLOAT_),
3316 ENDIANNESS_MAP(FLOAT64_),
3317 ENDIANNESS_MAP(IEC958_SUBFRAME_),
3321 * Fix up the DAI formats for endianness: codecs don't actually see
3322 * the endianness of the data but we're using the CPU format
3323 * definitions which do need to include endianness so we ensure that
3324 * codec DAIs always have both big and little endian variants set.
3326 static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3330 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3331 if (stream->formats & endianness_format_map[i])
3332 stream->formats |= endianness_format_map[i];
3335 int snd_soc_add_component(struct device *dev,
3336 struct snd_soc_component *component,
3337 const struct snd_soc_component_driver *component_driver,
3338 struct snd_soc_dai_driver *dai_drv,
3344 ret = snd_soc_component_initialize(component, component_driver, dev);
3348 component->ignore_pmdown_time = true;
3349 component->registered_as_component = true;
3351 if (component_driver->endianness) {
3352 for (i = 0; i < num_dai; i++) {
3353 convert_endianness_formats(&dai_drv[i].playback);
3354 convert_endianness_formats(&dai_drv[i].capture);
3358 ret = snd_soc_register_dais(component, dai_drv, num_dai,
3359 !component_driver->non_legacy_dai_naming);
3361 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3365 snd_soc_component_add(component);
3370 snd_soc_component_cleanup(component);
3374 EXPORT_SYMBOL_GPL(snd_soc_add_component);
3376 int snd_soc_register_component(struct device *dev,
3377 const struct snd_soc_component_driver *component_driver,
3378 struct snd_soc_dai_driver *dai_drv,
3381 struct snd_soc_component *component;
3383 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
3387 return snd_soc_add_component(dev, component, component_driver,
3390 EXPORT_SYMBOL_GPL(snd_soc_register_component);
3393 * snd_soc_unregister_component - Unregister all related component
3394 * from the ASoC core
3396 * @dev: The device to unregister
3398 static int __snd_soc_unregister_component(struct device *dev)
3400 struct snd_soc_component *component;
3403 mutex_lock(&client_mutex);
3404 list_for_each_entry(component, &component_list, list) {
3405 if (dev != component->dev ||
3406 !component->registered_as_component)
3409 snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);
3410 snd_soc_component_del_unlocked(component);
3414 mutex_unlock(&client_mutex);
3417 snd_soc_component_cleanup(component);
3423 void snd_soc_unregister_component(struct device *dev)
3425 while (__snd_soc_unregister_component(dev));
3427 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3429 struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
3430 const char *driver_name)
3432 struct snd_soc_component *component;
3433 struct snd_soc_component *ret;
3436 mutex_lock(&client_mutex);
3437 list_for_each_entry(component, &component_list, list) {
3438 if (dev != component->dev)
3442 (driver_name != component->driver->name) &&
3443 (strcmp(component->driver->name, driver_name) != 0))
3449 mutex_unlock(&client_mutex);
3453 EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
3455 static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3457 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3459 return codec->driver->probe(codec);
3462 static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3464 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3466 codec->driver->remove(codec);
3469 static int snd_soc_codec_drv_suspend(struct snd_soc_component *component)
3471 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3473 return codec->driver->suspend(codec);
3476 static int snd_soc_codec_drv_resume(struct snd_soc_component *component)
3478 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3480 return codec->driver->resume(codec);
3483 static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3484 unsigned int reg, unsigned int val)
3486 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3488 return codec->driver->write(codec, reg, val);
3491 static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3492 unsigned int reg, unsigned int *val)
3494 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3496 *val = codec->driver->read(codec, reg);
3501 static int snd_soc_codec_set_sysclk_(struct snd_soc_component *component,
3502 int clk_id, int source, unsigned int freq, int dir)
3504 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3506 return snd_soc_codec_set_sysclk(codec, clk_id, source, freq, dir);
3509 static int snd_soc_codec_set_pll_(struct snd_soc_component *component,
3510 int pll_id, int source, unsigned int freq_in,
3511 unsigned int freq_out)
3513 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3515 return snd_soc_codec_set_pll(codec, pll_id, source, freq_in, freq_out);
3518 static int snd_soc_codec_set_jack_(struct snd_soc_component *component,
3519 struct snd_soc_jack *jack, void *data)
3521 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3523 return snd_soc_codec_set_jack(codec, jack, data);
3526 static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
3527 enum snd_soc_bias_level level)
3529 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
3531 return codec->driver->set_bias_level(codec, level);
3535 * snd_soc_register_codec - Register a codec with the ASoC core
3537 * @dev: The parent device for this codec
3538 * @codec_drv: Codec driver
3539 * @dai_drv: The associated DAI driver
3540 * @num_dai: Number of DAIs
3542 int snd_soc_register_codec(struct device *dev,
3543 const struct snd_soc_codec_driver *codec_drv,
3544 struct snd_soc_dai_driver *dai_drv,
3547 struct snd_soc_dapm_context *dapm;
3548 struct snd_soc_codec *codec;
3549 struct snd_soc_dai *dai;
3552 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3554 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3558 codec->component.codec = codec;
3560 ret = snd_soc_component_initialize(&codec->component,
3561 &codec_drv->component_driver, dev);
3565 if (codec_drv->probe)
3566 codec->component.probe = snd_soc_codec_drv_probe;
3567 if (codec_drv->remove)
3568 codec->component.remove = snd_soc_codec_drv_remove;
3569 if (codec_drv->suspend)
3570 codec->component.suspend = snd_soc_codec_drv_suspend;
3571 if (codec_drv->resume)
3572 codec->component.resume = snd_soc_codec_drv_resume;
3573 if (codec_drv->write)
3574 codec->component.write = snd_soc_codec_drv_write;
3575 if (codec_drv->read)
3576 codec->component.read = snd_soc_codec_drv_read;
3577 if (codec_drv->set_sysclk)
3578 codec->component.set_sysclk = snd_soc_codec_set_sysclk_;
3579 if (codec_drv->set_pll)
3580 codec->component.set_pll = snd_soc_codec_set_pll_;
3581 if (codec_drv->set_jack)
3582 codec->component.set_jack = snd_soc_codec_set_jack_;
3583 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
3585 dapm = snd_soc_codec_get_dapm(codec);
3586 dapm->idle_bias_off = codec_drv->idle_bias_off;
3587 dapm->suspend_bias_off = codec_drv->suspend_bias_off;
3588 if (codec_drv->seq_notifier)
3589 dapm->seq_notifier = codec_drv->seq_notifier;
3590 if (codec_drv->set_bias_level)
3591 dapm->set_bias_level = snd_soc_codec_set_bias_level;
3593 codec->driver = codec_drv;
3594 codec->component.val_bytes = codec_drv->reg_word_size;
3596 #ifdef CONFIG_DEBUG_FS
3597 codec->component.init_debugfs = soc_init_codec_debugfs;
3598 codec->component.debugfs_prefix = "codec";
3601 if (codec_drv->get_regmap)
3602 codec->component.regmap = codec_drv->get_regmap(dev);
3604 for (i = 0; i < num_dai; i++) {
3605 convert_endianness_formats(&dai_drv[i].playback);
3606 convert_endianness_formats(&dai_drv[i].capture);
3609 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3611 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3615 list_for_each_entry(dai, &codec->component.dai_list, list)
3618 mutex_lock(&client_mutex);
3619 snd_soc_component_add_unlocked(&codec->component);
3620 list_add(&codec->list, &codec_list);
3621 mutex_unlock(&client_mutex);
3623 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3624 codec->component.name);
3628 snd_soc_component_cleanup(&codec->component);
3633 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3636 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3638 * @dev: codec to unregister
3640 void snd_soc_unregister_codec(struct device *dev)
3642 struct snd_soc_codec *codec;
3644 mutex_lock(&client_mutex);
3645 list_for_each_entry(codec, &codec_list, list) {
3646 if (dev == codec->dev)
3649 mutex_unlock(&client_mutex);
3653 list_del(&codec->list);
3654 snd_soc_component_del_unlocked(&codec->component);
3655 mutex_unlock(&client_mutex);
3657 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3658 codec->component.name);
3660 snd_soc_component_cleanup(&codec->component);
3661 snd_soc_cache_exit(codec);
3664 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3666 /* Retrieve a card's name from device tree */
3667 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3668 const char *propname)
3670 struct device_node *np;
3674 pr_err("card->dev is not set before calling %s\n", __func__);
3678 np = card->dev->of_node;
3680 ret = of_property_read_string_index(np, propname, 0, &card->name);
3682 * EINVAL means the property does not exist. This is fine providing
3683 * card->name was previously set, which is checked later in
3684 * snd_soc_register_card.
3686 if (ret < 0 && ret != -EINVAL) {
3688 "ASoC: Property '%s' could not be read: %d\n",
3695 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3697 static const struct snd_soc_dapm_widget simple_widgets[] = {
3698 SND_SOC_DAPM_MIC("Microphone", NULL),
3699 SND_SOC_DAPM_LINE("Line", NULL),
3700 SND_SOC_DAPM_HP("Headphone", NULL),
3701 SND_SOC_DAPM_SPK("Speaker", NULL),
3704 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
3705 const char *propname)
3707 struct device_node *np = card->dev->of_node;
3708 struct snd_soc_dapm_widget *widgets;
3709 const char *template, *wname;
3710 int i, j, num_widgets, ret;
3712 num_widgets = of_property_count_strings(np, propname);
3713 if (num_widgets < 0) {
3715 "ASoC: Property '%s' does not exist\n", propname);
3718 if (num_widgets & 1) {
3720 "ASoC: Property '%s' length is not even\n", propname);
3726 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3731 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3735 "ASoC: Could not allocate memory for widgets\n");
3739 for (i = 0; i < num_widgets; i++) {
3740 ret = of_property_read_string_index(np, propname,
3744 "ASoC: Property '%s' index %d read error:%d\n",
3745 propname, 2 * i, ret);
3749 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3750 if (!strncmp(template, simple_widgets[j].name,
3751 strlen(simple_widgets[j].name))) {
3752 widgets[i] = simple_widgets[j];
3757 if (j >= ARRAY_SIZE(simple_widgets)) {
3759 "ASoC: DAPM widget '%s' is not supported\n",
3764 ret = of_property_read_string_index(np, propname,
3769 "ASoC: Property '%s' index %d read error:%d\n",
3770 propname, (2 * i) + 1, ret);
3774 widgets[i].name = wname;
3777 card->of_dapm_widgets = widgets;
3778 card->num_of_dapm_widgets = num_widgets;
3782 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
3784 static int snd_soc_of_get_slot_mask(struct device_node *np,
3785 const char *prop_name,
3789 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
3795 for (i = 0; i < val; i++)
3796 if (be32_to_cpup(&of_slot_mask[i]))
3802 int snd_soc_of_parse_tdm_slot(struct device_node *np,
3803 unsigned int *tx_mask,
3804 unsigned int *rx_mask,
3805 unsigned int *slots,
3806 unsigned int *slot_width)
3812 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3814 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3816 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3817 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3825 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3826 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3836 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3838 void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
3839 struct snd_soc_codec_conf *codec_conf,
3840 struct device_node *of_node,
3841 const char *propname)
3843 struct device_node *np = card->dev->of_node;
3847 ret = of_property_read_string(np, propname, &str);
3849 /* no prefix is not error */
3853 codec_conf->of_node = of_node;
3854 codec_conf->name_prefix = str;
3856 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
3858 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3859 const char *propname)
3861 struct device_node *np = card->dev->of_node;
3863 struct snd_soc_dapm_route *routes;
3866 num_routes = of_property_count_strings(np, propname);
3867 if (num_routes < 0 || num_routes & 1) {
3869 "ASoC: Property '%s' does not exist or its length is not even\n",
3875 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3880 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3884 "ASoC: Could not allocate DAPM route table\n");
3888 for (i = 0; i < num_routes; i++) {
3889 ret = of_property_read_string_index(np, propname,
3890 2 * i, &routes[i].sink);
3893 "ASoC: Property '%s' index %d could not be read: %d\n",
3894 propname, 2 * i, ret);
3897 ret = of_property_read_string_index(np, propname,
3898 (2 * i) + 1, &routes[i].source);
3901 "ASoC: Property '%s' index %d could not be read: %d\n",
3902 propname, (2 * i) + 1, ret);
3907 card->num_of_dapm_routes = num_routes;
3908 card->of_dapm_routes = routes;
3912 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3914 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3916 struct device_node **bitclkmaster,
3917 struct device_node **framemaster)
3921 unsigned int format = 0;
3927 } of_fmt_table[] = {
3928 { "i2s", SND_SOC_DAIFMT_I2S },
3929 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3930 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3931 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3932 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3933 { "ac97", SND_SOC_DAIFMT_AC97 },
3934 { "pdm", SND_SOC_DAIFMT_PDM},
3935 { "msb", SND_SOC_DAIFMT_MSB },
3936 { "lsb", SND_SOC_DAIFMT_LSB },
3943 * check "dai-format = xxx"
3944 * or "[prefix]format = xxx"
3945 * SND_SOC_DAIFMT_FORMAT_MASK area
3947 ret = of_property_read_string(np, "dai-format", &str);
3949 snprintf(prop, sizeof(prop), "%sformat", prefix);
3950 ret = of_property_read_string(np, prop, &str);
3953 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3954 if (strcmp(str, of_fmt_table[i].name) == 0) {
3955 format |= of_fmt_table[i].val;
3962 * check "[prefix]continuous-clock"
3963 * SND_SOC_DAIFMT_CLOCK_MASK area
3965 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
3966 if (of_property_read_bool(np, prop))
3967 format |= SND_SOC_DAIFMT_CONT;
3969 format |= SND_SOC_DAIFMT_GATED;
3972 * check "[prefix]bitclock-inversion"
3973 * check "[prefix]frame-inversion"
3974 * SND_SOC_DAIFMT_INV_MASK area
3976 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3977 bit = !!of_get_property(np, prop, NULL);
3979 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3980 frame = !!of_get_property(np, prop, NULL);
3982 switch ((bit << 4) + frame) {
3984 format |= SND_SOC_DAIFMT_IB_IF;
3987 format |= SND_SOC_DAIFMT_IB_NF;
3990 format |= SND_SOC_DAIFMT_NB_IF;
3993 /* SND_SOC_DAIFMT_NB_NF is default */
3998 * check "[prefix]bitclock-master"
3999 * check "[prefix]frame-master"
4000 * SND_SOC_DAIFMT_MASTER_MASK area
4002 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4003 bit = !!of_get_property(np, prop, NULL);
4004 if (bit && bitclkmaster)
4005 *bitclkmaster = of_parse_phandle(np, prop, 0);
4007 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4008 frame = !!of_get_property(np, prop, NULL);
4009 if (frame && framemaster)
4010 *framemaster = of_parse_phandle(np, prop, 0);
4012 switch ((bit << 4) + frame) {
4014 format |= SND_SOC_DAIFMT_CBM_CFM;
4017 format |= SND_SOC_DAIFMT_CBM_CFS;
4020 format |= SND_SOC_DAIFMT_CBS_CFM;
4023 format |= SND_SOC_DAIFMT_CBS_CFS;
4029 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4031 int snd_soc_get_dai_id(struct device_node *ep)
4033 struct snd_soc_component *pos;
4034 struct device_node *node;
4037 node = of_graph_get_port_parent(ep);
4040 * For example HDMI case, HDMI has video/sound port,
4041 * but ALSA SoC needs sound port number only.
4042 * Thus counting HDMI DT port/endpoint doesn't work.
4043 * Then, it should have .of_xlate_dai_id
4046 mutex_lock(&client_mutex);
4047 list_for_each_entry(pos, &component_list, list) {
4048 struct device_node *component_of_node = pos->dev->of_node;
4050 if (!component_of_node && pos->dev->parent)
4051 component_of_node = pos->dev->parent->of_node;
4053 if (component_of_node != node)
4056 if (pos->driver->of_xlate_dai_id)
4057 ret = pos->driver->of_xlate_dai_id(pos, ep);
4061 mutex_unlock(&client_mutex);
4067 EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
4069 int snd_soc_get_dai_name(struct of_phandle_args *args,
4070 const char **dai_name)
4072 struct snd_soc_component *pos;
4073 struct device_node *component_of_node;
4074 int ret = -EPROBE_DEFER;
4076 mutex_lock(&client_mutex);
4077 list_for_each_entry(pos, &component_list, list) {
4078 component_of_node = pos->dev->of_node;
4079 if (!component_of_node && pos->dev->parent)
4080 component_of_node = pos->dev->parent->of_node;
4082 if (component_of_node != args->np)
4085 if (pos->driver->of_xlate_dai_name) {
4086 ret = pos->driver->of_xlate_dai_name(pos,
4090 struct snd_soc_dai *dai;
4093 switch (args->args_count) {
4095 id = 0; /* same as dai_drv[0] */
4105 if (id < 0 || id >= pos->num_dai) {
4112 /* find target DAI */
4113 list_for_each_entry(dai, &pos->dai_list, list) {
4119 *dai_name = dai->driver->name;
4121 *dai_name = pos->name;
4126 mutex_unlock(&client_mutex);
4129 EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
4131 int snd_soc_of_get_dai_name(struct device_node *of_node,
4132 const char **dai_name)
4134 struct of_phandle_args args;
4137 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4138 "#sound-dai-cells", 0, &args);
4142 ret = snd_soc_get_dai_name(&args, dai_name);
4144 of_node_put(args.np);
4148 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4151 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
4152 * @dai_link: DAI link
4154 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
4156 void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
4158 struct snd_soc_dai_link_component *component = dai_link->codecs;
4161 for (index = 0; index < dai_link->num_codecs; index++, component++) {
4162 if (!component->of_node)
4164 of_node_put(component->of_node);
4165 component->of_node = NULL;
4168 EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
4171 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
4173 * @of_node: Device node
4174 * @dai_link: DAI link
4176 * Builds an array of CODEC DAI components from the DAI link property
4178 * The array is set in the DAI link and the number of DAIs is set accordingly.
4179 * The device nodes in the array (of_node) must be dereferenced by calling
4180 * snd_soc_of_put_dai_link_codecs() on @dai_link.
4182 * Returns 0 for success
4184 int snd_soc_of_get_dai_link_codecs(struct device *dev,
4185 struct device_node *of_node,
4186 struct snd_soc_dai_link *dai_link)
4188 struct of_phandle_args args;
4189 struct snd_soc_dai_link_component *component;
4191 int index, num_codecs, ret;
4193 /* Count the number of CODECs */
4195 num_codecs = of_count_phandle_with_args(of_node, name,
4196 "#sound-dai-cells");
4197 if (num_codecs <= 0) {
4198 if (num_codecs == -ENOENT)
4199 dev_err(dev, "No 'sound-dai' property\n");
4201 dev_err(dev, "Bad phandle in 'sound-dai'\n");
4204 component = devm_kzalloc(dev,
4205 sizeof *component * num_codecs,
4209 dai_link->codecs = component;
4210 dai_link->num_codecs = num_codecs;
4212 /* Parse the list */
4213 for (index = 0, component = dai_link->codecs;
4214 index < dai_link->num_codecs;
4215 index++, component++) {
4216 ret = of_parse_phandle_with_args(of_node, name,
4221 component->of_node = args.np;
4222 ret = snd_soc_get_dai_name(&args, &component->dai_name);
4228 snd_soc_of_put_dai_link_codecs(dai_link);
4229 dai_link->codecs = NULL;
4230 dai_link->num_codecs = 0;
4233 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
4235 static int __init snd_soc_init(void)
4237 snd_soc_debugfs_init();
4238 snd_soc_util_init();
4240 return platform_driver_register(&soc_driver);
4242 module_init(snd_soc_init);
4244 static void __exit snd_soc_exit(void)
4246 snd_soc_util_exit();
4247 snd_soc_debugfs_exit();
4249 platform_driver_unregister(&soc_driver);
4251 module_exit(snd_soc_exit);
4253 /* Module information */
4254 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4255 MODULE_DESCRIPTION("ALSA SoC Core");
4256 MODULE_LICENSE("GPL");
4257 MODULE_ALIAS("platform:soc-audio");