2 * soc-core.c -- ALSA SoC Audio Layer
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
30 #include <linux/bitops.h>
31 #include <linux/debugfs.h>
32 #include <linux/platform_device.h>
33 #include <linux/pinctrl/consumer.h>
34 #include <linux/ctype.h>
35 #include <linux/slab.h>
37 #include <linux/gpio.h>
38 #include <linux/of_gpio.h>
39 #include <sound/ac97_codec.h>
40 #include <sound/core.h>
41 #include <sound/jack.h>
42 #include <sound/pcm.h>
43 #include <sound/pcm_params.h>
44 #include <sound/soc.h>
45 #include <sound/soc-dpcm.h>
46 #include <sound/initval.h>
48 #define CREATE_TRACE_POINTS
49 #include <trace/events/asoc.h>
53 #ifdef CONFIG_DEBUG_FS
54 struct dentry *snd_soc_debugfs_root;
55 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
58 static DEFINE_MUTEX(client_mutex);
59 static LIST_HEAD(platform_list);
60 static LIST_HEAD(codec_list);
61 static LIST_HEAD(component_list);
64 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
68 static int pmdown_time = 5000;
69 module_param(pmdown_time, int, 0);
70 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
72 struct snd_ac97_reset_cfg {
74 struct pinctrl_state *pstate_reset;
75 struct pinctrl_state *pstate_warm_reset;
76 struct pinctrl_state *pstate_run;
82 /* returns the minimum number of bytes needed to represent
83 * a particular given value */
84 static int min_bytes_needed(unsigned long val)
89 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
92 c = (sizeof val * 8) - c;
100 /* fill buf which is 'len' bytes with a formatted
101 * string of the form 'reg: value\n' */
102 static int format_register_str(struct snd_soc_codec *codec,
103 unsigned int reg, char *buf, size_t len)
105 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
106 int regsize = codec->driver->reg_word_size * 2;
108 char tmpbuf[len + 1];
109 char regbuf[regsize + 1];
111 /* since tmpbuf is allocated on the stack, warn the callers if they
112 * try to abuse this function */
115 /* +2 for ': ' and + 1 for '\n' */
116 if (wordsize + regsize + 2 + 1 != len)
119 ret = snd_soc_read(codec, reg);
121 memset(regbuf, 'X', regsize);
122 regbuf[regsize] = '\0';
124 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
127 /* prepare the buffer */
128 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
129 /* copy it back to the caller without the '\0' */
130 memcpy(buf, tmpbuf, len);
135 /* codec register dump */
136 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
137 size_t count, loff_t pos)
140 int wordsize, regsize;
145 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
146 regsize = codec->driver->reg_word_size * 2;
148 len = wordsize + regsize + 2 + 1;
150 if (!codec->driver->reg_cache_size)
153 if (codec->driver->reg_cache_step)
154 step = codec->driver->reg_cache_step;
156 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
157 /* only support larger than PAGE_SIZE bytes debugfs
158 * entries for the default case */
160 if (total + len >= count - 1)
162 format_register_str(codec, i, buf + total, len);
168 total = min(total, count - 1);
173 static ssize_t codec_reg_show(struct device *dev,
174 struct device_attribute *attr, char *buf)
176 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
178 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
181 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
183 static ssize_t pmdown_time_show(struct device *dev,
184 struct device_attribute *attr, char *buf)
186 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
188 return sprintf(buf, "%ld\n", rtd->pmdown_time);
191 static ssize_t pmdown_time_set(struct device *dev,
192 struct device_attribute *attr,
193 const char *buf, size_t count)
195 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
198 ret = kstrtol(buf, 10, &rtd->pmdown_time);
205 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
207 #ifdef CONFIG_DEBUG_FS
208 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
209 size_t count, loff_t *ppos)
212 struct snd_soc_codec *codec = file->private_data;
215 if (*ppos < 0 || !count)
218 buf = kmalloc(count, GFP_KERNEL);
222 ret = soc_codec_reg_show(codec, buf, count, *ppos);
224 if (copy_to_user(user_buf, buf, ret)) {
235 static ssize_t codec_reg_write_file(struct file *file,
236 const char __user *user_buf, size_t count, loff_t *ppos)
241 unsigned long reg, value;
242 struct snd_soc_codec *codec = file->private_data;
245 buf_size = min(count, (sizeof(buf)-1));
246 if (copy_from_user(buf, user_buf, buf_size))
250 while (*start == ' ')
252 reg = simple_strtoul(start, &start, 16);
253 while (*start == ' ')
255 ret = kstrtoul(start, 16, &value);
259 /* Userspace has been fiddling around behind the kernel's back */
260 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
262 snd_soc_write(codec, reg, value);
266 static const struct file_operations codec_reg_fops = {
268 .read = codec_reg_read_file,
269 .write = codec_reg_write_file,
270 .llseek = default_llseek,
273 static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
275 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
277 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
279 if (!codec->debugfs_codec_root) {
281 "ASoC: Failed to create codec debugfs directory\n");
285 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
287 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
290 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
291 codec->debugfs_codec_root,
292 codec, &codec_reg_fops);
293 if (!codec->debugfs_reg)
295 "ASoC: Failed to create codec register debugfs file\n");
297 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
300 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
302 debugfs_remove_recursive(codec->debugfs_codec_root);
305 static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
307 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
309 platform->debugfs_platform_root = debugfs_create_dir(platform->name,
311 if (!platform->debugfs_platform_root) {
312 dev_warn(platform->dev,
313 "ASoC: Failed to create platform debugfs directory\n");
317 snd_soc_dapm_debugfs_init(&platform->dapm,
318 platform->debugfs_platform_root);
321 static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
323 debugfs_remove_recursive(platform->debugfs_platform_root);
326 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
327 size_t count, loff_t *ppos)
329 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
330 ssize_t len, ret = 0;
331 struct snd_soc_codec *codec;
336 list_for_each_entry(codec, &codec_list, list) {
337 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
341 if (ret > PAGE_SIZE) {
348 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
355 static const struct file_operations codec_list_fops = {
356 .read = codec_list_read_file,
357 .llseek = default_llseek,/* read accesses f_pos */
360 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
361 size_t count, loff_t *ppos)
363 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
364 ssize_t len, ret = 0;
365 struct snd_soc_component *component;
366 struct snd_soc_dai *dai;
371 list_for_each_entry(component, &component_list, list) {
372 list_for_each_entry(dai, &component->dai_list, list) {
373 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
377 if (ret > PAGE_SIZE) {
384 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
391 static const struct file_operations dai_list_fops = {
392 .read = dai_list_read_file,
393 .llseek = default_llseek,/* read accesses f_pos */
396 static ssize_t platform_list_read_file(struct file *file,
397 char __user *user_buf,
398 size_t count, loff_t *ppos)
400 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
401 ssize_t len, ret = 0;
402 struct snd_soc_platform *platform;
407 list_for_each_entry(platform, &platform_list, list) {
408 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
412 if (ret > PAGE_SIZE) {
418 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
425 static const struct file_operations platform_list_fops = {
426 .read = platform_list_read_file,
427 .llseek = default_llseek,/* read accesses f_pos */
430 static void soc_init_card_debugfs(struct snd_soc_card *card)
432 card->debugfs_card_root = debugfs_create_dir(card->name,
433 snd_soc_debugfs_root);
434 if (!card->debugfs_card_root) {
436 "ASoC: Failed to create card debugfs directory\n");
440 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
441 card->debugfs_card_root,
443 if (!card->debugfs_pop_time)
445 "ASoC: Failed to create pop time debugfs file\n");
448 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
450 debugfs_remove_recursive(card->debugfs_card_root);
455 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
459 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
463 static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
467 static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
471 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
475 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
480 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
481 const char *dai_link, int stream)
485 for (i = 0; i < card->num_links; i++) {
486 if (card->rtd[i].dai_link->no_pcm &&
487 !strcmp(card->rtd[i].dai_link->name, dai_link))
488 return card->rtd[i].pcm->streams[stream].substream;
490 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
493 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
495 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
496 const char *dai_link)
500 for (i = 0; i < card->num_links; i++) {
501 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
502 return &card->rtd[i];
504 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
507 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
509 #ifdef CONFIG_SND_SOC_AC97_BUS
510 /* unregister ac97 codec */
511 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
513 if (codec->ac97->dev.bus)
514 device_unregister(&codec->ac97->dev);
518 /* stop no dev release warning */
519 static void soc_ac97_device_release(struct device *dev){}
521 /* register ac97 codec to bus */
522 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
526 codec->ac97->dev.bus = &ac97_bus_type;
527 codec->ac97->dev.parent = codec->card->dev;
528 codec->ac97->dev.release = soc_ac97_device_release;
530 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
531 codec->card->snd_card->number, 0, codec->name);
532 err = device_register(&codec->ac97->dev);
534 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
535 codec->ac97->dev.bus = NULL;
542 static void codec2codec_close_delayed_work(struct work_struct *work)
544 /* Currently nothing to do for c2c links
545 * Since c2c links are internal nodes in the DAPM graph and
546 * don't interface with the outside world or application layer
547 * we don't have to do any special handling on close.
551 #ifdef CONFIG_PM_SLEEP
552 /* powers down audio subsystem for suspend */
553 int snd_soc_suspend(struct device *dev)
555 struct snd_soc_card *card = dev_get_drvdata(dev);
556 struct snd_soc_codec *codec;
559 /* If the initialization of this soc device failed, there is no codec
560 * associated with it. Just bail out in this case.
562 if (list_empty(&card->codec_dev_list))
565 /* Due to the resume being scheduled into a workqueue we could
566 * suspend before that's finished - wait for it to complete.
568 snd_power_lock(card->snd_card);
569 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
570 snd_power_unlock(card->snd_card);
572 /* we're going to block userspace touching us until resume completes */
573 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
575 /* mute any active DACs */
576 for (i = 0; i < card->num_rtd; i++) {
577 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
578 struct snd_soc_dai_driver *drv = dai->driver;
580 if (card->rtd[i].dai_link->ignore_suspend)
583 if (drv->ops->digital_mute && dai->playback_active)
584 drv->ops->digital_mute(dai, 1);
587 /* suspend all pcms */
588 for (i = 0; i < card->num_rtd; i++) {
589 if (card->rtd[i].dai_link->ignore_suspend)
592 snd_pcm_suspend_all(card->rtd[i].pcm);
595 if (card->suspend_pre)
596 card->suspend_pre(card);
598 for (i = 0; i < card->num_rtd; i++) {
599 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
600 struct snd_soc_platform *platform = card->rtd[i].platform;
602 if (card->rtd[i].dai_link->ignore_suspend)
605 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
606 cpu_dai->driver->suspend(cpu_dai);
607 if (platform->driver->suspend && !platform->suspended) {
608 platform->driver->suspend(cpu_dai);
609 platform->suspended = 1;
613 /* close any waiting streams and save state */
614 for (i = 0; i < card->num_rtd; i++) {
615 flush_delayed_work(&card->rtd[i].delayed_work);
616 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
619 for (i = 0; i < card->num_rtd; i++) {
621 if (card->rtd[i].dai_link->ignore_suspend)
624 snd_soc_dapm_stream_event(&card->rtd[i],
625 SNDRV_PCM_STREAM_PLAYBACK,
626 SND_SOC_DAPM_STREAM_SUSPEND);
628 snd_soc_dapm_stream_event(&card->rtd[i],
629 SNDRV_PCM_STREAM_CAPTURE,
630 SND_SOC_DAPM_STREAM_SUSPEND);
633 /* Recheck all analogue paths too */
634 dapm_mark_io_dirty(&card->dapm);
635 snd_soc_dapm_sync(&card->dapm);
637 /* suspend all CODECs */
638 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
639 /* If there are paths active then the CODEC will be held with
640 * bias _ON and should not be suspended. */
641 if (!codec->suspended && codec->driver->suspend) {
642 switch (codec->dapm.bias_level) {
643 case SND_SOC_BIAS_STANDBY:
645 * If the CODEC is capable of idle
646 * bias off then being in STANDBY
647 * means it's doing something,
648 * otherwise fall through.
650 if (codec->dapm.idle_bias_off) {
652 "ASoC: idle_bias_off CODEC on over suspend\n");
655 case SND_SOC_BIAS_OFF:
656 codec->driver->suspend(codec);
657 codec->suspended = 1;
658 codec->cache_sync = 1;
659 if (codec->using_regmap)
660 regcache_mark_dirty(codec->control_data);
661 /* deactivate pins to sleep state */
662 pinctrl_pm_select_sleep_state(codec->dev);
666 "ASoC: CODEC is on over suspend\n");
672 for (i = 0; i < card->num_rtd; i++) {
673 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
675 if (card->rtd[i].dai_link->ignore_suspend)
678 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
679 cpu_dai->driver->suspend(cpu_dai);
681 /* deactivate pins to sleep state */
682 pinctrl_pm_select_sleep_state(cpu_dai->dev);
685 if (card->suspend_post)
686 card->suspend_post(card);
690 EXPORT_SYMBOL_GPL(snd_soc_suspend);
692 /* deferred resume work, so resume can complete before we finished
693 * setting our codec back up, which can be very slow on I2C
695 static void soc_resume_deferred(struct work_struct *work)
697 struct snd_soc_card *card =
698 container_of(work, struct snd_soc_card, deferred_resume_work);
699 struct snd_soc_codec *codec;
702 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
703 * so userspace apps are blocked from touching us
706 dev_dbg(card->dev, "ASoC: starting resume work\n");
708 /* Bring us up into D2 so that DAPM starts enabling things */
709 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
711 if (card->resume_pre)
712 card->resume_pre(card);
714 /* resume AC97 DAIs */
715 for (i = 0; i < card->num_rtd; i++) {
716 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
718 if (card->rtd[i].dai_link->ignore_suspend)
721 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
722 cpu_dai->driver->resume(cpu_dai);
725 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
726 /* If the CODEC was idle over suspend then it will have been
727 * left with bias OFF or STANDBY and suspended so we must now
728 * resume. Otherwise the suspend was suppressed.
730 if (codec->driver->resume && codec->suspended) {
731 switch (codec->dapm.bias_level) {
732 case SND_SOC_BIAS_STANDBY:
733 case SND_SOC_BIAS_OFF:
734 codec->driver->resume(codec);
735 codec->suspended = 0;
739 "ASoC: CODEC was on over suspend\n");
745 for (i = 0; i < card->num_rtd; i++) {
747 if (card->rtd[i].dai_link->ignore_suspend)
750 snd_soc_dapm_stream_event(&card->rtd[i],
751 SNDRV_PCM_STREAM_PLAYBACK,
752 SND_SOC_DAPM_STREAM_RESUME);
754 snd_soc_dapm_stream_event(&card->rtd[i],
755 SNDRV_PCM_STREAM_CAPTURE,
756 SND_SOC_DAPM_STREAM_RESUME);
759 /* unmute any active DACs */
760 for (i = 0; i < card->num_rtd; i++) {
761 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
762 struct snd_soc_dai_driver *drv = dai->driver;
764 if (card->rtd[i].dai_link->ignore_suspend)
767 if (drv->ops->digital_mute && dai->playback_active)
768 drv->ops->digital_mute(dai, 0);
771 for (i = 0; i < card->num_rtd; i++) {
772 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
773 struct snd_soc_platform *platform = card->rtd[i].platform;
775 if (card->rtd[i].dai_link->ignore_suspend)
778 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
779 cpu_dai->driver->resume(cpu_dai);
780 if (platform->driver->resume && platform->suspended) {
781 platform->driver->resume(cpu_dai);
782 platform->suspended = 0;
786 if (card->resume_post)
787 card->resume_post(card);
789 dev_dbg(card->dev, "ASoC: resume work completed\n");
791 /* userspace can access us now we are back as we were before */
792 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
794 /* Recheck all analogue paths too */
795 dapm_mark_io_dirty(&card->dapm);
796 snd_soc_dapm_sync(&card->dapm);
799 /* powers up audio subsystem after a suspend */
800 int snd_soc_resume(struct device *dev)
802 struct snd_soc_card *card = dev_get_drvdata(dev);
803 int i, ac97_control = 0;
805 /* If the initialization of this soc device failed, there is no codec
806 * associated with it. Just bail out in this case.
808 if (list_empty(&card->codec_dev_list))
811 /* activate pins from sleep state */
812 for (i = 0; i < card->num_rtd; i++) {
813 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
814 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
816 pinctrl_pm_select_default_state(cpu_dai->dev);
817 if (codec_dai->active)
818 pinctrl_pm_select_default_state(codec_dai->dev);
821 /* AC97 devices might have other drivers hanging off them so
822 * need to resume immediately. Other drivers don't have that
823 * problem and may take a substantial amount of time to resume
824 * due to I/O costs and anti-pop so handle them out of line.
826 for (i = 0; i < card->num_rtd; i++) {
827 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
828 ac97_control |= cpu_dai->driver->ac97_control;
831 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
832 soc_resume_deferred(&card->deferred_resume_work);
834 dev_dbg(dev, "ASoC: Scheduling resume work\n");
835 if (!schedule_work(&card->deferred_resume_work))
836 dev_err(dev, "ASoC: resume work item may be lost\n");
841 EXPORT_SYMBOL_GPL(snd_soc_resume);
843 #define snd_soc_suspend NULL
844 #define snd_soc_resume NULL
847 static const struct snd_soc_dai_ops null_dai_ops = {
850 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
852 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
853 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
854 struct snd_soc_component *component;
855 struct snd_soc_codec *codec;
856 struct snd_soc_platform *platform;
857 struct snd_soc_dai *codec_dai, *cpu_dai;
858 const char *platform_name;
860 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
862 /* Find CPU DAI from registered DAIs*/
863 list_for_each_entry(component, &component_list, list) {
864 if (dai_link->cpu_of_node &&
865 component->dev->of_node != dai_link->cpu_of_node)
867 if (dai_link->cpu_name &&
868 strcmp(dev_name(component->dev), dai_link->cpu_name))
870 list_for_each_entry(cpu_dai, &component->dai_list, list) {
871 if (dai_link->cpu_dai_name &&
872 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
875 rtd->cpu_dai = cpu_dai;
880 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
881 dai_link->cpu_dai_name);
882 return -EPROBE_DEFER;
885 /* Find CODEC from registered CODECs */
886 list_for_each_entry(codec, &codec_list, list) {
887 if (dai_link->codec_of_node) {
888 if (codec->dev->of_node != dai_link->codec_of_node)
891 if (strcmp(codec->name, dai_link->codec_name))
898 * CODEC found, so find CODEC DAI from registered DAIs from
901 list_for_each_entry(codec_dai, &codec->component.dai_list, list) {
902 if (!strcmp(codec_dai->name, dai_link->codec_dai_name)) {
903 rtd->codec_dai = codec_dai;
908 if (!rtd->codec_dai) {
909 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
910 dai_link->codec_dai_name);
911 return -EPROBE_DEFER;
916 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
917 dai_link->codec_name);
918 return -EPROBE_DEFER;
921 /* if there's no platform we match on the empty platform */
922 platform_name = dai_link->platform_name;
923 if (!platform_name && !dai_link->platform_of_node)
924 platform_name = "snd-soc-dummy";
926 /* find one from the set of registered platforms */
927 list_for_each_entry(platform, &platform_list, list) {
928 if (dai_link->platform_of_node) {
929 if (platform->dev->of_node !=
930 dai_link->platform_of_node)
933 if (strcmp(platform->name, platform_name))
937 rtd->platform = platform;
939 if (!rtd->platform) {
940 dev_err(card->dev, "ASoC: platform %s not registered\n",
941 dai_link->platform_name);
942 return -EPROBE_DEFER;
950 static int soc_remove_platform(struct snd_soc_platform *platform)
954 if (platform->driver->remove) {
955 ret = platform->driver->remove(platform);
957 dev_err(platform->dev, "ASoC: failed to remove %d\n",
961 /* Make sure all DAPM widgets are freed */
962 snd_soc_dapm_free(&platform->dapm);
964 soc_cleanup_platform_debugfs(platform);
965 platform->probed = 0;
966 list_del(&platform->card_list);
967 module_put(platform->dev->driver->owner);
972 static void soc_remove_codec(struct snd_soc_codec *codec)
976 if (codec->driver->remove) {
977 err = codec->driver->remove(codec);
979 dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
982 /* Make sure all DAPM widgets are freed */
983 snd_soc_dapm_free(&codec->dapm);
985 soc_cleanup_codec_debugfs(codec);
987 list_del(&codec->card_list);
988 module_put(codec->dev->driver->owner);
991 static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
993 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
994 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
997 /* unregister the rtd device */
998 if (rtd->dev_registered) {
999 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1000 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1001 device_unregister(rtd->dev);
1002 rtd->dev_registered = 0;
1005 /* remove the CODEC DAI */
1006 if (codec_dai && codec_dai->probed &&
1007 codec_dai->driver->remove_order == order) {
1008 if (codec_dai->driver->remove) {
1009 err = codec_dai->driver->remove(codec_dai);
1011 dev_err(codec_dai->dev,
1012 "ASoC: failed to remove %s: %d\n",
1013 codec_dai->name, err);
1015 codec_dai->probed = 0;
1016 list_del(&codec_dai->card_list);
1019 /* remove the cpu_dai */
1020 if (cpu_dai && cpu_dai->probed &&
1021 cpu_dai->driver->remove_order == order) {
1022 if (cpu_dai->driver->remove) {
1023 err = cpu_dai->driver->remove(cpu_dai);
1025 dev_err(cpu_dai->dev,
1026 "ASoC: failed to remove %s: %d\n",
1027 cpu_dai->name, err);
1029 cpu_dai->probed = 0;
1030 list_del(&cpu_dai->card_list);
1032 if (!cpu_dai->codec) {
1033 snd_soc_dapm_free(&cpu_dai->dapm);
1034 module_put(cpu_dai->dev->driver->owner);
1039 static void soc_remove_link_components(struct snd_soc_card *card, int num,
1042 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1043 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1044 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1045 struct snd_soc_platform *platform = rtd->platform;
1046 struct snd_soc_codec *codec;
1048 /* remove the platform */
1049 if (platform && platform->probed &&
1050 platform->driver->remove_order == order) {
1051 soc_remove_platform(platform);
1054 /* remove the CODEC-side CODEC */
1056 codec = codec_dai->codec;
1057 if (codec && codec->probed &&
1058 codec->driver->remove_order == order)
1059 soc_remove_codec(codec);
1062 /* remove any CPU-side CODEC */
1064 codec = cpu_dai->codec;
1065 if (codec && codec->probed &&
1066 codec->driver->remove_order == order)
1067 soc_remove_codec(codec);
1071 static void soc_remove_dai_links(struct snd_soc_card *card)
1075 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1077 for (dai = 0; dai < card->num_rtd; dai++)
1078 soc_remove_link_dais(card, dai, order);
1081 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1083 for (dai = 0; dai < card->num_rtd; dai++)
1084 soc_remove_link_components(card, dai, order);
1090 static void soc_set_name_prefix(struct snd_soc_card *card,
1091 struct snd_soc_codec *codec)
1095 if (card->codec_conf == NULL)
1098 for (i = 0; i < card->num_configs; i++) {
1099 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1100 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1101 codec->name_prefix = map->name_prefix;
1107 static int soc_probe_codec(struct snd_soc_card *card,
1108 struct snd_soc_codec *codec)
1111 const struct snd_soc_codec_driver *driver = codec->driver;
1112 struct snd_soc_dai *dai;
1115 codec->dapm.card = card;
1116 soc_set_name_prefix(card, codec);
1118 if (!try_module_get(codec->dev->driver->owner))
1121 soc_init_codec_debugfs(codec);
1123 if (driver->dapm_widgets) {
1124 ret = snd_soc_dapm_new_controls(&codec->dapm,
1125 driver->dapm_widgets,
1126 driver->num_dapm_widgets);
1130 "Failed to create new controls %d\n", ret);
1135 /* Create DAPM widgets for each DAI stream */
1136 list_for_each_entry(dai, &codec->component.dai_list, list) {
1137 ret = snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1141 "Failed to create DAI widgets %d\n", ret);
1146 codec->dapm.idle_bias_off = driver->idle_bias_off;
1148 if (!codec->write && dev_get_regmap(codec->dev, NULL)) {
1149 /* Set the default I/O up try regmap */
1150 ret = snd_soc_codec_set_cache_io(codec, NULL);
1153 "Failed to set cache I/O: %d\n", ret);
1158 if (driver->probe) {
1159 ret = driver->probe(codec);
1162 "ASoC: failed to probe CODEC %d\n", ret);
1165 WARN(codec->dapm.idle_bias_off &&
1166 codec->dapm.bias_level != SND_SOC_BIAS_OFF,
1167 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1171 if (driver->controls)
1172 snd_soc_add_codec_controls(codec, driver->controls,
1173 driver->num_controls);
1174 if (driver->dapm_routes)
1175 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1176 driver->num_dapm_routes);
1178 /* mark codec as probed and add to card codec list */
1180 list_add(&codec->card_list, &card->codec_dev_list);
1181 list_add(&codec->dapm.list, &card->dapm_list);
1186 soc_cleanup_codec_debugfs(codec);
1187 module_put(codec->dev->driver->owner);
1192 static int soc_probe_platform(struct snd_soc_card *card,
1193 struct snd_soc_platform *platform)
1196 const struct snd_soc_platform_driver *driver = platform->driver;
1197 struct snd_soc_component *component;
1198 struct snd_soc_dai *dai;
1200 platform->card = card;
1201 platform->dapm.card = card;
1203 if (!try_module_get(platform->dev->driver->owner))
1206 soc_init_platform_debugfs(platform);
1208 if (driver->dapm_widgets)
1209 snd_soc_dapm_new_controls(&platform->dapm,
1210 driver->dapm_widgets, driver->num_dapm_widgets);
1212 /* Create DAPM widgets for each DAI stream */
1213 list_for_each_entry(component, &component_list, list) {
1214 if (component->dev != platform->dev)
1216 list_for_each_entry(dai, &component->dai_list, list)
1217 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1220 platform->dapm.idle_bias_off = 1;
1222 if (driver->probe) {
1223 ret = driver->probe(platform);
1225 dev_err(platform->dev,
1226 "ASoC: failed to probe platform %d\n", ret);
1231 if (driver->controls)
1232 snd_soc_add_platform_controls(platform, driver->controls,
1233 driver->num_controls);
1234 if (driver->dapm_routes)
1235 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1236 driver->num_dapm_routes);
1238 /* mark platform as probed and add to card platform list */
1239 platform->probed = 1;
1240 list_add(&platform->card_list, &card->platform_dev_list);
1241 list_add(&platform->dapm.list, &card->dapm_list);
1246 soc_cleanup_platform_debugfs(platform);
1247 module_put(platform->dev->driver->owner);
1252 static void rtd_release(struct device *dev)
1257 static int soc_post_component_init(struct snd_soc_card *card,
1258 struct snd_soc_codec *codec,
1259 int num, int dailess)
1261 struct snd_soc_dai_link *dai_link = NULL;
1262 struct snd_soc_aux_dev *aux_dev = NULL;
1263 struct snd_soc_pcm_runtime *rtd;
1268 dai_link = &card->dai_link[num];
1269 rtd = &card->rtd[num];
1270 name = dai_link->name;
1272 aux_dev = &card->aux_dev[num];
1273 rtd = &card->rtd_aux[num];
1274 name = aux_dev->name;
1278 /* do machine specific initialization */
1279 if (!dailess && dai_link->init)
1280 ret = dai_link->init(rtd);
1281 else if (dailess && aux_dev->init)
1282 ret = aux_dev->init(&codec->dapm);
1284 dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
1288 /* register the rtd device */
1291 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1294 device_initialize(rtd->dev);
1295 rtd->dev->parent = card->dev;
1296 rtd->dev->release = rtd_release;
1297 rtd->dev->init_name = name;
1298 dev_set_drvdata(rtd->dev, rtd);
1299 mutex_init(&rtd->pcm_mutex);
1300 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1301 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1302 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1303 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1304 ret = device_add(rtd->dev);
1306 /* calling put_device() here to free the rtd->dev */
1307 put_device(rtd->dev);
1309 "ASoC: failed to register runtime device: %d\n", ret);
1312 rtd->dev_registered = 1;
1314 /* add DAPM sysfs entries for this codec */
1315 ret = snd_soc_dapm_sys_add(rtd->dev);
1318 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
1320 /* add codec sysfs entries */
1321 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1324 "ASoC: failed to add codec sysfs files: %d\n", ret);
1326 #ifdef CONFIG_DEBUG_FS
1327 /* add DPCM sysfs entries */
1328 if (!dailess && !dai_link->dynamic)
1331 ret = soc_dpcm_debugfs_add(rtd);
1333 dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
1340 static int soc_probe_link_components(struct snd_soc_card *card, int num,
1343 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1344 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1345 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1346 struct snd_soc_platform *platform = rtd->platform;
1349 /* probe the CPU-side component, if it is a CODEC */
1350 if (cpu_dai->codec &&
1351 !cpu_dai->codec->probed &&
1352 cpu_dai->codec->driver->probe_order == order) {
1353 ret = soc_probe_codec(card, cpu_dai->codec);
1358 /* probe the CODEC-side component */
1359 if (!codec_dai->codec->probed &&
1360 codec_dai->codec->driver->probe_order == order) {
1361 ret = soc_probe_codec(card, codec_dai->codec);
1366 /* probe the platform */
1367 if (!platform->probed &&
1368 platform->driver->probe_order == order) {
1369 ret = soc_probe_platform(card, platform);
1377 static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1379 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1380 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1381 struct snd_soc_codec *codec = rtd->codec;
1382 struct snd_soc_platform *platform = rtd->platform;
1383 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1384 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1385 struct snd_soc_dapm_widget *play_w, *capture_w;
1388 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1389 card->name, num, order);
1391 /* config components */
1392 cpu_dai->platform = platform;
1393 codec_dai->card = card;
1394 cpu_dai->card = card;
1396 /* set default power off timeout */
1397 rtd->pmdown_time = pmdown_time;
1399 /* probe the cpu_dai */
1400 if (!cpu_dai->probed &&
1401 cpu_dai->driver->probe_order == order) {
1402 if (!cpu_dai->codec) {
1403 cpu_dai->dapm.card = card;
1404 if (!try_module_get(cpu_dai->dev->driver->owner))
1407 list_add(&cpu_dai->dapm.list, &card->dapm_list);
1410 if (cpu_dai->driver->probe) {
1411 ret = cpu_dai->driver->probe(cpu_dai);
1413 dev_err(cpu_dai->dev,
1414 "ASoC: failed to probe CPU DAI %s: %d\n",
1415 cpu_dai->name, ret);
1416 module_put(cpu_dai->dev->driver->owner);
1420 cpu_dai->probed = 1;
1421 /* mark cpu_dai as probed and add to card dai list */
1422 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1425 /* probe the CODEC DAI */
1426 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1427 if (codec_dai->driver->probe) {
1428 ret = codec_dai->driver->probe(codec_dai);
1430 dev_err(codec_dai->dev,
1431 "ASoC: failed to probe CODEC DAI %s: %d\n",
1432 codec_dai->name, ret);
1437 /* mark codec_dai as probed and add to card dai list */
1438 codec_dai->probed = 1;
1439 list_add(&codec_dai->card_list, &card->dai_dev_list);
1442 /* complete DAI probe during last probe */
1443 if (order != SND_SOC_COMP_ORDER_LAST)
1446 ret = soc_post_component_init(card, codec, num, 0);
1450 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1452 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1455 if (cpu_dai->driver->compress_dai) {
1456 /*create compress_device"*/
1457 ret = soc_new_compress(rtd, num);
1459 dev_err(card->dev, "ASoC: can't create compress %s\n",
1460 dai_link->stream_name);
1465 if (!dai_link->params) {
1466 /* create the pcm */
1467 ret = soc_new_pcm(rtd, num);
1469 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1470 dai_link->stream_name, ret);
1474 INIT_DELAYED_WORK(&rtd->delayed_work,
1475 codec2codec_close_delayed_work);
1477 /* link the DAI widgets */
1478 play_w = codec_dai->playback_widget;
1479 capture_w = cpu_dai->capture_widget;
1480 if (play_w && capture_w) {
1481 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1484 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1485 play_w->name, capture_w->name, ret);
1490 play_w = cpu_dai->playback_widget;
1491 capture_w = codec_dai->capture_widget;
1492 if (play_w && capture_w) {
1493 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1496 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1497 play_w->name, capture_w->name, ret);
1504 /* add platform data for AC97 devices */
1505 if (rtd->codec_dai->driver->ac97_control)
1506 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1511 #ifdef CONFIG_SND_SOC_AC97_BUS
1512 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1516 /* Only instantiate AC97 if not already done by the adaptor
1517 * for the generic AC97 subsystem.
1519 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
1521 * It is possible that the AC97 device is already registered to
1522 * the device subsystem. This happens when the device is created
1523 * via snd_ac97_mixer(). Currently only SoC codec that does so
1524 * is the generic AC97 glue but others migh emerge.
1526 * In those cases we don't try to register the device again.
1528 if (!rtd->codec->ac97_created)
1531 ret = soc_ac97_dev_register(rtd->codec);
1533 dev_err(rtd->codec->dev,
1534 "ASoC: AC97 device register failed: %d\n", ret);
1538 rtd->codec->ac97_registered = 1;
1543 static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1545 if (codec->ac97_registered) {
1546 soc_ac97_dev_unregister(codec);
1547 codec->ac97_registered = 0;
1552 static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1554 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1555 struct snd_soc_codec *codec;
1557 /* find CODEC from registered CODECs*/
1558 list_for_each_entry(codec, &codec_list, list) {
1559 if (!strcmp(codec->name, aux_dev->codec_name))
1563 dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
1565 return -EPROBE_DEFER;
1568 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1570 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1571 struct snd_soc_codec *codec;
1574 /* find CODEC from registered CODECs*/
1575 list_for_each_entry(codec, &codec_list, list) {
1576 if (!strcmp(codec->name, aux_dev->codec_name)) {
1577 if (codec->probed) {
1579 "ASoC: codec already probed");
1586 /* codec not found */
1587 dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
1588 return -EPROBE_DEFER;
1591 ret = soc_probe_codec(card, codec);
1595 ret = soc_post_component_init(card, codec, num, 1);
1601 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1603 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1604 struct snd_soc_codec *codec = rtd->codec;
1606 /* unregister the rtd device */
1607 if (rtd->dev_registered) {
1608 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1609 device_unregister(rtd->dev);
1610 rtd->dev_registered = 0;
1613 if (codec && codec->probed)
1614 soc_remove_codec(codec);
1617 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1621 if (codec->cache_init)
1624 ret = snd_soc_cache_init(codec);
1627 "ASoC: Failed to set cache compression type: %d\n",
1631 codec->cache_init = 1;
1635 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1637 struct snd_soc_codec *codec;
1638 struct snd_soc_dai_link *dai_link;
1639 int ret, i, order, dai_fmt;
1641 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1644 for (i = 0; i < card->num_links; i++) {
1645 ret = soc_bind_dai_link(card, i);
1650 /* check aux_devs too */
1651 for (i = 0; i < card->num_aux_devs; i++) {
1652 ret = soc_check_aux_dev(card, i);
1657 /* initialize the register cache for each available codec */
1658 list_for_each_entry(codec, &codec_list, list) {
1659 if (codec->cache_init)
1661 ret = snd_soc_init_codec_cache(codec);
1666 /* card bind complete so register a sound card */
1667 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1668 card->owner, 0, &card->snd_card);
1671 "ASoC: can't create sound card for card %s: %d\n",
1676 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1677 card->dapm.dev = card->dev;
1678 card->dapm.card = card;
1679 list_add(&card->dapm.list, &card->dapm_list);
1681 #ifdef CONFIG_DEBUG_FS
1682 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1685 #ifdef CONFIG_PM_SLEEP
1686 /* deferred resume work */
1687 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1690 if (card->dapm_widgets)
1691 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1692 card->num_dapm_widgets);
1694 /* initialise the sound card only once */
1696 ret = card->probe(card);
1698 goto card_probe_error;
1701 /* probe all components used by DAI links on this card */
1702 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1704 for (i = 0; i < card->num_links; i++) {
1705 ret = soc_probe_link_components(card, i, order);
1708 "ASoC: failed to instantiate card %d\n",
1715 /* probe all DAI links on this card */
1716 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1718 for (i = 0; i < card->num_links; i++) {
1719 ret = soc_probe_link_dais(card, i, order);
1722 "ASoC: failed to instantiate card %d\n",
1729 for (i = 0; i < card->num_aux_devs; i++) {
1730 ret = soc_probe_aux_dev(card, i);
1733 "ASoC: failed to add auxiliary devices %d\n",
1735 goto probe_aux_dev_err;
1739 snd_soc_dapm_link_dai_widgets(card);
1740 snd_soc_dapm_connect_dai_link_widgets(card);
1743 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1745 if (card->dapm_routes)
1746 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1747 card->num_dapm_routes);
1749 for (i = 0; i < card->num_links; i++) {
1750 dai_link = &card->dai_link[i];
1751 dai_fmt = dai_link->dai_fmt;
1754 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1756 if (ret != 0 && ret != -ENOTSUPP)
1757 dev_warn(card->rtd[i].codec_dai->dev,
1758 "ASoC: Failed to set DAI format: %d\n",
1762 /* If this is a regular CPU link there will be a platform */
1764 (dai_link->platform_name || dai_link->platform_of_node)) {
1765 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1767 if (ret != 0 && ret != -ENOTSUPP)
1768 dev_warn(card->rtd[i].cpu_dai->dev,
1769 "ASoC: Failed to set DAI format: %d\n",
1771 } else if (dai_fmt) {
1772 /* Flip the polarity for the "CPU" end */
1773 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1774 switch (dai_link->dai_fmt &
1775 SND_SOC_DAIFMT_MASTER_MASK) {
1776 case SND_SOC_DAIFMT_CBM_CFM:
1777 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1779 case SND_SOC_DAIFMT_CBM_CFS:
1780 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1782 case SND_SOC_DAIFMT_CBS_CFM:
1783 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1785 case SND_SOC_DAIFMT_CBS_CFS:
1786 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1790 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1792 if (ret != 0 && ret != -ENOTSUPP)
1793 dev_warn(card->rtd[i].cpu_dai->dev,
1794 "ASoC: Failed to set DAI format: %d\n",
1799 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1801 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1802 "%s", card->long_name ? card->long_name : card->name);
1803 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1804 "%s", card->driver_name ? card->driver_name : card->name);
1805 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1806 switch (card->snd_card->driver[i]) {
1812 if (!isalnum(card->snd_card->driver[i]))
1813 card->snd_card->driver[i] = '_';
1818 if (card->late_probe) {
1819 ret = card->late_probe(card);
1821 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
1823 goto probe_aux_dev_err;
1827 if (card->fully_routed)
1828 list_for_each_entry(codec, &card->codec_dev_list, card_list)
1829 snd_soc_dapm_auto_nc_codec_pins(codec);
1831 snd_soc_dapm_new_widgets(card);
1833 ret = snd_card_register(card->snd_card);
1835 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1837 goto probe_aux_dev_err;
1840 #ifdef CONFIG_SND_SOC_AC97_BUS
1841 /* register any AC97 codecs */
1842 for (i = 0; i < card->num_rtd; i++) {
1843 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1846 "ASoC: failed to register AC97: %d\n", ret);
1848 soc_unregister_ac97_dai_link(card->rtd[i].codec);
1849 goto probe_aux_dev_err;
1854 card->instantiated = 1;
1855 snd_soc_dapm_sync(&card->dapm);
1856 mutex_unlock(&card->mutex);
1861 for (i = 0; i < card->num_aux_devs; i++)
1862 soc_remove_aux_dev(card, i);
1865 soc_remove_dai_links(card);
1871 snd_card_free(card->snd_card);
1874 mutex_unlock(&card->mutex);
1879 /* probes a new socdev */
1880 static int soc_probe(struct platform_device *pdev)
1882 struct snd_soc_card *card = platform_get_drvdata(pdev);
1885 * no card, so machine driver should be registering card
1886 * we should not be here in that case so ret error
1891 dev_warn(&pdev->dev,
1892 "ASoC: machine %s should use snd_soc_register_card()\n",
1895 /* Bodge while we unpick instantiation */
1896 card->dev = &pdev->dev;
1898 return snd_soc_register_card(card);
1901 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1905 /* make sure any delayed work runs */
1906 for (i = 0; i < card->num_rtd; i++) {
1907 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1908 flush_delayed_work(&rtd->delayed_work);
1911 /* remove auxiliary devices */
1912 for (i = 0; i < card->num_aux_devs; i++)
1913 soc_remove_aux_dev(card, i);
1915 /* remove and free each DAI */
1916 soc_remove_dai_links(card);
1918 soc_cleanup_card_debugfs(card);
1920 /* remove the card */
1924 snd_soc_dapm_free(&card->dapm);
1926 snd_card_free(card->snd_card);
1931 /* removes a socdev */
1932 static int soc_remove(struct platform_device *pdev)
1934 struct snd_soc_card *card = platform_get_drvdata(pdev);
1936 snd_soc_unregister_card(card);
1940 int snd_soc_poweroff(struct device *dev)
1942 struct snd_soc_card *card = dev_get_drvdata(dev);
1945 if (!card->instantiated)
1948 /* Flush out pmdown_time work - we actually do want to run it
1949 * now, we're shutting down so no imminent restart. */
1950 for (i = 0; i < card->num_rtd; i++) {
1951 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1952 flush_delayed_work(&rtd->delayed_work);
1955 snd_soc_dapm_shutdown(card);
1957 /* deactivate pins to sleep state */
1958 for (i = 0; i < card->num_rtd; i++) {
1959 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1960 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
1961 pinctrl_pm_select_sleep_state(codec_dai->dev);
1962 pinctrl_pm_select_sleep_state(cpu_dai->dev);
1967 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1969 const struct dev_pm_ops snd_soc_pm_ops = {
1970 .suspend = snd_soc_suspend,
1971 .resume = snd_soc_resume,
1972 .freeze = snd_soc_suspend,
1973 .thaw = snd_soc_resume,
1974 .poweroff = snd_soc_poweroff,
1975 .restore = snd_soc_resume,
1977 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1979 /* ASoC platform driver */
1980 static struct platform_driver soc_driver = {
1982 .name = "soc-audio",
1983 .owner = THIS_MODULE,
1984 .pm = &snd_soc_pm_ops,
1987 .remove = soc_remove,
1991 * snd_soc_new_ac97_codec - initailise AC97 device
1992 * @codec: audio codec
1993 * @ops: AC97 bus operations
1994 * @num: AC97 codec number
1996 * Initialises AC97 codec resources for use by ad-hoc devices only.
1998 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1999 struct snd_ac97_bus_ops *ops, int num)
2001 mutex_lock(&codec->mutex);
2003 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2004 if (codec->ac97 == NULL) {
2005 mutex_unlock(&codec->mutex);
2009 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2010 if (codec->ac97->bus == NULL) {
2013 mutex_unlock(&codec->mutex);
2017 codec->ac97->bus->ops = ops;
2018 codec->ac97->num = num;
2021 * Mark the AC97 device to be created by us. This way we ensure that the
2022 * device will be registered with the device subsystem later on.
2024 codec->ac97_created = 1;
2026 mutex_unlock(&codec->mutex);
2029 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2031 static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2033 static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2035 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2037 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2039 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2043 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2045 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2049 static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2051 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2053 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2055 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2056 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2057 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2061 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2063 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2067 static int snd_soc_ac97_parse_pinctl(struct device *dev,
2068 struct snd_ac97_reset_cfg *cfg)
2071 struct pinctrl_state *state;
2075 p = devm_pinctrl_get(dev);
2077 dev_err(dev, "Failed to get pinctrl\n");
2082 state = pinctrl_lookup_state(p, "ac97-reset");
2083 if (IS_ERR(state)) {
2084 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
2085 return PTR_ERR(state);
2087 cfg->pstate_reset = state;
2089 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2090 if (IS_ERR(state)) {
2091 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
2092 return PTR_ERR(state);
2094 cfg->pstate_warm_reset = state;
2096 state = pinctrl_lookup_state(p, "ac97-running");
2097 if (IS_ERR(state)) {
2098 dev_err(dev, "Can't find pinctrl state ac97-running\n");
2099 return PTR_ERR(state);
2101 cfg->pstate_run = state;
2103 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2105 dev_err(dev, "Can't find ac97-sync gpio\n");
2108 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2110 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2113 cfg->gpio_sync = gpio;
2115 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2117 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2120 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2122 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2125 cfg->gpio_sdata = gpio;
2127 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2129 dev_err(dev, "Can't find ac97-reset gpio\n");
2132 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2134 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2137 cfg->gpio_reset = gpio;
2142 struct snd_ac97_bus_ops *soc_ac97_ops;
2143 EXPORT_SYMBOL_GPL(soc_ac97_ops);
2145 int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2147 if (ops == soc_ac97_ops)
2150 if (soc_ac97_ops && ops)
2157 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2160 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2162 * This function sets the reset and warm_reset properties of ops and parses
2163 * the device node of pdev to get pinctrl states and gpio numbers to use.
2165 int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2166 struct platform_device *pdev)
2168 struct device *dev = &pdev->dev;
2169 struct snd_ac97_reset_cfg cfg;
2172 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2176 ret = snd_soc_set_ac97_ops(ops);
2180 ops->warm_reset = snd_soc_ac97_warm_reset;
2181 ops->reset = snd_soc_ac97_reset;
2183 snd_ac97_rst_cfg = cfg;
2186 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2189 * snd_soc_free_ac97_codec - free AC97 codec device
2190 * @codec: audio codec
2192 * Frees AC97 codec device resources.
2194 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2196 mutex_lock(&codec->mutex);
2197 #ifdef CONFIG_SND_SOC_AC97_BUS
2198 soc_unregister_ac97_dai_link(codec);
2200 kfree(codec->ac97->bus);
2203 codec->ac97_created = 0;
2204 mutex_unlock(&codec->mutex);
2206 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2209 * snd_soc_cnew - create new control
2210 * @_template: control template
2211 * @data: control private data
2212 * @long_name: control long name
2213 * @prefix: control name prefix
2215 * Create a new mixer control from a template control.
2217 * Returns 0 for success, else error.
2219 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2220 void *data, const char *long_name,
2223 struct snd_kcontrol_new template;
2224 struct snd_kcontrol *kcontrol;
2227 memcpy(&template, _template, sizeof(template));
2231 long_name = template.name;
2234 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2238 template.name = name;
2240 template.name = long_name;
2243 kcontrol = snd_ctl_new1(&template, data);
2249 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2251 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2252 const struct snd_kcontrol_new *controls, int num_controls,
2253 const char *prefix, void *data)
2257 for (i = 0; i < num_controls; i++) {
2258 const struct snd_kcontrol_new *control = &controls[i];
2259 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2260 control->name, prefix));
2262 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2263 control->name, err);
2271 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2274 struct snd_card *card = soc_card->snd_card;
2275 struct snd_kcontrol *kctl;
2277 if (unlikely(!name))
2280 list_for_each_entry(kctl, &card->controls, list)
2281 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2285 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2288 * snd_soc_add_codec_controls - add an array of controls to a codec.
2289 * Convenience function to add a list of controls. Many codecs were
2290 * duplicating this code.
2292 * @codec: codec to add controls to
2293 * @controls: array of controls to add
2294 * @num_controls: number of elements in the array
2296 * Return 0 for success, else error.
2298 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2299 const struct snd_kcontrol_new *controls, int num_controls)
2301 struct snd_card *card = codec->card->snd_card;
2303 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2304 codec->name_prefix, codec);
2306 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2309 * snd_soc_add_platform_controls - add an array of controls to a platform.
2310 * Convenience function to add a list of controls.
2312 * @platform: platform to add controls to
2313 * @controls: array of controls to add
2314 * @num_controls: number of elements in the array
2316 * Return 0 for success, else error.
2318 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2319 const struct snd_kcontrol_new *controls, int num_controls)
2321 struct snd_card *card = platform->card->snd_card;
2323 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2326 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2329 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2330 * Convenience function to add a list of controls.
2332 * @soc_card: SoC card to add controls to
2333 * @controls: array of controls to add
2334 * @num_controls: number of elements in the array
2336 * Return 0 for success, else error.
2338 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2339 const struct snd_kcontrol_new *controls, int num_controls)
2341 struct snd_card *card = soc_card->snd_card;
2343 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2346 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2349 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2350 * Convienience function to add a list of controls.
2352 * @dai: DAI to add controls to
2353 * @controls: array of controls to add
2354 * @num_controls: number of elements in the array
2356 * Return 0 for success, else error.
2358 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2359 const struct snd_kcontrol_new *controls, int num_controls)
2361 struct snd_card *card = dai->card->snd_card;
2363 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2366 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2369 * snd_soc_info_enum_double - enumerated double mixer info callback
2370 * @kcontrol: mixer control
2371 * @uinfo: control element information
2373 * Callback to provide information about a double enumerated
2376 * Returns 0 for success.
2378 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2379 struct snd_ctl_elem_info *uinfo)
2381 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2383 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2384 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2385 uinfo->value.enumerated.items = e->items;
2387 if (uinfo->value.enumerated.item >= e->items)
2388 uinfo->value.enumerated.item = e->items - 1;
2389 strlcpy(uinfo->value.enumerated.name,
2390 e->texts[uinfo->value.enumerated.item],
2391 sizeof(uinfo->value.enumerated.name));
2394 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2397 * snd_soc_get_enum_double - enumerated double mixer get callback
2398 * @kcontrol: mixer control
2399 * @ucontrol: control element information
2401 * Callback to get the value of a double enumerated mixer.
2403 * Returns 0 for success.
2405 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2406 struct snd_ctl_elem_value *ucontrol)
2408 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2409 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2410 unsigned int val, item;
2411 unsigned int reg_val;
2413 reg_val = snd_soc_read(codec, e->reg);
2414 val = (reg_val >> e->shift_l) & e->mask;
2415 item = snd_soc_enum_val_to_item(e, val);
2416 ucontrol->value.enumerated.item[0] = item;
2417 if (e->shift_l != e->shift_r) {
2418 val = (reg_val >> e->shift_l) & e->mask;
2419 item = snd_soc_enum_val_to_item(e, val);
2420 ucontrol->value.enumerated.item[1] = item;
2425 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2428 * snd_soc_put_enum_double - enumerated double mixer put callback
2429 * @kcontrol: mixer control
2430 * @ucontrol: control element information
2432 * Callback to set the value of a double enumerated mixer.
2434 * Returns 0 for success.
2436 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2437 struct snd_ctl_elem_value *ucontrol)
2439 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2440 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2441 unsigned int *item = ucontrol->value.enumerated.item;
2445 if (item[0] >= e->items)
2447 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
2448 mask = e->mask << e->shift_l;
2449 if (e->shift_l != e->shift_r) {
2450 if (item[1] >= e->items)
2452 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
2453 mask |= e->mask << e->shift_r;
2456 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2458 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2461 * snd_soc_read_signed - Read a codec register and interprete as signed value
2463 * @reg: Register to read
2464 * @mask: Mask to use after shifting the register value
2465 * @shift: Right shift of register value
2466 * @sign_bit: Bit that describes if a number is negative or not.
2468 * This functions reads a codec register. The register value is shifted right
2469 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2470 * the given registervalue into a signed integer if sign_bit is non-zero.
2472 * Returns the register value as signed int.
2474 static int snd_soc_read_signed(struct snd_soc_codec *codec, unsigned int reg,
2475 unsigned int mask, unsigned int shift, unsigned int sign_bit)
2480 val = (snd_soc_read(codec, reg) >> shift) & mask;
2485 /* non-negative number */
2486 if (!(val & BIT(sign_bit)))
2492 * The register most probably does not contain a full-sized int.
2493 * Instead we have an arbitrary number of bits in a signed
2494 * representation which has to be translated into a full-sized int.
2495 * This is done by filling up all bits above the sign-bit.
2497 ret |= ~((int)(BIT(sign_bit) - 1));
2503 * snd_soc_info_volsw - single mixer info callback
2504 * @kcontrol: mixer control
2505 * @uinfo: control element information
2507 * Callback to provide information about a single mixer control, or a double
2508 * mixer control that spans 2 registers.
2510 * Returns 0 for success.
2512 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2513 struct snd_ctl_elem_info *uinfo)
2515 struct soc_mixer_control *mc =
2516 (struct soc_mixer_control *)kcontrol->private_value;
2519 if (!mc->platform_max)
2520 mc->platform_max = mc->max;
2521 platform_max = mc->platform_max;
2523 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2524 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2526 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2528 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2529 uinfo->value.integer.min = 0;
2530 uinfo->value.integer.max = platform_max - mc->min;
2533 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2536 * snd_soc_get_volsw - single mixer get callback
2537 * @kcontrol: mixer control
2538 * @ucontrol: control element information
2540 * Callback to get the value of a single mixer control, or a double mixer
2541 * control that spans 2 registers.
2543 * Returns 0 for success.
2545 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2546 struct snd_ctl_elem_value *ucontrol)
2548 struct soc_mixer_control *mc =
2549 (struct soc_mixer_control *)kcontrol->private_value;
2550 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2551 unsigned int reg = mc->reg;
2552 unsigned int reg2 = mc->rreg;
2553 unsigned int shift = mc->shift;
2554 unsigned int rshift = mc->rshift;
2557 int sign_bit = mc->sign_bit;
2558 unsigned int mask = (1 << fls(max)) - 1;
2559 unsigned int invert = mc->invert;
2562 mask = BIT(sign_bit + 1) - 1;
2564 ucontrol->value.integer.value[0] = snd_soc_read_signed(codec, reg, mask,
2565 shift, sign_bit) - min;
2567 ucontrol->value.integer.value[0] =
2568 max - ucontrol->value.integer.value[0];
2570 if (snd_soc_volsw_is_stereo(mc)) {
2572 ucontrol->value.integer.value[1] =
2573 snd_soc_read_signed(codec, reg, mask, rshift,
2576 ucontrol->value.integer.value[1] =
2577 snd_soc_read_signed(codec, reg2, mask, shift,
2580 ucontrol->value.integer.value[1] =
2581 max - ucontrol->value.integer.value[1];
2586 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2589 * snd_soc_put_volsw - single mixer put callback
2590 * @kcontrol: mixer control
2591 * @ucontrol: control element information
2593 * Callback to set the value of a single mixer control, or a double mixer
2594 * control that spans 2 registers.
2596 * Returns 0 for success.
2598 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2599 struct snd_ctl_elem_value *ucontrol)
2601 struct soc_mixer_control *mc =
2602 (struct soc_mixer_control *)kcontrol->private_value;
2603 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2604 unsigned int reg = mc->reg;
2605 unsigned int reg2 = mc->rreg;
2606 unsigned int shift = mc->shift;
2607 unsigned int rshift = mc->rshift;
2610 unsigned int sign_bit = mc->sign_bit;
2611 unsigned int mask = (1 << fls(max)) - 1;
2612 unsigned int invert = mc->invert;
2614 bool type_2r = false;
2615 unsigned int val2 = 0;
2616 unsigned int val, val_mask;
2619 mask = BIT(sign_bit + 1) - 1;
2621 val = ((ucontrol->value.integer.value[0] + min) & mask);
2624 val_mask = mask << shift;
2626 if (snd_soc_volsw_is_stereo(mc)) {
2627 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
2631 val_mask |= mask << rshift;
2632 val |= val2 << rshift;
2634 val2 = val2 << shift;
2638 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2643 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2647 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2650 * snd_soc_get_volsw_sx - single mixer get callback
2651 * @kcontrol: mixer control
2652 * @ucontrol: control element information
2654 * Callback to get the value of a single mixer control, or a double mixer
2655 * control that spans 2 registers.
2657 * Returns 0 for success.
2659 int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2660 struct snd_ctl_elem_value *ucontrol)
2662 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2663 struct soc_mixer_control *mc =
2664 (struct soc_mixer_control *)kcontrol->private_value;
2666 unsigned int reg = mc->reg;
2667 unsigned int reg2 = mc->rreg;
2668 unsigned int shift = mc->shift;
2669 unsigned int rshift = mc->rshift;
2672 int mask = (1 << (fls(min + max) - 1)) - 1;
2674 ucontrol->value.integer.value[0] =
2675 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2677 if (snd_soc_volsw_is_stereo(mc))
2678 ucontrol->value.integer.value[1] =
2679 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2683 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2686 * snd_soc_put_volsw_sx - double mixer set callback
2687 * @kcontrol: mixer control
2688 * @uinfo: control element information
2690 * Callback to set the value of a double mixer control that spans 2 registers.
2692 * Returns 0 for success.
2694 int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2695 struct snd_ctl_elem_value *ucontrol)
2697 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2698 struct soc_mixer_control *mc =
2699 (struct soc_mixer_control *)kcontrol->private_value;
2701 unsigned int reg = mc->reg;
2702 unsigned int reg2 = mc->rreg;
2703 unsigned int shift = mc->shift;
2704 unsigned int rshift = mc->rshift;
2707 int mask = (1 << (fls(min + max) - 1)) - 1;
2709 unsigned int val, val_mask, val2 = 0;
2711 val_mask = mask << shift;
2712 val = (ucontrol->value.integer.value[0] + min) & mask;
2715 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2719 if (snd_soc_volsw_is_stereo(mc)) {
2720 val_mask = mask << rshift;
2721 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2722 val2 = val2 << rshift;
2724 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2729 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2732 * snd_soc_info_volsw_s8 - signed mixer info callback
2733 * @kcontrol: mixer control
2734 * @uinfo: control element information
2736 * Callback to provide information about a signed mixer control.
2738 * Returns 0 for success.
2740 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2741 struct snd_ctl_elem_info *uinfo)
2743 struct soc_mixer_control *mc =
2744 (struct soc_mixer_control *)kcontrol->private_value;
2748 if (!mc->platform_max)
2749 mc->platform_max = mc->max;
2750 platform_max = mc->platform_max;
2752 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2754 uinfo->value.integer.min = 0;
2755 uinfo->value.integer.max = platform_max - min;
2758 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2761 * snd_soc_get_volsw_s8 - signed mixer get callback
2762 * @kcontrol: mixer control
2763 * @ucontrol: control element information
2765 * Callback to get the value of a signed mixer control.
2767 * Returns 0 for success.
2769 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2770 struct snd_ctl_elem_value *ucontrol)
2772 struct soc_mixer_control *mc =
2773 (struct soc_mixer_control *)kcontrol->private_value;
2774 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2775 unsigned int reg = mc->reg;
2777 int val = snd_soc_read(codec, reg);
2779 ucontrol->value.integer.value[0] =
2780 ((signed char)(val & 0xff))-min;
2781 ucontrol->value.integer.value[1] =
2782 ((signed char)((val >> 8) & 0xff))-min;
2785 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2788 * snd_soc_put_volsw_sgn - signed mixer put callback
2789 * @kcontrol: mixer control
2790 * @ucontrol: control element information
2792 * Callback to set the value of a signed mixer control.
2794 * Returns 0 for success.
2796 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2797 struct snd_ctl_elem_value *ucontrol)
2799 struct soc_mixer_control *mc =
2800 (struct soc_mixer_control *)kcontrol->private_value;
2801 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2802 unsigned int reg = mc->reg;
2806 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2807 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2809 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2811 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2814 * snd_soc_info_volsw_range - single mixer info callback with range.
2815 * @kcontrol: mixer control
2816 * @uinfo: control element information
2818 * Callback to provide information, within a range, about a single
2821 * returns 0 for success.
2823 int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2824 struct snd_ctl_elem_info *uinfo)
2826 struct soc_mixer_control *mc =
2827 (struct soc_mixer_control *)kcontrol->private_value;
2831 if (!mc->platform_max)
2832 mc->platform_max = mc->max;
2833 platform_max = mc->platform_max;
2835 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2836 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2837 uinfo->value.integer.min = 0;
2838 uinfo->value.integer.max = platform_max - min;
2842 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2845 * snd_soc_put_volsw_range - single mixer put value callback with range.
2846 * @kcontrol: mixer control
2847 * @ucontrol: control element information
2849 * Callback to set the value, within a range, for a single mixer control.
2851 * Returns 0 for success.
2853 int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2854 struct snd_ctl_elem_value *ucontrol)
2856 struct soc_mixer_control *mc =
2857 (struct soc_mixer_control *)kcontrol->private_value;
2858 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2859 unsigned int reg = mc->reg;
2860 unsigned int rreg = mc->rreg;
2861 unsigned int shift = mc->shift;
2864 unsigned int mask = (1 << fls(max)) - 1;
2865 unsigned int invert = mc->invert;
2866 unsigned int val, val_mask;
2869 val = ((ucontrol->value.integer.value[0] + min) & mask);
2872 val_mask = mask << shift;
2875 ret = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2879 if (snd_soc_volsw_is_stereo(mc)) {
2880 val = ((ucontrol->value.integer.value[1] + min) & mask);
2883 val_mask = mask << shift;
2886 ret = snd_soc_update_bits_locked(codec, rreg, val_mask, val);
2891 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2894 * snd_soc_get_volsw_range - single mixer get callback with range
2895 * @kcontrol: mixer control
2896 * @ucontrol: control element information
2898 * Callback to get the value, within a range, of a single mixer control.
2900 * Returns 0 for success.
2902 int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2903 struct snd_ctl_elem_value *ucontrol)
2905 struct soc_mixer_control *mc =
2906 (struct soc_mixer_control *)kcontrol->private_value;
2907 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2908 unsigned int reg = mc->reg;
2909 unsigned int rreg = mc->rreg;
2910 unsigned int shift = mc->shift;
2913 unsigned int mask = (1 << fls(max)) - 1;
2914 unsigned int invert = mc->invert;
2916 ucontrol->value.integer.value[0] =
2917 (snd_soc_read(codec, reg) >> shift) & mask;
2919 ucontrol->value.integer.value[0] =
2920 max - ucontrol->value.integer.value[0];
2921 ucontrol->value.integer.value[0] =
2922 ucontrol->value.integer.value[0] - min;
2924 if (snd_soc_volsw_is_stereo(mc)) {
2925 ucontrol->value.integer.value[1] =
2926 (snd_soc_read(codec, rreg) >> shift) & mask;
2928 ucontrol->value.integer.value[1] =
2929 max - ucontrol->value.integer.value[1];
2930 ucontrol->value.integer.value[1] =
2931 ucontrol->value.integer.value[1] - min;
2936 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
2939 * snd_soc_limit_volume - Set new limit to an existing volume control.
2941 * @codec: where to look for the control
2942 * @name: Name of the control
2943 * @max: new maximum limit
2945 * Return 0 for success, else error.
2947 int snd_soc_limit_volume(struct snd_soc_codec *codec,
2948 const char *name, int max)
2950 struct snd_card *card = codec->card->snd_card;
2951 struct snd_kcontrol *kctl;
2952 struct soc_mixer_control *mc;
2956 /* Sanity check for name and max */
2957 if (unlikely(!name || max <= 0))
2960 list_for_each_entry(kctl, &card->controls, list) {
2961 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2967 mc = (struct soc_mixer_control *)kctl->private_value;
2968 if (max <= mc->max) {
2969 mc->platform_max = max;
2975 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2977 int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
2978 struct snd_ctl_elem_info *uinfo)
2980 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2981 struct soc_bytes *params = (void *)kcontrol->private_value;
2983 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
2984 uinfo->count = params->num_regs * codec->val_bytes;
2988 EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
2990 int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
2991 struct snd_ctl_elem_value *ucontrol)
2993 struct soc_bytes *params = (void *)kcontrol->private_value;
2994 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2997 if (codec->using_regmap)
2998 ret = regmap_raw_read(codec->control_data, params->base,
2999 ucontrol->value.bytes.data,
3000 params->num_regs * codec->val_bytes);
3004 /* Hide any masked bytes to ensure consistent data reporting */
3005 if (ret == 0 && params->mask) {
3006 switch (codec->val_bytes) {
3008 ucontrol->value.bytes.data[0] &= ~params->mask;
3011 ((u16 *)(&ucontrol->value.bytes.data))[0]
3012 &= cpu_to_be16(~params->mask);
3015 ((u32 *)(&ucontrol->value.bytes.data))[0]
3016 &= cpu_to_be32(~params->mask);
3025 EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3027 int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3028 struct snd_ctl_elem_value *ucontrol)
3030 struct soc_bytes *params = (void *)kcontrol->private_value;
3031 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3033 unsigned int val, mask;
3036 if (!codec->using_regmap)
3039 len = params->num_regs * codec->val_bytes;
3041 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3046 * If we've got a mask then we need to preserve the register
3047 * bits. We shouldn't modify the incoming data so take a
3051 ret = regmap_read(codec->control_data, params->base, &val);
3055 val &= params->mask;
3057 switch (codec->val_bytes) {
3059 ((u8 *)data)[0] &= ~params->mask;
3060 ((u8 *)data)[0] |= val;
3063 mask = ~params->mask;
3064 ret = regmap_parse_val(codec->control_data,
3069 ((u16 *)data)[0] &= mask;
3071 ret = regmap_parse_val(codec->control_data,
3076 ((u16 *)data)[0] |= val;
3079 mask = ~params->mask;
3080 ret = regmap_parse_val(codec->control_data,
3085 ((u32 *)data)[0] &= mask;
3087 ret = regmap_parse_val(codec->control_data,
3092 ((u32 *)data)[0] |= val;
3100 ret = regmap_raw_write(codec->control_data, params->base,
3108 EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3110 int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3111 struct snd_ctl_elem_info *ucontrol)
3113 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3115 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3116 ucontrol->count = params->max;
3120 EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3123 * snd_soc_info_xr_sx - signed multi register info callback
3124 * @kcontrol: mreg control
3125 * @uinfo: control element information
3127 * Callback to provide information of a control that can
3128 * span multiple codec registers which together
3129 * forms a single signed value in a MSB/LSB manner.
3131 * Returns 0 for success.
3133 int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3134 struct snd_ctl_elem_info *uinfo)
3136 struct soc_mreg_control *mc =
3137 (struct soc_mreg_control *)kcontrol->private_value;
3138 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3140 uinfo->value.integer.min = mc->min;
3141 uinfo->value.integer.max = mc->max;
3145 EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3148 * snd_soc_get_xr_sx - signed multi register get callback
3149 * @kcontrol: mreg control
3150 * @ucontrol: control element information
3152 * Callback to get the value of a control that can span
3153 * multiple codec registers which together forms a single
3154 * signed value in a MSB/LSB manner. The control supports
3155 * specifying total no of bits used to allow for bitfields
3156 * across the multiple codec registers.
3158 * Returns 0 for success.
3160 int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3161 struct snd_ctl_elem_value *ucontrol)
3163 struct soc_mreg_control *mc =
3164 (struct soc_mreg_control *)kcontrol->private_value;
3165 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3166 unsigned int regbase = mc->regbase;
3167 unsigned int regcount = mc->regcount;
3168 unsigned int regwshift = codec->val_bytes * BITS_PER_BYTE;
3169 unsigned int regwmask = (1<<regwshift)-1;
3170 unsigned int invert = mc->invert;
3171 unsigned long mask = (1UL<<mc->nbits)-1;
3175 unsigned long regval;
3178 for (i = 0; i < regcount; i++) {
3179 regval = snd_soc_read(codec, regbase+i) & regwmask;
3180 val |= regval << (regwshift*(regcount-i-1));
3183 if (min < 0 && val > max)
3187 ucontrol->value.integer.value[0] = val;
3191 EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3194 * snd_soc_put_xr_sx - signed multi register get callback
3195 * @kcontrol: mreg control
3196 * @ucontrol: control element information
3198 * Callback to set the value of a control that can span
3199 * multiple codec registers which together forms a single
3200 * signed value in a MSB/LSB manner. The control supports
3201 * specifying total no of bits used to allow for bitfields
3202 * across the multiple codec registers.
3204 * Returns 0 for success.
3206 int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3207 struct snd_ctl_elem_value *ucontrol)
3209 struct soc_mreg_control *mc =
3210 (struct soc_mreg_control *)kcontrol->private_value;
3211 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3212 unsigned int regbase = mc->regbase;
3213 unsigned int regcount = mc->regcount;
3214 unsigned int regwshift = codec->val_bytes * BITS_PER_BYTE;
3215 unsigned int regwmask = (1<<regwshift)-1;
3216 unsigned int invert = mc->invert;
3217 unsigned long mask = (1UL<<mc->nbits)-1;
3219 long val = ucontrol->value.integer.value[0];
3220 unsigned int i, regval, regmask;
3226 for (i = 0; i < regcount; i++) {
3227 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3228 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3229 err = snd_soc_update_bits_locked(codec, regbase+i,
3237 EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3240 * snd_soc_get_strobe - strobe get callback
3241 * @kcontrol: mixer control
3242 * @ucontrol: control element information
3244 * Callback get the value of a strobe mixer control.
3246 * Returns 0 for success.
3248 int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3249 struct snd_ctl_elem_value *ucontrol)
3251 struct soc_mixer_control *mc =
3252 (struct soc_mixer_control *)kcontrol->private_value;
3253 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3254 unsigned int reg = mc->reg;
3255 unsigned int shift = mc->shift;
3256 unsigned int mask = 1 << shift;
3257 unsigned int invert = mc->invert != 0;
3258 unsigned int val = snd_soc_read(codec, reg) & mask;
3260 if (shift != 0 && val != 0)
3262 ucontrol->value.enumerated.item[0] = val ^ invert;
3266 EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3269 * snd_soc_put_strobe - strobe put callback
3270 * @kcontrol: mixer control
3271 * @ucontrol: control element information
3273 * Callback strobe a register bit to high then low (or the inverse)
3274 * in one pass of a single mixer enum control.
3276 * Returns 1 for success.
3278 int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3279 struct snd_ctl_elem_value *ucontrol)
3281 struct soc_mixer_control *mc =
3282 (struct soc_mixer_control *)kcontrol->private_value;
3283 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3284 unsigned int reg = mc->reg;
3285 unsigned int shift = mc->shift;
3286 unsigned int mask = 1 << shift;
3287 unsigned int invert = mc->invert != 0;
3288 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3289 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3290 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3293 err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3297 err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3300 EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3303 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3305 * @clk_id: DAI specific clock ID
3306 * @freq: new clock frequency in Hz
3307 * @dir: new clock direction - input/output.
3309 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3311 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3312 unsigned int freq, int dir)
3314 if (dai->driver && dai->driver->ops->set_sysclk)
3315 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3316 else if (dai->codec && dai->codec->driver->set_sysclk)
3317 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3322 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3325 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3327 * @clk_id: DAI specific clock ID
3328 * @source: Source for the clock
3329 * @freq: new clock frequency in Hz
3330 * @dir: new clock direction - input/output.
3332 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3334 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3335 int source, unsigned int freq, int dir)
3337 if (codec->driver->set_sysclk)
3338 return codec->driver->set_sysclk(codec, clk_id, source,
3343 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3346 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3348 * @div_id: DAI specific clock divider ID
3349 * @div: new clock divisor.
3351 * Configures the clock dividers. This is used to derive the best DAI bit and
3352 * frame clocks from the system or master clock. It's best to set the DAI bit
3353 * and frame clocks as low as possible to save system power.
3355 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3356 int div_id, int div)
3358 if (dai->driver && dai->driver->ops->set_clkdiv)
3359 return dai->driver->ops->set_clkdiv(dai, div_id, div);
3363 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3366 * snd_soc_dai_set_pll - configure DAI PLL.
3368 * @pll_id: DAI specific PLL ID
3369 * @source: DAI specific source for the PLL
3370 * @freq_in: PLL input clock frequency in Hz
3371 * @freq_out: requested PLL output clock frequency in Hz
3373 * Configures and enables PLL to generate output clock based on input clock.
3375 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3376 unsigned int freq_in, unsigned int freq_out)
3378 if (dai->driver && dai->driver->ops->set_pll)
3379 return dai->driver->ops->set_pll(dai, pll_id, source,
3381 else if (dai->codec && dai->codec->driver->set_pll)
3382 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3387 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3390 * snd_soc_codec_set_pll - configure codec PLL.
3392 * @pll_id: DAI specific PLL ID
3393 * @source: DAI specific source for the PLL
3394 * @freq_in: PLL input clock frequency in Hz
3395 * @freq_out: requested PLL output clock frequency in Hz
3397 * Configures and enables PLL to generate output clock based on input clock.
3399 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3400 unsigned int freq_in, unsigned int freq_out)
3402 if (codec->driver->set_pll)
3403 return codec->driver->set_pll(codec, pll_id, source,
3408 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3411 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3413 * @ratio Ratio of BCLK to Sample rate.
3415 * Configures the DAI for a preset BCLK to sample rate ratio.
3417 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3419 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3420 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3424 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3427 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3429 * @fmt: SND_SOC_DAIFMT_ format value.
3431 * Configures the DAI hardware format and clocking.
3433 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3435 if (dai->driver == NULL)
3437 if (dai->driver->ops->set_fmt == NULL)
3439 return dai->driver->ops->set_fmt(dai, fmt);
3441 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3444 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
3445 * @slots: Number of slots in use.
3446 * @tx_mask: bitmask representing active TX slots.
3447 * @rx_mask: bitmask representing active RX slots.
3449 * Generates the TDM tx and rx slot default masks for DAI.
3451 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
3452 unsigned int *tx_mask,
3453 unsigned int *rx_mask)
3455 if (*tx_mask || *rx_mask)
3461 *tx_mask = (1 << slots) - 1;
3462 *rx_mask = (1 << slots) - 1;
3468 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3470 * @tx_mask: bitmask representing active TX slots.
3471 * @rx_mask: bitmask representing active RX slots.
3472 * @slots: Number of slots in use.
3473 * @slot_width: Width in bits for each slot.
3475 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3478 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3479 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
3481 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3482 dai->driver->ops->xlate_tdm_slot_mask(slots,
3483 &tx_mask, &rx_mask);
3485 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
3487 if (dai->driver && dai->driver->ops->set_tdm_slot)
3488 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3493 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3496 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3498 * @tx_num: how many TX channels
3499 * @tx_slot: pointer to an array which imply the TX slot number channel
3501 * @rx_num: how many RX channels
3502 * @rx_slot: pointer to an array which imply the RX slot number channel
3505 * configure the relationship between channel number and TDM slot number.
3507 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3508 unsigned int tx_num, unsigned int *tx_slot,
3509 unsigned int rx_num, unsigned int *rx_slot)
3511 if (dai->driver && dai->driver->ops->set_channel_map)
3512 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3517 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3520 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3522 * @tristate: tristate enable
3524 * Tristates the DAI so that others can use it.
3526 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3528 if (dai->driver && dai->driver->ops->set_tristate)
3529 return dai->driver->ops->set_tristate(dai, tristate);
3533 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3536 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3538 * @mute: mute enable
3539 * @direction: stream to mute
3541 * Mutes the DAI DAC.
3543 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3549 if (dai->driver->ops->mute_stream)
3550 return dai->driver->ops->mute_stream(dai, mute, direction);
3551 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3552 dai->driver->ops->digital_mute)
3553 return dai->driver->ops->digital_mute(dai, mute);
3557 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3560 * snd_soc_register_card - Register a card with the ASoC core
3562 * @card: Card to register
3565 int snd_soc_register_card(struct snd_soc_card *card)
3569 if (!card->name || !card->dev)
3572 for (i = 0; i < card->num_links; i++) {
3573 struct snd_soc_dai_link *link = &card->dai_link[i];
3576 * Codec must be specified by 1 of name or OF node,
3577 * not both or neither.
3579 if (!!link->codec_name == !!link->codec_of_node) {
3581 "ASoC: Neither/both codec name/of_node are set for %s\n",
3585 /* Codec DAI name must be specified */
3586 if (!link->codec_dai_name) {
3588 "ASoC: codec_dai_name not set for %s\n",
3594 * Platform may be specified by either name or OF node, but
3595 * can be left unspecified, and a dummy platform will be used.
3597 if (link->platform_name && link->platform_of_node) {
3599 "ASoC: Both platform name/of_node are set for %s\n",
3605 * CPU device may be specified by either name or OF node, but
3606 * can be left unspecified, and will be matched based on DAI
3609 if (link->cpu_name && link->cpu_of_node) {
3611 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3616 * At least one of CPU DAI name or CPU device name/node must be
3619 if (!link->cpu_dai_name &&
3620 !(link->cpu_name || link->cpu_of_node)) {
3622 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3628 dev_set_drvdata(card->dev, card);
3630 snd_soc_initialize_card_lists(card);
3632 soc_init_card_debugfs(card);
3634 card->rtd = devm_kzalloc(card->dev,
3635 sizeof(struct snd_soc_pcm_runtime) *
3636 (card->num_links + card->num_aux_devs),
3638 if (card->rtd == NULL)
3641 card->rtd_aux = &card->rtd[card->num_links];
3643 for (i = 0; i < card->num_links; i++)
3644 card->rtd[i].dai_link = &card->dai_link[i];
3646 INIT_LIST_HEAD(&card->list);
3647 INIT_LIST_HEAD(&card->dapm_dirty);
3648 card->instantiated = 0;
3649 mutex_init(&card->mutex);
3650 mutex_init(&card->dapm_mutex);
3652 ret = snd_soc_instantiate_card(card);
3654 soc_cleanup_card_debugfs(card);
3656 /* deactivate pins to sleep state */
3657 for (i = 0; i < card->num_rtd; i++) {
3658 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
3659 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
3660 if (!codec_dai->active)
3661 pinctrl_pm_select_sleep_state(codec_dai->dev);
3662 if (!cpu_dai->active)
3663 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3668 EXPORT_SYMBOL_GPL(snd_soc_register_card);
3671 * snd_soc_unregister_card - Unregister a card with the ASoC core
3673 * @card: Card to unregister
3676 int snd_soc_unregister_card(struct snd_soc_card *card)
3678 if (card->instantiated)
3679 soc_cleanup_card_resources(card);
3680 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
3684 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3687 * Simplify DAI link configuration by removing ".-1" from device names
3688 * and sanitizing names.
3690 static char *fmt_single_name(struct device *dev, int *id)
3692 char *found, name[NAME_SIZE];
3695 if (dev_name(dev) == NULL)
3698 strlcpy(name, dev_name(dev), NAME_SIZE);
3700 /* are we a "%s.%d" name (platform and SPI components) */
3701 found = strstr(name, dev->driver->name);
3704 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3706 /* discard ID from name if ID == -1 */
3708 found[strlen(dev->driver->name)] = '\0';
3712 /* I2C component devices are named "bus-addr" */
3713 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3714 char tmp[NAME_SIZE];
3716 /* create unique ID number from I2C addr and bus */
3717 *id = ((id1 & 0xffff) << 16) + id2;
3719 /* sanitize component name for DAI link creation */
3720 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3721 strlcpy(name, tmp, NAME_SIZE);
3726 return kstrdup(name, GFP_KERNEL);
3730 * Simplify DAI link naming for single devices with multiple DAIs by removing
3731 * any ".-1" and using the DAI name (instead of device name).
3733 static inline char *fmt_multiple_name(struct device *dev,
3734 struct snd_soc_dai_driver *dai_drv)
3736 if (dai_drv->name == NULL) {
3738 "ASoC: error - multiple DAI %s registered with no name\n",
3743 return kstrdup(dai_drv->name, GFP_KERNEL);
3747 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
3749 * @component: The component for which the DAIs should be unregistered
3751 static void snd_soc_unregister_dais(struct snd_soc_component *component)
3753 struct snd_soc_dai *dai, *_dai;
3755 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
3756 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3758 list_del(&dai->list);
3765 * snd_soc_register_dais - Register a DAI with the ASoC core
3767 * @component: The component the DAIs are registered for
3768 * @codec: The CODEC that the DAIs are registered for, NULL if the component is
3770 * @dai_drv: DAI driver to use for the DAIs
3771 * @count: Number of DAIs
3772 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3775 static int snd_soc_register_dais(struct snd_soc_component *component,
3776 struct snd_soc_codec *codec, struct snd_soc_dai_driver *dai_drv,
3777 size_t count, bool legacy_dai_naming)
3779 struct device *dev = component->dev;
3780 struct snd_soc_dai *dai;
3784 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
3786 for (i = 0; i < count; i++) {
3788 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3795 * Back in the old days when we still had component-less DAIs,
3796 * instead of having a static name, component-less DAIs would
3797 * inherit the name of the parent device so it is possible to
3798 * register multiple instances of the DAI. We still need to keep
3799 * the same naming style even though those DAIs are not
3800 * component-less anymore.
3802 if (count == 1 && legacy_dai_naming) {
3803 dai->name = fmt_single_name(dev, &dai->id);
3805 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3807 dai->id = dai_drv[i].id;
3811 if (dai->name == NULL) {
3817 dai->component = component;
3820 dai->driver = &dai_drv[i];
3821 dai->dapm.dev = dev;
3822 if (!dai->driver->ops)
3823 dai->driver->ops = &null_dai_ops;
3826 dai->dapm.idle_bias_off = 1;
3828 list_add(&dai->list, &component->dai_list);
3830 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3836 snd_soc_unregister_dais(component);
3842 * snd_soc_register_component - Register a component with the ASoC core
3846 __snd_soc_register_component(struct device *dev,
3847 struct snd_soc_component *cmpnt,
3848 const struct snd_soc_component_driver *cmpnt_drv,
3849 struct snd_soc_codec *codec,
3850 struct snd_soc_dai_driver *dai_drv,
3851 int num_dai, bool allow_single_dai)
3855 dev_dbg(dev, "component register %s\n", dev_name(dev));
3858 dev_err(dev, "ASoC: Failed to connecting component\n");
3862 cmpnt->name = fmt_single_name(dev, &cmpnt->id);
3864 dev_err(dev, "ASoC: Failed to simplifying name\n");
3869 cmpnt->driver = cmpnt_drv;
3870 cmpnt->dai_drv = dai_drv;
3871 cmpnt->num_dai = num_dai;
3872 INIT_LIST_HEAD(&cmpnt->dai_list);
3874 ret = snd_soc_register_dais(cmpnt, codec, dai_drv, num_dai,
3877 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
3878 goto error_component_name;
3881 mutex_lock(&client_mutex);
3882 list_add(&cmpnt->list, &component_list);
3883 mutex_unlock(&client_mutex);
3885 dev_dbg(cmpnt->dev, "ASoC: Registered component '%s'\n", cmpnt->name);
3889 error_component_name:
3895 int snd_soc_register_component(struct device *dev,
3896 const struct snd_soc_component_driver *cmpnt_drv,
3897 struct snd_soc_dai_driver *dai_drv,
3900 struct snd_soc_component *cmpnt;
3902 cmpnt = devm_kzalloc(dev, sizeof(*cmpnt), GFP_KERNEL);
3904 dev_err(dev, "ASoC: Failed to allocate memory\n");
3908 cmpnt->ignore_pmdown_time = true;
3909 cmpnt->registered_as_component = true;
3911 return __snd_soc_register_component(dev, cmpnt, cmpnt_drv, NULL,
3912 dai_drv, num_dai, true);
3914 EXPORT_SYMBOL_GPL(snd_soc_register_component);
3917 * snd_soc_unregister_component - Unregister a component from the ASoC core
3920 void snd_soc_unregister_component(struct device *dev)
3922 struct snd_soc_component *cmpnt;
3924 list_for_each_entry(cmpnt, &component_list, list) {
3925 if (dev == cmpnt->dev && cmpnt->registered_as_component)
3931 snd_soc_unregister_dais(cmpnt);
3933 mutex_lock(&client_mutex);
3934 list_del(&cmpnt->list);
3935 mutex_unlock(&client_mutex);
3937 dev_dbg(dev, "ASoC: Unregistered component '%s'\n", cmpnt->name);
3940 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3943 * snd_soc_add_platform - Add a platform to the ASoC core
3944 * @dev: The parent device for the platform
3945 * @platform: The platform to add
3946 * @platform_driver: The driver for the platform
3948 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
3949 const struct snd_soc_platform_driver *platform_drv)
3953 /* create platform component name */
3954 platform->name = fmt_single_name(dev, &platform->id);
3955 if (platform->name == NULL)
3958 platform->dev = dev;
3959 platform->driver = platform_drv;
3960 platform->dapm.dev = dev;
3961 platform->dapm.platform = platform;
3962 platform->dapm.stream_event = platform_drv->stream_event;
3963 mutex_init(&platform->mutex);
3965 /* register component */
3966 ret = __snd_soc_register_component(dev, &platform->component,
3967 &platform_drv->component_driver,
3968 NULL, NULL, 0, false);
3970 dev_err(platform->component.dev,
3971 "ASoC: Failed to register component: %d\n", ret);
3975 mutex_lock(&client_mutex);
3976 list_add(&platform->list, &platform_list);
3977 mutex_unlock(&client_mutex);
3979 dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
3983 EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3986 * snd_soc_register_platform - Register a platform with the ASoC core
3988 * @platform: platform to register
3990 int snd_soc_register_platform(struct device *dev,
3991 const struct snd_soc_platform_driver *platform_drv)
3993 struct snd_soc_platform *platform;
3996 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3998 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3999 if (platform == NULL)
4002 ret = snd_soc_add_platform(dev, platform, platform_drv);
4008 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4011 * snd_soc_remove_platform - Remove a platform from the ASoC core
4012 * @platform: the platform to remove
4014 void snd_soc_remove_platform(struct snd_soc_platform *platform)
4016 snd_soc_unregister_component(platform->dev);
4018 mutex_lock(&client_mutex);
4019 list_del(&platform->list);
4020 mutex_unlock(&client_mutex);
4022 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
4024 kfree(platform->name);
4026 EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4028 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4030 struct snd_soc_platform *platform;
4032 list_for_each_entry(platform, &platform_list, list) {
4033 if (dev == platform->dev)
4039 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4042 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4044 * @platform: platform to unregister
4046 void snd_soc_unregister_platform(struct device *dev)
4048 struct snd_soc_platform *platform;
4050 platform = snd_soc_lookup_platform(dev);
4054 snd_soc_remove_platform(platform);
4057 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4059 static u64 codec_format_map[] = {
4060 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4061 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4062 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4063 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4064 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4065 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4066 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4067 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4068 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4069 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4070 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4071 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4072 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4073 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4074 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4075 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4078 /* Fix up the DAI formats for endianness: codecs don't actually see
4079 * the endianness of the data but we're using the CPU format
4080 * definitions which do need to include endianness so we ensure that
4081 * codec DAIs always have both big and little endian variants set.
4083 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4087 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4088 if (stream->formats & codec_format_map[i])
4089 stream->formats |= codec_format_map[i];
4093 * snd_soc_register_codec - Register a codec with the ASoC core
4095 * @codec: codec to register
4097 int snd_soc_register_codec(struct device *dev,
4098 const struct snd_soc_codec_driver *codec_drv,
4099 struct snd_soc_dai_driver *dai_drv,
4102 struct snd_soc_codec *codec;
4105 dev_dbg(dev, "codec register %s\n", dev_name(dev));
4107 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4111 /* create CODEC component name */
4112 codec->name = fmt_single_name(dev, &codec->id);
4113 if (codec->name == NULL) {
4118 codec->write = codec_drv->write;
4119 codec->read = codec_drv->read;
4120 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
4121 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4122 codec->dapm.dev = dev;
4123 codec->dapm.codec = codec;
4124 codec->dapm.seq_notifier = codec_drv->seq_notifier;
4125 codec->dapm.stream_event = codec_drv->stream_event;
4127 codec->driver = codec_drv;
4128 codec->num_dai = num_dai;
4129 codec->val_bytes = codec_drv->reg_word_size;
4130 mutex_init(&codec->mutex);
4132 for (i = 0; i < num_dai; i++) {
4133 fixup_codec_formats(&dai_drv[i].playback);
4134 fixup_codec_formats(&dai_drv[i].capture);
4137 mutex_lock(&client_mutex);
4138 list_add(&codec->list, &codec_list);
4139 mutex_unlock(&client_mutex);
4141 /* register component */
4142 ret = __snd_soc_register_component(dev, &codec->component,
4143 &codec_drv->component_driver,
4144 codec, dai_drv, num_dai, false);
4146 dev_err(codec->dev, "ASoC: Failed to regster component: %d\n", ret);
4147 goto fail_codec_name;
4150 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
4154 mutex_lock(&client_mutex);
4155 list_del(&codec->list);
4156 mutex_unlock(&client_mutex);
4163 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4166 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4168 * @codec: codec to unregister
4170 void snd_soc_unregister_codec(struct device *dev)
4172 struct snd_soc_codec *codec;
4174 list_for_each_entry(codec, &codec_list, list) {
4175 if (dev == codec->dev)
4181 snd_soc_unregister_component(dev);
4183 mutex_lock(&client_mutex);
4184 list_del(&codec->list);
4185 mutex_unlock(&client_mutex);
4187 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
4189 snd_soc_cache_exit(codec);
4193 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4195 /* Retrieve a card's name from device tree */
4196 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4197 const char *propname)
4199 struct device_node *np = card->dev->of_node;
4202 ret = of_property_read_string_index(np, propname, 0, &card->name);
4204 * EINVAL means the property does not exist. This is fine providing
4205 * card->name was previously set, which is checked later in
4206 * snd_soc_register_card.
4208 if (ret < 0 && ret != -EINVAL) {
4210 "ASoC: Property '%s' could not be read: %d\n",
4217 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4219 static const struct snd_soc_dapm_widget simple_widgets[] = {
4220 SND_SOC_DAPM_MIC("Microphone", NULL),
4221 SND_SOC_DAPM_LINE("Line", NULL),
4222 SND_SOC_DAPM_HP("Headphone", NULL),
4223 SND_SOC_DAPM_SPK("Speaker", NULL),
4226 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4227 const char *propname)
4229 struct device_node *np = card->dev->of_node;
4230 struct snd_soc_dapm_widget *widgets;
4231 const char *template, *wname;
4232 int i, j, num_widgets, ret;
4234 num_widgets = of_property_count_strings(np, propname);
4235 if (num_widgets < 0) {
4237 "ASoC: Property '%s' does not exist\n", propname);
4240 if (num_widgets & 1) {
4242 "ASoC: Property '%s' length is not even\n", propname);
4248 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4253 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4257 "ASoC: Could not allocate memory for widgets\n");
4261 for (i = 0; i < num_widgets; i++) {
4262 ret = of_property_read_string_index(np, propname,
4266 "ASoC: Property '%s' index %d read error:%d\n",
4267 propname, 2 * i, ret);
4271 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4272 if (!strncmp(template, simple_widgets[j].name,
4273 strlen(simple_widgets[j].name))) {
4274 widgets[i] = simple_widgets[j];
4279 if (j >= ARRAY_SIZE(simple_widgets)) {
4281 "ASoC: DAPM widget '%s' is not supported\n",
4286 ret = of_property_read_string_index(np, propname,
4291 "ASoC: Property '%s' index %d read error:%d\n",
4292 propname, (2 * i) + 1, ret);
4296 widgets[i].name = wname;
4299 card->dapm_widgets = widgets;
4300 card->num_dapm_widgets = num_widgets;
4304 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4306 int snd_soc_of_parse_tdm_slot(struct device_node *np,
4307 unsigned int *slots,
4308 unsigned int *slot_width)
4313 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4314 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4322 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4323 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4333 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4335 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4336 const char *propname)
4338 struct device_node *np = card->dev->of_node;
4340 struct snd_soc_dapm_route *routes;
4343 num_routes = of_property_count_strings(np, propname);
4344 if (num_routes < 0 || num_routes & 1) {
4346 "ASoC: Property '%s' does not exist or its length is not even\n",
4352 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4357 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4361 "ASoC: Could not allocate DAPM route table\n");
4365 for (i = 0; i < num_routes; i++) {
4366 ret = of_property_read_string_index(np, propname,
4367 2 * i, &routes[i].sink);
4370 "ASoC: Property '%s' index %d could not be read: %d\n",
4371 propname, 2 * i, ret);
4374 ret = of_property_read_string_index(np, propname,
4375 (2 * i) + 1, &routes[i].source);
4378 "ASoC: Property '%s' index %d could not be read: %d\n",
4379 propname, (2 * i) + 1, ret);
4384 card->num_dapm_routes = num_routes;
4385 card->dapm_routes = routes;
4389 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4391 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4396 unsigned int format = 0;
4402 } of_fmt_table[] = {
4403 { "i2s", SND_SOC_DAIFMT_I2S },
4404 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4405 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4406 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4407 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4408 { "ac97", SND_SOC_DAIFMT_AC97 },
4409 { "pdm", SND_SOC_DAIFMT_PDM},
4410 { "msb", SND_SOC_DAIFMT_MSB },
4411 { "lsb", SND_SOC_DAIFMT_LSB },
4418 * check "[prefix]format = xxx"
4419 * SND_SOC_DAIFMT_FORMAT_MASK area
4421 snprintf(prop, sizeof(prop), "%sformat", prefix);
4422 ret = of_property_read_string(np, prop, &str);
4424 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4425 if (strcmp(str, of_fmt_table[i].name) == 0) {
4426 format |= of_fmt_table[i].val;
4433 * check "[prefix]continuous-clock"
4434 * SND_SOC_DAIFMT_CLOCK_MASK area
4436 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4437 if (of_get_property(np, prop, NULL))
4438 format |= SND_SOC_DAIFMT_CONT;
4440 format |= SND_SOC_DAIFMT_GATED;
4443 * check "[prefix]bitclock-inversion"
4444 * check "[prefix]frame-inversion"
4445 * SND_SOC_DAIFMT_INV_MASK area
4447 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4448 bit = !!of_get_property(np, prop, NULL);
4450 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4451 frame = !!of_get_property(np, prop, NULL);
4453 switch ((bit << 4) + frame) {
4455 format |= SND_SOC_DAIFMT_IB_IF;
4458 format |= SND_SOC_DAIFMT_IB_NF;
4461 format |= SND_SOC_DAIFMT_NB_IF;
4464 /* SND_SOC_DAIFMT_NB_NF is default */
4469 * check "[prefix]bitclock-master"
4470 * check "[prefix]frame-master"
4471 * SND_SOC_DAIFMT_MASTER_MASK area
4473 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4474 bit = !!of_get_property(np, prop, NULL);
4476 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4477 frame = !!of_get_property(np, prop, NULL);
4479 switch ((bit << 4) + frame) {
4481 format |= SND_SOC_DAIFMT_CBM_CFM;
4484 format |= SND_SOC_DAIFMT_CBM_CFS;
4487 format |= SND_SOC_DAIFMT_CBS_CFM;
4490 format |= SND_SOC_DAIFMT_CBS_CFS;
4496 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4498 int snd_soc_of_get_dai_name(struct device_node *of_node,
4499 const char **dai_name)
4501 struct snd_soc_component *pos;
4502 struct of_phandle_args args;
4505 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4506 "#sound-dai-cells", 0, &args);
4510 ret = -EPROBE_DEFER;
4512 mutex_lock(&client_mutex);
4513 list_for_each_entry(pos, &component_list, list) {
4514 if (pos->dev->of_node != args.np)
4517 if (pos->driver->of_xlate_dai_name) {
4518 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4522 switch (args.args_count) {
4524 id = 0; /* same as dai_drv[0] */
4534 if (id < 0 || id >= pos->num_dai) {
4541 *dai_name = pos->dai_drv[id].name;
4543 *dai_name = pos->name;
4548 mutex_unlock(&client_mutex);
4550 of_node_put(args.np);
4554 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4556 static int __init snd_soc_init(void)
4558 #ifdef CONFIG_DEBUG_FS
4559 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4560 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
4561 pr_warn("ASoC: Failed to create debugfs directory\n");
4562 snd_soc_debugfs_root = NULL;
4565 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4567 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4569 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4571 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
4573 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
4574 &platform_list_fops))
4575 pr_warn("ASoC: Failed to create platform list debugfs file\n");
4578 snd_soc_util_init();
4580 return platform_driver_register(&soc_driver);
4582 module_init(snd_soc_init);
4584 static void __exit snd_soc_exit(void)
4586 snd_soc_util_exit();
4588 #ifdef CONFIG_DEBUG_FS
4589 debugfs_remove_recursive(snd_soc_debugfs_root);
4591 platform_driver_unregister(&soc_driver);
4593 module_exit(snd_soc_exit);
4595 /* Module information */
4596 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4597 MODULE_DESCRIPTION("ALSA SoC Core");
4598 MODULE_LICENSE("GPL");
4599 MODULE_ALIAS("platform:soc-audio");