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)");
70 /* codec register dump */
71 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
73 int ret, i, step = 1, count = 0;
75 if (!codec->driver->reg_cache_size)
78 if (codec->driver->reg_cache_step)
79 step = codec->driver->reg_cache_step;
81 count += sprintf(buf, "%s registers\n", codec->name);
82 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
83 if (codec->driver->readable_register && !codec->driver->readable_register(i))
86 count += sprintf(buf + count, "%2x: ", i);
87 if (count >= PAGE_SIZE - 1)
90 if (codec->driver->display_register) {
91 count += codec->driver->display_register(codec, buf + count,
92 PAGE_SIZE - count, i);
94 /* If the read fails it's almost certainly due to
95 * the register being volatile and the device being
98 ret = codec->driver->read(codec, i);
100 count += snprintf(buf + count,
104 count += snprintf(buf + count,
106 "<no data: %d>", ret);
109 if (count >= PAGE_SIZE - 1)
112 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
113 if (count >= PAGE_SIZE - 1)
117 /* Truncate count; min() would cause a warning */
118 if (count >= PAGE_SIZE)
119 count = PAGE_SIZE - 1;
123 static ssize_t codec_reg_show(struct device *dev,
124 struct device_attribute *attr, char *buf)
126 struct snd_soc_pcm_runtime *rtd =
127 container_of(dev, struct snd_soc_pcm_runtime, dev);
129 return soc_codec_reg_show(rtd->codec, buf);
132 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
134 static ssize_t pmdown_time_show(struct device *dev,
135 struct device_attribute *attr, char *buf)
137 struct snd_soc_pcm_runtime *rtd =
138 container_of(dev, struct snd_soc_pcm_runtime, dev);
140 return sprintf(buf, "%ld\n", rtd->pmdown_time);
143 static ssize_t pmdown_time_set(struct device *dev,
144 struct device_attribute *attr,
145 const char *buf, size_t count)
147 struct snd_soc_pcm_runtime *rtd =
148 container_of(dev, struct snd_soc_pcm_runtime, dev);
151 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
158 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
160 #ifdef CONFIG_DEBUG_FS
161 static int codec_reg_open_file(struct inode *inode, struct file *file)
163 file->private_data = inode->i_private;
167 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
168 size_t count, loff_t *ppos)
171 struct snd_soc_codec *codec = file->private_data;
172 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
175 ret = soc_codec_reg_show(codec, buf);
177 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
182 static ssize_t codec_reg_write_file(struct file *file,
183 const char __user *user_buf, size_t count, loff_t *ppos)
188 unsigned long reg, value;
190 struct snd_soc_codec *codec = file->private_data;
192 buf_size = min(count, (sizeof(buf)-1));
193 if (copy_from_user(buf, user_buf, buf_size))
197 if (codec->driver->reg_cache_step)
198 step = codec->driver->reg_cache_step;
200 while (*start == ' ')
202 reg = simple_strtoul(start, &start, 16);
203 if ((reg >= codec->driver->reg_cache_size) || (reg % step))
205 while (*start == ' ')
207 if (strict_strtoul(start, 16, &value))
209 codec->driver->write(codec, reg, value);
213 static const struct file_operations codec_reg_fops = {
214 .open = codec_reg_open_file,
215 .read = codec_reg_read_file,
216 .write = codec_reg_write_file,
217 .llseek = default_llseek,
220 static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
222 codec->debugfs_codec_root = debugfs_create_dir(codec->name ,
224 if (!codec->debugfs_codec_root) {
226 "ASoC: Failed to create codec debugfs directory\n");
230 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
231 codec->debugfs_codec_root,
232 codec, &codec_reg_fops);
233 if (!codec->debugfs_reg)
235 "ASoC: Failed to create codec register debugfs file\n");
237 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
238 codec->debugfs_codec_root,
240 if (!codec->debugfs_pop_time)
242 "Failed to create pop time debugfs file\n");
244 codec->debugfs_dapm = debugfs_create_dir("dapm",
245 codec->debugfs_codec_root);
246 if (!codec->debugfs_dapm)
248 "Failed to create DAPM debugfs directory\n");
250 snd_soc_dapm_debugfs_init(codec);
253 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
255 debugfs_remove_recursive(codec->debugfs_codec_root);
258 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
259 size_t count, loff_t *ppos)
261 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
262 ssize_t len, ret = 0;
263 struct snd_soc_codec *codec;
268 list_for_each_entry(codec, &codec_list, list) {
269 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
273 if (ret > PAGE_SIZE) {
280 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
287 static const struct file_operations codec_list_fops = {
288 .read = codec_list_read_file,
289 .llseek = default_llseek,/* read accesses f_pos */
292 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
293 size_t count, loff_t *ppos)
295 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
296 ssize_t len, ret = 0;
297 struct snd_soc_dai *dai;
302 list_for_each_entry(dai, &dai_list, list) {
303 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
306 if (ret > PAGE_SIZE) {
312 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
319 static const struct file_operations dai_list_fops = {
320 .read = dai_list_read_file,
321 .llseek = default_llseek,/* read accesses f_pos */
324 static ssize_t platform_list_read_file(struct file *file,
325 char __user *user_buf,
326 size_t count, loff_t *ppos)
328 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
329 ssize_t len, ret = 0;
330 struct snd_soc_platform *platform;
335 list_for_each_entry(platform, &platform_list, list) {
336 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
340 if (ret > PAGE_SIZE) {
346 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
353 static const struct file_operations platform_list_fops = {
354 .read = platform_list_read_file,
355 .llseek = default_llseek,/* read accesses f_pos */
360 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
364 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
369 #ifdef CONFIG_SND_SOC_AC97_BUS
370 /* unregister ac97 codec */
371 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
373 if (codec->ac97->dev.bus)
374 device_unregister(&codec->ac97->dev);
378 /* stop no dev release warning */
379 static void soc_ac97_device_release(struct device *dev){}
381 /* register ac97 codec to bus */
382 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
386 codec->ac97->dev.bus = &ac97_bus_type;
387 codec->ac97->dev.parent = codec->card->dev;
388 codec->ac97->dev.release = soc_ac97_device_release;
390 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
391 codec->card->snd_card->number, 0, codec->name);
392 err = device_register(&codec->ac97->dev);
394 snd_printk(KERN_ERR "Can't register ac97 bus\n");
395 codec->ac97->dev.bus = NULL;
402 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
404 struct snd_soc_pcm_runtime *rtd = substream->private_data;
405 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
406 struct snd_soc_dai *codec_dai = rtd->codec_dai;
409 if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
410 rtd->dai_link->symmetric_rates) {
411 dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
414 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
415 SNDRV_PCM_HW_PARAM_RATE,
420 "Unable to apply rate symmetry constraint: %d\n", ret);
429 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
430 * then initialized and any private data can be allocated. This also calls
431 * startup for the cpu DAI, platform, machine and codec DAI.
433 static int soc_pcm_open(struct snd_pcm_substream *substream)
435 struct snd_soc_pcm_runtime *rtd = substream->private_data;
436 struct snd_pcm_runtime *runtime = substream->runtime;
437 struct snd_soc_platform *platform = rtd->platform;
438 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
439 struct snd_soc_dai *codec_dai = rtd->codec_dai;
440 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
441 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
444 mutex_lock(&pcm_mutex);
446 /* startup the audio subsystem */
447 if (cpu_dai->driver->ops->startup) {
448 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
450 printk(KERN_ERR "asoc: can't open interface %s\n",
456 if (platform->driver->ops->open) {
457 ret = platform->driver->ops->open(substream);
459 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
464 if (codec_dai->driver->ops->startup) {
465 ret = codec_dai->driver->ops->startup(substream, codec_dai);
467 printk(KERN_ERR "asoc: can't open codec %s\n",
473 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
474 ret = rtd->dai_link->ops->startup(substream);
476 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
481 /* Check that the codec and cpu DAI's are compatible */
482 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
483 runtime->hw.rate_min =
484 max(codec_dai_drv->playback.rate_min,
485 cpu_dai_drv->playback.rate_min);
486 runtime->hw.rate_max =
487 min(codec_dai_drv->playback.rate_max,
488 cpu_dai_drv->playback.rate_max);
489 runtime->hw.channels_min =
490 max(codec_dai_drv->playback.channels_min,
491 cpu_dai_drv->playback.channels_min);
492 runtime->hw.channels_max =
493 min(codec_dai_drv->playback.channels_max,
494 cpu_dai_drv->playback.channels_max);
495 runtime->hw.formats =
496 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
498 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
499 if (codec_dai_drv->playback.rates
500 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
501 runtime->hw.rates |= cpu_dai_drv->playback.rates;
502 if (cpu_dai_drv->playback.rates
503 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
504 runtime->hw.rates |= codec_dai_drv->playback.rates;
506 runtime->hw.rate_min =
507 max(codec_dai_drv->capture.rate_min,
508 cpu_dai_drv->capture.rate_min);
509 runtime->hw.rate_max =
510 min(codec_dai_drv->capture.rate_max,
511 cpu_dai_drv->capture.rate_max);
512 runtime->hw.channels_min =
513 max(codec_dai_drv->capture.channels_min,
514 cpu_dai_drv->capture.channels_min);
515 runtime->hw.channels_max =
516 min(codec_dai_drv->capture.channels_max,
517 cpu_dai_drv->capture.channels_max);
518 runtime->hw.formats =
519 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
521 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
522 if (codec_dai_drv->capture.rates
523 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
524 runtime->hw.rates |= cpu_dai_drv->capture.rates;
525 if (cpu_dai_drv->capture.rates
526 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
527 runtime->hw.rates |= codec_dai_drv->capture.rates;
530 snd_pcm_limit_hw_rates(runtime);
531 if (!runtime->hw.rates) {
532 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
533 codec_dai->name, cpu_dai->name);
536 if (!runtime->hw.formats) {
537 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
538 codec_dai->name, cpu_dai->name);
541 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
542 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
543 codec_dai->name, cpu_dai->name);
547 /* Symmetry only applies if we've already got an active stream. */
548 if (cpu_dai->active || codec_dai->active) {
549 ret = soc_pcm_apply_symmetry(substream);
554 pr_debug("asoc: %s <-> %s info:\n",
555 codec_dai->name, cpu_dai->name);
556 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
557 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
558 runtime->hw.channels_max);
559 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
560 runtime->hw.rate_max);
562 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
563 cpu_dai->playback_active++;
564 codec_dai->playback_active++;
566 cpu_dai->capture_active++;
567 codec_dai->capture_active++;
571 rtd->codec->active++;
572 mutex_unlock(&pcm_mutex);
576 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
577 rtd->dai_link->ops->shutdown(substream);
580 if (codec_dai->driver->ops->shutdown)
581 codec_dai->driver->ops->shutdown(substream, codec_dai);
584 if (platform->driver->ops->close)
585 platform->driver->ops->close(substream);
588 if (cpu_dai->driver->ops->shutdown)
589 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
591 mutex_unlock(&pcm_mutex);
596 * Power down the audio subsystem pmdown_time msecs after close is called.
597 * This is to ensure there are no pops or clicks in between any music tracks
598 * due to DAPM power cycling.
600 static void close_delayed_work(struct work_struct *work)
602 struct snd_soc_pcm_runtime *rtd =
603 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
604 struct snd_soc_dai *codec_dai = rtd->codec_dai;
606 mutex_lock(&pcm_mutex);
608 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
609 codec_dai->driver->playback.stream_name,
610 codec_dai->playback_active ? "active" : "inactive",
611 codec_dai->pop_wait ? "yes" : "no");
613 /* are we waiting on this codec DAI stream */
614 if (codec_dai->pop_wait == 1) {
615 codec_dai->pop_wait = 0;
616 snd_soc_dapm_stream_event(rtd,
617 codec_dai->driver->playback.stream_name,
618 SND_SOC_DAPM_STREAM_STOP);
621 mutex_unlock(&pcm_mutex);
625 * Called by ALSA when a PCM substream is closed. Private data can be
626 * freed here. The cpu DAI, codec DAI, machine and platform are also
629 static int soc_codec_close(struct snd_pcm_substream *substream)
631 struct snd_soc_pcm_runtime *rtd = substream->private_data;
632 struct snd_soc_platform *platform = rtd->platform;
633 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
634 struct snd_soc_dai *codec_dai = rtd->codec_dai;
635 struct snd_soc_codec *codec = rtd->codec;
637 mutex_lock(&pcm_mutex);
639 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
640 cpu_dai->playback_active--;
641 codec_dai->playback_active--;
643 cpu_dai->capture_active--;
644 codec_dai->capture_active--;
651 /* Muting the DAC suppresses artifacts caused during digital
652 * shutdown, for example from stopping clocks.
654 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
655 snd_soc_dai_digital_mute(codec_dai, 1);
657 if (cpu_dai->driver->ops->shutdown)
658 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
660 if (codec_dai->driver->ops->shutdown)
661 codec_dai->driver->ops->shutdown(substream, codec_dai);
663 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
664 rtd->dai_link->ops->shutdown(substream);
666 if (platform->driver->ops->close)
667 platform->driver->ops->close(substream);
668 cpu_dai->runtime = NULL;
670 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
671 /* start delayed pop wq here for playback streams */
672 codec_dai->pop_wait = 1;
673 schedule_delayed_work(&rtd->delayed_work,
674 msecs_to_jiffies(rtd->pmdown_time));
676 /* capture streams can be powered down now */
677 snd_soc_dapm_stream_event(rtd,
678 codec_dai->driver->capture.stream_name,
679 SND_SOC_DAPM_STREAM_STOP);
682 mutex_unlock(&pcm_mutex);
687 * Called by ALSA when the PCM substream is prepared, can set format, sample
688 * rate, etc. This function is non atomic and can be called multiple times,
689 * it can refer to the runtime info.
691 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
693 struct snd_soc_pcm_runtime *rtd = substream->private_data;
694 struct snd_soc_platform *platform = rtd->platform;
695 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
696 struct snd_soc_dai *codec_dai = rtd->codec_dai;
699 mutex_lock(&pcm_mutex);
701 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
702 ret = rtd->dai_link->ops->prepare(substream);
704 printk(KERN_ERR "asoc: machine prepare error\n");
709 if (platform->driver->ops->prepare) {
710 ret = platform->driver->ops->prepare(substream);
712 printk(KERN_ERR "asoc: platform prepare error\n");
717 if (codec_dai->driver->ops->prepare) {
718 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
720 printk(KERN_ERR "asoc: codec DAI prepare error\n");
725 if (cpu_dai->driver->ops->prepare) {
726 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
728 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
733 /* cancel any delayed stream shutdown that is pending */
734 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
735 codec_dai->pop_wait) {
736 codec_dai->pop_wait = 0;
737 cancel_delayed_work(&rtd->delayed_work);
740 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
741 snd_soc_dapm_stream_event(rtd,
742 codec_dai->driver->playback.stream_name,
743 SND_SOC_DAPM_STREAM_START);
745 snd_soc_dapm_stream_event(rtd,
746 codec_dai->driver->capture.stream_name,
747 SND_SOC_DAPM_STREAM_START);
749 snd_soc_dai_digital_mute(codec_dai, 0);
752 mutex_unlock(&pcm_mutex);
757 * Called by ALSA when the hardware params are set by application. This
758 * function can also be called multiple times and can allocate buffers
759 * (using snd_pcm_lib_* ). It's non-atomic.
761 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
762 struct snd_pcm_hw_params *params)
764 struct snd_soc_pcm_runtime *rtd = substream->private_data;
765 struct snd_soc_platform *platform = rtd->platform;
766 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
767 struct snd_soc_dai *codec_dai = rtd->codec_dai;
770 mutex_lock(&pcm_mutex);
772 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
773 ret = rtd->dai_link->ops->hw_params(substream, params);
775 printk(KERN_ERR "asoc: machine hw_params failed\n");
780 if (codec_dai->driver->ops->hw_params) {
781 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
783 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
789 if (cpu_dai->driver->ops->hw_params) {
790 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
792 printk(KERN_ERR "asoc: interface %s hw params failed\n",
798 if (platform->driver->ops->hw_params) {
799 ret = platform->driver->ops->hw_params(substream, params);
801 printk(KERN_ERR "asoc: platform %s hw params failed\n",
807 rtd->rate = params_rate(params);
810 mutex_unlock(&pcm_mutex);
814 if (cpu_dai->driver->ops->hw_free)
815 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
818 if (codec_dai->driver->ops->hw_free)
819 codec_dai->driver->ops->hw_free(substream, codec_dai);
822 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
823 rtd->dai_link->ops->hw_free(substream);
825 mutex_unlock(&pcm_mutex);
830 * Free's resources allocated by hw_params, can be called multiple times
832 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
834 struct snd_soc_pcm_runtime *rtd = substream->private_data;
835 struct snd_soc_platform *platform = rtd->platform;
836 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
837 struct snd_soc_dai *codec_dai = rtd->codec_dai;
838 struct snd_soc_codec *codec = rtd->codec;
840 mutex_lock(&pcm_mutex);
842 /* apply codec digital mute */
844 snd_soc_dai_digital_mute(codec_dai, 1);
846 /* free any machine hw params */
847 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
848 rtd->dai_link->ops->hw_free(substream);
850 /* free any DMA resources */
851 if (platform->driver->ops->hw_free)
852 platform->driver->ops->hw_free(substream);
854 /* now free hw params for the DAI's */
855 if (codec_dai->driver->ops->hw_free)
856 codec_dai->driver->ops->hw_free(substream, codec_dai);
858 if (cpu_dai->driver->ops->hw_free)
859 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
861 mutex_unlock(&pcm_mutex);
865 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
867 struct snd_soc_pcm_runtime *rtd = substream->private_data;
868 struct snd_soc_platform *platform = rtd->platform;
869 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
870 struct snd_soc_dai *codec_dai = rtd->codec_dai;
873 if (codec_dai->driver->ops->trigger) {
874 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
879 if (platform->driver->ops->trigger) {
880 ret = platform->driver->ops->trigger(substream, cmd);
885 if (cpu_dai->driver->ops->trigger) {
886 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
894 * soc level wrapper for pointer callback
895 * If cpu_dai, codec_dai, platform driver has the delay callback, than
896 * the runtime->delay will be updated accordingly.
898 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
900 struct snd_soc_pcm_runtime *rtd = substream->private_data;
901 struct snd_soc_platform *platform = rtd->platform;
902 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
903 struct snd_soc_dai *codec_dai = rtd->codec_dai;
904 struct snd_pcm_runtime *runtime = substream->runtime;
905 snd_pcm_uframes_t offset = 0;
906 snd_pcm_sframes_t delay = 0;
908 if (platform->driver->ops->pointer)
909 offset = platform->driver->ops->pointer(substream);
911 if (cpu_dai->driver->ops->delay)
912 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
914 if (codec_dai->driver->ops->delay)
915 delay += codec_dai->driver->ops->delay(substream, codec_dai);
917 if (platform->driver->delay)
918 delay += platform->driver->delay(substream, codec_dai);
920 runtime->delay = delay;
925 /* ASoC PCM operations */
926 static struct snd_pcm_ops soc_pcm_ops = {
927 .open = soc_pcm_open,
928 .close = soc_codec_close,
929 .hw_params = soc_pcm_hw_params,
930 .hw_free = soc_pcm_hw_free,
931 .prepare = soc_pcm_prepare,
932 .trigger = soc_pcm_trigger,
933 .pointer = soc_pcm_pointer,
937 /* powers down audio subsystem for suspend */
938 static int soc_suspend(struct device *dev)
940 struct platform_device *pdev = to_platform_device(dev);
941 struct snd_soc_card *card = platform_get_drvdata(pdev);
944 /* If the initialization of this soc device failed, there is no codec
945 * associated with it. Just bail out in this case.
947 if (list_empty(&card->codec_dev_list))
950 /* Due to the resume being scheduled into a workqueue we could
951 * suspend before that's finished - wait for it to complete.
953 snd_power_lock(card->snd_card);
954 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
955 snd_power_unlock(card->snd_card);
957 /* we're going to block userspace touching us until resume completes */
958 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
960 /* mute any active DAC's */
961 for (i = 0; i < card->num_rtd; i++) {
962 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
963 struct snd_soc_dai_driver *drv = dai->driver;
965 if (card->rtd[i].dai_link->ignore_suspend)
968 if (drv->ops->digital_mute && dai->playback_active)
969 drv->ops->digital_mute(dai, 1);
972 /* suspend all pcms */
973 for (i = 0; i < card->num_rtd; i++) {
974 if (card->rtd[i].dai_link->ignore_suspend)
977 snd_pcm_suspend_all(card->rtd[i].pcm);
980 if (card->suspend_pre)
981 card->suspend_pre(pdev, PMSG_SUSPEND);
983 for (i = 0; i < card->num_rtd; i++) {
984 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
985 struct snd_soc_platform *platform = card->rtd[i].platform;
987 if (card->rtd[i].dai_link->ignore_suspend)
990 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
991 cpu_dai->driver->suspend(cpu_dai);
992 if (platform->driver->suspend && !platform->suspended) {
993 platform->driver->suspend(cpu_dai);
994 platform->suspended = 1;
998 /* close any waiting streams and save state */
999 for (i = 0; i < card->num_rtd; i++) {
1000 flush_delayed_work_sync(&card->rtd[i].delayed_work);
1001 card->rtd[i].codec->suspend_bias_level = card->rtd[i].codec->bias_level;
1004 for (i = 0; i < card->num_rtd; i++) {
1005 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1007 if (card->rtd[i].dai_link->ignore_suspend)
1010 if (driver->playback.stream_name != NULL)
1011 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1012 SND_SOC_DAPM_STREAM_SUSPEND);
1014 if (driver->capture.stream_name != NULL)
1015 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1016 SND_SOC_DAPM_STREAM_SUSPEND);
1019 /* suspend all CODECs */
1020 for (i = 0; i < card->num_rtd; i++) {
1021 struct snd_soc_codec *codec = card->rtd[i].codec;
1022 /* If there are paths active then the CODEC will be held with
1023 * bias _ON and should not be suspended. */
1024 if (!codec->suspended && codec->driver->suspend) {
1025 switch (codec->bias_level) {
1026 case SND_SOC_BIAS_STANDBY:
1027 case SND_SOC_BIAS_OFF:
1028 codec->driver->suspend(codec, PMSG_SUSPEND);
1029 codec->suspended = 1;
1032 dev_dbg(codec->dev, "CODEC is on over suspend\n");
1038 for (i = 0; i < card->num_rtd; i++) {
1039 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1041 if (card->rtd[i].dai_link->ignore_suspend)
1044 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1045 cpu_dai->driver->suspend(cpu_dai);
1048 if (card->suspend_post)
1049 card->suspend_post(pdev, PMSG_SUSPEND);
1054 /* deferred resume work, so resume can complete before we finished
1055 * setting our codec back up, which can be very slow on I2C
1057 static void soc_resume_deferred(struct work_struct *work)
1059 struct snd_soc_card *card =
1060 container_of(work, struct snd_soc_card, deferred_resume_work);
1061 struct platform_device *pdev = to_platform_device(card->dev);
1064 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1065 * so userspace apps are blocked from touching us
1068 dev_dbg(card->dev, "starting resume work\n");
1070 /* Bring us up into D2 so that DAPM starts enabling things */
1071 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
1073 if (card->resume_pre)
1074 card->resume_pre(pdev);
1076 /* resume AC97 DAIs */
1077 for (i = 0; i < card->num_rtd; i++) {
1078 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1080 if (card->rtd[i].dai_link->ignore_suspend)
1083 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1084 cpu_dai->driver->resume(cpu_dai);
1087 for (i = 0; i < card->num_rtd; i++) {
1088 struct snd_soc_codec *codec = card->rtd[i].codec;
1089 /* If the CODEC was idle over suspend then it will have been
1090 * left with bias OFF or STANDBY and suspended so we must now
1091 * resume. Otherwise the suspend was suppressed.
1093 if (codec->driver->resume && codec->suspended) {
1094 switch (codec->bias_level) {
1095 case SND_SOC_BIAS_STANDBY:
1096 case SND_SOC_BIAS_OFF:
1097 codec->driver->resume(codec);
1098 codec->suspended = 0;
1101 dev_dbg(codec->dev, "CODEC was on over suspend\n");
1107 for (i = 0; i < card->num_rtd; i++) {
1108 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1110 if (card->rtd[i].dai_link->ignore_suspend)
1113 if (driver->playback.stream_name != NULL)
1114 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1115 SND_SOC_DAPM_STREAM_RESUME);
1117 if (driver->capture.stream_name != NULL)
1118 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1119 SND_SOC_DAPM_STREAM_RESUME);
1122 /* unmute any active DACs */
1123 for (i = 0; i < card->num_rtd; i++) {
1124 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1125 struct snd_soc_dai_driver *drv = dai->driver;
1127 if (card->rtd[i].dai_link->ignore_suspend)
1130 if (drv->ops->digital_mute && dai->playback_active)
1131 drv->ops->digital_mute(dai, 0);
1134 for (i = 0; i < card->num_rtd; i++) {
1135 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1136 struct snd_soc_platform *platform = card->rtd[i].platform;
1138 if (card->rtd[i].dai_link->ignore_suspend)
1141 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1142 cpu_dai->driver->resume(cpu_dai);
1143 if (platform->driver->resume && platform->suspended) {
1144 platform->driver->resume(cpu_dai);
1145 platform->suspended = 0;
1149 if (card->resume_post)
1150 card->resume_post(pdev);
1152 dev_dbg(card->dev, "resume work completed\n");
1154 /* userspace can access us now we are back as we were before */
1155 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
1158 /* powers up audio subsystem after a suspend */
1159 static int soc_resume(struct device *dev)
1161 struct platform_device *pdev = to_platform_device(dev);
1162 struct snd_soc_card *card = platform_get_drvdata(pdev);
1165 /* AC97 devices might have other drivers hanging off them so
1166 * need to resume immediately. Other drivers don't have that
1167 * problem and may take a substantial amount of time to resume
1168 * due to I/O costs and anti-pop so handle them out of line.
1170 for (i = 0; i < card->num_rtd; i++) {
1171 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1172 if (cpu_dai->driver->ac97_control) {
1173 dev_dbg(dev, "Resuming AC97 immediately\n");
1174 soc_resume_deferred(&card->deferred_resume_work);
1176 dev_dbg(dev, "Scheduling resume work\n");
1177 if (!schedule_work(&card->deferred_resume_work))
1178 dev_err(dev, "resume work item may be lost\n");
1185 #define soc_suspend NULL
1186 #define soc_resume NULL
1189 static struct snd_soc_dai_ops null_dai_ops = {
1192 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
1194 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1195 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1196 struct snd_soc_codec *codec;
1197 struct snd_soc_platform *platform;
1198 struct snd_soc_dai *codec_dai, *cpu_dai;
1202 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
1204 /* do we already have the CPU DAI for this link ? */
1208 /* no, then find CPU DAI from registered DAIs*/
1209 list_for_each_entry(cpu_dai, &dai_list, list) {
1210 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1212 if (!try_module_get(cpu_dai->dev->driver->owner))
1215 rtd->cpu_dai = cpu_dai;
1219 dev_dbg(card->dev, "CPU DAI %s not registered\n",
1220 dai_link->cpu_dai_name);
1223 /* do we already have the CODEC for this link ? */
1228 /* no, then find CODEC from registered CODECs*/
1229 list_for_each_entry(codec, &codec_list, list) {
1230 if (!strcmp(codec->name, dai_link->codec_name)) {
1233 if (!try_module_get(codec->dev->driver->owner))
1236 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1237 list_for_each_entry(codec_dai, &dai_list, list) {
1238 if (codec->dev == codec_dai->dev &&
1239 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1240 rtd->codec_dai = codec_dai;
1244 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1245 dai_link->codec_dai_name);
1250 dev_dbg(card->dev, "CODEC %s not registered\n",
1251 dai_link->codec_name);
1254 /* do we already have the CODEC DAI for this link ? */
1255 if (rtd->platform) {
1258 /* no, then find CPU DAI from registered DAIs*/
1259 list_for_each_entry(platform, &platform_list, list) {
1260 if (!strcmp(platform->name, dai_link->platform_name)) {
1262 if (!try_module_get(platform->dev->driver->owner))
1265 rtd->platform = platform;
1270 dev_dbg(card->dev, "platform %s not registered\n",
1271 dai_link->platform_name);
1275 /* mark rtd as complete if we found all 4 of our client devices */
1276 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1283 static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1285 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1286 struct snd_soc_codec *codec = rtd->codec;
1287 struct snd_soc_platform *platform = rtd->platform;
1288 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1291 /* unregister the rtd device */
1292 if (rtd->dev_registered) {
1293 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
1294 device_unregister(&rtd->dev);
1295 rtd->dev_registered = 0;
1298 /* remove the CODEC DAI */
1299 if (codec_dai && codec_dai->probed) {
1300 if (codec_dai->driver->remove) {
1301 err = codec_dai->driver->remove(codec_dai);
1303 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1305 codec_dai->probed = 0;
1306 list_del(&codec_dai->card_list);
1309 /* remove the platform */
1310 if (platform && platform->probed) {
1311 if (platform->driver->remove) {
1312 err = platform->driver->remove(platform);
1314 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1316 platform->probed = 0;
1317 list_del(&platform->card_list);
1318 module_put(platform->dev->driver->owner);
1321 /* remove the CODEC */
1322 if (codec && codec->probed) {
1323 if (codec->driver->remove) {
1324 err = codec->driver->remove(codec);
1326 printk(KERN_ERR "asoc: failed to remove %s\n", codec->name);
1329 /* Make sure all DAPM widgets are freed */
1330 snd_soc_dapm_free(codec);
1332 soc_cleanup_codec_debugfs(codec);
1333 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1335 list_del(&codec->card_list);
1336 module_put(codec->dev->driver->owner);
1339 /* remove the cpu_dai */
1340 if (cpu_dai && cpu_dai->probed) {
1341 if (cpu_dai->driver->remove) {
1342 err = cpu_dai->driver->remove(cpu_dai);
1344 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1346 cpu_dai->probed = 0;
1347 list_del(&cpu_dai->card_list);
1348 module_put(cpu_dai->dev->driver->owner);
1352 static void rtd_release(struct device *dev) {}
1354 static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1356 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1357 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1358 struct snd_soc_codec *codec = rtd->codec;
1359 struct snd_soc_platform *platform = rtd->platform;
1360 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1363 dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1365 /* config components */
1366 codec_dai->codec = codec;
1368 cpu_dai->platform = platform;
1370 rtd->dev.parent = card->dev;
1371 codec_dai->card = card;
1372 cpu_dai->card = card;
1374 /* set default power off timeout */
1375 rtd->pmdown_time = pmdown_time;
1377 /* probe the cpu_dai */
1378 if (!cpu_dai->probed) {
1379 if (cpu_dai->driver->probe) {
1380 ret = cpu_dai->driver->probe(cpu_dai);
1382 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1387 cpu_dai->probed = 1;
1388 /* mark cpu_dai as probed and add to card cpu_dai list */
1389 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1392 /* probe the CODEC */
1393 if (!codec->probed) {
1394 if (codec->driver->probe) {
1395 ret = codec->driver->probe(codec);
1397 printk(KERN_ERR "asoc: failed to probe CODEC %s\n",
1403 soc_init_codec_debugfs(codec);
1405 /* mark codec as probed and add to card codec list */
1407 list_add(&codec->card_list, &card->codec_dev_list);
1410 /* probe the platform */
1411 if (!platform->probed) {
1412 if (platform->driver->probe) {
1413 ret = platform->driver->probe(platform);
1415 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1420 /* mark platform as probed and add to card platform list */
1421 platform->probed = 1;
1422 list_add(&platform->card_list, &card->platform_dev_list);
1425 /* probe the CODEC DAI */
1426 if (!codec_dai->probed) {
1427 if (codec_dai->driver->probe) {
1428 ret = codec_dai->driver->probe(codec_dai);
1430 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1436 /* mark cpu_dai as probed and add to card cpu_dai list */
1437 codec_dai->probed = 1;
1438 list_add(&codec_dai->card_list, &card->dai_dev_list);
1441 /* DAPM dai link stream work */
1442 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1444 /* now that all clients have probed, initialise the DAI link */
1445 if (dai_link->init) {
1446 ret = dai_link->init(rtd);
1448 printk(KERN_ERR "asoc: failed to init %s\n", dai_link->stream_name);
1453 /* Make sure all DAPM widgets are instantiated */
1454 snd_soc_dapm_new_widgets(codec);
1455 snd_soc_dapm_sync(codec);
1457 /* register the rtd device */
1458 rtd->dev.release = rtd_release;
1459 rtd->dev.init_name = dai_link->name;
1460 ret = device_register(&rtd->dev);
1462 printk(KERN_ERR "asoc: failed to register DAI runtime device %d\n", ret);
1466 rtd->dev_registered = 1;
1467 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1469 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1471 /* add DAPM sysfs entries for this codec */
1472 ret = snd_soc_dapm_sys_add(&rtd->dev);
1474 printk(KERN_WARNING "asoc: failed to add codec dapm sysfs entries\n");
1476 /* add codec sysfs entries */
1477 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1479 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1481 /* create the pcm */
1482 ret = soc_new_pcm(rtd, num);
1484 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1488 /* add platform data for AC97 devices */
1489 if (rtd->codec_dai->driver->ac97_control)
1490 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1495 #ifdef CONFIG_SND_SOC_AC97_BUS
1496 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1500 /* Only instantiate AC97 if not already done by the adaptor
1501 * for the generic AC97 subsystem.
1503 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
1505 * It is possible that the AC97 device is already registered to
1506 * the device subsystem. This happens when the device is created
1507 * via snd_ac97_mixer(). Currently only SoC codec that does so
1508 * is the generic AC97 glue but others migh emerge.
1510 * In those cases we don't try to register the device again.
1512 if (!rtd->codec->ac97_created)
1515 ret = soc_ac97_dev_register(rtd->codec);
1517 printk(KERN_ERR "asoc: AC97 device register failed\n");
1521 rtd->codec->ac97_registered = 1;
1526 static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1528 if (codec->ac97_registered) {
1529 soc_ac97_dev_unregister(codec);
1530 codec->ac97_registered = 0;
1535 static void snd_soc_instantiate_card(struct snd_soc_card *card)
1537 struct platform_device *pdev = to_platform_device(card->dev);
1540 mutex_lock(&card->mutex);
1542 if (card->instantiated) {
1543 mutex_unlock(&card->mutex);
1548 for (i = 0; i < card->num_links; i++)
1549 soc_bind_dai_link(card, i);
1551 /* bind completed ? */
1552 if (card->num_rtd != card->num_links) {
1553 mutex_unlock(&card->mutex);
1557 /* card bind complete so register a sound card */
1558 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1559 card->owner, 0, &card->snd_card);
1561 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1563 mutex_unlock(&card->mutex);
1566 card->snd_card->dev = card->dev;
1569 /* deferred resume work */
1570 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1573 /* initialise the sound card only once */
1575 ret = card->probe(pdev);
1577 goto card_probe_error;
1580 for (i = 0; i < card->num_links; i++) {
1581 ret = soc_probe_dai_link(card, i);
1583 pr_err("asoc: failed to instantiate card %s: %d\n",
1589 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1591 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1594 ret = snd_card_register(card->snd_card);
1596 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1600 #ifdef CONFIG_SND_SOC_AC97_BUS
1601 /* register any AC97 codecs */
1602 for (i = 0; i < card->num_rtd; i++) {
1603 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1605 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1607 soc_unregister_ac97_dai_link(&card->rtd[i]);
1613 card->instantiated = 1;
1614 mutex_unlock(&card->mutex);
1618 for (i = 0; i < card->num_links; i++)
1619 soc_remove_dai_link(card, i);
1625 snd_card_free(card->snd_card);
1627 mutex_unlock(&card->mutex);
1631 * Attempt to initialise any uninitialised cards. Must be called with
1634 static void snd_soc_instantiate_cards(void)
1636 struct snd_soc_card *card;
1637 list_for_each_entry(card, &card_list, list)
1638 snd_soc_instantiate_card(card);
1641 /* probes a new socdev */
1642 static int soc_probe(struct platform_device *pdev)
1644 struct snd_soc_card *card = platform_get_drvdata(pdev);
1647 /* Bodge while we unpick instantiation */
1648 card->dev = &pdev->dev;
1649 INIT_LIST_HEAD(&card->dai_dev_list);
1650 INIT_LIST_HEAD(&card->codec_dev_list);
1651 INIT_LIST_HEAD(&card->platform_dev_list);
1653 ret = snd_soc_register_card(card);
1655 dev_err(&pdev->dev, "Failed to register card\n");
1662 /* removes a socdev */
1663 static int soc_remove(struct platform_device *pdev)
1665 struct snd_soc_card *card = platform_get_drvdata(pdev);
1668 if (card->instantiated) {
1670 /* make sure any delayed work runs */
1671 for (i = 0; i < card->num_rtd; i++) {
1672 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1673 flush_delayed_work_sync(&rtd->delayed_work);
1676 /* remove and free each DAI */
1677 for (i = 0; i < card->num_rtd; i++)
1678 soc_remove_dai_link(card, i);
1680 /* remove the card */
1685 snd_card_free(card->snd_card);
1687 snd_soc_unregister_card(card);
1691 static int soc_poweroff(struct device *dev)
1693 struct platform_device *pdev = to_platform_device(dev);
1694 struct snd_soc_card *card = platform_get_drvdata(pdev);
1697 if (!card->instantiated)
1700 /* Flush out pmdown_time work - we actually do want to run it
1701 * now, we're shutting down so no imminent restart. */
1702 for (i = 0; i < card->num_rtd; i++) {
1703 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1704 flush_delayed_work_sync(&rtd->delayed_work);
1707 snd_soc_dapm_shutdown(card);
1712 static const struct dev_pm_ops soc_pm_ops = {
1713 .suspend = soc_suspend,
1714 .resume = soc_resume,
1715 .poweroff = soc_poweroff,
1718 /* ASoC platform driver */
1719 static struct platform_driver soc_driver = {
1721 .name = "soc-audio",
1722 .owner = THIS_MODULE,
1726 .remove = soc_remove,
1729 /* create a new pcm */
1730 static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
1732 struct snd_soc_codec *codec = rtd->codec;
1733 struct snd_soc_platform *platform = rtd->platform;
1734 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1735 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1736 struct snd_pcm *pcm;
1738 int ret = 0, playback = 0, capture = 0;
1740 /* check client and interface hw capabilities */
1741 snprintf(new_name, sizeof(new_name), "%s %s-%d",
1742 rtd->dai_link->stream_name, codec_dai->name, num);
1744 if (codec_dai->driver->playback.channels_min)
1746 if (codec_dai->driver->capture.channels_min)
1749 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
1750 ret = snd_pcm_new(rtd->card->snd_card, new_name,
1751 num, playback, capture, &pcm);
1753 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
1758 pcm->private_data = rtd;
1759 soc_pcm_ops.mmap = platform->driver->ops->mmap;
1760 soc_pcm_ops.pointer = platform->driver->ops->pointer;
1761 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
1762 soc_pcm_ops.copy = platform->driver->ops->copy;
1763 soc_pcm_ops.silence = platform->driver->ops->silence;
1764 soc_pcm_ops.ack = platform->driver->ops->ack;
1765 soc_pcm_ops.page = platform->driver->ops->page;
1768 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1771 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1773 ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
1775 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1779 pcm->private_free = platform->driver->pcm_free;
1780 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1786 * snd_soc_codec_volatile_register: Report if a register is volatile.
1788 * @codec: CODEC to query.
1789 * @reg: Register to query.
1791 * Boolean function indiciating if a CODEC register is volatile.
1793 int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1795 if (codec->driver->volatile_register)
1796 return codec->driver->volatile_register(reg);
1800 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1803 * snd_soc_new_ac97_codec - initailise AC97 device
1804 * @codec: audio codec
1805 * @ops: AC97 bus operations
1806 * @num: AC97 codec number
1808 * Initialises AC97 codec resources for use by ad-hoc devices only.
1810 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1811 struct snd_ac97_bus_ops *ops, int num)
1813 mutex_lock(&codec->mutex);
1815 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1816 if (codec->ac97 == NULL) {
1817 mutex_unlock(&codec->mutex);
1821 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1822 if (codec->ac97->bus == NULL) {
1825 mutex_unlock(&codec->mutex);
1829 codec->ac97->bus->ops = ops;
1830 codec->ac97->num = num;
1833 * Mark the AC97 device to be created by us. This way we ensure that the
1834 * device will be registered with the device subsystem later on.
1836 codec->ac97_created = 1;
1838 mutex_unlock(&codec->mutex);
1841 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1844 * snd_soc_free_ac97_codec - free AC97 codec device
1845 * @codec: audio codec
1847 * Frees AC97 codec device resources.
1849 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1851 mutex_lock(&codec->mutex);
1852 #ifdef CONFIG_SND_SOC_AC97_BUS
1853 soc_unregister_ac97_dai_link(codec);
1855 kfree(codec->ac97->bus);
1858 codec->ac97_created = 0;
1859 mutex_unlock(&codec->mutex);
1861 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1864 * snd_soc_update_bits - update codec register bits
1865 * @codec: audio codec
1866 * @reg: codec register
1867 * @mask: register mask
1870 * Writes new register value.
1872 * Returns 1 for change else 0.
1874 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1875 unsigned int mask, unsigned int value)
1878 unsigned int old, new;
1880 old = snd_soc_read(codec, reg);
1881 new = (old & ~mask) | value;
1882 change = old != new;
1884 snd_soc_write(codec, reg, new);
1888 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1891 * snd_soc_update_bits_locked - update codec register bits
1892 * @codec: audio codec
1893 * @reg: codec register
1894 * @mask: register mask
1897 * Writes new register value, and takes the codec mutex.
1899 * Returns 1 for change else 0.
1901 int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1902 unsigned short reg, unsigned int mask,
1907 mutex_lock(&codec->mutex);
1908 change = snd_soc_update_bits(codec, reg, mask, value);
1909 mutex_unlock(&codec->mutex);
1913 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
1916 * snd_soc_test_bits - test register for change
1917 * @codec: audio codec
1918 * @reg: codec register
1919 * @mask: register mask
1922 * Tests a register with a new value and checks if the new value is
1923 * different from the old value.
1925 * Returns 1 for change else 0.
1927 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1928 unsigned int mask, unsigned int value)
1931 unsigned int old, new;
1933 old = snd_soc_read(codec, reg);
1934 new = (old & ~mask) | value;
1935 change = old != new;
1939 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1942 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1943 * @substream: the pcm substream
1944 * @hw: the hardware parameters
1946 * Sets the substream runtime hardware parameters.
1948 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1949 const struct snd_pcm_hardware *hw)
1951 struct snd_pcm_runtime *runtime = substream->runtime;
1952 runtime->hw.info = hw->info;
1953 runtime->hw.formats = hw->formats;
1954 runtime->hw.period_bytes_min = hw->period_bytes_min;
1955 runtime->hw.period_bytes_max = hw->period_bytes_max;
1956 runtime->hw.periods_min = hw->periods_min;
1957 runtime->hw.periods_max = hw->periods_max;
1958 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1959 runtime->hw.fifo_size = hw->fifo_size;
1962 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1965 * snd_soc_cnew - create new control
1966 * @_template: control template
1967 * @data: control private data
1968 * @long_name: control long name
1970 * Create a new mixer control from a template control.
1972 * Returns 0 for success, else error.
1974 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1975 void *data, char *long_name)
1977 struct snd_kcontrol_new template;
1979 memcpy(&template, _template, sizeof(template));
1981 template.name = long_name;
1984 return snd_ctl_new1(&template, data);
1986 EXPORT_SYMBOL_GPL(snd_soc_cnew);
1989 * snd_soc_add_controls - add an array of controls to a codec.
1990 * Convienience function to add a list of controls. Many codecs were
1991 * duplicating this code.
1993 * @codec: codec to add controls to
1994 * @controls: array of controls to add
1995 * @num_controls: number of elements in the array
1997 * Return 0 for success, else error.
1999 int snd_soc_add_controls(struct snd_soc_codec *codec,
2000 const struct snd_kcontrol_new *controls, int num_controls)
2002 struct snd_card *card = codec->card->snd_card;
2005 for (i = 0; i < num_controls; i++) {
2006 const struct snd_kcontrol_new *control = &controls[i];
2007 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
2009 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
2010 codec->name, control->name, err);
2017 EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2020 * snd_soc_info_enum_double - enumerated double mixer info callback
2021 * @kcontrol: mixer control
2022 * @uinfo: control element information
2024 * Callback to provide information about a double enumerated
2027 * Returns 0 for success.
2029 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2030 struct snd_ctl_elem_info *uinfo)
2032 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2034 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2035 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2036 uinfo->value.enumerated.items = e->max;
2038 if (uinfo->value.enumerated.item > e->max - 1)
2039 uinfo->value.enumerated.item = e->max - 1;
2040 strcpy(uinfo->value.enumerated.name,
2041 e->texts[uinfo->value.enumerated.item]);
2044 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2047 * snd_soc_get_enum_double - enumerated double mixer get callback
2048 * @kcontrol: mixer control
2049 * @ucontrol: control element information
2051 * Callback to get the value of a double enumerated mixer.
2053 * Returns 0 for success.
2055 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2056 struct snd_ctl_elem_value *ucontrol)
2058 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2059 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2060 unsigned int val, bitmask;
2062 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2064 val = snd_soc_read(codec, e->reg);
2065 ucontrol->value.enumerated.item[0]
2066 = (val >> e->shift_l) & (bitmask - 1);
2067 if (e->shift_l != e->shift_r)
2068 ucontrol->value.enumerated.item[1] =
2069 (val >> e->shift_r) & (bitmask - 1);
2073 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2076 * snd_soc_put_enum_double - enumerated double mixer put callback
2077 * @kcontrol: mixer control
2078 * @ucontrol: control element information
2080 * Callback to set the value of a double enumerated mixer.
2082 * Returns 0 for success.
2084 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2085 struct snd_ctl_elem_value *ucontrol)
2087 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2088 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2090 unsigned int mask, bitmask;
2092 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2094 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2096 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2097 mask = (bitmask - 1) << e->shift_l;
2098 if (e->shift_l != e->shift_r) {
2099 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2101 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2102 mask |= (bitmask - 1) << e->shift_r;
2105 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2107 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2110 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2111 * @kcontrol: mixer control
2112 * @ucontrol: control element information
2114 * Callback to get the value of a double semi enumerated mixer.
2116 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2117 * used for handling bitfield coded enumeration for example.
2119 * Returns 0 for success.
2121 int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2122 struct snd_ctl_elem_value *ucontrol)
2124 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2125 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2126 unsigned int reg_val, val, mux;
2128 reg_val = snd_soc_read(codec, e->reg);
2129 val = (reg_val >> e->shift_l) & e->mask;
2130 for (mux = 0; mux < e->max; mux++) {
2131 if (val == e->values[mux])
2134 ucontrol->value.enumerated.item[0] = mux;
2135 if (e->shift_l != e->shift_r) {
2136 val = (reg_val >> e->shift_r) & e->mask;
2137 for (mux = 0; mux < e->max; mux++) {
2138 if (val == e->values[mux])
2141 ucontrol->value.enumerated.item[1] = mux;
2146 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2149 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2150 * @kcontrol: mixer control
2151 * @ucontrol: control element information
2153 * Callback to set the value of a double semi enumerated mixer.
2155 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2156 * used for handling bitfield coded enumeration for example.
2158 * Returns 0 for success.
2160 int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2161 struct snd_ctl_elem_value *ucontrol)
2163 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2164 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2168 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2170 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2171 mask = e->mask << e->shift_l;
2172 if (e->shift_l != e->shift_r) {
2173 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2175 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2176 mask |= e->mask << e->shift_r;
2179 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2181 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2184 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2185 * @kcontrol: mixer control
2186 * @uinfo: control element information
2188 * Callback to provide information about an external enumerated
2191 * Returns 0 for success.
2193 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2194 struct snd_ctl_elem_info *uinfo)
2196 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2198 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2200 uinfo->value.enumerated.items = e->max;
2202 if (uinfo->value.enumerated.item > e->max - 1)
2203 uinfo->value.enumerated.item = e->max - 1;
2204 strcpy(uinfo->value.enumerated.name,
2205 e->texts[uinfo->value.enumerated.item]);
2208 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2211 * snd_soc_info_volsw_ext - external single mixer info callback
2212 * @kcontrol: mixer control
2213 * @uinfo: control element information
2215 * Callback to provide information about a single external mixer control.
2217 * Returns 0 for success.
2219 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2220 struct snd_ctl_elem_info *uinfo)
2222 int max = kcontrol->private_value;
2224 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2225 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2227 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2230 uinfo->value.integer.min = 0;
2231 uinfo->value.integer.max = max;
2234 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2237 * snd_soc_info_volsw - single mixer info callback
2238 * @kcontrol: mixer control
2239 * @uinfo: control element information
2241 * Callback to provide information about a single mixer control.
2243 * Returns 0 for success.
2245 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2246 struct snd_ctl_elem_info *uinfo)
2248 struct soc_mixer_control *mc =
2249 (struct soc_mixer_control *)kcontrol->private_value;
2251 unsigned int shift = mc->shift;
2252 unsigned int rshift = mc->rshift;
2254 if (!mc->platform_max)
2255 mc->platform_max = mc->max;
2256 platform_max = mc->platform_max;
2258 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2259 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2261 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2263 uinfo->count = shift == rshift ? 1 : 2;
2264 uinfo->value.integer.min = 0;
2265 uinfo->value.integer.max = platform_max;
2268 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2271 * snd_soc_get_volsw - single mixer get callback
2272 * @kcontrol: mixer control
2273 * @ucontrol: control element information
2275 * Callback to get the value of a single mixer control.
2277 * Returns 0 for success.
2279 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2280 struct snd_ctl_elem_value *ucontrol)
2282 struct soc_mixer_control *mc =
2283 (struct soc_mixer_control *)kcontrol->private_value;
2284 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2285 unsigned int reg = mc->reg;
2286 unsigned int shift = mc->shift;
2287 unsigned int rshift = mc->rshift;
2289 unsigned int mask = (1 << fls(max)) - 1;
2290 unsigned int invert = mc->invert;
2292 ucontrol->value.integer.value[0] =
2293 (snd_soc_read(codec, reg) >> shift) & mask;
2294 if (shift != rshift)
2295 ucontrol->value.integer.value[1] =
2296 (snd_soc_read(codec, reg) >> rshift) & mask;
2298 ucontrol->value.integer.value[0] =
2299 max - ucontrol->value.integer.value[0];
2300 if (shift != rshift)
2301 ucontrol->value.integer.value[1] =
2302 max - ucontrol->value.integer.value[1];
2307 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2310 * snd_soc_put_volsw - single mixer put callback
2311 * @kcontrol: mixer control
2312 * @ucontrol: control element information
2314 * Callback to set the value of a single mixer control.
2316 * Returns 0 for success.
2318 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2319 struct snd_ctl_elem_value *ucontrol)
2321 struct soc_mixer_control *mc =
2322 (struct soc_mixer_control *)kcontrol->private_value;
2323 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2324 unsigned int reg = mc->reg;
2325 unsigned int shift = mc->shift;
2326 unsigned int rshift = mc->rshift;
2328 unsigned int mask = (1 << fls(max)) - 1;
2329 unsigned int invert = mc->invert;
2330 unsigned int val, val2, val_mask;
2332 val = (ucontrol->value.integer.value[0] & mask);
2335 val_mask = mask << shift;
2337 if (shift != rshift) {
2338 val2 = (ucontrol->value.integer.value[1] & mask);
2341 val_mask |= mask << rshift;
2342 val |= val2 << rshift;
2344 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2346 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2349 * snd_soc_info_volsw_2r - double mixer info callback
2350 * @kcontrol: mixer control
2351 * @uinfo: control element information
2353 * Callback to provide information about a double mixer control that
2354 * spans 2 codec registers.
2356 * Returns 0 for success.
2358 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2359 struct snd_ctl_elem_info *uinfo)
2361 struct soc_mixer_control *mc =
2362 (struct soc_mixer_control *)kcontrol->private_value;
2365 if (!mc->platform_max)
2366 mc->platform_max = mc->max;
2367 platform_max = mc->platform_max;
2369 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2370 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2372 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2375 uinfo->value.integer.min = 0;
2376 uinfo->value.integer.max = platform_max;
2379 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2382 * snd_soc_get_volsw_2r - double mixer get callback
2383 * @kcontrol: mixer control
2384 * @ucontrol: control element information
2386 * Callback to get the value of a double mixer control that spans 2 registers.
2388 * Returns 0 for success.
2390 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2391 struct snd_ctl_elem_value *ucontrol)
2393 struct soc_mixer_control *mc =
2394 (struct soc_mixer_control *)kcontrol->private_value;
2395 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2396 unsigned int reg = mc->reg;
2397 unsigned int reg2 = mc->rreg;
2398 unsigned int shift = mc->shift;
2400 unsigned int mask = (1 << fls(max)) - 1;
2401 unsigned int invert = mc->invert;
2403 ucontrol->value.integer.value[0] =
2404 (snd_soc_read(codec, reg) >> shift) & mask;
2405 ucontrol->value.integer.value[1] =
2406 (snd_soc_read(codec, reg2) >> shift) & mask;
2408 ucontrol->value.integer.value[0] =
2409 max - ucontrol->value.integer.value[0];
2410 ucontrol->value.integer.value[1] =
2411 max - ucontrol->value.integer.value[1];
2416 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2419 * snd_soc_put_volsw_2r - double mixer set callback
2420 * @kcontrol: mixer control
2421 * @ucontrol: control element information
2423 * Callback to set the value of a double mixer control that spans 2 registers.
2425 * Returns 0 for success.
2427 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2428 struct snd_ctl_elem_value *ucontrol)
2430 struct soc_mixer_control *mc =
2431 (struct soc_mixer_control *)kcontrol->private_value;
2432 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2433 unsigned int reg = mc->reg;
2434 unsigned int reg2 = mc->rreg;
2435 unsigned int shift = mc->shift;
2437 unsigned int mask = (1 << fls(max)) - 1;
2438 unsigned int invert = mc->invert;
2440 unsigned int val, val2, val_mask;
2442 val_mask = mask << shift;
2443 val = (ucontrol->value.integer.value[0] & mask);
2444 val2 = (ucontrol->value.integer.value[1] & mask);
2452 val2 = val2 << shift;
2454 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2458 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2461 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2464 * snd_soc_info_volsw_s8 - signed mixer info callback
2465 * @kcontrol: mixer control
2466 * @uinfo: control element information
2468 * Callback to provide information about a signed mixer control.
2470 * Returns 0 for success.
2472 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2473 struct snd_ctl_elem_info *uinfo)
2475 struct soc_mixer_control *mc =
2476 (struct soc_mixer_control *)kcontrol->private_value;
2480 if (!mc->platform_max)
2481 mc->platform_max = mc->max;
2482 platform_max = mc->platform_max;
2484 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2486 uinfo->value.integer.min = 0;
2487 uinfo->value.integer.max = platform_max - min;
2490 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2493 * snd_soc_get_volsw_s8 - signed mixer get callback
2494 * @kcontrol: mixer control
2495 * @ucontrol: control element information
2497 * Callback to get the value of a signed mixer control.
2499 * Returns 0 for success.
2501 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2502 struct snd_ctl_elem_value *ucontrol)
2504 struct soc_mixer_control *mc =
2505 (struct soc_mixer_control *)kcontrol->private_value;
2506 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2507 unsigned int reg = mc->reg;
2509 int val = snd_soc_read(codec, reg);
2511 ucontrol->value.integer.value[0] =
2512 ((signed char)(val & 0xff))-min;
2513 ucontrol->value.integer.value[1] =
2514 ((signed char)((val >> 8) & 0xff))-min;
2517 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2520 * snd_soc_put_volsw_sgn - signed mixer put callback
2521 * @kcontrol: mixer control
2522 * @ucontrol: control element information
2524 * Callback to set the value of a signed mixer control.
2526 * Returns 0 for success.
2528 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2529 struct snd_ctl_elem_value *ucontrol)
2531 struct soc_mixer_control *mc =
2532 (struct soc_mixer_control *)kcontrol->private_value;
2533 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2534 unsigned int reg = mc->reg;
2538 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2539 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2541 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2543 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2546 * snd_soc_limit_volume - Set new limit to an existing volume control.
2548 * @codec: where to look for the control
2549 * @name: Name of the control
2550 * @max: new maximum limit
2552 * Return 0 for success, else error.
2554 int snd_soc_limit_volume(struct snd_soc_codec *codec,
2555 const char *name, int max)
2557 struct snd_card *card = codec->card->snd_card;
2558 struct snd_kcontrol *kctl;
2559 struct soc_mixer_control *mc;
2563 /* Sanity check for name and max */
2564 if (unlikely(!name || max <= 0))
2567 list_for_each_entry(kctl, &card->controls, list) {
2568 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2574 mc = (struct soc_mixer_control *)kctl->private_value;
2575 if (max <= mc->max) {
2576 mc->platform_max = max;
2582 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2585 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2586 * mixer info callback
2587 * @kcontrol: mixer control
2588 * @uinfo: control element information
2590 * Returns 0 for success.
2592 int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2593 struct snd_ctl_elem_info *uinfo)
2595 struct soc_mixer_control *mc =
2596 (struct soc_mixer_control *)kcontrol->private_value;
2600 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2602 uinfo->value.integer.min = 0;
2603 uinfo->value.integer.max = max-min;
2607 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2610 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2611 * mixer get callback
2612 * @kcontrol: mixer control
2613 * @uinfo: control element information
2615 * Returns 0 for success.
2617 int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2618 struct snd_ctl_elem_value *ucontrol)
2620 struct soc_mixer_control *mc =
2621 (struct soc_mixer_control *)kcontrol->private_value;
2622 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2623 unsigned int mask = (1<<mc->shift)-1;
2625 int val = snd_soc_read(codec, mc->reg) & mask;
2626 int valr = snd_soc_read(codec, mc->rreg) & mask;
2628 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2629 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
2632 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2635 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2636 * mixer put callback
2637 * @kcontrol: mixer control
2638 * @uinfo: control element information
2640 * Returns 0 for success.
2642 int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2643 struct snd_ctl_elem_value *ucontrol)
2645 struct soc_mixer_control *mc =
2646 (struct soc_mixer_control *)kcontrol->private_value;
2647 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2648 unsigned int mask = (1<<mc->shift)-1;
2651 unsigned int val, valr, oval, ovalr;
2653 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2655 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2658 oval = snd_soc_read(codec, mc->reg) & mask;
2659 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2663 ret = snd_soc_write(codec, mc->reg, val);
2667 if (ovalr != valr) {
2668 ret = snd_soc_write(codec, mc->rreg, valr);
2675 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2678 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2680 * @clk_id: DAI specific clock ID
2681 * @freq: new clock frequency in Hz
2682 * @dir: new clock direction - input/output.
2684 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2686 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2687 unsigned int freq, int dir)
2689 if (dai->driver && dai->driver->ops->set_sysclk)
2690 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2694 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2697 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2699 * @div_id: DAI specific clock divider ID
2700 * @div: new clock divisor.
2702 * Configures the clock dividers. This is used to derive the best DAI bit and
2703 * frame clocks from the system or master clock. It's best to set the DAI bit
2704 * and frame clocks as low as possible to save system power.
2706 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2707 int div_id, int div)
2709 if (dai->driver && dai->driver->ops->set_clkdiv)
2710 return dai->driver->ops->set_clkdiv(dai, div_id, div);
2714 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2717 * snd_soc_dai_set_pll - configure DAI PLL.
2719 * @pll_id: DAI specific PLL ID
2720 * @source: DAI specific source for the PLL
2721 * @freq_in: PLL input clock frequency in Hz
2722 * @freq_out: requested PLL output clock frequency in Hz
2724 * Configures and enables PLL to generate output clock based on input clock.
2726 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2727 unsigned int freq_in, unsigned int freq_out)
2729 if (dai->driver && dai->driver->ops->set_pll)
2730 return dai->driver->ops->set_pll(dai, pll_id, source,
2735 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2738 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2740 * @fmt: SND_SOC_DAIFMT_ format value.
2742 * Configures the DAI hardware format and clocking.
2744 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2746 if (dai->driver && dai->driver->ops->set_fmt)
2747 return dai->driver->ops->set_fmt(dai, fmt);
2751 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2754 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2756 * @tx_mask: bitmask representing active TX slots.
2757 * @rx_mask: bitmask representing active RX slots.
2758 * @slots: Number of slots in use.
2759 * @slot_width: Width in bits for each slot.
2761 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2764 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2765 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2767 if (dai->driver && dai->driver->ops->set_tdm_slot)
2768 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2773 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2776 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2778 * @tx_num: how many TX channels
2779 * @tx_slot: pointer to an array which imply the TX slot number channel
2781 * @rx_num: how many RX channels
2782 * @rx_slot: pointer to an array which imply the RX slot number channel
2785 * configure the relationship between channel number and TDM slot number.
2787 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2788 unsigned int tx_num, unsigned int *tx_slot,
2789 unsigned int rx_num, unsigned int *rx_slot)
2791 if (dai->driver && dai->driver->ops->set_channel_map)
2792 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2797 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2800 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2802 * @tristate: tristate enable
2804 * Tristates the DAI so that others can use it.
2806 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2808 if (dai->driver && dai->driver->ops->set_tristate)
2809 return dai->driver->ops->set_tristate(dai, tristate);
2813 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2816 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2818 * @mute: mute enable
2820 * Mutes the DAI DAC.
2822 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2824 if (dai->driver && dai->driver->ops->digital_mute)
2825 return dai->driver->ops->digital_mute(dai, mute);
2829 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2832 * snd_soc_register_card - Register a card with the ASoC core
2834 * @card: Card to register
2836 * Note that currently this is an internal only function: it will be
2837 * exposed to machine drivers after further backporting of ASoC v2
2838 * registration APIs.
2840 static int snd_soc_register_card(struct snd_soc_card *card)
2844 if (!card->name || !card->dev)
2847 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) * card->num_links,
2849 if (card->rtd == NULL)
2852 for (i = 0; i < card->num_links; i++)
2853 card->rtd[i].dai_link = &card->dai_link[i];
2855 INIT_LIST_HEAD(&card->list);
2856 card->instantiated = 0;
2857 mutex_init(&card->mutex);
2859 mutex_lock(&client_mutex);
2860 list_add(&card->list, &card_list);
2861 snd_soc_instantiate_cards();
2862 mutex_unlock(&client_mutex);
2864 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2870 * snd_soc_unregister_card - Unregister a card with the ASoC core
2872 * @card: Card to unregister
2874 * Note that currently this is an internal only function: it will be
2875 * exposed to machine drivers after further backporting of ASoC v2
2876 * registration APIs.
2878 static int snd_soc_unregister_card(struct snd_soc_card *card)
2880 mutex_lock(&client_mutex);
2881 list_del(&card->list);
2882 mutex_unlock(&client_mutex);
2883 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2889 * Simplify DAI link configuration by removing ".-1" from device names
2890 * and sanitizing names.
2892 static inline char *fmt_single_name(struct device *dev, int *id)
2894 char *found, name[NAME_SIZE];
2897 if (dev_name(dev) == NULL)
2900 strncpy(name, dev_name(dev), NAME_SIZE);
2902 /* are we a "%s.%d" name (platform and SPI components) */
2903 found = strstr(name, dev->driver->name);
2906 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2908 /* discard ID from name if ID == -1 */
2910 found[strlen(dev->driver->name)] = '\0';
2914 /* I2C component devices are named "bus-addr" */
2915 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2916 char tmp[NAME_SIZE];
2918 /* create unique ID number from I2C addr and bus */
2919 *id = ((id1 & 0xffff) << 16) + id2;
2921 /* sanitize component name for DAI link creation */
2922 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2923 strncpy(name, tmp, NAME_SIZE);
2928 return kstrdup(name, GFP_KERNEL);
2932 * Simplify DAI link naming for single devices with multiple DAIs by removing
2933 * any ".-1" and using the DAI name (instead of device name).
2935 static inline char *fmt_multiple_name(struct device *dev,
2936 struct snd_soc_dai_driver *dai_drv)
2938 if (dai_drv->name == NULL) {
2939 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
2944 return kstrdup(dai_drv->name, GFP_KERNEL);
2948 * snd_soc_register_dai - Register a DAI with the ASoC core
2950 * @dai: DAI to register
2952 int snd_soc_register_dai(struct device *dev,
2953 struct snd_soc_dai_driver *dai_drv)
2955 struct snd_soc_dai *dai;
2957 dev_dbg(dev, "dai register %s\n", dev_name(dev));
2959 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2963 /* create DAI component name */
2964 dai->name = fmt_single_name(dev, &dai->id);
2965 if (dai->name == NULL) {
2971 dai->driver = dai_drv;
2972 if (!dai->driver->ops)
2973 dai->driver->ops = &null_dai_ops;
2975 mutex_lock(&client_mutex);
2976 list_add(&dai->list, &dai_list);
2977 snd_soc_instantiate_cards();
2978 mutex_unlock(&client_mutex);
2980 pr_debug("Registered DAI '%s'\n", dai->name);
2984 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2987 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2989 * @dai: DAI to unregister
2991 void snd_soc_unregister_dai(struct device *dev)
2993 struct snd_soc_dai *dai;
2995 list_for_each_entry(dai, &dai_list, list) {
2996 if (dev == dai->dev)
3002 mutex_lock(&client_mutex);
3003 list_del(&dai->list);
3004 mutex_unlock(&client_mutex);
3006 pr_debug("Unregistered DAI '%s'\n", dai->name);
3010 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3013 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3015 * @dai: Array of DAIs to register
3016 * @count: Number of DAIs
3018 int snd_soc_register_dais(struct device *dev,
3019 struct snd_soc_dai_driver *dai_drv, size_t count)
3021 struct snd_soc_dai *dai;
3024 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
3026 for (i = 0; i < count; i++) {
3028 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3034 /* create DAI component name */
3035 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3036 if (dai->name == NULL) {
3043 dai->driver = &dai_drv[i];
3044 if (dai->driver->id)
3045 dai->id = dai->driver->id;
3048 if (!dai->driver->ops)
3049 dai->driver->ops = &null_dai_ops;
3051 mutex_lock(&client_mutex);
3052 list_add(&dai->list, &dai_list);
3053 mutex_unlock(&client_mutex);
3055 pr_debug("Registered DAI '%s'\n", dai->name);
3058 mutex_lock(&client_mutex);
3059 snd_soc_instantiate_cards();
3060 mutex_unlock(&client_mutex);
3064 for (i--; i >= 0; i--)
3065 snd_soc_unregister_dai(dev);
3069 EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3072 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3074 * @dai: Array of DAIs to unregister
3075 * @count: Number of DAIs
3077 void snd_soc_unregister_dais(struct device *dev, size_t count)
3081 for (i = 0; i < count; i++)
3082 snd_soc_unregister_dai(dev);
3084 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3087 * snd_soc_register_platform - Register a platform with the ASoC core
3089 * @platform: platform to register
3091 int snd_soc_register_platform(struct device *dev,
3092 struct snd_soc_platform_driver *platform_drv)
3094 struct snd_soc_platform *platform;
3096 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3098 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3099 if (platform == NULL)
3102 /* create platform component name */
3103 platform->name = fmt_single_name(dev, &platform->id);
3104 if (platform->name == NULL) {
3109 platform->dev = dev;
3110 platform->driver = platform_drv;
3112 mutex_lock(&client_mutex);
3113 list_add(&platform->list, &platform_list);
3114 snd_soc_instantiate_cards();
3115 mutex_unlock(&client_mutex);
3117 pr_debug("Registered platform '%s'\n", platform->name);
3121 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3124 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3126 * @platform: platform to unregister
3128 void snd_soc_unregister_platform(struct device *dev)
3130 struct snd_soc_platform *platform;
3132 list_for_each_entry(platform, &platform_list, list) {
3133 if (dev == platform->dev)
3139 mutex_lock(&client_mutex);
3140 list_del(&platform->list);
3141 mutex_unlock(&client_mutex);
3143 pr_debug("Unregistered platform '%s'\n", platform->name);
3144 kfree(platform->name);
3147 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3149 static u64 codec_format_map[] = {
3150 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3151 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3152 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3153 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3154 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3155 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3156 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3157 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3158 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3159 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3160 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3161 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3162 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3163 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3164 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3165 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3168 /* Fix up the DAI formats for endianness: codecs don't actually see
3169 * the endianness of the data but we're using the CPU format
3170 * definitions which do need to include endianness so we ensure that
3171 * codec DAIs always have both big and little endian variants set.
3173 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3177 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3178 if (stream->formats & codec_format_map[i])
3179 stream->formats |= codec_format_map[i];
3183 * snd_soc_register_codec - Register a codec with the ASoC core
3185 * @codec: codec to register
3187 int snd_soc_register_codec(struct device *dev,
3188 struct snd_soc_codec_driver *codec_drv,
3189 struct snd_soc_dai_driver *dai_drv, int num_dai)
3191 struct snd_soc_codec *codec;
3194 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3196 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3200 /* create CODEC component name */
3201 codec->name = fmt_single_name(dev, &codec->id);
3202 if (codec->name == NULL) {
3207 /* allocate CODEC register cache */
3208 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3210 if (codec_drv->reg_cache_default)
3211 codec->reg_cache = kmemdup(codec_drv->reg_cache_default,
3212 codec_drv->reg_cache_size * codec_drv->reg_word_size, GFP_KERNEL);
3214 codec->reg_cache = kzalloc(codec_drv->reg_cache_size *
3215 codec_drv->reg_word_size, GFP_KERNEL);
3217 if (codec->reg_cache == NULL) {
3225 codec->driver = codec_drv;
3226 codec->bias_level = SND_SOC_BIAS_OFF;
3227 codec->num_dai = num_dai;
3228 mutex_init(&codec->mutex);
3229 INIT_LIST_HEAD(&codec->dapm_widgets);
3230 INIT_LIST_HEAD(&codec->dapm_paths);
3232 for (i = 0; i < num_dai; i++) {
3233 fixup_codec_formats(&dai_drv[i].playback);
3234 fixup_codec_formats(&dai_drv[i].capture);
3237 /* register any DAIs */
3239 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3244 mutex_lock(&client_mutex);
3245 list_add(&codec->list, &codec_list);
3246 snd_soc_instantiate_cards();
3247 mutex_unlock(&client_mutex);
3249 pr_debug("Registered codec '%s'\n", codec->name);
3253 if (codec->reg_cache)
3254 kfree(codec->reg_cache);
3259 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3262 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3264 * @codec: codec to unregister
3266 void snd_soc_unregister_codec(struct device *dev)
3268 struct snd_soc_codec *codec;
3271 list_for_each_entry(codec, &codec_list, list) {
3272 if (dev == codec->dev)
3279 for (i = 0; i < codec->num_dai; i++)
3280 snd_soc_unregister_dai(dev);
3282 mutex_lock(&client_mutex);
3283 list_del(&codec->list);
3284 mutex_unlock(&client_mutex);
3286 pr_debug("Unregistered codec '%s'\n", codec->name);
3288 if (codec->reg_cache)
3289 kfree(codec->reg_cache);
3293 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3295 static int __init snd_soc_init(void)
3297 #ifdef CONFIG_DEBUG_FS
3298 debugfs_root = debugfs_create_dir("asoc", NULL);
3299 if (IS_ERR(debugfs_root) || !debugfs_root) {
3301 "ASoC: Failed to create debugfs directory\n");
3302 debugfs_root = NULL;
3305 if (!debugfs_create_file("codecs", 0444, debugfs_root, NULL,
3307 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3309 if (!debugfs_create_file("dais", 0444, debugfs_root, NULL,
3311 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
3313 if (!debugfs_create_file("platforms", 0444, debugfs_root, NULL,
3314 &platform_list_fops))
3315 pr_warn("ASoC: Failed to create platform list debugfs file\n");
3318 return platform_driver_register(&soc_driver);
3320 module_init(snd_soc_init);
3322 static void __exit snd_soc_exit(void)
3324 #ifdef CONFIG_DEBUG_FS
3325 debugfs_remove_recursive(debugfs_root);
3327 platform_driver_unregister(&soc_driver);
3329 module_exit(snd_soc_exit);
3331 /* Module information */
3332 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3333 MODULE_DESCRIPTION("ALSA SoC Core");
3334 MODULE_LICENSE("GPL");
3335 MODULE_ALIAS("platform:soc-audio");