2 * soc-core.c -- ALSA SoC Audio Layer
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
30 #include <linux/bitops.h>
31 #include <linux/debugfs.h>
32 #include <linux/platform_device.h>
33 #include <linux/pinctrl/consumer.h>
34 #include <linux/ctype.h>
35 #include <linux/slab.h>
37 #include <sound/core.h>
38 #include <sound/jack.h>
39 #include <sound/pcm.h>
40 #include <sound/pcm_params.h>
41 #include <sound/soc.h>
42 #include <sound/soc-dpcm.h>
43 #include <sound/initval.h>
45 #define CREATE_TRACE_POINTS
46 #include <trace/events/asoc.h>
50 #ifdef CONFIG_DEBUG_FS
51 struct dentry *snd_soc_debugfs_root;
52 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
55 static DEFINE_MUTEX(client_mutex);
56 static LIST_HEAD(platform_list);
57 static LIST_HEAD(codec_list);
58 static LIST_HEAD(component_list);
61 * This is a timeout to do a DAPM powerdown after a stream is closed().
62 * It can be used to eliminate pops between different playback streams, e.g.
63 * between two audio tracks.
65 static int pmdown_time = 5000;
66 module_param(pmdown_time, int, 0);
67 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
69 /* returns the minimum number of bytes needed to represent
70 * a particular given value */
71 static int min_bytes_needed(unsigned long val)
76 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
79 c = (sizeof val * 8) - c;
87 /* fill buf which is 'len' bytes with a formatted
88 * string of the form 'reg: value\n' */
89 static int format_register_str(struct snd_soc_codec *codec,
90 unsigned int reg, char *buf, size_t len)
92 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
93 int regsize = codec->driver->reg_word_size * 2;
96 char regbuf[regsize + 1];
98 /* since tmpbuf is allocated on the stack, warn the callers if they
99 * try to abuse this function */
102 /* +2 for ': ' and + 1 for '\n' */
103 if (wordsize + regsize + 2 + 1 != len)
106 ret = snd_soc_read(codec, reg);
108 memset(regbuf, 'X', regsize);
109 regbuf[regsize] = '\0';
111 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
114 /* prepare the buffer */
115 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
116 /* copy it back to the caller without the '\0' */
117 memcpy(buf, tmpbuf, len);
122 /* codec register dump */
123 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
124 size_t count, loff_t pos)
127 int wordsize, regsize;
132 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
133 regsize = codec->driver->reg_word_size * 2;
135 len = wordsize + regsize + 2 + 1;
137 if (!codec->driver->reg_cache_size)
140 if (codec->driver->reg_cache_step)
141 step = codec->driver->reg_cache_step;
143 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
144 /* only support larger than PAGE_SIZE bytes debugfs
145 * entries for the default case */
147 if (total + len >= count - 1)
149 format_register_str(codec, i, buf + total, len);
155 total = min(total, count - 1);
160 static ssize_t codec_reg_show(struct device *dev,
161 struct device_attribute *attr, char *buf)
163 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
165 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
168 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
170 static ssize_t pmdown_time_show(struct device *dev,
171 struct device_attribute *attr, char *buf)
173 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
175 return sprintf(buf, "%ld\n", rtd->pmdown_time);
178 static ssize_t pmdown_time_set(struct device *dev,
179 struct device_attribute *attr,
180 const char *buf, size_t count)
182 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
185 ret = kstrtol(buf, 10, &rtd->pmdown_time);
192 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
194 #ifdef CONFIG_DEBUG_FS
195 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
196 size_t count, loff_t *ppos)
199 struct snd_soc_codec *codec = file->private_data;
202 if (*ppos < 0 || !count)
205 buf = kmalloc(count, GFP_KERNEL);
209 ret = soc_codec_reg_show(codec, buf, count, *ppos);
211 if (copy_to_user(user_buf, buf, ret)) {
222 static ssize_t codec_reg_write_file(struct file *file,
223 const char __user *user_buf, size_t count, loff_t *ppos)
228 unsigned long reg, value;
229 struct snd_soc_codec *codec = file->private_data;
232 buf_size = min(count, (sizeof(buf)-1));
233 if (copy_from_user(buf, user_buf, buf_size))
237 while (*start == ' ')
239 reg = simple_strtoul(start, &start, 16);
240 while (*start == ' ')
242 ret = kstrtoul(start, 16, &value);
246 /* Userspace has been fiddling around behind the kernel's back */
247 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
249 snd_soc_write(codec, reg, value);
253 static const struct file_operations codec_reg_fops = {
255 .read = codec_reg_read_file,
256 .write = codec_reg_write_file,
257 .llseek = default_llseek,
260 static void soc_init_component_debugfs(struct snd_soc_component *component)
262 if (component->debugfs_prefix) {
265 name = kasprintf(GFP_KERNEL, "%s:%s",
266 component->debugfs_prefix, component->name);
268 component->debugfs_root = debugfs_create_dir(name,
269 component->card->debugfs_card_root);
273 component->debugfs_root = debugfs_create_dir(component->name,
274 component->card->debugfs_card_root);
277 if (!component->debugfs_root) {
278 dev_warn(component->dev,
279 "ASoC: Failed to create component debugfs directory\n");
283 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
284 component->debugfs_root);
286 if (component->init_debugfs)
287 component->init_debugfs(component);
290 static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
292 debugfs_remove_recursive(component->debugfs_root);
295 static void soc_init_codec_debugfs(struct snd_soc_component *component)
297 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
299 debugfs_create_bool("cache_sync", 0444, codec->component.debugfs_root,
302 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
303 codec->component.debugfs_root,
304 codec, &codec_reg_fops);
305 if (!codec->debugfs_reg)
307 "ASoC: Failed to create codec register debugfs file\n");
310 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
311 size_t count, loff_t *ppos)
313 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
314 ssize_t len, ret = 0;
315 struct snd_soc_codec *codec;
320 list_for_each_entry(codec, &codec_list, list) {
321 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
322 codec->component.name);
325 if (ret > PAGE_SIZE) {
332 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
339 static const struct file_operations codec_list_fops = {
340 .read = codec_list_read_file,
341 .llseek = default_llseek,/* read accesses f_pos */
344 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
345 size_t count, loff_t *ppos)
347 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
348 ssize_t len, ret = 0;
349 struct snd_soc_component *component;
350 struct snd_soc_dai *dai;
355 list_for_each_entry(component, &component_list, list) {
356 list_for_each_entry(dai, &component->dai_list, list) {
357 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
361 if (ret > PAGE_SIZE) {
368 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
375 static const struct file_operations dai_list_fops = {
376 .read = dai_list_read_file,
377 .llseek = default_llseek,/* read accesses f_pos */
380 static ssize_t platform_list_read_file(struct file *file,
381 char __user *user_buf,
382 size_t count, loff_t *ppos)
384 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
385 ssize_t len, ret = 0;
386 struct snd_soc_platform *platform;
391 list_for_each_entry(platform, &platform_list, list) {
392 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
393 platform->component.name);
396 if (ret > PAGE_SIZE) {
402 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
409 static const struct file_operations platform_list_fops = {
410 .read = platform_list_read_file,
411 .llseek = default_llseek,/* read accesses f_pos */
414 static void soc_init_card_debugfs(struct snd_soc_card *card)
416 card->debugfs_card_root = debugfs_create_dir(card->name,
417 snd_soc_debugfs_root);
418 if (!card->debugfs_card_root) {
420 "ASoC: Failed to create card debugfs directory\n");
424 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
425 card->debugfs_card_root,
427 if (!card->debugfs_pop_time)
429 "ASoC: Failed to create pop time debugfs file\n");
432 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
434 debugfs_remove_recursive(card->debugfs_card_root);
439 #define soc_init_codec_debugfs NULL
441 static inline void soc_init_component_debugfs(
442 struct snd_soc_component *component)
446 static inline void soc_cleanup_component_debugfs(
447 struct snd_soc_component *component)
451 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
455 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
460 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
461 const char *dai_link, int stream)
465 for (i = 0; i < card->num_links; i++) {
466 if (card->rtd[i].dai_link->no_pcm &&
467 !strcmp(card->rtd[i].dai_link->name, dai_link))
468 return card->rtd[i].pcm->streams[stream].substream;
470 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
473 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
475 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
476 const char *dai_link)
480 for (i = 0; i < card->num_links; i++) {
481 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
482 return &card->rtd[i];
484 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
487 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
489 static void codec2codec_close_delayed_work(struct work_struct *work)
491 /* Currently nothing to do for c2c links
492 * Since c2c links are internal nodes in the DAPM graph and
493 * don't interface with the outside world or application layer
494 * we don't have to do any special handling on close.
498 #ifdef CONFIG_PM_SLEEP
499 /* powers down audio subsystem for suspend */
500 int snd_soc_suspend(struct device *dev)
502 struct snd_soc_card *card = dev_get_drvdata(dev);
503 struct snd_soc_codec *codec;
506 /* If the card is not initialized yet there is nothing to do */
507 if (!card->instantiated)
510 /* Due to the resume being scheduled into a workqueue we could
511 * suspend before that's finished - wait for it to complete.
513 snd_power_lock(card->snd_card);
514 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
515 snd_power_unlock(card->snd_card);
517 /* we're going to block userspace touching us until resume completes */
518 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
520 /* mute any active DACs */
521 for (i = 0; i < card->num_rtd; i++) {
523 if (card->rtd[i].dai_link->ignore_suspend)
526 for (j = 0; j < card->rtd[i].num_codecs; j++) {
527 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
528 struct snd_soc_dai_driver *drv = dai->driver;
530 if (drv->ops->digital_mute && dai->playback_active)
531 drv->ops->digital_mute(dai, 1);
535 /* suspend all pcms */
536 for (i = 0; i < card->num_rtd; i++) {
537 if (card->rtd[i].dai_link->ignore_suspend)
540 snd_pcm_suspend_all(card->rtd[i].pcm);
543 if (card->suspend_pre)
544 card->suspend_pre(card);
546 for (i = 0; i < card->num_rtd; i++) {
547 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
548 struct snd_soc_platform *platform = card->rtd[i].platform;
550 if (card->rtd[i].dai_link->ignore_suspend)
553 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
554 cpu_dai->driver->suspend(cpu_dai);
555 if (platform->driver->suspend && !platform->suspended) {
556 platform->driver->suspend(cpu_dai);
557 platform->suspended = 1;
561 /* close any waiting streams and save state */
562 for (i = 0; i < card->num_rtd; i++) {
563 struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais;
564 flush_delayed_work(&card->rtd[i].delayed_work);
565 for (j = 0; j < card->rtd[i].num_codecs; j++) {
566 codec_dais[j]->codec->dapm.suspend_bias_level =
567 codec_dais[j]->codec->dapm.bias_level;
571 for (i = 0; i < card->num_rtd; i++) {
573 if (card->rtd[i].dai_link->ignore_suspend)
576 snd_soc_dapm_stream_event(&card->rtd[i],
577 SNDRV_PCM_STREAM_PLAYBACK,
578 SND_SOC_DAPM_STREAM_SUSPEND);
580 snd_soc_dapm_stream_event(&card->rtd[i],
581 SNDRV_PCM_STREAM_CAPTURE,
582 SND_SOC_DAPM_STREAM_SUSPEND);
585 /* Recheck all analogue paths too */
586 dapm_mark_io_dirty(&card->dapm);
587 snd_soc_dapm_sync(&card->dapm);
589 /* suspend all CODECs */
590 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
591 /* If there are paths active then the CODEC will be held with
592 * bias _ON and should not be suspended. */
593 if (!codec->suspended) {
594 switch (codec->dapm.bias_level) {
595 case SND_SOC_BIAS_STANDBY:
597 * If the CODEC is capable of idle
598 * bias off then being in STANDBY
599 * means it's doing something,
600 * otherwise fall through.
602 if (codec->dapm.idle_bias_off) {
604 "ASoC: idle_bias_off CODEC on over suspend\n");
608 case SND_SOC_BIAS_OFF:
609 if (codec->driver->suspend)
610 codec->driver->suspend(codec);
611 codec->suspended = 1;
612 codec->cache_sync = 1;
613 if (codec->component.regmap)
614 regcache_mark_dirty(codec->component.regmap);
615 /* deactivate pins to sleep state */
616 pinctrl_pm_select_sleep_state(codec->dev);
620 "ASoC: CODEC is on over suspend\n");
626 for (i = 0; i < card->num_rtd; i++) {
627 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
629 if (card->rtd[i].dai_link->ignore_suspend)
632 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
633 cpu_dai->driver->suspend(cpu_dai);
635 /* deactivate pins to sleep state */
636 pinctrl_pm_select_sleep_state(cpu_dai->dev);
639 if (card->suspend_post)
640 card->suspend_post(card);
644 EXPORT_SYMBOL_GPL(snd_soc_suspend);
646 /* deferred resume work, so resume can complete before we finished
647 * setting our codec back up, which can be very slow on I2C
649 static void soc_resume_deferred(struct work_struct *work)
651 struct snd_soc_card *card =
652 container_of(work, struct snd_soc_card, deferred_resume_work);
653 struct snd_soc_codec *codec;
656 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
657 * so userspace apps are blocked from touching us
660 dev_dbg(card->dev, "ASoC: starting resume work\n");
662 /* Bring us up into D2 so that DAPM starts enabling things */
663 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
665 if (card->resume_pre)
666 card->resume_pre(card);
668 /* resume AC97 DAIs */
669 for (i = 0; i < card->num_rtd; i++) {
670 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
672 if (card->rtd[i].dai_link->ignore_suspend)
675 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
676 cpu_dai->driver->resume(cpu_dai);
679 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
680 /* If the CODEC was idle over suspend then it will have been
681 * left with bias OFF or STANDBY and suspended so we must now
682 * resume. Otherwise the suspend was suppressed.
684 if (codec->suspended) {
685 switch (codec->dapm.bias_level) {
686 case SND_SOC_BIAS_STANDBY:
687 case SND_SOC_BIAS_OFF:
688 if (codec->driver->resume)
689 codec->driver->resume(codec);
690 codec->suspended = 0;
694 "ASoC: CODEC was on over suspend\n");
700 for (i = 0; i < card->num_rtd; i++) {
702 if (card->rtd[i].dai_link->ignore_suspend)
705 snd_soc_dapm_stream_event(&card->rtd[i],
706 SNDRV_PCM_STREAM_PLAYBACK,
707 SND_SOC_DAPM_STREAM_RESUME);
709 snd_soc_dapm_stream_event(&card->rtd[i],
710 SNDRV_PCM_STREAM_CAPTURE,
711 SND_SOC_DAPM_STREAM_RESUME);
714 /* unmute any active DACs */
715 for (i = 0; i < card->num_rtd; i++) {
717 if (card->rtd[i].dai_link->ignore_suspend)
720 for (j = 0; j < card->rtd[i].num_codecs; j++) {
721 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
722 struct snd_soc_dai_driver *drv = dai->driver;
724 if (drv->ops->digital_mute && dai->playback_active)
725 drv->ops->digital_mute(dai, 0);
729 for (i = 0; i < card->num_rtd; i++) {
730 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
731 struct snd_soc_platform *platform = card->rtd[i].platform;
733 if (card->rtd[i].dai_link->ignore_suspend)
736 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
737 cpu_dai->driver->resume(cpu_dai);
738 if (platform->driver->resume && platform->suspended) {
739 platform->driver->resume(cpu_dai);
740 platform->suspended = 0;
744 if (card->resume_post)
745 card->resume_post(card);
747 dev_dbg(card->dev, "ASoC: resume work completed\n");
749 /* userspace can access us now we are back as we were before */
750 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
752 /* Recheck all analogue paths too */
753 dapm_mark_io_dirty(&card->dapm);
754 snd_soc_dapm_sync(&card->dapm);
757 /* powers up audio subsystem after a suspend */
758 int snd_soc_resume(struct device *dev)
760 struct snd_soc_card *card = dev_get_drvdata(dev);
761 int i, ac97_control = 0;
763 /* If the card is not initialized yet there is nothing to do */
764 if (!card->instantiated)
767 /* activate pins from sleep state */
768 for (i = 0; i < card->num_rtd; i++) {
769 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
770 struct snd_soc_dai **codec_dais = rtd->codec_dais;
771 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
775 pinctrl_pm_select_default_state(cpu_dai->dev);
777 for (j = 0; j < rtd->num_codecs; j++) {
778 struct snd_soc_dai *codec_dai = codec_dais[j];
779 if (codec_dai->active)
780 pinctrl_pm_select_default_state(codec_dai->dev);
784 /* AC97 devices might have other drivers hanging off them so
785 * need to resume immediately. Other drivers don't have that
786 * problem and may take a substantial amount of time to resume
787 * due to I/O costs and anti-pop so handle them out of line.
789 for (i = 0; i < card->num_rtd; i++) {
790 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
791 ac97_control |= cpu_dai->driver->ac97_control;
794 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
795 soc_resume_deferred(&card->deferred_resume_work);
797 dev_dbg(dev, "ASoC: Scheduling resume work\n");
798 if (!schedule_work(&card->deferred_resume_work))
799 dev_err(dev, "ASoC: resume work item may be lost\n");
804 EXPORT_SYMBOL_GPL(snd_soc_resume);
806 #define snd_soc_suspend NULL
807 #define snd_soc_resume NULL
810 static const struct snd_soc_dai_ops null_dai_ops = {
813 static struct snd_soc_component *soc_find_component(
814 const struct device_node *of_node, const char *name)
816 struct snd_soc_component *component;
818 list_for_each_entry(component, &component_list, list) {
820 if (component->dev->of_node == of_node)
822 } else if (strcmp(component->name, name) == 0) {
830 static struct snd_soc_dai *snd_soc_find_dai(
831 const struct snd_soc_dai_link_component *dlc)
833 struct snd_soc_component *component;
834 struct snd_soc_dai *dai;
836 /* Find CPU DAI from registered DAIs*/
837 list_for_each_entry(component, &component_list, list) {
838 if (dlc->of_node && component->dev->of_node != dlc->of_node)
840 if (dlc->name && strcmp(dev_name(component->dev), dlc->name))
842 list_for_each_entry(dai, &component->dai_list, list) {
843 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
853 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
855 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
856 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
857 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
858 struct snd_soc_dai_link_component cpu_dai_component;
859 struct snd_soc_dai **codec_dais = rtd->codec_dais;
860 struct snd_soc_platform *platform;
861 const char *platform_name;
864 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
866 cpu_dai_component.name = dai_link->cpu_name;
867 cpu_dai_component.of_node = dai_link->cpu_of_node;
868 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
869 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
871 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
872 dai_link->cpu_dai_name);
873 return -EPROBE_DEFER;
876 rtd->num_codecs = dai_link->num_codecs;
878 /* Find CODEC from registered CODECs */
879 for (i = 0; i < rtd->num_codecs; i++) {
880 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
881 if (!codec_dais[i]) {
882 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
884 return -EPROBE_DEFER;
888 /* Single codec links expect codec and codec_dai in runtime data */
889 rtd->codec_dai = codec_dais[0];
890 rtd->codec = rtd->codec_dai->codec;
892 /* if there's no platform we match on the empty platform */
893 platform_name = dai_link->platform_name;
894 if (!platform_name && !dai_link->platform_of_node)
895 platform_name = "snd-soc-dummy";
897 /* find one from the set of registered platforms */
898 list_for_each_entry(platform, &platform_list, list) {
899 if (dai_link->platform_of_node) {
900 if (platform->dev->of_node !=
901 dai_link->platform_of_node)
904 if (strcmp(platform->component.name, platform_name))
908 rtd->platform = platform;
910 if (!rtd->platform) {
911 dev_err(card->dev, "ASoC: platform %s not registered\n",
912 dai_link->platform_name);
913 return -EPROBE_DEFER;
921 static void soc_remove_component(struct snd_soc_component *component)
923 if (!component->probed)
926 /* This is a HACK and will be removed soon */
927 if (component->codec)
928 list_del(&component->codec->card_list);
930 if (component->remove)
931 component->remove(component);
933 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
935 soc_cleanup_component_debugfs(component);
936 component->probed = 0;
937 module_put(component->dev->driver->owner);
940 static void soc_remove_dai(struct snd_soc_dai *dai, int order)
944 if (dai && dai->probed &&
945 dai->driver->remove_order == order) {
946 if (dai->driver->remove) {
947 err = dai->driver->remove(dai);
950 "ASoC: failed to remove %s: %d\n",
957 static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
959 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
962 /* unregister the rtd device */
963 if (rtd->dev_registered) {
964 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
965 device_remove_file(rtd->dev, &dev_attr_codec_reg);
966 device_unregister(rtd->dev);
967 rtd->dev_registered = 0;
970 /* remove the CODEC DAI */
971 for (i = 0; i < rtd->num_codecs; i++)
972 soc_remove_dai(rtd->codec_dais[i], order);
974 soc_remove_dai(rtd->cpu_dai, order);
977 static void soc_remove_link_components(struct snd_soc_card *card, int num,
980 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
981 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
982 struct snd_soc_platform *platform = rtd->platform;
983 struct snd_soc_component *component;
986 /* remove the platform */
987 if (platform && platform->component.driver->remove_order == order)
988 soc_remove_component(&platform->component);
990 /* remove the CODEC-side CODEC */
991 for (i = 0; i < rtd->num_codecs; i++) {
992 component = rtd->codec_dais[i]->component;
993 if (component->driver->remove_order == order)
994 soc_remove_component(component);
997 /* remove any CPU-side CODEC */
999 if (cpu_dai->component->driver->remove_order == order)
1000 soc_remove_component(cpu_dai->component);
1004 static void soc_remove_dai_links(struct snd_soc_card *card)
1008 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1010 for (dai = 0; dai < card->num_rtd; dai++)
1011 soc_remove_link_dais(card, dai, order);
1014 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1016 for (dai = 0; dai < card->num_rtd; dai++)
1017 soc_remove_link_components(card, dai, order);
1023 static void soc_set_name_prefix(struct snd_soc_card *card,
1024 struct snd_soc_component *component)
1028 if (card->codec_conf == NULL)
1031 for (i = 0; i < card->num_configs; i++) {
1032 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1033 if (map->of_node && component->dev->of_node != map->of_node)
1035 if (map->dev_name && strcmp(component->name, map->dev_name))
1037 component->name_prefix = map->name_prefix;
1042 static int soc_probe_component(struct snd_soc_card *card,
1043 struct snd_soc_component *component)
1045 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1046 struct snd_soc_dai *dai;
1049 if (component->probed)
1052 component->card = card;
1054 soc_set_name_prefix(card, component);
1056 if (!try_module_get(component->dev->driver->owner))
1059 soc_init_component_debugfs(component);
1061 if (component->dapm_widgets) {
1062 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1063 component->num_dapm_widgets);
1066 dev_err(component->dev,
1067 "Failed to create new controls %d\n", ret);
1072 list_for_each_entry(dai, &component->dai_list, list) {
1073 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1075 dev_err(component->dev,
1076 "Failed to create DAI widgets %d\n", ret);
1081 if (component->probe) {
1082 ret = component->probe(component);
1084 dev_err(component->dev,
1085 "ASoC: failed to probe component %d\n", ret);
1089 WARN(dapm->idle_bias_off &&
1090 dapm->bias_level != SND_SOC_BIAS_OFF,
1091 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1095 if (component->controls)
1096 snd_soc_add_component_controls(component, component->controls,
1097 component->num_controls);
1098 if (component->dapm_routes)
1099 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1100 component->num_dapm_routes);
1102 component->probed = 1;
1103 list_add(&dapm->list, &card->dapm_list);
1105 /* This is a HACK and will be removed soon */
1106 if (component->codec)
1107 list_add(&component->codec->card_list, &card->codec_dev_list);
1112 soc_cleanup_component_debugfs(component);
1113 module_put(component->dev->driver->owner);
1118 static void rtd_release(struct device *dev)
1123 static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1128 /* register the rtd device */
1129 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1132 device_initialize(rtd->dev);
1133 rtd->dev->parent = rtd->card->dev;
1134 rtd->dev->release = rtd_release;
1135 dev_set_name(rtd->dev, "%s", name);
1136 dev_set_drvdata(rtd->dev, rtd);
1137 mutex_init(&rtd->pcm_mutex);
1138 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1139 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1140 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1141 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1142 ret = device_add(rtd->dev);
1144 /* calling put_device() here to free the rtd->dev */
1145 put_device(rtd->dev);
1146 dev_err(rtd->card->dev,
1147 "ASoC: failed to register runtime device: %d\n", ret);
1150 rtd->dev_registered = 1;
1153 /* add DAPM sysfs entries for this codec */
1154 ret = snd_soc_dapm_sys_add(rtd->dev);
1157 "ASoC: failed to add codec dapm sysfs entries: %d\n",
1160 /* add codec sysfs entries */
1161 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1164 "ASoC: failed to add codec sysfs files: %d\n",
1171 static int soc_probe_link_components(struct snd_soc_card *card, int num,
1174 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1175 struct snd_soc_platform *platform = rtd->platform;
1176 struct snd_soc_component *component;
1179 /* probe the CPU-side component, if it is a CODEC */
1180 component = rtd->cpu_dai->component;
1181 if (component->driver->probe_order == order) {
1182 ret = soc_probe_component(card, component);
1187 /* probe the CODEC-side components */
1188 for (i = 0; i < rtd->num_codecs; i++) {
1189 component = rtd->codec_dais[i]->component;
1190 if (component->driver->probe_order == order) {
1191 ret = soc_probe_component(card, component);
1197 /* probe the platform */
1198 if (platform->component.driver->probe_order == order) {
1199 ret = soc_probe_component(card, &platform->component);
1207 static int soc_probe_codec_dai(struct snd_soc_card *card,
1208 struct snd_soc_dai *codec_dai,
1213 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1214 if (codec_dai->driver->probe) {
1215 ret = codec_dai->driver->probe(codec_dai);
1217 dev_err(codec_dai->dev,
1218 "ASoC: failed to probe CODEC DAI %s: %d\n",
1219 codec_dai->name, ret);
1224 /* mark codec_dai as probed and add to card dai list */
1225 codec_dai->probed = 1;
1231 static int soc_link_dai_widgets(struct snd_soc_card *card,
1232 struct snd_soc_dai_link *dai_link,
1233 struct snd_soc_pcm_runtime *rtd)
1235 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1236 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1237 struct snd_soc_dapm_widget *play_w, *capture_w;
1240 if (rtd->num_codecs > 1)
1241 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1243 /* link the DAI widgets */
1244 play_w = codec_dai->playback_widget;
1245 capture_w = cpu_dai->capture_widget;
1246 if (play_w && capture_w) {
1247 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1250 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1251 play_w->name, capture_w->name, ret);
1256 play_w = cpu_dai->playback_widget;
1257 capture_w = codec_dai->capture_widget;
1258 if (play_w && capture_w) {
1259 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1262 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1263 play_w->name, capture_w->name, ret);
1271 static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1273 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1274 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1275 struct snd_soc_platform *platform = rtd->platform;
1276 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1279 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1280 card->name, num, order);
1282 /* config components */
1283 cpu_dai->platform = platform;
1284 cpu_dai->card = card;
1285 for (i = 0; i < rtd->num_codecs; i++)
1286 rtd->codec_dais[i]->card = card;
1288 /* set default power off timeout */
1289 rtd->pmdown_time = pmdown_time;
1291 /* probe the cpu_dai */
1292 if (!cpu_dai->probed &&
1293 cpu_dai->driver->probe_order == order) {
1294 if (cpu_dai->driver->probe) {
1295 ret = cpu_dai->driver->probe(cpu_dai);
1297 dev_err(cpu_dai->dev,
1298 "ASoC: failed to probe CPU DAI %s: %d\n",
1299 cpu_dai->name, ret);
1303 cpu_dai->probed = 1;
1306 /* probe the CODEC DAI */
1307 for (i = 0; i < rtd->num_codecs; i++) {
1308 ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order);
1313 /* complete DAI probe during last probe */
1314 if (order != SND_SOC_COMP_ORDER_LAST)
1317 /* do machine specific initialization */
1318 if (dai_link->init) {
1319 ret = dai_link->init(rtd);
1321 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1322 dai_link->name, ret);
1327 ret = soc_post_component_init(rtd, dai_link->name);
1331 #ifdef CONFIG_DEBUG_FS
1332 /* add DPCM sysfs entries */
1333 if (dai_link->dynamic) {
1334 ret = soc_dpcm_debugfs_add(rtd);
1337 "ASoC: failed to add dpcm sysfs entries: %d\n",
1344 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1346 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1349 if (cpu_dai->driver->compress_dai) {
1350 /*create compress_device"*/
1351 ret = soc_new_compress(rtd, num);
1353 dev_err(card->dev, "ASoC: can't create compress %s\n",
1354 dai_link->stream_name);
1359 if (!dai_link->params) {
1360 /* create the pcm */
1361 ret = soc_new_pcm(rtd, num);
1363 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1364 dai_link->stream_name, ret);
1368 INIT_DELAYED_WORK(&rtd->delayed_work,
1369 codec2codec_close_delayed_work);
1371 /* link the DAI widgets */
1372 ret = soc_link_dai_widgets(card, dai_link, rtd);
1381 static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1383 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1384 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1385 const char *name = aux_dev->codec_name;
1387 rtd->component = soc_find_component(aux_dev->codec_of_node, name);
1388 if (!rtd->component) {
1389 if (aux_dev->codec_of_node)
1390 name = of_node_full_name(aux_dev->codec_of_node);
1392 dev_err(card->dev, "ASoC: %s not registered\n", name);
1393 return -EPROBE_DEFER;
1397 * Some places still reference rtd->codec, so we have to keep that
1398 * initialized if the component is a CODEC. Once all those references
1399 * have been removed, this code can be removed as well.
1401 rtd->codec = rtd->component->codec;
1406 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1408 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1409 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1412 ret = soc_probe_component(card, rtd->component);
1416 /* do machine specific initialization */
1417 if (aux_dev->init) {
1418 ret = aux_dev->init(rtd->component);
1420 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1421 aux_dev->name, ret);
1426 return soc_post_component_init(rtd, aux_dev->name);
1429 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1431 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1432 struct snd_soc_component *component = rtd->component;
1434 /* unregister the rtd device */
1435 if (rtd->dev_registered) {
1436 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1437 device_unregister(rtd->dev);
1438 rtd->dev_registered = 0;
1441 if (component && component->probed)
1442 soc_remove_component(component);
1445 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1449 if (codec->cache_init)
1452 ret = snd_soc_cache_init(codec);
1455 "ASoC: Failed to set cache compression type: %d\n",
1459 codec->cache_init = 1;
1463 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1465 struct snd_soc_codec *codec;
1466 struct snd_soc_dai_link *dai_link;
1467 int ret, i, order, dai_fmt;
1469 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1472 for (i = 0; i < card->num_links; i++) {
1473 ret = soc_bind_dai_link(card, i);
1478 /* bind aux_devs too */
1479 for (i = 0; i < card->num_aux_devs; i++) {
1480 ret = soc_bind_aux_dev(card, i);
1485 /* initialize the register cache for each available codec */
1486 list_for_each_entry(codec, &codec_list, list) {
1487 if (codec->cache_init)
1489 ret = snd_soc_init_codec_cache(codec);
1494 /* card bind complete so register a sound card */
1495 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1496 card->owner, 0, &card->snd_card);
1499 "ASoC: can't create sound card for card %s: %d\n",
1504 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1505 card->dapm.dev = card->dev;
1506 card->dapm.card = card;
1507 list_add(&card->dapm.list, &card->dapm_list);
1509 #ifdef CONFIG_DEBUG_FS
1510 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1513 #ifdef CONFIG_PM_SLEEP
1514 /* deferred resume work */
1515 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1518 if (card->dapm_widgets)
1519 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1520 card->num_dapm_widgets);
1522 /* initialise the sound card only once */
1524 ret = card->probe(card);
1526 goto card_probe_error;
1529 /* probe all components used by DAI links on this card */
1530 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1532 for (i = 0; i < card->num_links; i++) {
1533 ret = soc_probe_link_components(card, i, order);
1536 "ASoC: failed to instantiate card %d\n",
1543 /* probe all DAI links on this card */
1544 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1546 for (i = 0; i < card->num_links; i++) {
1547 ret = soc_probe_link_dais(card, i, order);
1550 "ASoC: failed to instantiate card %d\n",
1557 for (i = 0; i < card->num_aux_devs; i++) {
1558 ret = soc_probe_aux_dev(card, i);
1561 "ASoC: failed to add auxiliary devices %d\n",
1563 goto probe_aux_dev_err;
1567 snd_soc_dapm_link_dai_widgets(card);
1568 snd_soc_dapm_connect_dai_link_widgets(card);
1571 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1573 if (card->dapm_routes)
1574 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1575 card->num_dapm_routes);
1577 for (i = 0; i < card->num_links; i++) {
1578 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1579 dai_link = &card->dai_link[i];
1580 dai_fmt = dai_link->dai_fmt;
1583 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1586 for (j = 0; j < rtd->num_codecs; j++) {
1587 struct snd_soc_dai *codec_dai = codec_dais[j];
1589 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1590 if (ret != 0 && ret != -ENOTSUPP)
1591 dev_warn(codec_dai->dev,
1592 "ASoC: Failed to set DAI format: %d\n",
1597 /* If this is a regular CPU link there will be a platform */
1599 (dai_link->platform_name || dai_link->platform_of_node)) {
1600 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1602 if (ret != 0 && ret != -ENOTSUPP)
1603 dev_warn(card->rtd[i].cpu_dai->dev,
1604 "ASoC: Failed to set DAI format: %d\n",
1606 } else if (dai_fmt) {
1607 /* Flip the polarity for the "CPU" end */
1608 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1609 switch (dai_link->dai_fmt &
1610 SND_SOC_DAIFMT_MASTER_MASK) {
1611 case SND_SOC_DAIFMT_CBM_CFM:
1612 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1614 case SND_SOC_DAIFMT_CBM_CFS:
1615 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1617 case SND_SOC_DAIFMT_CBS_CFM:
1618 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1620 case SND_SOC_DAIFMT_CBS_CFS:
1621 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1625 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1627 if (ret != 0 && ret != -ENOTSUPP)
1628 dev_warn(card->rtd[i].cpu_dai->dev,
1629 "ASoC: Failed to set DAI format: %d\n",
1634 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1636 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1637 "%s", card->long_name ? card->long_name : card->name);
1638 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1639 "%s", card->driver_name ? card->driver_name : card->name);
1640 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1641 switch (card->snd_card->driver[i]) {
1647 if (!isalnum(card->snd_card->driver[i]))
1648 card->snd_card->driver[i] = '_';
1653 if (card->late_probe) {
1654 ret = card->late_probe(card);
1656 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
1658 goto probe_aux_dev_err;
1662 if (card->fully_routed)
1663 snd_soc_dapm_auto_nc_pins(card);
1665 snd_soc_dapm_new_widgets(card);
1667 ret = snd_card_register(card->snd_card);
1669 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1671 goto probe_aux_dev_err;
1674 card->instantiated = 1;
1675 snd_soc_dapm_sync(&card->dapm);
1676 mutex_unlock(&card->mutex);
1681 for (i = 0; i < card->num_aux_devs; i++)
1682 soc_remove_aux_dev(card, i);
1685 soc_remove_dai_links(card);
1691 snd_card_free(card->snd_card);
1694 mutex_unlock(&card->mutex);
1699 /* probes a new socdev */
1700 static int soc_probe(struct platform_device *pdev)
1702 struct snd_soc_card *card = platform_get_drvdata(pdev);
1705 * no card, so machine driver should be registering card
1706 * we should not be here in that case so ret error
1711 dev_warn(&pdev->dev,
1712 "ASoC: machine %s should use snd_soc_register_card()\n",
1715 /* Bodge while we unpick instantiation */
1716 card->dev = &pdev->dev;
1718 return snd_soc_register_card(card);
1721 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1725 /* make sure any delayed work runs */
1726 for (i = 0; i < card->num_rtd; i++) {
1727 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1728 flush_delayed_work(&rtd->delayed_work);
1731 /* remove auxiliary devices */
1732 for (i = 0; i < card->num_aux_devs; i++)
1733 soc_remove_aux_dev(card, i);
1735 /* remove and free each DAI */
1736 soc_remove_dai_links(card);
1738 soc_cleanup_card_debugfs(card);
1740 /* remove the card */
1744 snd_soc_dapm_free(&card->dapm);
1746 snd_card_free(card->snd_card);
1751 /* removes a socdev */
1752 static int soc_remove(struct platform_device *pdev)
1754 struct snd_soc_card *card = platform_get_drvdata(pdev);
1756 snd_soc_unregister_card(card);
1760 int snd_soc_poweroff(struct device *dev)
1762 struct snd_soc_card *card = dev_get_drvdata(dev);
1765 if (!card->instantiated)
1768 /* Flush out pmdown_time work - we actually do want to run it
1769 * now, we're shutting down so no imminent restart. */
1770 for (i = 0; i < card->num_rtd; i++) {
1771 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1772 flush_delayed_work(&rtd->delayed_work);
1775 snd_soc_dapm_shutdown(card);
1777 /* deactivate pins to sleep state */
1778 for (i = 0; i < card->num_rtd; i++) {
1779 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1780 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1783 pinctrl_pm_select_sleep_state(cpu_dai->dev);
1784 for (j = 0; j < rtd->num_codecs; j++) {
1785 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
1786 pinctrl_pm_select_sleep_state(codec_dai->dev);
1792 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1794 const struct dev_pm_ops snd_soc_pm_ops = {
1795 .suspend = snd_soc_suspend,
1796 .resume = snd_soc_resume,
1797 .freeze = snd_soc_suspend,
1798 .thaw = snd_soc_resume,
1799 .poweroff = snd_soc_poweroff,
1800 .restore = snd_soc_resume,
1802 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1804 /* ASoC platform driver */
1805 static struct platform_driver soc_driver = {
1807 .name = "soc-audio",
1808 .owner = THIS_MODULE,
1809 .pm = &snd_soc_pm_ops,
1812 .remove = soc_remove,
1816 * snd_soc_cnew - create new control
1817 * @_template: control template
1818 * @data: control private data
1819 * @long_name: control long name
1820 * @prefix: control name prefix
1822 * Create a new mixer control from a template control.
1824 * Returns 0 for success, else error.
1826 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1827 void *data, const char *long_name,
1830 struct snd_kcontrol_new template;
1831 struct snd_kcontrol *kcontrol;
1834 memcpy(&template, _template, sizeof(template));
1838 long_name = template.name;
1841 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
1845 template.name = name;
1847 template.name = long_name;
1850 kcontrol = snd_ctl_new1(&template, data);
1856 EXPORT_SYMBOL_GPL(snd_soc_cnew);
1858 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
1859 const struct snd_kcontrol_new *controls, int num_controls,
1860 const char *prefix, void *data)
1864 for (i = 0; i < num_controls; i++) {
1865 const struct snd_kcontrol_new *control = &controls[i];
1866 err = snd_ctl_add(card, snd_soc_cnew(control, data,
1867 control->name, prefix));
1869 dev_err(dev, "ASoC: Failed to add %s: %d\n",
1870 control->name, err);
1878 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
1881 struct snd_card *card = soc_card->snd_card;
1882 struct snd_kcontrol *kctl;
1884 if (unlikely(!name))
1887 list_for_each_entry(kctl, &card->controls, list)
1888 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
1892 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
1895 * snd_soc_add_component_controls - Add an array of controls to a component.
1897 * @component: Component to add controls to
1898 * @controls: Array of controls to add
1899 * @num_controls: Number of elements in the array
1901 * Return: 0 for success, else error.
1903 int snd_soc_add_component_controls(struct snd_soc_component *component,
1904 const struct snd_kcontrol_new *controls, unsigned int num_controls)
1906 struct snd_card *card = component->card->snd_card;
1908 return snd_soc_add_controls(card, component->dev, controls,
1909 num_controls, component->name_prefix, component);
1911 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
1914 * snd_soc_add_codec_controls - add an array of controls to a codec.
1915 * Convenience function to add a list of controls. Many codecs were
1916 * duplicating this code.
1918 * @codec: codec to add controls to
1919 * @controls: array of controls to add
1920 * @num_controls: number of elements in the array
1922 * Return 0 for success, else error.
1924 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
1925 const struct snd_kcontrol_new *controls, unsigned int num_controls)
1927 return snd_soc_add_component_controls(&codec->component, controls,
1930 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
1933 * snd_soc_add_platform_controls - add an array of controls to a platform.
1934 * Convenience function to add a list of controls.
1936 * @platform: platform to add controls to
1937 * @controls: array of controls to add
1938 * @num_controls: number of elements in the array
1940 * Return 0 for success, else error.
1942 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
1943 const struct snd_kcontrol_new *controls, unsigned int num_controls)
1945 return snd_soc_add_component_controls(&platform->component, controls,
1948 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
1951 * snd_soc_add_card_controls - add an array of controls to a SoC card.
1952 * Convenience function to add a list of controls.
1954 * @soc_card: SoC card to add controls to
1955 * @controls: array of controls to add
1956 * @num_controls: number of elements in the array
1958 * Return 0 for success, else error.
1960 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
1961 const struct snd_kcontrol_new *controls, int num_controls)
1963 struct snd_card *card = soc_card->snd_card;
1965 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
1968 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
1971 * snd_soc_add_dai_controls - add an array of controls to a DAI.
1972 * Convienience function to add a list of controls.
1974 * @dai: DAI to add controls to
1975 * @controls: array of controls to add
1976 * @num_controls: number of elements in the array
1978 * Return 0 for success, else error.
1980 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
1981 const struct snd_kcontrol_new *controls, int num_controls)
1983 struct snd_card *card = dai->card->snd_card;
1985 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
1988 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
1991 * snd_soc_info_enum_double - enumerated double mixer info callback
1992 * @kcontrol: mixer control
1993 * @uinfo: control element information
1995 * Callback to provide information about a double enumerated
1998 * Returns 0 for success.
2000 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2001 struct snd_ctl_elem_info *uinfo)
2003 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2005 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2006 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2007 uinfo->value.enumerated.items = e->items;
2009 if (uinfo->value.enumerated.item >= e->items)
2010 uinfo->value.enumerated.item = e->items - 1;
2011 strlcpy(uinfo->value.enumerated.name,
2012 e->texts[uinfo->value.enumerated.item],
2013 sizeof(uinfo->value.enumerated.name));
2016 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2019 * snd_soc_get_enum_double - enumerated double mixer get callback
2020 * @kcontrol: mixer control
2021 * @ucontrol: control element information
2023 * Callback to get the value of a double enumerated mixer.
2025 * Returns 0 for success.
2027 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2028 struct snd_ctl_elem_value *ucontrol)
2030 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2031 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2032 unsigned int val, item;
2033 unsigned int reg_val;
2036 ret = snd_soc_component_read(component, e->reg, ®_val);
2039 val = (reg_val >> e->shift_l) & e->mask;
2040 item = snd_soc_enum_val_to_item(e, val);
2041 ucontrol->value.enumerated.item[0] = item;
2042 if (e->shift_l != e->shift_r) {
2043 val = (reg_val >> e->shift_l) & e->mask;
2044 item = snd_soc_enum_val_to_item(e, val);
2045 ucontrol->value.enumerated.item[1] = item;
2050 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2053 * snd_soc_put_enum_double - enumerated double mixer put callback
2054 * @kcontrol: mixer control
2055 * @ucontrol: control element information
2057 * Callback to set the value of a double enumerated mixer.
2059 * Returns 0 for success.
2061 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2062 struct snd_ctl_elem_value *ucontrol)
2064 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2065 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2066 unsigned int *item = ucontrol->value.enumerated.item;
2070 if (item[0] >= e->items)
2072 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
2073 mask = e->mask << e->shift_l;
2074 if (e->shift_l != e->shift_r) {
2075 if (item[1] >= e->items)
2077 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
2078 mask |= e->mask << e->shift_r;
2081 return snd_soc_component_update_bits(component, e->reg, mask, val);
2083 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2086 * snd_soc_read_signed - Read a codec register and interprete as signed value
2087 * @component: component
2088 * @reg: Register to read
2089 * @mask: Mask to use after shifting the register value
2090 * @shift: Right shift of register value
2091 * @sign_bit: Bit that describes if a number is negative or not.
2092 * @signed_val: Pointer to where the read value should be stored
2094 * This functions reads a codec register. The register value is shifted right
2095 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2096 * the given registervalue into a signed integer if sign_bit is non-zero.
2098 * Returns 0 on sucess, otherwise an error value
2100 static int snd_soc_read_signed(struct snd_soc_component *component,
2101 unsigned int reg, unsigned int mask, unsigned int shift,
2102 unsigned int sign_bit, int *signed_val)
2107 ret = snd_soc_component_read(component, reg, &val);
2111 val = (val >> shift) & mask;
2118 /* non-negative number */
2119 if (!(val & BIT(sign_bit))) {
2127 * The register most probably does not contain a full-sized int.
2128 * Instead we have an arbitrary number of bits in a signed
2129 * representation which has to be translated into a full-sized int.
2130 * This is done by filling up all bits above the sign-bit.
2132 ret |= ~((int)(BIT(sign_bit) - 1));
2140 * snd_soc_info_volsw - single mixer info callback
2141 * @kcontrol: mixer control
2142 * @uinfo: control element information
2144 * Callback to provide information about a single mixer control, or a double
2145 * mixer control that spans 2 registers.
2147 * Returns 0 for success.
2149 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2150 struct snd_ctl_elem_info *uinfo)
2152 struct soc_mixer_control *mc =
2153 (struct soc_mixer_control *)kcontrol->private_value;
2156 if (!mc->platform_max)
2157 mc->platform_max = mc->max;
2158 platform_max = mc->platform_max;
2160 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2161 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2163 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2165 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2166 uinfo->value.integer.min = 0;
2167 uinfo->value.integer.max = platform_max - mc->min;
2170 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2173 * snd_soc_get_volsw - single mixer get callback
2174 * @kcontrol: mixer control
2175 * @ucontrol: control element information
2177 * Callback to get the value of a single mixer control, or a double mixer
2178 * control that spans 2 registers.
2180 * Returns 0 for success.
2182 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2183 struct snd_ctl_elem_value *ucontrol)
2185 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2186 struct soc_mixer_control *mc =
2187 (struct soc_mixer_control *)kcontrol->private_value;
2188 unsigned int reg = mc->reg;
2189 unsigned int reg2 = mc->rreg;
2190 unsigned int shift = mc->shift;
2191 unsigned int rshift = mc->rshift;
2194 int sign_bit = mc->sign_bit;
2195 unsigned int mask = (1 << fls(max)) - 1;
2196 unsigned int invert = mc->invert;
2201 mask = BIT(sign_bit + 1) - 1;
2203 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2207 ucontrol->value.integer.value[0] = val - min;
2209 ucontrol->value.integer.value[0] =
2210 max - ucontrol->value.integer.value[0];
2212 if (snd_soc_volsw_is_stereo(mc)) {
2214 ret = snd_soc_read_signed(component, reg, mask, rshift,
2217 ret = snd_soc_read_signed(component, reg2, mask, shift,
2222 ucontrol->value.integer.value[1] = val - min;
2224 ucontrol->value.integer.value[1] =
2225 max - ucontrol->value.integer.value[1];
2230 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2233 * snd_soc_put_volsw - single mixer put callback
2234 * @kcontrol: mixer control
2235 * @ucontrol: control element information
2237 * Callback to set the value of a single mixer control, or a double mixer
2238 * control that spans 2 registers.
2240 * Returns 0 for success.
2242 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2243 struct snd_ctl_elem_value *ucontrol)
2245 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2246 struct soc_mixer_control *mc =
2247 (struct soc_mixer_control *)kcontrol->private_value;
2248 unsigned int reg = mc->reg;
2249 unsigned int reg2 = mc->rreg;
2250 unsigned int shift = mc->shift;
2251 unsigned int rshift = mc->rshift;
2254 unsigned int sign_bit = mc->sign_bit;
2255 unsigned int mask = (1 << fls(max)) - 1;
2256 unsigned int invert = mc->invert;
2258 bool type_2r = false;
2259 unsigned int val2 = 0;
2260 unsigned int val, val_mask;
2263 mask = BIT(sign_bit + 1) - 1;
2265 val = ((ucontrol->value.integer.value[0] + min) & mask);
2268 val_mask = mask << shift;
2270 if (snd_soc_volsw_is_stereo(mc)) {
2271 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
2275 val_mask |= mask << rshift;
2276 val |= val2 << rshift;
2278 val2 = val2 << shift;
2282 err = snd_soc_component_update_bits(component, reg, val_mask, val);
2287 err = snd_soc_component_update_bits(component, reg2, val_mask,
2292 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2295 * snd_soc_get_volsw_sx - single mixer get callback
2296 * @kcontrol: mixer control
2297 * @ucontrol: control element information
2299 * Callback to get the value of a single mixer control, or a double mixer
2300 * control that spans 2 registers.
2302 * Returns 0 for success.
2304 int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2305 struct snd_ctl_elem_value *ucontrol)
2307 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2308 struct soc_mixer_control *mc =
2309 (struct soc_mixer_control *)kcontrol->private_value;
2310 unsigned int reg = mc->reg;
2311 unsigned int reg2 = mc->rreg;
2312 unsigned int shift = mc->shift;
2313 unsigned int rshift = mc->rshift;
2316 int mask = (1 << (fls(min + max) - 1)) - 1;
2320 ret = snd_soc_component_read(component, reg, &val);
2324 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2326 if (snd_soc_volsw_is_stereo(mc)) {
2327 ret = snd_soc_component_read(component, reg2, &val);
2331 val = ((val >> rshift) - min) & mask;
2332 ucontrol->value.integer.value[1] = val;
2337 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2340 * snd_soc_put_volsw_sx - double mixer set callback
2341 * @kcontrol: mixer control
2342 * @uinfo: control element information
2344 * Callback to set the value of a double mixer control that spans 2 registers.
2346 * Returns 0 for success.
2348 int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2349 struct snd_ctl_elem_value *ucontrol)
2351 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2352 struct soc_mixer_control *mc =
2353 (struct soc_mixer_control *)kcontrol->private_value;
2355 unsigned int reg = mc->reg;
2356 unsigned int reg2 = mc->rreg;
2357 unsigned int shift = mc->shift;
2358 unsigned int rshift = mc->rshift;
2361 int mask = (1 << (fls(min + max) - 1)) - 1;
2363 unsigned int val, val_mask, val2 = 0;
2365 val_mask = mask << shift;
2366 val = (ucontrol->value.integer.value[0] + min) & mask;
2369 err = snd_soc_component_update_bits(component, reg, val_mask, val);
2373 if (snd_soc_volsw_is_stereo(mc)) {
2374 val_mask = mask << rshift;
2375 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2376 val2 = val2 << rshift;
2378 err = snd_soc_component_update_bits(component, reg2, val_mask,
2383 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2386 * snd_soc_info_volsw_s8 - signed mixer info callback
2387 * @kcontrol: mixer control
2388 * @uinfo: control element information
2390 * Callback to provide information about a signed mixer control.
2392 * Returns 0 for success.
2394 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2395 struct snd_ctl_elem_info *uinfo)
2397 struct soc_mixer_control *mc =
2398 (struct soc_mixer_control *)kcontrol->private_value;
2402 if (!mc->platform_max)
2403 mc->platform_max = mc->max;
2404 platform_max = mc->platform_max;
2406 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2408 uinfo->value.integer.min = 0;
2409 uinfo->value.integer.max = platform_max - min;
2412 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2415 * snd_soc_get_volsw_s8 - signed mixer get callback
2416 * @kcontrol: mixer control
2417 * @ucontrol: control element information
2419 * Callback to get the value of a signed mixer control.
2421 * Returns 0 for success.
2423 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2424 struct snd_ctl_elem_value *ucontrol)
2426 struct soc_mixer_control *mc =
2427 (struct soc_mixer_control *)kcontrol->private_value;
2428 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2429 unsigned int reg = mc->reg;
2434 ret = snd_soc_component_read(component, reg, &val);
2438 ucontrol->value.integer.value[0] =
2439 ((signed char)(val & 0xff))-min;
2440 ucontrol->value.integer.value[1] =
2441 ((signed char)((val >> 8) & 0xff))-min;
2444 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2447 * snd_soc_put_volsw_sgn - signed mixer put callback
2448 * @kcontrol: mixer control
2449 * @ucontrol: control element information
2451 * Callback to set the value of a signed mixer control.
2453 * Returns 0 for success.
2455 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2456 struct snd_ctl_elem_value *ucontrol)
2458 struct soc_mixer_control *mc =
2459 (struct soc_mixer_control *)kcontrol->private_value;
2460 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2461 unsigned int reg = mc->reg;
2465 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2466 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2468 return snd_soc_component_update_bits(component, reg, 0xffff, val);
2470 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2473 * snd_soc_info_volsw_range - single mixer info callback with range.
2474 * @kcontrol: mixer control
2475 * @uinfo: control element information
2477 * Callback to provide information, within a range, about a single
2480 * returns 0 for success.
2482 int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2483 struct snd_ctl_elem_info *uinfo)
2485 struct soc_mixer_control *mc =
2486 (struct soc_mixer_control *)kcontrol->private_value;
2490 if (!mc->platform_max)
2491 mc->platform_max = mc->max;
2492 platform_max = mc->platform_max;
2494 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2495 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2496 uinfo->value.integer.min = 0;
2497 uinfo->value.integer.max = platform_max - min;
2501 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2504 * snd_soc_put_volsw_range - single mixer put value callback with range.
2505 * @kcontrol: mixer control
2506 * @ucontrol: control element information
2508 * Callback to set the value, within a range, for a single mixer control.
2510 * Returns 0 for success.
2512 int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2513 struct snd_ctl_elem_value *ucontrol)
2515 struct soc_mixer_control *mc =
2516 (struct soc_mixer_control *)kcontrol->private_value;
2517 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2518 unsigned int reg = mc->reg;
2519 unsigned int rreg = mc->rreg;
2520 unsigned int shift = mc->shift;
2523 unsigned int mask = (1 << fls(max)) - 1;
2524 unsigned int invert = mc->invert;
2525 unsigned int val, val_mask;
2529 val = (max - ucontrol->value.integer.value[0]) & mask;
2531 val = ((ucontrol->value.integer.value[0] + min) & mask);
2532 val_mask = mask << shift;
2535 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
2539 if (snd_soc_volsw_is_stereo(mc)) {
2541 val = (max - ucontrol->value.integer.value[1]) & mask;
2543 val = ((ucontrol->value.integer.value[1] + min) & mask);
2544 val_mask = mask << shift;
2547 ret = snd_soc_component_update_bits(component, rreg, val_mask,
2553 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2556 * snd_soc_get_volsw_range - single mixer get callback with range
2557 * @kcontrol: mixer control
2558 * @ucontrol: control element information
2560 * Callback to get the value, within a range, of a single mixer control.
2562 * Returns 0 for success.
2564 int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2565 struct snd_ctl_elem_value *ucontrol)
2567 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2568 struct soc_mixer_control *mc =
2569 (struct soc_mixer_control *)kcontrol->private_value;
2570 unsigned int reg = mc->reg;
2571 unsigned int rreg = mc->rreg;
2572 unsigned int shift = mc->shift;
2575 unsigned int mask = (1 << fls(max)) - 1;
2576 unsigned int invert = mc->invert;
2580 ret = snd_soc_component_read(component, reg, &val);
2584 ucontrol->value.integer.value[0] = (val >> shift) & mask;
2586 ucontrol->value.integer.value[0] =
2587 max - ucontrol->value.integer.value[0];
2589 ucontrol->value.integer.value[0] =
2590 ucontrol->value.integer.value[0] - min;
2592 if (snd_soc_volsw_is_stereo(mc)) {
2593 ret = snd_soc_component_read(component, rreg, &val);
2597 ucontrol->value.integer.value[1] = (val >> shift) & mask;
2599 ucontrol->value.integer.value[1] =
2600 max - ucontrol->value.integer.value[1];
2602 ucontrol->value.integer.value[1] =
2603 ucontrol->value.integer.value[1] - min;
2608 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
2611 * snd_soc_limit_volume - Set new limit to an existing volume control.
2613 * @codec: where to look for the control
2614 * @name: Name of the control
2615 * @max: new maximum limit
2617 * Return 0 for success, else error.
2619 int snd_soc_limit_volume(struct snd_soc_codec *codec,
2620 const char *name, int max)
2622 struct snd_card *card = codec->component.card->snd_card;
2623 struct snd_kcontrol *kctl;
2624 struct soc_mixer_control *mc;
2628 /* Sanity check for name and max */
2629 if (unlikely(!name || max <= 0))
2632 list_for_each_entry(kctl, &card->controls, list) {
2633 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2639 mc = (struct soc_mixer_control *)kctl->private_value;
2640 if (max <= mc->max) {
2641 mc->platform_max = max;
2647 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2649 int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
2650 struct snd_ctl_elem_info *uinfo)
2652 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2653 struct soc_bytes *params = (void *)kcontrol->private_value;
2655 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
2656 uinfo->count = params->num_regs * component->val_bytes;
2660 EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
2662 int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
2663 struct snd_ctl_elem_value *ucontrol)
2665 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2666 struct soc_bytes *params = (void *)kcontrol->private_value;
2669 if (component->regmap)
2670 ret = regmap_raw_read(component->regmap, params->base,
2671 ucontrol->value.bytes.data,
2672 params->num_regs * component->val_bytes);
2676 /* Hide any masked bytes to ensure consistent data reporting */
2677 if (ret == 0 && params->mask) {
2678 switch (component->val_bytes) {
2680 ucontrol->value.bytes.data[0] &= ~params->mask;
2683 ((u16 *)(&ucontrol->value.bytes.data))[0]
2684 &= cpu_to_be16(~params->mask);
2687 ((u32 *)(&ucontrol->value.bytes.data))[0]
2688 &= cpu_to_be32(~params->mask);
2697 EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
2699 int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
2700 struct snd_ctl_elem_value *ucontrol)
2702 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2703 struct soc_bytes *params = (void *)kcontrol->private_value;
2705 unsigned int val, mask;
2708 if (!component->regmap || !params->num_regs)
2711 len = params->num_regs * component->val_bytes;
2713 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
2718 * If we've got a mask then we need to preserve the register
2719 * bits. We shouldn't modify the incoming data so take a
2723 ret = regmap_read(component->regmap, params->base, &val);
2727 val &= params->mask;
2729 switch (component->val_bytes) {
2731 ((u8 *)data)[0] &= ~params->mask;
2732 ((u8 *)data)[0] |= val;
2735 mask = ~params->mask;
2736 ret = regmap_parse_val(component->regmap,
2741 ((u16 *)data)[0] &= mask;
2743 ret = regmap_parse_val(component->regmap,
2748 ((u16 *)data)[0] |= val;
2751 mask = ~params->mask;
2752 ret = regmap_parse_val(component->regmap,
2757 ((u32 *)data)[0] &= mask;
2759 ret = regmap_parse_val(component->regmap,
2764 ((u32 *)data)[0] |= val;
2772 ret = regmap_raw_write(component->regmap, params->base,
2780 EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
2782 int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
2783 struct snd_ctl_elem_info *ucontrol)
2785 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
2787 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
2788 ucontrol->count = params->max;
2792 EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
2794 int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
2795 unsigned int size, unsigned int __user *tlv)
2797 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
2798 unsigned int count = size < params->max ? size : params->max;
2802 case SNDRV_CTL_TLV_OP_READ:
2804 ret = params->get(tlv, count);
2806 case SNDRV_CTL_TLV_OP_WRITE:
2808 ret = params->put(tlv, count);
2813 EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
2816 * snd_soc_info_xr_sx - signed multi register info callback
2817 * @kcontrol: mreg control
2818 * @uinfo: control element information
2820 * Callback to provide information of a control that can
2821 * span multiple codec registers which together
2822 * forms a single signed value in a MSB/LSB manner.
2824 * Returns 0 for success.
2826 int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
2827 struct snd_ctl_elem_info *uinfo)
2829 struct soc_mreg_control *mc =
2830 (struct soc_mreg_control *)kcontrol->private_value;
2831 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2833 uinfo->value.integer.min = mc->min;
2834 uinfo->value.integer.max = mc->max;
2838 EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
2841 * snd_soc_get_xr_sx - signed multi register get callback
2842 * @kcontrol: mreg control
2843 * @ucontrol: control element information
2845 * Callback to get the value of a control that can span
2846 * multiple codec registers which together forms a single
2847 * signed value in a MSB/LSB manner. The control supports
2848 * specifying total no of bits used to allow for bitfields
2849 * across the multiple codec registers.
2851 * Returns 0 for success.
2853 int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
2854 struct snd_ctl_elem_value *ucontrol)
2856 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2857 struct soc_mreg_control *mc =
2858 (struct soc_mreg_control *)kcontrol->private_value;
2859 unsigned int regbase = mc->regbase;
2860 unsigned int regcount = mc->regcount;
2861 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
2862 unsigned int regwmask = (1<<regwshift)-1;
2863 unsigned int invert = mc->invert;
2864 unsigned long mask = (1UL<<mc->nbits)-1;
2868 unsigned int regval;
2872 for (i = 0; i < regcount; i++) {
2873 ret = snd_soc_component_read(component, regbase+i, ®val);
2876 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
2879 if (min < 0 && val > max)
2883 ucontrol->value.integer.value[0] = val;
2887 EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
2890 * snd_soc_put_xr_sx - signed multi register get callback
2891 * @kcontrol: mreg control
2892 * @ucontrol: control element information
2894 * Callback to set the value of a control that can span
2895 * multiple codec registers which together forms a single
2896 * signed value in a MSB/LSB manner. The control supports
2897 * specifying total no of bits used to allow for bitfields
2898 * across the multiple codec registers.
2900 * Returns 0 for success.
2902 int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
2903 struct snd_ctl_elem_value *ucontrol)
2905 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2906 struct soc_mreg_control *mc =
2907 (struct soc_mreg_control *)kcontrol->private_value;
2908 unsigned int regbase = mc->regbase;
2909 unsigned int regcount = mc->regcount;
2910 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
2911 unsigned int regwmask = (1<<regwshift)-1;
2912 unsigned int invert = mc->invert;
2913 unsigned long mask = (1UL<<mc->nbits)-1;
2915 long val = ucontrol->value.integer.value[0];
2916 unsigned int i, regval, regmask;
2922 for (i = 0; i < regcount; i++) {
2923 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
2924 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
2925 err = snd_soc_component_update_bits(component, regbase+i,
2933 EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
2936 * snd_soc_get_strobe - strobe get callback
2937 * @kcontrol: mixer control
2938 * @ucontrol: control element information
2940 * Callback get the value of a strobe mixer control.
2942 * Returns 0 for success.
2944 int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
2945 struct snd_ctl_elem_value *ucontrol)
2947 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2948 struct soc_mixer_control *mc =
2949 (struct soc_mixer_control *)kcontrol->private_value;
2950 unsigned int reg = mc->reg;
2951 unsigned int shift = mc->shift;
2952 unsigned int mask = 1 << shift;
2953 unsigned int invert = mc->invert != 0;
2957 ret = snd_soc_component_read(component, reg, &val);
2963 if (shift != 0 && val != 0)
2965 ucontrol->value.enumerated.item[0] = val ^ invert;
2969 EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
2972 * snd_soc_put_strobe - strobe put callback
2973 * @kcontrol: mixer control
2974 * @ucontrol: control element information
2976 * Callback strobe a register bit to high then low (or the inverse)
2977 * in one pass of a single mixer enum control.
2979 * Returns 1 for success.
2981 int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
2982 struct snd_ctl_elem_value *ucontrol)
2984 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2985 struct soc_mixer_control *mc =
2986 (struct soc_mixer_control *)kcontrol->private_value;
2987 unsigned int reg = mc->reg;
2988 unsigned int shift = mc->shift;
2989 unsigned int mask = 1 << shift;
2990 unsigned int invert = mc->invert != 0;
2991 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
2992 unsigned int val1 = (strobe ^ invert) ? mask : 0;
2993 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
2996 err = snd_soc_component_update_bits(component, reg, mask, val1);
3000 return snd_soc_component_update_bits(component, reg, mask, val2);
3002 EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3005 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3007 * @clk_id: DAI specific clock ID
3008 * @freq: new clock frequency in Hz
3009 * @dir: new clock direction - input/output.
3011 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3013 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3014 unsigned int freq, int dir)
3016 if (dai->driver && dai->driver->ops->set_sysclk)
3017 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3018 else if (dai->codec && dai->codec->driver->set_sysclk)
3019 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3024 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3027 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3029 * @clk_id: DAI specific clock ID
3030 * @source: Source for the clock
3031 * @freq: new clock frequency in Hz
3032 * @dir: new clock direction - input/output.
3034 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3036 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3037 int source, unsigned int freq, int dir)
3039 if (codec->driver->set_sysclk)
3040 return codec->driver->set_sysclk(codec, clk_id, source,
3045 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3048 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3050 * @div_id: DAI specific clock divider ID
3051 * @div: new clock divisor.
3053 * Configures the clock dividers. This is used to derive the best DAI bit and
3054 * frame clocks from the system or master clock. It's best to set the DAI bit
3055 * and frame clocks as low as possible to save system power.
3057 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3058 int div_id, int div)
3060 if (dai->driver && dai->driver->ops->set_clkdiv)
3061 return dai->driver->ops->set_clkdiv(dai, div_id, div);
3065 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3068 * snd_soc_dai_set_pll - configure DAI PLL.
3070 * @pll_id: DAI specific PLL ID
3071 * @source: DAI specific source for the PLL
3072 * @freq_in: PLL input clock frequency in Hz
3073 * @freq_out: requested PLL output clock frequency in Hz
3075 * Configures and enables PLL to generate output clock based on input clock.
3077 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3078 unsigned int freq_in, unsigned int freq_out)
3080 if (dai->driver && dai->driver->ops->set_pll)
3081 return dai->driver->ops->set_pll(dai, pll_id, source,
3083 else if (dai->codec && dai->codec->driver->set_pll)
3084 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3089 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3092 * snd_soc_codec_set_pll - configure codec PLL.
3094 * @pll_id: DAI specific PLL ID
3095 * @source: DAI specific source for the PLL
3096 * @freq_in: PLL input clock frequency in Hz
3097 * @freq_out: requested PLL output clock frequency in Hz
3099 * Configures and enables PLL to generate output clock based on input clock.
3101 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3102 unsigned int freq_in, unsigned int freq_out)
3104 if (codec->driver->set_pll)
3105 return codec->driver->set_pll(codec, pll_id, source,
3110 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3113 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3115 * @ratio Ratio of BCLK to Sample rate.
3117 * Configures the DAI for a preset BCLK to sample rate ratio.
3119 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3121 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3122 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3126 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3129 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3131 * @fmt: SND_SOC_DAIFMT_ format value.
3133 * Configures the DAI hardware format and clocking.
3135 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3137 if (dai->driver == NULL)
3139 if (dai->driver->ops->set_fmt == NULL)
3141 return dai->driver->ops->set_fmt(dai, fmt);
3143 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3146 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
3147 * @slots: Number of slots in use.
3148 * @tx_mask: bitmask representing active TX slots.
3149 * @rx_mask: bitmask representing active RX slots.
3151 * Generates the TDM tx and rx slot default masks for DAI.
3153 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
3154 unsigned int *tx_mask,
3155 unsigned int *rx_mask)
3157 if (*tx_mask || *rx_mask)
3163 *tx_mask = (1 << slots) - 1;
3164 *rx_mask = (1 << slots) - 1;
3170 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3172 * @tx_mask: bitmask representing active TX slots.
3173 * @rx_mask: bitmask representing active RX slots.
3174 * @slots: Number of slots in use.
3175 * @slot_width: Width in bits for each slot.
3177 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3180 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3181 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
3183 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3184 dai->driver->ops->xlate_tdm_slot_mask(slots,
3185 &tx_mask, &rx_mask);
3187 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
3189 dai->tx_mask = tx_mask;
3190 dai->rx_mask = rx_mask;
3192 if (dai->driver && dai->driver->ops->set_tdm_slot)
3193 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3198 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3201 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3203 * @tx_num: how many TX channels
3204 * @tx_slot: pointer to an array which imply the TX slot number channel
3206 * @rx_num: how many RX channels
3207 * @rx_slot: pointer to an array which imply the RX slot number channel
3210 * configure the relationship between channel number and TDM slot number.
3212 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3213 unsigned int tx_num, unsigned int *tx_slot,
3214 unsigned int rx_num, unsigned int *rx_slot)
3216 if (dai->driver && dai->driver->ops->set_channel_map)
3217 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3222 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3225 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3227 * @tristate: tristate enable
3229 * Tristates the DAI so that others can use it.
3231 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3233 if (dai->driver && dai->driver->ops->set_tristate)
3234 return dai->driver->ops->set_tristate(dai, tristate);
3238 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3241 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3243 * @mute: mute enable
3244 * @direction: stream to mute
3246 * Mutes the DAI DAC.
3248 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3254 if (dai->driver->ops->mute_stream)
3255 return dai->driver->ops->mute_stream(dai, mute, direction);
3256 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3257 dai->driver->ops->digital_mute)
3258 return dai->driver->ops->digital_mute(dai, mute);
3262 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3264 static int snd_soc_init_multicodec(struct snd_soc_card *card,
3265 struct snd_soc_dai_link *dai_link)
3267 /* Legacy codec/codec_dai link is a single entry in multicodec */
3268 if (dai_link->codec_name || dai_link->codec_of_node ||
3269 dai_link->codec_dai_name) {
3270 dai_link->num_codecs = 1;
3272 dai_link->codecs = devm_kzalloc(card->dev,
3273 sizeof(struct snd_soc_dai_link_component),
3275 if (!dai_link->codecs)
3278 dai_link->codecs[0].name = dai_link->codec_name;
3279 dai_link->codecs[0].of_node = dai_link->codec_of_node;
3280 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
3283 if (!dai_link->codecs) {
3284 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
3292 * snd_soc_register_card - Register a card with the ASoC core
3294 * @card: Card to register
3297 int snd_soc_register_card(struct snd_soc_card *card)
3301 if (!card->name || !card->dev)
3304 for (i = 0; i < card->num_links; i++) {
3305 struct snd_soc_dai_link *link = &card->dai_link[i];
3307 ret = snd_soc_init_multicodec(card, link);
3309 dev_err(card->dev, "ASoC: failed to init multicodec\n");
3313 for (j = 0; j < link->num_codecs; j++) {
3315 * Codec must be specified by 1 of name or OF node,
3316 * not both or neither.
3318 if (!!link->codecs[j].name ==
3319 !!link->codecs[j].of_node) {
3320 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
3324 /* Codec DAI name must be specified */
3325 if (!link->codecs[j].dai_name) {
3326 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
3333 * Platform may be specified by either name or OF node, but
3334 * can be left unspecified, and a dummy platform will be used.
3336 if (link->platform_name && link->platform_of_node) {
3338 "ASoC: Both platform name/of_node are set for %s\n",
3344 * CPU device may be specified by either name or OF node, but
3345 * can be left unspecified, and will be matched based on DAI
3348 if (link->cpu_name && link->cpu_of_node) {
3350 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3355 * At least one of CPU DAI name or CPU device name/node must be
3358 if (!link->cpu_dai_name &&
3359 !(link->cpu_name || link->cpu_of_node)) {
3361 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3367 dev_set_drvdata(card->dev, card);
3369 snd_soc_initialize_card_lists(card);
3371 soc_init_card_debugfs(card);
3373 card->rtd = devm_kzalloc(card->dev,
3374 sizeof(struct snd_soc_pcm_runtime) *
3375 (card->num_links + card->num_aux_devs),
3377 if (card->rtd == NULL)
3380 card->rtd_aux = &card->rtd[card->num_links];
3382 for (i = 0; i < card->num_links; i++) {
3383 card->rtd[i].card = card;
3384 card->rtd[i].dai_link = &card->dai_link[i];
3385 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
3386 sizeof(struct snd_soc_dai *) *
3387 (card->rtd[i].dai_link->num_codecs),
3389 if (card->rtd[i].codec_dais == NULL)
3393 for (i = 0; i < card->num_aux_devs; i++)
3394 card->rtd_aux[i].card = card;
3396 INIT_LIST_HEAD(&card->dapm_dirty);
3397 card->instantiated = 0;
3398 mutex_init(&card->mutex);
3399 mutex_init(&card->dapm_mutex);
3401 ret = snd_soc_instantiate_card(card);
3403 soc_cleanup_card_debugfs(card);
3405 /* deactivate pins to sleep state */
3406 for (i = 0; i < card->num_rtd; i++) {
3407 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
3408 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3411 for (j = 0; j < rtd->num_codecs; j++) {
3412 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
3413 if (!codec_dai->active)
3414 pinctrl_pm_select_sleep_state(codec_dai->dev);
3417 if (!cpu_dai->active)
3418 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3423 EXPORT_SYMBOL_GPL(snd_soc_register_card);
3426 * snd_soc_unregister_card - Unregister a card with the ASoC core
3428 * @card: Card to unregister
3431 int snd_soc_unregister_card(struct snd_soc_card *card)
3433 if (card->instantiated) {
3434 card->instantiated = false;
3435 snd_soc_dapm_shutdown(card);
3436 soc_cleanup_card_resources(card);
3438 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
3442 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3445 * Simplify DAI link configuration by removing ".-1" from device names
3446 * and sanitizing names.
3448 static char *fmt_single_name(struct device *dev, int *id)
3450 char *found, name[NAME_SIZE];
3453 if (dev_name(dev) == NULL)
3456 strlcpy(name, dev_name(dev), NAME_SIZE);
3458 /* are we a "%s.%d" name (platform and SPI components) */
3459 found = strstr(name, dev->driver->name);
3462 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3464 /* discard ID from name if ID == -1 */
3466 found[strlen(dev->driver->name)] = '\0';
3470 /* I2C component devices are named "bus-addr" */
3471 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3472 char tmp[NAME_SIZE];
3474 /* create unique ID number from I2C addr and bus */
3475 *id = ((id1 & 0xffff) << 16) + id2;
3477 /* sanitize component name for DAI link creation */
3478 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3479 strlcpy(name, tmp, NAME_SIZE);
3484 return kstrdup(name, GFP_KERNEL);
3488 * Simplify DAI link naming for single devices with multiple DAIs by removing
3489 * any ".-1" and using the DAI name (instead of device name).
3491 static inline char *fmt_multiple_name(struct device *dev,
3492 struct snd_soc_dai_driver *dai_drv)
3494 if (dai_drv->name == NULL) {
3496 "ASoC: error - multiple DAI %s registered with no name\n",
3501 return kstrdup(dai_drv->name, GFP_KERNEL);
3505 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
3507 * @component: The component for which the DAIs should be unregistered
3509 static void snd_soc_unregister_dais(struct snd_soc_component *component)
3511 struct snd_soc_dai *dai, *_dai;
3513 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
3514 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3516 list_del(&dai->list);
3523 * snd_soc_register_dais - Register a DAI with the ASoC core
3525 * @component: The component the DAIs are registered for
3526 * @dai_drv: DAI driver to use for the DAIs
3527 * @count: Number of DAIs
3528 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3531 static int snd_soc_register_dais(struct snd_soc_component *component,
3532 struct snd_soc_dai_driver *dai_drv, size_t count,
3533 bool legacy_dai_naming)
3535 struct device *dev = component->dev;
3536 struct snd_soc_dai *dai;
3540 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
3542 component->dai_drv = dai_drv;
3543 component->num_dai = count;
3545 for (i = 0; i < count; i++) {
3547 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3554 * Back in the old days when we still had component-less DAIs,
3555 * instead of having a static name, component-less DAIs would
3556 * inherit the name of the parent device so it is possible to
3557 * register multiple instances of the DAI. We still need to keep
3558 * the same naming style even though those DAIs are not
3559 * component-less anymore.
3561 if (count == 1 && legacy_dai_naming) {
3562 dai->name = fmt_single_name(dev, &dai->id);
3564 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3566 dai->id = dai_drv[i].id;
3570 if (dai->name == NULL) {
3576 dai->component = component;
3578 dai->driver = &dai_drv[i];
3579 if (!dai->driver->ops)
3580 dai->driver->ops = &null_dai_ops;
3582 list_add(&dai->list, &component->dai_list);
3584 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3590 snd_soc_unregister_dais(component);
3595 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3596 enum snd_soc_dapm_type type, int subseq)
3598 struct snd_soc_component *component = dapm->component;
3600 component->driver->seq_notifier(component, type, subseq);
3603 static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3606 struct snd_soc_component *component = dapm->component;
3608 return component->driver->stream_event(component, event);
3611 static int snd_soc_component_initialize(struct snd_soc_component *component,
3612 const struct snd_soc_component_driver *driver, struct device *dev)
3614 struct snd_soc_dapm_context *dapm;
3616 component->name = fmt_single_name(dev, &component->id);
3617 if (!component->name) {
3618 dev_err(dev, "ASoC: Failed to allocate name\n");
3622 component->dev = dev;
3623 component->driver = driver;
3624 component->probe = component->driver->probe;
3625 component->remove = component->driver->remove;
3627 if (!component->dapm_ptr)
3628 component->dapm_ptr = &component->dapm;
3630 dapm = component->dapm_ptr;
3632 dapm->component = component;
3633 dapm->bias_level = SND_SOC_BIAS_OFF;
3634 dapm->idle_bias_off = true;
3635 if (driver->seq_notifier)
3636 dapm->seq_notifier = snd_soc_component_seq_notifier;
3637 if (driver->stream_event)
3638 dapm->stream_event = snd_soc_component_stream_event;
3640 component->controls = driver->controls;
3641 component->num_controls = driver->num_controls;
3642 component->dapm_widgets = driver->dapm_widgets;
3643 component->num_dapm_widgets = driver->num_dapm_widgets;
3644 component->dapm_routes = driver->dapm_routes;
3645 component->num_dapm_routes = driver->num_dapm_routes;
3647 INIT_LIST_HEAD(&component->dai_list);
3648 mutex_init(&component->io_mutex);
3653 static void snd_soc_component_init_regmap(struct snd_soc_component *component)
3655 if (!component->regmap)
3656 component->regmap = dev_get_regmap(component->dev, NULL);
3657 if (component->regmap) {
3658 int val_bytes = regmap_get_val_bytes(component->regmap);
3659 /* Errors are legitimate for non-integer byte multiples */
3661 component->val_bytes = val_bytes;
3665 static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
3667 if (!component->write && !component->read)
3668 snd_soc_component_init_regmap(component);
3670 list_add(&component->list, &component_list);
3673 static void snd_soc_component_add(struct snd_soc_component *component)
3675 mutex_lock(&client_mutex);
3676 snd_soc_component_add_unlocked(component);
3677 mutex_unlock(&client_mutex);
3680 static void snd_soc_component_cleanup(struct snd_soc_component *component)
3682 snd_soc_unregister_dais(component);
3683 kfree(component->name);
3686 static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3688 list_del(&component->list);
3691 static void snd_soc_component_del(struct snd_soc_component *component)
3693 mutex_lock(&client_mutex);
3694 snd_soc_component_del_unlocked(component);
3695 mutex_unlock(&client_mutex);
3698 int snd_soc_register_component(struct device *dev,
3699 const struct snd_soc_component_driver *cmpnt_drv,
3700 struct snd_soc_dai_driver *dai_drv,
3703 struct snd_soc_component *cmpnt;
3706 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
3708 dev_err(dev, "ASoC: Failed to allocate memory\n");
3712 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
3716 cmpnt->ignore_pmdown_time = true;
3717 cmpnt->registered_as_component = true;
3719 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
3721 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
3725 snd_soc_component_add(cmpnt);
3730 snd_soc_component_cleanup(cmpnt);
3735 EXPORT_SYMBOL_GPL(snd_soc_register_component);
3738 * snd_soc_unregister_component - Unregister a component from the ASoC core
3741 void snd_soc_unregister_component(struct device *dev)
3743 struct snd_soc_component *cmpnt;
3745 list_for_each_entry(cmpnt, &component_list, list) {
3746 if (dev == cmpnt->dev && cmpnt->registered_as_component)
3752 snd_soc_component_del(cmpnt);
3753 snd_soc_component_cleanup(cmpnt);
3756 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3758 static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
3760 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3762 return platform->driver->probe(platform);
3765 static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
3767 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3769 platform->driver->remove(platform);
3773 * snd_soc_add_platform - Add a platform to the ASoC core
3774 * @dev: The parent device for the platform
3775 * @platform: The platform to add
3776 * @platform_driver: The driver for the platform
3778 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
3779 const struct snd_soc_platform_driver *platform_drv)
3783 ret = snd_soc_component_initialize(&platform->component,
3784 &platform_drv->component_driver, dev);
3788 platform->dev = dev;
3789 platform->driver = platform_drv;
3791 if (platform_drv->probe)
3792 platform->component.probe = snd_soc_platform_drv_probe;
3793 if (platform_drv->remove)
3794 platform->component.remove = snd_soc_platform_drv_remove;
3796 #ifdef CONFIG_DEBUG_FS
3797 platform->component.debugfs_prefix = "platform";
3800 mutex_lock(&client_mutex);
3801 snd_soc_component_add_unlocked(&platform->component);
3802 list_add(&platform->list, &platform_list);
3803 mutex_unlock(&client_mutex);
3805 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
3806 platform->component.name);
3810 EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3813 * snd_soc_register_platform - Register a platform with the ASoC core
3815 * @platform: platform to register
3817 int snd_soc_register_platform(struct device *dev,
3818 const struct snd_soc_platform_driver *platform_drv)
3820 struct snd_soc_platform *platform;
3823 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3825 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3826 if (platform == NULL)
3829 ret = snd_soc_add_platform(dev, platform, platform_drv);
3835 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3838 * snd_soc_remove_platform - Remove a platform from the ASoC core
3839 * @platform: the platform to remove
3841 void snd_soc_remove_platform(struct snd_soc_platform *platform)
3844 mutex_lock(&client_mutex);
3845 list_del(&platform->list);
3846 snd_soc_component_del_unlocked(&platform->component);
3847 mutex_unlock(&client_mutex);
3849 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3850 platform->component.name);
3852 snd_soc_component_cleanup(&platform->component);
3854 EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
3856 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
3858 struct snd_soc_platform *platform;
3860 list_for_each_entry(platform, &platform_list, list) {
3861 if (dev == platform->dev)
3867 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
3870 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3872 * @platform: platform to unregister
3874 void snd_soc_unregister_platform(struct device *dev)
3876 struct snd_soc_platform *platform;
3878 platform = snd_soc_lookup_platform(dev);
3882 snd_soc_remove_platform(platform);
3885 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3887 static u64 codec_format_map[] = {
3888 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3889 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3890 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3891 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3892 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3893 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3894 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3895 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3896 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3897 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3898 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3899 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3900 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3901 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3902 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3903 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3906 /* Fix up the DAI formats for endianness: codecs don't actually see
3907 * the endianness of the data but we're using the CPU format
3908 * definitions which do need to include endianness so we ensure that
3909 * codec DAIs always have both big and little endian variants set.
3911 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3915 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3916 if (stream->formats & codec_format_map[i])
3917 stream->formats |= codec_format_map[i];
3920 static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3922 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3924 return codec->driver->probe(codec);
3927 static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3929 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3931 codec->driver->remove(codec);
3934 static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3935 unsigned int reg, unsigned int val)
3937 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3939 return codec->driver->write(codec, reg, val);
3942 static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3943 unsigned int reg, unsigned int *val)
3945 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3947 *val = codec->driver->read(codec, reg);
3952 static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
3953 enum snd_soc_bias_level level)
3955 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
3957 return codec->driver->set_bias_level(codec, level);
3961 * snd_soc_register_codec - Register a codec with the ASoC core
3963 * @codec: codec to register
3965 int snd_soc_register_codec(struct device *dev,
3966 const struct snd_soc_codec_driver *codec_drv,
3967 struct snd_soc_dai_driver *dai_drv,
3970 struct snd_soc_codec *codec;
3971 struct snd_soc_dai *dai;
3974 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3976 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3980 codec->component.dapm_ptr = &codec->dapm;
3981 codec->component.codec = codec;
3983 ret = snd_soc_component_initialize(&codec->component,
3984 &codec_drv->component_driver, dev);
3988 if (codec_drv->controls) {
3989 codec->component.controls = codec_drv->controls;
3990 codec->component.num_controls = codec_drv->num_controls;
3992 if (codec_drv->dapm_widgets) {
3993 codec->component.dapm_widgets = codec_drv->dapm_widgets;
3994 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
3996 if (codec_drv->dapm_routes) {
3997 codec->component.dapm_routes = codec_drv->dapm_routes;
3998 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
4001 if (codec_drv->probe)
4002 codec->component.probe = snd_soc_codec_drv_probe;
4003 if (codec_drv->remove)
4004 codec->component.remove = snd_soc_codec_drv_remove;
4005 if (codec_drv->write)
4006 codec->component.write = snd_soc_codec_drv_write;
4007 if (codec_drv->read)
4008 codec->component.read = snd_soc_codec_drv_read;
4009 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
4010 codec->dapm.idle_bias_off = codec_drv->idle_bias_off;
4011 codec->dapm.suspend_bias_off = codec_drv->suspend_bias_off;
4012 if (codec_drv->seq_notifier)
4013 codec->dapm.seq_notifier = codec_drv->seq_notifier;
4014 if (codec_drv->set_bias_level)
4015 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
4017 codec->driver = codec_drv;
4018 codec->component.val_bytes = codec_drv->reg_word_size;
4019 mutex_init(&codec->mutex);
4021 #ifdef CONFIG_DEBUG_FS
4022 codec->component.init_debugfs = soc_init_codec_debugfs;
4023 codec->component.debugfs_prefix = "codec";
4026 if (codec_drv->get_regmap)
4027 codec->component.regmap = codec_drv->get_regmap(dev);
4029 for (i = 0; i < num_dai; i++) {
4030 fixup_codec_formats(&dai_drv[i].playback);
4031 fixup_codec_formats(&dai_drv[i].capture);
4034 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4036 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4040 list_for_each_entry(dai, &codec->component.dai_list, list)
4043 mutex_lock(&client_mutex);
4044 snd_soc_component_add_unlocked(&codec->component);
4045 list_add(&codec->list, &codec_list);
4046 mutex_unlock(&client_mutex);
4048 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4049 codec->component.name);
4053 snd_soc_component_cleanup(&codec->component);
4058 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4061 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4063 * @codec: codec to unregister
4065 void snd_soc_unregister_codec(struct device *dev)
4067 struct snd_soc_codec *codec;
4069 list_for_each_entry(codec, &codec_list, list) {
4070 if (dev == codec->dev)
4077 mutex_lock(&client_mutex);
4078 list_del(&codec->list);
4079 snd_soc_component_del_unlocked(&codec->component);
4080 mutex_unlock(&client_mutex);
4082 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4083 codec->component.name);
4085 snd_soc_component_cleanup(&codec->component);
4086 snd_soc_cache_exit(codec);
4089 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4091 /* Retrieve a card's name from device tree */
4092 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4093 const char *propname)
4095 struct device_node *np;
4099 pr_err("card->dev is not set before calling %s\n", __func__);
4103 np = card->dev->of_node;
4105 ret = of_property_read_string_index(np, propname, 0, &card->name);
4107 * EINVAL means the property does not exist. This is fine providing
4108 * card->name was previously set, which is checked later in
4109 * snd_soc_register_card.
4111 if (ret < 0 && ret != -EINVAL) {
4113 "ASoC: Property '%s' could not be read: %d\n",
4120 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4122 static const struct snd_soc_dapm_widget simple_widgets[] = {
4123 SND_SOC_DAPM_MIC("Microphone", NULL),
4124 SND_SOC_DAPM_LINE("Line", NULL),
4125 SND_SOC_DAPM_HP("Headphone", NULL),
4126 SND_SOC_DAPM_SPK("Speaker", NULL),
4129 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4130 const char *propname)
4132 struct device_node *np = card->dev->of_node;
4133 struct snd_soc_dapm_widget *widgets;
4134 const char *template, *wname;
4135 int i, j, num_widgets, ret;
4137 num_widgets = of_property_count_strings(np, propname);
4138 if (num_widgets < 0) {
4140 "ASoC: Property '%s' does not exist\n", propname);
4143 if (num_widgets & 1) {
4145 "ASoC: Property '%s' length is not even\n", propname);
4151 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4156 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4160 "ASoC: Could not allocate memory for widgets\n");
4164 for (i = 0; i < num_widgets; i++) {
4165 ret = of_property_read_string_index(np, propname,
4169 "ASoC: Property '%s' index %d read error:%d\n",
4170 propname, 2 * i, ret);
4174 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4175 if (!strncmp(template, simple_widgets[j].name,
4176 strlen(simple_widgets[j].name))) {
4177 widgets[i] = simple_widgets[j];
4182 if (j >= ARRAY_SIZE(simple_widgets)) {
4184 "ASoC: DAPM widget '%s' is not supported\n",
4189 ret = of_property_read_string_index(np, propname,
4194 "ASoC: Property '%s' index %d read error:%d\n",
4195 propname, (2 * i) + 1, ret);
4199 widgets[i].name = wname;
4202 card->dapm_widgets = widgets;
4203 card->num_dapm_widgets = num_widgets;
4207 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4209 int snd_soc_of_parse_tdm_slot(struct device_node *np,
4210 unsigned int *slots,
4211 unsigned int *slot_width)
4216 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4217 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4225 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4226 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4236 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4238 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4239 const char *propname)
4241 struct device_node *np = card->dev->of_node;
4243 struct snd_soc_dapm_route *routes;
4246 num_routes = of_property_count_strings(np, propname);
4247 if (num_routes < 0 || num_routes & 1) {
4249 "ASoC: Property '%s' does not exist or its length is not even\n",
4255 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4260 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4264 "ASoC: Could not allocate DAPM route table\n");
4268 for (i = 0; i < num_routes; i++) {
4269 ret = of_property_read_string_index(np, propname,
4270 2 * i, &routes[i].sink);
4273 "ASoC: Property '%s' index %d could not be read: %d\n",
4274 propname, 2 * i, ret);
4277 ret = of_property_read_string_index(np, propname,
4278 (2 * i) + 1, &routes[i].source);
4281 "ASoC: Property '%s' index %d could not be read: %d\n",
4282 propname, (2 * i) + 1, ret);
4287 card->num_dapm_routes = num_routes;
4288 card->dapm_routes = routes;
4292 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4294 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4296 struct device_node **bitclkmaster,
4297 struct device_node **framemaster)
4301 unsigned int format = 0;
4307 } of_fmt_table[] = {
4308 { "i2s", SND_SOC_DAIFMT_I2S },
4309 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4310 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4311 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4312 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4313 { "ac97", SND_SOC_DAIFMT_AC97 },
4314 { "pdm", SND_SOC_DAIFMT_PDM},
4315 { "msb", SND_SOC_DAIFMT_MSB },
4316 { "lsb", SND_SOC_DAIFMT_LSB },
4323 * check "[prefix]format = xxx"
4324 * SND_SOC_DAIFMT_FORMAT_MASK area
4326 snprintf(prop, sizeof(prop), "%sformat", prefix);
4327 ret = of_property_read_string(np, prop, &str);
4329 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4330 if (strcmp(str, of_fmt_table[i].name) == 0) {
4331 format |= of_fmt_table[i].val;
4338 * check "[prefix]continuous-clock"
4339 * SND_SOC_DAIFMT_CLOCK_MASK area
4341 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4342 if (of_get_property(np, prop, NULL))
4343 format |= SND_SOC_DAIFMT_CONT;
4345 format |= SND_SOC_DAIFMT_GATED;
4348 * check "[prefix]bitclock-inversion"
4349 * check "[prefix]frame-inversion"
4350 * SND_SOC_DAIFMT_INV_MASK area
4352 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4353 bit = !!of_get_property(np, prop, NULL);
4355 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4356 frame = !!of_get_property(np, prop, NULL);
4358 switch ((bit << 4) + frame) {
4360 format |= SND_SOC_DAIFMT_IB_IF;
4363 format |= SND_SOC_DAIFMT_IB_NF;
4366 format |= SND_SOC_DAIFMT_NB_IF;
4369 /* SND_SOC_DAIFMT_NB_NF is default */
4374 * check "[prefix]bitclock-master"
4375 * check "[prefix]frame-master"
4376 * SND_SOC_DAIFMT_MASTER_MASK area
4378 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4379 bit = !!of_get_property(np, prop, NULL);
4380 if (bit && bitclkmaster)
4381 *bitclkmaster = of_parse_phandle(np, prop, 0);
4383 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4384 frame = !!of_get_property(np, prop, NULL);
4385 if (frame && framemaster)
4386 *framemaster = of_parse_phandle(np, prop, 0);
4388 switch ((bit << 4) + frame) {
4390 format |= SND_SOC_DAIFMT_CBM_CFM;
4393 format |= SND_SOC_DAIFMT_CBM_CFS;
4396 format |= SND_SOC_DAIFMT_CBS_CFM;
4399 format |= SND_SOC_DAIFMT_CBS_CFS;
4405 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4407 int snd_soc_of_get_dai_name(struct device_node *of_node,
4408 const char **dai_name)
4410 struct snd_soc_component *pos;
4411 struct of_phandle_args args;
4414 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4415 "#sound-dai-cells", 0, &args);
4419 ret = -EPROBE_DEFER;
4421 mutex_lock(&client_mutex);
4422 list_for_each_entry(pos, &component_list, list) {
4423 if (pos->dev->of_node != args.np)
4426 if (pos->driver->of_xlate_dai_name) {
4427 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4431 switch (args.args_count) {
4433 id = 0; /* same as dai_drv[0] */
4443 if (id < 0 || id >= pos->num_dai) {
4450 *dai_name = pos->dai_drv[id].name;
4452 *dai_name = pos->name;
4457 mutex_unlock(&client_mutex);
4459 of_node_put(args.np);
4463 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4465 static int __init snd_soc_init(void)
4467 #ifdef CONFIG_DEBUG_FS
4468 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4469 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
4470 pr_warn("ASoC: Failed to create debugfs directory\n");
4471 snd_soc_debugfs_root = NULL;
4474 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4476 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4478 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4480 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
4482 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
4483 &platform_list_fops))
4484 pr_warn("ASoC: Failed to create platform list debugfs file\n");
4487 snd_soc_util_init();
4489 return platform_driver_register(&soc_driver);
4491 module_init(snd_soc_init);
4493 static void __exit snd_soc_exit(void)
4495 snd_soc_util_exit();
4497 #ifdef CONFIG_DEBUG_FS
4498 debugfs_remove_recursive(snd_soc_debugfs_root);
4500 platform_driver_unregister(&soc_driver);
4502 module_exit(snd_soc_exit);
4504 /* Module information */
4505 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4506 MODULE_DESCRIPTION("ALSA SoC Core");
4507 MODULE_LICENSE("GPL");
4508 MODULE_ALIAS("platform:soc-audio");