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(dai_list);
60 static LIST_HEAD(platform_list);
61 static LIST_HEAD(codec_list);
62 static LIST_HEAD(component_list);
65 * This is a timeout to do a DAPM powerdown after a stream is closed().
66 * It can be used to eliminate pops between different playback streams, e.g.
67 * between two audio tracks.
69 static int pmdown_time = 5000;
70 module_param(pmdown_time, int, 0);
71 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
73 struct snd_ac97_reset_cfg {
75 struct pinctrl_state *pstate_reset;
76 struct pinctrl_state *pstate_warm_reset;
77 struct pinctrl_state *pstate_run;
83 /* returns the minimum number of bytes needed to represent
84 * a particular given value */
85 static int min_bytes_needed(unsigned long val)
90 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
93 c = (sizeof val * 8) - c;
101 /* fill buf which is 'len' bytes with a formatted
102 * string of the form 'reg: value\n' */
103 static int format_register_str(struct snd_soc_codec *codec,
104 unsigned int reg, char *buf, size_t len)
106 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
107 int regsize = codec->driver->reg_word_size * 2;
109 char tmpbuf[len + 1];
110 char regbuf[regsize + 1];
112 /* since tmpbuf is allocated on the stack, warn the callers if they
113 * try to abuse this function */
116 /* +2 for ': ' and + 1 for '\n' */
117 if (wordsize + regsize + 2 + 1 != len)
120 ret = snd_soc_read(codec, reg);
122 memset(regbuf, 'X', regsize);
123 regbuf[regsize] = '\0';
125 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
128 /* prepare the buffer */
129 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
130 /* copy it back to the caller without the '\0' */
131 memcpy(buf, tmpbuf, len);
136 /* codec register dump */
137 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
138 size_t count, loff_t pos)
141 int wordsize, regsize;
146 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
147 regsize = codec->driver->reg_word_size * 2;
149 len = wordsize + regsize + 2 + 1;
151 if (!codec->driver->reg_cache_size)
154 if (codec->driver->reg_cache_step)
155 step = codec->driver->reg_cache_step;
157 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
158 if (!snd_soc_codec_readable_register(codec, i))
160 if (codec->driver->display_register) {
161 count += codec->driver->display_register(codec, buf + count,
162 PAGE_SIZE - count, i);
164 /* only support larger than PAGE_SIZE bytes debugfs
165 * entries for the default case */
167 if (total + len >= count - 1)
169 format_register_str(codec, i, buf + total, len);
176 total = min(total, count - 1);
181 static ssize_t codec_reg_show(struct device *dev,
182 struct device_attribute *attr, char *buf)
184 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
186 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
189 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
191 static ssize_t pmdown_time_show(struct device *dev,
192 struct device_attribute *attr, char *buf)
194 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
196 return sprintf(buf, "%ld\n", rtd->pmdown_time);
199 static ssize_t pmdown_time_set(struct device *dev,
200 struct device_attribute *attr,
201 const char *buf, size_t count)
203 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
206 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
213 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
215 #ifdef CONFIG_DEBUG_FS
216 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
217 size_t count, loff_t *ppos)
220 struct snd_soc_codec *codec = file->private_data;
223 if (*ppos < 0 || !count)
226 buf = kmalloc(count, GFP_KERNEL);
230 ret = soc_codec_reg_show(codec, buf, count, *ppos);
232 if (copy_to_user(user_buf, buf, ret)) {
243 static ssize_t codec_reg_write_file(struct file *file,
244 const char __user *user_buf, size_t count, loff_t *ppos)
249 unsigned long reg, value;
250 struct snd_soc_codec *codec = file->private_data;
252 buf_size = min(count, (sizeof(buf)-1));
253 if (copy_from_user(buf, user_buf, buf_size))
257 while (*start == ' ')
259 reg = simple_strtoul(start, &start, 16);
260 while (*start == ' ')
262 if (strict_strtoul(start, 16, &value))
265 /* Userspace has been fiddling around behind the kernel's back */
266 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
268 snd_soc_write(codec, reg, value);
272 static const struct file_operations codec_reg_fops = {
274 .read = codec_reg_read_file,
275 .write = codec_reg_write_file,
276 .llseek = default_llseek,
279 static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
281 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
283 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
285 if (!codec->debugfs_codec_root) {
287 "ASoC: Failed to create codec debugfs directory\n");
291 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
293 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
296 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
297 codec->debugfs_codec_root,
298 codec, &codec_reg_fops);
299 if (!codec->debugfs_reg)
301 "ASoC: Failed to create codec register debugfs file\n");
303 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
306 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
308 debugfs_remove_recursive(codec->debugfs_codec_root);
311 static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
313 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
315 platform->debugfs_platform_root = debugfs_create_dir(platform->name,
317 if (!platform->debugfs_platform_root) {
318 dev_warn(platform->dev,
319 "ASoC: Failed to create platform debugfs directory\n");
323 snd_soc_dapm_debugfs_init(&platform->dapm,
324 platform->debugfs_platform_root);
327 static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
329 debugfs_remove_recursive(platform->debugfs_platform_root);
332 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
333 size_t count, loff_t *ppos)
335 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
336 ssize_t len, ret = 0;
337 struct snd_soc_codec *codec;
342 list_for_each_entry(codec, &codec_list, list) {
343 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
347 if (ret > PAGE_SIZE) {
354 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
361 static const struct file_operations codec_list_fops = {
362 .read = codec_list_read_file,
363 .llseek = default_llseek,/* read accesses f_pos */
366 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
367 size_t count, loff_t *ppos)
369 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
370 ssize_t len, ret = 0;
371 struct snd_soc_dai *dai;
376 list_for_each_entry(dai, &dai_list, list) {
377 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
380 if (ret > PAGE_SIZE) {
386 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
393 static const struct file_operations dai_list_fops = {
394 .read = dai_list_read_file,
395 .llseek = default_llseek,/* read accesses f_pos */
398 static ssize_t platform_list_read_file(struct file *file,
399 char __user *user_buf,
400 size_t count, loff_t *ppos)
402 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
403 ssize_t len, ret = 0;
404 struct snd_soc_platform *platform;
409 list_for_each_entry(platform, &platform_list, list) {
410 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
414 if (ret > PAGE_SIZE) {
420 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
427 static const struct file_operations platform_list_fops = {
428 .read = platform_list_read_file,
429 .llseek = default_llseek,/* read accesses f_pos */
432 static void soc_init_card_debugfs(struct snd_soc_card *card)
434 card->debugfs_card_root = debugfs_create_dir(card->name,
435 snd_soc_debugfs_root);
436 if (!card->debugfs_card_root) {
438 "ASoC: Failed to create card debugfs directory\n");
442 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
443 card->debugfs_card_root,
445 if (!card->debugfs_pop_time)
447 "ASoC: Failed to create pop time debugfs file\n");
450 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
452 debugfs_remove_recursive(card->debugfs_card_root);
457 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
461 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
465 static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
469 static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
473 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
477 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
482 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
483 const char *dai_link, int stream)
487 for (i = 0; i < card->num_links; i++) {
488 if (card->rtd[i].dai_link->no_pcm &&
489 !strcmp(card->rtd[i].dai_link->name, dai_link))
490 return card->rtd[i].pcm->streams[stream].substream;
492 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
495 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
497 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
498 const char *dai_link)
502 for (i = 0; i < card->num_links; i++) {
503 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
504 return &card->rtd[i];
506 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
509 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
511 #ifdef CONFIG_SND_SOC_AC97_BUS
512 /* unregister ac97 codec */
513 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
515 if (codec->ac97->dev.bus)
516 device_unregister(&codec->ac97->dev);
520 /* stop no dev release warning */
521 static void soc_ac97_device_release(struct device *dev){}
523 /* register ac97 codec to bus */
524 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
528 codec->ac97->dev.bus = &ac97_bus_type;
529 codec->ac97->dev.parent = codec->card->dev;
530 codec->ac97->dev.release = soc_ac97_device_release;
532 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
533 codec->card->snd_card->number, 0, codec->name);
534 err = device_register(&codec->ac97->dev);
536 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
537 codec->ac97->dev.bus = NULL;
544 static void codec2codec_close_delayed_work(struct work_struct *work)
546 /* Currently nothing to do for c2c links
547 * Since c2c links are internal nodes in the DAPM graph and
548 * don't interface with the outside world or application layer
549 * we don't have to do any special handling on close.
553 #ifdef CONFIG_PM_SLEEP
554 /* powers down audio subsystem for suspend */
555 int snd_soc_suspend(struct device *dev)
557 struct snd_soc_card *card = dev_get_drvdata(dev);
558 struct snd_soc_codec *codec;
561 /* If the initialization of this soc device failed, there is no codec
562 * associated with it. Just bail out in this case.
564 if (list_empty(&card->codec_dev_list))
567 /* Due to the resume being scheduled into a workqueue we could
568 * suspend before that's finished - wait for it to complete.
570 snd_power_lock(card->snd_card);
571 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
572 snd_power_unlock(card->snd_card);
574 /* we're going to block userspace touching us until resume completes */
575 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
577 /* mute any active DACs */
578 for (i = 0; i < card->num_rtd; i++) {
579 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
580 struct snd_soc_dai_driver *drv = dai->driver;
582 if (card->rtd[i].dai_link->ignore_suspend)
585 if (drv->ops->digital_mute && dai->playback_active)
586 drv->ops->digital_mute(dai, 1);
589 /* suspend all pcms */
590 for (i = 0; i < card->num_rtd; i++) {
591 if (card->rtd[i].dai_link->ignore_suspend)
594 snd_pcm_suspend_all(card->rtd[i].pcm);
597 if (card->suspend_pre)
598 card->suspend_pre(card);
600 for (i = 0; i < card->num_rtd; i++) {
601 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
602 struct snd_soc_platform *platform = card->rtd[i].platform;
604 if (card->rtd[i].dai_link->ignore_suspend)
607 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
608 cpu_dai->driver->suspend(cpu_dai);
609 if (platform->driver->suspend && !platform->suspended) {
610 platform->driver->suspend(cpu_dai);
611 platform->suspended = 1;
615 /* close any waiting streams and save state */
616 for (i = 0; i < card->num_rtd; i++) {
617 flush_delayed_work(&card->rtd[i].delayed_work);
618 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
621 for (i = 0; i < card->num_rtd; i++) {
623 if (card->rtd[i].dai_link->ignore_suspend)
626 snd_soc_dapm_stream_event(&card->rtd[i],
627 SNDRV_PCM_STREAM_PLAYBACK,
628 SND_SOC_DAPM_STREAM_SUSPEND);
630 snd_soc_dapm_stream_event(&card->rtd[i],
631 SNDRV_PCM_STREAM_CAPTURE,
632 SND_SOC_DAPM_STREAM_SUSPEND);
635 /* Recheck all analogue paths too */
636 dapm_mark_io_dirty(&card->dapm);
637 snd_soc_dapm_sync(&card->dapm);
639 /* suspend all CODECs */
640 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
641 /* If there are paths active then the CODEC will be held with
642 * bias _ON and should not be suspended. */
643 if (!codec->suspended && codec->driver->suspend) {
644 switch (codec->dapm.bias_level) {
645 case SND_SOC_BIAS_STANDBY:
647 * If the CODEC is capable of idle
648 * bias off then being in STANDBY
649 * means it's doing something,
650 * otherwise fall through.
652 if (codec->dapm.idle_bias_off) {
654 "ASoC: idle_bias_off CODEC on over suspend\n");
657 case SND_SOC_BIAS_OFF:
658 codec->driver->suspend(codec);
659 codec->suspended = 1;
660 codec->cache_sync = 1;
661 if (codec->using_regmap)
662 regcache_mark_dirty(codec->control_data);
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);
682 if (card->suspend_post)
683 card->suspend_post(card);
687 EXPORT_SYMBOL_GPL(snd_soc_suspend);
689 /* deferred resume work, so resume can complete before we finished
690 * setting our codec back up, which can be very slow on I2C
692 static void soc_resume_deferred(struct work_struct *work)
694 struct snd_soc_card *card =
695 container_of(work, struct snd_soc_card, deferred_resume_work);
696 struct snd_soc_codec *codec;
699 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
700 * so userspace apps are blocked from touching us
703 dev_dbg(card->dev, "ASoC: starting resume work\n");
705 /* Bring us up into D2 so that DAPM starts enabling things */
706 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
708 if (card->resume_pre)
709 card->resume_pre(card);
711 /* resume AC97 DAIs */
712 for (i = 0; i < card->num_rtd; i++) {
713 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
715 if (card->rtd[i].dai_link->ignore_suspend)
718 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
719 cpu_dai->driver->resume(cpu_dai);
722 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
723 /* If the CODEC was idle over suspend then it will have been
724 * left with bias OFF or STANDBY and suspended so we must now
725 * resume. Otherwise the suspend was suppressed.
727 if (codec->driver->resume && codec->suspended) {
728 switch (codec->dapm.bias_level) {
729 case SND_SOC_BIAS_STANDBY:
730 case SND_SOC_BIAS_OFF:
731 codec->driver->resume(codec);
732 codec->suspended = 0;
736 "ASoC: CODEC was on over suspend\n");
742 for (i = 0; i < card->num_rtd; i++) {
744 if (card->rtd[i].dai_link->ignore_suspend)
747 snd_soc_dapm_stream_event(&card->rtd[i],
748 SNDRV_PCM_STREAM_PLAYBACK,
749 SND_SOC_DAPM_STREAM_RESUME);
751 snd_soc_dapm_stream_event(&card->rtd[i],
752 SNDRV_PCM_STREAM_CAPTURE,
753 SND_SOC_DAPM_STREAM_RESUME);
756 /* unmute any active DACs */
757 for (i = 0; i < card->num_rtd; i++) {
758 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
759 struct snd_soc_dai_driver *drv = dai->driver;
761 if (card->rtd[i].dai_link->ignore_suspend)
764 if (drv->ops->digital_mute && dai->playback_active)
765 drv->ops->digital_mute(dai, 0);
768 for (i = 0; i < card->num_rtd; i++) {
769 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
770 struct snd_soc_platform *platform = card->rtd[i].platform;
772 if (card->rtd[i].dai_link->ignore_suspend)
775 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
776 cpu_dai->driver->resume(cpu_dai);
777 if (platform->driver->resume && platform->suspended) {
778 platform->driver->resume(cpu_dai);
779 platform->suspended = 0;
783 if (card->resume_post)
784 card->resume_post(card);
786 dev_dbg(card->dev, "ASoC: resume work completed\n");
788 /* userspace can access us now we are back as we were before */
789 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
791 /* Recheck all analogue paths too */
792 dapm_mark_io_dirty(&card->dapm);
793 snd_soc_dapm_sync(&card->dapm);
796 /* powers up audio subsystem after a suspend */
797 int snd_soc_resume(struct device *dev)
799 struct snd_soc_card *card = dev_get_drvdata(dev);
800 int i, ac97_control = 0;
802 /* If the initialization of this soc device failed, there is no codec
803 * associated with it. Just bail out in this case.
805 if (list_empty(&card->codec_dev_list))
808 /* AC97 devices might have other drivers hanging off them so
809 * need to resume immediately. Other drivers don't have that
810 * problem and may take a substantial amount of time to resume
811 * due to I/O costs and anti-pop so handle them out of line.
813 for (i = 0; i < card->num_rtd; i++) {
814 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
815 ac97_control |= cpu_dai->driver->ac97_control;
818 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
819 soc_resume_deferred(&card->deferred_resume_work);
821 dev_dbg(dev, "ASoC: Scheduling resume work\n");
822 if (!schedule_work(&card->deferred_resume_work))
823 dev_err(dev, "ASoC: resume work item may be lost\n");
828 EXPORT_SYMBOL_GPL(snd_soc_resume);
830 #define snd_soc_suspend NULL
831 #define snd_soc_resume NULL
834 static const struct snd_soc_dai_ops null_dai_ops = {
837 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
839 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
840 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
841 struct snd_soc_codec *codec;
842 struct snd_soc_platform *platform;
843 struct snd_soc_dai *codec_dai, *cpu_dai;
844 const char *platform_name;
846 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
848 /* Find CPU DAI from registered DAIs*/
849 list_for_each_entry(cpu_dai, &dai_list, list) {
850 if (dai_link->cpu_of_node &&
851 (cpu_dai->dev->of_node != dai_link->cpu_of_node))
853 if (dai_link->cpu_name &&
854 strcmp(dev_name(cpu_dai->dev), dai_link->cpu_name))
856 if (dai_link->cpu_dai_name &&
857 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
860 rtd->cpu_dai = cpu_dai;
864 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
865 dai_link->cpu_dai_name);
866 return -EPROBE_DEFER;
869 /* Find CODEC from registered CODECs */
870 list_for_each_entry(codec, &codec_list, list) {
871 if (dai_link->codec_of_node) {
872 if (codec->dev->of_node != dai_link->codec_of_node)
875 if (strcmp(codec->name, dai_link->codec_name))
882 * CODEC found, so find CODEC DAI from registered DAIs from
885 list_for_each_entry(codec_dai, &dai_list, list) {
886 if (codec->dev == codec_dai->dev &&
887 !strcmp(codec_dai->name,
888 dai_link->codec_dai_name)) {
890 rtd->codec_dai = codec_dai;
894 if (!rtd->codec_dai) {
895 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
896 dai_link->codec_dai_name);
897 return -EPROBE_DEFER;
902 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
903 dai_link->codec_name);
904 return -EPROBE_DEFER;
907 /* if there's no platform we match on the empty platform */
908 platform_name = dai_link->platform_name;
909 if (!platform_name && !dai_link->platform_of_node)
910 platform_name = "snd-soc-dummy";
912 /* find one from the set of registered platforms */
913 list_for_each_entry(platform, &platform_list, list) {
914 if (dai_link->platform_of_node) {
915 if (platform->dev->of_node !=
916 dai_link->platform_of_node)
919 if (strcmp(platform->name, platform_name))
923 rtd->platform = platform;
925 if (!rtd->platform) {
926 dev_err(card->dev, "ASoC: platform %s not registered\n",
927 dai_link->platform_name);
928 return -EPROBE_DEFER;
936 static int soc_remove_platform(struct snd_soc_platform *platform)
940 if (platform->driver->remove) {
941 ret = platform->driver->remove(platform);
943 dev_err(platform->dev, "ASoC: failed to remove %d\n",
947 /* Make sure all DAPM widgets are freed */
948 snd_soc_dapm_free(&platform->dapm);
950 soc_cleanup_platform_debugfs(platform);
951 platform->probed = 0;
952 list_del(&platform->card_list);
953 module_put(platform->dev->driver->owner);
958 static void soc_remove_codec(struct snd_soc_codec *codec)
962 if (codec->driver->remove) {
963 err = codec->driver->remove(codec);
965 dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
968 /* Make sure all DAPM widgets are freed */
969 snd_soc_dapm_free(&codec->dapm);
971 soc_cleanup_codec_debugfs(codec);
973 list_del(&codec->card_list);
974 module_put(codec->dev->driver->owner);
977 static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
979 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
980 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
983 /* unregister the rtd device */
984 if (rtd->dev_registered) {
985 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
986 device_remove_file(rtd->dev, &dev_attr_codec_reg);
987 device_unregister(rtd->dev);
988 rtd->dev_registered = 0;
991 /* remove the CODEC DAI */
992 if (codec_dai && codec_dai->probed &&
993 codec_dai->driver->remove_order == order) {
994 if (codec_dai->driver->remove) {
995 err = codec_dai->driver->remove(codec_dai);
997 dev_err(codec_dai->dev,
998 "ASoC: failed to remove %s: %d\n",
999 codec_dai->name, err);
1001 codec_dai->probed = 0;
1002 list_del(&codec_dai->card_list);
1005 /* remove the cpu_dai */
1006 if (cpu_dai && cpu_dai->probed &&
1007 cpu_dai->driver->remove_order == order) {
1008 if (cpu_dai->driver->remove) {
1009 err = cpu_dai->driver->remove(cpu_dai);
1011 dev_err(cpu_dai->dev,
1012 "ASoC: failed to remove %s: %d\n",
1013 cpu_dai->name, err);
1015 cpu_dai->probed = 0;
1016 list_del(&cpu_dai->card_list);
1018 if (!cpu_dai->codec) {
1019 snd_soc_dapm_free(&cpu_dai->dapm);
1020 module_put(cpu_dai->dev->driver->owner);
1025 static void soc_remove_link_components(struct snd_soc_card *card, int num,
1028 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1029 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1030 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1031 struct snd_soc_platform *platform = rtd->platform;
1032 struct snd_soc_codec *codec;
1034 /* remove the platform */
1035 if (platform && platform->probed &&
1036 platform->driver->remove_order == order) {
1037 soc_remove_platform(platform);
1040 /* remove the CODEC-side CODEC */
1042 codec = codec_dai->codec;
1043 if (codec && codec->probed &&
1044 codec->driver->remove_order == order)
1045 soc_remove_codec(codec);
1048 /* remove any CPU-side CODEC */
1050 codec = cpu_dai->codec;
1051 if (codec && codec->probed &&
1052 codec->driver->remove_order == order)
1053 soc_remove_codec(codec);
1057 static void soc_remove_dai_links(struct snd_soc_card *card)
1061 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1063 for (dai = 0; dai < card->num_rtd; dai++)
1064 soc_remove_link_dais(card, dai, order);
1067 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1069 for (dai = 0; dai < card->num_rtd; dai++)
1070 soc_remove_link_components(card, dai, order);
1076 static void soc_set_name_prefix(struct snd_soc_card *card,
1077 struct snd_soc_codec *codec)
1081 if (card->codec_conf == NULL)
1084 for (i = 0; i < card->num_configs; i++) {
1085 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1086 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1087 codec->name_prefix = map->name_prefix;
1093 static int soc_probe_codec(struct snd_soc_card *card,
1094 struct snd_soc_codec *codec)
1097 const struct snd_soc_codec_driver *driver = codec->driver;
1098 struct snd_soc_dai *dai;
1101 codec->dapm.card = card;
1102 soc_set_name_prefix(card, codec);
1104 if (!try_module_get(codec->dev->driver->owner))
1107 soc_init_codec_debugfs(codec);
1109 if (driver->dapm_widgets)
1110 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1111 driver->num_dapm_widgets);
1113 /* Create DAPM widgets for each DAI stream */
1114 list_for_each_entry(dai, &dai_list, list) {
1115 if (dai->dev != codec->dev)
1118 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1121 codec->dapm.idle_bias_off = driver->idle_bias_off;
1123 if (driver->probe) {
1124 ret = driver->probe(codec);
1127 "ASoC: failed to probe CODEC %d\n", ret);
1130 WARN(codec->dapm.idle_bias_off &&
1131 codec->dapm.bias_level != SND_SOC_BIAS_OFF,
1132 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1136 /* If the driver didn't set I/O up try regmap */
1137 if (!codec->write && dev_get_regmap(codec->dev, NULL))
1138 snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
1140 if (driver->controls)
1141 snd_soc_add_codec_controls(codec, driver->controls,
1142 driver->num_controls);
1143 if (driver->dapm_routes)
1144 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1145 driver->num_dapm_routes);
1147 /* mark codec as probed and add to card codec list */
1149 list_add(&codec->card_list, &card->codec_dev_list);
1150 list_add(&codec->dapm.list, &card->dapm_list);
1155 soc_cleanup_codec_debugfs(codec);
1156 module_put(codec->dev->driver->owner);
1161 static int soc_probe_platform(struct snd_soc_card *card,
1162 struct snd_soc_platform *platform)
1165 const struct snd_soc_platform_driver *driver = platform->driver;
1166 struct snd_soc_dai *dai;
1168 platform->card = card;
1169 platform->dapm.card = card;
1171 if (!try_module_get(platform->dev->driver->owner))
1174 soc_init_platform_debugfs(platform);
1176 if (driver->dapm_widgets)
1177 snd_soc_dapm_new_controls(&platform->dapm,
1178 driver->dapm_widgets, driver->num_dapm_widgets);
1180 /* Create DAPM widgets for each DAI stream */
1181 list_for_each_entry(dai, &dai_list, list) {
1182 if (dai->dev != platform->dev)
1185 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1188 platform->dapm.idle_bias_off = 1;
1190 if (driver->probe) {
1191 ret = driver->probe(platform);
1193 dev_err(platform->dev,
1194 "ASoC: failed to probe platform %d\n", ret);
1199 if (driver->controls)
1200 snd_soc_add_platform_controls(platform, driver->controls,
1201 driver->num_controls);
1202 if (driver->dapm_routes)
1203 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1204 driver->num_dapm_routes);
1206 /* mark platform as probed and add to card platform list */
1207 platform->probed = 1;
1208 list_add(&platform->card_list, &card->platform_dev_list);
1209 list_add(&platform->dapm.list, &card->dapm_list);
1214 soc_cleanup_platform_debugfs(platform);
1215 module_put(platform->dev->driver->owner);
1220 static void rtd_release(struct device *dev)
1225 static int soc_post_component_init(struct snd_soc_card *card,
1226 struct snd_soc_codec *codec,
1227 int num, int dailess)
1229 struct snd_soc_dai_link *dai_link = NULL;
1230 struct snd_soc_aux_dev *aux_dev = NULL;
1231 struct snd_soc_pcm_runtime *rtd;
1232 const char *temp, *name;
1236 dai_link = &card->dai_link[num];
1237 rtd = &card->rtd[num];
1238 name = dai_link->name;
1240 aux_dev = &card->aux_dev[num];
1241 rtd = &card->rtd_aux[num];
1242 name = aux_dev->name;
1246 /* Make sure all DAPM widgets are instantiated */
1247 snd_soc_dapm_new_widgets(&codec->dapm);
1249 /* machine controls, routes and widgets are not prefixed */
1250 temp = codec->name_prefix;
1251 codec->name_prefix = NULL;
1253 /* do machine specific initialization */
1254 if (!dailess && dai_link->init)
1255 ret = dai_link->init(rtd);
1256 else if (dailess && aux_dev->init)
1257 ret = aux_dev->init(&codec->dapm);
1259 dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
1262 codec->name_prefix = temp;
1264 /* register the rtd device */
1267 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1270 device_initialize(rtd->dev);
1271 rtd->dev->parent = card->dev;
1272 rtd->dev->release = rtd_release;
1273 rtd->dev->init_name = name;
1274 dev_set_drvdata(rtd->dev, rtd);
1275 mutex_init(&rtd->pcm_mutex);
1276 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1277 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1278 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1279 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1280 ret = device_add(rtd->dev);
1282 /* calling put_device() here to free the rtd->dev */
1283 put_device(rtd->dev);
1285 "ASoC: failed to register runtime device: %d\n", ret);
1288 rtd->dev_registered = 1;
1290 /* add DAPM sysfs entries for this codec */
1291 ret = snd_soc_dapm_sys_add(rtd->dev);
1294 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
1296 /* add codec sysfs entries */
1297 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1300 "ASoC: failed to add codec sysfs files: %d\n", ret);
1302 #ifdef CONFIG_DEBUG_FS
1303 /* add DPCM sysfs entries */
1304 if (!dailess && !dai_link->dynamic)
1307 ret = soc_dpcm_debugfs_add(rtd);
1309 dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
1316 static int soc_probe_link_components(struct snd_soc_card *card, int num,
1319 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1320 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1321 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1322 struct snd_soc_platform *platform = rtd->platform;
1325 /* probe the CPU-side component, if it is a CODEC */
1326 if (cpu_dai->codec &&
1327 !cpu_dai->codec->probed &&
1328 cpu_dai->codec->driver->probe_order == order) {
1329 ret = soc_probe_codec(card, cpu_dai->codec);
1334 /* probe the CODEC-side component */
1335 if (!codec_dai->codec->probed &&
1336 codec_dai->codec->driver->probe_order == order) {
1337 ret = soc_probe_codec(card, codec_dai->codec);
1342 /* probe the platform */
1343 if (!platform->probed &&
1344 platform->driver->probe_order == order) {
1345 ret = soc_probe_platform(card, platform);
1353 static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1355 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1356 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1357 struct snd_soc_codec *codec = rtd->codec;
1358 struct snd_soc_platform *platform = rtd->platform;
1359 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1360 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1361 struct snd_soc_dapm_widget *play_w, *capture_w;
1364 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1365 card->name, num, order);
1367 /* config components */
1368 cpu_dai->platform = platform;
1369 codec_dai->card = card;
1370 cpu_dai->card = card;
1372 /* set default power off timeout */
1373 rtd->pmdown_time = pmdown_time;
1375 /* probe the cpu_dai */
1376 if (!cpu_dai->probed &&
1377 cpu_dai->driver->probe_order == order) {
1378 if (!cpu_dai->codec) {
1379 cpu_dai->dapm.card = card;
1380 if (!try_module_get(cpu_dai->dev->driver->owner))
1383 list_add(&cpu_dai->dapm.list, &card->dapm_list);
1384 snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1387 if (cpu_dai->driver->probe) {
1388 ret = cpu_dai->driver->probe(cpu_dai);
1390 dev_err(cpu_dai->dev,
1391 "ASoC: failed to probe CPU DAI %s: %d\n",
1392 cpu_dai->name, ret);
1393 module_put(cpu_dai->dev->driver->owner);
1397 cpu_dai->probed = 1;
1398 /* mark cpu_dai as probed and add to card dai list */
1399 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1402 /* probe the CODEC DAI */
1403 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1404 if (codec_dai->driver->probe) {
1405 ret = codec_dai->driver->probe(codec_dai);
1407 dev_err(codec_dai->dev,
1408 "ASoC: failed to probe CODEC DAI %s: %d\n",
1409 codec_dai->name, ret);
1414 /* mark codec_dai as probed and add to card dai list */
1415 codec_dai->probed = 1;
1416 list_add(&codec_dai->card_list, &card->dai_dev_list);
1419 /* complete DAI probe during last probe */
1420 if (order != SND_SOC_COMP_ORDER_LAST)
1423 ret = soc_post_component_init(card, codec, num, 0);
1427 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1429 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1432 if (cpu_dai->driver->compress_dai) {
1433 /*create compress_device"*/
1434 ret = soc_new_compress(rtd, num);
1436 dev_err(card->dev, "ASoC: can't create compress %s\n",
1437 dai_link->stream_name);
1442 if (!dai_link->params) {
1443 /* create the pcm */
1444 ret = soc_new_pcm(rtd, num);
1446 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1447 dai_link->stream_name, ret);
1451 INIT_DELAYED_WORK(&rtd->delayed_work,
1452 codec2codec_close_delayed_work);
1454 /* link the DAI widgets */
1455 play_w = codec_dai->playback_widget;
1456 capture_w = cpu_dai->capture_widget;
1457 if (play_w && capture_w) {
1458 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1461 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1462 play_w->name, capture_w->name, ret);
1467 play_w = cpu_dai->playback_widget;
1468 capture_w = codec_dai->capture_widget;
1469 if (play_w && capture_w) {
1470 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1473 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1474 play_w->name, capture_w->name, ret);
1481 /* add platform data for AC97 devices */
1482 if (rtd->codec_dai->driver->ac97_control)
1483 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1488 #ifdef CONFIG_SND_SOC_AC97_BUS
1489 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1493 /* Only instantiate AC97 if not already done by the adaptor
1494 * for the generic AC97 subsystem.
1496 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
1498 * It is possible that the AC97 device is already registered to
1499 * the device subsystem. This happens when the device is created
1500 * via snd_ac97_mixer(). Currently only SoC codec that does so
1501 * is the generic AC97 glue but others migh emerge.
1503 * In those cases we don't try to register the device again.
1505 if (!rtd->codec->ac97_created)
1508 ret = soc_ac97_dev_register(rtd->codec);
1510 dev_err(rtd->codec->dev,
1511 "ASoC: AC97 device register failed: %d\n", ret);
1515 rtd->codec->ac97_registered = 1;
1520 static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1522 if (codec->ac97_registered) {
1523 soc_ac97_dev_unregister(codec);
1524 codec->ac97_registered = 0;
1529 static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1531 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1532 struct snd_soc_codec *codec;
1534 /* find CODEC from registered CODECs*/
1535 list_for_each_entry(codec, &codec_list, list) {
1536 if (!strcmp(codec->name, aux_dev->codec_name))
1540 dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
1542 return -EPROBE_DEFER;
1545 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1547 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1548 struct snd_soc_codec *codec;
1551 /* find CODEC from registered CODECs*/
1552 list_for_each_entry(codec, &codec_list, list) {
1553 if (!strcmp(codec->name, aux_dev->codec_name)) {
1554 if (codec->probed) {
1556 "ASoC: codec already probed");
1563 /* codec not found */
1564 dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
1565 return -EPROBE_DEFER;
1568 ret = soc_probe_codec(card, codec);
1572 ret = soc_post_component_init(card, codec, num, 1);
1578 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1580 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1581 struct snd_soc_codec *codec = rtd->codec;
1583 /* unregister the rtd device */
1584 if (rtd->dev_registered) {
1585 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1586 device_unregister(rtd->dev);
1587 rtd->dev_registered = 0;
1590 if (codec && codec->probed)
1591 soc_remove_codec(codec);
1594 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1595 enum snd_soc_compress_type compress_type)
1599 if (codec->cache_init)
1602 /* override the compress_type if necessary */
1603 if (compress_type && codec->compress_type != compress_type)
1604 codec->compress_type = compress_type;
1605 ret = snd_soc_cache_init(codec);
1608 "ASoC: Failed to set cache compression type: %d\n",
1612 codec->cache_init = 1;
1616 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1618 struct snd_soc_codec *codec;
1619 struct snd_soc_codec_conf *codec_conf;
1620 enum snd_soc_compress_type compress_type;
1621 struct snd_soc_dai_link *dai_link;
1622 int ret, i, order, dai_fmt;
1624 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1627 for (i = 0; i < card->num_links; i++) {
1628 ret = soc_bind_dai_link(card, i);
1633 /* check aux_devs too */
1634 for (i = 0; i < card->num_aux_devs; i++) {
1635 ret = soc_check_aux_dev(card, i);
1640 /* initialize the register cache for each available codec */
1641 list_for_each_entry(codec, &codec_list, list) {
1642 if (codec->cache_init)
1644 /* by default we don't override the compress_type */
1646 /* check to see if we need to override the compress_type */
1647 for (i = 0; i < card->num_configs; ++i) {
1648 codec_conf = &card->codec_conf[i];
1649 if (!strcmp(codec->name, codec_conf->dev_name)) {
1650 compress_type = codec_conf->compress_type;
1651 if (compress_type && compress_type
1652 != codec->compress_type)
1656 ret = snd_soc_init_codec_cache(codec, compress_type);
1661 /* card bind complete so register a sound card */
1662 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1663 card->owner, 0, &card->snd_card);
1666 "ASoC: can't create sound card for card %s: %d\n",
1670 card->snd_card->dev = card->dev;
1672 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1673 card->dapm.dev = card->dev;
1674 card->dapm.card = card;
1675 list_add(&card->dapm.list, &card->dapm_list);
1677 #ifdef CONFIG_DEBUG_FS
1678 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1681 #ifdef CONFIG_PM_SLEEP
1682 /* deferred resume work */
1683 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1686 if (card->dapm_widgets)
1687 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1688 card->num_dapm_widgets);
1690 /* initialise the sound card only once */
1692 ret = card->probe(card);
1694 goto card_probe_error;
1697 /* probe all components used by DAI links on this card */
1698 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1700 for (i = 0; i < card->num_links; i++) {
1701 ret = soc_probe_link_components(card, i, order);
1704 "ASoC: failed to instantiate card %d\n",
1711 /* probe all DAI links on this card */
1712 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1714 for (i = 0; i < card->num_links; i++) {
1715 ret = soc_probe_link_dais(card, i, order);
1718 "ASoC: failed to instantiate card %d\n",
1725 for (i = 0; i < card->num_aux_devs; i++) {
1726 ret = soc_probe_aux_dev(card, i);
1729 "ASoC: failed to add auxiliary devices %d\n",
1731 goto probe_aux_dev_err;
1735 snd_soc_dapm_link_dai_widgets(card);
1738 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1740 if (card->dapm_routes)
1741 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1742 card->num_dapm_routes);
1744 snd_soc_dapm_new_widgets(&card->dapm);
1746 for (i = 0; i < card->num_links; i++) {
1747 dai_link = &card->dai_link[i];
1748 dai_fmt = dai_link->dai_fmt;
1751 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1753 if (ret != 0 && ret != -ENOTSUPP)
1754 dev_warn(card->rtd[i].codec_dai->dev,
1755 "ASoC: Failed to set DAI format: %d\n",
1759 /* If this is a regular CPU link there will be a platform */
1761 (dai_link->platform_name || dai_link->platform_of_node)) {
1762 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1764 if (ret != 0 && ret != -ENOTSUPP)
1765 dev_warn(card->rtd[i].cpu_dai->dev,
1766 "ASoC: Failed to set DAI format: %d\n",
1768 } else if (dai_fmt) {
1769 /* Flip the polarity for the "CPU" end */
1770 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1771 switch (dai_link->dai_fmt &
1772 SND_SOC_DAIFMT_MASTER_MASK) {
1773 case SND_SOC_DAIFMT_CBM_CFM:
1774 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1776 case SND_SOC_DAIFMT_CBM_CFS:
1777 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1779 case SND_SOC_DAIFMT_CBS_CFM:
1780 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1782 case SND_SOC_DAIFMT_CBS_CFS:
1783 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1787 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1789 if (ret != 0 && ret != -ENOTSUPP)
1790 dev_warn(card->rtd[i].cpu_dai->dev,
1791 "ASoC: Failed to set DAI format: %d\n",
1796 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1798 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1799 "%s", card->long_name ? card->long_name : card->name);
1800 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1801 "%s", card->driver_name ? card->driver_name : card->name);
1802 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1803 switch (card->snd_card->driver[i]) {
1809 if (!isalnum(card->snd_card->driver[i]))
1810 card->snd_card->driver[i] = '_';
1815 if (card->late_probe) {
1816 ret = card->late_probe(card);
1818 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
1820 goto probe_aux_dev_err;
1824 snd_soc_dapm_new_widgets(&card->dapm);
1826 if (card->fully_routed)
1827 list_for_each_entry(codec, &card->codec_dev_list, card_list)
1828 snd_soc_dapm_auto_nc_codec_pins(codec);
1830 ret = snd_card_register(card->snd_card);
1832 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1834 goto probe_aux_dev_err;
1837 #ifdef CONFIG_SND_SOC_AC97_BUS
1838 /* register any AC97 codecs */
1839 for (i = 0; i < card->num_rtd; i++) {
1840 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1843 "ASoC: failed to register AC97: %d\n", ret);
1845 soc_unregister_ac97_dai_link(card->rtd[i].codec);
1846 goto probe_aux_dev_err;
1851 card->instantiated = 1;
1852 snd_soc_dapm_sync(&card->dapm);
1853 mutex_unlock(&card->mutex);
1858 for (i = 0; i < card->num_aux_devs; i++)
1859 soc_remove_aux_dev(card, i);
1862 soc_remove_dai_links(card);
1868 snd_card_free(card->snd_card);
1871 mutex_unlock(&card->mutex);
1876 /* probes a new socdev */
1877 static int soc_probe(struct platform_device *pdev)
1879 struct snd_soc_card *card = platform_get_drvdata(pdev);
1882 * no card, so machine driver should be registering card
1883 * we should not be here in that case so ret error
1888 dev_warn(&pdev->dev,
1889 "ASoC: machine %s should use snd_soc_register_card()\n",
1892 /* Bodge while we unpick instantiation */
1893 card->dev = &pdev->dev;
1895 return snd_soc_register_card(card);
1898 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1902 /* make sure any delayed work runs */
1903 for (i = 0; i < card->num_rtd; i++) {
1904 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1905 flush_delayed_work(&rtd->delayed_work);
1908 /* remove auxiliary devices */
1909 for (i = 0; i < card->num_aux_devs; i++)
1910 soc_remove_aux_dev(card, i);
1912 /* remove and free each DAI */
1913 soc_remove_dai_links(card);
1915 soc_cleanup_card_debugfs(card);
1917 /* remove the card */
1921 snd_soc_dapm_free(&card->dapm);
1923 snd_card_free(card->snd_card);
1928 /* removes a socdev */
1929 static int soc_remove(struct platform_device *pdev)
1931 struct snd_soc_card *card = platform_get_drvdata(pdev);
1933 snd_soc_unregister_card(card);
1937 int snd_soc_poweroff(struct device *dev)
1939 struct snd_soc_card *card = dev_get_drvdata(dev);
1942 if (!card->instantiated)
1945 /* Flush out pmdown_time work - we actually do want to run it
1946 * now, we're shutting down so no imminent restart. */
1947 for (i = 0; i < card->num_rtd; i++) {
1948 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1949 flush_delayed_work(&rtd->delayed_work);
1952 snd_soc_dapm_shutdown(card);
1956 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1958 const struct dev_pm_ops snd_soc_pm_ops = {
1959 .suspend = snd_soc_suspend,
1960 .resume = snd_soc_resume,
1961 .freeze = snd_soc_suspend,
1962 .thaw = snd_soc_resume,
1963 .poweroff = snd_soc_poweroff,
1964 .restore = snd_soc_resume,
1966 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1968 /* ASoC platform driver */
1969 static struct platform_driver soc_driver = {
1971 .name = "soc-audio",
1972 .owner = THIS_MODULE,
1973 .pm = &snd_soc_pm_ops,
1976 .remove = soc_remove,
1980 * snd_soc_codec_volatile_register: Report if a register is volatile.
1982 * @codec: CODEC to query.
1983 * @reg: Register to query.
1985 * Boolean function indiciating if a CODEC register is volatile.
1987 int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1990 if (codec->volatile_register)
1991 return codec->volatile_register(codec, reg);
1995 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1998 * snd_soc_codec_readable_register: Report if a register is readable.
2000 * @codec: CODEC to query.
2001 * @reg: Register to query.
2003 * Boolean function indicating if a CODEC register is readable.
2005 int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
2008 if (codec->readable_register)
2009 return codec->readable_register(codec, reg);
2013 EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
2016 * snd_soc_codec_writable_register: Report if a register is writable.
2018 * @codec: CODEC to query.
2019 * @reg: Register to query.
2021 * Boolean function indicating if a CODEC register is writable.
2023 int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
2026 if (codec->writable_register)
2027 return codec->writable_register(codec, reg);
2031 EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
2033 int snd_soc_platform_read(struct snd_soc_platform *platform,
2038 if (!platform->driver->read) {
2039 dev_err(platform->dev, "ASoC: platform has no read back\n");
2043 ret = platform->driver->read(platform, reg);
2044 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
2045 trace_snd_soc_preg_read(platform, reg, ret);
2049 EXPORT_SYMBOL_GPL(snd_soc_platform_read);
2051 int snd_soc_platform_write(struct snd_soc_platform *platform,
2052 unsigned int reg, unsigned int val)
2054 if (!platform->driver->write) {
2055 dev_err(platform->dev, "ASoC: platform has no write back\n");
2059 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
2060 trace_snd_soc_preg_write(platform, reg, val);
2061 return platform->driver->write(platform, reg, val);
2063 EXPORT_SYMBOL_GPL(snd_soc_platform_write);
2066 * snd_soc_new_ac97_codec - initailise AC97 device
2067 * @codec: audio codec
2068 * @ops: AC97 bus operations
2069 * @num: AC97 codec number
2071 * Initialises AC97 codec resources for use by ad-hoc devices only.
2073 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2074 struct snd_ac97_bus_ops *ops, int num)
2076 mutex_lock(&codec->mutex);
2078 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2079 if (codec->ac97 == NULL) {
2080 mutex_unlock(&codec->mutex);
2084 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2085 if (codec->ac97->bus == NULL) {
2088 mutex_unlock(&codec->mutex);
2092 codec->ac97->bus->ops = ops;
2093 codec->ac97->num = num;
2096 * Mark the AC97 device to be created by us. This way we ensure that the
2097 * device will be registered with the device subsystem later on.
2099 codec->ac97_created = 1;
2101 mutex_unlock(&codec->mutex);
2104 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2106 static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2108 static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2110 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2112 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2114 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2118 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2120 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2124 static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2126 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2128 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2130 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2131 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2132 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2136 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2138 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2142 static int snd_soc_ac97_parse_pinctl(struct device *dev,
2143 struct snd_ac97_reset_cfg *cfg)
2146 struct pinctrl_state *state;
2150 p = devm_pinctrl_get(dev);
2152 dev_err(dev, "Failed to get pinctrl\n");
2157 state = pinctrl_lookup_state(p, "ac97-reset");
2158 if (IS_ERR(state)) {
2159 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
2160 return PTR_RET(state);
2162 cfg->pstate_reset = state;
2164 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2165 if (IS_ERR(state)) {
2166 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
2167 return PTR_RET(state);
2169 cfg->pstate_warm_reset = state;
2171 state = pinctrl_lookup_state(p, "ac97-running");
2172 if (IS_ERR(state)) {
2173 dev_err(dev, "Can't find pinctrl state ac97-running\n");
2174 return PTR_RET(state);
2176 cfg->pstate_run = state;
2178 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2180 dev_err(dev, "Can't find ac97-sync gpio\n");
2183 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2185 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2188 cfg->gpio_sync = gpio;
2190 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2192 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2195 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2197 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2200 cfg->gpio_sdata = gpio;
2202 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2204 dev_err(dev, "Can't find ac97-reset gpio\n");
2207 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2209 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2212 cfg->gpio_reset = gpio;
2217 struct snd_ac97_bus_ops *soc_ac97_ops;
2218 EXPORT_SYMBOL_GPL(soc_ac97_ops);
2220 int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2222 if (ops == soc_ac97_ops)
2225 if (soc_ac97_ops && ops)
2232 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2235 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2237 * This function sets the reset and warm_reset properties of ops and parses
2238 * the device node of pdev to get pinctrl states and gpio numbers to use.
2240 int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2241 struct platform_device *pdev)
2243 struct device *dev = &pdev->dev;
2244 struct snd_ac97_reset_cfg cfg;
2247 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2251 ret = snd_soc_set_ac97_ops(ops);
2255 ops->warm_reset = snd_soc_ac97_warm_reset;
2256 ops->reset = snd_soc_ac97_reset;
2258 snd_ac97_rst_cfg = cfg;
2261 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2264 * snd_soc_free_ac97_codec - free AC97 codec device
2265 * @codec: audio codec
2267 * Frees AC97 codec device resources.
2269 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2271 mutex_lock(&codec->mutex);
2272 #ifdef CONFIG_SND_SOC_AC97_BUS
2273 soc_unregister_ac97_dai_link(codec);
2275 kfree(codec->ac97->bus);
2278 codec->ac97_created = 0;
2279 mutex_unlock(&codec->mutex);
2281 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2283 unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2287 ret = codec->read(codec, reg);
2288 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
2289 trace_snd_soc_reg_read(codec, reg, ret);
2293 EXPORT_SYMBOL_GPL(snd_soc_read);
2295 unsigned int snd_soc_write(struct snd_soc_codec *codec,
2296 unsigned int reg, unsigned int val)
2298 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
2299 trace_snd_soc_reg_write(codec, reg, val);
2300 return codec->write(codec, reg, val);
2302 EXPORT_SYMBOL_GPL(snd_soc_write);
2304 unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
2305 unsigned int reg, const void *data, size_t len)
2307 return codec->bulk_write_raw(codec, reg, data, len);
2309 EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
2312 * snd_soc_update_bits - update codec register bits
2313 * @codec: audio codec
2314 * @reg: codec register
2315 * @mask: register mask
2318 * Writes new register value.
2320 * Returns 1 for change, 0 for no change, or negative error code.
2322 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
2323 unsigned int mask, unsigned int value)
2326 unsigned int old, new;
2329 if (codec->using_regmap) {
2330 ret = regmap_update_bits_check(codec->control_data, reg,
2331 mask, value, &change);
2333 ret = snd_soc_read(codec, reg);
2338 new = (old & ~mask) | (value & mask);
2339 change = old != new;
2341 ret = snd_soc_write(codec, reg, new);
2349 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2352 * snd_soc_update_bits_locked - update codec register bits
2353 * @codec: audio codec
2354 * @reg: codec register
2355 * @mask: register mask
2358 * Writes new register value, and takes the codec mutex.
2360 * Returns 1 for change else 0.
2362 int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2363 unsigned short reg, unsigned int mask,
2368 mutex_lock(&codec->mutex);
2369 change = snd_soc_update_bits(codec, reg, mask, value);
2370 mutex_unlock(&codec->mutex);
2374 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
2377 * snd_soc_test_bits - test register for change
2378 * @codec: audio codec
2379 * @reg: codec register
2380 * @mask: register mask
2383 * Tests a register with a new value and checks if the new value is
2384 * different from the old value.
2386 * Returns 1 for change else 0.
2388 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
2389 unsigned int mask, unsigned int value)
2392 unsigned int old, new;
2394 old = snd_soc_read(codec, reg);
2395 new = (old & ~mask) | value;
2396 change = old != new;
2400 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2403 * snd_soc_cnew - create new control
2404 * @_template: control template
2405 * @data: control private data
2406 * @long_name: control long name
2407 * @prefix: control name prefix
2409 * Create a new mixer control from a template control.
2411 * Returns 0 for success, else error.
2413 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2414 void *data, const char *long_name,
2417 struct snd_kcontrol_new template;
2418 struct snd_kcontrol *kcontrol;
2421 memcpy(&template, _template, sizeof(template));
2425 long_name = template.name;
2428 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2432 template.name = name;
2434 template.name = long_name;
2437 kcontrol = snd_ctl_new1(&template, data);
2443 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2445 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2446 const struct snd_kcontrol_new *controls, int num_controls,
2447 const char *prefix, void *data)
2451 for (i = 0; i < num_controls; i++) {
2452 const struct snd_kcontrol_new *control = &controls[i];
2453 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2454 control->name, prefix));
2456 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2457 control->name, err);
2465 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2468 struct snd_card *card = soc_card->snd_card;
2469 struct snd_kcontrol *kctl;
2471 if (unlikely(!name))
2474 list_for_each_entry(kctl, &card->controls, list)
2475 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2479 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2482 * snd_soc_add_codec_controls - add an array of controls to a codec.
2483 * Convenience function to add a list of controls. Many codecs were
2484 * duplicating this code.
2486 * @codec: codec to add controls to
2487 * @controls: array of controls to add
2488 * @num_controls: number of elements in the array
2490 * Return 0 for success, else error.
2492 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2493 const struct snd_kcontrol_new *controls, int num_controls)
2495 struct snd_card *card = codec->card->snd_card;
2497 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2498 codec->name_prefix, codec);
2500 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2503 * snd_soc_add_platform_controls - add an array of controls to a platform.
2504 * Convenience function to add a list of controls.
2506 * @platform: platform to add controls to
2507 * @controls: array of controls to add
2508 * @num_controls: number of elements in the array
2510 * Return 0 for success, else error.
2512 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2513 const struct snd_kcontrol_new *controls, int num_controls)
2515 struct snd_card *card = platform->card->snd_card;
2517 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2520 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2523 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2524 * Convenience function to add a list of controls.
2526 * @soc_card: SoC card to add controls to
2527 * @controls: array of controls to add
2528 * @num_controls: number of elements in the array
2530 * Return 0 for success, else error.
2532 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2533 const struct snd_kcontrol_new *controls, int num_controls)
2535 struct snd_card *card = soc_card->snd_card;
2537 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2540 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2543 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2544 * Convienience function to add a list of controls.
2546 * @dai: DAI to add controls to
2547 * @controls: array of controls to add
2548 * @num_controls: number of elements in the array
2550 * Return 0 for success, else error.
2552 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2553 const struct snd_kcontrol_new *controls, int num_controls)
2555 struct snd_card *card = dai->card->snd_card;
2557 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2560 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2563 * snd_soc_info_enum_double - enumerated double mixer info callback
2564 * @kcontrol: mixer control
2565 * @uinfo: control element information
2567 * Callback to provide information about a double enumerated
2570 * Returns 0 for success.
2572 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2573 struct snd_ctl_elem_info *uinfo)
2575 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2577 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2578 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2579 uinfo->value.enumerated.items = e->max;
2581 if (uinfo->value.enumerated.item > e->max - 1)
2582 uinfo->value.enumerated.item = e->max - 1;
2583 strcpy(uinfo->value.enumerated.name,
2584 e->texts[uinfo->value.enumerated.item]);
2587 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2590 * snd_soc_get_enum_double - enumerated double mixer get callback
2591 * @kcontrol: mixer control
2592 * @ucontrol: control element information
2594 * Callback to get the value of a double enumerated mixer.
2596 * Returns 0 for success.
2598 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2599 struct snd_ctl_elem_value *ucontrol)
2601 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2602 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2605 val = snd_soc_read(codec, e->reg);
2606 ucontrol->value.enumerated.item[0]
2607 = (val >> e->shift_l) & e->mask;
2608 if (e->shift_l != e->shift_r)
2609 ucontrol->value.enumerated.item[1] =
2610 (val >> e->shift_r) & e->mask;
2614 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2617 * snd_soc_put_enum_double - enumerated double mixer put callback
2618 * @kcontrol: mixer control
2619 * @ucontrol: control element information
2621 * Callback to set the value of a double enumerated mixer.
2623 * Returns 0 for success.
2625 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2626 struct snd_ctl_elem_value *ucontrol)
2628 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2629 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2633 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2635 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2636 mask = e->mask << e->shift_l;
2637 if (e->shift_l != e->shift_r) {
2638 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2640 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2641 mask |= e->mask << e->shift_r;
2644 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2646 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2649 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2650 * @kcontrol: mixer control
2651 * @ucontrol: control element information
2653 * Callback to get the value of a double semi enumerated mixer.
2655 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2656 * used for handling bitfield coded enumeration for example.
2658 * Returns 0 for success.
2660 int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2661 struct snd_ctl_elem_value *ucontrol)
2663 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2664 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2665 unsigned int reg_val, val, mux;
2667 reg_val = snd_soc_read(codec, e->reg);
2668 val = (reg_val >> e->shift_l) & e->mask;
2669 for (mux = 0; mux < e->max; mux++) {
2670 if (val == e->values[mux])
2673 ucontrol->value.enumerated.item[0] = mux;
2674 if (e->shift_l != e->shift_r) {
2675 val = (reg_val >> e->shift_r) & e->mask;
2676 for (mux = 0; mux < e->max; mux++) {
2677 if (val == e->values[mux])
2680 ucontrol->value.enumerated.item[1] = mux;
2685 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2688 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2689 * @kcontrol: mixer control
2690 * @ucontrol: control element information
2692 * Callback to set the value of a double semi enumerated mixer.
2694 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2695 * used for handling bitfield coded enumeration for example.
2697 * Returns 0 for success.
2699 int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2700 struct snd_ctl_elem_value *ucontrol)
2702 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2703 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2707 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2709 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2710 mask = e->mask << e->shift_l;
2711 if (e->shift_l != e->shift_r) {
2712 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2714 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2715 mask |= e->mask << e->shift_r;
2718 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2720 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2723 * snd_soc_info_volsw - single mixer info callback
2724 * @kcontrol: mixer control
2725 * @uinfo: control element information
2727 * Callback to provide information about a single mixer control, or a double
2728 * mixer control that spans 2 registers.
2730 * Returns 0 for success.
2732 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2733 struct snd_ctl_elem_info *uinfo)
2735 struct soc_mixer_control *mc =
2736 (struct soc_mixer_control *)kcontrol->private_value;
2739 if (!mc->platform_max)
2740 mc->platform_max = mc->max;
2741 platform_max = mc->platform_max;
2743 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2744 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2746 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2748 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2749 uinfo->value.integer.min = 0;
2750 uinfo->value.integer.max = platform_max;
2753 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2756 * snd_soc_get_volsw - single mixer get callback
2757 * @kcontrol: mixer control
2758 * @ucontrol: control element information
2760 * Callback to get the value of a single mixer control, or a double mixer
2761 * control that spans 2 registers.
2763 * Returns 0 for success.
2765 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2766 struct snd_ctl_elem_value *ucontrol)
2768 struct soc_mixer_control *mc =
2769 (struct soc_mixer_control *)kcontrol->private_value;
2770 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2771 unsigned int reg = mc->reg;
2772 unsigned int reg2 = mc->rreg;
2773 unsigned int shift = mc->shift;
2774 unsigned int rshift = mc->rshift;
2776 unsigned int mask = (1 << fls(max)) - 1;
2777 unsigned int invert = mc->invert;
2779 ucontrol->value.integer.value[0] =
2780 (snd_soc_read(codec, reg) >> shift) & mask;
2782 ucontrol->value.integer.value[0] =
2783 max - ucontrol->value.integer.value[0];
2785 if (snd_soc_volsw_is_stereo(mc)) {
2787 ucontrol->value.integer.value[1] =
2788 (snd_soc_read(codec, reg) >> rshift) & mask;
2790 ucontrol->value.integer.value[1] =
2791 (snd_soc_read(codec, reg2) >> shift) & mask;
2793 ucontrol->value.integer.value[1] =
2794 max - ucontrol->value.integer.value[1];
2799 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2802 * snd_soc_put_volsw - single mixer put callback
2803 * @kcontrol: mixer control
2804 * @ucontrol: control element information
2806 * Callback to set the value of a single mixer control, or a double mixer
2807 * control that spans 2 registers.
2809 * Returns 0 for success.
2811 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2812 struct snd_ctl_elem_value *ucontrol)
2814 struct soc_mixer_control *mc =
2815 (struct soc_mixer_control *)kcontrol->private_value;
2816 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2817 unsigned int reg = mc->reg;
2818 unsigned int reg2 = mc->rreg;
2819 unsigned int shift = mc->shift;
2820 unsigned int rshift = mc->rshift;
2822 unsigned int mask = (1 << fls(max)) - 1;
2823 unsigned int invert = mc->invert;
2826 unsigned int val2 = 0;
2827 unsigned int val, val_mask;
2829 val = (ucontrol->value.integer.value[0] & mask);
2832 val_mask = mask << shift;
2834 if (snd_soc_volsw_is_stereo(mc)) {
2835 val2 = (ucontrol->value.integer.value[1] & mask);
2839 val_mask |= mask << rshift;
2840 val |= val2 << rshift;
2842 val2 = val2 << shift;
2846 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2851 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2855 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2858 * snd_soc_get_volsw_sx - single mixer get callback
2859 * @kcontrol: mixer control
2860 * @ucontrol: control element information
2862 * Callback to get the value of a single mixer control, or a double mixer
2863 * control that spans 2 registers.
2865 * Returns 0 for success.
2867 int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2868 struct snd_ctl_elem_value *ucontrol)
2870 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2871 struct soc_mixer_control *mc =
2872 (struct soc_mixer_control *)kcontrol->private_value;
2874 unsigned int reg = mc->reg;
2875 unsigned int reg2 = mc->rreg;
2876 unsigned int shift = mc->shift;
2877 unsigned int rshift = mc->rshift;
2880 int mask = (1 << (fls(min + max) - 1)) - 1;
2882 ucontrol->value.integer.value[0] =
2883 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2885 if (snd_soc_volsw_is_stereo(mc))
2886 ucontrol->value.integer.value[1] =
2887 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2891 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2894 * snd_soc_put_volsw_sx - double mixer set callback
2895 * @kcontrol: mixer control
2896 * @uinfo: control element information
2898 * Callback to set the value of a double mixer control that spans 2 registers.
2900 * Returns 0 for success.
2902 int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2903 struct snd_ctl_elem_value *ucontrol)
2905 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2906 struct soc_mixer_control *mc =
2907 (struct soc_mixer_control *)kcontrol->private_value;
2909 unsigned int reg = mc->reg;
2910 unsigned int reg2 = mc->rreg;
2911 unsigned int shift = mc->shift;
2912 unsigned int rshift = mc->rshift;
2915 int mask = (1 << (fls(min + max) - 1)) - 1;
2917 unsigned short val, val_mask, val2 = 0;
2919 val_mask = mask << shift;
2920 val = (ucontrol->value.integer.value[0] + min) & mask;
2923 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2927 if (snd_soc_volsw_is_stereo(mc)) {
2928 val_mask = mask << rshift;
2929 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2930 val2 = val2 << rshift;
2932 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2937 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2940 * snd_soc_info_volsw_s8 - signed mixer info callback
2941 * @kcontrol: mixer control
2942 * @uinfo: control element information
2944 * Callback to provide information about a signed mixer control.
2946 * Returns 0 for success.
2948 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2949 struct snd_ctl_elem_info *uinfo)
2951 struct soc_mixer_control *mc =
2952 (struct soc_mixer_control *)kcontrol->private_value;
2956 if (!mc->platform_max)
2957 mc->platform_max = mc->max;
2958 platform_max = mc->platform_max;
2960 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2962 uinfo->value.integer.min = 0;
2963 uinfo->value.integer.max = platform_max - min;
2966 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2969 * snd_soc_get_volsw_s8 - signed mixer get callback
2970 * @kcontrol: mixer control
2971 * @ucontrol: control element information
2973 * Callback to get the value of a signed mixer control.
2975 * Returns 0 for success.
2977 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2978 struct snd_ctl_elem_value *ucontrol)
2980 struct soc_mixer_control *mc =
2981 (struct soc_mixer_control *)kcontrol->private_value;
2982 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2983 unsigned int reg = mc->reg;
2985 int val = snd_soc_read(codec, reg);
2987 ucontrol->value.integer.value[0] =
2988 ((signed char)(val & 0xff))-min;
2989 ucontrol->value.integer.value[1] =
2990 ((signed char)((val >> 8) & 0xff))-min;
2993 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2996 * snd_soc_put_volsw_sgn - signed mixer put callback
2997 * @kcontrol: mixer control
2998 * @ucontrol: control element information
3000 * Callback to set the value of a signed mixer control.
3002 * Returns 0 for success.
3004 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
3005 struct snd_ctl_elem_value *ucontrol)
3007 struct soc_mixer_control *mc =
3008 (struct soc_mixer_control *)kcontrol->private_value;
3009 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3010 unsigned int reg = mc->reg;
3014 val = (ucontrol->value.integer.value[0]+min) & 0xff;
3015 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
3017 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
3019 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
3022 * snd_soc_info_volsw_range - single mixer info callback with range.
3023 * @kcontrol: mixer control
3024 * @uinfo: control element information
3026 * Callback to provide information, within a range, about a single
3029 * returns 0 for success.
3031 int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
3032 struct snd_ctl_elem_info *uinfo)
3034 struct soc_mixer_control *mc =
3035 (struct soc_mixer_control *)kcontrol->private_value;
3039 if (!mc->platform_max)
3040 mc->platform_max = mc->max;
3041 platform_max = mc->platform_max;
3043 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3044 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
3045 uinfo->value.integer.min = 0;
3046 uinfo->value.integer.max = platform_max - min;
3050 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
3053 * snd_soc_put_volsw_range - single mixer put value callback with range.
3054 * @kcontrol: mixer control
3055 * @ucontrol: control element information
3057 * Callback to set the value, within a range, for a single mixer control.
3059 * Returns 0 for success.
3061 int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
3062 struct snd_ctl_elem_value *ucontrol)
3064 struct soc_mixer_control *mc =
3065 (struct soc_mixer_control *)kcontrol->private_value;
3066 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3067 unsigned int reg = mc->reg;
3068 unsigned int rreg = mc->rreg;
3069 unsigned int shift = mc->shift;
3072 unsigned int mask = (1 << fls(max)) - 1;
3073 unsigned int invert = mc->invert;
3074 unsigned int val, val_mask;
3077 val = ((ucontrol->value.integer.value[0] + min) & mask);
3080 val_mask = mask << shift;
3083 ret = snd_soc_update_bits_locked(codec, reg, val_mask, val);
3087 if (snd_soc_volsw_is_stereo(mc)) {
3088 val = ((ucontrol->value.integer.value[1] + min) & mask);
3091 val_mask = mask << shift;
3094 ret = snd_soc_update_bits_locked(codec, rreg, val_mask, val);
3099 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
3102 * snd_soc_get_volsw_range - single mixer get callback with range
3103 * @kcontrol: mixer control
3104 * @ucontrol: control element information
3106 * Callback to get the value, within a range, of a single mixer control.
3108 * Returns 0 for success.
3110 int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
3111 struct snd_ctl_elem_value *ucontrol)
3113 struct soc_mixer_control *mc =
3114 (struct soc_mixer_control *)kcontrol->private_value;
3115 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3116 unsigned int reg = mc->reg;
3117 unsigned int rreg = mc->rreg;
3118 unsigned int shift = mc->shift;
3121 unsigned int mask = (1 << fls(max)) - 1;
3122 unsigned int invert = mc->invert;
3124 ucontrol->value.integer.value[0] =
3125 (snd_soc_read(codec, reg) >> shift) & mask;
3127 ucontrol->value.integer.value[0] =
3128 max - ucontrol->value.integer.value[0];
3129 ucontrol->value.integer.value[0] =
3130 ucontrol->value.integer.value[0] - min;
3132 if (snd_soc_volsw_is_stereo(mc)) {
3133 ucontrol->value.integer.value[1] =
3134 (snd_soc_read(codec, rreg) >> shift) & mask;
3136 ucontrol->value.integer.value[1] =
3137 max - ucontrol->value.integer.value[1];
3138 ucontrol->value.integer.value[1] =
3139 ucontrol->value.integer.value[1] - min;
3144 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3147 * snd_soc_limit_volume - Set new limit to an existing volume control.
3149 * @codec: where to look for the control
3150 * @name: Name of the control
3151 * @max: new maximum limit
3153 * Return 0 for success, else error.
3155 int snd_soc_limit_volume(struct snd_soc_codec *codec,
3156 const char *name, int max)
3158 struct snd_card *card = codec->card->snd_card;
3159 struct snd_kcontrol *kctl;
3160 struct soc_mixer_control *mc;
3164 /* Sanity check for name and max */
3165 if (unlikely(!name || max <= 0))
3168 list_for_each_entry(kctl, &card->controls, list) {
3169 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3175 mc = (struct soc_mixer_control *)kctl->private_value;
3176 if (max <= mc->max) {
3177 mc->platform_max = max;
3183 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3185 int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3186 struct snd_ctl_elem_info *uinfo)
3188 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3189 struct soc_bytes *params = (void *)kcontrol->private_value;
3191 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3192 uinfo->count = params->num_regs * codec->val_bytes;
3196 EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3198 int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3199 struct snd_ctl_elem_value *ucontrol)
3201 struct soc_bytes *params = (void *)kcontrol->private_value;
3202 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3205 if (codec->using_regmap)
3206 ret = regmap_raw_read(codec->control_data, params->base,
3207 ucontrol->value.bytes.data,
3208 params->num_regs * codec->val_bytes);
3212 /* Hide any masked bytes to ensure consistent data reporting */
3213 if (ret == 0 && params->mask) {
3214 switch (codec->val_bytes) {
3216 ucontrol->value.bytes.data[0] &= ~params->mask;
3219 ((u16 *)(&ucontrol->value.bytes.data))[0]
3223 ((u32 *)(&ucontrol->value.bytes.data))[0]
3233 EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3235 int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3236 struct snd_ctl_elem_value *ucontrol)
3238 struct soc_bytes *params = (void *)kcontrol->private_value;
3239 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3244 if (!codec->using_regmap)
3247 len = params->num_regs * codec->val_bytes;
3249 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3254 * If we've got a mask then we need to preserve the register
3255 * bits. We shouldn't modify the incoming data so take a
3259 ret = regmap_read(codec->control_data, params->base, &val);
3263 val &= params->mask;
3265 switch (codec->val_bytes) {
3267 ((u8 *)data)[0] &= ~params->mask;
3268 ((u8 *)data)[0] |= val;
3271 ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
3272 ((u16 *)data)[0] |= cpu_to_be16(val);
3275 ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
3276 ((u32 *)data)[0] |= cpu_to_be32(val);
3284 ret = regmap_raw_write(codec->control_data, params->base,
3292 EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3295 * snd_soc_info_xr_sx - signed multi register info callback
3296 * @kcontrol: mreg control
3297 * @uinfo: control element information
3299 * Callback to provide information of a control that can
3300 * span multiple codec registers which together
3301 * forms a single signed value in a MSB/LSB manner.
3303 * Returns 0 for success.
3305 int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3306 struct snd_ctl_elem_info *uinfo)
3308 struct soc_mreg_control *mc =
3309 (struct soc_mreg_control *)kcontrol->private_value;
3310 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3312 uinfo->value.integer.min = mc->min;
3313 uinfo->value.integer.max = mc->max;
3317 EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3320 * snd_soc_get_xr_sx - signed multi register get callback
3321 * @kcontrol: mreg control
3322 * @ucontrol: control element information
3324 * Callback to get the value of a control that can span
3325 * multiple codec registers which together forms a single
3326 * signed value in a MSB/LSB manner. The control supports
3327 * specifying total no of bits used to allow for bitfields
3328 * across the multiple codec registers.
3330 * Returns 0 for success.
3332 int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3333 struct snd_ctl_elem_value *ucontrol)
3335 struct soc_mreg_control *mc =
3336 (struct soc_mreg_control *)kcontrol->private_value;
3337 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3338 unsigned int regbase = mc->regbase;
3339 unsigned int regcount = mc->regcount;
3340 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3341 unsigned int regwmask = (1<<regwshift)-1;
3342 unsigned int invert = mc->invert;
3343 unsigned long mask = (1UL<<mc->nbits)-1;
3347 unsigned long regval;
3350 for (i = 0; i < regcount; i++) {
3351 regval = snd_soc_read(codec, regbase+i) & regwmask;
3352 val |= regval << (regwshift*(regcount-i-1));
3355 if (min < 0 && val > max)
3359 ucontrol->value.integer.value[0] = val;
3363 EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3366 * snd_soc_put_xr_sx - signed multi register get callback
3367 * @kcontrol: mreg control
3368 * @ucontrol: control element information
3370 * Callback to set the value of a control that can span
3371 * multiple codec registers which together forms a single
3372 * signed value in a MSB/LSB manner. The control supports
3373 * specifying total no of bits used to allow for bitfields
3374 * across the multiple codec registers.
3376 * Returns 0 for success.
3378 int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3379 struct snd_ctl_elem_value *ucontrol)
3381 struct soc_mreg_control *mc =
3382 (struct soc_mreg_control *)kcontrol->private_value;
3383 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3384 unsigned int regbase = mc->regbase;
3385 unsigned int regcount = mc->regcount;
3386 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3387 unsigned int regwmask = (1<<regwshift)-1;
3388 unsigned int invert = mc->invert;
3389 unsigned long mask = (1UL<<mc->nbits)-1;
3391 long val = ucontrol->value.integer.value[0];
3392 unsigned int i, regval, regmask;
3398 for (i = 0; i < regcount; i++) {
3399 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3400 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3401 err = snd_soc_update_bits_locked(codec, regbase+i,
3409 EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3412 * snd_soc_get_strobe - strobe get callback
3413 * @kcontrol: mixer control
3414 * @ucontrol: control element information
3416 * Callback get the value of a strobe mixer control.
3418 * Returns 0 for success.
3420 int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3421 struct snd_ctl_elem_value *ucontrol)
3423 struct soc_mixer_control *mc =
3424 (struct soc_mixer_control *)kcontrol->private_value;
3425 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3426 unsigned int reg = mc->reg;
3427 unsigned int shift = mc->shift;
3428 unsigned int mask = 1 << shift;
3429 unsigned int invert = mc->invert != 0;
3430 unsigned int val = snd_soc_read(codec, reg) & mask;
3432 if (shift != 0 && val != 0)
3434 ucontrol->value.enumerated.item[0] = val ^ invert;
3438 EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3441 * snd_soc_put_strobe - strobe put callback
3442 * @kcontrol: mixer control
3443 * @ucontrol: control element information
3445 * Callback strobe a register bit to high then low (or the inverse)
3446 * in one pass of a single mixer enum control.
3448 * Returns 1 for success.
3450 int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3451 struct snd_ctl_elem_value *ucontrol)
3453 struct soc_mixer_control *mc =
3454 (struct soc_mixer_control *)kcontrol->private_value;
3455 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3456 unsigned int reg = mc->reg;
3457 unsigned int shift = mc->shift;
3458 unsigned int mask = 1 << shift;
3459 unsigned int invert = mc->invert != 0;
3460 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3461 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3462 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3465 err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3469 err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3472 EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3475 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3477 * @clk_id: DAI specific clock ID
3478 * @freq: new clock frequency in Hz
3479 * @dir: new clock direction - input/output.
3481 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3483 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3484 unsigned int freq, int dir)
3486 if (dai->driver && dai->driver->ops->set_sysclk)
3487 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3488 else if (dai->codec && dai->codec->driver->set_sysclk)
3489 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3494 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3497 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3499 * @clk_id: DAI specific clock ID
3500 * @source: Source for the clock
3501 * @freq: new clock frequency in Hz
3502 * @dir: new clock direction - input/output.
3504 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3506 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3507 int source, unsigned int freq, int dir)
3509 if (codec->driver->set_sysclk)
3510 return codec->driver->set_sysclk(codec, clk_id, source,
3515 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3518 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3520 * @div_id: DAI specific clock divider ID
3521 * @div: new clock divisor.
3523 * Configures the clock dividers. This is used to derive the best DAI bit and
3524 * frame clocks from the system or master clock. It's best to set the DAI bit
3525 * and frame clocks as low as possible to save system power.
3527 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3528 int div_id, int div)
3530 if (dai->driver && dai->driver->ops->set_clkdiv)
3531 return dai->driver->ops->set_clkdiv(dai, div_id, div);
3535 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3538 * snd_soc_dai_set_pll - configure DAI PLL.
3540 * @pll_id: DAI specific PLL ID
3541 * @source: DAI specific source for the PLL
3542 * @freq_in: PLL input clock frequency in Hz
3543 * @freq_out: requested PLL output clock frequency in Hz
3545 * Configures and enables PLL to generate output clock based on input clock.
3547 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3548 unsigned int freq_in, unsigned int freq_out)
3550 if (dai->driver && dai->driver->ops->set_pll)
3551 return dai->driver->ops->set_pll(dai, pll_id, source,
3553 else if (dai->codec && dai->codec->driver->set_pll)
3554 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3559 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3562 * snd_soc_codec_set_pll - configure codec PLL.
3564 * @pll_id: DAI specific PLL ID
3565 * @source: DAI specific source for the PLL
3566 * @freq_in: PLL input clock frequency in Hz
3567 * @freq_out: requested PLL output clock frequency in Hz
3569 * Configures and enables PLL to generate output clock based on input clock.
3571 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3572 unsigned int freq_in, unsigned int freq_out)
3574 if (codec->driver->set_pll)
3575 return codec->driver->set_pll(codec, pll_id, source,
3580 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3583 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3585 * @fmt: SND_SOC_DAIFMT_ format value.
3587 * Configures the DAI hardware format and clocking.
3589 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3591 if (dai->driver == NULL)
3593 if (dai->driver->ops->set_fmt == NULL)
3595 return dai->driver->ops->set_fmt(dai, fmt);
3597 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3600 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3602 * @tx_mask: bitmask representing active TX slots.
3603 * @rx_mask: bitmask representing active RX slots.
3604 * @slots: Number of slots in use.
3605 * @slot_width: Width in bits for each slot.
3607 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3610 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3611 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
3613 if (dai->driver && dai->driver->ops->set_tdm_slot)
3614 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3619 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3622 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3624 * @tx_num: how many TX channels
3625 * @tx_slot: pointer to an array which imply the TX slot number channel
3627 * @rx_num: how many RX channels
3628 * @rx_slot: pointer to an array which imply the RX slot number channel
3631 * configure the relationship between channel number and TDM slot number.
3633 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3634 unsigned int tx_num, unsigned int *tx_slot,
3635 unsigned int rx_num, unsigned int *rx_slot)
3637 if (dai->driver && dai->driver->ops->set_channel_map)
3638 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3643 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3646 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3648 * @tristate: tristate enable
3650 * Tristates the DAI so that others can use it.
3652 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3654 if (dai->driver && dai->driver->ops->set_tristate)
3655 return dai->driver->ops->set_tristate(dai, tristate);
3659 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3662 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3664 * @mute: mute enable
3665 * @direction: stream to mute
3667 * Mutes the DAI DAC.
3669 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3675 if (dai->driver->ops->mute_stream)
3676 return dai->driver->ops->mute_stream(dai, mute, direction);
3677 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3678 dai->driver->ops->digital_mute)
3679 return dai->driver->ops->digital_mute(dai, mute);
3683 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3686 * snd_soc_register_card - Register a card with the ASoC core
3688 * @card: Card to register
3691 int snd_soc_register_card(struct snd_soc_card *card)
3695 if (!card->name || !card->dev)
3698 for (i = 0; i < card->num_links; i++) {
3699 struct snd_soc_dai_link *link = &card->dai_link[i];
3702 * Codec must be specified by 1 of name or OF node,
3703 * not both or neither.
3705 if (!!link->codec_name == !!link->codec_of_node) {
3707 "ASoC: Neither/both codec name/of_node are set for %s\n",
3711 /* Codec DAI name must be specified */
3712 if (!link->codec_dai_name) {
3714 "ASoC: codec_dai_name not set for %s\n",
3720 * Platform may be specified by either name or OF node, but
3721 * can be left unspecified, and a dummy platform will be used.
3723 if (link->platform_name && link->platform_of_node) {
3725 "ASoC: Both platform name/of_node are set for %s\n",
3731 * CPU device may be specified by either name or OF node, but
3732 * can be left unspecified, and will be matched based on DAI
3735 if (link->cpu_name && link->cpu_of_node) {
3737 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3742 * At least one of CPU DAI name or CPU device name/node must be
3745 if (!link->cpu_dai_name &&
3746 !(link->cpu_name || link->cpu_of_node)) {
3748 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3754 dev_set_drvdata(card->dev, card);
3756 snd_soc_initialize_card_lists(card);
3758 soc_init_card_debugfs(card);
3760 card->rtd = devm_kzalloc(card->dev,
3761 sizeof(struct snd_soc_pcm_runtime) *
3762 (card->num_links + card->num_aux_devs),
3764 if (card->rtd == NULL)
3767 card->rtd_aux = &card->rtd[card->num_links];
3769 for (i = 0; i < card->num_links; i++)
3770 card->rtd[i].dai_link = &card->dai_link[i];
3772 INIT_LIST_HEAD(&card->list);
3773 INIT_LIST_HEAD(&card->dapm_dirty);
3774 card->instantiated = 0;
3775 mutex_init(&card->mutex);
3776 mutex_init(&card->dapm_mutex);
3778 ret = snd_soc_instantiate_card(card);
3780 soc_cleanup_card_debugfs(card);
3784 EXPORT_SYMBOL_GPL(snd_soc_register_card);
3787 * snd_soc_unregister_card - Unregister a card with the ASoC core
3789 * @card: Card to unregister
3792 int snd_soc_unregister_card(struct snd_soc_card *card)
3794 if (card->instantiated)
3795 soc_cleanup_card_resources(card);
3796 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
3800 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3803 * Simplify DAI link configuration by removing ".-1" from device names
3804 * and sanitizing names.
3806 static char *fmt_single_name(struct device *dev, int *id)
3808 char *found, name[NAME_SIZE];
3811 if (dev_name(dev) == NULL)
3814 strlcpy(name, dev_name(dev), NAME_SIZE);
3816 /* are we a "%s.%d" name (platform and SPI components) */
3817 found = strstr(name, dev->driver->name);
3820 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3822 /* discard ID from name if ID == -1 */
3824 found[strlen(dev->driver->name)] = '\0';
3828 /* I2C component devices are named "bus-addr" */
3829 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3830 char tmp[NAME_SIZE];
3832 /* create unique ID number from I2C addr and bus */
3833 *id = ((id1 & 0xffff) << 16) + id2;
3835 /* sanitize component name for DAI link creation */
3836 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3837 strlcpy(name, tmp, NAME_SIZE);
3842 return kstrdup(name, GFP_KERNEL);
3846 * Simplify DAI link naming for single devices with multiple DAIs by removing
3847 * any ".-1" and using the DAI name (instead of device name).
3849 static inline char *fmt_multiple_name(struct device *dev,
3850 struct snd_soc_dai_driver *dai_drv)
3852 if (dai_drv->name == NULL) {
3854 "ASoC: error - multiple DAI %s registered with no name\n",
3859 return kstrdup(dai_drv->name, GFP_KERNEL);
3863 * snd_soc_register_dai - Register a DAI with the ASoC core
3865 * @dai: DAI to register
3867 static int snd_soc_register_dai(struct device *dev,
3868 struct snd_soc_dai_driver *dai_drv)
3870 struct snd_soc_codec *codec;
3871 struct snd_soc_dai *dai;
3873 dev_dbg(dev, "ASoC: dai register %s\n", dev_name(dev));
3875 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3879 /* create DAI component name */
3880 dai->name = fmt_single_name(dev, &dai->id);
3881 if (dai->name == NULL) {
3887 dai->driver = dai_drv;
3888 dai->dapm.dev = dev;
3889 if (!dai->driver->ops)
3890 dai->driver->ops = &null_dai_ops;
3892 mutex_lock(&client_mutex);
3894 list_for_each_entry(codec, &codec_list, list) {
3895 if (codec->dev == dev) {
3896 dev_dbg(dev, "ASoC: Mapped DAI %s to CODEC %s\n",
3897 dai->name, codec->name);
3904 dai->dapm.idle_bias_off = 1;
3906 list_add(&dai->list, &dai_list);
3908 mutex_unlock(&client_mutex);
3910 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3916 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3918 * @dai: DAI to unregister
3920 static void snd_soc_unregister_dai(struct device *dev)
3922 struct snd_soc_dai *dai;
3924 list_for_each_entry(dai, &dai_list, list) {
3925 if (dev == dai->dev)
3931 mutex_lock(&client_mutex);
3932 list_del(&dai->list);
3933 mutex_unlock(&client_mutex);
3935 dev_dbg(dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
3941 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3943 * @dai: Array of DAIs to register
3944 * @count: Number of DAIs
3946 static int snd_soc_register_dais(struct device *dev,
3947 struct snd_soc_dai_driver *dai_drv, size_t count)
3949 struct snd_soc_codec *codec;
3950 struct snd_soc_dai *dai;
3953 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
3955 for (i = 0; i < count; i++) {
3957 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3963 /* create DAI component name */
3964 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3965 if (dai->name == NULL) {
3972 dai->driver = &dai_drv[i];
3973 if (dai->driver->id)
3974 dai->id = dai->driver->id;
3977 dai->dapm.dev = dev;
3978 if (!dai->driver->ops)
3979 dai->driver->ops = &null_dai_ops;
3981 mutex_lock(&client_mutex);
3983 list_for_each_entry(codec, &codec_list, list) {
3984 if (codec->dev == dev) {
3986 "ASoC: Mapped DAI %s to CODEC %s\n",
3987 dai->name, codec->name);
3994 dai->dapm.idle_bias_off = 1;
3996 list_add(&dai->list, &dai_list);
3998 mutex_unlock(&client_mutex);
4000 dev_dbg(dai->dev, "ASoC: Registered DAI '%s'\n", dai->name);
4006 for (i--; i >= 0; i--)
4007 snd_soc_unregister_dai(dev);
4013 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
4015 * @dai: Array of DAIs to unregister
4016 * @count: Number of DAIs
4018 static void snd_soc_unregister_dais(struct device *dev, size_t count)
4022 for (i = 0; i < count; i++)
4023 snd_soc_unregister_dai(dev);
4027 * snd_soc_add_platform - Add a platform to the ASoC core
4028 * @dev: The parent device for the platform
4029 * @platform: The platform to add
4030 * @platform_driver: The driver for the platform
4032 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
4033 const struct snd_soc_platform_driver *platform_drv)
4035 /* create platform component name */
4036 platform->name = fmt_single_name(dev, &platform->id);
4037 if (platform->name == NULL)
4040 platform->dev = dev;
4041 platform->driver = platform_drv;
4042 platform->dapm.dev = dev;
4043 platform->dapm.platform = platform;
4044 platform->dapm.stream_event = platform_drv->stream_event;
4045 mutex_init(&platform->mutex);
4047 mutex_lock(&client_mutex);
4048 list_add(&platform->list, &platform_list);
4049 mutex_unlock(&client_mutex);
4051 dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
4055 EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4058 * snd_soc_register_platform - Register a platform with the ASoC core
4060 * @platform: platform to register
4062 int snd_soc_register_platform(struct device *dev,
4063 const struct snd_soc_platform_driver *platform_drv)
4065 struct snd_soc_platform *platform;
4068 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4070 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4071 if (platform == NULL)
4074 ret = snd_soc_add_platform(dev, platform, platform_drv);
4080 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4083 * snd_soc_remove_platform - Remove a platform from the ASoC core
4084 * @platform: the platform to remove
4086 void snd_soc_remove_platform(struct snd_soc_platform *platform)
4088 mutex_lock(&client_mutex);
4089 list_del(&platform->list);
4090 mutex_unlock(&client_mutex);
4092 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
4094 kfree(platform->name);
4096 EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4098 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4100 struct snd_soc_platform *platform;
4102 list_for_each_entry(platform, &platform_list, list) {
4103 if (dev == platform->dev)
4109 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4112 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4114 * @platform: platform to unregister
4116 void snd_soc_unregister_platform(struct device *dev)
4118 struct snd_soc_platform *platform;
4120 platform = snd_soc_lookup_platform(dev);
4124 snd_soc_remove_platform(platform);
4127 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4129 static u64 codec_format_map[] = {
4130 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4131 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4132 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4133 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4134 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4135 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4136 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4137 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4138 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4139 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4140 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4141 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4142 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4143 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4144 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4145 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4148 /* Fix up the DAI formats for endianness: codecs don't actually see
4149 * the endianness of the data but we're using the CPU format
4150 * definitions which do need to include endianness so we ensure that
4151 * codec DAIs always have both big and little endian variants set.
4153 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4157 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4158 if (stream->formats & codec_format_map[i])
4159 stream->formats |= codec_format_map[i];
4163 * snd_soc_register_codec - Register a codec with the ASoC core
4165 * @codec: codec to register
4167 int snd_soc_register_codec(struct device *dev,
4168 const struct snd_soc_codec_driver *codec_drv,
4169 struct snd_soc_dai_driver *dai_drv,
4173 struct snd_soc_codec *codec;
4176 dev_dbg(dev, "codec register %s\n", dev_name(dev));
4178 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4182 /* create CODEC component name */
4183 codec->name = fmt_single_name(dev, &codec->id);
4184 if (codec->name == NULL) {
4189 if (codec_drv->compress_type)
4190 codec->compress_type = codec_drv->compress_type;
4192 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
4194 codec->write = codec_drv->write;
4195 codec->read = codec_drv->read;
4196 codec->volatile_register = codec_drv->volatile_register;
4197 codec->readable_register = codec_drv->readable_register;
4198 codec->writable_register = codec_drv->writable_register;
4199 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
4200 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4201 codec->dapm.dev = dev;
4202 codec->dapm.codec = codec;
4203 codec->dapm.seq_notifier = codec_drv->seq_notifier;
4204 codec->dapm.stream_event = codec_drv->stream_event;
4206 codec->driver = codec_drv;
4207 codec->num_dai = num_dai;
4208 mutex_init(&codec->mutex);
4210 /* allocate CODEC register cache */
4211 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
4212 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
4213 codec->reg_size = reg_size;
4214 /* it is necessary to make a copy of the default register cache
4215 * because in the case of using a compression type that requires
4216 * the default register cache to be marked as the
4217 * kernel might have freed the array by the time we initialize
4220 if (codec_drv->reg_cache_default) {
4221 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
4222 reg_size, GFP_KERNEL);
4223 if (!codec->reg_def_copy) {
4225 goto fail_codec_name;
4230 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
4231 if (!codec->volatile_register)
4232 codec->volatile_register = snd_soc_default_volatile_register;
4233 if (!codec->readable_register)
4234 codec->readable_register = snd_soc_default_readable_register;
4235 if (!codec->writable_register)
4236 codec->writable_register = snd_soc_default_writable_register;
4239 for (i = 0; i < num_dai; i++) {
4240 fixup_codec_formats(&dai_drv[i].playback);
4241 fixup_codec_formats(&dai_drv[i].capture);
4244 mutex_lock(&client_mutex);
4245 list_add(&codec->list, &codec_list);
4246 mutex_unlock(&client_mutex);
4248 /* register any DAIs */
4249 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
4251 dev_err(codec->dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4252 goto fail_codec_name;
4255 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
4259 mutex_lock(&client_mutex);
4260 list_del(&codec->list);
4261 mutex_unlock(&client_mutex);
4268 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4271 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4273 * @codec: codec to unregister
4275 void snd_soc_unregister_codec(struct device *dev)
4277 struct snd_soc_codec *codec;
4279 list_for_each_entry(codec, &codec_list, list) {
4280 if (dev == codec->dev)
4286 snd_soc_unregister_dais(dev, codec->num_dai);
4288 mutex_lock(&client_mutex);
4289 list_del(&codec->list);
4290 mutex_unlock(&client_mutex);
4292 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
4294 snd_soc_cache_exit(codec);
4295 kfree(codec->reg_def_copy);
4299 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4303 * snd_soc_register_component - Register a component with the ASoC core
4306 int snd_soc_register_component(struct device *dev,
4307 const struct snd_soc_component_driver *cmpnt_drv,
4308 struct snd_soc_dai_driver *dai_drv,
4311 struct snd_soc_component *cmpnt;
4314 dev_dbg(dev, "component register %s\n", dev_name(dev));
4316 cmpnt = devm_kzalloc(dev, sizeof(*cmpnt), GFP_KERNEL);
4318 dev_err(dev, "ASoC: Failed to allocate memory\n");
4322 cmpnt->name = fmt_single_name(dev, &cmpnt->id);
4324 dev_err(dev, "ASoC: Failed to simplifying name\n");
4329 cmpnt->driver = cmpnt_drv;
4330 cmpnt->num_dai = num_dai;
4333 * snd_soc_register_dai() uses fmt_single_name(), and
4334 * snd_soc_register_dais() uses fmt_multiple_name()
4335 * for dai->name which is used for name based matching
4338 ret = snd_soc_register_dai(dev, dai_drv);
4340 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
4342 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4343 goto error_component_name;
4346 mutex_lock(&client_mutex);
4347 list_add(&cmpnt->list, &component_list);
4348 mutex_unlock(&client_mutex);
4350 dev_dbg(cmpnt->dev, "ASoC: Registered component '%s'\n", cmpnt->name);
4354 error_component_name:
4359 EXPORT_SYMBOL_GPL(snd_soc_register_component);
4362 * snd_soc_unregister_component - Unregister a component from the ASoC core
4365 void snd_soc_unregister_component(struct device *dev)
4367 struct snd_soc_component *cmpnt;
4369 list_for_each_entry(cmpnt, &component_list, list) {
4370 if (dev == cmpnt->dev)
4376 snd_soc_unregister_dais(dev, cmpnt->num_dai);
4378 mutex_lock(&client_mutex);
4379 list_del(&cmpnt->list);
4380 mutex_unlock(&client_mutex);
4382 dev_dbg(dev, "ASoC: Unregistered component '%s'\n", cmpnt->name);
4385 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4387 /* Retrieve a card's name from device tree */
4388 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4389 const char *propname)
4391 struct device_node *np = card->dev->of_node;
4394 ret = of_property_read_string_index(np, propname, 0, &card->name);
4396 * EINVAL means the property does not exist. This is fine providing
4397 * card->name was previously set, which is checked later in
4398 * snd_soc_register_card.
4400 if (ret < 0 && ret != -EINVAL) {
4402 "ASoC: Property '%s' could not be read: %d\n",
4409 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4411 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4412 const char *propname)
4414 struct device_node *np = card->dev->of_node;
4416 struct snd_soc_dapm_route *routes;
4419 num_routes = of_property_count_strings(np, propname);
4420 if (num_routes < 0 || num_routes & 1) {
4422 "ASoC: Property '%s' does not exist or its length is not even\n",
4428 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4433 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4437 "ASoC: Could not allocate DAPM route table\n");
4441 for (i = 0; i < num_routes; i++) {
4442 ret = of_property_read_string_index(np, propname,
4443 2 * i, &routes[i].sink);
4446 "ASoC: Property '%s' index %d could not be read: %d\n",
4447 propname, 2 * i, ret);
4450 ret = of_property_read_string_index(np, propname,
4451 (2 * i) + 1, &routes[i].source);
4454 "ASoC: Property '%s' index %d could not be read: %d\n",
4455 propname, (2 * i) + 1, ret);
4460 card->num_dapm_routes = num_routes;
4461 card->dapm_routes = routes;
4465 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4467 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4472 unsigned int format = 0;
4478 } of_fmt_table[] = {
4479 { "i2s", SND_SOC_DAIFMT_I2S },
4480 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4481 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4482 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4483 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4484 { "ac97", SND_SOC_DAIFMT_AC97 },
4485 { "pdm", SND_SOC_DAIFMT_PDM},
4486 { "msb", SND_SOC_DAIFMT_MSB },
4487 { "lsb", SND_SOC_DAIFMT_LSB },
4494 * check "[prefix]format = xxx"
4495 * SND_SOC_DAIFMT_FORMAT_MASK area
4497 snprintf(prop, sizeof(prop), "%sformat", prefix);
4498 ret = of_property_read_string(np, prop, &str);
4500 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4501 if (strcmp(str, of_fmt_table[i].name) == 0) {
4502 format |= of_fmt_table[i].val;
4509 * check "[prefix]continuous-clock"
4510 * SND_SOC_DAIFMT_CLOCK_MASK area
4512 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4513 if (of_get_property(np, prop, NULL))
4514 format |= SND_SOC_DAIFMT_CONT;
4516 format |= SND_SOC_DAIFMT_GATED;
4519 * check "[prefix]bitclock-inversion"
4520 * check "[prefix]frame-inversion"
4521 * SND_SOC_DAIFMT_INV_MASK area
4523 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4524 bit = !!of_get_property(np, prop, NULL);
4526 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4527 frame = !!of_get_property(np, prop, NULL);
4529 switch ((bit << 4) + frame) {
4531 format |= SND_SOC_DAIFMT_IB_IF;
4534 format |= SND_SOC_DAIFMT_IB_NF;
4537 format |= SND_SOC_DAIFMT_NB_IF;
4540 /* SND_SOC_DAIFMT_NB_NF is default */
4545 * check "[prefix]bitclock-master"
4546 * check "[prefix]frame-master"
4547 * SND_SOC_DAIFMT_MASTER_MASK area
4549 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4550 bit = !!of_get_property(np, prop, NULL);
4552 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4553 frame = !!of_get_property(np, prop, NULL);
4555 switch ((bit << 4) + frame) {
4557 format |= SND_SOC_DAIFMT_CBM_CFM;
4560 format |= SND_SOC_DAIFMT_CBM_CFS;
4563 format |= SND_SOC_DAIFMT_CBS_CFM;
4566 format |= SND_SOC_DAIFMT_CBS_CFS;
4572 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4574 static int __init snd_soc_init(void)
4576 #ifdef CONFIG_DEBUG_FS
4577 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4578 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
4579 pr_warn("ASoC: Failed to create debugfs directory\n");
4580 snd_soc_debugfs_root = NULL;
4583 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4585 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4587 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4589 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
4591 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
4592 &platform_list_fops))
4593 pr_warn("ASoC: Failed to create platform list debugfs file\n");
4596 snd_soc_util_init();
4598 return platform_driver_register(&soc_driver);
4600 module_init(snd_soc_init);
4602 static void __exit snd_soc_exit(void)
4604 snd_soc_util_exit();
4606 #ifdef CONFIG_DEBUG_FS
4607 debugfs_remove_recursive(snd_soc_debugfs_root);
4609 platform_driver_unregister(&soc_driver);
4611 module_exit(snd_soc_exit);
4613 /* Module information */
4614 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4615 MODULE_DESCRIPTION("ALSA SoC Core");
4616 MODULE_LICENSE("GPL");
4617 MODULE_ALIAS("platform:soc-audio");