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/pinctrl/consumer.h>
34 #include <linux/ctype.h>
35 #include <linux/slab.h>
37 #include <linux/gpio.h>
38 #include <linux/of_gpio.h>
39 #include <sound/ac97_codec.h>
40 #include <sound/core.h>
41 #include <sound/jack.h>
42 #include <sound/pcm.h>
43 #include <sound/pcm_params.h>
44 #include <sound/soc.h>
45 #include <sound/soc-dpcm.h>
46 #include <sound/initval.h>
48 #define CREATE_TRACE_POINTS
49 #include <trace/events/asoc.h>
53 #ifdef CONFIG_DEBUG_FS
54 struct dentry *snd_soc_debugfs_root;
55 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
58 static DEFINE_MUTEX(client_mutex);
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 struct snd_ac97_reset_cfg {
74 struct pinctrl_state *pstate_reset;
75 struct pinctrl_state *pstate_warm_reset;
76 struct pinctrl_state *pstate_run;
82 /* returns the minimum number of bytes needed to represent
83 * a particular given value */
84 static int min_bytes_needed(unsigned long val)
89 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
92 c = (sizeof val * 8) - c;
100 /* fill buf which is 'len' bytes with a formatted
101 * string of the form 'reg: value\n' */
102 static int format_register_str(struct snd_soc_codec *codec,
103 unsigned int reg, char *buf, size_t len)
105 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
106 int regsize = codec->driver->reg_word_size * 2;
108 char tmpbuf[len + 1];
109 char regbuf[regsize + 1];
111 /* since tmpbuf is allocated on the stack, warn the callers if they
112 * try to abuse this function */
115 /* +2 for ': ' and + 1 for '\n' */
116 if (wordsize + regsize + 2 + 1 != len)
119 ret = snd_soc_read(codec, reg);
121 memset(regbuf, 'X', regsize);
122 regbuf[regsize] = '\0';
124 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
127 /* prepare the buffer */
128 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
129 /* copy it back to the caller without the '\0' */
130 memcpy(buf, tmpbuf, len);
135 /* codec register dump */
136 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
137 size_t count, loff_t pos)
140 int wordsize, regsize;
145 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
146 regsize = codec->driver->reg_word_size * 2;
148 len = wordsize + regsize + 2 + 1;
150 if (!codec->driver->reg_cache_size)
153 if (codec->driver->reg_cache_step)
154 step = codec->driver->reg_cache_step;
156 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
157 /* only support larger than PAGE_SIZE bytes debugfs
158 * entries for the default case */
160 if (total + len >= count - 1)
162 format_register_str(codec, i, buf + total, len);
168 total = min(total, count - 1);
173 static ssize_t codec_reg_show(struct device *dev,
174 struct device_attribute *attr, char *buf)
176 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
178 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
181 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
183 static ssize_t pmdown_time_show(struct device *dev,
184 struct device_attribute *attr, char *buf)
186 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
188 return sprintf(buf, "%ld\n", rtd->pmdown_time);
191 static ssize_t pmdown_time_set(struct device *dev,
192 struct device_attribute *attr,
193 const char *buf, size_t count)
195 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
198 ret = kstrtol(buf, 10, &rtd->pmdown_time);
205 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
207 #ifdef CONFIG_DEBUG_FS
208 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
209 size_t count, loff_t *ppos)
212 struct snd_soc_codec *codec = file->private_data;
215 if (*ppos < 0 || !count)
218 buf = kmalloc(count, GFP_KERNEL);
222 ret = soc_codec_reg_show(codec, buf, count, *ppos);
224 if (copy_to_user(user_buf, buf, ret)) {
235 static ssize_t codec_reg_write_file(struct file *file,
236 const char __user *user_buf, size_t count, loff_t *ppos)
241 unsigned long reg, value;
242 struct snd_soc_codec *codec = file->private_data;
245 buf_size = min(count, (sizeof(buf)-1));
246 if (copy_from_user(buf, user_buf, buf_size))
250 while (*start == ' ')
252 reg = simple_strtoul(start, &start, 16);
253 while (*start == ' ')
255 ret = kstrtoul(start, 16, &value);
259 /* Userspace has been fiddling around behind the kernel's back */
260 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
262 snd_soc_write(codec, reg, value);
266 static const struct file_operations codec_reg_fops = {
268 .read = codec_reg_read_file,
269 .write = codec_reg_write_file,
270 .llseek = default_llseek,
273 static void soc_init_component_debugfs(struct snd_soc_component *component)
275 if (component->debugfs_prefix) {
278 name = kasprintf(GFP_KERNEL, "%s:%s",
279 component->debugfs_prefix, component->name);
281 component->debugfs_root = debugfs_create_dir(name,
282 component->card->debugfs_card_root);
286 component->debugfs_root = debugfs_create_dir(component->name,
287 component->card->debugfs_card_root);
290 if (!component->debugfs_root) {
291 dev_warn(component->dev,
292 "ASoC: Failed to create component debugfs directory\n");
296 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
297 component->debugfs_root);
299 if (component->init_debugfs)
300 component->init_debugfs(component);
303 static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
305 debugfs_remove_recursive(component->debugfs_root);
308 static void soc_init_codec_debugfs(struct snd_soc_component *component)
310 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
312 debugfs_create_bool("cache_sync", 0444, codec->component.debugfs_root,
315 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
316 codec->component.debugfs_root,
317 codec, &codec_reg_fops);
318 if (!codec->debugfs_reg)
320 "ASoC: Failed to create codec register debugfs file\n");
323 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
324 size_t count, loff_t *ppos)
326 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
327 ssize_t len, ret = 0;
328 struct snd_soc_codec *codec;
333 list_for_each_entry(codec, &codec_list, list) {
334 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
335 codec->component.name);
338 if (ret > PAGE_SIZE) {
345 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
352 static const struct file_operations codec_list_fops = {
353 .read = codec_list_read_file,
354 .llseek = default_llseek,/* read accesses f_pos */
357 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
358 size_t count, loff_t *ppos)
360 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
361 ssize_t len, ret = 0;
362 struct snd_soc_component *component;
363 struct snd_soc_dai *dai;
368 list_for_each_entry(component, &component_list, list) {
369 list_for_each_entry(dai, &component->dai_list, list) {
370 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
374 if (ret > PAGE_SIZE) {
381 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
388 static const struct file_operations dai_list_fops = {
389 .read = dai_list_read_file,
390 .llseek = default_llseek,/* read accesses f_pos */
393 static ssize_t platform_list_read_file(struct file *file,
394 char __user *user_buf,
395 size_t count, loff_t *ppos)
397 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
398 ssize_t len, ret = 0;
399 struct snd_soc_platform *platform;
404 list_for_each_entry(platform, &platform_list, list) {
405 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
406 platform->component.name);
409 if (ret > PAGE_SIZE) {
415 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
422 static const struct file_operations platform_list_fops = {
423 .read = platform_list_read_file,
424 .llseek = default_llseek,/* read accesses f_pos */
427 static void soc_init_card_debugfs(struct snd_soc_card *card)
429 card->debugfs_card_root = debugfs_create_dir(card->name,
430 snd_soc_debugfs_root);
431 if (!card->debugfs_card_root) {
433 "ASoC: Failed to create card debugfs directory\n");
437 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
438 card->debugfs_card_root,
440 if (!card->debugfs_pop_time)
442 "ASoC: Failed to create pop time debugfs file\n");
445 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
447 debugfs_remove_recursive(card->debugfs_card_root);
452 #define soc_init_codec_debugfs NULL
454 static inline void soc_init_component_debugfs(
455 struct snd_soc_component *component)
459 static inline void soc_cleanup_component_debugfs(
460 struct snd_soc_component *component)
464 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
468 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
473 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
474 const char *dai_link, int stream)
478 for (i = 0; i < card->num_links; i++) {
479 if (card->rtd[i].dai_link->no_pcm &&
480 !strcmp(card->rtd[i].dai_link->name, dai_link))
481 return card->rtd[i].pcm->streams[stream].substream;
483 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
486 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
488 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
489 const char *dai_link)
493 for (i = 0; i < card->num_links; i++) {
494 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
495 return &card->rtd[i];
497 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
500 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
502 #ifdef CONFIG_SND_SOC_AC97_BUS
503 /* unregister ac97 codec */
504 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
506 if (codec->ac97->dev.bus)
507 device_unregister(&codec->ac97->dev);
511 /* stop no dev release warning */
512 static void soc_ac97_device_release(struct device *dev){}
514 /* register ac97 codec to bus */
515 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
519 codec->ac97->dev.bus = &ac97_bus_type;
520 codec->ac97->dev.parent = codec->component.card->dev;
521 codec->ac97->dev.release = soc_ac97_device_release;
523 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
524 codec->component.card->snd_card->number, 0,
525 codec->component.name);
526 err = device_register(&codec->ac97->dev);
528 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
529 codec->ac97->dev.bus = NULL;
536 static void codec2codec_close_delayed_work(struct work_struct *work)
538 /* Currently nothing to do for c2c links
539 * Since c2c links are internal nodes in the DAPM graph and
540 * don't interface with the outside world or application layer
541 * we don't have to do any special handling on close.
545 #ifdef CONFIG_PM_SLEEP
546 /* powers down audio subsystem for suspend */
547 int snd_soc_suspend(struct device *dev)
549 struct snd_soc_card *card = dev_get_drvdata(dev);
550 struct snd_soc_codec *codec;
553 /* If the card is not initialized yet there is nothing to do */
554 if (!card->instantiated)
557 /* Due to the resume being scheduled into a workqueue we could
558 * suspend before that's finished - wait for it to complete.
560 snd_power_lock(card->snd_card);
561 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
562 snd_power_unlock(card->snd_card);
564 /* we're going to block userspace touching us until resume completes */
565 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
567 /* mute any active DACs */
568 for (i = 0; i < card->num_rtd; i++) {
570 if (card->rtd[i].dai_link->ignore_suspend)
573 for (j = 0; j < card->rtd[i].num_codecs; j++) {
574 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
575 struct snd_soc_dai_driver *drv = dai->driver;
577 if (drv->ops->digital_mute && dai->playback_active)
578 drv->ops->digital_mute(dai, 1);
582 /* suspend all pcms */
583 for (i = 0; i < card->num_rtd; i++) {
584 if (card->rtd[i].dai_link->ignore_suspend)
587 snd_pcm_suspend_all(card->rtd[i].pcm);
590 if (card->suspend_pre)
591 card->suspend_pre(card);
593 for (i = 0; i < card->num_rtd; i++) {
594 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
595 struct snd_soc_platform *platform = card->rtd[i].platform;
597 if (card->rtd[i].dai_link->ignore_suspend)
600 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
601 cpu_dai->driver->suspend(cpu_dai);
602 if (platform->driver->suspend && !platform->suspended) {
603 platform->driver->suspend(cpu_dai);
604 platform->suspended = 1;
608 /* close any waiting streams and save state */
609 for (i = 0; i < card->num_rtd; i++) {
610 struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais;
611 flush_delayed_work(&card->rtd[i].delayed_work);
612 for (j = 0; j < card->rtd[i].num_codecs; j++) {
613 codec_dais[j]->codec->dapm.suspend_bias_level =
614 codec_dais[j]->codec->dapm.bias_level;
618 for (i = 0; i < card->num_rtd; i++) {
620 if (card->rtd[i].dai_link->ignore_suspend)
623 snd_soc_dapm_stream_event(&card->rtd[i],
624 SNDRV_PCM_STREAM_PLAYBACK,
625 SND_SOC_DAPM_STREAM_SUSPEND);
627 snd_soc_dapm_stream_event(&card->rtd[i],
628 SNDRV_PCM_STREAM_CAPTURE,
629 SND_SOC_DAPM_STREAM_SUSPEND);
632 /* Recheck all analogue paths too */
633 dapm_mark_io_dirty(&card->dapm);
634 snd_soc_dapm_sync(&card->dapm);
636 /* suspend all CODECs */
637 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
638 /* If there are paths active then the CODEC will be held with
639 * bias _ON and should not be suspended. */
640 if (!codec->suspended) {
641 switch (codec->dapm.bias_level) {
642 case SND_SOC_BIAS_STANDBY:
644 * If the CODEC is capable of idle
645 * bias off then being in STANDBY
646 * means it's doing something,
647 * otherwise fall through.
649 if (codec->dapm.idle_bias_off) {
651 "ASoC: idle_bias_off CODEC on over suspend\n");
655 case SND_SOC_BIAS_OFF:
656 if (codec->driver->suspend)
657 codec->driver->suspend(codec);
658 codec->suspended = 1;
659 codec->cache_sync = 1;
660 if (codec->component.regmap)
661 regcache_mark_dirty(codec->component.regmap);
662 /* deactivate pins to sleep state */
663 pinctrl_pm_select_sleep_state(codec->dev);
667 "ASoC: CODEC is on over suspend\n");
673 for (i = 0; i < card->num_rtd; i++) {
674 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
676 if (card->rtd[i].dai_link->ignore_suspend)
679 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
680 cpu_dai->driver->suspend(cpu_dai);
682 /* deactivate pins to sleep state */
683 pinctrl_pm_select_sleep_state(cpu_dai->dev);
686 if (card->suspend_post)
687 card->suspend_post(card);
691 EXPORT_SYMBOL_GPL(snd_soc_suspend);
693 /* deferred resume work, so resume can complete before we finished
694 * setting our codec back up, which can be very slow on I2C
696 static void soc_resume_deferred(struct work_struct *work)
698 struct snd_soc_card *card =
699 container_of(work, struct snd_soc_card, deferred_resume_work);
700 struct snd_soc_codec *codec;
703 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
704 * so userspace apps are blocked from touching us
707 dev_dbg(card->dev, "ASoC: starting resume work\n");
709 /* Bring us up into D2 so that DAPM starts enabling things */
710 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
712 if (card->resume_pre)
713 card->resume_pre(card);
715 /* resume AC97 DAIs */
716 for (i = 0; i < card->num_rtd; i++) {
717 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
719 if (card->rtd[i].dai_link->ignore_suspend)
722 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
723 cpu_dai->driver->resume(cpu_dai);
726 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
727 /* If the CODEC was idle over suspend then it will have been
728 * left with bias OFF or STANDBY and suspended so we must now
729 * resume. Otherwise the suspend was suppressed.
731 if (codec->suspended) {
732 switch (codec->dapm.bias_level) {
733 case SND_SOC_BIAS_STANDBY:
734 case SND_SOC_BIAS_OFF:
735 if (codec->driver->resume)
736 codec->driver->resume(codec);
737 codec->suspended = 0;
741 "ASoC: CODEC was on over suspend\n");
747 for (i = 0; i < card->num_rtd; i++) {
749 if (card->rtd[i].dai_link->ignore_suspend)
752 snd_soc_dapm_stream_event(&card->rtd[i],
753 SNDRV_PCM_STREAM_PLAYBACK,
754 SND_SOC_DAPM_STREAM_RESUME);
756 snd_soc_dapm_stream_event(&card->rtd[i],
757 SNDRV_PCM_STREAM_CAPTURE,
758 SND_SOC_DAPM_STREAM_RESUME);
761 /* unmute any active DACs */
762 for (i = 0; i < card->num_rtd; i++) {
764 if (card->rtd[i].dai_link->ignore_suspend)
767 for (j = 0; j < card->rtd[i].num_codecs; j++) {
768 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
769 struct snd_soc_dai_driver *drv = dai->driver;
771 if (drv->ops->digital_mute && dai->playback_active)
772 drv->ops->digital_mute(dai, 0);
776 for (i = 0; i < card->num_rtd; i++) {
777 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
778 struct snd_soc_platform *platform = card->rtd[i].platform;
780 if (card->rtd[i].dai_link->ignore_suspend)
783 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
784 cpu_dai->driver->resume(cpu_dai);
785 if (platform->driver->resume && platform->suspended) {
786 platform->driver->resume(cpu_dai);
787 platform->suspended = 0;
791 if (card->resume_post)
792 card->resume_post(card);
794 dev_dbg(card->dev, "ASoC: resume work completed\n");
796 /* userspace can access us now we are back as we were before */
797 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
799 /* Recheck all analogue paths too */
800 dapm_mark_io_dirty(&card->dapm);
801 snd_soc_dapm_sync(&card->dapm);
804 /* powers up audio subsystem after a suspend */
805 int snd_soc_resume(struct device *dev)
807 struct snd_soc_card *card = dev_get_drvdata(dev);
808 int i, ac97_control = 0;
810 /* If the card is not initialized yet there is nothing to do */
811 if (!card->instantiated)
814 /* activate pins from sleep state */
815 for (i = 0; i < card->num_rtd; i++) {
816 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
817 struct snd_soc_dai **codec_dais = rtd->codec_dais;
818 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
822 pinctrl_pm_select_default_state(cpu_dai->dev);
824 for (j = 0; j < rtd->num_codecs; j++) {
825 struct snd_soc_dai *codec_dai = codec_dais[j];
826 if (codec_dai->active)
827 pinctrl_pm_select_default_state(codec_dai->dev);
831 /* AC97 devices might have other drivers hanging off them so
832 * need to resume immediately. Other drivers don't have that
833 * problem and may take a substantial amount of time to resume
834 * due to I/O costs and anti-pop so handle them out of line.
836 for (i = 0; i < card->num_rtd; i++) {
837 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
838 ac97_control |= cpu_dai->driver->ac97_control;
841 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
842 soc_resume_deferred(&card->deferred_resume_work);
844 dev_dbg(dev, "ASoC: Scheduling resume work\n");
845 if (!schedule_work(&card->deferred_resume_work))
846 dev_err(dev, "ASoC: resume work item may be lost\n");
851 EXPORT_SYMBOL_GPL(snd_soc_resume);
853 #define snd_soc_suspend NULL
854 #define snd_soc_resume NULL
857 static const struct snd_soc_dai_ops null_dai_ops = {
860 static struct snd_soc_component *soc_find_component(
861 const struct device_node *of_node, const char *name)
863 struct snd_soc_component *component;
865 list_for_each_entry(component, &component_list, list) {
867 if (component->dev->of_node == of_node)
869 } else if (strcmp(component->name, name) == 0) {
877 static struct snd_soc_dai *snd_soc_find_dai(
878 const struct snd_soc_dai_link_component *dlc)
880 struct snd_soc_component *component;
881 struct snd_soc_dai *dai;
883 /* Find CPU DAI from registered DAIs*/
884 list_for_each_entry(component, &component_list, list) {
885 if (dlc->of_node && component->dev->of_node != dlc->of_node)
887 if (dlc->name && strcmp(dev_name(component->dev), dlc->name))
889 list_for_each_entry(dai, &component->dai_list, list) {
890 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
900 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
902 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
903 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
904 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
905 struct snd_soc_dai_link_component cpu_dai_component;
906 struct snd_soc_dai **codec_dais = rtd->codec_dais;
907 struct snd_soc_platform *platform;
908 const char *platform_name;
911 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
913 cpu_dai_component.name = dai_link->cpu_name;
914 cpu_dai_component.of_node = dai_link->cpu_of_node;
915 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
916 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
918 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
919 dai_link->cpu_dai_name);
920 return -EPROBE_DEFER;
923 rtd->num_codecs = dai_link->num_codecs;
925 /* Find CODEC from registered CODECs */
926 for (i = 0; i < rtd->num_codecs; i++) {
927 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
928 if (!codec_dais[i]) {
929 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
931 return -EPROBE_DEFER;
935 /* Single codec links expect codec and codec_dai in runtime data */
936 rtd->codec_dai = codec_dais[0];
937 rtd->codec = rtd->codec_dai->codec;
939 /* if there's no platform we match on the empty platform */
940 platform_name = dai_link->platform_name;
941 if (!platform_name && !dai_link->platform_of_node)
942 platform_name = "snd-soc-dummy";
944 /* find one from the set of registered platforms */
945 list_for_each_entry(platform, &platform_list, list) {
946 if (dai_link->platform_of_node) {
947 if (platform->dev->of_node !=
948 dai_link->platform_of_node)
951 if (strcmp(platform->component.name, platform_name))
955 rtd->platform = platform;
957 if (!rtd->platform) {
958 dev_err(card->dev, "ASoC: platform %s not registered\n",
959 dai_link->platform_name);
960 return -EPROBE_DEFER;
968 static void soc_remove_component(struct snd_soc_component *component)
970 if (!component->probed)
973 /* This is a HACK and will be removed soon */
974 if (component->codec)
975 list_del(&component->codec->card_list);
977 if (component->remove)
978 component->remove(component);
980 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
982 soc_cleanup_component_debugfs(component);
983 component->probed = 0;
984 module_put(component->dev->driver->owner);
987 static void soc_remove_dai(struct snd_soc_dai *dai, int order)
991 if (dai && dai->probed &&
992 dai->driver->remove_order == order) {
993 if (dai->driver->remove) {
994 err = dai->driver->remove(dai);
997 "ASoC: failed to remove %s: %d\n",
1004 static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
1006 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1009 /* unregister the rtd device */
1010 if (rtd->dev_registered) {
1011 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1012 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1013 device_unregister(rtd->dev);
1014 rtd->dev_registered = 0;
1017 /* remove the CODEC DAI */
1018 for (i = 0; i < rtd->num_codecs; i++)
1019 soc_remove_dai(rtd->codec_dais[i], order);
1021 soc_remove_dai(rtd->cpu_dai, order);
1024 static void soc_remove_link_components(struct snd_soc_card *card, int num,
1027 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1028 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1029 struct snd_soc_platform *platform = rtd->platform;
1030 struct snd_soc_component *component;
1033 /* remove the platform */
1034 if (platform && platform->component.driver->remove_order == order)
1035 soc_remove_component(&platform->component);
1037 /* remove the CODEC-side CODEC */
1038 for (i = 0; i < rtd->num_codecs; i++) {
1039 component = rtd->codec_dais[i]->component;
1040 if (component->driver->remove_order == order)
1041 soc_remove_component(component);
1044 /* remove any CPU-side CODEC */
1046 if (cpu_dai->component->driver->remove_order == order)
1047 soc_remove_component(cpu_dai->component);
1051 static void soc_remove_dai_links(struct snd_soc_card *card)
1055 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1057 for (dai = 0; dai < card->num_rtd; dai++)
1058 soc_remove_link_dais(card, dai, order);
1061 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1063 for (dai = 0; dai < card->num_rtd; dai++)
1064 soc_remove_link_components(card, dai, order);
1070 static void soc_set_name_prefix(struct snd_soc_card *card,
1071 struct snd_soc_component *component)
1075 if (card->codec_conf == NULL)
1078 for (i = 0; i < card->num_configs; i++) {
1079 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1080 if (map->of_node && component->dev->of_node != map->of_node)
1082 if (map->dev_name && strcmp(component->name, map->dev_name))
1084 component->name_prefix = map->name_prefix;
1089 static int soc_probe_component(struct snd_soc_card *card,
1090 struct snd_soc_component *component)
1092 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1093 struct snd_soc_component *dai_component, *component2;
1094 struct snd_soc_dai *dai;
1097 if (component->probed)
1100 component->card = card;
1102 soc_set_name_prefix(card, component);
1104 if (!try_module_get(component->dev->driver->owner))
1107 soc_init_component_debugfs(component);
1109 if (component->dapm_widgets) {
1110 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1111 component->num_dapm_widgets);
1114 dev_err(component->dev,
1115 "Failed to create new controls %d\n", ret);
1121 * This is rather ugly, but certain platforms expect that the DAPM
1122 * widgets for the DAIs for components with the same parent device are
1123 * created in the platforms DAPM context. Until that is fixed we need to
1126 if (component->steal_sibling_dai_widgets) {
1127 dai_component = NULL;
1128 list_for_each_entry(component2, &component_list, list) {
1129 if (component == component2)
1132 if (component2->dev == component->dev &&
1133 !list_empty(&component2->dai_list)) {
1134 dai_component = component2;
1139 dai_component = component;
1140 list_for_each_entry(component2, &component_list, list) {
1141 if (component2->dev == component->dev &&
1142 component2->steal_sibling_dai_widgets) {
1143 dai_component = NULL;
1149 if (dai_component) {
1150 list_for_each_entry(dai, &dai_component->dai_list, list) {
1151 snd_soc_dapm_new_dai_widgets(dapm, dai);
1153 dev_err(component->dev,
1154 "Failed to create DAI widgets %d\n",
1161 if (component->probe) {
1162 ret = component->probe(component);
1164 dev_err(component->dev,
1165 "ASoC: failed to probe component %d\n", ret);
1169 WARN(dapm->idle_bias_off &&
1170 dapm->bias_level != SND_SOC_BIAS_OFF,
1171 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1175 if (component->controls)
1176 snd_soc_add_component_controls(component, component->controls,
1177 component->num_controls);
1178 if (component->dapm_routes)
1179 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1180 component->num_dapm_routes);
1182 component->probed = 1;
1183 list_add(&dapm->list, &card->dapm_list);
1185 /* This is a HACK and will be removed soon */
1186 if (component->codec)
1187 list_add(&component->codec->card_list, &card->codec_dev_list);
1192 soc_cleanup_component_debugfs(component);
1193 module_put(component->dev->driver->owner);
1198 static void rtd_release(struct device *dev)
1203 static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1208 /* register the rtd device */
1209 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1212 device_initialize(rtd->dev);
1213 rtd->dev->parent = rtd->card->dev;
1214 rtd->dev->release = rtd_release;
1215 rtd->dev->init_name = name;
1216 dev_set_drvdata(rtd->dev, rtd);
1217 mutex_init(&rtd->pcm_mutex);
1218 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1219 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1220 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1221 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1222 ret = device_add(rtd->dev);
1224 /* calling put_device() here to free the rtd->dev */
1225 put_device(rtd->dev);
1226 dev_err(rtd->card->dev,
1227 "ASoC: failed to register runtime device: %d\n", ret);
1230 rtd->dev_registered = 1;
1233 /* add DAPM sysfs entries for this codec */
1234 ret = snd_soc_dapm_sys_add(rtd->dev);
1237 "ASoC: failed to add codec dapm sysfs entries: %d\n",
1240 /* add codec sysfs entries */
1241 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1244 "ASoC: failed to add codec sysfs files: %d\n",
1251 static int soc_probe_link_components(struct snd_soc_card *card, int num,
1254 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1255 struct snd_soc_platform *platform = rtd->platform;
1256 struct snd_soc_component *component;
1259 /* probe the CPU-side component, if it is a CODEC */
1260 component = rtd->cpu_dai->component;
1261 if (component->driver->probe_order == order) {
1262 ret = soc_probe_component(card, component);
1267 /* probe the CODEC-side components */
1268 for (i = 0; i < rtd->num_codecs; i++) {
1269 component = rtd->codec_dais[i]->component;
1270 if (component->driver->probe_order == order) {
1271 ret = soc_probe_component(card, component);
1277 /* probe the platform */
1278 if (platform->component.driver->probe_order == order) {
1279 ret = soc_probe_component(card, &platform->component);
1287 static int soc_probe_codec_dai(struct snd_soc_card *card,
1288 struct snd_soc_dai *codec_dai,
1293 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1294 if (codec_dai->driver->probe) {
1295 ret = codec_dai->driver->probe(codec_dai);
1297 dev_err(codec_dai->dev,
1298 "ASoC: failed to probe CODEC DAI %s: %d\n",
1299 codec_dai->name, ret);
1304 /* mark codec_dai as probed and add to card dai list */
1305 codec_dai->probed = 1;
1311 static int soc_link_dai_widgets(struct snd_soc_card *card,
1312 struct snd_soc_dai_link *dai_link,
1313 struct snd_soc_pcm_runtime *rtd)
1315 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1316 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1317 struct snd_soc_dapm_widget *play_w, *capture_w;
1320 if (rtd->num_codecs > 1)
1321 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1323 /* link the DAI widgets */
1324 play_w = codec_dai->playback_widget;
1325 capture_w = cpu_dai->capture_widget;
1326 if (play_w && capture_w) {
1327 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1330 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1331 play_w->name, capture_w->name, ret);
1336 play_w = cpu_dai->playback_widget;
1337 capture_w = codec_dai->capture_widget;
1338 if (play_w && capture_w) {
1339 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1342 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1343 play_w->name, capture_w->name, ret);
1351 static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1353 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1354 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1355 struct snd_soc_platform *platform = rtd->platform;
1356 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1359 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1360 card->name, num, order);
1362 /* config components */
1363 cpu_dai->platform = platform;
1364 cpu_dai->card = card;
1365 for (i = 0; i < rtd->num_codecs; i++)
1366 rtd->codec_dais[i]->card = card;
1368 /* set default power off timeout */
1369 rtd->pmdown_time = pmdown_time;
1371 /* probe the cpu_dai */
1372 if (!cpu_dai->probed &&
1373 cpu_dai->driver->probe_order == order) {
1374 if (cpu_dai->driver->probe) {
1375 ret = cpu_dai->driver->probe(cpu_dai);
1377 dev_err(cpu_dai->dev,
1378 "ASoC: failed to probe CPU DAI %s: %d\n",
1379 cpu_dai->name, ret);
1383 cpu_dai->probed = 1;
1386 /* probe the CODEC DAI */
1387 for (i = 0; i < rtd->num_codecs; i++) {
1388 ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order);
1393 /* complete DAI probe during last probe */
1394 if (order != SND_SOC_COMP_ORDER_LAST)
1397 /* do machine specific initialization */
1398 if (dai_link->init) {
1399 ret = dai_link->init(rtd);
1401 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1402 dai_link->name, ret);
1407 ret = soc_post_component_init(rtd, dai_link->name);
1411 #ifdef CONFIG_DEBUG_FS
1412 /* add DPCM sysfs entries */
1413 if (dai_link->dynamic) {
1414 ret = soc_dpcm_debugfs_add(rtd);
1417 "ASoC: failed to add dpcm sysfs entries: %d\n",
1424 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1426 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1429 if (cpu_dai->driver->compress_dai) {
1430 /*create compress_device"*/
1431 ret = soc_new_compress(rtd, num);
1433 dev_err(card->dev, "ASoC: can't create compress %s\n",
1434 dai_link->stream_name);
1439 if (!dai_link->params) {
1440 /* create the pcm */
1441 ret = soc_new_pcm(rtd, num);
1443 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1444 dai_link->stream_name, ret);
1448 INIT_DELAYED_WORK(&rtd->delayed_work,
1449 codec2codec_close_delayed_work);
1451 /* link the DAI widgets */
1452 ret = soc_link_dai_widgets(card, dai_link, rtd);
1458 /* add platform data for AC97 devices */
1459 for (i = 0; i < rtd->num_codecs; i++) {
1460 if (rtd->codec_dais[i]->driver->ac97_control)
1461 snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
1462 rtd->cpu_dai->ac97_pdata);
1468 #ifdef CONFIG_SND_SOC_AC97_BUS
1469 static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1470 struct snd_soc_dai *codec_dai)
1474 /* Only instantiate AC97 if not already done by the adaptor
1475 * for the generic AC97 subsystem.
1477 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
1479 * It is possible that the AC97 device is already registered to
1480 * the device subsystem. This happens when the device is created
1481 * via snd_ac97_mixer(). Currently only SoC codec that does so
1482 * is the generic AC97 glue but others migh emerge.
1484 * In those cases we don't try to register the device again.
1486 if (!codec->ac97_created)
1489 ret = soc_ac97_dev_register(codec);
1492 "ASoC: AC97 device register failed: %d\n", ret);
1496 codec->ac97_registered = 1;
1501 static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
1503 if (codec->ac97_registered) {
1504 soc_ac97_dev_unregister(codec);
1505 codec->ac97_registered = 0;
1509 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1513 for (i = 0; i < rtd->num_codecs; i++) {
1514 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1516 ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
1519 soc_unregister_ac97_codec(codec_dai->codec);
1527 static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1531 for (i = 0; i < rtd->num_codecs; i++)
1532 soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
1536 static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1538 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1539 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1540 const char *name = aux_dev->codec_name;
1542 rtd->component = soc_find_component(aux_dev->codec_of_node, name);
1543 if (!rtd->component) {
1544 if (aux_dev->codec_of_node)
1545 name = of_node_full_name(aux_dev->codec_of_node);
1547 dev_err(card->dev, "ASoC: %s not registered\n", name);
1548 return -EPROBE_DEFER;
1552 * Some places still reference rtd->codec, so we have to keep that
1553 * initialized if the component is a CODEC. Once all those references
1554 * have been removed, this code can be removed as well.
1556 rtd->codec = rtd->component->codec;
1561 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1563 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1564 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1567 ret = soc_probe_component(card, rtd->component);
1571 /* do machine specific initialization */
1572 if (aux_dev->init) {
1573 ret = aux_dev->init(rtd->component);
1575 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1576 aux_dev->name, ret);
1581 return soc_post_component_init(rtd, aux_dev->name);
1584 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1586 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1587 struct snd_soc_component *component = rtd->component;
1589 /* unregister the rtd device */
1590 if (rtd->dev_registered) {
1591 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1592 device_unregister(rtd->dev);
1593 rtd->dev_registered = 0;
1596 if (component && component->probed)
1597 soc_remove_component(component);
1600 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1604 if (codec->cache_init)
1607 ret = snd_soc_cache_init(codec);
1610 "ASoC: Failed to set cache compression type: %d\n",
1614 codec->cache_init = 1;
1618 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1620 struct snd_soc_codec *codec;
1621 struct snd_soc_dai_link *dai_link;
1622 int ret, i, order, dai_fmt;
1624 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1627 for (i = 0; i < card->num_links; i++) {
1628 ret = soc_bind_dai_link(card, i);
1633 /* bind aux_devs too */
1634 for (i = 0; i < card->num_aux_devs; i++) {
1635 ret = soc_bind_aux_dev(card, i);
1640 /* initialize the register cache for each available codec */
1641 list_for_each_entry(codec, &codec_list, list) {
1642 if (codec->cache_init)
1644 ret = snd_soc_init_codec_cache(codec);
1649 /* card bind complete so register a sound card */
1650 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1651 card->owner, 0, &card->snd_card);
1654 "ASoC: can't create sound card for card %s: %d\n",
1659 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1660 card->dapm.dev = card->dev;
1661 card->dapm.card = card;
1662 list_add(&card->dapm.list, &card->dapm_list);
1664 #ifdef CONFIG_DEBUG_FS
1665 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1668 #ifdef CONFIG_PM_SLEEP
1669 /* deferred resume work */
1670 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1673 if (card->dapm_widgets)
1674 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1675 card->num_dapm_widgets);
1677 /* initialise the sound card only once */
1679 ret = card->probe(card);
1681 goto card_probe_error;
1684 /* probe all components used by DAI links on this card */
1685 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1687 for (i = 0; i < card->num_links; i++) {
1688 ret = soc_probe_link_components(card, i, order);
1691 "ASoC: failed to instantiate card %d\n",
1698 /* probe all DAI links on this card */
1699 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1701 for (i = 0; i < card->num_links; i++) {
1702 ret = soc_probe_link_dais(card, i, order);
1705 "ASoC: failed to instantiate card %d\n",
1712 for (i = 0; i < card->num_aux_devs; i++) {
1713 ret = soc_probe_aux_dev(card, i);
1716 "ASoC: failed to add auxiliary devices %d\n",
1718 goto probe_aux_dev_err;
1722 snd_soc_dapm_link_dai_widgets(card);
1723 snd_soc_dapm_connect_dai_link_widgets(card);
1726 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1728 if (card->dapm_routes)
1729 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1730 card->num_dapm_routes);
1732 for (i = 0; i < card->num_links; i++) {
1733 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1734 dai_link = &card->dai_link[i];
1735 dai_fmt = dai_link->dai_fmt;
1738 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1741 for (j = 0; j < rtd->num_codecs; j++) {
1742 struct snd_soc_dai *codec_dai = codec_dais[j];
1744 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1745 if (ret != 0 && ret != -ENOTSUPP)
1746 dev_warn(codec_dai->dev,
1747 "ASoC: Failed to set DAI format: %d\n",
1752 /* If this is a regular CPU link there will be a platform */
1754 (dai_link->platform_name || dai_link->platform_of_node)) {
1755 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1757 if (ret != 0 && ret != -ENOTSUPP)
1758 dev_warn(card->rtd[i].cpu_dai->dev,
1759 "ASoC: Failed to set DAI format: %d\n",
1761 } else if (dai_fmt) {
1762 /* Flip the polarity for the "CPU" end */
1763 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1764 switch (dai_link->dai_fmt &
1765 SND_SOC_DAIFMT_MASTER_MASK) {
1766 case SND_SOC_DAIFMT_CBM_CFM:
1767 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1769 case SND_SOC_DAIFMT_CBM_CFS:
1770 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1772 case SND_SOC_DAIFMT_CBS_CFM:
1773 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1775 case SND_SOC_DAIFMT_CBS_CFS:
1776 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1780 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1782 if (ret != 0 && ret != -ENOTSUPP)
1783 dev_warn(card->rtd[i].cpu_dai->dev,
1784 "ASoC: Failed to set DAI format: %d\n",
1789 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1791 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1792 "%s", card->long_name ? card->long_name : card->name);
1793 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1794 "%s", card->driver_name ? card->driver_name : card->name);
1795 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1796 switch (card->snd_card->driver[i]) {
1802 if (!isalnum(card->snd_card->driver[i]))
1803 card->snd_card->driver[i] = '_';
1808 if (card->late_probe) {
1809 ret = card->late_probe(card);
1811 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
1813 goto probe_aux_dev_err;
1817 if (card->fully_routed)
1818 snd_soc_dapm_auto_nc_pins(card);
1820 snd_soc_dapm_new_widgets(card);
1822 ret = snd_card_register(card->snd_card);
1824 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1826 goto probe_aux_dev_err;
1829 #ifdef CONFIG_SND_SOC_AC97_BUS
1830 /* register any AC97 codecs */
1831 for (i = 0; i < card->num_rtd; i++) {
1832 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1835 "ASoC: failed to register AC97: %d\n", ret);
1837 soc_unregister_ac97_dai_link(&card->rtd[i]);
1838 goto probe_aux_dev_err;
1843 card->instantiated = 1;
1844 snd_soc_dapm_sync(&card->dapm);
1845 mutex_unlock(&card->mutex);
1850 for (i = 0; i < card->num_aux_devs; i++)
1851 soc_remove_aux_dev(card, i);
1854 soc_remove_dai_links(card);
1860 snd_card_free(card->snd_card);
1863 mutex_unlock(&card->mutex);
1868 /* probes a new socdev */
1869 static int soc_probe(struct platform_device *pdev)
1871 struct snd_soc_card *card = platform_get_drvdata(pdev);
1874 * no card, so machine driver should be registering card
1875 * we should not be here in that case so ret error
1880 dev_warn(&pdev->dev,
1881 "ASoC: machine %s should use snd_soc_register_card()\n",
1884 /* Bodge while we unpick instantiation */
1885 card->dev = &pdev->dev;
1887 return snd_soc_register_card(card);
1890 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1894 /* make sure any delayed work runs */
1895 for (i = 0; i < card->num_rtd; i++) {
1896 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1897 flush_delayed_work(&rtd->delayed_work);
1900 /* remove auxiliary devices */
1901 for (i = 0; i < card->num_aux_devs; i++)
1902 soc_remove_aux_dev(card, i);
1904 /* remove and free each DAI */
1905 soc_remove_dai_links(card);
1907 soc_cleanup_card_debugfs(card);
1909 /* remove the card */
1913 snd_soc_dapm_free(&card->dapm);
1915 snd_card_free(card->snd_card);
1920 /* removes a socdev */
1921 static int soc_remove(struct platform_device *pdev)
1923 struct snd_soc_card *card = platform_get_drvdata(pdev);
1925 snd_soc_unregister_card(card);
1929 int snd_soc_poweroff(struct device *dev)
1931 struct snd_soc_card *card = dev_get_drvdata(dev);
1934 if (!card->instantiated)
1937 /* Flush out pmdown_time work - we actually do want to run it
1938 * now, we're shutting down so no imminent restart. */
1939 for (i = 0; i < card->num_rtd; i++) {
1940 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1941 flush_delayed_work(&rtd->delayed_work);
1944 snd_soc_dapm_shutdown(card);
1946 /* deactivate pins to sleep state */
1947 for (i = 0; i < card->num_rtd; i++) {
1948 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1949 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1952 pinctrl_pm_select_sleep_state(cpu_dai->dev);
1953 for (j = 0; j < rtd->num_codecs; j++) {
1954 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
1955 pinctrl_pm_select_sleep_state(codec_dai->dev);
1961 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1963 const struct dev_pm_ops snd_soc_pm_ops = {
1964 .suspend = snd_soc_suspend,
1965 .resume = snd_soc_resume,
1966 .freeze = snd_soc_suspend,
1967 .thaw = snd_soc_resume,
1968 .poweroff = snd_soc_poweroff,
1969 .restore = snd_soc_resume,
1971 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1973 /* ASoC platform driver */
1974 static struct platform_driver soc_driver = {
1976 .name = "soc-audio",
1977 .owner = THIS_MODULE,
1978 .pm = &snd_soc_pm_ops,
1981 .remove = soc_remove,
1985 * snd_soc_new_ac97_codec - initailise AC97 device
1986 * @codec: audio codec
1987 * @ops: AC97 bus operations
1988 * @num: AC97 codec number
1990 * Initialises AC97 codec resources for use by ad-hoc devices only.
1992 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1993 struct snd_ac97_bus_ops *ops, int num)
1995 mutex_lock(&codec->mutex);
1997 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1998 if (codec->ac97 == NULL) {
1999 mutex_unlock(&codec->mutex);
2003 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2004 if (codec->ac97->bus == NULL) {
2007 mutex_unlock(&codec->mutex);
2011 codec->ac97->bus->ops = ops;
2012 codec->ac97->num = num;
2015 * Mark the AC97 device to be created by us. This way we ensure that the
2016 * device will be registered with the device subsystem later on.
2018 codec->ac97_created = 1;
2020 mutex_unlock(&codec->mutex);
2023 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2025 static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2027 static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2029 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2031 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2033 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2037 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2039 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2043 static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2045 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2047 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2049 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2050 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2051 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2055 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2057 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2061 static int snd_soc_ac97_parse_pinctl(struct device *dev,
2062 struct snd_ac97_reset_cfg *cfg)
2065 struct pinctrl_state *state;
2069 p = devm_pinctrl_get(dev);
2071 dev_err(dev, "Failed to get pinctrl\n");
2076 state = pinctrl_lookup_state(p, "ac97-reset");
2077 if (IS_ERR(state)) {
2078 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
2079 return PTR_ERR(state);
2081 cfg->pstate_reset = state;
2083 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2084 if (IS_ERR(state)) {
2085 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
2086 return PTR_ERR(state);
2088 cfg->pstate_warm_reset = state;
2090 state = pinctrl_lookup_state(p, "ac97-running");
2091 if (IS_ERR(state)) {
2092 dev_err(dev, "Can't find pinctrl state ac97-running\n");
2093 return PTR_ERR(state);
2095 cfg->pstate_run = state;
2097 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2099 dev_err(dev, "Can't find ac97-sync gpio\n");
2102 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2104 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2107 cfg->gpio_sync = gpio;
2109 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2111 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2114 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2116 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2119 cfg->gpio_sdata = gpio;
2121 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2123 dev_err(dev, "Can't find ac97-reset gpio\n");
2126 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2128 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2131 cfg->gpio_reset = gpio;
2136 struct snd_ac97_bus_ops *soc_ac97_ops;
2137 EXPORT_SYMBOL_GPL(soc_ac97_ops);
2139 int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2141 if (ops == soc_ac97_ops)
2144 if (soc_ac97_ops && ops)
2151 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2154 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2156 * This function sets the reset and warm_reset properties of ops and parses
2157 * the device node of pdev to get pinctrl states and gpio numbers to use.
2159 int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2160 struct platform_device *pdev)
2162 struct device *dev = &pdev->dev;
2163 struct snd_ac97_reset_cfg cfg;
2166 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2170 ret = snd_soc_set_ac97_ops(ops);
2174 ops->warm_reset = snd_soc_ac97_warm_reset;
2175 ops->reset = snd_soc_ac97_reset;
2177 snd_ac97_rst_cfg = cfg;
2180 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2183 * snd_soc_free_ac97_codec - free AC97 codec device
2184 * @codec: audio codec
2186 * Frees AC97 codec device resources.
2188 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2190 mutex_lock(&codec->mutex);
2191 #ifdef CONFIG_SND_SOC_AC97_BUS
2192 soc_unregister_ac97_codec(codec);
2194 kfree(codec->ac97->bus);
2197 codec->ac97_created = 0;
2198 mutex_unlock(&codec->mutex);
2200 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2203 * snd_soc_cnew - create new control
2204 * @_template: control template
2205 * @data: control private data
2206 * @long_name: control long name
2207 * @prefix: control name prefix
2209 * Create a new mixer control from a template control.
2211 * Returns 0 for success, else error.
2213 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2214 void *data, const char *long_name,
2217 struct snd_kcontrol_new template;
2218 struct snd_kcontrol *kcontrol;
2221 memcpy(&template, _template, sizeof(template));
2225 long_name = template.name;
2228 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2232 template.name = name;
2234 template.name = long_name;
2237 kcontrol = snd_ctl_new1(&template, data);
2243 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2245 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2246 const struct snd_kcontrol_new *controls, int num_controls,
2247 const char *prefix, void *data)
2251 for (i = 0; i < num_controls; i++) {
2252 const struct snd_kcontrol_new *control = &controls[i];
2253 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2254 control->name, prefix));
2256 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2257 control->name, err);
2265 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2268 struct snd_card *card = soc_card->snd_card;
2269 struct snd_kcontrol *kctl;
2271 if (unlikely(!name))
2274 list_for_each_entry(kctl, &card->controls, list)
2275 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2279 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2282 * snd_soc_add_component_controls - Add an array of controls to a component.
2284 * @component: Component to add controls to
2285 * @controls: Array of controls to add
2286 * @num_controls: Number of elements in the array
2288 * Return: 0 for success, else error.
2290 int snd_soc_add_component_controls(struct snd_soc_component *component,
2291 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2293 struct snd_card *card = component->card->snd_card;
2295 return snd_soc_add_controls(card, component->dev, controls,
2296 num_controls, component->name_prefix, component);
2298 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2301 * snd_soc_add_codec_controls - add an array of controls to a codec.
2302 * Convenience function to add a list of controls. Many codecs were
2303 * duplicating this code.
2305 * @codec: codec to add controls to
2306 * @controls: array of controls to add
2307 * @num_controls: number of elements in the array
2309 * Return 0 for success, else error.
2311 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2312 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2314 return snd_soc_add_component_controls(&codec->component, controls,
2317 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2320 * snd_soc_add_platform_controls - add an array of controls to a platform.
2321 * Convenience function to add a list of controls.
2323 * @platform: platform to add controls to
2324 * @controls: array of controls to add
2325 * @num_controls: number of elements in the array
2327 * Return 0 for success, else error.
2329 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2330 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2332 return snd_soc_add_component_controls(&platform->component, controls,
2335 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2338 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2339 * Convenience function to add a list of controls.
2341 * @soc_card: SoC card to add controls to
2342 * @controls: array of controls to add
2343 * @num_controls: number of elements in the array
2345 * Return 0 for success, else error.
2347 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2348 const struct snd_kcontrol_new *controls, int num_controls)
2350 struct snd_card *card = soc_card->snd_card;
2352 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2355 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2358 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2359 * Convienience function to add a list of controls.
2361 * @dai: DAI to add controls to
2362 * @controls: array of controls to add
2363 * @num_controls: number of elements in the array
2365 * Return 0 for success, else error.
2367 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2368 const struct snd_kcontrol_new *controls, int num_controls)
2370 struct snd_card *card = dai->card->snd_card;
2372 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2375 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2378 * snd_soc_info_enum_double - enumerated double mixer info callback
2379 * @kcontrol: mixer control
2380 * @uinfo: control element information
2382 * Callback to provide information about a double enumerated
2385 * Returns 0 for success.
2387 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2388 struct snd_ctl_elem_info *uinfo)
2390 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2392 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2393 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2394 uinfo->value.enumerated.items = e->items;
2396 if (uinfo->value.enumerated.item >= e->items)
2397 uinfo->value.enumerated.item = e->items - 1;
2398 strlcpy(uinfo->value.enumerated.name,
2399 e->texts[uinfo->value.enumerated.item],
2400 sizeof(uinfo->value.enumerated.name));
2403 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2406 * snd_soc_get_enum_double - enumerated double mixer get callback
2407 * @kcontrol: mixer control
2408 * @ucontrol: control element information
2410 * Callback to get the value of a double enumerated mixer.
2412 * Returns 0 for success.
2414 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2415 struct snd_ctl_elem_value *ucontrol)
2417 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2418 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2419 unsigned int val, item;
2420 unsigned int reg_val;
2423 ret = snd_soc_component_read(component, e->reg, ®_val);
2426 val = (reg_val >> e->shift_l) & e->mask;
2427 item = snd_soc_enum_val_to_item(e, val);
2428 ucontrol->value.enumerated.item[0] = item;
2429 if (e->shift_l != e->shift_r) {
2430 val = (reg_val >> e->shift_l) & e->mask;
2431 item = snd_soc_enum_val_to_item(e, val);
2432 ucontrol->value.enumerated.item[1] = item;
2437 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2440 * snd_soc_put_enum_double - enumerated double mixer put callback
2441 * @kcontrol: mixer control
2442 * @ucontrol: control element information
2444 * Callback to set the value of a double enumerated mixer.
2446 * Returns 0 for success.
2448 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2449 struct snd_ctl_elem_value *ucontrol)
2451 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2452 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2453 unsigned int *item = ucontrol->value.enumerated.item;
2457 if (item[0] >= e->items)
2459 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
2460 mask = e->mask << e->shift_l;
2461 if (e->shift_l != e->shift_r) {
2462 if (item[1] >= e->items)
2464 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
2465 mask |= e->mask << e->shift_r;
2468 return snd_soc_component_update_bits(component, e->reg, mask, val);
2470 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2473 * snd_soc_read_signed - Read a codec register and interprete as signed value
2474 * @component: component
2475 * @reg: Register to read
2476 * @mask: Mask to use after shifting the register value
2477 * @shift: Right shift of register value
2478 * @sign_bit: Bit that describes if a number is negative or not.
2479 * @signed_val: Pointer to where the read value should be stored
2481 * This functions reads a codec register. The register value is shifted right
2482 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2483 * the given registervalue into a signed integer if sign_bit is non-zero.
2485 * Returns 0 on sucess, otherwise an error value
2487 static int snd_soc_read_signed(struct snd_soc_component *component,
2488 unsigned int reg, unsigned int mask, unsigned int shift,
2489 unsigned int sign_bit, int *signed_val)
2494 ret = snd_soc_component_read(component, reg, &val);
2498 val = (val >> shift) & mask;
2505 /* non-negative number */
2506 if (!(val & BIT(sign_bit))) {
2514 * The register most probably does not contain a full-sized int.
2515 * Instead we have an arbitrary number of bits in a signed
2516 * representation which has to be translated into a full-sized int.
2517 * This is done by filling up all bits above the sign-bit.
2519 ret |= ~((int)(BIT(sign_bit) - 1));
2527 * snd_soc_info_volsw - single mixer info callback
2528 * @kcontrol: mixer control
2529 * @uinfo: control element information
2531 * Callback to provide information about a single mixer control, or a double
2532 * mixer control that spans 2 registers.
2534 * Returns 0 for success.
2536 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2537 struct snd_ctl_elem_info *uinfo)
2539 struct soc_mixer_control *mc =
2540 (struct soc_mixer_control *)kcontrol->private_value;
2543 if (!mc->platform_max)
2544 mc->platform_max = mc->max;
2545 platform_max = mc->platform_max;
2547 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2548 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2550 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2552 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2553 uinfo->value.integer.min = 0;
2554 uinfo->value.integer.max = platform_max - mc->min;
2557 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2560 * snd_soc_get_volsw - single mixer get callback
2561 * @kcontrol: mixer control
2562 * @ucontrol: control element information
2564 * Callback to get the value of a single mixer control, or a double mixer
2565 * control that spans 2 registers.
2567 * Returns 0 for success.
2569 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2570 struct snd_ctl_elem_value *ucontrol)
2572 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2573 struct soc_mixer_control *mc =
2574 (struct soc_mixer_control *)kcontrol->private_value;
2575 unsigned int reg = mc->reg;
2576 unsigned int reg2 = mc->rreg;
2577 unsigned int shift = mc->shift;
2578 unsigned int rshift = mc->rshift;
2581 int sign_bit = mc->sign_bit;
2582 unsigned int mask = (1 << fls(max)) - 1;
2583 unsigned int invert = mc->invert;
2588 mask = BIT(sign_bit + 1) - 1;
2590 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2594 ucontrol->value.integer.value[0] = val - min;
2596 ucontrol->value.integer.value[0] =
2597 max - ucontrol->value.integer.value[0];
2599 if (snd_soc_volsw_is_stereo(mc)) {
2601 ret = snd_soc_read_signed(component, reg, mask, rshift,
2604 ret = snd_soc_read_signed(component, reg2, mask, shift,
2609 ucontrol->value.integer.value[1] = val - min;
2611 ucontrol->value.integer.value[1] =
2612 max - ucontrol->value.integer.value[1];
2617 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2620 * snd_soc_put_volsw - single mixer put callback
2621 * @kcontrol: mixer control
2622 * @ucontrol: control element information
2624 * Callback to set the value of a single mixer control, or a double mixer
2625 * control that spans 2 registers.
2627 * Returns 0 for success.
2629 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2630 struct snd_ctl_elem_value *ucontrol)
2632 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2633 struct soc_mixer_control *mc =
2634 (struct soc_mixer_control *)kcontrol->private_value;
2635 unsigned int reg = mc->reg;
2636 unsigned int reg2 = mc->rreg;
2637 unsigned int shift = mc->shift;
2638 unsigned int rshift = mc->rshift;
2641 unsigned int sign_bit = mc->sign_bit;
2642 unsigned int mask = (1 << fls(max)) - 1;
2643 unsigned int invert = mc->invert;
2645 bool type_2r = false;
2646 unsigned int val2 = 0;
2647 unsigned int val, val_mask;
2650 mask = BIT(sign_bit + 1) - 1;
2652 val = ((ucontrol->value.integer.value[0] + min) & mask);
2655 val_mask = mask << shift;
2657 if (snd_soc_volsw_is_stereo(mc)) {
2658 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
2662 val_mask |= mask << rshift;
2663 val |= val2 << rshift;
2665 val2 = val2 << shift;
2669 err = snd_soc_component_update_bits(component, reg, val_mask, val);
2674 err = snd_soc_component_update_bits(component, reg2, val_mask,
2679 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2682 * snd_soc_get_volsw_sx - single mixer get callback
2683 * @kcontrol: mixer control
2684 * @ucontrol: control element information
2686 * Callback to get the value of a single mixer control, or a double mixer
2687 * control that spans 2 registers.
2689 * Returns 0 for success.
2691 int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2692 struct snd_ctl_elem_value *ucontrol)
2694 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2695 struct soc_mixer_control *mc =
2696 (struct soc_mixer_control *)kcontrol->private_value;
2697 unsigned int reg = mc->reg;
2698 unsigned int reg2 = mc->rreg;
2699 unsigned int shift = mc->shift;
2700 unsigned int rshift = mc->rshift;
2703 int mask = (1 << (fls(min + max) - 1)) - 1;
2707 ret = snd_soc_component_read(component, reg, &val);
2711 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2713 if (snd_soc_volsw_is_stereo(mc)) {
2714 ret = snd_soc_component_read(component, reg2, &val);
2718 val = ((val >> rshift) - min) & mask;
2719 ucontrol->value.integer.value[1] = val;
2724 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2727 * snd_soc_put_volsw_sx - double mixer set callback
2728 * @kcontrol: mixer control
2729 * @uinfo: control element information
2731 * Callback to set the value of a double mixer control that spans 2 registers.
2733 * Returns 0 for success.
2735 int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2736 struct snd_ctl_elem_value *ucontrol)
2738 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2739 struct soc_mixer_control *mc =
2740 (struct soc_mixer_control *)kcontrol->private_value;
2742 unsigned int reg = mc->reg;
2743 unsigned int reg2 = mc->rreg;
2744 unsigned int shift = mc->shift;
2745 unsigned int rshift = mc->rshift;
2748 int mask = (1 << (fls(min + max) - 1)) - 1;
2750 unsigned int val, val_mask, val2 = 0;
2752 val_mask = mask << shift;
2753 val = (ucontrol->value.integer.value[0] + min) & mask;
2756 err = snd_soc_component_update_bits(component, reg, val_mask, val);
2760 if (snd_soc_volsw_is_stereo(mc)) {
2761 val_mask = mask << rshift;
2762 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2763 val2 = val2 << rshift;
2765 err = snd_soc_component_update_bits(component, reg2, val_mask,
2770 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2773 * snd_soc_info_volsw_s8 - signed mixer info callback
2774 * @kcontrol: mixer control
2775 * @uinfo: control element information
2777 * Callback to provide information about a signed mixer control.
2779 * Returns 0 for success.
2781 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2782 struct snd_ctl_elem_info *uinfo)
2784 struct soc_mixer_control *mc =
2785 (struct soc_mixer_control *)kcontrol->private_value;
2789 if (!mc->platform_max)
2790 mc->platform_max = mc->max;
2791 platform_max = mc->platform_max;
2793 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2795 uinfo->value.integer.min = 0;
2796 uinfo->value.integer.max = platform_max - min;
2799 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2802 * snd_soc_get_volsw_s8 - signed mixer get callback
2803 * @kcontrol: mixer control
2804 * @ucontrol: control element information
2806 * Callback to get the value of a signed mixer control.
2808 * Returns 0 for success.
2810 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2811 struct snd_ctl_elem_value *ucontrol)
2813 struct soc_mixer_control *mc =
2814 (struct soc_mixer_control *)kcontrol->private_value;
2815 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2816 unsigned int reg = mc->reg;
2821 ret = snd_soc_component_read(component, reg, &val);
2825 ucontrol->value.integer.value[0] =
2826 ((signed char)(val & 0xff))-min;
2827 ucontrol->value.integer.value[1] =
2828 ((signed char)((val >> 8) & 0xff))-min;
2831 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2834 * snd_soc_put_volsw_sgn - signed mixer put callback
2835 * @kcontrol: mixer control
2836 * @ucontrol: control element information
2838 * Callback to set the value of a signed mixer control.
2840 * Returns 0 for success.
2842 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2843 struct snd_ctl_elem_value *ucontrol)
2845 struct soc_mixer_control *mc =
2846 (struct soc_mixer_control *)kcontrol->private_value;
2847 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2848 unsigned int reg = mc->reg;
2852 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2853 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2855 return snd_soc_component_update_bits(component, reg, 0xffff, val);
2857 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2860 * snd_soc_info_volsw_range - single mixer info callback with range.
2861 * @kcontrol: mixer control
2862 * @uinfo: control element information
2864 * Callback to provide information, within a range, about a single
2867 * returns 0 for success.
2869 int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2870 struct snd_ctl_elem_info *uinfo)
2872 struct soc_mixer_control *mc =
2873 (struct soc_mixer_control *)kcontrol->private_value;
2877 if (!mc->platform_max)
2878 mc->platform_max = mc->max;
2879 platform_max = mc->platform_max;
2881 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2882 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2883 uinfo->value.integer.min = 0;
2884 uinfo->value.integer.max = platform_max - min;
2888 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2891 * snd_soc_put_volsw_range - single mixer put value callback with range.
2892 * @kcontrol: mixer control
2893 * @ucontrol: control element information
2895 * Callback to set the value, within a range, for a single mixer control.
2897 * Returns 0 for success.
2899 int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2900 struct snd_ctl_elem_value *ucontrol)
2902 struct soc_mixer_control *mc =
2903 (struct soc_mixer_control *)kcontrol->private_value;
2904 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2905 unsigned int reg = mc->reg;
2906 unsigned int rreg = mc->rreg;
2907 unsigned int shift = mc->shift;
2910 unsigned int mask = (1 << fls(max)) - 1;
2911 unsigned int invert = mc->invert;
2912 unsigned int val, val_mask;
2915 val = ((ucontrol->value.integer.value[0] + min) & mask);
2918 val_mask = mask << shift;
2921 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
2925 if (snd_soc_volsw_is_stereo(mc)) {
2926 val = ((ucontrol->value.integer.value[1] + min) & mask);
2929 val_mask = mask << shift;
2932 ret = snd_soc_component_update_bits(component, rreg, val_mask,
2938 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2941 * snd_soc_get_volsw_range - single mixer get callback with range
2942 * @kcontrol: mixer control
2943 * @ucontrol: control element information
2945 * Callback to get the value, within a range, of a single mixer control.
2947 * Returns 0 for success.
2949 int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2950 struct snd_ctl_elem_value *ucontrol)
2952 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2953 struct soc_mixer_control *mc =
2954 (struct soc_mixer_control *)kcontrol->private_value;
2955 unsigned int reg = mc->reg;
2956 unsigned int rreg = mc->rreg;
2957 unsigned int shift = mc->shift;
2960 unsigned int mask = (1 << fls(max)) - 1;
2961 unsigned int invert = mc->invert;
2965 ret = snd_soc_component_read(component, reg, &val);
2969 ucontrol->value.integer.value[0] = (val >> shift) & mask;
2971 ucontrol->value.integer.value[0] =
2972 max - ucontrol->value.integer.value[0];
2973 ucontrol->value.integer.value[0] =
2974 ucontrol->value.integer.value[0] - min;
2976 if (snd_soc_volsw_is_stereo(mc)) {
2977 ret = snd_soc_component_read(component, rreg, &val);
2981 ucontrol->value.integer.value[1] = (val >> shift) & mask;
2983 ucontrol->value.integer.value[1] =
2984 max - ucontrol->value.integer.value[1];
2985 ucontrol->value.integer.value[1] =
2986 ucontrol->value.integer.value[1] - min;
2991 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
2994 * snd_soc_limit_volume - Set new limit to an existing volume control.
2996 * @codec: where to look for the control
2997 * @name: Name of the control
2998 * @max: new maximum limit
3000 * Return 0 for success, else error.
3002 int snd_soc_limit_volume(struct snd_soc_codec *codec,
3003 const char *name, int max)
3005 struct snd_card *card = codec->component.card->snd_card;
3006 struct snd_kcontrol *kctl;
3007 struct soc_mixer_control *mc;
3011 /* Sanity check for name and max */
3012 if (unlikely(!name || max <= 0))
3015 list_for_each_entry(kctl, &card->controls, list) {
3016 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3022 mc = (struct soc_mixer_control *)kctl->private_value;
3023 if (max <= mc->max) {
3024 mc->platform_max = max;
3030 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3032 int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3033 struct snd_ctl_elem_info *uinfo)
3035 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3036 struct soc_bytes *params = (void *)kcontrol->private_value;
3038 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3039 uinfo->count = params->num_regs * component->val_bytes;
3043 EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3045 int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3046 struct snd_ctl_elem_value *ucontrol)
3048 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3049 struct soc_bytes *params = (void *)kcontrol->private_value;
3052 if (component->regmap)
3053 ret = regmap_raw_read(component->regmap, params->base,
3054 ucontrol->value.bytes.data,
3055 params->num_regs * component->val_bytes);
3059 /* Hide any masked bytes to ensure consistent data reporting */
3060 if (ret == 0 && params->mask) {
3061 switch (component->val_bytes) {
3063 ucontrol->value.bytes.data[0] &= ~params->mask;
3066 ((u16 *)(&ucontrol->value.bytes.data))[0]
3067 &= cpu_to_be16(~params->mask);
3070 ((u32 *)(&ucontrol->value.bytes.data))[0]
3071 &= cpu_to_be32(~params->mask);
3080 EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3082 int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3083 struct snd_ctl_elem_value *ucontrol)
3085 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3086 struct soc_bytes *params = (void *)kcontrol->private_value;
3088 unsigned int val, mask;
3091 if (!component->regmap)
3094 len = params->num_regs * component->val_bytes;
3096 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3101 * If we've got a mask then we need to preserve the register
3102 * bits. We shouldn't modify the incoming data so take a
3106 ret = regmap_read(component->regmap, params->base, &val);
3110 val &= params->mask;
3112 switch (component->val_bytes) {
3114 ((u8 *)data)[0] &= ~params->mask;
3115 ((u8 *)data)[0] |= val;
3118 mask = ~params->mask;
3119 ret = regmap_parse_val(component->regmap,
3124 ((u16 *)data)[0] &= mask;
3126 ret = regmap_parse_val(component->regmap,
3131 ((u16 *)data)[0] |= val;
3134 mask = ~params->mask;
3135 ret = regmap_parse_val(component->regmap,
3140 ((u32 *)data)[0] &= mask;
3142 ret = regmap_parse_val(component->regmap,
3147 ((u32 *)data)[0] |= val;
3155 ret = regmap_raw_write(component->regmap, params->base,
3163 EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3165 int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3166 struct snd_ctl_elem_info *ucontrol)
3168 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3170 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3171 ucontrol->count = params->max;
3175 EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3177 int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
3178 unsigned int size, unsigned int __user *tlv)
3180 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3181 unsigned int count = size < params->max ? size : params->max;
3185 case SNDRV_CTL_TLV_OP_READ:
3187 ret = params->get(tlv, count);
3189 case SNDRV_CTL_TLV_OP_WRITE:
3191 ret = params->put(tlv, count);
3196 EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
3199 * snd_soc_info_xr_sx - signed multi register info callback
3200 * @kcontrol: mreg control
3201 * @uinfo: control element information
3203 * Callback to provide information of a control that can
3204 * span multiple codec registers which together
3205 * forms a single signed value in a MSB/LSB manner.
3207 * Returns 0 for success.
3209 int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3210 struct snd_ctl_elem_info *uinfo)
3212 struct soc_mreg_control *mc =
3213 (struct soc_mreg_control *)kcontrol->private_value;
3214 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3216 uinfo->value.integer.min = mc->min;
3217 uinfo->value.integer.max = mc->max;
3221 EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3224 * snd_soc_get_xr_sx - signed multi register get callback
3225 * @kcontrol: mreg control
3226 * @ucontrol: control element information
3228 * Callback to get the value of a control that can span
3229 * multiple codec registers which together forms a single
3230 * signed value in a MSB/LSB manner. The control supports
3231 * specifying total no of bits used to allow for bitfields
3232 * across the multiple codec registers.
3234 * Returns 0 for success.
3236 int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3237 struct snd_ctl_elem_value *ucontrol)
3239 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3240 struct soc_mreg_control *mc =
3241 (struct soc_mreg_control *)kcontrol->private_value;
3242 unsigned int regbase = mc->regbase;
3243 unsigned int regcount = mc->regcount;
3244 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
3245 unsigned int regwmask = (1<<regwshift)-1;
3246 unsigned int invert = mc->invert;
3247 unsigned long mask = (1UL<<mc->nbits)-1;
3251 unsigned int regval;
3255 for (i = 0; i < regcount; i++) {
3256 ret = snd_soc_component_read(component, regbase+i, ®val);
3259 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
3262 if (min < 0 && val > max)
3266 ucontrol->value.integer.value[0] = val;
3270 EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3273 * snd_soc_put_xr_sx - signed multi register get callback
3274 * @kcontrol: mreg control
3275 * @ucontrol: control element information
3277 * Callback to set the value of a control that can span
3278 * multiple codec registers which together forms a single
3279 * signed value in a MSB/LSB manner. The control supports
3280 * specifying total no of bits used to allow for bitfields
3281 * across the multiple codec registers.
3283 * Returns 0 for success.
3285 int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3286 struct snd_ctl_elem_value *ucontrol)
3288 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3289 struct soc_mreg_control *mc =
3290 (struct soc_mreg_control *)kcontrol->private_value;
3291 unsigned int regbase = mc->regbase;
3292 unsigned int regcount = mc->regcount;
3293 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
3294 unsigned int regwmask = (1<<regwshift)-1;
3295 unsigned int invert = mc->invert;
3296 unsigned long mask = (1UL<<mc->nbits)-1;
3298 long val = ucontrol->value.integer.value[0];
3299 unsigned int i, regval, regmask;
3305 for (i = 0; i < regcount; i++) {
3306 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3307 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3308 err = snd_soc_component_update_bits(component, regbase+i,
3316 EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3319 * snd_soc_get_strobe - strobe get callback
3320 * @kcontrol: mixer control
3321 * @ucontrol: control element information
3323 * Callback get the value of a strobe mixer control.
3325 * Returns 0 for success.
3327 int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3328 struct snd_ctl_elem_value *ucontrol)
3330 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3331 struct soc_mixer_control *mc =
3332 (struct soc_mixer_control *)kcontrol->private_value;
3333 unsigned int reg = mc->reg;
3334 unsigned int shift = mc->shift;
3335 unsigned int mask = 1 << shift;
3336 unsigned int invert = mc->invert != 0;
3340 ret = snd_soc_component_read(component, reg, &val);
3346 if (shift != 0 && val != 0)
3348 ucontrol->value.enumerated.item[0] = val ^ invert;
3352 EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3355 * snd_soc_put_strobe - strobe put callback
3356 * @kcontrol: mixer control
3357 * @ucontrol: control element information
3359 * Callback strobe a register bit to high then low (or the inverse)
3360 * in one pass of a single mixer enum control.
3362 * Returns 1 for success.
3364 int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3365 struct snd_ctl_elem_value *ucontrol)
3367 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3368 struct soc_mixer_control *mc =
3369 (struct soc_mixer_control *)kcontrol->private_value;
3370 unsigned int reg = mc->reg;
3371 unsigned int shift = mc->shift;
3372 unsigned int mask = 1 << shift;
3373 unsigned int invert = mc->invert != 0;
3374 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3375 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3376 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3379 err = snd_soc_component_update_bits(component, reg, mask, val1);
3383 return snd_soc_component_update_bits(component, reg, mask, val2);
3385 EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3388 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3390 * @clk_id: DAI specific clock ID
3391 * @freq: new clock frequency in Hz
3392 * @dir: new clock direction - input/output.
3394 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3396 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3397 unsigned int freq, int dir)
3399 if (dai->driver && dai->driver->ops->set_sysclk)
3400 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3401 else if (dai->codec && dai->codec->driver->set_sysclk)
3402 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3407 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3410 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3412 * @clk_id: DAI specific clock ID
3413 * @source: Source for the clock
3414 * @freq: new clock frequency in Hz
3415 * @dir: new clock direction - input/output.
3417 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3419 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3420 int source, unsigned int freq, int dir)
3422 if (codec->driver->set_sysclk)
3423 return codec->driver->set_sysclk(codec, clk_id, source,
3428 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3431 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3433 * @div_id: DAI specific clock divider ID
3434 * @div: new clock divisor.
3436 * Configures the clock dividers. This is used to derive the best DAI bit and
3437 * frame clocks from the system or master clock. It's best to set the DAI bit
3438 * and frame clocks as low as possible to save system power.
3440 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3441 int div_id, int div)
3443 if (dai->driver && dai->driver->ops->set_clkdiv)
3444 return dai->driver->ops->set_clkdiv(dai, div_id, div);
3448 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3451 * snd_soc_dai_set_pll - configure DAI PLL.
3453 * @pll_id: DAI specific PLL ID
3454 * @source: DAI specific source for the PLL
3455 * @freq_in: PLL input clock frequency in Hz
3456 * @freq_out: requested PLL output clock frequency in Hz
3458 * Configures and enables PLL to generate output clock based on input clock.
3460 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3461 unsigned int freq_in, unsigned int freq_out)
3463 if (dai->driver && dai->driver->ops->set_pll)
3464 return dai->driver->ops->set_pll(dai, pll_id, source,
3466 else if (dai->codec && dai->codec->driver->set_pll)
3467 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3472 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3475 * snd_soc_codec_set_pll - configure codec PLL.
3477 * @pll_id: DAI specific PLL ID
3478 * @source: DAI specific source for the PLL
3479 * @freq_in: PLL input clock frequency in Hz
3480 * @freq_out: requested PLL output clock frequency in Hz
3482 * Configures and enables PLL to generate output clock based on input clock.
3484 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3485 unsigned int freq_in, unsigned int freq_out)
3487 if (codec->driver->set_pll)
3488 return codec->driver->set_pll(codec, pll_id, source,
3493 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3496 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3498 * @ratio Ratio of BCLK to Sample rate.
3500 * Configures the DAI for a preset BCLK to sample rate ratio.
3502 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3504 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3505 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3509 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3512 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3514 * @fmt: SND_SOC_DAIFMT_ format value.
3516 * Configures the DAI hardware format and clocking.
3518 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3520 if (dai->driver == NULL)
3522 if (dai->driver->ops->set_fmt == NULL)
3524 return dai->driver->ops->set_fmt(dai, fmt);
3526 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3529 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
3530 * @slots: Number of slots in use.
3531 * @tx_mask: bitmask representing active TX slots.
3532 * @rx_mask: bitmask representing active RX slots.
3534 * Generates the TDM tx and rx slot default masks for DAI.
3536 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
3537 unsigned int *tx_mask,
3538 unsigned int *rx_mask)
3540 if (*tx_mask || *rx_mask)
3546 *tx_mask = (1 << slots) - 1;
3547 *rx_mask = (1 << slots) - 1;
3553 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3555 * @tx_mask: bitmask representing active TX slots.
3556 * @rx_mask: bitmask representing active RX slots.
3557 * @slots: Number of slots in use.
3558 * @slot_width: Width in bits for each slot.
3560 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3563 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3564 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
3566 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3567 dai->driver->ops->xlate_tdm_slot_mask(slots,
3568 &tx_mask, &rx_mask);
3570 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
3572 dai->tx_mask = tx_mask;
3573 dai->rx_mask = rx_mask;
3575 if (dai->driver && dai->driver->ops->set_tdm_slot)
3576 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3581 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3584 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3586 * @tx_num: how many TX channels
3587 * @tx_slot: pointer to an array which imply the TX slot number channel
3589 * @rx_num: how many RX channels
3590 * @rx_slot: pointer to an array which imply the RX slot number channel
3593 * configure the relationship between channel number and TDM slot number.
3595 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3596 unsigned int tx_num, unsigned int *tx_slot,
3597 unsigned int rx_num, unsigned int *rx_slot)
3599 if (dai->driver && dai->driver->ops->set_channel_map)
3600 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3605 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3608 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3610 * @tristate: tristate enable
3612 * Tristates the DAI so that others can use it.
3614 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3616 if (dai->driver && dai->driver->ops->set_tristate)
3617 return dai->driver->ops->set_tristate(dai, tristate);
3621 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3624 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3626 * @mute: mute enable
3627 * @direction: stream to mute
3629 * Mutes the DAI DAC.
3631 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3637 if (dai->driver->ops->mute_stream)
3638 return dai->driver->ops->mute_stream(dai, mute, direction);
3639 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3640 dai->driver->ops->digital_mute)
3641 return dai->driver->ops->digital_mute(dai, mute);
3645 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3647 static int snd_soc_init_multicodec(struct snd_soc_card *card,
3648 struct snd_soc_dai_link *dai_link)
3650 /* Legacy codec/codec_dai link is a single entry in multicodec */
3651 if (dai_link->codec_name || dai_link->codec_of_node ||
3652 dai_link->codec_dai_name) {
3653 dai_link->num_codecs = 1;
3655 dai_link->codecs = devm_kzalloc(card->dev,
3656 sizeof(struct snd_soc_dai_link_component),
3658 if (!dai_link->codecs)
3661 dai_link->codecs[0].name = dai_link->codec_name;
3662 dai_link->codecs[0].of_node = dai_link->codec_of_node;
3663 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
3666 if (!dai_link->codecs) {
3667 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
3675 * snd_soc_register_card - Register a card with the ASoC core
3677 * @card: Card to register
3680 int snd_soc_register_card(struct snd_soc_card *card)
3684 if (!card->name || !card->dev)
3687 for (i = 0; i < card->num_links; i++) {
3688 struct snd_soc_dai_link *link = &card->dai_link[i];
3690 ret = snd_soc_init_multicodec(card, link);
3692 dev_err(card->dev, "ASoC: failed to init multicodec\n");
3696 for (j = 0; j < link->num_codecs; j++) {
3698 * Codec must be specified by 1 of name or OF node,
3699 * not both or neither.
3701 if (!!link->codecs[j].name ==
3702 !!link->codecs[j].of_node) {
3703 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
3707 /* Codec DAI name must be specified */
3708 if (!link->codecs[j].dai_name) {
3709 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
3716 * Platform may be specified by either name or OF node, but
3717 * can be left unspecified, and a dummy platform will be used.
3719 if (link->platform_name && link->platform_of_node) {
3721 "ASoC: Both platform name/of_node are set for %s\n",
3727 * CPU device may be specified by either name or OF node, but
3728 * can be left unspecified, and will be matched based on DAI
3731 if (link->cpu_name && link->cpu_of_node) {
3733 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3738 * At least one of CPU DAI name or CPU device name/node must be
3741 if (!link->cpu_dai_name &&
3742 !(link->cpu_name || link->cpu_of_node)) {
3744 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3750 dev_set_drvdata(card->dev, card);
3752 snd_soc_initialize_card_lists(card);
3754 soc_init_card_debugfs(card);
3756 card->rtd = devm_kzalloc(card->dev,
3757 sizeof(struct snd_soc_pcm_runtime) *
3758 (card->num_links + card->num_aux_devs),
3760 if (card->rtd == NULL)
3763 card->rtd_aux = &card->rtd[card->num_links];
3765 for (i = 0; i < card->num_links; i++) {
3766 card->rtd[i].card = card;
3767 card->rtd[i].dai_link = &card->dai_link[i];
3768 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
3769 sizeof(struct snd_soc_dai *) *
3770 (card->rtd[i].dai_link->num_codecs),
3772 if (card->rtd[i].codec_dais == NULL)
3776 for (i = 0; i < card->num_aux_devs; i++)
3777 card->rtd_aux[i].card = card;
3779 INIT_LIST_HEAD(&card->dapm_dirty);
3780 card->instantiated = 0;
3781 mutex_init(&card->mutex);
3782 mutex_init(&card->dapm_mutex);
3784 ret = snd_soc_instantiate_card(card);
3786 soc_cleanup_card_debugfs(card);
3788 /* deactivate pins to sleep state */
3789 for (i = 0; i < card->num_rtd; i++) {
3790 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
3791 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3794 for (j = 0; j < rtd->num_codecs; j++) {
3795 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
3796 if (!codec_dai->active)
3797 pinctrl_pm_select_sleep_state(codec_dai->dev);
3800 if (!cpu_dai->active)
3801 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3806 EXPORT_SYMBOL_GPL(snd_soc_register_card);
3809 * snd_soc_unregister_card - Unregister a card with the ASoC core
3811 * @card: Card to unregister
3814 int snd_soc_unregister_card(struct snd_soc_card *card)
3816 if (card->instantiated) {
3817 card->instantiated = false;
3818 snd_soc_dapm_shutdown(card);
3819 soc_cleanup_card_resources(card);
3821 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
3825 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3828 * Simplify DAI link configuration by removing ".-1" from device names
3829 * and sanitizing names.
3831 static char *fmt_single_name(struct device *dev, int *id)
3833 char *found, name[NAME_SIZE];
3836 if (dev_name(dev) == NULL)
3839 strlcpy(name, dev_name(dev), NAME_SIZE);
3841 /* are we a "%s.%d" name (platform and SPI components) */
3842 found = strstr(name, dev->driver->name);
3845 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3847 /* discard ID from name if ID == -1 */
3849 found[strlen(dev->driver->name)] = '\0';
3853 /* I2C component devices are named "bus-addr" */
3854 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3855 char tmp[NAME_SIZE];
3857 /* create unique ID number from I2C addr and bus */
3858 *id = ((id1 & 0xffff) << 16) + id2;
3860 /* sanitize component name for DAI link creation */
3861 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3862 strlcpy(name, tmp, NAME_SIZE);
3867 return kstrdup(name, GFP_KERNEL);
3871 * Simplify DAI link naming for single devices with multiple DAIs by removing
3872 * any ".-1" and using the DAI name (instead of device name).
3874 static inline char *fmt_multiple_name(struct device *dev,
3875 struct snd_soc_dai_driver *dai_drv)
3877 if (dai_drv->name == NULL) {
3879 "ASoC: error - multiple DAI %s registered with no name\n",
3884 return kstrdup(dai_drv->name, GFP_KERNEL);
3888 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
3890 * @component: The component for which the DAIs should be unregistered
3892 static void snd_soc_unregister_dais(struct snd_soc_component *component)
3894 struct snd_soc_dai *dai, *_dai;
3896 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
3897 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3899 list_del(&dai->list);
3906 * snd_soc_register_dais - Register a DAI with the ASoC core
3908 * @component: The component the DAIs are registered for
3909 * @dai_drv: DAI driver to use for the DAIs
3910 * @count: Number of DAIs
3911 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3914 static int snd_soc_register_dais(struct snd_soc_component *component,
3915 struct snd_soc_dai_driver *dai_drv, size_t count,
3916 bool legacy_dai_naming)
3918 struct device *dev = component->dev;
3919 struct snd_soc_dai *dai;
3923 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
3925 component->dai_drv = dai_drv;
3926 component->num_dai = count;
3928 for (i = 0; i < count; i++) {
3930 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3937 * Back in the old days when we still had component-less DAIs,
3938 * instead of having a static name, component-less DAIs would
3939 * inherit the name of the parent device so it is possible to
3940 * register multiple instances of the DAI. We still need to keep
3941 * the same naming style even though those DAIs are not
3942 * component-less anymore.
3944 if (count == 1 && legacy_dai_naming) {
3945 dai->name = fmt_single_name(dev, &dai->id);
3947 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3949 dai->id = dai_drv[i].id;
3953 if (dai->name == NULL) {
3959 dai->component = component;
3961 dai->driver = &dai_drv[i];
3962 if (!dai->driver->ops)
3963 dai->driver->ops = &null_dai_ops;
3965 list_add(&dai->list, &component->dai_list);
3967 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3973 snd_soc_unregister_dais(component);
3978 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3979 enum snd_soc_dapm_type type, int subseq)
3981 struct snd_soc_component *component = dapm->component;
3983 component->driver->seq_notifier(component, type, subseq);
3986 static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3989 struct snd_soc_component *component = dapm->component;
3991 return component->driver->stream_event(component, event);
3994 static int snd_soc_component_initialize(struct snd_soc_component *component,
3995 const struct snd_soc_component_driver *driver, struct device *dev)
3997 struct snd_soc_dapm_context *dapm;
3999 component->name = fmt_single_name(dev, &component->id);
4000 if (!component->name) {
4001 dev_err(dev, "ASoC: Failed to allocate name\n");
4005 component->dev = dev;
4006 component->driver = driver;
4007 component->probe = component->driver->probe;
4008 component->remove = component->driver->remove;
4010 if (!component->dapm_ptr)
4011 component->dapm_ptr = &component->dapm;
4013 dapm = component->dapm_ptr;
4015 dapm->component = component;
4016 dapm->bias_level = SND_SOC_BIAS_OFF;
4017 dapm->idle_bias_off = true;
4018 if (driver->seq_notifier)
4019 dapm->seq_notifier = snd_soc_component_seq_notifier;
4020 if (driver->stream_event)
4021 dapm->stream_event = snd_soc_component_stream_event;
4023 component->controls = driver->controls;
4024 component->num_controls = driver->num_controls;
4025 component->dapm_widgets = driver->dapm_widgets;
4026 component->num_dapm_widgets = driver->num_dapm_widgets;
4027 component->dapm_routes = driver->dapm_routes;
4028 component->num_dapm_routes = driver->num_dapm_routes;
4030 INIT_LIST_HEAD(&component->dai_list);
4031 mutex_init(&component->io_mutex);
4036 static void snd_soc_component_init_regmap(struct snd_soc_component *component)
4038 if (!component->regmap)
4039 component->regmap = dev_get_regmap(component->dev, NULL);
4040 if (component->regmap) {
4041 int val_bytes = regmap_get_val_bytes(component->regmap);
4042 /* Errors are legitimate for non-integer byte multiples */
4044 component->val_bytes = val_bytes;
4048 static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
4050 if (!component->write && !component->read)
4051 snd_soc_component_init_regmap(component);
4053 list_add(&component->list, &component_list);
4056 static void snd_soc_component_add(struct snd_soc_component *component)
4058 mutex_lock(&client_mutex);
4059 snd_soc_component_add_unlocked(component);
4060 mutex_unlock(&client_mutex);
4063 static void snd_soc_component_cleanup(struct snd_soc_component *component)
4065 snd_soc_unregister_dais(component);
4066 kfree(component->name);
4069 static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
4071 list_del(&component->list);
4074 static void snd_soc_component_del(struct snd_soc_component *component)
4076 mutex_lock(&client_mutex);
4077 snd_soc_component_del_unlocked(component);
4078 mutex_unlock(&client_mutex);
4081 int snd_soc_register_component(struct device *dev,
4082 const struct snd_soc_component_driver *cmpnt_drv,
4083 struct snd_soc_dai_driver *dai_drv,
4086 struct snd_soc_component *cmpnt;
4089 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
4091 dev_err(dev, "ASoC: Failed to allocate memory\n");
4095 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
4099 cmpnt->ignore_pmdown_time = true;
4100 cmpnt->registered_as_component = true;
4102 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
4104 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4108 snd_soc_component_add(cmpnt);
4113 snd_soc_component_cleanup(cmpnt);
4118 EXPORT_SYMBOL_GPL(snd_soc_register_component);
4121 * snd_soc_unregister_component - Unregister a component from the ASoC core
4124 void snd_soc_unregister_component(struct device *dev)
4126 struct snd_soc_component *cmpnt;
4128 list_for_each_entry(cmpnt, &component_list, list) {
4129 if (dev == cmpnt->dev && cmpnt->registered_as_component)
4135 snd_soc_component_del(cmpnt);
4136 snd_soc_component_cleanup(cmpnt);
4139 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4141 static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
4143 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4145 return platform->driver->probe(platform);
4148 static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
4150 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4152 platform->driver->remove(platform);
4156 * snd_soc_add_platform - Add a platform to the ASoC core
4157 * @dev: The parent device for the platform
4158 * @platform: The platform to add
4159 * @platform_driver: The driver for the platform
4161 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
4162 const struct snd_soc_platform_driver *platform_drv)
4166 ret = snd_soc_component_initialize(&platform->component,
4167 &platform_drv->component_driver, dev);
4171 platform->dev = dev;
4172 platform->driver = platform_drv;
4173 if (platform_drv->controls) {
4174 platform->component.controls = platform_drv->controls;
4175 platform->component.num_controls = platform_drv->num_controls;
4177 if (platform_drv->dapm_widgets) {
4178 platform->component.dapm_widgets = platform_drv->dapm_widgets;
4179 platform->component.num_dapm_widgets = platform_drv->num_dapm_widgets;
4180 platform->component.steal_sibling_dai_widgets = true;
4182 if (platform_drv->dapm_routes) {
4183 platform->component.dapm_routes = platform_drv->dapm_routes;
4184 platform->component.num_dapm_routes = platform_drv->num_dapm_routes;
4187 if (platform_drv->probe)
4188 platform->component.probe = snd_soc_platform_drv_probe;
4189 if (platform_drv->remove)
4190 platform->component.remove = snd_soc_platform_drv_remove;
4192 #ifdef CONFIG_DEBUG_FS
4193 platform->component.debugfs_prefix = "platform";
4196 mutex_lock(&client_mutex);
4197 snd_soc_component_add_unlocked(&platform->component);
4198 list_add(&platform->list, &platform_list);
4199 mutex_unlock(&client_mutex);
4201 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
4202 platform->component.name);
4206 EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4209 * snd_soc_register_platform - Register a platform with the ASoC core
4211 * @platform: platform to register
4213 int snd_soc_register_platform(struct device *dev,
4214 const struct snd_soc_platform_driver *platform_drv)
4216 struct snd_soc_platform *platform;
4219 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4221 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4222 if (platform == NULL)
4225 ret = snd_soc_add_platform(dev, platform, platform_drv);
4231 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4234 * snd_soc_remove_platform - Remove a platform from the ASoC core
4235 * @platform: the platform to remove
4237 void snd_soc_remove_platform(struct snd_soc_platform *platform)
4240 mutex_lock(&client_mutex);
4241 list_del(&platform->list);
4242 snd_soc_component_del_unlocked(&platform->component);
4243 mutex_unlock(&client_mutex);
4245 snd_soc_component_cleanup(&platform->component);
4247 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
4248 platform->component.name);
4250 EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4252 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4254 struct snd_soc_platform *platform;
4256 list_for_each_entry(platform, &platform_list, list) {
4257 if (dev == platform->dev)
4263 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4266 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4268 * @platform: platform to unregister
4270 void snd_soc_unregister_platform(struct device *dev)
4272 struct snd_soc_platform *platform;
4274 platform = snd_soc_lookup_platform(dev);
4278 snd_soc_remove_platform(platform);
4281 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4283 static u64 codec_format_map[] = {
4284 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4285 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4286 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4287 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4288 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4289 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4290 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4291 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4292 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4293 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4294 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4295 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4296 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4297 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4298 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4299 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4302 /* Fix up the DAI formats for endianness: codecs don't actually see
4303 * the endianness of the data but we're using the CPU format
4304 * definitions which do need to include endianness so we ensure that
4305 * codec DAIs always have both big and little endian variants set.
4307 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4311 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4312 if (stream->formats & codec_format_map[i])
4313 stream->formats |= codec_format_map[i];
4316 static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
4318 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4320 return codec->driver->probe(codec);
4323 static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
4325 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4327 codec->driver->remove(codec);
4330 static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4331 unsigned int reg, unsigned int val)
4333 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4335 return codec->driver->write(codec, reg, val);
4338 static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4339 unsigned int reg, unsigned int *val)
4341 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4343 *val = codec->driver->read(codec, reg);
4348 static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
4349 enum snd_soc_bias_level level)
4351 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
4353 return codec->driver->set_bias_level(codec, level);
4357 * snd_soc_register_codec - Register a codec with the ASoC core
4359 * @codec: codec to register
4361 int snd_soc_register_codec(struct device *dev,
4362 const struct snd_soc_codec_driver *codec_drv,
4363 struct snd_soc_dai_driver *dai_drv,
4366 struct snd_soc_codec *codec;
4367 struct snd_soc_dai *dai;
4370 dev_dbg(dev, "codec register %s\n", dev_name(dev));
4372 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4376 codec->component.dapm_ptr = &codec->dapm;
4377 codec->component.codec = codec;
4379 ret = snd_soc_component_initialize(&codec->component,
4380 &codec_drv->component_driver, dev);
4384 if (codec_drv->controls) {
4385 codec->component.controls = codec_drv->controls;
4386 codec->component.num_controls = codec_drv->num_controls;
4388 if (codec_drv->dapm_widgets) {
4389 codec->component.dapm_widgets = codec_drv->dapm_widgets;
4390 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
4392 if (codec_drv->dapm_routes) {
4393 codec->component.dapm_routes = codec_drv->dapm_routes;
4394 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
4397 if (codec_drv->probe)
4398 codec->component.probe = snd_soc_codec_drv_probe;
4399 if (codec_drv->remove)
4400 codec->component.remove = snd_soc_codec_drv_remove;
4401 if (codec_drv->write)
4402 codec->component.write = snd_soc_codec_drv_write;
4403 if (codec_drv->read)
4404 codec->component.read = snd_soc_codec_drv_read;
4405 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
4406 codec->dapm.codec = codec;
4407 codec->dapm.idle_bias_off = codec_drv->idle_bias_off;
4408 codec->dapm.suspend_bias_off = codec_drv->suspend_bias_off;
4409 if (codec_drv->seq_notifier)
4410 codec->dapm.seq_notifier = codec_drv->seq_notifier;
4411 if (codec_drv->set_bias_level)
4412 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
4414 codec->driver = codec_drv;
4415 codec->component.val_bytes = codec_drv->reg_word_size;
4416 mutex_init(&codec->mutex);
4418 #ifdef CONFIG_DEBUG_FS
4419 codec->component.init_debugfs = soc_init_codec_debugfs;
4420 codec->component.debugfs_prefix = "codec";
4423 if (codec_drv->get_regmap)
4424 codec->component.regmap = codec_drv->get_regmap(dev);
4426 for (i = 0; i < num_dai; i++) {
4427 fixup_codec_formats(&dai_drv[i].playback);
4428 fixup_codec_formats(&dai_drv[i].capture);
4431 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4433 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4437 list_for_each_entry(dai, &codec->component.dai_list, list)
4440 mutex_lock(&client_mutex);
4441 snd_soc_component_add_unlocked(&codec->component);
4442 list_add(&codec->list, &codec_list);
4443 mutex_unlock(&client_mutex);
4445 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4446 codec->component.name);
4450 snd_soc_component_cleanup(&codec->component);
4455 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4458 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4460 * @codec: codec to unregister
4462 void snd_soc_unregister_codec(struct device *dev)
4464 struct snd_soc_codec *codec;
4466 list_for_each_entry(codec, &codec_list, list) {
4467 if (dev == codec->dev)
4474 mutex_lock(&client_mutex);
4475 list_del(&codec->list);
4476 snd_soc_component_del_unlocked(&codec->component);
4477 mutex_unlock(&client_mutex);
4479 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4480 codec->component.name);
4482 snd_soc_component_cleanup(&codec->component);
4483 snd_soc_cache_exit(codec);
4486 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4488 /* Retrieve a card's name from device tree */
4489 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4490 const char *propname)
4492 struct device_node *np;
4496 pr_err("card->dev is not set before calling %s\n", __func__);
4500 np = card->dev->of_node;
4502 ret = of_property_read_string_index(np, propname, 0, &card->name);
4504 * EINVAL means the property does not exist. This is fine providing
4505 * card->name was previously set, which is checked later in
4506 * snd_soc_register_card.
4508 if (ret < 0 && ret != -EINVAL) {
4510 "ASoC: Property '%s' could not be read: %d\n",
4517 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4519 static const struct snd_soc_dapm_widget simple_widgets[] = {
4520 SND_SOC_DAPM_MIC("Microphone", NULL),
4521 SND_SOC_DAPM_LINE("Line", NULL),
4522 SND_SOC_DAPM_HP("Headphone", NULL),
4523 SND_SOC_DAPM_SPK("Speaker", NULL),
4526 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4527 const char *propname)
4529 struct device_node *np = card->dev->of_node;
4530 struct snd_soc_dapm_widget *widgets;
4531 const char *template, *wname;
4532 int i, j, num_widgets, ret;
4534 num_widgets = of_property_count_strings(np, propname);
4535 if (num_widgets < 0) {
4537 "ASoC: Property '%s' does not exist\n", propname);
4540 if (num_widgets & 1) {
4542 "ASoC: Property '%s' length is not even\n", propname);
4548 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4553 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4557 "ASoC: Could not allocate memory for widgets\n");
4561 for (i = 0; i < num_widgets; i++) {
4562 ret = of_property_read_string_index(np, propname,
4566 "ASoC: Property '%s' index %d read error:%d\n",
4567 propname, 2 * i, ret);
4571 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4572 if (!strncmp(template, simple_widgets[j].name,
4573 strlen(simple_widgets[j].name))) {
4574 widgets[i] = simple_widgets[j];
4579 if (j >= ARRAY_SIZE(simple_widgets)) {
4581 "ASoC: DAPM widget '%s' is not supported\n",
4586 ret = of_property_read_string_index(np, propname,
4591 "ASoC: Property '%s' index %d read error:%d\n",
4592 propname, (2 * i) + 1, ret);
4596 widgets[i].name = wname;
4599 card->dapm_widgets = widgets;
4600 card->num_dapm_widgets = num_widgets;
4604 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4606 int snd_soc_of_parse_tdm_slot(struct device_node *np,
4607 unsigned int *slots,
4608 unsigned int *slot_width)
4613 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4614 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4622 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4623 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4633 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4635 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4636 const char *propname)
4638 struct device_node *np = card->dev->of_node;
4640 struct snd_soc_dapm_route *routes;
4643 num_routes = of_property_count_strings(np, propname);
4644 if (num_routes < 0 || num_routes & 1) {
4646 "ASoC: Property '%s' does not exist or its length is not even\n",
4652 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4657 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4661 "ASoC: Could not allocate DAPM route table\n");
4665 for (i = 0; i < num_routes; i++) {
4666 ret = of_property_read_string_index(np, propname,
4667 2 * i, &routes[i].sink);
4670 "ASoC: Property '%s' index %d could not be read: %d\n",
4671 propname, 2 * i, ret);
4674 ret = of_property_read_string_index(np, propname,
4675 (2 * i) + 1, &routes[i].source);
4678 "ASoC: Property '%s' index %d could not be read: %d\n",
4679 propname, (2 * i) + 1, ret);
4684 card->num_dapm_routes = num_routes;
4685 card->dapm_routes = routes;
4689 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4691 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4693 struct device_node **bitclkmaster,
4694 struct device_node **framemaster)
4698 unsigned int format = 0;
4704 } of_fmt_table[] = {
4705 { "i2s", SND_SOC_DAIFMT_I2S },
4706 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4707 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4708 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4709 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4710 { "ac97", SND_SOC_DAIFMT_AC97 },
4711 { "pdm", SND_SOC_DAIFMT_PDM},
4712 { "msb", SND_SOC_DAIFMT_MSB },
4713 { "lsb", SND_SOC_DAIFMT_LSB },
4720 * check "[prefix]format = xxx"
4721 * SND_SOC_DAIFMT_FORMAT_MASK area
4723 snprintf(prop, sizeof(prop), "%sformat", prefix);
4724 ret = of_property_read_string(np, prop, &str);
4726 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4727 if (strcmp(str, of_fmt_table[i].name) == 0) {
4728 format |= of_fmt_table[i].val;
4735 * check "[prefix]continuous-clock"
4736 * SND_SOC_DAIFMT_CLOCK_MASK area
4738 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4739 if (of_get_property(np, prop, NULL))
4740 format |= SND_SOC_DAIFMT_CONT;
4742 format |= SND_SOC_DAIFMT_GATED;
4745 * check "[prefix]bitclock-inversion"
4746 * check "[prefix]frame-inversion"
4747 * SND_SOC_DAIFMT_INV_MASK area
4749 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4750 bit = !!of_get_property(np, prop, NULL);
4752 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4753 frame = !!of_get_property(np, prop, NULL);
4755 switch ((bit << 4) + frame) {
4757 format |= SND_SOC_DAIFMT_IB_IF;
4760 format |= SND_SOC_DAIFMT_IB_NF;
4763 format |= SND_SOC_DAIFMT_NB_IF;
4766 /* SND_SOC_DAIFMT_NB_NF is default */
4771 * check "[prefix]bitclock-master"
4772 * check "[prefix]frame-master"
4773 * SND_SOC_DAIFMT_MASTER_MASK area
4775 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4776 bit = !!of_get_property(np, prop, NULL);
4777 if (bit && bitclkmaster)
4778 *bitclkmaster = of_parse_phandle(np, prop, 0);
4780 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4781 frame = !!of_get_property(np, prop, NULL);
4782 if (frame && framemaster)
4783 *framemaster = of_parse_phandle(np, prop, 0);
4785 switch ((bit << 4) + frame) {
4787 format |= SND_SOC_DAIFMT_CBM_CFM;
4790 format |= SND_SOC_DAIFMT_CBM_CFS;
4793 format |= SND_SOC_DAIFMT_CBS_CFM;
4796 format |= SND_SOC_DAIFMT_CBS_CFS;
4802 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4804 int snd_soc_of_get_dai_name(struct device_node *of_node,
4805 const char **dai_name)
4807 struct snd_soc_component *pos;
4808 struct of_phandle_args args;
4811 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4812 "#sound-dai-cells", 0, &args);
4816 ret = -EPROBE_DEFER;
4818 mutex_lock(&client_mutex);
4819 list_for_each_entry(pos, &component_list, list) {
4820 if (pos->dev->of_node != args.np)
4823 if (pos->driver->of_xlate_dai_name) {
4824 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4828 switch (args.args_count) {
4830 id = 0; /* same as dai_drv[0] */
4840 if (id < 0 || id >= pos->num_dai) {
4847 *dai_name = pos->dai_drv[id].name;
4849 *dai_name = pos->name;
4854 mutex_unlock(&client_mutex);
4856 of_node_put(args.np);
4860 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4862 static int __init snd_soc_init(void)
4864 #ifdef CONFIG_DEBUG_FS
4865 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4866 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
4867 pr_warn("ASoC: Failed to create debugfs directory\n");
4868 snd_soc_debugfs_root = NULL;
4871 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4873 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4875 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4877 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
4879 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
4880 &platform_list_fops))
4881 pr_warn("ASoC: Failed to create platform list debugfs file\n");
4884 snd_soc_util_init();
4886 return platform_driver_register(&soc_driver);
4888 module_init(snd_soc_init);
4890 static void __exit snd_soc_exit(void)
4892 snd_soc_util_exit();
4894 #ifdef CONFIG_DEBUG_FS
4895 debugfs_remove_recursive(snd_soc_debugfs_root);
4897 platform_driver_unregister(&soc_driver);
4899 module_exit(snd_soc_exit);
4901 /* Module information */
4902 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4903 MODULE_DESCRIPTION("ALSA SoC Core");
4904 MODULE_LICENSE("GPL");
4905 MODULE_ALIAS("platform:soc-audio");