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/ctype.h>
34 #include <linux/slab.h>
36 #include <sound/ac97_codec.h>
37 #include <sound/core.h>
38 #include <sound/jack.h>
39 #include <sound/pcm.h>
40 #include <sound/pcm_params.h>
41 #include <sound/soc.h>
42 #include <sound/soc-dpcm.h>
43 #include <sound/initval.h>
45 #define CREATE_TRACE_POINTS
46 #include <trace/events/asoc.h>
50 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
52 #ifdef CONFIG_DEBUG_FS
53 struct dentry *snd_soc_debugfs_root;
54 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
57 static DEFINE_MUTEX(client_mutex);
58 static LIST_HEAD(dai_list);
59 static LIST_HEAD(platform_list);
60 static LIST_HEAD(codec_list);
61 static LIST_HEAD(component_list);
64 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
68 static int pmdown_time = 5000;
69 module_param(pmdown_time, int, 0);
70 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
72 /* returns the minimum number of bytes needed to represent
73 * a particular given value */
74 static int min_bytes_needed(unsigned long val)
79 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
82 c = (sizeof val * 8) - c;
90 /* fill buf which is 'len' bytes with a formatted
91 * string of the form 'reg: value\n' */
92 static int format_register_str(struct snd_soc_codec *codec,
93 unsigned int reg, char *buf, size_t len)
95 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
96 int regsize = codec->driver->reg_word_size * 2;
99 char regbuf[regsize + 1];
101 /* since tmpbuf is allocated on the stack, warn the callers if they
102 * try to abuse this function */
105 /* +2 for ': ' and + 1 for '\n' */
106 if (wordsize + regsize + 2 + 1 != len)
109 ret = snd_soc_read(codec, reg);
111 memset(regbuf, 'X', regsize);
112 regbuf[regsize] = '\0';
114 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
117 /* prepare the buffer */
118 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
119 /* copy it back to the caller without the '\0' */
120 memcpy(buf, tmpbuf, len);
125 /* codec register dump */
126 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
127 size_t count, loff_t pos)
130 int wordsize, regsize;
135 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
136 regsize = codec->driver->reg_word_size * 2;
138 len = wordsize + regsize + 2 + 1;
140 if (!codec->driver->reg_cache_size)
143 if (codec->driver->reg_cache_step)
144 step = codec->driver->reg_cache_step;
146 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
147 if (!snd_soc_codec_readable_register(codec, i))
149 if (codec->driver->display_register) {
150 count += codec->driver->display_register(codec, buf + count,
151 PAGE_SIZE - count, i);
153 /* only support larger than PAGE_SIZE bytes debugfs
154 * entries for the default case */
156 if (total + len >= count - 1)
158 format_register_str(codec, i, buf + total, len);
165 total = min(total, count - 1);
170 static ssize_t codec_reg_show(struct device *dev,
171 struct device_attribute *attr, char *buf)
173 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
175 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
178 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
180 static ssize_t pmdown_time_show(struct device *dev,
181 struct device_attribute *attr, char *buf)
183 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
185 return sprintf(buf, "%ld\n", rtd->pmdown_time);
188 static ssize_t pmdown_time_set(struct device *dev,
189 struct device_attribute *attr,
190 const char *buf, size_t count)
192 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
195 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
202 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
204 #ifdef CONFIG_DEBUG_FS
205 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
206 size_t count, loff_t *ppos)
209 struct snd_soc_codec *codec = file->private_data;
212 if (*ppos < 0 || !count)
215 buf = kmalloc(count, GFP_KERNEL);
219 ret = soc_codec_reg_show(codec, buf, count, *ppos);
221 if (copy_to_user(user_buf, buf, ret)) {
232 static ssize_t codec_reg_write_file(struct file *file,
233 const char __user *user_buf, size_t count, loff_t *ppos)
238 unsigned long reg, value;
239 struct snd_soc_codec *codec = file->private_data;
241 buf_size = min(count, (sizeof(buf)-1));
242 if (copy_from_user(buf, user_buf, buf_size))
246 while (*start == ' ')
248 reg = simple_strtoul(start, &start, 16);
249 while (*start == ' ')
251 if (strict_strtoul(start, 16, &value))
254 /* Userspace has been fiddling around behind the kernel's back */
255 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
257 snd_soc_write(codec, reg, value);
261 static const struct file_operations codec_reg_fops = {
263 .read = codec_reg_read_file,
264 .write = codec_reg_write_file,
265 .llseek = default_llseek,
268 static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
270 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
272 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
274 if (!codec->debugfs_codec_root) {
275 dev_warn(codec->dev, "ASoC: Failed to create codec debugfs"
280 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
282 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
285 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
286 codec->debugfs_codec_root,
287 codec, &codec_reg_fops);
288 if (!codec->debugfs_reg)
289 dev_warn(codec->dev, "ASoC: Failed to create codec register"
292 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
295 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
297 debugfs_remove_recursive(codec->debugfs_codec_root);
300 static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
302 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
304 platform->debugfs_platform_root = debugfs_create_dir(platform->name,
306 if (!platform->debugfs_platform_root) {
307 dev_warn(platform->dev,
308 "ASoC: Failed to create platform debugfs directory\n");
312 snd_soc_dapm_debugfs_init(&platform->dapm,
313 platform->debugfs_platform_root);
316 static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
318 debugfs_remove_recursive(platform->debugfs_platform_root);
321 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
322 size_t count, loff_t *ppos)
324 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
325 ssize_t len, ret = 0;
326 struct snd_soc_codec *codec;
331 list_for_each_entry(codec, &codec_list, list) {
332 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
336 if (ret > PAGE_SIZE) {
343 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
350 static const struct file_operations codec_list_fops = {
351 .read = codec_list_read_file,
352 .llseek = default_llseek,/* read accesses f_pos */
355 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
356 size_t count, loff_t *ppos)
358 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
359 ssize_t len, ret = 0;
360 struct snd_soc_dai *dai;
365 list_for_each_entry(dai, &dai_list, list) {
366 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
369 if (ret > PAGE_SIZE) {
375 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
382 static const struct file_operations dai_list_fops = {
383 .read = dai_list_read_file,
384 .llseek = default_llseek,/* read accesses f_pos */
387 static ssize_t platform_list_read_file(struct file *file,
388 char __user *user_buf,
389 size_t count, loff_t *ppos)
391 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
392 ssize_t len, ret = 0;
393 struct snd_soc_platform *platform;
398 list_for_each_entry(platform, &platform_list, list) {
399 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
403 if (ret > PAGE_SIZE) {
409 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
416 static const struct file_operations platform_list_fops = {
417 .read = platform_list_read_file,
418 .llseek = default_llseek,/* read accesses f_pos */
421 static void soc_init_card_debugfs(struct snd_soc_card *card)
423 card->debugfs_card_root = debugfs_create_dir(card->name,
424 snd_soc_debugfs_root);
425 if (!card->debugfs_card_root) {
427 "ASoC: Failed to create card debugfs directory\n");
431 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
432 card->debugfs_card_root,
434 if (!card->debugfs_pop_time)
436 "ASoC: Failed to create pop time debugfs file\n");
439 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
441 debugfs_remove_recursive(card->debugfs_card_root);
446 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
450 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
454 static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
458 static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
462 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
466 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
471 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
472 const char *dai_link, int stream)
476 for (i = 0; i < card->num_links; i++) {
477 if (card->rtd[i].dai_link->no_pcm &&
478 !strcmp(card->rtd[i].dai_link->name, dai_link))
479 return card->rtd[i].pcm->streams[stream].substream;
481 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
484 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
486 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
487 const char *dai_link)
491 for (i = 0; i < card->num_links; i++) {
492 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
493 return &card->rtd[i];
495 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
498 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
500 #ifdef CONFIG_SND_SOC_AC97_BUS
501 /* unregister ac97 codec */
502 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
504 if (codec->ac97->dev.bus)
505 device_unregister(&codec->ac97->dev);
509 /* stop no dev release warning */
510 static void soc_ac97_device_release(struct device *dev){}
512 /* register ac97 codec to bus */
513 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
517 codec->ac97->dev.bus = &ac97_bus_type;
518 codec->ac97->dev.parent = codec->card->dev;
519 codec->ac97->dev.release = soc_ac97_device_release;
521 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
522 codec->card->snd_card->number, 0, codec->name);
523 err = device_register(&codec->ac97->dev);
525 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
526 codec->ac97->dev.bus = NULL;
533 #ifdef CONFIG_PM_SLEEP
534 /* powers down audio subsystem for suspend */
535 int snd_soc_suspend(struct device *dev)
537 struct snd_soc_card *card = dev_get_drvdata(dev);
538 struct snd_soc_codec *codec;
541 /* If the initialization of this soc device failed, there is no codec
542 * associated with it. Just bail out in this case.
544 if (list_empty(&card->codec_dev_list))
547 /* Due to the resume being scheduled into a workqueue we could
548 * suspend before that's finished - wait for it to complete.
550 snd_power_lock(card->snd_card);
551 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
552 snd_power_unlock(card->snd_card);
554 /* we're going to block userspace touching us until resume completes */
555 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
557 /* mute any active DACs */
558 for (i = 0; i < card->num_rtd; i++) {
559 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
560 struct snd_soc_dai_driver *drv = dai->driver;
562 if (card->rtd[i].dai_link->ignore_suspend)
565 if (drv->ops->digital_mute && dai->playback_active)
566 drv->ops->digital_mute(dai, 1);
569 /* suspend all pcms */
570 for (i = 0; i < card->num_rtd; i++) {
571 if (card->rtd[i].dai_link->ignore_suspend)
574 snd_pcm_suspend_all(card->rtd[i].pcm);
577 if (card->suspend_pre)
578 card->suspend_pre(card);
580 for (i = 0; i < card->num_rtd; i++) {
581 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
582 struct snd_soc_platform *platform = card->rtd[i].platform;
584 if (card->rtd[i].dai_link->ignore_suspend)
587 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
588 cpu_dai->driver->suspend(cpu_dai);
589 if (platform->driver->suspend && !platform->suspended) {
590 platform->driver->suspend(cpu_dai);
591 platform->suspended = 1;
595 /* close any waiting streams and save state */
596 for (i = 0; i < card->num_rtd; i++) {
597 flush_delayed_work(&card->rtd[i].delayed_work);
598 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
601 for (i = 0; i < card->num_rtd; i++) {
603 if (card->rtd[i].dai_link->ignore_suspend)
606 snd_soc_dapm_stream_event(&card->rtd[i],
607 SNDRV_PCM_STREAM_PLAYBACK,
608 SND_SOC_DAPM_STREAM_SUSPEND);
610 snd_soc_dapm_stream_event(&card->rtd[i],
611 SNDRV_PCM_STREAM_CAPTURE,
612 SND_SOC_DAPM_STREAM_SUSPEND);
615 /* Recheck all analogue paths too */
616 dapm_mark_io_dirty(&card->dapm);
617 snd_soc_dapm_sync(&card->dapm);
619 /* suspend all CODECs */
620 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
621 /* If there are paths active then the CODEC will be held with
622 * bias _ON and should not be suspended. */
623 if (!codec->suspended && codec->driver->suspend) {
624 switch (codec->dapm.bias_level) {
625 case SND_SOC_BIAS_STANDBY:
627 * If the CODEC is capable of idle
628 * bias off then being in STANDBY
629 * means it's doing something,
630 * otherwise fall through.
632 if (codec->dapm.idle_bias_off) {
634 "ASoC: idle_bias_off CODEC on"
638 case SND_SOC_BIAS_OFF:
639 codec->driver->suspend(codec);
640 codec->suspended = 1;
641 codec->cache_sync = 1;
642 if (codec->using_regmap)
643 regcache_mark_dirty(codec->control_data);
646 dev_dbg(codec->dev, "ASoC: CODEC is on"
653 for (i = 0; i < card->num_rtd; i++) {
654 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
656 if (card->rtd[i].dai_link->ignore_suspend)
659 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
660 cpu_dai->driver->suspend(cpu_dai);
663 if (card->suspend_post)
664 card->suspend_post(card);
668 EXPORT_SYMBOL_GPL(snd_soc_suspend);
670 /* deferred resume work, so resume can complete before we finished
671 * setting our codec back up, which can be very slow on I2C
673 static void soc_resume_deferred(struct work_struct *work)
675 struct snd_soc_card *card =
676 container_of(work, struct snd_soc_card, deferred_resume_work);
677 struct snd_soc_codec *codec;
680 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
681 * so userspace apps are blocked from touching us
684 dev_dbg(card->dev, "ASoC: starting resume work\n");
686 /* Bring us up into D2 so that DAPM starts enabling things */
687 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
689 if (card->resume_pre)
690 card->resume_pre(card);
692 /* resume AC97 DAIs */
693 for (i = 0; i < card->num_rtd; i++) {
694 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
696 if (card->rtd[i].dai_link->ignore_suspend)
699 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
700 cpu_dai->driver->resume(cpu_dai);
703 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
704 /* If the CODEC was idle over suspend then it will have been
705 * left with bias OFF or STANDBY and suspended so we must now
706 * resume. Otherwise the suspend was suppressed.
708 if (codec->driver->resume && codec->suspended) {
709 switch (codec->dapm.bias_level) {
710 case SND_SOC_BIAS_STANDBY:
711 case SND_SOC_BIAS_OFF:
712 codec->driver->resume(codec);
713 codec->suspended = 0;
716 dev_dbg(codec->dev, "ASoC: CODEC was on over"
723 for (i = 0; i < card->num_rtd; i++) {
725 if (card->rtd[i].dai_link->ignore_suspend)
728 snd_soc_dapm_stream_event(&card->rtd[i],
729 SNDRV_PCM_STREAM_PLAYBACK,
730 SND_SOC_DAPM_STREAM_RESUME);
732 snd_soc_dapm_stream_event(&card->rtd[i],
733 SNDRV_PCM_STREAM_CAPTURE,
734 SND_SOC_DAPM_STREAM_RESUME);
737 /* unmute any active DACs */
738 for (i = 0; i < card->num_rtd; i++) {
739 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
740 struct snd_soc_dai_driver *drv = dai->driver;
742 if (card->rtd[i].dai_link->ignore_suspend)
745 if (drv->ops->digital_mute && dai->playback_active)
746 drv->ops->digital_mute(dai, 0);
749 for (i = 0; i < card->num_rtd; i++) {
750 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
751 struct snd_soc_platform *platform = card->rtd[i].platform;
753 if (card->rtd[i].dai_link->ignore_suspend)
756 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
757 cpu_dai->driver->resume(cpu_dai);
758 if (platform->driver->resume && platform->suspended) {
759 platform->driver->resume(cpu_dai);
760 platform->suspended = 0;
764 if (card->resume_post)
765 card->resume_post(card);
767 dev_dbg(card->dev, "ASoC: resume work completed\n");
769 /* userspace can access us now we are back as we were before */
770 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
772 /* Recheck all analogue paths too */
773 dapm_mark_io_dirty(&card->dapm);
774 snd_soc_dapm_sync(&card->dapm);
777 /* powers up audio subsystem after a suspend */
778 int snd_soc_resume(struct device *dev)
780 struct snd_soc_card *card = dev_get_drvdata(dev);
781 int i, ac97_control = 0;
783 /* If the initialization of this soc device failed, there is no codec
784 * associated with it. Just bail out in this case.
786 if (list_empty(&card->codec_dev_list))
789 /* AC97 devices might have other drivers hanging off them so
790 * need to resume immediately. Other drivers don't have that
791 * problem and may take a substantial amount of time to resume
792 * due to I/O costs and anti-pop so handle them out of line.
794 for (i = 0; i < card->num_rtd; i++) {
795 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
796 ac97_control |= cpu_dai->driver->ac97_control;
799 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
800 soc_resume_deferred(&card->deferred_resume_work);
802 dev_dbg(dev, "ASoC: Scheduling resume work\n");
803 if (!schedule_work(&card->deferred_resume_work))
804 dev_err(dev, "ASoC: resume work item may be lost\n");
809 EXPORT_SYMBOL_GPL(snd_soc_resume);
811 #define snd_soc_suspend NULL
812 #define snd_soc_resume NULL
815 static const struct snd_soc_dai_ops null_dai_ops = {
818 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
820 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
821 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
822 struct snd_soc_codec *codec;
823 struct snd_soc_platform *platform;
824 struct snd_soc_dai *codec_dai, *cpu_dai;
825 const char *platform_name;
827 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
829 /* Find CPU DAI from registered DAIs*/
830 list_for_each_entry(cpu_dai, &dai_list, list) {
831 if (dai_link->cpu_of_node &&
832 (cpu_dai->dev->of_node != dai_link->cpu_of_node))
834 if (dai_link->cpu_name &&
835 strcmp(dev_name(cpu_dai->dev), dai_link->cpu_name))
837 if (dai_link->cpu_dai_name &&
838 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
841 rtd->cpu_dai = cpu_dai;
845 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
846 dai_link->cpu_dai_name);
847 return -EPROBE_DEFER;
850 /* Find CODEC from registered CODECs */
851 list_for_each_entry(codec, &codec_list, list) {
852 if (dai_link->codec_of_node) {
853 if (codec->dev->of_node != dai_link->codec_of_node)
856 if (strcmp(codec->name, dai_link->codec_name))
863 * CODEC found, so find CODEC DAI from registered DAIs from
866 list_for_each_entry(codec_dai, &dai_list, list) {
867 if (codec->dev == codec_dai->dev &&
868 !strcmp(codec_dai->name,
869 dai_link->codec_dai_name)) {
871 rtd->codec_dai = codec_dai;
875 if (!rtd->codec_dai) {
876 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
877 dai_link->codec_dai_name);
878 return -EPROBE_DEFER;
883 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
884 dai_link->codec_name);
885 return -EPROBE_DEFER;
888 /* if there's no platform we match on the empty platform */
889 platform_name = dai_link->platform_name;
890 if (!platform_name && !dai_link->platform_of_node)
891 platform_name = "snd-soc-dummy";
893 /* find one from the set of registered platforms */
894 list_for_each_entry(platform, &platform_list, list) {
895 if (dai_link->platform_of_node) {
896 if (platform->dev->of_node !=
897 dai_link->platform_of_node)
900 if (strcmp(platform->name, platform_name))
904 rtd->platform = platform;
906 if (!rtd->platform) {
907 dev_err(card->dev, "ASoC: platform %s not registered\n",
908 dai_link->platform_name);
909 return -EPROBE_DEFER;
917 static int soc_remove_platform(struct snd_soc_platform *platform)
921 if (platform->driver->remove) {
922 ret = platform->driver->remove(platform);
924 dev_err(platform->dev, "ASoC: failed to remove %d\n",
928 /* Make sure all DAPM widgets are freed */
929 snd_soc_dapm_free(&platform->dapm);
931 soc_cleanup_platform_debugfs(platform);
932 platform->probed = 0;
933 list_del(&platform->card_list);
934 module_put(platform->dev->driver->owner);
939 static void soc_remove_codec(struct snd_soc_codec *codec)
943 if (codec->driver->remove) {
944 err = codec->driver->remove(codec);
946 dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
949 /* Make sure all DAPM widgets are freed */
950 snd_soc_dapm_free(&codec->dapm);
952 soc_cleanup_codec_debugfs(codec);
954 list_del(&codec->card_list);
955 module_put(codec->dev->driver->owner);
958 static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
960 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
961 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
964 /* unregister the rtd device */
965 if (rtd->dev_registered) {
966 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
967 device_remove_file(rtd->dev, &dev_attr_codec_reg);
968 device_unregister(rtd->dev);
969 rtd->dev_registered = 0;
972 /* remove the CODEC DAI */
973 if (codec_dai && codec_dai->probed &&
974 codec_dai->driver->remove_order == order) {
975 if (codec_dai->driver->remove) {
976 err = codec_dai->driver->remove(codec_dai);
978 dev_err(codec_dai->dev,
979 "ASoC: failed to remove %s: %d\n",
980 codec_dai->name, err);
982 codec_dai->probed = 0;
983 list_del(&codec_dai->card_list);
986 /* remove the cpu_dai */
987 if (cpu_dai && cpu_dai->probed &&
988 cpu_dai->driver->remove_order == order) {
989 if (cpu_dai->driver->remove) {
990 err = cpu_dai->driver->remove(cpu_dai);
992 dev_err(cpu_dai->dev,
993 "ASoC: failed to remove %s: %d\n",
997 list_del(&cpu_dai->card_list);
999 if (!cpu_dai->codec) {
1000 snd_soc_dapm_free(&cpu_dai->dapm);
1001 module_put(cpu_dai->dev->driver->owner);
1006 static void soc_remove_link_components(struct snd_soc_card *card, int num,
1009 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1010 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1011 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1012 struct snd_soc_platform *platform = rtd->platform;
1013 struct snd_soc_codec *codec;
1015 /* remove the platform */
1016 if (platform && platform->probed &&
1017 platform->driver->remove_order == order) {
1018 soc_remove_platform(platform);
1021 /* remove the CODEC-side CODEC */
1023 codec = codec_dai->codec;
1024 if (codec && codec->probed &&
1025 codec->driver->remove_order == order)
1026 soc_remove_codec(codec);
1029 /* remove any CPU-side CODEC */
1031 codec = cpu_dai->codec;
1032 if (codec && codec->probed &&
1033 codec->driver->remove_order == order)
1034 soc_remove_codec(codec);
1038 static void soc_remove_dai_links(struct snd_soc_card *card)
1042 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1044 for (dai = 0; dai < card->num_rtd; dai++)
1045 soc_remove_link_dais(card, dai, order);
1048 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1050 for (dai = 0; dai < card->num_rtd; dai++)
1051 soc_remove_link_components(card, dai, order);
1057 static void soc_set_name_prefix(struct snd_soc_card *card,
1058 struct snd_soc_codec *codec)
1062 if (card->codec_conf == NULL)
1065 for (i = 0; i < card->num_configs; i++) {
1066 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1067 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1068 codec->name_prefix = map->name_prefix;
1074 static int soc_probe_codec(struct snd_soc_card *card,
1075 struct snd_soc_codec *codec)
1078 const struct snd_soc_codec_driver *driver = codec->driver;
1079 struct snd_soc_dai *dai;
1082 codec->dapm.card = card;
1083 soc_set_name_prefix(card, codec);
1085 if (!try_module_get(codec->dev->driver->owner))
1088 soc_init_codec_debugfs(codec);
1090 if (driver->dapm_widgets)
1091 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1092 driver->num_dapm_widgets);
1094 /* Create DAPM widgets for each DAI stream */
1095 list_for_each_entry(dai, &dai_list, list) {
1096 if (dai->dev != codec->dev)
1099 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1102 codec->dapm.idle_bias_off = driver->idle_bias_off;
1104 if (driver->probe) {
1105 ret = driver->probe(codec);
1108 "ASoC: failed to probe CODEC %d\n", ret);
1111 WARN(codec->dapm.idle_bias_off &&
1112 codec->dapm.bias_level != SND_SOC_BIAS_OFF,
1113 "codec %s can not start from non-off bias"
1114 " with idle_bias_off==1\n", codec->name);
1117 /* If the driver didn't set I/O up try regmap */
1118 if (!codec->write && dev_get_regmap(codec->dev, NULL))
1119 snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
1121 if (driver->controls)
1122 snd_soc_add_codec_controls(codec, driver->controls,
1123 driver->num_controls);
1124 if (driver->dapm_routes)
1125 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1126 driver->num_dapm_routes);
1128 /* mark codec as probed and add to card codec list */
1130 list_add(&codec->card_list, &card->codec_dev_list);
1131 list_add(&codec->dapm.list, &card->dapm_list);
1136 soc_cleanup_codec_debugfs(codec);
1137 module_put(codec->dev->driver->owner);
1142 static int soc_probe_platform(struct snd_soc_card *card,
1143 struct snd_soc_platform *platform)
1146 const struct snd_soc_platform_driver *driver = platform->driver;
1147 struct snd_soc_dai *dai;
1149 platform->card = card;
1150 platform->dapm.card = card;
1152 if (!try_module_get(platform->dev->driver->owner))
1155 soc_init_platform_debugfs(platform);
1157 if (driver->dapm_widgets)
1158 snd_soc_dapm_new_controls(&platform->dapm,
1159 driver->dapm_widgets, driver->num_dapm_widgets);
1161 /* Create DAPM widgets for each DAI stream */
1162 list_for_each_entry(dai, &dai_list, list) {
1163 if (dai->dev != platform->dev)
1166 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1169 platform->dapm.idle_bias_off = 1;
1171 if (driver->probe) {
1172 ret = driver->probe(platform);
1174 dev_err(platform->dev,
1175 "ASoC: failed to probe platform %d\n", ret);
1180 if (driver->controls)
1181 snd_soc_add_platform_controls(platform, driver->controls,
1182 driver->num_controls);
1183 if (driver->dapm_routes)
1184 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1185 driver->num_dapm_routes);
1187 /* mark platform as probed and add to card platform list */
1188 platform->probed = 1;
1189 list_add(&platform->card_list, &card->platform_dev_list);
1190 list_add(&platform->dapm.list, &card->dapm_list);
1195 soc_cleanup_platform_debugfs(platform);
1196 module_put(platform->dev->driver->owner);
1201 static void rtd_release(struct device *dev)
1206 static int soc_post_component_init(struct snd_soc_card *card,
1207 struct snd_soc_codec *codec,
1208 int num, int dailess)
1210 struct snd_soc_dai_link *dai_link = NULL;
1211 struct snd_soc_aux_dev *aux_dev = NULL;
1212 struct snd_soc_pcm_runtime *rtd;
1213 const char *temp, *name;
1217 dai_link = &card->dai_link[num];
1218 rtd = &card->rtd[num];
1219 name = dai_link->name;
1221 aux_dev = &card->aux_dev[num];
1222 rtd = &card->rtd_aux[num];
1223 name = aux_dev->name;
1227 /* Make sure all DAPM widgets are instantiated */
1228 snd_soc_dapm_new_widgets(&codec->dapm);
1230 /* machine controls, routes and widgets are not prefixed */
1231 temp = codec->name_prefix;
1232 codec->name_prefix = NULL;
1234 /* do machine specific initialization */
1235 if (!dailess && dai_link->init)
1236 ret = dai_link->init(rtd);
1237 else if (dailess && aux_dev->init)
1238 ret = aux_dev->init(&codec->dapm);
1240 dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
1243 codec->name_prefix = temp;
1245 /* register the rtd device */
1248 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1251 device_initialize(rtd->dev);
1252 rtd->dev->parent = card->dev;
1253 rtd->dev->release = rtd_release;
1254 rtd->dev->init_name = name;
1255 dev_set_drvdata(rtd->dev, rtd);
1256 mutex_init(&rtd->pcm_mutex);
1257 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1258 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1259 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1260 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1261 ret = device_add(rtd->dev);
1263 /* calling put_device() here to free the rtd->dev */
1264 put_device(rtd->dev);
1266 "ASoC: failed to register runtime device: %d\n", ret);
1269 rtd->dev_registered = 1;
1271 /* add DAPM sysfs entries for this codec */
1272 ret = snd_soc_dapm_sys_add(rtd->dev);
1275 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
1277 /* add codec sysfs entries */
1278 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1281 "ASoC: failed to add codec sysfs files: %d\n", ret);
1283 #ifdef CONFIG_DEBUG_FS
1284 /* add DPCM sysfs entries */
1285 if (!dailess && !dai_link->dynamic)
1288 ret = soc_dpcm_debugfs_add(rtd);
1290 dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
1297 static int soc_probe_link_components(struct snd_soc_card *card, int num,
1300 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1301 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1302 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1303 struct snd_soc_platform *platform = rtd->platform;
1306 /* probe the CPU-side component, if it is a CODEC */
1307 if (cpu_dai->codec &&
1308 !cpu_dai->codec->probed &&
1309 cpu_dai->codec->driver->probe_order == order) {
1310 ret = soc_probe_codec(card, cpu_dai->codec);
1315 /* probe the CODEC-side component */
1316 if (!codec_dai->codec->probed &&
1317 codec_dai->codec->driver->probe_order == order) {
1318 ret = soc_probe_codec(card, codec_dai->codec);
1323 /* probe the platform */
1324 if (!platform->probed &&
1325 platform->driver->probe_order == order) {
1326 ret = soc_probe_platform(card, platform);
1334 static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1336 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1337 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1338 struct snd_soc_codec *codec = rtd->codec;
1339 struct snd_soc_platform *platform = rtd->platform;
1340 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1341 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1342 struct snd_soc_dapm_widget *play_w, *capture_w;
1345 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1346 card->name, num, order);
1348 /* config components */
1349 cpu_dai->platform = platform;
1350 codec_dai->card = card;
1351 cpu_dai->card = card;
1353 /* set default power off timeout */
1354 rtd->pmdown_time = pmdown_time;
1356 /* probe the cpu_dai */
1357 if (!cpu_dai->probed &&
1358 cpu_dai->driver->probe_order == order) {
1359 if (!cpu_dai->codec) {
1360 cpu_dai->dapm.card = card;
1361 if (!try_module_get(cpu_dai->dev->driver->owner))
1364 list_add(&cpu_dai->dapm.list, &card->dapm_list);
1365 snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1368 if (cpu_dai->driver->probe) {
1369 ret = cpu_dai->driver->probe(cpu_dai);
1371 dev_err(cpu_dai->dev,
1372 "ASoC: failed to probe CPU DAI %s: %d\n",
1373 cpu_dai->name, ret);
1374 module_put(cpu_dai->dev->driver->owner);
1378 cpu_dai->probed = 1;
1379 /* mark cpu_dai as probed and add to card dai list */
1380 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1383 /* probe the CODEC DAI */
1384 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1385 if (codec_dai->driver->probe) {
1386 ret = codec_dai->driver->probe(codec_dai);
1388 dev_err(codec_dai->dev,
1389 "ASoC: failed to probe CODEC DAI %s: %d\n",
1390 codec_dai->name, ret);
1395 /* mark codec_dai as probed and add to card dai list */
1396 codec_dai->probed = 1;
1397 list_add(&codec_dai->card_list, &card->dai_dev_list);
1400 /* complete DAI probe during last probe */
1401 if (order != SND_SOC_COMP_ORDER_LAST)
1404 ret = soc_post_component_init(card, codec, num, 0);
1408 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1410 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1413 if (cpu_dai->driver->compress_dai) {
1414 /*create compress_device"*/
1415 ret = soc_new_compress(rtd, num);
1417 dev_err(card->dev, "ASoC: can't create compress %s\n",
1418 dai_link->stream_name);
1423 if (!dai_link->params) {
1424 /* create the pcm */
1425 ret = soc_new_pcm(rtd, num);
1427 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1428 dai_link->stream_name, ret);
1432 /* link the DAI widgets */
1433 play_w = codec_dai->playback_widget;
1434 capture_w = cpu_dai->capture_widget;
1435 if (play_w && capture_w) {
1436 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1439 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1440 play_w->name, capture_w->name, ret);
1445 play_w = cpu_dai->playback_widget;
1446 capture_w = codec_dai->capture_widget;
1447 if (play_w && capture_w) {
1448 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1451 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1452 play_w->name, capture_w->name, ret);
1459 /* add platform data for AC97 devices */
1460 if (rtd->codec_dai->driver->ac97_control)
1461 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1466 #ifdef CONFIG_SND_SOC_AC97_BUS
1467 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1471 /* Only instantiate AC97 if not already done by the adaptor
1472 * for the generic AC97 subsystem.
1474 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
1476 * It is possible that the AC97 device is already registered to
1477 * the device subsystem. This happens when the device is created
1478 * via snd_ac97_mixer(). Currently only SoC codec that does so
1479 * is the generic AC97 glue but others migh emerge.
1481 * In those cases we don't try to register the device again.
1483 if (!rtd->codec->ac97_created)
1486 ret = soc_ac97_dev_register(rtd->codec);
1488 dev_err(rtd->codec->dev,
1489 "ASoC: AC97 device register failed: %d\n", ret);
1493 rtd->codec->ac97_registered = 1;
1498 static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1500 if (codec->ac97_registered) {
1501 soc_ac97_dev_unregister(codec);
1502 codec->ac97_registered = 0;
1507 static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1509 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1510 struct snd_soc_codec *codec;
1512 /* find CODEC from registered CODECs*/
1513 list_for_each_entry(codec, &codec_list, list) {
1514 if (!strcmp(codec->name, aux_dev->codec_name))
1518 dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
1520 return -EPROBE_DEFER;
1523 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1525 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1526 struct snd_soc_codec *codec;
1529 /* find CODEC from registered CODECs*/
1530 list_for_each_entry(codec, &codec_list, list) {
1531 if (!strcmp(codec->name, aux_dev->codec_name)) {
1532 if (codec->probed) {
1534 "ASoC: codec already probed");
1541 /* codec not found */
1542 dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
1543 return -EPROBE_DEFER;
1546 ret = soc_probe_codec(card, codec);
1550 ret = soc_post_component_init(card, codec, num, 1);
1556 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1558 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1559 struct snd_soc_codec *codec = rtd->codec;
1561 /* unregister the rtd device */
1562 if (rtd->dev_registered) {
1563 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1564 device_unregister(rtd->dev);
1565 rtd->dev_registered = 0;
1568 if (codec && codec->probed)
1569 soc_remove_codec(codec);
1572 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1573 enum snd_soc_compress_type compress_type)
1577 if (codec->cache_init)
1580 /* override the compress_type if necessary */
1581 if (compress_type && codec->compress_type != compress_type)
1582 codec->compress_type = compress_type;
1583 ret = snd_soc_cache_init(codec);
1585 dev_err(codec->dev, "ASoC: Failed to set cache compression"
1586 " type: %d\n", ret);
1589 codec->cache_init = 1;
1593 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1595 struct snd_soc_codec *codec;
1596 struct snd_soc_codec_conf *codec_conf;
1597 enum snd_soc_compress_type compress_type;
1598 struct snd_soc_dai_link *dai_link;
1599 int ret, i, order, dai_fmt;
1601 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1604 for (i = 0; i < card->num_links; i++) {
1605 ret = soc_bind_dai_link(card, i);
1610 /* check aux_devs too */
1611 for (i = 0; i < card->num_aux_devs; i++) {
1612 ret = soc_check_aux_dev(card, i);
1617 /* initialize the register cache for each available codec */
1618 list_for_each_entry(codec, &codec_list, list) {
1619 if (codec->cache_init)
1621 /* by default we don't override the compress_type */
1623 /* check to see if we need to override the compress_type */
1624 for (i = 0; i < card->num_configs; ++i) {
1625 codec_conf = &card->codec_conf[i];
1626 if (!strcmp(codec->name, codec_conf->dev_name)) {
1627 compress_type = codec_conf->compress_type;
1628 if (compress_type && compress_type
1629 != codec->compress_type)
1633 ret = snd_soc_init_codec_cache(codec, compress_type);
1638 /* card bind complete so register a sound card */
1639 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1640 card->owner, 0, &card->snd_card);
1642 dev_err(card->dev, "ASoC: can't create sound card for"
1643 " card %s: %d\n", card->name, ret);
1646 card->snd_card->dev = card->dev;
1648 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1649 card->dapm.dev = card->dev;
1650 card->dapm.card = card;
1651 list_add(&card->dapm.list, &card->dapm_list);
1653 #ifdef CONFIG_DEBUG_FS
1654 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1657 #ifdef CONFIG_PM_SLEEP
1658 /* deferred resume work */
1659 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1662 if (card->dapm_widgets)
1663 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1664 card->num_dapm_widgets);
1666 /* initialise the sound card only once */
1668 ret = card->probe(card);
1670 goto card_probe_error;
1673 /* probe all components used by DAI links on this card */
1674 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1676 for (i = 0; i < card->num_links; i++) {
1677 ret = soc_probe_link_components(card, i, order);
1680 "ASoC: failed to instantiate card %d\n",
1687 /* probe all DAI links on this card */
1688 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1690 for (i = 0; i < card->num_links; i++) {
1691 ret = soc_probe_link_dais(card, i, order);
1694 "ASoC: failed to instantiate card %d\n",
1701 for (i = 0; i < card->num_aux_devs; i++) {
1702 ret = soc_probe_aux_dev(card, i);
1705 "ASoC: failed to add auxiliary devices %d\n",
1707 goto probe_aux_dev_err;
1711 snd_soc_dapm_link_dai_widgets(card);
1714 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1716 if (card->dapm_routes)
1717 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1718 card->num_dapm_routes);
1720 snd_soc_dapm_new_widgets(&card->dapm);
1722 for (i = 0; i < card->num_links; i++) {
1723 dai_link = &card->dai_link[i];
1724 dai_fmt = dai_link->dai_fmt;
1727 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1729 if (ret != 0 && ret != -ENOTSUPP)
1730 dev_warn(card->rtd[i].codec_dai->dev,
1731 "ASoC: Failed to set DAI format: %d\n",
1735 /* If this is a regular CPU link there will be a platform */
1737 (dai_link->platform_name || dai_link->platform_of_node)) {
1738 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1740 if (ret != 0 && ret != -ENOTSUPP)
1741 dev_warn(card->rtd[i].cpu_dai->dev,
1742 "ASoC: Failed to set DAI format: %d\n",
1744 } else if (dai_fmt) {
1745 /* Flip the polarity for the "CPU" end */
1746 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1747 switch (dai_link->dai_fmt &
1748 SND_SOC_DAIFMT_MASTER_MASK) {
1749 case SND_SOC_DAIFMT_CBM_CFM:
1750 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1752 case SND_SOC_DAIFMT_CBM_CFS:
1753 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1755 case SND_SOC_DAIFMT_CBS_CFM:
1756 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1758 case SND_SOC_DAIFMT_CBS_CFS:
1759 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1763 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1765 if (ret != 0 && ret != -ENOTSUPP)
1766 dev_warn(card->rtd[i].cpu_dai->dev,
1767 "ASoC: Failed to set DAI format: %d\n",
1772 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1774 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1775 "%s", card->long_name ? card->long_name : card->name);
1776 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1777 "%s", card->driver_name ? card->driver_name : card->name);
1778 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1779 switch (card->snd_card->driver[i]) {
1785 if (!isalnum(card->snd_card->driver[i]))
1786 card->snd_card->driver[i] = '_';
1791 if (card->late_probe) {
1792 ret = card->late_probe(card);
1794 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
1796 goto probe_aux_dev_err;
1800 snd_soc_dapm_new_widgets(&card->dapm);
1802 if (card->fully_routed)
1803 list_for_each_entry(codec, &card->codec_dev_list, card_list)
1804 snd_soc_dapm_auto_nc_codec_pins(codec);
1806 ret = snd_card_register(card->snd_card);
1808 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1810 goto probe_aux_dev_err;
1813 #ifdef CONFIG_SND_SOC_AC97_BUS
1814 /* register any AC97 codecs */
1815 for (i = 0; i < card->num_rtd; i++) {
1816 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1818 dev_err(card->dev, "ASoC: failed to register AC97:"
1821 soc_unregister_ac97_dai_link(card->rtd[i].codec);
1822 goto probe_aux_dev_err;
1827 card->instantiated = 1;
1828 snd_soc_dapm_sync(&card->dapm);
1829 mutex_unlock(&card->mutex);
1834 for (i = 0; i < card->num_aux_devs; i++)
1835 soc_remove_aux_dev(card, i);
1838 soc_remove_dai_links(card);
1844 snd_card_free(card->snd_card);
1847 mutex_unlock(&card->mutex);
1852 /* probes a new socdev */
1853 static int soc_probe(struct platform_device *pdev)
1855 struct snd_soc_card *card = platform_get_drvdata(pdev);
1858 * no card, so machine driver should be registering card
1859 * we should not be here in that case so ret error
1864 dev_warn(&pdev->dev,
1865 "ASoC: machine %s should use snd_soc_register_card()\n",
1868 /* Bodge while we unpick instantiation */
1869 card->dev = &pdev->dev;
1871 return snd_soc_register_card(card);
1874 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1878 /* make sure any delayed work runs */
1879 for (i = 0; i < card->num_rtd; i++) {
1880 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1881 flush_delayed_work(&rtd->delayed_work);
1884 /* remove auxiliary devices */
1885 for (i = 0; i < card->num_aux_devs; i++)
1886 soc_remove_aux_dev(card, i);
1888 /* remove and free each DAI */
1889 soc_remove_dai_links(card);
1891 soc_cleanup_card_debugfs(card);
1893 /* remove the card */
1897 snd_soc_dapm_free(&card->dapm);
1899 snd_card_free(card->snd_card);
1904 /* removes a socdev */
1905 static int soc_remove(struct platform_device *pdev)
1907 struct snd_soc_card *card = platform_get_drvdata(pdev);
1909 snd_soc_unregister_card(card);
1913 int snd_soc_poweroff(struct device *dev)
1915 struct snd_soc_card *card = dev_get_drvdata(dev);
1918 if (!card->instantiated)
1921 /* Flush out pmdown_time work - we actually do want to run it
1922 * now, we're shutting down so no imminent restart. */
1923 for (i = 0; i < card->num_rtd; i++) {
1924 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1925 flush_delayed_work(&rtd->delayed_work);
1928 snd_soc_dapm_shutdown(card);
1932 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1934 const struct dev_pm_ops snd_soc_pm_ops = {
1935 .suspend = snd_soc_suspend,
1936 .resume = snd_soc_resume,
1937 .freeze = snd_soc_suspend,
1938 .thaw = snd_soc_resume,
1939 .poweroff = snd_soc_poweroff,
1940 .restore = snd_soc_resume,
1942 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1944 /* ASoC platform driver */
1945 static struct platform_driver soc_driver = {
1947 .name = "soc-audio",
1948 .owner = THIS_MODULE,
1949 .pm = &snd_soc_pm_ops,
1952 .remove = soc_remove,
1956 * snd_soc_codec_volatile_register: Report if a register is volatile.
1958 * @codec: CODEC to query.
1959 * @reg: Register to query.
1961 * Boolean function indiciating if a CODEC register is volatile.
1963 int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1966 if (codec->volatile_register)
1967 return codec->volatile_register(codec, reg);
1971 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1974 * snd_soc_codec_readable_register: Report if a register is readable.
1976 * @codec: CODEC to query.
1977 * @reg: Register to query.
1979 * Boolean function indicating if a CODEC register is readable.
1981 int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1984 if (codec->readable_register)
1985 return codec->readable_register(codec, reg);
1989 EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1992 * snd_soc_codec_writable_register: Report if a register is writable.
1994 * @codec: CODEC to query.
1995 * @reg: Register to query.
1997 * Boolean function indicating if a CODEC register is writable.
1999 int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
2002 if (codec->writable_register)
2003 return codec->writable_register(codec, reg);
2007 EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
2009 int snd_soc_platform_read(struct snd_soc_platform *platform,
2014 if (!platform->driver->read) {
2015 dev_err(platform->dev, "ASoC: platform has no read back\n");
2019 ret = platform->driver->read(platform, reg);
2020 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
2021 trace_snd_soc_preg_read(platform, reg, ret);
2025 EXPORT_SYMBOL_GPL(snd_soc_platform_read);
2027 int snd_soc_platform_write(struct snd_soc_platform *platform,
2028 unsigned int reg, unsigned int val)
2030 if (!platform->driver->write) {
2031 dev_err(platform->dev, "ASoC: platform has no write back\n");
2035 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
2036 trace_snd_soc_preg_write(platform, reg, val);
2037 return platform->driver->write(platform, reg, val);
2039 EXPORT_SYMBOL_GPL(snd_soc_platform_write);
2042 * snd_soc_new_ac97_codec - initailise AC97 device
2043 * @codec: audio codec
2044 * @ops: AC97 bus operations
2045 * @num: AC97 codec number
2047 * Initialises AC97 codec resources for use by ad-hoc devices only.
2049 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2050 struct snd_ac97_bus_ops *ops, int num)
2052 mutex_lock(&codec->mutex);
2054 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2055 if (codec->ac97 == NULL) {
2056 mutex_unlock(&codec->mutex);
2060 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2061 if (codec->ac97->bus == NULL) {
2064 mutex_unlock(&codec->mutex);
2068 codec->ac97->bus->ops = ops;
2069 codec->ac97->num = num;
2072 * Mark the AC97 device to be created by us. This way we ensure that the
2073 * device will be registered with the device subsystem later on.
2075 codec->ac97_created = 1;
2077 mutex_unlock(&codec->mutex);
2080 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2082 struct snd_ac97_bus_ops *soc_ac97_ops;
2084 int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2086 if (ops == soc_ac97_ops)
2089 if (soc_ac97_ops && ops)
2096 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2099 * snd_soc_free_ac97_codec - free AC97 codec device
2100 * @codec: audio codec
2102 * Frees AC97 codec device resources.
2104 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2106 mutex_lock(&codec->mutex);
2107 #ifdef CONFIG_SND_SOC_AC97_BUS
2108 soc_unregister_ac97_dai_link(codec);
2110 kfree(codec->ac97->bus);
2113 codec->ac97_created = 0;
2114 mutex_unlock(&codec->mutex);
2116 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2118 unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2122 ret = codec->read(codec, reg);
2123 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
2124 trace_snd_soc_reg_read(codec, reg, ret);
2128 EXPORT_SYMBOL_GPL(snd_soc_read);
2130 unsigned int snd_soc_write(struct snd_soc_codec *codec,
2131 unsigned int reg, unsigned int val)
2133 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
2134 trace_snd_soc_reg_write(codec, reg, val);
2135 return codec->write(codec, reg, val);
2137 EXPORT_SYMBOL_GPL(snd_soc_write);
2139 unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
2140 unsigned int reg, const void *data, size_t len)
2142 return codec->bulk_write_raw(codec, reg, data, len);
2144 EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
2147 * snd_soc_update_bits - update codec register bits
2148 * @codec: audio codec
2149 * @reg: codec register
2150 * @mask: register mask
2153 * Writes new register value.
2155 * Returns 1 for change, 0 for no change, or negative error code.
2157 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
2158 unsigned int mask, unsigned int value)
2161 unsigned int old, new;
2164 if (codec->using_regmap) {
2165 ret = regmap_update_bits_check(codec->control_data, reg,
2166 mask, value, &change);
2168 ret = snd_soc_read(codec, reg);
2173 new = (old & ~mask) | (value & mask);
2174 change = old != new;
2176 ret = snd_soc_write(codec, reg, new);
2184 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2187 * snd_soc_update_bits_locked - update codec register bits
2188 * @codec: audio codec
2189 * @reg: codec register
2190 * @mask: register mask
2193 * Writes new register value, and takes the codec mutex.
2195 * Returns 1 for change else 0.
2197 int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2198 unsigned short reg, unsigned int mask,
2203 mutex_lock(&codec->mutex);
2204 change = snd_soc_update_bits(codec, reg, mask, value);
2205 mutex_unlock(&codec->mutex);
2209 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
2212 * snd_soc_test_bits - test register for change
2213 * @codec: audio codec
2214 * @reg: codec register
2215 * @mask: register mask
2218 * Tests a register with a new value and checks if the new value is
2219 * different from the old value.
2221 * Returns 1 for change else 0.
2223 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
2224 unsigned int mask, unsigned int value)
2227 unsigned int old, new;
2229 old = snd_soc_read(codec, reg);
2230 new = (old & ~mask) | value;
2231 change = old != new;
2235 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2238 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2239 * @substream: the pcm substream
2240 * @hw: the hardware parameters
2242 * Sets the substream runtime hardware parameters.
2244 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2245 const struct snd_pcm_hardware *hw)
2247 struct snd_pcm_runtime *runtime = substream->runtime;
2248 runtime->hw.info = hw->info;
2249 runtime->hw.formats = hw->formats;
2250 runtime->hw.period_bytes_min = hw->period_bytes_min;
2251 runtime->hw.period_bytes_max = hw->period_bytes_max;
2252 runtime->hw.periods_min = hw->periods_min;
2253 runtime->hw.periods_max = hw->periods_max;
2254 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2255 runtime->hw.fifo_size = hw->fifo_size;
2258 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2261 * snd_soc_cnew - create new control
2262 * @_template: control template
2263 * @data: control private data
2264 * @long_name: control long name
2265 * @prefix: control name prefix
2267 * Create a new mixer control from a template control.
2269 * Returns 0 for success, else error.
2271 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2272 void *data, const char *long_name,
2275 struct snd_kcontrol_new template;
2276 struct snd_kcontrol *kcontrol;
2280 memcpy(&template, _template, sizeof(template));
2284 long_name = template.name;
2287 name_len = strlen(long_name) + strlen(prefix) + 2;
2288 name = kmalloc(name_len, GFP_KERNEL);
2292 snprintf(name, name_len, "%s %s", prefix, long_name);
2294 template.name = name;
2296 template.name = long_name;
2299 kcontrol = snd_ctl_new1(&template, data);
2305 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2307 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2308 const struct snd_kcontrol_new *controls, int num_controls,
2309 const char *prefix, void *data)
2313 for (i = 0; i < num_controls; i++) {
2314 const struct snd_kcontrol_new *control = &controls[i];
2315 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2316 control->name, prefix));
2318 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2319 control->name, err);
2328 * snd_soc_add_codec_controls - add an array of controls to a codec.
2329 * Convenience function to add a list of controls. Many codecs were
2330 * duplicating this code.
2332 * @codec: codec to add controls to
2333 * @controls: array of controls to add
2334 * @num_controls: number of elements in the array
2336 * Return 0 for success, else error.
2338 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2339 const struct snd_kcontrol_new *controls, int num_controls)
2341 struct snd_card *card = codec->card->snd_card;
2343 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2344 codec->name_prefix, codec);
2346 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2349 * snd_soc_add_platform_controls - add an array of controls to a platform.
2350 * Convenience function to add a list of controls.
2352 * @platform: platform to add controls to
2353 * @controls: array of controls to add
2354 * @num_controls: number of elements in the array
2356 * Return 0 for success, else error.
2358 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2359 const struct snd_kcontrol_new *controls, int num_controls)
2361 struct snd_card *card = platform->card->snd_card;
2363 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2366 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2369 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2370 * Convenience function to add a list of controls.
2372 * @soc_card: SoC card to add controls to
2373 * @controls: array of controls to add
2374 * @num_controls: number of elements in the array
2376 * Return 0 for success, else error.
2378 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2379 const struct snd_kcontrol_new *controls, int num_controls)
2381 struct snd_card *card = soc_card->snd_card;
2383 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2386 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2389 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2390 * Convienience function to add a list of controls.
2392 * @dai: DAI to add controls to
2393 * @controls: array of controls to add
2394 * @num_controls: number of elements in the array
2396 * Return 0 for success, else error.
2398 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2399 const struct snd_kcontrol_new *controls, int num_controls)
2401 struct snd_card *card = dai->card->snd_card;
2403 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2406 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2409 * snd_soc_info_enum_double - enumerated double mixer info callback
2410 * @kcontrol: mixer control
2411 * @uinfo: control element information
2413 * Callback to provide information about a double enumerated
2416 * Returns 0 for success.
2418 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2419 struct snd_ctl_elem_info *uinfo)
2421 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2423 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2424 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2425 uinfo->value.enumerated.items = e->max;
2427 if (uinfo->value.enumerated.item > e->max - 1)
2428 uinfo->value.enumerated.item = e->max - 1;
2429 strcpy(uinfo->value.enumerated.name,
2430 e->texts[uinfo->value.enumerated.item]);
2433 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2436 * snd_soc_get_enum_double - enumerated double mixer get callback
2437 * @kcontrol: mixer control
2438 * @ucontrol: control element information
2440 * Callback to get the value of a double enumerated mixer.
2442 * Returns 0 for success.
2444 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2445 struct snd_ctl_elem_value *ucontrol)
2447 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2448 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2451 val = snd_soc_read(codec, e->reg);
2452 ucontrol->value.enumerated.item[0]
2453 = (val >> e->shift_l) & e->mask;
2454 if (e->shift_l != e->shift_r)
2455 ucontrol->value.enumerated.item[1] =
2456 (val >> e->shift_r) & e->mask;
2460 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2463 * snd_soc_put_enum_double - enumerated double mixer put callback
2464 * @kcontrol: mixer control
2465 * @ucontrol: control element information
2467 * Callback to set the value of a double enumerated mixer.
2469 * Returns 0 for success.
2471 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2472 struct snd_ctl_elem_value *ucontrol)
2474 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2475 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2479 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2481 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2482 mask = e->mask << e->shift_l;
2483 if (e->shift_l != e->shift_r) {
2484 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2486 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2487 mask |= e->mask << e->shift_r;
2490 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2492 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2495 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2496 * @kcontrol: mixer control
2497 * @ucontrol: control element information
2499 * Callback to get the value of a double semi enumerated mixer.
2501 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2502 * used for handling bitfield coded enumeration for example.
2504 * Returns 0 for success.
2506 int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2507 struct snd_ctl_elem_value *ucontrol)
2509 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2510 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2511 unsigned int reg_val, val, mux;
2513 reg_val = snd_soc_read(codec, e->reg);
2514 val = (reg_val >> e->shift_l) & e->mask;
2515 for (mux = 0; mux < e->max; mux++) {
2516 if (val == e->values[mux])
2519 ucontrol->value.enumerated.item[0] = mux;
2520 if (e->shift_l != e->shift_r) {
2521 val = (reg_val >> e->shift_r) & e->mask;
2522 for (mux = 0; mux < e->max; mux++) {
2523 if (val == e->values[mux])
2526 ucontrol->value.enumerated.item[1] = mux;
2531 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2534 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2535 * @kcontrol: mixer control
2536 * @ucontrol: control element information
2538 * Callback to set the value of a double semi enumerated mixer.
2540 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2541 * used for handling bitfield coded enumeration for example.
2543 * Returns 0 for success.
2545 int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2546 struct snd_ctl_elem_value *ucontrol)
2548 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2549 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2553 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2555 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2556 mask = e->mask << e->shift_l;
2557 if (e->shift_l != e->shift_r) {
2558 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2560 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2561 mask |= e->mask << e->shift_r;
2564 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2566 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2569 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2570 * @kcontrol: mixer control
2571 * @uinfo: control element information
2573 * Callback to provide information about an external enumerated
2576 * Returns 0 for success.
2578 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2579 struct snd_ctl_elem_info *uinfo)
2581 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2583 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2585 uinfo->value.enumerated.items = e->max;
2587 if (uinfo->value.enumerated.item > e->max - 1)
2588 uinfo->value.enumerated.item = e->max - 1;
2589 strcpy(uinfo->value.enumerated.name,
2590 e->texts[uinfo->value.enumerated.item]);
2593 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2596 * snd_soc_info_volsw_ext - external single mixer info callback
2597 * @kcontrol: mixer control
2598 * @uinfo: control element information
2600 * Callback to provide information about a single external mixer control.
2602 * Returns 0 for success.
2604 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2605 struct snd_ctl_elem_info *uinfo)
2607 int max = kcontrol->private_value;
2609 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2610 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2612 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2615 uinfo->value.integer.min = 0;
2616 uinfo->value.integer.max = max;
2619 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2622 * snd_soc_info_volsw - single mixer info callback
2623 * @kcontrol: mixer control
2624 * @uinfo: control element information
2626 * Callback to provide information about a single mixer control, or a double
2627 * mixer control that spans 2 registers.
2629 * Returns 0 for success.
2631 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2632 struct snd_ctl_elem_info *uinfo)
2634 struct soc_mixer_control *mc =
2635 (struct soc_mixer_control *)kcontrol->private_value;
2638 if (!mc->platform_max)
2639 mc->platform_max = mc->max;
2640 platform_max = mc->platform_max;
2642 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2643 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2645 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2647 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2648 uinfo->value.integer.min = 0;
2649 uinfo->value.integer.max = platform_max;
2652 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2655 * snd_soc_get_volsw - single mixer get callback
2656 * @kcontrol: mixer control
2657 * @ucontrol: control element information
2659 * Callback to get the value of a single mixer control, or a double mixer
2660 * control that spans 2 registers.
2662 * Returns 0 for success.
2664 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2665 struct snd_ctl_elem_value *ucontrol)
2667 struct soc_mixer_control *mc =
2668 (struct soc_mixer_control *)kcontrol->private_value;
2669 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2670 unsigned int reg = mc->reg;
2671 unsigned int reg2 = mc->rreg;
2672 unsigned int shift = mc->shift;
2673 unsigned int rshift = mc->rshift;
2675 unsigned int mask = (1 << fls(max)) - 1;
2676 unsigned int invert = mc->invert;
2678 ucontrol->value.integer.value[0] =
2679 (snd_soc_read(codec, reg) >> shift) & mask;
2681 ucontrol->value.integer.value[0] =
2682 max - ucontrol->value.integer.value[0];
2684 if (snd_soc_volsw_is_stereo(mc)) {
2686 ucontrol->value.integer.value[1] =
2687 (snd_soc_read(codec, reg) >> rshift) & mask;
2689 ucontrol->value.integer.value[1] =
2690 (snd_soc_read(codec, reg2) >> shift) & mask;
2692 ucontrol->value.integer.value[1] =
2693 max - ucontrol->value.integer.value[1];
2698 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2701 * snd_soc_put_volsw - single mixer put callback
2702 * @kcontrol: mixer control
2703 * @ucontrol: control element information
2705 * Callback to set the value of a single mixer control, or a double mixer
2706 * control that spans 2 registers.
2708 * Returns 0 for success.
2710 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2711 struct snd_ctl_elem_value *ucontrol)
2713 struct soc_mixer_control *mc =
2714 (struct soc_mixer_control *)kcontrol->private_value;
2715 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2716 unsigned int reg = mc->reg;
2717 unsigned int reg2 = mc->rreg;
2718 unsigned int shift = mc->shift;
2719 unsigned int rshift = mc->rshift;
2721 unsigned int mask = (1 << fls(max)) - 1;
2722 unsigned int invert = mc->invert;
2725 unsigned int val2 = 0;
2726 unsigned int val, val_mask;
2728 val = (ucontrol->value.integer.value[0] & mask);
2731 val_mask = mask << shift;
2733 if (snd_soc_volsw_is_stereo(mc)) {
2734 val2 = (ucontrol->value.integer.value[1] & mask);
2738 val_mask |= mask << rshift;
2739 val |= val2 << rshift;
2741 val2 = val2 << shift;
2745 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2750 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2754 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2757 * snd_soc_get_volsw_sx - single mixer get callback
2758 * @kcontrol: mixer control
2759 * @ucontrol: control element information
2761 * Callback to get the value of a single mixer control, or a double mixer
2762 * control that spans 2 registers.
2764 * Returns 0 for success.
2766 int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2767 struct snd_ctl_elem_value *ucontrol)
2769 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2770 struct soc_mixer_control *mc =
2771 (struct soc_mixer_control *)kcontrol->private_value;
2773 unsigned int reg = mc->reg;
2774 unsigned int reg2 = mc->rreg;
2775 unsigned int shift = mc->shift;
2776 unsigned int rshift = mc->rshift;
2779 int mask = (1 << (fls(min + max) - 1)) - 1;
2781 ucontrol->value.integer.value[0] =
2782 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2784 if (snd_soc_volsw_is_stereo(mc))
2785 ucontrol->value.integer.value[1] =
2786 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2790 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2793 * snd_soc_put_volsw_sx - double mixer set callback
2794 * @kcontrol: mixer control
2795 * @uinfo: control element information
2797 * Callback to set the value of a double mixer control that spans 2 registers.
2799 * Returns 0 for success.
2801 int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2802 struct snd_ctl_elem_value *ucontrol)
2804 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2805 struct soc_mixer_control *mc =
2806 (struct soc_mixer_control *)kcontrol->private_value;
2808 unsigned int reg = mc->reg;
2809 unsigned int reg2 = mc->rreg;
2810 unsigned int shift = mc->shift;
2811 unsigned int rshift = mc->rshift;
2814 int mask = (1 << (fls(min + max) - 1)) - 1;
2816 unsigned short val, val_mask, val2 = 0;
2818 val_mask = mask << shift;
2819 val = (ucontrol->value.integer.value[0] + min) & mask;
2822 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2826 if (snd_soc_volsw_is_stereo(mc)) {
2827 val_mask = mask << rshift;
2828 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2829 val2 = val2 << rshift;
2831 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2836 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2839 * snd_soc_info_volsw_s8 - signed mixer info callback
2840 * @kcontrol: mixer control
2841 * @uinfo: control element information
2843 * Callback to provide information about a signed mixer control.
2845 * Returns 0 for success.
2847 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2848 struct snd_ctl_elem_info *uinfo)
2850 struct soc_mixer_control *mc =
2851 (struct soc_mixer_control *)kcontrol->private_value;
2855 if (!mc->platform_max)
2856 mc->platform_max = mc->max;
2857 platform_max = mc->platform_max;
2859 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2861 uinfo->value.integer.min = 0;
2862 uinfo->value.integer.max = platform_max - min;
2865 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2868 * snd_soc_get_volsw_s8 - signed mixer get callback
2869 * @kcontrol: mixer control
2870 * @ucontrol: control element information
2872 * Callback to get the value of a signed mixer control.
2874 * Returns 0 for success.
2876 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2877 struct snd_ctl_elem_value *ucontrol)
2879 struct soc_mixer_control *mc =
2880 (struct soc_mixer_control *)kcontrol->private_value;
2881 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2882 unsigned int reg = mc->reg;
2884 int val = snd_soc_read(codec, reg);
2886 ucontrol->value.integer.value[0] =
2887 ((signed char)(val & 0xff))-min;
2888 ucontrol->value.integer.value[1] =
2889 ((signed char)((val >> 8) & 0xff))-min;
2892 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2895 * snd_soc_put_volsw_sgn - signed mixer put callback
2896 * @kcontrol: mixer control
2897 * @ucontrol: control element information
2899 * Callback to set the value of a signed mixer control.
2901 * Returns 0 for success.
2903 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2904 struct snd_ctl_elem_value *ucontrol)
2906 struct soc_mixer_control *mc =
2907 (struct soc_mixer_control *)kcontrol->private_value;
2908 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2909 unsigned int reg = mc->reg;
2913 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2914 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2916 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2918 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2921 * snd_soc_info_volsw_range - single mixer info callback with range.
2922 * @kcontrol: mixer control
2923 * @uinfo: control element information
2925 * Callback to provide information, within a range, about a single
2928 * returns 0 for success.
2930 int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2931 struct snd_ctl_elem_info *uinfo)
2933 struct soc_mixer_control *mc =
2934 (struct soc_mixer_control *)kcontrol->private_value;
2938 if (!mc->platform_max)
2939 mc->platform_max = mc->max;
2940 platform_max = mc->platform_max;
2942 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2943 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2944 uinfo->value.integer.min = 0;
2945 uinfo->value.integer.max = platform_max - min;
2949 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2952 * snd_soc_put_volsw_range - single mixer put value callback with range.
2953 * @kcontrol: mixer control
2954 * @ucontrol: control element information
2956 * Callback to set the value, within a range, for a single mixer control.
2958 * Returns 0 for success.
2960 int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2961 struct snd_ctl_elem_value *ucontrol)
2963 struct soc_mixer_control *mc =
2964 (struct soc_mixer_control *)kcontrol->private_value;
2965 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2966 unsigned int reg = mc->reg;
2967 unsigned int rreg = mc->rreg;
2968 unsigned int shift = mc->shift;
2971 unsigned int mask = (1 << fls(max)) - 1;
2972 unsigned int invert = mc->invert;
2973 unsigned int val, val_mask;
2976 val = ((ucontrol->value.integer.value[0] + min) & mask);
2979 val_mask = mask << shift;
2982 ret = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2986 if (snd_soc_volsw_is_stereo(mc)) {
2987 val = ((ucontrol->value.integer.value[1] + min) & mask);
2990 val_mask = mask << shift;
2993 ret = snd_soc_update_bits_locked(codec, rreg, val_mask, val);
2998 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
3001 * snd_soc_get_volsw_range - single mixer get callback with range
3002 * @kcontrol: mixer control
3003 * @ucontrol: control element information
3005 * Callback to get the value, within a range, of a single mixer control.
3007 * Returns 0 for success.
3009 int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
3010 struct snd_ctl_elem_value *ucontrol)
3012 struct soc_mixer_control *mc =
3013 (struct soc_mixer_control *)kcontrol->private_value;
3014 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3015 unsigned int reg = mc->reg;
3016 unsigned int rreg = mc->rreg;
3017 unsigned int shift = mc->shift;
3020 unsigned int mask = (1 << fls(max)) - 1;
3021 unsigned int invert = mc->invert;
3023 ucontrol->value.integer.value[0] =
3024 (snd_soc_read(codec, reg) >> shift) & mask;
3026 ucontrol->value.integer.value[0] =
3027 max - ucontrol->value.integer.value[0];
3028 ucontrol->value.integer.value[0] =
3029 ucontrol->value.integer.value[0] - min;
3031 if (snd_soc_volsw_is_stereo(mc)) {
3032 ucontrol->value.integer.value[1] =
3033 (snd_soc_read(codec, rreg) >> shift) & mask;
3035 ucontrol->value.integer.value[1] =
3036 max - ucontrol->value.integer.value[1];
3037 ucontrol->value.integer.value[1] =
3038 ucontrol->value.integer.value[1] - min;
3043 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3046 * snd_soc_limit_volume - Set new limit to an existing volume control.
3048 * @codec: where to look for the control
3049 * @name: Name of the control
3050 * @max: new maximum limit
3052 * Return 0 for success, else error.
3054 int snd_soc_limit_volume(struct snd_soc_codec *codec,
3055 const char *name, int max)
3057 struct snd_card *card = codec->card->snd_card;
3058 struct snd_kcontrol *kctl;
3059 struct soc_mixer_control *mc;
3063 /* Sanity check for name and max */
3064 if (unlikely(!name || max <= 0))
3067 list_for_each_entry(kctl, &card->controls, list) {
3068 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3074 mc = (struct soc_mixer_control *)kctl->private_value;
3075 if (max <= mc->max) {
3076 mc->platform_max = max;
3082 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3084 int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3085 struct snd_ctl_elem_info *uinfo)
3087 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3088 struct soc_bytes *params = (void *)kcontrol->private_value;
3090 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3091 uinfo->count = params->num_regs * codec->val_bytes;
3095 EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3097 int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3098 struct snd_ctl_elem_value *ucontrol)
3100 struct soc_bytes *params = (void *)kcontrol->private_value;
3101 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3104 if (codec->using_regmap)
3105 ret = regmap_raw_read(codec->control_data, params->base,
3106 ucontrol->value.bytes.data,
3107 params->num_regs * codec->val_bytes);
3111 /* Hide any masked bytes to ensure consistent data reporting */
3112 if (ret == 0 && params->mask) {
3113 switch (codec->val_bytes) {
3115 ucontrol->value.bytes.data[0] &= ~params->mask;
3118 ((u16 *)(&ucontrol->value.bytes.data))[0]
3122 ((u32 *)(&ucontrol->value.bytes.data))[0]
3132 EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3134 int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3135 struct snd_ctl_elem_value *ucontrol)
3137 struct soc_bytes *params = (void *)kcontrol->private_value;
3138 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3143 if (!codec->using_regmap)
3146 len = params->num_regs * codec->val_bytes;
3148 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3153 * If we've got a mask then we need to preserve the register
3154 * bits. We shouldn't modify the incoming data so take a
3158 ret = regmap_read(codec->control_data, params->base, &val);
3162 val &= params->mask;
3164 switch (codec->val_bytes) {
3166 ((u8 *)data)[0] &= ~params->mask;
3167 ((u8 *)data)[0] |= val;
3170 ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
3171 ((u16 *)data)[0] |= cpu_to_be16(val);
3174 ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
3175 ((u32 *)data)[0] |= cpu_to_be32(val);
3183 ret = regmap_raw_write(codec->control_data, params->base,
3191 EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3194 * snd_soc_info_xr_sx - signed multi register info callback
3195 * @kcontrol: mreg control
3196 * @uinfo: control element information
3198 * Callback to provide information of a control that can
3199 * span multiple codec registers which together
3200 * forms a single signed value in a MSB/LSB manner.
3202 * Returns 0 for success.
3204 int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3205 struct snd_ctl_elem_info *uinfo)
3207 struct soc_mreg_control *mc =
3208 (struct soc_mreg_control *)kcontrol->private_value;
3209 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3211 uinfo->value.integer.min = mc->min;
3212 uinfo->value.integer.max = mc->max;
3216 EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3219 * snd_soc_get_xr_sx - signed multi register get callback
3220 * @kcontrol: mreg control
3221 * @ucontrol: control element information
3223 * Callback to get the value of a control that can span
3224 * multiple codec registers which together forms a single
3225 * signed value in a MSB/LSB manner. The control supports
3226 * specifying total no of bits used to allow for bitfields
3227 * across the multiple codec registers.
3229 * Returns 0 for success.
3231 int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3232 struct snd_ctl_elem_value *ucontrol)
3234 struct soc_mreg_control *mc =
3235 (struct soc_mreg_control *)kcontrol->private_value;
3236 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3237 unsigned int regbase = mc->regbase;
3238 unsigned int regcount = mc->regcount;
3239 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3240 unsigned int regwmask = (1<<regwshift)-1;
3241 unsigned int invert = mc->invert;
3242 unsigned long mask = (1UL<<mc->nbits)-1;
3246 unsigned long regval;
3249 for (i = 0; i < regcount; i++) {
3250 regval = snd_soc_read(codec, regbase+i) & regwmask;
3251 val |= regval << (regwshift*(regcount-i-1));
3254 if (min < 0 && val > max)
3258 ucontrol->value.integer.value[0] = val;
3262 EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3265 * snd_soc_put_xr_sx - signed multi register get callback
3266 * @kcontrol: mreg control
3267 * @ucontrol: control element information
3269 * Callback to set the value of a control that can span
3270 * multiple codec registers which together forms a single
3271 * signed value in a MSB/LSB manner. The control supports
3272 * specifying total no of bits used to allow for bitfields
3273 * across the multiple codec registers.
3275 * Returns 0 for success.
3277 int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3278 struct snd_ctl_elem_value *ucontrol)
3280 struct soc_mreg_control *mc =
3281 (struct soc_mreg_control *)kcontrol->private_value;
3282 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3283 unsigned int regbase = mc->regbase;
3284 unsigned int regcount = mc->regcount;
3285 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3286 unsigned int regwmask = (1<<regwshift)-1;
3287 unsigned int invert = mc->invert;
3288 unsigned long mask = (1UL<<mc->nbits)-1;
3290 long val = ucontrol->value.integer.value[0];
3291 unsigned int i, regval, regmask;
3297 for (i = 0; i < regcount; i++) {
3298 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3299 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3300 err = snd_soc_update_bits_locked(codec, regbase+i,
3308 EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3311 * snd_soc_get_strobe - strobe get callback
3312 * @kcontrol: mixer control
3313 * @ucontrol: control element information
3315 * Callback get the value of a strobe mixer control.
3317 * Returns 0 for success.
3319 int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3320 struct snd_ctl_elem_value *ucontrol)
3322 struct soc_mixer_control *mc =
3323 (struct soc_mixer_control *)kcontrol->private_value;
3324 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3325 unsigned int reg = mc->reg;
3326 unsigned int shift = mc->shift;
3327 unsigned int mask = 1 << shift;
3328 unsigned int invert = mc->invert != 0;
3329 unsigned int val = snd_soc_read(codec, reg) & mask;
3331 if (shift != 0 && val != 0)
3333 ucontrol->value.enumerated.item[0] = val ^ invert;
3337 EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3340 * snd_soc_put_strobe - strobe put callback
3341 * @kcontrol: mixer control
3342 * @ucontrol: control element information
3344 * Callback strobe a register bit to high then low (or the inverse)
3345 * in one pass of a single mixer enum control.
3347 * Returns 1 for success.
3349 int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3350 struct snd_ctl_elem_value *ucontrol)
3352 struct soc_mixer_control *mc =
3353 (struct soc_mixer_control *)kcontrol->private_value;
3354 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3355 unsigned int reg = mc->reg;
3356 unsigned int shift = mc->shift;
3357 unsigned int mask = 1 << shift;
3358 unsigned int invert = mc->invert != 0;
3359 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3360 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3361 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3364 err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3368 err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3371 EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3374 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3376 * @clk_id: DAI specific clock ID
3377 * @freq: new clock frequency in Hz
3378 * @dir: new clock direction - input/output.
3380 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3382 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3383 unsigned int freq, int dir)
3385 if (dai->driver && dai->driver->ops->set_sysclk)
3386 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3387 else if (dai->codec && dai->codec->driver->set_sysclk)
3388 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3393 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3396 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3398 * @clk_id: DAI specific clock ID
3399 * @source: Source for the clock
3400 * @freq: new clock frequency in Hz
3401 * @dir: new clock direction - input/output.
3403 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3405 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3406 int source, unsigned int freq, int dir)
3408 if (codec->driver->set_sysclk)
3409 return codec->driver->set_sysclk(codec, clk_id, source,
3414 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3417 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3419 * @div_id: DAI specific clock divider ID
3420 * @div: new clock divisor.
3422 * Configures the clock dividers. This is used to derive the best DAI bit and
3423 * frame clocks from the system or master clock. It's best to set the DAI bit
3424 * and frame clocks as low as possible to save system power.
3426 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3427 int div_id, int div)
3429 if (dai->driver && dai->driver->ops->set_clkdiv)
3430 return dai->driver->ops->set_clkdiv(dai, div_id, div);
3434 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3437 * snd_soc_dai_set_pll - configure DAI PLL.
3439 * @pll_id: DAI specific PLL ID
3440 * @source: DAI specific source for the PLL
3441 * @freq_in: PLL input clock frequency in Hz
3442 * @freq_out: requested PLL output clock frequency in Hz
3444 * Configures and enables PLL to generate output clock based on input clock.
3446 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3447 unsigned int freq_in, unsigned int freq_out)
3449 if (dai->driver && dai->driver->ops->set_pll)
3450 return dai->driver->ops->set_pll(dai, pll_id, source,
3452 else if (dai->codec && dai->codec->driver->set_pll)
3453 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3458 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3461 * snd_soc_codec_set_pll - configure codec PLL.
3463 * @pll_id: DAI specific PLL ID
3464 * @source: DAI specific source for the PLL
3465 * @freq_in: PLL input clock frequency in Hz
3466 * @freq_out: requested PLL output clock frequency in Hz
3468 * Configures and enables PLL to generate output clock based on input clock.
3470 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3471 unsigned int freq_in, unsigned int freq_out)
3473 if (codec->driver->set_pll)
3474 return codec->driver->set_pll(codec, pll_id, source,
3479 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3482 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3484 * @fmt: SND_SOC_DAIFMT_ format value.
3486 * Configures the DAI hardware format and clocking.
3488 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3490 if (dai->driver == NULL)
3492 if (dai->driver->ops->set_fmt == NULL)
3494 return dai->driver->ops->set_fmt(dai, fmt);
3496 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3499 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3501 * @tx_mask: bitmask representing active TX slots.
3502 * @rx_mask: bitmask representing active RX slots.
3503 * @slots: Number of slots in use.
3504 * @slot_width: Width in bits for each slot.
3506 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3509 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3510 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
3512 if (dai->driver && dai->driver->ops->set_tdm_slot)
3513 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3518 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3521 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3523 * @tx_num: how many TX channels
3524 * @tx_slot: pointer to an array which imply the TX slot number channel
3526 * @rx_num: how many RX channels
3527 * @rx_slot: pointer to an array which imply the RX slot number channel
3530 * configure the relationship between channel number and TDM slot number.
3532 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3533 unsigned int tx_num, unsigned int *tx_slot,
3534 unsigned int rx_num, unsigned int *rx_slot)
3536 if (dai->driver && dai->driver->ops->set_channel_map)
3537 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3542 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3545 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3547 * @tristate: tristate enable
3549 * Tristates the DAI so that others can use it.
3551 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3553 if (dai->driver && dai->driver->ops->set_tristate)
3554 return dai->driver->ops->set_tristate(dai, tristate);
3558 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3561 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3563 * @mute: mute enable
3564 * @direction: stream to mute
3566 * Mutes the DAI DAC.
3568 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3574 if (dai->driver->ops->mute_stream)
3575 return dai->driver->ops->mute_stream(dai, mute, direction);
3576 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3577 dai->driver->ops->digital_mute)
3578 return dai->driver->ops->digital_mute(dai, mute);
3582 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3585 * snd_soc_register_card - Register a card with the ASoC core
3587 * @card: Card to register
3590 int snd_soc_register_card(struct snd_soc_card *card)
3594 if (!card->name || !card->dev)
3597 for (i = 0; i < card->num_links; i++) {
3598 struct snd_soc_dai_link *link = &card->dai_link[i];
3601 * Codec must be specified by 1 of name or OF node,
3602 * not both or neither.
3604 if (!!link->codec_name == !!link->codec_of_node) {
3605 dev_err(card->dev, "ASoC: Neither/both codec"
3606 " name/of_node are set for %s\n", link->name);
3609 /* Codec DAI name must be specified */
3610 if (!link->codec_dai_name) {
3611 dev_err(card->dev, "ASoC: codec_dai_name not"
3612 " set for %s\n", link->name);
3617 * Platform may be specified by either name or OF node, but
3618 * can be left unspecified, and a dummy platform will be used.
3620 if (link->platform_name && link->platform_of_node) {
3621 dev_err(card->dev, "ASoC: Both platform name/of_node"
3622 " are set for %s\n", link->name);
3627 * CPU device may be specified by either name or OF node, but
3628 * can be left unspecified, and will be matched based on DAI
3631 if (link->cpu_name && link->cpu_of_node) {
3632 dev_err(card->dev, "ASoC: Neither/both "
3633 "cpu name/of_node are set for %s\n",link->name);
3637 * At least one of CPU DAI name or CPU device name/node must be
3640 if (!link->cpu_dai_name &&
3641 !(link->cpu_name || link->cpu_of_node)) {
3642 dev_err(card->dev, "ASoC: Neither cpu_dai_name nor "
3643 "cpu_name/of_node are set for %s\n", link->name);
3648 dev_set_drvdata(card->dev, card);
3650 snd_soc_initialize_card_lists(card);
3652 soc_init_card_debugfs(card);
3654 card->rtd = devm_kzalloc(card->dev,
3655 sizeof(struct snd_soc_pcm_runtime) *
3656 (card->num_links + card->num_aux_devs),
3658 if (card->rtd == NULL)
3661 card->rtd_aux = &card->rtd[card->num_links];
3663 for (i = 0; i < card->num_links; i++)
3664 card->rtd[i].dai_link = &card->dai_link[i];
3666 INIT_LIST_HEAD(&card->list);
3667 INIT_LIST_HEAD(&card->dapm_dirty);
3668 card->instantiated = 0;
3669 mutex_init(&card->mutex);
3670 mutex_init(&card->dapm_mutex);
3672 ret = snd_soc_instantiate_card(card);
3674 soc_cleanup_card_debugfs(card);
3678 EXPORT_SYMBOL_GPL(snd_soc_register_card);
3681 * snd_soc_unregister_card - Unregister a card with the ASoC core
3683 * @card: Card to unregister
3686 int snd_soc_unregister_card(struct snd_soc_card *card)
3688 if (card->instantiated)
3689 soc_cleanup_card_resources(card);
3690 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
3694 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3697 * Simplify DAI link configuration by removing ".-1" from device names
3698 * and sanitizing names.
3700 static char *fmt_single_name(struct device *dev, int *id)
3702 char *found, name[NAME_SIZE];
3705 if (dev_name(dev) == NULL)
3708 strlcpy(name, dev_name(dev), NAME_SIZE);
3710 /* are we a "%s.%d" name (platform and SPI components) */
3711 found = strstr(name, dev->driver->name);
3714 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3716 /* discard ID from name if ID == -1 */
3718 found[strlen(dev->driver->name)] = '\0';
3722 /* I2C component devices are named "bus-addr" */
3723 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3724 char tmp[NAME_SIZE];
3726 /* create unique ID number from I2C addr and bus */
3727 *id = ((id1 & 0xffff) << 16) + id2;
3729 /* sanitize component name for DAI link creation */
3730 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3731 strlcpy(name, tmp, NAME_SIZE);
3736 return kstrdup(name, GFP_KERNEL);
3740 * Simplify DAI link naming for single devices with multiple DAIs by removing
3741 * any ".-1" and using the DAI name (instead of device name).
3743 static inline char *fmt_multiple_name(struct device *dev,
3744 struct snd_soc_dai_driver *dai_drv)
3746 if (dai_drv->name == NULL) {
3747 dev_err(dev, "ASoC: error - multiple DAI %s registered with"
3748 " no name\n", dev_name(dev));
3752 return kstrdup(dai_drv->name, GFP_KERNEL);
3756 * snd_soc_register_dai - Register a DAI with the ASoC core
3758 * @dai: DAI to register
3760 static int snd_soc_register_dai(struct device *dev,
3761 struct snd_soc_dai_driver *dai_drv)
3763 struct snd_soc_codec *codec;
3764 struct snd_soc_dai *dai;
3766 dev_dbg(dev, "ASoC: dai register %s\n", dev_name(dev));
3768 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3772 /* create DAI component name */
3773 dai->name = fmt_single_name(dev, &dai->id);
3774 if (dai->name == NULL) {
3780 dai->driver = dai_drv;
3781 dai->dapm.dev = dev;
3782 if (!dai->driver->ops)
3783 dai->driver->ops = &null_dai_ops;
3785 mutex_lock(&client_mutex);
3787 list_for_each_entry(codec, &codec_list, list) {
3788 if (codec->dev == dev) {
3789 dev_dbg(dev, "ASoC: Mapped DAI %s to CODEC %s\n",
3790 dai->name, codec->name);
3797 dai->dapm.idle_bias_off = 1;
3799 list_add(&dai->list, &dai_list);
3801 mutex_unlock(&client_mutex);
3803 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3809 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3811 * @dai: DAI to unregister
3813 static void snd_soc_unregister_dai(struct device *dev)
3815 struct snd_soc_dai *dai;
3817 list_for_each_entry(dai, &dai_list, list) {
3818 if (dev == dai->dev)
3824 mutex_lock(&client_mutex);
3825 list_del(&dai->list);
3826 mutex_unlock(&client_mutex);
3828 dev_dbg(dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
3834 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3836 * @dai: Array of DAIs to register
3837 * @count: Number of DAIs
3839 static int snd_soc_register_dais(struct device *dev,
3840 struct snd_soc_dai_driver *dai_drv, size_t count)
3842 struct snd_soc_codec *codec;
3843 struct snd_soc_dai *dai;
3846 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
3848 for (i = 0; i < count; i++) {
3850 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3856 /* create DAI component name */
3857 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3858 if (dai->name == NULL) {
3865 dai->driver = &dai_drv[i];
3866 if (dai->driver->id)
3867 dai->id = dai->driver->id;
3870 dai->dapm.dev = dev;
3871 if (!dai->driver->ops)
3872 dai->driver->ops = &null_dai_ops;
3874 mutex_lock(&client_mutex);
3876 list_for_each_entry(codec, &codec_list, list) {
3877 if (codec->dev == dev) {
3878 dev_dbg(dev, "ASoC: Mapped DAI %s to "
3879 "CODEC %s\n", dai->name, codec->name);
3886 dai->dapm.idle_bias_off = 1;
3888 list_add(&dai->list, &dai_list);
3890 mutex_unlock(&client_mutex);
3892 dev_dbg(dai->dev, "ASoC: Registered DAI '%s'\n", dai->name);
3898 for (i--; i >= 0; i--)
3899 snd_soc_unregister_dai(dev);
3905 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3907 * @dai: Array of DAIs to unregister
3908 * @count: Number of DAIs
3910 static void snd_soc_unregister_dais(struct device *dev, size_t count)
3914 for (i = 0; i < count; i++)
3915 snd_soc_unregister_dai(dev);
3919 * snd_soc_add_platform - Add a platform to the ASoC core
3920 * @dev: The parent device for the platform
3921 * @platform: The platform to add
3922 * @platform_driver: The driver for the platform
3924 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
3925 const struct snd_soc_platform_driver *platform_drv)
3927 /* create platform component name */
3928 platform->name = fmt_single_name(dev, &platform->id);
3929 if (platform->name == NULL) {
3934 platform->dev = dev;
3935 platform->driver = platform_drv;
3936 platform->dapm.dev = dev;
3937 platform->dapm.platform = platform;
3938 platform->dapm.stream_event = platform_drv->stream_event;
3939 mutex_init(&platform->mutex);
3941 mutex_lock(&client_mutex);
3942 list_add(&platform->list, &platform_list);
3943 mutex_unlock(&client_mutex);
3945 dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
3949 EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3952 * snd_soc_register_platform - Register a platform with the ASoC core
3954 * @platform: platform to register
3956 int snd_soc_register_platform(struct device *dev,
3957 const struct snd_soc_platform_driver *platform_drv)
3959 struct snd_soc_platform *platform;
3962 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3964 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3965 if (platform == NULL)
3968 ret = snd_soc_add_platform(dev, platform, platform_drv);
3974 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3977 * snd_soc_remove_platform - Remove a platform from the ASoC core
3978 * @platform: the platform to remove
3980 void snd_soc_remove_platform(struct snd_soc_platform *platform)
3982 mutex_lock(&client_mutex);
3983 list_del(&platform->list);
3984 mutex_unlock(&client_mutex);
3986 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3988 kfree(platform->name);
3990 EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
3992 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
3994 struct snd_soc_platform *platform;
3996 list_for_each_entry(platform, &platform_list, list) {
3997 if (dev == platform->dev)
4003 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4006 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4008 * @platform: platform to unregister
4010 void snd_soc_unregister_platform(struct device *dev)
4012 struct snd_soc_platform *platform;
4014 platform = snd_soc_lookup_platform(dev);
4018 snd_soc_remove_platform(platform);
4021 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4023 static u64 codec_format_map[] = {
4024 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4025 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4026 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4027 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4028 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4029 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4030 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4031 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4032 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4033 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4034 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4035 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4036 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4037 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4038 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4039 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4042 /* Fix up the DAI formats for endianness: codecs don't actually see
4043 * the endianness of the data but we're using the CPU format
4044 * definitions which do need to include endianness so we ensure that
4045 * codec DAIs always have both big and little endian variants set.
4047 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4051 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4052 if (stream->formats & codec_format_map[i])
4053 stream->formats |= codec_format_map[i];
4057 * snd_soc_register_codec - Register a codec with the ASoC core
4059 * @codec: codec to register
4061 int snd_soc_register_codec(struct device *dev,
4062 const struct snd_soc_codec_driver *codec_drv,
4063 struct snd_soc_dai_driver *dai_drv,
4067 struct snd_soc_codec *codec;
4070 dev_dbg(dev, "codec register %s\n", dev_name(dev));
4072 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4076 /* create CODEC component name */
4077 codec->name = fmt_single_name(dev, &codec->id);
4078 if (codec->name == NULL) {
4083 if (codec_drv->compress_type)
4084 codec->compress_type = codec_drv->compress_type;
4086 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
4088 codec->write = codec_drv->write;
4089 codec->read = codec_drv->read;
4090 codec->volatile_register = codec_drv->volatile_register;
4091 codec->readable_register = codec_drv->readable_register;
4092 codec->writable_register = codec_drv->writable_register;
4093 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
4094 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4095 codec->dapm.dev = dev;
4096 codec->dapm.codec = codec;
4097 codec->dapm.seq_notifier = codec_drv->seq_notifier;
4098 codec->dapm.stream_event = codec_drv->stream_event;
4100 codec->driver = codec_drv;
4101 codec->num_dai = num_dai;
4102 mutex_init(&codec->mutex);
4104 /* allocate CODEC register cache */
4105 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
4106 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
4107 codec->reg_size = reg_size;
4108 /* it is necessary to make a copy of the default register cache
4109 * because in the case of using a compression type that requires
4110 * the default register cache to be marked as the
4111 * kernel might have freed the array by the time we initialize
4114 if (codec_drv->reg_cache_default) {
4115 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
4116 reg_size, GFP_KERNEL);
4117 if (!codec->reg_def_copy) {
4119 goto fail_codec_name;
4124 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
4125 if (!codec->volatile_register)
4126 codec->volatile_register = snd_soc_default_volatile_register;
4127 if (!codec->readable_register)
4128 codec->readable_register = snd_soc_default_readable_register;
4129 if (!codec->writable_register)
4130 codec->writable_register = snd_soc_default_writable_register;
4133 for (i = 0; i < num_dai; i++) {
4134 fixup_codec_formats(&dai_drv[i].playback);
4135 fixup_codec_formats(&dai_drv[i].capture);
4138 mutex_lock(&client_mutex);
4139 list_add(&codec->list, &codec_list);
4140 mutex_unlock(&client_mutex);
4142 /* register any DAIs */
4143 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
4145 dev_err(codec->dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4146 goto fail_codec_name;
4149 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
4153 mutex_lock(&client_mutex);
4154 list_del(&codec->list);
4155 mutex_unlock(&client_mutex);
4162 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4165 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4167 * @codec: codec to unregister
4169 void snd_soc_unregister_codec(struct device *dev)
4171 struct snd_soc_codec *codec;
4173 list_for_each_entry(codec, &codec_list, list) {
4174 if (dev == codec->dev)
4180 snd_soc_unregister_dais(dev, codec->num_dai);
4182 mutex_lock(&client_mutex);
4183 list_del(&codec->list);
4184 mutex_unlock(&client_mutex);
4186 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
4188 snd_soc_cache_exit(codec);
4189 kfree(codec->reg_def_copy);
4193 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4197 * snd_soc_register_component - Register a component with the ASoC core
4200 int snd_soc_register_component(struct device *dev,
4201 const struct snd_soc_component_driver *cmpnt_drv,
4202 struct snd_soc_dai_driver *dai_drv,
4205 struct snd_soc_component *cmpnt;
4208 dev_dbg(dev, "component register %s\n", dev_name(dev));
4210 cmpnt = devm_kzalloc(dev, sizeof(*cmpnt), GFP_KERNEL);
4212 dev_err(dev, "ASoC: Failed to allocate memory\n");
4216 cmpnt->name = fmt_single_name(dev, &cmpnt->id);
4218 dev_err(dev, "ASoC: Failed to simplifying name\n");
4223 cmpnt->driver = cmpnt_drv;
4224 cmpnt->num_dai = num_dai;
4227 * snd_soc_register_dai() uses fmt_single_name(), and
4228 * snd_soc_register_dais() uses fmt_multiple_name()
4229 * for dai->name which is used for name based matching
4232 ret = snd_soc_register_dai(dev, dai_drv);
4234 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
4236 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4237 goto error_component_name;
4240 mutex_lock(&client_mutex);
4241 list_add(&cmpnt->list, &component_list);
4242 mutex_unlock(&client_mutex);
4244 dev_dbg(cmpnt->dev, "ASoC: Registered component '%s'\n", cmpnt->name);
4248 error_component_name:
4253 EXPORT_SYMBOL_GPL(snd_soc_register_component);
4256 * snd_soc_unregister_component - Unregister a component from the ASoC core
4259 void snd_soc_unregister_component(struct device *dev)
4261 struct snd_soc_component *cmpnt;
4263 list_for_each_entry(cmpnt, &component_list, list) {
4264 if (dev == cmpnt->dev)
4270 snd_soc_unregister_dais(dev, cmpnt->num_dai);
4272 mutex_lock(&client_mutex);
4273 list_del(&cmpnt->list);
4274 mutex_unlock(&client_mutex);
4276 dev_dbg(dev, "ASoC: Unregistered component '%s'\n", cmpnt->name);
4279 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4281 /* Retrieve a card's name from device tree */
4282 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4283 const char *propname)
4285 struct device_node *np = card->dev->of_node;
4288 ret = of_property_read_string_index(np, propname, 0, &card->name);
4290 * EINVAL means the property does not exist. This is fine providing
4291 * card->name was previously set, which is checked later in
4292 * snd_soc_register_card.
4294 if (ret < 0 && ret != -EINVAL) {
4296 "ASoC: Property '%s' could not be read: %d\n",
4303 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4305 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4306 const char *propname)
4308 struct device_node *np = card->dev->of_node;
4310 struct snd_soc_dapm_route *routes;
4313 num_routes = of_property_count_strings(np, propname);
4314 if (num_routes < 0 || num_routes & 1) {
4315 dev_err(card->dev, "ASoC: Property '%s' does not exist or its"
4316 " length is not even\n", propname);
4321 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4326 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4330 "ASoC: Could not allocate DAPM route table\n");
4334 for (i = 0; i < num_routes; i++) {
4335 ret = of_property_read_string_index(np, propname,
4336 2 * i, &routes[i].sink);
4339 "ASoC: Property '%s' index %d could not be read: %d\n",
4340 propname, 2 * i, ret);
4343 ret = of_property_read_string_index(np, propname,
4344 (2 * i) + 1, &routes[i].source);
4347 "ASoC: Property '%s' index %d could not be read: %d\n",
4348 propname, (2 * i) + 1, ret);
4353 card->num_dapm_routes = num_routes;
4354 card->dapm_routes = routes;
4358 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4360 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4365 unsigned int format = 0;
4371 } of_fmt_table[] = {
4372 { "i2s", SND_SOC_DAIFMT_I2S },
4373 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4374 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4375 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4376 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4377 { "ac97", SND_SOC_DAIFMT_AC97 },
4378 { "pdm", SND_SOC_DAIFMT_PDM},
4379 { "msb", SND_SOC_DAIFMT_MSB },
4380 { "lsb", SND_SOC_DAIFMT_LSB },
4387 * check "[prefix]format = xxx"
4388 * SND_SOC_DAIFMT_FORMAT_MASK area
4390 snprintf(prop, sizeof(prop), "%sformat", prefix);
4391 ret = of_property_read_string(np, prop, &str);
4393 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4394 if (strcmp(str, of_fmt_table[i].name) == 0) {
4395 format |= of_fmt_table[i].val;
4402 * check "[prefix]continuous-clock"
4403 * SND_SOC_DAIFMT_CLOCK_MASK area
4405 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4406 if (of_get_property(np, prop, NULL))
4407 format |= SND_SOC_DAIFMT_CONT;
4409 format |= SND_SOC_DAIFMT_GATED;
4412 * check "[prefix]bitclock-inversion"
4413 * check "[prefix]frame-inversion"
4414 * SND_SOC_DAIFMT_INV_MASK area
4416 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4417 bit = !!of_get_property(np, prop, NULL);
4419 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4420 frame = !!of_get_property(np, prop, NULL);
4422 switch ((bit << 4) + frame) {
4424 format |= SND_SOC_DAIFMT_IB_IF;
4427 format |= SND_SOC_DAIFMT_IB_NF;
4430 format |= SND_SOC_DAIFMT_NB_IF;
4433 /* SND_SOC_DAIFMT_NB_NF is default */
4438 * check "[prefix]bitclock-master"
4439 * check "[prefix]frame-master"
4440 * SND_SOC_DAIFMT_MASTER_MASK area
4442 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4443 bit = !!of_get_property(np, prop, NULL);
4445 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4446 frame = !!of_get_property(np, prop, NULL);
4448 switch ((bit << 4) + frame) {
4450 format |= SND_SOC_DAIFMT_CBM_CFM;
4453 format |= SND_SOC_DAIFMT_CBM_CFS;
4456 format |= SND_SOC_DAIFMT_CBS_CFM;
4459 format |= SND_SOC_DAIFMT_CBS_CFS;
4465 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4467 static int __init snd_soc_init(void)
4469 #ifdef CONFIG_DEBUG_FS
4470 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4471 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
4472 pr_warn("ASoC: Failed to create debugfs directory\n");
4473 snd_soc_debugfs_root = NULL;
4476 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4478 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4480 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4482 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
4484 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
4485 &platform_list_fops))
4486 pr_warn("ASoC: Failed to create platform list debugfs file\n");
4489 snd_soc_util_init();
4491 return platform_driver_register(&soc_driver);
4493 module_init(snd_soc_init);
4495 static void __exit snd_soc_exit(void)
4497 snd_soc_util_exit();
4499 #ifdef CONFIG_DEBUG_FS
4500 debugfs_remove_recursive(snd_soc_debugfs_root);
4502 platform_driver_unregister(&soc_driver);
4504 module_exit(snd_soc_exit);
4506 /* Module information */
4507 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4508 MODULE_DESCRIPTION("ALSA SoC Core");
4509 MODULE_LICENSE("GPL");
4510 MODULE_ALIAS("platform:soc-audio");