ASoC: Move card list initialization to snd_soc_register_card
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / soc / soc-core.c
blob3feddd91b9734fe5e38e00e538242869b763ed68
1 /*
2 * soc-core.c -- ALSA SoC Audio Layer
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
18 * TODO:
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
29 #include <linux/pm.h>
30 #include <linux/bitops.h>
31 #include <linux/debugfs.h>
32 #include <linux/platform_device.h>
33 #include <linux/slab.h>
34 #include <sound/ac97_codec.h>
35 #include <sound/core.h>
36 #include <sound/jack.h>
37 #include <sound/pcm.h>
38 #include <sound/pcm_params.h>
39 #include <sound/soc.h>
40 #include <sound/initval.h>
42 #define CREATE_TRACE_POINTS
43 #include <trace/events/asoc.h>
45 #define NAME_SIZE 32
47 static DEFINE_MUTEX(pcm_mutex);
48 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
50 #ifdef CONFIG_DEBUG_FS
51 struct dentry *snd_soc_debugfs_root;
52 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
53 #endif
55 static DEFINE_MUTEX(client_mutex);
56 static LIST_HEAD(card_list);
57 static LIST_HEAD(dai_list);
58 static LIST_HEAD(platform_list);
59 static LIST_HEAD(codec_list);
61 static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
64 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
68 static int pmdown_time = 5000;
69 module_param(pmdown_time, int, 0);
70 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
72 /* codec register dump */
73 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
75 int ret, i, step = 1, count = 0;
77 if (!codec->driver->reg_cache_size)
78 return 0;
80 if (codec->driver->reg_cache_step)
81 step = codec->driver->reg_cache_step;
83 count += sprintf(buf, "%s registers\n", codec->name);
84 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
85 if (codec->readable_register && !codec->readable_register(codec, i))
86 continue;
88 count += sprintf(buf + count, "%2x: ", i);
89 if (count >= PAGE_SIZE - 1)
90 break;
92 if (codec->driver->display_register) {
93 count += codec->driver->display_register(codec, buf + count,
94 PAGE_SIZE - count, i);
95 } else {
96 /* If the read fails it's almost certainly due to
97 * the register being volatile and the device being
98 * powered off.
100 ret = snd_soc_read(codec, i);
101 if (ret >= 0)
102 count += snprintf(buf + count,
103 PAGE_SIZE - count,
104 "%4x", ret);
105 else
106 count += snprintf(buf + count,
107 PAGE_SIZE - count,
108 "<no data: %d>", ret);
111 if (count >= PAGE_SIZE - 1)
112 break;
114 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
115 if (count >= PAGE_SIZE - 1)
116 break;
119 /* Truncate count; min() would cause a warning */
120 if (count >= PAGE_SIZE)
121 count = PAGE_SIZE - 1;
123 return count;
125 static ssize_t codec_reg_show(struct device *dev,
126 struct device_attribute *attr, char *buf)
128 struct snd_soc_pcm_runtime *rtd =
129 container_of(dev, struct snd_soc_pcm_runtime, dev);
131 return soc_codec_reg_show(rtd->codec, buf);
134 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
136 static ssize_t pmdown_time_show(struct device *dev,
137 struct device_attribute *attr, char *buf)
139 struct snd_soc_pcm_runtime *rtd =
140 container_of(dev, struct snd_soc_pcm_runtime, dev);
142 return sprintf(buf, "%ld\n", rtd->pmdown_time);
145 static ssize_t pmdown_time_set(struct device *dev,
146 struct device_attribute *attr,
147 const char *buf, size_t count)
149 struct snd_soc_pcm_runtime *rtd =
150 container_of(dev, struct snd_soc_pcm_runtime, dev);
151 int ret;
153 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
154 if (ret)
155 return ret;
157 return count;
160 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
162 #ifdef CONFIG_DEBUG_FS
163 static int codec_reg_open_file(struct inode *inode, struct file *file)
165 file->private_data = inode->i_private;
166 return 0;
169 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
170 size_t count, loff_t *ppos)
172 ssize_t ret;
173 struct snd_soc_codec *codec = file->private_data;
174 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
175 if (!buf)
176 return -ENOMEM;
177 ret = soc_codec_reg_show(codec, buf);
178 if (ret >= 0)
179 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
180 kfree(buf);
181 return ret;
184 static ssize_t codec_reg_write_file(struct file *file,
185 const char __user *user_buf, size_t count, loff_t *ppos)
187 char buf[32];
188 int buf_size;
189 char *start = buf;
190 unsigned long reg, value;
191 int step = 1;
192 struct snd_soc_codec *codec = file->private_data;
194 buf_size = min(count, (sizeof(buf)-1));
195 if (copy_from_user(buf, user_buf, buf_size))
196 return -EFAULT;
197 buf[buf_size] = 0;
199 if (codec->driver->reg_cache_step)
200 step = codec->driver->reg_cache_step;
202 while (*start == ' ')
203 start++;
204 reg = simple_strtoul(start, &start, 16);
205 if ((reg >= codec->driver->reg_cache_size) || (reg % step))
206 return -EINVAL;
207 while (*start == ' ')
208 start++;
209 if (strict_strtoul(start, 16, &value))
210 return -EINVAL;
212 /* Userspace has been fiddling around behind the kernel's back */
213 add_taint(TAINT_USER);
215 snd_soc_write(codec, reg, value);
216 return buf_size;
219 static const struct file_operations codec_reg_fops = {
220 .open = codec_reg_open_file,
221 .read = codec_reg_read_file,
222 .write = codec_reg_write_file,
223 .llseek = default_llseek,
226 static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
228 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
230 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
231 debugfs_card_root);
232 if (!codec->debugfs_codec_root) {
233 printk(KERN_WARNING
234 "ASoC: Failed to create codec debugfs directory\n");
235 return;
238 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
239 &codec->cache_sync);
240 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
241 &codec->cache_only);
243 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
244 codec->debugfs_codec_root,
245 codec, &codec_reg_fops);
246 if (!codec->debugfs_reg)
247 printk(KERN_WARNING
248 "ASoC: Failed to create codec register debugfs file\n");
250 codec->dapm.debugfs_dapm = debugfs_create_dir("dapm",
251 codec->debugfs_codec_root);
252 if (!codec->dapm.debugfs_dapm)
253 printk(KERN_WARNING
254 "Failed to create DAPM debugfs directory\n");
256 snd_soc_dapm_debugfs_init(&codec->dapm);
259 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
261 debugfs_remove_recursive(codec->debugfs_codec_root);
264 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
265 size_t count, loff_t *ppos)
267 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
268 ssize_t len, ret = 0;
269 struct snd_soc_codec *codec;
271 if (!buf)
272 return -ENOMEM;
274 list_for_each_entry(codec, &codec_list, list) {
275 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
276 codec->name);
277 if (len >= 0)
278 ret += len;
279 if (ret > PAGE_SIZE) {
280 ret = PAGE_SIZE;
281 break;
285 if (ret >= 0)
286 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
288 kfree(buf);
290 return ret;
293 static const struct file_operations codec_list_fops = {
294 .read = codec_list_read_file,
295 .llseek = default_llseek,/* read accesses f_pos */
298 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
299 size_t count, loff_t *ppos)
301 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
302 ssize_t len, ret = 0;
303 struct snd_soc_dai *dai;
305 if (!buf)
306 return -ENOMEM;
308 list_for_each_entry(dai, &dai_list, list) {
309 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
310 if (len >= 0)
311 ret += len;
312 if (ret > PAGE_SIZE) {
313 ret = PAGE_SIZE;
314 break;
318 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
320 kfree(buf);
322 return ret;
325 static const struct file_operations dai_list_fops = {
326 .read = dai_list_read_file,
327 .llseek = default_llseek,/* read accesses f_pos */
330 static ssize_t platform_list_read_file(struct file *file,
331 char __user *user_buf,
332 size_t count, loff_t *ppos)
334 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
335 ssize_t len, ret = 0;
336 struct snd_soc_platform *platform;
338 if (!buf)
339 return -ENOMEM;
341 list_for_each_entry(platform, &platform_list, list) {
342 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
343 platform->name);
344 if (len >= 0)
345 ret += len;
346 if (ret > PAGE_SIZE) {
347 ret = PAGE_SIZE;
348 break;
352 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
354 kfree(buf);
356 return ret;
359 static const struct file_operations platform_list_fops = {
360 .read = platform_list_read_file,
361 .llseek = default_llseek,/* read accesses f_pos */
364 static void soc_init_card_debugfs(struct snd_soc_card *card)
366 card->debugfs_card_root = debugfs_create_dir(card->name,
367 snd_soc_debugfs_root);
368 if (!card->debugfs_card_root) {
369 dev_warn(card->dev,
370 "ASoC: Failed to create codec debugfs directory\n");
371 return;
374 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
375 card->debugfs_card_root,
376 &card->pop_time);
377 if (!card->debugfs_pop_time)
378 dev_warn(card->dev,
379 "Failed to create pop time debugfs file\n");
382 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
384 debugfs_remove_recursive(card->debugfs_card_root);
387 #else
389 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
393 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
397 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
401 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
404 #endif
406 #ifdef CONFIG_SND_SOC_AC97_BUS
407 /* unregister ac97 codec */
408 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
410 if (codec->ac97->dev.bus)
411 device_unregister(&codec->ac97->dev);
412 return 0;
415 /* stop no dev release warning */
416 static void soc_ac97_device_release(struct device *dev){}
418 /* register ac97 codec to bus */
419 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
421 int err;
423 codec->ac97->dev.bus = &ac97_bus_type;
424 codec->ac97->dev.parent = codec->card->dev;
425 codec->ac97->dev.release = soc_ac97_device_release;
427 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
428 codec->card->snd_card->number, 0, codec->name);
429 err = device_register(&codec->ac97->dev);
430 if (err < 0) {
431 snd_printk(KERN_ERR "Can't register ac97 bus\n");
432 codec->ac97->dev.bus = NULL;
433 return err;
435 return 0;
437 #endif
439 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
441 struct snd_soc_pcm_runtime *rtd = substream->private_data;
442 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
443 struct snd_soc_dai *codec_dai = rtd->codec_dai;
444 int ret;
446 if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
447 rtd->dai_link->symmetric_rates) {
448 dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
449 rtd->rate);
451 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
452 SNDRV_PCM_HW_PARAM_RATE,
453 rtd->rate,
454 rtd->rate);
455 if (ret < 0) {
456 dev_err(&rtd->dev,
457 "Unable to apply rate symmetry constraint: %d\n", ret);
458 return ret;
462 return 0;
466 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
467 * then initialized and any private data can be allocated. This also calls
468 * startup for the cpu DAI, platform, machine and codec DAI.
470 static int soc_pcm_open(struct snd_pcm_substream *substream)
472 struct snd_soc_pcm_runtime *rtd = substream->private_data;
473 struct snd_pcm_runtime *runtime = substream->runtime;
474 struct snd_soc_platform *platform = rtd->platform;
475 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
476 struct snd_soc_dai *codec_dai = rtd->codec_dai;
477 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
478 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
479 int ret = 0;
481 mutex_lock(&pcm_mutex);
483 /* startup the audio subsystem */
484 if (cpu_dai->driver->ops->startup) {
485 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
486 if (ret < 0) {
487 printk(KERN_ERR "asoc: can't open interface %s\n",
488 cpu_dai->name);
489 goto out;
493 if (platform->driver->ops->open) {
494 ret = platform->driver->ops->open(substream);
495 if (ret < 0) {
496 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
497 goto platform_err;
501 if (codec_dai->driver->ops->startup) {
502 ret = codec_dai->driver->ops->startup(substream, codec_dai);
503 if (ret < 0) {
504 printk(KERN_ERR "asoc: can't open codec %s\n",
505 codec_dai->name);
506 goto codec_dai_err;
510 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
511 ret = rtd->dai_link->ops->startup(substream);
512 if (ret < 0) {
513 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
514 goto machine_err;
518 /* Check that the codec and cpu DAIs are compatible */
519 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
520 runtime->hw.rate_min =
521 max(codec_dai_drv->playback.rate_min,
522 cpu_dai_drv->playback.rate_min);
523 runtime->hw.rate_max =
524 min(codec_dai_drv->playback.rate_max,
525 cpu_dai_drv->playback.rate_max);
526 runtime->hw.channels_min =
527 max(codec_dai_drv->playback.channels_min,
528 cpu_dai_drv->playback.channels_min);
529 runtime->hw.channels_max =
530 min(codec_dai_drv->playback.channels_max,
531 cpu_dai_drv->playback.channels_max);
532 runtime->hw.formats =
533 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
534 runtime->hw.rates =
535 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
536 if (codec_dai_drv->playback.rates
537 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
538 runtime->hw.rates |= cpu_dai_drv->playback.rates;
539 if (cpu_dai_drv->playback.rates
540 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
541 runtime->hw.rates |= codec_dai_drv->playback.rates;
542 } else {
543 runtime->hw.rate_min =
544 max(codec_dai_drv->capture.rate_min,
545 cpu_dai_drv->capture.rate_min);
546 runtime->hw.rate_max =
547 min(codec_dai_drv->capture.rate_max,
548 cpu_dai_drv->capture.rate_max);
549 runtime->hw.channels_min =
550 max(codec_dai_drv->capture.channels_min,
551 cpu_dai_drv->capture.channels_min);
552 runtime->hw.channels_max =
553 min(codec_dai_drv->capture.channels_max,
554 cpu_dai_drv->capture.channels_max);
555 runtime->hw.formats =
556 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
557 runtime->hw.rates =
558 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
559 if (codec_dai_drv->capture.rates
560 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
561 runtime->hw.rates |= cpu_dai_drv->capture.rates;
562 if (cpu_dai_drv->capture.rates
563 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
564 runtime->hw.rates |= codec_dai_drv->capture.rates;
567 snd_pcm_limit_hw_rates(runtime);
568 if (!runtime->hw.rates) {
569 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
570 codec_dai->name, cpu_dai->name);
571 goto config_err;
573 if (!runtime->hw.formats) {
574 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
575 codec_dai->name, cpu_dai->name);
576 goto config_err;
578 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
579 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
580 codec_dai->name, cpu_dai->name);
581 goto config_err;
584 /* Symmetry only applies if we've already got an active stream. */
585 if (cpu_dai->active || codec_dai->active) {
586 ret = soc_pcm_apply_symmetry(substream);
587 if (ret != 0)
588 goto config_err;
591 pr_debug("asoc: %s <-> %s info:\n",
592 codec_dai->name, cpu_dai->name);
593 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
594 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
595 runtime->hw.channels_max);
596 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
597 runtime->hw.rate_max);
599 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
600 cpu_dai->playback_active++;
601 codec_dai->playback_active++;
602 } else {
603 cpu_dai->capture_active++;
604 codec_dai->capture_active++;
606 cpu_dai->active++;
607 codec_dai->active++;
608 rtd->codec->active++;
609 mutex_unlock(&pcm_mutex);
610 return 0;
612 config_err:
613 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
614 rtd->dai_link->ops->shutdown(substream);
616 machine_err:
617 if (codec_dai->driver->ops->shutdown)
618 codec_dai->driver->ops->shutdown(substream, codec_dai);
620 codec_dai_err:
621 if (platform->driver->ops->close)
622 platform->driver->ops->close(substream);
624 platform_err:
625 if (cpu_dai->driver->ops->shutdown)
626 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
627 out:
628 mutex_unlock(&pcm_mutex);
629 return ret;
633 * Power down the audio subsystem pmdown_time msecs after close is called.
634 * This is to ensure there are no pops or clicks in between any music tracks
635 * due to DAPM power cycling.
637 static void close_delayed_work(struct work_struct *work)
639 struct snd_soc_pcm_runtime *rtd =
640 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
641 struct snd_soc_dai *codec_dai = rtd->codec_dai;
643 mutex_lock(&pcm_mutex);
645 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
646 codec_dai->driver->playback.stream_name,
647 codec_dai->playback_active ? "active" : "inactive",
648 codec_dai->pop_wait ? "yes" : "no");
650 /* are we waiting on this codec DAI stream */
651 if (codec_dai->pop_wait == 1) {
652 codec_dai->pop_wait = 0;
653 snd_soc_dapm_stream_event(rtd,
654 codec_dai->driver->playback.stream_name,
655 SND_SOC_DAPM_STREAM_STOP);
658 mutex_unlock(&pcm_mutex);
662 * Called by ALSA when a PCM substream is closed. Private data can be
663 * freed here. The cpu DAI, codec DAI, machine and platform are also
664 * shutdown.
666 static int soc_codec_close(struct snd_pcm_substream *substream)
668 struct snd_soc_pcm_runtime *rtd = substream->private_data;
669 struct snd_soc_platform *platform = rtd->platform;
670 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
671 struct snd_soc_dai *codec_dai = rtd->codec_dai;
672 struct snd_soc_codec *codec = rtd->codec;
674 mutex_lock(&pcm_mutex);
676 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
677 cpu_dai->playback_active--;
678 codec_dai->playback_active--;
679 } else {
680 cpu_dai->capture_active--;
681 codec_dai->capture_active--;
684 cpu_dai->active--;
685 codec_dai->active--;
686 codec->active--;
688 /* Muting the DAC suppresses artifacts caused during digital
689 * shutdown, for example from stopping clocks.
691 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
692 snd_soc_dai_digital_mute(codec_dai, 1);
694 if (cpu_dai->driver->ops->shutdown)
695 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
697 if (codec_dai->driver->ops->shutdown)
698 codec_dai->driver->ops->shutdown(substream, codec_dai);
700 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
701 rtd->dai_link->ops->shutdown(substream);
703 if (platform->driver->ops->close)
704 platform->driver->ops->close(substream);
705 cpu_dai->runtime = NULL;
707 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
708 /* start delayed pop wq here for playback streams */
709 codec_dai->pop_wait = 1;
710 schedule_delayed_work(&rtd->delayed_work,
711 msecs_to_jiffies(rtd->pmdown_time));
712 } else {
713 /* capture streams can be powered down now */
714 snd_soc_dapm_stream_event(rtd,
715 codec_dai->driver->capture.stream_name,
716 SND_SOC_DAPM_STREAM_STOP);
719 mutex_unlock(&pcm_mutex);
720 return 0;
724 * Called by ALSA when the PCM substream is prepared, can set format, sample
725 * rate, etc. This function is non atomic and can be called multiple times,
726 * it can refer to the runtime info.
728 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
730 struct snd_soc_pcm_runtime *rtd = substream->private_data;
731 struct snd_soc_platform *platform = rtd->platform;
732 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
733 struct snd_soc_dai *codec_dai = rtd->codec_dai;
734 int ret = 0;
736 mutex_lock(&pcm_mutex);
738 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
739 ret = rtd->dai_link->ops->prepare(substream);
740 if (ret < 0) {
741 printk(KERN_ERR "asoc: machine prepare error\n");
742 goto out;
746 if (platform->driver->ops->prepare) {
747 ret = platform->driver->ops->prepare(substream);
748 if (ret < 0) {
749 printk(KERN_ERR "asoc: platform prepare error\n");
750 goto out;
754 if (codec_dai->driver->ops->prepare) {
755 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
756 if (ret < 0) {
757 printk(KERN_ERR "asoc: codec DAI prepare error\n");
758 goto out;
762 if (cpu_dai->driver->ops->prepare) {
763 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
764 if (ret < 0) {
765 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
766 goto out;
770 /* cancel any delayed stream shutdown that is pending */
771 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
772 codec_dai->pop_wait) {
773 codec_dai->pop_wait = 0;
774 cancel_delayed_work(&rtd->delayed_work);
777 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
778 snd_soc_dapm_stream_event(rtd,
779 codec_dai->driver->playback.stream_name,
780 SND_SOC_DAPM_STREAM_START);
781 else
782 snd_soc_dapm_stream_event(rtd,
783 codec_dai->driver->capture.stream_name,
784 SND_SOC_DAPM_STREAM_START);
786 snd_soc_dai_digital_mute(codec_dai, 0);
788 out:
789 mutex_unlock(&pcm_mutex);
790 return ret;
794 * Called by ALSA when the hardware params are set by application. This
795 * function can also be called multiple times and can allocate buffers
796 * (using snd_pcm_lib_* ). It's non-atomic.
798 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
799 struct snd_pcm_hw_params *params)
801 struct snd_soc_pcm_runtime *rtd = substream->private_data;
802 struct snd_soc_platform *platform = rtd->platform;
803 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
804 struct snd_soc_dai *codec_dai = rtd->codec_dai;
805 int ret = 0;
807 mutex_lock(&pcm_mutex);
809 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
810 ret = rtd->dai_link->ops->hw_params(substream, params);
811 if (ret < 0) {
812 printk(KERN_ERR "asoc: machine hw_params failed\n");
813 goto out;
817 if (codec_dai->driver->ops->hw_params) {
818 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
819 if (ret < 0) {
820 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
821 codec_dai->name);
822 goto codec_err;
826 if (cpu_dai->driver->ops->hw_params) {
827 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
828 if (ret < 0) {
829 printk(KERN_ERR "asoc: interface %s hw params failed\n",
830 cpu_dai->name);
831 goto interface_err;
835 if (platform->driver->ops->hw_params) {
836 ret = platform->driver->ops->hw_params(substream, params);
837 if (ret < 0) {
838 printk(KERN_ERR "asoc: platform %s hw params failed\n",
839 platform->name);
840 goto platform_err;
844 rtd->rate = params_rate(params);
846 out:
847 mutex_unlock(&pcm_mutex);
848 return ret;
850 platform_err:
851 if (cpu_dai->driver->ops->hw_free)
852 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
854 interface_err:
855 if (codec_dai->driver->ops->hw_free)
856 codec_dai->driver->ops->hw_free(substream, codec_dai);
858 codec_err:
859 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
860 rtd->dai_link->ops->hw_free(substream);
862 mutex_unlock(&pcm_mutex);
863 return ret;
867 * Frees resources allocated by hw_params, can be called multiple times
869 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
871 struct snd_soc_pcm_runtime *rtd = substream->private_data;
872 struct snd_soc_platform *platform = rtd->platform;
873 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
874 struct snd_soc_dai *codec_dai = rtd->codec_dai;
875 struct snd_soc_codec *codec = rtd->codec;
877 mutex_lock(&pcm_mutex);
879 /* apply codec digital mute */
880 if (!codec->active)
881 snd_soc_dai_digital_mute(codec_dai, 1);
883 /* free any machine hw params */
884 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
885 rtd->dai_link->ops->hw_free(substream);
887 /* free any DMA resources */
888 if (platform->driver->ops->hw_free)
889 platform->driver->ops->hw_free(substream);
891 /* now free hw params for the DAIs */
892 if (codec_dai->driver->ops->hw_free)
893 codec_dai->driver->ops->hw_free(substream, codec_dai);
895 if (cpu_dai->driver->ops->hw_free)
896 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
898 mutex_unlock(&pcm_mutex);
899 return 0;
902 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
904 struct snd_soc_pcm_runtime *rtd = substream->private_data;
905 struct snd_soc_platform *platform = rtd->platform;
906 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
907 struct snd_soc_dai *codec_dai = rtd->codec_dai;
908 int ret;
910 if (codec_dai->driver->ops->trigger) {
911 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
912 if (ret < 0)
913 return ret;
916 if (platform->driver->ops->trigger) {
917 ret = platform->driver->ops->trigger(substream, cmd);
918 if (ret < 0)
919 return ret;
922 if (cpu_dai->driver->ops->trigger) {
923 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
924 if (ret < 0)
925 return ret;
927 return 0;
931 * soc level wrapper for pointer callback
932 * If cpu_dai, codec_dai, platform driver has the delay callback, than
933 * the runtime->delay will be updated accordingly.
935 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
937 struct snd_soc_pcm_runtime *rtd = substream->private_data;
938 struct snd_soc_platform *platform = rtd->platform;
939 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
940 struct snd_soc_dai *codec_dai = rtd->codec_dai;
941 struct snd_pcm_runtime *runtime = substream->runtime;
942 snd_pcm_uframes_t offset = 0;
943 snd_pcm_sframes_t delay = 0;
945 if (platform->driver->ops->pointer)
946 offset = platform->driver->ops->pointer(substream);
948 if (cpu_dai->driver->ops->delay)
949 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
951 if (codec_dai->driver->ops->delay)
952 delay += codec_dai->driver->ops->delay(substream, codec_dai);
954 if (platform->driver->delay)
955 delay += platform->driver->delay(substream, codec_dai);
957 runtime->delay = delay;
959 return offset;
962 /* ASoC PCM operations */
963 static struct snd_pcm_ops soc_pcm_ops = {
964 .open = soc_pcm_open,
965 .close = soc_codec_close,
966 .hw_params = soc_pcm_hw_params,
967 .hw_free = soc_pcm_hw_free,
968 .prepare = soc_pcm_prepare,
969 .trigger = soc_pcm_trigger,
970 .pointer = soc_pcm_pointer,
973 #ifdef CONFIG_PM_SLEEP
974 /* powers down audio subsystem for suspend */
975 int snd_soc_suspend(struct device *dev)
977 struct snd_soc_card *card = dev_get_drvdata(dev);
978 struct snd_soc_codec *codec;
979 int i;
981 /* If the initialization of this soc device failed, there is no codec
982 * associated with it. Just bail out in this case.
984 if (list_empty(&card->codec_dev_list))
985 return 0;
987 /* Due to the resume being scheduled into a workqueue we could
988 * suspend before that's finished - wait for it to complete.
990 snd_power_lock(card->snd_card);
991 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
992 snd_power_unlock(card->snd_card);
994 /* we're going to block userspace touching us until resume completes */
995 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
997 /* mute any active DACs */
998 for (i = 0; i < card->num_rtd; i++) {
999 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1000 struct snd_soc_dai_driver *drv = dai->driver;
1002 if (card->rtd[i].dai_link->ignore_suspend)
1003 continue;
1005 if (drv->ops->digital_mute && dai->playback_active)
1006 drv->ops->digital_mute(dai, 1);
1009 /* suspend all pcms */
1010 for (i = 0; i < card->num_rtd; i++) {
1011 if (card->rtd[i].dai_link->ignore_suspend)
1012 continue;
1014 snd_pcm_suspend_all(card->rtd[i].pcm);
1017 if (card->suspend_pre)
1018 card->suspend_pre(card);
1020 for (i = 0; i < card->num_rtd; i++) {
1021 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1022 struct snd_soc_platform *platform = card->rtd[i].platform;
1024 if (card->rtd[i].dai_link->ignore_suspend)
1025 continue;
1027 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
1028 cpu_dai->driver->suspend(cpu_dai);
1029 if (platform->driver->suspend && !platform->suspended) {
1030 platform->driver->suspend(cpu_dai);
1031 platform->suspended = 1;
1035 /* close any waiting streams and save state */
1036 for (i = 0; i < card->num_rtd; i++) {
1037 flush_delayed_work_sync(&card->rtd[i].delayed_work);
1038 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
1041 for (i = 0; i < card->num_rtd; i++) {
1042 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1044 if (card->rtd[i].dai_link->ignore_suspend)
1045 continue;
1047 if (driver->playback.stream_name != NULL)
1048 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1049 SND_SOC_DAPM_STREAM_SUSPEND);
1051 if (driver->capture.stream_name != NULL)
1052 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1053 SND_SOC_DAPM_STREAM_SUSPEND);
1056 /* suspend all CODECs */
1057 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
1058 /* If there are paths active then the CODEC will be held with
1059 * bias _ON and should not be suspended. */
1060 if (!codec->suspended && codec->driver->suspend) {
1061 switch (codec->dapm.bias_level) {
1062 case SND_SOC_BIAS_STANDBY:
1063 case SND_SOC_BIAS_OFF:
1064 codec->driver->suspend(codec, PMSG_SUSPEND);
1065 codec->suspended = 1;
1066 break;
1067 default:
1068 dev_dbg(codec->dev, "CODEC is on over suspend\n");
1069 break;
1074 for (i = 0; i < card->num_rtd; i++) {
1075 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1077 if (card->rtd[i].dai_link->ignore_suspend)
1078 continue;
1080 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1081 cpu_dai->driver->suspend(cpu_dai);
1084 if (card->suspend_post)
1085 card->suspend_post(card);
1087 return 0;
1089 EXPORT_SYMBOL_GPL(snd_soc_suspend);
1091 /* deferred resume work, so resume can complete before we finished
1092 * setting our codec back up, which can be very slow on I2C
1094 static void soc_resume_deferred(struct work_struct *work)
1096 struct snd_soc_card *card =
1097 container_of(work, struct snd_soc_card, deferred_resume_work);
1098 struct snd_soc_codec *codec;
1099 int i;
1101 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1102 * so userspace apps are blocked from touching us
1105 dev_dbg(card->dev, "starting resume work\n");
1107 /* Bring us up into D2 so that DAPM starts enabling things */
1108 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
1110 if (card->resume_pre)
1111 card->resume_pre(card);
1113 /* resume AC97 DAIs */
1114 for (i = 0; i < card->num_rtd; i++) {
1115 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1117 if (card->rtd[i].dai_link->ignore_suspend)
1118 continue;
1120 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1121 cpu_dai->driver->resume(cpu_dai);
1124 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
1125 /* If the CODEC was idle over suspend then it will have been
1126 * left with bias OFF or STANDBY and suspended so we must now
1127 * resume. Otherwise the suspend was suppressed.
1129 if (codec->driver->resume && codec->suspended) {
1130 switch (codec->dapm.bias_level) {
1131 case SND_SOC_BIAS_STANDBY:
1132 case SND_SOC_BIAS_OFF:
1133 codec->driver->resume(codec);
1134 codec->suspended = 0;
1135 break;
1136 default:
1137 dev_dbg(codec->dev, "CODEC was on over suspend\n");
1138 break;
1143 for (i = 0; i < card->num_rtd; i++) {
1144 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1146 if (card->rtd[i].dai_link->ignore_suspend)
1147 continue;
1149 if (driver->playback.stream_name != NULL)
1150 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1151 SND_SOC_DAPM_STREAM_RESUME);
1153 if (driver->capture.stream_name != NULL)
1154 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1155 SND_SOC_DAPM_STREAM_RESUME);
1158 /* unmute any active DACs */
1159 for (i = 0; i < card->num_rtd; i++) {
1160 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1161 struct snd_soc_dai_driver *drv = dai->driver;
1163 if (card->rtd[i].dai_link->ignore_suspend)
1164 continue;
1166 if (drv->ops->digital_mute && dai->playback_active)
1167 drv->ops->digital_mute(dai, 0);
1170 for (i = 0; i < card->num_rtd; i++) {
1171 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1172 struct snd_soc_platform *platform = card->rtd[i].platform;
1174 if (card->rtd[i].dai_link->ignore_suspend)
1175 continue;
1177 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1178 cpu_dai->driver->resume(cpu_dai);
1179 if (platform->driver->resume && platform->suspended) {
1180 platform->driver->resume(cpu_dai);
1181 platform->suspended = 0;
1185 if (card->resume_post)
1186 card->resume_post(card);
1188 dev_dbg(card->dev, "resume work completed\n");
1190 /* userspace can access us now we are back as we were before */
1191 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
1194 /* powers up audio subsystem after a suspend */
1195 int snd_soc_resume(struct device *dev)
1197 struct snd_soc_card *card = dev_get_drvdata(dev);
1198 int i;
1200 /* AC97 devices might have other drivers hanging off them so
1201 * need to resume immediately. Other drivers don't have that
1202 * problem and may take a substantial amount of time to resume
1203 * due to I/O costs and anti-pop so handle them out of line.
1205 for (i = 0; i < card->num_rtd; i++) {
1206 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1207 if (cpu_dai->driver->ac97_control) {
1208 dev_dbg(dev, "Resuming AC97 immediately\n");
1209 soc_resume_deferred(&card->deferred_resume_work);
1210 } else {
1211 dev_dbg(dev, "Scheduling resume work\n");
1212 if (!schedule_work(&card->deferred_resume_work))
1213 dev_err(dev, "resume work item may be lost\n");
1217 return 0;
1219 EXPORT_SYMBOL_GPL(snd_soc_resume);
1220 #else
1221 #define snd_soc_suspend NULL
1222 #define snd_soc_resume NULL
1223 #endif
1225 static struct snd_soc_dai_ops null_dai_ops = {
1228 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
1230 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1231 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1232 struct snd_soc_codec *codec;
1233 struct snd_soc_platform *platform;
1234 struct snd_soc_dai *codec_dai, *cpu_dai;
1236 if (rtd->complete)
1237 return 1;
1238 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
1240 /* do we already have the CPU DAI for this link ? */
1241 if (rtd->cpu_dai) {
1242 goto find_codec;
1244 /* no, then find CPU DAI from registered DAIs*/
1245 list_for_each_entry(cpu_dai, &dai_list, list) {
1246 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1248 if (!try_module_get(cpu_dai->dev->driver->owner))
1249 return -ENODEV;
1251 rtd->cpu_dai = cpu_dai;
1252 goto find_codec;
1255 dev_dbg(card->dev, "CPU DAI %s not registered\n",
1256 dai_link->cpu_dai_name);
1258 find_codec:
1259 /* do we already have the CODEC for this link ? */
1260 if (rtd->codec) {
1261 goto find_platform;
1264 /* no, then find CODEC from registered CODECs*/
1265 list_for_each_entry(codec, &codec_list, list) {
1266 if (!strcmp(codec->name, dai_link->codec_name)) {
1267 rtd->codec = codec;
1269 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1270 list_for_each_entry(codec_dai, &dai_list, list) {
1271 if (codec->dev == codec_dai->dev &&
1272 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1273 rtd->codec_dai = codec_dai;
1274 goto find_platform;
1277 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1278 dai_link->codec_dai_name);
1280 goto find_platform;
1283 dev_dbg(card->dev, "CODEC %s not registered\n",
1284 dai_link->codec_name);
1286 find_platform:
1287 /* do we already have the CODEC DAI for this link ? */
1288 if (rtd->platform) {
1289 goto out;
1291 /* no, then find CPU DAI from registered DAIs*/
1292 list_for_each_entry(platform, &platform_list, list) {
1293 if (!strcmp(platform->name, dai_link->platform_name)) {
1294 rtd->platform = platform;
1295 goto out;
1299 dev_dbg(card->dev, "platform %s not registered\n",
1300 dai_link->platform_name);
1301 return 0;
1303 out:
1304 /* mark rtd as complete if we found all 4 of our client devices */
1305 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1306 rtd->complete = 1;
1307 card->num_rtd++;
1309 return 1;
1312 static void soc_remove_codec(struct snd_soc_codec *codec)
1314 int err;
1316 if (codec->driver->remove) {
1317 err = codec->driver->remove(codec);
1318 if (err < 0)
1319 dev_err(codec->dev,
1320 "asoc: failed to remove %s: %d\n",
1321 codec->name, err);
1324 /* Make sure all DAPM widgets are freed */
1325 snd_soc_dapm_free(&codec->dapm);
1327 soc_cleanup_codec_debugfs(codec);
1328 codec->probed = 0;
1329 list_del(&codec->card_list);
1330 module_put(codec->dev->driver->owner);
1333 static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1335 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1336 struct snd_soc_codec *codec = rtd->codec;
1337 struct snd_soc_platform *platform = rtd->platform;
1338 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1339 int err;
1341 /* unregister the rtd device */
1342 if (rtd->dev_registered) {
1343 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
1344 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1345 device_unregister(&rtd->dev);
1346 rtd->dev_registered = 0;
1349 /* remove the CODEC DAI */
1350 if (codec_dai && codec_dai->probed) {
1351 if (codec_dai->driver->remove) {
1352 err = codec_dai->driver->remove(codec_dai);
1353 if (err < 0)
1354 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1356 codec_dai->probed = 0;
1357 list_del(&codec_dai->card_list);
1360 /* remove the platform */
1361 if (platform && platform->probed) {
1362 if (platform->driver->remove) {
1363 err = platform->driver->remove(platform);
1364 if (err < 0)
1365 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1367 platform->probed = 0;
1368 list_del(&platform->card_list);
1369 module_put(platform->dev->driver->owner);
1372 /* remove the CODEC */
1373 if (codec && codec->probed)
1374 soc_remove_codec(codec);
1376 /* remove the cpu_dai */
1377 if (cpu_dai && cpu_dai->probed) {
1378 if (cpu_dai->driver->remove) {
1379 err = cpu_dai->driver->remove(cpu_dai);
1380 if (err < 0)
1381 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1383 cpu_dai->probed = 0;
1384 list_del(&cpu_dai->card_list);
1385 module_put(cpu_dai->dev->driver->owner);
1389 static void soc_set_name_prefix(struct snd_soc_card *card,
1390 struct snd_soc_codec *codec)
1392 int i;
1394 if (card->codec_conf == NULL)
1395 return;
1397 for (i = 0; i < card->num_configs; i++) {
1398 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1399 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1400 codec->name_prefix = map->name_prefix;
1401 break;
1406 static int soc_probe_codec(struct snd_soc_card *card,
1407 struct snd_soc_codec *codec)
1409 int ret = 0;
1411 codec->card = card;
1412 codec->dapm.card = card;
1413 soc_set_name_prefix(card, codec);
1415 if (!try_module_get(codec->dev->driver->owner))
1416 return -ENODEV;
1418 if (codec->driver->probe) {
1419 ret = codec->driver->probe(codec);
1420 if (ret < 0) {
1421 dev_err(codec->dev,
1422 "asoc: failed to probe CODEC %s: %d\n",
1423 codec->name, ret);
1424 goto err_probe;
1428 soc_init_codec_debugfs(codec);
1430 /* mark codec as probed and add to card codec list */
1431 codec->probed = 1;
1432 list_add(&codec->card_list, &card->codec_dev_list);
1433 list_add(&codec->dapm.list, &card->dapm_list);
1435 return 0;
1437 err_probe:
1438 module_put(codec->dev->driver->owner);
1440 return ret;
1443 static void rtd_release(struct device *dev) {}
1445 static int soc_post_component_init(struct snd_soc_card *card,
1446 struct snd_soc_codec *codec,
1447 int num, int dailess)
1449 struct snd_soc_dai_link *dai_link = NULL;
1450 struct snd_soc_aux_dev *aux_dev = NULL;
1451 struct snd_soc_pcm_runtime *rtd;
1452 const char *temp, *name;
1453 int ret = 0;
1455 if (!dailess) {
1456 dai_link = &card->dai_link[num];
1457 rtd = &card->rtd[num];
1458 name = dai_link->name;
1459 } else {
1460 aux_dev = &card->aux_dev[num];
1461 rtd = &card->rtd_aux[num];
1462 name = aux_dev->name;
1465 /* machine controls, routes and widgets are not prefixed */
1466 temp = codec->name_prefix;
1467 codec->name_prefix = NULL;
1469 /* do machine specific initialization */
1470 if (!dailess && dai_link->init)
1471 ret = dai_link->init(rtd);
1472 else if (dailess && aux_dev->init)
1473 ret = aux_dev->init(&codec->dapm);
1474 if (ret < 0) {
1475 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1476 return ret;
1478 codec->name_prefix = temp;
1480 /* Make sure all DAPM widgets are instantiated */
1481 snd_soc_dapm_new_widgets(&codec->dapm);
1483 /* register the rtd device */
1484 rtd->codec = codec;
1485 rtd->card = card;
1486 rtd->dev.parent = card->dev;
1487 rtd->dev.release = rtd_release;
1488 rtd->dev.init_name = name;
1489 ret = device_register(&rtd->dev);
1490 if (ret < 0) {
1491 dev_err(card->dev,
1492 "asoc: failed to register runtime device: %d\n", ret);
1493 return ret;
1495 rtd->dev_registered = 1;
1497 /* add DAPM sysfs entries for this codec */
1498 ret = snd_soc_dapm_sys_add(&rtd->dev);
1499 if (ret < 0)
1500 dev_err(codec->dev,
1501 "asoc: failed to add codec dapm sysfs entries: %d\n",
1502 ret);
1504 /* add codec sysfs entries */
1505 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1506 if (ret < 0)
1507 dev_err(codec->dev,
1508 "asoc: failed to add codec sysfs files: %d\n", ret);
1510 return 0;
1513 static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1515 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1516 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1517 struct snd_soc_codec *codec = rtd->codec;
1518 struct snd_soc_platform *platform = rtd->platform;
1519 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1520 int ret;
1522 dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1524 /* config components */
1525 codec_dai->codec = codec;
1526 cpu_dai->platform = platform;
1527 codec_dai->card = card;
1528 cpu_dai->card = card;
1530 /* set default power off timeout */
1531 rtd->pmdown_time = pmdown_time;
1533 /* probe the cpu_dai */
1534 if (!cpu_dai->probed) {
1535 if (cpu_dai->driver->probe) {
1536 ret = cpu_dai->driver->probe(cpu_dai);
1537 if (ret < 0) {
1538 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1539 cpu_dai->name);
1540 return ret;
1543 cpu_dai->probed = 1;
1544 /* mark cpu_dai as probed and add to card cpu_dai list */
1545 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1548 /* probe the CODEC */
1549 if (!codec->probed) {
1550 ret = soc_probe_codec(card, codec);
1551 if (ret < 0)
1552 return ret;
1555 /* probe the platform */
1556 if (!platform->probed) {
1557 if (!try_module_get(platform->dev->driver->owner))
1558 return -ENODEV;
1560 if (platform->driver->probe) {
1561 ret = platform->driver->probe(platform);
1562 if (ret < 0) {
1563 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1564 platform->name);
1565 module_put(platform->dev->driver->owner);
1566 return ret;
1569 /* mark platform as probed and add to card platform list */
1570 platform->probed = 1;
1571 list_add(&platform->card_list, &card->platform_dev_list);
1574 /* probe the CODEC DAI */
1575 if (!codec_dai->probed) {
1576 if (codec_dai->driver->probe) {
1577 ret = codec_dai->driver->probe(codec_dai);
1578 if (ret < 0) {
1579 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1580 codec_dai->name);
1581 return ret;
1585 /* mark cpu_dai as probed and add to card cpu_dai list */
1586 codec_dai->probed = 1;
1587 list_add(&codec_dai->card_list, &card->dai_dev_list);
1590 /* DAPM dai link stream work */
1591 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1593 ret = soc_post_component_init(card, codec, num, 0);
1594 if (ret)
1595 return ret;
1597 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1598 if (ret < 0)
1599 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1601 /* create the pcm */
1602 ret = soc_new_pcm(rtd, num);
1603 if (ret < 0) {
1604 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1605 return ret;
1608 /* add platform data for AC97 devices */
1609 if (rtd->codec_dai->driver->ac97_control)
1610 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1612 return 0;
1615 #ifdef CONFIG_SND_SOC_AC97_BUS
1616 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1618 int ret;
1620 /* Only instantiate AC97 if not already done by the adaptor
1621 * for the generic AC97 subsystem.
1623 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
1625 * It is possible that the AC97 device is already registered to
1626 * the device subsystem. This happens when the device is created
1627 * via snd_ac97_mixer(). Currently only SoC codec that does so
1628 * is the generic AC97 glue but others migh emerge.
1630 * In those cases we don't try to register the device again.
1632 if (!rtd->codec->ac97_created)
1633 return 0;
1635 ret = soc_ac97_dev_register(rtd->codec);
1636 if (ret < 0) {
1637 printk(KERN_ERR "asoc: AC97 device register failed\n");
1638 return ret;
1641 rtd->codec->ac97_registered = 1;
1643 return 0;
1646 static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1648 if (codec->ac97_registered) {
1649 soc_ac97_dev_unregister(codec);
1650 codec->ac97_registered = 0;
1653 #endif
1655 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1657 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1658 struct snd_soc_codec *codec;
1659 int ret = -ENODEV;
1661 /* find CODEC from registered CODECs*/
1662 list_for_each_entry(codec, &codec_list, list) {
1663 if (!strcmp(codec->name, aux_dev->codec_name)) {
1664 if (codec->probed) {
1665 dev_err(codec->dev,
1666 "asoc: codec already probed");
1667 ret = -EBUSY;
1668 goto out;
1670 goto found;
1673 /* codec not found */
1674 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1675 goto out;
1677 found:
1678 ret = soc_probe_codec(card, codec);
1679 if (ret < 0)
1680 return ret;
1682 ret = soc_post_component_init(card, codec, num, 1);
1684 out:
1685 return ret;
1688 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1690 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1691 struct snd_soc_codec *codec = rtd->codec;
1693 /* unregister the rtd device */
1694 if (rtd->dev_registered) {
1695 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1696 device_unregister(&rtd->dev);
1697 rtd->dev_registered = 0;
1700 if (codec && codec->probed)
1701 soc_remove_codec(codec);
1704 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1705 enum snd_soc_compress_type compress_type)
1707 int ret;
1709 if (codec->cache_init)
1710 return 0;
1712 /* override the compress_type if necessary */
1713 if (compress_type && codec->compress_type != compress_type)
1714 codec->compress_type = compress_type;
1715 ret = snd_soc_cache_init(codec);
1716 if (ret < 0) {
1717 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1718 ret);
1719 return ret;
1721 codec->cache_init = 1;
1722 return 0;
1725 static void snd_soc_instantiate_card(struct snd_soc_card *card)
1727 struct snd_soc_codec *codec;
1728 struct snd_soc_codec_conf *codec_conf;
1729 enum snd_soc_compress_type compress_type;
1730 int ret, i;
1732 mutex_lock(&card->mutex);
1734 if (card->instantiated) {
1735 mutex_unlock(&card->mutex);
1736 return;
1739 /* bind DAIs */
1740 for (i = 0; i < card->num_links; i++)
1741 soc_bind_dai_link(card, i);
1743 /* bind completed ? */
1744 if (card->num_rtd != card->num_links) {
1745 mutex_unlock(&card->mutex);
1746 return;
1749 /* initialize the register cache for each available codec */
1750 list_for_each_entry(codec, &codec_list, list) {
1751 if (codec->cache_init)
1752 continue;
1753 /* by default we don't override the compress_type */
1754 compress_type = 0;
1755 /* check to see if we need to override the compress_type */
1756 for (i = 0; i < card->num_configs; ++i) {
1757 codec_conf = &card->codec_conf[i];
1758 if (!strcmp(codec->name, codec_conf->dev_name)) {
1759 compress_type = codec_conf->compress_type;
1760 if (compress_type && compress_type
1761 != codec->compress_type)
1762 break;
1765 ret = snd_soc_init_codec_cache(codec, compress_type);
1766 if (ret < 0) {
1767 mutex_unlock(&card->mutex);
1768 return;
1772 /* card bind complete so register a sound card */
1773 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1774 card->owner, 0, &card->snd_card);
1775 if (ret < 0) {
1776 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1777 card->name);
1778 mutex_unlock(&card->mutex);
1779 return;
1781 card->snd_card->dev = card->dev;
1783 #ifdef CONFIG_PM
1784 /* deferred resume work */
1785 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1786 #endif
1788 /* initialise the sound card only once */
1789 if (card->probe) {
1790 ret = card->probe(card);
1791 if (ret < 0)
1792 goto card_probe_error;
1795 for (i = 0; i < card->num_links; i++) {
1796 ret = soc_probe_dai_link(card, i);
1797 if (ret < 0) {
1798 pr_err("asoc: failed to instantiate card %s: %d\n",
1799 card->name, ret);
1800 goto probe_dai_err;
1804 for (i = 0; i < card->num_aux_devs; i++) {
1805 ret = soc_probe_aux_dev(card, i);
1806 if (ret < 0) {
1807 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1808 card->name, ret);
1809 goto probe_aux_dev_err;
1813 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1814 "%s", card->name);
1815 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1816 "%s", card->name);
1818 ret = snd_card_register(card->snd_card);
1819 if (ret < 0) {
1820 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1821 goto probe_aux_dev_err;
1824 #ifdef CONFIG_SND_SOC_AC97_BUS
1825 /* register any AC97 codecs */
1826 for (i = 0; i < card->num_rtd; i++) {
1827 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1828 if (ret < 0) {
1829 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1830 while (--i >= 0)
1831 soc_unregister_ac97_dai_link(card->rtd[i].codec);
1832 goto probe_aux_dev_err;
1835 #endif
1837 card->instantiated = 1;
1838 mutex_unlock(&card->mutex);
1839 return;
1841 probe_aux_dev_err:
1842 for (i = 0; i < card->num_aux_devs; i++)
1843 soc_remove_aux_dev(card, i);
1845 probe_dai_err:
1846 for (i = 0; i < card->num_links; i++)
1847 soc_remove_dai_link(card, i);
1849 card_probe_error:
1850 if (card->remove)
1851 card->remove(card);
1853 snd_card_free(card->snd_card);
1855 mutex_unlock(&card->mutex);
1859 * Attempt to initialise any uninitialised cards. Must be called with
1860 * client_mutex.
1862 static void snd_soc_instantiate_cards(void)
1864 struct snd_soc_card *card;
1865 list_for_each_entry(card, &card_list, list)
1866 snd_soc_instantiate_card(card);
1869 /* probes a new socdev */
1870 static int soc_probe(struct platform_device *pdev)
1872 struct snd_soc_card *card = platform_get_drvdata(pdev);
1873 int ret = 0;
1876 * no card, so machine driver should be registering card
1877 * we should not be here in that case so ret error
1879 if (!card)
1880 return -EINVAL;
1882 /* Bodge while we unpick instantiation */
1883 card->dev = &pdev->dev;
1885 ret = snd_soc_register_card(card);
1886 if (ret != 0) {
1887 dev_err(&pdev->dev, "Failed to register card\n");
1888 return ret;
1891 return 0;
1894 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1896 int i;
1898 /* make sure any delayed work runs */
1899 for (i = 0; i < card->num_rtd; i++) {
1900 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1901 flush_delayed_work_sync(&rtd->delayed_work);
1904 /* remove auxiliary devices */
1905 for (i = 0; i < card->num_aux_devs; i++)
1906 soc_remove_aux_dev(card, i);
1908 /* remove and free each DAI */
1909 for (i = 0; i < card->num_rtd; i++)
1910 soc_remove_dai_link(card, i);
1912 soc_cleanup_card_debugfs(card);
1914 /* remove the card */
1915 if (card->remove)
1916 card->remove(card);
1918 kfree(card->rtd);
1919 snd_card_free(card->snd_card);
1920 return 0;
1924 /* removes a socdev */
1925 static int soc_remove(struct platform_device *pdev)
1927 struct snd_soc_card *card = platform_get_drvdata(pdev);
1929 snd_soc_unregister_card(card);
1930 return 0;
1933 int snd_soc_poweroff(struct device *dev)
1935 struct snd_soc_card *card = dev_get_drvdata(dev);
1936 int i;
1938 if (!card->instantiated)
1939 return 0;
1941 /* Flush out pmdown_time work - we actually do want to run it
1942 * now, we're shutting down so no imminent restart. */
1943 for (i = 0; i < card->num_rtd; i++) {
1944 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1945 flush_delayed_work_sync(&rtd->delayed_work);
1948 snd_soc_dapm_shutdown(card);
1950 return 0;
1952 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1954 const struct dev_pm_ops snd_soc_pm_ops = {
1955 .suspend = snd_soc_suspend,
1956 .resume = snd_soc_resume,
1957 .poweroff = snd_soc_poweroff,
1960 /* ASoC platform driver */
1961 static struct platform_driver soc_driver = {
1962 .driver = {
1963 .name = "soc-audio",
1964 .owner = THIS_MODULE,
1965 .pm = &snd_soc_pm_ops,
1967 .probe = soc_probe,
1968 .remove = soc_remove,
1971 /* create a new pcm */
1972 static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
1974 struct snd_soc_codec *codec = rtd->codec;
1975 struct snd_soc_platform *platform = rtd->platform;
1976 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1977 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1978 struct snd_pcm *pcm;
1979 char new_name[64];
1980 int ret = 0, playback = 0, capture = 0;
1982 /* check client and interface hw capabilities */
1983 snprintf(new_name, sizeof(new_name), "%s %s-%d",
1984 rtd->dai_link->stream_name, codec_dai->name, num);
1986 if (codec_dai->driver->playback.channels_min)
1987 playback = 1;
1988 if (codec_dai->driver->capture.channels_min)
1989 capture = 1;
1991 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
1992 ret = snd_pcm_new(rtd->card->snd_card, new_name,
1993 num, playback, capture, &pcm);
1994 if (ret < 0) {
1995 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
1996 return ret;
1999 rtd->pcm = pcm;
2000 pcm->private_data = rtd;
2001 soc_pcm_ops.mmap = platform->driver->ops->mmap;
2002 soc_pcm_ops.pointer = platform->driver->ops->pointer;
2003 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
2004 soc_pcm_ops.copy = platform->driver->ops->copy;
2005 soc_pcm_ops.silence = platform->driver->ops->silence;
2006 soc_pcm_ops.ack = platform->driver->ops->ack;
2007 soc_pcm_ops.page = platform->driver->ops->page;
2009 if (playback)
2010 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
2012 if (capture)
2013 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
2015 ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
2016 if (ret < 0) {
2017 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
2018 return ret;
2021 pcm->private_free = platform->driver->pcm_free;
2022 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
2023 cpu_dai->name);
2024 return ret;
2028 * snd_soc_codec_volatile_register: Report if a register is volatile.
2030 * @codec: CODEC to query.
2031 * @reg: Register to query.
2033 * Boolean function indiciating if a CODEC register is volatile.
2035 int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
2036 unsigned int reg)
2038 if (codec->volatile_register)
2039 return codec->volatile_register(codec, reg);
2040 else
2041 return 0;
2043 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
2046 * snd_soc_new_ac97_codec - initailise AC97 device
2047 * @codec: audio codec
2048 * @ops: AC97 bus operations
2049 * @num: AC97 codec number
2051 * Initialises AC97 codec resources for use by ad-hoc devices only.
2053 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2054 struct snd_ac97_bus_ops *ops, int num)
2056 mutex_lock(&codec->mutex);
2058 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2059 if (codec->ac97 == NULL) {
2060 mutex_unlock(&codec->mutex);
2061 return -ENOMEM;
2064 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2065 if (codec->ac97->bus == NULL) {
2066 kfree(codec->ac97);
2067 codec->ac97 = NULL;
2068 mutex_unlock(&codec->mutex);
2069 return -ENOMEM;
2072 codec->ac97->bus->ops = ops;
2073 codec->ac97->num = num;
2076 * Mark the AC97 device to be created by us. This way we ensure that the
2077 * device will be registered with the device subsystem later on.
2079 codec->ac97_created = 1;
2081 mutex_unlock(&codec->mutex);
2082 return 0;
2084 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2087 * snd_soc_free_ac97_codec - free AC97 codec device
2088 * @codec: audio codec
2090 * Frees AC97 codec device resources.
2092 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2094 mutex_lock(&codec->mutex);
2095 #ifdef CONFIG_SND_SOC_AC97_BUS
2096 soc_unregister_ac97_dai_link(codec);
2097 #endif
2098 kfree(codec->ac97->bus);
2099 kfree(codec->ac97);
2100 codec->ac97 = NULL;
2101 codec->ac97_created = 0;
2102 mutex_unlock(&codec->mutex);
2104 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2106 unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2108 unsigned int ret;
2110 ret = codec->read(codec, reg);
2111 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
2112 trace_snd_soc_reg_read(codec, reg, ret);
2114 return ret;
2116 EXPORT_SYMBOL_GPL(snd_soc_read);
2118 unsigned int snd_soc_write(struct snd_soc_codec *codec,
2119 unsigned int reg, unsigned int val)
2121 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
2122 trace_snd_soc_reg_write(codec, reg, val);
2123 return codec->write(codec, reg, val);
2125 EXPORT_SYMBOL_GPL(snd_soc_write);
2128 * snd_soc_update_bits - update codec register bits
2129 * @codec: audio codec
2130 * @reg: codec register
2131 * @mask: register mask
2132 * @value: new value
2134 * Writes new register value.
2136 * Returns 1 for change, 0 for no change, or negative error code.
2138 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
2139 unsigned int mask, unsigned int value)
2141 int change;
2142 unsigned int old, new;
2143 int ret;
2145 ret = snd_soc_read(codec, reg);
2146 if (ret < 0)
2147 return ret;
2149 old = ret;
2150 new = (old & ~mask) | value;
2151 change = old != new;
2152 if (change) {
2153 ret = snd_soc_write(codec, reg, new);
2154 if (ret < 0)
2155 return ret;
2158 return change;
2160 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2163 * snd_soc_update_bits_locked - update codec register bits
2164 * @codec: audio codec
2165 * @reg: codec register
2166 * @mask: register mask
2167 * @value: new value
2169 * Writes new register value, and takes the codec mutex.
2171 * Returns 1 for change else 0.
2173 int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2174 unsigned short reg, unsigned int mask,
2175 unsigned int value)
2177 int change;
2179 mutex_lock(&codec->mutex);
2180 change = snd_soc_update_bits(codec, reg, mask, value);
2181 mutex_unlock(&codec->mutex);
2183 return change;
2185 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
2188 * snd_soc_test_bits - test register for change
2189 * @codec: audio codec
2190 * @reg: codec register
2191 * @mask: register mask
2192 * @value: new value
2194 * Tests a register with a new value and checks if the new value is
2195 * different from the old value.
2197 * Returns 1 for change else 0.
2199 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
2200 unsigned int mask, unsigned int value)
2202 int change;
2203 unsigned int old, new;
2205 old = snd_soc_read(codec, reg);
2206 new = (old & ~mask) | value;
2207 change = old != new;
2209 return change;
2211 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2214 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2215 * @substream: the pcm substream
2216 * @hw: the hardware parameters
2218 * Sets the substream runtime hardware parameters.
2220 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2221 const struct snd_pcm_hardware *hw)
2223 struct snd_pcm_runtime *runtime = substream->runtime;
2224 runtime->hw.info = hw->info;
2225 runtime->hw.formats = hw->formats;
2226 runtime->hw.period_bytes_min = hw->period_bytes_min;
2227 runtime->hw.period_bytes_max = hw->period_bytes_max;
2228 runtime->hw.periods_min = hw->periods_min;
2229 runtime->hw.periods_max = hw->periods_max;
2230 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2231 runtime->hw.fifo_size = hw->fifo_size;
2232 return 0;
2234 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2237 * snd_soc_cnew - create new control
2238 * @_template: control template
2239 * @data: control private data
2240 * @long_name: control long name
2242 * Create a new mixer control from a template control.
2244 * Returns 0 for success, else error.
2246 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2247 void *data, char *long_name)
2249 struct snd_kcontrol_new template;
2251 memcpy(&template, _template, sizeof(template));
2252 if (long_name)
2253 template.name = long_name;
2254 template.index = 0;
2256 return snd_ctl_new1(&template, data);
2258 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2261 * snd_soc_add_controls - add an array of controls to a codec.
2262 * Convienience function to add a list of controls. Many codecs were
2263 * duplicating this code.
2265 * @codec: codec to add controls to
2266 * @controls: array of controls to add
2267 * @num_controls: number of elements in the array
2269 * Return 0 for success, else error.
2271 int snd_soc_add_controls(struct snd_soc_codec *codec,
2272 const struct snd_kcontrol_new *controls, int num_controls)
2274 struct snd_card *card = codec->card->snd_card;
2275 char prefixed_name[44], *name;
2276 int err, i;
2278 for (i = 0; i < num_controls; i++) {
2279 const struct snd_kcontrol_new *control = &controls[i];
2280 if (codec->name_prefix) {
2281 snprintf(prefixed_name, sizeof(prefixed_name), "%s %s",
2282 codec->name_prefix, control->name);
2283 name = prefixed_name;
2284 } else {
2285 name = control->name;
2287 err = snd_ctl_add(card, snd_soc_cnew(control, codec, name));
2288 if (err < 0) {
2289 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
2290 codec->name, name, err);
2291 return err;
2295 return 0;
2297 EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2300 * snd_soc_info_enum_double - enumerated double mixer info callback
2301 * @kcontrol: mixer control
2302 * @uinfo: control element information
2304 * Callback to provide information about a double enumerated
2305 * mixer control.
2307 * Returns 0 for success.
2309 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2310 struct snd_ctl_elem_info *uinfo)
2312 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2314 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2315 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2316 uinfo->value.enumerated.items = e->max;
2318 if (uinfo->value.enumerated.item > e->max - 1)
2319 uinfo->value.enumerated.item = e->max - 1;
2320 strcpy(uinfo->value.enumerated.name,
2321 e->texts[uinfo->value.enumerated.item]);
2322 return 0;
2324 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2327 * snd_soc_get_enum_double - enumerated double mixer get callback
2328 * @kcontrol: mixer control
2329 * @ucontrol: control element information
2331 * Callback to get the value of a double enumerated mixer.
2333 * Returns 0 for success.
2335 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2336 struct snd_ctl_elem_value *ucontrol)
2338 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2339 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2340 unsigned int val, bitmask;
2342 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2344 val = snd_soc_read(codec, e->reg);
2345 ucontrol->value.enumerated.item[0]
2346 = (val >> e->shift_l) & (bitmask - 1);
2347 if (e->shift_l != e->shift_r)
2348 ucontrol->value.enumerated.item[1] =
2349 (val >> e->shift_r) & (bitmask - 1);
2351 return 0;
2353 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2356 * snd_soc_put_enum_double - enumerated double mixer put callback
2357 * @kcontrol: mixer control
2358 * @ucontrol: control element information
2360 * Callback to set the value of a double enumerated mixer.
2362 * Returns 0 for success.
2364 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2365 struct snd_ctl_elem_value *ucontrol)
2367 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2368 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2369 unsigned int val;
2370 unsigned int mask, bitmask;
2372 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2374 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2375 return -EINVAL;
2376 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2377 mask = (bitmask - 1) << e->shift_l;
2378 if (e->shift_l != e->shift_r) {
2379 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2380 return -EINVAL;
2381 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2382 mask |= (bitmask - 1) << e->shift_r;
2385 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2387 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2390 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2391 * @kcontrol: mixer control
2392 * @ucontrol: control element information
2394 * Callback to get the value of a double semi enumerated mixer.
2396 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2397 * used for handling bitfield coded enumeration for example.
2399 * Returns 0 for success.
2401 int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2402 struct snd_ctl_elem_value *ucontrol)
2404 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2405 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2406 unsigned int reg_val, val, mux;
2408 reg_val = snd_soc_read(codec, e->reg);
2409 val = (reg_val >> e->shift_l) & e->mask;
2410 for (mux = 0; mux < e->max; mux++) {
2411 if (val == e->values[mux])
2412 break;
2414 ucontrol->value.enumerated.item[0] = mux;
2415 if (e->shift_l != e->shift_r) {
2416 val = (reg_val >> e->shift_r) & e->mask;
2417 for (mux = 0; mux < e->max; mux++) {
2418 if (val == e->values[mux])
2419 break;
2421 ucontrol->value.enumerated.item[1] = mux;
2424 return 0;
2426 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2429 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2430 * @kcontrol: mixer control
2431 * @ucontrol: control element information
2433 * Callback to set the value of a double semi enumerated mixer.
2435 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2436 * used for handling bitfield coded enumeration for example.
2438 * Returns 0 for success.
2440 int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2441 struct snd_ctl_elem_value *ucontrol)
2443 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2444 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2445 unsigned int val;
2446 unsigned int mask;
2448 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2449 return -EINVAL;
2450 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2451 mask = e->mask << e->shift_l;
2452 if (e->shift_l != e->shift_r) {
2453 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2454 return -EINVAL;
2455 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2456 mask |= e->mask << e->shift_r;
2459 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2461 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2464 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2465 * @kcontrol: mixer control
2466 * @uinfo: control element information
2468 * Callback to provide information about an external enumerated
2469 * single mixer.
2471 * Returns 0 for success.
2473 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2474 struct snd_ctl_elem_info *uinfo)
2476 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2478 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2479 uinfo->count = 1;
2480 uinfo->value.enumerated.items = e->max;
2482 if (uinfo->value.enumerated.item > e->max - 1)
2483 uinfo->value.enumerated.item = e->max - 1;
2484 strcpy(uinfo->value.enumerated.name,
2485 e->texts[uinfo->value.enumerated.item]);
2486 return 0;
2488 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2491 * snd_soc_info_volsw_ext - external single mixer info callback
2492 * @kcontrol: mixer control
2493 * @uinfo: control element information
2495 * Callback to provide information about a single external mixer control.
2497 * Returns 0 for success.
2499 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2500 struct snd_ctl_elem_info *uinfo)
2502 int max = kcontrol->private_value;
2504 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2505 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2506 else
2507 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2509 uinfo->count = 1;
2510 uinfo->value.integer.min = 0;
2511 uinfo->value.integer.max = max;
2512 return 0;
2514 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2517 * snd_soc_info_volsw - single mixer info callback
2518 * @kcontrol: mixer control
2519 * @uinfo: control element information
2521 * Callback to provide information about a single mixer control.
2523 * Returns 0 for success.
2525 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2526 struct snd_ctl_elem_info *uinfo)
2528 struct soc_mixer_control *mc =
2529 (struct soc_mixer_control *)kcontrol->private_value;
2530 int platform_max;
2531 unsigned int shift = mc->shift;
2532 unsigned int rshift = mc->rshift;
2534 if (!mc->platform_max)
2535 mc->platform_max = mc->max;
2536 platform_max = mc->platform_max;
2538 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2539 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2540 else
2541 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2543 uinfo->count = shift == rshift ? 1 : 2;
2544 uinfo->value.integer.min = 0;
2545 uinfo->value.integer.max = platform_max;
2546 return 0;
2548 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2551 * snd_soc_get_volsw - single mixer get callback
2552 * @kcontrol: mixer control
2553 * @ucontrol: control element information
2555 * Callback to get the value of a single mixer control.
2557 * Returns 0 for success.
2559 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2560 struct snd_ctl_elem_value *ucontrol)
2562 struct soc_mixer_control *mc =
2563 (struct soc_mixer_control *)kcontrol->private_value;
2564 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2565 unsigned int reg = mc->reg;
2566 unsigned int shift = mc->shift;
2567 unsigned int rshift = mc->rshift;
2568 int max = mc->max;
2569 unsigned int mask = (1 << fls(max)) - 1;
2570 unsigned int invert = mc->invert;
2572 ucontrol->value.integer.value[0] =
2573 (snd_soc_read(codec, reg) >> shift) & mask;
2574 if (shift != rshift)
2575 ucontrol->value.integer.value[1] =
2576 (snd_soc_read(codec, reg) >> rshift) & mask;
2577 if (invert) {
2578 ucontrol->value.integer.value[0] =
2579 max - ucontrol->value.integer.value[0];
2580 if (shift != rshift)
2581 ucontrol->value.integer.value[1] =
2582 max - ucontrol->value.integer.value[1];
2585 return 0;
2587 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2590 * snd_soc_put_volsw - single mixer put callback
2591 * @kcontrol: mixer control
2592 * @ucontrol: control element information
2594 * Callback to set the value of a single mixer control.
2596 * Returns 0 for success.
2598 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2599 struct snd_ctl_elem_value *ucontrol)
2601 struct soc_mixer_control *mc =
2602 (struct soc_mixer_control *)kcontrol->private_value;
2603 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2604 unsigned int reg = mc->reg;
2605 unsigned int shift = mc->shift;
2606 unsigned int rshift = mc->rshift;
2607 int max = mc->max;
2608 unsigned int mask = (1 << fls(max)) - 1;
2609 unsigned int invert = mc->invert;
2610 unsigned int val, val2, val_mask;
2612 val = (ucontrol->value.integer.value[0] & mask);
2613 if (invert)
2614 val = max - val;
2615 val_mask = mask << shift;
2616 val = val << shift;
2617 if (shift != rshift) {
2618 val2 = (ucontrol->value.integer.value[1] & mask);
2619 if (invert)
2620 val2 = max - val2;
2621 val_mask |= mask << rshift;
2622 val |= val2 << rshift;
2624 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2626 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2629 * snd_soc_info_volsw_2r - double mixer info callback
2630 * @kcontrol: mixer control
2631 * @uinfo: control element information
2633 * Callback to provide information about a double mixer control that
2634 * spans 2 codec registers.
2636 * Returns 0 for success.
2638 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2639 struct snd_ctl_elem_info *uinfo)
2641 struct soc_mixer_control *mc =
2642 (struct soc_mixer_control *)kcontrol->private_value;
2643 int platform_max;
2645 if (!mc->platform_max)
2646 mc->platform_max = mc->max;
2647 platform_max = mc->platform_max;
2649 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2650 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2651 else
2652 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2654 uinfo->count = 2;
2655 uinfo->value.integer.min = 0;
2656 uinfo->value.integer.max = platform_max;
2657 return 0;
2659 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2662 * snd_soc_get_volsw_2r - double mixer get callback
2663 * @kcontrol: mixer control
2664 * @ucontrol: control element information
2666 * Callback to get the value of a double mixer control that spans 2 registers.
2668 * Returns 0 for success.
2670 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2671 struct snd_ctl_elem_value *ucontrol)
2673 struct soc_mixer_control *mc =
2674 (struct soc_mixer_control *)kcontrol->private_value;
2675 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2676 unsigned int reg = mc->reg;
2677 unsigned int reg2 = mc->rreg;
2678 unsigned int shift = mc->shift;
2679 int max = mc->max;
2680 unsigned int mask = (1 << fls(max)) - 1;
2681 unsigned int invert = mc->invert;
2683 ucontrol->value.integer.value[0] =
2684 (snd_soc_read(codec, reg) >> shift) & mask;
2685 ucontrol->value.integer.value[1] =
2686 (snd_soc_read(codec, reg2) >> shift) & mask;
2687 if (invert) {
2688 ucontrol->value.integer.value[0] =
2689 max - ucontrol->value.integer.value[0];
2690 ucontrol->value.integer.value[1] =
2691 max - ucontrol->value.integer.value[1];
2694 return 0;
2696 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2699 * snd_soc_put_volsw_2r - double mixer set callback
2700 * @kcontrol: mixer control
2701 * @ucontrol: control element information
2703 * Callback to set the value of a double mixer control that spans 2 registers.
2705 * Returns 0 for success.
2707 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2708 struct snd_ctl_elem_value *ucontrol)
2710 struct soc_mixer_control *mc =
2711 (struct soc_mixer_control *)kcontrol->private_value;
2712 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2713 unsigned int reg = mc->reg;
2714 unsigned int reg2 = mc->rreg;
2715 unsigned int shift = mc->shift;
2716 int max = mc->max;
2717 unsigned int mask = (1 << fls(max)) - 1;
2718 unsigned int invert = mc->invert;
2719 int err;
2720 unsigned int val, val2, val_mask;
2722 val_mask = mask << shift;
2723 val = (ucontrol->value.integer.value[0] & mask);
2724 val2 = (ucontrol->value.integer.value[1] & mask);
2726 if (invert) {
2727 val = max - val;
2728 val2 = max - val2;
2731 val = val << shift;
2732 val2 = val2 << shift;
2734 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2735 if (err < 0)
2736 return err;
2738 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2739 return err;
2741 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2744 * snd_soc_info_volsw_s8 - signed mixer info callback
2745 * @kcontrol: mixer control
2746 * @uinfo: control element information
2748 * Callback to provide information about a signed mixer control.
2750 * Returns 0 for success.
2752 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2753 struct snd_ctl_elem_info *uinfo)
2755 struct soc_mixer_control *mc =
2756 (struct soc_mixer_control *)kcontrol->private_value;
2757 int platform_max;
2758 int min = mc->min;
2760 if (!mc->platform_max)
2761 mc->platform_max = mc->max;
2762 platform_max = mc->platform_max;
2764 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2765 uinfo->count = 2;
2766 uinfo->value.integer.min = 0;
2767 uinfo->value.integer.max = platform_max - min;
2768 return 0;
2770 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2773 * snd_soc_get_volsw_s8 - signed mixer get callback
2774 * @kcontrol: mixer control
2775 * @ucontrol: control element information
2777 * Callback to get the value of a signed mixer control.
2779 * Returns 0 for success.
2781 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2782 struct snd_ctl_elem_value *ucontrol)
2784 struct soc_mixer_control *mc =
2785 (struct soc_mixer_control *)kcontrol->private_value;
2786 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2787 unsigned int reg = mc->reg;
2788 int min = mc->min;
2789 int val = snd_soc_read(codec, reg);
2791 ucontrol->value.integer.value[0] =
2792 ((signed char)(val & 0xff))-min;
2793 ucontrol->value.integer.value[1] =
2794 ((signed char)((val >> 8) & 0xff))-min;
2795 return 0;
2797 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2800 * snd_soc_put_volsw_sgn - signed mixer put callback
2801 * @kcontrol: mixer control
2802 * @ucontrol: control element information
2804 * Callback to set the value of a signed mixer control.
2806 * Returns 0 for success.
2808 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2809 struct snd_ctl_elem_value *ucontrol)
2811 struct soc_mixer_control *mc =
2812 (struct soc_mixer_control *)kcontrol->private_value;
2813 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2814 unsigned int reg = mc->reg;
2815 int min = mc->min;
2816 unsigned int val;
2818 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2819 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2821 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2823 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2826 * snd_soc_limit_volume - Set new limit to an existing volume control.
2828 * @codec: where to look for the control
2829 * @name: Name of the control
2830 * @max: new maximum limit
2832 * Return 0 for success, else error.
2834 int snd_soc_limit_volume(struct snd_soc_codec *codec,
2835 const char *name, int max)
2837 struct snd_card *card = codec->card->snd_card;
2838 struct snd_kcontrol *kctl;
2839 struct soc_mixer_control *mc;
2840 int found = 0;
2841 int ret = -EINVAL;
2843 /* Sanity check for name and max */
2844 if (unlikely(!name || max <= 0))
2845 return -EINVAL;
2847 list_for_each_entry(kctl, &card->controls, list) {
2848 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2849 found = 1;
2850 break;
2853 if (found) {
2854 mc = (struct soc_mixer_control *)kctl->private_value;
2855 if (max <= mc->max) {
2856 mc->platform_max = max;
2857 ret = 0;
2860 return ret;
2862 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2865 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2866 * mixer info callback
2867 * @kcontrol: mixer control
2868 * @uinfo: control element information
2870 * Returns 0 for success.
2872 int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2873 struct snd_ctl_elem_info *uinfo)
2875 struct soc_mixer_control *mc =
2876 (struct soc_mixer_control *)kcontrol->private_value;
2877 int max = mc->max;
2878 int min = mc->min;
2880 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2881 uinfo->count = 2;
2882 uinfo->value.integer.min = 0;
2883 uinfo->value.integer.max = max-min;
2885 return 0;
2887 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2890 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2891 * mixer get callback
2892 * @kcontrol: mixer control
2893 * @uinfo: control element information
2895 * Returns 0 for success.
2897 int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2898 struct snd_ctl_elem_value *ucontrol)
2900 struct soc_mixer_control *mc =
2901 (struct soc_mixer_control *)kcontrol->private_value;
2902 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2903 unsigned int mask = (1<<mc->shift)-1;
2904 int min = mc->min;
2905 int val = snd_soc_read(codec, mc->reg) & mask;
2906 int valr = snd_soc_read(codec, mc->rreg) & mask;
2908 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2909 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
2910 return 0;
2912 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2915 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2916 * mixer put callback
2917 * @kcontrol: mixer control
2918 * @uinfo: control element information
2920 * Returns 0 for success.
2922 int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2923 struct snd_ctl_elem_value *ucontrol)
2925 struct soc_mixer_control *mc =
2926 (struct soc_mixer_control *)kcontrol->private_value;
2927 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2928 unsigned int mask = (1<<mc->shift)-1;
2929 int min = mc->min;
2930 int ret;
2931 unsigned int val, valr, oval, ovalr;
2933 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2934 val &= mask;
2935 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2936 valr &= mask;
2938 oval = snd_soc_read(codec, mc->reg) & mask;
2939 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2941 ret = 0;
2942 if (oval != val) {
2943 ret = snd_soc_write(codec, mc->reg, val);
2944 if (ret < 0)
2945 return ret;
2947 if (ovalr != valr) {
2948 ret = snd_soc_write(codec, mc->rreg, valr);
2949 if (ret < 0)
2950 return ret;
2953 return 0;
2955 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2958 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2959 * @dai: DAI
2960 * @clk_id: DAI specific clock ID
2961 * @freq: new clock frequency in Hz
2962 * @dir: new clock direction - input/output.
2964 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2966 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2967 unsigned int freq, int dir)
2969 if (dai->driver && dai->driver->ops->set_sysclk)
2970 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2971 else
2972 return -EINVAL;
2974 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2977 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2978 * @dai: DAI
2979 * @div_id: DAI specific clock divider ID
2980 * @div: new clock divisor.
2982 * Configures the clock dividers. This is used to derive the best DAI bit and
2983 * frame clocks from the system or master clock. It's best to set the DAI bit
2984 * and frame clocks as low as possible to save system power.
2986 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2987 int div_id, int div)
2989 if (dai->driver && dai->driver->ops->set_clkdiv)
2990 return dai->driver->ops->set_clkdiv(dai, div_id, div);
2991 else
2992 return -EINVAL;
2994 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2997 * snd_soc_dai_set_pll - configure DAI PLL.
2998 * @dai: DAI
2999 * @pll_id: DAI specific PLL ID
3000 * @source: DAI specific source for the PLL
3001 * @freq_in: PLL input clock frequency in Hz
3002 * @freq_out: requested PLL output clock frequency in Hz
3004 * Configures and enables PLL to generate output clock based on input clock.
3006 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3007 unsigned int freq_in, unsigned int freq_out)
3009 if (dai->driver && dai->driver->ops->set_pll)
3010 return dai->driver->ops->set_pll(dai, pll_id, source,
3011 freq_in, freq_out);
3012 else
3013 return -EINVAL;
3015 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3018 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3019 * @dai: DAI
3020 * @fmt: SND_SOC_DAIFMT_ format value.
3022 * Configures the DAI hardware format and clocking.
3024 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3026 if (dai->driver && dai->driver->ops->set_fmt)
3027 return dai->driver->ops->set_fmt(dai, fmt);
3028 else
3029 return -EINVAL;
3031 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3034 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3035 * @dai: DAI
3036 * @tx_mask: bitmask representing active TX slots.
3037 * @rx_mask: bitmask representing active RX slots.
3038 * @slots: Number of slots in use.
3039 * @slot_width: Width in bits for each slot.
3041 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3042 * specific.
3044 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3045 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
3047 if (dai->driver && dai->driver->ops->set_tdm_slot)
3048 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3049 slots, slot_width);
3050 else
3051 return -EINVAL;
3053 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3056 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3057 * @dai: DAI
3058 * @tx_num: how many TX channels
3059 * @tx_slot: pointer to an array which imply the TX slot number channel
3060 * 0~num-1 uses
3061 * @rx_num: how many RX channels
3062 * @rx_slot: pointer to an array which imply the RX slot number channel
3063 * 0~num-1 uses
3065 * configure the relationship between channel number and TDM slot number.
3067 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3068 unsigned int tx_num, unsigned int *tx_slot,
3069 unsigned int rx_num, unsigned int *rx_slot)
3071 if (dai->driver && dai->driver->ops->set_channel_map)
3072 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3073 rx_num, rx_slot);
3074 else
3075 return -EINVAL;
3077 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3080 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3081 * @dai: DAI
3082 * @tristate: tristate enable
3084 * Tristates the DAI so that others can use it.
3086 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3088 if (dai->driver && dai->driver->ops->set_tristate)
3089 return dai->driver->ops->set_tristate(dai, tristate);
3090 else
3091 return -EINVAL;
3093 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3096 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3097 * @dai: DAI
3098 * @mute: mute enable
3100 * Mutes the DAI DAC.
3102 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3104 if (dai->driver && dai->driver->ops->digital_mute)
3105 return dai->driver->ops->digital_mute(dai, mute);
3106 else
3107 return -EINVAL;
3109 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3112 * snd_soc_register_card - Register a card with the ASoC core
3114 * @card: Card to register
3117 int snd_soc_register_card(struct snd_soc_card *card)
3119 int i;
3121 if (!card->name || !card->dev)
3122 return -EINVAL;
3124 snd_soc_initialize_card_lists(card);
3126 soc_init_card_debugfs(card);
3128 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
3129 (card->num_links + card->num_aux_devs),
3130 GFP_KERNEL);
3131 if (card->rtd == NULL)
3132 return -ENOMEM;
3133 card->rtd_aux = &card->rtd[card->num_links];
3135 for (i = 0; i < card->num_links; i++)
3136 card->rtd[i].dai_link = &card->dai_link[i];
3138 INIT_LIST_HEAD(&card->list);
3139 card->instantiated = 0;
3140 mutex_init(&card->mutex);
3142 mutex_lock(&client_mutex);
3143 list_add(&card->list, &card_list);
3144 snd_soc_instantiate_cards();
3145 mutex_unlock(&client_mutex);
3147 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
3149 return 0;
3151 EXPORT_SYMBOL_GPL(snd_soc_register_card);
3154 * snd_soc_unregister_card - Unregister a card with the ASoC core
3156 * @card: Card to unregister
3159 int snd_soc_unregister_card(struct snd_soc_card *card)
3161 if (card->instantiated)
3162 soc_cleanup_card_resources(card);
3163 mutex_lock(&client_mutex);
3164 list_del(&card->list);
3165 mutex_unlock(&client_mutex);
3166 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3168 return 0;
3170 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3173 * Simplify DAI link configuration by removing ".-1" from device names
3174 * and sanitizing names.
3176 static char *fmt_single_name(struct device *dev, int *id)
3178 char *found, name[NAME_SIZE];
3179 int id1, id2;
3181 if (dev_name(dev) == NULL)
3182 return NULL;
3184 strlcpy(name, dev_name(dev), NAME_SIZE);
3186 /* are we a "%s.%d" name (platform and SPI components) */
3187 found = strstr(name, dev->driver->name);
3188 if (found) {
3189 /* get ID */
3190 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3192 /* discard ID from name if ID == -1 */
3193 if (*id == -1)
3194 found[strlen(dev->driver->name)] = '\0';
3197 } else {
3198 /* I2C component devices are named "bus-addr" */
3199 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3200 char tmp[NAME_SIZE];
3202 /* create unique ID number from I2C addr and bus */
3203 *id = ((id1 & 0xffff) << 16) + id2;
3205 /* sanitize component name for DAI link creation */
3206 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3207 strlcpy(name, tmp, NAME_SIZE);
3208 } else
3209 *id = 0;
3212 return kstrdup(name, GFP_KERNEL);
3216 * Simplify DAI link naming for single devices with multiple DAIs by removing
3217 * any ".-1" and using the DAI name (instead of device name).
3219 static inline char *fmt_multiple_name(struct device *dev,
3220 struct snd_soc_dai_driver *dai_drv)
3222 if (dai_drv->name == NULL) {
3223 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
3224 dev_name(dev));
3225 return NULL;
3228 return kstrdup(dai_drv->name, GFP_KERNEL);
3232 * snd_soc_register_dai - Register a DAI with the ASoC core
3234 * @dai: DAI to register
3236 int snd_soc_register_dai(struct device *dev,
3237 struct snd_soc_dai_driver *dai_drv)
3239 struct snd_soc_dai *dai;
3241 dev_dbg(dev, "dai register %s\n", dev_name(dev));
3243 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3244 if (dai == NULL)
3245 return -ENOMEM;
3247 /* create DAI component name */
3248 dai->name = fmt_single_name(dev, &dai->id);
3249 if (dai->name == NULL) {
3250 kfree(dai);
3251 return -ENOMEM;
3254 dai->dev = dev;
3255 dai->driver = dai_drv;
3256 if (!dai->driver->ops)
3257 dai->driver->ops = &null_dai_ops;
3259 mutex_lock(&client_mutex);
3260 list_add(&dai->list, &dai_list);
3261 snd_soc_instantiate_cards();
3262 mutex_unlock(&client_mutex);
3264 pr_debug("Registered DAI '%s'\n", dai->name);
3266 return 0;
3268 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3271 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3273 * @dai: DAI to unregister
3275 void snd_soc_unregister_dai(struct device *dev)
3277 struct snd_soc_dai *dai;
3279 list_for_each_entry(dai, &dai_list, list) {
3280 if (dev == dai->dev)
3281 goto found;
3283 return;
3285 found:
3286 mutex_lock(&client_mutex);
3287 list_del(&dai->list);
3288 mutex_unlock(&client_mutex);
3290 pr_debug("Unregistered DAI '%s'\n", dai->name);
3291 kfree(dai->name);
3292 kfree(dai);
3294 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3297 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3299 * @dai: Array of DAIs to register
3300 * @count: Number of DAIs
3302 int snd_soc_register_dais(struct device *dev,
3303 struct snd_soc_dai_driver *dai_drv, size_t count)
3305 struct snd_soc_dai *dai;
3306 int i, ret = 0;
3308 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
3310 for (i = 0; i < count; i++) {
3312 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3313 if (dai == NULL) {
3314 ret = -ENOMEM;
3315 goto err;
3318 /* create DAI component name */
3319 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3320 if (dai->name == NULL) {
3321 kfree(dai);
3322 ret = -EINVAL;
3323 goto err;
3326 dai->dev = dev;
3327 dai->driver = &dai_drv[i];
3328 if (dai->driver->id)
3329 dai->id = dai->driver->id;
3330 else
3331 dai->id = i;
3332 if (!dai->driver->ops)
3333 dai->driver->ops = &null_dai_ops;
3335 mutex_lock(&client_mutex);
3336 list_add(&dai->list, &dai_list);
3337 mutex_unlock(&client_mutex);
3339 pr_debug("Registered DAI '%s'\n", dai->name);
3342 mutex_lock(&client_mutex);
3343 snd_soc_instantiate_cards();
3344 mutex_unlock(&client_mutex);
3345 return 0;
3347 err:
3348 for (i--; i >= 0; i--)
3349 snd_soc_unregister_dai(dev);
3351 return ret;
3353 EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3356 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3358 * @dai: Array of DAIs to unregister
3359 * @count: Number of DAIs
3361 void snd_soc_unregister_dais(struct device *dev, size_t count)
3363 int i;
3365 for (i = 0; i < count; i++)
3366 snd_soc_unregister_dai(dev);
3368 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3371 * snd_soc_register_platform - Register a platform with the ASoC core
3373 * @platform: platform to register
3375 int snd_soc_register_platform(struct device *dev,
3376 struct snd_soc_platform_driver *platform_drv)
3378 struct snd_soc_platform *platform;
3380 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3382 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3383 if (platform == NULL)
3384 return -ENOMEM;
3386 /* create platform component name */
3387 platform->name = fmt_single_name(dev, &platform->id);
3388 if (platform->name == NULL) {
3389 kfree(platform);
3390 return -ENOMEM;
3393 platform->dev = dev;
3394 platform->driver = platform_drv;
3396 mutex_lock(&client_mutex);
3397 list_add(&platform->list, &platform_list);
3398 snd_soc_instantiate_cards();
3399 mutex_unlock(&client_mutex);
3401 pr_debug("Registered platform '%s'\n", platform->name);
3403 return 0;
3405 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3408 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3410 * @platform: platform to unregister
3412 void snd_soc_unregister_platform(struct device *dev)
3414 struct snd_soc_platform *platform;
3416 list_for_each_entry(platform, &platform_list, list) {
3417 if (dev == platform->dev)
3418 goto found;
3420 return;
3422 found:
3423 mutex_lock(&client_mutex);
3424 list_del(&platform->list);
3425 mutex_unlock(&client_mutex);
3427 pr_debug("Unregistered platform '%s'\n", platform->name);
3428 kfree(platform->name);
3429 kfree(platform);
3431 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3433 static u64 codec_format_map[] = {
3434 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3435 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3436 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3437 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3438 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3439 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3440 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3441 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3442 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3443 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3444 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3445 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3446 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3447 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3448 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3449 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3452 /* Fix up the DAI formats for endianness: codecs don't actually see
3453 * the endianness of the data but we're using the CPU format
3454 * definitions which do need to include endianness so we ensure that
3455 * codec DAIs always have both big and little endian variants set.
3457 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3459 int i;
3461 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3462 if (stream->formats & codec_format_map[i])
3463 stream->formats |= codec_format_map[i];
3467 * snd_soc_register_codec - Register a codec with the ASoC core
3469 * @codec: codec to register
3471 int snd_soc_register_codec(struct device *dev,
3472 const struct snd_soc_codec_driver *codec_drv,
3473 struct snd_soc_dai_driver *dai_drv,
3474 int num_dai)
3476 size_t reg_size;
3477 struct snd_soc_codec *codec;
3478 int ret, i;
3480 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3482 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3483 if (codec == NULL)
3484 return -ENOMEM;
3486 /* create CODEC component name */
3487 codec->name = fmt_single_name(dev, &codec->id);
3488 if (codec->name == NULL) {
3489 kfree(codec);
3490 return -ENOMEM;
3493 if (codec_drv->compress_type)
3494 codec->compress_type = codec_drv->compress_type;
3495 else
3496 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3498 codec->write = codec_drv->write;
3499 codec->read = codec_drv->read;
3500 codec->volatile_register = codec_drv->volatile_register;
3501 codec->readable_register = codec_drv->readable_register;
3502 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3503 codec->dapm.dev = dev;
3504 codec->dapm.codec = codec;
3505 codec->dapm.seq_notifier = codec_drv->seq_notifier;
3506 codec->dev = dev;
3507 codec->driver = codec_drv;
3508 codec->num_dai = num_dai;
3509 mutex_init(&codec->mutex);
3511 /* allocate CODEC register cache */
3512 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3513 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
3514 codec->reg_size = reg_size;
3515 /* it is necessary to make a copy of the default register cache
3516 * because in the case of using a compression type that requires
3517 * the default register cache to be marked as __devinitconst the
3518 * kernel might have freed the array by the time we initialize
3519 * the cache.
3521 if (codec_drv->reg_cache_default) {
3522 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3523 reg_size, GFP_KERNEL);
3524 if (!codec->reg_def_copy) {
3525 ret = -ENOMEM;
3526 goto fail;
3531 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3532 if (!codec->volatile_register)
3533 codec->volatile_register = snd_soc_default_volatile_register;
3534 if (!codec->readable_register)
3535 codec->readable_register = snd_soc_default_readable_register;
3538 for (i = 0; i < num_dai; i++) {
3539 fixup_codec_formats(&dai_drv[i].playback);
3540 fixup_codec_formats(&dai_drv[i].capture);
3543 /* register any DAIs */
3544 if (num_dai) {
3545 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3546 if (ret < 0)
3547 goto fail;
3550 mutex_lock(&client_mutex);
3551 list_add(&codec->list, &codec_list);
3552 snd_soc_instantiate_cards();
3553 mutex_unlock(&client_mutex);
3555 pr_debug("Registered codec '%s'\n", codec->name);
3556 return 0;
3558 fail:
3559 kfree(codec->reg_def_copy);
3560 codec->reg_def_copy = NULL;
3561 kfree(codec->name);
3562 kfree(codec);
3563 return ret;
3565 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3568 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3570 * @codec: codec to unregister
3572 void snd_soc_unregister_codec(struct device *dev)
3574 struct snd_soc_codec *codec;
3575 int i;
3577 list_for_each_entry(codec, &codec_list, list) {
3578 if (dev == codec->dev)
3579 goto found;
3581 return;
3583 found:
3584 if (codec->num_dai)
3585 for (i = 0; i < codec->num_dai; i++)
3586 snd_soc_unregister_dai(dev);
3588 mutex_lock(&client_mutex);
3589 list_del(&codec->list);
3590 mutex_unlock(&client_mutex);
3592 pr_debug("Unregistered codec '%s'\n", codec->name);
3594 snd_soc_cache_exit(codec);
3595 kfree(codec->reg_def_copy);
3596 kfree(codec->name);
3597 kfree(codec);
3599 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3601 static int __init snd_soc_init(void)
3603 #ifdef CONFIG_DEBUG_FS
3604 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3605 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
3606 printk(KERN_WARNING
3607 "ASoC: Failed to create debugfs directory\n");
3608 snd_soc_debugfs_root = NULL;
3611 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
3612 &codec_list_fops))
3613 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3615 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
3616 &dai_list_fops))
3617 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
3619 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
3620 &platform_list_fops))
3621 pr_warn("ASoC: Failed to create platform list debugfs file\n");
3622 #endif
3624 return platform_driver_register(&soc_driver);
3626 module_init(snd_soc_init);
3628 static void __exit snd_soc_exit(void)
3630 #ifdef CONFIG_DEBUG_FS
3631 debugfs_remove_recursive(snd_soc_debugfs_root);
3632 #endif
3633 platform_driver_unregister(&soc_driver);
3635 module_exit(snd_soc_exit);
3637 /* Module information */
3638 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3639 MODULE_DESCRIPTION("ALSA SoC Core");
3640 MODULE_LICENSE("GPL");
3641 MODULE_ALIAS("platform:soc-audio");