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 && codec->driver->suspend) {
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");
654 case SND_SOC_BIAS_OFF:
655 codec->driver->suspend(codec);
656 codec->suspended = 1;
657 codec->cache_sync = 1;
658 if (codec->component.regmap)
659 regcache_mark_dirty(codec->component.regmap);
660 /* deactivate pins to sleep state */
661 pinctrl_pm_select_sleep_state(codec->dev);
665 "ASoC: CODEC is on over suspend\n");
671 for (i = 0; i < card->num_rtd; i++) {
672 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
674 if (card->rtd[i].dai_link->ignore_suspend)
677 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
678 cpu_dai->driver->suspend(cpu_dai);
680 /* deactivate pins to sleep state */
681 pinctrl_pm_select_sleep_state(cpu_dai->dev);
684 if (card->suspend_post)
685 card->suspend_post(card);
689 EXPORT_SYMBOL_GPL(snd_soc_suspend);
691 /* deferred resume work, so resume can complete before we finished
692 * setting our codec back up, which can be very slow on I2C
694 static void soc_resume_deferred(struct work_struct *work)
696 struct snd_soc_card *card =
697 container_of(work, struct snd_soc_card, deferred_resume_work);
698 struct snd_soc_codec *codec;
701 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
702 * so userspace apps are blocked from touching us
705 dev_dbg(card->dev, "ASoC: starting resume work\n");
707 /* Bring us up into D2 so that DAPM starts enabling things */
708 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
710 if (card->resume_pre)
711 card->resume_pre(card);
713 /* resume AC97 DAIs */
714 for (i = 0; i < card->num_rtd; i++) {
715 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
717 if (card->rtd[i].dai_link->ignore_suspend)
720 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
721 cpu_dai->driver->resume(cpu_dai);
724 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
725 /* If the CODEC was idle over suspend then it will have been
726 * left with bias OFF or STANDBY and suspended so we must now
727 * resume. Otherwise the suspend was suppressed.
729 if (codec->driver->resume && codec->suspended) {
730 switch (codec->dapm.bias_level) {
731 case SND_SOC_BIAS_STANDBY:
732 case SND_SOC_BIAS_OFF:
733 codec->driver->resume(codec);
734 codec->suspended = 0;
738 "ASoC: CODEC was on over suspend\n");
744 for (i = 0; i < card->num_rtd; i++) {
746 if (card->rtd[i].dai_link->ignore_suspend)
749 snd_soc_dapm_stream_event(&card->rtd[i],
750 SNDRV_PCM_STREAM_PLAYBACK,
751 SND_SOC_DAPM_STREAM_RESUME);
753 snd_soc_dapm_stream_event(&card->rtd[i],
754 SNDRV_PCM_STREAM_CAPTURE,
755 SND_SOC_DAPM_STREAM_RESUME);
758 /* unmute any active DACs */
759 for (i = 0; i < card->num_rtd; i++) {
761 if (card->rtd[i].dai_link->ignore_suspend)
764 for (j = 0; j < card->rtd[i].num_codecs; j++) {
765 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
766 struct snd_soc_dai_driver *drv = dai->driver;
768 if (drv->ops->digital_mute && dai->playback_active)
769 drv->ops->digital_mute(dai, 0);
773 for (i = 0; i < card->num_rtd; i++) {
774 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
775 struct snd_soc_platform *platform = card->rtd[i].platform;
777 if (card->rtd[i].dai_link->ignore_suspend)
780 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
781 cpu_dai->driver->resume(cpu_dai);
782 if (platform->driver->resume && platform->suspended) {
783 platform->driver->resume(cpu_dai);
784 platform->suspended = 0;
788 if (card->resume_post)
789 card->resume_post(card);
791 dev_dbg(card->dev, "ASoC: resume work completed\n");
793 /* userspace can access us now we are back as we were before */
794 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
796 /* Recheck all analogue paths too */
797 dapm_mark_io_dirty(&card->dapm);
798 snd_soc_dapm_sync(&card->dapm);
801 /* powers up audio subsystem after a suspend */
802 int snd_soc_resume(struct device *dev)
804 struct snd_soc_card *card = dev_get_drvdata(dev);
805 int i, ac97_control = 0;
807 /* If the card is not initialized yet there is nothing to do */
808 if (!card->instantiated)
811 /* activate pins from sleep state */
812 for (i = 0; i < card->num_rtd; i++) {
813 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
814 struct snd_soc_dai **codec_dais = rtd->codec_dais;
815 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
819 pinctrl_pm_select_default_state(cpu_dai->dev);
821 for (j = 0; j < rtd->num_codecs; j++) {
822 struct snd_soc_dai *codec_dai = codec_dais[j];
823 if (codec_dai->active)
824 pinctrl_pm_select_default_state(codec_dai->dev);
828 /* AC97 devices might have other drivers hanging off them so
829 * need to resume immediately. Other drivers don't have that
830 * problem and may take a substantial amount of time to resume
831 * due to I/O costs and anti-pop so handle them out of line.
833 for (i = 0; i < card->num_rtd; i++) {
834 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
835 ac97_control |= cpu_dai->driver->ac97_control;
838 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
839 soc_resume_deferred(&card->deferred_resume_work);
841 dev_dbg(dev, "ASoC: Scheduling resume work\n");
842 if (!schedule_work(&card->deferred_resume_work))
843 dev_err(dev, "ASoC: resume work item may be lost\n");
848 EXPORT_SYMBOL_GPL(snd_soc_resume);
850 #define snd_soc_suspend NULL
851 #define snd_soc_resume NULL
854 static const struct snd_soc_dai_ops null_dai_ops = {
857 static struct snd_soc_component *soc_find_component(
858 const struct device_node *of_node, const char *name)
860 struct snd_soc_component *component;
862 list_for_each_entry(component, &component_list, list) {
864 if (component->dev->of_node == of_node)
866 } else if (strcmp(component->name, name) == 0) {
874 static struct snd_soc_dai *snd_soc_find_dai(
875 const struct snd_soc_dai_link_component *dlc)
877 struct snd_soc_component *component;
878 struct snd_soc_dai *dai;
880 /* Find CPU DAI from registered DAIs*/
881 list_for_each_entry(component, &component_list, list) {
882 if (dlc->of_node && component->dev->of_node != dlc->of_node)
884 if (dlc->name && strcmp(dev_name(component->dev), dlc->name))
886 list_for_each_entry(dai, &component->dai_list, list) {
887 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
897 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
899 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
900 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
901 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
902 struct snd_soc_dai_link_component cpu_dai_component;
903 struct snd_soc_dai **codec_dais = rtd->codec_dais;
904 struct snd_soc_platform *platform;
905 const char *platform_name;
908 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
910 cpu_dai_component.name = dai_link->cpu_name;
911 cpu_dai_component.of_node = dai_link->cpu_of_node;
912 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
913 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
915 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
916 dai_link->cpu_dai_name);
917 return -EPROBE_DEFER;
920 rtd->num_codecs = dai_link->num_codecs;
922 /* Find CODEC from registered CODECs */
923 for (i = 0; i < rtd->num_codecs; i++) {
924 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
925 if (!codec_dais[i]) {
926 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
928 return -EPROBE_DEFER;
932 /* Single codec links expect codec and codec_dai in runtime data */
933 rtd->codec_dai = codec_dais[0];
934 rtd->codec = rtd->codec_dai->codec;
936 /* if there's no platform we match on the empty platform */
937 platform_name = dai_link->platform_name;
938 if (!platform_name && !dai_link->platform_of_node)
939 platform_name = "snd-soc-dummy";
941 /* find one from the set of registered platforms */
942 list_for_each_entry(platform, &platform_list, list) {
943 if (dai_link->platform_of_node) {
944 if (platform->dev->of_node !=
945 dai_link->platform_of_node)
948 if (strcmp(platform->component.name, platform_name))
952 rtd->platform = platform;
954 if (!rtd->platform) {
955 dev_err(card->dev, "ASoC: platform %s not registered\n",
956 dai_link->platform_name);
957 return -EPROBE_DEFER;
965 static void soc_remove_component(struct snd_soc_component *component)
967 if (!component->probed)
970 /* This is a HACK and will be removed soon */
971 if (component->codec)
972 list_del(&component->codec->card_list);
974 if (component->remove)
975 component->remove(component);
977 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
979 soc_cleanup_component_debugfs(component);
980 component->probed = 0;
981 module_put(component->dev->driver->owner);
984 static void soc_remove_dai(struct snd_soc_dai *dai, int order)
988 if (dai && dai->probed &&
989 dai->driver->remove_order == order) {
990 if (dai->driver->remove) {
991 err = dai->driver->remove(dai);
994 "ASoC: failed to remove %s: %d\n",
1001 static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
1003 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1006 /* unregister the rtd device */
1007 if (rtd->dev_registered) {
1008 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1009 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1010 device_unregister(rtd->dev);
1011 rtd->dev_registered = 0;
1014 /* remove the CODEC DAI */
1015 for (i = 0; i < rtd->num_codecs; i++)
1016 soc_remove_dai(rtd->codec_dais[i], order);
1018 soc_remove_dai(rtd->cpu_dai, order);
1021 static void soc_remove_link_components(struct snd_soc_card *card, int num,
1024 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1025 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1026 struct snd_soc_platform *platform = rtd->platform;
1027 struct snd_soc_component *component;
1030 /* remove the platform */
1031 if (platform && platform->component.driver->remove_order == order)
1032 soc_remove_component(&platform->component);
1034 /* remove the CODEC-side CODEC */
1035 for (i = 0; i < rtd->num_codecs; i++) {
1036 component = rtd->codec_dais[i]->component;
1037 if (component->driver->remove_order == order)
1038 soc_remove_component(component);
1041 /* remove any CPU-side CODEC */
1043 if (cpu_dai->component->driver->remove_order == order)
1044 soc_remove_component(cpu_dai->component);
1048 static void soc_remove_dai_links(struct snd_soc_card *card)
1052 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1054 for (dai = 0; dai < card->num_rtd; dai++)
1055 soc_remove_link_dais(card, dai, order);
1058 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1060 for (dai = 0; dai < card->num_rtd; dai++)
1061 soc_remove_link_components(card, dai, order);
1067 static void soc_set_name_prefix(struct snd_soc_card *card,
1068 struct snd_soc_component *component)
1072 if (card->codec_conf == NULL)
1075 for (i = 0; i < card->num_configs; i++) {
1076 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1077 if (map->of_node && component->dev->of_node != map->of_node)
1079 if (map->dev_name && strcmp(component->name, map->dev_name))
1081 component->name_prefix = map->name_prefix;
1086 static int soc_probe_component(struct snd_soc_card *card,
1087 struct snd_soc_component *component)
1089 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1090 struct snd_soc_component *dai_component, *component2;
1091 struct snd_soc_dai *dai;
1094 if (component->probed)
1097 component->card = card;
1099 soc_set_name_prefix(card, component);
1101 if (!try_module_get(component->dev->driver->owner))
1104 soc_init_component_debugfs(component);
1106 if (component->dapm_widgets) {
1107 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1108 component->num_dapm_widgets);
1111 dev_err(component->dev,
1112 "Failed to create new controls %d\n", ret);
1118 * This is rather ugly, but certain platforms expect that the DAPM
1119 * widgets for the DAIs for components with the same parent device are
1120 * created in the platforms DAPM context. Until that is fixed we need to
1123 if (component->steal_sibling_dai_widgets) {
1124 dai_component = NULL;
1125 list_for_each_entry(component2, &component_list, list) {
1126 if (component == component2)
1129 if (component2->dev == component->dev &&
1130 !list_empty(&component2->dai_list)) {
1131 dai_component = component2;
1136 dai_component = component;
1137 list_for_each_entry(component2, &component_list, list) {
1138 if (component2->dev == component->dev &&
1139 component2->steal_sibling_dai_widgets) {
1140 dai_component = NULL;
1146 if (dai_component) {
1147 list_for_each_entry(dai, &dai_component->dai_list, list) {
1148 snd_soc_dapm_new_dai_widgets(dapm, dai);
1150 dev_err(component->dev,
1151 "Failed to create DAI widgets %d\n",
1158 if (component->probe) {
1159 ret = component->probe(component);
1161 dev_err(component->dev,
1162 "ASoC: failed to probe component %d\n", ret);
1166 WARN(dapm->idle_bias_off &&
1167 dapm->bias_level != SND_SOC_BIAS_OFF,
1168 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1172 if (component->controls)
1173 snd_soc_add_component_controls(component, component->controls,
1174 component->num_controls);
1175 if (component->dapm_routes)
1176 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1177 component->num_dapm_routes);
1179 component->probed = 1;
1180 list_add(&dapm->list, &card->dapm_list);
1182 /* This is a HACK and will be removed soon */
1183 if (component->codec)
1184 list_add(&component->codec->card_list, &card->codec_dev_list);
1189 soc_cleanup_component_debugfs(component);
1190 module_put(component->dev->driver->owner);
1195 static void rtd_release(struct device *dev)
1200 static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1205 /* register the rtd device */
1206 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1209 device_initialize(rtd->dev);
1210 rtd->dev->parent = rtd->card->dev;
1211 rtd->dev->release = rtd_release;
1212 rtd->dev->init_name = name;
1213 dev_set_drvdata(rtd->dev, rtd);
1214 mutex_init(&rtd->pcm_mutex);
1215 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1216 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1217 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1218 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1219 ret = device_add(rtd->dev);
1221 /* calling put_device() here to free the rtd->dev */
1222 put_device(rtd->dev);
1223 dev_err(rtd->card->dev,
1224 "ASoC: failed to register runtime device: %d\n", ret);
1227 rtd->dev_registered = 1;
1230 /* add DAPM sysfs entries for this codec */
1231 ret = snd_soc_dapm_sys_add(rtd->dev);
1234 "ASoC: failed to add codec dapm sysfs entries: %d\n",
1237 /* add codec sysfs entries */
1238 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1241 "ASoC: failed to add codec sysfs files: %d\n",
1248 static int soc_probe_link_components(struct snd_soc_card *card, int num,
1251 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1252 struct snd_soc_platform *platform = rtd->platform;
1253 struct snd_soc_component *component;
1256 /* probe the CPU-side component, if it is a CODEC */
1257 component = rtd->cpu_dai->component;
1258 if (component->driver->probe_order == order) {
1259 ret = soc_probe_component(card, component);
1264 /* probe the CODEC-side components */
1265 for (i = 0; i < rtd->num_codecs; i++) {
1266 component = rtd->codec_dais[i]->component;
1267 if (component->driver->probe_order == order) {
1268 ret = soc_probe_component(card, component);
1274 /* probe the platform */
1275 if (platform->component.driver->probe_order == order) {
1276 ret = soc_probe_component(card, &platform->component);
1284 static int soc_probe_codec_dai(struct snd_soc_card *card,
1285 struct snd_soc_dai *codec_dai,
1290 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1291 if (codec_dai->driver->probe) {
1292 ret = codec_dai->driver->probe(codec_dai);
1294 dev_err(codec_dai->dev,
1295 "ASoC: failed to probe CODEC DAI %s: %d\n",
1296 codec_dai->name, ret);
1301 /* mark codec_dai as probed and add to card dai list */
1302 codec_dai->probed = 1;
1308 static int soc_link_dai_widgets(struct snd_soc_card *card,
1309 struct snd_soc_dai_link *dai_link,
1310 struct snd_soc_pcm_runtime *rtd)
1312 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1313 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1314 struct snd_soc_dapm_widget *play_w, *capture_w;
1317 if (rtd->num_codecs > 1)
1318 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1320 /* link the DAI widgets */
1321 play_w = codec_dai->playback_widget;
1322 capture_w = cpu_dai->capture_widget;
1323 if (play_w && capture_w) {
1324 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1327 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1328 play_w->name, capture_w->name, ret);
1333 play_w = cpu_dai->playback_widget;
1334 capture_w = codec_dai->capture_widget;
1335 if (play_w && capture_w) {
1336 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1339 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1340 play_w->name, capture_w->name, ret);
1348 static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1350 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1351 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1352 struct snd_soc_platform *platform = rtd->platform;
1353 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1356 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1357 card->name, num, order);
1359 /* config components */
1360 cpu_dai->platform = platform;
1361 cpu_dai->card = card;
1362 for (i = 0; i < rtd->num_codecs; i++)
1363 rtd->codec_dais[i]->card = card;
1365 /* set default power off timeout */
1366 rtd->pmdown_time = pmdown_time;
1368 /* probe the cpu_dai */
1369 if (!cpu_dai->probed &&
1370 cpu_dai->driver->probe_order == order) {
1371 if (cpu_dai->driver->probe) {
1372 ret = cpu_dai->driver->probe(cpu_dai);
1374 dev_err(cpu_dai->dev,
1375 "ASoC: failed to probe CPU DAI %s: %d\n",
1376 cpu_dai->name, ret);
1380 cpu_dai->probed = 1;
1383 /* probe the CODEC DAI */
1384 for (i = 0; i < rtd->num_codecs; i++) {
1385 ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order);
1390 /* complete DAI probe during last probe */
1391 if (order != SND_SOC_COMP_ORDER_LAST)
1394 /* do machine specific initialization */
1395 if (dai_link->init) {
1396 ret = dai_link->init(rtd);
1398 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1399 dai_link->name, ret);
1404 ret = soc_post_component_init(rtd, dai_link->name);
1408 #ifdef CONFIG_DEBUG_FS
1409 /* add DPCM sysfs entries */
1410 if (dai_link->dynamic) {
1411 ret = soc_dpcm_debugfs_add(rtd);
1414 "ASoC: failed to add dpcm sysfs entries: %d\n",
1421 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1423 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1426 if (cpu_dai->driver->compress_dai) {
1427 /*create compress_device"*/
1428 ret = soc_new_compress(rtd, num);
1430 dev_err(card->dev, "ASoC: can't create compress %s\n",
1431 dai_link->stream_name);
1436 if (!dai_link->params) {
1437 /* create the pcm */
1438 ret = soc_new_pcm(rtd, num);
1440 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1441 dai_link->stream_name, ret);
1445 INIT_DELAYED_WORK(&rtd->delayed_work,
1446 codec2codec_close_delayed_work);
1448 /* link the DAI widgets */
1449 ret = soc_link_dai_widgets(card, dai_link, rtd);
1455 /* add platform data for AC97 devices */
1456 for (i = 0; i < rtd->num_codecs; i++) {
1457 if (rtd->codec_dais[i]->driver->ac97_control)
1458 snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
1459 rtd->cpu_dai->ac97_pdata);
1465 #ifdef CONFIG_SND_SOC_AC97_BUS
1466 static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1467 struct snd_soc_dai *codec_dai)
1471 /* Only instantiate AC97 if not already done by the adaptor
1472 * for the generic AC97 subsystem.
1474 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
1476 * It is possible that the AC97 device is already registered to
1477 * the device subsystem. This happens when the device is created
1478 * via snd_ac97_mixer(). Currently only SoC codec that does so
1479 * is the generic AC97 glue but others migh emerge.
1481 * In those cases we don't try to register the device again.
1483 if (!codec->ac97_created)
1486 ret = soc_ac97_dev_register(codec);
1489 "ASoC: AC97 device register failed: %d\n", ret);
1493 codec->ac97_registered = 1;
1498 static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
1500 if (codec->ac97_registered) {
1501 soc_ac97_dev_unregister(codec);
1502 codec->ac97_registered = 0;
1506 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1510 for (i = 0; i < rtd->num_codecs; i++) {
1511 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1513 ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
1516 soc_unregister_ac97_codec(codec_dai->codec);
1524 static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1528 for (i = 0; i < rtd->num_codecs; i++)
1529 soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
1533 static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1535 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1536 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1537 const char *name = aux_dev->codec_name;
1539 rtd->component = soc_find_component(aux_dev->codec_of_node, name);
1540 if (!rtd->component) {
1541 if (aux_dev->codec_of_node)
1542 name = of_node_full_name(aux_dev->codec_of_node);
1544 dev_err(card->dev, "ASoC: %s not registered\n", name);
1545 return -EPROBE_DEFER;
1549 * Some places still reference rtd->codec, so we have to keep that
1550 * initialized if the component is a CODEC. Once all those references
1551 * have been removed, this code can be removed as well.
1553 rtd->codec = rtd->component->codec;
1558 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1560 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1561 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1564 ret = soc_probe_component(card, rtd->component);
1568 /* do machine specific initialization */
1569 if (aux_dev->init) {
1570 ret = aux_dev->init(rtd->component);
1572 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1573 aux_dev->name, ret);
1578 return soc_post_component_init(rtd, aux_dev->name);
1581 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1583 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1584 struct snd_soc_component *component = rtd->component;
1586 /* unregister the rtd device */
1587 if (rtd->dev_registered) {
1588 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1589 device_unregister(rtd->dev);
1590 rtd->dev_registered = 0;
1593 if (component && component->probed)
1594 soc_remove_component(component);
1597 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1601 if (codec->cache_init)
1604 ret = snd_soc_cache_init(codec);
1607 "ASoC: Failed to set cache compression type: %d\n",
1611 codec->cache_init = 1;
1615 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1617 struct snd_soc_codec *codec;
1618 struct snd_soc_dai_link *dai_link;
1619 int ret, i, order, dai_fmt;
1621 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1624 for (i = 0; i < card->num_links; i++) {
1625 ret = soc_bind_dai_link(card, i);
1630 /* bind aux_devs too */
1631 for (i = 0; i < card->num_aux_devs; i++) {
1632 ret = soc_bind_aux_dev(card, i);
1637 /* initialize the register cache for each available codec */
1638 list_for_each_entry(codec, &codec_list, list) {
1639 if (codec->cache_init)
1641 ret = snd_soc_init_codec_cache(codec);
1646 /* card bind complete so register a sound card */
1647 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1648 card->owner, 0, &card->snd_card);
1651 "ASoC: can't create sound card for card %s: %d\n",
1656 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1657 card->dapm.dev = card->dev;
1658 card->dapm.card = card;
1659 list_add(&card->dapm.list, &card->dapm_list);
1661 #ifdef CONFIG_DEBUG_FS
1662 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1665 #ifdef CONFIG_PM_SLEEP
1666 /* deferred resume work */
1667 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1670 if (card->dapm_widgets)
1671 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1672 card->num_dapm_widgets);
1674 /* initialise the sound card only once */
1676 ret = card->probe(card);
1678 goto card_probe_error;
1681 /* probe all components used by DAI links on this card */
1682 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1684 for (i = 0; i < card->num_links; i++) {
1685 ret = soc_probe_link_components(card, i, order);
1688 "ASoC: failed to instantiate card %d\n",
1695 /* probe all DAI links on this card */
1696 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1698 for (i = 0; i < card->num_links; i++) {
1699 ret = soc_probe_link_dais(card, i, order);
1702 "ASoC: failed to instantiate card %d\n",
1709 for (i = 0; i < card->num_aux_devs; i++) {
1710 ret = soc_probe_aux_dev(card, i);
1713 "ASoC: failed to add auxiliary devices %d\n",
1715 goto probe_aux_dev_err;
1719 snd_soc_dapm_link_dai_widgets(card);
1720 snd_soc_dapm_connect_dai_link_widgets(card);
1723 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1725 if (card->dapm_routes)
1726 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1727 card->num_dapm_routes);
1729 for (i = 0; i < card->num_links; i++) {
1730 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1731 dai_link = &card->dai_link[i];
1732 dai_fmt = dai_link->dai_fmt;
1735 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1738 for (j = 0; j < rtd->num_codecs; j++) {
1739 struct snd_soc_dai *codec_dai = codec_dais[j];
1741 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1742 if (ret != 0 && ret != -ENOTSUPP)
1743 dev_warn(codec_dai->dev,
1744 "ASoC: Failed to set DAI format: %d\n",
1749 /* If this is a regular CPU link there will be a platform */
1751 (dai_link->platform_name || dai_link->platform_of_node)) {
1752 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1754 if (ret != 0 && ret != -ENOTSUPP)
1755 dev_warn(card->rtd[i].cpu_dai->dev,
1756 "ASoC: Failed to set DAI format: %d\n",
1758 } else if (dai_fmt) {
1759 /* Flip the polarity for the "CPU" end */
1760 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1761 switch (dai_link->dai_fmt &
1762 SND_SOC_DAIFMT_MASTER_MASK) {
1763 case SND_SOC_DAIFMT_CBM_CFM:
1764 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1766 case SND_SOC_DAIFMT_CBM_CFS:
1767 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1769 case SND_SOC_DAIFMT_CBS_CFM:
1770 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1772 case SND_SOC_DAIFMT_CBS_CFS:
1773 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1777 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1779 if (ret != 0 && ret != -ENOTSUPP)
1780 dev_warn(card->rtd[i].cpu_dai->dev,
1781 "ASoC: Failed to set DAI format: %d\n",
1786 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1788 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1789 "%s", card->long_name ? card->long_name : card->name);
1790 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1791 "%s", card->driver_name ? card->driver_name : card->name);
1792 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1793 switch (card->snd_card->driver[i]) {
1799 if (!isalnum(card->snd_card->driver[i]))
1800 card->snd_card->driver[i] = '_';
1805 if (card->late_probe) {
1806 ret = card->late_probe(card);
1808 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
1810 goto probe_aux_dev_err;
1814 if (card->fully_routed)
1815 snd_soc_dapm_auto_nc_pins(card);
1817 snd_soc_dapm_new_widgets(card);
1819 ret = snd_card_register(card->snd_card);
1821 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1823 goto probe_aux_dev_err;
1826 #ifdef CONFIG_SND_SOC_AC97_BUS
1827 /* register any AC97 codecs */
1828 for (i = 0; i < card->num_rtd; i++) {
1829 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1832 "ASoC: failed to register AC97: %d\n", ret);
1834 soc_unregister_ac97_dai_link(&card->rtd[i]);
1835 goto probe_aux_dev_err;
1840 card->instantiated = 1;
1841 snd_soc_dapm_sync(&card->dapm);
1842 mutex_unlock(&card->mutex);
1847 for (i = 0; i < card->num_aux_devs; i++)
1848 soc_remove_aux_dev(card, i);
1851 soc_remove_dai_links(card);
1857 snd_card_free(card->snd_card);
1860 mutex_unlock(&card->mutex);
1865 /* probes a new socdev */
1866 static int soc_probe(struct platform_device *pdev)
1868 struct snd_soc_card *card = platform_get_drvdata(pdev);
1871 * no card, so machine driver should be registering card
1872 * we should not be here in that case so ret error
1877 dev_warn(&pdev->dev,
1878 "ASoC: machine %s should use snd_soc_register_card()\n",
1881 /* Bodge while we unpick instantiation */
1882 card->dev = &pdev->dev;
1884 return snd_soc_register_card(card);
1887 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1891 /* make sure any delayed work runs */
1892 for (i = 0; i < card->num_rtd; i++) {
1893 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1894 flush_delayed_work(&rtd->delayed_work);
1897 /* remove auxiliary devices */
1898 for (i = 0; i < card->num_aux_devs; i++)
1899 soc_remove_aux_dev(card, i);
1901 /* remove and free each DAI */
1902 soc_remove_dai_links(card);
1904 soc_cleanup_card_debugfs(card);
1906 /* remove the card */
1910 snd_soc_dapm_free(&card->dapm);
1912 snd_card_free(card->snd_card);
1917 /* removes a socdev */
1918 static int soc_remove(struct platform_device *pdev)
1920 struct snd_soc_card *card = platform_get_drvdata(pdev);
1922 snd_soc_unregister_card(card);
1926 int snd_soc_poweroff(struct device *dev)
1928 struct snd_soc_card *card = dev_get_drvdata(dev);
1931 if (!card->instantiated)
1934 /* Flush out pmdown_time work - we actually do want to run it
1935 * now, we're shutting down so no imminent restart. */
1936 for (i = 0; i < card->num_rtd; i++) {
1937 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1938 flush_delayed_work(&rtd->delayed_work);
1941 snd_soc_dapm_shutdown(card);
1943 /* deactivate pins to sleep state */
1944 for (i = 0; i < card->num_rtd; i++) {
1945 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1946 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1949 pinctrl_pm_select_sleep_state(cpu_dai->dev);
1950 for (j = 0; j < rtd->num_codecs; j++) {
1951 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
1952 pinctrl_pm_select_sleep_state(codec_dai->dev);
1958 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1960 const struct dev_pm_ops snd_soc_pm_ops = {
1961 .suspend = snd_soc_suspend,
1962 .resume = snd_soc_resume,
1963 .freeze = snd_soc_suspend,
1964 .thaw = snd_soc_resume,
1965 .poweroff = snd_soc_poweroff,
1966 .restore = snd_soc_resume,
1968 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1970 /* ASoC platform driver */
1971 static struct platform_driver soc_driver = {
1973 .name = "soc-audio",
1974 .owner = THIS_MODULE,
1975 .pm = &snd_soc_pm_ops,
1978 .remove = soc_remove,
1982 * snd_soc_new_ac97_codec - initailise AC97 device
1983 * @codec: audio codec
1984 * @ops: AC97 bus operations
1985 * @num: AC97 codec number
1987 * Initialises AC97 codec resources for use by ad-hoc devices only.
1989 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1990 struct snd_ac97_bus_ops *ops, int num)
1992 mutex_lock(&codec->mutex);
1994 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1995 if (codec->ac97 == NULL) {
1996 mutex_unlock(&codec->mutex);
2000 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2001 if (codec->ac97->bus == NULL) {
2004 mutex_unlock(&codec->mutex);
2008 codec->ac97->bus->ops = ops;
2009 codec->ac97->num = num;
2012 * Mark the AC97 device to be created by us. This way we ensure that the
2013 * device will be registered with the device subsystem later on.
2015 codec->ac97_created = 1;
2017 mutex_unlock(&codec->mutex);
2020 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2022 static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2024 static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2026 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2028 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2030 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2034 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2036 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2040 static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2042 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2044 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2046 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2047 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2048 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2052 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2054 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2058 static int snd_soc_ac97_parse_pinctl(struct device *dev,
2059 struct snd_ac97_reset_cfg *cfg)
2062 struct pinctrl_state *state;
2066 p = devm_pinctrl_get(dev);
2068 dev_err(dev, "Failed to get pinctrl\n");
2073 state = pinctrl_lookup_state(p, "ac97-reset");
2074 if (IS_ERR(state)) {
2075 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
2076 return PTR_ERR(state);
2078 cfg->pstate_reset = state;
2080 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2081 if (IS_ERR(state)) {
2082 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
2083 return PTR_ERR(state);
2085 cfg->pstate_warm_reset = state;
2087 state = pinctrl_lookup_state(p, "ac97-running");
2088 if (IS_ERR(state)) {
2089 dev_err(dev, "Can't find pinctrl state ac97-running\n");
2090 return PTR_ERR(state);
2092 cfg->pstate_run = state;
2094 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2096 dev_err(dev, "Can't find ac97-sync gpio\n");
2099 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2101 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2104 cfg->gpio_sync = gpio;
2106 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2108 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2111 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2113 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2116 cfg->gpio_sdata = gpio;
2118 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2120 dev_err(dev, "Can't find ac97-reset gpio\n");
2123 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2125 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2128 cfg->gpio_reset = gpio;
2133 struct snd_ac97_bus_ops *soc_ac97_ops;
2134 EXPORT_SYMBOL_GPL(soc_ac97_ops);
2136 int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2138 if (ops == soc_ac97_ops)
2141 if (soc_ac97_ops && ops)
2148 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2151 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2153 * This function sets the reset and warm_reset properties of ops and parses
2154 * the device node of pdev to get pinctrl states and gpio numbers to use.
2156 int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2157 struct platform_device *pdev)
2159 struct device *dev = &pdev->dev;
2160 struct snd_ac97_reset_cfg cfg;
2163 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2167 ret = snd_soc_set_ac97_ops(ops);
2171 ops->warm_reset = snd_soc_ac97_warm_reset;
2172 ops->reset = snd_soc_ac97_reset;
2174 snd_ac97_rst_cfg = cfg;
2177 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2180 * snd_soc_free_ac97_codec - free AC97 codec device
2181 * @codec: audio codec
2183 * Frees AC97 codec device resources.
2185 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2187 mutex_lock(&codec->mutex);
2188 #ifdef CONFIG_SND_SOC_AC97_BUS
2189 soc_unregister_ac97_codec(codec);
2191 kfree(codec->ac97->bus);
2194 codec->ac97_created = 0;
2195 mutex_unlock(&codec->mutex);
2197 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2200 * snd_soc_cnew - create new control
2201 * @_template: control template
2202 * @data: control private data
2203 * @long_name: control long name
2204 * @prefix: control name prefix
2206 * Create a new mixer control from a template control.
2208 * Returns 0 for success, else error.
2210 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2211 void *data, const char *long_name,
2214 struct snd_kcontrol_new template;
2215 struct snd_kcontrol *kcontrol;
2218 memcpy(&template, _template, sizeof(template));
2222 long_name = template.name;
2225 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2229 template.name = name;
2231 template.name = long_name;
2234 kcontrol = snd_ctl_new1(&template, data);
2240 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2242 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2243 const struct snd_kcontrol_new *controls, int num_controls,
2244 const char *prefix, void *data)
2248 for (i = 0; i < num_controls; i++) {
2249 const struct snd_kcontrol_new *control = &controls[i];
2250 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2251 control->name, prefix));
2253 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2254 control->name, err);
2262 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2265 struct snd_card *card = soc_card->snd_card;
2266 struct snd_kcontrol *kctl;
2268 if (unlikely(!name))
2271 list_for_each_entry(kctl, &card->controls, list)
2272 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2276 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2279 * snd_soc_add_component_controls - Add an array of controls to a component.
2281 * @component: Component to add controls to
2282 * @controls: Array of controls to add
2283 * @num_controls: Number of elements in the array
2285 * Return: 0 for success, else error.
2287 int snd_soc_add_component_controls(struct snd_soc_component *component,
2288 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2290 struct snd_card *card = component->card->snd_card;
2292 return snd_soc_add_controls(card, component->dev, controls,
2293 num_controls, component->name_prefix, component);
2295 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2298 * snd_soc_add_codec_controls - add an array of controls to a codec.
2299 * Convenience function to add a list of controls. Many codecs were
2300 * duplicating this code.
2302 * @codec: codec to add controls to
2303 * @controls: array of controls to add
2304 * @num_controls: number of elements in the array
2306 * Return 0 for success, else error.
2308 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2309 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2311 return snd_soc_add_component_controls(&codec->component, controls,
2314 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2317 * snd_soc_add_platform_controls - add an array of controls to a platform.
2318 * Convenience function to add a list of controls.
2320 * @platform: platform to add controls to
2321 * @controls: array of controls to add
2322 * @num_controls: number of elements in the array
2324 * Return 0 for success, else error.
2326 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2327 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2329 return snd_soc_add_component_controls(&platform->component, controls,
2332 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2335 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2336 * Convenience function to add a list of controls.
2338 * @soc_card: SoC card to add controls to
2339 * @controls: array of controls to add
2340 * @num_controls: number of elements in the array
2342 * Return 0 for success, else error.
2344 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2345 const struct snd_kcontrol_new *controls, int num_controls)
2347 struct snd_card *card = soc_card->snd_card;
2349 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2352 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2355 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2356 * Convienience function to add a list of controls.
2358 * @dai: DAI to add controls to
2359 * @controls: array of controls to add
2360 * @num_controls: number of elements in the array
2362 * Return 0 for success, else error.
2364 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2365 const struct snd_kcontrol_new *controls, int num_controls)
2367 struct snd_card *card = dai->card->snd_card;
2369 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2372 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2375 * snd_soc_info_enum_double - enumerated double mixer info callback
2376 * @kcontrol: mixer control
2377 * @uinfo: control element information
2379 * Callback to provide information about a double enumerated
2382 * Returns 0 for success.
2384 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2385 struct snd_ctl_elem_info *uinfo)
2387 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2389 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2390 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2391 uinfo->value.enumerated.items = e->items;
2393 if (uinfo->value.enumerated.item >= e->items)
2394 uinfo->value.enumerated.item = e->items - 1;
2395 strlcpy(uinfo->value.enumerated.name,
2396 e->texts[uinfo->value.enumerated.item],
2397 sizeof(uinfo->value.enumerated.name));
2400 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2403 * snd_soc_get_enum_double - enumerated double mixer get callback
2404 * @kcontrol: mixer control
2405 * @ucontrol: control element information
2407 * Callback to get the value of a double enumerated mixer.
2409 * Returns 0 for success.
2411 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2412 struct snd_ctl_elem_value *ucontrol)
2414 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2415 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2416 unsigned int val, item;
2417 unsigned int reg_val;
2420 ret = snd_soc_component_read(component, e->reg, ®_val);
2423 val = (reg_val >> e->shift_l) & e->mask;
2424 item = snd_soc_enum_val_to_item(e, val);
2425 ucontrol->value.enumerated.item[0] = item;
2426 if (e->shift_l != e->shift_r) {
2427 val = (reg_val >> e->shift_l) & e->mask;
2428 item = snd_soc_enum_val_to_item(e, val);
2429 ucontrol->value.enumerated.item[1] = item;
2434 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2437 * snd_soc_put_enum_double - enumerated double mixer put callback
2438 * @kcontrol: mixer control
2439 * @ucontrol: control element information
2441 * Callback to set the value of a double enumerated mixer.
2443 * Returns 0 for success.
2445 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2446 struct snd_ctl_elem_value *ucontrol)
2448 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2449 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2450 unsigned int *item = ucontrol->value.enumerated.item;
2454 if (item[0] >= e->items)
2456 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
2457 mask = e->mask << e->shift_l;
2458 if (e->shift_l != e->shift_r) {
2459 if (item[1] >= e->items)
2461 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
2462 mask |= e->mask << e->shift_r;
2465 return snd_soc_component_update_bits(component, e->reg, mask, val);
2467 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2470 * snd_soc_read_signed - Read a codec register and interprete as signed value
2471 * @component: component
2472 * @reg: Register to read
2473 * @mask: Mask to use after shifting the register value
2474 * @shift: Right shift of register value
2475 * @sign_bit: Bit that describes if a number is negative or not.
2476 * @signed_val: Pointer to where the read value should be stored
2478 * This functions reads a codec register. The register value is shifted right
2479 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2480 * the given registervalue into a signed integer if sign_bit is non-zero.
2482 * Returns 0 on sucess, otherwise an error value
2484 static int snd_soc_read_signed(struct snd_soc_component *component,
2485 unsigned int reg, unsigned int mask, unsigned int shift,
2486 unsigned int sign_bit, int *signed_val)
2491 ret = snd_soc_component_read(component, reg, &val);
2495 val = (val >> shift) & mask;
2502 /* non-negative number */
2503 if (!(val & BIT(sign_bit))) {
2511 * The register most probably does not contain a full-sized int.
2512 * Instead we have an arbitrary number of bits in a signed
2513 * representation which has to be translated into a full-sized int.
2514 * This is done by filling up all bits above the sign-bit.
2516 ret |= ~((int)(BIT(sign_bit) - 1));
2524 * snd_soc_info_volsw - single mixer info callback
2525 * @kcontrol: mixer control
2526 * @uinfo: control element information
2528 * Callback to provide information about a single mixer control, or a double
2529 * mixer control that spans 2 registers.
2531 * Returns 0 for success.
2533 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2534 struct snd_ctl_elem_info *uinfo)
2536 struct soc_mixer_control *mc =
2537 (struct soc_mixer_control *)kcontrol->private_value;
2540 if (!mc->platform_max)
2541 mc->platform_max = mc->max;
2542 platform_max = mc->platform_max;
2544 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2545 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2547 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2549 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2550 uinfo->value.integer.min = 0;
2551 uinfo->value.integer.max = platform_max - mc->min;
2554 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2557 * snd_soc_get_volsw - single mixer get callback
2558 * @kcontrol: mixer control
2559 * @ucontrol: control element information
2561 * Callback to get the value of a single mixer control, or a double mixer
2562 * control that spans 2 registers.
2564 * Returns 0 for success.
2566 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2567 struct snd_ctl_elem_value *ucontrol)
2569 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2570 struct soc_mixer_control *mc =
2571 (struct soc_mixer_control *)kcontrol->private_value;
2572 unsigned int reg = mc->reg;
2573 unsigned int reg2 = mc->rreg;
2574 unsigned int shift = mc->shift;
2575 unsigned int rshift = mc->rshift;
2578 int sign_bit = mc->sign_bit;
2579 unsigned int mask = (1 << fls(max)) - 1;
2580 unsigned int invert = mc->invert;
2585 mask = BIT(sign_bit + 1) - 1;
2587 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2591 ucontrol->value.integer.value[0] = val - min;
2593 ucontrol->value.integer.value[0] =
2594 max - ucontrol->value.integer.value[0];
2596 if (snd_soc_volsw_is_stereo(mc)) {
2598 ret = snd_soc_read_signed(component, reg, mask, rshift,
2601 ret = snd_soc_read_signed(component, reg2, mask, shift,
2606 ucontrol->value.integer.value[1] = val - min;
2608 ucontrol->value.integer.value[1] =
2609 max - ucontrol->value.integer.value[1];
2614 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2617 * snd_soc_put_volsw - single mixer put callback
2618 * @kcontrol: mixer control
2619 * @ucontrol: control element information
2621 * Callback to set the value of a single mixer control, or a double mixer
2622 * control that spans 2 registers.
2624 * Returns 0 for success.
2626 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2627 struct snd_ctl_elem_value *ucontrol)
2629 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2630 struct soc_mixer_control *mc =
2631 (struct soc_mixer_control *)kcontrol->private_value;
2632 unsigned int reg = mc->reg;
2633 unsigned int reg2 = mc->rreg;
2634 unsigned int shift = mc->shift;
2635 unsigned int rshift = mc->rshift;
2638 unsigned int sign_bit = mc->sign_bit;
2639 unsigned int mask = (1 << fls(max)) - 1;
2640 unsigned int invert = mc->invert;
2642 bool type_2r = false;
2643 unsigned int val2 = 0;
2644 unsigned int val, val_mask;
2647 mask = BIT(sign_bit + 1) - 1;
2649 val = ((ucontrol->value.integer.value[0] + min) & mask);
2652 val_mask = mask << shift;
2654 if (snd_soc_volsw_is_stereo(mc)) {
2655 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
2659 val_mask |= mask << rshift;
2660 val |= val2 << rshift;
2662 val2 = val2 << shift;
2666 err = snd_soc_component_update_bits(component, reg, val_mask, val);
2671 err = snd_soc_component_update_bits(component, reg2, val_mask,
2676 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2679 * snd_soc_get_volsw_sx - single mixer get callback
2680 * @kcontrol: mixer control
2681 * @ucontrol: control element information
2683 * Callback to get the value of a single mixer control, or a double mixer
2684 * control that spans 2 registers.
2686 * Returns 0 for success.
2688 int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2689 struct snd_ctl_elem_value *ucontrol)
2691 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2692 struct soc_mixer_control *mc =
2693 (struct soc_mixer_control *)kcontrol->private_value;
2694 unsigned int reg = mc->reg;
2695 unsigned int reg2 = mc->rreg;
2696 unsigned int shift = mc->shift;
2697 unsigned int rshift = mc->rshift;
2700 int mask = (1 << (fls(min + max) - 1)) - 1;
2704 ret = snd_soc_component_read(component, reg, &val);
2708 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2710 if (snd_soc_volsw_is_stereo(mc)) {
2711 ret = snd_soc_component_read(component, reg2, &val);
2715 val = ((val >> rshift) - min) & mask;
2716 ucontrol->value.integer.value[1] = val;
2721 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2724 * snd_soc_put_volsw_sx - double mixer set callback
2725 * @kcontrol: mixer control
2726 * @uinfo: control element information
2728 * Callback to set the value of a double mixer control that spans 2 registers.
2730 * Returns 0 for success.
2732 int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2733 struct snd_ctl_elem_value *ucontrol)
2735 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2736 struct soc_mixer_control *mc =
2737 (struct soc_mixer_control *)kcontrol->private_value;
2739 unsigned int reg = mc->reg;
2740 unsigned int reg2 = mc->rreg;
2741 unsigned int shift = mc->shift;
2742 unsigned int rshift = mc->rshift;
2745 int mask = (1 << (fls(min + max) - 1)) - 1;
2747 unsigned int val, val_mask, val2 = 0;
2749 val_mask = mask << shift;
2750 val = (ucontrol->value.integer.value[0] + min) & mask;
2753 err = snd_soc_component_update_bits(component, reg, val_mask, val);
2757 if (snd_soc_volsw_is_stereo(mc)) {
2758 val_mask = mask << rshift;
2759 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2760 val2 = val2 << rshift;
2762 err = snd_soc_component_update_bits(component, reg2, val_mask,
2767 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2770 * snd_soc_info_volsw_s8 - signed mixer info callback
2771 * @kcontrol: mixer control
2772 * @uinfo: control element information
2774 * Callback to provide information about a signed mixer control.
2776 * Returns 0 for success.
2778 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2779 struct snd_ctl_elem_info *uinfo)
2781 struct soc_mixer_control *mc =
2782 (struct soc_mixer_control *)kcontrol->private_value;
2786 if (!mc->platform_max)
2787 mc->platform_max = mc->max;
2788 platform_max = mc->platform_max;
2790 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2792 uinfo->value.integer.min = 0;
2793 uinfo->value.integer.max = platform_max - min;
2796 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2799 * snd_soc_get_volsw_s8 - signed mixer get callback
2800 * @kcontrol: mixer control
2801 * @ucontrol: control element information
2803 * Callback to get the value of a signed mixer control.
2805 * Returns 0 for success.
2807 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2808 struct snd_ctl_elem_value *ucontrol)
2810 struct soc_mixer_control *mc =
2811 (struct soc_mixer_control *)kcontrol->private_value;
2812 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2813 unsigned int reg = mc->reg;
2818 ret = snd_soc_component_read(component, reg, &val);
2822 ucontrol->value.integer.value[0] =
2823 ((signed char)(val & 0xff))-min;
2824 ucontrol->value.integer.value[1] =
2825 ((signed char)((val >> 8) & 0xff))-min;
2828 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2831 * snd_soc_put_volsw_sgn - signed mixer put callback
2832 * @kcontrol: mixer control
2833 * @ucontrol: control element information
2835 * Callback to set the value of a signed mixer control.
2837 * Returns 0 for success.
2839 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2840 struct snd_ctl_elem_value *ucontrol)
2842 struct soc_mixer_control *mc =
2843 (struct soc_mixer_control *)kcontrol->private_value;
2844 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2845 unsigned int reg = mc->reg;
2849 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2850 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2852 return snd_soc_component_update_bits(component, reg, 0xffff, val);
2854 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2857 * snd_soc_info_volsw_range - single mixer info callback with range.
2858 * @kcontrol: mixer control
2859 * @uinfo: control element information
2861 * Callback to provide information, within a range, about a single
2864 * returns 0 for success.
2866 int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2867 struct snd_ctl_elem_info *uinfo)
2869 struct soc_mixer_control *mc =
2870 (struct soc_mixer_control *)kcontrol->private_value;
2874 if (!mc->platform_max)
2875 mc->platform_max = mc->max;
2876 platform_max = mc->platform_max;
2878 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2879 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2880 uinfo->value.integer.min = 0;
2881 uinfo->value.integer.max = platform_max - min;
2885 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2888 * snd_soc_put_volsw_range - single mixer put value callback with range.
2889 * @kcontrol: mixer control
2890 * @ucontrol: control element information
2892 * Callback to set the value, within a range, for a single mixer control.
2894 * Returns 0 for success.
2896 int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2897 struct snd_ctl_elem_value *ucontrol)
2899 struct soc_mixer_control *mc =
2900 (struct soc_mixer_control *)kcontrol->private_value;
2901 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2902 unsigned int reg = mc->reg;
2903 unsigned int rreg = mc->rreg;
2904 unsigned int shift = mc->shift;
2907 unsigned int mask = (1 << fls(max)) - 1;
2908 unsigned int invert = mc->invert;
2909 unsigned int val, val_mask;
2912 val = ((ucontrol->value.integer.value[0] + min) & mask);
2915 val_mask = mask << shift;
2918 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
2922 if (snd_soc_volsw_is_stereo(mc)) {
2923 val = ((ucontrol->value.integer.value[1] + min) & mask);
2926 val_mask = mask << shift;
2929 ret = snd_soc_component_update_bits(component, rreg, val_mask,
2935 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2938 * snd_soc_get_volsw_range - single mixer get callback with range
2939 * @kcontrol: mixer control
2940 * @ucontrol: control element information
2942 * Callback to get the value, within a range, of a single mixer control.
2944 * Returns 0 for success.
2946 int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2947 struct snd_ctl_elem_value *ucontrol)
2949 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2950 struct soc_mixer_control *mc =
2951 (struct soc_mixer_control *)kcontrol->private_value;
2952 unsigned int reg = mc->reg;
2953 unsigned int rreg = mc->rreg;
2954 unsigned int shift = mc->shift;
2957 unsigned int mask = (1 << fls(max)) - 1;
2958 unsigned int invert = mc->invert;
2962 ret = snd_soc_component_read(component, reg, &val);
2966 ucontrol->value.integer.value[0] = (val >> shift) & mask;
2968 ucontrol->value.integer.value[0] =
2969 max - ucontrol->value.integer.value[0];
2970 ucontrol->value.integer.value[0] =
2971 ucontrol->value.integer.value[0] - min;
2973 if (snd_soc_volsw_is_stereo(mc)) {
2974 ret = snd_soc_component_read(component, rreg, &val);
2978 ucontrol->value.integer.value[1] = (val >> shift) & mask;
2980 ucontrol->value.integer.value[1] =
2981 max - ucontrol->value.integer.value[1];
2982 ucontrol->value.integer.value[1] =
2983 ucontrol->value.integer.value[1] - min;
2988 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
2991 * snd_soc_limit_volume - Set new limit to an existing volume control.
2993 * @codec: where to look for the control
2994 * @name: Name of the control
2995 * @max: new maximum limit
2997 * Return 0 for success, else error.
2999 int snd_soc_limit_volume(struct snd_soc_codec *codec,
3000 const char *name, int max)
3002 struct snd_card *card = codec->component.card->snd_card;
3003 struct snd_kcontrol *kctl;
3004 struct soc_mixer_control *mc;
3008 /* Sanity check for name and max */
3009 if (unlikely(!name || max <= 0))
3012 list_for_each_entry(kctl, &card->controls, list) {
3013 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3019 mc = (struct soc_mixer_control *)kctl->private_value;
3020 if (max <= mc->max) {
3021 mc->platform_max = max;
3027 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3029 int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3030 struct snd_ctl_elem_info *uinfo)
3032 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3033 struct soc_bytes *params = (void *)kcontrol->private_value;
3035 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3036 uinfo->count = params->num_regs * component->val_bytes;
3040 EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3042 int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3043 struct snd_ctl_elem_value *ucontrol)
3045 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3046 struct soc_bytes *params = (void *)kcontrol->private_value;
3049 if (component->regmap)
3050 ret = regmap_raw_read(component->regmap, params->base,
3051 ucontrol->value.bytes.data,
3052 params->num_regs * component->val_bytes);
3056 /* Hide any masked bytes to ensure consistent data reporting */
3057 if (ret == 0 && params->mask) {
3058 switch (component->val_bytes) {
3060 ucontrol->value.bytes.data[0] &= ~params->mask;
3063 ((u16 *)(&ucontrol->value.bytes.data))[0]
3064 &= cpu_to_be16(~params->mask);
3067 ((u32 *)(&ucontrol->value.bytes.data))[0]
3068 &= cpu_to_be32(~params->mask);
3077 EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3079 int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3080 struct snd_ctl_elem_value *ucontrol)
3082 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3083 struct soc_bytes *params = (void *)kcontrol->private_value;
3085 unsigned int val, mask;
3088 if (!component->regmap)
3091 len = params->num_regs * component->val_bytes;
3093 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3098 * If we've got a mask then we need to preserve the register
3099 * bits. We shouldn't modify the incoming data so take a
3103 ret = regmap_read(component->regmap, params->base, &val);
3107 val &= params->mask;
3109 switch (component->val_bytes) {
3111 ((u8 *)data)[0] &= ~params->mask;
3112 ((u8 *)data)[0] |= val;
3115 mask = ~params->mask;
3116 ret = regmap_parse_val(component->regmap,
3121 ((u16 *)data)[0] &= mask;
3123 ret = regmap_parse_val(component->regmap,
3128 ((u16 *)data)[0] |= val;
3131 mask = ~params->mask;
3132 ret = regmap_parse_val(component->regmap,
3137 ((u32 *)data)[0] &= mask;
3139 ret = regmap_parse_val(component->regmap,
3144 ((u32 *)data)[0] |= val;
3152 ret = regmap_raw_write(component->regmap, params->base,
3160 EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3162 int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3163 struct snd_ctl_elem_info *ucontrol)
3165 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3167 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3168 ucontrol->count = params->max;
3172 EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3174 int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
3175 unsigned int size, unsigned int __user *tlv)
3177 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3178 unsigned int count = size < params->max ? size : params->max;
3182 case SNDRV_CTL_TLV_OP_READ:
3184 ret = params->get(tlv, count);
3186 case SNDRV_CTL_TLV_OP_WRITE:
3188 ret = params->put(tlv, count);
3193 EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
3196 * snd_soc_info_xr_sx - signed multi register info callback
3197 * @kcontrol: mreg control
3198 * @uinfo: control element information
3200 * Callback to provide information of a control that can
3201 * span multiple codec registers which together
3202 * forms a single signed value in a MSB/LSB manner.
3204 * Returns 0 for success.
3206 int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3207 struct snd_ctl_elem_info *uinfo)
3209 struct soc_mreg_control *mc =
3210 (struct soc_mreg_control *)kcontrol->private_value;
3211 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3213 uinfo->value.integer.min = mc->min;
3214 uinfo->value.integer.max = mc->max;
3218 EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3221 * snd_soc_get_xr_sx - signed multi register get callback
3222 * @kcontrol: mreg control
3223 * @ucontrol: control element information
3225 * Callback to get the value of a control that can span
3226 * multiple codec registers which together forms a single
3227 * signed value in a MSB/LSB manner. The control supports
3228 * specifying total no of bits used to allow for bitfields
3229 * across the multiple codec registers.
3231 * Returns 0 for success.
3233 int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3234 struct snd_ctl_elem_value *ucontrol)
3236 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3237 struct soc_mreg_control *mc =
3238 (struct soc_mreg_control *)kcontrol->private_value;
3239 unsigned int regbase = mc->regbase;
3240 unsigned int regcount = mc->regcount;
3241 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
3242 unsigned int regwmask = (1<<regwshift)-1;
3243 unsigned int invert = mc->invert;
3244 unsigned long mask = (1UL<<mc->nbits)-1;
3248 unsigned int regval;
3252 for (i = 0; i < regcount; i++) {
3253 ret = snd_soc_component_read(component, regbase+i, ®val);
3256 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
3259 if (min < 0 && val > max)
3263 ucontrol->value.integer.value[0] = val;
3267 EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3270 * snd_soc_put_xr_sx - signed multi register get callback
3271 * @kcontrol: mreg control
3272 * @ucontrol: control element information
3274 * Callback to set the value of a control that can span
3275 * multiple codec registers which together forms a single
3276 * signed value in a MSB/LSB manner. The control supports
3277 * specifying total no of bits used to allow for bitfields
3278 * across the multiple codec registers.
3280 * Returns 0 for success.
3282 int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3283 struct snd_ctl_elem_value *ucontrol)
3285 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3286 struct soc_mreg_control *mc =
3287 (struct soc_mreg_control *)kcontrol->private_value;
3288 unsigned int regbase = mc->regbase;
3289 unsigned int regcount = mc->regcount;
3290 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
3291 unsigned int regwmask = (1<<regwshift)-1;
3292 unsigned int invert = mc->invert;
3293 unsigned long mask = (1UL<<mc->nbits)-1;
3295 long val = ucontrol->value.integer.value[0];
3296 unsigned int i, regval, regmask;
3302 for (i = 0; i < regcount; i++) {
3303 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3304 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3305 err = snd_soc_component_update_bits(component, regbase+i,
3313 EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3316 * snd_soc_get_strobe - strobe get callback
3317 * @kcontrol: mixer control
3318 * @ucontrol: control element information
3320 * Callback get the value of a strobe mixer control.
3322 * Returns 0 for success.
3324 int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3325 struct snd_ctl_elem_value *ucontrol)
3327 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3328 struct soc_mixer_control *mc =
3329 (struct soc_mixer_control *)kcontrol->private_value;
3330 unsigned int reg = mc->reg;
3331 unsigned int shift = mc->shift;
3332 unsigned int mask = 1 << shift;
3333 unsigned int invert = mc->invert != 0;
3337 ret = snd_soc_component_read(component, reg, &val);
3343 if (shift != 0 && val != 0)
3345 ucontrol->value.enumerated.item[0] = val ^ invert;
3349 EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3352 * snd_soc_put_strobe - strobe put callback
3353 * @kcontrol: mixer control
3354 * @ucontrol: control element information
3356 * Callback strobe a register bit to high then low (or the inverse)
3357 * in one pass of a single mixer enum control.
3359 * Returns 1 for success.
3361 int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3362 struct snd_ctl_elem_value *ucontrol)
3364 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3365 struct soc_mixer_control *mc =
3366 (struct soc_mixer_control *)kcontrol->private_value;
3367 unsigned int reg = mc->reg;
3368 unsigned int shift = mc->shift;
3369 unsigned int mask = 1 << shift;
3370 unsigned int invert = mc->invert != 0;
3371 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3372 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3373 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3376 err = snd_soc_component_update_bits(component, reg, mask, val1);
3380 return snd_soc_component_update_bits(component, reg, mask, val2);
3382 EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3385 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3387 * @clk_id: DAI specific clock ID
3388 * @freq: new clock frequency in Hz
3389 * @dir: new clock direction - input/output.
3391 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3393 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3394 unsigned int freq, int dir)
3396 if (dai->driver && dai->driver->ops->set_sysclk)
3397 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3398 else if (dai->codec && dai->codec->driver->set_sysclk)
3399 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3404 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3407 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3409 * @clk_id: DAI specific clock ID
3410 * @source: Source for the clock
3411 * @freq: new clock frequency in Hz
3412 * @dir: new clock direction - input/output.
3414 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3416 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3417 int source, unsigned int freq, int dir)
3419 if (codec->driver->set_sysclk)
3420 return codec->driver->set_sysclk(codec, clk_id, source,
3425 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3428 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3430 * @div_id: DAI specific clock divider ID
3431 * @div: new clock divisor.
3433 * Configures the clock dividers. This is used to derive the best DAI bit and
3434 * frame clocks from the system or master clock. It's best to set the DAI bit
3435 * and frame clocks as low as possible to save system power.
3437 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3438 int div_id, int div)
3440 if (dai->driver && dai->driver->ops->set_clkdiv)
3441 return dai->driver->ops->set_clkdiv(dai, div_id, div);
3445 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3448 * snd_soc_dai_set_pll - configure DAI PLL.
3450 * @pll_id: DAI specific PLL ID
3451 * @source: DAI specific source for the PLL
3452 * @freq_in: PLL input clock frequency in Hz
3453 * @freq_out: requested PLL output clock frequency in Hz
3455 * Configures and enables PLL to generate output clock based on input clock.
3457 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3458 unsigned int freq_in, unsigned int freq_out)
3460 if (dai->driver && dai->driver->ops->set_pll)
3461 return dai->driver->ops->set_pll(dai, pll_id, source,
3463 else if (dai->codec && dai->codec->driver->set_pll)
3464 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3469 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3472 * snd_soc_codec_set_pll - configure codec PLL.
3474 * @pll_id: DAI specific PLL ID
3475 * @source: DAI specific source for the PLL
3476 * @freq_in: PLL input clock frequency in Hz
3477 * @freq_out: requested PLL output clock frequency in Hz
3479 * Configures and enables PLL to generate output clock based on input clock.
3481 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3482 unsigned int freq_in, unsigned int freq_out)
3484 if (codec->driver->set_pll)
3485 return codec->driver->set_pll(codec, pll_id, source,
3490 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3493 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3495 * @ratio Ratio of BCLK to Sample rate.
3497 * Configures the DAI for a preset BCLK to sample rate ratio.
3499 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3501 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3502 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3506 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3509 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3511 * @fmt: SND_SOC_DAIFMT_ format value.
3513 * Configures the DAI hardware format and clocking.
3515 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3517 if (dai->driver == NULL)
3519 if (dai->driver->ops->set_fmt == NULL)
3521 return dai->driver->ops->set_fmt(dai, fmt);
3523 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3526 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
3527 * @slots: Number of slots in use.
3528 * @tx_mask: bitmask representing active TX slots.
3529 * @rx_mask: bitmask representing active RX slots.
3531 * Generates the TDM tx and rx slot default masks for DAI.
3533 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
3534 unsigned int *tx_mask,
3535 unsigned int *rx_mask)
3537 if (*tx_mask || *rx_mask)
3543 *tx_mask = (1 << slots) - 1;
3544 *rx_mask = (1 << slots) - 1;
3550 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3552 * @tx_mask: bitmask representing active TX slots.
3553 * @rx_mask: bitmask representing active RX slots.
3554 * @slots: Number of slots in use.
3555 * @slot_width: Width in bits for each slot.
3557 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3560 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3561 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
3563 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3564 dai->driver->ops->xlate_tdm_slot_mask(slots,
3565 &tx_mask, &rx_mask);
3567 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
3569 dai->tx_mask = tx_mask;
3570 dai->rx_mask = rx_mask;
3572 if (dai->driver && dai->driver->ops->set_tdm_slot)
3573 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3578 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3581 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3583 * @tx_num: how many TX channels
3584 * @tx_slot: pointer to an array which imply the TX slot number channel
3586 * @rx_num: how many RX channels
3587 * @rx_slot: pointer to an array which imply the RX slot number channel
3590 * configure the relationship between channel number and TDM slot number.
3592 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3593 unsigned int tx_num, unsigned int *tx_slot,
3594 unsigned int rx_num, unsigned int *rx_slot)
3596 if (dai->driver && dai->driver->ops->set_channel_map)
3597 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3602 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3605 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3607 * @tristate: tristate enable
3609 * Tristates the DAI so that others can use it.
3611 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3613 if (dai->driver && dai->driver->ops->set_tristate)
3614 return dai->driver->ops->set_tristate(dai, tristate);
3618 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3621 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3623 * @mute: mute enable
3624 * @direction: stream to mute
3626 * Mutes the DAI DAC.
3628 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3634 if (dai->driver->ops->mute_stream)
3635 return dai->driver->ops->mute_stream(dai, mute, direction);
3636 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3637 dai->driver->ops->digital_mute)
3638 return dai->driver->ops->digital_mute(dai, mute);
3642 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3644 static int snd_soc_init_multicodec(struct snd_soc_card *card,
3645 struct snd_soc_dai_link *dai_link)
3647 /* Legacy codec/codec_dai link is a single entry in multicodec */
3648 if (dai_link->codec_name || dai_link->codec_of_node ||
3649 dai_link->codec_dai_name) {
3650 dai_link->num_codecs = 1;
3652 dai_link->codecs = devm_kzalloc(card->dev,
3653 sizeof(struct snd_soc_dai_link_component),
3655 if (!dai_link->codecs)
3658 dai_link->codecs[0].name = dai_link->codec_name;
3659 dai_link->codecs[0].of_node = dai_link->codec_of_node;
3660 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
3663 if (!dai_link->codecs) {
3664 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
3672 * snd_soc_register_card - Register a card with the ASoC core
3674 * @card: Card to register
3677 int snd_soc_register_card(struct snd_soc_card *card)
3681 if (!card->name || !card->dev)
3684 for (i = 0; i < card->num_links; i++) {
3685 struct snd_soc_dai_link *link = &card->dai_link[i];
3687 ret = snd_soc_init_multicodec(card, link);
3689 dev_err(card->dev, "ASoC: failed to init multicodec\n");
3693 for (j = 0; j < link->num_codecs; j++) {
3695 * Codec must be specified by 1 of name or OF node,
3696 * not both or neither.
3698 if (!!link->codecs[j].name ==
3699 !!link->codecs[j].of_node) {
3700 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
3704 /* Codec DAI name must be specified */
3705 if (!link->codecs[j].dai_name) {
3706 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
3713 * Platform may be specified by either name or OF node, but
3714 * can be left unspecified, and a dummy platform will be used.
3716 if (link->platform_name && link->platform_of_node) {
3718 "ASoC: Both platform name/of_node are set for %s\n",
3724 * CPU device may be specified by either name or OF node, but
3725 * can be left unspecified, and will be matched based on DAI
3728 if (link->cpu_name && link->cpu_of_node) {
3730 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3735 * At least one of CPU DAI name or CPU device name/node must be
3738 if (!link->cpu_dai_name &&
3739 !(link->cpu_name || link->cpu_of_node)) {
3741 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3747 dev_set_drvdata(card->dev, card);
3749 snd_soc_initialize_card_lists(card);
3751 soc_init_card_debugfs(card);
3753 card->rtd = devm_kzalloc(card->dev,
3754 sizeof(struct snd_soc_pcm_runtime) *
3755 (card->num_links + card->num_aux_devs),
3757 if (card->rtd == NULL)
3760 card->rtd_aux = &card->rtd[card->num_links];
3762 for (i = 0; i < card->num_links; i++) {
3763 card->rtd[i].card = card;
3764 card->rtd[i].dai_link = &card->dai_link[i];
3765 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
3766 sizeof(struct snd_soc_dai *) *
3767 (card->rtd[i].dai_link->num_codecs),
3769 if (card->rtd[i].codec_dais == NULL)
3773 for (i = 0; i < card->num_aux_devs; i++)
3774 card->rtd_aux[i].card = card;
3776 INIT_LIST_HEAD(&card->dapm_dirty);
3777 card->instantiated = 0;
3778 mutex_init(&card->mutex);
3779 mutex_init(&card->dapm_mutex);
3781 ret = snd_soc_instantiate_card(card);
3783 soc_cleanup_card_debugfs(card);
3785 /* deactivate pins to sleep state */
3786 for (i = 0; i < card->num_rtd; i++) {
3787 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
3788 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3791 for (j = 0; j < rtd->num_codecs; j++) {
3792 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
3793 if (!codec_dai->active)
3794 pinctrl_pm_select_sleep_state(codec_dai->dev);
3797 if (!cpu_dai->active)
3798 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3803 EXPORT_SYMBOL_GPL(snd_soc_register_card);
3806 * snd_soc_unregister_card - Unregister a card with the ASoC core
3808 * @card: Card to unregister
3811 int snd_soc_unregister_card(struct snd_soc_card *card)
3813 if (card->instantiated) {
3814 card->instantiated = false;
3815 snd_soc_dapm_shutdown(card);
3816 soc_cleanup_card_resources(card);
3818 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
3822 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3825 * Simplify DAI link configuration by removing ".-1" from device names
3826 * and sanitizing names.
3828 static char *fmt_single_name(struct device *dev, int *id)
3830 char *found, name[NAME_SIZE];
3833 if (dev_name(dev) == NULL)
3836 strlcpy(name, dev_name(dev), NAME_SIZE);
3838 /* are we a "%s.%d" name (platform and SPI components) */
3839 found = strstr(name, dev->driver->name);
3842 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3844 /* discard ID from name if ID == -1 */
3846 found[strlen(dev->driver->name)] = '\0';
3850 /* I2C component devices are named "bus-addr" */
3851 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3852 char tmp[NAME_SIZE];
3854 /* create unique ID number from I2C addr and bus */
3855 *id = ((id1 & 0xffff) << 16) + id2;
3857 /* sanitize component name for DAI link creation */
3858 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3859 strlcpy(name, tmp, NAME_SIZE);
3864 return kstrdup(name, GFP_KERNEL);
3868 * Simplify DAI link naming for single devices with multiple DAIs by removing
3869 * any ".-1" and using the DAI name (instead of device name).
3871 static inline char *fmt_multiple_name(struct device *dev,
3872 struct snd_soc_dai_driver *dai_drv)
3874 if (dai_drv->name == NULL) {
3876 "ASoC: error - multiple DAI %s registered with no name\n",
3881 return kstrdup(dai_drv->name, GFP_KERNEL);
3885 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
3887 * @component: The component for which the DAIs should be unregistered
3889 static void snd_soc_unregister_dais(struct snd_soc_component *component)
3891 struct snd_soc_dai *dai, *_dai;
3893 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
3894 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3896 list_del(&dai->list);
3903 * snd_soc_register_dais - Register a DAI with the ASoC core
3905 * @component: The component the DAIs are registered for
3906 * @dai_drv: DAI driver to use for the DAIs
3907 * @count: Number of DAIs
3908 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3911 static int snd_soc_register_dais(struct snd_soc_component *component,
3912 struct snd_soc_dai_driver *dai_drv, size_t count,
3913 bool legacy_dai_naming)
3915 struct device *dev = component->dev;
3916 struct snd_soc_dai *dai;
3920 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
3922 component->dai_drv = dai_drv;
3923 component->num_dai = count;
3925 for (i = 0; i < count; i++) {
3927 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3934 * Back in the old days when we still had component-less DAIs,
3935 * instead of having a static name, component-less DAIs would
3936 * inherit the name of the parent device so it is possible to
3937 * register multiple instances of the DAI. We still need to keep
3938 * the same naming style even though those DAIs are not
3939 * component-less anymore.
3941 if (count == 1 && legacy_dai_naming) {
3942 dai->name = fmt_single_name(dev, &dai->id);
3944 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3946 dai->id = dai_drv[i].id;
3950 if (dai->name == NULL) {
3956 dai->component = component;
3958 dai->driver = &dai_drv[i];
3959 if (!dai->driver->ops)
3960 dai->driver->ops = &null_dai_ops;
3962 list_add(&dai->list, &component->dai_list);
3964 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3970 snd_soc_unregister_dais(component);
3975 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3976 enum snd_soc_dapm_type type, int subseq)
3978 struct snd_soc_component *component = dapm->component;
3980 component->driver->seq_notifier(component, type, subseq);
3983 static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3986 struct snd_soc_component *component = dapm->component;
3988 return component->driver->stream_event(component, event);
3991 static int snd_soc_component_initialize(struct snd_soc_component *component,
3992 const struct snd_soc_component_driver *driver, struct device *dev)
3994 struct snd_soc_dapm_context *dapm;
3996 component->name = fmt_single_name(dev, &component->id);
3997 if (!component->name) {
3998 dev_err(dev, "ASoC: Failed to allocate name\n");
4002 component->dev = dev;
4003 component->driver = driver;
4004 component->probe = component->driver->probe;
4005 component->remove = component->driver->remove;
4007 if (!component->dapm_ptr)
4008 component->dapm_ptr = &component->dapm;
4010 dapm = component->dapm_ptr;
4012 dapm->component = component;
4013 dapm->bias_level = SND_SOC_BIAS_OFF;
4014 dapm->idle_bias_off = true;
4015 if (driver->seq_notifier)
4016 dapm->seq_notifier = snd_soc_component_seq_notifier;
4017 if (driver->stream_event)
4018 dapm->stream_event = snd_soc_component_stream_event;
4020 component->controls = driver->controls;
4021 component->num_controls = driver->num_controls;
4022 component->dapm_widgets = driver->dapm_widgets;
4023 component->num_dapm_widgets = driver->num_dapm_widgets;
4024 component->dapm_routes = driver->dapm_routes;
4025 component->num_dapm_routes = driver->num_dapm_routes;
4027 INIT_LIST_HEAD(&component->dai_list);
4028 mutex_init(&component->io_mutex);
4033 static void snd_soc_component_init_regmap(struct snd_soc_component *component)
4035 if (!component->regmap)
4036 component->regmap = dev_get_regmap(component->dev, NULL);
4037 if (component->regmap) {
4038 int val_bytes = regmap_get_val_bytes(component->regmap);
4039 /* Errors are legitimate for non-integer byte multiples */
4041 component->val_bytes = val_bytes;
4045 static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
4047 if (!component->write && !component->read)
4048 snd_soc_component_init_regmap(component);
4050 list_add(&component->list, &component_list);
4053 static void snd_soc_component_add(struct snd_soc_component *component)
4055 mutex_lock(&client_mutex);
4056 snd_soc_component_add_unlocked(component);
4057 mutex_unlock(&client_mutex);
4060 static void snd_soc_component_cleanup(struct snd_soc_component *component)
4062 snd_soc_unregister_dais(component);
4063 kfree(component->name);
4066 static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
4068 list_del(&component->list);
4071 static void snd_soc_component_del(struct snd_soc_component *component)
4073 mutex_lock(&client_mutex);
4074 snd_soc_component_del_unlocked(component);
4075 mutex_unlock(&client_mutex);
4078 int snd_soc_register_component(struct device *dev,
4079 const struct snd_soc_component_driver *cmpnt_drv,
4080 struct snd_soc_dai_driver *dai_drv,
4083 struct snd_soc_component *cmpnt;
4086 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
4088 dev_err(dev, "ASoC: Failed to allocate memory\n");
4092 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
4096 cmpnt->ignore_pmdown_time = true;
4097 cmpnt->registered_as_component = true;
4099 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
4101 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4105 snd_soc_component_add(cmpnt);
4110 snd_soc_component_cleanup(cmpnt);
4115 EXPORT_SYMBOL_GPL(snd_soc_register_component);
4118 * snd_soc_unregister_component - Unregister a component from the ASoC core
4121 void snd_soc_unregister_component(struct device *dev)
4123 struct snd_soc_component *cmpnt;
4125 list_for_each_entry(cmpnt, &component_list, list) {
4126 if (dev == cmpnt->dev && cmpnt->registered_as_component)
4132 snd_soc_component_del(cmpnt);
4133 snd_soc_component_cleanup(cmpnt);
4136 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4138 static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
4140 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4142 return platform->driver->probe(platform);
4145 static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
4147 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4149 platform->driver->remove(platform);
4153 * snd_soc_add_platform - Add a platform to the ASoC core
4154 * @dev: The parent device for the platform
4155 * @platform: The platform to add
4156 * @platform_driver: The driver for the platform
4158 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
4159 const struct snd_soc_platform_driver *platform_drv)
4163 ret = snd_soc_component_initialize(&platform->component,
4164 &platform_drv->component_driver, dev);
4168 platform->dev = dev;
4169 platform->driver = platform_drv;
4170 if (platform_drv->controls) {
4171 platform->component.controls = platform_drv->controls;
4172 platform->component.num_controls = platform_drv->num_controls;
4174 if (platform_drv->dapm_widgets) {
4175 platform->component.dapm_widgets = platform_drv->dapm_widgets;
4176 platform->component.num_dapm_widgets = platform_drv->num_dapm_widgets;
4177 platform->component.steal_sibling_dai_widgets = true;
4179 if (platform_drv->dapm_routes) {
4180 platform->component.dapm_routes = platform_drv->dapm_routes;
4181 platform->component.num_dapm_routes = platform_drv->num_dapm_routes;
4184 if (platform_drv->probe)
4185 platform->component.probe = snd_soc_platform_drv_probe;
4186 if (platform_drv->remove)
4187 platform->component.remove = snd_soc_platform_drv_remove;
4189 #ifdef CONFIG_DEBUG_FS
4190 platform->component.debugfs_prefix = "platform";
4193 mutex_lock(&client_mutex);
4194 snd_soc_component_add_unlocked(&platform->component);
4195 list_add(&platform->list, &platform_list);
4196 mutex_unlock(&client_mutex);
4198 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
4199 platform->component.name);
4203 EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4206 * snd_soc_register_platform - Register a platform with the ASoC core
4208 * @platform: platform to register
4210 int snd_soc_register_platform(struct device *dev,
4211 const struct snd_soc_platform_driver *platform_drv)
4213 struct snd_soc_platform *platform;
4216 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4218 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4219 if (platform == NULL)
4222 ret = snd_soc_add_platform(dev, platform, platform_drv);
4228 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4231 * snd_soc_remove_platform - Remove a platform from the ASoC core
4232 * @platform: the platform to remove
4234 void snd_soc_remove_platform(struct snd_soc_platform *platform)
4237 mutex_lock(&client_mutex);
4238 list_del(&platform->list);
4239 snd_soc_component_del_unlocked(&platform->component);
4240 mutex_unlock(&client_mutex);
4242 snd_soc_component_cleanup(&platform->component);
4244 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
4245 platform->component.name);
4247 EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4249 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4251 struct snd_soc_platform *platform;
4253 list_for_each_entry(platform, &platform_list, list) {
4254 if (dev == platform->dev)
4260 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4263 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4265 * @platform: platform to unregister
4267 void snd_soc_unregister_platform(struct device *dev)
4269 struct snd_soc_platform *platform;
4271 platform = snd_soc_lookup_platform(dev);
4275 snd_soc_remove_platform(platform);
4278 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4280 static u64 codec_format_map[] = {
4281 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4282 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4283 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4284 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4285 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4286 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4287 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4288 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4289 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4290 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4291 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4292 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4293 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4294 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4295 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4296 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4299 /* Fix up the DAI formats for endianness: codecs don't actually see
4300 * the endianness of the data but we're using the CPU format
4301 * definitions which do need to include endianness so we ensure that
4302 * codec DAIs always have both big and little endian variants set.
4304 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4308 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4309 if (stream->formats & codec_format_map[i])
4310 stream->formats |= codec_format_map[i];
4313 static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
4315 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4317 return codec->driver->probe(codec);
4320 static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
4322 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4324 codec->driver->remove(codec);
4327 static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4328 unsigned int reg, unsigned int val)
4330 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4332 return codec->driver->write(codec, reg, val);
4335 static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4336 unsigned int reg, unsigned int *val)
4338 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4340 *val = codec->driver->read(codec, reg);
4345 static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
4346 enum snd_soc_bias_level level)
4348 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
4350 return codec->driver->set_bias_level(codec, level);
4354 * snd_soc_register_codec - Register a codec with the ASoC core
4356 * @codec: codec to register
4358 int snd_soc_register_codec(struct device *dev,
4359 const struct snd_soc_codec_driver *codec_drv,
4360 struct snd_soc_dai_driver *dai_drv,
4363 struct snd_soc_codec *codec;
4364 struct snd_soc_dai *dai;
4367 dev_dbg(dev, "codec register %s\n", dev_name(dev));
4369 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4373 codec->component.dapm_ptr = &codec->dapm;
4374 codec->component.codec = codec;
4376 ret = snd_soc_component_initialize(&codec->component,
4377 &codec_drv->component_driver, dev);
4381 if (codec_drv->controls) {
4382 codec->component.controls = codec_drv->controls;
4383 codec->component.num_controls = codec_drv->num_controls;
4385 if (codec_drv->dapm_widgets) {
4386 codec->component.dapm_widgets = codec_drv->dapm_widgets;
4387 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
4389 if (codec_drv->dapm_routes) {
4390 codec->component.dapm_routes = codec_drv->dapm_routes;
4391 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
4394 if (codec_drv->probe)
4395 codec->component.probe = snd_soc_codec_drv_probe;
4396 if (codec_drv->remove)
4397 codec->component.remove = snd_soc_codec_drv_remove;
4398 if (codec_drv->write)
4399 codec->component.write = snd_soc_codec_drv_write;
4400 if (codec_drv->read)
4401 codec->component.read = snd_soc_codec_drv_read;
4402 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
4403 codec->dapm.codec = codec;
4404 codec->dapm.idle_bias_off = codec_drv->idle_bias_off;
4405 codec->dapm.suspend_bias_off = codec_drv->suspend_bias_off;
4406 if (codec_drv->seq_notifier)
4407 codec->dapm.seq_notifier = codec_drv->seq_notifier;
4408 if (codec_drv->set_bias_level)
4409 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
4411 codec->driver = codec_drv;
4412 codec->component.val_bytes = codec_drv->reg_word_size;
4413 mutex_init(&codec->mutex);
4415 #ifdef CONFIG_DEBUG_FS
4416 codec->component.init_debugfs = soc_init_codec_debugfs;
4417 codec->component.debugfs_prefix = "codec";
4420 if (codec_drv->get_regmap)
4421 codec->component.regmap = codec_drv->get_regmap(dev);
4423 for (i = 0; i < num_dai; i++) {
4424 fixup_codec_formats(&dai_drv[i].playback);
4425 fixup_codec_formats(&dai_drv[i].capture);
4428 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4430 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4434 list_for_each_entry(dai, &codec->component.dai_list, list)
4437 mutex_lock(&client_mutex);
4438 snd_soc_component_add_unlocked(&codec->component);
4439 list_add(&codec->list, &codec_list);
4440 mutex_unlock(&client_mutex);
4442 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4443 codec->component.name);
4447 snd_soc_component_cleanup(&codec->component);
4452 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4455 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4457 * @codec: codec to unregister
4459 void snd_soc_unregister_codec(struct device *dev)
4461 struct snd_soc_codec *codec;
4463 list_for_each_entry(codec, &codec_list, list) {
4464 if (dev == codec->dev)
4471 mutex_lock(&client_mutex);
4472 list_del(&codec->list);
4473 snd_soc_component_del_unlocked(&codec->component);
4474 mutex_unlock(&client_mutex);
4476 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4477 codec->component.name);
4479 snd_soc_component_cleanup(&codec->component);
4480 snd_soc_cache_exit(codec);
4483 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4485 /* Retrieve a card's name from device tree */
4486 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4487 const char *propname)
4489 struct device_node *np;
4493 pr_err("card->dev is not set before calling %s\n", __func__);
4497 np = card->dev->of_node;
4499 ret = of_property_read_string_index(np, propname, 0, &card->name);
4501 * EINVAL means the property does not exist. This is fine providing
4502 * card->name was previously set, which is checked later in
4503 * snd_soc_register_card.
4505 if (ret < 0 && ret != -EINVAL) {
4507 "ASoC: Property '%s' could not be read: %d\n",
4514 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4516 static const struct snd_soc_dapm_widget simple_widgets[] = {
4517 SND_SOC_DAPM_MIC("Microphone", NULL),
4518 SND_SOC_DAPM_LINE("Line", NULL),
4519 SND_SOC_DAPM_HP("Headphone", NULL),
4520 SND_SOC_DAPM_SPK("Speaker", NULL),
4523 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4524 const char *propname)
4526 struct device_node *np = card->dev->of_node;
4527 struct snd_soc_dapm_widget *widgets;
4528 const char *template, *wname;
4529 int i, j, num_widgets, ret;
4531 num_widgets = of_property_count_strings(np, propname);
4532 if (num_widgets < 0) {
4534 "ASoC: Property '%s' does not exist\n", propname);
4537 if (num_widgets & 1) {
4539 "ASoC: Property '%s' length is not even\n", propname);
4545 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4550 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4554 "ASoC: Could not allocate memory for widgets\n");
4558 for (i = 0; i < num_widgets; i++) {
4559 ret = of_property_read_string_index(np, propname,
4563 "ASoC: Property '%s' index %d read error:%d\n",
4564 propname, 2 * i, ret);
4568 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4569 if (!strncmp(template, simple_widgets[j].name,
4570 strlen(simple_widgets[j].name))) {
4571 widgets[i] = simple_widgets[j];
4576 if (j >= ARRAY_SIZE(simple_widgets)) {
4578 "ASoC: DAPM widget '%s' is not supported\n",
4583 ret = of_property_read_string_index(np, propname,
4588 "ASoC: Property '%s' index %d read error:%d\n",
4589 propname, (2 * i) + 1, ret);
4593 widgets[i].name = wname;
4596 card->dapm_widgets = widgets;
4597 card->num_dapm_widgets = num_widgets;
4601 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4603 int snd_soc_of_parse_tdm_slot(struct device_node *np,
4604 unsigned int *slots,
4605 unsigned int *slot_width)
4610 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4611 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4619 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4620 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4630 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4632 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4633 const char *propname)
4635 struct device_node *np = card->dev->of_node;
4637 struct snd_soc_dapm_route *routes;
4640 num_routes = of_property_count_strings(np, propname);
4641 if (num_routes < 0 || num_routes & 1) {
4643 "ASoC: Property '%s' does not exist or its length is not even\n",
4649 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4654 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4658 "ASoC: Could not allocate DAPM route table\n");
4662 for (i = 0; i < num_routes; i++) {
4663 ret = of_property_read_string_index(np, propname,
4664 2 * i, &routes[i].sink);
4667 "ASoC: Property '%s' index %d could not be read: %d\n",
4668 propname, 2 * i, ret);
4671 ret = of_property_read_string_index(np, propname,
4672 (2 * i) + 1, &routes[i].source);
4675 "ASoC: Property '%s' index %d could not be read: %d\n",
4676 propname, (2 * i) + 1, ret);
4681 card->num_dapm_routes = num_routes;
4682 card->dapm_routes = routes;
4686 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4688 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4690 struct device_node **bitclkmaster,
4691 struct device_node **framemaster)
4695 unsigned int format = 0;
4701 } of_fmt_table[] = {
4702 { "i2s", SND_SOC_DAIFMT_I2S },
4703 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4704 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4705 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4706 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4707 { "ac97", SND_SOC_DAIFMT_AC97 },
4708 { "pdm", SND_SOC_DAIFMT_PDM},
4709 { "msb", SND_SOC_DAIFMT_MSB },
4710 { "lsb", SND_SOC_DAIFMT_LSB },
4717 * check "[prefix]format = xxx"
4718 * SND_SOC_DAIFMT_FORMAT_MASK area
4720 snprintf(prop, sizeof(prop), "%sformat", prefix);
4721 ret = of_property_read_string(np, prop, &str);
4723 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4724 if (strcmp(str, of_fmt_table[i].name) == 0) {
4725 format |= of_fmt_table[i].val;
4732 * check "[prefix]continuous-clock"
4733 * SND_SOC_DAIFMT_CLOCK_MASK area
4735 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4736 if (of_get_property(np, prop, NULL))
4737 format |= SND_SOC_DAIFMT_CONT;
4739 format |= SND_SOC_DAIFMT_GATED;
4742 * check "[prefix]bitclock-inversion"
4743 * check "[prefix]frame-inversion"
4744 * SND_SOC_DAIFMT_INV_MASK area
4746 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4747 bit = !!of_get_property(np, prop, NULL);
4749 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4750 frame = !!of_get_property(np, prop, NULL);
4752 switch ((bit << 4) + frame) {
4754 format |= SND_SOC_DAIFMT_IB_IF;
4757 format |= SND_SOC_DAIFMT_IB_NF;
4760 format |= SND_SOC_DAIFMT_NB_IF;
4763 /* SND_SOC_DAIFMT_NB_NF is default */
4768 * check "[prefix]bitclock-master"
4769 * check "[prefix]frame-master"
4770 * SND_SOC_DAIFMT_MASTER_MASK area
4772 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4773 bit = !!of_get_property(np, prop, NULL);
4774 if (bit && bitclkmaster)
4775 *bitclkmaster = of_parse_phandle(np, prop, 0);
4777 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4778 frame = !!of_get_property(np, prop, NULL);
4779 if (frame && framemaster)
4780 *framemaster = of_parse_phandle(np, prop, 0);
4782 switch ((bit << 4) + frame) {
4784 format |= SND_SOC_DAIFMT_CBM_CFM;
4787 format |= SND_SOC_DAIFMT_CBM_CFS;
4790 format |= SND_SOC_DAIFMT_CBS_CFM;
4793 format |= SND_SOC_DAIFMT_CBS_CFS;
4799 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4801 int snd_soc_of_get_dai_name(struct device_node *of_node,
4802 const char **dai_name)
4804 struct snd_soc_component *pos;
4805 struct of_phandle_args args;
4808 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4809 "#sound-dai-cells", 0, &args);
4813 ret = -EPROBE_DEFER;
4815 mutex_lock(&client_mutex);
4816 list_for_each_entry(pos, &component_list, list) {
4817 if (pos->dev->of_node != args.np)
4820 if (pos->driver->of_xlate_dai_name) {
4821 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4825 switch (args.args_count) {
4827 id = 0; /* same as dai_drv[0] */
4837 if (id < 0 || id >= pos->num_dai) {
4844 *dai_name = pos->dai_drv[id].name;
4846 *dai_name = pos->name;
4851 mutex_unlock(&client_mutex);
4853 of_node_put(args.np);
4857 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4859 static int __init snd_soc_init(void)
4861 #ifdef CONFIG_DEBUG_FS
4862 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4863 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
4864 pr_warn("ASoC: Failed to create debugfs directory\n");
4865 snd_soc_debugfs_root = NULL;
4868 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4870 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4872 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4874 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
4876 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
4877 &platform_list_fops))
4878 pr_warn("ASoC: Failed to create platform list debugfs file\n");
4881 snd_soc_util_init();
4883 return platform_driver_register(&soc_driver);
4885 module_init(snd_soc_init);
4887 static void __exit snd_soc_exit(void)
4889 snd_soc_util_exit();
4891 #ifdef CONFIG_DEBUG_FS
4892 debugfs_remove_recursive(snd_soc_debugfs_root);
4894 platform_driver_unregister(&soc_driver);
4896 module_exit(snd_soc_exit);
4898 /* Module information */
4899 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4900 MODULE_DESCRIPTION("ALSA SoC Core");
4901 MODULE_LICENSE("GPL");
4902 MODULE_ALIAS("platform:soc-audio");