usb: musb: fix compilation breakage introduced by de47725
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / soc / soc-core.c
bloba5d3685a5d38049313391ddb8e174edd9c28a21b
1 /*
2 * soc-core.c -- ALSA SoC Audio Layer
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
18 * TODO:
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
29 #include <linux/pm.h>
30 #include <linux/bitops.h>
31 #include <linux/debugfs.h>
32 #include <linux/platform_device.h>
33 #include <linux/ctype.h>
34 #include <linux/slab.h>
35 #include <sound/ac97_codec.h>
36 #include <sound/core.h>
37 #include <sound/jack.h>
38 #include <sound/pcm.h>
39 #include <sound/pcm_params.h>
40 #include <sound/soc.h>
41 #include <sound/initval.h>
43 #define CREATE_TRACE_POINTS
44 #include <trace/events/asoc.h>
46 #define NAME_SIZE 32
48 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
50 #ifdef CONFIG_DEBUG_FS
51 struct dentry *snd_soc_debugfs_root;
52 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
53 #endif
55 static DEFINE_MUTEX(client_mutex);
56 static LIST_HEAD(card_list);
57 static LIST_HEAD(dai_list);
58 static LIST_HEAD(platform_list);
59 static LIST_HEAD(codec_list);
61 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
64 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
68 static int pmdown_time = 5000;
69 module_param(pmdown_time, int, 0);
70 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
72 /* returns the minimum number of bytes needed to represent
73 * a particular given value */
74 static int min_bytes_needed(unsigned long val)
76 int c = 0;
77 int i;
79 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
80 if (val & (1UL << i))
81 break;
82 c = (sizeof val * 8) - c;
83 if (!c || (c % 8))
84 c = (c + 8) / 8;
85 else
86 c /= 8;
87 return c;
90 /* fill buf which is 'len' bytes with a formatted
91 * string of the form 'reg: value\n' */
92 static int format_register_str(struct snd_soc_codec *codec,
93 unsigned int reg, char *buf, size_t len)
95 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
96 int regsize = codec->driver->reg_word_size * 2;
97 int ret;
98 char tmpbuf[len + 1];
99 char regbuf[regsize + 1];
101 /* since tmpbuf is allocated on the stack, warn the callers if they
102 * try to abuse this function */
103 WARN_ON(len > 63);
105 /* +2 for ': ' and + 1 for '\n' */
106 if (wordsize + regsize + 2 + 1 != len)
107 return -EINVAL;
109 ret = snd_soc_read(codec, reg);
110 if (ret < 0) {
111 memset(regbuf, 'X', regsize);
112 regbuf[regsize] = '\0';
113 } else {
114 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
117 /* prepare the buffer */
118 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
119 /* copy it back to the caller without the '\0' */
120 memcpy(buf, tmpbuf, len);
122 return 0;
125 /* codec register dump */
126 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
127 size_t count, loff_t pos)
129 int i, step = 1;
130 int wordsize, regsize;
131 int len;
132 size_t total = 0;
133 loff_t p = 0;
135 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
136 regsize = codec->driver->reg_word_size * 2;
138 len = wordsize + regsize + 2 + 1;
140 if (!codec->driver->reg_cache_size)
141 return 0;
143 if (codec->driver->reg_cache_step)
144 step = codec->driver->reg_cache_step;
146 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
147 if (!snd_soc_codec_readable_register(codec, i))
148 continue;
149 if (codec->driver->display_register) {
150 count += codec->driver->display_register(codec, buf + count,
151 PAGE_SIZE - count, i);
152 } else {
153 /* only support larger than PAGE_SIZE bytes debugfs
154 * entries for the default case */
155 if (p >= pos) {
156 if (total + len >= count - 1)
157 break;
158 format_register_str(codec, i, buf + total, len);
159 total += len;
161 p += len;
165 total = min(total, count - 1);
167 return total;
170 static ssize_t codec_reg_show(struct device *dev,
171 struct device_attribute *attr, char *buf)
173 struct snd_soc_pcm_runtime *rtd =
174 container_of(dev, struct snd_soc_pcm_runtime, dev);
176 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
179 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
181 static ssize_t pmdown_time_show(struct device *dev,
182 struct device_attribute *attr, char *buf)
184 struct snd_soc_pcm_runtime *rtd =
185 container_of(dev, struct snd_soc_pcm_runtime, dev);
187 return sprintf(buf, "%ld\n", rtd->pmdown_time);
190 static ssize_t pmdown_time_set(struct device *dev,
191 struct device_attribute *attr,
192 const char *buf, size_t count)
194 struct snd_soc_pcm_runtime *rtd =
195 container_of(dev, struct snd_soc_pcm_runtime, dev);
196 int ret;
198 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
199 if (ret)
200 return ret;
202 return count;
205 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
207 #ifdef CONFIG_DEBUG_FS
208 static int codec_reg_open_file(struct inode *inode, struct file *file)
210 file->private_data = inode->i_private;
211 return 0;
214 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
215 size_t count, loff_t *ppos)
217 ssize_t ret;
218 struct snd_soc_codec *codec = file->private_data;
219 char *buf;
221 if (*ppos < 0 || !count)
222 return -EINVAL;
224 buf = kmalloc(count, GFP_KERNEL);
225 if (!buf)
226 return -ENOMEM;
228 ret = soc_codec_reg_show(codec, buf, count, *ppos);
229 if (ret >= 0) {
230 if (copy_to_user(user_buf, buf, ret)) {
231 kfree(buf);
232 return -EFAULT;
234 *ppos += ret;
237 kfree(buf);
238 return ret;
241 static ssize_t codec_reg_write_file(struct file *file,
242 const char __user *user_buf, size_t count, loff_t *ppos)
244 char buf[32];
245 size_t buf_size;
246 char *start = buf;
247 unsigned long reg, value;
248 struct snd_soc_codec *codec = file->private_data;
250 buf_size = min(count, (sizeof(buf)-1));
251 if (copy_from_user(buf, user_buf, buf_size))
252 return -EFAULT;
253 buf[buf_size] = 0;
255 while (*start == ' ')
256 start++;
257 reg = simple_strtoul(start, &start, 16);
258 while (*start == ' ')
259 start++;
260 if (strict_strtoul(start, 16, &value))
261 return -EINVAL;
263 /* Userspace has been fiddling around behind the kernel's back */
264 add_taint(TAINT_USER);
266 snd_soc_write(codec, reg, value);
267 return buf_size;
270 static const struct file_operations codec_reg_fops = {
271 .open = codec_reg_open_file,
272 .read = codec_reg_read_file,
273 .write = codec_reg_write_file,
274 .llseek = default_llseek,
277 static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
279 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
281 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
282 debugfs_card_root);
283 if (!codec->debugfs_codec_root) {
284 printk(KERN_WARNING
285 "ASoC: Failed to create codec debugfs directory\n");
286 return;
289 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
290 &codec->cache_sync);
291 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
292 &codec->cache_only);
294 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
295 codec->debugfs_codec_root,
296 codec, &codec_reg_fops);
297 if (!codec->debugfs_reg)
298 printk(KERN_WARNING
299 "ASoC: Failed to create codec register debugfs file\n");
301 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
304 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
306 debugfs_remove_recursive(codec->debugfs_codec_root);
309 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
310 size_t count, loff_t *ppos)
312 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
313 ssize_t len, ret = 0;
314 struct snd_soc_codec *codec;
316 if (!buf)
317 return -ENOMEM;
319 list_for_each_entry(codec, &codec_list, list) {
320 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
321 codec->name);
322 if (len >= 0)
323 ret += len;
324 if (ret > PAGE_SIZE) {
325 ret = PAGE_SIZE;
326 break;
330 if (ret >= 0)
331 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
333 kfree(buf);
335 return ret;
338 static const struct file_operations codec_list_fops = {
339 .read = codec_list_read_file,
340 .llseek = default_llseek,/* read accesses f_pos */
343 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
344 size_t count, loff_t *ppos)
346 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
347 ssize_t len, ret = 0;
348 struct snd_soc_dai *dai;
350 if (!buf)
351 return -ENOMEM;
353 list_for_each_entry(dai, &dai_list, list) {
354 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
355 if (len >= 0)
356 ret += len;
357 if (ret > PAGE_SIZE) {
358 ret = PAGE_SIZE;
359 break;
363 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
365 kfree(buf);
367 return ret;
370 static const struct file_operations dai_list_fops = {
371 .read = dai_list_read_file,
372 .llseek = default_llseek,/* read accesses f_pos */
375 static ssize_t platform_list_read_file(struct file *file,
376 char __user *user_buf,
377 size_t count, loff_t *ppos)
379 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
380 ssize_t len, ret = 0;
381 struct snd_soc_platform *platform;
383 if (!buf)
384 return -ENOMEM;
386 list_for_each_entry(platform, &platform_list, list) {
387 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
388 platform->name);
389 if (len >= 0)
390 ret += len;
391 if (ret > PAGE_SIZE) {
392 ret = PAGE_SIZE;
393 break;
397 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
399 kfree(buf);
401 return ret;
404 static const struct file_operations platform_list_fops = {
405 .read = platform_list_read_file,
406 .llseek = default_llseek,/* read accesses f_pos */
409 static void soc_init_card_debugfs(struct snd_soc_card *card)
411 card->debugfs_card_root = debugfs_create_dir(card->name,
412 snd_soc_debugfs_root);
413 if (!card->debugfs_card_root) {
414 dev_warn(card->dev,
415 "ASoC: Failed to create codec debugfs directory\n");
416 return;
419 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
420 card->debugfs_card_root,
421 &card->pop_time);
422 if (!card->debugfs_pop_time)
423 dev_warn(card->dev,
424 "Failed to create pop time debugfs file\n");
427 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
429 debugfs_remove_recursive(card->debugfs_card_root);
432 #else
434 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
438 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
442 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
446 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
449 #endif
451 #ifdef CONFIG_SND_SOC_AC97_BUS
452 /* unregister ac97 codec */
453 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
455 if (codec->ac97->dev.bus)
456 device_unregister(&codec->ac97->dev);
457 return 0;
460 /* stop no dev release warning */
461 static void soc_ac97_device_release(struct device *dev){}
463 /* register ac97 codec to bus */
464 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
466 int err;
468 codec->ac97->dev.bus = &ac97_bus_type;
469 codec->ac97->dev.parent = codec->card->dev;
470 codec->ac97->dev.release = soc_ac97_device_release;
472 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
473 codec->card->snd_card->number, 0, codec->name);
474 err = device_register(&codec->ac97->dev);
475 if (err < 0) {
476 snd_printk(KERN_ERR "Can't register ac97 bus\n");
477 codec->ac97->dev.bus = NULL;
478 return err;
480 return 0;
482 #endif
484 #ifdef CONFIG_PM_SLEEP
485 /* powers down audio subsystem for suspend */
486 int snd_soc_suspend(struct device *dev)
488 struct snd_soc_card *card = dev_get_drvdata(dev);
489 struct snd_soc_codec *codec;
490 int i;
492 /* If the initialization of this soc device failed, there is no codec
493 * associated with it. Just bail out in this case.
495 if (list_empty(&card->codec_dev_list))
496 return 0;
498 /* Due to the resume being scheduled into a workqueue we could
499 * suspend before that's finished - wait for it to complete.
501 snd_power_lock(card->snd_card);
502 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
503 snd_power_unlock(card->snd_card);
505 /* we're going to block userspace touching us until resume completes */
506 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
508 /* mute any active DACs */
509 for (i = 0; i < card->num_rtd; i++) {
510 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
511 struct snd_soc_dai_driver *drv = dai->driver;
513 if (card->rtd[i].dai_link->ignore_suspend)
514 continue;
516 if (drv->ops->digital_mute && dai->playback_active)
517 drv->ops->digital_mute(dai, 1);
520 /* suspend all pcms */
521 for (i = 0; i < card->num_rtd; i++) {
522 if (card->rtd[i].dai_link->ignore_suspend)
523 continue;
525 snd_pcm_suspend_all(card->rtd[i].pcm);
528 if (card->suspend_pre)
529 card->suspend_pre(card);
531 for (i = 0; i < card->num_rtd; i++) {
532 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
533 struct snd_soc_platform *platform = card->rtd[i].platform;
535 if (card->rtd[i].dai_link->ignore_suspend)
536 continue;
538 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
539 cpu_dai->driver->suspend(cpu_dai);
540 if (platform->driver->suspend && !platform->suspended) {
541 platform->driver->suspend(cpu_dai);
542 platform->suspended = 1;
546 /* close any waiting streams and save state */
547 for (i = 0; i < card->num_rtd; i++) {
548 flush_delayed_work_sync(&card->rtd[i].delayed_work);
549 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
552 for (i = 0; i < card->num_rtd; i++) {
553 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
555 if (card->rtd[i].dai_link->ignore_suspend)
556 continue;
558 if (driver->playback.stream_name != NULL)
559 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
560 SND_SOC_DAPM_STREAM_SUSPEND);
562 if (driver->capture.stream_name != NULL)
563 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
564 SND_SOC_DAPM_STREAM_SUSPEND);
567 /* suspend all CODECs */
568 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
569 /* If there are paths active then the CODEC will be held with
570 * bias _ON and should not be suspended. */
571 if (!codec->suspended && codec->driver->suspend) {
572 switch (codec->dapm.bias_level) {
573 case SND_SOC_BIAS_STANDBY:
574 case SND_SOC_BIAS_OFF:
575 codec->driver->suspend(codec, PMSG_SUSPEND);
576 codec->suspended = 1;
577 codec->cache_sync = 1;
578 break;
579 default:
580 dev_dbg(codec->dev, "CODEC is on over suspend\n");
581 break;
586 for (i = 0; i < card->num_rtd; i++) {
587 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
589 if (card->rtd[i].dai_link->ignore_suspend)
590 continue;
592 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
593 cpu_dai->driver->suspend(cpu_dai);
596 if (card->suspend_post)
597 card->suspend_post(card);
599 return 0;
601 EXPORT_SYMBOL_GPL(snd_soc_suspend);
603 /* deferred resume work, so resume can complete before we finished
604 * setting our codec back up, which can be very slow on I2C
606 static void soc_resume_deferred(struct work_struct *work)
608 struct snd_soc_card *card =
609 container_of(work, struct snd_soc_card, deferred_resume_work);
610 struct snd_soc_codec *codec;
611 int i;
613 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
614 * so userspace apps are blocked from touching us
617 dev_dbg(card->dev, "starting resume work\n");
619 /* Bring us up into D2 so that DAPM starts enabling things */
620 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
622 if (card->resume_pre)
623 card->resume_pre(card);
625 /* resume AC97 DAIs */
626 for (i = 0; i < card->num_rtd; i++) {
627 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
629 if (card->rtd[i].dai_link->ignore_suspend)
630 continue;
632 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
633 cpu_dai->driver->resume(cpu_dai);
636 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
637 /* If the CODEC was idle over suspend then it will have been
638 * left with bias OFF or STANDBY and suspended so we must now
639 * resume. Otherwise the suspend was suppressed.
641 if (codec->driver->resume && codec->suspended) {
642 switch (codec->dapm.bias_level) {
643 case SND_SOC_BIAS_STANDBY:
644 case SND_SOC_BIAS_OFF:
645 codec->driver->resume(codec);
646 codec->suspended = 0;
647 break;
648 default:
649 dev_dbg(codec->dev, "CODEC was on over suspend\n");
650 break;
655 for (i = 0; i < card->num_rtd; i++) {
656 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
658 if (card->rtd[i].dai_link->ignore_suspend)
659 continue;
661 if (driver->playback.stream_name != NULL)
662 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
663 SND_SOC_DAPM_STREAM_RESUME);
665 if (driver->capture.stream_name != NULL)
666 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
667 SND_SOC_DAPM_STREAM_RESUME);
670 /* unmute any active DACs */
671 for (i = 0; i < card->num_rtd; i++) {
672 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
673 struct snd_soc_dai_driver *drv = dai->driver;
675 if (card->rtd[i].dai_link->ignore_suspend)
676 continue;
678 if (drv->ops->digital_mute && dai->playback_active)
679 drv->ops->digital_mute(dai, 0);
682 for (i = 0; i < card->num_rtd; i++) {
683 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
684 struct snd_soc_platform *platform = card->rtd[i].platform;
686 if (card->rtd[i].dai_link->ignore_suspend)
687 continue;
689 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
690 cpu_dai->driver->resume(cpu_dai);
691 if (platform->driver->resume && platform->suspended) {
692 platform->driver->resume(cpu_dai);
693 platform->suspended = 0;
697 if (card->resume_post)
698 card->resume_post(card);
700 dev_dbg(card->dev, "resume work completed\n");
702 /* userspace can access us now we are back as we were before */
703 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
706 /* powers up audio subsystem after a suspend */
707 int snd_soc_resume(struct device *dev)
709 struct snd_soc_card *card = dev_get_drvdata(dev);
710 int i, ac97_control = 0;
712 /* AC97 devices might have other drivers hanging off them so
713 * need to resume immediately. Other drivers don't have that
714 * problem and may take a substantial amount of time to resume
715 * due to I/O costs and anti-pop so handle them out of line.
717 for (i = 0; i < card->num_rtd; i++) {
718 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
719 ac97_control |= cpu_dai->driver->ac97_control;
721 if (ac97_control) {
722 dev_dbg(dev, "Resuming AC97 immediately\n");
723 soc_resume_deferred(&card->deferred_resume_work);
724 } else {
725 dev_dbg(dev, "Scheduling resume work\n");
726 if (!schedule_work(&card->deferred_resume_work))
727 dev_err(dev, "resume work item may be lost\n");
730 return 0;
732 EXPORT_SYMBOL_GPL(snd_soc_resume);
733 #else
734 #define snd_soc_suspend NULL
735 #define snd_soc_resume NULL
736 #endif
738 static struct snd_soc_dai_ops null_dai_ops = {
741 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
743 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
744 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
745 struct snd_soc_codec *codec;
746 struct snd_soc_platform *platform;
747 struct snd_soc_dai *codec_dai, *cpu_dai;
748 const char *platform_name;
750 if (rtd->complete)
751 return 1;
752 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
754 /* do we already have the CPU DAI for this link ? */
755 if (rtd->cpu_dai) {
756 goto find_codec;
758 /* no, then find CPU DAI from registered DAIs*/
759 list_for_each_entry(cpu_dai, &dai_list, list) {
760 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
761 rtd->cpu_dai = cpu_dai;
762 goto find_codec;
765 dev_dbg(card->dev, "CPU DAI %s not registered\n",
766 dai_link->cpu_dai_name);
768 find_codec:
769 /* do we already have the CODEC for this link ? */
770 if (rtd->codec) {
771 goto find_platform;
774 /* no, then find CODEC from registered CODECs*/
775 list_for_each_entry(codec, &codec_list, list) {
776 if (!strcmp(codec->name, dai_link->codec_name)) {
777 rtd->codec = codec;
779 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
780 list_for_each_entry(codec_dai, &dai_list, list) {
781 if (codec->dev == codec_dai->dev &&
782 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
783 rtd->codec_dai = codec_dai;
784 goto find_platform;
787 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
788 dai_link->codec_dai_name);
790 goto find_platform;
793 dev_dbg(card->dev, "CODEC %s not registered\n",
794 dai_link->codec_name);
796 find_platform:
797 /* do we need a platform? */
798 if (rtd->platform)
799 goto out;
801 /* if there's no platform we match on the empty platform */
802 platform_name = dai_link->platform_name;
803 if (!platform_name)
804 platform_name = "snd-soc-dummy";
806 /* no, then find one from the set of registered platforms */
807 list_for_each_entry(platform, &platform_list, list) {
808 if (!strcmp(platform->name, platform_name)) {
809 rtd->platform = platform;
810 goto out;
814 dev_dbg(card->dev, "platform %s not registered\n",
815 dai_link->platform_name);
816 return 0;
818 out:
819 /* mark rtd as complete if we found all 4 of our client devices */
820 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
821 rtd->complete = 1;
822 card->num_rtd++;
824 return 1;
827 static void soc_remove_codec(struct snd_soc_codec *codec)
829 int err;
831 if (codec->driver->remove) {
832 err = codec->driver->remove(codec);
833 if (err < 0)
834 dev_err(codec->dev,
835 "asoc: failed to remove %s: %d\n",
836 codec->name, err);
839 /* Make sure all DAPM widgets are freed */
840 snd_soc_dapm_free(&codec->dapm);
842 soc_cleanup_codec_debugfs(codec);
843 codec->probed = 0;
844 list_del(&codec->card_list);
845 module_put(codec->dev->driver->owner);
848 static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
850 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
851 struct snd_soc_codec *codec = rtd->codec;
852 struct snd_soc_platform *platform = rtd->platform;
853 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
854 int err;
856 /* unregister the rtd device */
857 if (rtd->dev_registered) {
858 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
859 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
860 device_unregister(&rtd->dev);
861 rtd->dev_registered = 0;
864 /* remove the CODEC DAI */
865 if (codec_dai && codec_dai->probed &&
866 codec_dai->driver->remove_order == order) {
867 if (codec_dai->driver->remove) {
868 err = codec_dai->driver->remove(codec_dai);
869 if (err < 0)
870 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
872 codec_dai->probed = 0;
873 list_del(&codec_dai->card_list);
876 /* remove the platform */
877 if (platform && platform->probed &&
878 platform->driver->remove_order == order) {
879 if (platform->driver->remove) {
880 err = platform->driver->remove(platform);
881 if (err < 0)
882 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
884 platform->probed = 0;
885 list_del(&platform->card_list);
886 module_put(platform->dev->driver->owner);
889 /* remove the CODEC */
890 if (codec && codec->probed &&
891 codec->driver->remove_order == order)
892 soc_remove_codec(codec);
894 /* remove the cpu_dai */
895 if (cpu_dai && cpu_dai->probed &&
896 cpu_dai->driver->remove_order == order) {
897 if (cpu_dai->driver->remove) {
898 err = cpu_dai->driver->remove(cpu_dai);
899 if (err < 0)
900 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
902 cpu_dai->probed = 0;
903 list_del(&cpu_dai->card_list);
904 module_put(cpu_dai->dev->driver->owner);
908 static void soc_remove_dai_links(struct snd_soc_card *card)
910 int dai, order;
912 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
913 order++) {
914 for (dai = 0; dai < card->num_rtd; dai++)
915 soc_remove_dai_link(card, dai, order);
917 card->num_rtd = 0;
920 static void soc_set_name_prefix(struct snd_soc_card *card,
921 struct snd_soc_codec *codec)
923 int i;
925 if (card->codec_conf == NULL)
926 return;
928 for (i = 0; i < card->num_configs; i++) {
929 struct snd_soc_codec_conf *map = &card->codec_conf[i];
930 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
931 codec->name_prefix = map->name_prefix;
932 break;
937 static int soc_probe_codec(struct snd_soc_card *card,
938 struct snd_soc_codec *codec)
940 int ret = 0;
941 const struct snd_soc_codec_driver *driver = codec->driver;
943 codec->card = card;
944 codec->dapm.card = card;
945 soc_set_name_prefix(card, codec);
947 if (!try_module_get(codec->dev->driver->owner))
948 return -ENODEV;
950 soc_init_codec_debugfs(codec);
952 if (driver->dapm_widgets)
953 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
954 driver->num_dapm_widgets);
956 codec->dapm.idle_bias_off = driver->idle_bias_off;
958 if (driver->probe) {
959 ret = driver->probe(codec);
960 if (ret < 0) {
961 dev_err(codec->dev,
962 "asoc: failed to probe CODEC %s: %d\n",
963 codec->name, ret);
964 goto err_probe;
968 if (driver->controls)
969 snd_soc_add_controls(codec, driver->controls,
970 driver->num_controls);
971 if (driver->dapm_routes)
972 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
973 driver->num_dapm_routes);
975 /* mark codec as probed and add to card codec list */
976 codec->probed = 1;
977 list_add(&codec->card_list, &card->codec_dev_list);
978 list_add(&codec->dapm.list, &card->dapm_list);
980 return 0;
982 err_probe:
983 soc_cleanup_codec_debugfs(codec);
984 module_put(codec->dev->driver->owner);
986 return ret;
989 static int soc_probe_platform(struct snd_soc_card *card,
990 struct snd_soc_platform *platform)
992 int ret = 0;
993 const struct snd_soc_platform_driver *driver = platform->driver;
995 platform->card = card;
996 platform->dapm.card = card;
998 if (!try_module_get(platform->dev->driver->owner))
999 return -ENODEV;
1001 if (driver->dapm_widgets)
1002 snd_soc_dapm_new_controls(&platform->dapm,
1003 driver->dapm_widgets, driver->num_dapm_widgets);
1005 if (driver->probe) {
1006 ret = driver->probe(platform);
1007 if (ret < 0) {
1008 dev_err(platform->dev,
1009 "asoc: failed to probe platform %s: %d\n",
1010 platform->name, ret);
1011 goto err_probe;
1015 if (driver->controls)
1016 snd_soc_add_platform_controls(platform, driver->controls,
1017 driver->num_controls);
1018 if (driver->dapm_routes)
1019 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1020 driver->num_dapm_routes);
1022 /* mark platform as probed and add to card platform list */
1023 platform->probed = 1;
1024 list_add(&platform->card_list, &card->platform_dev_list);
1025 list_add(&platform->dapm.list, &card->dapm_list);
1027 return 0;
1029 err_probe:
1030 module_put(platform->dev->driver->owner);
1032 return ret;
1035 static void rtd_release(struct device *dev) {}
1037 static int soc_post_component_init(struct snd_soc_card *card,
1038 struct snd_soc_codec *codec,
1039 int num, int dailess)
1041 struct snd_soc_dai_link *dai_link = NULL;
1042 struct snd_soc_aux_dev *aux_dev = NULL;
1043 struct snd_soc_pcm_runtime *rtd;
1044 const char *temp, *name;
1045 int ret = 0;
1047 if (!dailess) {
1048 dai_link = &card->dai_link[num];
1049 rtd = &card->rtd[num];
1050 name = dai_link->name;
1051 } else {
1052 aux_dev = &card->aux_dev[num];
1053 rtd = &card->rtd_aux[num];
1054 name = aux_dev->name;
1056 rtd->card = card;
1058 /* Make sure all DAPM widgets are instantiated */
1059 snd_soc_dapm_new_widgets(&codec->dapm);
1061 /* machine controls, routes and widgets are not prefixed */
1062 temp = codec->name_prefix;
1063 codec->name_prefix = NULL;
1065 /* do machine specific initialization */
1066 if (!dailess && dai_link->init)
1067 ret = dai_link->init(rtd);
1068 else if (dailess && aux_dev->init)
1069 ret = aux_dev->init(&codec->dapm);
1070 if (ret < 0) {
1071 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1072 return ret;
1074 codec->name_prefix = temp;
1076 /* register the rtd device */
1077 rtd->codec = codec;
1078 rtd->dev.parent = card->dev;
1079 rtd->dev.release = rtd_release;
1080 rtd->dev.init_name = name;
1081 mutex_init(&rtd->pcm_mutex);
1082 ret = device_register(&rtd->dev);
1083 if (ret < 0) {
1084 dev_err(card->dev,
1085 "asoc: failed to register runtime device: %d\n", ret);
1086 return ret;
1088 rtd->dev_registered = 1;
1090 /* add DAPM sysfs entries for this codec */
1091 ret = snd_soc_dapm_sys_add(&rtd->dev);
1092 if (ret < 0)
1093 dev_err(codec->dev,
1094 "asoc: failed to add codec dapm sysfs entries: %d\n",
1095 ret);
1097 /* add codec sysfs entries */
1098 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1099 if (ret < 0)
1100 dev_err(codec->dev,
1101 "asoc: failed to add codec sysfs files: %d\n", ret);
1103 return 0;
1106 static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
1108 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1109 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1110 struct snd_soc_codec *codec = rtd->codec;
1111 struct snd_soc_platform *platform = rtd->platform;
1112 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1113 int ret;
1115 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1116 card->name, num, order);
1118 /* config components */
1119 codec_dai->codec = codec;
1120 cpu_dai->platform = platform;
1121 codec_dai->card = card;
1122 cpu_dai->card = card;
1124 /* set default power off timeout */
1125 rtd->pmdown_time = pmdown_time;
1127 /* probe the cpu_dai */
1128 if (!cpu_dai->probed &&
1129 cpu_dai->driver->probe_order == order) {
1130 if (!try_module_get(cpu_dai->dev->driver->owner))
1131 return -ENODEV;
1133 if (cpu_dai->driver->probe) {
1134 ret = cpu_dai->driver->probe(cpu_dai);
1135 if (ret < 0) {
1136 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1137 cpu_dai->name);
1138 module_put(cpu_dai->dev->driver->owner);
1139 return ret;
1142 cpu_dai->probed = 1;
1143 /* mark cpu_dai as probed and add to card dai list */
1144 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1147 /* probe the CODEC */
1148 if (!codec->probed &&
1149 codec->driver->probe_order == order) {
1150 ret = soc_probe_codec(card, codec);
1151 if (ret < 0)
1152 return ret;
1155 /* probe the platform */
1156 if (!platform->probed &&
1157 platform->driver->probe_order == order) {
1158 ret = soc_probe_platform(card, platform);
1159 if (ret < 0)
1160 return ret;
1163 /* probe the CODEC DAI */
1164 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1165 if (codec_dai->driver->probe) {
1166 ret = codec_dai->driver->probe(codec_dai);
1167 if (ret < 0) {
1168 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1169 codec_dai->name);
1170 return ret;
1174 /* mark codec_dai as probed and add to card dai list */
1175 codec_dai->probed = 1;
1176 list_add(&codec_dai->card_list, &card->dai_dev_list);
1179 /* complete DAI probe during last probe */
1180 if (order != SND_SOC_COMP_ORDER_LAST)
1181 return 0;
1183 ret = soc_post_component_init(card, codec, num, 0);
1184 if (ret)
1185 return ret;
1187 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1188 if (ret < 0)
1189 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1191 /* create the pcm */
1192 ret = soc_new_pcm(rtd, num);
1193 if (ret < 0) {
1194 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1195 return ret;
1198 /* add platform data for AC97 devices */
1199 if (rtd->codec_dai->driver->ac97_control)
1200 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1202 return 0;
1205 #ifdef CONFIG_SND_SOC_AC97_BUS
1206 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1208 int ret;
1210 /* Only instantiate AC97 if not already done by the adaptor
1211 * for the generic AC97 subsystem.
1213 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
1215 * It is possible that the AC97 device is already registered to
1216 * the device subsystem. This happens when the device is created
1217 * via snd_ac97_mixer(). Currently only SoC codec that does so
1218 * is the generic AC97 glue but others migh emerge.
1220 * In those cases we don't try to register the device again.
1222 if (!rtd->codec->ac97_created)
1223 return 0;
1225 ret = soc_ac97_dev_register(rtd->codec);
1226 if (ret < 0) {
1227 printk(KERN_ERR "asoc: AC97 device register failed\n");
1228 return ret;
1231 rtd->codec->ac97_registered = 1;
1233 return 0;
1236 static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1238 if (codec->ac97_registered) {
1239 soc_ac97_dev_unregister(codec);
1240 codec->ac97_registered = 0;
1243 #endif
1245 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1247 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1248 struct snd_soc_codec *codec;
1249 int ret = -ENODEV;
1251 /* find CODEC from registered CODECs*/
1252 list_for_each_entry(codec, &codec_list, list) {
1253 if (!strcmp(codec->name, aux_dev->codec_name)) {
1254 if (codec->probed) {
1255 dev_err(codec->dev,
1256 "asoc: codec already probed");
1257 ret = -EBUSY;
1258 goto out;
1260 goto found;
1263 /* codec not found */
1264 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1265 goto out;
1267 found:
1268 ret = soc_probe_codec(card, codec);
1269 if (ret < 0)
1270 return ret;
1272 ret = soc_post_component_init(card, codec, num, 1);
1274 out:
1275 return ret;
1278 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1280 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1281 struct snd_soc_codec *codec = rtd->codec;
1283 /* unregister the rtd device */
1284 if (rtd->dev_registered) {
1285 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1286 device_unregister(&rtd->dev);
1287 rtd->dev_registered = 0;
1290 if (codec && codec->probed)
1291 soc_remove_codec(codec);
1294 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1295 enum snd_soc_compress_type compress_type)
1297 int ret;
1299 if (codec->cache_init)
1300 return 0;
1302 /* override the compress_type if necessary */
1303 if (compress_type && codec->compress_type != compress_type)
1304 codec->compress_type = compress_type;
1305 ret = snd_soc_cache_init(codec);
1306 if (ret < 0) {
1307 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1308 ret);
1309 return ret;
1311 codec->cache_init = 1;
1312 return 0;
1315 static void snd_soc_instantiate_card(struct snd_soc_card *card)
1317 struct snd_soc_codec *codec;
1318 struct snd_soc_codec_conf *codec_conf;
1319 enum snd_soc_compress_type compress_type;
1320 struct snd_soc_dai_link *dai_link;
1321 int ret, i, order;
1323 mutex_lock(&card->mutex);
1325 if (card->instantiated) {
1326 mutex_unlock(&card->mutex);
1327 return;
1330 /* bind DAIs */
1331 for (i = 0; i < card->num_links; i++)
1332 soc_bind_dai_link(card, i);
1334 /* bind completed ? */
1335 if (card->num_rtd != card->num_links) {
1336 mutex_unlock(&card->mutex);
1337 return;
1340 /* initialize the register cache for each available codec */
1341 list_for_each_entry(codec, &codec_list, list) {
1342 if (codec->cache_init)
1343 continue;
1344 /* by default we don't override the compress_type */
1345 compress_type = 0;
1346 /* check to see if we need to override the compress_type */
1347 for (i = 0; i < card->num_configs; ++i) {
1348 codec_conf = &card->codec_conf[i];
1349 if (!strcmp(codec->name, codec_conf->dev_name)) {
1350 compress_type = codec_conf->compress_type;
1351 if (compress_type && compress_type
1352 != codec->compress_type)
1353 break;
1356 ret = snd_soc_init_codec_cache(codec, compress_type);
1357 if (ret < 0) {
1358 mutex_unlock(&card->mutex);
1359 return;
1363 /* card bind complete so register a sound card */
1364 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1365 card->owner, 0, &card->snd_card);
1366 if (ret < 0) {
1367 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1368 card->name);
1369 mutex_unlock(&card->mutex);
1370 return;
1372 card->snd_card->dev = card->dev;
1374 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1375 card->dapm.dev = card->dev;
1376 card->dapm.card = card;
1377 list_add(&card->dapm.list, &card->dapm_list);
1379 #ifdef CONFIG_DEBUG_FS
1380 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1381 #endif
1383 #ifdef CONFIG_PM_SLEEP
1384 /* deferred resume work */
1385 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1386 #endif
1388 if (card->dapm_widgets)
1389 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1390 card->num_dapm_widgets);
1392 /* initialise the sound card only once */
1393 if (card->probe) {
1394 ret = card->probe(card);
1395 if (ret < 0)
1396 goto card_probe_error;
1399 /* early DAI link probe */
1400 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1401 order++) {
1402 for (i = 0; i < card->num_links; i++) {
1403 ret = soc_probe_dai_link(card, i, order);
1404 if (ret < 0) {
1405 pr_err("asoc: failed to instantiate card %s: %d\n",
1406 card->name, ret);
1407 goto probe_dai_err;
1412 for (i = 0; i < card->num_aux_devs; i++) {
1413 ret = soc_probe_aux_dev(card, i);
1414 if (ret < 0) {
1415 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1416 card->name, ret);
1417 goto probe_aux_dev_err;
1421 /* We should have a non-codec control add function but we don't */
1422 if (card->controls)
1423 snd_soc_add_controls(list_first_entry(&card->codec_dev_list,
1424 struct snd_soc_codec,
1425 card_list),
1426 card->controls,
1427 card->num_controls);
1429 if (card->dapm_routes)
1430 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1431 card->num_dapm_routes);
1433 snd_soc_dapm_new_widgets(&card->dapm);
1435 for (i = 0; i < card->num_links; i++) {
1436 dai_link = &card->dai_link[i];
1438 if (dai_link->dai_fmt) {
1439 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1440 dai_link->dai_fmt);
1441 if (ret != 0)
1442 dev_warn(card->rtd[i].codec_dai->dev,
1443 "Failed to set DAI format: %d\n",
1444 ret);
1446 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1447 dai_link->dai_fmt);
1448 if (ret != 0)
1449 dev_warn(card->rtd[i].cpu_dai->dev,
1450 "Failed to set DAI format: %d\n",
1451 ret);
1455 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1456 "%s", card->name);
1457 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1458 "%s", card->long_name ? card->long_name : card->name);
1459 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1460 "%s", card->driver_name ? card->driver_name : card->name);
1461 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1462 switch (card->snd_card->driver[i]) {
1463 case '_':
1464 case '-':
1465 case '\0':
1466 break;
1467 default:
1468 if (!isalnum(card->snd_card->driver[i]))
1469 card->snd_card->driver[i] = '_';
1470 break;
1474 if (card->late_probe) {
1475 ret = card->late_probe(card);
1476 if (ret < 0) {
1477 dev_err(card->dev, "%s late_probe() failed: %d\n",
1478 card->name, ret);
1479 goto probe_aux_dev_err;
1483 snd_soc_dapm_new_widgets(&card->dapm);
1485 ret = snd_card_register(card->snd_card);
1486 if (ret < 0) {
1487 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1488 goto probe_aux_dev_err;
1491 #ifdef CONFIG_SND_SOC_AC97_BUS
1492 /* register any AC97 codecs */
1493 for (i = 0; i < card->num_rtd; i++) {
1494 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1495 if (ret < 0) {
1496 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1497 while (--i >= 0)
1498 soc_unregister_ac97_dai_link(card->rtd[i].codec);
1499 goto probe_aux_dev_err;
1502 #endif
1504 card->instantiated = 1;
1505 snd_soc_dapm_sync(&card->dapm);
1506 mutex_unlock(&card->mutex);
1507 return;
1509 probe_aux_dev_err:
1510 for (i = 0; i < card->num_aux_devs; i++)
1511 soc_remove_aux_dev(card, i);
1513 probe_dai_err:
1514 soc_remove_dai_links(card);
1516 card_probe_error:
1517 if (card->remove)
1518 card->remove(card);
1520 snd_card_free(card->snd_card);
1522 mutex_unlock(&card->mutex);
1526 * Attempt to initialise any uninitialised cards. Must be called with
1527 * client_mutex.
1529 static void snd_soc_instantiate_cards(void)
1531 struct snd_soc_card *card;
1532 list_for_each_entry(card, &card_list, list)
1533 snd_soc_instantiate_card(card);
1536 /* probes a new socdev */
1537 static int soc_probe(struct platform_device *pdev)
1539 struct snd_soc_card *card = platform_get_drvdata(pdev);
1540 int ret = 0;
1543 * no card, so machine driver should be registering card
1544 * we should not be here in that case so ret error
1546 if (!card)
1547 return -EINVAL;
1549 /* Bodge while we unpick instantiation */
1550 card->dev = &pdev->dev;
1552 ret = snd_soc_register_card(card);
1553 if (ret != 0) {
1554 dev_err(&pdev->dev, "Failed to register card\n");
1555 return ret;
1558 return 0;
1561 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1563 int i;
1565 /* make sure any delayed work runs */
1566 for (i = 0; i < card->num_rtd; i++) {
1567 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1568 flush_delayed_work_sync(&rtd->delayed_work);
1571 /* remove auxiliary devices */
1572 for (i = 0; i < card->num_aux_devs; i++)
1573 soc_remove_aux_dev(card, i);
1575 /* remove and free each DAI */
1576 soc_remove_dai_links(card);
1578 soc_cleanup_card_debugfs(card);
1580 /* remove the card */
1581 if (card->remove)
1582 card->remove(card);
1584 snd_soc_dapm_free(&card->dapm);
1586 kfree(card->rtd);
1587 snd_card_free(card->snd_card);
1588 return 0;
1592 /* removes a socdev */
1593 static int soc_remove(struct platform_device *pdev)
1595 struct snd_soc_card *card = platform_get_drvdata(pdev);
1597 snd_soc_unregister_card(card);
1598 return 0;
1601 int snd_soc_poweroff(struct device *dev)
1603 struct snd_soc_card *card = dev_get_drvdata(dev);
1604 int i;
1606 if (!card->instantiated)
1607 return 0;
1609 /* Flush out pmdown_time work - we actually do want to run it
1610 * now, we're shutting down so no imminent restart. */
1611 for (i = 0; i < card->num_rtd; i++) {
1612 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1613 flush_delayed_work_sync(&rtd->delayed_work);
1616 snd_soc_dapm_shutdown(card);
1618 return 0;
1620 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1622 const struct dev_pm_ops snd_soc_pm_ops = {
1623 .suspend = snd_soc_suspend,
1624 .resume = snd_soc_resume,
1625 .poweroff = snd_soc_poweroff,
1627 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1629 /* ASoC platform driver */
1630 static struct platform_driver soc_driver = {
1631 .driver = {
1632 .name = "soc-audio",
1633 .owner = THIS_MODULE,
1634 .pm = &snd_soc_pm_ops,
1636 .probe = soc_probe,
1637 .remove = soc_remove,
1641 * snd_soc_codec_volatile_register: Report if a register is volatile.
1643 * @codec: CODEC to query.
1644 * @reg: Register to query.
1646 * Boolean function indiciating if a CODEC register is volatile.
1648 int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1649 unsigned int reg)
1651 if (codec->volatile_register)
1652 return codec->volatile_register(codec, reg);
1653 else
1654 return 0;
1656 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1659 * snd_soc_codec_readable_register: Report if a register is readable.
1661 * @codec: CODEC to query.
1662 * @reg: Register to query.
1664 * Boolean function indicating if a CODEC register is readable.
1666 int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1667 unsigned int reg)
1669 if (codec->readable_register)
1670 return codec->readable_register(codec, reg);
1671 else
1672 return 1;
1674 EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1677 * snd_soc_codec_writable_register: Report if a register is writable.
1679 * @codec: CODEC to query.
1680 * @reg: Register to query.
1682 * Boolean function indicating if a CODEC register is writable.
1684 int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1685 unsigned int reg)
1687 if (codec->writable_register)
1688 return codec->writable_register(codec, reg);
1689 else
1690 return 1;
1692 EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1694 int snd_soc_platform_read(struct snd_soc_platform *platform,
1695 unsigned int reg)
1697 unsigned int ret;
1699 if (!platform->driver->read) {
1700 dev_err(platform->dev, "platform has no read back\n");
1701 return -1;
1704 ret = platform->driver->read(platform, reg);
1705 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
1706 trace_snd_soc_preg_read(platform, reg, ret);
1708 return ret;
1710 EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1712 int snd_soc_platform_write(struct snd_soc_platform *platform,
1713 unsigned int reg, unsigned int val)
1715 if (!platform->driver->write) {
1716 dev_err(platform->dev, "platform has no write back\n");
1717 return -1;
1720 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
1721 trace_snd_soc_preg_write(platform, reg, val);
1722 return platform->driver->write(platform, reg, val);
1724 EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1727 * snd_soc_new_ac97_codec - initailise AC97 device
1728 * @codec: audio codec
1729 * @ops: AC97 bus operations
1730 * @num: AC97 codec number
1732 * Initialises AC97 codec resources for use by ad-hoc devices only.
1734 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1735 struct snd_ac97_bus_ops *ops, int num)
1737 mutex_lock(&codec->mutex);
1739 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1740 if (codec->ac97 == NULL) {
1741 mutex_unlock(&codec->mutex);
1742 return -ENOMEM;
1745 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1746 if (codec->ac97->bus == NULL) {
1747 kfree(codec->ac97);
1748 codec->ac97 = NULL;
1749 mutex_unlock(&codec->mutex);
1750 return -ENOMEM;
1753 codec->ac97->bus->ops = ops;
1754 codec->ac97->num = num;
1757 * Mark the AC97 device to be created by us. This way we ensure that the
1758 * device will be registered with the device subsystem later on.
1760 codec->ac97_created = 1;
1762 mutex_unlock(&codec->mutex);
1763 return 0;
1765 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1768 * snd_soc_free_ac97_codec - free AC97 codec device
1769 * @codec: audio codec
1771 * Frees AC97 codec device resources.
1773 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1775 mutex_lock(&codec->mutex);
1776 #ifdef CONFIG_SND_SOC_AC97_BUS
1777 soc_unregister_ac97_dai_link(codec);
1778 #endif
1779 kfree(codec->ac97->bus);
1780 kfree(codec->ac97);
1781 codec->ac97 = NULL;
1782 codec->ac97_created = 0;
1783 mutex_unlock(&codec->mutex);
1785 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1787 unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1789 unsigned int ret;
1791 ret = codec->read(codec, reg);
1792 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
1793 trace_snd_soc_reg_read(codec, reg, ret);
1795 return ret;
1797 EXPORT_SYMBOL_GPL(snd_soc_read);
1799 unsigned int snd_soc_write(struct snd_soc_codec *codec,
1800 unsigned int reg, unsigned int val)
1802 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
1803 trace_snd_soc_reg_write(codec, reg, val);
1804 return codec->write(codec, reg, val);
1806 EXPORT_SYMBOL_GPL(snd_soc_write);
1808 unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
1809 unsigned int reg, const void *data, size_t len)
1811 return codec->bulk_write_raw(codec, reg, data, len);
1813 EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
1816 * snd_soc_update_bits - update codec register bits
1817 * @codec: audio codec
1818 * @reg: codec register
1819 * @mask: register mask
1820 * @value: new value
1822 * Writes new register value.
1824 * Returns 1 for change, 0 for no change, or negative error code.
1826 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1827 unsigned int mask, unsigned int value)
1829 int change;
1830 unsigned int old, new;
1831 int ret;
1833 ret = snd_soc_read(codec, reg);
1834 if (ret < 0)
1835 return ret;
1837 old = ret;
1838 new = (old & ~mask) | (value & mask);
1839 change = old != new;
1840 if (change) {
1841 ret = snd_soc_write(codec, reg, new);
1842 if (ret < 0)
1843 return ret;
1846 return change;
1848 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1851 * snd_soc_update_bits_locked - update codec register bits
1852 * @codec: audio codec
1853 * @reg: codec register
1854 * @mask: register mask
1855 * @value: new value
1857 * Writes new register value, and takes the codec mutex.
1859 * Returns 1 for change else 0.
1861 int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1862 unsigned short reg, unsigned int mask,
1863 unsigned int value)
1865 int change;
1867 mutex_lock(&codec->mutex);
1868 change = snd_soc_update_bits(codec, reg, mask, value);
1869 mutex_unlock(&codec->mutex);
1871 return change;
1873 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
1876 * snd_soc_test_bits - test register for change
1877 * @codec: audio codec
1878 * @reg: codec register
1879 * @mask: register mask
1880 * @value: new value
1882 * Tests a register with a new value and checks if the new value is
1883 * different from the old value.
1885 * Returns 1 for change else 0.
1887 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1888 unsigned int mask, unsigned int value)
1890 int change;
1891 unsigned int old, new;
1893 old = snd_soc_read(codec, reg);
1894 new = (old & ~mask) | value;
1895 change = old != new;
1897 return change;
1899 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1902 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1903 * @substream: the pcm substream
1904 * @hw: the hardware parameters
1906 * Sets the substream runtime hardware parameters.
1908 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1909 const struct snd_pcm_hardware *hw)
1911 struct snd_pcm_runtime *runtime = substream->runtime;
1912 runtime->hw.info = hw->info;
1913 runtime->hw.formats = hw->formats;
1914 runtime->hw.period_bytes_min = hw->period_bytes_min;
1915 runtime->hw.period_bytes_max = hw->period_bytes_max;
1916 runtime->hw.periods_min = hw->periods_min;
1917 runtime->hw.periods_max = hw->periods_max;
1918 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1919 runtime->hw.fifo_size = hw->fifo_size;
1920 return 0;
1922 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1925 * snd_soc_cnew - create new control
1926 * @_template: control template
1927 * @data: control private data
1928 * @long_name: control long name
1929 * @prefix: control name prefix
1931 * Create a new mixer control from a template control.
1933 * Returns 0 for success, else error.
1935 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1936 void *data, char *long_name,
1937 const char *prefix)
1939 struct snd_kcontrol_new template;
1940 struct snd_kcontrol *kcontrol;
1941 char *name = NULL;
1942 int name_len;
1944 memcpy(&template, _template, sizeof(template));
1945 template.index = 0;
1947 if (!long_name)
1948 long_name = template.name;
1950 if (prefix) {
1951 name_len = strlen(long_name) + strlen(prefix) + 2;
1952 name = kmalloc(name_len, GFP_KERNEL);
1953 if (!name)
1954 return NULL;
1956 snprintf(name, name_len, "%s %s", prefix, long_name);
1958 template.name = name;
1959 } else {
1960 template.name = long_name;
1963 kcontrol = snd_ctl_new1(&template, data);
1965 kfree(name);
1967 return kcontrol;
1969 EXPORT_SYMBOL_GPL(snd_soc_cnew);
1972 * snd_soc_add_controls - add an array of controls to a codec.
1973 * Convienience function to add a list of controls. Many codecs were
1974 * duplicating this code.
1976 * @codec: codec to add controls to
1977 * @controls: array of controls to add
1978 * @num_controls: number of elements in the array
1980 * Return 0 for success, else error.
1982 int snd_soc_add_controls(struct snd_soc_codec *codec,
1983 const struct snd_kcontrol_new *controls, int num_controls)
1985 struct snd_card *card = codec->card->snd_card;
1986 int err, i;
1988 for (i = 0; i < num_controls; i++) {
1989 const struct snd_kcontrol_new *control = &controls[i];
1990 err = snd_ctl_add(card, snd_soc_cnew(control, codec,
1991 control->name,
1992 codec->name_prefix));
1993 if (err < 0) {
1994 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
1995 codec->name, control->name, err);
1996 return err;
2000 return 0;
2002 EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2005 * snd_soc_add_platform_controls - add an array of controls to a platform.
2006 * Convienience function to add a list of controls.
2008 * @platform: platform to add controls to
2009 * @controls: array of controls to add
2010 * @num_controls: number of elements in the array
2012 * Return 0 for success, else error.
2014 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2015 const struct snd_kcontrol_new *controls, int num_controls)
2017 struct snd_card *card = platform->card->snd_card;
2018 int err, i;
2020 for (i = 0; i < num_controls; i++) {
2021 const struct snd_kcontrol_new *control = &controls[i];
2022 err = snd_ctl_add(card, snd_soc_cnew(control, platform,
2023 control->name, NULL));
2024 if (err < 0) {
2025 dev_err(platform->dev, "Failed to add %s %d\n",control->name, err);
2026 return err;
2030 return 0;
2032 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2035 * snd_soc_info_enum_double - enumerated double mixer info callback
2036 * @kcontrol: mixer control
2037 * @uinfo: control element information
2039 * Callback to provide information about a double enumerated
2040 * mixer control.
2042 * Returns 0 for success.
2044 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2045 struct snd_ctl_elem_info *uinfo)
2047 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2049 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2050 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2051 uinfo->value.enumerated.items = e->max;
2053 if (uinfo->value.enumerated.item > e->max - 1)
2054 uinfo->value.enumerated.item = e->max - 1;
2055 strcpy(uinfo->value.enumerated.name,
2056 e->texts[uinfo->value.enumerated.item]);
2057 return 0;
2059 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2062 * snd_soc_get_enum_double - enumerated double mixer get callback
2063 * @kcontrol: mixer control
2064 * @ucontrol: control element information
2066 * Callback to get the value of a double enumerated mixer.
2068 * Returns 0 for success.
2070 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2071 struct snd_ctl_elem_value *ucontrol)
2073 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2074 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2075 unsigned int val, bitmask;
2077 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2079 val = snd_soc_read(codec, e->reg);
2080 ucontrol->value.enumerated.item[0]
2081 = (val >> e->shift_l) & (bitmask - 1);
2082 if (e->shift_l != e->shift_r)
2083 ucontrol->value.enumerated.item[1] =
2084 (val >> e->shift_r) & (bitmask - 1);
2086 return 0;
2088 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2091 * snd_soc_put_enum_double - enumerated double mixer put callback
2092 * @kcontrol: mixer control
2093 * @ucontrol: control element information
2095 * Callback to set the value of a double enumerated mixer.
2097 * Returns 0 for success.
2099 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2100 struct snd_ctl_elem_value *ucontrol)
2102 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2103 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2104 unsigned int val;
2105 unsigned int mask, bitmask;
2107 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2109 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2110 return -EINVAL;
2111 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2112 mask = (bitmask - 1) << e->shift_l;
2113 if (e->shift_l != e->shift_r) {
2114 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2115 return -EINVAL;
2116 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2117 mask |= (bitmask - 1) << e->shift_r;
2120 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2122 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2125 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2126 * @kcontrol: mixer control
2127 * @ucontrol: control element information
2129 * Callback to get the value of a double semi enumerated mixer.
2131 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2132 * used for handling bitfield coded enumeration for example.
2134 * Returns 0 for success.
2136 int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2137 struct snd_ctl_elem_value *ucontrol)
2139 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2140 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2141 unsigned int reg_val, val, mux;
2143 reg_val = snd_soc_read(codec, e->reg);
2144 val = (reg_val >> e->shift_l) & e->mask;
2145 for (mux = 0; mux < e->max; mux++) {
2146 if (val == e->values[mux])
2147 break;
2149 ucontrol->value.enumerated.item[0] = mux;
2150 if (e->shift_l != e->shift_r) {
2151 val = (reg_val >> e->shift_r) & e->mask;
2152 for (mux = 0; mux < e->max; mux++) {
2153 if (val == e->values[mux])
2154 break;
2156 ucontrol->value.enumerated.item[1] = mux;
2159 return 0;
2161 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2164 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2165 * @kcontrol: mixer control
2166 * @ucontrol: control element information
2168 * Callback to set the value of a double semi enumerated mixer.
2170 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2171 * used for handling bitfield coded enumeration for example.
2173 * Returns 0 for success.
2175 int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2176 struct snd_ctl_elem_value *ucontrol)
2178 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2179 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2180 unsigned int val;
2181 unsigned int mask;
2183 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2184 return -EINVAL;
2185 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2186 mask = e->mask << e->shift_l;
2187 if (e->shift_l != e->shift_r) {
2188 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2189 return -EINVAL;
2190 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2191 mask |= e->mask << e->shift_r;
2194 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2196 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2199 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2200 * @kcontrol: mixer control
2201 * @uinfo: control element information
2203 * Callback to provide information about an external enumerated
2204 * single mixer.
2206 * Returns 0 for success.
2208 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2209 struct snd_ctl_elem_info *uinfo)
2211 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2213 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2214 uinfo->count = 1;
2215 uinfo->value.enumerated.items = e->max;
2217 if (uinfo->value.enumerated.item > e->max - 1)
2218 uinfo->value.enumerated.item = e->max - 1;
2219 strcpy(uinfo->value.enumerated.name,
2220 e->texts[uinfo->value.enumerated.item]);
2221 return 0;
2223 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2226 * snd_soc_info_volsw_ext - external single mixer info callback
2227 * @kcontrol: mixer control
2228 * @uinfo: control element information
2230 * Callback to provide information about a single external mixer control.
2232 * Returns 0 for success.
2234 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2235 struct snd_ctl_elem_info *uinfo)
2237 int max = kcontrol->private_value;
2239 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2240 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2241 else
2242 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2244 uinfo->count = 1;
2245 uinfo->value.integer.min = 0;
2246 uinfo->value.integer.max = max;
2247 return 0;
2249 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2252 * snd_soc_info_volsw - single mixer info callback
2253 * @kcontrol: mixer control
2254 * @uinfo: control element information
2256 * Callback to provide information about a single mixer control, or a double
2257 * mixer control that spans 2 registers.
2259 * Returns 0 for success.
2261 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2262 struct snd_ctl_elem_info *uinfo)
2264 struct soc_mixer_control *mc =
2265 (struct soc_mixer_control *)kcontrol->private_value;
2266 int platform_max;
2268 if (!mc->platform_max)
2269 mc->platform_max = mc->max;
2270 platform_max = mc->platform_max;
2272 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2273 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2274 else
2275 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2277 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2278 uinfo->value.integer.min = 0;
2279 uinfo->value.integer.max = platform_max;
2280 return 0;
2282 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2285 * snd_soc_get_volsw - single mixer get callback
2286 * @kcontrol: mixer control
2287 * @ucontrol: control element information
2289 * Callback to get the value of a single mixer control, or a double mixer
2290 * control that spans 2 registers.
2292 * Returns 0 for success.
2294 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2295 struct snd_ctl_elem_value *ucontrol)
2297 struct soc_mixer_control *mc =
2298 (struct soc_mixer_control *)kcontrol->private_value;
2299 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2300 unsigned int reg = mc->reg;
2301 unsigned int reg2 = mc->rreg;
2302 unsigned int shift = mc->shift;
2303 unsigned int rshift = mc->rshift;
2304 int max = mc->max;
2305 unsigned int mask = (1 << fls(max)) - 1;
2306 unsigned int invert = mc->invert;
2308 ucontrol->value.integer.value[0] =
2309 (snd_soc_read(codec, reg) >> shift) & mask;
2310 if (invert)
2311 ucontrol->value.integer.value[0] =
2312 max - ucontrol->value.integer.value[0];
2314 if (snd_soc_volsw_is_stereo(mc)) {
2315 if (reg == reg2)
2316 ucontrol->value.integer.value[1] =
2317 (snd_soc_read(codec, reg) >> rshift) & mask;
2318 else
2319 ucontrol->value.integer.value[1] =
2320 (snd_soc_read(codec, reg2) >> shift) & mask;
2321 if (invert)
2322 ucontrol->value.integer.value[1] =
2323 max - ucontrol->value.integer.value[1];
2326 return 0;
2328 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2331 * snd_soc_put_volsw - single mixer put callback
2332 * @kcontrol: mixer control
2333 * @ucontrol: control element information
2335 * Callback to set the value of a single mixer control, or a double mixer
2336 * control that spans 2 registers.
2338 * Returns 0 for success.
2340 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2341 struct snd_ctl_elem_value *ucontrol)
2343 struct soc_mixer_control *mc =
2344 (struct soc_mixer_control *)kcontrol->private_value;
2345 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2346 unsigned int reg = mc->reg;
2347 unsigned int reg2 = mc->rreg;
2348 unsigned int shift = mc->shift;
2349 unsigned int rshift = mc->rshift;
2350 int max = mc->max;
2351 unsigned int mask = (1 << fls(max)) - 1;
2352 unsigned int invert = mc->invert;
2353 int err;
2354 bool type_2r = 0;
2355 unsigned int val2 = 0;
2356 unsigned int val, val_mask;
2358 val = (ucontrol->value.integer.value[0] & mask);
2359 if (invert)
2360 val = max - val;
2361 val_mask = mask << shift;
2362 val = val << shift;
2363 if (snd_soc_volsw_is_stereo(mc)) {
2364 val2 = (ucontrol->value.integer.value[1] & mask);
2365 if (invert)
2366 val2 = max - val2;
2367 if (reg == reg2) {
2368 val_mask |= mask << rshift;
2369 val |= val2 << rshift;
2370 } else {
2371 val2 = val2 << shift;
2372 type_2r = 1;
2375 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2376 if (err < 0)
2377 return err;
2379 if (type_2r)
2380 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2382 return err;
2384 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2387 * snd_soc_info_volsw_s8 - signed mixer info callback
2388 * @kcontrol: mixer control
2389 * @uinfo: control element information
2391 * Callback to provide information about a signed mixer control.
2393 * Returns 0 for success.
2395 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2396 struct snd_ctl_elem_info *uinfo)
2398 struct soc_mixer_control *mc =
2399 (struct soc_mixer_control *)kcontrol->private_value;
2400 int platform_max;
2401 int min = mc->min;
2403 if (!mc->platform_max)
2404 mc->platform_max = mc->max;
2405 platform_max = mc->platform_max;
2407 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2408 uinfo->count = 2;
2409 uinfo->value.integer.min = 0;
2410 uinfo->value.integer.max = platform_max - min;
2411 return 0;
2413 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2416 * snd_soc_get_volsw_s8 - signed mixer get callback
2417 * @kcontrol: mixer control
2418 * @ucontrol: control element information
2420 * Callback to get the value of a signed mixer control.
2422 * Returns 0 for success.
2424 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2425 struct snd_ctl_elem_value *ucontrol)
2427 struct soc_mixer_control *mc =
2428 (struct soc_mixer_control *)kcontrol->private_value;
2429 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2430 unsigned int reg = mc->reg;
2431 int min = mc->min;
2432 int val = snd_soc_read(codec, reg);
2434 ucontrol->value.integer.value[0] =
2435 ((signed char)(val & 0xff))-min;
2436 ucontrol->value.integer.value[1] =
2437 ((signed char)((val >> 8) & 0xff))-min;
2438 return 0;
2440 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2443 * snd_soc_put_volsw_sgn - signed mixer put callback
2444 * @kcontrol: mixer control
2445 * @ucontrol: control element information
2447 * Callback to set the value of a signed mixer control.
2449 * Returns 0 for success.
2451 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2452 struct snd_ctl_elem_value *ucontrol)
2454 struct soc_mixer_control *mc =
2455 (struct soc_mixer_control *)kcontrol->private_value;
2456 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2457 unsigned int reg = mc->reg;
2458 int min = mc->min;
2459 unsigned int val;
2461 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2462 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2464 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2466 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2469 * snd_soc_limit_volume - Set new limit to an existing volume control.
2471 * @codec: where to look for the control
2472 * @name: Name of the control
2473 * @max: new maximum limit
2475 * Return 0 for success, else error.
2477 int snd_soc_limit_volume(struct snd_soc_codec *codec,
2478 const char *name, int max)
2480 struct snd_card *card = codec->card->snd_card;
2481 struct snd_kcontrol *kctl;
2482 struct soc_mixer_control *mc;
2483 int found = 0;
2484 int ret = -EINVAL;
2486 /* Sanity check for name and max */
2487 if (unlikely(!name || max <= 0))
2488 return -EINVAL;
2490 list_for_each_entry(kctl, &card->controls, list) {
2491 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2492 found = 1;
2493 break;
2496 if (found) {
2497 mc = (struct soc_mixer_control *)kctl->private_value;
2498 if (max <= mc->max) {
2499 mc->platform_max = max;
2500 ret = 0;
2503 return ret;
2505 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2508 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2509 * mixer info callback
2510 * @kcontrol: mixer control
2511 * @uinfo: control element information
2513 * Returns 0 for success.
2515 int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2516 struct snd_ctl_elem_info *uinfo)
2518 struct soc_mixer_control *mc =
2519 (struct soc_mixer_control *)kcontrol->private_value;
2520 int max = mc->max;
2521 int min = mc->min;
2523 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2524 uinfo->count = 2;
2525 uinfo->value.integer.min = 0;
2526 uinfo->value.integer.max = max-min;
2528 return 0;
2530 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2533 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2534 * mixer get callback
2535 * @kcontrol: mixer control
2536 * @uinfo: control element information
2538 * Returns 0 for success.
2540 int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2541 struct snd_ctl_elem_value *ucontrol)
2543 struct soc_mixer_control *mc =
2544 (struct soc_mixer_control *)kcontrol->private_value;
2545 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2546 unsigned int mask = (1<<mc->shift)-1;
2547 int min = mc->min;
2548 int val = snd_soc_read(codec, mc->reg) & mask;
2549 int valr = snd_soc_read(codec, mc->rreg) & mask;
2551 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2552 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
2553 return 0;
2555 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2558 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2559 * mixer put callback
2560 * @kcontrol: mixer control
2561 * @uinfo: control element information
2563 * Returns 0 for success.
2565 int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2566 struct snd_ctl_elem_value *ucontrol)
2568 struct soc_mixer_control *mc =
2569 (struct soc_mixer_control *)kcontrol->private_value;
2570 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2571 unsigned int mask = (1<<mc->shift)-1;
2572 int min = mc->min;
2573 int ret;
2574 unsigned int val, valr, oval, ovalr;
2576 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2577 val &= mask;
2578 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2579 valr &= mask;
2581 oval = snd_soc_read(codec, mc->reg) & mask;
2582 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2584 ret = 0;
2585 if (oval != val) {
2586 ret = snd_soc_write(codec, mc->reg, val);
2587 if (ret < 0)
2588 return ret;
2590 if (ovalr != valr) {
2591 ret = snd_soc_write(codec, mc->rreg, valr);
2592 if (ret < 0)
2593 return ret;
2596 return 0;
2598 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2601 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2602 * @dai: DAI
2603 * @clk_id: DAI specific clock ID
2604 * @freq: new clock frequency in Hz
2605 * @dir: new clock direction - input/output.
2607 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2609 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2610 unsigned int freq, int dir)
2612 if (dai->driver && dai->driver->ops->set_sysclk)
2613 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2614 else if (dai->codec && dai->codec->driver->set_sysclk)
2615 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
2616 freq, dir);
2617 else
2618 return -EINVAL;
2620 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2623 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2624 * @codec: CODEC
2625 * @clk_id: DAI specific clock ID
2626 * @source: Source for the clock
2627 * @freq: new clock frequency in Hz
2628 * @dir: new clock direction - input/output.
2630 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2632 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2633 int source, unsigned int freq, int dir)
2635 if (codec->driver->set_sysclk)
2636 return codec->driver->set_sysclk(codec, clk_id, source,
2637 freq, dir);
2638 else
2639 return -EINVAL;
2641 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2644 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2645 * @dai: DAI
2646 * @div_id: DAI specific clock divider ID
2647 * @div: new clock divisor.
2649 * Configures the clock dividers. This is used to derive the best DAI bit and
2650 * frame clocks from the system or master clock. It's best to set the DAI bit
2651 * and frame clocks as low as possible to save system power.
2653 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2654 int div_id, int div)
2656 if (dai->driver && dai->driver->ops->set_clkdiv)
2657 return dai->driver->ops->set_clkdiv(dai, div_id, div);
2658 else
2659 return -EINVAL;
2661 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2664 * snd_soc_dai_set_pll - configure DAI PLL.
2665 * @dai: DAI
2666 * @pll_id: DAI specific PLL ID
2667 * @source: DAI specific source for the PLL
2668 * @freq_in: PLL input clock frequency in Hz
2669 * @freq_out: requested PLL output clock frequency in Hz
2671 * Configures and enables PLL to generate output clock based on input clock.
2673 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2674 unsigned int freq_in, unsigned int freq_out)
2676 if (dai->driver && dai->driver->ops->set_pll)
2677 return dai->driver->ops->set_pll(dai, pll_id, source,
2678 freq_in, freq_out);
2679 else if (dai->codec && dai->codec->driver->set_pll)
2680 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2681 freq_in, freq_out);
2682 else
2683 return -EINVAL;
2685 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2688 * snd_soc_codec_set_pll - configure codec PLL.
2689 * @codec: CODEC
2690 * @pll_id: DAI specific PLL ID
2691 * @source: DAI specific source for the PLL
2692 * @freq_in: PLL input clock frequency in Hz
2693 * @freq_out: requested PLL output clock frequency in Hz
2695 * Configures and enables PLL to generate output clock based on input clock.
2697 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2698 unsigned int freq_in, unsigned int freq_out)
2700 if (codec->driver->set_pll)
2701 return codec->driver->set_pll(codec, pll_id, source,
2702 freq_in, freq_out);
2703 else
2704 return -EINVAL;
2706 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2709 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2710 * @dai: DAI
2711 * @fmt: SND_SOC_DAIFMT_ format value.
2713 * Configures the DAI hardware format and clocking.
2715 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2717 if (dai->driver && dai->driver->ops->set_fmt)
2718 return dai->driver->ops->set_fmt(dai, fmt);
2719 else
2720 return -EINVAL;
2722 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2725 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2726 * @dai: DAI
2727 * @tx_mask: bitmask representing active TX slots.
2728 * @rx_mask: bitmask representing active RX slots.
2729 * @slots: Number of slots in use.
2730 * @slot_width: Width in bits for each slot.
2732 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2733 * specific.
2735 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2736 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2738 if (dai->driver && dai->driver->ops->set_tdm_slot)
2739 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2740 slots, slot_width);
2741 else
2742 return -EINVAL;
2744 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2747 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2748 * @dai: DAI
2749 * @tx_num: how many TX channels
2750 * @tx_slot: pointer to an array which imply the TX slot number channel
2751 * 0~num-1 uses
2752 * @rx_num: how many RX channels
2753 * @rx_slot: pointer to an array which imply the RX slot number channel
2754 * 0~num-1 uses
2756 * configure the relationship between channel number and TDM slot number.
2758 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2759 unsigned int tx_num, unsigned int *tx_slot,
2760 unsigned int rx_num, unsigned int *rx_slot)
2762 if (dai->driver && dai->driver->ops->set_channel_map)
2763 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2764 rx_num, rx_slot);
2765 else
2766 return -EINVAL;
2768 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2771 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2772 * @dai: DAI
2773 * @tristate: tristate enable
2775 * Tristates the DAI so that others can use it.
2777 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2779 if (dai->driver && dai->driver->ops->set_tristate)
2780 return dai->driver->ops->set_tristate(dai, tristate);
2781 else
2782 return -EINVAL;
2784 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2787 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2788 * @dai: DAI
2789 * @mute: mute enable
2791 * Mutes the DAI DAC.
2793 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2795 if (dai->driver && dai->driver->ops->digital_mute)
2796 return dai->driver->ops->digital_mute(dai, mute);
2797 else
2798 return -EINVAL;
2800 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2803 * snd_soc_register_card - Register a card with the ASoC core
2805 * @card: Card to register
2808 int snd_soc_register_card(struct snd_soc_card *card)
2810 int i;
2812 if (!card->name || !card->dev)
2813 return -EINVAL;
2815 dev_set_drvdata(card->dev, card);
2817 snd_soc_initialize_card_lists(card);
2819 soc_init_card_debugfs(card);
2821 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
2822 (card->num_links + card->num_aux_devs),
2823 GFP_KERNEL);
2824 if (card->rtd == NULL)
2825 return -ENOMEM;
2826 card->rtd_aux = &card->rtd[card->num_links];
2828 for (i = 0; i < card->num_links; i++)
2829 card->rtd[i].dai_link = &card->dai_link[i];
2831 INIT_LIST_HEAD(&card->list);
2832 INIT_LIST_HEAD(&card->dapm_dirty);
2833 card->instantiated = 0;
2834 mutex_init(&card->mutex);
2836 mutex_lock(&client_mutex);
2837 list_add(&card->list, &card_list);
2838 snd_soc_instantiate_cards();
2839 mutex_unlock(&client_mutex);
2841 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2843 return 0;
2845 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2848 * snd_soc_unregister_card - Unregister a card with the ASoC core
2850 * @card: Card to unregister
2853 int snd_soc_unregister_card(struct snd_soc_card *card)
2855 if (card->instantiated)
2856 soc_cleanup_card_resources(card);
2857 mutex_lock(&client_mutex);
2858 list_del(&card->list);
2859 mutex_unlock(&client_mutex);
2860 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2862 return 0;
2864 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2867 * Simplify DAI link configuration by removing ".-1" from device names
2868 * and sanitizing names.
2870 static char *fmt_single_name(struct device *dev, int *id)
2872 char *found, name[NAME_SIZE];
2873 int id1, id2;
2875 if (dev_name(dev) == NULL)
2876 return NULL;
2878 strlcpy(name, dev_name(dev), NAME_SIZE);
2880 /* are we a "%s.%d" name (platform and SPI components) */
2881 found = strstr(name, dev->driver->name);
2882 if (found) {
2883 /* get ID */
2884 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2886 /* discard ID from name if ID == -1 */
2887 if (*id == -1)
2888 found[strlen(dev->driver->name)] = '\0';
2891 } else {
2892 /* I2C component devices are named "bus-addr" */
2893 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2894 char tmp[NAME_SIZE];
2896 /* create unique ID number from I2C addr and bus */
2897 *id = ((id1 & 0xffff) << 16) + id2;
2899 /* sanitize component name for DAI link creation */
2900 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2901 strlcpy(name, tmp, NAME_SIZE);
2902 } else
2903 *id = 0;
2906 return kstrdup(name, GFP_KERNEL);
2910 * Simplify DAI link naming for single devices with multiple DAIs by removing
2911 * any ".-1" and using the DAI name (instead of device name).
2913 static inline char *fmt_multiple_name(struct device *dev,
2914 struct snd_soc_dai_driver *dai_drv)
2916 if (dai_drv->name == NULL) {
2917 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
2918 dev_name(dev));
2919 return NULL;
2922 return kstrdup(dai_drv->name, GFP_KERNEL);
2926 * snd_soc_register_dai - Register a DAI with the ASoC core
2928 * @dai: DAI to register
2930 int snd_soc_register_dai(struct device *dev,
2931 struct snd_soc_dai_driver *dai_drv)
2933 struct snd_soc_dai *dai;
2935 dev_dbg(dev, "dai register %s\n", dev_name(dev));
2937 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2938 if (dai == NULL)
2939 return -ENOMEM;
2941 /* create DAI component name */
2942 dai->name = fmt_single_name(dev, &dai->id);
2943 if (dai->name == NULL) {
2944 kfree(dai);
2945 return -ENOMEM;
2948 dai->dev = dev;
2949 dai->driver = dai_drv;
2950 if (!dai->driver->ops)
2951 dai->driver->ops = &null_dai_ops;
2953 mutex_lock(&client_mutex);
2954 list_add(&dai->list, &dai_list);
2955 snd_soc_instantiate_cards();
2956 mutex_unlock(&client_mutex);
2958 pr_debug("Registered DAI '%s'\n", dai->name);
2960 return 0;
2962 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2965 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2967 * @dai: DAI to unregister
2969 void snd_soc_unregister_dai(struct device *dev)
2971 struct snd_soc_dai *dai;
2973 list_for_each_entry(dai, &dai_list, list) {
2974 if (dev == dai->dev)
2975 goto found;
2977 return;
2979 found:
2980 mutex_lock(&client_mutex);
2981 list_del(&dai->list);
2982 mutex_unlock(&client_mutex);
2984 pr_debug("Unregistered DAI '%s'\n", dai->name);
2985 kfree(dai->name);
2986 kfree(dai);
2988 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2991 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2993 * @dai: Array of DAIs to register
2994 * @count: Number of DAIs
2996 int snd_soc_register_dais(struct device *dev,
2997 struct snd_soc_dai_driver *dai_drv, size_t count)
2999 struct snd_soc_dai *dai;
3000 int i, ret = 0;
3002 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
3004 for (i = 0; i < count; i++) {
3006 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3007 if (dai == NULL) {
3008 ret = -ENOMEM;
3009 goto err;
3012 /* create DAI component name */
3013 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3014 if (dai->name == NULL) {
3015 kfree(dai);
3016 ret = -EINVAL;
3017 goto err;
3020 dai->dev = dev;
3021 dai->driver = &dai_drv[i];
3022 if (dai->driver->id)
3023 dai->id = dai->driver->id;
3024 else
3025 dai->id = i;
3026 if (!dai->driver->ops)
3027 dai->driver->ops = &null_dai_ops;
3029 mutex_lock(&client_mutex);
3030 list_add(&dai->list, &dai_list);
3031 mutex_unlock(&client_mutex);
3033 pr_debug("Registered DAI '%s'\n", dai->name);
3036 mutex_lock(&client_mutex);
3037 snd_soc_instantiate_cards();
3038 mutex_unlock(&client_mutex);
3039 return 0;
3041 err:
3042 for (i--; i >= 0; i--)
3043 snd_soc_unregister_dai(dev);
3045 return ret;
3047 EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3050 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3052 * @dai: Array of DAIs to unregister
3053 * @count: Number of DAIs
3055 void snd_soc_unregister_dais(struct device *dev, size_t count)
3057 int i;
3059 for (i = 0; i < count; i++)
3060 snd_soc_unregister_dai(dev);
3062 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3065 * snd_soc_register_platform - Register a platform with the ASoC core
3067 * @platform: platform to register
3069 int snd_soc_register_platform(struct device *dev,
3070 struct snd_soc_platform_driver *platform_drv)
3072 struct snd_soc_platform *platform;
3074 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3076 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3077 if (platform == NULL)
3078 return -ENOMEM;
3080 /* create platform component name */
3081 platform->name = fmt_single_name(dev, &platform->id);
3082 if (platform->name == NULL) {
3083 kfree(platform);
3084 return -ENOMEM;
3087 platform->dev = dev;
3088 platform->driver = platform_drv;
3089 platform->dapm.dev = dev;
3090 platform->dapm.platform = platform;
3091 platform->dapm.stream_event = platform_drv->stream_event;
3093 mutex_lock(&client_mutex);
3094 list_add(&platform->list, &platform_list);
3095 snd_soc_instantiate_cards();
3096 mutex_unlock(&client_mutex);
3098 pr_debug("Registered platform '%s'\n", platform->name);
3100 return 0;
3102 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3105 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3107 * @platform: platform to unregister
3109 void snd_soc_unregister_platform(struct device *dev)
3111 struct snd_soc_platform *platform;
3113 list_for_each_entry(platform, &platform_list, list) {
3114 if (dev == platform->dev)
3115 goto found;
3117 return;
3119 found:
3120 mutex_lock(&client_mutex);
3121 list_del(&platform->list);
3122 mutex_unlock(&client_mutex);
3124 pr_debug("Unregistered platform '%s'\n", platform->name);
3125 kfree(platform->name);
3126 kfree(platform);
3128 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3130 static u64 codec_format_map[] = {
3131 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3132 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3133 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3134 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3135 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3136 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3137 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3138 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3139 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3140 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3141 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3142 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3143 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3144 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3145 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3146 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3149 /* Fix up the DAI formats for endianness: codecs don't actually see
3150 * the endianness of the data but we're using the CPU format
3151 * definitions which do need to include endianness so we ensure that
3152 * codec DAIs always have both big and little endian variants set.
3154 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3156 int i;
3158 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3159 if (stream->formats & codec_format_map[i])
3160 stream->formats |= codec_format_map[i];
3164 * snd_soc_register_codec - Register a codec with the ASoC core
3166 * @codec: codec to register
3168 int snd_soc_register_codec(struct device *dev,
3169 const struct snd_soc_codec_driver *codec_drv,
3170 struct snd_soc_dai_driver *dai_drv,
3171 int num_dai)
3173 size_t reg_size;
3174 struct snd_soc_codec *codec;
3175 int ret, i;
3177 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3179 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3180 if (codec == NULL)
3181 return -ENOMEM;
3183 /* create CODEC component name */
3184 codec->name = fmt_single_name(dev, &codec->id);
3185 if (codec->name == NULL) {
3186 kfree(codec);
3187 return -ENOMEM;
3190 if (codec_drv->compress_type)
3191 codec->compress_type = codec_drv->compress_type;
3192 else
3193 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3195 codec->write = codec_drv->write;
3196 codec->read = codec_drv->read;
3197 codec->volatile_register = codec_drv->volatile_register;
3198 codec->readable_register = codec_drv->readable_register;
3199 codec->writable_register = codec_drv->writable_register;
3200 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3201 codec->dapm.dev = dev;
3202 codec->dapm.codec = codec;
3203 codec->dapm.seq_notifier = codec_drv->seq_notifier;
3204 codec->dapm.stream_event = codec_drv->stream_event;
3205 codec->dev = dev;
3206 codec->driver = codec_drv;
3207 codec->num_dai = num_dai;
3208 mutex_init(&codec->mutex);
3210 /* allocate CODEC register cache */
3211 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3212 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
3213 codec->reg_size = reg_size;
3214 /* it is necessary to make a copy of the default register cache
3215 * because in the case of using a compression type that requires
3216 * the default register cache to be marked as __devinitconst the
3217 * kernel might have freed the array by the time we initialize
3218 * the cache.
3220 if (codec_drv->reg_cache_default) {
3221 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3222 reg_size, GFP_KERNEL);
3223 if (!codec->reg_def_copy) {
3224 ret = -ENOMEM;
3225 goto fail;
3230 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3231 if (!codec->volatile_register)
3232 codec->volatile_register = snd_soc_default_volatile_register;
3233 if (!codec->readable_register)
3234 codec->readable_register = snd_soc_default_readable_register;
3235 if (!codec->writable_register)
3236 codec->writable_register = snd_soc_default_writable_register;
3239 for (i = 0; i < num_dai; i++) {
3240 fixup_codec_formats(&dai_drv[i].playback);
3241 fixup_codec_formats(&dai_drv[i].capture);
3244 /* register any DAIs */
3245 if (num_dai) {
3246 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3247 if (ret < 0)
3248 goto fail;
3251 mutex_lock(&client_mutex);
3252 list_add(&codec->list, &codec_list);
3253 snd_soc_instantiate_cards();
3254 mutex_unlock(&client_mutex);
3256 pr_debug("Registered codec '%s'\n", codec->name);
3257 return 0;
3259 fail:
3260 kfree(codec->reg_def_copy);
3261 codec->reg_def_copy = NULL;
3262 kfree(codec->name);
3263 kfree(codec);
3264 return ret;
3266 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3269 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3271 * @codec: codec to unregister
3273 void snd_soc_unregister_codec(struct device *dev)
3275 struct snd_soc_codec *codec;
3276 int i;
3278 list_for_each_entry(codec, &codec_list, list) {
3279 if (dev == codec->dev)
3280 goto found;
3282 return;
3284 found:
3285 if (codec->num_dai)
3286 for (i = 0; i < codec->num_dai; i++)
3287 snd_soc_unregister_dai(dev);
3289 mutex_lock(&client_mutex);
3290 list_del(&codec->list);
3291 mutex_unlock(&client_mutex);
3293 pr_debug("Unregistered codec '%s'\n", codec->name);
3295 snd_soc_cache_exit(codec);
3296 kfree(codec->reg_def_copy);
3297 kfree(codec->name);
3298 kfree(codec);
3300 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3302 static int __init snd_soc_init(void)
3304 #ifdef CONFIG_DEBUG_FS
3305 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3306 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
3307 printk(KERN_WARNING
3308 "ASoC: Failed to create debugfs directory\n");
3309 snd_soc_debugfs_root = NULL;
3312 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
3313 &codec_list_fops))
3314 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3316 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
3317 &dai_list_fops))
3318 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
3320 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
3321 &platform_list_fops))
3322 pr_warn("ASoC: Failed to create platform list debugfs file\n");
3323 #endif
3325 snd_soc_util_init();
3327 return platform_driver_register(&soc_driver);
3329 module_init(snd_soc_init);
3331 static void __exit snd_soc_exit(void)
3333 snd_soc_util_exit();
3335 #ifdef CONFIG_DEBUG_FS
3336 debugfs_remove_recursive(snd_soc_debugfs_root);
3337 #endif
3338 platform_driver_unregister(&soc_driver);
3340 module_exit(snd_soc_exit);
3342 /* Module information */
3343 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3344 MODULE_DESCRIPTION("ALSA SoC Core");
3345 MODULE_LICENSE("GPL");
3346 MODULE_ALIAS("platform:soc-audio");