2 * soc-core.c -- ALSA SoC Audio Layer
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
30 #include <linux/bitops.h>
31 #include <linux/debugfs.h>
32 #include <linux/platform_device.h>
33 #include <linux/ctype.h>
34 #include <linux/slab.h>
36 #include <sound/ac97_codec.h>
37 #include <sound/core.h>
38 #include <sound/jack.h>
39 #include <sound/pcm.h>
40 #include <sound/pcm_params.h>
41 #include <sound/soc.h>
42 #include <sound/soc-dpcm.h>
43 #include <sound/initval.h>
45 #define CREATE_TRACE_POINTS
46 #include <trace/events/asoc.h>
50 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
52 #ifdef CONFIG_DEBUG_FS
53 struct dentry *snd_soc_debugfs_root;
54 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
57 static DEFINE_MUTEX(client_mutex);
58 static LIST_HEAD(dai_list);
59 static LIST_HEAD(platform_list);
60 static LIST_HEAD(codec_list);
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++) {
571 if (card->rtd[i].dai_link->ignore_suspend)
574 snd_soc_dapm_stream_event(&card->rtd[i],
575 SNDRV_PCM_STREAM_PLAYBACK,
576 SND_SOC_DAPM_STREAM_SUSPEND);
578 snd_soc_dapm_stream_event(&card->rtd[i],
579 SNDRV_PCM_STREAM_CAPTURE,
580 SND_SOC_DAPM_STREAM_SUSPEND);
583 /* suspend all CODECs */
584 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
585 /* If there are paths active then the CODEC will be held with
586 * bias _ON and should not be suspended. */
587 if (!codec->suspended && codec->driver->suspend) {
588 switch (codec->dapm.bias_level) {
589 case SND_SOC_BIAS_STANDBY:
591 * If the CODEC is capable of idle
592 * bias off then being in STANDBY
593 * means it's doing something,
594 * otherwise fall through.
596 if (codec->dapm.idle_bias_off) {
598 "idle_bias_off CODEC on over suspend\n");
601 case SND_SOC_BIAS_OFF:
602 codec->driver->suspend(codec);
603 codec->suspended = 1;
604 codec->cache_sync = 1;
607 dev_dbg(codec->dev, "CODEC is on over suspend\n");
613 for (i = 0; i < card->num_rtd; i++) {
614 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
616 if (card->rtd[i].dai_link->ignore_suspend)
619 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
620 cpu_dai->driver->suspend(cpu_dai);
623 if (card->suspend_post)
624 card->suspend_post(card);
628 EXPORT_SYMBOL_GPL(snd_soc_suspend);
630 /* deferred resume work, so resume can complete before we finished
631 * setting our codec back up, which can be very slow on I2C
633 static void soc_resume_deferred(struct work_struct *work)
635 struct snd_soc_card *card =
636 container_of(work, struct snd_soc_card, deferred_resume_work);
637 struct snd_soc_codec *codec;
640 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
641 * so userspace apps are blocked from touching us
644 dev_dbg(card->dev, "starting resume work\n");
646 /* Bring us up into D2 so that DAPM starts enabling things */
647 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
649 if (card->resume_pre)
650 card->resume_pre(card);
652 /* resume AC97 DAIs */
653 for (i = 0; i < card->num_rtd; i++) {
654 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
656 if (card->rtd[i].dai_link->ignore_suspend)
659 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
660 cpu_dai->driver->resume(cpu_dai);
663 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
664 /* If the CODEC was idle over suspend then it will have been
665 * left with bias OFF or STANDBY and suspended so we must now
666 * resume. Otherwise the suspend was suppressed.
668 if (codec->driver->resume && codec->suspended) {
669 switch (codec->dapm.bias_level) {
670 case SND_SOC_BIAS_STANDBY:
671 case SND_SOC_BIAS_OFF:
672 codec->driver->resume(codec);
673 codec->suspended = 0;
676 dev_dbg(codec->dev, "CODEC was on over suspend\n");
682 for (i = 0; i < card->num_rtd; i++) {
684 if (card->rtd[i].dai_link->ignore_suspend)
687 snd_soc_dapm_stream_event(&card->rtd[i],
688 SNDRV_PCM_STREAM_PLAYBACK,
689 SND_SOC_DAPM_STREAM_RESUME);
691 snd_soc_dapm_stream_event(&card->rtd[i],
692 SNDRV_PCM_STREAM_CAPTURE,
693 SND_SOC_DAPM_STREAM_RESUME);
696 /* unmute any active DACs */
697 for (i = 0; i < card->num_rtd; i++) {
698 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
699 struct snd_soc_dai_driver *drv = dai->driver;
701 if (card->rtd[i].dai_link->ignore_suspend)
704 if (drv->ops->digital_mute && dai->playback_active)
705 drv->ops->digital_mute(dai, 0);
708 for (i = 0; i < card->num_rtd; i++) {
709 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
710 struct snd_soc_platform *platform = card->rtd[i].platform;
712 if (card->rtd[i].dai_link->ignore_suspend)
715 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
716 cpu_dai->driver->resume(cpu_dai);
717 if (platform->driver->resume && platform->suspended) {
718 platform->driver->resume(cpu_dai);
719 platform->suspended = 0;
723 if (card->resume_post)
724 card->resume_post(card);
726 dev_dbg(card->dev, "resume work completed\n");
728 /* userspace can access us now we are back as we were before */
729 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
732 /* powers up audio subsystem after a suspend */
733 int snd_soc_resume(struct device *dev)
735 struct snd_soc_card *card = dev_get_drvdata(dev);
736 int i, ac97_control = 0;
738 /* If the initialization of this soc device failed, there is no codec
739 * associated with it. Just bail out in this case.
741 if (list_empty(&card->codec_dev_list))
744 /* AC97 devices might have other drivers hanging off them so
745 * need to resume immediately. Other drivers don't have that
746 * problem and may take a substantial amount of time to resume
747 * due to I/O costs and anti-pop so handle them out of line.
749 for (i = 0; i < card->num_rtd; i++) {
750 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
751 ac97_control |= cpu_dai->driver->ac97_control;
754 dev_dbg(dev, "Resuming AC97 immediately\n");
755 soc_resume_deferred(&card->deferred_resume_work);
757 dev_dbg(dev, "Scheduling resume work\n");
758 if (!schedule_work(&card->deferred_resume_work))
759 dev_err(dev, "resume work item may be lost\n");
764 EXPORT_SYMBOL_GPL(snd_soc_resume);
766 #define snd_soc_suspend NULL
767 #define snd_soc_resume NULL
770 static const struct snd_soc_dai_ops null_dai_ops = {
773 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
775 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
776 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
777 struct snd_soc_codec *codec;
778 struct snd_soc_platform *platform;
779 struct snd_soc_dai *codec_dai, *cpu_dai;
780 const char *platform_name;
782 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
784 /* Find CPU DAI from registered DAIs*/
785 list_for_each_entry(cpu_dai, &dai_list, list) {
786 if (dai_link->cpu_dai_of_node) {
787 if (cpu_dai->dev->of_node != dai_link->cpu_dai_of_node)
790 if (strcmp(cpu_dai->name, dai_link->cpu_dai_name))
794 rtd->cpu_dai = cpu_dai;
798 dev_dbg(card->dev, "CPU DAI %s not registered\n",
799 dai_link->cpu_dai_name);
800 return -EPROBE_DEFER;
803 /* Find CODEC from registered CODECs */
804 list_for_each_entry(codec, &codec_list, list) {
805 if (dai_link->codec_of_node) {
806 if (codec->dev->of_node != dai_link->codec_of_node)
809 if (strcmp(codec->name, dai_link->codec_name))
816 * CODEC found, so find CODEC DAI from registered DAIs from
819 list_for_each_entry(codec_dai, &dai_list, list) {
820 if (codec->dev == codec_dai->dev &&
821 !strcmp(codec_dai->name,
822 dai_link->codec_dai_name)) {
824 rtd->codec_dai = codec_dai;
828 if (!rtd->codec_dai) {
829 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
830 dai_link->codec_dai_name);
831 return -EPROBE_DEFER;
836 dev_dbg(card->dev, "CODEC %s not registered\n",
837 dai_link->codec_name);
838 return -EPROBE_DEFER;
841 /* if there's no platform we match on the empty platform */
842 platform_name = dai_link->platform_name;
843 if (!platform_name && !dai_link->platform_of_node)
844 platform_name = "snd-soc-dummy";
846 /* find one from the set of registered platforms */
847 list_for_each_entry(platform, &platform_list, list) {
848 if (dai_link->platform_of_node) {
849 if (platform->dev->of_node !=
850 dai_link->platform_of_node)
853 if (strcmp(platform->name, platform_name))
857 rtd->platform = platform;
859 if (!rtd->platform) {
860 dev_dbg(card->dev, "platform %s not registered\n",
861 dai_link->platform_name);
862 return -EPROBE_DEFER;
870 static void soc_remove_codec(struct snd_soc_codec *codec)
874 if (codec->driver->remove) {
875 err = codec->driver->remove(codec);
878 "asoc: failed to remove %s: %d\n",
882 /* Make sure all DAPM widgets are freed */
883 snd_soc_dapm_free(&codec->dapm);
885 soc_cleanup_codec_debugfs(codec);
887 list_del(&codec->card_list);
888 module_put(codec->dev->driver->owner);
891 static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
893 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
894 struct snd_soc_codec *codec = rtd->codec;
895 struct snd_soc_platform *platform = rtd->platform;
896 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
899 /* unregister the rtd device */
900 if (rtd->dev_registered) {
901 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
902 device_remove_file(rtd->dev, &dev_attr_codec_reg);
903 device_unregister(rtd->dev);
904 rtd->dev_registered = 0;
907 /* remove the CODEC DAI */
908 if (codec_dai && codec_dai->probed &&
909 codec_dai->driver->remove_order == order) {
910 if (codec_dai->driver->remove) {
911 err = codec_dai->driver->remove(codec_dai);
913 pr_err("asoc: failed to remove %s: %d\n",
914 codec_dai->name, err);
916 codec_dai->probed = 0;
917 list_del(&codec_dai->card_list);
920 /* remove the platform */
921 if (platform && platform->probed &&
922 platform->driver->remove_order == order) {
923 if (platform->driver->remove) {
924 err = platform->driver->remove(platform);
926 pr_err("asoc: failed to remove %s: %d\n",
927 platform->name, err);
930 /* Make sure all DAPM widgets are freed */
931 snd_soc_dapm_free(&platform->dapm);
933 soc_cleanup_platform_debugfs(platform);
934 platform->probed = 0;
935 list_del(&platform->card_list);
936 module_put(platform->dev->driver->owner);
939 /* remove the CODEC */
940 if (codec && codec->probed &&
941 codec->driver->remove_order == order)
942 soc_remove_codec(codec);
944 /* remove the cpu_dai */
945 if (cpu_dai && cpu_dai->probed &&
946 cpu_dai->driver->remove_order == order) {
947 if (cpu_dai->driver->remove) {
948 err = cpu_dai->driver->remove(cpu_dai);
950 pr_err("asoc: failed to remove %s: %d\n",
954 list_del(&cpu_dai->card_list);
955 module_put(cpu_dai->dev->driver->owner);
959 static void soc_remove_dai_links(struct snd_soc_card *card)
963 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
965 for (dai = 0; dai < card->num_rtd; dai++)
966 soc_remove_dai_link(card, dai, order);
971 static void soc_set_name_prefix(struct snd_soc_card *card,
972 struct snd_soc_codec *codec)
976 if (card->codec_conf == NULL)
979 for (i = 0; i < card->num_configs; i++) {
980 struct snd_soc_codec_conf *map = &card->codec_conf[i];
981 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
982 codec->name_prefix = map->name_prefix;
988 static int soc_probe_codec(struct snd_soc_card *card,
989 struct snd_soc_codec *codec)
992 const struct snd_soc_codec_driver *driver = codec->driver;
993 struct snd_soc_dai *dai;
996 codec->dapm.card = card;
997 soc_set_name_prefix(card, codec);
999 if (!try_module_get(codec->dev->driver->owner))
1002 soc_init_codec_debugfs(codec);
1004 if (driver->dapm_widgets)
1005 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1006 driver->num_dapm_widgets);
1008 /* Create DAPM widgets for each DAI stream */
1009 list_for_each_entry(dai, &dai_list, list) {
1010 if (dai->dev != codec->dev)
1013 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1016 codec->dapm.idle_bias_off = driver->idle_bias_off;
1018 if (driver->probe) {
1019 ret = driver->probe(codec);
1022 "asoc: failed to probe CODEC %s: %d\n",
1028 if (driver->controls)
1029 snd_soc_add_codec_controls(codec, driver->controls,
1030 driver->num_controls);
1031 if (driver->dapm_routes)
1032 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1033 driver->num_dapm_routes);
1035 /* mark codec as probed and add to card codec list */
1037 list_add(&codec->card_list, &card->codec_dev_list);
1038 list_add(&codec->dapm.list, &card->dapm_list);
1043 soc_cleanup_codec_debugfs(codec);
1044 module_put(codec->dev->driver->owner);
1049 static int soc_probe_platform(struct snd_soc_card *card,
1050 struct snd_soc_platform *platform)
1053 const struct snd_soc_platform_driver *driver = platform->driver;
1054 struct snd_soc_dai *dai;
1056 platform->card = card;
1057 platform->dapm.card = card;
1059 if (!try_module_get(platform->dev->driver->owner))
1062 soc_init_platform_debugfs(platform);
1064 if (driver->dapm_widgets)
1065 snd_soc_dapm_new_controls(&platform->dapm,
1066 driver->dapm_widgets, driver->num_dapm_widgets);
1068 /* Create DAPM widgets for each DAI stream */
1069 list_for_each_entry(dai, &dai_list, list) {
1070 if (dai->dev != platform->dev)
1073 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1076 platform->dapm.idle_bias_off = 1;
1078 if (driver->probe) {
1079 ret = driver->probe(platform);
1081 dev_err(platform->dev,
1082 "asoc: failed to probe platform %s: %d\n",
1083 platform->name, ret);
1088 if (driver->controls)
1089 snd_soc_add_platform_controls(platform, driver->controls,
1090 driver->num_controls);
1091 if (driver->dapm_routes)
1092 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1093 driver->num_dapm_routes);
1095 /* mark platform as probed and add to card platform list */
1096 platform->probed = 1;
1097 list_add(&platform->card_list, &card->platform_dev_list);
1098 list_add(&platform->dapm.list, &card->dapm_list);
1103 soc_cleanup_platform_debugfs(platform);
1104 module_put(platform->dev->driver->owner);
1109 static void rtd_release(struct device *dev)
1114 static int soc_post_component_init(struct snd_soc_card *card,
1115 struct snd_soc_codec *codec,
1116 int num, int dailess)
1118 struct snd_soc_dai_link *dai_link = NULL;
1119 struct snd_soc_aux_dev *aux_dev = NULL;
1120 struct snd_soc_pcm_runtime *rtd;
1121 const char *temp, *name;
1125 dai_link = &card->dai_link[num];
1126 rtd = &card->rtd[num];
1127 name = dai_link->name;
1129 aux_dev = &card->aux_dev[num];
1130 rtd = &card->rtd_aux[num];
1131 name = aux_dev->name;
1135 /* Make sure all DAPM widgets are instantiated */
1136 snd_soc_dapm_new_widgets(&codec->dapm);
1138 /* machine controls, routes and widgets are not prefixed */
1139 temp = codec->name_prefix;
1140 codec->name_prefix = NULL;
1142 /* do machine specific initialization */
1143 if (!dailess && dai_link->init)
1144 ret = dai_link->init(rtd);
1145 else if (dailess && aux_dev->init)
1146 ret = aux_dev->init(&codec->dapm);
1148 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1151 codec->name_prefix = temp;
1153 /* register the rtd device */
1156 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1159 device_initialize(rtd->dev);
1160 rtd->dev->parent = card->dev;
1161 rtd->dev->release = rtd_release;
1162 rtd->dev->init_name = name;
1163 dev_set_drvdata(rtd->dev, rtd);
1164 mutex_init(&rtd->pcm_mutex);
1165 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1166 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1167 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1168 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1169 ret = device_add(rtd->dev);
1172 "asoc: failed to register runtime device: %d\n", ret);
1175 rtd->dev_registered = 1;
1177 /* add DAPM sysfs entries for this codec */
1178 ret = snd_soc_dapm_sys_add(rtd->dev);
1181 "asoc: failed to add codec dapm sysfs entries: %d\n",
1184 /* add codec sysfs entries */
1185 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1188 "asoc: failed to add codec sysfs files: %d\n", ret);
1193 static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
1195 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1196 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1197 struct snd_soc_codec *codec = rtd->codec;
1198 struct snd_soc_platform *platform = rtd->platform;
1199 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1200 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1201 struct snd_soc_dapm_widget *play_w, *capture_w;
1204 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1205 card->name, num, order);
1207 /* config components */
1208 cpu_dai->platform = platform;
1209 codec_dai->card = card;
1210 cpu_dai->card = card;
1212 /* set default power off timeout */
1213 rtd->pmdown_time = pmdown_time;
1215 /* probe the cpu_dai */
1216 if (!cpu_dai->probed &&
1217 cpu_dai->driver->probe_order == order) {
1218 cpu_dai->dapm.card = card;
1219 if (!try_module_get(cpu_dai->dev->driver->owner))
1222 snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1224 if (cpu_dai->driver->probe) {
1225 ret = cpu_dai->driver->probe(cpu_dai);
1227 pr_err("asoc: failed to probe CPU DAI %s: %d\n",
1228 cpu_dai->name, ret);
1229 module_put(cpu_dai->dev->driver->owner);
1233 cpu_dai->probed = 1;
1234 /* mark cpu_dai as probed and add to card dai list */
1235 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1238 /* probe the CODEC */
1239 if (!codec->probed &&
1240 codec->driver->probe_order == order) {
1241 ret = soc_probe_codec(card, codec);
1246 /* probe the platform */
1247 if (!platform->probed &&
1248 platform->driver->probe_order == order) {
1249 ret = soc_probe_platform(card, platform);
1254 /* probe the CODEC DAI */
1255 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1256 if (codec_dai->driver->probe) {
1257 ret = codec_dai->driver->probe(codec_dai);
1259 pr_err("asoc: failed to probe CODEC DAI %s: %d\n",
1260 codec_dai->name, ret);
1265 /* mark codec_dai as probed and add to card dai list */
1266 codec_dai->probed = 1;
1267 list_add(&codec_dai->card_list, &card->dai_dev_list);
1270 /* complete DAI probe during last probe */
1271 if (order != SND_SOC_COMP_ORDER_LAST)
1274 ret = soc_post_component_init(card, codec, num, 0);
1278 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1280 pr_warn("asoc: failed to add pmdown_time sysfs:%d\n", ret);
1282 if (!dai_link->params) {
1283 /* create the pcm */
1284 ret = soc_new_pcm(rtd, num);
1286 pr_err("asoc: can't create pcm %s :%d\n",
1287 dai_link->stream_name, ret);
1291 /* link the DAI widgets */
1292 play_w = codec_dai->playback_widget;
1293 capture_w = cpu_dai->capture_widget;
1294 if (play_w && capture_w) {
1295 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1298 dev_err(card->dev, "Can't link %s to %s: %d\n",
1299 play_w->name, capture_w->name, ret);
1304 play_w = cpu_dai->playback_widget;
1305 capture_w = codec_dai->capture_widget;
1306 if (play_w && capture_w) {
1307 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1310 dev_err(card->dev, "Can't link %s to %s: %d\n",
1311 play_w->name, capture_w->name, ret);
1317 /* add platform data for AC97 devices */
1318 if (rtd->codec_dai->driver->ac97_control)
1319 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1324 #ifdef CONFIG_SND_SOC_AC97_BUS
1325 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1329 /* Only instantiate AC97 if not already done by the adaptor
1330 * for the generic AC97 subsystem.
1332 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
1334 * It is possible that the AC97 device is already registered to
1335 * the device subsystem. This happens when the device is created
1336 * via snd_ac97_mixer(). Currently only SoC codec that does so
1337 * is the generic AC97 glue but others migh emerge.
1339 * In those cases we don't try to register the device again.
1341 if (!rtd->codec->ac97_created)
1344 ret = soc_ac97_dev_register(rtd->codec);
1346 pr_err("asoc: AC97 device register failed:%d\n", ret);
1350 rtd->codec->ac97_registered = 1;
1355 static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1357 if (codec->ac97_registered) {
1358 soc_ac97_dev_unregister(codec);
1359 codec->ac97_registered = 0;
1364 static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1366 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1367 struct snd_soc_codec *codec;
1369 /* find CODEC from registered CODECs*/
1370 list_for_each_entry(codec, &codec_list, list) {
1371 if (!strcmp(codec->name, aux_dev->codec_name))
1375 return -EPROBE_DEFER;
1378 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1380 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1381 struct snd_soc_codec *codec;
1384 /* find CODEC from registered CODECs*/
1385 list_for_each_entry(codec, &codec_list, list) {
1386 if (!strcmp(codec->name, aux_dev->codec_name)) {
1387 if (codec->probed) {
1389 "asoc: codec already probed");
1396 /* codec not found */
1397 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1398 return -EPROBE_DEFER;
1401 ret = soc_probe_codec(card, codec);
1405 ret = soc_post_component_init(card, codec, num, 1);
1411 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1413 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1414 struct snd_soc_codec *codec = rtd->codec;
1416 /* unregister the rtd device */
1417 if (rtd->dev_registered) {
1418 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1419 device_del(rtd->dev);
1420 rtd->dev_registered = 0;
1423 if (codec && codec->probed)
1424 soc_remove_codec(codec);
1427 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1428 enum snd_soc_compress_type compress_type)
1432 if (codec->cache_init)
1435 /* override the compress_type if necessary */
1436 if (compress_type && codec->compress_type != compress_type)
1437 codec->compress_type = compress_type;
1438 ret = snd_soc_cache_init(codec);
1440 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1444 codec->cache_init = 1;
1448 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1450 struct snd_soc_codec *codec;
1451 struct snd_soc_codec_conf *codec_conf;
1452 enum snd_soc_compress_type compress_type;
1453 struct snd_soc_dai_link *dai_link;
1454 int ret, i, order, dai_fmt;
1456 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1459 for (i = 0; i < card->num_links; i++) {
1460 ret = soc_bind_dai_link(card, i);
1465 /* check aux_devs too */
1466 for (i = 0; i < card->num_aux_devs; i++) {
1467 ret = soc_check_aux_dev(card, i);
1472 /* initialize the register cache for each available codec */
1473 list_for_each_entry(codec, &codec_list, list) {
1474 if (codec->cache_init)
1476 /* by default we don't override the compress_type */
1478 /* check to see if we need to override the compress_type */
1479 for (i = 0; i < card->num_configs; ++i) {
1480 codec_conf = &card->codec_conf[i];
1481 if (!strcmp(codec->name, codec_conf->dev_name)) {
1482 compress_type = codec_conf->compress_type;
1483 if (compress_type && compress_type
1484 != codec->compress_type)
1488 ret = snd_soc_init_codec_cache(codec, compress_type);
1493 /* card bind complete so register a sound card */
1494 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1495 card->owner, 0, &card->snd_card);
1497 pr_err("asoc: can't create sound card for card %s: %d\n",
1501 card->snd_card->dev = card->dev;
1503 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1504 card->dapm.dev = card->dev;
1505 card->dapm.card = card;
1506 list_add(&card->dapm.list, &card->dapm_list);
1508 #ifdef CONFIG_DEBUG_FS
1509 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1512 #ifdef CONFIG_PM_SLEEP
1513 /* deferred resume work */
1514 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1517 if (card->dapm_widgets)
1518 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1519 card->num_dapm_widgets);
1521 /* initialise the sound card only once */
1523 ret = card->probe(card);
1525 goto card_probe_error;
1528 /* early DAI link probe */
1529 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1531 for (i = 0; i < card->num_links; i++) {
1532 ret = soc_probe_dai_link(card, i, order);
1534 pr_err("asoc: failed to instantiate card %s: %d\n",
1541 for (i = 0; i < card->num_aux_devs; i++) {
1542 ret = soc_probe_aux_dev(card, i);
1544 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1546 goto probe_aux_dev_err;
1550 snd_soc_dapm_link_dai_widgets(card);
1553 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1555 if (card->dapm_routes)
1556 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1557 card->num_dapm_routes);
1559 snd_soc_dapm_new_widgets(&card->dapm);
1561 for (i = 0; i < card->num_links; i++) {
1562 dai_link = &card->dai_link[i];
1563 dai_fmt = dai_link->dai_fmt;
1566 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1568 if (ret != 0 && ret != -ENOTSUPP)
1569 dev_warn(card->rtd[i].codec_dai->dev,
1570 "Failed to set DAI format: %d\n",
1574 /* If this is a regular CPU link there will be a platform */
1575 if (dai_fmt && dai_link->platform_name) {
1576 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1578 if (ret != 0 && ret != -ENOTSUPP)
1579 dev_warn(card->rtd[i].cpu_dai->dev,
1580 "Failed to set DAI format: %d\n",
1582 } else if (dai_fmt) {
1583 /* Flip the polarity for the "CPU" end */
1584 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1585 switch (dai_link->dai_fmt &
1586 SND_SOC_DAIFMT_MASTER_MASK) {
1587 case SND_SOC_DAIFMT_CBM_CFM:
1588 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1590 case SND_SOC_DAIFMT_CBM_CFS:
1591 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1593 case SND_SOC_DAIFMT_CBS_CFM:
1594 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1596 case SND_SOC_DAIFMT_CBS_CFS:
1597 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1601 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1603 if (ret != 0 && ret != -ENOTSUPP)
1604 dev_warn(card->rtd[i].cpu_dai->dev,
1605 "Failed to set DAI format: %d\n",
1610 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1612 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1613 "%s", card->long_name ? card->long_name : card->name);
1614 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1615 "%s", card->driver_name ? card->driver_name : card->name);
1616 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1617 switch (card->snd_card->driver[i]) {
1623 if (!isalnum(card->snd_card->driver[i]))
1624 card->snd_card->driver[i] = '_';
1629 if (card->late_probe) {
1630 ret = card->late_probe(card);
1632 dev_err(card->dev, "%s late_probe() failed: %d\n",
1634 goto probe_aux_dev_err;
1638 snd_soc_dapm_new_widgets(&card->dapm);
1640 if (card->fully_routed)
1641 list_for_each_entry(codec, &card->codec_dev_list, card_list)
1642 snd_soc_dapm_auto_nc_codec_pins(codec);
1644 ret = snd_card_register(card->snd_card);
1646 pr_err("asoc: failed to register soundcard for %s: %d\n",
1648 goto probe_aux_dev_err;
1651 #ifdef CONFIG_SND_SOC_AC97_BUS
1652 /* register any AC97 codecs */
1653 for (i = 0; i < card->num_rtd; i++) {
1654 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1656 pr_err("asoc: failed to register AC97 %s: %d\n",
1659 soc_unregister_ac97_dai_link(card->rtd[i].codec);
1660 goto probe_aux_dev_err;
1665 card->instantiated = 1;
1666 snd_soc_dapm_sync(&card->dapm);
1667 mutex_unlock(&card->mutex);
1672 for (i = 0; i < card->num_aux_devs; i++)
1673 soc_remove_aux_dev(card, i);
1676 soc_remove_dai_links(card);
1682 snd_card_free(card->snd_card);
1685 mutex_unlock(&card->mutex);
1690 /* probes a new socdev */
1691 static int soc_probe(struct platform_device *pdev)
1693 struct snd_soc_card *card = platform_get_drvdata(pdev);
1697 * no card, so machine driver should be registering card
1698 * we should not be here in that case so ret error
1703 dev_warn(&pdev->dev,
1704 "ASoC machine %s should use snd_soc_register_card()\n",
1707 /* Bodge while we unpick instantiation */
1708 card->dev = &pdev->dev;
1710 ret = snd_soc_register_card(card);
1712 dev_err(&pdev->dev, "Failed to register card\n");
1719 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1723 /* make sure any delayed work runs */
1724 for (i = 0; i < card->num_rtd; i++) {
1725 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1726 flush_delayed_work_sync(&rtd->delayed_work);
1729 /* remove auxiliary devices */
1730 for (i = 0; i < card->num_aux_devs; i++)
1731 soc_remove_aux_dev(card, i);
1733 /* remove and free each DAI */
1734 soc_remove_dai_links(card);
1736 soc_cleanup_card_debugfs(card);
1738 /* remove the card */
1742 snd_soc_dapm_free(&card->dapm);
1744 snd_card_free(card->snd_card);
1749 /* removes a socdev */
1750 static int soc_remove(struct platform_device *pdev)
1752 struct snd_soc_card *card = platform_get_drvdata(pdev);
1754 snd_soc_unregister_card(card);
1758 int snd_soc_poweroff(struct device *dev)
1760 struct snd_soc_card *card = dev_get_drvdata(dev);
1763 if (!card->instantiated)
1766 /* Flush out pmdown_time work - we actually do want to run it
1767 * now, we're shutting down so no imminent restart. */
1768 for (i = 0; i < card->num_rtd; i++) {
1769 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1770 flush_delayed_work_sync(&rtd->delayed_work);
1773 snd_soc_dapm_shutdown(card);
1777 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1779 const struct dev_pm_ops snd_soc_pm_ops = {
1780 .suspend = snd_soc_suspend,
1781 .resume = snd_soc_resume,
1782 .freeze = snd_soc_suspend,
1783 .thaw = snd_soc_resume,
1784 .poweroff = snd_soc_poweroff,
1785 .restore = snd_soc_resume,
1787 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1789 /* ASoC platform driver */
1790 static struct platform_driver soc_driver = {
1792 .name = "soc-audio",
1793 .owner = THIS_MODULE,
1794 .pm = &snd_soc_pm_ops,
1797 .remove = soc_remove,
1801 * snd_soc_codec_volatile_register: Report if a register is volatile.
1803 * @codec: CODEC to query.
1804 * @reg: Register to query.
1806 * Boolean function indiciating if a CODEC register is volatile.
1808 int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1811 if (codec->volatile_register)
1812 return codec->volatile_register(codec, reg);
1816 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1819 * snd_soc_codec_readable_register: Report if a register is readable.
1821 * @codec: CODEC to query.
1822 * @reg: Register to query.
1824 * Boolean function indicating if a CODEC register is readable.
1826 int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1829 if (codec->readable_register)
1830 return codec->readable_register(codec, reg);
1834 EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1837 * snd_soc_codec_writable_register: Report if a register is writable.
1839 * @codec: CODEC to query.
1840 * @reg: Register to query.
1842 * Boolean function indicating if a CODEC register is writable.
1844 int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1847 if (codec->writable_register)
1848 return codec->writable_register(codec, reg);
1852 EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1854 int snd_soc_platform_read(struct snd_soc_platform *platform,
1859 if (!platform->driver->read) {
1860 dev_err(platform->dev, "platform has no read back\n");
1864 ret = platform->driver->read(platform, reg);
1865 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
1866 trace_snd_soc_preg_read(platform, reg, ret);
1870 EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1872 int snd_soc_platform_write(struct snd_soc_platform *platform,
1873 unsigned int reg, unsigned int val)
1875 if (!platform->driver->write) {
1876 dev_err(platform->dev, "platform has no write back\n");
1880 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
1881 trace_snd_soc_preg_write(platform, reg, val);
1882 return platform->driver->write(platform, reg, val);
1884 EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1887 * snd_soc_new_ac97_codec - initailise AC97 device
1888 * @codec: audio codec
1889 * @ops: AC97 bus operations
1890 * @num: AC97 codec number
1892 * Initialises AC97 codec resources for use by ad-hoc devices only.
1894 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1895 struct snd_ac97_bus_ops *ops, int num)
1897 mutex_lock(&codec->mutex);
1899 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1900 if (codec->ac97 == NULL) {
1901 mutex_unlock(&codec->mutex);
1905 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1906 if (codec->ac97->bus == NULL) {
1909 mutex_unlock(&codec->mutex);
1913 codec->ac97->bus->ops = ops;
1914 codec->ac97->num = num;
1917 * Mark the AC97 device to be created by us. This way we ensure that the
1918 * device will be registered with the device subsystem later on.
1920 codec->ac97_created = 1;
1922 mutex_unlock(&codec->mutex);
1925 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1928 * snd_soc_free_ac97_codec - free AC97 codec device
1929 * @codec: audio codec
1931 * Frees AC97 codec device resources.
1933 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1935 mutex_lock(&codec->mutex);
1936 #ifdef CONFIG_SND_SOC_AC97_BUS
1937 soc_unregister_ac97_dai_link(codec);
1939 kfree(codec->ac97->bus);
1942 codec->ac97_created = 0;
1943 mutex_unlock(&codec->mutex);
1945 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1947 unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1951 ret = codec->read(codec, reg);
1952 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
1953 trace_snd_soc_reg_read(codec, reg, ret);
1957 EXPORT_SYMBOL_GPL(snd_soc_read);
1959 unsigned int snd_soc_write(struct snd_soc_codec *codec,
1960 unsigned int reg, unsigned int val)
1962 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
1963 trace_snd_soc_reg_write(codec, reg, val);
1964 return codec->write(codec, reg, val);
1966 EXPORT_SYMBOL_GPL(snd_soc_write);
1968 unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
1969 unsigned int reg, const void *data, size_t len)
1971 return codec->bulk_write_raw(codec, reg, data, len);
1973 EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
1976 * snd_soc_update_bits - update codec register bits
1977 * @codec: audio codec
1978 * @reg: codec register
1979 * @mask: register mask
1982 * Writes new register value.
1984 * Returns 1 for change, 0 for no change, or negative error code.
1986 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1987 unsigned int mask, unsigned int value)
1990 unsigned int old, new;
1993 if (codec->using_regmap) {
1994 ret = regmap_update_bits_check(codec->control_data, reg,
1995 mask, value, &change);
1997 ret = snd_soc_read(codec, reg);
2002 new = (old & ~mask) | (value & mask);
2003 change = old != new;
2005 ret = snd_soc_write(codec, reg, new);
2013 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2016 * snd_soc_update_bits_locked - update codec register bits
2017 * @codec: audio codec
2018 * @reg: codec register
2019 * @mask: register mask
2022 * Writes new register value, and takes the codec mutex.
2024 * Returns 1 for change else 0.
2026 int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2027 unsigned short reg, unsigned int mask,
2032 mutex_lock(&codec->mutex);
2033 change = snd_soc_update_bits(codec, reg, mask, value);
2034 mutex_unlock(&codec->mutex);
2038 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
2041 * snd_soc_test_bits - test register for change
2042 * @codec: audio codec
2043 * @reg: codec register
2044 * @mask: register mask
2047 * Tests a register with a new value and checks if the new value is
2048 * different from the old value.
2050 * Returns 1 for change else 0.
2052 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
2053 unsigned int mask, unsigned int value)
2056 unsigned int old, new;
2058 old = snd_soc_read(codec, reg);
2059 new = (old & ~mask) | value;
2060 change = old != new;
2064 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2067 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2068 * @substream: the pcm substream
2069 * @hw: the hardware parameters
2071 * Sets the substream runtime hardware parameters.
2073 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2074 const struct snd_pcm_hardware *hw)
2076 struct snd_pcm_runtime *runtime = substream->runtime;
2077 runtime->hw.info = hw->info;
2078 runtime->hw.formats = hw->formats;
2079 runtime->hw.period_bytes_min = hw->period_bytes_min;
2080 runtime->hw.period_bytes_max = hw->period_bytes_max;
2081 runtime->hw.periods_min = hw->periods_min;
2082 runtime->hw.periods_max = hw->periods_max;
2083 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2084 runtime->hw.fifo_size = hw->fifo_size;
2087 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2090 * snd_soc_cnew - create new control
2091 * @_template: control template
2092 * @data: control private data
2093 * @long_name: control long name
2094 * @prefix: control name prefix
2096 * Create a new mixer control from a template control.
2098 * Returns 0 for success, else error.
2100 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2101 void *data, const char *long_name,
2104 struct snd_kcontrol_new template;
2105 struct snd_kcontrol *kcontrol;
2109 memcpy(&template, _template, sizeof(template));
2113 long_name = template.name;
2116 name_len = strlen(long_name) + strlen(prefix) + 2;
2117 name = kmalloc(name_len, GFP_KERNEL);
2121 snprintf(name, name_len, "%s %s", prefix, long_name);
2123 template.name = name;
2125 template.name = long_name;
2128 kcontrol = snd_ctl_new1(&template, data);
2134 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2136 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2137 const struct snd_kcontrol_new *controls, int num_controls,
2138 const char *prefix, void *data)
2142 for (i = 0; i < num_controls; i++) {
2143 const struct snd_kcontrol_new *control = &controls[i];
2144 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2145 control->name, prefix));
2147 dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2156 * snd_soc_add_codec_controls - add an array of controls to a codec.
2157 * Convenience function to add a list of controls. Many codecs were
2158 * duplicating this code.
2160 * @codec: codec to add controls to
2161 * @controls: array of controls to add
2162 * @num_controls: number of elements in the array
2164 * Return 0 for success, else error.
2166 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2167 const struct snd_kcontrol_new *controls, int num_controls)
2169 struct snd_card *card = codec->card->snd_card;
2171 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2172 codec->name_prefix, codec);
2174 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2177 * snd_soc_add_platform_controls - add an array of controls to a platform.
2178 * Convenience function to add a list of controls.
2180 * @platform: platform to add controls to
2181 * @controls: array of controls to add
2182 * @num_controls: number of elements in the array
2184 * Return 0 for success, else error.
2186 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2187 const struct snd_kcontrol_new *controls, int num_controls)
2189 struct snd_card *card = platform->card->snd_card;
2191 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2194 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2197 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2198 * Convenience function to add a list of controls.
2200 * @soc_card: SoC card to add controls to
2201 * @controls: array of controls to add
2202 * @num_controls: number of elements in the array
2204 * Return 0 for success, else error.
2206 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2207 const struct snd_kcontrol_new *controls, int num_controls)
2209 struct snd_card *card = soc_card->snd_card;
2211 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2214 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2217 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2218 * Convienience function to add a list of controls.
2220 * @dai: DAI to add controls to
2221 * @controls: array of controls to add
2222 * @num_controls: number of elements in the array
2224 * Return 0 for success, else error.
2226 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2227 const struct snd_kcontrol_new *controls, int num_controls)
2229 struct snd_card *card = dai->card->snd_card;
2231 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2234 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2237 * snd_soc_info_enum_double - enumerated double mixer info callback
2238 * @kcontrol: mixer control
2239 * @uinfo: control element information
2241 * Callback to provide information about a double enumerated
2244 * Returns 0 for success.
2246 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2247 struct snd_ctl_elem_info *uinfo)
2249 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2251 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2252 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2253 uinfo->value.enumerated.items = e->max;
2255 if (uinfo->value.enumerated.item > e->max - 1)
2256 uinfo->value.enumerated.item = e->max - 1;
2257 strcpy(uinfo->value.enumerated.name,
2258 e->texts[uinfo->value.enumerated.item]);
2261 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2264 * snd_soc_get_enum_double - enumerated double mixer get callback
2265 * @kcontrol: mixer control
2266 * @ucontrol: control element information
2268 * Callback to get the value of a double enumerated mixer.
2270 * Returns 0 for success.
2272 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2273 struct snd_ctl_elem_value *ucontrol)
2275 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2276 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2277 unsigned int val, bitmask;
2279 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2281 val = snd_soc_read(codec, e->reg);
2282 ucontrol->value.enumerated.item[0]
2283 = (val >> e->shift_l) & (bitmask - 1);
2284 if (e->shift_l != e->shift_r)
2285 ucontrol->value.enumerated.item[1] =
2286 (val >> e->shift_r) & (bitmask - 1);
2290 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2293 * snd_soc_put_enum_double - enumerated double mixer put callback
2294 * @kcontrol: mixer control
2295 * @ucontrol: control element information
2297 * Callback to set the value of a double enumerated mixer.
2299 * Returns 0 for success.
2301 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2302 struct snd_ctl_elem_value *ucontrol)
2304 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2305 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2307 unsigned int mask, bitmask;
2309 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2311 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2313 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2314 mask = (bitmask - 1) << e->shift_l;
2315 if (e->shift_l != e->shift_r) {
2316 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2318 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2319 mask |= (bitmask - 1) << e->shift_r;
2322 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2324 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2327 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2328 * @kcontrol: mixer control
2329 * @ucontrol: control element information
2331 * Callback to get the value of a double semi enumerated mixer.
2333 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2334 * used for handling bitfield coded enumeration for example.
2336 * Returns 0 for success.
2338 int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2339 struct snd_ctl_elem_value *ucontrol)
2341 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2342 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2343 unsigned int reg_val, val, mux;
2345 reg_val = snd_soc_read(codec, e->reg);
2346 val = (reg_val >> e->shift_l) & e->mask;
2347 for (mux = 0; mux < e->max; mux++) {
2348 if (val == e->values[mux])
2351 ucontrol->value.enumerated.item[0] = mux;
2352 if (e->shift_l != e->shift_r) {
2353 val = (reg_val >> e->shift_r) & e->mask;
2354 for (mux = 0; mux < e->max; mux++) {
2355 if (val == e->values[mux])
2358 ucontrol->value.enumerated.item[1] = mux;
2363 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2366 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2367 * @kcontrol: mixer control
2368 * @ucontrol: control element information
2370 * Callback to set the value of a double semi enumerated mixer.
2372 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2373 * used for handling bitfield coded enumeration for example.
2375 * Returns 0 for success.
2377 int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2378 struct snd_ctl_elem_value *ucontrol)
2380 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2381 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2385 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2387 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2388 mask = e->mask << e->shift_l;
2389 if (e->shift_l != e->shift_r) {
2390 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2392 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2393 mask |= e->mask << e->shift_r;
2396 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2398 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2401 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2402 * @kcontrol: mixer control
2403 * @uinfo: control element information
2405 * Callback to provide information about an external enumerated
2408 * Returns 0 for success.
2410 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2411 struct snd_ctl_elem_info *uinfo)
2413 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2415 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2417 uinfo->value.enumerated.items = e->max;
2419 if (uinfo->value.enumerated.item > e->max - 1)
2420 uinfo->value.enumerated.item = e->max - 1;
2421 strcpy(uinfo->value.enumerated.name,
2422 e->texts[uinfo->value.enumerated.item]);
2425 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2428 * snd_soc_info_volsw_ext - external single mixer info callback
2429 * @kcontrol: mixer control
2430 * @uinfo: control element information
2432 * Callback to provide information about a single external mixer control.
2434 * Returns 0 for success.
2436 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2437 struct snd_ctl_elem_info *uinfo)
2439 int max = kcontrol->private_value;
2441 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2442 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2444 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2447 uinfo->value.integer.min = 0;
2448 uinfo->value.integer.max = max;
2451 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2454 * snd_soc_info_volsw - single mixer info callback
2455 * @kcontrol: mixer control
2456 * @uinfo: control element information
2458 * Callback to provide information about a single mixer control, or a double
2459 * mixer control that spans 2 registers.
2461 * Returns 0 for success.
2463 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2464 struct snd_ctl_elem_info *uinfo)
2466 struct soc_mixer_control *mc =
2467 (struct soc_mixer_control *)kcontrol->private_value;
2470 if (!mc->platform_max)
2471 mc->platform_max = mc->max;
2472 platform_max = mc->platform_max;
2474 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2475 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2477 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2479 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2480 uinfo->value.integer.min = 0;
2481 uinfo->value.integer.max = platform_max;
2484 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2487 * snd_soc_get_volsw - single mixer get callback
2488 * @kcontrol: mixer control
2489 * @ucontrol: control element information
2491 * Callback to get the value of a single mixer control, or a double mixer
2492 * control that spans 2 registers.
2494 * Returns 0 for success.
2496 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2497 struct snd_ctl_elem_value *ucontrol)
2499 struct soc_mixer_control *mc =
2500 (struct soc_mixer_control *)kcontrol->private_value;
2501 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2502 unsigned int reg = mc->reg;
2503 unsigned int reg2 = mc->rreg;
2504 unsigned int shift = mc->shift;
2505 unsigned int rshift = mc->rshift;
2507 unsigned int mask = (1 << fls(max)) - 1;
2508 unsigned int invert = mc->invert;
2510 ucontrol->value.integer.value[0] =
2511 (snd_soc_read(codec, reg) >> shift) & mask;
2513 ucontrol->value.integer.value[0] =
2514 max - ucontrol->value.integer.value[0];
2516 if (snd_soc_volsw_is_stereo(mc)) {
2518 ucontrol->value.integer.value[1] =
2519 (snd_soc_read(codec, reg) >> rshift) & mask;
2521 ucontrol->value.integer.value[1] =
2522 (snd_soc_read(codec, reg2) >> shift) & mask;
2524 ucontrol->value.integer.value[1] =
2525 max - ucontrol->value.integer.value[1];
2530 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2533 * snd_soc_put_volsw - single mixer put callback
2534 * @kcontrol: mixer control
2535 * @ucontrol: control element information
2537 * Callback to set the value of a single mixer control, or a double mixer
2538 * control that spans 2 registers.
2540 * Returns 0 for success.
2542 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2543 struct snd_ctl_elem_value *ucontrol)
2545 struct soc_mixer_control *mc =
2546 (struct soc_mixer_control *)kcontrol->private_value;
2547 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2548 unsigned int reg = mc->reg;
2549 unsigned int reg2 = mc->rreg;
2550 unsigned int shift = mc->shift;
2551 unsigned int rshift = mc->rshift;
2553 unsigned int mask = (1 << fls(max)) - 1;
2554 unsigned int invert = mc->invert;
2557 unsigned int val2 = 0;
2558 unsigned int val, val_mask;
2560 val = (ucontrol->value.integer.value[0] & mask);
2563 val_mask = mask << shift;
2565 if (snd_soc_volsw_is_stereo(mc)) {
2566 val2 = (ucontrol->value.integer.value[1] & mask);
2570 val_mask |= mask << rshift;
2571 val |= val2 << rshift;
2573 val2 = val2 << shift;
2577 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2582 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2586 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2589 * snd_soc_get_volsw_sx - single mixer get callback
2590 * @kcontrol: mixer control
2591 * @ucontrol: control element information
2593 * Callback to get the value of a single mixer control, or a double mixer
2594 * control that spans 2 registers.
2596 * Returns 0 for success.
2598 int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2599 struct snd_ctl_elem_value *ucontrol)
2601 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2602 struct soc_mixer_control *mc =
2603 (struct soc_mixer_control *)kcontrol->private_value;
2605 unsigned int reg = mc->reg;
2606 unsigned int reg2 = mc->rreg;
2607 unsigned int shift = mc->shift;
2608 unsigned int rshift = mc->rshift;
2611 int mask = (1 << (fls(min + max) - 1)) - 1;
2613 ucontrol->value.integer.value[0] =
2614 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2616 if (snd_soc_volsw_is_stereo(mc))
2617 ucontrol->value.integer.value[1] =
2618 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2622 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2625 * snd_soc_put_volsw_sx - double mixer set callback
2626 * @kcontrol: mixer control
2627 * @uinfo: control element information
2629 * Callback to set the value of a double mixer control that spans 2 registers.
2631 * Returns 0 for success.
2633 int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2634 struct snd_ctl_elem_value *ucontrol)
2636 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2637 struct soc_mixer_control *mc =
2638 (struct soc_mixer_control *)kcontrol->private_value;
2640 unsigned int reg = mc->reg;
2641 unsigned int reg2 = mc->rreg;
2642 unsigned int shift = mc->shift;
2643 unsigned int rshift = mc->rshift;
2646 int mask = (1 << (fls(min + max) - 1)) - 1;
2648 unsigned short val, val_mask, val2 = 0;
2650 val_mask = mask << shift;
2651 val = (ucontrol->value.integer.value[0] + min) & mask;
2654 if (snd_soc_update_bits_locked(codec, reg, val_mask, val))
2657 if (snd_soc_volsw_is_stereo(mc)) {
2658 val_mask = mask << rshift;
2659 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2660 val2 = val2 << rshift;
2662 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2667 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2670 * snd_soc_info_volsw_s8 - signed mixer info callback
2671 * @kcontrol: mixer control
2672 * @uinfo: control element information
2674 * Callback to provide information about a signed mixer control.
2676 * Returns 0 for success.
2678 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2679 struct snd_ctl_elem_info *uinfo)
2681 struct soc_mixer_control *mc =
2682 (struct soc_mixer_control *)kcontrol->private_value;
2686 if (!mc->platform_max)
2687 mc->platform_max = mc->max;
2688 platform_max = mc->platform_max;
2690 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2692 uinfo->value.integer.min = 0;
2693 uinfo->value.integer.max = platform_max - min;
2696 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2699 * snd_soc_get_volsw_s8 - signed mixer get callback
2700 * @kcontrol: mixer control
2701 * @ucontrol: control element information
2703 * Callback to get the value of a signed mixer control.
2705 * Returns 0 for success.
2707 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2708 struct snd_ctl_elem_value *ucontrol)
2710 struct soc_mixer_control *mc =
2711 (struct soc_mixer_control *)kcontrol->private_value;
2712 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2713 unsigned int reg = mc->reg;
2715 int val = snd_soc_read(codec, reg);
2717 ucontrol->value.integer.value[0] =
2718 ((signed char)(val & 0xff))-min;
2719 ucontrol->value.integer.value[1] =
2720 ((signed char)((val >> 8) & 0xff))-min;
2723 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2726 * snd_soc_put_volsw_sgn - signed mixer put callback
2727 * @kcontrol: mixer control
2728 * @ucontrol: control element information
2730 * Callback to set the value of a signed mixer control.
2732 * Returns 0 for success.
2734 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2735 struct snd_ctl_elem_value *ucontrol)
2737 struct soc_mixer_control *mc =
2738 (struct soc_mixer_control *)kcontrol->private_value;
2739 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2740 unsigned int reg = mc->reg;
2744 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2745 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2747 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2749 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2752 * snd_soc_limit_volume - Set new limit to an existing volume control.
2754 * @codec: where to look for the control
2755 * @name: Name of the control
2756 * @max: new maximum limit
2758 * Return 0 for success, else error.
2760 int snd_soc_limit_volume(struct snd_soc_codec *codec,
2761 const char *name, int max)
2763 struct snd_card *card = codec->card->snd_card;
2764 struct snd_kcontrol *kctl;
2765 struct soc_mixer_control *mc;
2769 /* Sanity check for name and max */
2770 if (unlikely(!name || max <= 0))
2773 list_for_each_entry(kctl, &card->controls, list) {
2774 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2780 mc = (struct soc_mixer_control *)kctl->private_value;
2781 if (max <= mc->max) {
2782 mc->platform_max = max;
2788 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2790 int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
2791 struct snd_ctl_elem_info *uinfo)
2793 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2794 struct soc_bytes *params = (void *)kcontrol->private_value;
2796 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
2797 uinfo->count = params->num_regs * codec->val_bytes;
2801 EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
2803 int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
2804 struct snd_ctl_elem_value *ucontrol)
2806 struct soc_bytes *params = (void *)kcontrol->private_value;
2807 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2810 if (codec->using_regmap)
2811 ret = regmap_raw_read(codec->control_data, params->base,
2812 ucontrol->value.bytes.data,
2813 params->num_regs * codec->val_bytes);
2817 /* Hide any masked bytes to ensure consistent data reporting */
2818 if (ret == 0 && params->mask) {
2819 switch (codec->val_bytes) {
2821 ucontrol->value.bytes.data[0] &= ~params->mask;
2824 ((u16 *)(&ucontrol->value.bytes.data))[0]
2828 ((u32 *)(&ucontrol->value.bytes.data))[0]
2838 EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
2840 int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
2841 struct snd_ctl_elem_value *ucontrol)
2843 struct soc_bytes *params = (void *)kcontrol->private_value;
2844 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2849 if (!codec->using_regmap)
2852 data = ucontrol->value.bytes.data;
2853 len = params->num_regs * codec->val_bytes;
2856 * If we've got a mask then we need to preserve the register
2857 * bits. We shouldn't modify the incoming data so take a
2861 ret = regmap_read(codec->control_data, params->base, &val);
2865 val &= params->mask;
2867 data = kmemdup(data, len, GFP_KERNEL);
2871 switch (codec->val_bytes) {
2873 ((u8 *)data)[0] &= ~params->mask;
2874 ((u8 *)data)[0] |= val;
2877 ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
2878 ((u16 *)data)[0] |= cpu_to_be16(val);
2881 ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
2882 ((u32 *)data)[0] |= cpu_to_be32(val);
2889 ret = regmap_raw_write(codec->control_data, params->base,
2897 EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
2900 * snd_soc_info_xr_sx - signed multi register info callback
2901 * @kcontrol: mreg control
2902 * @uinfo: control element information
2904 * Callback to provide information of a control that can
2905 * span multiple codec registers which together
2906 * forms a single signed value in a MSB/LSB manner.
2908 * Returns 0 for success.
2910 int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
2911 struct snd_ctl_elem_info *uinfo)
2913 struct soc_mreg_control *mc =
2914 (struct soc_mreg_control *)kcontrol->private_value;
2915 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2917 uinfo->value.integer.min = mc->min;
2918 uinfo->value.integer.max = mc->max;
2922 EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
2925 * snd_soc_get_xr_sx - signed multi register get callback
2926 * @kcontrol: mreg control
2927 * @ucontrol: control element information
2929 * Callback to get the value of a control that can span
2930 * multiple codec registers which together forms a single
2931 * signed value in a MSB/LSB manner. The control supports
2932 * specifying total no of bits used to allow for bitfields
2933 * across the multiple codec registers.
2935 * Returns 0 for success.
2937 int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
2938 struct snd_ctl_elem_value *ucontrol)
2940 struct soc_mreg_control *mc =
2941 (struct soc_mreg_control *)kcontrol->private_value;
2942 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2943 unsigned int regbase = mc->regbase;
2944 unsigned int regcount = mc->regcount;
2945 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
2946 unsigned int regwmask = (1<<regwshift)-1;
2947 unsigned int invert = mc->invert;
2948 unsigned long mask = (1UL<<mc->nbits)-1;
2952 unsigned long regval;
2955 for (i = 0; i < regcount; i++) {
2956 regval = snd_soc_read(codec, regbase+i) & regwmask;
2957 val |= regval << (regwshift*(regcount-i-1));
2960 if (min < 0 && val > max)
2964 ucontrol->value.integer.value[0] = val;
2968 EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
2971 * snd_soc_put_xr_sx - signed multi register get callback
2972 * @kcontrol: mreg control
2973 * @ucontrol: control element information
2975 * Callback to set the value of a control that can span
2976 * multiple codec registers which together forms a single
2977 * signed value in a MSB/LSB manner. The control supports
2978 * specifying total no of bits used to allow for bitfields
2979 * across the multiple codec registers.
2981 * Returns 0 for success.
2983 int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
2984 struct snd_ctl_elem_value *ucontrol)
2986 struct soc_mreg_control *mc =
2987 (struct soc_mreg_control *)kcontrol->private_value;
2988 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2989 unsigned int regbase = mc->regbase;
2990 unsigned int regcount = mc->regcount;
2991 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
2992 unsigned int regwmask = (1<<regwshift)-1;
2993 unsigned int invert = mc->invert;
2994 unsigned long mask = (1UL<<mc->nbits)-1;
2996 long val = ucontrol->value.integer.value[0];
2997 unsigned int i, regval, regmask;
3003 for (i = 0; i < regcount; i++) {
3004 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3005 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3006 err = snd_soc_update_bits_locked(codec, regbase+i,
3014 EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3017 * snd_soc_get_strobe - strobe get callback
3018 * @kcontrol: mixer control
3019 * @ucontrol: control element information
3021 * Callback get the value of a strobe mixer control.
3023 * Returns 0 for success.
3025 int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3026 struct snd_ctl_elem_value *ucontrol)
3028 struct soc_mixer_control *mc =
3029 (struct soc_mixer_control *)kcontrol->private_value;
3030 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3031 unsigned int reg = mc->reg;
3032 unsigned int shift = mc->shift;
3033 unsigned int mask = 1 << shift;
3034 unsigned int invert = mc->invert != 0;
3035 unsigned int val = snd_soc_read(codec, reg) & mask;
3037 if (shift != 0 && val != 0)
3039 ucontrol->value.enumerated.item[0] = val ^ invert;
3043 EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3046 * snd_soc_put_strobe - strobe put callback
3047 * @kcontrol: mixer control
3048 * @ucontrol: control element information
3050 * Callback strobe a register bit to high then low (or the inverse)
3051 * in one pass of a single mixer enum control.
3053 * Returns 1 for success.
3055 int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3056 struct snd_ctl_elem_value *ucontrol)
3058 struct soc_mixer_control *mc =
3059 (struct soc_mixer_control *)kcontrol->private_value;
3060 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3061 unsigned int reg = mc->reg;
3062 unsigned int shift = mc->shift;
3063 unsigned int mask = 1 << shift;
3064 unsigned int invert = mc->invert != 0;
3065 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3066 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3067 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3070 err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3074 err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3077 EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3080 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3082 * @clk_id: DAI specific clock ID
3083 * @freq: new clock frequency in Hz
3084 * @dir: new clock direction - input/output.
3086 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3088 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3089 unsigned int freq, int dir)
3091 if (dai->driver && dai->driver->ops->set_sysclk)
3092 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3093 else if (dai->codec && dai->codec->driver->set_sysclk)
3094 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3099 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3102 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3104 * @clk_id: DAI specific clock ID
3105 * @source: Source for the clock
3106 * @freq: new clock frequency in Hz
3107 * @dir: new clock direction - input/output.
3109 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3111 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3112 int source, unsigned int freq, int dir)
3114 if (codec->driver->set_sysclk)
3115 return codec->driver->set_sysclk(codec, clk_id, source,
3120 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3123 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3125 * @div_id: DAI specific clock divider ID
3126 * @div: new clock divisor.
3128 * Configures the clock dividers. This is used to derive the best DAI bit and
3129 * frame clocks from the system or master clock. It's best to set the DAI bit
3130 * and frame clocks as low as possible to save system power.
3132 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3133 int div_id, int div)
3135 if (dai->driver && dai->driver->ops->set_clkdiv)
3136 return dai->driver->ops->set_clkdiv(dai, div_id, div);
3140 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3143 * snd_soc_dai_set_pll - configure DAI PLL.
3145 * @pll_id: DAI specific PLL ID
3146 * @source: DAI specific source for the PLL
3147 * @freq_in: PLL input clock frequency in Hz
3148 * @freq_out: requested PLL output clock frequency in Hz
3150 * Configures and enables PLL to generate output clock based on input clock.
3152 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3153 unsigned int freq_in, unsigned int freq_out)
3155 if (dai->driver && dai->driver->ops->set_pll)
3156 return dai->driver->ops->set_pll(dai, pll_id, source,
3158 else if (dai->codec && dai->codec->driver->set_pll)
3159 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3164 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3167 * snd_soc_codec_set_pll - configure codec PLL.
3169 * @pll_id: DAI specific PLL ID
3170 * @source: DAI specific source for the PLL
3171 * @freq_in: PLL input clock frequency in Hz
3172 * @freq_out: requested PLL output clock frequency in Hz
3174 * Configures and enables PLL to generate output clock based on input clock.
3176 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3177 unsigned int freq_in, unsigned int freq_out)
3179 if (codec->driver->set_pll)
3180 return codec->driver->set_pll(codec, pll_id, source,
3185 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3188 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3190 * @fmt: SND_SOC_DAIFMT_ format value.
3192 * Configures the DAI hardware format and clocking.
3194 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3196 if (dai->driver == NULL)
3198 if (dai->driver->ops->set_fmt == NULL)
3200 return dai->driver->ops->set_fmt(dai, fmt);
3202 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3205 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3207 * @tx_mask: bitmask representing active TX slots.
3208 * @rx_mask: bitmask representing active RX slots.
3209 * @slots: Number of slots in use.
3210 * @slot_width: Width in bits for each slot.
3212 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3215 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3216 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
3218 if (dai->driver && dai->driver->ops->set_tdm_slot)
3219 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3224 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3227 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3229 * @tx_num: how many TX channels
3230 * @tx_slot: pointer to an array which imply the TX slot number channel
3232 * @rx_num: how many RX channels
3233 * @rx_slot: pointer to an array which imply the RX slot number channel
3236 * configure the relationship between channel number and TDM slot number.
3238 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3239 unsigned int tx_num, unsigned int *tx_slot,
3240 unsigned int rx_num, unsigned int *rx_slot)
3242 if (dai->driver && dai->driver->ops->set_channel_map)
3243 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3248 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3251 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3253 * @tristate: tristate enable
3255 * Tristates the DAI so that others can use it.
3257 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3259 if (dai->driver && dai->driver->ops->set_tristate)
3260 return dai->driver->ops->set_tristate(dai, tristate);
3264 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3267 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3269 * @mute: mute enable
3271 * Mutes the DAI DAC.
3273 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3275 if (dai->driver && dai->driver->ops->digital_mute)
3276 return dai->driver->ops->digital_mute(dai, mute);
3280 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3283 * snd_soc_register_card - Register a card with the ASoC core
3285 * @card: Card to register
3288 int snd_soc_register_card(struct snd_soc_card *card)
3292 if (!card->name || !card->dev)
3295 for (i = 0; i < card->num_links; i++) {
3296 struct snd_soc_dai_link *link = &card->dai_link[i];
3299 * Codec must be specified by 1 of name or OF node,
3300 * not both or neither.
3302 if (!!link->codec_name == !!link->codec_of_node) {
3304 "Neither/both codec name/of_node are set for %s\n",
3310 * Platform may be specified by either name or OF node, but
3311 * can be left unspecified, and a dummy platform will be used.
3313 if (link->platform_name && link->platform_of_node) {
3315 "Both platform name/of_node are set for %s\n", link->name);
3320 * CPU DAI must be specified by 1 of name or OF node,
3321 * not both or neither.
3323 if (!!link->cpu_dai_name == !!link->cpu_dai_of_node) {
3325 "Neither/both cpu_dai name/of_node are set for %s\n",
3331 dev_set_drvdata(card->dev, card);
3333 snd_soc_initialize_card_lists(card);
3335 soc_init_card_debugfs(card);
3337 card->rtd = devm_kzalloc(card->dev,
3338 sizeof(struct snd_soc_pcm_runtime) *
3339 (card->num_links + card->num_aux_devs),
3341 if (card->rtd == NULL)
3343 card->rtd_aux = &card->rtd[card->num_links];
3345 for (i = 0; i < card->num_links; i++)
3346 card->rtd[i].dai_link = &card->dai_link[i];
3348 INIT_LIST_HEAD(&card->list);
3349 INIT_LIST_HEAD(&card->dapm_dirty);
3350 card->instantiated = 0;
3351 mutex_init(&card->mutex);
3352 mutex_init(&card->dapm_mutex);
3354 ret = snd_soc_instantiate_card(card);
3356 soc_cleanup_card_debugfs(card);
3360 EXPORT_SYMBOL_GPL(snd_soc_register_card);
3363 * snd_soc_unregister_card - Unregister a card with the ASoC core
3365 * @card: Card to unregister
3368 int snd_soc_unregister_card(struct snd_soc_card *card)
3370 if (card->instantiated)
3371 soc_cleanup_card_resources(card);
3372 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3376 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3379 * Simplify DAI link configuration by removing ".-1" from device names
3380 * and sanitizing names.
3382 static char *fmt_single_name(struct device *dev, int *id)
3384 char *found, name[NAME_SIZE];
3387 if (dev_name(dev) == NULL)
3390 strlcpy(name, dev_name(dev), NAME_SIZE);
3392 /* are we a "%s.%d" name (platform and SPI components) */
3393 found = strstr(name, dev->driver->name);
3396 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3398 /* discard ID from name if ID == -1 */
3400 found[strlen(dev->driver->name)] = '\0';
3404 /* I2C component devices are named "bus-addr" */
3405 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3406 char tmp[NAME_SIZE];
3408 /* create unique ID number from I2C addr and bus */
3409 *id = ((id1 & 0xffff) << 16) + id2;
3411 /* sanitize component name for DAI link creation */
3412 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3413 strlcpy(name, tmp, NAME_SIZE);
3418 return kstrdup(name, GFP_KERNEL);
3422 * Simplify DAI link naming for single devices with multiple DAIs by removing
3423 * any ".-1" and using the DAI name (instead of device name).
3425 static inline char *fmt_multiple_name(struct device *dev,
3426 struct snd_soc_dai_driver *dai_drv)
3428 if (dai_drv->name == NULL) {
3429 pr_err("asoc: error - multiple DAI %s registered with no name\n",
3434 return kstrdup(dai_drv->name, GFP_KERNEL);
3438 * snd_soc_register_dai - Register a DAI with the ASoC core
3440 * @dai: DAI to register
3442 int snd_soc_register_dai(struct device *dev,
3443 struct snd_soc_dai_driver *dai_drv)
3445 struct snd_soc_codec *codec;
3446 struct snd_soc_dai *dai;
3448 dev_dbg(dev, "dai register %s\n", dev_name(dev));
3450 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3454 /* create DAI component name */
3455 dai->name = fmt_single_name(dev, &dai->id);
3456 if (dai->name == NULL) {
3462 dai->driver = dai_drv;
3463 dai->dapm.dev = dev;
3464 if (!dai->driver->ops)
3465 dai->driver->ops = &null_dai_ops;
3467 mutex_lock(&client_mutex);
3469 list_for_each_entry(codec, &codec_list, list) {
3470 if (codec->dev == dev) {
3471 dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3472 dai->name, codec->name);
3478 list_add(&dai->list, &dai_list);
3480 mutex_unlock(&client_mutex);
3482 pr_debug("Registered DAI '%s'\n", dai->name);
3486 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3489 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3491 * @dai: DAI to unregister
3493 void snd_soc_unregister_dai(struct device *dev)
3495 struct snd_soc_dai *dai;
3497 list_for_each_entry(dai, &dai_list, list) {
3498 if (dev == dai->dev)
3504 mutex_lock(&client_mutex);
3505 list_del(&dai->list);
3506 mutex_unlock(&client_mutex);
3508 pr_debug("Unregistered DAI '%s'\n", dai->name);
3512 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3515 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3517 * @dai: Array of DAIs to register
3518 * @count: Number of DAIs
3520 int snd_soc_register_dais(struct device *dev,
3521 struct snd_soc_dai_driver *dai_drv, size_t count)
3523 struct snd_soc_codec *codec;
3524 struct snd_soc_dai *dai;
3527 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
3529 for (i = 0; i < count; i++) {
3531 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3537 /* create DAI component name */
3538 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3539 if (dai->name == NULL) {
3546 dai->driver = &dai_drv[i];
3547 if (dai->driver->id)
3548 dai->id = dai->driver->id;
3551 dai->dapm.dev = dev;
3552 if (!dai->driver->ops)
3553 dai->driver->ops = &null_dai_ops;
3555 mutex_lock(&client_mutex);
3557 list_for_each_entry(codec, &codec_list, list) {
3558 if (codec->dev == dev) {
3559 dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3560 dai->name, codec->name);
3566 list_add(&dai->list, &dai_list);
3568 mutex_unlock(&client_mutex);
3570 pr_debug("Registered DAI '%s'\n", dai->name);
3576 for (i--; i >= 0; i--)
3577 snd_soc_unregister_dai(dev);
3581 EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3584 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3586 * @dai: Array of DAIs to unregister
3587 * @count: Number of DAIs
3589 void snd_soc_unregister_dais(struct device *dev, size_t count)
3593 for (i = 0; i < count; i++)
3594 snd_soc_unregister_dai(dev);
3596 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3599 * snd_soc_register_platform - Register a platform with the ASoC core
3601 * @platform: platform to register
3603 int snd_soc_register_platform(struct device *dev,
3604 struct snd_soc_platform_driver *platform_drv)
3606 struct snd_soc_platform *platform;
3608 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3610 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3611 if (platform == NULL)
3614 /* create platform component name */
3615 platform->name = fmt_single_name(dev, &platform->id);
3616 if (platform->name == NULL) {
3621 platform->dev = dev;
3622 platform->driver = platform_drv;
3623 platform->dapm.dev = dev;
3624 platform->dapm.platform = platform;
3625 platform->dapm.stream_event = platform_drv->stream_event;
3626 mutex_init(&platform->mutex);
3628 mutex_lock(&client_mutex);
3629 list_add(&platform->list, &platform_list);
3630 mutex_unlock(&client_mutex);
3632 pr_debug("Registered platform '%s'\n", platform->name);
3636 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3639 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3641 * @platform: platform to unregister
3643 void snd_soc_unregister_platform(struct device *dev)
3645 struct snd_soc_platform *platform;
3647 list_for_each_entry(platform, &platform_list, list) {
3648 if (dev == platform->dev)
3654 mutex_lock(&client_mutex);
3655 list_del(&platform->list);
3656 mutex_unlock(&client_mutex);
3658 pr_debug("Unregistered platform '%s'\n", platform->name);
3659 kfree(platform->name);
3662 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3664 static u64 codec_format_map[] = {
3665 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3666 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3667 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3668 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3669 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3670 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3671 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3672 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3673 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3674 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3675 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3676 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3677 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3678 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3679 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3680 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3683 /* Fix up the DAI formats for endianness: codecs don't actually see
3684 * the endianness of the data but we're using the CPU format
3685 * definitions which do need to include endianness so we ensure that
3686 * codec DAIs always have both big and little endian variants set.
3688 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3692 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3693 if (stream->formats & codec_format_map[i])
3694 stream->formats |= codec_format_map[i];
3698 * snd_soc_register_codec - Register a codec with the ASoC core
3700 * @codec: codec to register
3702 int snd_soc_register_codec(struct device *dev,
3703 const struct snd_soc_codec_driver *codec_drv,
3704 struct snd_soc_dai_driver *dai_drv,
3708 struct snd_soc_codec *codec;
3711 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3713 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3717 /* create CODEC component name */
3718 codec->name = fmt_single_name(dev, &codec->id);
3719 if (codec->name == NULL) {
3724 if (codec_drv->compress_type)
3725 codec->compress_type = codec_drv->compress_type;
3727 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3729 codec->write = codec_drv->write;
3730 codec->read = codec_drv->read;
3731 codec->volatile_register = codec_drv->volatile_register;
3732 codec->readable_register = codec_drv->readable_register;
3733 codec->writable_register = codec_drv->writable_register;
3734 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
3735 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3736 codec->dapm.dev = dev;
3737 codec->dapm.codec = codec;
3738 codec->dapm.seq_notifier = codec_drv->seq_notifier;
3739 codec->dapm.stream_event = codec_drv->stream_event;
3741 codec->driver = codec_drv;
3742 codec->num_dai = num_dai;
3743 mutex_init(&codec->mutex);
3745 /* allocate CODEC register cache */
3746 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3747 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
3748 codec->reg_size = reg_size;
3749 /* it is necessary to make a copy of the default register cache
3750 * because in the case of using a compression type that requires
3751 * the default register cache to be marked as __devinitconst the
3752 * kernel might have freed the array by the time we initialize
3755 if (codec_drv->reg_cache_default) {
3756 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3757 reg_size, GFP_KERNEL);
3758 if (!codec->reg_def_copy) {
3765 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3766 if (!codec->volatile_register)
3767 codec->volatile_register = snd_soc_default_volatile_register;
3768 if (!codec->readable_register)
3769 codec->readable_register = snd_soc_default_readable_register;
3770 if (!codec->writable_register)
3771 codec->writable_register = snd_soc_default_writable_register;
3774 for (i = 0; i < num_dai; i++) {
3775 fixup_codec_formats(&dai_drv[i].playback);
3776 fixup_codec_formats(&dai_drv[i].capture);
3779 mutex_lock(&client_mutex);
3780 list_add(&codec->list, &codec_list);
3781 mutex_unlock(&client_mutex);
3783 /* register any DAIs */
3785 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3787 dev_err(codec->dev, "Failed to regster DAIs: %d\n",
3791 pr_debug("Registered codec '%s'\n", codec->name);
3795 kfree(codec->reg_def_copy);
3796 codec->reg_def_copy = NULL;
3801 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3804 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3806 * @codec: codec to unregister
3808 void snd_soc_unregister_codec(struct device *dev)
3810 struct snd_soc_codec *codec;
3813 list_for_each_entry(codec, &codec_list, list) {
3814 if (dev == codec->dev)
3821 for (i = 0; i < codec->num_dai; i++)
3822 snd_soc_unregister_dai(dev);
3824 mutex_lock(&client_mutex);
3825 list_del(&codec->list);
3826 mutex_unlock(&client_mutex);
3828 pr_debug("Unregistered codec '%s'\n", codec->name);
3830 snd_soc_cache_exit(codec);
3831 kfree(codec->reg_def_copy);
3835 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3837 /* Retrieve a card's name from device tree */
3838 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3839 const char *propname)
3841 struct device_node *np = card->dev->of_node;
3844 ret = of_property_read_string_index(np, propname, 0, &card->name);
3846 * EINVAL means the property does not exist. This is fine providing
3847 * card->name was previously set, which is checked later in
3848 * snd_soc_register_card.
3850 if (ret < 0 && ret != -EINVAL) {
3852 "Property '%s' could not be read: %d\n",
3859 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3861 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3862 const char *propname)
3864 struct device_node *np = card->dev->of_node;
3866 struct snd_soc_dapm_route *routes;
3869 num_routes = of_property_count_strings(np, propname);
3870 if (num_routes & 1) {
3872 "Property '%s's length is not even\n",
3879 "Property '%s's length is zero\n",
3884 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3888 "Could not allocate DAPM route table\n");
3892 for (i = 0; i < num_routes; i++) {
3893 ret = of_property_read_string_index(np, propname,
3894 2 * i, &routes[i].sink);
3897 "Property '%s' index %d could not be read: %d\n",
3898 propname, 2 * i, ret);
3901 ret = of_property_read_string_index(np, propname,
3902 (2 * i) + 1, &routes[i].source);
3905 "Property '%s' index %d could not be read: %d\n",
3906 propname, (2 * i) + 1, ret);
3911 card->num_dapm_routes = num_routes;
3912 card->dapm_routes = routes;
3916 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3918 static int __init snd_soc_init(void)
3920 #ifdef CONFIG_DEBUG_FS
3921 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3922 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
3923 pr_warn("ASoC: Failed to create debugfs directory\n");
3924 snd_soc_debugfs_root = NULL;
3927 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
3929 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3931 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
3933 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
3935 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
3936 &platform_list_fops))
3937 pr_warn("ASoC: Failed to create platform list debugfs file\n");
3940 snd_soc_util_init();
3942 return platform_driver_register(&soc_driver);
3944 module_init(snd_soc_init);
3946 static void __exit snd_soc_exit(void)
3948 snd_soc_util_exit();
3950 #ifdef CONFIG_DEBUG_FS
3951 debugfs_remove_recursive(snd_soc_debugfs_root);
3953 platform_driver_unregister(&soc_driver);
3955 module_exit(snd_soc_exit);
3957 /* Module information */
3958 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3959 MODULE_DESCRIPTION("ALSA SoC Core");
3960 MODULE_LICENSE("GPL");
3961 MODULE_ALIAS("platform:soc-audio");