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.
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>
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>
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/soc-dpcm.h>
43 #include <sound/initval.h>
45 #define CREATE_TRACE_POINTS
46 #include <trace/events/asoc.h>
50 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
52 #ifdef CONFIG_DEBUG_FS
53 struct dentry *snd_soc_debugfs_root;
54 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
57 static DEFINE_MUTEX(client_mutex);
58 static LIST_HEAD(dai_list);
59 static LIST_HEAD(platform_list);
60 static LIST_HEAD(codec_list);
61 static LIST_HEAD(component_list);
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)
79 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
82 c = (sizeof val * 8) - 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;
99 char regbuf[regsize + 1];
101 /* since tmpbuf is allocated on the stack, warn the callers if they
102 * try to abuse this function */
105 /* +2 for ': ' and + 1 for '\n' */
106 if (wordsize + regsize + 2 + 1 != len)
109 ret = snd_soc_read(codec, reg);
111 memset(regbuf, 'X', regsize);
112 regbuf[regsize] = '\0';
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);
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)
130 int wordsize, regsize;
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)
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))
149 if (codec->driver->display_register) {
150 count += codec->driver->display_register(codec, buf + count,
151 PAGE_SIZE - count, i);
153 /* only support larger than PAGE_SIZE bytes debugfs
154 * entries for the default case */
156 if (total + len >= count - 1)
158 format_register_str(codec, i, buf + total, len);
165 total = min(total, count - 1);
170 static ssize_t codec_reg_show(struct device *dev,
171 struct device_attribute *attr, char *buf)
173 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
175 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
178 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
180 static ssize_t pmdown_time_show(struct device *dev,
181 struct device_attribute *attr, char *buf)
183 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
185 return sprintf(buf, "%ld\n", rtd->pmdown_time);
188 static ssize_t pmdown_time_set(struct device *dev,
189 struct device_attribute *attr,
190 const char *buf, size_t count)
192 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
195 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
202 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
204 #ifdef CONFIG_DEBUG_FS
205 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
206 size_t count, loff_t *ppos)
209 struct snd_soc_codec *codec = file->private_data;
212 if (*ppos < 0 || !count)
215 buf = kmalloc(count, GFP_KERNEL);
219 ret = soc_codec_reg_show(codec, buf, count, *ppos);
221 if (copy_to_user(user_buf, buf, ret)) {
232 static ssize_t codec_reg_write_file(struct file *file,
233 const char __user *user_buf, size_t count, loff_t *ppos)
238 unsigned long reg, value;
239 struct snd_soc_codec *codec = file->private_data;
241 buf_size = min(count, (sizeof(buf)-1));
242 if (copy_from_user(buf, user_buf, buf_size))
246 while (*start == ' ')
248 reg = simple_strtoul(start, &start, 16);
249 while (*start == ' ')
251 if (strict_strtoul(start, 16, &value))
254 /* Userspace has been fiddling around behind the kernel's back */
255 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
257 snd_soc_write(codec, reg, value);
261 static const struct file_operations codec_reg_fops = {
263 .read = codec_reg_read_file,
264 .write = codec_reg_write_file,
265 .llseek = default_llseek,
268 static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
270 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
272 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
274 if (!codec->debugfs_codec_root) {
276 "ASoC: Failed to create codec debugfs directory\n");
280 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
282 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
285 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
286 codec->debugfs_codec_root,
287 codec, &codec_reg_fops);
288 if (!codec->debugfs_reg)
290 "ASoC: Failed to create codec register debugfs file\n");
292 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
295 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
297 debugfs_remove_recursive(codec->debugfs_codec_root);
300 static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
302 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
304 platform->debugfs_platform_root = debugfs_create_dir(platform->name,
306 if (!platform->debugfs_platform_root) {
307 dev_warn(platform->dev,
308 "ASoC: Failed to create platform debugfs directory\n");
312 snd_soc_dapm_debugfs_init(&platform->dapm,
313 platform->debugfs_platform_root);
316 static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
318 debugfs_remove_recursive(platform->debugfs_platform_root);
321 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
322 size_t count, loff_t *ppos)
324 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
325 ssize_t len, ret = 0;
326 struct snd_soc_codec *codec;
331 list_for_each_entry(codec, &codec_list, list) {
332 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
336 if (ret > PAGE_SIZE) {
343 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
350 static const struct file_operations codec_list_fops = {
351 .read = codec_list_read_file,
352 .llseek = default_llseek,/* read accesses f_pos */
355 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
356 size_t count, loff_t *ppos)
358 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
359 ssize_t len, ret = 0;
360 struct snd_soc_dai *dai;
365 list_for_each_entry(dai, &dai_list, list) {
366 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
369 if (ret > PAGE_SIZE) {
375 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
382 static const struct file_operations dai_list_fops = {
383 .read = dai_list_read_file,
384 .llseek = default_llseek,/* read accesses f_pos */
387 static ssize_t platform_list_read_file(struct file *file,
388 char __user *user_buf,
389 size_t count, loff_t *ppos)
391 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
392 ssize_t len, ret = 0;
393 struct snd_soc_platform *platform;
398 list_for_each_entry(platform, &platform_list, list) {
399 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
403 if (ret > PAGE_SIZE) {
409 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
416 static const struct file_operations platform_list_fops = {
417 .read = platform_list_read_file,
418 .llseek = default_llseek,/* read accesses f_pos */
421 static void soc_init_card_debugfs(struct snd_soc_card *card)
423 card->debugfs_card_root = debugfs_create_dir(card->name,
424 snd_soc_debugfs_root);
425 if (!card->debugfs_card_root) {
427 "ASoC: Failed to create card debugfs directory\n");
431 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
432 card->debugfs_card_root,
434 if (!card->debugfs_pop_time)
436 "ASoC: Failed to create pop time debugfs file\n");
439 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
441 debugfs_remove_recursive(card->debugfs_card_root);
446 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
450 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
454 static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
458 static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
462 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
466 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
471 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
472 const char *dai_link, int stream)
476 for (i = 0; i < card->num_links; i++) {
477 if (card->rtd[i].dai_link->no_pcm &&
478 !strcmp(card->rtd[i].dai_link->name, dai_link))
479 return card->rtd[i].pcm->streams[stream].substream;
481 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
484 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
486 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
487 const char *dai_link)
491 for (i = 0; i < card->num_links; i++) {
492 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
493 return &card->rtd[i];
495 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
498 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
500 #ifdef CONFIG_SND_SOC_AC97_BUS
501 /* unregister ac97 codec */
502 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
504 if (codec->ac97->dev.bus)
505 device_unregister(&codec->ac97->dev);
509 /* stop no dev release warning */
510 static void soc_ac97_device_release(struct device *dev){}
512 /* register ac97 codec to bus */
513 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
517 codec->ac97->dev.bus = &ac97_bus_type;
518 codec->ac97->dev.parent = codec->card->dev;
519 codec->ac97->dev.release = soc_ac97_device_release;
521 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
522 codec->card->snd_card->number, 0, codec->name);
523 err = device_register(&codec->ac97->dev);
525 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
526 codec->ac97->dev.bus = NULL;
533 #ifdef CONFIG_PM_SLEEP
534 /* powers down audio subsystem for suspend */
535 int snd_soc_suspend(struct device *dev)
537 struct snd_soc_card *card = dev_get_drvdata(dev);
538 struct snd_soc_codec *codec;
541 /* If the initialization of this soc device failed, there is no codec
542 * associated with it. Just bail out in this case.
544 if (list_empty(&card->codec_dev_list))
547 /* Due to the resume being scheduled into a workqueue we could
548 * suspend before that's finished - wait for it to complete.
550 snd_power_lock(card->snd_card);
551 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
552 snd_power_unlock(card->snd_card);
554 /* we're going to block userspace touching us until resume completes */
555 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
557 /* mute any active DACs */
558 for (i = 0; i < card->num_rtd; i++) {
559 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
560 struct snd_soc_dai_driver *drv = dai->driver;
562 if (card->rtd[i].dai_link->ignore_suspend)
565 if (drv->ops->digital_mute && dai->playback_active)
566 drv->ops->digital_mute(dai, 1);
569 /* suspend all pcms */
570 for (i = 0; i < card->num_rtd; i++) {
571 if (card->rtd[i].dai_link->ignore_suspend)
574 snd_pcm_suspend_all(card->rtd[i].pcm);
577 if (card->suspend_pre)
578 card->suspend_pre(card);
580 for (i = 0; i < card->num_rtd; i++) {
581 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
582 struct snd_soc_platform *platform = card->rtd[i].platform;
584 if (card->rtd[i].dai_link->ignore_suspend)
587 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
588 cpu_dai->driver->suspend(cpu_dai);
589 if (platform->driver->suspend && !platform->suspended) {
590 platform->driver->suspend(cpu_dai);
591 platform->suspended = 1;
595 /* close any waiting streams and save state */
596 for (i = 0; i < card->num_rtd; i++) {
597 flush_delayed_work(&card->rtd[i].delayed_work);
598 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
601 for (i = 0; i < card->num_rtd; i++) {
603 if (card->rtd[i].dai_link->ignore_suspend)
606 snd_soc_dapm_stream_event(&card->rtd[i],
607 SNDRV_PCM_STREAM_PLAYBACK,
608 SND_SOC_DAPM_STREAM_SUSPEND);
610 snd_soc_dapm_stream_event(&card->rtd[i],
611 SNDRV_PCM_STREAM_CAPTURE,
612 SND_SOC_DAPM_STREAM_SUSPEND);
615 /* Recheck all analogue paths too */
616 dapm_mark_io_dirty(&card->dapm);
617 snd_soc_dapm_sync(&card->dapm);
619 /* suspend all CODECs */
620 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
621 /* If there are paths active then the CODEC will be held with
622 * bias _ON and should not be suspended. */
623 if (!codec->suspended && codec->driver->suspend) {
624 switch (codec->dapm.bias_level) {
625 case SND_SOC_BIAS_STANDBY:
627 * If the CODEC is capable of idle
628 * bias off then being in STANDBY
629 * means it's doing something,
630 * otherwise fall through.
632 if (codec->dapm.idle_bias_off) {
634 "ASoC: idle_bias_off CODEC on over suspend\n");
637 case SND_SOC_BIAS_OFF:
638 codec->driver->suspend(codec);
639 codec->suspended = 1;
640 codec->cache_sync = 1;
641 if (codec->using_regmap)
642 regcache_mark_dirty(codec->control_data);
646 "ASoC: CODEC is on over suspend\n");
652 for (i = 0; i < card->num_rtd; i++) {
653 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
655 if (card->rtd[i].dai_link->ignore_suspend)
658 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
659 cpu_dai->driver->suspend(cpu_dai);
662 if (card->suspend_post)
663 card->suspend_post(card);
667 EXPORT_SYMBOL_GPL(snd_soc_suspend);
669 /* deferred resume work, so resume can complete before we finished
670 * setting our codec back up, which can be very slow on I2C
672 static void soc_resume_deferred(struct work_struct *work)
674 struct snd_soc_card *card =
675 container_of(work, struct snd_soc_card, deferred_resume_work);
676 struct snd_soc_codec *codec;
679 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
680 * so userspace apps are blocked from touching us
683 dev_dbg(card->dev, "ASoC: starting resume work\n");
685 /* Bring us up into D2 so that DAPM starts enabling things */
686 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
688 if (card->resume_pre)
689 card->resume_pre(card);
691 /* resume AC97 DAIs */
692 for (i = 0; i < card->num_rtd; i++) {
693 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
695 if (card->rtd[i].dai_link->ignore_suspend)
698 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
699 cpu_dai->driver->resume(cpu_dai);
702 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
703 /* If the CODEC was idle over suspend then it will have been
704 * left with bias OFF or STANDBY and suspended so we must now
705 * resume. Otherwise the suspend was suppressed.
707 if (codec->driver->resume && codec->suspended) {
708 switch (codec->dapm.bias_level) {
709 case SND_SOC_BIAS_STANDBY:
710 case SND_SOC_BIAS_OFF:
711 codec->driver->resume(codec);
712 codec->suspended = 0;
716 "ASoC: CODEC was on over suspend\n");
722 for (i = 0; i < card->num_rtd; i++) {
724 if (card->rtd[i].dai_link->ignore_suspend)
727 snd_soc_dapm_stream_event(&card->rtd[i],
728 SNDRV_PCM_STREAM_PLAYBACK,
729 SND_SOC_DAPM_STREAM_RESUME);
731 snd_soc_dapm_stream_event(&card->rtd[i],
732 SNDRV_PCM_STREAM_CAPTURE,
733 SND_SOC_DAPM_STREAM_RESUME);
736 /* unmute any active DACs */
737 for (i = 0; i < card->num_rtd; i++) {
738 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
739 struct snd_soc_dai_driver *drv = dai->driver;
741 if (card->rtd[i].dai_link->ignore_suspend)
744 if (drv->ops->digital_mute && dai->playback_active)
745 drv->ops->digital_mute(dai, 0);
748 for (i = 0; i < card->num_rtd; i++) {
749 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
750 struct snd_soc_platform *platform = card->rtd[i].platform;
752 if (card->rtd[i].dai_link->ignore_suspend)
755 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
756 cpu_dai->driver->resume(cpu_dai);
757 if (platform->driver->resume && platform->suspended) {
758 platform->driver->resume(cpu_dai);
759 platform->suspended = 0;
763 if (card->resume_post)
764 card->resume_post(card);
766 dev_dbg(card->dev, "ASoC: resume work completed\n");
768 /* userspace can access us now we are back as we were before */
769 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
771 /* Recheck all analogue paths too */
772 dapm_mark_io_dirty(&card->dapm);
773 snd_soc_dapm_sync(&card->dapm);
776 /* powers up audio subsystem after a suspend */
777 int snd_soc_resume(struct device *dev)
779 struct snd_soc_card *card = dev_get_drvdata(dev);
780 int i, ac97_control = 0;
782 /* If the initialization of this soc device failed, there is no codec
783 * associated with it. Just bail out in this case.
785 if (list_empty(&card->codec_dev_list))
788 /* AC97 devices might have other drivers hanging off them so
789 * need to resume immediately. Other drivers don't have that
790 * problem and may take a substantial amount of time to resume
791 * due to I/O costs and anti-pop so handle them out of line.
793 for (i = 0; i < card->num_rtd; i++) {
794 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
795 ac97_control |= cpu_dai->driver->ac97_control;
798 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
799 soc_resume_deferred(&card->deferred_resume_work);
801 dev_dbg(dev, "ASoC: Scheduling resume work\n");
802 if (!schedule_work(&card->deferred_resume_work))
803 dev_err(dev, "ASoC: resume work item may be lost\n");
808 EXPORT_SYMBOL_GPL(snd_soc_resume);
810 #define snd_soc_suspend NULL
811 #define snd_soc_resume NULL
814 static const struct snd_soc_dai_ops null_dai_ops = {
817 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
819 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
820 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
821 struct snd_soc_codec *codec;
822 struct snd_soc_platform *platform;
823 struct snd_soc_dai *codec_dai, *cpu_dai;
824 const char *platform_name;
826 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
828 /* Find CPU DAI from registered DAIs*/
829 list_for_each_entry(cpu_dai, &dai_list, list) {
830 if (dai_link->cpu_of_node &&
831 (cpu_dai->dev->of_node != dai_link->cpu_of_node))
833 if (dai_link->cpu_name &&
834 strcmp(dev_name(cpu_dai->dev), dai_link->cpu_name))
836 if (dai_link->cpu_dai_name &&
837 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
840 rtd->cpu_dai = cpu_dai;
844 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
845 dai_link->cpu_dai_name);
846 return -EPROBE_DEFER;
849 /* Find CODEC from registered CODECs */
850 list_for_each_entry(codec, &codec_list, list) {
851 if (dai_link->codec_of_node) {
852 if (codec->dev->of_node != dai_link->codec_of_node)
855 if (strcmp(codec->name, dai_link->codec_name))
862 * CODEC found, so find CODEC DAI from registered DAIs from
865 list_for_each_entry(codec_dai, &dai_list, list) {
866 if (codec->dev == codec_dai->dev &&
867 !strcmp(codec_dai->name,
868 dai_link->codec_dai_name)) {
870 rtd->codec_dai = codec_dai;
874 if (!rtd->codec_dai) {
875 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
876 dai_link->codec_dai_name);
877 return -EPROBE_DEFER;
882 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
883 dai_link->codec_name);
884 return -EPROBE_DEFER;
887 /* if there's no platform we match on the empty platform */
888 platform_name = dai_link->platform_name;
889 if (!platform_name && !dai_link->platform_of_node)
890 platform_name = "snd-soc-dummy";
892 /* find one from the set of registered platforms */
893 list_for_each_entry(platform, &platform_list, list) {
894 if (dai_link->platform_of_node) {
895 if (platform->dev->of_node !=
896 dai_link->platform_of_node)
899 if (strcmp(platform->name, platform_name))
903 rtd->platform = platform;
905 if (!rtd->platform) {
906 dev_err(card->dev, "ASoC: platform %s not registered\n",
907 dai_link->platform_name);
908 return -EPROBE_DEFER;
916 static int soc_remove_platform(struct snd_soc_platform *platform)
920 if (platform->driver->remove) {
921 ret = platform->driver->remove(platform);
923 dev_err(platform->dev, "ASoC: failed to remove %d\n",
927 /* Make sure all DAPM widgets are freed */
928 snd_soc_dapm_free(&platform->dapm);
930 soc_cleanup_platform_debugfs(platform);
931 platform->probed = 0;
932 list_del(&platform->card_list);
933 module_put(platform->dev->driver->owner);
938 static void soc_remove_codec(struct snd_soc_codec *codec)
942 if (codec->driver->remove) {
943 err = codec->driver->remove(codec);
945 dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
948 /* Make sure all DAPM widgets are freed */
949 snd_soc_dapm_free(&codec->dapm);
951 soc_cleanup_codec_debugfs(codec);
953 list_del(&codec->card_list);
954 module_put(codec->dev->driver->owner);
957 static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
959 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
960 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
963 /* unregister the rtd device */
964 if (rtd->dev_registered) {
965 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
966 device_remove_file(rtd->dev, &dev_attr_codec_reg);
967 device_unregister(rtd->dev);
968 rtd->dev_registered = 0;
971 /* remove the CODEC DAI */
972 if (codec_dai && codec_dai->probed &&
973 codec_dai->driver->remove_order == order) {
974 if (codec_dai->driver->remove) {
975 err = codec_dai->driver->remove(codec_dai);
977 dev_err(codec_dai->dev,
978 "ASoC: failed to remove %s: %d\n",
979 codec_dai->name, err);
981 codec_dai->probed = 0;
982 list_del(&codec_dai->card_list);
985 /* remove the cpu_dai */
986 if (cpu_dai && cpu_dai->probed &&
987 cpu_dai->driver->remove_order == order) {
988 if (cpu_dai->driver->remove) {
989 err = cpu_dai->driver->remove(cpu_dai);
991 dev_err(cpu_dai->dev,
992 "ASoC: failed to remove %s: %d\n",
996 list_del(&cpu_dai->card_list);
998 if (!cpu_dai->codec) {
999 snd_soc_dapm_free(&cpu_dai->dapm);
1000 module_put(cpu_dai->dev->driver->owner);
1005 static void soc_remove_link_components(struct snd_soc_card *card, int num,
1008 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1009 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1010 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1011 struct snd_soc_platform *platform = rtd->platform;
1012 struct snd_soc_codec *codec;
1014 /* remove the platform */
1015 if (platform && platform->probed &&
1016 platform->driver->remove_order == order) {
1017 soc_remove_platform(platform);
1020 /* remove the CODEC-side CODEC */
1022 codec = codec_dai->codec;
1023 if (codec && codec->probed &&
1024 codec->driver->remove_order == order)
1025 soc_remove_codec(codec);
1028 /* remove any CPU-side CODEC */
1030 codec = cpu_dai->codec;
1031 if (codec && codec->probed &&
1032 codec->driver->remove_order == order)
1033 soc_remove_codec(codec);
1037 static void soc_remove_dai_links(struct snd_soc_card *card)
1041 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1043 for (dai = 0; dai < card->num_rtd; dai++)
1044 soc_remove_link_dais(card, dai, order);
1047 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1049 for (dai = 0; dai < card->num_rtd; dai++)
1050 soc_remove_link_components(card, dai, order);
1056 static void soc_set_name_prefix(struct snd_soc_card *card,
1057 struct snd_soc_codec *codec)
1061 if (card->codec_conf == NULL)
1064 for (i = 0; i < card->num_configs; i++) {
1065 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1066 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1067 codec->name_prefix = map->name_prefix;
1073 static int soc_probe_codec(struct snd_soc_card *card,
1074 struct snd_soc_codec *codec)
1077 const struct snd_soc_codec_driver *driver = codec->driver;
1078 struct snd_soc_dai *dai;
1081 codec->dapm.card = card;
1082 soc_set_name_prefix(card, codec);
1084 if (!try_module_get(codec->dev->driver->owner))
1087 soc_init_codec_debugfs(codec);
1089 if (driver->dapm_widgets)
1090 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1091 driver->num_dapm_widgets);
1093 /* Create DAPM widgets for each DAI stream */
1094 list_for_each_entry(dai, &dai_list, list) {
1095 if (dai->dev != codec->dev)
1098 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1101 codec->dapm.idle_bias_off = driver->idle_bias_off;
1103 if (driver->probe) {
1104 ret = driver->probe(codec);
1107 "ASoC: failed to probe CODEC %d\n", ret);
1110 WARN(codec->dapm.idle_bias_off &&
1111 codec->dapm.bias_level != SND_SOC_BIAS_OFF,
1112 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1116 /* If the driver didn't set I/O up try regmap */
1117 if (!codec->write && dev_get_regmap(codec->dev, NULL))
1118 snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
1120 if (driver->controls)
1121 snd_soc_add_codec_controls(codec, driver->controls,
1122 driver->num_controls);
1123 if (driver->dapm_routes)
1124 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1125 driver->num_dapm_routes);
1127 /* mark codec as probed and add to card codec list */
1129 list_add(&codec->card_list, &card->codec_dev_list);
1130 list_add(&codec->dapm.list, &card->dapm_list);
1135 soc_cleanup_codec_debugfs(codec);
1136 module_put(codec->dev->driver->owner);
1141 static int soc_probe_platform(struct snd_soc_card *card,
1142 struct snd_soc_platform *platform)
1145 const struct snd_soc_platform_driver *driver = platform->driver;
1146 struct snd_soc_dai *dai;
1148 platform->card = card;
1149 platform->dapm.card = card;
1151 if (!try_module_get(platform->dev->driver->owner))
1154 soc_init_platform_debugfs(platform);
1156 if (driver->dapm_widgets)
1157 snd_soc_dapm_new_controls(&platform->dapm,
1158 driver->dapm_widgets, driver->num_dapm_widgets);
1160 /* Create DAPM widgets for each DAI stream */
1161 list_for_each_entry(dai, &dai_list, list) {
1162 if (dai->dev != platform->dev)
1165 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1168 platform->dapm.idle_bias_off = 1;
1170 if (driver->probe) {
1171 ret = driver->probe(platform);
1173 dev_err(platform->dev,
1174 "ASoC: failed to probe platform %d\n", ret);
1179 if (driver->controls)
1180 snd_soc_add_platform_controls(platform, driver->controls,
1181 driver->num_controls);
1182 if (driver->dapm_routes)
1183 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1184 driver->num_dapm_routes);
1186 /* mark platform as probed and add to card platform list */
1187 platform->probed = 1;
1188 list_add(&platform->card_list, &card->platform_dev_list);
1189 list_add(&platform->dapm.list, &card->dapm_list);
1194 soc_cleanup_platform_debugfs(platform);
1195 module_put(platform->dev->driver->owner);
1200 static void rtd_release(struct device *dev)
1205 static int soc_post_component_init(struct snd_soc_card *card,
1206 struct snd_soc_codec *codec,
1207 int num, int dailess)
1209 struct snd_soc_dai_link *dai_link = NULL;
1210 struct snd_soc_aux_dev *aux_dev = NULL;
1211 struct snd_soc_pcm_runtime *rtd;
1212 const char *temp, *name;
1216 dai_link = &card->dai_link[num];
1217 rtd = &card->rtd[num];
1218 name = dai_link->name;
1220 aux_dev = &card->aux_dev[num];
1221 rtd = &card->rtd_aux[num];
1222 name = aux_dev->name;
1226 /* Make sure all DAPM widgets are instantiated */
1227 snd_soc_dapm_new_widgets(&codec->dapm);
1229 /* machine controls, routes and widgets are not prefixed */
1230 temp = codec->name_prefix;
1231 codec->name_prefix = NULL;
1233 /* do machine specific initialization */
1234 if (!dailess && dai_link->init)
1235 ret = dai_link->init(rtd);
1236 else if (dailess && aux_dev->init)
1237 ret = aux_dev->init(&codec->dapm);
1239 dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
1242 codec->name_prefix = temp;
1244 /* register the rtd device */
1247 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1250 device_initialize(rtd->dev);
1251 rtd->dev->parent = card->dev;
1252 rtd->dev->release = rtd_release;
1253 rtd->dev->init_name = name;
1254 dev_set_drvdata(rtd->dev, rtd);
1255 mutex_init(&rtd->pcm_mutex);
1256 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1257 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1258 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1259 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1260 ret = device_add(rtd->dev);
1262 /* calling put_device() here to free the rtd->dev */
1263 put_device(rtd->dev);
1265 "ASoC: failed to register runtime device: %d\n", ret);
1268 rtd->dev_registered = 1;
1270 /* add DAPM sysfs entries for this codec */
1271 ret = snd_soc_dapm_sys_add(rtd->dev);
1274 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
1276 /* add codec sysfs entries */
1277 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1280 "ASoC: failed to add codec sysfs files: %d\n", ret);
1282 #ifdef CONFIG_DEBUG_FS
1283 /* add DPCM sysfs entries */
1284 if (!dailess && !dai_link->dynamic)
1287 ret = soc_dpcm_debugfs_add(rtd);
1289 dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
1296 static int soc_probe_link_components(struct snd_soc_card *card, int num,
1299 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1300 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1301 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1302 struct snd_soc_platform *platform = rtd->platform;
1305 /* probe the CPU-side component, if it is a CODEC */
1306 if (cpu_dai->codec &&
1307 !cpu_dai->codec->probed &&
1308 cpu_dai->codec->driver->probe_order == order) {
1309 ret = soc_probe_codec(card, cpu_dai->codec);
1314 /* probe the CODEC-side component */
1315 if (!codec_dai->codec->probed &&
1316 codec_dai->codec->driver->probe_order == order) {
1317 ret = soc_probe_codec(card, codec_dai->codec);
1322 /* probe the platform */
1323 if (!platform->probed &&
1324 platform->driver->probe_order == order) {
1325 ret = soc_probe_platform(card, platform);
1333 static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1335 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1336 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1337 struct snd_soc_codec *codec = rtd->codec;
1338 struct snd_soc_platform *platform = rtd->platform;
1339 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1340 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1341 struct snd_soc_dapm_widget *play_w, *capture_w;
1344 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1345 card->name, num, order);
1347 /* config components */
1348 cpu_dai->platform = platform;
1349 codec_dai->card = card;
1350 cpu_dai->card = card;
1352 /* set default power off timeout */
1353 rtd->pmdown_time = pmdown_time;
1355 /* probe the cpu_dai */
1356 if (!cpu_dai->probed &&
1357 cpu_dai->driver->probe_order == order) {
1358 if (!cpu_dai->codec) {
1359 cpu_dai->dapm.card = card;
1360 if (!try_module_get(cpu_dai->dev->driver->owner))
1363 list_add(&cpu_dai->dapm.list, &card->dapm_list);
1364 snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1367 if (cpu_dai->driver->probe) {
1368 ret = cpu_dai->driver->probe(cpu_dai);
1370 dev_err(cpu_dai->dev,
1371 "ASoC: failed to probe CPU DAI %s: %d\n",
1372 cpu_dai->name, ret);
1373 module_put(cpu_dai->dev->driver->owner);
1377 cpu_dai->probed = 1;
1378 /* mark cpu_dai as probed and add to card dai list */
1379 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1382 /* probe the CODEC DAI */
1383 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1384 if (codec_dai->driver->probe) {
1385 ret = codec_dai->driver->probe(codec_dai);
1387 dev_err(codec_dai->dev,
1388 "ASoC: failed to probe CODEC DAI %s: %d\n",
1389 codec_dai->name, ret);
1394 /* mark codec_dai as probed and add to card dai list */
1395 codec_dai->probed = 1;
1396 list_add(&codec_dai->card_list, &card->dai_dev_list);
1399 /* complete DAI probe during last probe */
1400 if (order != SND_SOC_COMP_ORDER_LAST)
1403 ret = soc_post_component_init(card, codec, num, 0);
1407 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1409 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1412 if (cpu_dai->driver->compress_dai) {
1413 /*create compress_device"*/
1414 ret = soc_new_compress(rtd, num);
1416 dev_err(card->dev, "ASoC: can't create compress %s\n",
1417 dai_link->stream_name);
1422 if (!dai_link->params) {
1423 /* create the pcm */
1424 ret = soc_new_pcm(rtd, num);
1426 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1427 dai_link->stream_name, ret);
1431 /* link the DAI widgets */
1432 play_w = codec_dai->playback_widget;
1433 capture_w = cpu_dai->capture_widget;
1434 if (play_w && capture_w) {
1435 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1438 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1439 play_w->name, capture_w->name, ret);
1444 play_w = cpu_dai->playback_widget;
1445 capture_w = codec_dai->capture_widget;
1446 if (play_w && capture_w) {
1447 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1450 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1451 play_w->name, capture_w->name, ret);
1458 /* add platform data for AC97 devices */
1459 if (rtd->codec_dai->driver->ac97_control)
1460 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1465 #ifdef CONFIG_SND_SOC_AC97_BUS
1466 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1470 /* Only instantiate AC97 if not already done by the adaptor
1471 * for the generic AC97 subsystem.
1473 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
1475 * It is possible that the AC97 device is already registered to
1476 * the device subsystem. This happens when the device is created
1477 * via snd_ac97_mixer(). Currently only SoC codec that does so
1478 * is the generic AC97 glue but others migh emerge.
1480 * In those cases we don't try to register the device again.
1482 if (!rtd->codec->ac97_created)
1485 ret = soc_ac97_dev_register(rtd->codec);
1487 dev_err(rtd->codec->dev,
1488 "ASoC: AC97 device register failed: %d\n", ret);
1492 rtd->codec->ac97_registered = 1;
1497 static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1499 if (codec->ac97_registered) {
1500 soc_ac97_dev_unregister(codec);
1501 codec->ac97_registered = 0;
1506 static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1508 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1509 struct snd_soc_codec *codec;
1511 /* find CODEC from registered CODECs*/
1512 list_for_each_entry(codec, &codec_list, list) {
1513 if (!strcmp(codec->name, aux_dev->codec_name))
1517 dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
1519 return -EPROBE_DEFER;
1522 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1524 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1525 struct snd_soc_codec *codec;
1528 /* find CODEC from registered CODECs*/
1529 list_for_each_entry(codec, &codec_list, list) {
1530 if (!strcmp(codec->name, aux_dev->codec_name)) {
1531 if (codec->probed) {
1533 "ASoC: codec already probed");
1540 /* codec not found */
1541 dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
1542 return -EPROBE_DEFER;
1545 ret = soc_probe_codec(card, codec);
1549 ret = soc_post_component_init(card, codec, num, 1);
1555 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1557 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1558 struct snd_soc_codec *codec = rtd->codec;
1560 /* unregister the rtd device */
1561 if (rtd->dev_registered) {
1562 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1563 device_unregister(rtd->dev);
1564 rtd->dev_registered = 0;
1567 if (codec && codec->probed)
1568 soc_remove_codec(codec);
1571 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1572 enum snd_soc_compress_type compress_type)
1576 if (codec->cache_init)
1579 /* override the compress_type if necessary */
1580 if (compress_type && codec->compress_type != compress_type)
1581 codec->compress_type = compress_type;
1582 ret = snd_soc_cache_init(codec);
1585 "ASoC: Failed to set cache compression type: %d\n",
1589 codec->cache_init = 1;
1593 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1595 struct snd_soc_codec *codec;
1596 struct snd_soc_codec_conf *codec_conf;
1597 enum snd_soc_compress_type compress_type;
1598 struct snd_soc_dai_link *dai_link;
1599 int ret, i, order, dai_fmt;
1601 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1604 for (i = 0; i < card->num_links; i++) {
1605 ret = soc_bind_dai_link(card, i);
1610 /* check aux_devs too */
1611 for (i = 0; i < card->num_aux_devs; i++) {
1612 ret = soc_check_aux_dev(card, i);
1617 /* initialize the register cache for each available codec */
1618 list_for_each_entry(codec, &codec_list, list) {
1619 if (codec->cache_init)
1621 /* by default we don't override the compress_type */
1623 /* check to see if we need to override the compress_type */
1624 for (i = 0; i < card->num_configs; ++i) {
1625 codec_conf = &card->codec_conf[i];
1626 if (!strcmp(codec->name, codec_conf->dev_name)) {
1627 compress_type = codec_conf->compress_type;
1628 if (compress_type && compress_type
1629 != codec->compress_type)
1633 ret = snd_soc_init_codec_cache(codec, compress_type);
1638 /* card bind complete so register a sound card */
1639 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1640 card->owner, 0, &card->snd_card);
1643 "ASoC: can't create sound card for card %s: %d\n",
1647 card->snd_card->dev = card->dev;
1649 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1650 card->dapm.dev = card->dev;
1651 card->dapm.card = card;
1652 list_add(&card->dapm.list, &card->dapm_list);
1654 #ifdef CONFIG_DEBUG_FS
1655 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1658 #ifdef CONFIG_PM_SLEEP
1659 /* deferred resume work */
1660 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1663 if (card->dapm_widgets)
1664 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1665 card->num_dapm_widgets);
1667 /* initialise the sound card only once */
1669 ret = card->probe(card);
1671 goto card_probe_error;
1674 /* probe all components used by DAI links on this card */
1675 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1677 for (i = 0; i < card->num_links; i++) {
1678 ret = soc_probe_link_components(card, i, order);
1681 "ASoC: failed to instantiate card %d\n",
1688 /* probe all DAI links on this card */
1689 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1691 for (i = 0; i < card->num_links; i++) {
1692 ret = soc_probe_link_dais(card, i, order);
1695 "ASoC: failed to instantiate card %d\n",
1702 for (i = 0; i < card->num_aux_devs; i++) {
1703 ret = soc_probe_aux_dev(card, i);
1706 "ASoC: failed to add auxiliary devices %d\n",
1708 goto probe_aux_dev_err;
1712 snd_soc_dapm_link_dai_widgets(card);
1715 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1717 if (card->dapm_routes)
1718 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1719 card->num_dapm_routes);
1721 snd_soc_dapm_new_widgets(&card->dapm);
1723 for (i = 0; i < card->num_links; i++) {
1724 dai_link = &card->dai_link[i];
1725 dai_fmt = dai_link->dai_fmt;
1728 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1730 if (ret != 0 && ret != -ENOTSUPP)
1731 dev_warn(card->rtd[i].codec_dai->dev,
1732 "ASoC: Failed to set DAI format: %d\n",
1736 /* If this is a regular CPU link there will be a platform */
1738 (dai_link->platform_name || dai_link->platform_of_node)) {
1739 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1741 if (ret != 0 && ret != -ENOTSUPP)
1742 dev_warn(card->rtd[i].cpu_dai->dev,
1743 "ASoC: Failed to set DAI format: %d\n",
1745 } else if (dai_fmt) {
1746 /* Flip the polarity for the "CPU" end */
1747 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1748 switch (dai_link->dai_fmt &
1749 SND_SOC_DAIFMT_MASTER_MASK) {
1750 case SND_SOC_DAIFMT_CBM_CFM:
1751 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1753 case SND_SOC_DAIFMT_CBM_CFS:
1754 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1756 case SND_SOC_DAIFMT_CBS_CFM:
1757 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1759 case SND_SOC_DAIFMT_CBS_CFS:
1760 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1764 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1766 if (ret != 0 && ret != -ENOTSUPP)
1767 dev_warn(card->rtd[i].cpu_dai->dev,
1768 "ASoC: Failed to set DAI format: %d\n",
1773 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1775 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1776 "%s", card->long_name ? card->long_name : card->name);
1777 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1778 "%s", card->driver_name ? card->driver_name : card->name);
1779 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1780 switch (card->snd_card->driver[i]) {
1786 if (!isalnum(card->snd_card->driver[i]))
1787 card->snd_card->driver[i] = '_';
1792 if (card->late_probe) {
1793 ret = card->late_probe(card);
1795 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
1797 goto probe_aux_dev_err;
1801 snd_soc_dapm_new_widgets(&card->dapm);
1803 if (card->fully_routed)
1804 list_for_each_entry(codec, &card->codec_dev_list, card_list)
1805 snd_soc_dapm_auto_nc_codec_pins(codec);
1807 ret = snd_card_register(card->snd_card);
1809 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1811 goto probe_aux_dev_err;
1814 #ifdef CONFIG_SND_SOC_AC97_BUS
1815 /* register any AC97 codecs */
1816 for (i = 0; i < card->num_rtd; i++) {
1817 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1820 "ASoC: failed to register AC97: %d\n", ret);
1822 soc_unregister_ac97_dai_link(card->rtd[i].codec);
1823 goto probe_aux_dev_err;
1828 card->instantiated = 1;
1829 snd_soc_dapm_sync(&card->dapm);
1830 mutex_unlock(&card->mutex);
1835 for (i = 0; i < card->num_aux_devs; i++)
1836 soc_remove_aux_dev(card, i);
1839 soc_remove_dai_links(card);
1845 snd_card_free(card->snd_card);
1848 mutex_unlock(&card->mutex);
1853 /* probes a new socdev */
1854 static int soc_probe(struct platform_device *pdev)
1856 struct snd_soc_card *card = platform_get_drvdata(pdev);
1859 * no card, so machine driver should be registering card
1860 * we should not be here in that case so ret error
1865 dev_warn(&pdev->dev,
1866 "ASoC: machine %s should use snd_soc_register_card()\n",
1869 /* Bodge while we unpick instantiation */
1870 card->dev = &pdev->dev;
1872 return snd_soc_register_card(card);
1875 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1879 /* make sure any delayed work runs */
1880 for (i = 0; i < card->num_rtd; i++) {
1881 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1882 flush_delayed_work(&rtd->delayed_work);
1885 /* remove auxiliary devices */
1886 for (i = 0; i < card->num_aux_devs; i++)
1887 soc_remove_aux_dev(card, i);
1889 /* remove and free each DAI */
1890 soc_remove_dai_links(card);
1892 soc_cleanup_card_debugfs(card);
1894 /* remove the card */
1898 snd_soc_dapm_free(&card->dapm);
1900 snd_card_free(card->snd_card);
1905 /* removes a socdev */
1906 static int soc_remove(struct platform_device *pdev)
1908 struct snd_soc_card *card = platform_get_drvdata(pdev);
1910 snd_soc_unregister_card(card);
1914 int snd_soc_poweroff(struct device *dev)
1916 struct snd_soc_card *card = dev_get_drvdata(dev);
1919 if (!card->instantiated)
1922 /* Flush out pmdown_time work - we actually do want to run it
1923 * now, we're shutting down so no imminent restart. */
1924 for (i = 0; i < card->num_rtd; i++) {
1925 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1926 flush_delayed_work(&rtd->delayed_work);
1929 snd_soc_dapm_shutdown(card);
1933 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1935 const struct dev_pm_ops snd_soc_pm_ops = {
1936 .suspend = snd_soc_suspend,
1937 .resume = snd_soc_resume,
1938 .freeze = snd_soc_suspend,
1939 .thaw = snd_soc_resume,
1940 .poweroff = snd_soc_poweroff,
1941 .restore = snd_soc_resume,
1943 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1945 /* ASoC platform driver */
1946 static struct platform_driver soc_driver = {
1948 .name = "soc-audio",
1949 .owner = THIS_MODULE,
1950 .pm = &snd_soc_pm_ops,
1953 .remove = soc_remove,
1957 * snd_soc_codec_volatile_register: Report if a register is volatile.
1959 * @codec: CODEC to query.
1960 * @reg: Register to query.
1962 * Boolean function indiciating if a CODEC register is volatile.
1964 int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1967 if (codec->volatile_register)
1968 return codec->volatile_register(codec, reg);
1972 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1975 * snd_soc_codec_readable_register: Report if a register is readable.
1977 * @codec: CODEC to query.
1978 * @reg: Register to query.
1980 * Boolean function indicating if a CODEC register is readable.
1982 int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1985 if (codec->readable_register)
1986 return codec->readable_register(codec, reg);
1990 EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1993 * snd_soc_codec_writable_register: Report if a register is writable.
1995 * @codec: CODEC to query.
1996 * @reg: Register to query.
1998 * Boolean function indicating if a CODEC register is writable.
2000 int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
2003 if (codec->writable_register)
2004 return codec->writable_register(codec, reg);
2008 EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
2010 int snd_soc_platform_read(struct snd_soc_platform *platform,
2015 if (!platform->driver->read) {
2016 dev_err(platform->dev, "ASoC: platform has no read back\n");
2020 ret = platform->driver->read(platform, reg);
2021 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
2022 trace_snd_soc_preg_read(platform, reg, ret);
2026 EXPORT_SYMBOL_GPL(snd_soc_platform_read);
2028 int snd_soc_platform_write(struct snd_soc_platform *platform,
2029 unsigned int reg, unsigned int val)
2031 if (!platform->driver->write) {
2032 dev_err(platform->dev, "ASoC: platform has no write back\n");
2036 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
2037 trace_snd_soc_preg_write(platform, reg, val);
2038 return platform->driver->write(platform, reg, val);
2040 EXPORT_SYMBOL_GPL(snd_soc_platform_write);
2043 * snd_soc_new_ac97_codec - initailise AC97 device
2044 * @codec: audio codec
2045 * @ops: AC97 bus operations
2046 * @num: AC97 codec number
2048 * Initialises AC97 codec resources for use by ad-hoc devices only.
2050 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2051 struct snd_ac97_bus_ops *ops, int num)
2053 mutex_lock(&codec->mutex);
2055 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2056 if (codec->ac97 == NULL) {
2057 mutex_unlock(&codec->mutex);
2061 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2062 if (codec->ac97->bus == NULL) {
2065 mutex_unlock(&codec->mutex);
2069 codec->ac97->bus->ops = ops;
2070 codec->ac97->num = num;
2073 * Mark the AC97 device to be created by us. This way we ensure that the
2074 * device will be registered with the device subsystem later on.
2076 codec->ac97_created = 1;
2078 mutex_unlock(&codec->mutex);
2081 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2084 * snd_soc_free_ac97_codec - free AC97 codec device
2085 * @codec: audio codec
2087 * Frees AC97 codec device resources.
2089 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2091 mutex_lock(&codec->mutex);
2092 #ifdef CONFIG_SND_SOC_AC97_BUS
2093 soc_unregister_ac97_dai_link(codec);
2095 kfree(codec->ac97->bus);
2098 codec->ac97_created = 0;
2099 mutex_unlock(&codec->mutex);
2101 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2103 unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2107 ret = codec->read(codec, reg);
2108 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
2109 trace_snd_soc_reg_read(codec, reg, ret);
2113 EXPORT_SYMBOL_GPL(snd_soc_read);
2115 unsigned int snd_soc_write(struct snd_soc_codec *codec,
2116 unsigned int reg, unsigned int val)
2118 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
2119 trace_snd_soc_reg_write(codec, reg, val);
2120 return codec->write(codec, reg, val);
2122 EXPORT_SYMBOL_GPL(snd_soc_write);
2124 unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
2125 unsigned int reg, const void *data, size_t len)
2127 return codec->bulk_write_raw(codec, reg, data, len);
2129 EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
2132 * snd_soc_update_bits - update codec register bits
2133 * @codec: audio codec
2134 * @reg: codec register
2135 * @mask: register mask
2138 * Writes new register value.
2140 * Returns 1 for change, 0 for no change, or negative error code.
2142 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
2143 unsigned int mask, unsigned int value)
2146 unsigned int old, new;
2149 if (codec->using_regmap) {
2150 ret = regmap_update_bits_check(codec->control_data, reg,
2151 mask, value, &change);
2153 ret = snd_soc_read(codec, reg);
2158 new = (old & ~mask) | (value & mask);
2159 change = old != new;
2161 ret = snd_soc_write(codec, reg, new);
2169 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2172 * snd_soc_update_bits_locked - update codec register bits
2173 * @codec: audio codec
2174 * @reg: codec register
2175 * @mask: register mask
2178 * Writes new register value, and takes the codec mutex.
2180 * Returns 1 for change else 0.
2182 int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2183 unsigned short reg, unsigned int mask,
2188 mutex_lock(&codec->mutex);
2189 change = snd_soc_update_bits(codec, reg, mask, value);
2190 mutex_unlock(&codec->mutex);
2194 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
2197 * snd_soc_test_bits - test register for change
2198 * @codec: audio codec
2199 * @reg: codec register
2200 * @mask: register mask
2203 * Tests a register with a new value and checks if the new value is
2204 * different from the old value.
2206 * Returns 1 for change else 0.
2208 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
2209 unsigned int mask, unsigned int value)
2212 unsigned int old, new;
2214 old = snd_soc_read(codec, reg);
2215 new = (old & ~mask) | value;
2216 change = old != new;
2220 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2223 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2224 * @substream: the pcm substream
2225 * @hw: the hardware parameters
2227 * Sets the substream runtime hardware parameters.
2229 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2230 const struct snd_pcm_hardware *hw)
2232 struct snd_pcm_runtime *runtime = substream->runtime;
2233 runtime->hw.info = hw->info;
2234 runtime->hw.formats = hw->formats;
2235 runtime->hw.period_bytes_min = hw->period_bytes_min;
2236 runtime->hw.period_bytes_max = hw->period_bytes_max;
2237 runtime->hw.periods_min = hw->periods_min;
2238 runtime->hw.periods_max = hw->periods_max;
2239 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2240 runtime->hw.fifo_size = hw->fifo_size;
2243 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2246 * snd_soc_cnew - create new control
2247 * @_template: control template
2248 * @data: control private data
2249 * @long_name: control long name
2250 * @prefix: control name prefix
2252 * Create a new mixer control from a template control.
2254 * Returns 0 for success, else error.
2256 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2257 void *data, const char *long_name,
2260 struct snd_kcontrol_new template;
2261 struct snd_kcontrol *kcontrol;
2265 memcpy(&template, _template, sizeof(template));
2269 long_name = template.name;
2272 name_len = strlen(long_name) + strlen(prefix) + 2;
2273 name = kmalloc(name_len, GFP_KERNEL);
2277 snprintf(name, name_len, "%s %s", prefix, long_name);
2279 template.name = name;
2281 template.name = long_name;
2284 kcontrol = snd_ctl_new1(&template, data);
2290 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2292 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2293 const struct snd_kcontrol_new *controls, int num_controls,
2294 const char *prefix, void *data)
2298 for (i = 0; i < num_controls; i++) {
2299 const struct snd_kcontrol_new *control = &controls[i];
2300 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2301 control->name, prefix));
2303 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2304 control->name, err);
2313 * snd_soc_add_codec_controls - add an array of controls to a codec.
2314 * Convenience function to add a list of controls. Many codecs were
2315 * duplicating this code.
2317 * @codec: codec to add controls to
2318 * @controls: array of controls to add
2319 * @num_controls: number of elements in the array
2321 * Return 0 for success, else error.
2323 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2324 const struct snd_kcontrol_new *controls, int num_controls)
2326 struct snd_card *card = codec->card->snd_card;
2328 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2329 codec->name_prefix, codec);
2331 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2334 * snd_soc_add_platform_controls - add an array of controls to a platform.
2335 * Convenience function to add a list of controls.
2337 * @platform: platform to add controls to
2338 * @controls: array of controls to add
2339 * @num_controls: number of elements in the array
2341 * Return 0 for success, else error.
2343 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2344 const struct snd_kcontrol_new *controls, int num_controls)
2346 struct snd_card *card = platform->card->snd_card;
2348 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2351 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2354 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2355 * Convenience function to add a list of controls.
2357 * @soc_card: SoC card to add controls to
2358 * @controls: array of controls to add
2359 * @num_controls: number of elements in the array
2361 * Return 0 for success, else error.
2363 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2364 const struct snd_kcontrol_new *controls, int num_controls)
2366 struct snd_card *card = soc_card->snd_card;
2368 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2371 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2374 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2375 * Convienience function to add a list of controls.
2377 * @dai: DAI to add controls to
2378 * @controls: array of controls to add
2379 * @num_controls: number of elements in the array
2381 * Return 0 for success, else error.
2383 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2384 const struct snd_kcontrol_new *controls, int num_controls)
2386 struct snd_card *card = dai->card->snd_card;
2388 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2391 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2394 * snd_soc_info_enum_double - enumerated double mixer info callback
2395 * @kcontrol: mixer control
2396 * @uinfo: control element information
2398 * Callback to provide information about a double enumerated
2401 * Returns 0 for success.
2403 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2404 struct snd_ctl_elem_info *uinfo)
2406 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2408 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2409 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2410 uinfo->value.enumerated.items = e->max;
2412 if (uinfo->value.enumerated.item > e->max - 1)
2413 uinfo->value.enumerated.item = e->max - 1;
2414 strcpy(uinfo->value.enumerated.name,
2415 e->texts[uinfo->value.enumerated.item]);
2418 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2421 * snd_soc_get_enum_double - enumerated double mixer get callback
2422 * @kcontrol: mixer control
2423 * @ucontrol: control element information
2425 * Callback to get the value of a double enumerated mixer.
2427 * Returns 0 for success.
2429 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2430 struct snd_ctl_elem_value *ucontrol)
2432 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2433 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2436 val = snd_soc_read(codec, e->reg);
2437 ucontrol->value.enumerated.item[0]
2438 = (val >> e->shift_l) & e->mask;
2439 if (e->shift_l != e->shift_r)
2440 ucontrol->value.enumerated.item[1] =
2441 (val >> e->shift_r) & e->mask;
2445 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2448 * snd_soc_put_enum_double - enumerated double mixer put callback
2449 * @kcontrol: mixer control
2450 * @ucontrol: control element information
2452 * Callback to set the value of a double enumerated mixer.
2454 * Returns 0 for success.
2456 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2457 struct snd_ctl_elem_value *ucontrol)
2459 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2460 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2464 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2466 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2467 mask = e->mask << e->shift_l;
2468 if (e->shift_l != e->shift_r) {
2469 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2471 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2472 mask |= e->mask << e->shift_r;
2475 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2477 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2480 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2481 * @kcontrol: mixer control
2482 * @ucontrol: control element information
2484 * Callback to get the value of a double semi enumerated mixer.
2486 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2487 * used for handling bitfield coded enumeration for example.
2489 * Returns 0 for success.
2491 int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2492 struct snd_ctl_elem_value *ucontrol)
2494 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2495 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2496 unsigned int reg_val, val, mux;
2498 reg_val = snd_soc_read(codec, e->reg);
2499 val = (reg_val >> e->shift_l) & e->mask;
2500 for (mux = 0; mux < e->max; mux++) {
2501 if (val == e->values[mux])
2504 ucontrol->value.enumerated.item[0] = mux;
2505 if (e->shift_l != e->shift_r) {
2506 val = (reg_val >> e->shift_r) & e->mask;
2507 for (mux = 0; mux < e->max; mux++) {
2508 if (val == e->values[mux])
2511 ucontrol->value.enumerated.item[1] = mux;
2516 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2519 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2520 * @kcontrol: mixer control
2521 * @ucontrol: control element information
2523 * Callback to set the value of a double semi enumerated mixer.
2525 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2526 * used for handling bitfield coded enumeration for example.
2528 * Returns 0 for success.
2530 int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2531 struct snd_ctl_elem_value *ucontrol)
2533 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2534 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2538 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2540 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2541 mask = e->mask << e->shift_l;
2542 if (e->shift_l != e->shift_r) {
2543 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2545 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2546 mask |= e->mask << e->shift_r;
2549 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2551 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2554 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2555 * @kcontrol: mixer control
2556 * @uinfo: control element information
2558 * Callback to provide information about an external enumerated
2561 * Returns 0 for success.
2563 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2564 struct snd_ctl_elem_info *uinfo)
2566 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2568 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2570 uinfo->value.enumerated.items = e->max;
2572 if (uinfo->value.enumerated.item > e->max - 1)
2573 uinfo->value.enumerated.item = e->max - 1;
2574 strcpy(uinfo->value.enumerated.name,
2575 e->texts[uinfo->value.enumerated.item]);
2578 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2581 * snd_soc_info_volsw_ext - external single mixer info callback
2582 * @kcontrol: mixer control
2583 * @uinfo: control element information
2585 * Callback to provide information about a single external mixer control.
2587 * Returns 0 for success.
2589 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2590 struct snd_ctl_elem_info *uinfo)
2592 int max = kcontrol->private_value;
2594 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2595 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2597 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2600 uinfo->value.integer.min = 0;
2601 uinfo->value.integer.max = max;
2604 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2607 * snd_soc_info_volsw - single mixer info callback
2608 * @kcontrol: mixer control
2609 * @uinfo: control element information
2611 * Callback to provide information about a single mixer control, or a double
2612 * mixer control that spans 2 registers.
2614 * Returns 0 for success.
2616 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2617 struct snd_ctl_elem_info *uinfo)
2619 struct soc_mixer_control *mc =
2620 (struct soc_mixer_control *)kcontrol->private_value;
2623 if (!mc->platform_max)
2624 mc->platform_max = mc->max;
2625 platform_max = mc->platform_max;
2627 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2628 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2630 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2632 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2633 uinfo->value.integer.min = 0;
2634 uinfo->value.integer.max = platform_max;
2637 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2640 * snd_soc_get_volsw - single mixer get callback
2641 * @kcontrol: mixer control
2642 * @ucontrol: control element information
2644 * Callback to get the value of a single mixer control, or a double mixer
2645 * control that spans 2 registers.
2647 * Returns 0 for success.
2649 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2650 struct snd_ctl_elem_value *ucontrol)
2652 struct soc_mixer_control *mc =
2653 (struct soc_mixer_control *)kcontrol->private_value;
2654 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2655 unsigned int reg = mc->reg;
2656 unsigned int reg2 = mc->rreg;
2657 unsigned int shift = mc->shift;
2658 unsigned int rshift = mc->rshift;
2660 unsigned int mask = (1 << fls(max)) - 1;
2661 unsigned int invert = mc->invert;
2663 ucontrol->value.integer.value[0] =
2664 (snd_soc_read(codec, reg) >> shift) & mask;
2666 ucontrol->value.integer.value[0] =
2667 max - ucontrol->value.integer.value[0];
2669 if (snd_soc_volsw_is_stereo(mc)) {
2671 ucontrol->value.integer.value[1] =
2672 (snd_soc_read(codec, reg) >> rshift) & mask;
2674 ucontrol->value.integer.value[1] =
2675 (snd_soc_read(codec, reg2) >> shift) & mask;
2677 ucontrol->value.integer.value[1] =
2678 max - ucontrol->value.integer.value[1];
2683 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2686 * snd_soc_put_volsw - single mixer put callback
2687 * @kcontrol: mixer control
2688 * @ucontrol: control element information
2690 * Callback to set the value of a single mixer control, or a double mixer
2691 * control that spans 2 registers.
2693 * Returns 0 for success.
2695 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2696 struct snd_ctl_elem_value *ucontrol)
2698 struct soc_mixer_control *mc =
2699 (struct soc_mixer_control *)kcontrol->private_value;
2700 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2701 unsigned int reg = mc->reg;
2702 unsigned int reg2 = mc->rreg;
2703 unsigned int shift = mc->shift;
2704 unsigned int rshift = mc->rshift;
2706 unsigned int mask = (1 << fls(max)) - 1;
2707 unsigned int invert = mc->invert;
2710 unsigned int val2 = 0;
2711 unsigned int val, val_mask;
2713 val = (ucontrol->value.integer.value[0] & mask);
2716 val_mask = mask << shift;
2718 if (snd_soc_volsw_is_stereo(mc)) {
2719 val2 = (ucontrol->value.integer.value[1] & mask);
2723 val_mask |= mask << rshift;
2724 val |= val2 << rshift;
2726 val2 = val2 << shift;
2730 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2735 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2739 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2742 * snd_soc_get_volsw_sx - single mixer get callback
2743 * @kcontrol: mixer control
2744 * @ucontrol: control element information
2746 * Callback to get the value of a single mixer control, or a double mixer
2747 * control that spans 2 registers.
2749 * Returns 0 for success.
2751 int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2752 struct snd_ctl_elem_value *ucontrol)
2754 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2755 struct soc_mixer_control *mc =
2756 (struct soc_mixer_control *)kcontrol->private_value;
2758 unsigned int reg = mc->reg;
2759 unsigned int reg2 = mc->rreg;
2760 unsigned int shift = mc->shift;
2761 unsigned int rshift = mc->rshift;
2764 int mask = (1 << (fls(min + max) - 1)) - 1;
2766 ucontrol->value.integer.value[0] =
2767 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2769 if (snd_soc_volsw_is_stereo(mc))
2770 ucontrol->value.integer.value[1] =
2771 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2775 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2778 * snd_soc_put_volsw_sx - double mixer set callback
2779 * @kcontrol: mixer control
2780 * @uinfo: control element information
2782 * Callback to set the value of a double mixer control that spans 2 registers.
2784 * Returns 0 for success.
2786 int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2787 struct snd_ctl_elem_value *ucontrol)
2789 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2790 struct soc_mixer_control *mc =
2791 (struct soc_mixer_control *)kcontrol->private_value;
2793 unsigned int reg = mc->reg;
2794 unsigned int reg2 = mc->rreg;
2795 unsigned int shift = mc->shift;
2796 unsigned int rshift = mc->rshift;
2799 int mask = (1 << (fls(min + max) - 1)) - 1;
2801 unsigned short val, val_mask, val2 = 0;
2803 val_mask = mask << shift;
2804 val = (ucontrol->value.integer.value[0] + min) & mask;
2807 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2811 if (snd_soc_volsw_is_stereo(mc)) {
2812 val_mask = mask << rshift;
2813 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2814 val2 = val2 << rshift;
2816 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2821 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2824 * snd_soc_info_volsw_s8 - signed mixer info callback
2825 * @kcontrol: mixer control
2826 * @uinfo: control element information
2828 * Callback to provide information about a signed mixer control.
2830 * Returns 0 for success.
2832 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2833 struct snd_ctl_elem_info *uinfo)
2835 struct soc_mixer_control *mc =
2836 (struct soc_mixer_control *)kcontrol->private_value;
2840 if (!mc->platform_max)
2841 mc->platform_max = mc->max;
2842 platform_max = mc->platform_max;
2844 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2846 uinfo->value.integer.min = 0;
2847 uinfo->value.integer.max = platform_max - min;
2850 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2853 * snd_soc_get_volsw_s8 - signed mixer get callback
2854 * @kcontrol: mixer control
2855 * @ucontrol: control element information
2857 * Callback to get the value of a signed mixer control.
2859 * Returns 0 for success.
2861 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2862 struct snd_ctl_elem_value *ucontrol)
2864 struct soc_mixer_control *mc =
2865 (struct soc_mixer_control *)kcontrol->private_value;
2866 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2867 unsigned int reg = mc->reg;
2869 int val = snd_soc_read(codec, reg);
2871 ucontrol->value.integer.value[0] =
2872 ((signed char)(val & 0xff))-min;
2873 ucontrol->value.integer.value[1] =
2874 ((signed char)((val >> 8) & 0xff))-min;
2877 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2880 * snd_soc_put_volsw_sgn - signed mixer put callback
2881 * @kcontrol: mixer control
2882 * @ucontrol: control element information
2884 * Callback to set the value of a signed mixer control.
2886 * Returns 0 for success.
2888 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2889 struct snd_ctl_elem_value *ucontrol)
2891 struct soc_mixer_control *mc =
2892 (struct soc_mixer_control *)kcontrol->private_value;
2893 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2894 unsigned int reg = mc->reg;
2898 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2899 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2901 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2903 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2906 * snd_soc_info_volsw_range - single mixer info callback with range.
2907 * @kcontrol: mixer control
2908 * @uinfo: control element information
2910 * Callback to provide information, within a range, about a single
2913 * returns 0 for success.
2915 int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2916 struct snd_ctl_elem_info *uinfo)
2918 struct soc_mixer_control *mc =
2919 (struct soc_mixer_control *)kcontrol->private_value;
2923 if (!mc->platform_max)
2924 mc->platform_max = mc->max;
2925 platform_max = mc->platform_max;
2927 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2928 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2929 uinfo->value.integer.min = 0;
2930 uinfo->value.integer.max = platform_max - min;
2934 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2937 * snd_soc_put_volsw_range - single mixer put value callback with range.
2938 * @kcontrol: mixer control
2939 * @ucontrol: control element information
2941 * Callback to set the value, within a range, for a single mixer control.
2943 * Returns 0 for success.
2945 int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2946 struct snd_ctl_elem_value *ucontrol)
2948 struct soc_mixer_control *mc =
2949 (struct soc_mixer_control *)kcontrol->private_value;
2950 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2951 unsigned int reg = mc->reg;
2952 unsigned int rreg = mc->rreg;
2953 unsigned int shift = mc->shift;
2956 unsigned int mask = (1 << fls(max)) - 1;
2957 unsigned int invert = mc->invert;
2958 unsigned int val, val_mask;
2961 val = ((ucontrol->value.integer.value[0] + min) & mask);
2964 val_mask = mask << shift;
2967 ret = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2971 if (snd_soc_volsw_is_stereo(mc)) {
2972 val = ((ucontrol->value.integer.value[1] + min) & mask);
2975 val_mask = mask << shift;
2978 ret = snd_soc_update_bits_locked(codec, rreg, val_mask, val);
2983 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2986 * snd_soc_get_volsw_range - single mixer get callback with range
2987 * @kcontrol: mixer control
2988 * @ucontrol: control element information
2990 * Callback to get the value, within a range, of a single mixer control.
2992 * Returns 0 for success.
2994 int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2995 struct snd_ctl_elem_value *ucontrol)
2997 struct soc_mixer_control *mc =
2998 (struct soc_mixer_control *)kcontrol->private_value;
2999 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3000 unsigned int reg = mc->reg;
3001 unsigned int rreg = mc->rreg;
3002 unsigned int shift = mc->shift;
3005 unsigned int mask = (1 << fls(max)) - 1;
3006 unsigned int invert = mc->invert;
3008 ucontrol->value.integer.value[0] =
3009 (snd_soc_read(codec, reg) >> shift) & mask;
3011 ucontrol->value.integer.value[0] =
3012 max - ucontrol->value.integer.value[0];
3013 ucontrol->value.integer.value[0] =
3014 ucontrol->value.integer.value[0] - min;
3016 if (snd_soc_volsw_is_stereo(mc)) {
3017 ucontrol->value.integer.value[1] =
3018 (snd_soc_read(codec, rreg) >> shift) & mask;
3020 ucontrol->value.integer.value[1] =
3021 max - ucontrol->value.integer.value[1];
3022 ucontrol->value.integer.value[1] =
3023 ucontrol->value.integer.value[1] - min;
3028 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3031 * snd_soc_limit_volume - Set new limit to an existing volume control.
3033 * @codec: where to look for the control
3034 * @name: Name of the control
3035 * @max: new maximum limit
3037 * Return 0 for success, else error.
3039 int snd_soc_limit_volume(struct snd_soc_codec *codec,
3040 const char *name, int max)
3042 struct snd_card *card = codec->card->snd_card;
3043 struct snd_kcontrol *kctl;
3044 struct soc_mixer_control *mc;
3048 /* Sanity check for name and max */
3049 if (unlikely(!name || max <= 0))
3052 list_for_each_entry(kctl, &card->controls, list) {
3053 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3059 mc = (struct soc_mixer_control *)kctl->private_value;
3060 if (max <= mc->max) {
3061 mc->platform_max = max;
3067 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3069 int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3070 struct snd_ctl_elem_info *uinfo)
3072 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3073 struct soc_bytes *params = (void *)kcontrol->private_value;
3075 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3076 uinfo->count = params->num_regs * codec->val_bytes;
3080 EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3082 int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3083 struct snd_ctl_elem_value *ucontrol)
3085 struct soc_bytes *params = (void *)kcontrol->private_value;
3086 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3089 if (codec->using_regmap)
3090 ret = regmap_raw_read(codec->control_data, params->base,
3091 ucontrol->value.bytes.data,
3092 params->num_regs * codec->val_bytes);
3096 /* Hide any masked bytes to ensure consistent data reporting */
3097 if (ret == 0 && params->mask) {
3098 switch (codec->val_bytes) {
3100 ucontrol->value.bytes.data[0] &= ~params->mask;
3103 ((u16 *)(&ucontrol->value.bytes.data))[0]
3107 ((u32 *)(&ucontrol->value.bytes.data))[0]
3117 EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3119 int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3120 struct snd_ctl_elem_value *ucontrol)
3122 struct soc_bytes *params = (void *)kcontrol->private_value;
3123 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3128 if (!codec->using_regmap)
3131 len = params->num_regs * codec->val_bytes;
3133 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3138 * If we've got a mask then we need to preserve the register
3139 * bits. We shouldn't modify the incoming data so take a
3143 ret = regmap_read(codec->control_data, params->base, &val);
3147 val &= params->mask;
3149 switch (codec->val_bytes) {
3151 ((u8 *)data)[0] &= ~params->mask;
3152 ((u8 *)data)[0] |= val;
3155 ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
3156 ((u16 *)data)[0] |= cpu_to_be16(val);
3159 ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
3160 ((u32 *)data)[0] |= cpu_to_be32(val);
3168 ret = regmap_raw_write(codec->control_data, params->base,
3176 EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3179 * snd_soc_info_xr_sx - signed multi register info callback
3180 * @kcontrol: mreg control
3181 * @uinfo: control element information
3183 * Callback to provide information of a control that can
3184 * span multiple codec registers which together
3185 * forms a single signed value in a MSB/LSB manner.
3187 * Returns 0 for success.
3189 int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3190 struct snd_ctl_elem_info *uinfo)
3192 struct soc_mreg_control *mc =
3193 (struct soc_mreg_control *)kcontrol->private_value;
3194 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3196 uinfo->value.integer.min = mc->min;
3197 uinfo->value.integer.max = mc->max;
3201 EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3204 * snd_soc_get_xr_sx - signed multi register get callback
3205 * @kcontrol: mreg control
3206 * @ucontrol: control element information
3208 * Callback to get the value of a control that can span
3209 * multiple codec registers which together forms a single
3210 * signed value in a MSB/LSB manner. The control supports
3211 * specifying total no of bits used to allow for bitfields
3212 * across the multiple codec registers.
3214 * Returns 0 for success.
3216 int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3217 struct snd_ctl_elem_value *ucontrol)
3219 struct soc_mreg_control *mc =
3220 (struct soc_mreg_control *)kcontrol->private_value;
3221 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3222 unsigned int regbase = mc->regbase;
3223 unsigned int regcount = mc->regcount;
3224 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3225 unsigned int regwmask = (1<<regwshift)-1;
3226 unsigned int invert = mc->invert;
3227 unsigned long mask = (1UL<<mc->nbits)-1;
3231 unsigned long regval;
3234 for (i = 0; i < regcount; i++) {
3235 regval = snd_soc_read(codec, regbase+i) & regwmask;
3236 val |= regval << (regwshift*(regcount-i-1));
3239 if (min < 0 && val > max)
3243 ucontrol->value.integer.value[0] = val;
3247 EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3250 * snd_soc_put_xr_sx - signed multi register get callback
3251 * @kcontrol: mreg control
3252 * @ucontrol: control element information
3254 * Callback to set the value of a control that can span
3255 * multiple codec registers which together forms a single
3256 * signed value in a MSB/LSB manner. The control supports
3257 * specifying total no of bits used to allow for bitfields
3258 * across the multiple codec registers.
3260 * Returns 0 for success.
3262 int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3263 struct snd_ctl_elem_value *ucontrol)
3265 struct soc_mreg_control *mc =
3266 (struct soc_mreg_control *)kcontrol->private_value;
3267 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3268 unsigned int regbase = mc->regbase;
3269 unsigned int regcount = mc->regcount;
3270 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3271 unsigned int regwmask = (1<<regwshift)-1;
3272 unsigned int invert = mc->invert;
3273 unsigned long mask = (1UL<<mc->nbits)-1;
3275 long val = ucontrol->value.integer.value[0];
3276 unsigned int i, regval, regmask;
3282 for (i = 0; i < regcount; i++) {
3283 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3284 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3285 err = snd_soc_update_bits_locked(codec, regbase+i,
3293 EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3296 * snd_soc_get_strobe - strobe get callback
3297 * @kcontrol: mixer control
3298 * @ucontrol: control element information
3300 * Callback get the value of a strobe mixer control.
3302 * Returns 0 for success.
3304 int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3305 struct snd_ctl_elem_value *ucontrol)
3307 struct soc_mixer_control *mc =
3308 (struct soc_mixer_control *)kcontrol->private_value;
3309 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3310 unsigned int reg = mc->reg;
3311 unsigned int shift = mc->shift;
3312 unsigned int mask = 1 << shift;
3313 unsigned int invert = mc->invert != 0;
3314 unsigned int val = snd_soc_read(codec, reg) & mask;
3316 if (shift != 0 && val != 0)
3318 ucontrol->value.enumerated.item[0] = val ^ invert;
3322 EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3325 * snd_soc_put_strobe - strobe put callback
3326 * @kcontrol: mixer control
3327 * @ucontrol: control element information
3329 * Callback strobe a register bit to high then low (or the inverse)
3330 * in one pass of a single mixer enum control.
3332 * Returns 1 for success.
3334 int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3335 struct snd_ctl_elem_value *ucontrol)
3337 struct soc_mixer_control *mc =
3338 (struct soc_mixer_control *)kcontrol->private_value;
3339 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3340 unsigned int reg = mc->reg;
3341 unsigned int shift = mc->shift;
3342 unsigned int mask = 1 << shift;
3343 unsigned int invert = mc->invert != 0;
3344 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3345 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3346 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3349 err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3353 err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3356 EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3359 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3361 * @clk_id: DAI specific clock ID
3362 * @freq: new clock frequency in Hz
3363 * @dir: new clock direction - input/output.
3365 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3367 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3368 unsigned int freq, int dir)
3370 if (dai->driver && dai->driver->ops->set_sysclk)
3371 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3372 else if (dai->codec && dai->codec->driver->set_sysclk)
3373 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3378 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3381 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3383 * @clk_id: DAI specific clock ID
3384 * @source: Source for the clock
3385 * @freq: new clock frequency in Hz
3386 * @dir: new clock direction - input/output.
3388 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3390 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3391 int source, unsigned int freq, int dir)
3393 if (codec->driver->set_sysclk)
3394 return codec->driver->set_sysclk(codec, clk_id, source,
3399 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3402 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3404 * @div_id: DAI specific clock divider ID
3405 * @div: new clock divisor.
3407 * Configures the clock dividers. This is used to derive the best DAI bit and
3408 * frame clocks from the system or master clock. It's best to set the DAI bit
3409 * and frame clocks as low as possible to save system power.
3411 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3412 int div_id, int div)
3414 if (dai->driver && dai->driver->ops->set_clkdiv)
3415 return dai->driver->ops->set_clkdiv(dai, div_id, div);
3419 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3422 * snd_soc_dai_set_pll - configure DAI PLL.
3424 * @pll_id: DAI specific PLL ID
3425 * @source: DAI specific source for the PLL
3426 * @freq_in: PLL input clock frequency in Hz
3427 * @freq_out: requested PLL output clock frequency in Hz
3429 * Configures and enables PLL to generate output clock based on input clock.
3431 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3432 unsigned int freq_in, unsigned int freq_out)
3434 if (dai->driver && dai->driver->ops->set_pll)
3435 return dai->driver->ops->set_pll(dai, pll_id, source,
3437 else if (dai->codec && dai->codec->driver->set_pll)
3438 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3443 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3446 * snd_soc_codec_set_pll - configure codec PLL.
3448 * @pll_id: DAI specific PLL ID
3449 * @source: DAI specific source for the PLL
3450 * @freq_in: PLL input clock frequency in Hz
3451 * @freq_out: requested PLL output clock frequency in Hz
3453 * Configures and enables PLL to generate output clock based on input clock.
3455 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3456 unsigned int freq_in, unsigned int freq_out)
3458 if (codec->driver->set_pll)
3459 return codec->driver->set_pll(codec, pll_id, source,
3464 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3467 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3469 * @fmt: SND_SOC_DAIFMT_ format value.
3471 * Configures the DAI hardware format and clocking.
3473 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3475 if (dai->driver == NULL)
3477 if (dai->driver->ops->set_fmt == NULL)
3479 return dai->driver->ops->set_fmt(dai, fmt);
3481 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3484 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3486 * @tx_mask: bitmask representing active TX slots.
3487 * @rx_mask: bitmask representing active RX slots.
3488 * @slots: Number of slots in use.
3489 * @slot_width: Width in bits for each slot.
3491 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3494 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3495 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
3497 if (dai->driver && dai->driver->ops->set_tdm_slot)
3498 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3503 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3506 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3508 * @tx_num: how many TX channels
3509 * @tx_slot: pointer to an array which imply the TX slot number channel
3511 * @rx_num: how many RX channels
3512 * @rx_slot: pointer to an array which imply the RX slot number channel
3515 * configure the relationship between channel number and TDM slot number.
3517 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3518 unsigned int tx_num, unsigned int *tx_slot,
3519 unsigned int rx_num, unsigned int *rx_slot)
3521 if (dai->driver && dai->driver->ops->set_channel_map)
3522 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3527 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3530 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3532 * @tristate: tristate enable
3534 * Tristates the DAI so that others can use it.
3536 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3538 if (dai->driver && dai->driver->ops->set_tristate)
3539 return dai->driver->ops->set_tristate(dai, tristate);
3543 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3546 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3548 * @mute: mute enable
3549 * @direction: stream to mute
3551 * Mutes the DAI DAC.
3553 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3559 if (dai->driver->ops->mute_stream)
3560 return dai->driver->ops->mute_stream(dai, mute, direction);
3561 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3562 dai->driver->ops->digital_mute)
3563 return dai->driver->ops->digital_mute(dai, mute);
3567 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3570 * snd_soc_register_card - Register a card with the ASoC core
3572 * @card: Card to register
3575 int snd_soc_register_card(struct snd_soc_card *card)
3579 if (!card->name || !card->dev)
3582 for (i = 0; i < card->num_links; i++) {
3583 struct snd_soc_dai_link *link = &card->dai_link[i];
3586 * Codec must be specified by 1 of name or OF node,
3587 * not both or neither.
3589 if (!!link->codec_name == !!link->codec_of_node) {
3591 "ASoC: Neither/both codec name/of_node are set for %s\n",
3595 /* Codec DAI name must be specified */
3596 if (!link->codec_dai_name) {
3598 "ASoC: codec_dai_name not set for %s\n",
3604 * Platform may be specified by either name or OF node, but
3605 * can be left unspecified, and a dummy platform will be used.
3607 if (link->platform_name && link->platform_of_node) {
3609 "ASoC: Both platform name/of_node are set for %s\n",
3615 * CPU device may be specified by either name or OF node, but
3616 * can be left unspecified, and will be matched based on DAI
3619 if (link->cpu_name && link->cpu_of_node) {
3621 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3626 * At least one of CPU DAI name or CPU device name/node must be
3629 if (!link->cpu_dai_name &&
3630 !(link->cpu_name || link->cpu_of_node)) {
3632 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3638 dev_set_drvdata(card->dev, card);
3640 snd_soc_initialize_card_lists(card);
3642 soc_init_card_debugfs(card);
3644 card->rtd = devm_kzalloc(card->dev,
3645 sizeof(struct snd_soc_pcm_runtime) *
3646 (card->num_links + card->num_aux_devs),
3648 if (card->rtd == NULL)
3651 card->rtd_aux = &card->rtd[card->num_links];
3653 for (i = 0; i < card->num_links; i++)
3654 card->rtd[i].dai_link = &card->dai_link[i];
3656 INIT_LIST_HEAD(&card->list);
3657 INIT_LIST_HEAD(&card->dapm_dirty);
3658 card->instantiated = 0;
3659 mutex_init(&card->mutex);
3660 mutex_init(&card->dapm_mutex);
3662 ret = snd_soc_instantiate_card(card);
3664 soc_cleanup_card_debugfs(card);
3668 EXPORT_SYMBOL_GPL(snd_soc_register_card);
3671 * snd_soc_unregister_card - Unregister a card with the ASoC core
3673 * @card: Card to unregister
3676 int snd_soc_unregister_card(struct snd_soc_card *card)
3678 if (card->instantiated)
3679 soc_cleanup_card_resources(card);
3680 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
3684 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3687 * Simplify DAI link configuration by removing ".-1" from device names
3688 * and sanitizing names.
3690 static char *fmt_single_name(struct device *dev, int *id)
3692 char *found, name[NAME_SIZE];
3695 if (dev_name(dev) == NULL)
3698 strlcpy(name, dev_name(dev), NAME_SIZE);
3700 /* are we a "%s.%d" name (platform and SPI components) */
3701 found = strstr(name, dev->driver->name);
3704 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3706 /* discard ID from name if ID == -1 */
3708 found[strlen(dev->driver->name)] = '\0';
3712 /* I2C component devices are named "bus-addr" */
3713 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3714 char tmp[NAME_SIZE];
3716 /* create unique ID number from I2C addr and bus */
3717 *id = ((id1 & 0xffff) << 16) + id2;
3719 /* sanitize component name for DAI link creation */
3720 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3721 strlcpy(name, tmp, NAME_SIZE);
3726 return kstrdup(name, GFP_KERNEL);
3730 * Simplify DAI link naming for single devices with multiple DAIs by removing
3731 * any ".-1" and using the DAI name (instead of device name).
3733 static inline char *fmt_multiple_name(struct device *dev,
3734 struct snd_soc_dai_driver *dai_drv)
3736 if (dai_drv->name == NULL) {
3738 "ASoC: error - multiple DAI %s registered with no name\n",
3743 return kstrdup(dai_drv->name, GFP_KERNEL);
3747 * snd_soc_register_dai - Register a DAI with the ASoC core
3749 * @dai: DAI to register
3751 static int snd_soc_register_dai(struct device *dev,
3752 struct snd_soc_dai_driver *dai_drv)
3754 struct snd_soc_codec *codec;
3755 struct snd_soc_dai *dai;
3757 dev_dbg(dev, "ASoC: dai register %s\n", dev_name(dev));
3759 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3763 /* create DAI component name */
3764 dai->name = fmt_single_name(dev, &dai->id);
3765 if (dai->name == NULL) {
3771 dai->driver = dai_drv;
3772 dai->dapm.dev = dev;
3773 if (!dai->driver->ops)
3774 dai->driver->ops = &null_dai_ops;
3776 mutex_lock(&client_mutex);
3778 list_for_each_entry(codec, &codec_list, list) {
3779 if (codec->dev == dev) {
3780 dev_dbg(dev, "ASoC: Mapped DAI %s to CODEC %s\n",
3781 dai->name, codec->name);
3788 dai->dapm.idle_bias_off = 1;
3790 list_add(&dai->list, &dai_list);
3792 mutex_unlock(&client_mutex);
3794 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3800 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3802 * @dai: DAI to unregister
3804 static void snd_soc_unregister_dai(struct device *dev)
3806 struct snd_soc_dai *dai;
3808 list_for_each_entry(dai, &dai_list, list) {
3809 if (dev == dai->dev)
3815 mutex_lock(&client_mutex);
3816 list_del(&dai->list);
3817 mutex_unlock(&client_mutex);
3819 dev_dbg(dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
3825 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3827 * @dai: Array of DAIs to register
3828 * @count: Number of DAIs
3830 static int snd_soc_register_dais(struct device *dev,
3831 struct snd_soc_dai_driver *dai_drv, size_t count)
3833 struct snd_soc_codec *codec;
3834 struct snd_soc_dai *dai;
3837 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
3839 for (i = 0; i < count; i++) {
3841 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3847 /* create DAI component name */
3848 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3849 if (dai->name == NULL) {
3856 dai->driver = &dai_drv[i];
3857 if (dai->driver->id)
3858 dai->id = dai->driver->id;
3861 dai->dapm.dev = dev;
3862 if (!dai->driver->ops)
3863 dai->driver->ops = &null_dai_ops;
3865 mutex_lock(&client_mutex);
3867 list_for_each_entry(codec, &codec_list, list) {
3868 if (codec->dev == dev) {
3870 "ASoC: Mapped DAI %s to CODEC %s\n",
3871 dai->name, codec->name);
3878 dai->dapm.idle_bias_off = 1;
3880 list_add(&dai->list, &dai_list);
3882 mutex_unlock(&client_mutex);
3884 dev_dbg(dai->dev, "ASoC: Registered DAI '%s'\n", dai->name);
3890 for (i--; i >= 0; i--)
3891 snd_soc_unregister_dai(dev);
3897 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3899 * @dai: Array of DAIs to unregister
3900 * @count: Number of DAIs
3902 static void snd_soc_unregister_dais(struct device *dev, size_t count)
3906 for (i = 0; i < count; i++)
3907 snd_soc_unregister_dai(dev);
3911 * snd_soc_add_platform - Add a platform to the ASoC core
3912 * @dev: The parent device for the platform
3913 * @platform: The platform to add
3914 * @platform_driver: The driver for the platform
3916 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
3917 const struct snd_soc_platform_driver *platform_drv)
3919 /* create platform component name */
3920 platform->name = fmt_single_name(dev, &platform->id);
3921 if (platform->name == NULL) {
3926 platform->dev = dev;
3927 platform->driver = platform_drv;
3928 platform->dapm.dev = dev;
3929 platform->dapm.platform = platform;
3930 platform->dapm.stream_event = platform_drv->stream_event;
3931 mutex_init(&platform->mutex);
3933 mutex_lock(&client_mutex);
3934 list_add(&platform->list, &platform_list);
3935 mutex_unlock(&client_mutex);
3937 dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
3941 EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3944 * snd_soc_register_platform - Register a platform with the ASoC core
3946 * @platform: platform to register
3948 int snd_soc_register_platform(struct device *dev,
3949 const struct snd_soc_platform_driver *platform_drv)
3951 struct snd_soc_platform *platform;
3954 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3956 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3957 if (platform == NULL)
3960 ret = snd_soc_add_platform(dev, platform, platform_drv);
3966 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3969 * snd_soc_remove_platform - Remove a platform from the ASoC core
3970 * @platform: the platform to remove
3972 void snd_soc_remove_platform(struct snd_soc_platform *platform)
3974 mutex_lock(&client_mutex);
3975 list_del(&platform->list);
3976 mutex_unlock(&client_mutex);
3978 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3980 kfree(platform->name);
3982 EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
3984 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
3986 struct snd_soc_platform *platform;
3988 list_for_each_entry(platform, &platform_list, list) {
3989 if (dev == platform->dev)
3995 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
3998 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4000 * @platform: platform to unregister
4002 void snd_soc_unregister_platform(struct device *dev)
4004 struct snd_soc_platform *platform;
4006 platform = snd_soc_lookup_platform(dev);
4010 snd_soc_remove_platform(platform);
4013 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4015 static u64 codec_format_map[] = {
4016 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4017 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4018 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4019 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4020 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4021 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4022 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4023 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4024 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4025 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4026 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4027 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4028 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4029 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4030 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4031 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4034 /* Fix up the DAI formats for endianness: codecs don't actually see
4035 * the endianness of the data but we're using the CPU format
4036 * definitions which do need to include endianness so we ensure that
4037 * codec DAIs always have both big and little endian variants set.
4039 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4043 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4044 if (stream->formats & codec_format_map[i])
4045 stream->formats |= codec_format_map[i];
4049 * snd_soc_register_codec - Register a codec with the ASoC core
4051 * @codec: codec to register
4053 int snd_soc_register_codec(struct device *dev,
4054 const struct snd_soc_codec_driver *codec_drv,
4055 struct snd_soc_dai_driver *dai_drv,
4059 struct snd_soc_codec *codec;
4062 dev_dbg(dev, "codec register %s\n", dev_name(dev));
4064 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4068 /* create CODEC component name */
4069 codec->name = fmt_single_name(dev, &codec->id);
4070 if (codec->name == NULL) {
4075 if (codec_drv->compress_type)
4076 codec->compress_type = codec_drv->compress_type;
4078 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
4080 codec->write = codec_drv->write;
4081 codec->read = codec_drv->read;
4082 codec->volatile_register = codec_drv->volatile_register;
4083 codec->readable_register = codec_drv->readable_register;
4084 codec->writable_register = codec_drv->writable_register;
4085 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
4086 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4087 codec->dapm.dev = dev;
4088 codec->dapm.codec = codec;
4089 codec->dapm.seq_notifier = codec_drv->seq_notifier;
4090 codec->dapm.stream_event = codec_drv->stream_event;
4092 codec->driver = codec_drv;
4093 codec->num_dai = num_dai;
4094 mutex_init(&codec->mutex);
4096 /* allocate CODEC register cache */
4097 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
4098 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
4099 codec->reg_size = reg_size;
4100 /* it is necessary to make a copy of the default register cache
4101 * because in the case of using a compression type that requires
4102 * the default register cache to be marked as the
4103 * kernel might have freed the array by the time we initialize
4106 if (codec_drv->reg_cache_default) {
4107 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
4108 reg_size, GFP_KERNEL);
4109 if (!codec->reg_def_copy) {
4111 goto fail_codec_name;
4116 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
4117 if (!codec->volatile_register)
4118 codec->volatile_register = snd_soc_default_volatile_register;
4119 if (!codec->readable_register)
4120 codec->readable_register = snd_soc_default_readable_register;
4121 if (!codec->writable_register)
4122 codec->writable_register = snd_soc_default_writable_register;
4125 for (i = 0; i < num_dai; i++) {
4126 fixup_codec_formats(&dai_drv[i].playback);
4127 fixup_codec_formats(&dai_drv[i].capture);
4130 mutex_lock(&client_mutex);
4131 list_add(&codec->list, &codec_list);
4132 mutex_unlock(&client_mutex);
4134 /* register any DAIs */
4135 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
4137 dev_err(codec->dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4138 goto fail_codec_name;
4141 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
4145 mutex_lock(&client_mutex);
4146 list_del(&codec->list);
4147 mutex_unlock(&client_mutex);
4154 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4157 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4159 * @codec: codec to unregister
4161 void snd_soc_unregister_codec(struct device *dev)
4163 struct snd_soc_codec *codec;
4165 list_for_each_entry(codec, &codec_list, list) {
4166 if (dev == codec->dev)
4172 snd_soc_unregister_dais(dev, codec->num_dai);
4174 mutex_lock(&client_mutex);
4175 list_del(&codec->list);
4176 mutex_unlock(&client_mutex);
4178 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
4180 snd_soc_cache_exit(codec);
4181 kfree(codec->reg_def_copy);
4185 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4189 * snd_soc_register_component - Register a component with the ASoC core
4192 int snd_soc_register_component(struct device *dev,
4193 const struct snd_soc_component_driver *cmpnt_drv,
4194 struct snd_soc_dai_driver *dai_drv,
4197 struct snd_soc_component *cmpnt;
4200 dev_dbg(dev, "component register %s\n", dev_name(dev));
4202 cmpnt = devm_kzalloc(dev, sizeof(*cmpnt), GFP_KERNEL);
4204 dev_err(dev, "ASoC: Failed to allocate memory\n");
4208 cmpnt->name = fmt_single_name(dev, &cmpnt->id);
4210 dev_err(dev, "ASoC: Failed to simplifying name\n");
4215 cmpnt->driver = cmpnt_drv;
4216 cmpnt->num_dai = num_dai;
4219 * snd_soc_register_dai() uses fmt_single_name(), and
4220 * snd_soc_register_dais() uses fmt_multiple_name()
4221 * for dai->name which is used for name based matching
4224 ret = snd_soc_register_dai(dev, dai_drv);
4226 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
4228 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4229 goto error_component_name;
4232 mutex_lock(&client_mutex);
4233 list_add(&cmpnt->list, &component_list);
4234 mutex_unlock(&client_mutex);
4236 dev_dbg(cmpnt->dev, "ASoC: Registered component '%s'\n", cmpnt->name);
4240 error_component_name:
4245 EXPORT_SYMBOL_GPL(snd_soc_register_component);
4248 * snd_soc_unregister_component - Unregister a component from the ASoC core
4251 void snd_soc_unregister_component(struct device *dev)
4253 struct snd_soc_component *cmpnt;
4255 list_for_each_entry(cmpnt, &component_list, list) {
4256 if (dev == cmpnt->dev)
4262 snd_soc_unregister_dais(dev, cmpnt->num_dai);
4264 mutex_lock(&client_mutex);
4265 list_del(&cmpnt->list);
4266 mutex_unlock(&client_mutex);
4268 dev_dbg(dev, "ASoC: Unregistered component '%s'\n", cmpnt->name);
4271 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4273 /* Retrieve a card's name from device tree */
4274 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4275 const char *propname)
4277 struct device_node *np = card->dev->of_node;
4280 ret = of_property_read_string_index(np, propname, 0, &card->name);
4282 * EINVAL means the property does not exist. This is fine providing
4283 * card->name was previously set, which is checked later in
4284 * snd_soc_register_card.
4286 if (ret < 0 && ret != -EINVAL) {
4288 "ASoC: Property '%s' could not be read: %d\n",
4295 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4297 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4298 const char *propname)
4300 struct device_node *np = card->dev->of_node;
4302 struct snd_soc_dapm_route *routes;
4305 num_routes = of_property_count_strings(np, propname);
4306 if (num_routes < 0 || num_routes & 1) {
4308 "ASoC: Property '%s' does not exist or its length is not even\n",
4314 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4319 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4323 "ASoC: Could not allocate DAPM route table\n");
4327 for (i = 0; i < num_routes; i++) {
4328 ret = of_property_read_string_index(np, propname,
4329 2 * i, &routes[i].sink);
4332 "ASoC: Property '%s' index %d could not be read: %d\n",
4333 propname, 2 * i, ret);
4336 ret = of_property_read_string_index(np, propname,
4337 (2 * i) + 1, &routes[i].source);
4340 "ASoC: Property '%s' index %d could not be read: %d\n",
4341 propname, (2 * i) + 1, ret);
4346 card->num_dapm_routes = num_routes;
4347 card->dapm_routes = routes;
4351 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4353 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4358 unsigned int format = 0;
4364 } of_fmt_table[] = {
4365 { "i2s", SND_SOC_DAIFMT_I2S },
4366 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4367 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4368 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4369 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4370 { "ac97", SND_SOC_DAIFMT_AC97 },
4371 { "pdm", SND_SOC_DAIFMT_PDM},
4372 { "msb", SND_SOC_DAIFMT_MSB },
4373 { "lsb", SND_SOC_DAIFMT_LSB },
4380 * check "[prefix]format = xxx"
4381 * SND_SOC_DAIFMT_FORMAT_MASK area
4383 snprintf(prop, sizeof(prop), "%sformat", prefix);
4384 ret = of_property_read_string(np, prop, &str);
4386 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4387 if (strcmp(str, of_fmt_table[i].name) == 0) {
4388 format |= of_fmt_table[i].val;
4395 * check "[prefix]continuous-clock"
4396 * SND_SOC_DAIFMT_CLOCK_MASK area
4398 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4399 if (of_get_property(np, prop, NULL))
4400 format |= SND_SOC_DAIFMT_CONT;
4402 format |= SND_SOC_DAIFMT_GATED;
4405 * check "[prefix]bitclock-inversion"
4406 * check "[prefix]frame-inversion"
4407 * SND_SOC_DAIFMT_INV_MASK area
4409 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4410 bit = !!of_get_property(np, prop, NULL);
4412 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4413 frame = !!of_get_property(np, prop, NULL);
4415 switch ((bit << 4) + frame) {
4417 format |= SND_SOC_DAIFMT_IB_IF;
4420 format |= SND_SOC_DAIFMT_IB_NF;
4423 format |= SND_SOC_DAIFMT_NB_IF;
4426 /* SND_SOC_DAIFMT_NB_NF is default */
4431 * check "[prefix]bitclock-master"
4432 * check "[prefix]frame-master"
4433 * SND_SOC_DAIFMT_MASTER_MASK area
4435 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4436 bit = !!of_get_property(np, prop, NULL);
4438 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4439 frame = !!of_get_property(np, prop, NULL);
4441 switch ((bit << 4) + frame) {
4443 format |= SND_SOC_DAIFMT_CBM_CFM;
4446 format |= SND_SOC_DAIFMT_CBM_CFS;
4449 format |= SND_SOC_DAIFMT_CBS_CFM;
4452 format |= SND_SOC_DAIFMT_CBS_CFS;
4458 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4460 static int __init snd_soc_init(void)
4462 #ifdef CONFIG_DEBUG_FS
4463 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4464 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
4465 pr_warn("ASoC: Failed to create debugfs directory\n");
4466 snd_soc_debugfs_root = NULL;
4469 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4471 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4473 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4475 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
4477 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
4478 &platform_list_fops))
4479 pr_warn("ASoC: Failed to create platform list debugfs file\n");
4482 snd_soc_util_init();
4484 return platform_driver_register(&soc_driver);
4486 module_init(snd_soc_init);
4488 static void __exit snd_soc_exit(void)
4490 snd_soc_util_exit();
4492 #ifdef CONFIG_DEBUG_FS
4493 debugfs_remove_recursive(snd_soc_debugfs_root);
4495 platform_driver_unregister(&soc_driver);
4497 module_exit(snd_soc_exit);
4499 /* Module information */
4500 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4501 MODULE_DESCRIPTION("ALSA SoC Core");
4502 MODULE_LICENSE("GPL");
4503 MODULE_ALIAS("platform:soc-audio");