xref: /openbmc/linux/sound/soc/soc-core.c (revision adb76b5b9c4740a11f6ad6c68764515961ae8ade)
1873486edSKuninori Morimoto // SPDX-License-Identifier: GPL-2.0+
2873486edSKuninori Morimoto //
3873486edSKuninori Morimoto // soc-core.c  --  ALSA SoC Audio Layer
4873486edSKuninori Morimoto //
5873486edSKuninori Morimoto // Copyright 2005 Wolfson Microelectronics PLC.
6873486edSKuninori Morimoto // Copyright 2005 Openedhand Ltd.
7873486edSKuninori Morimoto // Copyright (C) 2010 Slimlogic Ltd.
8873486edSKuninori Morimoto // Copyright (C) 2010 Texas Instruments Inc.
9873486edSKuninori Morimoto //
10873486edSKuninori Morimoto // Author: Liam Girdwood <lrg@slimlogic.co.uk>
11873486edSKuninori Morimoto //         with code, comments and ideas from :-
12873486edSKuninori Morimoto //         Richard Purdie <richard@openedhand.com>
13873486edSKuninori Morimoto //
14873486edSKuninori Morimoto //  TODO:
15873486edSKuninori Morimoto //   o Add hw rules to enforce rates, etc.
16873486edSKuninori Morimoto //   o More testing with other codecs/machines.
17873486edSKuninori Morimoto //   o Add more codecs and platforms to ensure good API coverage.
18873486edSKuninori Morimoto //   o Support TDM on PCM and I2S
19db2a4165SFrank Mandarino 
20db2a4165SFrank Mandarino #include <linux/module.h>
21db2a4165SFrank Mandarino #include <linux/moduleparam.h>
22db2a4165SFrank Mandarino #include <linux/init.h>
23db2a4165SFrank Mandarino #include <linux/delay.h>
24db2a4165SFrank Mandarino #include <linux/pm.h>
25db2a4165SFrank Mandarino #include <linux/bitops.h>
2612ef193dSTroy Kisky #include <linux/debugfs.h>
27db2a4165SFrank Mandarino #include <linux/platform_device.h>
28741a509fSMarkus Pargmann #include <linux/pinctrl/consumer.h>
29f0e8ed85SMark Brown #include <linux/ctype.h>
305a0e3ad6STejun Heo #include <linux/slab.h>
31bec4fa05SStephen Warren #include <linux/of.h>
32a180e8b9SKuninori Morimoto #include <linux/of_graph.h>
33345233d7SLiam Girdwood #include <linux/dmi.h>
34db2a4165SFrank Mandarino #include <sound/core.h>
353028eb8cSMark Brown #include <sound/jack.h>
36db2a4165SFrank Mandarino #include <sound/pcm.h>
37db2a4165SFrank Mandarino #include <sound/pcm_params.h>
38db2a4165SFrank Mandarino #include <sound/soc.h>
3901d7584cSLiam Girdwood #include <sound/soc-dpcm.h>
408a978234SLiam Girdwood #include <sound/soc-topology.h>
41db2a4165SFrank Mandarino #include <sound/initval.h>
42db2a4165SFrank Mandarino 
43a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
44a8b1d34fSMark Brown #include <trace/events/asoc.h>
45a8b1d34fSMark Brown 
46f0fba2adSLiam Girdwood #define NAME_SIZE	32
47f0fba2adSLiam Girdwood 
48384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
498a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root;
508a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
51384c89e2SMark Brown #endif
52384c89e2SMark Brown 
53c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
54030e79f6SKuninori Morimoto static LIST_HEAD(component_list);
55e894efefSSrinivas Kandagatla static LIST_HEAD(unbind_card_list);
56c5af3a2eSMark Brown 
57368dee94SKuninori Morimoto #define for_each_component(component)			\
58368dee94SKuninori Morimoto 	list_for_each_entry(component, &component_list, list)
59368dee94SKuninori Morimoto 
60db2a4165SFrank Mandarino /*
61587c9844SKuninori Morimoto  * This is used if driver don't need to have CPU/Codec/Platform
62587c9844SKuninori Morimoto  * dai_link. see soc.h
63587c9844SKuninori Morimoto  */
64587c9844SKuninori Morimoto struct snd_soc_dai_link_component null_dailink_component[0];
65587c9844SKuninori Morimoto EXPORT_SYMBOL_GPL(null_dailink_component);
66587c9844SKuninori Morimoto 
67587c9844SKuninori Morimoto /*
68db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
69db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
70db2a4165SFrank Mandarino  * between two audio tracks.
71db2a4165SFrank Mandarino  */
72db2a4165SFrank Mandarino static int pmdown_time = 5000;
73db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
74db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
75db2a4165SFrank Mandarino 
762c7b696aSMarcel Ziswiler /*
772c7b696aSMarcel Ziswiler  * If a DMI filed contain strings in this blacklist (e.g.
7898faf436SMengdong Lin  * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
7998faf436SMengdong Lin  * as invalid and dropped when setting the card long name from DMI info.
8098faf436SMengdong Lin  */
8198faf436SMengdong Lin static const char * const dmi_blacklist[] = {
8298faf436SMengdong Lin 	"To be filled by OEM",
8398faf436SMengdong Lin 	"TBD by OEM",
8498faf436SMengdong Lin 	"Default String",
8598faf436SMengdong Lin 	"Board Manufacturer",
8698faf436SMengdong Lin 	"Board Vendor Name",
8798faf436SMengdong Lin 	"Board Product Name",
8898faf436SMengdong Lin 	NULL,	/* terminator */
8998faf436SMengdong Lin };
9098faf436SMengdong Lin 
91dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
92dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
93dbe21408SMark Brown {
9436ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
95dbe21408SMark Brown 
96f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
97dbe21408SMark Brown }
98dbe21408SMark Brown 
99dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
100dbe21408SMark Brown 			       struct device_attribute *attr,
101dbe21408SMark Brown 			       const char *buf, size_t count)
102dbe21408SMark Brown {
10336ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
104c593b520SMark Brown 	int ret;
105dbe21408SMark Brown 
106b785a492SJingoo Han 	ret = kstrtol(buf, 10, &rtd->pmdown_time);
107c593b520SMark Brown 	if (ret)
108c593b520SMark Brown 		return ret;
109dbe21408SMark Brown 
110dbe21408SMark Brown 	return count;
111dbe21408SMark Brown }
112dbe21408SMark Brown 
113dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
114dbe21408SMark Brown 
115d29697dcSTakashi Iwai static struct attribute *soc_dev_attrs[] = {
116d29697dcSTakashi Iwai 	&dev_attr_pmdown_time.attr,
117d29697dcSTakashi Iwai 	NULL
118d29697dcSTakashi Iwai };
119d29697dcSTakashi Iwai 
120d29697dcSTakashi Iwai static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
121d29697dcSTakashi Iwai 				       struct attribute *attr, int idx)
122d29697dcSTakashi Iwai {
123d29697dcSTakashi Iwai 	struct device *dev = kobj_to_dev(kobj);
124d29697dcSTakashi Iwai 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
125d29697dcSTakashi Iwai 
126d29697dcSTakashi Iwai 	if (attr == &dev_attr_pmdown_time.attr)
127d29697dcSTakashi Iwai 		return attr->mode; /* always visible */
1283b6eed8dSKuninori Morimoto 	return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
129d29697dcSTakashi Iwai }
130d29697dcSTakashi Iwai 
131d29697dcSTakashi Iwai static const struct attribute_group soc_dapm_dev_group = {
132d29697dcSTakashi Iwai 	.attrs = soc_dapm_dev_attrs,
133d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
134d29697dcSTakashi Iwai };
135d29697dcSTakashi Iwai 
136f7e73b26SMark Brown static const struct attribute_group soc_dev_group = {
137d29697dcSTakashi Iwai 	.attrs = soc_dev_attrs,
138d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
139d29697dcSTakashi Iwai };
140d29697dcSTakashi Iwai 
141d29697dcSTakashi Iwai static const struct attribute_group *soc_dev_attr_groups[] = {
142d29697dcSTakashi Iwai 	&soc_dapm_dev_group,
143f7e73b26SMark Brown 	&soc_dev_group,
144d29697dcSTakashi Iwai 	NULL
145d29697dcSTakashi Iwai };
146d29697dcSTakashi Iwai 
1472624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
14881c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component)
149e73f3de5SRussell King {
1506553bf06SLars-Peter Clausen 	if (!component->card->debugfs_card_root)
1516553bf06SLars-Peter Clausen 		return;
1526553bf06SLars-Peter Clausen 
15381c7cfd1SLars-Peter Clausen 	if (component->debugfs_prefix) {
15481c7cfd1SLars-Peter Clausen 		char *name;
155e73f3de5SRussell King 
15681c7cfd1SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s:%s",
15781c7cfd1SLars-Peter Clausen 			component->debugfs_prefix, component->name);
15881c7cfd1SLars-Peter Clausen 		if (name) {
15981c7cfd1SLars-Peter Clausen 			component->debugfs_root = debugfs_create_dir(name,
16081c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
16181c7cfd1SLars-Peter Clausen 			kfree(name);
16281c7cfd1SLars-Peter Clausen 		}
16381c7cfd1SLars-Peter Clausen 	} else {
16481c7cfd1SLars-Peter Clausen 		component->debugfs_root = debugfs_create_dir(component->name,
16581c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
166e73f3de5SRussell King 	}
167e73f3de5SRussell King 
16881c7cfd1SLars-Peter Clausen 	if (!component->debugfs_root) {
16981c7cfd1SLars-Peter Clausen 		dev_warn(component->dev,
17081c7cfd1SLars-Peter Clausen 			"ASoC: Failed to create component debugfs directory\n");
1712624d5faSMark Brown 		return;
1722624d5faSMark Brown 	}
1732624d5faSMark Brown 
17481c7cfd1SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
17581c7cfd1SLars-Peter Clausen 		component->debugfs_root);
17681c7cfd1SLars-Peter Clausen }
17781c7cfd1SLars-Peter Clausen 
17881c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
17981c7cfd1SLars-Peter Clausen {
18081c7cfd1SLars-Peter Clausen 	debugfs_remove_recursive(component->debugfs_root);
18181c7cfd1SLars-Peter Clausen }
18281c7cfd1SLars-Peter Clausen 
183c15b2a1dSPeng Donglin static int dai_list_show(struct seq_file *m, void *v)
184f3208780SMark Brown {
1851438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
186f3208780SMark Brown 	struct snd_soc_dai *dai;
187f3208780SMark Brown 
18834e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
18934e81ab4SLars-Peter Clausen 
190368dee94SKuninori Morimoto 	for_each_component(component)
19115a0c645SKuninori Morimoto 		for_each_component_dais(component, dai)
192700c17caSDonglin Peng 			seq_printf(m, "%s\n", dai->name);
193f3208780SMark Brown 
19434e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
19534e81ab4SLars-Peter Clausen 
196700c17caSDonglin Peng 	return 0;
197700c17caSDonglin Peng }
198c15b2a1dSPeng Donglin DEFINE_SHOW_ATTRIBUTE(dai_list);
199f3208780SMark Brown 
200db795f9bSKuninori Morimoto static int component_list_show(struct seq_file *m, void *v)
201db795f9bSKuninori Morimoto {
202db795f9bSKuninori Morimoto 	struct snd_soc_component *component;
203db795f9bSKuninori Morimoto 
204db795f9bSKuninori Morimoto 	mutex_lock(&client_mutex);
205db795f9bSKuninori Morimoto 
206368dee94SKuninori Morimoto 	for_each_component(component)
207db795f9bSKuninori Morimoto 		seq_printf(m, "%s\n", component->name);
208db795f9bSKuninori Morimoto 
209db795f9bSKuninori Morimoto 	mutex_unlock(&client_mutex);
210db795f9bSKuninori Morimoto 
211db795f9bSKuninori Morimoto 	return 0;
212db795f9bSKuninori Morimoto }
213db795f9bSKuninori Morimoto DEFINE_SHOW_ATTRIBUTE(component_list);
214db795f9bSKuninori Morimoto 
215a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
216a6052154SJarkko Nikula {
2176553bf06SLars-Peter Clausen 	if (!snd_soc_debugfs_root)
2186553bf06SLars-Peter Clausen 		return;
2196553bf06SLars-Peter Clausen 
220a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
2218a9dab1aSMark Brown 						     snd_soc_debugfs_root);
2223a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
223a6052154SJarkko Nikula 		dev_warn(card->dev,
2247c08be84SLothar Waßmann 			 "ASoC: Failed to create card debugfs directory\n");
2253a45b867SJarkko Nikula 		return;
2263a45b867SJarkko Nikula 	}
2273a45b867SJarkko Nikula 
2283a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
2293a45b867SJarkko Nikula 						    card->debugfs_card_root,
2303a45b867SJarkko Nikula 						    &card->pop_time);
2313a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
2323a45b867SJarkko Nikula 		dev_warn(card->dev,
233f110bfc7SLiam Girdwood 			 "ASoC: Failed to create pop time debugfs file\n");
234a6052154SJarkko Nikula }
235a6052154SJarkko Nikula 
236a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
237a6052154SJarkko Nikula {
23829040d1aSKuninori Morimoto 	if (!card->debugfs_card_root)
23929040d1aSKuninori Morimoto 		return;
240a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
24129040d1aSKuninori Morimoto 	card->debugfs_card_root = NULL;
242a6052154SJarkko Nikula }
243a6052154SJarkko Nikula 
2446553bf06SLars-Peter Clausen static void snd_soc_debugfs_init(void)
2456553bf06SLars-Peter Clausen {
2466553bf06SLars-Peter Clausen 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
247d9a02c55SFabio Estevam 	if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) {
2486553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create debugfs directory\n");
2496553bf06SLars-Peter Clausen 		snd_soc_debugfs_root = NULL;
2506553bf06SLars-Peter Clausen 		return;
2516553bf06SLars-Peter Clausen 	}
2526553bf06SLars-Peter Clausen 
2536553bf06SLars-Peter Clausen 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
2546553bf06SLars-Peter Clausen 				 &dai_list_fops))
2556553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
256db795f9bSKuninori Morimoto 
257db795f9bSKuninori Morimoto 	if (!debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
258db795f9bSKuninori Morimoto 				 &component_list_fops))
259db795f9bSKuninori Morimoto 		pr_warn("ASoC: Failed to create component list debugfs file\n");
2606553bf06SLars-Peter Clausen }
2616553bf06SLars-Peter Clausen 
2626553bf06SLars-Peter Clausen static void snd_soc_debugfs_exit(void)
2636553bf06SLars-Peter Clausen {
2646553bf06SLars-Peter Clausen 	debugfs_remove_recursive(snd_soc_debugfs_root);
2656553bf06SLars-Peter Clausen }
2666553bf06SLars-Peter Clausen 
2672624d5faSMark Brown #else
2682624d5faSMark Brown 
26981c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs(
27081c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
2712624d5faSMark Brown {
2722624d5faSMark Brown }
2732624d5faSMark Brown 
27481c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs(
27581c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
276731f1ab2SSebastien Guiriec {
277731f1ab2SSebastien Guiriec }
278731f1ab2SSebastien Guiriec 
279b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
280b95fccbcSAxel Lin {
281b95fccbcSAxel Lin }
282b95fccbcSAxel Lin 
283b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
284b95fccbcSAxel Lin {
285b95fccbcSAxel Lin }
2866553bf06SLars-Peter Clausen 
2876553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_init(void)
2886553bf06SLars-Peter Clausen {
2896553bf06SLars-Peter Clausen }
2906553bf06SLars-Peter Clausen 
2916553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_exit(void)
2926553bf06SLars-Peter Clausen {
2936553bf06SLars-Peter Clausen }
2946553bf06SLars-Peter Clausen 
2952624d5faSMark Brown #endif
2962624d5faSMark Brown 
297a0ac4411SKuninori Morimoto static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
298a0ac4411SKuninori Morimoto 			      struct snd_soc_component *component)
299a0ac4411SKuninori Morimoto {
300a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
301a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *new_rtdcom;
302a0ac4411SKuninori Morimoto 
303a0ac4411SKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
304a0ac4411SKuninori Morimoto 		/* already connected */
305a0ac4411SKuninori Morimoto 		if (rtdcom->component == component)
306a0ac4411SKuninori Morimoto 			return 0;
307a0ac4411SKuninori Morimoto 	}
308a0ac4411SKuninori Morimoto 
309a0ac4411SKuninori Morimoto 	new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
310a0ac4411SKuninori Morimoto 	if (!new_rtdcom)
311a0ac4411SKuninori Morimoto 		return -ENOMEM;
312a0ac4411SKuninori Morimoto 
313a0ac4411SKuninori Morimoto 	new_rtdcom->component = component;
314a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&new_rtdcom->list);
315a0ac4411SKuninori Morimoto 
316a0ac4411SKuninori Morimoto 	list_add_tail(&new_rtdcom->list, &rtd->component_list);
317a0ac4411SKuninori Morimoto 
318a0ac4411SKuninori Morimoto 	return 0;
319a0ac4411SKuninori Morimoto }
320a0ac4411SKuninori Morimoto 
321a0ac4411SKuninori Morimoto static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
322a0ac4411SKuninori Morimoto {
323a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
324a0ac4411SKuninori Morimoto 
325a0ac4411SKuninori Morimoto 	for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
326a0ac4411SKuninori Morimoto 		kfree(rtdcom1);
327a0ac4411SKuninori Morimoto 
328a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
329a0ac4411SKuninori Morimoto }
330a0ac4411SKuninori Morimoto 
331a0ac4411SKuninori Morimoto struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
332a0ac4411SKuninori Morimoto 						const char *driver_name)
333a0ac4411SKuninori Morimoto {
334a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
335a0ac4411SKuninori Morimoto 
336971da24cSKuninori Morimoto 	if (!driver_name)
337971da24cSKuninori Morimoto 		return NULL;
338971da24cSKuninori Morimoto 
339a0ac4411SKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
340971da24cSKuninori Morimoto 		const char *component_name = rtdcom->component->driver->name;
341971da24cSKuninori Morimoto 
342971da24cSKuninori Morimoto 		if (!component_name)
343971da24cSKuninori Morimoto 			continue;
344971da24cSKuninori Morimoto 
345971da24cSKuninori Morimoto 		if ((component_name == driver_name) ||
346971da24cSKuninori Morimoto 		    strcmp(component_name, driver_name) == 0)
347a0ac4411SKuninori Morimoto 			return rtdcom->component;
348a0ac4411SKuninori Morimoto 	}
349a0ac4411SKuninori Morimoto 
350a0ac4411SKuninori Morimoto 	return NULL;
351a0ac4411SKuninori Morimoto }
352031734b7SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
353a0ac4411SKuninori Morimoto 
35447c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
35547c88fffSLiam Girdwood 		const char *dai_link, int stream)
35647c88fffSLiam Girdwood {
3571a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
35847c88fffSLiam Girdwood 
359bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
3601a497983SMengdong Lin 		if (rtd->dai_link->no_pcm &&
3611a497983SMengdong Lin 			!strcmp(rtd->dai_link->name, dai_link))
3621a497983SMengdong Lin 			return rtd->pcm->streams[stream].substream;
36347c88fffSLiam Girdwood 	}
364f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
36547c88fffSLiam Girdwood 	return NULL;
36647c88fffSLiam Girdwood }
36747c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
36847c88fffSLiam Girdwood 
36975ab9eb6SKuninori Morimoto static const struct snd_soc_ops null_snd_soc_ops;
37075ab9eb6SKuninori Morimoto 
3711a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
3721a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
3731a497983SMengdong Lin {
3741a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
3751a497983SMengdong Lin 
3761a497983SMengdong Lin 	rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
3771a497983SMengdong Lin 	if (!rtd)
3781a497983SMengdong Lin 		return NULL;
3791a497983SMengdong Lin 
380a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
3811a497983SMengdong Lin 	rtd->card = card;
3821a497983SMengdong Lin 	rtd->dai_link = dai_link;
38375ab9eb6SKuninori Morimoto 	if (!rtd->dai_link->ops)
38475ab9eb6SKuninori Morimoto 		rtd->dai_link->ops = &null_snd_soc_ops;
38575ab9eb6SKuninori Morimoto 
3866396bb22SKees Cook 	rtd->codec_dais = kcalloc(dai_link->num_codecs,
3876396bb22SKees Cook 					sizeof(struct snd_soc_dai *),
3881a497983SMengdong Lin 					GFP_KERNEL);
3891a497983SMengdong Lin 	if (!rtd->codec_dais) {
3901a497983SMengdong Lin 		kfree(rtd);
3911a497983SMengdong Lin 		return NULL;
3921a497983SMengdong Lin 	}
3931a497983SMengdong Lin 
3941a497983SMengdong Lin 	return rtd;
3951a497983SMengdong Lin }
3961a497983SMengdong Lin 
3971a497983SMengdong Lin static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
3981a497983SMengdong Lin {
3991a497983SMengdong Lin 	kfree(rtd->codec_dais);
400a0ac4411SKuninori Morimoto 	snd_soc_rtdcom_del_all(rtd);
4011a497983SMengdong Lin 	kfree(rtd);
4021a497983SMengdong Lin }
4031a497983SMengdong Lin 
4041a497983SMengdong Lin static void soc_add_pcm_runtime(struct snd_soc_card *card,
4051a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd)
4061a497983SMengdong Lin {
4071a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
4081a497983SMengdong Lin 	rtd->num = card->num_rtd;
4091a497983SMengdong Lin 	card->num_rtd++;
4101a497983SMengdong Lin }
4111a497983SMengdong Lin 
4121a497983SMengdong Lin static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
4131a497983SMengdong Lin {
4141a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, *_rtd;
4151a497983SMengdong Lin 
416bcb1fd1fSKuninori Morimoto 	for_each_card_rtds_safe(card, rtd, _rtd) {
4171a497983SMengdong Lin 		list_del(&rtd->list);
4181a497983SMengdong Lin 		soc_free_pcm_runtime(rtd);
4191a497983SMengdong Lin 	}
4201a497983SMengdong Lin 
4211a497983SMengdong Lin 	card->num_rtd = 0;
4221a497983SMengdong Lin }
4231a497983SMengdong Lin 
42447c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
42547c88fffSLiam Girdwood 		const char *dai_link)
42647c88fffSLiam Girdwood {
4271a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
42847c88fffSLiam Girdwood 
429bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4301a497983SMengdong Lin 		if (!strcmp(rtd->dai_link->name, dai_link))
4311a497983SMengdong Lin 			return rtd;
43247c88fffSLiam Girdwood 	}
433f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
43447c88fffSLiam Girdwood 	return NULL;
43547c88fffSLiam Girdwood }
43647c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
43747c88fffSLiam Girdwood 
43865462e44SKuninori Morimoto static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
43965462e44SKuninori Morimoto {
44065462e44SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
44165462e44SKuninori Morimoto 
44265462e44SKuninori Morimoto 	for_each_card_rtds(card, rtd)
44365462e44SKuninori Morimoto 		flush_delayed_work(&rtd->delayed_work);
44465462e44SKuninori Morimoto }
44565462e44SKuninori Morimoto 
4469d58a077SRichard Fitzgerald static void codec2codec_close_delayed_work(struct work_struct *work)
4479d58a077SRichard Fitzgerald {
4482c7b696aSMarcel Ziswiler 	/*
4492c7b696aSMarcel Ziswiler 	 * Currently nothing to do for c2c links
4509d58a077SRichard Fitzgerald 	 * Since c2c links are internal nodes in the DAPM graph and
4519d58a077SRichard Fitzgerald 	 * don't interface with the outside world or application layer
4529d58a077SRichard Fitzgerald 	 * we don't have to do any special handling on close.
4539d58a077SRichard Fitzgerald 	 */
4549d58a077SRichard Fitzgerald }
4559d58a077SRichard Fitzgerald 
4566f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
457db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
4586f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
459db2a4165SFrank Mandarino {
4606f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
461d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
4621a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
4631a497983SMengdong Lin 	int i;
464db2a4165SFrank Mandarino 
465c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
466c5599b87SLars-Peter Clausen 	if (!card->instantiated)
467e3509ff0SDaniel Mack 		return 0;
468e3509ff0SDaniel Mack 
4692c7b696aSMarcel Ziswiler 	/*
4702c7b696aSMarcel Ziswiler 	 * Due to the resume being scheduled into a workqueue we could
4716ed25978SAndy Green 	 * suspend before that's finished - wait for it to complete.
4726ed25978SAndy Green 	 */
473f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
4746ed25978SAndy Green 
4756ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
476f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
4776ed25978SAndy Green 
478a00f90f9SMark Brown 	/* mute any active DACs */
479bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4800b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
4813efab7dcSMark Brown 
4821a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4833efab7dcSMark Brown 			continue;
4843efab7dcSMark Brown 
4850b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
48688bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
48788bd870fSBenoit Cousson 
488f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
489f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 1);
490db2a4165SFrank Mandarino 		}
49188bd870fSBenoit Cousson 	}
492db2a4165SFrank Mandarino 
4934ccab3e7SLiam Girdwood 	/* suspend all pcms */
494bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4951a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4963efab7dcSMark Brown 			continue;
4973efab7dcSMark Brown 
4981a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
4993efab7dcSMark Brown 	}
5004ccab3e7SLiam Girdwood 
50187506549SMark Brown 	if (card->suspend_pre)
50270b2ac12SMark Brown 		card->suspend_pre(card);
503db2a4165SFrank Mandarino 
504bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5051a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
5063efab7dcSMark Brown 
5071a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5083efab7dcSMark Brown 			continue;
5093efab7dcSMark Brown 
510bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
511f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
512db2a4165SFrank Mandarino 	}
513db2a4165SFrank Mandarino 
51437660b6dSLars-Peter Clausen 	/* close any waiting streams */
51565462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
516db2a4165SFrank Mandarino 
517bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5183efab7dcSMark Brown 
5191a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5203efab7dcSMark Brown 			continue;
5213efab7dcSMark Brown 
5221a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
5237bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
524db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
525f0fba2adSLiam Girdwood 
5261a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
5277bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
528db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
529db2a4165SFrank Mandarino 	}
530db2a4165SFrank Mandarino 
5318be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
5328be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
533e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
534e2d32ff6SMark Brown 
5359178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
536f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
5372c7b696aSMarcel Ziswiler 		struct snd_soc_dapm_context *dapm =
5382c7b696aSMarcel Ziswiler 				snd_soc_component_get_dapm(component);
539d9fc4063SKuninori Morimoto 
5402c7b696aSMarcel Ziswiler 		/*
5412c7b696aSMarcel Ziswiler 		 * If there are paths active then the COMPONENT will be held
5422c7b696aSMarcel Ziswiler 		 * with bias _ON and should not be suspended.
5432c7b696aSMarcel Ziswiler 		 */
5449178feb4SKuninori Morimoto 		if (!component->suspended) {
5454890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
5461547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
547125a25daSMark Brown 				/*
5489178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
549125a25daSMark Brown 				 * bias off then being in STANDBY
550125a25daSMark Brown 				 * means it's doing something,
551125a25daSMark Brown 				 * otherwise fall through.
552125a25daSMark Brown 				 */
5534890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
5549178feb4SKuninori Morimoto 					dev_dbg(component->dev,
55510e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
556125a25daSMark Brown 					break;
557125a25daSMark Brown 				}
5581a12d5dcSGustavo A. R. Silva 				/* fall through */
559a8093297SLars-Peter Clausen 
5601547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
561999f7f5aSKuninori Morimoto 				if (component->driver->suspend)
562999f7f5aSKuninori Morimoto 					component->driver->suspend(component);
5639178feb4SKuninori Morimoto 				component->suspended = 1;
5649178feb4SKuninori Morimoto 				if (component->regmap)
5659178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
566988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
5679178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
5681547aba9SMark Brown 				break;
5691547aba9SMark Brown 			default:
5709178feb4SKuninori Morimoto 				dev_dbg(component->dev,
5719178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
5721547aba9SMark Brown 				break;
5731547aba9SMark Brown 			}
5741547aba9SMark Brown 		}
575f0fba2adSLiam Girdwood 	}
576db2a4165SFrank Mandarino 
577bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5781a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
5793efab7dcSMark Brown 
5801a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5813efab7dcSMark Brown 			continue;
5823efab7dcSMark Brown 
583bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
584f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
585988e8cc4SNicolin Chen 
586988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
587988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
588db2a4165SFrank Mandarino 	}
589db2a4165SFrank Mandarino 
59087506549SMark Brown 	if (card->suspend_post)
59170b2ac12SMark Brown 		card->suspend_post(card);
592db2a4165SFrank Mandarino 
593db2a4165SFrank Mandarino 	return 0;
594db2a4165SFrank Mandarino }
5956f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
596db2a4165SFrank Mandarino 
5972c7b696aSMarcel Ziswiler /*
5982c7b696aSMarcel Ziswiler  * deferred resume work, so resume can complete before we finished
5996ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6006ed25978SAndy Green  */
6016ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
602db2a4165SFrank Mandarino {
603f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
6042c7b696aSMarcel Ziswiler 			container_of(work, struct snd_soc_card,
6052c7b696aSMarcel Ziswiler 				     deferred_resume_work);
6061a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
607d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
6081a497983SMengdong Lin 	int i;
609db2a4165SFrank Mandarino 
6102c7b696aSMarcel Ziswiler 	/*
6112c7b696aSMarcel Ziswiler 	 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6126ed25978SAndy Green 	 * so userspace apps are blocked from touching us
6136ed25978SAndy Green 	 */
6146ed25978SAndy Green 
615f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
6166ed25978SAndy Green 
6179949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
618f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
6199949788bSMark Brown 
62087506549SMark Brown 	if (card->resume_pre)
62170b2ac12SMark Brown 		card->resume_pre(card);
622db2a4165SFrank Mandarino 
623bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
624bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6251a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6263efab7dcSMark Brown 
6271a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6283efab7dcSMark Brown 			continue;
6293efab7dcSMark Brown 
630bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
631f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
632db2a4165SFrank Mandarino 	}
633db2a4165SFrank Mandarino 
634f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
6359178feb4SKuninori Morimoto 		if (component->suspended) {
636999f7f5aSKuninori Morimoto 			if (component->driver->resume)
637999f7f5aSKuninori Morimoto 				component->driver->resume(component);
6389178feb4SKuninori Morimoto 			component->suspended = 0;
6391547aba9SMark Brown 		}
640f0fba2adSLiam Girdwood 	}
641db2a4165SFrank Mandarino 
642bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6433efab7dcSMark Brown 
6441a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6453efab7dcSMark Brown 			continue;
6463efab7dcSMark Brown 
6471a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
648d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
649db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
650f0fba2adSLiam Girdwood 
6511a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
652d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
653db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
654db2a4165SFrank Mandarino 	}
655db2a4165SFrank Mandarino 
6563ff3f64bSMark Brown 	/* unmute any active DACs */
657bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6580b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
6593efab7dcSMark Brown 
6601a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6613efab7dcSMark Brown 			continue;
6623efab7dcSMark Brown 
6630b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
66488bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
66588bd870fSBenoit Cousson 
666f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
667f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 0);
668db2a4165SFrank Mandarino 		}
66988bd870fSBenoit Cousson 	}
670db2a4165SFrank Mandarino 
671bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6721a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6733efab7dcSMark Brown 
6741a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6753efab7dcSMark Brown 			continue;
6763efab7dcSMark Brown 
677bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
678f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
679db2a4165SFrank Mandarino 	}
680db2a4165SFrank Mandarino 
68187506549SMark Brown 	if (card->resume_post)
68270b2ac12SMark Brown 		card->resume_post(card);
683db2a4165SFrank Mandarino 
684f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
6856ed25978SAndy Green 
6868be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
6878be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
688e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
6891a7aaa58SJeeja KP 
6901a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
6911a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
6926ed25978SAndy Green }
6936ed25978SAndy Green 
6946ed25978SAndy Green /* powers up audio subsystem after a suspend */
6956f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
6966ed25978SAndy Green {
6976f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
698bc263214SLars-Peter Clausen 	bool bus_control = false;
6991a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
70022d251a5SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
70122d251a5SKuninori Morimoto 	int i;
702b9dd94a8SPeter Ujfalusi 
703c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
704c5599b87SLars-Peter Clausen 	if (!card->instantiated)
7055ff1ddf2SEric Miao 		return 0;
7065ff1ddf2SEric Miao 
707988e8cc4SNicolin Chen 	/* activate pins from sleep state */
708bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
70988bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
71088bd870fSBenoit Cousson 
711988e8cc4SNicolin Chen 		if (cpu_dai->active)
712988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
71388bd870fSBenoit Cousson 
71422d251a5SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
715988e8cc4SNicolin Chen 			if (codec_dai->active)
716988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
717988e8cc4SNicolin Chen 		}
71888bd870fSBenoit Cousson 	}
719988e8cc4SNicolin Chen 
720bc263214SLars-Peter Clausen 	/*
721bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
722bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
723bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
72464ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
72564ab9baaSMark Brown 	 */
726bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7271a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7282c7b696aSMarcel Ziswiler 
729bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
73082e14e8bSStephen Warren 	}
731bc263214SLars-Peter Clausen 	if (bus_control) {
732bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
73364ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
73464ab9baaSMark Brown 	} else {
735f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
7366308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
737f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
738f0fba2adSLiam Girdwood 	}
7396ed25978SAndy Green 
740db2a4165SFrank Mandarino 	return 0;
741db2a4165SFrank Mandarino }
7426f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
743db2a4165SFrank Mandarino #else
7446f8ab4acSMark Brown #define snd_soc_suspend NULL
7456f8ab4acSMark Brown #define snd_soc_resume NULL
746db2a4165SFrank Mandarino #endif
747db2a4165SFrank Mandarino 
74885e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
74902a06d30SBarry Song };
75002a06d30SBarry Song 
751c0834440SKuninori Morimoto static struct device_node
752c0834440SKuninori Morimoto *soc_component_to_node(struct snd_soc_component *component)
753c0834440SKuninori Morimoto {
754c0834440SKuninori Morimoto 	struct device_node *of_node;
755c0834440SKuninori Morimoto 
756c0834440SKuninori Morimoto 	of_node = component->dev->of_node;
757c0834440SKuninori Morimoto 	if (!of_node && component->dev->parent)
758c0834440SKuninori Morimoto 		of_node = component->dev->parent->of_node;
759c0834440SKuninori Morimoto 
760c0834440SKuninori Morimoto 	return of_node;
761c0834440SKuninori Morimoto }
762c0834440SKuninori Morimoto 
76365d9361fSLars-Peter Clausen static struct snd_soc_component *soc_find_component(
76465d9361fSLars-Peter Clausen 	const struct device_node *of_node, const char *name)
76512023a9aSMisael Lopez Cruz {
76665d9361fSLars-Peter Clausen 	struct snd_soc_component *component;
767d0b95e6cSCharles Keepax 	struct device_node *component_of_node;
76812023a9aSMisael Lopez Cruz 
76934e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
77034e81ab4SLars-Peter Clausen 
771368dee94SKuninori Morimoto 	for_each_component(component) {
77265d9361fSLars-Peter Clausen 		if (of_node) {
773c0834440SKuninori Morimoto 			component_of_node = soc_component_to_node(component);
774d0b95e6cSCharles Keepax 
775d0b95e6cSCharles Keepax 			if (component_of_node == of_node)
77665d9361fSLars-Peter Clausen 				return component;
7775a7b2aabSMark Brown 		} else if (name && strcmp(component->name, name) == 0) {
77865d9361fSLars-Peter Clausen 			return component;
77912023a9aSMisael Lopez Cruz 		}
78012023a9aSMisael Lopez Cruz 	}
78112023a9aSMisael Lopez Cruz 
78212023a9aSMisael Lopez Cruz 	return NULL;
78312023a9aSMisael Lopez Cruz }
78412023a9aSMisael Lopez Cruz 
785be6ac0a9SKuninori Morimoto static int snd_soc_is_matching_component(
786be6ac0a9SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc,
787be6ac0a9SKuninori Morimoto 	struct snd_soc_component *component)
788be6ac0a9SKuninori Morimoto {
789be6ac0a9SKuninori Morimoto 	struct device_node *component_of_node;
790be6ac0a9SKuninori Morimoto 
791c0834440SKuninori Morimoto 	component_of_node = soc_component_to_node(component);
792be6ac0a9SKuninori Morimoto 
793be6ac0a9SKuninori Morimoto 	if (dlc->of_node && component_of_node != dlc->of_node)
794be6ac0a9SKuninori Morimoto 		return 0;
795be6ac0a9SKuninori Morimoto 	if (dlc->name && strcmp(component->name, dlc->name))
796be6ac0a9SKuninori Morimoto 		return 0;
797be6ac0a9SKuninori Morimoto 
798be6ac0a9SKuninori Morimoto 	return 1;
799be6ac0a9SKuninori Morimoto }
800be6ac0a9SKuninori Morimoto 
801fbb88b5cSMengdong Lin /**
802fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
803fbb88b5cSMengdong Lin  *
8044958471bSJeffy Chen  * @dlc: name of the DAI or the DAI driver and optional component info to match
805fbb88b5cSMengdong Lin  *
806ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
807fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
808fbb88b5cSMengdong Lin  * should also match if being specified.
809fbb88b5cSMengdong Lin  *
810fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
811fbb88b5cSMengdong Lin  */
812305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
81314621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
81412023a9aSMisael Lopez Cruz {
81514621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
81614621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
81712023a9aSMisael Lopez Cruz 
81834e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
81934e81ab4SLars-Peter Clausen 
82014621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs */
821368dee94SKuninori Morimoto 	for_each_component(component) {
822be6ac0a9SKuninori Morimoto 		if (!snd_soc_is_matching_component(dlc, component))
82312023a9aSMisael Lopez Cruz 			continue;
82415a0c645SKuninori Morimoto 		for_each_component_dais(component, dai) {
8254958471bSJeffy Chen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
8266a6dafdaSJeffy Chen 			    && (!dai->driver->name
8276a6dafdaSJeffy Chen 				|| strcmp(dai->driver->name, dlc->dai_name)))
82814621c7eSLars-Peter Clausen 				continue;
82912023a9aSMisael Lopez Cruz 
83014621c7eSLars-Peter Clausen 			return dai;
83112023a9aSMisael Lopez Cruz 		}
83212023a9aSMisael Lopez Cruz 	}
83312023a9aSMisael Lopez Cruz 
83412023a9aSMisael Lopez Cruz 	return NULL;
83512023a9aSMisael Lopez Cruz }
836305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
83712023a9aSMisael Lopez Cruz 
83817fb1755SMengdong Lin /**
83917fb1755SMengdong Lin  * snd_soc_find_dai_link - Find a DAI link
84017fb1755SMengdong Lin  *
84117fb1755SMengdong Lin  * @card: soc card
84217fb1755SMengdong Lin  * @id: DAI link ID to match
84317fb1755SMengdong Lin  * @name: DAI link name to match, optional
8448abab35fSCharles Keepax  * @stream_name: DAI link stream name to match, optional
84517fb1755SMengdong Lin  *
84617fb1755SMengdong Lin  * This function will search all existing DAI links of the soc card to
84717fb1755SMengdong Lin  * find the link of the same ID. Since DAI links may not have their
84817fb1755SMengdong Lin  * unique ID, so name and stream name should also match if being
84917fb1755SMengdong Lin  * specified.
85017fb1755SMengdong Lin  *
85117fb1755SMengdong Lin  * Return: pointer of DAI link, or NULL if not found.
85217fb1755SMengdong Lin  */
85317fb1755SMengdong Lin struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
85417fb1755SMengdong Lin 					       int id, const char *name,
85517fb1755SMengdong Lin 					       const char *stream_name)
85617fb1755SMengdong Lin {
85717fb1755SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
85817fb1755SMengdong Lin 
85917fb1755SMengdong Lin 	lockdep_assert_held(&client_mutex);
86017fb1755SMengdong Lin 
86198061fdbSKuninori Morimoto 	for_each_card_links_safe(card, link, _link) {
86217fb1755SMengdong Lin 		if (link->id != id)
86317fb1755SMengdong Lin 			continue;
86417fb1755SMengdong Lin 
86517fb1755SMengdong Lin 		if (name && (!link->name || strcmp(name, link->name)))
86617fb1755SMengdong Lin 			continue;
86717fb1755SMengdong Lin 
86817fb1755SMengdong Lin 		if (stream_name && (!link->stream_name
86917fb1755SMengdong Lin 			|| strcmp(stream_name, link->stream_name)))
87017fb1755SMengdong Lin 			continue;
87117fb1755SMengdong Lin 
87217fb1755SMengdong Lin 		return link;
87317fb1755SMengdong Lin 	}
87417fb1755SMengdong Lin 
87517fb1755SMengdong Lin 	return NULL;
87617fb1755SMengdong Lin }
87717fb1755SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
87817fb1755SMengdong Lin 
87949a5ba1cSMengdong Lin static bool soc_is_dai_link_bound(struct snd_soc_card *card,
88049a5ba1cSMengdong Lin 		struct snd_soc_dai_link *dai_link)
881db2a4165SFrank Mandarino {
88249a5ba1cSMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
88349a5ba1cSMengdong Lin 
884bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
88549a5ba1cSMengdong Lin 		if (rtd->dai_link == dai_link)
88649a5ba1cSMengdong Lin 			return true;
88749a5ba1cSMengdong Lin 	}
88849a5ba1cSMengdong Lin 
88949a5ba1cSMengdong Lin 	return false;
89049a5ba1cSMengdong Lin }
89149a5ba1cSMengdong Lin 
8926f2f1ff0SMengdong Lin static int soc_bind_dai_link(struct snd_soc_card *card,
8936f2f1ff0SMengdong Lin 	struct snd_soc_dai_link *dai_link)
894db2a4165SFrank Mandarino {
8951a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
896720734a0SKuninori Morimoto 	struct snd_soc_dai_link_component *codecs;
89790be711eSKuninori Morimoto 	struct snd_soc_component *component;
89888bd870fSBenoit Cousson 	int i;
899db2a4165SFrank Mandarino 
900a655de80SLiam Girdwood 	if (dai_link->ignore)
901a655de80SLiam Girdwood 		return 0;
902a655de80SLiam Girdwood 
9036f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
9046308419aSMark Brown 
90549a5ba1cSMengdong Lin 	if (soc_is_dai_link_bound(card, dai_link)) {
90649a5ba1cSMengdong Lin 		dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
90749a5ba1cSMengdong Lin 			dai_link->name);
90849a5ba1cSMengdong Lin 		return 0;
90949a5ba1cSMengdong Lin 	}
910db2a4165SFrank Mandarino 
911513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
912513cb311SSudip Mukherjee 	if (!rtd)
913513cb311SSudip Mukherjee 		return -ENOMEM;
914513cb311SSudip Mukherjee 
91508a5841eSKuninori Morimoto 	/* FIXME: we need multi CPU support in the future */
91608a5841eSKuninori Morimoto 	rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
917b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
9186b490879SMartin Hundebøll 		dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
91908a5841eSKuninori Morimoto 			 dai_link->cpus->dai_name);
9201a497983SMengdong Lin 		goto _err_defer;
921c5af3a2eSMark Brown 	}
92290be711eSKuninori Morimoto 	snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
923c5af3a2eSMark Brown 
92488bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
925e2b30edfSKuninori Morimoto 	rtd->num_codecs = dai_link->num_codecs;
926720734a0SKuninori Morimoto 	for_each_link_codecs(dai_link, i, codecs) {
9270a2cfcd9SKuninori Morimoto 		rtd->codec_dais[i] = snd_soc_find_dai(codecs);
9280a2cfcd9SKuninori Morimoto 		if (!rtd->codec_dais[i]) {
9297c7e2d6aSStefan Agner 			dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
930720734a0SKuninori Morimoto 				 codecs->dai_name);
9311a497983SMengdong Lin 			goto _err_defer;
93212023a9aSMisael Lopez Cruz 		}
9330a2cfcd9SKuninori Morimoto 		snd_soc_rtdcom_add(rtd, rtd->codec_dais[i]->component);
93488bd870fSBenoit Cousson 	}
93588bd870fSBenoit Cousson 
93688bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
9370a2cfcd9SKuninori Morimoto 	rtd->codec_dai = rtd->codec_dais[0];
93812023a9aSMisael Lopez Cruz 
939e2b30edfSKuninori Morimoto 	/* Find PLATFORM from registered PLATFORMs */
940368dee94SKuninori Morimoto 	for_each_component(component) {
941910fdcabSKuninori Morimoto 		if (!snd_soc_is_matching_component(dai_link->platforms,
942be6ac0a9SKuninori Morimoto 						   component))
94390be711eSKuninori Morimoto 			continue;
94490be711eSKuninori Morimoto 
94590be711eSKuninori Morimoto 		snd_soc_rtdcom_add(rtd, component);
94690be711eSKuninori Morimoto 	}
94790be711eSKuninori Morimoto 
9481a497983SMengdong Lin 	soc_add_pcm_runtime(card, rtd);
949b19e6e7bSMark Brown 	return 0;
9501a497983SMengdong Lin 
9511a497983SMengdong Lin _err_defer:
9521a497983SMengdong Lin 	soc_free_pcm_runtime(rtd);
9531a497983SMengdong Lin 	return -EPROBE_DEFER;
9546b05eda6SMark Brown }
9556b05eda6SMark Brown 
95622d14231SKuninori Morimoto static void soc_cleanup_component(struct snd_soc_component *component)
95722d14231SKuninori Morimoto {
9583bb936f5SAmadeusz Sławiński 	snd_soc_component_set_jack(component, NULL, NULL);
95922d14231SKuninori Morimoto 	list_del(&component->card_list);
96022d14231SKuninori Morimoto 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
96122d14231SKuninori Morimoto 	soc_cleanup_component_debugfs(component);
96222d14231SKuninori Morimoto 	component->card = NULL;
963b4ed6b51SRanjani Sridharan 	if (!component->driver->module_get_upon_open)
96422d14231SKuninori Morimoto 		module_put(component->dev->driver->owner);
96522d14231SKuninori Morimoto }
96622d14231SKuninori Morimoto 
967f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
968d12cd198SStephen Warren {
969abd31b32SLars-Peter Clausen 	if (!component->card)
97070090bbbSLars-Peter Clausen 		return;
971d12cd198SStephen Warren 
972999f7f5aSKuninori Morimoto 	if (component->driver->remove)
973999f7f5aSKuninori Morimoto 		component->driver->remove(component);
974d12cd198SStephen Warren 
97522d14231SKuninori Morimoto 	soc_cleanup_component(component);
976d12cd198SStephen Warren }
977d12cd198SStephen Warren 
978e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
979589c3563SJarkko Nikula {
980589c3563SJarkko Nikula 	int err;
981589c3563SJarkko Nikula 
98252abe6ccSGuennadi Liakhovetski 	if (!dai || !dai->probed || !dai->driver ||
9832eda3cb1SKuninori Morimoto 	    dai->driver->remove_order != order)
9842eda3cb1SKuninori Morimoto 		return;
9852eda3cb1SKuninori Morimoto 
986e60cd14fSLars-Peter Clausen 	if (dai->driver->remove) {
987e60cd14fSLars-Peter Clausen 		err = dai->driver->remove(dai);
988589c3563SJarkko Nikula 		if (err < 0)
989e60cd14fSLars-Peter Clausen 			dev_err(dai->dev,
990b0aa88afSMisael Lopez Cruz 				"ASoC: failed to remove %s: %d\n",
991e60cd14fSLars-Peter Clausen 				dai->name, err);
992b0aa88afSMisael Lopez Cruz 	}
993e60cd14fSLars-Peter Clausen 	dai->probed = 0;
994b0aa88afSMisael Lopez Cruz }
995b0aa88afSMisael Lopez Cruz 
9961a497983SMengdong Lin static void soc_remove_link_dais(struct snd_soc_card *card,
9971a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
998f0fba2adSLiam Girdwood {
999e60cd14fSLars-Peter Clausen 	int i;
10000b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1001f0fba2adSLiam Girdwood 
1002f0fba2adSLiam Girdwood 	/* unregister the rtd device */
1003f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
100436ae1a96SMark Brown 		device_unregister(rtd->dev);
1005f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
100602a06d30SBarry Song 	}
100702a06d30SBarry Song 
1008f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
10090b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
10100b7990e3SKuninori Morimoto 		soc_remove_dai(codec_dai, order);
1011f0fba2adSLiam Girdwood 
1012e60cd14fSLars-Peter Clausen 	soc_remove_dai(rtd->cpu_dai, order);
1013f0fba2adSLiam Girdwood }
1014f0fba2adSLiam Girdwood 
10151a497983SMengdong Lin static void soc_remove_link_components(struct snd_soc_card *card,
10161a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, int order)
101762ae68faSStephen Warren {
101861aca564SLars-Peter Clausen 	struct snd_soc_component *component;
101990be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
102062ae68faSStephen Warren 
102134ac3c3eSRanjani Sridharan 	mutex_lock(&client_mutex);
102290be711eSKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
102390be711eSKuninori Morimoto 		component = rtdcom->component;
102462ae68faSStephen Warren 
102570090bbbSLars-Peter Clausen 		if (component->driver->remove_order == order)
102661aca564SLars-Peter Clausen 			soc_remove_component(component);
102762ae68faSStephen Warren 	}
102834ac3c3eSRanjani Sridharan 	mutex_unlock(&client_mutex);
102962ae68faSStephen Warren }
103062ae68faSStephen Warren 
10310671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
10320671fd8eSKuninori Morimoto {
10331a497983SMengdong Lin 	int order;
10341a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
1035f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
10360671fd8eSKuninori Morimoto 
10371a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1038bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd)
10391a497983SMengdong Lin 			soc_remove_link_dais(card, rtd, order);
10400168bf0dSLiam Girdwood 	}
104162ae68faSStephen Warren 
10421a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1043bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd)
10441a497983SMengdong Lin 			soc_remove_link_components(card, rtd, order);
104562ae68faSStephen Warren 	}
104662ae68faSStephen Warren 
104798061fdbSKuninori Morimoto 	for_each_card_links_safe(card, link, _link) {
1048f8f80361SMengdong Lin 		if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1049f8f80361SMengdong Lin 			dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1050f8f80361SMengdong Lin 				link->name);
1051f8f80361SMengdong Lin 
1052f8f80361SMengdong Lin 		list_del(&link->list);
10530671fd8eSKuninori Morimoto 	}
10540671fd8eSKuninori Morimoto }
10550671fd8eSKuninori Morimoto 
1056*adb76b5bSKuninori Morimoto static struct snd_soc_dai_link_component dummy_link = COMP_DUMMY();
1057923c5e61SMengdong Lin 
1058923c5e61SMengdong Lin static int soc_init_dai_link(struct snd_soc_card *card,
1059923c5e61SMengdong Lin 			     struct snd_soc_dai_link *link)
1060923c5e61SMengdong Lin {
1061*adb76b5bSKuninori Morimoto 	int i;
10623db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *codec;
1063923c5e61SMengdong Lin 
1064*adb76b5bSKuninori Morimoto 	/* default Platform */
1065*adb76b5bSKuninori Morimoto 	if (!link->platforms || !link->num_platforms) {
1066*adb76b5bSKuninori Morimoto 		link->platforms = &dummy_link;
1067*adb76b5bSKuninori Morimoto 		link->num_platforms = 1;
1068923c5e61SMengdong Lin 	}
1069923c5e61SMengdong Lin 
10703db769f1SKuninori Morimoto 	for_each_link_codecs(link, i, codec) {
1071923c5e61SMengdong Lin 		/*
1072923c5e61SMengdong Lin 		 * Codec must be specified by 1 of name or OF node,
1073923c5e61SMengdong Lin 		 * not both or neither.
1074923c5e61SMengdong Lin 		 */
10753db769f1SKuninori Morimoto 		if (!!codec->name ==
10763db769f1SKuninori Morimoto 		    !!codec->of_node) {
1077923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1078923c5e61SMengdong Lin 				link->name);
1079923c5e61SMengdong Lin 			return -EINVAL;
1080923c5e61SMengdong Lin 		}
1081923c5e61SMengdong Lin 		/* Codec DAI name must be specified */
10823db769f1SKuninori Morimoto 		if (!codec->dai_name) {
1083923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1084923c5e61SMengdong Lin 				link->name);
1085923c5e61SMengdong Lin 			return -EINVAL;
1086923c5e61SMengdong Lin 		}
1087923c5e61SMengdong Lin 	}
1088923c5e61SMengdong Lin 
1089910fdcabSKuninori Morimoto 	/* FIXME */
1090910fdcabSKuninori Morimoto 	if (link->num_platforms > 1) {
1091910fdcabSKuninori Morimoto 		dev_err(card->dev,
1092910fdcabSKuninori Morimoto 			"ASoC: multi platform is not yet supported %s\n",
1093910fdcabSKuninori Morimoto 			link->name);
1094910fdcabSKuninori Morimoto 		return -EINVAL;
1095910fdcabSKuninori Morimoto 	}
1096910fdcabSKuninori Morimoto 
1097923c5e61SMengdong Lin 	/*
1098923c5e61SMengdong Lin 	 * Platform may be specified by either name or OF node, but
1099923c5e61SMengdong Lin 	 * can be left unspecified, and a dummy platform will be used.
1100923c5e61SMengdong Lin 	 */
1101910fdcabSKuninori Morimoto 	if (link->platforms->name && link->platforms->of_node) {
1102923c5e61SMengdong Lin 		dev_err(card->dev,
1103923c5e61SMengdong Lin 			"ASoC: Both platform name/of_node are set for %s\n",
1104923c5e61SMengdong Lin 			link->name);
1105923c5e61SMengdong Lin 		return -EINVAL;
1106923c5e61SMengdong Lin 	}
11078780cf11SAjit Pandey 
11088780cf11SAjit Pandey 	/*
11098780cf11SAjit Pandey 	 * Defer card registartion if platform dai component is not added to
11108780cf11SAjit Pandey 	 * component list.
11118780cf11SAjit Pandey 	 */
1112910fdcabSKuninori Morimoto 	if ((link->platforms->of_node || link->platforms->name) &&
1113910fdcabSKuninori Morimoto 	    !soc_find_component(link->platforms->of_node, link->platforms->name))
11148780cf11SAjit Pandey 		return -EPROBE_DEFER;
11158780cf11SAjit Pandey 
111608a5841eSKuninori Morimoto 	/* FIXME */
111708a5841eSKuninori Morimoto 	if (link->num_cpus > 1) {
111808a5841eSKuninori Morimoto 		dev_err(card->dev,
111908a5841eSKuninori Morimoto 			"ASoC: multi cpu is not yet supported %s\n",
112008a5841eSKuninori Morimoto 			link->name);
112108a5841eSKuninori Morimoto 		return -EINVAL;
112208a5841eSKuninori Morimoto 	}
112308a5841eSKuninori Morimoto 
1124923c5e61SMengdong Lin 	/*
1125923c5e61SMengdong Lin 	 * CPU device may be specified by either name or OF node, but
1126923c5e61SMengdong Lin 	 * can be left unspecified, and will be matched based on DAI
1127923c5e61SMengdong Lin 	 * name alone..
1128923c5e61SMengdong Lin 	 */
112908a5841eSKuninori Morimoto 	if (link->cpus->name && link->cpus->of_node) {
1130923c5e61SMengdong Lin 		dev_err(card->dev,
1131923c5e61SMengdong Lin 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
1132923c5e61SMengdong Lin 			link->name);
1133923c5e61SMengdong Lin 		return -EINVAL;
1134923c5e61SMengdong Lin 	}
11358780cf11SAjit Pandey 
11368780cf11SAjit Pandey 	/*
11378780cf11SAjit Pandey 	 * Defer card registartion if cpu dai component is not added to
11388780cf11SAjit Pandey 	 * component list.
11398780cf11SAjit Pandey 	 */
114008a5841eSKuninori Morimoto 	if ((link->cpus->of_node || link->cpus->name) &&
114108a5841eSKuninori Morimoto 	    !soc_find_component(link->cpus->of_node, link->cpus->name))
11428780cf11SAjit Pandey 		return -EPROBE_DEFER;
11438780cf11SAjit Pandey 
1144923c5e61SMengdong Lin 	/*
1145923c5e61SMengdong Lin 	 * At least one of CPU DAI name or CPU device name/node must be
1146923c5e61SMengdong Lin 	 * specified
1147923c5e61SMengdong Lin 	 */
114808a5841eSKuninori Morimoto 	if (!link->cpus->dai_name &&
114908a5841eSKuninori Morimoto 	    !(link->cpus->name || link->cpus->of_node)) {
1150923c5e61SMengdong Lin 		dev_err(card->dev,
1151923c5e61SMengdong Lin 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1152923c5e61SMengdong Lin 			link->name);
1153923c5e61SMengdong Lin 		return -EINVAL;
1154923c5e61SMengdong Lin 	}
1155923c5e61SMengdong Lin 
1156923c5e61SMengdong Lin 	return 0;
1157923c5e61SMengdong Lin }
1158923c5e61SMengdong Lin 
1159ef2e8175SKuninori Morimoto void snd_soc_disconnect_sync(struct device *dev)
1160ef2e8175SKuninori Morimoto {
11612c7b696aSMarcel Ziswiler 	struct snd_soc_component *component =
11622c7b696aSMarcel Ziswiler 			snd_soc_lookup_component(dev, NULL);
1163ef2e8175SKuninori Morimoto 
1164ef2e8175SKuninori Morimoto 	if (!component || !component->card)
1165ef2e8175SKuninori Morimoto 		return;
1166ef2e8175SKuninori Morimoto 
1167ef2e8175SKuninori Morimoto 	snd_card_disconnect_sync(component->card->snd_card);
1168ef2e8175SKuninori Morimoto }
1169df532185SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
1170ef2e8175SKuninori Morimoto 
1171f8f80361SMengdong Lin /**
1172f8f80361SMengdong Lin  * snd_soc_add_dai_link - Add a DAI link dynamically
1173f8f80361SMengdong Lin  * @card: The ASoC card to which the DAI link is added
1174f8f80361SMengdong Lin  * @dai_link: The new DAI link to add
1175f8f80361SMengdong Lin  *
1176f8f80361SMengdong Lin  * This function adds a DAI link to the ASoC card's link list.
1177f8f80361SMengdong Lin  *
1178f8f80361SMengdong Lin  * Note: Topology can use this API to add DAI links when probing the
1179f8f80361SMengdong Lin  * topology component. And machine drivers can still define static
1180f8f80361SMengdong Lin  * DAI links in dai_link array.
1181f8f80361SMengdong Lin  */
1182f8f80361SMengdong Lin int snd_soc_add_dai_link(struct snd_soc_card *card,
1183f8f80361SMengdong Lin 		struct snd_soc_dai_link *dai_link)
1184f8f80361SMengdong Lin {
1185f8f80361SMengdong Lin 	if (dai_link->dobj.type
1186f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1187f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1188f8f80361SMengdong Lin 			dai_link->dobj.type);
1189f8f80361SMengdong Lin 		return -EINVAL;
1190f8f80361SMengdong Lin 	}
1191f8f80361SMengdong Lin 
1192f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
11932c7b696aSMarcel Ziswiler 	/*
11942c7b696aSMarcel Ziswiler 	 * Notify the machine driver for extra initialization
1195d6f220eaSMengdong Lin 	 * on the link created by topology.
1196d6f220eaSMengdong Lin 	 */
1197d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->add_dai_link)
1198d6f220eaSMengdong Lin 		card->add_dai_link(card, dai_link);
1199d6f220eaSMengdong Lin 
1200f8f80361SMengdong Lin 	list_add_tail(&dai_link->list, &card->dai_link_list);
1201f8f80361SMengdong Lin 
1202f8f80361SMengdong Lin 	return 0;
1203f8f80361SMengdong Lin }
1204f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1205f8f80361SMengdong Lin 
1206f8f80361SMengdong Lin /**
1207f8f80361SMengdong Lin  * snd_soc_remove_dai_link - Remove a DAI link from the list
1208f8f80361SMengdong Lin  * @card: The ASoC card that owns the link
1209f8f80361SMengdong Lin  * @dai_link: The DAI link to remove
1210f8f80361SMengdong Lin  *
1211f8f80361SMengdong Lin  * This function removes a DAI link from the ASoC card's link list.
1212f8f80361SMengdong Lin  *
1213f8f80361SMengdong Lin  * For DAI links previously added by topology, topology should
1214f8f80361SMengdong Lin  * remove them by using the dobj embedded in the link.
1215f8f80361SMengdong Lin  */
1216f8f80361SMengdong Lin void snd_soc_remove_dai_link(struct snd_soc_card *card,
1217f8f80361SMengdong Lin 			     struct snd_soc_dai_link *dai_link)
1218f8f80361SMengdong Lin {
1219f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
1220f8f80361SMengdong Lin 
1221f8f80361SMengdong Lin 	if (dai_link->dobj.type
1222f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1223f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1224f8f80361SMengdong Lin 			dai_link->dobj.type);
1225f8f80361SMengdong Lin 		return;
1226f8f80361SMengdong Lin 	}
1227f8f80361SMengdong Lin 
1228f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
12292c7b696aSMarcel Ziswiler 	/*
12302c7b696aSMarcel Ziswiler 	 * Notify the machine driver for extra destruction
1231d6f220eaSMengdong Lin 	 * on the link created by topology.
1232d6f220eaSMengdong Lin 	 */
1233d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->remove_dai_link)
1234d6f220eaSMengdong Lin 		card->remove_dai_link(card, dai_link);
1235d6f220eaSMengdong Lin 
123698061fdbSKuninori Morimoto 	for_each_card_links_safe(card, link, _link) {
1237f8f80361SMengdong Lin 		if (link == dai_link) {
1238f8f80361SMengdong Lin 			list_del(&link->list);
1239f8f80361SMengdong Lin 			return;
1240f8f80361SMengdong Lin 		}
1241f8f80361SMengdong Lin 	}
1242f8f80361SMengdong Lin }
1243f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1244f0fba2adSLiam Girdwood 
1245aefba455SJerome Brunet static void soc_set_of_name_prefix(struct snd_soc_component *component)
1246aefba455SJerome Brunet {
1247c0834440SKuninori Morimoto 	struct device_node *component_of_node = soc_component_to_node(component);
1248aefba455SJerome Brunet 	const char *str;
1249aefba455SJerome Brunet 	int ret;
1250aefba455SJerome Brunet 
1251aefba455SJerome Brunet 	ret = of_property_read_string(component_of_node, "sound-name-prefix",
1252aefba455SJerome Brunet 				      &str);
1253aefba455SJerome Brunet 	if (!ret)
1254aefba455SJerome Brunet 		component->name_prefix = str;
1255aefba455SJerome Brunet }
1256aefba455SJerome Brunet 
1257ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
125894f99c87SLars-Peter Clausen 				struct snd_soc_component *component)
1259ead9b919SJarkko Nikula {
1260ead9b919SJarkko Nikula 	int i;
1261ead9b919SJarkko Nikula 
1262aefba455SJerome Brunet 	for (i = 0; i < card->num_configs && card->codec_conf; i++) {
1263ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1264c0834440SKuninori Morimoto 		struct device_node *component_of_node = soc_component_to_node(component);
1265b24c539bSCharles Keepax 
1266b24c539bSCharles Keepax 		if (map->of_node && component_of_node != map->of_node)
12673ca041edSSebastian Reichel 			continue;
126894f99c87SLars-Peter Clausen 		if (map->dev_name && strcmp(component->name, map->dev_name))
12693ca041edSSebastian Reichel 			continue;
127094f99c87SLars-Peter Clausen 		component->name_prefix = map->name_prefix;
1271aefba455SJerome Brunet 		return;
1272ead9b919SJarkko Nikula 	}
1273aefba455SJerome Brunet 
1274aefba455SJerome Brunet 	/*
1275aefba455SJerome Brunet 	 * If there is no configuration table or no match in the table,
1276aefba455SJerome Brunet 	 * check if a prefix is provided in the node
1277aefba455SJerome Brunet 	 */
1278aefba455SJerome Brunet 	soc_set_of_name_prefix(component);
1279ead9b919SJarkko Nikula }
1280ead9b919SJarkko Nikula 
1281f1d45cc3SLars-Peter Clausen static int soc_probe_component(struct snd_soc_card *card,
1282f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component)
1283589c3563SJarkko Nikula {
12842c7b696aSMarcel Ziswiler 	struct snd_soc_dapm_context *dapm =
12852c7b696aSMarcel Ziswiler 			snd_soc_component_get_dapm(component);
1286888df395SMark Brown 	struct snd_soc_dai *dai;
1287f1d45cc3SLars-Peter Clausen 	int ret;
1288589c3563SJarkko Nikula 
12891b7c1231SLars-Peter Clausen 	if (!strcmp(component->name, "snd-soc-dummy"))
129070090bbbSLars-Peter Clausen 		return 0;
1291589c3563SJarkko Nikula 
1292abd31b32SLars-Peter Clausen 	if (component->card) {
12931b7c1231SLars-Peter Clausen 		if (component->card != card) {
12941b7c1231SLars-Peter Clausen 			dev_err(component->dev,
12951b7c1231SLars-Peter Clausen 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
12961b7c1231SLars-Peter Clausen 				card->name, component->card->name);
12971b7c1231SLars-Peter Clausen 			return -ENODEV;
12981b7c1231SLars-Peter Clausen 		}
12991b7c1231SLars-Peter Clausen 		return 0;
13001b7c1231SLars-Peter Clausen 	}
13011b7c1231SLars-Peter Clausen 
1302b4ed6b51SRanjani Sridharan 	if (!component->driver->module_get_upon_open &&
1303b450b878SPierre-Louis Bossart 	    !try_module_get(component->dev->driver->owner))
1304abd31b32SLars-Peter Clausen 		return -ENODEV;
1305abd31b32SLars-Peter Clausen 
1306f1d45cc3SLars-Peter Clausen 	component->card = card;
1307f1d45cc3SLars-Peter Clausen 	dapm->card = card;
130822d14231SKuninori Morimoto 	INIT_LIST_HEAD(&component->card_list);
130922d14231SKuninori Morimoto 	INIT_LIST_HEAD(&dapm->list);
1310f1d45cc3SLars-Peter Clausen 	soc_set_name_prefix(card, component);
1311589c3563SJarkko Nikula 
1312f1d45cc3SLars-Peter Clausen 	soc_init_component_debugfs(component);
1313d5d1e0beSLars-Peter Clausen 
1314688d0ebfSKuninori Morimoto 	if (component->driver->dapm_widgets) {
1315688d0ebfSKuninori Morimoto 		ret = snd_soc_dapm_new_controls(dapm,
1316688d0ebfSKuninori Morimoto 					component->driver->dapm_widgets,
1317688d0ebfSKuninori Morimoto 					component->driver->num_dapm_widgets);
131877530150SLars-Peter Clausen 
1319b318ad50SNariman Poushin 		if (ret != 0) {
1320f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1321b318ad50SNariman Poushin 				"Failed to create new controls %d\n", ret);
1322b318ad50SNariman Poushin 			goto err_probe;
1323b318ad50SNariman Poushin 		}
1324b318ad50SNariman Poushin 	}
1325b318ad50SNariman Poushin 
132615a0c645SKuninori Morimoto 	for_each_component_dais(component, dai) {
13270634814fSLars-Peter Clausen 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1328261edc70SNariman Poushin 		if (ret != 0) {
1329f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1330261edc70SNariman Poushin 				"Failed to create DAI widgets %d\n", ret);
1331261edc70SNariman Poushin 			goto err_probe;
1332261edc70SNariman Poushin 		}
1333261edc70SNariman Poushin 	}
1334888df395SMark Brown 
1335999f7f5aSKuninori Morimoto 	if (component->driver->probe) {
1336999f7f5aSKuninori Morimoto 		ret = component->driver->probe(component);
1337589c3563SJarkko Nikula 		if (ret < 0) {
1338f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1339f1d45cc3SLars-Peter Clausen 				"ASoC: failed to probe component %d\n", ret);
134070d29331SJarkko Nikula 			goto err_probe;
1341589c3563SJarkko Nikula 		}
13422ffb0f58SKuninori Morimoto 	}
1343f1d45cc3SLars-Peter Clausen 	WARN(dapm->idle_bias_off &&
1344f1d45cc3SLars-Peter Clausen 	     dapm->bias_level != SND_SOC_BIAS_OFF,
134510e8aa9aSMichał Mirosław 	     "codec %s can not start from non-off bias with idle_bias_off==1\n",
1346f1d45cc3SLars-Peter Clausen 	     component->name);
1347589c3563SJarkko Nikula 
1348f2ed6b07SMengdong Lin 	/* machine specific init */
1349f2ed6b07SMengdong Lin 	if (component->init) {
1350f2ed6b07SMengdong Lin 		ret = component->init(component);
1351f2ed6b07SMengdong Lin 		if (ret < 0) {
1352f2ed6b07SMengdong Lin 			dev_err(component->dev,
1353f2ed6b07SMengdong Lin 				"Failed to do machine specific init %d\n", ret);
1354f2ed6b07SMengdong Lin 			goto err_probe;
1355f2ed6b07SMengdong Lin 		}
1356f2ed6b07SMengdong Lin 	}
1357f2ed6b07SMengdong Lin 
1358b8972bf0SKuninori Morimoto 	if (component->driver->controls)
1359b8972bf0SKuninori Morimoto 		snd_soc_add_component_controls(component,
1360b8972bf0SKuninori Morimoto 					       component->driver->controls,
1361b8972bf0SKuninori Morimoto 					       component->driver->num_controls);
13626969b2baSKuninori Morimoto 	if (component->driver->dapm_routes)
13636969b2baSKuninori Morimoto 		snd_soc_dapm_add_routes(dapm,
13646969b2baSKuninori Morimoto 					component->driver->dapm_routes,
13656969b2baSKuninori Morimoto 					component->driver->num_dapm_routes);
136689b95ac0SMark Brown 
1367f1d45cc3SLars-Peter Clausen 	list_add(&dapm->list, &card->dapm_list);
1368f70f18f7SKuninori Morimoto 	/* see for_each_card_components */
1369d9fc4063SKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1370589c3563SJarkko Nikula 
137170d29331SJarkko Nikula err_probe:
137222d14231SKuninori Morimoto 	if (ret < 0)
137322d14231SKuninori Morimoto 		soc_cleanup_component(component);
1374956245e9SLiam Girdwood 
1375956245e9SLiam Girdwood 	return ret;
1376956245e9SLiam Girdwood }
1377956245e9SLiam Girdwood 
137836ae1a96SMark Brown static void rtd_release(struct device *dev)
137936ae1a96SMark Brown {
138036ae1a96SMark Brown 	kfree(dev);
138136ae1a96SMark Brown }
1382f0fba2adSLiam Girdwood 
13835f3484acSLars-Peter Clausen static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
13845f3484acSLars-Peter Clausen 	const char *name)
1385503ae5e0SMisael Lopez Cruz {
1386589c3563SJarkko Nikula 	int ret = 0;
1387589c3563SJarkko Nikula 
1388589c3563SJarkko Nikula 	/* register the rtd device */
138936ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
139036ae1a96SMark Brown 	if (!rtd->dev)
139136ae1a96SMark Brown 		return -ENOMEM;
139236ae1a96SMark Brown 	device_initialize(rtd->dev);
13935f3484acSLars-Peter Clausen 	rtd->dev->parent = rtd->card->dev;
139436ae1a96SMark Brown 	rtd->dev->release = rtd_release;
1395d29697dcSTakashi Iwai 	rtd->dev->groups = soc_dev_attr_groups;
1396f294afedSLars-Peter Clausen 	dev_set_name(rtd->dev, "%s", name);
139736ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1398b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
139901d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
140001d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
140101d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
140201d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
140336ae1a96SMark Brown 	ret = device_add(rtd->dev);
1404589c3563SJarkko Nikula 	if (ret < 0) {
1405865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1406865df9cbSChuansheng Liu 		put_device(rtd->dev);
14075f3484acSLars-Peter Clausen 		dev_err(rtd->card->dev,
1408f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1409589c3563SJarkko Nikula 		return ret;
1410589c3563SJarkko Nikula 	}
1411589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1412589c3563SJarkko Nikula 	return 0;
1413589c3563SJarkko Nikula }
1414589c3563SJarkko Nikula 
14151a497983SMengdong Lin static int soc_probe_link_components(struct snd_soc_card *card,
14162c7b696aSMarcel Ziswiler 				     struct snd_soc_pcm_runtime *rtd, int order)
141762ae68faSStephen Warren {
1418f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component;
141990be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
142090be711eSKuninori Morimoto 	int ret;
142162ae68faSStephen Warren 
142290be711eSKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
142390be711eSKuninori Morimoto 		component = rtdcom->component;
142490be711eSKuninori Morimoto 
142570090bbbSLars-Peter Clausen 		if (component->driver->probe_order == order) {
1426f1d45cc3SLars-Peter Clausen 			ret = soc_probe_component(card, component);
142762ae68faSStephen Warren 			if (ret < 0)
142862ae68faSStephen Warren 				return ret;
142962ae68faSStephen Warren 		}
143062ae68faSStephen Warren 	}
143162ae68faSStephen Warren 
143262ae68faSStephen Warren 	return 0;
143362ae68faSStephen Warren }
143462ae68faSStephen Warren 
14358e2be562SLars-Peter Clausen static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1436b0aa88afSMisael Lopez Cruz {
14377a2ccad5SKuninori Morimoto 	if (dai->probed ||
14387a2ccad5SKuninori Morimoto 	    dai->driver->probe_order != order)
14397a2ccad5SKuninori Morimoto 		return 0;
1440b0aa88afSMisael Lopez Cruz 
14418e2be562SLars-Peter Clausen 	if (dai->driver->probe) {
14427a2ccad5SKuninori Morimoto 		int ret = dai->driver->probe(dai);
14432c7b696aSMarcel Ziswiler 
1444b0aa88afSMisael Lopez Cruz 		if (ret < 0) {
14457a2ccad5SKuninori Morimoto 			dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
14468e2be562SLars-Peter Clausen 				dai->name, ret);
1447b0aa88afSMisael Lopez Cruz 			return ret;
1448b0aa88afSMisael Lopez Cruz 		}
1449b0aa88afSMisael Lopez Cruz 	}
1450b0aa88afSMisael Lopez Cruz 
14518e2be562SLars-Peter Clausen 	dai->probed = 1;
1452b0aa88afSMisael Lopez Cruz 
1453b0aa88afSMisael Lopez Cruz 	return 0;
1454b0aa88afSMisael Lopez Cruz }
1455b0aa88afSMisael Lopez Cruz 
145625f7b701SArnaud Pouliquen static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
145725f7b701SArnaud Pouliquen 				struct snd_soc_pcm_runtime *rtd)
145825f7b701SArnaud Pouliquen {
145925f7b701SArnaud Pouliquen 	int i, ret = 0;
146025f7b701SArnaud Pouliquen 
146125f7b701SArnaud Pouliquen 	for (i = 0; i < num_dais; ++i) {
146225f7b701SArnaud Pouliquen 		struct snd_soc_dai_driver *drv = dais[i]->driver;
146325f7b701SArnaud Pouliquen 
1464de17f14eSRohit kumar 		if (drv->pcm_new)
146525f7b701SArnaud Pouliquen 			ret = drv->pcm_new(rtd, dais[i]);
146625f7b701SArnaud Pouliquen 		if (ret < 0) {
146725f7b701SArnaud Pouliquen 			dev_err(dais[i]->dev,
146825f7b701SArnaud Pouliquen 				"ASoC: Failed to bind %s with pcm device\n",
146925f7b701SArnaud Pouliquen 				dais[i]->name);
147025f7b701SArnaud Pouliquen 			return ret;
147125f7b701SArnaud Pouliquen 		}
147225f7b701SArnaud Pouliquen 	}
147325f7b701SArnaud Pouliquen 
147425f7b701SArnaud Pouliquen 	return 0;
147525f7b701SArnaud Pouliquen }
147625f7b701SArnaud Pouliquen 
14771a497983SMengdong Lin static int soc_probe_link_dais(struct snd_soc_card *card,
14781a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1479f0fba2adSLiam Girdwood {
14801a497983SMengdong Lin 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1481c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1482a655de80SLiam Girdwood 	struct snd_soc_rtdcom_list *rtdcom;
1483a655de80SLiam Girdwood 	struct snd_soc_component *component;
14840b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1485a655de80SLiam Girdwood 	int i, ret, num;
1486f0fba2adSLiam Girdwood 
1487f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
14881a497983SMengdong Lin 			card->name, rtd->num, order);
1489f0fba2adSLiam Girdwood 
1490f0fba2adSLiam Girdwood 	/* set default power off timeout */
1491f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1492f0fba2adSLiam Girdwood 
14938e2be562SLars-Peter Clausen 	ret = soc_probe_dai(cpu_dai, order);
14948e2be562SLars-Peter Clausen 	if (ret)
1495f0fba2adSLiam Girdwood 		return ret;
1496f0fba2adSLiam Girdwood 
1497f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
14980b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
14990b7990e3SKuninori Morimoto 		ret = soc_probe_dai(codec_dai, order);
1500b0aa88afSMisael Lopez Cruz 		if (ret)
1501f0fba2adSLiam Girdwood 			return ret;
150288bd870fSBenoit Cousson 	}
1503f0fba2adSLiam Girdwood 
15040168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
15050168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
15060168bf0dSLiam Girdwood 		return 0;
15070168bf0dSLiam Girdwood 
15085f3484acSLars-Peter Clausen 	/* do machine specific initialization */
15095f3484acSLars-Peter Clausen 	if (dai_link->init) {
15105f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
15115f3484acSLars-Peter Clausen 		if (ret < 0) {
15125f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
15135f3484acSLars-Peter Clausen 				dai_link->name, ret);
15145f3484acSLars-Peter Clausen 			return ret;
15155f3484acSLars-Peter Clausen 		}
15165f3484acSLars-Peter Clausen 	}
15175f3484acSLars-Peter Clausen 
1518a5053a8eSKuninori Morimoto 	if (dai_link->dai_fmt)
1519a5053a8eSKuninori Morimoto 		snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1520a5053a8eSKuninori Morimoto 
15215f3484acSLars-Peter Clausen 	ret = soc_post_component_init(rtd, dai_link->name);
1522589c3563SJarkko Nikula 	if (ret)
1523f0fba2adSLiam Girdwood 		return ret;
1524f0fba2adSLiam Girdwood 
15255f3484acSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
15265f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
15272e55b90aSLars-Peter Clausen 	if (dai_link->dynamic)
15282e55b90aSLars-Peter Clausen 		soc_dpcm_debugfs_add(rtd);
15295f3484acSLars-Peter Clausen #endif
15305f3484acSLars-Peter Clausen 
1531a655de80SLiam Girdwood 	num = rtd->num;
1532a655de80SLiam Girdwood 
1533a655de80SLiam Girdwood 	/*
1534a655de80SLiam Girdwood 	 * most drivers will register their PCMs using DAI link ordering but
1535a655de80SLiam Girdwood 	 * topology based drivers can use the DAI link id field to set PCM
1536a655de80SLiam Girdwood 	 * device number and then use rtd + a base offset of the BEs.
1537a655de80SLiam Girdwood 	 */
1538a655de80SLiam Girdwood 	for_each_rtdcom(rtd, rtdcom) {
1539a655de80SLiam Girdwood 		component = rtdcom->component;
1540a655de80SLiam Girdwood 
1541a655de80SLiam Girdwood 		if (!component->driver->use_dai_pcm_id)
1542a655de80SLiam Girdwood 			continue;
1543a655de80SLiam Girdwood 
1544a655de80SLiam Girdwood 		if (rtd->dai_link->no_pcm)
1545a655de80SLiam Girdwood 			num += component->driver->be_pcm_base;
1546a655de80SLiam Girdwood 		else
1547a655de80SLiam Girdwood 			num = rtd->dai_link->id;
1548a655de80SLiam Girdwood 	}
1549a655de80SLiam Girdwood 
15506f0c4226SJie Yang 	if (cpu_dai->driver->compress_new) {
15511245b700SNamarta Kohli 		/* create compress_device" */
1552a655de80SLiam Girdwood 		ret = cpu_dai->driver->compress_new(rtd, num);
15531245b700SNamarta Kohli 		if (ret < 0) {
1554f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
15551245b700SNamarta Kohli 					 dai_link->stream_name);
15561245b700SNamarta Kohli 			return ret;
15571245b700SNamarta Kohli 		}
155852293596SKuninori Morimoto 	} else if (!dai_link->params) {
1559f0fba2adSLiam Girdwood 		/* create the pcm */
1560a655de80SLiam Girdwood 		ret = soc_new_pcm(rtd, num);
1561f0fba2adSLiam Girdwood 		if (ret < 0) {
1562f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
15630837fc62SFabio Estevam 				dai_link->stream_name, ret);
1564f0fba2adSLiam Girdwood 			return ret;
1565f0fba2adSLiam Girdwood 		}
156625f7b701SArnaud Pouliquen 		ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
156725f7b701SArnaud Pouliquen 		if (ret < 0)
156825f7b701SArnaud Pouliquen 			return ret;
156925f7b701SArnaud Pouliquen 		ret = soc_link_dai_pcm_new(rtd->codec_dais,
157025f7b701SArnaud Pouliquen 					   rtd->num_codecs, rtd);
157125f7b701SArnaud Pouliquen 		if (ret < 0)
157225f7b701SArnaud Pouliquen 			return ret;
1573c74184edSMark Brown 	} else {
15749d58a077SRichard Fitzgerald 		INIT_DELAYED_WORK(&rtd->delayed_work,
15759d58a077SRichard Fitzgerald 				  codec2codec_close_delayed_work);
1576c74184edSMark Brown 	}
1577c74184edSMark Brown 
1578f0fba2adSLiam Girdwood 	return 0;
1579f0fba2adSLiam Girdwood }
1580f0fba2adSLiam Girdwood 
158144c69bb1SLars-Peter Clausen static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1582b19e6e7bSMark Brown {
15833ca041edSSebastian Reichel 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1584f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1585f2ed6b07SMengdong Lin 	const char *name;
1586f2ed6b07SMengdong Lin 	struct device_node *codec_of_node;
15873ca041edSSebastian Reichel 
1588f2ed6b07SMengdong Lin 	if (aux_dev->codec_of_node || aux_dev->codec_name) {
1589f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1590f2ed6b07SMengdong Lin 		name = aux_dev->codec_name;
1591f2ed6b07SMengdong Lin 		codec_of_node = aux_dev->codec_of_node;
1592f2ed6b07SMengdong Lin 		component = soc_find_component(codec_of_node, name);
1593f2ed6b07SMengdong Lin 		if (!component) {
1594f2ed6b07SMengdong Lin 			if (codec_of_node)
1595f2ed6b07SMengdong Lin 				name = of_node_full_name(codec_of_node);
1596f2ed6b07SMengdong Lin 			goto err_defer;
1597f2ed6b07SMengdong Lin 		}
1598f2ed6b07SMengdong Lin 	} else if (aux_dev->name) {
1599f2ed6b07SMengdong Lin 		/* generic components */
1600f2ed6b07SMengdong Lin 		name = aux_dev->name;
1601f2ed6b07SMengdong Lin 		component = soc_find_component(NULL, name);
1602f2ed6b07SMengdong Lin 		if (!component)
1603f2ed6b07SMengdong Lin 			goto err_defer;
1604f2ed6b07SMengdong Lin 	} else {
1605f2ed6b07SMengdong Lin 		dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1606f2ed6b07SMengdong Lin 		return -EINVAL;
1607f2ed6b07SMengdong Lin 	}
16083ca041edSSebastian Reichel 
1609f2ed6b07SMengdong Lin 	component->init = aux_dev->init;
1610d2e3a135SSylwester Nawrocki 	list_add(&component->card_aux_list, &card->aux_comp_list);
16111a653aa4SKuninori Morimoto 
1612f2ed6b07SMengdong Lin 	return 0;
1613f2ed6b07SMengdong Lin 
1614f2ed6b07SMengdong Lin err_defer:
161565d9361fSLars-Peter Clausen 	dev_err(card->dev, "ASoC: %s not registered\n", name);
1616b19e6e7bSMark Brown 	return -EPROBE_DEFER;
1617b19e6e7bSMark Brown }
1618b19e6e7bSMark Brown 
1619f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1620f2ed6b07SMengdong Lin {
1621991454e1SKuninori Morimoto 	struct snd_soc_component *comp;
1622f2ed6b07SMengdong Lin 	int order;
1623f2ed6b07SMengdong Lin 	int ret;
1624f2ed6b07SMengdong Lin 
16251a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1626991454e1SKuninori Morimoto 		list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
1627f2ed6b07SMengdong Lin 			if (comp->driver->probe_order == order) {
1628f2ed6b07SMengdong Lin 				ret = soc_probe_component(card,	comp);
1629f2ed6b07SMengdong Lin 				if (ret < 0) {
1630f2ed6b07SMengdong Lin 					dev_err(card->dev,
1631f2ed6b07SMengdong Lin 						"ASoC: failed to probe aux component %s %d\n",
1632f2ed6b07SMengdong Lin 						comp->name, ret);
1633f2ed6b07SMengdong Lin 					return ret;
1634f2ed6b07SMengdong Lin 				}
1635f2ed6b07SMengdong Lin 			}
1636f2ed6b07SMengdong Lin 		}
1637f2ed6b07SMengdong Lin 	}
163865d9361fSLars-Peter Clausen 
163944c69bb1SLars-Peter Clausen 	return 0;
16403ca041edSSebastian Reichel }
16412eea392dSJarkko Nikula 
1642f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
164344c69bb1SLars-Peter Clausen {
1644f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1645f2ed6b07SMengdong Lin 	int order;
164644c69bb1SLars-Peter Clausen 
16471a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1648f2ed6b07SMengdong Lin 		list_for_each_entry_safe(comp, _comp,
1649991454e1SKuninori Morimoto 			&card->aux_comp_list, card_aux_list) {
16501a653aa4SKuninori Morimoto 
1651f2ed6b07SMengdong Lin 			if (comp->driver->remove_order == order) {
1652f2ed6b07SMengdong Lin 				soc_remove_component(comp);
1653991454e1SKuninori Morimoto 				/* remove it from the card's aux_comp_list */
1654991454e1SKuninori Morimoto 				list_del(&comp->card_aux_list);
16552eea392dSJarkko Nikula 			}
16565f3484acSLars-Peter Clausen 		}
16572eea392dSJarkko Nikula 	}
16582eea392dSJarkko Nikula }
16592eea392dSJarkko Nikula 
1660ce64c8b9SLars-Peter Clausen /**
1661ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1662ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1663ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1664ce64c8b9SLars-Peter Clausen  *
1665ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1666ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1667ce64c8b9SLars-Peter Clausen  *
1668ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1669ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1670ce64c8b9SLars-Peter Clausen  *
1671ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1672ce64c8b9SLars-Peter Clausen  */
1673ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1674ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1675ce64c8b9SLars-Peter Clausen {
1676ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
16770b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1678ce64c8b9SLars-Peter Clausen 	unsigned int i;
1679ce64c8b9SLars-Peter Clausen 	int ret;
1680ce64c8b9SLars-Peter Clausen 
16810b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1682ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1683ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1684ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1685ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1686ce64c8b9SLars-Peter Clausen 			return ret;
1687ce64c8b9SLars-Peter Clausen 		}
1688ce64c8b9SLars-Peter Clausen 	}
1689ce64c8b9SLars-Peter Clausen 
16902c7b696aSMarcel Ziswiler 	/*
16912c7b696aSMarcel Ziswiler 	 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
16922c7b696aSMarcel Ziswiler 	 * the component which has non_legacy_dai_naming is Codec
16932c7b696aSMarcel Ziswiler 	 */
1694999f7f5aSKuninori Morimoto 	if (cpu_dai->component->driver->non_legacy_dai_naming) {
1695ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1696ce64c8b9SLars-Peter Clausen 
1697ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1698ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1699ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1700ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1701ce64c8b9SLars-Peter Clausen 			break;
1702ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1703ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1704ce64c8b9SLars-Peter Clausen 			break;
1705ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1706ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1707ce64c8b9SLars-Peter Clausen 			break;
1708ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1709ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1710ce64c8b9SLars-Peter Clausen 			break;
1711ce64c8b9SLars-Peter Clausen 		}
1712ce64c8b9SLars-Peter Clausen 
1713ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1714ce64c8b9SLars-Peter Clausen 	}
1715ce64c8b9SLars-Peter Clausen 
1716ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1717ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1718ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1719ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1720ce64c8b9SLars-Peter Clausen 		return ret;
1721ce64c8b9SLars-Peter Clausen 	}
1722ce64c8b9SLars-Peter Clausen 
1723ce64c8b9SLars-Peter Clausen 	return 0;
1724ce64c8b9SLars-Peter Clausen }
1725ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1726ce64c8b9SLars-Peter Clausen 
17271f5a4535STakashi Iwai #ifdef CONFIG_DMI
17282c7b696aSMarcel Ziswiler /*
17292c7b696aSMarcel Ziswiler  * Trim special characters, and replace '-' with '_' since '-' is used to
1730345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1731345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1732345233d7SLiam Girdwood  */
1733345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1734345233d7SLiam Girdwood {
1735345233d7SLiam Girdwood 	int i, j = 0;
1736345233d7SLiam Girdwood 
1737345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1738345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1739345233d7SLiam Girdwood 		    || (name[i] == '_'))
1740345233d7SLiam Girdwood 			name[j++] = name[i];
1741345233d7SLiam Girdwood 		else if (name[i] == '-')
1742345233d7SLiam Girdwood 			name[j++] = '_';
1743345233d7SLiam Girdwood 	}
1744345233d7SLiam Girdwood 
1745345233d7SLiam Girdwood 	name[j] = '\0';
1746345233d7SLiam Girdwood }
1747345233d7SLiam Girdwood 
17482c7b696aSMarcel Ziswiler /*
17492c7b696aSMarcel Ziswiler  * Check if a DMI field is valid, i.e. not containing any string
175098faf436SMengdong Lin  * in the black list.
175198faf436SMengdong Lin  */
175298faf436SMengdong Lin static int is_dmi_valid(const char *field)
175398faf436SMengdong Lin {
175498faf436SMengdong Lin 	int i = 0;
175598faf436SMengdong Lin 
175698faf436SMengdong Lin 	while (dmi_blacklist[i]) {
175798faf436SMengdong Lin 		if (strstr(field, dmi_blacklist[i]))
175898faf436SMengdong Lin 			return 0;
175998faf436SMengdong Lin 		i++;
176046b5a4d2SWu Fengguang 	}
176198faf436SMengdong Lin 
176298faf436SMengdong Lin 	return 1;
176398faf436SMengdong Lin }
176498faf436SMengdong Lin 
1765345233d7SLiam Girdwood /**
1766345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
1767345233d7SLiam Girdwood  * @card: The card to register DMI names
1768345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
1769345233d7SLiam Girdwood  *
1770345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
1771345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
1772345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
1773345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
1774345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
1775345233d7SLiam Girdwood  * as the sound card long name, in the format of
1776345233d7SLiam Girdwood  * "vendor-product-version-board"
1777345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
1778345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
1779345233d7SLiam Girdwood  * (UCM) configurations for the card.
1780345233d7SLiam Girdwood  *
1781345233d7SLiam Girdwood  * Possible card long names may be:
1782345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
1783345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1784345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1785345233d7SLiam Girdwood  *
1786345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
1787345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
1788345233d7SLiam Girdwood  *
1789345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
1790345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
1791345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
1792345233d7SLiam Girdwood  * charactors like SPACE.
1793345233d7SLiam Girdwood  *
1794345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
1795345233d7SLiam Girdwood  */
1796345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1797345233d7SLiam Girdwood {
1798345233d7SLiam Girdwood 	const char *vendor, *product, *product_version, *board;
1799345233d7SLiam Girdwood 	size_t longname_buf_size = sizeof(card->snd_card->longname);
1800345233d7SLiam Girdwood 	size_t len;
1801345233d7SLiam Girdwood 
1802345233d7SLiam Girdwood 	if (card->long_name)
1803345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
1804345233d7SLiam Girdwood 
1805345233d7SLiam Girdwood 	/* make up dmi long name as: vendor.product.version.board */
1806345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
180798faf436SMengdong Lin 	if (!vendor || !is_dmi_valid(vendor)) {
1808345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1809345233d7SLiam Girdwood 		return 0;
1810345233d7SLiam Girdwood 	}
1811345233d7SLiam Girdwood 
1812345233d7SLiam Girdwood 	snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1813345233d7SLiam Girdwood 			 "%s", vendor);
1814345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
1815345233d7SLiam Girdwood 
1816345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
181798faf436SMengdong Lin 	if (product && is_dmi_valid(product)) {
1818345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1819345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1820345233d7SLiam Girdwood 			 longname_buf_size - len,
1821345233d7SLiam Girdwood 			 "-%s", product);
1822345233d7SLiam Girdwood 
1823345233d7SLiam Girdwood 		len++;	/* skip the separator "-" */
1824345233d7SLiam Girdwood 		if (len < longname_buf_size)
1825345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1826345233d7SLiam Girdwood 
18272c7b696aSMarcel Ziswiler 		/*
18282c7b696aSMarcel Ziswiler 		 * some vendors like Lenovo may only put a self-explanatory
1829345233d7SLiam Girdwood 		 * name in the product version field
1830345233d7SLiam Girdwood 		 */
1831345233d7SLiam Girdwood 		product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
183298faf436SMengdong Lin 		if (product_version && is_dmi_valid(product_version)) {
1833345233d7SLiam Girdwood 			len = strlen(card->dmi_longname);
1834345233d7SLiam Girdwood 			snprintf(card->dmi_longname + len,
1835345233d7SLiam Girdwood 				 longname_buf_size - len,
1836345233d7SLiam Girdwood 				 "-%s", product_version);
1837345233d7SLiam Girdwood 
1838345233d7SLiam Girdwood 			len++;
1839345233d7SLiam Girdwood 			if (len < longname_buf_size)
1840345233d7SLiam Girdwood 				cleanup_dmi_name(card->dmi_longname + len);
1841345233d7SLiam Girdwood 		}
1842345233d7SLiam Girdwood 	}
1843345233d7SLiam Girdwood 
1844345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
184598faf436SMengdong Lin 	if (board && is_dmi_valid(board)) {
1846345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1847345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1848345233d7SLiam Girdwood 			 longname_buf_size - len,
1849345233d7SLiam Girdwood 			 "-%s", board);
1850345233d7SLiam Girdwood 
1851345233d7SLiam Girdwood 		len++;
1852345233d7SLiam Girdwood 		if (len < longname_buf_size)
1853345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1854345233d7SLiam Girdwood 	} else if (!product) {
1855345233d7SLiam Girdwood 		/* fall back to using legacy name */
1856345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1857345233d7SLiam Girdwood 		return 0;
1858345233d7SLiam Girdwood 	}
1859345233d7SLiam Girdwood 
1860345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
1861345233d7SLiam Girdwood 	if (flavour) {
1862345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1863345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1864345233d7SLiam Girdwood 			 longname_buf_size - len,
1865345233d7SLiam Girdwood 			 "-%s", flavour);
1866345233d7SLiam Girdwood 
1867345233d7SLiam Girdwood 		len++;
1868345233d7SLiam Girdwood 		if (len < longname_buf_size)
1869345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1870345233d7SLiam Girdwood 	}
1871345233d7SLiam Girdwood 
1872345233d7SLiam Girdwood 	/* set the card long name */
1873345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
1874345233d7SLiam Girdwood 
1875345233d7SLiam Girdwood 	return 0;
1876345233d7SLiam Girdwood }
1877345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
18781f5a4535STakashi Iwai #endif /* CONFIG_DMI */
1879345233d7SLiam Girdwood 
1880a655de80SLiam Girdwood static void soc_check_tplg_fes(struct snd_soc_card *card)
1881a655de80SLiam Girdwood {
1882a655de80SLiam Girdwood 	struct snd_soc_component *component;
1883a655de80SLiam Girdwood 	const struct snd_soc_component_driver *comp_drv;
1884a655de80SLiam Girdwood 	struct snd_soc_dai_link *dai_link;
1885a655de80SLiam Girdwood 	int i;
1886a655de80SLiam Girdwood 
1887368dee94SKuninori Morimoto 	for_each_component(component) {
1888a655de80SLiam Girdwood 
1889a655de80SLiam Girdwood 		/* does this component override FEs ? */
1890a655de80SLiam Girdwood 		if (!component->driver->ignore_machine)
1891a655de80SLiam Girdwood 			continue;
1892a655de80SLiam Girdwood 
1893a655de80SLiam Girdwood 		/* for this machine ? */
1894e194098bSPierre-Louis Bossart 		if (!strcmp(component->driver->ignore_machine,
1895a655de80SLiam Girdwood 			    card->dev->driver->name))
1896e194098bSPierre-Louis Bossart 			goto match;
1897e194098bSPierre-Louis Bossart 		if (strcmp(component->driver->ignore_machine,
1898e194098bSPierre-Louis Bossart 			   dev_name(card->dev)))
1899a655de80SLiam Girdwood 			continue;
1900e194098bSPierre-Louis Bossart match:
1901a655de80SLiam Girdwood 		/* machine matches, so override the rtd data */
19027fe072b4SKuninori Morimoto 		for_each_card_prelinks(card, i, dai_link) {
1903a655de80SLiam Girdwood 
1904a655de80SLiam Girdwood 			/* ignore this FE */
1905a655de80SLiam Girdwood 			if (dai_link->dynamic) {
1906a655de80SLiam Girdwood 				dai_link->ignore = true;
1907a655de80SLiam Girdwood 				continue;
1908a655de80SLiam Girdwood 			}
1909a655de80SLiam Girdwood 
1910a655de80SLiam Girdwood 			dev_info(card->dev, "info: override FE DAI link %s\n",
1911a655de80SLiam Girdwood 				 card->dai_link[i].name);
1912a655de80SLiam Girdwood 
1913a655de80SLiam Girdwood 			/* override platform component */
1914*adb76b5bSKuninori Morimoto 			if (!dai_link->platforms) {
1915daecf46eSKuninori Morimoto 				dev_err(card->dev, "init platform error");
1916daecf46eSKuninori Morimoto 				continue;
1917daecf46eSKuninori Morimoto 			}
1918910fdcabSKuninori Morimoto 			dai_link->platforms->name = component->name;
1919a655de80SLiam Girdwood 
1920a655de80SLiam Girdwood 			/* convert non BE into BE */
1921a655de80SLiam Girdwood 			dai_link->no_pcm = 1;
1922a655de80SLiam Girdwood 
1923a655de80SLiam Girdwood 			/* override any BE fixups */
1924a655de80SLiam Girdwood 			dai_link->be_hw_params_fixup =
1925a655de80SLiam Girdwood 				component->driver->be_hw_params_fixup;
1926a655de80SLiam Girdwood 
19272c7b696aSMarcel Ziswiler 			/*
19282c7b696aSMarcel Ziswiler 			 * most BE links don't set stream name, so set it to
1929a655de80SLiam Girdwood 			 * dai link name if it's NULL to help bind widgets.
1930a655de80SLiam Girdwood 			 */
1931a655de80SLiam Girdwood 			if (!dai_link->stream_name)
1932a655de80SLiam Girdwood 				dai_link->stream_name = dai_link->name;
1933a655de80SLiam Girdwood 		}
1934a655de80SLiam Girdwood 
1935a655de80SLiam Girdwood 		/* Inform userspace we are using alternate topology */
1936a655de80SLiam Girdwood 		if (component->driver->topology_name_prefix) {
1937a655de80SLiam Girdwood 
1938a655de80SLiam Girdwood 			/* topology shortname created? */
1939a655de80SLiam Girdwood 			if (!card->topology_shortname_created) {
1940a655de80SLiam Girdwood 				comp_drv = component->driver;
1941a655de80SLiam Girdwood 
1942a655de80SLiam Girdwood 				snprintf(card->topology_shortname, 32, "%s-%s",
1943a655de80SLiam Girdwood 					 comp_drv->topology_name_prefix,
1944a655de80SLiam Girdwood 					 card->name);
1945a655de80SLiam Girdwood 				card->topology_shortname_created = true;
1946a655de80SLiam Girdwood 			}
1947a655de80SLiam Girdwood 
1948a655de80SLiam Girdwood 			/* use topology shortname */
1949a655de80SLiam Girdwood 			card->name = card->topology_shortname;
1950a655de80SLiam Girdwood 		}
1951a655de80SLiam Girdwood 	}
1952a655de80SLiam Girdwood }
1953a655de80SLiam Girdwood 
195453e947a0SKuninori Morimoto static int soc_cleanup_card_resources(struct snd_soc_card *card)
195553e947a0SKuninori Morimoto {
195653e947a0SKuninori Morimoto 	/* free the ALSA card at first; this syncs with pending operations */
195729040d1aSKuninori Morimoto 	if (card->snd_card) {
195853e947a0SKuninori Morimoto 		snd_card_free(card->snd_card);
195929040d1aSKuninori Morimoto 		card->snd_card = NULL;
196029040d1aSKuninori Morimoto 	}
196153e947a0SKuninori Morimoto 
196253e947a0SKuninori Morimoto 	/* remove and free each DAI */
196353e947a0SKuninori Morimoto 	soc_remove_dai_links(card);
196453e947a0SKuninori Morimoto 	soc_remove_pcm_runtimes(card);
196553e947a0SKuninori Morimoto 
196653e947a0SKuninori Morimoto 	/* remove auxiliary devices */
196753e947a0SKuninori Morimoto 	soc_remove_aux_devices(card);
196853e947a0SKuninori Morimoto 
196953e947a0SKuninori Morimoto 	snd_soc_dapm_free(&card->dapm);
197053e947a0SKuninori Morimoto 	soc_cleanup_card_debugfs(card);
197153e947a0SKuninori Morimoto 
197253e947a0SKuninori Morimoto 	/* remove the card */
197353e947a0SKuninori Morimoto 	if (card->remove)
197453e947a0SKuninori Morimoto 		card->remove(card);
197553e947a0SKuninori Morimoto 
197653e947a0SKuninori Morimoto 	return 0;
197753e947a0SKuninori Morimoto }
197853e947a0SKuninori Morimoto 
1979b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1980f0fba2adSLiam Girdwood {
19811a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
198261b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
1983ce64c8b9SLars-Peter Clausen 	int ret, i, order;
198496dd3622SMark Brown 
198534e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
198601b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1987f0fba2adSLiam Girdwood 
198853e947a0SKuninori Morimoto 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
198953e947a0SKuninori Morimoto 	card->dapm.dev = card->dev;
199053e947a0SKuninori Morimoto 	card->dapm.card = card;
199153e947a0SKuninori Morimoto 	list_add(&card->dapm.list, &card->dapm_list);
199253e947a0SKuninori Morimoto 
1993a655de80SLiam Girdwood 	/* check whether any platform is ignore machine FE and using topology */
1994a655de80SLiam Girdwood 	soc_check_tplg_fes(card);
1995a655de80SLiam Girdwood 
1996b19e6e7bSMark Brown 	/* bind DAIs */
19977fe072b4SKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link) {
19987fe072b4SKuninori Morimoto 		ret = soc_bind_dai_link(card, dai_link);
1999b19e6e7bSMark Brown 		if (ret != 0)
200053e947a0SKuninori Morimoto 			goto probe_end;
2001db2a4165SFrank Mandarino 	}
2002db2a4165SFrank Mandarino 
200344c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
2004b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
200544c69bb1SLars-Peter Clausen 		ret = soc_bind_aux_dev(card, i);
2006b19e6e7bSMark Brown 		if (ret != 0)
200753e947a0SKuninori Morimoto 			goto probe_end;
2008db2a4165SFrank Mandarino 	}
2009db2a4165SFrank Mandarino 
2010f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
20117fe072b4SKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link)
20127fe072b4SKuninori Morimoto 		snd_soc_add_dai_link(card, dai_link);
2013f8f80361SMengdong Lin 
2014f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
2015102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2016f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
2017f0fba2adSLiam Girdwood 	if (ret < 0) {
201810e8aa9aSMichał Mirosław 		dev_err(card->dev,
201910e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
202010e8aa9aSMichał Mirosław 			card->name, ret);
202153e947a0SKuninori Morimoto 		goto probe_end;
2022db2a4165SFrank Mandarino 	}
2023db2a4165SFrank Mandarino 
20240757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
20250757d834SLars-Peter Clausen 
2026d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
2027d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2028d5d1e0beSLars-Peter Clausen #endif
2029d5d1e0beSLars-Peter Clausen 
203088ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
20316ed25978SAndy Green 	/* deferred resume work */
20326308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
20331301a964SRandy Dunlap #endif
20346ed25978SAndy Green 
20359a841ebbSMark Brown 	if (card->dapm_widgets)
20369a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
20379a841ebbSMark Brown 					  card->num_dapm_widgets);
20389a841ebbSMark Brown 
2039f23e860eSNicolin Chen 	if (card->of_dapm_widgets)
2040f23e860eSNicolin Chen 		snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2041f23e860eSNicolin Chen 					  card->num_of_dapm_widgets);
2042f23e860eSNicolin Chen 
2043f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
2044f0fba2adSLiam Girdwood 	if (card->probe) {
2045e7361ec4SMark Brown 		ret = card->probe(card);
2046f0fba2adSLiam Girdwood 		if (ret < 0)
204753e947a0SKuninori Morimoto 			goto probe_end;
2048f0fba2adSLiam Girdwood 	}
2049f0fba2adSLiam Girdwood 
205062ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
20511a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
2052bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
20531a497983SMengdong Lin 			ret = soc_probe_link_components(card, rtd, order);
205462ae68faSStephen Warren 			if (ret < 0) {
2055f110bfc7SLiam Girdwood 				dev_err(card->dev,
2056f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
2057f110bfc7SLiam Girdwood 					ret);
205853e947a0SKuninori Morimoto 				goto probe_end;
205962ae68faSStephen Warren 			}
206062ae68faSStephen Warren 		}
206162ae68faSStephen Warren 	}
206262ae68faSStephen Warren 
2063f2ed6b07SMengdong Lin 	/* probe auxiliary components */
2064f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
2065f2ed6b07SMengdong Lin 	if (ret < 0)
206653e947a0SKuninori Morimoto 		goto probe_end;
2067f2ed6b07SMengdong Lin 
20682c7b696aSMarcel Ziswiler 	/*
20692c7b696aSMarcel Ziswiler 	 * Find new DAI links added during probing components and bind them.
207061b0088bSMengdong Lin 	 * Components with topology may bring new DAIs and DAI links.
207161b0088bSMengdong Lin 	 */
207298061fdbSKuninori Morimoto 	for_each_card_links(card, dai_link) {
207361b0088bSMengdong Lin 		if (soc_is_dai_link_bound(card, dai_link))
207461b0088bSMengdong Lin 			continue;
207561b0088bSMengdong Lin 
207661b0088bSMengdong Lin 		ret = soc_init_dai_link(card, dai_link);
207761b0088bSMengdong Lin 		if (ret)
207853e947a0SKuninori Morimoto 			goto probe_end;
207961b0088bSMengdong Lin 		ret = soc_bind_dai_link(card, dai_link);
208061b0088bSMengdong Lin 		if (ret)
208153e947a0SKuninori Morimoto 			goto probe_end;
208261b0088bSMengdong Lin 	}
208361b0088bSMengdong Lin 
208462ae68faSStephen Warren 	/* probe all DAI links on this card */
20851a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
2086bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
20871a497983SMengdong Lin 			ret = soc_probe_link_dais(card, rtd, order);
2088fe3e78e0SMark Brown 			if (ret < 0) {
2089f110bfc7SLiam Girdwood 				dev_err(card->dev,
2090f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
2091f110bfc7SLiam Girdwood 					ret);
209253e947a0SKuninori Morimoto 				goto probe_end;
2093fe3e78e0SMark Brown 			}
2094fe3e78e0SMark Brown 		}
20950168bf0dSLiam Girdwood 	}
2096fe3e78e0SMark Brown 
2097888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
2098b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
2099888df395SMark Brown 
2100b7af1dafSMark Brown 	if (card->controls)
21012c7b696aSMarcel Ziswiler 		snd_soc_add_card_controls(card, card->controls,
21022c7b696aSMarcel Ziswiler 					  card->num_controls);
2103b7af1dafSMark Brown 
2104b8ad29deSMark Brown 	if (card->dapm_routes)
2105b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2106b8ad29deSMark Brown 					card->num_dapm_routes);
2107b8ad29deSMark Brown 
2108f23e860eSNicolin Chen 	if (card->of_dapm_routes)
2109f23e860eSNicolin Chen 		snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2110f23e860eSNicolin Chen 					card->num_of_dapm_routes);
211175d9ac46SMark Brown 
2112861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
2113861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
2114861886d3STakashi Iwai 
2115f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
2116fe3e78e0SMark Brown 		 "%s", card->name);
2117f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
211822de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
2119f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2120f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
2121f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2122f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
2123f0e8ed85SMark Brown 		case '_':
2124f0e8ed85SMark Brown 		case '-':
2125f0e8ed85SMark Brown 		case '\0':
2126f0e8ed85SMark Brown 			break;
2127f0e8ed85SMark Brown 		default:
2128f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
2129f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
2130f0e8ed85SMark Brown 			break;
2131f0e8ed85SMark Brown 		}
2132f0e8ed85SMark Brown 	}
2133fe3e78e0SMark Brown 
213428e9ad92SMark Brown 	if (card->late_probe) {
213528e9ad92SMark Brown 		ret = card->late_probe(card);
213628e9ad92SMark Brown 		if (ret < 0) {
2137f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
213828e9ad92SMark Brown 				card->name, ret);
213953e947a0SKuninori Morimoto 			goto probe_end;
214028e9ad92SMark Brown 		}
214128e9ad92SMark Brown 	}
214228e9ad92SMark Brown 
2143824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
21448c193b8dSLars-Peter Clausen 
2145f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2146fe3e78e0SMark Brown 	if (ret < 0) {
2147f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2148f110bfc7SLiam Girdwood 				ret);
214953e947a0SKuninori Morimoto 		goto probe_end;
2150fe3e78e0SMark Brown 	}
2151fe3e78e0SMark Brown 
2152435c5e25SMark Brown 	card->instantiated = 1;
2153882eab6cSTzung-Bi Shih 	dapm_mark_endpoints_dirty(card);
21544f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2155b19e6e7bSMark Brown 
215653e947a0SKuninori Morimoto probe_end:
215753e947a0SKuninori Morimoto 	if (ret < 0)
215853e947a0SKuninori Morimoto 		soc_cleanup_card_resources(card);
2159db2a4165SFrank Mandarino 
2160f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
216134e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2162db2a4165SFrank Mandarino 
2163b19e6e7bSMark Brown 	return ret;
2164435c5e25SMark Brown }
2165435c5e25SMark Brown 
2166435c5e25SMark Brown /* probes a new socdev */
2167435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2168435c5e25SMark Brown {
2169f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2170435c5e25SMark Brown 
217170a7ca34SVinod Koul 	/*
217270a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
217370a7ca34SVinod Koul 	 * we should not be here in that case so ret error
217470a7ca34SVinod Koul 	 */
217570a7ca34SVinod Koul 	if (!card)
217670a7ca34SVinod Koul 		return -EINVAL;
217770a7ca34SVinod Koul 
2178fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2179f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2180fe4085e8SMark Brown 		 card->name);
2181fe4085e8SMark Brown 
2182435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2183435c5e25SMark Brown 	card->dev = &pdev->dev;
2184f0fba2adSLiam Girdwood 
218528d528c8SMark Brown 	return snd_soc_register_card(card);
2186435c5e25SMark Brown }
2187435c5e25SMark Brown 
2188b0e26485SVinod Koul /* removes a socdev */
2189b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2190b0e26485SVinod Koul {
2191b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2192b0e26485SVinod Koul 
2193c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2194db2a4165SFrank Mandarino 	return 0;
2195db2a4165SFrank Mandarino }
2196db2a4165SFrank Mandarino 
21976f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
219851737470SMark Brown {
21996f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
22001a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
220151737470SMark Brown 
220251737470SMark Brown 	if (!card->instantiated)
2203416356fcSMark Brown 		return 0;
220451737470SMark Brown 
22052c7b696aSMarcel Ziswiler 	/*
22062c7b696aSMarcel Ziswiler 	 * Flush out pmdown_time work - we actually do want to run it
22072c7b696aSMarcel Ziswiler 	 * now, we're shutting down so no imminent restart.
22082c7b696aSMarcel Ziswiler 	 */
220965462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
221051737470SMark Brown 
2211f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2212416356fcSMark Brown 
2213988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
2214bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
221588bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
22160b7990e3SKuninori Morimoto 		struct snd_soc_dai *codec_dai;
22171a497983SMengdong Lin 		int i;
221888bd870fSBenoit Cousson 
2219988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
22200b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
222188bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
222288bd870fSBenoit Cousson 		}
2223988e8cc4SNicolin Chen 	}
2224988e8cc4SNicolin Chen 
2225416356fcSMark Brown 	return 0;
222651737470SMark Brown }
22276f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
222851737470SMark Brown 
22296f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2230b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2231b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2232b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2233b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
22346f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2235b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2236416356fcSMark Brown };
2237deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2238416356fcSMark Brown 
2239db2a4165SFrank Mandarino /* ASoC platform driver */
2240db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2241db2a4165SFrank Mandarino 	.driver		= {
2242db2a4165SFrank Mandarino 		.name		= "soc-audio",
22436f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2244db2a4165SFrank Mandarino 	},
2245db2a4165SFrank Mandarino 	.probe		= soc_probe,
2246db2a4165SFrank Mandarino 	.remove		= soc_remove,
2247db2a4165SFrank Mandarino };
2248db2a4165SFrank Mandarino 
2249096e49d5SMark Brown /**
2250db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2251db2a4165SFrank Mandarino  * @_template: control template
2252db2a4165SFrank Mandarino  * @data: control private data
2253ac11a2b3SMark Brown  * @long_name: control long name
2254efb7ac3fSMark Brown  * @prefix: control name prefix
2255db2a4165SFrank Mandarino  *
2256db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2257db2a4165SFrank Mandarino  *
2258db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2259db2a4165SFrank Mandarino  */
2260db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
22613056557fSMark Brown 				  void *data, const char *long_name,
2262efb7ac3fSMark Brown 				  const char *prefix)
2263db2a4165SFrank Mandarino {
2264db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2265efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2266efb7ac3fSMark Brown 	char *name = NULL;
2267db2a4165SFrank Mandarino 
2268db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2269db2a4165SFrank Mandarino 	template.index = 0;
2270db2a4165SFrank Mandarino 
2271efb7ac3fSMark Brown 	if (!long_name)
2272efb7ac3fSMark Brown 		long_name = template.name;
2273efb7ac3fSMark Brown 
2274efb7ac3fSMark Brown 	if (prefix) {
22752b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2276efb7ac3fSMark Brown 		if (!name)
2277efb7ac3fSMark Brown 			return NULL;
2278efb7ac3fSMark Brown 
2279efb7ac3fSMark Brown 		template.name = name;
2280efb7ac3fSMark Brown 	} else {
2281efb7ac3fSMark Brown 		template.name = long_name;
2282efb7ac3fSMark Brown 	}
2283efb7ac3fSMark Brown 
2284efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2285efb7ac3fSMark Brown 
2286efb7ac3fSMark Brown 	kfree(name);
2287efb7ac3fSMark Brown 
2288efb7ac3fSMark Brown 	return kcontrol;
2289db2a4165SFrank Mandarino }
2290db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2291db2a4165SFrank Mandarino 
2292022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2293022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2294022658beSLiam Girdwood 	const char *prefix, void *data)
2295022658beSLiam Girdwood {
2296022658beSLiam Girdwood 	int err, i;
2297022658beSLiam Girdwood 
2298022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2299022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
23002c7b696aSMarcel Ziswiler 
2301022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2302022658beSLiam Girdwood 						     control->name, prefix));
2303022658beSLiam Girdwood 		if (err < 0) {
2304f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2305f110bfc7SLiam Girdwood 				control->name, err);
2306022658beSLiam Girdwood 			return err;
2307022658beSLiam Girdwood 		}
2308022658beSLiam Girdwood 	}
2309022658beSLiam Girdwood 
2310022658beSLiam Girdwood 	return 0;
2311022658beSLiam Girdwood }
2312022658beSLiam Girdwood 
23134fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
23144fefd698SDimitris Papastamos 					       const char *name)
23154fefd698SDimitris Papastamos {
23164fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
23174fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
23184fefd698SDimitris Papastamos 
23194fefd698SDimitris Papastamos 	if (unlikely(!name))
23204fefd698SDimitris Papastamos 		return NULL;
23214fefd698SDimitris Papastamos 
23224fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
23234fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
23244fefd698SDimitris Papastamos 			return kctl;
23254fefd698SDimitris Papastamos 	return NULL;
23264fefd698SDimitris Papastamos }
23274fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
23284fefd698SDimitris Papastamos 
2329db2a4165SFrank Mandarino /**
23300f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
23310f2780adSLars-Peter Clausen  *
23320f2780adSLars-Peter Clausen  * @component: Component to add controls to
23330f2780adSLars-Peter Clausen  * @controls: Array of controls to add
23340f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
23350f2780adSLars-Peter Clausen  *
23360f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
23370f2780adSLars-Peter Clausen  */
23380f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
23390f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
23400f2780adSLars-Peter Clausen {
23410f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
23420f2780adSLars-Peter Clausen 
23430f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
23440f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
23450f2780adSLars-Peter Clausen }
23460f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
23470f2780adSLars-Peter Clausen 
23480f2780adSLars-Peter Clausen /**
2349022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2350022658beSLiam Girdwood  * Convenience function to add a list of controls.
2351022658beSLiam Girdwood  *
2352022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2353022658beSLiam Girdwood  * @controls: array of controls to add
2354022658beSLiam Girdwood  * @num_controls: number of elements in the array
2355022658beSLiam Girdwood  *
2356022658beSLiam Girdwood  * Return 0 for success, else error.
2357022658beSLiam Girdwood  */
2358022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2359022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2360022658beSLiam Girdwood {
2361022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2362022658beSLiam Girdwood 
2363022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2364022658beSLiam Girdwood 			NULL, soc_card);
2365022658beSLiam Girdwood }
2366022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2367022658beSLiam Girdwood 
2368022658beSLiam Girdwood /**
2369022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2370022658beSLiam Girdwood  * Convienience function to add a list of controls.
2371022658beSLiam Girdwood  *
2372022658beSLiam Girdwood  * @dai: DAI to add controls to
2373022658beSLiam Girdwood  * @controls: array of controls to add
2374022658beSLiam Girdwood  * @num_controls: number of elements in the array
2375022658beSLiam Girdwood  *
2376022658beSLiam Girdwood  * Return 0 for success, else error.
2377022658beSLiam Girdwood  */
2378022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2379022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2380022658beSLiam Girdwood {
2381313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2382022658beSLiam Girdwood 
2383022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2384022658beSLiam Girdwood 			NULL, dai);
2385022658beSLiam Girdwood }
2386022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2387022658beSLiam Girdwood 
2388022658beSLiam Girdwood /**
23898c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
23908c6529dbSLiam Girdwood  * @dai: DAI
23918c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
23928c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
23938c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
23948c6529dbSLiam Girdwood  *
23958c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
23968c6529dbSLiam Girdwood  */
23978c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
23988c6529dbSLiam Girdwood 	unsigned int freq, int dir)
23998c6529dbSLiam Girdwood {
240046471925SKuninori Morimoto 	if (dai->driver->ops->set_sysclk)
2401f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
240271ccef0dSKuninori Morimoto 
240371ccef0dSKuninori Morimoto 	return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2404ec4ee52aSMark Brown 					    freq, dir);
24058c6529dbSLiam Girdwood }
24068c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
24078c6529dbSLiam Girdwood 
24088c6529dbSLiam Girdwood /**
240971ccef0dSKuninori Morimoto  * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
241071ccef0dSKuninori Morimoto  * @component: COMPONENT
241171ccef0dSKuninori Morimoto  * @clk_id: DAI specific clock ID
241271ccef0dSKuninori Morimoto  * @source: Source for the clock
241371ccef0dSKuninori Morimoto  * @freq: new clock frequency in Hz
241471ccef0dSKuninori Morimoto  * @dir: new clock direction - input/output.
241571ccef0dSKuninori Morimoto  *
241671ccef0dSKuninori Morimoto  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
241771ccef0dSKuninori Morimoto  */
24182c7b696aSMarcel Ziswiler int snd_soc_component_set_sysclk(struct snd_soc_component *component,
24192c7b696aSMarcel Ziswiler 				 int clk_id, int source, unsigned int freq,
24202c7b696aSMarcel Ziswiler 				 int dir)
242171ccef0dSKuninori Morimoto {
242271ccef0dSKuninori Morimoto 	if (component->driver->set_sysclk)
242371ccef0dSKuninori Morimoto 		return component->driver->set_sysclk(component, clk_id, source,
242471ccef0dSKuninori Morimoto 						 freq, dir);
242571ccef0dSKuninori Morimoto 
242671ccef0dSKuninori Morimoto 	return -ENOTSUPP;
242771ccef0dSKuninori Morimoto }
242871ccef0dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
242971ccef0dSKuninori Morimoto 
243071ccef0dSKuninori Morimoto /**
24318c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
24328c6529dbSLiam Girdwood  * @dai: DAI
2433ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
24348c6529dbSLiam Girdwood  * @div: new clock divisor.
24358c6529dbSLiam Girdwood  *
24368c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
24378c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
24388c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
24398c6529dbSLiam Girdwood  */
24408c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
24418c6529dbSLiam Girdwood 	int div_id, int div)
24428c6529dbSLiam Girdwood {
244346471925SKuninori Morimoto 	if (dai->driver->ops->set_clkdiv)
2444f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
24458c6529dbSLiam Girdwood 	else
24468c6529dbSLiam Girdwood 		return -EINVAL;
24478c6529dbSLiam Girdwood }
24488c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
24498c6529dbSLiam Girdwood 
24508c6529dbSLiam Girdwood /**
24518c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
24528c6529dbSLiam Girdwood  * @dai: DAI
24538c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
245485488037SMark Brown  * @source: DAI specific source for the PLL
24558c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
24568c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
24578c6529dbSLiam Girdwood  *
24588c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
24598c6529dbSLiam Girdwood  */
246085488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
246185488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
24628c6529dbSLiam Girdwood {
246346471925SKuninori Morimoto 	if (dai->driver->ops->set_pll)
2464f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
246585488037SMark Brown 					 freq_in, freq_out);
2466ef641e5dSKuninori Morimoto 
2467ef641e5dSKuninori Morimoto 	return snd_soc_component_set_pll(dai->component, pll_id, source,
2468ec4ee52aSMark Brown 					 freq_in, freq_out);
24698c6529dbSLiam Girdwood }
24708c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
24718c6529dbSLiam Girdwood 
2472ec4ee52aSMark Brown /*
2473ef641e5dSKuninori Morimoto  * snd_soc_component_set_pll - configure component PLL.
2474ef641e5dSKuninori Morimoto  * @component: COMPONENT
2475ef641e5dSKuninori Morimoto  * @pll_id: DAI specific PLL ID
2476ef641e5dSKuninori Morimoto  * @source: DAI specific source for the PLL
2477ef641e5dSKuninori Morimoto  * @freq_in: PLL input clock frequency in Hz
2478ef641e5dSKuninori Morimoto  * @freq_out: requested PLL output clock frequency in Hz
2479ef641e5dSKuninori Morimoto  *
2480ef641e5dSKuninori Morimoto  * Configures and enables PLL to generate output clock based on input clock.
2481ef641e5dSKuninori Morimoto  */
2482ef641e5dSKuninori Morimoto int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2483ef641e5dSKuninori Morimoto 			      int source, unsigned int freq_in,
2484ef641e5dSKuninori Morimoto 			      unsigned int freq_out)
2485ef641e5dSKuninori Morimoto {
2486ef641e5dSKuninori Morimoto 	if (component->driver->set_pll)
2487ef641e5dSKuninori Morimoto 		return component->driver->set_pll(component, pll_id, source,
2488ef641e5dSKuninori Morimoto 						  freq_in, freq_out);
2489ef641e5dSKuninori Morimoto 
2490ef641e5dSKuninori Morimoto 	return -EINVAL;
2491ef641e5dSKuninori Morimoto }
2492ef641e5dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2493ef641e5dSKuninori Morimoto 
24948c6529dbSLiam Girdwood /**
2495e54cf76bSLiam Girdwood  * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2496e54cf76bSLiam Girdwood  * @dai: DAI
2497231b86b1SMasanari Iida  * @ratio: Ratio of BCLK to Sample rate.
2498e54cf76bSLiam Girdwood  *
2499e54cf76bSLiam Girdwood  * Configures the DAI for a preset BCLK to sample rate ratio.
2500e54cf76bSLiam Girdwood  */
2501e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2502e54cf76bSLiam Girdwood {
250346471925SKuninori Morimoto 	if (dai->driver->ops->set_bclk_ratio)
2504e54cf76bSLiam Girdwood 		return dai->driver->ops->set_bclk_ratio(dai, ratio);
2505e54cf76bSLiam Girdwood 	else
2506e54cf76bSLiam Girdwood 		return -EINVAL;
2507e54cf76bSLiam Girdwood }
2508e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2509e54cf76bSLiam Girdwood 
2510e54cf76bSLiam Girdwood /**
25118c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
25128c6529dbSLiam Girdwood  * @dai: DAI
2513bb19ba2aSRandy Dunlap  * @fmt: SND_SOC_DAIFMT_* format value.
25148c6529dbSLiam Girdwood  *
25158c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
25168c6529dbSLiam Girdwood  */
25178c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
25188c6529dbSLiam Girdwood {
25195e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
25205e4ba569SShawn Guo 		return -ENOTSUPP;
25215e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
25228c6529dbSLiam Girdwood }
25238c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
25248c6529dbSLiam Girdwood 
25258c6529dbSLiam Girdwood /**
2526e5c21514SXiubo Li  * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
252789c67857SXiubo Li  * @slots: Number of slots in use.
252889c67857SXiubo Li  * @tx_mask: bitmask representing active TX slots.
252989c67857SXiubo Li  * @rx_mask: bitmask representing active RX slots.
253089c67857SXiubo Li  *
253189c67857SXiubo Li  * Generates the TDM tx and rx slot default masks for DAI.
253289c67857SXiubo Li  */
2533e5c21514SXiubo Li static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
253489c67857SXiubo Li 				       unsigned int *tx_mask,
253589c67857SXiubo Li 				       unsigned int *rx_mask)
253689c67857SXiubo Li {
253789c67857SXiubo Li 	if (*tx_mask || *rx_mask)
253889c67857SXiubo Li 		return 0;
253989c67857SXiubo Li 
254089c67857SXiubo Li 	if (!slots)
254189c67857SXiubo Li 		return -EINVAL;
254289c67857SXiubo Li 
254389c67857SXiubo Li 	*tx_mask = (1 << slots) - 1;
254489c67857SXiubo Li 	*rx_mask = (1 << slots) - 1;
254589c67857SXiubo Li 
254689c67857SXiubo Li 	return 0;
254789c67857SXiubo Li }
254889c67857SXiubo Li 
254989c67857SXiubo Li /**
2550e46c9366SLars-Peter Clausen  * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2551e46c9366SLars-Peter Clausen  * @dai: The DAI to configure
2552a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2553a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
25548c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2555a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
25568c6529dbSLiam Girdwood  *
2557e46c9366SLars-Peter Clausen  * This function configures the specified DAI for TDM operation. @slot contains
2558e46c9366SLars-Peter Clausen  * the total number of slots of the TDM stream and @slot_with the width of each
2559e46c9366SLars-Peter Clausen  * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2560e46c9366SLars-Peter Clausen  * active slots of the TDM stream for the specified DAI, i.e. which slots the
2561e46c9366SLars-Peter Clausen  * DAI should write to or read from. If a bit is set the corresponding slot is
2562e46c9366SLars-Peter Clausen  * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2563e46c9366SLars-Peter Clausen  * the first slot, bit 1 to the second slot and so on. The first active slot
2564e46c9366SLars-Peter Clausen  * maps to the first channel of the DAI, the second active slot to the second
2565e46c9366SLars-Peter Clausen  * channel and so on.
2566e46c9366SLars-Peter Clausen  *
2567e46c9366SLars-Peter Clausen  * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2568e46c9366SLars-Peter Clausen  * @rx_mask and @slot_width will be ignored.
2569e46c9366SLars-Peter Clausen  *
2570e46c9366SLars-Peter Clausen  * Returns 0 on success, a negative error code otherwise.
25718c6529dbSLiam Girdwood  */
25728c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2573a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
25748c6529dbSLiam Girdwood {
257546471925SKuninori Morimoto 	if (dai->driver->ops->xlate_tdm_slot_mask)
2576e5c21514SXiubo Li 		dai->driver->ops->xlate_tdm_slot_mask(slots,
257789c67857SXiubo Li 						&tx_mask, &rx_mask);
257889c67857SXiubo Li 	else
2579e5c21514SXiubo Li 		snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
258089c67857SXiubo Li 
258188bd870fSBenoit Cousson 	dai->tx_mask = tx_mask;
258288bd870fSBenoit Cousson 	dai->rx_mask = rx_mask;
258388bd870fSBenoit Cousson 
258446471925SKuninori Morimoto 	if (dai->driver->ops->set_tdm_slot)
2585f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2586a5479e38SDaniel Ribeiro 				slots, slot_width);
25878c6529dbSLiam Girdwood 	else
2588b2cbb6e1SXiubo Li 		return -ENOTSUPP;
25898c6529dbSLiam Girdwood }
25908c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
25918c6529dbSLiam Girdwood 
25928c6529dbSLiam Girdwood /**
2593472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2594472df3cbSBarry Song  * @dai: DAI
2595472df3cbSBarry Song  * @tx_num: how many TX channels
2596472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2597472df3cbSBarry Song  *           0~num-1 uses
2598472df3cbSBarry Song  * @rx_num: how many RX channels
2599472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2600472df3cbSBarry Song  *           0~num-1 uses
2601472df3cbSBarry Song  *
2602472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2603472df3cbSBarry Song  */
2604472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2605472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
2606472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
2607472df3cbSBarry Song {
260846471925SKuninori Morimoto 	if (dai->driver->ops->set_channel_map)
2609f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2610472df3cbSBarry Song 			rx_num, rx_slot);
2611472df3cbSBarry Song 	else
2612472df3cbSBarry Song 		return -EINVAL;
2613472df3cbSBarry Song }
2614472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2615472df3cbSBarry Song 
2616472df3cbSBarry Song /**
2617467b061fSSrinivas Kandagatla  * snd_soc_dai_get_channel_map - Get DAI audio channel map
2618467b061fSSrinivas Kandagatla  * @dai: DAI
2619467b061fSSrinivas Kandagatla  * @tx_num: how many TX channels
2620467b061fSSrinivas Kandagatla  * @tx_slot: pointer to an array which imply the TX slot number channel
2621467b061fSSrinivas Kandagatla  *           0~num-1 uses
2622467b061fSSrinivas Kandagatla  * @rx_num: how many RX channels
2623467b061fSSrinivas Kandagatla  * @rx_slot: pointer to an array which imply the RX slot number channel
2624467b061fSSrinivas Kandagatla  *           0~num-1 uses
2625467b061fSSrinivas Kandagatla  */
2626467b061fSSrinivas Kandagatla int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
2627467b061fSSrinivas Kandagatla 	unsigned int *tx_num, unsigned int *tx_slot,
2628467b061fSSrinivas Kandagatla 	unsigned int *rx_num, unsigned int *rx_slot)
2629467b061fSSrinivas Kandagatla {
2630467b061fSSrinivas Kandagatla 	if (dai->driver->ops->get_channel_map)
2631467b061fSSrinivas Kandagatla 		return dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
2632467b061fSSrinivas Kandagatla 			rx_num, rx_slot);
2633467b061fSSrinivas Kandagatla 	else
2634467b061fSSrinivas Kandagatla 		return -ENOTSUPP;
2635467b061fSSrinivas Kandagatla }
2636467b061fSSrinivas Kandagatla EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
2637467b061fSSrinivas Kandagatla 
2638467b061fSSrinivas Kandagatla /**
26398c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
26408c6529dbSLiam Girdwood  * @dai: DAI
26418c6529dbSLiam Girdwood  * @tristate: tristate enable
26428c6529dbSLiam Girdwood  *
26438c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
26448c6529dbSLiam Girdwood  */
26458c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
26468c6529dbSLiam Girdwood {
264746471925SKuninori Morimoto 	if (dai->driver->ops->set_tristate)
2648f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
26498c6529dbSLiam Girdwood 	else
26508c6529dbSLiam Girdwood 		return -EINVAL;
26518c6529dbSLiam Girdwood }
26528c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
26538c6529dbSLiam Girdwood 
26548c6529dbSLiam Girdwood /**
26558c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
26568c6529dbSLiam Girdwood  * @dai: DAI
26578c6529dbSLiam Girdwood  * @mute: mute enable
2658da18396fSMark Brown  * @direction: stream to mute
26598c6529dbSLiam Girdwood  *
26608c6529dbSLiam Girdwood  * Mutes the DAI DAC.
26618c6529dbSLiam Girdwood  */
2662da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2663da18396fSMark Brown 			     int direction)
26648c6529dbSLiam Girdwood {
2665da18396fSMark Brown 	if (dai->driver->ops->mute_stream)
2666da18396fSMark Brown 		return dai->driver->ops->mute_stream(dai, mute, direction);
2667da18396fSMark Brown 	else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2668da18396fSMark Brown 		 dai->driver->ops->digital_mute)
2669f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
26708c6529dbSLiam Girdwood 	else
267104570c62SMark Brown 		return -ENOTSUPP;
26728c6529dbSLiam Girdwood }
26738c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
26748c6529dbSLiam Girdwood 
2675e894efefSSrinivas Kandagatla static int snd_soc_bind_card(struct snd_soc_card *card)
2676e894efefSSrinivas Kandagatla {
2677e894efefSSrinivas Kandagatla 	struct snd_soc_pcm_runtime *rtd;
2678e894efefSSrinivas Kandagatla 	int ret;
2679e894efefSSrinivas Kandagatla 
2680e894efefSSrinivas Kandagatla 	ret = snd_soc_instantiate_card(card);
2681e894efefSSrinivas Kandagatla 	if (ret != 0)
2682e894efefSSrinivas Kandagatla 		return ret;
2683e894efefSSrinivas Kandagatla 
2684e894efefSSrinivas Kandagatla 	/* deactivate pins to sleep state */
2685bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
2686e894efefSSrinivas Kandagatla 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2687e894efefSSrinivas Kandagatla 		struct snd_soc_dai *codec_dai;
2688e894efefSSrinivas Kandagatla 		int j;
2689e894efefSSrinivas Kandagatla 
2690e894efefSSrinivas Kandagatla 		for_each_rtd_codec_dai(rtd, j, codec_dai) {
2691e894efefSSrinivas Kandagatla 			if (!codec_dai->active)
2692e894efefSSrinivas Kandagatla 				pinctrl_pm_select_sleep_state(codec_dai->dev);
2693e894efefSSrinivas Kandagatla 		}
2694e894efefSSrinivas Kandagatla 
2695e894efefSSrinivas Kandagatla 		if (!cpu_dai->active)
2696e894efefSSrinivas Kandagatla 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
2697e894efefSSrinivas Kandagatla 	}
2698e894efefSSrinivas Kandagatla 
2699e894efefSSrinivas Kandagatla 	return ret;
2700e894efefSSrinivas Kandagatla }
2701e894efefSSrinivas Kandagatla 
2702c5af3a2eSMark Brown /**
2703c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2704c5af3a2eSMark Brown  *
2705ac11a2b3SMark Brown  * @card: Card to register
2706c5af3a2eSMark Brown  *
2707c5af3a2eSMark Brown  */
270870a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2709c5af3a2eSMark Brown {
2710923c5e61SMengdong Lin 	int i, ret;
27117fe072b4SKuninori Morimoto 	struct snd_soc_dai_link *link;
2712f0fba2adSLiam Girdwood 
2713c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2714c5af3a2eSMark Brown 		return -EINVAL;
2715c5af3a2eSMark Brown 
271604eb1efcSRohit kumar 	mutex_lock(&client_mutex);
27177fe072b4SKuninori Morimoto 	for_each_card_prelinks(card, i, link) {
27185a504963SStephen Warren 
2719923c5e61SMengdong Lin 		ret = soc_init_dai_link(card, link);
272088bd870fSBenoit Cousson 		if (ret) {
2721923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: failed to init link %s\n",
2722923c5e61SMengdong Lin 				link->name);
272304eb1efcSRohit kumar 			mutex_unlock(&client_mutex);
272488bd870fSBenoit Cousson 			return ret;
272588bd870fSBenoit Cousson 		}
27265a504963SStephen Warren 	}
272704eb1efcSRohit kumar 	mutex_unlock(&client_mutex);
27285a504963SStephen Warren 
2729ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2730ed77cc12SMark Brown 
2731111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
2732111c6419SStephen Warren 
2733f8f80361SMengdong Lin 	INIT_LIST_HEAD(&card->dai_link_list);
2734f0fba2adSLiam Girdwood 
27351a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
27369115171aSMark Brown 	card->num_rtd = 0;
2737db2a4165SFrank Mandarino 
2738db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
27398a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2740db2a4165SFrank Mandarino 	card->instantiated = 0;
2741db2a4165SFrank Mandarino 	mutex_init(&card->mutex);
2742a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
2743a9764869SKaiChieh Chuang 	spin_lock_init(&card->dpcm_lock);
2744db2a4165SFrank Mandarino 
2745e894efefSSrinivas Kandagatla 	return snd_soc_bind_card(card);
2746db2a4165SFrank Mandarino }
274770a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2748db2a4165SFrank Mandarino 
2749e894efefSSrinivas Kandagatla static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2750e894efefSSrinivas Kandagatla {
2751f96fb7d1SRanjani Sridharan 	struct snd_soc_pcm_runtime *rtd;
2752f96fb7d1SRanjani Sridharan 	int order;
2753f96fb7d1SRanjani Sridharan 
2754e894efefSSrinivas Kandagatla 	if (card->instantiated) {
2755e894efefSSrinivas Kandagatla 		card->instantiated = false;
2756e894efefSSrinivas Kandagatla 		snd_soc_dapm_shutdown(card);
275753e947a0SKuninori Morimoto 		snd_soc_flush_all_delayed_work(card);
2758f96fb7d1SRanjani Sridharan 
2759f96fb7d1SRanjani Sridharan 		/* remove all components used by DAI links on this card */
2760f96fb7d1SRanjani Sridharan 		for_each_comp_order(order) {
2761f96fb7d1SRanjani Sridharan 			for_each_card_rtds(card, rtd) {
2762f96fb7d1SRanjani Sridharan 				soc_remove_link_components(card, rtd, order);
2763f96fb7d1SRanjani Sridharan 			}
2764f96fb7d1SRanjani Sridharan 		}
2765f96fb7d1SRanjani Sridharan 
2766e894efefSSrinivas Kandagatla 		soc_cleanup_card_resources(card);
2767e894efefSSrinivas Kandagatla 		if (!unregister)
2768e894efefSSrinivas Kandagatla 			list_add(&card->list, &unbind_card_list);
2769e894efefSSrinivas Kandagatla 	} else {
2770e894efefSSrinivas Kandagatla 		if (unregister)
2771e894efefSSrinivas Kandagatla 			list_del(&card->list);
2772e894efefSSrinivas Kandagatla 	}
2773e894efefSSrinivas Kandagatla }
2774e894efefSSrinivas Kandagatla 
2775db2a4165SFrank Mandarino /**
2776db2a4165SFrank Mandarino  * snd_soc_unregister_card - Unregister a card with the ASoC core
2777db2a4165SFrank Mandarino  *
2778db2a4165SFrank Mandarino  * @card: Card to unregister
2779db2a4165SFrank Mandarino  *
2780db2a4165SFrank Mandarino  */
278170a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2782db2a4165SFrank Mandarino {
2783e894efefSSrinivas Kandagatla 	snd_soc_unbind_card(card, true);
2784f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2785db2a4165SFrank Mandarino 
2786db2a4165SFrank Mandarino 	return 0;
2787db2a4165SFrank Mandarino }
278870a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2789db2a4165SFrank Mandarino 
2790db2a4165SFrank Mandarino /*
2791db2a4165SFrank Mandarino  * Simplify DAI link configuration by removing ".-1" from device names
2792db2a4165SFrank Mandarino  * and sanitizing names.
2793db2a4165SFrank Mandarino  */
27940b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2795db2a4165SFrank Mandarino {
2796db2a4165SFrank Mandarino 	char *found, name[NAME_SIZE];
2797db2a4165SFrank Mandarino 	int id1, id2;
2798db2a4165SFrank Mandarino 
2799db2a4165SFrank Mandarino 	if (dev_name(dev) == NULL)
2800db2a4165SFrank Mandarino 		return NULL;
2801db2a4165SFrank Mandarino 
280258818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2803db2a4165SFrank Mandarino 
2804db2a4165SFrank Mandarino 	/* are we a "%s.%d" name (platform and SPI components) */
2805c5af3a2eSMark Brown 	found = strstr(name, dev->driver->name);
2806c5af3a2eSMark Brown 	if (found) {
2807c5af3a2eSMark Brown 		/* get ID */
2808c5af3a2eSMark Brown 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2809c5af3a2eSMark Brown 
2810c5af3a2eSMark Brown 			/* discard ID from name if ID == -1 */
2811c5af3a2eSMark Brown 			if (*id == -1)
2812c5af3a2eSMark Brown 				found[strlen(dev->driver->name)] = '\0';
2813c5af3a2eSMark Brown 		}
2814c5af3a2eSMark Brown 
2815c5af3a2eSMark Brown 	} else {
2816c5af3a2eSMark Brown 		/* I2C component devices are named "bus-addr" */
2817c5af3a2eSMark Brown 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2818c5af3a2eSMark Brown 			char tmp[NAME_SIZE];
2819c5af3a2eSMark Brown 
2820c5af3a2eSMark Brown 			/* create unique ID number from I2C addr and bus */
2821c5af3a2eSMark Brown 			*id = ((id1 & 0xffff) << 16) + id2;
2822c5af3a2eSMark Brown 
2823c5af3a2eSMark Brown 			/* sanitize component name for DAI link creation */
28242c7b696aSMarcel Ziswiler 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
28252c7b696aSMarcel Ziswiler 				 name);
282658818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2827c5af3a2eSMark Brown 		} else
2828c5af3a2eSMark Brown 			*id = 0;
2829c5af3a2eSMark Brown 	}
2830c5af3a2eSMark Brown 
2831c5af3a2eSMark Brown 	return kstrdup(name, GFP_KERNEL);
2832c5af3a2eSMark Brown }
2833c5af3a2eSMark Brown 
2834c5af3a2eSMark Brown /*
2835c5af3a2eSMark Brown  * Simplify DAI link naming for single devices with multiple DAIs by removing
2836c5af3a2eSMark Brown  * any ".-1" and using the DAI name (instead of device name).
2837c5af3a2eSMark Brown  */
2838c5af3a2eSMark Brown static inline char *fmt_multiple_name(struct device *dev,
2839c5af3a2eSMark Brown 		struct snd_soc_dai_driver *dai_drv)
2840c5af3a2eSMark Brown {
2841c5af3a2eSMark Brown 	if (dai_drv->name == NULL) {
284210e8aa9aSMichał Mirosław 		dev_err(dev,
284310e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
284410e8aa9aSMichał Mirosław 			dev_name(dev));
2845c5af3a2eSMark Brown 		return NULL;
2846c5af3a2eSMark Brown 	}
2847c5af3a2eSMark Brown 
2848c5af3a2eSMark Brown 	return kstrdup(dai_drv->name, GFP_KERNEL);
2849c5af3a2eSMark Brown }
2850c5af3a2eSMark Brown 
2851c5af3a2eSMark Brown /**
285232c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
28539115171aSMark Brown  *
285432c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
28559115171aSMark Brown  */
285632c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
28579115171aSMark Brown {
28585c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
28599115171aSMark Brown 
286015a0c645SKuninori Morimoto 	for_each_component_dais_safe(component, dai, _dai) {
286132c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
286232c9ba54SLars-Peter Clausen 			dai->name);
28639115171aSMark Brown 		list_del(&dai->list);
2864f0fba2adSLiam Girdwood 		kfree(dai->name);
2865f0fba2adSLiam Girdwood 		kfree(dai);
28669115171aSMark Brown 	}
286732c9ba54SLars-Peter Clausen }
28689115171aSMark Brown 
28695e4fb372SMengdong Lin /* Create a DAI and add it to the component's DAI list */
28705e4fb372SMengdong Lin static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
28715e4fb372SMengdong Lin 	struct snd_soc_dai_driver *dai_drv,
28725e4fb372SMengdong Lin 	bool legacy_dai_naming)
28735e4fb372SMengdong Lin {
28745e4fb372SMengdong Lin 	struct device *dev = component->dev;
28755e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
28765e4fb372SMengdong Lin 
28775e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
28785e4fb372SMengdong Lin 
28795e4fb372SMengdong Lin 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
28805e4fb372SMengdong Lin 	if (dai == NULL)
28815e4fb372SMengdong Lin 		return NULL;
28825e4fb372SMengdong Lin 
28835e4fb372SMengdong Lin 	/*
28845e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
28855e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
28865e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
28875e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
28885e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
28895e4fb372SMengdong Lin 	 * component-less anymore.
28905e4fb372SMengdong Lin 	 */
28915e4fb372SMengdong Lin 	if (legacy_dai_naming &&
28925e4fb372SMengdong Lin 	    (dai_drv->id == 0 || dai_drv->name == NULL)) {
28935e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
28945e4fb372SMengdong Lin 	} else {
28955e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
28965e4fb372SMengdong Lin 		if (dai_drv->id)
28975e4fb372SMengdong Lin 			dai->id = dai_drv->id;
28985e4fb372SMengdong Lin 		else
28995e4fb372SMengdong Lin 			dai->id = component->num_dai;
29005e4fb372SMengdong Lin 	}
29015e4fb372SMengdong Lin 	if (dai->name == NULL) {
29025e4fb372SMengdong Lin 		kfree(dai);
29035e4fb372SMengdong Lin 		return NULL;
29045e4fb372SMengdong Lin 	}
29055e4fb372SMengdong Lin 
29065e4fb372SMengdong Lin 	dai->component = component;
29075e4fb372SMengdong Lin 	dai->dev = dev;
29085e4fb372SMengdong Lin 	dai->driver = dai_drv;
29095e4fb372SMengdong Lin 	if (!dai->driver->ops)
29105e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
29115e4fb372SMengdong Lin 
291215a0c645SKuninori Morimoto 	/* see for_each_component_dais */
291358bf4179SKuninori Morimoto 	list_add_tail(&dai->list, &component->dai_list);
29145e4fb372SMengdong Lin 	component->num_dai++;
29155e4fb372SMengdong Lin 
29165e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
29175e4fb372SMengdong Lin 	return dai;
29185e4fb372SMengdong Lin }
29195e4fb372SMengdong Lin 
29209115171aSMark Brown /**
292132c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
29229115171aSMark Brown  *
29236106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
29246106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
2925ac11a2b3SMark Brown  * @count: Number of DAIs
29269115171aSMark Brown  */
292732c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
29282c7b696aSMarcel Ziswiler 				 struct snd_soc_dai_driver *dai_drv,
29292c7b696aSMarcel Ziswiler 				 size_t count)
29309115171aSMark Brown {
29316106d129SLars-Peter Clausen 	struct device *dev = component->dev;
2932f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
293332c9ba54SLars-Peter Clausen 	unsigned int i;
293432c9ba54SLars-Peter Clausen 	int ret;
2935f0fba2adSLiam Girdwood 
29365b5e0928SAlexey Dobriyan 	dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
29379115171aSMark Brown 
29389115171aSMark Brown 	for (i = 0; i < count; i++) {
2939f0fba2adSLiam Girdwood 
29402c7b696aSMarcel Ziswiler 		dai = soc_add_dai(component, dai_drv + i, count == 1 &&
29412c7b696aSMarcel Ziswiler 				  !component->driver->non_legacy_dai_naming);
2942c46e0079SAxel Lin 		if (dai == NULL) {
2943c46e0079SAxel Lin 			ret = -ENOMEM;
2944c46e0079SAxel Lin 			goto err;
2945c46e0079SAxel Lin 		}
2946f0fba2adSLiam Girdwood 	}
2947f0fba2adSLiam Girdwood 
29489115171aSMark Brown 	return 0;
29499115171aSMark Brown 
29509115171aSMark Brown err:
295132c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
29529115171aSMark Brown 
29539115171aSMark Brown 	return ret;
29549115171aSMark Brown }
29559115171aSMark Brown 
295668003e6cSMengdong Lin /**
295768003e6cSMengdong Lin  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
295868003e6cSMengdong Lin  *
295968003e6cSMengdong Lin  * @component: The component the DAIs are registered for
296068003e6cSMengdong Lin  * @dai_drv: DAI driver to use for the DAI
296168003e6cSMengdong Lin  *
296268003e6cSMengdong Lin  * Topology can use this API to register DAIs when probing a component.
296368003e6cSMengdong Lin  * These DAIs's widgets will be freed in the card cleanup and the DAIs
296468003e6cSMengdong Lin  * will be freed in the component cleanup.
296568003e6cSMengdong Lin  */
296668003e6cSMengdong Lin int snd_soc_register_dai(struct snd_soc_component *component,
296768003e6cSMengdong Lin 	struct snd_soc_dai_driver *dai_drv)
296868003e6cSMengdong Lin {
296968003e6cSMengdong Lin 	struct snd_soc_dapm_context *dapm =
297068003e6cSMengdong Lin 		snd_soc_component_get_dapm(component);
297168003e6cSMengdong Lin 	struct snd_soc_dai *dai;
297268003e6cSMengdong Lin 	int ret;
297368003e6cSMengdong Lin 
297468003e6cSMengdong Lin 	if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
297568003e6cSMengdong Lin 		dev_err(component->dev, "Invalid dai type %d\n",
297668003e6cSMengdong Lin 			dai_drv->dobj.type);
297768003e6cSMengdong Lin 		return -EINVAL;
297868003e6cSMengdong Lin 	}
297968003e6cSMengdong Lin 
298068003e6cSMengdong Lin 	lockdep_assert_held(&client_mutex);
298168003e6cSMengdong Lin 	dai = soc_add_dai(component, dai_drv, false);
298268003e6cSMengdong Lin 	if (!dai)
298368003e6cSMengdong Lin 		return -ENOMEM;
298468003e6cSMengdong Lin 
29852c7b696aSMarcel Ziswiler 	/*
29862c7b696aSMarcel Ziswiler 	 * Create the DAI widgets here. After adding DAIs, topology may
298768003e6cSMengdong Lin 	 * also add routes that need these widgets as source or sink.
298868003e6cSMengdong Lin 	 */
298968003e6cSMengdong Lin 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
299068003e6cSMengdong Lin 	if (ret != 0) {
299168003e6cSMengdong Lin 		dev_err(component->dev,
299268003e6cSMengdong Lin 			"Failed to create DAI widgets %d\n", ret);
299368003e6cSMengdong Lin 	}
299468003e6cSMengdong Lin 
299568003e6cSMengdong Lin 	return ret;
299668003e6cSMengdong Lin }
299768003e6cSMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_register_dai);
299868003e6cSMengdong Lin 
299914e8bdebSLars-Peter Clausen static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
300014e8bdebSLars-Peter Clausen 	enum snd_soc_dapm_type type, int subseq)
3001d191bd8dSKuninori Morimoto {
300214e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
3003d191bd8dSKuninori Morimoto 
300414e8bdebSLars-Peter Clausen 	component->driver->seq_notifier(component, type, subseq);
300514e8bdebSLars-Peter Clausen }
3006d191bd8dSKuninori Morimoto 
300714e8bdebSLars-Peter Clausen static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
300814e8bdebSLars-Peter Clausen 	int event)
300914e8bdebSLars-Peter Clausen {
301014e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
301114e8bdebSLars-Peter Clausen 
301214e8bdebSLars-Peter Clausen 	return component->driver->stream_event(component, event);
301314e8bdebSLars-Peter Clausen }
301414e8bdebSLars-Peter Clausen 
30157ba236ceSKuninori Morimoto static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
30167ba236ceSKuninori Morimoto 					enum snd_soc_bias_level level)
30177ba236ceSKuninori Morimoto {
30187ba236ceSKuninori Morimoto 	struct snd_soc_component *component = dapm->component;
30197ba236ceSKuninori Morimoto 
30207ba236ceSKuninori Morimoto 	return component->driver->set_bias_level(component, level);
30217ba236ceSKuninori Morimoto }
30227ba236ceSKuninori Morimoto 
3023bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
3024bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
3025d191bd8dSKuninori Morimoto {
3026ce0fc93aSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
3027ce0fc93aSLars-Peter Clausen 
3028bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
3029bb13109dSLars-Peter Clausen 	if (!component->name) {
3030bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
3031d191bd8dSKuninori Morimoto 		return -ENOMEM;
3032d191bd8dSKuninori Morimoto 	}
3033d191bd8dSKuninori Morimoto 
3034bb13109dSLars-Peter Clausen 	component->dev = dev;
3035bb13109dSLars-Peter Clausen 	component->driver = driver;
3036e2c330b9SLars-Peter Clausen 
303788c27465SKuninori Morimoto 	dapm = snd_soc_component_get_dapm(component);
3038ce0fc93aSLars-Peter Clausen 	dapm->dev = dev;
3039ce0fc93aSLars-Peter Clausen 	dapm->component = component;
3040ce0fc93aSLars-Peter Clausen 	dapm->bias_level = SND_SOC_BIAS_OFF;
30417ba236ceSKuninori Morimoto 	dapm->idle_bias_off = !driver->idle_bias_on;
30427ba236ceSKuninori Morimoto 	dapm->suspend_bias_off = driver->suspend_bias_off;
304314e8bdebSLars-Peter Clausen 	if (driver->seq_notifier)
304414e8bdebSLars-Peter Clausen 		dapm->seq_notifier = snd_soc_component_seq_notifier;
304514e8bdebSLars-Peter Clausen 	if (driver->stream_event)
304614e8bdebSLars-Peter Clausen 		dapm->stream_event = snd_soc_component_stream_event;
30477ba236ceSKuninori Morimoto 	if (driver->set_bias_level)
30487ba236ceSKuninori Morimoto 		dapm->set_bias_level = snd_soc_component_set_bias_level;
3049ce0fc93aSLars-Peter Clausen 
3050bb13109dSLars-Peter Clausen 	INIT_LIST_HEAD(&component->dai_list);
3051bb13109dSLars-Peter Clausen 	mutex_init(&component->io_mutex);
3052bb13109dSLars-Peter Clausen 
3053bb13109dSLars-Peter Clausen 	return 0;
3054d191bd8dSKuninori Morimoto }
3055d191bd8dSKuninori Morimoto 
305620feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
3057886f5692SLars-Peter Clausen {
3058886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
305920feb881SLars-Peter Clausen 
3060886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
3061886f5692SLars-Peter Clausen 	if (val_bytes > 0)
3062886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
3063886f5692SLars-Peter Clausen }
306420feb881SLars-Peter Clausen 
3065e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
3066e874bf5fSLars-Peter Clausen 
306720feb881SLars-Peter Clausen /**
30682c7b696aSMarcel Ziswiler  * snd_soc_component_init_regmap() - Initialize regmap instance for the
30692c7b696aSMarcel Ziswiler  *                                   component
307020feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
307120feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
307220feb881SLars-Peter Clausen  *
307320feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
307420feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
307520feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
307620feb881SLars-Peter Clausen  * before the first IO attempt of the component.
307720feb881SLars-Peter Clausen  */
307820feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
307920feb881SLars-Peter Clausen 	struct regmap *regmap)
308020feb881SLars-Peter Clausen {
308120feb881SLars-Peter Clausen 	component->regmap = regmap;
308220feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
3083886f5692SLars-Peter Clausen }
308420feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
308520feb881SLars-Peter Clausen 
308620feb881SLars-Peter Clausen /**
30872c7b696aSMarcel Ziswiler  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
30882c7b696aSMarcel Ziswiler  *                                   component
308920feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
309020feb881SLars-Peter Clausen  *
309120feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
309220feb881SLars-Peter Clausen  * removes the regmap instance from the component.
309320feb881SLars-Peter Clausen  *
309420feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
309520feb881SLars-Peter Clausen  * to initialize the regmap instance.
309620feb881SLars-Peter Clausen  */
309720feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
309820feb881SLars-Peter Clausen {
309920feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
310020feb881SLars-Peter Clausen 	component->regmap = NULL;
310120feb881SLars-Peter Clausen }
310220feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3103886f5692SLars-Peter Clausen 
3104e874bf5fSLars-Peter Clausen #endif
3105d191bd8dSKuninori Morimoto 
3106359c71eeSKuninori Morimoto static void snd_soc_component_add(struct snd_soc_component *component)
3107bb13109dSLars-Peter Clausen {
3108359c71eeSKuninori Morimoto 	mutex_lock(&client_mutex);
3109359c71eeSKuninori Morimoto 
3110999f7f5aSKuninori Morimoto 	if (!component->driver->write && !component->driver->read) {
311120feb881SLars-Peter Clausen 		if (!component->regmap)
31122c7b696aSMarcel Ziswiler 			component->regmap = dev_get_regmap(component->dev,
31132c7b696aSMarcel Ziswiler 							   NULL);
311420feb881SLars-Peter Clausen 		if (component->regmap)
311520feb881SLars-Peter Clausen 			snd_soc_component_setup_regmap(component);
311620feb881SLars-Peter Clausen 	}
3117886f5692SLars-Peter Clausen 
3118368dee94SKuninori Morimoto 	/* see for_each_component */
3119bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
31208a978234SLiam Girdwood 	INIT_LIST_HEAD(&component->dobj_list);
3121d191bd8dSKuninori Morimoto 
3122d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
3123bb13109dSLars-Peter Clausen }
3124d191bd8dSKuninori Morimoto 
3125bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
3126bb13109dSLars-Peter Clausen {
3127bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
3128bb13109dSLars-Peter Clausen 	kfree(component->name);
3129bb13109dSLars-Peter Clausen }
3130d191bd8dSKuninori Morimoto 
3131bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3132bb13109dSLars-Peter Clausen {
3133c12c1aadSKuninori Morimoto 	struct snd_soc_card *card = component->card;
3134c12c1aadSKuninori Morimoto 
3135c12c1aadSKuninori Morimoto 	if (card)
3136e894efefSSrinivas Kandagatla 		snd_soc_unbind_card(card, false);
3137c12c1aadSKuninori Morimoto 
3138bb13109dSLars-Peter Clausen 	list_del(&component->list);
3139bb13109dSLars-Peter Clausen }
3140d191bd8dSKuninori Morimoto 
3141273d778eSKuninori Morimoto #define ENDIANNESS_MAP(name) \
3142273d778eSKuninori Morimoto 	(SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3143273d778eSKuninori Morimoto static u64 endianness_format_map[] = {
3144273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S16_),
3145273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U16_),
3146273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_),
3147273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_),
3148273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S32_),
3149273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U32_),
3150273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_3),
3151273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_3),
3152273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S20_3),
3153273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U20_3),
3154273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S18_3),
3155273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U18_3),
3156273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT_),
3157273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT64_),
3158273d778eSKuninori Morimoto 	ENDIANNESS_MAP(IEC958_SUBFRAME_),
3159273d778eSKuninori Morimoto };
3160273d778eSKuninori Morimoto 
3161273d778eSKuninori Morimoto /*
3162273d778eSKuninori Morimoto  * Fix up the DAI formats for endianness: codecs don't actually see
3163273d778eSKuninori Morimoto  * the endianness of the data but we're using the CPU format
3164273d778eSKuninori Morimoto  * definitions which do need to include endianness so we ensure that
3165273d778eSKuninori Morimoto  * codec DAIs always have both big and little endian variants set.
3166273d778eSKuninori Morimoto  */
3167273d778eSKuninori Morimoto static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3168273d778eSKuninori Morimoto {
3169273d778eSKuninori Morimoto 	int i;
3170273d778eSKuninori Morimoto 
3171273d778eSKuninori Morimoto 	for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3172273d778eSKuninori Morimoto 		if (stream->formats & endianness_format_map[i])
3173273d778eSKuninori Morimoto 			stream->formats |= endianness_format_map[i];
3174273d778eSKuninori Morimoto }
3175273d778eSKuninori Morimoto 
3176e894efefSSrinivas Kandagatla static void snd_soc_try_rebind_card(void)
3177e894efefSSrinivas Kandagatla {
3178e894efefSSrinivas Kandagatla 	struct snd_soc_card *card, *c;
3179e894efefSSrinivas Kandagatla 
3180e894efefSSrinivas Kandagatla 	if (!list_empty(&unbind_card_list)) {
3181e894efefSSrinivas Kandagatla 		list_for_each_entry_safe(card, c, &unbind_card_list, list) {
3182e894efefSSrinivas Kandagatla 			if (!snd_soc_bind_card(card))
3183e894efefSSrinivas Kandagatla 				list_del(&card->list);
3184e894efefSSrinivas Kandagatla 		}
3185e894efefSSrinivas Kandagatla 	}
3186e894efefSSrinivas Kandagatla }
3187e894efefSSrinivas Kandagatla 
3188e0dac41bSKuninori Morimoto int snd_soc_add_component(struct device *dev,
3189e0dac41bSKuninori Morimoto 			struct snd_soc_component *component,
3190cf9e829eSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
3191d191bd8dSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
3192d191bd8dSKuninori Morimoto 			int num_dai)
3193d191bd8dSKuninori Morimoto {
3194bb13109dSLars-Peter Clausen 	int ret;
3195273d778eSKuninori Morimoto 	int i;
3196d191bd8dSKuninori Morimoto 
3197cf9e829eSKuninori Morimoto 	ret = snd_soc_component_initialize(component, component_driver, dev);
3198bb13109dSLars-Peter Clausen 	if (ret)
3199bb13109dSLars-Peter Clausen 		goto err_free;
3200bb13109dSLars-Peter Clausen 
3201273d778eSKuninori Morimoto 	if (component_driver->endianness) {
3202273d778eSKuninori Morimoto 		for (i = 0; i < num_dai; i++) {
3203273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].playback);
3204273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].capture);
3205273d778eSKuninori Morimoto 		}
3206273d778eSKuninori Morimoto 	}
3207273d778eSKuninori Morimoto 
32080e7b25c6SKuninori Morimoto 	ret = snd_soc_register_dais(component, dai_drv, num_dai);
3209bb13109dSLars-Peter Clausen 	if (ret < 0) {
3210f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3211bb13109dSLars-Peter Clausen 		goto err_cleanup;
3212bb13109dSLars-Peter Clausen 	}
3213bb13109dSLars-Peter Clausen 
3214cf9e829eSKuninori Morimoto 	snd_soc_component_add(component);
3215e894efefSSrinivas Kandagatla 	snd_soc_try_rebind_card();
3216bb13109dSLars-Peter Clausen 
3217bb13109dSLars-Peter Clausen 	return 0;
3218bb13109dSLars-Peter Clausen 
3219bb13109dSLars-Peter Clausen err_cleanup:
3220cf9e829eSKuninori Morimoto 	snd_soc_component_cleanup(component);
3221bb13109dSLars-Peter Clausen err_free:
3222bb13109dSLars-Peter Clausen 	return ret;
3223d191bd8dSKuninori Morimoto }
3224e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component);
3225e0dac41bSKuninori Morimoto 
3226e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev,
3227e0dac41bSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
3228e0dac41bSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
3229e0dac41bSKuninori Morimoto 			int num_dai)
3230e0dac41bSKuninori Morimoto {
3231e0dac41bSKuninori Morimoto 	struct snd_soc_component *component;
3232e0dac41bSKuninori Morimoto 
32337ecbd6a9SKuninori Morimoto 	component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
323408e61d03SKuninori Morimoto 	if (!component)
3235e0dac41bSKuninori Morimoto 		return -ENOMEM;
3236e0dac41bSKuninori Morimoto 
3237e0dac41bSKuninori Morimoto 	return snd_soc_add_component(dev, component, component_driver,
3238e0dac41bSKuninori Morimoto 				     dai_drv, num_dai);
3239e0dac41bSKuninori Morimoto }
3240d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
3241d191bd8dSKuninori Morimoto 
3242d191bd8dSKuninori Morimoto /**
32432eccea8cSKuninori Morimoto  * snd_soc_unregister_component - Unregister all related component
32442eccea8cSKuninori Morimoto  * from the ASoC core
3245d191bd8dSKuninori Morimoto  *
3246628536eaSJonathan Corbet  * @dev: The device to unregister
3247d191bd8dSKuninori Morimoto  */
32482eccea8cSKuninori Morimoto static int __snd_soc_unregister_component(struct device *dev)
3249d191bd8dSKuninori Morimoto {
3250cf9e829eSKuninori Morimoto 	struct snd_soc_component *component;
325121a03528SKuninori Morimoto 	int found = 0;
3252d191bd8dSKuninori Morimoto 
325334e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
3254368dee94SKuninori Morimoto 	for_each_component(component) {
3255999f7f5aSKuninori Morimoto 		if (dev != component->dev)
325621a03528SKuninori Morimoto 			continue;
3257d191bd8dSKuninori Morimoto 
32582c7b696aSMarcel Ziswiler 		snd_soc_tplg_component_remove(component,
32592c7b696aSMarcel Ziswiler 					      SND_SOC_TPLG_INDEX_ALL);
3260cf9e829eSKuninori Morimoto 		snd_soc_component_del_unlocked(component);
326121a03528SKuninori Morimoto 		found = 1;
326221a03528SKuninori Morimoto 		break;
3263d191bd8dSKuninori Morimoto 	}
3264d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
3265d191bd8dSKuninori Morimoto 
32662c7b696aSMarcel Ziswiler 	if (found)
3267cf9e829eSKuninori Morimoto 		snd_soc_component_cleanup(component);
32682eccea8cSKuninori Morimoto 
32692eccea8cSKuninori Morimoto 	return found;
32702eccea8cSKuninori Morimoto }
32712eccea8cSKuninori Morimoto 
32722eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
32732eccea8cSKuninori Morimoto {
32742c7b696aSMarcel Ziswiler 	while (__snd_soc_unregister_component(dev))
32752c7b696aSMarcel Ziswiler 		;
3276d191bd8dSKuninori Morimoto }
3277d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3278d191bd8dSKuninori Morimoto 
32797dd5d0d9SKuninori Morimoto struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
32807dd5d0d9SKuninori Morimoto 						   const char *driver_name)
32817dd5d0d9SKuninori Morimoto {
32827dd5d0d9SKuninori Morimoto 	struct snd_soc_component *component;
32837dd5d0d9SKuninori Morimoto 	struct snd_soc_component *ret;
32847dd5d0d9SKuninori Morimoto 
32857dd5d0d9SKuninori Morimoto 	ret = NULL;
32867dd5d0d9SKuninori Morimoto 	mutex_lock(&client_mutex);
3287368dee94SKuninori Morimoto 	for_each_component(component) {
32887dd5d0d9SKuninori Morimoto 		if (dev != component->dev)
32897dd5d0d9SKuninori Morimoto 			continue;
32907dd5d0d9SKuninori Morimoto 
32917dd5d0d9SKuninori Morimoto 		if (driver_name &&
32927dd5d0d9SKuninori Morimoto 		    (driver_name != component->driver->name) &&
32937dd5d0d9SKuninori Morimoto 		    (strcmp(component->driver->name, driver_name) != 0))
32947dd5d0d9SKuninori Morimoto 			continue;
32957dd5d0d9SKuninori Morimoto 
32967dd5d0d9SKuninori Morimoto 		ret = component;
32977dd5d0d9SKuninori Morimoto 		break;
32987dd5d0d9SKuninori Morimoto 	}
32997dd5d0d9SKuninori Morimoto 	mutex_unlock(&client_mutex);
33007dd5d0d9SKuninori Morimoto 
33017dd5d0d9SKuninori Morimoto 	return ret;
33027dd5d0d9SKuninori Morimoto }
33037dd5d0d9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
33047dd5d0d9SKuninori Morimoto 
3305bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
3306b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3307bec4fa05SStephen Warren 			       const char *propname)
3308bec4fa05SStephen Warren {
3309b07609ceSKuninori Morimoto 	struct device_node *np;
3310bec4fa05SStephen Warren 	int ret;
3311bec4fa05SStephen Warren 
33127e07e7c0STushar Behera 	if (!card->dev) {
33137e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
33147e07e7c0STushar Behera 		return -EINVAL;
33157e07e7c0STushar Behera 	}
33167e07e7c0STushar Behera 
33177e07e7c0STushar Behera 	np = card->dev->of_node;
33187e07e7c0STushar Behera 
3319bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
3320bec4fa05SStephen Warren 	/*
3321bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
3322bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
3323bec4fa05SStephen Warren 	 * snd_soc_register_card.
3324bec4fa05SStephen Warren 	 */
3325bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
3326bec4fa05SStephen Warren 		dev_err(card->dev,
3327f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
3328bec4fa05SStephen Warren 			propname, ret);
3329bec4fa05SStephen Warren 		return ret;
3330bec4fa05SStephen Warren 	}
3331bec4fa05SStephen Warren 
3332bec4fa05SStephen Warren 	return 0;
3333bec4fa05SStephen Warren }
3334b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3335bec4fa05SStephen Warren 
33369a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
33379a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
33389a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
33399a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
33409a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
33419a6d4860SXiubo Li };
33429a6d4860SXiubo Li 
334321efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
33449a6d4860SXiubo Li 					  const char *propname)
33459a6d4860SXiubo Li {
334621efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
33479a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
33489a6d4860SXiubo Li 	const char *template, *wname;
33499a6d4860SXiubo Li 	int i, j, num_widgets, ret;
33509a6d4860SXiubo Li 
33519a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
33529a6d4860SXiubo Li 	if (num_widgets < 0) {
33539a6d4860SXiubo Li 		dev_err(card->dev,
33549a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
33559a6d4860SXiubo Li 		return -EINVAL;
33569a6d4860SXiubo Li 	}
33579a6d4860SXiubo Li 	if (num_widgets & 1) {
33589a6d4860SXiubo Li 		dev_err(card->dev,
33599a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
33609a6d4860SXiubo Li 		return -EINVAL;
33619a6d4860SXiubo Li 	}
33629a6d4860SXiubo Li 
33639a6d4860SXiubo Li 	num_widgets /= 2;
33649a6d4860SXiubo Li 	if (!num_widgets) {
33659a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
33669a6d4860SXiubo Li 			propname);
33679a6d4860SXiubo Li 		return -EINVAL;
33689a6d4860SXiubo Li 	}
33699a6d4860SXiubo Li 
33709a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
33719a6d4860SXiubo Li 			       GFP_KERNEL);
33729a6d4860SXiubo Li 	if (!widgets) {
33739a6d4860SXiubo Li 		dev_err(card->dev,
33749a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
33759a6d4860SXiubo Li 		return -ENOMEM;
33769a6d4860SXiubo Li 	}
33779a6d4860SXiubo Li 
33789a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
33799a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
33809a6d4860SXiubo Li 			2 * i, &template);
33819a6d4860SXiubo Li 		if (ret) {
33829a6d4860SXiubo Li 			dev_err(card->dev,
33839a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
33849a6d4860SXiubo Li 				propname, 2 * i, ret);
33859a6d4860SXiubo Li 			return -EINVAL;
33869a6d4860SXiubo Li 		}
33879a6d4860SXiubo Li 
33889a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
33899a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
33909a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
33919a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
33929a6d4860SXiubo Li 				break;
33939a6d4860SXiubo Li 			}
33949a6d4860SXiubo Li 		}
33959a6d4860SXiubo Li 
33969a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
33979a6d4860SXiubo Li 			dev_err(card->dev,
33989a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
33999a6d4860SXiubo Li 				template);
34009a6d4860SXiubo Li 			return -EINVAL;
34019a6d4860SXiubo Li 		}
34029a6d4860SXiubo Li 
34039a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
34049a6d4860SXiubo Li 						    (2 * i) + 1,
34059a6d4860SXiubo Li 						    &wname);
34069a6d4860SXiubo Li 		if (ret) {
34079a6d4860SXiubo Li 			dev_err(card->dev,
34089a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
34099a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
34109a6d4860SXiubo Li 			return -EINVAL;
34119a6d4860SXiubo Li 		}
34129a6d4860SXiubo Li 
34139a6d4860SXiubo Li 		widgets[i].name = wname;
34149a6d4860SXiubo Li 	}
34159a6d4860SXiubo Li 
3416f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
3417f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
34189a6d4860SXiubo Li 
34199a6d4860SXiubo Li 	return 0;
34209a6d4860SXiubo Li }
342121efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
34229a6d4860SXiubo Li 
3423cbdfab3bSJerome Brunet int snd_soc_of_get_slot_mask(struct device_node *np,
34246131084aSJyri Sarha 			     const char *prop_name,
34256131084aSJyri Sarha 			     unsigned int *mask)
34266131084aSJyri Sarha {
34276131084aSJyri Sarha 	u32 val;
34286c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
34296131084aSJyri Sarha 	int i;
34306131084aSJyri Sarha 
34316131084aSJyri Sarha 	if (!of_slot_mask)
34326131084aSJyri Sarha 		return 0;
34336131084aSJyri Sarha 	val /= sizeof(u32);
34346131084aSJyri Sarha 	for (i = 0; i < val; i++)
34356131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
34366131084aSJyri Sarha 			*mask |= (1 << i);
34376131084aSJyri Sarha 
34386131084aSJyri Sarha 	return val;
34396131084aSJyri Sarha }
3440cbdfab3bSJerome Brunet EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
34416131084aSJyri Sarha 
344289c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
34436131084aSJyri Sarha 			      unsigned int *tx_mask,
34446131084aSJyri Sarha 			      unsigned int *rx_mask,
344589c67857SXiubo Li 			      unsigned int *slots,
344689c67857SXiubo Li 			      unsigned int *slot_width)
344789c67857SXiubo Li {
344889c67857SXiubo Li 	u32 val;
344989c67857SXiubo Li 	int ret;
345089c67857SXiubo Li 
34516131084aSJyri Sarha 	if (tx_mask)
34526131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
34536131084aSJyri Sarha 	if (rx_mask)
34546131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
34556131084aSJyri Sarha 
345689c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
345789c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
345889c67857SXiubo Li 		if (ret)
345989c67857SXiubo Li 			return ret;
346089c67857SXiubo Li 
346189c67857SXiubo Li 		if (slots)
346289c67857SXiubo Li 			*slots = val;
346389c67857SXiubo Li 	}
346489c67857SXiubo Li 
346589c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
346689c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
346789c67857SXiubo Li 		if (ret)
346889c67857SXiubo Li 			return ret;
346989c67857SXiubo Li 
347089c67857SXiubo Li 		if (slot_width)
347189c67857SXiubo Li 			*slot_width = val;
347289c67857SXiubo Li 	}
347389c67857SXiubo Li 
347489c67857SXiubo Li 	return 0;
347589c67857SXiubo Li }
347689c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
347789c67857SXiubo Li 
34783b710356SKuninori Morimoto void snd_soc_of_parse_node_prefix(struct device_node *np,
34795e3cdaa2SKuninori Morimoto 				  struct snd_soc_codec_conf *codec_conf,
34805e3cdaa2SKuninori Morimoto 				  struct device_node *of_node,
34815e3cdaa2SKuninori Morimoto 				  const char *propname)
34825e3cdaa2SKuninori Morimoto {
34835e3cdaa2SKuninori Morimoto 	const char *str;
34845e3cdaa2SKuninori Morimoto 	int ret;
34855e3cdaa2SKuninori Morimoto 
34865e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
34875e3cdaa2SKuninori Morimoto 	if (ret < 0) {
34885e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
34895e3cdaa2SKuninori Morimoto 		return;
34905e3cdaa2SKuninori Morimoto 	}
34915e3cdaa2SKuninori Morimoto 
34925e3cdaa2SKuninori Morimoto 	codec_conf->of_node	= of_node;
34935e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
34945e3cdaa2SKuninori Morimoto }
34953b710356SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
34965e3cdaa2SKuninori Morimoto 
34972bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3498a4a54dd5SStephen Warren 				   const char *propname)
3499a4a54dd5SStephen Warren {
35002bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
3501e3b1e6a1SMark Brown 	int num_routes;
3502a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3503a4a54dd5SStephen Warren 	int i, ret;
3504a4a54dd5SStephen Warren 
3505a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3506c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
350710e8aa9aSMichał Mirosław 		dev_err(card->dev,
350810e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
350910e8aa9aSMichał Mirosław 			propname);
3510a4a54dd5SStephen Warren 		return -EINVAL;
3511a4a54dd5SStephen Warren 	}
3512a4a54dd5SStephen Warren 	num_routes /= 2;
3513a4a54dd5SStephen Warren 	if (!num_routes) {
3514f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3515a4a54dd5SStephen Warren 			propname);
3516a4a54dd5SStephen Warren 		return -EINVAL;
3517a4a54dd5SStephen Warren 	}
3518a4a54dd5SStephen Warren 
3519a86854d0SKees Cook 	routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
3520a4a54dd5SStephen Warren 			      GFP_KERNEL);
3521a4a54dd5SStephen Warren 	if (!routes) {
3522a4a54dd5SStephen Warren 		dev_err(card->dev,
3523f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3524a4a54dd5SStephen Warren 		return -EINVAL;
3525a4a54dd5SStephen Warren 	}
3526a4a54dd5SStephen Warren 
3527a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3528a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3529e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
3530a4a54dd5SStephen Warren 		if (ret) {
3531c871bd0bSMark Brown 			dev_err(card->dev,
3532c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3533c871bd0bSMark Brown 				propname, 2 * i, ret);
3534a4a54dd5SStephen Warren 			return -EINVAL;
3535a4a54dd5SStephen Warren 		}
3536a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3537e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3538a4a54dd5SStephen Warren 		if (ret) {
3539a4a54dd5SStephen Warren 			dev_err(card->dev,
3540c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3541c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3542a4a54dd5SStephen Warren 			return -EINVAL;
3543a4a54dd5SStephen Warren 		}
3544a4a54dd5SStephen Warren 	}
3545a4a54dd5SStephen Warren 
3546f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
3547f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
3548a4a54dd5SStephen Warren 
3549a4a54dd5SStephen Warren 	return 0;
3550a4a54dd5SStephen Warren }
35512bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3552a4a54dd5SStephen Warren 
3553a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3554389cb834SJyri Sarha 				     const char *prefix,
3555389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3556389cb834SJyri Sarha 				     struct device_node **framemaster)
3557a7930ed4SKuninori Morimoto {
3558a7930ed4SKuninori Morimoto 	int ret, i;
3559a7930ed4SKuninori Morimoto 	char prop[128];
3560a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3561a7930ed4SKuninori Morimoto 	int bit, frame;
3562a7930ed4SKuninori Morimoto 	const char *str;
3563a7930ed4SKuninori Morimoto 	struct {
3564a7930ed4SKuninori Morimoto 		char *name;
3565a7930ed4SKuninori Morimoto 		unsigned int val;
3566a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3567a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3568a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3569a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3570a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3571a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3572a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3573a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3574a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3575a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3576a7930ed4SKuninori Morimoto 	};
3577a7930ed4SKuninori Morimoto 
3578a7930ed4SKuninori Morimoto 	if (!prefix)
3579a7930ed4SKuninori Morimoto 		prefix = "";
3580a7930ed4SKuninori Morimoto 
3581a7930ed4SKuninori Morimoto 	/*
35825711c979SKuninori Morimoto 	 * check "dai-format = xxx"
35835711c979SKuninori Morimoto 	 * or    "[prefix]format = xxx"
3584a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3585a7930ed4SKuninori Morimoto 	 */
35865711c979SKuninori Morimoto 	ret = of_property_read_string(np, "dai-format", &str);
35875711c979SKuninori Morimoto 	if (ret < 0) {
3588a7930ed4SKuninori Morimoto 		snprintf(prop, sizeof(prop), "%sformat", prefix);
3589a7930ed4SKuninori Morimoto 		ret = of_property_read_string(np, prop, &str);
35905711c979SKuninori Morimoto 	}
3591a7930ed4SKuninori Morimoto 	if (ret == 0) {
3592a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3593a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3594a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3595a7930ed4SKuninori Morimoto 				break;
3596a7930ed4SKuninori Morimoto 			}
3597a7930ed4SKuninori Morimoto 		}
3598a7930ed4SKuninori Morimoto 	}
3599a7930ed4SKuninori Morimoto 
3600a7930ed4SKuninori Morimoto 	/*
36018c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3602a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3603a7930ed4SKuninori Morimoto 	 */
36048c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
360551930295SJulia Lawall 	if (of_property_read_bool(np, prop))
36068c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
36078c2d6a9fSKuninori Morimoto 	else
36088c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3609a7930ed4SKuninori Morimoto 
3610a7930ed4SKuninori Morimoto 	/*
3611a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3612a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3613a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3614a7930ed4SKuninori Morimoto 	 */
3615a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3616a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3617a7930ed4SKuninori Morimoto 
3618a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3619a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3620a7930ed4SKuninori Morimoto 
3621a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3622a7930ed4SKuninori Morimoto 	case 0x11:
3623a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3624a7930ed4SKuninori Morimoto 		break;
3625a7930ed4SKuninori Morimoto 	case 0x10:
3626a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3627a7930ed4SKuninori Morimoto 		break;
3628a7930ed4SKuninori Morimoto 	case 0x01:
3629a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3630a7930ed4SKuninori Morimoto 		break;
3631a7930ed4SKuninori Morimoto 	default:
3632a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3633a7930ed4SKuninori Morimoto 		break;
3634a7930ed4SKuninori Morimoto 	}
3635a7930ed4SKuninori Morimoto 
3636a7930ed4SKuninori Morimoto 	/*
3637a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3638a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3639a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3640a7930ed4SKuninori Morimoto 	 */
3641a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3642a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3643389cb834SJyri Sarha 	if (bit && bitclkmaster)
3644389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3645a7930ed4SKuninori Morimoto 
3646a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3647a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3648389cb834SJyri Sarha 	if (frame && framemaster)
3649389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3650a7930ed4SKuninori Morimoto 
3651a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3652a7930ed4SKuninori Morimoto 	case 0x11:
3653a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3654a7930ed4SKuninori Morimoto 		break;
3655a7930ed4SKuninori Morimoto 	case 0x10:
3656a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3657a7930ed4SKuninori Morimoto 		break;
3658a7930ed4SKuninori Morimoto 	case 0x01:
3659a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3660a7930ed4SKuninori Morimoto 		break;
3661a7930ed4SKuninori Morimoto 	default:
3662a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3663a7930ed4SKuninori Morimoto 		break;
3664a7930ed4SKuninori Morimoto 	}
3665a7930ed4SKuninori Morimoto 
3666a7930ed4SKuninori Morimoto 	return format;
3667a7930ed4SKuninori Morimoto }
3668a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3669a7930ed4SKuninori Morimoto 
3670a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
3671a180e8b9SKuninori Morimoto {
367209d4cc03SKuninori Morimoto 	struct snd_soc_component *component;
3673a180e8b9SKuninori Morimoto 	struct device_node *node;
3674a180e8b9SKuninori Morimoto 	int ret;
3675a180e8b9SKuninori Morimoto 
3676a180e8b9SKuninori Morimoto 	node = of_graph_get_port_parent(ep);
3677a180e8b9SKuninori Morimoto 
3678a180e8b9SKuninori Morimoto 	/*
3679a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
3680a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
3681a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
3682a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
3683a180e8b9SKuninori Morimoto 	 */
3684a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
3685a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
368609d4cc03SKuninori Morimoto 	component = soc_find_component(node, NULL);
368709d4cc03SKuninori Morimoto 	if (component &&
368809d4cc03SKuninori Morimoto 	    component->driver->of_xlate_dai_id)
368909d4cc03SKuninori Morimoto 		ret = component->driver->of_xlate_dai_id(component, ep);
3690a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
3691a180e8b9SKuninori Morimoto 
3692c0a480d1STony Lindgren 	of_node_put(node);
3693c0a480d1STony Lindgren 
3694a180e8b9SKuninori Morimoto 	return ret;
3695a180e8b9SKuninori Morimoto }
3696a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3697a180e8b9SKuninori Morimoto 
36981ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args,
3699cb470087SKuninori Morimoto 				const char **dai_name)
3700cb470087SKuninori Morimoto {
3701cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
37023e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
370393b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3704cb470087SKuninori Morimoto 
3705cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3706368dee94SKuninori Morimoto 	for_each_component(pos) {
3707c0834440SKuninori Morimoto 		component_of_node = soc_component_to_node(pos);
37083e0aa8d8SJyri Sarha 
37093e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
3710cb470087SKuninori Morimoto 			continue;
3711cb470087SKuninori Morimoto 
37126833c452SKuninori Morimoto 		if (pos->driver->of_xlate_dai_name) {
371393b0f3eeSJean-Francois Moine 			ret = pos->driver->of_xlate_dai_name(pos,
371493b0f3eeSJean-Francois Moine 							     args,
371593b0f3eeSJean-Francois Moine 							     dai_name);
37166833c452SKuninori Morimoto 		} else {
371758bf4179SKuninori Morimoto 			struct snd_soc_dai *dai;
37186833c452SKuninori Morimoto 			int id = -1;
37196833c452SKuninori Morimoto 
372093b0f3eeSJean-Francois Moine 			switch (args->args_count) {
37216833c452SKuninori Morimoto 			case 0:
37226833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
37236833c452SKuninori Morimoto 				break;
37246833c452SKuninori Morimoto 			case 1:
372593b0f3eeSJean-Francois Moine 				id = args->args[0];
37266833c452SKuninori Morimoto 				break;
37276833c452SKuninori Morimoto 			default:
37286833c452SKuninori Morimoto 				/* not supported */
3729cb470087SKuninori Morimoto 				break;
3730cb470087SKuninori Morimoto 			}
3731cb470087SKuninori Morimoto 
37326833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
37336833c452SKuninori Morimoto 				ret = -EINVAL;
37343dcba280SNicolin Chen 				continue;
37356833c452SKuninori Morimoto 			}
3736e41975edSXiubo Li 
3737e41975edSXiubo Li 			ret = 0;
3738e41975edSXiubo Li 
373958bf4179SKuninori Morimoto 			/* find target DAI */
374015a0c645SKuninori Morimoto 			for_each_component_dais(pos, dai) {
374158bf4179SKuninori Morimoto 				if (id == 0)
374258bf4179SKuninori Morimoto 					break;
374358bf4179SKuninori Morimoto 				id--;
374458bf4179SKuninori Morimoto 			}
374558bf4179SKuninori Morimoto 
374658bf4179SKuninori Morimoto 			*dai_name = dai->driver->name;
3747e41975edSXiubo Li 			if (!*dai_name)
3748e41975edSXiubo Li 				*dai_name = pos->name;
37496833c452SKuninori Morimoto 		}
37506833c452SKuninori Morimoto 
3751cb470087SKuninori Morimoto 		break;
3752cb470087SKuninori Morimoto 	}
3753cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
375493b0f3eeSJean-Francois Moine 	return ret;
375593b0f3eeSJean-Francois Moine }
37561ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
375793b0f3eeSJean-Francois Moine 
375893b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
375993b0f3eeSJean-Francois Moine 			    const char **dai_name)
376093b0f3eeSJean-Francois Moine {
376193b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
376293b0f3eeSJean-Francois Moine 	int ret;
376393b0f3eeSJean-Francois Moine 
376493b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
376593b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
376693b0f3eeSJean-Francois Moine 	if (ret)
376793b0f3eeSJean-Francois Moine 		return ret;
376893b0f3eeSJean-Francois Moine 
376993b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3770cb470087SKuninori Morimoto 
3771cb470087SKuninori Morimoto 	of_node_put(args.np);
3772cb470087SKuninori Morimoto 
3773cb470087SKuninori Morimoto 	return ret;
3774cb470087SKuninori Morimoto }
3775cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3776cb470087SKuninori Morimoto 
377793b0f3eeSJean-Francois Moine /*
377894685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
377994685763SSylwester Nawrocki  * @dai_link: DAI link
378094685763SSylwester Nawrocki  *
378194685763SSylwester Nawrocki  * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
378294685763SSylwester Nawrocki  */
378394685763SSylwester Nawrocki void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
378494685763SSylwester Nawrocki {
37853db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *component;
378694685763SSylwester Nawrocki 	int index;
378794685763SSylwester Nawrocki 
37883db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
378994685763SSylwester Nawrocki 		if (!component->of_node)
379094685763SSylwester Nawrocki 			break;
379194685763SSylwester Nawrocki 		of_node_put(component->of_node);
379294685763SSylwester Nawrocki 		component->of_node = NULL;
379394685763SSylwester Nawrocki 	}
379494685763SSylwester Nawrocki }
379594685763SSylwester Nawrocki EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
379694685763SSylwester Nawrocki 
379794685763SSylwester Nawrocki /*
379893b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
379993b0f3eeSJean-Francois Moine  * @dev: Card device
380093b0f3eeSJean-Francois Moine  * @of_node: Device node
380193b0f3eeSJean-Francois Moine  * @dai_link: DAI link
380293b0f3eeSJean-Francois Moine  *
380393b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
380493b0f3eeSJean-Francois Moine  * 'sound-dai'.
380593b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
380694685763SSylwester Nawrocki  * The device nodes in the array (of_node) must be dereferenced by calling
380794685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs() on @dai_link.
380893b0f3eeSJean-Francois Moine  *
380993b0f3eeSJean-Francois Moine  * Returns 0 for success
381093b0f3eeSJean-Francois Moine  */
381193b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
381293b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
381393b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
381493b0f3eeSJean-Francois Moine {
381593b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
381693b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
381793b0f3eeSJean-Francois Moine 	char *name;
381893b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
381993b0f3eeSJean-Francois Moine 
382093b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
382193b0f3eeSJean-Francois Moine 	name = "sound-dai";
382293b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
382393b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
382493b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
382593b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
382693b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
382793b0f3eeSJean-Francois Moine 		else
382893b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
382993b0f3eeSJean-Francois Moine 		return num_codecs;
383093b0f3eeSJean-Francois Moine 	}
3831a86854d0SKees Cook 	component = devm_kcalloc(dev,
3832a86854d0SKees Cook 				 num_codecs, sizeof(*component),
383393b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
383493b0f3eeSJean-Francois Moine 	if (!component)
383593b0f3eeSJean-Francois Moine 		return -ENOMEM;
383693b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
383793b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
383893b0f3eeSJean-Francois Moine 
383993b0f3eeSJean-Francois Moine 	/* Parse the list */
38403db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
384193b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
384293b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
384393b0f3eeSJean-Francois Moine 						 index, &args);
384493b0f3eeSJean-Francois Moine 		if (ret)
384593b0f3eeSJean-Francois Moine 			goto err;
384693b0f3eeSJean-Francois Moine 		component->of_node = args.np;
384793b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
384893b0f3eeSJean-Francois Moine 		if (ret < 0)
384993b0f3eeSJean-Francois Moine 			goto err;
385093b0f3eeSJean-Francois Moine 	}
385193b0f3eeSJean-Francois Moine 	return 0;
385293b0f3eeSJean-Francois Moine err:
385394685763SSylwester Nawrocki 	snd_soc_of_put_dai_link_codecs(dai_link);
385493b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
385593b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
385693b0f3eeSJean-Francois Moine 	return ret;
385793b0f3eeSJean-Francois Moine }
385893b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
385993b0f3eeSJean-Francois Moine 
3860c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3861db2a4165SFrank Mandarino {
38626553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
3863fb257897SMark Brown 	snd_soc_util_init();
3864fb257897SMark Brown 
3865db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3866db2a4165SFrank Mandarino }
38674abe8e16SMark Brown module_init(snd_soc_init);
3868db2a4165SFrank Mandarino 
38697d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3870db2a4165SFrank Mandarino {
3871fb257897SMark Brown 	snd_soc_util_exit();
38726553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3873fb257897SMark Brown 
3874db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3875db2a4165SFrank Mandarino }
3876db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3877db2a4165SFrank Mandarino 
3878db2a4165SFrank Mandarino /* Module information */
3879d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3880db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3881db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
3882db2a4165SFrank Mandarino MODULE_ALIAS("platform:soc-audio");
3883