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 <sound/core.h>
38 #include <sound/jack.h>
39 #include <sound/pcm.h>
40 #include <sound/pcm_params.h>
41 #include <sound/soc.h>
42 #include <sound/soc-dpcm.h>
43 #include <sound/soc-topology.h>
44 #include <sound/initval.h>
46 #define CREATE_TRACE_POINTS
47 #include <trace/events/asoc.h>
51 #ifdef CONFIG_DEBUG_FS
52 struct dentry *snd_soc_debugfs_root;
53 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
56 static DEFINE_MUTEX(client_mutex);
57 static LIST_HEAD(platform_list);
58 static LIST_HEAD(codec_list);
59 static LIST_HEAD(component_list);
62 * This is a timeout to do a DAPM powerdown after a stream is closed().
63 * It can be used to eliminate pops between different playback streams, e.g.
64 * between two audio tracks.
66 static int pmdown_time = 5000;
67 module_param(pmdown_time, int, 0);
68 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
70 /* returns the minimum number of bytes needed to represent
71 * a particular given value */
72 static int min_bytes_needed(unsigned long val)
77 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
80 c = (sizeof val * 8) - c;
88 /* fill buf which is 'len' bytes with a formatted
89 * string of the form 'reg: value\n' */
90 static int format_register_str(struct snd_soc_codec *codec,
91 unsigned int reg, char *buf, size_t len)
93 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
94 int regsize = codec->driver->reg_word_size * 2;
97 /* +2 for ': ' and + 1 for '\n' */
98 if (wordsize + regsize + 2 + 1 != len)
101 sprintf(buf, "%.*x: ", wordsize, reg);
104 ret = snd_soc_read(codec, reg);
106 memset(buf, 'X', regsize);
108 sprintf(buf, "%.*x", regsize, ret);
110 /* no NUL-termination needed */
114 /* codec register dump */
115 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
116 size_t count, loff_t pos)
119 int wordsize, regsize;
124 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
125 regsize = codec->driver->reg_word_size * 2;
127 len = wordsize + regsize + 2 + 1;
129 if (!codec->driver->reg_cache_size)
132 if (codec->driver->reg_cache_step)
133 step = codec->driver->reg_cache_step;
135 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
136 /* only support larger than PAGE_SIZE bytes debugfs
137 * entries for the default case */
139 if (total + len >= count - 1)
141 format_register_str(codec, i, buf + total, len);
147 total = min(total, count - 1);
152 static ssize_t codec_reg_show(struct device *dev,
153 struct device_attribute *attr, char *buf)
155 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
157 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
160 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
162 static ssize_t pmdown_time_show(struct device *dev,
163 struct device_attribute *attr, char *buf)
165 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
167 return sprintf(buf, "%ld\n", rtd->pmdown_time);
170 static ssize_t pmdown_time_set(struct device *dev,
171 struct device_attribute *attr,
172 const char *buf, size_t count)
174 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
177 ret = kstrtol(buf, 10, &rtd->pmdown_time);
184 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
186 static struct attribute *soc_dev_attrs[] = {
187 &dev_attr_codec_reg.attr,
188 &dev_attr_pmdown_time.attr,
192 static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
193 struct attribute *attr, int idx)
195 struct device *dev = kobj_to_dev(kobj);
196 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
198 if (attr == &dev_attr_pmdown_time.attr)
199 return attr->mode; /* always visible */
200 return rtd->codec ? attr->mode : 0; /* enabled only with codec */
203 static const struct attribute_group soc_dapm_dev_group = {
204 .attrs = soc_dapm_dev_attrs,
205 .is_visible = soc_dev_attr_is_visible,
208 static const struct attribute_group soc_dev_roup = {
209 .attrs = soc_dev_attrs,
210 .is_visible = soc_dev_attr_is_visible,
213 static const struct attribute_group *soc_dev_attr_groups[] = {
219 #ifdef CONFIG_DEBUG_FS
220 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
221 size_t count, loff_t *ppos)
224 struct snd_soc_codec *codec = file->private_data;
227 if (*ppos < 0 || !count)
230 buf = kmalloc(count, GFP_KERNEL);
234 ret = soc_codec_reg_show(codec, buf, count, *ppos);
236 if (copy_to_user(user_buf, buf, ret)) {
247 static ssize_t codec_reg_write_file(struct file *file,
248 const char __user *user_buf, size_t count, loff_t *ppos)
253 unsigned long reg, value;
254 struct snd_soc_codec *codec = file->private_data;
257 buf_size = min(count, (sizeof(buf)-1));
258 if (copy_from_user(buf, user_buf, buf_size))
262 while (*start == ' ')
264 reg = simple_strtoul(start, &start, 16);
265 while (*start == ' ')
267 ret = kstrtoul(start, 16, &value);
271 /* Userspace has been fiddling around behind the kernel's back */
272 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
274 snd_soc_write(codec, reg, value);
278 static const struct file_operations codec_reg_fops = {
280 .read = codec_reg_read_file,
281 .write = codec_reg_write_file,
282 .llseek = default_llseek,
285 static void soc_init_component_debugfs(struct snd_soc_component *component)
287 if (!component->card->debugfs_card_root)
290 if (component->debugfs_prefix) {
293 name = kasprintf(GFP_KERNEL, "%s:%s",
294 component->debugfs_prefix, component->name);
296 component->debugfs_root = debugfs_create_dir(name,
297 component->card->debugfs_card_root);
301 component->debugfs_root = debugfs_create_dir(component->name,
302 component->card->debugfs_card_root);
305 if (!component->debugfs_root) {
306 dev_dbg(component->dev,
307 "ASoC: Failed to create component debugfs directory\n");
311 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
312 component->debugfs_root);
314 if (component->init_debugfs)
315 component->init_debugfs(component);
318 static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
320 debugfs_remove_recursive(component->debugfs_root);
323 static void soc_init_codec_debugfs(struct snd_soc_component *component)
325 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
327 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
328 codec->component.debugfs_root,
329 codec, &codec_reg_fops);
330 if (!codec->debugfs_reg)
332 "ASoC: Failed to create codec register debugfs file\n");
335 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
336 size_t count, loff_t *ppos)
338 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
339 ssize_t len, ret = 0;
340 struct snd_soc_codec *codec;
345 mutex_lock(&client_mutex);
347 list_for_each_entry(codec, &codec_list, list) {
348 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
349 codec->component.name);
352 if (ret > PAGE_SIZE) {
358 mutex_unlock(&client_mutex);
361 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
368 static const struct file_operations codec_list_fops = {
369 .read = codec_list_read_file,
370 .llseek = default_llseek,/* read accesses f_pos */
373 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
374 size_t count, loff_t *ppos)
376 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
377 ssize_t len, ret = 0;
378 struct snd_soc_component *component;
379 struct snd_soc_dai *dai;
384 mutex_lock(&client_mutex);
386 list_for_each_entry(component, &component_list, list) {
387 list_for_each_entry(dai, &component->dai_list, list) {
388 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
392 if (ret > PAGE_SIZE) {
399 mutex_unlock(&client_mutex);
401 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
408 static const struct file_operations dai_list_fops = {
409 .read = dai_list_read_file,
410 .llseek = default_llseek,/* read accesses f_pos */
413 static ssize_t platform_list_read_file(struct file *file,
414 char __user *user_buf,
415 size_t count, loff_t *ppos)
417 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
418 ssize_t len, ret = 0;
419 struct snd_soc_platform *platform;
424 mutex_lock(&client_mutex);
426 list_for_each_entry(platform, &platform_list, list) {
427 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
428 platform->component.name);
431 if (ret > PAGE_SIZE) {
437 mutex_unlock(&client_mutex);
439 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
446 static const struct file_operations platform_list_fops = {
447 .read = platform_list_read_file,
448 .llseek = default_llseek,/* read accesses f_pos */
451 static void soc_init_card_debugfs(struct snd_soc_card *card)
453 if (!snd_soc_debugfs_root)
456 card->debugfs_card_root = debugfs_create_dir(card->name,
457 snd_soc_debugfs_root);
458 if (!card->debugfs_card_root) {
460 "ASoC: Failed to create card debugfs directory\n");
464 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
465 card->debugfs_card_root,
467 if (!card->debugfs_pop_time)
469 "ASoC: Failed to create pop time debugfs file\n");
472 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
474 debugfs_remove_recursive(card->debugfs_card_root);
478 static void snd_soc_debugfs_init(void)
480 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
481 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
482 pr_warn("ASoC: Failed to create debugfs directory\n");
483 snd_soc_debugfs_root = NULL;
487 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
489 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
491 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
493 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
495 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
496 &platform_list_fops))
497 pr_warn("ASoC: Failed to create platform list debugfs file\n");
500 static void snd_soc_debugfs_exit(void)
502 debugfs_remove_recursive(snd_soc_debugfs_root);
507 #define soc_init_codec_debugfs NULL
509 static inline void soc_init_component_debugfs(
510 struct snd_soc_component *component)
514 static inline void soc_cleanup_component_debugfs(
515 struct snd_soc_component *component)
519 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
523 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
527 static inline void snd_soc_debugfs_init(void)
531 static inline void snd_soc_debugfs_exit(void)
537 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
538 const char *dai_link, int stream)
542 for (i = 0; i < card->num_links; i++) {
543 if (card->rtd[i].dai_link->no_pcm &&
544 !strcmp(card->rtd[i].dai_link->name, dai_link))
545 return card->rtd[i].pcm->streams[stream].substream;
547 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
550 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
552 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
553 const char *dai_link)
557 for (i = 0; i < card->num_links; i++) {
558 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
559 return &card->rtd[i];
561 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
564 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
566 static void codec2codec_close_delayed_work(struct work_struct *work)
568 /* Currently nothing to do for c2c links
569 * Since c2c links are internal nodes in the DAPM graph and
570 * don't interface with the outside world or application layer
571 * we don't have to do any special handling on close.
575 #ifdef CONFIG_PM_SLEEP
576 /* powers down audio subsystem for suspend */
577 int snd_soc_suspend(struct device *dev)
579 struct snd_soc_card *card = dev_get_drvdata(dev);
580 struct snd_soc_codec *codec;
583 /* If the card is not initialized yet there is nothing to do */
584 if (!card->instantiated)
587 /* Due to the resume being scheduled into a workqueue we could
588 * suspend before that's finished - wait for it to complete.
590 snd_power_lock(card->snd_card);
591 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
592 snd_power_unlock(card->snd_card);
594 /* we're going to block userspace touching us until resume completes */
595 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
597 /* mute any active DACs */
598 for (i = 0; i < card->num_rtd; i++) {
600 if (card->rtd[i].dai_link->ignore_suspend)
603 for (j = 0; j < card->rtd[i].num_codecs; j++) {
604 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
605 struct snd_soc_dai_driver *drv = dai->driver;
607 if (drv->ops->digital_mute && dai->playback_active)
608 drv->ops->digital_mute(dai, 1);
612 /* suspend all pcms */
613 for (i = 0; i < card->num_rtd; i++) {
614 if (card->rtd[i].dai_link->ignore_suspend)
617 snd_pcm_suspend_all(card->rtd[i].pcm);
620 if (card->suspend_pre)
621 card->suspend_pre(card);
623 for (i = 0; i < card->num_rtd; i++) {
624 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
626 if (card->rtd[i].dai_link->ignore_suspend)
629 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
630 cpu_dai->driver->suspend(cpu_dai);
633 /* close any waiting streams */
634 for (i = 0; i < card->num_rtd; i++)
635 flush_delayed_work(&card->rtd[i].delayed_work);
637 for (i = 0; i < card->num_rtd; i++) {
639 if (card->rtd[i].dai_link->ignore_suspend)
642 snd_soc_dapm_stream_event(&card->rtd[i],
643 SNDRV_PCM_STREAM_PLAYBACK,
644 SND_SOC_DAPM_STREAM_SUSPEND);
646 snd_soc_dapm_stream_event(&card->rtd[i],
647 SNDRV_PCM_STREAM_CAPTURE,
648 SND_SOC_DAPM_STREAM_SUSPEND);
651 /* Recheck all endpoints too, their state is affected by suspend */
652 dapm_mark_endpoints_dirty(card);
653 snd_soc_dapm_sync(&card->dapm);
655 /* suspend all CODECs */
656 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
657 struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
659 /* If there are paths active then the CODEC will be held with
660 * bias _ON and should not be suspended. */
661 if (!codec->suspended) {
662 switch (snd_soc_dapm_get_bias_level(dapm)) {
663 case SND_SOC_BIAS_STANDBY:
665 * If the CODEC is capable of idle
666 * bias off then being in STANDBY
667 * means it's doing something,
668 * otherwise fall through.
670 if (dapm->idle_bias_off) {
672 "ASoC: idle_bias_off CODEC on over suspend\n");
676 case SND_SOC_BIAS_OFF:
677 if (codec->driver->suspend)
678 codec->driver->suspend(codec);
679 codec->suspended = 1;
680 if (codec->component.regmap)
681 regcache_mark_dirty(codec->component.regmap);
682 /* deactivate pins to sleep state */
683 pinctrl_pm_select_sleep_state(codec->dev);
687 "ASoC: CODEC is on over suspend\n");
693 for (i = 0; i < card->num_rtd; i++) {
694 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
696 if (card->rtd[i].dai_link->ignore_suspend)
699 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
700 cpu_dai->driver->suspend(cpu_dai);
702 /* deactivate pins to sleep state */
703 pinctrl_pm_select_sleep_state(cpu_dai->dev);
706 if (card->suspend_post)
707 card->suspend_post(card);
711 EXPORT_SYMBOL_GPL(snd_soc_suspend);
713 /* deferred resume work, so resume can complete before we finished
714 * setting our codec back up, which can be very slow on I2C
716 static void soc_resume_deferred(struct work_struct *work)
718 struct snd_soc_card *card =
719 container_of(work, struct snd_soc_card, deferred_resume_work);
720 struct snd_soc_codec *codec;
723 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
724 * so userspace apps are blocked from touching us
727 dev_dbg(card->dev, "ASoC: starting resume work\n");
729 /* Bring us up into D2 so that DAPM starts enabling things */
730 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
732 if (card->resume_pre)
733 card->resume_pre(card);
735 /* resume control bus DAIs */
736 for (i = 0; i < card->num_rtd; i++) {
737 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
739 if (card->rtd[i].dai_link->ignore_suspend)
742 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
743 cpu_dai->driver->resume(cpu_dai);
746 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
747 if (codec->suspended) {
748 if (codec->driver->resume)
749 codec->driver->resume(codec);
750 codec->suspended = 0;
754 for (i = 0; i < card->num_rtd; i++) {
756 if (card->rtd[i].dai_link->ignore_suspend)
759 snd_soc_dapm_stream_event(&card->rtd[i],
760 SNDRV_PCM_STREAM_PLAYBACK,
761 SND_SOC_DAPM_STREAM_RESUME);
763 snd_soc_dapm_stream_event(&card->rtd[i],
764 SNDRV_PCM_STREAM_CAPTURE,
765 SND_SOC_DAPM_STREAM_RESUME);
768 /* unmute any active DACs */
769 for (i = 0; i < card->num_rtd; i++) {
771 if (card->rtd[i].dai_link->ignore_suspend)
774 for (j = 0; j < card->rtd[i].num_codecs; j++) {
775 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
776 struct snd_soc_dai_driver *drv = dai->driver;
778 if (drv->ops->digital_mute && dai->playback_active)
779 drv->ops->digital_mute(dai, 0);
783 for (i = 0; i < card->num_rtd; i++) {
784 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
786 if (card->rtd[i].dai_link->ignore_suspend)
789 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
790 cpu_dai->driver->resume(cpu_dai);
793 if (card->resume_post)
794 card->resume_post(card);
796 dev_dbg(card->dev, "ASoC: resume work completed\n");
798 /* Recheck all endpoints too, their state is affected by suspend */
799 dapm_mark_endpoints_dirty(card);
800 snd_soc_dapm_sync(&card->dapm);
802 /* userspace can access us now we are back as we were before */
803 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
806 /* powers up audio subsystem after a suspend */
807 int snd_soc_resume(struct device *dev)
809 struct snd_soc_card *card = dev_get_drvdata(dev);
810 bool bus_control = false;
813 /* If the card is not initialized yet there is nothing to do */
814 if (!card->instantiated)
817 /* activate pins from sleep state */
818 for (i = 0; i < card->num_rtd; i++) {
819 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
820 struct snd_soc_dai **codec_dais = rtd->codec_dais;
821 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
825 pinctrl_pm_select_default_state(cpu_dai->dev);
827 for (j = 0; j < rtd->num_codecs; j++) {
828 struct snd_soc_dai *codec_dai = codec_dais[j];
829 if (codec_dai->active)
830 pinctrl_pm_select_default_state(codec_dai->dev);
835 * DAIs that also act as the control bus master might have other drivers
836 * hanging off them so need to resume immediately. Other drivers don't
837 * have that problem and may take a substantial amount of time to resume
838 * due to I/O costs and anti-pop so handle them out of line.
840 for (i = 0; i < card->num_rtd; i++) {
841 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
842 bus_control |= cpu_dai->driver->bus_control;
845 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
846 soc_resume_deferred(&card->deferred_resume_work);
848 dev_dbg(dev, "ASoC: Scheduling resume work\n");
849 if (!schedule_work(&card->deferred_resume_work))
850 dev_err(dev, "ASoC: resume work item may be lost\n");
855 EXPORT_SYMBOL_GPL(snd_soc_resume);
857 #define snd_soc_suspend NULL
858 #define snd_soc_resume NULL
861 static const struct snd_soc_dai_ops null_dai_ops = {
865 * soc_find_component: find a component from component_list in ASoC core
867 * @of_node: of_node of the component to query.
868 * @name: name of the component to query.
870 * function to find out if a component is already registered with ASoC core.
872 * Returns component handle for success, else NULL error.
874 struct snd_soc_component *soc_find_component(
875 const struct device_node *of_node, const char *name)
877 struct snd_soc_component *component;
879 if (!of_node && !name) {
880 pr_err("%s: Either of_node or name must be valid\n",
885 lockdep_assert_held(&client_mutex);
887 list_for_each_entry(component, &component_list, list) {
889 if (component->dev->of_node == of_node)
891 } else if (strcmp(component->name, name) == 0) {
898 EXPORT_SYMBOL(soc_find_component);
900 static struct snd_soc_dai *snd_soc_find_dai(
901 const struct snd_soc_dai_link_component *dlc)
903 struct snd_soc_component *component;
904 struct snd_soc_dai *dai;
905 struct device_node *component_of_node;
907 lockdep_assert_held(&client_mutex);
909 /* Find CPU DAI from registered DAIs*/
910 list_for_each_entry(component, &component_list, list) {
911 component_of_node = component->dev->of_node;
912 if (!component_of_node && component->dev->parent)
913 component_of_node = component->dev->parent->of_node;
915 if (dlc->of_node && component_of_node != dlc->of_node)
917 if (dlc->name && strcmp(component->name, dlc->name))
919 list_for_each_entry(dai, &component->dai_list, list) {
920 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
930 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
932 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
933 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
934 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
935 struct snd_soc_dai_link_component cpu_dai_component;
936 struct snd_soc_dai **codec_dais = rtd->codec_dais;
937 struct snd_soc_platform *platform;
938 const char *platform_name;
941 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
943 cpu_dai_component.name = dai_link->cpu_name;
944 cpu_dai_component.of_node = dai_link->cpu_of_node;
945 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
946 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
948 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
949 dai_link->cpu_dai_name);
950 return -EPROBE_DEFER;
953 rtd->num_codecs = dai_link->num_codecs;
955 /* Find CODEC from registered CODECs */
956 for (i = 0; i < rtd->num_codecs; i++) {
957 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
958 if (!codec_dais[i]) {
959 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
961 return -EPROBE_DEFER;
965 /* Single codec links expect codec and codec_dai in runtime data */
966 rtd->codec_dai = codec_dais[0];
967 rtd->codec = rtd->codec_dai->codec;
969 /* if there's no platform we match on the empty platform */
970 platform_name = dai_link->platform_name;
971 if (!platform_name && !dai_link->platform_of_node)
972 platform_name = "snd-soc-dummy";
974 /* find one from the set of registered platforms */
975 list_for_each_entry(platform, &platform_list, list) {
976 if (dai_link->platform_of_node) {
977 if (platform->dev->of_node !=
978 dai_link->platform_of_node)
981 if (strcmp(platform->component.name, platform_name))
985 rtd->platform = platform;
987 if (!rtd->platform) {
988 dev_err(card->dev, "ASoC: platform %s not registered\n",
989 dai_link->platform_name);
990 return -EPROBE_DEFER;
998 static void soc_remove_component(struct snd_soc_component *component)
1000 if (!component->card)
1003 /* This is a HACK and will be removed soon */
1004 if (component->codec)
1005 list_del(&component->codec->card_list);
1007 if (component->remove)
1008 component->remove(component);
1010 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1012 soc_cleanup_component_debugfs(component);
1013 component->card = NULL;
1014 module_put(component->dev->driver->owner);
1017 static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1021 if (dai && dai->probed &&
1022 dai->driver->remove_order == order) {
1023 if (dai->driver->remove) {
1024 err = dai->driver->remove(dai);
1027 "ASoC: failed to remove %s: %d\n",
1034 static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
1036 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1039 /* unregister the rtd device */
1040 if (rtd->dev_registered) {
1041 device_unregister(rtd->dev);
1042 rtd->dev_registered = 0;
1045 /* remove the CODEC DAI */
1046 for (i = 0; i < rtd->num_codecs; i++)
1047 soc_remove_dai(rtd->codec_dais[i], order);
1049 soc_remove_dai(rtd->cpu_dai, order);
1052 static void soc_remove_link_components(struct snd_soc_card *card, int num,
1055 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1056 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1057 struct snd_soc_platform *platform = rtd->platform;
1058 struct snd_soc_component *component;
1061 /* remove the platform */
1062 if (platform && platform->component.driver->remove_order == order)
1063 soc_remove_component(&platform->component);
1065 /* remove the CODEC-side CODEC */
1066 for (i = 0; i < rtd->num_codecs; i++) {
1067 component = rtd->codec_dais[i]->component;
1068 if (component->driver->remove_order == order)
1069 soc_remove_component(component);
1072 /* remove any CPU-side CODEC */
1074 if (cpu_dai->component->driver->remove_order == order)
1075 soc_remove_component(cpu_dai->component);
1079 static void soc_remove_dai_links(struct snd_soc_card *card)
1083 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1085 for (dai = 0; dai < card->num_rtd; dai++)
1086 soc_remove_link_dais(card, dai, order);
1089 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1091 for (dai = 0; dai < card->num_rtd; dai++)
1092 soc_remove_link_components(card, dai, order);
1098 static void soc_set_name_prefix(struct snd_soc_card *card,
1099 struct snd_soc_component *component)
1103 if (card->codec_conf == NULL)
1106 for (i = 0; i < card->num_configs; i++) {
1107 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1108 if (map->of_node && component->dev->of_node != map->of_node)
1110 if (map->dev_name && strcmp(component->name, map->dev_name))
1112 component->name_prefix = map->name_prefix;
1117 static int soc_probe_component(struct snd_soc_card *card,
1118 struct snd_soc_component *component)
1120 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1121 struct snd_soc_dai *dai;
1124 if (!strcmp(component->name, "snd-soc-dummy"))
1127 if (component->card) {
1128 if (component->card != card) {
1129 dev_err(component->dev,
1130 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1131 card->name, component->card->name);
1137 if (!try_module_get(component->dev->driver->owner))
1140 component->card = card;
1142 soc_set_name_prefix(card, component);
1144 soc_init_component_debugfs(component);
1146 if (component->dapm_widgets) {
1147 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1148 component->num_dapm_widgets);
1151 dev_err(component->dev,
1152 "Failed to create new controls %d\n", ret);
1157 list_for_each_entry(dai, &component->dai_list, list) {
1158 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1160 dev_err(component->dev,
1161 "Failed to create DAI widgets %d\n", ret);
1166 if (component->probe) {
1167 ret = component->probe(component);
1169 dev_err(component->dev,
1170 "ASoC: failed to probe component %d\n", ret);
1174 WARN(dapm->idle_bias_off &&
1175 dapm->bias_level != SND_SOC_BIAS_OFF,
1176 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1180 if (component->controls)
1181 snd_soc_add_component_controls(component, component->controls,
1182 component->num_controls);
1183 if (component->dapm_routes)
1184 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1185 component->num_dapm_routes);
1187 list_add(&dapm->list, &card->dapm_list);
1189 /* This is a HACK and will be removed soon */
1190 if (component->codec)
1191 list_add(&component->codec->card_list, &card->codec_dev_list);
1196 soc_cleanup_component_debugfs(component);
1197 component->card = NULL;
1198 module_put(component->dev->driver->owner);
1203 static void rtd_release(struct device *dev)
1208 static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1213 /* register the rtd device */
1214 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1217 device_initialize(rtd->dev);
1218 rtd->dev->parent = rtd->card->dev;
1219 rtd->dev->release = rtd_release;
1220 rtd->dev->groups = soc_dev_attr_groups;
1221 dev_set_name(rtd->dev, "%s", name);
1222 dev_set_drvdata(rtd->dev, rtd);
1223 mutex_init(&rtd->pcm_mutex);
1224 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1225 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1226 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1227 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1228 ret = device_add(rtd->dev);
1230 /* calling put_device() here to free the rtd->dev */
1231 put_device(rtd->dev);
1232 dev_err(rtd->card->dev,
1233 "ASoC: failed to register runtime device: %d\n", ret);
1236 rtd->dev_registered = 1;
1240 static int soc_probe_link_components(struct snd_soc_card *card, int num,
1243 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1244 struct snd_soc_platform *platform = rtd->platform;
1245 struct snd_soc_component *component;
1248 /* probe the CPU-side component, if it is a CODEC */
1249 component = rtd->cpu_dai->component;
1250 if (component->driver->probe_order == order) {
1251 ret = soc_probe_component(card, component);
1256 /* probe the CODEC-side components */
1257 for (i = 0; i < rtd->num_codecs; i++) {
1258 component = rtd->codec_dais[i]->component;
1259 if (component->driver->probe_order == order) {
1260 ret = soc_probe_component(card, component);
1266 /* probe the platform */
1267 if (platform->component.driver->probe_order == order) {
1268 ret = soc_probe_component(card, &platform->component);
1276 static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1280 if (!dai->probed && dai->driver->probe_order == order) {
1281 if (dai->driver->probe) {
1282 ret = dai->driver->probe(dai);
1285 "ASoC: failed to probe DAI %s: %d\n",
1297 static int soc_link_dai_widgets(struct snd_soc_card *card,
1298 struct snd_soc_dai_link *dai_link,
1299 struct snd_soc_pcm_runtime *rtd)
1301 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1302 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1303 struct snd_soc_dapm_widget *play_w, *capture_w;
1306 if (rtd->num_codecs > 1)
1307 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1309 /* link the DAI widgets */
1310 play_w = codec_dai->playback_widget;
1311 capture_w = cpu_dai->capture_widget;
1312 if (play_w && capture_w) {
1313 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1314 dai_link->num_params, capture_w,
1317 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1318 play_w->name, capture_w->name, ret);
1323 play_w = cpu_dai->playback_widget;
1324 capture_w = codec_dai->capture_widget;
1325 if (play_w && capture_w) {
1326 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1327 dai_link->num_params, capture_w,
1330 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1331 play_w->name, capture_w->name, ret);
1339 static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1341 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1342 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1343 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1346 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1347 card->name, num, order);
1349 /* set default power off timeout */
1350 rtd->pmdown_time = pmdown_time;
1352 ret = soc_probe_dai(cpu_dai, order);
1356 /* probe the CODEC DAI */
1357 for (i = 0; i < rtd->num_codecs; i++) {
1358 ret = soc_probe_dai(rtd->codec_dais[i], order);
1363 /* complete DAI probe during last probe */
1364 if (order != SND_SOC_COMP_ORDER_LAST)
1367 /* do machine specific initialization */
1368 if (dai_link->init) {
1369 ret = dai_link->init(rtd);
1371 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1372 dai_link->name, ret);
1377 if (dai_link->dai_fmt)
1378 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1380 ret = soc_post_component_init(rtd, dai_link->name);
1384 #ifdef CONFIG_DEBUG_FS
1385 /* add DPCM sysfs entries */
1386 if (dai_link->dynamic)
1387 soc_dpcm_debugfs_add(rtd);
1390 if (cpu_dai->driver->compress_new) {
1391 /*create compress_device"*/
1392 ret = cpu_dai->driver->compress_new(rtd, num);
1394 dev_err(card->dev, "ASoC: can't create compress %s\n",
1395 dai_link->stream_name);
1400 if (!dai_link->params) {
1401 /* create the pcm */
1402 ret = soc_new_pcm(rtd, num);
1404 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1405 dai_link->stream_name, ret);
1409 INIT_DELAYED_WORK(&rtd->delayed_work,
1410 codec2codec_close_delayed_work);
1412 /* link the DAI widgets */
1413 ret = soc_link_dai_widgets(card, dai_link, rtd);
1422 static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1424 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1425 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1426 const char *name = aux_dev->codec_name;
1428 rtd->component = soc_find_component(aux_dev->codec_of_node, name);
1429 if (!rtd->component) {
1430 if (aux_dev->codec_of_node)
1431 name = of_node_full_name(aux_dev->codec_of_node);
1433 dev_err(card->dev, "ASoC: %s not registered\n", name);
1434 return -EPROBE_DEFER;
1438 * Some places still reference rtd->codec, so we have to keep that
1439 * initialized if the component is a CODEC. Once all those references
1440 * have been removed, this code can be removed as well.
1442 rtd->codec = rtd->component->codec;
1447 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1449 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1450 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1453 ret = soc_probe_component(card, rtd->component);
1457 /* do machine specific initialization */
1458 if (aux_dev->init) {
1459 ret = aux_dev->init(rtd->component);
1461 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1462 aux_dev->name, ret);
1467 return soc_post_component_init(rtd, aux_dev->name);
1470 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1472 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1473 struct snd_soc_component *component = rtd->component;
1475 /* unregister the rtd device */
1476 if (rtd->dev_registered) {
1477 device_unregister(rtd->dev);
1478 rtd->dev_registered = 0;
1482 soc_remove_component(component);
1485 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1489 if (codec->cache_init)
1492 ret = snd_soc_cache_init(codec);
1495 "ASoC: Failed to set cache compression type: %d\n",
1499 codec->cache_init = 1;
1504 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1505 * @rtd: The runtime for which the DAI link format should be changed
1506 * @dai_fmt: The new DAI link format
1508 * This function updates the DAI link format for all DAIs connected to the DAI
1509 * link for the specified runtime.
1511 * Note: For setups with a static format set the dai_fmt field in the
1512 * corresponding snd_dai_link struct instead of using this function.
1514 * Returns 0 on success, otherwise a negative error code.
1516 int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1517 unsigned int dai_fmt)
1519 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1520 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1524 for (i = 0; i < rtd->num_codecs; i++) {
1525 struct snd_soc_dai *codec_dai = codec_dais[i];
1527 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1528 if (ret != 0 && ret != -ENOTSUPP) {
1529 dev_warn(codec_dai->dev,
1530 "ASoC: Failed to set DAI format: %d\n", ret);
1535 /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1536 if (cpu_dai->codec) {
1537 unsigned int inv_dai_fmt;
1539 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1540 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1541 case SND_SOC_DAIFMT_CBM_CFM:
1542 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1544 case SND_SOC_DAIFMT_CBM_CFS:
1545 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1547 case SND_SOC_DAIFMT_CBS_CFM:
1548 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1550 case SND_SOC_DAIFMT_CBS_CFS:
1551 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1555 dai_fmt = inv_dai_fmt;
1558 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1559 if (ret != 0 && ret != -ENOTSUPP) {
1560 dev_warn(cpu_dai->dev,
1561 "ASoC: Failed to set DAI format: %d\n", ret);
1567 EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1569 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1571 struct snd_soc_codec *codec;
1574 mutex_lock(&client_mutex);
1575 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1578 for (i = 0; i < card->num_links; i++) {
1579 ret = soc_bind_dai_link(card, i);
1584 /* bind aux_devs too */
1585 for (i = 0; i < card->num_aux_devs; i++) {
1586 ret = soc_bind_aux_dev(card, i);
1591 /* initialize the register cache for each available codec */
1592 list_for_each_entry(codec, &codec_list, list) {
1593 if (codec->cache_init)
1595 ret = snd_soc_init_codec_cache(codec);
1600 /* card bind complete so register a sound card */
1601 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1602 card->owner, 0, &card->snd_card);
1605 "ASoC: can't create sound card for card %s: %d\n",
1610 soc_init_card_debugfs(card);
1612 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1613 card->dapm.dev = card->dev;
1614 card->dapm.card = card;
1615 list_add(&card->dapm.list, &card->dapm_list);
1617 #ifdef CONFIG_DEBUG_FS
1618 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1621 #ifdef CONFIG_PM_SLEEP
1622 /* deferred resume work */
1623 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1626 if (card->dapm_widgets)
1627 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1628 card->num_dapm_widgets);
1630 if (card->of_dapm_widgets)
1631 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1632 card->num_of_dapm_widgets);
1634 /* initialise the sound card only once */
1636 ret = card->probe(card);
1638 goto card_probe_error;
1641 /* probe all components used by DAI links on this card */
1642 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1644 for (i = 0; i < card->num_links; i++) {
1645 ret = soc_probe_link_components(card, i, order);
1648 "ASoC: failed to instantiate card %d\n",
1655 /* probe all DAI links on this card */
1656 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1658 for (i = 0; i < card->num_links; i++) {
1659 ret = soc_probe_link_dais(card, i, order);
1662 "ASoC: failed to instantiate card %d\n",
1669 for (i = 0; i < card->num_aux_devs; i++) {
1670 ret = soc_probe_aux_dev(card, i);
1673 "ASoC: failed to add auxiliary devices %d\n",
1675 goto probe_aux_dev_err;
1679 snd_soc_dapm_link_dai_widgets(card);
1680 snd_soc_dapm_connect_dai_link_widgets(card);
1683 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1685 if (card->dapm_routes)
1686 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1687 card->num_dapm_routes);
1689 if (card->of_dapm_routes)
1690 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
1691 card->num_of_dapm_routes);
1693 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1695 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1696 "%s", card->long_name ? card->long_name : card->name);
1697 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1698 "%s", card->driver_name ? card->driver_name : card->name);
1699 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1700 switch (card->snd_card->driver[i]) {
1706 if (!isalnum(card->snd_card->driver[i]))
1707 card->snd_card->driver[i] = '_';
1712 if (card->late_probe) {
1713 ret = card->late_probe(card);
1715 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
1717 goto probe_aux_dev_err;
1721 snd_soc_dapm_new_widgets(card);
1723 ret = snd_card_register(card->snd_card);
1725 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1727 goto probe_aux_dev_err;
1730 card->instantiated = 1;
1731 dapm_mark_endpoints_dirty(card);
1732 snd_soc_dapm_sync(&card->dapm);
1733 mutex_unlock(&card->mutex);
1734 mutex_unlock(&client_mutex);
1739 for (i = 0; i < card->num_aux_devs; i++)
1740 soc_remove_aux_dev(card, i);
1743 soc_remove_dai_links(card);
1749 snd_soc_dapm_free(&card->dapm);
1750 soc_cleanup_card_debugfs(card);
1751 snd_card_free(card->snd_card);
1754 mutex_unlock(&card->mutex);
1755 mutex_unlock(&client_mutex);
1760 /* probes a new socdev */
1761 static int soc_probe(struct platform_device *pdev)
1763 struct snd_soc_card *card = platform_get_drvdata(pdev);
1766 * no card, so machine driver should be registering card
1767 * we should not be here in that case so ret error
1772 dev_warn(&pdev->dev,
1773 "ASoC: machine %s should use snd_soc_register_card()\n",
1776 /* Bodge while we unpick instantiation */
1777 card->dev = &pdev->dev;
1779 return snd_soc_register_card(card);
1782 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1786 /* make sure any delayed work runs */
1787 for (i = 0; i < card->num_rtd; i++) {
1788 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1789 flush_delayed_work(&rtd->delayed_work);
1792 /* remove auxiliary devices */
1793 for (i = 0; i < card->num_aux_devs; i++)
1794 soc_remove_aux_dev(card, i);
1796 /* free the ALSA card at first; this syncs with pending operations */
1797 snd_card_free(card->snd_card);
1799 /* remove and free each DAI */
1800 soc_remove_dai_links(card);
1802 soc_cleanup_card_debugfs(card);
1804 /* remove the card */
1808 snd_soc_dapm_free(&card->dapm);
1813 /* removes a socdev */
1814 static int soc_remove(struct platform_device *pdev)
1816 struct snd_soc_card *card = platform_get_drvdata(pdev);
1818 snd_soc_unregister_card(card);
1822 int snd_soc_poweroff(struct device *dev)
1824 struct snd_soc_card *card = dev_get_drvdata(dev);
1827 if (!card->instantiated)
1830 /* Flush out pmdown_time work - we actually do want to run it
1831 * now, we're shutting down so no imminent restart. */
1832 for (i = 0; i < card->num_rtd; i++) {
1833 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1834 flush_delayed_work(&rtd->delayed_work);
1837 snd_soc_dapm_shutdown(card);
1839 /* deactivate pins to sleep state */
1840 for (i = 0; i < card->num_rtd; i++) {
1841 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1842 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1845 pinctrl_pm_select_sleep_state(cpu_dai->dev);
1846 for (j = 0; j < rtd->num_codecs; j++) {
1847 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
1848 pinctrl_pm_select_sleep_state(codec_dai->dev);
1854 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1856 const struct dev_pm_ops snd_soc_pm_ops = {
1857 .suspend = snd_soc_suspend,
1858 .resume = snd_soc_resume,
1859 .freeze = snd_soc_suspend,
1860 .thaw = snd_soc_resume,
1861 .poweroff = snd_soc_poweroff,
1862 .restore = snd_soc_resume,
1864 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1866 /* ASoC platform driver */
1867 static struct platform_driver soc_driver = {
1869 .name = "soc-audio",
1870 .pm = &snd_soc_pm_ops,
1873 .remove = soc_remove,
1877 * snd_soc_cnew - create new control
1878 * @_template: control template
1879 * @data: control private data
1880 * @long_name: control long name
1881 * @prefix: control name prefix
1883 * Create a new mixer control from a template control.
1885 * Returns 0 for success, else error.
1887 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1888 void *data, const char *long_name,
1891 struct snd_kcontrol_new template;
1892 struct snd_kcontrol *kcontrol;
1895 memcpy(&template, _template, sizeof(template));
1899 long_name = template.name;
1902 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
1906 template.name = name;
1908 template.name = long_name;
1911 kcontrol = snd_ctl_new1(&template, data);
1917 EXPORT_SYMBOL_GPL(snd_soc_cnew);
1919 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
1920 const struct snd_kcontrol_new *controls, int num_controls,
1921 const char *prefix, void *data)
1925 for (i = 0; i < num_controls; i++) {
1926 const struct snd_kcontrol_new *control = &controls[i];
1927 err = snd_ctl_add(card, snd_soc_cnew(control, data,
1928 control->name, prefix));
1930 dev_err(dev, "ASoC: Failed to add %s: %d\n",
1931 control->name, err);
1939 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
1942 struct snd_card *card = soc_card->snd_card;
1943 struct snd_kcontrol *kctl;
1945 if (unlikely(!name))
1948 list_for_each_entry(kctl, &card->controls, list)
1949 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
1953 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
1956 * snd_soc_add_component_controls - Add an array of controls to a component.
1958 * @component: Component to add controls to
1959 * @controls: Array of controls to add
1960 * @num_controls: Number of elements in the array
1962 * Return: 0 for success, else error.
1964 int snd_soc_add_component_controls(struct snd_soc_component *component,
1965 const struct snd_kcontrol_new *controls, unsigned int num_controls)
1967 struct snd_card *card = component->card->snd_card;
1969 return snd_soc_add_controls(card, component->dev, controls,
1970 num_controls, component->name_prefix, component);
1972 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
1975 * snd_soc_add_codec_controls - add an array of controls to a codec.
1976 * Convenience function to add a list of controls. Many codecs were
1977 * duplicating this code.
1979 * @codec: codec to add controls to
1980 * @controls: array of controls to add
1981 * @num_controls: number of elements in the array
1983 * Return 0 for success, else error.
1985 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
1986 const struct snd_kcontrol_new *controls, unsigned int num_controls)
1988 return snd_soc_add_component_controls(&codec->component, controls,
1991 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
1994 * snd_soc_add_platform_controls - add an array of controls to a platform.
1995 * Convenience function to add a list of controls.
1997 * @platform: platform to add controls to
1998 * @controls: array of controls to add
1999 * @num_controls: number of elements in the array
2001 * Return 0 for success, else error.
2003 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2004 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2006 return snd_soc_add_component_controls(&platform->component, controls,
2009 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2012 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2013 * Convenience function to add a list of controls.
2015 * @soc_card: SoC card to add controls to
2016 * @controls: array of controls to add
2017 * @num_controls: number of elements in the array
2019 * Return 0 for success, else error.
2021 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2022 const struct snd_kcontrol_new *controls, int num_controls)
2024 struct snd_card *card = soc_card->snd_card;
2026 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2029 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2032 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2033 * Convienience function to add a list of controls.
2035 * @dai: DAI to add controls to
2036 * @controls: array of controls to add
2037 * @num_controls: number of elements in the array
2039 * Return 0 for success, else error.
2041 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2042 const struct snd_kcontrol_new *controls, int num_controls)
2044 struct snd_card *card = dai->component->card->snd_card;
2046 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2049 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2052 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2054 * @clk_id: DAI specific clock ID
2055 * @freq: new clock frequency in Hz
2056 * @dir: new clock direction - input/output.
2058 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2060 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2061 unsigned int freq, int dir)
2063 if (dai->driver && dai->driver->ops->set_sysclk)
2064 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2065 else if (dai->codec && dai->codec->driver->set_sysclk)
2066 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
2071 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2074 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2076 * @clk_id: DAI specific clock ID
2077 * @source: Source for the clock
2078 * @freq: new clock frequency in Hz
2079 * @dir: new clock direction - input/output.
2081 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2083 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2084 int source, unsigned int freq, int dir)
2086 if (codec->driver->set_sysclk)
2087 return codec->driver->set_sysclk(codec, clk_id, source,
2092 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2095 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2097 * @div_id: DAI specific clock divider ID
2098 * @div: new clock divisor.
2100 * Configures the clock dividers. This is used to derive the best DAI bit and
2101 * frame clocks from the system or master clock. It's best to set the DAI bit
2102 * and frame clocks as low as possible to save system power.
2104 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2105 int div_id, int div)
2107 if (dai->driver && dai->driver->ops->set_clkdiv)
2108 return dai->driver->ops->set_clkdiv(dai, div_id, div);
2112 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2115 * snd_soc_dai_set_pll - configure DAI PLL.
2117 * @pll_id: DAI specific PLL ID
2118 * @source: DAI specific source for the PLL
2119 * @freq_in: PLL input clock frequency in Hz
2120 * @freq_out: requested PLL output clock frequency in Hz
2122 * Configures and enables PLL to generate output clock based on input clock.
2124 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2125 unsigned int freq_in, unsigned int freq_out)
2127 if (dai->driver && dai->driver->ops->set_pll)
2128 return dai->driver->ops->set_pll(dai, pll_id, source,
2130 else if (dai->codec && dai->codec->driver->set_pll)
2131 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2136 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2139 * snd_soc_codec_set_pll - configure codec PLL.
2141 * @pll_id: DAI specific PLL ID
2142 * @source: DAI specific source for the PLL
2143 * @freq_in: PLL input clock frequency in Hz
2144 * @freq_out: requested PLL output clock frequency in Hz
2146 * Configures and enables PLL to generate output clock based on input clock.
2148 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2149 unsigned int freq_in, unsigned int freq_out)
2151 if (codec->driver->set_pll)
2152 return codec->driver->set_pll(codec, pll_id, source,
2157 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2160 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2162 * @ratio: Ratio of BCLK to Sample rate.
2164 * Configures the DAI for a preset BCLK to sample rate ratio.
2166 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2168 if (dai->driver && dai->driver->ops->set_bclk_ratio)
2169 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2173 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2176 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2178 * @fmt: SND_SOC_DAIFMT_ format value.
2180 * Configures the DAI hardware format and clocking.
2182 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2184 if (dai->driver == NULL)
2186 if (dai->driver->ops->set_fmt == NULL)
2188 return dai->driver->ops->set_fmt(dai, fmt);
2190 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2193 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
2194 * @slots: Number of slots in use.
2195 * @tx_mask: bitmask representing active TX slots.
2196 * @rx_mask: bitmask representing active RX slots.
2198 * Generates the TDM tx and rx slot default masks for DAI.
2200 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
2201 unsigned int *tx_mask,
2202 unsigned int *rx_mask)
2204 if (*tx_mask || *rx_mask)
2210 *tx_mask = (1 << slots) - 1;
2211 *rx_mask = (1 << slots) - 1;
2217 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2218 * @dai: The DAI to configure
2219 * @tx_mask: bitmask representing active TX slots.
2220 * @rx_mask: bitmask representing active RX slots.
2221 * @slots: Number of slots in use.
2222 * @slot_width: Width in bits for each slot.
2224 * This function configures the specified DAI for TDM operation. @slot contains
2225 * the total number of slots of the TDM stream and @slot_with the width of each
2226 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2227 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2228 * DAI should write to or read from. If a bit is set the corresponding slot is
2229 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2230 * the first slot, bit 1 to the second slot and so on. The first active slot
2231 * maps to the first channel of the DAI, the second active slot to the second
2232 * channel and so on.
2234 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2235 * @rx_mask and @slot_width will be ignored.
2237 * Returns 0 on success, a negative error code otherwise.
2239 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2240 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2242 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2243 dai->driver->ops->xlate_tdm_slot_mask(slots,
2244 &tx_mask, &rx_mask);
2246 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
2248 dai->tx_mask = tx_mask;
2249 dai->rx_mask = rx_mask;
2251 if (dai->driver && dai->driver->ops->set_tdm_slot)
2252 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2257 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2260 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2262 * @tx_num: how many TX channels
2263 * @tx_slot: pointer to an array which imply the TX slot number channel
2265 * @rx_num: how many RX channels
2266 * @rx_slot: pointer to an array which imply the RX slot number channel
2269 * configure the relationship between channel number and TDM slot number.
2271 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2272 unsigned int tx_num, unsigned int *tx_slot,
2273 unsigned int rx_num, unsigned int *rx_slot)
2275 if (dai->driver && dai->driver->ops->set_channel_map)
2276 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2281 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2284 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2286 * @tristate: tristate enable
2288 * Tristates the DAI so that others can use it.
2290 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2292 if (dai->driver && dai->driver->ops->set_tristate)
2293 return dai->driver->ops->set_tristate(dai, tristate);
2297 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2300 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2302 * @mute: mute enable
2303 * @direction: stream to mute
2305 * Mutes the DAI DAC.
2307 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2313 if (dai->driver->ops->mute_stream)
2314 return dai->driver->ops->mute_stream(dai, mute, direction);
2315 else if (dai->driver->ops->digital_mute)
2316 return dai->driver->ops->digital_mute(dai, mute);
2320 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2322 static int snd_soc_init_multicodec(struct snd_soc_card *card,
2323 struct snd_soc_dai_link *dai_link)
2325 /* Legacy codec/codec_dai link is a single entry in multicodec */
2326 if (dai_link->codec_name || dai_link->codec_of_node ||
2327 dai_link->codec_dai_name) {
2328 dai_link->num_codecs = 1;
2330 dai_link->codecs = devm_kzalloc(card->dev,
2331 sizeof(struct snd_soc_dai_link_component),
2333 if (!dai_link->codecs)
2336 dai_link->codecs[0].name = dai_link->codec_name;
2337 dai_link->codecs[0].of_node = dai_link->codec_of_node;
2338 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
2341 if (!dai_link->codecs) {
2342 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
2350 * snd_soc_register_card - Register a card with the ASoC core
2352 * @card: Card to register
2355 int snd_soc_register_card(struct snd_soc_card *card)
2359 if (!card->name || !card->dev)
2362 for (i = 0; i < card->num_links; i++) {
2363 struct snd_soc_dai_link *link = &card->dai_link[i];
2365 ret = snd_soc_init_multicodec(card, link);
2367 dev_err(card->dev, "ASoC: failed to init multicodec\n");
2371 for (j = 0; j < link->num_codecs; j++) {
2373 * Codec must be specified by 1 of name or OF node,
2374 * not both or neither.
2376 if (!!link->codecs[j].name ==
2377 !!link->codecs[j].of_node) {
2378 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
2382 /* Codec DAI name must be specified */
2383 if (!link->codecs[j].dai_name) {
2384 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
2391 * Platform may be specified by either name or OF node, but
2392 * can be left unspecified, and a dummy platform will be used.
2394 if (link->platform_name && link->platform_of_node) {
2396 "ASoC: Both platform name/of_node are set for %s\n",
2402 * CPU device may be specified by either name or OF node, but
2403 * can be left unspecified, and will be matched based on DAI
2406 if (link->cpu_name && link->cpu_of_node) {
2408 "ASoC: Neither/both cpu name/of_node are set for %s\n",
2413 * At least one of CPU DAI name or CPU device name/node must be
2416 if (!link->cpu_dai_name &&
2417 !(link->cpu_name || link->cpu_of_node)) {
2419 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
2425 dev_set_drvdata(card->dev, card);
2427 snd_soc_initialize_card_lists(card);
2429 card->rtd = devm_kzalloc(card->dev,
2430 sizeof(struct snd_soc_pcm_runtime) *
2431 (card->num_links + card->num_aux_devs),
2433 if (card->rtd == NULL)
2436 card->rtd_aux = &card->rtd[card->num_links];
2438 for (i = 0; i < card->num_links; i++) {
2439 card->rtd[i].card = card;
2440 card->rtd[i].dai_link = &card->dai_link[i];
2441 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
2442 sizeof(struct snd_soc_dai *) *
2443 (card->rtd[i].dai_link->num_codecs),
2445 if (card->rtd[i].codec_dais == NULL)
2449 for (i = 0; i < card->num_aux_devs; i++)
2450 card->rtd_aux[i].card = card;
2452 INIT_LIST_HEAD(&card->dapm_dirty);
2453 INIT_LIST_HEAD(&card->dobj_list);
2454 card->instantiated = 0;
2455 mutex_init(&card->mutex);
2456 mutex_init(&card->dapm_mutex);
2457 mutex_init(&card->dapm_power_mutex);
2459 ret = snd_soc_instantiate_card(card);
2463 /* deactivate pins to sleep state */
2464 for (i = 0; i < card->num_rtd; i++) {
2465 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2466 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2469 for (j = 0; j < rtd->num_codecs; j++) {
2470 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2471 if (!codec_dai->active)
2472 pinctrl_pm_select_sleep_state(codec_dai->dev);
2475 if (!cpu_dai->active)
2476 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2481 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2484 * snd_soc_unregister_card - Unregister a card with the ASoC core
2486 * @card: Card to unregister
2489 int snd_soc_unregister_card(struct snd_soc_card *card)
2491 if (card->instantiated) {
2492 card->instantiated = false;
2493 snd_soc_dapm_shutdown(card);
2494 soc_cleanup_card_resources(card);
2495 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2500 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2503 * Simplify DAI link configuration by removing ".-1" from device names
2504 * and sanitizing names.
2506 static char *fmt_single_name(struct device *dev, int *id)
2508 char *found, name[NAME_SIZE];
2511 if (dev_name(dev) == NULL)
2514 strlcpy(name, dev_name(dev), NAME_SIZE);
2516 /* are we a "%s.%d" name (platform and SPI components) */
2517 found = strstr(name, dev->driver->name);
2520 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2522 /* discard ID from name if ID == -1 */
2524 found[strlen(dev->driver->name)] = '\0';
2528 /* I2C component devices are named "bus-addr" */
2529 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2530 char tmp[NAME_SIZE];
2532 /* create unique ID number from I2C addr and bus */
2533 *id = ((id1 & 0xffff) << 16) + id2;
2535 /* sanitize component name for DAI link creation */
2536 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2537 strlcpy(name, tmp, NAME_SIZE);
2542 return kstrdup(name, GFP_KERNEL);
2546 * Simplify DAI link naming for single devices with multiple DAIs by removing
2547 * any ".-1" and using the DAI name (instead of device name).
2549 static inline char *fmt_multiple_name(struct device *dev,
2550 struct snd_soc_dai_driver *dai_drv)
2552 if (dai_drv->name == NULL) {
2554 "ASoC: error - multiple DAI %s registered with no name\n",
2559 return kstrdup(dai_drv->name, GFP_KERNEL);
2563 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2565 * @component: The component for which the DAIs should be unregistered
2567 static void snd_soc_unregister_dais(struct snd_soc_component *component)
2569 struct snd_soc_dai *dai, *_dai;
2571 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
2572 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2574 list_del(&dai->list);
2581 * snd_soc_register_dais - Register a DAI with the ASoC core
2583 * @component: The component the DAIs are registered for
2584 * @dai_drv: DAI driver to use for the DAIs
2585 * @count: Number of DAIs
2586 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
2589 static int snd_soc_register_dais(struct snd_soc_component *component,
2590 struct snd_soc_dai_driver *dai_drv, size_t count,
2591 bool legacy_dai_naming)
2593 struct device *dev = component->dev;
2594 struct snd_soc_dai *dai;
2598 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
2600 component->dai_drv = dai_drv;
2601 component->num_dai = count;
2603 for (i = 0; i < count; i++) {
2605 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2612 * Back in the old days when we still had component-less DAIs,
2613 * instead of having a static name, component-less DAIs would
2614 * inherit the name of the parent device so it is possible to
2615 * register multiple instances of the DAI. We still need to keep
2616 * the same naming style even though those DAIs are not
2617 * component-less anymore.
2619 if (count == 1 && legacy_dai_naming &&
2620 (dai_drv[i].id == 0 || dai_drv[i].name == NULL)) {
2621 dai->name = fmt_single_name(dev, &dai->id);
2623 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
2625 dai->id = dai_drv[i].id;
2629 if (dai->name == NULL) {
2635 dai->component = component;
2637 dai->driver = &dai_drv[i];
2638 if (!dai->driver->ops)
2639 dai->driver->ops = &null_dai_ops;
2641 list_add(&dai->list, &component->dai_list);
2643 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2649 snd_soc_unregister_dais(component);
2654 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
2655 enum snd_soc_dapm_type type, int subseq)
2657 struct snd_soc_component *component = dapm->component;
2659 component->driver->seq_notifier(component, type, subseq);
2662 static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
2665 struct snd_soc_component *component = dapm->component;
2667 return component->driver->stream_event(component, event);
2670 static int snd_soc_component_initialize(struct snd_soc_component *component,
2671 const struct snd_soc_component_driver *driver, struct device *dev)
2673 struct snd_soc_dapm_context *dapm;
2675 component->name = fmt_single_name(dev, &component->id);
2676 if (!component->name) {
2677 dev_err(dev, "ASoC: Failed to allocate name\n");
2681 component->dev = dev;
2682 component->driver = driver;
2683 component->probe = component->driver->probe;
2684 component->remove = component->driver->remove;
2686 dapm = &component->dapm;
2688 dapm->component = component;
2689 dapm->bias_level = SND_SOC_BIAS_OFF;
2690 dapm->idle_bias_off = true;
2691 if (driver->seq_notifier)
2692 dapm->seq_notifier = snd_soc_component_seq_notifier;
2693 if (driver->stream_event)
2694 dapm->stream_event = snd_soc_component_stream_event;
2696 component->controls = driver->controls;
2697 component->num_controls = driver->num_controls;
2698 component->dapm_widgets = driver->dapm_widgets;
2699 component->num_dapm_widgets = driver->num_dapm_widgets;
2700 component->dapm_routes = driver->dapm_routes;
2701 component->num_dapm_routes = driver->num_dapm_routes;
2703 INIT_LIST_HEAD(&component->dai_list);
2704 mutex_init(&component->io_mutex);
2709 static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2711 int val_bytes = regmap_get_val_bytes(component->regmap);
2713 /* Errors are legitimate for non-integer byte multiples */
2715 component->val_bytes = val_bytes;
2718 #ifdef CONFIG_REGMAP
2721 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
2722 * @component: The component for which to initialize the regmap instance
2723 * @regmap: The regmap instance that should be used by the component
2725 * This function allows deferred assignment of the regmap instance that is
2726 * associated with the component. Only use this if the regmap instance is not
2727 * yet ready when the component is registered. The function must also be called
2728 * before the first IO attempt of the component.
2730 void snd_soc_component_init_regmap(struct snd_soc_component *component,
2731 struct regmap *regmap)
2733 component->regmap = regmap;
2734 snd_soc_component_setup_regmap(component);
2736 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
2739 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
2740 * @component: The component for which to de-initialize the regmap instance
2742 * Calls regmap_exit() on the regmap instance associated to the component and
2743 * removes the regmap instance from the component.
2745 * This function should only be used if snd_soc_component_init_regmap() was used
2746 * to initialize the regmap instance.
2748 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
2750 regmap_exit(component->regmap);
2751 component->regmap = NULL;
2753 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2757 static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
2759 if (!component->write && !component->read) {
2760 if (!component->regmap)
2761 component->regmap = dev_get_regmap(component->dev, NULL);
2762 if (component->regmap)
2763 snd_soc_component_setup_regmap(component);
2766 list_add(&component->list, &component_list);
2767 INIT_LIST_HEAD(&component->dobj_list);
2770 static void snd_soc_component_add(struct snd_soc_component *component)
2772 mutex_lock(&client_mutex);
2773 snd_soc_component_add_unlocked(component);
2774 mutex_unlock(&client_mutex);
2777 static void snd_soc_component_cleanup(struct snd_soc_component *component)
2779 snd_soc_unregister_dais(component);
2780 kfree(component->name);
2783 static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2785 list_del(&component->list);
2788 int snd_soc_register_component(struct device *dev,
2789 const struct snd_soc_component_driver *cmpnt_drv,
2790 struct snd_soc_dai_driver *dai_drv,
2793 struct snd_soc_component *cmpnt;
2796 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
2798 dev_err(dev, "ASoC: Failed to allocate memory\n");
2802 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
2806 cmpnt->ignore_pmdown_time = true;
2807 cmpnt->registered_as_component = true;
2809 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
2811 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
2815 snd_soc_component_add(cmpnt);
2820 snd_soc_component_cleanup(cmpnt);
2825 EXPORT_SYMBOL_GPL(snd_soc_register_component);
2828 * snd_soc_unregister_component - Unregister a component from the ASoC core
2830 * @dev: The device to unregister
2832 void snd_soc_unregister_component(struct device *dev)
2834 struct snd_soc_component *cmpnt;
2836 mutex_lock(&client_mutex);
2837 list_for_each_entry(cmpnt, &component_list, list) {
2838 if (dev == cmpnt->dev && cmpnt->registered_as_component)
2841 mutex_unlock(&client_mutex);
2845 snd_soc_tplg_component_remove(cmpnt, SND_SOC_TPLG_INDEX_ALL);
2846 snd_soc_component_del_unlocked(cmpnt);
2847 mutex_unlock(&client_mutex);
2848 snd_soc_component_cleanup(cmpnt);
2851 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2853 static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
2855 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
2857 return platform->driver->probe(platform);
2860 static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
2862 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
2864 platform->driver->remove(platform);
2868 * snd_soc_add_platform - Add a platform to the ASoC core
2869 * @dev: The parent device for the platform
2870 * @platform: The platform to add
2871 * @platform_drv: The driver for the platform
2873 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
2874 const struct snd_soc_platform_driver *platform_drv)
2878 ret = snd_soc_component_initialize(&platform->component,
2879 &platform_drv->component_driver, dev);
2883 platform->dev = dev;
2884 platform->driver = platform_drv;
2886 if (platform_drv->probe)
2887 platform->component.probe = snd_soc_platform_drv_probe;
2888 if (platform_drv->remove)
2889 platform->component.remove = snd_soc_platform_drv_remove;
2891 #ifdef CONFIG_DEBUG_FS
2892 platform->component.debugfs_prefix = "platform";
2895 mutex_lock(&client_mutex);
2896 snd_soc_component_add_unlocked(&platform->component);
2897 list_add(&platform->list, &platform_list);
2898 mutex_unlock(&client_mutex);
2900 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
2901 platform->component.name);
2905 EXPORT_SYMBOL_GPL(snd_soc_add_platform);
2908 * snd_soc_register_platform - Register a platform with the ASoC core
2910 * @dev: The device for the platform
2911 * @platform_drv: The driver for the platform
2913 int snd_soc_register_platform(struct device *dev,
2914 const struct snd_soc_platform_driver *platform_drv)
2916 struct snd_soc_platform *platform;
2919 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
2921 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
2922 if (platform == NULL)
2925 ret = snd_soc_add_platform(dev, platform, platform_drv);
2931 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2934 * snd_soc_remove_platform - Remove a platform from the ASoC core
2935 * @platform: the platform to remove
2937 void snd_soc_remove_platform(struct snd_soc_platform *platform)
2940 mutex_lock(&client_mutex);
2941 list_del(&platform->list);
2942 snd_soc_component_del_unlocked(&platform->component);
2943 mutex_unlock(&client_mutex);
2945 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
2946 platform->component.name);
2948 snd_soc_component_cleanup(&platform->component);
2950 EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
2952 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
2954 struct snd_soc_platform *platform;
2956 mutex_lock(&client_mutex);
2957 list_for_each_entry(platform, &platform_list, list) {
2958 if (dev == platform->dev) {
2959 mutex_unlock(&client_mutex);
2963 mutex_unlock(&client_mutex);
2967 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
2970 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2972 * @dev: platform to unregister
2974 void snd_soc_unregister_platform(struct device *dev)
2976 struct snd_soc_platform *platform;
2978 platform = snd_soc_lookup_platform(dev);
2982 snd_soc_remove_platform(platform);
2985 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2987 static u64 codec_format_map[] = {
2988 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2989 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2990 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2991 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2992 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2993 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2994 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2995 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2996 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2997 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2998 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2999 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3000 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3001 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3002 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3003 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3006 /* Fix up the DAI formats for endianness: codecs don't actually see
3007 * the endianness of the data but we're using the CPU format
3008 * definitions which do need to include endianness so we ensure that
3009 * codec DAIs always have both big and little endian variants set.
3011 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3015 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3016 if (stream->formats & codec_format_map[i])
3017 stream->formats |= codec_format_map[i];
3020 static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3022 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3024 return codec->driver->probe(codec);
3027 static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3029 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3031 codec->driver->remove(codec);
3034 static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3035 unsigned int reg, unsigned int val)
3037 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3039 return codec->driver->write(codec, reg, val);
3042 static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3043 unsigned int reg, unsigned int *val)
3045 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3047 *val = codec->driver->read(codec, reg);
3052 static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
3053 enum snd_soc_bias_level level)
3055 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
3057 return codec->driver->set_bias_level(codec, level);
3061 * snd_soc_card_change_online_state - Mark if soc card is online/offline
3063 * @soc_card : soc_card to mark
3065 void snd_soc_card_change_online_state(struct snd_soc_card *soc_card, int online)
3067 if (soc_card && soc_card->snd_card)
3068 snd_card_change_online_state(soc_card->snd_card, online);
3070 EXPORT_SYMBOL(snd_soc_card_change_online_state);
3073 * snd_soc_register_codec - Register a codec with the ASoC core
3075 * @dev: The parent device for this codec
3076 * @codec_drv: Codec driver
3077 * @dai_drv: The associated DAI driver
3078 * @num_dai: Number of DAIs
3080 int snd_soc_register_codec(struct device *dev,
3081 const struct snd_soc_codec_driver *codec_drv,
3082 struct snd_soc_dai_driver *dai_drv,
3085 struct snd_soc_dapm_context *dapm;
3086 struct snd_soc_codec *codec;
3087 struct snd_soc_dai *dai;
3090 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3092 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3096 codec->component.codec = codec;
3098 ret = snd_soc_component_initialize(&codec->component,
3099 &codec_drv->component_driver, dev);
3103 if (codec_drv->controls) {
3104 codec->component.controls = codec_drv->controls;
3105 codec->component.num_controls = codec_drv->num_controls;
3107 if (codec_drv->dapm_widgets) {
3108 codec->component.dapm_widgets = codec_drv->dapm_widgets;
3109 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
3111 if (codec_drv->dapm_routes) {
3112 codec->component.dapm_routes = codec_drv->dapm_routes;
3113 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
3116 if (codec_drv->probe)
3117 codec->component.probe = snd_soc_codec_drv_probe;
3118 if (codec_drv->remove)
3119 codec->component.remove = snd_soc_codec_drv_remove;
3120 if (codec_drv->write)
3121 codec->component.write = snd_soc_codec_drv_write;
3122 if (codec_drv->read)
3123 codec->component.read = snd_soc_codec_drv_read;
3124 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
3126 dapm = snd_soc_codec_get_dapm(codec);
3127 dapm->idle_bias_off = codec_drv->idle_bias_off;
3128 dapm->suspend_bias_off = codec_drv->suspend_bias_off;
3129 if (codec_drv->seq_notifier)
3130 dapm->seq_notifier = codec_drv->seq_notifier;
3131 if (codec_drv->set_bias_level)
3132 dapm->set_bias_level = snd_soc_codec_set_bias_level;
3134 codec->driver = codec_drv;
3135 codec->component.val_bytes = codec_drv->reg_word_size;
3137 #ifdef CONFIG_DEBUG_FS
3138 codec->component.init_debugfs = soc_init_codec_debugfs;
3139 codec->component.debugfs_prefix = "codec";
3142 if (codec_drv->get_regmap)
3143 codec->component.regmap = codec_drv->get_regmap(dev);
3145 for (i = 0; i < num_dai; i++) {
3146 fixup_codec_formats(&dai_drv[i].playback);
3147 fixup_codec_formats(&dai_drv[i].capture);
3150 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3152 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3156 list_for_each_entry(dai, &codec->component.dai_list, list)
3159 mutex_lock(&client_mutex);
3160 snd_soc_component_add_unlocked(&codec->component);
3161 list_add(&codec->list, &codec_list);
3162 mutex_unlock(&client_mutex);
3164 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3165 codec->component.name);
3169 snd_soc_component_cleanup(&codec->component);
3174 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3177 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3179 * @dev: codec to unregister
3181 void snd_soc_unregister_codec(struct device *dev)
3183 struct snd_soc_codec *codec;
3185 mutex_lock(&client_mutex);
3186 list_for_each_entry(codec, &codec_list, list) {
3187 if (dev == codec->dev)
3190 mutex_unlock(&client_mutex);
3194 list_del(&codec->list);
3195 snd_soc_component_del_unlocked(&codec->component);
3196 mutex_unlock(&client_mutex);
3198 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3199 codec->component.name);
3201 snd_soc_component_cleanup(&codec->component);
3202 snd_soc_cache_exit(codec);
3205 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3207 /* Retrieve a card's name from device tree */
3208 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3209 const char *propname)
3211 struct device_node *np;
3215 pr_err("card->dev is not set before calling %s\n", __func__);
3219 np = card->dev->of_node;
3221 ret = of_property_read_string_index(np, propname, 0, &card->name);
3223 * EINVAL means the property does not exist. This is fine providing
3224 * card->name was previously set, which is checked later in
3225 * snd_soc_register_card.
3227 if (ret < 0 && ret != -EINVAL) {
3229 "ASoC: Property '%s' could not be read: %d\n",
3236 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3238 static const struct snd_soc_dapm_widget simple_widgets[] = {
3239 SND_SOC_DAPM_MIC("Microphone", NULL),
3240 SND_SOC_DAPM_LINE("Line", NULL),
3241 SND_SOC_DAPM_HP("Headphone", NULL),
3242 SND_SOC_DAPM_SPK("Speaker", NULL),
3245 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
3246 const char *propname)
3248 struct device_node *np = card->dev->of_node;
3249 struct snd_soc_dapm_widget *widgets;
3250 const char *template, *wname;
3251 int i, j, num_widgets, ret;
3253 num_widgets = of_property_count_strings(np, propname);
3254 if (num_widgets < 0) {
3256 "ASoC: Property '%s' does not exist\n", propname);
3259 if (num_widgets & 1) {
3261 "ASoC: Property '%s' length is not even\n", propname);
3267 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3272 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3276 "ASoC: Could not allocate memory for widgets\n");
3280 for (i = 0; i < num_widgets; i++) {
3281 ret = of_property_read_string_index(np, propname,
3285 "ASoC: Property '%s' index %d read error:%d\n",
3286 propname, 2 * i, ret);
3290 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3291 if (!strncmp(template, simple_widgets[j].name,
3292 strlen(simple_widgets[j].name))) {
3293 widgets[i] = simple_widgets[j];
3298 if (j >= ARRAY_SIZE(simple_widgets)) {
3300 "ASoC: DAPM widget '%s' is not supported\n",
3305 ret = of_property_read_string_index(np, propname,
3310 "ASoC: Property '%s' index %d read error:%d\n",
3311 propname, (2 * i) + 1, ret);
3315 widgets[i].name = wname;
3318 card->of_dapm_widgets = widgets;
3319 card->num_of_dapm_widgets = num_widgets;
3323 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
3325 static int snd_soc_of_get_slot_mask(struct device_node *np,
3326 const char *prop_name,
3330 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
3336 for (i = 0; i < val; i++)
3337 if (be32_to_cpup(&of_slot_mask[i]))
3343 int snd_soc_of_parse_tdm_slot(struct device_node *np,
3344 unsigned int *tx_mask,
3345 unsigned int *rx_mask,
3346 unsigned int *slots,
3347 unsigned int *slot_width)
3353 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3355 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3357 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3358 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3366 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3367 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3377 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3379 void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
3380 struct snd_soc_codec_conf *codec_conf,
3381 struct device_node *of_node,
3382 const char *propname)
3384 struct device_node *np = card->dev->of_node;
3388 ret = of_property_read_string(np, propname, &str);
3390 /* no prefix is not error */
3394 codec_conf->of_node = of_node;
3395 codec_conf->name_prefix = str;
3397 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
3399 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3400 const char *propname)
3402 struct device_node *np = card->dev->of_node;
3404 struct snd_soc_dapm_route *routes;
3407 num_routes = of_property_count_strings(np, propname);
3408 if (num_routes < 0 || num_routes & 1) {
3410 "ASoC: Property '%s' does not exist or its length is not even\n",
3416 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3421 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3425 "ASoC: Could not allocate DAPM route table\n");
3429 for (i = 0; i < num_routes; i++) {
3430 ret = of_property_read_string_index(np, propname,
3431 2 * i, &routes[i].sink);
3434 "ASoC: Property '%s' index %d could not be read: %d\n",
3435 propname, 2 * i, ret);
3438 ret = of_property_read_string_index(np, propname,
3439 (2 * i) + 1, &routes[i].source);
3442 "ASoC: Property '%s' index %d could not be read: %d\n",
3443 propname, (2 * i) + 1, ret);
3448 card->num_of_dapm_routes = num_routes;
3449 card->of_dapm_routes = routes;
3453 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3455 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3457 struct device_node **bitclkmaster,
3458 struct device_node **framemaster)
3462 unsigned int format = 0;
3468 } of_fmt_table[] = {
3469 { "i2s", SND_SOC_DAIFMT_I2S },
3470 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3471 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3472 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3473 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3474 { "ac97", SND_SOC_DAIFMT_AC97 },
3475 { "pdm", SND_SOC_DAIFMT_PDM},
3476 { "msb", SND_SOC_DAIFMT_MSB },
3477 { "lsb", SND_SOC_DAIFMT_LSB },
3484 * check "[prefix]format = xxx"
3485 * SND_SOC_DAIFMT_FORMAT_MASK area
3487 snprintf(prop, sizeof(prop), "%sformat", prefix);
3488 ret = of_property_read_string(np, prop, &str);
3490 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3491 if (strcmp(str, of_fmt_table[i].name) == 0) {
3492 format |= of_fmt_table[i].val;
3499 * check "[prefix]continuous-clock"
3500 * SND_SOC_DAIFMT_CLOCK_MASK area
3502 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
3503 if (of_get_property(np, prop, NULL))
3504 format |= SND_SOC_DAIFMT_CONT;
3506 format |= SND_SOC_DAIFMT_GATED;
3509 * check "[prefix]bitclock-inversion"
3510 * check "[prefix]frame-inversion"
3511 * SND_SOC_DAIFMT_INV_MASK area
3513 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3514 bit = !!of_get_property(np, prop, NULL);
3516 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3517 frame = !!of_get_property(np, prop, NULL);
3519 switch ((bit << 4) + frame) {
3521 format |= SND_SOC_DAIFMT_IB_IF;
3524 format |= SND_SOC_DAIFMT_IB_NF;
3527 format |= SND_SOC_DAIFMT_NB_IF;
3530 /* SND_SOC_DAIFMT_NB_NF is default */
3535 * check "[prefix]bitclock-master"
3536 * check "[prefix]frame-master"
3537 * SND_SOC_DAIFMT_MASTER_MASK area
3539 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3540 bit = !!of_get_property(np, prop, NULL);
3541 if (bit && bitclkmaster)
3542 *bitclkmaster = of_parse_phandle(np, prop, 0);
3544 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3545 frame = !!of_get_property(np, prop, NULL);
3546 if (frame && framemaster)
3547 *framemaster = of_parse_phandle(np, prop, 0);
3549 switch ((bit << 4) + frame) {
3551 format |= SND_SOC_DAIFMT_CBM_CFM;
3554 format |= SND_SOC_DAIFMT_CBM_CFS;
3557 format |= SND_SOC_DAIFMT_CBS_CFM;
3560 format |= SND_SOC_DAIFMT_CBS_CFS;
3566 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3568 static int snd_soc_get_dai_name(struct of_phandle_args *args,
3569 const char **dai_name)
3571 struct snd_soc_component *pos;
3572 struct device_node *component_of_node;
3573 int ret = -EPROBE_DEFER;
3575 mutex_lock(&client_mutex);
3576 list_for_each_entry(pos, &component_list, list) {
3577 component_of_node = pos->dev->of_node;
3578 if (!component_of_node && pos->dev->parent)
3579 component_of_node = pos->dev->parent->of_node;
3581 if (component_of_node != args->np)
3584 if (pos->driver->of_xlate_dai_name) {
3585 ret = pos->driver->of_xlate_dai_name(pos,
3591 switch (args->args_count) {
3593 id = 0; /* same as dai_drv[0] */
3603 if (id < 0 || id >= pos->num_dai) {
3610 *dai_name = pos->dai_drv[id].name;
3612 *dai_name = pos->name;
3617 mutex_unlock(&client_mutex);
3622 * snd_soc_info_multi_ext - external single mixer info callback
3623 * @kcontrol: mixer control
3624 * @uinfo: control element information
3626 * Callback to provide information about a single external mixer control.
3627 * that accepts multiple input.
3629 * Returns 0 for success.
3631 int snd_soc_info_multi_ext(struct snd_kcontrol *kcontrol,
3632 struct snd_ctl_elem_info *uinfo)
3634 struct soc_multi_mixer_control *mc =
3635 (struct soc_multi_mixer_control *)kcontrol->private_value;
3638 if (!mc->platform_max)
3639 mc->platform_max = mc->max;
3640 platform_max = mc->platform_max;
3642 if (platform_max == 1 && !strnstr(kcontrol->id.name, " Volume", 30))
3643 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3645 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3647 uinfo->count = mc->count;
3648 uinfo->value.integer.min = 0;
3649 uinfo->value.integer.max = platform_max;
3652 EXPORT_SYMBOL_GPL(snd_soc_info_multi_ext);
3655 * snd_soc_dai_get_channel_map - configure DAI audio channel map
3657 * @tx_num: how many TX channels
3658 * @tx_slot: pointer to an array which imply the TX slot number channel
3660 * @rx_num: how many RX channels
3661 * @rx_slot: pointer to an array which imply the RX slot number channel
3664 * configure the relationship between channel number and TDM slot number.
3666 int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
3667 unsigned int *tx_num, unsigned int *tx_slot,
3668 unsigned int *rx_num, unsigned int *rx_slot)
3670 if (dai->driver && dai->driver->ops->get_channel_map)
3671 return dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
3676 EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
3678 int snd_soc_of_get_dai_name(struct device_node *of_node,
3679 const char **dai_name)
3681 struct of_phandle_args args;
3684 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3685 "#sound-dai-cells", 0, &args);
3689 ret = snd_soc_get_dai_name(&args, dai_name);
3691 of_node_put(args.np);
3695 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3698 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3700 * @of_node: Device node
3701 * @dai_link: DAI link
3703 * Builds an array of CODEC DAI components from the DAI link property
3705 * The array is set in the DAI link and the number of DAIs is set accordingly.
3706 * The device nodes in the array (of_node) must be dereferenced by the caller.
3708 * Returns 0 for success
3710 int snd_soc_of_get_dai_link_codecs(struct device *dev,
3711 struct device_node *of_node,
3712 struct snd_soc_dai_link *dai_link)
3714 struct of_phandle_args args;
3715 struct snd_soc_dai_link_component *component;
3717 int index, num_codecs, ret;
3719 /* Count the number of CODECs */
3721 num_codecs = of_count_phandle_with_args(of_node, name,
3722 "#sound-dai-cells");
3723 if (num_codecs <= 0) {
3724 if (num_codecs == -ENOENT)
3725 dev_err(dev, "No 'sound-dai' property\n");
3727 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3730 component = devm_kzalloc(dev,
3731 sizeof *component * num_codecs,
3735 dai_link->codecs = component;
3736 dai_link->num_codecs = num_codecs;
3738 /* Parse the list */
3739 for (index = 0, component = dai_link->codecs;
3740 index < dai_link->num_codecs;
3741 index++, component++) {
3742 ret = of_parse_phandle_with_args(of_node, name,
3747 component->of_node = args.np;
3748 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3754 for (index = 0, component = dai_link->codecs;
3755 index < dai_link->num_codecs;
3756 index++, component++) {
3757 if (!component->of_node)
3759 of_node_put(component->of_node);
3760 component->of_node = NULL;
3762 dai_link->codecs = NULL;
3763 dai_link->num_codecs = 0;
3766 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3768 static int __init snd_soc_init(void)
3770 snd_soc_debugfs_init();
3771 snd_soc_util_init();
3773 return platform_driver_register(&soc_driver);
3775 module_init(snd_soc_init);
3777 static void __exit snd_soc_exit(void)
3779 snd_soc_util_exit();
3780 snd_soc_debugfs_exit();
3782 #ifdef CONFIG_DEBUG_FS
3784 platform_driver_unregister(&soc_driver);
3786 module_exit(snd_soc_exit);
3788 /* Module information */
3789 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3790 MODULE_DESCRIPTION("ALSA SoC Core");
3791 MODULE_LICENSE("GPL");
3792 MODULE_ALIAS("platform:soc-audio");