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 #ifdef CONFIG_SND_SOC_PM_DOWN_TIME
69 static int pmdown_time = CONFIG_SND_SOC_PM_DOWN_TIME;
71 static int pmdown_time = 5000;
73 module_param(pmdown_time, int, 0);
74 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
76 /* returns the minimum number of bytes needed to represent
77 * a particular given value */
78 static int min_bytes_needed(unsigned long val)
83 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
86 c = (sizeof val * 8) - c;
94 /* fill buf which is 'len' bytes with a formatted
95 * string of the form 'reg: value\n' */
96 static int format_register_str(struct snd_soc_codec *codec,
97 unsigned int reg, char *buf, size_t len)
99 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
100 int regsize = codec->driver->reg_word_size * 2;
102 char tmpbuf[len + 1];
103 char regbuf[regsize + 1];
105 /* since tmpbuf is allocated on the stack, warn the callers if they
106 * try to abuse this function */
109 /* +2 for ': ' and + 1 for '\n' */
110 if (wordsize + regsize + 2 + 1 != len)
113 ret = snd_soc_read(codec, reg);
115 memset(regbuf, 'X', regsize);
116 regbuf[regsize] = '\0';
118 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
121 /* prepare the buffer */
122 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
123 /* copy it back to the caller without the '\0' */
124 memcpy(buf, tmpbuf, len);
129 /* codec register dump */
130 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
131 size_t count, loff_t pos)
134 int wordsize, regsize;
139 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
140 regsize = codec->driver->reg_word_size * 2;
142 len = wordsize + regsize + 2 + 1;
144 if (!codec->driver->reg_cache_size)
147 if (codec->driver->reg_cache_step)
148 step = codec->driver->reg_cache_step;
150 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
151 if (!snd_soc_codec_readable_register(codec, i))
153 if (codec->driver->display_register) {
154 count += codec->driver->display_register(codec, buf + count,
155 PAGE_SIZE - count, i);
157 /* only support larger than PAGE_SIZE bytes debugfs
158 * entries for the default case */
160 if (total + len >= count - 1)
162 format_register_str(codec, i, buf + total, len);
169 total = min(total, count - 1);
174 static ssize_t codec_reg_show(struct device *dev,
175 struct device_attribute *attr, char *buf)
177 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
179 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
182 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
184 static ssize_t pmdown_time_show(struct device *dev,
185 struct device_attribute *attr, char *buf)
187 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
189 return sprintf(buf, "%ld\n", rtd->pmdown_time);
192 static ssize_t pmdown_time_set(struct device *dev,
193 struct device_attribute *attr,
194 const char *buf, size_t count)
196 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
199 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
206 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
208 #ifdef CONFIG_DEBUG_FS
209 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
210 size_t count, loff_t *ppos)
213 struct snd_soc_codec *codec = file->private_data;
216 if (*ppos < 0 || !count)
219 buf = kmalloc(count, GFP_KERNEL);
223 ret = soc_codec_reg_show(codec, buf, count, *ppos);
225 if (copy_to_user(user_buf, buf, ret)) {
236 static ssize_t codec_reg_write_file(struct file *file,
237 const char __user *user_buf, size_t count, loff_t *ppos)
242 unsigned long reg, value;
243 struct snd_soc_codec *codec = file->private_data;
245 buf_size = min(count, (sizeof(buf)-1));
246 if (copy_from_user(buf, user_buf, buf_size))
250 while (*start == ' ')
252 reg = simple_strtoul(start, &start, 16);
253 while (*start == ' ')
255 if (strict_strtoul(start, 16, &value))
258 /* Userspace has been fiddling around behind the kernel's back */
259 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
261 snd_soc_write(codec, reg, value);
265 static const struct file_operations codec_reg_fops = {
267 .read = codec_reg_read_file,
268 .write = codec_reg_write_file,
269 .llseek = default_llseek,
272 static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
274 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
276 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
278 if (!codec->debugfs_codec_root) {
279 dev_warn(codec->dev, "ASoC: Failed to create codec debugfs"
284 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
286 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
289 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
290 codec->debugfs_codec_root,
291 codec, &codec_reg_fops);
292 if (!codec->debugfs_reg)
293 dev_warn(codec->dev, "ASoC: Failed to create codec register"
296 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
299 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
301 debugfs_remove_recursive(codec->debugfs_codec_root);
304 static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
306 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
308 platform->debugfs_platform_root = debugfs_create_dir(platform->name,
310 if (!platform->debugfs_platform_root) {
311 dev_warn(platform->dev,
312 "ASoC: Failed to create platform debugfs directory\n");
316 snd_soc_dapm_debugfs_init(&platform->dapm,
317 platform->debugfs_platform_root);
320 static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
322 debugfs_remove_recursive(platform->debugfs_platform_root);
325 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
326 size_t count, loff_t *ppos)
328 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
329 ssize_t len, ret = 0;
330 struct snd_soc_codec *codec;
335 list_for_each_entry(codec, &codec_list, list) {
336 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
340 if (ret > PAGE_SIZE) {
347 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
354 static const struct file_operations codec_list_fops = {
355 .read = codec_list_read_file,
356 .llseek = default_llseek,/* read accesses f_pos */
359 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
360 size_t count, loff_t *ppos)
362 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
363 ssize_t len, ret = 0;
364 struct snd_soc_dai *dai;
369 list_for_each_entry(dai, &dai_list, list) {
370 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
373 if (ret > PAGE_SIZE) {
379 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
386 static const struct file_operations dai_list_fops = {
387 .read = dai_list_read_file,
388 .llseek = default_llseek,/* read accesses f_pos */
391 static ssize_t platform_list_read_file(struct file *file,
392 char __user *user_buf,
393 size_t count, loff_t *ppos)
395 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
396 ssize_t len, ret = 0;
397 struct snd_soc_platform *platform;
402 list_for_each_entry(platform, &platform_list, list) {
403 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
407 if (ret > PAGE_SIZE) {
413 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
420 static const struct file_operations platform_list_fops = {
421 .read = platform_list_read_file,
422 .llseek = default_llseek,/* read accesses f_pos */
425 static void soc_init_card_debugfs(struct snd_soc_card *card)
427 card->debugfs_card_root = debugfs_create_dir(card->name,
428 snd_soc_debugfs_root);
429 if (!card->debugfs_card_root) {
431 "ASoC: Failed to create card debugfs directory\n");
435 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
436 card->debugfs_card_root,
438 if (!card->debugfs_pop_time)
440 "ASoC: Failed to create pop time debugfs file\n");
443 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
445 debugfs_remove_recursive(card->debugfs_card_root);
450 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
454 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
458 static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
462 static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
466 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
470 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
475 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
476 const char *dai_link, int stream)
480 for (i = 0; i < card->num_links; i++) {
481 if (card->rtd[i].dai_link->no_pcm &&
482 !strcmp(card->rtd[i].dai_link->name, dai_link))
483 return card->rtd[i].pcm->streams[stream].substream;
485 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
488 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
490 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
491 const char *dai_link)
495 for (i = 0; i < card->num_links; i++) {
496 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
497 return &card->rtd[i];
499 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
502 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
504 #ifdef CONFIG_SND_SOC_AC97_BUS
505 /* unregister ac97 codec */
506 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
508 if (codec->ac97->dev.bus)
509 device_unregister(&codec->ac97->dev);
513 /* stop no dev release warning */
514 static void soc_ac97_device_release(struct device *dev){}
516 /* register ac97 codec to bus */
517 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
521 codec->ac97->dev.bus = &ac97_bus_type;
522 codec->ac97->dev.parent = codec->card->dev;
523 codec->ac97->dev.release = soc_ac97_device_release;
525 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
526 codec->card->snd_card->number, 0, codec->name);
527 err = device_register(&codec->ac97->dev);
529 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
530 codec->ac97->dev.bus = NULL;
537 #ifdef CONFIG_PM_SLEEP
538 /* powers down audio subsystem for suspend */
539 int snd_soc_suspend(struct device *dev)
541 struct snd_soc_card *card = dev_get_drvdata(dev);
542 struct snd_soc_codec *codec;
545 /* If the initialization of this soc device failed, there is no codec
546 * associated with it. Just bail out in this case.
548 if (list_empty(&card->codec_dev_list))
551 /* Due to the resume being scheduled into a workqueue we could
552 * suspend before that's finished - wait for it to complete.
554 snd_power_lock(card->snd_card);
555 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
556 snd_power_unlock(card->snd_card);
558 /* we're going to block userspace touching us until resume completes */
559 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
561 /* mute any active DACs */
562 for (i = 0; i < card->num_rtd; i++) {
563 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
564 struct snd_soc_dai_driver *drv = dai->driver;
566 if (card->rtd[i].dai_link->ignore_suspend)
569 if (drv->ops->digital_mute && dai->playback_active)
570 drv->ops->digital_mute(dai, 1);
573 /* suspend all pcms */
574 for (i = 0; i < card->num_rtd; i++) {
575 if (card->rtd[i].dai_link->ignore_suspend)
578 snd_pcm_suspend_all(card->rtd[i].pcm);
581 if (card->suspend_pre)
582 card->suspend_pre(card);
584 for (i = 0; i < card->num_rtd; i++) {
585 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
586 struct snd_soc_platform *platform = card->rtd[i].platform;
588 if (card->rtd[i].dai_link->ignore_suspend)
591 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
592 cpu_dai->driver->suspend(cpu_dai);
593 if (platform->driver->suspend && !platform->suspended) {
594 platform->driver->suspend(cpu_dai);
595 platform->suspended = 1;
599 /* close any waiting streams and save state */
600 for (i = 0; i < card->num_rtd; i++) {
601 flush_delayed_work(&card->rtd[i].delayed_work);
602 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
605 for (i = 0; i < card->num_rtd; i++) {
607 if (card->rtd[i].dai_link->ignore_suspend)
610 snd_soc_dapm_stream_event(&card->rtd[i],
611 SNDRV_PCM_STREAM_PLAYBACK,
612 SND_SOC_DAPM_STREAM_SUSPEND);
614 snd_soc_dapm_stream_event(&card->rtd[i],
615 SNDRV_PCM_STREAM_CAPTURE,
616 SND_SOC_DAPM_STREAM_SUSPEND);
619 /* Recheck all analogue paths too */
620 dapm_mark_io_dirty(&card->dapm);
621 snd_soc_dapm_sync(&card->dapm);
623 /* suspend all CODECs */
624 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
625 /* If there are paths active then the CODEC will be held with
626 * bias _ON and should not be suspended. */
627 if (!codec->suspended && codec->driver->suspend) {
628 switch (codec->dapm.bias_level) {
629 case SND_SOC_BIAS_STANDBY:
631 * If the CODEC is capable of idle
632 * bias off then being in STANDBY
633 * means it's doing something,
634 * otherwise fall through.
636 if (codec->dapm.idle_bias_off) {
638 "ASoC: idle_bias_off CODEC on"
642 case SND_SOC_BIAS_OFF:
643 codec->driver->suspend(codec);
644 codec->suspended = 1;
645 codec->cache_sync = 1;
646 if (codec->using_regmap)
647 regcache_mark_dirty(codec->control_data);
650 dev_dbg(codec->dev, "ASoC: CODEC is on"
657 for (i = 0; i < card->num_rtd; i++) {
658 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
660 if (card->rtd[i].dai_link->ignore_suspend)
663 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
664 cpu_dai->driver->suspend(cpu_dai);
667 if (card->suspend_post)
668 card->suspend_post(card);
672 EXPORT_SYMBOL_GPL(snd_soc_suspend);
674 /* deferred resume work, so resume can complete before we finished
675 * setting our codec back up, which can be very slow on I2C
677 static void soc_resume_deferred(struct work_struct *work)
679 struct snd_soc_card *card =
680 container_of(work, struct snd_soc_card, deferred_resume_work);
681 struct snd_soc_codec *codec;
684 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
685 * so userspace apps are blocked from touching us
688 dev_dbg(card->dev, "ASoC: starting resume work\n");
690 /* Bring us up into D2 so that DAPM starts enabling things */
691 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
693 if (card->resume_pre)
694 card->resume_pre(card);
696 /* resume AC97 DAIs */
697 for (i = 0; i < card->num_rtd; i++) {
698 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
700 if (card->rtd[i].dai_link->ignore_suspend)
703 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
704 cpu_dai->driver->resume(cpu_dai);
707 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
708 /* If the CODEC was idle over suspend then it will have been
709 * left with bias OFF or STANDBY and suspended so we must now
710 * resume. Otherwise the suspend was suppressed.
712 if (codec->driver->resume && codec->suspended) {
713 switch (codec->dapm.bias_level) {
714 case SND_SOC_BIAS_STANDBY:
715 case SND_SOC_BIAS_OFF:
716 codec->driver->resume(codec);
717 codec->suspended = 0;
720 dev_dbg(codec->dev, "ASoC: CODEC was on over"
727 for (i = 0; i < card->num_rtd; i++) {
729 if (card->rtd[i].dai_link->ignore_suspend)
732 snd_soc_dapm_stream_event(&card->rtd[i],
733 SNDRV_PCM_STREAM_PLAYBACK,
734 SND_SOC_DAPM_STREAM_RESUME);
736 snd_soc_dapm_stream_event(&card->rtd[i],
737 SNDRV_PCM_STREAM_CAPTURE,
738 SND_SOC_DAPM_STREAM_RESUME);
741 /* unmute any active DACs */
742 for (i = 0; i < card->num_rtd; i++) {
743 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
744 struct snd_soc_dai_driver *drv = dai->driver;
746 if (card->rtd[i].dai_link->ignore_suspend)
749 if (drv->ops->digital_mute && dai->playback_active)
750 drv->ops->digital_mute(dai, 0);
753 for (i = 0; i < card->num_rtd; i++) {
754 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
755 struct snd_soc_platform *platform = card->rtd[i].platform;
757 if (card->rtd[i].dai_link->ignore_suspend)
760 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
761 cpu_dai->driver->resume(cpu_dai);
762 if (platform->driver->resume && platform->suspended) {
763 platform->driver->resume(cpu_dai);
764 platform->suspended = 0;
768 if (card->resume_post)
769 card->resume_post(card);
771 dev_dbg(card->dev, "ASoC: resume work completed\n");
773 /* userspace can access us now we are back as we were before */
774 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
776 /* Recheck all analogue paths too */
777 dapm_mark_io_dirty(&card->dapm);
778 snd_soc_dapm_sync(&card->dapm);
781 /* powers up audio subsystem after a suspend */
782 int snd_soc_resume(struct device *dev)
784 struct snd_soc_card *card = dev_get_drvdata(dev);
785 int i, ac97_control = 0;
787 /* If the initialization of this soc device failed, there is no codec
788 * associated with it. Just bail out in this case.
790 if (list_empty(&card->codec_dev_list))
793 /* AC97 devices might have other drivers hanging off them so
794 * need to resume immediately. Other drivers don't have that
795 * problem and may take a substantial amount of time to resume
796 * due to I/O costs and anti-pop so handle them out of line.
798 for (i = 0; i < card->num_rtd; i++) {
799 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
800 ac97_control |= cpu_dai->driver->ac97_control;
803 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
804 soc_resume_deferred(&card->deferred_resume_work);
806 dev_dbg(dev, "ASoC: Scheduling resume work\n");
807 if (!schedule_work(&card->deferred_resume_work))
808 dev_err(dev, "ASoC: resume work item may be lost\n");
813 EXPORT_SYMBOL_GPL(snd_soc_resume);
815 #define snd_soc_suspend NULL
816 #define snd_soc_resume NULL
819 static const struct snd_soc_dai_ops null_dai_ops = {
822 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
824 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
825 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
826 struct snd_soc_codec *codec;
827 struct snd_soc_platform *platform;
828 struct snd_soc_dai *codec_dai, *cpu_dai;
829 const char *platform_name;
831 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
833 /* Find CPU DAI from registered DAIs*/
834 list_for_each_entry(cpu_dai, &dai_list, list) {
835 if (dai_link->cpu_of_node &&
836 (cpu_dai->dev->of_node != dai_link->cpu_of_node))
838 if (dai_link->cpu_name &&
839 strcmp(dev_name(cpu_dai->dev), dai_link->cpu_name))
841 if (dai_link->cpu_dai_name &&
842 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
845 rtd->cpu_dai = cpu_dai;
849 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
850 dai_link->cpu_dai_name);
851 return -EPROBE_DEFER;
854 /* Find CODEC from registered CODECs */
855 list_for_each_entry(codec, &codec_list, list) {
856 if (dai_link->codec_of_node) {
857 if (codec->dev->of_node != dai_link->codec_of_node)
860 if (strcmp(codec->name, dai_link->codec_name))
867 * CODEC found, so find CODEC DAI from registered DAIs from
870 list_for_each_entry(codec_dai, &dai_list, list) {
871 if (codec->dev == codec_dai->dev &&
872 !strcmp(codec_dai->name,
873 dai_link->codec_dai_name)) {
875 rtd->codec_dai = codec_dai;
879 if (!rtd->codec_dai) {
880 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
881 dai_link->codec_dai_name);
882 return -EPROBE_DEFER;
887 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
888 dai_link->codec_name);
889 return -EPROBE_DEFER;
892 /* if there's no platform we match on the empty platform */
893 platform_name = dai_link->platform_name;
894 if (!platform_name && !dai_link->platform_of_node)
895 platform_name = "snd-soc-dummy";
897 /* find one from the set of registered platforms */
898 list_for_each_entry(platform, &platform_list, list) {
899 if (dai_link->platform_of_node) {
900 if (platform->dev->of_node !=
901 dai_link->platform_of_node)
904 if (strcmp(platform->name, platform_name))
908 rtd->platform = platform;
910 if (!rtd->platform) {
911 dev_err(card->dev, "ASoC: platform %s not registered\n",
912 dai_link->platform_name);
913 return -EPROBE_DEFER;
921 static int soc_remove_platform(struct snd_soc_platform *platform)
925 if (platform->driver->remove) {
926 ret = platform->driver->remove(platform);
928 dev_err(platform->dev, "ASoC: failed to remove %d\n",
932 /* Make sure all DAPM widgets are freed */
933 snd_soc_dapm_free(&platform->dapm);
935 soc_cleanup_platform_debugfs(platform);
936 platform->probed = 0;
937 list_del(&platform->card_list);
938 module_put(platform->dev->driver->owner);
943 static void soc_remove_codec(struct snd_soc_codec *codec)
947 if (codec->driver->remove) {
948 err = codec->driver->remove(codec);
950 dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
953 /* Make sure all DAPM widgets are freed */
954 snd_soc_dapm_free(&codec->dapm);
956 soc_cleanup_codec_debugfs(codec);
958 list_del(&codec->card_list);
959 module_put(codec->dev->driver->owner);
962 static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
964 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
965 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
968 /* unregister the rtd device */
969 if (rtd->dev_registered) {
970 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
971 device_remove_file(rtd->dev, &dev_attr_codec_reg);
972 device_unregister(rtd->dev);
973 rtd->dev_registered = 0;
976 /* remove the CODEC DAI */
977 if (codec_dai && codec_dai->probed &&
978 codec_dai->driver->remove_order == order) {
979 if (codec_dai->driver->remove) {
980 err = codec_dai->driver->remove(codec_dai);
982 dev_err(codec_dai->dev,
983 "ASoC: failed to remove %s: %d\n",
984 codec_dai->name, err);
986 codec_dai->probed = 0;
987 list_del(&codec_dai->card_list);
990 /* remove the cpu_dai */
991 if (cpu_dai && cpu_dai->probed &&
992 cpu_dai->driver->remove_order == order) {
993 if (cpu_dai->driver->remove) {
994 err = cpu_dai->driver->remove(cpu_dai);
996 dev_err(cpu_dai->dev,
997 "ASoC: failed to remove %s: %d\n",
1000 cpu_dai->probed = 0;
1001 list_del(&cpu_dai->card_list);
1003 if (!cpu_dai->codec) {
1004 snd_soc_dapm_free(&cpu_dai->dapm);
1005 module_put(cpu_dai->dev->driver->owner);
1010 static void soc_remove_link_components(struct snd_soc_card *card, int num,
1013 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1014 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1015 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1016 struct snd_soc_platform *platform = rtd->platform;
1017 struct snd_soc_codec *codec;
1019 /* remove the platform */
1020 if (platform && platform->probed &&
1021 platform->driver->remove_order == order) {
1022 soc_remove_platform(platform);
1025 /* remove the CODEC-side CODEC */
1027 codec = codec_dai->codec;
1028 if (codec && codec->probed &&
1029 codec->driver->remove_order == order)
1030 soc_remove_codec(codec);
1033 /* remove any CPU-side CODEC */
1035 codec = cpu_dai->codec;
1036 if (codec && codec->probed &&
1037 codec->driver->remove_order == order)
1038 soc_remove_codec(codec);
1042 static void soc_remove_dai_links(struct snd_soc_card *card)
1046 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1048 for (dai = 0; dai < card->num_rtd; dai++)
1049 soc_remove_link_dais(card, dai, order);
1052 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1054 for (dai = 0; dai < card->num_rtd; dai++)
1055 soc_remove_link_components(card, dai, order);
1061 static void soc_set_name_prefix(struct snd_soc_card *card,
1062 struct snd_soc_codec *codec)
1066 if (card->codec_conf == NULL)
1069 for (i = 0; i < card->num_configs; i++) {
1070 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1071 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1072 codec->name_prefix = map->name_prefix;
1078 static int soc_probe_codec(struct snd_soc_card *card,
1079 struct snd_soc_codec *codec)
1082 const struct snd_soc_codec_driver *driver = codec->driver;
1083 struct snd_soc_dai *dai;
1086 codec->dapm.card = card;
1087 soc_set_name_prefix(card, codec);
1089 if (!try_module_get(codec->dev->driver->owner))
1092 soc_init_codec_debugfs(codec);
1094 if (driver->dapm_widgets)
1095 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1096 driver->num_dapm_widgets);
1098 /* Create DAPM widgets for each DAI stream */
1099 list_for_each_entry(dai, &dai_list, list) {
1100 if (dai->dev != codec->dev)
1103 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1106 codec->dapm.idle_bias_off = driver->idle_bias_off;
1108 if (driver->probe) {
1109 ret = driver->probe(codec);
1112 "ASoC: failed to probe CODEC %d\n", ret);
1115 WARN(codec->dapm.idle_bias_off &&
1116 codec->dapm.bias_level != SND_SOC_BIAS_OFF,
1117 "codec %s can not start from non-off bias"
1118 " with idle_bias_off==1\n", codec->name);
1121 /* If the driver didn't set I/O up try regmap */
1122 if (!codec->write && dev_get_regmap(codec->dev, NULL))
1123 snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
1125 if (driver->controls)
1126 snd_soc_add_codec_controls(codec, driver->controls,
1127 driver->num_controls);
1128 if (driver->dapm_routes)
1129 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1130 driver->num_dapm_routes);
1132 /* mark codec as probed and add to card codec list */
1134 list_add(&codec->card_list, &card->codec_dev_list);
1135 list_add(&codec->dapm.list, &card->dapm_list);
1140 soc_cleanup_codec_debugfs(codec);
1141 module_put(codec->dev->driver->owner);
1146 static int soc_probe_platform(struct snd_soc_card *card,
1147 struct snd_soc_platform *platform)
1150 const struct snd_soc_platform_driver *driver = platform->driver;
1151 struct snd_soc_dai *dai;
1153 platform->card = card;
1154 platform->dapm.card = card;
1156 if (!try_module_get(platform->dev->driver->owner))
1159 soc_init_platform_debugfs(platform);
1161 if (driver->dapm_widgets)
1162 snd_soc_dapm_new_controls(&platform->dapm,
1163 driver->dapm_widgets, driver->num_dapm_widgets);
1165 /* Create DAPM widgets for each DAI stream */
1166 list_for_each_entry(dai, &dai_list, list) {
1167 if (dai->dev != platform->dev)
1170 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1173 platform->dapm.idle_bias_off = 1;
1175 if (driver->probe) {
1176 ret = driver->probe(platform);
1178 dev_err(platform->dev,
1179 "ASoC: failed to probe platform %d\n", ret);
1184 if (driver->controls)
1185 snd_soc_add_platform_controls(platform, driver->controls,
1186 driver->num_controls);
1187 if (driver->dapm_routes)
1188 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1189 driver->num_dapm_routes);
1191 /* mark platform as probed and add to card platform list */
1192 platform->probed = 1;
1193 list_add(&platform->card_list, &card->platform_dev_list);
1194 list_add(&platform->dapm.list, &card->dapm_list);
1199 soc_cleanup_platform_debugfs(platform);
1200 module_put(platform->dev->driver->owner);
1205 static void rtd_release(struct device *dev)
1210 static int soc_post_component_init(struct snd_soc_card *card,
1211 struct snd_soc_codec *codec,
1212 int num, int dailess)
1214 struct snd_soc_dai_link *dai_link = NULL;
1215 struct snd_soc_aux_dev *aux_dev = NULL;
1216 struct snd_soc_pcm_runtime *rtd;
1217 const char *temp, *name;
1221 dai_link = &card->dai_link[num];
1222 rtd = &card->rtd[num];
1223 name = dai_link->name;
1225 aux_dev = &card->aux_dev[num];
1226 rtd = &card->rtd_aux[num];
1227 name = aux_dev->name;
1231 /* Make sure all DAPM widgets are instantiated */
1232 snd_soc_dapm_new_widgets(&codec->dapm);
1234 /* machine controls, routes and widgets are not prefixed */
1235 temp = codec->name_prefix;
1236 codec->name_prefix = NULL;
1238 /* do machine specific initialization */
1239 if (!dailess && dai_link->init)
1240 ret = dai_link->init(rtd);
1241 else if (dailess && aux_dev->init)
1242 ret = aux_dev->init(&codec->dapm);
1244 dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
1247 codec->name_prefix = temp;
1249 /* register the rtd device */
1252 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1255 device_initialize(rtd->dev);
1256 rtd->dev->parent = card->dev;
1257 rtd->dev->release = rtd_release;
1258 rtd->dev->init_name = name;
1259 dev_set_drvdata(rtd->dev, rtd);
1260 mutex_init(&rtd->pcm_mutex);
1261 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1262 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1263 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1264 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1265 ret = device_add(rtd->dev);
1267 /* calling put_device() here to free the rtd->dev */
1268 put_device(rtd->dev);
1270 "ASoC: failed to register runtime device: %d\n", ret);
1273 rtd->dev_registered = 1;
1275 /* add DAPM sysfs entries for this codec */
1276 ret = snd_soc_dapm_sys_add(rtd->dev);
1279 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
1281 /* add codec sysfs entries */
1282 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1285 "ASoC: failed to add codec sysfs files: %d\n", ret);
1287 #ifdef CONFIG_DEBUG_FS
1288 /* add DPCM sysfs entries */
1289 if (!dailess && !dai_link->dynamic)
1292 ret = soc_dpcm_debugfs_add(rtd);
1294 dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
1301 static int soc_probe_link_components(struct snd_soc_card *card, int num,
1304 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1305 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1306 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1307 struct snd_soc_platform *platform = rtd->platform;
1310 /* probe the CPU-side component, if it is a CODEC */
1311 if (cpu_dai->codec &&
1312 !cpu_dai->codec->probed &&
1313 cpu_dai->codec->driver->probe_order == order) {
1314 ret = soc_probe_codec(card, cpu_dai->codec);
1319 /* probe the CODEC-side component */
1320 if (!codec_dai->codec->probed &&
1321 codec_dai->codec->driver->probe_order == order) {
1322 ret = soc_probe_codec(card, codec_dai->codec);
1327 /* probe the platform */
1328 if (!platform->probed &&
1329 platform->driver->probe_order == order) {
1330 ret = soc_probe_platform(card, platform);
1338 static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1340 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1341 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1342 struct snd_soc_codec *codec = rtd->codec;
1343 struct snd_soc_platform *platform = rtd->platform;
1344 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1345 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1346 struct snd_soc_dapm_widget *play_w, *capture_w;
1349 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1350 card->name, num, order);
1352 /* config components */
1353 cpu_dai->platform = platform;
1354 codec_dai->card = card;
1355 cpu_dai->card = card;
1357 /* set default power off timeout */
1358 rtd->pmdown_time = pmdown_time;
1360 /* probe the cpu_dai */
1361 if (!cpu_dai->probed &&
1362 cpu_dai->driver->probe_order == order) {
1363 if (!cpu_dai->codec) {
1364 cpu_dai->dapm.card = card;
1365 if (!try_module_get(cpu_dai->dev->driver->owner))
1368 list_add(&cpu_dai->dapm.list, &card->dapm_list);
1369 snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1372 if (cpu_dai->driver->probe) {
1373 ret = cpu_dai->driver->probe(cpu_dai);
1375 dev_err(cpu_dai->dev,
1376 "ASoC: failed to probe CPU DAI %s: %d\n",
1377 cpu_dai->name, ret);
1378 module_put(cpu_dai->dev->driver->owner);
1382 cpu_dai->probed = 1;
1383 /* mark cpu_dai as probed and add to card dai list */
1384 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1387 /* probe the CODEC DAI */
1388 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1389 if (codec_dai->driver->probe) {
1390 ret = codec_dai->driver->probe(codec_dai);
1392 dev_err(codec_dai->dev,
1393 "ASoC: failed to probe CODEC DAI %s: %d\n",
1394 codec_dai->name, ret);
1399 /* mark codec_dai as probed and add to card dai list */
1400 codec_dai->probed = 1;
1401 list_add(&codec_dai->card_list, &card->dai_dev_list);
1404 /* complete DAI probe during last probe */
1405 if (order != SND_SOC_COMP_ORDER_LAST)
1408 ret = soc_post_component_init(card, codec, num, 0);
1412 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1414 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1417 if (cpu_dai->driver->compress_dai) {
1418 /*create compress_device"*/
1419 ret = soc_new_compress(rtd, num);
1421 dev_err(card->dev, "ASoC: can't create compress %s\n",
1422 dai_link->stream_name);
1427 if (!dai_link->params) {
1428 /* create the pcm */
1429 ret = soc_new_pcm(rtd, num);
1431 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1432 dai_link->stream_name, ret);
1436 /* link the DAI widgets */
1437 play_w = codec_dai->playback_widget;
1438 capture_w = cpu_dai->capture_widget;
1439 if (play_w && capture_w) {
1440 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1443 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1444 play_w->name, capture_w->name, ret);
1449 play_w = cpu_dai->playback_widget;
1450 capture_w = codec_dai->capture_widget;
1451 if (play_w && capture_w) {
1452 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1455 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1456 play_w->name, capture_w->name, ret);
1463 /* add platform data for AC97 devices */
1464 if (rtd->codec_dai->driver->ac97_control)
1465 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1470 #ifdef CONFIG_SND_SOC_AC97_BUS
1471 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1475 /* Only instantiate AC97 if not already done by the adaptor
1476 * for the generic AC97 subsystem.
1478 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
1480 * It is possible that the AC97 device is already registered to
1481 * the device subsystem. This happens when the device is created
1482 * via snd_ac97_mixer(). Currently only SoC codec that does so
1483 * is the generic AC97 glue but others migh emerge.
1485 * In those cases we don't try to register the device again.
1487 if (!rtd->codec->ac97_created)
1490 ret = soc_ac97_dev_register(rtd->codec);
1492 dev_err(rtd->codec->dev,
1493 "ASoC: AC97 device register failed: %d\n", ret);
1497 rtd->codec->ac97_registered = 1;
1502 static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1504 if (codec->ac97_registered) {
1505 soc_ac97_dev_unregister(codec);
1506 codec->ac97_registered = 0;
1511 static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1513 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1514 struct snd_soc_codec *codec;
1516 /* find CODEC from registered CODECs*/
1517 list_for_each_entry(codec, &codec_list, list) {
1518 if (!strcmp(codec->name, aux_dev->codec_name))
1522 dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
1524 return -EPROBE_DEFER;
1527 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1529 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1530 struct snd_soc_codec *codec;
1533 /* find CODEC from registered CODECs*/
1534 list_for_each_entry(codec, &codec_list, list) {
1535 if (!strcmp(codec->name, aux_dev->codec_name)) {
1536 if (codec->probed) {
1538 "ASoC: codec already probed");
1545 /* codec not found */
1546 dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
1547 return -EPROBE_DEFER;
1550 ret = soc_probe_codec(card, codec);
1554 ret = soc_post_component_init(card, codec, num, 1);
1560 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1562 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1563 struct snd_soc_codec *codec = rtd->codec;
1565 /* unregister the rtd device */
1566 if (rtd->dev_registered) {
1567 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1568 device_unregister(rtd->dev);
1569 rtd->dev_registered = 0;
1572 if (codec && codec->probed)
1573 soc_remove_codec(codec);
1576 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1577 enum snd_soc_compress_type compress_type)
1581 if (codec->cache_init)
1584 /* override the compress_type if necessary */
1585 if (compress_type && codec->compress_type != compress_type)
1586 codec->compress_type = compress_type;
1587 ret = snd_soc_cache_init(codec);
1589 dev_err(codec->dev, "ASoC: Failed to set cache compression"
1590 " type: %d\n", ret);
1593 codec->cache_init = 1;
1597 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1599 struct snd_soc_codec *codec;
1600 struct snd_soc_codec_conf *codec_conf;
1601 enum snd_soc_compress_type compress_type;
1602 struct snd_soc_dai_link *dai_link;
1603 int ret, i, order, dai_fmt;
1605 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1608 for (i = 0; i < card->num_links; i++) {
1609 ret = soc_bind_dai_link(card, i);
1614 /* check aux_devs too */
1615 for (i = 0; i < card->num_aux_devs; i++) {
1616 ret = soc_check_aux_dev(card, i);
1621 /* initialize the register cache for each available codec */
1622 list_for_each_entry(codec, &codec_list, list) {
1623 if (codec->cache_init)
1625 /* by default we don't override the compress_type */
1627 /* check to see if we need to override the compress_type */
1628 for (i = 0; i < card->num_configs; ++i) {
1629 codec_conf = &card->codec_conf[i];
1630 if (!strcmp(codec->name, codec_conf->dev_name)) {
1631 compress_type = codec_conf->compress_type;
1632 if (compress_type && compress_type
1633 != codec->compress_type)
1637 ret = snd_soc_init_codec_cache(codec, compress_type);
1642 /* card bind complete so register a sound card */
1643 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1644 card->owner, 0, &card->snd_card);
1646 dev_err(card->dev, "ASoC: can't create sound card for"
1647 " card %s: %d\n", card->name, ret);
1650 card->snd_card->dev = card->dev;
1652 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1653 card->dapm.dev = card->dev;
1654 card->dapm.card = card;
1655 list_add(&card->dapm.list, &card->dapm_list);
1657 #ifdef CONFIG_DEBUG_FS
1658 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1661 #ifdef CONFIG_PM_SLEEP
1662 /* deferred resume work */
1663 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1666 if (card->dapm_widgets)
1667 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1668 card->num_dapm_widgets);
1670 /* initialise the sound card only once */
1672 ret = card->probe(card);
1674 goto card_probe_error;
1677 /* probe all components used by DAI links on this card */
1678 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1680 for (i = 0; i < card->num_links; i++) {
1681 ret = soc_probe_link_components(card, i, order);
1684 "ASoC: failed to instantiate card %d\n",
1691 /* probe all DAI links on this card */
1692 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1694 for (i = 0; i < card->num_links; i++) {
1695 ret = soc_probe_link_dais(card, i, order);
1698 "ASoC: failed to instantiate card %d\n",
1705 for (i = 0; i < card->num_aux_devs; i++) {
1706 ret = soc_probe_aux_dev(card, i);
1709 "ASoC: failed to add auxiliary devices %d\n",
1711 goto probe_aux_dev_err;
1715 snd_soc_dapm_link_dai_widgets(card);
1718 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1720 if (card->dapm_routes)
1721 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1722 card->num_dapm_routes);
1724 snd_soc_dapm_new_widgets(&card->dapm);
1726 for (i = 0; i < card->num_links; i++) {
1727 dai_link = &card->dai_link[i];
1728 dai_fmt = dai_link->dai_fmt;
1731 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1733 if (ret != 0 && ret != -ENOTSUPP)
1734 dev_warn(card->rtd[i].codec_dai->dev,
1735 "ASoC: Failed to set DAI format: %d\n",
1739 /* If this is a regular CPU link there will be a platform */
1741 (dai_link->platform_name || dai_link->platform_of_node)) {
1742 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1744 if (ret != 0 && ret != -ENOTSUPP)
1745 dev_warn(card->rtd[i].cpu_dai->dev,
1746 "ASoC: Failed to set DAI format: %d\n",
1748 } else if (dai_fmt) {
1749 /* Flip the polarity for the "CPU" end */
1750 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1751 switch (dai_link->dai_fmt &
1752 SND_SOC_DAIFMT_MASTER_MASK) {
1753 case SND_SOC_DAIFMT_CBM_CFM:
1754 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1756 case SND_SOC_DAIFMT_CBM_CFS:
1757 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1759 case SND_SOC_DAIFMT_CBS_CFM:
1760 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1762 case SND_SOC_DAIFMT_CBS_CFS:
1763 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1767 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1769 if (ret != 0 && ret != -ENOTSUPP)
1770 dev_warn(card->rtd[i].cpu_dai->dev,
1771 "ASoC: Failed to set DAI format: %d\n",
1776 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1778 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1779 "%s", card->long_name ? card->long_name : card->name);
1780 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1781 "%s", card->driver_name ? card->driver_name : card->name);
1782 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1783 switch (card->snd_card->driver[i]) {
1789 if (!isalnum(card->snd_card->driver[i]))
1790 card->snd_card->driver[i] = '_';
1795 if (card->late_probe) {
1796 ret = card->late_probe(card);
1798 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
1800 goto probe_aux_dev_err;
1804 snd_soc_dapm_new_widgets(&card->dapm);
1806 if (card->fully_routed)
1807 list_for_each_entry(codec, &card->codec_dev_list, card_list)
1808 snd_soc_dapm_auto_nc_codec_pins(codec);
1810 ret = snd_card_register(card->snd_card);
1812 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1814 goto probe_aux_dev_err;
1817 #ifdef CONFIG_SND_SOC_AC97_BUS
1818 /* register any AC97 codecs */
1819 for (i = 0; i < card->num_rtd; i++) {
1820 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1822 dev_err(card->dev, "ASoC: failed to register AC97:"
1825 soc_unregister_ac97_dai_link(card->rtd[i].codec);
1826 goto probe_aux_dev_err;
1831 card->instantiated = 1;
1832 snd_soc_dapm_sync(&card->dapm);
1833 mutex_unlock(&card->mutex);
1838 for (i = 0; i < card->num_aux_devs; i++)
1839 soc_remove_aux_dev(card, i);
1842 soc_remove_dai_links(card);
1848 snd_card_free(card->snd_card);
1851 mutex_unlock(&card->mutex);
1856 /* probes a new socdev */
1857 static int soc_probe(struct platform_device *pdev)
1859 struct snd_soc_card *card = platform_get_drvdata(pdev);
1862 * no card, so machine driver should be registering card
1863 * we should not be here in that case so ret error
1868 dev_warn(&pdev->dev,
1869 "ASoC: machine %s should use snd_soc_register_card()\n",
1872 /* Bodge while we unpick instantiation */
1873 card->dev = &pdev->dev;
1875 return snd_soc_register_card(card);
1878 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1882 /* make sure any delayed work runs */
1883 for (i = 0; i < card->num_rtd; i++) {
1884 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1885 flush_delayed_work(&rtd->delayed_work);
1888 /* remove auxiliary devices */
1889 for (i = 0; i < card->num_aux_devs; i++)
1890 soc_remove_aux_dev(card, i);
1892 /* remove and free each DAI */
1893 soc_remove_dai_links(card);
1895 soc_cleanup_card_debugfs(card);
1897 /* remove the card */
1901 snd_soc_dapm_free(&card->dapm);
1903 snd_card_free(card->snd_card);
1908 /* removes a socdev */
1909 static int soc_remove(struct platform_device *pdev)
1911 struct snd_soc_card *card = platform_get_drvdata(pdev);
1913 snd_soc_unregister_card(card);
1917 int snd_soc_poweroff(struct device *dev)
1919 struct snd_soc_card *card = dev_get_drvdata(dev);
1922 if (!card->instantiated)
1925 /* Flush out pmdown_time work - we actually do want to run it
1926 * now, we're shutting down so no imminent restart. */
1927 for (i = 0; i < card->num_rtd; i++) {
1928 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1929 flush_delayed_work(&rtd->delayed_work);
1932 snd_soc_dapm_shutdown(card);
1936 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1938 const struct dev_pm_ops snd_soc_pm_ops = {
1939 .suspend = snd_soc_suspend,
1940 .resume = snd_soc_resume,
1941 .freeze = snd_soc_suspend,
1942 .thaw = snd_soc_resume,
1943 .poweroff = snd_soc_poweroff,
1944 .restore = snd_soc_resume,
1946 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1948 /* ASoC platform driver */
1949 static struct platform_driver soc_driver = {
1951 .name = "soc-audio",
1952 .owner = THIS_MODULE,
1953 .pm = &snd_soc_pm_ops,
1956 .remove = soc_remove,
1960 * snd_soc_codec_volatile_register: Report if a register is volatile.
1962 * @codec: CODEC to query.
1963 * @reg: Register to query.
1965 * Boolean function indiciating if a CODEC register is volatile.
1967 int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1970 if (codec->volatile_register)
1971 return codec->volatile_register(codec, reg);
1975 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1978 * snd_soc_codec_readable_register: Report if a register is readable.
1980 * @codec: CODEC to query.
1981 * @reg: Register to query.
1983 * Boolean function indicating if a CODEC register is readable.
1985 int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1988 if (codec->readable_register)
1989 return codec->readable_register(codec, reg);
1993 EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1996 * snd_soc_codec_writable_register: Report if a register is writable.
1998 * @codec: CODEC to query.
1999 * @reg: Register to query.
2001 * Boolean function indicating if a CODEC register is writable.
2003 int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
2006 if (codec->writable_register)
2007 return codec->writable_register(codec, reg);
2011 EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
2013 int snd_soc_platform_read(struct snd_soc_platform *platform,
2018 if (!platform->driver->read) {
2019 dev_err(platform->dev, "ASoC: platform has no read back\n");
2023 ret = platform->driver->read(platform, reg);
2024 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
2025 trace_snd_soc_preg_read(platform, reg, ret);
2029 EXPORT_SYMBOL_GPL(snd_soc_platform_read);
2031 int snd_soc_platform_write(struct snd_soc_platform *platform,
2032 unsigned int reg, unsigned int val)
2034 if (!platform->driver->write) {
2035 dev_err(platform->dev, "ASoC: platform has no write back\n");
2039 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
2040 trace_snd_soc_preg_write(platform, reg, val);
2041 return platform->driver->write(platform, reg, val);
2043 EXPORT_SYMBOL_GPL(snd_soc_platform_write);
2046 * snd_soc_new_ac97_codec - initailise AC97 device
2047 * @codec: audio codec
2048 * @ops: AC97 bus operations
2049 * @num: AC97 codec number
2051 * Initialises AC97 codec resources for use by ad-hoc devices only.
2053 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2054 struct snd_ac97_bus_ops *ops, int num)
2056 mutex_lock(&codec->mutex);
2058 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2059 if (codec->ac97 == NULL) {
2060 mutex_unlock(&codec->mutex);
2064 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2065 if (codec->ac97->bus == NULL) {
2068 mutex_unlock(&codec->mutex);
2072 codec->ac97->bus->ops = ops;
2073 codec->ac97->num = num;
2076 * Mark the AC97 device to be created by us. This way we ensure that the
2077 * device will be registered with the device subsystem later on.
2079 codec->ac97_created = 1;
2081 mutex_unlock(&codec->mutex);
2084 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2087 * snd_soc_free_ac97_codec - free AC97 codec device
2088 * @codec: audio codec
2090 * Frees AC97 codec device resources.
2092 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2094 mutex_lock(&codec->mutex);
2095 #ifdef CONFIG_SND_SOC_AC97_BUS
2096 soc_unregister_ac97_dai_link(codec);
2098 kfree(codec->ac97->bus);
2101 codec->ac97_created = 0;
2102 mutex_unlock(&codec->mutex);
2104 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2106 unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2110 ret = codec->read(codec, reg);
2111 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
2112 trace_snd_soc_reg_read(codec, reg, ret);
2116 EXPORT_SYMBOL_GPL(snd_soc_read);
2118 unsigned int snd_soc_write(struct snd_soc_codec *codec,
2119 unsigned int reg, unsigned int val)
2121 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
2122 trace_snd_soc_reg_write(codec, reg, val);
2123 return codec->write(codec, reg, val);
2125 EXPORT_SYMBOL_GPL(snd_soc_write);
2127 unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
2128 unsigned int reg, const void *data, size_t len)
2130 return codec->bulk_write_raw(codec, reg, data, len);
2132 EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
2135 * snd_soc_update_bits - update codec register bits
2136 * @codec: audio codec
2137 * @reg: codec register
2138 * @mask: register mask
2141 * Writes new register value.
2143 * Returns 1 for change, 0 for no change, or negative error code.
2145 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
2146 unsigned int mask, unsigned int value)
2149 unsigned int old, new;
2152 if (codec->using_regmap) {
2153 ret = regmap_update_bits_check(codec->control_data, reg,
2154 mask, value, &change);
2156 ret = snd_soc_read(codec, reg);
2161 new = (old & ~mask) | (value & mask);
2162 change = old != new;
2164 ret = snd_soc_write(codec, reg, new);
2172 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2175 * snd_soc_update_bits_locked - update codec register bits
2176 * @codec: audio codec
2177 * @reg: codec register
2178 * @mask: register mask
2181 * Writes new register value, and takes the codec mutex.
2183 * Returns 1 for change else 0.
2185 int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2186 unsigned short reg, unsigned int mask,
2191 mutex_lock(&codec->mutex);
2192 change = snd_soc_update_bits(codec, reg, mask, value);
2193 mutex_unlock(&codec->mutex);
2197 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
2200 * snd_soc_test_bits - test register for change
2201 * @codec: audio codec
2202 * @reg: codec register
2203 * @mask: register mask
2206 * Tests a register with a new value and checks if the new value is
2207 * different from the old value.
2209 * Returns 1 for change else 0.
2211 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
2212 unsigned int mask, unsigned int value)
2215 unsigned int old, new;
2217 old = snd_soc_read(codec, reg);
2218 new = (old & ~mask) | value;
2219 change = old != new;
2223 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2226 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2227 * @substream: the pcm substream
2228 * @hw: the hardware parameters
2230 * Sets the substream runtime hardware parameters.
2232 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2233 const struct snd_pcm_hardware *hw)
2235 struct snd_pcm_runtime *runtime = substream->runtime;
2236 runtime->hw.info = hw->info;
2237 runtime->hw.formats = hw->formats;
2238 runtime->hw.period_bytes_min = hw->period_bytes_min;
2239 runtime->hw.period_bytes_max = hw->period_bytes_max;
2240 runtime->hw.periods_min = hw->periods_min;
2241 runtime->hw.periods_max = hw->periods_max;
2242 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2243 runtime->hw.fifo_size = hw->fifo_size;
2246 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2249 * snd_soc_cnew - create new control
2250 * @_template: control template
2251 * @data: control private data
2252 * @long_name: control long name
2253 * @prefix: control name prefix
2255 * Create a new mixer control from a template control.
2257 * Returns 0 for success, else error.
2259 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2260 void *data, const char *long_name,
2263 struct snd_kcontrol_new template;
2264 struct snd_kcontrol *kcontrol;
2268 memcpy(&template, _template, sizeof(template));
2272 long_name = template.name;
2275 name_len = strlen(long_name) + strlen(prefix) + 2;
2276 name = kmalloc(name_len, GFP_KERNEL);
2280 snprintf(name, name_len, "%s %s", prefix, long_name);
2282 template.name = name;
2284 template.name = long_name;
2287 kcontrol = snd_ctl_new1(&template, data);
2293 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2295 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2296 const struct snd_kcontrol_new *controls, int num_controls,
2297 const char *prefix, void *data)
2301 for (i = 0; i < num_controls; i++) {
2302 const struct snd_kcontrol_new *control = &controls[i];
2303 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2304 control->name, prefix));
2306 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2307 control->name, err);
2316 * snd_soc_add_codec_controls - add an array of controls to a codec.
2317 * Convenience function to add a list of controls. Many codecs were
2318 * duplicating this code.
2320 * @codec: codec to add controls to
2321 * @controls: array of controls to add
2322 * @num_controls: number of elements in the array
2324 * Return 0 for success, else error.
2326 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2327 const struct snd_kcontrol_new *controls, int num_controls)
2329 struct snd_card *card = codec->card->snd_card;
2331 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2332 codec->name_prefix, codec);
2334 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2337 * snd_soc_add_platform_controls - add an array of controls to a platform.
2338 * Convenience function to add a list of controls.
2340 * @platform: platform to add controls to
2341 * @controls: array of controls to add
2342 * @num_controls: number of elements in the array
2344 * Return 0 for success, else error.
2346 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2347 const struct snd_kcontrol_new *controls, int num_controls)
2349 struct snd_card *card = platform->card->snd_card;
2351 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2354 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2357 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2358 * Convenience function to add a list of controls.
2360 * @soc_card: SoC card to add controls to
2361 * @controls: array of controls to add
2362 * @num_controls: number of elements in the array
2364 * Return 0 for success, else error.
2366 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2367 const struct snd_kcontrol_new *controls, int num_controls)
2369 struct snd_card *card = soc_card->snd_card;
2371 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2374 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2377 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2378 * Convienience function to add a list of controls.
2380 * @dai: DAI to add controls to
2381 * @controls: array of controls to add
2382 * @num_controls: number of elements in the array
2384 * Return 0 for success, else error.
2386 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2387 const struct snd_kcontrol_new *controls, int num_controls)
2389 struct snd_card *card = dai->card->snd_card;
2391 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2394 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2397 * snd_soc_info_enum_double - enumerated double mixer info callback
2398 * @kcontrol: mixer control
2399 * @uinfo: control element information
2401 * Callback to provide information about a double enumerated
2404 * Returns 0 for success.
2406 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2407 struct snd_ctl_elem_info *uinfo)
2409 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2411 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2412 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2413 uinfo->value.enumerated.items = e->max;
2415 if (uinfo->value.enumerated.item > e->max - 1)
2416 uinfo->value.enumerated.item = e->max - 1;
2417 strcpy(uinfo->value.enumerated.name,
2418 e->texts[uinfo->value.enumerated.item]);
2421 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2424 * snd_soc_get_enum_double - enumerated double mixer get callback
2425 * @kcontrol: mixer control
2426 * @ucontrol: control element information
2428 * Callback to get the value of a double enumerated mixer.
2430 * Returns 0 for success.
2432 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2433 struct snd_ctl_elem_value *ucontrol)
2435 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2436 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2439 val = snd_soc_read(codec, e->reg);
2440 ucontrol->value.enumerated.item[0]
2441 = (val >> e->shift_l) & e->mask;
2442 if (e->shift_l != e->shift_r)
2443 ucontrol->value.enumerated.item[1] =
2444 (val >> e->shift_r) & e->mask;
2448 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2451 * snd_soc_put_enum_double - enumerated double mixer put callback
2452 * @kcontrol: mixer control
2453 * @ucontrol: control element information
2455 * Callback to set the value of a double enumerated mixer.
2457 * Returns 0 for success.
2459 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2460 struct snd_ctl_elem_value *ucontrol)
2462 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2463 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2467 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2469 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2470 mask = e->mask << e->shift_l;
2471 if (e->shift_l != e->shift_r) {
2472 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2474 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2475 mask |= e->mask << e->shift_r;
2478 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2480 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2483 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2484 * @kcontrol: mixer control
2485 * @ucontrol: control element information
2487 * Callback to get the value of a double semi enumerated mixer.
2489 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2490 * used for handling bitfield coded enumeration for example.
2492 * Returns 0 for success.
2494 int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2495 struct snd_ctl_elem_value *ucontrol)
2497 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2498 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2499 unsigned int reg_val, val, mux;
2501 reg_val = snd_soc_read(codec, e->reg);
2502 val = (reg_val >> e->shift_l) & e->mask;
2503 for (mux = 0; mux < e->max; mux++) {
2504 if (val == e->values[mux])
2507 ucontrol->value.enumerated.item[0] = mux;
2508 if (e->shift_l != e->shift_r) {
2509 val = (reg_val >> e->shift_r) & e->mask;
2510 for (mux = 0; mux < e->max; mux++) {
2511 if (val == e->values[mux])
2514 ucontrol->value.enumerated.item[1] = mux;
2519 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2522 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2523 * @kcontrol: mixer control
2524 * @ucontrol: control element information
2526 * Callback to set the value of a double semi enumerated mixer.
2528 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2529 * used for handling bitfield coded enumeration for example.
2531 * Returns 0 for success.
2533 int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2534 struct snd_ctl_elem_value *ucontrol)
2536 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2537 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2541 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2543 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2544 mask = e->mask << e->shift_l;
2545 if (e->shift_l != e->shift_r) {
2546 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2548 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2549 mask |= e->mask << e->shift_r;
2552 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2554 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2557 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2558 * @kcontrol: mixer control
2559 * @uinfo: control element information
2561 * Callback to provide information about an external enumerated
2564 * Returns 0 for success.
2566 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2567 struct snd_ctl_elem_info *uinfo)
2569 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2571 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2573 uinfo->value.enumerated.items = e->max;
2575 if (uinfo->value.enumerated.item > e->max - 1)
2576 uinfo->value.enumerated.item = e->max - 1;
2577 strcpy(uinfo->value.enumerated.name,
2578 e->texts[uinfo->value.enumerated.item]);
2581 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2584 * snd_soc_info_volsw_ext - external single mixer info callback
2585 * @kcontrol: mixer control
2586 * @uinfo: control element information
2588 * Callback to provide information about a single external mixer control.
2590 * Returns 0 for success.
2592 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2593 struct snd_ctl_elem_info *uinfo)
2595 int max = kcontrol->private_value;
2597 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2598 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2600 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2603 uinfo->value.integer.min = 0;
2604 uinfo->value.integer.max = max;
2607 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2610 * snd_soc_info_volsw - single mixer info callback
2611 * @kcontrol: mixer control
2612 * @uinfo: control element information
2614 * Callback to provide information about a single mixer control, or a double
2615 * mixer control that spans 2 registers.
2617 * Returns 0 for success.
2619 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2620 struct snd_ctl_elem_info *uinfo)
2622 struct soc_mixer_control *mc =
2623 (struct soc_mixer_control *)kcontrol->private_value;
2626 if (!mc->platform_max)
2627 mc->platform_max = mc->max;
2628 platform_max = mc->platform_max;
2630 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2631 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2633 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2635 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2636 uinfo->value.integer.min = 0;
2637 uinfo->value.integer.max = platform_max;
2640 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2643 * snd_soc_get_volsw - single mixer get callback
2644 * @kcontrol: mixer control
2645 * @ucontrol: control element information
2647 * Callback to get the value of a single mixer control, or a double mixer
2648 * control that spans 2 registers.
2650 * Returns 0 for success.
2652 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2653 struct snd_ctl_elem_value *ucontrol)
2655 struct soc_mixer_control *mc =
2656 (struct soc_mixer_control *)kcontrol->private_value;
2657 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2658 unsigned int reg = mc->reg;
2659 unsigned int reg2 = mc->rreg;
2660 unsigned int shift = mc->shift;
2661 unsigned int rshift = mc->rshift;
2663 unsigned int mask = (1 << fls(max)) - 1;
2664 unsigned int invert = mc->invert;
2666 ucontrol->value.integer.value[0] =
2667 (snd_soc_read(codec, reg) >> shift) & mask;
2669 ucontrol->value.integer.value[0] =
2670 max - ucontrol->value.integer.value[0];
2672 if (snd_soc_volsw_is_stereo(mc)) {
2674 ucontrol->value.integer.value[1] =
2675 (snd_soc_read(codec, reg) >> rshift) & mask;
2677 ucontrol->value.integer.value[1] =
2678 (snd_soc_read(codec, reg2) >> shift) & mask;
2680 ucontrol->value.integer.value[1] =
2681 max - ucontrol->value.integer.value[1];
2686 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2689 * snd_soc_put_volsw - single mixer put callback
2690 * @kcontrol: mixer control
2691 * @ucontrol: control element information
2693 * Callback to set the value of a single mixer control, or a double mixer
2694 * control that spans 2 registers.
2696 * Returns 0 for success.
2698 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2699 struct snd_ctl_elem_value *ucontrol)
2701 struct soc_mixer_control *mc =
2702 (struct soc_mixer_control *)kcontrol->private_value;
2703 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2704 unsigned int reg = mc->reg;
2705 unsigned int reg2 = mc->rreg;
2706 unsigned int shift = mc->shift;
2707 unsigned int rshift = mc->rshift;
2709 unsigned int mask = (1 << fls(max)) - 1;
2710 unsigned int invert = mc->invert;
2713 unsigned int val2 = 0;
2714 unsigned int val, val_mask;
2716 val = (ucontrol->value.integer.value[0] & mask);
2719 val_mask = mask << shift;
2721 if (snd_soc_volsw_is_stereo(mc)) {
2722 val2 = (ucontrol->value.integer.value[1] & mask);
2726 val_mask |= mask << rshift;
2727 val |= val2 << rshift;
2729 val2 = val2 << shift;
2733 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2738 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2742 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2745 * snd_soc_get_volsw_sx - single mixer get callback
2746 * @kcontrol: mixer control
2747 * @ucontrol: control element information
2749 * Callback to get the value of a single mixer control, or a double mixer
2750 * control that spans 2 registers.
2752 * Returns 0 for success.
2754 int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2755 struct snd_ctl_elem_value *ucontrol)
2757 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2758 struct soc_mixer_control *mc =
2759 (struct soc_mixer_control *)kcontrol->private_value;
2761 unsigned int reg = mc->reg;
2762 unsigned int reg2 = mc->rreg;
2763 unsigned int shift = mc->shift;
2764 unsigned int rshift = mc->rshift;
2767 int mask = (1 << (fls(min + max) - 1)) - 1;
2769 ucontrol->value.integer.value[0] =
2770 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2772 if (snd_soc_volsw_is_stereo(mc))
2773 ucontrol->value.integer.value[1] =
2774 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2778 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2781 * snd_soc_put_volsw_sx - double mixer set callback
2782 * @kcontrol: mixer control
2783 * @uinfo: control element information
2785 * Callback to set the value of a double mixer control that spans 2 registers.
2787 * Returns 0 for success.
2789 int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2790 struct snd_ctl_elem_value *ucontrol)
2792 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2793 struct soc_mixer_control *mc =
2794 (struct soc_mixer_control *)kcontrol->private_value;
2796 unsigned int reg = mc->reg;
2797 unsigned int reg2 = mc->rreg;
2798 unsigned int shift = mc->shift;
2799 unsigned int rshift = mc->rshift;
2802 int mask = (1 << (fls(min + max) - 1)) - 1;
2804 unsigned short val, val_mask, val2 = 0;
2806 val_mask = mask << shift;
2807 val = (ucontrol->value.integer.value[0] + min) & mask;
2810 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2814 if (snd_soc_volsw_is_stereo(mc)) {
2815 val_mask = mask << rshift;
2816 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2817 val2 = val2 << rshift;
2819 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2824 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2827 * snd_soc_info_volsw_s8 - signed mixer info callback
2828 * @kcontrol: mixer control
2829 * @uinfo: control element information
2831 * Callback to provide information about a signed mixer control.
2833 * Returns 0 for success.
2835 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2836 struct snd_ctl_elem_info *uinfo)
2838 struct soc_mixer_control *mc =
2839 (struct soc_mixer_control *)kcontrol->private_value;
2843 if (!mc->platform_max)
2844 mc->platform_max = mc->max;
2845 platform_max = mc->platform_max;
2847 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2849 uinfo->value.integer.min = 0;
2850 uinfo->value.integer.max = platform_max - min;
2853 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2856 * snd_soc_get_volsw_s8 - signed mixer get callback
2857 * @kcontrol: mixer control
2858 * @ucontrol: control element information
2860 * Callback to get the value of a signed mixer control.
2862 * Returns 0 for success.
2864 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2865 struct snd_ctl_elem_value *ucontrol)
2867 struct soc_mixer_control *mc =
2868 (struct soc_mixer_control *)kcontrol->private_value;
2869 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2870 unsigned int reg = mc->reg;
2872 int val = snd_soc_read(codec, reg);
2874 ucontrol->value.integer.value[0] =
2875 ((signed char)(val & 0xff))-min;
2876 ucontrol->value.integer.value[1] =
2877 ((signed char)((val >> 8) & 0xff))-min;
2880 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2883 * snd_soc_put_volsw_sgn - signed mixer put callback
2884 * @kcontrol: mixer control
2885 * @ucontrol: control element information
2887 * Callback to set the value of a signed mixer control.
2889 * Returns 0 for success.
2891 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2892 struct snd_ctl_elem_value *ucontrol)
2894 struct soc_mixer_control *mc =
2895 (struct soc_mixer_control *)kcontrol->private_value;
2896 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2897 unsigned int reg = mc->reg;
2901 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2902 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2904 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2906 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2909 * snd_soc_info_volsw_range - single mixer info callback with range.
2910 * @kcontrol: mixer control
2911 * @uinfo: control element information
2913 * Callback to provide information, within a range, about a single
2916 * returns 0 for success.
2918 int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2919 struct snd_ctl_elem_info *uinfo)
2921 struct soc_mixer_control *mc =
2922 (struct soc_mixer_control *)kcontrol->private_value;
2926 if (!mc->platform_max)
2927 mc->platform_max = mc->max;
2928 platform_max = mc->platform_max;
2930 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2931 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2932 uinfo->value.integer.min = 0;
2933 uinfo->value.integer.max = platform_max - min;
2937 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2940 * snd_soc_put_volsw_range - single mixer put value callback with range.
2941 * @kcontrol: mixer control
2942 * @ucontrol: control element information
2944 * Callback to set the value, within a range, for a single mixer control.
2946 * Returns 0 for success.
2948 int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2949 struct snd_ctl_elem_value *ucontrol)
2951 struct soc_mixer_control *mc =
2952 (struct soc_mixer_control *)kcontrol->private_value;
2953 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2954 unsigned int reg = mc->reg;
2955 unsigned int rreg = mc->rreg;
2956 unsigned int shift = mc->shift;
2959 unsigned int mask = (1 << fls(max)) - 1;
2960 unsigned int invert = mc->invert;
2961 unsigned int val, val_mask;
2964 val = ((ucontrol->value.integer.value[0] + min) & mask);
2967 val_mask = mask << shift;
2970 ret = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2974 if (snd_soc_volsw_is_stereo(mc)) {
2975 val = ((ucontrol->value.integer.value[1] + min) & mask);
2978 val_mask = mask << shift;
2981 ret = snd_soc_update_bits_locked(codec, rreg, val_mask, val);
2986 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2989 * snd_soc_get_volsw_range - single mixer get callback with range
2990 * @kcontrol: mixer control
2991 * @ucontrol: control element information
2993 * Callback to get the value, within a range, of a single mixer control.
2995 * Returns 0 for success.
2997 int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2998 struct snd_ctl_elem_value *ucontrol)
3000 struct soc_mixer_control *mc =
3001 (struct soc_mixer_control *)kcontrol->private_value;
3002 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3003 unsigned int reg = mc->reg;
3004 unsigned int rreg = mc->rreg;
3005 unsigned int shift = mc->shift;
3008 unsigned int mask = (1 << fls(max)) - 1;
3009 unsigned int invert = mc->invert;
3011 ucontrol->value.integer.value[0] =
3012 (snd_soc_read(codec, reg) >> shift) & mask;
3014 ucontrol->value.integer.value[0] =
3015 max - ucontrol->value.integer.value[0];
3016 ucontrol->value.integer.value[0] =
3017 ucontrol->value.integer.value[0] - min;
3019 if (snd_soc_volsw_is_stereo(mc)) {
3020 ucontrol->value.integer.value[1] =
3021 (snd_soc_read(codec, rreg) >> shift) & mask;
3023 ucontrol->value.integer.value[1] =
3024 max - ucontrol->value.integer.value[1];
3025 ucontrol->value.integer.value[1] =
3026 ucontrol->value.integer.value[1] - min;
3031 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3034 * snd_soc_limit_volume - Set new limit to an existing volume control.
3036 * @codec: where to look for the control
3037 * @name: Name of the control
3038 * @max: new maximum limit
3040 * Return 0 for success, else error.
3042 int snd_soc_limit_volume(struct snd_soc_codec *codec,
3043 const char *name, int max)
3045 struct snd_card *card = codec->card->snd_card;
3046 struct snd_kcontrol *kctl;
3047 struct soc_mixer_control *mc;
3051 /* Sanity check for name and max */
3052 if (unlikely(!name || max <= 0))
3055 list_for_each_entry(kctl, &card->controls, list) {
3056 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3062 mc = (struct soc_mixer_control *)kctl->private_value;
3063 if (max <= mc->max) {
3064 mc->platform_max = max;
3070 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3072 int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3073 struct snd_ctl_elem_info *uinfo)
3075 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3076 struct soc_bytes *params = (void *)kcontrol->private_value;
3078 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3079 uinfo->count = params->num_regs * codec->val_bytes;
3083 EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3085 int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3086 struct snd_ctl_elem_value *ucontrol)
3088 struct soc_bytes *params = (void *)kcontrol->private_value;
3089 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3092 if (codec->using_regmap)
3093 ret = regmap_raw_read(codec->control_data, params->base,
3094 ucontrol->value.bytes.data,
3095 params->num_regs * codec->val_bytes);
3099 /* Hide any masked bytes to ensure consistent data reporting */
3100 if (ret == 0 && params->mask) {
3101 switch (codec->val_bytes) {
3103 ucontrol->value.bytes.data[0] &= ~params->mask;
3106 ((u16 *)(&ucontrol->value.bytes.data))[0]
3110 ((u32 *)(&ucontrol->value.bytes.data))[0]
3120 EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3122 int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3123 struct snd_ctl_elem_value *ucontrol)
3125 struct soc_bytes *params = (void *)kcontrol->private_value;
3126 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3131 if (!codec->using_regmap)
3134 len = params->num_regs * codec->val_bytes;
3136 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3141 * If we've got a mask then we need to preserve the register
3142 * bits. We shouldn't modify the incoming data so take a
3146 ret = regmap_read(codec->control_data, params->base, &val);
3150 val &= params->mask;
3152 switch (codec->val_bytes) {
3154 ((u8 *)data)[0] &= ~params->mask;
3155 ((u8 *)data)[0] |= val;
3158 ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
3159 ((u16 *)data)[0] |= cpu_to_be16(val);
3162 ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
3163 ((u32 *)data)[0] |= cpu_to_be32(val);
3171 ret = regmap_raw_write(codec->control_data, params->base,
3179 EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3182 * snd_soc_info_xr_sx - signed multi register info callback
3183 * @kcontrol: mreg control
3184 * @uinfo: control element information
3186 * Callback to provide information of a control that can
3187 * span multiple codec registers which together
3188 * forms a single signed value in a MSB/LSB manner.
3190 * Returns 0 for success.
3192 int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3193 struct snd_ctl_elem_info *uinfo)
3195 struct soc_mreg_control *mc =
3196 (struct soc_mreg_control *)kcontrol->private_value;
3197 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3199 uinfo->value.integer.min = mc->min;
3200 uinfo->value.integer.max = mc->max;
3204 EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3207 * snd_soc_get_xr_sx - signed multi register get callback
3208 * @kcontrol: mreg control
3209 * @ucontrol: control element information
3211 * Callback to get the value of a control that can span
3212 * multiple codec registers which together forms a single
3213 * signed value in a MSB/LSB manner. The control supports
3214 * specifying total no of bits used to allow for bitfields
3215 * across the multiple codec registers.
3217 * Returns 0 for success.
3219 int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3220 struct snd_ctl_elem_value *ucontrol)
3222 struct soc_mreg_control *mc =
3223 (struct soc_mreg_control *)kcontrol->private_value;
3224 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3225 unsigned int regbase = mc->regbase;
3226 unsigned int regcount = mc->regcount;
3227 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3228 unsigned int regwmask = (1<<regwshift)-1;
3229 unsigned int invert = mc->invert;
3230 unsigned long mask = (1UL<<mc->nbits)-1;
3234 unsigned long regval;
3237 for (i = 0; i < regcount; i++) {
3238 regval = snd_soc_read(codec, regbase+i) & regwmask;
3239 val |= regval << (regwshift*(regcount-i-1));
3242 if (min < 0 && val > max)
3246 ucontrol->value.integer.value[0] = val;
3250 EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3253 * snd_soc_put_xr_sx - signed multi register get callback
3254 * @kcontrol: mreg control
3255 * @ucontrol: control element information
3257 * Callback to set the value of a control that can span
3258 * multiple codec registers which together forms a single
3259 * signed value in a MSB/LSB manner. The control supports
3260 * specifying total no of bits used to allow for bitfields
3261 * across the multiple codec registers.
3263 * Returns 0 for success.
3265 int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3266 struct snd_ctl_elem_value *ucontrol)
3268 struct soc_mreg_control *mc =
3269 (struct soc_mreg_control *)kcontrol->private_value;
3270 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3271 unsigned int regbase = mc->regbase;
3272 unsigned int regcount = mc->regcount;
3273 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3274 unsigned int regwmask = (1<<regwshift)-1;
3275 unsigned int invert = mc->invert;
3276 unsigned long mask = (1UL<<mc->nbits)-1;
3278 long val = ucontrol->value.integer.value[0];
3279 unsigned int i, regval, regmask;
3285 for (i = 0; i < regcount; i++) {
3286 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3287 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3288 err = snd_soc_update_bits_locked(codec, regbase+i,
3296 EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3299 * snd_soc_get_strobe - strobe get callback
3300 * @kcontrol: mixer control
3301 * @ucontrol: control element information
3303 * Callback get the value of a strobe mixer control.
3305 * Returns 0 for success.
3307 int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3308 struct snd_ctl_elem_value *ucontrol)
3310 struct soc_mixer_control *mc =
3311 (struct soc_mixer_control *)kcontrol->private_value;
3312 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3313 unsigned int reg = mc->reg;
3314 unsigned int shift = mc->shift;
3315 unsigned int mask = 1 << shift;
3316 unsigned int invert = mc->invert != 0;
3317 unsigned int val = snd_soc_read(codec, reg) & mask;
3319 if (shift != 0 && val != 0)
3321 ucontrol->value.enumerated.item[0] = val ^ invert;
3325 EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3328 * snd_soc_put_strobe - strobe put callback
3329 * @kcontrol: mixer control
3330 * @ucontrol: control element information
3332 * Callback strobe a register bit to high then low (or the inverse)
3333 * in one pass of a single mixer enum control.
3335 * Returns 1 for success.
3337 int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3338 struct snd_ctl_elem_value *ucontrol)
3340 struct soc_mixer_control *mc =
3341 (struct soc_mixer_control *)kcontrol->private_value;
3342 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3343 unsigned int reg = mc->reg;
3344 unsigned int shift = mc->shift;
3345 unsigned int mask = 1 << shift;
3346 unsigned int invert = mc->invert != 0;
3347 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3348 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3349 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3352 err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3356 err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3359 EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3362 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3364 * @clk_id: DAI specific clock ID
3365 * @freq: new clock frequency in Hz
3366 * @dir: new clock direction - input/output.
3368 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3370 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3371 unsigned int freq, int dir)
3373 if (dai->driver && dai->driver->ops->set_sysclk)
3374 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3375 else if (dai->codec && dai->codec->driver->set_sysclk)
3376 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3381 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3384 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3386 * @clk_id: DAI specific clock ID
3387 * @source: Source for the clock
3388 * @freq: new clock frequency in Hz
3389 * @dir: new clock direction - input/output.
3391 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3393 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3394 int source, unsigned int freq, int dir)
3396 if (codec->driver->set_sysclk)
3397 return codec->driver->set_sysclk(codec, clk_id, source,
3402 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3405 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3407 * @div_id: DAI specific clock divider ID
3408 * @div: new clock divisor.
3410 * Configures the clock dividers. This is used to derive the best DAI bit and
3411 * frame clocks from the system or master clock. It's best to set the DAI bit
3412 * and frame clocks as low as possible to save system power.
3414 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3415 int div_id, int div)
3417 if (dai->driver && dai->driver->ops->set_clkdiv)
3418 return dai->driver->ops->set_clkdiv(dai, div_id, div);
3422 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3425 * snd_soc_dai_set_pll - configure DAI PLL.
3427 * @pll_id: DAI specific PLL ID
3428 * @source: DAI specific source for the PLL
3429 * @freq_in: PLL input clock frequency in Hz
3430 * @freq_out: requested PLL output clock frequency in Hz
3432 * Configures and enables PLL to generate output clock based on input clock.
3434 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3435 unsigned int freq_in, unsigned int freq_out)
3437 if (dai->driver && dai->driver->ops->set_pll)
3438 return dai->driver->ops->set_pll(dai, pll_id, source,
3440 else if (dai->codec && dai->codec->driver->set_pll)
3441 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3446 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3449 * snd_soc_codec_set_pll - configure codec PLL.
3451 * @pll_id: DAI specific PLL ID
3452 * @source: DAI specific source for the PLL
3453 * @freq_in: PLL input clock frequency in Hz
3454 * @freq_out: requested PLL output clock frequency in Hz
3456 * Configures and enables PLL to generate output clock based on input clock.
3458 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3459 unsigned int freq_in, unsigned int freq_out)
3461 if (codec->driver->set_pll)
3462 return codec->driver->set_pll(codec, pll_id, source,
3467 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3470 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3472 * @fmt: SND_SOC_DAIFMT_ format value.
3474 * Configures the DAI hardware format and clocking.
3476 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3478 if (dai->driver == NULL)
3480 if (dai->driver->ops->set_fmt == NULL)
3482 return dai->driver->ops->set_fmt(dai, fmt);
3484 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3487 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3489 * @tx_mask: bitmask representing active TX slots.
3490 * @rx_mask: bitmask representing active RX slots.
3491 * @slots: Number of slots in use.
3492 * @slot_width: Width in bits for each slot.
3494 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3497 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3498 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
3500 if (dai->driver && dai->driver->ops->set_tdm_slot)
3501 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3506 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3509 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3511 * @tx_num: how many TX channels
3512 * @tx_slot: pointer to an array which imply the TX slot number channel
3514 * @rx_num: how many RX channels
3515 * @rx_slot: pointer to an array which imply the RX slot number channel
3518 * configure the relationship between channel number and TDM slot number.
3520 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3521 unsigned int tx_num, unsigned int *tx_slot,
3522 unsigned int rx_num, unsigned int *rx_slot)
3524 if (dai->driver && dai->driver->ops->set_channel_map)
3525 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3530 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3533 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3535 * @tristate: tristate enable
3537 * Tristates the DAI so that others can use it.
3539 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3541 if (dai->driver && dai->driver->ops->set_tristate)
3542 return dai->driver->ops->set_tristate(dai, tristate);
3546 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3549 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3551 * @mute: mute enable
3552 * @direction: stream to mute
3554 * Mutes the DAI DAC.
3556 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3562 if (dai->driver->ops->mute_stream)
3563 return dai->driver->ops->mute_stream(dai, mute, direction);
3564 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3565 dai->driver->ops->digital_mute)
3566 return dai->driver->ops->digital_mute(dai, mute);
3570 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3573 * snd_soc_register_card - Register a card with the ASoC core
3575 * @card: Card to register
3578 int snd_soc_register_card(struct snd_soc_card *card)
3582 if (!card->name || !card->dev)
3585 for (i = 0; i < card->num_links; i++) {
3586 struct snd_soc_dai_link *link = &card->dai_link[i];
3589 * Codec must be specified by 1 of name or OF node,
3590 * not both or neither.
3592 if (!!link->codec_name == !!link->codec_of_node) {
3593 dev_err(card->dev, "ASoC: Neither/both codec"
3594 " name/of_node are set for %s\n", link->name);
3597 /* Codec DAI name must be specified */
3598 if (!link->codec_dai_name) {
3599 dev_err(card->dev, "ASoC: codec_dai_name not"
3600 " set for %s\n", link->name);
3605 * Platform may be specified by either name or OF node, but
3606 * can be left unspecified, and a dummy platform will be used.
3608 if (link->platform_name && link->platform_of_node) {
3609 dev_err(card->dev, "ASoC: Both platform name/of_node"
3610 " are set for %s\n", link->name);
3615 * CPU device may be specified by either name or OF node, but
3616 * can be left unspecified, and will be matched based on DAI
3619 if (link->cpu_name && link->cpu_of_node) {
3620 dev_err(card->dev, "ASoC: Neither/both "
3621 "cpu name/of_node are set for %s\n",link->name);
3625 * At least one of CPU DAI name or CPU device name/node must be
3628 if (!link->cpu_dai_name &&
3629 !(link->cpu_name || link->cpu_of_node)) {
3630 dev_err(card->dev, "ASoC: Neither cpu_dai_name nor "
3631 "cpu_name/of_node are set for %s\n", link->name);
3636 dev_set_drvdata(card->dev, card);
3638 snd_soc_initialize_card_lists(card);
3640 soc_init_card_debugfs(card);
3642 card->rtd = devm_kzalloc(card->dev,
3643 sizeof(struct snd_soc_pcm_runtime) *
3644 (card->num_links + card->num_aux_devs),
3646 if (card->rtd == NULL)
3649 card->rtd_aux = &card->rtd[card->num_links];
3651 for (i = 0; i < card->num_links; i++)
3652 card->rtd[i].dai_link = &card->dai_link[i];
3654 INIT_LIST_HEAD(&card->list);
3655 INIT_LIST_HEAD(&card->dapm_dirty);
3656 card->instantiated = 0;
3657 mutex_init(&card->mutex);
3658 mutex_init(&card->dapm_mutex);
3660 ret = snd_soc_instantiate_card(card);
3662 soc_cleanup_card_debugfs(card);
3666 EXPORT_SYMBOL_GPL(snd_soc_register_card);
3669 * snd_soc_unregister_card - Unregister a card with the ASoC core
3671 * @card: Card to unregister
3674 int snd_soc_unregister_card(struct snd_soc_card *card)
3676 if (card->instantiated)
3677 soc_cleanup_card_resources(card);
3678 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
3682 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3685 * Simplify DAI link configuration by removing ".-1" from device names
3686 * and sanitizing names.
3688 static char *fmt_single_name(struct device *dev, int *id)
3690 char *found, name[NAME_SIZE];
3693 if (dev_name(dev) == NULL)
3696 strlcpy(name, dev_name(dev), NAME_SIZE);
3698 /* are we a "%s.%d" name (platform and SPI components) */
3699 found = strstr(name, dev->driver->name);
3702 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3704 /* discard ID from name if ID == -1 */
3706 found[strlen(dev->driver->name)] = '\0';
3710 /* I2C component devices are named "bus-addr" */
3711 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3712 char tmp[NAME_SIZE];
3714 /* create unique ID number from I2C addr and bus */
3715 *id = ((id1 & 0xffff) << 16) + id2;
3717 /* sanitize component name for DAI link creation */
3718 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3719 strlcpy(name, tmp, NAME_SIZE);
3724 return kstrdup(name, GFP_KERNEL);
3728 * Simplify DAI link naming for single devices with multiple DAIs by removing
3729 * any ".-1" and using the DAI name (instead of device name).
3731 static inline char *fmt_multiple_name(struct device *dev,
3732 struct snd_soc_dai_driver *dai_drv)
3734 if (dai_drv->name == NULL) {
3735 dev_err(dev, "ASoC: error - multiple DAI %s registered with"
3736 " no name\n", dev_name(dev));
3740 return kstrdup(dai_drv->name, GFP_KERNEL);
3744 * snd_soc_register_dai - Register a DAI with the ASoC core
3746 * @dai: DAI to register
3748 static int snd_soc_register_dai(struct device *dev,
3749 struct snd_soc_dai_driver *dai_drv)
3751 struct snd_soc_codec *codec;
3752 struct snd_soc_dai *dai;
3754 dev_dbg(dev, "ASoC: dai register %s\n", dev_name(dev));
3756 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3760 /* create DAI component name */
3761 dai->name = fmt_single_name(dev, &dai->id);
3762 if (dai->name == NULL) {
3768 dai->driver = dai_drv;
3769 dai->dapm.dev = dev;
3770 if (!dai->driver->ops)
3771 dai->driver->ops = &null_dai_ops;
3773 mutex_lock(&client_mutex);
3775 list_for_each_entry(codec, &codec_list, list) {
3776 if (codec->dev == dev) {
3777 dev_dbg(dev, "ASoC: Mapped DAI %s to CODEC %s\n",
3778 dai->name, codec->name);
3785 dai->dapm.idle_bias_off = 1;
3787 list_add(&dai->list, &dai_list);
3789 mutex_unlock(&client_mutex);
3791 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3797 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3799 * @dai: DAI to unregister
3801 static void snd_soc_unregister_dai(struct device *dev)
3803 struct snd_soc_dai *dai;
3805 list_for_each_entry(dai, &dai_list, list) {
3806 if (dev == dai->dev)
3812 mutex_lock(&client_mutex);
3813 list_del(&dai->list);
3814 mutex_unlock(&client_mutex);
3816 dev_dbg(dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
3822 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3824 * @dai: Array of DAIs to register
3825 * @count: Number of DAIs
3827 static int snd_soc_register_dais(struct device *dev,
3828 struct snd_soc_dai_driver *dai_drv, size_t count)
3830 struct snd_soc_codec *codec;
3831 struct snd_soc_dai *dai;
3834 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
3836 for (i = 0; i < count; i++) {
3838 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3844 /* create DAI component name */
3845 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3846 if (dai->name == NULL) {
3853 dai->driver = &dai_drv[i];
3854 if (dai->driver->id)
3855 dai->id = dai->driver->id;
3858 dai->dapm.dev = dev;
3859 if (!dai->driver->ops)
3860 dai->driver->ops = &null_dai_ops;
3862 mutex_lock(&client_mutex);
3864 list_for_each_entry(codec, &codec_list, list) {
3865 if (codec->dev == dev) {
3866 dev_dbg(dev, "ASoC: Mapped DAI %s to "
3867 "CODEC %s\n", dai->name, codec->name);
3874 dai->dapm.idle_bias_off = 1;
3876 list_add(&dai->list, &dai_list);
3878 mutex_unlock(&client_mutex);
3880 dev_dbg(dai->dev, "ASoC: Registered DAI '%s'\n", dai->name);
3886 for (i--; i >= 0; i--)
3887 snd_soc_unregister_dai(dev);
3893 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3895 * @dai: Array of DAIs to unregister
3896 * @count: Number of DAIs
3898 static void snd_soc_unregister_dais(struct device *dev, size_t count)
3902 for (i = 0; i < count; i++)
3903 snd_soc_unregister_dai(dev);
3907 * snd_soc_add_platform - Add a platform to the ASoC core
3908 * @dev: The parent device for the platform
3909 * @platform: The platform to add
3910 * @platform_driver: The driver for the platform
3912 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
3913 const struct snd_soc_platform_driver *platform_drv)
3915 /* create platform component name */
3916 platform->name = fmt_single_name(dev, &platform->id);
3917 if (platform->name == NULL) {
3922 platform->dev = dev;
3923 platform->driver = platform_drv;
3924 platform->dapm.dev = dev;
3925 platform->dapm.platform = platform;
3926 platform->dapm.stream_event = platform_drv->stream_event;
3927 mutex_init(&platform->mutex);
3929 mutex_lock(&client_mutex);
3930 list_add(&platform->list, &platform_list);
3931 mutex_unlock(&client_mutex);
3933 dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
3937 EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3940 * snd_soc_register_platform - Register a platform with the ASoC core
3942 * @platform: platform to register
3944 int snd_soc_register_platform(struct device *dev,
3945 const struct snd_soc_platform_driver *platform_drv)
3947 struct snd_soc_platform *platform;
3950 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3952 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3953 if (platform == NULL)
3956 ret = snd_soc_add_platform(dev, platform, platform_drv);
3962 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3965 * snd_soc_remove_platform - Remove a platform from the ASoC core
3966 * @platform: the platform to remove
3968 void snd_soc_remove_platform(struct snd_soc_platform *platform)
3970 mutex_lock(&client_mutex);
3971 list_del(&platform->list);
3972 mutex_unlock(&client_mutex);
3974 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3976 kfree(platform->name);
3978 EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
3980 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
3982 struct snd_soc_platform *platform;
3984 list_for_each_entry(platform, &platform_list, list) {
3985 if (dev == platform->dev)
3991 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
3994 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3996 * @platform: platform to unregister
3998 void snd_soc_unregister_platform(struct device *dev)
4000 struct snd_soc_platform *platform;
4002 platform = snd_soc_lookup_platform(dev);
4006 snd_soc_remove_platform(platform);
4009 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4011 static u64 codec_format_map[] = {
4012 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4013 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4014 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4015 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4016 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4017 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4018 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4019 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4020 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4021 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4022 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4023 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4024 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4025 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4026 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4027 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4030 /* Fix up the DAI formats for endianness: codecs don't actually see
4031 * the endianness of the data but we're using the CPU format
4032 * definitions which do need to include endianness so we ensure that
4033 * codec DAIs always have both big and little endian variants set.
4035 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4039 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4040 if (stream->formats & codec_format_map[i])
4041 stream->formats |= codec_format_map[i];
4045 * snd_soc_register_codec - Register a codec with the ASoC core
4047 * @codec: codec to register
4049 int snd_soc_register_codec(struct device *dev,
4050 const struct snd_soc_codec_driver *codec_drv,
4051 struct snd_soc_dai_driver *dai_drv,
4055 struct snd_soc_codec *codec;
4058 dev_dbg(dev, "codec register %s\n", dev_name(dev));
4060 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4064 /* create CODEC component name */
4065 codec->name = fmt_single_name(dev, &codec->id);
4066 if (codec->name == NULL) {
4071 if (codec_drv->compress_type)
4072 codec->compress_type = codec_drv->compress_type;
4074 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
4076 codec->write = codec_drv->write;
4077 codec->read = codec_drv->read;
4078 codec->volatile_register = codec_drv->volatile_register;
4079 codec->readable_register = codec_drv->readable_register;
4080 codec->writable_register = codec_drv->writable_register;
4081 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
4082 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4083 codec->dapm.dev = dev;
4084 codec->dapm.codec = codec;
4085 codec->dapm.seq_notifier = codec_drv->seq_notifier;
4086 codec->dapm.stream_event = codec_drv->stream_event;
4088 codec->driver = codec_drv;
4089 codec->num_dai = num_dai;
4090 mutex_init(&codec->mutex);
4092 /* allocate CODEC register cache */
4093 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
4094 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
4095 codec->reg_size = reg_size;
4096 /* it is necessary to make a copy of the default register cache
4097 * because in the case of using a compression type that requires
4098 * the default register cache to be marked as the
4099 * kernel might have freed the array by the time we initialize
4102 if (codec_drv->reg_cache_default) {
4103 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
4104 reg_size, GFP_KERNEL);
4105 if (!codec->reg_def_copy) {
4107 goto fail_codec_name;
4112 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
4113 if (!codec->volatile_register)
4114 codec->volatile_register = snd_soc_default_volatile_register;
4115 if (!codec->readable_register)
4116 codec->readable_register = snd_soc_default_readable_register;
4117 if (!codec->writable_register)
4118 codec->writable_register = snd_soc_default_writable_register;
4121 for (i = 0; i < num_dai; i++) {
4122 fixup_codec_formats(&dai_drv[i].playback);
4123 fixup_codec_formats(&dai_drv[i].capture);
4126 mutex_lock(&client_mutex);
4127 list_add(&codec->list, &codec_list);
4128 mutex_unlock(&client_mutex);
4130 /* register any DAIs */
4131 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
4133 dev_err(codec->dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4134 goto fail_codec_name;
4137 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
4141 mutex_lock(&client_mutex);
4142 list_del(&codec->list);
4143 mutex_unlock(&client_mutex);
4150 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4153 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4155 * @codec: codec to unregister
4157 void snd_soc_unregister_codec(struct device *dev)
4159 struct snd_soc_codec *codec;
4161 list_for_each_entry(codec, &codec_list, list) {
4162 if (dev == codec->dev)
4168 snd_soc_unregister_dais(dev, codec->num_dai);
4170 mutex_lock(&client_mutex);
4171 list_del(&codec->list);
4172 mutex_unlock(&client_mutex);
4174 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
4176 snd_soc_cache_exit(codec);
4177 kfree(codec->reg_def_copy);
4181 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4185 * snd_soc_register_component - Register a component with the ASoC core
4188 int snd_soc_register_component(struct device *dev,
4189 const struct snd_soc_component_driver *cmpnt_drv,
4190 struct snd_soc_dai_driver *dai_drv,
4193 struct snd_soc_component *cmpnt;
4196 dev_dbg(dev, "component register %s\n", dev_name(dev));
4198 cmpnt = devm_kzalloc(dev, sizeof(*cmpnt), GFP_KERNEL);
4200 dev_err(dev, "ASoC: Failed to allocate memory\n");
4204 cmpnt->name = fmt_single_name(dev, &cmpnt->id);
4206 dev_err(dev, "ASoC: Failed to simplifying name\n");
4211 cmpnt->driver = cmpnt_drv;
4212 cmpnt->num_dai = num_dai;
4215 * snd_soc_register_dai() uses fmt_single_name(), and
4216 * snd_soc_register_dais() uses fmt_multiple_name()
4217 * for dai->name which is used for name based matching
4220 ret = snd_soc_register_dai(dev, dai_drv);
4222 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
4224 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4225 goto error_component_name;
4228 mutex_lock(&client_mutex);
4229 list_add(&cmpnt->list, &component_list);
4230 mutex_unlock(&client_mutex);
4232 dev_dbg(cmpnt->dev, "ASoC: Registered component '%s'\n", cmpnt->name);
4236 error_component_name:
4241 EXPORT_SYMBOL_GPL(snd_soc_register_component);
4244 * snd_soc_unregister_component - Unregister a component from the ASoC core
4247 void snd_soc_unregister_component(struct device *dev)
4249 struct snd_soc_component *cmpnt;
4251 list_for_each_entry(cmpnt, &component_list, list) {
4252 if (dev == cmpnt->dev)
4258 snd_soc_unregister_dais(dev, cmpnt->num_dai);
4260 mutex_lock(&client_mutex);
4261 list_del(&cmpnt->list);
4262 mutex_unlock(&client_mutex);
4264 dev_dbg(dev, "ASoC: Unregistered component '%s'\n", cmpnt->name);
4267 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4269 /* Retrieve a card's name from device tree */
4270 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4271 const char *propname)
4273 struct device_node *np = card->dev->of_node;
4276 ret = of_property_read_string_index(np, propname, 0, &card->name);
4278 * EINVAL means the property does not exist. This is fine providing
4279 * card->name was previously set, which is checked later in
4280 * snd_soc_register_card.
4282 if (ret < 0 && ret != -EINVAL) {
4284 "ASoC: Property '%s' could not be read: %d\n",
4291 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4293 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4294 const char *propname)
4296 struct device_node *np = card->dev->of_node;
4298 struct snd_soc_dapm_route *routes;
4301 num_routes = of_property_count_strings(np, propname);
4302 if (num_routes < 0 || num_routes & 1) {
4303 dev_err(card->dev, "ASoC: Property '%s' does not exist or its"
4304 " length is not even\n", propname);
4309 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4314 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4318 "ASoC: Could not allocate DAPM route table\n");
4322 for (i = 0; i < num_routes; i++) {
4323 ret = of_property_read_string_index(np, propname,
4324 2 * i, &routes[i].sink);
4327 "ASoC: Property '%s' index %d could not be read: %d\n",
4328 propname, 2 * i, ret);
4331 ret = of_property_read_string_index(np, propname,
4332 (2 * i) + 1, &routes[i].source);
4335 "ASoC: Property '%s' index %d could not be read: %d\n",
4336 propname, (2 * i) + 1, ret);
4341 card->num_dapm_routes = num_routes;
4342 card->dapm_routes = routes;
4346 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4348 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4353 unsigned int format = 0;
4359 } of_fmt_table[] = {
4360 { "i2s", SND_SOC_DAIFMT_I2S },
4361 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4362 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4363 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4364 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4365 { "ac97", SND_SOC_DAIFMT_AC97 },
4366 { "pdm", SND_SOC_DAIFMT_PDM},
4367 { "msb", SND_SOC_DAIFMT_MSB },
4368 { "lsb", SND_SOC_DAIFMT_LSB },
4375 * check "[prefix]format = xxx"
4376 * SND_SOC_DAIFMT_FORMAT_MASK area
4378 snprintf(prop, sizeof(prop), "%sformat", prefix);
4379 ret = of_property_read_string(np, prop, &str);
4381 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4382 if (strcmp(str, of_fmt_table[i].name) == 0) {
4383 format |= of_fmt_table[i].val;
4390 * check "[prefix]continuous-clock"
4391 * SND_SOC_DAIFMT_CLOCK_MASK area
4393 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4394 if (of_get_property(np, prop, NULL))
4395 format |= SND_SOC_DAIFMT_CONT;
4397 format |= SND_SOC_DAIFMT_GATED;
4400 * check "[prefix]bitclock-inversion"
4401 * check "[prefix]frame-inversion"
4402 * SND_SOC_DAIFMT_INV_MASK area
4404 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4405 bit = !!of_get_property(np, prop, NULL);
4407 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4408 frame = !!of_get_property(np, prop, NULL);
4410 switch ((bit << 4) + frame) {
4412 format |= SND_SOC_DAIFMT_IB_IF;
4415 format |= SND_SOC_DAIFMT_IB_NF;
4418 format |= SND_SOC_DAIFMT_NB_IF;
4421 /* SND_SOC_DAIFMT_NB_NF is default */
4426 * check "[prefix]bitclock-master"
4427 * check "[prefix]frame-master"
4428 * SND_SOC_DAIFMT_MASTER_MASK area
4430 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4431 bit = !!of_get_property(np, prop, NULL);
4433 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4434 frame = !!of_get_property(np, prop, NULL);
4436 switch ((bit << 4) + frame) {
4438 format |= SND_SOC_DAIFMT_CBM_CFM;
4441 format |= SND_SOC_DAIFMT_CBM_CFS;
4444 format |= SND_SOC_DAIFMT_CBS_CFM;
4447 format |= SND_SOC_DAIFMT_CBS_CFS;
4453 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4455 static int __init snd_soc_init(void)
4457 #ifdef CONFIG_DEBUG_FS
4458 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4459 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
4460 pr_warn("ASoC: Failed to create debugfs directory\n");
4461 snd_soc_debugfs_root = NULL;
4464 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4466 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4468 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4470 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
4472 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
4473 &platform_list_fops))
4474 pr_warn("ASoC: Failed to create platform list debugfs file\n");
4477 snd_soc_util_init();
4479 return platform_driver_register(&soc_driver);
4481 module_init(snd_soc_init);
4483 static void __exit snd_soc_exit(void)
4485 snd_soc_util_exit();
4487 #ifdef CONFIG_DEBUG_FS
4488 debugfs_remove_recursive(snd_soc_debugfs_root);
4490 platform_driver_unregister(&soc_driver);
4492 module_exit(snd_soc_exit);
4494 /* Module information */
4495 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4496 MODULE_DESCRIPTION("ALSA SoC Core");
4497 MODULE_LICENSE("GPL");
4498 MODULE_ALIAS("platform:soc-audio");