2 * soc-core.c -- ALSA SoC Audio Layer
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
30 #include <linux/bitops.h>
31 #include <linux/debugfs.h>
32 #include <linux/platform_device.h>
33 #include <linux/pinctrl/consumer.h>
34 #include <linux/ctype.h>
35 #include <linux/slab.h>
37 #include <linux/gpio.h>
38 #include <linux/of_gpio.h>
39 #include <sound/ac97_codec.h>
40 #include <sound/core.h>
41 #include <sound/jack.h>
42 #include <sound/pcm.h>
43 #include <sound/pcm_params.h>
44 #include <sound/soc.h>
45 #include <sound/soc-dpcm.h>
46 #include <sound/initval.h>
48 #define CREATE_TRACE_POINTS
49 #include <trace/events/asoc.h>
53 #ifdef CONFIG_DEBUG_FS
54 struct dentry *snd_soc_debugfs_root;
55 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
58 static DEFINE_MUTEX(client_mutex);
59 static LIST_HEAD(platform_list);
60 static LIST_HEAD(codec_list);
61 static LIST_HEAD(component_list);
64 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
68 static int pmdown_time = 5000;
69 module_param(pmdown_time, int, 0);
70 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
72 struct snd_ac97_reset_cfg {
74 struct pinctrl_state *pstate_reset;
75 struct pinctrl_state *pstate_warm_reset;
76 struct pinctrl_state *pstate_run;
82 /* returns the minimum number of bytes needed to represent
83 * a particular given value */
84 static int min_bytes_needed(unsigned long val)
89 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
92 c = (sizeof val * 8) - c;
100 /* fill buf which is 'len' bytes with a formatted
101 * string of the form 'reg: value\n' */
102 static int format_register_str(struct snd_soc_codec *codec,
103 unsigned int reg, char *buf, size_t len)
105 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
106 int regsize = codec->driver->reg_word_size * 2;
108 char tmpbuf[len + 1];
109 char regbuf[regsize + 1];
111 /* since tmpbuf is allocated on the stack, warn the callers if they
112 * try to abuse this function */
115 /* +2 for ': ' and + 1 for '\n' */
116 if (wordsize + regsize + 2 + 1 != len)
119 ret = snd_soc_read(codec, reg);
121 memset(regbuf, 'X', regsize);
122 regbuf[regsize] = '\0';
124 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
127 /* prepare the buffer */
128 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
129 /* copy it back to the caller without the '\0' */
130 memcpy(buf, tmpbuf, len);
135 /* codec register dump */
136 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
137 size_t count, loff_t pos)
140 int wordsize, regsize;
145 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
146 regsize = codec->driver->reg_word_size * 2;
148 len = wordsize + regsize + 2 + 1;
150 if (!codec->driver->reg_cache_size)
153 if (codec->driver->reg_cache_step)
154 step = codec->driver->reg_cache_step;
156 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
157 /* only support larger than PAGE_SIZE bytes debugfs
158 * entries for the default case */
160 if (total + len >= count - 1)
162 format_register_str(codec, i, buf + total, len);
168 total = min(total, count - 1);
173 static ssize_t codec_reg_show(struct device *dev,
174 struct device_attribute *attr, char *buf)
176 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
178 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
181 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
183 static ssize_t pmdown_time_show(struct device *dev,
184 struct device_attribute *attr, char *buf)
186 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
188 return sprintf(buf, "%ld\n", rtd->pmdown_time);
191 static ssize_t pmdown_time_set(struct device *dev,
192 struct device_attribute *attr,
193 const char *buf, size_t count)
195 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
198 ret = kstrtol(buf, 10, &rtd->pmdown_time);
205 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
207 #ifdef CONFIG_DEBUG_FS
208 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
209 size_t count, loff_t *ppos)
212 struct snd_soc_codec *codec = file->private_data;
215 if (*ppos < 0 || !count)
218 buf = kmalloc(count, GFP_KERNEL);
222 ret = soc_codec_reg_show(codec, buf, count, *ppos);
224 if (copy_to_user(user_buf, buf, ret)) {
235 static ssize_t codec_reg_write_file(struct file *file,
236 const char __user *user_buf, size_t count, loff_t *ppos)
241 unsigned long reg, value;
242 struct snd_soc_codec *codec = file->private_data;
245 buf_size = min(count, (sizeof(buf)-1));
246 if (copy_from_user(buf, user_buf, buf_size))
250 while (*start == ' ')
252 reg = simple_strtoul(start, &start, 16);
253 while (*start == ' ')
255 ret = kstrtoul(start, 16, &value);
259 /* Userspace has been fiddling around behind the kernel's back */
260 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
262 snd_soc_write(codec, reg, value);
266 static const struct file_operations codec_reg_fops = {
268 .read = codec_reg_read_file,
269 .write = codec_reg_write_file,
270 .llseek = default_llseek,
273 static struct dentry *soc_debugfs_create_dir(struct dentry *parent,
274 const char *fmt, ...)
281 s = kvasprintf(GFP_KERNEL, fmt, ap);
287 de = debugfs_create_dir(s, parent);
293 static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
295 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
297 codec->debugfs_codec_root = soc_debugfs_create_dir(debugfs_card_root,
298 "codec:%s", codec->name);
299 if (!codec->debugfs_codec_root) {
301 "ASoC: Failed to create codec debugfs directory\n");
305 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
307 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
310 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
311 codec->debugfs_codec_root,
312 codec, &codec_reg_fops);
313 if (!codec->debugfs_reg)
315 "ASoC: Failed to create codec register debugfs file\n");
317 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
320 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
322 debugfs_remove_recursive(codec->debugfs_codec_root);
325 static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
327 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
329 platform->debugfs_platform_root = soc_debugfs_create_dir(debugfs_card_root,
330 "platform:%s", platform->name);
331 if (!platform->debugfs_platform_root) {
332 dev_warn(platform->dev,
333 "ASoC: Failed to create platform debugfs directory\n");
337 snd_soc_dapm_debugfs_init(&platform->dapm,
338 platform->debugfs_platform_root);
341 static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
343 debugfs_remove_recursive(platform->debugfs_platform_root);
346 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
347 size_t count, loff_t *ppos)
349 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
350 ssize_t len, ret = 0;
351 struct snd_soc_codec *codec;
356 list_for_each_entry(codec, &codec_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 codec_list_fops = {
376 .read = codec_list_read_file,
377 .llseek = default_llseek,/* read accesses f_pos */
380 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
381 size_t count, loff_t *ppos)
383 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
384 ssize_t len, ret = 0;
385 struct snd_soc_component *component;
386 struct snd_soc_dai *dai;
391 list_for_each_entry(component, &component_list, list) {
392 list_for_each_entry(dai, &component->dai_list, list) {
393 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
397 if (ret > PAGE_SIZE) {
404 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
411 static const struct file_operations dai_list_fops = {
412 .read = dai_list_read_file,
413 .llseek = default_llseek,/* read accesses f_pos */
416 static ssize_t platform_list_read_file(struct file *file,
417 char __user *user_buf,
418 size_t count, loff_t *ppos)
420 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
421 ssize_t len, ret = 0;
422 struct snd_soc_platform *platform;
427 list_for_each_entry(platform, &platform_list, list) {
428 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
432 if (ret > PAGE_SIZE) {
438 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
445 static const struct file_operations platform_list_fops = {
446 .read = platform_list_read_file,
447 .llseek = default_llseek,/* read accesses f_pos */
450 static void soc_init_card_debugfs(struct snd_soc_card *card)
452 card->debugfs_card_root = debugfs_create_dir(card->name,
453 snd_soc_debugfs_root);
454 if (!card->debugfs_card_root) {
456 "ASoC: Failed to create card debugfs directory\n");
460 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
461 card->debugfs_card_root,
463 if (!card->debugfs_pop_time)
465 "ASoC: Failed to create pop time debugfs file\n");
468 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
470 debugfs_remove_recursive(card->debugfs_card_root);
475 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
479 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
483 static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
487 static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
491 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
495 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
500 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
501 const char *dai_link, int stream)
505 for (i = 0; i < card->num_links; i++) {
506 if (card->rtd[i].dai_link->no_pcm &&
507 !strcmp(card->rtd[i].dai_link->name, dai_link))
508 return card->rtd[i].pcm->streams[stream].substream;
510 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
513 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
515 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
516 const char *dai_link)
520 for (i = 0; i < card->num_links; i++) {
521 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
522 return &card->rtd[i];
524 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
527 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
529 #ifdef CONFIG_SND_SOC_AC97_BUS
530 /* unregister ac97 codec */
531 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
533 if (codec->ac97->dev.bus)
534 device_unregister(&codec->ac97->dev);
538 /* stop no dev release warning */
539 static void soc_ac97_device_release(struct device *dev){}
541 /* register ac97 codec to bus */
542 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
546 codec->ac97->dev.bus = &ac97_bus_type;
547 codec->ac97->dev.parent = codec->card->dev;
548 codec->ac97->dev.release = soc_ac97_device_release;
550 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
551 codec->card->snd_card->number, 0, codec->name);
552 err = device_register(&codec->ac97->dev);
554 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
555 codec->ac97->dev.bus = NULL;
562 static void codec2codec_close_delayed_work(struct work_struct *work)
564 /* Currently nothing to do for c2c links
565 * Since c2c links are internal nodes in the DAPM graph and
566 * don't interface with the outside world or application layer
567 * we don't have to do any special handling on close.
571 #ifdef CONFIG_PM_SLEEP
572 /* powers down audio subsystem for suspend */
573 int snd_soc_suspend(struct device *dev)
575 struct snd_soc_card *card = dev_get_drvdata(dev);
576 struct snd_soc_codec *codec;
579 /* If the initialization of this soc device failed, there is no codec
580 * associated with it. Just bail out in this case.
582 if (list_empty(&card->codec_dev_list))
585 /* Due to the resume being scheduled into a workqueue we could
586 * suspend before that's finished - wait for it to complete.
588 snd_power_lock(card->snd_card);
589 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
590 snd_power_unlock(card->snd_card);
592 /* we're going to block userspace touching us until resume completes */
593 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
595 /* mute any active DACs */
596 for (i = 0; i < card->num_rtd; i++) {
597 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
598 struct snd_soc_dai_driver *drv = dai->driver;
600 if (card->rtd[i].dai_link->ignore_suspend)
603 if (drv->ops->digital_mute && dai->playback_active)
604 drv->ops->digital_mute(dai, 1);
607 /* suspend all pcms */
608 for (i = 0; i < card->num_rtd; i++) {
609 if (card->rtd[i].dai_link->ignore_suspend)
612 snd_pcm_suspend_all(card->rtd[i].pcm);
615 if (card->suspend_pre)
616 card->suspend_pre(card);
618 for (i = 0; i < card->num_rtd; i++) {
619 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
620 struct snd_soc_platform *platform = card->rtd[i].platform;
622 if (card->rtd[i].dai_link->ignore_suspend)
625 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
626 cpu_dai->driver->suspend(cpu_dai);
627 if (platform->driver->suspend && !platform->suspended) {
628 platform->driver->suspend(cpu_dai);
629 platform->suspended = 1;
633 /* close any waiting streams and save state */
634 for (i = 0; i < card->num_rtd; i++) {
635 flush_delayed_work(&card->rtd[i].delayed_work);
636 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
639 for (i = 0; i < card->num_rtd; i++) {
641 if (card->rtd[i].dai_link->ignore_suspend)
644 snd_soc_dapm_stream_event(&card->rtd[i],
645 SNDRV_PCM_STREAM_PLAYBACK,
646 SND_SOC_DAPM_STREAM_SUSPEND);
648 snd_soc_dapm_stream_event(&card->rtd[i],
649 SNDRV_PCM_STREAM_CAPTURE,
650 SND_SOC_DAPM_STREAM_SUSPEND);
653 /* Recheck all analogue paths too */
654 dapm_mark_io_dirty(&card->dapm);
655 snd_soc_dapm_sync(&card->dapm);
657 /* suspend all CODECs */
658 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
659 /* If there are paths active then the CODEC will be held with
660 * bias _ON and should not be suspended. */
661 if (!codec->suspended && codec->driver->suspend) {
662 switch (codec->dapm.bias_level) {
663 case SND_SOC_BIAS_STANDBY:
665 * If the CODEC is capable of idle
666 * bias off then being in STANDBY
667 * means it's doing something,
668 * otherwise fall through.
670 if (codec->dapm.idle_bias_off) {
672 "ASoC: idle_bias_off CODEC on over suspend\n");
675 case SND_SOC_BIAS_OFF:
676 codec->driver->suspend(codec);
677 codec->suspended = 1;
678 codec->cache_sync = 1;
679 if (codec->component.regmap)
680 regcache_mark_dirty(codec->component.regmap);
681 /* deactivate pins to sleep state */
682 pinctrl_pm_select_sleep_state(codec->dev);
686 "ASoC: CODEC is on over suspend\n");
692 for (i = 0; i < card->num_rtd; i++) {
693 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
695 if (card->rtd[i].dai_link->ignore_suspend)
698 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
699 cpu_dai->driver->suspend(cpu_dai);
701 /* deactivate pins to sleep state */
702 pinctrl_pm_select_sleep_state(cpu_dai->dev);
705 if (card->suspend_post)
706 card->suspend_post(card);
710 EXPORT_SYMBOL_GPL(snd_soc_suspend);
712 /* deferred resume work, so resume can complete before we finished
713 * setting our codec back up, which can be very slow on I2C
715 static void soc_resume_deferred(struct work_struct *work)
717 struct snd_soc_card *card =
718 container_of(work, struct snd_soc_card, deferred_resume_work);
719 struct snd_soc_codec *codec;
722 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
723 * so userspace apps are blocked from touching us
726 dev_dbg(card->dev, "ASoC: starting resume work\n");
728 /* Bring us up into D2 so that DAPM starts enabling things */
729 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
731 if (card->resume_pre)
732 card->resume_pre(card);
734 /* resume AC97 DAIs */
735 for (i = 0; i < card->num_rtd; i++) {
736 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
738 if (card->rtd[i].dai_link->ignore_suspend)
741 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
742 cpu_dai->driver->resume(cpu_dai);
745 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
746 /* If the CODEC was idle over suspend then it will have been
747 * left with bias OFF or STANDBY and suspended so we must now
748 * resume. Otherwise the suspend was suppressed.
750 if (codec->driver->resume && codec->suspended) {
751 switch (codec->dapm.bias_level) {
752 case SND_SOC_BIAS_STANDBY:
753 case SND_SOC_BIAS_OFF:
754 codec->driver->resume(codec);
755 codec->suspended = 0;
759 "ASoC: CODEC was on over suspend\n");
765 for (i = 0; i < card->num_rtd; i++) {
767 if (card->rtd[i].dai_link->ignore_suspend)
770 snd_soc_dapm_stream_event(&card->rtd[i],
771 SNDRV_PCM_STREAM_PLAYBACK,
772 SND_SOC_DAPM_STREAM_RESUME);
774 snd_soc_dapm_stream_event(&card->rtd[i],
775 SNDRV_PCM_STREAM_CAPTURE,
776 SND_SOC_DAPM_STREAM_RESUME);
779 /* unmute any active DACs */
780 for (i = 0; i < card->num_rtd; i++) {
781 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
782 struct snd_soc_dai_driver *drv = dai->driver;
784 if (card->rtd[i].dai_link->ignore_suspend)
787 if (drv->ops->digital_mute && dai->playback_active)
788 drv->ops->digital_mute(dai, 0);
791 for (i = 0; i < card->num_rtd; i++) {
792 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
793 struct snd_soc_platform *platform = card->rtd[i].platform;
795 if (card->rtd[i].dai_link->ignore_suspend)
798 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
799 cpu_dai->driver->resume(cpu_dai);
800 if (platform->driver->resume && platform->suspended) {
801 platform->driver->resume(cpu_dai);
802 platform->suspended = 0;
806 if (card->resume_post)
807 card->resume_post(card);
809 dev_dbg(card->dev, "ASoC: resume work completed\n");
811 /* userspace can access us now we are back as we were before */
812 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
814 /* Recheck all analogue paths too */
815 dapm_mark_io_dirty(&card->dapm);
816 snd_soc_dapm_sync(&card->dapm);
819 /* powers up audio subsystem after a suspend */
820 int snd_soc_resume(struct device *dev)
822 struct snd_soc_card *card = dev_get_drvdata(dev);
823 int i, ac97_control = 0;
825 /* If the initialization of this soc device failed, there is no codec
826 * associated with it. Just bail out in this case.
828 if (list_empty(&card->codec_dev_list))
831 /* activate pins from sleep state */
832 for (i = 0; i < card->num_rtd; i++) {
833 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
834 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
836 pinctrl_pm_select_default_state(cpu_dai->dev);
837 if (codec_dai->active)
838 pinctrl_pm_select_default_state(codec_dai->dev);
841 /* AC97 devices might have other drivers hanging off them so
842 * need to resume immediately. Other drivers don't have that
843 * problem and may take a substantial amount of time to resume
844 * due to I/O costs and anti-pop so handle them out of line.
846 for (i = 0; i < card->num_rtd; i++) {
847 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
848 ac97_control |= cpu_dai->driver->ac97_control;
851 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
852 soc_resume_deferred(&card->deferred_resume_work);
854 dev_dbg(dev, "ASoC: Scheduling resume work\n");
855 if (!schedule_work(&card->deferred_resume_work))
856 dev_err(dev, "ASoC: resume work item may be lost\n");
861 EXPORT_SYMBOL_GPL(snd_soc_resume);
863 #define snd_soc_suspend NULL
864 #define snd_soc_resume NULL
867 static const struct snd_soc_dai_ops null_dai_ops = {
870 static struct snd_soc_codec *soc_find_codec(const struct device_node *codec_of_node,
871 const char *codec_name)
873 struct snd_soc_codec *codec;
875 list_for_each_entry(codec, &codec_list, list) {
877 if (codec->dev->of_node != codec_of_node)
880 if (strcmp(codec->name, codec_name))
890 static struct snd_soc_dai *soc_find_codec_dai(struct snd_soc_codec *codec,
891 const char *codec_dai_name)
893 struct snd_soc_dai *codec_dai;
895 list_for_each_entry(codec_dai, &codec->component.dai_list, list) {
896 if (!strcmp(codec_dai->name, codec_dai_name)) {
904 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
906 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
907 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
908 struct snd_soc_component *component;
909 struct snd_soc_platform *platform;
910 struct snd_soc_dai *cpu_dai;
911 const char *platform_name;
913 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
915 /* Find CPU DAI from registered DAIs*/
916 list_for_each_entry(component, &component_list, list) {
917 if (dai_link->cpu_of_node &&
918 component->dev->of_node != dai_link->cpu_of_node)
920 if (dai_link->cpu_name &&
921 strcmp(dev_name(component->dev), dai_link->cpu_name))
923 list_for_each_entry(cpu_dai, &component->dai_list, list) {
924 if (dai_link->cpu_dai_name &&
925 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
928 rtd->cpu_dai = cpu_dai;
933 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
934 dai_link->cpu_dai_name);
935 return -EPROBE_DEFER;
938 /* Find CODEC from registered list */
939 rtd->codec = soc_find_codec(dai_link->codec_of_node,
940 dai_link->codec_name);
942 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
943 dai_link->codec_name);
944 return -EPROBE_DEFER;
947 /* Find CODEC DAI from registered list */
948 rtd->codec_dai = soc_find_codec_dai(rtd->codec,
949 dai_link->codec_dai_name);
950 if (!rtd->codec_dai) {
951 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
952 dai_link->codec_dai_name);
953 return -EPROBE_DEFER;
956 /* if there's no platform we match on the empty platform */
957 platform_name = dai_link->platform_name;
958 if (!platform_name && !dai_link->platform_of_node)
959 platform_name = "snd-soc-dummy";
961 /* find one from the set of registered platforms */
962 list_for_each_entry(platform, &platform_list, list) {
963 if (dai_link->platform_of_node) {
964 if (platform->dev->of_node !=
965 dai_link->platform_of_node)
968 if (strcmp(platform->name, platform_name))
972 rtd->platform = platform;
974 if (!rtd->platform) {
975 dev_err(card->dev, "ASoC: platform %s not registered\n",
976 dai_link->platform_name);
977 return -EPROBE_DEFER;
985 static int soc_remove_platform(struct snd_soc_platform *platform)
989 if (platform->driver->remove) {
990 ret = platform->driver->remove(platform);
992 dev_err(platform->dev, "ASoC: failed to remove %d\n",
996 /* Make sure all DAPM widgets are freed */
997 snd_soc_dapm_free(&platform->dapm);
999 soc_cleanup_platform_debugfs(platform);
1000 platform->probed = 0;
1001 list_del(&platform->card_list);
1002 module_put(platform->dev->driver->owner);
1007 static void soc_remove_codec(struct snd_soc_codec *codec)
1011 if (codec->driver->remove) {
1012 err = codec->driver->remove(codec);
1014 dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
1017 /* Make sure all DAPM widgets are freed */
1018 snd_soc_dapm_free(&codec->dapm);
1020 soc_cleanup_codec_debugfs(codec);
1022 list_del(&codec->card_list);
1023 module_put(codec->dev->driver->owner);
1026 static void soc_remove_codec_dai(struct snd_soc_dai *codec_dai, int order)
1030 if (codec_dai && codec_dai->probed &&
1031 codec_dai->driver->remove_order == order) {
1032 if (codec_dai->driver->remove) {
1033 err = codec_dai->driver->remove(codec_dai);
1035 dev_err(codec_dai->dev,
1036 "ASoC: failed to remove %s: %d\n",
1037 codec_dai->name, err);
1039 codec_dai->probed = 0;
1043 static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
1045 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1046 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1049 /* unregister the rtd device */
1050 if (rtd->dev_registered) {
1051 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1052 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1053 device_unregister(rtd->dev);
1054 rtd->dev_registered = 0;
1057 /* remove the CODEC DAI */
1058 soc_remove_codec_dai(codec_dai, order);
1060 /* remove the cpu_dai */
1061 if (cpu_dai && cpu_dai->probed &&
1062 cpu_dai->driver->remove_order == order) {
1063 if (cpu_dai->driver->remove) {
1064 err = cpu_dai->driver->remove(cpu_dai);
1066 dev_err(cpu_dai->dev,
1067 "ASoC: failed to remove %s: %d\n",
1068 cpu_dai->name, err);
1070 cpu_dai->probed = 0;
1072 if (!cpu_dai->codec) {
1073 snd_soc_dapm_free(&cpu_dai->dapm);
1074 module_put(cpu_dai->dev->driver->owner);
1079 static void soc_remove_link_components(struct snd_soc_card *card, int num,
1082 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1083 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1084 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1085 struct snd_soc_platform *platform = rtd->platform;
1086 struct snd_soc_codec *codec;
1088 /* remove the platform */
1089 if (platform && platform->probed &&
1090 platform->driver->remove_order == order) {
1091 soc_remove_platform(platform);
1094 /* remove the CODEC-side CODEC */
1096 codec = codec_dai->codec;
1097 if (codec && codec->probed &&
1098 codec->driver->remove_order == order)
1099 soc_remove_codec(codec);
1102 /* remove any CPU-side CODEC */
1104 codec = cpu_dai->codec;
1105 if (codec && codec->probed &&
1106 codec->driver->remove_order == order)
1107 soc_remove_codec(codec);
1111 static void soc_remove_dai_links(struct snd_soc_card *card)
1115 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1117 for (dai = 0; dai < card->num_rtd; dai++)
1118 soc_remove_link_dais(card, dai, order);
1121 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1123 for (dai = 0; dai < card->num_rtd; dai++)
1124 soc_remove_link_components(card, dai, order);
1130 static void soc_set_name_prefix(struct snd_soc_card *card,
1131 struct snd_soc_codec *codec)
1135 if (card->codec_conf == NULL)
1138 for (i = 0; i < card->num_configs; i++) {
1139 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1140 if (map->of_node && codec->dev->of_node != map->of_node)
1142 if (map->dev_name && strcmp(codec->name, map->dev_name))
1144 codec->name_prefix = map->name_prefix;
1149 static int soc_probe_codec(struct snd_soc_card *card,
1150 struct snd_soc_codec *codec)
1153 const struct snd_soc_codec_driver *driver = codec->driver;
1154 struct snd_soc_dai *dai;
1157 codec->dapm.card = card;
1158 soc_set_name_prefix(card, codec);
1160 if (!try_module_get(codec->dev->driver->owner))
1163 soc_init_codec_debugfs(codec);
1165 if (driver->dapm_widgets) {
1166 ret = snd_soc_dapm_new_controls(&codec->dapm,
1167 driver->dapm_widgets,
1168 driver->num_dapm_widgets);
1172 "Failed to create new controls %d\n", ret);
1177 /* Create DAPM widgets for each DAI stream */
1178 list_for_each_entry(dai, &codec->component.dai_list, list) {
1179 ret = snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1183 "Failed to create DAI widgets %d\n", ret);
1188 codec->dapm.idle_bias_off = driver->idle_bias_off;
1190 if (driver->probe) {
1191 ret = driver->probe(codec);
1194 "ASoC: failed to probe CODEC %d\n", ret);
1197 WARN(codec->dapm.idle_bias_off &&
1198 codec->dapm.bias_level != SND_SOC_BIAS_OFF,
1199 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1203 if (driver->controls)
1204 snd_soc_add_codec_controls(codec, driver->controls,
1205 driver->num_controls);
1206 if (driver->dapm_routes)
1207 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1208 driver->num_dapm_routes);
1210 /* mark codec as probed and add to card codec list */
1212 list_add(&codec->card_list, &card->codec_dev_list);
1213 list_add(&codec->dapm.list, &card->dapm_list);
1218 soc_cleanup_codec_debugfs(codec);
1219 module_put(codec->dev->driver->owner);
1224 static int soc_probe_platform(struct snd_soc_card *card,
1225 struct snd_soc_platform *platform)
1228 const struct snd_soc_platform_driver *driver = platform->driver;
1229 struct snd_soc_component *component;
1230 struct snd_soc_dai *dai;
1232 platform->card = card;
1233 platform->dapm.card = card;
1235 if (!try_module_get(platform->dev->driver->owner))
1238 soc_init_platform_debugfs(platform);
1240 if (driver->dapm_widgets)
1241 snd_soc_dapm_new_controls(&platform->dapm,
1242 driver->dapm_widgets, driver->num_dapm_widgets);
1244 /* Create DAPM widgets for each DAI stream */
1245 list_for_each_entry(component, &component_list, list) {
1246 if (component->dev != platform->dev)
1248 list_for_each_entry(dai, &component->dai_list, list)
1249 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1252 platform->dapm.idle_bias_off = 1;
1254 if (driver->probe) {
1255 ret = driver->probe(platform);
1257 dev_err(platform->dev,
1258 "ASoC: failed to probe platform %d\n", ret);
1263 if (driver->controls)
1264 snd_soc_add_platform_controls(platform, driver->controls,
1265 driver->num_controls);
1266 if (driver->dapm_routes)
1267 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1268 driver->num_dapm_routes);
1270 /* mark platform as probed and add to card platform list */
1271 platform->probed = 1;
1272 list_add(&platform->card_list, &card->platform_dev_list);
1273 list_add(&platform->dapm.list, &card->dapm_list);
1278 soc_cleanup_platform_debugfs(platform);
1279 module_put(platform->dev->driver->owner);
1284 static void rtd_release(struct device *dev)
1289 static int soc_aux_dev_init(struct snd_soc_card *card,
1290 struct snd_soc_codec *codec,
1293 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1294 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1299 /* do machine specific initialization */
1300 if (aux_dev->init) {
1301 ret = aux_dev->init(&codec->dapm);
1311 static int soc_dai_link_init(struct snd_soc_card *card,
1312 struct snd_soc_codec *codec,
1315 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1316 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1321 /* do machine specific initialization */
1322 if (dai_link->init) {
1323 ret = dai_link->init(rtd);
1333 static int soc_post_component_init(struct snd_soc_card *card,
1334 struct snd_soc_codec *codec,
1335 int num, int dailess)
1337 struct snd_soc_dai_link *dai_link = NULL;
1338 struct snd_soc_aux_dev *aux_dev = NULL;
1339 struct snd_soc_pcm_runtime *rtd;
1344 dai_link = &card->dai_link[num];
1345 rtd = &card->rtd[num];
1346 name = dai_link->name;
1347 ret = soc_dai_link_init(card, codec, num);
1349 aux_dev = &card->aux_dev[num];
1350 rtd = &card->rtd_aux[num];
1351 name = aux_dev->name;
1352 ret = soc_aux_dev_init(card, codec, num);
1356 dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
1360 /* register the rtd device */
1361 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1364 device_initialize(rtd->dev);
1365 rtd->dev->parent = card->dev;
1366 rtd->dev->release = rtd_release;
1367 rtd->dev->init_name = name;
1368 dev_set_drvdata(rtd->dev, rtd);
1369 mutex_init(&rtd->pcm_mutex);
1370 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1371 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1372 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1373 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1374 ret = device_add(rtd->dev);
1376 /* calling put_device() here to free the rtd->dev */
1377 put_device(rtd->dev);
1379 "ASoC: failed to register runtime device: %d\n", ret);
1382 rtd->dev_registered = 1;
1384 /* add DAPM sysfs entries for this codec */
1385 ret = snd_soc_dapm_sys_add(rtd->dev);
1388 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
1390 /* add codec sysfs entries */
1391 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1394 "ASoC: failed to add codec sysfs files: %d\n", ret);
1396 #ifdef CONFIG_DEBUG_FS
1397 /* add DPCM sysfs entries */
1398 if (!dailess && !dai_link->dynamic)
1401 ret = soc_dpcm_debugfs_add(rtd);
1403 dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
1410 static int soc_probe_link_components(struct snd_soc_card *card, int num,
1413 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1414 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1415 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1416 struct snd_soc_platform *platform = rtd->platform;
1419 /* probe the CPU-side component, if it is a CODEC */
1420 if (cpu_dai->codec &&
1421 !cpu_dai->codec->probed &&
1422 cpu_dai->codec->driver->probe_order == order) {
1423 ret = soc_probe_codec(card, cpu_dai->codec);
1428 /* probe the CODEC-side component */
1429 if (!codec_dai->codec->probed &&
1430 codec_dai->codec->driver->probe_order == order) {
1431 ret = soc_probe_codec(card, codec_dai->codec);
1436 /* probe the platform */
1437 if (!platform->probed &&
1438 platform->driver->probe_order == order) {
1439 ret = soc_probe_platform(card, platform);
1447 static int soc_probe_codec_dai(struct snd_soc_card *card,
1448 struct snd_soc_dai *codec_dai,
1453 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1454 if (codec_dai->driver->probe) {
1455 ret = codec_dai->driver->probe(codec_dai);
1457 dev_err(codec_dai->dev,
1458 "ASoC: failed to probe CODEC DAI %s: %d\n",
1459 codec_dai->name, ret);
1464 /* mark codec_dai as probed and add to card dai list */
1465 codec_dai->probed = 1;
1471 static int soc_link_dai_widgets(struct snd_soc_card *card,
1472 struct snd_soc_dai_link *dai_link,
1473 struct snd_soc_dai *cpu_dai,
1474 struct snd_soc_dai *codec_dai)
1476 struct snd_soc_dapm_widget *play_w, *capture_w;
1479 /* link the DAI widgets */
1480 play_w = codec_dai->playback_widget;
1481 capture_w = cpu_dai->capture_widget;
1482 if (play_w && capture_w) {
1483 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1486 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1487 play_w->name, capture_w->name, ret);
1492 play_w = cpu_dai->playback_widget;
1493 capture_w = codec_dai->capture_widget;
1494 if (play_w && capture_w) {
1495 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1498 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1499 play_w->name, capture_w->name, ret);
1507 static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1509 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1510 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1511 struct snd_soc_codec *codec = rtd->codec;
1512 struct snd_soc_platform *platform = rtd->platform;
1513 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1514 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1517 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1518 card->name, num, order);
1520 /* config components */
1521 cpu_dai->platform = platform;
1522 codec_dai->card = card;
1523 cpu_dai->card = card;
1525 /* set default power off timeout */
1526 rtd->pmdown_time = pmdown_time;
1528 /* probe the cpu_dai */
1529 if (!cpu_dai->probed &&
1530 cpu_dai->driver->probe_order == order) {
1531 if (!cpu_dai->codec) {
1532 cpu_dai->dapm.card = card;
1533 if (!try_module_get(cpu_dai->dev->driver->owner))
1536 list_add(&cpu_dai->dapm.list, &card->dapm_list);
1539 if (cpu_dai->driver->probe) {
1540 ret = cpu_dai->driver->probe(cpu_dai);
1542 dev_err(cpu_dai->dev,
1543 "ASoC: failed to probe CPU DAI %s: %d\n",
1544 cpu_dai->name, ret);
1545 module_put(cpu_dai->dev->driver->owner);
1549 cpu_dai->probed = 1;
1552 /* probe the CODEC DAI */
1553 ret = soc_probe_codec_dai(card, codec_dai, order);
1557 /* complete DAI probe during last probe */
1558 if (order != SND_SOC_COMP_ORDER_LAST)
1561 ret = soc_post_component_init(card, codec, num, 0);
1565 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1567 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1570 if (cpu_dai->driver->compress_dai) {
1571 /*create compress_device"*/
1572 ret = soc_new_compress(rtd, num);
1574 dev_err(card->dev, "ASoC: can't create compress %s\n",
1575 dai_link->stream_name);
1580 if (!dai_link->params) {
1581 /* create the pcm */
1582 ret = soc_new_pcm(rtd, num);
1584 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1585 dai_link->stream_name, ret);
1589 INIT_DELAYED_WORK(&rtd->delayed_work,
1590 codec2codec_close_delayed_work);
1592 /* link the DAI widgets */
1593 ret = soc_link_dai_widgets(card, dai_link,
1594 cpu_dai, codec_dai);
1600 /* add platform data for AC97 devices */
1601 if (rtd->codec_dai->driver->ac97_control)
1602 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1607 #ifdef CONFIG_SND_SOC_AC97_BUS
1608 static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1609 struct snd_soc_dai *codec_dai)
1613 /* Only instantiate AC97 if not already done by the adaptor
1614 * for the generic AC97 subsystem.
1616 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
1618 * It is possible that the AC97 device is already registered to
1619 * the device subsystem. This happens when the device is created
1620 * via snd_ac97_mixer(). Currently only SoC codec that does so
1621 * is the generic AC97 glue but others migh emerge.
1623 * In those cases we don't try to register the device again.
1625 if (!codec->ac97_created)
1628 ret = soc_ac97_dev_register(codec);
1631 "ASoC: AC97 device register failed: %d\n", ret);
1635 codec->ac97_registered = 1;
1640 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1642 return soc_register_ac97_codec(rtd->codec, rtd->codec_dai);
1645 static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
1647 if (codec->ac97_registered) {
1648 soc_ac97_dev_unregister(codec);
1649 codec->ac97_registered = 0;
1653 static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1655 soc_unregister_ac97_codec(rtd->codec);
1659 static struct snd_soc_codec *soc_find_matching_codec(struct snd_soc_card *card,
1662 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1663 struct snd_soc_codec *codec;
1665 /* find CODEC from registered CODECs */
1666 list_for_each_entry(codec, &codec_list, list) {
1667 if (aux_dev->codec_of_node &&
1668 (codec->dev->of_node != aux_dev->codec_of_node))
1670 if (aux_dev->codec_name && strcmp(codec->name, aux_dev->codec_name))
1678 static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1680 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1681 const char *codecname = aux_dev->codec_name;
1682 struct snd_soc_codec *codec = soc_find_matching_codec(card, num);
1686 if (aux_dev->codec_of_node)
1687 codecname = of_node_full_name(aux_dev->codec_of_node);
1689 dev_err(card->dev, "ASoC: %s not registered\n", codecname);
1690 return -EPROBE_DEFER;
1693 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1695 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1696 const char *codecname = aux_dev->codec_name;
1698 struct snd_soc_codec *codec = soc_find_matching_codec(card, num);
1701 if (aux_dev->codec_of_node)
1702 codecname = of_node_full_name(aux_dev->codec_of_node);
1704 /* codec not found */
1705 dev_err(card->dev, "ASoC: codec %s not found", codecname);
1706 return -EPROBE_DEFER;
1709 if (codec->probed) {
1710 dev_err(codec->dev, "ASoC: codec already probed");
1714 ret = soc_probe_codec(card, codec);
1718 ret = soc_post_component_init(card, codec, num, 1);
1723 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1725 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1726 struct snd_soc_codec *codec = rtd->codec;
1728 /* unregister the rtd device */
1729 if (rtd->dev_registered) {
1730 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1731 device_unregister(rtd->dev);
1732 rtd->dev_registered = 0;
1735 if (codec && codec->probed)
1736 soc_remove_codec(codec);
1739 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1743 if (codec->cache_init)
1746 ret = snd_soc_cache_init(codec);
1749 "ASoC: Failed to set cache compression type: %d\n",
1753 codec->cache_init = 1;
1757 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1759 struct snd_soc_codec *codec;
1760 struct snd_soc_dai_link *dai_link;
1761 int ret, i, order, dai_fmt;
1763 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1766 for (i = 0; i < card->num_links; i++) {
1767 ret = soc_bind_dai_link(card, i);
1772 /* check aux_devs too */
1773 for (i = 0; i < card->num_aux_devs; i++) {
1774 ret = soc_check_aux_dev(card, i);
1779 /* initialize the register cache for each available codec */
1780 list_for_each_entry(codec, &codec_list, list) {
1781 if (codec->cache_init)
1783 ret = snd_soc_init_codec_cache(codec);
1788 /* card bind complete so register a sound card */
1789 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1790 card->owner, 0, &card->snd_card);
1793 "ASoC: can't create sound card for card %s: %d\n",
1798 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1799 card->dapm.dev = card->dev;
1800 card->dapm.card = card;
1801 list_add(&card->dapm.list, &card->dapm_list);
1803 #ifdef CONFIG_DEBUG_FS
1804 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1807 #ifdef CONFIG_PM_SLEEP
1808 /* deferred resume work */
1809 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1812 if (card->dapm_widgets)
1813 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1814 card->num_dapm_widgets);
1816 /* initialise the sound card only once */
1818 ret = card->probe(card);
1820 goto card_probe_error;
1823 /* probe all components used by DAI links on this card */
1824 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1826 for (i = 0; i < card->num_links; i++) {
1827 ret = soc_probe_link_components(card, i, order);
1830 "ASoC: failed to instantiate card %d\n",
1837 /* probe all DAI links on this card */
1838 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1840 for (i = 0; i < card->num_links; i++) {
1841 ret = soc_probe_link_dais(card, i, order);
1844 "ASoC: failed to instantiate card %d\n",
1851 for (i = 0; i < card->num_aux_devs; i++) {
1852 ret = soc_probe_aux_dev(card, i);
1855 "ASoC: failed to add auxiliary devices %d\n",
1857 goto probe_aux_dev_err;
1861 snd_soc_dapm_link_dai_widgets(card);
1862 snd_soc_dapm_connect_dai_link_widgets(card);
1865 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1867 if (card->dapm_routes)
1868 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1869 card->num_dapm_routes);
1871 for (i = 0; i < card->num_links; i++) {
1872 dai_link = &card->dai_link[i];
1873 dai_fmt = dai_link->dai_fmt;
1876 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1878 if (ret != 0 && ret != -ENOTSUPP)
1879 dev_warn(card->rtd[i].codec_dai->dev,
1880 "ASoC: Failed to set DAI format: %d\n",
1884 /* If this is a regular CPU link there will be a platform */
1886 (dai_link->platform_name || dai_link->platform_of_node)) {
1887 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1889 if (ret != 0 && ret != -ENOTSUPP)
1890 dev_warn(card->rtd[i].cpu_dai->dev,
1891 "ASoC: Failed to set DAI format: %d\n",
1893 } else if (dai_fmt) {
1894 /* Flip the polarity for the "CPU" end */
1895 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1896 switch (dai_link->dai_fmt &
1897 SND_SOC_DAIFMT_MASTER_MASK) {
1898 case SND_SOC_DAIFMT_CBM_CFM:
1899 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1901 case SND_SOC_DAIFMT_CBM_CFS:
1902 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1904 case SND_SOC_DAIFMT_CBS_CFM:
1905 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1907 case SND_SOC_DAIFMT_CBS_CFS:
1908 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1912 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1914 if (ret != 0 && ret != -ENOTSUPP)
1915 dev_warn(card->rtd[i].cpu_dai->dev,
1916 "ASoC: Failed to set DAI format: %d\n",
1921 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1923 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1924 "%s", card->long_name ? card->long_name : card->name);
1925 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1926 "%s", card->driver_name ? card->driver_name : card->name);
1927 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1928 switch (card->snd_card->driver[i]) {
1934 if (!isalnum(card->snd_card->driver[i]))
1935 card->snd_card->driver[i] = '_';
1940 if (card->late_probe) {
1941 ret = card->late_probe(card);
1943 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
1945 goto probe_aux_dev_err;
1949 if (card->fully_routed)
1950 list_for_each_entry(codec, &card->codec_dev_list, card_list)
1951 snd_soc_dapm_auto_nc_codec_pins(codec);
1953 snd_soc_dapm_new_widgets(card);
1955 ret = snd_card_register(card->snd_card);
1957 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1959 goto probe_aux_dev_err;
1962 #ifdef CONFIG_SND_SOC_AC97_BUS
1963 /* register any AC97 codecs */
1964 for (i = 0; i < card->num_rtd; i++) {
1965 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1968 "ASoC: failed to register AC97: %d\n", ret);
1970 soc_unregister_ac97_dai_link(&card->rtd[i]);
1971 goto probe_aux_dev_err;
1976 card->instantiated = 1;
1977 snd_soc_dapm_sync(&card->dapm);
1978 mutex_unlock(&card->mutex);
1983 for (i = 0; i < card->num_aux_devs; i++)
1984 soc_remove_aux_dev(card, i);
1987 soc_remove_dai_links(card);
1993 snd_card_free(card->snd_card);
1996 mutex_unlock(&card->mutex);
2001 /* probes a new socdev */
2002 static int soc_probe(struct platform_device *pdev)
2004 struct snd_soc_card *card = platform_get_drvdata(pdev);
2007 * no card, so machine driver should be registering card
2008 * we should not be here in that case so ret error
2013 dev_warn(&pdev->dev,
2014 "ASoC: machine %s should use snd_soc_register_card()\n",
2017 /* Bodge while we unpick instantiation */
2018 card->dev = &pdev->dev;
2020 return snd_soc_register_card(card);
2023 static int soc_cleanup_card_resources(struct snd_soc_card *card)
2027 /* make sure any delayed work runs */
2028 for (i = 0; i < card->num_rtd; i++) {
2029 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2030 flush_delayed_work(&rtd->delayed_work);
2033 /* remove auxiliary devices */
2034 for (i = 0; i < card->num_aux_devs; i++)
2035 soc_remove_aux_dev(card, i);
2037 /* remove and free each DAI */
2038 soc_remove_dai_links(card);
2040 soc_cleanup_card_debugfs(card);
2042 /* remove the card */
2046 snd_soc_dapm_free(&card->dapm);
2048 snd_card_free(card->snd_card);
2053 /* removes a socdev */
2054 static int soc_remove(struct platform_device *pdev)
2056 struct snd_soc_card *card = platform_get_drvdata(pdev);
2058 snd_soc_unregister_card(card);
2062 int snd_soc_poweroff(struct device *dev)
2064 struct snd_soc_card *card = dev_get_drvdata(dev);
2067 if (!card->instantiated)
2070 /* Flush out pmdown_time work - we actually do want to run it
2071 * now, we're shutting down so no imminent restart. */
2072 for (i = 0; i < card->num_rtd; i++) {
2073 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2074 flush_delayed_work(&rtd->delayed_work);
2077 snd_soc_dapm_shutdown(card);
2079 /* deactivate pins to sleep state */
2080 for (i = 0; i < card->num_rtd; i++) {
2081 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
2082 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
2083 pinctrl_pm_select_sleep_state(codec_dai->dev);
2084 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2089 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2091 const struct dev_pm_ops snd_soc_pm_ops = {
2092 .suspend = snd_soc_suspend,
2093 .resume = snd_soc_resume,
2094 .freeze = snd_soc_suspend,
2095 .thaw = snd_soc_resume,
2096 .poweroff = snd_soc_poweroff,
2097 .restore = snd_soc_resume,
2099 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2101 /* ASoC platform driver */
2102 static struct platform_driver soc_driver = {
2104 .name = "soc-audio",
2105 .owner = THIS_MODULE,
2106 .pm = &snd_soc_pm_ops,
2109 .remove = soc_remove,
2113 * snd_soc_new_ac97_codec - initailise AC97 device
2114 * @codec: audio codec
2115 * @ops: AC97 bus operations
2116 * @num: AC97 codec number
2118 * Initialises AC97 codec resources for use by ad-hoc devices only.
2120 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2121 struct snd_ac97_bus_ops *ops, int num)
2123 mutex_lock(&codec->mutex);
2125 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2126 if (codec->ac97 == NULL) {
2127 mutex_unlock(&codec->mutex);
2131 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2132 if (codec->ac97->bus == NULL) {
2135 mutex_unlock(&codec->mutex);
2139 codec->ac97->bus->ops = ops;
2140 codec->ac97->num = num;
2143 * Mark the AC97 device to be created by us. This way we ensure that the
2144 * device will be registered with the device subsystem later on.
2146 codec->ac97_created = 1;
2148 mutex_unlock(&codec->mutex);
2151 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2153 static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2155 static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2157 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2159 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2161 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2165 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2167 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2171 static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2173 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2175 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2177 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2178 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2179 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2183 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2185 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2189 static int snd_soc_ac97_parse_pinctl(struct device *dev,
2190 struct snd_ac97_reset_cfg *cfg)
2193 struct pinctrl_state *state;
2197 p = devm_pinctrl_get(dev);
2199 dev_err(dev, "Failed to get pinctrl\n");
2204 state = pinctrl_lookup_state(p, "ac97-reset");
2205 if (IS_ERR(state)) {
2206 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
2207 return PTR_ERR(state);
2209 cfg->pstate_reset = state;
2211 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2212 if (IS_ERR(state)) {
2213 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
2214 return PTR_ERR(state);
2216 cfg->pstate_warm_reset = state;
2218 state = pinctrl_lookup_state(p, "ac97-running");
2219 if (IS_ERR(state)) {
2220 dev_err(dev, "Can't find pinctrl state ac97-running\n");
2221 return PTR_ERR(state);
2223 cfg->pstate_run = state;
2225 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2227 dev_err(dev, "Can't find ac97-sync gpio\n");
2230 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2232 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2235 cfg->gpio_sync = gpio;
2237 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2239 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2242 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2244 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2247 cfg->gpio_sdata = gpio;
2249 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2251 dev_err(dev, "Can't find ac97-reset gpio\n");
2254 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2256 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2259 cfg->gpio_reset = gpio;
2264 struct snd_ac97_bus_ops *soc_ac97_ops;
2265 EXPORT_SYMBOL_GPL(soc_ac97_ops);
2267 int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2269 if (ops == soc_ac97_ops)
2272 if (soc_ac97_ops && ops)
2279 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2282 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2284 * This function sets the reset and warm_reset properties of ops and parses
2285 * the device node of pdev to get pinctrl states and gpio numbers to use.
2287 int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2288 struct platform_device *pdev)
2290 struct device *dev = &pdev->dev;
2291 struct snd_ac97_reset_cfg cfg;
2294 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2298 ret = snd_soc_set_ac97_ops(ops);
2302 ops->warm_reset = snd_soc_ac97_warm_reset;
2303 ops->reset = snd_soc_ac97_reset;
2305 snd_ac97_rst_cfg = cfg;
2308 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2311 * snd_soc_free_ac97_codec - free AC97 codec device
2312 * @codec: audio codec
2314 * Frees AC97 codec device resources.
2316 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2318 mutex_lock(&codec->mutex);
2319 #ifdef CONFIG_SND_SOC_AC97_BUS
2320 soc_unregister_ac97_codec(codec);
2322 kfree(codec->ac97->bus);
2325 codec->ac97_created = 0;
2326 mutex_unlock(&codec->mutex);
2328 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2331 * snd_soc_cnew - create new control
2332 * @_template: control template
2333 * @data: control private data
2334 * @long_name: control long name
2335 * @prefix: control name prefix
2337 * Create a new mixer control from a template control.
2339 * Returns 0 for success, else error.
2341 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2342 void *data, const char *long_name,
2345 struct snd_kcontrol_new template;
2346 struct snd_kcontrol *kcontrol;
2349 memcpy(&template, _template, sizeof(template));
2353 long_name = template.name;
2356 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2360 template.name = name;
2362 template.name = long_name;
2365 kcontrol = snd_ctl_new1(&template, data);
2371 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2373 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2374 const struct snd_kcontrol_new *controls, int num_controls,
2375 const char *prefix, void *data)
2379 for (i = 0; i < num_controls; i++) {
2380 const struct snd_kcontrol_new *control = &controls[i];
2381 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2382 control->name, prefix));
2384 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2385 control->name, err);
2393 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2396 struct snd_card *card = soc_card->snd_card;
2397 struct snd_kcontrol *kctl;
2399 if (unlikely(!name))
2402 list_for_each_entry(kctl, &card->controls, list)
2403 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2407 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2410 * snd_soc_add_codec_controls - add an array of controls to a codec.
2411 * Convenience function to add a list of controls. Many codecs were
2412 * duplicating this code.
2414 * @codec: codec to add controls to
2415 * @controls: array of controls to add
2416 * @num_controls: number of elements in the array
2418 * Return 0 for success, else error.
2420 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2421 const struct snd_kcontrol_new *controls, int num_controls)
2423 struct snd_card *card = codec->card->snd_card;
2425 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2426 codec->name_prefix, &codec->component);
2428 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2431 * snd_soc_add_platform_controls - add an array of controls to a platform.
2432 * Convenience function to add a list of controls.
2434 * @platform: platform to add controls to
2435 * @controls: array of controls to add
2436 * @num_controls: number of elements in the array
2438 * Return 0 for success, else error.
2440 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2441 const struct snd_kcontrol_new *controls, int num_controls)
2443 struct snd_card *card = platform->card->snd_card;
2445 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2446 NULL, &platform->component);
2448 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2451 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2452 * Convenience function to add a list of controls.
2454 * @soc_card: SoC card to add controls to
2455 * @controls: array of controls to add
2456 * @num_controls: number of elements in the array
2458 * Return 0 for success, else error.
2460 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2461 const struct snd_kcontrol_new *controls, int num_controls)
2463 struct snd_card *card = soc_card->snd_card;
2465 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2468 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2471 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2472 * Convienience function to add a list of controls.
2474 * @dai: DAI to add controls to
2475 * @controls: array of controls to add
2476 * @num_controls: number of elements in the array
2478 * Return 0 for success, else error.
2480 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2481 const struct snd_kcontrol_new *controls, int num_controls)
2483 struct snd_card *card = dai->card->snd_card;
2485 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2488 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2491 * snd_soc_info_enum_double - enumerated double mixer info callback
2492 * @kcontrol: mixer control
2493 * @uinfo: control element information
2495 * Callback to provide information about a double enumerated
2498 * Returns 0 for success.
2500 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2501 struct snd_ctl_elem_info *uinfo)
2503 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2505 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2506 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2507 uinfo->value.enumerated.items = e->items;
2509 if (uinfo->value.enumerated.item >= e->items)
2510 uinfo->value.enumerated.item = e->items - 1;
2511 strlcpy(uinfo->value.enumerated.name,
2512 e->texts[uinfo->value.enumerated.item],
2513 sizeof(uinfo->value.enumerated.name));
2516 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2519 * snd_soc_get_enum_double - enumerated double mixer get callback
2520 * @kcontrol: mixer control
2521 * @ucontrol: control element information
2523 * Callback to get the value of a double enumerated mixer.
2525 * Returns 0 for success.
2527 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2528 struct snd_ctl_elem_value *ucontrol)
2530 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2531 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2532 unsigned int val, item;
2533 unsigned int reg_val;
2536 ret = snd_soc_component_read(component, e->reg, ®_val);
2539 val = (reg_val >> e->shift_l) & e->mask;
2540 item = snd_soc_enum_val_to_item(e, val);
2541 ucontrol->value.enumerated.item[0] = item;
2542 if (e->shift_l != e->shift_r) {
2543 val = (reg_val >> e->shift_l) & e->mask;
2544 item = snd_soc_enum_val_to_item(e, val);
2545 ucontrol->value.enumerated.item[1] = item;
2550 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2553 * snd_soc_put_enum_double - enumerated double mixer put callback
2554 * @kcontrol: mixer control
2555 * @ucontrol: control element information
2557 * Callback to set the value of a double enumerated mixer.
2559 * Returns 0 for success.
2561 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2562 struct snd_ctl_elem_value *ucontrol)
2564 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2565 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2566 unsigned int *item = ucontrol->value.enumerated.item;
2570 if (item[0] >= e->items)
2572 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
2573 mask = e->mask << e->shift_l;
2574 if (e->shift_l != e->shift_r) {
2575 if (item[1] >= e->items)
2577 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
2578 mask |= e->mask << e->shift_r;
2581 return snd_soc_component_update_bits(component, e->reg, mask, val);
2583 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2586 * snd_soc_read_signed - Read a codec register and interprete as signed value
2587 * @component: component
2588 * @reg: Register to read
2589 * @mask: Mask to use after shifting the register value
2590 * @shift: Right shift of register value
2591 * @sign_bit: Bit that describes if a number is negative or not.
2592 * @signed_val: Pointer to where the read value should be stored
2594 * This functions reads a codec register. The register value is shifted right
2595 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2596 * the given registervalue into a signed integer if sign_bit is non-zero.
2598 * Returns 0 on sucess, otherwise an error value
2600 static int snd_soc_read_signed(struct snd_soc_component *component,
2601 unsigned int reg, unsigned int mask, unsigned int shift,
2602 unsigned int sign_bit, int *signed_val)
2607 ret = snd_soc_component_read(component, reg, &val);
2611 val = (val >> shift) & mask;
2618 /* non-negative number */
2619 if (!(val & BIT(sign_bit))) {
2627 * The register most probably does not contain a full-sized int.
2628 * Instead we have an arbitrary number of bits in a signed
2629 * representation which has to be translated into a full-sized int.
2630 * This is done by filling up all bits above the sign-bit.
2632 ret |= ~((int)(BIT(sign_bit) - 1));
2640 * snd_soc_info_volsw - single mixer info callback
2641 * @kcontrol: mixer control
2642 * @uinfo: control element information
2644 * Callback to provide information about a single mixer control, or a double
2645 * mixer control that spans 2 registers.
2647 * Returns 0 for success.
2649 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2650 struct snd_ctl_elem_info *uinfo)
2652 struct soc_mixer_control *mc =
2653 (struct soc_mixer_control *)kcontrol->private_value;
2656 if (!mc->platform_max)
2657 mc->platform_max = mc->max;
2658 platform_max = mc->platform_max;
2660 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2661 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2663 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2665 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2666 uinfo->value.integer.min = 0;
2667 uinfo->value.integer.max = platform_max - mc->min;
2670 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2673 * snd_soc_get_volsw - single mixer get callback
2674 * @kcontrol: mixer control
2675 * @ucontrol: control element information
2677 * Callback to get the value of a single mixer control, or a double mixer
2678 * control that spans 2 registers.
2680 * Returns 0 for success.
2682 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2683 struct snd_ctl_elem_value *ucontrol)
2685 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2686 struct soc_mixer_control *mc =
2687 (struct soc_mixer_control *)kcontrol->private_value;
2688 unsigned int reg = mc->reg;
2689 unsigned int reg2 = mc->rreg;
2690 unsigned int shift = mc->shift;
2691 unsigned int rshift = mc->rshift;
2694 int sign_bit = mc->sign_bit;
2695 unsigned int mask = (1 << fls(max)) - 1;
2696 unsigned int invert = mc->invert;
2701 mask = BIT(sign_bit + 1) - 1;
2703 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2707 ucontrol->value.integer.value[0] = val - min;
2709 ucontrol->value.integer.value[0] =
2710 max - ucontrol->value.integer.value[0];
2712 if (snd_soc_volsw_is_stereo(mc)) {
2714 ret = snd_soc_read_signed(component, reg, mask, rshift,
2717 ret = snd_soc_read_signed(component, reg2, mask, shift,
2722 ucontrol->value.integer.value[1] = val - min;
2724 ucontrol->value.integer.value[1] =
2725 max - ucontrol->value.integer.value[1];
2730 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2733 * snd_soc_put_volsw - single mixer put callback
2734 * @kcontrol: mixer control
2735 * @ucontrol: control element information
2737 * Callback to set the value of a single mixer control, or a double mixer
2738 * control that spans 2 registers.
2740 * Returns 0 for success.
2742 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2743 struct snd_ctl_elem_value *ucontrol)
2745 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2746 struct soc_mixer_control *mc =
2747 (struct soc_mixer_control *)kcontrol->private_value;
2748 unsigned int reg = mc->reg;
2749 unsigned int reg2 = mc->rreg;
2750 unsigned int shift = mc->shift;
2751 unsigned int rshift = mc->rshift;
2754 unsigned int sign_bit = mc->sign_bit;
2755 unsigned int mask = (1 << fls(max)) - 1;
2756 unsigned int invert = mc->invert;
2758 bool type_2r = false;
2759 unsigned int val2 = 0;
2760 unsigned int val, val_mask;
2763 mask = BIT(sign_bit + 1) - 1;
2765 val = ((ucontrol->value.integer.value[0] + min) & mask);
2768 val_mask = mask << shift;
2770 if (snd_soc_volsw_is_stereo(mc)) {
2771 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
2775 val_mask |= mask << rshift;
2776 val |= val2 << rshift;
2778 val2 = val2 << shift;
2782 err = snd_soc_component_update_bits(component, reg, val_mask, val);
2787 err = snd_soc_component_update_bits(component, reg2, val_mask,
2792 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2795 * snd_soc_get_volsw_sx - single mixer get callback
2796 * @kcontrol: mixer control
2797 * @ucontrol: control element information
2799 * Callback to get the value of a single mixer control, or a double mixer
2800 * control that spans 2 registers.
2802 * Returns 0 for success.
2804 int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2805 struct snd_ctl_elem_value *ucontrol)
2807 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2808 struct soc_mixer_control *mc =
2809 (struct soc_mixer_control *)kcontrol->private_value;
2810 unsigned int reg = mc->reg;
2811 unsigned int reg2 = mc->rreg;
2812 unsigned int shift = mc->shift;
2813 unsigned int rshift = mc->rshift;
2816 int mask = (1 << (fls(min + max) - 1)) - 1;
2820 ret = snd_soc_component_read(component, reg, &val);
2824 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2826 if (snd_soc_volsw_is_stereo(mc)) {
2827 ret = snd_soc_component_read(component, reg2, &val);
2831 val = ((val >> rshift) - min) & mask;
2832 ucontrol->value.integer.value[1] = val;
2837 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2840 * snd_soc_put_volsw_sx - double mixer set callback
2841 * @kcontrol: mixer control
2842 * @uinfo: control element information
2844 * Callback to set the value of a double mixer control that spans 2 registers.
2846 * Returns 0 for success.
2848 int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2849 struct snd_ctl_elem_value *ucontrol)
2851 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2852 struct soc_mixer_control *mc =
2853 (struct soc_mixer_control *)kcontrol->private_value;
2855 unsigned int reg = mc->reg;
2856 unsigned int reg2 = mc->rreg;
2857 unsigned int shift = mc->shift;
2858 unsigned int rshift = mc->rshift;
2861 int mask = (1 << (fls(min + max) - 1)) - 1;
2863 unsigned int val, val_mask, val2 = 0;
2865 val_mask = mask << shift;
2866 val = (ucontrol->value.integer.value[0] + min) & mask;
2869 err = snd_soc_component_update_bits(component, reg, val_mask, val);
2873 if (snd_soc_volsw_is_stereo(mc)) {
2874 val_mask = mask << rshift;
2875 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2876 val2 = val2 << rshift;
2878 err = snd_soc_component_update_bits(component, reg2, val_mask,
2883 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2886 * snd_soc_info_volsw_s8 - signed mixer info callback
2887 * @kcontrol: mixer control
2888 * @uinfo: control element information
2890 * Callback to provide information about a signed mixer control.
2892 * Returns 0 for success.
2894 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2895 struct snd_ctl_elem_info *uinfo)
2897 struct soc_mixer_control *mc =
2898 (struct soc_mixer_control *)kcontrol->private_value;
2902 if (!mc->platform_max)
2903 mc->platform_max = mc->max;
2904 platform_max = mc->platform_max;
2906 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2908 uinfo->value.integer.min = 0;
2909 uinfo->value.integer.max = platform_max - min;
2912 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2915 * snd_soc_get_volsw_s8 - signed mixer get callback
2916 * @kcontrol: mixer control
2917 * @ucontrol: control element information
2919 * Callback to get the value of a signed mixer control.
2921 * Returns 0 for success.
2923 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2924 struct snd_ctl_elem_value *ucontrol)
2926 struct soc_mixer_control *mc =
2927 (struct soc_mixer_control *)kcontrol->private_value;
2928 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2929 unsigned int reg = mc->reg;
2934 ret = snd_soc_component_read(component, reg, &val);
2938 ucontrol->value.integer.value[0] =
2939 ((signed char)(val & 0xff))-min;
2940 ucontrol->value.integer.value[1] =
2941 ((signed char)((val >> 8) & 0xff))-min;
2944 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2947 * snd_soc_put_volsw_sgn - signed mixer put callback
2948 * @kcontrol: mixer control
2949 * @ucontrol: control element information
2951 * Callback to set the value of a signed mixer control.
2953 * Returns 0 for success.
2955 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2956 struct snd_ctl_elem_value *ucontrol)
2958 struct soc_mixer_control *mc =
2959 (struct soc_mixer_control *)kcontrol->private_value;
2960 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2961 unsigned int reg = mc->reg;
2965 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2966 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2968 return snd_soc_component_update_bits(component, reg, 0xffff, val);
2970 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2973 * snd_soc_info_volsw_range - single mixer info callback with range.
2974 * @kcontrol: mixer control
2975 * @uinfo: control element information
2977 * Callback to provide information, within a range, about a single
2980 * returns 0 for success.
2982 int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2983 struct snd_ctl_elem_info *uinfo)
2985 struct soc_mixer_control *mc =
2986 (struct soc_mixer_control *)kcontrol->private_value;
2990 if (!mc->platform_max)
2991 mc->platform_max = mc->max;
2992 platform_max = mc->platform_max;
2994 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2995 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2996 uinfo->value.integer.min = 0;
2997 uinfo->value.integer.max = platform_max - min;
3001 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
3004 * snd_soc_put_volsw_range - single mixer put value callback with range.
3005 * @kcontrol: mixer control
3006 * @ucontrol: control element information
3008 * Callback to set the value, within a range, for a single mixer control.
3010 * Returns 0 for success.
3012 int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
3013 struct snd_ctl_elem_value *ucontrol)
3015 struct soc_mixer_control *mc =
3016 (struct soc_mixer_control *)kcontrol->private_value;
3017 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3018 unsigned int reg = mc->reg;
3019 unsigned int rreg = mc->rreg;
3020 unsigned int shift = mc->shift;
3023 unsigned int mask = (1 << fls(max)) - 1;
3024 unsigned int invert = mc->invert;
3025 unsigned int val, val_mask;
3028 val = ((ucontrol->value.integer.value[0] + min) & mask);
3031 val_mask = mask << shift;
3034 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
3038 if (snd_soc_volsw_is_stereo(mc)) {
3039 val = ((ucontrol->value.integer.value[1] + min) & mask);
3042 val_mask = mask << shift;
3045 ret = snd_soc_component_update_bits(component, rreg, val_mask,
3051 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
3054 * snd_soc_get_volsw_range - single mixer get callback with range
3055 * @kcontrol: mixer control
3056 * @ucontrol: control element information
3058 * Callback to get the value, within a range, of a single mixer control.
3060 * Returns 0 for success.
3062 int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
3063 struct snd_ctl_elem_value *ucontrol)
3065 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3066 struct soc_mixer_control *mc =
3067 (struct soc_mixer_control *)kcontrol->private_value;
3068 unsigned int reg = mc->reg;
3069 unsigned int rreg = mc->rreg;
3070 unsigned int shift = mc->shift;
3073 unsigned int mask = (1 << fls(max)) - 1;
3074 unsigned int invert = mc->invert;
3078 ret = snd_soc_component_read(component, reg, &val);
3082 ucontrol->value.integer.value[0] = (val >> shift) & mask;
3084 ucontrol->value.integer.value[0] =
3085 max - ucontrol->value.integer.value[0];
3086 ucontrol->value.integer.value[0] =
3087 ucontrol->value.integer.value[0] - min;
3089 if (snd_soc_volsw_is_stereo(mc)) {
3090 ret = snd_soc_component_read(component, rreg, &val);
3094 ucontrol->value.integer.value[1] = (val >> shift) & mask;
3096 ucontrol->value.integer.value[1] =
3097 max - ucontrol->value.integer.value[1];
3098 ucontrol->value.integer.value[1] =
3099 ucontrol->value.integer.value[1] - min;
3104 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3107 * snd_soc_limit_volume - Set new limit to an existing volume control.
3109 * @codec: where to look for the control
3110 * @name: Name of the control
3111 * @max: new maximum limit
3113 * Return 0 for success, else error.
3115 int snd_soc_limit_volume(struct snd_soc_codec *codec,
3116 const char *name, int max)
3118 struct snd_card *card = codec->card->snd_card;
3119 struct snd_kcontrol *kctl;
3120 struct soc_mixer_control *mc;
3124 /* Sanity check for name and max */
3125 if (unlikely(!name || max <= 0))
3128 list_for_each_entry(kctl, &card->controls, list) {
3129 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3135 mc = (struct soc_mixer_control *)kctl->private_value;
3136 if (max <= mc->max) {
3137 mc->platform_max = max;
3143 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3145 int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3146 struct snd_ctl_elem_info *uinfo)
3148 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3149 struct soc_bytes *params = (void *)kcontrol->private_value;
3151 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3152 uinfo->count = params->num_regs * component->val_bytes;
3156 EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3158 int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3159 struct snd_ctl_elem_value *ucontrol)
3161 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3162 struct soc_bytes *params = (void *)kcontrol->private_value;
3165 if (component->regmap)
3166 ret = regmap_raw_read(component->regmap, params->base,
3167 ucontrol->value.bytes.data,
3168 params->num_regs * component->val_bytes);
3172 /* Hide any masked bytes to ensure consistent data reporting */
3173 if (ret == 0 && params->mask) {
3174 switch (component->val_bytes) {
3176 ucontrol->value.bytes.data[0] &= ~params->mask;
3179 ((u16 *)(&ucontrol->value.bytes.data))[0]
3180 &= cpu_to_be16(~params->mask);
3183 ((u32 *)(&ucontrol->value.bytes.data))[0]
3184 &= cpu_to_be32(~params->mask);
3193 EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3195 int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3196 struct snd_ctl_elem_value *ucontrol)
3198 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3199 struct soc_bytes *params = (void *)kcontrol->private_value;
3201 unsigned int val, mask;
3204 if (!component->regmap)
3207 len = params->num_regs * component->val_bytes;
3209 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3214 * If we've got a mask then we need to preserve the register
3215 * bits. We shouldn't modify the incoming data so take a
3219 ret = regmap_read(component->regmap, params->base, &val);
3223 val &= params->mask;
3225 switch (component->val_bytes) {
3227 ((u8 *)data)[0] &= ~params->mask;
3228 ((u8 *)data)[0] |= val;
3231 mask = ~params->mask;
3232 ret = regmap_parse_val(component->regmap,
3237 ((u16 *)data)[0] &= mask;
3239 ret = regmap_parse_val(component->regmap,
3244 ((u16 *)data)[0] |= val;
3247 mask = ~params->mask;
3248 ret = regmap_parse_val(component->regmap,
3253 ((u32 *)data)[0] &= mask;
3255 ret = regmap_parse_val(component->regmap,
3260 ((u32 *)data)[0] |= val;
3268 ret = regmap_raw_write(component->regmap, params->base,
3276 EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3278 int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3279 struct snd_ctl_elem_info *ucontrol)
3281 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3283 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3284 ucontrol->count = params->max;
3288 EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3291 * snd_soc_info_xr_sx - signed multi register info callback
3292 * @kcontrol: mreg control
3293 * @uinfo: control element information
3295 * Callback to provide information of a control that can
3296 * span multiple codec registers which together
3297 * forms a single signed value in a MSB/LSB manner.
3299 * Returns 0 for success.
3301 int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3302 struct snd_ctl_elem_info *uinfo)
3304 struct soc_mreg_control *mc =
3305 (struct soc_mreg_control *)kcontrol->private_value;
3306 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3308 uinfo->value.integer.min = mc->min;
3309 uinfo->value.integer.max = mc->max;
3313 EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3316 * snd_soc_get_xr_sx - signed multi register get callback
3317 * @kcontrol: mreg control
3318 * @ucontrol: control element information
3320 * Callback to get the value of a control that can span
3321 * multiple codec registers which together forms a single
3322 * signed value in a MSB/LSB manner. The control supports
3323 * specifying total no of bits used to allow for bitfields
3324 * across the multiple codec registers.
3326 * Returns 0 for success.
3328 int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3329 struct snd_ctl_elem_value *ucontrol)
3331 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3332 struct soc_mreg_control *mc =
3333 (struct soc_mreg_control *)kcontrol->private_value;
3334 unsigned int regbase = mc->regbase;
3335 unsigned int regcount = mc->regcount;
3336 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
3337 unsigned int regwmask = (1<<regwshift)-1;
3338 unsigned int invert = mc->invert;
3339 unsigned long mask = (1UL<<mc->nbits)-1;
3343 unsigned int regval;
3347 for (i = 0; i < regcount; i++) {
3348 ret = snd_soc_component_read(component, regbase+i, ®val);
3351 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
3354 if (min < 0 && val > max)
3358 ucontrol->value.integer.value[0] = val;
3362 EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3365 * snd_soc_put_xr_sx - signed multi register get callback
3366 * @kcontrol: mreg control
3367 * @ucontrol: control element information
3369 * Callback to set the value of a control that can span
3370 * multiple codec registers which together forms a single
3371 * signed value in a MSB/LSB manner. The control supports
3372 * specifying total no of bits used to allow for bitfields
3373 * across the multiple codec registers.
3375 * Returns 0 for success.
3377 int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3378 struct snd_ctl_elem_value *ucontrol)
3380 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3381 struct soc_mreg_control *mc =
3382 (struct soc_mreg_control *)kcontrol->private_value;
3383 unsigned int regbase = mc->regbase;
3384 unsigned int regcount = mc->regcount;
3385 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
3386 unsigned int regwmask = (1<<regwshift)-1;
3387 unsigned int invert = mc->invert;
3388 unsigned long mask = (1UL<<mc->nbits)-1;
3390 long val = ucontrol->value.integer.value[0];
3391 unsigned int i, regval, regmask;
3397 for (i = 0; i < regcount; i++) {
3398 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3399 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3400 err = snd_soc_component_update_bits(component, regbase+i,
3408 EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3411 * snd_soc_get_strobe - strobe get callback
3412 * @kcontrol: mixer control
3413 * @ucontrol: control element information
3415 * Callback get the value of a strobe mixer control.
3417 * Returns 0 for success.
3419 int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3420 struct snd_ctl_elem_value *ucontrol)
3422 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3423 struct soc_mixer_control *mc =
3424 (struct soc_mixer_control *)kcontrol->private_value;
3425 unsigned int reg = mc->reg;
3426 unsigned int shift = mc->shift;
3427 unsigned int mask = 1 << shift;
3428 unsigned int invert = mc->invert != 0;
3432 ret = snd_soc_component_read(component, reg, &val);
3438 if (shift != 0 && val != 0)
3440 ucontrol->value.enumerated.item[0] = val ^ invert;
3444 EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3447 * snd_soc_put_strobe - strobe put callback
3448 * @kcontrol: mixer control
3449 * @ucontrol: control element information
3451 * Callback strobe a register bit to high then low (or the inverse)
3452 * in one pass of a single mixer enum control.
3454 * Returns 1 for success.
3456 int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3457 struct snd_ctl_elem_value *ucontrol)
3459 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3460 struct soc_mixer_control *mc =
3461 (struct soc_mixer_control *)kcontrol->private_value;
3462 unsigned int reg = mc->reg;
3463 unsigned int shift = mc->shift;
3464 unsigned int mask = 1 << shift;
3465 unsigned int invert = mc->invert != 0;
3466 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3467 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3468 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3471 err = snd_soc_component_update_bits(component, reg, mask, val1);
3475 return snd_soc_component_update_bits(component, reg, mask, val2);
3477 EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3480 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3482 * @clk_id: DAI specific clock ID
3483 * @freq: new clock frequency in Hz
3484 * @dir: new clock direction - input/output.
3486 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3488 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3489 unsigned int freq, int dir)
3491 if (dai->driver && dai->driver->ops->set_sysclk)
3492 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3493 else if (dai->codec && dai->codec->driver->set_sysclk)
3494 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3499 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3502 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3504 * @clk_id: DAI specific clock ID
3505 * @source: Source for the clock
3506 * @freq: new clock frequency in Hz
3507 * @dir: new clock direction - input/output.
3509 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3511 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3512 int source, unsigned int freq, int dir)
3514 if (codec->driver->set_sysclk)
3515 return codec->driver->set_sysclk(codec, clk_id, source,
3520 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3523 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3525 * @div_id: DAI specific clock divider ID
3526 * @div: new clock divisor.
3528 * Configures the clock dividers. This is used to derive the best DAI bit and
3529 * frame clocks from the system or master clock. It's best to set the DAI bit
3530 * and frame clocks as low as possible to save system power.
3532 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3533 int div_id, int div)
3535 if (dai->driver && dai->driver->ops->set_clkdiv)
3536 return dai->driver->ops->set_clkdiv(dai, div_id, div);
3540 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3543 * snd_soc_dai_set_pll - configure DAI PLL.
3545 * @pll_id: DAI specific PLL ID
3546 * @source: DAI specific source for the PLL
3547 * @freq_in: PLL input clock frequency in Hz
3548 * @freq_out: requested PLL output clock frequency in Hz
3550 * Configures and enables PLL to generate output clock based on input clock.
3552 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3553 unsigned int freq_in, unsigned int freq_out)
3555 if (dai->driver && dai->driver->ops->set_pll)
3556 return dai->driver->ops->set_pll(dai, pll_id, source,
3558 else if (dai->codec && dai->codec->driver->set_pll)
3559 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3564 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3567 * snd_soc_codec_set_pll - configure codec PLL.
3569 * @pll_id: DAI specific PLL ID
3570 * @source: DAI specific source for the PLL
3571 * @freq_in: PLL input clock frequency in Hz
3572 * @freq_out: requested PLL output clock frequency in Hz
3574 * Configures and enables PLL to generate output clock based on input clock.
3576 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3577 unsigned int freq_in, unsigned int freq_out)
3579 if (codec->driver->set_pll)
3580 return codec->driver->set_pll(codec, pll_id, source,
3585 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3588 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3590 * @ratio Ratio of BCLK to Sample rate.
3592 * Configures the DAI for a preset BCLK to sample rate ratio.
3594 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3596 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3597 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3601 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3604 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3606 * @fmt: SND_SOC_DAIFMT_ format value.
3608 * Configures the DAI hardware format and clocking.
3610 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3612 if (dai->driver == NULL)
3614 if (dai->driver->ops->set_fmt == NULL)
3616 return dai->driver->ops->set_fmt(dai, fmt);
3618 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3621 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
3622 * @slots: Number of slots in use.
3623 * @tx_mask: bitmask representing active TX slots.
3624 * @rx_mask: bitmask representing active RX slots.
3626 * Generates the TDM tx and rx slot default masks for DAI.
3628 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
3629 unsigned int *tx_mask,
3630 unsigned int *rx_mask)
3632 if (*tx_mask || *rx_mask)
3638 *tx_mask = (1 << slots) - 1;
3639 *rx_mask = (1 << slots) - 1;
3645 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3647 * @tx_mask: bitmask representing active TX slots.
3648 * @rx_mask: bitmask representing active RX slots.
3649 * @slots: Number of slots in use.
3650 * @slot_width: Width in bits for each slot.
3652 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3655 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3656 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
3658 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3659 dai->driver->ops->xlate_tdm_slot_mask(slots,
3660 &tx_mask, &rx_mask);
3662 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
3664 if (dai->driver && dai->driver->ops->set_tdm_slot)
3665 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3670 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3673 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3675 * @tx_num: how many TX channels
3676 * @tx_slot: pointer to an array which imply the TX slot number channel
3678 * @rx_num: how many RX channels
3679 * @rx_slot: pointer to an array which imply the RX slot number channel
3682 * configure the relationship between channel number and TDM slot number.
3684 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3685 unsigned int tx_num, unsigned int *tx_slot,
3686 unsigned int rx_num, unsigned int *rx_slot)
3688 if (dai->driver && dai->driver->ops->set_channel_map)
3689 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3694 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3697 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3699 * @tristate: tristate enable
3701 * Tristates the DAI so that others can use it.
3703 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3705 if (dai->driver && dai->driver->ops->set_tristate)
3706 return dai->driver->ops->set_tristate(dai, tristate);
3710 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3713 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3715 * @mute: mute enable
3716 * @direction: stream to mute
3718 * Mutes the DAI DAC.
3720 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3726 if (dai->driver->ops->mute_stream)
3727 return dai->driver->ops->mute_stream(dai, mute, direction);
3728 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3729 dai->driver->ops->digital_mute)
3730 return dai->driver->ops->digital_mute(dai, mute);
3734 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3737 * snd_soc_register_card - Register a card with the ASoC core
3739 * @card: Card to register
3742 int snd_soc_register_card(struct snd_soc_card *card)
3746 if (!card->name || !card->dev)
3749 for (i = 0; i < card->num_links; i++) {
3750 struct snd_soc_dai_link *link = &card->dai_link[i];
3753 * Codec must be specified by 1 of name or OF node,
3754 * not both or neither.
3756 if (!!link->codec_name == !!link->codec_of_node) {
3758 "ASoC: Neither/both codec name/of_node are set for %s\n",
3762 /* Codec DAI name must be specified */
3763 if (!link->codec_dai_name) {
3765 "ASoC: codec_dai_name not set for %s\n",
3771 * Platform may be specified by either name or OF node, but
3772 * can be left unspecified, and a dummy platform will be used.
3774 if (link->platform_name && link->platform_of_node) {
3776 "ASoC: Both platform name/of_node are set for %s\n",
3782 * CPU device may be specified by either name or OF node, but
3783 * can be left unspecified, and will be matched based on DAI
3786 if (link->cpu_name && link->cpu_of_node) {
3788 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3793 * At least one of CPU DAI name or CPU device name/node must be
3796 if (!link->cpu_dai_name &&
3797 !(link->cpu_name || link->cpu_of_node)) {
3799 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3805 dev_set_drvdata(card->dev, card);
3807 snd_soc_initialize_card_lists(card);
3809 soc_init_card_debugfs(card);
3811 card->rtd = devm_kzalloc(card->dev,
3812 sizeof(struct snd_soc_pcm_runtime) *
3813 (card->num_links + card->num_aux_devs),
3815 if (card->rtd == NULL)
3818 card->rtd_aux = &card->rtd[card->num_links];
3820 for (i = 0; i < card->num_links; i++)
3821 card->rtd[i].dai_link = &card->dai_link[i];
3823 INIT_LIST_HEAD(&card->dapm_dirty);
3824 card->instantiated = 0;
3825 mutex_init(&card->mutex);
3826 mutex_init(&card->dapm_mutex);
3828 ret = snd_soc_instantiate_card(card);
3830 soc_cleanup_card_debugfs(card);
3832 /* deactivate pins to sleep state */
3833 for (i = 0; i < card->num_rtd; i++) {
3834 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
3835 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
3836 if (!codec_dai->active)
3837 pinctrl_pm_select_sleep_state(codec_dai->dev);
3838 if (!cpu_dai->active)
3839 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3844 EXPORT_SYMBOL_GPL(snd_soc_register_card);
3847 * snd_soc_unregister_card - Unregister a card with the ASoC core
3849 * @card: Card to unregister
3852 int snd_soc_unregister_card(struct snd_soc_card *card)
3854 if (card->instantiated)
3855 soc_cleanup_card_resources(card);
3856 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
3860 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3863 * Simplify DAI link configuration by removing ".-1" from device names
3864 * and sanitizing names.
3866 static char *fmt_single_name(struct device *dev, int *id)
3868 char *found, name[NAME_SIZE];
3871 if (dev_name(dev) == NULL)
3874 strlcpy(name, dev_name(dev), NAME_SIZE);
3876 /* are we a "%s.%d" name (platform and SPI components) */
3877 found = strstr(name, dev->driver->name);
3880 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3882 /* discard ID from name if ID == -1 */
3884 found[strlen(dev->driver->name)] = '\0';
3888 /* I2C component devices are named "bus-addr" */
3889 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3890 char tmp[NAME_SIZE];
3892 /* create unique ID number from I2C addr and bus */
3893 *id = ((id1 & 0xffff) << 16) + id2;
3895 /* sanitize component name for DAI link creation */
3896 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3897 strlcpy(name, tmp, NAME_SIZE);
3902 return kstrdup(name, GFP_KERNEL);
3906 * Simplify DAI link naming for single devices with multiple DAIs by removing
3907 * any ".-1" and using the DAI name (instead of device name).
3909 static inline char *fmt_multiple_name(struct device *dev,
3910 struct snd_soc_dai_driver *dai_drv)
3912 if (dai_drv->name == NULL) {
3914 "ASoC: error - multiple DAI %s registered with no name\n",
3919 return kstrdup(dai_drv->name, GFP_KERNEL);
3923 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
3925 * @component: The component for which the DAIs should be unregistered
3927 static void snd_soc_unregister_dais(struct snd_soc_component *component)
3929 struct snd_soc_dai *dai, *_dai;
3931 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
3932 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3934 list_del(&dai->list);
3941 * snd_soc_register_dais - Register a DAI with the ASoC core
3943 * @component: The component the DAIs are registered for
3944 * @codec: The CODEC that the DAIs are registered for, NULL if the component is
3946 * @dai_drv: DAI driver to use for the DAIs
3947 * @count: Number of DAIs
3948 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3951 static int snd_soc_register_dais(struct snd_soc_component *component,
3952 struct snd_soc_codec *codec, struct snd_soc_dai_driver *dai_drv,
3953 size_t count, bool legacy_dai_naming)
3955 struct device *dev = component->dev;
3956 struct snd_soc_dai *dai;
3960 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
3962 for (i = 0; i < count; i++) {
3964 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3971 * Back in the old days when we still had component-less DAIs,
3972 * instead of having a static name, component-less DAIs would
3973 * inherit the name of the parent device so it is possible to
3974 * register multiple instances of the DAI. We still need to keep
3975 * the same naming style even though those DAIs are not
3976 * component-less anymore.
3978 if (count == 1 && legacy_dai_naming) {
3979 dai->name = fmt_single_name(dev, &dai->id);
3981 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3983 dai->id = dai_drv[i].id;
3987 if (dai->name == NULL) {
3993 dai->component = component;
3996 dai->driver = &dai_drv[i];
3997 dai->dapm.dev = dev;
3998 if (!dai->driver->ops)
3999 dai->driver->ops = &null_dai_ops;
4002 dai->dapm.idle_bias_off = 1;
4004 list_add(&dai->list, &component->dai_list);
4006 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
4012 snd_soc_unregister_dais(component);
4018 * snd_soc_register_component - Register a component with the ASoC core
4022 __snd_soc_register_component(struct device *dev,
4023 struct snd_soc_component *cmpnt,
4024 const struct snd_soc_component_driver *cmpnt_drv,
4025 struct snd_soc_codec *codec,
4026 struct snd_soc_dai_driver *dai_drv,
4027 int num_dai, bool allow_single_dai)
4031 dev_dbg(dev, "component register %s\n", dev_name(dev));
4034 dev_err(dev, "ASoC: Failed to connecting component\n");
4038 mutex_init(&cmpnt->io_mutex);
4040 cmpnt->name = fmt_single_name(dev, &cmpnt->id);
4042 dev_err(dev, "ASoC: Failed to simplifying name\n");
4047 cmpnt->driver = cmpnt_drv;
4048 cmpnt->dai_drv = dai_drv;
4049 cmpnt->num_dai = num_dai;
4050 INIT_LIST_HEAD(&cmpnt->dai_list);
4052 ret = snd_soc_register_dais(cmpnt, codec, dai_drv, num_dai,
4055 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4056 goto error_component_name;
4059 mutex_lock(&client_mutex);
4060 list_add(&cmpnt->list, &component_list);
4061 mutex_unlock(&client_mutex);
4063 dev_dbg(cmpnt->dev, "ASoC: Registered component '%s'\n", cmpnt->name);
4067 error_component_name:
4073 int snd_soc_register_component(struct device *dev,
4074 const struct snd_soc_component_driver *cmpnt_drv,
4075 struct snd_soc_dai_driver *dai_drv,
4078 struct snd_soc_component *cmpnt;
4080 cmpnt = devm_kzalloc(dev, sizeof(*cmpnt), GFP_KERNEL);
4082 dev_err(dev, "ASoC: Failed to allocate memory\n");
4086 cmpnt->ignore_pmdown_time = true;
4087 cmpnt->registered_as_component = true;
4089 return __snd_soc_register_component(dev, cmpnt, cmpnt_drv, NULL,
4090 dai_drv, num_dai, true);
4092 EXPORT_SYMBOL_GPL(snd_soc_register_component);
4094 static void __snd_soc_unregister_component(struct snd_soc_component *cmpnt)
4096 snd_soc_unregister_dais(cmpnt);
4098 mutex_lock(&client_mutex);
4099 list_del(&cmpnt->list);
4100 mutex_unlock(&client_mutex);
4102 dev_dbg(cmpnt->dev, "ASoC: Unregistered component '%s'\n", cmpnt->name);
4107 * snd_soc_unregister_component - Unregister a component from the ASoC core
4110 void snd_soc_unregister_component(struct device *dev)
4112 struct snd_soc_component *cmpnt;
4114 list_for_each_entry(cmpnt, &component_list, list) {
4115 if (dev == cmpnt->dev && cmpnt->registered_as_component)
4121 __snd_soc_unregister_component(cmpnt);
4123 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4125 static int snd_soc_platform_drv_write(struct snd_soc_component *component,
4126 unsigned int reg, unsigned int val)
4128 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4130 return platform->driver->write(platform, reg, val);
4133 static int snd_soc_platform_drv_read(struct snd_soc_component *component,
4134 unsigned int reg, unsigned int *val)
4136 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4138 *val = platform->driver->read(platform, reg);
4144 * snd_soc_add_platform - Add a platform to the ASoC core
4145 * @dev: The parent device for the platform
4146 * @platform: The platform to add
4147 * @platform_driver: The driver for the platform
4149 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
4150 const struct snd_soc_platform_driver *platform_drv)
4154 /* create platform component name */
4155 platform->name = fmt_single_name(dev, &platform->id);
4156 if (platform->name == NULL)
4159 platform->dev = dev;
4160 platform->driver = platform_drv;
4161 platform->dapm.dev = dev;
4162 platform->dapm.platform = platform;
4163 platform->dapm.component = &platform->component;
4164 platform->dapm.stream_event = platform_drv->stream_event;
4165 if (platform_drv->write)
4166 platform->component.write = snd_soc_platform_drv_write;
4167 if (platform_drv->read)
4168 platform->component.read = snd_soc_platform_drv_read;
4170 /* register component */
4171 ret = __snd_soc_register_component(dev, &platform->component,
4172 &platform_drv->component_driver,
4173 NULL, NULL, 0, false);
4175 dev_err(platform->component.dev,
4176 "ASoC: Failed to register component: %d\n", ret);
4180 mutex_lock(&client_mutex);
4181 list_add(&platform->list, &platform_list);
4182 mutex_unlock(&client_mutex);
4184 dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
4188 EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4191 * snd_soc_register_platform - Register a platform with the ASoC core
4193 * @platform: platform to register
4195 int snd_soc_register_platform(struct device *dev,
4196 const struct snd_soc_platform_driver *platform_drv)
4198 struct snd_soc_platform *platform;
4201 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4203 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4204 if (platform == NULL)
4207 ret = snd_soc_add_platform(dev, platform, platform_drv);
4213 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4216 * snd_soc_remove_platform - Remove a platform from the ASoC core
4217 * @platform: the platform to remove
4219 void snd_soc_remove_platform(struct snd_soc_platform *platform)
4221 __snd_soc_unregister_component(&platform->component);
4223 mutex_lock(&client_mutex);
4224 list_del(&platform->list);
4225 mutex_unlock(&client_mutex);
4227 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
4229 kfree(platform->name);
4231 EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4233 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4235 struct snd_soc_platform *platform;
4237 list_for_each_entry(platform, &platform_list, list) {
4238 if (dev == platform->dev)
4244 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4247 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4249 * @platform: platform to unregister
4251 void snd_soc_unregister_platform(struct device *dev)
4253 struct snd_soc_platform *platform;
4255 platform = snd_soc_lookup_platform(dev);
4259 snd_soc_remove_platform(platform);
4262 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4264 static u64 codec_format_map[] = {
4265 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4266 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4267 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4268 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4269 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4270 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4271 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4272 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4273 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4274 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4275 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4276 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4277 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4278 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4279 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4280 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4283 /* Fix up the DAI formats for endianness: codecs don't actually see
4284 * the endianness of the data but we're using the CPU format
4285 * definitions which do need to include endianness so we ensure that
4286 * codec DAIs always have both big and little endian variants set.
4288 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4292 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4293 if (stream->formats & codec_format_map[i])
4294 stream->formats |= codec_format_map[i];
4297 static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4298 unsigned int reg, unsigned int val)
4300 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4302 return codec->driver->write(codec, reg, val);
4305 static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4306 unsigned int reg, unsigned int *val)
4308 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4310 *val = codec->driver->read(codec, reg);
4316 * snd_soc_register_codec - Register a codec with the ASoC core
4318 * @codec: codec to register
4320 int snd_soc_register_codec(struct device *dev,
4321 const struct snd_soc_codec_driver *codec_drv,
4322 struct snd_soc_dai_driver *dai_drv,
4325 struct snd_soc_codec *codec;
4326 struct regmap *regmap;
4329 dev_dbg(dev, "codec register %s\n", dev_name(dev));
4331 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4335 /* create CODEC component name */
4336 codec->name = fmt_single_name(dev, &codec->id);
4337 if (codec->name == NULL) {
4342 if (codec_drv->write)
4343 codec->component.write = snd_soc_codec_drv_write;
4344 if (codec_drv->read)
4345 codec->component.read = snd_soc_codec_drv_read;
4346 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
4347 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4348 codec->dapm.dev = dev;
4349 codec->dapm.codec = codec;
4350 codec->dapm.component = &codec->component;
4351 codec->dapm.seq_notifier = codec_drv->seq_notifier;
4352 codec->dapm.stream_event = codec_drv->stream_event;
4354 codec->driver = codec_drv;
4355 codec->component.val_bytes = codec_drv->reg_word_size;
4356 mutex_init(&codec->mutex);
4358 if (!codec->component.write) {
4359 if (codec_drv->get_regmap)
4360 regmap = codec_drv->get_regmap(dev);
4362 regmap = dev_get_regmap(dev, NULL);
4365 ret = snd_soc_component_init_io(&codec->component,
4369 "Failed to set cache I/O:%d\n",
4376 for (i = 0; i < num_dai; i++) {
4377 fixup_codec_formats(&dai_drv[i].playback);
4378 fixup_codec_formats(&dai_drv[i].capture);
4381 mutex_lock(&client_mutex);
4382 list_add(&codec->list, &codec_list);
4383 mutex_unlock(&client_mutex);
4385 /* register component */
4386 ret = __snd_soc_register_component(dev, &codec->component,
4387 &codec_drv->component_driver,
4388 codec, dai_drv, num_dai, false);
4390 dev_err(codec->dev, "ASoC: Failed to regster component: %d\n", ret);
4391 goto fail_codec_name;
4394 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
4398 mutex_lock(&client_mutex);
4399 list_del(&codec->list);
4400 mutex_unlock(&client_mutex);
4407 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4410 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4412 * @codec: codec to unregister
4414 void snd_soc_unregister_codec(struct device *dev)
4416 struct snd_soc_codec *codec;
4418 list_for_each_entry(codec, &codec_list, list) {
4419 if (dev == codec->dev)
4425 __snd_soc_unregister_component(&codec->component);
4427 mutex_lock(&client_mutex);
4428 list_del(&codec->list);
4429 mutex_unlock(&client_mutex);
4431 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
4433 snd_soc_cache_exit(codec);
4437 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4439 /* Retrieve a card's name from device tree */
4440 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4441 const char *propname)
4443 struct device_node *np = card->dev->of_node;
4446 ret = of_property_read_string_index(np, propname, 0, &card->name);
4448 * EINVAL means the property does not exist. This is fine providing
4449 * card->name was previously set, which is checked later in
4450 * snd_soc_register_card.
4452 if (ret < 0 && ret != -EINVAL) {
4454 "ASoC: Property '%s' could not be read: %d\n",
4461 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4463 static const struct snd_soc_dapm_widget simple_widgets[] = {
4464 SND_SOC_DAPM_MIC("Microphone", NULL),
4465 SND_SOC_DAPM_LINE("Line", NULL),
4466 SND_SOC_DAPM_HP("Headphone", NULL),
4467 SND_SOC_DAPM_SPK("Speaker", NULL),
4470 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4471 const char *propname)
4473 struct device_node *np = card->dev->of_node;
4474 struct snd_soc_dapm_widget *widgets;
4475 const char *template, *wname;
4476 int i, j, num_widgets, ret;
4478 num_widgets = of_property_count_strings(np, propname);
4479 if (num_widgets < 0) {
4481 "ASoC: Property '%s' does not exist\n", propname);
4484 if (num_widgets & 1) {
4486 "ASoC: Property '%s' length is not even\n", propname);
4492 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4497 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4501 "ASoC: Could not allocate memory for widgets\n");
4505 for (i = 0; i < num_widgets; i++) {
4506 ret = of_property_read_string_index(np, propname,
4510 "ASoC: Property '%s' index %d read error:%d\n",
4511 propname, 2 * i, ret);
4515 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4516 if (!strncmp(template, simple_widgets[j].name,
4517 strlen(simple_widgets[j].name))) {
4518 widgets[i] = simple_widgets[j];
4523 if (j >= ARRAY_SIZE(simple_widgets)) {
4525 "ASoC: DAPM widget '%s' is not supported\n",
4530 ret = of_property_read_string_index(np, propname,
4535 "ASoC: Property '%s' index %d read error:%d\n",
4536 propname, (2 * i) + 1, ret);
4540 widgets[i].name = wname;
4543 card->dapm_widgets = widgets;
4544 card->num_dapm_widgets = num_widgets;
4548 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4550 int snd_soc_of_parse_tdm_slot(struct device_node *np,
4551 unsigned int *slots,
4552 unsigned int *slot_width)
4557 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4558 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4566 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4567 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4577 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4579 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4580 const char *propname)
4582 struct device_node *np = card->dev->of_node;
4584 struct snd_soc_dapm_route *routes;
4587 num_routes = of_property_count_strings(np, propname);
4588 if (num_routes < 0 || num_routes & 1) {
4590 "ASoC: Property '%s' does not exist or its length is not even\n",
4596 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4601 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4605 "ASoC: Could not allocate DAPM route table\n");
4609 for (i = 0; i < num_routes; i++) {
4610 ret = of_property_read_string_index(np, propname,
4611 2 * i, &routes[i].sink);
4614 "ASoC: Property '%s' index %d could not be read: %d\n",
4615 propname, 2 * i, ret);
4618 ret = of_property_read_string_index(np, propname,
4619 (2 * i) + 1, &routes[i].source);
4622 "ASoC: Property '%s' index %d could not be read: %d\n",
4623 propname, (2 * i) + 1, ret);
4628 card->num_dapm_routes = num_routes;
4629 card->dapm_routes = routes;
4633 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4635 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4637 struct device_node **bitclkmaster,
4638 struct device_node **framemaster)
4642 unsigned int format = 0;
4648 } of_fmt_table[] = {
4649 { "i2s", SND_SOC_DAIFMT_I2S },
4650 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4651 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4652 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4653 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4654 { "ac97", SND_SOC_DAIFMT_AC97 },
4655 { "pdm", SND_SOC_DAIFMT_PDM},
4656 { "msb", SND_SOC_DAIFMT_MSB },
4657 { "lsb", SND_SOC_DAIFMT_LSB },
4664 * check "[prefix]format = xxx"
4665 * SND_SOC_DAIFMT_FORMAT_MASK area
4667 snprintf(prop, sizeof(prop), "%sformat", prefix);
4668 ret = of_property_read_string(np, prop, &str);
4670 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4671 if (strcmp(str, of_fmt_table[i].name) == 0) {
4672 format |= of_fmt_table[i].val;
4679 * check "[prefix]continuous-clock"
4680 * SND_SOC_DAIFMT_CLOCK_MASK area
4682 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4683 if (of_get_property(np, prop, NULL))
4684 format |= SND_SOC_DAIFMT_CONT;
4686 format |= SND_SOC_DAIFMT_GATED;
4689 * check "[prefix]bitclock-inversion"
4690 * check "[prefix]frame-inversion"
4691 * SND_SOC_DAIFMT_INV_MASK area
4693 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4694 bit = !!of_get_property(np, prop, NULL);
4696 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4697 frame = !!of_get_property(np, prop, NULL);
4699 switch ((bit << 4) + frame) {
4701 format |= SND_SOC_DAIFMT_IB_IF;
4704 format |= SND_SOC_DAIFMT_IB_NF;
4707 format |= SND_SOC_DAIFMT_NB_IF;
4710 /* SND_SOC_DAIFMT_NB_NF is default */
4715 * check "[prefix]bitclock-master"
4716 * check "[prefix]frame-master"
4717 * SND_SOC_DAIFMT_MASTER_MASK area
4719 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4720 bit = !!of_get_property(np, prop, NULL);
4721 if (bit && bitclkmaster)
4722 *bitclkmaster = of_parse_phandle(np, prop, 0);
4724 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4725 frame = !!of_get_property(np, prop, NULL);
4726 if (frame && framemaster)
4727 *framemaster = of_parse_phandle(np, prop, 0);
4729 switch ((bit << 4) + frame) {
4731 format |= SND_SOC_DAIFMT_CBM_CFM;
4734 format |= SND_SOC_DAIFMT_CBM_CFS;
4737 format |= SND_SOC_DAIFMT_CBS_CFM;
4740 format |= SND_SOC_DAIFMT_CBS_CFS;
4746 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4748 int snd_soc_of_get_dai_name(struct device_node *of_node,
4749 const char **dai_name)
4751 struct snd_soc_component *pos;
4752 struct of_phandle_args args;
4755 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4756 "#sound-dai-cells", 0, &args);
4760 ret = -EPROBE_DEFER;
4762 mutex_lock(&client_mutex);
4763 list_for_each_entry(pos, &component_list, list) {
4764 if (pos->dev->of_node != args.np)
4767 if (pos->driver->of_xlate_dai_name) {
4768 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4772 switch (args.args_count) {
4774 id = 0; /* same as dai_drv[0] */
4784 if (id < 0 || id >= pos->num_dai) {
4791 *dai_name = pos->dai_drv[id].name;
4793 *dai_name = pos->name;
4798 mutex_unlock(&client_mutex);
4800 of_node_put(args.np);
4804 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4806 static int __init snd_soc_init(void)
4808 #ifdef CONFIG_DEBUG_FS
4809 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4810 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
4811 pr_warn("ASoC: Failed to create debugfs directory\n");
4812 snd_soc_debugfs_root = NULL;
4815 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4817 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4819 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4821 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
4823 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
4824 &platform_list_fops))
4825 pr_warn("ASoC: Failed to create platform list debugfs file\n");
4828 snd_soc_util_init();
4830 return platform_driver_register(&soc_driver);
4832 module_init(snd_soc_init);
4834 static void __exit snd_soc_exit(void)
4836 snd_soc_util_exit();
4838 #ifdef CONFIG_DEBUG_FS
4839 debugfs_remove_recursive(snd_soc_debugfs_root);
4841 platform_driver_unregister(&soc_driver);
4843 module_exit(snd_soc_exit);
4845 /* Module information */
4846 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4847 MODULE_DESCRIPTION("ALSA SoC Core");
4848 MODULE_LICENSE("GPL");
4849 MODULE_ALIAS("platform:soc-audio");