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;
408 runtime->hw.rate_min =
409 max(codec_dai->capture.rate_min,
410 cpu_dai->capture.rate_min);
411 runtime->hw.rate_max =
412 min(codec_dai->capture.rate_max,
413 cpu_dai->capture.rate_max);
414 runtime->hw.channels_min =
415 max(codec_dai->capture.channels_min,
416 cpu_dai->capture.channels_min);
417 runtime->hw.channels_max =
418 min(codec_dai->capture.channels_max,
419 cpu_dai->capture.channels_max);
420 runtime->hw.formats =
421 codec_dai->capture.formats & cpu_dai->capture.formats;
423 codec_dai->capture.rates & cpu_dai->capture.rates;
426 snd_pcm_limit_hw_rates(runtime);
427 if (!runtime->hw.rates) {
428 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
429 codec_dai->name, cpu_dai->name);
432 if (!runtime->hw.formats) {
433 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
434 codec_dai->name, cpu_dai->name);
437 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
438 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
439 codec_dai->name, cpu_dai->name);
443 /* Symmetry only applies if we've already got an active stream. */
444 if (cpu_dai->active || codec_dai->active) {
445 ret = soc_pcm_apply_symmetry(substream);
450 pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
451 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
452 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
453 runtime->hw.channels_max);
454 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
455 runtime->hw.rate_max);
457 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
458 cpu_dai->playback.active = codec_dai->playback.active = 1;
460 cpu_dai->capture.active = codec_dai->capture.active = 1;
461 cpu_dai->active = codec_dai->active = 1;
462 cpu_dai->runtime = runtime;
463 card->codec->active++;
464 mutex_unlock(&pcm_mutex);
468 if (machine->ops && machine->ops->shutdown)
469 machine->ops->shutdown(substream);
472 if (codec_dai->ops->shutdown)
473 codec_dai->ops->shutdown(substream, codec_dai);
476 if (platform->pcm_ops->close)
477 platform->pcm_ops->close(substream);
480 if (cpu_dai->ops->shutdown)
481 cpu_dai->ops->shutdown(substream, cpu_dai);
483 mutex_unlock(&pcm_mutex);
488 * Power down the audio subsystem pmdown_time msecs after close is called.
489 * This is to ensure there are no pops or clicks in between any music tracks
490 * due to DAPM power cycling.
492 static void close_delayed_work(struct work_struct *work)
494 struct snd_soc_card *card = container_of(work, struct snd_soc_card,
496 struct snd_soc_codec *codec = card->codec;
497 struct snd_soc_dai *codec_dai;
500 mutex_lock(&pcm_mutex);
501 for (i = 0; i < codec->num_dai; i++) {
502 codec_dai = &codec->dai[i];
504 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
505 codec_dai->playback.stream_name,
506 codec_dai->playback.active ? "active" : "inactive",
507 codec_dai->pop_wait ? "yes" : "no");
509 /* are we waiting on this codec DAI stream */
510 if (codec_dai->pop_wait == 1) {
511 codec_dai->pop_wait = 0;
512 snd_soc_dapm_stream_event(codec,
513 codec_dai->playback.stream_name,
514 SND_SOC_DAPM_STREAM_STOP);
517 mutex_unlock(&pcm_mutex);
521 * Called by ALSA when a PCM substream is closed. Private data can be
522 * freed here. The cpu DAI, codec DAI, machine and platform are also
525 static int soc_codec_close(struct snd_pcm_substream *substream)
527 struct snd_soc_pcm_runtime *rtd = substream->private_data;
528 struct snd_soc_device *socdev = rtd->socdev;
529 struct snd_soc_card *card = socdev->card;
530 struct snd_soc_dai_link *machine = rtd->dai;
531 struct snd_soc_platform *platform = card->platform;
532 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
533 struct snd_soc_dai *codec_dai = machine->codec_dai;
534 struct snd_soc_codec *codec = card->codec;
536 mutex_lock(&pcm_mutex);
538 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
539 cpu_dai->playback.active = codec_dai->playback.active = 0;
541 cpu_dai->capture.active = codec_dai->capture.active = 0;
543 if (codec_dai->playback.active == 0 &&
544 codec_dai->capture.active == 0) {
545 cpu_dai->active = codec_dai->active = 0;
549 /* Muting the DAC suppresses artifacts caused during digital
550 * shutdown, for example from stopping clocks.
552 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
553 snd_soc_dai_digital_mute(codec_dai, 1);
555 if (cpu_dai->ops->shutdown)
556 cpu_dai->ops->shutdown(substream, cpu_dai);
558 if (codec_dai->ops->shutdown)
559 codec_dai->ops->shutdown(substream, codec_dai);
561 if (machine->ops && machine->ops->shutdown)
562 machine->ops->shutdown(substream);
564 if (platform->pcm_ops->close)
565 platform->pcm_ops->close(substream);
566 cpu_dai->runtime = NULL;
568 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
569 /* start delayed pop wq here for playback streams */
570 codec_dai->pop_wait = 1;
571 schedule_delayed_work(&card->delayed_work,
572 msecs_to_jiffies(card->pmdown_time));
574 /* capture streams can be powered down now */
575 snd_soc_dapm_stream_event(codec,
576 codec_dai->capture.stream_name,
577 SND_SOC_DAPM_STREAM_STOP);
580 mutex_unlock(&pcm_mutex);
585 * Called by ALSA when the PCM substream is prepared, can set format, sample
586 * rate, etc. This function is non atomic and can be called multiple times,
587 * it can refer to the runtime info.
589 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
591 struct snd_soc_pcm_runtime *rtd = substream->private_data;
592 struct snd_soc_device *socdev = rtd->socdev;
593 struct snd_soc_card *card = socdev->card;
594 struct snd_soc_dai_link *machine = rtd->dai;
595 struct snd_soc_platform *platform = card->platform;
596 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
597 struct snd_soc_dai *codec_dai = machine->codec_dai;
598 struct snd_soc_codec *codec = card->codec;
601 mutex_lock(&pcm_mutex);
603 if (machine->ops && machine->ops->prepare) {
604 ret = machine->ops->prepare(substream);
606 printk(KERN_ERR "asoc: machine prepare error\n");
611 if (platform->pcm_ops->prepare) {
612 ret = platform->pcm_ops->prepare(substream);
614 printk(KERN_ERR "asoc: platform prepare error\n");
619 if (codec_dai->ops->prepare) {
620 ret = codec_dai->ops->prepare(substream, codec_dai);
622 printk(KERN_ERR "asoc: codec DAI prepare error\n");
627 if (cpu_dai->ops->prepare) {
628 ret = cpu_dai->ops->prepare(substream, cpu_dai);
630 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
635 /* cancel any delayed stream shutdown that is pending */
636 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
637 codec_dai->pop_wait) {
638 codec_dai->pop_wait = 0;
639 cancel_delayed_work(&card->delayed_work);
642 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
643 snd_soc_dapm_stream_event(codec,
644 codec_dai->playback.stream_name,
645 SND_SOC_DAPM_STREAM_START);
647 snd_soc_dapm_stream_event(codec,
648 codec_dai->capture.stream_name,
649 SND_SOC_DAPM_STREAM_START);
651 snd_soc_dai_digital_mute(codec_dai, 0);
654 mutex_unlock(&pcm_mutex);
659 * Called by ALSA when the hardware params are set by application. This
660 * function can also be called multiple times and can allocate buffers
661 * (using snd_pcm_lib_* ). It's non-atomic.
663 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
664 struct snd_pcm_hw_params *params)
666 struct snd_soc_pcm_runtime *rtd = substream->private_data;
667 struct snd_soc_device *socdev = rtd->socdev;
668 struct snd_soc_dai_link *machine = rtd->dai;
669 struct snd_soc_card *card = socdev->card;
670 struct snd_soc_platform *platform = card->platform;
671 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
672 struct snd_soc_dai *codec_dai = machine->codec_dai;
675 mutex_lock(&pcm_mutex);
677 if (machine->ops && machine->ops->hw_params) {
678 ret = machine->ops->hw_params(substream, params);
680 printk(KERN_ERR "asoc: machine hw_params failed\n");
685 if (codec_dai->ops->hw_params) {
686 ret = codec_dai->ops->hw_params(substream, params, codec_dai);
688 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
694 if (cpu_dai->ops->hw_params) {
695 ret = cpu_dai->ops->hw_params(substream, params, cpu_dai);
697 printk(KERN_ERR "asoc: interface %s hw params failed\n",
703 if (platform->pcm_ops->hw_params) {
704 ret = platform->pcm_ops->hw_params(substream, params);
706 printk(KERN_ERR "asoc: platform %s hw params failed\n",
712 machine->rate = params_rate(params);
715 mutex_unlock(&pcm_mutex);
719 if (cpu_dai->ops->hw_free)
720 cpu_dai->ops->hw_free(substream, cpu_dai);
723 if (codec_dai->ops->hw_free)
724 codec_dai->ops->hw_free(substream, codec_dai);
727 if (machine->ops && machine->ops->hw_free)
728 machine->ops->hw_free(substream);
730 mutex_unlock(&pcm_mutex);
735 * Free's resources allocated by hw_params, can be called multiple times
737 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
739 struct snd_soc_pcm_runtime *rtd = substream->private_data;
740 struct snd_soc_device *socdev = rtd->socdev;
741 struct snd_soc_dai_link *machine = rtd->dai;
742 struct snd_soc_card *card = socdev->card;
743 struct snd_soc_platform *platform = card->platform;
744 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
745 struct snd_soc_dai *codec_dai = machine->codec_dai;
746 struct snd_soc_codec *codec = card->codec;
748 mutex_lock(&pcm_mutex);
750 /* apply codec digital mute */
752 snd_soc_dai_digital_mute(codec_dai, 1);
754 /* free any machine hw params */
755 if (machine->ops && machine->ops->hw_free)
756 machine->ops->hw_free(substream);
758 /* free any DMA resources */
759 if (platform->pcm_ops->hw_free)
760 platform->pcm_ops->hw_free(substream);
762 /* now free hw params for the DAI's */
763 if (codec_dai->ops->hw_free)
764 codec_dai->ops->hw_free(substream, codec_dai);
766 if (cpu_dai->ops->hw_free)
767 cpu_dai->ops->hw_free(substream, cpu_dai);
769 mutex_unlock(&pcm_mutex);
773 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
775 struct snd_soc_pcm_runtime *rtd = substream->private_data;
776 struct snd_soc_device *socdev = rtd->socdev;
777 struct snd_soc_card *card= socdev->card;
778 struct snd_soc_dai_link *machine = rtd->dai;
779 struct snd_soc_platform *platform = card->platform;
780 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
781 struct snd_soc_dai *codec_dai = machine->codec_dai;
784 if (codec_dai->ops->trigger) {
785 ret = codec_dai->ops->trigger(substream, cmd, codec_dai);
790 if (platform->pcm_ops->trigger) {
791 ret = platform->pcm_ops->trigger(substream, cmd);
796 if (cpu_dai->ops->trigger) {
797 ret = cpu_dai->ops->trigger(substream, cmd, cpu_dai);
804 /* ASoC PCM operations */
805 static struct snd_pcm_ops soc_pcm_ops = {
806 .open = soc_pcm_open,
807 .close = soc_codec_close,
808 .hw_params = soc_pcm_hw_params,
809 .hw_free = soc_pcm_hw_free,
810 .prepare = soc_pcm_prepare,
811 .trigger = soc_pcm_trigger,
815 /* powers down audio subsystem for suspend */
816 static int soc_suspend(struct device *dev)
818 struct platform_device *pdev = to_platform_device(dev);
819 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
820 struct snd_soc_card *card = socdev->card;
821 struct snd_soc_platform *platform = card->platform;
822 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
823 struct snd_soc_codec *codec = card->codec;
826 /* If the initialization of this soc device failed, there is no codec
827 * associated with it. Just bail out in this case.
832 /* Due to the resume being scheduled into a workqueue we could
833 * suspend before that's finished - wait for it to complete.
835 snd_power_lock(codec->card);
836 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
837 snd_power_unlock(codec->card);
839 /* we're going to block userspace touching us until resume completes */
840 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
842 /* mute any active DAC's */
843 for (i = 0; i < card->num_links; i++) {
844 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
845 if (dai->ops->digital_mute && dai->playback.active)
846 dai->ops->digital_mute(dai, 1);
849 /* suspend all pcms */
850 for (i = 0; i < card->num_links; i++)
851 snd_pcm_suspend_all(card->dai_link[i].pcm);
853 if (card->suspend_pre)
854 card->suspend_pre(pdev, PMSG_SUSPEND);
856 for (i = 0; i < card->num_links; i++) {
857 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
858 if (cpu_dai->suspend && !cpu_dai->ac97_control)
859 cpu_dai->suspend(cpu_dai);
860 if (platform->suspend)
861 platform->suspend(cpu_dai);
864 /* close any waiting streams and save state */
865 run_delayed_work(&card->delayed_work);
866 codec->suspend_bias_level = codec->bias_level;
868 for (i = 0; i < codec->num_dai; i++) {
869 char *stream = codec->dai[i].playback.stream_name;
871 snd_soc_dapm_stream_event(codec, stream,
872 SND_SOC_DAPM_STREAM_SUSPEND);
873 stream = codec->dai[i].capture.stream_name;
875 snd_soc_dapm_stream_event(codec, stream,
876 SND_SOC_DAPM_STREAM_SUSPEND);
879 if (codec_dev->suspend)
880 codec_dev->suspend(pdev, PMSG_SUSPEND);
882 for (i = 0; i < card->num_links; i++) {
883 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
884 if (cpu_dai->suspend && cpu_dai->ac97_control)
885 cpu_dai->suspend(cpu_dai);
888 if (card->suspend_post)
889 card->suspend_post(pdev, PMSG_SUSPEND);
894 /* deferred resume work, so resume can complete before we finished
895 * setting our codec back up, which can be very slow on I2C
897 static void soc_resume_deferred(struct work_struct *work)
899 struct snd_soc_card *card = container_of(work,
901 deferred_resume_work);
902 struct snd_soc_device *socdev = card->socdev;
903 struct snd_soc_platform *platform = card->platform;
904 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
905 struct snd_soc_codec *codec = card->codec;
906 struct platform_device *pdev = to_platform_device(socdev->dev);
909 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
910 * so userspace apps are blocked from touching us
913 dev_dbg(socdev->dev, "starting resume work\n");
915 if (card->resume_pre)
916 card->resume_pre(pdev);
918 for (i = 0; i < card->num_links; i++) {
919 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
920 if (cpu_dai->resume && cpu_dai->ac97_control)
921 cpu_dai->resume(cpu_dai);
924 if (codec_dev->resume)
925 codec_dev->resume(pdev);
927 for (i = 0; i < codec->num_dai; i++) {
928 char *stream = codec->dai[i].playback.stream_name;
930 snd_soc_dapm_stream_event(codec, stream,
931 SND_SOC_DAPM_STREAM_RESUME);
932 stream = codec->dai[i].capture.stream_name;
934 snd_soc_dapm_stream_event(codec, stream,
935 SND_SOC_DAPM_STREAM_RESUME);
938 /* unmute any active DACs */
939 for (i = 0; i < card->num_links; i++) {
940 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
941 if (dai->ops->digital_mute && dai->playback.active)
942 dai->ops->digital_mute(dai, 0);
945 for (i = 0; i < card->num_links; i++) {
946 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
947 if (cpu_dai->resume && !cpu_dai->ac97_control)
948 cpu_dai->resume(cpu_dai);
949 if (platform->resume)
950 platform->resume(cpu_dai);
953 if (card->resume_post)
954 card->resume_post(pdev);
956 dev_dbg(socdev->dev, "resume work completed\n");
958 /* userspace can access us now we are back as we were before */
959 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
962 /* powers up audio subsystem after a suspend */
963 static int soc_resume(struct device *dev)
965 struct platform_device *pdev = to_platform_device(dev);
966 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
967 struct snd_soc_card *card = socdev->card;
968 struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai;
970 /* If the initialization of this soc device failed, there is no codec
971 * associated with it. Just bail out in this case.
976 /* AC97 devices might have other drivers hanging off them so
977 * need to resume immediately. Other drivers don't have that
978 * problem and may take a substantial amount of time to resume
979 * due to I/O costs and anti-pop so handle them out of line.
981 if (cpu_dai->ac97_control) {
982 dev_dbg(socdev->dev, "Resuming AC97 immediately\n");
983 soc_resume_deferred(&card->deferred_resume_work);
985 dev_dbg(socdev->dev, "Scheduling resume work\n");
986 if (!schedule_work(&card->deferred_resume_work))
987 dev_err(socdev->dev, "resume work item may be lost\n");
993 #define soc_suspend NULL
994 #define soc_resume NULL
997 static struct snd_soc_dai_ops null_dai_ops = {
1000 static void snd_soc_instantiate_card(struct snd_soc_card *card)
1002 struct platform_device *pdev = container_of(card->dev,
1003 struct platform_device,
1005 struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
1006 struct snd_soc_codec *codec;
1007 struct snd_soc_platform *platform;
1008 struct snd_soc_dai *dai;
1009 int i, found, ret, ac97;
1011 if (card->instantiated)
1015 list_for_each_entry(platform, &platform_list, list)
1016 if (card->platform == platform) {
1021 dev_dbg(card->dev, "Platform %s not registered\n",
1022 card->platform->name);
1027 for (i = 0; i < card->num_links; i++) {
1029 list_for_each_entry(dai, &dai_list, list)
1030 if (card->dai_link[i].cpu_dai == dai) {
1035 dev_dbg(card->dev, "DAI %s not registered\n",
1036 card->dai_link[i].cpu_dai->name);
1040 if (card->dai_link[i].cpu_dai->ac97_control)
1044 for (i = 0; i < card->num_links; i++) {
1045 if (!card->dai_link[i].codec_dai->ops)
1046 card->dai_link[i].codec_dai->ops = &null_dai_ops;
1049 /* If we have AC97 in the system then don't wait for the
1050 * codec. This will need revisiting if we have to handle
1051 * systems with mixed AC97 and non-AC97 parts. Only check for
1052 * DAIs currently; we can't do this per link since some AC97
1053 * codecs have non-AC97 DAIs.
1056 for (i = 0; i < card->num_links; i++) {
1058 list_for_each_entry(dai, &dai_list, list)
1059 if (card->dai_link[i].codec_dai == dai) {
1064 dev_dbg(card->dev, "DAI %s not registered\n",
1065 card->dai_link[i].codec_dai->name);
1070 /* Note that we do not current check for codec components */
1072 dev_dbg(card->dev, "All components present, instantiating\n");
1074 /* Found everything, bring it up */
1075 card->pmdown_time = pmdown_time;
1078 ret = card->probe(pdev);
1083 for (i = 0; i < card->num_links; i++) {
1084 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1085 if (cpu_dai->probe) {
1086 ret = cpu_dai->probe(pdev, cpu_dai);
1092 if (codec_dev->probe) {
1093 ret = codec_dev->probe(pdev);
1097 codec = card->codec;
1099 if (platform->probe) {
1100 ret = platform->probe(pdev);
1105 /* DAPM stream work */
1106 INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
1108 /* deferred resume work */
1109 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1112 for (i = 0; i < card->num_links; i++) {
1113 if (card->dai_link[i].init) {
1114 ret = card->dai_link[i].init(codec);
1116 printk(KERN_ERR "asoc: failed to init %s\n",
1117 card->dai_link[i].stream_name);
1121 if (card->dai_link[i].codec_dai->ac97_control)
1125 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1127 snprintf(codec->card->longname, sizeof(codec->card->longname),
1128 "%s (%s)", card->name, codec->name);
1130 /* Make sure all DAPM widgets are instantiated */
1131 snd_soc_dapm_new_widgets(codec);
1133 ret = snd_card_register(codec->card);
1135 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1140 mutex_lock(&codec->mutex);
1141 #ifdef CONFIG_SND_SOC_AC97_BUS
1142 /* Only instantiate AC97 if not already done by the adaptor
1143 * for the generic AC97 subsystem.
1145 if (ac97 && strcmp(codec->name, "AC97") != 0) {
1146 ret = soc_ac97_dev_register(codec);
1148 printk(KERN_ERR "asoc: AC97 device register failed\n");
1149 snd_card_free(codec->card);
1150 mutex_unlock(&codec->mutex);
1156 ret = snd_soc_dapm_sys_add(card->socdev->dev);
1158 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1160 ret = device_create_file(card->socdev->dev, &dev_attr_pmdown_time);
1162 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1164 ret = device_create_file(card->socdev->dev, &dev_attr_codec_reg);
1166 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1168 soc_init_codec_debugfs(codec);
1169 mutex_unlock(&codec->mutex);
1171 card->instantiated = 1;
1176 if (platform->remove)
1177 platform->remove(pdev);
1180 if (codec_dev->remove)
1181 codec_dev->remove(pdev);
1184 for (i--; i >= 0; i--) {
1185 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1186 if (cpu_dai->remove)
1187 cpu_dai->remove(pdev, cpu_dai);
1195 * Attempt to initialise any uninitalised cards. Must be called with
1198 static void snd_soc_instantiate_cards(void)
1200 struct snd_soc_card *card;
1201 list_for_each_entry(card, &card_list, list)
1202 snd_soc_instantiate_card(card);
1205 /* probes a new socdev */
1206 static int soc_probe(struct platform_device *pdev)
1209 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1210 struct snd_soc_card *card = socdev->card;
1212 /* Bodge while we push things out of socdev */
1213 card->socdev = socdev;
1215 /* Bodge while we unpick instantiation */
1216 card->dev = &pdev->dev;
1217 ret = snd_soc_register_card(card);
1219 dev_err(&pdev->dev, "Failed to register card\n");
1226 /* removes a socdev */
1227 static int soc_remove(struct platform_device *pdev)
1230 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1231 struct snd_soc_card *card = socdev->card;
1232 struct snd_soc_platform *platform = card->platform;
1233 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1235 if (!card->instantiated)
1238 run_delayed_work(&card->delayed_work);
1240 if (platform->remove)
1241 platform->remove(pdev);
1243 if (codec_dev->remove)
1244 codec_dev->remove(pdev);
1246 for (i = 0; i < card->num_links; i++) {
1247 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1248 if (cpu_dai->remove)
1249 cpu_dai->remove(pdev, cpu_dai);
1255 snd_soc_unregister_card(card);
1260 static int soc_poweroff(struct device *dev)
1262 struct platform_device *pdev = to_platform_device(dev);
1263 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1264 struct snd_soc_card *card = socdev->card;
1266 if (!card->instantiated)
1269 /* Flush out pmdown_time work - we actually do want to run it
1270 * now, we're shutting down so no imminent restart. */
1271 run_delayed_work(&card->delayed_work);
1273 snd_soc_dapm_shutdown(socdev);
1278 static const struct dev_pm_ops soc_pm_ops = {
1279 .suspend = soc_suspend,
1280 .resume = soc_resume,
1281 .poweroff = soc_poweroff,
1284 /* ASoC platform driver */
1285 static struct platform_driver soc_driver = {
1287 .name = "soc-audio",
1288 .owner = THIS_MODULE,
1292 .remove = soc_remove,
1295 /* create a new pcm */
1296 static int soc_new_pcm(struct snd_soc_device *socdev,
1297 struct snd_soc_dai_link *dai_link, int num)
1299 struct snd_soc_card *card = socdev->card;
1300 struct snd_soc_codec *codec = card->codec;
1301 struct snd_soc_platform *platform = card->platform;
1302 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
1303 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
1304 struct snd_soc_pcm_runtime *rtd;
1305 struct snd_pcm *pcm;
1307 int ret = 0, playback = 0, capture = 0;
1309 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1313 rtd->dai = dai_link;
1314 rtd->socdev = socdev;
1315 codec_dai->codec = card->codec;
1317 /* check client and interface hw capabilities */
1318 snprintf(new_name, sizeof(new_name), "%s %s-%d",
1319 dai_link->stream_name, codec_dai->name, num);
1321 if (codec_dai->playback.channels_min)
1323 if (codec_dai->capture.channels_min)
1326 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1329 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
1335 dai_link->pcm = pcm;
1336 pcm->private_data = rtd;
1337 soc_pcm_ops.mmap = platform->pcm_ops->mmap;
1338 soc_pcm_ops.pointer = platform->pcm_ops->pointer;
1339 soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
1340 soc_pcm_ops.copy = platform->pcm_ops->copy;
1341 soc_pcm_ops.silence = platform->pcm_ops->silence;
1342 soc_pcm_ops.ack = platform->pcm_ops->ack;
1343 soc_pcm_ops.page = platform->pcm_ops->page;
1346 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1349 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1351 ret = platform->pcm_new(codec->card, codec_dai, pcm);
1353 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1358 pcm->private_free = platform->pcm_free;
1359 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1365 * snd_soc_codec_volatile_register: Report if a register is volatile.
1367 * @codec: CODEC to query.
1368 * @reg: Register to query.
1370 * Boolean function indiciating if a CODEC register is volatile.
1372 int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1374 if (codec->volatile_register)
1375 return codec->volatile_register(reg);
1379 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1382 * snd_soc_new_ac97_codec - initailise AC97 device
1383 * @codec: audio codec
1384 * @ops: AC97 bus operations
1385 * @num: AC97 codec number
1387 * Initialises AC97 codec resources for use by ad-hoc devices only.
1389 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1390 struct snd_ac97_bus_ops *ops, int num)
1392 mutex_lock(&codec->mutex);
1394 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1395 if (codec->ac97 == NULL) {
1396 mutex_unlock(&codec->mutex);
1400 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1401 if (codec->ac97->bus == NULL) {
1404 mutex_unlock(&codec->mutex);
1408 codec->ac97->bus->ops = ops;
1409 codec->ac97->num = num;
1410 codec->dev = &codec->ac97->dev;
1411 mutex_unlock(&codec->mutex);
1414 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1417 * snd_soc_free_ac97_codec - free AC97 codec device
1418 * @codec: audio codec
1420 * Frees AC97 codec device resources.
1422 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1424 mutex_lock(&codec->mutex);
1425 kfree(codec->ac97->bus);
1428 mutex_unlock(&codec->mutex);
1430 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1433 * snd_soc_update_bits - update codec register bits
1434 * @codec: audio codec
1435 * @reg: codec register
1436 * @mask: register mask
1439 * Writes new register value.
1441 * Returns 1 for change else 0.
1443 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1444 unsigned int mask, unsigned int value)
1447 unsigned int old, new;
1449 old = snd_soc_read(codec, reg);
1450 new = (old & ~mask) | value;
1451 change = old != new;
1453 snd_soc_write(codec, reg, new);
1457 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1460 * snd_soc_update_bits_locked - update codec register bits
1461 * @codec: audio codec
1462 * @reg: codec register
1463 * @mask: register mask
1466 * Writes new register value, and takes the codec mutex.
1468 * Returns 1 for change else 0.
1470 int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1471 unsigned short reg, unsigned int mask,
1476 mutex_lock(&codec->mutex);
1477 change = snd_soc_update_bits(codec, reg, mask, value);
1478 mutex_unlock(&codec->mutex);
1482 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
1485 * snd_soc_test_bits - test register for change
1486 * @codec: audio codec
1487 * @reg: codec register
1488 * @mask: register mask
1491 * Tests a register with a new value and checks if the new value is
1492 * different from the old value.
1494 * Returns 1 for change else 0.
1496 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1497 unsigned int mask, unsigned int value)
1500 unsigned int old, new;
1502 old = snd_soc_read(codec, reg);
1503 new = (old & ~mask) | value;
1504 change = old != new;
1508 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1511 * snd_soc_new_pcms - create new sound card and pcms
1512 * @socdev: the SoC audio device
1513 * @idx: ALSA card index
1514 * @xid: card identification
1516 * Create a new sound card based upon the codec and interface pcms.
1518 * Returns 0 for success, else error.
1520 int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
1522 struct snd_soc_card *card = socdev->card;
1523 struct snd_soc_codec *codec = card->codec;
1526 mutex_lock(&codec->mutex);
1528 /* register a sound card */
1529 ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
1531 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1533 mutex_unlock(&codec->mutex);
1537 codec->socdev = socdev;
1538 codec->card->dev = socdev->dev;
1539 codec->card->private_data = codec;
1540 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1542 /* create the pcms */
1543 for (i = 0; i < card->num_links; i++) {
1544 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
1546 printk(KERN_ERR "asoc: can't create pcm %s\n",
1547 card->dai_link[i].stream_name);
1548 mutex_unlock(&codec->mutex);
1551 /* Check for codec->ac97 to handle the ac97.c fun */
1552 if (card->dai_link[i].codec_dai->ac97_control && codec->ac97) {
1553 snd_ac97_dev_add_pdata(codec->ac97,
1554 card->dai_link[i].cpu_dai->ac97_pdata);
1558 mutex_unlock(&codec->mutex);
1561 EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1564 * snd_soc_free_pcms - free sound card and pcms
1565 * @socdev: the SoC audio device
1567 * Frees sound card and pcms associated with the socdev.
1568 * Also unregister the codec if it is an AC97 device.
1570 void snd_soc_free_pcms(struct snd_soc_device *socdev)
1572 struct snd_soc_codec *codec = socdev->card->codec;
1573 #ifdef CONFIG_SND_SOC_AC97_BUS
1574 struct snd_soc_dai *codec_dai;
1578 mutex_lock(&codec->mutex);
1579 soc_cleanup_codec_debugfs(codec);
1580 #ifdef CONFIG_SND_SOC_AC97_BUS
1581 for (i = 0; i < codec->num_dai; i++) {
1582 codec_dai = &codec->dai[i];
1583 if (codec_dai->ac97_control && codec->ac97 &&
1584 strcmp(codec->name, "AC97") != 0) {
1585 soc_ac97_dev_unregister(codec);
1593 snd_card_free(codec->card);
1594 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1595 mutex_unlock(&codec->mutex);
1597 EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1600 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1601 * @substream: the pcm substream
1602 * @hw: the hardware parameters
1604 * Sets the substream runtime hardware parameters.
1606 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1607 const struct snd_pcm_hardware *hw)
1609 struct snd_pcm_runtime *runtime = substream->runtime;
1610 runtime->hw.info = hw->info;
1611 runtime->hw.formats = hw->formats;
1612 runtime->hw.period_bytes_min = hw->period_bytes_min;
1613 runtime->hw.period_bytes_max = hw->period_bytes_max;
1614 runtime->hw.periods_min = hw->periods_min;
1615 runtime->hw.periods_max = hw->periods_max;
1616 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1617 runtime->hw.fifo_size = hw->fifo_size;
1620 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1623 * snd_soc_cnew - create new control
1624 * @_template: control template
1625 * @data: control private data
1626 * @long_name: control long name
1628 * Create a new mixer control from a template control.
1630 * Returns 0 for success, else error.
1632 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1633 void *data, char *long_name)
1635 struct snd_kcontrol_new template;
1637 memcpy(&template, _template, sizeof(template));
1639 template.name = long_name;
1642 return snd_ctl_new1(&template, data);
1644 EXPORT_SYMBOL_GPL(snd_soc_cnew);
1647 * snd_soc_add_controls - add an array of controls to a codec.
1648 * Convienience function to add a list of controls. Many codecs were
1649 * duplicating this code.
1651 * @codec: codec to add controls to
1652 * @controls: array of controls to add
1653 * @num_controls: number of elements in the array
1655 * Return 0 for success, else error.
1657 int snd_soc_add_controls(struct snd_soc_codec *codec,
1658 const struct snd_kcontrol_new *controls, int num_controls)
1660 struct snd_card *card = codec->card;
1663 for (i = 0; i < num_controls; i++) {
1664 const struct snd_kcontrol_new *control = &controls[i];
1665 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
1667 dev_err(codec->dev, "%s: Failed to add %s\n",
1668 codec->name, control->name);
1675 EXPORT_SYMBOL_GPL(snd_soc_add_controls);
1678 * snd_soc_info_enum_double - enumerated double mixer info callback
1679 * @kcontrol: mixer control
1680 * @uinfo: control element information
1682 * Callback to provide information about a double enumerated
1685 * Returns 0 for success.
1687 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1688 struct snd_ctl_elem_info *uinfo)
1690 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1692 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1693 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1694 uinfo->value.enumerated.items = e->max;
1696 if (uinfo->value.enumerated.item > e->max - 1)
1697 uinfo->value.enumerated.item = e->max - 1;
1698 strcpy(uinfo->value.enumerated.name,
1699 e->texts[uinfo->value.enumerated.item]);
1702 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1705 * snd_soc_get_enum_double - enumerated double mixer get callback
1706 * @kcontrol: mixer control
1707 * @ucontrol: control element information
1709 * Callback to get the value of a double enumerated mixer.
1711 * Returns 0 for success.
1713 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1714 struct snd_ctl_elem_value *ucontrol)
1716 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1717 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1718 unsigned int val, bitmask;
1720 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1722 val = snd_soc_read(codec, e->reg);
1723 ucontrol->value.enumerated.item[0]
1724 = (val >> e->shift_l) & (bitmask - 1);
1725 if (e->shift_l != e->shift_r)
1726 ucontrol->value.enumerated.item[1] =
1727 (val >> e->shift_r) & (bitmask - 1);
1731 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1734 * snd_soc_put_enum_double - enumerated double mixer put callback
1735 * @kcontrol: mixer control
1736 * @ucontrol: control element information
1738 * Callback to set the value of a double enumerated mixer.
1740 * Returns 0 for success.
1742 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1743 struct snd_ctl_elem_value *ucontrol)
1745 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1746 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1748 unsigned int mask, bitmask;
1750 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1752 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1754 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1755 mask = (bitmask - 1) << e->shift_l;
1756 if (e->shift_l != e->shift_r) {
1757 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1759 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1760 mask |= (bitmask - 1) << e->shift_r;
1763 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
1765 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1768 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
1769 * @kcontrol: mixer control
1770 * @ucontrol: control element information
1772 * Callback to get the value of a double semi enumerated mixer.
1774 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1775 * used for handling bitfield coded enumeration for example.
1777 * Returns 0 for success.
1779 int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
1780 struct snd_ctl_elem_value *ucontrol)
1782 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1783 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1784 unsigned int reg_val, val, mux;
1786 reg_val = snd_soc_read(codec, e->reg);
1787 val = (reg_val >> e->shift_l) & e->mask;
1788 for (mux = 0; mux < e->max; mux++) {
1789 if (val == e->values[mux])
1792 ucontrol->value.enumerated.item[0] = mux;
1793 if (e->shift_l != e->shift_r) {
1794 val = (reg_val >> e->shift_r) & e->mask;
1795 for (mux = 0; mux < e->max; mux++) {
1796 if (val == e->values[mux])
1799 ucontrol->value.enumerated.item[1] = mux;
1804 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
1807 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
1808 * @kcontrol: mixer control
1809 * @ucontrol: control element information
1811 * Callback to set the value of a double semi enumerated mixer.
1813 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1814 * used for handling bitfield coded enumeration for example.
1816 * Returns 0 for success.
1818 int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
1819 struct snd_ctl_elem_value *ucontrol)
1821 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1822 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1826 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1828 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1829 mask = e->mask << e->shift_l;
1830 if (e->shift_l != e->shift_r) {
1831 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1833 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1834 mask |= e->mask << e->shift_r;
1837 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
1839 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
1842 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1843 * @kcontrol: mixer control
1844 * @uinfo: control element information
1846 * Callback to provide information about an external enumerated
1849 * Returns 0 for success.
1851 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1852 struct snd_ctl_elem_info *uinfo)
1854 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1856 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1858 uinfo->value.enumerated.items = e->max;
1860 if (uinfo->value.enumerated.item > e->max - 1)
1861 uinfo->value.enumerated.item = e->max - 1;
1862 strcpy(uinfo->value.enumerated.name,
1863 e->texts[uinfo->value.enumerated.item]);
1866 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1869 * snd_soc_info_volsw_ext - external single mixer info callback
1870 * @kcontrol: mixer control
1871 * @uinfo: control element information
1873 * Callback to provide information about a single external mixer control.
1875 * Returns 0 for success.
1877 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1878 struct snd_ctl_elem_info *uinfo)
1880 int max = kcontrol->private_value;
1882 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
1883 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1885 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1888 uinfo->value.integer.min = 0;
1889 uinfo->value.integer.max = max;
1892 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1895 * snd_soc_info_volsw - single mixer info callback
1896 * @kcontrol: mixer control
1897 * @uinfo: control element information
1899 * Callback to provide information about a single mixer control.
1901 * Returns 0 for success.
1903 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1904 struct snd_ctl_elem_info *uinfo)
1906 struct soc_mixer_control *mc =
1907 (struct soc_mixer_control *)kcontrol->private_value;
1909 unsigned int shift = mc->shift;
1910 unsigned int rshift = mc->rshift;
1912 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
1913 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1915 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1917 uinfo->count = shift == rshift ? 1 : 2;
1918 uinfo->value.integer.min = 0;
1919 uinfo->value.integer.max = max;
1922 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1925 * snd_soc_get_volsw - single mixer get callback
1926 * @kcontrol: mixer control
1927 * @ucontrol: control element information
1929 * Callback to get the value of a single mixer control.
1931 * Returns 0 for success.
1933 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1934 struct snd_ctl_elem_value *ucontrol)
1936 struct soc_mixer_control *mc =
1937 (struct soc_mixer_control *)kcontrol->private_value;
1938 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1939 unsigned int reg = mc->reg;
1940 unsigned int shift = mc->shift;
1941 unsigned int rshift = mc->rshift;
1943 unsigned int mask = (1 << fls(max)) - 1;
1944 unsigned int invert = mc->invert;
1946 ucontrol->value.integer.value[0] =
1947 (snd_soc_read(codec, reg) >> shift) & mask;
1948 if (shift != rshift)
1949 ucontrol->value.integer.value[1] =
1950 (snd_soc_read(codec, reg) >> rshift) & mask;
1952 ucontrol->value.integer.value[0] =
1953 max - ucontrol->value.integer.value[0];
1954 if (shift != rshift)
1955 ucontrol->value.integer.value[1] =
1956 max - ucontrol->value.integer.value[1];
1961 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1964 * snd_soc_put_volsw - single mixer put callback
1965 * @kcontrol: mixer control
1966 * @ucontrol: control element information
1968 * Callback to set the value of a single mixer control.
1970 * Returns 0 for success.
1972 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1973 struct snd_ctl_elem_value *ucontrol)
1975 struct soc_mixer_control *mc =
1976 (struct soc_mixer_control *)kcontrol->private_value;
1977 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1978 unsigned int reg = mc->reg;
1979 unsigned int shift = mc->shift;
1980 unsigned int rshift = mc->rshift;
1982 unsigned int mask = (1 << fls(max)) - 1;
1983 unsigned int invert = mc->invert;
1984 unsigned int val, val2, val_mask;
1986 val = (ucontrol->value.integer.value[0] & mask);
1989 val_mask = mask << shift;
1991 if (shift != rshift) {
1992 val2 = (ucontrol->value.integer.value[1] & mask);
1995 val_mask |= mask << rshift;
1996 val |= val2 << rshift;
1998 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2000 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2003 * snd_soc_info_volsw_2r - double mixer info callback
2004 * @kcontrol: mixer control
2005 * @uinfo: control element information
2007 * Callback to provide information about a double mixer control that
2008 * spans 2 codec registers.
2010 * Returns 0 for success.
2012 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2013 struct snd_ctl_elem_info *uinfo)
2015 struct soc_mixer_control *mc =
2016 (struct soc_mixer_control *)kcontrol->private_value;
2019 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2020 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2022 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2025 uinfo->value.integer.min = 0;
2026 uinfo->value.integer.max = max;
2029 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2032 * snd_soc_get_volsw_2r - double mixer get callback
2033 * @kcontrol: mixer control
2034 * @ucontrol: control element information
2036 * Callback to get the value of a double mixer control that spans 2 registers.
2038 * Returns 0 for success.
2040 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2041 struct snd_ctl_elem_value *ucontrol)
2043 struct soc_mixer_control *mc =
2044 (struct soc_mixer_control *)kcontrol->private_value;
2045 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2046 unsigned int reg = mc->reg;
2047 unsigned int reg2 = mc->rreg;
2048 unsigned int shift = mc->shift;
2050 unsigned int mask = (1 << fls(max)) - 1;
2051 unsigned int invert = mc->invert;
2053 ucontrol->value.integer.value[0] =
2054 (snd_soc_read(codec, reg) >> shift) & mask;
2055 ucontrol->value.integer.value[1] =
2056 (snd_soc_read(codec, reg2) >> shift) & mask;
2058 ucontrol->value.integer.value[0] =
2059 max - ucontrol->value.integer.value[0];
2060 ucontrol->value.integer.value[1] =
2061 max - ucontrol->value.integer.value[1];
2066 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2069 * snd_soc_put_volsw_2r - double mixer set callback
2070 * @kcontrol: mixer control
2071 * @ucontrol: control element information
2073 * Callback to set the value of a double mixer control that spans 2 registers.
2075 * Returns 0 for success.
2077 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2078 struct snd_ctl_elem_value *ucontrol)
2080 struct soc_mixer_control *mc =
2081 (struct soc_mixer_control *)kcontrol->private_value;
2082 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2083 unsigned int reg = mc->reg;
2084 unsigned int reg2 = mc->rreg;
2085 unsigned int shift = mc->shift;
2087 unsigned int mask = (1 << fls(max)) - 1;
2088 unsigned int invert = mc->invert;
2090 unsigned int val, val2, val_mask;
2092 val_mask = mask << shift;
2093 val = (ucontrol->value.integer.value[0] & mask);
2094 val2 = (ucontrol->value.integer.value[1] & mask);
2102 val2 = val2 << shift;
2104 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2108 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2111 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2114 * snd_soc_info_volsw_s8 - signed mixer info callback
2115 * @kcontrol: mixer control
2116 * @uinfo: control element information
2118 * Callback to provide information about a signed mixer control.
2120 * Returns 0 for success.
2122 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2123 struct snd_ctl_elem_info *uinfo)
2125 struct soc_mixer_control *mc =
2126 (struct soc_mixer_control *)kcontrol->private_value;
2130 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2132 uinfo->value.integer.min = 0;
2133 uinfo->value.integer.max = max-min;
2136 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2139 * snd_soc_get_volsw_s8 - signed mixer get callback
2140 * @kcontrol: mixer control
2141 * @ucontrol: control element information
2143 * Callback to get the value of a signed mixer control.
2145 * Returns 0 for success.
2147 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2148 struct snd_ctl_elem_value *ucontrol)
2150 struct soc_mixer_control *mc =
2151 (struct soc_mixer_control *)kcontrol->private_value;
2152 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2153 unsigned int reg = mc->reg;
2155 int val = snd_soc_read(codec, reg);
2157 ucontrol->value.integer.value[0] =
2158 ((signed char)(val & 0xff))-min;
2159 ucontrol->value.integer.value[1] =
2160 ((signed char)((val >> 8) & 0xff))-min;
2163 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2166 * snd_soc_put_volsw_sgn - signed mixer put callback
2167 * @kcontrol: mixer control
2168 * @ucontrol: control element information
2170 * Callback to set the value of a signed mixer control.
2172 * Returns 0 for success.
2174 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2175 struct snd_ctl_elem_value *ucontrol)
2177 struct soc_mixer_control *mc =
2178 (struct soc_mixer_control *)kcontrol->private_value;
2179 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2180 unsigned int reg = mc->reg;
2184 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2185 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2187 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2189 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2192 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2194 * @clk_id: DAI specific clock ID
2195 * @freq: new clock frequency in Hz
2196 * @dir: new clock direction - input/output.
2198 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2200 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2201 unsigned int freq, int dir)
2203 if (dai->ops && dai->ops->set_sysclk)
2204 return dai->ops->set_sysclk(dai, clk_id, freq, dir);
2208 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2211 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2213 * @div_id: DAI specific clock divider ID
2214 * @div: new clock divisor.
2216 * Configures the clock dividers. This is used to derive the best DAI bit and
2217 * frame clocks from the system or master clock. It's best to set the DAI bit
2218 * and frame clocks as low as possible to save system power.
2220 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2221 int div_id, int div)
2223 if (dai->ops && dai->ops->set_clkdiv)
2224 return dai->ops->set_clkdiv(dai, div_id, div);
2228 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2231 * snd_soc_dai_set_pll - configure DAI PLL.
2233 * @pll_id: DAI specific PLL ID
2234 * @source: DAI specific source for the PLL
2235 * @freq_in: PLL input clock frequency in Hz
2236 * @freq_out: requested PLL output clock frequency in Hz
2238 * Configures and enables PLL to generate output clock based on input clock.
2240 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2241 unsigned int freq_in, unsigned int freq_out)
2243 if (dai->ops && dai->ops->set_pll)
2244 return dai->ops->set_pll(dai, pll_id, source,
2249 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2252 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2254 * @fmt: SND_SOC_DAIFMT_ format value.
2256 * Configures the DAI hardware format and clocking.
2258 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2260 if (dai->ops && dai->ops->set_fmt)
2261 return dai->ops->set_fmt(dai, fmt);
2265 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2268 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2270 * @tx_mask: bitmask representing active TX slots.
2271 * @rx_mask: bitmask representing active RX slots.
2272 * @slots: Number of slots in use.
2273 * @slot_width: Width in bits for each slot.
2275 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2278 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2279 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2281 if (dai->ops && dai->ops->set_tdm_slot)
2282 return dai->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2287 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2290 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2292 * @tx_num: how many TX channels
2293 * @tx_slot: pointer to an array which imply the TX slot number channel
2295 * @rx_num: how many RX channels
2296 * @rx_slot: pointer to an array which imply the RX slot number channel
2299 * configure the relationship between channel number and TDM slot number.
2301 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2302 unsigned int tx_num, unsigned int *tx_slot,
2303 unsigned int rx_num, unsigned int *rx_slot)
2305 if (dai->ops && dai->ops->set_channel_map)
2306 return dai->ops->set_channel_map(dai, tx_num, tx_slot,
2311 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2314 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2316 * @tristate: tristate enable
2318 * Tristates the DAI so that others can use it.
2320 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2322 if (dai->ops && dai->ops->set_tristate)
2323 return dai->ops->set_tristate(dai, tristate);
2327 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2330 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2332 * @mute: mute enable
2334 * Mutes the DAI DAC.
2336 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2338 if (dai->ops && dai->ops->digital_mute)
2339 return dai->ops->digital_mute(dai, mute);
2343 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2346 * snd_soc_register_card - Register a card with the ASoC core
2348 * @card: Card to register
2350 * Note that currently this is an internal only function: it will be
2351 * exposed to machine drivers after further backporting of ASoC v2
2352 * registration APIs.
2354 static int snd_soc_register_card(struct snd_soc_card *card)
2356 if (!card->name || !card->dev)
2359 INIT_LIST_HEAD(&card->list);
2360 card->instantiated = 0;
2362 mutex_lock(&client_mutex);
2363 list_add(&card->list, &card_list);
2364 snd_soc_instantiate_cards();
2365 mutex_unlock(&client_mutex);
2367 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2373 * snd_soc_unregister_card - Unregister a card with the ASoC core
2375 * @card: Card to unregister
2377 * Note that currently this is an internal only function: it will be
2378 * exposed to machine drivers after further backporting of ASoC v2
2379 * registration APIs.
2381 static int snd_soc_unregister_card(struct snd_soc_card *card)
2383 mutex_lock(&client_mutex);
2384 list_del(&card->list);
2385 mutex_unlock(&client_mutex);
2387 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2393 * snd_soc_register_dai - Register a DAI with the ASoC core
2395 * @dai: DAI to register
2397 int snd_soc_register_dai(struct snd_soc_dai *dai)
2402 /* The device should become mandatory over time */
2404 printk(KERN_WARNING "No device for DAI %s\n", dai->name);
2407 dai->ops = &null_dai_ops;
2409 INIT_LIST_HEAD(&dai->list);
2411 mutex_lock(&client_mutex);
2412 list_add(&dai->list, &dai_list);
2413 snd_soc_instantiate_cards();
2414 mutex_unlock(&client_mutex);
2416 pr_debug("Registered DAI '%s'\n", dai->name);
2420 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2423 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2425 * @dai: DAI to unregister
2427 void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2429 mutex_lock(&client_mutex);
2430 list_del(&dai->list);
2431 mutex_unlock(&client_mutex);
2433 pr_debug("Unregistered DAI '%s'\n", dai->name);
2435 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2438 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2440 * @dai: Array of DAIs to register
2441 * @count: Number of DAIs
2443 int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
2447 for (i = 0; i < count; i++) {
2448 ret = snd_soc_register_dai(&dai[i]);
2456 for (i--; i >= 0; i--)
2457 snd_soc_unregister_dai(&dai[i]);
2461 EXPORT_SYMBOL_GPL(snd_soc_register_dais);
2464 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
2466 * @dai: Array of DAIs to unregister
2467 * @count: Number of DAIs
2469 void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
2473 for (i = 0; i < count; i++)
2474 snd_soc_unregister_dai(&dai[i]);
2476 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
2479 * snd_soc_register_platform - Register a platform with the ASoC core
2481 * @platform: platform to register
2483 int snd_soc_register_platform(struct snd_soc_platform *platform)
2485 if (!platform->name)
2488 INIT_LIST_HEAD(&platform->list);
2490 mutex_lock(&client_mutex);
2491 list_add(&platform->list, &platform_list);
2492 snd_soc_instantiate_cards();
2493 mutex_unlock(&client_mutex);
2495 pr_debug("Registered platform '%s'\n", platform->name);
2499 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2502 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2504 * @platform: platform to unregister
2506 void snd_soc_unregister_platform(struct snd_soc_platform *platform)
2508 mutex_lock(&client_mutex);
2509 list_del(&platform->list);
2510 mutex_unlock(&client_mutex);
2512 pr_debug("Unregistered platform '%s'\n", platform->name);
2514 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2516 static u64 codec_format_map[] = {
2517 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2518 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2519 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2520 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2521 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2522 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2523 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2524 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2525 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2526 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2527 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2528 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2529 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2530 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2531 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2532 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2535 /* Fix up the DAI formats for endianness: codecs don't actually see
2536 * the endianness of the data but we're using the CPU format
2537 * definitions which do need to include endianness so we ensure that
2538 * codec DAIs always have both big and little endian variants set.
2540 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2544 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2545 if (stream->formats & codec_format_map[i])
2546 stream->formats |= codec_format_map[i];
2550 * snd_soc_register_codec - Register a codec with the ASoC core
2552 * @codec: codec to register
2554 int snd_soc_register_codec(struct snd_soc_codec *codec)
2561 /* The device should become mandatory over time */
2563 printk(KERN_WARNING "No device for codec %s\n", codec->name);
2565 INIT_LIST_HEAD(&codec->list);
2567 for (i = 0; i < codec->num_dai; i++) {
2568 fixup_codec_formats(&codec->dai[i].playback);
2569 fixup_codec_formats(&codec->dai[i].capture);
2572 mutex_lock(&client_mutex);
2573 list_add(&codec->list, &codec_list);
2574 snd_soc_instantiate_cards();
2575 mutex_unlock(&client_mutex);
2577 pr_debug("Registered codec '%s'\n", codec->name);
2581 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
2584 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
2586 * @codec: codec to unregister
2588 void snd_soc_unregister_codec(struct snd_soc_codec *codec)
2590 mutex_lock(&client_mutex);
2591 list_del(&codec->list);
2592 mutex_unlock(&client_mutex);
2594 pr_debug("Unregistered codec '%s'\n", codec->name);
2596 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
2598 static int __init snd_soc_init(void)
2600 #ifdef CONFIG_DEBUG_FS
2601 debugfs_root = debugfs_create_dir("asoc", NULL);
2602 if (IS_ERR(debugfs_root) || !debugfs_root) {
2604 "ASoC: Failed to create debugfs directory\n");
2605 debugfs_root = NULL;
2609 return platform_driver_register(&soc_driver);
2612 static void __exit snd_soc_exit(void)
2614 #ifdef CONFIG_DEBUG_FS
2615 debugfs_remove_recursive(debugfs_root);
2617 platform_driver_unregister(&soc_driver);
2620 module_init(snd_soc_init);
2621 module_exit(snd_soc_exit);
2623 /* Module information */
2624 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2625 MODULE_DESCRIPTION("ALSA SoC Core");
2626 MODULE_LICENSE("GPL");
2627 MODULE_ALIAS("platform:soc-audio");