virtio_console: Don't access uninitialized data.
[linux/fpc-iii.git] / sound / soc / soc-core.c
blobe2bfe1d69e7469d36dfe8b3d81290ea7a2640423
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/ctype.h>
34 #include <linux/slab.h>
35 #include <sound/ac97_codec.h>
36 #include <sound/core.h>
37 #include <sound/jack.h>
38 #include <sound/pcm.h>
39 #include <sound/pcm_params.h>
40 #include <sound/soc.h>
41 #include <sound/initval.h>
43 #define CREATE_TRACE_POINTS
44 #include <trace/events/asoc.h>
46 #define NAME_SIZE 32
48 static DEFINE_MUTEX(pcm_mutex);
49 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
51 #ifdef CONFIG_DEBUG_FS
52 struct dentry *snd_soc_debugfs_root;
53 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
54 #endif
56 static DEFINE_MUTEX(client_mutex);
57 static LIST_HEAD(card_list);
58 static LIST_HEAD(dai_list);
59 static LIST_HEAD(platform_list);
60 static LIST_HEAD(codec_list);
62 static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
65 * This is a timeout to do a DAPM powerdown after a stream is closed().
66 * It can be used to eliminate pops between different playback streams, e.g.
67 * between two audio tracks.
69 static int pmdown_time = 5000;
70 module_param(pmdown_time, int, 0);
71 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
73 /* returns the minimum number of bytes needed to represent
74 * a particular given value */
75 static int min_bytes_needed(unsigned long val)
77 int c = 0;
78 int i;
80 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
81 if (val & (1UL << i))
82 break;
83 c = (sizeof val * 8) - c;
84 if (!c || (c % 8))
85 c = (c + 8) / 8;
86 else
87 c /= 8;
88 return c;
91 /* fill buf which is 'len' bytes with a formatted
92 * string of the form 'reg: value\n' */
93 static int format_register_str(struct snd_soc_codec *codec,
94 unsigned int reg, char *buf, size_t len)
96 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
97 int regsize = codec->driver->reg_word_size * 2;
98 int ret;
99 char tmpbuf[len + 1];
100 char regbuf[regsize + 1];
102 /* since tmpbuf is allocated on the stack, warn the callers if they
103 * try to abuse this function */
104 WARN_ON(len > 63);
106 /* +2 for ': ' and + 1 for '\n' */
107 if (wordsize + regsize + 2 + 1 != len)
108 return -EINVAL;
110 ret = snd_soc_read(codec , reg);
111 if (ret < 0) {
112 memset(regbuf, 'X', regsize);
113 regbuf[regsize] = '\0';
114 } else {
115 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
118 /* prepare the buffer */
119 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
120 /* copy it back to the caller without the '\0' */
121 memcpy(buf, tmpbuf, len);
123 return 0;
126 /* codec register dump */
127 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
128 size_t count, loff_t pos)
130 int i, step = 1;
131 int wordsize, regsize;
132 int len;
133 size_t total = 0;
134 loff_t p = 0;
136 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
137 regsize = codec->driver->reg_word_size * 2;
139 len = wordsize + regsize + 2 + 1;
141 if (!codec->driver->reg_cache_size)
142 return 0;
144 if (codec->driver->reg_cache_step)
145 step = codec->driver->reg_cache_step;
147 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
148 if (codec->readable_register && !codec->readable_register(codec, i))
149 continue;
150 if (codec->driver->display_register) {
151 count += codec->driver->display_register(codec, buf + count,
152 PAGE_SIZE - count, i);
153 } else {
154 /* only support larger than PAGE_SIZE bytes debugfs
155 * entries for the default case */
156 if (p >= pos) {
157 if (total + len >= count - 1)
158 break;
159 format_register_str(codec, i, buf + total, len);
160 total += len;
162 p += len;
166 total = min(total, count - 1);
168 return total;
171 static ssize_t codec_reg_show(struct device *dev,
172 struct device_attribute *attr, char *buf)
174 struct snd_soc_pcm_runtime *rtd =
175 container_of(dev, struct snd_soc_pcm_runtime, dev);
177 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
180 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
182 static ssize_t pmdown_time_show(struct device *dev,
183 struct device_attribute *attr, char *buf)
185 struct snd_soc_pcm_runtime *rtd =
186 container_of(dev, struct snd_soc_pcm_runtime, dev);
188 return sprintf(buf, "%ld\n", rtd->pmdown_time);
191 static ssize_t pmdown_time_set(struct device *dev,
192 struct device_attribute *attr,
193 const char *buf, size_t count)
195 struct snd_soc_pcm_runtime *rtd =
196 container_of(dev, struct snd_soc_pcm_runtime, dev);
197 int ret;
199 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
200 if (ret)
201 return ret;
203 return count;
206 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
208 #ifdef CONFIG_DEBUG_FS
209 static int codec_reg_open_file(struct inode *inode, struct file *file)
211 file->private_data = inode->i_private;
212 return 0;
215 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
216 size_t count, loff_t *ppos)
218 ssize_t ret;
219 struct snd_soc_codec *codec = file->private_data;
220 char *buf;
222 if (*ppos < 0 || !count)
223 return -EINVAL;
225 buf = kmalloc(count, GFP_KERNEL);
226 if (!buf)
227 return -ENOMEM;
229 ret = soc_codec_reg_show(codec, buf, count, *ppos);
230 if (ret >= 0) {
231 if (copy_to_user(user_buf, buf, ret)) {
232 kfree(buf);
233 return -EFAULT;
235 *ppos += ret;
238 kfree(buf);
239 return ret;
242 static ssize_t codec_reg_write_file(struct file *file,
243 const char __user *user_buf, size_t count, loff_t *ppos)
245 char buf[32];
246 size_t buf_size;
247 char *start = buf;
248 unsigned long reg, value;
249 int step = 1;
250 struct snd_soc_codec *codec = file->private_data;
252 buf_size = min(count, (sizeof(buf)-1));
253 if (copy_from_user(buf, user_buf, buf_size))
254 return -EFAULT;
255 buf[buf_size] = 0;
257 if (codec->driver->reg_cache_step)
258 step = codec->driver->reg_cache_step;
260 while (*start == ' ')
261 start++;
262 reg = simple_strtoul(start, &start, 16);
263 while (*start == ' ')
264 start++;
265 if (strict_strtoul(start, 16, &value))
266 return -EINVAL;
268 /* Userspace has been fiddling around behind the kernel's back */
269 add_taint(TAINT_USER);
271 snd_soc_write(codec, reg, value);
272 return buf_size;
275 static const struct file_operations codec_reg_fops = {
276 .open = codec_reg_open_file,
277 .read = codec_reg_read_file,
278 .write = codec_reg_write_file,
279 .llseek = default_llseek,
282 static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
284 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
286 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
287 debugfs_card_root);
288 if (!codec->debugfs_codec_root) {
289 printk(KERN_WARNING
290 "ASoC: Failed to create codec debugfs directory\n");
291 return;
294 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
295 &codec->cache_sync);
296 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
297 &codec->cache_only);
299 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
300 codec->debugfs_codec_root,
301 codec, &codec_reg_fops);
302 if (!codec->debugfs_reg)
303 printk(KERN_WARNING
304 "ASoC: Failed to create codec register debugfs file\n");
306 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
309 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
311 debugfs_remove_recursive(codec->debugfs_codec_root);
314 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
315 size_t count, loff_t *ppos)
317 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
318 ssize_t len, ret = 0;
319 struct snd_soc_codec *codec;
321 if (!buf)
322 return -ENOMEM;
324 list_for_each_entry(codec, &codec_list, list) {
325 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
326 codec->name);
327 if (len >= 0)
328 ret += len;
329 if (ret > PAGE_SIZE) {
330 ret = PAGE_SIZE;
331 break;
335 if (ret >= 0)
336 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
338 kfree(buf);
340 return ret;
343 static const struct file_operations codec_list_fops = {
344 .read = codec_list_read_file,
345 .llseek = default_llseek,/* read accesses f_pos */
348 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
349 size_t count, loff_t *ppos)
351 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
352 ssize_t len, ret = 0;
353 struct snd_soc_dai *dai;
355 if (!buf)
356 return -ENOMEM;
358 list_for_each_entry(dai, &dai_list, list) {
359 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
360 if (len >= 0)
361 ret += len;
362 if (ret > PAGE_SIZE) {
363 ret = PAGE_SIZE;
364 break;
368 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
370 kfree(buf);
372 return ret;
375 static const struct file_operations dai_list_fops = {
376 .read = dai_list_read_file,
377 .llseek = default_llseek,/* read accesses f_pos */
380 static ssize_t platform_list_read_file(struct file *file,
381 char __user *user_buf,
382 size_t count, loff_t *ppos)
384 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
385 ssize_t len, ret = 0;
386 struct snd_soc_platform *platform;
388 if (!buf)
389 return -ENOMEM;
391 list_for_each_entry(platform, &platform_list, list) {
392 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
393 platform->name);
394 if (len >= 0)
395 ret += len;
396 if (ret > PAGE_SIZE) {
397 ret = PAGE_SIZE;
398 break;
402 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
404 kfree(buf);
406 return ret;
409 static const struct file_operations platform_list_fops = {
410 .read = platform_list_read_file,
411 .llseek = default_llseek,/* read accesses f_pos */
414 static void soc_init_card_debugfs(struct snd_soc_card *card)
416 card->debugfs_card_root = debugfs_create_dir(card->name,
417 snd_soc_debugfs_root);
418 if (!card->debugfs_card_root) {
419 dev_warn(card->dev,
420 "ASoC: Failed to create codec debugfs directory\n");
421 return;
424 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
425 card->debugfs_card_root,
426 &card->pop_time);
427 if (!card->debugfs_pop_time)
428 dev_warn(card->dev,
429 "Failed to create pop time debugfs file\n");
432 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
434 debugfs_remove_recursive(card->debugfs_card_root);
437 #else
439 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
443 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
447 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
451 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
454 #endif
456 #ifdef CONFIG_SND_SOC_AC97_BUS
457 /* unregister ac97 codec */
458 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
460 if (codec->ac97->dev.bus)
461 device_unregister(&codec->ac97->dev);
462 return 0;
465 /* stop no dev release warning */
466 static void soc_ac97_device_release(struct device *dev){}
468 /* register ac97 codec to bus */
469 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
471 int err;
473 codec->ac97->dev.bus = &ac97_bus_type;
474 codec->ac97->dev.parent = codec->card->dev;
475 codec->ac97->dev.release = soc_ac97_device_release;
477 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
478 codec->card->snd_card->number, 0, codec->name);
479 err = device_register(&codec->ac97->dev);
480 if (err < 0) {
481 snd_printk(KERN_ERR "Can't register ac97 bus\n");
482 codec->ac97->dev.bus = NULL;
483 return err;
485 return 0;
487 #endif
489 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
491 struct snd_soc_pcm_runtime *rtd = substream->private_data;
492 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
493 struct snd_soc_dai *codec_dai = rtd->codec_dai;
494 int ret;
496 if (!codec_dai->driver->symmetric_rates &&
497 !cpu_dai->driver->symmetric_rates &&
498 !rtd->dai_link->symmetric_rates)
499 return 0;
501 /* This can happen if multiple streams are starting simultaneously -
502 * the second can need to get its constraints before the first has
503 * picked a rate. Complain and allow the application to carry on.
505 if (!rtd->rate) {
506 dev_warn(&rtd->dev,
507 "Not enforcing symmetric_rates due to race\n");
508 return 0;
511 dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n", rtd->rate);
513 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
514 SNDRV_PCM_HW_PARAM_RATE,
515 rtd->rate, rtd->rate);
516 if (ret < 0) {
517 dev_err(&rtd->dev,
518 "Unable to apply rate symmetry constraint: %d\n", ret);
519 return ret;
522 return 0;
526 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
527 * then initialized and any private data can be allocated. This also calls
528 * startup for the cpu DAI, platform, machine and codec DAI.
530 static int soc_pcm_open(struct snd_pcm_substream *substream)
532 struct snd_soc_pcm_runtime *rtd = substream->private_data;
533 struct snd_pcm_runtime *runtime = substream->runtime;
534 struct snd_soc_platform *platform = rtd->platform;
535 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
536 struct snd_soc_dai *codec_dai = rtd->codec_dai;
537 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
538 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
539 int ret = 0;
541 mutex_lock(&pcm_mutex);
543 /* startup the audio subsystem */
544 if (cpu_dai->driver->ops->startup) {
545 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
546 if (ret < 0) {
547 printk(KERN_ERR "asoc: can't open interface %s\n",
548 cpu_dai->name);
549 goto out;
553 if (platform->driver->ops && platform->driver->ops->open) {
554 ret = platform->driver->ops->open(substream);
555 if (ret < 0) {
556 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
557 goto platform_err;
561 if (codec_dai->driver->ops->startup) {
562 ret = codec_dai->driver->ops->startup(substream, codec_dai);
563 if (ret < 0) {
564 printk(KERN_ERR "asoc: can't open codec %s\n",
565 codec_dai->name);
566 goto codec_dai_err;
570 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
571 ret = rtd->dai_link->ops->startup(substream);
572 if (ret < 0) {
573 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
574 goto machine_err;
578 /* Check that the codec and cpu DAIs are compatible */
579 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
580 runtime->hw.rate_min =
581 max(codec_dai_drv->playback.rate_min,
582 cpu_dai_drv->playback.rate_min);
583 runtime->hw.rate_max =
584 min(codec_dai_drv->playback.rate_max,
585 cpu_dai_drv->playback.rate_max);
586 runtime->hw.channels_min =
587 max(codec_dai_drv->playback.channels_min,
588 cpu_dai_drv->playback.channels_min);
589 runtime->hw.channels_max =
590 min(codec_dai_drv->playback.channels_max,
591 cpu_dai_drv->playback.channels_max);
592 runtime->hw.formats =
593 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
594 runtime->hw.rates =
595 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
596 if (codec_dai_drv->playback.rates
597 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
598 runtime->hw.rates |= cpu_dai_drv->playback.rates;
599 if (cpu_dai_drv->playback.rates
600 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
601 runtime->hw.rates |= codec_dai_drv->playback.rates;
602 } else {
603 runtime->hw.rate_min =
604 max(codec_dai_drv->capture.rate_min,
605 cpu_dai_drv->capture.rate_min);
606 runtime->hw.rate_max =
607 min(codec_dai_drv->capture.rate_max,
608 cpu_dai_drv->capture.rate_max);
609 runtime->hw.channels_min =
610 max(codec_dai_drv->capture.channels_min,
611 cpu_dai_drv->capture.channels_min);
612 runtime->hw.channels_max =
613 min(codec_dai_drv->capture.channels_max,
614 cpu_dai_drv->capture.channels_max);
615 runtime->hw.formats =
616 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
617 runtime->hw.rates =
618 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
619 if (codec_dai_drv->capture.rates
620 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
621 runtime->hw.rates |= cpu_dai_drv->capture.rates;
622 if (cpu_dai_drv->capture.rates
623 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
624 runtime->hw.rates |= codec_dai_drv->capture.rates;
627 ret = -EINVAL;
628 snd_pcm_limit_hw_rates(runtime);
629 if (!runtime->hw.rates) {
630 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
631 codec_dai->name, cpu_dai->name);
632 goto config_err;
634 if (!runtime->hw.formats) {
635 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
636 codec_dai->name, cpu_dai->name);
637 goto config_err;
639 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
640 runtime->hw.channels_min > runtime->hw.channels_max) {
641 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
642 codec_dai->name, cpu_dai->name);
643 goto config_err;
646 /* Symmetry only applies if we've already got an active stream. */
647 if (cpu_dai->active || codec_dai->active) {
648 ret = soc_pcm_apply_symmetry(substream);
649 if (ret != 0)
650 goto config_err;
653 pr_debug("asoc: %s <-> %s info:\n",
654 codec_dai->name, cpu_dai->name);
655 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
656 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
657 runtime->hw.channels_max);
658 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
659 runtime->hw.rate_max);
661 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
662 cpu_dai->playback_active++;
663 codec_dai->playback_active++;
664 } else {
665 cpu_dai->capture_active++;
666 codec_dai->capture_active++;
668 cpu_dai->active++;
669 codec_dai->active++;
670 rtd->codec->active++;
671 mutex_unlock(&pcm_mutex);
672 return 0;
674 config_err:
675 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
676 rtd->dai_link->ops->shutdown(substream);
678 machine_err:
679 if (codec_dai->driver->ops->shutdown)
680 codec_dai->driver->ops->shutdown(substream, codec_dai);
682 codec_dai_err:
683 if (platform->driver->ops && platform->driver->ops->close)
684 platform->driver->ops->close(substream);
686 platform_err:
687 if (cpu_dai->driver->ops->shutdown)
688 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
689 out:
690 mutex_unlock(&pcm_mutex);
691 return ret;
695 * Power down the audio subsystem pmdown_time msecs after close is called.
696 * This is to ensure there are no pops or clicks in between any music tracks
697 * due to DAPM power cycling.
699 static void close_delayed_work(struct work_struct *work)
701 struct snd_soc_pcm_runtime *rtd =
702 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
703 struct snd_soc_dai *codec_dai = rtd->codec_dai;
705 mutex_lock(&pcm_mutex);
707 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
708 codec_dai->driver->playback.stream_name,
709 codec_dai->playback_active ? "active" : "inactive",
710 codec_dai->pop_wait ? "yes" : "no");
712 /* are we waiting on this codec DAI stream */
713 if (codec_dai->pop_wait == 1) {
714 codec_dai->pop_wait = 0;
715 snd_soc_dapm_stream_event(rtd,
716 codec_dai->driver->playback.stream_name,
717 SND_SOC_DAPM_STREAM_STOP);
720 mutex_unlock(&pcm_mutex);
724 * Called by ALSA when a PCM substream is closed. Private data can be
725 * freed here. The cpu DAI, codec DAI, machine and platform are also
726 * shutdown.
728 static int soc_codec_close(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 struct snd_soc_codec *codec = rtd->codec;
736 mutex_lock(&pcm_mutex);
738 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
739 cpu_dai->playback_active--;
740 codec_dai->playback_active--;
741 } else {
742 cpu_dai->capture_active--;
743 codec_dai->capture_active--;
746 cpu_dai->active--;
747 codec_dai->active--;
748 codec->active--;
750 /* Muting the DAC suppresses artifacts caused during digital
751 * shutdown, for example from stopping clocks.
753 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
754 snd_soc_dai_digital_mute(codec_dai, 1);
756 if (cpu_dai->driver->ops->shutdown)
757 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
759 if (codec_dai->driver->ops->shutdown)
760 codec_dai->driver->ops->shutdown(substream, codec_dai);
762 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
763 rtd->dai_link->ops->shutdown(substream);
765 if (platform->driver->ops && platform->driver->ops->close)
766 platform->driver->ops->close(substream);
767 cpu_dai->runtime = NULL;
769 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
770 /* start delayed pop wq here for playback streams */
771 codec_dai->pop_wait = 1;
772 schedule_delayed_work(&rtd->delayed_work,
773 msecs_to_jiffies(rtd->pmdown_time));
774 } else {
775 /* capture streams can be powered down now */
776 snd_soc_dapm_stream_event(rtd,
777 codec_dai->driver->capture.stream_name,
778 SND_SOC_DAPM_STREAM_STOP);
781 mutex_unlock(&pcm_mutex);
782 return 0;
786 * Called by ALSA when the PCM substream is prepared, can set format, sample
787 * rate, etc. This function is non atomic and can be called multiple times,
788 * it can refer to the runtime info.
790 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
792 struct snd_soc_pcm_runtime *rtd = substream->private_data;
793 struct snd_soc_platform *platform = rtd->platform;
794 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
795 struct snd_soc_dai *codec_dai = rtd->codec_dai;
796 int ret = 0;
798 mutex_lock(&pcm_mutex);
800 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
801 ret = rtd->dai_link->ops->prepare(substream);
802 if (ret < 0) {
803 printk(KERN_ERR "asoc: machine prepare error\n");
804 goto out;
808 if (platform->driver->ops && platform->driver->ops->prepare) {
809 ret = platform->driver->ops->prepare(substream);
810 if (ret < 0) {
811 printk(KERN_ERR "asoc: platform prepare error\n");
812 goto out;
816 if (codec_dai->driver->ops->prepare) {
817 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
818 if (ret < 0) {
819 printk(KERN_ERR "asoc: codec DAI prepare error\n");
820 goto out;
824 if (cpu_dai->driver->ops->prepare) {
825 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
826 if (ret < 0) {
827 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
828 goto out;
832 /* cancel any delayed stream shutdown that is pending */
833 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
834 codec_dai->pop_wait) {
835 codec_dai->pop_wait = 0;
836 cancel_delayed_work(&rtd->delayed_work);
839 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
840 snd_soc_dapm_stream_event(rtd,
841 codec_dai->driver->playback.stream_name,
842 SND_SOC_DAPM_STREAM_START);
843 else
844 snd_soc_dapm_stream_event(rtd,
845 codec_dai->driver->capture.stream_name,
846 SND_SOC_DAPM_STREAM_START);
848 snd_soc_dai_digital_mute(codec_dai, 0);
850 out:
851 mutex_unlock(&pcm_mutex);
852 return ret;
856 * Called by ALSA when the hardware params are set by application. This
857 * function can also be called multiple times and can allocate buffers
858 * (using snd_pcm_lib_* ). It's non-atomic.
860 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
861 struct snd_pcm_hw_params *params)
863 struct snd_soc_pcm_runtime *rtd = substream->private_data;
864 struct snd_soc_platform *platform = rtd->platform;
865 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
866 struct snd_soc_dai *codec_dai = rtd->codec_dai;
867 int ret = 0;
869 mutex_lock(&pcm_mutex);
871 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
872 ret = rtd->dai_link->ops->hw_params(substream, params);
873 if (ret < 0) {
874 printk(KERN_ERR "asoc: machine hw_params failed\n");
875 goto out;
879 if (codec_dai->driver->ops->hw_params) {
880 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
881 if (ret < 0) {
882 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
883 codec_dai->name);
884 goto codec_err;
888 if (cpu_dai->driver->ops->hw_params) {
889 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
890 if (ret < 0) {
891 printk(KERN_ERR "asoc: interface %s hw params failed\n",
892 cpu_dai->name);
893 goto interface_err;
897 if (platform->driver->ops && platform->driver->ops->hw_params) {
898 ret = platform->driver->ops->hw_params(substream, params);
899 if (ret < 0) {
900 printk(KERN_ERR "asoc: platform %s hw params failed\n",
901 platform->name);
902 goto platform_err;
906 rtd->rate = params_rate(params);
908 out:
909 mutex_unlock(&pcm_mutex);
910 return ret;
912 platform_err:
913 if (cpu_dai->driver->ops->hw_free)
914 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
916 interface_err:
917 if (codec_dai->driver->ops->hw_free)
918 codec_dai->driver->ops->hw_free(substream, codec_dai);
920 codec_err:
921 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
922 rtd->dai_link->ops->hw_free(substream);
924 mutex_unlock(&pcm_mutex);
925 return ret;
929 * Frees resources allocated by hw_params, can be called multiple times
931 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
933 struct snd_soc_pcm_runtime *rtd = substream->private_data;
934 struct snd_soc_platform *platform = rtd->platform;
935 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
936 struct snd_soc_dai *codec_dai = rtd->codec_dai;
937 struct snd_soc_codec *codec = rtd->codec;
939 mutex_lock(&pcm_mutex);
941 /* apply codec digital mute */
942 if (!codec->active)
943 snd_soc_dai_digital_mute(codec_dai, 1);
945 /* free any machine hw params */
946 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
947 rtd->dai_link->ops->hw_free(substream);
949 /* free any DMA resources */
950 if (platform->driver->ops && platform->driver->ops->hw_free)
951 platform->driver->ops->hw_free(substream);
953 /* now free hw params for the DAIs */
954 if (codec_dai->driver->ops->hw_free)
955 codec_dai->driver->ops->hw_free(substream, codec_dai);
957 if (cpu_dai->driver->ops->hw_free)
958 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
960 mutex_unlock(&pcm_mutex);
961 return 0;
964 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
966 struct snd_soc_pcm_runtime *rtd = substream->private_data;
967 struct snd_soc_platform *platform = rtd->platform;
968 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
969 struct snd_soc_dai *codec_dai = rtd->codec_dai;
970 int ret;
972 if (codec_dai->driver->ops->trigger) {
973 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
974 if (ret < 0)
975 return ret;
978 if (platform->driver->ops && platform->driver->ops->trigger) {
979 ret = platform->driver->ops->trigger(substream, cmd);
980 if (ret < 0)
981 return ret;
984 if (cpu_dai->driver->ops->trigger) {
985 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
986 if (ret < 0)
987 return ret;
989 return 0;
993 * soc level wrapper for pointer callback
994 * If cpu_dai, codec_dai, platform driver has the delay callback, than
995 * the runtime->delay will be updated accordingly.
997 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
999 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1000 struct snd_soc_platform *platform = rtd->platform;
1001 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1002 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1003 struct snd_pcm_runtime *runtime = substream->runtime;
1004 snd_pcm_uframes_t offset = 0;
1005 snd_pcm_sframes_t delay = 0;
1007 if (platform->driver->ops && platform->driver->ops->pointer)
1008 offset = platform->driver->ops->pointer(substream);
1010 if (cpu_dai->driver->ops->delay)
1011 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1013 if (codec_dai->driver->ops->delay)
1014 delay += codec_dai->driver->ops->delay(substream, codec_dai);
1016 if (platform->driver->delay)
1017 delay += platform->driver->delay(substream, codec_dai);
1019 runtime->delay = delay;
1021 return offset;
1024 /* ASoC PCM operations */
1025 static struct snd_pcm_ops soc_pcm_ops = {
1026 .open = soc_pcm_open,
1027 .close = soc_codec_close,
1028 .hw_params = soc_pcm_hw_params,
1029 .hw_free = soc_pcm_hw_free,
1030 .prepare = soc_pcm_prepare,
1031 .trigger = soc_pcm_trigger,
1032 .pointer = soc_pcm_pointer,
1035 #ifdef CONFIG_PM_SLEEP
1036 /* powers down audio subsystem for suspend */
1037 int snd_soc_suspend(struct device *dev)
1039 struct snd_soc_card *card = dev_get_drvdata(dev);
1040 struct snd_soc_codec *codec;
1041 int i;
1043 /* If the initialization of this soc device failed, there is no codec
1044 * associated with it. Just bail out in this case.
1046 if (list_empty(&card->codec_dev_list))
1047 return 0;
1049 /* Due to the resume being scheduled into a workqueue we could
1050 * suspend before that's finished - wait for it to complete.
1052 snd_power_lock(card->snd_card);
1053 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
1054 snd_power_unlock(card->snd_card);
1056 /* we're going to block userspace touching us until resume completes */
1057 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
1059 /* mute any active DACs */
1060 for (i = 0; i < card->num_rtd; i++) {
1061 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1062 struct snd_soc_dai_driver *drv = dai->driver;
1064 if (card->rtd[i].dai_link->ignore_suspend)
1065 continue;
1067 if (drv->ops->digital_mute && dai->playback_active)
1068 drv->ops->digital_mute(dai, 1);
1071 /* suspend all pcms */
1072 for (i = 0; i < card->num_rtd; i++) {
1073 if (card->rtd[i].dai_link->ignore_suspend)
1074 continue;
1076 snd_pcm_suspend_all(card->rtd[i].pcm);
1079 if (card->suspend_pre)
1080 card->suspend_pre(card);
1082 for (i = 0; i < card->num_rtd; i++) {
1083 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1084 struct snd_soc_platform *platform = card->rtd[i].platform;
1086 if (card->rtd[i].dai_link->ignore_suspend)
1087 continue;
1089 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
1090 cpu_dai->driver->suspend(cpu_dai);
1091 if (platform->driver->suspend && !platform->suspended) {
1092 platform->driver->suspend(cpu_dai);
1093 platform->suspended = 1;
1097 /* close any waiting streams and save state */
1098 for (i = 0; i < card->num_rtd; i++) {
1099 flush_delayed_work_sync(&card->rtd[i].delayed_work);
1100 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
1103 for (i = 0; i < card->num_rtd; i++) {
1104 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1106 if (card->rtd[i].dai_link->ignore_suspend)
1107 continue;
1109 if (driver->playback.stream_name != NULL)
1110 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1111 SND_SOC_DAPM_STREAM_SUSPEND);
1113 if (driver->capture.stream_name != NULL)
1114 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1115 SND_SOC_DAPM_STREAM_SUSPEND);
1118 /* suspend all CODECs */
1119 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
1120 /* If there are paths active then the CODEC will be held with
1121 * bias _ON and should not be suspended. */
1122 if (!codec->suspended && codec->driver->suspend) {
1123 switch (codec->dapm.bias_level) {
1124 case SND_SOC_BIAS_STANDBY:
1125 case SND_SOC_BIAS_OFF:
1126 codec->driver->suspend(codec, PMSG_SUSPEND);
1127 codec->suspended = 1;
1128 codec->cache_sync = 1;
1129 break;
1130 default:
1131 dev_dbg(codec->dev, "CODEC is on over suspend\n");
1132 break;
1137 for (i = 0; i < card->num_rtd; i++) {
1138 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1140 if (card->rtd[i].dai_link->ignore_suspend)
1141 continue;
1143 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1144 cpu_dai->driver->suspend(cpu_dai);
1147 if (card->suspend_post)
1148 card->suspend_post(card);
1150 return 0;
1152 EXPORT_SYMBOL_GPL(snd_soc_suspend);
1154 /* deferred resume work, so resume can complete before we finished
1155 * setting our codec back up, which can be very slow on I2C
1157 static void soc_resume_deferred(struct work_struct *work)
1159 struct snd_soc_card *card =
1160 container_of(work, struct snd_soc_card, deferred_resume_work);
1161 struct snd_soc_codec *codec;
1162 int i;
1164 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1165 * so userspace apps are blocked from touching us
1168 dev_dbg(card->dev, "starting resume work\n");
1170 /* Bring us up into D2 so that DAPM starts enabling things */
1171 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
1173 if (card->resume_pre)
1174 card->resume_pre(card);
1176 /* resume AC97 DAIs */
1177 for (i = 0; i < card->num_rtd; i++) {
1178 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1180 if (card->rtd[i].dai_link->ignore_suspend)
1181 continue;
1183 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1184 cpu_dai->driver->resume(cpu_dai);
1187 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
1188 /* If the CODEC was idle over suspend then it will have been
1189 * left with bias OFF or STANDBY and suspended so we must now
1190 * resume. Otherwise the suspend was suppressed.
1192 if (codec->driver->resume && codec->suspended) {
1193 switch (codec->dapm.bias_level) {
1194 case SND_SOC_BIAS_STANDBY:
1195 case SND_SOC_BIAS_OFF:
1196 codec->driver->resume(codec);
1197 codec->suspended = 0;
1198 break;
1199 default:
1200 dev_dbg(codec->dev, "CODEC was on over suspend\n");
1201 break;
1206 for (i = 0; i < card->num_rtd; i++) {
1207 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1209 if (card->rtd[i].dai_link->ignore_suspend)
1210 continue;
1212 if (driver->playback.stream_name != NULL)
1213 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1214 SND_SOC_DAPM_STREAM_RESUME);
1216 if (driver->capture.stream_name != NULL)
1217 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1218 SND_SOC_DAPM_STREAM_RESUME);
1221 /* unmute any active DACs */
1222 for (i = 0; i < card->num_rtd; i++) {
1223 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1224 struct snd_soc_dai_driver *drv = dai->driver;
1226 if (card->rtd[i].dai_link->ignore_suspend)
1227 continue;
1229 if (drv->ops->digital_mute && dai->playback_active)
1230 drv->ops->digital_mute(dai, 0);
1233 for (i = 0; i < card->num_rtd; i++) {
1234 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1235 struct snd_soc_platform *platform = card->rtd[i].platform;
1237 if (card->rtd[i].dai_link->ignore_suspend)
1238 continue;
1240 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1241 cpu_dai->driver->resume(cpu_dai);
1242 if (platform->driver->resume && platform->suspended) {
1243 platform->driver->resume(cpu_dai);
1244 platform->suspended = 0;
1248 if (card->resume_post)
1249 card->resume_post(card);
1251 dev_dbg(card->dev, "resume work completed\n");
1253 /* userspace can access us now we are back as we were before */
1254 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
1257 /* powers up audio subsystem after a suspend */
1258 int snd_soc_resume(struct device *dev)
1260 struct snd_soc_card *card = dev_get_drvdata(dev);
1261 int i, ac97_control = 0;
1263 /* AC97 devices might have other drivers hanging off them so
1264 * need to resume immediately. Other drivers don't have that
1265 * problem and may take a substantial amount of time to resume
1266 * due to I/O costs and anti-pop so handle them out of line.
1268 for (i = 0; i < card->num_rtd; i++) {
1269 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1270 ac97_control |= cpu_dai->driver->ac97_control;
1272 if (ac97_control) {
1273 dev_dbg(dev, "Resuming AC97 immediately\n");
1274 soc_resume_deferred(&card->deferred_resume_work);
1275 } else {
1276 dev_dbg(dev, "Scheduling resume work\n");
1277 if (!schedule_work(&card->deferred_resume_work))
1278 dev_err(dev, "resume work item may be lost\n");
1281 return 0;
1283 EXPORT_SYMBOL_GPL(snd_soc_resume);
1284 #else
1285 #define snd_soc_suspend NULL
1286 #define snd_soc_resume NULL
1287 #endif
1289 static struct snd_soc_dai_ops null_dai_ops = {
1292 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
1294 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1295 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1296 struct snd_soc_codec *codec;
1297 struct snd_soc_platform *platform;
1298 struct snd_soc_dai *codec_dai, *cpu_dai;
1299 const char *platform_name;
1301 if (rtd->complete)
1302 return 1;
1303 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
1305 /* do we already have the CPU DAI for this link ? */
1306 if (rtd->cpu_dai) {
1307 goto find_codec;
1309 /* no, then find CPU DAI from registered DAIs*/
1310 list_for_each_entry(cpu_dai, &dai_list, list) {
1311 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1312 rtd->cpu_dai = cpu_dai;
1313 goto find_codec;
1316 dev_dbg(card->dev, "CPU DAI %s not registered\n",
1317 dai_link->cpu_dai_name);
1319 find_codec:
1320 /* do we already have the CODEC for this link ? */
1321 if (rtd->codec) {
1322 goto find_platform;
1325 /* no, then find CODEC from registered CODECs*/
1326 list_for_each_entry(codec, &codec_list, list) {
1327 if (!strcmp(codec->name, dai_link->codec_name)) {
1328 rtd->codec = codec;
1330 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1331 list_for_each_entry(codec_dai, &dai_list, list) {
1332 if (codec->dev == codec_dai->dev &&
1333 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1334 rtd->codec_dai = codec_dai;
1335 goto find_platform;
1338 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1339 dai_link->codec_dai_name);
1341 goto find_platform;
1344 dev_dbg(card->dev, "CODEC %s not registered\n",
1345 dai_link->codec_name);
1347 find_platform:
1348 /* do we need a platform? */
1349 if (rtd->platform)
1350 goto out;
1352 /* if there's no platform we match on the empty platform */
1353 platform_name = dai_link->platform_name;
1354 if (!platform_name)
1355 platform_name = "snd-soc-dummy";
1357 /* no, then find one from the set of registered platforms */
1358 list_for_each_entry(platform, &platform_list, list) {
1359 if (!strcmp(platform->name, platform_name)) {
1360 rtd->platform = platform;
1361 goto out;
1365 dev_dbg(card->dev, "platform %s not registered\n",
1366 dai_link->platform_name);
1367 return 0;
1369 out:
1370 /* mark rtd as complete if we found all 4 of our client devices */
1371 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1372 rtd->complete = 1;
1373 card->num_rtd++;
1375 return 1;
1378 static void soc_remove_codec(struct snd_soc_codec *codec)
1380 int err;
1382 if (codec->driver->remove) {
1383 err = codec->driver->remove(codec);
1384 if (err < 0)
1385 dev_err(codec->dev,
1386 "asoc: failed to remove %s: %d\n",
1387 codec->name, err);
1390 /* Make sure all DAPM widgets are freed */
1391 snd_soc_dapm_free(&codec->dapm);
1393 soc_cleanup_codec_debugfs(codec);
1394 codec->probed = 0;
1395 list_del(&codec->card_list);
1396 module_put(codec->dev->driver->owner);
1399 static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1401 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1402 struct snd_soc_codec *codec = rtd->codec;
1403 struct snd_soc_platform *platform = rtd->platform;
1404 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1405 int err;
1407 /* unregister the rtd device */
1408 if (rtd->dev_registered) {
1409 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
1410 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1411 device_unregister(&rtd->dev);
1412 rtd->dev_registered = 0;
1415 /* remove the CODEC DAI */
1416 if (codec_dai && codec_dai->probed) {
1417 if (codec_dai->driver->remove) {
1418 err = codec_dai->driver->remove(codec_dai);
1419 if (err < 0)
1420 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1422 codec_dai->probed = 0;
1423 list_del(&codec_dai->card_list);
1426 /* remove the platform */
1427 if (platform && platform->probed) {
1428 if (platform->driver->remove) {
1429 err = platform->driver->remove(platform);
1430 if (err < 0)
1431 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1433 platform->probed = 0;
1434 list_del(&platform->card_list);
1435 module_put(platform->dev->driver->owner);
1438 /* remove the CODEC */
1439 if (codec && codec->probed)
1440 soc_remove_codec(codec);
1442 /* remove the cpu_dai */
1443 if (cpu_dai && cpu_dai->probed) {
1444 if (cpu_dai->driver->remove) {
1445 err = cpu_dai->driver->remove(cpu_dai);
1446 if (err < 0)
1447 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1449 cpu_dai->probed = 0;
1450 list_del(&cpu_dai->card_list);
1451 module_put(cpu_dai->dev->driver->owner);
1455 static void soc_remove_dai_links(struct snd_soc_card *card)
1457 int i;
1459 for (i = 0; i < card->num_rtd; i++)
1460 soc_remove_dai_link(card, i);
1462 card->num_rtd = 0;
1465 static void soc_set_name_prefix(struct snd_soc_card *card,
1466 struct snd_soc_codec *codec)
1468 int i;
1470 if (card->codec_conf == NULL)
1471 return;
1473 for (i = 0; i < card->num_configs; i++) {
1474 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1475 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1476 codec->name_prefix = map->name_prefix;
1477 break;
1482 static int soc_probe_codec(struct snd_soc_card *card,
1483 struct snd_soc_codec *codec)
1485 int ret = 0;
1486 const struct snd_soc_codec_driver *driver = codec->driver;
1488 codec->card = card;
1489 codec->dapm.card = card;
1490 soc_set_name_prefix(card, codec);
1492 if (!try_module_get(codec->dev->driver->owner))
1493 return -ENODEV;
1495 soc_init_codec_debugfs(codec);
1497 if (driver->dapm_widgets)
1498 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1499 driver->num_dapm_widgets);
1501 if (driver->probe) {
1502 ret = driver->probe(codec);
1503 if (ret < 0) {
1504 dev_err(codec->dev,
1505 "asoc: failed to probe CODEC %s: %d\n",
1506 codec->name, ret);
1507 goto err_probe;
1511 if (driver->controls)
1512 snd_soc_add_controls(codec, driver->controls,
1513 driver->num_controls);
1514 if (driver->dapm_routes)
1515 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1516 driver->num_dapm_routes);
1518 /* mark codec as probed and add to card codec list */
1519 codec->probed = 1;
1520 list_add(&codec->card_list, &card->codec_dev_list);
1521 list_add(&codec->dapm.list, &card->dapm_list);
1523 return 0;
1525 err_probe:
1526 soc_cleanup_codec_debugfs(codec);
1527 module_put(codec->dev->driver->owner);
1529 return ret;
1532 static void rtd_release(struct device *dev) {}
1534 static int soc_post_component_init(struct snd_soc_card *card,
1535 struct snd_soc_codec *codec,
1536 int num, int dailess)
1538 struct snd_soc_dai_link *dai_link = NULL;
1539 struct snd_soc_aux_dev *aux_dev = NULL;
1540 struct snd_soc_pcm_runtime *rtd;
1541 const char *temp, *name;
1542 int ret = 0;
1544 if (!dailess) {
1545 dai_link = &card->dai_link[num];
1546 rtd = &card->rtd[num];
1547 name = dai_link->name;
1548 } else {
1549 aux_dev = &card->aux_dev[num];
1550 rtd = &card->rtd_aux[num];
1551 name = aux_dev->name;
1553 rtd->card = card;
1555 /* machine controls, routes and widgets are not prefixed */
1556 temp = codec->name_prefix;
1557 codec->name_prefix = NULL;
1559 /* do machine specific initialization */
1560 if (!dailess && dai_link->init)
1561 ret = dai_link->init(rtd);
1562 else if (dailess && aux_dev->init)
1563 ret = aux_dev->init(&codec->dapm);
1564 if (ret < 0) {
1565 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1566 return ret;
1568 codec->name_prefix = temp;
1570 /* Make sure all DAPM widgets are instantiated */
1571 snd_soc_dapm_new_widgets(&codec->dapm);
1573 /* register the rtd device */
1574 rtd->codec = codec;
1575 rtd->dev.parent = card->dev;
1576 rtd->dev.release = rtd_release;
1577 rtd->dev.init_name = name;
1578 ret = device_register(&rtd->dev);
1579 if (ret < 0) {
1580 dev_err(card->dev,
1581 "asoc: failed to register runtime device: %d\n", ret);
1582 return ret;
1584 rtd->dev_registered = 1;
1586 /* add DAPM sysfs entries for this codec */
1587 ret = snd_soc_dapm_sys_add(&rtd->dev);
1588 if (ret < 0)
1589 dev_err(codec->dev,
1590 "asoc: failed to add codec dapm sysfs entries: %d\n",
1591 ret);
1593 /* add codec sysfs entries */
1594 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1595 if (ret < 0)
1596 dev_err(codec->dev,
1597 "asoc: failed to add codec sysfs files: %d\n", ret);
1599 return 0;
1602 static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1604 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1605 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1606 struct snd_soc_codec *codec = rtd->codec;
1607 struct snd_soc_platform *platform = rtd->platform;
1608 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1609 int ret;
1611 dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1613 /* config components */
1614 codec_dai->codec = codec;
1615 cpu_dai->platform = platform;
1616 codec_dai->card = card;
1617 cpu_dai->card = card;
1619 /* set default power off timeout */
1620 rtd->pmdown_time = pmdown_time;
1622 /* probe the cpu_dai */
1623 if (!cpu_dai->probed) {
1624 if (!try_module_get(cpu_dai->dev->driver->owner))
1625 return -ENODEV;
1627 if (cpu_dai->driver->probe) {
1628 ret = cpu_dai->driver->probe(cpu_dai);
1629 if (ret < 0) {
1630 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1631 cpu_dai->name);
1632 module_put(cpu_dai->dev->driver->owner);
1633 return ret;
1636 cpu_dai->probed = 1;
1637 /* mark cpu_dai as probed and add to card cpu_dai list */
1638 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1641 /* probe the CODEC */
1642 if (!codec->probed) {
1643 ret = soc_probe_codec(card, codec);
1644 if (ret < 0)
1645 return ret;
1648 /* probe the platform */
1649 if (!platform->probed) {
1650 if (!try_module_get(platform->dev->driver->owner))
1651 return -ENODEV;
1653 if (platform->driver->probe) {
1654 ret = platform->driver->probe(platform);
1655 if (ret < 0) {
1656 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1657 platform->name);
1658 module_put(platform->dev->driver->owner);
1659 return ret;
1662 /* mark platform as probed and add to card platform list */
1663 platform->probed = 1;
1664 list_add(&platform->card_list, &card->platform_dev_list);
1667 /* probe the CODEC DAI */
1668 if (!codec_dai->probed) {
1669 if (codec_dai->driver->probe) {
1670 ret = codec_dai->driver->probe(codec_dai);
1671 if (ret < 0) {
1672 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1673 codec_dai->name);
1674 return ret;
1678 /* mark cpu_dai as probed and add to card cpu_dai list */
1679 codec_dai->probed = 1;
1680 list_add(&codec_dai->card_list, &card->dai_dev_list);
1683 /* DAPM dai link stream work */
1684 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1686 ret = soc_post_component_init(card, codec, num, 0);
1687 if (ret)
1688 return ret;
1690 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1691 if (ret < 0)
1692 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1694 /* create the pcm */
1695 ret = soc_new_pcm(rtd, num);
1696 if (ret < 0) {
1697 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1698 return ret;
1701 /* add platform data for AC97 devices */
1702 if (rtd->codec_dai->driver->ac97_control)
1703 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1705 return 0;
1708 #ifdef CONFIG_SND_SOC_AC97_BUS
1709 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1711 int ret;
1713 /* Only instantiate AC97 if not already done by the adaptor
1714 * for the generic AC97 subsystem.
1716 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
1718 * It is possible that the AC97 device is already registered to
1719 * the device subsystem. This happens when the device is created
1720 * via snd_ac97_mixer(). Currently only SoC codec that does so
1721 * is the generic AC97 glue but others migh emerge.
1723 * In those cases we don't try to register the device again.
1725 if (!rtd->codec->ac97_created)
1726 return 0;
1728 ret = soc_ac97_dev_register(rtd->codec);
1729 if (ret < 0) {
1730 printk(KERN_ERR "asoc: AC97 device register failed\n");
1731 return ret;
1734 rtd->codec->ac97_registered = 1;
1736 return 0;
1739 static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1741 if (codec->ac97_registered) {
1742 soc_ac97_dev_unregister(codec);
1743 codec->ac97_registered = 0;
1746 #endif
1748 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1750 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1751 struct snd_soc_codec *codec;
1752 int ret = -ENODEV;
1754 /* find CODEC from registered CODECs*/
1755 list_for_each_entry(codec, &codec_list, list) {
1756 if (!strcmp(codec->name, aux_dev->codec_name)) {
1757 if (codec->probed) {
1758 dev_err(codec->dev,
1759 "asoc: codec already probed");
1760 ret = -EBUSY;
1761 goto out;
1763 goto found;
1766 /* codec not found */
1767 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1768 goto out;
1770 found:
1771 ret = soc_probe_codec(card, codec);
1772 if (ret < 0)
1773 return ret;
1775 ret = soc_post_component_init(card, codec, num, 1);
1777 out:
1778 return ret;
1781 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1783 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1784 struct snd_soc_codec *codec = rtd->codec;
1786 /* unregister the rtd device */
1787 if (rtd->dev_registered) {
1788 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1789 device_unregister(&rtd->dev);
1790 rtd->dev_registered = 0;
1793 if (codec && codec->probed)
1794 soc_remove_codec(codec);
1797 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1798 enum snd_soc_compress_type compress_type)
1800 int ret;
1802 if (codec->cache_init)
1803 return 0;
1805 /* override the compress_type if necessary */
1806 if (compress_type && codec->compress_type != compress_type)
1807 codec->compress_type = compress_type;
1808 ret = snd_soc_cache_init(codec);
1809 if (ret < 0) {
1810 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1811 ret);
1812 return ret;
1814 codec->cache_init = 1;
1815 return 0;
1818 static void snd_soc_instantiate_card(struct snd_soc_card *card)
1820 struct snd_soc_codec *codec;
1821 struct snd_soc_codec_conf *codec_conf;
1822 enum snd_soc_compress_type compress_type;
1823 int ret, i;
1825 mutex_lock(&card->mutex);
1827 if (card->instantiated) {
1828 mutex_unlock(&card->mutex);
1829 return;
1832 /* bind DAIs */
1833 for (i = 0; i < card->num_links; i++)
1834 soc_bind_dai_link(card, i);
1836 /* bind completed ? */
1837 if (card->num_rtd != card->num_links) {
1838 mutex_unlock(&card->mutex);
1839 return;
1842 /* initialize the register cache for each available codec */
1843 list_for_each_entry(codec, &codec_list, list) {
1844 if (codec->cache_init)
1845 continue;
1846 /* by default we don't override the compress_type */
1847 compress_type = 0;
1848 /* check to see if we need to override the compress_type */
1849 for (i = 0; i < card->num_configs; ++i) {
1850 codec_conf = &card->codec_conf[i];
1851 if (!strcmp(codec->name, codec_conf->dev_name)) {
1852 compress_type = codec_conf->compress_type;
1853 if (compress_type && compress_type
1854 != codec->compress_type)
1855 break;
1858 ret = snd_soc_init_codec_cache(codec, compress_type);
1859 if (ret < 0) {
1860 mutex_unlock(&card->mutex);
1861 return;
1865 /* card bind complete so register a sound card */
1866 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1867 card->owner, 0, &card->snd_card);
1868 if (ret < 0) {
1869 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1870 card->name);
1871 mutex_unlock(&card->mutex);
1872 return;
1874 card->snd_card->dev = card->dev;
1876 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1877 card->dapm.dev = card->dev;
1878 card->dapm.card = card;
1879 list_add(&card->dapm.list, &card->dapm_list);
1881 #ifdef CONFIG_DEBUG_FS
1882 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1883 #endif
1885 #ifdef CONFIG_PM_SLEEP
1886 /* deferred resume work */
1887 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1888 #endif
1890 if (card->dapm_widgets)
1891 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1892 card->num_dapm_widgets);
1894 /* initialise the sound card only once */
1895 if (card->probe) {
1896 ret = card->probe(card);
1897 if (ret < 0)
1898 goto card_probe_error;
1901 for (i = 0; i < card->num_links; i++) {
1902 ret = soc_probe_dai_link(card, i);
1903 if (ret < 0) {
1904 pr_err("asoc: failed to instantiate card %s: %d\n",
1905 card->name, ret);
1906 goto probe_dai_err;
1910 for (i = 0; i < card->num_aux_devs; i++) {
1911 ret = soc_probe_aux_dev(card, i);
1912 if (ret < 0) {
1913 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1914 card->name, ret);
1915 goto probe_aux_dev_err;
1919 /* We should have a non-codec control add function but we don't */
1920 if (card->controls)
1921 snd_soc_add_controls(list_first_entry(&card->codec_dev_list,
1922 struct snd_soc_codec,
1923 card_list),
1924 card->controls,
1925 card->num_controls);
1927 if (card->dapm_routes)
1928 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1929 card->num_dapm_routes);
1931 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1932 "%s", card->name);
1933 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1934 "%s", card->long_name ? card->long_name : card->name);
1935 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1936 "%s", card->driver_name ? card->driver_name : card->name);
1937 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1938 switch (card->snd_card->driver[i]) {
1939 case '_':
1940 case '-':
1941 case '\0':
1942 break;
1943 default:
1944 if (!isalnum(card->snd_card->driver[i]))
1945 card->snd_card->driver[i] = '_';
1946 break;
1950 if (card->late_probe) {
1951 ret = card->late_probe(card);
1952 if (ret < 0) {
1953 dev_err(card->dev, "%s late_probe() failed: %d\n",
1954 card->name, ret);
1955 goto probe_aux_dev_err;
1959 ret = snd_card_register(card->snd_card);
1960 if (ret < 0) {
1961 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1962 goto probe_aux_dev_err;
1965 #ifdef CONFIG_SND_SOC_AC97_BUS
1966 /* register any AC97 codecs */
1967 for (i = 0; i < card->num_rtd; i++) {
1968 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1969 if (ret < 0) {
1970 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1971 while (--i >= 0)
1972 soc_unregister_ac97_dai_link(card->rtd[i].codec);
1973 goto probe_aux_dev_err;
1976 #endif
1978 card->instantiated = 1;
1979 mutex_unlock(&card->mutex);
1980 return;
1982 probe_aux_dev_err:
1983 for (i = 0; i < card->num_aux_devs; i++)
1984 soc_remove_aux_dev(card, i);
1986 probe_dai_err:
1987 soc_remove_dai_links(card);
1989 card_probe_error:
1990 if (card->remove)
1991 card->remove(card);
1993 snd_card_free(card->snd_card);
1995 mutex_unlock(&card->mutex);
1999 * Attempt to initialise any uninitialised cards. Must be called with
2000 * client_mutex.
2002 static void snd_soc_instantiate_cards(void)
2004 struct snd_soc_card *card;
2005 list_for_each_entry(card, &card_list, list)
2006 snd_soc_instantiate_card(card);
2009 /* probes a new socdev */
2010 static int soc_probe(struct platform_device *pdev)
2012 struct snd_soc_card *card = platform_get_drvdata(pdev);
2013 int ret = 0;
2016 * no card, so machine driver should be registering card
2017 * we should not be here in that case so ret error
2019 if (!card)
2020 return -EINVAL;
2022 /* Bodge while we unpick instantiation */
2023 card->dev = &pdev->dev;
2025 ret = snd_soc_register_card(card);
2026 if (ret != 0) {
2027 dev_err(&pdev->dev, "Failed to register card\n");
2028 return ret;
2031 return 0;
2034 static int soc_cleanup_card_resources(struct snd_soc_card *card)
2036 int i;
2038 /* make sure any delayed work runs */
2039 for (i = 0; i < card->num_rtd; i++) {
2040 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2041 flush_delayed_work_sync(&rtd->delayed_work);
2044 /* remove auxiliary devices */
2045 for (i = 0; i < card->num_aux_devs; i++)
2046 soc_remove_aux_dev(card, i);
2048 /* remove and free each DAI */
2049 soc_remove_dai_links(card);
2051 soc_cleanup_card_debugfs(card);
2053 /* remove the card */
2054 if (card->remove)
2055 card->remove(card);
2057 snd_soc_dapm_free(&card->dapm);
2059 kfree(card->rtd);
2060 snd_card_free(card->snd_card);
2061 return 0;
2065 /* removes a socdev */
2066 static int soc_remove(struct platform_device *pdev)
2068 struct snd_soc_card *card = platform_get_drvdata(pdev);
2070 snd_soc_unregister_card(card);
2071 return 0;
2074 int snd_soc_poweroff(struct device *dev)
2076 struct snd_soc_card *card = dev_get_drvdata(dev);
2077 int i;
2079 if (!card->instantiated)
2080 return 0;
2082 /* Flush out pmdown_time work - we actually do want to run it
2083 * now, we're shutting down so no imminent restart. */
2084 for (i = 0; i < card->num_rtd; i++) {
2085 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2086 flush_delayed_work_sync(&rtd->delayed_work);
2089 snd_soc_dapm_shutdown(card);
2091 return 0;
2093 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2095 const struct dev_pm_ops snd_soc_pm_ops = {
2096 .suspend = snd_soc_suspend,
2097 .resume = snd_soc_resume,
2098 .poweroff = snd_soc_poweroff,
2100 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2102 /* ASoC platform driver */
2103 static struct platform_driver soc_driver = {
2104 .driver = {
2105 .name = "soc-audio",
2106 .owner = THIS_MODULE,
2107 .pm = &snd_soc_pm_ops,
2109 .probe = soc_probe,
2110 .remove = soc_remove,
2113 /* create a new pcm */
2114 static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2116 struct snd_soc_codec *codec = rtd->codec;
2117 struct snd_soc_platform *platform = rtd->platform;
2118 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2119 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2120 struct snd_pcm *pcm;
2121 char new_name[64];
2122 int ret = 0, playback = 0, capture = 0;
2124 /* check client and interface hw capabilities */
2125 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2126 rtd->dai_link->stream_name, codec_dai->name, num);
2128 if (codec_dai->driver->playback.channels_min)
2129 playback = 1;
2130 if (codec_dai->driver->capture.channels_min)
2131 capture = 1;
2133 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
2134 ret = snd_pcm_new(rtd->card->snd_card, new_name,
2135 num, playback, capture, &pcm);
2136 if (ret < 0) {
2137 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
2138 return ret;
2141 rtd->pcm = pcm;
2142 pcm->private_data = rtd;
2143 if (platform->driver->ops) {
2144 soc_pcm_ops.mmap = platform->driver->ops->mmap;
2145 soc_pcm_ops.pointer = platform->driver->ops->pointer;
2146 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
2147 soc_pcm_ops.copy = platform->driver->ops->copy;
2148 soc_pcm_ops.silence = platform->driver->ops->silence;
2149 soc_pcm_ops.ack = platform->driver->ops->ack;
2150 soc_pcm_ops.page = platform->driver->ops->page;
2153 if (playback)
2154 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
2156 if (capture)
2157 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
2159 if (platform->driver->pcm_new) {
2160 ret = platform->driver->pcm_new(rtd->card->snd_card,
2161 codec_dai, pcm);
2162 if (ret < 0) {
2163 pr_err("asoc: platform pcm constructor failed\n");
2164 return ret;
2168 pcm->private_free = platform->driver->pcm_free;
2169 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
2170 cpu_dai->name);
2171 return ret;
2175 * snd_soc_codec_volatile_register: Report if a register is volatile.
2177 * @codec: CODEC to query.
2178 * @reg: Register to query.
2180 * Boolean function indiciating if a CODEC register is volatile.
2182 int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
2183 unsigned int reg)
2185 if (codec->volatile_register)
2186 return codec->volatile_register(codec, reg);
2187 else
2188 return 0;
2190 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
2193 * snd_soc_codec_readable_register: Report if a register is readable.
2195 * @codec: CODEC to query.
2196 * @reg: Register to query.
2198 * Boolean function indicating if a CODEC register is readable.
2200 int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
2201 unsigned int reg)
2203 if (codec->readable_register)
2204 return codec->readable_register(codec, reg);
2205 else
2206 return 0;
2208 EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
2211 * snd_soc_codec_writable_register: Report if a register is writable.
2213 * @codec: CODEC to query.
2214 * @reg: Register to query.
2216 * Boolean function indicating if a CODEC register is writable.
2218 int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
2219 unsigned int reg)
2221 if (codec->writable_register)
2222 return codec->writable_register(codec, reg);
2223 else
2224 return 0;
2226 EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
2229 * snd_soc_new_ac97_codec - initailise AC97 device
2230 * @codec: audio codec
2231 * @ops: AC97 bus operations
2232 * @num: AC97 codec number
2234 * Initialises AC97 codec resources for use by ad-hoc devices only.
2236 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2237 struct snd_ac97_bus_ops *ops, int num)
2239 mutex_lock(&codec->mutex);
2241 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2242 if (codec->ac97 == NULL) {
2243 mutex_unlock(&codec->mutex);
2244 return -ENOMEM;
2247 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2248 if (codec->ac97->bus == NULL) {
2249 kfree(codec->ac97);
2250 codec->ac97 = NULL;
2251 mutex_unlock(&codec->mutex);
2252 return -ENOMEM;
2255 codec->ac97->bus->ops = ops;
2256 codec->ac97->num = num;
2259 * Mark the AC97 device to be created by us. This way we ensure that the
2260 * device will be registered with the device subsystem later on.
2262 codec->ac97_created = 1;
2264 mutex_unlock(&codec->mutex);
2265 return 0;
2267 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2270 * snd_soc_free_ac97_codec - free AC97 codec device
2271 * @codec: audio codec
2273 * Frees AC97 codec device resources.
2275 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2277 mutex_lock(&codec->mutex);
2278 #ifdef CONFIG_SND_SOC_AC97_BUS
2279 soc_unregister_ac97_dai_link(codec);
2280 #endif
2281 kfree(codec->ac97->bus);
2282 kfree(codec->ac97);
2283 codec->ac97 = NULL;
2284 codec->ac97_created = 0;
2285 mutex_unlock(&codec->mutex);
2287 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2289 unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2291 unsigned int ret;
2293 ret = codec->read(codec, reg);
2294 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
2295 trace_snd_soc_reg_read(codec, reg, ret);
2297 return ret;
2299 EXPORT_SYMBOL_GPL(snd_soc_read);
2301 unsigned int snd_soc_write(struct snd_soc_codec *codec,
2302 unsigned int reg, unsigned int val)
2304 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
2305 trace_snd_soc_reg_write(codec, reg, val);
2306 return codec->write(codec, reg, val);
2308 EXPORT_SYMBOL_GPL(snd_soc_write);
2310 unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
2311 unsigned int reg, const void *data, size_t len)
2313 return codec->bulk_write_raw(codec, reg, data, len);
2315 EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
2318 * snd_soc_update_bits - update codec register bits
2319 * @codec: audio codec
2320 * @reg: codec register
2321 * @mask: register mask
2322 * @value: new value
2324 * Writes new register value.
2326 * Returns 1 for change, 0 for no change, or negative error code.
2328 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
2329 unsigned int mask, unsigned int value)
2331 int change;
2332 unsigned int old, new;
2333 int ret;
2335 ret = snd_soc_read(codec, reg);
2336 if (ret < 0)
2337 return ret;
2339 old = ret;
2340 new = (old & ~mask) | value;
2341 change = old != new;
2342 if (change) {
2343 ret = snd_soc_write(codec, reg, new);
2344 if (ret < 0)
2345 return ret;
2348 return change;
2350 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2353 * snd_soc_update_bits_locked - update codec register bits
2354 * @codec: audio codec
2355 * @reg: codec register
2356 * @mask: register mask
2357 * @value: new value
2359 * Writes new register value, and takes the codec mutex.
2361 * Returns 1 for change else 0.
2363 int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2364 unsigned short reg, unsigned int mask,
2365 unsigned int value)
2367 int change;
2369 mutex_lock(&codec->mutex);
2370 change = snd_soc_update_bits(codec, reg, mask, value);
2371 mutex_unlock(&codec->mutex);
2373 return change;
2375 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
2378 * snd_soc_test_bits - test register for change
2379 * @codec: audio codec
2380 * @reg: codec register
2381 * @mask: register mask
2382 * @value: new value
2384 * Tests a register with a new value and checks if the new value is
2385 * different from the old value.
2387 * Returns 1 for change else 0.
2389 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
2390 unsigned int mask, unsigned int value)
2392 int change;
2393 unsigned int old, new;
2395 old = snd_soc_read(codec, reg);
2396 new = (old & ~mask) | value;
2397 change = old != new;
2399 return change;
2401 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2404 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2405 * @substream: the pcm substream
2406 * @hw: the hardware parameters
2408 * Sets the substream runtime hardware parameters.
2410 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2411 const struct snd_pcm_hardware *hw)
2413 struct snd_pcm_runtime *runtime = substream->runtime;
2414 runtime->hw.info = hw->info;
2415 runtime->hw.formats = hw->formats;
2416 runtime->hw.period_bytes_min = hw->period_bytes_min;
2417 runtime->hw.period_bytes_max = hw->period_bytes_max;
2418 runtime->hw.periods_min = hw->periods_min;
2419 runtime->hw.periods_max = hw->periods_max;
2420 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2421 runtime->hw.fifo_size = hw->fifo_size;
2422 return 0;
2424 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2427 * snd_soc_cnew - create new control
2428 * @_template: control template
2429 * @data: control private data
2430 * @long_name: control long name
2431 * @prefix: control name prefix
2433 * Create a new mixer control from a template control.
2435 * Returns 0 for success, else error.
2437 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2438 void *data, char *long_name,
2439 const char *prefix)
2441 struct snd_kcontrol_new template;
2442 struct snd_kcontrol *kcontrol;
2443 char *name = NULL;
2444 int name_len;
2446 memcpy(&template, _template, sizeof(template));
2447 template.index = 0;
2449 if (!long_name)
2450 long_name = template.name;
2452 if (prefix) {
2453 name_len = strlen(long_name) + strlen(prefix) + 2;
2454 name = kmalloc(name_len, GFP_ATOMIC);
2455 if (!name)
2456 return NULL;
2458 snprintf(name, name_len, "%s %s", prefix, long_name);
2460 template.name = name;
2461 } else {
2462 template.name = long_name;
2465 kcontrol = snd_ctl_new1(&template, data);
2467 kfree(name);
2469 return kcontrol;
2471 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2474 * snd_soc_add_controls - add an array of controls to a codec.
2475 * Convienience function to add a list of controls. Many codecs were
2476 * duplicating this code.
2478 * @codec: codec to add controls to
2479 * @controls: array of controls to add
2480 * @num_controls: number of elements in the array
2482 * Return 0 for success, else error.
2484 int snd_soc_add_controls(struct snd_soc_codec *codec,
2485 const struct snd_kcontrol_new *controls, int num_controls)
2487 struct snd_card *card = codec->card->snd_card;
2488 int err, i;
2490 for (i = 0; i < num_controls; i++) {
2491 const struct snd_kcontrol_new *control = &controls[i];
2492 err = snd_ctl_add(card, snd_soc_cnew(control, codec,
2493 control->name,
2494 codec->name_prefix));
2495 if (err < 0) {
2496 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
2497 codec->name, control->name, err);
2498 return err;
2502 return 0;
2504 EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2507 * snd_soc_info_enum_double - enumerated double mixer info callback
2508 * @kcontrol: mixer control
2509 * @uinfo: control element information
2511 * Callback to provide information about a double enumerated
2512 * mixer control.
2514 * Returns 0 for success.
2516 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2517 struct snd_ctl_elem_info *uinfo)
2519 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2521 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2522 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2523 uinfo->value.enumerated.items = e->max;
2525 if (uinfo->value.enumerated.item > e->max - 1)
2526 uinfo->value.enumerated.item = e->max - 1;
2527 strcpy(uinfo->value.enumerated.name,
2528 e->texts[uinfo->value.enumerated.item]);
2529 return 0;
2531 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2534 * snd_soc_get_enum_double - enumerated double mixer get callback
2535 * @kcontrol: mixer control
2536 * @ucontrol: control element information
2538 * Callback to get the value of a double enumerated mixer.
2540 * Returns 0 for success.
2542 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2543 struct snd_ctl_elem_value *ucontrol)
2545 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2546 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2547 unsigned int val, bitmask;
2549 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2551 val = snd_soc_read(codec, e->reg);
2552 ucontrol->value.enumerated.item[0]
2553 = (val >> e->shift_l) & (bitmask - 1);
2554 if (e->shift_l != e->shift_r)
2555 ucontrol->value.enumerated.item[1] =
2556 (val >> e->shift_r) & (bitmask - 1);
2558 return 0;
2560 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2563 * snd_soc_put_enum_double - enumerated double mixer put callback
2564 * @kcontrol: mixer control
2565 * @ucontrol: control element information
2567 * Callback to set the value of a double enumerated mixer.
2569 * Returns 0 for success.
2571 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2572 struct snd_ctl_elem_value *ucontrol)
2574 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2575 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2576 unsigned int val;
2577 unsigned int mask, bitmask;
2579 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2581 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2582 return -EINVAL;
2583 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2584 mask = (bitmask - 1) << e->shift_l;
2585 if (e->shift_l != e->shift_r) {
2586 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2587 return -EINVAL;
2588 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2589 mask |= (bitmask - 1) << e->shift_r;
2592 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2594 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2597 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2598 * @kcontrol: mixer control
2599 * @ucontrol: control element information
2601 * Callback to get the value of a double semi enumerated mixer.
2603 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2604 * used for handling bitfield coded enumeration for example.
2606 * Returns 0 for success.
2608 int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2609 struct snd_ctl_elem_value *ucontrol)
2611 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2612 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2613 unsigned int reg_val, val, mux;
2615 reg_val = snd_soc_read(codec, e->reg);
2616 val = (reg_val >> e->shift_l) & e->mask;
2617 for (mux = 0; mux < e->max; mux++) {
2618 if (val == e->values[mux])
2619 break;
2621 ucontrol->value.enumerated.item[0] = mux;
2622 if (e->shift_l != e->shift_r) {
2623 val = (reg_val >> e->shift_r) & e->mask;
2624 for (mux = 0; mux < e->max; mux++) {
2625 if (val == e->values[mux])
2626 break;
2628 ucontrol->value.enumerated.item[1] = mux;
2631 return 0;
2633 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2636 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2637 * @kcontrol: mixer control
2638 * @ucontrol: control element information
2640 * Callback to set the value of a double semi enumerated mixer.
2642 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2643 * used for handling bitfield coded enumeration for example.
2645 * Returns 0 for success.
2647 int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2648 struct snd_ctl_elem_value *ucontrol)
2650 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2651 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2652 unsigned int val;
2653 unsigned int mask;
2655 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2656 return -EINVAL;
2657 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2658 mask = e->mask << e->shift_l;
2659 if (e->shift_l != e->shift_r) {
2660 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2661 return -EINVAL;
2662 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2663 mask |= e->mask << e->shift_r;
2666 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2668 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2671 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2672 * @kcontrol: mixer control
2673 * @uinfo: control element information
2675 * Callback to provide information about an external enumerated
2676 * single mixer.
2678 * Returns 0 for success.
2680 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2681 struct snd_ctl_elem_info *uinfo)
2683 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2685 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2686 uinfo->count = 1;
2687 uinfo->value.enumerated.items = e->max;
2689 if (uinfo->value.enumerated.item > e->max - 1)
2690 uinfo->value.enumerated.item = e->max - 1;
2691 strcpy(uinfo->value.enumerated.name,
2692 e->texts[uinfo->value.enumerated.item]);
2693 return 0;
2695 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2698 * snd_soc_info_volsw_ext - external single mixer info callback
2699 * @kcontrol: mixer control
2700 * @uinfo: control element information
2702 * Callback to provide information about a single external mixer control.
2704 * Returns 0 for success.
2706 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2707 struct snd_ctl_elem_info *uinfo)
2709 int max = kcontrol->private_value;
2711 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2712 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2713 else
2714 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2716 uinfo->count = 1;
2717 uinfo->value.integer.min = 0;
2718 uinfo->value.integer.max = max;
2719 return 0;
2721 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2724 * snd_soc_info_volsw - single mixer info callback
2725 * @kcontrol: mixer control
2726 * @uinfo: control element information
2728 * Callback to provide information about a single mixer control.
2730 * Returns 0 for success.
2732 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2733 struct snd_ctl_elem_info *uinfo)
2735 struct soc_mixer_control *mc =
2736 (struct soc_mixer_control *)kcontrol->private_value;
2737 int platform_max;
2738 unsigned int shift = mc->shift;
2739 unsigned int rshift = mc->rshift;
2741 if (!mc->platform_max)
2742 mc->platform_max = mc->max;
2743 platform_max = mc->platform_max;
2745 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2746 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2747 else
2748 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2750 uinfo->count = shift == rshift ? 1 : 2;
2751 uinfo->value.integer.min = 0;
2752 uinfo->value.integer.max = platform_max;
2753 return 0;
2755 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2758 * snd_soc_get_volsw - single mixer get callback
2759 * @kcontrol: mixer control
2760 * @ucontrol: control element information
2762 * Callback to get the value of a single mixer control.
2764 * Returns 0 for success.
2766 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2767 struct snd_ctl_elem_value *ucontrol)
2769 struct soc_mixer_control *mc =
2770 (struct soc_mixer_control *)kcontrol->private_value;
2771 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2772 unsigned int reg = mc->reg;
2773 unsigned int shift = mc->shift;
2774 unsigned int rshift = mc->rshift;
2775 int max = mc->max;
2776 unsigned int mask = (1 << fls(max)) - 1;
2777 unsigned int invert = mc->invert;
2779 ucontrol->value.integer.value[0] =
2780 (snd_soc_read(codec, reg) >> shift) & mask;
2781 if (shift != rshift)
2782 ucontrol->value.integer.value[1] =
2783 (snd_soc_read(codec, reg) >> rshift) & mask;
2784 if (invert) {
2785 ucontrol->value.integer.value[0] =
2786 max - ucontrol->value.integer.value[0];
2787 if (shift != rshift)
2788 ucontrol->value.integer.value[1] =
2789 max - ucontrol->value.integer.value[1];
2792 return 0;
2794 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2797 * snd_soc_put_volsw - single mixer put callback
2798 * @kcontrol: mixer control
2799 * @ucontrol: control element information
2801 * Callback to set the value of a single mixer control.
2803 * Returns 0 for success.
2805 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2806 struct snd_ctl_elem_value *ucontrol)
2808 struct soc_mixer_control *mc =
2809 (struct soc_mixer_control *)kcontrol->private_value;
2810 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2811 unsigned int reg = mc->reg;
2812 unsigned int shift = mc->shift;
2813 unsigned int rshift = mc->rshift;
2814 int max = mc->max;
2815 unsigned int mask = (1 << fls(max)) - 1;
2816 unsigned int invert = mc->invert;
2817 unsigned int val, val2, val_mask;
2819 val = (ucontrol->value.integer.value[0] & mask);
2820 if (invert)
2821 val = max - val;
2822 val_mask = mask << shift;
2823 val = val << shift;
2824 if (shift != rshift) {
2825 val2 = (ucontrol->value.integer.value[1] & mask);
2826 if (invert)
2827 val2 = max - val2;
2828 val_mask |= mask << rshift;
2829 val |= val2 << rshift;
2831 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2833 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2836 * snd_soc_info_volsw_2r - double mixer info callback
2837 * @kcontrol: mixer control
2838 * @uinfo: control element information
2840 * Callback to provide information about a double mixer control that
2841 * spans 2 codec registers.
2843 * Returns 0 for success.
2845 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2846 struct snd_ctl_elem_info *uinfo)
2848 struct soc_mixer_control *mc =
2849 (struct soc_mixer_control *)kcontrol->private_value;
2850 int platform_max;
2852 if (!mc->platform_max)
2853 mc->platform_max = mc->max;
2854 platform_max = mc->platform_max;
2856 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2857 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2858 else
2859 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2861 uinfo->count = 2;
2862 uinfo->value.integer.min = 0;
2863 uinfo->value.integer.max = platform_max;
2864 return 0;
2866 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2869 * snd_soc_get_volsw_2r - double mixer get callback
2870 * @kcontrol: mixer control
2871 * @ucontrol: control element information
2873 * Callback to get the value of a double mixer control that spans 2 registers.
2875 * Returns 0 for success.
2877 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2878 struct snd_ctl_elem_value *ucontrol)
2880 struct soc_mixer_control *mc =
2881 (struct soc_mixer_control *)kcontrol->private_value;
2882 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2883 unsigned int reg = mc->reg;
2884 unsigned int reg2 = mc->rreg;
2885 unsigned int shift = mc->shift;
2886 int max = mc->max;
2887 unsigned int mask = (1 << fls(max)) - 1;
2888 unsigned int invert = mc->invert;
2890 ucontrol->value.integer.value[0] =
2891 (snd_soc_read(codec, reg) >> shift) & mask;
2892 ucontrol->value.integer.value[1] =
2893 (snd_soc_read(codec, reg2) >> shift) & mask;
2894 if (invert) {
2895 ucontrol->value.integer.value[0] =
2896 max - ucontrol->value.integer.value[0];
2897 ucontrol->value.integer.value[1] =
2898 max - ucontrol->value.integer.value[1];
2901 return 0;
2903 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2906 * snd_soc_put_volsw_2r - double mixer set callback
2907 * @kcontrol: mixer control
2908 * @ucontrol: control element information
2910 * Callback to set the value of a double mixer control that spans 2 registers.
2912 * Returns 0 for success.
2914 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2915 struct snd_ctl_elem_value *ucontrol)
2917 struct soc_mixer_control *mc =
2918 (struct soc_mixer_control *)kcontrol->private_value;
2919 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2920 unsigned int reg = mc->reg;
2921 unsigned int reg2 = mc->rreg;
2922 unsigned int shift = mc->shift;
2923 int max = mc->max;
2924 unsigned int mask = (1 << fls(max)) - 1;
2925 unsigned int invert = mc->invert;
2926 int err;
2927 unsigned int val, val2, val_mask;
2929 val_mask = mask << shift;
2930 val = (ucontrol->value.integer.value[0] & mask);
2931 val2 = (ucontrol->value.integer.value[1] & mask);
2933 if (invert) {
2934 val = max - val;
2935 val2 = max - val2;
2938 val = val << shift;
2939 val2 = val2 << shift;
2941 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2942 if (err < 0)
2943 return err;
2945 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2946 return err;
2948 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2951 * snd_soc_info_volsw_s8 - signed mixer info callback
2952 * @kcontrol: mixer control
2953 * @uinfo: control element information
2955 * Callback to provide information about a signed mixer control.
2957 * Returns 0 for success.
2959 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2960 struct snd_ctl_elem_info *uinfo)
2962 struct soc_mixer_control *mc =
2963 (struct soc_mixer_control *)kcontrol->private_value;
2964 int platform_max;
2965 int min = mc->min;
2967 if (!mc->platform_max)
2968 mc->platform_max = mc->max;
2969 platform_max = mc->platform_max;
2971 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2972 uinfo->count = 2;
2973 uinfo->value.integer.min = 0;
2974 uinfo->value.integer.max = platform_max - min;
2975 return 0;
2977 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2980 * snd_soc_get_volsw_s8 - signed mixer get callback
2981 * @kcontrol: mixer control
2982 * @ucontrol: control element information
2984 * Callback to get the value of a signed mixer control.
2986 * Returns 0 for success.
2988 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2989 struct snd_ctl_elem_value *ucontrol)
2991 struct soc_mixer_control *mc =
2992 (struct soc_mixer_control *)kcontrol->private_value;
2993 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2994 unsigned int reg = mc->reg;
2995 int min = mc->min;
2996 int val = snd_soc_read(codec, reg);
2998 ucontrol->value.integer.value[0] =
2999 ((signed char)(val & 0xff))-min;
3000 ucontrol->value.integer.value[1] =
3001 ((signed char)((val >> 8) & 0xff))-min;
3002 return 0;
3004 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
3007 * snd_soc_put_volsw_sgn - signed mixer put callback
3008 * @kcontrol: mixer control
3009 * @ucontrol: control element information
3011 * Callback to set the value of a signed mixer control.
3013 * Returns 0 for success.
3015 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
3016 struct snd_ctl_elem_value *ucontrol)
3018 struct soc_mixer_control *mc =
3019 (struct soc_mixer_control *)kcontrol->private_value;
3020 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3021 unsigned int reg = mc->reg;
3022 int min = mc->min;
3023 unsigned int val;
3025 val = (ucontrol->value.integer.value[0]+min) & 0xff;
3026 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
3028 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
3030 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
3033 * snd_soc_limit_volume - Set new limit to an existing volume control.
3035 * @codec: where to look for the control
3036 * @name: Name of the control
3037 * @max: new maximum limit
3039 * Return 0 for success, else error.
3041 int snd_soc_limit_volume(struct snd_soc_codec *codec,
3042 const char *name, int max)
3044 struct snd_card *card = codec->card->snd_card;
3045 struct snd_kcontrol *kctl;
3046 struct soc_mixer_control *mc;
3047 int found = 0;
3048 int ret = -EINVAL;
3050 /* Sanity check for name and max */
3051 if (unlikely(!name || max <= 0))
3052 return -EINVAL;
3054 list_for_each_entry(kctl, &card->controls, list) {
3055 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3056 found = 1;
3057 break;
3060 if (found) {
3061 mc = (struct soc_mixer_control *)kctl->private_value;
3062 if (max <= mc->max) {
3063 mc->platform_max = max;
3064 ret = 0;
3067 return ret;
3069 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3072 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
3073 * mixer info callback
3074 * @kcontrol: mixer control
3075 * @uinfo: control element information
3077 * Returns 0 for success.
3079 int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
3080 struct snd_ctl_elem_info *uinfo)
3082 struct soc_mixer_control *mc =
3083 (struct soc_mixer_control *)kcontrol->private_value;
3084 int max = mc->max;
3085 int min = mc->min;
3087 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3088 uinfo->count = 2;
3089 uinfo->value.integer.min = 0;
3090 uinfo->value.integer.max = max-min;
3092 return 0;
3094 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
3097 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
3098 * mixer get callback
3099 * @kcontrol: mixer control
3100 * @uinfo: control element information
3102 * Returns 0 for success.
3104 int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
3105 struct snd_ctl_elem_value *ucontrol)
3107 struct soc_mixer_control *mc =
3108 (struct soc_mixer_control *)kcontrol->private_value;
3109 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3110 unsigned int mask = (1<<mc->shift)-1;
3111 int min = mc->min;
3112 int val = snd_soc_read(codec, mc->reg) & mask;
3113 int valr = snd_soc_read(codec, mc->rreg) & mask;
3115 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
3116 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
3117 return 0;
3119 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
3122 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
3123 * mixer put callback
3124 * @kcontrol: mixer control
3125 * @uinfo: control element information
3127 * Returns 0 for success.
3129 int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
3130 struct snd_ctl_elem_value *ucontrol)
3132 struct soc_mixer_control *mc =
3133 (struct soc_mixer_control *)kcontrol->private_value;
3134 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3135 unsigned int mask = (1<<mc->shift)-1;
3136 int min = mc->min;
3137 int ret;
3138 unsigned int val, valr, oval, ovalr;
3140 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
3141 val &= mask;
3142 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
3143 valr &= mask;
3145 oval = snd_soc_read(codec, mc->reg) & mask;
3146 ovalr = snd_soc_read(codec, mc->rreg) & mask;
3148 ret = 0;
3149 if (oval != val) {
3150 ret = snd_soc_write(codec, mc->reg, val);
3151 if (ret < 0)
3152 return ret;
3154 if (ovalr != valr) {
3155 ret = snd_soc_write(codec, mc->rreg, valr);
3156 if (ret < 0)
3157 return ret;
3160 return 0;
3162 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
3165 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3166 * @dai: DAI
3167 * @clk_id: DAI specific clock ID
3168 * @freq: new clock frequency in Hz
3169 * @dir: new clock direction - input/output.
3171 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3173 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3174 unsigned int freq, int dir)
3176 if (dai->driver && dai->driver->ops->set_sysclk)
3177 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3178 else if (dai->codec && dai->codec->driver->set_sysclk)
3179 return dai->codec->driver->set_sysclk(dai->codec, clk_id,
3180 freq, dir);
3181 else
3182 return -EINVAL;
3184 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3187 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3188 * @codec: CODEC
3189 * @clk_id: DAI specific clock ID
3190 * @freq: new clock frequency in Hz
3191 * @dir: new clock direction - input/output.
3193 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3195 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3196 unsigned int freq, int dir)
3198 if (codec->driver->set_sysclk)
3199 return codec->driver->set_sysclk(codec, clk_id, freq, dir);
3200 else
3201 return -EINVAL;
3203 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3206 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3207 * @dai: DAI
3208 * @div_id: DAI specific clock divider ID
3209 * @div: new clock divisor.
3211 * Configures the clock dividers. This is used to derive the best DAI bit and
3212 * frame clocks from the system or master clock. It's best to set the DAI bit
3213 * and frame clocks as low as possible to save system power.
3215 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3216 int div_id, int div)
3218 if (dai->driver && dai->driver->ops->set_clkdiv)
3219 return dai->driver->ops->set_clkdiv(dai, div_id, div);
3220 else
3221 return -EINVAL;
3223 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3226 * snd_soc_dai_set_pll - configure DAI PLL.
3227 * @dai: DAI
3228 * @pll_id: DAI specific PLL ID
3229 * @source: DAI specific source for the PLL
3230 * @freq_in: PLL input clock frequency in Hz
3231 * @freq_out: requested PLL output clock frequency in Hz
3233 * Configures and enables PLL to generate output clock based on input clock.
3235 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3236 unsigned int freq_in, unsigned int freq_out)
3238 if (dai->driver && dai->driver->ops->set_pll)
3239 return dai->driver->ops->set_pll(dai, pll_id, source,
3240 freq_in, freq_out);
3241 else if (dai->codec && dai->codec->driver->set_pll)
3242 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3243 freq_in, freq_out);
3244 else
3245 return -EINVAL;
3247 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3250 * snd_soc_codec_set_pll - configure codec PLL.
3251 * @codec: CODEC
3252 * @pll_id: DAI specific PLL ID
3253 * @source: DAI specific source for the PLL
3254 * @freq_in: PLL input clock frequency in Hz
3255 * @freq_out: requested PLL output clock frequency in Hz
3257 * Configures and enables PLL to generate output clock based on input clock.
3259 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3260 unsigned int freq_in, unsigned int freq_out)
3262 if (codec->driver->set_pll)
3263 return codec->driver->set_pll(codec, pll_id, source,
3264 freq_in, freq_out);
3265 else
3266 return -EINVAL;
3268 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3271 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3272 * @dai: DAI
3273 * @fmt: SND_SOC_DAIFMT_ format value.
3275 * Configures the DAI hardware format and clocking.
3277 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3279 if (dai->driver && dai->driver->ops->set_fmt)
3280 return dai->driver->ops->set_fmt(dai, fmt);
3281 else
3282 return -EINVAL;
3284 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3287 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3288 * @dai: DAI
3289 * @tx_mask: bitmask representing active TX slots.
3290 * @rx_mask: bitmask representing active RX slots.
3291 * @slots: Number of slots in use.
3292 * @slot_width: Width in bits for each slot.
3294 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3295 * specific.
3297 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3298 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
3300 if (dai->driver && dai->driver->ops->set_tdm_slot)
3301 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3302 slots, slot_width);
3303 else
3304 return -EINVAL;
3306 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3309 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3310 * @dai: DAI
3311 * @tx_num: how many TX channels
3312 * @tx_slot: pointer to an array which imply the TX slot number channel
3313 * 0~num-1 uses
3314 * @rx_num: how many RX channels
3315 * @rx_slot: pointer to an array which imply the RX slot number channel
3316 * 0~num-1 uses
3318 * configure the relationship between channel number and TDM slot number.
3320 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3321 unsigned int tx_num, unsigned int *tx_slot,
3322 unsigned int rx_num, unsigned int *rx_slot)
3324 if (dai->driver && dai->driver->ops->set_channel_map)
3325 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3326 rx_num, rx_slot);
3327 else
3328 return -EINVAL;
3330 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3333 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3334 * @dai: DAI
3335 * @tristate: tristate enable
3337 * Tristates the DAI so that others can use it.
3339 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3341 if (dai->driver && dai->driver->ops->set_tristate)
3342 return dai->driver->ops->set_tristate(dai, tristate);
3343 else
3344 return -EINVAL;
3346 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3349 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3350 * @dai: DAI
3351 * @mute: mute enable
3353 * Mutes the DAI DAC.
3355 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3357 if (dai->driver && dai->driver->ops->digital_mute)
3358 return dai->driver->ops->digital_mute(dai, mute);
3359 else
3360 return -EINVAL;
3362 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3365 * snd_soc_register_card - Register a card with the ASoC core
3367 * @card: Card to register
3370 int snd_soc_register_card(struct snd_soc_card *card)
3372 int i;
3374 if (!card->name || !card->dev)
3375 return -EINVAL;
3377 dev_set_drvdata(card->dev, card);
3379 snd_soc_initialize_card_lists(card);
3381 soc_init_card_debugfs(card);
3383 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
3384 (card->num_links + card->num_aux_devs),
3385 GFP_KERNEL);
3386 if (card->rtd == NULL)
3387 return -ENOMEM;
3388 card->rtd_aux = &card->rtd[card->num_links];
3390 for (i = 0; i < card->num_links; i++)
3391 card->rtd[i].dai_link = &card->dai_link[i];
3393 INIT_LIST_HEAD(&card->list);
3394 card->instantiated = 0;
3395 mutex_init(&card->mutex);
3397 mutex_lock(&client_mutex);
3398 list_add(&card->list, &card_list);
3399 snd_soc_instantiate_cards();
3400 mutex_unlock(&client_mutex);
3402 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
3404 return 0;
3406 EXPORT_SYMBOL_GPL(snd_soc_register_card);
3409 * snd_soc_unregister_card - Unregister a card with the ASoC core
3411 * @card: Card to unregister
3414 int snd_soc_unregister_card(struct snd_soc_card *card)
3416 if (card->instantiated)
3417 soc_cleanup_card_resources(card);
3418 mutex_lock(&client_mutex);
3419 list_del(&card->list);
3420 mutex_unlock(&client_mutex);
3421 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3423 return 0;
3425 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3428 * Simplify DAI link configuration by removing ".-1" from device names
3429 * and sanitizing names.
3431 static char *fmt_single_name(struct device *dev, int *id)
3433 char *found, name[NAME_SIZE];
3434 int id1, id2;
3436 if (dev_name(dev) == NULL)
3437 return NULL;
3439 strlcpy(name, dev_name(dev), NAME_SIZE);
3441 /* are we a "%s.%d" name (platform and SPI components) */
3442 found = strstr(name, dev->driver->name);
3443 if (found) {
3444 /* get ID */
3445 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3447 /* discard ID from name if ID == -1 */
3448 if (*id == -1)
3449 found[strlen(dev->driver->name)] = '\0';
3452 } else {
3453 /* I2C component devices are named "bus-addr" */
3454 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3455 char tmp[NAME_SIZE];
3457 /* create unique ID number from I2C addr and bus */
3458 *id = ((id1 & 0xffff) << 16) + id2;
3460 /* sanitize component name for DAI link creation */
3461 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3462 strlcpy(name, tmp, NAME_SIZE);
3463 } else
3464 *id = 0;
3467 return kstrdup(name, GFP_KERNEL);
3471 * Simplify DAI link naming for single devices with multiple DAIs by removing
3472 * any ".-1" and using the DAI name (instead of device name).
3474 static inline char *fmt_multiple_name(struct device *dev,
3475 struct snd_soc_dai_driver *dai_drv)
3477 if (dai_drv->name == NULL) {
3478 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
3479 dev_name(dev));
3480 return NULL;
3483 return kstrdup(dai_drv->name, GFP_KERNEL);
3487 * snd_soc_register_dai - Register a DAI with the ASoC core
3489 * @dai: DAI to register
3491 int snd_soc_register_dai(struct device *dev,
3492 struct snd_soc_dai_driver *dai_drv)
3494 struct snd_soc_dai *dai;
3496 dev_dbg(dev, "dai register %s\n", dev_name(dev));
3498 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3499 if (dai == NULL)
3500 return -ENOMEM;
3502 /* create DAI component name */
3503 dai->name = fmt_single_name(dev, &dai->id);
3504 if (dai->name == NULL) {
3505 kfree(dai);
3506 return -ENOMEM;
3509 dai->dev = dev;
3510 dai->driver = dai_drv;
3511 if (!dai->driver->ops)
3512 dai->driver->ops = &null_dai_ops;
3514 mutex_lock(&client_mutex);
3515 list_add(&dai->list, &dai_list);
3516 snd_soc_instantiate_cards();
3517 mutex_unlock(&client_mutex);
3519 pr_debug("Registered DAI '%s'\n", dai->name);
3521 return 0;
3523 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3526 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3528 * @dai: DAI to unregister
3530 void snd_soc_unregister_dai(struct device *dev)
3532 struct snd_soc_dai *dai;
3534 list_for_each_entry(dai, &dai_list, list) {
3535 if (dev == dai->dev)
3536 goto found;
3538 return;
3540 found:
3541 mutex_lock(&client_mutex);
3542 list_del(&dai->list);
3543 mutex_unlock(&client_mutex);
3545 pr_debug("Unregistered DAI '%s'\n", dai->name);
3546 kfree(dai->name);
3547 kfree(dai);
3549 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3552 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3554 * @dai: Array of DAIs to register
3555 * @count: Number of DAIs
3557 int snd_soc_register_dais(struct device *dev,
3558 struct snd_soc_dai_driver *dai_drv, size_t count)
3560 struct snd_soc_dai *dai;
3561 int i, ret = 0;
3563 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
3565 for (i = 0; i < count; i++) {
3567 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3568 if (dai == NULL) {
3569 ret = -ENOMEM;
3570 goto err;
3573 /* create DAI component name */
3574 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3575 if (dai->name == NULL) {
3576 kfree(dai);
3577 ret = -EINVAL;
3578 goto err;
3581 dai->dev = dev;
3582 dai->driver = &dai_drv[i];
3583 if (dai->driver->id)
3584 dai->id = dai->driver->id;
3585 else
3586 dai->id = i;
3587 if (!dai->driver->ops)
3588 dai->driver->ops = &null_dai_ops;
3590 mutex_lock(&client_mutex);
3591 list_add(&dai->list, &dai_list);
3592 mutex_unlock(&client_mutex);
3594 pr_debug("Registered DAI '%s'\n", dai->name);
3597 mutex_lock(&client_mutex);
3598 snd_soc_instantiate_cards();
3599 mutex_unlock(&client_mutex);
3600 return 0;
3602 err:
3603 for (i--; i >= 0; i--)
3604 snd_soc_unregister_dai(dev);
3606 return ret;
3608 EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3611 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3613 * @dai: Array of DAIs to unregister
3614 * @count: Number of DAIs
3616 void snd_soc_unregister_dais(struct device *dev, size_t count)
3618 int i;
3620 for (i = 0; i < count; i++)
3621 snd_soc_unregister_dai(dev);
3623 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3626 * snd_soc_register_platform - Register a platform with the ASoC core
3628 * @platform: platform to register
3630 int snd_soc_register_platform(struct device *dev,
3631 struct snd_soc_platform_driver *platform_drv)
3633 struct snd_soc_platform *platform;
3635 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3637 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3638 if (platform == NULL)
3639 return -ENOMEM;
3641 /* create platform component name */
3642 platform->name = fmt_single_name(dev, &platform->id);
3643 if (platform->name == NULL) {
3644 kfree(platform);
3645 return -ENOMEM;
3648 platform->dev = dev;
3649 platform->driver = platform_drv;
3651 mutex_lock(&client_mutex);
3652 list_add(&platform->list, &platform_list);
3653 snd_soc_instantiate_cards();
3654 mutex_unlock(&client_mutex);
3656 pr_debug("Registered platform '%s'\n", platform->name);
3658 return 0;
3660 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3663 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3665 * @platform: platform to unregister
3667 void snd_soc_unregister_platform(struct device *dev)
3669 struct snd_soc_platform *platform;
3671 list_for_each_entry(platform, &platform_list, list) {
3672 if (dev == platform->dev)
3673 goto found;
3675 return;
3677 found:
3678 mutex_lock(&client_mutex);
3679 list_del(&platform->list);
3680 mutex_unlock(&client_mutex);
3682 pr_debug("Unregistered platform '%s'\n", platform->name);
3683 kfree(platform->name);
3684 kfree(platform);
3686 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3688 static u64 codec_format_map[] = {
3689 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3690 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3691 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3692 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3693 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3694 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3695 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3696 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3697 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3698 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3699 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3700 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3701 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3702 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3703 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3704 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3707 /* Fix up the DAI formats for endianness: codecs don't actually see
3708 * the endianness of the data but we're using the CPU format
3709 * definitions which do need to include endianness so we ensure that
3710 * codec DAIs always have both big and little endian variants set.
3712 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3714 int i;
3716 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3717 if (stream->formats & codec_format_map[i])
3718 stream->formats |= codec_format_map[i];
3722 * snd_soc_register_codec - Register a codec with the ASoC core
3724 * @codec: codec to register
3726 int snd_soc_register_codec(struct device *dev,
3727 const struct snd_soc_codec_driver *codec_drv,
3728 struct snd_soc_dai_driver *dai_drv,
3729 int num_dai)
3731 size_t reg_size;
3732 struct snd_soc_codec *codec;
3733 int ret, i;
3735 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3737 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3738 if (codec == NULL)
3739 return -ENOMEM;
3741 /* create CODEC component name */
3742 codec->name = fmt_single_name(dev, &codec->id);
3743 if (codec->name == NULL) {
3744 kfree(codec);
3745 return -ENOMEM;
3748 if (codec_drv->compress_type)
3749 codec->compress_type = codec_drv->compress_type;
3750 else
3751 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3753 codec->write = codec_drv->write;
3754 codec->read = codec_drv->read;
3755 codec->volatile_register = codec_drv->volatile_register;
3756 codec->readable_register = codec_drv->readable_register;
3757 codec->writable_register = codec_drv->writable_register;
3758 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3759 codec->dapm.dev = dev;
3760 codec->dapm.codec = codec;
3761 codec->dapm.seq_notifier = codec_drv->seq_notifier;
3762 codec->dev = dev;
3763 codec->driver = codec_drv;
3764 codec->num_dai = num_dai;
3765 mutex_init(&codec->mutex);
3767 /* allocate CODEC register cache */
3768 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3769 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
3770 codec->reg_size = reg_size;
3771 /* it is necessary to make a copy of the default register cache
3772 * because in the case of using a compression type that requires
3773 * the default register cache to be marked as __devinitconst the
3774 * kernel might have freed the array by the time we initialize
3775 * the cache.
3777 if (codec_drv->reg_cache_default) {
3778 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3779 reg_size, GFP_KERNEL);
3780 if (!codec->reg_def_copy) {
3781 ret = -ENOMEM;
3782 goto fail;
3787 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3788 if (!codec->volatile_register)
3789 codec->volatile_register = snd_soc_default_volatile_register;
3790 if (!codec->readable_register)
3791 codec->readable_register = snd_soc_default_readable_register;
3792 if (!codec->writable_register)
3793 codec->writable_register = snd_soc_default_writable_register;
3796 for (i = 0; i < num_dai; i++) {
3797 fixup_codec_formats(&dai_drv[i].playback);
3798 fixup_codec_formats(&dai_drv[i].capture);
3801 /* register any DAIs */
3802 if (num_dai) {
3803 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3804 if (ret < 0)
3805 goto fail;
3808 mutex_lock(&client_mutex);
3809 list_add(&codec->list, &codec_list);
3810 snd_soc_instantiate_cards();
3811 mutex_unlock(&client_mutex);
3813 pr_debug("Registered codec '%s'\n", codec->name);
3814 return 0;
3816 fail:
3817 kfree(codec->reg_def_copy);
3818 codec->reg_def_copy = NULL;
3819 kfree(codec->name);
3820 kfree(codec);
3821 return ret;
3823 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3826 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3828 * @codec: codec to unregister
3830 void snd_soc_unregister_codec(struct device *dev)
3832 struct snd_soc_codec *codec;
3833 int i;
3835 list_for_each_entry(codec, &codec_list, list) {
3836 if (dev == codec->dev)
3837 goto found;
3839 return;
3841 found:
3842 if (codec->num_dai)
3843 for (i = 0; i < codec->num_dai; i++)
3844 snd_soc_unregister_dai(dev);
3846 mutex_lock(&client_mutex);
3847 list_del(&codec->list);
3848 mutex_unlock(&client_mutex);
3850 pr_debug("Unregistered codec '%s'\n", codec->name);
3852 snd_soc_cache_exit(codec);
3853 kfree(codec->reg_def_copy);
3854 kfree(codec->name);
3855 kfree(codec);
3857 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3859 static int __init snd_soc_init(void)
3861 #ifdef CONFIG_DEBUG_FS
3862 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3863 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
3864 printk(KERN_WARNING
3865 "ASoC: Failed to create debugfs directory\n");
3866 snd_soc_debugfs_root = NULL;
3869 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
3870 &codec_list_fops))
3871 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3873 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
3874 &dai_list_fops))
3875 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
3877 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
3878 &platform_list_fops))
3879 pr_warn("ASoC: Failed to create platform list debugfs file\n");
3880 #endif
3882 snd_soc_util_init();
3884 return platform_driver_register(&soc_driver);
3886 module_init(snd_soc_init);
3888 static void __exit snd_soc_exit(void)
3890 snd_soc_util_exit();
3892 #ifdef CONFIG_DEBUG_FS
3893 debugfs_remove_recursive(snd_soc_debugfs_root);
3894 #endif
3895 platform_driver_unregister(&soc_driver);
3897 module_exit(snd_soc_exit);
3899 /* Module information */
3900 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3901 MODULE_DESCRIPTION("ALSA SoC Core");
3902 MODULE_LICENSE("GPL");
3903 MODULE_ALIAS("platform:soc-audio");