ASoC: dapm: Implement and instantiate DAI widgets
[linux-2.6/libata-dev.git] / sound / soc / soc-core.c
blob32ca75e2002412efa54022c255ab23ef816dd9f0
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 <linux/of.h>
36 #include <sound/ac97_codec.h>
37 #include <sound/core.h>
38 #include <sound/jack.h>
39 #include <sound/pcm.h>
40 #include <sound/pcm_params.h>
41 #include <sound/soc.h>
42 #include <sound/initval.h>
44 #define CREATE_TRACE_POINTS
45 #include <trace/events/asoc.h>
47 #define NAME_SIZE 32
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);
63 * This is a timeout to do a DAPM powerdown after a stream is closed().
64 * It can be used to eliminate pops between different playback streams, e.g.
65 * between two audio tracks.
67 static int pmdown_time = 5000;
68 module_param(pmdown_time, int, 0);
69 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
71 /* returns the minimum number of bytes needed to represent
72 * a particular given value */
73 static int min_bytes_needed(unsigned long val)
75 int c = 0;
76 int i;
78 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
79 if (val & (1UL << i))
80 break;
81 c = (sizeof val * 8) - c;
82 if (!c || (c % 8))
83 c = (c + 8) / 8;
84 else
85 c /= 8;
86 return c;
89 /* fill buf which is 'len' bytes with a formatted
90 * string of the form 'reg: value\n' */
91 static int format_register_str(struct snd_soc_codec *codec,
92 unsigned int reg, char *buf, size_t len)
94 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
95 int regsize = codec->driver->reg_word_size * 2;
96 int ret;
97 char tmpbuf[len + 1];
98 char regbuf[regsize + 1];
100 /* since tmpbuf is allocated on the stack, warn the callers if they
101 * try to abuse this function */
102 WARN_ON(len > 63);
104 /* +2 for ': ' and + 1 for '\n' */
105 if (wordsize + regsize + 2 + 1 != len)
106 return -EINVAL;
108 ret = snd_soc_read(codec, reg);
109 if (ret < 0) {
110 memset(regbuf, 'X', regsize);
111 regbuf[regsize] = '\0';
112 } else {
113 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
116 /* prepare the buffer */
117 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
118 /* copy it back to the caller without the '\0' */
119 memcpy(buf, tmpbuf, len);
121 return 0;
124 /* codec register dump */
125 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
126 size_t count, loff_t pos)
128 int i, step = 1;
129 int wordsize, regsize;
130 int len;
131 size_t total = 0;
132 loff_t p = 0;
134 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
135 regsize = codec->driver->reg_word_size * 2;
137 len = wordsize + regsize + 2 + 1;
139 if (!codec->driver->reg_cache_size)
140 return 0;
142 if (codec->driver->reg_cache_step)
143 step = codec->driver->reg_cache_step;
145 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
146 if (!snd_soc_codec_readable_register(codec, i))
147 continue;
148 if (codec->driver->display_register) {
149 count += codec->driver->display_register(codec, buf + count,
150 PAGE_SIZE - count, i);
151 } else {
152 /* only support larger than PAGE_SIZE bytes debugfs
153 * entries for the default case */
154 if (p >= pos) {
155 if (total + len >= count - 1)
156 break;
157 format_register_str(codec, i, buf + total, len);
158 total += len;
160 p += len;
164 total = min(total, count - 1);
166 return total;
169 static ssize_t codec_reg_show(struct device *dev,
170 struct device_attribute *attr, char *buf)
172 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
174 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
177 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
179 static ssize_t pmdown_time_show(struct device *dev,
180 struct device_attribute *attr, char *buf)
182 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
184 return sprintf(buf, "%ld\n", rtd->pmdown_time);
187 static ssize_t pmdown_time_set(struct device *dev,
188 struct device_attribute *attr,
189 const char *buf, size_t count)
191 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
192 int ret;
194 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
195 if (ret)
196 return ret;
198 return count;
201 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
203 #ifdef CONFIG_DEBUG_FS
204 static int codec_reg_open_file(struct inode *inode, struct file *file)
206 file->private_data = inode->i_private;
207 return 0;
210 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
211 size_t count, loff_t *ppos)
213 ssize_t ret;
214 struct snd_soc_codec *codec = file->private_data;
215 char *buf;
217 if (*ppos < 0 || !count)
218 return -EINVAL;
220 buf = kmalloc(count, GFP_KERNEL);
221 if (!buf)
222 return -ENOMEM;
224 ret = soc_codec_reg_show(codec, buf, count, *ppos);
225 if (ret >= 0) {
226 if (copy_to_user(user_buf, buf, ret)) {
227 kfree(buf);
228 return -EFAULT;
230 *ppos += ret;
233 kfree(buf);
234 return ret;
237 static ssize_t codec_reg_write_file(struct file *file,
238 const char __user *user_buf, size_t count, loff_t *ppos)
240 char buf[32];
241 size_t buf_size;
242 char *start = buf;
243 unsigned long reg, value;
244 struct snd_soc_codec *codec = file->private_data;
246 buf_size = min(count, (sizeof(buf)-1));
247 if (copy_from_user(buf, user_buf, buf_size))
248 return -EFAULT;
249 buf[buf_size] = 0;
251 while (*start == ' ')
252 start++;
253 reg = simple_strtoul(start, &start, 16);
254 while (*start == ' ')
255 start++;
256 if (strict_strtoul(start, 16, &value))
257 return -EINVAL;
259 /* Userspace has been fiddling around behind the kernel's back */
260 add_taint(TAINT_USER);
262 snd_soc_write(codec, reg, value);
263 return buf_size;
266 static const struct file_operations codec_reg_fops = {
267 .open = codec_reg_open_file,
268 .read = codec_reg_read_file,
269 .write = codec_reg_write_file,
270 .llseek = default_llseek,
273 static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
275 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
277 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
278 debugfs_card_root);
279 if (!codec->debugfs_codec_root) {
280 dev_warn(codec->dev, "Failed to create codec debugfs directory\n");
281 return;
284 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
285 &codec->cache_sync);
286 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
287 &codec->cache_only);
289 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
290 codec->debugfs_codec_root,
291 codec, &codec_reg_fops);
292 if (!codec->debugfs_reg)
293 dev_warn(codec->dev, "Failed to create codec register debugfs file\n");
295 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
298 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
300 debugfs_remove_recursive(codec->debugfs_codec_root);
303 static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
305 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
307 platform->debugfs_platform_root = debugfs_create_dir(platform->name,
308 debugfs_card_root);
309 if (!platform->debugfs_platform_root) {
310 dev_warn(platform->dev,
311 "Failed to create platform debugfs directory\n");
312 return;
315 snd_soc_dapm_debugfs_init(&platform->dapm,
316 platform->debugfs_platform_root);
319 static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
321 debugfs_remove_recursive(platform->debugfs_platform_root);
324 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
325 size_t count, loff_t *ppos)
327 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
328 ssize_t len, ret = 0;
329 struct snd_soc_codec *codec;
331 if (!buf)
332 return -ENOMEM;
334 list_for_each_entry(codec, &codec_list, list) {
335 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
336 codec->name);
337 if (len >= 0)
338 ret += len;
339 if (ret > PAGE_SIZE) {
340 ret = PAGE_SIZE;
341 break;
345 if (ret >= 0)
346 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
348 kfree(buf);
350 return ret;
353 static const struct file_operations codec_list_fops = {
354 .read = codec_list_read_file,
355 .llseek = default_llseek,/* read accesses f_pos */
358 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
359 size_t count, loff_t *ppos)
361 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
362 ssize_t len, ret = 0;
363 struct snd_soc_dai *dai;
365 if (!buf)
366 return -ENOMEM;
368 list_for_each_entry(dai, &dai_list, list) {
369 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
370 if (len >= 0)
371 ret += len;
372 if (ret > PAGE_SIZE) {
373 ret = PAGE_SIZE;
374 break;
378 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
380 kfree(buf);
382 return ret;
385 static const struct file_operations dai_list_fops = {
386 .read = dai_list_read_file,
387 .llseek = default_llseek,/* read accesses f_pos */
390 static ssize_t platform_list_read_file(struct file *file,
391 char __user *user_buf,
392 size_t count, loff_t *ppos)
394 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
395 ssize_t len, ret = 0;
396 struct snd_soc_platform *platform;
398 if (!buf)
399 return -ENOMEM;
401 list_for_each_entry(platform, &platform_list, list) {
402 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
403 platform->name);
404 if (len >= 0)
405 ret += len;
406 if (ret > PAGE_SIZE) {
407 ret = PAGE_SIZE;
408 break;
412 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
414 kfree(buf);
416 return ret;
419 static const struct file_operations platform_list_fops = {
420 .read = platform_list_read_file,
421 .llseek = default_llseek,/* read accesses f_pos */
424 static void soc_init_card_debugfs(struct snd_soc_card *card)
426 card->debugfs_card_root = debugfs_create_dir(card->name,
427 snd_soc_debugfs_root);
428 if (!card->debugfs_card_root) {
429 dev_warn(card->dev,
430 "ASoC: Failed to create card debugfs directory\n");
431 return;
434 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
435 card->debugfs_card_root,
436 &card->pop_time);
437 if (!card->debugfs_pop_time)
438 dev_warn(card->dev,
439 "Failed to create pop time debugfs file\n");
442 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
444 debugfs_remove_recursive(card->debugfs_card_root);
447 #else
449 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
453 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
457 static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
461 static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
465 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
469 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
472 #endif
474 #ifdef CONFIG_SND_SOC_AC97_BUS
475 /* unregister ac97 codec */
476 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
478 if (codec->ac97->dev.bus)
479 device_unregister(&codec->ac97->dev);
480 return 0;
483 /* stop no dev release warning */
484 static void soc_ac97_device_release(struct device *dev){}
486 /* register ac97 codec to bus */
487 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
489 int err;
491 codec->ac97->dev.bus = &ac97_bus_type;
492 codec->ac97->dev.parent = codec->card->dev;
493 codec->ac97->dev.release = soc_ac97_device_release;
495 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
496 codec->card->snd_card->number, 0, codec->name);
497 err = device_register(&codec->ac97->dev);
498 if (err < 0) {
499 snd_printk(KERN_ERR "Can't register ac97 bus\n");
500 codec->ac97->dev.bus = NULL;
501 return err;
503 return 0;
505 #endif
507 #ifdef CONFIG_PM_SLEEP
508 /* powers down audio subsystem for suspend */
509 int snd_soc_suspend(struct device *dev)
511 struct snd_soc_card *card = dev_get_drvdata(dev);
512 struct snd_soc_codec *codec;
513 int i;
515 /* If the initialization of this soc device failed, there is no codec
516 * associated with it. Just bail out in this case.
518 if (list_empty(&card->codec_dev_list))
519 return 0;
521 /* Due to the resume being scheduled into a workqueue we could
522 * suspend before that's finished - wait for it to complete.
524 snd_power_lock(card->snd_card);
525 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
526 snd_power_unlock(card->snd_card);
528 /* we're going to block userspace touching us until resume completes */
529 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
531 /* mute any active DACs */
532 for (i = 0; i < card->num_rtd; i++) {
533 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
534 struct snd_soc_dai_driver *drv = dai->driver;
536 if (card->rtd[i].dai_link->ignore_suspend)
537 continue;
539 if (drv->ops->digital_mute && dai->playback_active)
540 drv->ops->digital_mute(dai, 1);
543 /* suspend all pcms */
544 for (i = 0; i < card->num_rtd; i++) {
545 if (card->rtd[i].dai_link->ignore_suspend)
546 continue;
548 snd_pcm_suspend_all(card->rtd[i].pcm);
551 if (card->suspend_pre)
552 card->suspend_pre(card);
554 for (i = 0; i < card->num_rtd; i++) {
555 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
556 struct snd_soc_platform *platform = card->rtd[i].platform;
558 if (card->rtd[i].dai_link->ignore_suspend)
559 continue;
561 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
562 cpu_dai->driver->suspend(cpu_dai);
563 if (platform->driver->suspend && !platform->suspended) {
564 platform->driver->suspend(cpu_dai);
565 platform->suspended = 1;
569 /* close any waiting streams and save state */
570 for (i = 0; i < card->num_rtd; i++) {
571 flush_delayed_work_sync(&card->rtd[i].delayed_work);
572 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
575 for (i = 0; i < card->num_rtd; i++) {
576 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
578 if (card->rtd[i].dai_link->ignore_suspend)
579 continue;
581 snd_soc_dapm_stream_event(&card->rtd[i],
582 SNDRV_PCM_STREAM_PLAYBACK,
583 codec_dai,
584 SND_SOC_DAPM_STREAM_SUSPEND);
586 snd_soc_dapm_stream_event(&card->rtd[i],
587 SNDRV_PCM_STREAM_CAPTURE,
588 codec_dai,
589 SND_SOC_DAPM_STREAM_SUSPEND);
592 /* suspend all CODECs */
593 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
594 /* If there are paths active then the CODEC will be held with
595 * bias _ON and should not be suspended. */
596 if (!codec->suspended && codec->driver->suspend) {
597 switch (codec->dapm.bias_level) {
598 case SND_SOC_BIAS_STANDBY:
600 * If the CODEC is capable of idle
601 * bias off then being in STANDBY
602 * means it's doing something,
603 * otherwise fall through.
605 if (codec->dapm.idle_bias_off) {
606 dev_dbg(codec->dev,
607 "idle_bias_off CODEC on over suspend\n");
608 break;
610 case SND_SOC_BIAS_OFF:
611 codec->driver->suspend(codec);
612 codec->suspended = 1;
613 codec->cache_sync = 1;
614 break;
615 default:
616 dev_dbg(codec->dev, "CODEC is on over suspend\n");
617 break;
622 for (i = 0; i < card->num_rtd; i++) {
623 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
625 if (card->rtd[i].dai_link->ignore_suspend)
626 continue;
628 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
629 cpu_dai->driver->suspend(cpu_dai);
632 if (card->suspend_post)
633 card->suspend_post(card);
635 return 0;
637 EXPORT_SYMBOL_GPL(snd_soc_suspend);
639 /* deferred resume work, so resume can complete before we finished
640 * setting our codec back up, which can be very slow on I2C
642 static void soc_resume_deferred(struct work_struct *work)
644 struct snd_soc_card *card =
645 container_of(work, struct snd_soc_card, deferred_resume_work);
646 struct snd_soc_codec *codec;
647 int i;
649 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
650 * so userspace apps are blocked from touching us
653 dev_dbg(card->dev, "starting resume work\n");
655 /* Bring us up into D2 so that DAPM starts enabling things */
656 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
658 if (card->resume_pre)
659 card->resume_pre(card);
661 /* resume AC97 DAIs */
662 for (i = 0; i < card->num_rtd; i++) {
663 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
665 if (card->rtd[i].dai_link->ignore_suspend)
666 continue;
668 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
669 cpu_dai->driver->resume(cpu_dai);
672 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
673 /* If the CODEC was idle over suspend then it will have been
674 * left with bias OFF or STANDBY and suspended so we must now
675 * resume. Otherwise the suspend was suppressed.
677 if (codec->driver->resume && codec->suspended) {
678 switch (codec->dapm.bias_level) {
679 case SND_SOC_BIAS_STANDBY:
680 case SND_SOC_BIAS_OFF:
681 codec->driver->resume(codec);
682 codec->suspended = 0;
683 break;
684 default:
685 dev_dbg(codec->dev, "CODEC was on over suspend\n");
686 break;
691 for (i = 0; i < card->num_rtd; i++) {
692 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
694 if (card->rtd[i].dai_link->ignore_suspend)
695 continue;
697 snd_soc_dapm_stream_event(&card->rtd[i],
698 SNDRV_PCM_STREAM_PLAYBACK, codec_dai,
699 SND_SOC_DAPM_STREAM_RESUME);
701 snd_soc_dapm_stream_event(&card->rtd[i],
702 SNDRV_PCM_STREAM_CAPTURE, codec_dai,
703 SND_SOC_DAPM_STREAM_RESUME);
706 /* unmute any active DACs */
707 for (i = 0; i < card->num_rtd; i++) {
708 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
709 struct snd_soc_dai_driver *drv = dai->driver;
711 if (card->rtd[i].dai_link->ignore_suspend)
712 continue;
714 if (drv->ops->digital_mute && dai->playback_active)
715 drv->ops->digital_mute(dai, 0);
718 for (i = 0; i < card->num_rtd; i++) {
719 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
720 struct snd_soc_platform *platform = card->rtd[i].platform;
722 if (card->rtd[i].dai_link->ignore_suspend)
723 continue;
725 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
726 cpu_dai->driver->resume(cpu_dai);
727 if (platform->driver->resume && platform->suspended) {
728 platform->driver->resume(cpu_dai);
729 platform->suspended = 0;
733 if (card->resume_post)
734 card->resume_post(card);
736 dev_dbg(card->dev, "resume work completed\n");
738 /* userspace can access us now we are back as we were before */
739 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
742 /* powers up audio subsystem after a suspend */
743 int snd_soc_resume(struct device *dev)
745 struct snd_soc_card *card = dev_get_drvdata(dev);
746 int i, ac97_control = 0;
748 /* If the initialization of this soc device failed, there is no codec
749 * associated with it. Just bail out in this case.
751 if (list_empty(&card->codec_dev_list))
752 return 0;
754 /* AC97 devices might have other drivers hanging off them so
755 * need to resume immediately. Other drivers don't have that
756 * problem and may take a substantial amount of time to resume
757 * due to I/O costs and anti-pop so handle them out of line.
759 for (i = 0; i < card->num_rtd; i++) {
760 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
761 ac97_control |= cpu_dai->driver->ac97_control;
763 if (ac97_control) {
764 dev_dbg(dev, "Resuming AC97 immediately\n");
765 soc_resume_deferred(&card->deferred_resume_work);
766 } else {
767 dev_dbg(dev, "Scheduling resume work\n");
768 if (!schedule_work(&card->deferred_resume_work))
769 dev_err(dev, "resume work item may be lost\n");
772 return 0;
774 EXPORT_SYMBOL_GPL(snd_soc_resume);
775 #else
776 #define snd_soc_suspend NULL
777 #define snd_soc_resume NULL
778 #endif
780 static const struct snd_soc_dai_ops null_dai_ops = {
783 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
785 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
786 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
787 struct snd_soc_codec *codec;
788 struct snd_soc_platform *platform;
789 struct snd_soc_dai *codec_dai, *cpu_dai;
790 const char *platform_name;
792 if (rtd->complete)
793 return 1;
794 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
796 /* do we already have the CPU DAI for this link ? */
797 if (rtd->cpu_dai) {
798 goto find_codec;
800 /* no, then find CPU DAI from registered DAIs*/
801 list_for_each_entry(cpu_dai, &dai_list, list) {
802 if (dai_link->cpu_dai_of_node) {
803 if (cpu_dai->dev->of_node != dai_link->cpu_dai_of_node)
804 continue;
805 } else {
806 if (strcmp(cpu_dai->name, dai_link->cpu_dai_name))
807 continue;
810 rtd->cpu_dai = cpu_dai;
811 goto find_codec;
813 dev_dbg(card->dev, "CPU DAI %s not registered\n",
814 dai_link->cpu_dai_name);
816 find_codec:
817 /* do we already have the CODEC for this link ? */
818 if (rtd->codec) {
819 goto find_platform;
822 /* no, then find CODEC from registered CODECs*/
823 list_for_each_entry(codec, &codec_list, list) {
824 if (dai_link->codec_of_node) {
825 if (codec->dev->of_node != dai_link->codec_of_node)
826 continue;
827 } else {
828 if (strcmp(codec->name, dai_link->codec_name))
829 continue;
832 rtd->codec = codec;
835 * CODEC found, so find CODEC DAI from registered DAIs from
836 * this CODEC
838 list_for_each_entry(codec_dai, &dai_list, list) {
839 if (codec->dev == codec_dai->dev &&
840 !strcmp(codec_dai->name,
841 dai_link->codec_dai_name)) {
843 rtd->codec_dai = codec_dai;
844 goto find_platform;
847 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
848 dai_link->codec_dai_name);
850 goto find_platform;
852 dev_dbg(card->dev, "CODEC %s not registered\n",
853 dai_link->codec_name);
855 find_platform:
856 /* do we need a platform? */
857 if (rtd->platform)
858 goto out;
860 /* if there's no platform we match on the empty platform */
861 platform_name = dai_link->platform_name;
862 if (!platform_name && !dai_link->platform_of_node)
863 platform_name = "snd-soc-dummy";
865 /* no, then find one from the set of registered platforms */
866 list_for_each_entry(platform, &platform_list, list) {
867 if (dai_link->platform_of_node) {
868 if (platform->dev->of_node !=
869 dai_link->platform_of_node)
870 continue;
871 } else {
872 if (strcmp(platform->name, platform_name))
873 continue;
876 rtd->platform = platform;
877 goto out;
880 dev_dbg(card->dev, "platform %s not registered\n",
881 dai_link->platform_name);
882 return 0;
884 out:
885 /* mark rtd as complete if we found all 4 of our client devices */
886 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
887 rtd->complete = 1;
888 card->num_rtd++;
890 return 1;
893 static void soc_remove_codec(struct snd_soc_codec *codec)
895 int err;
897 if (codec->driver->remove) {
898 err = codec->driver->remove(codec);
899 if (err < 0)
900 dev_err(codec->dev,
901 "asoc: failed to remove %s: %d\n",
902 codec->name, err);
905 /* Make sure all DAPM widgets are freed */
906 snd_soc_dapm_free(&codec->dapm);
908 soc_cleanup_codec_debugfs(codec);
909 codec->probed = 0;
910 list_del(&codec->card_list);
911 module_put(codec->dev->driver->owner);
914 static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
916 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
917 struct snd_soc_codec *codec = rtd->codec;
918 struct snd_soc_platform *platform = rtd->platform;
919 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
920 int err;
922 /* unregister the rtd device */
923 if (rtd->dev_registered) {
924 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
925 device_remove_file(rtd->dev, &dev_attr_codec_reg);
926 device_unregister(rtd->dev);
927 rtd->dev_registered = 0;
930 /* remove the CODEC DAI */
931 if (codec_dai && codec_dai->probed &&
932 codec_dai->driver->remove_order == order) {
933 if (codec_dai->driver->remove) {
934 err = codec_dai->driver->remove(codec_dai);
935 if (err < 0)
936 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
938 codec_dai->probed = 0;
939 list_del(&codec_dai->card_list);
942 /* remove the platform */
943 if (platform && platform->probed &&
944 platform->driver->remove_order == order) {
945 if (platform->driver->remove) {
946 err = platform->driver->remove(platform);
947 if (err < 0)
948 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
951 /* Make sure all DAPM widgets are freed */
952 snd_soc_dapm_free(&platform->dapm);
954 soc_cleanup_platform_debugfs(platform);
955 platform->probed = 0;
956 list_del(&platform->card_list);
957 module_put(platform->dev->driver->owner);
960 /* remove the CODEC */
961 if (codec && codec->probed &&
962 codec->driver->remove_order == order)
963 soc_remove_codec(codec);
965 /* remove the cpu_dai */
966 if (cpu_dai && cpu_dai->probed &&
967 cpu_dai->driver->remove_order == order) {
968 if (cpu_dai->driver->remove) {
969 err = cpu_dai->driver->remove(cpu_dai);
970 if (err < 0)
971 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
973 cpu_dai->probed = 0;
974 list_del(&cpu_dai->card_list);
975 module_put(cpu_dai->dev->driver->owner);
979 static void soc_remove_dai_links(struct snd_soc_card *card)
981 int dai, order;
983 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
984 order++) {
985 for (dai = 0; dai < card->num_rtd; dai++)
986 soc_remove_dai_link(card, dai, order);
988 card->num_rtd = 0;
991 static void soc_set_name_prefix(struct snd_soc_card *card,
992 struct snd_soc_codec *codec)
994 int i;
996 if (card->codec_conf == NULL)
997 return;
999 for (i = 0; i < card->num_configs; i++) {
1000 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1001 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1002 codec->name_prefix = map->name_prefix;
1003 break;
1008 static int soc_probe_codec(struct snd_soc_card *card,
1009 struct snd_soc_codec *codec)
1011 int ret = 0;
1012 const struct snd_soc_codec_driver *driver = codec->driver;
1013 struct snd_soc_dai *dai;
1015 codec->card = card;
1016 codec->dapm.card = card;
1017 soc_set_name_prefix(card, codec);
1019 if (!try_module_get(codec->dev->driver->owner))
1020 return -ENODEV;
1022 soc_init_codec_debugfs(codec);
1024 if (driver->dapm_widgets)
1025 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1026 driver->num_dapm_widgets);
1028 /* Create DAPM widgets for each DAI stream */
1029 list_for_each_entry(dai, &dai_list, list) {
1030 if (dai->dev != codec->dev)
1031 continue;
1033 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1036 codec->dapm.idle_bias_off = driver->idle_bias_off;
1038 if (driver->probe) {
1039 ret = driver->probe(codec);
1040 if (ret < 0) {
1041 dev_err(codec->dev,
1042 "asoc: failed to probe CODEC %s: %d\n",
1043 codec->name, ret);
1044 goto err_probe;
1048 if (driver->controls)
1049 snd_soc_add_codec_controls(codec, driver->controls,
1050 driver->num_controls);
1051 if (driver->dapm_routes)
1052 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1053 driver->num_dapm_routes);
1055 /* mark codec as probed and add to card codec list */
1056 codec->probed = 1;
1057 list_add(&codec->card_list, &card->codec_dev_list);
1058 list_add(&codec->dapm.list, &card->dapm_list);
1060 return 0;
1062 err_probe:
1063 soc_cleanup_codec_debugfs(codec);
1064 module_put(codec->dev->driver->owner);
1066 return ret;
1069 static int soc_probe_platform(struct snd_soc_card *card,
1070 struct snd_soc_platform *platform)
1072 int ret = 0;
1073 const struct snd_soc_platform_driver *driver = platform->driver;
1075 platform->card = card;
1076 platform->dapm.card = card;
1078 if (!try_module_get(platform->dev->driver->owner))
1079 return -ENODEV;
1081 soc_init_platform_debugfs(platform);
1083 if (driver->dapm_widgets)
1084 snd_soc_dapm_new_controls(&platform->dapm,
1085 driver->dapm_widgets, driver->num_dapm_widgets);
1087 if (driver->probe) {
1088 ret = driver->probe(platform);
1089 if (ret < 0) {
1090 dev_err(platform->dev,
1091 "asoc: failed to probe platform %s: %d\n",
1092 platform->name, ret);
1093 goto err_probe;
1097 if (driver->controls)
1098 snd_soc_add_platform_controls(platform, driver->controls,
1099 driver->num_controls);
1100 if (driver->dapm_routes)
1101 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1102 driver->num_dapm_routes);
1104 /* mark platform as probed and add to card platform list */
1105 platform->probed = 1;
1106 list_add(&platform->card_list, &card->platform_dev_list);
1107 list_add(&platform->dapm.list, &card->dapm_list);
1109 return 0;
1111 err_probe:
1112 module_put(platform->dev->driver->owner);
1114 return ret;
1117 static void rtd_release(struct device *dev)
1119 kfree(dev);
1122 static int soc_post_component_init(struct snd_soc_card *card,
1123 struct snd_soc_codec *codec,
1124 int num, int dailess)
1126 struct snd_soc_dai_link *dai_link = NULL;
1127 struct snd_soc_aux_dev *aux_dev = NULL;
1128 struct snd_soc_pcm_runtime *rtd;
1129 const char *temp, *name;
1130 int ret = 0;
1132 if (!dailess) {
1133 dai_link = &card->dai_link[num];
1134 rtd = &card->rtd[num];
1135 name = dai_link->name;
1136 } else {
1137 aux_dev = &card->aux_dev[num];
1138 rtd = &card->rtd_aux[num];
1139 name = aux_dev->name;
1141 rtd->card = card;
1143 /* Make sure all DAPM widgets are instantiated */
1144 snd_soc_dapm_new_widgets(&codec->dapm);
1146 /* machine controls, routes and widgets are not prefixed */
1147 temp = codec->name_prefix;
1148 codec->name_prefix = NULL;
1150 /* do machine specific initialization */
1151 if (!dailess && dai_link->init)
1152 ret = dai_link->init(rtd);
1153 else if (dailess && aux_dev->init)
1154 ret = aux_dev->init(&codec->dapm);
1155 if (ret < 0) {
1156 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1157 return ret;
1159 codec->name_prefix = temp;
1161 /* register the rtd device */
1162 rtd->codec = codec;
1164 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1165 if (!rtd->dev)
1166 return -ENOMEM;
1167 device_initialize(rtd->dev);
1168 rtd->dev->parent = card->dev;
1169 rtd->dev->release = rtd_release;
1170 rtd->dev->init_name = name;
1171 dev_set_drvdata(rtd->dev, rtd);
1172 mutex_init(&rtd->pcm_mutex);
1173 ret = device_add(rtd->dev);
1174 if (ret < 0) {
1175 dev_err(card->dev,
1176 "asoc: failed to register runtime device: %d\n", ret);
1177 return ret;
1179 rtd->dev_registered = 1;
1181 /* add DAPM sysfs entries for this codec */
1182 ret = snd_soc_dapm_sys_add(rtd->dev);
1183 if (ret < 0)
1184 dev_err(codec->dev,
1185 "asoc: failed to add codec dapm sysfs entries: %d\n",
1186 ret);
1188 /* add codec sysfs entries */
1189 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1190 if (ret < 0)
1191 dev_err(codec->dev,
1192 "asoc: failed to add codec sysfs files: %d\n", ret);
1194 return 0;
1197 static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
1199 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1200 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1201 struct snd_soc_codec *codec = rtd->codec;
1202 struct snd_soc_platform *platform = rtd->platform;
1203 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1204 int ret;
1206 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1207 card->name, num, order);
1209 /* config components */
1210 codec_dai->codec = codec;
1211 cpu_dai->platform = platform;
1212 codec_dai->card = card;
1213 cpu_dai->card = card;
1215 /* set default power off timeout */
1216 rtd->pmdown_time = pmdown_time;
1218 /* probe the cpu_dai */
1219 if (!cpu_dai->probed &&
1220 cpu_dai->driver->probe_order == order) {
1221 if (!try_module_get(cpu_dai->dev->driver->owner))
1222 return -ENODEV;
1224 if (cpu_dai->driver->probe) {
1225 ret = cpu_dai->driver->probe(cpu_dai);
1226 if (ret < 0) {
1227 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1228 cpu_dai->name);
1229 module_put(cpu_dai->dev->driver->owner);
1230 return ret;
1233 cpu_dai->probed = 1;
1234 /* mark cpu_dai as probed and add to card dai list */
1235 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1238 /* probe the CODEC */
1239 if (!codec->probed &&
1240 codec->driver->probe_order == order) {
1241 ret = soc_probe_codec(card, codec);
1242 if (ret < 0)
1243 return ret;
1246 /* probe the platform */
1247 if (!platform->probed &&
1248 platform->driver->probe_order == order) {
1249 ret = soc_probe_platform(card, platform);
1250 if (ret < 0)
1251 return ret;
1254 /* probe the CODEC DAI */
1255 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1256 if (codec_dai->driver->probe) {
1257 ret = codec_dai->driver->probe(codec_dai);
1258 if (ret < 0) {
1259 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1260 codec_dai->name);
1261 return ret;
1265 /* mark codec_dai as probed and add to card dai list */
1266 codec_dai->probed = 1;
1267 list_add(&codec_dai->card_list, &card->dai_dev_list);
1270 /* complete DAI probe during last probe */
1271 if (order != SND_SOC_COMP_ORDER_LAST)
1272 return 0;
1274 ret = soc_post_component_init(card, codec, num, 0);
1275 if (ret)
1276 return ret;
1278 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1279 if (ret < 0)
1280 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1282 /* create the pcm */
1283 ret = soc_new_pcm(rtd, num);
1284 if (ret < 0) {
1285 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1286 return ret;
1289 /* add platform data for AC97 devices */
1290 if (rtd->codec_dai->driver->ac97_control)
1291 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1293 return 0;
1296 #ifdef CONFIG_SND_SOC_AC97_BUS
1297 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1299 int ret;
1301 /* Only instantiate AC97 if not already done by the adaptor
1302 * for the generic AC97 subsystem.
1304 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
1306 * It is possible that the AC97 device is already registered to
1307 * the device subsystem. This happens when the device is created
1308 * via snd_ac97_mixer(). Currently only SoC codec that does so
1309 * is the generic AC97 glue but others migh emerge.
1311 * In those cases we don't try to register the device again.
1313 if (!rtd->codec->ac97_created)
1314 return 0;
1316 ret = soc_ac97_dev_register(rtd->codec);
1317 if (ret < 0) {
1318 printk(KERN_ERR "asoc: AC97 device register failed\n");
1319 return ret;
1322 rtd->codec->ac97_registered = 1;
1324 return 0;
1327 static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1329 if (codec->ac97_registered) {
1330 soc_ac97_dev_unregister(codec);
1331 codec->ac97_registered = 0;
1334 #endif
1336 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1338 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1339 struct snd_soc_codec *codec;
1340 int ret = -ENODEV;
1342 /* find CODEC from registered CODECs*/
1343 list_for_each_entry(codec, &codec_list, list) {
1344 if (!strcmp(codec->name, aux_dev->codec_name)) {
1345 if (codec->probed) {
1346 dev_err(codec->dev,
1347 "asoc: codec already probed");
1348 ret = -EBUSY;
1349 goto out;
1351 goto found;
1354 /* codec not found */
1355 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1356 goto out;
1358 found:
1359 ret = soc_probe_codec(card, codec);
1360 if (ret < 0)
1361 return ret;
1363 ret = soc_post_component_init(card, codec, num, 1);
1365 out:
1366 return ret;
1369 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1371 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1372 struct snd_soc_codec *codec = rtd->codec;
1374 /* unregister the rtd device */
1375 if (rtd->dev_registered) {
1376 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1377 device_del(rtd->dev);
1378 rtd->dev_registered = 0;
1381 if (codec && codec->probed)
1382 soc_remove_codec(codec);
1385 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1386 enum snd_soc_compress_type compress_type)
1388 int ret;
1390 if (codec->cache_init)
1391 return 0;
1393 /* override the compress_type if necessary */
1394 if (compress_type && codec->compress_type != compress_type)
1395 codec->compress_type = compress_type;
1396 ret = snd_soc_cache_init(codec);
1397 if (ret < 0) {
1398 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1399 ret);
1400 return ret;
1402 codec->cache_init = 1;
1403 return 0;
1406 static void snd_soc_instantiate_card(struct snd_soc_card *card)
1408 struct snd_soc_codec *codec;
1409 struct snd_soc_codec_conf *codec_conf;
1410 enum snd_soc_compress_type compress_type;
1411 struct snd_soc_dai_link *dai_link;
1412 int ret, i, order;
1414 mutex_lock(&card->mutex);
1416 if (card->instantiated) {
1417 mutex_unlock(&card->mutex);
1418 return;
1421 /* bind DAIs */
1422 for (i = 0; i < card->num_links; i++)
1423 soc_bind_dai_link(card, i);
1425 /* bind completed ? */
1426 if (card->num_rtd != card->num_links) {
1427 mutex_unlock(&card->mutex);
1428 return;
1431 /* initialize the register cache for each available codec */
1432 list_for_each_entry(codec, &codec_list, list) {
1433 if (codec->cache_init)
1434 continue;
1435 /* by default we don't override the compress_type */
1436 compress_type = 0;
1437 /* check to see if we need to override the compress_type */
1438 for (i = 0; i < card->num_configs; ++i) {
1439 codec_conf = &card->codec_conf[i];
1440 if (!strcmp(codec->name, codec_conf->dev_name)) {
1441 compress_type = codec_conf->compress_type;
1442 if (compress_type && compress_type
1443 != codec->compress_type)
1444 break;
1447 ret = snd_soc_init_codec_cache(codec, compress_type);
1448 if (ret < 0) {
1449 mutex_unlock(&card->mutex);
1450 return;
1454 /* card bind complete so register a sound card */
1455 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1456 card->owner, 0, &card->snd_card);
1457 if (ret < 0) {
1458 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1459 card->name);
1460 mutex_unlock(&card->mutex);
1461 return;
1463 card->snd_card->dev = card->dev;
1465 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1466 card->dapm.dev = card->dev;
1467 card->dapm.card = card;
1468 list_add(&card->dapm.list, &card->dapm_list);
1470 #ifdef CONFIG_DEBUG_FS
1471 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1472 #endif
1474 #ifdef CONFIG_PM_SLEEP
1475 /* deferred resume work */
1476 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1477 #endif
1479 if (card->dapm_widgets)
1480 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1481 card->num_dapm_widgets);
1483 /* initialise the sound card only once */
1484 if (card->probe) {
1485 ret = card->probe(card);
1486 if (ret < 0)
1487 goto card_probe_error;
1490 /* early DAI link probe */
1491 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1492 order++) {
1493 for (i = 0; i < card->num_links; i++) {
1494 ret = soc_probe_dai_link(card, i, order);
1495 if (ret < 0) {
1496 pr_err("asoc: failed to instantiate card %s: %d\n",
1497 card->name, ret);
1498 goto probe_dai_err;
1503 for (i = 0; i < card->num_aux_devs; i++) {
1504 ret = soc_probe_aux_dev(card, i);
1505 if (ret < 0) {
1506 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1507 card->name, ret);
1508 goto probe_aux_dev_err;
1512 snd_soc_dapm_link_dai_widgets(card);
1514 if (card->controls)
1515 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1517 if (card->dapm_routes)
1518 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1519 card->num_dapm_routes);
1521 snd_soc_dapm_new_widgets(&card->dapm);
1523 for (i = 0; i < card->num_links; i++) {
1524 dai_link = &card->dai_link[i];
1526 if (dai_link->dai_fmt) {
1527 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1528 dai_link->dai_fmt);
1529 if (ret != 0)
1530 dev_warn(card->rtd[i].codec_dai->dev,
1531 "Failed to set DAI format: %d\n",
1532 ret);
1534 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1535 dai_link->dai_fmt);
1536 if (ret != 0)
1537 dev_warn(card->rtd[i].cpu_dai->dev,
1538 "Failed to set DAI format: %d\n",
1539 ret);
1543 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1544 "%s", card->name);
1545 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1546 "%s", card->long_name ? card->long_name : card->name);
1547 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1548 "%s", card->driver_name ? card->driver_name : card->name);
1549 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1550 switch (card->snd_card->driver[i]) {
1551 case '_':
1552 case '-':
1553 case '\0':
1554 break;
1555 default:
1556 if (!isalnum(card->snd_card->driver[i]))
1557 card->snd_card->driver[i] = '_';
1558 break;
1562 if (card->late_probe) {
1563 ret = card->late_probe(card);
1564 if (ret < 0) {
1565 dev_err(card->dev, "%s late_probe() failed: %d\n",
1566 card->name, ret);
1567 goto probe_aux_dev_err;
1571 snd_soc_dapm_new_widgets(&card->dapm);
1573 if (card->fully_routed)
1574 list_for_each_entry(codec, &card->codec_dev_list, card_list)
1575 snd_soc_dapm_auto_nc_codec_pins(codec);
1577 ret = snd_card_register(card->snd_card);
1578 if (ret < 0) {
1579 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1580 goto probe_aux_dev_err;
1583 #ifdef CONFIG_SND_SOC_AC97_BUS
1584 /* register any AC97 codecs */
1585 for (i = 0; i < card->num_rtd; i++) {
1586 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1587 if (ret < 0) {
1588 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1589 while (--i >= 0)
1590 soc_unregister_ac97_dai_link(card->rtd[i].codec);
1591 goto probe_aux_dev_err;
1594 #endif
1596 card->instantiated = 1;
1597 snd_soc_dapm_sync(&card->dapm);
1598 mutex_unlock(&card->mutex);
1599 return;
1601 probe_aux_dev_err:
1602 for (i = 0; i < card->num_aux_devs; i++)
1603 soc_remove_aux_dev(card, i);
1605 probe_dai_err:
1606 soc_remove_dai_links(card);
1608 card_probe_error:
1609 if (card->remove)
1610 card->remove(card);
1612 snd_card_free(card->snd_card);
1614 mutex_unlock(&card->mutex);
1618 * Attempt to initialise any uninitialised cards. Must be called with
1619 * client_mutex.
1621 static void snd_soc_instantiate_cards(void)
1623 struct snd_soc_card *card;
1624 list_for_each_entry(card, &card_list, list)
1625 snd_soc_instantiate_card(card);
1628 /* probes a new socdev */
1629 static int soc_probe(struct platform_device *pdev)
1631 struct snd_soc_card *card = platform_get_drvdata(pdev);
1632 int ret = 0;
1635 * no card, so machine driver should be registering card
1636 * we should not be here in that case so ret error
1638 if (!card)
1639 return -EINVAL;
1641 /* Bodge while we unpick instantiation */
1642 card->dev = &pdev->dev;
1644 ret = snd_soc_register_card(card);
1645 if (ret != 0) {
1646 dev_err(&pdev->dev, "Failed to register card\n");
1647 return ret;
1650 return 0;
1653 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1655 int i;
1657 /* make sure any delayed work runs */
1658 for (i = 0; i < card->num_rtd; i++) {
1659 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1660 flush_delayed_work_sync(&rtd->delayed_work);
1663 /* remove auxiliary devices */
1664 for (i = 0; i < card->num_aux_devs; i++)
1665 soc_remove_aux_dev(card, i);
1667 /* remove and free each DAI */
1668 soc_remove_dai_links(card);
1670 soc_cleanup_card_debugfs(card);
1672 /* remove the card */
1673 if (card->remove)
1674 card->remove(card);
1676 snd_soc_dapm_free(&card->dapm);
1678 kfree(card->rtd);
1679 snd_card_free(card->snd_card);
1680 return 0;
1684 /* removes a socdev */
1685 static int soc_remove(struct platform_device *pdev)
1687 struct snd_soc_card *card = platform_get_drvdata(pdev);
1689 snd_soc_unregister_card(card);
1690 return 0;
1693 int snd_soc_poweroff(struct device *dev)
1695 struct snd_soc_card *card = dev_get_drvdata(dev);
1696 int i;
1698 if (!card->instantiated)
1699 return 0;
1701 /* Flush out pmdown_time work - we actually do want to run it
1702 * now, we're shutting down so no imminent restart. */
1703 for (i = 0; i < card->num_rtd; i++) {
1704 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1705 flush_delayed_work_sync(&rtd->delayed_work);
1708 snd_soc_dapm_shutdown(card);
1710 return 0;
1712 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1714 const struct dev_pm_ops snd_soc_pm_ops = {
1715 SET_SYSTEM_SLEEP_PM_OPS(snd_soc_suspend, snd_soc_resume)
1716 .poweroff = snd_soc_poweroff,
1718 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1720 /* ASoC platform driver */
1721 static struct platform_driver soc_driver = {
1722 .driver = {
1723 .name = "soc-audio",
1724 .owner = THIS_MODULE,
1725 .pm = &snd_soc_pm_ops,
1727 .probe = soc_probe,
1728 .remove = soc_remove,
1732 * snd_soc_codec_volatile_register: Report if a register is volatile.
1734 * @codec: CODEC to query.
1735 * @reg: Register to query.
1737 * Boolean function indiciating if a CODEC register is volatile.
1739 int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1740 unsigned int reg)
1742 if (codec->volatile_register)
1743 return codec->volatile_register(codec, reg);
1744 else
1745 return 0;
1747 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1750 * snd_soc_codec_readable_register: Report if a register is readable.
1752 * @codec: CODEC to query.
1753 * @reg: Register to query.
1755 * Boolean function indicating if a CODEC register is readable.
1757 int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1758 unsigned int reg)
1760 if (codec->readable_register)
1761 return codec->readable_register(codec, reg);
1762 else
1763 return 1;
1765 EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1768 * snd_soc_codec_writable_register: Report if a register is writable.
1770 * @codec: CODEC to query.
1771 * @reg: Register to query.
1773 * Boolean function indicating if a CODEC register is writable.
1775 int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1776 unsigned int reg)
1778 if (codec->writable_register)
1779 return codec->writable_register(codec, reg);
1780 else
1781 return 1;
1783 EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1785 int snd_soc_platform_read(struct snd_soc_platform *platform,
1786 unsigned int reg)
1788 unsigned int ret;
1790 if (!platform->driver->read) {
1791 dev_err(platform->dev, "platform has no read back\n");
1792 return -1;
1795 ret = platform->driver->read(platform, reg);
1796 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
1797 trace_snd_soc_preg_read(platform, reg, ret);
1799 return ret;
1801 EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1803 int snd_soc_platform_write(struct snd_soc_platform *platform,
1804 unsigned int reg, unsigned int val)
1806 if (!platform->driver->write) {
1807 dev_err(platform->dev, "platform has no write back\n");
1808 return -1;
1811 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
1812 trace_snd_soc_preg_write(platform, reg, val);
1813 return platform->driver->write(platform, reg, val);
1815 EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1818 * snd_soc_new_ac97_codec - initailise AC97 device
1819 * @codec: audio codec
1820 * @ops: AC97 bus operations
1821 * @num: AC97 codec number
1823 * Initialises AC97 codec resources for use by ad-hoc devices only.
1825 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1826 struct snd_ac97_bus_ops *ops, int num)
1828 mutex_lock(&codec->mutex);
1830 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1831 if (codec->ac97 == NULL) {
1832 mutex_unlock(&codec->mutex);
1833 return -ENOMEM;
1836 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1837 if (codec->ac97->bus == NULL) {
1838 kfree(codec->ac97);
1839 codec->ac97 = NULL;
1840 mutex_unlock(&codec->mutex);
1841 return -ENOMEM;
1844 codec->ac97->bus->ops = ops;
1845 codec->ac97->num = num;
1848 * Mark the AC97 device to be created by us. This way we ensure that the
1849 * device will be registered with the device subsystem later on.
1851 codec->ac97_created = 1;
1853 mutex_unlock(&codec->mutex);
1854 return 0;
1856 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1859 * snd_soc_free_ac97_codec - free AC97 codec device
1860 * @codec: audio codec
1862 * Frees AC97 codec device resources.
1864 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1866 mutex_lock(&codec->mutex);
1867 #ifdef CONFIG_SND_SOC_AC97_BUS
1868 soc_unregister_ac97_dai_link(codec);
1869 #endif
1870 kfree(codec->ac97->bus);
1871 kfree(codec->ac97);
1872 codec->ac97 = NULL;
1873 codec->ac97_created = 0;
1874 mutex_unlock(&codec->mutex);
1876 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1878 unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1880 unsigned int ret;
1882 ret = codec->read(codec, reg);
1883 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
1884 trace_snd_soc_reg_read(codec, reg, ret);
1886 return ret;
1888 EXPORT_SYMBOL_GPL(snd_soc_read);
1890 unsigned int snd_soc_write(struct snd_soc_codec *codec,
1891 unsigned int reg, unsigned int val)
1893 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
1894 trace_snd_soc_reg_write(codec, reg, val);
1895 return codec->write(codec, reg, val);
1897 EXPORT_SYMBOL_GPL(snd_soc_write);
1899 unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
1900 unsigned int reg, const void *data, size_t len)
1902 return codec->bulk_write_raw(codec, reg, data, len);
1904 EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
1907 * snd_soc_update_bits - update codec register bits
1908 * @codec: audio codec
1909 * @reg: codec register
1910 * @mask: register mask
1911 * @value: new value
1913 * Writes new register value.
1915 * Returns 1 for change, 0 for no change, or negative error code.
1917 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1918 unsigned int mask, unsigned int value)
1920 bool change;
1921 unsigned int old, new;
1922 int ret;
1924 if (codec->using_regmap) {
1925 ret = regmap_update_bits_check(codec->control_data, reg,
1926 mask, value, &change);
1927 } else {
1928 ret = snd_soc_read(codec, reg);
1929 if (ret < 0)
1930 return ret;
1932 old = ret;
1933 new = (old & ~mask) | (value & mask);
1934 change = old != new;
1935 if (change)
1936 ret = snd_soc_write(codec, reg, new);
1939 if (ret < 0)
1940 return ret;
1942 return change;
1944 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1947 * snd_soc_update_bits_locked - update codec register bits
1948 * @codec: audio codec
1949 * @reg: codec register
1950 * @mask: register mask
1951 * @value: new value
1953 * Writes new register value, and takes the codec mutex.
1955 * Returns 1 for change else 0.
1957 int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1958 unsigned short reg, unsigned int mask,
1959 unsigned int value)
1961 int change;
1963 mutex_lock(&codec->mutex);
1964 change = snd_soc_update_bits(codec, reg, mask, value);
1965 mutex_unlock(&codec->mutex);
1967 return change;
1969 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
1972 * snd_soc_test_bits - test register for change
1973 * @codec: audio codec
1974 * @reg: codec register
1975 * @mask: register mask
1976 * @value: new value
1978 * Tests a register with a new value and checks if the new value is
1979 * different from the old value.
1981 * Returns 1 for change else 0.
1983 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1984 unsigned int mask, unsigned int value)
1986 int change;
1987 unsigned int old, new;
1989 old = snd_soc_read(codec, reg);
1990 new = (old & ~mask) | value;
1991 change = old != new;
1993 return change;
1995 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1998 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1999 * @substream: the pcm substream
2000 * @hw: the hardware parameters
2002 * Sets the substream runtime hardware parameters.
2004 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2005 const struct snd_pcm_hardware *hw)
2007 struct snd_pcm_runtime *runtime = substream->runtime;
2008 runtime->hw.info = hw->info;
2009 runtime->hw.formats = hw->formats;
2010 runtime->hw.period_bytes_min = hw->period_bytes_min;
2011 runtime->hw.period_bytes_max = hw->period_bytes_max;
2012 runtime->hw.periods_min = hw->periods_min;
2013 runtime->hw.periods_max = hw->periods_max;
2014 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2015 runtime->hw.fifo_size = hw->fifo_size;
2016 return 0;
2018 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2021 * snd_soc_cnew - create new control
2022 * @_template: control template
2023 * @data: control private data
2024 * @long_name: control long name
2025 * @prefix: control name prefix
2027 * Create a new mixer control from a template control.
2029 * Returns 0 for success, else error.
2031 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2032 void *data, const char *long_name,
2033 const char *prefix)
2035 struct snd_kcontrol_new template;
2036 struct snd_kcontrol *kcontrol;
2037 char *name = NULL;
2038 int name_len;
2040 memcpy(&template, _template, sizeof(template));
2041 template.index = 0;
2043 if (!long_name)
2044 long_name = template.name;
2046 if (prefix) {
2047 name_len = strlen(long_name) + strlen(prefix) + 2;
2048 name = kmalloc(name_len, GFP_KERNEL);
2049 if (!name)
2050 return NULL;
2052 snprintf(name, name_len, "%s %s", prefix, long_name);
2054 template.name = name;
2055 } else {
2056 template.name = long_name;
2059 kcontrol = snd_ctl_new1(&template, data);
2061 kfree(name);
2063 return kcontrol;
2065 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2067 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2068 const struct snd_kcontrol_new *controls, int num_controls,
2069 const char *prefix, void *data)
2071 int err, i;
2073 for (i = 0; i < num_controls; i++) {
2074 const struct snd_kcontrol_new *control = &controls[i];
2075 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2076 control->name, prefix));
2077 if (err < 0) {
2078 dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2079 return err;
2083 return 0;
2087 * snd_soc_add_codec_controls - add an array of controls to a codec.
2088 * Convenience function to add a list of controls. Many codecs were
2089 * duplicating this code.
2091 * @codec: codec to add controls to
2092 * @controls: array of controls to add
2093 * @num_controls: number of elements in the array
2095 * Return 0 for success, else error.
2097 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2098 const struct snd_kcontrol_new *controls, int num_controls)
2100 struct snd_card *card = codec->card->snd_card;
2102 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2103 codec->name_prefix, codec);
2105 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2108 * snd_soc_add_platform_controls - add an array of controls to a platform.
2109 * Convenience function to add a list of controls.
2111 * @platform: platform to add controls to
2112 * @controls: array of controls to add
2113 * @num_controls: number of elements in the array
2115 * Return 0 for success, else error.
2117 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2118 const struct snd_kcontrol_new *controls, int num_controls)
2120 struct snd_card *card = platform->card->snd_card;
2122 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2123 NULL, platform);
2125 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2128 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2129 * Convenience function to add a list of controls.
2131 * @soc_card: SoC card to add controls to
2132 * @controls: array of controls to add
2133 * @num_controls: number of elements in the array
2135 * Return 0 for success, else error.
2137 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2138 const struct snd_kcontrol_new *controls, int num_controls)
2140 struct snd_card *card = soc_card->snd_card;
2142 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2143 NULL, soc_card);
2145 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2148 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2149 * Convienience function to add a list of controls.
2151 * @dai: DAI to add controls to
2152 * @controls: array of controls to add
2153 * @num_controls: number of elements in the array
2155 * Return 0 for success, else error.
2157 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2158 const struct snd_kcontrol_new *controls, int num_controls)
2160 struct snd_card *card = dai->card->snd_card;
2162 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2163 NULL, dai);
2165 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2168 * snd_soc_info_enum_double - enumerated double mixer info callback
2169 * @kcontrol: mixer control
2170 * @uinfo: control element information
2172 * Callback to provide information about a double enumerated
2173 * mixer control.
2175 * Returns 0 for success.
2177 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2178 struct snd_ctl_elem_info *uinfo)
2180 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2182 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2183 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2184 uinfo->value.enumerated.items = e->max;
2186 if (uinfo->value.enumerated.item > e->max - 1)
2187 uinfo->value.enumerated.item = e->max - 1;
2188 strcpy(uinfo->value.enumerated.name,
2189 e->texts[uinfo->value.enumerated.item]);
2190 return 0;
2192 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2195 * snd_soc_get_enum_double - enumerated double mixer get callback
2196 * @kcontrol: mixer control
2197 * @ucontrol: control element information
2199 * Callback to get the value of a double enumerated mixer.
2201 * Returns 0 for success.
2203 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2204 struct snd_ctl_elem_value *ucontrol)
2206 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2207 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2208 unsigned int val, bitmask;
2210 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2212 val = snd_soc_read(codec, e->reg);
2213 ucontrol->value.enumerated.item[0]
2214 = (val >> e->shift_l) & (bitmask - 1);
2215 if (e->shift_l != e->shift_r)
2216 ucontrol->value.enumerated.item[1] =
2217 (val >> e->shift_r) & (bitmask - 1);
2219 return 0;
2221 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2224 * snd_soc_put_enum_double - enumerated double mixer put callback
2225 * @kcontrol: mixer control
2226 * @ucontrol: control element information
2228 * Callback to set the value of a double enumerated mixer.
2230 * Returns 0 for success.
2232 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2233 struct snd_ctl_elem_value *ucontrol)
2235 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2236 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2237 unsigned int val;
2238 unsigned int mask, bitmask;
2240 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2242 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2243 return -EINVAL;
2244 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2245 mask = (bitmask - 1) << e->shift_l;
2246 if (e->shift_l != e->shift_r) {
2247 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2248 return -EINVAL;
2249 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2250 mask |= (bitmask - 1) << e->shift_r;
2253 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2255 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2258 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2259 * @kcontrol: mixer control
2260 * @ucontrol: control element information
2262 * Callback to get the value of a double semi enumerated mixer.
2264 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2265 * used for handling bitfield coded enumeration for example.
2267 * Returns 0 for success.
2269 int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2270 struct snd_ctl_elem_value *ucontrol)
2272 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2273 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2274 unsigned int reg_val, val, mux;
2276 reg_val = snd_soc_read(codec, e->reg);
2277 val = (reg_val >> e->shift_l) & e->mask;
2278 for (mux = 0; mux < e->max; mux++) {
2279 if (val == e->values[mux])
2280 break;
2282 ucontrol->value.enumerated.item[0] = mux;
2283 if (e->shift_l != e->shift_r) {
2284 val = (reg_val >> e->shift_r) & e->mask;
2285 for (mux = 0; mux < e->max; mux++) {
2286 if (val == e->values[mux])
2287 break;
2289 ucontrol->value.enumerated.item[1] = mux;
2292 return 0;
2294 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2297 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2298 * @kcontrol: mixer control
2299 * @ucontrol: control element information
2301 * Callback to set the value of a double semi enumerated mixer.
2303 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2304 * used for handling bitfield coded enumeration for example.
2306 * Returns 0 for success.
2308 int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2309 struct snd_ctl_elem_value *ucontrol)
2311 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2312 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2313 unsigned int val;
2314 unsigned int mask;
2316 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2317 return -EINVAL;
2318 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2319 mask = e->mask << e->shift_l;
2320 if (e->shift_l != e->shift_r) {
2321 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2322 return -EINVAL;
2323 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2324 mask |= e->mask << e->shift_r;
2327 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2329 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2332 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2333 * @kcontrol: mixer control
2334 * @uinfo: control element information
2336 * Callback to provide information about an external enumerated
2337 * single mixer.
2339 * Returns 0 for success.
2341 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2342 struct snd_ctl_elem_info *uinfo)
2344 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2346 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2347 uinfo->count = 1;
2348 uinfo->value.enumerated.items = e->max;
2350 if (uinfo->value.enumerated.item > e->max - 1)
2351 uinfo->value.enumerated.item = e->max - 1;
2352 strcpy(uinfo->value.enumerated.name,
2353 e->texts[uinfo->value.enumerated.item]);
2354 return 0;
2356 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2359 * snd_soc_info_volsw_ext - external single mixer info callback
2360 * @kcontrol: mixer control
2361 * @uinfo: control element information
2363 * Callback to provide information about a single external mixer control.
2365 * Returns 0 for success.
2367 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2368 struct snd_ctl_elem_info *uinfo)
2370 int max = kcontrol->private_value;
2372 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2373 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2374 else
2375 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2377 uinfo->count = 1;
2378 uinfo->value.integer.min = 0;
2379 uinfo->value.integer.max = max;
2380 return 0;
2382 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2385 * snd_soc_info_volsw - single mixer info callback
2386 * @kcontrol: mixer control
2387 * @uinfo: control element information
2389 * Callback to provide information about a single mixer control, or a double
2390 * mixer control that spans 2 registers.
2392 * Returns 0 for success.
2394 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2395 struct snd_ctl_elem_info *uinfo)
2397 struct soc_mixer_control *mc =
2398 (struct soc_mixer_control *)kcontrol->private_value;
2399 int platform_max;
2401 if (!mc->platform_max)
2402 mc->platform_max = mc->max;
2403 platform_max = mc->platform_max;
2405 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2406 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2407 else
2408 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2410 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2411 uinfo->value.integer.min = 0;
2412 uinfo->value.integer.max = platform_max;
2413 return 0;
2415 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2418 * snd_soc_get_volsw - single mixer get callback
2419 * @kcontrol: mixer control
2420 * @ucontrol: control element information
2422 * Callback to get the value of a single mixer control, or a double mixer
2423 * control that spans 2 registers.
2425 * Returns 0 for success.
2427 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2428 struct snd_ctl_elem_value *ucontrol)
2430 struct soc_mixer_control *mc =
2431 (struct soc_mixer_control *)kcontrol->private_value;
2432 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2433 unsigned int reg = mc->reg;
2434 unsigned int reg2 = mc->rreg;
2435 unsigned int shift = mc->shift;
2436 unsigned int rshift = mc->rshift;
2437 int max = mc->max;
2438 unsigned int mask = (1 << fls(max)) - 1;
2439 unsigned int invert = mc->invert;
2441 ucontrol->value.integer.value[0] =
2442 (snd_soc_read(codec, reg) >> shift) & mask;
2443 if (invert)
2444 ucontrol->value.integer.value[0] =
2445 max - ucontrol->value.integer.value[0];
2447 if (snd_soc_volsw_is_stereo(mc)) {
2448 if (reg == reg2)
2449 ucontrol->value.integer.value[1] =
2450 (snd_soc_read(codec, reg) >> rshift) & mask;
2451 else
2452 ucontrol->value.integer.value[1] =
2453 (snd_soc_read(codec, reg2) >> shift) & mask;
2454 if (invert)
2455 ucontrol->value.integer.value[1] =
2456 max - ucontrol->value.integer.value[1];
2459 return 0;
2461 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2464 * snd_soc_put_volsw - single mixer put callback
2465 * @kcontrol: mixer control
2466 * @ucontrol: control element information
2468 * Callback to set the value of a single mixer control, or a double mixer
2469 * control that spans 2 registers.
2471 * Returns 0 for success.
2473 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2474 struct snd_ctl_elem_value *ucontrol)
2476 struct soc_mixer_control *mc =
2477 (struct soc_mixer_control *)kcontrol->private_value;
2478 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2479 unsigned int reg = mc->reg;
2480 unsigned int reg2 = mc->rreg;
2481 unsigned int shift = mc->shift;
2482 unsigned int rshift = mc->rshift;
2483 int max = mc->max;
2484 unsigned int mask = (1 << fls(max)) - 1;
2485 unsigned int invert = mc->invert;
2486 int err;
2487 bool type_2r = 0;
2488 unsigned int val2 = 0;
2489 unsigned int val, val_mask;
2491 val = (ucontrol->value.integer.value[0] & mask);
2492 if (invert)
2493 val = max - val;
2494 val_mask = mask << shift;
2495 val = val << shift;
2496 if (snd_soc_volsw_is_stereo(mc)) {
2497 val2 = (ucontrol->value.integer.value[1] & mask);
2498 if (invert)
2499 val2 = max - val2;
2500 if (reg == reg2) {
2501 val_mask |= mask << rshift;
2502 val |= val2 << rshift;
2503 } else {
2504 val2 = val2 << shift;
2505 type_2r = 1;
2508 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2509 if (err < 0)
2510 return err;
2512 if (type_2r)
2513 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2515 return err;
2517 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2520 * snd_soc_info_volsw_s8 - signed mixer info callback
2521 * @kcontrol: mixer control
2522 * @uinfo: control element information
2524 * Callback to provide information about a signed mixer control.
2526 * Returns 0 for success.
2528 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2529 struct snd_ctl_elem_info *uinfo)
2531 struct soc_mixer_control *mc =
2532 (struct soc_mixer_control *)kcontrol->private_value;
2533 int platform_max;
2534 int min = mc->min;
2536 if (!mc->platform_max)
2537 mc->platform_max = mc->max;
2538 platform_max = mc->platform_max;
2540 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2541 uinfo->count = 2;
2542 uinfo->value.integer.min = 0;
2543 uinfo->value.integer.max = platform_max - min;
2544 return 0;
2546 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2549 * snd_soc_get_volsw_s8 - signed mixer get callback
2550 * @kcontrol: mixer control
2551 * @ucontrol: control element information
2553 * Callback to get the value of a signed mixer control.
2555 * Returns 0 for success.
2557 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2558 struct snd_ctl_elem_value *ucontrol)
2560 struct soc_mixer_control *mc =
2561 (struct soc_mixer_control *)kcontrol->private_value;
2562 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2563 unsigned int reg = mc->reg;
2564 int min = mc->min;
2565 int val = snd_soc_read(codec, reg);
2567 ucontrol->value.integer.value[0] =
2568 ((signed char)(val & 0xff))-min;
2569 ucontrol->value.integer.value[1] =
2570 ((signed char)((val >> 8) & 0xff))-min;
2571 return 0;
2573 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2576 * snd_soc_put_volsw_sgn - signed mixer put callback
2577 * @kcontrol: mixer control
2578 * @ucontrol: control element information
2580 * Callback to set the value of a signed mixer control.
2582 * Returns 0 for success.
2584 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2585 struct snd_ctl_elem_value *ucontrol)
2587 struct soc_mixer_control *mc =
2588 (struct soc_mixer_control *)kcontrol->private_value;
2589 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2590 unsigned int reg = mc->reg;
2591 int min = mc->min;
2592 unsigned int val;
2594 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2595 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2597 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2599 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2602 * snd_soc_limit_volume - Set new limit to an existing volume control.
2604 * @codec: where to look for the control
2605 * @name: Name of the control
2606 * @max: new maximum limit
2608 * Return 0 for success, else error.
2610 int snd_soc_limit_volume(struct snd_soc_codec *codec,
2611 const char *name, int max)
2613 struct snd_card *card = codec->card->snd_card;
2614 struct snd_kcontrol *kctl;
2615 struct soc_mixer_control *mc;
2616 int found = 0;
2617 int ret = -EINVAL;
2619 /* Sanity check for name and max */
2620 if (unlikely(!name || max <= 0))
2621 return -EINVAL;
2623 list_for_each_entry(kctl, &card->controls, list) {
2624 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2625 found = 1;
2626 break;
2629 if (found) {
2630 mc = (struct soc_mixer_control *)kctl->private_value;
2631 if (max <= mc->max) {
2632 mc->platform_max = max;
2633 ret = 0;
2636 return ret;
2638 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2641 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2642 * mixer info callback
2643 * @kcontrol: mixer control
2644 * @uinfo: control element information
2646 * Returns 0 for success.
2648 int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2649 struct snd_ctl_elem_info *uinfo)
2651 struct soc_mixer_control *mc =
2652 (struct soc_mixer_control *)kcontrol->private_value;
2653 int max = mc->max;
2654 int min = mc->min;
2656 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2657 uinfo->count = 2;
2658 uinfo->value.integer.min = 0;
2659 uinfo->value.integer.max = max-min;
2661 return 0;
2663 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2666 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2667 * mixer get callback
2668 * @kcontrol: mixer control
2669 * @uinfo: control element information
2671 * Returns 0 for success.
2673 int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2674 struct snd_ctl_elem_value *ucontrol)
2676 struct soc_mixer_control *mc =
2677 (struct soc_mixer_control *)kcontrol->private_value;
2678 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2679 unsigned int mask = (1<<mc->shift)-1;
2680 int min = mc->min;
2681 int val = snd_soc_read(codec, mc->reg) & mask;
2682 int valr = snd_soc_read(codec, mc->rreg) & mask;
2684 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2685 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
2686 return 0;
2688 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2691 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2692 * mixer put callback
2693 * @kcontrol: mixer control
2694 * @uinfo: control element information
2696 * Returns 0 for success.
2698 int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2699 struct snd_ctl_elem_value *ucontrol)
2701 struct soc_mixer_control *mc =
2702 (struct soc_mixer_control *)kcontrol->private_value;
2703 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2704 unsigned int mask = (1<<mc->shift)-1;
2705 int min = mc->min;
2706 int ret;
2707 unsigned int val, valr, oval, ovalr;
2709 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2710 val &= mask;
2711 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2712 valr &= mask;
2714 oval = snd_soc_read(codec, mc->reg) & mask;
2715 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2717 ret = 0;
2718 if (oval != val) {
2719 ret = snd_soc_write(codec, mc->reg, val);
2720 if (ret < 0)
2721 return ret;
2723 if (ovalr != valr) {
2724 ret = snd_soc_write(codec, mc->rreg, valr);
2725 if (ret < 0)
2726 return ret;
2729 return 0;
2731 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2734 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2735 * @dai: DAI
2736 * @clk_id: DAI specific clock ID
2737 * @freq: new clock frequency in Hz
2738 * @dir: new clock direction - input/output.
2740 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2742 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2743 unsigned int freq, int dir)
2745 if (dai->driver && dai->driver->ops->set_sysclk)
2746 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2747 else if (dai->codec && dai->codec->driver->set_sysclk)
2748 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
2749 freq, dir);
2750 else
2751 return -EINVAL;
2753 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2756 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2757 * @codec: CODEC
2758 * @clk_id: DAI specific clock ID
2759 * @source: Source for the clock
2760 * @freq: new clock frequency in Hz
2761 * @dir: new clock direction - input/output.
2763 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2765 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2766 int source, unsigned int freq, int dir)
2768 if (codec->driver->set_sysclk)
2769 return codec->driver->set_sysclk(codec, clk_id, source,
2770 freq, dir);
2771 else
2772 return -EINVAL;
2774 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2777 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2778 * @dai: DAI
2779 * @div_id: DAI specific clock divider ID
2780 * @div: new clock divisor.
2782 * Configures the clock dividers. This is used to derive the best DAI bit and
2783 * frame clocks from the system or master clock. It's best to set the DAI bit
2784 * and frame clocks as low as possible to save system power.
2786 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2787 int div_id, int div)
2789 if (dai->driver && dai->driver->ops->set_clkdiv)
2790 return dai->driver->ops->set_clkdiv(dai, div_id, div);
2791 else
2792 return -EINVAL;
2794 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2797 * snd_soc_dai_set_pll - configure DAI PLL.
2798 * @dai: DAI
2799 * @pll_id: DAI specific PLL ID
2800 * @source: DAI specific source for the PLL
2801 * @freq_in: PLL input clock frequency in Hz
2802 * @freq_out: requested PLL output clock frequency in Hz
2804 * Configures and enables PLL to generate output clock based on input clock.
2806 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2807 unsigned int freq_in, unsigned int freq_out)
2809 if (dai->driver && dai->driver->ops->set_pll)
2810 return dai->driver->ops->set_pll(dai, pll_id, source,
2811 freq_in, freq_out);
2812 else if (dai->codec && dai->codec->driver->set_pll)
2813 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2814 freq_in, freq_out);
2815 else
2816 return -EINVAL;
2818 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2821 * snd_soc_codec_set_pll - configure codec PLL.
2822 * @codec: CODEC
2823 * @pll_id: DAI specific PLL ID
2824 * @source: DAI specific source for the PLL
2825 * @freq_in: PLL input clock frequency in Hz
2826 * @freq_out: requested PLL output clock frequency in Hz
2828 * Configures and enables PLL to generate output clock based on input clock.
2830 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2831 unsigned int freq_in, unsigned int freq_out)
2833 if (codec->driver->set_pll)
2834 return codec->driver->set_pll(codec, pll_id, source,
2835 freq_in, freq_out);
2836 else
2837 return -EINVAL;
2839 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2842 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2843 * @dai: DAI
2844 * @fmt: SND_SOC_DAIFMT_ format value.
2846 * Configures the DAI hardware format and clocking.
2848 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2850 if (dai->driver && dai->driver->ops->set_fmt)
2851 return dai->driver->ops->set_fmt(dai, fmt);
2852 else
2853 return -EINVAL;
2855 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2858 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2859 * @dai: DAI
2860 * @tx_mask: bitmask representing active TX slots.
2861 * @rx_mask: bitmask representing active RX slots.
2862 * @slots: Number of slots in use.
2863 * @slot_width: Width in bits for each slot.
2865 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2866 * specific.
2868 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2869 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2871 if (dai->driver && dai->driver->ops->set_tdm_slot)
2872 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2873 slots, slot_width);
2874 else
2875 return -EINVAL;
2877 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2880 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2881 * @dai: DAI
2882 * @tx_num: how many TX channels
2883 * @tx_slot: pointer to an array which imply the TX slot number channel
2884 * 0~num-1 uses
2885 * @rx_num: how many RX channels
2886 * @rx_slot: pointer to an array which imply the RX slot number channel
2887 * 0~num-1 uses
2889 * configure the relationship between channel number and TDM slot number.
2891 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2892 unsigned int tx_num, unsigned int *tx_slot,
2893 unsigned int rx_num, unsigned int *rx_slot)
2895 if (dai->driver && dai->driver->ops->set_channel_map)
2896 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2897 rx_num, rx_slot);
2898 else
2899 return -EINVAL;
2901 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2904 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2905 * @dai: DAI
2906 * @tristate: tristate enable
2908 * Tristates the DAI so that others can use it.
2910 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2912 if (dai->driver && dai->driver->ops->set_tristate)
2913 return dai->driver->ops->set_tristate(dai, tristate);
2914 else
2915 return -EINVAL;
2917 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2920 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2921 * @dai: DAI
2922 * @mute: mute enable
2924 * Mutes the DAI DAC.
2926 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2928 if (dai->driver && dai->driver->ops->digital_mute)
2929 return dai->driver->ops->digital_mute(dai, mute);
2930 else
2931 return -EINVAL;
2933 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2936 * snd_soc_register_card - Register a card with the ASoC core
2938 * @card: Card to register
2941 int snd_soc_register_card(struct snd_soc_card *card)
2943 int i;
2945 if (!card->name || !card->dev)
2946 return -EINVAL;
2948 for (i = 0; i < card->num_links; i++) {
2949 struct snd_soc_dai_link *link = &card->dai_link[i];
2952 * Codec must be specified by 1 of name or OF node,
2953 * not both or neither.
2955 if (!!link->codec_name == !!link->codec_of_node) {
2956 dev_err(card->dev,
2957 "Neither/both codec name/of_node are set for %s\n",
2958 link->name);
2959 return -EINVAL;
2963 * Platform may be specified by either name or OF node, but
2964 * can be left unspecified, and a dummy platform will be used.
2966 if (link->platform_name && link->platform_of_node) {
2967 dev_err(card->dev,
2968 "Both platform name/of_node are set for %s\n", link->name);
2969 return -EINVAL;
2973 * CPU DAI must be specified by 1 of name or OF node,
2974 * not both or neither.
2976 if (!!link->cpu_dai_name == !!link->cpu_dai_of_node) {
2977 dev_err(card->dev,
2978 "Neither/both cpu_dai name/of_node are set for %s\n",
2979 link->name);
2980 return -EINVAL;
2984 dev_set_drvdata(card->dev, card);
2986 snd_soc_initialize_card_lists(card);
2988 soc_init_card_debugfs(card);
2990 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
2991 (card->num_links + card->num_aux_devs),
2992 GFP_KERNEL);
2993 if (card->rtd == NULL)
2994 return -ENOMEM;
2995 card->rtd_aux = &card->rtd[card->num_links];
2997 for (i = 0; i < card->num_links; i++)
2998 card->rtd[i].dai_link = &card->dai_link[i];
3000 INIT_LIST_HEAD(&card->list);
3001 INIT_LIST_HEAD(&card->dapm_dirty);
3002 card->instantiated = 0;
3003 mutex_init(&card->mutex);
3005 mutex_lock(&client_mutex);
3006 list_add(&card->list, &card_list);
3007 snd_soc_instantiate_cards();
3008 mutex_unlock(&client_mutex);
3010 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
3012 return 0;
3014 EXPORT_SYMBOL_GPL(snd_soc_register_card);
3017 * snd_soc_unregister_card - Unregister a card with the ASoC core
3019 * @card: Card to unregister
3022 int snd_soc_unregister_card(struct snd_soc_card *card)
3024 if (card->instantiated)
3025 soc_cleanup_card_resources(card);
3026 mutex_lock(&client_mutex);
3027 list_del(&card->list);
3028 mutex_unlock(&client_mutex);
3029 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3031 return 0;
3033 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3036 * Simplify DAI link configuration by removing ".-1" from device names
3037 * and sanitizing names.
3039 static char *fmt_single_name(struct device *dev, int *id)
3041 char *found, name[NAME_SIZE];
3042 int id1, id2;
3044 if (dev_name(dev) == NULL)
3045 return NULL;
3047 strlcpy(name, dev_name(dev), NAME_SIZE);
3049 /* are we a "%s.%d" name (platform and SPI components) */
3050 found = strstr(name, dev->driver->name);
3051 if (found) {
3052 /* get ID */
3053 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3055 /* discard ID from name if ID == -1 */
3056 if (*id == -1)
3057 found[strlen(dev->driver->name)] = '\0';
3060 } else {
3061 /* I2C component devices are named "bus-addr" */
3062 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3063 char tmp[NAME_SIZE];
3065 /* create unique ID number from I2C addr and bus */
3066 *id = ((id1 & 0xffff) << 16) + id2;
3068 /* sanitize component name for DAI link creation */
3069 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3070 strlcpy(name, tmp, NAME_SIZE);
3071 } else
3072 *id = 0;
3075 return kstrdup(name, GFP_KERNEL);
3079 * Simplify DAI link naming for single devices with multiple DAIs by removing
3080 * any ".-1" and using the DAI name (instead of device name).
3082 static inline char *fmt_multiple_name(struct device *dev,
3083 struct snd_soc_dai_driver *dai_drv)
3085 if (dai_drv->name == NULL) {
3086 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
3087 dev_name(dev));
3088 return NULL;
3091 return kstrdup(dai_drv->name, GFP_KERNEL);
3095 * snd_soc_register_dai - Register a DAI with the ASoC core
3097 * @dai: DAI to register
3099 int snd_soc_register_dai(struct device *dev,
3100 struct snd_soc_dai_driver *dai_drv)
3102 struct snd_soc_dai *dai;
3104 dev_dbg(dev, "dai register %s\n", dev_name(dev));
3106 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3107 if (dai == NULL)
3108 return -ENOMEM;
3110 /* create DAI component name */
3111 dai->name = fmt_single_name(dev, &dai->id);
3112 if (dai->name == NULL) {
3113 kfree(dai);
3114 return -ENOMEM;
3117 dai->dev = dev;
3118 dai->driver = dai_drv;
3119 if (!dai->driver->ops)
3120 dai->driver->ops = &null_dai_ops;
3122 mutex_lock(&client_mutex);
3123 list_add(&dai->list, &dai_list);
3124 snd_soc_instantiate_cards();
3125 mutex_unlock(&client_mutex);
3127 pr_debug("Registered DAI '%s'\n", dai->name);
3129 return 0;
3131 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3134 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3136 * @dai: DAI to unregister
3138 void snd_soc_unregister_dai(struct device *dev)
3140 struct snd_soc_dai *dai;
3142 list_for_each_entry(dai, &dai_list, list) {
3143 if (dev == dai->dev)
3144 goto found;
3146 return;
3148 found:
3149 mutex_lock(&client_mutex);
3150 list_del(&dai->list);
3151 mutex_unlock(&client_mutex);
3153 pr_debug("Unregistered DAI '%s'\n", dai->name);
3154 kfree(dai->name);
3155 kfree(dai);
3157 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3160 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3162 * @dai: Array of DAIs to register
3163 * @count: Number of DAIs
3165 int snd_soc_register_dais(struct device *dev,
3166 struct snd_soc_dai_driver *dai_drv, size_t count)
3168 struct snd_soc_dai *dai;
3169 int i, ret = 0;
3171 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
3173 for (i = 0; i < count; i++) {
3175 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3176 if (dai == NULL) {
3177 ret = -ENOMEM;
3178 goto err;
3181 /* create DAI component name */
3182 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3183 if (dai->name == NULL) {
3184 kfree(dai);
3185 ret = -EINVAL;
3186 goto err;
3189 dai->dev = dev;
3190 dai->driver = &dai_drv[i];
3191 if (dai->driver->id)
3192 dai->id = dai->driver->id;
3193 else
3194 dai->id = i;
3195 if (!dai->driver->ops)
3196 dai->driver->ops = &null_dai_ops;
3198 mutex_lock(&client_mutex);
3199 list_add(&dai->list, &dai_list);
3200 mutex_unlock(&client_mutex);
3202 pr_debug("Registered DAI '%s'\n", dai->name);
3205 mutex_lock(&client_mutex);
3206 snd_soc_instantiate_cards();
3207 mutex_unlock(&client_mutex);
3208 return 0;
3210 err:
3211 for (i--; i >= 0; i--)
3212 snd_soc_unregister_dai(dev);
3214 return ret;
3216 EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3219 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3221 * @dai: Array of DAIs to unregister
3222 * @count: Number of DAIs
3224 void snd_soc_unregister_dais(struct device *dev, size_t count)
3226 int i;
3228 for (i = 0; i < count; i++)
3229 snd_soc_unregister_dai(dev);
3231 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3234 * snd_soc_register_platform - Register a platform with the ASoC core
3236 * @platform: platform to register
3238 int snd_soc_register_platform(struct device *dev,
3239 struct snd_soc_platform_driver *platform_drv)
3241 struct snd_soc_platform *platform;
3243 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3245 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3246 if (platform == NULL)
3247 return -ENOMEM;
3249 /* create platform component name */
3250 platform->name = fmt_single_name(dev, &platform->id);
3251 if (platform->name == NULL) {
3252 kfree(platform);
3253 return -ENOMEM;
3256 platform->dev = dev;
3257 platform->driver = platform_drv;
3258 platform->dapm.dev = dev;
3259 platform->dapm.platform = platform;
3260 platform->dapm.stream_event = platform_drv->stream_event;
3262 mutex_lock(&client_mutex);
3263 list_add(&platform->list, &platform_list);
3264 snd_soc_instantiate_cards();
3265 mutex_unlock(&client_mutex);
3267 pr_debug("Registered platform '%s'\n", platform->name);
3269 return 0;
3271 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3274 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3276 * @platform: platform to unregister
3278 void snd_soc_unregister_platform(struct device *dev)
3280 struct snd_soc_platform *platform;
3282 list_for_each_entry(platform, &platform_list, list) {
3283 if (dev == platform->dev)
3284 goto found;
3286 return;
3288 found:
3289 mutex_lock(&client_mutex);
3290 list_del(&platform->list);
3291 mutex_unlock(&client_mutex);
3293 pr_debug("Unregistered platform '%s'\n", platform->name);
3294 kfree(platform->name);
3295 kfree(platform);
3297 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3299 static u64 codec_format_map[] = {
3300 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3301 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3302 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3303 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3304 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3305 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3306 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3307 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3308 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3309 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3310 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3311 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3312 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3313 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3314 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3315 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3318 /* Fix up the DAI formats for endianness: codecs don't actually see
3319 * the endianness of the data but we're using the CPU format
3320 * definitions which do need to include endianness so we ensure that
3321 * codec DAIs always have both big and little endian variants set.
3323 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3325 int i;
3327 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3328 if (stream->formats & codec_format_map[i])
3329 stream->formats |= codec_format_map[i];
3333 * snd_soc_register_codec - Register a codec with the ASoC core
3335 * @codec: codec to register
3337 int snd_soc_register_codec(struct device *dev,
3338 const struct snd_soc_codec_driver *codec_drv,
3339 struct snd_soc_dai_driver *dai_drv,
3340 int num_dai)
3342 size_t reg_size;
3343 struct snd_soc_codec *codec;
3344 int ret, i;
3346 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3348 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3349 if (codec == NULL)
3350 return -ENOMEM;
3352 /* create CODEC component name */
3353 codec->name = fmt_single_name(dev, &codec->id);
3354 if (codec->name == NULL) {
3355 kfree(codec);
3356 return -ENOMEM;
3359 if (codec_drv->compress_type)
3360 codec->compress_type = codec_drv->compress_type;
3361 else
3362 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3364 codec->write = codec_drv->write;
3365 codec->read = codec_drv->read;
3366 codec->volatile_register = codec_drv->volatile_register;
3367 codec->readable_register = codec_drv->readable_register;
3368 codec->writable_register = codec_drv->writable_register;
3369 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
3370 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3371 codec->dapm.dev = dev;
3372 codec->dapm.codec = codec;
3373 codec->dapm.seq_notifier = codec_drv->seq_notifier;
3374 codec->dapm.stream_event = codec_drv->stream_event;
3375 codec->dev = dev;
3376 codec->driver = codec_drv;
3377 codec->num_dai = num_dai;
3378 mutex_init(&codec->mutex);
3380 /* allocate CODEC register cache */
3381 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3382 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
3383 codec->reg_size = reg_size;
3384 /* it is necessary to make a copy of the default register cache
3385 * because in the case of using a compression type that requires
3386 * the default register cache to be marked as __devinitconst the
3387 * kernel might have freed the array by the time we initialize
3388 * the cache.
3390 if (codec_drv->reg_cache_default) {
3391 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3392 reg_size, GFP_KERNEL);
3393 if (!codec->reg_def_copy) {
3394 ret = -ENOMEM;
3395 goto fail;
3400 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3401 if (!codec->volatile_register)
3402 codec->volatile_register = snd_soc_default_volatile_register;
3403 if (!codec->readable_register)
3404 codec->readable_register = snd_soc_default_readable_register;
3405 if (!codec->writable_register)
3406 codec->writable_register = snd_soc_default_writable_register;
3409 for (i = 0; i < num_dai; i++) {
3410 fixup_codec_formats(&dai_drv[i].playback);
3411 fixup_codec_formats(&dai_drv[i].capture);
3414 /* register any DAIs */
3415 if (num_dai) {
3416 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3417 if (ret < 0)
3418 goto fail;
3421 mutex_lock(&client_mutex);
3422 list_add(&codec->list, &codec_list);
3423 snd_soc_instantiate_cards();
3424 mutex_unlock(&client_mutex);
3426 pr_debug("Registered codec '%s'\n", codec->name);
3427 return 0;
3429 fail:
3430 kfree(codec->reg_def_copy);
3431 codec->reg_def_copy = NULL;
3432 kfree(codec->name);
3433 kfree(codec);
3434 return ret;
3436 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3439 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3441 * @codec: codec to unregister
3443 void snd_soc_unregister_codec(struct device *dev)
3445 struct snd_soc_codec *codec;
3446 int i;
3448 list_for_each_entry(codec, &codec_list, list) {
3449 if (dev == codec->dev)
3450 goto found;
3452 return;
3454 found:
3455 if (codec->num_dai)
3456 for (i = 0; i < codec->num_dai; i++)
3457 snd_soc_unregister_dai(dev);
3459 mutex_lock(&client_mutex);
3460 list_del(&codec->list);
3461 mutex_unlock(&client_mutex);
3463 pr_debug("Unregistered codec '%s'\n", codec->name);
3465 snd_soc_cache_exit(codec);
3466 kfree(codec->reg_def_copy);
3467 kfree(codec->name);
3468 kfree(codec);
3470 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3472 /* Retrieve a card's name from device tree */
3473 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3474 const char *propname)
3476 struct device_node *np = card->dev->of_node;
3477 int ret;
3479 ret = of_property_read_string_index(np, propname, 0, &card->name);
3481 * EINVAL means the property does not exist. This is fine providing
3482 * card->name was previously set, which is checked later in
3483 * snd_soc_register_card.
3485 if (ret < 0 && ret != -EINVAL) {
3486 dev_err(card->dev,
3487 "Property '%s' could not be read: %d\n",
3488 propname, ret);
3489 return ret;
3492 return 0;
3494 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3496 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3497 const char *propname)
3499 struct device_node *np = card->dev->of_node;
3500 int num_routes;
3501 struct snd_soc_dapm_route *routes;
3502 int i, ret;
3504 num_routes = of_property_count_strings(np, propname);
3505 if (num_routes & 1) {
3506 dev_err(card->dev,
3507 "Property '%s's length is not even\n",
3508 propname);
3509 return -EINVAL;
3511 num_routes /= 2;
3512 if (!num_routes) {
3513 dev_err(card->dev,
3514 "Property '%s's length is zero\n",
3515 propname);
3516 return -EINVAL;
3519 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3520 GFP_KERNEL);
3521 if (!routes) {
3522 dev_err(card->dev,
3523 "Could not allocate DAPM route table\n");
3524 return -EINVAL;
3527 for (i = 0; i < num_routes; i++) {
3528 ret = of_property_read_string_index(np, propname,
3529 2 * i, &routes[i].sink);
3530 if (ret) {
3531 dev_err(card->dev,
3532 "Property '%s' index %d could not be read: %d\n",
3533 propname, 2 * i, ret);
3534 return -EINVAL;
3536 ret = of_property_read_string_index(np, propname,
3537 (2 * i) + 1, &routes[i].source);
3538 if (ret) {
3539 dev_err(card->dev,
3540 "Property '%s' index %d could not be read: %d\n",
3541 propname, (2 * i) + 1, ret);
3542 return -EINVAL;
3546 card->num_dapm_routes = num_routes;
3547 card->dapm_routes = routes;
3549 return 0;
3551 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3553 static int __init snd_soc_init(void)
3555 #ifdef CONFIG_DEBUG_FS
3556 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3557 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
3558 printk(KERN_WARNING
3559 "ASoC: Failed to create debugfs directory\n");
3560 snd_soc_debugfs_root = NULL;
3563 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
3564 &codec_list_fops))
3565 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3567 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
3568 &dai_list_fops))
3569 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
3571 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
3572 &platform_list_fops))
3573 pr_warn("ASoC: Failed to create platform list debugfs file\n");
3574 #endif
3576 snd_soc_util_init();
3578 return platform_driver_register(&soc_driver);
3580 module_init(snd_soc_init);
3582 static void __exit snd_soc_exit(void)
3584 snd_soc_util_exit();
3586 #ifdef CONFIG_DEBUG_FS
3587 debugfs_remove_recursive(snd_soc_debugfs_root);
3588 #endif
3589 platform_driver_unregister(&soc_driver);
3591 module_exit(snd_soc_exit);
3593 /* Module information */
3594 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3595 MODULE_DESCRIPTION("ALSA SoC Core");
3596 MODULE_LICENSE("GPL");
3597 MODULE_ALIAS("platform:soc-audio");