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/slab.h>
34 #include <sound/ac97_codec.h>
35 #include <sound/core.h>
36 #include <sound/pcm.h>
37 #include <sound/pcm_params.h>
38 #include <sound/soc.h>
39 #include <sound/soc-dapm.h>
40 #include <sound/initval.h>
44 static DEFINE_MUTEX(pcm_mutex);
45 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
47 #ifdef CONFIG_DEBUG_FS
48 static struct dentry *debugfs_root;
51 static DEFINE_MUTEX(client_mutex);
52 static LIST_HEAD(card_list);
53 static LIST_HEAD(dai_list);
54 static LIST_HEAD(platform_list);
55 static LIST_HEAD(codec_list);
57 static int snd_soc_register_card(struct snd_soc_card *card);
58 static int snd_soc_unregister_card(struct snd_soc_card *card);
59 static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
62 * This is a timeout to do a DAPM powerdown after a stream is closed().
63 * It can be used to eliminate pops between different playback streams, e.g.
64 * between two audio tracks.
66 static int pmdown_time = 5000;
67 module_param(pmdown_time, int, 0);
68 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
71 * This function forces any delayed work to be queued and run.
73 static int run_delayed_work(struct delayed_work *dwork)
77 /* cancel any work waiting to be queued. */
78 ret = cancel_delayed_work(dwork);
80 /* if there was any work waiting then we run it now and
81 * wait for it's completion */
83 schedule_delayed_work(dwork, 0);
84 flush_scheduled_work();
89 /* codec register dump */
90 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
92 int ret, i, step = 1, count = 0;
94 if (!codec->driver->reg_cache_size)
97 if (codec->driver->reg_cache_step)
98 step = codec->driver->reg_cache_step;
100 count += sprintf(buf, "%s registers\n", codec->name);
101 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
102 if (codec->driver->readable_register && !codec->driver->readable_register(i))
105 count += sprintf(buf + count, "%2x: ", i);
106 if (count >= PAGE_SIZE - 1)
109 if (codec->driver->display_register) {
110 count += codec->driver->display_register(codec, buf + count,
111 PAGE_SIZE - count, i);
113 /* If the read fails it's almost certainly due to
114 * the register being volatile and the device being
117 ret = codec->driver->read(codec, i);
119 count += snprintf(buf + count,
123 count += snprintf(buf + count,
125 "<no data: %d>", ret);
128 if (count >= PAGE_SIZE - 1)
131 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
132 if (count >= PAGE_SIZE - 1)
136 /* Truncate count; min() would cause a warning */
137 if (count >= PAGE_SIZE)
138 count = PAGE_SIZE - 1;
142 static ssize_t codec_reg_show(struct device *dev,
143 struct device_attribute *attr, char *buf)
145 struct snd_soc_pcm_runtime *rtd =
146 container_of(dev, struct snd_soc_pcm_runtime, dev);
148 return soc_codec_reg_show(rtd->codec, buf);
151 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
153 static ssize_t pmdown_time_show(struct device *dev,
154 struct device_attribute *attr, char *buf)
156 struct snd_soc_pcm_runtime *rtd =
157 container_of(dev, struct snd_soc_pcm_runtime, dev);
159 return sprintf(buf, "%ld\n", rtd->pmdown_time);
162 static ssize_t pmdown_time_set(struct device *dev,
163 struct device_attribute *attr,
164 const char *buf, size_t count)
166 struct snd_soc_pcm_runtime *rtd =
167 container_of(dev, struct snd_soc_pcm_runtime, dev);
169 strict_strtol(buf, 10, &rtd->pmdown_time);
174 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
176 #ifdef CONFIG_DEBUG_FS
177 static int codec_reg_open_file(struct inode *inode, struct file *file)
179 file->private_data = inode->i_private;
183 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
184 size_t count, loff_t *ppos)
187 struct snd_soc_codec *codec = file->private_data;
188 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
191 ret = soc_codec_reg_show(codec, buf);
193 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
198 static ssize_t codec_reg_write_file(struct file *file,
199 const char __user *user_buf, size_t count, loff_t *ppos)
204 unsigned long reg, value;
206 struct snd_soc_codec *codec = file->private_data;
208 buf_size = min(count, (sizeof(buf)-1));
209 if (copy_from_user(buf, user_buf, buf_size))
213 if (codec->driver->reg_cache_step)
214 step = codec->driver->reg_cache_step;
216 while (*start == ' ')
218 reg = simple_strtoul(start, &start, 16);
219 if ((reg >= codec->driver->reg_cache_size) || (reg % step))
221 while (*start == ' ')
223 if (strict_strtoul(start, 16, &value))
225 codec->driver->write(codec, reg, value);
229 static const struct file_operations codec_reg_fops = {
230 .open = codec_reg_open_file,
231 .read = codec_reg_read_file,
232 .write = codec_reg_write_file,
235 static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
237 codec->debugfs_codec_root = debugfs_create_dir(codec->name ,
239 if (!codec->debugfs_codec_root) {
241 "ASoC: Failed to create codec debugfs directory\n");
245 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
246 codec->debugfs_codec_root,
247 codec, &codec_reg_fops);
248 if (!codec->debugfs_reg)
250 "ASoC: Failed to create codec register debugfs file\n");
252 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
253 codec->debugfs_codec_root,
255 if (!codec->debugfs_pop_time)
257 "Failed to create pop time debugfs file\n");
259 codec->debugfs_dapm = debugfs_create_dir("dapm",
260 codec->debugfs_codec_root);
261 if (!codec->debugfs_dapm)
263 "Failed to create DAPM debugfs directory\n");
265 snd_soc_dapm_debugfs_init(codec);
268 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
270 debugfs_remove_recursive(codec->debugfs_codec_root);
273 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
274 size_t count, loff_t *ppos)
276 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
277 ssize_t len, ret = 0;
278 struct snd_soc_codec *codec;
283 list_for_each_entry(codec, &codec_list, list) {
284 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
288 if (ret > PAGE_SIZE) {
295 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
302 static const struct file_operations codec_list_fops = {
303 .read = codec_list_read_file,
304 .llseek = default_llseek,/* read accesses f_pos */
307 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
308 size_t count, loff_t *ppos)
310 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
311 ssize_t len, ret = 0;
312 struct snd_soc_dai *dai;
317 list_for_each_entry(dai, &dai_list, list) {
318 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
321 if (ret > PAGE_SIZE) {
327 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
334 static const struct file_operations dai_list_fops = {
335 .read = dai_list_read_file,
336 .llseek = default_llseek,/* read accesses f_pos */
339 static ssize_t platform_list_read_file(struct file *file,
340 char __user *user_buf,
341 size_t count, loff_t *ppos)
343 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
344 ssize_t len, ret = 0;
345 struct snd_soc_platform *platform;
350 list_for_each_entry(platform, &platform_list, list) {
351 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
355 if (ret > PAGE_SIZE) {
361 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
368 static const struct file_operations platform_list_fops = {
369 .read = platform_list_read_file,
370 .llseek = default_llseek,/* read accesses f_pos */
375 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
379 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
384 #ifdef CONFIG_SND_SOC_AC97_BUS
385 /* unregister ac97 codec */
386 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
388 if (codec->ac97->dev.bus)
389 device_unregister(&codec->ac97->dev);
393 /* stop no dev release warning */
394 static void soc_ac97_device_release(struct device *dev){}
396 /* register ac97 codec to bus */
397 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
401 codec->ac97->dev.bus = &ac97_bus_type;
402 codec->ac97->dev.parent = codec->card->dev;
403 codec->ac97->dev.release = soc_ac97_device_release;
405 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
406 codec->card->snd_card->number, 0, codec->name);
407 err = device_register(&codec->ac97->dev);
409 snd_printk(KERN_ERR "Can't register ac97 bus\n");
410 codec->ac97->dev.bus = NULL;
417 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
419 struct snd_soc_pcm_runtime *rtd = substream->private_data;
420 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
421 struct snd_soc_dai *codec_dai = rtd->codec_dai;
424 if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
425 rtd->dai_link->symmetric_rates) {
426 dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
429 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
430 SNDRV_PCM_HW_PARAM_RATE,
435 "Unable to apply rate symmetry constraint: %d\n", ret);
444 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
445 * then initialized and any private data can be allocated. This also calls
446 * startup for the cpu DAI, platform, machine and codec DAI.
448 static int soc_pcm_open(struct snd_pcm_substream *substream)
450 struct snd_soc_pcm_runtime *rtd = substream->private_data;
451 struct snd_pcm_runtime *runtime = substream->runtime;
452 struct snd_soc_platform *platform = rtd->platform;
453 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
454 struct snd_soc_dai *codec_dai = rtd->codec_dai;
455 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
456 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
459 mutex_lock(&pcm_mutex);
461 /* startup the audio subsystem */
462 if (cpu_dai->driver->ops->startup) {
463 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
465 printk(KERN_ERR "asoc: can't open interface %s\n",
471 if (platform->driver->ops->open) {
472 ret = platform->driver->ops->open(substream);
474 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
479 if (codec_dai->driver->ops->startup) {
480 ret = codec_dai->driver->ops->startup(substream, codec_dai);
482 printk(KERN_ERR "asoc: can't open codec %s\n",
488 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
489 ret = rtd->dai_link->ops->startup(substream);
491 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
496 /* Check that the codec and cpu DAI's are compatible */
497 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
498 runtime->hw.rate_min =
499 max(codec_dai_drv->playback.rate_min,
500 cpu_dai_drv->playback.rate_min);
501 runtime->hw.rate_max =
502 min(codec_dai_drv->playback.rate_max,
503 cpu_dai_drv->playback.rate_max);
504 runtime->hw.channels_min =
505 max(codec_dai_drv->playback.channels_min,
506 cpu_dai_drv->playback.channels_min);
507 runtime->hw.channels_max =
508 min(codec_dai_drv->playback.channels_max,
509 cpu_dai_drv->playback.channels_max);
510 runtime->hw.formats =
511 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
513 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
514 if (codec_dai_drv->playback.rates
515 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
516 runtime->hw.rates |= cpu_dai_drv->playback.rates;
517 if (cpu_dai_drv->playback.rates
518 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
519 runtime->hw.rates |= codec_dai_drv->playback.rates;
521 runtime->hw.rate_min =
522 max(codec_dai_drv->capture.rate_min,
523 cpu_dai_drv->capture.rate_min);
524 runtime->hw.rate_max =
525 min(codec_dai_drv->capture.rate_max,
526 cpu_dai_drv->capture.rate_max);
527 runtime->hw.channels_min =
528 max(codec_dai_drv->capture.channels_min,
529 cpu_dai_drv->capture.channels_min);
530 runtime->hw.channels_max =
531 min(codec_dai_drv->capture.channels_max,
532 cpu_dai_drv->capture.channels_max);
533 runtime->hw.formats =
534 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
536 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
537 if (codec_dai_drv->capture.rates
538 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
539 runtime->hw.rates |= cpu_dai_drv->capture.rates;
540 if (cpu_dai_drv->capture.rates
541 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
542 runtime->hw.rates |= codec_dai_drv->capture.rates;
545 snd_pcm_limit_hw_rates(runtime);
546 if (!runtime->hw.rates) {
547 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
548 codec_dai->name, cpu_dai->name);
551 if (!runtime->hw.formats) {
552 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
553 codec_dai->name, cpu_dai->name);
556 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
557 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
558 codec_dai->name, cpu_dai->name);
562 /* Symmetry only applies if we've already got an active stream. */
563 if (cpu_dai->active || codec_dai->active) {
564 ret = soc_pcm_apply_symmetry(substream);
569 pr_debug("asoc: %s <-> %s info:\n",
570 codec_dai->name, cpu_dai->name);
571 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
572 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
573 runtime->hw.channels_max);
574 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
575 runtime->hw.rate_max);
577 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
578 cpu_dai->playback_active++;
579 codec_dai->playback_active++;
581 cpu_dai->capture_active++;
582 codec_dai->capture_active++;
586 rtd->codec->active++;
587 mutex_unlock(&pcm_mutex);
591 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
592 rtd->dai_link->ops->shutdown(substream);
595 if (codec_dai->driver->ops->shutdown)
596 codec_dai->driver->ops->shutdown(substream, codec_dai);
599 if (platform->driver->ops->close)
600 platform->driver->ops->close(substream);
603 if (cpu_dai->driver->ops->shutdown)
604 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
606 mutex_unlock(&pcm_mutex);
611 * Power down the audio subsystem pmdown_time msecs after close is called.
612 * This is to ensure there are no pops or clicks in between any music tracks
613 * due to DAPM power cycling.
615 static void close_delayed_work(struct work_struct *work)
617 struct snd_soc_pcm_runtime *rtd =
618 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
619 struct snd_soc_dai *codec_dai = rtd->codec_dai;
621 mutex_lock(&pcm_mutex);
623 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
624 codec_dai->driver->playback.stream_name,
625 codec_dai->playback_active ? "active" : "inactive",
626 codec_dai->pop_wait ? "yes" : "no");
628 /* are we waiting on this codec DAI stream */
629 if (codec_dai->pop_wait == 1) {
630 codec_dai->pop_wait = 0;
631 snd_soc_dapm_stream_event(rtd,
632 codec_dai->driver->playback.stream_name,
633 SND_SOC_DAPM_STREAM_STOP);
636 mutex_unlock(&pcm_mutex);
640 * Called by ALSA when a PCM substream is closed. Private data can be
641 * freed here. The cpu DAI, codec DAI, machine and platform are also
644 static int soc_codec_close(struct snd_pcm_substream *substream)
646 struct snd_soc_pcm_runtime *rtd = substream->private_data;
647 struct snd_soc_platform *platform = rtd->platform;
648 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
649 struct snd_soc_dai *codec_dai = rtd->codec_dai;
650 struct snd_soc_codec *codec = rtd->codec;
652 mutex_lock(&pcm_mutex);
654 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
655 cpu_dai->playback_active--;
656 codec_dai->playback_active--;
658 cpu_dai->capture_active--;
659 codec_dai->capture_active--;
666 /* Muting the DAC suppresses artifacts caused during digital
667 * shutdown, for example from stopping clocks.
669 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
670 snd_soc_dai_digital_mute(codec_dai, 1);
672 if (cpu_dai->driver->ops->shutdown)
673 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
675 if (codec_dai->driver->ops->shutdown)
676 codec_dai->driver->ops->shutdown(substream, codec_dai);
678 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
679 rtd->dai_link->ops->shutdown(substream);
681 if (platform->driver->ops->close)
682 platform->driver->ops->close(substream);
683 cpu_dai->runtime = NULL;
685 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
686 /* start delayed pop wq here for playback streams */
687 codec_dai->pop_wait = 1;
688 schedule_delayed_work(&rtd->delayed_work,
689 msecs_to_jiffies(rtd->pmdown_time));
691 /* capture streams can be powered down now */
692 snd_soc_dapm_stream_event(rtd,
693 codec_dai->driver->capture.stream_name,
694 SND_SOC_DAPM_STREAM_STOP);
697 mutex_unlock(&pcm_mutex);
702 * Called by ALSA when the PCM substream is prepared, can set format, sample
703 * rate, etc. This function is non atomic and can be called multiple times,
704 * it can refer to the runtime info.
706 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
708 struct snd_soc_pcm_runtime *rtd = substream->private_data;
709 struct snd_soc_platform *platform = rtd->platform;
710 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
711 struct snd_soc_dai *codec_dai = rtd->codec_dai;
714 mutex_lock(&pcm_mutex);
716 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
717 ret = rtd->dai_link->ops->prepare(substream);
719 printk(KERN_ERR "asoc: machine prepare error\n");
724 if (platform->driver->ops->prepare) {
725 ret = platform->driver->ops->prepare(substream);
727 printk(KERN_ERR "asoc: platform prepare error\n");
732 if (codec_dai->driver->ops->prepare) {
733 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
735 printk(KERN_ERR "asoc: codec DAI prepare error\n");
740 if (cpu_dai->driver->ops->prepare) {
741 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
743 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
748 /* cancel any delayed stream shutdown that is pending */
749 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
750 codec_dai->pop_wait) {
751 codec_dai->pop_wait = 0;
752 cancel_delayed_work(&rtd->delayed_work);
755 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
756 snd_soc_dapm_stream_event(rtd,
757 codec_dai->driver->playback.stream_name,
758 SND_SOC_DAPM_STREAM_START);
760 snd_soc_dapm_stream_event(rtd,
761 codec_dai->driver->capture.stream_name,
762 SND_SOC_DAPM_STREAM_START);
764 snd_soc_dai_digital_mute(codec_dai, 0);
767 mutex_unlock(&pcm_mutex);
772 * Called by ALSA when the hardware params are set by application. This
773 * function can also be called multiple times and can allocate buffers
774 * (using snd_pcm_lib_* ). It's non-atomic.
776 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
777 struct snd_pcm_hw_params *params)
779 struct snd_soc_pcm_runtime *rtd = substream->private_data;
780 struct snd_soc_platform *platform = rtd->platform;
781 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
782 struct snd_soc_dai *codec_dai = rtd->codec_dai;
785 mutex_lock(&pcm_mutex);
787 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
788 ret = rtd->dai_link->ops->hw_params(substream, params);
790 printk(KERN_ERR "asoc: machine hw_params failed\n");
795 if (codec_dai->driver->ops->hw_params) {
796 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
798 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
804 if (cpu_dai->driver->ops->hw_params) {
805 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
807 printk(KERN_ERR "asoc: interface %s hw params failed\n",
813 if (platform->driver->ops->hw_params) {
814 ret = platform->driver->ops->hw_params(substream, params);
816 printk(KERN_ERR "asoc: platform %s hw params failed\n",
822 rtd->rate = params_rate(params);
825 mutex_unlock(&pcm_mutex);
829 if (cpu_dai->driver->ops->hw_free)
830 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
833 if (codec_dai->driver->ops->hw_free)
834 codec_dai->driver->ops->hw_free(substream, codec_dai);
837 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
838 rtd->dai_link->ops->hw_free(substream);
840 mutex_unlock(&pcm_mutex);
845 * Free's resources allocated by hw_params, can be called multiple times
847 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
849 struct snd_soc_pcm_runtime *rtd = substream->private_data;
850 struct snd_soc_platform *platform = rtd->platform;
851 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
852 struct snd_soc_dai *codec_dai = rtd->codec_dai;
853 struct snd_soc_codec *codec = rtd->codec;
855 mutex_lock(&pcm_mutex);
857 /* apply codec digital mute */
859 snd_soc_dai_digital_mute(codec_dai, 1);
861 /* free any machine hw params */
862 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
863 rtd->dai_link->ops->hw_free(substream);
865 /* free any DMA resources */
866 if (platform->driver->ops->hw_free)
867 platform->driver->ops->hw_free(substream);
869 /* now free hw params for the DAI's */
870 if (codec_dai->driver->ops->hw_free)
871 codec_dai->driver->ops->hw_free(substream, codec_dai);
873 if (cpu_dai->driver->ops->hw_free)
874 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
876 mutex_unlock(&pcm_mutex);
880 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
882 struct snd_soc_pcm_runtime *rtd = substream->private_data;
883 struct snd_soc_platform *platform = rtd->platform;
884 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
885 struct snd_soc_dai *codec_dai = rtd->codec_dai;
888 if (codec_dai->driver->ops->trigger) {
889 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
894 if (platform->driver->ops->trigger) {
895 ret = platform->driver->ops->trigger(substream, cmd);
900 if (cpu_dai->driver->ops->trigger) {
901 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
909 * soc level wrapper for pointer callback
910 * If cpu_dai, codec_dai, platform driver has the delay callback, than
911 * the runtime->delay will be updated accordingly.
913 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
915 struct snd_soc_pcm_runtime *rtd = substream->private_data;
916 struct snd_soc_platform *platform = rtd->platform;
917 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
918 struct snd_soc_dai *codec_dai = rtd->codec_dai;
919 struct snd_pcm_runtime *runtime = substream->runtime;
920 snd_pcm_uframes_t offset = 0;
921 snd_pcm_sframes_t delay = 0;
923 if (platform->driver->ops->pointer)
924 offset = platform->driver->ops->pointer(substream);
926 if (cpu_dai->driver->ops->delay)
927 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
929 if (codec_dai->driver->ops->delay)
930 delay += codec_dai->driver->ops->delay(substream, codec_dai);
932 if (platform->driver->delay)
933 delay += platform->driver->delay(substream, codec_dai);
935 runtime->delay = delay;
940 /* ASoC PCM operations */
941 static struct snd_pcm_ops soc_pcm_ops = {
942 .open = soc_pcm_open,
943 .close = soc_codec_close,
944 .hw_params = soc_pcm_hw_params,
945 .hw_free = soc_pcm_hw_free,
946 .prepare = soc_pcm_prepare,
947 .trigger = soc_pcm_trigger,
948 .pointer = soc_pcm_pointer,
952 /* powers down audio subsystem for suspend */
953 static int soc_suspend(struct device *dev)
955 struct platform_device *pdev = to_platform_device(dev);
956 struct snd_soc_card *card = platform_get_drvdata(pdev);
959 /* If the initialization of this soc device failed, there is no codec
960 * associated with it. Just bail out in this case.
962 if (list_empty(&card->codec_dev_list))
965 /* Due to the resume being scheduled into a workqueue we could
966 * suspend before that's finished - wait for it to complete.
968 snd_power_lock(card->snd_card);
969 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
970 snd_power_unlock(card->snd_card);
972 /* we're going to block userspace touching us until resume completes */
973 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
975 /* mute any active DAC's */
976 for (i = 0; i < card->num_rtd; i++) {
977 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
978 struct snd_soc_dai_driver *drv = dai->driver;
980 if (card->rtd[i].dai_link->ignore_suspend)
983 if (drv->ops->digital_mute && dai->playback_active)
984 drv->ops->digital_mute(dai, 1);
987 /* suspend all pcms */
988 for (i = 0; i < card->num_rtd; i++) {
989 if (card->rtd[i].dai_link->ignore_suspend)
992 snd_pcm_suspend_all(card->rtd[i].pcm);
995 if (card->suspend_pre)
996 card->suspend_pre(pdev, PMSG_SUSPEND);
998 for (i = 0; i < card->num_rtd; i++) {
999 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1000 struct snd_soc_platform *platform = card->rtd[i].platform;
1002 if (card->rtd[i].dai_link->ignore_suspend)
1005 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
1006 cpu_dai->driver->suspend(cpu_dai);
1007 if (platform->driver->suspend && !platform->suspended) {
1008 platform->driver->suspend(cpu_dai);
1009 platform->suspended = 1;
1013 /* close any waiting streams and save state */
1014 for (i = 0; i < card->num_rtd; i++) {
1015 run_delayed_work(&card->rtd[i].delayed_work);
1016 card->rtd[i].codec->suspend_bias_level = card->rtd[i].codec->bias_level;
1019 for (i = 0; i < card->num_rtd; i++) {
1020 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1022 if (card->rtd[i].dai_link->ignore_suspend)
1025 if (driver->playback.stream_name != NULL)
1026 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1027 SND_SOC_DAPM_STREAM_SUSPEND);
1029 if (driver->capture.stream_name != NULL)
1030 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1031 SND_SOC_DAPM_STREAM_SUSPEND);
1034 /* suspend all CODECs */
1035 for (i = 0; i < card->num_rtd; i++) {
1036 struct snd_soc_codec *codec = card->rtd[i].codec;
1037 /* If there are paths active then the CODEC will be held with
1038 * bias _ON and should not be suspended. */
1039 if (!codec->suspended && codec->driver->suspend) {
1040 switch (codec->bias_level) {
1041 case SND_SOC_BIAS_STANDBY:
1042 case SND_SOC_BIAS_OFF:
1043 codec->driver->suspend(codec, PMSG_SUSPEND);
1044 codec->suspended = 1;
1047 dev_dbg(codec->dev, "CODEC is on over suspend\n");
1053 for (i = 0; i < card->num_rtd; i++) {
1054 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1056 if (card->rtd[i].dai_link->ignore_suspend)
1059 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1060 cpu_dai->driver->suspend(cpu_dai);
1063 if (card->suspend_post)
1064 card->suspend_post(pdev, PMSG_SUSPEND);
1069 /* deferred resume work, so resume can complete before we finished
1070 * setting our codec back up, which can be very slow on I2C
1072 static void soc_resume_deferred(struct work_struct *work)
1074 struct snd_soc_card *card =
1075 container_of(work, struct snd_soc_card, deferred_resume_work);
1076 struct platform_device *pdev = to_platform_device(card->dev);
1079 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1080 * so userspace apps are blocked from touching us
1083 dev_dbg(card->dev, "starting resume work\n");
1085 /* Bring us up into D2 so that DAPM starts enabling things */
1086 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
1088 if (card->resume_pre)
1089 card->resume_pre(pdev);
1091 /* resume AC97 DAIs */
1092 for (i = 0; i < card->num_rtd; i++) {
1093 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1095 if (card->rtd[i].dai_link->ignore_suspend)
1098 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1099 cpu_dai->driver->resume(cpu_dai);
1102 for (i = 0; i < card->num_rtd; i++) {
1103 struct snd_soc_codec *codec = card->rtd[i].codec;
1104 /* If the CODEC was idle over suspend then it will have been
1105 * left with bias OFF or STANDBY and suspended so we must now
1106 * resume. Otherwise the suspend was suppressed.
1108 if (codec->driver->resume && codec->suspended) {
1109 switch (codec->bias_level) {
1110 case SND_SOC_BIAS_STANDBY:
1111 case SND_SOC_BIAS_OFF:
1112 codec->driver->resume(codec);
1113 codec->suspended = 0;
1116 dev_dbg(codec->dev, "CODEC was on over suspend\n");
1122 for (i = 0; i < card->num_rtd; i++) {
1123 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1125 if (card->rtd[i].dai_link->ignore_suspend)
1128 if (driver->playback.stream_name != NULL)
1129 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1130 SND_SOC_DAPM_STREAM_RESUME);
1132 if (driver->capture.stream_name != NULL)
1133 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1134 SND_SOC_DAPM_STREAM_RESUME);
1137 /* unmute any active DACs */
1138 for (i = 0; i < card->num_rtd; i++) {
1139 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1140 struct snd_soc_dai_driver *drv = dai->driver;
1142 if (card->rtd[i].dai_link->ignore_suspend)
1145 if (drv->ops->digital_mute && dai->playback_active)
1146 drv->ops->digital_mute(dai, 0);
1149 for (i = 0; i < card->num_rtd; i++) {
1150 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1151 struct snd_soc_platform *platform = card->rtd[i].platform;
1153 if (card->rtd[i].dai_link->ignore_suspend)
1156 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1157 cpu_dai->driver->resume(cpu_dai);
1158 if (platform->driver->resume && platform->suspended) {
1159 platform->driver->resume(cpu_dai);
1160 platform->suspended = 0;
1164 if (card->resume_post)
1165 card->resume_post(pdev);
1167 dev_dbg(card->dev, "resume work completed\n");
1169 /* userspace can access us now we are back as we were before */
1170 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
1173 /* powers up audio subsystem after a suspend */
1174 static int soc_resume(struct device *dev)
1176 struct platform_device *pdev = to_platform_device(dev);
1177 struct snd_soc_card *card = platform_get_drvdata(pdev);
1180 /* AC97 devices might have other drivers hanging off them so
1181 * need to resume immediately. Other drivers don't have that
1182 * problem and may take a substantial amount of time to resume
1183 * due to I/O costs and anti-pop so handle them out of line.
1185 for (i = 0; i < card->num_rtd; i++) {
1186 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1187 if (cpu_dai->driver->ac97_control) {
1188 dev_dbg(dev, "Resuming AC97 immediately\n");
1189 soc_resume_deferred(&card->deferred_resume_work);
1191 dev_dbg(dev, "Scheduling resume work\n");
1192 if (!schedule_work(&card->deferred_resume_work))
1193 dev_err(dev, "resume work item may be lost\n");
1200 #define soc_suspend NULL
1201 #define soc_resume NULL
1204 static struct snd_soc_dai_ops null_dai_ops = {
1207 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
1209 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1210 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1211 struct snd_soc_codec *codec;
1212 struct snd_soc_platform *platform;
1213 struct snd_soc_dai *codec_dai, *cpu_dai;
1217 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
1219 /* do we already have the CPU DAI for this link ? */
1223 /* no, then find CPU DAI from registered DAIs*/
1224 list_for_each_entry(cpu_dai, &dai_list, list) {
1225 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1227 if (!try_module_get(cpu_dai->dev->driver->owner))
1230 rtd->cpu_dai = cpu_dai;
1234 dev_dbg(card->dev, "CPU DAI %s not registered\n",
1235 dai_link->cpu_dai_name);
1238 /* do we already have the CODEC for this link ? */
1243 /* no, then find CODEC from registered CODECs*/
1244 list_for_each_entry(codec, &codec_list, list) {
1245 if (!strcmp(codec->name, dai_link->codec_name)) {
1248 if (!try_module_get(codec->dev->driver->owner))
1251 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1252 list_for_each_entry(codec_dai, &dai_list, list) {
1253 if (codec->dev == codec_dai->dev &&
1254 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1255 rtd->codec_dai = codec_dai;
1259 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1260 dai_link->codec_dai_name);
1265 dev_dbg(card->dev, "CODEC %s not registered\n",
1266 dai_link->codec_name);
1269 /* do we already have the CODEC DAI for this link ? */
1270 if (rtd->platform) {
1273 /* no, then find CPU DAI from registered DAIs*/
1274 list_for_each_entry(platform, &platform_list, list) {
1275 if (!strcmp(platform->name, dai_link->platform_name)) {
1277 if (!try_module_get(platform->dev->driver->owner))
1280 rtd->platform = platform;
1285 dev_dbg(card->dev, "platform %s not registered\n",
1286 dai_link->platform_name);
1290 /* mark rtd as complete if we found all 4 of our client devices */
1291 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1298 static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1300 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1301 struct snd_soc_codec *codec = rtd->codec;
1302 struct snd_soc_platform *platform = rtd->platform;
1303 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1306 /* unregister the rtd device */
1307 if (rtd->dev_registered) {
1308 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
1309 device_unregister(&rtd->dev);
1310 rtd->dev_registered = 0;
1313 /* remove the CODEC DAI */
1314 if (codec_dai && codec_dai->probed) {
1315 if (codec_dai->driver->remove) {
1316 err = codec_dai->driver->remove(codec_dai);
1318 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1320 codec_dai->probed = 0;
1321 list_del(&codec_dai->card_list);
1324 /* remove the platform */
1325 if (platform && platform->probed) {
1326 if (platform->driver->remove) {
1327 err = platform->driver->remove(platform);
1329 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1331 platform->probed = 0;
1332 list_del(&platform->card_list);
1333 module_put(platform->dev->driver->owner);
1336 /* remove the CODEC */
1337 if (codec && codec->probed) {
1338 if (codec->driver->remove) {
1339 err = codec->driver->remove(codec);
1341 printk(KERN_ERR "asoc: failed to remove %s\n", codec->name);
1344 /* Make sure all DAPM widgets are freed */
1345 snd_soc_dapm_free(codec);
1347 soc_cleanup_codec_debugfs(codec);
1348 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1350 list_del(&codec->card_list);
1351 module_put(codec->dev->driver->owner);
1354 /* remove the cpu_dai */
1355 if (cpu_dai && cpu_dai->probed) {
1356 if (cpu_dai->driver->remove) {
1357 err = cpu_dai->driver->remove(cpu_dai);
1359 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1361 cpu_dai->probed = 0;
1362 list_del(&cpu_dai->card_list);
1363 module_put(cpu_dai->dev->driver->owner);
1367 static void rtd_release(struct device *dev) {}
1369 static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1371 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1372 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1373 struct snd_soc_codec *codec = rtd->codec;
1374 struct snd_soc_platform *platform = rtd->platform;
1375 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1378 dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1380 /* config components */
1381 codec_dai->codec = codec;
1383 cpu_dai->platform = platform;
1385 rtd->dev.parent = card->dev;
1386 codec_dai->card = card;
1387 cpu_dai->card = card;
1389 /* set default power off timeout */
1390 rtd->pmdown_time = pmdown_time;
1392 /* probe the cpu_dai */
1393 if (!cpu_dai->probed) {
1394 if (cpu_dai->driver->probe) {
1395 ret = cpu_dai->driver->probe(cpu_dai);
1397 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1402 cpu_dai->probed = 1;
1403 /* mark cpu_dai as probed and add to card cpu_dai list */
1404 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1407 /* probe the CODEC */
1408 if (!codec->probed) {
1409 if (codec->driver->probe) {
1410 ret = codec->driver->probe(codec);
1412 printk(KERN_ERR "asoc: failed to probe CODEC %s\n",
1418 soc_init_codec_debugfs(codec);
1420 /* mark codec as probed and add to card codec list */
1422 list_add(&codec->card_list, &card->codec_dev_list);
1425 /* probe the platform */
1426 if (!platform->probed) {
1427 if (platform->driver->probe) {
1428 ret = platform->driver->probe(platform);
1430 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1435 /* mark platform as probed and add to card platform list */
1436 platform->probed = 1;
1437 list_add(&platform->card_list, &card->platform_dev_list);
1440 /* probe the CODEC DAI */
1441 if (!codec_dai->probed) {
1442 if (codec_dai->driver->probe) {
1443 ret = codec_dai->driver->probe(codec_dai);
1445 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1451 /* mark cpu_dai as probed and add to card cpu_dai list */
1452 codec_dai->probed = 1;
1453 list_add(&codec_dai->card_list, &card->dai_dev_list);
1456 /* DAPM dai link stream work */
1457 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1459 /* now that all clients have probed, initialise the DAI link */
1460 if (dai_link->init) {
1461 ret = dai_link->init(rtd);
1463 printk(KERN_ERR "asoc: failed to init %s\n", dai_link->stream_name);
1468 /* Make sure all DAPM widgets are instantiated */
1469 snd_soc_dapm_new_widgets(codec);
1470 snd_soc_dapm_sync(codec);
1472 /* register the rtd device */
1473 rtd->dev.release = rtd_release;
1474 rtd->dev.init_name = dai_link->name;
1475 ret = device_register(&rtd->dev);
1477 printk(KERN_ERR "asoc: failed to register DAI runtime device %d\n", ret);
1481 rtd->dev_registered = 1;
1482 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1484 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1486 /* add DAPM sysfs entries for this codec */
1487 ret = snd_soc_dapm_sys_add(&rtd->dev);
1489 printk(KERN_WARNING "asoc: failed to add codec dapm sysfs entries\n");
1491 /* add codec sysfs entries */
1492 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1494 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1496 /* create the pcm */
1497 ret = soc_new_pcm(rtd, num);
1499 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1503 /* add platform data for AC97 devices */
1504 if (rtd->codec_dai->driver->ac97_control)
1505 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1510 #ifdef CONFIG_SND_SOC_AC97_BUS
1511 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1515 /* Only instantiate AC97 if not already done by the adaptor
1516 * for the generic AC97 subsystem.
1518 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
1520 * It is possible that the AC97 device is already registered to
1521 * the device subsystem. This happens when the device is created
1522 * via snd_ac97_mixer(). Currently only SoC codec that does so
1523 * is the generic AC97 glue but others migh emerge.
1525 * In those cases we don't try to register the device again.
1527 if (!rtd->codec->ac97_created)
1530 ret = soc_ac97_dev_register(rtd->codec);
1532 printk(KERN_ERR "asoc: AC97 device register failed\n");
1536 rtd->codec->ac97_registered = 1;
1541 static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1543 if (codec->ac97_registered) {
1544 soc_ac97_dev_unregister(codec);
1545 codec->ac97_registered = 0;
1550 static void snd_soc_instantiate_card(struct snd_soc_card *card)
1552 struct platform_device *pdev = to_platform_device(card->dev);
1555 mutex_lock(&card->mutex);
1557 if (card->instantiated) {
1558 mutex_unlock(&card->mutex);
1563 for (i = 0; i < card->num_links; i++)
1564 soc_bind_dai_link(card, i);
1566 /* bind completed ? */
1567 if (card->num_rtd != card->num_links) {
1568 mutex_unlock(&card->mutex);
1572 /* card bind complete so register a sound card */
1573 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1574 card->owner, 0, &card->snd_card);
1576 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1578 mutex_unlock(&card->mutex);
1581 card->snd_card->dev = card->dev;
1584 /* deferred resume work */
1585 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1588 /* initialise the sound card only once */
1590 ret = card->probe(pdev);
1592 goto card_probe_error;
1595 for (i = 0; i < card->num_links; i++) {
1596 ret = soc_probe_dai_link(card, i);
1598 pr_err("asoc: failed to instantiate card %s: %d\n",
1604 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1606 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1609 ret = snd_card_register(card->snd_card);
1611 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1615 #ifdef CONFIG_SND_SOC_AC97_BUS
1616 /* register any AC97 codecs */
1617 for (i = 0; i < card->num_rtd; i++) {
1618 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1620 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1626 card->instantiated = 1;
1627 mutex_unlock(&card->mutex);
1631 for (i = 0; i < card->num_links; i++)
1632 soc_remove_dai_link(card, i);
1638 snd_card_free(card->snd_card);
1640 mutex_unlock(&card->mutex);
1644 * Attempt to initialise any uninitialised cards. Must be called with
1647 static void snd_soc_instantiate_cards(void)
1649 struct snd_soc_card *card;
1650 list_for_each_entry(card, &card_list, list)
1651 snd_soc_instantiate_card(card);
1654 /* probes a new socdev */
1655 static int soc_probe(struct platform_device *pdev)
1657 struct snd_soc_card *card = platform_get_drvdata(pdev);
1660 /* Bodge while we unpick instantiation */
1661 card->dev = &pdev->dev;
1662 INIT_LIST_HEAD(&card->dai_dev_list);
1663 INIT_LIST_HEAD(&card->codec_dev_list);
1664 INIT_LIST_HEAD(&card->platform_dev_list);
1666 ret = snd_soc_register_card(card);
1668 dev_err(&pdev->dev, "Failed to register card\n");
1675 /* removes a socdev */
1676 static int soc_remove(struct platform_device *pdev)
1678 struct snd_soc_card *card = platform_get_drvdata(pdev);
1681 if (card->instantiated) {
1683 /* make sure any delayed work runs */
1684 for (i = 0; i < card->num_rtd; i++) {
1685 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1686 run_delayed_work(&rtd->delayed_work);
1689 /* remove and free each DAI */
1690 for (i = 0; i < card->num_rtd; i++)
1691 soc_remove_dai_link(card, i);
1693 /* remove the card */
1698 snd_card_free(card->snd_card);
1700 snd_soc_unregister_card(card);
1704 static int soc_poweroff(struct device *dev)
1706 struct platform_device *pdev = to_platform_device(dev);
1707 struct snd_soc_card *card = platform_get_drvdata(pdev);
1710 if (!card->instantiated)
1713 /* Flush out pmdown_time work - we actually do want to run it
1714 * now, we're shutting down so no imminent restart. */
1715 for (i = 0; i < card->num_rtd; i++) {
1716 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1717 run_delayed_work(&rtd->delayed_work);
1720 snd_soc_dapm_shutdown(card);
1725 static const struct dev_pm_ops soc_pm_ops = {
1726 .suspend = soc_suspend,
1727 .resume = soc_resume,
1728 .poweroff = soc_poweroff,
1731 /* ASoC platform driver */
1732 static struct platform_driver soc_driver = {
1734 .name = "soc-audio",
1735 .owner = THIS_MODULE,
1739 .remove = soc_remove,
1742 /* create a new pcm */
1743 static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
1745 struct snd_soc_codec *codec = rtd->codec;
1746 struct snd_soc_platform *platform = rtd->platform;
1747 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1748 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1749 struct snd_pcm *pcm;
1751 int ret = 0, playback = 0, capture = 0;
1753 /* check client and interface hw capabilities */
1754 snprintf(new_name, sizeof(new_name), "%s %s-%d",
1755 rtd->dai_link->stream_name, codec_dai->name, num);
1757 if (codec_dai->driver->playback.channels_min)
1759 if (codec_dai->driver->capture.channels_min)
1762 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
1763 ret = snd_pcm_new(rtd->card->snd_card, new_name,
1764 num, playback, capture, &pcm);
1766 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
1771 pcm->private_data = rtd;
1772 soc_pcm_ops.mmap = platform->driver->ops->mmap;
1773 soc_pcm_ops.pointer = platform->driver->ops->pointer;
1774 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
1775 soc_pcm_ops.copy = platform->driver->ops->copy;
1776 soc_pcm_ops.silence = platform->driver->ops->silence;
1777 soc_pcm_ops.ack = platform->driver->ops->ack;
1778 soc_pcm_ops.page = platform->driver->ops->page;
1781 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1784 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1786 ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
1788 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1792 pcm->private_free = platform->driver->pcm_free;
1793 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1799 * snd_soc_codec_volatile_register: Report if a register is volatile.
1801 * @codec: CODEC to query.
1802 * @reg: Register to query.
1804 * Boolean function indiciating if a CODEC register is volatile.
1806 int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1808 if (codec->driver->volatile_register)
1809 return codec->driver->volatile_register(reg);
1813 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1816 * snd_soc_new_ac97_codec - initailise AC97 device
1817 * @codec: audio codec
1818 * @ops: AC97 bus operations
1819 * @num: AC97 codec number
1821 * Initialises AC97 codec resources for use by ad-hoc devices only.
1823 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1824 struct snd_ac97_bus_ops *ops, int num)
1826 mutex_lock(&codec->mutex);
1828 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1829 if (codec->ac97 == NULL) {
1830 mutex_unlock(&codec->mutex);
1834 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1835 if (codec->ac97->bus == NULL) {
1838 mutex_unlock(&codec->mutex);
1842 codec->ac97->bus->ops = ops;
1843 codec->ac97->num = num;
1846 * Mark the AC97 device to be created by us. This way we ensure that the
1847 * device will be registered with the device subsystem later on.
1849 codec->ac97_created = 1;
1851 mutex_unlock(&codec->mutex);
1854 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1857 * snd_soc_free_ac97_codec - free AC97 codec device
1858 * @codec: audio codec
1860 * Frees AC97 codec device resources.
1862 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1864 mutex_lock(&codec->mutex);
1865 #ifdef CONFIG_SND_SOC_AC97_BUS
1866 soc_unregister_ac97_dai_link(codec);
1868 kfree(codec->ac97->bus);
1871 codec->ac97_created = 0;
1872 mutex_unlock(&codec->mutex);
1874 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1877 * snd_soc_update_bits - update codec register bits
1878 * @codec: audio codec
1879 * @reg: codec register
1880 * @mask: register mask
1883 * Writes new register value.
1885 * Returns 1 for change else 0.
1887 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1888 unsigned int mask, unsigned int value)
1891 unsigned int old, new;
1893 old = snd_soc_read(codec, reg);
1894 new = (old & ~mask) | value;
1895 change = old != new;
1897 snd_soc_write(codec, reg, new);
1901 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1904 * snd_soc_update_bits_locked - update codec register bits
1905 * @codec: audio codec
1906 * @reg: codec register
1907 * @mask: register mask
1910 * Writes new register value, and takes the codec mutex.
1912 * Returns 1 for change else 0.
1914 int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1915 unsigned short reg, unsigned int mask,
1920 mutex_lock(&codec->mutex);
1921 change = snd_soc_update_bits(codec, reg, mask, value);
1922 mutex_unlock(&codec->mutex);
1926 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
1929 * snd_soc_test_bits - test register for change
1930 * @codec: audio codec
1931 * @reg: codec register
1932 * @mask: register mask
1935 * Tests a register with a new value and checks if the new value is
1936 * different from the old value.
1938 * Returns 1 for change else 0.
1940 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1941 unsigned int mask, unsigned int value)
1944 unsigned int old, new;
1946 old = snd_soc_read(codec, reg);
1947 new = (old & ~mask) | value;
1948 change = old != new;
1952 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1955 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1956 * @substream: the pcm substream
1957 * @hw: the hardware parameters
1959 * Sets the substream runtime hardware parameters.
1961 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1962 const struct snd_pcm_hardware *hw)
1964 struct snd_pcm_runtime *runtime = substream->runtime;
1965 runtime->hw.info = hw->info;
1966 runtime->hw.formats = hw->formats;
1967 runtime->hw.period_bytes_min = hw->period_bytes_min;
1968 runtime->hw.period_bytes_max = hw->period_bytes_max;
1969 runtime->hw.periods_min = hw->periods_min;
1970 runtime->hw.periods_max = hw->periods_max;
1971 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1972 runtime->hw.fifo_size = hw->fifo_size;
1975 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1978 * snd_soc_cnew - create new control
1979 * @_template: control template
1980 * @data: control private data
1981 * @long_name: control long name
1983 * Create a new mixer control from a template control.
1985 * Returns 0 for success, else error.
1987 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1988 void *data, char *long_name)
1990 struct snd_kcontrol_new template;
1992 memcpy(&template, _template, sizeof(template));
1994 template.name = long_name;
1997 return snd_ctl_new1(&template, data);
1999 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2002 * snd_soc_add_controls - add an array of controls to a codec.
2003 * Convienience function to add a list of controls. Many codecs were
2004 * duplicating this code.
2006 * @codec: codec to add controls to
2007 * @controls: array of controls to add
2008 * @num_controls: number of elements in the array
2010 * Return 0 for success, else error.
2012 int snd_soc_add_controls(struct snd_soc_codec *codec,
2013 const struct snd_kcontrol_new *controls, int num_controls)
2015 struct snd_card *card = codec->card->snd_card;
2018 for (i = 0; i < num_controls; i++) {
2019 const struct snd_kcontrol_new *control = &controls[i];
2020 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
2022 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
2023 codec->name, control->name, err);
2030 EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2033 * snd_soc_info_enum_double - enumerated double mixer info callback
2034 * @kcontrol: mixer control
2035 * @uinfo: control element information
2037 * Callback to provide information about a double enumerated
2040 * Returns 0 for success.
2042 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2043 struct snd_ctl_elem_info *uinfo)
2045 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2047 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2048 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2049 uinfo->value.enumerated.items = e->max;
2051 if (uinfo->value.enumerated.item > e->max - 1)
2052 uinfo->value.enumerated.item = e->max - 1;
2053 strcpy(uinfo->value.enumerated.name,
2054 e->texts[uinfo->value.enumerated.item]);
2057 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2060 * snd_soc_get_enum_double - enumerated double mixer get callback
2061 * @kcontrol: mixer control
2062 * @ucontrol: control element information
2064 * Callback to get the value of a double enumerated mixer.
2066 * Returns 0 for success.
2068 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2069 struct snd_ctl_elem_value *ucontrol)
2071 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2072 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2073 unsigned int val, bitmask;
2075 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2077 val = snd_soc_read(codec, e->reg);
2078 ucontrol->value.enumerated.item[0]
2079 = (val >> e->shift_l) & (bitmask - 1);
2080 if (e->shift_l != e->shift_r)
2081 ucontrol->value.enumerated.item[1] =
2082 (val >> e->shift_r) & (bitmask - 1);
2086 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2089 * snd_soc_put_enum_double - enumerated double mixer put callback
2090 * @kcontrol: mixer control
2091 * @ucontrol: control element information
2093 * Callback to set the value of a double enumerated mixer.
2095 * Returns 0 for success.
2097 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2098 struct snd_ctl_elem_value *ucontrol)
2100 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2101 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2103 unsigned int mask, bitmask;
2105 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2107 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2109 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2110 mask = (bitmask - 1) << e->shift_l;
2111 if (e->shift_l != e->shift_r) {
2112 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2114 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2115 mask |= (bitmask - 1) << e->shift_r;
2118 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2120 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2123 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2124 * @kcontrol: mixer control
2125 * @ucontrol: control element information
2127 * Callback to get the value of a double semi enumerated mixer.
2129 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2130 * used for handling bitfield coded enumeration for example.
2132 * Returns 0 for success.
2134 int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2135 struct snd_ctl_elem_value *ucontrol)
2137 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2138 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2139 unsigned int reg_val, val, mux;
2141 reg_val = snd_soc_read(codec, e->reg);
2142 val = (reg_val >> e->shift_l) & e->mask;
2143 for (mux = 0; mux < e->max; mux++) {
2144 if (val == e->values[mux])
2147 ucontrol->value.enumerated.item[0] = mux;
2148 if (e->shift_l != e->shift_r) {
2149 val = (reg_val >> e->shift_r) & e->mask;
2150 for (mux = 0; mux < e->max; mux++) {
2151 if (val == e->values[mux])
2154 ucontrol->value.enumerated.item[1] = mux;
2159 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2162 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2163 * @kcontrol: mixer control
2164 * @ucontrol: control element information
2166 * Callback to set the value of a double semi enumerated mixer.
2168 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2169 * used for handling bitfield coded enumeration for example.
2171 * Returns 0 for success.
2173 int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2174 struct snd_ctl_elem_value *ucontrol)
2176 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2177 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2181 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2183 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2184 mask = e->mask << e->shift_l;
2185 if (e->shift_l != e->shift_r) {
2186 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2188 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2189 mask |= e->mask << e->shift_r;
2192 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2194 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2197 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2198 * @kcontrol: mixer control
2199 * @uinfo: control element information
2201 * Callback to provide information about an external enumerated
2204 * Returns 0 for success.
2206 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2207 struct snd_ctl_elem_info *uinfo)
2209 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2211 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2213 uinfo->value.enumerated.items = e->max;
2215 if (uinfo->value.enumerated.item > e->max - 1)
2216 uinfo->value.enumerated.item = e->max - 1;
2217 strcpy(uinfo->value.enumerated.name,
2218 e->texts[uinfo->value.enumerated.item]);
2221 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2224 * snd_soc_info_volsw_ext - external single mixer info callback
2225 * @kcontrol: mixer control
2226 * @uinfo: control element information
2228 * Callback to provide information about a single external mixer control.
2230 * Returns 0 for success.
2232 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2233 struct snd_ctl_elem_info *uinfo)
2235 int max = kcontrol->private_value;
2237 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2238 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2240 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2243 uinfo->value.integer.min = 0;
2244 uinfo->value.integer.max = max;
2247 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2250 * snd_soc_info_volsw - single mixer info callback
2251 * @kcontrol: mixer control
2252 * @uinfo: control element information
2254 * Callback to provide information about a single mixer control.
2256 * Returns 0 for success.
2258 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2259 struct snd_ctl_elem_info *uinfo)
2261 struct soc_mixer_control *mc =
2262 (struct soc_mixer_control *)kcontrol->private_value;
2264 unsigned int shift = mc->shift;
2265 unsigned int rshift = mc->rshift;
2267 if (!mc->platform_max)
2268 mc->platform_max = mc->max;
2269 platform_max = mc->platform_max;
2271 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2272 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2274 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2276 uinfo->count = shift == rshift ? 1 : 2;
2277 uinfo->value.integer.min = 0;
2278 uinfo->value.integer.max = platform_max;
2281 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2284 * snd_soc_get_volsw - single mixer get callback
2285 * @kcontrol: mixer control
2286 * @ucontrol: control element information
2288 * Callback to get the value of a single mixer control.
2290 * Returns 0 for success.
2292 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2293 struct snd_ctl_elem_value *ucontrol)
2295 struct soc_mixer_control *mc =
2296 (struct soc_mixer_control *)kcontrol->private_value;
2297 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2298 unsigned int reg = mc->reg;
2299 unsigned int shift = mc->shift;
2300 unsigned int rshift = mc->rshift;
2302 unsigned int mask = (1 << fls(max)) - 1;
2303 unsigned int invert = mc->invert;
2305 ucontrol->value.integer.value[0] =
2306 (snd_soc_read(codec, reg) >> shift) & mask;
2307 if (shift != rshift)
2308 ucontrol->value.integer.value[1] =
2309 (snd_soc_read(codec, reg) >> rshift) & mask;
2311 ucontrol->value.integer.value[0] =
2312 max - ucontrol->value.integer.value[0];
2313 if (shift != rshift)
2314 ucontrol->value.integer.value[1] =
2315 max - ucontrol->value.integer.value[1];
2320 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2323 * snd_soc_put_volsw - single mixer put callback
2324 * @kcontrol: mixer control
2325 * @ucontrol: control element information
2327 * Callback to set the value of a single mixer control.
2329 * Returns 0 for success.
2331 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2332 struct snd_ctl_elem_value *ucontrol)
2334 struct soc_mixer_control *mc =
2335 (struct soc_mixer_control *)kcontrol->private_value;
2336 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2337 unsigned int reg = mc->reg;
2338 unsigned int shift = mc->shift;
2339 unsigned int rshift = mc->rshift;
2341 unsigned int mask = (1 << fls(max)) - 1;
2342 unsigned int invert = mc->invert;
2343 unsigned int val, val2, val_mask;
2345 val = (ucontrol->value.integer.value[0] & mask);
2348 val_mask = mask << shift;
2350 if (shift != rshift) {
2351 val2 = (ucontrol->value.integer.value[1] & mask);
2354 val_mask |= mask << rshift;
2355 val |= val2 << rshift;
2357 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2359 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2362 * snd_soc_info_volsw_2r - double mixer info callback
2363 * @kcontrol: mixer control
2364 * @uinfo: control element information
2366 * Callback to provide information about a double mixer control that
2367 * spans 2 codec registers.
2369 * Returns 0 for success.
2371 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2372 struct snd_ctl_elem_info *uinfo)
2374 struct soc_mixer_control *mc =
2375 (struct soc_mixer_control *)kcontrol->private_value;
2378 if (!mc->platform_max)
2379 mc->platform_max = mc->max;
2380 platform_max = mc->platform_max;
2382 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2383 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2385 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2388 uinfo->value.integer.min = 0;
2389 uinfo->value.integer.max = platform_max;
2392 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2395 * snd_soc_get_volsw_2r - double mixer get callback
2396 * @kcontrol: mixer control
2397 * @ucontrol: control element information
2399 * Callback to get the value of a double mixer control that spans 2 registers.
2401 * Returns 0 for success.
2403 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2404 struct snd_ctl_elem_value *ucontrol)
2406 struct soc_mixer_control *mc =
2407 (struct soc_mixer_control *)kcontrol->private_value;
2408 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2409 unsigned int reg = mc->reg;
2410 unsigned int reg2 = mc->rreg;
2411 unsigned int shift = mc->shift;
2413 unsigned int mask = (1 << fls(max)) - 1;
2414 unsigned int invert = mc->invert;
2416 ucontrol->value.integer.value[0] =
2417 (snd_soc_read(codec, reg) >> shift) & mask;
2418 ucontrol->value.integer.value[1] =
2419 (snd_soc_read(codec, reg2) >> shift) & mask;
2421 ucontrol->value.integer.value[0] =
2422 max - ucontrol->value.integer.value[0];
2423 ucontrol->value.integer.value[1] =
2424 max - ucontrol->value.integer.value[1];
2429 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2432 * snd_soc_put_volsw_2r - double mixer set callback
2433 * @kcontrol: mixer control
2434 * @ucontrol: control element information
2436 * Callback to set the value of a double mixer control that spans 2 registers.
2438 * Returns 0 for success.
2440 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2441 struct snd_ctl_elem_value *ucontrol)
2443 struct soc_mixer_control *mc =
2444 (struct soc_mixer_control *)kcontrol->private_value;
2445 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2446 unsigned int reg = mc->reg;
2447 unsigned int reg2 = mc->rreg;
2448 unsigned int shift = mc->shift;
2450 unsigned int mask = (1 << fls(max)) - 1;
2451 unsigned int invert = mc->invert;
2453 unsigned int val, val2, val_mask;
2455 val_mask = mask << shift;
2456 val = (ucontrol->value.integer.value[0] & mask);
2457 val2 = (ucontrol->value.integer.value[1] & mask);
2465 val2 = val2 << shift;
2467 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2471 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2474 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2477 * snd_soc_info_volsw_s8 - signed mixer info callback
2478 * @kcontrol: mixer control
2479 * @uinfo: control element information
2481 * Callback to provide information about a signed mixer control.
2483 * Returns 0 for success.
2485 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2486 struct snd_ctl_elem_info *uinfo)
2488 struct soc_mixer_control *mc =
2489 (struct soc_mixer_control *)kcontrol->private_value;
2493 if (!mc->platform_max)
2494 mc->platform_max = mc->max;
2495 platform_max = mc->platform_max;
2497 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2499 uinfo->value.integer.min = 0;
2500 uinfo->value.integer.max = platform_max - min;
2503 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2506 * snd_soc_get_volsw_s8 - signed mixer get callback
2507 * @kcontrol: mixer control
2508 * @ucontrol: control element information
2510 * Callback to get the value of a signed mixer control.
2512 * Returns 0 for success.
2514 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2515 struct snd_ctl_elem_value *ucontrol)
2517 struct soc_mixer_control *mc =
2518 (struct soc_mixer_control *)kcontrol->private_value;
2519 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2520 unsigned int reg = mc->reg;
2522 int val = snd_soc_read(codec, reg);
2524 ucontrol->value.integer.value[0] =
2525 ((signed char)(val & 0xff))-min;
2526 ucontrol->value.integer.value[1] =
2527 ((signed char)((val >> 8) & 0xff))-min;
2530 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2533 * snd_soc_put_volsw_sgn - signed mixer put callback
2534 * @kcontrol: mixer control
2535 * @ucontrol: control element information
2537 * Callback to set the value of a signed mixer control.
2539 * Returns 0 for success.
2541 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2542 struct snd_ctl_elem_value *ucontrol)
2544 struct soc_mixer_control *mc =
2545 (struct soc_mixer_control *)kcontrol->private_value;
2546 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2547 unsigned int reg = mc->reg;
2551 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2552 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2554 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2556 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2559 * snd_soc_limit_volume - Set new limit to an existing volume control.
2561 * @codec: where to look for the control
2562 * @name: Name of the control
2563 * @max: new maximum limit
2565 * Return 0 for success, else error.
2567 int snd_soc_limit_volume(struct snd_soc_codec *codec,
2568 const char *name, int max)
2570 struct snd_card *card = codec->card->snd_card;
2571 struct snd_kcontrol *kctl;
2572 struct soc_mixer_control *mc;
2576 /* Sanity check for name and max */
2577 if (unlikely(!name || max <= 0))
2580 list_for_each_entry(kctl, &card->controls, list) {
2581 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2587 mc = (struct soc_mixer_control *)kctl->private_value;
2588 if (max <= mc->max) {
2589 mc->platform_max = max;
2595 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2598 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2599 * mixer info callback
2600 * @kcontrol: mixer control
2601 * @uinfo: control element information
2603 * Returns 0 for success.
2605 int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2606 struct snd_ctl_elem_info *uinfo)
2608 struct soc_mixer_control *mc =
2609 (struct soc_mixer_control *)kcontrol->private_value;
2613 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2615 uinfo->value.integer.min = 0;
2616 uinfo->value.integer.max = max-min;
2620 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2623 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2624 * mixer get callback
2625 * @kcontrol: mixer control
2626 * @uinfo: control element information
2628 * Returns 0 for success.
2630 int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2631 struct snd_ctl_elem_value *ucontrol)
2633 struct soc_mixer_control *mc =
2634 (struct soc_mixer_control *)kcontrol->private_value;
2635 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2636 unsigned int mask = (1<<mc->shift)-1;
2638 int val = snd_soc_read(codec, mc->reg) & mask;
2639 int valr = snd_soc_read(codec, mc->rreg) & mask;
2641 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2642 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
2645 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2648 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2649 * mixer put callback
2650 * @kcontrol: mixer control
2651 * @uinfo: control element information
2653 * Returns 0 for success.
2655 int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2656 struct snd_ctl_elem_value *ucontrol)
2658 struct soc_mixer_control *mc =
2659 (struct soc_mixer_control *)kcontrol->private_value;
2660 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2661 unsigned int mask = (1<<mc->shift)-1;
2664 unsigned int val, valr, oval, ovalr;
2666 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2668 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2671 oval = snd_soc_read(codec, mc->reg) & mask;
2672 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2676 ret = snd_soc_write(codec, mc->reg, val);
2680 if (ovalr != valr) {
2681 ret = snd_soc_write(codec, mc->rreg, valr);
2688 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2691 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2693 * @clk_id: DAI specific clock ID
2694 * @freq: new clock frequency in Hz
2695 * @dir: new clock direction - input/output.
2697 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2699 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2700 unsigned int freq, int dir)
2702 if (dai->driver && dai->driver->ops->set_sysclk)
2703 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2707 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2710 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2712 * @div_id: DAI specific clock divider ID
2713 * @div: new clock divisor.
2715 * Configures the clock dividers. This is used to derive the best DAI bit and
2716 * frame clocks from the system or master clock. It's best to set the DAI bit
2717 * and frame clocks as low as possible to save system power.
2719 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2720 int div_id, int div)
2722 if (dai->driver && dai->driver->ops->set_clkdiv)
2723 return dai->driver->ops->set_clkdiv(dai, div_id, div);
2727 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2730 * snd_soc_dai_set_pll - configure DAI PLL.
2732 * @pll_id: DAI specific PLL ID
2733 * @source: DAI specific source for the PLL
2734 * @freq_in: PLL input clock frequency in Hz
2735 * @freq_out: requested PLL output clock frequency in Hz
2737 * Configures and enables PLL to generate output clock based on input clock.
2739 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2740 unsigned int freq_in, unsigned int freq_out)
2742 if (dai->driver && dai->driver->ops->set_pll)
2743 return dai->driver->ops->set_pll(dai, pll_id, source,
2748 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2751 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2753 * @fmt: SND_SOC_DAIFMT_ format value.
2755 * Configures the DAI hardware format and clocking.
2757 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2759 if (dai->driver && dai->driver->ops->set_fmt)
2760 return dai->driver->ops->set_fmt(dai, fmt);
2764 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2767 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2769 * @tx_mask: bitmask representing active TX slots.
2770 * @rx_mask: bitmask representing active RX slots.
2771 * @slots: Number of slots in use.
2772 * @slot_width: Width in bits for each slot.
2774 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2777 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2778 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2780 if (dai->driver && dai->driver->ops->set_tdm_slot)
2781 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2786 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2789 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2791 * @tx_num: how many TX channels
2792 * @tx_slot: pointer to an array which imply the TX slot number channel
2794 * @rx_num: how many RX channels
2795 * @rx_slot: pointer to an array which imply the RX slot number channel
2798 * configure the relationship between channel number and TDM slot number.
2800 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2801 unsigned int tx_num, unsigned int *tx_slot,
2802 unsigned int rx_num, unsigned int *rx_slot)
2804 if (dai->driver && dai->driver->ops->set_channel_map)
2805 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2810 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2813 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2815 * @tristate: tristate enable
2817 * Tristates the DAI so that others can use it.
2819 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2821 if (dai->driver && dai->driver->ops->set_tristate)
2822 return dai->driver->ops->set_tristate(dai, tristate);
2826 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2829 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2831 * @mute: mute enable
2833 * Mutes the DAI DAC.
2835 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2837 if (dai->driver && dai->driver->ops->digital_mute)
2838 return dai->driver->ops->digital_mute(dai, mute);
2842 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2845 * snd_soc_register_card - Register a card with the ASoC core
2847 * @card: Card to register
2849 * Note that currently this is an internal only function: it will be
2850 * exposed to machine drivers after further backporting of ASoC v2
2851 * registration APIs.
2853 static int snd_soc_register_card(struct snd_soc_card *card)
2857 if (!card->name || !card->dev)
2860 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) * card->num_links,
2862 if (card->rtd == NULL)
2865 for (i = 0; i < card->num_links; i++)
2866 card->rtd[i].dai_link = &card->dai_link[i];
2868 INIT_LIST_HEAD(&card->list);
2869 card->instantiated = 0;
2870 mutex_init(&card->mutex);
2872 mutex_lock(&client_mutex);
2873 list_add(&card->list, &card_list);
2874 snd_soc_instantiate_cards();
2875 mutex_unlock(&client_mutex);
2877 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2883 * snd_soc_unregister_card - Unregister a card with the ASoC core
2885 * @card: Card to unregister
2887 * Note that currently this is an internal only function: it will be
2888 * exposed to machine drivers after further backporting of ASoC v2
2889 * registration APIs.
2891 static int snd_soc_unregister_card(struct snd_soc_card *card)
2893 mutex_lock(&client_mutex);
2894 list_del(&card->list);
2895 mutex_unlock(&client_mutex);
2896 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2902 * Simplify DAI link configuration by removing ".-1" from device names
2903 * and sanitizing names.
2905 static inline char *fmt_single_name(struct device *dev, int *id)
2907 char *found, name[NAME_SIZE];
2910 if (dev_name(dev) == NULL)
2913 strncpy(name, dev_name(dev), NAME_SIZE);
2915 /* are we a "%s.%d" name (platform and SPI components) */
2916 found = strstr(name, dev->driver->name);
2919 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2921 /* discard ID from name if ID == -1 */
2923 found[strlen(dev->driver->name)] = '\0';
2927 /* I2C component devices are named "bus-addr" */
2928 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2929 char tmp[NAME_SIZE];
2931 /* create unique ID number from I2C addr and bus */
2932 *id = ((id1 & 0xffff) << 16) + id2;
2934 /* sanitize component name for DAI link creation */
2935 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2936 strncpy(name, tmp, NAME_SIZE);
2941 return kstrdup(name, GFP_KERNEL);
2945 * Simplify DAI link naming for single devices with multiple DAIs by removing
2946 * any ".-1" and using the DAI name (instead of device name).
2948 static inline char *fmt_multiple_name(struct device *dev,
2949 struct snd_soc_dai_driver *dai_drv)
2951 if (dai_drv->name == NULL) {
2952 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
2957 return kstrdup(dai_drv->name, GFP_KERNEL);
2961 * snd_soc_register_dai - Register a DAI with the ASoC core
2963 * @dai: DAI to register
2965 int snd_soc_register_dai(struct device *dev,
2966 struct snd_soc_dai_driver *dai_drv)
2968 struct snd_soc_dai *dai;
2970 dev_dbg(dev, "dai register %s\n", dev_name(dev));
2972 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2976 /* create DAI component name */
2977 dai->name = fmt_single_name(dev, &dai->id);
2978 if (dai->name == NULL) {
2984 dai->driver = dai_drv;
2985 if (!dai->driver->ops)
2986 dai->driver->ops = &null_dai_ops;
2988 mutex_lock(&client_mutex);
2989 list_add(&dai->list, &dai_list);
2990 snd_soc_instantiate_cards();
2991 mutex_unlock(&client_mutex);
2993 pr_debug("Registered DAI '%s'\n", dai->name);
2997 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3000 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3002 * @dai: DAI to unregister
3004 void snd_soc_unregister_dai(struct device *dev)
3006 struct snd_soc_dai *dai;
3008 list_for_each_entry(dai, &dai_list, list) {
3009 if (dev == dai->dev)
3015 mutex_lock(&client_mutex);
3016 list_del(&dai->list);
3017 mutex_unlock(&client_mutex);
3019 pr_debug("Unregistered DAI '%s'\n", dai->name);
3023 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3026 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3028 * @dai: Array of DAIs to register
3029 * @count: Number of DAIs
3031 int snd_soc_register_dais(struct device *dev,
3032 struct snd_soc_dai_driver *dai_drv, size_t count)
3034 struct snd_soc_dai *dai;
3037 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
3039 for (i = 0; i < count; i++) {
3041 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3045 /* create DAI component name */
3046 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3047 if (dai->name == NULL) {
3054 dai->driver = &dai_drv[i];
3055 if (dai->driver->id)
3056 dai->id = dai->driver->id;
3059 if (!dai->driver->ops)
3060 dai->driver->ops = &null_dai_ops;
3062 mutex_lock(&client_mutex);
3063 list_add(&dai->list, &dai_list);
3064 mutex_unlock(&client_mutex);
3066 pr_debug("Registered DAI '%s'\n", dai->name);
3069 snd_soc_instantiate_cards();
3073 for (i--; i >= 0; i--)
3074 snd_soc_unregister_dai(dev);
3078 EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3081 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3083 * @dai: Array of DAIs to unregister
3084 * @count: Number of DAIs
3086 void snd_soc_unregister_dais(struct device *dev, size_t count)
3090 for (i = 0; i < count; i++)
3091 snd_soc_unregister_dai(dev);
3093 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3096 * snd_soc_register_platform - Register a platform with the ASoC core
3098 * @platform: platform to register
3100 int snd_soc_register_platform(struct device *dev,
3101 struct snd_soc_platform_driver *platform_drv)
3103 struct snd_soc_platform *platform;
3105 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3107 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3108 if (platform == NULL)
3111 /* create platform component name */
3112 platform->name = fmt_single_name(dev, &platform->id);
3113 if (platform->name == NULL) {
3118 platform->dev = dev;
3119 platform->driver = platform_drv;
3121 mutex_lock(&client_mutex);
3122 list_add(&platform->list, &platform_list);
3123 snd_soc_instantiate_cards();
3124 mutex_unlock(&client_mutex);
3126 pr_debug("Registered platform '%s'\n", platform->name);
3130 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3133 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3135 * @platform: platform to unregister
3137 void snd_soc_unregister_platform(struct device *dev)
3139 struct snd_soc_platform *platform;
3141 list_for_each_entry(platform, &platform_list, list) {
3142 if (dev == platform->dev)
3148 mutex_lock(&client_mutex);
3149 list_del(&platform->list);
3150 mutex_unlock(&client_mutex);
3152 pr_debug("Unregistered platform '%s'\n", platform->name);
3153 kfree(platform->name);
3156 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3158 static u64 codec_format_map[] = {
3159 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3160 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3161 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3162 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3163 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3164 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3165 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3166 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3167 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3168 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3169 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3170 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3171 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3172 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3173 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3174 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3177 /* Fix up the DAI formats for endianness: codecs don't actually see
3178 * the endianness of the data but we're using the CPU format
3179 * definitions which do need to include endianness so we ensure that
3180 * codec DAIs always have both big and little endian variants set.
3182 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3186 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3187 if (stream->formats & codec_format_map[i])
3188 stream->formats |= codec_format_map[i];
3192 * snd_soc_register_codec - Register a codec with the ASoC core
3194 * @codec: codec to register
3196 int snd_soc_register_codec(struct device *dev,
3197 struct snd_soc_codec_driver *codec_drv,
3198 struct snd_soc_dai_driver *dai_drv, int num_dai)
3200 struct snd_soc_codec *codec;
3203 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3205 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3209 /* create CODEC component name */
3210 codec->name = fmt_single_name(dev, &codec->id);
3211 if (codec->name == NULL) {
3216 /* allocate CODEC register cache */
3217 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3219 if (codec_drv->reg_cache_default)
3220 codec->reg_cache = kmemdup(codec_drv->reg_cache_default,
3221 codec_drv->reg_cache_size * codec_drv->reg_word_size, GFP_KERNEL);
3223 codec->reg_cache = kzalloc(codec_drv->reg_cache_size *
3224 codec_drv->reg_word_size, GFP_KERNEL);
3226 if (codec->reg_cache == NULL) {
3234 codec->driver = codec_drv;
3235 codec->bias_level = SND_SOC_BIAS_OFF;
3236 codec->num_dai = num_dai;
3237 mutex_init(&codec->mutex);
3238 INIT_LIST_HEAD(&codec->dapm_widgets);
3239 INIT_LIST_HEAD(&codec->dapm_paths);
3241 for (i = 0; i < num_dai; i++) {
3242 fixup_codec_formats(&dai_drv[i].playback);
3243 fixup_codec_formats(&dai_drv[i].capture);
3246 /* register any DAIs */
3248 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3253 mutex_lock(&client_mutex);
3254 list_add(&codec->list, &codec_list);
3255 snd_soc_instantiate_cards();
3256 mutex_unlock(&client_mutex);
3258 pr_debug("Registered codec '%s'\n", codec->name);
3262 for (i--; i >= 0; i--)
3263 snd_soc_unregister_dai(dev);
3265 if (codec->reg_cache)
3266 kfree(codec->reg_cache);
3271 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3274 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3276 * @codec: codec to unregister
3278 void snd_soc_unregister_codec(struct device *dev)
3280 struct snd_soc_codec *codec;
3283 list_for_each_entry(codec, &codec_list, list) {
3284 if (dev == codec->dev)
3291 for (i = 0; i < codec->num_dai; i++)
3292 snd_soc_unregister_dai(dev);
3294 mutex_lock(&client_mutex);
3295 list_del(&codec->list);
3296 mutex_unlock(&client_mutex);
3298 pr_debug("Unregistered codec '%s'\n", codec->name);
3300 if (codec->reg_cache)
3301 kfree(codec->reg_cache);
3304 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3306 static int __init snd_soc_init(void)
3308 #ifdef CONFIG_DEBUG_FS
3309 debugfs_root = debugfs_create_dir("asoc", NULL);
3310 if (IS_ERR(debugfs_root) || !debugfs_root) {
3312 "ASoC: Failed to create debugfs directory\n");
3313 debugfs_root = NULL;
3316 if (!debugfs_create_file("codecs", 0444, debugfs_root, NULL,
3318 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3320 if (!debugfs_create_file("dais", 0444, debugfs_root, NULL,
3322 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
3324 if (!debugfs_create_file("platforms", 0444, debugfs_root, NULL,
3325 &platform_list_fops))
3326 pr_warn("ASoC: Failed to create platform list debugfs file\n");
3329 return platform_driver_register(&soc_driver);
3331 module_init(snd_soc_init);
3333 static void __exit snd_soc_exit(void)
3335 #ifdef CONFIG_DEBUG_FS
3336 debugfs_remove_recursive(debugfs_root);
3338 platform_driver_unregister(&soc_driver);
3340 module_exit(snd_soc_exit);
3342 /* Module information */
3343 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3344 MODULE_DESCRIPTION("ALSA SoC Core");
3345 MODULE_LICENSE("GPL");
3346 MODULE_ALIAS("platform:soc-audio");