2 * soc-core.c -- ALSA SoC Audio Layer
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
30 #include <linux/bitops.h>
31 #include <linux/debugfs.h>
32 #include <linux/platform_device.h>
33 #include <linux/pinctrl/consumer.h>
34 #include <linux/ctype.h>
35 #include <linux/slab.h>
37 #include <linux/gpio.h>
38 #include <linux/of_gpio.h>
39 #include <sound/ac97_codec.h>
40 #include <sound/core.h>
41 #include <sound/jack.h>
42 #include <sound/pcm.h>
43 #include <sound/pcm_params.h>
44 #include <sound/soc.h>
45 #include <sound/soc-dpcm.h>
46 #include <sound/initval.h>
48 #define CREATE_TRACE_POINTS
49 #include <trace/events/asoc.h>
53 #ifdef CONFIG_DEBUG_FS
54 struct dentry *snd_soc_debugfs_root;
55 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
58 static DEFINE_MUTEX(client_mutex);
59 static LIST_HEAD(platform_list);
60 static LIST_HEAD(codec_list);
61 static LIST_HEAD(component_list);
64 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
68 static int pmdown_time = 5000;
69 module_param(pmdown_time, int, 0);
70 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
72 struct snd_ac97_reset_cfg {
74 struct pinctrl_state *pstate_reset;
75 struct pinctrl_state *pstate_warm_reset;
76 struct pinctrl_state *pstate_run;
82 /* returns the minimum number of bytes needed to represent
83 * a particular given value */
84 static int min_bytes_needed(unsigned long val)
89 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
92 c = (sizeof val * 8) - c;
100 /* fill buf which is 'len' bytes with a formatted
101 * string of the form 'reg: value\n' */
102 static int format_register_str(struct snd_soc_codec *codec,
103 unsigned int reg, char *buf, size_t len)
105 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
106 int regsize = codec->driver->reg_word_size * 2;
108 char tmpbuf[len + 1];
109 char regbuf[regsize + 1];
111 /* since tmpbuf is allocated on the stack, warn the callers if they
112 * try to abuse this function */
115 /* +2 for ': ' and + 1 for '\n' */
116 if (wordsize + regsize + 2 + 1 != len)
119 ret = snd_soc_read(codec, reg);
121 memset(regbuf, 'X', regsize);
122 regbuf[regsize] = '\0';
124 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
127 /* prepare the buffer */
128 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
129 /* copy it back to the caller without the '\0' */
130 memcpy(buf, tmpbuf, len);
135 /* codec register dump */
136 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
137 size_t count, loff_t pos)
140 int wordsize, regsize;
145 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
146 regsize = codec->driver->reg_word_size * 2;
148 len = wordsize + regsize + 2 + 1;
150 if (!codec->driver->reg_cache_size)
153 if (codec->driver->reg_cache_step)
154 step = codec->driver->reg_cache_step;
156 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
157 /* only support larger than PAGE_SIZE bytes debugfs
158 * entries for the default case */
160 if (total + len >= count - 1)
162 format_register_str(codec, i, buf + total, len);
168 total = min(total, count - 1);
173 static ssize_t codec_reg_show(struct device *dev,
174 struct device_attribute *attr, char *buf)
176 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
178 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
181 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
183 static ssize_t pmdown_time_show(struct device *dev,
184 struct device_attribute *attr, char *buf)
186 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
188 return sprintf(buf, "%ld\n", rtd->pmdown_time);
191 static ssize_t pmdown_time_set(struct device *dev,
192 struct device_attribute *attr,
193 const char *buf, size_t count)
195 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
198 ret = kstrtol(buf, 10, &rtd->pmdown_time);
205 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
207 #ifdef CONFIG_DEBUG_FS
208 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
209 size_t count, loff_t *ppos)
212 struct snd_soc_codec *codec = file->private_data;
215 if (*ppos < 0 || !count)
218 buf = kmalloc(count, GFP_KERNEL);
222 ret = soc_codec_reg_show(codec, buf, count, *ppos);
224 if (copy_to_user(user_buf, buf, ret)) {
235 static ssize_t codec_reg_write_file(struct file *file,
236 const char __user *user_buf, size_t count, loff_t *ppos)
241 unsigned long reg, value;
242 struct snd_soc_codec *codec = file->private_data;
245 buf_size = min(count, (sizeof(buf)-1));
246 if (copy_from_user(buf, user_buf, buf_size))
250 while (*start == ' ')
252 reg = simple_strtoul(start, &start, 16);
253 while (*start == ' ')
255 ret = kstrtoul(start, 16, &value);
259 /* Userspace has been fiddling around behind the kernel's back */
260 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
262 snd_soc_write(codec, reg, value);
266 static const struct file_operations codec_reg_fops = {
268 .read = codec_reg_read_file,
269 .write = codec_reg_write_file,
270 .llseek = default_llseek,
273 static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
275 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
277 codec->debugfs_codec_root = debugfs_create_dir(codec->component.name,
279 if (!codec->debugfs_codec_root) {
281 "ASoC: Failed to create codec debugfs directory\n");
285 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
287 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
290 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
291 codec->debugfs_codec_root,
292 codec, &codec_reg_fops);
293 if (!codec->debugfs_reg)
295 "ASoC: Failed to create codec register debugfs file\n");
297 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
300 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
302 debugfs_remove_recursive(codec->debugfs_codec_root);
305 static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
307 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
309 platform->debugfs_platform_root = debugfs_create_dir(
310 platform->component.name, debugfs_card_root);
311 if (!platform->debugfs_platform_root) {
312 dev_warn(platform->dev,
313 "ASoC: Failed to create platform debugfs directory\n");
317 snd_soc_dapm_debugfs_init(&platform->component.dapm,
318 platform->debugfs_platform_root);
321 static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
323 debugfs_remove_recursive(platform->debugfs_platform_root);
326 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
327 size_t count, loff_t *ppos)
329 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
330 ssize_t len, ret = 0;
331 struct snd_soc_codec *codec;
336 list_for_each_entry(codec, &codec_list, list) {
337 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
338 codec->component.name);
341 if (ret > PAGE_SIZE) {
348 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
355 static const struct file_operations codec_list_fops = {
356 .read = codec_list_read_file,
357 .llseek = default_llseek,/* read accesses f_pos */
360 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
361 size_t count, loff_t *ppos)
363 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
364 ssize_t len, ret = 0;
365 struct snd_soc_component *component;
366 struct snd_soc_dai *dai;
371 list_for_each_entry(component, &component_list, list) {
372 list_for_each_entry(dai, &component->dai_list, list) {
373 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
377 if (ret > PAGE_SIZE) {
384 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
391 static const struct file_operations dai_list_fops = {
392 .read = dai_list_read_file,
393 .llseek = default_llseek,/* read accesses f_pos */
396 static ssize_t platform_list_read_file(struct file *file,
397 char __user *user_buf,
398 size_t count, loff_t *ppos)
400 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
401 ssize_t len, ret = 0;
402 struct snd_soc_platform *platform;
407 list_for_each_entry(platform, &platform_list, list) {
408 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
409 platform->component.name);
412 if (ret > PAGE_SIZE) {
418 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
425 static const struct file_operations platform_list_fops = {
426 .read = platform_list_read_file,
427 .llseek = default_llseek,/* read accesses f_pos */
430 static void soc_init_card_debugfs(struct snd_soc_card *card)
432 card->debugfs_card_root = debugfs_create_dir(card->name,
433 snd_soc_debugfs_root);
434 if (!card->debugfs_card_root) {
436 "ASoC: Failed to create card debugfs directory\n");
440 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
441 card->debugfs_card_root,
443 if (!card->debugfs_pop_time)
445 "ASoC: Failed to create pop time debugfs file\n");
448 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
450 debugfs_remove_recursive(card->debugfs_card_root);
455 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
459 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
463 static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
467 static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
471 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
475 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
480 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
481 const char *dai_link, int stream)
485 for (i = 0; i < card->num_links; i++) {
486 if (card->rtd[i].dai_link->no_pcm &&
487 !strcmp(card->rtd[i].dai_link->name, dai_link))
488 return card->rtd[i].pcm->streams[stream].substream;
490 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
493 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
495 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
496 const char *dai_link)
500 for (i = 0; i < card->num_links; i++) {
501 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
502 return &card->rtd[i];
504 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
507 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
509 #ifdef CONFIG_SND_SOC_AC97_BUS
510 /* unregister ac97 codec */
511 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
513 if (codec->ac97->dev.bus)
514 device_unregister(&codec->ac97->dev);
518 /* stop no dev release warning */
519 static void soc_ac97_device_release(struct device *dev){}
521 /* register ac97 codec to bus */
522 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
526 codec->ac97->dev.bus = &ac97_bus_type;
527 codec->ac97->dev.parent = codec->card->dev;
528 codec->ac97->dev.release = soc_ac97_device_release;
530 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
531 codec->card->snd_card->number, 0, codec->component.name);
532 err = device_register(&codec->ac97->dev);
534 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
535 codec->ac97->dev.bus = NULL;
542 static void codec2codec_close_delayed_work(struct work_struct *work)
544 /* Currently nothing to do for c2c links
545 * Since c2c links are internal nodes in the DAPM graph and
546 * don't interface with the outside world or application layer
547 * we don't have to do any special handling on close.
551 #ifdef CONFIG_PM_SLEEP
552 /* powers down audio subsystem for suspend */
553 int snd_soc_suspend(struct device *dev)
555 struct snd_soc_card *card = dev_get_drvdata(dev);
556 struct snd_soc_codec *codec;
559 /* If the initialization of this soc device failed, there is no codec
560 * associated with it. Just bail out in this case.
562 if (list_empty(&card->codec_dev_list))
565 /* Due to the resume being scheduled into a workqueue we could
566 * suspend before that's finished - wait for it to complete.
568 snd_power_lock(card->snd_card);
569 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
570 snd_power_unlock(card->snd_card);
572 /* we're going to block userspace touching us until resume completes */
573 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
575 /* mute any active DACs */
576 for (i = 0; i < card->num_rtd; i++) {
577 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
578 struct snd_soc_dai_driver *drv = dai->driver;
580 if (card->rtd[i].dai_link->ignore_suspend)
583 if (drv->ops->digital_mute && dai->playback_active)
584 drv->ops->digital_mute(dai, 1);
587 /* suspend all pcms */
588 for (i = 0; i < card->num_rtd; i++) {
589 if (card->rtd[i].dai_link->ignore_suspend)
592 snd_pcm_suspend_all(card->rtd[i].pcm);
595 if (card->suspend_pre)
596 card->suspend_pre(card);
598 for (i = 0; i < card->num_rtd; i++) {
599 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
600 struct snd_soc_platform *platform = card->rtd[i].platform;
602 if (card->rtd[i].dai_link->ignore_suspend)
605 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
606 cpu_dai->driver->suspend(cpu_dai);
607 if (platform->driver->suspend && !platform->suspended) {
608 platform->driver->suspend(cpu_dai);
609 platform->suspended = 1;
613 /* close any waiting streams and save state */
614 for (i = 0; i < card->num_rtd; i++) {
615 flush_delayed_work(&card->rtd[i].delayed_work);
616 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
619 for (i = 0; i < card->num_rtd; i++) {
621 if (card->rtd[i].dai_link->ignore_suspend)
624 snd_soc_dapm_stream_event(&card->rtd[i],
625 SNDRV_PCM_STREAM_PLAYBACK,
626 SND_SOC_DAPM_STREAM_SUSPEND);
628 snd_soc_dapm_stream_event(&card->rtd[i],
629 SNDRV_PCM_STREAM_CAPTURE,
630 SND_SOC_DAPM_STREAM_SUSPEND);
633 /* Recheck all analogue paths too */
634 dapm_mark_io_dirty(&card->dapm);
635 snd_soc_dapm_sync(&card->dapm);
637 /* suspend all CODECs */
638 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
639 /* If there are paths active then the CODEC will be held with
640 * bias _ON and should not be suspended. */
641 if (!codec->suspended && codec->driver->suspend) {
642 switch (codec->dapm.bias_level) {
643 case SND_SOC_BIAS_STANDBY:
645 * If the CODEC is capable of idle
646 * bias off then being in STANDBY
647 * means it's doing something,
648 * otherwise fall through.
650 if (codec->dapm.idle_bias_off) {
652 "ASoC: idle_bias_off CODEC on over suspend\n");
655 case SND_SOC_BIAS_OFF:
656 codec->driver->suspend(codec);
657 codec->suspended = 1;
658 codec->cache_sync = 1;
659 if (codec->component.regmap)
660 regcache_mark_dirty(codec->component.regmap);
661 /* deactivate pins to sleep state */
662 pinctrl_pm_select_sleep_state(codec->dev);
666 "ASoC: CODEC is on over suspend\n");
672 for (i = 0; i < card->num_rtd; i++) {
673 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
675 if (card->rtd[i].dai_link->ignore_suspend)
678 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
679 cpu_dai->driver->suspend(cpu_dai);
681 /* deactivate pins to sleep state */
682 pinctrl_pm_select_sleep_state(cpu_dai->dev);
685 if (card->suspend_post)
686 card->suspend_post(card);
690 EXPORT_SYMBOL_GPL(snd_soc_suspend);
692 /* deferred resume work, so resume can complete before we finished
693 * setting our codec back up, which can be very slow on I2C
695 static void soc_resume_deferred(struct work_struct *work)
697 struct snd_soc_card *card =
698 container_of(work, struct snd_soc_card, deferred_resume_work);
699 struct snd_soc_codec *codec;
702 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
703 * so userspace apps are blocked from touching us
706 dev_dbg(card->dev, "ASoC: starting resume work\n");
708 /* Bring us up into D2 so that DAPM starts enabling things */
709 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
711 if (card->resume_pre)
712 card->resume_pre(card);
714 /* resume AC97 DAIs */
715 for (i = 0; i < card->num_rtd; i++) {
716 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
718 if (card->rtd[i].dai_link->ignore_suspend)
721 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
722 cpu_dai->driver->resume(cpu_dai);
725 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
726 /* If the CODEC was idle over suspend then it will have been
727 * left with bias OFF or STANDBY and suspended so we must now
728 * resume. Otherwise the suspend was suppressed.
730 if (codec->driver->resume && codec->suspended) {
731 switch (codec->dapm.bias_level) {
732 case SND_SOC_BIAS_STANDBY:
733 case SND_SOC_BIAS_OFF:
734 codec->driver->resume(codec);
735 codec->suspended = 0;
739 "ASoC: CODEC was on over suspend\n");
745 for (i = 0; i < card->num_rtd; i++) {
747 if (card->rtd[i].dai_link->ignore_suspend)
750 snd_soc_dapm_stream_event(&card->rtd[i],
751 SNDRV_PCM_STREAM_PLAYBACK,
752 SND_SOC_DAPM_STREAM_RESUME);
754 snd_soc_dapm_stream_event(&card->rtd[i],
755 SNDRV_PCM_STREAM_CAPTURE,
756 SND_SOC_DAPM_STREAM_RESUME);
759 /* unmute any active DACs */
760 for (i = 0; i < card->num_rtd; i++) {
761 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
762 struct snd_soc_dai_driver *drv = dai->driver;
764 if (card->rtd[i].dai_link->ignore_suspend)
767 if (drv->ops->digital_mute && dai->playback_active)
768 drv->ops->digital_mute(dai, 0);
771 for (i = 0; i < card->num_rtd; i++) {
772 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
773 struct snd_soc_platform *platform = card->rtd[i].platform;
775 if (card->rtd[i].dai_link->ignore_suspend)
778 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
779 cpu_dai->driver->resume(cpu_dai);
780 if (platform->driver->resume && platform->suspended) {
781 platform->driver->resume(cpu_dai);
782 platform->suspended = 0;
786 if (card->resume_post)
787 card->resume_post(card);
789 dev_dbg(card->dev, "ASoC: resume work completed\n");
791 /* userspace can access us now we are back as we were before */
792 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
794 /* Recheck all analogue paths too */
795 dapm_mark_io_dirty(&card->dapm);
796 snd_soc_dapm_sync(&card->dapm);
799 /* powers up audio subsystem after a suspend */
800 int snd_soc_resume(struct device *dev)
802 struct snd_soc_card *card = dev_get_drvdata(dev);
803 int i, ac97_control = 0;
805 /* If the initialization of this soc device failed, there is no codec
806 * associated with it. Just bail out in this case.
808 if (list_empty(&card->codec_dev_list))
811 /* activate pins from sleep state */
812 for (i = 0; i < card->num_rtd; i++) {
813 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
814 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
816 pinctrl_pm_select_default_state(cpu_dai->dev);
817 if (codec_dai->active)
818 pinctrl_pm_select_default_state(codec_dai->dev);
821 /* AC97 devices might have other drivers hanging off them so
822 * need to resume immediately. Other drivers don't have that
823 * problem and may take a substantial amount of time to resume
824 * due to I/O costs and anti-pop so handle them out of line.
826 for (i = 0; i < card->num_rtd; i++) {
827 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
828 ac97_control |= cpu_dai->driver->ac97_control;
831 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
832 soc_resume_deferred(&card->deferred_resume_work);
834 dev_dbg(dev, "ASoC: Scheduling resume work\n");
835 if (!schedule_work(&card->deferred_resume_work))
836 dev_err(dev, "ASoC: resume work item may be lost\n");
841 EXPORT_SYMBOL_GPL(snd_soc_resume);
843 #define snd_soc_suspend NULL
844 #define snd_soc_resume NULL
847 static const struct snd_soc_dai_ops null_dai_ops = {
850 static struct snd_soc_codec *soc_find_codec(const struct device_node *codec_of_node,
851 const char *codec_name)
853 struct snd_soc_codec *codec;
855 list_for_each_entry(codec, &codec_list, list) {
857 if (codec->dev->of_node != codec_of_node)
860 if (strcmp(codec->component.name, codec_name))
870 static struct snd_soc_dai *soc_find_codec_dai(struct snd_soc_codec *codec,
871 const char *codec_dai_name)
873 struct snd_soc_dai *codec_dai;
875 list_for_each_entry(codec_dai, &codec->component.dai_list, list) {
876 if (!strcmp(codec_dai->name, codec_dai_name)) {
884 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
886 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
887 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
888 struct snd_soc_component *component;
889 struct snd_soc_platform *platform;
890 struct snd_soc_dai *cpu_dai;
891 const char *platform_name;
893 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
895 /* Find CPU DAI from registered DAIs*/
896 list_for_each_entry(component, &component_list, list) {
897 if (dai_link->cpu_of_node &&
898 component->dev->of_node != dai_link->cpu_of_node)
900 if (dai_link->cpu_name &&
901 strcmp(dev_name(component->dev), dai_link->cpu_name))
903 list_for_each_entry(cpu_dai, &component->dai_list, list) {
904 if (dai_link->cpu_dai_name &&
905 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
908 rtd->cpu_dai = cpu_dai;
913 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
914 dai_link->cpu_dai_name);
915 return -EPROBE_DEFER;
918 /* Find CODEC from registered list */
919 rtd->codec = soc_find_codec(dai_link->codec_of_node,
920 dai_link->codec_name);
922 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
923 dai_link->codec_name);
924 return -EPROBE_DEFER;
927 /* Find CODEC DAI from registered list */
928 rtd->codec_dai = soc_find_codec_dai(rtd->codec,
929 dai_link->codec_dai_name);
930 if (!rtd->codec_dai) {
931 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
932 dai_link->codec_dai_name);
933 return -EPROBE_DEFER;
936 /* if there's no platform we match on the empty platform */
937 platform_name = dai_link->platform_name;
938 if (!platform_name && !dai_link->platform_of_node)
939 platform_name = "snd-soc-dummy";
941 /* find one from the set of registered platforms */
942 list_for_each_entry(platform, &platform_list, list) {
943 if (dai_link->platform_of_node) {
944 if (platform->dev->of_node !=
945 dai_link->platform_of_node)
948 if (strcmp(platform->component.name, platform_name))
952 rtd->platform = platform;
954 if (!rtd->platform) {
955 dev_err(card->dev, "ASoC: platform %s not registered\n",
956 dai_link->platform_name);
957 return -EPROBE_DEFER;
965 static int soc_remove_platform(struct snd_soc_platform *platform)
969 if (platform->driver->remove) {
970 ret = platform->driver->remove(platform);
972 dev_err(platform->dev, "ASoC: failed to remove %d\n",
976 /* Make sure all DAPM widgets are freed */
977 snd_soc_dapm_free(&platform->component.dapm);
979 soc_cleanup_platform_debugfs(platform);
980 platform->probed = 0;
981 list_del(&platform->card_list);
982 module_put(platform->dev->driver->owner);
987 static void soc_remove_codec(struct snd_soc_codec *codec)
991 if (codec->driver->remove) {
992 err = codec->driver->remove(codec);
994 dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
997 /* Make sure all DAPM widgets are freed */
998 snd_soc_dapm_free(&codec->dapm);
1000 soc_cleanup_codec_debugfs(codec);
1002 list_del(&codec->card_list);
1003 module_put(codec->dev->driver->owner);
1006 static void soc_remove_codec_dai(struct snd_soc_dai *codec_dai, int order)
1010 if (codec_dai && codec_dai->probed &&
1011 codec_dai->driver->remove_order == order) {
1012 if (codec_dai->driver->remove) {
1013 err = codec_dai->driver->remove(codec_dai);
1015 dev_err(codec_dai->dev,
1016 "ASoC: failed to remove %s: %d\n",
1017 codec_dai->name, err);
1019 codec_dai->probed = 0;
1023 static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
1025 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1026 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1029 /* unregister the rtd device */
1030 if (rtd->dev_registered) {
1031 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1032 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1033 device_unregister(rtd->dev);
1034 rtd->dev_registered = 0;
1037 /* remove the CODEC DAI */
1038 soc_remove_codec_dai(codec_dai, order);
1040 /* remove the cpu_dai */
1041 if (cpu_dai && cpu_dai->probed &&
1042 cpu_dai->driver->remove_order == order) {
1043 if (cpu_dai->driver->remove) {
1044 err = cpu_dai->driver->remove(cpu_dai);
1046 dev_err(cpu_dai->dev,
1047 "ASoC: failed to remove %s: %d\n",
1048 cpu_dai->name, err);
1050 cpu_dai->probed = 0;
1052 if (!cpu_dai->codec) {
1053 snd_soc_dapm_free(&cpu_dai->dapm);
1054 module_put(cpu_dai->dev->driver->owner);
1059 static void soc_remove_link_components(struct snd_soc_card *card, int num,
1062 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1063 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1064 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1065 struct snd_soc_platform *platform = rtd->platform;
1066 struct snd_soc_codec *codec;
1068 /* remove the platform */
1069 if (platform && platform->probed &&
1070 platform->driver->remove_order == order) {
1071 soc_remove_platform(platform);
1074 /* remove the CODEC-side CODEC */
1076 codec = codec_dai->codec;
1077 if (codec && codec->probed &&
1078 codec->driver->remove_order == order)
1079 soc_remove_codec(codec);
1082 /* remove any CPU-side CODEC */
1084 codec = cpu_dai->codec;
1085 if (codec && codec->probed &&
1086 codec->driver->remove_order == order)
1087 soc_remove_codec(codec);
1091 static void soc_remove_dai_links(struct snd_soc_card *card)
1095 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1097 for (dai = 0; dai < card->num_rtd; dai++)
1098 soc_remove_link_dais(card, dai, order);
1101 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1103 for (dai = 0; dai < card->num_rtd; dai++)
1104 soc_remove_link_components(card, dai, order);
1110 static void soc_set_name_prefix(struct snd_soc_card *card,
1111 struct snd_soc_component *component)
1115 if (card->codec_conf == NULL)
1118 for (i = 0; i < card->num_configs; i++) {
1119 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1120 if (map->of_node && component->dev->of_node != map->of_node)
1122 if (map->dev_name && strcmp(component->name, map->dev_name))
1124 component->name_prefix = map->name_prefix;
1129 static int soc_probe_codec(struct snd_soc_card *card,
1130 struct snd_soc_codec *codec)
1133 const struct snd_soc_codec_driver *driver = codec->driver;
1134 struct snd_soc_dai *dai;
1137 codec->dapm.card = card;
1138 soc_set_name_prefix(card, &codec->component);
1140 if (!try_module_get(codec->dev->driver->owner))
1143 soc_init_codec_debugfs(codec);
1145 if (driver->dapm_widgets) {
1146 ret = snd_soc_dapm_new_controls(&codec->dapm,
1147 driver->dapm_widgets,
1148 driver->num_dapm_widgets);
1152 "Failed to create new controls %d\n", ret);
1157 /* Create DAPM widgets for each DAI stream */
1158 list_for_each_entry(dai, &codec->component.dai_list, list) {
1159 ret = snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1163 "Failed to create DAI widgets %d\n", ret);
1168 codec->dapm.idle_bias_off = driver->idle_bias_off;
1170 if (driver->probe) {
1171 ret = driver->probe(codec);
1174 "ASoC: failed to probe CODEC %d\n", ret);
1177 WARN(codec->dapm.idle_bias_off &&
1178 codec->dapm.bias_level != SND_SOC_BIAS_OFF,
1179 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1180 codec->component.name);
1183 if (driver->controls)
1184 snd_soc_add_codec_controls(codec, driver->controls,
1185 driver->num_controls);
1186 if (driver->dapm_routes)
1187 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1188 driver->num_dapm_routes);
1190 /* mark codec as probed and add to card codec list */
1192 list_add(&codec->card_list, &card->codec_dev_list);
1193 list_add(&codec->dapm.list, &card->dapm_list);
1198 soc_cleanup_codec_debugfs(codec);
1199 module_put(codec->dev->driver->owner);
1204 static int soc_probe_platform(struct snd_soc_card *card,
1205 struct snd_soc_platform *platform)
1208 const struct snd_soc_platform_driver *driver = platform->driver;
1209 struct snd_soc_component *component;
1210 struct snd_soc_dai *dai;
1212 platform->card = card;
1213 platform->component.dapm.card = card;
1215 if (!try_module_get(platform->dev->driver->owner))
1218 soc_init_platform_debugfs(platform);
1220 if (driver->dapm_widgets)
1221 snd_soc_dapm_new_controls(&platform->component.dapm,
1222 driver->dapm_widgets, driver->num_dapm_widgets);
1224 /* Create DAPM widgets for each DAI stream */
1225 list_for_each_entry(component, &component_list, list) {
1226 if (component->dev != platform->dev)
1228 list_for_each_entry(dai, &component->dai_list, list)
1229 snd_soc_dapm_new_dai_widgets(&platform->component.dapm,
1233 platform->component.dapm.idle_bias_off = 1;
1235 if (driver->probe) {
1236 ret = driver->probe(platform);
1238 dev_err(platform->dev,
1239 "ASoC: failed to probe platform %d\n", ret);
1244 if (driver->controls)
1245 snd_soc_add_platform_controls(platform, driver->controls,
1246 driver->num_controls);
1247 if (driver->dapm_routes)
1248 snd_soc_dapm_add_routes(&platform->component.dapm,
1249 driver->dapm_routes, driver->num_dapm_routes);
1251 /* mark platform as probed and add to card platform list */
1252 platform->probed = 1;
1253 list_add(&platform->card_list, &card->platform_dev_list);
1254 list_add(&platform->component.dapm.list, &card->dapm_list);
1259 soc_cleanup_platform_debugfs(platform);
1260 module_put(platform->dev->driver->owner);
1265 static void rtd_release(struct device *dev)
1270 static int soc_aux_dev_init(struct snd_soc_card *card,
1271 struct snd_soc_codec *codec,
1274 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1275 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1280 /* do machine specific initialization */
1281 if (aux_dev->init) {
1282 ret = aux_dev->init(&codec->dapm);
1292 static int soc_dai_link_init(struct snd_soc_card *card,
1293 struct snd_soc_codec *codec,
1296 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1297 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1302 /* do machine specific initialization */
1303 if (dai_link->init) {
1304 ret = dai_link->init(rtd);
1314 static int soc_post_component_init(struct snd_soc_card *card,
1315 struct snd_soc_codec *codec,
1316 int num, int dailess)
1318 struct snd_soc_dai_link *dai_link = NULL;
1319 struct snd_soc_aux_dev *aux_dev = NULL;
1320 struct snd_soc_pcm_runtime *rtd;
1325 dai_link = &card->dai_link[num];
1326 rtd = &card->rtd[num];
1327 name = dai_link->name;
1328 ret = soc_dai_link_init(card, codec, num);
1330 aux_dev = &card->aux_dev[num];
1331 rtd = &card->rtd_aux[num];
1332 name = aux_dev->name;
1333 ret = soc_aux_dev_init(card, codec, num);
1337 dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
1341 /* register the rtd device */
1342 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1345 device_initialize(rtd->dev);
1346 rtd->dev->parent = card->dev;
1347 rtd->dev->release = rtd_release;
1348 rtd->dev->init_name = name;
1349 dev_set_drvdata(rtd->dev, rtd);
1350 mutex_init(&rtd->pcm_mutex);
1351 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1352 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1353 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1354 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1355 ret = device_add(rtd->dev);
1357 /* calling put_device() here to free the rtd->dev */
1358 put_device(rtd->dev);
1360 "ASoC: failed to register runtime device: %d\n", ret);
1363 rtd->dev_registered = 1;
1365 /* add DAPM sysfs entries for this codec */
1366 ret = snd_soc_dapm_sys_add(rtd->dev);
1369 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
1371 /* add codec sysfs entries */
1372 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1375 "ASoC: failed to add codec sysfs files: %d\n", ret);
1377 #ifdef CONFIG_DEBUG_FS
1378 /* add DPCM sysfs entries */
1379 if (!dailess && !dai_link->dynamic)
1382 ret = soc_dpcm_debugfs_add(rtd);
1384 dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
1391 static int soc_probe_link_components(struct snd_soc_card *card, int num,
1394 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1395 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1396 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1397 struct snd_soc_platform *platform = rtd->platform;
1400 /* probe the CPU-side component, if it is a CODEC */
1401 if (cpu_dai->codec &&
1402 !cpu_dai->codec->probed &&
1403 cpu_dai->codec->driver->probe_order == order) {
1404 ret = soc_probe_codec(card, cpu_dai->codec);
1409 /* probe the CODEC-side component */
1410 if (!codec_dai->codec->probed &&
1411 codec_dai->codec->driver->probe_order == order) {
1412 ret = soc_probe_codec(card, codec_dai->codec);
1417 /* probe the platform */
1418 if (!platform->probed &&
1419 platform->driver->probe_order == order) {
1420 ret = soc_probe_platform(card, platform);
1428 static int soc_probe_codec_dai(struct snd_soc_card *card,
1429 struct snd_soc_dai *codec_dai,
1434 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1435 if (codec_dai->driver->probe) {
1436 ret = codec_dai->driver->probe(codec_dai);
1438 dev_err(codec_dai->dev,
1439 "ASoC: failed to probe CODEC DAI %s: %d\n",
1440 codec_dai->name, ret);
1445 /* mark codec_dai as probed and add to card dai list */
1446 codec_dai->probed = 1;
1452 static int soc_link_dai_widgets(struct snd_soc_card *card,
1453 struct snd_soc_dai_link *dai_link,
1454 struct snd_soc_dai *cpu_dai,
1455 struct snd_soc_dai *codec_dai)
1457 struct snd_soc_dapm_widget *play_w, *capture_w;
1460 /* link the DAI widgets */
1461 play_w = codec_dai->playback_widget;
1462 capture_w = cpu_dai->capture_widget;
1463 if (play_w && capture_w) {
1464 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1467 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1468 play_w->name, capture_w->name, ret);
1473 play_w = cpu_dai->playback_widget;
1474 capture_w = codec_dai->capture_widget;
1475 if (play_w && capture_w) {
1476 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1479 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1480 play_w->name, capture_w->name, ret);
1488 static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1490 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1491 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1492 struct snd_soc_codec *codec = rtd->codec;
1493 struct snd_soc_platform *platform = rtd->platform;
1494 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1495 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1498 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1499 card->name, num, order);
1501 /* config components */
1502 cpu_dai->platform = platform;
1503 codec_dai->card = card;
1504 cpu_dai->card = card;
1506 /* set default power off timeout */
1507 rtd->pmdown_time = pmdown_time;
1509 /* probe the cpu_dai */
1510 if (!cpu_dai->probed &&
1511 cpu_dai->driver->probe_order == order) {
1512 if (!cpu_dai->codec) {
1513 cpu_dai->dapm.card = card;
1514 if (!try_module_get(cpu_dai->dev->driver->owner))
1517 list_add(&cpu_dai->dapm.list, &card->dapm_list);
1520 if (cpu_dai->driver->probe) {
1521 ret = cpu_dai->driver->probe(cpu_dai);
1523 dev_err(cpu_dai->dev,
1524 "ASoC: failed to probe CPU DAI %s: %d\n",
1525 cpu_dai->name, ret);
1526 module_put(cpu_dai->dev->driver->owner);
1530 cpu_dai->probed = 1;
1533 /* probe the CODEC DAI */
1534 ret = soc_probe_codec_dai(card, codec_dai, order);
1538 /* complete DAI probe during last probe */
1539 if (order != SND_SOC_COMP_ORDER_LAST)
1542 ret = soc_post_component_init(card, codec, num, 0);
1546 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1548 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1551 if (cpu_dai->driver->compress_dai) {
1552 /*create compress_device"*/
1553 ret = soc_new_compress(rtd, num);
1555 dev_err(card->dev, "ASoC: can't create compress %s\n",
1556 dai_link->stream_name);
1561 if (!dai_link->params) {
1562 /* create the pcm */
1563 ret = soc_new_pcm(rtd, num);
1565 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1566 dai_link->stream_name, ret);
1570 INIT_DELAYED_WORK(&rtd->delayed_work,
1571 codec2codec_close_delayed_work);
1573 /* link the DAI widgets */
1574 ret = soc_link_dai_widgets(card, dai_link,
1575 cpu_dai, codec_dai);
1581 /* add platform data for AC97 devices */
1582 if (rtd->codec_dai->driver->ac97_control)
1583 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1588 #ifdef CONFIG_SND_SOC_AC97_BUS
1589 static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1590 struct snd_soc_dai *codec_dai)
1594 /* Only instantiate AC97 if not already done by the adaptor
1595 * for the generic AC97 subsystem.
1597 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
1599 * It is possible that the AC97 device is already registered to
1600 * the device subsystem. This happens when the device is created
1601 * via snd_ac97_mixer(). Currently only SoC codec that does so
1602 * is the generic AC97 glue but others migh emerge.
1604 * In those cases we don't try to register the device again.
1606 if (!codec->ac97_created)
1609 ret = soc_ac97_dev_register(codec);
1612 "ASoC: AC97 device register failed: %d\n", ret);
1616 codec->ac97_registered = 1;
1621 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1623 return soc_register_ac97_codec(rtd->codec, rtd->codec_dai);
1626 static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
1628 if (codec->ac97_registered) {
1629 soc_ac97_dev_unregister(codec);
1630 codec->ac97_registered = 0;
1634 static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1636 soc_unregister_ac97_codec(rtd->codec);
1640 static struct snd_soc_codec *soc_find_matching_codec(struct snd_soc_card *card,
1643 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1644 struct snd_soc_codec *codec;
1646 /* find CODEC from registered CODECs */
1647 list_for_each_entry(codec, &codec_list, list) {
1648 if (aux_dev->codec_of_node &&
1649 (codec->dev->of_node != aux_dev->codec_of_node))
1651 if (aux_dev->codec_name &&
1652 strcmp(codec->component.name, aux_dev->codec_name))
1660 static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1662 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1663 const char *codecname = aux_dev->codec_name;
1664 struct snd_soc_codec *codec = soc_find_matching_codec(card, num);
1668 if (aux_dev->codec_of_node)
1669 codecname = of_node_full_name(aux_dev->codec_of_node);
1671 dev_err(card->dev, "ASoC: %s not registered\n", codecname);
1672 return -EPROBE_DEFER;
1675 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1677 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1678 const char *codecname = aux_dev->codec_name;
1680 struct snd_soc_codec *codec = soc_find_matching_codec(card, num);
1683 if (aux_dev->codec_of_node)
1684 codecname = of_node_full_name(aux_dev->codec_of_node);
1686 /* codec not found */
1687 dev_err(card->dev, "ASoC: codec %s not found", codecname);
1688 return -EPROBE_DEFER;
1691 if (codec->probed) {
1692 dev_err(codec->dev, "ASoC: codec already probed");
1696 ret = soc_probe_codec(card, codec);
1700 ret = soc_post_component_init(card, codec, num, 1);
1705 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1707 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1708 struct snd_soc_codec *codec = rtd->codec;
1710 /* unregister the rtd device */
1711 if (rtd->dev_registered) {
1712 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1713 device_unregister(rtd->dev);
1714 rtd->dev_registered = 0;
1717 if (codec && codec->probed)
1718 soc_remove_codec(codec);
1721 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1725 if (codec->cache_init)
1728 ret = snd_soc_cache_init(codec);
1731 "ASoC: Failed to set cache compression type: %d\n",
1735 codec->cache_init = 1;
1739 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1741 struct snd_soc_codec *codec;
1742 struct snd_soc_dai_link *dai_link;
1743 int ret, i, order, dai_fmt;
1745 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1748 for (i = 0; i < card->num_links; i++) {
1749 ret = soc_bind_dai_link(card, i);
1754 /* check aux_devs too */
1755 for (i = 0; i < card->num_aux_devs; i++) {
1756 ret = soc_check_aux_dev(card, i);
1761 /* initialize the register cache for each available codec */
1762 list_for_each_entry(codec, &codec_list, list) {
1763 if (codec->cache_init)
1765 ret = snd_soc_init_codec_cache(codec);
1770 /* card bind complete so register a sound card */
1771 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1772 card->owner, 0, &card->snd_card);
1775 "ASoC: can't create sound card for card %s: %d\n",
1780 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1781 card->dapm.dev = card->dev;
1782 card->dapm.card = card;
1783 list_add(&card->dapm.list, &card->dapm_list);
1785 #ifdef CONFIG_DEBUG_FS
1786 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1789 #ifdef CONFIG_PM_SLEEP
1790 /* deferred resume work */
1791 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1794 if (card->dapm_widgets)
1795 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1796 card->num_dapm_widgets);
1798 /* initialise the sound card only once */
1800 ret = card->probe(card);
1802 goto card_probe_error;
1805 /* probe all components used by DAI links on this card */
1806 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1808 for (i = 0; i < card->num_links; i++) {
1809 ret = soc_probe_link_components(card, i, order);
1812 "ASoC: failed to instantiate card %d\n",
1819 /* probe all DAI links on this card */
1820 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1822 for (i = 0; i < card->num_links; i++) {
1823 ret = soc_probe_link_dais(card, i, order);
1826 "ASoC: failed to instantiate card %d\n",
1833 for (i = 0; i < card->num_aux_devs; i++) {
1834 ret = soc_probe_aux_dev(card, i);
1837 "ASoC: failed to add auxiliary devices %d\n",
1839 goto probe_aux_dev_err;
1843 snd_soc_dapm_link_dai_widgets(card);
1844 snd_soc_dapm_connect_dai_link_widgets(card);
1847 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1849 if (card->dapm_routes)
1850 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1851 card->num_dapm_routes);
1853 for (i = 0; i < card->num_links; i++) {
1854 dai_link = &card->dai_link[i];
1855 dai_fmt = dai_link->dai_fmt;
1858 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1860 if (ret != 0 && ret != -ENOTSUPP)
1861 dev_warn(card->rtd[i].codec_dai->dev,
1862 "ASoC: Failed to set DAI format: %d\n",
1866 /* If this is a regular CPU link there will be a platform */
1868 (dai_link->platform_name || dai_link->platform_of_node)) {
1869 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1871 if (ret != 0 && ret != -ENOTSUPP)
1872 dev_warn(card->rtd[i].cpu_dai->dev,
1873 "ASoC: Failed to set DAI format: %d\n",
1875 } else if (dai_fmt) {
1876 /* Flip the polarity for the "CPU" end */
1877 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1878 switch (dai_link->dai_fmt &
1879 SND_SOC_DAIFMT_MASTER_MASK) {
1880 case SND_SOC_DAIFMT_CBM_CFM:
1881 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1883 case SND_SOC_DAIFMT_CBM_CFS:
1884 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1886 case SND_SOC_DAIFMT_CBS_CFM:
1887 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1889 case SND_SOC_DAIFMT_CBS_CFS:
1890 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1894 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1896 if (ret != 0 && ret != -ENOTSUPP)
1897 dev_warn(card->rtd[i].cpu_dai->dev,
1898 "ASoC: Failed to set DAI format: %d\n",
1903 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1905 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1906 "%s", card->long_name ? card->long_name : card->name);
1907 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1908 "%s", card->driver_name ? card->driver_name : card->name);
1909 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1910 switch (card->snd_card->driver[i]) {
1916 if (!isalnum(card->snd_card->driver[i]))
1917 card->snd_card->driver[i] = '_';
1922 if (card->late_probe) {
1923 ret = card->late_probe(card);
1925 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
1927 goto probe_aux_dev_err;
1931 if (card->fully_routed)
1932 snd_soc_dapm_auto_nc_pins(card);
1934 snd_soc_dapm_new_widgets(card);
1936 ret = snd_card_register(card->snd_card);
1938 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1940 goto probe_aux_dev_err;
1943 #ifdef CONFIG_SND_SOC_AC97_BUS
1944 /* register any AC97 codecs */
1945 for (i = 0; i < card->num_rtd; i++) {
1946 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1949 "ASoC: failed to register AC97: %d\n", ret);
1951 soc_unregister_ac97_dai_link(&card->rtd[i]);
1952 goto probe_aux_dev_err;
1957 card->instantiated = 1;
1958 snd_soc_dapm_sync(&card->dapm);
1959 mutex_unlock(&card->mutex);
1964 for (i = 0; i < card->num_aux_devs; i++)
1965 soc_remove_aux_dev(card, i);
1968 soc_remove_dai_links(card);
1974 snd_card_free(card->snd_card);
1977 mutex_unlock(&card->mutex);
1982 /* probes a new socdev */
1983 static int soc_probe(struct platform_device *pdev)
1985 struct snd_soc_card *card = platform_get_drvdata(pdev);
1988 * no card, so machine driver should be registering card
1989 * we should not be here in that case so ret error
1994 dev_warn(&pdev->dev,
1995 "ASoC: machine %s should use snd_soc_register_card()\n",
1998 /* Bodge while we unpick instantiation */
1999 card->dev = &pdev->dev;
2001 return snd_soc_register_card(card);
2004 static int soc_cleanup_card_resources(struct snd_soc_card *card)
2008 /* make sure any delayed work runs */
2009 for (i = 0; i < card->num_rtd; i++) {
2010 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2011 flush_delayed_work(&rtd->delayed_work);
2014 /* remove auxiliary devices */
2015 for (i = 0; i < card->num_aux_devs; i++)
2016 soc_remove_aux_dev(card, i);
2018 /* remove and free each DAI */
2019 soc_remove_dai_links(card);
2021 soc_cleanup_card_debugfs(card);
2023 /* remove the card */
2027 snd_soc_dapm_free(&card->dapm);
2029 snd_card_free(card->snd_card);
2034 /* removes a socdev */
2035 static int soc_remove(struct platform_device *pdev)
2037 struct snd_soc_card *card = platform_get_drvdata(pdev);
2039 snd_soc_unregister_card(card);
2043 int snd_soc_poweroff(struct device *dev)
2045 struct snd_soc_card *card = dev_get_drvdata(dev);
2048 if (!card->instantiated)
2051 /* Flush out pmdown_time work - we actually do want to run it
2052 * now, we're shutting down so no imminent restart. */
2053 for (i = 0; i < card->num_rtd; i++) {
2054 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2055 flush_delayed_work(&rtd->delayed_work);
2058 snd_soc_dapm_shutdown(card);
2060 /* deactivate pins to sleep state */
2061 for (i = 0; i < card->num_rtd; i++) {
2062 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
2063 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
2064 pinctrl_pm_select_sleep_state(codec_dai->dev);
2065 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2070 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2072 const struct dev_pm_ops snd_soc_pm_ops = {
2073 .suspend = snd_soc_suspend,
2074 .resume = snd_soc_resume,
2075 .freeze = snd_soc_suspend,
2076 .thaw = snd_soc_resume,
2077 .poweroff = snd_soc_poweroff,
2078 .restore = snd_soc_resume,
2080 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2082 /* ASoC platform driver */
2083 static struct platform_driver soc_driver = {
2085 .name = "soc-audio",
2086 .owner = THIS_MODULE,
2087 .pm = &snd_soc_pm_ops,
2090 .remove = soc_remove,
2094 * snd_soc_new_ac97_codec - initailise AC97 device
2095 * @codec: audio codec
2096 * @ops: AC97 bus operations
2097 * @num: AC97 codec number
2099 * Initialises AC97 codec resources for use by ad-hoc devices only.
2101 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2102 struct snd_ac97_bus_ops *ops, int num)
2104 mutex_lock(&codec->mutex);
2106 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2107 if (codec->ac97 == NULL) {
2108 mutex_unlock(&codec->mutex);
2112 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2113 if (codec->ac97->bus == NULL) {
2116 mutex_unlock(&codec->mutex);
2120 codec->ac97->bus->ops = ops;
2121 codec->ac97->num = num;
2124 * Mark the AC97 device to be created by us. This way we ensure that the
2125 * device will be registered with the device subsystem later on.
2127 codec->ac97_created = 1;
2129 mutex_unlock(&codec->mutex);
2132 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2134 static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2136 static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2138 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2140 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2142 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2146 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2148 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2152 static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2154 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2156 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2158 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2159 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2160 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2164 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2166 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2170 static int snd_soc_ac97_parse_pinctl(struct device *dev,
2171 struct snd_ac97_reset_cfg *cfg)
2174 struct pinctrl_state *state;
2178 p = devm_pinctrl_get(dev);
2180 dev_err(dev, "Failed to get pinctrl\n");
2185 state = pinctrl_lookup_state(p, "ac97-reset");
2186 if (IS_ERR(state)) {
2187 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
2188 return PTR_ERR(state);
2190 cfg->pstate_reset = state;
2192 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2193 if (IS_ERR(state)) {
2194 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
2195 return PTR_ERR(state);
2197 cfg->pstate_warm_reset = state;
2199 state = pinctrl_lookup_state(p, "ac97-running");
2200 if (IS_ERR(state)) {
2201 dev_err(dev, "Can't find pinctrl state ac97-running\n");
2202 return PTR_ERR(state);
2204 cfg->pstate_run = state;
2206 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2208 dev_err(dev, "Can't find ac97-sync gpio\n");
2211 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2213 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2216 cfg->gpio_sync = gpio;
2218 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2220 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2223 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2225 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2228 cfg->gpio_sdata = gpio;
2230 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2232 dev_err(dev, "Can't find ac97-reset gpio\n");
2235 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2237 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2240 cfg->gpio_reset = gpio;
2245 struct snd_ac97_bus_ops *soc_ac97_ops;
2246 EXPORT_SYMBOL_GPL(soc_ac97_ops);
2248 int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2250 if (ops == soc_ac97_ops)
2253 if (soc_ac97_ops && ops)
2260 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2263 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2265 * This function sets the reset and warm_reset properties of ops and parses
2266 * the device node of pdev to get pinctrl states and gpio numbers to use.
2268 int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2269 struct platform_device *pdev)
2271 struct device *dev = &pdev->dev;
2272 struct snd_ac97_reset_cfg cfg;
2275 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2279 ret = snd_soc_set_ac97_ops(ops);
2283 ops->warm_reset = snd_soc_ac97_warm_reset;
2284 ops->reset = snd_soc_ac97_reset;
2286 snd_ac97_rst_cfg = cfg;
2289 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2292 * snd_soc_free_ac97_codec - free AC97 codec device
2293 * @codec: audio codec
2295 * Frees AC97 codec device resources.
2297 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2299 mutex_lock(&codec->mutex);
2300 #ifdef CONFIG_SND_SOC_AC97_BUS
2301 soc_unregister_ac97_codec(codec);
2303 kfree(codec->ac97->bus);
2306 codec->ac97_created = 0;
2307 mutex_unlock(&codec->mutex);
2309 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2312 * snd_soc_cnew - create new control
2313 * @_template: control template
2314 * @data: control private data
2315 * @long_name: control long name
2316 * @prefix: control name prefix
2318 * Create a new mixer control from a template control.
2320 * Returns 0 for success, else error.
2322 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2323 void *data, const char *long_name,
2326 struct snd_kcontrol_new template;
2327 struct snd_kcontrol *kcontrol;
2330 memcpy(&template, _template, sizeof(template));
2334 long_name = template.name;
2337 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2341 template.name = name;
2343 template.name = long_name;
2346 kcontrol = snd_ctl_new1(&template, data);
2352 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2354 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2355 const struct snd_kcontrol_new *controls, int num_controls,
2356 const char *prefix, void *data)
2360 for (i = 0; i < num_controls; i++) {
2361 const struct snd_kcontrol_new *control = &controls[i];
2362 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2363 control->name, prefix));
2365 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2366 control->name, err);
2374 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2377 struct snd_card *card = soc_card->snd_card;
2378 struct snd_kcontrol *kctl;
2380 if (unlikely(!name))
2383 list_for_each_entry(kctl, &card->controls, list)
2384 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2388 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2391 * snd_soc_add_codec_controls - add an array of controls to a codec.
2392 * Convenience function to add a list of controls. Many codecs were
2393 * duplicating this code.
2395 * @codec: codec to add controls to
2396 * @controls: array of controls to add
2397 * @num_controls: number of elements in the array
2399 * Return 0 for success, else error.
2401 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2402 const struct snd_kcontrol_new *controls, int num_controls)
2404 struct snd_card *card = codec->card->snd_card;
2406 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2407 codec->component.name_prefix, &codec->component);
2409 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2412 * snd_soc_add_platform_controls - add an array of controls to a platform.
2413 * Convenience function to add a list of controls.
2415 * @platform: platform to add controls to
2416 * @controls: array of controls to add
2417 * @num_controls: number of elements in the array
2419 * Return 0 for success, else error.
2421 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2422 const struct snd_kcontrol_new *controls, int num_controls)
2424 struct snd_card *card = platform->card->snd_card;
2426 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2427 NULL, &platform->component);
2429 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2432 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2433 * Convenience function to add a list of controls.
2435 * @soc_card: SoC card to add controls to
2436 * @controls: array of controls to add
2437 * @num_controls: number of elements in the array
2439 * Return 0 for success, else error.
2441 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2442 const struct snd_kcontrol_new *controls, int num_controls)
2444 struct snd_card *card = soc_card->snd_card;
2446 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2449 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2452 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2453 * Convienience function to add a list of controls.
2455 * @dai: DAI to add controls to
2456 * @controls: array of controls to add
2457 * @num_controls: number of elements in the array
2459 * Return 0 for success, else error.
2461 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2462 const struct snd_kcontrol_new *controls, int num_controls)
2464 struct snd_card *card = dai->card->snd_card;
2466 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2469 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2472 * snd_soc_info_enum_double - enumerated double mixer info callback
2473 * @kcontrol: mixer control
2474 * @uinfo: control element information
2476 * Callback to provide information about a double enumerated
2479 * Returns 0 for success.
2481 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2482 struct snd_ctl_elem_info *uinfo)
2484 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2486 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2487 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2488 uinfo->value.enumerated.items = e->items;
2490 if (uinfo->value.enumerated.item >= e->items)
2491 uinfo->value.enumerated.item = e->items - 1;
2492 strlcpy(uinfo->value.enumerated.name,
2493 e->texts[uinfo->value.enumerated.item],
2494 sizeof(uinfo->value.enumerated.name));
2497 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2500 * snd_soc_get_enum_double - enumerated double mixer get callback
2501 * @kcontrol: mixer control
2502 * @ucontrol: control element information
2504 * Callback to get the value of a double enumerated mixer.
2506 * Returns 0 for success.
2508 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2509 struct snd_ctl_elem_value *ucontrol)
2511 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2512 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2513 unsigned int val, item;
2514 unsigned int reg_val;
2517 ret = snd_soc_component_read(component, e->reg, ®_val);
2520 val = (reg_val >> e->shift_l) & e->mask;
2521 item = snd_soc_enum_val_to_item(e, val);
2522 ucontrol->value.enumerated.item[0] = item;
2523 if (e->shift_l != e->shift_r) {
2524 val = (reg_val >> e->shift_l) & e->mask;
2525 item = snd_soc_enum_val_to_item(e, val);
2526 ucontrol->value.enumerated.item[1] = item;
2531 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2534 * snd_soc_put_enum_double - enumerated double mixer put callback
2535 * @kcontrol: mixer control
2536 * @ucontrol: control element information
2538 * Callback to set the value of a double enumerated mixer.
2540 * Returns 0 for success.
2542 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2543 struct snd_ctl_elem_value *ucontrol)
2545 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2546 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2547 unsigned int *item = ucontrol->value.enumerated.item;
2551 if (item[0] >= e->items)
2553 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
2554 mask = e->mask << e->shift_l;
2555 if (e->shift_l != e->shift_r) {
2556 if (item[1] >= e->items)
2558 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
2559 mask |= e->mask << e->shift_r;
2562 return snd_soc_component_update_bits(component, e->reg, mask, val);
2564 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2567 * snd_soc_read_signed - Read a codec register and interprete as signed value
2568 * @component: component
2569 * @reg: Register to read
2570 * @mask: Mask to use after shifting the register value
2571 * @shift: Right shift of register value
2572 * @sign_bit: Bit that describes if a number is negative or not.
2573 * @signed_val: Pointer to where the read value should be stored
2575 * This functions reads a codec register. The register value is shifted right
2576 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2577 * the given registervalue into a signed integer if sign_bit is non-zero.
2579 * Returns 0 on sucess, otherwise an error value
2581 static int snd_soc_read_signed(struct snd_soc_component *component,
2582 unsigned int reg, unsigned int mask, unsigned int shift,
2583 unsigned int sign_bit, int *signed_val)
2588 ret = snd_soc_component_read(component, reg, &val);
2592 val = (val >> shift) & mask;
2599 /* non-negative number */
2600 if (!(val & BIT(sign_bit))) {
2608 * The register most probably does not contain a full-sized int.
2609 * Instead we have an arbitrary number of bits in a signed
2610 * representation which has to be translated into a full-sized int.
2611 * This is done by filling up all bits above the sign-bit.
2613 ret |= ~((int)(BIT(sign_bit) - 1));
2621 * snd_soc_info_volsw - single mixer info callback
2622 * @kcontrol: mixer control
2623 * @uinfo: control element information
2625 * Callback to provide information about a single mixer control, or a double
2626 * mixer control that spans 2 registers.
2628 * Returns 0 for success.
2630 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2631 struct snd_ctl_elem_info *uinfo)
2633 struct soc_mixer_control *mc =
2634 (struct soc_mixer_control *)kcontrol->private_value;
2637 if (!mc->platform_max)
2638 mc->platform_max = mc->max;
2639 platform_max = mc->platform_max;
2641 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2642 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2644 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2646 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2647 uinfo->value.integer.min = 0;
2648 uinfo->value.integer.max = platform_max - mc->min;
2651 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2654 * snd_soc_get_volsw - single mixer get callback
2655 * @kcontrol: mixer control
2656 * @ucontrol: control element information
2658 * Callback to get the value of a single mixer control, or a double mixer
2659 * control that spans 2 registers.
2661 * Returns 0 for success.
2663 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2664 struct snd_ctl_elem_value *ucontrol)
2666 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2667 struct soc_mixer_control *mc =
2668 (struct soc_mixer_control *)kcontrol->private_value;
2669 unsigned int reg = mc->reg;
2670 unsigned int reg2 = mc->rreg;
2671 unsigned int shift = mc->shift;
2672 unsigned int rshift = mc->rshift;
2675 int sign_bit = mc->sign_bit;
2676 unsigned int mask = (1 << fls(max)) - 1;
2677 unsigned int invert = mc->invert;
2682 mask = BIT(sign_bit + 1) - 1;
2684 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2688 ucontrol->value.integer.value[0] = val - min;
2690 ucontrol->value.integer.value[0] =
2691 max - ucontrol->value.integer.value[0];
2693 if (snd_soc_volsw_is_stereo(mc)) {
2695 ret = snd_soc_read_signed(component, reg, mask, rshift,
2698 ret = snd_soc_read_signed(component, reg2, mask, shift,
2703 ucontrol->value.integer.value[1] = val - min;
2705 ucontrol->value.integer.value[1] =
2706 max - ucontrol->value.integer.value[1];
2711 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2714 * snd_soc_put_volsw - single mixer put callback
2715 * @kcontrol: mixer control
2716 * @ucontrol: control element information
2718 * Callback to set the value of a single mixer control, or a double mixer
2719 * control that spans 2 registers.
2721 * Returns 0 for success.
2723 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2724 struct snd_ctl_elem_value *ucontrol)
2726 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2727 struct soc_mixer_control *mc =
2728 (struct soc_mixer_control *)kcontrol->private_value;
2729 unsigned int reg = mc->reg;
2730 unsigned int reg2 = mc->rreg;
2731 unsigned int shift = mc->shift;
2732 unsigned int rshift = mc->rshift;
2735 unsigned int sign_bit = mc->sign_bit;
2736 unsigned int mask = (1 << fls(max)) - 1;
2737 unsigned int invert = mc->invert;
2739 bool type_2r = false;
2740 unsigned int val2 = 0;
2741 unsigned int val, val_mask;
2744 mask = BIT(sign_bit + 1) - 1;
2746 val = ((ucontrol->value.integer.value[0] + min) & mask);
2749 val_mask = mask << shift;
2751 if (snd_soc_volsw_is_stereo(mc)) {
2752 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
2756 val_mask |= mask << rshift;
2757 val |= val2 << rshift;
2759 val2 = val2 << shift;
2763 err = snd_soc_component_update_bits(component, reg, val_mask, val);
2768 err = snd_soc_component_update_bits(component, reg2, val_mask,
2773 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2776 * snd_soc_get_volsw_sx - single mixer get callback
2777 * @kcontrol: mixer control
2778 * @ucontrol: control element information
2780 * Callback to get the value of a single mixer control, or a double mixer
2781 * control that spans 2 registers.
2783 * Returns 0 for success.
2785 int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2786 struct snd_ctl_elem_value *ucontrol)
2788 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2789 struct soc_mixer_control *mc =
2790 (struct soc_mixer_control *)kcontrol->private_value;
2791 unsigned int reg = mc->reg;
2792 unsigned int reg2 = mc->rreg;
2793 unsigned int shift = mc->shift;
2794 unsigned int rshift = mc->rshift;
2797 int mask = (1 << (fls(min + max) - 1)) - 1;
2801 ret = snd_soc_component_read(component, reg, &val);
2805 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2807 if (snd_soc_volsw_is_stereo(mc)) {
2808 ret = snd_soc_component_read(component, reg2, &val);
2812 val = ((val >> rshift) - min) & mask;
2813 ucontrol->value.integer.value[1] = val;
2818 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2821 * snd_soc_put_volsw_sx - double mixer set callback
2822 * @kcontrol: mixer control
2823 * @uinfo: control element information
2825 * Callback to set the value of a double mixer control that spans 2 registers.
2827 * Returns 0 for success.
2829 int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2830 struct snd_ctl_elem_value *ucontrol)
2832 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2833 struct soc_mixer_control *mc =
2834 (struct soc_mixer_control *)kcontrol->private_value;
2836 unsigned int reg = mc->reg;
2837 unsigned int reg2 = mc->rreg;
2838 unsigned int shift = mc->shift;
2839 unsigned int rshift = mc->rshift;
2842 int mask = (1 << (fls(min + max) - 1)) - 1;
2844 unsigned int val, val_mask, val2 = 0;
2846 val_mask = mask << shift;
2847 val = (ucontrol->value.integer.value[0] + min) & mask;
2850 err = snd_soc_component_update_bits(component, reg, val_mask, val);
2854 if (snd_soc_volsw_is_stereo(mc)) {
2855 val_mask = mask << rshift;
2856 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2857 val2 = val2 << rshift;
2859 err = snd_soc_component_update_bits(component, reg2, val_mask,
2864 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2867 * snd_soc_info_volsw_s8 - signed mixer info callback
2868 * @kcontrol: mixer control
2869 * @uinfo: control element information
2871 * Callback to provide information about a signed mixer control.
2873 * Returns 0 for success.
2875 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2876 struct snd_ctl_elem_info *uinfo)
2878 struct soc_mixer_control *mc =
2879 (struct soc_mixer_control *)kcontrol->private_value;
2883 if (!mc->platform_max)
2884 mc->platform_max = mc->max;
2885 platform_max = mc->platform_max;
2887 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2889 uinfo->value.integer.min = 0;
2890 uinfo->value.integer.max = platform_max - min;
2893 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2896 * snd_soc_get_volsw_s8 - signed mixer get callback
2897 * @kcontrol: mixer control
2898 * @ucontrol: control element information
2900 * Callback to get the value of a signed mixer control.
2902 * Returns 0 for success.
2904 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2905 struct snd_ctl_elem_value *ucontrol)
2907 struct soc_mixer_control *mc =
2908 (struct soc_mixer_control *)kcontrol->private_value;
2909 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2910 unsigned int reg = mc->reg;
2915 ret = snd_soc_component_read(component, reg, &val);
2919 ucontrol->value.integer.value[0] =
2920 ((signed char)(val & 0xff))-min;
2921 ucontrol->value.integer.value[1] =
2922 ((signed char)((val >> 8) & 0xff))-min;
2925 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2928 * snd_soc_put_volsw_sgn - signed mixer put callback
2929 * @kcontrol: mixer control
2930 * @ucontrol: control element information
2932 * Callback to set the value of a signed mixer control.
2934 * Returns 0 for success.
2936 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2937 struct snd_ctl_elem_value *ucontrol)
2939 struct soc_mixer_control *mc =
2940 (struct soc_mixer_control *)kcontrol->private_value;
2941 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2942 unsigned int reg = mc->reg;
2946 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2947 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2949 return snd_soc_component_update_bits(component, reg, 0xffff, val);
2951 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2954 * snd_soc_info_volsw_range - single mixer info callback with range.
2955 * @kcontrol: mixer control
2956 * @uinfo: control element information
2958 * Callback to provide information, within a range, about a single
2961 * returns 0 for success.
2963 int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2964 struct snd_ctl_elem_info *uinfo)
2966 struct soc_mixer_control *mc =
2967 (struct soc_mixer_control *)kcontrol->private_value;
2971 if (!mc->platform_max)
2972 mc->platform_max = mc->max;
2973 platform_max = mc->platform_max;
2975 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2976 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2977 uinfo->value.integer.min = 0;
2978 uinfo->value.integer.max = platform_max - min;
2982 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2985 * snd_soc_put_volsw_range - single mixer put value callback with range.
2986 * @kcontrol: mixer control
2987 * @ucontrol: control element information
2989 * Callback to set the value, within a range, for a single mixer control.
2991 * Returns 0 for success.
2993 int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2994 struct snd_ctl_elem_value *ucontrol)
2996 struct soc_mixer_control *mc =
2997 (struct soc_mixer_control *)kcontrol->private_value;
2998 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2999 unsigned int reg = mc->reg;
3000 unsigned int rreg = mc->rreg;
3001 unsigned int shift = mc->shift;
3004 unsigned int mask = (1 << fls(max)) - 1;
3005 unsigned int invert = mc->invert;
3006 unsigned int val, val_mask;
3009 val = ((ucontrol->value.integer.value[0] + min) & mask);
3012 val_mask = mask << shift;
3015 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
3019 if (snd_soc_volsw_is_stereo(mc)) {
3020 val = ((ucontrol->value.integer.value[1] + min) & mask);
3023 val_mask = mask << shift;
3026 ret = snd_soc_component_update_bits(component, rreg, val_mask,
3032 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
3035 * snd_soc_get_volsw_range - single mixer get callback with range
3036 * @kcontrol: mixer control
3037 * @ucontrol: control element information
3039 * Callback to get the value, within a range, of a single mixer control.
3041 * Returns 0 for success.
3043 int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
3044 struct snd_ctl_elem_value *ucontrol)
3046 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3047 struct soc_mixer_control *mc =
3048 (struct soc_mixer_control *)kcontrol->private_value;
3049 unsigned int reg = mc->reg;
3050 unsigned int rreg = mc->rreg;
3051 unsigned int shift = mc->shift;
3054 unsigned int mask = (1 << fls(max)) - 1;
3055 unsigned int invert = mc->invert;
3059 ret = snd_soc_component_read(component, reg, &val);
3063 ucontrol->value.integer.value[0] = (val >> shift) & mask;
3065 ucontrol->value.integer.value[0] =
3066 max - ucontrol->value.integer.value[0];
3067 ucontrol->value.integer.value[0] =
3068 ucontrol->value.integer.value[0] - min;
3070 if (snd_soc_volsw_is_stereo(mc)) {
3071 ret = snd_soc_component_read(component, rreg, &val);
3075 ucontrol->value.integer.value[1] = (val >> shift) & mask;
3077 ucontrol->value.integer.value[1] =
3078 max - ucontrol->value.integer.value[1];
3079 ucontrol->value.integer.value[1] =
3080 ucontrol->value.integer.value[1] - min;
3085 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3088 * snd_soc_limit_volume - Set new limit to an existing volume control.
3090 * @codec: where to look for the control
3091 * @name: Name of the control
3092 * @max: new maximum limit
3094 * Return 0 for success, else error.
3096 int snd_soc_limit_volume(struct snd_soc_codec *codec,
3097 const char *name, int max)
3099 struct snd_card *card = codec->card->snd_card;
3100 struct snd_kcontrol *kctl;
3101 struct soc_mixer_control *mc;
3105 /* Sanity check for name and max */
3106 if (unlikely(!name || max <= 0))
3109 list_for_each_entry(kctl, &card->controls, list) {
3110 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3116 mc = (struct soc_mixer_control *)kctl->private_value;
3117 if (max <= mc->max) {
3118 mc->platform_max = max;
3124 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3126 int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3127 struct snd_ctl_elem_info *uinfo)
3129 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3130 struct soc_bytes *params = (void *)kcontrol->private_value;
3132 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3133 uinfo->count = params->num_regs * component->val_bytes;
3137 EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3139 int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3140 struct snd_ctl_elem_value *ucontrol)
3142 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3143 struct soc_bytes *params = (void *)kcontrol->private_value;
3146 if (component->regmap)
3147 ret = regmap_raw_read(component->regmap, params->base,
3148 ucontrol->value.bytes.data,
3149 params->num_regs * component->val_bytes);
3153 /* Hide any masked bytes to ensure consistent data reporting */
3154 if (ret == 0 && params->mask) {
3155 switch (component->val_bytes) {
3157 ucontrol->value.bytes.data[0] &= ~params->mask;
3160 ((u16 *)(&ucontrol->value.bytes.data))[0]
3161 &= cpu_to_be16(~params->mask);
3164 ((u32 *)(&ucontrol->value.bytes.data))[0]
3165 &= cpu_to_be32(~params->mask);
3174 EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3176 int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3177 struct snd_ctl_elem_value *ucontrol)
3179 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3180 struct soc_bytes *params = (void *)kcontrol->private_value;
3182 unsigned int val, mask;
3185 if (!component->regmap)
3188 len = params->num_regs * component->val_bytes;
3190 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3195 * If we've got a mask then we need to preserve the register
3196 * bits. We shouldn't modify the incoming data so take a
3200 ret = regmap_read(component->regmap, params->base, &val);
3204 val &= params->mask;
3206 switch (component->val_bytes) {
3208 ((u8 *)data)[0] &= ~params->mask;
3209 ((u8 *)data)[0] |= val;
3212 mask = ~params->mask;
3213 ret = regmap_parse_val(component->regmap,
3218 ((u16 *)data)[0] &= mask;
3220 ret = regmap_parse_val(component->regmap,
3225 ((u16 *)data)[0] |= val;
3228 mask = ~params->mask;
3229 ret = regmap_parse_val(component->regmap,
3234 ((u32 *)data)[0] &= mask;
3236 ret = regmap_parse_val(component->regmap,
3241 ((u32 *)data)[0] |= val;
3249 ret = regmap_raw_write(component->regmap, params->base,
3257 EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3259 int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3260 struct snd_ctl_elem_info *ucontrol)
3262 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3264 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3265 ucontrol->count = params->max;
3269 EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3272 * snd_soc_info_xr_sx - signed multi register info callback
3273 * @kcontrol: mreg control
3274 * @uinfo: control element information
3276 * Callback to provide information of a control that can
3277 * span multiple codec registers which together
3278 * forms a single signed value in a MSB/LSB manner.
3280 * Returns 0 for success.
3282 int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3283 struct snd_ctl_elem_info *uinfo)
3285 struct soc_mreg_control *mc =
3286 (struct soc_mreg_control *)kcontrol->private_value;
3287 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3289 uinfo->value.integer.min = mc->min;
3290 uinfo->value.integer.max = mc->max;
3294 EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3297 * snd_soc_get_xr_sx - signed multi register get callback
3298 * @kcontrol: mreg control
3299 * @ucontrol: control element information
3301 * Callback to get the value of a control that can span
3302 * multiple codec registers which together forms a single
3303 * signed value in a MSB/LSB manner. The control supports
3304 * specifying total no of bits used to allow for bitfields
3305 * across the multiple codec registers.
3307 * Returns 0 for success.
3309 int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3310 struct snd_ctl_elem_value *ucontrol)
3312 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3313 struct soc_mreg_control *mc =
3314 (struct soc_mreg_control *)kcontrol->private_value;
3315 unsigned int regbase = mc->regbase;
3316 unsigned int regcount = mc->regcount;
3317 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
3318 unsigned int regwmask = (1<<regwshift)-1;
3319 unsigned int invert = mc->invert;
3320 unsigned long mask = (1UL<<mc->nbits)-1;
3324 unsigned int regval;
3328 for (i = 0; i < regcount; i++) {
3329 ret = snd_soc_component_read(component, regbase+i, ®val);
3332 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
3335 if (min < 0 && val > max)
3339 ucontrol->value.integer.value[0] = val;
3343 EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3346 * snd_soc_put_xr_sx - signed multi register get callback
3347 * @kcontrol: mreg control
3348 * @ucontrol: control element information
3350 * Callback to set the value of a control that can span
3351 * multiple codec registers which together forms a single
3352 * signed value in a MSB/LSB manner. The control supports
3353 * specifying total no of bits used to allow for bitfields
3354 * across the multiple codec registers.
3356 * Returns 0 for success.
3358 int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3359 struct snd_ctl_elem_value *ucontrol)
3361 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3362 struct soc_mreg_control *mc =
3363 (struct soc_mreg_control *)kcontrol->private_value;
3364 unsigned int regbase = mc->regbase;
3365 unsigned int regcount = mc->regcount;
3366 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
3367 unsigned int regwmask = (1<<regwshift)-1;
3368 unsigned int invert = mc->invert;
3369 unsigned long mask = (1UL<<mc->nbits)-1;
3371 long val = ucontrol->value.integer.value[0];
3372 unsigned int i, regval, regmask;
3378 for (i = 0; i < regcount; i++) {
3379 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3380 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3381 err = snd_soc_component_update_bits(component, regbase+i,
3389 EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3392 * snd_soc_get_strobe - strobe get callback
3393 * @kcontrol: mixer control
3394 * @ucontrol: control element information
3396 * Callback get the value of a strobe mixer control.
3398 * Returns 0 for success.
3400 int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3401 struct snd_ctl_elem_value *ucontrol)
3403 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3404 struct soc_mixer_control *mc =
3405 (struct soc_mixer_control *)kcontrol->private_value;
3406 unsigned int reg = mc->reg;
3407 unsigned int shift = mc->shift;
3408 unsigned int mask = 1 << shift;
3409 unsigned int invert = mc->invert != 0;
3413 ret = snd_soc_component_read(component, reg, &val);
3419 if (shift != 0 && val != 0)
3421 ucontrol->value.enumerated.item[0] = val ^ invert;
3425 EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3428 * snd_soc_put_strobe - strobe put callback
3429 * @kcontrol: mixer control
3430 * @ucontrol: control element information
3432 * Callback strobe a register bit to high then low (or the inverse)
3433 * in one pass of a single mixer enum control.
3435 * Returns 1 for success.
3437 int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3438 struct snd_ctl_elem_value *ucontrol)
3440 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3441 struct soc_mixer_control *mc =
3442 (struct soc_mixer_control *)kcontrol->private_value;
3443 unsigned int reg = mc->reg;
3444 unsigned int shift = mc->shift;
3445 unsigned int mask = 1 << shift;
3446 unsigned int invert = mc->invert != 0;
3447 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3448 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3449 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3452 err = snd_soc_component_update_bits(component, reg, mask, val1);
3456 return snd_soc_component_update_bits(component, reg, mask, val2);
3458 EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3461 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3463 * @clk_id: DAI specific clock ID
3464 * @freq: new clock frequency in Hz
3465 * @dir: new clock direction - input/output.
3467 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3469 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3470 unsigned int freq, int dir)
3472 if (dai->driver && dai->driver->ops->set_sysclk)
3473 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3474 else if (dai->codec && dai->codec->driver->set_sysclk)
3475 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3480 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3483 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3485 * @clk_id: DAI specific clock ID
3486 * @source: Source for the clock
3487 * @freq: new clock frequency in Hz
3488 * @dir: new clock direction - input/output.
3490 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3492 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3493 int source, unsigned int freq, int dir)
3495 if (codec->driver->set_sysclk)
3496 return codec->driver->set_sysclk(codec, clk_id, source,
3501 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3504 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3506 * @div_id: DAI specific clock divider ID
3507 * @div: new clock divisor.
3509 * Configures the clock dividers. This is used to derive the best DAI bit and
3510 * frame clocks from the system or master clock. It's best to set the DAI bit
3511 * and frame clocks as low as possible to save system power.
3513 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3514 int div_id, int div)
3516 if (dai->driver && dai->driver->ops->set_clkdiv)
3517 return dai->driver->ops->set_clkdiv(dai, div_id, div);
3521 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3524 * snd_soc_dai_set_pll - configure DAI PLL.
3526 * @pll_id: DAI specific PLL ID
3527 * @source: DAI specific source for the PLL
3528 * @freq_in: PLL input clock frequency in Hz
3529 * @freq_out: requested PLL output clock frequency in Hz
3531 * Configures and enables PLL to generate output clock based on input clock.
3533 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3534 unsigned int freq_in, unsigned int freq_out)
3536 if (dai->driver && dai->driver->ops->set_pll)
3537 return dai->driver->ops->set_pll(dai, pll_id, source,
3539 else if (dai->codec && dai->codec->driver->set_pll)
3540 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3545 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3548 * snd_soc_codec_set_pll - configure codec PLL.
3550 * @pll_id: DAI specific PLL ID
3551 * @source: DAI specific source for the PLL
3552 * @freq_in: PLL input clock frequency in Hz
3553 * @freq_out: requested PLL output clock frequency in Hz
3555 * Configures and enables PLL to generate output clock based on input clock.
3557 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3558 unsigned int freq_in, unsigned int freq_out)
3560 if (codec->driver->set_pll)
3561 return codec->driver->set_pll(codec, pll_id, source,
3566 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3569 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3571 * @ratio Ratio of BCLK to Sample rate.
3573 * Configures the DAI for a preset BCLK to sample rate ratio.
3575 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3577 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3578 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3582 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3585 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3587 * @fmt: SND_SOC_DAIFMT_ format value.
3589 * Configures the DAI hardware format and clocking.
3591 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3593 if (dai->driver == NULL)
3595 if (dai->driver->ops->set_fmt == NULL)
3597 return dai->driver->ops->set_fmt(dai, fmt);
3599 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3602 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
3603 * @slots: Number of slots in use.
3604 * @tx_mask: bitmask representing active TX slots.
3605 * @rx_mask: bitmask representing active RX slots.
3607 * Generates the TDM tx and rx slot default masks for DAI.
3609 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
3610 unsigned int *tx_mask,
3611 unsigned int *rx_mask)
3613 if (*tx_mask || *rx_mask)
3619 *tx_mask = (1 << slots) - 1;
3620 *rx_mask = (1 << slots) - 1;
3626 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3628 * @tx_mask: bitmask representing active TX slots.
3629 * @rx_mask: bitmask representing active RX slots.
3630 * @slots: Number of slots in use.
3631 * @slot_width: Width in bits for each slot.
3633 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3636 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3637 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
3639 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3640 dai->driver->ops->xlate_tdm_slot_mask(slots,
3641 &tx_mask, &rx_mask);
3643 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
3645 if (dai->driver && dai->driver->ops->set_tdm_slot)
3646 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3651 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3654 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3656 * @tx_num: how many TX channels
3657 * @tx_slot: pointer to an array which imply the TX slot number channel
3659 * @rx_num: how many RX channels
3660 * @rx_slot: pointer to an array which imply the RX slot number channel
3663 * configure the relationship between channel number and TDM slot number.
3665 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3666 unsigned int tx_num, unsigned int *tx_slot,
3667 unsigned int rx_num, unsigned int *rx_slot)
3669 if (dai->driver && dai->driver->ops->set_channel_map)
3670 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3675 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3678 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3680 * @tristate: tristate enable
3682 * Tristates the DAI so that others can use it.
3684 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3686 if (dai->driver && dai->driver->ops->set_tristate)
3687 return dai->driver->ops->set_tristate(dai, tristate);
3691 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3694 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3696 * @mute: mute enable
3697 * @direction: stream to mute
3699 * Mutes the DAI DAC.
3701 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3707 if (dai->driver->ops->mute_stream)
3708 return dai->driver->ops->mute_stream(dai, mute, direction);
3709 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3710 dai->driver->ops->digital_mute)
3711 return dai->driver->ops->digital_mute(dai, mute);
3715 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3718 * snd_soc_register_card - Register a card with the ASoC core
3720 * @card: Card to register
3723 int snd_soc_register_card(struct snd_soc_card *card)
3727 if (!card->name || !card->dev)
3730 for (i = 0; i < card->num_links; i++) {
3731 struct snd_soc_dai_link *link = &card->dai_link[i];
3734 * Codec must be specified by 1 of name or OF node,
3735 * not both or neither.
3737 if (!!link->codec_name == !!link->codec_of_node) {
3739 "ASoC: Neither/both codec name/of_node are set for %s\n",
3743 /* Codec DAI name must be specified */
3744 if (!link->codec_dai_name) {
3746 "ASoC: codec_dai_name not set for %s\n",
3752 * Platform may be specified by either name or OF node, but
3753 * can be left unspecified, and a dummy platform will be used.
3755 if (link->platform_name && link->platform_of_node) {
3757 "ASoC: Both platform name/of_node are set for %s\n",
3763 * CPU device may be specified by either name or OF node, but
3764 * can be left unspecified, and will be matched based on DAI
3767 if (link->cpu_name && link->cpu_of_node) {
3769 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3774 * At least one of CPU DAI name or CPU device name/node must be
3777 if (!link->cpu_dai_name &&
3778 !(link->cpu_name || link->cpu_of_node)) {
3780 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3786 dev_set_drvdata(card->dev, card);
3788 snd_soc_initialize_card_lists(card);
3790 soc_init_card_debugfs(card);
3792 card->rtd = devm_kzalloc(card->dev,
3793 sizeof(struct snd_soc_pcm_runtime) *
3794 (card->num_links + card->num_aux_devs),
3796 if (card->rtd == NULL)
3799 card->rtd_aux = &card->rtd[card->num_links];
3801 for (i = 0; i < card->num_links; i++)
3802 card->rtd[i].dai_link = &card->dai_link[i];
3804 INIT_LIST_HEAD(&card->dapm_dirty);
3805 card->instantiated = 0;
3806 mutex_init(&card->mutex);
3807 mutex_init(&card->dapm_mutex);
3809 ret = snd_soc_instantiate_card(card);
3811 soc_cleanup_card_debugfs(card);
3813 /* deactivate pins to sleep state */
3814 for (i = 0; i < card->num_rtd; i++) {
3815 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
3816 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
3817 if (!codec_dai->active)
3818 pinctrl_pm_select_sleep_state(codec_dai->dev);
3819 if (!cpu_dai->active)
3820 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3825 EXPORT_SYMBOL_GPL(snd_soc_register_card);
3828 * snd_soc_unregister_card - Unregister a card with the ASoC core
3830 * @card: Card to unregister
3833 int snd_soc_unregister_card(struct snd_soc_card *card)
3835 if (card->instantiated)
3836 soc_cleanup_card_resources(card);
3837 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
3841 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3844 * Simplify DAI link configuration by removing ".-1" from device names
3845 * and sanitizing names.
3847 static char *fmt_single_name(struct device *dev, int *id)
3849 char *found, name[NAME_SIZE];
3852 if (dev_name(dev) == NULL)
3855 strlcpy(name, dev_name(dev), NAME_SIZE);
3857 /* are we a "%s.%d" name (platform and SPI components) */
3858 found = strstr(name, dev->driver->name);
3861 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3863 /* discard ID from name if ID == -1 */
3865 found[strlen(dev->driver->name)] = '\0';
3869 /* I2C component devices are named "bus-addr" */
3870 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3871 char tmp[NAME_SIZE];
3873 /* create unique ID number from I2C addr and bus */
3874 *id = ((id1 & 0xffff) << 16) + id2;
3876 /* sanitize component name for DAI link creation */
3877 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3878 strlcpy(name, tmp, NAME_SIZE);
3883 return kstrdup(name, GFP_KERNEL);
3887 * Simplify DAI link naming for single devices with multiple DAIs by removing
3888 * any ".-1" and using the DAI name (instead of device name).
3890 static inline char *fmt_multiple_name(struct device *dev,
3891 struct snd_soc_dai_driver *dai_drv)
3893 if (dai_drv->name == NULL) {
3895 "ASoC: error - multiple DAI %s registered with no name\n",
3900 return kstrdup(dai_drv->name, GFP_KERNEL);
3904 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
3906 * @component: The component for which the DAIs should be unregistered
3908 static void snd_soc_unregister_dais(struct snd_soc_component *component)
3910 struct snd_soc_dai *dai, *_dai;
3912 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
3913 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3915 list_del(&dai->list);
3922 * snd_soc_register_dais - Register a DAI with the ASoC core
3924 * @component: The component the DAIs are registered for
3925 * @dai_drv: DAI driver to use for the DAIs
3926 * @count: Number of DAIs
3927 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3930 static int snd_soc_register_dais(struct snd_soc_component *component,
3931 struct snd_soc_dai_driver *dai_drv, size_t count,
3932 bool legacy_dai_naming)
3934 struct device *dev = component->dev;
3935 struct snd_soc_dai *dai;
3939 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
3941 component->dai_drv = dai_drv;
3942 component->num_dai = count;
3944 for (i = 0; i < count; i++) {
3946 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3953 * Back in the old days when we still had component-less DAIs,
3954 * instead of having a static name, component-less DAIs would
3955 * inherit the name of the parent device so it is possible to
3956 * register multiple instances of the DAI. We still need to keep
3957 * the same naming style even though those DAIs are not
3958 * component-less anymore.
3960 if (count == 1 && legacy_dai_naming) {
3961 dai->name = fmt_single_name(dev, &dai->id);
3963 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3965 dai->id = dai_drv[i].id;
3969 if (dai->name == NULL) {
3975 dai->component = component;
3977 dai->driver = &dai_drv[i];
3978 dai->dapm.dev = dev;
3979 if (!dai->driver->ops)
3980 dai->driver->ops = &null_dai_ops;
3983 dai->dapm.idle_bias_off = 1;
3985 list_add(&dai->list, &component->dai_list);
3987 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3993 snd_soc_unregister_dais(component);
3998 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3999 enum snd_soc_dapm_type type, int subseq)
4001 struct snd_soc_component *component = dapm->component;
4003 component->driver->seq_notifier(component, type, subseq);
4006 static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
4009 struct snd_soc_component *component = dapm->component;
4011 return component->driver->stream_event(component, event);
4014 static int snd_soc_component_initialize(struct snd_soc_component *component,
4015 const struct snd_soc_component_driver *driver, struct device *dev)
4017 struct snd_soc_dapm_context *dapm;
4019 component->name = fmt_single_name(dev, &component->id);
4020 if (!component->name) {
4021 dev_err(dev, "ASoC: Failed to allocate name\n");
4025 component->dev = dev;
4026 component->driver = driver;
4028 if (!component->dapm_ptr)
4029 component->dapm_ptr = &component->dapm;
4031 dapm = component->dapm_ptr;
4033 dapm->component = component;
4034 dapm->bias_level = SND_SOC_BIAS_OFF;
4035 if (driver->seq_notifier)
4036 dapm->seq_notifier = snd_soc_component_seq_notifier;
4037 if (driver->stream_event)
4038 dapm->stream_event = snd_soc_component_stream_event;
4040 INIT_LIST_HEAD(&component->dai_list);
4041 mutex_init(&component->io_mutex);
4046 static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
4048 list_add(&component->list, &component_list);
4051 static void snd_soc_component_add(struct snd_soc_component *component)
4053 mutex_lock(&client_mutex);
4054 snd_soc_component_add_unlocked(component);
4055 mutex_unlock(&client_mutex);
4058 static void snd_soc_component_cleanup(struct snd_soc_component *component)
4060 snd_soc_unregister_dais(component);
4061 kfree(component->name);
4064 static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
4066 list_del(&component->list);
4069 static void snd_soc_component_del(struct snd_soc_component *component)
4071 mutex_lock(&client_mutex);
4072 snd_soc_component_del_unlocked(component);
4073 mutex_unlock(&client_mutex);
4076 int snd_soc_register_component(struct device *dev,
4077 const struct snd_soc_component_driver *cmpnt_drv,
4078 struct snd_soc_dai_driver *dai_drv,
4081 struct snd_soc_component *cmpnt;
4084 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
4086 dev_err(dev, "ASoC: Failed to allocate memory\n");
4090 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
4094 cmpnt->ignore_pmdown_time = true;
4095 cmpnt->registered_as_component = true;
4097 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
4099 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4103 snd_soc_component_add(cmpnt);
4108 snd_soc_component_cleanup(cmpnt);
4113 EXPORT_SYMBOL_GPL(snd_soc_register_component);
4116 * snd_soc_unregister_component - Unregister a component from the ASoC core
4119 void snd_soc_unregister_component(struct device *dev)
4121 struct snd_soc_component *cmpnt;
4123 list_for_each_entry(cmpnt, &component_list, list) {
4124 if (dev == cmpnt->dev && cmpnt->registered_as_component)
4130 snd_soc_component_del(cmpnt);
4131 snd_soc_component_cleanup(cmpnt);
4134 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4136 static int snd_soc_platform_drv_write(struct snd_soc_component *component,
4137 unsigned int reg, unsigned int val)
4139 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4141 return platform->driver->write(platform, reg, val);
4144 static int snd_soc_platform_drv_read(struct snd_soc_component *component,
4145 unsigned int reg, unsigned int *val)
4147 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4149 *val = platform->driver->read(platform, reg);
4155 * snd_soc_add_platform - Add a platform to the ASoC core
4156 * @dev: The parent device for the platform
4157 * @platform: The platform to add
4158 * @platform_driver: The driver for the platform
4160 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
4161 const struct snd_soc_platform_driver *platform_drv)
4165 ret = snd_soc_component_initialize(&platform->component,
4166 &platform_drv->component_driver, dev);
4170 platform->dev = dev;
4171 platform->driver = platform_drv;
4172 platform->component.dapm.platform = platform;
4173 if (platform_drv->write)
4174 platform->component.write = snd_soc_platform_drv_write;
4175 if (platform_drv->read)
4176 platform->component.read = snd_soc_platform_drv_read;
4178 mutex_lock(&client_mutex);
4179 snd_soc_component_add_unlocked(&platform->component);
4180 list_add(&platform->list, &platform_list);
4181 mutex_unlock(&client_mutex);
4183 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
4184 platform->component.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)
4222 mutex_lock(&client_mutex);
4223 list_del(&platform->list);
4224 snd_soc_component_del_unlocked(&platform->component);
4225 mutex_unlock(&client_mutex);
4227 snd_soc_component_cleanup(&platform->component);
4229 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
4230 platform->component.name);
4232 EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4234 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4236 struct snd_soc_platform *platform;
4238 list_for_each_entry(platform, &platform_list, list) {
4239 if (dev == platform->dev)
4245 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4248 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4250 * @platform: platform to unregister
4252 void snd_soc_unregister_platform(struct device *dev)
4254 struct snd_soc_platform *platform;
4256 platform = snd_soc_lookup_platform(dev);
4260 snd_soc_remove_platform(platform);
4263 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4265 static u64 codec_format_map[] = {
4266 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4267 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4268 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4269 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4270 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4271 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4272 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4273 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4274 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4275 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4276 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4277 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4278 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4279 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4280 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4281 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4284 /* Fix up the DAI formats for endianness: codecs don't actually see
4285 * the endianness of the data but we're using the CPU format
4286 * definitions which do need to include endianness so we ensure that
4287 * codec DAIs always have both big and little endian variants set.
4289 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4293 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4294 if (stream->formats & codec_format_map[i])
4295 stream->formats |= codec_format_map[i];
4298 static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4299 unsigned int reg, unsigned int val)
4301 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4303 return codec->driver->write(codec, reg, val);
4306 static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4307 unsigned int reg, unsigned int *val)
4309 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4311 *val = codec->driver->read(codec, reg);
4316 static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
4317 enum snd_soc_bias_level level)
4319 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
4321 return codec->driver->set_bias_level(codec, level);
4325 * snd_soc_register_codec - Register a codec with the ASoC core
4327 * @codec: codec to register
4329 int snd_soc_register_codec(struct device *dev,
4330 const struct snd_soc_codec_driver *codec_drv,
4331 struct snd_soc_dai_driver *dai_drv,
4334 struct snd_soc_codec *codec;
4335 struct snd_soc_dai *dai;
4336 struct regmap *regmap;
4339 dev_dbg(dev, "codec register %s\n", dev_name(dev));
4341 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4345 codec->component.dapm_ptr = &codec->dapm;
4347 ret = snd_soc_component_initialize(&codec->component,
4348 &codec_drv->component_driver, dev);
4352 if (codec_drv->write)
4353 codec->component.write = snd_soc_codec_drv_write;
4354 if (codec_drv->read)
4355 codec->component.read = snd_soc_codec_drv_read;
4356 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
4357 codec->dapm.codec = codec;
4358 if (codec_drv->seq_notifier)
4359 codec->dapm.seq_notifier = codec_drv->seq_notifier;
4360 if (codec_drv->set_bias_level)
4361 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
4363 codec->driver = codec_drv;
4364 codec->component.val_bytes = codec_drv->reg_word_size;
4365 mutex_init(&codec->mutex);
4367 if (!codec->component.write) {
4368 if (codec_drv->get_regmap)
4369 regmap = codec_drv->get_regmap(dev);
4371 regmap = dev_get_regmap(dev, NULL);
4374 ret = snd_soc_component_init_io(&codec->component,
4378 "Failed to set cache I/O:%d\n",
4385 for (i = 0; i < num_dai; i++) {
4386 fixup_codec_formats(&dai_drv[i].playback);
4387 fixup_codec_formats(&dai_drv[i].capture);
4390 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4392 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4396 list_for_each_entry(dai, &codec->component.dai_list, list)
4399 mutex_lock(&client_mutex);
4400 snd_soc_component_add_unlocked(&codec->component);
4401 list_add(&codec->list, &codec_list);
4402 mutex_unlock(&client_mutex);
4404 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4405 codec->component.name);
4409 snd_soc_component_cleanup(&codec->component);
4414 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4417 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4419 * @codec: codec to unregister
4421 void snd_soc_unregister_codec(struct device *dev)
4423 struct snd_soc_codec *codec;
4425 list_for_each_entry(codec, &codec_list, list) {
4426 if (dev == codec->dev)
4433 mutex_lock(&client_mutex);
4434 list_del(&codec->list);
4435 snd_soc_component_del_unlocked(&codec->component);
4436 mutex_unlock(&client_mutex);
4438 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4439 codec->component.name);
4441 snd_soc_component_cleanup(&codec->component);
4442 snd_soc_cache_exit(codec);
4445 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4447 /* Retrieve a card's name from device tree */
4448 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4449 const char *propname)
4451 struct device_node *np = card->dev->of_node;
4454 ret = of_property_read_string_index(np, propname, 0, &card->name);
4456 * EINVAL means the property does not exist. This is fine providing
4457 * card->name was previously set, which is checked later in
4458 * snd_soc_register_card.
4460 if (ret < 0 && ret != -EINVAL) {
4462 "ASoC: Property '%s' could not be read: %d\n",
4469 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4471 static const struct snd_soc_dapm_widget simple_widgets[] = {
4472 SND_SOC_DAPM_MIC("Microphone", NULL),
4473 SND_SOC_DAPM_LINE("Line", NULL),
4474 SND_SOC_DAPM_HP("Headphone", NULL),
4475 SND_SOC_DAPM_SPK("Speaker", NULL),
4478 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4479 const char *propname)
4481 struct device_node *np = card->dev->of_node;
4482 struct snd_soc_dapm_widget *widgets;
4483 const char *template, *wname;
4484 int i, j, num_widgets, ret;
4486 num_widgets = of_property_count_strings(np, propname);
4487 if (num_widgets < 0) {
4489 "ASoC: Property '%s' does not exist\n", propname);
4492 if (num_widgets & 1) {
4494 "ASoC: Property '%s' length is not even\n", propname);
4500 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4505 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4509 "ASoC: Could not allocate memory for widgets\n");
4513 for (i = 0; i < num_widgets; i++) {
4514 ret = of_property_read_string_index(np, propname,
4518 "ASoC: Property '%s' index %d read error:%d\n",
4519 propname, 2 * i, ret);
4523 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4524 if (!strncmp(template, simple_widgets[j].name,
4525 strlen(simple_widgets[j].name))) {
4526 widgets[i] = simple_widgets[j];
4531 if (j >= ARRAY_SIZE(simple_widgets)) {
4533 "ASoC: DAPM widget '%s' is not supported\n",
4538 ret = of_property_read_string_index(np, propname,
4543 "ASoC: Property '%s' index %d read error:%d\n",
4544 propname, (2 * i) + 1, ret);
4548 widgets[i].name = wname;
4551 card->dapm_widgets = widgets;
4552 card->num_dapm_widgets = num_widgets;
4556 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4558 int snd_soc_of_parse_tdm_slot(struct device_node *np,
4559 unsigned int *slots,
4560 unsigned int *slot_width)
4565 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4566 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4574 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4575 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4585 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4587 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4588 const char *propname)
4590 struct device_node *np = card->dev->of_node;
4592 struct snd_soc_dapm_route *routes;
4595 num_routes = of_property_count_strings(np, propname);
4596 if (num_routes < 0 || num_routes & 1) {
4598 "ASoC: Property '%s' does not exist or its length is not even\n",
4604 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4609 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4613 "ASoC: Could not allocate DAPM route table\n");
4617 for (i = 0; i < num_routes; i++) {
4618 ret = of_property_read_string_index(np, propname,
4619 2 * i, &routes[i].sink);
4622 "ASoC: Property '%s' index %d could not be read: %d\n",
4623 propname, 2 * i, ret);
4626 ret = of_property_read_string_index(np, propname,
4627 (2 * i) + 1, &routes[i].source);
4630 "ASoC: Property '%s' index %d could not be read: %d\n",
4631 propname, (2 * i) + 1, ret);
4636 card->num_dapm_routes = num_routes;
4637 card->dapm_routes = routes;
4641 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4643 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4645 struct device_node **bitclkmaster,
4646 struct device_node **framemaster)
4650 unsigned int format = 0;
4656 } of_fmt_table[] = {
4657 { "i2s", SND_SOC_DAIFMT_I2S },
4658 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4659 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4660 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4661 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4662 { "ac97", SND_SOC_DAIFMT_AC97 },
4663 { "pdm", SND_SOC_DAIFMT_PDM},
4664 { "msb", SND_SOC_DAIFMT_MSB },
4665 { "lsb", SND_SOC_DAIFMT_LSB },
4672 * check "[prefix]format = xxx"
4673 * SND_SOC_DAIFMT_FORMAT_MASK area
4675 snprintf(prop, sizeof(prop), "%sformat", prefix);
4676 ret = of_property_read_string(np, prop, &str);
4678 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4679 if (strcmp(str, of_fmt_table[i].name) == 0) {
4680 format |= of_fmt_table[i].val;
4687 * check "[prefix]continuous-clock"
4688 * SND_SOC_DAIFMT_CLOCK_MASK area
4690 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4691 if (of_get_property(np, prop, NULL))
4692 format |= SND_SOC_DAIFMT_CONT;
4694 format |= SND_SOC_DAIFMT_GATED;
4697 * check "[prefix]bitclock-inversion"
4698 * check "[prefix]frame-inversion"
4699 * SND_SOC_DAIFMT_INV_MASK area
4701 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4702 bit = !!of_get_property(np, prop, NULL);
4704 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4705 frame = !!of_get_property(np, prop, NULL);
4707 switch ((bit << 4) + frame) {
4709 format |= SND_SOC_DAIFMT_IB_IF;
4712 format |= SND_SOC_DAIFMT_IB_NF;
4715 format |= SND_SOC_DAIFMT_NB_IF;
4718 /* SND_SOC_DAIFMT_NB_NF is default */
4723 * check "[prefix]bitclock-master"
4724 * check "[prefix]frame-master"
4725 * SND_SOC_DAIFMT_MASTER_MASK area
4727 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4728 bit = !!of_get_property(np, prop, NULL);
4729 if (bit && bitclkmaster)
4730 *bitclkmaster = of_parse_phandle(np, prop, 0);
4732 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4733 frame = !!of_get_property(np, prop, NULL);
4734 if (frame && framemaster)
4735 *framemaster = of_parse_phandle(np, prop, 0);
4737 switch ((bit << 4) + frame) {
4739 format |= SND_SOC_DAIFMT_CBM_CFM;
4742 format |= SND_SOC_DAIFMT_CBM_CFS;
4745 format |= SND_SOC_DAIFMT_CBS_CFM;
4748 format |= SND_SOC_DAIFMT_CBS_CFS;
4754 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4756 int snd_soc_of_get_dai_name(struct device_node *of_node,
4757 const char **dai_name)
4759 struct snd_soc_component *pos;
4760 struct of_phandle_args args;
4763 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4764 "#sound-dai-cells", 0, &args);
4768 ret = -EPROBE_DEFER;
4770 mutex_lock(&client_mutex);
4771 list_for_each_entry(pos, &component_list, list) {
4772 if (pos->dev->of_node != args.np)
4775 if (pos->driver->of_xlate_dai_name) {
4776 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4780 switch (args.args_count) {
4782 id = 0; /* same as dai_drv[0] */
4792 if (id < 0 || id >= pos->num_dai) {
4799 *dai_name = pos->dai_drv[id].name;
4801 *dai_name = pos->name;
4806 mutex_unlock(&client_mutex);
4808 of_node_put(args.np);
4812 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4814 static int __init snd_soc_init(void)
4816 #ifdef CONFIG_DEBUG_FS
4817 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4818 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
4819 pr_warn("ASoC: Failed to create debugfs directory\n");
4820 snd_soc_debugfs_root = NULL;
4823 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4825 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4827 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4829 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
4831 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
4832 &platform_list_fops))
4833 pr_warn("ASoC: Failed to create platform list debugfs file\n");
4836 snd_soc_util_init();
4838 return platform_driver_register(&soc_driver);
4840 module_init(snd_soc_init);
4842 static void __exit snd_soc_exit(void)
4844 snd_soc_util_exit();
4846 #ifdef CONFIG_DEBUG_FS
4847 debugfs_remove_recursive(snd_soc_debugfs_root);
4849 platform_driver_unregister(&soc_driver);
4851 module_exit(snd_soc_exit);
4853 /* Module information */
4854 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4855 MODULE_DESCRIPTION("ALSA SoC Core");
4856 MODULE_LICENSE("GPL");
4857 MODULE_ALIAS("platform:soc-audio");