2 * soc-core.c -- ALSA SoC Audio Layer
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
30 #include <linux/bitops.h>
31 #include <linux/debugfs.h>
32 #include <linux/platform_device.h>
33 #include <linux/pinctrl/consumer.h>
34 #include <linux/ctype.h>
35 #include <linux/slab.h>
37 #include <linux/gpio.h>
38 #include <linux/of_gpio.h>
39 #include <sound/ac97_codec.h>
40 #include <sound/core.h>
41 #include <sound/jack.h>
42 #include <sound/pcm.h>
43 #include <sound/pcm_params.h>
44 #include <sound/soc.h>
45 #include <sound/soc-dpcm.h>
46 #include <sound/initval.h>
48 #define CREATE_TRACE_POINTS
49 #include <trace/events/asoc.h>
53 #ifdef CONFIG_DEBUG_FS
54 struct dentry *snd_soc_debugfs_root;
55 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
58 static DEFINE_MUTEX(client_mutex);
59 static LIST_HEAD(platform_list);
60 static LIST_HEAD(codec_list);
61 static LIST_HEAD(component_list);
64 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
68 static int pmdown_time = 5000;
69 module_param(pmdown_time, int, 0);
70 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
72 struct snd_ac97_reset_cfg {
74 struct pinctrl_state *pstate_reset;
75 struct pinctrl_state *pstate_warm_reset;
76 struct pinctrl_state *pstate_run;
82 /* returns the minimum number of bytes needed to represent
83 * a particular given value */
84 static int min_bytes_needed(unsigned long val)
89 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
92 c = (sizeof val * 8) - c;
100 /* fill buf which is 'len' bytes with a formatted
101 * string of the form 'reg: value\n' */
102 static int format_register_str(struct snd_soc_codec *codec,
103 unsigned int reg, char *buf, size_t len)
105 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
106 int regsize = codec->driver->reg_word_size * 2;
108 char tmpbuf[len + 1];
109 char regbuf[regsize + 1];
111 /* since tmpbuf is allocated on the stack, warn the callers if they
112 * try to abuse this function */
115 /* +2 for ': ' and + 1 for '\n' */
116 if (wordsize + regsize + 2 + 1 != len)
119 ret = snd_soc_read(codec, reg);
121 memset(regbuf, 'X', regsize);
122 regbuf[regsize] = '\0';
124 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
127 /* prepare the buffer */
128 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
129 /* copy it back to the caller without the '\0' */
130 memcpy(buf, tmpbuf, len);
135 /* codec register dump */
136 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
137 size_t count, loff_t pos)
140 int wordsize, regsize;
145 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
146 regsize = codec->driver->reg_word_size * 2;
148 len = wordsize + regsize + 2 + 1;
150 if (!codec->driver->reg_cache_size)
153 if (codec->driver->reg_cache_step)
154 step = codec->driver->reg_cache_step;
156 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
157 /* only support larger than PAGE_SIZE bytes debugfs
158 * entries for the default case */
160 if (total + len >= count - 1)
162 format_register_str(codec, i, buf + total, len);
168 total = min(total, count - 1);
173 static ssize_t codec_reg_show(struct device *dev,
174 struct device_attribute *attr, char *buf)
176 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
178 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
181 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
183 static ssize_t pmdown_time_show(struct device *dev,
184 struct device_attribute *attr, char *buf)
186 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
188 return sprintf(buf, "%ld\n", rtd->pmdown_time);
191 static ssize_t pmdown_time_set(struct device *dev,
192 struct device_attribute *attr,
193 const char *buf, size_t count)
195 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
198 ret = kstrtol(buf, 10, &rtd->pmdown_time);
205 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
207 #ifdef CONFIG_DEBUG_FS
208 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
209 size_t count, loff_t *ppos)
212 struct snd_soc_codec *codec = file->private_data;
215 if (*ppos < 0 || !count)
218 buf = kmalloc(count, GFP_KERNEL);
222 ret = soc_codec_reg_show(codec, buf, count, *ppos);
224 if (copy_to_user(user_buf, buf, ret)) {
235 static ssize_t codec_reg_write_file(struct file *file,
236 const char __user *user_buf, size_t count, loff_t *ppos)
241 unsigned long reg, value;
242 struct snd_soc_codec *codec = file->private_data;
245 buf_size = min(count, (sizeof(buf)-1));
246 if (copy_from_user(buf, user_buf, buf_size))
250 while (*start == ' ')
252 reg = simple_strtoul(start, &start, 16);
253 while (*start == ' ')
255 ret = kstrtoul(start, 16, &value);
259 /* Userspace has been fiddling around behind the kernel's back */
260 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
262 snd_soc_write(codec, reg, value);
266 static const struct file_operations codec_reg_fops = {
268 .read = codec_reg_read_file,
269 .write = codec_reg_write_file,
270 .llseek = default_llseek,
273 static void soc_init_component_debugfs(struct snd_soc_component *component)
275 if (component->debugfs_prefix) {
278 name = kasprintf(GFP_KERNEL, "%s:%s",
279 component->debugfs_prefix, component->name);
281 component->debugfs_root = debugfs_create_dir(name,
282 component->card->debugfs_card_root);
286 component->debugfs_root = debugfs_create_dir(component->name,
287 component->card->debugfs_card_root);
290 if (!component->debugfs_root) {
291 dev_warn(component->dev,
292 "ASoC: Failed to create component debugfs directory\n");
296 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
297 component->debugfs_root);
299 if (component->init_debugfs)
300 component->init_debugfs(component);
303 static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
305 debugfs_remove_recursive(component->debugfs_root);
308 static void soc_init_codec_debugfs(struct snd_soc_component *component)
310 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
312 debugfs_create_bool("cache_sync", 0444, codec->component.debugfs_root,
314 debugfs_create_bool("cache_only", 0444, codec->component.debugfs_root,
317 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
318 codec->component.debugfs_root,
319 codec, &codec_reg_fops);
320 if (!codec->debugfs_reg)
322 "ASoC: Failed to create codec register debugfs file\n");
325 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
326 size_t count, loff_t *ppos)
328 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
329 ssize_t len, ret = 0;
330 struct snd_soc_codec *codec;
335 list_for_each_entry(codec, &codec_list, list) {
336 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
337 codec->component.name);
340 if (ret > PAGE_SIZE) {
347 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
354 static const struct file_operations codec_list_fops = {
355 .read = codec_list_read_file,
356 .llseek = default_llseek,/* read accesses f_pos */
359 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
360 size_t count, loff_t *ppos)
362 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
363 ssize_t len, ret = 0;
364 struct snd_soc_component *component;
365 struct snd_soc_dai *dai;
370 list_for_each_entry(component, &component_list, list) {
371 list_for_each_entry(dai, &component->dai_list, list) {
372 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
376 if (ret > PAGE_SIZE) {
383 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
390 static const struct file_operations dai_list_fops = {
391 .read = dai_list_read_file,
392 .llseek = default_llseek,/* read accesses f_pos */
395 static ssize_t platform_list_read_file(struct file *file,
396 char __user *user_buf,
397 size_t count, loff_t *ppos)
399 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
400 ssize_t len, ret = 0;
401 struct snd_soc_platform *platform;
406 list_for_each_entry(platform, &platform_list, list) {
407 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
408 platform->component.name);
411 if (ret > PAGE_SIZE) {
417 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
424 static const struct file_operations platform_list_fops = {
425 .read = platform_list_read_file,
426 .llseek = default_llseek,/* read accesses f_pos */
429 static void soc_init_card_debugfs(struct snd_soc_card *card)
431 card->debugfs_card_root = debugfs_create_dir(card->name,
432 snd_soc_debugfs_root);
433 if (!card->debugfs_card_root) {
435 "ASoC: Failed to create card debugfs directory\n");
439 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
440 card->debugfs_card_root,
442 if (!card->debugfs_pop_time)
444 "ASoC: Failed to create pop time debugfs file\n");
447 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
449 debugfs_remove_recursive(card->debugfs_card_root);
454 #define soc_init_codec_debugfs NULL
456 static inline void soc_init_component_debugfs(
457 struct snd_soc_component *component)
461 static inline void soc_cleanup_component_debugfs(
462 struct snd_soc_component *component)
466 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
470 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
475 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
476 const char *dai_link, int stream)
480 for (i = 0; i < card->num_links; i++) {
481 if (card->rtd[i].dai_link->no_pcm &&
482 !strcmp(card->rtd[i].dai_link->name, dai_link))
483 return card->rtd[i].pcm->streams[stream].substream;
485 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
488 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
490 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
491 const char *dai_link)
495 for (i = 0; i < card->num_links; i++) {
496 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
497 return &card->rtd[i];
499 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
502 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
504 #ifdef CONFIG_SND_SOC_AC97_BUS
505 /* unregister ac97 codec */
506 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
508 if (codec->ac97->dev.bus)
509 device_unregister(&codec->ac97->dev);
513 /* stop no dev release warning */
514 static void soc_ac97_device_release(struct device *dev){}
516 /* register ac97 codec to bus */
517 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
521 codec->ac97->dev.bus = &ac97_bus_type;
522 codec->ac97->dev.parent = codec->component.card->dev;
523 codec->ac97->dev.release = soc_ac97_device_release;
525 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
526 codec->component.card->snd_card->number, 0,
527 codec->component.name);
528 err = device_register(&codec->ac97->dev);
530 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
531 codec->ac97->dev.bus = NULL;
538 static void codec2codec_close_delayed_work(struct work_struct *work)
540 /* Currently nothing to do for c2c links
541 * Since c2c links are internal nodes in the DAPM graph and
542 * don't interface with the outside world or application layer
543 * we don't have to do any special handling on close.
547 #ifdef CONFIG_PM_SLEEP
548 /* powers down audio subsystem for suspend */
549 int snd_soc_suspend(struct device *dev)
551 struct snd_soc_card *card = dev_get_drvdata(dev);
552 struct snd_soc_codec *codec;
555 /* If the card is not initialized yet there is nothing to do */
556 if (!card->instantiated)
559 /* Due to the resume being scheduled into a workqueue we could
560 * suspend before that's finished - wait for it to complete.
562 snd_power_lock(card->snd_card);
563 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
564 snd_power_unlock(card->snd_card);
566 /* we're going to block userspace touching us until resume completes */
567 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
569 /* mute any active DACs */
570 for (i = 0; i < card->num_rtd; i++) {
572 if (card->rtd[i].dai_link->ignore_suspend)
575 for (j = 0; j < card->rtd[i].num_codecs; j++) {
576 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
577 struct snd_soc_dai_driver *drv = dai->driver;
579 if (drv->ops->digital_mute && dai->playback_active)
580 drv->ops->digital_mute(dai, 1);
584 /* suspend all pcms */
585 for (i = 0; i < card->num_rtd; i++) {
586 if (card->rtd[i].dai_link->ignore_suspend)
589 snd_pcm_suspend_all(card->rtd[i].pcm);
592 if (card->suspend_pre)
593 card->suspend_pre(card);
595 for (i = 0; i < card->num_rtd; i++) {
596 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
597 struct snd_soc_platform *platform = card->rtd[i].platform;
599 if (card->rtd[i].dai_link->ignore_suspend)
602 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
603 cpu_dai->driver->suspend(cpu_dai);
604 if (platform->driver->suspend && !platform->suspended) {
605 platform->driver->suspend(cpu_dai);
606 platform->suspended = 1;
610 /* close any waiting streams and save state */
611 for (i = 0; i < card->num_rtd; i++) {
612 struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais;
613 flush_delayed_work(&card->rtd[i].delayed_work);
614 for (j = 0; j < card->rtd[i].num_codecs; j++) {
615 codec_dais[j]->codec->dapm.suspend_bias_level =
616 codec_dais[j]->codec->dapm.bias_level;
620 for (i = 0; i < card->num_rtd; i++) {
622 if (card->rtd[i].dai_link->ignore_suspend)
625 snd_soc_dapm_stream_event(&card->rtd[i],
626 SNDRV_PCM_STREAM_PLAYBACK,
627 SND_SOC_DAPM_STREAM_SUSPEND);
629 snd_soc_dapm_stream_event(&card->rtd[i],
630 SNDRV_PCM_STREAM_CAPTURE,
631 SND_SOC_DAPM_STREAM_SUSPEND);
634 /* Recheck all analogue paths too */
635 dapm_mark_io_dirty(&card->dapm);
636 snd_soc_dapm_sync(&card->dapm);
638 /* suspend all CODECs */
639 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
640 /* If there are paths active then the CODEC will be held with
641 * bias _ON and should not be suspended. */
642 if (!codec->suspended && codec->driver->suspend) {
643 switch (codec->dapm.bias_level) {
644 case SND_SOC_BIAS_STANDBY:
646 * If the CODEC is capable of idle
647 * bias off then being in STANDBY
648 * means it's doing something,
649 * otherwise fall through.
651 if (codec->dapm.idle_bias_off) {
653 "ASoC: idle_bias_off CODEC on over suspend\n");
656 case SND_SOC_BIAS_OFF:
657 codec->driver->suspend(codec);
658 codec->suspended = 1;
659 codec->cache_sync = 1;
660 if (codec->component.regmap)
661 regcache_mark_dirty(codec->component.regmap);
662 /* deactivate pins to sleep state */
663 pinctrl_pm_select_sleep_state(codec->dev);
667 "ASoC: CODEC is on over suspend\n");
673 for (i = 0; i < card->num_rtd; i++) {
674 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
676 if (card->rtd[i].dai_link->ignore_suspend)
679 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
680 cpu_dai->driver->suspend(cpu_dai);
682 /* deactivate pins to sleep state */
683 pinctrl_pm_select_sleep_state(cpu_dai->dev);
686 if (card->suspend_post)
687 card->suspend_post(card);
691 EXPORT_SYMBOL_GPL(snd_soc_suspend);
693 /* deferred resume work, so resume can complete before we finished
694 * setting our codec back up, which can be very slow on I2C
696 static void soc_resume_deferred(struct work_struct *work)
698 struct snd_soc_card *card =
699 container_of(work, struct snd_soc_card, deferred_resume_work);
700 struct snd_soc_codec *codec;
703 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
704 * so userspace apps are blocked from touching us
707 dev_dbg(card->dev, "ASoC: starting resume work\n");
709 /* Bring us up into D2 so that DAPM starts enabling things */
710 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
712 if (card->resume_pre)
713 card->resume_pre(card);
715 /* resume AC97 DAIs */
716 for (i = 0; i < card->num_rtd; i++) {
717 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
719 if (card->rtd[i].dai_link->ignore_suspend)
722 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
723 cpu_dai->driver->resume(cpu_dai);
726 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
727 /* If the CODEC was idle over suspend then it will have been
728 * left with bias OFF or STANDBY and suspended so we must now
729 * resume. Otherwise the suspend was suppressed.
731 if (codec->driver->resume && codec->suspended) {
732 switch (codec->dapm.bias_level) {
733 case SND_SOC_BIAS_STANDBY:
734 case SND_SOC_BIAS_OFF:
735 codec->driver->resume(codec);
736 codec->suspended = 0;
740 "ASoC: CODEC was on over suspend\n");
746 for (i = 0; i < card->num_rtd; i++) {
748 if (card->rtd[i].dai_link->ignore_suspend)
751 snd_soc_dapm_stream_event(&card->rtd[i],
752 SNDRV_PCM_STREAM_PLAYBACK,
753 SND_SOC_DAPM_STREAM_RESUME);
755 snd_soc_dapm_stream_event(&card->rtd[i],
756 SNDRV_PCM_STREAM_CAPTURE,
757 SND_SOC_DAPM_STREAM_RESUME);
760 /* unmute any active DACs */
761 for (i = 0; i < card->num_rtd; i++) {
763 if (card->rtd[i].dai_link->ignore_suspend)
766 for (j = 0; j < card->rtd[i].num_codecs; j++) {
767 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
768 struct snd_soc_dai_driver *drv = dai->driver;
770 if (drv->ops->digital_mute && dai->playback_active)
771 drv->ops->digital_mute(dai, 0);
775 for (i = 0; i < card->num_rtd; i++) {
776 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
777 struct snd_soc_platform *platform = card->rtd[i].platform;
779 if (card->rtd[i].dai_link->ignore_suspend)
782 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
783 cpu_dai->driver->resume(cpu_dai);
784 if (platform->driver->resume && platform->suspended) {
785 platform->driver->resume(cpu_dai);
786 platform->suspended = 0;
790 if (card->resume_post)
791 card->resume_post(card);
793 dev_dbg(card->dev, "ASoC: resume work completed\n");
795 /* userspace can access us now we are back as we were before */
796 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
798 /* Recheck all analogue paths too */
799 dapm_mark_io_dirty(&card->dapm);
800 snd_soc_dapm_sync(&card->dapm);
803 /* powers up audio subsystem after a suspend */
804 int snd_soc_resume(struct device *dev)
806 struct snd_soc_card *card = dev_get_drvdata(dev);
807 int i, ac97_control = 0;
809 /* If the card is not initialized yet there is nothing to do */
810 if (!card->instantiated)
813 /* activate pins from sleep state */
814 for (i = 0; i < card->num_rtd; i++) {
815 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
816 struct snd_soc_dai **codec_dais = rtd->codec_dais;
817 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
821 pinctrl_pm_select_default_state(cpu_dai->dev);
823 for (j = 0; j < rtd->num_codecs; j++) {
824 struct snd_soc_dai *codec_dai = codec_dais[j];
825 if (codec_dai->active)
826 pinctrl_pm_select_default_state(codec_dai->dev);
830 /* AC97 devices might have other drivers hanging off them so
831 * need to resume immediately. Other drivers don't have that
832 * problem and may take a substantial amount of time to resume
833 * due to I/O costs and anti-pop so handle them out of line.
835 for (i = 0; i < card->num_rtd; i++) {
836 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
837 ac97_control |= cpu_dai->driver->ac97_control;
840 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
841 soc_resume_deferred(&card->deferred_resume_work);
843 dev_dbg(dev, "ASoC: Scheduling resume work\n");
844 if (!schedule_work(&card->deferred_resume_work))
845 dev_err(dev, "ASoC: resume work item may be lost\n");
850 EXPORT_SYMBOL_GPL(snd_soc_resume);
852 #define snd_soc_suspend NULL
853 #define snd_soc_resume NULL
856 static const struct snd_soc_dai_ops null_dai_ops = {
859 static struct snd_soc_component *soc_find_component(
860 const struct device_node *of_node, const char *name)
862 struct snd_soc_component *component;
864 list_for_each_entry(component, &component_list, list) {
866 if (component->dev->of_node == of_node)
868 } else if (strcmp(component->name, name) == 0) {
876 static struct snd_soc_dai *snd_soc_find_dai(
877 const struct snd_soc_dai_link_component *dlc)
879 struct snd_soc_component *component;
880 struct snd_soc_dai *dai;
882 /* Find CPU DAI from registered DAIs*/
883 list_for_each_entry(component, &component_list, list) {
884 if (dlc->of_node && component->dev->of_node != dlc->of_node)
886 if (dlc->name && strcmp(dev_name(component->dev), dlc->name))
888 list_for_each_entry(dai, &component->dai_list, list) {
889 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
899 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
901 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
902 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
903 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
904 struct snd_soc_dai_link_component cpu_dai_component;
905 struct snd_soc_dai **codec_dais = rtd->codec_dais;
906 struct snd_soc_platform *platform;
907 const char *platform_name;
910 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
912 cpu_dai_component.name = dai_link->cpu_name;
913 cpu_dai_component.of_node = dai_link->cpu_of_node;
914 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
915 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
917 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
918 dai_link->cpu_dai_name);
919 return -EPROBE_DEFER;
922 rtd->num_codecs = dai_link->num_codecs;
924 /* Find CODEC from registered CODECs */
925 for (i = 0; i < rtd->num_codecs; i++) {
926 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
927 if (!codec_dais[i]) {
928 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
930 return -EPROBE_DEFER;
934 /* Single codec links expect codec and codec_dai in runtime data */
935 rtd->codec_dai = codec_dais[0];
936 rtd->codec = rtd->codec_dai->codec;
938 /* if there's no platform we match on the empty platform */
939 platform_name = dai_link->platform_name;
940 if (!platform_name && !dai_link->platform_of_node)
941 platform_name = "snd-soc-dummy";
943 /* find one from the set of registered platforms */
944 list_for_each_entry(platform, &platform_list, list) {
945 if (dai_link->platform_of_node) {
946 if (platform->dev->of_node !=
947 dai_link->platform_of_node)
950 if (strcmp(platform->component.name, platform_name))
954 rtd->platform = platform;
956 if (!rtd->platform) {
957 dev_err(card->dev, "ASoC: platform %s not registered\n",
958 dai_link->platform_name);
959 return -EPROBE_DEFER;
967 static void soc_remove_component(struct snd_soc_component *component)
969 if (!component->probed)
972 /* This is a HACK and will be removed soon */
973 if (component->codec)
974 list_del(&component->codec->card_list);
976 if (component->remove)
977 component->remove(component);
979 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
981 soc_cleanup_component_debugfs(component);
982 component->probed = 0;
983 module_put(component->dev->driver->owner);
986 static void soc_remove_dai(struct snd_soc_dai *dai, int order)
990 if (dai && dai->probed &&
991 dai->driver->remove_order == order) {
992 if (dai->driver->remove) {
993 err = dai->driver->remove(dai);
996 "ASoC: failed to remove %s: %d\n",
1003 static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
1005 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1008 /* unregister the rtd device */
1009 if (rtd->dev_registered) {
1010 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1011 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1012 device_unregister(rtd->dev);
1013 rtd->dev_registered = 0;
1016 /* remove the CODEC DAI */
1017 for (i = 0; i < rtd->num_codecs; i++)
1018 soc_remove_dai(rtd->codec_dais[i], order);
1020 soc_remove_dai(rtd->cpu_dai, order);
1023 static void soc_remove_link_components(struct snd_soc_card *card, int num,
1026 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1027 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1028 struct snd_soc_platform *platform = rtd->platform;
1029 struct snd_soc_component *component;
1032 /* remove the platform */
1033 if (platform && platform->component.driver->remove_order == order)
1034 soc_remove_component(&platform->component);
1036 /* remove the CODEC-side CODEC */
1037 for (i = 0; i < rtd->num_codecs; i++) {
1038 component = rtd->codec_dais[i]->component;
1039 if (component->driver->remove_order == order)
1040 soc_remove_component(component);
1043 /* remove any CPU-side CODEC */
1045 if (cpu_dai->component->driver->remove_order == order)
1046 soc_remove_component(cpu_dai->component);
1050 static void soc_remove_dai_links(struct snd_soc_card *card)
1054 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1056 for (dai = 0; dai < card->num_rtd; dai++)
1057 soc_remove_link_dais(card, dai, order);
1060 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1062 for (dai = 0; dai < card->num_rtd; dai++)
1063 soc_remove_link_components(card, dai, order);
1069 static void soc_set_name_prefix(struct snd_soc_card *card,
1070 struct snd_soc_component *component)
1074 if (card->codec_conf == NULL)
1077 for (i = 0; i < card->num_configs; i++) {
1078 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1079 if (map->of_node && component->dev->of_node != map->of_node)
1081 if (map->dev_name && strcmp(component->name, map->dev_name))
1083 component->name_prefix = map->name_prefix;
1088 static int soc_probe_component(struct snd_soc_card *card,
1089 struct snd_soc_component *component)
1091 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1092 struct snd_soc_component *dai_component, *component2;
1093 struct snd_soc_dai *dai;
1096 if (component->probed)
1099 component->card = card;
1101 soc_set_name_prefix(card, component);
1103 if (!try_module_get(component->dev->driver->owner))
1106 soc_init_component_debugfs(component);
1108 if (component->dapm_widgets) {
1109 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1110 component->num_dapm_widgets);
1113 dev_err(component->dev,
1114 "Failed to create new controls %d\n", ret);
1120 * This is rather ugly, but certain platforms expect that the DAPM
1121 * widgets for the DAIs for components with the same parent device are
1122 * created in the platforms DAPM context. Until that is fixed we need to
1125 if (component->steal_sibling_dai_widgets) {
1126 dai_component = NULL;
1127 list_for_each_entry(component2, &component_list, list) {
1128 if (component == component2)
1131 if (component2->dev == component->dev &&
1132 !list_empty(&component2->dai_list)) {
1133 dai_component = component2;
1138 dai_component = component;
1139 list_for_each_entry(component2, &component_list, list) {
1140 if (component2->dev == component->dev &&
1141 component2->steal_sibling_dai_widgets) {
1142 dai_component = NULL;
1148 if (dai_component) {
1149 list_for_each_entry(dai, &dai_component->dai_list, list) {
1150 snd_soc_dapm_new_dai_widgets(dapm, dai);
1152 dev_err(component->dev,
1153 "Failed to create DAI widgets %d\n",
1160 if (component->probe) {
1161 ret = component->probe(component);
1163 dev_err(component->dev,
1164 "ASoC: failed to probe component %d\n", ret);
1168 WARN(dapm->idle_bias_off &&
1169 dapm->bias_level != SND_SOC_BIAS_OFF,
1170 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1174 if (component->controls)
1175 snd_soc_add_component_controls(component, component->controls,
1176 component->num_controls);
1177 if (component->dapm_routes)
1178 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1179 component->num_dapm_routes);
1181 component->probed = 1;
1182 list_add(&dapm->list, &card->dapm_list);
1184 /* This is a HACK and will be removed soon */
1185 if (component->codec)
1186 list_add(&component->codec->card_list, &card->codec_dev_list);
1191 soc_cleanup_component_debugfs(component);
1192 module_put(component->dev->driver->owner);
1197 static void rtd_release(struct device *dev)
1202 static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1207 /* register the rtd device */
1208 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1211 device_initialize(rtd->dev);
1212 rtd->dev->parent = rtd->card->dev;
1213 rtd->dev->release = rtd_release;
1214 rtd->dev->init_name = name;
1215 dev_set_drvdata(rtd->dev, rtd);
1216 mutex_init(&rtd->pcm_mutex);
1217 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1218 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1219 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1220 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1221 ret = device_add(rtd->dev);
1223 /* calling put_device() here to free the rtd->dev */
1224 put_device(rtd->dev);
1225 dev_err(rtd->card->dev,
1226 "ASoC: failed to register runtime device: %d\n", ret);
1229 rtd->dev_registered = 1;
1232 /* add DAPM sysfs entries for this codec */
1233 ret = snd_soc_dapm_sys_add(rtd->dev);
1236 "ASoC: failed to add codec dapm sysfs entries: %d\n",
1239 /* add codec sysfs entries */
1240 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1243 "ASoC: failed to add codec sysfs files: %d\n",
1250 static int soc_probe_link_components(struct snd_soc_card *card, int num,
1253 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1254 struct snd_soc_platform *platform = rtd->platform;
1255 struct snd_soc_component *component;
1258 /* probe the CPU-side component, if it is a CODEC */
1259 component = rtd->cpu_dai->component;
1260 if (component->driver->probe_order == order) {
1261 ret = soc_probe_component(card, component);
1266 /* probe the CODEC-side components */
1267 for (i = 0; i < rtd->num_codecs; i++) {
1268 component = rtd->codec_dais[i]->component;
1269 if (component->driver->probe_order == order) {
1270 ret = soc_probe_component(card, component);
1276 /* probe the platform */
1277 if (platform->component.driver->probe_order == order) {
1278 ret = soc_probe_component(card, &platform->component);
1286 static int soc_probe_codec_dai(struct snd_soc_card *card,
1287 struct snd_soc_dai *codec_dai,
1292 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1293 if (codec_dai->driver->probe) {
1294 ret = codec_dai->driver->probe(codec_dai);
1296 dev_err(codec_dai->dev,
1297 "ASoC: failed to probe CODEC DAI %s: %d\n",
1298 codec_dai->name, ret);
1303 /* mark codec_dai as probed and add to card dai list */
1304 codec_dai->probed = 1;
1310 static int soc_link_dai_widgets(struct snd_soc_card *card,
1311 struct snd_soc_dai_link *dai_link,
1312 struct snd_soc_pcm_runtime *rtd)
1314 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1315 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1316 struct snd_soc_dapm_widget *play_w, *capture_w;
1319 if (rtd->num_codecs > 1)
1320 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1322 /* link the DAI widgets */
1323 play_w = codec_dai->playback_widget;
1324 capture_w = cpu_dai->capture_widget;
1325 if (play_w && capture_w) {
1326 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1329 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1330 play_w->name, capture_w->name, ret);
1335 play_w = cpu_dai->playback_widget;
1336 capture_w = codec_dai->capture_widget;
1337 if (play_w && capture_w) {
1338 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1341 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1342 play_w->name, capture_w->name, ret);
1350 static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1352 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1353 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1354 struct snd_soc_platform *platform = rtd->platform;
1355 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1358 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1359 card->name, num, order);
1361 /* config components */
1362 cpu_dai->platform = platform;
1363 cpu_dai->card = card;
1364 for (i = 0; i < rtd->num_codecs; i++)
1365 rtd->codec_dais[i]->card = card;
1367 /* set default power off timeout */
1368 rtd->pmdown_time = pmdown_time;
1370 /* probe the cpu_dai */
1371 if (!cpu_dai->probed &&
1372 cpu_dai->driver->probe_order == order) {
1373 if (cpu_dai->driver->probe) {
1374 ret = cpu_dai->driver->probe(cpu_dai);
1376 dev_err(cpu_dai->dev,
1377 "ASoC: failed to probe CPU DAI %s: %d\n",
1378 cpu_dai->name, ret);
1382 cpu_dai->probed = 1;
1385 /* probe the CODEC DAI */
1386 for (i = 0; i < rtd->num_codecs; i++) {
1387 ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order);
1392 /* complete DAI probe during last probe */
1393 if (order != SND_SOC_COMP_ORDER_LAST)
1396 /* do machine specific initialization */
1397 if (dai_link->init) {
1398 ret = dai_link->init(rtd);
1400 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1401 dai_link->name, ret);
1406 ret = soc_post_component_init(rtd, dai_link->name);
1410 #ifdef CONFIG_DEBUG_FS
1411 /* add DPCM sysfs entries */
1412 if (dai_link->dynamic) {
1413 ret = soc_dpcm_debugfs_add(rtd);
1416 "ASoC: failed to add dpcm sysfs entries: %d\n",
1423 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1425 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1428 if (cpu_dai->driver->compress_dai) {
1429 /*create compress_device"*/
1430 ret = soc_new_compress(rtd, num);
1432 dev_err(card->dev, "ASoC: can't create compress %s\n",
1433 dai_link->stream_name);
1438 if (!dai_link->params) {
1439 /* create the pcm */
1440 ret = soc_new_pcm(rtd, num);
1442 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1443 dai_link->stream_name, ret);
1447 INIT_DELAYED_WORK(&rtd->delayed_work,
1448 codec2codec_close_delayed_work);
1450 /* link the DAI widgets */
1451 ret = soc_link_dai_widgets(card, dai_link, rtd);
1457 /* add platform data for AC97 devices */
1458 for (i = 0; i < rtd->num_codecs; i++) {
1459 if (rtd->codec_dais[i]->driver->ac97_control)
1460 snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
1461 rtd->cpu_dai->ac97_pdata);
1467 #ifdef CONFIG_SND_SOC_AC97_BUS
1468 static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1469 struct snd_soc_dai *codec_dai)
1473 /* Only instantiate AC97 if not already done by the adaptor
1474 * for the generic AC97 subsystem.
1476 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
1478 * It is possible that the AC97 device is already registered to
1479 * the device subsystem. This happens when the device is created
1480 * via snd_ac97_mixer(). Currently only SoC codec that does so
1481 * is the generic AC97 glue but others migh emerge.
1483 * In those cases we don't try to register the device again.
1485 if (!codec->ac97_created)
1488 ret = soc_ac97_dev_register(codec);
1491 "ASoC: AC97 device register failed: %d\n", ret);
1495 codec->ac97_registered = 1;
1500 static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
1502 if (codec->ac97_registered) {
1503 soc_ac97_dev_unregister(codec);
1504 codec->ac97_registered = 0;
1508 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1512 for (i = 0; i < rtd->num_codecs; i++) {
1513 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1515 ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
1518 soc_unregister_ac97_codec(codec_dai->codec);
1526 static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1530 for (i = 0; i < rtd->num_codecs; i++)
1531 soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
1535 static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1537 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1538 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1539 const char *name = aux_dev->codec_name;
1541 rtd->component = soc_find_component(aux_dev->codec_of_node, name);
1542 if (!rtd->component) {
1543 if (aux_dev->codec_of_node)
1544 name = of_node_full_name(aux_dev->codec_of_node);
1546 dev_err(card->dev, "ASoC: %s not registered\n", name);
1547 return -EPROBE_DEFER;
1551 * Some places still reference rtd->codec, so we have to keep that
1552 * initialized if the component is a CODEC. Once all those references
1553 * have been removed, this code can be removed as well.
1555 rtd->codec = rtd->component->codec;
1560 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1562 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1563 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1566 ret = soc_probe_component(card, rtd->component);
1570 /* do machine specific initialization */
1571 if (aux_dev->init) {
1572 ret = aux_dev->init(rtd->component);
1574 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1575 aux_dev->name, ret);
1580 return soc_post_component_init(rtd, aux_dev->name);
1583 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1585 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1586 struct snd_soc_component *component = rtd->component;
1588 /* unregister the rtd device */
1589 if (rtd->dev_registered) {
1590 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1591 device_unregister(rtd->dev);
1592 rtd->dev_registered = 0;
1595 if (component && component->probed)
1596 soc_remove_component(component);
1599 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1603 if (codec->cache_init)
1606 ret = snd_soc_cache_init(codec);
1609 "ASoC: Failed to set cache compression type: %d\n",
1613 codec->cache_init = 1;
1617 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1619 struct snd_soc_codec *codec;
1620 struct snd_soc_dai_link *dai_link;
1621 int ret, i, order, dai_fmt;
1623 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1626 for (i = 0; i < card->num_links; i++) {
1627 ret = soc_bind_dai_link(card, i);
1632 /* bind aux_devs too */
1633 for (i = 0; i < card->num_aux_devs; i++) {
1634 ret = soc_bind_aux_dev(card, i);
1639 /* initialize the register cache for each available codec */
1640 list_for_each_entry(codec, &codec_list, list) {
1641 if (codec->cache_init)
1643 ret = snd_soc_init_codec_cache(codec);
1648 /* card bind complete so register a sound card */
1649 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1650 card->owner, 0, &card->snd_card);
1653 "ASoC: can't create sound card for card %s: %d\n",
1658 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1659 card->dapm.dev = card->dev;
1660 card->dapm.card = card;
1661 list_add(&card->dapm.list, &card->dapm_list);
1663 #ifdef CONFIG_DEBUG_FS
1664 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1667 #ifdef CONFIG_PM_SLEEP
1668 /* deferred resume work */
1669 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1672 if (card->dapm_widgets)
1673 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1674 card->num_dapm_widgets);
1676 /* initialise the sound card only once */
1678 ret = card->probe(card);
1680 goto card_probe_error;
1683 /* probe all components used by DAI links on this card */
1684 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1686 for (i = 0; i < card->num_links; i++) {
1687 ret = soc_probe_link_components(card, i, order);
1690 "ASoC: failed to instantiate card %d\n",
1697 /* probe all DAI links on this card */
1698 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1700 for (i = 0; i < card->num_links; i++) {
1701 ret = soc_probe_link_dais(card, i, order);
1704 "ASoC: failed to instantiate card %d\n",
1711 for (i = 0; i < card->num_aux_devs; i++) {
1712 ret = soc_probe_aux_dev(card, i);
1715 "ASoC: failed to add auxiliary devices %d\n",
1717 goto probe_aux_dev_err;
1721 snd_soc_dapm_link_dai_widgets(card);
1722 snd_soc_dapm_connect_dai_link_widgets(card);
1725 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1727 if (card->dapm_routes)
1728 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1729 card->num_dapm_routes);
1731 for (i = 0; i < card->num_links; i++) {
1732 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1733 dai_link = &card->dai_link[i];
1734 dai_fmt = dai_link->dai_fmt;
1737 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1740 for (j = 0; j < rtd->num_codecs; j++) {
1741 struct snd_soc_dai *codec_dai = codec_dais[j];
1743 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1744 if (ret != 0 && ret != -ENOTSUPP)
1745 dev_warn(codec_dai->dev,
1746 "ASoC: Failed to set DAI format: %d\n",
1751 /* If this is a regular CPU link there will be a platform */
1753 (dai_link->platform_name || dai_link->platform_of_node)) {
1754 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1756 if (ret != 0 && ret != -ENOTSUPP)
1757 dev_warn(card->rtd[i].cpu_dai->dev,
1758 "ASoC: Failed to set DAI format: %d\n",
1760 } else if (dai_fmt) {
1761 /* Flip the polarity for the "CPU" end */
1762 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1763 switch (dai_link->dai_fmt &
1764 SND_SOC_DAIFMT_MASTER_MASK) {
1765 case SND_SOC_DAIFMT_CBM_CFM:
1766 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1768 case SND_SOC_DAIFMT_CBM_CFS:
1769 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1771 case SND_SOC_DAIFMT_CBS_CFM:
1772 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1774 case SND_SOC_DAIFMT_CBS_CFS:
1775 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1779 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1781 if (ret != 0 && ret != -ENOTSUPP)
1782 dev_warn(card->rtd[i].cpu_dai->dev,
1783 "ASoC: Failed to set DAI format: %d\n",
1788 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1790 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1791 "%s", card->long_name ? card->long_name : card->name);
1792 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1793 "%s", card->driver_name ? card->driver_name : card->name);
1794 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1795 switch (card->snd_card->driver[i]) {
1801 if (!isalnum(card->snd_card->driver[i]))
1802 card->snd_card->driver[i] = '_';
1807 if (card->late_probe) {
1808 ret = card->late_probe(card);
1810 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
1812 goto probe_aux_dev_err;
1816 if (card->fully_routed)
1817 snd_soc_dapm_auto_nc_pins(card);
1819 snd_soc_dapm_new_widgets(card);
1821 ret = snd_card_register(card->snd_card);
1823 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1825 goto probe_aux_dev_err;
1828 #ifdef CONFIG_SND_SOC_AC97_BUS
1829 /* register any AC97 codecs */
1830 for (i = 0; i < card->num_rtd; i++) {
1831 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1834 "ASoC: failed to register AC97: %d\n", ret);
1836 soc_unregister_ac97_dai_link(&card->rtd[i]);
1837 goto probe_aux_dev_err;
1842 card->instantiated = 1;
1843 snd_soc_dapm_sync(&card->dapm);
1844 mutex_unlock(&card->mutex);
1849 for (i = 0; i < card->num_aux_devs; i++)
1850 soc_remove_aux_dev(card, i);
1853 soc_remove_dai_links(card);
1859 snd_card_free(card->snd_card);
1862 mutex_unlock(&card->mutex);
1867 /* probes a new socdev */
1868 static int soc_probe(struct platform_device *pdev)
1870 struct snd_soc_card *card = platform_get_drvdata(pdev);
1873 * no card, so machine driver should be registering card
1874 * we should not be here in that case so ret error
1879 dev_warn(&pdev->dev,
1880 "ASoC: machine %s should use snd_soc_register_card()\n",
1883 /* Bodge while we unpick instantiation */
1884 card->dev = &pdev->dev;
1886 return snd_soc_register_card(card);
1889 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1893 /* make sure any delayed work runs */
1894 for (i = 0; i < card->num_rtd; i++) {
1895 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1896 flush_delayed_work(&rtd->delayed_work);
1899 /* remove auxiliary devices */
1900 for (i = 0; i < card->num_aux_devs; i++)
1901 soc_remove_aux_dev(card, i);
1903 /* remove and free each DAI */
1904 soc_remove_dai_links(card);
1906 soc_cleanup_card_debugfs(card);
1908 /* remove the card */
1912 snd_soc_dapm_free(&card->dapm);
1914 snd_card_free(card->snd_card);
1919 /* removes a socdev */
1920 static int soc_remove(struct platform_device *pdev)
1922 struct snd_soc_card *card = platform_get_drvdata(pdev);
1924 snd_soc_unregister_card(card);
1928 int snd_soc_poweroff(struct device *dev)
1930 struct snd_soc_card *card = dev_get_drvdata(dev);
1933 if (!card->instantiated)
1936 /* Flush out pmdown_time work - we actually do want to run it
1937 * now, we're shutting down so no imminent restart. */
1938 for (i = 0; i < card->num_rtd; i++) {
1939 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1940 flush_delayed_work(&rtd->delayed_work);
1943 snd_soc_dapm_shutdown(card);
1945 /* deactivate pins to sleep state */
1946 for (i = 0; i < card->num_rtd; i++) {
1947 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1948 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1951 pinctrl_pm_select_sleep_state(cpu_dai->dev);
1952 for (j = 0; j < rtd->num_codecs; j++) {
1953 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
1954 pinctrl_pm_select_sleep_state(codec_dai->dev);
1960 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1962 const struct dev_pm_ops snd_soc_pm_ops = {
1963 .suspend = snd_soc_suspend,
1964 .resume = snd_soc_resume,
1965 .freeze = snd_soc_suspend,
1966 .thaw = snd_soc_resume,
1967 .poweroff = snd_soc_poweroff,
1968 .restore = snd_soc_resume,
1970 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1972 /* ASoC platform driver */
1973 static struct platform_driver soc_driver = {
1975 .name = "soc-audio",
1976 .owner = THIS_MODULE,
1977 .pm = &snd_soc_pm_ops,
1980 .remove = soc_remove,
1984 * snd_soc_new_ac97_codec - initailise AC97 device
1985 * @codec: audio codec
1986 * @ops: AC97 bus operations
1987 * @num: AC97 codec number
1989 * Initialises AC97 codec resources for use by ad-hoc devices only.
1991 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1992 struct snd_ac97_bus_ops *ops, int num)
1994 mutex_lock(&codec->mutex);
1996 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1997 if (codec->ac97 == NULL) {
1998 mutex_unlock(&codec->mutex);
2002 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2003 if (codec->ac97->bus == NULL) {
2006 mutex_unlock(&codec->mutex);
2010 codec->ac97->bus->ops = ops;
2011 codec->ac97->num = num;
2014 * Mark the AC97 device to be created by us. This way we ensure that the
2015 * device will be registered with the device subsystem later on.
2017 codec->ac97_created = 1;
2019 mutex_unlock(&codec->mutex);
2022 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2024 static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2026 static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2028 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2030 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2032 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2036 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2038 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2042 static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2044 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2046 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2048 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2049 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2050 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2054 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2056 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2060 static int snd_soc_ac97_parse_pinctl(struct device *dev,
2061 struct snd_ac97_reset_cfg *cfg)
2064 struct pinctrl_state *state;
2068 p = devm_pinctrl_get(dev);
2070 dev_err(dev, "Failed to get pinctrl\n");
2075 state = pinctrl_lookup_state(p, "ac97-reset");
2076 if (IS_ERR(state)) {
2077 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
2078 return PTR_ERR(state);
2080 cfg->pstate_reset = state;
2082 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2083 if (IS_ERR(state)) {
2084 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
2085 return PTR_ERR(state);
2087 cfg->pstate_warm_reset = state;
2089 state = pinctrl_lookup_state(p, "ac97-running");
2090 if (IS_ERR(state)) {
2091 dev_err(dev, "Can't find pinctrl state ac97-running\n");
2092 return PTR_ERR(state);
2094 cfg->pstate_run = state;
2096 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2098 dev_err(dev, "Can't find ac97-sync gpio\n");
2101 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2103 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2106 cfg->gpio_sync = gpio;
2108 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2110 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2113 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2115 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2118 cfg->gpio_sdata = gpio;
2120 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2122 dev_err(dev, "Can't find ac97-reset gpio\n");
2125 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2127 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2130 cfg->gpio_reset = gpio;
2135 struct snd_ac97_bus_ops *soc_ac97_ops;
2136 EXPORT_SYMBOL_GPL(soc_ac97_ops);
2138 int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2140 if (ops == soc_ac97_ops)
2143 if (soc_ac97_ops && ops)
2150 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2153 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2155 * This function sets the reset and warm_reset properties of ops and parses
2156 * the device node of pdev to get pinctrl states and gpio numbers to use.
2158 int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2159 struct platform_device *pdev)
2161 struct device *dev = &pdev->dev;
2162 struct snd_ac97_reset_cfg cfg;
2165 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2169 ret = snd_soc_set_ac97_ops(ops);
2173 ops->warm_reset = snd_soc_ac97_warm_reset;
2174 ops->reset = snd_soc_ac97_reset;
2176 snd_ac97_rst_cfg = cfg;
2179 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2182 * snd_soc_free_ac97_codec - free AC97 codec device
2183 * @codec: audio codec
2185 * Frees AC97 codec device resources.
2187 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2189 mutex_lock(&codec->mutex);
2190 #ifdef CONFIG_SND_SOC_AC97_BUS
2191 soc_unregister_ac97_codec(codec);
2193 kfree(codec->ac97->bus);
2196 codec->ac97_created = 0;
2197 mutex_unlock(&codec->mutex);
2199 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2202 * snd_soc_cnew - create new control
2203 * @_template: control template
2204 * @data: control private data
2205 * @long_name: control long name
2206 * @prefix: control name prefix
2208 * Create a new mixer control from a template control.
2210 * Returns 0 for success, else error.
2212 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2213 void *data, const char *long_name,
2216 struct snd_kcontrol_new template;
2217 struct snd_kcontrol *kcontrol;
2220 memcpy(&template, _template, sizeof(template));
2224 long_name = template.name;
2227 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2231 template.name = name;
2233 template.name = long_name;
2236 kcontrol = snd_ctl_new1(&template, data);
2242 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2244 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2245 const struct snd_kcontrol_new *controls, int num_controls,
2246 const char *prefix, void *data)
2250 for (i = 0; i < num_controls; i++) {
2251 const struct snd_kcontrol_new *control = &controls[i];
2252 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2253 control->name, prefix));
2255 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2256 control->name, err);
2264 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2267 struct snd_card *card = soc_card->snd_card;
2268 struct snd_kcontrol *kctl;
2270 if (unlikely(!name))
2273 list_for_each_entry(kctl, &card->controls, list)
2274 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2278 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2281 * snd_soc_add_component_controls - Add an array of controls to a component.
2283 * @component: Component to add controls to
2284 * @controls: Array of controls to add
2285 * @num_controls: Number of elements in the array
2287 * Return: 0 for success, else error.
2289 int snd_soc_add_component_controls(struct snd_soc_component *component,
2290 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2292 struct snd_card *card = component->card->snd_card;
2294 return snd_soc_add_controls(card, component->dev, controls,
2295 num_controls, component->name_prefix, component);
2297 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2300 * snd_soc_add_codec_controls - add an array of controls to a codec.
2301 * Convenience function to add a list of controls. Many codecs were
2302 * duplicating this code.
2304 * @codec: codec to add controls to
2305 * @controls: array of controls to add
2306 * @num_controls: number of elements in the array
2308 * Return 0 for success, else error.
2310 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2311 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2313 return snd_soc_add_component_controls(&codec->component, controls,
2316 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2319 * snd_soc_add_platform_controls - add an array of controls to a platform.
2320 * Convenience function to add a list of controls.
2322 * @platform: platform to add controls to
2323 * @controls: array of controls to add
2324 * @num_controls: number of elements in the array
2326 * Return 0 for success, else error.
2328 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2329 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2331 return snd_soc_add_component_controls(&platform->component, controls,
2334 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2337 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2338 * Convenience function to add a list of controls.
2340 * @soc_card: SoC card to add controls to
2341 * @controls: array of controls to add
2342 * @num_controls: number of elements in the array
2344 * Return 0 for success, else error.
2346 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2347 const struct snd_kcontrol_new *controls, int num_controls)
2349 struct snd_card *card = soc_card->snd_card;
2351 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2354 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2357 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2358 * Convienience function to add a list of controls.
2360 * @dai: DAI to add controls to
2361 * @controls: array of controls to add
2362 * @num_controls: number of elements in the array
2364 * Return 0 for success, else error.
2366 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2367 const struct snd_kcontrol_new *controls, int num_controls)
2369 struct snd_card *card = dai->card->snd_card;
2371 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2374 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2377 * snd_soc_info_enum_double - enumerated double mixer info callback
2378 * @kcontrol: mixer control
2379 * @uinfo: control element information
2381 * Callback to provide information about a double enumerated
2384 * Returns 0 for success.
2386 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2387 struct snd_ctl_elem_info *uinfo)
2389 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2391 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2392 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2393 uinfo->value.enumerated.items = e->items;
2395 if (uinfo->value.enumerated.item >= e->items)
2396 uinfo->value.enumerated.item = e->items - 1;
2397 strlcpy(uinfo->value.enumerated.name,
2398 e->texts[uinfo->value.enumerated.item],
2399 sizeof(uinfo->value.enumerated.name));
2402 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2405 * snd_soc_get_enum_double - enumerated double mixer get callback
2406 * @kcontrol: mixer control
2407 * @ucontrol: control element information
2409 * Callback to get the value of a double enumerated mixer.
2411 * Returns 0 for success.
2413 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2414 struct snd_ctl_elem_value *ucontrol)
2416 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2417 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2418 unsigned int val, item;
2419 unsigned int reg_val;
2422 ret = snd_soc_component_read(component, e->reg, ®_val);
2425 val = (reg_val >> e->shift_l) & e->mask;
2426 item = snd_soc_enum_val_to_item(e, val);
2427 ucontrol->value.enumerated.item[0] = item;
2428 if (e->shift_l != e->shift_r) {
2429 val = (reg_val >> e->shift_l) & e->mask;
2430 item = snd_soc_enum_val_to_item(e, val);
2431 ucontrol->value.enumerated.item[1] = item;
2436 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2439 * snd_soc_put_enum_double - enumerated double mixer put callback
2440 * @kcontrol: mixer control
2441 * @ucontrol: control element information
2443 * Callback to set the value of a double enumerated mixer.
2445 * Returns 0 for success.
2447 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2448 struct snd_ctl_elem_value *ucontrol)
2450 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2451 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2452 unsigned int *item = ucontrol->value.enumerated.item;
2456 if (item[0] >= e->items)
2458 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
2459 mask = e->mask << e->shift_l;
2460 if (e->shift_l != e->shift_r) {
2461 if (item[1] >= e->items)
2463 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
2464 mask |= e->mask << e->shift_r;
2467 return snd_soc_component_update_bits(component, e->reg, mask, val);
2469 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2472 * snd_soc_read_signed - Read a codec register and interprete as signed value
2473 * @component: component
2474 * @reg: Register to read
2475 * @mask: Mask to use after shifting the register value
2476 * @shift: Right shift of register value
2477 * @sign_bit: Bit that describes if a number is negative or not.
2478 * @signed_val: Pointer to where the read value should be stored
2480 * This functions reads a codec register. The register value is shifted right
2481 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2482 * the given registervalue into a signed integer if sign_bit is non-zero.
2484 * Returns 0 on sucess, otherwise an error value
2486 static int snd_soc_read_signed(struct snd_soc_component *component,
2487 unsigned int reg, unsigned int mask, unsigned int shift,
2488 unsigned int sign_bit, int *signed_val)
2493 ret = snd_soc_component_read(component, reg, &val);
2497 val = (val >> shift) & mask;
2504 /* non-negative number */
2505 if (!(val & BIT(sign_bit))) {
2513 * The register most probably does not contain a full-sized int.
2514 * Instead we have an arbitrary number of bits in a signed
2515 * representation which has to be translated into a full-sized int.
2516 * This is done by filling up all bits above the sign-bit.
2518 ret |= ~((int)(BIT(sign_bit) - 1));
2526 * snd_soc_info_volsw - single mixer info callback
2527 * @kcontrol: mixer control
2528 * @uinfo: control element information
2530 * Callback to provide information about a single mixer control, or a double
2531 * mixer control that spans 2 registers.
2533 * Returns 0 for success.
2535 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2536 struct snd_ctl_elem_info *uinfo)
2538 struct soc_mixer_control *mc =
2539 (struct soc_mixer_control *)kcontrol->private_value;
2542 if (!mc->platform_max)
2543 mc->platform_max = mc->max;
2544 platform_max = mc->platform_max;
2546 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2547 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2549 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2551 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2552 uinfo->value.integer.min = 0;
2553 uinfo->value.integer.max = platform_max - mc->min;
2556 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2559 * snd_soc_get_volsw - single mixer get callback
2560 * @kcontrol: mixer control
2561 * @ucontrol: control element information
2563 * Callback to get the value of a single mixer control, or a double mixer
2564 * control that spans 2 registers.
2566 * Returns 0 for success.
2568 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2569 struct snd_ctl_elem_value *ucontrol)
2571 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2572 struct soc_mixer_control *mc =
2573 (struct soc_mixer_control *)kcontrol->private_value;
2574 unsigned int reg = mc->reg;
2575 unsigned int reg2 = mc->rreg;
2576 unsigned int shift = mc->shift;
2577 unsigned int rshift = mc->rshift;
2580 int sign_bit = mc->sign_bit;
2581 unsigned int mask = (1 << fls(max)) - 1;
2582 unsigned int invert = mc->invert;
2587 mask = BIT(sign_bit + 1) - 1;
2589 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2593 ucontrol->value.integer.value[0] = val - min;
2595 ucontrol->value.integer.value[0] =
2596 max - ucontrol->value.integer.value[0];
2598 if (snd_soc_volsw_is_stereo(mc)) {
2600 ret = snd_soc_read_signed(component, reg, mask, rshift,
2603 ret = snd_soc_read_signed(component, reg2, mask, shift,
2608 ucontrol->value.integer.value[1] = val - min;
2610 ucontrol->value.integer.value[1] =
2611 max - ucontrol->value.integer.value[1];
2616 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2619 * snd_soc_put_volsw - single mixer put callback
2620 * @kcontrol: mixer control
2621 * @ucontrol: control element information
2623 * Callback to set the value of a single mixer control, or a double mixer
2624 * control that spans 2 registers.
2626 * Returns 0 for success.
2628 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2629 struct snd_ctl_elem_value *ucontrol)
2631 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2632 struct soc_mixer_control *mc =
2633 (struct soc_mixer_control *)kcontrol->private_value;
2634 unsigned int reg = mc->reg;
2635 unsigned int reg2 = mc->rreg;
2636 unsigned int shift = mc->shift;
2637 unsigned int rshift = mc->rshift;
2640 unsigned int sign_bit = mc->sign_bit;
2641 unsigned int mask = (1 << fls(max)) - 1;
2642 unsigned int invert = mc->invert;
2644 bool type_2r = false;
2645 unsigned int val2 = 0;
2646 unsigned int val, val_mask;
2649 mask = BIT(sign_bit + 1) - 1;
2651 val = ((ucontrol->value.integer.value[0] + min) & mask);
2654 val_mask = mask << shift;
2656 if (snd_soc_volsw_is_stereo(mc)) {
2657 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
2661 val_mask |= mask << rshift;
2662 val |= val2 << rshift;
2664 val2 = val2 << shift;
2668 err = snd_soc_component_update_bits(component, reg, val_mask, val);
2673 err = snd_soc_component_update_bits(component, reg2, val_mask,
2678 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2681 * snd_soc_get_volsw_sx - single mixer get callback
2682 * @kcontrol: mixer control
2683 * @ucontrol: control element information
2685 * Callback to get the value of a single mixer control, or a double mixer
2686 * control that spans 2 registers.
2688 * Returns 0 for success.
2690 int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2691 struct snd_ctl_elem_value *ucontrol)
2693 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2694 struct soc_mixer_control *mc =
2695 (struct soc_mixer_control *)kcontrol->private_value;
2696 unsigned int reg = mc->reg;
2697 unsigned int reg2 = mc->rreg;
2698 unsigned int shift = mc->shift;
2699 unsigned int rshift = mc->rshift;
2702 int mask = (1 << (fls(min + max) - 1)) - 1;
2706 ret = snd_soc_component_read(component, reg, &val);
2710 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2712 if (snd_soc_volsw_is_stereo(mc)) {
2713 ret = snd_soc_component_read(component, reg2, &val);
2717 val = ((val >> rshift) - min) & mask;
2718 ucontrol->value.integer.value[1] = val;
2723 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2726 * snd_soc_put_volsw_sx - double mixer set callback
2727 * @kcontrol: mixer control
2728 * @uinfo: control element information
2730 * Callback to set the value of a double mixer control that spans 2 registers.
2732 * Returns 0 for success.
2734 int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2735 struct snd_ctl_elem_value *ucontrol)
2737 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2738 struct soc_mixer_control *mc =
2739 (struct soc_mixer_control *)kcontrol->private_value;
2741 unsigned int reg = mc->reg;
2742 unsigned int reg2 = mc->rreg;
2743 unsigned int shift = mc->shift;
2744 unsigned int rshift = mc->rshift;
2747 int mask = (1 << (fls(min + max) - 1)) - 1;
2749 unsigned int val, val_mask, val2 = 0;
2751 val_mask = mask << shift;
2752 val = (ucontrol->value.integer.value[0] + min) & mask;
2755 err = snd_soc_component_update_bits(component, reg, val_mask, val);
2759 if (snd_soc_volsw_is_stereo(mc)) {
2760 val_mask = mask << rshift;
2761 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2762 val2 = val2 << rshift;
2764 err = snd_soc_component_update_bits(component, reg2, val_mask,
2769 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2772 * snd_soc_info_volsw_s8 - signed mixer info callback
2773 * @kcontrol: mixer control
2774 * @uinfo: control element information
2776 * Callback to provide information about a signed mixer control.
2778 * Returns 0 for success.
2780 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2781 struct snd_ctl_elem_info *uinfo)
2783 struct soc_mixer_control *mc =
2784 (struct soc_mixer_control *)kcontrol->private_value;
2788 if (!mc->platform_max)
2789 mc->platform_max = mc->max;
2790 platform_max = mc->platform_max;
2792 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2794 uinfo->value.integer.min = 0;
2795 uinfo->value.integer.max = platform_max - min;
2798 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2801 * snd_soc_get_volsw_s8 - signed mixer get callback
2802 * @kcontrol: mixer control
2803 * @ucontrol: control element information
2805 * Callback to get the value of a signed mixer control.
2807 * Returns 0 for success.
2809 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2810 struct snd_ctl_elem_value *ucontrol)
2812 struct soc_mixer_control *mc =
2813 (struct soc_mixer_control *)kcontrol->private_value;
2814 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2815 unsigned int reg = mc->reg;
2820 ret = snd_soc_component_read(component, reg, &val);
2824 ucontrol->value.integer.value[0] =
2825 ((signed char)(val & 0xff))-min;
2826 ucontrol->value.integer.value[1] =
2827 ((signed char)((val >> 8) & 0xff))-min;
2830 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2833 * snd_soc_put_volsw_sgn - signed mixer put callback
2834 * @kcontrol: mixer control
2835 * @ucontrol: control element information
2837 * Callback to set the value of a signed mixer control.
2839 * Returns 0 for success.
2841 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2842 struct snd_ctl_elem_value *ucontrol)
2844 struct soc_mixer_control *mc =
2845 (struct soc_mixer_control *)kcontrol->private_value;
2846 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2847 unsigned int reg = mc->reg;
2851 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2852 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2854 return snd_soc_component_update_bits(component, reg, 0xffff, val);
2856 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2859 * snd_soc_info_volsw_range - single mixer info callback with range.
2860 * @kcontrol: mixer control
2861 * @uinfo: control element information
2863 * Callback to provide information, within a range, about a single
2866 * returns 0 for success.
2868 int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2869 struct snd_ctl_elem_info *uinfo)
2871 struct soc_mixer_control *mc =
2872 (struct soc_mixer_control *)kcontrol->private_value;
2876 if (!mc->platform_max)
2877 mc->platform_max = mc->max;
2878 platform_max = mc->platform_max;
2880 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2881 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2882 uinfo->value.integer.min = 0;
2883 uinfo->value.integer.max = platform_max - min;
2887 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2890 * snd_soc_put_volsw_range - single mixer put value callback with range.
2891 * @kcontrol: mixer control
2892 * @ucontrol: control element information
2894 * Callback to set the value, within a range, for a single mixer control.
2896 * Returns 0 for success.
2898 int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2899 struct snd_ctl_elem_value *ucontrol)
2901 struct soc_mixer_control *mc =
2902 (struct soc_mixer_control *)kcontrol->private_value;
2903 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2904 unsigned int reg = mc->reg;
2905 unsigned int rreg = mc->rreg;
2906 unsigned int shift = mc->shift;
2909 unsigned int mask = (1 << fls(max)) - 1;
2910 unsigned int invert = mc->invert;
2911 unsigned int val, val_mask;
2914 val = ((ucontrol->value.integer.value[0] + min) & mask);
2917 val_mask = mask << shift;
2920 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
2924 if (snd_soc_volsw_is_stereo(mc)) {
2925 val = ((ucontrol->value.integer.value[1] + min) & mask);
2928 val_mask = mask << shift;
2931 ret = snd_soc_component_update_bits(component, rreg, val_mask,
2937 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2940 * snd_soc_get_volsw_range - single mixer get callback with range
2941 * @kcontrol: mixer control
2942 * @ucontrol: control element information
2944 * Callback to get the value, within a range, of a single mixer control.
2946 * Returns 0 for success.
2948 int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2949 struct snd_ctl_elem_value *ucontrol)
2951 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2952 struct soc_mixer_control *mc =
2953 (struct soc_mixer_control *)kcontrol->private_value;
2954 unsigned int reg = mc->reg;
2955 unsigned int rreg = mc->rreg;
2956 unsigned int shift = mc->shift;
2959 unsigned int mask = (1 << fls(max)) - 1;
2960 unsigned int invert = mc->invert;
2964 ret = snd_soc_component_read(component, reg, &val);
2968 ucontrol->value.integer.value[0] = (val >> shift) & mask;
2970 ucontrol->value.integer.value[0] =
2971 max - ucontrol->value.integer.value[0];
2972 ucontrol->value.integer.value[0] =
2973 ucontrol->value.integer.value[0] - min;
2975 if (snd_soc_volsw_is_stereo(mc)) {
2976 ret = snd_soc_component_read(component, rreg, &val);
2980 ucontrol->value.integer.value[1] = (val >> shift) & mask;
2982 ucontrol->value.integer.value[1] =
2983 max - ucontrol->value.integer.value[1];
2984 ucontrol->value.integer.value[1] =
2985 ucontrol->value.integer.value[1] - min;
2990 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
2993 * snd_soc_limit_volume - Set new limit to an existing volume control.
2995 * @codec: where to look for the control
2996 * @name: Name of the control
2997 * @max: new maximum limit
2999 * Return 0 for success, else error.
3001 int snd_soc_limit_volume(struct snd_soc_codec *codec,
3002 const char *name, int max)
3004 struct snd_card *card = codec->component.card->snd_card;
3005 struct snd_kcontrol *kctl;
3006 struct soc_mixer_control *mc;
3010 /* Sanity check for name and max */
3011 if (unlikely(!name || max <= 0))
3014 list_for_each_entry(kctl, &card->controls, list) {
3015 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3021 mc = (struct soc_mixer_control *)kctl->private_value;
3022 if (max <= mc->max) {
3023 mc->platform_max = max;
3029 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3031 int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3032 struct snd_ctl_elem_info *uinfo)
3034 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3035 struct soc_bytes *params = (void *)kcontrol->private_value;
3037 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3038 uinfo->count = params->num_regs * component->val_bytes;
3042 EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3044 int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3045 struct snd_ctl_elem_value *ucontrol)
3047 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3048 struct soc_bytes *params = (void *)kcontrol->private_value;
3051 if (component->regmap)
3052 ret = regmap_raw_read(component->regmap, params->base,
3053 ucontrol->value.bytes.data,
3054 params->num_regs * component->val_bytes);
3058 /* Hide any masked bytes to ensure consistent data reporting */
3059 if (ret == 0 && params->mask) {
3060 switch (component->val_bytes) {
3062 ucontrol->value.bytes.data[0] &= ~params->mask;
3065 ((u16 *)(&ucontrol->value.bytes.data))[0]
3066 &= cpu_to_be16(~params->mask);
3069 ((u32 *)(&ucontrol->value.bytes.data))[0]
3070 &= cpu_to_be32(~params->mask);
3079 EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3081 int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3082 struct snd_ctl_elem_value *ucontrol)
3084 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3085 struct soc_bytes *params = (void *)kcontrol->private_value;
3087 unsigned int val, mask;
3090 if (!component->regmap)
3093 len = params->num_regs * component->val_bytes;
3095 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3100 * If we've got a mask then we need to preserve the register
3101 * bits. We shouldn't modify the incoming data so take a
3105 ret = regmap_read(component->regmap, params->base, &val);
3109 val &= params->mask;
3111 switch (component->val_bytes) {
3113 ((u8 *)data)[0] &= ~params->mask;
3114 ((u8 *)data)[0] |= val;
3117 mask = ~params->mask;
3118 ret = regmap_parse_val(component->regmap,
3123 ((u16 *)data)[0] &= mask;
3125 ret = regmap_parse_val(component->regmap,
3130 ((u16 *)data)[0] |= val;
3133 mask = ~params->mask;
3134 ret = regmap_parse_val(component->regmap,
3139 ((u32 *)data)[0] &= mask;
3141 ret = regmap_parse_val(component->regmap,
3146 ((u32 *)data)[0] |= val;
3154 ret = regmap_raw_write(component->regmap, params->base,
3162 EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3164 int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3165 struct snd_ctl_elem_info *ucontrol)
3167 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3169 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3170 ucontrol->count = params->max;
3174 EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3176 int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
3177 unsigned int size, unsigned int __user *tlv)
3179 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3180 unsigned int count = size < params->max ? size : params->max;
3184 case SNDRV_CTL_TLV_OP_READ:
3186 ret = params->get(tlv, count);
3188 case SNDRV_CTL_TLV_OP_WRITE:
3190 ret = params->put(tlv, count);
3195 EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
3198 * snd_soc_info_xr_sx - signed multi register info callback
3199 * @kcontrol: mreg control
3200 * @uinfo: control element information
3202 * Callback to provide information of a control that can
3203 * span multiple codec registers which together
3204 * forms a single signed value in a MSB/LSB manner.
3206 * Returns 0 for success.
3208 int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3209 struct snd_ctl_elem_info *uinfo)
3211 struct soc_mreg_control *mc =
3212 (struct soc_mreg_control *)kcontrol->private_value;
3213 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3215 uinfo->value.integer.min = mc->min;
3216 uinfo->value.integer.max = mc->max;
3220 EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3223 * snd_soc_get_xr_sx - signed multi register get callback
3224 * @kcontrol: mreg control
3225 * @ucontrol: control element information
3227 * Callback to get the value of a control that can span
3228 * multiple codec registers which together forms a single
3229 * signed value in a MSB/LSB manner. The control supports
3230 * specifying total no of bits used to allow for bitfields
3231 * across the multiple codec registers.
3233 * Returns 0 for success.
3235 int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3236 struct snd_ctl_elem_value *ucontrol)
3238 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3239 struct soc_mreg_control *mc =
3240 (struct soc_mreg_control *)kcontrol->private_value;
3241 unsigned int regbase = mc->regbase;
3242 unsigned int regcount = mc->regcount;
3243 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
3244 unsigned int regwmask = (1<<regwshift)-1;
3245 unsigned int invert = mc->invert;
3246 unsigned long mask = (1UL<<mc->nbits)-1;
3250 unsigned int regval;
3254 for (i = 0; i < regcount; i++) {
3255 ret = snd_soc_component_read(component, regbase+i, ®val);
3258 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
3261 if (min < 0 && val > max)
3265 ucontrol->value.integer.value[0] = val;
3269 EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3272 * snd_soc_put_xr_sx - signed multi register get callback
3273 * @kcontrol: mreg control
3274 * @ucontrol: control element information
3276 * Callback to set the value of a control that can span
3277 * multiple codec registers which together forms a single
3278 * signed value in a MSB/LSB manner. The control supports
3279 * specifying total no of bits used to allow for bitfields
3280 * across the multiple codec registers.
3282 * Returns 0 for success.
3284 int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3285 struct snd_ctl_elem_value *ucontrol)
3287 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3288 struct soc_mreg_control *mc =
3289 (struct soc_mreg_control *)kcontrol->private_value;
3290 unsigned int regbase = mc->regbase;
3291 unsigned int regcount = mc->regcount;
3292 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
3293 unsigned int regwmask = (1<<regwshift)-1;
3294 unsigned int invert = mc->invert;
3295 unsigned long mask = (1UL<<mc->nbits)-1;
3297 long val = ucontrol->value.integer.value[0];
3298 unsigned int i, regval, regmask;
3304 for (i = 0; i < regcount; i++) {
3305 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3306 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3307 err = snd_soc_component_update_bits(component, regbase+i,
3315 EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3318 * snd_soc_get_strobe - strobe get callback
3319 * @kcontrol: mixer control
3320 * @ucontrol: control element information
3322 * Callback get the value of a strobe mixer control.
3324 * Returns 0 for success.
3326 int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3327 struct snd_ctl_elem_value *ucontrol)
3329 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3330 struct soc_mixer_control *mc =
3331 (struct soc_mixer_control *)kcontrol->private_value;
3332 unsigned int reg = mc->reg;
3333 unsigned int shift = mc->shift;
3334 unsigned int mask = 1 << shift;
3335 unsigned int invert = mc->invert != 0;
3339 ret = snd_soc_component_read(component, reg, &val);
3345 if (shift != 0 && val != 0)
3347 ucontrol->value.enumerated.item[0] = val ^ invert;
3351 EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3354 * snd_soc_put_strobe - strobe put callback
3355 * @kcontrol: mixer control
3356 * @ucontrol: control element information
3358 * Callback strobe a register bit to high then low (or the inverse)
3359 * in one pass of a single mixer enum control.
3361 * Returns 1 for success.
3363 int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3364 struct snd_ctl_elem_value *ucontrol)
3366 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3367 struct soc_mixer_control *mc =
3368 (struct soc_mixer_control *)kcontrol->private_value;
3369 unsigned int reg = mc->reg;
3370 unsigned int shift = mc->shift;
3371 unsigned int mask = 1 << shift;
3372 unsigned int invert = mc->invert != 0;
3373 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3374 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3375 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3378 err = snd_soc_component_update_bits(component, reg, mask, val1);
3382 return snd_soc_component_update_bits(component, reg, mask, val2);
3384 EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3387 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3389 * @clk_id: DAI specific clock ID
3390 * @freq: new clock frequency in Hz
3391 * @dir: new clock direction - input/output.
3393 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3395 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3396 unsigned int freq, int dir)
3398 if (dai->driver && dai->driver->ops->set_sysclk)
3399 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3400 else if (dai->codec && dai->codec->driver->set_sysclk)
3401 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3406 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3409 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3411 * @clk_id: DAI specific clock ID
3412 * @source: Source for the clock
3413 * @freq: new clock frequency in Hz
3414 * @dir: new clock direction - input/output.
3416 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3418 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3419 int source, unsigned int freq, int dir)
3421 if (codec->driver->set_sysclk)
3422 return codec->driver->set_sysclk(codec, clk_id, source,
3427 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3430 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3432 * @div_id: DAI specific clock divider ID
3433 * @div: new clock divisor.
3435 * Configures the clock dividers. This is used to derive the best DAI bit and
3436 * frame clocks from the system or master clock. It's best to set the DAI bit
3437 * and frame clocks as low as possible to save system power.
3439 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3440 int div_id, int div)
3442 if (dai->driver && dai->driver->ops->set_clkdiv)
3443 return dai->driver->ops->set_clkdiv(dai, div_id, div);
3447 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3450 * snd_soc_dai_set_pll - configure DAI PLL.
3452 * @pll_id: DAI specific PLL ID
3453 * @source: DAI specific source for the PLL
3454 * @freq_in: PLL input clock frequency in Hz
3455 * @freq_out: requested PLL output clock frequency in Hz
3457 * Configures and enables PLL to generate output clock based on input clock.
3459 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3460 unsigned int freq_in, unsigned int freq_out)
3462 if (dai->driver && dai->driver->ops->set_pll)
3463 return dai->driver->ops->set_pll(dai, pll_id, source,
3465 else if (dai->codec && dai->codec->driver->set_pll)
3466 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3471 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3474 * snd_soc_codec_set_pll - configure codec PLL.
3476 * @pll_id: DAI specific PLL ID
3477 * @source: DAI specific source for the PLL
3478 * @freq_in: PLL input clock frequency in Hz
3479 * @freq_out: requested PLL output clock frequency in Hz
3481 * Configures and enables PLL to generate output clock based on input clock.
3483 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3484 unsigned int freq_in, unsigned int freq_out)
3486 if (codec->driver->set_pll)
3487 return codec->driver->set_pll(codec, pll_id, source,
3492 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3495 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3497 * @ratio Ratio of BCLK to Sample rate.
3499 * Configures the DAI for a preset BCLK to sample rate ratio.
3501 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3503 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3504 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3508 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3511 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3513 * @fmt: SND_SOC_DAIFMT_ format value.
3515 * Configures the DAI hardware format and clocking.
3517 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3519 if (dai->driver == NULL)
3521 if (dai->driver->ops->set_fmt == NULL)
3523 return dai->driver->ops->set_fmt(dai, fmt);
3525 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3528 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
3529 * @slots: Number of slots in use.
3530 * @tx_mask: bitmask representing active TX slots.
3531 * @rx_mask: bitmask representing active RX slots.
3533 * Generates the TDM tx and rx slot default masks for DAI.
3535 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
3536 unsigned int *tx_mask,
3537 unsigned int *rx_mask)
3539 if (*tx_mask || *rx_mask)
3545 *tx_mask = (1 << slots) - 1;
3546 *rx_mask = (1 << slots) - 1;
3552 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3554 * @tx_mask: bitmask representing active TX slots.
3555 * @rx_mask: bitmask representing active RX slots.
3556 * @slots: Number of slots in use.
3557 * @slot_width: Width in bits for each slot.
3559 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3562 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3563 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
3565 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3566 dai->driver->ops->xlate_tdm_slot_mask(slots,
3567 &tx_mask, &rx_mask);
3569 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
3571 dai->tx_mask = tx_mask;
3572 dai->rx_mask = rx_mask;
3574 if (dai->driver && dai->driver->ops->set_tdm_slot)
3575 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3580 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3583 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3585 * @tx_num: how many TX channels
3586 * @tx_slot: pointer to an array which imply the TX slot number channel
3588 * @rx_num: how many RX channels
3589 * @rx_slot: pointer to an array which imply the RX slot number channel
3592 * configure the relationship between channel number and TDM slot number.
3594 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3595 unsigned int tx_num, unsigned int *tx_slot,
3596 unsigned int rx_num, unsigned int *rx_slot)
3598 if (dai->driver && dai->driver->ops->set_channel_map)
3599 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3604 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3607 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3609 * @tristate: tristate enable
3611 * Tristates the DAI so that others can use it.
3613 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3615 if (dai->driver && dai->driver->ops->set_tristate)
3616 return dai->driver->ops->set_tristate(dai, tristate);
3620 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3623 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3625 * @mute: mute enable
3626 * @direction: stream to mute
3628 * Mutes the DAI DAC.
3630 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3636 if (dai->driver->ops->mute_stream)
3637 return dai->driver->ops->mute_stream(dai, mute, direction);
3638 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3639 dai->driver->ops->digital_mute)
3640 return dai->driver->ops->digital_mute(dai, mute);
3644 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3646 static int snd_soc_init_multicodec(struct snd_soc_card *card,
3647 struct snd_soc_dai_link *dai_link)
3649 /* Legacy codec/codec_dai link is a single entry in multicodec */
3650 if (dai_link->codec_name || dai_link->codec_of_node ||
3651 dai_link->codec_dai_name) {
3652 dai_link->num_codecs = 1;
3654 dai_link->codecs = devm_kzalloc(card->dev,
3655 sizeof(struct snd_soc_dai_link_component),
3657 if (!dai_link->codecs)
3660 dai_link->codecs[0].name = dai_link->codec_name;
3661 dai_link->codecs[0].of_node = dai_link->codec_of_node;
3662 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
3665 if (!dai_link->codecs) {
3666 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
3674 * snd_soc_register_card - Register a card with the ASoC core
3676 * @card: Card to register
3679 int snd_soc_register_card(struct snd_soc_card *card)
3683 if (!card->name || !card->dev)
3686 for (i = 0; i < card->num_links; i++) {
3687 struct snd_soc_dai_link *link = &card->dai_link[i];
3689 ret = snd_soc_init_multicodec(card, link);
3691 dev_err(card->dev, "ASoC: failed to init multicodec\n");
3695 for (j = 0; j < link->num_codecs; j++) {
3697 * Codec must be specified by 1 of name or OF node,
3698 * not both or neither.
3700 if (!!link->codecs[j].name ==
3701 !!link->codecs[j].of_node) {
3702 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
3706 /* Codec DAI name must be specified */
3707 if (!link->codecs[j].dai_name) {
3708 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
3715 * Platform may be specified by either name or OF node, but
3716 * can be left unspecified, and a dummy platform will be used.
3718 if (link->platform_name && link->platform_of_node) {
3720 "ASoC: Both platform name/of_node are set for %s\n",
3726 * CPU device may be specified by either name or OF node, but
3727 * can be left unspecified, and will be matched based on DAI
3730 if (link->cpu_name && link->cpu_of_node) {
3732 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3737 * At least one of CPU DAI name or CPU device name/node must be
3740 if (!link->cpu_dai_name &&
3741 !(link->cpu_name || link->cpu_of_node)) {
3743 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3749 dev_set_drvdata(card->dev, card);
3751 snd_soc_initialize_card_lists(card);
3753 soc_init_card_debugfs(card);
3755 card->rtd = devm_kzalloc(card->dev,
3756 sizeof(struct snd_soc_pcm_runtime) *
3757 (card->num_links + card->num_aux_devs),
3759 if (card->rtd == NULL)
3762 card->rtd_aux = &card->rtd[card->num_links];
3764 for (i = 0; i < card->num_links; i++) {
3765 card->rtd[i].card = card;
3766 card->rtd[i].dai_link = &card->dai_link[i];
3767 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
3768 sizeof(struct snd_soc_dai *) *
3769 (card->rtd[i].dai_link->num_codecs),
3771 if (card->rtd[i].codec_dais == NULL)
3775 for (i = 0; i < card->num_aux_devs; i++)
3776 card->rtd_aux[i].card = card;
3778 INIT_LIST_HEAD(&card->dapm_dirty);
3779 card->instantiated = 0;
3780 mutex_init(&card->mutex);
3781 mutex_init(&card->dapm_mutex);
3783 ret = snd_soc_instantiate_card(card);
3785 soc_cleanup_card_debugfs(card);
3787 /* deactivate pins to sleep state */
3788 for (i = 0; i < card->num_rtd; i++) {
3789 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
3790 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3793 for (j = 0; j < rtd->num_codecs; j++) {
3794 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
3795 if (!codec_dai->active)
3796 pinctrl_pm_select_sleep_state(codec_dai->dev);
3799 if (!cpu_dai->active)
3800 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3805 EXPORT_SYMBOL_GPL(snd_soc_register_card);
3808 * snd_soc_unregister_card - Unregister a card with the ASoC core
3810 * @card: Card to unregister
3813 int snd_soc_unregister_card(struct snd_soc_card *card)
3815 if (card->instantiated)
3816 soc_cleanup_card_resources(card);
3817 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
3821 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3824 * Simplify DAI link configuration by removing ".-1" from device names
3825 * and sanitizing names.
3827 static char *fmt_single_name(struct device *dev, int *id)
3829 char *found, name[NAME_SIZE];
3832 if (dev_name(dev) == NULL)
3835 strlcpy(name, dev_name(dev), NAME_SIZE);
3837 /* are we a "%s.%d" name (platform and SPI components) */
3838 found = strstr(name, dev->driver->name);
3841 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3843 /* discard ID from name if ID == -1 */
3845 found[strlen(dev->driver->name)] = '\0';
3849 /* I2C component devices are named "bus-addr" */
3850 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3851 char tmp[NAME_SIZE];
3853 /* create unique ID number from I2C addr and bus */
3854 *id = ((id1 & 0xffff) << 16) + id2;
3856 /* sanitize component name for DAI link creation */
3857 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3858 strlcpy(name, tmp, NAME_SIZE);
3863 return kstrdup(name, GFP_KERNEL);
3867 * Simplify DAI link naming for single devices with multiple DAIs by removing
3868 * any ".-1" and using the DAI name (instead of device name).
3870 static inline char *fmt_multiple_name(struct device *dev,
3871 struct snd_soc_dai_driver *dai_drv)
3873 if (dai_drv->name == NULL) {
3875 "ASoC: error - multiple DAI %s registered with no name\n",
3880 return kstrdup(dai_drv->name, GFP_KERNEL);
3884 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
3886 * @component: The component for which the DAIs should be unregistered
3888 static void snd_soc_unregister_dais(struct snd_soc_component *component)
3890 struct snd_soc_dai *dai, *_dai;
3892 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
3893 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3895 list_del(&dai->list);
3902 * snd_soc_register_dais - Register a DAI with the ASoC core
3904 * @component: The component the DAIs are registered for
3905 * @dai_drv: DAI driver to use for the DAIs
3906 * @count: Number of DAIs
3907 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3910 static int snd_soc_register_dais(struct snd_soc_component *component,
3911 struct snd_soc_dai_driver *dai_drv, size_t count,
3912 bool legacy_dai_naming)
3914 struct device *dev = component->dev;
3915 struct snd_soc_dai *dai;
3919 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
3921 component->dai_drv = dai_drv;
3922 component->num_dai = count;
3924 for (i = 0; i < count; i++) {
3926 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3933 * Back in the old days when we still had component-less DAIs,
3934 * instead of having a static name, component-less DAIs would
3935 * inherit the name of the parent device so it is possible to
3936 * register multiple instances of the DAI. We still need to keep
3937 * the same naming style even though those DAIs are not
3938 * component-less anymore.
3940 if (count == 1 && legacy_dai_naming) {
3941 dai->name = fmt_single_name(dev, &dai->id);
3943 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3945 dai->id = dai_drv[i].id;
3949 if (dai->name == NULL) {
3955 dai->component = component;
3957 dai->driver = &dai_drv[i];
3958 if (!dai->driver->ops)
3959 dai->driver->ops = &null_dai_ops;
3961 list_add(&dai->list, &component->dai_list);
3963 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3969 snd_soc_unregister_dais(component);
3974 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3975 enum snd_soc_dapm_type type, int subseq)
3977 struct snd_soc_component *component = dapm->component;
3979 component->driver->seq_notifier(component, type, subseq);
3982 static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3985 struct snd_soc_component *component = dapm->component;
3987 return component->driver->stream_event(component, event);
3990 static int snd_soc_component_initialize(struct snd_soc_component *component,
3991 const struct snd_soc_component_driver *driver, struct device *dev)
3993 struct snd_soc_dapm_context *dapm;
3995 component->name = fmt_single_name(dev, &component->id);
3996 if (!component->name) {
3997 dev_err(dev, "ASoC: Failed to allocate name\n");
4001 component->dev = dev;
4002 component->driver = driver;
4003 component->probe = component->driver->probe;
4004 component->remove = component->driver->remove;
4006 if (!component->dapm_ptr)
4007 component->dapm_ptr = &component->dapm;
4009 dapm = component->dapm_ptr;
4011 dapm->component = component;
4012 dapm->bias_level = SND_SOC_BIAS_OFF;
4013 dapm->idle_bias_off = true;
4014 if (driver->seq_notifier)
4015 dapm->seq_notifier = snd_soc_component_seq_notifier;
4016 if (driver->stream_event)
4017 dapm->stream_event = snd_soc_component_stream_event;
4019 component->controls = driver->controls;
4020 component->num_controls = driver->num_controls;
4021 component->dapm_widgets = driver->dapm_widgets;
4022 component->num_dapm_widgets = driver->num_dapm_widgets;
4023 component->dapm_routes = driver->dapm_routes;
4024 component->num_dapm_routes = driver->num_dapm_routes;
4026 INIT_LIST_HEAD(&component->dai_list);
4027 mutex_init(&component->io_mutex);
4032 static void snd_soc_component_init_regmap(struct snd_soc_component *component)
4034 if (!component->regmap)
4035 component->regmap = dev_get_regmap(component->dev, NULL);
4036 if (component->regmap) {
4037 int val_bytes = regmap_get_val_bytes(component->regmap);
4038 /* Errors are legitimate for non-integer byte multiples */
4040 component->val_bytes = val_bytes;
4044 static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
4046 if (!component->write && !component->read)
4047 snd_soc_component_init_regmap(component);
4049 list_add(&component->list, &component_list);
4052 static void snd_soc_component_add(struct snd_soc_component *component)
4054 mutex_lock(&client_mutex);
4055 snd_soc_component_add_unlocked(component);
4056 mutex_unlock(&client_mutex);
4059 static void snd_soc_component_cleanup(struct snd_soc_component *component)
4061 snd_soc_unregister_dais(component);
4062 kfree(component->name);
4065 static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
4067 list_del(&component->list);
4070 static void snd_soc_component_del(struct snd_soc_component *component)
4072 mutex_lock(&client_mutex);
4073 snd_soc_component_del_unlocked(component);
4074 mutex_unlock(&client_mutex);
4077 int snd_soc_register_component(struct device *dev,
4078 const struct snd_soc_component_driver *cmpnt_drv,
4079 struct snd_soc_dai_driver *dai_drv,
4082 struct snd_soc_component *cmpnt;
4085 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
4087 dev_err(dev, "ASoC: Failed to allocate memory\n");
4091 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
4095 cmpnt->ignore_pmdown_time = true;
4096 cmpnt->registered_as_component = true;
4098 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
4100 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4104 snd_soc_component_add(cmpnt);
4109 snd_soc_component_cleanup(cmpnt);
4114 EXPORT_SYMBOL_GPL(snd_soc_register_component);
4117 * snd_soc_unregister_component - Unregister a component from the ASoC core
4120 void snd_soc_unregister_component(struct device *dev)
4122 struct snd_soc_component *cmpnt;
4124 list_for_each_entry(cmpnt, &component_list, list) {
4125 if (dev == cmpnt->dev && cmpnt->registered_as_component)
4131 snd_soc_component_del(cmpnt);
4132 snd_soc_component_cleanup(cmpnt);
4135 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4137 static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
4139 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4141 return platform->driver->probe(platform);
4144 static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
4146 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4148 platform->driver->remove(platform);
4152 * snd_soc_add_platform - Add a platform to the ASoC core
4153 * @dev: The parent device for the platform
4154 * @platform: The platform to add
4155 * @platform_driver: The driver for the platform
4157 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
4158 const struct snd_soc_platform_driver *platform_drv)
4162 ret = snd_soc_component_initialize(&platform->component,
4163 &platform_drv->component_driver, dev);
4167 platform->dev = dev;
4168 platform->driver = platform_drv;
4169 if (platform_drv->controls) {
4170 platform->component.controls = platform_drv->controls;
4171 platform->component.num_controls = platform_drv->num_controls;
4173 if (platform_drv->dapm_widgets) {
4174 platform->component.dapm_widgets = platform_drv->dapm_widgets;
4175 platform->component.num_dapm_widgets = platform_drv->num_dapm_widgets;
4176 platform->component.steal_sibling_dai_widgets = true;
4178 if (platform_drv->dapm_routes) {
4179 platform->component.dapm_routes = platform_drv->dapm_routes;
4180 platform->component.num_dapm_routes = platform_drv->num_dapm_routes;
4183 if (platform_drv->probe)
4184 platform->component.probe = snd_soc_platform_drv_probe;
4185 if (platform_drv->remove)
4186 platform->component.remove = snd_soc_platform_drv_remove;
4188 #ifdef CONFIG_DEBUG_FS
4189 platform->component.debugfs_prefix = "platform";
4192 mutex_lock(&client_mutex);
4193 snd_soc_component_add_unlocked(&platform->component);
4194 list_add(&platform->list, &platform_list);
4195 mutex_unlock(&client_mutex);
4197 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
4198 platform->component.name);
4202 EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4205 * snd_soc_register_platform - Register a platform with the ASoC core
4207 * @platform: platform to register
4209 int snd_soc_register_platform(struct device *dev,
4210 const struct snd_soc_platform_driver *platform_drv)
4212 struct snd_soc_platform *platform;
4215 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4217 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4218 if (platform == NULL)
4221 ret = snd_soc_add_platform(dev, platform, platform_drv);
4227 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4230 * snd_soc_remove_platform - Remove a platform from the ASoC core
4231 * @platform: the platform to remove
4233 void snd_soc_remove_platform(struct snd_soc_platform *platform)
4236 mutex_lock(&client_mutex);
4237 list_del(&platform->list);
4238 snd_soc_component_del_unlocked(&platform->component);
4239 mutex_unlock(&client_mutex);
4241 snd_soc_component_cleanup(&platform->component);
4243 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
4244 platform->component.name);
4246 EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4248 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4250 struct snd_soc_platform *platform;
4252 list_for_each_entry(platform, &platform_list, list) {
4253 if (dev == platform->dev)
4259 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4262 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4264 * @platform: platform to unregister
4266 void snd_soc_unregister_platform(struct device *dev)
4268 struct snd_soc_platform *platform;
4270 platform = snd_soc_lookup_platform(dev);
4274 snd_soc_remove_platform(platform);
4277 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4279 static u64 codec_format_map[] = {
4280 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4281 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4282 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4283 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4284 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4285 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4286 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4287 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4288 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4289 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4290 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4291 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4292 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4293 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4294 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4295 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4298 /* Fix up the DAI formats for endianness: codecs don't actually see
4299 * the endianness of the data but we're using the CPU format
4300 * definitions which do need to include endianness so we ensure that
4301 * codec DAIs always have both big and little endian variants set.
4303 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4307 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4308 if (stream->formats & codec_format_map[i])
4309 stream->formats |= codec_format_map[i];
4312 static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
4314 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4316 return codec->driver->probe(codec);
4319 static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
4321 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4323 codec->driver->remove(codec);
4326 static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4327 unsigned int reg, unsigned int val)
4329 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4331 return codec->driver->write(codec, reg, val);
4334 static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4335 unsigned int reg, unsigned int *val)
4337 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4339 *val = codec->driver->read(codec, reg);
4344 static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
4345 enum snd_soc_bias_level level)
4347 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
4349 return codec->driver->set_bias_level(codec, level);
4353 * snd_soc_register_codec - Register a codec with the ASoC core
4355 * @codec: codec to register
4357 int snd_soc_register_codec(struct device *dev,
4358 const struct snd_soc_codec_driver *codec_drv,
4359 struct snd_soc_dai_driver *dai_drv,
4362 struct snd_soc_codec *codec;
4363 struct snd_soc_dai *dai;
4366 dev_dbg(dev, "codec register %s\n", dev_name(dev));
4368 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4372 codec->component.dapm_ptr = &codec->dapm;
4373 codec->component.codec = codec;
4375 ret = snd_soc_component_initialize(&codec->component,
4376 &codec_drv->component_driver, dev);
4380 if (codec_drv->controls) {
4381 codec->component.controls = codec_drv->controls;
4382 codec->component.num_controls = codec_drv->num_controls;
4384 if (codec_drv->dapm_widgets) {
4385 codec->component.dapm_widgets = codec_drv->dapm_widgets;
4386 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
4388 if (codec_drv->dapm_routes) {
4389 codec->component.dapm_routes = codec_drv->dapm_routes;
4390 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
4393 if (codec_drv->probe)
4394 codec->component.probe = snd_soc_codec_drv_probe;
4395 if (codec_drv->remove)
4396 codec->component.remove = snd_soc_codec_drv_remove;
4397 if (codec_drv->write)
4398 codec->component.write = snd_soc_codec_drv_write;
4399 if (codec_drv->read)
4400 codec->component.read = snd_soc_codec_drv_read;
4401 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
4402 codec->dapm.codec = codec;
4403 codec->dapm.idle_bias_off = codec_drv->idle_bias_off;
4404 if (codec_drv->seq_notifier)
4405 codec->dapm.seq_notifier = codec_drv->seq_notifier;
4406 if (codec_drv->set_bias_level)
4407 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
4409 codec->driver = codec_drv;
4410 codec->component.val_bytes = codec_drv->reg_word_size;
4411 mutex_init(&codec->mutex);
4413 #ifdef CONFIG_DEBUG_FS
4414 codec->component.init_debugfs = soc_init_codec_debugfs;
4415 codec->component.debugfs_prefix = "codec";
4418 if (codec_drv->get_regmap)
4419 codec->component.regmap = codec_drv->get_regmap(dev);
4421 for (i = 0; i < num_dai; i++) {
4422 fixup_codec_formats(&dai_drv[i].playback);
4423 fixup_codec_formats(&dai_drv[i].capture);
4426 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4428 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4432 list_for_each_entry(dai, &codec->component.dai_list, list)
4435 mutex_lock(&client_mutex);
4436 snd_soc_component_add_unlocked(&codec->component);
4437 list_add(&codec->list, &codec_list);
4438 mutex_unlock(&client_mutex);
4440 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4441 codec->component.name);
4445 snd_soc_component_cleanup(&codec->component);
4450 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4453 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4455 * @codec: codec to unregister
4457 void snd_soc_unregister_codec(struct device *dev)
4459 struct snd_soc_codec *codec;
4461 list_for_each_entry(codec, &codec_list, list) {
4462 if (dev == codec->dev)
4469 mutex_lock(&client_mutex);
4470 list_del(&codec->list);
4471 snd_soc_component_del_unlocked(&codec->component);
4472 mutex_unlock(&client_mutex);
4474 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4475 codec->component.name);
4477 snd_soc_component_cleanup(&codec->component);
4478 snd_soc_cache_exit(codec);
4481 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4483 /* Retrieve a card's name from device tree */
4484 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4485 const char *propname)
4487 struct device_node *np;
4491 pr_err("card->dev is not set before calling %s\n", __func__);
4495 np = card->dev->of_node;
4497 ret = of_property_read_string_index(np, propname, 0, &card->name);
4499 * EINVAL means the property does not exist. This is fine providing
4500 * card->name was previously set, which is checked later in
4501 * snd_soc_register_card.
4503 if (ret < 0 && ret != -EINVAL) {
4505 "ASoC: Property '%s' could not be read: %d\n",
4512 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4514 static const struct snd_soc_dapm_widget simple_widgets[] = {
4515 SND_SOC_DAPM_MIC("Microphone", NULL),
4516 SND_SOC_DAPM_LINE("Line", NULL),
4517 SND_SOC_DAPM_HP("Headphone", NULL),
4518 SND_SOC_DAPM_SPK("Speaker", NULL),
4521 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4522 const char *propname)
4524 struct device_node *np = card->dev->of_node;
4525 struct snd_soc_dapm_widget *widgets;
4526 const char *template, *wname;
4527 int i, j, num_widgets, ret;
4529 num_widgets = of_property_count_strings(np, propname);
4530 if (num_widgets < 0) {
4532 "ASoC: Property '%s' does not exist\n", propname);
4535 if (num_widgets & 1) {
4537 "ASoC: Property '%s' length is not even\n", propname);
4543 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4548 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4552 "ASoC: Could not allocate memory for widgets\n");
4556 for (i = 0; i < num_widgets; i++) {
4557 ret = of_property_read_string_index(np, propname,
4561 "ASoC: Property '%s' index %d read error:%d\n",
4562 propname, 2 * i, ret);
4566 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4567 if (!strncmp(template, simple_widgets[j].name,
4568 strlen(simple_widgets[j].name))) {
4569 widgets[i] = simple_widgets[j];
4574 if (j >= ARRAY_SIZE(simple_widgets)) {
4576 "ASoC: DAPM widget '%s' is not supported\n",
4581 ret = of_property_read_string_index(np, propname,
4586 "ASoC: Property '%s' index %d read error:%d\n",
4587 propname, (2 * i) + 1, ret);
4591 widgets[i].name = wname;
4594 card->dapm_widgets = widgets;
4595 card->num_dapm_widgets = num_widgets;
4599 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4601 int snd_soc_of_parse_tdm_slot(struct device_node *np,
4602 unsigned int *slots,
4603 unsigned int *slot_width)
4608 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4609 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4617 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4618 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4628 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4630 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4631 const char *propname)
4633 struct device_node *np = card->dev->of_node;
4635 struct snd_soc_dapm_route *routes;
4638 num_routes = of_property_count_strings(np, propname);
4639 if (num_routes < 0 || num_routes & 1) {
4641 "ASoC: Property '%s' does not exist or its length is not even\n",
4647 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4652 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4656 "ASoC: Could not allocate DAPM route table\n");
4660 for (i = 0; i < num_routes; i++) {
4661 ret = of_property_read_string_index(np, propname,
4662 2 * i, &routes[i].sink);
4665 "ASoC: Property '%s' index %d could not be read: %d\n",
4666 propname, 2 * i, ret);
4669 ret = of_property_read_string_index(np, propname,
4670 (2 * i) + 1, &routes[i].source);
4673 "ASoC: Property '%s' index %d could not be read: %d\n",
4674 propname, (2 * i) + 1, ret);
4679 card->num_dapm_routes = num_routes;
4680 card->dapm_routes = routes;
4684 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4686 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4688 struct device_node **bitclkmaster,
4689 struct device_node **framemaster)
4693 unsigned int format = 0;
4699 } of_fmt_table[] = {
4700 { "i2s", SND_SOC_DAIFMT_I2S },
4701 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4702 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4703 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4704 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4705 { "ac97", SND_SOC_DAIFMT_AC97 },
4706 { "pdm", SND_SOC_DAIFMT_PDM},
4707 { "msb", SND_SOC_DAIFMT_MSB },
4708 { "lsb", SND_SOC_DAIFMT_LSB },
4715 * check "[prefix]format = xxx"
4716 * SND_SOC_DAIFMT_FORMAT_MASK area
4718 snprintf(prop, sizeof(prop), "%sformat", prefix);
4719 ret = of_property_read_string(np, prop, &str);
4721 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4722 if (strcmp(str, of_fmt_table[i].name) == 0) {
4723 format |= of_fmt_table[i].val;
4730 * check "[prefix]continuous-clock"
4731 * SND_SOC_DAIFMT_CLOCK_MASK area
4733 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4734 if (of_get_property(np, prop, NULL))
4735 format |= SND_SOC_DAIFMT_CONT;
4737 format |= SND_SOC_DAIFMT_GATED;
4740 * check "[prefix]bitclock-inversion"
4741 * check "[prefix]frame-inversion"
4742 * SND_SOC_DAIFMT_INV_MASK area
4744 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4745 bit = !!of_get_property(np, prop, NULL);
4747 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4748 frame = !!of_get_property(np, prop, NULL);
4750 switch ((bit << 4) + frame) {
4752 format |= SND_SOC_DAIFMT_IB_IF;
4755 format |= SND_SOC_DAIFMT_IB_NF;
4758 format |= SND_SOC_DAIFMT_NB_IF;
4761 /* SND_SOC_DAIFMT_NB_NF is default */
4766 * check "[prefix]bitclock-master"
4767 * check "[prefix]frame-master"
4768 * SND_SOC_DAIFMT_MASTER_MASK area
4770 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4771 bit = !!of_get_property(np, prop, NULL);
4772 if (bit && bitclkmaster)
4773 *bitclkmaster = of_parse_phandle(np, prop, 0);
4775 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4776 frame = !!of_get_property(np, prop, NULL);
4777 if (frame && framemaster)
4778 *framemaster = of_parse_phandle(np, prop, 0);
4780 switch ((bit << 4) + frame) {
4782 format |= SND_SOC_DAIFMT_CBM_CFM;
4785 format |= SND_SOC_DAIFMT_CBM_CFS;
4788 format |= SND_SOC_DAIFMT_CBS_CFM;
4791 format |= SND_SOC_DAIFMT_CBS_CFS;
4797 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4799 int snd_soc_of_get_dai_name(struct device_node *of_node,
4800 const char **dai_name)
4802 struct snd_soc_component *pos;
4803 struct of_phandle_args args;
4806 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4807 "#sound-dai-cells", 0, &args);
4811 ret = -EPROBE_DEFER;
4813 mutex_lock(&client_mutex);
4814 list_for_each_entry(pos, &component_list, list) {
4815 if (pos->dev->of_node != args.np)
4818 if (pos->driver->of_xlate_dai_name) {
4819 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4823 switch (args.args_count) {
4825 id = 0; /* same as dai_drv[0] */
4835 if (id < 0 || id >= pos->num_dai) {
4842 *dai_name = pos->dai_drv[id].name;
4844 *dai_name = pos->name;
4849 mutex_unlock(&client_mutex);
4851 of_node_put(args.np);
4855 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4857 static int __init snd_soc_init(void)
4859 #ifdef CONFIG_DEBUG_FS
4860 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4861 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
4862 pr_warn("ASoC: Failed to create debugfs directory\n");
4863 snd_soc_debugfs_root = NULL;
4866 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4868 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4870 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4872 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
4874 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
4875 &platform_list_fops))
4876 pr_warn("ASoC: Failed to create platform list debugfs file\n");
4879 snd_soc_util_init();
4881 return platform_driver_register(&soc_driver);
4883 module_init(snd_soc_init);
4885 static void __exit snd_soc_exit(void)
4887 snd_soc_util_exit();
4889 #ifdef CONFIG_DEBUG_FS
4890 debugfs_remove_recursive(snd_soc_debugfs_root);
4892 platform_driver_unregister(&soc_driver);
4894 module_exit(snd_soc_exit);
4896 /* Module information */
4897 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4898 MODULE_DESCRIPTION("ALSA SoC Core");
4899 MODULE_LICENSE("GPL");
4900 MODULE_ALIAS("platform:soc-audio");