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->bus_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->bus_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 control bus 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->bus_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->bus_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 bool bus_control = false;
764 /* If the card is not initialized yet there is nothing to do */
765 if (!card->instantiated)
768 /* activate pins from sleep state */
769 for (i = 0; i < card->num_rtd; i++) {
770 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
771 struct snd_soc_dai **codec_dais = rtd->codec_dais;
772 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
776 pinctrl_pm_select_default_state(cpu_dai->dev);
778 for (j = 0; j < rtd->num_codecs; j++) {
779 struct snd_soc_dai *codec_dai = codec_dais[j];
780 if (codec_dai->active)
781 pinctrl_pm_select_default_state(codec_dai->dev);
786 * DAIs that also act as the control bus master might have other drivers
787 * hanging off them so need to resume immediately. Other drivers don't
788 * have that problem and may take a substantial amount of time to resume
789 * due to I/O costs and anti-pop so handle them out of line.
791 for (i = 0; i < card->num_rtd; i++) {
792 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
793 bus_control |= cpu_dai->driver->bus_control;
796 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
797 soc_resume_deferred(&card->deferred_resume_work);
799 dev_dbg(dev, "ASoC: Scheduling resume work\n");
800 if (!schedule_work(&card->deferred_resume_work))
801 dev_err(dev, "ASoC: resume work item may be lost\n");
806 EXPORT_SYMBOL_GPL(snd_soc_resume);
808 #define snd_soc_suspend NULL
809 #define snd_soc_resume NULL
812 static const struct snd_soc_dai_ops null_dai_ops = {
815 static struct snd_soc_component *soc_find_component(
816 const struct device_node *of_node, const char *name)
818 struct snd_soc_component *component;
820 list_for_each_entry(component, &component_list, list) {
822 if (component->dev->of_node == of_node)
824 } else if (strcmp(component->name, name) == 0) {
832 static struct snd_soc_dai *snd_soc_find_dai(
833 const struct snd_soc_dai_link_component *dlc)
835 struct snd_soc_component *component;
836 struct snd_soc_dai *dai;
838 /* Find CPU DAI from registered DAIs*/
839 list_for_each_entry(component, &component_list, list) {
840 if (dlc->of_node && component->dev->of_node != dlc->of_node)
842 if (dlc->name && strcmp(dev_name(component->dev), dlc->name))
844 list_for_each_entry(dai, &component->dai_list, list) {
845 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
855 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
857 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
858 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
859 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
860 struct snd_soc_dai_link_component cpu_dai_component;
861 struct snd_soc_dai **codec_dais = rtd->codec_dais;
862 struct snd_soc_platform *platform;
863 const char *platform_name;
866 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
868 cpu_dai_component.name = dai_link->cpu_name;
869 cpu_dai_component.of_node = dai_link->cpu_of_node;
870 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
871 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
873 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
874 dai_link->cpu_dai_name);
875 return -EPROBE_DEFER;
878 rtd->num_codecs = dai_link->num_codecs;
880 /* Find CODEC from registered CODECs */
881 for (i = 0; i < rtd->num_codecs; i++) {
882 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
883 if (!codec_dais[i]) {
884 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
886 return -EPROBE_DEFER;
890 /* Single codec links expect codec and codec_dai in runtime data */
891 rtd->codec_dai = codec_dais[0];
892 rtd->codec = rtd->codec_dai->codec;
894 /* if there's no platform we match on the empty platform */
895 platform_name = dai_link->platform_name;
896 if (!platform_name && !dai_link->platform_of_node)
897 platform_name = "snd-soc-dummy";
899 /* find one from the set of registered platforms */
900 list_for_each_entry(platform, &platform_list, list) {
901 if (dai_link->platform_of_node) {
902 if (platform->dev->of_node !=
903 dai_link->platform_of_node)
906 if (strcmp(platform->component.name, platform_name))
910 rtd->platform = platform;
912 if (!rtd->platform) {
913 dev_err(card->dev, "ASoC: platform %s not registered\n",
914 dai_link->platform_name);
915 return -EPROBE_DEFER;
923 static void soc_remove_component(struct snd_soc_component *component)
925 if (!component->probed)
928 /* This is a HACK and will be removed soon */
929 if (component->codec)
930 list_del(&component->codec->card_list);
932 if (component->remove)
933 component->remove(component);
935 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
937 soc_cleanup_component_debugfs(component);
938 component->probed = 0;
939 module_put(component->dev->driver->owner);
942 static void soc_remove_dai(struct snd_soc_dai *dai, int order)
946 if (dai && dai->probed &&
947 dai->driver->remove_order == order) {
948 if (dai->driver->remove) {
949 err = dai->driver->remove(dai);
952 "ASoC: failed to remove %s: %d\n",
959 static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
961 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
964 /* unregister the rtd device */
965 if (rtd->dev_registered) {
966 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
967 device_remove_file(rtd->dev, &dev_attr_codec_reg);
968 device_unregister(rtd->dev);
969 rtd->dev_registered = 0;
972 /* remove the CODEC DAI */
973 for (i = 0; i < rtd->num_codecs; i++)
974 soc_remove_dai(rtd->codec_dais[i], order);
976 soc_remove_dai(rtd->cpu_dai, order);
979 static void soc_remove_link_components(struct snd_soc_card *card, int num,
982 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
983 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
984 struct snd_soc_platform *platform = rtd->platform;
985 struct snd_soc_component *component;
988 /* remove the platform */
989 if (platform && platform->component.driver->remove_order == order)
990 soc_remove_component(&platform->component);
992 /* remove the CODEC-side CODEC */
993 for (i = 0; i < rtd->num_codecs; i++) {
994 component = rtd->codec_dais[i]->component;
995 if (component->driver->remove_order == order)
996 soc_remove_component(component);
999 /* remove any CPU-side CODEC */
1001 if (cpu_dai->component->driver->remove_order == order)
1002 soc_remove_component(cpu_dai->component);
1006 static void soc_remove_dai_links(struct snd_soc_card *card)
1010 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1012 for (dai = 0; dai < card->num_rtd; dai++)
1013 soc_remove_link_dais(card, dai, order);
1016 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1018 for (dai = 0; dai < card->num_rtd; dai++)
1019 soc_remove_link_components(card, dai, order);
1025 static void soc_set_name_prefix(struct snd_soc_card *card,
1026 struct snd_soc_component *component)
1030 if (card->codec_conf == NULL)
1033 for (i = 0; i < card->num_configs; i++) {
1034 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1035 if (map->of_node && component->dev->of_node != map->of_node)
1037 if (map->dev_name && strcmp(component->name, map->dev_name))
1039 component->name_prefix = map->name_prefix;
1044 static int soc_probe_component(struct snd_soc_card *card,
1045 struct snd_soc_component *component)
1047 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1048 struct snd_soc_dai *dai;
1051 if (component->probed)
1054 component->card = card;
1056 soc_set_name_prefix(card, component);
1058 if (!try_module_get(component->dev->driver->owner))
1061 soc_init_component_debugfs(component);
1063 if (component->dapm_widgets) {
1064 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1065 component->num_dapm_widgets);
1068 dev_err(component->dev,
1069 "Failed to create new controls %d\n", ret);
1074 list_for_each_entry(dai, &component->dai_list, list) {
1075 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1077 dev_err(component->dev,
1078 "Failed to create DAI widgets %d\n", ret);
1083 if (component->probe) {
1084 ret = component->probe(component);
1086 dev_err(component->dev,
1087 "ASoC: failed to probe component %d\n", ret);
1091 WARN(dapm->idle_bias_off &&
1092 dapm->bias_level != SND_SOC_BIAS_OFF,
1093 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1097 if (component->controls)
1098 snd_soc_add_component_controls(component, component->controls,
1099 component->num_controls);
1100 if (component->dapm_routes)
1101 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1102 component->num_dapm_routes);
1104 component->probed = 1;
1105 list_add(&dapm->list, &card->dapm_list);
1107 /* This is a HACK and will be removed soon */
1108 if (component->codec)
1109 list_add(&component->codec->card_list, &card->codec_dev_list);
1114 soc_cleanup_component_debugfs(component);
1115 module_put(component->dev->driver->owner);
1120 static void rtd_release(struct device *dev)
1125 static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1130 /* register the rtd device */
1131 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1134 device_initialize(rtd->dev);
1135 rtd->dev->parent = rtd->card->dev;
1136 rtd->dev->release = rtd_release;
1137 dev_set_name(rtd->dev, "%s", name);
1138 dev_set_drvdata(rtd->dev, rtd);
1139 mutex_init(&rtd->pcm_mutex);
1140 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1141 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1142 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1143 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1144 ret = device_add(rtd->dev);
1146 /* calling put_device() here to free the rtd->dev */
1147 put_device(rtd->dev);
1148 dev_err(rtd->card->dev,
1149 "ASoC: failed to register runtime device: %d\n", ret);
1152 rtd->dev_registered = 1;
1155 /* add DAPM sysfs entries for this codec */
1156 ret = snd_soc_dapm_sys_add(rtd->dev);
1159 "ASoC: failed to add codec dapm sysfs entries: %d\n",
1162 /* add codec sysfs entries */
1163 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1166 "ASoC: failed to add codec sysfs files: %d\n",
1173 static int soc_probe_link_components(struct snd_soc_card *card, int num,
1176 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1177 struct snd_soc_platform *platform = rtd->platform;
1178 struct snd_soc_component *component;
1181 /* probe the CPU-side component, if it is a CODEC */
1182 component = rtd->cpu_dai->component;
1183 if (component->driver->probe_order == order) {
1184 ret = soc_probe_component(card, component);
1189 /* probe the CODEC-side components */
1190 for (i = 0; i < rtd->num_codecs; i++) {
1191 component = rtd->codec_dais[i]->component;
1192 if (component->driver->probe_order == order) {
1193 ret = soc_probe_component(card, component);
1199 /* probe the platform */
1200 if (platform->component.driver->probe_order == order) {
1201 ret = soc_probe_component(card, &platform->component);
1209 static int soc_probe_codec_dai(struct snd_soc_card *card,
1210 struct snd_soc_dai *codec_dai,
1215 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1216 if (codec_dai->driver->probe) {
1217 ret = codec_dai->driver->probe(codec_dai);
1219 dev_err(codec_dai->dev,
1220 "ASoC: failed to probe CODEC DAI %s: %d\n",
1221 codec_dai->name, ret);
1226 /* mark codec_dai as probed and add to card dai list */
1227 codec_dai->probed = 1;
1233 static int soc_link_dai_widgets(struct snd_soc_card *card,
1234 struct snd_soc_dai_link *dai_link,
1235 struct snd_soc_pcm_runtime *rtd)
1237 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1238 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1239 struct snd_soc_dapm_widget *play_w, *capture_w;
1242 if (rtd->num_codecs > 1)
1243 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1245 /* link the DAI widgets */
1246 play_w = codec_dai->playback_widget;
1247 capture_w = cpu_dai->capture_widget;
1248 if (play_w && capture_w) {
1249 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1252 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1253 play_w->name, capture_w->name, ret);
1258 play_w = cpu_dai->playback_widget;
1259 capture_w = codec_dai->capture_widget;
1260 if (play_w && capture_w) {
1261 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1264 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1265 play_w->name, capture_w->name, ret);
1273 static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1275 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1276 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1277 struct snd_soc_platform *platform = rtd->platform;
1278 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1281 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1282 card->name, num, order);
1284 /* config components */
1285 cpu_dai->platform = platform;
1286 cpu_dai->card = card;
1287 for (i = 0; i < rtd->num_codecs; i++)
1288 rtd->codec_dais[i]->card = card;
1290 /* set default power off timeout */
1291 rtd->pmdown_time = pmdown_time;
1293 /* probe the cpu_dai */
1294 if (!cpu_dai->probed &&
1295 cpu_dai->driver->probe_order == order) {
1296 if (cpu_dai->driver->probe) {
1297 ret = cpu_dai->driver->probe(cpu_dai);
1299 dev_err(cpu_dai->dev,
1300 "ASoC: failed to probe CPU DAI %s: %d\n",
1301 cpu_dai->name, ret);
1305 cpu_dai->probed = 1;
1308 /* probe the CODEC DAI */
1309 for (i = 0; i < rtd->num_codecs; i++) {
1310 ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order);
1315 /* complete DAI probe during last probe */
1316 if (order != SND_SOC_COMP_ORDER_LAST)
1319 /* do machine specific initialization */
1320 if (dai_link->init) {
1321 ret = dai_link->init(rtd);
1323 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1324 dai_link->name, ret);
1329 ret = soc_post_component_init(rtd, dai_link->name);
1333 #ifdef CONFIG_DEBUG_FS
1334 /* add DPCM sysfs entries */
1335 if (dai_link->dynamic) {
1336 ret = soc_dpcm_debugfs_add(rtd);
1339 "ASoC: failed to add dpcm sysfs entries: %d\n",
1346 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1348 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1351 if (cpu_dai->driver->compress_dai) {
1352 /*create compress_device"*/
1353 ret = soc_new_compress(rtd, num);
1355 dev_err(card->dev, "ASoC: can't create compress %s\n",
1356 dai_link->stream_name);
1361 if (!dai_link->params) {
1362 /* create the pcm */
1363 ret = soc_new_pcm(rtd, num);
1365 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1366 dai_link->stream_name, ret);
1370 INIT_DELAYED_WORK(&rtd->delayed_work,
1371 codec2codec_close_delayed_work);
1373 /* link the DAI widgets */
1374 ret = soc_link_dai_widgets(card, dai_link, rtd);
1383 static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1385 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1386 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1387 const char *name = aux_dev->codec_name;
1389 rtd->component = soc_find_component(aux_dev->codec_of_node, name);
1390 if (!rtd->component) {
1391 if (aux_dev->codec_of_node)
1392 name = of_node_full_name(aux_dev->codec_of_node);
1394 dev_err(card->dev, "ASoC: %s not registered\n", name);
1395 return -EPROBE_DEFER;
1399 * Some places still reference rtd->codec, so we have to keep that
1400 * initialized if the component is a CODEC. Once all those references
1401 * have been removed, this code can be removed as well.
1403 rtd->codec = rtd->component->codec;
1408 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1410 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1411 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1414 ret = soc_probe_component(card, rtd->component);
1418 /* do machine specific initialization */
1419 if (aux_dev->init) {
1420 ret = aux_dev->init(rtd->component);
1422 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1423 aux_dev->name, ret);
1428 return soc_post_component_init(rtd, aux_dev->name);
1431 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1433 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1434 struct snd_soc_component *component = rtd->component;
1436 /* unregister the rtd device */
1437 if (rtd->dev_registered) {
1438 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1439 device_unregister(rtd->dev);
1440 rtd->dev_registered = 0;
1443 if (component && component->probed)
1444 soc_remove_component(component);
1447 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1451 if (codec->cache_init)
1454 ret = snd_soc_cache_init(codec);
1457 "ASoC: Failed to set cache compression type: %d\n",
1461 codec->cache_init = 1;
1465 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1467 struct snd_soc_codec *codec;
1468 struct snd_soc_dai_link *dai_link;
1469 int ret, i, order, dai_fmt;
1471 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1474 for (i = 0; i < card->num_links; i++) {
1475 ret = soc_bind_dai_link(card, i);
1480 /* bind aux_devs too */
1481 for (i = 0; i < card->num_aux_devs; i++) {
1482 ret = soc_bind_aux_dev(card, i);
1487 /* initialize the register cache for each available codec */
1488 list_for_each_entry(codec, &codec_list, list) {
1489 if (codec->cache_init)
1491 ret = snd_soc_init_codec_cache(codec);
1496 /* card bind complete so register a sound card */
1497 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1498 card->owner, 0, &card->snd_card);
1501 "ASoC: can't create sound card for card %s: %d\n",
1506 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1507 card->dapm.dev = card->dev;
1508 card->dapm.card = card;
1509 list_add(&card->dapm.list, &card->dapm_list);
1511 #ifdef CONFIG_DEBUG_FS
1512 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1515 #ifdef CONFIG_PM_SLEEP
1516 /* deferred resume work */
1517 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1520 if (card->dapm_widgets)
1521 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1522 card->num_dapm_widgets);
1524 /* initialise the sound card only once */
1526 ret = card->probe(card);
1528 goto card_probe_error;
1531 /* probe all components used by DAI links on this card */
1532 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1534 for (i = 0; i < card->num_links; i++) {
1535 ret = soc_probe_link_components(card, i, order);
1538 "ASoC: failed to instantiate card %d\n",
1545 /* probe all DAI links on this card */
1546 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1548 for (i = 0; i < card->num_links; i++) {
1549 ret = soc_probe_link_dais(card, i, order);
1552 "ASoC: failed to instantiate card %d\n",
1559 for (i = 0; i < card->num_aux_devs; i++) {
1560 ret = soc_probe_aux_dev(card, i);
1563 "ASoC: failed to add auxiliary devices %d\n",
1565 goto probe_aux_dev_err;
1569 snd_soc_dapm_link_dai_widgets(card);
1570 snd_soc_dapm_connect_dai_link_widgets(card);
1573 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1575 if (card->dapm_routes)
1576 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1577 card->num_dapm_routes);
1579 for (i = 0; i < card->num_links; i++) {
1580 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1581 dai_link = &card->dai_link[i];
1582 dai_fmt = dai_link->dai_fmt;
1585 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1588 for (j = 0; j < rtd->num_codecs; j++) {
1589 struct snd_soc_dai *codec_dai = codec_dais[j];
1591 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1592 if (ret != 0 && ret != -ENOTSUPP)
1593 dev_warn(codec_dai->dev,
1594 "ASoC: Failed to set DAI format: %d\n",
1599 /* If this is a regular CPU link there will be a platform */
1601 (dai_link->platform_name || dai_link->platform_of_node)) {
1602 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1604 if (ret != 0 && ret != -ENOTSUPP)
1605 dev_warn(card->rtd[i].cpu_dai->dev,
1606 "ASoC: Failed to set DAI format: %d\n",
1608 } else if (dai_fmt) {
1609 /* Flip the polarity for the "CPU" end */
1610 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1611 switch (dai_link->dai_fmt &
1612 SND_SOC_DAIFMT_MASTER_MASK) {
1613 case SND_SOC_DAIFMT_CBM_CFM:
1614 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1616 case SND_SOC_DAIFMT_CBM_CFS:
1617 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1619 case SND_SOC_DAIFMT_CBS_CFM:
1620 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1622 case SND_SOC_DAIFMT_CBS_CFS:
1623 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1627 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1629 if (ret != 0 && ret != -ENOTSUPP)
1630 dev_warn(card->rtd[i].cpu_dai->dev,
1631 "ASoC: Failed to set DAI format: %d\n",
1636 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1638 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1639 "%s", card->long_name ? card->long_name : card->name);
1640 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1641 "%s", card->driver_name ? card->driver_name : card->name);
1642 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1643 switch (card->snd_card->driver[i]) {
1649 if (!isalnum(card->snd_card->driver[i]))
1650 card->snd_card->driver[i] = '_';
1655 if (card->late_probe) {
1656 ret = card->late_probe(card);
1658 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
1660 goto probe_aux_dev_err;
1664 if (card->fully_routed)
1665 snd_soc_dapm_auto_nc_pins(card);
1667 snd_soc_dapm_new_widgets(card);
1669 ret = snd_card_register(card->snd_card);
1671 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1673 goto probe_aux_dev_err;
1676 card->instantiated = 1;
1677 snd_soc_dapm_sync(&card->dapm);
1678 mutex_unlock(&card->mutex);
1683 for (i = 0; i < card->num_aux_devs; i++)
1684 soc_remove_aux_dev(card, i);
1687 soc_remove_dai_links(card);
1693 snd_card_free(card->snd_card);
1696 mutex_unlock(&card->mutex);
1701 /* probes a new socdev */
1702 static int soc_probe(struct platform_device *pdev)
1704 struct snd_soc_card *card = platform_get_drvdata(pdev);
1707 * no card, so machine driver should be registering card
1708 * we should not be here in that case so ret error
1713 dev_warn(&pdev->dev,
1714 "ASoC: machine %s should use snd_soc_register_card()\n",
1717 /* Bodge while we unpick instantiation */
1718 card->dev = &pdev->dev;
1720 return snd_soc_register_card(card);
1723 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1727 /* make sure any delayed work runs */
1728 for (i = 0; i < card->num_rtd; i++) {
1729 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1730 flush_delayed_work(&rtd->delayed_work);
1733 /* remove auxiliary devices */
1734 for (i = 0; i < card->num_aux_devs; i++)
1735 soc_remove_aux_dev(card, i);
1737 /* remove and free each DAI */
1738 soc_remove_dai_links(card);
1740 soc_cleanup_card_debugfs(card);
1742 /* remove the card */
1746 snd_soc_dapm_free(&card->dapm);
1748 snd_card_free(card->snd_card);
1753 /* removes a socdev */
1754 static int soc_remove(struct platform_device *pdev)
1756 struct snd_soc_card *card = platform_get_drvdata(pdev);
1758 snd_soc_unregister_card(card);
1762 int snd_soc_poweroff(struct device *dev)
1764 struct snd_soc_card *card = dev_get_drvdata(dev);
1767 if (!card->instantiated)
1770 /* Flush out pmdown_time work - we actually do want to run it
1771 * now, we're shutting down so no imminent restart. */
1772 for (i = 0; i < card->num_rtd; i++) {
1773 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1774 flush_delayed_work(&rtd->delayed_work);
1777 snd_soc_dapm_shutdown(card);
1779 /* deactivate pins to sleep state */
1780 for (i = 0; i < card->num_rtd; i++) {
1781 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1782 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1785 pinctrl_pm_select_sleep_state(cpu_dai->dev);
1786 for (j = 0; j < rtd->num_codecs; j++) {
1787 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
1788 pinctrl_pm_select_sleep_state(codec_dai->dev);
1794 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1796 const struct dev_pm_ops snd_soc_pm_ops = {
1797 .suspend = snd_soc_suspend,
1798 .resume = snd_soc_resume,
1799 .freeze = snd_soc_suspend,
1800 .thaw = snd_soc_resume,
1801 .poweroff = snd_soc_poweroff,
1802 .restore = snd_soc_resume,
1804 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1806 /* ASoC platform driver */
1807 static struct platform_driver soc_driver = {
1809 .name = "soc-audio",
1810 .owner = THIS_MODULE,
1811 .pm = &snd_soc_pm_ops,
1814 .remove = soc_remove,
1818 * snd_soc_cnew - create new control
1819 * @_template: control template
1820 * @data: control private data
1821 * @long_name: control long name
1822 * @prefix: control name prefix
1824 * Create a new mixer control from a template control.
1826 * Returns 0 for success, else error.
1828 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1829 void *data, const char *long_name,
1832 struct snd_kcontrol_new template;
1833 struct snd_kcontrol *kcontrol;
1836 memcpy(&template, _template, sizeof(template));
1840 long_name = template.name;
1843 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
1847 template.name = name;
1849 template.name = long_name;
1852 kcontrol = snd_ctl_new1(&template, data);
1858 EXPORT_SYMBOL_GPL(snd_soc_cnew);
1860 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
1861 const struct snd_kcontrol_new *controls, int num_controls,
1862 const char *prefix, void *data)
1866 for (i = 0; i < num_controls; i++) {
1867 const struct snd_kcontrol_new *control = &controls[i];
1868 err = snd_ctl_add(card, snd_soc_cnew(control, data,
1869 control->name, prefix));
1871 dev_err(dev, "ASoC: Failed to add %s: %d\n",
1872 control->name, err);
1880 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
1883 struct snd_card *card = soc_card->snd_card;
1884 struct snd_kcontrol *kctl;
1886 if (unlikely(!name))
1889 list_for_each_entry(kctl, &card->controls, list)
1890 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
1894 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
1897 * snd_soc_add_component_controls - Add an array of controls to a component.
1899 * @component: Component to add controls to
1900 * @controls: Array of controls to add
1901 * @num_controls: Number of elements in the array
1903 * Return: 0 for success, else error.
1905 int snd_soc_add_component_controls(struct snd_soc_component *component,
1906 const struct snd_kcontrol_new *controls, unsigned int num_controls)
1908 struct snd_card *card = component->card->snd_card;
1910 return snd_soc_add_controls(card, component->dev, controls,
1911 num_controls, component->name_prefix, component);
1913 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
1916 * snd_soc_add_codec_controls - add an array of controls to a codec.
1917 * Convenience function to add a list of controls. Many codecs were
1918 * duplicating this code.
1920 * @codec: codec to add controls to
1921 * @controls: array of controls to add
1922 * @num_controls: number of elements in the array
1924 * Return 0 for success, else error.
1926 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
1927 const struct snd_kcontrol_new *controls, unsigned int num_controls)
1929 return snd_soc_add_component_controls(&codec->component, controls,
1932 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
1935 * snd_soc_add_platform_controls - add an array of controls to a platform.
1936 * Convenience function to add a list of controls.
1938 * @platform: platform to add controls to
1939 * @controls: array of controls to add
1940 * @num_controls: number of elements in the array
1942 * Return 0 for success, else error.
1944 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
1945 const struct snd_kcontrol_new *controls, unsigned int num_controls)
1947 return snd_soc_add_component_controls(&platform->component, controls,
1950 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
1953 * snd_soc_add_card_controls - add an array of controls to a SoC card.
1954 * Convenience function to add a list of controls.
1956 * @soc_card: SoC card to add controls to
1957 * @controls: array of controls to add
1958 * @num_controls: number of elements in the array
1960 * Return 0 for success, else error.
1962 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
1963 const struct snd_kcontrol_new *controls, int num_controls)
1965 struct snd_card *card = soc_card->snd_card;
1967 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
1970 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
1973 * snd_soc_add_dai_controls - add an array of controls to a DAI.
1974 * Convienience function to add a list of controls.
1976 * @dai: DAI to add controls to
1977 * @controls: array of controls to add
1978 * @num_controls: number of elements in the array
1980 * Return 0 for success, else error.
1982 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
1983 const struct snd_kcontrol_new *controls, int num_controls)
1985 struct snd_card *card = dai->card->snd_card;
1987 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
1990 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
1993 * snd_soc_info_enum_double - enumerated double mixer info callback
1994 * @kcontrol: mixer control
1995 * @uinfo: control element information
1997 * Callback to provide information about a double enumerated
2000 * Returns 0 for success.
2002 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2003 struct snd_ctl_elem_info *uinfo)
2005 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2007 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2008 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2009 uinfo->value.enumerated.items = e->items;
2011 if (uinfo->value.enumerated.item >= e->items)
2012 uinfo->value.enumerated.item = e->items - 1;
2013 strlcpy(uinfo->value.enumerated.name,
2014 e->texts[uinfo->value.enumerated.item],
2015 sizeof(uinfo->value.enumerated.name));
2018 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2021 * snd_soc_get_enum_double - enumerated double mixer get callback
2022 * @kcontrol: mixer control
2023 * @ucontrol: control element information
2025 * Callback to get the value of a double enumerated mixer.
2027 * Returns 0 for success.
2029 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2030 struct snd_ctl_elem_value *ucontrol)
2032 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2033 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2034 unsigned int val, item;
2035 unsigned int reg_val;
2038 ret = snd_soc_component_read(component, e->reg, ®_val);
2041 val = (reg_val >> e->shift_l) & e->mask;
2042 item = snd_soc_enum_val_to_item(e, val);
2043 ucontrol->value.enumerated.item[0] = item;
2044 if (e->shift_l != e->shift_r) {
2045 val = (reg_val >> e->shift_l) & e->mask;
2046 item = snd_soc_enum_val_to_item(e, val);
2047 ucontrol->value.enumerated.item[1] = item;
2052 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2055 * snd_soc_put_enum_double - enumerated double mixer put callback
2056 * @kcontrol: mixer control
2057 * @ucontrol: control element information
2059 * Callback to set the value of a double enumerated mixer.
2061 * Returns 0 for success.
2063 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2064 struct snd_ctl_elem_value *ucontrol)
2066 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2067 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2068 unsigned int *item = ucontrol->value.enumerated.item;
2072 if (item[0] >= e->items)
2074 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
2075 mask = e->mask << e->shift_l;
2076 if (e->shift_l != e->shift_r) {
2077 if (item[1] >= e->items)
2079 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
2080 mask |= e->mask << e->shift_r;
2083 return snd_soc_component_update_bits(component, e->reg, mask, val);
2085 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2088 * snd_soc_read_signed - Read a codec register and interprete as signed value
2089 * @component: component
2090 * @reg: Register to read
2091 * @mask: Mask to use after shifting the register value
2092 * @shift: Right shift of register value
2093 * @sign_bit: Bit that describes if a number is negative or not.
2094 * @signed_val: Pointer to where the read value should be stored
2096 * This functions reads a codec register. The register value is shifted right
2097 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2098 * the given registervalue into a signed integer if sign_bit is non-zero.
2100 * Returns 0 on sucess, otherwise an error value
2102 static int snd_soc_read_signed(struct snd_soc_component *component,
2103 unsigned int reg, unsigned int mask, unsigned int shift,
2104 unsigned int sign_bit, int *signed_val)
2109 ret = snd_soc_component_read(component, reg, &val);
2113 val = (val >> shift) & mask;
2120 /* non-negative number */
2121 if (!(val & BIT(sign_bit))) {
2129 * The register most probably does not contain a full-sized int.
2130 * Instead we have an arbitrary number of bits in a signed
2131 * representation which has to be translated into a full-sized int.
2132 * This is done by filling up all bits above the sign-bit.
2134 ret |= ~((int)(BIT(sign_bit) - 1));
2142 * snd_soc_info_volsw - single mixer info callback
2143 * @kcontrol: mixer control
2144 * @uinfo: control element information
2146 * Callback to provide information about a single mixer control, or a double
2147 * mixer control that spans 2 registers.
2149 * Returns 0 for success.
2151 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2152 struct snd_ctl_elem_info *uinfo)
2154 struct soc_mixer_control *mc =
2155 (struct soc_mixer_control *)kcontrol->private_value;
2158 if (!mc->platform_max)
2159 mc->platform_max = mc->max;
2160 platform_max = mc->platform_max;
2162 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2163 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2165 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2167 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2168 uinfo->value.integer.min = 0;
2169 uinfo->value.integer.max = platform_max - mc->min;
2172 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2175 * snd_soc_get_volsw - single mixer get callback
2176 * @kcontrol: mixer control
2177 * @ucontrol: control element information
2179 * Callback to get the value of a single mixer control, or a double mixer
2180 * control that spans 2 registers.
2182 * Returns 0 for success.
2184 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2185 struct snd_ctl_elem_value *ucontrol)
2187 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2188 struct soc_mixer_control *mc =
2189 (struct soc_mixer_control *)kcontrol->private_value;
2190 unsigned int reg = mc->reg;
2191 unsigned int reg2 = mc->rreg;
2192 unsigned int shift = mc->shift;
2193 unsigned int rshift = mc->rshift;
2196 int sign_bit = mc->sign_bit;
2197 unsigned int mask = (1 << fls(max)) - 1;
2198 unsigned int invert = mc->invert;
2203 mask = BIT(sign_bit + 1) - 1;
2205 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2209 ucontrol->value.integer.value[0] = val - min;
2211 ucontrol->value.integer.value[0] =
2212 max - ucontrol->value.integer.value[0];
2214 if (snd_soc_volsw_is_stereo(mc)) {
2216 ret = snd_soc_read_signed(component, reg, mask, rshift,
2219 ret = snd_soc_read_signed(component, reg2, mask, shift,
2224 ucontrol->value.integer.value[1] = val - min;
2226 ucontrol->value.integer.value[1] =
2227 max - ucontrol->value.integer.value[1];
2232 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2235 * snd_soc_put_volsw - single mixer put callback
2236 * @kcontrol: mixer control
2237 * @ucontrol: control element information
2239 * Callback to set the value of a single mixer control, or a double mixer
2240 * control that spans 2 registers.
2242 * Returns 0 for success.
2244 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2245 struct snd_ctl_elem_value *ucontrol)
2247 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2248 struct soc_mixer_control *mc =
2249 (struct soc_mixer_control *)kcontrol->private_value;
2250 unsigned int reg = mc->reg;
2251 unsigned int reg2 = mc->rreg;
2252 unsigned int shift = mc->shift;
2253 unsigned int rshift = mc->rshift;
2256 unsigned int sign_bit = mc->sign_bit;
2257 unsigned int mask = (1 << fls(max)) - 1;
2258 unsigned int invert = mc->invert;
2260 bool type_2r = false;
2261 unsigned int val2 = 0;
2262 unsigned int val, val_mask;
2265 mask = BIT(sign_bit + 1) - 1;
2267 val = ((ucontrol->value.integer.value[0] + min) & mask);
2270 val_mask = mask << shift;
2272 if (snd_soc_volsw_is_stereo(mc)) {
2273 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
2277 val_mask |= mask << rshift;
2278 val |= val2 << rshift;
2280 val2 = val2 << shift;
2284 err = snd_soc_component_update_bits(component, reg, val_mask, val);
2289 err = snd_soc_component_update_bits(component, reg2, val_mask,
2294 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2297 * snd_soc_get_volsw_sx - single mixer get callback
2298 * @kcontrol: mixer control
2299 * @ucontrol: control element information
2301 * Callback to get the value of a single mixer control, or a double mixer
2302 * control that spans 2 registers.
2304 * Returns 0 for success.
2306 int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2307 struct snd_ctl_elem_value *ucontrol)
2309 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2310 struct soc_mixer_control *mc =
2311 (struct soc_mixer_control *)kcontrol->private_value;
2312 unsigned int reg = mc->reg;
2313 unsigned int reg2 = mc->rreg;
2314 unsigned int shift = mc->shift;
2315 unsigned int rshift = mc->rshift;
2318 int mask = (1 << (fls(min + max) - 1)) - 1;
2322 ret = snd_soc_component_read(component, reg, &val);
2326 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2328 if (snd_soc_volsw_is_stereo(mc)) {
2329 ret = snd_soc_component_read(component, reg2, &val);
2333 val = ((val >> rshift) - min) & mask;
2334 ucontrol->value.integer.value[1] = val;
2339 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2342 * snd_soc_put_volsw_sx - double mixer set callback
2343 * @kcontrol: mixer control
2344 * @uinfo: control element information
2346 * Callback to set the value of a double mixer control that spans 2 registers.
2348 * Returns 0 for success.
2350 int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2351 struct snd_ctl_elem_value *ucontrol)
2353 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2354 struct soc_mixer_control *mc =
2355 (struct soc_mixer_control *)kcontrol->private_value;
2357 unsigned int reg = mc->reg;
2358 unsigned int reg2 = mc->rreg;
2359 unsigned int shift = mc->shift;
2360 unsigned int rshift = mc->rshift;
2363 int mask = (1 << (fls(min + max) - 1)) - 1;
2365 unsigned int val, val_mask, val2 = 0;
2367 val_mask = mask << shift;
2368 val = (ucontrol->value.integer.value[0] + min) & mask;
2371 err = snd_soc_component_update_bits(component, reg, val_mask, val);
2375 if (snd_soc_volsw_is_stereo(mc)) {
2376 val_mask = mask << rshift;
2377 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2378 val2 = val2 << rshift;
2380 err = snd_soc_component_update_bits(component, reg2, val_mask,
2385 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2388 * snd_soc_info_volsw_s8 - signed mixer info callback
2389 * @kcontrol: mixer control
2390 * @uinfo: control element information
2392 * Callback to provide information about a signed mixer control.
2394 * Returns 0 for success.
2396 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2397 struct snd_ctl_elem_info *uinfo)
2399 struct soc_mixer_control *mc =
2400 (struct soc_mixer_control *)kcontrol->private_value;
2404 if (!mc->platform_max)
2405 mc->platform_max = mc->max;
2406 platform_max = mc->platform_max;
2408 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2410 uinfo->value.integer.min = 0;
2411 uinfo->value.integer.max = platform_max - min;
2414 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2417 * snd_soc_get_volsw_s8 - signed mixer get callback
2418 * @kcontrol: mixer control
2419 * @ucontrol: control element information
2421 * Callback to get the value of a signed mixer control.
2423 * Returns 0 for success.
2425 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2426 struct snd_ctl_elem_value *ucontrol)
2428 struct soc_mixer_control *mc =
2429 (struct soc_mixer_control *)kcontrol->private_value;
2430 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2431 unsigned int reg = mc->reg;
2436 ret = snd_soc_component_read(component, reg, &val);
2440 ucontrol->value.integer.value[0] =
2441 ((signed char)(val & 0xff))-min;
2442 ucontrol->value.integer.value[1] =
2443 ((signed char)((val >> 8) & 0xff))-min;
2446 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2449 * snd_soc_put_volsw_sgn - signed mixer put callback
2450 * @kcontrol: mixer control
2451 * @ucontrol: control element information
2453 * Callback to set the value of a signed mixer control.
2455 * Returns 0 for success.
2457 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2458 struct snd_ctl_elem_value *ucontrol)
2460 struct soc_mixer_control *mc =
2461 (struct soc_mixer_control *)kcontrol->private_value;
2462 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2463 unsigned int reg = mc->reg;
2467 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2468 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2470 return snd_soc_component_update_bits(component, reg, 0xffff, val);
2472 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2475 * snd_soc_info_volsw_range - single mixer info callback with range.
2476 * @kcontrol: mixer control
2477 * @uinfo: control element information
2479 * Callback to provide information, within a range, about a single
2482 * returns 0 for success.
2484 int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2485 struct snd_ctl_elem_info *uinfo)
2487 struct soc_mixer_control *mc =
2488 (struct soc_mixer_control *)kcontrol->private_value;
2492 if (!mc->platform_max)
2493 mc->platform_max = mc->max;
2494 platform_max = mc->platform_max;
2496 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2497 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2498 uinfo->value.integer.min = 0;
2499 uinfo->value.integer.max = platform_max - min;
2503 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2506 * snd_soc_put_volsw_range - single mixer put value callback with range.
2507 * @kcontrol: mixer control
2508 * @ucontrol: control element information
2510 * Callback to set the value, within a range, for a single mixer control.
2512 * Returns 0 for success.
2514 int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2515 struct snd_ctl_elem_value *ucontrol)
2517 struct soc_mixer_control *mc =
2518 (struct soc_mixer_control *)kcontrol->private_value;
2519 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2520 unsigned int reg = mc->reg;
2521 unsigned int rreg = mc->rreg;
2522 unsigned int shift = mc->shift;
2525 unsigned int mask = (1 << fls(max)) - 1;
2526 unsigned int invert = mc->invert;
2527 unsigned int val, val_mask;
2531 val = (max - ucontrol->value.integer.value[0]) & mask;
2533 val = ((ucontrol->value.integer.value[0] + min) & mask);
2534 val_mask = mask << shift;
2537 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
2541 if (snd_soc_volsw_is_stereo(mc)) {
2543 val = (max - ucontrol->value.integer.value[1]) & mask;
2545 val = ((ucontrol->value.integer.value[1] + min) & mask);
2546 val_mask = mask << shift;
2549 ret = snd_soc_component_update_bits(component, rreg, val_mask,
2555 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2558 * snd_soc_get_volsw_range - single mixer get callback with range
2559 * @kcontrol: mixer control
2560 * @ucontrol: control element information
2562 * Callback to get the value, within a range, of a single mixer control.
2564 * Returns 0 for success.
2566 int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2567 struct snd_ctl_elem_value *ucontrol)
2569 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2570 struct soc_mixer_control *mc =
2571 (struct soc_mixer_control *)kcontrol->private_value;
2572 unsigned int reg = mc->reg;
2573 unsigned int rreg = mc->rreg;
2574 unsigned int shift = mc->shift;
2577 unsigned int mask = (1 << fls(max)) - 1;
2578 unsigned int invert = mc->invert;
2582 ret = snd_soc_component_read(component, reg, &val);
2586 ucontrol->value.integer.value[0] = (val >> shift) & mask;
2588 ucontrol->value.integer.value[0] =
2589 max - ucontrol->value.integer.value[0];
2591 ucontrol->value.integer.value[0] =
2592 ucontrol->value.integer.value[0] - min;
2594 if (snd_soc_volsw_is_stereo(mc)) {
2595 ret = snd_soc_component_read(component, rreg, &val);
2599 ucontrol->value.integer.value[1] = (val >> shift) & mask;
2601 ucontrol->value.integer.value[1] =
2602 max - ucontrol->value.integer.value[1];
2604 ucontrol->value.integer.value[1] =
2605 ucontrol->value.integer.value[1] - min;
2610 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
2613 * snd_soc_limit_volume - Set new limit to an existing volume control.
2615 * @codec: where to look for the control
2616 * @name: Name of the control
2617 * @max: new maximum limit
2619 * Return 0 for success, else error.
2621 int snd_soc_limit_volume(struct snd_soc_codec *codec,
2622 const char *name, int max)
2624 struct snd_card *card = codec->component.card->snd_card;
2625 struct snd_kcontrol *kctl;
2626 struct soc_mixer_control *mc;
2630 /* Sanity check for name and max */
2631 if (unlikely(!name || max <= 0))
2634 list_for_each_entry(kctl, &card->controls, list) {
2635 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2641 mc = (struct soc_mixer_control *)kctl->private_value;
2642 if (max <= mc->max) {
2643 mc->platform_max = max;
2649 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2651 int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
2652 struct snd_ctl_elem_info *uinfo)
2654 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2655 struct soc_bytes *params = (void *)kcontrol->private_value;
2657 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
2658 uinfo->count = params->num_regs * component->val_bytes;
2662 EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
2664 int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
2665 struct snd_ctl_elem_value *ucontrol)
2667 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2668 struct soc_bytes *params = (void *)kcontrol->private_value;
2671 if (component->regmap)
2672 ret = regmap_raw_read(component->regmap, params->base,
2673 ucontrol->value.bytes.data,
2674 params->num_regs * component->val_bytes);
2678 /* Hide any masked bytes to ensure consistent data reporting */
2679 if (ret == 0 && params->mask) {
2680 switch (component->val_bytes) {
2682 ucontrol->value.bytes.data[0] &= ~params->mask;
2685 ((u16 *)(&ucontrol->value.bytes.data))[0]
2686 &= cpu_to_be16(~params->mask);
2689 ((u32 *)(&ucontrol->value.bytes.data))[0]
2690 &= cpu_to_be32(~params->mask);
2699 EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
2701 int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
2702 struct snd_ctl_elem_value *ucontrol)
2704 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2705 struct soc_bytes *params = (void *)kcontrol->private_value;
2707 unsigned int val, mask;
2710 if (!component->regmap || !params->num_regs)
2713 len = params->num_regs * component->val_bytes;
2715 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
2720 * If we've got a mask then we need to preserve the register
2721 * bits. We shouldn't modify the incoming data so take a
2725 ret = regmap_read(component->regmap, params->base, &val);
2729 val &= params->mask;
2731 switch (component->val_bytes) {
2733 ((u8 *)data)[0] &= ~params->mask;
2734 ((u8 *)data)[0] |= val;
2737 mask = ~params->mask;
2738 ret = regmap_parse_val(component->regmap,
2743 ((u16 *)data)[0] &= mask;
2745 ret = regmap_parse_val(component->regmap,
2750 ((u16 *)data)[0] |= val;
2753 mask = ~params->mask;
2754 ret = regmap_parse_val(component->regmap,
2759 ((u32 *)data)[0] &= mask;
2761 ret = regmap_parse_val(component->regmap,
2766 ((u32 *)data)[0] |= val;
2774 ret = regmap_raw_write(component->regmap, params->base,
2782 EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
2784 int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
2785 struct snd_ctl_elem_info *ucontrol)
2787 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
2789 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
2790 ucontrol->count = params->max;
2794 EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
2796 int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
2797 unsigned int size, unsigned int __user *tlv)
2799 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
2800 unsigned int count = size < params->max ? size : params->max;
2804 case SNDRV_CTL_TLV_OP_READ:
2806 ret = params->get(tlv, count);
2808 case SNDRV_CTL_TLV_OP_WRITE:
2810 ret = params->put(tlv, count);
2815 EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
2818 * snd_soc_info_xr_sx - signed multi register info callback
2819 * @kcontrol: mreg control
2820 * @uinfo: control element information
2822 * Callback to provide information of a control that can
2823 * span multiple codec registers which together
2824 * forms a single signed value in a MSB/LSB manner.
2826 * Returns 0 for success.
2828 int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
2829 struct snd_ctl_elem_info *uinfo)
2831 struct soc_mreg_control *mc =
2832 (struct soc_mreg_control *)kcontrol->private_value;
2833 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2835 uinfo->value.integer.min = mc->min;
2836 uinfo->value.integer.max = mc->max;
2840 EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
2843 * snd_soc_get_xr_sx - signed multi register get callback
2844 * @kcontrol: mreg control
2845 * @ucontrol: control element information
2847 * Callback to get the value of a control that can span
2848 * multiple codec registers which together forms a single
2849 * signed value in a MSB/LSB manner. The control supports
2850 * specifying total no of bits used to allow for bitfields
2851 * across the multiple codec registers.
2853 * Returns 0 for success.
2855 int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
2856 struct snd_ctl_elem_value *ucontrol)
2858 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2859 struct soc_mreg_control *mc =
2860 (struct soc_mreg_control *)kcontrol->private_value;
2861 unsigned int regbase = mc->regbase;
2862 unsigned int regcount = mc->regcount;
2863 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
2864 unsigned int regwmask = (1<<regwshift)-1;
2865 unsigned int invert = mc->invert;
2866 unsigned long mask = (1UL<<mc->nbits)-1;
2870 unsigned int regval;
2874 for (i = 0; i < regcount; i++) {
2875 ret = snd_soc_component_read(component, regbase+i, ®val);
2878 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
2881 if (min < 0 && val > max)
2885 ucontrol->value.integer.value[0] = val;
2889 EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
2892 * snd_soc_put_xr_sx - signed multi register get callback
2893 * @kcontrol: mreg control
2894 * @ucontrol: control element information
2896 * Callback to set the value of a control that can span
2897 * multiple codec registers which together forms a single
2898 * signed value in a MSB/LSB manner. The control supports
2899 * specifying total no of bits used to allow for bitfields
2900 * across the multiple codec registers.
2902 * Returns 0 for success.
2904 int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
2905 struct snd_ctl_elem_value *ucontrol)
2907 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2908 struct soc_mreg_control *mc =
2909 (struct soc_mreg_control *)kcontrol->private_value;
2910 unsigned int regbase = mc->regbase;
2911 unsigned int regcount = mc->regcount;
2912 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
2913 unsigned int regwmask = (1<<regwshift)-1;
2914 unsigned int invert = mc->invert;
2915 unsigned long mask = (1UL<<mc->nbits)-1;
2917 long val = ucontrol->value.integer.value[0];
2918 unsigned int i, regval, regmask;
2924 for (i = 0; i < regcount; i++) {
2925 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
2926 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
2927 err = snd_soc_component_update_bits(component, regbase+i,
2935 EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
2938 * snd_soc_get_strobe - strobe get callback
2939 * @kcontrol: mixer control
2940 * @ucontrol: control element information
2942 * Callback get the value of a strobe mixer control.
2944 * Returns 0 for success.
2946 int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
2947 struct snd_ctl_elem_value *ucontrol)
2949 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2950 struct soc_mixer_control *mc =
2951 (struct soc_mixer_control *)kcontrol->private_value;
2952 unsigned int reg = mc->reg;
2953 unsigned int shift = mc->shift;
2954 unsigned int mask = 1 << shift;
2955 unsigned int invert = mc->invert != 0;
2959 ret = snd_soc_component_read(component, reg, &val);
2965 if (shift != 0 && val != 0)
2967 ucontrol->value.enumerated.item[0] = val ^ invert;
2971 EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
2974 * snd_soc_put_strobe - strobe put callback
2975 * @kcontrol: mixer control
2976 * @ucontrol: control element information
2978 * Callback strobe a register bit to high then low (or the inverse)
2979 * in one pass of a single mixer enum control.
2981 * Returns 1 for success.
2983 int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
2984 struct snd_ctl_elem_value *ucontrol)
2986 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2987 struct soc_mixer_control *mc =
2988 (struct soc_mixer_control *)kcontrol->private_value;
2989 unsigned int reg = mc->reg;
2990 unsigned int shift = mc->shift;
2991 unsigned int mask = 1 << shift;
2992 unsigned int invert = mc->invert != 0;
2993 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
2994 unsigned int val1 = (strobe ^ invert) ? mask : 0;
2995 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
2998 err = snd_soc_component_update_bits(component, reg, mask, val1);
3002 return snd_soc_component_update_bits(component, reg, mask, val2);
3004 EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3007 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3009 * @clk_id: DAI specific clock ID
3010 * @freq: new clock frequency in Hz
3011 * @dir: new clock direction - input/output.
3013 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3015 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3016 unsigned int freq, int dir)
3018 if (dai->driver && dai->driver->ops->set_sysclk)
3019 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3020 else if (dai->codec && dai->codec->driver->set_sysclk)
3021 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3026 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3029 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3031 * @clk_id: DAI specific clock ID
3032 * @source: Source for the clock
3033 * @freq: new clock frequency in Hz
3034 * @dir: new clock direction - input/output.
3036 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3038 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3039 int source, unsigned int freq, int dir)
3041 if (codec->driver->set_sysclk)
3042 return codec->driver->set_sysclk(codec, clk_id, source,
3047 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3050 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3052 * @div_id: DAI specific clock divider ID
3053 * @div: new clock divisor.
3055 * Configures the clock dividers. This is used to derive the best DAI bit and
3056 * frame clocks from the system or master clock. It's best to set the DAI bit
3057 * and frame clocks as low as possible to save system power.
3059 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3060 int div_id, int div)
3062 if (dai->driver && dai->driver->ops->set_clkdiv)
3063 return dai->driver->ops->set_clkdiv(dai, div_id, div);
3067 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3070 * snd_soc_dai_set_pll - configure DAI PLL.
3072 * @pll_id: DAI specific PLL ID
3073 * @source: DAI specific source for the PLL
3074 * @freq_in: PLL input clock frequency in Hz
3075 * @freq_out: requested PLL output clock frequency in Hz
3077 * Configures and enables PLL to generate output clock based on input clock.
3079 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3080 unsigned int freq_in, unsigned int freq_out)
3082 if (dai->driver && dai->driver->ops->set_pll)
3083 return dai->driver->ops->set_pll(dai, pll_id, source,
3085 else if (dai->codec && dai->codec->driver->set_pll)
3086 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3091 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3094 * snd_soc_codec_set_pll - configure codec PLL.
3096 * @pll_id: DAI specific PLL ID
3097 * @source: DAI specific source for the PLL
3098 * @freq_in: PLL input clock frequency in Hz
3099 * @freq_out: requested PLL output clock frequency in Hz
3101 * Configures and enables PLL to generate output clock based on input clock.
3103 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3104 unsigned int freq_in, unsigned int freq_out)
3106 if (codec->driver->set_pll)
3107 return codec->driver->set_pll(codec, pll_id, source,
3112 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3115 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3117 * @ratio Ratio of BCLK to Sample rate.
3119 * Configures the DAI for a preset BCLK to sample rate ratio.
3121 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3123 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3124 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3128 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3131 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3133 * @fmt: SND_SOC_DAIFMT_ format value.
3135 * Configures the DAI hardware format and clocking.
3137 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3139 if (dai->driver == NULL)
3141 if (dai->driver->ops->set_fmt == NULL)
3143 return dai->driver->ops->set_fmt(dai, fmt);
3145 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3148 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
3149 * @slots: Number of slots in use.
3150 * @tx_mask: bitmask representing active TX slots.
3151 * @rx_mask: bitmask representing active RX slots.
3153 * Generates the TDM tx and rx slot default masks for DAI.
3155 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
3156 unsigned int *tx_mask,
3157 unsigned int *rx_mask)
3159 if (*tx_mask || *rx_mask)
3165 *tx_mask = (1 << slots) - 1;
3166 *rx_mask = (1 << slots) - 1;
3172 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3174 * @tx_mask: bitmask representing active TX slots.
3175 * @rx_mask: bitmask representing active RX slots.
3176 * @slots: Number of slots in use.
3177 * @slot_width: Width in bits for each slot.
3179 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3182 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3183 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
3185 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3186 dai->driver->ops->xlate_tdm_slot_mask(slots,
3187 &tx_mask, &rx_mask);
3189 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
3191 dai->tx_mask = tx_mask;
3192 dai->rx_mask = rx_mask;
3194 if (dai->driver && dai->driver->ops->set_tdm_slot)
3195 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3200 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3203 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3205 * @tx_num: how many TX channels
3206 * @tx_slot: pointer to an array which imply the TX slot number channel
3208 * @rx_num: how many RX channels
3209 * @rx_slot: pointer to an array which imply the RX slot number channel
3212 * configure the relationship between channel number and TDM slot number.
3214 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3215 unsigned int tx_num, unsigned int *tx_slot,
3216 unsigned int rx_num, unsigned int *rx_slot)
3218 if (dai->driver && dai->driver->ops->set_channel_map)
3219 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3224 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3227 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3229 * @tristate: tristate enable
3231 * Tristates the DAI so that others can use it.
3233 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3235 if (dai->driver && dai->driver->ops->set_tristate)
3236 return dai->driver->ops->set_tristate(dai, tristate);
3240 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3243 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3245 * @mute: mute enable
3246 * @direction: stream to mute
3248 * Mutes the DAI DAC.
3250 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3256 if (dai->driver->ops->mute_stream)
3257 return dai->driver->ops->mute_stream(dai, mute, direction);
3258 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3259 dai->driver->ops->digital_mute)
3260 return dai->driver->ops->digital_mute(dai, mute);
3264 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3266 static int snd_soc_init_multicodec(struct snd_soc_card *card,
3267 struct snd_soc_dai_link *dai_link)
3269 /* Legacy codec/codec_dai link is a single entry in multicodec */
3270 if (dai_link->codec_name || dai_link->codec_of_node ||
3271 dai_link->codec_dai_name) {
3272 dai_link->num_codecs = 1;
3274 dai_link->codecs = devm_kzalloc(card->dev,
3275 sizeof(struct snd_soc_dai_link_component),
3277 if (!dai_link->codecs)
3280 dai_link->codecs[0].name = dai_link->codec_name;
3281 dai_link->codecs[0].of_node = dai_link->codec_of_node;
3282 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
3285 if (!dai_link->codecs) {
3286 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
3294 * snd_soc_register_card - Register a card with the ASoC core
3296 * @card: Card to register
3299 int snd_soc_register_card(struct snd_soc_card *card)
3303 if (!card->name || !card->dev)
3306 for (i = 0; i < card->num_links; i++) {
3307 struct snd_soc_dai_link *link = &card->dai_link[i];
3309 ret = snd_soc_init_multicodec(card, link);
3311 dev_err(card->dev, "ASoC: failed to init multicodec\n");
3315 for (j = 0; j < link->num_codecs; j++) {
3317 * Codec must be specified by 1 of name or OF node,
3318 * not both or neither.
3320 if (!!link->codecs[j].name ==
3321 !!link->codecs[j].of_node) {
3322 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
3326 /* Codec DAI name must be specified */
3327 if (!link->codecs[j].dai_name) {
3328 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
3335 * Platform may be specified by either name or OF node, but
3336 * can be left unspecified, and a dummy platform will be used.
3338 if (link->platform_name && link->platform_of_node) {
3340 "ASoC: Both platform name/of_node are set for %s\n",
3346 * CPU device may be specified by either name or OF node, but
3347 * can be left unspecified, and will be matched based on DAI
3350 if (link->cpu_name && link->cpu_of_node) {
3352 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3357 * At least one of CPU DAI name or CPU device name/node must be
3360 if (!link->cpu_dai_name &&
3361 !(link->cpu_name || link->cpu_of_node)) {
3363 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3369 dev_set_drvdata(card->dev, card);
3371 snd_soc_initialize_card_lists(card);
3373 soc_init_card_debugfs(card);
3375 card->rtd = devm_kzalloc(card->dev,
3376 sizeof(struct snd_soc_pcm_runtime) *
3377 (card->num_links + card->num_aux_devs),
3379 if (card->rtd == NULL)
3382 card->rtd_aux = &card->rtd[card->num_links];
3384 for (i = 0; i < card->num_links; i++) {
3385 card->rtd[i].card = card;
3386 card->rtd[i].dai_link = &card->dai_link[i];
3387 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
3388 sizeof(struct snd_soc_dai *) *
3389 (card->rtd[i].dai_link->num_codecs),
3391 if (card->rtd[i].codec_dais == NULL)
3395 for (i = 0; i < card->num_aux_devs; i++)
3396 card->rtd_aux[i].card = card;
3398 INIT_LIST_HEAD(&card->dapm_dirty);
3399 card->instantiated = 0;
3400 mutex_init(&card->mutex);
3401 mutex_init(&card->dapm_mutex);
3403 ret = snd_soc_instantiate_card(card);
3405 soc_cleanup_card_debugfs(card);
3407 /* deactivate pins to sleep state */
3408 for (i = 0; i < card->num_rtd; i++) {
3409 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
3410 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3413 for (j = 0; j < rtd->num_codecs; j++) {
3414 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
3415 if (!codec_dai->active)
3416 pinctrl_pm_select_sleep_state(codec_dai->dev);
3419 if (!cpu_dai->active)
3420 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3425 EXPORT_SYMBOL_GPL(snd_soc_register_card);
3428 * snd_soc_unregister_card - Unregister a card with the ASoC core
3430 * @card: Card to unregister
3433 int snd_soc_unregister_card(struct snd_soc_card *card)
3435 if (card->instantiated) {
3436 card->instantiated = false;
3437 snd_soc_dapm_shutdown(card);
3438 soc_cleanup_card_resources(card);
3440 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
3444 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3447 * Simplify DAI link configuration by removing ".-1" from device names
3448 * and sanitizing names.
3450 static char *fmt_single_name(struct device *dev, int *id)
3452 char *found, name[NAME_SIZE];
3455 if (dev_name(dev) == NULL)
3458 strlcpy(name, dev_name(dev), NAME_SIZE);
3460 /* are we a "%s.%d" name (platform and SPI components) */
3461 found = strstr(name, dev->driver->name);
3464 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3466 /* discard ID from name if ID == -1 */
3468 found[strlen(dev->driver->name)] = '\0';
3472 /* I2C component devices are named "bus-addr" */
3473 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3474 char tmp[NAME_SIZE];
3476 /* create unique ID number from I2C addr and bus */
3477 *id = ((id1 & 0xffff) << 16) + id2;
3479 /* sanitize component name for DAI link creation */
3480 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3481 strlcpy(name, tmp, NAME_SIZE);
3486 return kstrdup(name, GFP_KERNEL);
3490 * Simplify DAI link naming for single devices with multiple DAIs by removing
3491 * any ".-1" and using the DAI name (instead of device name).
3493 static inline char *fmt_multiple_name(struct device *dev,
3494 struct snd_soc_dai_driver *dai_drv)
3496 if (dai_drv->name == NULL) {
3498 "ASoC: error - multiple DAI %s registered with no name\n",
3503 return kstrdup(dai_drv->name, GFP_KERNEL);
3507 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
3509 * @component: The component for which the DAIs should be unregistered
3511 static void snd_soc_unregister_dais(struct snd_soc_component *component)
3513 struct snd_soc_dai *dai, *_dai;
3515 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
3516 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3518 list_del(&dai->list);
3525 * snd_soc_register_dais - Register a DAI with the ASoC core
3527 * @component: The component the DAIs are registered for
3528 * @dai_drv: DAI driver to use for the DAIs
3529 * @count: Number of DAIs
3530 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3533 static int snd_soc_register_dais(struct snd_soc_component *component,
3534 struct snd_soc_dai_driver *dai_drv, size_t count,
3535 bool legacy_dai_naming)
3537 struct device *dev = component->dev;
3538 struct snd_soc_dai *dai;
3542 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
3544 component->dai_drv = dai_drv;
3545 component->num_dai = count;
3547 for (i = 0; i < count; i++) {
3549 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3556 * Back in the old days when we still had component-less DAIs,
3557 * instead of having a static name, component-less DAIs would
3558 * inherit the name of the parent device so it is possible to
3559 * register multiple instances of the DAI. We still need to keep
3560 * the same naming style even though those DAIs are not
3561 * component-less anymore.
3563 if (count == 1 && legacy_dai_naming) {
3564 dai->name = fmt_single_name(dev, &dai->id);
3566 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3568 dai->id = dai_drv[i].id;
3572 if (dai->name == NULL) {
3578 dai->component = component;
3580 dai->driver = &dai_drv[i];
3581 if (!dai->driver->ops)
3582 dai->driver->ops = &null_dai_ops;
3584 list_add(&dai->list, &component->dai_list);
3586 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3592 snd_soc_unregister_dais(component);
3597 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3598 enum snd_soc_dapm_type type, int subseq)
3600 struct snd_soc_component *component = dapm->component;
3602 component->driver->seq_notifier(component, type, subseq);
3605 static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3608 struct snd_soc_component *component = dapm->component;
3610 return component->driver->stream_event(component, event);
3613 static int snd_soc_component_initialize(struct snd_soc_component *component,
3614 const struct snd_soc_component_driver *driver, struct device *dev)
3616 struct snd_soc_dapm_context *dapm;
3618 component->name = fmt_single_name(dev, &component->id);
3619 if (!component->name) {
3620 dev_err(dev, "ASoC: Failed to allocate name\n");
3624 component->dev = dev;
3625 component->driver = driver;
3626 component->probe = component->driver->probe;
3627 component->remove = component->driver->remove;
3629 if (!component->dapm_ptr)
3630 component->dapm_ptr = &component->dapm;
3632 dapm = component->dapm_ptr;
3634 dapm->component = component;
3635 dapm->bias_level = SND_SOC_BIAS_OFF;
3636 dapm->idle_bias_off = true;
3637 if (driver->seq_notifier)
3638 dapm->seq_notifier = snd_soc_component_seq_notifier;
3639 if (driver->stream_event)
3640 dapm->stream_event = snd_soc_component_stream_event;
3642 component->controls = driver->controls;
3643 component->num_controls = driver->num_controls;
3644 component->dapm_widgets = driver->dapm_widgets;
3645 component->num_dapm_widgets = driver->num_dapm_widgets;
3646 component->dapm_routes = driver->dapm_routes;
3647 component->num_dapm_routes = driver->num_dapm_routes;
3649 INIT_LIST_HEAD(&component->dai_list);
3650 mutex_init(&component->io_mutex);
3655 static void snd_soc_component_init_regmap(struct snd_soc_component *component)
3657 if (!component->regmap)
3658 component->regmap = dev_get_regmap(component->dev, NULL);
3659 if (component->regmap) {
3660 int val_bytes = regmap_get_val_bytes(component->regmap);
3661 /* Errors are legitimate for non-integer byte multiples */
3663 component->val_bytes = val_bytes;
3667 static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
3669 if (!component->write && !component->read)
3670 snd_soc_component_init_regmap(component);
3672 list_add(&component->list, &component_list);
3675 static void snd_soc_component_add(struct snd_soc_component *component)
3677 mutex_lock(&client_mutex);
3678 snd_soc_component_add_unlocked(component);
3679 mutex_unlock(&client_mutex);
3682 static void snd_soc_component_cleanup(struct snd_soc_component *component)
3684 snd_soc_unregister_dais(component);
3685 kfree(component->name);
3688 static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3690 list_del(&component->list);
3693 static void snd_soc_component_del(struct snd_soc_component *component)
3695 mutex_lock(&client_mutex);
3696 snd_soc_component_del_unlocked(component);
3697 mutex_unlock(&client_mutex);
3700 int snd_soc_register_component(struct device *dev,
3701 const struct snd_soc_component_driver *cmpnt_drv,
3702 struct snd_soc_dai_driver *dai_drv,
3705 struct snd_soc_component *cmpnt;
3708 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
3710 dev_err(dev, "ASoC: Failed to allocate memory\n");
3714 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
3718 cmpnt->ignore_pmdown_time = true;
3719 cmpnt->registered_as_component = true;
3721 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
3723 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
3727 snd_soc_component_add(cmpnt);
3732 snd_soc_component_cleanup(cmpnt);
3737 EXPORT_SYMBOL_GPL(snd_soc_register_component);
3740 * snd_soc_unregister_component - Unregister a component from the ASoC core
3743 void snd_soc_unregister_component(struct device *dev)
3745 struct snd_soc_component *cmpnt;
3747 list_for_each_entry(cmpnt, &component_list, list) {
3748 if (dev == cmpnt->dev && cmpnt->registered_as_component)
3754 snd_soc_component_del(cmpnt);
3755 snd_soc_component_cleanup(cmpnt);
3758 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3760 static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
3762 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3764 return platform->driver->probe(platform);
3767 static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
3769 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3771 platform->driver->remove(platform);
3775 * snd_soc_add_platform - Add a platform to the ASoC core
3776 * @dev: The parent device for the platform
3777 * @platform: The platform to add
3778 * @platform_driver: The driver for the platform
3780 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
3781 const struct snd_soc_platform_driver *platform_drv)
3785 ret = snd_soc_component_initialize(&platform->component,
3786 &platform_drv->component_driver, dev);
3790 platform->dev = dev;
3791 platform->driver = platform_drv;
3793 if (platform_drv->probe)
3794 platform->component.probe = snd_soc_platform_drv_probe;
3795 if (platform_drv->remove)
3796 platform->component.remove = snd_soc_platform_drv_remove;
3798 #ifdef CONFIG_DEBUG_FS
3799 platform->component.debugfs_prefix = "platform";
3802 mutex_lock(&client_mutex);
3803 snd_soc_component_add_unlocked(&platform->component);
3804 list_add(&platform->list, &platform_list);
3805 mutex_unlock(&client_mutex);
3807 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
3808 platform->component.name);
3812 EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3815 * snd_soc_register_platform - Register a platform with the ASoC core
3817 * @platform: platform to register
3819 int snd_soc_register_platform(struct device *dev,
3820 const struct snd_soc_platform_driver *platform_drv)
3822 struct snd_soc_platform *platform;
3825 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3827 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3828 if (platform == NULL)
3831 ret = snd_soc_add_platform(dev, platform, platform_drv);
3837 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3840 * snd_soc_remove_platform - Remove a platform from the ASoC core
3841 * @platform: the platform to remove
3843 void snd_soc_remove_platform(struct snd_soc_platform *platform)
3846 mutex_lock(&client_mutex);
3847 list_del(&platform->list);
3848 snd_soc_component_del_unlocked(&platform->component);
3849 mutex_unlock(&client_mutex);
3851 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3852 platform->component.name);
3854 snd_soc_component_cleanup(&platform->component);
3856 EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
3858 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
3860 struct snd_soc_platform *platform;
3862 list_for_each_entry(platform, &platform_list, list) {
3863 if (dev == platform->dev)
3869 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
3872 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3874 * @platform: platform to unregister
3876 void snd_soc_unregister_platform(struct device *dev)
3878 struct snd_soc_platform *platform;
3880 platform = snd_soc_lookup_platform(dev);
3884 snd_soc_remove_platform(platform);
3887 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3889 static u64 codec_format_map[] = {
3890 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3891 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3892 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3893 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3894 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3895 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3896 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3897 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3898 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3899 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3900 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3901 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3902 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3903 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3904 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3905 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3908 /* Fix up the DAI formats for endianness: codecs don't actually see
3909 * the endianness of the data but we're using the CPU format
3910 * definitions which do need to include endianness so we ensure that
3911 * codec DAIs always have both big and little endian variants set.
3913 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3917 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3918 if (stream->formats & codec_format_map[i])
3919 stream->formats |= codec_format_map[i];
3922 static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3924 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3926 return codec->driver->probe(codec);
3929 static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3931 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3933 codec->driver->remove(codec);
3936 static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3937 unsigned int reg, unsigned int val)
3939 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3941 return codec->driver->write(codec, reg, val);
3944 static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3945 unsigned int reg, unsigned int *val)
3947 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3949 *val = codec->driver->read(codec, reg);
3954 static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
3955 enum snd_soc_bias_level level)
3957 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
3959 return codec->driver->set_bias_level(codec, level);
3963 * snd_soc_register_codec - Register a codec with the ASoC core
3965 * @codec: codec to register
3967 int snd_soc_register_codec(struct device *dev,
3968 const struct snd_soc_codec_driver *codec_drv,
3969 struct snd_soc_dai_driver *dai_drv,
3972 struct snd_soc_codec *codec;
3973 struct snd_soc_dai *dai;
3976 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3978 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3982 codec->component.dapm_ptr = &codec->dapm;
3983 codec->component.codec = codec;
3985 ret = snd_soc_component_initialize(&codec->component,
3986 &codec_drv->component_driver, dev);
3990 if (codec_drv->controls) {
3991 codec->component.controls = codec_drv->controls;
3992 codec->component.num_controls = codec_drv->num_controls;
3994 if (codec_drv->dapm_widgets) {
3995 codec->component.dapm_widgets = codec_drv->dapm_widgets;
3996 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
3998 if (codec_drv->dapm_routes) {
3999 codec->component.dapm_routes = codec_drv->dapm_routes;
4000 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
4003 if (codec_drv->probe)
4004 codec->component.probe = snd_soc_codec_drv_probe;
4005 if (codec_drv->remove)
4006 codec->component.remove = snd_soc_codec_drv_remove;
4007 if (codec_drv->write)
4008 codec->component.write = snd_soc_codec_drv_write;
4009 if (codec_drv->read)
4010 codec->component.read = snd_soc_codec_drv_read;
4011 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
4012 codec->dapm.idle_bias_off = codec_drv->idle_bias_off;
4013 codec->dapm.suspend_bias_off = codec_drv->suspend_bias_off;
4014 if (codec_drv->seq_notifier)
4015 codec->dapm.seq_notifier = codec_drv->seq_notifier;
4016 if (codec_drv->set_bias_level)
4017 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
4019 codec->driver = codec_drv;
4020 codec->component.val_bytes = codec_drv->reg_word_size;
4021 mutex_init(&codec->mutex);
4023 #ifdef CONFIG_DEBUG_FS
4024 codec->component.init_debugfs = soc_init_codec_debugfs;
4025 codec->component.debugfs_prefix = "codec";
4028 if (codec_drv->get_regmap)
4029 codec->component.regmap = codec_drv->get_regmap(dev);
4031 for (i = 0; i < num_dai; i++) {
4032 fixup_codec_formats(&dai_drv[i].playback);
4033 fixup_codec_formats(&dai_drv[i].capture);
4036 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4038 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4042 list_for_each_entry(dai, &codec->component.dai_list, list)
4045 mutex_lock(&client_mutex);
4046 snd_soc_component_add_unlocked(&codec->component);
4047 list_add(&codec->list, &codec_list);
4048 mutex_unlock(&client_mutex);
4050 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4051 codec->component.name);
4055 snd_soc_component_cleanup(&codec->component);
4060 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4063 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4065 * @codec: codec to unregister
4067 void snd_soc_unregister_codec(struct device *dev)
4069 struct snd_soc_codec *codec;
4071 list_for_each_entry(codec, &codec_list, list) {
4072 if (dev == codec->dev)
4079 mutex_lock(&client_mutex);
4080 list_del(&codec->list);
4081 snd_soc_component_del_unlocked(&codec->component);
4082 mutex_unlock(&client_mutex);
4084 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4085 codec->component.name);
4087 snd_soc_component_cleanup(&codec->component);
4088 snd_soc_cache_exit(codec);
4091 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4093 /* Retrieve a card's name from device tree */
4094 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4095 const char *propname)
4097 struct device_node *np;
4101 pr_err("card->dev is not set before calling %s\n", __func__);
4105 np = card->dev->of_node;
4107 ret = of_property_read_string_index(np, propname, 0, &card->name);
4109 * EINVAL means the property does not exist. This is fine providing
4110 * card->name was previously set, which is checked later in
4111 * snd_soc_register_card.
4113 if (ret < 0 && ret != -EINVAL) {
4115 "ASoC: Property '%s' could not be read: %d\n",
4122 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4124 static const struct snd_soc_dapm_widget simple_widgets[] = {
4125 SND_SOC_DAPM_MIC("Microphone", NULL),
4126 SND_SOC_DAPM_LINE("Line", NULL),
4127 SND_SOC_DAPM_HP("Headphone", NULL),
4128 SND_SOC_DAPM_SPK("Speaker", NULL),
4131 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4132 const char *propname)
4134 struct device_node *np = card->dev->of_node;
4135 struct snd_soc_dapm_widget *widgets;
4136 const char *template, *wname;
4137 int i, j, num_widgets, ret;
4139 num_widgets = of_property_count_strings(np, propname);
4140 if (num_widgets < 0) {
4142 "ASoC: Property '%s' does not exist\n", propname);
4145 if (num_widgets & 1) {
4147 "ASoC: Property '%s' length is not even\n", propname);
4153 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4158 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4162 "ASoC: Could not allocate memory for widgets\n");
4166 for (i = 0; i < num_widgets; i++) {
4167 ret = of_property_read_string_index(np, propname,
4171 "ASoC: Property '%s' index %d read error:%d\n",
4172 propname, 2 * i, ret);
4176 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4177 if (!strncmp(template, simple_widgets[j].name,
4178 strlen(simple_widgets[j].name))) {
4179 widgets[i] = simple_widgets[j];
4184 if (j >= ARRAY_SIZE(simple_widgets)) {
4186 "ASoC: DAPM widget '%s' is not supported\n",
4191 ret = of_property_read_string_index(np, propname,
4196 "ASoC: Property '%s' index %d read error:%d\n",
4197 propname, (2 * i) + 1, ret);
4201 widgets[i].name = wname;
4204 card->dapm_widgets = widgets;
4205 card->num_dapm_widgets = num_widgets;
4209 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4211 int snd_soc_of_parse_tdm_slot(struct device_node *np,
4212 unsigned int *slots,
4213 unsigned int *slot_width)
4218 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4219 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4227 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4228 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4238 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4240 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4241 const char *propname)
4243 struct device_node *np = card->dev->of_node;
4245 struct snd_soc_dapm_route *routes;
4248 num_routes = of_property_count_strings(np, propname);
4249 if (num_routes < 0 || num_routes & 1) {
4251 "ASoC: Property '%s' does not exist or its length is not even\n",
4257 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4262 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4266 "ASoC: Could not allocate DAPM route table\n");
4270 for (i = 0; i < num_routes; i++) {
4271 ret = of_property_read_string_index(np, propname,
4272 2 * i, &routes[i].sink);
4275 "ASoC: Property '%s' index %d could not be read: %d\n",
4276 propname, 2 * i, ret);
4279 ret = of_property_read_string_index(np, propname,
4280 (2 * i) + 1, &routes[i].source);
4283 "ASoC: Property '%s' index %d could not be read: %d\n",
4284 propname, (2 * i) + 1, ret);
4289 card->num_dapm_routes = num_routes;
4290 card->dapm_routes = routes;
4294 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4296 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4298 struct device_node **bitclkmaster,
4299 struct device_node **framemaster)
4303 unsigned int format = 0;
4309 } of_fmt_table[] = {
4310 { "i2s", SND_SOC_DAIFMT_I2S },
4311 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4312 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4313 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4314 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4315 { "ac97", SND_SOC_DAIFMT_AC97 },
4316 { "pdm", SND_SOC_DAIFMT_PDM},
4317 { "msb", SND_SOC_DAIFMT_MSB },
4318 { "lsb", SND_SOC_DAIFMT_LSB },
4325 * check "[prefix]format = xxx"
4326 * SND_SOC_DAIFMT_FORMAT_MASK area
4328 snprintf(prop, sizeof(prop), "%sformat", prefix);
4329 ret = of_property_read_string(np, prop, &str);
4331 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4332 if (strcmp(str, of_fmt_table[i].name) == 0) {
4333 format |= of_fmt_table[i].val;
4340 * check "[prefix]continuous-clock"
4341 * SND_SOC_DAIFMT_CLOCK_MASK area
4343 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4344 if (of_get_property(np, prop, NULL))
4345 format |= SND_SOC_DAIFMT_CONT;
4347 format |= SND_SOC_DAIFMT_GATED;
4350 * check "[prefix]bitclock-inversion"
4351 * check "[prefix]frame-inversion"
4352 * SND_SOC_DAIFMT_INV_MASK area
4354 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4355 bit = !!of_get_property(np, prop, NULL);
4357 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4358 frame = !!of_get_property(np, prop, NULL);
4360 switch ((bit << 4) + frame) {
4362 format |= SND_SOC_DAIFMT_IB_IF;
4365 format |= SND_SOC_DAIFMT_IB_NF;
4368 format |= SND_SOC_DAIFMT_NB_IF;
4371 /* SND_SOC_DAIFMT_NB_NF is default */
4376 * check "[prefix]bitclock-master"
4377 * check "[prefix]frame-master"
4378 * SND_SOC_DAIFMT_MASTER_MASK area
4380 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4381 bit = !!of_get_property(np, prop, NULL);
4382 if (bit && bitclkmaster)
4383 *bitclkmaster = of_parse_phandle(np, prop, 0);
4385 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4386 frame = !!of_get_property(np, prop, NULL);
4387 if (frame && framemaster)
4388 *framemaster = of_parse_phandle(np, prop, 0);
4390 switch ((bit << 4) + frame) {
4392 format |= SND_SOC_DAIFMT_CBM_CFM;
4395 format |= SND_SOC_DAIFMT_CBM_CFS;
4398 format |= SND_SOC_DAIFMT_CBS_CFM;
4401 format |= SND_SOC_DAIFMT_CBS_CFS;
4407 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4409 int snd_soc_of_get_dai_name(struct device_node *of_node,
4410 const char **dai_name)
4412 struct snd_soc_component *pos;
4413 struct of_phandle_args args;
4416 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4417 "#sound-dai-cells", 0, &args);
4421 ret = -EPROBE_DEFER;
4423 mutex_lock(&client_mutex);
4424 list_for_each_entry(pos, &component_list, list) {
4425 if (pos->dev->of_node != args.np)
4428 if (pos->driver->of_xlate_dai_name) {
4429 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4433 switch (args.args_count) {
4435 id = 0; /* same as dai_drv[0] */
4445 if (id < 0 || id >= pos->num_dai) {
4452 *dai_name = pos->dai_drv[id].name;
4454 *dai_name = pos->name;
4459 mutex_unlock(&client_mutex);
4461 of_node_put(args.np);
4465 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4467 static int __init snd_soc_init(void)
4469 #ifdef CONFIG_DEBUG_FS
4470 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4471 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
4472 pr_warn("ASoC: Failed to create debugfs directory\n");
4473 snd_soc_debugfs_root = NULL;
4476 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4478 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4480 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4482 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
4484 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
4485 &platform_list_fops))
4486 pr_warn("ASoC: Failed to create platform list debugfs file\n");
4489 snd_soc_util_init();
4491 return platform_driver_register(&soc_driver);
4493 module_init(snd_soc_init);
4495 static void __exit snd_soc_exit(void)
4497 snd_soc_util_exit();
4499 #ifdef CONFIG_DEBUG_FS
4500 debugfs_remove_recursive(snd_soc_debugfs_root);
4502 platform_driver_unregister(&soc_driver);
4504 module_exit(snd_soc_exit);
4506 /* Module information */
4507 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4508 MODULE_DESCRIPTION("ALSA SoC Core");
4509 MODULE_LICENSE("GPL");
4510 MODULE_ALIAS("platform:soc-audio");