2 * soc-core.c -- ALSA SoC Audio Layer
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
8 * with code, comments and ideas from :-
9 * Richard Purdie <richard@openedhand.com>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
17 * o Add hw rules to enforce rates, etc.
18 * o More testing with other codecs/machines.
19 * o Add more codecs and platforms to ensure good API coverage.
20 * o Support TDM on PCM and I2S
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
28 #include <linux/bitops.h>
29 #include <linux/debugfs.h>
30 #include <linux/platform_device.h>
31 #include <sound/ac97_codec.h>
32 #include <sound/core.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/soc.h>
36 #include <sound/soc-dapm.h>
37 #include <sound/initval.h>
39 static DEFINE_MUTEX(pcm_mutex);
40 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
42 #ifdef CONFIG_DEBUG_FS
43 static struct dentry *debugfs_root;
46 static DEFINE_MUTEX(client_mutex);
47 static LIST_HEAD(card_list);
48 static LIST_HEAD(dai_list);
49 static LIST_HEAD(platform_list);
50 static LIST_HEAD(codec_list);
52 static int snd_soc_register_card(struct snd_soc_card *card);
53 static int snd_soc_unregister_card(struct snd_soc_card *card);
56 * This is a timeout to do a DAPM powerdown after a stream is closed().
57 * It can be used to eliminate pops between different playback streams, e.g.
58 * between two audio tracks.
60 static int pmdown_time = 5000;
61 module_param(pmdown_time, int, 0);
62 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
65 * This function forces any delayed work to be queued and run.
67 static int run_delayed_work(struct delayed_work *dwork)
71 /* cancel any work waiting to be queued. */
72 ret = cancel_delayed_work(dwork);
74 /* if there was any work waiting then we run it now and
75 * wait for it's completion */
77 schedule_delayed_work(dwork, 0);
78 flush_scheduled_work();
83 /* codec register dump */
84 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
86 int i, step = 1, count = 0;
88 if (!codec->reg_cache_size)
91 if (codec->reg_cache_step)
92 step = codec->reg_cache_step;
94 count += sprintf(buf, "%s registers\n", codec->name);
95 for (i = 0; i < codec->reg_cache_size; i += step) {
96 if (codec->readable_register && !codec->readable_register(i))
99 count += sprintf(buf + count, "%2x: ", i);
100 if (count >= PAGE_SIZE - 1)
103 if (codec->display_register)
104 count += codec->display_register(codec, buf + count,
105 PAGE_SIZE - count, i);
107 count += snprintf(buf + count, PAGE_SIZE - count,
108 "%4x", codec->read(codec, i));
110 if (count >= PAGE_SIZE - 1)
113 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
114 if (count >= PAGE_SIZE - 1)
118 /* Truncate count; min() would cause a warning */
119 if (count >= PAGE_SIZE)
120 count = PAGE_SIZE - 1;
124 static ssize_t codec_reg_show(struct device *dev,
125 struct device_attribute *attr, char *buf)
127 struct snd_soc_device *devdata = dev_get_drvdata(dev);
128 return soc_codec_reg_show(devdata->card->codec, buf);
131 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
133 static ssize_t pmdown_time_show(struct device *dev,
134 struct device_attribute *attr, char *buf)
136 struct snd_soc_device *socdev = dev_get_drvdata(dev);
137 struct snd_soc_card *card = socdev->card;
139 return sprintf(buf, "%ld\n", card->pmdown_time);
142 static ssize_t pmdown_time_set(struct device *dev,
143 struct device_attribute *attr,
144 const char *buf, size_t count)
146 struct snd_soc_device *socdev = dev_get_drvdata(dev);
147 struct snd_soc_card *card = socdev->card;
149 strict_strtol(buf, 10, &card->pmdown_time);
154 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
156 #ifdef CONFIG_DEBUG_FS
157 static int codec_reg_open_file(struct inode *inode, struct file *file)
159 file->private_data = inode->i_private;
163 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
164 size_t count, loff_t *ppos)
167 struct snd_soc_codec *codec = file->private_data;
168 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
171 ret = soc_codec_reg_show(codec, buf);
173 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
178 static ssize_t codec_reg_write_file(struct file *file,
179 const char __user *user_buf, size_t count, loff_t *ppos)
184 unsigned long reg, value;
186 struct snd_soc_codec *codec = file->private_data;
188 buf_size = min(count, (sizeof(buf)-1));
189 if (copy_from_user(buf, user_buf, buf_size))
193 if (codec->reg_cache_step)
194 step = codec->reg_cache_step;
196 while (*start == ' ')
198 reg = simple_strtoul(start, &start, 16);
199 if ((reg >= codec->reg_cache_size) || (reg % step))
201 while (*start == ' ')
203 if (strict_strtoul(start, 16, &value))
205 codec->write(codec, reg, value);
209 static const struct file_operations codec_reg_fops = {
210 .open = codec_reg_open_file,
211 .read = codec_reg_read_file,
212 .write = codec_reg_write_file,
215 static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
217 char codec_root[128];
220 snprintf(codec_root, sizeof(codec_root),
221 "%s.%s", codec->name, dev_name(codec->dev));
223 snprintf(codec_root, sizeof(codec_root),
226 codec->debugfs_codec_root = debugfs_create_dir(codec_root,
228 if (!codec->debugfs_codec_root) {
230 "ASoC: Failed to create codec debugfs directory\n");
234 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
235 codec->debugfs_codec_root,
236 codec, &codec_reg_fops);
237 if (!codec->debugfs_reg)
239 "ASoC: Failed to create codec register debugfs file\n");
241 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744,
242 codec->debugfs_codec_root,
244 if (!codec->debugfs_pop_time)
246 "Failed to create pop time debugfs file\n");
248 codec->debugfs_dapm = debugfs_create_dir("dapm",
249 codec->debugfs_codec_root);
250 if (!codec->debugfs_dapm)
252 "Failed to create DAPM debugfs directory\n");
254 snd_soc_dapm_debugfs_init(codec);
257 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
259 debugfs_remove_recursive(codec->debugfs_codec_root);
264 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
268 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
273 #ifdef CONFIG_SND_SOC_AC97_BUS
274 /* unregister ac97 codec */
275 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
277 if (codec->ac97->dev.bus)
278 device_unregister(&codec->ac97->dev);
282 /* stop no dev release warning */
283 static void soc_ac97_device_release(struct device *dev){}
285 /* register ac97 codec to bus */
286 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
290 codec->ac97->dev.bus = &ac97_bus_type;
291 codec->ac97->dev.parent = codec->card->dev;
292 codec->ac97->dev.release = soc_ac97_device_release;
294 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
295 codec->card->number, 0, codec->name);
296 err = device_register(&codec->ac97->dev);
298 snd_printk(KERN_ERR "Can't register ac97 bus\n");
299 codec->ac97->dev.bus = NULL;
306 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
308 struct snd_soc_pcm_runtime *rtd = substream->private_data;
309 struct snd_soc_device *socdev = rtd->socdev;
310 struct snd_soc_card *card = socdev->card;
311 struct snd_soc_dai_link *machine = rtd->dai;
312 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
313 struct snd_soc_dai *codec_dai = machine->codec_dai;
316 if (codec_dai->symmetric_rates || cpu_dai->symmetric_rates ||
317 machine->symmetric_rates) {
318 dev_dbg(card->dev, "Symmetry forces %dHz rate\n",
321 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
322 SNDRV_PCM_HW_PARAM_RATE,
327 "Unable to apply rate symmetry constraint: %d\n", ret);
336 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
337 * then initialized and any private data can be allocated. This also calls
338 * startup for the cpu DAI, platform, machine and codec DAI.
340 static int soc_pcm_open(struct snd_pcm_substream *substream)
342 struct snd_soc_pcm_runtime *rtd = substream->private_data;
343 struct snd_soc_device *socdev = rtd->socdev;
344 struct snd_soc_card *card = socdev->card;
345 struct snd_pcm_runtime *runtime = substream->runtime;
346 struct snd_soc_dai_link *machine = rtd->dai;
347 struct snd_soc_platform *platform = card->platform;
348 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
349 struct snd_soc_dai *codec_dai = machine->codec_dai;
352 mutex_lock(&pcm_mutex);
354 /* startup the audio subsystem */
355 if (cpu_dai->ops->startup) {
356 ret = cpu_dai->ops->startup(substream, cpu_dai);
358 printk(KERN_ERR "asoc: can't open interface %s\n",
364 if (platform->pcm_ops->open) {
365 ret = platform->pcm_ops->open(substream);
367 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
372 if (codec_dai->ops->startup) {
373 ret = codec_dai->ops->startup(substream, codec_dai);
375 printk(KERN_ERR "asoc: can't open codec %s\n",
381 if (machine->ops && machine->ops->startup) {
382 ret = machine->ops->startup(substream);
384 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
389 /* Check that the codec and cpu DAI's are compatible */
390 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
391 runtime->hw.rate_min =
392 max(codec_dai->playback.rate_min,
393 cpu_dai->playback.rate_min);
394 runtime->hw.rate_max =
395 min(codec_dai->playback.rate_max,
396 cpu_dai->playback.rate_max);
397 runtime->hw.channels_min =
398 max(codec_dai->playback.channels_min,
399 cpu_dai->playback.channels_min);
400 runtime->hw.channels_max =
401 min(codec_dai->playback.channels_max,
402 cpu_dai->playback.channels_max);
403 runtime->hw.formats =
404 codec_dai->playback.formats & cpu_dai->playback.formats;
406 codec_dai->playback.rates & cpu_dai->playback.rates;
407 if (codec_dai->playback.rates
408 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
409 runtime->hw.rates |= cpu_dai->playback.rates;
410 if (cpu_dai->playback.rates
411 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
412 runtime->hw.rates |= codec_dai->playback.rates;
414 runtime->hw.rate_min =
415 max(codec_dai->capture.rate_min,
416 cpu_dai->capture.rate_min);
417 runtime->hw.rate_max =
418 min(codec_dai->capture.rate_max,
419 cpu_dai->capture.rate_max);
420 runtime->hw.channels_min =
421 max(codec_dai->capture.channels_min,
422 cpu_dai->capture.channels_min);
423 runtime->hw.channels_max =
424 min(codec_dai->capture.channels_max,
425 cpu_dai->capture.channels_max);
426 runtime->hw.formats =
427 codec_dai->capture.formats & cpu_dai->capture.formats;
429 codec_dai->capture.rates & cpu_dai->capture.rates;
430 if (codec_dai->capture.rates
431 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
432 runtime->hw.rates |= cpu_dai->capture.rates;
433 if (cpu_dai->capture.rates
434 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
435 runtime->hw.rates |= codec_dai->capture.rates;
438 snd_pcm_limit_hw_rates(runtime);
439 if (!runtime->hw.rates) {
440 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
441 codec_dai->name, cpu_dai->name);
444 if (!runtime->hw.formats) {
445 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
446 codec_dai->name, cpu_dai->name);
449 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
450 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
451 codec_dai->name, cpu_dai->name);
455 /* Symmetry only applies if we've already got an active stream. */
456 if (cpu_dai->active || codec_dai->active) {
457 ret = soc_pcm_apply_symmetry(substream);
462 pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
463 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
464 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
465 runtime->hw.channels_max);
466 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
467 runtime->hw.rate_max);
469 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
470 cpu_dai->playback.active++;
471 codec_dai->playback.active++;
473 cpu_dai->capture.active++;
474 codec_dai->capture.active++;
478 card->codec->active++;
479 mutex_unlock(&pcm_mutex);
483 if (machine->ops && machine->ops->shutdown)
484 machine->ops->shutdown(substream);
487 if (codec_dai->ops->shutdown)
488 codec_dai->ops->shutdown(substream, codec_dai);
491 if (platform->pcm_ops->close)
492 platform->pcm_ops->close(substream);
495 if (cpu_dai->ops->shutdown)
496 cpu_dai->ops->shutdown(substream, cpu_dai);
498 mutex_unlock(&pcm_mutex);
503 * Power down the audio subsystem pmdown_time msecs after close is called.
504 * This is to ensure there are no pops or clicks in between any music tracks
505 * due to DAPM power cycling.
507 static void close_delayed_work(struct work_struct *work)
509 struct snd_soc_card *card = container_of(work, struct snd_soc_card,
511 struct snd_soc_codec *codec = card->codec;
512 struct snd_soc_dai *codec_dai;
515 mutex_lock(&pcm_mutex);
516 for (i = 0; i < codec->num_dai; i++) {
517 codec_dai = &codec->dai[i];
519 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
520 codec_dai->playback.stream_name,
521 codec_dai->playback.active ? "active" : "inactive",
522 codec_dai->pop_wait ? "yes" : "no");
524 /* are we waiting on this codec DAI stream */
525 if (codec_dai->pop_wait == 1) {
526 codec_dai->pop_wait = 0;
527 snd_soc_dapm_stream_event(codec,
528 codec_dai->playback.stream_name,
529 SND_SOC_DAPM_STREAM_STOP);
532 mutex_unlock(&pcm_mutex);
536 * Called by ALSA when a PCM substream is closed. Private data can be
537 * freed here. The cpu DAI, codec DAI, machine and platform are also
540 static int soc_codec_close(struct snd_pcm_substream *substream)
542 struct snd_soc_pcm_runtime *rtd = substream->private_data;
543 struct snd_soc_device *socdev = rtd->socdev;
544 struct snd_soc_card *card = socdev->card;
545 struct snd_soc_dai_link *machine = rtd->dai;
546 struct snd_soc_platform *platform = card->platform;
547 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
548 struct snd_soc_dai *codec_dai = machine->codec_dai;
549 struct snd_soc_codec *codec = card->codec;
551 mutex_lock(&pcm_mutex);
553 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
554 cpu_dai->playback.active--;
555 codec_dai->playback.active--;
557 cpu_dai->capture.active--;
558 codec_dai->capture.active--;
565 /* Muting the DAC suppresses artifacts caused during digital
566 * shutdown, for example from stopping clocks.
568 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
569 snd_soc_dai_digital_mute(codec_dai, 1);
571 if (cpu_dai->ops->shutdown)
572 cpu_dai->ops->shutdown(substream, cpu_dai);
574 if (codec_dai->ops->shutdown)
575 codec_dai->ops->shutdown(substream, codec_dai);
577 if (machine->ops && machine->ops->shutdown)
578 machine->ops->shutdown(substream);
580 if (platform->pcm_ops->close)
581 platform->pcm_ops->close(substream);
583 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
584 /* start delayed pop wq here for playback streams */
585 codec_dai->pop_wait = 1;
586 schedule_delayed_work(&card->delayed_work,
587 msecs_to_jiffies(card->pmdown_time));
589 /* capture streams can be powered down now */
590 snd_soc_dapm_stream_event(codec,
591 codec_dai->capture.stream_name,
592 SND_SOC_DAPM_STREAM_STOP);
595 mutex_unlock(&pcm_mutex);
600 * Called by ALSA when the PCM substream is prepared, can set format, sample
601 * rate, etc. This function is non atomic and can be called multiple times,
602 * it can refer to the runtime info.
604 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
606 struct snd_soc_pcm_runtime *rtd = substream->private_data;
607 struct snd_soc_device *socdev = rtd->socdev;
608 struct snd_soc_card *card = socdev->card;
609 struct snd_soc_dai_link *machine = rtd->dai;
610 struct snd_soc_platform *platform = card->platform;
611 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
612 struct snd_soc_dai *codec_dai = machine->codec_dai;
613 struct snd_soc_codec *codec = card->codec;
616 mutex_lock(&pcm_mutex);
618 if (machine->ops && machine->ops->prepare) {
619 ret = machine->ops->prepare(substream);
621 printk(KERN_ERR "asoc: machine prepare error\n");
626 if (platform->pcm_ops->prepare) {
627 ret = platform->pcm_ops->prepare(substream);
629 printk(KERN_ERR "asoc: platform prepare error\n");
634 if (codec_dai->ops->prepare) {
635 ret = codec_dai->ops->prepare(substream, codec_dai);
637 printk(KERN_ERR "asoc: codec DAI prepare error\n");
642 if (cpu_dai->ops->prepare) {
643 ret = cpu_dai->ops->prepare(substream, cpu_dai);
645 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
650 /* cancel any delayed stream shutdown that is pending */
651 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
652 codec_dai->pop_wait) {
653 codec_dai->pop_wait = 0;
654 cancel_delayed_work(&card->delayed_work);
657 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
658 snd_soc_dapm_stream_event(codec,
659 codec_dai->playback.stream_name,
660 SND_SOC_DAPM_STREAM_START);
662 snd_soc_dapm_stream_event(codec,
663 codec_dai->capture.stream_name,
664 SND_SOC_DAPM_STREAM_START);
666 snd_soc_dai_digital_mute(codec_dai, 0);
669 mutex_unlock(&pcm_mutex);
674 * Called by ALSA when the hardware params are set by application. This
675 * function can also be called multiple times and can allocate buffers
676 * (using snd_pcm_lib_* ). It's non-atomic.
678 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
679 struct snd_pcm_hw_params *params)
681 struct snd_soc_pcm_runtime *rtd = substream->private_data;
682 struct snd_soc_device *socdev = rtd->socdev;
683 struct snd_soc_dai_link *machine = rtd->dai;
684 struct snd_soc_card *card = socdev->card;
685 struct snd_soc_platform *platform = card->platform;
686 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
687 struct snd_soc_dai *codec_dai = machine->codec_dai;
690 mutex_lock(&pcm_mutex);
692 if (machine->ops && machine->ops->hw_params) {
693 ret = machine->ops->hw_params(substream, params);
695 printk(KERN_ERR "asoc: machine hw_params failed\n");
700 if (codec_dai->ops->hw_params) {
701 ret = codec_dai->ops->hw_params(substream, params, codec_dai);
703 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
709 if (cpu_dai->ops->hw_params) {
710 ret = cpu_dai->ops->hw_params(substream, params, cpu_dai);
712 printk(KERN_ERR "asoc: interface %s hw params failed\n",
718 if (platform->pcm_ops->hw_params) {
719 ret = platform->pcm_ops->hw_params(substream, params);
721 printk(KERN_ERR "asoc: platform %s hw params failed\n",
727 machine->rate = params_rate(params);
730 mutex_unlock(&pcm_mutex);
734 if (cpu_dai->ops->hw_free)
735 cpu_dai->ops->hw_free(substream, cpu_dai);
738 if (codec_dai->ops->hw_free)
739 codec_dai->ops->hw_free(substream, codec_dai);
742 if (machine->ops && machine->ops->hw_free)
743 machine->ops->hw_free(substream);
745 mutex_unlock(&pcm_mutex);
750 * Free's resources allocated by hw_params, can be called multiple times
752 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
754 struct snd_soc_pcm_runtime *rtd = substream->private_data;
755 struct snd_soc_device *socdev = rtd->socdev;
756 struct snd_soc_dai_link *machine = rtd->dai;
757 struct snd_soc_card *card = socdev->card;
758 struct snd_soc_platform *platform = card->platform;
759 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
760 struct snd_soc_dai *codec_dai = machine->codec_dai;
761 struct snd_soc_codec *codec = card->codec;
763 mutex_lock(&pcm_mutex);
765 /* apply codec digital mute */
767 snd_soc_dai_digital_mute(codec_dai, 1);
769 /* free any machine hw params */
770 if (machine->ops && machine->ops->hw_free)
771 machine->ops->hw_free(substream);
773 /* free any DMA resources */
774 if (platform->pcm_ops->hw_free)
775 platform->pcm_ops->hw_free(substream);
777 /* now free hw params for the DAI's */
778 if (codec_dai->ops->hw_free)
779 codec_dai->ops->hw_free(substream, codec_dai);
781 if (cpu_dai->ops->hw_free)
782 cpu_dai->ops->hw_free(substream, cpu_dai);
784 mutex_unlock(&pcm_mutex);
788 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
790 struct snd_soc_pcm_runtime *rtd = substream->private_data;
791 struct snd_soc_device *socdev = rtd->socdev;
792 struct snd_soc_card *card= socdev->card;
793 struct snd_soc_dai_link *machine = rtd->dai;
794 struct snd_soc_platform *platform = card->platform;
795 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
796 struct snd_soc_dai *codec_dai = machine->codec_dai;
799 if (codec_dai->ops->trigger) {
800 ret = codec_dai->ops->trigger(substream, cmd, codec_dai);
805 if (platform->pcm_ops->trigger) {
806 ret = platform->pcm_ops->trigger(substream, cmd);
811 if (cpu_dai->ops->trigger) {
812 ret = cpu_dai->ops->trigger(substream, cmd, cpu_dai);
820 * soc level wrapper for pointer callback
821 * If cpu_dai, codec_dai, platform driver has the delay callback, than
822 * the runtime->delay will be updated accordingly.
824 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
826 struct snd_soc_pcm_runtime *rtd = substream->private_data;
827 struct snd_soc_device *socdev = rtd->socdev;
828 struct snd_soc_card *card = socdev->card;
829 struct snd_soc_platform *platform = card->platform;
830 struct snd_soc_dai_link *machine = rtd->dai;
831 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
832 struct snd_soc_dai *codec_dai = machine->codec_dai;
833 struct snd_pcm_runtime *runtime = substream->runtime;
834 snd_pcm_uframes_t offset = 0;
835 snd_pcm_sframes_t delay = 0;
837 if (platform->pcm_ops->pointer)
838 offset = platform->pcm_ops->pointer(substream);
840 if (cpu_dai->ops->delay)
841 delay += cpu_dai->ops->delay(substream, cpu_dai);
843 if (codec_dai->ops->delay)
844 delay += codec_dai->ops->delay(substream, codec_dai);
847 delay += platform->delay(substream, codec_dai);
849 runtime->delay = delay;
854 /* ASoC PCM operations */
855 static struct snd_pcm_ops soc_pcm_ops = {
856 .open = soc_pcm_open,
857 .close = soc_codec_close,
858 .hw_params = soc_pcm_hw_params,
859 .hw_free = soc_pcm_hw_free,
860 .prepare = soc_pcm_prepare,
861 .trigger = soc_pcm_trigger,
862 .pointer = soc_pcm_pointer,
866 /* powers down audio subsystem for suspend */
867 static int soc_suspend(struct device *dev)
869 struct platform_device *pdev = to_platform_device(dev);
870 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
871 struct snd_soc_card *card = socdev->card;
872 struct snd_soc_platform *platform = card->platform;
873 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
874 struct snd_soc_codec *codec = card->codec;
877 /* If the initialization of this soc device failed, there is no codec
878 * associated with it. Just bail out in this case.
883 /* Due to the resume being scheduled into a workqueue we could
884 * suspend before that's finished - wait for it to complete.
886 snd_power_lock(codec->card);
887 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
888 snd_power_unlock(codec->card);
890 /* we're going to block userspace touching us until resume completes */
891 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
893 /* mute any active DAC's */
894 for (i = 0; i < card->num_links; i++) {
895 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
896 if (dai->ops->digital_mute && dai->playback.active)
897 dai->ops->digital_mute(dai, 1);
900 /* suspend all pcms */
901 for (i = 0; i < card->num_links; i++)
902 snd_pcm_suspend_all(card->dai_link[i].pcm);
904 if (card->suspend_pre)
905 card->suspend_pre(pdev, PMSG_SUSPEND);
907 for (i = 0; i < card->num_links; i++) {
908 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
909 if (cpu_dai->suspend && !cpu_dai->ac97_control)
910 cpu_dai->suspend(cpu_dai);
911 if (platform->suspend)
912 platform->suspend(&card->dai_link[i]);
915 /* close any waiting streams and save state */
916 run_delayed_work(&card->delayed_work);
917 codec->suspend_bias_level = codec->bias_level;
919 for (i = 0; i < codec->num_dai; i++) {
920 char *stream = codec->dai[i].playback.stream_name;
922 snd_soc_dapm_stream_event(codec, stream,
923 SND_SOC_DAPM_STREAM_SUSPEND);
924 stream = codec->dai[i].capture.stream_name;
926 snd_soc_dapm_stream_event(codec, stream,
927 SND_SOC_DAPM_STREAM_SUSPEND);
930 if (codec_dev->suspend)
931 codec_dev->suspend(pdev, PMSG_SUSPEND);
933 for (i = 0; i < card->num_links; i++) {
934 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
935 if (cpu_dai->suspend && cpu_dai->ac97_control)
936 cpu_dai->suspend(cpu_dai);
939 if (card->suspend_post)
940 card->suspend_post(pdev, PMSG_SUSPEND);
945 /* deferred resume work, so resume can complete before we finished
946 * setting our codec back up, which can be very slow on I2C
948 static void soc_resume_deferred(struct work_struct *work)
950 struct snd_soc_card *card = container_of(work,
952 deferred_resume_work);
953 struct snd_soc_device *socdev = card->socdev;
954 struct snd_soc_platform *platform = card->platform;
955 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
956 struct snd_soc_codec *codec = card->codec;
957 struct platform_device *pdev = to_platform_device(socdev->dev);
960 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
961 * so userspace apps are blocked from touching us
964 dev_dbg(socdev->dev, "starting resume work\n");
966 if (card->resume_pre)
967 card->resume_pre(pdev);
969 for (i = 0; i < card->num_links; i++) {
970 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
971 if (cpu_dai->resume && cpu_dai->ac97_control)
972 cpu_dai->resume(cpu_dai);
975 if (codec_dev->resume)
976 codec_dev->resume(pdev);
978 for (i = 0; i < codec->num_dai; i++) {
979 char *stream = codec->dai[i].playback.stream_name;
981 snd_soc_dapm_stream_event(codec, stream,
982 SND_SOC_DAPM_STREAM_RESUME);
983 stream = codec->dai[i].capture.stream_name;
985 snd_soc_dapm_stream_event(codec, stream,
986 SND_SOC_DAPM_STREAM_RESUME);
989 /* unmute any active DACs */
990 for (i = 0; i < card->num_links; i++) {
991 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
992 if (dai->ops->digital_mute && dai->playback.active)
993 dai->ops->digital_mute(dai, 0);
996 for (i = 0; i < card->num_links; i++) {
997 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
998 if (cpu_dai->resume && !cpu_dai->ac97_control)
999 cpu_dai->resume(cpu_dai);
1000 if (platform->resume)
1001 platform->resume(&card->dai_link[i]);
1004 if (card->resume_post)
1005 card->resume_post(pdev);
1007 dev_dbg(socdev->dev, "resume work completed\n");
1009 /* userspace can access us now we are back as we were before */
1010 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
1013 /* powers up audio subsystem after a suspend */
1014 static int soc_resume(struct device *dev)
1016 struct platform_device *pdev = to_platform_device(dev);
1017 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1018 struct snd_soc_card *card = socdev->card;
1019 struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai;
1021 /* If the initialization of this soc device failed, there is no codec
1022 * associated with it. Just bail out in this case.
1027 /* AC97 devices might have other drivers hanging off them so
1028 * need to resume immediately. Other drivers don't have that
1029 * problem and may take a substantial amount of time to resume
1030 * due to I/O costs and anti-pop so handle them out of line.
1032 if (cpu_dai->ac97_control) {
1033 dev_dbg(socdev->dev, "Resuming AC97 immediately\n");
1034 soc_resume_deferred(&card->deferred_resume_work);
1036 dev_dbg(socdev->dev, "Scheduling resume work\n");
1037 if (!schedule_work(&card->deferred_resume_work))
1038 dev_err(socdev->dev, "resume work item may be lost\n");
1044 #define soc_suspend NULL
1045 #define soc_resume NULL
1048 static struct snd_soc_dai_ops null_dai_ops = {
1051 static void snd_soc_instantiate_card(struct snd_soc_card *card)
1053 struct platform_device *pdev = container_of(card->dev,
1054 struct platform_device,
1056 struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
1057 struct snd_soc_codec *codec;
1058 struct snd_soc_platform *platform;
1059 struct snd_soc_dai *dai;
1060 int i, found, ret, ac97;
1062 if (card->instantiated)
1066 list_for_each_entry(platform, &platform_list, list)
1067 if (card->platform == platform) {
1072 dev_dbg(card->dev, "Platform %s not registered\n",
1073 card->platform->name);
1078 for (i = 0; i < card->num_links; i++) {
1080 list_for_each_entry(dai, &dai_list, list)
1081 if (card->dai_link[i].cpu_dai == dai) {
1086 dev_dbg(card->dev, "DAI %s not registered\n",
1087 card->dai_link[i].cpu_dai->name);
1091 if (card->dai_link[i].cpu_dai->ac97_control)
1095 for (i = 0; i < card->num_links; i++) {
1096 if (!card->dai_link[i].codec_dai->ops)
1097 card->dai_link[i].codec_dai->ops = &null_dai_ops;
1100 /* If we have AC97 in the system then don't wait for the
1101 * codec. This will need revisiting if we have to handle
1102 * systems with mixed AC97 and non-AC97 parts. Only check for
1103 * DAIs currently; we can't do this per link since some AC97
1104 * codecs have non-AC97 DAIs.
1107 for (i = 0; i < card->num_links; i++) {
1109 list_for_each_entry(dai, &dai_list, list)
1110 if (card->dai_link[i].codec_dai == dai) {
1115 dev_dbg(card->dev, "DAI %s not registered\n",
1116 card->dai_link[i].codec_dai->name);
1121 /* Note that we do not current check for codec components */
1123 dev_dbg(card->dev, "All components present, instantiating\n");
1125 /* Found everything, bring it up */
1126 card->pmdown_time = pmdown_time;
1129 ret = card->probe(pdev);
1134 for (i = 0; i < card->num_links; i++) {
1135 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1136 if (cpu_dai->probe) {
1137 ret = cpu_dai->probe(pdev, cpu_dai);
1143 if (codec_dev->probe) {
1144 ret = codec_dev->probe(pdev);
1148 codec = card->codec;
1150 if (platform->probe) {
1151 ret = platform->probe(pdev);
1156 /* DAPM stream work */
1157 INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
1159 /* deferred resume work */
1160 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1163 for (i = 0; i < card->num_links; i++) {
1164 if (card->dai_link[i].init) {
1165 ret = card->dai_link[i].init(codec);
1167 printk(KERN_ERR "asoc: failed to init %s\n",
1168 card->dai_link[i].stream_name);
1172 if (card->dai_link[i].codec_dai->ac97_control)
1176 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1178 snprintf(codec->card->longname, sizeof(codec->card->longname),
1179 "%s (%s)", card->name, codec->name);
1181 /* Make sure all DAPM widgets are instantiated */
1182 snd_soc_dapm_new_widgets(codec);
1184 ret = snd_card_register(codec->card);
1186 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1191 mutex_lock(&codec->mutex);
1192 #ifdef CONFIG_SND_SOC_AC97_BUS
1193 /* Only instantiate AC97 if not already done by the adaptor
1194 * for the generic AC97 subsystem.
1196 if (ac97 && strcmp(codec->name, "AC97") != 0) {
1197 ret = soc_ac97_dev_register(codec);
1199 printk(KERN_ERR "asoc: AC97 device register failed\n");
1200 snd_card_free(codec->card);
1201 mutex_unlock(&codec->mutex);
1207 ret = snd_soc_dapm_sys_add(card->socdev->dev);
1209 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1211 ret = device_create_file(card->socdev->dev, &dev_attr_pmdown_time);
1213 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1215 ret = device_create_file(card->socdev->dev, &dev_attr_codec_reg);
1217 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1219 soc_init_codec_debugfs(codec);
1220 mutex_unlock(&codec->mutex);
1222 card->instantiated = 1;
1227 if (platform->remove)
1228 platform->remove(pdev);
1231 if (codec_dev->remove)
1232 codec_dev->remove(pdev);
1235 for (i--; i >= 0; i--) {
1236 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1237 if (cpu_dai->remove)
1238 cpu_dai->remove(pdev, cpu_dai);
1246 * Attempt to initialise any uninitalised cards. Must be called with
1249 static void snd_soc_instantiate_cards(void)
1251 struct snd_soc_card *card;
1252 list_for_each_entry(card, &card_list, list)
1253 snd_soc_instantiate_card(card);
1256 /* probes a new socdev */
1257 static int soc_probe(struct platform_device *pdev)
1260 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1261 struct snd_soc_card *card = socdev->card;
1263 /* Bodge while we push things out of socdev */
1264 card->socdev = socdev;
1266 /* Bodge while we unpick instantiation */
1267 card->dev = &pdev->dev;
1268 ret = snd_soc_register_card(card);
1270 dev_err(&pdev->dev, "Failed to register card\n");
1277 /* removes a socdev */
1278 static int soc_remove(struct platform_device *pdev)
1281 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1282 struct snd_soc_card *card = socdev->card;
1283 struct snd_soc_platform *platform = card->platform;
1284 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1286 if (card->instantiated) {
1287 run_delayed_work(&card->delayed_work);
1289 if (platform->remove)
1290 platform->remove(pdev);
1292 if (codec_dev->remove)
1293 codec_dev->remove(pdev);
1295 for (i = 0; i < card->num_links; i++) {
1296 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1297 if (cpu_dai->remove)
1298 cpu_dai->remove(pdev, cpu_dai);
1305 snd_soc_unregister_card(card);
1310 static int soc_poweroff(struct device *dev)
1312 struct platform_device *pdev = to_platform_device(dev);
1313 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1314 struct snd_soc_card *card = socdev->card;
1316 if (!card->instantiated)
1319 /* Flush out pmdown_time work - we actually do want to run it
1320 * now, we're shutting down so no imminent restart. */
1321 run_delayed_work(&card->delayed_work);
1323 snd_soc_dapm_shutdown(socdev);
1328 static const struct dev_pm_ops soc_pm_ops = {
1329 .suspend = soc_suspend,
1330 .resume = soc_resume,
1331 .poweroff = soc_poweroff,
1334 /* ASoC platform driver */
1335 static struct platform_driver soc_driver = {
1337 .name = "soc-audio",
1338 .owner = THIS_MODULE,
1342 .remove = soc_remove,
1345 /* create a new pcm */
1346 static int soc_new_pcm(struct snd_soc_device *socdev,
1347 struct snd_soc_dai_link *dai_link, int num)
1349 struct snd_soc_card *card = socdev->card;
1350 struct snd_soc_codec *codec = card->codec;
1351 struct snd_soc_platform *platform = card->platform;
1352 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
1353 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
1354 struct snd_soc_pcm_runtime *rtd;
1355 struct snd_pcm *pcm;
1357 int ret = 0, playback = 0, capture = 0;
1359 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1363 rtd->dai = dai_link;
1364 rtd->socdev = socdev;
1365 codec_dai->codec = card->codec;
1367 /* check client and interface hw capabilities */
1368 snprintf(new_name, sizeof(new_name), "%s %s-%d",
1369 dai_link->stream_name, codec_dai->name, num);
1371 if (codec_dai->playback.channels_min)
1373 if (codec_dai->capture.channels_min)
1376 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1379 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
1385 dai_link->pcm = pcm;
1386 pcm->private_data = rtd;
1387 soc_pcm_ops.mmap = platform->pcm_ops->mmap;
1388 soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
1389 soc_pcm_ops.copy = platform->pcm_ops->copy;
1390 soc_pcm_ops.silence = platform->pcm_ops->silence;
1391 soc_pcm_ops.ack = platform->pcm_ops->ack;
1392 soc_pcm_ops.page = platform->pcm_ops->page;
1395 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1398 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1400 ret = platform->pcm_new(codec->card, codec_dai, pcm);
1402 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1407 pcm->private_free = platform->pcm_free;
1408 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1414 * snd_soc_codec_volatile_register: Report if a register is volatile.
1416 * @codec: CODEC to query.
1417 * @reg: Register to query.
1419 * Boolean function indiciating if a CODEC register is volatile.
1421 int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1423 if (codec->volatile_register)
1424 return codec->volatile_register(reg);
1428 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1431 * snd_soc_new_ac97_codec - initailise AC97 device
1432 * @codec: audio codec
1433 * @ops: AC97 bus operations
1434 * @num: AC97 codec number
1436 * Initialises AC97 codec resources for use by ad-hoc devices only.
1438 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1439 struct snd_ac97_bus_ops *ops, int num)
1441 mutex_lock(&codec->mutex);
1443 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1444 if (codec->ac97 == NULL) {
1445 mutex_unlock(&codec->mutex);
1449 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1450 if (codec->ac97->bus == NULL) {
1453 mutex_unlock(&codec->mutex);
1457 codec->ac97->bus->ops = ops;
1458 codec->ac97->num = num;
1459 codec->dev = &codec->ac97->dev;
1460 mutex_unlock(&codec->mutex);
1463 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1466 * snd_soc_free_ac97_codec - free AC97 codec device
1467 * @codec: audio codec
1469 * Frees AC97 codec device resources.
1471 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1473 mutex_lock(&codec->mutex);
1474 kfree(codec->ac97->bus);
1477 mutex_unlock(&codec->mutex);
1479 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1482 * snd_soc_update_bits - update codec register bits
1483 * @codec: audio codec
1484 * @reg: codec register
1485 * @mask: register mask
1488 * Writes new register value.
1490 * Returns 1 for change else 0.
1492 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1493 unsigned int mask, unsigned int value)
1496 unsigned int old, new;
1498 old = snd_soc_read(codec, reg);
1499 new = (old & ~mask) | value;
1500 change = old != new;
1502 snd_soc_write(codec, reg, new);
1506 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1509 * snd_soc_update_bits_locked - update codec register bits
1510 * @codec: audio codec
1511 * @reg: codec register
1512 * @mask: register mask
1515 * Writes new register value, and takes the codec mutex.
1517 * Returns 1 for change else 0.
1519 int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1520 unsigned short reg, unsigned int mask,
1525 mutex_lock(&codec->mutex);
1526 change = snd_soc_update_bits(codec, reg, mask, value);
1527 mutex_unlock(&codec->mutex);
1531 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
1534 * snd_soc_test_bits - test register for change
1535 * @codec: audio codec
1536 * @reg: codec register
1537 * @mask: register mask
1540 * Tests a register with a new value and checks if the new value is
1541 * different from the old value.
1543 * Returns 1 for change else 0.
1545 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1546 unsigned int mask, unsigned int value)
1549 unsigned int old, new;
1551 old = snd_soc_read(codec, reg);
1552 new = (old & ~mask) | value;
1553 change = old != new;
1557 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1560 * snd_soc_new_pcms - create new sound card and pcms
1561 * @socdev: the SoC audio device
1562 * @idx: ALSA card index
1563 * @xid: card identification
1565 * Create a new sound card based upon the codec and interface pcms.
1567 * Returns 0 for success, else error.
1569 int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
1571 struct snd_soc_card *card = socdev->card;
1572 struct snd_soc_codec *codec = card->codec;
1575 mutex_lock(&codec->mutex);
1577 /* register a sound card */
1578 ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
1580 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1582 mutex_unlock(&codec->mutex);
1586 codec->socdev = socdev;
1587 codec->card->dev = socdev->dev;
1588 codec->card->private_data = codec;
1589 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1591 /* create the pcms */
1592 for (i = 0; i < card->num_links; i++) {
1593 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
1595 printk(KERN_ERR "asoc: can't create pcm %s\n",
1596 card->dai_link[i].stream_name);
1597 mutex_unlock(&codec->mutex);
1600 /* Check for codec->ac97 to handle the ac97.c fun */
1601 if (card->dai_link[i].codec_dai->ac97_control && codec->ac97) {
1602 snd_ac97_dev_add_pdata(codec->ac97,
1603 card->dai_link[i].cpu_dai->ac97_pdata);
1607 mutex_unlock(&codec->mutex);
1610 EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1613 * snd_soc_free_pcms - free sound card and pcms
1614 * @socdev: the SoC audio device
1616 * Frees sound card and pcms associated with the socdev.
1617 * Also unregister the codec if it is an AC97 device.
1619 void snd_soc_free_pcms(struct snd_soc_device *socdev)
1621 struct snd_soc_codec *codec = socdev->card->codec;
1622 #ifdef CONFIG_SND_SOC_AC97_BUS
1623 struct snd_soc_dai *codec_dai;
1627 mutex_lock(&codec->mutex);
1628 soc_cleanup_codec_debugfs(codec);
1629 #ifdef CONFIG_SND_SOC_AC97_BUS
1630 for (i = 0; i < codec->num_dai; i++) {
1631 codec_dai = &codec->dai[i];
1632 if (codec_dai->ac97_control && codec->ac97 &&
1633 strcmp(codec->name, "AC97") != 0) {
1634 soc_ac97_dev_unregister(codec);
1642 snd_card_free(codec->card);
1643 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1644 mutex_unlock(&codec->mutex);
1646 EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1649 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1650 * @substream: the pcm substream
1651 * @hw: the hardware parameters
1653 * Sets the substream runtime hardware parameters.
1655 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1656 const struct snd_pcm_hardware *hw)
1658 struct snd_pcm_runtime *runtime = substream->runtime;
1659 runtime->hw.info = hw->info;
1660 runtime->hw.formats = hw->formats;
1661 runtime->hw.period_bytes_min = hw->period_bytes_min;
1662 runtime->hw.period_bytes_max = hw->period_bytes_max;
1663 runtime->hw.periods_min = hw->periods_min;
1664 runtime->hw.periods_max = hw->periods_max;
1665 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1666 runtime->hw.fifo_size = hw->fifo_size;
1669 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1672 * snd_soc_cnew - create new control
1673 * @_template: control template
1674 * @data: control private data
1675 * @long_name: control long name
1677 * Create a new mixer control from a template control.
1679 * Returns 0 for success, else error.
1681 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1682 void *data, char *long_name)
1684 struct snd_kcontrol_new template;
1686 memcpy(&template, _template, sizeof(template));
1688 template.name = long_name;
1691 return snd_ctl_new1(&template, data);
1693 EXPORT_SYMBOL_GPL(snd_soc_cnew);
1696 * snd_soc_add_controls - add an array of controls to a codec.
1697 * Convienience function to add a list of controls. Many codecs were
1698 * duplicating this code.
1700 * @codec: codec to add controls to
1701 * @controls: array of controls to add
1702 * @num_controls: number of elements in the array
1704 * Return 0 for success, else error.
1706 int snd_soc_add_controls(struct snd_soc_codec *codec,
1707 const struct snd_kcontrol_new *controls, int num_controls)
1709 struct snd_card *card = codec->card;
1712 for (i = 0; i < num_controls; i++) {
1713 const struct snd_kcontrol_new *control = &controls[i];
1714 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
1716 dev_err(codec->dev, "%s: Failed to add %s\n",
1717 codec->name, control->name);
1724 EXPORT_SYMBOL_GPL(snd_soc_add_controls);
1727 * snd_soc_info_enum_double - enumerated double mixer info callback
1728 * @kcontrol: mixer control
1729 * @uinfo: control element information
1731 * Callback to provide information about a double enumerated
1734 * Returns 0 for success.
1736 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1737 struct snd_ctl_elem_info *uinfo)
1739 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1741 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1742 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1743 uinfo->value.enumerated.items = e->max;
1745 if (uinfo->value.enumerated.item > e->max - 1)
1746 uinfo->value.enumerated.item = e->max - 1;
1747 strcpy(uinfo->value.enumerated.name,
1748 e->texts[uinfo->value.enumerated.item]);
1751 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1754 * snd_soc_get_enum_double - enumerated double mixer get callback
1755 * @kcontrol: mixer control
1756 * @ucontrol: control element information
1758 * Callback to get the value of a double enumerated mixer.
1760 * Returns 0 for success.
1762 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1763 struct snd_ctl_elem_value *ucontrol)
1765 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1766 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1767 unsigned int val, bitmask;
1769 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1771 val = snd_soc_read(codec, e->reg);
1772 ucontrol->value.enumerated.item[0]
1773 = (val >> e->shift_l) & (bitmask - 1);
1774 if (e->shift_l != e->shift_r)
1775 ucontrol->value.enumerated.item[1] =
1776 (val >> e->shift_r) & (bitmask - 1);
1780 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1783 * snd_soc_put_enum_double - enumerated double mixer put callback
1784 * @kcontrol: mixer control
1785 * @ucontrol: control element information
1787 * Callback to set the value of a double enumerated mixer.
1789 * Returns 0 for success.
1791 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1792 struct snd_ctl_elem_value *ucontrol)
1794 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1795 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1797 unsigned int mask, bitmask;
1799 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1801 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1803 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1804 mask = (bitmask - 1) << e->shift_l;
1805 if (e->shift_l != e->shift_r) {
1806 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1808 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1809 mask |= (bitmask - 1) << e->shift_r;
1812 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
1814 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1817 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
1818 * @kcontrol: mixer control
1819 * @ucontrol: control element information
1821 * Callback to get the value of a double semi enumerated mixer.
1823 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1824 * used for handling bitfield coded enumeration for example.
1826 * Returns 0 for success.
1828 int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
1829 struct snd_ctl_elem_value *ucontrol)
1831 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1832 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1833 unsigned int reg_val, val, mux;
1835 reg_val = snd_soc_read(codec, e->reg);
1836 val = (reg_val >> e->shift_l) & e->mask;
1837 for (mux = 0; mux < e->max; mux++) {
1838 if (val == e->values[mux])
1841 ucontrol->value.enumerated.item[0] = mux;
1842 if (e->shift_l != e->shift_r) {
1843 val = (reg_val >> e->shift_r) & e->mask;
1844 for (mux = 0; mux < e->max; mux++) {
1845 if (val == e->values[mux])
1848 ucontrol->value.enumerated.item[1] = mux;
1853 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
1856 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
1857 * @kcontrol: mixer control
1858 * @ucontrol: control element information
1860 * Callback to set the value of a double semi enumerated mixer.
1862 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1863 * used for handling bitfield coded enumeration for example.
1865 * Returns 0 for success.
1867 int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
1868 struct snd_ctl_elem_value *ucontrol)
1870 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1871 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1875 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1877 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1878 mask = e->mask << e->shift_l;
1879 if (e->shift_l != e->shift_r) {
1880 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1882 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1883 mask |= e->mask << e->shift_r;
1886 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
1888 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
1891 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1892 * @kcontrol: mixer control
1893 * @uinfo: control element information
1895 * Callback to provide information about an external enumerated
1898 * Returns 0 for success.
1900 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1901 struct snd_ctl_elem_info *uinfo)
1903 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1905 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1907 uinfo->value.enumerated.items = e->max;
1909 if (uinfo->value.enumerated.item > e->max - 1)
1910 uinfo->value.enumerated.item = e->max - 1;
1911 strcpy(uinfo->value.enumerated.name,
1912 e->texts[uinfo->value.enumerated.item]);
1915 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1918 * snd_soc_info_volsw_ext - external single mixer info callback
1919 * @kcontrol: mixer control
1920 * @uinfo: control element information
1922 * Callback to provide information about a single external mixer control.
1924 * Returns 0 for success.
1926 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1927 struct snd_ctl_elem_info *uinfo)
1929 int max = kcontrol->private_value;
1931 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
1932 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1934 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1937 uinfo->value.integer.min = 0;
1938 uinfo->value.integer.max = max;
1941 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1944 * snd_soc_info_volsw - single mixer info callback
1945 * @kcontrol: mixer control
1946 * @uinfo: control element information
1948 * Callback to provide information about a single mixer control.
1950 * Returns 0 for success.
1952 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1953 struct snd_ctl_elem_info *uinfo)
1955 struct soc_mixer_control *mc =
1956 (struct soc_mixer_control *)kcontrol->private_value;
1958 unsigned int shift = mc->shift;
1959 unsigned int rshift = mc->rshift;
1961 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
1962 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1964 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1966 uinfo->count = shift == rshift ? 1 : 2;
1967 uinfo->value.integer.min = 0;
1968 uinfo->value.integer.max = max;
1971 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1974 * snd_soc_get_volsw - single mixer get callback
1975 * @kcontrol: mixer control
1976 * @ucontrol: control element information
1978 * Callback to get the value of a single mixer control.
1980 * Returns 0 for success.
1982 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1983 struct snd_ctl_elem_value *ucontrol)
1985 struct soc_mixer_control *mc =
1986 (struct soc_mixer_control *)kcontrol->private_value;
1987 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1988 unsigned int reg = mc->reg;
1989 unsigned int shift = mc->shift;
1990 unsigned int rshift = mc->rshift;
1992 unsigned int mask = (1 << fls(max)) - 1;
1993 unsigned int invert = mc->invert;
1995 ucontrol->value.integer.value[0] =
1996 (snd_soc_read(codec, reg) >> shift) & mask;
1997 if (shift != rshift)
1998 ucontrol->value.integer.value[1] =
1999 (snd_soc_read(codec, reg) >> rshift) & mask;
2001 ucontrol->value.integer.value[0] =
2002 max - ucontrol->value.integer.value[0];
2003 if (shift != rshift)
2004 ucontrol->value.integer.value[1] =
2005 max - ucontrol->value.integer.value[1];
2010 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2013 * snd_soc_put_volsw - single mixer put callback
2014 * @kcontrol: mixer control
2015 * @ucontrol: control element information
2017 * Callback to set the value of a single mixer control.
2019 * Returns 0 for success.
2021 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2022 struct snd_ctl_elem_value *ucontrol)
2024 struct soc_mixer_control *mc =
2025 (struct soc_mixer_control *)kcontrol->private_value;
2026 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2027 unsigned int reg = mc->reg;
2028 unsigned int shift = mc->shift;
2029 unsigned int rshift = mc->rshift;
2031 unsigned int mask = (1 << fls(max)) - 1;
2032 unsigned int invert = mc->invert;
2033 unsigned int val, val2, val_mask;
2035 val = (ucontrol->value.integer.value[0] & mask);
2038 val_mask = mask << shift;
2040 if (shift != rshift) {
2041 val2 = (ucontrol->value.integer.value[1] & mask);
2044 val_mask |= mask << rshift;
2045 val |= val2 << rshift;
2047 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2049 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2052 * snd_soc_info_volsw_2r - double mixer info callback
2053 * @kcontrol: mixer control
2054 * @uinfo: control element information
2056 * Callback to provide information about a double mixer control that
2057 * spans 2 codec registers.
2059 * Returns 0 for success.
2061 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2062 struct snd_ctl_elem_info *uinfo)
2064 struct soc_mixer_control *mc =
2065 (struct soc_mixer_control *)kcontrol->private_value;
2068 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2069 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2071 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2074 uinfo->value.integer.min = 0;
2075 uinfo->value.integer.max = max;
2078 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2081 * snd_soc_get_volsw_2r - double mixer get callback
2082 * @kcontrol: mixer control
2083 * @ucontrol: control element information
2085 * Callback to get the value of a double mixer control that spans 2 registers.
2087 * Returns 0 for success.
2089 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2090 struct snd_ctl_elem_value *ucontrol)
2092 struct soc_mixer_control *mc =
2093 (struct soc_mixer_control *)kcontrol->private_value;
2094 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2095 unsigned int reg = mc->reg;
2096 unsigned int reg2 = mc->rreg;
2097 unsigned int shift = mc->shift;
2099 unsigned int mask = (1 << fls(max)) - 1;
2100 unsigned int invert = mc->invert;
2102 ucontrol->value.integer.value[0] =
2103 (snd_soc_read(codec, reg) >> shift) & mask;
2104 ucontrol->value.integer.value[1] =
2105 (snd_soc_read(codec, reg2) >> shift) & mask;
2107 ucontrol->value.integer.value[0] =
2108 max - ucontrol->value.integer.value[0];
2109 ucontrol->value.integer.value[1] =
2110 max - ucontrol->value.integer.value[1];
2115 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2118 * snd_soc_put_volsw_2r - double mixer set callback
2119 * @kcontrol: mixer control
2120 * @ucontrol: control element information
2122 * Callback to set the value of a double mixer control that spans 2 registers.
2124 * Returns 0 for success.
2126 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2127 struct snd_ctl_elem_value *ucontrol)
2129 struct soc_mixer_control *mc =
2130 (struct soc_mixer_control *)kcontrol->private_value;
2131 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2132 unsigned int reg = mc->reg;
2133 unsigned int reg2 = mc->rreg;
2134 unsigned int shift = mc->shift;
2136 unsigned int mask = (1 << fls(max)) - 1;
2137 unsigned int invert = mc->invert;
2139 unsigned int val, val2, val_mask;
2141 val_mask = mask << shift;
2142 val = (ucontrol->value.integer.value[0] & mask);
2143 val2 = (ucontrol->value.integer.value[1] & mask);
2151 val2 = val2 << shift;
2153 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2157 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2160 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2163 * snd_soc_info_volsw_s8 - signed mixer info callback
2164 * @kcontrol: mixer control
2165 * @uinfo: control element information
2167 * Callback to provide information about a signed mixer control.
2169 * Returns 0 for success.
2171 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2172 struct snd_ctl_elem_info *uinfo)
2174 struct soc_mixer_control *mc =
2175 (struct soc_mixer_control *)kcontrol->private_value;
2179 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2181 uinfo->value.integer.min = 0;
2182 uinfo->value.integer.max = max-min;
2185 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2188 * snd_soc_get_volsw_s8 - signed mixer get callback
2189 * @kcontrol: mixer control
2190 * @ucontrol: control element information
2192 * Callback to get the value of a signed mixer control.
2194 * Returns 0 for success.
2196 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2197 struct snd_ctl_elem_value *ucontrol)
2199 struct soc_mixer_control *mc =
2200 (struct soc_mixer_control *)kcontrol->private_value;
2201 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2202 unsigned int reg = mc->reg;
2204 int val = snd_soc_read(codec, reg);
2206 ucontrol->value.integer.value[0] =
2207 ((signed char)(val & 0xff))-min;
2208 ucontrol->value.integer.value[1] =
2209 ((signed char)((val >> 8) & 0xff))-min;
2212 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2215 * snd_soc_put_volsw_sgn - signed mixer put callback
2216 * @kcontrol: mixer control
2217 * @ucontrol: control element information
2219 * Callback to set the value of a signed mixer control.
2221 * Returns 0 for success.
2223 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2224 struct snd_ctl_elem_value *ucontrol)
2226 struct soc_mixer_control *mc =
2227 (struct soc_mixer_control *)kcontrol->private_value;
2228 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2229 unsigned int reg = mc->reg;
2233 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2234 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2236 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2238 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2241 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2243 * @clk_id: DAI specific clock ID
2244 * @freq: new clock frequency in Hz
2245 * @dir: new clock direction - input/output.
2247 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2249 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2250 unsigned int freq, int dir)
2252 if (dai->ops && dai->ops->set_sysclk)
2253 return dai->ops->set_sysclk(dai, clk_id, freq, dir);
2257 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2260 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2262 * @div_id: DAI specific clock divider ID
2263 * @div: new clock divisor.
2265 * Configures the clock dividers. This is used to derive the best DAI bit and
2266 * frame clocks from the system or master clock. It's best to set the DAI bit
2267 * and frame clocks as low as possible to save system power.
2269 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2270 int div_id, int div)
2272 if (dai->ops && dai->ops->set_clkdiv)
2273 return dai->ops->set_clkdiv(dai, div_id, div);
2277 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2280 * snd_soc_dai_set_pll - configure DAI PLL.
2282 * @pll_id: DAI specific PLL ID
2283 * @source: DAI specific source for the PLL
2284 * @freq_in: PLL input clock frequency in Hz
2285 * @freq_out: requested PLL output clock frequency in Hz
2287 * Configures and enables PLL to generate output clock based on input clock.
2289 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2290 unsigned int freq_in, unsigned int freq_out)
2292 if (dai->ops && dai->ops->set_pll)
2293 return dai->ops->set_pll(dai, pll_id, source,
2298 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2301 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2303 * @fmt: SND_SOC_DAIFMT_ format value.
2305 * Configures the DAI hardware format and clocking.
2307 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2309 if (dai->ops && dai->ops->set_fmt)
2310 return dai->ops->set_fmt(dai, fmt);
2314 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2317 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2319 * @tx_mask: bitmask representing active TX slots.
2320 * @rx_mask: bitmask representing active RX slots.
2321 * @slots: Number of slots in use.
2322 * @slot_width: Width in bits for each slot.
2324 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2327 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2328 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2330 if (dai->ops && dai->ops->set_tdm_slot)
2331 return dai->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2336 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2339 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2341 * @tx_num: how many TX channels
2342 * @tx_slot: pointer to an array which imply the TX slot number channel
2344 * @rx_num: how many RX channels
2345 * @rx_slot: pointer to an array which imply the RX slot number channel
2348 * configure the relationship between channel number and TDM slot number.
2350 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2351 unsigned int tx_num, unsigned int *tx_slot,
2352 unsigned int rx_num, unsigned int *rx_slot)
2354 if (dai->ops && dai->ops->set_channel_map)
2355 return dai->ops->set_channel_map(dai, tx_num, tx_slot,
2360 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2363 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2365 * @tristate: tristate enable
2367 * Tristates the DAI so that others can use it.
2369 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2371 if (dai->ops && dai->ops->set_tristate)
2372 return dai->ops->set_tristate(dai, tristate);
2376 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2379 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2381 * @mute: mute enable
2383 * Mutes the DAI DAC.
2385 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2387 if (dai->ops && dai->ops->digital_mute)
2388 return dai->ops->digital_mute(dai, mute);
2392 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2395 * snd_soc_register_card - Register a card with the ASoC core
2397 * @card: Card to register
2399 * Note that currently this is an internal only function: it will be
2400 * exposed to machine drivers after further backporting of ASoC v2
2401 * registration APIs.
2403 static int snd_soc_register_card(struct snd_soc_card *card)
2405 if (!card->name || !card->dev)
2408 INIT_LIST_HEAD(&card->list);
2409 card->instantiated = 0;
2411 mutex_lock(&client_mutex);
2412 list_add(&card->list, &card_list);
2413 snd_soc_instantiate_cards();
2414 mutex_unlock(&client_mutex);
2416 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2422 * snd_soc_unregister_card - Unregister a card with the ASoC core
2424 * @card: Card to unregister
2426 * Note that currently this is an internal only function: it will be
2427 * exposed to machine drivers after further backporting of ASoC v2
2428 * registration APIs.
2430 static int snd_soc_unregister_card(struct snd_soc_card *card)
2432 mutex_lock(&client_mutex);
2433 list_del(&card->list);
2434 mutex_unlock(&client_mutex);
2436 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2442 * snd_soc_register_dai - Register a DAI with the ASoC core
2444 * @dai: DAI to register
2446 int snd_soc_register_dai(struct snd_soc_dai *dai)
2451 /* The device should become mandatory over time */
2453 printk(KERN_WARNING "No device for DAI %s\n", dai->name);
2456 dai->ops = &null_dai_ops;
2458 INIT_LIST_HEAD(&dai->list);
2460 mutex_lock(&client_mutex);
2461 list_add(&dai->list, &dai_list);
2462 snd_soc_instantiate_cards();
2463 mutex_unlock(&client_mutex);
2465 pr_debug("Registered DAI '%s'\n", dai->name);
2469 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2472 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2474 * @dai: DAI to unregister
2476 void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2478 mutex_lock(&client_mutex);
2479 list_del(&dai->list);
2480 mutex_unlock(&client_mutex);
2482 pr_debug("Unregistered DAI '%s'\n", dai->name);
2484 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2487 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2489 * @dai: Array of DAIs to register
2490 * @count: Number of DAIs
2492 int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
2496 for (i = 0; i < count; i++) {
2497 ret = snd_soc_register_dai(&dai[i]);
2505 for (i--; i >= 0; i--)
2506 snd_soc_unregister_dai(&dai[i]);
2510 EXPORT_SYMBOL_GPL(snd_soc_register_dais);
2513 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
2515 * @dai: Array of DAIs to unregister
2516 * @count: Number of DAIs
2518 void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
2522 for (i = 0; i < count; i++)
2523 snd_soc_unregister_dai(&dai[i]);
2525 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
2528 * snd_soc_register_platform - Register a platform with the ASoC core
2530 * @platform: platform to register
2532 int snd_soc_register_platform(struct snd_soc_platform *platform)
2534 if (!platform->name)
2537 INIT_LIST_HEAD(&platform->list);
2539 mutex_lock(&client_mutex);
2540 list_add(&platform->list, &platform_list);
2541 snd_soc_instantiate_cards();
2542 mutex_unlock(&client_mutex);
2544 pr_debug("Registered platform '%s'\n", platform->name);
2548 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2551 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2553 * @platform: platform to unregister
2555 void snd_soc_unregister_platform(struct snd_soc_platform *platform)
2557 mutex_lock(&client_mutex);
2558 list_del(&platform->list);
2559 mutex_unlock(&client_mutex);
2561 pr_debug("Unregistered platform '%s'\n", platform->name);
2563 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2565 static u64 codec_format_map[] = {
2566 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2567 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2568 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2569 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2570 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2571 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2572 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2573 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2574 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2575 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2576 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2577 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2578 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2579 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2580 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2581 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2584 /* Fix up the DAI formats for endianness: codecs don't actually see
2585 * the endianness of the data but we're using the CPU format
2586 * definitions which do need to include endianness so we ensure that
2587 * codec DAIs always have both big and little endian variants set.
2589 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2593 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2594 if (stream->formats & codec_format_map[i])
2595 stream->formats |= codec_format_map[i];
2599 * snd_soc_register_codec - Register a codec with the ASoC core
2601 * @codec: codec to register
2603 int snd_soc_register_codec(struct snd_soc_codec *codec)
2610 /* The device should become mandatory over time */
2612 printk(KERN_WARNING "No device for codec %s\n", codec->name);
2614 INIT_LIST_HEAD(&codec->list);
2616 for (i = 0; i < codec->num_dai; i++) {
2617 fixup_codec_formats(&codec->dai[i].playback);
2618 fixup_codec_formats(&codec->dai[i].capture);
2621 mutex_lock(&client_mutex);
2622 list_add(&codec->list, &codec_list);
2623 snd_soc_instantiate_cards();
2624 mutex_unlock(&client_mutex);
2626 pr_debug("Registered codec '%s'\n", codec->name);
2630 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
2633 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
2635 * @codec: codec to unregister
2637 void snd_soc_unregister_codec(struct snd_soc_codec *codec)
2639 mutex_lock(&client_mutex);
2640 list_del(&codec->list);
2641 mutex_unlock(&client_mutex);
2643 pr_debug("Unregistered codec '%s'\n", codec->name);
2645 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
2647 static int __init snd_soc_init(void)
2649 #ifdef CONFIG_DEBUG_FS
2650 debugfs_root = debugfs_create_dir("asoc", NULL);
2651 if (IS_ERR(debugfs_root) || !debugfs_root) {
2653 "ASoC: Failed to create debugfs directory\n");
2654 debugfs_root = NULL;
2658 return platform_driver_register(&soc_driver);
2661 static void __exit snd_soc_exit(void)
2663 #ifdef CONFIG_DEBUG_FS
2664 debugfs_remove_recursive(debugfs_root);
2666 platform_driver_unregister(&soc_driver);
2669 module_init(snd_soc_init);
2670 module_exit(snd_soc_exit);
2672 /* Module information */
2673 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2674 MODULE_DESCRIPTION("ALSA SoC Core");
2675 MODULE_LICENSE("GPL");
2676 MODULE_ALIAS("platform:soc-audio");