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);
170 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
177 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
179 #ifdef CONFIG_DEBUG_FS
180 static int codec_reg_open_file(struct inode *inode, struct file *file)
182 file->private_data = inode->i_private;
186 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
187 size_t count, loff_t *ppos)
190 struct snd_soc_codec *codec = file->private_data;
191 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
194 ret = soc_codec_reg_show(codec, buf);
196 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
201 static ssize_t codec_reg_write_file(struct file *file,
202 const char __user *user_buf, size_t count, loff_t *ppos)
207 unsigned long reg, value;
209 struct snd_soc_codec *codec = file->private_data;
211 buf_size = min(count, (sizeof(buf)-1));
212 if (copy_from_user(buf, user_buf, buf_size))
216 if (codec->driver->reg_cache_step)
217 step = codec->driver->reg_cache_step;
219 while (*start == ' ')
221 reg = simple_strtoul(start, &start, 16);
222 if ((reg >= codec->driver->reg_cache_size) || (reg % step))
224 while (*start == ' ')
226 if (strict_strtoul(start, 16, &value))
228 codec->driver->write(codec, reg, value);
232 static const struct file_operations codec_reg_fops = {
233 .open = codec_reg_open_file,
234 .read = codec_reg_read_file,
235 .write = codec_reg_write_file,
236 .llseek = default_llseek,
239 static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
241 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
243 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
245 if (!codec->debugfs_codec_root) {
247 "ASoC: Failed to create codec debugfs directory\n");
251 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
252 codec->debugfs_codec_root,
253 codec, &codec_reg_fops);
254 if (!codec->debugfs_reg)
256 "ASoC: Failed to create codec register debugfs file\n");
258 codec->dapm.debugfs_dapm = debugfs_create_dir("dapm",
259 codec->debugfs_codec_root);
260 if (!codec->dapm.debugfs_dapm)
262 "Failed to create DAPM debugfs directory\n");
264 snd_soc_dapm_debugfs_init(&codec->dapm);
267 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
269 debugfs_remove_recursive(codec->debugfs_codec_root);
272 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
273 size_t count, loff_t *ppos)
275 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
276 ssize_t len, ret = 0;
277 struct snd_soc_codec *codec;
282 list_for_each_entry(codec, &codec_list, list) {
283 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
287 if (ret > PAGE_SIZE) {
294 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
301 static const struct file_operations codec_list_fops = {
302 .read = codec_list_read_file,
303 .llseek = default_llseek,/* read accesses f_pos */
306 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
307 size_t count, loff_t *ppos)
309 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
310 ssize_t len, ret = 0;
311 struct snd_soc_dai *dai;
316 list_for_each_entry(dai, &dai_list, list) {
317 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
320 if (ret > PAGE_SIZE) {
326 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
333 static const struct file_operations dai_list_fops = {
334 .read = dai_list_read_file,
335 .llseek = default_llseek,/* read accesses f_pos */
338 static ssize_t platform_list_read_file(struct file *file,
339 char __user *user_buf,
340 size_t count, loff_t *ppos)
342 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
343 ssize_t len, ret = 0;
344 struct snd_soc_platform *platform;
349 list_for_each_entry(platform, &platform_list, list) {
350 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
354 if (ret > PAGE_SIZE) {
360 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
367 static const struct file_operations platform_list_fops = {
368 .read = platform_list_read_file,
369 .llseek = default_llseek,/* read accesses f_pos */
372 static void soc_init_card_debugfs(struct snd_soc_card *card)
374 card->debugfs_card_root = debugfs_create_dir(card->name,
376 if (!card->debugfs_card_root) {
378 "ASoC: Failed to create codec debugfs directory\n");
382 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
383 card->debugfs_card_root,
385 if (!card->debugfs_pop_time)
387 "Failed to create pop time debugfs file\n");
390 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
392 debugfs_remove_recursive(card->debugfs_card_root);
397 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
401 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
405 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
409 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
414 #ifdef CONFIG_SND_SOC_AC97_BUS
415 /* unregister ac97 codec */
416 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
418 if (codec->ac97->dev.bus)
419 device_unregister(&codec->ac97->dev);
423 /* stop no dev release warning */
424 static void soc_ac97_device_release(struct device *dev){}
426 /* register ac97 codec to bus */
427 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
431 codec->ac97->dev.bus = &ac97_bus_type;
432 codec->ac97->dev.parent = codec->card->dev;
433 codec->ac97->dev.release = soc_ac97_device_release;
435 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
436 codec->card->snd_card->number, 0, codec->name);
437 err = device_register(&codec->ac97->dev);
439 snd_printk(KERN_ERR "Can't register ac97 bus\n");
440 codec->ac97->dev.bus = NULL;
447 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
449 struct snd_soc_pcm_runtime *rtd = substream->private_data;
450 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
451 struct snd_soc_dai *codec_dai = rtd->codec_dai;
454 if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
455 rtd->dai_link->symmetric_rates) {
456 dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
459 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
460 SNDRV_PCM_HW_PARAM_RATE,
465 "Unable to apply rate symmetry constraint: %d\n", ret);
474 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
475 * then initialized and any private data can be allocated. This also calls
476 * startup for the cpu DAI, platform, machine and codec DAI.
478 static int soc_pcm_open(struct snd_pcm_substream *substream)
480 struct snd_soc_pcm_runtime *rtd = substream->private_data;
481 struct snd_pcm_runtime *runtime = substream->runtime;
482 struct snd_soc_platform *platform = rtd->platform;
483 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
484 struct snd_soc_dai *codec_dai = rtd->codec_dai;
485 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
486 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
489 mutex_lock(&pcm_mutex);
491 /* startup the audio subsystem */
492 if (cpu_dai->driver->ops->startup) {
493 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
495 printk(KERN_ERR "asoc: can't open interface %s\n",
501 if (platform->driver->ops->open) {
502 ret = platform->driver->ops->open(substream);
504 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
509 if (codec_dai->driver->ops->startup) {
510 ret = codec_dai->driver->ops->startup(substream, codec_dai);
512 printk(KERN_ERR "asoc: can't open codec %s\n",
518 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
519 ret = rtd->dai_link->ops->startup(substream);
521 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
526 /* Check that the codec and cpu DAI's are compatible */
527 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
528 runtime->hw.rate_min =
529 max(codec_dai_drv->playback.rate_min,
530 cpu_dai_drv->playback.rate_min);
531 runtime->hw.rate_max =
532 min(codec_dai_drv->playback.rate_max,
533 cpu_dai_drv->playback.rate_max);
534 runtime->hw.channels_min =
535 max(codec_dai_drv->playback.channels_min,
536 cpu_dai_drv->playback.channels_min);
537 runtime->hw.channels_max =
538 min(codec_dai_drv->playback.channels_max,
539 cpu_dai_drv->playback.channels_max);
540 runtime->hw.formats =
541 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
543 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
544 if (codec_dai_drv->playback.rates
545 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
546 runtime->hw.rates |= cpu_dai_drv->playback.rates;
547 if (cpu_dai_drv->playback.rates
548 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
549 runtime->hw.rates |= codec_dai_drv->playback.rates;
551 runtime->hw.rate_min =
552 max(codec_dai_drv->capture.rate_min,
553 cpu_dai_drv->capture.rate_min);
554 runtime->hw.rate_max =
555 min(codec_dai_drv->capture.rate_max,
556 cpu_dai_drv->capture.rate_max);
557 runtime->hw.channels_min =
558 max(codec_dai_drv->capture.channels_min,
559 cpu_dai_drv->capture.channels_min);
560 runtime->hw.channels_max =
561 min(codec_dai_drv->capture.channels_max,
562 cpu_dai_drv->capture.channels_max);
563 runtime->hw.formats =
564 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
566 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
567 if (codec_dai_drv->capture.rates
568 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
569 runtime->hw.rates |= cpu_dai_drv->capture.rates;
570 if (cpu_dai_drv->capture.rates
571 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
572 runtime->hw.rates |= codec_dai_drv->capture.rates;
575 snd_pcm_limit_hw_rates(runtime);
576 if (!runtime->hw.rates) {
577 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
578 codec_dai->name, cpu_dai->name);
581 if (!runtime->hw.formats) {
582 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
583 codec_dai->name, cpu_dai->name);
586 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
587 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
588 codec_dai->name, cpu_dai->name);
592 /* Symmetry only applies if we've already got an active stream. */
593 if (cpu_dai->active || codec_dai->active) {
594 ret = soc_pcm_apply_symmetry(substream);
599 pr_debug("asoc: %s <-> %s info:\n",
600 codec_dai->name, cpu_dai->name);
601 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
602 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
603 runtime->hw.channels_max);
604 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
605 runtime->hw.rate_max);
607 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
608 cpu_dai->playback_active++;
609 codec_dai->playback_active++;
611 cpu_dai->capture_active++;
612 codec_dai->capture_active++;
616 rtd->codec->active++;
617 mutex_unlock(&pcm_mutex);
621 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
622 rtd->dai_link->ops->shutdown(substream);
625 if (codec_dai->driver->ops->shutdown)
626 codec_dai->driver->ops->shutdown(substream, codec_dai);
629 if (platform->driver->ops->close)
630 platform->driver->ops->close(substream);
633 if (cpu_dai->driver->ops->shutdown)
634 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
636 mutex_unlock(&pcm_mutex);
641 * Power down the audio subsystem pmdown_time msecs after close is called.
642 * This is to ensure there are no pops or clicks in between any music tracks
643 * due to DAPM power cycling.
645 static void close_delayed_work(struct work_struct *work)
647 struct snd_soc_pcm_runtime *rtd =
648 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
649 struct snd_soc_dai *codec_dai = rtd->codec_dai;
651 mutex_lock(&pcm_mutex);
653 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
654 codec_dai->driver->playback.stream_name,
655 codec_dai->playback_active ? "active" : "inactive",
656 codec_dai->pop_wait ? "yes" : "no");
658 /* are we waiting on this codec DAI stream */
659 if (codec_dai->pop_wait == 1) {
660 codec_dai->pop_wait = 0;
661 snd_soc_dapm_stream_event(rtd,
662 codec_dai->driver->playback.stream_name,
663 SND_SOC_DAPM_STREAM_STOP);
666 mutex_unlock(&pcm_mutex);
670 * Called by ALSA when a PCM substream is closed. Private data can be
671 * freed here. The cpu DAI, codec DAI, machine and platform are also
674 static int soc_codec_close(struct snd_pcm_substream *substream)
676 struct snd_soc_pcm_runtime *rtd = substream->private_data;
677 struct snd_soc_platform *platform = rtd->platform;
678 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
679 struct snd_soc_dai *codec_dai = rtd->codec_dai;
680 struct snd_soc_codec *codec = rtd->codec;
682 mutex_lock(&pcm_mutex);
684 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
685 cpu_dai->playback_active--;
686 codec_dai->playback_active--;
688 cpu_dai->capture_active--;
689 codec_dai->capture_active--;
696 /* Muting the DAC suppresses artifacts caused during digital
697 * shutdown, for example from stopping clocks.
699 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
700 snd_soc_dai_digital_mute(codec_dai, 1);
702 if (cpu_dai->driver->ops->shutdown)
703 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
705 if (codec_dai->driver->ops->shutdown)
706 codec_dai->driver->ops->shutdown(substream, codec_dai);
708 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
709 rtd->dai_link->ops->shutdown(substream);
711 if (platform->driver->ops->close)
712 platform->driver->ops->close(substream);
713 cpu_dai->runtime = NULL;
715 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
716 /* start delayed pop wq here for playback streams */
717 codec_dai->pop_wait = 1;
718 schedule_delayed_work(&rtd->delayed_work,
719 msecs_to_jiffies(rtd->pmdown_time));
721 /* capture streams can be powered down now */
722 snd_soc_dapm_stream_event(rtd,
723 codec_dai->driver->capture.stream_name,
724 SND_SOC_DAPM_STREAM_STOP);
727 mutex_unlock(&pcm_mutex);
732 * Called by ALSA when the PCM substream is prepared, can set format, sample
733 * rate, etc. This function is non atomic and can be called multiple times,
734 * it can refer to the runtime info.
736 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
738 struct snd_soc_pcm_runtime *rtd = substream->private_data;
739 struct snd_soc_platform *platform = rtd->platform;
740 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
741 struct snd_soc_dai *codec_dai = rtd->codec_dai;
744 mutex_lock(&pcm_mutex);
746 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
747 ret = rtd->dai_link->ops->prepare(substream);
749 printk(KERN_ERR "asoc: machine prepare error\n");
754 if (platform->driver->ops->prepare) {
755 ret = platform->driver->ops->prepare(substream);
757 printk(KERN_ERR "asoc: platform prepare error\n");
762 if (codec_dai->driver->ops->prepare) {
763 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
765 printk(KERN_ERR "asoc: codec DAI prepare error\n");
770 if (cpu_dai->driver->ops->prepare) {
771 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
773 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
778 /* cancel any delayed stream shutdown that is pending */
779 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
780 codec_dai->pop_wait) {
781 codec_dai->pop_wait = 0;
782 cancel_delayed_work(&rtd->delayed_work);
785 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
786 snd_soc_dapm_stream_event(rtd,
787 codec_dai->driver->playback.stream_name,
788 SND_SOC_DAPM_STREAM_START);
790 snd_soc_dapm_stream_event(rtd,
791 codec_dai->driver->capture.stream_name,
792 SND_SOC_DAPM_STREAM_START);
794 snd_soc_dai_digital_mute(codec_dai, 0);
797 mutex_unlock(&pcm_mutex);
802 * Called by ALSA when the hardware params are set by application. This
803 * function can also be called multiple times and can allocate buffers
804 * (using snd_pcm_lib_* ). It's non-atomic.
806 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
807 struct snd_pcm_hw_params *params)
809 struct snd_soc_pcm_runtime *rtd = substream->private_data;
810 struct snd_soc_platform *platform = rtd->platform;
811 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
812 struct snd_soc_dai *codec_dai = rtd->codec_dai;
815 mutex_lock(&pcm_mutex);
817 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
818 ret = rtd->dai_link->ops->hw_params(substream, params);
820 printk(KERN_ERR "asoc: machine hw_params failed\n");
825 if (codec_dai->driver->ops->hw_params) {
826 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
828 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
834 if (cpu_dai->driver->ops->hw_params) {
835 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
837 printk(KERN_ERR "asoc: interface %s hw params failed\n",
843 if (platform->driver->ops->hw_params) {
844 ret = platform->driver->ops->hw_params(substream, params);
846 printk(KERN_ERR "asoc: platform %s hw params failed\n",
852 rtd->rate = params_rate(params);
855 mutex_unlock(&pcm_mutex);
859 if (cpu_dai->driver->ops->hw_free)
860 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
863 if (codec_dai->driver->ops->hw_free)
864 codec_dai->driver->ops->hw_free(substream, codec_dai);
867 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
868 rtd->dai_link->ops->hw_free(substream);
870 mutex_unlock(&pcm_mutex);
875 * Free's resources allocated by hw_params, can be called multiple times
877 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
879 struct snd_soc_pcm_runtime *rtd = substream->private_data;
880 struct snd_soc_platform *platform = rtd->platform;
881 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
882 struct snd_soc_dai *codec_dai = rtd->codec_dai;
883 struct snd_soc_codec *codec = rtd->codec;
885 mutex_lock(&pcm_mutex);
887 /* apply codec digital mute */
889 snd_soc_dai_digital_mute(codec_dai, 1);
891 /* free any machine hw params */
892 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
893 rtd->dai_link->ops->hw_free(substream);
895 /* free any DMA resources */
896 if (platform->driver->ops->hw_free)
897 platform->driver->ops->hw_free(substream);
899 /* now free hw params for the DAI's */
900 if (codec_dai->driver->ops->hw_free)
901 codec_dai->driver->ops->hw_free(substream, codec_dai);
903 if (cpu_dai->driver->ops->hw_free)
904 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
906 mutex_unlock(&pcm_mutex);
910 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
912 struct snd_soc_pcm_runtime *rtd = substream->private_data;
913 struct snd_soc_platform *platform = rtd->platform;
914 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
915 struct snd_soc_dai *codec_dai = rtd->codec_dai;
918 if (codec_dai->driver->ops->trigger) {
919 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
924 if (platform->driver->ops->trigger) {
925 ret = platform->driver->ops->trigger(substream, cmd);
930 if (cpu_dai->driver->ops->trigger) {
931 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
939 * soc level wrapper for pointer callback
940 * If cpu_dai, codec_dai, platform driver has the delay callback, than
941 * the runtime->delay will be updated accordingly.
943 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
945 struct snd_soc_pcm_runtime *rtd = substream->private_data;
946 struct snd_soc_platform *platform = rtd->platform;
947 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
948 struct snd_soc_dai *codec_dai = rtd->codec_dai;
949 struct snd_pcm_runtime *runtime = substream->runtime;
950 snd_pcm_uframes_t offset = 0;
951 snd_pcm_sframes_t delay = 0;
953 if (platform->driver->ops->pointer)
954 offset = platform->driver->ops->pointer(substream);
956 if (cpu_dai->driver->ops->delay)
957 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
959 if (codec_dai->driver->ops->delay)
960 delay += codec_dai->driver->ops->delay(substream, codec_dai);
962 if (platform->driver->delay)
963 delay += platform->driver->delay(substream, codec_dai);
965 runtime->delay = delay;
970 /* ASoC PCM operations */
971 static struct snd_pcm_ops soc_pcm_ops = {
972 .open = soc_pcm_open,
973 .close = soc_codec_close,
974 .hw_params = soc_pcm_hw_params,
975 .hw_free = soc_pcm_hw_free,
976 .prepare = soc_pcm_prepare,
977 .trigger = soc_pcm_trigger,
978 .pointer = soc_pcm_pointer,
982 /* powers down audio subsystem for suspend */
983 static int soc_suspend(struct device *dev)
985 struct platform_device *pdev = to_platform_device(dev);
986 struct snd_soc_card *card = platform_get_drvdata(pdev);
989 /* If the initialization of this soc device failed, there is no codec
990 * associated with it. Just bail out in this case.
992 if (list_empty(&card->codec_dev_list))
995 /* Due to the resume being scheduled into a workqueue we could
996 * suspend before that's finished - wait for it to complete.
998 snd_power_lock(card->snd_card);
999 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
1000 snd_power_unlock(card->snd_card);
1002 /* we're going to block userspace touching us until resume completes */
1003 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
1005 /* mute any active DAC's */
1006 for (i = 0; i < card->num_rtd; i++) {
1007 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1008 struct snd_soc_dai_driver *drv = dai->driver;
1010 if (card->rtd[i].dai_link->ignore_suspend)
1013 if (drv->ops->digital_mute && dai->playback_active)
1014 drv->ops->digital_mute(dai, 1);
1017 /* suspend all pcms */
1018 for (i = 0; i < card->num_rtd; i++) {
1019 if (card->rtd[i].dai_link->ignore_suspend)
1022 snd_pcm_suspend_all(card->rtd[i].pcm);
1025 if (card->suspend_pre)
1026 card->suspend_pre(pdev, PMSG_SUSPEND);
1028 for (i = 0; i < card->num_rtd; i++) {
1029 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1030 struct snd_soc_platform *platform = card->rtd[i].platform;
1032 if (card->rtd[i].dai_link->ignore_suspend)
1035 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
1036 cpu_dai->driver->suspend(cpu_dai);
1037 if (platform->driver->suspend && !platform->suspended) {
1038 platform->driver->suspend(cpu_dai);
1039 platform->suspended = 1;
1043 /* close any waiting streams and save state */
1044 for (i = 0; i < card->num_rtd; i++) {
1045 run_delayed_work(&card->rtd[i].delayed_work);
1046 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
1049 for (i = 0; i < card->num_rtd; i++) {
1050 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1052 if (card->rtd[i].dai_link->ignore_suspend)
1055 if (driver->playback.stream_name != NULL)
1056 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1057 SND_SOC_DAPM_STREAM_SUSPEND);
1059 if (driver->capture.stream_name != NULL)
1060 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1061 SND_SOC_DAPM_STREAM_SUSPEND);
1064 /* suspend all CODECs */
1065 for (i = 0; i < card->num_rtd; i++) {
1066 struct snd_soc_codec *codec = card->rtd[i].codec;
1067 /* If there are paths active then the CODEC will be held with
1068 * bias _ON and should not be suspended. */
1069 if (!codec->suspended && codec->driver->suspend) {
1070 switch (codec->dapm.bias_level) {
1071 case SND_SOC_BIAS_STANDBY:
1072 case SND_SOC_BIAS_OFF:
1073 codec->driver->suspend(codec, PMSG_SUSPEND);
1074 codec->suspended = 1;
1077 dev_dbg(codec->dev, "CODEC is on over suspend\n");
1083 for (i = 0; i < card->num_rtd; i++) {
1084 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1086 if (card->rtd[i].dai_link->ignore_suspend)
1089 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1090 cpu_dai->driver->suspend(cpu_dai);
1093 if (card->suspend_post)
1094 card->suspend_post(pdev, PMSG_SUSPEND);
1099 /* deferred resume work, so resume can complete before we finished
1100 * setting our codec back up, which can be very slow on I2C
1102 static void soc_resume_deferred(struct work_struct *work)
1104 struct snd_soc_card *card =
1105 container_of(work, struct snd_soc_card, deferred_resume_work);
1106 struct platform_device *pdev = to_platform_device(card->dev);
1109 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1110 * so userspace apps are blocked from touching us
1113 dev_dbg(card->dev, "starting resume work\n");
1115 /* Bring us up into D2 so that DAPM starts enabling things */
1116 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
1118 if (card->resume_pre)
1119 card->resume_pre(pdev);
1121 /* resume AC97 DAIs */
1122 for (i = 0; i < card->num_rtd; i++) {
1123 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1125 if (card->rtd[i].dai_link->ignore_suspend)
1128 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1129 cpu_dai->driver->resume(cpu_dai);
1132 for (i = 0; i < card->num_rtd; i++) {
1133 struct snd_soc_codec *codec = card->rtd[i].codec;
1134 /* If the CODEC was idle over suspend then it will have been
1135 * left with bias OFF or STANDBY and suspended so we must now
1136 * resume. Otherwise the suspend was suppressed.
1138 if (codec->driver->resume && codec->suspended) {
1139 switch (codec->dapm.bias_level) {
1140 case SND_SOC_BIAS_STANDBY:
1141 case SND_SOC_BIAS_OFF:
1142 codec->driver->resume(codec);
1143 codec->suspended = 0;
1146 dev_dbg(codec->dev, "CODEC was on over suspend\n");
1152 for (i = 0; i < card->num_rtd; i++) {
1153 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1155 if (card->rtd[i].dai_link->ignore_suspend)
1158 if (driver->playback.stream_name != NULL)
1159 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1160 SND_SOC_DAPM_STREAM_RESUME);
1162 if (driver->capture.stream_name != NULL)
1163 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1164 SND_SOC_DAPM_STREAM_RESUME);
1167 /* unmute any active DACs */
1168 for (i = 0; i < card->num_rtd; i++) {
1169 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1170 struct snd_soc_dai_driver *drv = dai->driver;
1172 if (card->rtd[i].dai_link->ignore_suspend)
1175 if (drv->ops->digital_mute && dai->playback_active)
1176 drv->ops->digital_mute(dai, 0);
1179 for (i = 0; i < card->num_rtd; i++) {
1180 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1181 struct snd_soc_platform *platform = card->rtd[i].platform;
1183 if (card->rtd[i].dai_link->ignore_suspend)
1186 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1187 cpu_dai->driver->resume(cpu_dai);
1188 if (platform->driver->resume && platform->suspended) {
1189 platform->driver->resume(cpu_dai);
1190 platform->suspended = 0;
1194 if (card->resume_post)
1195 card->resume_post(pdev);
1197 dev_dbg(card->dev, "resume work completed\n");
1199 /* userspace can access us now we are back as we were before */
1200 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
1203 /* powers up audio subsystem after a suspend */
1204 static int soc_resume(struct device *dev)
1206 struct platform_device *pdev = to_platform_device(dev);
1207 struct snd_soc_card *card = platform_get_drvdata(pdev);
1210 /* AC97 devices might have other drivers hanging off them so
1211 * need to resume immediately. Other drivers don't have that
1212 * problem and may take a substantial amount of time to resume
1213 * due to I/O costs and anti-pop so handle them out of line.
1215 for (i = 0; i < card->num_rtd; i++) {
1216 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1217 if (cpu_dai->driver->ac97_control) {
1218 dev_dbg(dev, "Resuming AC97 immediately\n");
1219 soc_resume_deferred(&card->deferred_resume_work);
1221 dev_dbg(dev, "Scheduling resume work\n");
1222 if (!schedule_work(&card->deferred_resume_work))
1223 dev_err(dev, "resume work item may be lost\n");
1230 #define soc_suspend NULL
1231 #define soc_resume NULL
1234 static struct snd_soc_dai_ops null_dai_ops = {
1237 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
1239 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1240 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1241 struct snd_soc_codec *codec;
1242 struct snd_soc_platform *platform;
1243 struct snd_soc_dai *codec_dai, *cpu_dai;
1247 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
1249 /* do we already have the CPU DAI for this link ? */
1253 /* no, then find CPU DAI from registered DAIs*/
1254 list_for_each_entry(cpu_dai, &dai_list, list) {
1255 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1257 if (!try_module_get(cpu_dai->dev->driver->owner))
1260 rtd->cpu_dai = cpu_dai;
1264 dev_dbg(card->dev, "CPU DAI %s not registered\n",
1265 dai_link->cpu_dai_name);
1268 /* do we already have the CODEC for this link ? */
1273 /* no, then find CODEC from registered CODECs*/
1274 list_for_each_entry(codec, &codec_list, list) {
1275 if (!strcmp(codec->name, dai_link->codec_name)) {
1278 if (!try_module_get(codec->dev->driver->owner))
1281 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1282 list_for_each_entry(codec_dai, &dai_list, list) {
1283 if (codec->dev == codec_dai->dev &&
1284 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1285 rtd->codec_dai = codec_dai;
1289 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1290 dai_link->codec_dai_name);
1295 dev_dbg(card->dev, "CODEC %s not registered\n",
1296 dai_link->codec_name);
1299 /* do we already have the CODEC DAI for this link ? */
1300 if (rtd->platform) {
1303 /* no, then find CPU DAI from registered DAIs*/
1304 list_for_each_entry(platform, &platform_list, list) {
1305 if (!strcmp(platform->name, dai_link->platform_name)) {
1307 if (!try_module_get(platform->dev->driver->owner))
1310 rtd->platform = platform;
1315 dev_dbg(card->dev, "platform %s not registered\n",
1316 dai_link->platform_name);
1320 /* mark rtd as complete if we found all 4 of our client devices */
1321 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1328 static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1330 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1331 struct snd_soc_codec *codec = rtd->codec;
1332 struct snd_soc_platform *platform = rtd->platform;
1333 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1336 /* unregister the rtd device */
1337 if (rtd->dev_registered) {
1338 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
1339 device_unregister(&rtd->dev);
1340 rtd->dev_registered = 0;
1343 /* remove the CODEC DAI */
1344 if (codec_dai && codec_dai->probed) {
1345 if (codec_dai->driver->remove) {
1346 err = codec_dai->driver->remove(codec_dai);
1348 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1350 codec_dai->probed = 0;
1351 list_del(&codec_dai->card_list);
1354 /* remove the platform */
1355 if (platform && platform->probed) {
1356 if (platform->driver->remove) {
1357 err = platform->driver->remove(platform);
1359 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1361 platform->probed = 0;
1362 list_del(&platform->card_list);
1363 module_put(platform->dev->driver->owner);
1366 /* remove the CODEC */
1367 if (codec && codec->probed) {
1368 if (codec->driver->remove) {
1369 err = codec->driver->remove(codec);
1371 printk(KERN_ERR "asoc: failed to remove %s\n", codec->name);
1374 /* Make sure all DAPM widgets are freed */
1375 snd_soc_dapm_free(&codec->dapm);
1377 soc_cleanup_codec_debugfs(codec);
1378 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1380 list_del(&codec->card_list);
1381 module_put(codec->dev->driver->owner);
1384 /* remove the cpu_dai */
1385 if (cpu_dai && cpu_dai->probed) {
1386 if (cpu_dai->driver->remove) {
1387 err = cpu_dai->driver->remove(cpu_dai);
1389 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1391 cpu_dai->probed = 0;
1392 list_del(&cpu_dai->card_list);
1393 module_put(cpu_dai->dev->driver->owner);
1397 static void rtd_release(struct device *dev) {}
1399 static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1401 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1402 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1403 struct snd_soc_codec *codec = rtd->codec;
1404 struct snd_soc_platform *platform = rtd->platform;
1405 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1408 dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1410 /* config components */
1411 codec_dai->codec = codec;
1413 cpu_dai->platform = platform;
1415 rtd->dev.parent = card->dev;
1416 codec_dai->card = card;
1417 cpu_dai->card = card;
1419 /* set default power off timeout */
1420 rtd->pmdown_time = pmdown_time;
1422 /* probe the cpu_dai */
1423 if (!cpu_dai->probed) {
1424 if (cpu_dai->driver->probe) {
1425 ret = cpu_dai->driver->probe(cpu_dai);
1427 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1432 cpu_dai->probed = 1;
1433 /* mark cpu_dai as probed and add to card cpu_dai list */
1434 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1437 /* probe the CODEC */
1438 if (!codec->probed) {
1439 codec->dapm.card = card;
1440 if (codec->driver->probe) {
1441 ret = codec->driver->probe(codec);
1443 printk(KERN_ERR "asoc: failed to probe CODEC %s\n",
1449 soc_init_codec_debugfs(codec);
1451 /* mark codec as probed and add to card codec list */
1453 list_add(&codec->card_list, &card->codec_dev_list);
1456 /* probe the platform */
1457 if (!platform->probed) {
1458 if (platform->driver->probe) {
1459 ret = platform->driver->probe(platform);
1461 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1466 /* mark platform as probed and add to card platform list */
1467 platform->probed = 1;
1468 list_add(&platform->card_list, &card->platform_dev_list);
1471 /* probe the CODEC DAI */
1472 if (!codec_dai->probed) {
1473 if (codec_dai->driver->probe) {
1474 ret = codec_dai->driver->probe(codec_dai);
1476 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1482 /* mark cpu_dai as probed and add to card cpu_dai list */
1483 codec_dai->probed = 1;
1484 list_add(&codec_dai->card_list, &card->dai_dev_list);
1487 /* DAPM dai link stream work */
1488 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1490 /* now that all clients have probed, initialise the DAI link */
1491 if (dai_link->init) {
1492 ret = dai_link->init(rtd);
1494 printk(KERN_ERR "asoc: failed to init %s\n", dai_link->stream_name);
1499 /* Make sure all DAPM widgets are instantiated */
1500 snd_soc_dapm_new_widgets(&codec->dapm);
1501 snd_soc_dapm_sync(&codec->dapm);
1503 /* register the rtd device */
1504 rtd->dev.release = rtd_release;
1505 rtd->dev.init_name = dai_link->name;
1506 ret = device_register(&rtd->dev);
1508 printk(KERN_ERR "asoc: failed to register DAI runtime device %d\n", ret);
1512 rtd->dev_registered = 1;
1513 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1515 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1517 /* add DAPM sysfs entries for this codec */
1518 ret = snd_soc_dapm_sys_add(&rtd->dev);
1520 printk(KERN_WARNING "asoc: failed to add codec dapm sysfs entries\n");
1522 /* add codec sysfs entries */
1523 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1525 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1527 /* create the pcm */
1528 ret = soc_new_pcm(rtd, num);
1530 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1534 /* add platform data for AC97 devices */
1535 if (rtd->codec_dai->driver->ac97_control)
1536 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1541 #ifdef CONFIG_SND_SOC_AC97_BUS
1542 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1546 /* Only instantiate AC97 if not already done by the adaptor
1547 * for the generic AC97 subsystem.
1549 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
1551 * It is possible that the AC97 device is already registered to
1552 * the device subsystem. This happens when the device is created
1553 * via snd_ac97_mixer(). Currently only SoC codec that does so
1554 * is the generic AC97 glue but others migh emerge.
1556 * In those cases we don't try to register the device again.
1558 if (!rtd->codec->ac97_created)
1561 ret = soc_ac97_dev_register(rtd->codec);
1563 printk(KERN_ERR "asoc: AC97 device register failed\n");
1567 rtd->codec->ac97_registered = 1;
1572 static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1574 if (codec->ac97_registered) {
1575 soc_ac97_dev_unregister(codec);
1576 codec->ac97_registered = 0;
1581 static void snd_soc_instantiate_card(struct snd_soc_card *card)
1583 struct platform_device *pdev = to_platform_device(card->dev);
1586 mutex_lock(&card->mutex);
1588 if (card->instantiated) {
1589 mutex_unlock(&card->mutex);
1594 for (i = 0; i < card->num_links; i++)
1595 soc_bind_dai_link(card, i);
1597 /* bind completed ? */
1598 if (card->num_rtd != card->num_links) {
1599 mutex_unlock(&card->mutex);
1603 /* card bind complete so register a sound card */
1604 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1605 card->owner, 0, &card->snd_card);
1607 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1609 mutex_unlock(&card->mutex);
1612 card->snd_card->dev = card->dev;
1615 /* deferred resume work */
1616 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1619 /* initialise the sound card only once */
1621 ret = card->probe(pdev);
1623 goto card_probe_error;
1626 for (i = 0; i < card->num_links; i++) {
1627 ret = soc_probe_dai_link(card, i);
1629 pr_err("asoc: failed to instantiate card %s: %d\n",
1635 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1637 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1640 ret = snd_card_register(card->snd_card);
1642 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1646 #ifdef CONFIG_SND_SOC_AC97_BUS
1647 /* register any AC97 codecs */
1648 for (i = 0; i < card->num_rtd; i++) {
1649 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1651 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1657 card->instantiated = 1;
1658 mutex_unlock(&card->mutex);
1662 for (i = 0; i < card->num_links; i++)
1663 soc_remove_dai_link(card, i);
1669 snd_card_free(card->snd_card);
1671 mutex_unlock(&card->mutex);
1675 * Attempt to initialise any uninitialised cards. Must be called with
1678 static void snd_soc_instantiate_cards(void)
1680 struct snd_soc_card *card;
1681 list_for_each_entry(card, &card_list, list)
1682 snd_soc_instantiate_card(card);
1685 /* probes a new socdev */
1686 static int soc_probe(struct platform_device *pdev)
1688 struct snd_soc_card *card = platform_get_drvdata(pdev);
1691 /* Bodge while we unpick instantiation */
1692 card->dev = &pdev->dev;
1693 INIT_LIST_HEAD(&card->dai_dev_list);
1694 INIT_LIST_HEAD(&card->codec_dev_list);
1695 INIT_LIST_HEAD(&card->platform_dev_list);
1697 soc_init_card_debugfs(card);
1699 ret = snd_soc_register_card(card);
1701 dev_err(&pdev->dev, "Failed to register card\n");
1708 /* removes a socdev */
1709 static int soc_remove(struct platform_device *pdev)
1711 struct snd_soc_card *card = platform_get_drvdata(pdev);
1714 if (card->instantiated) {
1716 /* make sure any delayed work runs */
1717 for (i = 0; i < card->num_rtd; i++) {
1718 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1719 run_delayed_work(&rtd->delayed_work);
1722 /* remove and free each DAI */
1723 for (i = 0; i < card->num_rtd; i++)
1724 soc_remove_dai_link(card, i);
1726 soc_cleanup_card_debugfs(card);
1728 /* remove the card */
1733 snd_card_free(card->snd_card);
1735 snd_soc_unregister_card(card);
1739 static int soc_poweroff(struct device *dev)
1741 struct platform_device *pdev = to_platform_device(dev);
1742 struct snd_soc_card *card = platform_get_drvdata(pdev);
1745 if (!card->instantiated)
1748 /* Flush out pmdown_time work - we actually do want to run it
1749 * now, we're shutting down so no imminent restart. */
1750 for (i = 0; i < card->num_rtd; i++) {
1751 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1752 run_delayed_work(&rtd->delayed_work);
1755 snd_soc_dapm_shutdown(card);
1760 static const struct dev_pm_ops soc_pm_ops = {
1761 .suspend = soc_suspend,
1762 .resume = soc_resume,
1763 .poweroff = soc_poweroff,
1766 /* ASoC platform driver */
1767 static struct platform_driver soc_driver = {
1769 .name = "soc-audio",
1770 .owner = THIS_MODULE,
1774 .remove = soc_remove,
1777 /* create a new pcm */
1778 static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
1780 struct snd_soc_codec *codec = rtd->codec;
1781 struct snd_soc_platform *platform = rtd->platform;
1782 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1783 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1784 struct snd_pcm *pcm;
1786 int ret = 0, playback = 0, capture = 0;
1788 /* check client and interface hw capabilities */
1789 snprintf(new_name, sizeof(new_name), "%s %s-%d",
1790 rtd->dai_link->stream_name, codec_dai->name, num);
1792 if (codec_dai->driver->playback.channels_min)
1794 if (codec_dai->driver->capture.channels_min)
1797 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
1798 ret = snd_pcm_new(rtd->card->snd_card, new_name,
1799 num, playback, capture, &pcm);
1801 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
1806 pcm->private_data = rtd;
1807 soc_pcm_ops.mmap = platform->driver->ops->mmap;
1808 soc_pcm_ops.pointer = platform->driver->ops->pointer;
1809 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
1810 soc_pcm_ops.copy = platform->driver->ops->copy;
1811 soc_pcm_ops.silence = platform->driver->ops->silence;
1812 soc_pcm_ops.ack = platform->driver->ops->ack;
1813 soc_pcm_ops.page = platform->driver->ops->page;
1816 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1819 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1821 ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
1823 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1827 pcm->private_free = platform->driver->pcm_free;
1828 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1834 * snd_soc_codec_volatile_register: Report if a register is volatile.
1836 * @codec: CODEC to query.
1837 * @reg: Register to query.
1839 * Boolean function indiciating if a CODEC register is volatile.
1841 int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1843 if (codec->driver->volatile_register)
1844 return codec->driver->volatile_register(reg);
1848 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1851 * snd_soc_new_ac97_codec - initailise AC97 device
1852 * @codec: audio codec
1853 * @ops: AC97 bus operations
1854 * @num: AC97 codec number
1856 * Initialises AC97 codec resources for use by ad-hoc devices only.
1858 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1859 struct snd_ac97_bus_ops *ops, int num)
1861 mutex_lock(&codec->mutex);
1863 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1864 if (codec->ac97 == NULL) {
1865 mutex_unlock(&codec->mutex);
1869 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1870 if (codec->ac97->bus == NULL) {
1873 mutex_unlock(&codec->mutex);
1877 codec->ac97->bus->ops = ops;
1878 codec->ac97->num = num;
1881 * Mark the AC97 device to be created by us. This way we ensure that the
1882 * device will be registered with the device subsystem later on.
1884 codec->ac97_created = 1;
1886 mutex_unlock(&codec->mutex);
1889 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1892 * snd_soc_free_ac97_codec - free AC97 codec device
1893 * @codec: audio codec
1895 * Frees AC97 codec device resources.
1897 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1899 mutex_lock(&codec->mutex);
1900 #ifdef CONFIG_SND_SOC_AC97_BUS
1901 soc_unregister_ac97_dai_link(codec);
1903 kfree(codec->ac97->bus);
1906 codec->ac97_created = 0;
1907 mutex_unlock(&codec->mutex);
1909 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1911 unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1915 ret = codec->driver->read(codec, reg);
1916 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
1920 EXPORT_SYMBOL_GPL(snd_soc_read);
1922 unsigned int snd_soc_write(struct snd_soc_codec *codec,
1923 unsigned int reg, unsigned int val)
1925 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
1926 return codec->driver->write(codec, reg, val);
1928 EXPORT_SYMBOL_GPL(snd_soc_write);
1931 * snd_soc_update_bits - update codec register bits
1932 * @codec: audio codec
1933 * @reg: codec register
1934 * @mask: register mask
1937 * Writes new register value.
1939 * Returns 1 for change else 0.
1941 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1942 unsigned int mask, unsigned int value)
1945 unsigned int old, new;
1947 old = snd_soc_read(codec, reg);
1948 new = (old & ~mask) | value;
1949 change = old != new;
1951 snd_soc_write(codec, reg, new);
1955 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1958 * snd_soc_update_bits_locked - update codec register bits
1959 * @codec: audio codec
1960 * @reg: codec register
1961 * @mask: register mask
1964 * Writes new register value, and takes the codec mutex.
1966 * Returns 1 for change else 0.
1968 int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1969 unsigned short reg, unsigned int mask,
1974 mutex_lock(&codec->mutex);
1975 change = snd_soc_update_bits(codec, reg, mask, value);
1976 mutex_unlock(&codec->mutex);
1980 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
1983 * snd_soc_test_bits - test register for change
1984 * @codec: audio codec
1985 * @reg: codec register
1986 * @mask: register mask
1989 * Tests a register with a new value and checks if the new value is
1990 * different from the old value.
1992 * Returns 1 for change else 0.
1994 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1995 unsigned int mask, unsigned int value)
1998 unsigned int old, new;
2000 old = snd_soc_read(codec, reg);
2001 new = (old & ~mask) | value;
2002 change = old != new;
2006 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2009 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2010 * @substream: the pcm substream
2011 * @hw: the hardware parameters
2013 * Sets the substream runtime hardware parameters.
2015 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2016 const struct snd_pcm_hardware *hw)
2018 struct snd_pcm_runtime *runtime = substream->runtime;
2019 runtime->hw.info = hw->info;
2020 runtime->hw.formats = hw->formats;
2021 runtime->hw.period_bytes_min = hw->period_bytes_min;
2022 runtime->hw.period_bytes_max = hw->period_bytes_max;
2023 runtime->hw.periods_min = hw->periods_min;
2024 runtime->hw.periods_max = hw->periods_max;
2025 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2026 runtime->hw.fifo_size = hw->fifo_size;
2029 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2032 * snd_soc_cnew - create new control
2033 * @_template: control template
2034 * @data: control private data
2035 * @long_name: control long name
2037 * Create a new mixer control from a template control.
2039 * Returns 0 for success, else error.
2041 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2042 void *data, char *long_name)
2044 struct snd_kcontrol_new template;
2046 memcpy(&template, _template, sizeof(template));
2048 template.name = long_name;
2051 return snd_ctl_new1(&template, data);
2053 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2056 * snd_soc_add_controls - add an array of controls to a codec.
2057 * Convienience function to add a list of controls. Many codecs were
2058 * duplicating this code.
2060 * @codec: codec to add controls to
2061 * @controls: array of controls to add
2062 * @num_controls: number of elements in the array
2064 * Return 0 for success, else error.
2066 int snd_soc_add_controls(struct snd_soc_codec *codec,
2067 const struct snd_kcontrol_new *controls, int num_controls)
2069 struct snd_card *card = codec->card->snd_card;
2072 for (i = 0; i < num_controls; i++) {
2073 const struct snd_kcontrol_new *control = &controls[i];
2074 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
2076 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
2077 codec->name, control->name, err);
2084 EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2087 * snd_soc_info_enum_double - enumerated double mixer info callback
2088 * @kcontrol: mixer control
2089 * @uinfo: control element information
2091 * Callback to provide information about a double enumerated
2094 * Returns 0 for success.
2096 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2097 struct snd_ctl_elem_info *uinfo)
2099 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2101 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2102 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2103 uinfo->value.enumerated.items = e->max;
2105 if (uinfo->value.enumerated.item > e->max - 1)
2106 uinfo->value.enumerated.item = e->max - 1;
2107 strcpy(uinfo->value.enumerated.name,
2108 e->texts[uinfo->value.enumerated.item]);
2111 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2114 * snd_soc_get_enum_double - enumerated double mixer get callback
2115 * @kcontrol: mixer control
2116 * @ucontrol: control element information
2118 * Callback to get the value of a double enumerated mixer.
2120 * Returns 0 for success.
2122 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2123 struct snd_ctl_elem_value *ucontrol)
2125 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2126 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2127 unsigned int val, bitmask;
2129 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2131 val = snd_soc_read(codec, e->reg);
2132 ucontrol->value.enumerated.item[0]
2133 = (val >> e->shift_l) & (bitmask - 1);
2134 if (e->shift_l != e->shift_r)
2135 ucontrol->value.enumerated.item[1] =
2136 (val >> e->shift_r) & (bitmask - 1);
2140 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2143 * snd_soc_put_enum_double - enumerated double mixer put callback
2144 * @kcontrol: mixer control
2145 * @ucontrol: control element information
2147 * Callback to set the value of a double enumerated mixer.
2149 * Returns 0 for success.
2151 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2152 struct snd_ctl_elem_value *ucontrol)
2154 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2155 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2157 unsigned int mask, bitmask;
2159 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2161 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2163 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2164 mask = (bitmask - 1) << e->shift_l;
2165 if (e->shift_l != e->shift_r) {
2166 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2168 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2169 mask |= (bitmask - 1) << e->shift_r;
2172 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2174 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2177 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2178 * @kcontrol: mixer control
2179 * @ucontrol: control element information
2181 * Callback to get the value of a double semi enumerated mixer.
2183 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2184 * used for handling bitfield coded enumeration for example.
2186 * Returns 0 for success.
2188 int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2189 struct snd_ctl_elem_value *ucontrol)
2191 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2192 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2193 unsigned int reg_val, val, mux;
2195 reg_val = snd_soc_read(codec, e->reg);
2196 val = (reg_val >> e->shift_l) & e->mask;
2197 for (mux = 0; mux < e->max; mux++) {
2198 if (val == e->values[mux])
2201 ucontrol->value.enumerated.item[0] = mux;
2202 if (e->shift_l != e->shift_r) {
2203 val = (reg_val >> e->shift_r) & e->mask;
2204 for (mux = 0; mux < e->max; mux++) {
2205 if (val == e->values[mux])
2208 ucontrol->value.enumerated.item[1] = mux;
2213 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2216 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2217 * @kcontrol: mixer control
2218 * @ucontrol: control element information
2220 * Callback to set the value of a double semi enumerated mixer.
2222 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2223 * used for handling bitfield coded enumeration for example.
2225 * Returns 0 for success.
2227 int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2228 struct snd_ctl_elem_value *ucontrol)
2230 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2231 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2235 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2237 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2238 mask = e->mask << e->shift_l;
2239 if (e->shift_l != e->shift_r) {
2240 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2242 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2243 mask |= e->mask << e->shift_r;
2246 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2248 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2251 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2252 * @kcontrol: mixer control
2253 * @uinfo: control element information
2255 * Callback to provide information about an external enumerated
2258 * Returns 0 for success.
2260 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2261 struct snd_ctl_elem_info *uinfo)
2263 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2265 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2267 uinfo->value.enumerated.items = e->max;
2269 if (uinfo->value.enumerated.item > e->max - 1)
2270 uinfo->value.enumerated.item = e->max - 1;
2271 strcpy(uinfo->value.enumerated.name,
2272 e->texts[uinfo->value.enumerated.item]);
2275 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2278 * snd_soc_info_volsw_ext - external single mixer info callback
2279 * @kcontrol: mixer control
2280 * @uinfo: control element information
2282 * Callback to provide information about a single external mixer control.
2284 * Returns 0 for success.
2286 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2287 struct snd_ctl_elem_info *uinfo)
2289 int max = kcontrol->private_value;
2291 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2292 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2294 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2297 uinfo->value.integer.min = 0;
2298 uinfo->value.integer.max = max;
2301 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2304 * snd_soc_info_volsw - single mixer info callback
2305 * @kcontrol: mixer control
2306 * @uinfo: control element information
2308 * Callback to provide information about a single mixer control.
2310 * Returns 0 for success.
2312 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2313 struct snd_ctl_elem_info *uinfo)
2315 struct soc_mixer_control *mc =
2316 (struct soc_mixer_control *)kcontrol->private_value;
2318 unsigned int shift = mc->shift;
2319 unsigned int rshift = mc->rshift;
2321 if (!mc->platform_max)
2322 mc->platform_max = mc->max;
2323 platform_max = mc->platform_max;
2325 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2326 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2328 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2330 uinfo->count = shift == rshift ? 1 : 2;
2331 uinfo->value.integer.min = 0;
2332 uinfo->value.integer.max = platform_max;
2335 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2338 * snd_soc_get_volsw - single mixer get callback
2339 * @kcontrol: mixer control
2340 * @ucontrol: control element information
2342 * Callback to get the value of a single mixer control.
2344 * Returns 0 for success.
2346 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2347 struct snd_ctl_elem_value *ucontrol)
2349 struct soc_mixer_control *mc =
2350 (struct soc_mixer_control *)kcontrol->private_value;
2351 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2352 unsigned int reg = mc->reg;
2353 unsigned int shift = mc->shift;
2354 unsigned int rshift = mc->rshift;
2356 unsigned int mask = (1 << fls(max)) - 1;
2357 unsigned int invert = mc->invert;
2359 ucontrol->value.integer.value[0] =
2360 (snd_soc_read(codec, reg) >> shift) & mask;
2361 if (shift != rshift)
2362 ucontrol->value.integer.value[1] =
2363 (snd_soc_read(codec, reg) >> rshift) & mask;
2365 ucontrol->value.integer.value[0] =
2366 max - ucontrol->value.integer.value[0];
2367 if (shift != rshift)
2368 ucontrol->value.integer.value[1] =
2369 max - ucontrol->value.integer.value[1];
2374 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2377 * snd_soc_put_volsw - single mixer put callback
2378 * @kcontrol: mixer control
2379 * @ucontrol: control element information
2381 * Callback to set the value of a single mixer control.
2383 * Returns 0 for success.
2385 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2386 struct snd_ctl_elem_value *ucontrol)
2388 struct soc_mixer_control *mc =
2389 (struct soc_mixer_control *)kcontrol->private_value;
2390 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2391 unsigned int reg = mc->reg;
2392 unsigned int shift = mc->shift;
2393 unsigned int rshift = mc->rshift;
2395 unsigned int mask = (1 << fls(max)) - 1;
2396 unsigned int invert = mc->invert;
2397 unsigned int val, val2, val_mask;
2399 val = (ucontrol->value.integer.value[0] & mask);
2402 val_mask = mask << shift;
2404 if (shift != rshift) {
2405 val2 = (ucontrol->value.integer.value[1] & mask);
2408 val_mask |= mask << rshift;
2409 val |= val2 << rshift;
2411 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2413 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2416 * snd_soc_info_volsw_2r - double mixer info callback
2417 * @kcontrol: mixer control
2418 * @uinfo: control element information
2420 * Callback to provide information about a double mixer control that
2421 * spans 2 codec registers.
2423 * Returns 0 for success.
2425 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2426 struct snd_ctl_elem_info *uinfo)
2428 struct soc_mixer_control *mc =
2429 (struct soc_mixer_control *)kcontrol->private_value;
2432 if (!mc->platform_max)
2433 mc->platform_max = mc->max;
2434 platform_max = mc->platform_max;
2436 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2437 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2439 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2442 uinfo->value.integer.min = 0;
2443 uinfo->value.integer.max = platform_max;
2446 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2449 * snd_soc_get_volsw_2r - double mixer get callback
2450 * @kcontrol: mixer control
2451 * @ucontrol: control element information
2453 * Callback to get the value of a double mixer control that spans 2 registers.
2455 * Returns 0 for success.
2457 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2458 struct snd_ctl_elem_value *ucontrol)
2460 struct soc_mixer_control *mc =
2461 (struct soc_mixer_control *)kcontrol->private_value;
2462 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2463 unsigned int reg = mc->reg;
2464 unsigned int reg2 = mc->rreg;
2465 unsigned int shift = mc->shift;
2467 unsigned int mask = (1 << fls(max)) - 1;
2468 unsigned int invert = mc->invert;
2470 ucontrol->value.integer.value[0] =
2471 (snd_soc_read(codec, reg) >> shift) & mask;
2472 ucontrol->value.integer.value[1] =
2473 (snd_soc_read(codec, reg2) >> shift) & mask;
2475 ucontrol->value.integer.value[0] =
2476 max - ucontrol->value.integer.value[0];
2477 ucontrol->value.integer.value[1] =
2478 max - ucontrol->value.integer.value[1];
2483 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2486 * snd_soc_put_volsw_2r - double mixer set callback
2487 * @kcontrol: mixer control
2488 * @ucontrol: control element information
2490 * Callback to set the value of a double mixer control that spans 2 registers.
2492 * Returns 0 for success.
2494 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2495 struct snd_ctl_elem_value *ucontrol)
2497 struct soc_mixer_control *mc =
2498 (struct soc_mixer_control *)kcontrol->private_value;
2499 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2500 unsigned int reg = mc->reg;
2501 unsigned int reg2 = mc->rreg;
2502 unsigned int shift = mc->shift;
2504 unsigned int mask = (1 << fls(max)) - 1;
2505 unsigned int invert = mc->invert;
2507 unsigned int val, val2, val_mask;
2509 val_mask = mask << shift;
2510 val = (ucontrol->value.integer.value[0] & mask);
2511 val2 = (ucontrol->value.integer.value[1] & mask);
2519 val2 = val2 << shift;
2521 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2525 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2528 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2531 * snd_soc_info_volsw_s8 - signed mixer info callback
2532 * @kcontrol: mixer control
2533 * @uinfo: control element information
2535 * Callback to provide information about a signed mixer control.
2537 * Returns 0 for success.
2539 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2540 struct snd_ctl_elem_info *uinfo)
2542 struct soc_mixer_control *mc =
2543 (struct soc_mixer_control *)kcontrol->private_value;
2547 if (!mc->platform_max)
2548 mc->platform_max = mc->max;
2549 platform_max = mc->platform_max;
2551 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2553 uinfo->value.integer.min = 0;
2554 uinfo->value.integer.max = platform_max - min;
2557 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2560 * snd_soc_get_volsw_s8 - signed mixer get callback
2561 * @kcontrol: mixer control
2562 * @ucontrol: control element information
2564 * Callback to get the value of a signed mixer control.
2566 * Returns 0 for success.
2568 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2569 struct snd_ctl_elem_value *ucontrol)
2571 struct soc_mixer_control *mc =
2572 (struct soc_mixer_control *)kcontrol->private_value;
2573 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2574 unsigned int reg = mc->reg;
2576 int val = snd_soc_read(codec, reg);
2578 ucontrol->value.integer.value[0] =
2579 ((signed char)(val & 0xff))-min;
2580 ucontrol->value.integer.value[1] =
2581 ((signed char)((val >> 8) & 0xff))-min;
2584 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2587 * snd_soc_put_volsw_sgn - signed mixer put callback
2588 * @kcontrol: mixer control
2589 * @ucontrol: control element information
2591 * Callback to set the value of a signed mixer control.
2593 * Returns 0 for success.
2595 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2596 struct snd_ctl_elem_value *ucontrol)
2598 struct soc_mixer_control *mc =
2599 (struct soc_mixer_control *)kcontrol->private_value;
2600 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2601 unsigned int reg = mc->reg;
2605 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2606 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2608 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2610 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2613 * snd_soc_limit_volume - Set new limit to an existing volume control.
2615 * @codec: where to look for the control
2616 * @name: Name of the control
2617 * @max: new maximum limit
2619 * Return 0 for success, else error.
2621 int snd_soc_limit_volume(struct snd_soc_codec *codec,
2622 const char *name, int max)
2624 struct snd_card *card = codec->card->snd_card;
2625 struct snd_kcontrol *kctl;
2626 struct soc_mixer_control *mc;
2630 /* Sanity check for name and max */
2631 if (unlikely(!name || max <= 0))
2634 list_for_each_entry(kctl, &card->controls, list) {
2635 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2641 mc = (struct soc_mixer_control *)kctl->private_value;
2642 if (max <= mc->max) {
2643 mc->platform_max = max;
2649 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2652 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2653 * mixer info callback
2654 * @kcontrol: mixer control
2655 * @uinfo: control element information
2657 * Returns 0 for success.
2659 int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2660 struct snd_ctl_elem_info *uinfo)
2662 struct soc_mixer_control *mc =
2663 (struct soc_mixer_control *)kcontrol->private_value;
2667 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2669 uinfo->value.integer.min = 0;
2670 uinfo->value.integer.max = max-min;
2674 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2677 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2678 * mixer get callback
2679 * @kcontrol: mixer control
2680 * @uinfo: control element information
2682 * Returns 0 for success.
2684 int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2685 struct snd_ctl_elem_value *ucontrol)
2687 struct soc_mixer_control *mc =
2688 (struct soc_mixer_control *)kcontrol->private_value;
2689 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2690 unsigned int mask = (1<<mc->shift)-1;
2692 int val = snd_soc_read(codec, mc->reg) & mask;
2693 int valr = snd_soc_read(codec, mc->rreg) & mask;
2695 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2696 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
2699 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2702 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2703 * mixer put callback
2704 * @kcontrol: mixer control
2705 * @uinfo: control element information
2707 * Returns 0 for success.
2709 int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2710 struct snd_ctl_elem_value *ucontrol)
2712 struct soc_mixer_control *mc =
2713 (struct soc_mixer_control *)kcontrol->private_value;
2714 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2715 unsigned int mask = (1<<mc->shift)-1;
2718 unsigned int val, valr, oval, ovalr;
2720 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2722 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2725 oval = snd_soc_read(codec, mc->reg) & mask;
2726 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2730 ret = snd_soc_write(codec, mc->reg, val);
2734 if (ovalr != valr) {
2735 ret = snd_soc_write(codec, mc->rreg, valr);
2742 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2745 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2747 * @clk_id: DAI specific clock ID
2748 * @freq: new clock frequency in Hz
2749 * @dir: new clock direction - input/output.
2751 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2753 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2754 unsigned int freq, int dir)
2756 if (dai->driver && dai->driver->ops->set_sysclk)
2757 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2761 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2764 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2766 * @div_id: DAI specific clock divider ID
2767 * @div: new clock divisor.
2769 * Configures the clock dividers. This is used to derive the best DAI bit and
2770 * frame clocks from the system or master clock. It's best to set the DAI bit
2771 * and frame clocks as low as possible to save system power.
2773 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2774 int div_id, int div)
2776 if (dai->driver && dai->driver->ops->set_clkdiv)
2777 return dai->driver->ops->set_clkdiv(dai, div_id, div);
2781 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2784 * snd_soc_dai_set_pll - configure DAI PLL.
2786 * @pll_id: DAI specific PLL ID
2787 * @source: DAI specific source for the PLL
2788 * @freq_in: PLL input clock frequency in Hz
2789 * @freq_out: requested PLL output clock frequency in Hz
2791 * Configures and enables PLL to generate output clock based on input clock.
2793 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2794 unsigned int freq_in, unsigned int freq_out)
2796 if (dai->driver && dai->driver->ops->set_pll)
2797 return dai->driver->ops->set_pll(dai, pll_id, source,
2802 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2805 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2807 * @fmt: SND_SOC_DAIFMT_ format value.
2809 * Configures the DAI hardware format and clocking.
2811 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2813 if (dai->driver && dai->driver->ops->set_fmt)
2814 return dai->driver->ops->set_fmt(dai, fmt);
2818 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2821 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2823 * @tx_mask: bitmask representing active TX slots.
2824 * @rx_mask: bitmask representing active RX slots.
2825 * @slots: Number of slots in use.
2826 * @slot_width: Width in bits for each slot.
2828 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2831 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2832 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2834 if (dai->driver && dai->driver->ops->set_tdm_slot)
2835 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2840 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2843 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2845 * @tx_num: how many TX channels
2846 * @tx_slot: pointer to an array which imply the TX slot number channel
2848 * @rx_num: how many RX channels
2849 * @rx_slot: pointer to an array which imply the RX slot number channel
2852 * configure the relationship between channel number and TDM slot number.
2854 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2855 unsigned int tx_num, unsigned int *tx_slot,
2856 unsigned int rx_num, unsigned int *rx_slot)
2858 if (dai->driver && dai->driver->ops->set_channel_map)
2859 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2864 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2867 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2869 * @tristate: tristate enable
2871 * Tristates the DAI so that others can use it.
2873 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2875 if (dai->driver && dai->driver->ops->set_tristate)
2876 return dai->driver->ops->set_tristate(dai, tristate);
2880 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2883 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2885 * @mute: mute enable
2887 * Mutes the DAI DAC.
2889 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2891 if (dai->driver && dai->driver->ops->digital_mute)
2892 return dai->driver->ops->digital_mute(dai, mute);
2896 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2899 * snd_soc_register_card - Register a card with the ASoC core
2901 * @card: Card to register
2903 * Note that currently this is an internal only function: it will be
2904 * exposed to machine drivers after further backporting of ASoC v2
2905 * registration APIs.
2907 static int snd_soc_register_card(struct snd_soc_card *card)
2911 if (!card->name || !card->dev)
2914 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) * card->num_links,
2916 if (card->rtd == NULL)
2919 for (i = 0; i < card->num_links; i++)
2920 card->rtd[i].dai_link = &card->dai_link[i];
2922 INIT_LIST_HEAD(&card->list);
2923 card->instantiated = 0;
2924 mutex_init(&card->mutex);
2926 mutex_lock(&client_mutex);
2927 list_add(&card->list, &card_list);
2928 snd_soc_instantiate_cards();
2929 mutex_unlock(&client_mutex);
2931 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2937 * snd_soc_unregister_card - Unregister a card with the ASoC core
2939 * @card: Card to unregister
2941 * Note that currently this is an internal only function: it will be
2942 * exposed to machine drivers after further backporting of ASoC v2
2943 * registration APIs.
2945 static int snd_soc_unregister_card(struct snd_soc_card *card)
2947 mutex_lock(&client_mutex);
2948 list_del(&card->list);
2949 mutex_unlock(&client_mutex);
2950 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2956 * Simplify DAI link configuration by removing ".-1" from device names
2957 * and sanitizing names.
2959 static inline char *fmt_single_name(struct device *dev, int *id)
2961 char *found, name[NAME_SIZE];
2964 if (dev_name(dev) == NULL)
2967 strncpy(name, dev_name(dev), NAME_SIZE);
2969 /* are we a "%s.%d" name (platform and SPI components) */
2970 found = strstr(name, dev->driver->name);
2973 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2975 /* discard ID from name if ID == -1 */
2977 found[strlen(dev->driver->name)] = '\0';
2981 /* I2C component devices are named "bus-addr" */
2982 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2983 char tmp[NAME_SIZE];
2985 /* create unique ID number from I2C addr and bus */
2986 *id = ((id1 & 0xffff) << 16) + id2;
2988 /* sanitize component name for DAI link creation */
2989 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2990 strncpy(name, tmp, NAME_SIZE);
2995 return kstrdup(name, GFP_KERNEL);
2999 * Simplify DAI link naming for single devices with multiple DAIs by removing
3000 * any ".-1" and using the DAI name (instead of device name).
3002 static inline char *fmt_multiple_name(struct device *dev,
3003 struct snd_soc_dai_driver *dai_drv)
3005 if (dai_drv->name == NULL) {
3006 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
3011 return kstrdup(dai_drv->name, GFP_KERNEL);
3015 * snd_soc_register_dai - Register a DAI with the ASoC core
3017 * @dai: DAI to register
3019 int snd_soc_register_dai(struct device *dev,
3020 struct snd_soc_dai_driver *dai_drv)
3022 struct snd_soc_dai *dai;
3024 dev_dbg(dev, "dai register %s\n", dev_name(dev));
3026 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3030 /* create DAI component name */
3031 dai->name = fmt_single_name(dev, &dai->id);
3032 if (dai->name == NULL) {
3038 dai->driver = dai_drv;
3039 if (!dai->driver->ops)
3040 dai->driver->ops = &null_dai_ops;
3042 mutex_lock(&client_mutex);
3043 list_add(&dai->list, &dai_list);
3044 snd_soc_instantiate_cards();
3045 mutex_unlock(&client_mutex);
3047 pr_debug("Registered DAI '%s'\n", dai->name);
3051 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3054 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3056 * @dai: DAI to unregister
3058 void snd_soc_unregister_dai(struct device *dev)
3060 struct snd_soc_dai *dai;
3062 list_for_each_entry(dai, &dai_list, list) {
3063 if (dev == dai->dev)
3069 mutex_lock(&client_mutex);
3070 list_del(&dai->list);
3071 mutex_unlock(&client_mutex);
3073 pr_debug("Unregistered DAI '%s'\n", dai->name);
3077 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3080 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3082 * @dai: Array of DAIs to register
3083 * @count: Number of DAIs
3085 int snd_soc_register_dais(struct device *dev,
3086 struct snd_soc_dai_driver *dai_drv, size_t count)
3088 struct snd_soc_dai *dai;
3091 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
3093 for (i = 0; i < count; i++) {
3095 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3101 /* create DAI component name */
3102 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3103 if (dai->name == NULL) {
3110 dai->driver = &dai_drv[i];
3111 if (dai->driver->id)
3112 dai->id = dai->driver->id;
3115 if (!dai->driver->ops)
3116 dai->driver->ops = &null_dai_ops;
3118 mutex_lock(&client_mutex);
3119 list_add(&dai->list, &dai_list);
3120 mutex_unlock(&client_mutex);
3122 pr_debug("Registered DAI '%s'\n", dai->name);
3125 snd_soc_instantiate_cards();
3129 for (i--; i >= 0; i--)
3130 snd_soc_unregister_dai(dev);
3134 EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3137 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3139 * @dai: Array of DAIs to unregister
3140 * @count: Number of DAIs
3142 void snd_soc_unregister_dais(struct device *dev, size_t count)
3146 for (i = 0; i < count; i++)
3147 snd_soc_unregister_dai(dev);
3149 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3152 * snd_soc_register_platform - Register a platform with the ASoC core
3154 * @platform: platform to register
3156 int snd_soc_register_platform(struct device *dev,
3157 struct snd_soc_platform_driver *platform_drv)
3159 struct snd_soc_platform *platform;
3161 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3163 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3164 if (platform == NULL)
3167 /* create platform component name */
3168 platform->name = fmt_single_name(dev, &platform->id);
3169 if (platform->name == NULL) {
3174 platform->dev = dev;
3175 platform->driver = platform_drv;
3177 mutex_lock(&client_mutex);
3178 list_add(&platform->list, &platform_list);
3179 snd_soc_instantiate_cards();
3180 mutex_unlock(&client_mutex);
3182 pr_debug("Registered platform '%s'\n", platform->name);
3186 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3189 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3191 * @platform: platform to unregister
3193 void snd_soc_unregister_platform(struct device *dev)
3195 struct snd_soc_platform *platform;
3197 list_for_each_entry(platform, &platform_list, list) {
3198 if (dev == platform->dev)
3204 mutex_lock(&client_mutex);
3205 list_del(&platform->list);
3206 mutex_unlock(&client_mutex);
3208 pr_debug("Unregistered platform '%s'\n", platform->name);
3209 kfree(platform->name);
3212 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3214 static u64 codec_format_map[] = {
3215 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3216 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3217 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3218 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3219 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3220 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3221 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3222 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3223 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3224 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3225 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3226 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3227 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3228 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3229 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3230 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3233 /* Fix up the DAI formats for endianness: codecs don't actually see
3234 * the endianness of the data but we're using the CPU format
3235 * definitions which do need to include endianness so we ensure that
3236 * codec DAIs always have both big and little endian variants set.
3238 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3242 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3243 if (stream->formats & codec_format_map[i])
3244 stream->formats |= codec_format_map[i];
3248 * snd_soc_register_codec - Register a codec with the ASoC core
3250 * @codec: codec to register
3252 int snd_soc_register_codec(struct device *dev,
3253 struct snd_soc_codec_driver *codec_drv,
3254 struct snd_soc_dai_driver *dai_drv, int num_dai)
3256 struct snd_soc_codec *codec;
3259 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3261 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3265 /* create CODEC component name */
3266 codec->name = fmt_single_name(dev, &codec->id);
3267 if (codec->name == NULL) {
3272 INIT_LIST_HEAD(&codec->dapm.widgets);
3273 INIT_LIST_HEAD(&codec->dapm.paths);
3274 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3275 codec->dapm.dev = dev;
3276 codec->dapm.codec = codec;
3278 /* allocate CODEC register cache */
3279 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3281 if (codec_drv->reg_cache_default)
3282 codec->reg_cache = kmemdup(codec_drv->reg_cache_default,
3283 codec_drv->reg_cache_size * codec_drv->reg_word_size, GFP_KERNEL);
3285 codec->reg_cache = kzalloc(codec_drv->reg_cache_size *
3286 codec_drv->reg_word_size, GFP_KERNEL);
3288 if (codec->reg_cache == NULL) {
3296 codec->driver = codec_drv;
3297 codec->num_dai = num_dai;
3298 mutex_init(&codec->mutex);
3300 for (i = 0; i < num_dai; i++) {
3301 fixup_codec_formats(&dai_drv[i].playback);
3302 fixup_codec_formats(&dai_drv[i].capture);
3305 /* register any DAIs */
3307 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3312 mutex_lock(&client_mutex);
3313 list_add(&codec->list, &codec_list);
3314 snd_soc_instantiate_cards();
3315 mutex_unlock(&client_mutex);
3317 pr_debug("Registered codec '%s'\n", codec->name);
3321 if (codec->reg_cache)
3322 kfree(codec->reg_cache);
3327 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3330 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3332 * @codec: codec to unregister
3334 void snd_soc_unregister_codec(struct device *dev)
3336 struct snd_soc_codec *codec;
3339 list_for_each_entry(codec, &codec_list, list) {
3340 if (dev == codec->dev)
3347 for (i = 0; i < codec->num_dai; i++)
3348 snd_soc_unregister_dai(dev);
3350 mutex_lock(&client_mutex);
3351 list_del(&codec->list);
3352 mutex_unlock(&client_mutex);
3354 pr_debug("Unregistered codec '%s'\n", codec->name);
3356 if (codec->reg_cache)
3357 kfree(codec->reg_cache);
3361 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3363 static int __init snd_soc_init(void)
3365 #ifdef CONFIG_DEBUG_FS
3366 debugfs_root = debugfs_create_dir("asoc", NULL);
3367 if (IS_ERR(debugfs_root) || !debugfs_root) {
3369 "ASoC: Failed to create debugfs directory\n");
3370 debugfs_root = NULL;
3373 if (!debugfs_create_file("codecs", 0444, debugfs_root, NULL,
3375 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3377 if (!debugfs_create_file("dais", 0444, debugfs_root, NULL,
3379 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
3381 if (!debugfs_create_file("platforms", 0444, debugfs_root, NULL,
3382 &platform_list_fops))
3383 pr_warn("ASoC: Failed to create platform list debugfs file\n");
3386 return platform_driver_register(&soc_driver);
3388 module_init(snd_soc_init);
3390 static void __exit snd_soc_exit(void)
3392 #ifdef CONFIG_DEBUG_FS
3393 debugfs_remove_recursive(debugfs_root);
3395 platform_driver_unregister(&soc_driver);
3397 module_exit(snd_soc_exit);
3399 /* Module information */
3400 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3401 MODULE_DESCRIPTION("ALSA SoC Core");
3402 MODULE_LICENSE("GPL");
3403 MODULE_ALIAS("platform:soc-audio");