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/initval.h>
45 #define CREATE_TRACE_POINTS
46 #include <trace/events/asoc.h>
50 #ifdef CONFIG_DEBUG_FS
51 struct dentry *snd_soc_debugfs_root;
52 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
55 static DEFINE_MUTEX(client_mutex);
56 static LIST_HEAD(platform_list);
57 static LIST_HEAD(codec_list);
58 static LIST_HEAD(component_list);
61 * This is a timeout to do a DAPM powerdown after a stream is closed().
62 * It can be used to eliminate pops between different playback streams, e.g.
63 * between two audio tracks.
65 static int pmdown_time = 5000;
66 module_param(pmdown_time, int, 0);
67 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
69 /* returns the minimum number of bytes needed to represent
70 * a particular given value */
71 static int min_bytes_needed(unsigned long val)
76 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
79 c = (sizeof val * 8) - c;
87 /* fill buf which is 'len' bytes with a formatted
88 * string of the form 'reg: value\n' */
89 static int format_register_str(struct snd_soc_codec *codec,
90 unsigned int reg, char *buf, size_t len)
92 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
93 int regsize = codec->driver->reg_word_size * 2;
96 char regbuf[regsize + 1];
98 /* since tmpbuf is allocated on the stack, warn the callers if they
99 * try to abuse this function */
102 /* +2 for ': ' and + 1 for '\n' */
103 if (wordsize + regsize + 2 + 1 != len)
106 ret = snd_soc_read(codec, reg);
108 memset(regbuf, 'X', regsize);
109 regbuf[regsize] = '\0';
111 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
114 /* prepare the buffer */
115 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
116 /* copy it back to the caller without the '\0' */
117 memcpy(buf, tmpbuf, len);
122 /* codec register dump */
123 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
124 size_t count, loff_t pos)
127 int wordsize, regsize;
132 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
133 regsize = codec->driver->reg_word_size * 2;
135 len = wordsize + regsize + 2 + 1;
137 if (!codec->driver->reg_cache_size)
140 if (codec->driver->reg_cache_step)
141 step = codec->driver->reg_cache_step;
143 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
144 /* only support larger than PAGE_SIZE bytes debugfs
145 * entries for the default case */
147 if (total + len >= count - 1)
149 format_register_str(codec, i, buf + total, len);
155 total = min(total, count - 1);
160 static ssize_t codec_reg_show(struct device *dev,
161 struct device_attribute *attr, char *buf)
163 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
165 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
168 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
170 static ssize_t pmdown_time_show(struct device *dev,
171 struct device_attribute *attr, char *buf)
173 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
175 return sprintf(buf, "%ld\n", rtd->pmdown_time);
178 static ssize_t pmdown_time_set(struct device *dev,
179 struct device_attribute *attr,
180 const char *buf, size_t count)
182 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
185 ret = kstrtol(buf, 10, &rtd->pmdown_time);
192 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
194 static struct attribute *soc_dev_attrs[] = {
195 &dev_attr_codec_reg.attr,
196 &dev_attr_pmdown_time.attr,
200 static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
201 struct attribute *attr, int idx)
203 struct device *dev = kobj_to_dev(kobj);
204 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
206 if (attr == &dev_attr_pmdown_time.attr)
207 return attr->mode; /* always visible */
208 return rtd->codec ? attr->mode : 0; /* enabled only with codec */
211 static const struct attribute_group soc_dapm_dev_group = {
212 .attrs = soc_dapm_dev_attrs,
213 .is_visible = soc_dev_attr_is_visible,
216 static const struct attribute_group soc_dev_roup = {
217 .attrs = soc_dev_attrs,
218 .is_visible = soc_dev_attr_is_visible,
221 static const struct attribute_group *soc_dev_attr_groups[] = {
227 #ifdef CONFIG_DEBUG_FS
228 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
229 size_t count, loff_t *ppos)
232 struct snd_soc_codec *codec = file->private_data;
235 if (*ppos < 0 || !count)
238 buf = kmalloc(count, GFP_KERNEL);
242 ret = soc_codec_reg_show(codec, buf, count, *ppos);
244 if (copy_to_user(user_buf, buf, ret)) {
255 static ssize_t codec_reg_write_file(struct file *file,
256 const char __user *user_buf, size_t count, loff_t *ppos)
261 unsigned long reg, value;
262 struct snd_soc_codec *codec = file->private_data;
265 buf_size = min(count, (sizeof(buf)-1));
266 if (copy_from_user(buf, user_buf, buf_size))
270 while (*start == ' ')
272 reg = simple_strtoul(start, &start, 16);
273 while (*start == ' ')
275 ret = kstrtoul(start, 16, &value);
279 /* Userspace has been fiddling around behind the kernel's back */
280 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
282 snd_soc_write(codec, reg, value);
286 static const struct file_operations codec_reg_fops = {
288 .read = codec_reg_read_file,
289 .write = codec_reg_write_file,
290 .llseek = default_llseek,
293 static void soc_init_component_debugfs(struct snd_soc_component *component)
295 if (component->debugfs_prefix) {
298 name = kasprintf(GFP_KERNEL, "%s:%s",
299 component->debugfs_prefix, component->name);
301 component->debugfs_root = debugfs_create_dir(name,
302 component->card->debugfs_card_root);
306 component->debugfs_root = debugfs_create_dir(component->name,
307 component->card->debugfs_card_root);
310 if (!component->debugfs_root) {
311 dev_warn(component->dev,
312 "ASoC: Failed to create component debugfs directory\n");
316 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
317 component->debugfs_root);
319 if (component->init_debugfs)
320 component->init_debugfs(component);
323 static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
325 debugfs_remove_recursive(component->debugfs_root);
328 static void soc_init_codec_debugfs(struct snd_soc_component *component)
330 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
332 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
333 codec->component.debugfs_root,
334 codec, &codec_reg_fops);
335 if (!codec->debugfs_reg)
337 "ASoC: Failed to create codec register debugfs file\n");
340 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
341 size_t count, loff_t *ppos)
343 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
344 ssize_t len, ret = 0;
345 struct snd_soc_codec *codec;
350 mutex_lock(&client_mutex);
352 list_for_each_entry(codec, &codec_list, list) {
353 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
354 codec->component.name);
357 if (ret > PAGE_SIZE) {
363 mutex_unlock(&client_mutex);
366 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
373 static const struct file_operations codec_list_fops = {
374 .read = codec_list_read_file,
375 .llseek = default_llseek,/* read accesses f_pos */
378 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
379 size_t count, loff_t *ppos)
381 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
382 ssize_t len, ret = 0;
383 struct snd_soc_component *component;
384 struct snd_soc_dai *dai;
389 mutex_lock(&client_mutex);
391 list_for_each_entry(component, &component_list, list) {
392 list_for_each_entry(dai, &component->dai_list, list) {
393 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
397 if (ret > PAGE_SIZE) {
404 mutex_unlock(&client_mutex);
406 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
413 static const struct file_operations dai_list_fops = {
414 .read = dai_list_read_file,
415 .llseek = default_llseek,/* read accesses f_pos */
418 static ssize_t platform_list_read_file(struct file *file,
419 char __user *user_buf,
420 size_t count, loff_t *ppos)
422 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
423 ssize_t len, ret = 0;
424 struct snd_soc_platform *platform;
429 mutex_lock(&client_mutex);
431 list_for_each_entry(platform, &platform_list, list) {
432 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
433 platform->component.name);
436 if (ret > PAGE_SIZE) {
442 mutex_unlock(&client_mutex);
444 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
451 static const struct file_operations platform_list_fops = {
452 .read = platform_list_read_file,
453 .llseek = default_llseek,/* read accesses f_pos */
456 static void soc_init_card_debugfs(struct snd_soc_card *card)
458 card->debugfs_card_root = debugfs_create_dir(card->name,
459 snd_soc_debugfs_root);
460 if (!card->debugfs_card_root) {
462 "ASoC: Failed to create card debugfs directory\n");
466 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
467 card->debugfs_card_root,
469 if (!card->debugfs_pop_time)
471 "ASoC: Failed to create pop time debugfs file\n");
474 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
476 debugfs_remove_recursive(card->debugfs_card_root);
481 #define soc_init_codec_debugfs NULL
483 static inline void soc_init_component_debugfs(
484 struct snd_soc_component *component)
488 static inline void soc_cleanup_component_debugfs(
489 struct snd_soc_component *component)
493 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
497 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
502 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
503 const char *dai_link, int stream)
507 for (i = 0; i < card->num_links; i++) {
508 if (card->rtd[i].dai_link->no_pcm &&
509 !strcmp(card->rtd[i].dai_link->name, dai_link))
510 return card->rtd[i].pcm->streams[stream].substream;
512 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
515 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
517 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
518 const char *dai_link)
522 for (i = 0; i < card->num_links; i++) {
523 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
524 return &card->rtd[i];
526 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
529 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
531 static void codec2codec_close_delayed_work(struct work_struct *work)
533 /* Currently nothing to do for c2c links
534 * Since c2c links are internal nodes in the DAPM graph and
535 * don't interface with the outside world or application layer
536 * we don't have to do any special handling on close.
540 #ifdef CONFIG_PM_SLEEP
541 /* powers down audio subsystem for suspend */
542 int snd_soc_suspend(struct device *dev)
544 struct snd_soc_card *card = dev_get_drvdata(dev);
545 struct snd_soc_codec *codec;
548 /* If the card is not initialized yet there is nothing to do */
549 if (!card->instantiated)
552 /* Due to the resume being scheduled into a workqueue we could
553 * suspend before that's finished - wait for it to complete.
555 snd_power_lock(card->snd_card);
556 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
557 snd_power_unlock(card->snd_card);
559 /* we're going to block userspace touching us until resume completes */
560 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
562 /* mute any active DACs */
563 for (i = 0; i < card->num_rtd; i++) {
565 if (card->rtd[i].dai_link->ignore_suspend)
568 for (j = 0; j < card->rtd[i].num_codecs; j++) {
569 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
570 struct snd_soc_dai_driver *drv = dai->driver;
572 if (drv->ops->digital_mute && dai->playback_active)
573 drv->ops->digital_mute(dai, 1);
577 /* suspend all pcms */
578 for (i = 0; i < card->num_rtd; i++) {
579 if (card->rtd[i].dai_link->ignore_suspend)
582 snd_pcm_suspend_all(card->rtd[i].pcm);
585 if (card->suspend_pre)
586 card->suspend_pre(card);
588 for (i = 0; i < card->num_rtd; i++) {
589 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
591 if (card->rtd[i].dai_link->ignore_suspend)
594 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
595 cpu_dai->driver->suspend(cpu_dai);
598 /* close any waiting streams */
599 for (i = 0; i < card->num_rtd; i++)
600 flush_delayed_work(&card->rtd[i].delayed_work);
602 for (i = 0; i < card->num_rtd; i++) {
604 if (card->rtd[i].dai_link->ignore_suspend)
607 snd_soc_dapm_stream_event(&card->rtd[i],
608 SNDRV_PCM_STREAM_PLAYBACK,
609 SND_SOC_DAPM_STREAM_SUSPEND);
611 snd_soc_dapm_stream_event(&card->rtd[i],
612 SNDRV_PCM_STREAM_CAPTURE,
613 SND_SOC_DAPM_STREAM_SUSPEND);
616 /* Recheck all endpoints too, their state is affected by suspend */
617 dapm_mark_endpoints_dirty(card);
618 snd_soc_dapm_sync(&card->dapm);
620 /* suspend all CODECs */
621 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
622 /* If there are paths active then the CODEC will be held with
623 * bias _ON and should not be suspended. */
624 if (!codec->suspended) {
625 switch (codec->dapm.bias_level) {
626 case SND_SOC_BIAS_STANDBY:
628 * If the CODEC is capable of idle
629 * bias off then being in STANDBY
630 * means it's doing something,
631 * otherwise fall through.
633 if (codec->dapm.idle_bias_off) {
635 "ASoC: idle_bias_off CODEC on over suspend\n");
639 case SND_SOC_BIAS_OFF:
640 if (codec->driver->suspend)
641 codec->driver->suspend(codec);
642 codec->suspended = 1;
643 if (codec->component.regmap)
644 regcache_mark_dirty(codec->component.regmap);
645 /* deactivate pins to sleep state */
646 pinctrl_pm_select_sleep_state(codec->dev);
650 "ASoC: CODEC is on over suspend\n");
656 for (i = 0; i < card->num_rtd; i++) {
657 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
659 if (card->rtd[i].dai_link->ignore_suspend)
662 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
663 cpu_dai->driver->suspend(cpu_dai);
665 /* deactivate pins to sleep state */
666 pinctrl_pm_select_sleep_state(cpu_dai->dev);
669 if (card->suspend_post)
670 card->suspend_post(card);
674 EXPORT_SYMBOL_GPL(snd_soc_suspend);
676 /* deferred resume work, so resume can complete before we finished
677 * setting our codec back up, which can be very slow on I2C
679 static void soc_resume_deferred(struct work_struct *work)
681 struct snd_soc_card *card =
682 container_of(work, struct snd_soc_card, deferred_resume_work);
683 struct snd_soc_codec *codec;
686 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
687 * so userspace apps are blocked from touching us
690 dev_dbg(card->dev, "ASoC: starting resume work\n");
692 /* Bring us up into D2 so that DAPM starts enabling things */
693 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
695 if (card->resume_pre)
696 card->resume_pre(card);
698 /* resume control bus DAIs */
699 for (i = 0; i < card->num_rtd; i++) {
700 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
702 if (card->rtd[i].dai_link->ignore_suspend)
705 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
706 cpu_dai->driver->resume(cpu_dai);
709 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
710 /* If the CODEC was idle over suspend then it will have been
711 * left with bias OFF or STANDBY and suspended so we must now
712 * resume. Otherwise the suspend was suppressed.
714 if (codec->suspended) {
715 switch (codec->dapm.bias_level) {
716 case SND_SOC_BIAS_STANDBY:
717 case SND_SOC_BIAS_OFF:
718 if (codec->driver->resume)
719 codec->driver->resume(codec);
720 codec->suspended = 0;
724 "ASoC: CODEC was on over suspend\n");
730 for (i = 0; i < card->num_rtd; i++) {
732 if (card->rtd[i].dai_link->ignore_suspend)
735 snd_soc_dapm_stream_event(&card->rtd[i],
736 SNDRV_PCM_STREAM_PLAYBACK,
737 SND_SOC_DAPM_STREAM_RESUME);
739 snd_soc_dapm_stream_event(&card->rtd[i],
740 SNDRV_PCM_STREAM_CAPTURE,
741 SND_SOC_DAPM_STREAM_RESUME);
744 /* unmute any active DACs */
745 for (i = 0; i < card->num_rtd; i++) {
747 if (card->rtd[i].dai_link->ignore_suspend)
750 for (j = 0; j < card->rtd[i].num_codecs; j++) {
751 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
752 struct snd_soc_dai_driver *drv = dai->driver;
754 if (drv->ops->digital_mute && dai->playback_active)
755 drv->ops->digital_mute(dai, 0);
759 for (i = 0; i < card->num_rtd; i++) {
760 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
762 if (card->rtd[i].dai_link->ignore_suspend)
765 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
766 cpu_dai->driver->resume(cpu_dai);
769 if (card->resume_post)
770 card->resume_post(card);
772 dev_dbg(card->dev, "ASoC: resume work completed\n");
774 /* userspace can access us now we are back as we were before */
775 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
777 /* Recheck all endpoints too, their state is affected by suspend */
778 dapm_mark_endpoints_dirty(card);
779 snd_soc_dapm_sync(&card->dapm);
782 /* powers up audio subsystem after a suspend */
783 int snd_soc_resume(struct device *dev)
785 struct snd_soc_card *card = dev_get_drvdata(dev);
786 bool bus_control = false;
789 /* If the card is not initialized yet there is nothing to do */
790 if (!card->instantiated)
793 /* activate pins from sleep state */
794 for (i = 0; i < card->num_rtd; i++) {
795 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
796 struct snd_soc_dai **codec_dais = rtd->codec_dais;
797 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
801 pinctrl_pm_select_default_state(cpu_dai->dev);
803 for (j = 0; j < rtd->num_codecs; j++) {
804 struct snd_soc_dai *codec_dai = codec_dais[j];
805 if (codec_dai->active)
806 pinctrl_pm_select_default_state(codec_dai->dev);
811 * DAIs that also act as the control bus master might have other drivers
812 * hanging off them so need to resume immediately. Other drivers don't
813 * have that problem and may take a substantial amount of time to resume
814 * due to I/O costs and anti-pop so handle them out of line.
816 for (i = 0; i < card->num_rtd; i++) {
817 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
818 bus_control |= cpu_dai->driver->bus_control;
821 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
822 soc_resume_deferred(&card->deferred_resume_work);
824 dev_dbg(dev, "ASoC: Scheduling resume work\n");
825 if (!schedule_work(&card->deferred_resume_work))
826 dev_err(dev, "ASoC: resume work item may be lost\n");
831 EXPORT_SYMBOL_GPL(snd_soc_resume);
833 #define snd_soc_suspend NULL
834 #define snd_soc_resume NULL
837 static const struct snd_soc_dai_ops null_dai_ops = {
840 static struct snd_soc_component *soc_find_component(
841 const struct device_node *of_node, const char *name)
843 struct snd_soc_component *component;
845 lockdep_assert_held(&client_mutex);
847 list_for_each_entry(component, &component_list, list) {
849 if (component->dev->of_node == of_node)
851 } else if (strcmp(component->name, name) == 0) {
859 static struct snd_soc_dai *snd_soc_find_dai(
860 const struct snd_soc_dai_link_component *dlc)
862 struct snd_soc_component *component;
863 struct snd_soc_dai *dai;
865 lockdep_assert_held(&client_mutex);
867 /* Find CPU DAI from registered DAIs*/
868 list_for_each_entry(component, &component_list, list) {
869 if (dlc->of_node && component->dev->of_node != dlc->of_node)
871 if (dlc->name && strcmp(component->name, dlc->name))
873 list_for_each_entry(dai, &component->dai_list, list) {
874 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
884 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
886 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
887 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
888 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
889 struct snd_soc_dai_link_component cpu_dai_component;
890 struct snd_soc_dai **codec_dais = rtd->codec_dais;
891 struct snd_soc_platform *platform;
892 const char *platform_name;
895 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
897 cpu_dai_component.name = dai_link->cpu_name;
898 cpu_dai_component.of_node = dai_link->cpu_of_node;
899 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
900 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
902 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
903 dai_link->cpu_dai_name);
904 return -EPROBE_DEFER;
907 rtd->num_codecs = dai_link->num_codecs;
909 /* Find CODEC from registered CODECs */
910 for (i = 0; i < rtd->num_codecs; i++) {
911 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
912 if (!codec_dais[i]) {
913 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
915 return -EPROBE_DEFER;
919 /* Single codec links expect codec and codec_dai in runtime data */
920 rtd->codec_dai = codec_dais[0];
921 rtd->codec = rtd->codec_dai->codec;
923 /* if there's no platform we match on the empty platform */
924 platform_name = dai_link->platform_name;
925 if (!platform_name && !dai_link->platform_of_node)
926 platform_name = "snd-soc-dummy";
928 /* find one from the set of registered platforms */
929 list_for_each_entry(platform, &platform_list, list) {
930 if (dai_link->platform_of_node) {
931 if (platform->dev->of_node !=
932 dai_link->platform_of_node)
935 if (strcmp(platform->component.name, platform_name))
939 rtd->platform = platform;
941 if (!rtd->platform) {
942 dev_err(card->dev, "ASoC: platform %s not registered\n",
943 dai_link->platform_name);
944 return -EPROBE_DEFER;
952 static void soc_remove_component(struct snd_soc_component *component)
954 if (!component->probed)
957 /* This is a HACK and will be removed soon */
958 if (component->codec)
959 list_del(&component->codec->card_list);
961 if (component->remove)
962 component->remove(component);
964 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
966 soc_cleanup_component_debugfs(component);
967 component->probed = 0;
968 module_put(component->dev->driver->owner);
971 static void soc_remove_dai(struct snd_soc_dai *dai, int order)
975 if (dai && dai->probed &&
976 dai->driver->remove_order == order) {
977 if (dai->driver->remove) {
978 err = dai->driver->remove(dai);
981 "ASoC: failed to remove %s: %d\n",
988 static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
990 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
993 /* unregister the rtd device */
994 if (rtd->dev_registered) {
995 device_unregister(rtd->dev);
996 rtd->dev_registered = 0;
999 /* remove the CODEC DAI */
1000 for (i = 0; i < rtd->num_codecs; i++)
1001 soc_remove_dai(rtd->codec_dais[i], order);
1003 soc_remove_dai(rtd->cpu_dai, order);
1006 static void soc_remove_link_components(struct snd_soc_card *card, int num,
1009 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1010 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1011 struct snd_soc_platform *platform = rtd->platform;
1012 struct snd_soc_component *component;
1015 /* remove the platform */
1016 if (platform && platform->component.driver->remove_order == order)
1017 soc_remove_component(&platform->component);
1019 /* remove the CODEC-side CODEC */
1020 for (i = 0; i < rtd->num_codecs; i++) {
1021 component = rtd->codec_dais[i]->component;
1022 if (component->driver->remove_order == order)
1023 soc_remove_component(component);
1026 /* remove any CPU-side CODEC */
1028 if (cpu_dai->component->driver->remove_order == order)
1029 soc_remove_component(cpu_dai->component);
1033 static void soc_remove_dai_links(struct snd_soc_card *card)
1037 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1039 for (dai = 0; dai < card->num_rtd; dai++)
1040 soc_remove_link_dais(card, dai, order);
1043 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1045 for (dai = 0; dai < card->num_rtd; dai++)
1046 soc_remove_link_components(card, dai, order);
1052 static void soc_set_name_prefix(struct snd_soc_card *card,
1053 struct snd_soc_component *component)
1057 if (card->codec_conf == NULL)
1060 for (i = 0; i < card->num_configs; i++) {
1061 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1062 if (map->of_node && component->dev->of_node != map->of_node)
1064 if (map->dev_name && strcmp(component->name, map->dev_name))
1066 component->name_prefix = map->name_prefix;
1071 static int soc_probe_component(struct snd_soc_card *card,
1072 struct snd_soc_component *component)
1074 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1075 struct snd_soc_dai *dai;
1078 if (component->probed)
1081 component->card = card;
1083 soc_set_name_prefix(card, component);
1085 if (!try_module_get(component->dev->driver->owner))
1088 soc_init_component_debugfs(component);
1090 if (component->dapm_widgets) {
1091 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1092 component->num_dapm_widgets);
1095 dev_err(component->dev,
1096 "Failed to create new controls %d\n", ret);
1101 list_for_each_entry(dai, &component->dai_list, list) {
1102 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1104 dev_err(component->dev,
1105 "Failed to create DAI widgets %d\n", ret);
1110 if (component->probe) {
1111 ret = component->probe(component);
1113 dev_err(component->dev,
1114 "ASoC: failed to probe component %d\n", ret);
1118 WARN(dapm->idle_bias_off &&
1119 dapm->bias_level != SND_SOC_BIAS_OFF,
1120 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1124 if (component->controls)
1125 snd_soc_add_component_controls(component, component->controls,
1126 component->num_controls);
1127 if (component->dapm_routes)
1128 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1129 component->num_dapm_routes);
1131 component->probed = 1;
1132 list_add(&dapm->list, &card->dapm_list);
1134 /* This is a HACK and will be removed soon */
1135 if (component->codec)
1136 list_add(&component->codec->card_list, &card->codec_dev_list);
1141 soc_cleanup_component_debugfs(component);
1142 module_put(component->dev->driver->owner);
1147 static void rtd_release(struct device *dev)
1152 static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1157 /* register the rtd device */
1158 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1161 device_initialize(rtd->dev);
1162 rtd->dev->parent = rtd->card->dev;
1163 rtd->dev->release = rtd_release;
1164 rtd->dev->groups = soc_dev_attr_groups;
1165 dev_set_name(rtd->dev, "%s", name);
1166 dev_set_drvdata(rtd->dev, rtd);
1167 mutex_init(&rtd->pcm_mutex);
1168 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1169 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1170 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1171 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1172 ret = device_add(rtd->dev);
1174 /* calling put_device() here to free the rtd->dev */
1175 put_device(rtd->dev);
1176 dev_err(rtd->card->dev,
1177 "ASoC: failed to register runtime device: %d\n", ret);
1180 rtd->dev_registered = 1;
1184 static int soc_probe_link_components(struct snd_soc_card *card, int num,
1187 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1188 struct snd_soc_platform *platform = rtd->platform;
1189 struct snd_soc_component *component;
1192 /* probe the CPU-side component, if it is a CODEC */
1193 component = rtd->cpu_dai->component;
1194 if (component->driver->probe_order == order) {
1195 ret = soc_probe_component(card, component);
1200 /* probe the CODEC-side components */
1201 for (i = 0; i < rtd->num_codecs; i++) {
1202 component = rtd->codec_dais[i]->component;
1203 if (component->driver->probe_order == order) {
1204 ret = soc_probe_component(card, component);
1210 /* probe the platform */
1211 if (platform->component.driver->probe_order == order) {
1212 ret = soc_probe_component(card, &platform->component);
1220 static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1224 if (!dai->probed && dai->driver->probe_order == order) {
1225 if (dai->driver->probe) {
1226 ret = dai->driver->probe(dai);
1229 "ASoC: failed to probe DAI %s: %d\n",
1241 static int soc_link_dai_widgets(struct snd_soc_card *card,
1242 struct snd_soc_dai_link *dai_link,
1243 struct snd_soc_pcm_runtime *rtd)
1245 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1246 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1247 struct snd_soc_dapm_widget *play_w, *capture_w;
1250 if (rtd->num_codecs > 1)
1251 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1253 /* link the DAI widgets */
1254 play_w = codec_dai->playback_widget;
1255 capture_w = cpu_dai->capture_widget;
1256 if (play_w && capture_w) {
1257 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1258 dai_link->num_params, capture_w,
1261 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1262 play_w->name, capture_w->name, ret);
1267 play_w = cpu_dai->playback_widget;
1268 capture_w = codec_dai->capture_widget;
1269 if (play_w && capture_w) {
1270 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1271 dai_link->num_params, capture_w,
1274 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1275 play_w->name, capture_w->name, ret);
1283 static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1285 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1286 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1287 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1290 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1291 card->name, num, order);
1293 /* set default power off timeout */
1294 rtd->pmdown_time = pmdown_time;
1296 ret = soc_probe_dai(cpu_dai, order);
1300 /* probe the CODEC DAI */
1301 for (i = 0; i < rtd->num_codecs; i++) {
1302 ret = soc_probe_dai(rtd->codec_dais[i], order);
1307 /* complete DAI probe during last probe */
1308 if (order != SND_SOC_COMP_ORDER_LAST)
1311 /* do machine specific initialization */
1312 if (dai_link->init) {
1313 ret = dai_link->init(rtd);
1315 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1316 dai_link->name, ret);
1321 ret = soc_post_component_init(rtd, dai_link->name);
1325 #ifdef CONFIG_DEBUG_FS
1326 /* add DPCM sysfs entries */
1327 if (dai_link->dynamic) {
1328 ret = soc_dpcm_debugfs_add(rtd);
1331 "ASoC: failed to add dpcm sysfs entries: %d\n",
1338 if (cpu_dai->driver->compress_dai) {
1339 /*create compress_device"*/
1340 ret = soc_new_compress(rtd, num);
1342 dev_err(card->dev, "ASoC: can't create compress %s\n",
1343 dai_link->stream_name);
1348 if (!dai_link->params) {
1349 /* create the pcm */
1350 ret = soc_new_pcm(rtd, num);
1352 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1353 dai_link->stream_name, ret);
1357 INIT_DELAYED_WORK(&rtd->delayed_work,
1358 codec2codec_close_delayed_work);
1360 /* link the DAI widgets */
1361 ret = soc_link_dai_widgets(card, dai_link, rtd);
1370 static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1372 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1373 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1374 const char *name = aux_dev->codec_name;
1376 rtd->component = soc_find_component(aux_dev->codec_of_node, name);
1377 if (!rtd->component) {
1378 if (aux_dev->codec_of_node)
1379 name = of_node_full_name(aux_dev->codec_of_node);
1381 dev_err(card->dev, "ASoC: %s not registered\n", name);
1382 return -EPROBE_DEFER;
1386 * Some places still reference rtd->codec, so we have to keep that
1387 * initialized if the component is a CODEC. Once all those references
1388 * have been removed, this code can be removed as well.
1390 rtd->codec = rtd->component->codec;
1395 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1397 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1398 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1401 ret = soc_probe_component(card, rtd->component);
1405 /* do machine specific initialization */
1406 if (aux_dev->init) {
1407 ret = aux_dev->init(rtd->component);
1409 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1410 aux_dev->name, ret);
1415 return soc_post_component_init(rtd, aux_dev->name);
1418 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1420 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1421 struct snd_soc_component *component = rtd->component;
1423 /* unregister the rtd device */
1424 if (rtd->dev_registered) {
1425 device_unregister(rtd->dev);
1426 rtd->dev_registered = 0;
1429 if (component && component->probed)
1430 soc_remove_component(component);
1433 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1437 if (codec->cache_init)
1440 ret = snd_soc_cache_init(codec);
1443 "ASoC: Failed to set cache compression type: %d\n",
1447 codec->cache_init = 1;
1452 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1453 * @rtd: The runtime for which the DAI link format should be changed
1454 * @dai_fmt: The new DAI link format
1456 * This function updates the DAI link format for all DAIs connected to the DAI
1457 * link for the specified runtime.
1459 * Note: For setups with a static format set the dai_fmt field in the
1460 * corresponding snd_dai_link struct instead of using this function.
1462 * Returns 0 on success, otherwise a negative error code.
1464 int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1465 unsigned int dai_fmt)
1467 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1468 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1472 for (i = 0; i < rtd->num_codecs; i++) {
1473 struct snd_soc_dai *codec_dai = codec_dais[i];
1475 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1476 if (ret != 0 && ret != -ENOTSUPP) {
1477 dev_warn(codec_dai->dev,
1478 "ASoC: Failed to set DAI format: %d\n", ret);
1483 /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1484 if (cpu_dai->codec) {
1485 unsigned int inv_dai_fmt;
1487 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1488 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1489 case SND_SOC_DAIFMT_CBM_CFM:
1490 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1492 case SND_SOC_DAIFMT_CBM_CFS:
1493 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1495 case SND_SOC_DAIFMT_CBS_CFM:
1496 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1498 case SND_SOC_DAIFMT_CBS_CFS:
1499 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1503 dai_fmt = inv_dai_fmt;
1506 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1507 if (ret != 0 && ret != -ENOTSUPP) {
1508 dev_warn(cpu_dai->dev,
1509 "ASoC: Failed to set DAI format: %d\n", ret);
1515 EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1517 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1519 struct snd_soc_codec *codec;
1522 mutex_lock(&client_mutex);
1523 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1526 for (i = 0; i < card->num_links; i++) {
1527 ret = soc_bind_dai_link(card, i);
1532 /* bind aux_devs too */
1533 for (i = 0; i < card->num_aux_devs; i++) {
1534 ret = soc_bind_aux_dev(card, i);
1539 /* initialize the register cache for each available codec */
1540 list_for_each_entry(codec, &codec_list, list) {
1541 if (codec->cache_init)
1543 ret = snd_soc_init_codec_cache(codec);
1548 /* card bind complete so register a sound card */
1549 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1550 card->owner, 0, &card->snd_card);
1553 "ASoC: can't create sound card for card %s: %d\n",
1558 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1559 card->dapm.dev = card->dev;
1560 card->dapm.card = card;
1561 list_add(&card->dapm.list, &card->dapm_list);
1563 #ifdef CONFIG_DEBUG_FS
1564 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1567 #ifdef CONFIG_PM_SLEEP
1568 /* deferred resume work */
1569 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1572 if (card->dapm_widgets)
1573 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1574 card->num_dapm_widgets);
1576 if (card->of_dapm_widgets)
1577 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1578 card->num_of_dapm_widgets);
1580 /* initialise the sound card only once */
1582 ret = card->probe(card);
1584 goto card_probe_error;
1587 /* probe all components used by DAI links on this card */
1588 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1590 for (i = 0; i < card->num_links; i++) {
1591 ret = soc_probe_link_components(card, i, order);
1594 "ASoC: failed to instantiate card %d\n",
1601 /* probe all DAI links on this card */
1602 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1604 for (i = 0; i < card->num_links; i++) {
1605 ret = soc_probe_link_dais(card, i, order);
1608 "ASoC: failed to instantiate card %d\n",
1615 for (i = 0; i < card->num_aux_devs; i++) {
1616 ret = soc_probe_aux_dev(card, i);
1619 "ASoC: failed to add auxiliary devices %d\n",
1621 goto probe_aux_dev_err;
1625 snd_soc_dapm_link_dai_widgets(card);
1626 snd_soc_dapm_connect_dai_link_widgets(card);
1629 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1631 if (card->dapm_routes)
1632 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1633 card->num_dapm_routes);
1635 if (card->of_dapm_routes)
1636 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
1637 card->num_of_dapm_routes);
1639 for (i = 0; i < card->num_links; i++) {
1640 if (card->dai_link[i].dai_fmt)
1641 snd_soc_runtime_set_dai_fmt(&card->rtd[i],
1642 card->dai_link[i].dai_fmt);
1645 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1647 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1648 "%s", card->long_name ? card->long_name : card->name);
1649 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1650 "%s", card->driver_name ? card->driver_name : card->name);
1651 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1652 switch (card->snd_card->driver[i]) {
1658 if (!isalnum(card->snd_card->driver[i]))
1659 card->snd_card->driver[i] = '_';
1664 if (card->late_probe) {
1665 ret = card->late_probe(card);
1667 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
1669 goto probe_aux_dev_err;
1673 snd_soc_dapm_new_widgets(card);
1675 ret = snd_card_register(card->snd_card);
1677 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1679 goto probe_aux_dev_err;
1682 card->instantiated = 1;
1683 snd_soc_dapm_sync(&card->dapm);
1684 mutex_unlock(&card->mutex);
1685 mutex_unlock(&client_mutex);
1687 soc_init_card_debugfs(card);
1692 for (i = 0; i < card->num_aux_devs; i++)
1693 soc_remove_aux_dev(card, i);
1696 soc_remove_dai_links(card);
1702 snd_card_free(card->snd_card);
1705 mutex_unlock(&card->mutex);
1706 mutex_unlock(&client_mutex);
1711 /* probes a new socdev */
1712 static int soc_probe(struct platform_device *pdev)
1714 struct snd_soc_card *card = platform_get_drvdata(pdev);
1717 * no card, so machine driver should be registering card
1718 * we should not be here in that case so ret error
1723 dev_warn(&pdev->dev,
1724 "ASoC: machine %s should use snd_soc_register_card()\n",
1727 /* Bodge while we unpick instantiation */
1728 card->dev = &pdev->dev;
1730 return snd_soc_register_card(card);
1733 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1737 /* make sure any delayed work runs */
1738 for (i = 0; i < card->num_rtd; i++) {
1739 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1740 flush_delayed_work(&rtd->delayed_work);
1743 /* remove auxiliary devices */
1744 for (i = 0; i < card->num_aux_devs; i++)
1745 soc_remove_aux_dev(card, i);
1747 /* remove and free each DAI */
1748 soc_remove_dai_links(card);
1750 soc_cleanup_card_debugfs(card);
1752 /* remove the card */
1756 snd_soc_dapm_free(&card->dapm);
1758 snd_card_free(card->snd_card);
1763 /* removes a socdev */
1764 static int soc_remove(struct platform_device *pdev)
1766 struct snd_soc_card *card = platform_get_drvdata(pdev);
1768 snd_soc_unregister_card(card);
1772 int snd_soc_poweroff(struct device *dev)
1774 struct snd_soc_card *card = dev_get_drvdata(dev);
1777 if (!card->instantiated)
1780 /* Flush out pmdown_time work - we actually do want to run it
1781 * now, we're shutting down so no imminent restart. */
1782 for (i = 0; i < card->num_rtd; i++) {
1783 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1784 flush_delayed_work(&rtd->delayed_work);
1787 snd_soc_dapm_shutdown(card);
1789 /* deactivate pins to sleep state */
1790 for (i = 0; i < card->num_rtd; i++) {
1791 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1792 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1795 pinctrl_pm_select_sleep_state(cpu_dai->dev);
1796 for (j = 0; j < rtd->num_codecs; j++) {
1797 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
1798 pinctrl_pm_select_sleep_state(codec_dai->dev);
1804 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1806 const struct dev_pm_ops snd_soc_pm_ops = {
1807 .suspend = snd_soc_suspend,
1808 .resume = snd_soc_resume,
1809 .freeze = snd_soc_suspend,
1810 .thaw = snd_soc_resume,
1811 .poweroff = snd_soc_poweroff,
1812 .restore = snd_soc_resume,
1814 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1816 /* ASoC platform driver */
1817 static struct platform_driver soc_driver = {
1819 .name = "soc-audio",
1820 .pm = &snd_soc_pm_ops,
1823 .remove = soc_remove,
1827 * snd_soc_cnew - create new control
1828 * @_template: control template
1829 * @data: control private data
1830 * @long_name: control long name
1831 * @prefix: control name prefix
1833 * Create a new mixer control from a template control.
1835 * Returns 0 for success, else error.
1837 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1838 void *data, const char *long_name,
1841 struct snd_kcontrol_new template;
1842 struct snd_kcontrol *kcontrol;
1845 memcpy(&template, _template, sizeof(template));
1849 long_name = template.name;
1852 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
1856 template.name = name;
1858 template.name = long_name;
1861 kcontrol = snd_ctl_new1(&template, data);
1867 EXPORT_SYMBOL_GPL(snd_soc_cnew);
1869 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
1870 const struct snd_kcontrol_new *controls, int num_controls,
1871 const char *prefix, void *data)
1875 for (i = 0; i < num_controls; i++) {
1876 const struct snd_kcontrol_new *control = &controls[i];
1877 err = snd_ctl_add(card, snd_soc_cnew(control, data,
1878 control->name, prefix));
1880 dev_err(dev, "ASoC: Failed to add %s: %d\n",
1881 control->name, err);
1889 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
1892 struct snd_card *card = soc_card->snd_card;
1893 struct snd_kcontrol *kctl;
1895 if (unlikely(!name))
1898 list_for_each_entry(kctl, &card->controls, list)
1899 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
1903 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
1906 * snd_soc_add_component_controls - Add an array of controls to a component.
1908 * @component: Component to add controls to
1909 * @controls: Array of controls to add
1910 * @num_controls: Number of elements in the array
1912 * Return: 0 for success, else error.
1914 int snd_soc_add_component_controls(struct snd_soc_component *component,
1915 const struct snd_kcontrol_new *controls, unsigned int num_controls)
1917 struct snd_card *card = component->card->snd_card;
1919 return snd_soc_add_controls(card, component->dev, controls,
1920 num_controls, component->name_prefix, component);
1922 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
1925 * snd_soc_add_codec_controls - add an array of controls to a codec.
1926 * Convenience function to add a list of controls. Many codecs were
1927 * duplicating this code.
1929 * @codec: codec to add controls to
1930 * @controls: array of controls to add
1931 * @num_controls: number of elements in the array
1933 * Return 0 for success, else error.
1935 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
1936 const struct snd_kcontrol_new *controls, unsigned int num_controls)
1938 return snd_soc_add_component_controls(&codec->component, controls,
1941 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
1944 * snd_soc_add_platform_controls - add an array of controls to a platform.
1945 * Convenience function to add a list of controls.
1947 * @platform: platform to add controls to
1948 * @controls: array of controls to add
1949 * @num_controls: number of elements in the array
1951 * Return 0 for success, else error.
1953 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
1954 const struct snd_kcontrol_new *controls, unsigned int num_controls)
1956 return snd_soc_add_component_controls(&platform->component, controls,
1959 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
1962 * snd_soc_add_card_controls - add an array of controls to a SoC card.
1963 * Convenience function to add a list of controls.
1965 * @soc_card: SoC card to add controls to
1966 * @controls: array of controls to add
1967 * @num_controls: number of elements in the array
1969 * Return 0 for success, else error.
1971 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
1972 const struct snd_kcontrol_new *controls, int num_controls)
1974 struct snd_card *card = soc_card->snd_card;
1976 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
1979 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
1982 * snd_soc_add_dai_controls - add an array of controls to a DAI.
1983 * Convienience function to add a list of controls.
1985 * @dai: DAI to add controls to
1986 * @controls: array of controls to add
1987 * @num_controls: number of elements in the array
1989 * Return 0 for success, else error.
1991 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
1992 const struct snd_kcontrol_new *controls, int num_controls)
1994 struct snd_card *card = dai->component->card->snd_card;
1996 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
1999 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2002 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2004 * @clk_id: DAI specific clock ID
2005 * @freq: new clock frequency in Hz
2006 * @dir: new clock direction - input/output.
2008 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2010 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2011 unsigned int freq, int dir)
2013 if (dai->driver && dai->driver->ops->set_sysclk)
2014 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2015 else if (dai->codec && dai->codec->driver->set_sysclk)
2016 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
2021 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2024 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2026 * @clk_id: DAI specific clock ID
2027 * @source: Source for the clock
2028 * @freq: new clock frequency in Hz
2029 * @dir: new clock direction - input/output.
2031 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2033 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2034 int source, unsigned int freq, int dir)
2036 if (codec->driver->set_sysclk)
2037 return codec->driver->set_sysclk(codec, clk_id, source,
2042 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2045 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2047 * @div_id: DAI specific clock divider ID
2048 * @div: new clock divisor.
2050 * Configures the clock dividers. This is used to derive the best DAI bit and
2051 * frame clocks from the system or master clock. It's best to set the DAI bit
2052 * and frame clocks as low as possible to save system power.
2054 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2055 int div_id, int div)
2057 if (dai->driver && dai->driver->ops->set_clkdiv)
2058 return dai->driver->ops->set_clkdiv(dai, div_id, div);
2062 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2065 * snd_soc_dai_set_pll - configure DAI PLL.
2067 * @pll_id: DAI specific PLL ID
2068 * @source: DAI specific source for the PLL
2069 * @freq_in: PLL input clock frequency in Hz
2070 * @freq_out: requested PLL output clock frequency in Hz
2072 * Configures and enables PLL to generate output clock based on input clock.
2074 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2075 unsigned int freq_in, unsigned int freq_out)
2077 if (dai->driver && dai->driver->ops->set_pll)
2078 return dai->driver->ops->set_pll(dai, pll_id, source,
2080 else if (dai->codec && dai->codec->driver->set_pll)
2081 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2086 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2089 * snd_soc_codec_set_pll - configure codec PLL.
2091 * @pll_id: DAI specific PLL ID
2092 * @source: DAI specific source for the PLL
2093 * @freq_in: PLL input clock frequency in Hz
2094 * @freq_out: requested PLL output clock frequency in Hz
2096 * Configures and enables PLL to generate output clock based on input clock.
2098 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2099 unsigned int freq_in, unsigned int freq_out)
2101 if (codec->driver->set_pll)
2102 return codec->driver->set_pll(codec, pll_id, source,
2107 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2110 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2112 * @ratio Ratio of BCLK to Sample rate.
2114 * Configures the DAI for a preset BCLK to sample rate ratio.
2116 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2118 if (dai->driver && dai->driver->ops->set_bclk_ratio)
2119 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2123 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2126 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2128 * @fmt: SND_SOC_DAIFMT_ format value.
2130 * Configures the DAI hardware format and clocking.
2132 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2134 if (dai->driver == NULL)
2136 if (dai->driver->ops->set_fmt == NULL)
2138 return dai->driver->ops->set_fmt(dai, fmt);
2140 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2143 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
2144 * @slots: Number of slots in use.
2145 * @tx_mask: bitmask representing active TX slots.
2146 * @rx_mask: bitmask representing active RX slots.
2148 * Generates the TDM tx and rx slot default masks for DAI.
2150 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
2151 unsigned int *tx_mask,
2152 unsigned int *rx_mask)
2154 if (*tx_mask || *rx_mask)
2160 *tx_mask = (1 << slots) - 1;
2161 *rx_mask = (1 << slots) - 1;
2167 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2168 * @dai: The DAI to configure
2169 * @tx_mask: bitmask representing active TX slots.
2170 * @rx_mask: bitmask representing active RX slots.
2171 * @slots: Number of slots in use.
2172 * @slot_width: Width in bits for each slot.
2174 * This function configures the specified DAI for TDM operation. @slot contains
2175 * the total number of slots of the TDM stream and @slot_with the width of each
2176 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2177 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2178 * DAI should write to or read from. If a bit is set the corresponding slot is
2179 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2180 * the first slot, bit 1 to the second slot and so on. The first active slot
2181 * maps to the first channel of the DAI, the second active slot to the second
2182 * channel and so on.
2184 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2185 * @rx_mask and @slot_width will be ignored.
2187 * Returns 0 on success, a negative error code otherwise.
2189 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2190 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2192 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2193 dai->driver->ops->xlate_tdm_slot_mask(slots,
2194 &tx_mask, &rx_mask);
2196 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
2198 dai->tx_mask = tx_mask;
2199 dai->rx_mask = rx_mask;
2201 if (dai->driver && dai->driver->ops->set_tdm_slot)
2202 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2207 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2210 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2212 * @tx_num: how many TX channels
2213 * @tx_slot: pointer to an array which imply the TX slot number channel
2215 * @rx_num: how many RX channels
2216 * @rx_slot: pointer to an array which imply the RX slot number channel
2219 * configure the relationship between channel number and TDM slot number.
2221 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2222 unsigned int tx_num, unsigned int *tx_slot,
2223 unsigned int rx_num, unsigned int *rx_slot)
2225 if (dai->driver && dai->driver->ops->set_channel_map)
2226 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2231 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2234 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2236 * @tristate: tristate enable
2238 * Tristates the DAI so that others can use it.
2240 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2242 if (dai->driver && dai->driver->ops->set_tristate)
2243 return dai->driver->ops->set_tristate(dai, tristate);
2247 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2250 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2252 * @mute: mute enable
2253 * @direction: stream to mute
2255 * Mutes the DAI DAC.
2257 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2263 if (dai->driver->ops->mute_stream)
2264 return dai->driver->ops->mute_stream(dai, mute, direction);
2265 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2266 dai->driver->ops->digital_mute)
2267 return dai->driver->ops->digital_mute(dai, mute);
2271 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2273 static int snd_soc_init_multicodec(struct snd_soc_card *card,
2274 struct snd_soc_dai_link *dai_link)
2276 /* Legacy codec/codec_dai link is a single entry in multicodec */
2277 if (dai_link->codec_name || dai_link->codec_of_node ||
2278 dai_link->codec_dai_name) {
2279 dai_link->num_codecs = 1;
2281 dai_link->codecs = devm_kzalloc(card->dev,
2282 sizeof(struct snd_soc_dai_link_component),
2284 if (!dai_link->codecs)
2287 dai_link->codecs[0].name = dai_link->codec_name;
2288 dai_link->codecs[0].of_node = dai_link->codec_of_node;
2289 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
2292 if (!dai_link->codecs) {
2293 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
2301 * snd_soc_register_card - Register a card with the ASoC core
2303 * @card: Card to register
2306 int snd_soc_register_card(struct snd_soc_card *card)
2310 if (!card->name || !card->dev)
2313 for (i = 0; i < card->num_links; i++) {
2314 struct snd_soc_dai_link *link = &card->dai_link[i];
2316 ret = snd_soc_init_multicodec(card, link);
2318 dev_err(card->dev, "ASoC: failed to init multicodec\n");
2322 for (j = 0; j < link->num_codecs; j++) {
2324 * Codec must be specified by 1 of name or OF node,
2325 * not both or neither.
2327 if (!!link->codecs[j].name ==
2328 !!link->codecs[j].of_node) {
2329 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
2333 /* Codec DAI name must be specified */
2334 if (!link->codecs[j].dai_name) {
2335 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
2342 * Platform may be specified by either name or OF node, but
2343 * can be left unspecified, and a dummy platform will be used.
2345 if (link->platform_name && link->platform_of_node) {
2347 "ASoC: Both platform name/of_node are set for %s\n",
2353 * CPU device may be specified by either name or OF node, but
2354 * can be left unspecified, and will be matched based on DAI
2357 if (link->cpu_name && link->cpu_of_node) {
2359 "ASoC: Neither/both cpu name/of_node are set for %s\n",
2364 * At least one of CPU DAI name or CPU device name/node must be
2367 if (!link->cpu_dai_name &&
2368 !(link->cpu_name || link->cpu_of_node)) {
2370 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
2376 dev_set_drvdata(card->dev, card);
2378 snd_soc_initialize_card_lists(card);
2380 card->rtd = devm_kzalloc(card->dev,
2381 sizeof(struct snd_soc_pcm_runtime) *
2382 (card->num_links + card->num_aux_devs),
2384 if (card->rtd == NULL)
2387 card->rtd_aux = &card->rtd[card->num_links];
2389 for (i = 0; i < card->num_links; i++) {
2390 card->rtd[i].card = card;
2391 card->rtd[i].dai_link = &card->dai_link[i];
2392 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
2393 sizeof(struct snd_soc_dai *) *
2394 (card->rtd[i].dai_link->num_codecs),
2396 if (card->rtd[i].codec_dais == NULL)
2400 for (i = 0; i < card->num_aux_devs; i++)
2401 card->rtd_aux[i].card = card;
2403 INIT_LIST_HEAD(&card->dapm_dirty);
2404 card->instantiated = 0;
2405 mutex_init(&card->mutex);
2406 mutex_init(&card->dapm_mutex);
2408 ret = snd_soc_instantiate_card(card);
2412 /* deactivate pins to sleep state */
2413 for (i = 0; i < card->num_rtd; i++) {
2414 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2415 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2418 for (j = 0; j < rtd->num_codecs; j++) {
2419 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2420 if (!codec_dai->active)
2421 pinctrl_pm_select_sleep_state(codec_dai->dev);
2424 if (!cpu_dai->active)
2425 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2430 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2433 * snd_soc_unregister_card - Unregister a card with the ASoC core
2435 * @card: Card to unregister
2438 int snd_soc_unregister_card(struct snd_soc_card *card)
2440 if (card->instantiated) {
2441 card->instantiated = false;
2442 snd_soc_dapm_shutdown(card);
2443 soc_cleanup_card_resources(card);
2444 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2449 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2452 * Simplify DAI link configuration by removing ".-1" from device names
2453 * and sanitizing names.
2455 static char *fmt_single_name(struct device *dev, int *id)
2457 char *found, name[NAME_SIZE];
2460 if (dev_name(dev) == NULL)
2463 strlcpy(name, dev_name(dev), NAME_SIZE);
2465 /* are we a "%s.%d" name (platform and SPI components) */
2466 found = strstr(name, dev->driver->name);
2469 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2471 /* discard ID from name if ID == -1 */
2473 found[strlen(dev->driver->name)] = '\0';
2477 /* I2C component devices are named "bus-addr" */
2478 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2479 char tmp[NAME_SIZE];
2481 /* create unique ID number from I2C addr and bus */
2482 *id = ((id1 & 0xffff) << 16) + id2;
2484 /* sanitize component name for DAI link creation */
2485 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2486 strlcpy(name, tmp, NAME_SIZE);
2491 return kstrdup(name, GFP_KERNEL);
2495 * Simplify DAI link naming for single devices with multiple DAIs by removing
2496 * any ".-1" and using the DAI name (instead of device name).
2498 static inline char *fmt_multiple_name(struct device *dev,
2499 struct snd_soc_dai_driver *dai_drv)
2501 if (dai_drv->name == NULL) {
2503 "ASoC: error - multiple DAI %s registered with no name\n",
2508 return kstrdup(dai_drv->name, GFP_KERNEL);
2512 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2514 * @component: The component for which the DAIs should be unregistered
2516 static void snd_soc_unregister_dais(struct snd_soc_component *component)
2518 struct snd_soc_dai *dai, *_dai;
2520 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
2521 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2523 list_del(&dai->list);
2530 * snd_soc_register_dais - Register a DAI with the ASoC core
2532 * @component: The component the DAIs are registered for
2533 * @dai_drv: DAI driver to use for the DAIs
2534 * @count: Number of DAIs
2535 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
2538 static int snd_soc_register_dais(struct snd_soc_component *component,
2539 struct snd_soc_dai_driver *dai_drv, size_t count,
2540 bool legacy_dai_naming)
2542 struct device *dev = component->dev;
2543 struct snd_soc_dai *dai;
2547 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
2549 component->dai_drv = dai_drv;
2550 component->num_dai = count;
2552 for (i = 0; i < count; i++) {
2554 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2561 * Back in the old days when we still had component-less DAIs,
2562 * instead of having a static name, component-less DAIs would
2563 * inherit the name of the parent device so it is possible to
2564 * register multiple instances of the DAI. We still need to keep
2565 * the same naming style even though those DAIs are not
2566 * component-less anymore.
2568 if (count == 1 && legacy_dai_naming) {
2569 dai->name = fmt_single_name(dev, &dai->id);
2571 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
2573 dai->id = dai_drv[i].id;
2577 if (dai->name == NULL) {
2583 dai->component = component;
2585 dai->driver = &dai_drv[i];
2586 if (!dai->driver->ops)
2587 dai->driver->ops = &null_dai_ops;
2589 list_add(&dai->list, &component->dai_list);
2591 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2597 snd_soc_unregister_dais(component);
2602 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
2603 enum snd_soc_dapm_type type, int subseq)
2605 struct snd_soc_component *component = dapm->component;
2607 component->driver->seq_notifier(component, type, subseq);
2610 static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
2613 struct snd_soc_component *component = dapm->component;
2615 return component->driver->stream_event(component, event);
2618 static int snd_soc_component_initialize(struct snd_soc_component *component,
2619 const struct snd_soc_component_driver *driver, struct device *dev)
2621 struct snd_soc_dapm_context *dapm;
2623 component->name = fmt_single_name(dev, &component->id);
2624 if (!component->name) {
2625 dev_err(dev, "ASoC: Failed to allocate name\n");
2629 component->dev = dev;
2630 component->driver = driver;
2631 component->probe = component->driver->probe;
2632 component->remove = component->driver->remove;
2634 if (!component->dapm_ptr)
2635 component->dapm_ptr = &component->dapm;
2637 dapm = component->dapm_ptr;
2639 dapm->component = component;
2640 dapm->bias_level = SND_SOC_BIAS_OFF;
2641 dapm->idle_bias_off = true;
2642 if (driver->seq_notifier)
2643 dapm->seq_notifier = snd_soc_component_seq_notifier;
2644 if (driver->stream_event)
2645 dapm->stream_event = snd_soc_component_stream_event;
2647 component->controls = driver->controls;
2648 component->num_controls = driver->num_controls;
2649 component->dapm_widgets = driver->dapm_widgets;
2650 component->num_dapm_widgets = driver->num_dapm_widgets;
2651 component->dapm_routes = driver->dapm_routes;
2652 component->num_dapm_routes = driver->num_dapm_routes;
2654 INIT_LIST_HEAD(&component->dai_list);
2655 mutex_init(&component->io_mutex);
2660 static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2662 int val_bytes = regmap_get_val_bytes(component->regmap);
2664 /* Errors are legitimate for non-integer byte multiples */
2666 component->val_bytes = val_bytes;
2669 #ifdef CONFIG_REGMAP
2672 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
2673 * @component: The component for which to initialize the regmap instance
2674 * @regmap: The regmap instance that should be used by the component
2676 * This function allows deferred assignment of the regmap instance that is
2677 * associated with the component. Only use this if the regmap instance is not
2678 * yet ready when the component is registered. The function must also be called
2679 * before the first IO attempt of the component.
2681 void snd_soc_component_init_regmap(struct snd_soc_component *component,
2682 struct regmap *regmap)
2684 component->regmap = regmap;
2685 snd_soc_component_setup_regmap(component);
2687 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
2690 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
2691 * @component: The component for which to de-initialize the regmap instance
2693 * Calls regmap_exit() on the regmap instance associated to the component and
2694 * removes the regmap instance from the component.
2696 * This function should only be used if snd_soc_component_init_regmap() was used
2697 * to initialize the regmap instance.
2699 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
2701 regmap_exit(component->regmap);
2702 component->regmap = NULL;
2704 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2708 static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
2710 if (!component->write && !component->read) {
2711 if (!component->regmap)
2712 component->regmap = dev_get_regmap(component->dev, NULL);
2713 if (component->regmap)
2714 snd_soc_component_setup_regmap(component);
2717 list_add(&component->list, &component_list);
2720 static void snd_soc_component_add(struct snd_soc_component *component)
2722 mutex_lock(&client_mutex);
2723 snd_soc_component_add_unlocked(component);
2724 mutex_unlock(&client_mutex);
2727 static void snd_soc_component_cleanup(struct snd_soc_component *component)
2729 snd_soc_unregister_dais(component);
2730 kfree(component->name);
2733 static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2735 list_del(&component->list);
2738 int snd_soc_register_component(struct device *dev,
2739 const struct snd_soc_component_driver *cmpnt_drv,
2740 struct snd_soc_dai_driver *dai_drv,
2743 struct snd_soc_component *cmpnt;
2746 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
2748 dev_err(dev, "ASoC: Failed to allocate memory\n");
2752 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
2756 cmpnt->ignore_pmdown_time = true;
2757 cmpnt->registered_as_component = true;
2759 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
2761 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
2765 snd_soc_component_add(cmpnt);
2770 snd_soc_component_cleanup(cmpnt);
2775 EXPORT_SYMBOL_GPL(snd_soc_register_component);
2778 * snd_soc_unregister_component - Unregister a component from the ASoC core
2781 void snd_soc_unregister_component(struct device *dev)
2783 struct snd_soc_component *cmpnt;
2785 mutex_lock(&client_mutex);
2786 list_for_each_entry(cmpnt, &component_list, list) {
2787 if (dev == cmpnt->dev && cmpnt->registered_as_component)
2790 mutex_unlock(&client_mutex);
2794 snd_soc_component_del_unlocked(cmpnt);
2795 mutex_unlock(&client_mutex);
2796 snd_soc_component_cleanup(cmpnt);
2799 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2801 static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
2803 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
2805 return platform->driver->probe(platform);
2808 static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
2810 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
2812 platform->driver->remove(platform);
2816 * snd_soc_add_platform - Add a platform to the ASoC core
2817 * @dev: The parent device for the platform
2818 * @platform: The platform to add
2819 * @platform_driver: The driver for the platform
2821 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
2822 const struct snd_soc_platform_driver *platform_drv)
2826 ret = snd_soc_component_initialize(&platform->component,
2827 &platform_drv->component_driver, dev);
2831 platform->dev = dev;
2832 platform->driver = platform_drv;
2834 if (platform_drv->probe)
2835 platform->component.probe = snd_soc_platform_drv_probe;
2836 if (platform_drv->remove)
2837 platform->component.remove = snd_soc_platform_drv_remove;
2839 #ifdef CONFIG_DEBUG_FS
2840 platform->component.debugfs_prefix = "platform";
2843 mutex_lock(&client_mutex);
2844 snd_soc_component_add_unlocked(&platform->component);
2845 list_add(&platform->list, &platform_list);
2846 mutex_unlock(&client_mutex);
2848 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
2849 platform->component.name);
2853 EXPORT_SYMBOL_GPL(snd_soc_add_platform);
2856 * snd_soc_register_platform - Register a platform with the ASoC core
2858 * @platform: platform to register
2860 int snd_soc_register_platform(struct device *dev,
2861 const struct snd_soc_platform_driver *platform_drv)
2863 struct snd_soc_platform *platform;
2866 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
2868 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
2869 if (platform == NULL)
2872 ret = snd_soc_add_platform(dev, platform, platform_drv);
2878 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2881 * snd_soc_remove_platform - Remove a platform from the ASoC core
2882 * @platform: the platform to remove
2884 void snd_soc_remove_platform(struct snd_soc_platform *platform)
2887 mutex_lock(&client_mutex);
2888 list_del(&platform->list);
2889 snd_soc_component_del_unlocked(&platform->component);
2890 mutex_unlock(&client_mutex);
2892 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
2893 platform->component.name);
2895 snd_soc_component_cleanup(&platform->component);
2897 EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
2899 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
2901 struct snd_soc_platform *platform;
2903 mutex_lock(&client_mutex);
2904 list_for_each_entry(platform, &platform_list, list) {
2905 if (dev == platform->dev) {
2906 mutex_unlock(&client_mutex);
2910 mutex_unlock(&client_mutex);
2914 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
2917 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2919 * @platform: platform to unregister
2921 void snd_soc_unregister_platform(struct device *dev)
2923 struct snd_soc_platform *platform;
2925 platform = snd_soc_lookup_platform(dev);
2929 snd_soc_remove_platform(platform);
2932 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2934 static u64 codec_format_map[] = {
2935 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2936 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2937 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2938 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2939 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2940 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2941 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2942 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2943 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2944 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2945 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2946 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2947 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2948 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2949 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2950 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2953 /* Fix up the DAI formats for endianness: codecs don't actually see
2954 * the endianness of the data but we're using the CPU format
2955 * definitions which do need to include endianness so we ensure that
2956 * codec DAIs always have both big and little endian variants set.
2958 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2962 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2963 if (stream->formats & codec_format_map[i])
2964 stream->formats |= codec_format_map[i];
2967 static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
2969 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
2971 return codec->driver->probe(codec);
2974 static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
2976 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
2978 codec->driver->remove(codec);
2981 static int snd_soc_codec_drv_write(struct snd_soc_component *component,
2982 unsigned int reg, unsigned int val)
2984 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
2986 return codec->driver->write(codec, reg, val);
2989 static int snd_soc_codec_drv_read(struct snd_soc_component *component,
2990 unsigned int reg, unsigned int *val)
2992 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
2994 *val = codec->driver->read(codec, reg);
2999 static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
3000 enum snd_soc_bias_level level)
3002 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
3004 return codec->driver->set_bias_level(codec, level);
3008 * snd_soc_register_codec - Register a codec with the ASoC core
3010 * @codec: codec to register
3012 int snd_soc_register_codec(struct device *dev,
3013 const struct snd_soc_codec_driver *codec_drv,
3014 struct snd_soc_dai_driver *dai_drv,
3017 struct snd_soc_codec *codec;
3018 struct snd_soc_dai *dai;
3021 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3023 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3027 codec->component.dapm_ptr = &codec->dapm;
3028 codec->component.codec = codec;
3030 ret = snd_soc_component_initialize(&codec->component,
3031 &codec_drv->component_driver, dev);
3035 if (codec_drv->controls) {
3036 codec->component.controls = codec_drv->controls;
3037 codec->component.num_controls = codec_drv->num_controls;
3039 if (codec_drv->dapm_widgets) {
3040 codec->component.dapm_widgets = codec_drv->dapm_widgets;
3041 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
3043 if (codec_drv->dapm_routes) {
3044 codec->component.dapm_routes = codec_drv->dapm_routes;
3045 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
3048 if (codec_drv->probe)
3049 codec->component.probe = snd_soc_codec_drv_probe;
3050 if (codec_drv->remove)
3051 codec->component.remove = snd_soc_codec_drv_remove;
3052 if (codec_drv->write)
3053 codec->component.write = snd_soc_codec_drv_write;
3054 if (codec_drv->read)
3055 codec->component.read = snd_soc_codec_drv_read;
3056 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
3057 codec->dapm.idle_bias_off = codec_drv->idle_bias_off;
3058 codec->dapm.suspend_bias_off = codec_drv->suspend_bias_off;
3059 if (codec_drv->seq_notifier)
3060 codec->dapm.seq_notifier = codec_drv->seq_notifier;
3061 if (codec_drv->set_bias_level)
3062 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
3064 codec->driver = codec_drv;
3065 codec->component.val_bytes = codec_drv->reg_word_size;
3067 #ifdef CONFIG_DEBUG_FS
3068 codec->component.init_debugfs = soc_init_codec_debugfs;
3069 codec->component.debugfs_prefix = "codec";
3072 if (codec_drv->get_regmap)
3073 codec->component.regmap = codec_drv->get_regmap(dev);
3075 for (i = 0; i < num_dai; i++) {
3076 fixup_codec_formats(&dai_drv[i].playback);
3077 fixup_codec_formats(&dai_drv[i].capture);
3080 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3082 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3086 list_for_each_entry(dai, &codec->component.dai_list, list)
3089 mutex_lock(&client_mutex);
3090 snd_soc_component_add_unlocked(&codec->component);
3091 list_add(&codec->list, &codec_list);
3092 mutex_unlock(&client_mutex);
3094 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3095 codec->component.name);
3099 snd_soc_component_cleanup(&codec->component);
3104 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3107 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3109 * @codec: codec to unregister
3111 void snd_soc_unregister_codec(struct device *dev)
3113 struct snd_soc_codec *codec;
3115 mutex_lock(&client_mutex);
3116 list_for_each_entry(codec, &codec_list, list) {
3117 if (dev == codec->dev)
3120 mutex_unlock(&client_mutex);
3124 list_del(&codec->list);
3125 snd_soc_component_del_unlocked(&codec->component);
3126 mutex_unlock(&client_mutex);
3128 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3129 codec->component.name);
3131 snd_soc_component_cleanup(&codec->component);
3132 snd_soc_cache_exit(codec);
3135 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3137 /* Retrieve a card's name from device tree */
3138 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3139 const char *propname)
3141 struct device_node *np;
3145 pr_err("card->dev is not set before calling %s\n", __func__);
3149 np = card->dev->of_node;
3151 ret = of_property_read_string_index(np, propname, 0, &card->name);
3153 * EINVAL means the property does not exist. This is fine providing
3154 * card->name was previously set, which is checked later in
3155 * snd_soc_register_card.
3157 if (ret < 0 && ret != -EINVAL) {
3159 "ASoC: Property '%s' could not be read: %d\n",
3166 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3168 static const struct snd_soc_dapm_widget simple_widgets[] = {
3169 SND_SOC_DAPM_MIC("Microphone", NULL),
3170 SND_SOC_DAPM_LINE("Line", NULL),
3171 SND_SOC_DAPM_HP("Headphone", NULL),
3172 SND_SOC_DAPM_SPK("Speaker", NULL),
3175 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
3176 const char *propname)
3178 struct device_node *np = card->dev->of_node;
3179 struct snd_soc_dapm_widget *widgets;
3180 const char *template, *wname;
3181 int i, j, num_widgets, ret;
3183 num_widgets = of_property_count_strings(np, propname);
3184 if (num_widgets < 0) {
3186 "ASoC: Property '%s' does not exist\n", propname);
3189 if (num_widgets & 1) {
3191 "ASoC: Property '%s' length is not even\n", propname);
3197 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3202 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3206 "ASoC: Could not allocate memory for widgets\n");
3210 for (i = 0; i < num_widgets; i++) {
3211 ret = of_property_read_string_index(np, propname,
3215 "ASoC: Property '%s' index %d read error:%d\n",
3216 propname, 2 * i, ret);
3220 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3221 if (!strncmp(template, simple_widgets[j].name,
3222 strlen(simple_widgets[j].name))) {
3223 widgets[i] = simple_widgets[j];
3228 if (j >= ARRAY_SIZE(simple_widgets)) {
3230 "ASoC: DAPM widget '%s' is not supported\n",
3235 ret = of_property_read_string_index(np, propname,
3240 "ASoC: Property '%s' index %d read error:%d\n",
3241 propname, (2 * i) + 1, ret);
3245 widgets[i].name = wname;
3248 card->of_dapm_widgets = widgets;
3249 card->num_of_dapm_widgets = num_widgets;
3253 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
3255 int snd_soc_of_parse_tdm_slot(struct device_node *np,
3256 unsigned int *slots,
3257 unsigned int *slot_width)
3262 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3263 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3271 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3272 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3282 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3284 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3285 const char *propname)
3287 struct device_node *np = card->dev->of_node;
3289 struct snd_soc_dapm_route *routes;
3292 num_routes = of_property_count_strings(np, propname);
3293 if (num_routes < 0 || num_routes & 1) {
3295 "ASoC: Property '%s' does not exist or its length is not even\n",
3301 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3306 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3310 "ASoC: Could not allocate DAPM route table\n");
3314 for (i = 0; i < num_routes; i++) {
3315 ret = of_property_read_string_index(np, propname,
3316 2 * i, &routes[i].sink);
3319 "ASoC: Property '%s' index %d could not be read: %d\n",
3320 propname, 2 * i, ret);
3323 ret = of_property_read_string_index(np, propname,
3324 (2 * i) + 1, &routes[i].source);
3327 "ASoC: Property '%s' index %d could not be read: %d\n",
3328 propname, (2 * i) + 1, ret);
3333 card->num_of_dapm_routes = num_routes;
3334 card->of_dapm_routes = routes;
3338 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3340 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3342 struct device_node **bitclkmaster,
3343 struct device_node **framemaster)
3347 unsigned int format = 0;
3353 } of_fmt_table[] = {
3354 { "i2s", SND_SOC_DAIFMT_I2S },
3355 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3356 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3357 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3358 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3359 { "ac97", SND_SOC_DAIFMT_AC97 },
3360 { "pdm", SND_SOC_DAIFMT_PDM},
3361 { "msb", SND_SOC_DAIFMT_MSB },
3362 { "lsb", SND_SOC_DAIFMT_LSB },
3369 * check "[prefix]format = xxx"
3370 * SND_SOC_DAIFMT_FORMAT_MASK area
3372 snprintf(prop, sizeof(prop), "%sformat", prefix);
3373 ret = of_property_read_string(np, prop, &str);
3375 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3376 if (strcmp(str, of_fmt_table[i].name) == 0) {
3377 format |= of_fmt_table[i].val;
3384 * check "[prefix]continuous-clock"
3385 * SND_SOC_DAIFMT_CLOCK_MASK area
3387 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
3388 if (of_get_property(np, prop, NULL))
3389 format |= SND_SOC_DAIFMT_CONT;
3391 format |= SND_SOC_DAIFMT_GATED;
3394 * check "[prefix]bitclock-inversion"
3395 * check "[prefix]frame-inversion"
3396 * SND_SOC_DAIFMT_INV_MASK area
3398 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3399 bit = !!of_get_property(np, prop, NULL);
3401 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3402 frame = !!of_get_property(np, prop, NULL);
3404 switch ((bit << 4) + frame) {
3406 format |= SND_SOC_DAIFMT_IB_IF;
3409 format |= SND_SOC_DAIFMT_IB_NF;
3412 format |= SND_SOC_DAIFMT_NB_IF;
3415 /* SND_SOC_DAIFMT_NB_NF is default */
3420 * check "[prefix]bitclock-master"
3421 * check "[prefix]frame-master"
3422 * SND_SOC_DAIFMT_MASTER_MASK area
3424 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3425 bit = !!of_get_property(np, prop, NULL);
3426 if (bit && bitclkmaster)
3427 *bitclkmaster = of_parse_phandle(np, prop, 0);
3429 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3430 frame = !!of_get_property(np, prop, NULL);
3431 if (frame && framemaster)
3432 *framemaster = of_parse_phandle(np, prop, 0);
3434 switch ((bit << 4) + frame) {
3436 format |= SND_SOC_DAIFMT_CBM_CFM;
3439 format |= SND_SOC_DAIFMT_CBM_CFS;
3442 format |= SND_SOC_DAIFMT_CBS_CFM;
3445 format |= SND_SOC_DAIFMT_CBS_CFS;
3451 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3453 static int snd_soc_get_dai_name(struct of_phandle_args *args,
3454 const char **dai_name)
3456 struct snd_soc_component *pos;
3457 int ret = -EPROBE_DEFER;
3459 mutex_lock(&client_mutex);
3460 list_for_each_entry(pos, &component_list, list) {
3461 if (pos->dev->of_node != args->np)
3464 if (pos->driver->of_xlate_dai_name) {
3465 ret = pos->driver->of_xlate_dai_name(pos,
3471 switch (args->args_count) {
3473 id = 0; /* same as dai_drv[0] */
3483 if (id < 0 || id >= pos->num_dai) {
3490 *dai_name = pos->dai_drv[id].name;
3492 *dai_name = pos->name;
3497 mutex_unlock(&client_mutex);
3501 int snd_soc_of_get_dai_name(struct device_node *of_node,
3502 const char **dai_name)
3504 struct of_phandle_args args;
3507 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3508 "#sound-dai-cells", 0, &args);
3512 ret = snd_soc_get_dai_name(&args, dai_name);
3514 of_node_put(args.np);
3518 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3521 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3523 * @of_node: Device node
3524 * @dai_link: DAI link
3526 * Builds an array of CODEC DAI components from the DAI link property
3528 * The array is set in the DAI link and the number of DAIs is set accordingly.
3529 * The device nodes in the array (of_node) must be dereferenced by the caller.
3531 * Returns 0 for success
3533 int snd_soc_of_get_dai_link_codecs(struct device *dev,
3534 struct device_node *of_node,
3535 struct snd_soc_dai_link *dai_link)
3537 struct of_phandle_args args;
3538 struct snd_soc_dai_link_component *component;
3540 int index, num_codecs, ret;
3542 /* Count the number of CODECs */
3544 num_codecs = of_count_phandle_with_args(of_node, name,
3545 "#sound-dai-cells");
3546 if (num_codecs <= 0) {
3547 if (num_codecs == -ENOENT)
3548 dev_err(dev, "No 'sound-dai' property\n");
3550 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3553 component = devm_kzalloc(dev,
3554 sizeof *component * num_codecs,
3558 dai_link->codecs = component;
3559 dai_link->num_codecs = num_codecs;
3561 /* Parse the list */
3562 for (index = 0, component = dai_link->codecs;
3563 index < dai_link->num_codecs;
3564 index++, component++) {
3565 ret = of_parse_phandle_with_args(of_node, name,
3570 component->of_node = args.np;
3571 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3577 for (index = 0, component = dai_link->codecs;
3578 index < dai_link->num_codecs;
3579 index++, component++) {
3580 if (!component->of_node)
3582 of_node_put(component->of_node);
3583 component->of_node = NULL;
3585 dai_link->codecs = NULL;
3586 dai_link->num_codecs = 0;
3589 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3591 static int __init snd_soc_init(void)
3593 #ifdef CONFIG_DEBUG_FS
3594 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3595 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
3596 pr_warn("ASoC: Failed to create debugfs directory\n");
3597 snd_soc_debugfs_root = NULL;
3600 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
3602 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3604 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
3606 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
3608 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
3609 &platform_list_fops))
3610 pr_warn("ASoC: Failed to create platform list debugfs file\n");
3613 snd_soc_util_init();
3615 return platform_driver_register(&soc_driver);
3617 module_init(snd_soc_init);
3619 static void __exit snd_soc_exit(void)
3621 snd_soc_util_exit();
3623 #ifdef CONFIG_DEBUG_FS
3624 debugfs_remove_recursive(snd_soc_debugfs_root);
3626 platform_driver_unregister(&soc_driver);
3628 module_exit(snd_soc_exit);
3630 /* Module information */
3631 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3632 MODULE_DESCRIPTION("ALSA SoC Core");
3633 MODULE_LICENSE("GPL");
3634 MODULE_ALIAS("platform:soc-audio");