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/initval.h>
44 #define CREATE_TRACE_POINTS
45 #include <trace/events/asoc.h>
49 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
51 #ifdef CONFIG_DEBUG_FS
52 struct dentry *snd_soc_debugfs_root;
53 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
56 static DEFINE_MUTEX(client_mutex);
57 static LIST_HEAD(dai_list);
58 static LIST_HEAD(platform_list);
59 static LIST_HEAD(codec_list);
62 * This is a timeout to do a DAPM powerdown after a stream is closed().
63 * It can be used to eliminate pops between different playback streams, e.g.
64 * between two audio tracks.
66 static int pmdown_time = 5000;
67 module_param(pmdown_time, int, 0);
68 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
70 /* returns the minimum number of bytes needed to represent
71 * a particular given value */
72 static int min_bytes_needed(unsigned long val)
77 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
80 c = (sizeof val * 8) - c;
88 /* fill buf which is 'len' bytes with a formatted
89 * string of the form 'reg: value\n' */
90 static int format_register_str(struct snd_soc_codec *codec,
91 unsigned int reg, char *buf, size_t len)
93 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
94 int regsize = codec->driver->reg_word_size * 2;
97 char regbuf[regsize + 1];
99 /* since tmpbuf is allocated on the stack, warn the callers if they
100 * try to abuse this function */
103 /* +2 for ': ' and + 1 for '\n' */
104 if (wordsize + regsize + 2 + 1 != len)
107 ret = snd_soc_read(codec, reg);
109 memset(regbuf, 'X', regsize);
110 regbuf[regsize] = '\0';
112 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
115 /* prepare the buffer */
116 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
117 /* copy it back to the caller without the '\0' */
118 memcpy(buf, tmpbuf, len);
123 /* codec register dump */
124 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
125 size_t count, loff_t pos)
128 int wordsize, regsize;
133 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
134 regsize = codec->driver->reg_word_size * 2;
136 len = wordsize + regsize + 2 + 1;
138 if (!codec->driver->reg_cache_size)
141 if (codec->driver->reg_cache_step)
142 step = codec->driver->reg_cache_step;
144 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
145 if (!snd_soc_codec_readable_register(codec, i))
147 if (codec->driver->display_register) {
148 count += codec->driver->display_register(codec, buf + count,
149 PAGE_SIZE - count, i);
151 /* only support larger than PAGE_SIZE bytes debugfs
152 * entries for the default case */
154 if (total + len >= count - 1)
156 format_register_str(codec, i, buf + total, len);
163 total = min(total, count - 1);
168 static ssize_t codec_reg_show(struct device *dev,
169 struct device_attribute *attr, char *buf)
171 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
173 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
176 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
178 static ssize_t pmdown_time_show(struct device *dev,
179 struct device_attribute *attr, char *buf)
181 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
183 return sprintf(buf, "%ld\n", rtd->pmdown_time);
186 static ssize_t pmdown_time_set(struct device *dev,
187 struct device_attribute *attr,
188 const char *buf, size_t count)
190 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
193 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
200 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
202 #ifdef CONFIG_DEBUG_FS
203 static int codec_reg_open_file(struct inode *inode, struct file *file)
205 file->private_data = inode->i_private;
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);
261 snd_soc_write(codec, reg, value);
265 static const struct file_operations codec_reg_fops = {
266 .open = codec_reg_open_file,
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, "Failed to create codec debugfs directory\n");
283 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
285 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
288 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
289 codec->debugfs_codec_root,
290 codec, &codec_reg_fops);
291 if (!codec->debugfs_reg)
292 dev_warn(codec->dev, "Failed to create codec register debugfs file\n");
294 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
297 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
299 debugfs_remove_recursive(codec->debugfs_codec_root);
302 static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
304 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
306 platform->debugfs_platform_root = debugfs_create_dir(platform->name,
308 if (!platform->debugfs_platform_root) {
309 dev_warn(platform->dev,
310 "Failed to create platform debugfs directory\n");
314 snd_soc_dapm_debugfs_init(&platform->dapm,
315 platform->debugfs_platform_root);
318 static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
320 debugfs_remove_recursive(platform->debugfs_platform_root);
323 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
324 size_t count, loff_t *ppos)
326 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
327 ssize_t len, ret = 0;
328 struct snd_soc_codec *codec;
333 list_for_each_entry(codec, &codec_list, list) {
334 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
338 if (ret > PAGE_SIZE) {
345 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
352 static const struct file_operations codec_list_fops = {
353 .read = codec_list_read_file,
354 .llseek = default_llseek,/* read accesses f_pos */
357 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
358 size_t count, loff_t *ppos)
360 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
361 ssize_t len, ret = 0;
362 struct snd_soc_dai *dai;
367 list_for_each_entry(dai, &dai_list, list) {
368 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
371 if (ret > PAGE_SIZE) {
377 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
384 static const struct file_operations dai_list_fops = {
385 .read = dai_list_read_file,
386 .llseek = default_llseek,/* read accesses f_pos */
389 static ssize_t platform_list_read_file(struct file *file,
390 char __user *user_buf,
391 size_t count, loff_t *ppos)
393 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
394 ssize_t len, ret = 0;
395 struct snd_soc_platform *platform;
400 list_for_each_entry(platform, &platform_list, list) {
401 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
405 if (ret > PAGE_SIZE) {
411 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
418 static const struct file_operations platform_list_fops = {
419 .read = platform_list_read_file,
420 .llseek = default_llseek,/* read accesses f_pos */
423 static void soc_init_card_debugfs(struct snd_soc_card *card)
425 card->debugfs_card_root = debugfs_create_dir(card->name,
426 snd_soc_debugfs_root);
427 if (!card->debugfs_card_root) {
429 "ASoC: Failed to create card debugfs directory\n");
433 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
434 card->debugfs_card_root,
436 if (!card->debugfs_pop_time)
438 "Failed to create pop time debugfs file\n");
441 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
443 debugfs_remove_recursive(card->debugfs_card_root);
448 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
452 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
456 static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
460 static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
464 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
468 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
473 #ifdef CONFIG_SND_SOC_AC97_BUS
474 /* unregister ac97 codec */
475 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
477 if (codec->ac97->dev.bus)
478 device_unregister(&codec->ac97->dev);
482 /* stop no dev release warning */
483 static void soc_ac97_device_release(struct device *dev){}
485 /* register ac97 codec to bus */
486 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
490 codec->ac97->dev.bus = &ac97_bus_type;
491 codec->ac97->dev.parent = codec->card->dev;
492 codec->ac97->dev.release = soc_ac97_device_release;
494 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
495 codec->card->snd_card->number, 0, codec->name);
496 err = device_register(&codec->ac97->dev);
498 snd_printk(KERN_ERR "Can't register ac97 bus\n");
499 codec->ac97->dev.bus = NULL;
506 #ifdef CONFIG_PM_SLEEP
507 /* powers down audio subsystem for suspend */
508 int snd_soc_suspend(struct device *dev)
510 struct snd_soc_card *card = dev_get_drvdata(dev);
511 struct snd_soc_codec *codec;
514 /* If the initialization of this soc device failed, there is no codec
515 * associated with it. Just bail out in this case.
517 if (list_empty(&card->codec_dev_list))
520 /* Due to the resume being scheduled into a workqueue we could
521 * suspend before that's finished - wait for it to complete.
523 snd_power_lock(card->snd_card);
524 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
525 snd_power_unlock(card->snd_card);
527 /* we're going to block userspace touching us until resume completes */
528 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
530 /* mute any active DACs */
531 for (i = 0; i < card->num_rtd; i++) {
532 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
533 struct snd_soc_dai_driver *drv = dai->driver;
535 if (card->rtd[i].dai_link->ignore_suspend)
538 if (drv->ops->digital_mute && dai->playback_active)
539 drv->ops->digital_mute(dai, 1);
542 /* suspend all pcms */
543 for (i = 0; i < card->num_rtd; i++) {
544 if (card->rtd[i].dai_link->ignore_suspend)
547 snd_pcm_suspend_all(card->rtd[i].pcm);
550 if (card->suspend_pre)
551 card->suspend_pre(card);
553 for (i = 0; i < card->num_rtd; i++) {
554 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
555 struct snd_soc_platform *platform = card->rtd[i].platform;
557 if (card->rtd[i].dai_link->ignore_suspend)
560 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
561 cpu_dai->driver->suspend(cpu_dai);
562 if (platform->driver->suspend && !platform->suspended) {
563 platform->driver->suspend(cpu_dai);
564 platform->suspended = 1;
568 /* close any waiting streams and save state */
569 for (i = 0; i < card->num_rtd; i++) {
570 flush_delayed_work_sync(&card->rtd[i].delayed_work);
571 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
574 for (i = 0; i < card->num_rtd; i++) {
576 if (card->rtd[i].dai_link->ignore_suspend)
579 snd_soc_dapm_stream_event(&card->rtd[i],
580 SNDRV_PCM_STREAM_PLAYBACK,
581 SND_SOC_DAPM_STREAM_SUSPEND);
583 snd_soc_dapm_stream_event(&card->rtd[i],
584 SNDRV_PCM_STREAM_CAPTURE,
585 SND_SOC_DAPM_STREAM_SUSPEND);
588 /* suspend all CODECs */
589 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
590 /* If there are paths active then the CODEC will be held with
591 * bias _ON and should not be suspended. */
592 if (!codec->suspended && codec->driver->suspend) {
593 switch (codec->dapm.bias_level) {
594 case SND_SOC_BIAS_STANDBY:
596 * If the CODEC is capable of idle
597 * bias off then being in STANDBY
598 * means it's doing something,
599 * otherwise fall through.
601 if (codec->dapm.idle_bias_off) {
603 "idle_bias_off CODEC on over suspend\n");
606 case SND_SOC_BIAS_OFF:
607 codec->driver->suspend(codec);
608 codec->suspended = 1;
609 codec->cache_sync = 1;
612 dev_dbg(codec->dev, "CODEC is on over suspend\n");
618 for (i = 0; i < card->num_rtd; i++) {
619 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
621 if (card->rtd[i].dai_link->ignore_suspend)
624 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
625 cpu_dai->driver->suspend(cpu_dai);
628 if (card->suspend_post)
629 card->suspend_post(card);
633 EXPORT_SYMBOL_GPL(snd_soc_suspend);
635 /* deferred resume work, so resume can complete before we finished
636 * setting our codec back up, which can be very slow on I2C
638 static void soc_resume_deferred(struct work_struct *work)
640 struct snd_soc_card *card =
641 container_of(work, struct snd_soc_card, deferred_resume_work);
642 struct snd_soc_codec *codec;
645 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
646 * so userspace apps are blocked from touching us
649 dev_dbg(card->dev, "starting resume work\n");
651 /* Bring us up into D2 so that DAPM starts enabling things */
652 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
654 if (card->resume_pre)
655 card->resume_pre(card);
657 /* resume AC97 DAIs */
658 for (i = 0; i < card->num_rtd; i++) {
659 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
661 if (card->rtd[i].dai_link->ignore_suspend)
664 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
665 cpu_dai->driver->resume(cpu_dai);
668 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
669 /* If the CODEC was idle over suspend then it will have been
670 * left with bias OFF or STANDBY and suspended so we must now
671 * resume. Otherwise the suspend was suppressed.
673 if (codec->driver->resume && codec->suspended) {
674 switch (codec->dapm.bias_level) {
675 case SND_SOC_BIAS_STANDBY:
676 case SND_SOC_BIAS_OFF:
677 codec->driver->resume(codec);
678 codec->suspended = 0;
681 dev_dbg(codec->dev, "CODEC was on over suspend\n");
687 for (i = 0; i < card->num_rtd; i++) {
689 if (card->rtd[i].dai_link->ignore_suspend)
692 snd_soc_dapm_stream_event(&card->rtd[i],
693 SNDRV_PCM_STREAM_PLAYBACK,
694 SND_SOC_DAPM_STREAM_RESUME);
696 snd_soc_dapm_stream_event(&card->rtd[i],
697 SNDRV_PCM_STREAM_CAPTURE,
698 SND_SOC_DAPM_STREAM_RESUME);
701 /* unmute any active DACs */
702 for (i = 0; i < card->num_rtd; i++) {
703 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
704 struct snd_soc_dai_driver *drv = dai->driver;
706 if (card->rtd[i].dai_link->ignore_suspend)
709 if (drv->ops->digital_mute && dai->playback_active)
710 drv->ops->digital_mute(dai, 0);
713 for (i = 0; i < card->num_rtd; i++) {
714 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
715 struct snd_soc_platform *platform = card->rtd[i].platform;
717 if (card->rtd[i].dai_link->ignore_suspend)
720 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
721 cpu_dai->driver->resume(cpu_dai);
722 if (platform->driver->resume && platform->suspended) {
723 platform->driver->resume(cpu_dai);
724 platform->suspended = 0;
728 if (card->resume_post)
729 card->resume_post(card);
731 dev_dbg(card->dev, "resume work completed\n");
733 /* userspace can access us now we are back as we were before */
734 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
737 /* powers up audio subsystem after a suspend */
738 int snd_soc_resume(struct device *dev)
740 struct snd_soc_card *card = dev_get_drvdata(dev);
741 int i, ac97_control = 0;
743 /* If the initialization of this soc device failed, there is no codec
744 * associated with it. Just bail out in this case.
746 if (list_empty(&card->codec_dev_list))
749 /* AC97 devices might have other drivers hanging off them so
750 * need to resume immediately. Other drivers don't have that
751 * problem and may take a substantial amount of time to resume
752 * due to I/O costs and anti-pop so handle them out of line.
754 for (i = 0; i < card->num_rtd; i++) {
755 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
756 ac97_control |= cpu_dai->driver->ac97_control;
759 dev_dbg(dev, "Resuming AC97 immediately\n");
760 soc_resume_deferred(&card->deferred_resume_work);
762 dev_dbg(dev, "Scheduling resume work\n");
763 if (!schedule_work(&card->deferred_resume_work))
764 dev_err(dev, "resume work item may be lost\n");
769 EXPORT_SYMBOL_GPL(snd_soc_resume);
771 #define snd_soc_suspend NULL
772 #define snd_soc_resume NULL
775 static const struct snd_soc_dai_ops null_dai_ops = {
778 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
780 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
781 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
782 struct snd_soc_codec *codec;
783 struct snd_soc_platform *platform;
784 struct snd_soc_dai *codec_dai, *cpu_dai;
785 const char *platform_name;
787 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
789 /* Find CPU DAI from registered DAIs*/
790 list_for_each_entry(cpu_dai, &dai_list, list) {
791 if (dai_link->cpu_dai_of_node) {
792 if (cpu_dai->dev->of_node != dai_link->cpu_dai_of_node)
795 if (strcmp(cpu_dai->name, dai_link->cpu_dai_name))
799 rtd->cpu_dai = cpu_dai;
803 dev_dbg(card->dev, "CPU DAI %s not registered\n",
804 dai_link->cpu_dai_name);
805 return -EPROBE_DEFER;
808 /* Find CODEC from registered CODECs */
809 list_for_each_entry(codec, &codec_list, list) {
810 if (dai_link->codec_of_node) {
811 if (codec->dev->of_node != dai_link->codec_of_node)
814 if (strcmp(codec->name, dai_link->codec_name))
821 * CODEC found, so find CODEC DAI from registered DAIs from
824 list_for_each_entry(codec_dai, &dai_list, list) {
825 if (codec->dev == codec_dai->dev &&
826 !strcmp(codec_dai->name,
827 dai_link->codec_dai_name)) {
829 rtd->codec_dai = codec_dai;
833 if (!rtd->codec_dai) {
834 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
835 dai_link->codec_dai_name);
836 return -EPROBE_DEFER;
841 dev_dbg(card->dev, "CODEC %s not registered\n",
842 dai_link->codec_name);
843 return -EPROBE_DEFER;
846 /* if there's no platform we match on the empty platform */
847 platform_name = dai_link->platform_name;
848 if (!platform_name && !dai_link->platform_of_node)
849 platform_name = "snd-soc-dummy";
851 /* find one from the set of registered platforms */
852 list_for_each_entry(platform, &platform_list, list) {
853 if (dai_link->platform_of_node) {
854 if (platform->dev->of_node !=
855 dai_link->platform_of_node)
858 if (strcmp(platform->name, platform_name))
862 rtd->platform = platform;
864 if (!rtd->platform) {
865 dev_dbg(card->dev, "platform %s not registered\n",
866 dai_link->platform_name);
867 return -EPROBE_DEFER;
875 static void soc_remove_codec(struct snd_soc_codec *codec)
879 if (codec->driver->remove) {
880 err = codec->driver->remove(codec);
883 "asoc: failed to remove %s: %d\n",
887 /* Make sure all DAPM widgets are freed */
888 snd_soc_dapm_free(&codec->dapm);
890 soc_cleanup_codec_debugfs(codec);
892 list_del(&codec->card_list);
893 module_put(codec->dev->driver->owner);
896 static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
898 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
899 struct snd_soc_codec *codec = rtd->codec;
900 struct snd_soc_platform *platform = rtd->platform;
901 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
904 /* unregister the rtd device */
905 if (rtd->dev_registered) {
906 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
907 device_remove_file(rtd->dev, &dev_attr_codec_reg);
908 device_unregister(rtd->dev);
909 rtd->dev_registered = 0;
912 /* remove the CODEC DAI */
913 if (codec_dai && codec_dai->probed &&
914 codec_dai->driver->remove_order == order) {
915 if (codec_dai->driver->remove) {
916 err = codec_dai->driver->remove(codec_dai);
918 pr_err("asoc: failed to remove %s: %d\n",
919 codec_dai->name, err);
921 codec_dai->probed = 0;
922 list_del(&codec_dai->card_list);
925 /* remove the platform */
926 if (platform && platform->probed &&
927 platform->driver->remove_order == order) {
928 if (platform->driver->remove) {
929 err = platform->driver->remove(platform);
931 pr_err("asoc: failed to remove %s: %d\n",
932 platform->name, err);
935 /* Make sure all DAPM widgets are freed */
936 snd_soc_dapm_free(&platform->dapm);
938 soc_cleanup_platform_debugfs(platform);
939 platform->probed = 0;
940 list_del(&platform->card_list);
941 module_put(platform->dev->driver->owner);
944 /* remove the CODEC */
945 if (codec && codec->probed &&
946 codec->driver->remove_order == order)
947 soc_remove_codec(codec);
949 /* remove the cpu_dai */
950 if (cpu_dai && cpu_dai->probed &&
951 cpu_dai->driver->remove_order == order) {
952 if (cpu_dai->driver->remove) {
953 err = cpu_dai->driver->remove(cpu_dai);
955 pr_err("asoc: failed to remove %s: %d\n",
959 list_del(&cpu_dai->card_list);
960 module_put(cpu_dai->dev->driver->owner);
964 static void soc_remove_dai_links(struct snd_soc_card *card)
968 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
970 for (dai = 0; dai < card->num_rtd; dai++)
971 soc_remove_dai_link(card, dai, order);
976 static void soc_set_name_prefix(struct snd_soc_card *card,
977 struct snd_soc_codec *codec)
981 if (card->codec_conf == NULL)
984 for (i = 0; i < card->num_configs; i++) {
985 struct snd_soc_codec_conf *map = &card->codec_conf[i];
986 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
987 codec->name_prefix = map->name_prefix;
993 static int soc_probe_codec(struct snd_soc_card *card,
994 struct snd_soc_codec *codec)
997 const struct snd_soc_codec_driver *driver = codec->driver;
998 struct snd_soc_dai *dai;
1001 codec->dapm.card = card;
1002 soc_set_name_prefix(card, codec);
1004 if (!try_module_get(codec->dev->driver->owner))
1007 soc_init_codec_debugfs(codec);
1009 if (driver->dapm_widgets)
1010 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1011 driver->num_dapm_widgets);
1013 /* Create DAPM widgets for each DAI stream */
1014 list_for_each_entry(dai, &dai_list, list) {
1015 if (dai->dev != codec->dev)
1018 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1021 codec->dapm.idle_bias_off = driver->idle_bias_off;
1023 if (driver->probe) {
1024 ret = driver->probe(codec);
1027 "asoc: failed to probe CODEC %s: %d\n",
1033 if (driver->controls)
1034 snd_soc_add_codec_controls(codec, driver->controls,
1035 driver->num_controls);
1036 if (driver->dapm_routes)
1037 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1038 driver->num_dapm_routes);
1040 /* mark codec as probed and add to card codec list */
1042 list_add(&codec->card_list, &card->codec_dev_list);
1043 list_add(&codec->dapm.list, &card->dapm_list);
1048 soc_cleanup_codec_debugfs(codec);
1049 module_put(codec->dev->driver->owner);
1054 static int soc_probe_platform(struct snd_soc_card *card,
1055 struct snd_soc_platform *platform)
1058 const struct snd_soc_platform_driver *driver = platform->driver;
1059 struct snd_soc_dai *dai;
1061 platform->card = card;
1062 platform->dapm.card = card;
1064 if (!try_module_get(platform->dev->driver->owner))
1067 soc_init_platform_debugfs(platform);
1069 if (driver->dapm_widgets)
1070 snd_soc_dapm_new_controls(&platform->dapm,
1071 driver->dapm_widgets, driver->num_dapm_widgets);
1073 /* Create DAPM widgets for each DAI stream */
1074 list_for_each_entry(dai, &dai_list, list) {
1075 if (dai->dev != platform->dev)
1078 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1081 if (driver->probe) {
1082 ret = driver->probe(platform);
1084 dev_err(platform->dev,
1085 "asoc: failed to probe platform %s: %d\n",
1086 platform->name, ret);
1091 if (driver->controls)
1092 snd_soc_add_platform_controls(platform, driver->controls,
1093 driver->num_controls);
1094 if (driver->dapm_routes)
1095 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1096 driver->num_dapm_routes);
1098 /* mark platform as probed and add to card platform list */
1099 platform->probed = 1;
1100 list_add(&platform->card_list, &card->platform_dev_list);
1101 list_add(&platform->dapm.list, &card->dapm_list);
1106 soc_cleanup_platform_debugfs(platform);
1107 module_put(platform->dev->driver->owner);
1112 static void rtd_release(struct device *dev)
1117 static int soc_post_component_init(struct snd_soc_card *card,
1118 struct snd_soc_codec *codec,
1119 int num, int dailess)
1121 struct snd_soc_dai_link *dai_link = NULL;
1122 struct snd_soc_aux_dev *aux_dev = NULL;
1123 struct snd_soc_pcm_runtime *rtd;
1124 const char *temp, *name;
1128 dai_link = &card->dai_link[num];
1129 rtd = &card->rtd[num];
1130 name = dai_link->name;
1132 aux_dev = &card->aux_dev[num];
1133 rtd = &card->rtd_aux[num];
1134 name = aux_dev->name;
1138 /* Make sure all DAPM widgets are instantiated */
1139 snd_soc_dapm_new_widgets(&codec->dapm);
1141 /* machine controls, routes and widgets are not prefixed */
1142 temp = codec->name_prefix;
1143 codec->name_prefix = NULL;
1145 /* do machine specific initialization */
1146 if (!dailess && dai_link->init)
1147 ret = dai_link->init(rtd);
1148 else if (dailess && aux_dev->init)
1149 ret = aux_dev->init(&codec->dapm);
1151 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1154 codec->name_prefix = temp;
1156 /* register the rtd device */
1159 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1162 device_initialize(rtd->dev);
1163 rtd->dev->parent = card->dev;
1164 rtd->dev->release = rtd_release;
1165 rtd->dev->init_name = name;
1166 dev_set_drvdata(rtd->dev, rtd);
1167 mutex_init(&rtd->pcm_mutex);
1168 ret = device_add(rtd->dev);
1171 "asoc: failed to register runtime device: %d\n", ret);
1174 rtd->dev_registered = 1;
1176 /* add DAPM sysfs entries for this codec */
1177 ret = snd_soc_dapm_sys_add(rtd->dev);
1180 "asoc: failed to add codec dapm sysfs entries: %d\n",
1183 /* add codec sysfs entries */
1184 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1187 "asoc: failed to add codec sysfs files: %d\n", ret);
1192 static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
1194 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1195 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1196 struct snd_soc_codec *codec = rtd->codec;
1197 struct snd_soc_platform *platform = rtd->platform;
1198 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1201 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1202 card->name, num, order);
1204 /* config components */
1205 codec_dai->codec = codec;
1206 cpu_dai->platform = platform;
1207 codec_dai->card = card;
1208 cpu_dai->card = card;
1210 /* set default power off timeout */
1211 rtd->pmdown_time = pmdown_time;
1213 /* probe the cpu_dai */
1214 if (!cpu_dai->probed &&
1215 cpu_dai->driver->probe_order == order) {
1216 cpu_dai->dapm.card = card;
1217 if (!try_module_get(cpu_dai->dev->driver->owner))
1220 snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1222 if (cpu_dai->driver->probe) {
1223 ret = cpu_dai->driver->probe(cpu_dai);
1225 pr_err("asoc: failed to probe CPU DAI %s: %d\n",
1226 cpu_dai->name, ret);
1227 module_put(cpu_dai->dev->driver->owner);
1231 cpu_dai->probed = 1;
1232 /* mark cpu_dai as probed and add to card dai list */
1233 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1236 /* probe the CODEC */
1237 if (!codec->probed &&
1238 codec->driver->probe_order == order) {
1239 ret = soc_probe_codec(card, codec);
1244 /* probe the platform */
1245 if (!platform->probed &&
1246 platform->driver->probe_order == order) {
1247 ret = soc_probe_platform(card, platform);
1252 /* probe the CODEC DAI */
1253 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1254 if (codec_dai->driver->probe) {
1255 ret = codec_dai->driver->probe(codec_dai);
1257 pr_err("asoc: failed to probe CODEC DAI %s: %d\n",
1258 codec_dai->name, ret);
1263 /* mark codec_dai as probed and add to card dai list */
1264 codec_dai->probed = 1;
1265 list_add(&codec_dai->card_list, &card->dai_dev_list);
1268 /* complete DAI probe during last probe */
1269 if (order != SND_SOC_COMP_ORDER_LAST)
1272 ret = soc_post_component_init(card, codec, num, 0);
1276 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1278 pr_warn("asoc: failed to add pmdown_time sysfs:%d\n", ret);
1280 /* create the pcm */
1281 ret = soc_new_pcm(rtd, num);
1283 pr_err("asoc: can't create pcm %s :%d\n",
1284 dai_link->stream_name, ret);
1288 /* add platform data for AC97 devices */
1289 if (rtd->codec_dai->driver->ac97_control)
1290 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1295 #ifdef CONFIG_SND_SOC_AC97_BUS
1296 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1300 /* Only instantiate AC97 if not already done by the adaptor
1301 * for the generic AC97 subsystem.
1303 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
1305 * It is possible that the AC97 device is already registered to
1306 * the device subsystem. This happens when the device is created
1307 * via snd_ac97_mixer(). Currently only SoC codec that does so
1308 * is the generic AC97 glue but others migh emerge.
1310 * In those cases we don't try to register the device again.
1312 if (!rtd->codec->ac97_created)
1315 ret = soc_ac97_dev_register(rtd->codec);
1317 pr_err("asoc: AC97 device register failed:%d\n", ret);
1321 rtd->codec->ac97_registered = 1;
1326 static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1328 if (codec->ac97_registered) {
1329 soc_ac97_dev_unregister(codec);
1330 codec->ac97_registered = 0;
1335 static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1337 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1338 struct snd_soc_codec *codec;
1340 /* find CODEC from registered CODECs*/
1341 list_for_each_entry(codec, &codec_list, list) {
1342 if (!strcmp(codec->name, aux_dev->codec_name))
1346 return -EPROBE_DEFER;
1349 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1351 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1352 struct snd_soc_codec *codec;
1355 /* find CODEC from registered CODECs*/
1356 list_for_each_entry(codec, &codec_list, list) {
1357 if (!strcmp(codec->name, aux_dev->codec_name)) {
1358 if (codec->probed) {
1360 "asoc: codec already probed");
1367 /* codec not found */
1368 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1369 return -EPROBE_DEFER;
1372 ret = soc_probe_codec(card, codec);
1376 ret = soc_post_component_init(card, codec, num, 1);
1382 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1384 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1385 struct snd_soc_codec *codec = rtd->codec;
1387 /* unregister the rtd device */
1388 if (rtd->dev_registered) {
1389 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1390 device_del(rtd->dev);
1391 rtd->dev_registered = 0;
1394 if (codec && codec->probed)
1395 soc_remove_codec(codec);
1398 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1399 enum snd_soc_compress_type compress_type)
1403 if (codec->cache_init)
1406 /* override the compress_type if necessary */
1407 if (compress_type && codec->compress_type != compress_type)
1408 codec->compress_type = compress_type;
1409 ret = snd_soc_cache_init(codec);
1411 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1415 codec->cache_init = 1;
1419 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1421 struct snd_soc_codec *codec;
1422 struct snd_soc_codec_conf *codec_conf;
1423 enum snd_soc_compress_type compress_type;
1424 struct snd_soc_dai_link *dai_link;
1427 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1430 for (i = 0; i < card->num_links; i++) {
1431 ret = soc_bind_dai_link(card, i);
1436 /* check aux_devs too */
1437 for (i = 0; i < card->num_aux_devs; i++) {
1438 ret = soc_check_aux_dev(card, i);
1443 /* initialize the register cache for each available codec */
1444 list_for_each_entry(codec, &codec_list, list) {
1445 if (codec->cache_init)
1447 /* by default we don't override the compress_type */
1449 /* check to see if we need to override the compress_type */
1450 for (i = 0; i < card->num_configs; ++i) {
1451 codec_conf = &card->codec_conf[i];
1452 if (!strcmp(codec->name, codec_conf->dev_name)) {
1453 compress_type = codec_conf->compress_type;
1454 if (compress_type && compress_type
1455 != codec->compress_type)
1459 ret = snd_soc_init_codec_cache(codec, compress_type);
1464 /* card bind complete so register a sound card */
1465 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1466 card->owner, 0, &card->snd_card);
1468 pr_err("asoc: can't create sound card for card %s: %d\n",
1472 card->snd_card->dev = card->dev;
1474 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1475 card->dapm.dev = card->dev;
1476 card->dapm.card = card;
1477 list_add(&card->dapm.list, &card->dapm_list);
1479 #ifdef CONFIG_DEBUG_FS
1480 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1483 #ifdef CONFIG_PM_SLEEP
1484 /* deferred resume work */
1485 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1488 if (card->dapm_widgets)
1489 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1490 card->num_dapm_widgets);
1492 /* initialise the sound card only once */
1494 ret = card->probe(card);
1496 goto card_probe_error;
1499 /* early DAI link probe */
1500 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1502 for (i = 0; i < card->num_links; i++) {
1503 ret = soc_probe_dai_link(card, i, order);
1505 pr_err("asoc: failed to instantiate card %s: %d\n",
1512 for (i = 0; i < card->num_aux_devs; i++) {
1513 ret = soc_probe_aux_dev(card, i);
1515 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1517 goto probe_aux_dev_err;
1521 snd_soc_dapm_link_dai_widgets(card);
1524 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1526 if (card->dapm_routes)
1527 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1528 card->num_dapm_routes);
1530 snd_soc_dapm_new_widgets(&card->dapm);
1532 for (i = 0; i < card->num_links; i++) {
1533 dai_link = &card->dai_link[i];
1535 if (dai_link->dai_fmt) {
1536 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1538 if (ret != 0 && ret != -ENOTSUPP)
1539 dev_warn(card->rtd[i].codec_dai->dev,
1540 "Failed to set DAI format: %d\n",
1543 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1545 if (ret != 0 && ret != -ENOTSUPP)
1546 dev_warn(card->rtd[i].cpu_dai->dev,
1547 "Failed to set DAI format: %d\n",
1552 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1554 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1555 "%s", card->long_name ? card->long_name : card->name);
1556 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1557 "%s", card->driver_name ? card->driver_name : card->name);
1558 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1559 switch (card->snd_card->driver[i]) {
1565 if (!isalnum(card->snd_card->driver[i]))
1566 card->snd_card->driver[i] = '_';
1571 if (card->late_probe) {
1572 ret = card->late_probe(card);
1574 dev_err(card->dev, "%s late_probe() failed: %d\n",
1576 goto probe_aux_dev_err;
1580 snd_soc_dapm_new_widgets(&card->dapm);
1582 if (card->fully_routed)
1583 list_for_each_entry(codec, &card->codec_dev_list, card_list)
1584 snd_soc_dapm_auto_nc_codec_pins(codec);
1586 ret = snd_card_register(card->snd_card);
1588 pr_err("asoc: failed to register soundcard for %s: %d\n",
1590 goto probe_aux_dev_err;
1593 #ifdef CONFIG_SND_SOC_AC97_BUS
1594 /* register any AC97 codecs */
1595 for (i = 0; i < card->num_rtd; i++) {
1596 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1598 pr_err("asoc: failed to register AC97 %s: %d\n",
1601 soc_unregister_ac97_dai_link(card->rtd[i].codec);
1602 goto probe_aux_dev_err;
1607 card->instantiated = 1;
1608 snd_soc_dapm_sync(&card->dapm);
1609 mutex_unlock(&card->mutex);
1614 for (i = 0; i < card->num_aux_devs; i++)
1615 soc_remove_aux_dev(card, i);
1618 soc_remove_dai_links(card);
1624 snd_card_free(card->snd_card);
1627 mutex_unlock(&card->mutex);
1632 /* probes a new socdev */
1633 static int soc_probe(struct platform_device *pdev)
1635 struct snd_soc_card *card = platform_get_drvdata(pdev);
1639 * no card, so machine driver should be registering card
1640 * we should not be here in that case so ret error
1645 dev_warn(&pdev->dev,
1646 "ASoC machine %s should use snd_soc_register_card()\n",
1649 /* Bodge while we unpick instantiation */
1650 card->dev = &pdev->dev;
1652 ret = snd_soc_register_card(card);
1654 dev_err(&pdev->dev, "Failed to register card\n");
1661 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1665 /* make sure any delayed work runs */
1666 for (i = 0; i < card->num_rtd; i++) {
1667 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1668 flush_delayed_work_sync(&rtd->delayed_work);
1671 /* remove auxiliary devices */
1672 for (i = 0; i < card->num_aux_devs; i++)
1673 soc_remove_aux_dev(card, i);
1675 /* remove and free each DAI */
1676 soc_remove_dai_links(card);
1678 soc_cleanup_card_debugfs(card);
1680 /* remove the card */
1684 snd_soc_dapm_free(&card->dapm);
1686 snd_card_free(card->snd_card);
1691 /* removes a socdev */
1692 static int soc_remove(struct platform_device *pdev)
1694 struct snd_soc_card *card = platform_get_drvdata(pdev);
1696 snd_soc_unregister_card(card);
1700 int snd_soc_poweroff(struct device *dev)
1702 struct snd_soc_card *card = dev_get_drvdata(dev);
1705 if (!card->instantiated)
1708 /* Flush out pmdown_time work - we actually do want to run it
1709 * now, we're shutting down so no imminent restart. */
1710 for (i = 0; i < card->num_rtd; i++) {
1711 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1712 flush_delayed_work_sync(&rtd->delayed_work);
1715 snd_soc_dapm_shutdown(card);
1719 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1721 const struct dev_pm_ops snd_soc_pm_ops = {
1722 .suspend = snd_soc_suspend,
1723 .resume = snd_soc_resume,
1724 .freeze = snd_soc_suspend,
1725 .thaw = snd_soc_resume,
1726 .poweroff = snd_soc_poweroff,
1727 .restore = snd_soc_resume,
1729 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1731 /* ASoC platform driver */
1732 static struct platform_driver soc_driver = {
1734 .name = "soc-audio",
1735 .owner = THIS_MODULE,
1736 .pm = &snd_soc_pm_ops,
1739 .remove = soc_remove,
1743 * snd_soc_codec_volatile_register: Report if a register is volatile.
1745 * @codec: CODEC to query.
1746 * @reg: Register to query.
1748 * Boolean function indiciating if a CODEC register is volatile.
1750 int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1753 if (codec->volatile_register)
1754 return codec->volatile_register(codec, reg);
1758 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1761 * snd_soc_codec_readable_register: Report if a register is readable.
1763 * @codec: CODEC to query.
1764 * @reg: Register to query.
1766 * Boolean function indicating if a CODEC register is readable.
1768 int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1771 if (codec->readable_register)
1772 return codec->readable_register(codec, reg);
1776 EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1779 * snd_soc_codec_writable_register: Report if a register is writable.
1781 * @codec: CODEC to query.
1782 * @reg: Register to query.
1784 * Boolean function indicating if a CODEC register is writable.
1786 int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1789 if (codec->writable_register)
1790 return codec->writable_register(codec, reg);
1794 EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1796 int snd_soc_platform_read(struct snd_soc_platform *platform,
1801 if (!platform->driver->read) {
1802 dev_err(platform->dev, "platform has no read back\n");
1806 ret = platform->driver->read(platform, reg);
1807 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
1808 trace_snd_soc_preg_read(platform, reg, ret);
1812 EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1814 int snd_soc_platform_write(struct snd_soc_platform *platform,
1815 unsigned int reg, unsigned int val)
1817 if (!platform->driver->write) {
1818 dev_err(platform->dev, "platform has no write back\n");
1822 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
1823 trace_snd_soc_preg_write(platform, reg, val);
1824 return platform->driver->write(platform, reg, val);
1826 EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1829 * snd_soc_new_ac97_codec - initailise AC97 device
1830 * @codec: audio codec
1831 * @ops: AC97 bus operations
1832 * @num: AC97 codec number
1834 * Initialises AC97 codec resources for use by ad-hoc devices only.
1836 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1837 struct snd_ac97_bus_ops *ops, int num)
1839 mutex_lock(&codec->mutex);
1841 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1842 if (codec->ac97 == NULL) {
1843 mutex_unlock(&codec->mutex);
1847 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1848 if (codec->ac97->bus == NULL) {
1851 mutex_unlock(&codec->mutex);
1855 codec->ac97->bus->ops = ops;
1856 codec->ac97->num = num;
1859 * Mark the AC97 device to be created by us. This way we ensure that the
1860 * device will be registered with the device subsystem later on.
1862 codec->ac97_created = 1;
1864 mutex_unlock(&codec->mutex);
1867 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1870 * snd_soc_free_ac97_codec - free AC97 codec device
1871 * @codec: audio codec
1873 * Frees AC97 codec device resources.
1875 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1877 mutex_lock(&codec->mutex);
1878 #ifdef CONFIG_SND_SOC_AC97_BUS
1879 soc_unregister_ac97_dai_link(codec);
1881 kfree(codec->ac97->bus);
1884 codec->ac97_created = 0;
1885 mutex_unlock(&codec->mutex);
1887 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1889 unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1893 ret = codec->read(codec, reg);
1894 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
1895 trace_snd_soc_reg_read(codec, reg, ret);
1899 EXPORT_SYMBOL_GPL(snd_soc_read);
1901 unsigned int snd_soc_write(struct snd_soc_codec *codec,
1902 unsigned int reg, unsigned int val)
1904 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
1905 trace_snd_soc_reg_write(codec, reg, val);
1906 return codec->write(codec, reg, val);
1908 EXPORT_SYMBOL_GPL(snd_soc_write);
1910 unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
1911 unsigned int reg, const void *data, size_t len)
1913 return codec->bulk_write_raw(codec, reg, data, len);
1915 EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
1918 * snd_soc_update_bits - update codec register bits
1919 * @codec: audio codec
1920 * @reg: codec register
1921 * @mask: register mask
1924 * Writes new register value.
1926 * Returns 1 for change, 0 for no change, or negative error code.
1928 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1929 unsigned int mask, unsigned int value)
1932 unsigned int old, new;
1935 if (codec->using_regmap) {
1936 ret = regmap_update_bits_check(codec->control_data, reg,
1937 mask, value, &change);
1939 ret = snd_soc_read(codec, reg);
1944 new = (old & ~mask) | (value & mask);
1945 change = old != new;
1947 ret = snd_soc_write(codec, reg, new);
1955 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1958 * snd_soc_update_bits_locked - update codec register bits
1959 * @codec: audio codec
1960 * @reg: codec register
1961 * @mask: register mask
1964 * Writes new register value, and takes the codec mutex.
1966 * Returns 1 for change else 0.
1968 int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1969 unsigned short reg, unsigned int mask,
1974 mutex_lock(&codec->mutex);
1975 change = snd_soc_update_bits(codec, reg, mask, value);
1976 mutex_unlock(&codec->mutex);
1980 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
1983 * snd_soc_test_bits - test register for change
1984 * @codec: audio codec
1985 * @reg: codec register
1986 * @mask: register mask
1989 * Tests a register with a new value and checks if the new value is
1990 * different from the old value.
1992 * Returns 1 for change else 0.
1994 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1995 unsigned int mask, unsigned int value)
1998 unsigned int old, new;
2000 old = snd_soc_read(codec, reg);
2001 new = (old & ~mask) | value;
2002 change = old != new;
2006 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2009 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2010 * @substream: the pcm substream
2011 * @hw: the hardware parameters
2013 * Sets the substream runtime hardware parameters.
2015 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2016 const struct snd_pcm_hardware *hw)
2018 struct snd_pcm_runtime *runtime = substream->runtime;
2019 runtime->hw.info = hw->info;
2020 runtime->hw.formats = hw->formats;
2021 runtime->hw.period_bytes_min = hw->period_bytes_min;
2022 runtime->hw.period_bytes_max = hw->period_bytes_max;
2023 runtime->hw.periods_min = hw->periods_min;
2024 runtime->hw.periods_max = hw->periods_max;
2025 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2026 runtime->hw.fifo_size = hw->fifo_size;
2029 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2032 * snd_soc_cnew - create new control
2033 * @_template: control template
2034 * @data: control private data
2035 * @long_name: control long name
2036 * @prefix: control name prefix
2038 * Create a new mixer control from a template control.
2040 * Returns 0 for success, else error.
2042 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2043 void *data, const char *long_name,
2046 struct snd_kcontrol_new template;
2047 struct snd_kcontrol *kcontrol;
2051 memcpy(&template, _template, sizeof(template));
2055 long_name = template.name;
2058 name_len = strlen(long_name) + strlen(prefix) + 2;
2059 name = kmalloc(name_len, GFP_KERNEL);
2063 snprintf(name, name_len, "%s %s", prefix, long_name);
2065 template.name = name;
2067 template.name = long_name;
2070 kcontrol = snd_ctl_new1(&template, data);
2076 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2078 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2079 const struct snd_kcontrol_new *controls, int num_controls,
2080 const char *prefix, void *data)
2084 for (i = 0; i < num_controls; i++) {
2085 const struct snd_kcontrol_new *control = &controls[i];
2086 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2087 control->name, prefix));
2089 dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2098 * snd_soc_add_codec_controls - add an array of controls to a codec.
2099 * Convenience function to add a list of controls. Many codecs were
2100 * duplicating this code.
2102 * @codec: codec to add controls to
2103 * @controls: array of controls to add
2104 * @num_controls: number of elements in the array
2106 * Return 0 for success, else error.
2108 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2109 const struct snd_kcontrol_new *controls, int num_controls)
2111 struct snd_card *card = codec->card->snd_card;
2113 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2114 codec->name_prefix, codec);
2116 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2119 * snd_soc_add_platform_controls - add an array of controls to a platform.
2120 * Convenience function to add a list of controls.
2122 * @platform: platform to add controls to
2123 * @controls: array of controls to add
2124 * @num_controls: number of elements in the array
2126 * Return 0 for success, else error.
2128 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2129 const struct snd_kcontrol_new *controls, int num_controls)
2131 struct snd_card *card = platform->card->snd_card;
2133 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2136 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2139 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2140 * Convenience function to add a list of controls.
2142 * @soc_card: SoC card to add controls to
2143 * @controls: array of controls to add
2144 * @num_controls: number of elements in the array
2146 * Return 0 for success, else error.
2148 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2149 const struct snd_kcontrol_new *controls, int num_controls)
2151 struct snd_card *card = soc_card->snd_card;
2153 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2156 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2159 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2160 * Convienience function to add a list of controls.
2162 * @dai: DAI to add controls to
2163 * @controls: array of controls to add
2164 * @num_controls: number of elements in the array
2166 * Return 0 for success, else error.
2168 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2169 const struct snd_kcontrol_new *controls, int num_controls)
2171 struct snd_card *card = dai->card->snd_card;
2173 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2176 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2179 * snd_soc_info_enum_double - enumerated double mixer info callback
2180 * @kcontrol: mixer control
2181 * @uinfo: control element information
2183 * Callback to provide information about a double enumerated
2186 * Returns 0 for success.
2188 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2189 struct snd_ctl_elem_info *uinfo)
2191 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2193 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2194 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2195 uinfo->value.enumerated.items = e->max;
2197 if (uinfo->value.enumerated.item > e->max - 1)
2198 uinfo->value.enumerated.item = e->max - 1;
2199 strcpy(uinfo->value.enumerated.name,
2200 e->texts[uinfo->value.enumerated.item]);
2203 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2206 * snd_soc_get_enum_double - enumerated double mixer get callback
2207 * @kcontrol: mixer control
2208 * @ucontrol: control element information
2210 * Callback to get the value of a double enumerated mixer.
2212 * Returns 0 for success.
2214 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2215 struct snd_ctl_elem_value *ucontrol)
2217 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2218 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2219 unsigned int val, bitmask;
2221 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2223 val = snd_soc_read(codec, e->reg);
2224 ucontrol->value.enumerated.item[0]
2225 = (val >> e->shift_l) & (bitmask - 1);
2226 if (e->shift_l != e->shift_r)
2227 ucontrol->value.enumerated.item[1] =
2228 (val >> e->shift_r) & (bitmask - 1);
2232 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2235 * snd_soc_put_enum_double - enumerated double mixer put callback
2236 * @kcontrol: mixer control
2237 * @ucontrol: control element information
2239 * Callback to set the value of a double enumerated mixer.
2241 * Returns 0 for success.
2243 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2244 struct snd_ctl_elem_value *ucontrol)
2246 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2247 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2249 unsigned int mask, bitmask;
2251 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2253 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2255 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2256 mask = (bitmask - 1) << e->shift_l;
2257 if (e->shift_l != e->shift_r) {
2258 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2260 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2261 mask |= (bitmask - 1) << e->shift_r;
2264 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2266 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2269 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2270 * @kcontrol: mixer control
2271 * @ucontrol: control element information
2273 * Callback to get the value of a double semi enumerated mixer.
2275 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2276 * used for handling bitfield coded enumeration for example.
2278 * Returns 0 for success.
2280 int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2281 struct snd_ctl_elem_value *ucontrol)
2283 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2284 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2285 unsigned int reg_val, val, mux;
2287 reg_val = snd_soc_read(codec, e->reg);
2288 val = (reg_val >> e->shift_l) & e->mask;
2289 for (mux = 0; mux < e->max; mux++) {
2290 if (val == e->values[mux])
2293 ucontrol->value.enumerated.item[0] = mux;
2294 if (e->shift_l != e->shift_r) {
2295 val = (reg_val >> e->shift_r) & e->mask;
2296 for (mux = 0; mux < e->max; mux++) {
2297 if (val == e->values[mux])
2300 ucontrol->value.enumerated.item[1] = mux;
2305 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2308 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2309 * @kcontrol: mixer control
2310 * @ucontrol: control element information
2312 * Callback to set the value of a double semi enumerated mixer.
2314 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2315 * used for handling bitfield coded enumeration for example.
2317 * Returns 0 for success.
2319 int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2320 struct snd_ctl_elem_value *ucontrol)
2322 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2323 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2327 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2329 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2330 mask = e->mask << e->shift_l;
2331 if (e->shift_l != e->shift_r) {
2332 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2334 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2335 mask |= e->mask << e->shift_r;
2338 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2340 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2343 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2344 * @kcontrol: mixer control
2345 * @uinfo: control element information
2347 * Callback to provide information about an external enumerated
2350 * Returns 0 for success.
2352 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2353 struct snd_ctl_elem_info *uinfo)
2355 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2357 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2359 uinfo->value.enumerated.items = e->max;
2361 if (uinfo->value.enumerated.item > e->max - 1)
2362 uinfo->value.enumerated.item = e->max - 1;
2363 strcpy(uinfo->value.enumerated.name,
2364 e->texts[uinfo->value.enumerated.item]);
2367 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2370 * snd_soc_info_volsw_ext - external single mixer info callback
2371 * @kcontrol: mixer control
2372 * @uinfo: control element information
2374 * Callback to provide information about a single external mixer control.
2376 * Returns 0 for success.
2378 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2379 struct snd_ctl_elem_info *uinfo)
2381 int max = kcontrol->private_value;
2383 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2384 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2386 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2389 uinfo->value.integer.min = 0;
2390 uinfo->value.integer.max = max;
2393 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2396 * snd_soc_info_volsw - single mixer info callback
2397 * @kcontrol: mixer control
2398 * @uinfo: control element information
2400 * Callback to provide information about a single mixer control, or a double
2401 * mixer control that spans 2 registers.
2403 * Returns 0 for success.
2405 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2406 struct snd_ctl_elem_info *uinfo)
2408 struct soc_mixer_control *mc =
2409 (struct soc_mixer_control *)kcontrol->private_value;
2412 if (!mc->platform_max)
2413 mc->platform_max = mc->max;
2414 platform_max = mc->platform_max;
2416 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2417 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2419 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2421 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2422 uinfo->value.integer.min = 0;
2423 uinfo->value.integer.max = platform_max;
2426 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2429 * snd_soc_get_volsw - single mixer get callback
2430 * @kcontrol: mixer control
2431 * @ucontrol: control element information
2433 * Callback to get the value of a single mixer control, or a double mixer
2434 * control that spans 2 registers.
2436 * Returns 0 for success.
2438 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2439 struct snd_ctl_elem_value *ucontrol)
2441 struct soc_mixer_control *mc =
2442 (struct soc_mixer_control *)kcontrol->private_value;
2443 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2444 unsigned int reg = mc->reg;
2445 unsigned int reg2 = mc->rreg;
2446 unsigned int shift = mc->shift;
2447 unsigned int rshift = mc->rshift;
2449 unsigned int mask = (1 << fls(max)) - 1;
2450 unsigned int invert = mc->invert;
2452 ucontrol->value.integer.value[0] =
2453 (snd_soc_read(codec, reg) >> shift) & mask;
2455 ucontrol->value.integer.value[0] =
2456 max - ucontrol->value.integer.value[0];
2458 if (snd_soc_volsw_is_stereo(mc)) {
2460 ucontrol->value.integer.value[1] =
2461 (snd_soc_read(codec, reg) >> rshift) & mask;
2463 ucontrol->value.integer.value[1] =
2464 (snd_soc_read(codec, reg2) >> shift) & mask;
2466 ucontrol->value.integer.value[1] =
2467 max - ucontrol->value.integer.value[1];
2472 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2475 * snd_soc_put_volsw - single mixer put callback
2476 * @kcontrol: mixer control
2477 * @ucontrol: control element information
2479 * Callback to set the value of a single mixer control, or a double mixer
2480 * control that spans 2 registers.
2482 * Returns 0 for success.
2484 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2485 struct snd_ctl_elem_value *ucontrol)
2487 struct soc_mixer_control *mc =
2488 (struct soc_mixer_control *)kcontrol->private_value;
2489 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2490 unsigned int reg = mc->reg;
2491 unsigned int reg2 = mc->rreg;
2492 unsigned int shift = mc->shift;
2493 unsigned int rshift = mc->rshift;
2495 unsigned int mask = (1 << fls(max)) - 1;
2496 unsigned int invert = mc->invert;
2499 unsigned int val2 = 0;
2500 unsigned int val, val_mask;
2502 val = (ucontrol->value.integer.value[0] & mask);
2505 val_mask = mask << shift;
2507 if (snd_soc_volsw_is_stereo(mc)) {
2508 val2 = (ucontrol->value.integer.value[1] & mask);
2512 val_mask |= mask << rshift;
2513 val |= val2 << rshift;
2515 val2 = val2 << shift;
2519 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2524 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2528 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2531 * snd_soc_info_volsw_s8 - signed mixer info callback
2532 * @kcontrol: mixer control
2533 * @uinfo: control element information
2535 * Callback to provide information about a signed mixer control.
2537 * Returns 0 for success.
2539 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2540 struct snd_ctl_elem_info *uinfo)
2542 struct soc_mixer_control *mc =
2543 (struct soc_mixer_control *)kcontrol->private_value;
2547 if (!mc->platform_max)
2548 mc->platform_max = mc->max;
2549 platform_max = mc->platform_max;
2551 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2553 uinfo->value.integer.min = 0;
2554 uinfo->value.integer.max = platform_max - min;
2557 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2560 * snd_soc_get_volsw_s8 - signed mixer get callback
2561 * @kcontrol: mixer control
2562 * @ucontrol: control element information
2564 * Callback to get the value of a signed mixer control.
2566 * Returns 0 for success.
2568 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2569 struct snd_ctl_elem_value *ucontrol)
2571 struct soc_mixer_control *mc =
2572 (struct soc_mixer_control *)kcontrol->private_value;
2573 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2574 unsigned int reg = mc->reg;
2576 int val = snd_soc_read(codec, reg);
2578 ucontrol->value.integer.value[0] =
2579 ((signed char)(val & 0xff))-min;
2580 ucontrol->value.integer.value[1] =
2581 ((signed char)((val >> 8) & 0xff))-min;
2584 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2587 * snd_soc_put_volsw_sgn - signed mixer put callback
2588 * @kcontrol: mixer control
2589 * @ucontrol: control element information
2591 * Callback to set the value of a signed mixer control.
2593 * Returns 0 for success.
2595 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2596 struct snd_ctl_elem_value *ucontrol)
2598 struct soc_mixer_control *mc =
2599 (struct soc_mixer_control *)kcontrol->private_value;
2600 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2601 unsigned int reg = mc->reg;
2605 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2606 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2608 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2610 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2613 * snd_soc_limit_volume - Set new limit to an existing volume control.
2615 * @codec: where to look for the control
2616 * @name: Name of the control
2617 * @max: new maximum limit
2619 * Return 0 for success, else error.
2621 int snd_soc_limit_volume(struct snd_soc_codec *codec,
2622 const char *name, int max)
2624 struct snd_card *card = codec->card->snd_card;
2625 struct snd_kcontrol *kctl;
2626 struct soc_mixer_control *mc;
2630 /* Sanity check for name and max */
2631 if (unlikely(!name || max <= 0))
2634 list_for_each_entry(kctl, &card->controls, list) {
2635 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2641 mc = (struct soc_mixer_control *)kctl->private_value;
2642 if (max <= mc->max) {
2643 mc->platform_max = max;
2649 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2652 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2653 * mixer info callback
2654 * @kcontrol: mixer control
2655 * @uinfo: control element information
2657 * Returns 0 for success.
2659 int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2660 struct snd_ctl_elem_info *uinfo)
2662 struct soc_mixer_control *mc =
2663 (struct soc_mixer_control *)kcontrol->private_value;
2667 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2669 uinfo->value.integer.min = 0;
2670 uinfo->value.integer.max = max-min;
2674 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2677 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2678 * mixer get callback
2679 * @kcontrol: mixer control
2680 * @uinfo: control element information
2682 * Returns 0 for success.
2684 int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2685 struct snd_ctl_elem_value *ucontrol)
2687 struct soc_mixer_control *mc =
2688 (struct soc_mixer_control *)kcontrol->private_value;
2689 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2690 unsigned int mask = (1<<mc->shift)-1;
2692 int val = snd_soc_read(codec, mc->reg) & mask;
2693 int valr = snd_soc_read(codec, mc->rreg) & mask;
2695 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2696 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
2699 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2702 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2703 * mixer put callback
2704 * @kcontrol: mixer control
2705 * @uinfo: control element information
2707 * Returns 0 for success.
2709 int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2710 struct snd_ctl_elem_value *ucontrol)
2712 struct soc_mixer_control *mc =
2713 (struct soc_mixer_control *)kcontrol->private_value;
2714 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2715 unsigned int mask = (1<<mc->shift)-1;
2718 unsigned int val, valr, oval, ovalr;
2720 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2722 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2725 oval = snd_soc_read(codec, mc->reg) & mask;
2726 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2730 ret = snd_soc_write(codec, mc->reg, val);
2734 if (ovalr != valr) {
2735 ret = snd_soc_write(codec, mc->rreg, valr);
2742 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2744 int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
2745 struct snd_ctl_elem_info *uinfo)
2747 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2748 struct soc_bytes *params = (void *)kcontrol->private_value;
2750 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
2751 uinfo->count = params->num_regs * codec->val_bytes;
2755 EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
2757 int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
2758 struct snd_ctl_elem_value *ucontrol)
2760 struct soc_bytes *params = (void *)kcontrol->private_value;
2761 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2764 if (codec->using_regmap)
2765 ret = regmap_raw_read(codec->control_data, params->base,
2766 ucontrol->value.bytes.data,
2767 params->num_regs * codec->val_bytes);
2771 /* Hide any masked bytes to ensure consistent data reporting */
2772 if (ret == 0 && params->mask) {
2773 switch (codec->val_bytes) {
2775 ucontrol->value.bytes.data[0] &= ~params->mask;
2778 ((u16 *)(&ucontrol->value.bytes.data))[0]
2782 ((u32 *)(&ucontrol->value.bytes.data))[0]
2792 EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
2794 int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
2795 struct snd_ctl_elem_value *ucontrol)
2797 struct soc_bytes *params = (void *)kcontrol->private_value;
2798 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2803 if (!codec->using_regmap)
2806 data = ucontrol->value.bytes.data;
2807 len = params->num_regs * codec->val_bytes;
2810 * If we've got a mask then we need to preserve the register
2811 * bits. We shouldn't modify the incoming data so take a
2815 ret = regmap_read(codec->control_data, params->base, &val);
2819 val &= params->mask;
2821 data = kmemdup(data, len, GFP_KERNEL);
2825 switch (codec->val_bytes) {
2827 ((u8 *)data)[0] &= ~params->mask;
2828 ((u8 *)data)[0] |= val;
2831 ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
2832 ((u16 *)data)[0] |= cpu_to_be16(val);
2835 ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
2836 ((u32 *)data)[0] |= cpu_to_be32(val);
2843 ret = regmap_raw_write(codec->control_data, params->base,
2851 EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
2854 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2856 * @clk_id: DAI specific clock ID
2857 * @freq: new clock frequency in Hz
2858 * @dir: new clock direction - input/output.
2860 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2862 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2863 unsigned int freq, int dir)
2865 if (dai->driver && dai->driver->ops->set_sysclk)
2866 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2867 else if (dai->codec && dai->codec->driver->set_sysclk)
2868 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
2873 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2876 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2878 * @clk_id: DAI specific clock ID
2879 * @source: Source for the clock
2880 * @freq: new clock frequency in Hz
2881 * @dir: new clock direction - input/output.
2883 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2885 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2886 int source, unsigned int freq, int dir)
2888 if (codec->driver->set_sysclk)
2889 return codec->driver->set_sysclk(codec, clk_id, source,
2894 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2897 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2899 * @div_id: DAI specific clock divider ID
2900 * @div: new clock divisor.
2902 * Configures the clock dividers. This is used to derive the best DAI bit and
2903 * frame clocks from the system or master clock. It's best to set the DAI bit
2904 * and frame clocks as low as possible to save system power.
2906 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2907 int div_id, int div)
2909 if (dai->driver && dai->driver->ops->set_clkdiv)
2910 return dai->driver->ops->set_clkdiv(dai, div_id, div);
2914 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2917 * snd_soc_dai_set_pll - configure DAI PLL.
2919 * @pll_id: DAI specific PLL ID
2920 * @source: DAI specific source for the PLL
2921 * @freq_in: PLL input clock frequency in Hz
2922 * @freq_out: requested PLL output clock frequency in Hz
2924 * Configures and enables PLL to generate output clock based on input clock.
2926 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2927 unsigned int freq_in, unsigned int freq_out)
2929 if (dai->driver && dai->driver->ops->set_pll)
2930 return dai->driver->ops->set_pll(dai, pll_id, source,
2932 else if (dai->codec && dai->codec->driver->set_pll)
2933 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2938 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2941 * snd_soc_codec_set_pll - configure codec PLL.
2943 * @pll_id: DAI specific PLL ID
2944 * @source: DAI specific source for the PLL
2945 * @freq_in: PLL input clock frequency in Hz
2946 * @freq_out: requested PLL output clock frequency in Hz
2948 * Configures and enables PLL to generate output clock based on input clock.
2950 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2951 unsigned int freq_in, unsigned int freq_out)
2953 if (codec->driver->set_pll)
2954 return codec->driver->set_pll(codec, pll_id, source,
2959 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2962 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2964 * @fmt: SND_SOC_DAIFMT_ format value.
2966 * Configures the DAI hardware format and clocking.
2968 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2970 if (dai->driver == NULL)
2972 if (dai->driver->ops->set_fmt == NULL)
2974 return dai->driver->ops->set_fmt(dai, fmt);
2976 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2979 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2981 * @tx_mask: bitmask representing active TX slots.
2982 * @rx_mask: bitmask representing active RX slots.
2983 * @slots: Number of slots in use.
2984 * @slot_width: Width in bits for each slot.
2986 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2989 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2990 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2992 if (dai->driver && dai->driver->ops->set_tdm_slot)
2993 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2998 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3001 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3003 * @tx_num: how many TX channels
3004 * @tx_slot: pointer to an array which imply the TX slot number channel
3006 * @rx_num: how many RX channels
3007 * @rx_slot: pointer to an array which imply the RX slot number channel
3010 * configure the relationship between channel number and TDM slot number.
3012 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3013 unsigned int tx_num, unsigned int *tx_slot,
3014 unsigned int rx_num, unsigned int *rx_slot)
3016 if (dai->driver && dai->driver->ops->set_channel_map)
3017 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3022 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3025 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3027 * @tristate: tristate enable
3029 * Tristates the DAI so that others can use it.
3031 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3033 if (dai->driver && dai->driver->ops->set_tristate)
3034 return dai->driver->ops->set_tristate(dai, tristate);
3038 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3041 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3043 * @mute: mute enable
3045 * Mutes the DAI DAC.
3047 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3049 if (dai->driver && dai->driver->ops->digital_mute)
3050 return dai->driver->ops->digital_mute(dai, mute);
3054 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3057 * snd_soc_register_card - Register a card with the ASoC core
3059 * @card: Card to register
3062 int snd_soc_register_card(struct snd_soc_card *card)
3066 if (!card->name || !card->dev)
3069 for (i = 0; i < card->num_links; i++) {
3070 struct snd_soc_dai_link *link = &card->dai_link[i];
3073 * Codec must be specified by 1 of name or OF node,
3074 * not both or neither.
3076 if (!!link->codec_name == !!link->codec_of_node) {
3078 "Neither/both codec name/of_node are set for %s\n",
3084 * Platform may be specified by either name or OF node, but
3085 * can be left unspecified, and a dummy platform will be used.
3087 if (link->platform_name && link->platform_of_node) {
3089 "Both platform name/of_node are set for %s\n", link->name);
3094 * CPU DAI must be specified by 1 of name or OF node,
3095 * not both or neither.
3097 if (!!link->cpu_dai_name == !!link->cpu_dai_of_node) {
3099 "Neither/both cpu_dai name/of_node are set for %s\n",
3105 dev_set_drvdata(card->dev, card);
3107 snd_soc_initialize_card_lists(card);
3109 soc_init_card_debugfs(card);
3111 card->rtd = devm_kzalloc(card->dev,
3112 sizeof(struct snd_soc_pcm_runtime) *
3113 (card->num_links + card->num_aux_devs),
3115 if (card->rtd == NULL)
3117 card->rtd_aux = &card->rtd[card->num_links];
3119 for (i = 0; i < card->num_links; i++)
3120 card->rtd[i].dai_link = &card->dai_link[i];
3122 INIT_LIST_HEAD(&card->list);
3123 INIT_LIST_HEAD(&card->dapm_dirty);
3124 card->instantiated = 0;
3125 mutex_init(&card->mutex);
3126 mutex_init(&card->dapm_mutex);
3128 ret = snd_soc_instantiate_card(card);
3130 soc_cleanup_card_debugfs(card);
3134 EXPORT_SYMBOL_GPL(snd_soc_register_card);
3137 * snd_soc_unregister_card - Unregister a card with the ASoC core
3139 * @card: Card to unregister
3142 int snd_soc_unregister_card(struct snd_soc_card *card)
3144 if (card->instantiated)
3145 soc_cleanup_card_resources(card);
3146 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3150 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3153 * Simplify DAI link configuration by removing ".-1" from device names
3154 * and sanitizing names.
3156 static char *fmt_single_name(struct device *dev, int *id)
3158 char *found, name[NAME_SIZE];
3161 if (dev_name(dev) == NULL)
3164 strlcpy(name, dev_name(dev), NAME_SIZE);
3166 /* are we a "%s.%d" name (platform and SPI components) */
3167 found = strstr(name, dev->driver->name);
3170 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3172 /* discard ID from name if ID == -1 */
3174 found[strlen(dev->driver->name)] = '\0';
3178 /* I2C component devices are named "bus-addr" */
3179 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3180 char tmp[NAME_SIZE];
3182 /* create unique ID number from I2C addr and bus */
3183 *id = ((id1 & 0xffff) << 16) + id2;
3185 /* sanitize component name for DAI link creation */
3186 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3187 strlcpy(name, tmp, NAME_SIZE);
3192 return kstrdup(name, GFP_KERNEL);
3196 * Simplify DAI link naming for single devices with multiple DAIs by removing
3197 * any ".-1" and using the DAI name (instead of device name).
3199 static inline char *fmt_multiple_name(struct device *dev,
3200 struct snd_soc_dai_driver *dai_drv)
3202 if (dai_drv->name == NULL) {
3203 pr_err("asoc: error - multiple DAI %s registered with no name\n",
3208 return kstrdup(dai_drv->name, GFP_KERNEL);
3212 * snd_soc_register_dai - Register a DAI with the ASoC core
3214 * @dai: DAI to register
3216 int snd_soc_register_dai(struct device *dev,
3217 struct snd_soc_dai_driver *dai_drv)
3219 struct snd_soc_dai *dai;
3221 dev_dbg(dev, "dai register %s\n", dev_name(dev));
3223 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3227 /* create DAI component name */
3228 dai->name = fmt_single_name(dev, &dai->id);
3229 if (dai->name == NULL) {
3235 dai->driver = dai_drv;
3236 dai->dapm.dev = dev;
3237 if (!dai->driver->ops)
3238 dai->driver->ops = &null_dai_ops;
3240 mutex_lock(&client_mutex);
3241 list_add(&dai->list, &dai_list);
3242 mutex_unlock(&client_mutex);
3244 pr_debug("Registered DAI '%s'\n", dai->name);
3248 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3251 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3253 * @dai: DAI to unregister
3255 void snd_soc_unregister_dai(struct device *dev)
3257 struct snd_soc_dai *dai;
3259 list_for_each_entry(dai, &dai_list, list) {
3260 if (dev == dai->dev)
3266 mutex_lock(&client_mutex);
3267 list_del(&dai->list);
3268 mutex_unlock(&client_mutex);
3270 pr_debug("Unregistered DAI '%s'\n", dai->name);
3274 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3277 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3279 * @dai: Array of DAIs to register
3280 * @count: Number of DAIs
3282 int snd_soc_register_dais(struct device *dev,
3283 struct snd_soc_dai_driver *dai_drv, size_t count)
3285 struct snd_soc_dai *dai;
3288 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
3290 for (i = 0; i < count; i++) {
3292 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3298 /* create DAI component name */
3299 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3300 if (dai->name == NULL) {
3307 dai->driver = &dai_drv[i];
3308 if (dai->driver->id)
3309 dai->id = dai->driver->id;
3312 dai->dapm.dev = dev;
3313 if (!dai->driver->ops)
3314 dai->driver->ops = &null_dai_ops;
3316 mutex_lock(&client_mutex);
3317 list_add(&dai->list, &dai_list);
3318 mutex_unlock(&client_mutex);
3320 pr_debug("Registered DAI '%s'\n", dai->name);
3326 for (i--; i >= 0; i--)
3327 snd_soc_unregister_dai(dev);
3331 EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3334 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3336 * @dai: Array of DAIs to unregister
3337 * @count: Number of DAIs
3339 void snd_soc_unregister_dais(struct device *dev, size_t count)
3343 for (i = 0; i < count; i++)
3344 snd_soc_unregister_dai(dev);
3346 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3349 * snd_soc_register_platform - Register a platform with the ASoC core
3351 * @platform: platform to register
3353 int snd_soc_register_platform(struct device *dev,
3354 struct snd_soc_platform_driver *platform_drv)
3356 struct snd_soc_platform *platform;
3358 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3360 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3361 if (platform == NULL)
3364 /* create platform component name */
3365 platform->name = fmt_single_name(dev, &platform->id);
3366 if (platform->name == NULL) {
3371 platform->dev = dev;
3372 platform->driver = platform_drv;
3373 platform->dapm.dev = dev;
3374 platform->dapm.platform = platform;
3375 platform->dapm.stream_event = platform_drv->stream_event;
3376 mutex_init(&platform->mutex);
3378 mutex_lock(&client_mutex);
3379 list_add(&platform->list, &platform_list);
3380 mutex_unlock(&client_mutex);
3382 pr_debug("Registered platform '%s'\n", platform->name);
3386 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3389 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3391 * @platform: platform to unregister
3393 void snd_soc_unregister_platform(struct device *dev)
3395 struct snd_soc_platform *platform;
3397 list_for_each_entry(platform, &platform_list, list) {
3398 if (dev == platform->dev)
3404 mutex_lock(&client_mutex);
3405 list_del(&platform->list);
3406 mutex_unlock(&client_mutex);
3408 pr_debug("Unregistered platform '%s'\n", platform->name);
3409 kfree(platform->name);
3412 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3414 static u64 codec_format_map[] = {
3415 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3416 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3417 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3418 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3419 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3420 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3421 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3422 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3423 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3424 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3425 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3426 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3427 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3428 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3429 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3430 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3433 /* Fix up the DAI formats for endianness: codecs don't actually see
3434 * the endianness of the data but we're using the CPU format
3435 * definitions which do need to include endianness so we ensure that
3436 * codec DAIs always have both big and little endian variants set.
3438 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3442 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3443 if (stream->formats & codec_format_map[i])
3444 stream->formats |= codec_format_map[i];
3448 * snd_soc_register_codec - Register a codec with the ASoC core
3450 * @codec: codec to register
3452 int snd_soc_register_codec(struct device *dev,
3453 const struct snd_soc_codec_driver *codec_drv,
3454 struct snd_soc_dai_driver *dai_drv,
3458 struct snd_soc_codec *codec;
3461 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3463 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3467 /* create CODEC component name */
3468 codec->name = fmt_single_name(dev, &codec->id);
3469 if (codec->name == NULL) {
3474 if (codec_drv->compress_type)
3475 codec->compress_type = codec_drv->compress_type;
3477 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3479 codec->write = codec_drv->write;
3480 codec->read = codec_drv->read;
3481 codec->volatile_register = codec_drv->volatile_register;
3482 codec->readable_register = codec_drv->readable_register;
3483 codec->writable_register = codec_drv->writable_register;
3484 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
3485 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3486 codec->dapm.dev = dev;
3487 codec->dapm.codec = codec;
3488 codec->dapm.seq_notifier = codec_drv->seq_notifier;
3489 codec->dapm.stream_event = codec_drv->stream_event;
3491 codec->driver = codec_drv;
3492 codec->num_dai = num_dai;
3493 mutex_init(&codec->mutex);
3495 /* allocate CODEC register cache */
3496 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3497 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
3498 codec->reg_size = reg_size;
3499 /* it is necessary to make a copy of the default register cache
3500 * because in the case of using a compression type that requires
3501 * the default register cache to be marked as __devinitconst the
3502 * kernel might have freed the array by the time we initialize
3505 if (codec_drv->reg_cache_default) {
3506 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3507 reg_size, GFP_KERNEL);
3508 if (!codec->reg_def_copy) {
3515 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3516 if (!codec->volatile_register)
3517 codec->volatile_register = snd_soc_default_volatile_register;
3518 if (!codec->readable_register)
3519 codec->readable_register = snd_soc_default_readable_register;
3520 if (!codec->writable_register)
3521 codec->writable_register = snd_soc_default_writable_register;
3524 for (i = 0; i < num_dai; i++) {
3525 fixup_codec_formats(&dai_drv[i].playback);
3526 fixup_codec_formats(&dai_drv[i].capture);
3529 /* register any DAIs */
3531 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3536 mutex_lock(&client_mutex);
3537 list_add(&codec->list, &codec_list);
3538 mutex_unlock(&client_mutex);
3540 pr_debug("Registered codec '%s'\n", codec->name);
3544 kfree(codec->reg_def_copy);
3545 codec->reg_def_copy = NULL;
3550 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3553 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3555 * @codec: codec to unregister
3557 void snd_soc_unregister_codec(struct device *dev)
3559 struct snd_soc_codec *codec;
3562 list_for_each_entry(codec, &codec_list, list) {
3563 if (dev == codec->dev)
3570 for (i = 0; i < codec->num_dai; i++)
3571 snd_soc_unregister_dai(dev);
3573 mutex_lock(&client_mutex);
3574 list_del(&codec->list);
3575 mutex_unlock(&client_mutex);
3577 pr_debug("Unregistered codec '%s'\n", codec->name);
3579 snd_soc_cache_exit(codec);
3580 kfree(codec->reg_def_copy);
3584 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3586 /* Retrieve a card's name from device tree */
3587 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3588 const char *propname)
3590 struct device_node *np = card->dev->of_node;
3593 ret = of_property_read_string_index(np, propname, 0, &card->name);
3595 * EINVAL means the property does not exist. This is fine providing
3596 * card->name was previously set, which is checked later in
3597 * snd_soc_register_card.
3599 if (ret < 0 && ret != -EINVAL) {
3601 "Property '%s' could not be read: %d\n",
3608 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3610 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3611 const char *propname)
3613 struct device_node *np = card->dev->of_node;
3615 struct snd_soc_dapm_route *routes;
3618 num_routes = of_property_count_strings(np, propname);
3619 if (num_routes & 1) {
3621 "Property '%s's length is not even\n",
3628 "Property '%s's length is zero\n",
3633 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3637 "Could not allocate DAPM route table\n");
3641 for (i = 0; i < num_routes; i++) {
3642 ret = of_property_read_string_index(np, propname,
3643 2 * i, &routes[i].sink);
3646 "Property '%s' index %d could not be read: %d\n",
3647 propname, 2 * i, ret);
3650 ret = of_property_read_string_index(np, propname,
3651 (2 * i) + 1, &routes[i].source);
3654 "Property '%s' index %d could not be read: %d\n",
3655 propname, (2 * i) + 1, ret);
3660 card->num_dapm_routes = num_routes;
3661 card->dapm_routes = routes;
3665 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3667 static int __init snd_soc_init(void)
3669 #ifdef CONFIG_DEBUG_FS
3670 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3671 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
3672 pr_warn("ASoC: Failed to create debugfs directory\n");
3673 snd_soc_debugfs_root = NULL;
3676 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
3678 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3680 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
3682 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
3684 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
3685 &platform_list_fops))
3686 pr_warn("ASoC: Failed to create platform list debugfs file\n");
3689 snd_soc_util_init();
3691 return platform_driver_register(&soc_driver);
3693 module_init(snd_soc_init);
3695 static void __exit snd_soc_exit(void)
3697 snd_soc_util_exit();
3699 #ifdef CONFIG_DEBUG_FS
3700 debugfs_remove_recursive(snd_soc_debugfs_root);
3702 platform_driver_unregister(&soc_driver);
3704 module_exit(snd_soc_exit);
3706 /* Module information */
3707 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3708 MODULE_DESCRIPTION("ALSA SoC Core");
3709 MODULE_LICENSE("GPL");
3710 MODULE_ALIAS("platform:soc-audio");