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 debugfs_create_bool("cache_sync", 0444, codec->component.debugfs_root,
315 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
316 codec->component.debugfs_root,
317 codec, &codec_reg_fops);
318 if (!codec->debugfs_reg)
320 "ASoC: Failed to create codec register debugfs file\n");
323 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
324 size_t count, loff_t *ppos)
326 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
327 ssize_t len, ret = 0;
328 struct snd_soc_codec *codec;
333 list_for_each_entry(codec, &codec_list, list) {
334 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
335 codec->component.name);
338 if (ret > PAGE_SIZE) {
345 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
352 static const struct file_operations codec_list_fops = {
353 .read = codec_list_read_file,
354 .llseek = default_llseek,/* read accesses f_pos */
357 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
358 size_t count, loff_t *ppos)
360 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
361 ssize_t len, ret = 0;
362 struct snd_soc_component *component;
363 struct snd_soc_dai *dai;
368 list_for_each_entry(component, &component_list, list) {
369 list_for_each_entry(dai, &component->dai_list, list) {
370 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
374 if (ret > PAGE_SIZE) {
381 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
388 static const struct file_operations dai_list_fops = {
389 .read = dai_list_read_file,
390 .llseek = default_llseek,/* read accesses f_pos */
393 static ssize_t platform_list_read_file(struct file *file,
394 char __user *user_buf,
395 size_t count, loff_t *ppos)
397 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
398 ssize_t len, ret = 0;
399 struct snd_soc_platform *platform;
404 list_for_each_entry(platform, &platform_list, list) {
405 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
406 platform->component.name);
409 if (ret > PAGE_SIZE) {
415 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
422 static const struct file_operations platform_list_fops = {
423 .read = platform_list_read_file,
424 .llseek = default_llseek,/* read accesses f_pos */
427 static void soc_init_card_debugfs(struct snd_soc_card *card)
429 card->debugfs_card_root = debugfs_create_dir(card->name,
430 snd_soc_debugfs_root);
431 if (!card->debugfs_card_root) {
433 "ASoC: Failed to create card debugfs directory\n");
437 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
438 card->debugfs_card_root,
440 if (!card->debugfs_pop_time)
442 "ASoC: Failed to create pop time debugfs file\n");
445 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
447 debugfs_remove_recursive(card->debugfs_card_root);
452 #define soc_init_codec_debugfs NULL
454 static inline void soc_init_component_debugfs(
455 struct snd_soc_component *component)
459 static inline void soc_cleanup_component_debugfs(
460 struct snd_soc_component *component)
464 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
468 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
473 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
474 const char *dai_link, int stream)
478 for (i = 0; i < card->num_links; i++) {
479 if (card->rtd[i].dai_link->no_pcm &&
480 !strcmp(card->rtd[i].dai_link->name, dai_link))
481 return card->rtd[i].pcm->streams[stream].substream;
483 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
486 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
488 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
489 const char *dai_link)
493 for (i = 0; i < card->num_links; i++) {
494 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
495 return &card->rtd[i];
497 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
500 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
502 #ifdef CONFIG_SND_SOC_AC97_BUS
503 /* unregister ac97 codec */
504 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
506 if (codec->ac97->dev.bus)
507 device_unregister(&codec->ac97->dev);
511 /* stop no dev release warning */
512 static void soc_ac97_device_release(struct device *dev){}
514 /* register ac97 codec to bus */
515 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
519 codec->ac97->dev.bus = &ac97_bus_type;
520 codec->ac97->dev.parent = codec->component.card->dev;
521 codec->ac97->dev.release = soc_ac97_device_release;
523 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
524 codec->component.card->snd_card->number, 0,
525 codec->component.name);
526 err = device_register(&codec->ac97->dev);
528 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
529 codec->ac97->dev.bus = NULL;
536 static void codec2codec_close_delayed_work(struct work_struct *work)
538 /* Currently nothing to do for c2c links
539 * Since c2c links are internal nodes in the DAPM graph and
540 * don't interface with the outside world or application layer
541 * we don't have to do any special handling on close.
545 #ifdef CONFIG_PM_SLEEP
546 /* powers down audio subsystem for suspend */
547 int snd_soc_suspend(struct device *dev)
549 struct snd_soc_card *card = dev_get_drvdata(dev);
550 struct snd_soc_codec *codec;
553 /* If the card is not initialized yet there is nothing to do */
554 if (!card->instantiated)
557 /* Due to the resume being scheduled into a workqueue we could
558 * suspend before that's finished - wait for it to complete.
560 snd_power_lock(card->snd_card);
561 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
562 snd_power_unlock(card->snd_card);
564 /* we're going to block userspace touching us until resume completes */
565 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
567 /* mute any active DACs */
568 for (i = 0; i < card->num_rtd; i++) {
570 if (card->rtd[i].dai_link->ignore_suspend)
573 for (j = 0; j < card->rtd[i].num_codecs; j++) {
574 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
575 struct snd_soc_dai_driver *drv = dai->driver;
577 if (drv->ops->digital_mute && dai->playback_active)
578 drv->ops->digital_mute(dai, 1);
582 /* suspend all pcms */
583 for (i = 0; i < card->num_rtd; i++) {
584 if (card->rtd[i].dai_link->ignore_suspend)
587 snd_pcm_suspend_all(card->rtd[i].pcm);
590 if (card->suspend_pre)
591 card->suspend_pre(card);
593 for (i = 0; i < card->num_rtd; i++) {
594 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
596 if (card->rtd[i].dai_link->ignore_suspend)
599 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
600 cpu_dai->driver->suspend(cpu_dai);
603 /* close any waiting streams and save state */
604 for (i = 0; i < card->num_rtd; i++) {
605 struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais;
606 flush_delayed_work(&card->rtd[i].delayed_work);
607 for (j = 0; j < card->rtd[i].num_codecs; j++) {
608 codec_dais[j]->codec->dapm.suspend_bias_level =
609 codec_dais[j]->codec->dapm.bias_level;
613 for (i = 0; i < card->num_rtd; i++) {
615 if (card->rtd[i].dai_link->ignore_suspend)
618 snd_soc_dapm_stream_event(&card->rtd[i],
619 SNDRV_PCM_STREAM_PLAYBACK,
620 SND_SOC_DAPM_STREAM_SUSPEND);
622 snd_soc_dapm_stream_event(&card->rtd[i],
623 SNDRV_PCM_STREAM_CAPTURE,
624 SND_SOC_DAPM_STREAM_SUSPEND);
627 /* Recheck all endpoints too, their state is affected by suspend */
628 dapm_mark_endpoints_dirty(card);
629 snd_soc_dapm_sync(&card->dapm);
631 /* suspend all CODECs */
632 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
633 /* If there are paths active then the CODEC will be held with
634 * bias _ON and should not be suspended. */
635 if (!codec->suspended) {
636 switch (codec->dapm.bias_level) {
637 case SND_SOC_BIAS_STANDBY:
639 * If the CODEC is capable of idle
640 * bias off then being in STANDBY
641 * means it's doing something,
642 * otherwise fall through.
644 if (codec->dapm.idle_bias_off) {
646 "ASoC: idle_bias_off CODEC on over suspend\n");
650 case SND_SOC_BIAS_OFF:
651 if (codec->driver->suspend)
652 codec->driver->suspend(codec);
653 codec->suspended = 1;
654 codec->cache_sync = 1;
655 if (codec->component.regmap)
656 regcache_mark_dirty(codec->component.regmap);
657 /* deactivate pins to sleep state */
658 pinctrl_pm_select_sleep_state(codec->dev);
662 "ASoC: CODEC is on over suspend\n");
668 for (i = 0; i < card->num_rtd; i++) {
669 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
671 if (card->rtd[i].dai_link->ignore_suspend)
674 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
675 cpu_dai->driver->suspend(cpu_dai);
677 /* deactivate pins to sleep state */
678 pinctrl_pm_select_sleep_state(cpu_dai->dev);
681 if (card->suspend_post)
682 card->suspend_post(card);
686 EXPORT_SYMBOL_GPL(snd_soc_suspend);
688 /* deferred resume work, so resume can complete before we finished
689 * setting our codec back up, which can be very slow on I2C
691 static void soc_resume_deferred(struct work_struct *work)
693 struct snd_soc_card *card =
694 container_of(work, struct snd_soc_card, deferred_resume_work);
695 struct snd_soc_codec *codec;
698 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
699 * so userspace apps are blocked from touching us
702 dev_dbg(card->dev, "ASoC: starting resume work\n");
704 /* Bring us up into D2 so that DAPM starts enabling things */
705 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
707 if (card->resume_pre)
708 card->resume_pre(card);
710 /* resume AC97 DAIs */
711 for (i = 0; i < card->num_rtd; i++) {
712 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
714 if (card->rtd[i].dai_link->ignore_suspend)
717 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
718 cpu_dai->driver->resume(cpu_dai);
721 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
722 /* If the CODEC was idle over suspend then it will have been
723 * left with bias OFF or STANDBY and suspended so we must now
724 * resume. Otherwise the suspend was suppressed.
726 if (codec->suspended) {
727 switch (codec->dapm.bias_level) {
728 case SND_SOC_BIAS_STANDBY:
729 case SND_SOC_BIAS_OFF:
730 if (codec->driver->resume)
731 codec->driver->resume(codec);
732 codec->suspended = 0;
736 "ASoC: CODEC was on over suspend\n");
742 for (i = 0; i < card->num_rtd; i++) {
744 if (card->rtd[i].dai_link->ignore_suspend)
747 snd_soc_dapm_stream_event(&card->rtd[i],
748 SNDRV_PCM_STREAM_PLAYBACK,
749 SND_SOC_DAPM_STREAM_RESUME);
751 snd_soc_dapm_stream_event(&card->rtd[i],
752 SNDRV_PCM_STREAM_CAPTURE,
753 SND_SOC_DAPM_STREAM_RESUME);
756 /* unmute any active DACs */
757 for (i = 0; i < card->num_rtd; i++) {
759 if (card->rtd[i].dai_link->ignore_suspend)
762 for (j = 0; j < card->rtd[i].num_codecs; j++) {
763 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
764 struct snd_soc_dai_driver *drv = dai->driver;
766 if (drv->ops->digital_mute && dai->playback_active)
767 drv->ops->digital_mute(dai, 0);
771 for (i = 0; i < card->num_rtd; i++) {
772 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
774 if (card->rtd[i].dai_link->ignore_suspend)
777 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
778 cpu_dai->driver->resume(cpu_dai);
781 if (card->resume_post)
782 card->resume_post(card);
784 dev_dbg(card->dev, "ASoC: resume work completed\n");
786 /* userspace can access us now we are back as we were before */
787 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
789 /* Recheck all endpoints too, their state is affected by suspend */
790 dapm_mark_endpoints_dirty(card);
791 snd_soc_dapm_sync(&card->dapm);
794 /* powers up audio subsystem after a suspend */
795 int snd_soc_resume(struct device *dev)
797 struct snd_soc_card *card = dev_get_drvdata(dev);
798 int i, ac97_control = 0;
800 /* If the card is not initialized yet there is nothing to do */
801 if (!card->instantiated)
804 /* activate pins from sleep state */
805 for (i = 0; i < card->num_rtd; i++) {
806 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
807 struct snd_soc_dai **codec_dais = rtd->codec_dais;
808 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
812 pinctrl_pm_select_default_state(cpu_dai->dev);
814 for (j = 0; j < rtd->num_codecs; j++) {
815 struct snd_soc_dai *codec_dai = codec_dais[j];
816 if (codec_dai->active)
817 pinctrl_pm_select_default_state(codec_dai->dev);
821 /* AC97 devices might have other drivers hanging off them so
822 * need to resume immediately. Other drivers don't have that
823 * problem and may take a substantial amount of time to resume
824 * due to I/O costs and anti-pop so handle them out of line.
826 for (i = 0; i < card->num_rtd; i++) {
827 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
828 ac97_control |= cpu_dai->driver->ac97_control;
831 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
832 soc_resume_deferred(&card->deferred_resume_work);
834 dev_dbg(dev, "ASoC: Scheduling resume work\n");
835 if (!schedule_work(&card->deferred_resume_work))
836 dev_err(dev, "ASoC: resume work item may be lost\n");
841 EXPORT_SYMBOL_GPL(snd_soc_resume);
843 #define snd_soc_suspend NULL
844 #define snd_soc_resume NULL
847 static const struct snd_soc_dai_ops null_dai_ops = {
850 static struct snd_soc_component *soc_find_component(
851 const struct device_node *of_node, const char *name)
853 struct snd_soc_component *component;
855 list_for_each_entry(component, &component_list, list) {
857 if (component->dev->of_node == of_node)
859 } else if (strcmp(component->name, name) == 0) {
867 static struct snd_soc_dai *snd_soc_find_dai(
868 const struct snd_soc_dai_link_component *dlc)
870 struct snd_soc_component *component;
871 struct snd_soc_dai *dai;
873 /* Find CPU DAI from registered DAIs*/
874 list_for_each_entry(component, &component_list, list) {
875 if (dlc->of_node && component->dev->of_node != dlc->of_node)
877 if (dlc->name && strcmp(dev_name(component->dev), dlc->name))
879 list_for_each_entry(dai, &component->dai_list, list) {
880 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
890 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
892 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
893 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
894 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
895 struct snd_soc_dai_link_component cpu_dai_component;
896 struct snd_soc_dai **codec_dais = rtd->codec_dais;
897 struct snd_soc_platform *platform;
898 const char *platform_name;
901 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
903 cpu_dai_component.name = dai_link->cpu_name;
904 cpu_dai_component.of_node = dai_link->cpu_of_node;
905 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
906 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
908 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
909 dai_link->cpu_dai_name);
910 return -EPROBE_DEFER;
913 rtd->num_codecs = dai_link->num_codecs;
915 /* Find CODEC from registered CODECs */
916 for (i = 0; i < rtd->num_codecs; i++) {
917 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
918 if (!codec_dais[i]) {
919 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
921 return -EPROBE_DEFER;
925 /* Single codec links expect codec and codec_dai in runtime data */
926 rtd->codec_dai = codec_dais[0];
927 rtd->codec = rtd->codec_dai->codec;
929 /* if there's no platform we match on the empty platform */
930 platform_name = dai_link->platform_name;
931 if (!platform_name && !dai_link->platform_of_node)
932 platform_name = "snd-soc-dummy";
934 /* find one from the set of registered platforms */
935 list_for_each_entry(platform, &platform_list, list) {
936 if (dai_link->platform_of_node) {
937 if (platform->dev->of_node !=
938 dai_link->platform_of_node)
941 if (strcmp(platform->component.name, platform_name))
945 rtd->platform = platform;
947 if (!rtd->platform) {
948 dev_err(card->dev, "ASoC: platform %s not registered\n",
949 dai_link->platform_name);
950 return -EPROBE_DEFER;
958 static void soc_remove_component(struct snd_soc_component *component)
960 if (!component->probed)
963 /* This is a HACK and will be removed soon */
964 if (component->codec)
965 list_del(&component->codec->card_list);
967 if (component->remove)
968 component->remove(component);
970 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
972 soc_cleanup_component_debugfs(component);
973 component->probed = 0;
974 module_put(component->dev->driver->owner);
977 static void soc_remove_dai(struct snd_soc_dai *dai, int order)
981 if (dai && dai->probed &&
982 dai->driver->remove_order == order) {
983 if (dai->driver->remove) {
984 err = dai->driver->remove(dai);
987 "ASoC: failed to remove %s: %d\n",
994 static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
996 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
999 /* unregister the rtd device */
1000 if (rtd->dev_registered) {
1001 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1002 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1003 device_unregister(rtd->dev);
1004 rtd->dev_registered = 0;
1007 /* remove the CODEC DAI */
1008 for (i = 0; i < rtd->num_codecs; i++)
1009 soc_remove_dai(rtd->codec_dais[i], order);
1011 soc_remove_dai(rtd->cpu_dai, order);
1014 static void soc_remove_link_components(struct snd_soc_card *card, int num,
1017 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1018 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1019 struct snd_soc_platform *platform = rtd->platform;
1020 struct snd_soc_component *component;
1023 /* remove the platform */
1024 if (platform && platform->component.driver->remove_order == order)
1025 soc_remove_component(&platform->component);
1027 /* remove the CODEC-side CODEC */
1028 for (i = 0; i < rtd->num_codecs; i++) {
1029 component = rtd->codec_dais[i]->component;
1030 if (component->driver->remove_order == order)
1031 soc_remove_component(component);
1034 /* remove any CPU-side CODEC */
1036 if (cpu_dai->component->driver->remove_order == order)
1037 soc_remove_component(cpu_dai->component);
1041 static void soc_remove_dai_links(struct snd_soc_card *card)
1045 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1047 for (dai = 0; dai < card->num_rtd; dai++)
1048 soc_remove_link_dais(card, dai, order);
1051 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1053 for (dai = 0; dai < card->num_rtd; dai++)
1054 soc_remove_link_components(card, dai, order);
1060 static void soc_set_name_prefix(struct snd_soc_card *card,
1061 struct snd_soc_component *component)
1065 if (card->codec_conf == NULL)
1068 for (i = 0; i < card->num_configs; i++) {
1069 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1070 if (map->of_node && component->dev->of_node != map->of_node)
1072 if (map->dev_name && strcmp(component->name, map->dev_name))
1074 component->name_prefix = map->name_prefix;
1079 static int soc_probe_component(struct snd_soc_card *card,
1080 struct snd_soc_component *component)
1082 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1083 struct snd_soc_dai *dai;
1086 if (component->probed)
1089 component->card = card;
1091 soc_set_name_prefix(card, component);
1093 if (!try_module_get(component->dev->driver->owner))
1096 soc_init_component_debugfs(component);
1098 if (component->dapm_widgets) {
1099 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1100 component->num_dapm_widgets);
1103 dev_err(component->dev,
1104 "Failed to create new controls %d\n", ret);
1109 list_for_each_entry(dai, &component->dai_list, list) {
1110 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1112 dev_err(component->dev,
1113 "Failed to create DAI widgets %d\n", ret);
1118 if (component->probe) {
1119 ret = component->probe(component);
1121 dev_err(component->dev,
1122 "ASoC: failed to probe component %d\n", ret);
1126 WARN(dapm->idle_bias_off &&
1127 dapm->bias_level != SND_SOC_BIAS_OFF,
1128 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1132 if (component->controls)
1133 snd_soc_add_component_controls(component, component->controls,
1134 component->num_controls);
1135 if (component->dapm_routes)
1136 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1137 component->num_dapm_routes);
1139 component->probed = 1;
1140 list_add(&dapm->list, &card->dapm_list);
1142 /* This is a HACK and will be removed soon */
1143 if (component->codec)
1144 list_add(&component->codec->card_list, &card->codec_dev_list);
1149 soc_cleanup_component_debugfs(component);
1150 module_put(component->dev->driver->owner);
1155 static void rtd_release(struct device *dev)
1160 static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1165 /* register the rtd device */
1166 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1169 device_initialize(rtd->dev);
1170 rtd->dev->parent = rtd->card->dev;
1171 rtd->dev->release = rtd_release;
1172 dev_set_name(rtd->dev, "%s", name);
1173 dev_set_drvdata(rtd->dev, rtd);
1174 mutex_init(&rtd->pcm_mutex);
1175 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1176 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1177 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1178 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1179 ret = device_add(rtd->dev);
1181 /* calling put_device() here to free the rtd->dev */
1182 put_device(rtd->dev);
1183 dev_err(rtd->card->dev,
1184 "ASoC: failed to register runtime device: %d\n", ret);
1187 rtd->dev_registered = 1;
1190 /* add DAPM sysfs entries for this codec */
1191 ret = snd_soc_dapm_sys_add(rtd->dev);
1194 "ASoC: failed to add codec dapm sysfs entries: %d\n",
1197 /* add codec sysfs entries */
1198 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1201 "ASoC: failed to add codec sysfs files: %d\n",
1208 static int soc_probe_link_components(struct snd_soc_card *card, int num,
1211 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1212 struct snd_soc_platform *platform = rtd->platform;
1213 struct snd_soc_component *component;
1216 /* probe the CPU-side component, if it is a CODEC */
1217 component = rtd->cpu_dai->component;
1218 if (component->driver->probe_order == order) {
1219 ret = soc_probe_component(card, component);
1224 /* probe the CODEC-side components */
1225 for (i = 0; i < rtd->num_codecs; i++) {
1226 component = rtd->codec_dais[i]->component;
1227 if (component->driver->probe_order == order) {
1228 ret = soc_probe_component(card, component);
1234 /* probe the platform */
1235 if (platform->component.driver->probe_order == order) {
1236 ret = soc_probe_component(card, &platform->component);
1244 static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1248 if (!dai->probed && dai->driver->probe_order == order) {
1249 if (dai->driver->probe) {
1250 ret = dai->driver->probe(dai);
1253 "ASoC: failed to probe DAI %s: %d\n",
1265 static int soc_link_dai_widgets(struct snd_soc_card *card,
1266 struct snd_soc_dai_link *dai_link,
1267 struct snd_soc_pcm_runtime *rtd)
1269 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1270 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1271 struct snd_soc_dapm_widget *play_w, *capture_w;
1274 if (rtd->num_codecs > 1)
1275 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1277 /* link the DAI widgets */
1278 play_w = codec_dai->playback_widget;
1279 capture_w = cpu_dai->capture_widget;
1280 if (play_w && capture_w) {
1281 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1284 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1285 play_w->name, capture_w->name, ret);
1290 play_w = cpu_dai->playback_widget;
1291 capture_w = codec_dai->capture_widget;
1292 if (play_w && capture_w) {
1293 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1296 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1297 play_w->name, capture_w->name, ret);
1305 static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1307 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1308 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1309 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1312 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1313 card->name, num, order);
1315 /* set default power off timeout */
1316 rtd->pmdown_time = pmdown_time;
1318 ret = soc_probe_dai(cpu_dai, order);
1322 /* probe the CODEC DAI */
1323 for (i = 0; i < rtd->num_codecs; i++) {
1324 ret = soc_probe_dai(rtd->codec_dais[i], order);
1329 /* complete DAI probe during last probe */
1330 if (order != SND_SOC_COMP_ORDER_LAST)
1333 /* do machine specific initialization */
1334 if (dai_link->init) {
1335 ret = dai_link->init(rtd);
1337 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1338 dai_link->name, ret);
1343 ret = soc_post_component_init(rtd, dai_link->name);
1347 #ifdef CONFIG_DEBUG_FS
1348 /* add DPCM sysfs entries */
1349 if (dai_link->dynamic) {
1350 ret = soc_dpcm_debugfs_add(rtd);
1353 "ASoC: failed to add dpcm sysfs entries: %d\n",
1360 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1362 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1365 if (cpu_dai->driver->compress_dai) {
1366 /*create compress_device"*/
1367 ret = soc_new_compress(rtd, num);
1369 dev_err(card->dev, "ASoC: can't create compress %s\n",
1370 dai_link->stream_name);
1375 if (!dai_link->params) {
1376 /* create the pcm */
1377 ret = soc_new_pcm(rtd, num);
1379 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1380 dai_link->stream_name, ret);
1384 INIT_DELAYED_WORK(&rtd->delayed_work,
1385 codec2codec_close_delayed_work);
1387 /* link the DAI widgets */
1388 ret = soc_link_dai_widgets(card, dai_link, rtd);
1394 /* add platform data for AC97 devices */
1395 for (i = 0; i < rtd->num_codecs; i++) {
1396 if (rtd->codec_dais[i]->driver->ac97_control)
1397 snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
1398 rtd->cpu_dai->ac97_pdata);
1404 #ifdef CONFIG_SND_SOC_AC97_BUS
1405 static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1406 struct snd_soc_dai *codec_dai)
1410 /* Only instantiate AC97 if not already done by the adaptor
1411 * for the generic AC97 subsystem.
1413 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
1415 * It is possible that the AC97 device is already registered to
1416 * the device subsystem. This happens when the device is created
1417 * via snd_ac97_mixer(). Currently only SoC codec that does so
1418 * is the generic AC97 glue but others migh emerge.
1420 * In those cases we don't try to register the device again.
1422 if (!codec->ac97_created)
1425 ret = soc_ac97_dev_register(codec);
1428 "ASoC: AC97 device register failed: %d\n", ret);
1432 codec->ac97_registered = 1;
1437 static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
1439 if (codec->ac97_registered) {
1440 soc_ac97_dev_unregister(codec);
1441 codec->ac97_registered = 0;
1445 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1449 for (i = 0; i < rtd->num_codecs; i++) {
1450 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1452 ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
1455 soc_unregister_ac97_codec(codec_dai->codec);
1463 static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1467 for (i = 0; i < rtd->num_codecs; i++)
1468 soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
1472 static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1474 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1475 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1476 const char *name = aux_dev->codec_name;
1478 rtd->component = soc_find_component(aux_dev->codec_of_node, name);
1479 if (!rtd->component) {
1480 if (aux_dev->codec_of_node)
1481 name = of_node_full_name(aux_dev->codec_of_node);
1483 dev_err(card->dev, "ASoC: %s not registered\n", name);
1484 return -EPROBE_DEFER;
1488 * Some places still reference rtd->codec, so we have to keep that
1489 * initialized if the component is a CODEC. Once all those references
1490 * have been removed, this code can be removed as well.
1492 rtd->codec = rtd->component->codec;
1497 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1499 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1500 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1503 ret = soc_probe_component(card, rtd->component);
1507 /* do machine specific initialization */
1508 if (aux_dev->init) {
1509 ret = aux_dev->init(rtd->component);
1511 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1512 aux_dev->name, ret);
1517 return soc_post_component_init(rtd, aux_dev->name);
1520 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1522 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1523 struct snd_soc_component *component = rtd->component;
1525 /* unregister the rtd device */
1526 if (rtd->dev_registered) {
1527 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1528 device_unregister(rtd->dev);
1529 rtd->dev_registered = 0;
1532 if (component && component->probed)
1533 soc_remove_component(component);
1536 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1540 if (codec->cache_init)
1543 ret = snd_soc_cache_init(codec);
1546 "ASoC: Failed to set cache compression type: %d\n",
1550 codec->cache_init = 1;
1554 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1556 struct snd_soc_codec *codec;
1557 struct snd_soc_dai_link *dai_link;
1558 int ret, i, order, dai_fmt;
1560 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1563 for (i = 0; i < card->num_links; i++) {
1564 ret = soc_bind_dai_link(card, i);
1569 /* bind aux_devs too */
1570 for (i = 0; i < card->num_aux_devs; i++) {
1571 ret = soc_bind_aux_dev(card, i);
1576 /* initialize the register cache for each available codec */
1577 list_for_each_entry(codec, &codec_list, list) {
1578 if (codec->cache_init)
1580 ret = snd_soc_init_codec_cache(codec);
1585 /* card bind complete so register a sound card */
1586 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1587 card->owner, 0, &card->snd_card);
1590 "ASoC: can't create sound card for card %s: %d\n",
1595 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1596 card->dapm.dev = card->dev;
1597 card->dapm.card = card;
1598 list_add(&card->dapm.list, &card->dapm_list);
1600 #ifdef CONFIG_DEBUG_FS
1601 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1604 #ifdef CONFIG_PM_SLEEP
1605 /* deferred resume work */
1606 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1609 if (card->dapm_widgets)
1610 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1611 card->num_dapm_widgets);
1613 /* initialise the sound card only once */
1615 ret = card->probe(card);
1617 goto card_probe_error;
1620 /* probe all components used by DAI links on this card */
1621 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1623 for (i = 0; i < card->num_links; i++) {
1624 ret = soc_probe_link_components(card, i, order);
1627 "ASoC: failed to instantiate card %d\n",
1634 /* probe all DAI links on this card */
1635 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1637 for (i = 0; i < card->num_links; i++) {
1638 ret = soc_probe_link_dais(card, i, order);
1641 "ASoC: failed to instantiate card %d\n",
1648 for (i = 0; i < card->num_aux_devs; i++) {
1649 ret = soc_probe_aux_dev(card, i);
1652 "ASoC: failed to add auxiliary devices %d\n",
1654 goto probe_aux_dev_err;
1658 snd_soc_dapm_link_dai_widgets(card);
1659 snd_soc_dapm_connect_dai_link_widgets(card);
1662 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1664 if (card->dapm_routes)
1665 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1666 card->num_dapm_routes);
1668 for (i = 0; i < card->num_links; i++) {
1669 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1670 dai_link = &card->dai_link[i];
1671 dai_fmt = dai_link->dai_fmt;
1674 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1677 for (j = 0; j < rtd->num_codecs; j++) {
1678 struct snd_soc_dai *codec_dai = codec_dais[j];
1680 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1681 if (ret != 0 && ret != -ENOTSUPP)
1682 dev_warn(codec_dai->dev,
1683 "ASoC: Failed to set DAI format: %d\n",
1688 /* If this is a regular CPU link there will be a platform */
1690 (dai_link->platform_name || dai_link->platform_of_node)) {
1691 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1693 if (ret != 0 && ret != -ENOTSUPP)
1694 dev_warn(card->rtd[i].cpu_dai->dev,
1695 "ASoC: Failed to set DAI format: %d\n",
1697 } else if (dai_fmt) {
1698 /* Flip the polarity for the "CPU" end */
1699 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1700 switch (dai_link->dai_fmt &
1701 SND_SOC_DAIFMT_MASTER_MASK) {
1702 case SND_SOC_DAIFMT_CBM_CFM:
1703 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1705 case SND_SOC_DAIFMT_CBM_CFS:
1706 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1708 case SND_SOC_DAIFMT_CBS_CFM:
1709 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1711 case SND_SOC_DAIFMT_CBS_CFS:
1712 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1716 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1718 if (ret != 0 && ret != -ENOTSUPP)
1719 dev_warn(card->rtd[i].cpu_dai->dev,
1720 "ASoC: Failed to set DAI format: %d\n",
1725 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1727 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1728 "%s", card->long_name ? card->long_name : card->name);
1729 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1730 "%s", card->driver_name ? card->driver_name : card->name);
1731 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1732 switch (card->snd_card->driver[i]) {
1738 if (!isalnum(card->snd_card->driver[i]))
1739 card->snd_card->driver[i] = '_';
1744 if (card->late_probe) {
1745 ret = card->late_probe(card);
1747 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
1749 goto probe_aux_dev_err;
1753 if (card->fully_routed)
1754 snd_soc_dapm_auto_nc_pins(card);
1756 snd_soc_dapm_new_widgets(card);
1758 ret = snd_card_register(card->snd_card);
1760 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1762 goto probe_aux_dev_err;
1765 #ifdef CONFIG_SND_SOC_AC97_BUS
1766 /* register any AC97 codecs */
1767 for (i = 0; i < card->num_rtd; i++) {
1768 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1771 "ASoC: failed to register AC97: %d\n", ret);
1773 soc_unregister_ac97_dai_link(&card->rtd[i]);
1774 goto probe_aux_dev_err;
1779 card->instantiated = 1;
1780 snd_soc_dapm_sync(&card->dapm);
1781 mutex_unlock(&card->mutex);
1786 for (i = 0; i < card->num_aux_devs; i++)
1787 soc_remove_aux_dev(card, i);
1790 soc_remove_dai_links(card);
1796 snd_card_free(card->snd_card);
1799 mutex_unlock(&card->mutex);
1804 /* probes a new socdev */
1805 static int soc_probe(struct platform_device *pdev)
1807 struct snd_soc_card *card = platform_get_drvdata(pdev);
1810 * no card, so machine driver should be registering card
1811 * we should not be here in that case so ret error
1816 dev_warn(&pdev->dev,
1817 "ASoC: machine %s should use snd_soc_register_card()\n",
1820 /* Bodge while we unpick instantiation */
1821 card->dev = &pdev->dev;
1823 return snd_soc_register_card(card);
1826 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1830 /* make sure any delayed work runs */
1831 for (i = 0; i < card->num_rtd; i++) {
1832 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1833 flush_delayed_work(&rtd->delayed_work);
1836 /* remove auxiliary devices */
1837 for (i = 0; i < card->num_aux_devs; i++)
1838 soc_remove_aux_dev(card, i);
1840 /* remove and free each DAI */
1841 soc_remove_dai_links(card);
1843 soc_cleanup_card_debugfs(card);
1845 /* remove the card */
1849 snd_soc_dapm_free(&card->dapm);
1851 snd_card_free(card->snd_card);
1856 /* removes a socdev */
1857 static int soc_remove(struct platform_device *pdev)
1859 struct snd_soc_card *card = platform_get_drvdata(pdev);
1861 snd_soc_unregister_card(card);
1865 int snd_soc_poweroff(struct device *dev)
1867 struct snd_soc_card *card = dev_get_drvdata(dev);
1870 if (!card->instantiated)
1873 /* Flush out pmdown_time work - we actually do want to run it
1874 * now, we're shutting down so no imminent restart. */
1875 for (i = 0; i < card->num_rtd; i++) {
1876 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1877 flush_delayed_work(&rtd->delayed_work);
1880 snd_soc_dapm_shutdown(card);
1882 /* deactivate pins to sleep state */
1883 for (i = 0; i < card->num_rtd; i++) {
1884 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1885 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1888 pinctrl_pm_select_sleep_state(cpu_dai->dev);
1889 for (j = 0; j < rtd->num_codecs; j++) {
1890 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
1891 pinctrl_pm_select_sleep_state(codec_dai->dev);
1897 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1899 const struct dev_pm_ops snd_soc_pm_ops = {
1900 .suspend = snd_soc_suspend,
1901 .resume = snd_soc_resume,
1902 .freeze = snd_soc_suspend,
1903 .thaw = snd_soc_resume,
1904 .poweroff = snd_soc_poweroff,
1905 .restore = snd_soc_resume,
1907 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1909 /* ASoC platform driver */
1910 static struct platform_driver soc_driver = {
1912 .name = "soc-audio",
1913 .owner = THIS_MODULE,
1914 .pm = &snd_soc_pm_ops,
1917 .remove = soc_remove,
1921 * snd_soc_new_ac97_codec - initailise AC97 device
1922 * @codec: audio codec
1923 * @ops: AC97 bus operations
1924 * @num: AC97 codec number
1926 * Initialises AC97 codec resources for use by ad-hoc devices only.
1928 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1929 struct snd_ac97_bus_ops *ops, int num)
1931 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1932 if (codec->ac97 == NULL)
1935 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1936 if (codec->ac97->bus == NULL) {
1942 codec->ac97->bus->ops = ops;
1943 codec->ac97->num = num;
1946 * Mark the AC97 device to be created by us. This way we ensure that the
1947 * device will be registered with the device subsystem later on.
1949 codec->ac97_created = 1;
1953 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1955 static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
1957 static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
1959 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
1961 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
1963 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
1967 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
1969 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
1973 static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
1975 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
1977 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
1979 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
1980 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
1981 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
1985 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
1987 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
1991 static int snd_soc_ac97_parse_pinctl(struct device *dev,
1992 struct snd_ac97_reset_cfg *cfg)
1995 struct pinctrl_state *state;
1999 p = devm_pinctrl_get(dev);
2001 dev_err(dev, "Failed to get pinctrl\n");
2006 state = pinctrl_lookup_state(p, "ac97-reset");
2007 if (IS_ERR(state)) {
2008 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
2009 return PTR_ERR(state);
2011 cfg->pstate_reset = state;
2013 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2014 if (IS_ERR(state)) {
2015 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
2016 return PTR_ERR(state);
2018 cfg->pstate_warm_reset = state;
2020 state = pinctrl_lookup_state(p, "ac97-running");
2021 if (IS_ERR(state)) {
2022 dev_err(dev, "Can't find pinctrl state ac97-running\n");
2023 return PTR_ERR(state);
2025 cfg->pstate_run = state;
2027 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2029 dev_err(dev, "Can't find ac97-sync gpio\n");
2032 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2034 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2037 cfg->gpio_sync = gpio;
2039 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2041 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2044 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2046 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2049 cfg->gpio_sdata = gpio;
2051 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2053 dev_err(dev, "Can't find ac97-reset gpio\n");
2056 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2058 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2061 cfg->gpio_reset = gpio;
2066 struct snd_ac97_bus_ops *soc_ac97_ops;
2067 EXPORT_SYMBOL_GPL(soc_ac97_ops);
2069 int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2071 if (ops == soc_ac97_ops)
2074 if (soc_ac97_ops && ops)
2081 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2084 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2086 * This function sets the reset and warm_reset properties of ops and parses
2087 * the device node of pdev to get pinctrl states and gpio numbers to use.
2089 int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2090 struct platform_device *pdev)
2092 struct device *dev = &pdev->dev;
2093 struct snd_ac97_reset_cfg cfg;
2096 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2100 ret = snd_soc_set_ac97_ops(ops);
2104 ops->warm_reset = snd_soc_ac97_warm_reset;
2105 ops->reset = snd_soc_ac97_reset;
2107 snd_ac97_rst_cfg = cfg;
2110 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2113 * snd_soc_free_ac97_codec - free AC97 codec device
2114 * @codec: audio codec
2116 * Frees AC97 codec device resources.
2118 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2120 #ifdef CONFIG_SND_SOC_AC97_BUS
2121 soc_unregister_ac97_codec(codec);
2123 kfree(codec->ac97->bus);
2126 codec->ac97_created = 0;
2128 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2131 * snd_soc_cnew - create new control
2132 * @_template: control template
2133 * @data: control private data
2134 * @long_name: control long name
2135 * @prefix: control name prefix
2137 * Create a new mixer control from a template control.
2139 * Returns 0 for success, else error.
2141 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2142 void *data, const char *long_name,
2145 struct snd_kcontrol_new template;
2146 struct snd_kcontrol *kcontrol;
2149 memcpy(&template, _template, sizeof(template));
2153 long_name = template.name;
2156 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2160 template.name = name;
2162 template.name = long_name;
2165 kcontrol = snd_ctl_new1(&template, data);
2171 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2173 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2174 const struct snd_kcontrol_new *controls, int num_controls,
2175 const char *prefix, void *data)
2179 for (i = 0; i < num_controls; i++) {
2180 const struct snd_kcontrol_new *control = &controls[i];
2181 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2182 control->name, prefix));
2184 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2185 control->name, err);
2193 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2196 struct snd_card *card = soc_card->snd_card;
2197 struct snd_kcontrol *kctl;
2199 if (unlikely(!name))
2202 list_for_each_entry(kctl, &card->controls, list)
2203 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2207 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2210 * snd_soc_add_component_controls - Add an array of controls to a component.
2212 * @component: Component to add controls to
2213 * @controls: Array of controls to add
2214 * @num_controls: Number of elements in the array
2216 * Return: 0 for success, else error.
2218 int snd_soc_add_component_controls(struct snd_soc_component *component,
2219 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2221 struct snd_card *card = component->card->snd_card;
2223 return snd_soc_add_controls(card, component->dev, controls,
2224 num_controls, component->name_prefix, component);
2226 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2229 * snd_soc_add_codec_controls - add an array of controls to a codec.
2230 * Convenience function to add a list of controls. Many codecs were
2231 * duplicating this code.
2233 * @codec: codec to add controls to
2234 * @controls: array of controls to add
2235 * @num_controls: number of elements in the array
2237 * Return 0 for success, else error.
2239 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2240 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2242 return snd_soc_add_component_controls(&codec->component, controls,
2245 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2248 * snd_soc_add_platform_controls - add an array of controls to a platform.
2249 * Convenience function to add a list of controls.
2251 * @platform: platform to add controls to
2252 * @controls: array of controls to add
2253 * @num_controls: number of elements in the array
2255 * Return 0 for success, else error.
2257 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2258 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2260 return snd_soc_add_component_controls(&platform->component, controls,
2263 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2266 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2267 * Convenience function to add a list of controls.
2269 * @soc_card: SoC card to add controls to
2270 * @controls: array of controls to add
2271 * @num_controls: number of elements in the array
2273 * Return 0 for success, else error.
2275 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2276 const struct snd_kcontrol_new *controls, int num_controls)
2278 struct snd_card *card = soc_card->snd_card;
2280 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2283 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2286 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2287 * Convienience function to add a list of controls.
2289 * @dai: DAI to add controls to
2290 * @controls: array of controls to add
2291 * @num_controls: number of elements in the array
2293 * Return 0 for success, else error.
2295 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2296 const struct snd_kcontrol_new *controls, int num_controls)
2298 struct snd_card *card = dai->component->card->snd_card;
2300 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2303 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2306 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2308 * @clk_id: DAI specific clock ID
2309 * @freq: new clock frequency in Hz
2310 * @dir: new clock direction - input/output.
2312 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2314 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2315 unsigned int freq, int dir)
2317 if (dai->driver && dai->driver->ops->set_sysclk)
2318 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2319 else if (dai->codec && dai->codec->driver->set_sysclk)
2320 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
2325 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2328 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2330 * @clk_id: DAI specific clock ID
2331 * @source: Source for the clock
2332 * @freq: new clock frequency in Hz
2333 * @dir: new clock direction - input/output.
2335 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2337 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2338 int source, unsigned int freq, int dir)
2340 if (codec->driver->set_sysclk)
2341 return codec->driver->set_sysclk(codec, clk_id, source,
2346 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2349 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2351 * @div_id: DAI specific clock divider ID
2352 * @div: new clock divisor.
2354 * Configures the clock dividers. This is used to derive the best DAI bit and
2355 * frame clocks from the system or master clock. It's best to set the DAI bit
2356 * and frame clocks as low as possible to save system power.
2358 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2359 int div_id, int div)
2361 if (dai->driver && dai->driver->ops->set_clkdiv)
2362 return dai->driver->ops->set_clkdiv(dai, div_id, div);
2366 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2369 * snd_soc_dai_set_pll - configure DAI PLL.
2371 * @pll_id: DAI specific PLL ID
2372 * @source: DAI specific source for the PLL
2373 * @freq_in: PLL input clock frequency in Hz
2374 * @freq_out: requested PLL output clock frequency in Hz
2376 * Configures and enables PLL to generate output clock based on input clock.
2378 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2379 unsigned int freq_in, unsigned int freq_out)
2381 if (dai->driver && dai->driver->ops->set_pll)
2382 return dai->driver->ops->set_pll(dai, pll_id, source,
2384 else if (dai->codec && dai->codec->driver->set_pll)
2385 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2390 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2393 * snd_soc_codec_set_pll - configure codec PLL.
2395 * @pll_id: DAI specific PLL ID
2396 * @source: DAI specific source for the PLL
2397 * @freq_in: PLL input clock frequency in Hz
2398 * @freq_out: requested PLL output clock frequency in Hz
2400 * Configures and enables PLL to generate output clock based on input clock.
2402 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2403 unsigned int freq_in, unsigned int freq_out)
2405 if (codec->driver->set_pll)
2406 return codec->driver->set_pll(codec, pll_id, source,
2411 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2414 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2416 * @ratio Ratio of BCLK to Sample rate.
2418 * Configures the DAI for a preset BCLK to sample rate ratio.
2420 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2422 if (dai->driver && dai->driver->ops->set_bclk_ratio)
2423 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2427 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2430 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2432 * @fmt: SND_SOC_DAIFMT_ format value.
2434 * Configures the DAI hardware format and clocking.
2436 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2438 if (dai->driver == NULL)
2440 if (dai->driver->ops->set_fmt == NULL)
2442 return dai->driver->ops->set_fmt(dai, fmt);
2444 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2447 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
2448 * @slots: Number of slots in use.
2449 * @tx_mask: bitmask representing active TX slots.
2450 * @rx_mask: bitmask representing active RX slots.
2452 * Generates the TDM tx and rx slot default masks for DAI.
2454 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
2455 unsigned int *tx_mask,
2456 unsigned int *rx_mask)
2458 if (*tx_mask || *rx_mask)
2464 *tx_mask = (1 << slots) - 1;
2465 *rx_mask = (1 << slots) - 1;
2471 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2473 * @tx_mask: bitmask representing active TX slots.
2474 * @rx_mask: bitmask representing active RX slots.
2475 * @slots: Number of slots in use.
2476 * @slot_width: Width in bits for each slot.
2478 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2481 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2482 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2484 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2485 dai->driver->ops->xlate_tdm_slot_mask(slots,
2486 &tx_mask, &rx_mask);
2488 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
2490 dai->tx_mask = tx_mask;
2491 dai->rx_mask = rx_mask;
2493 if (dai->driver && dai->driver->ops->set_tdm_slot)
2494 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2499 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2502 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2504 * @tx_num: how many TX channels
2505 * @tx_slot: pointer to an array which imply the TX slot number channel
2507 * @rx_num: how many RX channels
2508 * @rx_slot: pointer to an array which imply the RX slot number channel
2511 * configure the relationship between channel number and TDM slot number.
2513 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2514 unsigned int tx_num, unsigned int *tx_slot,
2515 unsigned int rx_num, unsigned int *rx_slot)
2517 if (dai->driver && dai->driver->ops->set_channel_map)
2518 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2523 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2526 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2528 * @tristate: tristate enable
2530 * Tristates the DAI so that others can use it.
2532 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2534 if (dai->driver && dai->driver->ops->set_tristate)
2535 return dai->driver->ops->set_tristate(dai, tristate);
2539 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2542 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2544 * @mute: mute enable
2545 * @direction: stream to mute
2547 * Mutes the DAI DAC.
2549 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2555 if (dai->driver->ops->mute_stream)
2556 return dai->driver->ops->mute_stream(dai, mute, direction);
2557 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2558 dai->driver->ops->digital_mute)
2559 return dai->driver->ops->digital_mute(dai, mute);
2563 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2565 static int snd_soc_init_multicodec(struct snd_soc_card *card,
2566 struct snd_soc_dai_link *dai_link)
2568 /* Legacy codec/codec_dai link is a single entry in multicodec */
2569 if (dai_link->codec_name || dai_link->codec_of_node ||
2570 dai_link->codec_dai_name) {
2571 dai_link->num_codecs = 1;
2573 dai_link->codecs = devm_kzalloc(card->dev,
2574 sizeof(struct snd_soc_dai_link_component),
2576 if (!dai_link->codecs)
2579 dai_link->codecs[0].name = dai_link->codec_name;
2580 dai_link->codecs[0].of_node = dai_link->codec_of_node;
2581 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
2584 if (!dai_link->codecs) {
2585 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
2593 * snd_soc_register_card - Register a card with the ASoC core
2595 * @card: Card to register
2598 int snd_soc_register_card(struct snd_soc_card *card)
2602 if (!card->name || !card->dev)
2605 for (i = 0; i < card->num_links; i++) {
2606 struct snd_soc_dai_link *link = &card->dai_link[i];
2608 ret = snd_soc_init_multicodec(card, link);
2610 dev_err(card->dev, "ASoC: failed to init multicodec\n");
2614 for (j = 0; j < link->num_codecs; j++) {
2616 * Codec must be specified by 1 of name or OF node,
2617 * not both or neither.
2619 if (!!link->codecs[j].name ==
2620 !!link->codecs[j].of_node) {
2621 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
2625 /* Codec DAI name must be specified */
2626 if (!link->codecs[j].dai_name) {
2627 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
2634 * Platform may be specified by either name or OF node, but
2635 * can be left unspecified, and a dummy platform will be used.
2637 if (link->platform_name && link->platform_of_node) {
2639 "ASoC: Both platform name/of_node are set for %s\n",
2645 * CPU device may be specified by either name or OF node, but
2646 * can be left unspecified, and will be matched based on DAI
2649 if (link->cpu_name && link->cpu_of_node) {
2651 "ASoC: Neither/both cpu name/of_node are set for %s\n",
2656 * At least one of CPU DAI name or CPU device name/node must be
2659 if (!link->cpu_dai_name &&
2660 !(link->cpu_name || link->cpu_of_node)) {
2662 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
2668 dev_set_drvdata(card->dev, card);
2670 snd_soc_initialize_card_lists(card);
2672 soc_init_card_debugfs(card);
2674 card->rtd = devm_kzalloc(card->dev,
2675 sizeof(struct snd_soc_pcm_runtime) *
2676 (card->num_links + card->num_aux_devs),
2678 if (card->rtd == NULL)
2681 card->rtd_aux = &card->rtd[card->num_links];
2683 for (i = 0; i < card->num_links; i++) {
2684 card->rtd[i].card = card;
2685 card->rtd[i].dai_link = &card->dai_link[i];
2686 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
2687 sizeof(struct snd_soc_dai *) *
2688 (card->rtd[i].dai_link->num_codecs),
2690 if (card->rtd[i].codec_dais == NULL)
2694 for (i = 0; i < card->num_aux_devs; i++)
2695 card->rtd_aux[i].card = card;
2697 INIT_LIST_HEAD(&card->dapm_dirty);
2698 card->instantiated = 0;
2699 mutex_init(&card->mutex);
2700 mutex_init(&card->dapm_mutex);
2702 ret = snd_soc_instantiate_card(card);
2704 soc_cleanup_card_debugfs(card);
2706 /* deactivate pins to sleep state */
2707 for (i = 0; i < card->num_rtd; i++) {
2708 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2709 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2712 for (j = 0; j < rtd->num_codecs; j++) {
2713 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2714 if (!codec_dai->active)
2715 pinctrl_pm_select_sleep_state(codec_dai->dev);
2718 if (!cpu_dai->active)
2719 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2724 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2727 * snd_soc_unregister_card - Unregister a card with the ASoC core
2729 * @card: Card to unregister
2732 int snd_soc_unregister_card(struct snd_soc_card *card)
2734 if (card->instantiated) {
2735 card->instantiated = false;
2736 snd_soc_dapm_shutdown(card);
2737 soc_cleanup_card_resources(card);
2739 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2743 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2746 * Simplify DAI link configuration by removing ".-1" from device names
2747 * and sanitizing names.
2749 static char *fmt_single_name(struct device *dev, int *id)
2751 char *found, name[NAME_SIZE];
2754 if (dev_name(dev) == NULL)
2757 strlcpy(name, dev_name(dev), NAME_SIZE);
2759 /* are we a "%s.%d" name (platform and SPI components) */
2760 found = strstr(name, dev->driver->name);
2763 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2765 /* discard ID from name if ID == -1 */
2767 found[strlen(dev->driver->name)] = '\0';
2771 /* I2C component devices are named "bus-addr" */
2772 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2773 char tmp[NAME_SIZE];
2775 /* create unique ID number from I2C addr and bus */
2776 *id = ((id1 & 0xffff) << 16) + id2;
2778 /* sanitize component name for DAI link creation */
2779 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2780 strlcpy(name, tmp, NAME_SIZE);
2785 return kstrdup(name, GFP_KERNEL);
2789 * Simplify DAI link naming for single devices with multiple DAIs by removing
2790 * any ".-1" and using the DAI name (instead of device name).
2792 static inline char *fmt_multiple_name(struct device *dev,
2793 struct snd_soc_dai_driver *dai_drv)
2795 if (dai_drv->name == NULL) {
2797 "ASoC: error - multiple DAI %s registered with no name\n",
2802 return kstrdup(dai_drv->name, GFP_KERNEL);
2806 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2808 * @component: The component for which the DAIs should be unregistered
2810 static void snd_soc_unregister_dais(struct snd_soc_component *component)
2812 struct snd_soc_dai *dai, *_dai;
2814 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
2815 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2817 list_del(&dai->list);
2824 * snd_soc_register_dais - Register a DAI with the ASoC core
2826 * @component: The component the DAIs are registered for
2827 * @dai_drv: DAI driver to use for the DAIs
2828 * @count: Number of DAIs
2829 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
2832 static int snd_soc_register_dais(struct snd_soc_component *component,
2833 struct snd_soc_dai_driver *dai_drv, size_t count,
2834 bool legacy_dai_naming)
2836 struct device *dev = component->dev;
2837 struct snd_soc_dai *dai;
2841 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
2843 component->dai_drv = dai_drv;
2844 component->num_dai = count;
2846 for (i = 0; i < count; i++) {
2848 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2855 * Back in the old days when we still had component-less DAIs,
2856 * instead of having a static name, component-less DAIs would
2857 * inherit the name of the parent device so it is possible to
2858 * register multiple instances of the DAI. We still need to keep
2859 * the same naming style even though those DAIs are not
2860 * component-less anymore.
2862 if (count == 1 && legacy_dai_naming) {
2863 dai->name = fmt_single_name(dev, &dai->id);
2865 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
2867 dai->id = dai_drv[i].id;
2871 if (dai->name == NULL) {
2877 dai->component = component;
2879 dai->driver = &dai_drv[i];
2880 if (!dai->driver->ops)
2881 dai->driver->ops = &null_dai_ops;
2883 list_add(&dai->list, &component->dai_list);
2885 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2891 snd_soc_unregister_dais(component);
2896 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
2897 enum snd_soc_dapm_type type, int subseq)
2899 struct snd_soc_component *component = dapm->component;
2901 component->driver->seq_notifier(component, type, subseq);
2904 static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
2907 struct snd_soc_component *component = dapm->component;
2909 return component->driver->stream_event(component, event);
2912 static int snd_soc_component_initialize(struct snd_soc_component *component,
2913 const struct snd_soc_component_driver *driver, struct device *dev)
2915 struct snd_soc_dapm_context *dapm;
2917 component->name = fmt_single_name(dev, &component->id);
2918 if (!component->name) {
2919 dev_err(dev, "ASoC: Failed to allocate name\n");
2923 component->dev = dev;
2924 component->driver = driver;
2925 component->probe = component->driver->probe;
2926 component->remove = component->driver->remove;
2928 if (!component->dapm_ptr)
2929 component->dapm_ptr = &component->dapm;
2931 dapm = component->dapm_ptr;
2933 dapm->component = component;
2934 dapm->bias_level = SND_SOC_BIAS_OFF;
2935 dapm->idle_bias_off = true;
2936 if (driver->seq_notifier)
2937 dapm->seq_notifier = snd_soc_component_seq_notifier;
2938 if (driver->stream_event)
2939 dapm->stream_event = snd_soc_component_stream_event;
2941 component->controls = driver->controls;
2942 component->num_controls = driver->num_controls;
2943 component->dapm_widgets = driver->dapm_widgets;
2944 component->num_dapm_widgets = driver->num_dapm_widgets;
2945 component->dapm_routes = driver->dapm_routes;
2946 component->num_dapm_routes = driver->num_dapm_routes;
2948 INIT_LIST_HEAD(&component->dai_list);
2949 mutex_init(&component->io_mutex);
2954 static void snd_soc_component_init_regmap(struct snd_soc_component *component)
2956 if (!component->regmap)
2957 component->regmap = dev_get_regmap(component->dev, NULL);
2958 if (component->regmap) {
2959 int val_bytes = regmap_get_val_bytes(component->regmap);
2960 /* Errors are legitimate for non-integer byte multiples */
2962 component->val_bytes = val_bytes;
2966 static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
2968 if (!component->write && !component->read)
2969 snd_soc_component_init_regmap(component);
2971 list_add(&component->list, &component_list);
2974 static void snd_soc_component_add(struct snd_soc_component *component)
2976 mutex_lock(&client_mutex);
2977 snd_soc_component_add_unlocked(component);
2978 mutex_unlock(&client_mutex);
2981 static void snd_soc_component_cleanup(struct snd_soc_component *component)
2983 snd_soc_unregister_dais(component);
2984 kfree(component->name);
2987 static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2989 list_del(&component->list);
2992 static void snd_soc_component_del(struct snd_soc_component *component)
2994 mutex_lock(&client_mutex);
2995 snd_soc_component_del_unlocked(component);
2996 mutex_unlock(&client_mutex);
2999 int snd_soc_register_component(struct device *dev,
3000 const struct snd_soc_component_driver *cmpnt_drv,
3001 struct snd_soc_dai_driver *dai_drv,
3004 struct snd_soc_component *cmpnt;
3007 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
3009 dev_err(dev, "ASoC: Failed to allocate memory\n");
3013 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
3017 cmpnt->ignore_pmdown_time = true;
3018 cmpnt->registered_as_component = true;
3020 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
3022 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
3026 snd_soc_component_add(cmpnt);
3031 snd_soc_component_cleanup(cmpnt);
3036 EXPORT_SYMBOL_GPL(snd_soc_register_component);
3039 * snd_soc_unregister_component - Unregister a component from the ASoC core
3042 void snd_soc_unregister_component(struct device *dev)
3044 struct snd_soc_component *cmpnt;
3046 list_for_each_entry(cmpnt, &component_list, list) {
3047 if (dev == cmpnt->dev && cmpnt->registered_as_component)
3053 snd_soc_component_del(cmpnt);
3054 snd_soc_component_cleanup(cmpnt);
3057 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3059 static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
3061 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3063 return platform->driver->probe(platform);
3066 static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
3068 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3070 platform->driver->remove(platform);
3074 * snd_soc_add_platform - Add a platform to the ASoC core
3075 * @dev: The parent device for the platform
3076 * @platform: The platform to add
3077 * @platform_driver: The driver for the platform
3079 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
3080 const struct snd_soc_platform_driver *platform_drv)
3084 ret = snd_soc_component_initialize(&platform->component,
3085 &platform_drv->component_driver, dev);
3089 platform->dev = dev;
3090 platform->driver = platform_drv;
3092 if (platform_drv->probe)
3093 platform->component.probe = snd_soc_platform_drv_probe;
3094 if (platform_drv->remove)
3095 platform->component.remove = snd_soc_platform_drv_remove;
3097 #ifdef CONFIG_DEBUG_FS
3098 platform->component.debugfs_prefix = "platform";
3101 mutex_lock(&client_mutex);
3102 snd_soc_component_add_unlocked(&platform->component);
3103 list_add(&platform->list, &platform_list);
3104 mutex_unlock(&client_mutex);
3106 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
3107 platform->component.name);
3111 EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3114 * snd_soc_register_platform - Register a platform with the ASoC core
3116 * @platform: platform to register
3118 int snd_soc_register_platform(struct device *dev,
3119 const struct snd_soc_platform_driver *platform_drv)
3121 struct snd_soc_platform *platform;
3124 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3126 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3127 if (platform == NULL)
3130 ret = snd_soc_add_platform(dev, platform, platform_drv);
3136 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3139 * snd_soc_remove_platform - Remove a platform from the ASoC core
3140 * @platform: the platform to remove
3142 void snd_soc_remove_platform(struct snd_soc_platform *platform)
3145 mutex_lock(&client_mutex);
3146 list_del(&platform->list);
3147 snd_soc_component_del_unlocked(&platform->component);
3148 mutex_unlock(&client_mutex);
3150 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3151 platform->component.name);
3153 snd_soc_component_cleanup(&platform->component);
3155 EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
3157 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
3159 struct snd_soc_platform *platform;
3161 list_for_each_entry(platform, &platform_list, list) {
3162 if (dev == platform->dev)
3168 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
3171 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3173 * @platform: platform to unregister
3175 void snd_soc_unregister_platform(struct device *dev)
3177 struct snd_soc_platform *platform;
3179 platform = snd_soc_lookup_platform(dev);
3183 snd_soc_remove_platform(platform);
3186 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3188 static u64 codec_format_map[] = {
3189 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3190 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3191 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3192 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3193 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3194 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3195 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3196 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3197 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3198 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3199 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3200 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3201 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3202 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3203 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3204 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3207 /* Fix up the DAI formats for endianness: codecs don't actually see
3208 * the endianness of the data but we're using the CPU format
3209 * definitions which do need to include endianness so we ensure that
3210 * codec DAIs always have both big and little endian variants set.
3212 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3216 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3217 if (stream->formats & codec_format_map[i])
3218 stream->formats |= codec_format_map[i];
3221 static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3223 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3225 return codec->driver->probe(codec);
3228 static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3230 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3232 codec->driver->remove(codec);
3235 static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3236 unsigned int reg, unsigned int val)
3238 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3240 return codec->driver->write(codec, reg, val);
3243 static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3244 unsigned int reg, unsigned int *val)
3246 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3248 *val = codec->driver->read(codec, reg);
3253 static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
3254 enum snd_soc_bias_level level)
3256 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
3258 return codec->driver->set_bias_level(codec, level);
3262 * snd_soc_register_codec - Register a codec with the ASoC core
3264 * @codec: codec to register
3266 int snd_soc_register_codec(struct device *dev,
3267 const struct snd_soc_codec_driver *codec_drv,
3268 struct snd_soc_dai_driver *dai_drv,
3271 struct snd_soc_codec *codec;
3272 struct snd_soc_dai *dai;
3275 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3277 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3281 codec->component.dapm_ptr = &codec->dapm;
3282 codec->component.codec = codec;
3284 ret = snd_soc_component_initialize(&codec->component,
3285 &codec_drv->component_driver, dev);
3289 if (codec_drv->controls) {
3290 codec->component.controls = codec_drv->controls;
3291 codec->component.num_controls = codec_drv->num_controls;
3293 if (codec_drv->dapm_widgets) {
3294 codec->component.dapm_widgets = codec_drv->dapm_widgets;
3295 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
3297 if (codec_drv->dapm_routes) {
3298 codec->component.dapm_routes = codec_drv->dapm_routes;
3299 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
3302 if (codec_drv->probe)
3303 codec->component.probe = snd_soc_codec_drv_probe;
3304 if (codec_drv->remove)
3305 codec->component.remove = snd_soc_codec_drv_remove;
3306 if (codec_drv->write)
3307 codec->component.write = snd_soc_codec_drv_write;
3308 if (codec_drv->read)
3309 codec->component.read = snd_soc_codec_drv_read;
3310 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
3311 codec->dapm.idle_bias_off = codec_drv->idle_bias_off;
3312 codec->dapm.suspend_bias_off = codec_drv->suspend_bias_off;
3313 if (codec_drv->seq_notifier)
3314 codec->dapm.seq_notifier = codec_drv->seq_notifier;
3315 if (codec_drv->set_bias_level)
3316 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
3318 codec->driver = codec_drv;
3319 codec->component.val_bytes = codec_drv->reg_word_size;
3320 mutex_init(&codec->mutex);
3322 #ifdef CONFIG_DEBUG_FS
3323 codec->component.init_debugfs = soc_init_codec_debugfs;
3324 codec->component.debugfs_prefix = "codec";
3327 if (codec_drv->get_regmap)
3328 codec->component.regmap = codec_drv->get_regmap(dev);
3330 for (i = 0; i < num_dai; i++) {
3331 fixup_codec_formats(&dai_drv[i].playback);
3332 fixup_codec_formats(&dai_drv[i].capture);
3335 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3337 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
3341 list_for_each_entry(dai, &codec->component.dai_list, list)
3344 mutex_lock(&client_mutex);
3345 snd_soc_component_add_unlocked(&codec->component);
3346 list_add(&codec->list, &codec_list);
3347 mutex_unlock(&client_mutex);
3349 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3350 codec->component.name);
3354 snd_soc_component_cleanup(&codec->component);
3359 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3362 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3364 * @codec: codec to unregister
3366 void snd_soc_unregister_codec(struct device *dev)
3368 struct snd_soc_codec *codec;
3370 list_for_each_entry(codec, &codec_list, list) {
3371 if (dev == codec->dev)
3378 mutex_lock(&client_mutex);
3379 list_del(&codec->list);
3380 snd_soc_component_del_unlocked(&codec->component);
3381 mutex_unlock(&client_mutex);
3383 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3384 codec->component.name);
3386 snd_soc_component_cleanup(&codec->component);
3387 snd_soc_cache_exit(codec);
3390 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3392 /* Retrieve a card's name from device tree */
3393 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3394 const char *propname)
3396 struct device_node *np;
3400 pr_err("card->dev is not set before calling %s\n", __func__);
3404 np = card->dev->of_node;
3406 ret = of_property_read_string_index(np, propname, 0, &card->name);
3408 * EINVAL means the property does not exist. This is fine providing
3409 * card->name was previously set, which is checked later in
3410 * snd_soc_register_card.
3412 if (ret < 0 && ret != -EINVAL) {
3414 "ASoC: Property '%s' could not be read: %d\n",
3421 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3423 static const struct snd_soc_dapm_widget simple_widgets[] = {
3424 SND_SOC_DAPM_MIC("Microphone", NULL),
3425 SND_SOC_DAPM_LINE("Line", NULL),
3426 SND_SOC_DAPM_HP("Headphone", NULL),
3427 SND_SOC_DAPM_SPK("Speaker", NULL),
3430 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
3431 const char *propname)
3433 struct device_node *np = card->dev->of_node;
3434 struct snd_soc_dapm_widget *widgets;
3435 const char *template, *wname;
3436 int i, j, num_widgets, ret;
3438 num_widgets = of_property_count_strings(np, propname);
3439 if (num_widgets < 0) {
3441 "ASoC: Property '%s' does not exist\n", propname);
3444 if (num_widgets & 1) {
3446 "ASoC: Property '%s' length is not even\n", propname);
3452 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3457 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3461 "ASoC: Could not allocate memory for widgets\n");
3465 for (i = 0; i < num_widgets; i++) {
3466 ret = of_property_read_string_index(np, propname,
3470 "ASoC: Property '%s' index %d read error:%d\n",
3471 propname, 2 * i, ret);
3475 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3476 if (!strncmp(template, simple_widgets[j].name,
3477 strlen(simple_widgets[j].name))) {
3478 widgets[i] = simple_widgets[j];
3483 if (j >= ARRAY_SIZE(simple_widgets)) {
3485 "ASoC: DAPM widget '%s' is not supported\n",
3490 ret = of_property_read_string_index(np, propname,
3495 "ASoC: Property '%s' index %d read error:%d\n",
3496 propname, (2 * i) + 1, ret);
3500 widgets[i].name = wname;
3503 card->dapm_widgets = widgets;
3504 card->num_dapm_widgets = num_widgets;
3508 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
3510 int snd_soc_of_parse_tdm_slot(struct device_node *np,
3511 unsigned int *slots,
3512 unsigned int *slot_width)
3517 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3518 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3526 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3527 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3537 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3539 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3540 const char *propname)
3542 struct device_node *np = card->dev->of_node;
3544 struct snd_soc_dapm_route *routes;
3547 num_routes = of_property_count_strings(np, propname);
3548 if (num_routes < 0 || num_routes & 1) {
3550 "ASoC: Property '%s' does not exist or its length is not even\n",
3556 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3561 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3565 "ASoC: Could not allocate DAPM route table\n");
3569 for (i = 0; i < num_routes; i++) {
3570 ret = of_property_read_string_index(np, propname,
3571 2 * i, &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[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");