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 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
313 codec->component.debugfs_root,
314 codec, &codec_reg_fops);
315 if (!codec->debugfs_reg)
317 "ASoC: Failed to create codec register debugfs file\n");
320 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
321 size_t count, loff_t *ppos)
323 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
324 ssize_t len, ret = 0;
325 struct snd_soc_codec *codec;
330 list_for_each_entry(codec, &codec_list, list) {
331 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
332 codec->component.name);
335 if (ret > PAGE_SIZE) {
342 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
349 static const struct file_operations codec_list_fops = {
350 .read = codec_list_read_file,
351 .llseek = default_llseek,/* read accesses f_pos */
354 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
355 size_t count, loff_t *ppos)
357 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
358 ssize_t len, ret = 0;
359 struct snd_soc_component *component;
360 struct snd_soc_dai *dai;
365 list_for_each_entry(component, &component_list, list) {
366 list_for_each_entry(dai, &component->dai_list, list) {
367 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
371 if (ret > PAGE_SIZE) {
378 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
385 static const struct file_operations dai_list_fops = {
386 .read = dai_list_read_file,
387 .llseek = default_llseek,/* read accesses f_pos */
390 static ssize_t platform_list_read_file(struct file *file,
391 char __user *user_buf,
392 size_t count, loff_t *ppos)
394 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
395 ssize_t len, ret = 0;
396 struct snd_soc_platform *platform;
401 list_for_each_entry(platform, &platform_list, list) {
402 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
403 platform->component.name);
406 if (ret > PAGE_SIZE) {
412 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
419 static const struct file_operations platform_list_fops = {
420 .read = platform_list_read_file,
421 .llseek = default_llseek,/* read accesses f_pos */
424 static void soc_init_card_debugfs(struct snd_soc_card *card)
426 card->debugfs_card_root = debugfs_create_dir(card->name,
427 snd_soc_debugfs_root);
428 if (!card->debugfs_card_root) {
430 "ASoC: Failed to create card debugfs directory\n");
434 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
435 card->debugfs_card_root,
437 if (!card->debugfs_pop_time)
439 "ASoC: Failed to create pop time debugfs file\n");
442 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
444 debugfs_remove_recursive(card->debugfs_card_root);
449 #define soc_init_codec_debugfs NULL
451 static inline void soc_init_component_debugfs(
452 struct snd_soc_component *component)
456 static inline void soc_cleanup_component_debugfs(
457 struct snd_soc_component *component)
461 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
465 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
470 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
471 const char *dai_link, int stream)
475 for (i = 0; i < card->num_links; i++) {
476 if (card->rtd[i].dai_link->no_pcm &&
477 !strcmp(card->rtd[i].dai_link->name, dai_link))
478 return card->rtd[i].pcm->streams[stream].substream;
480 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
483 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
485 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
486 const char *dai_link)
490 for (i = 0; i < card->num_links; i++) {
491 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
492 return &card->rtd[i];
494 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
497 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
499 #ifdef CONFIG_SND_SOC_AC97_BUS
500 /* unregister ac97 codec */
501 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
503 if (codec->ac97->dev.bus)
504 device_unregister(&codec->ac97->dev);
508 /* stop no dev release warning */
509 static void soc_ac97_device_release(struct device *dev){}
511 /* register ac97 codec to bus */
512 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
516 codec->ac97->dev.bus = &ac97_bus_type;
517 codec->ac97->dev.parent = codec->component.card->dev;
518 codec->ac97->dev.release = soc_ac97_device_release;
520 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
521 codec->component.card->snd_card->number, 0,
522 codec->component.name);
523 err = device_register(&codec->ac97->dev);
525 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
526 codec->ac97->dev.bus = NULL;
533 static void codec2codec_close_delayed_work(struct work_struct *work)
535 /* Currently nothing to do for c2c links
536 * Since c2c links are internal nodes in the DAPM graph and
537 * don't interface with the outside world or application layer
538 * we don't have to do any special handling on close.
542 #ifdef CONFIG_PM_SLEEP
543 /* powers down audio subsystem for suspend */
544 int snd_soc_suspend(struct device *dev)
546 struct snd_soc_card *card = dev_get_drvdata(dev);
547 struct snd_soc_codec *codec;
550 /* If the card is not initialized yet there is nothing to do */
551 if (!card->instantiated)
554 /* Due to the resume being scheduled into a workqueue we could
555 * suspend before that's finished - wait for it to complete.
557 snd_power_lock(card->snd_card);
558 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
559 snd_power_unlock(card->snd_card);
561 /* we're going to block userspace touching us until resume completes */
562 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
564 /* mute any active DACs */
565 for (i = 0; i < card->num_rtd; i++) {
567 if (card->rtd[i].dai_link->ignore_suspend)
570 for (j = 0; j < card->rtd[i].num_codecs; j++) {
571 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
572 struct snd_soc_dai_driver *drv = dai->driver;
574 if (drv->ops->digital_mute && dai->playback_active)
575 drv->ops->digital_mute(dai, 1);
579 /* suspend all pcms */
580 for (i = 0; i < card->num_rtd; i++) {
581 if (card->rtd[i].dai_link->ignore_suspend)
584 snd_pcm_suspend_all(card->rtd[i].pcm);
587 if (card->suspend_pre)
588 card->suspend_pre(card);
590 for (i = 0; i < card->num_rtd; i++) {
591 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
592 struct snd_soc_platform *platform = card->rtd[i].platform;
594 if (card->rtd[i].dai_link->ignore_suspend)
597 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
598 cpu_dai->driver->suspend(cpu_dai);
599 if (platform->driver->suspend && !platform->suspended) {
600 platform->driver->suspend(cpu_dai);
601 platform->suspended = 1;
605 /* close any waiting streams and save state */
606 for (i = 0; i < card->num_rtd; i++) {
607 struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais;
608 flush_delayed_work(&card->rtd[i].delayed_work);
609 for (j = 0; j < card->rtd[i].num_codecs; j++) {
610 codec_dais[j]->codec->dapm.suspend_bias_level =
611 codec_dais[j]->codec->dapm.bias_level;
615 for (i = 0; i < card->num_rtd; i++) {
617 if (card->rtd[i].dai_link->ignore_suspend)
620 snd_soc_dapm_stream_event(&card->rtd[i],
621 SNDRV_PCM_STREAM_PLAYBACK,
622 SND_SOC_DAPM_STREAM_SUSPEND);
624 snd_soc_dapm_stream_event(&card->rtd[i],
625 SNDRV_PCM_STREAM_CAPTURE,
626 SND_SOC_DAPM_STREAM_SUSPEND);
629 /* Recheck all analogue paths too */
630 dapm_mark_io_dirty(&card->dapm);
631 snd_soc_dapm_sync(&card->dapm);
633 /* suspend all CODECs */
634 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
635 /* If there are paths active then the CODEC will be held with
636 * bias _ON and should not be suspended. */
637 if (!codec->suspended) {
638 switch (codec->dapm.bias_level) {
639 case SND_SOC_BIAS_STANDBY:
641 * If the CODEC is capable of idle
642 * bias off then being in STANDBY
643 * means it's doing something,
644 * otherwise fall through.
646 if (codec->dapm.idle_bias_off) {
648 "ASoC: idle_bias_off CODEC on over suspend\n");
652 case SND_SOC_BIAS_OFF:
653 if (codec->driver->suspend)
654 codec->driver->suspend(codec);
655 codec->suspended = 1;
656 if (codec->component.regmap)
657 regcache_mark_dirty(codec->component.regmap);
658 /* deactivate pins to sleep state */
659 pinctrl_pm_select_sleep_state(codec->dev);
663 "ASoC: CODEC is on over suspend\n");
669 for (i = 0; i < card->num_rtd; i++) {
670 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
672 if (card->rtd[i].dai_link->ignore_suspend)
675 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
676 cpu_dai->driver->suspend(cpu_dai);
678 /* deactivate pins to sleep state */
679 pinctrl_pm_select_sleep_state(cpu_dai->dev);
682 if (card->suspend_post)
683 card->suspend_post(card);
687 EXPORT_SYMBOL_GPL(snd_soc_suspend);
689 /* deferred resume work, so resume can complete before we finished
690 * setting our codec back up, which can be very slow on I2C
692 static void soc_resume_deferred(struct work_struct *work)
694 struct snd_soc_card *card =
695 container_of(work, struct snd_soc_card, deferred_resume_work);
696 struct snd_soc_codec *codec;
699 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
700 * so userspace apps are blocked from touching us
703 dev_dbg(card->dev, "ASoC: starting resume work\n");
705 /* Bring us up into D2 so that DAPM starts enabling things */
706 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
708 if (card->resume_pre)
709 card->resume_pre(card);
711 /* resume AC97 DAIs */
712 for (i = 0; i < card->num_rtd; i++) {
713 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
715 if (card->rtd[i].dai_link->ignore_suspend)
718 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
719 cpu_dai->driver->resume(cpu_dai);
722 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
723 /* If the CODEC was idle over suspend then it will have been
724 * left with bias OFF or STANDBY and suspended so we must now
725 * resume. Otherwise the suspend was suppressed.
727 if (codec->suspended) {
728 switch (codec->dapm.bias_level) {
729 case SND_SOC_BIAS_STANDBY:
730 case SND_SOC_BIAS_OFF:
731 if (codec->driver->resume)
732 codec->driver->resume(codec);
733 codec->suspended = 0;
737 "ASoC: CODEC was on over suspend\n");
743 for (i = 0; i < card->num_rtd; i++) {
745 if (card->rtd[i].dai_link->ignore_suspend)
748 snd_soc_dapm_stream_event(&card->rtd[i],
749 SNDRV_PCM_STREAM_PLAYBACK,
750 SND_SOC_DAPM_STREAM_RESUME);
752 snd_soc_dapm_stream_event(&card->rtd[i],
753 SNDRV_PCM_STREAM_CAPTURE,
754 SND_SOC_DAPM_STREAM_RESUME);
757 /* unmute any active DACs */
758 for (i = 0; i < card->num_rtd; i++) {
760 if (card->rtd[i].dai_link->ignore_suspend)
763 for (j = 0; j < card->rtd[i].num_codecs; j++) {
764 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
765 struct snd_soc_dai_driver *drv = dai->driver;
767 if (drv->ops->digital_mute && dai->playback_active)
768 drv->ops->digital_mute(dai, 0);
772 for (i = 0; i < card->num_rtd; i++) {
773 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
774 struct snd_soc_platform *platform = card->rtd[i].platform;
776 if (card->rtd[i].dai_link->ignore_suspend)
779 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
780 cpu_dai->driver->resume(cpu_dai);
781 if (platform->driver->resume && platform->suspended) {
782 platform->driver->resume(cpu_dai);
783 platform->suspended = 0;
787 if (card->resume_post)
788 card->resume_post(card);
790 dev_dbg(card->dev, "ASoC: resume work completed\n");
792 /* userspace can access us now we are back as we were before */
793 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
795 /* Recheck all analogue paths too */
796 dapm_mark_io_dirty(&card->dapm);
797 snd_soc_dapm_sync(&card->dapm);
800 /* powers up audio subsystem after a suspend */
801 int snd_soc_resume(struct device *dev)
803 struct snd_soc_card *card = dev_get_drvdata(dev);
804 int i, ac97_control = 0;
806 /* If the card is not initialized yet there is nothing to do */
807 if (!card->instantiated)
810 /* activate pins from sleep state */
811 for (i = 0; i < card->num_rtd; i++) {
812 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
813 struct snd_soc_dai **codec_dais = rtd->codec_dais;
814 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
818 pinctrl_pm_select_default_state(cpu_dai->dev);
820 for (j = 0; j < rtd->num_codecs; j++) {
821 struct snd_soc_dai *codec_dai = codec_dais[j];
822 if (codec_dai->active)
823 pinctrl_pm_select_default_state(codec_dai->dev);
827 /* AC97 devices might have other drivers hanging off them so
828 * need to resume immediately. Other drivers don't have that
829 * problem and may take a substantial amount of time to resume
830 * due to I/O costs and anti-pop so handle them out of line.
832 for (i = 0; i < card->num_rtd; i++) {
833 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
834 ac97_control |= cpu_dai->driver->ac97_control;
837 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
838 soc_resume_deferred(&card->deferred_resume_work);
840 dev_dbg(dev, "ASoC: Scheduling resume work\n");
841 if (!schedule_work(&card->deferred_resume_work))
842 dev_err(dev, "ASoC: resume work item may be lost\n");
847 EXPORT_SYMBOL_GPL(snd_soc_resume);
849 #define snd_soc_suspend NULL
850 #define snd_soc_resume NULL
853 static const struct snd_soc_dai_ops null_dai_ops = {
856 static struct snd_soc_component *soc_find_component(
857 const struct device_node *of_node, const char *name)
859 struct snd_soc_component *component;
861 list_for_each_entry(component, &component_list, list) {
863 if (component->dev->of_node == of_node)
865 } else if (strcmp(component->name, name) == 0) {
873 static struct snd_soc_dai *snd_soc_find_dai(
874 const struct snd_soc_dai_link_component *dlc)
876 struct snd_soc_component *component;
877 struct snd_soc_dai *dai;
879 /* Find CPU DAI from registered DAIs*/
880 list_for_each_entry(component, &component_list, list) {
881 if (dlc->of_node && component->dev->of_node != dlc->of_node)
883 if (dlc->name && strcmp(component->name, dlc->name))
885 list_for_each_entry(dai, &component->dai_list, list) {
886 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
896 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
898 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
899 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
900 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
901 struct snd_soc_dai_link_component cpu_dai_component;
902 struct snd_soc_dai **codec_dais = rtd->codec_dais;
903 struct snd_soc_platform *platform;
904 const char *platform_name;
907 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
909 cpu_dai_component.name = dai_link->cpu_name;
910 cpu_dai_component.of_node = dai_link->cpu_of_node;
911 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
912 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
914 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
915 dai_link->cpu_dai_name);
916 return -EPROBE_DEFER;
919 rtd->num_codecs = dai_link->num_codecs;
921 /* Find CODEC from registered CODECs */
922 for (i = 0; i < rtd->num_codecs; i++) {
923 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
924 if (!codec_dais[i]) {
925 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
927 return -EPROBE_DEFER;
931 /* Single codec links expect codec and codec_dai in runtime data */
932 rtd->codec_dai = codec_dais[0];
933 rtd->codec = rtd->codec_dai->codec;
935 /* if there's no platform we match on the empty platform */
936 platform_name = dai_link->platform_name;
937 if (!platform_name && !dai_link->platform_of_node)
938 platform_name = "snd-soc-dummy";
940 /* find one from the set of registered platforms */
941 list_for_each_entry(platform, &platform_list, list) {
942 if (dai_link->platform_of_node) {
943 if (platform->dev->of_node !=
944 dai_link->platform_of_node)
947 if (strcmp(platform->component.name, platform_name))
951 rtd->platform = platform;
953 if (!rtd->platform) {
954 dev_err(card->dev, "ASoC: platform %s not registered\n",
955 dai_link->platform_name);
956 return -EPROBE_DEFER;
964 static void soc_remove_component(struct snd_soc_component *component)
966 if (!component->probed)
969 /* This is a HACK and will be removed soon */
970 if (component->codec)
971 list_del(&component->codec->card_list);
973 if (component->remove)
974 component->remove(component);
976 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
978 soc_cleanup_component_debugfs(component);
979 component->probed = 0;
980 module_put(component->dev->driver->owner);
983 static void soc_remove_dai(struct snd_soc_dai *dai, int order)
987 if (dai && dai->probed &&
988 dai->driver->remove_order == order) {
989 if (dai->driver->remove) {
990 err = dai->driver->remove(dai);
993 "ASoC: failed to remove %s: %d\n",
1000 static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
1002 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1005 /* unregister the rtd device */
1006 if (rtd->dev_registered) {
1007 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1008 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1009 device_unregister(rtd->dev);
1010 rtd->dev_registered = 0;
1013 /* remove the CODEC DAI */
1014 for (i = 0; i < rtd->num_codecs; i++)
1015 soc_remove_dai(rtd->codec_dais[i], order);
1017 soc_remove_dai(rtd->cpu_dai, order);
1020 static void soc_remove_link_components(struct snd_soc_card *card, int num,
1023 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1024 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1025 struct snd_soc_platform *platform = rtd->platform;
1026 struct snd_soc_component *component;
1029 /* remove the platform */
1030 if (platform && platform->component.driver->remove_order == order)
1031 soc_remove_component(&platform->component);
1033 /* remove the CODEC-side CODEC */
1034 for (i = 0; i < rtd->num_codecs; i++) {
1035 component = rtd->codec_dais[i]->component;
1036 if (component->driver->remove_order == order)
1037 soc_remove_component(component);
1040 /* remove any CPU-side CODEC */
1042 if (cpu_dai->component->driver->remove_order == order)
1043 soc_remove_component(cpu_dai->component);
1047 static void soc_remove_dai_links(struct snd_soc_card *card)
1051 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1053 for (dai = 0; dai < card->num_rtd; dai++)
1054 soc_remove_link_dais(card, dai, order);
1057 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1059 for (dai = 0; dai < card->num_rtd; dai++)
1060 soc_remove_link_components(card, dai, order);
1066 static void soc_set_name_prefix(struct snd_soc_card *card,
1067 struct snd_soc_component *component)
1071 if (card->codec_conf == NULL)
1074 for (i = 0; i < card->num_configs; i++) {
1075 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1076 if (map->of_node && component->dev->of_node != map->of_node)
1078 if (map->dev_name && strcmp(component->name, map->dev_name))
1080 component->name_prefix = map->name_prefix;
1085 static int soc_probe_component(struct snd_soc_card *card,
1086 struct snd_soc_component *component)
1088 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1089 struct snd_soc_dai *dai;
1092 if (component->probed)
1095 component->card = card;
1097 soc_set_name_prefix(card, component);
1099 if (!try_module_get(component->dev->driver->owner))
1102 soc_init_component_debugfs(component);
1104 if (component->dapm_widgets) {
1105 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1106 component->num_dapm_widgets);
1109 dev_err(component->dev,
1110 "Failed to create new controls %d\n", ret);
1115 list_for_each_entry(dai, &component->dai_list, list) {
1116 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1118 dev_err(component->dev,
1119 "Failed to create DAI widgets %d\n", ret);
1124 if (component->probe) {
1125 ret = component->probe(component);
1127 dev_err(component->dev,
1128 "ASoC: failed to probe component %d\n", ret);
1132 WARN(dapm->idle_bias_off &&
1133 dapm->bias_level != SND_SOC_BIAS_OFF,
1134 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1138 if (component->controls)
1139 snd_soc_add_component_controls(component, component->controls,
1140 component->num_controls);
1141 if (component->dapm_routes)
1142 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1143 component->num_dapm_routes);
1145 component->probed = 1;
1146 list_add(&dapm->list, &card->dapm_list);
1148 /* This is a HACK and will be removed soon */
1149 if (component->codec)
1150 list_add(&component->codec->card_list, &card->codec_dev_list);
1155 soc_cleanup_component_debugfs(component);
1156 module_put(component->dev->driver->owner);
1161 static void rtd_release(struct device *dev)
1166 static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1171 /* register the rtd device */
1172 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1175 device_initialize(rtd->dev);
1176 rtd->dev->parent = rtd->card->dev;
1177 rtd->dev->release = rtd_release;
1178 dev_set_name(rtd->dev, "%s", name);
1179 dev_set_drvdata(rtd->dev, rtd);
1180 mutex_init(&rtd->pcm_mutex);
1181 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1182 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1183 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1184 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1185 ret = device_add(rtd->dev);
1187 /* calling put_device() here to free the rtd->dev */
1188 put_device(rtd->dev);
1189 dev_err(rtd->card->dev,
1190 "ASoC: failed to register runtime device: %d\n", ret);
1193 rtd->dev_registered = 1;
1196 /* add DAPM sysfs entries for this codec */
1197 ret = snd_soc_dapm_sys_add(rtd->dev);
1200 "ASoC: failed to add codec dapm sysfs entries: %d\n",
1203 /* add codec sysfs entries */
1204 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1207 "ASoC: failed to add codec sysfs files: %d\n",
1214 static int soc_probe_link_components(struct snd_soc_card *card, int num,
1217 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1218 struct snd_soc_platform *platform = rtd->platform;
1219 struct snd_soc_component *component;
1222 /* probe the CPU-side component, if it is a CODEC */
1223 component = rtd->cpu_dai->component;
1224 if (component->driver->probe_order == order) {
1225 ret = soc_probe_component(card, component);
1230 /* probe the CODEC-side components */
1231 for (i = 0; i < rtd->num_codecs; i++) {
1232 component = rtd->codec_dais[i]->component;
1233 if (component->driver->probe_order == order) {
1234 ret = soc_probe_component(card, component);
1240 /* probe the platform */
1241 if (platform->component.driver->probe_order == order) {
1242 ret = soc_probe_component(card, &platform->component);
1250 static int soc_probe_codec_dai(struct snd_soc_card *card,
1251 struct snd_soc_dai *codec_dai,
1256 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1257 if (codec_dai->driver->probe) {
1258 ret = codec_dai->driver->probe(codec_dai);
1260 dev_err(codec_dai->dev,
1261 "ASoC: failed to probe CODEC DAI %s: %d\n",
1262 codec_dai->name, ret);
1267 /* mark codec_dai as probed and add to card dai list */
1268 codec_dai->probed = 1;
1274 static int soc_link_dai_widgets(struct snd_soc_card *card,
1275 struct snd_soc_dai_link *dai_link,
1276 struct snd_soc_pcm_runtime *rtd)
1278 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1279 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1280 struct snd_soc_dapm_widget *play_w, *capture_w;
1283 if (rtd->num_codecs > 1)
1284 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1286 /* link the DAI widgets */
1287 play_w = codec_dai->playback_widget;
1288 capture_w = cpu_dai->capture_widget;
1289 if (play_w && capture_w) {
1290 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1293 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1294 play_w->name, capture_w->name, ret);
1299 play_w = cpu_dai->playback_widget;
1300 capture_w = codec_dai->capture_widget;
1301 if (play_w && capture_w) {
1302 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1305 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1306 play_w->name, capture_w->name, ret);
1314 static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1316 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1317 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1318 struct snd_soc_platform *platform = rtd->platform;
1319 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1322 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1323 card->name, num, order);
1325 /* config components */
1326 cpu_dai->platform = platform;
1327 cpu_dai->card = card;
1328 for (i = 0; i < rtd->num_codecs; i++)
1329 rtd->codec_dais[i]->card = card;
1331 /* set default power off timeout */
1332 rtd->pmdown_time = pmdown_time;
1334 /* probe the cpu_dai */
1335 if (!cpu_dai->probed &&
1336 cpu_dai->driver->probe_order == order) {
1337 if (cpu_dai->driver->probe) {
1338 ret = cpu_dai->driver->probe(cpu_dai);
1340 dev_err(cpu_dai->dev,
1341 "ASoC: failed to probe CPU DAI %s: %d\n",
1342 cpu_dai->name, ret);
1346 cpu_dai->probed = 1;
1349 /* probe the CODEC DAI */
1350 for (i = 0; i < rtd->num_codecs; i++) {
1351 ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order);
1356 /* complete DAI probe during last probe */
1357 if (order != SND_SOC_COMP_ORDER_LAST)
1360 /* do machine specific initialization */
1361 if (dai_link->init) {
1362 ret = dai_link->init(rtd);
1364 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1365 dai_link->name, ret);
1370 ret = soc_post_component_init(rtd, dai_link->name);
1374 #ifdef CONFIG_DEBUG_FS
1375 /* add DPCM sysfs entries */
1376 if (dai_link->dynamic) {
1377 ret = soc_dpcm_debugfs_add(rtd);
1380 "ASoC: failed to add dpcm sysfs entries: %d\n",
1387 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1389 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1392 if (cpu_dai->driver->compress_dai) {
1393 /*create compress_device"*/
1394 ret = soc_new_compress(rtd, num);
1396 dev_err(card->dev, "ASoC: can't create compress %s\n",
1397 dai_link->stream_name);
1402 if (!dai_link->params) {
1403 /* create the pcm */
1404 ret = soc_new_pcm(rtd, num);
1406 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1407 dai_link->stream_name, ret);
1411 INIT_DELAYED_WORK(&rtd->delayed_work,
1412 codec2codec_close_delayed_work);
1414 /* link the DAI widgets */
1415 ret = soc_link_dai_widgets(card, dai_link, rtd);
1421 /* add platform data for AC97 devices */
1422 for (i = 0; i < rtd->num_codecs; i++) {
1423 if (rtd->codec_dais[i]->driver->ac97_control)
1424 snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
1425 rtd->cpu_dai->ac97_pdata);
1431 #ifdef CONFIG_SND_SOC_AC97_BUS
1432 static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1433 struct snd_soc_dai *codec_dai)
1437 /* Only instantiate AC97 if not already done by the adaptor
1438 * for the generic AC97 subsystem.
1440 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
1442 * It is possible that the AC97 device is already registered to
1443 * the device subsystem. This happens when the device is created
1444 * via snd_ac97_mixer(). Currently only SoC codec that does so
1445 * is the generic AC97 glue but others migh emerge.
1447 * In those cases we don't try to register the device again.
1449 if (!codec->ac97_created)
1452 ret = soc_ac97_dev_register(codec);
1455 "ASoC: AC97 device register failed: %d\n", ret);
1459 codec->ac97_registered = 1;
1464 static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
1466 if (codec->ac97_registered) {
1467 soc_ac97_dev_unregister(codec);
1468 codec->ac97_registered = 0;
1472 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1476 for (i = 0; i < rtd->num_codecs; i++) {
1477 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1479 ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
1482 soc_unregister_ac97_codec(codec_dai->codec);
1490 static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1494 for (i = 0; i < rtd->num_codecs; i++)
1495 soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
1499 static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1501 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1502 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1503 const char *name = aux_dev->codec_name;
1505 rtd->component = soc_find_component(aux_dev->codec_of_node, name);
1506 if (!rtd->component) {
1507 if (aux_dev->codec_of_node)
1508 name = of_node_full_name(aux_dev->codec_of_node);
1510 dev_err(card->dev, "ASoC: %s not registered\n", name);
1511 return -EPROBE_DEFER;
1515 * Some places still reference rtd->codec, so we have to keep that
1516 * initialized if the component is a CODEC. Once all those references
1517 * have been removed, this code can be removed as well.
1519 rtd->codec = rtd->component->codec;
1524 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1526 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1527 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1530 ret = soc_probe_component(card, rtd->component);
1534 /* do machine specific initialization */
1535 if (aux_dev->init) {
1536 ret = aux_dev->init(rtd->component);
1538 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1539 aux_dev->name, ret);
1544 return soc_post_component_init(rtd, aux_dev->name);
1547 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1549 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1550 struct snd_soc_component *component = rtd->component;
1552 /* unregister the rtd device */
1553 if (rtd->dev_registered) {
1554 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1555 device_unregister(rtd->dev);
1556 rtd->dev_registered = 0;
1559 if (component && component->probed)
1560 soc_remove_component(component);
1563 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1567 if (codec->cache_init)
1570 ret = snd_soc_cache_init(codec);
1573 "ASoC: Failed to set cache compression type: %d\n",
1577 codec->cache_init = 1;
1581 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1583 struct snd_soc_codec *codec;
1584 struct snd_soc_dai_link *dai_link;
1585 int ret, i, order, dai_fmt;
1587 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1590 for (i = 0; i < card->num_links; i++) {
1591 ret = soc_bind_dai_link(card, i);
1596 /* bind aux_devs too */
1597 for (i = 0; i < card->num_aux_devs; i++) {
1598 ret = soc_bind_aux_dev(card, i);
1603 /* initialize the register cache for each available codec */
1604 list_for_each_entry(codec, &codec_list, list) {
1605 if (codec->cache_init)
1607 ret = snd_soc_init_codec_cache(codec);
1612 /* card bind complete so register a sound card */
1613 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1614 card->owner, 0, &card->snd_card);
1617 "ASoC: can't create sound card for card %s: %d\n",
1622 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1623 card->dapm.dev = card->dev;
1624 card->dapm.card = card;
1625 list_add(&card->dapm.list, &card->dapm_list);
1627 #ifdef CONFIG_DEBUG_FS
1628 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1631 #ifdef CONFIG_PM_SLEEP
1632 /* deferred resume work */
1633 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1636 if (card->dapm_widgets)
1637 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1638 card->num_dapm_widgets);
1640 /* initialise the sound card only once */
1642 ret = card->probe(card);
1644 goto card_probe_error;
1647 /* probe all components used by DAI links on this card */
1648 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1650 for (i = 0; i < card->num_links; i++) {
1651 ret = soc_probe_link_components(card, i, order);
1654 "ASoC: failed to instantiate card %d\n",
1661 /* probe all DAI links on this card */
1662 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1664 for (i = 0; i < card->num_links; i++) {
1665 ret = soc_probe_link_dais(card, i, order);
1668 "ASoC: failed to instantiate card %d\n",
1675 for (i = 0; i < card->num_aux_devs; i++) {
1676 ret = soc_probe_aux_dev(card, i);
1679 "ASoC: failed to add auxiliary devices %d\n",
1681 goto probe_aux_dev_err;
1685 snd_soc_dapm_link_dai_widgets(card);
1686 snd_soc_dapm_connect_dai_link_widgets(card);
1689 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1691 if (card->dapm_routes)
1692 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1693 card->num_dapm_routes);
1695 for (i = 0; i < card->num_links; i++) {
1696 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1697 dai_link = &card->dai_link[i];
1698 dai_fmt = dai_link->dai_fmt;
1701 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1704 for (j = 0; j < rtd->num_codecs; j++) {
1705 struct snd_soc_dai *codec_dai = codec_dais[j];
1707 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1708 if (ret != 0 && ret != -ENOTSUPP)
1709 dev_warn(codec_dai->dev,
1710 "ASoC: Failed to set DAI format: %d\n",
1715 /* If this is a regular CPU link there will be a platform */
1717 (dai_link->platform_name || dai_link->platform_of_node)) {
1718 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1720 if (ret != 0 && ret != -ENOTSUPP)
1721 dev_warn(card->rtd[i].cpu_dai->dev,
1722 "ASoC: Failed to set DAI format: %d\n",
1724 } else if (dai_fmt) {
1725 /* Flip the polarity for the "CPU" end */
1726 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1727 switch (dai_link->dai_fmt &
1728 SND_SOC_DAIFMT_MASTER_MASK) {
1729 case SND_SOC_DAIFMT_CBM_CFM:
1730 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1732 case SND_SOC_DAIFMT_CBM_CFS:
1733 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1735 case SND_SOC_DAIFMT_CBS_CFM:
1736 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1738 case SND_SOC_DAIFMT_CBS_CFS:
1739 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1743 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1745 if (ret != 0 && ret != -ENOTSUPP)
1746 dev_warn(card->rtd[i].cpu_dai->dev,
1747 "ASoC: Failed to set DAI format: %d\n",
1752 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1754 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1755 "%s", card->long_name ? card->long_name : card->name);
1756 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1757 "%s", card->driver_name ? card->driver_name : card->name);
1758 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1759 switch (card->snd_card->driver[i]) {
1765 if (!isalnum(card->snd_card->driver[i]))
1766 card->snd_card->driver[i] = '_';
1771 if (card->late_probe) {
1772 ret = card->late_probe(card);
1774 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
1776 goto probe_aux_dev_err;
1780 if (card->fully_routed)
1781 snd_soc_dapm_auto_nc_pins(card);
1783 snd_soc_dapm_new_widgets(card);
1785 ret = snd_card_register(card->snd_card);
1787 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1789 goto probe_aux_dev_err;
1792 #ifdef CONFIG_SND_SOC_AC97_BUS
1793 /* register any AC97 codecs */
1794 for (i = 0; i < card->num_rtd; i++) {
1795 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1798 "ASoC: failed to register AC97: %d\n", ret);
1800 soc_unregister_ac97_dai_link(&card->rtd[i]);
1801 goto probe_aux_dev_err;
1806 card->instantiated = 1;
1807 snd_soc_dapm_sync(&card->dapm);
1808 mutex_unlock(&card->mutex);
1813 for (i = 0; i < card->num_aux_devs; i++)
1814 soc_remove_aux_dev(card, i);
1817 soc_remove_dai_links(card);
1823 snd_card_free(card->snd_card);
1826 mutex_unlock(&card->mutex);
1831 /* probes a new socdev */
1832 static int soc_probe(struct platform_device *pdev)
1834 struct snd_soc_card *card = platform_get_drvdata(pdev);
1837 * no card, so machine driver should be registering card
1838 * we should not be here in that case so ret error
1843 dev_warn(&pdev->dev,
1844 "ASoC: machine %s should use snd_soc_register_card()\n",
1847 /* Bodge while we unpick instantiation */
1848 card->dev = &pdev->dev;
1850 return snd_soc_register_card(card);
1853 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1857 /* make sure any delayed work runs */
1858 for (i = 0; i < card->num_rtd; i++) {
1859 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1860 flush_delayed_work(&rtd->delayed_work);
1863 /* remove auxiliary devices */
1864 for (i = 0; i < card->num_aux_devs; i++)
1865 soc_remove_aux_dev(card, i);
1867 /* remove and free each DAI */
1868 soc_remove_dai_links(card);
1870 soc_cleanup_card_debugfs(card);
1872 /* remove the card */
1876 snd_soc_dapm_free(&card->dapm);
1878 snd_card_free(card->snd_card);
1883 /* removes a socdev */
1884 static int soc_remove(struct platform_device *pdev)
1886 struct snd_soc_card *card = platform_get_drvdata(pdev);
1888 snd_soc_unregister_card(card);
1892 int snd_soc_poweroff(struct device *dev)
1894 struct snd_soc_card *card = dev_get_drvdata(dev);
1897 if (!card->instantiated)
1900 /* Flush out pmdown_time work - we actually do want to run it
1901 * now, we're shutting down so no imminent restart. */
1902 for (i = 0; i < card->num_rtd; i++) {
1903 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1904 flush_delayed_work(&rtd->delayed_work);
1907 snd_soc_dapm_shutdown(card);
1909 /* deactivate pins to sleep state */
1910 for (i = 0; i < card->num_rtd; i++) {
1911 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1912 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1915 pinctrl_pm_select_sleep_state(cpu_dai->dev);
1916 for (j = 0; j < rtd->num_codecs; j++) {
1917 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
1918 pinctrl_pm_select_sleep_state(codec_dai->dev);
1924 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1926 const struct dev_pm_ops snd_soc_pm_ops = {
1927 .suspend = snd_soc_suspend,
1928 .resume = snd_soc_resume,
1929 .freeze = snd_soc_suspend,
1930 .thaw = snd_soc_resume,
1931 .poweroff = snd_soc_poweroff,
1932 .restore = snd_soc_resume,
1934 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1936 /* ASoC platform driver */
1937 static struct platform_driver soc_driver = {
1939 .name = "soc-audio",
1940 .owner = THIS_MODULE,
1941 .pm = &snd_soc_pm_ops,
1944 .remove = soc_remove,
1948 * snd_soc_new_ac97_codec - initailise AC97 device
1949 * @codec: audio codec
1950 * @ops: AC97 bus operations
1951 * @num: AC97 codec number
1953 * Initialises AC97 codec resources for use by ad-hoc devices only.
1955 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1956 struct snd_ac97_bus_ops *ops, int num)
1958 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1959 if (codec->ac97 == NULL)
1962 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1963 if (codec->ac97->bus == NULL) {
1969 codec->ac97->bus->ops = ops;
1970 codec->ac97->num = num;
1973 * Mark the AC97 device to be created by us. This way we ensure that the
1974 * device will be registered with the device subsystem later on.
1976 codec->ac97_created = 1;
1980 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1982 static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
1984 static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
1986 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
1988 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
1990 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
1994 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
1996 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2000 static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2002 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2004 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2006 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2007 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2008 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2012 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2014 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2018 static int snd_soc_ac97_parse_pinctl(struct device *dev,
2019 struct snd_ac97_reset_cfg *cfg)
2022 struct pinctrl_state *state;
2026 p = devm_pinctrl_get(dev);
2028 dev_err(dev, "Failed to get pinctrl\n");
2033 state = pinctrl_lookup_state(p, "ac97-reset");
2034 if (IS_ERR(state)) {
2035 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
2036 return PTR_ERR(state);
2038 cfg->pstate_reset = state;
2040 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2041 if (IS_ERR(state)) {
2042 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
2043 return PTR_ERR(state);
2045 cfg->pstate_warm_reset = state;
2047 state = pinctrl_lookup_state(p, "ac97-running");
2048 if (IS_ERR(state)) {
2049 dev_err(dev, "Can't find pinctrl state ac97-running\n");
2050 return PTR_ERR(state);
2052 cfg->pstate_run = state;
2054 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2056 dev_err(dev, "Can't find ac97-sync gpio\n");
2059 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2061 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2064 cfg->gpio_sync = gpio;
2066 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2068 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2071 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2073 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2076 cfg->gpio_sdata = gpio;
2078 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2080 dev_err(dev, "Can't find ac97-reset gpio\n");
2083 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2085 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2088 cfg->gpio_reset = gpio;
2093 struct snd_ac97_bus_ops *soc_ac97_ops;
2094 EXPORT_SYMBOL_GPL(soc_ac97_ops);
2096 int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2098 if (ops == soc_ac97_ops)
2101 if (soc_ac97_ops && ops)
2108 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2111 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2113 * This function sets the reset and warm_reset properties of ops and parses
2114 * the device node of pdev to get pinctrl states and gpio numbers to use.
2116 int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2117 struct platform_device *pdev)
2119 struct device *dev = &pdev->dev;
2120 struct snd_ac97_reset_cfg cfg;
2123 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2127 ret = snd_soc_set_ac97_ops(ops);
2131 ops->warm_reset = snd_soc_ac97_warm_reset;
2132 ops->reset = snd_soc_ac97_reset;
2134 snd_ac97_rst_cfg = cfg;
2137 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2140 * snd_soc_free_ac97_codec - free AC97 codec device
2141 * @codec: audio codec
2143 * Frees AC97 codec device resources.
2145 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2147 #ifdef CONFIG_SND_SOC_AC97_BUS
2148 soc_unregister_ac97_codec(codec);
2150 kfree(codec->ac97->bus);
2153 codec->ac97_created = 0;
2155 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2158 * snd_soc_cnew - create new control
2159 * @_template: control template
2160 * @data: control private data
2161 * @long_name: control long name
2162 * @prefix: control name prefix
2164 * Create a new mixer control from a template control.
2166 * Returns 0 for success, else error.
2168 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2169 void *data, const char *long_name,
2172 struct snd_kcontrol_new template;
2173 struct snd_kcontrol *kcontrol;
2176 memcpy(&template, _template, sizeof(template));
2180 long_name = template.name;
2183 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2187 template.name = name;
2189 template.name = long_name;
2192 kcontrol = snd_ctl_new1(&template, data);
2198 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2200 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2201 const struct snd_kcontrol_new *controls, int num_controls,
2202 const char *prefix, void *data)
2206 for (i = 0; i < num_controls; i++) {
2207 const struct snd_kcontrol_new *control = &controls[i];
2208 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2209 control->name, prefix));
2211 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2212 control->name, err);
2220 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2223 struct snd_card *card = soc_card->snd_card;
2224 struct snd_kcontrol *kctl;
2226 if (unlikely(!name))
2229 list_for_each_entry(kctl, &card->controls, list)
2230 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2234 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2237 * snd_soc_add_component_controls - Add an array of controls to a component.
2239 * @component: Component to add controls to
2240 * @controls: Array of controls to add
2241 * @num_controls: Number of elements in the array
2243 * Return: 0 for success, else error.
2245 int snd_soc_add_component_controls(struct snd_soc_component *component,
2246 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2248 struct snd_card *card = component->card->snd_card;
2250 return snd_soc_add_controls(card, component->dev, controls,
2251 num_controls, component->name_prefix, component);
2253 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2256 * snd_soc_add_codec_controls - add an array of controls to a codec.
2257 * Convenience function to add a list of controls. Many codecs were
2258 * duplicating this code.
2260 * @codec: codec to add controls to
2261 * @controls: array of controls to add
2262 * @num_controls: number of elements in the array
2264 * Return 0 for success, else error.
2266 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2267 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2269 return snd_soc_add_component_controls(&codec->component, controls,
2272 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2275 * snd_soc_add_platform_controls - add an array of controls to a platform.
2276 * Convenience function to add a list of controls.
2278 * @platform: platform to add controls to
2279 * @controls: array of controls to add
2280 * @num_controls: number of elements in the array
2282 * Return 0 for success, else error.
2284 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2285 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2287 return snd_soc_add_component_controls(&platform->component, controls,
2290 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2293 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2294 * Convenience function to add a list of controls.
2296 * @soc_card: SoC card to add controls to
2297 * @controls: array of controls to add
2298 * @num_controls: number of elements in the array
2300 * Return 0 for success, else error.
2302 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2303 const struct snd_kcontrol_new *controls, int num_controls)
2305 struct snd_card *card = soc_card->snd_card;
2307 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2310 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2313 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2314 * Convienience function to add a list of controls.
2316 * @dai: DAI to add controls to
2317 * @controls: array of controls to add
2318 * @num_controls: number of elements in the array
2320 * Return 0 for success, else error.
2322 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2323 const struct snd_kcontrol_new *controls, int num_controls)
2325 struct snd_card *card = dai->card->snd_card;
2327 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2330 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2333 * snd_soc_info_enum_double - enumerated double mixer info callback
2334 * @kcontrol: mixer control
2335 * @uinfo: control element information
2337 * Callback to provide information about a double enumerated
2340 * Returns 0 for success.
2342 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2343 struct snd_ctl_elem_info *uinfo)
2345 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2347 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2348 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2349 uinfo->value.enumerated.items = e->items;
2351 if (uinfo->value.enumerated.item >= e->items)
2352 uinfo->value.enumerated.item = e->items - 1;
2353 strlcpy(uinfo->value.enumerated.name,
2354 e->texts[uinfo->value.enumerated.item],
2355 sizeof(uinfo->value.enumerated.name));
2358 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2361 * snd_soc_get_enum_double - enumerated double mixer get callback
2362 * @kcontrol: mixer control
2363 * @ucontrol: control element information
2365 * Callback to get the value of a double enumerated mixer.
2367 * Returns 0 for success.
2369 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2370 struct snd_ctl_elem_value *ucontrol)
2372 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2373 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2374 unsigned int val, item;
2375 unsigned int reg_val;
2378 ret = snd_soc_component_read(component, e->reg, ®_val);
2381 val = (reg_val >> e->shift_l) & e->mask;
2382 item = snd_soc_enum_val_to_item(e, val);
2383 ucontrol->value.enumerated.item[0] = item;
2384 if (e->shift_l != e->shift_r) {
2385 val = (reg_val >> e->shift_l) & e->mask;
2386 item = snd_soc_enum_val_to_item(e, val);
2387 ucontrol->value.enumerated.item[1] = item;
2392 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2395 * snd_soc_put_enum_double - enumerated double mixer put callback
2396 * @kcontrol: mixer control
2397 * @ucontrol: control element information
2399 * Callback to set the value of a double enumerated mixer.
2401 * Returns 0 for success.
2403 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2404 struct snd_ctl_elem_value *ucontrol)
2406 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2407 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2408 unsigned int *item = ucontrol->value.enumerated.item;
2412 if (item[0] >= e->items)
2414 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
2415 mask = e->mask << e->shift_l;
2416 if (e->shift_l != e->shift_r) {
2417 if (item[1] >= e->items)
2419 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
2420 mask |= e->mask << e->shift_r;
2423 return snd_soc_component_update_bits(component, e->reg, mask, val);
2425 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2428 * snd_soc_read_signed - Read a codec register and interprete as signed value
2429 * @component: component
2430 * @reg: Register to read
2431 * @mask: Mask to use after shifting the register value
2432 * @shift: Right shift of register value
2433 * @sign_bit: Bit that describes if a number is negative or not.
2434 * @signed_val: Pointer to where the read value should be stored
2436 * This functions reads a codec register. The register value is shifted right
2437 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2438 * the given registervalue into a signed integer if sign_bit is non-zero.
2440 * Returns 0 on sucess, otherwise an error value
2442 static int snd_soc_read_signed(struct snd_soc_component *component,
2443 unsigned int reg, unsigned int mask, unsigned int shift,
2444 unsigned int sign_bit, int *signed_val)
2449 ret = snd_soc_component_read(component, reg, &val);
2453 val = (val >> shift) & mask;
2460 /* non-negative number */
2461 if (!(val & BIT(sign_bit))) {
2469 * The register most probably does not contain a full-sized int.
2470 * Instead we have an arbitrary number of bits in a signed
2471 * representation which has to be translated into a full-sized int.
2472 * This is done by filling up all bits above the sign-bit.
2474 ret |= ~((int)(BIT(sign_bit) - 1));
2482 * snd_soc_info_volsw - single mixer info callback
2483 * @kcontrol: mixer control
2484 * @uinfo: control element information
2486 * Callback to provide information about a single mixer control, or a double
2487 * mixer control that spans 2 registers.
2489 * Returns 0 for success.
2491 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2492 struct snd_ctl_elem_info *uinfo)
2494 struct soc_mixer_control *mc =
2495 (struct soc_mixer_control *)kcontrol->private_value;
2498 if (!mc->platform_max)
2499 mc->platform_max = mc->max;
2500 platform_max = mc->platform_max;
2502 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2503 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2505 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2507 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2508 uinfo->value.integer.min = 0;
2509 uinfo->value.integer.max = platform_max - mc->min;
2512 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2515 * snd_soc_get_volsw - single mixer get callback
2516 * @kcontrol: mixer control
2517 * @ucontrol: control element information
2519 * Callback to get the value of a single mixer control, or a double mixer
2520 * control that spans 2 registers.
2522 * Returns 0 for success.
2524 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2525 struct snd_ctl_elem_value *ucontrol)
2527 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2528 struct soc_mixer_control *mc =
2529 (struct soc_mixer_control *)kcontrol->private_value;
2530 unsigned int reg = mc->reg;
2531 unsigned int reg2 = mc->rreg;
2532 unsigned int shift = mc->shift;
2533 unsigned int rshift = mc->rshift;
2536 int sign_bit = mc->sign_bit;
2537 unsigned int mask = (1 << fls(max)) - 1;
2538 unsigned int invert = mc->invert;
2543 mask = BIT(sign_bit + 1) - 1;
2545 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2549 ucontrol->value.integer.value[0] = val - min;
2551 ucontrol->value.integer.value[0] =
2552 max - ucontrol->value.integer.value[0];
2554 if (snd_soc_volsw_is_stereo(mc)) {
2556 ret = snd_soc_read_signed(component, reg, mask, rshift,
2559 ret = snd_soc_read_signed(component, reg2, mask, shift,
2564 ucontrol->value.integer.value[1] = val - min;
2566 ucontrol->value.integer.value[1] =
2567 max - ucontrol->value.integer.value[1];
2572 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2575 * snd_soc_put_volsw - single mixer put callback
2576 * @kcontrol: mixer control
2577 * @ucontrol: control element information
2579 * Callback to set the value of a single mixer control, or a double mixer
2580 * control that spans 2 registers.
2582 * Returns 0 for success.
2584 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2585 struct snd_ctl_elem_value *ucontrol)
2587 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2588 struct soc_mixer_control *mc =
2589 (struct soc_mixer_control *)kcontrol->private_value;
2590 unsigned int reg = mc->reg;
2591 unsigned int reg2 = mc->rreg;
2592 unsigned int shift = mc->shift;
2593 unsigned int rshift = mc->rshift;
2596 unsigned int sign_bit = mc->sign_bit;
2597 unsigned int mask = (1 << fls(max)) - 1;
2598 unsigned int invert = mc->invert;
2600 bool type_2r = false;
2601 unsigned int val2 = 0;
2602 unsigned int val, val_mask;
2605 mask = BIT(sign_bit + 1) - 1;
2607 val = ((ucontrol->value.integer.value[0] + min) & mask);
2610 val_mask = mask << shift;
2612 if (snd_soc_volsw_is_stereo(mc)) {
2613 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
2617 val_mask |= mask << rshift;
2618 val |= val2 << rshift;
2620 val2 = val2 << shift;
2624 err = snd_soc_component_update_bits(component, reg, val_mask, val);
2629 err = snd_soc_component_update_bits(component, reg2, val_mask,
2634 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2637 * snd_soc_get_volsw_sx - single mixer get callback
2638 * @kcontrol: mixer control
2639 * @ucontrol: control element information
2641 * Callback to get the value of a single mixer control, or a double mixer
2642 * control that spans 2 registers.
2644 * Returns 0 for success.
2646 int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2647 struct snd_ctl_elem_value *ucontrol)
2649 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2650 struct soc_mixer_control *mc =
2651 (struct soc_mixer_control *)kcontrol->private_value;
2652 unsigned int reg = mc->reg;
2653 unsigned int reg2 = mc->rreg;
2654 unsigned int shift = mc->shift;
2655 unsigned int rshift = mc->rshift;
2658 int mask = (1 << (fls(min + max) - 1)) - 1;
2662 ret = snd_soc_component_read(component, reg, &val);
2666 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2668 if (snd_soc_volsw_is_stereo(mc)) {
2669 ret = snd_soc_component_read(component, reg2, &val);
2673 val = ((val >> rshift) - min) & mask;
2674 ucontrol->value.integer.value[1] = val;
2679 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2682 * snd_soc_put_volsw_sx - double mixer set callback
2683 * @kcontrol: mixer control
2684 * @uinfo: control element information
2686 * Callback to set the value of a double mixer control that spans 2 registers.
2688 * Returns 0 for success.
2690 int snd_soc_put_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;
2697 unsigned int reg = mc->reg;
2698 unsigned int reg2 = mc->rreg;
2699 unsigned int shift = mc->shift;
2700 unsigned int rshift = mc->rshift;
2703 int mask = (1 << (fls(min + max) - 1)) - 1;
2705 unsigned int val, val_mask, val2 = 0;
2707 val_mask = mask << shift;
2708 val = (ucontrol->value.integer.value[0] + min) & mask;
2711 err = snd_soc_component_update_bits(component, reg, val_mask, val);
2715 if (snd_soc_volsw_is_stereo(mc)) {
2716 val_mask = mask << rshift;
2717 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2718 val2 = val2 << rshift;
2720 err = snd_soc_component_update_bits(component, reg2, val_mask,
2725 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2728 * snd_soc_info_volsw_s8 - signed mixer info callback
2729 * @kcontrol: mixer control
2730 * @uinfo: control element information
2732 * Callback to provide information about a signed mixer control.
2734 * Returns 0 for success.
2736 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2737 struct snd_ctl_elem_info *uinfo)
2739 struct soc_mixer_control *mc =
2740 (struct soc_mixer_control *)kcontrol->private_value;
2744 if (!mc->platform_max)
2745 mc->platform_max = mc->max;
2746 platform_max = mc->platform_max;
2748 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2750 uinfo->value.integer.min = 0;
2751 uinfo->value.integer.max = platform_max - min;
2754 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2757 * snd_soc_get_volsw_s8 - signed mixer get callback
2758 * @kcontrol: mixer control
2759 * @ucontrol: control element information
2761 * Callback to get the value of a signed mixer control.
2763 * Returns 0 for success.
2765 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2766 struct snd_ctl_elem_value *ucontrol)
2768 struct soc_mixer_control *mc =
2769 (struct soc_mixer_control *)kcontrol->private_value;
2770 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2771 unsigned int reg = mc->reg;
2776 ret = snd_soc_component_read(component, reg, &val);
2780 ucontrol->value.integer.value[0] =
2781 ((signed char)(val & 0xff))-min;
2782 ucontrol->value.integer.value[1] =
2783 ((signed char)((val >> 8) & 0xff))-min;
2786 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2789 * snd_soc_put_volsw_sgn - signed mixer put callback
2790 * @kcontrol: mixer control
2791 * @ucontrol: control element information
2793 * Callback to set the value of a signed mixer control.
2795 * Returns 0 for success.
2797 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2798 struct snd_ctl_elem_value *ucontrol)
2800 struct soc_mixer_control *mc =
2801 (struct soc_mixer_control *)kcontrol->private_value;
2802 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2803 unsigned int reg = mc->reg;
2807 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2808 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2810 return snd_soc_component_update_bits(component, reg, 0xffff, val);
2812 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2815 * snd_soc_info_volsw_range - single mixer info callback with range.
2816 * @kcontrol: mixer control
2817 * @uinfo: control element information
2819 * Callback to provide information, within a range, about a single
2822 * returns 0 for success.
2824 int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2825 struct snd_ctl_elem_info *uinfo)
2827 struct soc_mixer_control *mc =
2828 (struct soc_mixer_control *)kcontrol->private_value;
2832 if (!mc->platform_max)
2833 mc->platform_max = mc->max;
2834 platform_max = mc->platform_max;
2836 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2837 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2838 uinfo->value.integer.min = 0;
2839 uinfo->value.integer.max = platform_max - min;
2843 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2846 * snd_soc_put_volsw_range - single mixer put value callback with range.
2847 * @kcontrol: mixer control
2848 * @ucontrol: control element information
2850 * Callback to set the value, within a range, for a single mixer control.
2852 * Returns 0 for success.
2854 int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2855 struct snd_ctl_elem_value *ucontrol)
2857 struct soc_mixer_control *mc =
2858 (struct soc_mixer_control *)kcontrol->private_value;
2859 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2860 unsigned int reg = mc->reg;
2861 unsigned int rreg = mc->rreg;
2862 unsigned int shift = mc->shift;
2865 unsigned int mask = (1 << fls(max)) - 1;
2866 unsigned int invert = mc->invert;
2867 unsigned int val, val_mask;
2871 val = (max - ucontrol->value.integer.value[0]) & mask;
2873 val = ((ucontrol->value.integer.value[0] + min) & mask);
2874 val_mask = mask << shift;
2877 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
2881 if (snd_soc_volsw_is_stereo(mc)) {
2883 val = (max - ucontrol->value.integer.value[1]) & mask;
2885 val = ((ucontrol->value.integer.value[1] + min) & mask);
2886 val_mask = mask << shift;
2889 ret = snd_soc_component_update_bits(component, rreg, val_mask,
2895 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2898 * snd_soc_get_volsw_range - single mixer get callback with range
2899 * @kcontrol: mixer control
2900 * @ucontrol: control element information
2902 * Callback to get the value, within a range, of a single mixer control.
2904 * Returns 0 for success.
2906 int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2907 struct snd_ctl_elem_value *ucontrol)
2909 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2910 struct soc_mixer_control *mc =
2911 (struct soc_mixer_control *)kcontrol->private_value;
2912 unsigned int reg = mc->reg;
2913 unsigned int rreg = mc->rreg;
2914 unsigned int shift = mc->shift;
2917 unsigned int mask = (1 << fls(max)) - 1;
2918 unsigned int invert = mc->invert;
2922 ret = snd_soc_component_read(component, reg, &val);
2926 ucontrol->value.integer.value[0] = (val >> shift) & mask;
2928 ucontrol->value.integer.value[0] =
2929 max - ucontrol->value.integer.value[0];
2931 ucontrol->value.integer.value[0] =
2932 ucontrol->value.integer.value[0] - min;
2934 if (snd_soc_volsw_is_stereo(mc)) {
2935 ret = snd_soc_component_read(component, rreg, &val);
2939 ucontrol->value.integer.value[1] = (val >> shift) & mask;
2941 ucontrol->value.integer.value[1] =
2942 max - ucontrol->value.integer.value[1];
2944 ucontrol->value.integer.value[1] =
2945 ucontrol->value.integer.value[1] - min;
2950 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
2953 * snd_soc_limit_volume - Set new limit to an existing volume control.
2955 * @codec: where to look for the control
2956 * @name: Name of the control
2957 * @max: new maximum limit
2959 * Return 0 for success, else error.
2961 int snd_soc_limit_volume(struct snd_soc_codec *codec,
2962 const char *name, int max)
2964 struct snd_card *card = codec->component.card->snd_card;
2965 struct snd_kcontrol *kctl;
2966 struct soc_mixer_control *mc;
2970 /* Sanity check for name and max */
2971 if (unlikely(!name || max <= 0))
2974 list_for_each_entry(kctl, &card->controls, list) {
2975 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2981 mc = (struct soc_mixer_control *)kctl->private_value;
2982 if (max <= mc->max) {
2983 mc->platform_max = max;
2989 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2991 int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
2992 struct snd_ctl_elem_info *uinfo)
2994 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2995 struct soc_bytes *params = (void *)kcontrol->private_value;
2997 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
2998 uinfo->count = params->num_regs * component->val_bytes;
3002 EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3004 int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3005 struct snd_ctl_elem_value *ucontrol)
3007 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3008 struct soc_bytes *params = (void *)kcontrol->private_value;
3011 if (component->regmap)
3012 ret = regmap_raw_read(component->regmap, params->base,
3013 ucontrol->value.bytes.data,
3014 params->num_regs * component->val_bytes);
3018 /* Hide any masked bytes to ensure consistent data reporting */
3019 if (ret == 0 && params->mask) {
3020 switch (component->val_bytes) {
3022 ucontrol->value.bytes.data[0] &= ~params->mask;
3025 ((u16 *)(&ucontrol->value.bytes.data))[0]
3026 &= cpu_to_be16(~params->mask);
3029 ((u32 *)(&ucontrol->value.bytes.data))[0]
3030 &= cpu_to_be32(~params->mask);
3039 EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3041 int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3042 struct snd_ctl_elem_value *ucontrol)
3044 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3045 struct soc_bytes *params = (void *)kcontrol->private_value;
3047 unsigned int val, mask;
3050 if (!component->regmap || !params->num_regs)
3053 len = params->num_regs * component->val_bytes;
3055 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3060 * If we've got a mask then we need to preserve the register
3061 * bits. We shouldn't modify the incoming data so take a
3065 ret = regmap_read(component->regmap, params->base, &val);
3069 val &= params->mask;
3071 switch (component->val_bytes) {
3073 ((u8 *)data)[0] &= ~params->mask;
3074 ((u8 *)data)[0] |= val;
3077 mask = ~params->mask;
3078 ret = regmap_parse_val(component->regmap,
3083 ((u16 *)data)[0] &= mask;
3085 ret = regmap_parse_val(component->regmap,
3090 ((u16 *)data)[0] |= val;
3093 mask = ~params->mask;
3094 ret = regmap_parse_val(component->regmap,
3099 ((u32 *)data)[0] &= mask;
3101 ret = regmap_parse_val(component->regmap,
3106 ((u32 *)data)[0] |= val;
3114 ret = regmap_raw_write(component->regmap, params->base,
3122 EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3124 int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3125 struct snd_ctl_elem_info *ucontrol)
3127 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3129 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3130 ucontrol->count = params->max;
3134 EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3136 int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
3137 unsigned int size, unsigned int __user *tlv)
3139 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3140 unsigned int count = size < params->max ? size : params->max;
3144 case SNDRV_CTL_TLV_OP_READ:
3146 ret = params->get(tlv, count);
3148 case SNDRV_CTL_TLV_OP_WRITE:
3150 ret = params->put(tlv, count);
3155 EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
3158 * snd_soc_info_xr_sx - signed multi register info callback
3159 * @kcontrol: mreg control
3160 * @uinfo: control element information
3162 * Callback to provide information of a control that can
3163 * span multiple codec registers which together
3164 * forms a single signed value in a MSB/LSB manner.
3166 * Returns 0 for success.
3168 int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3169 struct snd_ctl_elem_info *uinfo)
3171 struct soc_mreg_control *mc =
3172 (struct soc_mreg_control *)kcontrol->private_value;
3173 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3175 uinfo->value.integer.min = mc->min;
3176 uinfo->value.integer.max = mc->max;
3180 EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3183 * snd_soc_get_xr_sx - signed multi register get callback
3184 * @kcontrol: mreg control
3185 * @ucontrol: control element information
3187 * Callback to get the value of a control that can span
3188 * multiple codec registers which together forms a single
3189 * signed value in a MSB/LSB manner. The control supports
3190 * specifying total no of bits used to allow for bitfields
3191 * across the multiple codec registers.
3193 * Returns 0 for success.
3195 int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3196 struct snd_ctl_elem_value *ucontrol)
3198 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3199 struct soc_mreg_control *mc =
3200 (struct soc_mreg_control *)kcontrol->private_value;
3201 unsigned int regbase = mc->regbase;
3202 unsigned int regcount = mc->regcount;
3203 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
3204 unsigned int regwmask = (1<<regwshift)-1;
3205 unsigned int invert = mc->invert;
3206 unsigned long mask = (1UL<<mc->nbits)-1;
3210 unsigned int regval;
3214 for (i = 0; i < regcount; i++) {
3215 ret = snd_soc_component_read(component, regbase+i, ®val);
3218 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
3221 if (min < 0 && val > max)
3225 ucontrol->value.integer.value[0] = val;
3229 EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3232 * snd_soc_put_xr_sx - signed multi register get callback
3233 * @kcontrol: mreg control
3234 * @ucontrol: control element information
3236 * Callback to set the value of a control that can span
3237 * multiple codec registers which together forms a single
3238 * signed value in a MSB/LSB manner. The control supports
3239 * specifying total no of bits used to allow for bitfields
3240 * across the multiple codec registers.
3242 * Returns 0 for success.
3244 int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3245 struct snd_ctl_elem_value *ucontrol)
3247 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3248 struct soc_mreg_control *mc =
3249 (struct soc_mreg_control *)kcontrol->private_value;
3250 unsigned int regbase = mc->regbase;
3251 unsigned int regcount = mc->regcount;
3252 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
3253 unsigned int regwmask = (1<<regwshift)-1;
3254 unsigned int invert = mc->invert;
3255 unsigned long mask = (1UL<<mc->nbits)-1;
3257 long val = ucontrol->value.integer.value[0];
3258 unsigned int i, regval, regmask;
3264 for (i = 0; i < regcount; i++) {
3265 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3266 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3267 err = snd_soc_component_update_bits(component, regbase+i,
3275 EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3278 * snd_soc_get_strobe - strobe get callback
3279 * @kcontrol: mixer control
3280 * @ucontrol: control element information
3282 * Callback get the value of a strobe mixer control.
3284 * Returns 0 for success.
3286 int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3287 struct snd_ctl_elem_value *ucontrol)
3289 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3290 struct soc_mixer_control *mc =
3291 (struct soc_mixer_control *)kcontrol->private_value;
3292 unsigned int reg = mc->reg;
3293 unsigned int shift = mc->shift;
3294 unsigned int mask = 1 << shift;
3295 unsigned int invert = mc->invert != 0;
3299 ret = snd_soc_component_read(component, reg, &val);
3305 if (shift != 0 && val != 0)
3307 ucontrol->value.enumerated.item[0] = val ^ invert;
3311 EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3314 * snd_soc_put_strobe - strobe put callback
3315 * @kcontrol: mixer control
3316 * @ucontrol: control element information
3318 * Callback strobe a register bit to high then low (or the inverse)
3319 * in one pass of a single mixer enum control.
3321 * Returns 1 for success.
3323 int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3324 struct snd_ctl_elem_value *ucontrol)
3326 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3327 struct soc_mixer_control *mc =
3328 (struct soc_mixer_control *)kcontrol->private_value;
3329 unsigned int reg = mc->reg;
3330 unsigned int shift = mc->shift;
3331 unsigned int mask = 1 << shift;
3332 unsigned int invert = mc->invert != 0;
3333 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3334 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3335 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3338 err = snd_soc_component_update_bits(component, reg, mask, val1);
3342 return snd_soc_component_update_bits(component, reg, mask, val2);
3344 EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3347 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3349 * @clk_id: DAI specific clock ID
3350 * @freq: new clock frequency in Hz
3351 * @dir: new clock direction - input/output.
3353 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3355 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3356 unsigned int freq, int dir)
3358 if (dai->driver && dai->driver->ops->set_sysclk)
3359 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3360 else if (dai->codec && dai->codec->driver->set_sysclk)
3361 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3366 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3369 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3371 * @clk_id: DAI specific clock ID
3372 * @source: Source for the clock
3373 * @freq: new clock frequency in Hz
3374 * @dir: new clock direction - input/output.
3376 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3378 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3379 int source, unsigned int freq, int dir)
3381 if (codec->driver->set_sysclk)
3382 return codec->driver->set_sysclk(codec, clk_id, source,
3387 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3390 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3392 * @div_id: DAI specific clock divider ID
3393 * @div: new clock divisor.
3395 * Configures the clock dividers. This is used to derive the best DAI bit and
3396 * frame clocks from the system or master clock. It's best to set the DAI bit
3397 * and frame clocks as low as possible to save system power.
3399 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3400 int div_id, int div)
3402 if (dai->driver && dai->driver->ops->set_clkdiv)
3403 return dai->driver->ops->set_clkdiv(dai, div_id, div);
3407 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3410 * snd_soc_dai_set_pll - configure DAI PLL.
3412 * @pll_id: DAI specific PLL ID
3413 * @source: DAI specific source for the PLL
3414 * @freq_in: PLL input clock frequency in Hz
3415 * @freq_out: requested PLL output clock frequency in Hz
3417 * Configures and enables PLL to generate output clock based on input clock.
3419 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3420 unsigned int freq_in, unsigned int freq_out)
3422 if (dai->driver && dai->driver->ops->set_pll)
3423 return dai->driver->ops->set_pll(dai, pll_id, source,
3425 else if (dai->codec && dai->codec->driver->set_pll)
3426 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3431 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3434 * snd_soc_codec_set_pll - configure codec PLL.
3436 * @pll_id: DAI specific PLL ID
3437 * @source: DAI specific source for the PLL
3438 * @freq_in: PLL input clock frequency in Hz
3439 * @freq_out: requested PLL output clock frequency in Hz
3441 * Configures and enables PLL to generate output clock based on input clock.
3443 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3444 unsigned int freq_in, unsigned int freq_out)
3446 if (codec->driver->set_pll)
3447 return codec->driver->set_pll(codec, pll_id, source,
3452 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3455 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3457 * @ratio Ratio of BCLK to Sample rate.
3459 * Configures the DAI for a preset BCLK to sample rate ratio.
3461 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3463 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3464 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3468 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3471 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3473 * @fmt: SND_SOC_DAIFMT_ format value.
3475 * Configures the DAI hardware format and clocking.
3477 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3479 if (dai->driver == NULL)
3481 if (dai->driver->ops->set_fmt == NULL)
3483 return dai->driver->ops->set_fmt(dai, fmt);
3485 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3488 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
3489 * @slots: Number of slots in use.
3490 * @tx_mask: bitmask representing active TX slots.
3491 * @rx_mask: bitmask representing active RX slots.
3493 * Generates the TDM tx and rx slot default masks for DAI.
3495 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
3496 unsigned int *tx_mask,
3497 unsigned int *rx_mask)
3499 if (*tx_mask || *rx_mask)
3505 *tx_mask = (1 << slots) - 1;
3506 *rx_mask = (1 << slots) - 1;
3512 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3514 * @tx_mask: bitmask representing active TX slots.
3515 * @rx_mask: bitmask representing active RX slots.
3516 * @slots: Number of slots in use.
3517 * @slot_width: Width in bits for each slot.
3519 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3522 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3523 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
3525 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3526 dai->driver->ops->xlate_tdm_slot_mask(slots,
3527 &tx_mask, &rx_mask);
3529 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
3531 dai->tx_mask = tx_mask;
3532 dai->rx_mask = rx_mask;
3534 if (dai->driver && dai->driver->ops->set_tdm_slot)
3535 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3540 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3543 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3545 * @tx_num: how many TX channels
3546 * @tx_slot: pointer to an array which imply the TX slot number channel
3548 * @rx_num: how many RX channels
3549 * @rx_slot: pointer to an array which imply the RX slot number channel
3552 * configure the relationship between channel number and TDM slot number.
3554 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3555 unsigned int tx_num, unsigned int *tx_slot,
3556 unsigned int rx_num, unsigned int *rx_slot)
3558 if (dai->driver && dai->driver->ops->set_channel_map)
3559 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3564 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3567 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3569 * @tristate: tristate enable
3571 * Tristates the DAI so that others can use it.
3573 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3575 if (dai->driver && dai->driver->ops->set_tristate)
3576 return dai->driver->ops->set_tristate(dai, tristate);
3580 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3583 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3585 * @mute: mute enable
3586 * @direction: stream to mute
3588 * Mutes the DAI DAC.
3590 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3596 if (dai->driver->ops->mute_stream)
3597 return dai->driver->ops->mute_stream(dai, mute, direction);
3598 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3599 dai->driver->ops->digital_mute)
3600 return dai->driver->ops->digital_mute(dai, mute);
3604 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3606 static int snd_soc_init_multicodec(struct snd_soc_card *card,
3607 struct snd_soc_dai_link *dai_link)
3609 /* Legacy codec/codec_dai link is a single entry in multicodec */
3610 if (dai_link->codec_name || dai_link->codec_of_node ||
3611 dai_link->codec_dai_name) {
3612 dai_link->num_codecs = 1;
3614 dai_link->codecs = devm_kzalloc(card->dev,
3615 sizeof(struct snd_soc_dai_link_component),
3617 if (!dai_link->codecs)
3620 dai_link->codecs[0].name = dai_link->codec_name;
3621 dai_link->codecs[0].of_node = dai_link->codec_of_node;
3622 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
3625 if (!dai_link->codecs) {
3626 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
3634 * snd_soc_register_card - Register a card with the ASoC core
3636 * @card: Card to register
3639 int snd_soc_register_card(struct snd_soc_card *card)
3643 if (!card->name || !card->dev)
3646 for (i = 0; i < card->num_links; i++) {
3647 struct snd_soc_dai_link *link = &card->dai_link[i];
3649 ret = snd_soc_init_multicodec(card, link);
3651 dev_err(card->dev, "ASoC: failed to init multicodec\n");
3655 for (j = 0; j < link->num_codecs; j++) {
3657 * Codec must be specified by 1 of name or OF node,
3658 * not both or neither.
3660 if (!!link->codecs[j].name ==
3661 !!link->codecs[j].of_node) {
3662 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
3666 /* Codec DAI name must be specified */
3667 if (!link->codecs[j].dai_name) {
3668 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
3675 * Platform may be specified by either name or OF node, but
3676 * can be left unspecified, and a dummy platform will be used.
3678 if (link->platform_name && link->platform_of_node) {
3680 "ASoC: Both platform name/of_node are set for %s\n",
3686 * CPU device may be specified by either name or OF node, but
3687 * can be left unspecified, and will be matched based on DAI
3690 if (link->cpu_name && link->cpu_of_node) {
3692 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3697 * At least one of CPU DAI name or CPU device name/node must be
3700 if (!link->cpu_dai_name &&
3701 !(link->cpu_name || link->cpu_of_node)) {
3703 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3709 dev_set_drvdata(card->dev, card);
3711 snd_soc_initialize_card_lists(card);
3713 soc_init_card_debugfs(card);
3715 card->rtd = devm_kzalloc(card->dev,
3716 sizeof(struct snd_soc_pcm_runtime) *
3717 (card->num_links + card->num_aux_devs),
3719 if (card->rtd == NULL)
3722 card->rtd_aux = &card->rtd[card->num_links];
3724 for (i = 0; i < card->num_links; i++) {
3725 card->rtd[i].card = card;
3726 card->rtd[i].dai_link = &card->dai_link[i];
3727 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
3728 sizeof(struct snd_soc_dai *) *
3729 (card->rtd[i].dai_link->num_codecs),
3731 if (card->rtd[i].codec_dais == NULL)
3735 for (i = 0; i < card->num_aux_devs; i++)
3736 card->rtd_aux[i].card = card;
3738 INIT_LIST_HEAD(&card->dapm_dirty);
3739 card->instantiated = 0;
3740 mutex_init(&card->mutex);
3741 mutex_init(&card->dapm_mutex);
3743 ret = snd_soc_instantiate_card(card);
3745 soc_cleanup_card_debugfs(card);
3747 /* deactivate pins to sleep state */
3748 for (i = 0; i < card->num_rtd; i++) {
3749 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
3750 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3753 for (j = 0; j < rtd->num_codecs; j++) {
3754 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
3755 if (!codec_dai->active)
3756 pinctrl_pm_select_sleep_state(codec_dai->dev);
3759 if (!cpu_dai->active)
3760 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3765 EXPORT_SYMBOL_GPL(snd_soc_register_card);
3768 * snd_soc_unregister_card - Unregister a card with the ASoC core
3770 * @card: Card to unregister
3773 int snd_soc_unregister_card(struct snd_soc_card *card)
3775 if (card->instantiated) {
3776 card->instantiated = false;
3777 snd_soc_dapm_shutdown(card);
3778 soc_cleanup_card_resources(card);
3780 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
3784 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3787 * Simplify DAI link configuration by removing ".-1" from device names
3788 * and sanitizing names.
3790 static char *fmt_single_name(struct device *dev, int *id)
3792 char *found, name[NAME_SIZE];
3795 if (dev_name(dev) == NULL)
3798 strlcpy(name, dev_name(dev), NAME_SIZE);
3800 /* are we a "%s.%d" name (platform and SPI components) */
3801 found = strstr(name, dev->driver->name);
3804 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3806 /* discard ID from name if ID == -1 */
3808 found[strlen(dev->driver->name)] = '\0';
3812 /* I2C component devices are named "bus-addr" */
3813 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3814 char tmp[NAME_SIZE];
3816 /* create unique ID number from I2C addr and bus */
3817 *id = ((id1 & 0xffff) << 16) + id2;
3819 /* sanitize component name for DAI link creation */
3820 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3821 strlcpy(name, tmp, NAME_SIZE);
3826 return kstrdup(name, GFP_KERNEL);
3830 * Simplify DAI link naming for single devices with multiple DAIs by removing
3831 * any ".-1" and using the DAI name (instead of device name).
3833 static inline char *fmt_multiple_name(struct device *dev,
3834 struct snd_soc_dai_driver *dai_drv)
3836 if (dai_drv->name == NULL) {
3838 "ASoC: error - multiple DAI %s registered with no name\n",
3843 return kstrdup(dai_drv->name, GFP_KERNEL);
3847 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
3849 * @component: The component for which the DAIs should be unregistered
3851 static void snd_soc_unregister_dais(struct snd_soc_component *component)
3853 struct snd_soc_dai *dai, *_dai;
3855 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
3856 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3858 list_del(&dai->list);
3865 * snd_soc_register_dais - Register a DAI with the ASoC core
3867 * @component: The component the DAIs are registered for
3868 * @dai_drv: DAI driver to use for the DAIs
3869 * @count: Number of DAIs
3870 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3873 static int snd_soc_register_dais(struct snd_soc_component *component,
3874 struct snd_soc_dai_driver *dai_drv, size_t count,
3875 bool legacy_dai_naming)
3877 struct device *dev = component->dev;
3878 struct snd_soc_dai *dai;
3882 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
3884 component->dai_drv = dai_drv;
3885 component->num_dai = count;
3887 for (i = 0; i < count; i++) {
3889 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3896 * Back in the old days when we still had component-less DAIs,
3897 * instead of having a static name, component-less DAIs would
3898 * inherit the name of the parent device so it is possible to
3899 * register multiple instances of the DAI. We still need to keep
3900 * the same naming style even though those DAIs are not
3901 * component-less anymore.
3903 if (count == 1 && legacy_dai_naming) {
3904 dai->name = fmt_single_name(dev, &dai->id);
3906 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3908 dai->id = dai_drv[i].id;
3912 if (dai->name == NULL) {
3918 dai->component = component;
3920 dai->driver = &dai_drv[i];
3921 if (!dai->driver->ops)
3922 dai->driver->ops = &null_dai_ops;
3924 list_add(&dai->list, &component->dai_list);
3926 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3932 snd_soc_unregister_dais(component);
3937 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3938 enum snd_soc_dapm_type type, int subseq)
3940 struct snd_soc_component *component = dapm->component;
3942 component->driver->seq_notifier(component, type, subseq);
3945 static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3948 struct snd_soc_component *component = dapm->component;
3950 return component->driver->stream_event(component, event);
3953 static int snd_soc_component_initialize(struct snd_soc_component *component,
3954 const struct snd_soc_component_driver *driver, struct device *dev)
3956 struct snd_soc_dapm_context *dapm;
3958 component->name = fmt_single_name(dev, &component->id);
3959 if (!component->name) {
3960 dev_err(dev, "ASoC: Failed to allocate name\n");
3964 component->dev = dev;
3965 component->driver = driver;
3966 component->probe = component->driver->probe;
3967 component->remove = component->driver->remove;
3969 if (!component->dapm_ptr)
3970 component->dapm_ptr = &component->dapm;
3972 dapm = component->dapm_ptr;
3974 dapm->component = component;
3975 dapm->bias_level = SND_SOC_BIAS_OFF;
3976 dapm->idle_bias_off = true;
3977 if (driver->seq_notifier)
3978 dapm->seq_notifier = snd_soc_component_seq_notifier;
3979 if (driver->stream_event)
3980 dapm->stream_event = snd_soc_component_stream_event;
3982 component->controls = driver->controls;
3983 component->num_controls = driver->num_controls;
3984 component->dapm_widgets = driver->dapm_widgets;
3985 component->num_dapm_widgets = driver->num_dapm_widgets;
3986 component->dapm_routes = driver->dapm_routes;
3987 component->num_dapm_routes = driver->num_dapm_routes;
3989 INIT_LIST_HEAD(&component->dai_list);
3990 mutex_init(&component->io_mutex);
3995 static void snd_soc_component_init_regmap(struct snd_soc_component *component)
3997 if (!component->regmap)
3998 component->regmap = dev_get_regmap(component->dev, NULL);
3999 if (component->regmap) {
4000 int val_bytes = regmap_get_val_bytes(component->regmap);
4001 /* Errors are legitimate for non-integer byte multiples */
4003 component->val_bytes = val_bytes;
4007 static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
4009 if (!component->write && !component->read)
4010 snd_soc_component_init_regmap(component);
4012 list_add(&component->list, &component_list);
4015 static void snd_soc_component_add(struct snd_soc_component *component)
4017 mutex_lock(&client_mutex);
4018 snd_soc_component_add_unlocked(component);
4019 mutex_unlock(&client_mutex);
4022 static void snd_soc_component_cleanup(struct snd_soc_component *component)
4024 snd_soc_unregister_dais(component);
4025 kfree(component->name);
4028 static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
4030 list_del(&component->list);
4033 static void snd_soc_component_del(struct snd_soc_component *component)
4035 mutex_lock(&client_mutex);
4036 snd_soc_component_del_unlocked(component);
4037 mutex_unlock(&client_mutex);
4040 int snd_soc_register_component(struct device *dev,
4041 const struct snd_soc_component_driver *cmpnt_drv,
4042 struct snd_soc_dai_driver *dai_drv,
4045 struct snd_soc_component *cmpnt;
4048 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
4050 dev_err(dev, "ASoC: Failed to allocate memory\n");
4054 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
4058 cmpnt->ignore_pmdown_time = true;
4059 cmpnt->registered_as_component = true;
4061 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
4063 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4067 snd_soc_component_add(cmpnt);
4072 snd_soc_component_cleanup(cmpnt);
4077 EXPORT_SYMBOL_GPL(snd_soc_register_component);
4080 * snd_soc_unregister_component - Unregister a component from the ASoC core
4083 void snd_soc_unregister_component(struct device *dev)
4085 struct snd_soc_component *cmpnt;
4087 list_for_each_entry(cmpnt, &component_list, list) {
4088 if (dev == cmpnt->dev && cmpnt->registered_as_component)
4094 snd_soc_component_del(cmpnt);
4095 snd_soc_component_cleanup(cmpnt);
4098 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4100 static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
4102 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4104 return platform->driver->probe(platform);
4107 static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
4109 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4111 platform->driver->remove(platform);
4115 * snd_soc_add_platform - Add a platform to the ASoC core
4116 * @dev: The parent device for the platform
4117 * @platform: The platform to add
4118 * @platform_driver: The driver for the platform
4120 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
4121 const struct snd_soc_platform_driver *platform_drv)
4125 ret = snd_soc_component_initialize(&platform->component,
4126 &platform_drv->component_driver, dev);
4130 platform->dev = dev;
4131 platform->driver = platform_drv;
4133 if (platform_drv->probe)
4134 platform->component.probe = snd_soc_platform_drv_probe;
4135 if (platform_drv->remove)
4136 platform->component.remove = snd_soc_platform_drv_remove;
4138 #ifdef CONFIG_DEBUG_FS
4139 platform->component.debugfs_prefix = "platform";
4142 mutex_lock(&client_mutex);
4143 snd_soc_component_add_unlocked(&platform->component);
4144 list_add(&platform->list, &platform_list);
4145 mutex_unlock(&client_mutex);
4147 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
4148 platform->component.name);
4152 EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4155 * snd_soc_register_platform - Register a platform with the ASoC core
4157 * @platform: platform to register
4159 int snd_soc_register_platform(struct device *dev,
4160 const struct snd_soc_platform_driver *platform_drv)
4162 struct snd_soc_platform *platform;
4165 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4167 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4168 if (platform == NULL)
4171 ret = snd_soc_add_platform(dev, platform, platform_drv);
4177 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4180 * snd_soc_remove_platform - Remove a platform from the ASoC core
4181 * @platform: the platform to remove
4183 void snd_soc_remove_platform(struct snd_soc_platform *platform)
4186 mutex_lock(&client_mutex);
4187 list_del(&platform->list);
4188 snd_soc_component_del_unlocked(&platform->component);
4189 mutex_unlock(&client_mutex);
4191 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
4192 platform->component.name);
4194 snd_soc_component_cleanup(&platform->component);
4196 EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4198 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4200 struct snd_soc_platform *platform;
4202 list_for_each_entry(platform, &platform_list, list) {
4203 if (dev == platform->dev)
4209 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4212 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4214 * @platform: platform to unregister
4216 void snd_soc_unregister_platform(struct device *dev)
4218 struct snd_soc_platform *platform;
4220 platform = snd_soc_lookup_platform(dev);
4224 snd_soc_remove_platform(platform);
4227 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4229 static u64 codec_format_map[] = {
4230 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4231 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4232 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4233 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4234 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4235 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4236 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4237 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4238 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4239 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4240 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4241 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4242 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4243 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4244 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4245 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4248 /* Fix up the DAI formats for endianness: codecs don't actually see
4249 * the endianness of the data but we're using the CPU format
4250 * definitions which do need to include endianness so we ensure that
4251 * codec DAIs always have both big and little endian variants set.
4253 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4257 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4258 if (stream->formats & codec_format_map[i])
4259 stream->formats |= codec_format_map[i];
4262 static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
4264 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4266 return codec->driver->probe(codec);
4269 static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
4271 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4273 codec->driver->remove(codec);
4276 static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4277 unsigned int reg, unsigned int val)
4279 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4281 return codec->driver->write(codec, reg, val);
4284 static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4285 unsigned int reg, unsigned int *val)
4287 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4289 *val = codec->driver->read(codec, reg);
4294 static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
4295 enum snd_soc_bias_level level)
4297 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
4299 return codec->driver->set_bias_level(codec, level);
4303 * snd_soc_register_codec - Register a codec with the ASoC core
4305 * @codec: codec to register
4307 int snd_soc_register_codec(struct device *dev,
4308 const struct snd_soc_codec_driver *codec_drv,
4309 struct snd_soc_dai_driver *dai_drv,
4312 struct snd_soc_codec *codec;
4313 struct snd_soc_dai *dai;
4316 dev_dbg(dev, "codec register %s\n", dev_name(dev));
4318 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4322 codec->component.dapm_ptr = &codec->dapm;
4323 codec->component.codec = codec;
4325 ret = snd_soc_component_initialize(&codec->component,
4326 &codec_drv->component_driver, dev);
4330 if (codec_drv->controls) {
4331 codec->component.controls = codec_drv->controls;
4332 codec->component.num_controls = codec_drv->num_controls;
4334 if (codec_drv->dapm_widgets) {
4335 codec->component.dapm_widgets = codec_drv->dapm_widgets;
4336 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
4338 if (codec_drv->dapm_routes) {
4339 codec->component.dapm_routes = codec_drv->dapm_routes;
4340 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
4343 if (codec_drv->probe)
4344 codec->component.probe = snd_soc_codec_drv_probe;
4345 if (codec_drv->remove)
4346 codec->component.remove = snd_soc_codec_drv_remove;
4347 if (codec_drv->write)
4348 codec->component.write = snd_soc_codec_drv_write;
4349 if (codec_drv->read)
4350 codec->component.read = snd_soc_codec_drv_read;
4351 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
4352 codec->dapm.idle_bias_off = codec_drv->idle_bias_off;
4353 codec->dapm.suspend_bias_off = codec_drv->suspend_bias_off;
4354 if (codec_drv->seq_notifier)
4355 codec->dapm.seq_notifier = codec_drv->seq_notifier;
4356 if (codec_drv->set_bias_level)
4357 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
4359 codec->driver = codec_drv;
4360 codec->component.val_bytes = codec_drv->reg_word_size;
4361 mutex_init(&codec->mutex);
4363 #ifdef CONFIG_DEBUG_FS
4364 codec->component.init_debugfs = soc_init_codec_debugfs;
4365 codec->component.debugfs_prefix = "codec";
4368 if (codec_drv->get_regmap)
4369 codec->component.regmap = codec_drv->get_regmap(dev);
4371 for (i = 0; i < num_dai; i++) {
4372 fixup_codec_formats(&dai_drv[i].playback);
4373 fixup_codec_formats(&dai_drv[i].capture);
4376 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4378 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4382 list_for_each_entry(dai, &codec->component.dai_list, list)
4385 mutex_lock(&client_mutex);
4386 snd_soc_component_add_unlocked(&codec->component);
4387 list_add(&codec->list, &codec_list);
4388 mutex_unlock(&client_mutex);
4390 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4391 codec->component.name);
4395 snd_soc_component_cleanup(&codec->component);
4400 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4403 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4405 * @codec: codec to unregister
4407 void snd_soc_unregister_codec(struct device *dev)
4409 struct snd_soc_codec *codec;
4411 list_for_each_entry(codec, &codec_list, list) {
4412 if (dev == codec->dev)
4419 mutex_lock(&client_mutex);
4420 list_del(&codec->list);
4421 snd_soc_component_del_unlocked(&codec->component);
4422 mutex_unlock(&client_mutex);
4424 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4425 codec->component.name);
4427 snd_soc_component_cleanup(&codec->component);
4428 snd_soc_cache_exit(codec);
4431 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4433 /* Retrieve a card's name from device tree */
4434 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4435 const char *propname)
4437 struct device_node *np;
4441 pr_err("card->dev is not set before calling %s\n", __func__);
4445 np = card->dev->of_node;
4447 ret = of_property_read_string_index(np, propname, 0, &card->name);
4449 * EINVAL means the property does not exist. This is fine providing
4450 * card->name was previously set, which is checked later in
4451 * snd_soc_register_card.
4453 if (ret < 0 && ret != -EINVAL) {
4455 "ASoC: Property '%s' could not be read: %d\n",
4462 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4464 static const struct snd_soc_dapm_widget simple_widgets[] = {
4465 SND_SOC_DAPM_MIC("Microphone", NULL),
4466 SND_SOC_DAPM_LINE("Line", NULL),
4467 SND_SOC_DAPM_HP("Headphone", NULL),
4468 SND_SOC_DAPM_SPK("Speaker", NULL),
4471 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4472 const char *propname)
4474 struct device_node *np = card->dev->of_node;
4475 struct snd_soc_dapm_widget *widgets;
4476 const char *template, *wname;
4477 int i, j, num_widgets, ret;
4479 num_widgets = of_property_count_strings(np, propname);
4480 if (num_widgets < 0) {
4482 "ASoC: Property '%s' does not exist\n", propname);
4485 if (num_widgets & 1) {
4487 "ASoC: Property '%s' length is not even\n", propname);
4493 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4498 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4502 "ASoC: Could not allocate memory for widgets\n");
4506 for (i = 0; i < num_widgets; i++) {
4507 ret = of_property_read_string_index(np, propname,
4511 "ASoC: Property '%s' index %d read error:%d\n",
4512 propname, 2 * i, ret);
4516 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4517 if (!strncmp(template, simple_widgets[j].name,
4518 strlen(simple_widgets[j].name))) {
4519 widgets[i] = simple_widgets[j];
4524 if (j >= ARRAY_SIZE(simple_widgets)) {
4526 "ASoC: DAPM widget '%s' is not supported\n",
4531 ret = of_property_read_string_index(np, propname,
4536 "ASoC: Property '%s' index %d read error:%d\n",
4537 propname, (2 * i) + 1, ret);
4541 widgets[i].name = wname;
4544 card->dapm_widgets = widgets;
4545 card->num_dapm_widgets = num_widgets;
4549 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4551 int snd_soc_of_parse_tdm_slot(struct device_node *np,
4552 unsigned int *slots,
4553 unsigned int *slot_width)
4558 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4559 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4567 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4568 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4578 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4580 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4581 const char *propname)
4583 struct device_node *np = card->dev->of_node;
4585 struct snd_soc_dapm_route *routes;
4588 num_routes = of_property_count_strings(np, propname);
4589 if (num_routes < 0 || num_routes & 1) {
4591 "ASoC: Property '%s' does not exist or its length is not even\n",
4597 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4602 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4606 "ASoC: Could not allocate DAPM route table\n");
4610 for (i = 0; i < num_routes; i++) {
4611 ret = of_property_read_string_index(np, propname,
4612 2 * i, &routes[i].sink);
4615 "ASoC: Property '%s' index %d could not be read: %d\n",
4616 propname, 2 * i, ret);
4619 ret = of_property_read_string_index(np, propname,
4620 (2 * i) + 1, &routes[i].source);
4623 "ASoC: Property '%s' index %d could not be read: %d\n",
4624 propname, (2 * i) + 1, ret);
4629 card->num_dapm_routes = num_routes;
4630 card->dapm_routes = routes;
4634 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4636 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4638 struct device_node **bitclkmaster,
4639 struct device_node **framemaster)
4643 unsigned int format = 0;
4649 } of_fmt_table[] = {
4650 { "i2s", SND_SOC_DAIFMT_I2S },
4651 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4652 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4653 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4654 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4655 { "ac97", SND_SOC_DAIFMT_AC97 },
4656 { "pdm", SND_SOC_DAIFMT_PDM},
4657 { "msb", SND_SOC_DAIFMT_MSB },
4658 { "lsb", SND_SOC_DAIFMT_LSB },
4665 * check "[prefix]format = xxx"
4666 * SND_SOC_DAIFMT_FORMAT_MASK area
4668 snprintf(prop, sizeof(prop), "%sformat", prefix);
4669 ret = of_property_read_string(np, prop, &str);
4671 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4672 if (strcmp(str, of_fmt_table[i].name) == 0) {
4673 format |= of_fmt_table[i].val;
4680 * check "[prefix]continuous-clock"
4681 * SND_SOC_DAIFMT_CLOCK_MASK area
4683 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4684 if (of_get_property(np, prop, NULL))
4685 format |= SND_SOC_DAIFMT_CONT;
4687 format |= SND_SOC_DAIFMT_GATED;
4690 * check "[prefix]bitclock-inversion"
4691 * check "[prefix]frame-inversion"
4692 * SND_SOC_DAIFMT_INV_MASK area
4694 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4695 bit = !!of_get_property(np, prop, NULL);
4697 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4698 frame = !!of_get_property(np, prop, NULL);
4700 switch ((bit << 4) + frame) {
4702 format |= SND_SOC_DAIFMT_IB_IF;
4705 format |= SND_SOC_DAIFMT_IB_NF;
4708 format |= SND_SOC_DAIFMT_NB_IF;
4711 /* SND_SOC_DAIFMT_NB_NF is default */
4716 * check "[prefix]bitclock-master"
4717 * check "[prefix]frame-master"
4718 * SND_SOC_DAIFMT_MASTER_MASK area
4720 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4721 bit = !!of_get_property(np, prop, NULL);
4722 if (bit && bitclkmaster)
4723 *bitclkmaster = of_parse_phandle(np, prop, 0);
4725 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4726 frame = !!of_get_property(np, prop, NULL);
4727 if (frame && framemaster)
4728 *framemaster = of_parse_phandle(np, prop, 0);
4730 switch ((bit << 4) + frame) {
4732 format |= SND_SOC_DAIFMT_CBM_CFM;
4735 format |= SND_SOC_DAIFMT_CBM_CFS;
4738 format |= SND_SOC_DAIFMT_CBS_CFM;
4741 format |= SND_SOC_DAIFMT_CBS_CFS;
4747 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4749 int snd_soc_of_get_dai_name(struct device_node *of_node,
4750 const char **dai_name)
4752 struct snd_soc_component *pos;
4753 struct of_phandle_args args;
4756 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4757 "#sound-dai-cells", 0, &args);
4761 ret = -EPROBE_DEFER;
4763 mutex_lock(&client_mutex);
4764 list_for_each_entry(pos, &component_list, list) {
4765 if (pos->dev->of_node != args.np)
4768 if (pos->driver->of_xlate_dai_name) {
4769 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4773 switch (args.args_count) {
4775 id = 0; /* same as dai_drv[0] */
4785 if (id < 0 || id >= pos->num_dai) {
4792 *dai_name = pos->dai_drv[id].name;
4794 *dai_name = pos->name;
4799 mutex_unlock(&client_mutex);
4801 of_node_put(args.np);
4805 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4807 static int __init snd_soc_init(void)
4809 #ifdef CONFIG_DEBUG_FS
4810 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4811 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
4812 pr_warn("ASoC: Failed to create debugfs directory\n");
4813 snd_soc_debugfs_root = NULL;
4816 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4818 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4820 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4822 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
4824 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
4825 &platform_list_fops))
4826 pr_warn("ASoC: Failed to create platform list debugfs file\n");
4829 snd_soc_util_init();
4831 return platform_driver_register(&soc_driver);
4833 module_init(snd_soc_init);
4835 static void __exit snd_soc_exit(void)
4837 snd_soc_util_exit();
4839 #ifdef CONFIG_DEBUG_FS
4840 debugfs_remove_recursive(snd_soc_debugfs_root);
4842 platform_driver_unregister(&soc_driver);
4844 module_exit(snd_soc_exit);
4846 /* Module information */
4847 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4848 MODULE_DESCRIPTION("ALSA SoC Core");
4849 MODULE_LICENSE("GPL");
4850 MODULE_ALIAS("platform:soc-audio");