2 * soc-core.c -- ALSA SoC Audio Layer
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
30 #include <linux/bitops.h>
31 #include <linux/debugfs.h>
32 #include <linux/platform_device.h>
33 #include <linux/pinctrl/consumer.h>
34 #include <linux/ctype.h>
35 #include <linux/slab.h>
37 #include <linux/gpio.h>
38 #include <linux/of_gpio.h>
39 #include <sound/ac97_codec.h>
40 #include <sound/core.h>
41 #include <sound/jack.h>
42 #include <sound/pcm.h>
43 #include <sound/pcm_params.h>
44 #include <sound/soc.h>
45 #include <sound/soc-dpcm.h>
46 #include <sound/initval.h>
48 #define CREATE_TRACE_POINTS
49 #include <trace/events/asoc.h>
53 #ifdef CONFIG_DEBUG_FS
54 struct dentry *snd_soc_debugfs_root;
55 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
58 static DEFINE_MUTEX(client_mutex);
59 static LIST_HEAD(platform_list);
60 static LIST_HEAD(codec_list);
61 static LIST_HEAD(component_list);
64 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
68 static int pmdown_time = 5000;
69 module_param(pmdown_time, int, 0);
70 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
72 struct snd_ac97_reset_cfg {
74 struct pinctrl_state *pstate_reset;
75 struct pinctrl_state *pstate_warm_reset;
76 struct pinctrl_state *pstate_run;
82 /* returns the minimum number of bytes needed to represent
83 * a particular given value */
84 static int min_bytes_needed(unsigned long val)
89 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
92 c = (sizeof val * 8) - c;
100 /* fill buf which is 'len' bytes with a formatted
101 * string of the form 'reg: value\n' */
102 static int format_register_str(struct snd_soc_codec *codec,
103 unsigned int reg, char *buf, size_t len)
105 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
106 int regsize = codec->driver->reg_word_size * 2;
108 char tmpbuf[len + 1];
109 char regbuf[regsize + 1];
111 /* since tmpbuf is allocated on the stack, warn the callers if they
112 * try to abuse this function */
115 /* +2 for ': ' and + 1 for '\n' */
116 if (wordsize + regsize + 2 + 1 != len)
119 ret = snd_soc_read(codec, reg);
121 memset(regbuf, 'X', regsize);
122 regbuf[regsize] = '\0';
124 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
127 /* prepare the buffer */
128 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
129 /* copy it back to the caller without the '\0' */
130 memcpy(buf, tmpbuf, len);
135 /* codec register dump */
136 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
137 size_t count, loff_t pos)
140 int wordsize, regsize;
145 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
146 regsize = codec->driver->reg_word_size * 2;
148 len = wordsize + regsize + 2 + 1;
150 if (!codec->driver->reg_cache_size)
153 if (codec->driver->reg_cache_step)
154 step = codec->driver->reg_cache_step;
156 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
157 /* only support larger than PAGE_SIZE bytes debugfs
158 * entries for the default case */
160 if (total + len >= count - 1)
162 format_register_str(codec, i, buf + total, len);
168 total = min(total, count - 1);
173 static ssize_t codec_reg_show(struct device *dev,
174 struct device_attribute *attr, char *buf)
176 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
178 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
181 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
183 static ssize_t pmdown_time_show(struct device *dev,
184 struct device_attribute *attr, char *buf)
186 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
188 return sprintf(buf, "%ld\n", rtd->pmdown_time);
191 static ssize_t pmdown_time_set(struct device *dev,
192 struct device_attribute *attr,
193 const char *buf, size_t count)
195 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
198 ret = kstrtol(buf, 10, &rtd->pmdown_time);
205 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
207 #ifdef CONFIG_DEBUG_FS
208 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
209 size_t count, loff_t *ppos)
212 struct snd_soc_codec *codec = file->private_data;
215 if (*ppos < 0 || !count)
218 buf = kmalloc(count, GFP_KERNEL);
222 ret = soc_codec_reg_show(codec, buf, count, *ppos);
224 if (copy_to_user(user_buf, buf, ret)) {
235 static ssize_t codec_reg_write_file(struct file *file,
236 const char __user *user_buf, size_t count, loff_t *ppos)
241 unsigned long reg, value;
242 struct snd_soc_codec *codec = file->private_data;
245 buf_size = min(count, (sizeof(buf)-1));
246 if (copy_from_user(buf, user_buf, buf_size))
250 while (*start == ' ')
252 reg = simple_strtoul(start, &start, 16);
253 while (*start == ' ')
255 ret = kstrtoul(start, 16, &value);
259 /* Userspace has been fiddling around behind the kernel's back */
260 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
262 snd_soc_write(codec, reg, value);
266 static const struct file_operations codec_reg_fops = {
268 .read = codec_reg_read_file,
269 .write = codec_reg_write_file,
270 .llseek = default_llseek,
273 static void soc_init_component_debugfs(struct snd_soc_component *component)
275 if (component->debugfs_prefix) {
278 name = kasprintf(GFP_KERNEL, "%s:%s",
279 component->debugfs_prefix, component->name);
281 component->debugfs_root = debugfs_create_dir(name,
282 component->card->debugfs_card_root);
286 component->debugfs_root = debugfs_create_dir(component->name,
287 component->card->debugfs_card_root);
290 if (!component->debugfs_root) {
291 dev_warn(component->dev,
292 "ASoC: Failed to create component debugfs directory\n");
296 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
297 component->debugfs_root);
299 if (component->init_debugfs)
300 component->init_debugfs(component);
303 static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
305 debugfs_remove_recursive(component->debugfs_root);
308 static void soc_init_codec_debugfs(struct snd_soc_component *component)
310 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
312 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
313 codec->component.debugfs_root,
314 codec, &codec_reg_fops);
315 if (!codec->debugfs_reg)
317 "ASoC: Failed to create codec register debugfs file\n");
320 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
321 size_t count, loff_t *ppos)
323 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
324 ssize_t len, ret = 0;
325 struct snd_soc_codec *codec;
330 list_for_each_entry(codec, &codec_list, list) {
331 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
332 codec->component.name);
335 if (ret > PAGE_SIZE) {
342 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
349 static const struct file_operations codec_list_fops = {
350 .read = codec_list_read_file,
351 .llseek = default_llseek,/* read accesses f_pos */
354 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
355 size_t count, loff_t *ppos)
357 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
358 ssize_t len, ret = 0;
359 struct snd_soc_component *component;
360 struct snd_soc_dai *dai;
365 list_for_each_entry(component, &component_list, list) {
366 list_for_each_entry(dai, &component->dai_list, list) {
367 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
371 if (ret > PAGE_SIZE) {
378 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
385 static const struct file_operations dai_list_fops = {
386 .read = dai_list_read_file,
387 .llseek = default_llseek,/* read accesses f_pos */
390 static ssize_t platform_list_read_file(struct file *file,
391 char __user *user_buf,
392 size_t count, loff_t *ppos)
394 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
395 ssize_t len, ret = 0;
396 struct snd_soc_platform *platform;
401 list_for_each_entry(platform, &platform_list, list) {
402 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
403 platform->component.name);
406 if (ret > PAGE_SIZE) {
412 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
419 static const struct file_operations platform_list_fops = {
420 .read = platform_list_read_file,
421 .llseek = default_llseek,/* read accesses f_pos */
424 static void soc_init_card_debugfs(struct snd_soc_card *card)
426 card->debugfs_card_root = debugfs_create_dir(card->name,
427 snd_soc_debugfs_root);
428 if (!card->debugfs_card_root) {
430 "ASoC: Failed to create card debugfs directory\n");
434 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
435 card->debugfs_card_root,
437 if (!card->debugfs_pop_time)
439 "ASoC: Failed to create pop time debugfs file\n");
442 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
444 debugfs_remove_recursive(card->debugfs_card_root);
449 #define soc_init_codec_debugfs NULL
451 static inline void soc_init_component_debugfs(
452 struct snd_soc_component *component)
456 static inline void soc_cleanup_component_debugfs(
457 struct snd_soc_component *component)
461 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
465 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
470 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
471 const char *dai_link, int stream)
475 for (i = 0; i < card->num_links; i++) {
476 if (card->rtd[i].dai_link->no_pcm &&
477 !strcmp(card->rtd[i].dai_link->name, dai_link))
478 return card->rtd[i].pcm->streams[stream].substream;
480 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
483 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
485 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
486 const char *dai_link)
490 for (i = 0; i < card->num_links; i++) {
491 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
492 return &card->rtd[i];
494 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
497 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
499 #ifdef CONFIG_SND_SOC_AC97_BUS
500 /* unregister ac97 codec */
501 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
503 if (codec->ac97->dev.bus)
504 device_unregister(&codec->ac97->dev);
508 /* stop no dev release warning */
509 static void soc_ac97_device_release(struct device *dev){}
511 /* register ac97 codec to bus */
512 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
516 codec->ac97->dev.bus = &ac97_bus_type;
517 codec->ac97->dev.parent = codec->component.card->dev;
518 codec->ac97->dev.release = soc_ac97_device_release;
520 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
521 codec->component.card->snd_card->number, 0,
522 codec->component.name);
523 err = device_register(&codec->ac97->dev);
525 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
526 codec->ac97->dev.bus = NULL;
533 static void codec2codec_close_delayed_work(struct work_struct *work)
535 /* Currently nothing to do for c2c links
536 * Since c2c links are internal nodes in the DAPM graph and
537 * don't interface with the outside world or application layer
538 * we don't have to do any special handling on close.
542 #ifdef CONFIG_PM_SLEEP
543 /* powers down audio subsystem for suspend */
544 int snd_soc_suspend(struct device *dev)
546 struct snd_soc_card *card = dev_get_drvdata(dev);
547 struct snd_soc_codec *codec;
550 /* If the card is not initialized yet there is nothing to do */
551 if (!card->instantiated)
554 /* Due to the resume being scheduled into a workqueue we could
555 * suspend before that's finished - wait for it to complete.
557 snd_power_lock(card->snd_card);
558 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
559 snd_power_unlock(card->snd_card);
561 /* we're going to block userspace touching us until resume completes */
562 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
564 /* mute any active DACs */
565 for (i = 0; i < card->num_rtd; i++) {
567 if (card->rtd[i].dai_link->ignore_suspend)
570 for (j = 0; j < card->rtd[i].num_codecs; j++) {
571 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
572 struct snd_soc_dai_driver *drv = dai->driver;
574 if (drv->ops->digital_mute && dai->playback_active)
575 drv->ops->digital_mute(dai, 1);
579 /* suspend all pcms */
580 for (i = 0; i < card->num_rtd; i++) {
581 if (card->rtd[i].dai_link->ignore_suspend)
584 snd_pcm_suspend_all(card->rtd[i].pcm);
587 if (card->suspend_pre)
588 card->suspend_pre(card);
590 for (i = 0; i < card->num_rtd; i++) {
591 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
593 if (card->rtd[i].dai_link->ignore_suspend)
596 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
597 cpu_dai->driver->suspend(cpu_dai);
600 /* close any waiting streams and save state */
601 for (i = 0; i < card->num_rtd; i++) {
602 struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais;
603 flush_delayed_work(&card->rtd[i].delayed_work);
604 for (j = 0; j < card->rtd[i].num_codecs; j++) {
605 codec_dais[j]->codec->dapm.suspend_bias_level =
606 codec_dais[j]->codec->dapm.bias_level;
610 for (i = 0; i < card->num_rtd; i++) {
612 if (card->rtd[i].dai_link->ignore_suspend)
615 snd_soc_dapm_stream_event(&card->rtd[i],
616 SNDRV_PCM_STREAM_PLAYBACK,
617 SND_SOC_DAPM_STREAM_SUSPEND);
619 snd_soc_dapm_stream_event(&card->rtd[i],
620 SNDRV_PCM_STREAM_CAPTURE,
621 SND_SOC_DAPM_STREAM_SUSPEND);
624 /* Recheck all endpoints too, their state is affected by suspend */
625 dapm_mark_endpoints_dirty(card);
626 snd_soc_dapm_sync(&card->dapm);
628 /* suspend all CODECs */
629 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
630 /* If there are paths active then the CODEC will be held with
631 * bias _ON and should not be suspended. */
632 if (!codec->suspended) {
633 switch (codec->dapm.bias_level) {
634 case SND_SOC_BIAS_STANDBY:
636 * If the CODEC is capable of idle
637 * bias off then being in STANDBY
638 * means it's doing something,
639 * otherwise fall through.
641 if (codec->dapm.idle_bias_off) {
643 "ASoC: idle_bias_off CODEC on over suspend\n");
647 case SND_SOC_BIAS_OFF:
648 if (codec->driver->suspend)
649 codec->driver->suspend(codec);
650 codec->suspended = 1;
651 if (codec->component.regmap)
652 regcache_mark_dirty(codec->component.regmap);
653 /* deactivate pins to sleep state */
654 pinctrl_pm_select_sleep_state(codec->dev);
658 "ASoC: CODEC is on over suspend\n");
664 for (i = 0; i < card->num_rtd; i++) {
665 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
667 if (card->rtd[i].dai_link->ignore_suspend)
670 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
671 cpu_dai->driver->suspend(cpu_dai);
673 /* deactivate pins to sleep state */
674 pinctrl_pm_select_sleep_state(cpu_dai->dev);
677 if (card->suspend_post)
678 card->suspend_post(card);
682 EXPORT_SYMBOL_GPL(snd_soc_suspend);
684 /* deferred resume work, so resume can complete before we finished
685 * setting our codec back up, which can be very slow on I2C
687 static void soc_resume_deferred(struct work_struct *work)
689 struct snd_soc_card *card =
690 container_of(work, struct snd_soc_card, deferred_resume_work);
691 struct snd_soc_codec *codec;
694 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
695 * so userspace apps are blocked from touching us
698 dev_dbg(card->dev, "ASoC: starting resume work\n");
700 /* Bring us up into D2 so that DAPM starts enabling things */
701 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
703 if (card->resume_pre)
704 card->resume_pre(card);
706 /* resume AC97 DAIs */
707 for (i = 0; i < card->num_rtd; i++) {
708 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
710 if (card->rtd[i].dai_link->ignore_suspend)
713 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
714 cpu_dai->driver->resume(cpu_dai);
717 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
718 /* If the CODEC was idle over suspend then it will have been
719 * left with bias OFF or STANDBY and suspended so we must now
720 * resume. Otherwise the suspend was suppressed.
722 if (codec->suspended) {
723 switch (codec->dapm.bias_level) {
724 case SND_SOC_BIAS_STANDBY:
725 case SND_SOC_BIAS_OFF:
726 if (codec->driver->resume)
727 codec->driver->resume(codec);
728 codec->suspended = 0;
732 "ASoC: CODEC was on over suspend\n");
738 for (i = 0; i < card->num_rtd; i++) {
740 if (card->rtd[i].dai_link->ignore_suspend)
743 snd_soc_dapm_stream_event(&card->rtd[i],
744 SNDRV_PCM_STREAM_PLAYBACK,
745 SND_SOC_DAPM_STREAM_RESUME);
747 snd_soc_dapm_stream_event(&card->rtd[i],
748 SNDRV_PCM_STREAM_CAPTURE,
749 SND_SOC_DAPM_STREAM_RESUME);
752 /* unmute any active DACs */
753 for (i = 0; i < card->num_rtd; i++) {
755 if (card->rtd[i].dai_link->ignore_suspend)
758 for (j = 0; j < card->rtd[i].num_codecs; j++) {
759 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
760 struct snd_soc_dai_driver *drv = dai->driver;
762 if (drv->ops->digital_mute && dai->playback_active)
763 drv->ops->digital_mute(dai, 0);
767 for (i = 0; i < card->num_rtd; i++) {
768 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
770 if (card->rtd[i].dai_link->ignore_suspend)
773 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
774 cpu_dai->driver->resume(cpu_dai);
777 if (card->resume_post)
778 card->resume_post(card);
780 dev_dbg(card->dev, "ASoC: resume work completed\n");
782 /* userspace can access us now we are back as we were before */
783 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
785 /* Recheck all endpoints too, their state is affected by suspend */
786 dapm_mark_endpoints_dirty(card);
787 snd_soc_dapm_sync(&card->dapm);
790 /* powers up audio subsystem after a suspend */
791 int snd_soc_resume(struct device *dev)
793 struct snd_soc_card *card = dev_get_drvdata(dev);
794 int i, ac97_control = 0;
796 /* If the card is not initialized yet there is nothing to do */
797 if (!card->instantiated)
800 /* activate pins from sleep state */
801 for (i = 0; i < card->num_rtd; i++) {
802 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
803 struct snd_soc_dai **codec_dais = rtd->codec_dais;
804 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
808 pinctrl_pm_select_default_state(cpu_dai->dev);
810 for (j = 0; j < rtd->num_codecs; j++) {
811 struct snd_soc_dai *codec_dai = codec_dais[j];
812 if (codec_dai->active)
813 pinctrl_pm_select_default_state(codec_dai->dev);
817 /* AC97 devices might have other drivers hanging off them so
818 * need to resume immediately. Other drivers don't have that
819 * problem and may take a substantial amount of time to resume
820 * due to I/O costs and anti-pop so handle them out of line.
822 for (i = 0; i < card->num_rtd; i++) {
823 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
824 ac97_control |= cpu_dai->driver->ac97_control;
827 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
828 soc_resume_deferred(&card->deferred_resume_work);
830 dev_dbg(dev, "ASoC: Scheduling resume work\n");
831 if (!schedule_work(&card->deferred_resume_work))
832 dev_err(dev, "ASoC: resume work item may be lost\n");
837 EXPORT_SYMBOL_GPL(snd_soc_resume);
839 #define snd_soc_suspend NULL
840 #define snd_soc_resume NULL
843 static const struct snd_soc_dai_ops null_dai_ops = {
846 static struct snd_soc_component *soc_find_component(
847 const struct device_node *of_node, const char *name)
849 struct snd_soc_component *component;
851 list_for_each_entry(component, &component_list, list) {
853 if (component->dev->of_node == of_node)
855 } else if (strcmp(component->name, name) == 0) {
863 static struct snd_soc_dai *snd_soc_find_dai(
864 const struct snd_soc_dai_link_component *dlc)
866 struct snd_soc_component *component;
867 struct snd_soc_dai *dai;
869 /* Find CPU DAI from registered DAIs*/
870 list_for_each_entry(component, &component_list, list) {
871 if (dlc->of_node && component->dev->of_node != dlc->of_node)
873 if (dlc->name && strcmp(component->name, dlc->name))
875 list_for_each_entry(dai, &component->dai_list, list) {
876 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
886 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
888 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
889 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
890 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
891 struct snd_soc_dai_link_component cpu_dai_component;
892 struct snd_soc_dai **codec_dais = rtd->codec_dais;
893 struct snd_soc_platform *platform;
894 const char *platform_name;
897 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
899 cpu_dai_component.name = dai_link->cpu_name;
900 cpu_dai_component.of_node = dai_link->cpu_of_node;
901 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
902 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
904 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
905 dai_link->cpu_dai_name);
906 return -EPROBE_DEFER;
909 rtd->num_codecs = dai_link->num_codecs;
911 /* Find CODEC from registered CODECs */
912 for (i = 0; i < rtd->num_codecs; i++) {
913 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
914 if (!codec_dais[i]) {
915 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
917 return -EPROBE_DEFER;
921 /* Single codec links expect codec and codec_dai in runtime data */
922 rtd->codec_dai = codec_dais[0];
923 rtd->codec = rtd->codec_dai->codec;
925 /* if there's no platform we match on the empty platform */
926 platform_name = dai_link->platform_name;
927 if (!platform_name && !dai_link->platform_of_node)
928 platform_name = "snd-soc-dummy";
930 /* find one from the set of registered platforms */
931 list_for_each_entry(platform, &platform_list, list) {
932 if (dai_link->platform_of_node) {
933 if (platform->dev->of_node !=
934 dai_link->platform_of_node)
937 if (strcmp(platform->component.name, platform_name))
941 rtd->platform = platform;
943 if (!rtd->platform) {
944 dev_err(card->dev, "ASoC: platform %s not registered\n",
945 dai_link->platform_name);
946 return -EPROBE_DEFER;
954 static void soc_remove_component(struct snd_soc_component *component)
956 if (!component->probed)
959 /* This is a HACK and will be removed soon */
960 if (component->codec)
961 list_del(&component->codec->card_list);
963 if (component->remove)
964 component->remove(component);
966 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
968 soc_cleanup_component_debugfs(component);
969 component->probed = 0;
970 module_put(component->dev->driver->owner);
973 static void soc_remove_dai(struct snd_soc_dai *dai, int order)
977 if (dai && dai->probed &&
978 dai->driver->remove_order == order) {
979 if (dai->driver->remove) {
980 err = dai->driver->remove(dai);
983 "ASoC: failed to remove %s: %d\n",
990 static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
992 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
995 /* unregister the rtd device */
996 if (rtd->dev_registered) {
997 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
998 device_remove_file(rtd->dev, &dev_attr_codec_reg);
999 device_unregister(rtd->dev);
1000 rtd->dev_registered = 0;
1003 /* remove the CODEC DAI */
1004 for (i = 0; i < rtd->num_codecs; i++)
1005 soc_remove_dai(rtd->codec_dais[i], order);
1007 soc_remove_dai(rtd->cpu_dai, order);
1010 static void soc_remove_link_components(struct snd_soc_card *card, int num,
1013 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1014 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1015 struct snd_soc_platform *platform = rtd->platform;
1016 struct snd_soc_component *component;
1019 /* remove the platform */
1020 if (platform && platform->component.driver->remove_order == order)
1021 soc_remove_component(&platform->component);
1023 /* remove the CODEC-side CODEC */
1024 for (i = 0; i < rtd->num_codecs; i++) {
1025 component = rtd->codec_dais[i]->component;
1026 if (component->driver->remove_order == order)
1027 soc_remove_component(component);
1030 /* remove any CPU-side CODEC */
1032 if (cpu_dai->component->driver->remove_order == order)
1033 soc_remove_component(cpu_dai->component);
1037 static void soc_remove_dai_links(struct snd_soc_card *card)
1041 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1043 for (dai = 0; dai < card->num_rtd; dai++)
1044 soc_remove_link_dais(card, dai, order);
1047 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1049 for (dai = 0; dai < card->num_rtd; dai++)
1050 soc_remove_link_components(card, dai, order);
1056 static void soc_set_name_prefix(struct snd_soc_card *card,
1057 struct snd_soc_component *component)
1061 if (card->codec_conf == NULL)
1064 for (i = 0; i < card->num_configs; i++) {
1065 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1066 if (map->of_node && component->dev->of_node != map->of_node)
1068 if (map->dev_name && strcmp(component->name, map->dev_name))
1070 component->name_prefix = map->name_prefix;
1075 static int soc_probe_component(struct snd_soc_card *card,
1076 struct snd_soc_component *component)
1078 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1079 struct snd_soc_dai *dai;
1082 if (component->probed)
1085 component->card = card;
1087 soc_set_name_prefix(card, component);
1089 if (!try_module_get(component->dev->driver->owner))
1092 soc_init_component_debugfs(component);
1094 if (component->dapm_widgets) {
1095 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1096 component->num_dapm_widgets);
1099 dev_err(component->dev,
1100 "Failed to create new controls %d\n", ret);
1105 list_for_each_entry(dai, &component->dai_list, list) {
1106 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1108 dev_err(component->dev,
1109 "Failed to create DAI widgets %d\n", ret);
1114 if (component->probe) {
1115 ret = component->probe(component);
1117 dev_err(component->dev,
1118 "ASoC: failed to probe component %d\n", ret);
1122 WARN(dapm->idle_bias_off &&
1123 dapm->bias_level != SND_SOC_BIAS_OFF,
1124 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1128 if (component->controls)
1129 snd_soc_add_component_controls(component, component->controls,
1130 component->num_controls);
1131 if (component->dapm_routes)
1132 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1133 component->num_dapm_routes);
1135 component->probed = 1;
1136 list_add(&dapm->list, &card->dapm_list);
1138 /* This is a HACK and will be removed soon */
1139 if (component->codec)
1140 list_add(&component->codec->card_list, &card->codec_dev_list);
1145 soc_cleanup_component_debugfs(component);
1146 module_put(component->dev->driver->owner);
1151 static void rtd_release(struct device *dev)
1156 static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1161 /* register the rtd device */
1162 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1165 device_initialize(rtd->dev);
1166 rtd->dev->parent = rtd->card->dev;
1167 rtd->dev->release = rtd_release;
1168 dev_set_name(rtd->dev, "%s", name);
1169 dev_set_drvdata(rtd->dev, rtd);
1170 mutex_init(&rtd->pcm_mutex);
1171 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1172 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1173 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1174 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1175 ret = device_add(rtd->dev);
1177 /* calling put_device() here to free the rtd->dev */
1178 put_device(rtd->dev);
1179 dev_err(rtd->card->dev,
1180 "ASoC: failed to register runtime device: %d\n", ret);
1183 rtd->dev_registered = 1;
1186 /* add DAPM sysfs entries for this codec */
1187 ret = snd_soc_dapm_sys_add(rtd->dev);
1190 "ASoC: failed to add codec dapm sysfs entries: %d\n",
1193 /* add codec sysfs entries */
1194 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1197 "ASoC: failed to add codec sysfs files: %d\n",
1204 static int soc_probe_link_components(struct snd_soc_card *card, int num,
1207 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1208 struct snd_soc_platform *platform = rtd->platform;
1209 struct snd_soc_component *component;
1212 /* probe the CPU-side component, if it is a CODEC */
1213 component = rtd->cpu_dai->component;
1214 if (component->driver->probe_order == order) {
1215 ret = soc_probe_component(card, component);
1220 /* probe the CODEC-side components */
1221 for (i = 0; i < rtd->num_codecs; i++) {
1222 component = rtd->codec_dais[i]->component;
1223 if (component->driver->probe_order == order) {
1224 ret = soc_probe_component(card, component);
1230 /* probe the platform */
1231 if (platform->component.driver->probe_order == order) {
1232 ret = soc_probe_component(card, &platform->component);
1240 static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1244 if (!dai->probed && dai->driver->probe_order == order) {
1245 if (dai->driver->probe) {
1246 ret = dai->driver->probe(dai);
1249 "ASoC: failed to probe DAI %s: %d\n",
1261 static int soc_link_dai_widgets(struct snd_soc_card *card,
1262 struct snd_soc_dai_link *dai_link,
1263 struct snd_soc_pcm_runtime *rtd)
1265 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1266 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1267 struct snd_soc_dapm_widget *play_w, *capture_w;
1270 if (rtd->num_codecs > 1)
1271 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1273 /* link the DAI widgets */
1274 play_w = codec_dai->playback_widget;
1275 capture_w = cpu_dai->capture_widget;
1276 if (play_w && capture_w) {
1277 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1280 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1281 play_w->name, capture_w->name, ret);
1286 play_w = cpu_dai->playback_widget;
1287 capture_w = codec_dai->capture_widget;
1288 if (play_w && capture_w) {
1289 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1292 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1293 play_w->name, capture_w->name, ret);
1301 static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1303 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1304 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1305 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1308 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1309 card->name, num, order);
1311 /* set default power off timeout */
1312 rtd->pmdown_time = pmdown_time;
1314 ret = soc_probe_dai(cpu_dai, order);
1318 /* probe the CODEC DAI */
1319 for (i = 0; i < rtd->num_codecs; i++) {
1320 ret = soc_probe_dai(rtd->codec_dais[i], order);
1325 /* complete DAI probe during last probe */
1326 if (order != SND_SOC_COMP_ORDER_LAST)
1329 /* do machine specific initialization */
1330 if (dai_link->init) {
1331 ret = dai_link->init(rtd);
1333 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1334 dai_link->name, ret);
1339 ret = soc_post_component_init(rtd, dai_link->name);
1343 #ifdef CONFIG_DEBUG_FS
1344 /* add DPCM sysfs entries */
1345 if (dai_link->dynamic) {
1346 ret = soc_dpcm_debugfs_add(rtd);
1349 "ASoC: failed to add dpcm sysfs entries: %d\n",
1356 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1358 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1361 if (cpu_dai->driver->compress_dai) {
1362 /*create compress_device"*/
1363 ret = soc_new_compress(rtd, num);
1365 dev_err(card->dev, "ASoC: can't create compress %s\n",
1366 dai_link->stream_name);
1371 if (!dai_link->params) {
1372 /* create the pcm */
1373 ret = soc_new_pcm(rtd, num);
1375 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1376 dai_link->stream_name, ret);
1380 INIT_DELAYED_WORK(&rtd->delayed_work,
1381 codec2codec_close_delayed_work);
1383 /* link the DAI widgets */
1384 ret = soc_link_dai_widgets(card, dai_link, rtd);
1390 /* add platform data for AC97 devices */
1391 for (i = 0; i < rtd->num_codecs; i++) {
1392 if (rtd->codec_dais[i]->driver->ac97_control)
1393 snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
1394 rtd->cpu_dai->ac97_pdata);
1400 #ifdef CONFIG_SND_SOC_AC97_BUS
1401 static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1402 struct snd_soc_dai *codec_dai)
1406 /* Only instantiate AC97 if not already done by the adaptor
1407 * for the generic AC97 subsystem.
1409 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
1411 * It is possible that the AC97 device is already registered to
1412 * the device subsystem. This happens when the device is created
1413 * via snd_ac97_mixer(). Currently only SoC codec that does so
1414 * is the generic AC97 glue but others migh emerge.
1416 * In those cases we don't try to register the device again.
1418 if (!codec->ac97_created)
1421 ret = soc_ac97_dev_register(codec);
1424 "ASoC: AC97 device register failed: %d\n", ret);
1428 codec->ac97_registered = 1;
1433 static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
1435 if (codec->ac97_registered) {
1436 soc_ac97_dev_unregister(codec);
1437 codec->ac97_registered = 0;
1441 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1445 for (i = 0; i < rtd->num_codecs; i++) {
1446 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1448 ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
1451 soc_unregister_ac97_codec(codec_dai->codec);
1459 static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1463 for (i = 0; i < rtd->num_codecs; i++)
1464 soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
1468 static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1470 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1471 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1472 const char *name = aux_dev->codec_name;
1474 rtd->component = soc_find_component(aux_dev->codec_of_node, name);
1475 if (!rtd->component) {
1476 if (aux_dev->codec_of_node)
1477 name = of_node_full_name(aux_dev->codec_of_node);
1479 dev_err(card->dev, "ASoC: %s not registered\n", name);
1480 return -EPROBE_DEFER;
1484 * Some places still reference rtd->codec, so we have to keep that
1485 * initialized if the component is a CODEC. Once all those references
1486 * have been removed, this code can be removed as well.
1488 rtd->codec = rtd->component->codec;
1493 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1495 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1496 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1499 ret = soc_probe_component(card, rtd->component);
1503 /* do machine specific initialization */
1504 if (aux_dev->init) {
1505 ret = aux_dev->init(rtd->component);
1507 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1508 aux_dev->name, ret);
1513 return soc_post_component_init(rtd, aux_dev->name);
1516 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1518 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1519 struct snd_soc_component *component = rtd->component;
1521 /* unregister the rtd device */
1522 if (rtd->dev_registered) {
1523 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1524 device_unregister(rtd->dev);
1525 rtd->dev_registered = 0;
1528 if (component && component->probed)
1529 soc_remove_component(component);
1532 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1536 if (codec->cache_init)
1539 ret = snd_soc_cache_init(codec);
1542 "ASoC: Failed to set cache compression type: %d\n",
1546 codec->cache_init = 1;
1550 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1552 struct snd_soc_codec *codec;
1553 struct snd_soc_dai_link *dai_link;
1554 int ret, i, order, dai_fmt;
1556 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1559 for (i = 0; i < card->num_links; i++) {
1560 ret = soc_bind_dai_link(card, i);
1565 /* bind aux_devs too */
1566 for (i = 0; i < card->num_aux_devs; i++) {
1567 ret = soc_bind_aux_dev(card, i);
1572 /* initialize the register cache for each available codec */
1573 list_for_each_entry(codec, &codec_list, list) {
1574 if (codec->cache_init)
1576 ret = snd_soc_init_codec_cache(codec);
1581 /* card bind complete so register a sound card */
1582 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1583 card->owner, 0, &card->snd_card);
1586 "ASoC: can't create sound card for card %s: %d\n",
1591 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1592 card->dapm.dev = card->dev;
1593 card->dapm.card = card;
1594 list_add(&card->dapm.list, &card->dapm_list);
1596 #ifdef CONFIG_DEBUG_FS
1597 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1600 #ifdef CONFIG_PM_SLEEP
1601 /* deferred resume work */
1602 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1605 if (card->dapm_widgets)
1606 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1607 card->num_dapm_widgets);
1609 /* initialise the sound card only once */
1611 ret = card->probe(card);
1613 goto card_probe_error;
1616 /* probe all components used by DAI links on this card */
1617 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1619 for (i = 0; i < card->num_links; i++) {
1620 ret = soc_probe_link_components(card, i, order);
1623 "ASoC: failed to instantiate card %d\n",
1630 /* probe all DAI links on this card */
1631 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1633 for (i = 0; i < card->num_links; i++) {
1634 ret = soc_probe_link_dais(card, i, order);
1637 "ASoC: failed to instantiate card %d\n",
1644 for (i = 0; i < card->num_aux_devs; i++) {
1645 ret = soc_probe_aux_dev(card, i);
1648 "ASoC: failed to add auxiliary devices %d\n",
1650 goto probe_aux_dev_err;
1654 snd_soc_dapm_link_dai_widgets(card);
1655 snd_soc_dapm_connect_dai_link_widgets(card);
1658 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1660 if (card->dapm_routes)
1661 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1662 card->num_dapm_routes);
1664 for (i = 0; i < card->num_links; i++) {
1665 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1666 dai_link = &card->dai_link[i];
1667 dai_fmt = dai_link->dai_fmt;
1670 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1673 for (j = 0; j < rtd->num_codecs; j++) {
1674 struct snd_soc_dai *codec_dai = codec_dais[j];
1676 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1677 if (ret != 0 && ret != -ENOTSUPP)
1678 dev_warn(codec_dai->dev,
1679 "ASoC: Failed to set DAI format: %d\n",
1684 /* If this is a regular CPU link there will be a platform */
1686 (dai_link->platform_name || dai_link->platform_of_node)) {
1687 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1689 if (ret != 0 && ret != -ENOTSUPP)
1690 dev_warn(card->rtd[i].cpu_dai->dev,
1691 "ASoC: Failed to set DAI format: %d\n",
1693 } else if (dai_fmt) {
1694 /* Flip the polarity for the "CPU" end */
1695 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1696 switch (dai_link->dai_fmt &
1697 SND_SOC_DAIFMT_MASTER_MASK) {
1698 case SND_SOC_DAIFMT_CBM_CFM:
1699 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1701 case SND_SOC_DAIFMT_CBM_CFS:
1702 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1704 case SND_SOC_DAIFMT_CBS_CFM:
1705 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1707 case SND_SOC_DAIFMT_CBS_CFS:
1708 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1712 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1714 if (ret != 0 && ret != -ENOTSUPP)
1715 dev_warn(card->rtd[i].cpu_dai->dev,
1716 "ASoC: Failed to set DAI format: %d\n",
1721 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1723 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1724 "%s", card->long_name ? card->long_name : card->name);
1725 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1726 "%s", card->driver_name ? card->driver_name : card->name);
1727 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1728 switch (card->snd_card->driver[i]) {
1734 if (!isalnum(card->snd_card->driver[i]))
1735 card->snd_card->driver[i] = '_';
1740 if (card->late_probe) {
1741 ret = card->late_probe(card);
1743 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
1745 goto probe_aux_dev_err;
1749 if (card->fully_routed)
1750 snd_soc_dapm_auto_nc_pins(card);
1752 snd_soc_dapm_new_widgets(card);
1754 ret = snd_card_register(card->snd_card);
1756 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1758 goto probe_aux_dev_err;
1761 #ifdef CONFIG_SND_SOC_AC97_BUS
1762 /* register any AC97 codecs */
1763 for (i = 0; i < card->num_rtd; i++) {
1764 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1767 "ASoC: failed to register AC97: %d\n", ret);
1769 soc_unregister_ac97_dai_link(&card->rtd[i]);
1770 goto probe_aux_dev_err;
1775 card->instantiated = 1;
1776 snd_soc_dapm_sync(&card->dapm);
1777 mutex_unlock(&card->mutex);
1782 for (i = 0; i < card->num_aux_devs; i++)
1783 soc_remove_aux_dev(card, i);
1786 soc_remove_dai_links(card);
1792 snd_card_free(card->snd_card);
1795 mutex_unlock(&card->mutex);
1800 /* probes a new socdev */
1801 static int soc_probe(struct platform_device *pdev)
1803 struct snd_soc_card *card = platform_get_drvdata(pdev);
1806 * no card, so machine driver should be registering card
1807 * we should not be here in that case so ret error
1812 dev_warn(&pdev->dev,
1813 "ASoC: machine %s should use snd_soc_register_card()\n",
1816 /* Bodge while we unpick instantiation */
1817 card->dev = &pdev->dev;
1819 return snd_soc_register_card(card);
1822 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1826 /* make sure any delayed work runs */
1827 for (i = 0; i < card->num_rtd; i++) {
1828 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1829 flush_delayed_work(&rtd->delayed_work);
1832 /* remove auxiliary devices */
1833 for (i = 0; i < card->num_aux_devs; i++)
1834 soc_remove_aux_dev(card, i);
1836 /* remove and free each DAI */
1837 soc_remove_dai_links(card);
1839 soc_cleanup_card_debugfs(card);
1841 /* remove the card */
1845 snd_soc_dapm_free(&card->dapm);
1847 snd_card_free(card->snd_card);
1852 /* removes a socdev */
1853 static int soc_remove(struct platform_device *pdev)
1855 struct snd_soc_card *card = platform_get_drvdata(pdev);
1857 snd_soc_unregister_card(card);
1861 int snd_soc_poweroff(struct device *dev)
1863 struct snd_soc_card *card = dev_get_drvdata(dev);
1866 if (!card->instantiated)
1869 /* Flush out pmdown_time work - we actually do want to run it
1870 * now, we're shutting down so no imminent restart. */
1871 for (i = 0; i < card->num_rtd; i++) {
1872 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1873 flush_delayed_work(&rtd->delayed_work);
1876 snd_soc_dapm_shutdown(card);
1878 /* deactivate pins to sleep state */
1879 for (i = 0; i < card->num_rtd; i++) {
1880 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1881 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1884 pinctrl_pm_select_sleep_state(cpu_dai->dev);
1885 for (j = 0; j < rtd->num_codecs; j++) {
1886 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
1887 pinctrl_pm_select_sleep_state(codec_dai->dev);
1893 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1895 const struct dev_pm_ops snd_soc_pm_ops = {
1896 .suspend = snd_soc_suspend,
1897 .resume = snd_soc_resume,
1898 .freeze = snd_soc_suspend,
1899 .thaw = snd_soc_resume,
1900 .poweroff = snd_soc_poweroff,
1901 .restore = snd_soc_resume,
1903 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1905 /* ASoC platform driver */
1906 static struct platform_driver soc_driver = {
1908 .name = "soc-audio",
1909 .owner = THIS_MODULE,
1910 .pm = &snd_soc_pm_ops,
1913 .remove = soc_remove,
1917 * snd_soc_new_ac97_codec - initailise AC97 device
1918 * @codec: audio codec
1919 * @ops: AC97 bus operations
1920 * @num: AC97 codec number
1922 * Initialises AC97 codec resources for use by ad-hoc devices only.
1924 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1925 struct snd_ac97_bus_ops *ops, int num)
1927 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1928 if (codec->ac97 == NULL)
1931 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1932 if (codec->ac97->bus == NULL) {
1938 codec->ac97->bus->ops = ops;
1939 codec->ac97->num = num;
1942 * Mark the AC97 device to be created by us. This way we ensure that the
1943 * device will be registered with the device subsystem later on.
1945 codec->ac97_created = 1;
1949 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1951 static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
1953 static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
1955 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
1957 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
1959 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
1963 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
1965 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
1969 static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
1971 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
1973 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
1975 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
1976 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
1977 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
1981 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
1983 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
1987 static int snd_soc_ac97_parse_pinctl(struct device *dev,
1988 struct snd_ac97_reset_cfg *cfg)
1991 struct pinctrl_state *state;
1995 p = devm_pinctrl_get(dev);
1997 dev_err(dev, "Failed to get pinctrl\n");
2002 state = pinctrl_lookup_state(p, "ac97-reset");
2003 if (IS_ERR(state)) {
2004 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
2005 return PTR_ERR(state);
2007 cfg->pstate_reset = state;
2009 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2010 if (IS_ERR(state)) {
2011 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
2012 return PTR_ERR(state);
2014 cfg->pstate_warm_reset = state;
2016 state = pinctrl_lookup_state(p, "ac97-running");
2017 if (IS_ERR(state)) {
2018 dev_err(dev, "Can't find pinctrl state ac97-running\n");
2019 return PTR_ERR(state);
2021 cfg->pstate_run = state;
2023 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2025 dev_err(dev, "Can't find ac97-sync gpio\n");
2028 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2030 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2033 cfg->gpio_sync = gpio;
2035 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2037 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2040 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2042 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2045 cfg->gpio_sdata = gpio;
2047 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2049 dev_err(dev, "Can't find ac97-reset gpio\n");
2052 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2054 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2057 cfg->gpio_reset = gpio;
2062 struct snd_ac97_bus_ops *soc_ac97_ops;
2063 EXPORT_SYMBOL_GPL(soc_ac97_ops);
2065 int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2067 if (ops == soc_ac97_ops)
2070 if (soc_ac97_ops && ops)
2077 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2080 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2082 * This function sets the reset and warm_reset properties of ops and parses
2083 * the device node of pdev to get pinctrl states and gpio numbers to use.
2085 int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2086 struct platform_device *pdev)
2088 struct device *dev = &pdev->dev;
2089 struct snd_ac97_reset_cfg cfg;
2092 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2096 ret = snd_soc_set_ac97_ops(ops);
2100 ops->warm_reset = snd_soc_ac97_warm_reset;
2101 ops->reset = snd_soc_ac97_reset;
2103 snd_ac97_rst_cfg = cfg;
2106 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2109 * snd_soc_free_ac97_codec - free AC97 codec device
2110 * @codec: audio codec
2112 * Frees AC97 codec device resources.
2114 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2116 #ifdef CONFIG_SND_SOC_AC97_BUS
2117 soc_unregister_ac97_codec(codec);
2119 kfree(codec->ac97->bus);
2122 codec->ac97_created = 0;
2124 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2127 * snd_soc_cnew - create new control
2128 * @_template: control template
2129 * @data: control private data
2130 * @long_name: control long name
2131 * @prefix: control name prefix
2133 * Create a new mixer control from a template control.
2135 * Returns 0 for success, else error.
2137 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2138 void *data, const char *long_name,
2141 struct snd_kcontrol_new template;
2142 struct snd_kcontrol *kcontrol;
2145 memcpy(&template, _template, sizeof(template));
2149 long_name = template.name;
2152 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2156 template.name = name;
2158 template.name = long_name;
2161 kcontrol = snd_ctl_new1(&template, data);
2167 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2169 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2170 const struct snd_kcontrol_new *controls, int num_controls,
2171 const char *prefix, void *data)
2175 for (i = 0; i < num_controls; i++) {
2176 const struct snd_kcontrol_new *control = &controls[i];
2177 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2178 control->name, prefix));
2180 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2181 control->name, err);
2189 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2192 struct snd_card *card = soc_card->snd_card;
2193 struct snd_kcontrol *kctl;
2195 if (unlikely(!name))
2198 list_for_each_entry(kctl, &card->controls, list)
2199 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2203 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2206 * snd_soc_add_component_controls - Add an array of controls to a component.
2208 * @component: Component to add controls to
2209 * @controls: Array of controls to add
2210 * @num_controls: Number of elements in the array
2212 * Return: 0 for success, else error.
2214 int snd_soc_add_component_controls(struct snd_soc_component *component,
2215 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2217 struct snd_card *card = component->card->snd_card;
2219 return snd_soc_add_controls(card, component->dev, controls,
2220 num_controls, component->name_prefix, component);
2222 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2225 * snd_soc_add_codec_controls - add an array of controls to a codec.
2226 * Convenience function to add a list of controls. Many codecs were
2227 * duplicating this code.
2229 * @codec: codec to add controls to
2230 * @controls: array of controls to add
2231 * @num_controls: number of elements in the array
2233 * Return 0 for success, else error.
2235 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2236 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2238 return snd_soc_add_component_controls(&codec->component, controls,
2241 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2244 * snd_soc_add_platform_controls - add an array of controls to a platform.
2245 * Convenience function to add a list of controls.
2247 * @platform: platform to add controls to
2248 * @controls: array of controls to add
2249 * @num_controls: number of elements in the array
2251 * Return 0 for success, else error.
2253 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2254 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2256 return snd_soc_add_component_controls(&platform->component, controls,
2259 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2262 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2263 * Convenience function to add a list of controls.
2265 * @soc_card: SoC card to add controls to
2266 * @controls: array of controls to add
2267 * @num_controls: number of elements in the array
2269 * Return 0 for success, else error.
2271 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2272 const struct snd_kcontrol_new *controls, int num_controls)
2274 struct snd_card *card = soc_card->snd_card;
2276 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2279 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2282 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2283 * Convienience function to add a list of controls.
2285 * @dai: DAI to add controls to
2286 * @controls: array of controls to add
2287 * @num_controls: number of elements in the array
2289 * Return 0 for success, else error.
2291 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2292 const struct snd_kcontrol_new *controls, int num_controls)
2294 struct snd_card *card = dai->component->card->snd_card;
2296 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2299 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2302 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2304 * @clk_id: DAI specific clock ID
2305 * @freq: new clock frequency in Hz
2306 * @dir: new clock direction - input/output.
2308 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2310 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2311 unsigned int freq, int dir)
2313 if (dai->driver && dai->driver->ops->set_sysclk)
2314 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2315 else if (dai->codec && dai->codec->driver->set_sysclk)
2316 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
2321 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2324 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2326 * @clk_id: DAI specific clock ID
2327 * @source: Source for the clock
2328 * @freq: new clock frequency in Hz
2329 * @dir: new clock direction - input/output.
2331 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2333 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2334 int source, unsigned int freq, int dir)
2336 if (codec->driver->set_sysclk)
2337 return codec->driver->set_sysclk(codec, clk_id, source,
2342 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2345 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2347 * @div_id: DAI specific clock divider ID
2348 * @div: new clock divisor.
2350 * Configures the clock dividers. This is used to derive the best DAI bit and
2351 * frame clocks from the system or master clock. It's best to set the DAI bit
2352 * and frame clocks as low as possible to save system power.
2354 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2355 int div_id, int div)
2357 if (dai->driver && dai->driver->ops->set_clkdiv)
2358 return dai->driver->ops->set_clkdiv(dai, div_id, div);
2362 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2365 * snd_soc_dai_set_pll - configure DAI PLL.
2367 * @pll_id: DAI specific PLL ID
2368 * @source: DAI specific source for the PLL
2369 * @freq_in: PLL input clock frequency in Hz
2370 * @freq_out: requested PLL output clock frequency in Hz
2372 * Configures and enables PLL to generate output clock based on input clock.
2374 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2375 unsigned int freq_in, unsigned int freq_out)
2377 if (dai->driver && dai->driver->ops->set_pll)
2378 return dai->driver->ops->set_pll(dai, pll_id, source,
2380 else if (dai->codec && dai->codec->driver->set_pll)
2381 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2386 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2389 * snd_soc_codec_set_pll - configure codec PLL.
2391 * @pll_id: DAI specific PLL ID
2392 * @source: DAI specific source for the PLL
2393 * @freq_in: PLL input clock frequency in Hz
2394 * @freq_out: requested PLL output clock frequency in Hz
2396 * Configures and enables PLL to generate output clock based on input clock.
2398 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2399 unsigned int freq_in, unsigned int freq_out)
2401 if (codec->driver->set_pll)
2402 return codec->driver->set_pll(codec, pll_id, source,
2407 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2410 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2412 * @ratio Ratio of BCLK to Sample rate.
2414 * Configures the DAI for a preset BCLK to sample rate ratio.
2416 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2418 if (dai->driver && dai->driver->ops->set_bclk_ratio)
2419 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2423 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2426 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2428 * @fmt: SND_SOC_DAIFMT_ format value.
2430 * Configures the DAI hardware format and clocking.
2432 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2434 if (dai->driver == NULL)
2436 if (dai->driver->ops->set_fmt == NULL)
2438 return dai->driver->ops->set_fmt(dai, fmt);
2440 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2443 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
2444 * @slots: Number of slots in use.
2445 * @tx_mask: bitmask representing active TX slots.
2446 * @rx_mask: bitmask representing active RX slots.
2448 * Generates the TDM tx and rx slot default masks for DAI.
2450 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
2451 unsigned int *tx_mask,
2452 unsigned int *rx_mask)
2454 if (*tx_mask || *rx_mask)
2460 *tx_mask = (1 << slots) - 1;
2461 *rx_mask = (1 << slots) - 1;
2467 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2469 * @tx_mask: bitmask representing active TX slots.
2470 * @rx_mask: bitmask representing active RX slots.
2471 * @slots: Number of slots in use.
2472 * @slot_width: Width in bits for each slot.
2474 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2477 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2478 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2480 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2481 dai->driver->ops->xlate_tdm_slot_mask(slots,
2482 &tx_mask, &rx_mask);
2484 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
2486 dai->tx_mask = tx_mask;
2487 dai->rx_mask = rx_mask;
2489 if (dai->driver && dai->driver->ops->set_tdm_slot)
2490 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2495 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2498 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2500 * @tx_num: how many TX channels
2501 * @tx_slot: pointer to an array which imply the TX slot number channel
2503 * @rx_num: how many RX channels
2504 * @rx_slot: pointer to an array which imply the RX slot number channel
2507 * configure the relationship between channel number and TDM slot number.
2509 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2510 unsigned int tx_num, unsigned int *tx_slot,
2511 unsigned int rx_num, unsigned int *rx_slot)
2513 if (dai->driver && dai->driver->ops->set_channel_map)
2514 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2519 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2522 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2524 * @tristate: tristate enable
2526 * Tristates the DAI so that others can use it.
2528 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2530 if (dai->driver && dai->driver->ops->set_tristate)
2531 return dai->driver->ops->set_tristate(dai, tristate);
2535 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2538 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2540 * @mute: mute enable
2541 * @direction: stream to mute
2543 * Mutes the DAI DAC.
2545 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2551 if (dai->driver->ops->mute_stream)
2552 return dai->driver->ops->mute_stream(dai, mute, direction);
2553 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2554 dai->driver->ops->digital_mute)
2555 return dai->driver->ops->digital_mute(dai, mute);
2559 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2561 static int snd_soc_init_multicodec(struct snd_soc_card *card,
2562 struct snd_soc_dai_link *dai_link)
2564 /* Legacy codec/codec_dai link is a single entry in multicodec */
2565 if (dai_link->codec_name || dai_link->codec_of_node ||
2566 dai_link->codec_dai_name) {
2567 dai_link->num_codecs = 1;
2569 dai_link->codecs = devm_kzalloc(card->dev,
2570 sizeof(struct snd_soc_dai_link_component),
2572 if (!dai_link->codecs)
2575 dai_link->codecs[0].name = dai_link->codec_name;
2576 dai_link->codecs[0].of_node = dai_link->codec_of_node;
2577 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
2580 if (!dai_link->codecs) {
2581 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
2589 * snd_soc_register_card - Register a card with the ASoC core
2591 * @card: Card to register
2594 int snd_soc_register_card(struct snd_soc_card *card)
2598 if (!card->name || !card->dev)
2601 for (i = 0; i < card->num_links; i++) {
2602 struct snd_soc_dai_link *link = &card->dai_link[i];
2604 ret = snd_soc_init_multicodec(card, link);
2606 dev_err(card->dev, "ASoC: failed to init multicodec\n");
2610 for (j = 0; j < link->num_codecs; j++) {
2612 * Codec must be specified by 1 of name or OF node,
2613 * not both or neither.
2615 if (!!link->codecs[j].name ==
2616 !!link->codecs[j].of_node) {
2617 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
2621 /* Codec DAI name must be specified */
2622 if (!link->codecs[j].dai_name) {
2623 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
2630 * Platform may be specified by either name or OF node, but
2631 * can be left unspecified, and a dummy platform will be used.
2633 if (link->platform_name && link->platform_of_node) {
2635 "ASoC: Both platform name/of_node are set for %s\n",
2641 * CPU device may be specified by either name or OF node, but
2642 * can be left unspecified, and will be matched based on DAI
2645 if (link->cpu_name && link->cpu_of_node) {
2647 "ASoC: Neither/both cpu name/of_node are set for %s\n",
2652 * At least one of CPU DAI name or CPU device name/node must be
2655 if (!link->cpu_dai_name &&
2656 !(link->cpu_name || link->cpu_of_node)) {
2658 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
2664 dev_set_drvdata(card->dev, card);
2666 snd_soc_initialize_card_lists(card);
2668 soc_init_card_debugfs(card);
2670 card->rtd = devm_kzalloc(card->dev,
2671 sizeof(struct snd_soc_pcm_runtime) *
2672 (card->num_links + card->num_aux_devs),
2674 if (card->rtd == NULL)
2677 card->rtd_aux = &card->rtd[card->num_links];
2679 for (i = 0; i < card->num_links; i++) {
2680 card->rtd[i].card = card;
2681 card->rtd[i].dai_link = &card->dai_link[i];
2682 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
2683 sizeof(struct snd_soc_dai *) *
2684 (card->rtd[i].dai_link->num_codecs),
2686 if (card->rtd[i].codec_dais == NULL)
2690 for (i = 0; i < card->num_aux_devs; i++)
2691 card->rtd_aux[i].card = card;
2693 INIT_LIST_HEAD(&card->dapm_dirty);
2694 card->instantiated = 0;
2695 mutex_init(&card->mutex);
2696 mutex_init(&card->dapm_mutex);
2698 ret = snd_soc_instantiate_card(card);
2700 soc_cleanup_card_debugfs(card);
2702 /* deactivate pins to sleep state */
2703 for (i = 0; i < card->num_rtd; i++) {
2704 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2705 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2708 for (j = 0; j < rtd->num_codecs; j++) {
2709 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2710 if (!codec_dai->active)
2711 pinctrl_pm_select_sleep_state(codec_dai->dev);
2714 if (!cpu_dai->active)
2715 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2720 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2723 * snd_soc_unregister_card - Unregister a card with the ASoC core
2725 * @card: Card to unregister
2728 int snd_soc_unregister_card(struct snd_soc_card *card)
2730 if (card->instantiated) {
2731 card->instantiated = false;
2732 snd_soc_dapm_shutdown(card);
2733 soc_cleanup_card_resources(card);
2735 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2739 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2742 * Simplify DAI link configuration by removing ".-1" from device names
2743 * and sanitizing names.
2745 static char *fmt_single_name(struct device *dev, int *id)
2747 char *found, name[NAME_SIZE];
2750 if (dev_name(dev) == NULL)
2753 strlcpy(name, dev_name(dev), NAME_SIZE);
2755 /* are we a "%s.%d" name (platform and SPI components) */
2756 found = strstr(name, dev->driver->name);
2759 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2761 /* discard ID from name if ID == -1 */
2763 found[strlen(dev->driver->name)] = '\0';
2767 /* I2C component devices are named "bus-addr" */
2768 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2769 char tmp[NAME_SIZE];
2771 /* create unique ID number from I2C addr and bus */
2772 *id = ((id1 & 0xffff) << 16) + id2;
2774 /* sanitize component name for DAI link creation */
2775 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2776 strlcpy(name, tmp, NAME_SIZE);
2781 return kstrdup(name, GFP_KERNEL);
2785 * Simplify DAI link naming for single devices with multiple DAIs by removing
2786 * any ".-1" and using the DAI name (instead of device name).
2788 static inline char *fmt_multiple_name(struct device *dev,
2789 struct snd_soc_dai_driver *dai_drv)
2791 if (dai_drv->name == NULL) {
2793 "ASoC: error - multiple DAI %s registered with no name\n",
2798 return kstrdup(dai_drv->name, GFP_KERNEL);
2802 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2804 * @component: The component for which the DAIs should be unregistered
2806 static void snd_soc_unregister_dais(struct snd_soc_component *component)
2808 struct snd_soc_dai *dai, *_dai;
2810 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
2811 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2813 list_del(&dai->list);
2820 * snd_soc_register_dais - Register a DAI with the ASoC core
2822 * @component: The component the DAIs are registered for
2823 * @dai_drv: DAI driver to use for the DAIs
2824 * @count: Number of DAIs
2825 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
2828 static int snd_soc_register_dais(struct snd_soc_component *component,
2829 struct snd_soc_dai_driver *dai_drv, size_t count,
2830 bool legacy_dai_naming)
2832 struct device *dev = component->dev;
2833 struct snd_soc_dai *dai;
2837 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
2839 component->dai_drv = dai_drv;
2840 component->num_dai = count;
2842 for (i = 0; i < count; i++) {
2844 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2851 * Back in the old days when we still had component-less DAIs,
2852 * instead of having a static name, component-less DAIs would
2853 * inherit the name of the parent device so it is possible to
2854 * register multiple instances of the DAI. We still need to keep
2855 * the same naming style even though those DAIs are not
2856 * component-less anymore.
2858 if (count == 1 && legacy_dai_naming) {
2859 dai->name = fmt_single_name(dev, &dai->id);
2861 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
2863 dai->id = dai_drv[i].id;
2867 if (dai->name == NULL) {
2873 dai->component = component;
2875 dai->driver = &dai_drv[i];
2876 if (!dai->driver->ops)
2877 dai->driver->ops = &null_dai_ops;
2879 list_add(&dai->list, &component->dai_list);
2881 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2887 snd_soc_unregister_dais(component);
2892 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
2893 enum snd_soc_dapm_type type, int subseq)
2895 struct snd_soc_component *component = dapm->component;
2897 component->driver->seq_notifier(component, type, subseq);
2900 static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
2903 struct snd_soc_component *component = dapm->component;
2905 return component->driver->stream_event(component, event);
2908 static int snd_soc_component_initialize(struct snd_soc_component *component,
2909 const struct snd_soc_component_driver *driver, struct device *dev)
2911 struct snd_soc_dapm_context *dapm;
2913 component->name = fmt_single_name(dev, &component->id);
2914 if (!component->name) {
2915 dev_err(dev, "ASoC: Failed to allocate name\n");
2919 component->dev = dev;
2920 component->driver = driver;
2921 component->probe = component->driver->probe;
2922 component->remove = component->driver->remove;
2924 if (!component->dapm_ptr)
2925 component->dapm_ptr = &component->dapm;
2927 dapm = component->dapm_ptr;
2929 dapm->component = component;
2930 dapm->bias_level = SND_SOC_BIAS_OFF;
2931 dapm->idle_bias_off = true;
2932 if (driver->seq_notifier)
2933 dapm->seq_notifier = snd_soc_component_seq_notifier;
2934 if (driver->stream_event)
2935 dapm->stream_event = snd_soc_component_stream_event;
2937 component->controls = driver->controls;
2938 component->num_controls = driver->num_controls;
2939 component->dapm_widgets = driver->dapm_widgets;
2940 component->num_dapm_widgets = driver->num_dapm_widgets;
2941 component->dapm_routes = driver->dapm_routes;
2942 component->num_dapm_routes = driver->num_dapm_routes;
2944 INIT_LIST_HEAD(&component->dai_list);
2945 mutex_init(&component->io_mutex);
2950 static void snd_soc_component_init_regmap(struct snd_soc_component *component)
2952 if (!component->regmap)
2953 component->regmap = dev_get_regmap(component->dev, NULL);
2954 if (component->regmap) {
2955 int val_bytes = regmap_get_val_bytes(component->regmap);
2956 /* Errors are legitimate for non-integer byte multiples */
2958 component->val_bytes = val_bytes;
2962 static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
2964 if (!component->write && !component->read)
2965 snd_soc_component_init_regmap(component);
2967 list_add(&component->list, &component_list);
2970 static void snd_soc_component_add(struct snd_soc_component *component)
2972 mutex_lock(&client_mutex);
2973 snd_soc_component_add_unlocked(component);
2974 mutex_unlock(&client_mutex);
2977 static void snd_soc_component_cleanup(struct snd_soc_component *component)
2979 snd_soc_unregister_dais(component);
2980 kfree(component->name);
2983 static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2985 list_del(&component->list);
2988 static void snd_soc_component_del(struct snd_soc_component *component)
2990 mutex_lock(&client_mutex);
2991 snd_soc_component_del_unlocked(component);
2992 mutex_unlock(&client_mutex);
2995 int snd_soc_register_component(struct device *dev,
2996 const struct snd_soc_component_driver *cmpnt_drv,
2997 struct snd_soc_dai_driver *dai_drv,
3000 struct snd_soc_component *cmpnt;
3003 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
3005 dev_err(dev, "ASoC: Failed to allocate memory\n");
3009 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
3013 cmpnt->ignore_pmdown_time = true;
3014 cmpnt->registered_as_component = true;
3016 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
3018 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
3022 snd_soc_component_add(cmpnt);
3027 snd_soc_component_cleanup(cmpnt);
3032 EXPORT_SYMBOL_GPL(snd_soc_register_component);
3035 * snd_soc_unregister_component - Unregister a component from the ASoC core
3038 void snd_soc_unregister_component(struct device *dev)
3040 struct snd_soc_component *cmpnt;
3042 list_for_each_entry(cmpnt, &component_list, list) {
3043 if (dev == cmpnt->dev && cmpnt->registered_as_component)
3049 snd_soc_component_del(cmpnt);
3050 snd_soc_component_cleanup(cmpnt);
3053 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3055 static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
3057 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3059 return platform->driver->probe(platform);
3062 static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
3064 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3066 platform->driver->remove(platform);
3070 * snd_soc_add_platform - Add a platform to the ASoC core
3071 * @dev: The parent device for the platform
3072 * @platform: The platform to add
3073 * @platform_driver: The driver for the platform
3075 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
3076 const struct snd_soc_platform_driver *platform_drv)
3080 ret = snd_soc_component_initialize(&platform->component,
3081 &platform_drv->component_driver, dev);
3085 platform->dev = dev;
3086 platform->driver = platform_drv;
3088 if (platform_drv->probe)
3089 platform->component.probe = snd_soc_platform_drv_probe;
3090 if (platform_drv->remove)
3091 platform->component.remove = snd_soc_platform_drv_remove;
3093 #ifdef CONFIG_DEBUG_FS
3094 platform->component.debugfs_prefix = "platform";
3097 mutex_lock(&client_mutex);
3098 snd_soc_component_add_unlocked(&platform->component);
3099 list_add(&platform->list, &platform_list);
3100 mutex_unlock(&client_mutex);
3102 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
3103 platform->component.name);
3107 EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3110 * snd_soc_register_platform - Register a platform with the ASoC core
3112 * @platform: platform to register
3114 int snd_soc_register_platform(struct device *dev,
3115 const struct snd_soc_platform_driver *platform_drv)
3117 struct snd_soc_platform *platform;
3120 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3122 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3123 if (platform == NULL)
3126 ret = snd_soc_add_platform(dev, platform, platform_drv);
3132 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3135 * snd_soc_remove_platform - Remove a platform from the ASoC core
3136 * @platform: the platform to remove
3138 void snd_soc_remove_platform(struct snd_soc_platform *platform)
3141 mutex_lock(&client_mutex);
3142 list_del(&platform->list);
3143 snd_soc_component_del_unlocked(&platform->component);
3144 mutex_unlock(&client_mutex);
3146 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3147 platform->component.name);
3149 snd_soc_component_cleanup(&platform->component);
3151 EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
3153 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
3155 struct snd_soc_platform *platform;
3157 list_for_each_entry(platform, &platform_list, list) {
3158 if (dev == platform->dev)
3164 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
3167 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3169 * @platform: platform to unregister
3171 void snd_soc_unregister_platform(struct device *dev)
3173 struct snd_soc_platform *platform;
3175 platform = snd_soc_lookup_platform(dev);
3179 snd_soc_remove_platform(platform);
3182 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3184 static u64 codec_format_map[] = {
3185 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3186 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3187 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3188 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3189 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3190 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3191 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3192 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3193 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3194 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3195 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3196 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3197 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3198 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3199 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3200 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3203 /* Fix up the DAI formats for endianness: codecs don't actually see
3204 * the endianness of the data but we're using the CPU format
3205 * definitions which do need to include endianness so we ensure that
3206 * codec DAIs always have both big and little endian variants set.
3208 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3212 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3213 if (stream->formats & codec_format_map[i])
3214 stream->formats |= codec_format_map[i];
3217 static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3219 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3221 return codec->driver->probe(codec);
3224 static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3226 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3228 codec->driver->remove(codec);
3231 static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3232 unsigned int reg, unsigned int val)
3234 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3236 return codec->driver->write(codec, reg, val);
3239 static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3240 unsigned int reg, unsigned int *val)
3242 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3244 *val = codec->driver->read(codec, reg);
3249 static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
3250 enum snd_soc_bias_level level)
3252 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
3254 return codec->driver->set_bias_level(codec, level);
3258 * snd_soc_register_codec - Register a codec with the ASoC core
3260 * @codec: codec to register
3262 int snd_soc_register_codec(struct device *dev,
3263 const struct snd_soc_codec_driver *codec_drv,
3264 struct snd_soc_dai_driver *dai_drv,
3267 struct snd_soc_codec *codec;
3268 struct snd_soc_dai *dai;
3271 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3273 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3277 codec->component.dapm_ptr = &codec->dapm;
3278 codec->component.codec = codec;
3280 ret = snd_soc_component_initialize(&codec->component,
3281 &codec_drv->component_driver, dev);
3285 if (codec_drv->controls) {
3286 codec->component.controls = codec_drv->controls;
3287 codec->component.num_controls = codec_drv->num_controls;
3289 if (codec_drv->dapm_widgets) {
3290 codec->component.dapm_widgets = codec_drv->dapm_widgets;
3291 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
3293 if (codec_drv->dapm_routes) {
3294 codec->component.dapm_routes = codec_drv->dapm_routes;
3295 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
3298 if (codec_drv->probe)
3299 codec->component.probe = snd_soc_codec_drv_probe;
3300 if (codec_drv->remove)
3301 codec->component.remove = snd_soc_codec_drv_remove;
3302 if (codec_drv->write)
3303 codec->component.write = snd_soc_codec_drv_write;
3304 if (codec_drv->read)
3305 codec->component.read = snd_soc_codec_drv_read;
3306 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
3307 codec->dapm.idle_bias_off = codec_drv->idle_bias_off;
3308 codec->dapm.suspend_bias_off = codec_drv->suspend_bias_off;
3309 if (codec_drv->seq_notifier)
3310 codec->dapm.seq_notifier = codec_drv->seq_notifier;
3311 if (codec_drv->set_bias_level)
3312 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
3314 codec->driver = codec_drv;
3315 codec->component.val_bytes = codec_drv->reg_word_size;
3316 mutex_init(&codec->mutex);
3318 #ifdef CONFIG_DEBUG_FS
3319 codec->component.init_debugfs = soc_init_codec_debugfs;
3320 codec->component.debugfs_prefix = "codec";
3323 if (codec_drv->get_regmap)
3324 codec->component.regmap = codec_drv->get_regmap(dev);
3326 for (i = 0; i < num_dai; i++) {
3327 fixup_codec_formats(&dai_drv[i].playback);
3328 fixup_codec_formats(&dai_drv[i].capture);
3331 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3333 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
3337 list_for_each_entry(dai, &codec->component.dai_list, list)
3340 mutex_lock(&client_mutex);
3341 snd_soc_component_add_unlocked(&codec->component);
3342 list_add(&codec->list, &codec_list);
3343 mutex_unlock(&client_mutex);
3345 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3346 codec->component.name);
3350 snd_soc_component_cleanup(&codec->component);
3355 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3358 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3360 * @codec: codec to unregister
3362 void snd_soc_unregister_codec(struct device *dev)
3364 struct snd_soc_codec *codec;
3366 list_for_each_entry(codec, &codec_list, list) {
3367 if (dev == codec->dev)
3374 mutex_lock(&client_mutex);
3375 list_del(&codec->list);
3376 snd_soc_component_del_unlocked(&codec->component);
3377 mutex_unlock(&client_mutex);
3379 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3380 codec->component.name);
3382 snd_soc_component_cleanup(&codec->component);
3383 snd_soc_cache_exit(codec);
3386 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3388 /* Retrieve a card's name from device tree */
3389 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3390 const char *propname)
3392 struct device_node *np;
3396 pr_err("card->dev is not set before calling %s\n", __func__);
3400 np = card->dev->of_node;
3402 ret = of_property_read_string_index(np, propname, 0, &card->name);
3404 * EINVAL means the property does not exist. This is fine providing
3405 * card->name was previously set, which is checked later in
3406 * snd_soc_register_card.
3408 if (ret < 0 && ret != -EINVAL) {
3410 "ASoC: Property '%s' could not be read: %d\n",
3417 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3419 static const struct snd_soc_dapm_widget simple_widgets[] = {
3420 SND_SOC_DAPM_MIC("Microphone", NULL),
3421 SND_SOC_DAPM_LINE("Line", NULL),
3422 SND_SOC_DAPM_HP("Headphone", NULL),
3423 SND_SOC_DAPM_SPK("Speaker", NULL),
3426 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
3427 const char *propname)
3429 struct device_node *np = card->dev->of_node;
3430 struct snd_soc_dapm_widget *widgets;
3431 const char *template, *wname;
3432 int i, j, num_widgets, ret;
3434 num_widgets = of_property_count_strings(np, propname);
3435 if (num_widgets < 0) {
3437 "ASoC: Property '%s' does not exist\n", propname);
3440 if (num_widgets & 1) {
3442 "ASoC: Property '%s' length is not even\n", propname);
3448 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3453 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3457 "ASoC: Could not allocate memory for widgets\n");
3461 for (i = 0; i < num_widgets; i++) {
3462 ret = of_property_read_string_index(np, propname,
3466 "ASoC: Property '%s' index %d read error:%d\n",
3467 propname, 2 * i, ret);
3471 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3472 if (!strncmp(template, simple_widgets[j].name,
3473 strlen(simple_widgets[j].name))) {
3474 widgets[i] = simple_widgets[j];
3479 if (j >= ARRAY_SIZE(simple_widgets)) {
3481 "ASoC: DAPM widget '%s' is not supported\n",
3486 ret = of_property_read_string_index(np, propname,
3491 "ASoC: Property '%s' index %d read error:%d\n",
3492 propname, (2 * i) + 1, ret);
3496 widgets[i].name = wname;
3499 card->dapm_widgets = widgets;
3500 card->num_dapm_widgets = num_widgets;
3504 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
3506 int snd_soc_of_parse_tdm_slot(struct device_node *np,
3507 unsigned int *slots,
3508 unsigned int *slot_width)
3513 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3514 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3522 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3523 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3533 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3535 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3536 const char *propname)
3538 struct device_node *np = card->dev->of_node;
3539 int num_routes, old_routes;
3540 struct snd_soc_dapm_route *routes;
3543 num_routes = of_property_count_strings(np, propname);
3544 if (num_routes < 0 || num_routes & 1) {
3546 "ASoC: Property '%s' does not exist or its length is not even\n",
3552 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3557 old_routes = card->num_dapm_routes;
3558 routes = devm_kzalloc(card->dev,
3559 (old_routes + num_routes) * sizeof(*routes),
3563 "ASoC: Could not allocate DAPM route table\n");
3567 memcpy(routes, card->dapm_routes, old_routes * sizeof(*routes));
3569 for (i = 0; i < num_routes; i++) {
3570 ret = of_property_read_string_index(np, propname,
3571 2 * i, &routes[old_routes + i].sink);
3574 "ASoC: Property '%s' index %d could not be read: %d\n",
3575 propname, 2 * i, ret);
3578 ret = of_property_read_string_index(np, propname,
3579 (2 * i) + 1, &routes[old_routes + i].source);
3582 "ASoC: Property '%s' index %d could not be read: %d\n",
3583 propname, (2 * i) + 1, ret);
3588 card->num_dapm_routes += num_routes;
3589 card->dapm_routes = routes;
3593 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3595 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3597 struct device_node **bitclkmaster,
3598 struct device_node **framemaster)
3602 unsigned int format = 0;
3608 } of_fmt_table[] = {
3609 { "i2s", SND_SOC_DAIFMT_I2S },
3610 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3611 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3612 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3613 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3614 { "ac97", SND_SOC_DAIFMT_AC97 },
3615 { "pdm", SND_SOC_DAIFMT_PDM},
3616 { "msb", SND_SOC_DAIFMT_MSB },
3617 { "lsb", SND_SOC_DAIFMT_LSB },
3624 * check "[prefix]format = xxx"
3625 * SND_SOC_DAIFMT_FORMAT_MASK area
3627 snprintf(prop, sizeof(prop), "%sformat", prefix);
3628 ret = of_property_read_string(np, prop, &str);
3630 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3631 if (strcmp(str, of_fmt_table[i].name) == 0) {
3632 format |= of_fmt_table[i].val;
3639 * check "[prefix]continuous-clock"
3640 * SND_SOC_DAIFMT_CLOCK_MASK area
3642 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
3643 if (of_get_property(np, prop, NULL))
3644 format |= SND_SOC_DAIFMT_CONT;
3646 format |= SND_SOC_DAIFMT_GATED;
3649 * check "[prefix]bitclock-inversion"
3650 * check "[prefix]frame-inversion"
3651 * SND_SOC_DAIFMT_INV_MASK area
3653 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3654 bit = !!of_get_property(np, prop, NULL);
3656 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3657 frame = !!of_get_property(np, prop, NULL);
3659 switch ((bit << 4) + frame) {
3661 format |= SND_SOC_DAIFMT_IB_IF;
3664 format |= SND_SOC_DAIFMT_IB_NF;
3667 format |= SND_SOC_DAIFMT_NB_IF;
3670 /* SND_SOC_DAIFMT_NB_NF is default */
3675 * check "[prefix]bitclock-master"
3676 * check "[prefix]frame-master"
3677 * SND_SOC_DAIFMT_MASTER_MASK area
3679 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3680 bit = !!of_get_property(np, prop, NULL);
3681 if (bit && bitclkmaster)
3682 *bitclkmaster = of_parse_phandle(np, prop, 0);
3684 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3685 frame = !!of_get_property(np, prop, NULL);
3686 if (frame && framemaster)
3687 *framemaster = of_parse_phandle(np, prop, 0);
3689 switch ((bit << 4) + frame) {
3691 format |= SND_SOC_DAIFMT_CBM_CFM;
3694 format |= SND_SOC_DAIFMT_CBM_CFS;
3697 format |= SND_SOC_DAIFMT_CBS_CFM;
3700 format |= SND_SOC_DAIFMT_CBS_CFS;
3706 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3708 int snd_soc_of_get_dai_name(struct device_node *of_node,
3709 const char **dai_name)
3711 struct snd_soc_component *pos;
3712 struct of_phandle_args args;
3715 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3716 "#sound-dai-cells", 0, &args);
3720 ret = -EPROBE_DEFER;
3722 mutex_lock(&client_mutex);
3723 list_for_each_entry(pos, &component_list, list) {
3724 if (pos->dev->of_node != args.np)
3727 if (pos->driver->of_xlate_dai_name) {
3728 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
3732 switch (args.args_count) {
3734 id = 0; /* same as dai_drv[0] */
3744 if (id < 0 || id >= pos->num_dai) {
3751 *dai_name = pos->dai_drv[id].name;
3753 *dai_name = pos->name;
3758 mutex_unlock(&client_mutex);
3760 of_node_put(args.np);
3764 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3766 static int __init snd_soc_init(void)
3768 #ifdef CONFIG_DEBUG_FS
3769 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3770 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
3771 pr_warn("ASoC: Failed to create debugfs directory\n");
3772 snd_soc_debugfs_root = NULL;
3775 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
3777 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3779 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
3781 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
3783 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
3784 &platform_list_fops))
3785 pr_warn("ASoC: Failed to create platform list debugfs file\n");
3788 snd_soc_util_init();
3790 return platform_driver_register(&soc_driver);
3792 module_init(snd_soc_init);
3794 static void __exit snd_soc_exit(void)
3796 snd_soc_util_exit();
3798 #ifdef CONFIG_DEBUG_FS
3799 debugfs_remove_recursive(snd_soc_debugfs_root);
3801 platform_driver_unregister(&soc_driver);
3803 module_exit(snd_soc_exit);
3805 /* Module information */
3806 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3807 MODULE_DESCRIPTION("ALSA SoC Core");
3808 MODULE_LICENSE("GPL");
3809 MODULE_ALIAS("platform:soc-audio");