x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / sound / soc / soc-core.c
blob42c2a3065b7799bfd971c58f9d0cd7239353efa8
1 /*
2 * soc-core.c -- ALSA SoC Audio Layer
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
18 * TODO:
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
29 #include <linux/pm.h>
30 #include <linux/bitops.h>
31 #include <linux/debugfs.h>
32 #include <linux/platform_device.h>
33 #include <linux/pinctrl/consumer.h>
34 #include <linux/ctype.h>
35 #include <linux/slab.h>
36 #include <linux/of.h>
37 #include <linux/of_graph.h>
38 #include <linux/dmi.h>
39 #include <sound/core.h>
40 #include <sound/jack.h>
41 #include <sound/pcm.h>
42 #include <sound/pcm_params.h>
43 #include <sound/soc.h>
44 #include <sound/soc-dpcm.h>
45 #include <sound/soc-topology.h>
46 #include <sound/initval.h>
48 #define CREATE_TRACE_POINTS
49 #include <trace/events/asoc.h>
51 #define NAME_SIZE 32
53 #ifdef CONFIG_DEBUG_FS
54 struct dentry *snd_soc_debugfs_root;
55 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
56 #endif
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 /* If a DMI filed contain strings in this blacklist (e.g.
73 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
74 * as invalid and dropped when setting the card long name from DMI info.
76 static const char * const dmi_blacklist[] = {
77 "To be filled by OEM",
78 "TBD by OEM",
79 "Default String",
80 "Board Manufacturer",
81 "Board Vendor Name",
82 "Board Product Name",
83 NULL, /* terminator */
86 /* returns the minimum number of bytes needed to represent
87 * a particular given value */
88 static int min_bytes_needed(unsigned long val)
90 int c = 0;
91 int i;
93 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
94 if (val & (1UL << i))
95 break;
96 c = (sizeof val * 8) - c;
97 if (!c || (c % 8))
98 c = (c + 8) / 8;
99 else
100 c /= 8;
101 return c;
104 /* fill buf which is 'len' bytes with a formatted
105 * string of the form 'reg: value\n' */
106 static int format_register_str(struct snd_soc_codec *codec,
107 unsigned int reg, char *buf, size_t len)
109 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
110 int regsize = codec->driver->reg_word_size * 2;
111 int ret;
113 /* +2 for ': ' and + 1 for '\n' */
114 if (wordsize + regsize + 2 + 1 != len)
115 return -EINVAL;
117 sprintf(buf, "%.*x: ", wordsize, reg);
118 buf += wordsize + 2;
120 ret = snd_soc_read(codec, reg);
121 if (ret < 0)
122 memset(buf, 'X', regsize);
123 else
124 sprintf(buf, "%.*x", regsize, ret);
125 buf[regsize] = '\n';
126 /* no NUL-termination needed */
127 return 0;
130 /* codec register dump */
131 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
132 size_t count, loff_t pos)
134 int i, step = 1;
135 int wordsize, regsize;
136 int len;
137 size_t total = 0;
138 loff_t p = 0;
140 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
141 regsize = codec->driver->reg_word_size * 2;
143 len = wordsize + regsize + 2 + 1;
145 if (!codec->driver->reg_cache_size)
146 return 0;
148 if (codec->driver->reg_cache_step)
149 step = codec->driver->reg_cache_step;
151 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
152 /* only support larger than PAGE_SIZE bytes debugfs
153 * entries for the default case */
154 if (p >= pos) {
155 if (total + len >= count - 1)
156 break;
157 format_register_str(codec, i, buf + total, len);
158 total += len;
160 p += len;
163 total = min(total, count - 1);
165 return total;
168 static ssize_t codec_reg_show(struct device *dev,
169 struct device_attribute *attr, char *buf)
171 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
173 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
176 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
178 static ssize_t pmdown_time_show(struct device *dev,
179 struct device_attribute *attr, char *buf)
181 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
183 return sprintf(buf, "%ld\n", rtd->pmdown_time);
186 static ssize_t pmdown_time_set(struct device *dev,
187 struct device_attribute *attr,
188 const char *buf, size_t count)
190 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
191 int ret;
193 ret = kstrtol(buf, 10, &rtd->pmdown_time);
194 if (ret)
195 return ret;
197 return count;
200 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
202 static struct attribute *soc_dev_attrs[] = {
203 &dev_attr_codec_reg.attr,
204 &dev_attr_pmdown_time.attr,
205 NULL
208 static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
209 struct attribute *attr, int idx)
211 struct device *dev = kobj_to_dev(kobj);
212 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
214 if (attr == &dev_attr_pmdown_time.attr)
215 return attr->mode; /* always visible */
216 return rtd->codec ? attr->mode : 0; /* enabled only with codec */
219 static const struct attribute_group soc_dapm_dev_group = {
220 .attrs = soc_dapm_dev_attrs,
221 .is_visible = soc_dev_attr_is_visible,
224 static const struct attribute_group soc_dev_roup = {
225 .attrs = soc_dev_attrs,
226 .is_visible = soc_dev_attr_is_visible,
229 static const struct attribute_group *soc_dev_attr_groups[] = {
230 &soc_dapm_dev_group,
231 &soc_dev_roup,
232 NULL
235 #ifdef CONFIG_DEBUG_FS
236 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
237 size_t count, loff_t *ppos)
239 ssize_t ret;
240 struct snd_soc_codec *codec = file->private_data;
241 char *buf;
243 if (*ppos < 0 || !count)
244 return -EINVAL;
246 buf = kmalloc(count, GFP_KERNEL);
247 if (!buf)
248 return -ENOMEM;
250 ret = soc_codec_reg_show(codec, buf, count, *ppos);
251 if (ret >= 0) {
252 if (copy_to_user(user_buf, buf, ret)) {
253 kfree(buf);
254 return -EFAULT;
256 *ppos += ret;
259 kfree(buf);
260 return ret;
263 static ssize_t codec_reg_write_file(struct file *file,
264 const char __user *user_buf, size_t count, loff_t *ppos)
266 char buf[32];
267 size_t buf_size;
268 char *start = buf;
269 unsigned long reg, value;
270 struct snd_soc_codec *codec = file->private_data;
271 int ret;
273 buf_size = min(count, (sizeof(buf)-1));
274 if (copy_from_user(buf, user_buf, buf_size))
275 return -EFAULT;
276 buf[buf_size] = 0;
278 while (*start == ' ')
279 start++;
280 reg = simple_strtoul(start, &start, 16);
281 while (*start == ' ')
282 start++;
283 ret = kstrtoul(start, 16, &value);
284 if (ret)
285 return ret;
287 /* Userspace has been fiddling around behind the kernel's back */
288 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
290 snd_soc_write(codec, reg, value);
291 return buf_size;
294 static const struct file_operations codec_reg_fops = {
295 .open = simple_open,
296 .read = codec_reg_read_file,
297 .write = codec_reg_write_file,
298 .llseek = default_llseek,
301 static void soc_init_component_debugfs(struct snd_soc_component *component)
303 if (!component->card->debugfs_card_root)
304 return;
306 if (component->debugfs_prefix) {
307 char *name;
309 name = kasprintf(GFP_KERNEL, "%s:%s",
310 component->debugfs_prefix, component->name);
311 if (name) {
312 component->debugfs_root = debugfs_create_dir(name,
313 component->card->debugfs_card_root);
314 kfree(name);
316 } else {
317 component->debugfs_root = debugfs_create_dir(component->name,
318 component->card->debugfs_card_root);
321 if (!component->debugfs_root) {
322 dev_warn(component->dev,
323 "ASoC: Failed to create component debugfs directory\n");
324 return;
327 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
328 component->debugfs_root);
330 if (component->init_debugfs)
331 component->init_debugfs(component);
334 static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
336 debugfs_remove_recursive(component->debugfs_root);
339 static void soc_init_codec_debugfs(struct snd_soc_component *component)
341 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
342 struct dentry *debugfs_reg;
344 debugfs_reg = debugfs_create_file("codec_reg", 0644,
345 codec->component.debugfs_root,
346 codec, &codec_reg_fops);
347 if (!debugfs_reg)
348 dev_warn(codec->dev,
349 "ASoC: Failed to create codec register debugfs file\n");
352 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
353 size_t count, loff_t *ppos)
355 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
356 ssize_t len, ret = 0;
357 struct snd_soc_codec *codec;
359 if (!buf)
360 return -ENOMEM;
362 mutex_lock(&client_mutex);
364 list_for_each_entry(codec, &codec_list, list) {
365 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
366 codec->component.name);
367 if (len >= 0)
368 ret += len;
369 if (ret > PAGE_SIZE) {
370 ret = PAGE_SIZE;
371 break;
375 mutex_unlock(&client_mutex);
377 if (ret >= 0)
378 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
380 kfree(buf);
382 return ret;
385 static const struct file_operations codec_list_fops = {
386 .read = codec_list_read_file,
387 .llseek = default_llseek,/* read accesses f_pos */
390 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
391 size_t count, loff_t *ppos)
393 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
394 ssize_t len, ret = 0;
395 struct snd_soc_component *component;
396 struct snd_soc_dai *dai;
398 if (!buf)
399 return -ENOMEM;
401 mutex_lock(&client_mutex);
403 list_for_each_entry(component, &component_list, list) {
404 list_for_each_entry(dai, &component->dai_list, list) {
405 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
406 dai->name);
407 if (len >= 0)
408 ret += len;
409 if (ret > PAGE_SIZE) {
410 ret = PAGE_SIZE;
411 break;
416 mutex_unlock(&client_mutex);
418 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
420 kfree(buf);
422 return ret;
425 static const struct file_operations dai_list_fops = {
426 .read = dai_list_read_file,
427 .llseek = default_llseek,/* read accesses f_pos */
430 static ssize_t platform_list_read_file(struct file *file,
431 char __user *user_buf,
432 size_t count, loff_t *ppos)
434 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
435 ssize_t len, ret = 0;
436 struct snd_soc_platform *platform;
438 if (!buf)
439 return -ENOMEM;
441 mutex_lock(&client_mutex);
443 list_for_each_entry(platform, &platform_list, list) {
444 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
445 platform->component.name);
446 if (len >= 0)
447 ret += len;
448 if (ret > PAGE_SIZE) {
449 ret = PAGE_SIZE;
450 break;
454 mutex_unlock(&client_mutex);
456 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
458 kfree(buf);
460 return ret;
463 static const struct file_operations platform_list_fops = {
464 .read = platform_list_read_file,
465 .llseek = default_llseek,/* read accesses f_pos */
468 static void soc_init_card_debugfs(struct snd_soc_card *card)
470 if (!snd_soc_debugfs_root)
471 return;
473 card->debugfs_card_root = debugfs_create_dir(card->name,
474 snd_soc_debugfs_root);
475 if (!card->debugfs_card_root) {
476 dev_warn(card->dev,
477 "ASoC: Failed to create card debugfs directory\n");
478 return;
481 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
482 card->debugfs_card_root,
483 &card->pop_time);
484 if (!card->debugfs_pop_time)
485 dev_warn(card->dev,
486 "ASoC: Failed to create pop time debugfs file\n");
489 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
491 debugfs_remove_recursive(card->debugfs_card_root);
495 static void snd_soc_debugfs_init(void)
497 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
498 if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) {
499 pr_warn("ASoC: Failed to create debugfs directory\n");
500 snd_soc_debugfs_root = NULL;
501 return;
504 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
505 &codec_list_fops))
506 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
508 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
509 &dai_list_fops))
510 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
512 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
513 &platform_list_fops))
514 pr_warn("ASoC: Failed to create platform list debugfs file\n");
517 static void snd_soc_debugfs_exit(void)
519 debugfs_remove_recursive(snd_soc_debugfs_root);
522 #else
524 #define soc_init_codec_debugfs NULL
526 static inline void soc_init_component_debugfs(
527 struct snd_soc_component *component)
531 static inline void soc_cleanup_component_debugfs(
532 struct snd_soc_component *component)
536 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
540 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
544 static inline void snd_soc_debugfs_init(void)
548 static inline void snd_soc_debugfs_exit(void)
552 #endif
554 static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
555 struct snd_soc_component *component)
557 struct snd_soc_rtdcom_list *rtdcom;
558 struct snd_soc_rtdcom_list *new_rtdcom;
560 for_each_rtdcom(rtd, rtdcom) {
561 /* already connected */
562 if (rtdcom->component == component)
563 return 0;
566 new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
567 if (!new_rtdcom)
568 return -ENOMEM;
570 new_rtdcom->component = component;
571 INIT_LIST_HEAD(&new_rtdcom->list);
573 list_add_tail(&new_rtdcom->list, &rtd->component_list);
575 return 0;
578 static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
580 struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
582 for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
583 kfree(rtdcom1);
585 INIT_LIST_HEAD(&rtd->component_list);
588 struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
589 const char *driver_name)
591 struct snd_soc_rtdcom_list *rtdcom;
593 for_each_rtdcom(rtd, rtdcom) {
594 if ((rtdcom->component->driver->name == driver_name) ||
595 strcmp(rtdcom->component->driver->name, driver_name) == 0)
596 return rtdcom->component;
599 return NULL;
602 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
603 const char *dai_link, int stream)
605 struct snd_soc_pcm_runtime *rtd;
607 list_for_each_entry(rtd, &card->rtd_list, list) {
608 if (rtd->dai_link->no_pcm &&
609 !strcmp(rtd->dai_link->name, dai_link))
610 return rtd->pcm->streams[stream].substream;
612 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
613 return NULL;
615 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
617 static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
618 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
620 struct snd_soc_pcm_runtime *rtd;
622 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
623 if (!rtd)
624 return NULL;
626 INIT_LIST_HEAD(&rtd->component_list);
627 rtd->card = card;
628 rtd->dai_link = dai_link;
629 rtd->codec_dais = kzalloc(sizeof(struct snd_soc_dai *) *
630 dai_link->num_codecs,
631 GFP_KERNEL);
632 if (!rtd->codec_dais) {
633 kfree(rtd);
634 return NULL;
637 return rtd;
640 static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
642 if (rtd && rtd->codec_dais)
643 kfree(rtd->codec_dais);
644 snd_soc_rtdcom_del_all(rtd);
645 kfree(rtd);
648 static void soc_add_pcm_runtime(struct snd_soc_card *card,
649 struct snd_soc_pcm_runtime *rtd)
651 list_add_tail(&rtd->list, &card->rtd_list);
652 rtd->num = card->num_rtd;
653 card->num_rtd++;
656 static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
658 struct snd_soc_pcm_runtime *rtd, *_rtd;
660 list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) {
661 list_del(&rtd->list);
662 soc_free_pcm_runtime(rtd);
665 card->num_rtd = 0;
668 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
669 const char *dai_link)
671 struct snd_soc_pcm_runtime *rtd;
673 list_for_each_entry(rtd, &card->rtd_list, list) {
674 if (!strcmp(rtd->dai_link->name, dai_link))
675 return rtd;
677 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
678 return NULL;
680 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
682 static void codec2codec_close_delayed_work(struct work_struct *work)
684 /* Currently nothing to do for c2c links
685 * Since c2c links are internal nodes in the DAPM graph and
686 * don't interface with the outside world or application layer
687 * we don't have to do any special handling on close.
691 #ifdef CONFIG_PM_SLEEP
692 /* powers down audio subsystem for suspend */
693 int snd_soc_suspend(struct device *dev)
695 struct snd_soc_card *card = dev_get_drvdata(dev);
696 struct snd_soc_component *component;
697 struct snd_soc_pcm_runtime *rtd;
698 int i;
700 /* If the card is not initialized yet there is nothing to do */
701 if (!card->instantiated)
702 return 0;
704 /* Due to the resume being scheduled into a workqueue we could
705 * suspend before that's finished - wait for it to complete.
707 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
709 /* we're going to block userspace touching us until resume completes */
710 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
712 /* mute any active DACs */
713 list_for_each_entry(rtd, &card->rtd_list, list) {
715 if (rtd->dai_link->ignore_suspend)
716 continue;
718 for (i = 0; i < rtd->num_codecs; i++) {
719 struct snd_soc_dai *dai = rtd->codec_dais[i];
720 struct snd_soc_dai_driver *drv = dai->driver;
722 if (drv->ops->digital_mute && dai->playback_active)
723 drv->ops->digital_mute(dai, 1);
727 /* suspend all pcms */
728 list_for_each_entry(rtd, &card->rtd_list, list) {
729 if (rtd->dai_link->ignore_suspend)
730 continue;
732 snd_pcm_suspend_all(rtd->pcm);
735 if (card->suspend_pre)
736 card->suspend_pre(card);
738 list_for_each_entry(rtd, &card->rtd_list, list) {
739 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
741 if (rtd->dai_link->ignore_suspend)
742 continue;
744 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
745 cpu_dai->driver->suspend(cpu_dai);
748 /* close any waiting streams */
749 list_for_each_entry(rtd, &card->rtd_list, list)
750 flush_delayed_work(&rtd->delayed_work);
752 list_for_each_entry(rtd, &card->rtd_list, list) {
754 if (rtd->dai_link->ignore_suspend)
755 continue;
757 snd_soc_dapm_stream_event(rtd,
758 SNDRV_PCM_STREAM_PLAYBACK,
759 SND_SOC_DAPM_STREAM_SUSPEND);
761 snd_soc_dapm_stream_event(rtd,
762 SNDRV_PCM_STREAM_CAPTURE,
763 SND_SOC_DAPM_STREAM_SUSPEND);
766 /* Recheck all endpoints too, their state is affected by suspend */
767 dapm_mark_endpoints_dirty(card);
768 snd_soc_dapm_sync(&card->dapm);
770 /* suspend all COMPONENTs */
771 list_for_each_entry(component, &card->component_dev_list, card_list) {
772 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
774 /* If there are paths active then the COMPONENT will be held with
775 * bias _ON and should not be suspended. */
776 if (!component->suspended) {
777 switch (snd_soc_dapm_get_bias_level(dapm)) {
778 case SND_SOC_BIAS_STANDBY:
780 * If the COMPONENT is capable of idle
781 * bias off then being in STANDBY
782 * means it's doing something,
783 * otherwise fall through.
785 if (dapm->idle_bias_off) {
786 dev_dbg(component->dev,
787 "ASoC: idle_bias_off CODEC on over suspend\n");
788 break;
791 case SND_SOC_BIAS_OFF:
792 if (component->suspend)
793 component->suspend(component);
794 component->suspended = 1;
795 if (component->regmap)
796 regcache_mark_dirty(component->regmap);
797 /* deactivate pins to sleep state */
798 pinctrl_pm_select_sleep_state(component->dev);
799 break;
800 default:
801 dev_dbg(component->dev,
802 "ASoC: COMPONENT is on over suspend\n");
803 break;
808 list_for_each_entry(rtd, &card->rtd_list, list) {
809 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
811 if (rtd->dai_link->ignore_suspend)
812 continue;
814 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
815 cpu_dai->driver->suspend(cpu_dai);
817 /* deactivate pins to sleep state */
818 pinctrl_pm_select_sleep_state(cpu_dai->dev);
821 if (card->suspend_post)
822 card->suspend_post(card);
824 return 0;
826 EXPORT_SYMBOL_GPL(snd_soc_suspend);
828 /* deferred resume work, so resume can complete before we finished
829 * setting our codec back up, which can be very slow on I2C
831 static void soc_resume_deferred(struct work_struct *work)
833 struct snd_soc_card *card =
834 container_of(work, struct snd_soc_card, deferred_resume_work);
835 struct snd_soc_pcm_runtime *rtd;
836 struct snd_soc_component *component;
837 int i;
839 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
840 * so userspace apps are blocked from touching us
843 dev_dbg(card->dev, "ASoC: starting resume work\n");
845 /* Bring us up into D2 so that DAPM starts enabling things */
846 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
848 if (card->resume_pre)
849 card->resume_pre(card);
851 /* resume control bus DAIs */
852 list_for_each_entry(rtd, &card->rtd_list, list) {
853 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
855 if (rtd->dai_link->ignore_suspend)
856 continue;
858 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
859 cpu_dai->driver->resume(cpu_dai);
862 list_for_each_entry(component, &card->component_dev_list, card_list) {
863 if (component->suspended) {
864 if (component->resume)
865 component->resume(component);
866 component->suspended = 0;
870 list_for_each_entry(rtd, &card->rtd_list, list) {
872 if (rtd->dai_link->ignore_suspend)
873 continue;
875 snd_soc_dapm_stream_event(rtd,
876 SNDRV_PCM_STREAM_PLAYBACK,
877 SND_SOC_DAPM_STREAM_RESUME);
879 snd_soc_dapm_stream_event(rtd,
880 SNDRV_PCM_STREAM_CAPTURE,
881 SND_SOC_DAPM_STREAM_RESUME);
884 /* unmute any active DACs */
885 list_for_each_entry(rtd, &card->rtd_list, list) {
887 if (rtd->dai_link->ignore_suspend)
888 continue;
890 for (i = 0; i < rtd->num_codecs; i++) {
891 struct snd_soc_dai *dai = rtd->codec_dais[i];
892 struct snd_soc_dai_driver *drv = dai->driver;
894 if (drv->ops->digital_mute && dai->playback_active)
895 drv->ops->digital_mute(dai, 0);
899 list_for_each_entry(rtd, &card->rtd_list, list) {
900 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
902 if (rtd->dai_link->ignore_suspend)
903 continue;
905 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
906 cpu_dai->driver->resume(cpu_dai);
909 if (card->resume_post)
910 card->resume_post(card);
912 dev_dbg(card->dev, "ASoC: resume work completed\n");
914 /* Recheck all endpoints too, their state is affected by suspend */
915 dapm_mark_endpoints_dirty(card);
916 snd_soc_dapm_sync(&card->dapm);
918 /* userspace can access us now we are back as we were before */
919 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
922 /* powers up audio subsystem after a suspend */
923 int snd_soc_resume(struct device *dev)
925 struct snd_soc_card *card = dev_get_drvdata(dev);
926 bool bus_control = false;
927 struct snd_soc_pcm_runtime *rtd;
929 /* If the card is not initialized yet there is nothing to do */
930 if (!card->instantiated)
931 return 0;
933 /* activate pins from sleep state */
934 list_for_each_entry(rtd, &card->rtd_list, list) {
935 struct snd_soc_dai **codec_dais = rtd->codec_dais;
936 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
937 int j;
939 if (cpu_dai->active)
940 pinctrl_pm_select_default_state(cpu_dai->dev);
942 for (j = 0; j < rtd->num_codecs; j++) {
943 struct snd_soc_dai *codec_dai = codec_dais[j];
944 if (codec_dai->active)
945 pinctrl_pm_select_default_state(codec_dai->dev);
950 * DAIs that also act as the control bus master might have other drivers
951 * hanging off them so need to resume immediately. Other drivers don't
952 * have that problem and may take a substantial amount of time to resume
953 * due to I/O costs and anti-pop so handle them out of line.
955 list_for_each_entry(rtd, &card->rtd_list, list) {
956 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
957 bus_control |= cpu_dai->driver->bus_control;
959 if (bus_control) {
960 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
961 soc_resume_deferred(&card->deferred_resume_work);
962 } else {
963 dev_dbg(dev, "ASoC: Scheduling resume work\n");
964 if (!schedule_work(&card->deferred_resume_work))
965 dev_err(dev, "ASoC: resume work item may be lost\n");
968 return 0;
970 EXPORT_SYMBOL_GPL(snd_soc_resume);
971 #else
972 #define snd_soc_suspend NULL
973 #define snd_soc_resume NULL
974 #endif
976 static const struct snd_soc_dai_ops null_dai_ops = {
979 static struct snd_soc_component *soc_find_component(
980 const struct device_node *of_node, const char *name)
982 struct snd_soc_component *component;
984 lockdep_assert_held(&client_mutex);
986 list_for_each_entry(component, &component_list, list) {
987 if (of_node) {
988 if (component->dev->of_node == of_node)
989 return component;
990 } else if (strcmp(component->name, name) == 0) {
991 return component;
995 return NULL;
999 * snd_soc_find_dai - Find a registered DAI
1001 * @dlc: name of the DAI or the DAI driver and optional component info to match
1003 * This function will search all registered components and their DAIs to
1004 * find the DAI of the same name. The component's of_node and name
1005 * should also match if being specified.
1007 * Return: pointer of DAI, or NULL if not found.
1009 struct snd_soc_dai *snd_soc_find_dai(
1010 const struct snd_soc_dai_link_component *dlc)
1012 struct snd_soc_component *component;
1013 struct snd_soc_dai *dai;
1014 struct device_node *component_of_node;
1016 lockdep_assert_held(&client_mutex);
1018 /* Find CPU DAI from registered DAIs*/
1019 list_for_each_entry(component, &component_list, list) {
1020 component_of_node = component->dev->of_node;
1021 if (!component_of_node && component->dev->parent)
1022 component_of_node = component->dev->parent->of_node;
1024 if (dlc->of_node && component_of_node != dlc->of_node)
1025 continue;
1026 if (dlc->name && strcmp(component->name, dlc->name))
1027 continue;
1028 list_for_each_entry(dai, &component->dai_list, list) {
1029 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
1030 && (!dai->driver->name
1031 || strcmp(dai->driver->name, dlc->dai_name)))
1032 continue;
1034 return dai;
1038 return NULL;
1040 EXPORT_SYMBOL_GPL(snd_soc_find_dai);
1044 * snd_soc_find_dai_link - Find a DAI link
1046 * @card: soc card
1047 * @id: DAI link ID to match
1048 * @name: DAI link name to match, optional
1049 * @stream_name: DAI link stream name to match, optional
1051 * This function will search all existing DAI links of the soc card to
1052 * find the link of the same ID. Since DAI links may not have their
1053 * unique ID, so name and stream name should also match if being
1054 * specified.
1056 * Return: pointer of DAI link, or NULL if not found.
1058 struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
1059 int id, const char *name,
1060 const char *stream_name)
1062 struct snd_soc_dai_link *link, *_link;
1064 lockdep_assert_held(&client_mutex);
1066 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1067 if (link->id != id)
1068 continue;
1070 if (name && (!link->name || strcmp(name, link->name)))
1071 continue;
1073 if (stream_name && (!link->stream_name
1074 || strcmp(stream_name, link->stream_name)))
1075 continue;
1077 return link;
1080 return NULL;
1082 EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
1084 static bool soc_is_dai_link_bound(struct snd_soc_card *card,
1085 struct snd_soc_dai_link *dai_link)
1087 struct snd_soc_pcm_runtime *rtd;
1089 list_for_each_entry(rtd, &card->rtd_list, list) {
1090 if (rtd->dai_link == dai_link)
1091 return true;
1094 return false;
1097 static int soc_bind_dai_link(struct snd_soc_card *card,
1098 struct snd_soc_dai_link *dai_link)
1100 struct snd_soc_pcm_runtime *rtd;
1101 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
1102 struct snd_soc_dai_link_component cpu_dai_component;
1103 struct snd_soc_component *component;
1104 struct snd_soc_dai **codec_dais;
1105 struct snd_soc_platform *platform;
1106 struct device_node *platform_of_node;
1107 const char *platform_name;
1108 int i;
1110 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
1112 if (soc_is_dai_link_bound(card, dai_link)) {
1113 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
1114 dai_link->name);
1115 return 0;
1118 rtd = soc_new_pcm_runtime(card, dai_link);
1119 if (!rtd)
1120 return -ENOMEM;
1122 cpu_dai_component.name = dai_link->cpu_name;
1123 cpu_dai_component.of_node = dai_link->cpu_of_node;
1124 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
1125 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
1126 if (!rtd->cpu_dai) {
1127 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
1128 dai_link->cpu_dai_name);
1129 goto _err_defer;
1131 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
1133 rtd->num_codecs = dai_link->num_codecs;
1135 /* Find CODEC from registered CODECs */
1136 codec_dais = rtd->codec_dais;
1137 for (i = 0; i < rtd->num_codecs; i++) {
1138 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
1139 if (!codec_dais[i]) {
1140 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
1141 codecs[i].dai_name);
1142 goto _err_defer;
1144 snd_soc_rtdcom_add(rtd, codec_dais[i]->component);
1147 /* Single codec links expect codec and codec_dai in runtime data */
1148 rtd->codec_dai = codec_dais[0];
1149 rtd->codec = rtd->codec_dai->codec;
1151 /* if there's no platform we match on the empty platform */
1152 platform_name = dai_link->platform_name;
1153 if (!platform_name && !dai_link->platform_of_node)
1154 platform_name = "snd-soc-dummy";
1156 /* find one from the set of registered platforms */
1157 list_for_each_entry(component, &component_list, list) {
1158 platform_of_node = component->dev->of_node;
1159 if (!platform_of_node && component->dev->parent->of_node)
1160 platform_of_node = component->dev->parent->of_node;
1162 if (dai_link->platform_of_node) {
1163 if (platform_of_node != dai_link->platform_of_node)
1164 continue;
1165 } else {
1166 if (strcmp(component->name, platform_name))
1167 continue;
1170 snd_soc_rtdcom_add(rtd, component);
1173 /* find one from the set of registered platforms */
1174 list_for_each_entry(platform, &platform_list, list) {
1175 platform_of_node = platform->dev->of_node;
1176 if (!platform_of_node && platform->dev->parent->of_node)
1177 platform_of_node = platform->dev->parent->of_node;
1179 if (dai_link->platform_of_node) {
1180 if (platform_of_node != dai_link->platform_of_node)
1181 continue;
1182 } else {
1183 if (strcmp(platform->component.name, platform_name))
1184 continue;
1187 rtd->platform = platform;
1189 if (!rtd->platform) {
1190 dev_err(card->dev, "ASoC: platform %s not registered\n",
1191 dai_link->platform_name);
1192 goto _err_defer;
1195 soc_add_pcm_runtime(card, rtd);
1196 return 0;
1198 _err_defer:
1199 soc_free_pcm_runtime(rtd);
1200 return -EPROBE_DEFER;
1203 static void soc_remove_component(struct snd_soc_component *component)
1205 if (!component->card)
1206 return;
1208 list_del(&component->card_list);
1210 if (component->remove)
1211 component->remove(component);
1213 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1215 soc_cleanup_component_debugfs(component);
1216 component->card = NULL;
1217 module_put(component->dev->driver->owner);
1220 static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1222 int err;
1224 if (dai && dai->probed &&
1225 dai->driver->remove_order == order) {
1226 if (dai->driver->remove) {
1227 err = dai->driver->remove(dai);
1228 if (err < 0)
1229 dev_err(dai->dev,
1230 "ASoC: failed to remove %s: %d\n",
1231 dai->name, err);
1233 dai->probed = 0;
1237 static void soc_remove_link_dais(struct snd_soc_card *card,
1238 struct snd_soc_pcm_runtime *rtd, int order)
1240 int i;
1242 /* unregister the rtd device */
1243 if (rtd->dev_registered) {
1244 device_unregister(rtd->dev);
1245 rtd->dev_registered = 0;
1248 /* remove the CODEC DAI */
1249 for (i = 0; i < rtd->num_codecs; i++)
1250 soc_remove_dai(rtd->codec_dais[i], order);
1252 soc_remove_dai(rtd->cpu_dai, order);
1255 static void soc_remove_link_components(struct snd_soc_card *card,
1256 struct snd_soc_pcm_runtime *rtd, int order)
1258 struct snd_soc_component *component;
1259 struct snd_soc_rtdcom_list *rtdcom;
1261 for_each_rtdcom(rtd, rtdcom) {
1262 component = rtdcom->component;
1264 if (component->driver->remove_order == order)
1265 soc_remove_component(component);
1269 static void soc_remove_dai_links(struct snd_soc_card *card)
1271 int order;
1272 struct snd_soc_pcm_runtime *rtd;
1273 struct snd_soc_dai_link *link, *_link;
1275 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1276 order++) {
1277 list_for_each_entry(rtd, &card->rtd_list, list)
1278 soc_remove_link_dais(card, rtd, order);
1281 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1282 order++) {
1283 list_for_each_entry(rtd, &card->rtd_list, list)
1284 soc_remove_link_components(card, rtd, order);
1287 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1288 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1289 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1290 link->name);
1292 list_del(&link->list);
1293 card->num_dai_links--;
1297 static int snd_soc_init_multicodec(struct snd_soc_card *card,
1298 struct snd_soc_dai_link *dai_link)
1300 /* Legacy codec/codec_dai link is a single entry in multicodec */
1301 if (dai_link->codec_name || dai_link->codec_of_node ||
1302 dai_link->codec_dai_name) {
1303 dai_link->num_codecs = 1;
1305 dai_link->codecs = devm_kzalloc(card->dev,
1306 sizeof(struct snd_soc_dai_link_component),
1307 GFP_KERNEL);
1308 if (!dai_link->codecs)
1309 return -ENOMEM;
1311 dai_link->codecs[0].name = dai_link->codec_name;
1312 dai_link->codecs[0].of_node = dai_link->codec_of_node;
1313 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1316 if (!dai_link->codecs) {
1317 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1318 return -EINVAL;
1321 return 0;
1324 static int soc_init_dai_link(struct snd_soc_card *card,
1325 struct snd_soc_dai_link *link)
1327 int i, ret;
1329 ret = snd_soc_init_multicodec(card, link);
1330 if (ret) {
1331 dev_err(card->dev, "ASoC: failed to init multicodec\n");
1332 return ret;
1335 for (i = 0; i < link->num_codecs; i++) {
1337 * Codec must be specified by 1 of name or OF node,
1338 * not both or neither.
1340 if (!!link->codecs[i].name ==
1341 !!link->codecs[i].of_node) {
1342 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1343 link->name);
1344 return -EINVAL;
1346 /* Codec DAI name must be specified */
1347 if (!link->codecs[i].dai_name) {
1348 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1349 link->name);
1350 return -EINVAL;
1355 * Platform may be specified by either name or OF node, but
1356 * can be left unspecified, and a dummy platform will be used.
1358 if (link->platform_name && link->platform_of_node) {
1359 dev_err(card->dev,
1360 "ASoC: Both platform name/of_node are set for %s\n",
1361 link->name);
1362 return -EINVAL;
1366 * CPU device may be specified by either name or OF node, but
1367 * can be left unspecified, and will be matched based on DAI
1368 * name alone..
1370 if (link->cpu_name && link->cpu_of_node) {
1371 dev_err(card->dev,
1372 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1373 link->name);
1374 return -EINVAL;
1377 * At least one of CPU DAI name or CPU device name/node must be
1378 * specified
1380 if (!link->cpu_dai_name &&
1381 !(link->cpu_name || link->cpu_of_node)) {
1382 dev_err(card->dev,
1383 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1384 link->name);
1385 return -EINVAL;
1388 return 0;
1392 * snd_soc_add_dai_link - Add a DAI link dynamically
1393 * @card: The ASoC card to which the DAI link is added
1394 * @dai_link: The new DAI link to add
1396 * This function adds a DAI link to the ASoC card's link list.
1398 * Note: Topology can use this API to add DAI links when probing the
1399 * topology component. And machine drivers can still define static
1400 * DAI links in dai_link array.
1402 int snd_soc_add_dai_link(struct snd_soc_card *card,
1403 struct snd_soc_dai_link *dai_link)
1405 if (dai_link->dobj.type
1406 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1407 dev_err(card->dev, "Invalid dai link type %d\n",
1408 dai_link->dobj.type);
1409 return -EINVAL;
1412 lockdep_assert_held(&client_mutex);
1413 /* Notify the machine driver for extra initialization
1414 * on the link created by topology.
1416 if (dai_link->dobj.type && card->add_dai_link)
1417 card->add_dai_link(card, dai_link);
1419 list_add_tail(&dai_link->list, &card->dai_link_list);
1420 card->num_dai_links++;
1422 return 0;
1424 EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1427 * snd_soc_remove_dai_link - Remove a DAI link from the list
1428 * @card: The ASoC card that owns the link
1429 * @dai_link: The DAI link to remove
1431 * This function removes a DAI link from the ASoC card's link list.
1433 * For DAI links previously added by topology, topology should
1434 * remove them by using the dobj embedded in the link.
1436 void snd_soc_remove_dai_link(struct snd_soc_card *card,
1437 struct snd_soc_dai_link *dai_link)
1439 struct snd_soc_dai_link *link, *_link;
1441 if (dai_link->dobj.type
1442 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1443 dev_err(card->dev, "Invalid dai link type %d\n",
1444 dai_link->dobj.type);
1445 return;
1448 lockdep_assert_held(&client_mutex);
1449 /* Notify the machine driver for extra destruction
1450 * on the link created by topology.
1452 if (dai_link->dobj.type && card->remove_dai_link)
1453 card->remove_dai_link(card, dai_link);
1455 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1456 if (link == dai_link) {
1457 list_del(&link->list);
1458 card->num_dai_links--;
1459 return;
1463 EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1465 static void soc_set_name_prefix(struct snd_soc_card *card,
1466 struct snd_soc_component *component)
1468 int i;
1470 if (card->codec_conf == NULL)
1471 return;
1473 for (i = 0; i < card->num_configs; i++) {
1474 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1475 if (map->of_node && component->dev->of_node != map->of_node)
1476 continue;
1477 if (map->dev_name && strcmp(component->name, map->dev_name))
1478 continue;
1479 component->name_prefix = map->name_prefix;
1480 break;
1484 static int soc_probe_component(struct snd_soc_card *card,
1485 struct snd_soc_component *component)
1487 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1488 struct snd_soc_dai *dai;
1489 int ret;
1491 if (!strcmp(component->name, "snd-soc-dummy"))
1492 return 0;
1494 if (component->card) {
1495 if (component->card != card) {
1496 dev_err(component->dev,
1497 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1498 card->name, component->card->name);
1499 return -ENODEV;
1501 return 0;
1504 if (!try_module_get(component->dev->driver->owner))
1505 return -ENODEV;
1507 component->card = card;
1508 dapm->card = card;
1509 soc_set_name_prefix(card, component);
1511 soc_init_component_debugfs(component);
1513 if (component->driver->dapm_widgets) {
1514 ret = snd_soc_dapm_new_controls(dapm,
1515 component->driver->dapm_widgets,
1516 component->driver->num_dapm_widgets);
1518 if (ret != 0) {
1519 dev_err(component->dev,
1520 "Failed to create new controls %d\n", ret);
1521 goto err_probe;
1525 list_for_each_entry(dai, &component->dai_list, list) {
1526 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1527 if (ret != 0) {
1528 dev_err(component->dev,
1529 "Failed to create DAI widgets %d\n", ret);
1530 goto err_probe;
1534 if (component->probe) {
1535 ret = component->probe(component);
1536 if (ret < 0) {
1537 dev_err(component->dev,
1538 "ASoC: failed to probe component %d\n", ret);
1539 goto err_probe;
1542 WARN(dapm->idle_bias_off &&
1543 dapm->bias_level != SND_SOC_BIAS_OFF,
1544 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1545 component->name);
1548 /* machine specific init */
1549 if (component->init) {
1550 ret = component->init(component);
1551 if (ret < 0) {
1552 dev_err(component->dev,
1553 "Failed to do machine specific init %d\n", ret);
1554 goto err_probe;
1558 if (component->driver->controls)
1559 snd_soc_add_component_controls(component,
1560 component->driver->controls,
1561 component->driver->num_controls);
1562 if (component->driver->dapm_routes)
1563 snd_soc_dapm_add_routes(dapm,
1564 component->driver->dapm_routes,
1565 component->driver->num_dapm_routes);
1567 list_add(&dapm->list, &card->dapm_list);
1568 list_add(&component->card_list, &card->component_dev_list);
1570 return 0;
1572 err_probe:
1573 soc_cleanup_component_debugfs(component);
1574 component->card = NULL;
1575 module_put(component->dev->driver->owner);
1577 return ret;
1580 static void rtd_release(struct device *dev)
1582 kfree(dev);
1585 static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1586 const char *name)
1588 int ret = 0;
1590 /* register the rtd device */
1591 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1592 if (!rtd->dev)
1593 return -ENOMEM;
1594 device_initialize(rtd->dev);
1595 rtd->dev->parent = rtd->card->dev;
1596 rtd->dev->release = rtd_release;
1597 rtd->dev->groups = soc_dev_attr_groups;
1598 dev_set_name(rtd->dev, "%s", name);
1599 dev_set_drvdata(rtd->dev, rtd);
1600 mutex_init(&rtd->pcm_mutex);
1601 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1602 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1603 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1604 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1605 ret = device_add(rtd->dev);
1606 if (ret < 0) {
1607 /* calling put_device() here to free the rtd->dev */
1608 put_device(rtd->dev);
1609 dev_err(rtd->card->dev,
1610 "ASoC: failed to register runtime device: %d\n", ret);
1611 return ret;
1613 rtd->dev_registered = 1;
1614 return 0;
1617 static int soc_probe_link_components(struct snd_soc_card *card,
1618 struct snd_soc_pcm_runtime *rtd,
1619 int order)
1621 struct snd_soc_component *component;
1622 struct snd_soc_rtdcom_list *rtdcom;
1623 int ret;
1625 for_each_rtdcom(rtd, rtdcom) {
1626 component = rtdcom->component;
1628 if (component->driver->probe_order == order) {
1629 ret = soc_probe_component(card, component);
1630 if (ret < 0)
1631 return ret;
1635 return 0;
1638 static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1640 int ret;
1642 if (!dai->probed && dai->driver->probe_order == order) {
1643 if (dai->driver->probe) {
1644 ret = dai->driver->probe(dai);
1645 if (ret < 0) {
1646 dev_err(dai->dev,
1647 "ASoC: failed to probe DAI %s: %d\n",
1648 dai->name, ret);
1649 return ret;
1653 dai->probed = 1;
1656 return 0;
1659 static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1660 struct snd_soc_pcm_runtime *rtd)
1662 int i, ret = 0;
1664 for (i = 0; i < num_dais; ++i) {
1665 struct snd_soc_dai_driver *drv = dais[i]->driver;
1667 if (!rtd->dai_link->no_pcm && drv->pcm_new)
1668 ret = drv->pcm_new(rtd, dais[i]);
1669 if (ret < 0) {
1670 dev_err(dais[i]->dev,
1671 "ASoC: Failed to bind %s with pcm device\n",
1672 dais[i]->name);
1673 return ret;
1677 return 0;
1680 static int soc_link_dai_widgets(struct snd_soc_card *card,
1681 struct snd_soc_dai_link *dai_link,
1682 struct snd_soc_pcm_runtime *rtd)
1684 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1685 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1686 struct snd_soc_dapm_widget *sink, *source;
1687 int ret;
1689 if (rtd->num_codecs > 1)
1690 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1692 /* link the DAI widgets */
1693 sink = codec_dai->playback_widget;
1694 source = cpu_dai->capture_widget;
1695 if (sink && source) {
1696 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1697 dai_link->num_params,
1698 source, sink);
1699 if (ret != 0) {
1700 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1701 sink->name, source->name, ret);
1702 return ret;
1706 sink = cpu_dai->playback_widget;
1707 source = codec_dai->capture_widget;
1708 if (sink && source) {
1709 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1710 dai_link->num_params,
1711 source, sink);
1712 if (ret != 0) {
1713 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1714 sink->name, source->name, ret);
1715 return ret;
1719 return 0;
1722 static int soc_probe_link_dais(struct snd_soc_card *card,
1723 struct snd_soc_pcm_runtime *rtd, int order)
1725 struct snd_soc_dai_link *dai_link = rtd->dai_link;
1726 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1727 int i, ret;
1729 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1730 card->name, rtd->num, order);
1732 /* set default power off timeout */
1733 rtd->pmdown_time = pmdown_time;
1735 ret = soc_probe_dai(cpu_dai, order);
1736 if (ret)
1737 return ret;
1739 /* probe the CODEC DAI */
1740 for (i = 0; i < rtd->num_codecs; i++) {
1741 ret = soc_probe_dai(rtd->codec_dais[i], order);
1742 if (ret)
1743 return ret;
1746 /* complete DAI probe during last probe */
1747 if (order != SND_SOC_COMP_ORDER_LAST)
1748 return 0;
1750 /* do machine specific initialization */
1751 if (dai_link->init) {
1752 ret = dai_link->init(rtd);
1753 if (ret < 0) {
1754 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1755 dai_link->name, ret);
1756 return ret;
1760 if (dai_link->dai_fmt)
1761 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1763 ret = soc_post_component_init(rtd, dai_link->name);
1764 if (ret)
1765 return ret;
1767 #ifdef CONFIG_DEBUG_FS
1768 /* add DPCM sysfs entries */
1769 if (dai_link->dynamic)
1770 soc_dpcm_debugfs_add(rtd);
1771 #endif
1773 if (cpu_dai->driver->compress_new) {
1774 /*create compress_device"*/
1775 ret = cpu_dai->driver->compress_new(rtd, rtd->num);
1776 if (ret < 0) {
1777 dev_err(card->dev, "ASoC: can't create compress %s\n",
1778 dai_link->stream_name);
1779 return ret;
1781 } else {
1783 if (!dai_link->params) {
1784 /* create the pcm */
1785 ret = soc_new_pcm(rtd, rtd->num);
1786 if (ret < 0) {
1787 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1788 dai_link->stream_name, ret);
1789 return ret;
1791 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1792 if (ret < 0)
1793 return ret;
1794 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1795 rtd->num_codecs, rtd);
1796 if (ret < 0)
1797 return ret;
1798 } else {
1799 INIT_DELAYED_WORK(&rtd->delayed_work,
1800 codec2codec_close_delayed_work);
1802 /* link the DAI widgets */
1803 ret = soc_link_dai_widgets(card, dai_link, rtd);
1804 if (ret)
1805 return ret;
1809 return 0;
1812 static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1814 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1815 struct snd_soc_component *component;
1816 const char *name;
1817 struct device_node *codec_of_node;
1819 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1820 /* codecs, usually analog devices */
1821 name = aux_dev->codec_name;
1822 codec_of_node = aux_dev->codec_of_node;
1823 component = soc_find_component(codec_of_node, name);
1824 if (!component) {
1825 if (codec_of_node)
1826 name = of_node_full_name(codec_of_node);
1827 goto err_defer;
1829 } else if (aux_dev->name) {
1830 /* generic components */
1831 name = aux_dev->name;
1832 component = soc_find_component(NULL, name);
1833 if (!component)
1834 goto err_defer;
1835 } else {
1836 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1837 return -EINVAL;
1840 component->init = aux_dev->init;
1841 list_add(&component->card_aux_list, &card->aux_comp_list);
1843 return 0;
1845 err_defer:
1846 dev_err(card->dev, "ASoC: %s not registered\n", name);
1847 return -EPROBE_DEFER;
1850 static int soc_probe_aux_devices(struct snd_soc_card *card)
1852 struct snd_soc_component *comp;
1853 int order;
1854 int ret;
1856 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1857 order++) {
1858 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
1859 if (comp->driver->probe_order == order) {
1860 ret = soc_probe_component(card, comp);
1861 if (ret < 0) {
1862 dev_err(card->dev,
1863 "ASoC: failed to probe aux component %s %d\n",
1864 comp->name, ret);
1865 return ret;
1871 return 0;
1874 static void soc_remove_aux_devices(struct snd_soc_card *card)
1876 struct snd_soc_component *comp, *_comp;
1877 int order;
1879 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1880 order++) {
1881 list_for_each_entry_safe(comp, _comp,
1882 &card->aux_comp_list, card_aux_list) {
1884 if (comp->driver->remove_order == order) {
1885 soc_remove_component(comp);
1886 /* remove it from the card's aux_comp_list */
1887 list_del(&comp->card_aux_list);
1893 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1895 int ret;
1897 if (codec->cache_init)
1898 return 0;
1900 ret = snd_soc_cache_init(codec);
1901 if (ret < 0) {
1902 dev_err(codec->dev,
1903 "ASoC: Failed to set cache compression type: %d\n",
1904 ret);
1905 return ret;
1907 codec->cache_init = 1;
1908 return 0;
1912 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1913 * @rtd: The runtime for which the DAI link format should be changed
1914 * @dai_fmt: The new DAI link format
1916 * This function updates the DAI link format for all DAIs connected to the DAI
1917 * link for the specified runtime.
1919 * Note: For setups with a static format set the dai_fmt field in the
1920 * corresponding snd_dai_link struct instead of using this function.
1922 * Returns 0 on success, otherwise a negative error code.
1924 int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1925 unsigned int dai_fmt)
1927 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1928 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1929 unsigned int i;
1930 int ret;
1932 for (i = 0; i < rtd->num_codecs; i++) {
1933 struct snd_soc_dai *codec_dai = codec_dais[i];
1935 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1936 if (ret != 0 && ret != -ENOTSUPP) {
1937 dev_warn(codec_dai->dev,
1938 "ASoC: Failed to set DAI format: %d\n", ret);
1939 return ret;
1943 /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1944 if (cpu_dai->codec) {
1945 unsigned int inv_dai_fmt;
1947 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1948 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1949 case SND_SOC_DAIFMT_CBM_CFM:
1950 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1951 break;
1952 case SND_SOC_DAIFMT_CBM_CFS:
1953 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1954 break;
1955 case SND_SOC_DAIFMT_CBS_CFM:
1956 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1957 break;
1958 case SND_SOC_DAIFMT_CBS_CFS:
1959 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1960 break;
1963 dai_fmt = inv_dai_fmt;
1966 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1967 if (ret != 0 && ret != -ENOTSUPP) {
1968 dev_warn(cpu_dai->dev,
1969 "ASoC: Failed to set DAI format: %d\n", ret);
1970 return ret;
1973 return 0;
1975 EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1978 #ifdef CONFIG_DMI
1979 /* Trim special characters, and replace '-' with '_' since '-' is used to
1980 * separate different DMI fields in the card long name. Only number and
1981 * alphabet characters and a few separator characters are kept.
1983 static void cleanup_dmi_name(char *name)
1985 int i, j = 0;
1987 for (i = 0; name[i]; i++) {
1988 if (isalnum(name[i]) || (name[i] == '.')
1989 || (name[i] == '_'))
1990 name[j++] = name[i];
1991 else if (name[i] == '-')
1992 name[j++] = '_';
1995 name[j] = '\0';
1998 /* Check if a DMI field is valid, i.e. not containing any string
1999 * in the black list.
2001 static int is_dmi_valid(const char *field)
2003 int i = 0;
2005 while (dmi_blacklist[i]) {
2006 if (strstr(field, dmi_blacklist[i]))
2007 return 0;
2008 i++;
2011 return 1;
2015 * snd_soc_set_dmi_name() - Register DMI names to card
2016 * @card: The card to register DMI names
2017 * @flavour: The flavour "differentiator" for the card amongst its peers.
2019 * An Intel machine driver may be used by many different devices but are
2020 * difficult for userspace to differentiate, since machine drivers ususally
2021 * use their own name as the card short name and leave the card long name
2022 * blank. To differentiate such devices and fix bugs due to lack of
2023 * device-specific configurations, this function allows DMI info to be used
2024 * as the sound card long name, in the format of
2025 * "vendor-product-version-board"
2026 * (Character '-' is used to separate different DMI fields here).
2027 * This will help the user space to load the device-specific Use Case Manager
2028 * (UCM) configurations for the card.
2030 * Possible card long names may be:
2031 * DellInc.-XPS139343-01-0310JH
2032 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
2033 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
2035 * This function also supports flavoring the card longname to provide
2036 * the extra differentiation, like "vendor-product-version-board-flavor".
2038 * We only keep number and alphabet characters and a few separator characters
2039 * in the card long name since UCM in the user space uses the card long names
2040 * as card configuration directory names and AudoConf cannot support special
2041 * charactors like SPACE.
2043 * Returns 0 on success, otherwise a negative error code.
2045 int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
2047 const char *vendor, *product, *product_version, *board;
2048 size_t longname_buf_size = sizeof(card->snd_card->longname);
2049 size_t len;
2051 if (card->long_name)
2052 return 0; /* long name already set by driver or from DMI */
2054 /* make up dmi long name as: vendor.product.version.board */
2055 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2056 if (!vendor || !is_dmi_valid(vendor)) {
2057 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
2058 return 0;
2062 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
2063 "%s", vendor);
2064 cleanup_dmi_name(card->dmi_longname);
2066 product = dmi_get_system_info(DMI_PRODUCT_NAME);
2067 if (product && is_dmi_valid(product)) {
2068 len = strlen(card->dmi_longname);
2069 snprintf(card->dmi_longname + len,
2070 longname_buf_size - len,
2071 "-%s", product);
2073 len++; /* skip the separator "-" */
2074 if (len < longname_buf_size)
2075 cleanup_dmi_name(card->dmi_longname + len);
2077 /* some vendors like Lenovo may only put a self-explanatory
2078 * name in the product version field
2080 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
2081 if (product_version && is_dmi_valid(product_version)) {
2082 len = strlen(card->dmi_longname);
2083 snprintf(card->dmi_longname + len,
2084 longname_buf_size - len,
2085 "-%s", product_version);
2087 len++;
2088 if (len < longname_buf_size)
2089 cleanup_dmi_name(card->dmi_longname + len);
2093 board = dmi_get_system_info(DMI_BOARD_NAME);
2094 if (board && is_dmi_valid(board)) {
2095 len = strlen(card->dmi_longname);
2096 snprintf(card->dmi_longname + len,
2097 longname_buf_size - len,
2098 "-%s", board);
2100 len++;
2101 if (len < longname_buf_size)
2102 cleanup_dmi_name(card->dmi_longname + len);
2103 } else if (!product) {
2104 /* fall back to using legacy name */
2105 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
2106 return 0;
2109 /* Add flavour to dmi long name */
2110 if (flavour) {
2111 len = strlen(card->dmi_longname);
2112 snprintf(card->dmi_longname + len,
2113 longname_buf_size - len,
2114 "-%s", flavour);
2116 len++;
2117 if (len < longname_buf_size)
2118 cleanup_dmi_name(card->dmi_longname + len);
2121 /* set the card long name */
2122 card->long_name = card->dmi_longname;
2124 return 0;
2126 EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
2127 #endif /* CONFIG_DMI */
2129 static int snd_soc_instantiate_card(struct snd_soc_card *card)
2131 struct snd_soc_codec *codec;
2132 struct snd_soc_pcm_runtime *rtd;
2133 struct snd_soc_dai_link *dai_link;
2134 int ret, i, order;
2136 mutex_lock(&client_mutex);
2137 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
2139 /* bind DAIs */
2140 for (i = 0; i < card->num_links; i++) {
2141 ret = soc_bind_dai_link(card, &card->dai_link[i]);
2142 if (ret != 0)
2143 goto base_error;
2146 /* bind aux_devs too */
2147 for (i = 0; i < card->num_aux_devs; i++) {
2148 ret = soc_bind_aux_dev(card, i);
2149 if (ret != 0)
2150 goto base_error;
2153 /* add predefined DAI links to the list */
2154 for (i = 0; i < card->num_links; i++)
2155 snd_soc_add_dai_link(card, card->dai_link+i);
2157 /* initialize the register cache for each available codec */
2158 list_for_each_entry(codec, &codec_list, list) {
2159 if (codec->cache_init)
2160 continue;
2161 ret = snd_soc_init_codec_cache(codec);
2162 if (ret < 0)
2163 goto base_error;
2166 /* card bind complete so register a sound card */
2167 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2168 card->owner, 0, &card->snd_card);
2169 if (ret < 0) {
2170 dev_err(card->dev,
2171 "ASoC: can't create sound card for card %s: %d\n",
2172 card->name, ret);
2173 goto base_error;
2176 soc_init_card_debugfs(card);
2178 card->dapm.bias_level = SND_SOC_BIAS_OFF;
2179 card->dapm.dev = card->dev;
2180 card->dapm.card = card;
2181 list_add(&card->dapm.list, &card->dapm_list);
2183 #ifdef CONFIG_DEBUG_FS
2184 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2185 #endif
2187 #ifdef CONFIG_PM_SLEEP
2188 /* deferred resume work */
2189 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
2190 #endif
2192 if (card->dapm_widgets)
2193 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2194 card->num_dapm_widgets);
2196 if (card->of_dapm_widgets)
2197 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2198 card->num_of_dapm_widgets);
2200 /* initialise the sound card only once */
2201 if (card->probe) {
2202 ret = card->probe(card);
2203 if (ret < 0)
2204 goto card_probe_error;
2207 /* probe all components used by DAI links on this card */
2208 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2209 order++) {
2210 list_for_each_entry(rtd, &card->rtd_list, list) {
2211 ret = soc_probe_link_components(card, rtd, order);
2212 if (ret < 0) {
2213 dev_err(card->dev,
2214 "ASoC: failed to instantiate card %d\n",
2215 ret);
2216 goto probe_dai_err;
2221 /* probe auxiliary components */
2222 ret = soc_probe_aux_devices(card);
2223 if (ret < 0)
2224 goto probe_dai_err;
2226 /* Find new DAI links added during probing components and bind them.
2227 * Components with topology may bring new DAIs and DAI links.
2229 list_for_each_entry(dai_link, &card->dai_link_list, list) {
2230 if (soc_is_dai_link_bound(card, dai_link))
2231 continue;
2233 ret = soc_init_dai_link(card, dai_link);
2234 if (ret)
2235 goto probe_dai_err;
2236 ret = soc_bind_dai_link(card, dai_link);
2237 if (ret)
2238 goto probe_dai_err;
2241 /* probe all DAI links on this card */
2242 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2243 order++) {
2244 list_for_each_entry(rtd, &card->rtd_list, list) {
2245 ret = soc_probe_link_dais(card, rtd, order);
2246 if (ret < 0) {
2247 dev_err(card->dev,
2248 "ASoC: failed to instantiate card %d\n",
2249 ret);
2250 goto probe_dai_err;
2255 snd_soc_dapm_link_dai_widgets(card);
2256 snd_soc_dapm_connect_dai_link_widgets(card);
2258 if (card->controls)
2259 snd_soc_add_card_controls(card, card->controls, card->num_controls);
2261 if (card->dapm_routes)
2262 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2263 card->num_dapm_routes);
2265 if (card->of_dapm_routes)
2266 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2267 card->num_of_dapm_routes);
2269 /* try to set some sane longname if DMI is available */
2270 snd_soc_set_dmi_name(card, NULL);
2272 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
2273 "%s", card->name);
2274 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2275 "%s", card->long_name ? card->long_name : card->name);
2276 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2277 "%s", card->driver_name ? card->driver_name : card->name);
2278 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2279 switch (card->snd_card->driver[i]) {
2280 case '_':
2281 case '-':
2282 case '\0':
2283 break;
2284 default:
2285 if (!isalnum(card->snd_card->driver[i]))
2286 card->snd_card->driver[i] = '_';
2287 break;
2291 if (card->late_probe) {
2292 ret = card->late_probe(card);
2293 if (ret < 0) {
2294 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
2295 card->name, ret);
2296 goto probe_aux_dev_err;
2300 snd_soc_dapm_new_widgets(card);
2302 ret = snd_card_register(card->snd_card);
2303 if (ret < 0) {
2304 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2305 ret);
2306 goto probe_aux_dev_err;
2309 card->instantiated = 1;
2310 dapm_mark_endpoints_dirty(card);
2311 snd_soc_dapm_sync(&card->dapm);
2312 mutex_unlock(&card->mutex);
2313 mutex_unlock(&client_mutex);
2315 return 0;
2317 probe_aux_dev_err:
2318 soc_remove_aux_devices(card);
2320 probe_dai_err:
2321 soc_remove_dai_links(card);
2323 card_probe_error:
2324 if (card->remove)
2325 card->remove(card);
2327 snd_soc_dapm_free(&card->dapm);
2328 soc_cleanup_card_debugfs(card);
2329 snd_card_free(card->snd_card);
2331 base_error:
2332 soc_remove_pcm_runtimes(card);
2333 mutex_unlock(&card->mutex);
2334 mutex_unlock(&client_mutex);
2336 return ret;
2339 /* probes a new socdev */
2340 static int soc_probe(struct platform_device *pdev)
2342 struct snd_soc_card *card = platform_get_drvdata(pdev);
2345 * no card, so machine driver should be registering card
2346 * we should not be here in that case so ret error
2348 if (!card)
2349 return -EINVAL;
2351 dev_warn(&pdev->dev,
2352 "ASoC: machine %s should use snd_soc_register_card()\n",
2353 card->name);
2355 /* Bodge while we unpick instantiation */
2356 card->dev = &pdev->dev;
2358 return snd_soc_register_card(card);
2361 static int soc_cleanup_card_resources(struct snd_soc_card *card)
2363 struct snd_soc_pcm_runtime *rtd;
2365 /* make sure any delayed work runs */
2366 list_for_each_entry(rtd, &card->rtd_list, list)
2367 flush_delayed_work(&rtd->delayed_work);
2369 /* free the ALSA card at first; this syncs with pending operations */
2370 snd_card_free(card->snd_card);
2372 /* remove and free each DAI */
2373 soc_remove_dai_links(card);
2374 soc_remove_pcm_runtimes(card);
2376 /* remove auxiliary devices */
2377 soc_remove_aux_devices(card);
2379 snd_soc_dapm_free(&card->dapm);
2380 soc_cleanup_card_debugfs(card);
2382 /* remove the card */
2383 if (card->remove)
2384 card->remove(card);
2386 return 0;
2389 /* removes a socdev */
2390 static int soc_remove(struct platform_device *pdev)
2392 struct snd_soc_card *card = platform_get_drvdata(pdev);
2394 snd_soc_unregister_card(card);
2395 return 0;
2398 int snd_soc_poweroff(struct device *dev)
2400 struct snd_soc_card *card = dev_get_drvdata(dev);
2401 struct snd_soc_pcm_runtime *rtd;
2403 if (!card->instantiated)
2404 return 0;
2406 /* Flush out pmdown_time work - we actually do want to run it
2407 * now, we're shutting down so no imminent restart. */
2408 list_for_each_entry(rtd, &card->rtd_list, list)
2409 flush_delayed_work(&rtd->delayed_work);
2411 snd_soc_dapm_shutdown(card);
2413 /* deactivate pins to sleep state */
2414 list_for_each_entry(rtd, &card->rtd_list, list) {
2415 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2416 int i;
2418 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2419 for (i = 0; i < rtd->num_codecs; i++) {
2420 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
2421 pinctrl_pm_select_sleep_state(codec_dai->dev);
2425 return 0;
2427 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2429 const struct dev_pm_ops snd_soc_pm_ops = {
2430 .suspend = snd_soc_suspend,
2431 .resume = snd_soc_resume,
2432 .freeze = snd_soc_suspend,
2433 .thaw = snd_soc_resume,
2434 .poweroff = snd_soc_poweroff,
2435 .restore = snd_soc_resume,
2437 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2439 /* ASoC platform driver */
2440 static struct platform_driver soc_driver = {
2441 .driver = {
2442 .name = "soc-audio",
2443 .pm = &snd_soc_pm_ops,
2445 .probe = soc_probe,
2446 .remove = soc_remove,
2450 * snd_soc_cnew - create new control
2451 * @_template: control template
2452 * @data: control private data
2453 * @long_name: control long name
2454 * @prefix: control name prefix
2456 * Create a new mixer control from a template control.
2458 * Returns 0 for success, else error.
2460 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2461 void *data, const char *long_name,
2462 const char *prefix)
2464 struct snd_kcontrol_new template;
2465 struct snd_kcontrol *kcontrol;
2466 char *name = NULL;
2468 memcpy(&template, _template, sizeof(template));
2469 template.index = 0;
2471 if (!long_name)
2472 long_name = template.name;
2474 if (prefix) {
2475 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2476 if (!name)
2477 return NULL;
2479 template.name = name;
2480 } else {
2481 template.name = long_name;
2484 kcontrol = snd_ctl_new1(&template, data);
2486 kfree(name);
2488 return kcontrol;
2490 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2492 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2493 const struct snd_kcontrol_new *controls, int num_controls,
2494 const char *prefix, void *data)
2496 int err, i;
2498 for (i = 0; i < num_controls; i++) {
2499 const struct snd_kcontrol_new *control = &controls[i];
2500 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2501 control->name, prefix));
2502 if (err < 0) {
2503 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2504 control->name, err);
2505 return err;
2509 return 0;
2512 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2513 const char *name)
2515 struct snd_card *card = soc_card->snd_card;
2516 struct snd_kcontrol *kctl;
2518 if (unlikely(!name))
2519 return NULL;
2521 list_for_each_entry(kctl, &card->controls, list)
2522 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2523 return kctl;
2524 return NULL;
2526 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2529 * snd_soc_add_component_controls - Add an array of controls to a component.
2531 * @component: Component to add controls to
2532 * @controls: Array of controls to add
2533 * @num_controls: Number of elements in the array
2535 * Return: 0 for success, else error.
2537 int snd_soc_add_component_controls(struct snd_soc_component *component,
2538 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2540 struct snd_card *card = component->card->snd_card;
2542 return snd_soc_add_controls(card, component->dev, controls,
2543 num_controls, component->name_prefix, component);
2545 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2548 * snd_soc_add_codec_controls - add an array of controls to a codec.
2549 * Convenience function to add a list of controls. Many codecs were
2550 * duplicating this code.
2552 * @codec: codec to add controls to
2553 * @controls: array of controls to add
2554 * @num_controls: number of elements in the array
2556 * Return 0 for success, else error.
2558 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2559 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2561 return snd_soc_add_component_controls(&codec->component, controls,
2562 num_controls);
2564 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2567 * snd_soc_add_platform_controls - add an array of controls to a platform.
2568 * Convenience function to add a list of controls.
2570 * @platform: platform to add controls to
2571 * @controls: array of controls to add
2572 * @num_controls: number of elements in the array
2574 * Return 0 for success, else error.
2576 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2577 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2579 return snd_soc_add_component_controls(&platform->component, controls,
2580 num_controls);
2582 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2585 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2586 * Convenience function to add a list of controls.
2588 * @soc_card: SoC card to add controls to
2589 * @controls: array of controls to add
2590 * @num_controls: number of elements in the array
2592 * Return 0 for success, else error.
2594 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2595 const struct snd_kcontrol_new *controls, int num_controls)
2597 struct snd_card *card = soc_card->snd_card;
2599 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2600 NULL, soc_card);
2602 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2605 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2606 * Convienience function to add a list of controls.
2608 * @dai: DAI to add controls to
2609 * @controls: array of controls to add
2610 * @num_controls: number of elements in the array
2612 * Return 0 for success, else error.
2614 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2615 const struct snd_kcontrol_new *controls, int num_controls)
2617 struct snd_card *card = dai->component->card->snd_card;
2619 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2620 NULL, dai);
2622 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2625 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2626 * @dai: DAI
2627 * @clk_id: DAI specific clock ID
2628 * @freq: new clock frequency in Hz
2629 * @dir: new clock direction - input/output.
2631 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2633 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2634 unsigned int freq, int dir)
2636 if (dai->driver && dai->driver->ops->set_sysclk)
2637 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2639 return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2640 freq, dir);
2642 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2645 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2646 * @codec: CODEC
2647 * @clk_id: DAI specific clock ID
2648 * @source: Source for the clock
2649 * @freq: new clock frequency in Hz
2650 * @dir: new clock direction - input/output.
2652 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2654 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2655 int source, unsigned int freq, int dir)
2657 if (codec->driver->set_sysclk)
2658 return codec->driver->set_sysclk(codec, clk_id, source,
2659 freq, dir);
2660 else
2661 return -ENOTSUPP;
2663 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2666 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2667 * @component: COMPONENT
2668 * @clk_id: DAI specific clock ID
2669 * @source: Source for the clock
2670 * @freq: new clock frequency in Hz
2671 * @dir: new clock direction - input/output.
2673 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2675 int snd_soc_component_set_sysclk(struct snd_soc_component *component, int clk_id,
2676 int source, unsigned int freq, int dir)
2678 /* will be removed */
2679 if (component->set_sysclk)
2680 return component->set_sysclk(component, clk_id, source,
2681 freq, dir);
2683 if (component->driver->set_sysclk)
2684 return component->driver->set_sysclk(component, clk_id, source,
2685 freq, dir);
2687 return -ENOTSUPP;
2689 EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2692 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2693 * @dai: DAI
2694 * @div_id: DAI specific clock divider ID
2695 * @div: new clock divisor.
2697 * Configures the clock dividers. This is used to derive the best DAI bit and
2698 * frame clocks from the system or master clock. It's best to set the DAI bit
2699 * and frame clocks as low as possible to save system power.
2701 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2702 int div_id, int div)
2704 if (dai->driver && dai->driver->ops->set_clkdiv)
2705 return dai->driver->ops->set_clkdiv(dai, div_id, div);
2706 else
2707 return -EINVAL;
2709 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2712 * snd_soc_dai_set_pll - configure DAI PLL.
2713 * @dai: DAI
2714 * @pll_id: DAI specific PLL ID
2715 * @source: DAI specific source for the PLL
2716 * @freq_in: PLL input clock frequency in Hz
2717 * @freq_out: requested PLL output clock frequency in Hz
2719 * Configures and enables PLL to generate output clock based on input clock.
2721 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2722 unsigned int freq_in, unsigned int freq_out)
2724 if (dai->driver && dai->driver->ops->set_pll)
2725 return dai->driver->ops->set_pll(dai, pll_id, source,
2726 freq_in, freq_out);
2728 return snd_soc_component_set_pll(dai->component, pll_id, source,
2729 freq_in, freq_out);
2731 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2734 * snd_soc_codec_set_pll - configure codec PLL.
2735 * @codec: CODEC
2736 * @pll_id: DAI specific PLL ID
2737 * @source: DAI specific source for the PLL
2738 * @freq_in: PLL input clock frequency in Hz
2739 * @freq_out: requested PLL output clock frequency in Hz
2741 * Configures and enables PLL to generate output clock based on input clock.
2743 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2744 unsigned int freq_in, unsigned int freq_out)
2746 if (codec->driver->set_pll)
2747 return codec->driver->set_pll(codec, pll_id, source,
2748 freq_in, freq_out);
2749 else
2750 return -EINVAL;
2752 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2755 * snd_soc_component_set_pll - configure component PLL.
2756 * @component: COMPONENT
2757 * @pll_id: DAI specific PLL ID
2758 * @source: DAI specific source for the PLL
2759 * @freq_in: PLL input clock frequency in Hz
2760 * @freq_out: requested PLL output clock frequency in Hz
2762 * Configures and enables PLL to generate output clock based on input clock.
2764 int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2765 int source, unsigned int freq_in,
2766 unsigned int freq_out)
2768 /* will be removed */
2769 if (component->set_pll)
2770 return component->set_pll(component, pll_id, source,
2771 freq_in, freq_out);
2773 if (component->driver->set_pll)
2774 return component->driver->set_pll(component, pll_id, source,
2775 freq_in, freq_out);
2777 return -EINVAL;
2779 EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2782 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2783 * @dai: DAI
2784 * @ratio: Ratio of BCLK to Sample rate.
2786 * Configures the DAI for a preset BCLK to sample rate ratio.
2788 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2790 if (dai->driver && dai->driver->ops->set_bclk_ratio)
2791 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2792 else
2793 return -EINVAL;
2795 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2798 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2799 * @dai: DAI
2800 * @fmt: SND_SOC_DAIFMT_ format value.
2802 * Configures the DAI hardware format and clocking.
2804 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2806 if (dai->driver == NULL)
2807 return -EINVAL;
2808 if (dai->driver->ops->set_fmt == NULL)
2809 return -ENOTSUPP;
2810 return dai->driver->ops->set_fmt(dai, fmt);
2812 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2815 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
2816 * @slots: Number of slots in use.
2817 * @tx_mask: bitmask representing active TX slots.
2818 * @rx_mask: bitmask representing active RX slots.
2820 * Generates the TDM tx and rx slot default masks for DAI.
2822 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
2823 unsigned int *tx_mask,
2824 unsigned int *rx_mask)
2826 if (*tx_mask || *rx_mask)
2827 return 0;
2829 if (!slots)
2830 return -EINVAL;
2832 *tx_mask = (1 << slots) - 1;
2833 *rx_mask = (1 << slots) - 1;
2835 return 0;
2839 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2840 * @dai: The DAI to configure
2841 * @tx_mask: bitmask representing active TX slots.
2842 * @rx_mask: bitmask representing active RX slots.
2843 * @slots: Number of slots in use.
2844 * @slot_width: Width in bits for each slot.
2846 * This function configures the specified DAI for TDM operation. @slot contains
2847 * the total number of slots of the TDM stream and @slot_with the width of each
2848 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2849 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2850 * DAI should write to or read from. If a bit is set the corresponding slot is
2851 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2852 * the first slot, bit 1 to the second slot and so on. The first active slot
2853 * maps to the first channel of the DAI, the second active slot to the second
2854 * channel and so on.
2856 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2857 * @rx_mask and @slot_width will be ignored.
2859 * Returns 0 on success, a negative error code otherwise.
2861 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2862 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2864 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2865 dai->driver->ops->xlate_tdm_slot_mask(slots,
2866 &tx_mask, &rx_mask);
2867 else
2868 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
2870 dai->tx_mask = tx_mask;
2871 dai->rx_mask = rx_mask;
2873 if (dai->driver && dai->driver->ops->set_tdm_slot)
2874 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2875 slots, slot_width);
2876 else
2877 return -ENOTSUPP;
2879 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2882 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2883 * @dai: DAI
2884 * @tx_num: how many TX channels
2885 * @tx_slot: pointer to an array which imply the TX slot number channel
2886 * 0~num-1 uses
2887 * @rx_num: how many RX channels
2888 * @rx_slot: pointer to an array which imply the RX slot number channel
2889 * 0~num-1 uses
2891 * configure the relationship between channel number and TDM slot number.
2893 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2894 unsigned int tx_num, unsigned int *tx_slot,
2895 unsigned int rx_num, unsigned int *rx_slot)
2897 if (dai->driver && dai->driver->ops->set_channel_map)
2898 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2899 rx_num, rx_slot);
2900 else
2901 return -EINVAL;
2903 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2906 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2907 * @dai: DAI
2908 * @tristate: tristate enable
2910 * Tristates the DAI so that others can use it.
2912 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2914 if (dai->driver && dai->driver->ops->set_tristate)
2915 return dai->driver->ops->set_tristate(dai, tristate);
2916 else
2917 return -EINVAL;
2919 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2922 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2923 * @dai: DAI
2924 * @mute: mute enable
2925 * @direction: stream to mute
2927 * Mutes the DAI DAC.
2929 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2930 int direction)
2932 if (!dai->driver)
2933 return -ENOTSUPP;
2935 if (dai->driver->ops->mute_stream)
2936 return dai->driver->ops->mute_stream(dai, mute, direction);
2937 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2938 dai->driver->ops->digital_mute)
2939 return dai->driver->ops->digital_mute(dai, mute);
2940 else
2941 return -ENOTSUPP;
2943 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2946 * snd_soc_register_card - Register a card with the ASoC core
2948 * @card: Card to register
2951 int snd_soc_register_card(struct snd_soc_card *card)
2953 int i, ret;
2954 struct snd_soc_pcm_runtime *rtd;
2956 if (!card->name || !card->dev)
2957 return -EINVAL;
2959 for (i = 0; i < card->num_links; i++) {
2960 struct snd_soc_dai_link *link = &card->dai_link[i];
2962 ret = soc_init_dai_link(card, link);
2963 if (ret) {
2964 dev_err(card->dev, "ASoC: failed to init link %s\n",
2965 link->name);
2966 return ret;
2970 dev_set_drvdata(card->dev, card);
2972 snd_soc_initialize_card_lists(card);
2974 INIT_LIST_HEAD(&card->dai_link_list);
2975 card->num_dai_links = 0;
2977 INIT_LIST_HEAD(&card->rtd_list);
2978 card->num_rtd = 0;
2980 INIT_LIST_HEAD(&card->dapm_dirty);
2981 INIT_LIST_HEAD(&card->dobj_list);
2982 card->instantiated = 0;
2983 mutex_init(&card->mutex);
2984 mutex_init(&card->dapm_mutex);
2986 ret = snd_soc_instantiate_card(card);
2987 if (ret != 0)
2988 return ret;
2990 /* deactivate pins to sleep state */
2991 list_for_each_entry(rtd, &card->rtd_list, list) {
2992 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2993 int j;
2995 for (j = 0; j < rtd->num_codecs; j++) {
2996 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2997 if (!codec_dai->active)
2998 pinctrl_pm_select_sleep_state(codec_dai->dev);
3001 if (!cpu_dai->active)
3002 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3005 return ret;
3007 EXPORT_SYMBOL_GPL(snd_soc_register_card);
3010 * snd_soc_unregister_card - Unregister a card with the ASoC core
3012 * @card: Card to unregister
3015 int snd_soc_unregister_card(struct snd_soc_card *card)
3017 if (card->instantiated) {
3018 card->instantiated = false;
3019 snd_soc_dapm_shutdown(card);
3020 soc_cleanup_card_resources(card);
3021 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
3024 return 0;
3026 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3029 * Simplify DAI link configuration by removing ".-1" from device names
3030 * and sanitizing names.
3032 static char *fmt_single_name(struct device *dev, int *id)
3034 char *found, name[NAME_SIZE];
3035 int id1, id2;
3037 if (dev_name(dev) == NULL)
3038 return NULL;
3040 strlcpy(name, dev_name(dev), NAME_SIZE);
3042 /* are we a "%s.%d" name (platform and SPI components) */
3043 found = strstr(name, dev->driver->name);
3044 if (found) {
3045 /* get ID */
3046 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3048 /* discard ID from name if ID == -1 */
3049 if (*id == -1)
3050 found[strlen(dev->driver->name)] = '\0';
3053 } else {
3054 /* I2C component devices are named "bus-addr" */
3055 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3056 char tmp[NAME_SIZE];
3058 /* create unique ID number from I2C addr and bus */
3059 *id = ((id1 & 0xffff) << 16) + id2;
3061 /* sanitize component name for DAI link creation */
3062 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3063 strlcpy(name, tmp, NAME_SIZE);
3064 } else
3065 *id = 0;
3068 return kstrdup(name, GFP_KERNEL);
3072 * Simplify DAI link naming for single devices with multiple DAIs by removing
3073 * any ".-1" and using the DAI name (instead of device name).
3075 static inline char *fmt_multiple_name(struct device *dev,
3076 struct snd_soc_dai_driver *dai_drv)
3078 if (dai_drv->name == NULL) {
3079 dev_err(dev,
3080 "ASoC: error - multiple DAI %s registered with no name\n",
3081 dev_name(dev));
3082 return NULL;
3085 return kstrdup(dai_drv->name, GFP_KERNEL);
3089 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
3091 * @component: The component for which the DAIs should be unregistered
3093 static void snd_soc_unregister_dais(struct snd_soc_component *component)
3095 struct snd_soc_dai *dai, *_dai;
3097 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
3098 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3099 dai->name);
3100 list_del(&dai->list);
3101 kfree(dai->name);
3102 kfree(dai);
3106 /* Create a DAI and add it to the component's DAI list */
3107 static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
3108 struct snd_soc_dai_driver *dai_drv,
3109 bool legacy_dai_naming)
3111 struct device *dev = component->dev;
3112 struct snd_soc_dai *dai;
3114 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
3116 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3117 if (dai == NULL)
3118 return NULL;
3121 * Back in the old days when we still had component-less DAIs,
3122 * instead of having a static name, component-less DAIs would
3123 * inherit the name of the parent device so it is possible to
3124 * register multiple instances of the DAI. We still need to keep
3125 * the same naming style even though those DAIs are not
3126 * component-less anymore.
3128 if (legacy_dai_naming &&
3129 (dai_drv->id == 0 || dai_drv->name == NULL)) {
3130 dai->name = fmt_single_name(dev, &dai->id);
3131 } else {
3132 dai->name = fmt_multiple_name(dev, dai_drv);
3133 if (dai_drv->id)
3134 dai->id = dai_drv->id;
3135 else
3136 dai->id = component->num_dai;
3138 if (dai->name == NULL) {
3139 kfree(dai);
3140 return NULL;
3143 dai->component = component;
3144 dai->dev = dev;
3145 dai->driver = dai_drv;
3146 if (!dai->driver->ops)
3147 dai->driver->ops = &null_dai_ops;
3149 list_add(&dai->list, &component->dai_list);
3150 component->num_dai++;
3152 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3153 return dai;
3157 * snd_soc_register_dais - Register a DAI with the ASoC core
3159 * @component: The component the DAIs are registered for
3160 * @dai_drv: DAI driver to use for the DAIs
3161 * @count: Number of DAIs
3162 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3163 * parent's name.
3165 static int snd_soc_register_dais(struct snd_soc_component *component,
3166 struct snd_soc_dai_driver *dai_drv, size_t count,
3167 bool legacy_dai_naming)
3169 struct device *dev = component->dev;
3170 struct snd_soc_dai *dai;
3171 unsigned int i;
3172 int ret;
3174 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
3176 component->dai_drv = dai_drv;
3178 for (i = 0; i < count; i++) {
3180 dai = soc_add_dai(component, dai_drv + i,
3181 count == 1 && legacy_dai_naming);
3182 if (dai == NULL) {
3183 ret = -ENOMEM;
3184 goto err;
3188 return 0;
3190 err:
3191 snd_soc_unregister_dais(component);
3193 return ret;
3197 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
3199 * @component: The component the DAIs are registered for
3200 * @dai_drv: DAI driver to use for the DAI
3202 * Topology can use this API to register DAIs when probing a component.
3203 * These DAIs's widgets will be freed in the card cleanup and the DAIs
3204 * will be freed in the component cleanup.
3206 int snd_soc_register_dai(struct snd_soc_component *component,
3207 struct snd_soc_dai_driver *dai_drv)
3209 struct snd_soc_dapm_context *dapm =
3210 snd_soc_component_get_dapm(component);
3211 struct snd_soc_dai *dai;
3212 int ret;
3214 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
3215 dev_err(component->dev, "Invalid dai type %d\n",
3216 dai_drv->dobj.type);
3217 return -EINVAL;
3220 lockdep_assert_held(&client_mutex);
3221 dai = soc_add_dai(component, dai_drv, false);
3222 if (!dai)
3223 return -ENOMEM;
3225 /* Create the DAI widgets here. After adding DAIs, topology may
3226 * also add routes that need these widgets as source or sink.
3228 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
3229 if (ret != 0) {
3230 dev_err(component->dev,
3231 "Failed to create DAI widgets %d\n", ret);
3234 return ret;
3236 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3238 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3239 enum snd_soc_dapm_type type, int subseq)
3241 struct snd_soc_component *component = dapm->component;
3243 component->driver->seq_notifier(component, type, subseq);
3246 static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3247 int event)
3249 struct snd_soc_component *component = dapm->component;
3251 return component->driver->stream_event(component, event);
3254 static int snd_soc_component_initialize(struct snd_soc_component *component,
3255 const struct snd_soc_component_driver *driver, struct device *dev)
3257 struct snd_soc_dapm_context *dapm;
3259 component->name = fmt_single_name(dev, &component->id);
3260 if (!component->name) {
3261 dev_err(dev, "ASoC: Failed to allocate name\n");
3262 return -ENOMEM;
3265 component->dev = dev;
3266 component->driver = driver;
3267 component->probe = component->driver->probe;
3268 component->remove = component->driver->remove;
3269 component->suspend = component->driver->suspend;
3270 component->resume = component->driver->resume;
3271 component->set_sysclk = component->driver->set_sysclk;
3272 component->set_pll = component->driver->set_pll;
3273 component->set_jack = component->driver->set_jack;
3275 dapm = snd_soc_component_get_dapm(component);
3276 dapm->dev = dev;
3277 dapm->component = component;
3278 dapm->bias_level = SND_SOC_BIAS_OFF;
3279 dapm->idle_bias_off = true;
3280 if (driver->seq_notifier)
3281 dapm->seq_notifier = snd_soc_component_seq_notifier;
3282 if (driver->stream_event)
3283 dapm->stream_event = snd_soc_component_stream_event;
3285 INIT_LIST_HEAD(&component->dai_list);
3286 mutex_init(&component->io_mutex);
3288 return 0;
3291 static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
3293 int val_bytes = regmap_get_val_bytes(component->regmap);
3295 /* Errors are legitimate for non-integer byte multiples */
3296 if (val_bytes > 0)
3297 component->val_bytes = val_bytes;
3300 #ifdef CONFIG_REGMAP
3303 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
3304 * @component: The component for which to initialize the regmap instance
3305 * @regmap: The regmap instance that should be used by the component
3307 * This function allows deferred assignment of the regmap instance that is
3308 * associated with the component. Only use this if the regmap instance is not
3309 * yet ready when the component is registered. The function must also be called
3310 * before the first IO attempt of the component.
3312 void snd_soc_component_init_regmap(struct snd_soc_component *component,
3313 struct regmap *regmap)
3315 component->regmap = regmap;
3316 snd_soc_component_setup_regmap(component);
3318 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3321 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
3322 * @component: The component for which to de-initialize the regmap instance
3324 * Calls regmap_exit() on the regmap instance associated to the component and
3325 * removes the regmap instance from the component.
3327 * This function should only be used if snd_soc_component_init_regmap() was used
3328 * to initialize the regmap instance.
3330 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3332 regmap_exit(component->regmap);
3333 component->regmap = NULL;
3335 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3337 #endif
3339 static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
3341 if (!component->write && !component->read) {
3342 if (!component->regmap)
3343 component->regmap = dev_get_regmap(component->dev, NULL);
3344 if (component->regmap)
3345 snd_soc_component_setup_regmap(component);
3348 list_add(&component->list, &component_list);
3349 INIT_LIST_HEAD(&component->dobj_list);
3352 static void snd_soc_component_add(struct snd_soc_component *component)
3354 mutex_lock(&client_mutex);
3355 snd_soc_component_add_unlocked(component);
3356 mutex_unlock(&client_mutex);
3359 static void snd_soc_component_cleanup(struct snd_soc_component *component)
3361 snd_soc_unregister_dais(component);
3362 kfree(component->name);
3365 static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3367 struct snd_soc_card *card = component->card;
3369 if (card)
3370 snd_soc_unregister_card(card);
3372 list_del(&component->list);
3375 int snd_soc_register_component(struct device *dev,
3376 const struct snd_soc_component_driver *component_driver,
3377 struct snd_soc_dai_driver *dai_drv,
3378 int num_dai)
3380 struct snd_soc_component *component;
3381 int ret;
3383 component = kzalloc(sizeof(*component), GFP_KERNEL);
3384 if (!component) {
3385 dev_err(dev, "ASoC: Failed to allocate memory\n");
3386 return -ENOMEM;
3389 ret = snd_soc_component_initialize(component, component_driver, dev);
3390 if (ret)
3391 goto err_free;
3393 component->ignore_pmdown_time = true;
3394 component->registered_as_component = true;
3396 ret = snd_soc_register_dais(component, dai_drv, num_dai, true);
3397 if (ret < 0) {
3398 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3399 goto err_cleanup;
3402 snd_soc_component_add(component);
3404 return 0;
3406 err_cleanup:
3407 snd_soc_component_cleanup(component);
3408 err_free:
3409 kfree(component);
3410 return ret;
3412 EXPORT_SYMBOL_GPL(snd_soc_register_component);
3415 * snd_soc_unregister_component - Unregister all related component
3416 * from the ASoC core
3418 * @dev: The device to unregister
3420 static int __snd_soc_unregister_component(struct device *dev)
3422 struct snd_soc_component *component;
3423 int found = 0;
3425 mutex_lock(&client_mutex);
3426 list_for_each_entry(component, &component_list, list) {
3427 if (dev != component->dev ||
3428 !component->registered_as_component)
3429 continue;
3431 snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);
3432 snd_soc_component_del_unlocked(component);
3433 found = 1;
3434 break;
3436 mutex_unlock(&client_mutex);
3438 if (found) {
3439 snd_soc_component_cleanup(component);
3440 kfree(component);
3443 return found;
3446 void snd_soc_unregister_component(struct device *dev)
3448 while (__snd_soc_unregister_component(dev));
3450 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3452 static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
3454 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3456 return platform->driver->probe(platform);
3459 static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
3461 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3463 platform->driver->remove(platform);
3467 * snd_soc_add_platform - Add a platform to the ASoC core
3468 * @dev: The parent device for the platform
3469 * @platform: The platform to add
3470 * @platform_drv: The driver for the platform
3472 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
3473 const struct snd_soc_platform_driver *platform_drv)
3475 int ret;
3477 ret = snd_soc_component_initialize(&platform->component,
3478 &platform_drv->component_driver, dev);
3479 if (ret)
3480 return ret;
3482 platform->dev = dev;
3483 platform->driver = platform_drv;
3485 if (platform_drv->probe)
3486 platform->component.probe = snd_soc_platform_drv_probe;
3487 if (platform_drv->remove)
3488 platform->component.remove = snd_soc_platform_drv_remove;
3490 #ifdef CONFIG_DEBUG_FS
3491 platform->component.debugfs_prefix = "platform";
3492 #endif
3494 mutex_lock(&client_mutex);
3495 snd_soc_component_add_unlocked(&platform->component);
3496 list_add(&platform->list, &platform_list);
3497 mutex_unlock(&client_mutex);
3499 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
3500 platform->component.name);
3502 return 0;
3504 EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3507 * snd_soc_register_platform - Register a platform with the ASoC core
3509 * @dev: The device for the platform
3510 * @platform_drv: The driver for the platform
3512 int snd_soc_register_platform(struct device *dev,
3513 const struct snd_soc_platform_driver *platform_drv)
3515 struct snd_soc_platform *platform;
3516 int ret;
3518 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3520 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3521 if (platform == NULL)
3522 return -ENOMEM;
3524 ret = snd_soc_add_platform(dev, platform, platform_drv);
3525 if (ret)
3526 kfree(platform);
3528 return ret;
3530 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3533 * snd_soc_remove_platform - Remove a platform from the ASoC core
3534 * @platform: the platform to remove
3536 void snd_soc_remove_platform(struct snd_soc_platform *platform)
3539 mutex_lock(&client_mutex);
3540 list_del(&platform->list);
3541 snd_soc_component_del_unlocked(&platform->component);
3542 mutex_unlock(&client_mutex);
3544 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3545 platform->component.name);
3547 snd_soc_component_cleanup(&platform->component);
3549 EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
3551 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
3553 struct snd_soc_platform *platform;
3555 mutex_lock(&client_mutex);
3556 list_for_each_entry(platform, &platform_list, list) {
3557 if (dev == platform->dev) {
3558 mutex_unlock(&client_mutex);
3559 return platform;
3562 mutex_unlock(&client_mutex);
3564 return NULL;
3566 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
3569 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3571 * @dev: platform to unregister
3573 void snd_soc_unregister_platform(struct device *dev)
3575 struct snd_soc_platform *platform;
3577 platform = snd_soc_lookup_platform(dev);
3578 if (!platform)
3579 return;
3581 snd_soc_remove_platform(platform);
3582 kfree(platform);
3584 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3586 static u64 codec_format_map[] = {
3587 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3588 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3589 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3590 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3591 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3592 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3593 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3594 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3595 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3596 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3597 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3598 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3599 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3600 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3601 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3602 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3605 /* Fix up the DAI formats for endianness: codecs don't actually see
3606 * the endianness of the data but we're using the CPU format
3607 * definitions which do need to include endianness so we ensure that
3608 * codec DAIs always have both big and little endian variants set.
3610 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3612 int i;
3614 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3615 if (stream->formats & codec_format_map[i])
3616 stream->formats |= codec_format_map[i];
3619 static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3621 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3623 return codec->driver->probe(codec);
3626 static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3628 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3630 codec->driver->remove(codec);
3633 static int snd_soc_codec_drv_suspend(struct snd_soc_component *component)
3635 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3637 return codec->driver->suspend(codec);
3640 static int snd_soc_codec_drv_resume(struct snd_soc_component *component)
3642 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3644 return codec->driver->resume(codec);
3647 static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3648 unsigned int reg, unsigned int val)
3650 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3652 return codec->driver->write(codec, reg, val);
3655 static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3656 unsigned int reg, unsigned int *val)
3658 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3660 *val = codec->driver->read(codec, reg);
3662 return 0;
3665 static int snd_soc_codec_set_sysclk_(struct snd_soc_component *component,
3666 int clk_id, int source, unsigned int freq, int dir)
3668 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3670 return snd_soc_codec_set_sysclk(codec, clk_id, source, freq, dir);
3673 static int snd_soc_codec_set_pll_(struct snd_soc_component *component,
3674 int pll_id, int source, unsigned int freq_in,
3675 unsigned int freq_out)
3677 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3679 return snd_soc_codec_set_pll(codec, pll_id, source, freq_in, freq_out);
3682 static int snd_soc_codec_set_jack_(struct snd_soc_component *component,
3683 struct snd_soc_jack *jack, void *data)
3685 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3687 return snd_soc_codec_set_jack(codec, jack, data);
3690 static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
3691 enum snd_soc_bias_level level)
3693 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
3695 return codec->driver->set_bias_level(codec, level);
3699 * snd_soc_register_codec - Register a codec with the ASoC core
3701 * @dev: The parent device for this codec
3702 * @codec_drv: Codec driver
3703 * @dai_drv: The associated DAI driver
3704 * @num_dai: Number of DAIs
3706 int snd_soc_register_codec(struct device *dev,
3707 const struct snd_soc_codec_driver *codec_drv,
3708 struct snd_soc_dai_driver *dai_drv,
3709 int num_dai)
3711 struct snd_soc_dapm_context *dapm;
3712 struct snd_soc_codec *codec;
3713 struct snd_soc_dai *dai;
3714 int ret, i;
3716 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3718 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3719 if (codec == NULL)
3720 return -ENOMEM;
3722 codec->component.codec = codec;
3724 ret = snd_soc_component_initialize(&codec->component,
3725 &codec_drv->component_driver, dev);
3726 if (ret)
3727 goto err_free;
3729 if (codec_drv->probe)
3730 codec->component.probe = snd_soc_codec_drv_probe;
3731 if (codec_drv->remove)
3732 codec->component.remove = snd_soc_codec_drv_remove;
3733 if (codec_drv->suspend)
3734 codec->component.suspend = snd_soc_codec_drv_suspend;
3735 if (codec_drv->resume)
3736 codec->component.resume = snd_soc_codec_drv_resume;
3737 if (codec_drv->write)
3738 codec->component.write = snd_soc_codec_drv_write;
3739 if (codec_drv->read)
3740 codec->component.read = snd_soc_codec_drv_read;
3741 if (codec_drv->set_sysclk)
3742 codec->component.set_sysclk = snd_soc_codec_set_sysclk_;
3743 if (codec_drv->set_pll)
3744 codec->component.set_pll = snd_soc_codec_set_pll_;
3745 if (codec_drv->set_jack)
3746 codec->component.set_jack = snd_soc_codec_set_jack_;
3747 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
3749 dapm = snd_soc_codec_get_dapm(codec);
3750 dapm->idle_bias_off = codec_drv->idle_bias_off;
3751 dapm->suspend_bias_off = codec_drv->suspend_bias_off;
3752 if (codec_drv->seq_notifier)
3753 dapm->seq_notifier = codec_drv->seq_notifier;
3754 if (codec_drv->set_bias_level)
3755 dapm->set_bias_level = snd_soc_codec_set_bias_level;
3756 codec->dev = dev;
3757 codec->driver = codec_drv;
3758 codec->component.val_bytes = codec_drv->reg_word_size;
3760 #ifdef CONFIG_DEBUG_FS
3761 codec->component.init_debugfs = soc_init_codec_debugfs;
3762 codec->component.debugfs_prefix = "codec";
3763 #endif
3765 if (codec_drv->get_regmap)
3766 codec->component.regmap = codec_drv->get_regmap(dev);
3768 for (i = 0; i < num_dai; i++) {
3769 fixup_codec_formats(&dai_drv[i].playback);
3770 fixup_codec_formats(&dai_drv[i].capture);
3773 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3774 if (ret < 0) {
3775 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3776 goto err_cleanup;
3779 list_for_each_entry(dai, &codec->component.dai_list, list)
3780 dai->codec = codec;
3782 mutex_lock(&client_mutex);
3783 snd_soc_component_add_unlocked(&codec->component);
3784 list_add(&codec->list, &codec_list);
3785 mutex_unlock(&client_mutex);
3787 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3788 codec->component.name);
3789 return 0;
3791 err_cleanup:
3792 snd_soc_component_cleanup(&codec->component);
3793 err_free:
3794 kfree(codec);
3795 return ret;
3797 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3800 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3802 * @dev: codec to unregister
3804 void snd_soc_unregister_codec(struct device *dev)
3806 struct snd_soc_codec *codec;
3808 mutex_lock(&client_mutex);
3809 list_for_each_entry(codec, &codec_list, list) {
3810 if (dev == codec->dev)
3811 goto found;
3813 mutex_unlock(&client_mutex);
3814 return;
3816 found:
3817 list_del(&codec->list);
3818 snd_soc_component_del_unlocked(&codec->component);
3819 mutex_unlock(&client_mutex);
3821 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3822 codec->component.name);
3824 snd_soc_component_cleanup(&codec->component);
3825 snd_soc_cache_exit(codec);
3826 kfree(codec);
3828 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3830 /* Retrieve a card's name from device tree */
3831 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3832 const char *propname)
3834 struct device_node *np;
3835 int ret;
3837 if (!card->dev) {
3838 pr_err("card->dev is not set before calling %s\n", __func__);
3839 return -EINVAL;
3842 np = card->dev->of_node;
3844 ret = of_property_read_string_index(np, propname, 0, &card->name);
3846 * EINVAL means the property does not exist. This is fine providing
3847 * card->name was previously set, which is checked later in
3848 * snd_soc_register_card.
3850 if (ret < 0 && ret != -EINVAL) {
3851 dev_err(card->dev,
3852 "ASoC: Property '%s' could not be read: %d\n",
3853 propname, ret);
3854 return ret;
3857 return 0;
3859 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3861 static const struct snd_soc_dapm_widget simple_widgets[] = {
3862 SND_SOC_DAPM_MIC("Microphone", NULL),
3863 SND_SOC_DAPM_LINE("Line", NULL),
3864 SND_SOC_DAPM_HP("Headphone", NULL),
3865 SND_SOC_DAPM_SPK("Speaker", NULL),
3868 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
3869 const char *propname)
3871 struct device_node *np = card->dev->of_node;
3872 struct snd_soc_dapm_widget *widgets;
3873 const char *template, *wname;
3874 int i, j, num_widgets, ret;
3876 num_widgets = of_property_count_strings(np, propname);
3877 if (num_widgets < 0) {
3878 dev_err(card->dev,
3879 "ASoC: Property '%s' does not exist\n", propname);
3880 return -EINVAL;
3882 if (num_widgets & 1) {
3883 dev_err(card->dev,
3884 "ASoC: Property '%s' length is not even\n", propname);
3885 return -EINVAL;
3888 num_widgets /= 2;
3889 if (!num_widgets) {
3890 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3891 propname);
3892 return -EINVAL;
3895 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3896 GFP_KERNEL);
3897 if (!widgets) {
3898 dev_err(card->dev,
3899 "ASoC: Could not allocate memory for widgets\n");
3900 return -ENOMEM;
3903 for (i = 0; i < num_widgets; i++) {
3904 ret = of_property_read_string_index(np, propname,
3905 2 * i, &template);
3906 if (ret) {
3907 dev_err(card->dev,
3908 "ASoC: Property '%s' index %d read error:%d\n",
3909 propname, 2 * i, ret);
3910 return -EINVAL;
3913 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3914 if (!strncmp(template, simple_widgets[j].name,
3915 strlen(simple_widgets[j].name))) {
3916 widgets[i] = simple_widgets[j];
3917 break;
3921 if (j >= ARRAY_SIZE(simple_widgets)) {
3922 dev_err(card->dev,
3923 "ASoC: DAPM widget '%s' is not supported\n",
3924 template);
3925 return -EINVAL;
3928 ret = of_property_read_string_index(np, propname,
3929 (2 * i) + 1,
3930 &wname);
3931 if (ret) {
3932 dev_err(card->dev,
3933 "ASoC: Property '%s' index %d read error:%d\n",
3934 propname, (2 * i) + 1, ret);
3935 return -EINVAL;
3938 widgets[i].name = wname;
3941 card->of_dapm_widgets = widgets;
3942 card->num_of_dapm_widgets = num_widgets;
3944 return 0;
3946 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
3948 static int snd_soc_of_get_slot_mask(struct device_node *np,
3949 const char *prop_name,
3950 unsigned int *mask)
3952 u32 val;
3953 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
3954 int i;
3956 if (!of_slot_mask)
3957 return 0;
3958 val /= sizeof(u32);
3959 for (i = 0; i < val; i++)
3960 if (be32_to_cpup(&of_slot_mask[i]))
3961 *mask |= (1 << i);
3963 return val;
3966 int snd_soc_of_parse_tdm_slot(struct device_node *np,
3967 unsigned int *tx_mask,
3968 unsigned int *rx_mask,
3969 unsigned int *slots,
3970 unsigned int *slot_width)
3972 u32 val;
3973 int ret;
3975 if (tx_mask)
3976 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3977 if (rx_mask)
3978 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3980 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3981 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3982 if (ret)
3983 return ret;
3985 if (slots)
3986 *slots = val;
3989 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3990 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3991 if (ret)
3992 return ret;
3994 if (slot_width)
3995 *slot_width = val;
3998 return 0;
4000 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4002 void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
4003 struct snd_soc_codec_conf *codec_conf,
4004 struct device_node *of_node,
4005 const char *propname)
4007 struct device_node *np = card->dev->of_node;
4008 const char *str;
4009 int ret;
4011 ret = of_property_read_string(np, propname, &str);
4012 if (ret < 0) {
4013 /* no prefix is not error */
4014 return;
4017 codec_conf->of_node = of_node;
4018 codec_conf->name_prefix = str;
4020 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
4022 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4023 const char *propname)
4025 struct device_node *np = card->dev->of_node;
4026 int num_routes;
4027 struct snd_soc_dapm_route *routes;
4028 int i, ret;
4030 num_routes = of_property_count_strings(np, propname);
4031 if (num_routes < 0 || num_routes & 1) {
4032 dev_err(card->dev,
4033 "ASoC: Property '%s' does not exist or its length is not even\n",
4034 propname);
4035 return -EINVAL;
4037 num_routes /= 2;
4038 if (!num_routes) {
4039 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4040 propname);
4041 return -EINVAL;
4044 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4045 GFP_KERNEL);
4046 if (!routes) {
4047 dev_err(card->dev,
4048 "ASoC: Could not allocate DAPM route table\n");
4049 return -EINVAL;
4052 for (i = 0; i < num_routes; i++) {
4053 ret = of_property_read_string_index(np, propname,
4054 2 * i, &routes[i].sink);
4055 if (ret) {
4056 dev_err(card->dev,
4057 "ASoC: Property '%s' index %d could not be read: %d\n",
4058 propname, 2 * i, ret);
4059 return -EINVAL;
4061 ret = of_property_read_string_index(np, propname,
4062 (2 * i) + 1, &routes[i].source);
4063 if (ret) {
4064 dev_err(card->dev,
4065 "ASoC: Property '%s' index %d could not be read: %d\n",
4066 propname, (2 * i) + 1, ret);
4067 return -EINVAL;
4071 card->num_of_dapm_routes = num_routes;
4072 card->of_dapm_routes = routes;
4074 return 0;
4076 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4078 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4079 const char *prefix,
4080 struct device_node **bitclkmaster,
4081 struct device_node **framemaster)
4083 int ret, i;
4084 char prop[128];
4085 unsigned int format = 0;
4086 int bit, frame;
4087 const char *str;
4088 struct {
4089 char *name;
4090 unsigned int val;
4091 } of_fmt_table[] = {
4092 { "i2s", SND_SOC_DAIFMT_I2S },
4093 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4094 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4095 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4096 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4097 { "ac97", SND_SOC_DAIFMT_AC97 },
4098 { "pdm", SND_SOC_DAIFMT_PDM},
4099 { "msb", SND_SOC_DAIFMT_MSB },
4100 { "lsb", SND_SOC_DAIFMT_LSB },
4103 if (!prefix)
4104 prefix = "";
4107 * check "dai-format = xxx"
4108 * or "[prefix]format = xxx"
4109 * SND_SOC_DAIFMT_FORMAT_MASK area
4111 ret = of_property_read_string(np, "dai-format", &str);
4112 if (ret < 0) {
4113 snprintf(prop, sizeof(prop), "%sformat", prefix);
4114 ret = of_property_read_string(np, prop, &str);
4116 if (ret == 0) {
4117 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4118 if (strcmp(str, of_fmt_table[i].name) == 0) {
4119 format |= of_fmt_table[i].val;
4120 break;
4126 * check "[prefix]continuous-clock"
4127 * SND_SOC_DAIFMT_CLOCK_MASK area
4129 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4130 if (of_property_read_bool(np, prop))
4131 format |= SND_SOC_DAIFMT_CONT;
4132 else
4133 format |= SND_SOC_DAIFMT_GATED;
4136 * check "[prefix]bitclock-inversion"
4137 * check "[prefix]frame-inversion"
4138 * SND_SOC_DAIFMT_INV_MASK area
4140 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4141 bit = !!of_get_property(np, prop, NULL);
4143 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4144 frame = !!of_get_property(np, prop, NULL);
4146 switch ((bit << 4) + frame) {
4147 case 0x11:
4148 format |= SND_SOC_DAIFMT_IB_IF;
4149 break;
4150 case 0x10:
4151 format |= SND_SOC_DAIFMT_IB_NF;
4152 break;
4153 case 0x01:
4154 format |= SND_SOC_DAIFMT_NB_IF;
4155 break;
4156 default:
4157 /* SND_SOC_DAIFMT_NB_NF is default */
4158 break;
4162 * check "[prefix]bitclock-master"
4163 * check "[prefix]frame-master"
4164 * SND_SOC_DAIFMT_MASTER_MASK area
4166 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4167 bit = !!of_get_property(np, prop, NULL);
4168 if (bit && bitclkmaster)
4169 *bitclkmaster = of_parse_phandle(np, prop, 0);
4171 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4172 frame = !!of_get_property(np, prop, NULL);
4173 if (frame && framemaster)
4174 *framemaster = of_parse_phandle(np, prop, 0);
4176 switch ((bit << 4) + frame) {
4177 case 0x11:
4178 format |= SND_SOC_DAIFMT_CBM_CFM;
4179 break;
4180 case 0x10:
4181 format |= SND_SOC_DAIFMT_CBM_CFS;
4182 break;
4183 case 0x01:
4184 format |= SND_SOC_DAIFMT_CBS_CFM;
4185 break;
4186 default:
4187 format |= SND_SOC_DAIFMT_CBS_CFS;
4188 break;
4191 return format;
4193 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4195 int snd_soc_get_dai_id(struct device_node *ep)
4197 struct snd_soc_component *pos;
4198 struct device_node *node;
4199 int ret;
4201 node = of_graph_get_port_parent(ep);
4204 * For example HDMI case, HDMI has video/sound port,
4205 * but ALSA SoC needs sound port number only.
4206 * Thus counting HDMI DT port/endpoint doesn't work.
4207 * Then, it should have .of_xlate_dai_id
4209 ret = -ENOTSUPP;
4210 mutex_lock(&client_mutex);
4211 list_for_each_entry(pos, &component_list, list) {
4212 struct device_node *component_of_node = pos->dev->of_node;
4214 if (!component_of_node && pos->dev->parent)
4215 component_of_node = pos->dev->parent->of_node;
4217 if (component_of_node != node)
4218 continue;
4220 if (pos->driver->of_xlate_dai_id)
4221 ret = pos->driver->of_xlate_dai_id(pos, ep);
4223 break;
4225 mutex_unlock(&client_mutex);
4227 of_node_put(node);
4229 return ret;
4231 EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
4233 int snd_soc_get_dai_name(struct of_phandle_args *args,
4234 const char **dai_name)
4236 struct snd_soc_component *pos;
4237 struct device_node *component_of_node;
4238 int ret = -EPROBE_DEFER;
4240 mutex_lock(&client_mutex);
4241 list_for_each_entry(pos, &component_list, list) {
4242 component_of_node = pos->dev->of_node;
4243 if (!component_of_node && pos->dev->parent)
4244 component_of_node = pos->dev->parent->of_node;
4246 if (component_of_node != args->np)
4247 continue;
4249 if (pos->driver->of_xlate_dai_name) {
4250 ret = pos->driver->of_xlate_dai_name(pos,
4251 args,
4252 dai_name);
4253 } else {
4254 int id = -1;
4256 switch (args->args_count) {
4257 case 0:
4258 id = 0; /* same as dai_drv[0] */
4259 break;
4260 case 1:
4261 id = args->args[0];
4262 break;
4263 default:
4264 /* not supported */
4265 break;
4268 if (id < 0 || id >= pos->num_dai) {
4269 ret = -EINVAL;
4270 continue;
4273 ret = 0;
4275 *dai_name = pos->dai_drv[id].name;
4276 if (!*dai_name)
4277 *dai_name = pos->name;
4280 break;
4282 mutex_unlock(&client_mutex);
4283 return ret;
4285 EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
4287 int snd_soc_of_get_dai_name(struct device_node *of_node,
4288 const char **dai_name)
4290 struct of_phandle_args args;
4291 int ret;
4293 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4294 "#sound-dai-cells", 0, &args);
4295 if (ret)
4296 return ret;
4298 ret = snd_soc_get_dai_name(&args, dai_name);
4300 of_node_put(args.np);
4302 return ret;
4304 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4307 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
4308 * @dev: Card device
4309 * @of_node: Device node
4310 * @dai_link: DAI link
4312 * Builds an array of CODEC DAI components from the DAI link property
4313 * 'sound-dai'.
4314 * The array is set in the DAI link and the number of DAIs is set accordingly.
4315 * The device nodes in the array (of_node) must be dereferenced by the caller.
4317 * Returns 0 for success
4319 int snd_soc_of_get_dai_link_codecs(struct device *dev,
4320 struct device_node *of_node,
4321 struct snd_soc_dai_link *dai_link)
4323 struct of_phandle_args args;
4324 struct snd_soc_dai_link_component *component;
4325 char *name;
4326 int index, num_codecs, ret;
4328 /* Count the number of CODECs */
4329 name = "sound-dai";
4330 num_codecs = of_count_phandle_with_args(of_node, name,
4331 "#sound-dai-cells");
4332 if (num_codecs <= 0) {
4333 if (num_codecs == -ENOENT)
4334 dev_err(dev, "No 'sound-dai' property\n");
4335 else
4336 dev_err(dev, "Bad phandle in 'sound-dai'\n");
4337 return num_codecs;
4339 component = devm_kzalloc(dev,
4340 sizeof *component * num_codecs,
4341 GFP_KERNEL);
4342 if (!component)
4343 return -ENOMEM;
4344 dai_link->codecs = component;
4345 dai_link->num_codecs = num_codecs;
4347 /* Parse the list */
4348 for (index = 0, component = dai_link->codecs;
4349 index < dai_link->num_codecs;
4350 index++, component++) {
4351 ret = of_parse_phandle_with_args(of_node, name,
4352 "#sound-dai-cells",
4353 index, &args);
4354 if (ret)
4355 goto err;
4356 component->of_node = args.np;
4357 ret = snd_soc_get_dai_name(&args, &component->dai_name);
4358 if (ret < 0)
4359 goto err;
4361 return 0;
4362 err:
4363 for (index = 0, component = dai_link->codecs;
4364 index < dai_link->num_codecs;
4365 index++, component++) {
4366 if (!component->of_node)
4367 break;
4368 of_node_put(component->of_node);
4369 component->of_node = NULL;
4371 dai_link->codecs = NULL;
4372 dai_link->num_codecs = 0;
4373 return ret;
4375 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
4377 static int __init snd_soc_init(void)
4379 snd_soc_debugfs_init();
4380 snd_soc_util_init();
4382 return platform_driver_register(&soc_driver);
4384 module_init(snd_soc_init);
4386 static void __exit snd_soc_exit(void)
4388 snd_soc_util_exit();
4389 snd_soc_debugfs_exit();
4391 platform_driver_unregister(&soc_driver);
4393 module_exit(snd_soc_exit);
4395 /* Module information */
4396 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4397 MODULE_DESCRIPTION("ALSA SoC Core");
4398 MODULE_LICENSE("GPL");
4399 MODULE_ALIAS("platform:soc-audio");