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 <linux/slab.h>
32 #include <sound/ac97_codec.h>
33 #include <sound/core.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
36 #include <sound/soc.h>
37 #include <sound/soc-dapm.h>
38 #include <sound/initval.h>
40 static DEFINE_MUTEX(pcm_mutex);
41 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
43 #ifdef CONFIG_DEBUG_FS
44 static struct dentry *debugfs_root;
47 static DEFINE_MUTEX(client_mutex);
48 static LIST_HEAD(card_list);
49 static LIST_HEAD(dai_list);
50 static LIST_HEAD(platform_list);
51 static LIST_HEAD(codec_list);
53 static int snd_soc_register_card(struct snd_soc_card *card);
54 static int snd_soc_unregister_card(struct snd_soc_card *card);
57 * This is a timeout to do a DAPM powerdown after a stream is closed().
58 * It can be used to eliminate pops between different playback streams, e.g.
59 * between two audio tracks.
61 static int pmdown_time = 5000;
62 module_param(pmdown_time, int, 0);
63 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
66 * This function forces any delayed work to be queued and run.
68 static int run_delayed_work(struct delayed_work *dwork)
72 /* cancel any work waiting to be queued. */
73 ret = cancel_delayed_work(dwork);
75 /* if there was any work waiting then we run it now and
76 * wait for it's completion */
78 schedule_delayed_work(dwork, 0);
79 flush_scheduled_work();
84 /* codec register dump */
85 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
87 int ret, i, step = 1, count = 0;
89 if (!codec->reg_cache_size)
92 if (codec->reg_cache_step)
93 step = codec->reg_cache_step;
95 count += sprintf(buf, "%s registers\n", codec->name);
96 for (i = 0; i < codec->reg_cache_size; i += step) {
97 if (codec->readable_register && !codec->readable_register(i))
100 count += sprintf(buf + count, "%2x: ", i);
101 if (count >= PAGE_SIZE - 1)
104 if (codec->display_register) {
105 count += codec->display_register(codec, buf + count,
106 PAGE_SIZE - count, i);
108 /* If the read fails it's almost certainly due to
109 * the register being volatile and the device being
112 ret = codec->read(codec, i);
114 count += snprintf(buf + count,
118 count += snprintf(buf + count,
120 "<no data: %d>", ret);
123 if (count >= PAGE_SIZE - 1)
126 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
127 if (count >= PAGE_SIZE - 1)
131 /* Truncate count; min() would cause a warning */
132 if (count >= PAGE_SIZE)
133 count = PAGE_SIZE - 1;
137 static ssize_t codec_reg_show(struct device *dev,
138 struct device_attribute *attr, char *buf)
140 struct snd_soc_device *devdata = dev_get_drvdata(dev);
141 return soc_codec_reg_show(devdata->card->codec, buf);
144 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
146 static ssize_t pmdown_time_show(struct device *dev,
147 struct device_attribute *attr, char *buf)
149 struct snd_soc_device *socdev = dev_get_drvdata(dev);
150 struct snd_soc_card *card = socdev->card;
152 return sprintf(buf, "%ld\n", card->pmdown_time);
155 static ssize_t pmdown_time_set(struct device *dev,
156 struct device_attribute *attr,
157 const char *buf, size_t count)
159 struct snd_soc_device *socdev = dev_get_drvdata(dev);
160 struct snd_soc_card *card = socdev->card;
162 strict_strtol(buf, 10, &card->pmdown_time);
167 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
169 #ifdef CONFIG_DEBUG_FS
170 static int codec_reg_open_file(struct inode *inode, struct file *file)
172 file->private_data = inode->i_private;
176 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
177 size_t count, loff_t *ppos)
180 struct snd_soc_codec *codec = file->private_data;
181 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
184 ret = soc_codec_reg_show(codec, buf);
186 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
191 static ssize_t codec_reg_write_file(struct file *file,
192 const char __user *user_buf, size_t count, loff_t *ppos)
197 unsigned long reg, value;
199 struct snd_soc_codec *codec = file->private_data;
201 buf_size = min(count, (sizeof(buf)-1));
202 if (copy_from_user(buf, user_buf, buf_size))
206 if (codec->reg_cache_step)
207 step = codec->reg_cache_step;
209 while (*start == ' ')
211 reg = simple_strtoul(start, &start, 16);
212 if ((reg >= codec->reg_cache_size) || (reg % step))
214 while (*start == ' ')
216 if (strict_strtoul(start, 16, &value))
218 codec->write(codec, reg, value);
222 static const struct file_operations codec_reg_fops = {
223 .open = codec_reg_open_file,
224 .read = codec_reg_read_file,
225 .write = codec_reg_write_file,
228 static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
230 char codec_root[128];
233 snprintf(codec_root, sizeof(codec_root),
234 "%s.%s", codec->name, dev_name(codec->dev));
236 snprintf(codec_root, sizeof(codec_root),
239 codec->debugfs_codec_root = debugfs_create_dir(codec_root,
241 if (!codec->debugfs_codec_root) {
243 "ASoC: Failed to create codec debugfs directory\n");
247 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
248 codec->debugfs_codec_root,
249 codec, &codec_reg_fops);
250 if (!codec->debugfs_reg)
252 "ASoC: Failed to create codec register debugfs file\n");
254 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
255 codec->debugfs_codec_root,
257 if (!codec->debugfs_pop_time)
259 "Failed to create pop time debugfs file\n");
261 codec->debugfs_dapm = debugfs_create_dir("dapm",
262 codec->debugfs_codec_root);
263 if (!codec->debugfs_dapm)
265 "Failed to create DAPM debugfs directory\n");
267 snd_soc_dapm_debugfs_init(codec);
270 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
272 debugfs_remove_recursive(codec->debugfs_codec_root);
277 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
281 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
286 #ifdef CONFIG_SND_SOC_AC97_BUS
287 /* unregister ac97 codec */
288 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
290 if (codec->ac97->dev.bus)
291 device_unregister(&codec->ac97->dev);
295 /* stop no dev release warning */
296 static void soc_ac97_device_release(struct device *dev){}
298 /* register ac97 codec to bus */
299 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
303 codec->ac97->dev.bus = &ac97_bus_type;
304 codec->ac97->dev.parent = codec->card->dev;
305 codec->ac97->dev.release = soc_ac97_device_release;
307 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
308 codec->card->number, 0, codec->name);
309 err = device_register(&codec->ac97->dev);
311 snd_printk(KERN_ERR "Can't register ac97 bus\n");
312 codec->ac97->dev.bus = NULL;
319 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
321 struct snd_soc_pcm_runtime *rtd = substream->private_data;
322 struct snd_soc_device *socdev = rtd->socdev;
323 struct snd_soc_card *card = socdev->card;
324 struct snd_soc_dai_link *machine = rtd->dai;
325 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
326 struct snd_soc_dai *codec_dai = machine->codec_dai;
329 if (codec_dai->symmetric_rates || cpu_dai->symmetric_rates ||
330 machine->symmetric_rates) {
331 dev_dbg(card->dev, "Symmetry forces %dHz rate\n",
334 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
335 SNDRV_PCM_HW_PARAM_RATE,
340 "Unable to apply rate symmetry constraint: %d\n", ret);
349 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
350 * then initialized and any private data can be allocated. This also calls
351 * startup for the cpu DAI, platform, machine and codec DAI.
353 static int soc_pcm_open(struct snd_pcm_substream *substream)
355 struct snd_soc_pcm_runtime *rtd = substream->private_data;
356 struct snd_soc_device *socdev = rtd->socdev;
357 struct snd_soc_card *card = socdev->card;
358 struct snd_pcm_runtime *runtime = substream->runtime;
359 struct snd_soc_dai_link *machine = rtd->dai;
360 struct snd_soc_platform *platform = card->platform;
361 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
362 struct snd_soc_dai *codec_dai = machine->codec_dai;
365 mutex_lock(&pcm_mutex);
367 /* startup the audio subsystem */
368 if (cpu_dai->active == 0 && cpu_dai->ops->startup) {
369 ret = cpu_dai->ops->startup(substream, cpu_dai);
371 printk(KERN_ERR "asoc: can't open interface %s\n",
377 if (platform->pcm_ops->open) {
378 ret = platform->pcm_ops->open(substream);
380 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
385 if (codec_dai->active == 0 && codec_dai->ops->startup) {
386 ret = codec_dai->ops->startup(substream, codec_dai);
388 printk(KERN_ERR "asoc: can't open codec %s\n",
394 if (machine->ops && machine->ops->startup) {
395 ret = machine->ops->startup(substream);
397 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
402 /* Check that the codec and cpu DAI's are compatible */
403 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
404 runtime->hw.rate_min =
405 max(codec_dai->playback.rate_min,
406 cpu_dai->playback.rate_min);
407 runtime->hw.rate_max =
408 min(codec_dai->playback.rate_max,
409 cpu_dai->playback.rate_max);
410 runtime->hw.channels_min =
411 max(codec_dai->playback.channels_min,
412 cpu_dai->playback.channels_min);
413 runtime->hw.channels_max =
414 min(codec_dai->playback.channels_max,
415 cpu_dai->playback.channels_max);
416 runtime->hw.formats =
417 codec_dai->playback.formats & cpu_dai->playback.formats;
419 codec_dai->playback.rates & cpu_dai->playback.rates;
420 if (codec_dai->playback.rates
421 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
422 runtime->hw.rates |= cpu_dai->playback.rates;
423 if (cpu_dai->playback.rates
424 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
425 runtime->hw.rates |= codec_dai->playback.rates;
427 runtime->hw.rate_min =
428 max(codec_dai->capture.rate_min,
429 cpu_dai->capture.rate_min);
430 runtime->hw.rate_max =
431 min(codec_dai->capture.rate_max,
432 cpu_dai->capture.rate_max);
433 runtime->hw.channels_min =
434 max(codec_dai->capture.channels_min,
435 cpu_dai->capture.channels_min);
436 runtime->hw.channels_max =
437 min(codec_dai->capture.channels_max,
438 cpu_dai->capture.channels_max);
439 runtime->hw.formats =
440 codec_dai->capture.formats & cpu_dai->capture.formats;
442 codec_dai->capture.rates & cpu_dai->capture.rates;
443 if (codec_dai->capture.rates
444 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
445 runtime->hw.rates |= cpu_dai->capture.rates;
446 if (cpu_dai->capture.rates
447 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
448 runtime->hw.rates |= codec_dai->capture.rates;
451 snd_pcm_limit_hw_rates(runtime);
452 if (!runtime->hw.rates) {
453 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
454 codec_dai->name, cpu_dai->name);
457 if (!runtime->hw.formats) {
458 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
459 codec_dai->name, cpu_dai->name);
462 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
463 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
464 codec_dai->name, cpu_dai->name);
468 /* Symmetry only applies if we've already got an active stream. */
469 if (cpu_dai->active || codec_dai->active) {
470 ret = soc_pcm_apply_symmetry(substream);
475 pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
476 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
477 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
478 runtime->hw.channels_max);
479 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
480 runtime->hw.rate_max);
482 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
483 cpu_dai->playback.active++;
484 codec_dai->playback.active++;
486 cpu_dai->capture.active++;
487 codec_dai->capture.active++;
491 card->codec->active++;
492 mutex_unlock(&pcm_mutex);
496 if (machine->ops && machine->ops->shutdown)
497 machine->ops->shutdown(substream);
500 if (codec_dai->active == 0 && codec_dai->ops->shutdown)
501 codec_dai->ops->shutdown(substream, codec_dai);
504 if (platform->pcm_ops->close)
505 platform->pcm_ops->close(substream);
508 if (cpu_dai->active == 0 && cpu_dai->ops->shutdown)
509 cpu_dai->ops->shutdown(substream, cpu_dai);
511 mutex_unlock(&pcm_mutex);
516 * Power down the audio subsystem pmdown_time msecs after close is called.
517 * This is to ensure there are no pops or clicks in between any music tracks
518 * due to DAPM power cycling.
520 static void close_delayed_work(struct work_struct *work)
522 struct snd_soc_card *card = container_of(work, struct snd_soc_card,
524 struct snd_soc_codec *codec = card->codec;
525 struct snd_soc_dai *codec_dai;
528 mutex_lock(&pcm_mutex);
529 for (i = 0; i < codec->num_dai; i++) {
530 codec_dai = &codec->dai[i];
532 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
533 codec_dai->playback.stream_name,
534 codec_dai->playback.active ? "active" : "inactive",
535 codec_dai->pop_wait ? "yes" : "no");
537 /* are we waiting on this codec DAI stream */
538 if (codec_dai->pop_wait == 1) {
539 codec_dai->pop_wait = 0;
540 snd_soc_dapm_stream_event(codec,
541 codec_dai->playback.stream_name,
542 SND_SOC_DAPM_STREAM_STOP);
545 mutex_unlock(&pcm_mutex);
549 * Called by ALSA when a PCM substream is closed. Private data can be
550 * freed here. The cpu DAI, codec DAI, machine and platform are also
553 static int soc_codec_close(struct snd_pcm_substream *substream)
555 struct snd_soc_pcm_runtime *rtd = substream->private_data;
556 struct snd_soc_device *socdev = rtd->socdev;
557 struct snd_soc_card *card = socdev->card;
558 struct snd_soc_dai_link *machine = rtd->dai;
559 struct snd_soc_platform *platform = card->platform;
560 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
561 struct snd_soc_dai *codec_dai = machine->codec_dai;
562 struct snd_soc_codec *codec = card->codec;
564 mutex_lock(&pcm_mutex);
566 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
567 cpu_dai->playback.active--;
568 codec_dai->playback.active--;
570 cpu_dai->capture.active--;
571 codec_dai->capture.active--;
578 /* Muting the DAC suppresses artifacts caused during digital
579 * shutdown, for example from stopping clocks.
581 if (codec_dai->active == 0 &&
582 substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
583 snd_soc_dai_digital_mute(codec_dai, 1);
585 if (cpu_dai->active == 0 && cpu_dai->ops->shutdown)
586 cpu_dai->ops->shutdown(substream, cpu_dai);
588 if (codec_dai->active == 0 && codec_dai->ops->shutdown)
589 codec_dai->ops->shutdown(substream, codec_dai);
591 if (machine->ops && machine->ops->shutdown)
592 machine->ops->shutdown(substream);
594 if (platform->pcm_ops->close)
595 platform->pcm_ops->close(substream);
597 if (codec_dai->active)
600 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
601 /* start delayed pop wq here for playback streams */
602 codec_dai->pop_wait = 1;
603 schedule_delayed_work(&card->delayed_work,
604 msecs_to_jiffies(card->pmdown_time));
606 /* capture streams can be powered down now */
607 snd_soc_dapm_stream_event(codec,
608 codec_dai->capture.stream_name,
609 SND_SOC_DAPM_STREAM_STOP);
613 mutex_unlock(&pcm_mutex);
618 * Called by ALSA when the PCM substream is prepared, can set format, sample
619 * rate, etc. This function is non atomic and can be called multiple times,
620 * it can refer to the runtime info.
622 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
624 struct snd_soc_pcm_runtime *rtd = substream->private_data;
625 struct snd_soc_device *socdev = rtd->socdev;
626 struct snd_soc_card *card = socdev->card;
627 struct snd_soc_dai_link *machine = rtd->dai;
628 struct snd_soc_platform *platform = card->platform;
629 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
630 struct snd_soc_dai *codec_dai = machine->codec_dai;
631 struct snd_soc_codec *codec = card->codec;
634 mutex_lock(&pcm_mutex);
636 if (machine->ops && machine->ops->prepare) {
637 ret = machine->ops->prepare(substream);
639 printk(KERN_ERR "asoc: machine prepare error\n");
644 if (platform->pcm_ops->prepare) {
645 ret = platform->pcm_ops->prepare(substream);
647 printk(KERN_ERR "asoc: platform prepare error\n");
652 if (codec_dai->ops->prepare) {
653 ret = codec_dai->ops->prepare(substream, codec_dai);
655 printk(KERN_ERR "asoc: codec DAI prepare error\n");
660 if (cpu_dai->ops->prepare) {
661 ret = cpu_dai->ops->prepare(substream, cpu_dai);
663 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
668 /* cancel any delayed stream shutdown that is pending */
669 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
670 codec_dai->pop_wait) {
671 codec_dai->pop_wait = 0;
672 cancel_delayed_work(&card->delayed_work);
675 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
676 snd_soc_dapm_stream_event(codec,
677 codec_dai->playback.stream_name,
678 SND_SOC_DAPM_STREAM_START);
680 snd_soc_dapm_stream_event(codec,
681 codec_dai->capture.stream_name,
682 SND_SOC_DAPM_STREAM_START);
684 snd_soc_dai_digital_mute(codec_dai, 0);
687 mutex_unlock(&pcm_mutex);
692 * Called by ALSA when the hardware params are set by application. This
693 * function can also be called multiple times and can allocate buffers
694 * (using snd_pcm_lib_* ). It's non-atomic.
696 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
697 struct snd_pcm_hw_params *params)
699 struct snd_soc_pcm_runtime *rtd = substream->private_data;
700 struct snd_soc_device *socdev = rtd->socdev;
701 struct snd_soc_dai_link *machine = rtd->dai;
702 struct snd_soc_card *card = socdev->card;
703 struct snd_soc_platform *platform = card->platform;
704 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
705 struct snd_soc_dai *codec_dai = machine->codec_dai;
708 mutex_lock(&pcm_mutex);
710 if (machine->ops && machine->ops->hw_params) {
711 ret = machine->ops->hw_params(substream, params);
713 printk(KERN_ERR "asoc: machine hw_params failed\n");
718 if (codec_dai->ops->hw_params) {
719 ret = codec_dai->ops->hw_params(substream, params, codec_dai);
721 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
727 if (cpu_dai->ops->hw_params) {
728 ret = cpu_dai->ops->hw_params(substream, params, cpu_dai);
730 printk(KERN_ERR "asoc: interface %s hw params failed\n",
736 if (platform->pcm_ops->hw_params) {
737 ret = platform->pcm_ops->hw_params(substream, params);
739 printk(KERN_ERR "asoc: platform %s hw params failed\n",
745 machine->rate = params_rate(params);
748 mutex_unlock(&pcm_mutex);
752 if (cpu_dai->ops->hw_free)
753 cpu_dai->ops->hw_free(substream, cpu_dai);
756 if (codec_dai->ops->hw_free)
757 codec_dai->ops->hw_free(substream, codec_dai);
760 if (machine->ops && machine->ops->hw_free)
761 machine->ops->hw_free(substream);
763 mutex_unlock(&pcm_mutex);
768 * Free's resources allocated by hw_params, can be called multiple times
770 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
772 struct snd_soc_pcm_runtime *rtd = substream->private_data;
773 struct snd_soc_device *socdev = rtd->socdev;
774 struct snd_soc_dai_link *machine = rtd->dai;
775 struct snd_soc_card *card = socdev->card;
776 struct snd_soc_platform *platform = card->platform;
777 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
778 struct snd_soc_dai *codec_dai = machine->codec_dai;
779 struct snd_soc_codec *codec = card->codec;
781 mutex_lock(&pcm_mutex);
783 /* apply codec digital mute */
785 snd_soc_dai_digital_mute(codec_dai, 1);
787 /* free any machine hw params */
788 if (machine->ops && machine->ops->hw_free)
789 machine->ops->hw_free(substream);
791 /* free any DMA resources */
792 if (platform->pcm_ops->hw_free)
793 platform->pcm_ops->hw_free(substream);
795 /* now free hw params for the DAI's */
796 if (codec_dai->active == 0 && codec_dai->ops->hw_free)
797 codec_dai->ops->hw_free(substream, codec_dai);
799 if (cpu_dai->active == 0 && cpu_dai->ops->hw_free)
800 cpu_dai->ops->hw_free(substream, cpu_dai);
802 mutex_unlock(&pcm_mutex);
806 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
808 struct snd_soc_pcm_runtime *rtd = substream->private_data;
809 struct snd_soc_device *socdev = rtd->socdev;
810 struct snd_soc_card *card= socdev->card;
811 struct snd_soc_dai_link *machine = rtd->dai;
812 struct snd_soc_platform *platform = card->platform;
813 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
814 struct snd_soc_dai *codec_dai = machine->codec_dai;
817 if (codec_dai->ops->trigger) {
818 ret = codec_dai->ops->trigger(substream, cmd, codec_dai);
823 if (platform->pcm_ops->trigger) {
824 ret = platform->pcm_ops->trigger(substream, cmd);
829 if (cpu_dai->ops->trigger) {
830 ret = cpu_dai->ops->trigger(substream, cmd, cpu_dai);
838 * soc level wrapper for pointer callback
839 * If cpu_dai, codec_dai, platform driver has the delay callback, than
840 * the runtime->delay will be updated accordingly.
842 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
844 struct snd_soc_pcm_runtime *rtd = substream->private_data;
845 struct snd_soc_device *socdev = rtd->socdev;
846 struct snd_soc_card *card = socdev->card;
847 struct snd_soc_platform *platform = card->platform;
848 struct snd_soc_dai_link *machine = rtd->dai;
849 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
850 struct snd_soc_dai *codec_dai = machine->codec_dai;
851 struct snd_pcm_runtime *runtime = substream->runtime;
852 snd_pcm_uframes_t offset = 0;
853 snd_pcm_sframes_t delay = 0;
855 if (platform->pcm_ops->pointer)
856 offset = platform->pcm_ops->pointer(substream);
858 if (cpu_dai->ops->delay)
859 delay += cpu_dai->ops->delay(substream, cpu_dai);
861 if (codec_dai->ops->delay)
862 delay += codec_dai->ops->delay(substream, codec_dai);
865 delay += platform->delay(substream, codec_dai);
867 runtime->delay = delay;
872 /* ASoC PCM operations */
873 static struct snd_pcm_ops soc_pcm_ops = {
874 .open = soc_pcm_open,
875 .close = soc_codec_close,
876 .hw_params = soc_pcm_hw_params,
877 .hw_free = soc_pcm_hw_free,
878 .prepare = soc_pcm_prepare,
879 .trigger = soc_pcm_trigger,
880 .pointer = soc_pcm_pointer,
884 /* powers down audio subsystem for suspend */
885 static int soc_suspend(struct device *dev)
887 struct platform_device *pdev = to_platform_device(dev);
888 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
889 struct snd_soc_card *card = socdev->card;
890 struct snd_soc_platform *platform = card->platform;
891 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
892 struct snd_soc_codec *codec = card->codec;
895 /* If the initialization of this soc device failed, there is no codec
896 * associated with it. Just bail out in this case.
901 /* Keep active state of codec when voice-call and skip suspend
902 * but, I2S/Audio Machine driver execute suspend() to reduce current power. */
903 if (codec->enable_on_suspend) {
904 for (i = 0; i < card->num_links; i++) {
905 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
906 if (card->dai_link[i].ignore_suspend)
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]);
917 /* If we use iDMA, do not get into suspend process. */
918 for (i = 0; i < card->num_links; i++)
919 if (card->dai_link[i].cpu_dai->use_idma)
922 /* Due to the resume being scheduled into a workqueue we could
923 * suspend before that's finished - wait for it to complete.
925 snd_power_lock(codec->card);
926 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
927 snd_power_unlock(codec->card);
929 /* we're going to block userspace touching us until resume completes */
930 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
932 /* mute any active DAC's */
933 for (i = 0; i < card->num_links; i++) {
934 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
936 if (card->dai_link[i].ignore_suspend)
939 if (dai->ops->digital_mute && dai->playback.active)
940 dai->ops->digital_mute(dai, 1);
943 /* suspend all pcms */
944 for (i = 0; i < card->num_links; i++) {
945 if (card->dai_link[i].ignore_suspend)
948 snd_pcm_suspend_all(card->dai_link[i].pcm);
951 if (card->suspend_pre)
952 card->suspend_pre(pdev, PMSG_SUSPEND);
954 for (i = 0; i < card->num_links; i++) {
955 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
957 if (card->dai_link[i].ignore_suspend)
960 if (cpu_dai->suspend && !cpu_dai->ac97_control)
961 cpu_dai->suspend(cpu_dai);
962 if (platform->suspend)
963 platform->suspend(&card->dai_link[i]);
966 /* close any waiting streams and save state */
967 run_delayed_work(&card->delayed_work);
968 codec->suspend_bias_level = codec->bias_level;
970 for (i = 0; i < codec->num_dai; i++) {
971 char *stream = codec->dai[i].playback.stream_name;
973 if (card->dai_link[i].ignore_suspend)
977 snd_soc_dapm_stream_event(codec, stream,
978 SND_SOC_DAPM_STREAM_SUSPEND);
979 stream = codec->dai[i].capture.stream_name;
981 snd_soc_dapm_stream_event(codec, stream,
982 SND_SOC_DAPM_STREAM_SUSPEND);
985 /* If there are paths active then the CODEC will be held with
986 * bias _ON and should not be suspended. */
987 if (codec_dev->suspend) {
988 switch (codec->bias_level) {
989 case SND_SOC_BIAS_STANDBY:
990 case SND_SOC_BIAS_OFF:
991 codec_dev->suspend(pdev, PMSG_SUSPEND);
994 dev_dbg(socdev->dev, "CODEC is on over suspend\n");
999 for (i = 0; i < card->num_links; i++) {
1000 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1002 if (card->dai_link[i].ignore_suspend)
1005 if (cpu_dai->suspend && cpu_dai->ac97_control)
1006 cpu_dai->suspend(cpu_dai);
1009 if (card->suspend_post)
1010 card->suspend_post(pdev, PMSG_SUSPEND);
1015 /* deferred resume work, so resume can complete before we finished
1016 * setting our codec back up, which can be very slow on I2C
1018 static void soc_resume_deferred(struct work_struct *work)
1020 struct snd_soc_card *card = container_of(work,
1021 struct snd_soc_card,
1022 deferred_resume_work);
1023 struct snd_soc_device *socdev = card->socdev;
1024 struct snd_soc_platform *platform = card->platform;
1025 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1026 struct snd_soc_codec *codec = card->codec;
1027 struct platform_device *pdev = to_platform_device(socdev->dev);
1030 /* Keep active state of codec when voice-call and skip resume
1031 * but, I2S/Audio Machine driver execute resume() to reduce current power. */
1032 if (codec->enable_on_suspend) {
1033 for (i = 0; i < card->num_links; i++) {
1034 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1036 if (card->dai_link[i].ignore_suspend)
1039 if (cpu_dai->resume && !cpu_dai->ac97_control)
1040 cpu_dai->resume(cpu_dai);
1041 if (platform->resume)
1042 platform->resume(&card->dai_link[i]);
1047 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1048 * so userspace apps are blocked from touching us
1051 dev_dbg(socdev->dev, "starting resume work\n");
1053 /* If we use iDMA, do not get into resume process. */
1054 for (i = 0; i < card->num_links; i++)
1055 if (card->dai_link[i].cpu_dai->use_idma)
1058 /* Bring us up into D2 so that DAPM starts enabling things */
1059 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D2);
1061 if (card->resume_pre)
1062 card->resume_pre(pdev);
1064 for (i = 0; i < card->num_links; i++) {
1065 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1067 if (card->dai_link[i].ignore_suspend)
1070 if (cpu_dai->resume && cpu_dai->ac97_control)
1071 cpu_dai->resume(cpu_dai);
1074 /* If the CODEC was idle over suspend then it will have been
1075 * left with bias OFF or STANDBY and suspended so we must now
1076 * resume. Otherwise the suspend was suppressed.
1078 if (codec_dev->resume) {
1079 switch (codec->bias_level) {
1080 case SND_SOC_BIAS_STANDBY:
1081 case SND_SOC_BIAS_OFF:
1082 codec_dev->resume(pdev);
1085 dev_dbg(socdev->dev, "CODEC was on over suspend\n");
1090 for (i = 0; i < codec->num_dai; i++) {
1091 char *stream = codec->dai[i].playback.stream_name;
1093 if (card->dai_link[i].ignore_suspend)
1097 snd_soc_dapm_stream_event(codec, stream,
1098 SND_SOC_DAPM_STREAM_RESUME);
1099 stream = codec->dai[i].capture.stream_name;
1101 snd_soc_dapm_stream_event(codec, stream,
1102 SND_SOC_DAPM_STREAM_RESUME);
1105 /* unmute any active DACs */
1106 for (i = 0; i < card->num_links; i++) {
1107 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
1109 if (card->dai_link[i].ignore_suspend)
1112 if (dai->ops->digital_mute && dai->playback.active)
1113 dai->ops->digital_mute(dai, 0);
1116 for (i = 0; i < card->num_links; i++) {
1117 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1119 if (card->dai_link[i].ignore_suspend)
1122 if (cpu_dai->resume && !cpu_dai->ac97_control)
1123 cpu_dai->resume(cpu_dai);
1124 if (platform->resume)
1125 platform->resume(&card->dai_link[i]);
1128 if (card->resume_post)
1129 card->resume_post(pdev);
1131 dev_dbg(socdev->dev, "resume work completed\n");
1133 /* userspace can access us now we are back as we were before */
1134 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
1137 /* powers up audio subsystem after a suspend */
1138 static int soc_resume(struct device *dev)
1140 struct platform_device *pdev = to_platform_device(dev);
1141 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1142 struct snd_soc_card *card = socdev->card;
1143 struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai;
1145 /* If the initialization of this soc device failed, there is no codec
1146 * associated with it. Just bail out in this case.
1151 /* AC97 devices might have other drivers hanging off them so
1152 * need to resume immediately. Other drivers don't have that
1153 * problem and may take a substantial amount of time to resume
1154 * due to I/O costs and anti-pop so handle them out of line.
1156 if (cpu_dai->ac97_control) {
1157 dev_dbg(socdev->dev, "Resuming AC97 immediately\n");
1158 soc_resume_deferred(&card->deferred_resume_work);
1160 dev_dbg(socdev->dev, "Scheduling resume work\n");
1161 if (!schedule_work(&card->deferred_resume_work))
1162 dev_err(socdev->dev, "resume work item may be lost\n");
1168 /* check if the deferred resume work is complete. if not, wait for it. */
1169 static void soc_resume_complete(struct device *dev)
1171 struct platform_device *pdev = to_platform_device(dev);
1172 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1173 struct snd_soc_card *card = socdev->card;
1174 struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai;
1176 /* We've scheduled something */
1177 if (!cpu_dai->ac97_control)
1178 flush_work(&card->deferred_resume_work);
1181 #define soc_suspend NULL
1182 #define soc_resume NULL
1183 #define soc_resume_complete NULL
1186 static struct snd_soc_dai_ops null_dai_ops = {
1189 static void snd_soc_instantiate_card(struct snd_soc_card *card)
1191 struct platform_device *pdev = container_of(card->dev,
1192 struct platform_device,
1194 struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
1195 struct snd_soc_codec *codec;
1196 struct snd_soc_platform *platform;
1197 struct snd_soc_dai *dai;
1198 int i, found, ret, ac97;
1200 if (card->instantiated)
1204 list_for_each_entry(platform, &platform_list, list)
1205 if (card->platform == platform) {
1210 dev_dbg(card->dev, "Platform %s not registered\n",
1211 card->platform->name);
1216 for (i = 0; i < card->num_links; i++) {
1218 list_for_each_entry(dai, &dai_list, list)
1219 if (card->dai_link[i].cpu_dai == dai) {
1224 dev_dbg(card->dev, "DAI %s not registered\n",
1225 card->dai_link[i].cpu_dai->name);
1229 if (card->dai_link[i].cpu_dai->ac97_control)
1233 for (i = 0; i < card->num_links; i++) {
1234 if (!card->dai_link[i].codec_dai->ops)
1235 card->dai_link[i].codec_dai->ops = &null_dai_ops;
1238 /* If we have AC97 in the system then don't wait for the
1239 * codec. This will need revisiting if we have to handle
1240 * systems with mixed AC97 and non-AC97 parts. Only check for
1241 * DAIs currently; we can't do this per link since some AC97
1242 * codecs have non-AC97 DAIs.
1245 for (i = 0; i < card->num_links; i++) {
1247 list_for_each_entry(dai, &dai_list, list)
1248 if (card->dai_link[i].codec_dai == dai) {
1253 dev_dbg(card->dev, "DAI %s not registered\n",
1254 card->dai_link[i].codec_dai->name);
1259 /* Note that we do not current check for codec components */
1261 dev_dbg(card->dev, "All components present, instantiating\n");
1263 /* Found everything, bring it up */
1264 card->pmdown_time = pmdown_time;
1267 ret = card->probe(pdev);
1272 for (i = 0; i < card->num_links; i++) {
1273 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1274 if (cpu_dai->probe) {
1275 ret = cpu_dai->probe(pdev, cpu_dai);
1281 if (codec_dev->probe) {
1282 ret = codec_dev->probe(pdev);
1286 codec = card->codec;
1288 if (platform->probe) {
1289 ret = platform->probe(pdev);
1294 /* DAPM stream work */
1295 INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
1297 /* deferred resume work */
1298 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1301 for (i = 0; i < card->num_links; i++) {
1302 if (card->dai_link[i].init) {
1303 ret = card->dai_link[i].init(codec);
1305 printk(KERN_ERR "asoc: failed to init %s\n",
1306 card->dai_link[i].stream_name);
1310 if (card->dai_link[i].codec_dai->ac97_control)
1314 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1316 snprintf(codec->card->longname, sizeof(codec->card->longname),
1317 "%s (%s)", card->name, codec->name);
1319 /* Make sure all DAPM widgets are instantiated */
1320 snd_soc_dapm_new_widgets(codec);
1322 ret = snd_card_register(codec->card);
1324 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1329 mutex_lock(&codec->mutex);
1330 #ifdef CONFIG_SND_SOC_AC97_BUS
1331 /* Only instantiate AC97 if not already done by the adaptor
1332 * for the generic AC97 subsystem.
1334 if (ac97 && strcmp(codec->name, "AC97") != 0) {
1335 ret = soc_ac97_dev_register(codec);
1337 printk(KERN_ERR "asoc: AC97 device register failed\n");
1338 snd_card_free(codec->card);
1339 mutex_unlock(&codec->mutex);
1345 ret = snd_soc_dapm_sys_add(card->socdev->dev);
1347 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1349 ret = device_create_file(card->socdev->dev, &dev_attr_pmdown_time);
1351 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1353 ret = device_create_file(card->socdev->dev, &dev_attr_codec_reg);
1355 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1357 soc_init_codec_debugfs(codec);
1358 mutex_unlock(&codec->mutex);
1360 card->instantiated = 1;
1365 if (platform->remove)
1366 platform->remove(pdev);
1369 if (codec_dev->remove)
1370 codec_dev->remove(pdev);
1373 for (i--; i >= 0; i--) {
1374 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1375 if (cpu_dai->remove)
1376 cpu_dai->remove(pdev, cpu_dai);
1384 * Attempt to initialise any uninitialised cards. Must be called with
1387 static void snd_soc_instantiate_cards(void)
1389 struct snd_soc_card *card;
1390 list_for_each_entry(card, &card_list, list)
1391 snd_soc_instantiate_card(card);
1394 /* probes a new socdev */
1395 static int soc_probe(struct platform_device *pdev)
1398 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1399 struct snd_soc_card *card = socdev->card;
1401 /* Bodge while we push things out of socdev */
1402 card->socdev = socdev;
1404 /* Bodge while we unpick instantiation */
1405 card->dev = &pdev->dev;
1406 ret = snd_soc_register_card(card);
1408 dev_err(&pdev->dev, "Failed to register card\n");
1415 /* removes a socdev */
1416 static int soc_remove(struct platform_device *pdev)
1419 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1420 struct snd_soc_card *card = socdev->card;
1421 struct snd_soc_platform *platform = card->platform;
1422 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1424 if (card->instantiated) {
1425 run_delayed_work(&card->delayed_work);
1427 if (platform->remove)
1428 platform->remove(pdev);
1430 if (codec_dev->remove)
1431 codec_dev->remove(pdev);
1433 for (i = 0; i < card->num_links; i++) {
1434 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1435 if (cpu_dai->remove)
1436 cpu_dai->remove(pdev, cpu_dai);
1443 snd_soc_unregister_card(card);
1448 static int soc_poweroff(struct device *dev)
1450 struct platform_device *pdev = to_platform_device(dev);
1451 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1452 struct snd_soc_card *card = socdev->card;
1454 if (!card->instantiated)
1457 /* Flush out pmdown_time work - we actually do want to run it
1458 * now, we're shutting down so no imminent restart. */
1459 run_delayed_work(&card->delayed_work);
1461 snd_soc_dapm_shutdown(socdev);
1466 static const struct dev_pm_ops soc_pm_ops = {
1467 .suspend = soc_suspend,
1468 .resume = soc_resume,
1469 .complete = soc_resume_complete,
1470 .poweroff = soc_poweroff,
1471 #ifdef CONFIG_HIBERNATION
1472 .freeze = soc_suspend,
1473 .restore = soc_resume,
1477 /* ASoC platform driver */
1478 static struct platform_driver soc_driver = {
1480 .name = "soc-audio",
1481 .owner = THIS_MODULE,
1485 .remove = soc_remove,
1488 /* create a new pcm */
1489 static int soc_new_pcm(struct snd_soc_device *socdev,
1490 struct snd_soc_dai_link *dai_link, int num)
1492 struct snd_soc_card *card = socdev->card;
1493 struct snd_soc_codec *codec = card->codec;
1494 struct snd_soc_platform *platform = card->platform;
1495 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
1496 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
1497 struct snd_soc_pcm_runtime *rtd;
1498 struct snd_pcm *pcm;
1500 int ret = 0, playback = 0, capture = 0;
1502 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1506 rtd->dai = dai_link;
1507 rtd->socdev = socdev;
1508 codec_dai->codec = card->codec;
1510 /* check client and interface hw capabilities */
1511 snprintf(new_name, sizeof(new_name), "%s %s-%d",
1512 dai_link->stream_name, codec_dai->name, num);
1514 if (codec_dai->playback.channels_min)
1516 if (codec_dai->capture.channels_min)
1519 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1522 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
1528 dai_link->pcm = pcm;
1529 pcm->private_data = rtd;
1530 soc_pcm_ops.mmap = platform->pcm_ops->mmap;
1531 soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
1532 soc_pcm_ops.copy = platform->pcm_ops->copy;
1533 soc_pcm_ops.silence = platform->pcm_ops->silence;
1534 soc_pcm_ops.ack = platform->pcm_ops->ack;
1535 soc_pcm_ops.page = platform->pcm_ops->page;
1538 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1541 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1543 ret = platform->pcm_new(codec->card, codec_dai, pcm);
1545 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1550 pcm->private_free = platform->pcm_free;
1551 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1557 * snd_soc_codec_volatile_register: Report if a register is volatile.
1559 * @codec: CODEC to query.
1560 * @reg: Register to query.
1562 * Boolean function indiciating if a CODEC register is volatile.
1564 int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1566 if (codec->volatile_register)
1567 return codec->volatile_register(reg);
1571 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1574 * snd_soc_new_ac97_codec - initailise AC97 device
1575 * @codec: audio codec
1576 * @ops: AC97 bus operations
1577 * @num: AC97 codec number
1579 * Initialises AC97 codec resources for use by ad-hoc devices only.
1581 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1582 struct snd_ac97_bus_ops *ops, int num)
1584 mutex_lock(&codec->mutex);
1586 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1587 if (codec->ac97 == NULL) {
1588 mutex_unlock(&codec->mutex);
1592 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1593 if (codec->ac97->bus == NULL) {
1596 mutex_unlock(&codec->mutex);
1600 codec->ac97->bus->ops = ops;
1601 codec->ac97->num = num;
1602 codec->dev = &codec->ac97->dev;
1603 mutex_unlock(&codec->mutex);
1606 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1609 * snd_soc_free_ac97_codec - free AC97 codec device
1610 * @codec: audio codec
1612 * Frees AC97 codec device resources.
1614 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1616 mutex_lock(&codec->mutex);
1617 kfree(codec->ac97->bus);
1620 mutex_unlock(&codec->mutex);
1622 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1625 * snd_soc_update_bits - update codec register bits
1626 * @codec: audio codec
1627 * @reg: codec register
1628 * @mask: register mask
1631 * Writes new register value.
1633 * Returns 1 for change else 0.
1635 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1636 unsigned int mask, unsigned int value)
1639 unsigned int old, new;
1641 old = snd_soc_read(codec, reg);
1642 new = (old & ~mask) | value;
1643 change = old != new;
1645 snd_soc_write(codec, reg, new);
1649 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1652 * snd_soc_update_bits_locked - update codec register bits
1653 * @codec: audio codec
1654 * @reg: codec register
1655 * @mask: register mask
1658 * Writes new register value, and takes the codec mutex.
1660 * Returns 1 for change else 0.
1662 int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1663 unsigned short reg, unsigned int mask,
1668 mutex_lock(&codec->mutex);
1669 change = snd_soc_update_bits(codec, reg, mask, value);
1670 mutex_unlock(&codec->mutex);
1674 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
1677 * snd_soc_test_bits - test register for change
1678 * @codec: audio codec
1679 * @reg: codec register
1680 * @mask: register mask
1683 * Tests a register with a new value and checks if the new value is
1684 * different from the old value.
1686 * Returns 1 for change else 0.
1688 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1689 unsigned int mask, unsigned int value)
1692 unsigned int old, new;
1694 old = snd_soc_read(codec, reg);
1695 new = (old & ~mask) | value;
1696 change = old != new;
1700 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1703 * snd_soc_new_pcms - create new sound card and pcms
1704 * @socdev: the SoC audio device
1705 * @idx: ALSA card index
1706 * @xid: card identification
1708 * Create a new sound card based upon the codec and interface pcms.
1710 * Returns 0 for success, else error.
1712 int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
1714 struct snd_soc_card *card = socdev->card;
1715 struct snd_soc_codec *codec = card->codec;
1718 mutex_lock(&codec->mutex);
1720 /* register a sound card */
1721 ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
1723 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1725 mutex_unlock(&codec->mutex);
1729 codec->socdev = socdev;
1730 codec->card->dev = socdev->dev;
1731 codec->card->private_data = codec;
1732 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1734 /* create the pcms */
1735 for (i = 0; i < card->num_links; i++) {
1736 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
1738 printk(KERN_ERR "asoc: can't create pcm %s\n",
1739 card->dai_link[i].stream_name);
1740 mutex_unlock(&codec->mutex);
1743 /* Check for codec->ac97 to handle the ac97.c fun */
1744 if (card->dai_link[i].codec_dai->ac97_control && codec->ac97) {
1745 snd_ac97_dev_add_pdata(codec->ac97,
1746 card->dai_link[i].cpu_dai->ac97_pdata);
1750 mutex_unlock(&codec->mutex);
1753 EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1756 * snd_soc_free_pcms - free sound card and pcms
1757 * @socdev: the SoC audio device
1759 * Frees sound card and pcms associated with the socdev.
1760 * Also unregister the codec if it is an AC97 device.
1762 void snd_soc_free_pcms(struct snd_soc_device *socdev)
1764 struct snd_soc_codec *codec = socdev->card->codec;
1765 #ifdef CONFIG_SND_SOC_AC97_BUS
1766 struct snd_soc_dai *codec_dai;
1770 mutex_lock(&codec->mutex);
1771 soc_cleanup_codec_debugfs(codec);
1772 #ifdef CONFIG_SND_SOC_AC97_BUS
1773 for (i = 0; i < codec->num_dai; i++) {
1774 codec_dai = &codec->dai[i];
1775 if (codec_dai->ac97_control && codec->ac97 &&
1776 strcmp(codec->name, "AC97") != 0) {
1777 soc_ac97_dev_unregister(codec);
1785 snd_card_free(codec->card);
1786 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1787 mutex_unlock(&codec->mutex);
1789 EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1792 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1793 * @substream: the pcm substream
1794 * @hw: the hardware parameters
1796 * Sets the substream runtime hardware parameters.
1798 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1799 const struct snd_pcm_hardware *hw)
1801 struct snd_pcm_runtime *runtime = substream->runtime;
1802 runtime->hw.info = hw->info;
1803 runtime->hw.formats = hw->formats;
1804 runtime->hw.period_bytes_min = hw->period_bytes_min;
1805 runtime->hw.period_bytes_max = hw->period_bytes_max;
1806 runtime->hw.periods_min = hw->periods_min;
1807 runtime->hw.periods_max = hw->periods_max;
1808 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1809 runtime->hw.fifo_size = hw->fifo_size;
1812 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1815 * snd_soc_cnew - create new control
1816 * @_template: control template
1817 * @data: control private data
1818 * @long_name: control long name
1820 * Create a new mixer control from a template control.
1822 * Returns 0 for success, else error.
1824 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1825 void *data, char *long_name)
1827 struct snd_kcontrol_new template;
1829 memcpy(&template, _template, sizeof(template));
1831 template.name = long_name;
1834 return snd_ctl_new1(&template, data);
1836 EXPORT_SYMBOL_GPL(snd_soc_cnew);
1839 * snd_soc_add_controls - add an array of controls to a codec.
1840 * Convienience function to add a list of controls. Many codecs were
1841 * duplicating this code.
1843 * @codec: codec to add controls to
1844 * @controls: array of controls to add
1845 * @num_controls: number of elements in the array
1847 * Return 0 for success, else error.
1849 int snd_soc_add_controls(struct snd_soc_codec *codec,
1850 const struct snd_kcontrol_new *controls, int num_controls)
1852 struct snd_card *card = codec->card;
1855 for (i = 0; i < num_controls; i++) {
1856 const struct snd_kcontrol_new *control = &controls[i];
1857 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
1859 dev_err(codec->dev, "%s: Failed to add %s\n",
1860 codec->name, control->name);
1867 EXPORT_SYMBOL_GPL(snd_soc_add_controls);
1870 * snd_soc_info_enum_double - enumerated double mixer info callback
1871 * @kcontrol: mixer control
1872 * @uinfo: control element information
1874 * Callback to provide information about a double enumerated
1877 * Returns 0 for success.
1879 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1880 struct snd_ctl_elem_info *uinfo)
1882 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1884 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1885 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1886 uinfo->value.enumerated.items = e->max;
1888 if (uinfo->value.enumerated.item > e->max - 1)
1889 uinfo->value.enumerated.item = e->max - 1;
1890 strcpy(uinfo->value.enumerated.name,
1891 e->texts[uinfo->value.enumerated.item]);
1894 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1897 * snd_soc_get_enum_double - enumerated double mixer get callback
1898 * @kcontrol: mixer control
1899 * @ucontrol: control element information
1901 * Callback to get the value of a double enumerated mixer.
1903 * Returns 0 for success.
1905 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1906 struct snd_ctl_elem_value *ucontrol)
1908 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1909 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1910 unsigned int val, bitmask;
1912 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1914 val = snd_soc_read(codec, e->reg);
1915 ucontrol->value.enumerated.item[0]
1916 = (val >> e->shift_l) & (bitmask - 1);
1917 if (e->shift_l != e->shift_r)
1918 ucontrol->value.enumerated.item[1] =
1919 (val >> e->shift_r) & (bitmask - 1);
1923 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1926 * snd_soc_put_enum_double - enumerated double mixer put callback
1927 * @kcontrol: mixer control
1928 * @ucontrol: control element information
1930 * Callback to set the value of a double enumerated mixer.
1932 * Returns 0 for success.
1934 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1935 struct snd_ctl_elem_value *ucontrol)
1937 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1938 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1940 unsigned int mask, bitmask;
1942 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1944 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1946 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1947 mask = (bitmask - 1) << e->shift_l;
1948 if (e->shift_l != e->shift_r) {
1949 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1951 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1952 mask |= (bitmask - 1) << e->shift_r;
1955 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
1957 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1960 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
1961 * @kcontrol: mixer control
1962 * @ucontrol: control element information
1964 * Callback to get the value of a double semi enumerated mixer.
1966 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1967 * used for handling bitfield coded enumeration for example.
1969 * Returns 0 for success.
1971 int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
1972 struct snd_ctl_elem_value *ucontrol)
1974 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1975 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1976 unsigned int reg_val, val, mux;
1978 reg_val = snd_soc_read(codec, e->reg);
1979 val = (reg_val >> e->shift_l) & e->mask;
1980 for (mux = 0; mux < e->max; mux++) {
1981 if (val == e->values[mux])
1984 ucontrol->value.enumerated.item[0] = mux;
1985 if (e->shift_l != e->shift_r) {
1986 val = (reg_val >> e->shift_r) & e->mask;
1987 for (mux = 0; mux < e->max; mux++) {
1988 if (val == e->values[mux])
1991 ucontrol->value.enumerated.item[1] = mux;
1996 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
1999 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2000 * @kcontrol: mixer control
2001 * @ucontrol: control element information
2003 * Callback to set the value of a double semi enumerated mixer.
2005 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2006 * used for handling bitfield coded enumeration for example.
2008 * Returns 0 for success.
2010 int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2011 struct snd_ctl_elem_value *ucontrol)
2013 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2014 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2018 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2020 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2021 mask = e->mask << e->shift_l;
2022 if (e->shift_l != e->shift_r) {
2023 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2025 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2026 mask |= e->mask << e->shift_r;
2029 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2031 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2034 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2035 * @kcontrol: mixer control
2036 * @uinfo: control element information
2038 * Callback to provide information about an external enumerated
2041 * Returns 0 for success.
2043 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2044 struct snd_ctl_elem_info *uinfo)
2046 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2048 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2050 uinfo->value.enumerated.items = e->max;
2052 if (uinfo->value.enumerated.item > e->max - 1)
2053 uinfo->value.enumerated.item = e->max - 1;
2054 strcpy(uinfo->value.enumerated.name,
2055 e->texts[uinfo->value.enumerated.item]);
2058 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2061 * snd_soc_info_volsw_ext - external single mixer info callback
2062 * @kcontrol: mixer control
2063 * @uinfo: control element information
2065 * Callback to provide information about a single external mixer control.
2067 * Returns 0 for success.
2069 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2070 struct snd_ctl_elem_info *uinfo)
2072 int max = kcontrol->private_value;
2074 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2075 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2077 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2080 uinfo->value.integer.min = 0;
2081 uinfo->value.integer.max = max;
2084 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2087 * snd_soc_info_volsw - single mixer info callback
2088 * @kcontrol: mixer control
2089 * @uinfo: control element information
2091 * Callback to provide information about a single mixer control.
2093 * Returns 0 for success.
2095 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2096 struct snd_ctl_elem_info *uinfo)
2098 struct soc_mixer_control *mc =
2099 (struct soc_mixer_control *)kcontrol->private_value;
2101 unsigned int shift = mc->shift;
2102 unsigned int rshift = mc->rshift;
2104 if (!mc->platform_max)
2105 mc->platform_max = mc->max;
2106 platform_max = mc->platform_max;
2108 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2109 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2111 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2113 uinfo->count = shift == rshift ? 1 : 2;
2114 uinfo->value.integer.min = 0;
2115 uinfo->value.integer.max = platform_max;
2118 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2121 * snd_soc_get_volsw - single mixer get callback
2122 * @kcontrol: mixer control
2123 * @ucontrol: control element information
2125 * Callback to get the value of a single mixer control.
2127 * Returns 0 for success.
2129 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2130 struct snd_ctl_elem_value *ucontrol)
2132 struct soc_mixer_control *mc =
2133 (struct soc_mixer_control *)kcontrol->private_value;
2134 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2135 unsigned int reg = mc->reg;
2136 unsigned int shift = mc->shift;
2137 unsigned int rshift = mc->rshift;
2139 unsigned int mask = (1 << fls(max)) - 1;
2140 unsigned int invert = mc->invert;
2142 ucontrol->value.integer.value[0] =
2143 (snd_soc_read(codec, reg) >> shift) & mask;
2144 if (shift != rshift)
2145 ucontrol->value.integer.value[1] =
2146 (snd_soc_read(codec, reg) >> rshift) & mask;
2148 ucontrol->value.integer.value[0] =
2149 max - ucontrol->value.integer.value[0];
2150 if (shift != rshift)
2151 ucontrol->value.integer.value[1] =
2152 max - ucontrol->value.integer.value[1];
2157 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2160 * snd_soc_put_volsw - single mixer put callback
2161 * @kcontrol: mixer control
2162 * @ucontrol: control element information
2164 * Callback to set the value of a single mixer control.
2166 * Returns 0 for success.
2168 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2169 struct snd_ctl_elem_value *ucontrol)
2171 struct soc_mixer_control *mc =
2172 (struct soc_mixer_control *)kcontrol->private_value;
2173 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2174 unsigned int reg = mc->reg;
2175 unsigned int shift = mc->shift;
2176 unsigned int rshift = mc->rshift;
2178 unsigned int mask = (1 << fls(max)) - 1;
2179 unsigned int invert = mc->invert;
2180 unsigned int val, val2, val_mask;
2182 val = (ucontrol->value.integer.value[0] & mask);
2185 val_mask = mask << shift;
2187 if (shift != rshift) {
2188 val2 = (ucontrol->value.integer.value[1] & mask);
2191 val_mask |= mask << rshift;
2192 val |= val2 << rshift;
2194 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2196 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2199 * snd_soc_info_volsw_2r - double mixer info callback
2200 * @kcontrol: mixer control
2201 * @uinfo: control element information
2203 * Callback to provide information about a double mixer control that
2204 * spans 2 codec registers.
2206 * Returns 0 for success.
2208 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2209 struct snd_ctl_elem_info *uinfo)
2211 struct soc_mixer_control *mc =
2212 (struct soc_mixer_control *)kcontrol->private_value;
2215 if (!mc->platform_max)
2216 mc->platform_max = mc->max;
2217 platform_max = mc->platform_max;
2219 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2220 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2222 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2225 uinfo->value.integer.min = 0;
2226 uinfo->value.integer.max = platform_max;
2229 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2232 * snd_soc_get_volsw_2r - double mixer get callback
2233 * @kcontrol: mixer control
2234 * @ucontrol: control element information
2236 * Callback to get the value of a double mixer control that spans 2 registers.
2238 * Returns 0 for success.
2240 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2241 struct snd_ctl_elem_value *ucontrol)
2243 struct soc_mixer_control *mc =
2244 (struct soc_mixer_control *)kcontrol->private_value;
2245 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2246 unsigned int reg = mc->reg;
2247 unsigned int reg2 = mc->rreg;
2248 unsigned int shift = mc->shift;
2250 unsigned int mask = (1 << fls(max)) - 1;
2251 unsigned int invert = mc->invert;
2253 ucontrol->value.integer.value[0] =
2254 (snd_soc_read(codec, reg) >> shift) & mask;
2255 ucontrol->value.integer.value[1] =
2256 (snd_soc_read(codec, reg2) >> shift) & mask;
2258 ucontrol->value.integer.value[0] =
2259 max - ucontrol->value.integer.value[0];
2260 ucontrol->value.integer.value[1] =
2261 max - ucontrol->value.integer.value[1];
2266 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2269 * snd_soc_put_volsw_2r - double mixer set callback
2270 * @kcontrol: mixer control
2271 * @ucontrol: control element information
2273 * Callback to set the value of a double mixer control that spans 2 registers.
2275 * Returns 0 for success.
2277 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2278 struct snd_ctl_elem_value *ucontrol)
2280 struct soc_mixer_control *mc =
2281 (struct soc_mixer_control *)kcontrol->private_value;
2282 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2283 unsigned int reg = mc->reg;
2284 unsigned int reg2 = mc->rreg;
2285 unsigned int shift = mc->shift;
2287 unsigned int mask = (1 << fls(max)) - 1;
2288 unsigned int invert = mc->invert;
2290 unsigned int val, val2, val_mask;
2292 val_mask = mask << shift;
2293 val = (ucontrol->value.integer.value[0] & mask);
2294 val2 = (ucontrol->value.integer.value[1] & mask);
2302 val2 = val2 << shift;
2304 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2308 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2311 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2314 * snd_soc_info_volsw_s8 - signed mixer info callback
2315 * @kcontrol: mixer control
2316 * @uinfo: control element information
2318 * Callback to provide information about a signed mixer control.
2320 * Returns 0 for success.
2322 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2323 struct snd_ctl_elem_info *uinfo)
2325 struct soc_mixer_control *mc =
2326 (struct soc_mixer_control *)kcontrol->private_value;
2330 if (!mc->platform_max)
2331 mc->platform_max = mc->max;
2332 platform_max = mc->platform_max;
2334 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2336 uinfo->value.integer.min = 0;
2337 uinfo->value.integer.max = platform_max - min;
2340 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2343 * snd_soc_get_volsw_s8 - signed mixer get callback
2344 * @kcontrol: mixer control
2345 * @ucontrol: control element information
2347 * Callback to get the value of a signed mixer control.
2349 * Returns 0 for success.
2351 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2352 struct snd_ctl_elem_value *ucontrol)
2354 struct soc_mixer_control *mc =
2355 (struct soc_mixer_control *)kcontrol->private_value;
2356 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2357 unsigned int reg = mc->reg;
2359 int val = snd_soc_read(codec, reg);
2361 ucontrol->value.integer.value[0] =
2362 ((signed char)(val & 0xff))-min;
2363 ucontrol->value.integer.value[1] =
2364 ((signed char)((val >> 8) & 0xff))-min;
2367 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2370 * snd_soc_put_volsw_sgn - signed mixer put callback
2371 * @kcontrol: mixer control
2372 * @ucontrol: control element information
2374 * Callback to set the value of a signed mixer control.
2376 * Returns 0 for success.
2378 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2379 struct snd_ctl_elem_value *ucontrol)
2381 struct soc_mixer_control *mc =
2382 (struct soc_mixer_control *)kcontrol->private_value;
2383 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2384 unsigned int reg = mc->reg;
2388 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2389 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2391 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2393 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2396 * snd_soc_limit_volume - Set new limit to an existing volume control.
2398 * @codec: where to look for the control
2399 * @name: Name of the control
2400 * @max: new maximum limit
2402 * Return 0 for success, else error.
2404 int snd_soc_limit_volume(struct snd_soc_codec *codec,
2405 const char *name, int max)
2407 struct snd_card *card = codec->card;
2408 struct snd_kcontrol *kctl;
2409 struct soc_mixer_control *mc;
2413 /* Sanity check for name and max */
2414 if (unlikely(!name || max <= 0))
2417 list_for_each_entry(kctl, &card->controls, list) {
2418 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2424 mc = (struct soc_mixer_control *)kctl->private_value;
2425 if (max <= mc->max) {
2426 mc->platform_max = max;
2432 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2435 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2436 * mixer info callback
2437 * @kcontrol: mixer control
2438 * @uinfo: control element information
2440 * Returns 0 for success.
2442 int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2443 struct snd_ctl_elem_info *uinfo)
2445 struct soc_mixer_control *mc =
2446 (struct soc_mixer_control *)kcontrol->private_value;
2450 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2452 uinfo->value.integer.min = 0;
2453 uinfo->value.integer.max = max-min;
2457 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2460 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2461 * mixer get callback
2462 * @kcontrol: mixer control
2463 * @uinfo: control element information
2465 * Returns 0 for success.
2467 int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2468 struct snd_ctl_elem_value *ucontrol)
2470 struct soc_mixer_control *mc =
2471 (struct soc_mixer_control *)kcontrol->private_value;
2472 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2473 unsigned int mask = (1<<mc->shift)-1;
2475 int val = snd_soc_read(codec, mc->reg) & mask;
2476 int valr = snd_soc_read(codec, mc->rreg) & mask;
2478 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2479 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
2482 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2485 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2486 * mixer put callback
2487 * @kcontrol: mixer control
2488 * @uinfo: control element information
2490 * Returns 0 for success.
2492 int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2493 struct snd_ctl_elem_value *ucontrol)
2495 struct soc_mixer_control *mc =
2496 (struct soc_mixer_control *)kcontrol->private_value;
2497 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2498 unsigned int mask = (1<<mc->shift)-1;
2501 unsigned int val, valr, oval, ovalr;
2503 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2505 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2508 oval = snd_soc_read(codec, mc->reg) & mask;
2509 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2513 ret = snd_soc_write(codec, mc->reg, val);
2517 if (ovalr != valr) {
2518 ret = snd_soc_write(codec, mc->rreg, valr);
2525 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2528 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2530 * @clk_id: DAI specific clock ID
2531 * @freq: new clock frequency in Hz
2532 * @dir: new clock direction - input/output.
2534 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2536 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2537 unsigned int freq, int dir)
2539 if (dai->ops && dai->ops->set_sysclk)
2540 return dai->ops->set_sysclk(dai, clk_id, freq, dir);
2544 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2547 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2549 * @div_id: DAI specific clock divider ID
2550 * @div: new clock divisor.
2552 * Configures the clock dividers. This is used to derive the best DAI bit and
2553 * frame clocks from the system or master clock. It's best to set the DAI bit
2554 * and frame clocks as low as possible to save system power.
2556 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2557 int div_id, int div)
2559 if (dai->ops && dai->ops->set_clkdiv)
2560 return dai->ops->set_clkdiv(dai, div_id, div);
2564 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2567 * snd_soc_dai_set_pll - configure DAI PLL.
2569 * @pll_id: DAI specific PLL ID
2570 * @source: DAI specific source for the PLL
2571 * @freq_in: PLL input clock frequency in Hz
2572 * @freq_out: requested PLL output clock frequency in Hz
2574 * Configures and enables PLL to generate output clock based on input clock.
2576 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2577 unsigned int freq_in, unsigned int freq_out)
2579 if (dai->ops && dai->ops->set_pll)
2580 return dai->ops->set_pll(dai, pll_id, source,
2585 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2588 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2590 * @fmt: SND_SOC_DAIFMT_ format value.
2592 * Configures the DAI hardware format and clocking.
2594 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2596 if (dai->ops && dai->ops->set_fmt)
2597 return dai->ops->set_fmt(dai, fmt);
2601 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2604 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2606 * @tx_mask: bitmask representing active TX slots.
2607 * @rx_mask: bitmask representing active RX slots.
2608 * @slots: Number of slots in use.
2609 * @slot_width: Width in bits for each slot.
2611 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2614 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2615 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2617 if (dai->ops && dai->ops->set_tdm_slot)
2618 return dai->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2623 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2626 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2628 * @tx_num: how many TX channels
2629 * @tx_slot: pointer to an array which imply the TX slot number channel
2631 * @rx_num: how many RX channels
2632 * @rx_slot: pointer to an array which imply the RX slot number channel
2635 * configure the relationship between channel number and TDM slot number.
2637 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2638 unsigned int tx_num, unsigned int *tx_slot,
2639 unsigned int rx_num, unsigned int *rx_slot)
2641 if (dai->ops && dai->ops->set_channel_map)
2642 return dai->ops->set_channel_map(dai, tx_num, tx_slot,
2647 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2650 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2652 * @tristate: tristate enable
2654 * Tristates the DAI so that others can use it.
2656 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2658 if (dai->ops && dai->ops->set_tristate)
2659 return dai->ops->set_tristate(dai, tristate);
2663 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2666 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2668 * @mute: mute enable
2670 * Mutes the DAI DAC.
2672 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2674 if (dai->ops && dai->ops->digital_mute)
2675 return dai->ops->digital_mute(dai, mute);
2679 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2682 * snd_soc_register_card - Register a card with the ASoC core
2684 * @card: Card to register
2686 * Note that currently this is an internal only function: it will be
2687 * exposed to machine drivers after further backporting of ASoC v2
2688 * registration APIs.
2690 static int snd_soc_register_card(struct snd_soc_card *card)
2692 if (!card->name || !card->dev)
2695 INIT_LIST_HEAD(&card->list);
2696 card->instantiated = 0;
2698 mutex_lock(&client_mutex);
2699 list_add(&card->list, &card_list);
2700 snd_soc_instantiate_cards();
2701 mutex_unlock(&client_mutex);
2703 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2709 * snd_soc_unregister_card - Unregister a card with the ASoC core
2711 * @card: Card to unregister
2713 * Note that currently this is an internal only function: it will be
2714 * exposed to machine drivers after further backporting of ASoC v2
2715 * registration APIs.
2717 static int snd_soc_unregister_card(struct snd_soc_card *card)
2719 mutex_lock(&client_mutex);
2720 list_del(&card->list);
2721 mutex_unlock(&client_mutex);
2723 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2729 * snd_soc_register_dai - Register a DAI with the ASoC core
2731 * @dai: DAI to register
2733 int snd_soc_register_dai(struct snd_soc_dai *dai)
2738 /* The device should become mandatory over time */
2740 printk(KERN_WARNING "No device for DAI %s\n", dai->name);
2743 dai->ops = &null_dai_ops;
2745 INIT_LIST_HEAD(&dai->list);
2747 mutex_lock(&client_mutex);
2748 list_add(&dai->list, &dai_list);
2749 snd_soc_instantiate_cards();
2750 mutex_unlock(&client_mutex);
2752 pr_debug("Registered DAI '%s'\n", dai->name);
2756 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2759 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2761 * @dai: DAI to unregister
2763 void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2765 mutex_lock(&client_mutex);
2766 list_del(&dai->list);
2767 mutex_unlock(&client_mutex);
2769 pr_debug("Unregistered DAI '%s'\n", dai->name);
2771 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2774 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2776 * @dai: Array of DAIs to register
2777 * @count: Number of DAIs
2779 int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
2783 for (i = 0; i < count; i++) {
2784 ret = snd_soc_register_dai(&dai[i]);
2792 for (i--; i >= 0; i--)
2793 snd_soc_unregister_dai(&dai[i]);
2797 EXPORT_SYMBOL_GPL(snd_soc_register_dais);
2800 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
2802 * @dai: Array of DAIs to unregister
2803 * @count: Number of DAIs
2805 void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
2809 for (i = 0; i < count; i++)
2810 snd_soc_unregister_dai(&dai[i]);
2812 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
2815 * snd_soc_register_platform - Register a platform with the ASoC core
2817 * @platform: platform to register
2819 int snd_soc_register_platform(struct snd_soc_platform *platform)
2821 if (!platform->name)
2824 INIT_LIST_HEAD(&platform->list);
2826 mutex_lock(&client_mutex);
2827 list_add(&platform->list, &platform_list);
2828 snd_soc_instantiate_cards();
2829 mutex_unlock(&client_mutex);
2831 pr_debug("Registered platform '%s'\n", platform->name);
2835 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2838 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2840 * @platform: platform to unregister
2842 void snd_soc_unregister_platform(struct snd_soc_platform *platform)
2844 mutex_lock(&client_mutex);
2845 list_del(&platform->list);
2846 mutex_unlock(&client_mutex);
2848 pr_debug("Unregistered platform '%s'\n", platform->name);
2850 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2852 static u64 codec_format_map[] = {
2853 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2854 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2855 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2856 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2857 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2858 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2859 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2860 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2861 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2862 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2863 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2864 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2865 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2866 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2867 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2868 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2871 /* Fix up the DAI formats for endianness: codecs don't actually see
2872 * the endianness of the data but we're using the CPU format
2873 * definitions which do need to include endianness so we ensure that
2874 * codec DAIs always have both big and little endian variants set.
2876 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2880 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2881 if (stream->formats & codec_format_map[i])
2882 stream->formats |= codec_format_map[i];
2886 * snd_soc_register_codec - Register a codec with the ASoC core
2888 * @codec: codec to register
2890 int snd_soc_register_codec(struct snd_soc_codec *codec)
2897 /* The device should become mandatory over time */
2899 printk(KERN_WARNING "No device for codec %s\n", codec->name);
2901 INIT_LIST_HEAD(&codec->list);
2903 for (i = 0; i < codec->num_dai; i++) {
2904 fixup_codec_formats(&codec->dai[i].playback);
2905 fixup_codec_formats(&codec->dai[i].capture);
2908 mutex_lock(&client_mutex);
2909 list_add(&codec->list, &codec_list);
2910 snd_soc_instantiate_cards();
2911 mutex_unlock(&client_mutex);
2913 pr_debug("Registered codec '%s'\n", codec->name);
2917 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
2920 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
2922 * @codec: codec to unregister
2924 void snd_soc_unregister_codec(struct snd_soc_codec *codec)
2926 mutex_lock(&client_mutex);
2927 list_del(&codec->list);
2928 mutex_unlock(&client_mutex);
2930 pr_debug("Unregistered codec '%s'\n", codec->name);
2932 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
2934 static int __init snd_soc_init(void)
2936 #ifdef CONFIG_DEBUG_FS
2937 debugfs_root = debugfs_create_dir("asoc", NULL);
2938 if (IS_ERR(debugfs_root) || !debugfs_root) {
2940 "ASoC: Failed to create debugfs directory\n");
2941 debugfs_root = NULL;
2945 return platform_driver_register(&soc_driver);
2948 static void __exit snd_soc_exit(void)
2950 #ifdef CONFIG_DEBUG_FS
2951 debugfs_remove_recursive(debugfs_root);
2953 platform_driver_unregister(&soc_driver);
2956 module_init(snd_soc_init);
2957 module_exit(snd_soc_exit);
2959 /* Module information */
2960 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2961 MODULE_DESCRIPTION("ALSA SoC Core");
2962 MODULE_LICENSE("GPL");
2963 MODULE_ALIAS("platform:soc-audio");