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(card_list);
58 static LIST_HEAD(dai_list);
59 static LIST_HEAD(platform_list);
60 static LIST_HEAD(codec_list);
63 * This is a timeout to do a DAPM powerdown after a stream is closed().
64 * It can be used to eliminate pops between different playback streams, e.g.
65 * between two audio tracks.
67 static int pmdown_time = 5000;
68 module_param(pmdown_time, int, 0);
69 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
71 /* returns the minimum number of bytes needed to represent
72 * a particular given value */
73 static int min_bytes_needed(unsigned long val)
78 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
81 c = (sizeof val * 8) - c;
89 /* fill buf which is 'len' bytes with a formatted
90 * string of the form 'reg: value\n' */
91 static int format_register_str(struct snd_soc_codec *codec,
92 unsigned int reg, char *buf, size_t len)
94 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
95 int regsize = codec->driver->reg_word_size * 2;
98 char regbuf[regsize + 1];
100 /* since tmpbuf is allocated on the stack, warn the callers if they
101 * try to abuse this function */
104 /* +2 for ': ' and + 1 for '\n' */
105 if (wordsize + regsize + 2 + 1 != len)
108 ret = snd_soc_read(codec, reg);
110 memset(regbuf, 'X', regsize);
111 regbuf[regsize] = '\0';
113 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
116 /* prepare the buffer */
117 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
118 /* copy it back to the caller without the '\0' */
119 memcpy(buf, tmpbuf, len);
124 /* codec register dump */
125 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
126 size_t count, loff_t pos)
129 int wordsize, regsize;
134 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
135 regsize = codec->driver->reg_word_size * 2;
137 len = wordsize + regsize + 2 + 1;
139 if (!codec->driver->reg_cache_size)
142 if (codec->driver->reg_cache_step)
143 step = codec->driver->reg_cache_step;
145 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
146 if (!snd_soc_codec_readable_register(codec, i))
148 if (codec->driver->display_register) {
149 count += codec->driver->display_register(codec, buf + count,
150 PAGE_SIZE - count, i);
152 /* only support larger than PAGE_SIZE bytes debugfs
153 * entries for the default case */
155 if (total + len >= count - 1)
157 format_register_str(codec, i, buf + total, len);
164 total = min(total, count - 1);
169 static ssize_t codec_reg_show(struct device *dev,
170 struct device_attribute *attr, char *buf)
172 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
174 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
177 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
179 static ssize_t pmdown_time_show(struct device *dev,
180 struct device_attribute *attr, char *buf)
182 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
184 return sprintf(buf, "%ld\n", rtd->pmdown_time);
187 static ssize_t pmdown_time_set(struct device *dev,
188 struct device_attribute *attr,
189 const char *buf, size_t count)
191 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
194 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
201 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
203 #ifdef CONFIG_DEBUG_FS
204 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
205 size_t count, loff_t *ppos)
208 struct snd_soc_codec *codec = file->private_data;
211 if (*ppos < 0 || !count)
214 buf = kmalloc(count, GFP_KERNEL);
218 ret = soc_codec_reg_show(codec, buf, count, *ppos);
220 if (copy_to_user(user_buf, buf, ret)) {
231 static ssize_t codec_reg_write_file(struct file *file,
232 const char __user *user_buf, size_t count, loff_t *ppos)
237 unsigned long reg, value;
238 struct snd_soc_codec *codec = file->private_data;
240 buf_size = min(count, (sizeof(buf)-1));
241 if (copy_from_user(buf, user_buf, buf_size))
245 while (*start == ' ')
247 reg = simple_strtoul(start, &start, 16);
248 while (*start == ' ')
250 if (strict_strtoul(start, 16, &value))
253 /* Userspace has been fiddling around behind the kernel's back */
254 add_taint(TAINT_USER);
256 snd_soc_write(codec, reg, value);
260 static const struct file_operations codec_reg_fops = {
262 .read = codec_reg_read_file,
263 .write = codec_reg_write_file,
264 .llseek = default_llseek,
267 static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
269 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
271 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
273 if (!codec->debugfs_codec_root) {
274 dev_warn(codec->dev, "Failed to create codec debugfs directory\n");
278 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
280 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
283 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
284 codec->debugfs_codec_root,
285 codec, &codec_reg_fops);
286 if (!codec->debugfs_reg)
287 dev_warn(codec->dev, "Failed to create codec register debugfs file\n");
289 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
292 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
294 debugfs_remove_recursive(codec->debugfs_codec_root);
297 static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
299 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
301 platform->debugfs_platform_root = debugfs_create_dir(platform->name,
303 if (!platform->debugfs_platform_root) {
304 dev_warn(platform->dev,
305 "Failed to create platform debugfs directory\n");
309 snd_soc_dapm_debugfs_init(&platform->dapm,
310 platform->debugfs_platform_root);
313 static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
315 debugfs_remove_recursive(platform->debugfs_platform_root);
318 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
319 size_t count, loff_t *ppos)
321 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
322 ssize_t len, ret = 0;
323 struct snd_soc_codec *codec;
328 list_for_each_entry(codec, &codec_list, list) {
329 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
333 if (ret > PAGE_SIZE) {
340 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
347 static const struct file_operations codec_list_fops = {
348 .read = codec_list_read_file,
349 .llseek = default_llseek,/* read accesses f_pos */
352 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
353 size_t count, loff_t *ppos)
355 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
356 ssize_t len, ret = 0;
357 struct snd_soc_dai *dai;
362 list_for_each_entry(dai, &dai_list, list) {
363 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
366 if (ret > PAGE_SIZE) {
372 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
379 static const struct file_operations dai_list_fops = {
380 .read = dai_list_read_file,
381 .llseek = default_llseek,/* read accesses f_pos */
384 static ssize_t platform_list_read_file(struct file *file,
385 char __user *user_buf,
386 size_t count, loff_t *ppos)
388 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
389 ssize_t len, ret = 0;
390 struct snd_soc_platform *platform;
395 list_for_each_entry(platform, &platform_list, list) {
396 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
400 if (ret > PAGE_SIZE) {
406 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
413 static const struct file_operations platform_list_fops = {
414 .read = platform_list_read_file,
415 .llseek = default_llseek,/* read accesses f_pos */
418 static void soc_init_card_debugfs(struct snd_soc_card *card)
420 card->debugfs_card_root = debugfs_create_dir(card->name,
421 snd_soc_debugfs_root);
422 if (!card->debugfs_card_root) {
424 "ASoC: Failed to create card debugfs directory\n");
428 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
429 card->debugfs_card_root,
431 if (!card->debugfs_pop_time)
433 "Failed to create pop time debugfs file\n");
436 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
438 debugfs_remove_recursive(card->debugfs_card_root);
443 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
447 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
451 static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
455 static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
459 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
463 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
468 #ifdef CONFIG_SND_SOC_AC97_BUS
469 /* unregister ac97 codec */
470 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
472 if (codec->ac97->dev.bus)
473 device_unregister(&codec->ac97->dev);
477 /* stop no dev release warning */
478 static void soc_ac97_device_release(struct device *dev){}
480 /* register ac97 codec to bus */
481 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
485 codec->ac97->dev.bus = &ac97_bus_type;
486 codec->ac97->dev.parent = codec->card->dev;
487 codec->ac97->dev.release = soc_ac97_device_release;
489 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
490 codec->card->snd_card->number, 0, codec->name);
491 err = device_register(&codec->ac97->dev);
493 snd_printk(KERN_ERR "Can't register ac97 bus\n");
494 codec->ac97->dev.bus = NULL;
501 #ifdef CONFIG_PM_SLEEP
502 /* powers down audio subsystem for suspend */
503 int snd_soc_suspend(struct device *dev)
505 struct snd_soc_card *card = dev_get_drvdata(dev);
506 struct snd_soc_codec *codec;
509 /* If the initialization of this soc device failed, there is no codec
510 * associated with it. Just bail out in this case.
512 if (list_empty(&card->codec_dev_list))
515 /* Due to the resume being scheduled into a workqueue we could
516 * suspend before that's finished - wait for it to complete.
518 snd_power_lock(card->snd_card);
519 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
520 snd_power_unlock(card->snd_card);
522 /* we're going to block userspace touching us until resume completes */
523 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
525 /* mute any active DACs */
526 for (i = 0; i < card->num_rtd; i++) {
527 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
528 struct snd_soc_dai_driver *drv = dai->driver;
530 if (card->rtd[i].dai_link->ignore_suspend)
533 if (drv->ops->digital_mute && dai->playback_active)
534 drv->ops->digital_mute(dai, 1);
537 /* suspend all pcms */
538 for (i = 0; i < card->num_rtd; i++) {
539 if (card->rtd[i].dai_link->ignore_suspend)
542 snd_pcm_suspend_all(card->rtd[i].pcm);
545 if (card->suspend_pre)
546 card->suspend_pre(card);
548 for (i = 0; i < card->num_rtd; i++) {
549 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
550 struct snd_soc_platform *platform = card->rtd[i].platform;
552 if (card->rtd[i].dai_link->ignore_suspend)
555 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
556 cpu_dai->driver->suspend(cpu_dai);
557 if (platform->driver->suspend && !platform->suspended) {
558 platform->driver->suspend(cpu_dai);
559 platform->suspended = 1;
563 /* close any waiting streams and save state */
564 for (i = 0; i < card->num_rtd; i++) {
565 flush_delayed_work_sync(&card->rtd[i].delayed_work);
566 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
569 for (i = 0; i < card->num_rtd; i++) {
570 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
572 if (card->rtd[i].dai_link->ignore_suspend)
575 snd_soc_dapm_stream_event(&card->rtd[i],
576 SNDRV_PCM_STREAM_PLAYBACK,
578 SND_SOC_DAPM_STREAM_SUSPEND);
580 snd_soc_dapm_stream_event(&card->rtd[i],
581 SNDRV_PCM_STREAM_CAPTURE,
583 SND_SOC_DAPM_STREAM_SUSPEND);
586 /* suspend all CODECs */
587 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
588 /* If there are paths active then the CODEC will be held with
589 * bias _ON and should not be suspended. */
590 if (!codec->suspended && codec->driver->suspend) {
591 switch (codec->dapm.bias_level) {
592 case SND_SOC_BIAS_STANDBY:
594 * If the CODEC is capable of idle
595 * bias off then being in STANDBY
596 * means it's doing something,
597 * otherwise fall through.
599 if (codec->dapm.idle_bias_off) {
601 "idle_bias_off CODEC on over suspend\n");
604 case SND_SOC_BIAS_OFF:
605 codec->driver->suspend(codec);
606 codec->suspended = 1;
607 codec->cache_sync = 1;
610 dev_dbg(codec->dev, "CODEC is on over suspend\n");
616 for (i = 0; i < card->num_rtd; i++) {
617 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
619 if (card->rtd[i].dai_link->ignore_suspend)
622 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
623 cpu_dai->driver->suspend(cpu_dai);
626 if (card->suspend_post)
627 card->suspend_post(card);
631 EXPORT_SYMBOL_GPL(snd_soc_suspend);
633 /* deferred resume work, so resume can complete before we finished
634 * setting our codec back up, which can be very slow on I2C
636 static void soc_resume_deferred(struct work_struct *work)
638 struct snd_soc_card *card =
639 container_of(work, struct snd_soc_card, deferred_resume_work);
640 struct snd_soc_codec *codec;
643 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
644 * so userspace apps are blocked from touching us
647 dev_dbg(card->dev, "starting resume work\n");
649 /* Bring us up into D2 so that DAPM starts enabling things */
650 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
652 if (card->resume_pre)
653 card->resume_pre(card);
655 /* resume AC97 DAIs */
656 for (i = 0; i < card->num_rtd; i++) {
657 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
659 if (card->rtd[i].dai_link->ignore_suspend)
662 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
663 cpu_dai->driver->resume(cpu_dai);
666 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
667 /* If the CODEC was idle over suspend then it will have been
668 * left with bias OFF or STANDBY and suspended so we must now
669 * resume. Otherwise the suspend was suppressed.
671 if (codec->driver->resume && codec->suspended) {
672 switch (codec->dapm.bias_level) {
673 case SND_SOC_BIAS_STANDBY:
674 case SND_SOC_BIAS_OFF:
675 codec->driver->resume(codec);
676 codec->suspended = 0;
679 dev_dbg(codec->dev, "CODEC was on over suspend\n");
685 for (i = 0; i < card->num_rtd; i++) {
686 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
688 if (card->rtd[i].dai_link->ignore_suspend)
691 snd_soc_dapm_stream_event(&card->rtd[i],
692 SNDRV_PCM_STREAM_PLAYBACK, codec_dai,
693 SND_SOC_DAPM_STREAM_RESUME);
695 snd_soc_dapm_stream_event(&card->rtd[i],
696 SNDRV_PCM_STREAM_CAPTURE, codec_dai,
697 SND_SOC_DAPM_STREAM_RESUME);
700 /* unmute any active DACs */
701 for (i = 0; i < card->num_rtd; i++) {
702 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
703 struct snd_soc_dai_driver *drv = dai->driver;
705 if (card->rtd[i].dai_link->ignore_suspend)
708 if (drv->ops->digital_mute && dai->playback_active)
709 drv->ops->digital_mute(dai, 0);
712 for (i = 0; i < card->num_rtd; i++) {
713 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
714 struct snd_soc_platform *platform = card->rtd[i].platform;
716 if (card->rtd[i].dai_link->ignore_suspend)
719 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
720 cpu_dai->driver->resume(cpu_dai);
721 if (platform->driver->resume && platform->suspended) {
722 platform->driver->resume(cpu_dai);
723 platform->suspended = 0;
727 if (card->resume_post)
728 card->resume_post(card);
730 dev_dbg(card->dev, "resume work completed\n");
732 /* userspace can access us now we are back as we were before */
733 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
736 /* powers up audio subsystem after a suspend */
737 int snd_soc_resume(struct device *dev)
739 struct snd_soc_card *card = dev_get_drvdata(dev);
740 int i, ac97_control = 0;
742 /* If the initialization of this soc device failed, there is no codec
743 * associated with it. Just bail out in this case.
745 if (list_empty(&card->codec_dev_list))
748 /* AC97 devices might have other drivers hanging off them so
749 * need to resume immediately. Other drivers don't have that
750 * problem and may take a substantial amount of time to resume
751 * due to I/O costs and anti-pop so handle them out of line.
753 for (i = 0; i < card->num_rtd; i++) {
754 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
755 ac97_control |= cpu_dai->driver->ac97_control;
758 dev_dbg(dev, "Resuming AC97 immediately\n");
759 soc_resume_deferred(&card->deferred_resume_work);
761 dev_dbg(dev, "Scheduling resume work\n");
762 if (!schedule_work(&card->deferred_resume_work))
763 dev_err(dev, "resume work item may be lost\n");
768 EXPORT_SYMBOL_GPL(snd_soc_resume);
770 #define snd_soc_suspend NULL
771 #define snd_soc_resume NULL
774 static const struct snd_soc_dai_ops null_dai_ops = {
777 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
779 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
780 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
781 struct snd_soc_codec *codec;
782 struct snd_soc_platform *platform;
783 struct snd_soc_dai *codec_dai, *cpu_dai;
784 const char *platform_name;
788 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
790 /* do we already have the CPU DAI for this link ? */
794 /* no, then find CPU DAI from registered DAIs*/
795 list_for_each_entry(cpu_dai, &dai_list, list) {
796 if (dai_link->cpu_dai_of_node) {
797 if (cpu_dai->dev->of_node != dai_link->cpu_dai_of_node)
800 if (strcmp(cpu_dai->name, dai_link->cpu_dai_name))
804 rtd->cpu_dai = cpu_dai;
807 dev_dbg(card->dev, "CPU DAI %s not registered\n",
808 dai_link->cpu_dai_name);
811 /* do we already have the CODEC for this link ? */
816 /* no, then find CODEC from registered CODECs*/
817 list_for_each_entry(codec, &codec_list, list) {
818 if (dai_link->codec_of_node) {
819 if (codec->dev->of_node != dai_link->codec_of_node)
822 if (strcmp(codec->name, dai_link->codec_name))
829 * CODEC found, so find CODEC DAI from registered DAIs from
832 list_for_each_entry(codec_dai, &dai_list, list) {
833 if (codec->dev == codec_dai->dev &&
834 !strcmp(codec_dai->name,
835 dai_link->codec_dai_name)) {
837 rtd->codec_dai = codec_dai;
841 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
842 dai_link->codec_dai_name);
846 dev_dbg(card->dev, "CODEC %s not registered\n",
847 dai_link->codec_name);
850 /* do we need a platform? */
854 /* if there's no platform we match on the empty platform */
855 platform_name = dai_link->platform_name;
856 if (!platform_name && !dai_link->platform_of_node)
857 platform_name = "snd-soc-dummy";
859 /* no, then find one from the set of registered platforms */
860 list_for_each_entry(platform, &platform_list, list) {
861 if (dai_link->platform_of_node) {
862 if (platform->dev->of_node !=
863 dai_link->platform_of_node)
866 if (strcmp(platform->name, platform_name))
870 rtd->platform = platform;
874 dev_dbg(card->dev, "platform %s not registered\n",
875 dai_link->platform_name);
879 /* mark rtd as complete if we found all 4 of our client devices */
880 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
887 static void soc_remove_codec(struct snd_soc_codec *codec)
891 if (codec->driver->remove) {
892 err = codec->driver->remove(codec);
895 "asoc: failed to remove %s: %d\n",
899 /* Make sure all DAPM widgets are freed */
900 snd_soc_dapm_free(&codec->dapm);
902 soc_cleanup_codec_debugfs(codec);
904 list_del(&codec->card_list);
905 module_put(codec->dev->driver->owner);
908 static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
910 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
911 struct snd_soc_codec *codec = rtd->codec;
912 struct snd_soc_platform *platform = rtd->platform;
913 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
916 /* unregister the rtd device */
917 if (rtd->dev_registered) {
918 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
919 device_remove_file(rtd->dev, &dev_attr_codec_reg);
920 device_unregister(rtd->dev);
921 rtd->dev_registered = 0;
924 /* remove the CODEC DAI */
925 if (codec_dai && codec_dai->probed &&
926 codec_dai->driver->remove_order == order) {
927 if (codec_dai->driver->remove) {
928 err = codec_dai->driver->remove(codec_dai);
930 pr_err("asoc: failed to remove %s: %d\n",
931 codec_dai->name, err);
933 codec_dai->probed = 0;
934 list_del(&codec_dai->card_list);
937 /* remove the platform */
938 if (platform && platform->probed &&
939 platform->driver->remove_order == order) {
940 if (platform->driver->remove) {
941 err = platform->driver->remove(platform);
943 pr_err("asoc: failed to remove %s: %d\n",
944 platform->name, err);
947 /* Make sure all DAPM widgets are freed */
948 snd_soc_dapm_free(&platform->dapm);
950 soc_cleanup_platform_debugfs(platform);
951 platform->probed = 0;
952 list_del(&platform->card_list);
953 module_put(platform->dev->driver->owner);
956 /* remove the CODEC */
957 if (codec && codec->probed &&
958 codec->driver->remove_order == order)
959 soc_remove_codec(codec);
961 /* remove the cpu_dai */
962 if (cpu_dai && cpu_dai->probed &&
963 cpu_dai->driver->remove_order == order) {
964 if (cpu_dai->driver->remove) {
965 err = cpu_dai->driver->remove(cpu_dai);
967 pr_err("asoc: failed to remove %s: %d\n",
971 list_del(&cpu_dai->card_list);
972 module_put(cpu_dai->dev->driver->owner);
976 static void soc_remove_dai_links(struct snd_soc_card *card)
980 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
982 for (dai = 0; dai < card->num_rtd; dai++)
983 soc_remove_dai_link(card, dai, order);
988 static void soc_set_name_prefix(struct snd_soc_card *card,
989 struct snd_soc_codec *codec)
993 if (card->codec_conf == NULL)
996 for (i = 0; i < card->num_configs; i++) {
997 struct snd_soc_codec_conf *map = &card->codec_conf[i];
998 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
999 codec->name_prefix = map->name_prefix;
1005 static int soc_probe_codec(struct snd_soc_card *card,
1006 struct snd_soc_codec *codec)
1009 const struct snd_soc_codec_driver *driver = codec->driver;
1010 struct snd_soc_dai *dai;
1013 codec->dapm.card = card;
1014 soc_set_name_prefix(card, codec);
1016 if (!try_module_get(codec->dev->driver->owner))
1019 soc_init_codec_debugfs(codec);
1021 if (driver->dapm_widgets)
1022 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1023 driver->num_dapm_widgets);
1025 /* Create DAPM widgets for each DAI stream */
1026 list_for_each_entry(dai, &dai_list, list) {
1027 if (dai->dev != codec->dev)
1030 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1033 codec->dapm.idle_bias_off = driver->idle_bias_off;
1035 if (driver->probe) {
1036 ret = driver->probe(codec);
1039 "asoc: failed to probe CODEC %s: %d\n",
1045 if (driver->controls)
1046 snd_soc_add_codec_controls(codec, driver->controls,
1047 driver->num_controls);
1048 if (driver->dapm_routes)
1049 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1050 driver->num_dapm_routes);
1052 /* mark codec as probed and add to card codec list */
1054 list_add(&codec->card_list, &card->codec_dev_list);
1055 list_add(&codec->dapm.list, &card->dapm_list);
1060 soc_cleanup_codec_debugfs(codec);
1061 module_put(codec->dev->driver->owner);
1066 static int soc_probe_platform(struct snd_soc_card *card,
1067 struct snd_soc_platform *platform)
1070 const struct snd_soc_platform_driver *driver = platform->driver;
1072 platform->card = card;
1073 platform->dapm.card = card;
1075 if (!try_module_get(platform->dev->driver->owner))
1078 soc_init_platform_debugfs(platform);
1080 if (driver->dapm_widgets)
1081 snd_soc_dapm_new_controls(&platform->dapm,
1082 driver->dapm_widgets, driver->num_dapm_widgets);
1084 if (driver->probe) {
1085 ret = driver->probe(platform);
1087 dev_err(platform->dev,
1088 "asoc: failed to probe platform %s: %d\n",
1089 platform->name, ret);
1094 if (driver->controls)
1095 snd_soc_add_platform_controls(platform, driver->controls,
1096 driver->num_controls);
1097 if (driver->dapm_routes)
1098 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1099 driver->num_dapm_routes);
1101 /* mark platform as probed and add to card platform list */
1102 platform->probed = 1;
1103 list_add(&platform->card_list, &card->platform_dev_list);
1104 list_add(&platform->dapm.list, &card->dapm_list);
1109 soc_cleanup_platform_debugfs(platform);
1110 module_put(platform->dev->driver->owner);
1115 static void rtd_release(struct device *dev)
1120 static int soc_post_component_init(struct snd_soc_card *card,
1121 struct snd_soc_codec *codec,
1122 int num, int dailess)
1124 struct snd_soc_dai_link *dai_link = NULL;
1125 struct snd_soc_aux_dev *aux_dev = NULL;
1126 struct snd_soc_pcm_runtime *rtd;
1127 const char *temp, *name;
1131 dai_link = &card->dai_link[num];
1132 rtd = &card->rtd[num];
1133 name = dai_link->name;
1135 aux_dev = &card->aux_dev[num];
1136 rtd = &card->rtd_aux[num];
1137 name = aux_dev->name;
1141 /* Make sure all DAPM widgets are instantiated */
1142 snd_soc_dapm_new_widgets(&codec->dapm);
1144 /* machine controls, routes and widgets are not prefixed */
1145 temp = codec->name_prefix;
1146 codec->name_prefix = NULL;
1148 /* do machine specific initialization */
1149 if (!dailess && dai_link->init)
1150 ret = dai_link->init(rtd);
1151 else if (dailess && aux_dev->init)
1152 ret = aux_dev->init(&codec->dapm);
1154 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1157 codec->name_prefix = temp;
1159 /* register the rtd device */
1162 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1165 device_initialize(rtd->dev);
1166 rtd->dev->parent = card->dev;
1167 rtd->dev->release = rtd_release;
1168 rtd->dev->init_name = name;
1169 dev_set_drvdata(rtd->dev, rtd);
1170 mutex_init(&rtd->pcm_mutex);
1171 ret = device_add(rtd->dev);
1174 "asoc: failed to register runtime device: %d\n", ret);
1177 rtd->dev_registered = 1;
1179 /* add DAPM sysfs entries for this codec */
1180 ret = snd_soc_dapm_sys_add(rtd->dev);
1183 "asoc: failed to add codec dapm sysfs entries: %d\n",
1186 /* add codec sysfs entries */
1187 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1190 "asoc: failed to add codec sysfs files: %d\n", ret);
1195 static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
1197 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1198 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1199 struct snd_soc_codec *codec = rtd->codec;
1200 struct snd_soc_platform *platform = rtd->platform;
1201 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1204 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1205 card->name, num, order);
1207 /* config components */
1208 codec_dai->codec = codec;
1209 cpu_dai->platform = platform;
1210 codec_dai->card = card;
1211 cpu_dai->card = card;
1213 /* set default power off timeout */
1214 rtd->pmdown_time = pmdown_time;
1216 /* probe the cpu_dai */
1217 if (!cpu_dai->probed &&
1218 cpu_dai->driver->probe_order == order) {
1219 if (!try_module_get(cpu_dai->dev->driver->owner))
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_probe_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;
1341 /* find CODEC from registered CODECs*/
1342 list_for_each_entry(codec, &codec_list, list) {
1343 if (!strcmp(codec->name, aux_dev->codec_name)) {
1344 if (codec->probed) {
1346 "asoc: codec already probed");
1353 /* codec not found */
1354 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1358 ret = soc_probe_codec(card, codec);
1362 ret = soc_post_component_init(card, codec, num, 1);
1368 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1370 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1371 struct snd_soc_codec *codec = rtd->codec;
1373 /* unregister the rtd device */
1374 if (rtd->dev_registered) {
1375 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1376 device_del(rtd->dev);
1377 rtd->dev_registered = 0;
1380 if (codec && codec->probed)
1381 soc_remove_codec(codec);
1384 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1385 enum snd_soc_compress_type compress_type)
1389 if (codec->cache_init)
1392 /* override the compress_type if necessary */
1393 if (compress_type && codec->compress_type != compress_type)
1394 codec->compress_type = compress_type;
1395 ret = snd_soc_cache_init(codec);
1397 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1401 codec->cache_init = 1;
1405 static void snd_soc_instantiate_card(struct snd_soc_card *card)
1407 struct snd_soc_codec *codec;
1408 struct snd_soc_codec_conf *codec_conf;
1409 enum snd_soc_compress_type compress_type;
1410 struct snd_soc_dai_link *dai_link;
1413 mutex_lock(&card->mutex);
1415 if (card->instantiated) {
1416 mutex_unlock(&card->mutex);
1421 for (i = 0; i < card->num_links; i++)
1422 soc_bind_dai_link(card, i);
1424 /* bind completed ? */
1425 if (card->num_rtd != card->num_links) {
1426 mutex_unlock(&card->mutex);
1430 /* initialize the register cache for each available codec */
1431 list_for_each_entry(codec, &codec_list, list) {
1432 if (codec->cache_init)
1434 /* by default we don't override the compress_type */
1436 /* check to see if we need to override the compress_type */
1437 for (i = 0; i < card->num_configs; ++i) {
1438 codec_conf = &card->codec_conf[i];
1439 if (!strcmp(codec->name, codec_conf->dev_name)) {
1440 compress_type = codec_conf->compress_type;
1441 if (compress_type && compress_type
1442 != codec->compress_type)
1446 ret = snd_soc_init_codec_cache(codec, compress_type);
1448 mutex_unlock(&card->mutex);
1453 /* card bind complete so register a sound card */
1454 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1455 card->owner, 0, &card->snd_card);
1457 pr_err("asoc: can't create sound card for card %s: %d\n",
1459 mutex_unlock(&card->mutex);
1462 card->snd_card->dev = card->dev;
1464 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1465 card->dapm.dev = card->dev;
1466 card->dapm.card = card;
1467 list_add(&card->dapm.list, &card->dapm_list);
1469 #ifdef CONFIG_DEBUG_FS
1470 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1473 #ifdef CONFIG_PM_SLEEP
1474 /* deferred resume work */
1475 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1478 if (card->dapm_widgets)
1479 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1480 card->num_dapm_widgets);
1482 /* initialise the sound card only once */
1484 ret = card->probe(card);
1486 goto card_probe_error;
1489 /* early DAI link probe */
1490 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1492 for (i = 0; i < card->num_links; i++) {
1493 ret = soc_probe_dai_link(card, i, order);
1495 pr_err("asoc: failed to instantiate card %s: %d\n",
1502 for (i = 0; i < card->num_aux_devs; i++) {
1503 ret = soc_probe_aux_dev(card, i);
1505 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1507 goto probe_aux_dev_err;
1511 snd_soc_dapm_link_dai_widgets(card);
1514 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1516 if (card->dapm_routes)
1517 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1518 card->num_dapm_routes);
1520 snd_soc_dapm_new_widgets(&card->dapm);
1522 for (i = 0; i < card->num_links; i++) {
1523 dai_link = &card->dai_link[i];
1525 if (dai_link->dai_fmt) {
1526 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1528 if (ret != 0 && ret != -ENOTSUPP)
1529 dev_warn(card->rtd[i].codec_dai->dev,
1530 "Failed to set DAI format: %d\n",
1533 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1535 if (ret != 0 && ret != -ENOTSUPP)
1536 dev_warn(card->rtd[i].cpu_dai->dev,
1537 "Failed to set DAI format: %d\n",
1542 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1544 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1545 "%s", card->long_name ? card->long_name : card->name);
1546 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1547 "%s", card->driver_name ? card->driver_name : card->name);
1548 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1549 switch (card->snd_card->driver[i]) {
1555 if (!isalnum(card->snd_card->driver[i]))
1556 card->snd_card->driver[i] = '_';
1561 if (card->late_probe) {
1562 ret = card->late_probe(card);
1564 dev_err(card->dev, "%s late_probe() failed: %d\n",
1566 goto probe_aux_dev_err;
1570 snd_soc_dapm_new_widgets(&card->dapm);
1572 if (card->fully_routed)
1573 list_for_each_entry(codec, &card->codec_dev_list, card_list)
1574 snd_soc_dapm_auto_nc_codec_pins(codec);
1576 ret = snd_card_register(card->snd_card);
1578 pr_err("asoc: failed to register soundcard for %s: %d\n",
1580 goto probe_aux_dev_err;
1583 #ifdef CONFIG_SND_SOC_AC97_BUS
1584 /* register any AC97 codecs */
1585 for (i = 0; i < card->num_rtd; i++) {
1586 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1588 pr_err("asoc: failed to register AC97 %s: %d\n",
1591 soc_unregister_ac97_dai_link(card->rtd[i].codec);
1592 goto probe_aux_dev_err;
1597 card->instantiated = 1;
1598 snd_soc_dapm_sync(&card->dapm);
1599 mutex_unlock(&card->mutex);
1603 for (i = 0; i < card->num_aux_devs; i++)
1604 soc_remove_aux_dev(card, i);
1607 soc_remove_dai_links(card);
1613 snd_card_free(card->snd_card);
1615 mutex_unlock(&card->mutex);
1619 * Attempt to initialise any uninitialised cards. Must be called with
1622 static void snd_soc_instantiate_cards(void)
1624 struct snd_soc_card *card;
1625 list_for_each_entry(card, &card_list, list)
1626 snd_soc_instantiate_card(card);
1629 /* probes a new socdev */
1630 static int soc_probe(struct platform_device *pdev)
1632 struct snd_soc_card *card = platform_get_drvdata(pdev);
1636 * no card, so machine driver should be registering card
1637 * we should not be here in that case so ret error
1642 dev_warn(&pdev->dev,
1643 "ASoC machine %s should use snd_soc_register_card()\n",
1646 /* Bodge while we unpick instantiation */
1647 card->dev = &pdev->dev;
1649 ret = snd_soc_register_card(card);
1651 dev_err(&pdev->dev, "Failed to register card\n");
1658 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1662 /* make sure any delayed work runs */
1663 for (i = 0; i < card->num_rtd; i++) {
1664 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1665 flush_delayed_work_sync(&rtd->delayed_work);
1668 /* remove auxiliary devices */
1669 for (i = 0; i < card->num_aux_devs; i++)
1670 soc_remove_aux_dev(card, i);
1672 /* remove and free each DAI */
1673 soc_remove_dai_links(card);
1675 soc_cleanup_card_debugfs(card);
1677 /* remove the card */
1681 snd_soc_dapm_free(&card->dapm);
1683 snd_card_free(card->snd_card);
1688 /* removes a socdev */
1689 static int soc_remove(struct platform_device *pdev)
1691 struct snd_soc_card *card = platform_get_drvdata(pdev);
1693 snd_soc_unregister_card(card);
1697 int snd_soc_poweroff(struct device *dev)
1699 struct snd_soc_card *card = dev_get_drvdata(dev);
1702 if (!card->instantiated)
1705 /* Flush out pmdown_time work - we actually do want to run it
1706 * now, we're shutting down so no imminent restart. */
1707 for (i = 0; i < card->num_rtd; i++) {
1708 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1709 flush_delayed_work_sync(&rtd->delayed_work);
1712 snd_soc_dapm_shutdown(card);
1716 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1718 const struct dev_pm_ops snd_soc_pm_ops = {
1719 .suspend = snd_soc_suspend,
1720 .resume = snd_soc_resume,
1721 .freeze = snd_soc_suspend,
1722 .thaw = snd_soc_resume,
1723 .poweroff = snd_soc_poweroff,
1724 .restore = snd_soc_resume,
1726 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1728 /* ASoC platform driver */
1729 static struct platform_driver soc_driver = {
1731 .name = "soc-audio",
1732 .owner = THIS_MODULE,
1733 .pm = &snd_soc_pm_ops,
1736 .remove = soc_remove,
1740 * snd_soc_codec_volatile_register: Report if a register is volatile.
1742 * @codec: CODEC to query.
1743 * @reg: Register to query.
1745 * Boolean function indiciating if a CODEC register is volatile.
1747 int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1750 if (codec->volatile_register)
1751 return codec->volatile_register(codec, reg);
1755 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1758 * snd_soc_codec_readable_register: Report if a register is readable.
1760 * @codec: CODEC to query.
1761 * @reg: Register to query.
1763 * Boolean function indicating if a CODEC register is readable.
1765 int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1768 if (codec->readable_register)
1769 return codec->readable_register(codec, reg);
1773 EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1776 * snd_soc_codec_writable_register: Report if a register is writable.
1778 * @codec: CODEC to query.
1779 * @reg: Register to query.
1781 * Boolean function indicating if a CODEC register is writable.
1783 int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1786 if (codec->writable_register)
1787 return codec->writable_register(codec, reg);
1791 EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1793 int snd_soc_platform_read(struct snd_soc_platform *platform,
1798 if (!platform->driver->read) {
1799 dev_err(platform->dev, "platform has no read back\n");
1803 ret = platform->driver->read(platform, reg);
1804 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
1805 trace_snd_soc_preg_read(platform, reg, ret);
1809 EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1811 int snd_soc_platform_write(struct snd_soc_platform *platform,
1812 unsigned int reg, unsigned int val)
1814 if (!platform->driver->write) {
1815 dev_err(platform->dev, "platform has no write back\n");
1819 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
1820 trace_snd_soc_preg_write(platform, reg, val);
1821 return platform->driver->write(platform, reg, val);
1823 EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1826 * snd_soc_new_ac97_codec - initailise AC97 device
1827 * @codec: audio codec
1828 * @ops: AC97 bus operations
1829 * @num: AC97 codec number
1831 * Initialises AC97 codec resources for use by ad-hoc devices only.
1833 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1834 struct snd_ac97_bus_ops *ops, int num)
1836 mutex_lock(&codec->mutex);
1838 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1839 if (codec->ac97 == NULL) {
1840 mutex_unlock(&codec->mutex);
1844 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1845 if (codec->ac97->bus == NULL) {
1848 mutex_unlock(&codec->mutex);
1852 codec->ac97->bus->ops = ops;
1853 codec->ac97->num = num;
1856 * Mark the AC97 device to be created by us. This way we ensure that the
1857 * device will be registered with the device subsystem later on.
1859 codec->ac97_created = 1;
1861 mutex_unlock(&codec->mutex);
1864 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1867 * snd_soc_free_ac97_codec - free AC97 codec device
1868 * @codec: audio codec
1870 * Frees AC97 codec device resources.
1872 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1874 mutex_lock(&codec->mutex);
1875 #ifdef CONFIG_SND_SOC_AC97_BUS
1876 soc_unregister_ac97_dai_link(codec);
1878 kfree(codec->ac97->bus);
1881 codec->ac97_created = 0;
1882 mutex_unlock(&codec->mutex);
1884 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1886 unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1890 ret = codec->read(codec, reg);
1891 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
1892 trace_snd_soc_reg_read(codec, reg, ret);
1896 EXPORT_SYMBOL_GPL(snd_soc_read);
1898 unsigned int snd_soc_write(struct snd_soc_codec *codec,
1899 unsigned int reg, unsigned int val)
1901 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
1902 trace_snd_soc_reg_write(codec, reg, val);
1903 return codec->write(codec, reg, val);
1905 EXPORT_SYMBOL_GPL(snd_soc_write);
1907 unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
1908 unsigned int reg, const void *data, size_t len)
1910 return codec->bulk_write_raw(codec, reg, data, len);
1912 EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
1915 * snd_soc_update_bits - update codec register bits
1916 * @codec: audio codec
1917 * @reg: codec register
1918 * @mask: register mask
1921 * Writes new register value.
1923 * Returns 1 for change, 0 for no change, or negative error code.
1925 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1926 unsigned int mask, unsigned int value)
1929 unsigned int old, new;
1932 if (codec->using_regmap) {
1933 ret = regmap_update_bits_check(codec->control_data, reg,
1934 mask, value, &change);
1936 ret = snd_soc_read(codec, reg);
1941 new = (old & ~mask) | (value & mask);
1942 change = old != new;
1944 ret = snd_soc_write(codec, reg, new);
1952 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1955 * snd_soc_update_bits_locked - update codec register bits
1956 * @codec: audio codec
1957 * @reg: codec register
1958 * @mask: register mask
1961 * Writes new register value, and takes the codec mutex.
1963 * Returns 1 for change else 0.
1965 int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1966 unsigned short reg, unsigned int mask,
1971 mutex_lock(&codec->mutex);
1972 change = snd_soc_update_bits(codec, reg, mask, value);
1973 mutex_unlock(&codec->mutex);
1977 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
1980 * snd_soc_test_bits - test register for change
1981 * @codec: audio codec
1982 * @reg: codec register
1983 * @mask: register mask
1986 * Tests a register with a new value and checks if the new value is
1987 * different from the old value.
1989 * Returns 1 for change else 0.
1991 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1992 unsigned int mask, unsigned int value)
1995 unsigned int old, new;
1997 old = snd_soc_read(codec, reg);
1998 new = (old & ~mask) | value;
1999 change = old != new;
2003 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2006 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2007 * @substream: the pcm substream
2008 * @hw: the hardware parameters
2010 * Sets the substream runtime hardware parameters.
2012 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2013 const struct snd_pcm_hardware *hw)
2015 struct snd_pcm_runtime *runtime = substream->runtime;
2016 runtime->hw.info = hw->info;
2017 runtime->hw.formats = hw->formats;
2018 runtime->hw.period_bytes_min = hw->period_bytes_min;
2019 runtime->hw.period_bytes_max = hw->period_bytes_max;
2020 runtime->hw.periods_min = hw->periods_min;
2021 runtime->hw.periods_max = hw->periods_max;
2022 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2023 runtime->hw.fifo_size = hw->fifo_size;
2026 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2029 * snd_soc_cnew - create new control
2030 * @_template: control template
2031 * @data: control private data
2032 * @long_name: control long name
2033 * @prefix: control name prefix
2035 * Create a new mixer control from a template control.
2037 * Returns 0 for success, else error.
2039 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2040 void *data, const char *long_name,
2043 struct snd_kcontrol_new template;
2044 struct snd_kcontrol *kcontrol;
2048 memcpy(&template, _template, sizeof(template));
2052 long_name = template.name;
2055 name_len = strlen(long_name) + strlen(prefix) + 2;
2056 name = kmalloc(name_len, GFP_KERNEL);
2060 snprintf(name, name_len, "%s %s", prefix, long_name);
2062 template.name = name;
2064 template.name = long_name;
2067 kcontrol = snd_ctl_new1(&template, data);
2073 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2075 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2076 const struct snd_kcontrol_new *controls, int num_controls,
2077 const char *prefix, void *data)
2081 for (i = 0; i < num_controls; i++) {
2082 const struct snd_kcontrol_new *control = &controls[i];
2083 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2084 control->name, prefix));
2086 dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2095 * snd_soc_add_codec_controls - add an array of controls to a codec.
2096 * Convenience function to add a list of controls. Many codecs were
2097 * duplicating this code.
2099 * @codec: codec to add controls to
2100 * @controls: array of controls to add
2101 * @num_controls: number of elements in the array
2103 * Return 0 for success, else error.
2105 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2106 const struct snd_kcontrol_new *controls, int num_controls)
2108 struct snd_card *card = codec->card->snd_card;
2110 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2111 codec->name_prefix, codec);
2113 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2116 * snd_soc_add_platform_controls - add an array of controls to a platform.
2117 * Convenience function to add a list of controls.
2119 * @platform: platform to add controls to
2120 * @controls: array of controls to add
2121 * @num_controls: number of elements in the array
2123 * Return 0 for success, else error.
2125 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2126 const struct snd_kcontrol_new *controls, int num_controls)
2128 struct snd_card *card = platform->card->snd_card;
2130 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2133 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2136 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2137 * Convenience function to add a list of controls.
2139 * @soc_card: SoC card to add controls to
2140 * @controls: array of controls to add
2141 * @num_controls: number of elements in the array
2143 * Return 0 for success, else error.
2145 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2146 const struct snd_kcontrol_new *controls, int num_controls)
2148 struct snd_card *card = soc_card->snd_card;
2150 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2153 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2156 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2157 * Convienience function to add a list of controls.
2159 * @dai: DAI to add controls to
2160 * @controls: array of controls to add
2161 * @num_controls: number of elements in the array
2163 * Return 0 for success, else error.
2165 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2166 const struct snd_kcontrol_new *controls, int num_controls)
2168 struct snd_card *card = dai->card->snd_card;
2170 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2173 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2176 * snd_soc_info_enum_double - enumerated double mixer info callback
2177 * @kcontrol: mixer control
2178 * @uinfo: control element information
2180 * Callback to provide information about a double enumerated
2183 * Returns 0 for success.
2185 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2186 struct snd_ctl_elem_info *uinfo)
2188 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2190 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2191 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2192 uinfo->value.enumerated.items = e->max;
2194 if (uinfo->value.enumerated.item > e->max - 1)
2195 uinfo->value.enumerated.item = e->max - 1;
2196 strcpy(uinfo->value.enumerated.name,
2197 e->texts[uinfo->value.enumerated.item]);
2200 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2203 * snd_soc_get_enum_double - enumerated double mixer get callback
2204 * @kcontrol: mixer control
2205 * @ucontrol: control element information
2207 * Callback to get the value of a double enumerated mixer.
2209 * Returns 0 for success.
2211 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2212 struct snd_ctl_elem_value *ucontrol)
2214 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2215 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2216 unsigned int val, bitmask;
2218 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2220 val = snd_soc_read(codec, e->reg);
2221 ucontrol->value.enumerated.item[0]
2222 = (val >> e->shift_l) & (bitmask - 1);
2223 if (e->shift_l != e->shift_r)
2224 ucontrol->value.enumerated.item[1] =
2225 (val >> e->shift_r) & (bitmask - 1);
2229 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2232 * snd_soc_put_enum_double - enumerated double mixer put callback
2233 * @kcontrol: mixer control
2234 * @ucontrol: control element information
2236 * Callback to set the value of a double enumerated mixer.
2238 * Returns 0 for success.
2240 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2241 struct snd_ctl_elem_value *ucontrol)
2243 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2244 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2246 unsigned int mask, bitmask;
2248 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2250 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2252 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2253 mask = (bitmask - 1) << e->shift_l;
2254 if (e->shift_l != e->shift_r) {
2255 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2257 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2258 mask |= (bitmask - 1) << e->shift_r;
2261 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2263 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2266 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2267 * @kcontrol: mixer control
2268 * @ucontrol: control element information
2270 * Callback to get the value of a double semi enumerated mixer.
2272 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2273 * used for handling bitfield coded enumeration for example.
2275 * Returns 0 for success.
2277 int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2278 struct snd_ctl_elem_value *ucontrol)
2280 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2281 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2282 unsigned int reg_val, val, mux;
2284 reg_val = snd_soc_read(codec, e->reg);
2285 val = (reg_val >> e->shift_l) & e->mask;
2286 for (mux = 0; mux < e->max; mux++) {
2287 if (val == e->values[mux])
2290 ucontrol->value.enumerated.item[0] = mux;
2291 if (e->shift_l != e->shift_r) {
2292 val = (reg_val >> e->shift_r) & e->mask;
2293 for (mux = 0; mux < e->max; mux++) {
2294 if (val == e->values[mux])
2297 ucontrol->value.enumerated.item[1] = mux;
2302 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2305 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2306 * @kcontrol: mixer control
2307 * @ucontrol: control element information
2309 * Callback to set the value of a double semi enumerated mixer.
2311 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2312 * used for handling bitfield coded enumeration for example.
2314 * Returns 0 for success.
2316 int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2317 struct snd_ctl_elem_value *ucontrol)
2319 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2320 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2324 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2326 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2327 mask = e->mask << e->shift_l;
2328 if (e->shift_l != e->shift_r) {
2329 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2331 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2332 mask |= e->mask << e->shift_r;
2335 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2337 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2340 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2341 * @kcontrol: mixer control
2342 * @uinfo: control element information
2344 * Callback to provide information about an external enumerated
2347 * Returns 0 for success.
2349 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2350 struct snd_ctl_elem_info *uinfo)
2352 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2354 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2356 uinfo->value.enumerated.items = e->max;
2358 if (uinfo->value.enumerated.item > e->max - 1)
2359 uinfo->value.enumerated.item = e->max - 1;
2360 strcpy(uinfo->value.enumerated.name,
2361 e->texts[uinfo->value.enumerated.item]);
2364 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2367 * snd_soc_info_volsw_ext - external single mixer info callback
2368 * @kcontrol: mixer control
2369 * @uinfo: control element information
2371 * Callback to provide information about a single external mixer control.
2373 * Returns 0 for success.
2375 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2376 struct snd_ctl_elem_info *uinfo)
2378 int max = kcontrol->private_value;
2380 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2381 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2383 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2386 uinfo->value.integer.min = 0;
2387 uinfo->value.integer.max = max;
2390 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2393 * snd_soc_info_volsw - single mixer info callback
2394 * @kcontrol: mixer control
2395 * @uinfo: control element information
2397 * Callback to provide information about a single mixer control, or a double
2398 * mixer control that spans 2 registers.
2400 * Returns 0 for success.
2402 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2403 struct snd_ctl_elem_info *uinfo)
2405 struct soc_mixer_control *mc =
2406 (struct soc_mixer_control *)kcontrol->private_value;
2409 if (!mc->platform_max)
2410 mc->platform_max = mc->max;
2411 platform_max = mc->platform_max;
2413 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2414 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2416 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2418 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2419 uinfo->value.integer.min = 0;
2420 uinfo->value.integer.max = platform_max;
2423 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2426 * snd_soc_get_volsw - single mixer get callback
2427 * @kcontrol: mixer control
2428 * @ucontrol: control element information
2430 * Callback to get the value of a single mixer control, or a double mixer
2431 * control that spans 2 registers.
2433 * Returns 0 for success.
2435 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2436 struct snd_ctl_elem_value *ucontrol)
2438 struct soc_mixer_control *mc =
2439 (struct soc_mixer_control *)kcontrol->private_value;
2440 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2441 unsigned int reg = mc->reg;
2442 unsigned int reg2 = mc->rreg;
2443 unsigned int shift = mc->shift;
2444 unsigned int rshift = mc->rshift;
2446 unsigned int mask = (1 << fls(max)) - 1;
2447 unsigned int invert = mc->invert;
2449 ucontrol->value.integer.value[0] =
2450 (snd_soc_read(codec, reg) >> shift) & mask;
2452 ucontrol->value.integer.value[0] =
2453 max - ucontrol->value.integer.value[0];
2455 if (snd_soc_volsw_is_stereo(mc)) {
2457 ucontrol->value.integer.value[1] =
2458 (snd_soc_read(codec, reg) >> rshift) & mask;
2460 ucontrol->value.integer.value[1] =
2461 (snd_soc_read(codec, reg2) >> shift) & mask;
2463 ucontrol->value.integer.value[1] =
2464 max - ucontrol->value.integer.value[1];
2469 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2472 * snd_soc_put_volsw - single mixer put callback
2473 * @kcontrol: mixer control
2474 * @ucontrol: control element information
2476 * Callback to set the value of a single mixer control, or a double mixer
2477 * control that spans 2 registers.
2479 * Returns 0 for success.
2481 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2482 struct snd_ctl_elem_value *ucontrol)
2484 struct soc_mixer_control *mc =
2485 (struct soc_mixer_control *)kcontrol->private_value;
2486 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2487 unsigned int reg = mc->reg;
2488 unsigned int reg2 = mc->rreg;
2489 unsigned int shift = mc->shift;
2490 unsigned int rshift = mc->rshift;
2492 unsigned int mask = (1 << fls(max)) - 1;
2493 unsigned int invert = mc->invert;
2496 unsigned int val2 = 0;
2497 unsigned int val, val_mask;
2499 val = (ucontrol->value.integer.value[0] & mask);
2502 val_mask = mask << shift;
2504 if (snd_soc_volsw_is_stereo(mc)) {
2505 val2 = (ucontrol->value.integer.value[1] & mask);
2509 val_mask |= mask << rshift;
2510 val |= val2 << rshift;
2512 val2 = val2 << shift;
2516 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2521 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2525 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2528 * snd_soc_info_volsw_s8 - signed mixer info callback
2529 * @kcontrol: mixer control
2530 * @uinfo: control element information
2532 * Callback to provide information about a signed mixer control.
2534 * Returns 0 for success.
2536 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2537 struct snd_ctl_elem_info *uinfo)
2539 struct soc_mixer_control *mc =
2540 (struct soc_mixer_control *)kcontrol->private_value;
2544 if (!mc->platform_max)
2545 mc->platform_max = mc->max;
2546 platform_max = mc->platform_max;
2548 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2550 uinfo->value.integer.min = 0;
2551 uinfo->value.integer.max = platform_max - min;
2554 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2557 * snd_soc_get_volsw_s8 - signed mixer get callback
2558 * @kcontrol: mixer control
2559 * @ucontrol: control element information
2561 * Callback to get the value of a signed mixer control.
2563 * Returns 0 for success.
2565 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2566 struct snd_ctl_elem_value *ucontrol)
2568 struct soc_mixer_control *mc =
2569 (struct soc_mixer_control *)kcontrol->private_value;
2570 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2571 unsigned int reg = mc->reg;
2573 int val = snd_soc_read(codec, reg);
2575 ucontrol->value.integer.value[0] =
2576 ((signed char)(val & 0xff))-min;
2577 ucontrol->value.integer.value[1] =
2578 ((signed char)((val >> 8) & 0xff))-min;
2581 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2584 * snd_soc_put_volsw_sgn - signed mixer put callback
2585 * @kcontrol: mixer control
2586 * @ucontrol: control element information
2588 * Callback to set the value of a signed mixer control.
2590 * Returns 0 for success.
2592 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2593 struct snd_ctl_elem_value *ucontrol)
2595 struct soc_mixer_control *mc =
2596 (struct soc_mixer_control *)kcontrol->private_value;
2597 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2598 unsigned int reg = mc->reg;
2602 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2603 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2605 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2607 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2610 * snd_soc_limit_volume - Set new limit to an existing volume control.
2612 * @codec: where to look for the control
2613 * @name: Name of the control
2614 * @max: new maximum limit
2616 * Return 0 for success, else error.
2618 int snd_soc_limit_volume(struct snd_soc_codec *codec,
2619 const char *name, int max)
2621 struct snd_card *card = codec->card->snd_card;
2622 struct snd_kcontrol *kctl;
2623 struct soc_mixer_control *mc;
2627 /* Sanity check for name and max */
2628 if (unlikely(!name || max <= 0))
2631 list_for_each_entry(kctl, &card->controls, list) {
2632 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2638 mc = (struct soc_mixer_control *)kctl->private_value;
2639 if (max <= mc->max) {
2640 mc->platform_max = max;
2646 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2649 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2650 * mixer info callback
2651 * @kcontrol: mixer control
2652 * @uinfo: control element information
2654 * Returns 0 for success.
2656 int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2657 struct snd_ctl_elem_info *uinfo)
2659 struct soc_mixer_control *mc =
2660 (struct soc_mixer_control *)kcontrol->private_value;
2664 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2666 uinfo->value.integer.min = 0;
2667 uinfo->value.integer.max = max-min;
2671 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2674 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2675 * mixer get callback
2676 * @kcontrol: mixer control
2677 * @uinfo: control element information
2679 * Returns 0 for success.
2681 int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2682 struct snd_ctl_elem_value *ucontrol)
2684 struct soc_mixer_control *mc =
2685 (struct soc_mixer_control *)kcontrol->private_value;
2686 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2687 unsigned int mask = (1<<mc->shift)-1;
2689 int val = snd_soc_read(codec, mc->reg) & mask;
2690 int valr = snd_soc_read(codec, mc->rreg) & mask;
2692 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2693 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
2696 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2699 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2700 * mixer put callback
2701 * @kcontrol: mixer control
2702 * @uinfo: control element information
2704 * Returns 0 for success.
2706 int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2707 struct snd_ctl_elem_value *ucontrol)
2709 struct soc_mixer_control *mc =
2710 (struct soc_mixer_control *)kcontrol->private_value;
2711 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2712 unsigned int mask = (1<<mc->shift)-1;
2715 unsigned int val, valr, oval, ovalr;
2717 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2719 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2722 oval = snd_soc_read(codec, mc->reg) & mask;
2723 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2727 ret = snd_soc_write(codec, mc->reg, val);
2731 if (ovalr != valr) {
2732 ret = snd_soc_write(codec, mc->rreg, valr);
2739 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2741 int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
2742 struct snd_ctl_elem_info *uinfo)
2744 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2745 struct soc_bytes *params = (void *)kcontrol->private_value;
2747 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
2748 uinfo->count = params->num_regs * codec->val_bytes;
2752 EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
2754 int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
2755 struct snd_ctl_elem_value *ucontrol)
2757 struct soc_bytes *params = (void *)kcontrol->private_value;
2758 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2761 if (codec->using_regmap)
2762 ret = regmap_raw_read(codec->control_data, params->base,
2763 ucontrol->value.bytes.data,
2764 params->num_regs * codec->val_bytes);
2768 /* Hide any masked bytes to ensure consistent data reporting */
2769 if (ret == 0 && params->mask) {
2770 switch (codec->val_bytes) {
2772 ucontrol->value.bytes.data[0] &= ~params->mask;
2775 ((u16 *)(&ucontrol->value.bytes.data))[0]
2779 ((u32 *)(&ucontrol->value.bytes.data))[0]
2789 EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
2791 int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
2792 struct snd_ctl_elem_value *ucontrol)
2794 struct soc_bytes *params = (void *)kcontrol->private_value;
2795 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2800 if (!codec->using_regmap)
2803 data = ucontrol->value.bytes.data;
2804 len = params->num_regs * codec->val_bytes;
2807 * If we've got a mask then we need to preserve the register
2808 * bits. We shouldn't modify the incoming data so take a
2812 ret = regmap_read(codec->control_data, params->base, &val);
2816 val &= params->mask;
2818 data = kmemdup(data, len, GFP_KERNEL);
2822 switch (codec->val_bytes) {
2824 ((u8 *)data)[0] &= ~params->mask;
2825 ((u8 *)data)[0] |= val;
2828 ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
2829 ((u16 *)data)[0] |= cpu_to_be16(val);
2832 ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
2833 ((u32 *)data)[0] |= cpu_to_be32(val);
2840 ret = regmap_raw_write(codec->control_data, params->base,
2848 EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
2851 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2853 * @clk_id: DAI specific clock ID
2854 * @freq: new clock frequency in Hz
2855 * @dir: new clock direction - input/output.
2857 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2859 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2860 unsigned int freq, int dir)
2862 if (dai->driver && dai->driver->ops->set_sysclk)
2863 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2864 else if (dai->codec && dai->codec->driver->set_sysclk)
2865 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
2870 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2873 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2875 * @clk_id: DAI specific clock ID
2876 * @source: Source for the clock
2877 * @freq: new clock frequency in Hz
2878 * @dir: new clock direction - input/output.
2880 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2882 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2883 int source, unsigned int freq, int dir)
2885 if (codec->driver->set_sysclk)
2886 return codec->driver->set_sysclk(codec, clk_id, source,
2891 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2894 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2896 * @div_id: DAI specific clock divider ID
2897 * @div: new clock divisor.
2899 * Configures the clock dividers. This is used to derive the best DAI bit and
2900 * frame clocks from the system or master clock. It's best to set the DAI bit
2901 * and frame clocks as low as possible to save system power.
2903 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2904 int div_id, int div)
2906 if (dai->driver && dai->driver->ops->set_clkdiv)
2907 return dai->driver->ops->set_clkdiv(dai, div_id, div);
2911 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2914 * snd_soc_dai_set_pll - configure DAI PLL.
2916 * @pll_id: DAI specific PLL ID
2917 * @source: DAI specific source for the PLL
2918 * @freq_in: PLL input clock frequency in Hz
2919 * @freq_out: requested PLL output clock frequency in Hz
2921 * Configures and enables PLL to generate output clock based on input clock.
2923 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2924 unsigned int freq_in, unsigned int freq_out)
2926 if (dai->driver && dai->driver->ops->set_pll)
2927 return dai->driver->ops->set_pll(dai, pll_id, source,
2929 else if (dai->codec && dai->codec->driver->set_pll)
2930 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2935 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2938 * snd_soc_codec_set_pll - configure codec PLL.
2940 * @pll_id: DAI specific PLL ID
2941 * @source: DAI specific source for the PLL
2942 * @freq_in: PLL input clock frequency in Hz
2943 * @freq_out: requested PLL output clock frequency in Hz
2945 * Configures and enables PLL to generate output clock based on input clock.
2947 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2948 unsigned int freq_in, unsigned int freq_out)
2950 if (codec->driver->set_pll)
2951 return codec->driver->set_pll(codec, pll_id, source,
2956 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2959 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2961 * @fmt: SND_SOC_DAIFMT_ format value.
2963 * Configures the DAI hardware format and clocking.
2965 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2967 if (dai->driver == NULL)
2969 if (dai->driver->ops->set_fmt == NULL)
2971 return dai->driver->ops->set_fmt(dai, fmt);
2973 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2976 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2978 * @tx_mask: bitmask representing active TX slots.
2979 * @rx_mask: bitmask representing active RX slots.
2980 * @slots: Number of slots in use.
2981 * @slot_width: Width in bits for each slot.
2983 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2986 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2987 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2989 if (dai->driver && dai->driver->ops->set_tdm_slot)
2990 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2995 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2998 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3000 * @tx_num: how many TX channels
3001 * @tx_slot: pointer to an array which imply the TX slot number channel
3003 * @rx_num: how many RX channels
3004 * @rx_slot: pointer to an array which imply the RX slot number channel
3007 * configure the relationship between channel number and TDM slot number.
3009 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3010 unsigned int tx_num, unsigned int *tx_slot,
3011 unsigned int rx_num, unsigned int *rx_slot)
3013 if (dai->driver && dai->driver->ops->set_channel_map)
3014 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3019 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3022 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3024 * @tristate: tristate enable
3026 * Tristates the DAI so that others can use it.
3028 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3030 if (dai->driver && dai->driver->ops->set_tristate)
3031 return dai->driver->ops->set_tristate(dai, tristate);
3035 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3038 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3040 * @mute: mute enable
3042 * Mutes the DAI DAC.
3044 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3046 if (dai->driver && dai->driver->ops->digital_mute)
3047 return dai->driver->ops->digital_mute(dai, mute);
3051 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3054 * snd_soc_register_card - Register a card with the ASoC core
3056 * @card: Card to register
3059 int snd_soc_register_card(struct snd_soc_card *card)
3063 if (!card->name || !card->dev)
3066 for (i = 0; i < card->num_links; i++) {
3067 struct snd_soc_dai_link *link = &card->dai_link[i];
3070 * Codec must be specified by 1 of name or OF node,
3071 * not both or neither.
3073 if (!!link->codec_name == !!link->codec_of_node) {
3075 "Neither/both codec name/of_node are set for %s\n",
3081 * Platform may be specified by either name or OF node, but
3082 * can be left unspecified, and a dummy platform will be used.
3084 if (link->platform_name && link->platform_of_node) {
3086 "Both platform name/of_node are set for %s\n", link->name);
3091 * CPU DAI must be specified by 1 of name or OF node,
3092 * not both or neither.
3094 if (!!link->cpu_dai_name == !!link->cpu_dai_of_node) {
3096 "Neither/both cpu_dai name/of_node are set for %s\n",
3102 dev_set_drvdata(card->dev, card);
3104 snd_soc_initialize_card_lists(card);
3106 soc_init_card_debugfs(card);
3108 card->rtd = devm_kzalloc(card->dev,
3109 sizeof(struct snd_soc_pcm_runtime) *
3110 (card->num_links + card->num_aux_devs),
3112 if (card->rtd == NULL)
3114 card->rtd_aux = &card->rtd[card->num_links];
3116 for (i = 0; i < card->num_links; i++)
3117 card->rtd[i].dai_link = &card->dai_link[i];
3119 INIT_LIST_HEAD(&card->list);
3120 INIT_LIST_HEAD(&card->dapm_dirty);
3121 card->instantiated = 0;
3122 mutex_init(&card->mutex);
3124 mutex_lock(&client_mutex);
3125 list_add(&card->list, &card_list);
3126 snd_soc_instantiate_cards();
3127 mutex_unlock(&client_mutex);
3129 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
3133 EXPORT_SYMBOL_GPL(snd_soc_register_card);
3136 * snd_soc_unregister_card - Unregister a card with the ASoC core
3138 * @card: Card to unregister
3141 int snd_soc_unregister_card(struct snd_soc_card *card)
3143 if (card->instantiated)
3144 soc_cleanup_card_resources(card);
3145 mutex_lock(&client_mutex);
3146 list_del(&card->list);
3147 mutex_unlock(&client_mutex);
3148 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3152 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3155 * Simplify DAI link configuration by removing ".-1" from device names
3156 * and sanitizing names.
3158 static char *fmt_single_name(struct device *dev, int *id)
3160 char *found, name[NAME_SIZE];
3163 if (dev_name(dev) == NULL)
3166 strlcpy(name, dev_name(dev), NAME_SIZE);
3168 /* are we a "%s.%d" name (platform and SPI components) */
3169 found = strstr(name, dev->driver->name);
3172 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3174 /* discard ID from name if ID == -1 */
3176 found[strlen(dev->driver->name)] = '\0';
3180 /* I2C component devices are named "bus-addr" */
3181 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3182 char tmp[NAME_SIZE];
3184 /* create unique ID number from I2C addr and bus */
3185 *id = ((id1 & 0xffff) << 16) + id2;
3187 /* sanitize component name for DAI link creation */
3188 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3189 strlcpy(name, tmp, NAME_SIZE);
3194 return kstrdup(name, GFP_KERNEL);
3198 * Simplify DAI link naming for single devices with multiple DAIs by removing
3199 * any ".-1" and using the DAI name (instead of device name).
3201 static inline char *fmt_multiple_name(struct device *dev,
3202 struct snd_soc_dai_driver *dai_drv)
3204 if (dai_drv->name == NULL) {
3205 pr_err("asoc: error - multiple DAI %s registered with no name\n",
3210 return kstrdup(dai_drv->name, GFP_KERNEL);
3214 * snd_soc_register_dai - Register a DAI with the ASoC core
3216 * @dai: DAI to register
3218 int snd_soc_register_dai(struct device *dev,
3219 struct snd_soc_dai_driver *dai_drv)
3221 struct snd_soc_dai *dai;
3223 dev_dbg(dev, "dai register %s\n", dev_name(dev));
3225 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3229 /* create DAI component name */
3230 dai->name = fmt_single_name(dev, &dai->id);
3231 if (dai->name == NULL) {
3237 dai->driver = dai_drv;
3238 if (!dai->driver->ops)
3239 dai->driver->ops = &null_dai_ops;
3241 mutex_lock(&client_mutex);
3242 list_add(&dai->list, &dai_list);
3243 snd_soc_instantiate_cards();
3244 mutex_unlock(&client_mutex);
3246 pr_debug("Registered DAI '%s'\n", dai->name);
3250 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3253 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3255 * @dai: DAI to unregister
3257 void snd_soc_unregister_dai(struct device *dev)
3259 struct snd_soc_dai *dai;
3261 list_for_each_entry(dai, &dai_list, list) {
3262 if (dev == dai->dev)
3268 mutex_lock(&client_mutex);
3269 list_del(&dai->list);
3270 mutex_unlock(&client_mutex);
3272 pr_debug("Unregistered DAI '%s'\n", dai->name);
3276 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3279 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3281 * @dai: Array of DAIs to register
3282 * @count: Number of DAIs
3284 int snd_soc_register_dais(struct device *dev,
3285 struct snd_soc_dai_driver *dai_drv, size_t count)
3287 struct snd_soc_dai *dai;
3290 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
3292 for (i = 0; i < count; i++) {
3294 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3300 /* create DAI component name */
3301 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3302 if (dai->name == NULL) {
3309 dai->driver = &dai_drv[i];
3310 if (dai->driver->id)
3311 dai->id = dai->driver->id;
3314 if (!dai->driver->ops)
3315 dai->driver->ops = &null_dai_ops;
3317 mutex_lock(&client_mutex);
3318 list_add(&dai->list, &dai_list);
3319 mutex_unlock(&client_mutex);
3321 pr_debug("Registered DAI '%s'\n", dai->name);
3324 mutex_lock(&client_mutex);
3325 snd_soc_instantiate_cards();
3326 mutex_unlock(&client_mutex);
3330 for (i--; i >= 0; i--)
3331 snd_soc_unregister_dai(dev);
3335 EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3338 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3340 * @dai: Array of DAIs to unregister
3341 * @count: Number of DAIs
3343 void snd_soc_unregister_dais(struct device *dev, size_t count)
3347 for (i = 0; i < count; i++)
3348 snd_soc_unregister_dai(dev);
3350 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3353 * snd_soc_register_platform - Register a platform with the ASoC core
3355 * @platform: platform to register
3357 int snd_soc_register_platform(struct device *dev,
3358 struct snd_soc_platform_driver *platform_drv)
3360 struct snd_soc_platform *platform;
3362 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3364 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3365 if (platform == NULL)
3368 /* create platform component name */
3369 platform->name = fmt_single_name(dev, &platform->id);
3370 if (platform->name == NULL) {
3375 platform->dev = dev;
3376 platform->driver = platform_drv;
3377 platform->dapm.dev = dev;
3378 platform->dapm.platform = platform;
3379 platform->dapm.stream_event = platform_drv->stream_event;
3380 mutex_init(&platform->mutex);
3382 mutex_lock(&client_mutex);
3383 list_add(&platform->list, &platform_list);
3384 snd_soc_instantiate_cards();
3385 mutex_unlock(&client_mutex);
3387 pr_debug("Registered platform '%s'\n", platform->name);
3391 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3394 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3396 * @platform: platform to unregister
3398 void snd_soc_unregister_platform(struct device *dev)
3400 struct snd_soc_platform *platform;
3402 list_for_each_entry(platform, &platform_list, list) {
3403 if (dev == platform->dev)
3409 mutex_lock(&client_mutex);
3410 list_del(&platform->list);
3411 mutex_unlock(&client_mutex);
3413 pr_debug("Unregistered platform '%s'\n", platform->name);
3414 kfree(platform->name);
3417 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3419 static u64 codec_format_map[] = {
3420 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3421 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3422 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3423 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3424 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3425 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3426 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3427 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3428 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3429 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3430 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3431 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3432 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3433 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3434 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3435 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3438 /* Fix up the DAI formats for endianness: codecs don't actually see
3439 * the endianness of the data but we're using the CPU format
3440 * definitions which do need to include endianness so we ensure that
3441 * codec DAIs always have both big and little endian variants set.
3443 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3447 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3448 if (stream->formats & codec_format_map[i])
3449 stream->formats |= codec_format_map[i];
3453 * snd_soc_register_codec - Register a codec with the ASoC core
3455 * @codec: codec to register
3457 int snd_soc_register_codec(struct device *dev,
3458 const struct snd_soc_codec_driver *codec_drv,
3459 struct snd_soc_dai_driver *dai_drv,
3463 struct snd_soc_codec *codec;
3466 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3468 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3472 /* create CODEC component name */
3473 codec->name = fmt_single_name(dev, &codec->id);
3474 if (codec->name == NULL) {
3479 if (codec_drv->compress_type)
3480 codec->compress_type = codec_drv->compress_type;
3482 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3484 codec->write = codec_drv->write;
3485 codec->read = codec_drv->read;
3486 codec->volatile_register = codec_drv->volatile_register;
3487 codec->readable_register = codec_drv->readable_register;
3488 codec->writable_register = codec_drv->writable_register;
3489 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
3490 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3491 codec->dapm.dev = dev;
3492 codec->dapm.codec = codec;
3493 codec->dapm.seq_notifier = codec_drv->seq_notifier;
3494 codec->dapm.stream_event = codec_drv->stream_event;
3496 codec->driver = codec_drv;
3497 codec->num_dai = num_dai;
3498 mutex_init(&codec->mutex);
3500 /* allocate CODEC register cache */
3501 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3502 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
3503 codec->reg_size = reg_size;
3504 /* it is necessary to make a copy of the default register cache
3505 * because in the case of using a compression type that requires
3506 * the default register cache to be marked as __devinitconst the
3507 * kernel might have freed the array by the time we initialize
3510 if (codec_drv->reg_cache_default) {
3511 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3512 reg_size, GFP_KERNEL);
3513 if (!codec->reg_def_copy) {
3520 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3521 if (!codec->volatile_register)
3522 codec->volatile_register = snd_soc_default_volatile_register;
3523 if (!codec->readable_register)
3524 codec->readable_register = snd_soc_default_readable_register;
3525 if (!codec->writable_register)
3526 codec->writable_register = snd_soc_default_writable_register;
3529 for (i = 0; i < num_dai; i++) {
3530 fixup_codec_formats(&dai_drv[i].playback);
3531 fixup_codec_formats(&dai_drv[i].capture);
3534 /* register any DAIs */
3536 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3541 mutex_lock(&client_mutex);
3542 list_add(&codec->list, &codec_list);
3543 snd_soc_instantiate_cards();
3544 mutex_unlock(&client_mutex);
3546 pr_debug("Registered codec '%s'\n", codec->name);
3550 kfree(codec->reg_def_copy);
3551 codec->reg_def_copy = NULL;
3556 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3559 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3561 * @codec: codec to unregister
3563 void snd_soc_unregister_codec(struct device *dev)
3565 struct snd_soc_codec *codec;
3568 list_for_each_entry(codec, &codec_list, list) {
3569 if (dev == codec->dev)
3576 for (i = 0; i < codec->num_dai; i++)
3577 snd_soc_unregister_dai(dev);
3579 mutex_lock(&client_mutex);
3580 list_del(&codec->list);
3581 mutex_unlock(&client_mutex);
3583 pr_debug("Unregistered codec '%s'\n", codec->name);
3585 snd_soc_cache_exit(codec);
3586 kfree(codec->reg_def_copy);
3590 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3592 /* Retrieve a card's name from device tree */
3593 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3594 const char *propname)
3596 struct device_node *np = card->dev->of_node;
3599 ret = of_property_read_string_index(np, propname, 0, &card->name);
3601 * EINVAL means the property does not exist. This is fine providing
3602 * card->name was previously set, which is checked later in
3603 * snd_soc_register_card.
3605 if (ret < 0 && ret != -EINVAL) {
3607 "Property '%s' could not be read: %d\n",
3614 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3616 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3617 const char *propname)
3619 struct device_node *np = card->dev->of_node;
3621 struct snd_soc_dapm_route *routes;
3624 num_routes = of_property_count_strings(np, propname);
3625 if (num_routes & 1) {
3627 "Property '%s's length is not even\n",
3634 "Property '%s's length is zero\n",
3639 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3643 "Could not allocate DAPM route table\n");
3647 for (i = 0; i < num_routes; i++) {
3648 ret = of_property_read_string_index(np, propname,
3649 2 * i, &routes[i].sink);
3652 "Property '%s' index %d could not be read: %d\n",
3653 propname, 2 * i, ret);
3656 ret = of_property_read_string_index(np, propname,
3657 (2 * i) + 1, &routes[i].source);
3660 "Property '%s' index %d could not be read: %d\n",
3661 propname, (2 * i) + 1, ret);
3666 card->num_dapm_routes = num_routes;
3667 card->dapm_routes = routes;
3671 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3673 static int __init snd_soc_init(void)
3675 #ifdef CONFIG_DEBUG_FS
3676 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3677 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
3678 pr_warn("ASoC: Failed to create debugfs directory\n");
3679 snd_soc_debugfs_root = NULL;
3682 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
3684 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3686 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
3688 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
3690 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
3691 &platform_list_fops))
3692 pr_warn("ASoC: Failed to create platform list debugfs file\n");
3695 snd_soc_util_init();
3697 return platform_driver_register(&soc_driver);
3699 module_init(snd_soc_init);
3701 static void __exit snd_soc_exit(void)
3703 snd_soc_util_exit();
3705 #ifdef CONFIG_DEBUG_FS
3706 debugfs_remove_recursive(snd_soc_debugfs_root);
3708 platform_driver_unregister(&soc_driver);
3710 module_exit(snd_soc_exit);
3712 /* Module information */
3713 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3714 MODULE_DESCRIPTION("ALSA SoC Core");
3715 MODULE_LICENSE("GPL");
3716 MODULE_ALIAS("platform:soc-audio");