xref: /openbmc/linux/sound/soc/soc-core.c (revision 04eb1efcd614d6f067b76a355b3a3599667959dc)
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 /*
61db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
62db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
63db2a4165SFrank Mandarino  * between two audio tracks.
64db2a4165SFrank Mandarino  */
65db2a4165SFrank Mandarino static int pmdown_time = 5000;
66db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
67db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
68db2a4165SFrank Mandarino 
692c7b696aSMarcel Ziswiler /*
702c7b696aSMarcel Ziswiler  * If a DMI filed contain strings in this blacklist (e.g.
7198faf436SMengdong Lin  * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
7298faf436SMengdong Lin  * as invalid and dropped when setting the card long name from DMI info.
7398faf436SMengdong Lin  */
7498faf436SMengdong Lin static const char * const dmi_blacklist[] = {
7598faf436SMengdong Lin 	"To be filled by OEM",
7698faf436SMengdong Lin 	"TBD by OEM",
7798faf436SMengdong Lin 	"Default String",
7898faf436SMengdong Lin 	"Board Manufacturer",
7998faf436SMengdong Lin 	"Board Vendor Name",
8098faf436SMengdong Lin 	"Board Product Name",
8198faf436SMengdong Lin 	NULL,	/* terminator */
8298faf436SMengdong Lin };
8398faf436SMengdong Lin 
84dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
85dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
86dbe21408SMark Brown {
8736ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
88dbe21408SMark Brown 
89f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
90dbe21408SMark Brown }
91dbe21408SMark Brown 
92dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
93dbe21408SMark Brown 			       struct device_attribute *attr,
94dbe21408SMark Brown 			       const char *buf, size_t count)
95dbe21408SMark Brown {
9636ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
97c593b520SMark Brown 	int ret;
98dbe21408SMark Brown 
99b785a492SJingoo Han 	ret = kstrtol(buf, 10, &rtd->pmdown_time);
100c593b520SMark Brown 	if (ret)
101c593b520SMark Brown 		return ret;
102dbe21408SMark Brown 
103dbe21408SMark Brown 	return count;
104dbe21408SMark Brown }
105dbe21408SMark Brown 
106dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
107dbe21408SMark Brown 
108d29697dcSTakashi Iwai static struct attribute *soc_dev_attrs[] = {
109d29697dcSTakashi Iwai 	&dev_attr_pmdown_time.attr,
110d29697dcSTakashi Iwai 	NULL
111d29697dcSTakashi Iwai };
112d29697dcSTakashi Iwai 
113d29697dcSTakashi Iwai static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
114d29697dcSTakashi Iwai 				       struct attribute *attr, int idx)
115d29697dcSTakashi Iwai {
116d29697dcSTakashi Iwai 	struct device *dev = kobj_to_dev(kobj);
117d29697dcSTakashi Iwai 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
118d29697dcSTakashi Iwai 
119d29697dcSTakashi Iwai 	if (attr == &dev_attr_pmdown_time.attr)
120d29697dcSTakashi Iwai 		return attr->mode; /* always visible */
1213b6eed8dSKuninori Morimoto 	return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
122d29697dcSTakashi Iwai }
123d29697dcSTakashi Iwai 
124d29697dcSTakashi Iwai static const struct attribute_group soc_dapm_dev_group = {
125d29697dcSTakashi Iwai 	.attrs = soc_dapm_dev_attrs,
126d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
127d29697dcSTakashi Iwai };
128d29697dcSTakashi Iwai 
129f7e73b26SMark Brown static const struct attribute_group soc_dev_group = {
130d29697dcSTakashi Iwai 	.attrs = soc_dev_attrs,
131d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
132d29697dcSTakashi Iwai };
133d29697dcSTakashi Iwai 
134d29697dcSTakashi Iwai static const struct attribute_group *soc_dev_attr_groups[] = {
135d29697dcSTakashi Iwai 	&soc_dapm_dev_group,
136f7e73b26SMark Brown 	&soc_dev_group,
137d29697dcSTakashi Iwai 	NULL
138d29697dcSTakashi Iwai };
139d29697dcSTakashi Iwai 
1402624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
14181c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component)
142e73f3de5SRussell King {
1436553bf06SLars-Peter Clausen 	if (!component->card->debugfs_card_root)
1446553bf06SLars-Peter Clausen 		return;
1456553bf06SLars-Peter Clausen 
14681c7cfd1SLars-Peter Clausen 	if (component->debugfs_prefix) {
14781c7cfd1SLars-Peter Clausen 		char *name;
148e73f3de5SRussell King 
14981c7cfd1SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s:%s",
15081c7cfd1SLars-Peter Clausen 			component->debugfs_prefix, component->name);
15181c7cfd1SLars-Peter Clausen 		if (name) {
15281c7cfd1SLars-Peter Clausen 			component->debugfs_root = debugfs_create_dir(name,
15381c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
15481c7cfd1SLars-Peter Clausen 			kfree(name);
15581c7cfd1SLars-Peter Clausen 		}
15681c7cfd1SLars-Peter Clausen 	} else {
15781c7cfd1SLars-Peter Clausen 		component->debugfs_root = debugfs_create_dir(component->name,
15881c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
159e73f3de5SRussell King 	}
160e73f3de5SRussell King 
16181c7cfd1SLars-Peter Clausen 	if (!component->debugfs_root) {
16281c7cfd1SLars-Peter Clausen 		dev_warn(component->dev,
16381c7cfd1SLars-Peter Clausen 			"ASoC: Failed to create component debugfs directory\n");
1642624d5faSMark Brown 		return;
1652624d5faSMark Brown 	}
1662624d5faSMark Brown 
16781c7cfd1SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
16881c7cfd1SLars-Peter Clausen 		component->debugfs_root);
16981c7cfd1SLars-Peter Clausen }
17081c7cfd1SLars-Peter Clausen 
17181c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
17281c7cfd1SLars-Peter Clausen {
17381c7cfd1SLars-Peter Clausen 	debugfs_remove_recursive(component->debugfs_root);
17481c7cfd1SLars-Peter Clausen }
17581c7cfd1SLars-Peter Clausen 
176c15b2a1dSPeng Donglin static int dai_list_show(struct seq_file *m, void *v)
177f3208780SMark Brown {
1781438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
179f3208780SMark Brown 	struct snd_soc_dai *dai;
180f3208780SMark Brown 
18134e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
18234e81ab4SLars-Peter Clausen 
183368dee94SKuninori Morimoto 	for_each_component(component)
18415a0c645SKuninori Morimoto 		for_each_component_dais(component, dai)
185700c17caSDonglin Peng 			seq_printf(m, "%s\n", dai->name);
186f3208780SMark Brown 
18734e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
18834e81ab4SLars-Peter Clausen 
189700c17caSDonglin Peng 	return 0;
190700c17caSDonglin Peng }
191c15b2a1dSPeng Donglin DEFINE_SHOW_ATTRIBUTE(dai_list);
192f3208780SMark Brown 
193db795f9bSKuninori Morimoto static int component_list_show(struct seq_file *m, void *v)
194db795f9bSKuninori Morimoto {
195db795f9bSKuninori Morimoto 	struct snd_soc_component *component;
196db795f9bSKuninori Morimoto 
197db795f9bSKuninori Morimoto 	mutex_lock(&client_mutex);
198db795f9bSKuninori Morimoto 
199368dee94SKuninori Morimoto 	for_each_component(component)
200db795f9bSKuninori Morimoto 		seq_printf(m, "%s\n", component->name);
201db795f9bSKuninori Morimoto 
202db795f9bSKuninori Morimoto 	mutex_unlock(&client_mutex);
203db795f9bSKuninori Morimoto 
204db795f9bSKuninori Morimoto 	return 0;
205db795f9bSKuninori Morimoto }
206db795f9bSKuninori Morimoto DEFINE_SHOW_ATTRIBUTE(component_list);
207db795f9bSKuninori Morimoto 
208a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
209a6052154SJarkko Nikula {
2106553bf06SLars-Peter Clausen 	if (!snd_soc_debugfs_root)
2116553bf06SLars-Peter Clausen 		return;
2126553bf06SLars-Peter Clausen 
213a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
2148a9dab1aSMark Brown 						     snd_soc_debugfs_root);
2153a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
216a6052154SJarkko Nikula 		dev_warn(card->dev,
2177c08be84SLothar Waßmann 			 "ASoC: Failed to create card debugfs directory\n");
2183a45b867SJarkko Nikula 		return;
2193a45b867SJarkko Nikula 	}
2203a45b867SJarkko Nikula 
2213a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
2223a45b867SJarkko Nikula 						    card->debugfs_card_root,
2233a45b867SJarkko Nikula 						    &card->pop_time);
2243a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
2253a45b867SJarkko Nikula 		dev_warn(card->dev,
226f110bfc7SLiam Girdwood 			 "ASoC: Failed to create pop time debugfs file\n");
227a6052154SJarkko Nikula }
228a6052154SJarkko Nikula 
229a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
230a6052154SJarkko Nikula {
231a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
232a6052154SJarkko Nikula }
233a6052154SJarkko Nikula 
2346553bf06SLars-Peter Clausen static void snd_soc_debugfs_init(void)
2356553bf06SLars-Peter Clausen {
2366553bf06SLars-Peter Clausen 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
237d9a02c55SFabio Estevam 	if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) {
2386553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create debugfs directory\n");
2396553bf06SLars-Peter Clausen 		snd_soc_debugfs_root = NULL;
2406553bf06SLars-Peter Clausen 		return;
2416553bf06SLars-Peter Clausen 	}
2426553bf06SLars-Peter Clausen 
2436553bf06SLars-Peter Clausen 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
2446553bf06SLars-Peter Clausen 				 &dai_list_fops))
2456553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
246db795f9bSKuninori Morimoto 
247db795f9bSKuninori Morimoto 	if (!debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
248db795f9bSKuninori Morimoto 				 &component_list_fops))
249db795f9bSKuninori Morimoto 		pr_warn("ASoC: Failed to create component list debugfs file\n");
2506553bf06SLars-Peter Clausen }
2516553bf06SLars-Peter Clausen 
2526553bf06SLars-Peter Clausen static void snd_soc_debugfs_exit(void)
2536553bf06SLars-Peter Clausen {
2546553bf06SLars-Peter Clausen 	debugfs_remove_recursive(snd_soc_debugfs_root);
2556553bf06SLars-Peter Clausen }
2566553bf06SLars-Peter Clausen 
2572624d5faSMark Brown #else
2582624d5faSMark Brown 
25981c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs(
26081c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
2612624d5faSMark Brown {
2622624d5faSMark Brown }
2632624d5faSMark Brown 
26481c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs(
26581c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
266731f1ab2SSebastien Guiriec {
267731f1ab2SSebastien Guiriec }
268731f1ab2SSebastien Guiriec 
269b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
270b95fccbcSAxel Lin {
271b95fccbcSAxel Lin }
272b95fccbcSAxel Lin 
273b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
274b95fccbcSAxel Lin {
275b95fccbcSAxel Lin }
2766553bf06SLars-Peter Clausen 
2776553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_init(void)
2786553bf06SLars-Peter Clausen {
2796553bf06SLars-Peter Clausen }
2806553bf06SLars-Peter Clausen 
2816553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_exit(void)
2826553bf06SLars-Peter Clausen {
2836553bf06SLars-Peter Clausen }
2846553bf06SLars-Peter Clausen 
2852624d5faSMark Brown #endif
2862624d5faSMark Brown 
287a0ac4411SKuninori Morimoto static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
288a0ac4411SKuninori Morimoto 			      struct snd_soc_component *component)
289a0ac4411SKuninori Morimoto {
290a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
291a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *new_rtdcom;
292a0ac4411SKuninori Morimoto 
293a0ac4411SKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
294a0ac4411SKuninori Morimoto 		/* already connected */
295a0ac4411SKuninori Morimoto 		if (rtdcom->component == component)
296a0ac4411SKuninori Morimoto 			return 0;
297a0ac4411SKuninori Morimoto 	}
298a0ac4411SKuninori Morimoto 
299a0ac4411SKuninori Morimoto 	new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
300a0ac4411SKuninori Morimoto 	if (!new_rtdcom)
301a0ac4411SKuninori Morimoto 		return -ENOMEM;
302a0ac4411SKuninori Morimoto 
303a0ac4411SKuninori Morimoto 	new_rtdcom->component = component;
304a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&new_rtdcom->list);
305a0ac4411SKuninori Morimoto 
306a0ac4411SKuninori Morimoto 	list_add_tail(&new_rtdcom->list, &rtd->component_list);
307a0ac4411SKuninori Morimoto 
308a0ac4411SKuninori Morimoto 	return 0;
309a0ac4411SKuninori Morimoto }
310a0ac4411SKuninori Morimoto 
311a0ac4411SKuninori Morimoto static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
312a0ac4411SKuninori Morimoto {
313a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
314a0ac4411SKuninori Morimoto 
315a0ac4411SKuninori Morimoto 	for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
316a0ac4411SKuninori Morimoto 		kfree(rtdcom1);
317a0ac4411SKuninori Morimoto 
318a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
319a0ac4411SKuninori Morimoto }
320a0ac4411SKuninori Morimoto 
321a0ac4411SKuninori Morimoto struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
322a0ac4411SKuninori Morimoto 						const char *driver_name)
323a0ac4411SKuninori Morimoto {
324a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
325a0ac4411SKuninori Morimoto 
326971da24cSKuninori Morimoto 	if (!driver_name)
327971da24cSKuninori Morimoto 		return NULL;
328971da24cSKuninori Morimoto 
329a0ac4411SKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
330971da24cSKuninori Morimoto 		const char *component_name = rtdcom->component->driver->name;
331971da24cSKuninori Morimoto 
332971da24cSKuninori Morimoto 		if (!component_name)
333971da24cSKuninori Morimoto 			continue;
334971da24cSKuninori Morimoto 
335971da24cSKuninori Morimoto 		if ((component_name == driver_name) ||
336971da24cSKuninori Morimoto 		    strcmp(component_name, driver_name) == 0)
337a0ac4411SKuninori Morimoto 			return rtdcom->component;
338a0ac4411SKuninori Morimoto 	}
339a0ac4411SKuninori Morimoto 
340a0ac4411SKuninori Morimoto 	return NULL;
341a0ac4411SKuninori Morimoto }
342031734b7SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
343a0ac4411SKuninori Morimoto 
34447c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
34547c88fffSLiam Girdwood 		const char *dai_link, int stream)
34647c88fffSLiam Girdwood {
3471a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
34847c88fffSLiam Girdwood 
349bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
3501a497983SMengdong Lin 		if (rtd->dai_link->no_pcm &&
3511a497983SMengdong Lin 			!strcmp(rtd->dai_link->name, dai_link))
3521a497983SMengdong Lin 			return rtd->pcm->streams[stream].substream;
35347c88fffSLiam Girdwood 	}
354f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
35547c88fffSLiam Girdwood 	return NULL;
35647c88fffSLiam Girdwood }
35747c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
35847c88fffSLiam Girdwood 
35975ab9eb6SKuninori Morimoto static const struct snd_soc_ops null_snd_soc_ops;
36075ab9eb6SKuninori Morimoto 
3611a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
3621a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
3631a497983SMengdong Lin {
3641a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
3651a497983SMengdong Lin 
3661a497983SMengdong Lin 	rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
3671a497983SMengdong Lin 	if (!rtd)
3681a497983SMengdong Lin 		return NULL;
3691a497983SMengdong Lin 
370a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
3711a497983SMengdong Lin 	rtd->card = card;
3721a497983SMengdong Lin 	rtd->dai_link = dai_link;
37375ab9eb6SKuninori Morimoto 	if (!rtd->dai_link->ops)
37475ab9eb6SKuninori Morimoto 		rtd->dai_link->ops = &null_snd_soc_ops;
37575ab9eb6SKuninori Morimoto 
3766396bb22SKees Cook 	rtd->codec_dais = kcalloc(dai_link->num_codecs,
3776396bb22SKees Cook 					sizeof(struct snd_soc_dai *),
3781a497983SMengdong Lin 					GFP_KERNEL);
3791a497983SMengdong Lin 	if (!rtd->codec_dais) {
3801a497983SMengdong Lin 		kfree(rtd);
3811a497983SMengdong Lin 		return NULL;
3821a497983SMengdong Lin 	}
3831a497983SMengdong Lin 
3841a497983SMengdong Lin 	return rtd;
3851a497983SMengdong Lin }
3861a497983SMengdong Lin 
3871a497983SMengdong Lin static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
3881a497983SMengdong Lin {
3891a497983SMengdong Lin 	kfree(rtd->codec_dais);
390a0ac4411SKuninori Morimoto 	snd_soc_rtdcom_del_all(rtd);
3911a497983SMengdong Lin 	kfree(rtd);
3921a497983SMengdong Lin }
3931a497983SMengdong Lin 
3941a497983SMengdong Lin static void soc_add_pcm_runtime(struct snd_soc_card *card,
3951a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd)
3961a497983SMengdong Lin {
3971a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
3981a497983SMengdong Lin 	rtd->num = card->num_rtd;
3991a497983SMengdong Lin 	card->num_rtd++;
4001a497983SMengdong Lin }
4011a497983SMengdong Lin 
4021a497983SMengdong Lin static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
4031a497983SMengdong Lin {
4041a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, *_rtd;
4051a497983SMengdong Lin 
406bcb1fd1fSKuninori Morimoto 	for_each_card_rtds_safe(card, rtd, _rtd) {
4071a497983SMengdong Lin 		list_del(&rtd->list);
4081a497983SMengdong Lin 		soc_free_pcm_runtime(rtd);
4091a497983SMengdong Lin 	}
4101a497983SMengdong Lin 
4111a497983SMengdong Lin 	card->num_rtd = 0;
4121a497983SMengdong Lin }
4131a497983SMengdong Lin 
41447c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
41547c88fffSLiam Girdwood 		const char *dai_link)
41647c88fffSLiam Girdwood {
4171a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
41847c88fffSLiam Girdwood 
419bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4201a497983SMengdong Lin 		if (!strcmp(rtd->dai_link->name, dai_link))
4211a497983SMengdong Lin 			return rtd;
42247c88fffSLiam Girdwood 	}
423f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
42447c88fffSLiam Girdwood 	return NULL;
42547c88fffSLiam Girdwood }
42647c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
42747c88fffSLiam Girdwood 
4289d58a077SRichard Fitzgerald static void codec2codec_close_delayed_work(struct work_struct *work)
4299d58a077SRichard Fitzgerald {
4302c7b696aSMarcel Ziswiler 	/*
4312c7b696aSMarcel Ziswiler 	 * Currently nothing to do for c2c links
4329d58a077SRichard Fitzgerald 	 * Since c2c links are internal nodes in the DAPM graph and
4339d58a077SRichard Fitzgerald 	 * don't interface with the outside world or application layer
4349d58a077SRichard Fitzgerald 	 * we don't have to do any special handling on close.
4359d58a077SRichard Fitzgerald 	 */
4369d58a077SRichard Fitzgerald }
4379d58a077SRichard Fitzgerald 
4386f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
439db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
4406f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
441db2a4165SFrank Mandarino {
4426f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
443d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
4441a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
4451a497983SMengdong Lin 	int i;
446db2a4165SFrank Mandarino 
447c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
448c5599b87SLars-Peter Clausen 	if (!card->instantiated)
449e3509ff0SDaniel Mack 		return 0;
450e3509ff0SDaniel Mack 
4512c7b696aSMarcel Ziswiler 	/*
4522c7b696aSMarcel Ziswiler 	 * Due to the resume being scheduled into a workqueue we could
4536ed25978SAndy Green 	 * suspend before that's finished - wait for it to complete.
4546ed25978SAndy Green 	 */
455f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
4566ed25978SAndy Green 
4576ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
458f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
4596ed25978SAndy Green 
460a00f90f9SMark Brown 	/* mute any active DACs */
461bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4620b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
4633efab7dcSMark Brown 
4641a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4653efab7dcSMark Brown 			continue;
4663efab7dcSMark Brown 
4670b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
46888bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
46988bd870fSBenoit Cousson 
470f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
471f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 1);
472db2a4165SFrank Mandarino 		}
47388bd870fSBenoit Cousson 	}
474db2a4165SFrank Mandarino 
4754ccab3e7SLiam Girdwood 	/* suspend all pcms */
476bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4771a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4783efab7dcSMark Brown 			continue;
4793efab7dcSMark Brown 
4801a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
4813efab7dcSMark Brown 	}
4824ccab3e7SLiam Girdwood 
48387506549SMark Brown 	if (card->suspend_pre)
48470b2ac12SMark Brown 		card->suspend_pre(card);
485db2a4165SFrank Mandarino 
486bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4871a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
4883efab7dcSMark Brown 
4891a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4903efab7dcSMark Brown 			continue;
4913efab7dcSMark Brown 
492bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
493f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
494db2a4165SFrank Mandarino 	}
495db2a4165SFrank Mandarino 
49637660b6dSLars-Peter Clausen 	/* close any waiting streams */
497bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd)
4981a497983SMengdong Lin 		flush_delayed_work(&rtd->delayed_work);
499db2a4165SFrank Mandarino 
500bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5013efab7dcSMark Brown 
5021a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5033efab7dcSMark Brown 			continue;
5043efab7dcSMark Brown 
5051a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
5067bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
507db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
508f0fba2adSLiam Girdwood 
5091a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
5107bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
511db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
512db2a4165SFrank Mandarino 	}
513db2a4165SFrank Mandarino 
5148be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
5158be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
516e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
517e2d32ff6SMark Brown 
5189178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
519f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
5202c7b696aSMarcel Ziswiler 		struct snd_soc_dapm_context *dapm =
5212c7b696aSMarcel Ziswiler 				snd_soc_component_get_dapm(component);
522d9fc4063SKuninori Morimoto 
5232c7b696aSMarcel Ziswiler 		/*
5242c7b696aSMarcel Ziswiler 		 * If there are paths active then the COMPONENT will be held
5252c7b696aSMarcel Ziswiler 		 * with bias _ON and should not be suspended.
5262c7b696aSMarcel Ziswiler 		 */
5279178feb4SKuninori Morimoto 		if (!component->suspended) {
5284890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
5291547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
530125a25daSMark Brown 				/*
5319178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
532125a25daSMark Brown 				 * bias off then being in STANDBY
533125a25daSMark Brown 				 * means it's doing something,
534125a25daSMark Brown 				 * otherwise fall through.
535125a25daSMark Brown 				 */
5364890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
5379178feb4SKuninori Morimoto 					dev_dbg(component->dev,
53810e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
539125a25daSMark Brown 					break;
540125a25daSMark Brown 				}
5411a12d5dcSGustavo A. R. Silva 				/* fall through */
542a8093297SLars-Peter Clausen 
5431547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
544999f7f5aSKuninori Morimoto 				if (component->driver->suspend)
545999f7f5aSKuninori Morimoto 					component->driver->suspend(component);
5469178feb4SKuninori Morimoto 				component->suspended = 1;
5479178feb4SKuninori Morimoto 				if (component->regmap)
5489178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
549988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
5509178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
5511547aba9SMark Brown 				break;
5521547aba9SMark Brown 			default:
5539178feb4SKuninori Morimoto 				dev_dbg(component->dev,
5549178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
5551547aba9SMark Brown 				break;
5561547aba9SMark Brown 			}
5571547aba9SMark Brown 		}
558f0fba2adSLiam Girdwood 	}
559db2a4165SFrank Mandarino 
560bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5611a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
5623efab7dcSMark Brown 
5631a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5643efab7dcSMark Brown 			continue;
5653efab7dcSMark Brown 
566bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
567f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
568988e8cc4SNicolin Chen 
569988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
570988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
571db2a4165SFrank Mandarino 	}
572db2a4165SFrank Mandarino 
57387506549SMark Brown 	if (card->suspend_post)
57470b2ac12SMark Brown 		card->suspend_post(card);
575db2a4165SFrank Mandarino 
576db2a4165SFrank Mandarino 	return 0;
577db2a4165SFrank Mandarino }
5786f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
579db2a4165SFrank Mandarino 
5802c7b696aSMarcel Ziswiler /*
5812c7b696aSMarcel Ziswiler  * deferred resume work, so resume can complete before we finished
5826ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
5836ed25978SAndy Green  */
5846ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
585db2a4165SFrank Mandarino {
586f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
5872c7b696aSMarcel Ziswiler 			container_of(work, struct snd_soc_card,
5882c7b696aSMarcel Ziswiler 				     deferred_resume_work);
5891a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
590d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
5911a497983SMengdong Lin 	int i;
592db2a4165SFrank Mandarino 
5932c7b696aSMarcel Ziswiler 	/*
5942c7b696aSMarcel Ziswiler 	 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
5956ed25978SAndy Green 	 * so userspace apps are blocked from touching us
5966ed25978SAndy Green 	 */
5976ed25978SAndy Green 
598f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
5996ed25978SAndy Green 
6009949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
601f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
6029949788bSMark Brown 
60387506549SMark Brown 	if (card->resume_pre)
60470b2ac12SMark Brown 		card->resume_pre(card);
605db2a4165SFrank Mandarino 
606bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
607bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6081a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6093efab7dcSMark Brown 
6101a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6113efab7dcSMark Brown 			continue;
6123efab7dcSMark Brown 
613bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
614f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
615db2a4165SFrank Mandarino 	}
616db2a4165SFrank Mandarino 
617f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
6189178feb4SKuninori Morimoto 		if (component->suspended) {
619999f7f5aSKuninori Morimoto 			if (component->driver->resume)
620999f7f5aSKuninori Morimoto 				component->driver->resume(component);
6219178feb4SKuninori Morimoto 			component->suspended = 0;
6221547aba9SMark Brown 		}
623f0fba2adSLiam Girdwood 	}
624db2a4165SFrank Mandarino 
625bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6263efab7dcSMark Brown 
6271a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6283efab7dcSMark Brown 			continue;
6293efab7dcSMark Brown 
6301a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
631d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
632db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
633f0fba2adSLiam Girdwood 
6341a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
635d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
636db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
637db2a4165SFrank Mandarino 	}
638db2a4165SFrank Mandarino 
6393ff3f64bSMark Brown 	/* unmute any active DACs */
640bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6410b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
6423efab7dcSMark Brown 
6431a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6443efab7dcSMark Brown 			continue;
6453efab7dcSMark Brown 
6460b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
64788bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
64888bd870fSBenoit Cousson 
649f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
650f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 0);
651db2a4165SFrank Mandarino 		}
65288bd870fSBenoit Cousson 	}
653db2a4165SFrank Mandarino 
654bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6551a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6563efab7dcSMark Brown 
6571a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6583efab7dcSMark Brown 			continue;
6593efab7dcSMark Brown 
660bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
661f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
662db2a4165SFrank Mandarino 	}
663db2a4165SFrank Mandarino 
66487506549SMark Brown 	if (card->resume_post)
66570b2ac12SMark Brown 		card->resume_post(card);
666db2a4165SFrank Mandarino 
667f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
6686ed25978SAndy Green 
6698be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
6708be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
671e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
6721a7aaa58SJeeja KP 
6731a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
6741a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
6756ed25978SAndy Green }
6766ed25978SAndy Green 
6776ed25978SAndy Green /* powers up audio subsystem after a suspend */
6786f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
6796ed25978SAndy Green {
6806f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
681bc263214SLars-Peter Clausen 	bool bus_control = false;
6821a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
683b9dd94a8SPeter Ujfalusi 
684c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
685c5599b87SLars-Peter Clausen 	if (!card->instantiated)
6865ff1ddf2SEric Miao 		return 0;
6875ff1ddf2SEric Miao 
688988e8cc4SNicolin Chen 	/* activate pins from sleep state */
689bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6900b7990e3SKuninori Morimoto 		struct snd_soc_dai *codec_dai;
69188bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
69288bd870fSBenoit Cousson 		int j;
69388bd870fSBenoit Cousson 
694988e8cc4SNicolin Chen 		if (cpu_dai->active)
695988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
69688bd870fSBenoit Cousson 
6970b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, j, codec_dai) {
698988e8cc4SNicolin Chen 			if (codec_dai->active)
699988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
700988e8cc4SNicolin Chen 		}
70188bd870fSBenoit Cousson 	}
702988e8cc4SNicolin Chen 
703bc263214SLars-Peter Clausen 	/*
704bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
705bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
706bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
70764ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
70864ab9baaSMark Brown 	 */
709bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7101a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7112c7b696aSMarcel Ziswiler 
712bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
71382e14e8bSStephen Warren 	}
714bc263214SLars-Peter Clausen 	if (bus_control) {
715bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
71664ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
71764ab9baaSMark Brown 	} else {
718f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
7196308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
720f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
721f0fba2adSLiam Girdwood 	}
7226ed25978SAndy Green 
723db2a4165SFrank Mandarino 	return 0;
724db2a4165SFrank Mandarino }
7256f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
726db2a4165SFrank Mandarino #else
7276f8ab4acSMark Brown #define snd_soc_suspend NULL
7286f8ab4acSMark Brown #define snd_soc_resume NULL
729db2a4165SFrank Mandarino #endif
730db2a4165SFrank Mandarino 
73185e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
73202a06d30SBarry Song };
73302a06d30SBarry Song 
73465d9361fSLars-Peter Clausen static struct snd_soc_component *soc_find_component(
73565d9361fSLars-Peter Clausen 	const struct device_node *of_node, const char *name)
73612023a9aSMisael Lopez Cruz {
73765d9361fSLars-Peter Clausen 	struct snd_soc_component *component;
73812023a9aSMisael Lopez Cruz 
73934e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
74034e81ab4SLars-Peter Clausen 
741368dee94SKuninori Morimoto 	for_each_component(component) {
74265d9361fSLars-Peter Clausen 		if (of_node) {
74365d9361fSLars-Peter Clausen 			if (component->dev->of_node == of_node)
74465d9361fSLars-Peter Clausen 				return component;
74565d9361fSLars-Peter Clausen 		} else if (strcmp(component->name, name) == 0) {
74665d9361fSLars-Peter Clausen 			return component;
74712023a9aSMisael Lopez Cruz 		}
74812023a9aSMisael Lopez Cruz 	}
74912023a9aSMisael Lopez Cruz 
75012023a9aSMisael Lopez Cruz 	return NULL;
75112023a9aSMisael Lopez Cruz }
75212023a9aSMisael Lopez Cruz 
753be6ac0a9SKuninori Morimoto static int snd_soc_is_matching_component(
754be6ac0a9SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc,
755be6ac0a9SKuninori Morimoto 	struct snd_soc_component *component)
756be6ac0a9SKuninori Morimoto {
757be6ac0a9SKuninori Morimoto 	struct device_node *component_of_node;
758be6ac0a9SKuninori Morimoto 
759be6ac0a9SKuninori Morimoto 	component_of_node = component->dev->of_node;
760be6ac0a9SKuninori Morimoto 	if (!component_of_node && component->dev->parent)
761be6ac0a9SKuninori Morimoto 		component_of_node = component->dev->parent->of_node;
762be6ac0a9SKuninori Morimoto 
763be6ac0a9SKuninori Morimoto 	if (dlc->of_node && component_of_node != dlc->of_node)
764be6ac0a9SKuninori Morimoto 		return 0;
765be6ac0a9SKuninori Morimoto 	if (dlc->name && strcmp(component->name, dlc->name))
766be6ac0a9SKuninori Morimoto 		return 0;
767be6ac0a9SKuninori Morimoto 
768be6ac0a9SKuninori Morimoto 	return 1;
769be6ac0a9SKuninori Morimoto }
770be6ac0a9SKuninori Morimoto 
771fbb88b5cSMengdong Lin /**
772fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
773fbb88b5cSMengdong Lin  *
7744958471bSJeffy Chen  * @dlc: name of the DAI or the DAI driver and optional component info to match
775fbb88b5cSMengdong Lin  *
776ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
777fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
778fbb88b5cSMengdong Lin  * should also match if being specified.
779fbb88b5cSMengdong Lin  *
780fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
781fbb88b5cSMengdong Lin  */
782305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
78314621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
78412023a9aSMisael Lopez Cruz {
78514621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
78614621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
78712023a9aSMisael Lopez Cruz 
78834e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
78934e81ab4SLars-Peter Clausen 
79014621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs */
791368dee94SKuninori Morimoto 	for_each_component(component) {
792be6ac0a9SKuninori Morimoto 		if (!snd_soc_is_matching_component(dlc, component))
79312023a9aSMisael Lopez Cruz 			continue;
79415a0c645SKuninori Morimoto 		for_each_component_dais(component, dai) {
7954958471bSJeffy Chen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
7966a6dafdaSJeffy Chen 			    && (!dai->driver->name
7976a6dafdaSJeffy Chen 				|| strcmp(dai->driver->name, dlc->dai_name)))
79814621c7eSLars-Peter Clausen 				continue;
79912023a9aSMisael Lopez Cruz 
80014621c7eSLars-Peter Clausen 			return dai;
80112023a9aSMisael Lopez Cruz 		}
80212023a9aSMisael Lopez Cruz 	}
80312023a9aSMisael Lopez Cruz 
80412023a9aSMisael Lopez Cruz 	return NULL;
80512023a9aSMisael Lopez Cruz }
806305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
80712023a9aSMisael Lopez Cruz 
80817fb1755SMengdong Lin /**
80917fb1755SMengdong Lin  * snd_soc_find_dai_link - Find a DAI link
81017fb1755SMengdong Lin  *
81117fb1755SMengdong Lin  * @card: soc card
81217fb1755SMengdong Lin  * @id: DAI link ID to match
81317fb1755SMengdong Lin  * @name: DAI link name to match, optional
8148abab35fSCharles Keepax  * @stream_name: DAI link stream name to match, optional
81517fb1755SMengdong Lin  *
81617fb1755SMengdong Lin  * This function will search all existing DAI links of the soc card to
81717fb1755SMengdong Lin  * find the link of the same ID. Since DAI links may not have their
81817fb1755SMengdong Lin  * unique ID, so name and stream name should also match if being
81917fb1755SMengdong Lin  * specified.
82017fb1755SMengdong Lin  *
82117fb1755SMengdong Lin  * Return: pointer of DAI link, or NULL if not found.
82217fb1755SMengdong Lin  */
82317fb1755SMengdong Lin struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
82417fb1755SMengdong Lin 					       int id, const char *name,
82517fb1755SMengdong Lin 					       const char *stream_name)
82617fb1755SMengdong Lin {
82717fb1755SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
82817fb1755SMengdong Lin 
82917fb1755SMengdong Lin 	lockdep_assert_held(&client_mutex);
83017fb1755SMengdong Lin 
83198061fdbSKuninori Morimoto 	for_each_card_links_safe(card, link, _link) {
83217fb1755SMengdong Lin 		if (link->id != id)
83317fb1755SMengdong Lin 			continue;
83417fb1755SMengdong Lin 
83517fb1755SMengdong Lin 		if (name && (!link->name || strcmp(name, link->name)))
83617fb1755SMengdong Lin 			continue;
83717fb1755SMengdong Lin 
83817fb1755SMengdong Lin 		if (stream_name && (!link->stream_name
83917fb1755SMengdong Lin 			|| strcmp(stream_name, link->stream_name)))
84017fb1755SMengdong Lin 			continue;
84117fb1755SMengdong Lin 
84217fb1755SMengdong Lin 		return link;
84317fb1755SMengdong Lin 	}
84417fb1755SMengdong Lin 
84517fb1755SMengdong Lin 	return NULL;
84617fb1755SMengdong Lin }
84717fb1755SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
84817fb1755SMengdong Lin 
84949a5ba1cSMengdong Lin static bool soc_is_dai_link_bound(struct snd_soc_card *card,
85049a5ba1cSMengdong Lin 		struct snd_soc_dai_link *dai_link)
851db2a4165SFrank Mandarino {
85249a5ba1cSMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
85349a5ba1cSMengdong Lin 
854bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
85549a5ba1cSMengdong Lin 		if (rtd->dai_link == dai_link)
85649a5ba1cSMengdong Lin 			return true;
85749a5ba1cSMengdong Lin 	}
85849a5ba1cSMengdong Lin 
85949a5ba1cSMengdong Lin 	return false;
86049a5ba1cSMengdong Lin }
86149a5ba1cSMengdong Lin 
8626f2f1ff0SMengdong Lin static int soc_bind_dai_link(struct snd_soc_card *card,
8636f2f1ff0SMengdong Lin 	struct snd_soc_dai_link *dai_link)
864db2a4165SFrank Mandarino {
8651a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
86688bd870fSBenoit Cousson 	struct snd_soc_dai_link_component *codecs = dai_link->codecs;
86714621c7eSLars-Peter Clausen 	struct snd_soc_dai_link_component cpu_dai_component;
86890be711eSKuninori Morimoto 	struct snd_soc_component *component;
8691a497983SMengdong Lin 	struct snd_soc_dai **codec_dais;
87088bd870fSBenoit Cousson 	int i;
871db2a4165SFrank Mandarino 
872a655de80SLiam Girdwood 	if (dai_link->ignore)
873a655de80SLiam Girdwood 		return 0;
874a655de80SLiam Girdwood 
8756f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
8766308419aSMark Brown 
87749a5ba1cSMengdong Lin 	if (soc_is_dai_link_bound(card, dai_link)) {
87849a5ba1cSMengdong Lin 		dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
87949a5ba1cSMengdong Lin 			dai_link->name);
88049a5ba1cSMengdong Lin 		return 0;
88149a5ba1cSMengdong Lin 	}
882db2a4165SFrank Mandarino 
883513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
884513cb311SSudip Mukherjee 	if (!rtd)
885513cb311SSudip Mukherjee 		return -ENOMEM;
886513cb311SSudip Mukherjee 
88714621c7eSLars-Peter Clausen 	cpu_dai_component.name = dai_link->cpu_name;
88814621c7eSLars-Peter Clausen 	cpu_dai_component.of_node = dai_link->cpu_of_node;
88914621c7eSLars-Peter Clausen 	cpu_dai_component.dai_name = dai_link->cpu_dai_name;
89014621c7eSLars-Peter Clausen 	rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
891b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
8926b490879SMartin Hundebøll 		dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
893f0fba2adSLiam Girdwood 			 dai_link->cpu_dai_name);
8941a497983SMengdong Lin 		goto _err_defer;
895c5af3a2eSMark Brown 	}
89690be711eSKuninori Morimoto 	snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
897c5af3a2eSMark Brown 
89888bd870fSBenoit Cousson 	rtd->num_codecs = dai_link->num_codecs;
89988bd870fSBenoit Cousson 
90088bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
9010b7990e3SKuninori Morimoto 	/* we can use for_each_rtd_codec_dai() after this */
9021a497983SMengdong Lin 	codec_dais = rtd->codec_dais;
90388bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
90414621c7eSLars-Peter Clausen 		codec_dais[i] = snd_soc_find_dai(&codecs[i]);
90588bd870fSBenoit Cousson 		if (!codec_dais[i]) {
90612023a9aSMisael Lopez Cruz 			dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
90788bd870fSBenoit Cousson 				codecs[i].dai_name);
9081a497983SMengdong Lin 			goto _err_defer;
90912023a9aSMisael Lopez Cruz 		}
91090be711eSKuninori Morimoto 		snd_soc_rtdcom_add(rtd, codec_dais[i]->component);
91188bd870fSBenoit Cousson 	}
91288bd870fSBenoit Cousson 
91388bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
91488bd870fSBenoit Cousson 	rtd->codec_dai = codec_dais[0];
91512023a9aSMisael Lopez Cruz 
916b19e6e7bSMark Brown 	/* find one from the set of registered platforms */
917368dee94SKuninori Morimoto 	for_each_component(component) {
918be6ac0a9SKuninori Morimoto 		if (!snd_soc_is_matching_component(dai_link->platform,
919be6ac0a9SKuninori Morimoto 						   component))
92090be711eSKuninori Morimoto 			continue;
92190be711eSKuninori Morimoto 
92290be711eSKuninori Morimoto 		snd_soc_rtdcom_add(rtd, component);
92390be711eSKuninori Morimoto 	}
92490be711eSKuninori Morimoto 
9251a497983SMengdong Lin 	soc_add_pcm_runtime(card, rtd);
926b19e6e7bSMark Brown 	return 0;
9271a497983SMengdong Lin 
9281a497983SMengdong Lin _err_defer:
9291a497983SMengdong Lin 	soc_free_pcm_runtime(rtd);
9301a497983SMengdong Lin 	return -EPROBE_DEFER;
9316b05eda6SMark Brown }
9326b05eda6SMark Brown 
933f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
934d12cd198SStephen Warren {
935abd31b32SLars-Peter Clausen 	if (!component->card)
93670090bbbSLars-Peter Clausen 		return;
937d12cd198SStephen Warren 
938d9fc4063SKuninori Morimoto 	list_del(&component->card_list);
939d12cd198SStephen Warren 
940999f7f5aSKuninori Morimoto 	if (component->driver->remove)
941999f7f5aSKuninori Morimoto 		component->driver->remove(component);
942d12cd198SStephen Warren 
943f1d45cc3SLars-Peter Clausen 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
944d12cd198SStephen Warren 
945f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
946abd31b32SLars-Peter Clausen 	component->card = NULL;
947f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
948d12cd198SStephen Warren }
949d12cd198SStephen Warren 
950e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
951589c3563SJarkko Nikula {
952589c3563SJarkko Nikula 	int err;
953589c3563SJarkko Nikula 
9542eda3cb1SKuninori Morimoto 	if (!dai || !dai->probed ||
9552eda3cb1SKuninori Morimoto 	    dai->driver->remove_order != order)
9562eda3cb1SKuninori Morimoto 		return;
9572eda3cb1SKuninori Morimoto 
958e60cd14fSLars-Peter Clausen 	if (dai->driver->remove) {
959e60cd14fSLars-Peter Clausen 		err = dai->driver->remove(dai);
960589c3563SJarkko Nikula 		if (err < 0)
961e60cd14fSLars-Peter Clausen 			dev_err(dai->dev,
962b0aa88afSMisael Lopez Cruz 				"ASoC: failed to remove %s: %d\n",
963e60cd14fSLars-Peter Clausen 				dai->name, err);
964b0aa88afSMisael Lopez Cruz 	}
965e60cd14fSLars-Peter Clausen 	dai->probed = 0;
966b0aa88afSMisael Lopez Cruz }
967b0aa88afSMisael Lopez Cruz 
9681a497983SMengdong Lin static void soc_remove_link_dais(struct snd_soc_card *card,
9691a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
970f0fba2adSLiam Girdwood {
971e60cd14fSLars-Peter Clausen 	int i;
9720b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
973f0fba2adSLiam Girdwood 
974f0fba2adSLiam Girdwood 	/* unregister the rtd device */
975f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
97636ae1a96SMark Brown 		device_unregister(rtd->dev);
977f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
97802a06d30SBarry Song 	}
97902a06d30SBarry Song 
980f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
9810b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
9820b7990e3SKuninori Morimoto 		soc_remove_dai(codec_dai, order);
983f0fba2adSLiam Girdwood 
984e60cd14fSLars-Peter Clausen 	soc_remove_dai(rtd->cpu_dai, order);
985f0fba2adSLiam Girdwood }
986f0fba2adSLiam Girdwood 
9871a497983SMengdong Lin static void soc_remove_link_components(struct snd_soc_card *card,
9881a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, int order)
98962ae68faSStephen Warren {
99061aca564SLars-Peter Clausen 	struct snd_soc_component *component;
99190be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
99262ae68faSStephen Warren 
99390be711eSKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
99490be711eSKuninori Morimoto 		component = rtdcom->component;
99562ae68faSStephen Warren 
99670090bbbSLars-Peter Clausen 		if (component->driver->remove_order == order)
99761aca564SLars-Peter Clausen 			soc_remove_component(component);
99862ae68faSStephen Warren 	}
99962ae68faSStephen Warren }
100062ae68faSStephen Warren 
10010671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
10020671fd8eSKuninori Morimoto {
10031a497983SMengdong Lin 	int order;
10041a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
1005f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
10060671fd8eSKuninori Morimoto 
10071a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1008bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd)
10091a497983SMengdong Lin 			soc_remove_link_dais(card, rtd, order);
10100168bf0dSLiam Girdwood 	}
101162ae68faSStephen Warren 
10121a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1013bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd)
10141a497983SMengdong Lin 			soc_remove_link_components(card, rtd, order);
101562ae68faSStephen Warren 	}
101662ae68faSStephen Warren 
101798061fdbSKuninori Morimoto 	for_each_card_links_safe(card, link, _link) {
1018f8f80361SMengdong Lin 		if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1019f8f80361SMengdong Lin 			dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1020f8f80361SMengdong Lin 				link->name);
1021f8f80361SMengdong Lin 
1022f8f80361SMengdong Lin 		list_del(&link->list);
10230671fd8eSKuninori Morimoto 	}
10240671fd8eSKuninori Morimoto }
10250671fd8eSKuninori Morimoto 
1026daecf46eSKuninori Morimoto static int snd_soc_init_platform(struct snd_soc_card *card,
1027daecf46eSKuninori Morimoto 				 struct snd_soc_dai_link *dai_link)
1028daecf46eSKuninori Morimoto {
10294a9ed394SKuninori Morimoto 	struct snd_soc_dai_link_component *platform = dai_link->platform;
1030*04eb1efcSRohit kumar 
1031daecf46eSKuninori Morimoto 	/*
1032daecf46eSKuninori Morimoto 	 * FIXME
1033daecf46eSKuninori Morimoto 	 *
1034daecf46eSKuninori Morimoto 	 * this function should be removed in the future
1035daecf46eSKuninori Morimoto 	 */
1036daecf46eSKuninori Morimoto 	/* convert Legacy platform link */
10374a9ed394SKuninori Morimoto 	if (!platform) {
10384a9ed394SKuninori Morimoto 		platform = devm_kzalloc(card->dev,
1039daecf46eSKuninori Morimoto 				sizeof(struct snd_soc_dai_link_component),
1040daecf46eSKuninori Morimoto 				GFP_KERNEL);
10414a9ed394SKuninori Morimoto 		if (!platform)
1042daecf46eSKuninori Morimoto 			return -ENOMEM;
1043daecf46eSKuninori Morimoto 
10444a9ed394SKuninori Morimoto 		dai_link->platform	= platform;
10454a9ed394SKuninori Morimoto 		platform->name		= dai_link->platform_name;
10464a9ed394SKuninori Morimoto 		platform->of_node	= dai_link->platform_of_node;
10474a9ed394SKuninori Morimoto 		platform->dai_name	= NULL;
10484a9ed394SKuninori Morimoto 	}
10494a9ed394SKuninori Morimoto 
10504a9ed394SKuninori Morimoto 	/* if there's no platform we match on the empty platform */
10514a9ed394SKuninori Morimoto 	if (!platform->name &&
10524a9ed394SKuninori Morimoto 	    !platform->of_node)
10534a9ed394SKuninori Morimoto 		platform->name = "snd-soc-dummy";
1054daecf46eSKuninori Morimoto 
1055daecf46eSKuninori Morimoto 	return 0;
1056daecf46eSKuninori Morimoto }
1057daecf46eSKuninori Morimoto 
1058923c5e61SMengdong Lin static int snd_soc_init_multicodec(struct snd_soc_card *card,
1059923c5e61SMengdong Lin 				   struct snd_soc_dai_link *dai_link)
1060923c5e61SMengdong Lin {
1061923c5e61SMengdong Lin 	/* Legacy codec/codec_dai link is a single entry in multicodec */
1062923c5e61SMengdong Lin 	if (dai_link->codec_name || dai_link->codec_of_node ||
1063923c5e61SMengdong Lin 	    dai_link->codec_dai_name) {
1064923c5e61SMengdong Lin 		dai_link->num_codecs = 1;
1065923c5e61SMengdong Lin 
1066923c5e61SMengdong Lin 		dai_link->codecs = devm_kzalloc(card->dev,
1067923c5e61SMengdong Lin 				sizeof(struct snd_soc_dai_link_component),
1068923c5e61SMengdong Lin 				GFP_KERNEL);
1069923c5e61SMengdong Lin 		if (!dai_link->codecs)
1070923c5e61SMengdong Lin 			return -ENOMEM;
1071923c5e61SMengdong Lin 
1072923c5e61SMengdong Lin 		dai_link->codecs[0].name = dai_link->codec_name;
1073923c5e61SMengdong Lin 		dai_link->codecs[0].of_node = dai_link->codec_of_node;
1074923c5e61SMengdong Lin 		dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1075923c5e61SMengdong Lin 	}
1076923c5e61SMengdong Lin 
1077923c5e61SMengdong Lin 	if (!dai_link->codecs) {
1078923c5e61SMengdong Lin 		dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1079923c5e61SMengdong Lin 		return -EINVAL;
1080923c5e61SMengdong Lin 	}
1081923c5e61SMengdong Lin 
1082923c5e61SMengdong Lin 	return 0;
1083923c5e61SMengdong Lin }
1084923c5e61SMengdong Lin 
1085923c5e61SMengdong Lin static int soc_init_dai_link(struct snd_soc_card *card,
1086923c5e61SMengdong Lin 			     struct snd_soc_dai_link *link)
1087923c5e61SMengdong Lin {
1088923c5e61SMengdong Lin 	int i, ret;
10893db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *codec;
1090923c5e61SMengdong Lin 
1091daecf46eSKuninori Morimoto 	ret = snd_soc_init_platform(card, link);
1092daecf46eSKuninori Morimoto 	if (ret) {
1093daecf46eSKuninori Morimoto 		dev_err(card->dev, "ASoC: failed to init multiplatform\n");
1094daecf46eSKuninori Morimoto 		return ret;
1095daecf46eSKuninori Morimoto 	}
1096daecf46eSKuninori Morimoto 
1097923c5e61SMengdong Lin 	ret = snd_soc_init_multicodec(card, link);
1098923c5e61SMengdong Lin 	if (ret) {
1099923c5e61SMengdong Lin 		dev_err(card->dev, "ASoC: failed to init multicodec\n");
1100923c5e61SMengdong Lin 		return ret;
1101923c5e61SMengdong Lin 	}
1102923c5e61SMengdong Lin 
11033db769f1SKuninori Morimoto 	for_each_link_codecs(link, i, codec) {
1104923c5e61SMengdong Lin 		/*
1105923c5e61SMengdong Lin 		 * Codec must be specified by 1 of name or OF node,
1106923c5e61SMengdong Lin 		 * not both or neither.
1107923c5e61SMengdong Lin 		 */
11083db769f1SKuninori Morimoto 		if (!!codec->name ==
11093db769f1SKuninori Morimoto 		    !!codec->of_node) {
1110923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1111923c5e61SMengdong Lin 				link->name);
1112923c5e61SMengdong Lin 			return -EINVAL;
1113923c5e61SMengdong Lin 		}
1114923c5e61SMengdong Lin 		/* Codec DAI name must be specified */
11153db769f1SKuninori Morimoto 		if (!codec->dai_name) {
1116923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1117923c5e61SMengdong Lin 				link->name);
1118923c5e61SMengdong Lin 			return -EINVAL;
1119923c5e61SMengdong Lin 		}
1120923c5e61SMengdong Lin 	}
1121923c5e61SMengdong Lin 
1122923c5e61SMengdong Lin 	/*
1123923c5e61SMengdong Lin 	 * Platform may be specified by either name or OF node, but
1124923c5e61SMengdong Lin 	 * can be left unspecified, and a dummy platform will be used.
1125923c5e61SMengdong Lin 	 */
1126daecf46eSKuninori Morimoto 	if (link->platform->name && link->platform->of_node) {
1127923c5e61SMengdong Lin 		dev_err(card->dev,
1128923c5e61SMengdong Lin 			"ASoC: Both platform name/of_node are set for %s\n",
1129923c5e61SMengdong Lin 			link->name);
1130923c5e61SMengdong Lin 		return -EINVAL;
1131923c5e61SMengdong Lin 	}
11328780cf11SAjit Pandey 
11338780cf11SAjit Pandey 	/*
11348780cf11SAjit Pandey 	 * Defer card registartion if platform dai component is not added to
11358780cf11SAjit Pandey 	 * component list.
11368780cf11SAjit Pandey 	 */
11378780cf11SAjit Pandey 	if (!soc_find_component(link->platform->of_node, link->platform->name))
11388780cf11SAjit Pandey 		return -EPROBE_DEFER;
11398780cf11SAjit Pandey 
1140923c5e61SMengdong Lin 	/*
1141923c5e61SMengdong Lin 	 * CPU device may be specified by either name or OF node, but
1142923c5e61SMengdong Lin 	 * can be left unspecified, and will be matched based on DAI
1143923c5e61SMengdong Lin 	 * name alone..
1144923c5e61SMengdong Lin 	 */
1145923c5e61SMengdong Lin 	if (link->cpu_name && link->cpu_of_node) {
1146923c5e61SMengdong Lin 		dev_err(card->dev,
1147923c5e61SMengdong Lin 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
1148923c5e61SMengdong Lin 			link->name);
1149923c5e61SMengdong Lin 		return -EINVAL;
1150923c5e61SMengdong Lin 	}
11518780cf11SAjit Pandey 
11528780cf11SAjit Pandey 	/*
11538780cf11SAjit Pandey 	 * Defer card registartion if cpu dai component is not added to
11548780cf11SAjit Pandey 	 * component list.
11558780cf11SAjit Pandey 	 */
11568780cf11SAjit Pandey 	if (!soc_find_component(link->cpu_of_node, link->cpu_name))
11578780cf11SAjit Pandey 		return -EPROBE_DEFER;
11588780cf11SAjit Pandey 
1159923c5e61SMengdong Lin 	/*
1160923c5e61SMengdong Lin 	 * At least one of CPU DAI name or CPU device name/node must be
1161923c5e61SMengdong Lin 	 * specified
1162923c5e61SMengdong Lin 	 */
1163923c5e61SMengdong Lin 	if (!link->cpu_dai_name &&
1164923c5e61SMengdong Lin 	    !(link->cpu_name || link->cpu_of_node)) {
1165923c5e61SMengdong Lin 		dev_err(card->dev,
1166923c5e61SMengdong Lin 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1167923c5e61SMengdong Lin 			link->name);
1168923c5e61SMengdong Lin 		return -EINVAL;
1169923c5e61SMengdong Lin 	}
1170923c5e61SMengdong Lin 
1171923c5e61SMengdong Lin 	return 0;
1172923c5e61SMengdong Lin }
1173923c5e61SMengdong Lin 
1174ef2e8175SKuninori Morimoto void snd_soc_disconnect_sync(struct device *dev)
1175ef2e8175SKuninori Morimoto {
11762c7b696aSMarcel Ziswiler 	struct snd_soc_component *component =
11772c7b696aSMarcel Ziswiler 			snd_soc_lookup_component(dev, NULL);
1178ef2e8175SKuninori Morimoto 
1179ef2e8175SKuninori Morimoto 	if (!component || !component->card)
1180ef2e8175SKuninori Morimoto 		return;
1181ef2e8175SKuninori Morimoto 
1182ef2e8175SKuninori Morimoto 	snd_card_disconnect_sync(component->card->snd_card);
1183ef2e8175SKuninori Morimoto }
1184df532185SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
1185ef2e8175SKuninori Morimoto 
1186f8f80361SMengdong Lin /**
1187f8f80361SMengdong Lin  * snd_soc_add_dai_link - Add a DAI link dynamically
1188f8f80361SMengdong Lin  * @card: The ASoC card to which the DAI link is added
1189f8f80361SMengdong Lin  * @dai_link: The new DAI link to add
1190f8f80361SMengdong Lin  *
1191f8f80361SMengdong Lin  * This function adds a DAI link to the ASoC card's link list.
1192f8f80361SMengdong Lin  *
1193f8f80361SMengdong Lin  * Note: Topology can use this API to add DAI links when probing the
1194f8f80361SMengdong Lin  * topology component. And machine drivers can still define static
1195f8f80361SMengdong Lin  * DAI links in dai_link array.
1196f8f80361SMengdong Lin  */
1197f8f80361SMengdong Lin int snd_soc_add_dai_link(struct snd_soc_card *card,
1198f8f80361SMengdong Lin 		struct snd_soc_dai_link *dai_link)
1199f8f80361SMengdong Lin {
1200f8f80361SMengdong Lin 	if (dai_link->dobj.type
1201f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1202f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1203f8f80361SMengdong Lin 			dai_link->dobj.type);
1204f8f80361SMengdong Lin 		return -EINVAL;
1205f8f80361SMengdong Lin 	}
1206f8f80361SMengdong Lin 
1207f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
12082c7b696aSMarcel Ziswiler 	/*
12092c7b696aSMarcel Ziswiler 	 * Notify the machine driver for extra initialization
1210d6f220eaSMengdong Lin 	 * on the link created by topology.
1211d6f220eaSMengdong Lin 	 */
1212d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->add_dai_link)
1213d6f220eaSMengdong Lin 		card->add_dai_link(card, dai_link);
1214d6f220eaSMengdong Lin 
1215f8f80361SMengdong Lin 	list_add_tail(&dai_link->list, &card->dai_link_list);
1216f8f80361SMengdong Lin 
1217f8f80361SMengdong Lin 	return 0;
1218f8f80361SMengdong Lin }
1219f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1220f8f80361SMengdong Lin 
1221f8f80361SMengdong Lin /**
1222f8f80361SMengdong Lin  * snd_soc_remove_dai_link - Remove a DAI link from the list
1223f8f80361SMengdong Lin  * @card: The ASoC card that owns the link
1224f8f80361SMengdong Lin  * @dai_link: The DAI link to remove
1225f8f80361SMengdong Lin  *
1226f8f80361SMengdong Lin  * This function removes a DAI link from the ASoC card's link list.
1227f8f80361SMengdong Lin  *
1228f8f80361SMengdong Lin  * For DAI links previously added by topology, topology should
1229f8f80361SMengdong Lin  * remove them by using the dobj embedded in the link.
1230f8f80361SMengdong Lin  */
1231f8f80361SMengdong Lin void snd_soc_remove_dai_link(struct snd_soc_card *card,
1232f8f80361SMengdong Lin 			     struct snd_soc_dai_link *dai_link)
1233f8f80361SMengdong Lin {
1234f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
1235f8f80361SMengdong Lin 
1236f8f80361SMengdong Lin 	if (dai_link->dobj.type
1237f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1238f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1239f8f80361SMengdong Lin 			dai_link->dobj.type);
1240f8f80361SMengdong Lin 		return;
1241f8f80361SMengdong Lin 	}
1242f8f80361SMengdong Lin 
1243f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
12442c7b696aSMarcel Ziswiler 	/*
12452c7b696aSMarcel Ziswiler 	 * Notify the machine driver for extra destruction
1246d6f220eaSMengdong Lin 	 * on the link created by topology.
1247d6f220eaSMengdong Lin 	 */
1248d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->remove_dai_link)
1249d6f220eaSMengdong Lin 		card->remove_dai_link(card, dai_link);
1250d6f220eaSMengdong Lin 
125198061fdbSKuninori Morimoto 	for_each_card_links_safe(card, link, _link) {
1252f8f80361SMengdong Lin 		if (link == dai_link) {
1253f8f80361SMengdong Lin 			list_del(&link->list);
1254f8f80361SMengdong Lin 			return;
1255f8f80361SMengdong Lin 		}
1256f8f80361SMengdong Lin 	}
1257f8f80361SMengdong Lin }
1258f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1259f0fba2adSLiam Girdwood 
1260aefba455SJerome Brunet static void soc_set_of_name_prefix(struct snd_soc_component *component)
1261aefba455SJerome Brunet {
1262aefba455SJerome Brunet 	struct device_node *component_of_node = component->dev->of_node;
1263aefba455SJerome Brunet 	const char *str;
1264aefba455SJerome Brunet 	int ret;
1265aefba455SJerome Brunet 
1266aefba455SJerome Brunet 	if (!component_of_node && component->dev->parent)
1267aefba455SJerome Brunet 		component_of_node = component->dev->parent->of_node;
1268aefba455SJerome Brunet 
1269aefba455SJerome Brunet 	ret = of_property_read_string(component_of_node, "sound-name-prefix",
1270aefba455SJerome Brunet 				      &str);
1271aefba455SJerome Brunet 	if (!ret)
1272aefba455SJerome Brunet 		component->name_prefix = str;
1273aefba455SJerome Brunet }
1274aefba455SJerome Brunet 
1275ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
127694f99c87SLars-Peter Clausen 				struct snd_soc_component *component)
1277ead9b919SJarkko Nikula {
1278ead9b919SJarkko Nikula 	int i;
1279ead9b919SJarkko Nikula 
1280aefba455SJerome Brunet 	for (i = 0; i < card->num_configs && card->codec_conf; i++) {
1281ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1282b24c539bSCharles Keepax 		struct device_node *component_of_node = component->dev->of_node;
1283b24c539bSCharles Keepax 
1284b24c539bSCharles Keepax 		if (!component_of_node && component->dev->parent)
1285b24c539bSCharles Keepax 			component_of_node = component->dev->parent->of_node;
1286b24c539bSCharles Keepax 
1287b24c539bSCharles Keepax 		if (map->of_node && component_of_node != map->of_node)
12883ca041edSSebastian Reichel 			continue;
128994f99c87SLars-Peter Clausen 		if (map->dev_name && strcmp(component->name, map->dev_name))
12903ca041edSSebastian Reichel 			continue;
129194f99c87SLars-Peter Clausen 		component->name_prefix = map->name_prefix;
1292aefba455SJerome Brunet 		return;
1293ead9b919SJarkko Nikula 	}
1294aefba455SJerome Brunet 
1295aefba455SJerome Brunet 	/*
1296aefba455SJerome Brunet 	 * If there is no configuration table or no match in the table,
1297aefba455SJerome Brunet 	 * check if a prefix is provided in the node
1298aefba455SJerome Brunet 	 */
1299aefba455SJerome Brunet 	soc_set_of_name_prefix(component);
1300ead9b919SJarkko Nikula }
1301ead9b919SJarkko Nikula 
1302f1d45cc3SLars-Peter Clausen static int soc_probe_component(struct snd_soc_card *card,
1303f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component)
1304589c3563SJarkko Nikula {
13052c7b696aSMarcel Ziswiler 	struct snd_soc_dapm_context *dapm =
13062c7b696aSMarcel Ziswiler 			snd_soc_component_get_dapm(component);
1307888df395SMark Brown 	struct snd_soc_dai *dai;
1308f1d45cc3SLars-Peter Clausen 	int ret;
1309589c3563SJarkko Nikula 
13101b7c1231SLars-Peter Clausen 	if (!strcmp(component->name, "snd-soc-dummy"))
131170090bbbSLars-Peter Clausen 		return 0;
1312589c3563SJarkko Nikula 
1313abd31b32SLars-Peter Clausen 	if (component->card) {
13141b7c1231SLars-Peter Clausen 		if (component->card != card) {
13151b7c1231SLars-Peter Clausen 			dev_err(component->dev,
13161b7c1231SLars-Peter Clausen 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
13171b7c1231SLars-Peter Clausen 				card->name, component->card->name);
13181b7c1231SLars-Peter Clausen 			return -ENODEV;
13191b7c1231SLars-Peter Clausen 		}
13201b7c1231SLars-Peter Clausen 		return 0;
13211b7c1231SLars-Peter Clausen 	}
13221b7c1231SLars-Peter Clausen 
1323abd31b32SLars-Peter Clausen 	if (!try_module_get(component->dev->driver->owner))
1324abd31b32SLars-Peter Clausen 		return -ENODEV;
1325abd31b32SLars-Peter Clausen 
1326f1d45cc3SLars-Peter Clausen 	component->card = card;
1327f1d45cc3SLars-Peter Clausen 	dapm->card = card;
1328f1d45cc3SLars-Peter Clausen 	soc_set_name_prefix(card, component);
1329589c3563SJarkko Nikula 
1330f1d45cc3SLars-Peter Clausen 	soc_init_component_debugfs(component);
1331d5d1e0beSLars-Peter Clausen 
1332688d0ebfSKuninori Morimoto 	if (component->driver->dapm_widgets) {
1333688d0ebfSKuninori Morimoto 		ret = snd_soc_dapm_new_controls(dapm,
1334688d0ebfSKuninori Morimoto 					component->driver->dapm_widgets,
1335688d0ebfSKuninori Morimoto 					component->driver->num_dapm_widgets);
133677530150SLars-Peter Clausen 
1337b318ad50SNariman Poushin 		if (ret != 0) {
1338f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1339b318ad50SNariman Poushin 				"Failed to create new controls %d\n", ret);
1340b318ad50SNariman Poushin 			goto err_probe;
1341b318ad50SNariman Poushin 		}
1342b318ad50SNariman Poushin 	}
1343b318ad50SNariman Poushin 
134415a0c645SKuninori Morimoto 	for_each_component_dais(component, dai) {
13450634814fSLars-Peter Clausen 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1346261edc70SNariman Poushin 		if (ret != 0) {
1347f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1348261edc70SNariman Poushin 				"Failed to create DAI widgets %d\n", ret);
1349261edc70SNariman Poushin 			goto err_probe;
1350261edc70SNariman Poushin 		}
1351261edc70SNariman Poushin 	}
1352888df395SMark Brown 
1353999f7f5aSKuninori Morimoto 	if (component->driver->probe) {
1354999f7f5aSKuninori Morimoto 		ret = component->driver->probe(component);
1355589c3563SJarkko Nikula 		if (ret < 0) {
1356f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1357f1d45cc3SLars-Peter Clausen 				"ASoC: failed to probe component %d\n", ret);
135870d29331SJarkko Nikula 			goto err_probe;
1359589c3563SJarkko Nikula 		}
1360cb2cf612SLiam Girdwood 
1361f1d45cc3SLars-Peter Clausen 		WARN(dapm->idle_bias_off &&
1362f1d45cc3SLars-Peter Clausen 			dapm->bias_level != SND_SOC_BIAS_OFF,
1363956245e9SLiam Girdwood 			"codec %s can not start from non-off bias with idle_bias_off==1\n",
1364f1d45cc3SLars-Peter Clausen 			component->name);
1365956245e9SLiam Girdwood 	}
1366956245e9SLiam Girdwood 
1367f2ed6b07SMengdong Lin 	/* machine specific init */
1368f2ed6b07SMengdong Lin 	if (component->init) {
1369f2ed6b07SMengdong Lin 		ret = component->init(component);
1370f2ed6b07SMengdong Lin 		if (ret < 0) {
1371f2ed6b07SMengdong Lin 			dev_err(component->dev,
1372f2ed6b07SMengdong Lin 				"Failed to do machine specific init %d\n", ret);
1373f2ed6b07SMengdong Lin 			goto err_probe;
1374f2ed6b07SMengdong Lin 		}
1375f2ed6b07SMengdong Lin 	}
1376f2ed6b07SMengdong Lin 
1377b8972bf0SKuninori Morimoto 	if (component->driver->controls)
1378b8972bf0SKuninori Morimoto 		snd_soc_add_component_controls(component,
1379b8972bf0SKuninori Morimoto 					       component->driver->controls,
1380b8972bf0SKuninori Morimoto 					       component->driver->num_controls);
13816969b2baSKuninori Morimoto 	if (component->driver->dapm_routes)
13826969b2baSKuninori Morimoto 		snd_soc_dapm_add_routes(dapm,
13836969b2baSKuninori Morimoto 					component->driver->dapm_routes,
13846969b2baSKuninori Morimoto 					component->driver->num_dapm_routes);
1385956245e9SLiam Girdwood 
1386f1d45cc3SLars-Peter Clausen 	list_add(&dapm->list, &card->dapm_list);
1387f70f18f7SKuninori Morimoto 	/* see for_each_card_components */
1388d9fc4063SKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1389956245e9SLiam Girdwood 
1390956245e9SLiam Girdwood 	return 0;
1391956245e9SLiam Girdwood 
1392956245e9SLiam Girdwood err_probe:
1393f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1394abd31b32SLars-Peter Clausen 	component->card = NULL;
1395f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1396956245e9SLiam Girdwood 
1397956245e9SLiam Girdwood 	return ret;
1398956245e9SLiam Girdwood }
1399956245e9SLiam Girdwood 
140036ae1a96SMark Brown static void rtd_release(struct device *dev)
140136ae1a96SMark Brown {
140236ae1a96SMark Brown 	kfree(dev);
140336ae1a96SMark Brown }
1404f0fba2adSLiam Girdwood 
14055f3484acSLars-Peter Clausen static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
14065f3484acSLars-Peter Clausen 	const char *name)
1407503ae5e0SMisael Lopez Cruz {
1408589c3563SJarkko Nikula 	int ret = 0;
1409589c3563SJarkko Nikula 
1410589c3563SJarkko Nikula 	/* register the rtd device */
141136ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
141236ae1a96SMark Brown 	if (!rtd->dev)
141336ae1a96SMark Brown 		return -ENOMEM;
141436ae1a96SMark Brown 	device_initialize(rtd->dev);
14155f3484acSLars-Peter Clausen 	rtd->dev->parent = rtd->card->dev;
141636ae1a96SMark Brown 	rtd->dev->release = rtd_release;
1417d29697dcSTakashi Iwai 	rtd->dev->groups = soc_dev_attr_groups;
1418f294afedSLars-Peter Clausen 	dev_set_name(rtd->dev, "%s", name);
141936ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1420b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
142101d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
142201d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
142301d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
142401d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
142536ae1a96SMark Brown 	ret = device_add(rtd->dev);
1426589c3563SJarkko Nikula 	if (ret < 0) {
1427865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1428865df9cbSChuansheng Liu 		put_device(rtd->dev);
14295f3484acSLars-Peter Clausen 		dev_err(rtd->card->dev,
1430f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1431589c3563SJarkko Nikula 		return ret;
1432589c3563SJarkko Nikula 	}
1433589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1434589c3563SJarkko Nikula 	return 0;
1435589c3563SJarkko Nikula }
1436589c3563SJarkko Nikula 
14371a497983SMengdong Lin static int soc_probe_link_components(struct snd_soc_card *card,
14382c7b696aSMarcel Ziswiler 				     struct snd_soc_pcm_runtime *rtd, int order)
143962ae68faSStephen Warren {
1440f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component;
144190be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
144290be711eSKuninori Morimoto 	int ret;
144362ae68faSStephen Warren 
144490be711eSKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
144590be711eSKuninori Morimoto 		component = rtdcom->component;
144690be711eSKuninori Morimoto 
144770090bbbSLars-Peter Clausen 		if (component->driver->probe_order == order) {
1448f1d45cc3SLars-Peter Clausen 			ret = soc_probe_component(card, component);
144962ae68faSStephen Warren 			if (ret < 0)
145062ae68faSStephen Warren 				return ret;
145162ae68faSStephen Warren 		}
145262ae68faSStephen Warren 	}
145362ae68faSStephen Warren 
145462ae68faSStephen Warren 	return 0;
145562ae68faSStephen Warren }
145662ae68faSStephen Warren 
14578e2be562SLars-Peter Clausen static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1458b0aa88afSMisael Lopez Cruz {
14597a2ccad5SKuninori Morimoto 	if (dai->probed ||
14607a2ccad5SKuninori Morimoto 	    dai->driver->probe_order != order)
14617a2ccad5SKuninori Morimoto 		return 0;
1462b0aa88afSMisael Lopez Cruz 
14638e2be562SLars-Peter Clausen 	if (dai->driver->probe) {
14647a2ccad5SKuninori Morimoto 		int ret = dai->driver->probe(dai);
14652c7b696aSMarcel Ziswiler 
1466b0aa88afSMisael Lopez Cruz 		if (ret < 0) {
14677a2ccad5SKuninori Morimoto 			dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
14688e2be562SLars-Peter Clausen 				dai->name, ret);
1469b0aa88afSMisael Lopez Cruz 			return ret;
1470b0aa88afSMisael Lopez Cruz 		}
1471b0aa88afSMisael Lopez Cruz 	}
1472b0aa88afSMisael Lopez Cruz 
14738e2be562SLars-Peter Clausen 	dai->probed = 1;
1474b0aa88afSMisael Lopez Cruz 
1475b0aa88afSMisael Lopez Cruz 	return 0;
1476b0aa88afSMisael Lopez Cruz }
1477b0aa88afSMisael Lopez Cruz 
147825f7b701SArnaud Pouliquen static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
147925f7b701SArnaud Pouliquen 				struct snd_soc_pcm_runtime *rtd)
148025f7b701SArnaud Pouliquen {
148125f7b701SArnaud Pouliquen 	int i, ret = 0;
148225f7b701SArnaud Pouliquen 
148325f7b701SArnaud Pouliquen 	for (i = 0; i < num_dais; ++i) {
148425f7b701SArnaud Pouliquen 		struct snd_soc_dai_driver *drv = dais[i]->driver;
148525f7b701SArnaud Pouliquen 
1486de17f14eSRohit kumar 		if (drv->pcm_new)
148725f7b701SArnaud Pouliquen 			ret = drv->pcm_new(rtd, dais[i]);
148825f7b701SArnaud Pouliquen 		if (ret < 0) {
148925f7b701SArnaud Pouliquen 			dev_err(dais[i]->dev,
149025f7b701SArnaud Pouliquen 				"ASoC: Failed to bind %s with pcm device\n",
149125f7b701SArnaud Pouliquen 				dais[i]->name);
149225f7b701SArnaud Pouliquen 			return ret;
149325f7b701SArnaud Pouliquen 		}
149425f7b701SArnaud Pouliquen 	}
149525f7b701SArnaud Pouliquen 
149625f7b701SArnaud Pouliquen 	return 0;
149725f7b701SArnaud Pouliquen }
149825f7b701SArnaud Pouliquen 
14991a497983SMengdong Lin static int soc_probe_link_dais(struct snd_soc_card *card,
15001a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1501f0fba2adSLiam Girdwood {
15021a497983SMengdong Lin 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1503c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1504a655de80SLiam Girdwood 	struct snd_soc_rtdcom_list *rtdcom;
1505a655de80SLiam Girdwood 	struct snd_soc_component *component;
15060b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1507a655de80SLiam Girdwood 	int i, ret, num;
1508f0fba2adSLiam Girdwood 
1509f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
15101a497983SMengdong Lin 			card->name, rtd->num, order);
1511f0fba2adSLiam Girdwood 
1512f0fba2adSLiam Girdwood 	/* set default power off timeout */
1513f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1514f0fba2adSLiam Girdwood 
15158e2be562SLars-Peter Clausen 	ret = soc_probe_dai(cpu_dai, order);
15168e2be562SLars-Peter Clausen 	if (ret)
1517f0fba2adSLiam Girdwood 		return ret;
1518f0fba2adSLiam Girdwood 
1519f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
15200b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
15210b7990e3SKuninori Morimoto 		ret = soc_probe_dai(codec_dai, order);
1522b0aa88afSMisael Lopez Cruz 		if (ret)
1523f0fba2adSLiam Girdwood 			return ret;
152488bd870fSBenoit Cousson 	}
1525f0fba2adSLiam Girdwood 
15260168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
15270168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
15280168bf0dSLiam Girdwood 		return 0;
15290168bf0dSLiam Girdwood 
15305f3484acSLars-Peter Clausen 	/* do machine specific initialization */
15315f3484acSLars-Peter Clausen 	if (dai_link->init) {
15325f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
15335f3484acSLars-Peter Clausen 		if (ret < 0) {
15345f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
15355f3484acSLars-Peter Clausen 				dai_link->name, ret);
15365f3484acSLars-Peter Clausen 			return ret;
15375f3484acSLars-Peter Clausen 		}
15385f3484acSLars-Peter Clausen 	}
15395f3484acSLars-Peter Clausen 
1540a5053a8eSKuninori Morimoto 	if (dai_link->dai_fmt)
1541a5053a8eSKuninori Morimoto 		snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1542a5053a8eSKuninori Morimoto 
15435f3484acSLars-Peter Clausen 	ret = soc_post_component_init(rtd, dai_link->name);
1544589c3563SJarkko Nikula 	if (ret)
1545f0fba2adSLiam Girdwood 		return ret;
1546f0fba2adSLiam Girdwood 
15475f3484acSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
15485f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
15492e55b90aSLars-Peter Clausen 	if (dai_link->dynamic)
15502e55b90aSLars-Peter Clausen 		soc_dpcm_debugfs_add(rtd);
15515f3484acSLars-Peter Clausen #endif
15525f3484acSLars-Peter Clausen 
1553a655de80SLiam Girdwood 	num = rtd->num;
1554a655de80SLiam Girdwood 
1555a655de80SLiam Girdwood 	/*
1556a655de80SLiam Girdwood 	 * most drivers will register their PCMs using DAI link ordering but
1557a655de80SLiam Girdwood 	 * topology based drivers can use the DAI link id field to set PCM
1558a655de80SLiam Girdwood 	 * device number and then use rtd + a base offset of the BEs.
1559a655de80SLiam Girdwood 	 */
1560a655de80SLiam Girdwood 	for_each_rtdcom(rtd, rtdcom) {
1561a655de80SLiam Girdwood 		component = rtdcom->component;
1562a655de80SLiam Girdwood 
1563a655de80SLiam Girdwood 		if (!component->driver->use_dai_pcm_id)
1564a655de80SLiam Girdwood 			continue;
1565a655de80SLiam Girdwood 
1566a655de80SLiam Girdwood 		if (rtd->dai_link->no_pcm)
1567a655de80SLiam Girdwood 			num += component->driver->be_pcm_base;
1568a655de80SLiam Girdwood 		else
1569a655de80SLiam Girdwood 			num = rtd->dai_link->id;
1570a655de80SLiam Girdwood 	}
1571a655de80SLiam Girdwood 
15726f0c4226SJie Yang 	if (cpu_dai->driver->compress_new) {
15731245b700SNamarta Kohli 		/* create compress_device" */
1574a655de80SLiam Girdwood 		ret = cpu_dai->driver->compress_new(rtd, num);
15751245b700SNamarta Kohli 		if (ret < 0) {
1576f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
15771245b700SNamarta Kohli 					 dai_link->stream_name);
15781245b700SNamarta Kohli 			return ret;
15791245b700SNamarta Kohli 		}
15801245b700SNamarta Kohli 	} else {
15811245b700SNamarta Kohli 
1582c74184edSMark Brown 		if (!dai_link->params) {
1583f0fba2adSLiam Girdwood 			/* create the pcm */
1584a655de80SLiam Girdwood 			ret = soc_new_pcm(rtd, num);
1585f0fba2adSLiam Girdwood 			if (ret < 0) {
1586f110bfc7SLiam Girdwood 				dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
15870837fc62SFabio Estevam 					dai_link->stream_name, ret);
1588f0fba2adSLiam Girdwood 				return ret;
1589f0fba2adSLiam Girdwood 			}
159025f7b701SArnaud Pouliquen 			ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
159125f7b701SArnaud Pouliquen 			if (ret < 0)
159225f7b701SArnaud Pouliquen 				return ret;
159325f7b701SArnaud Pouliquen 			ret = soc_link_dai_pcm_new(rtd->codec_dais,
159425f7b701SArnaud Pouliquen 						   rtd->num_codecs, rtd);
159525f7b701SArnaud Pouliquen 			if (ret < 0)
159625f7b701SArnaud Pouliquen 				return ret;
1597c74184edSMark Brown 		} else {
15989d58a077SRichard Fitzgerald 			INIT_DELAYED_WORK(&rtd->delayed_work,
15999d58a077SRichard Fitzgerald 						codec2codec_close_delayed_work);
1600c74184edSMark Brown 		}
1601c74184edSMark Brown 	}
1602c74184edSMark Brown 
1603f0fba2adSLiam Girdwood 	return 0;
1604f0fba2adSLiam Girdwood }
1605f0fba2adSLiam Girdwood 
160644c69bb1SLars-Peter Clausen static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1607b19e6e7bSMark Brown {
16083ca041edSSebastian Reichel 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1609f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1610f2ed6b07SMengdong Lin 	const char *name;
1611f2ed6b07SMengdong Lin 	struct device_node *codec_of_node;
16123ca041edSSebastian Reichel 
1613f2ed6b07SMengdong Lin 	if (aux_dev->codec_of_node || aux_dev->codec_name) {
1614f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1615f2ed6b07SMengdong Lin 		name = aux_dev->codec_name;
1616f2ed6b07SMengdong Lin 		codec_of_node = aux_dev->codec_of_node;
1617f2ed6b07SMengdong Lin 		component = soc_find_component(codec_of_node, name);
1618f2ed6b07SMengdong Lin 		if (!component) {
1619f2ed6b07SMengdong Lin 			if (codec_of_node)
1620f2ed6b07SMengdong Lin 				name = of_node_full_name(codec_of_node);
1621f2ed6b07SMengdong Lin 			goto err_defer;
1622f2ed6b07SMengdong Lin 		}
1623f2ed6b07SMengdong Lin 	} else if (aux_dev->name) {
1624f2ed6b07SMengdong Lin 		/* generic components */
1625f2ed6b07SMengdong Lin 		name = aux_dev->name;
1626f2ed6b07SMengdong Lin 		component = soc_find_component(NULL, name);
1627f2ed6b07SMengdong Lin 		if (!component)
1628f2ed6b07SMengdong Lin 			goto err_defer;
1629f2ed6b07SMengdong Lin 	} else {
1630f2ed6b07SMengdong Lin 		dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1631f2ed6b07SMengdong Lin 		return -EINVAL;
1632f2ed6b07SMengdong Lin 	}
16333ca041edSSebastian Reichel 
1634f2ed6b07SMengdong Lin 	component->init = aux_dev->init;
1635d2e3a135SSylwester Nawrocki 	list_add(&component->card_aux_list, &card->aux_comp_list);
16361a653aa4SKuninori Morimoto 
1637f2ed6b07SMengdong Lin 	return 0;
1638f2ed6b07SMengdong Lin 
1639f2ed6b07SMengdong Lin err_defer:
164065d9361fSLars-Peter Clausen 	dev_err(card->dev, "ASoC: %s not registered\n", name);
1641b19e6e7bSMark Brown 	return -EPROBE_DEFER;
1642b19e6e7bSMark Brown }
1643b19e6e7bSMark Brown 
1644f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1645f2ed6b07SMengdong Lin {
1646991454e1SKuninori Morimoto 	struct snd_soc_component *comp;
1647f2ed6b07SMengdong Lin 	int order;
1648f2ed6b07SMengdong Lin 	int ret;
1649f2ed6b07SMengdong Lin 
16501a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1651991454e1SKuninori Morimoto 		list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
1652f2ed6b07SMengdong Lin 			if (comp->driver->probe_order == order) {
1653f2ed6b07SMengdong Lin 				ret = soc_probe_component(card,	comp);
1654f2ed6b07SMengdong Lin 				if (ret < 0) {
1655f2ed6b07SMengdong Lin 					dev_err(card->dev,
1656f2ed6b07SMengdong Lin 						"ASoC: failed to probe aux component %s %d\n",
1657f2ed6b07SMengdong Lin 						comp->name, ret);
1658f2ed6b07SMengdong Lin 					return ret;
1659f2ed6b07SMengdong Lin 				}
1660f2ed6b07SMengdong Lin 			}
1661f2ed6b07SMengdong Lin 		}
1662f2ed6b07SMengdong Lin 	}
166365d9361fSLars-Peter Clausen 
166444c69bb1SLars-Peter Clausen 	return 0;
16653ca041edSSebastian Reichel }
16662eea392dSJarkko Nikula 
1667f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
166844c69bb1SLars-Peter Clausen {
1669f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1670f2ed6b07SMengdong Lin 	int order;
167144c69bb1SLars-Peter Clausen 
16721a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1673f2ed6b07SMengdong Lin 		list_for_each_entry_safe(comp, _comp,
1674991454e1SKuninori Morimoto 			&card->aux_comp_list, card_aux_list) {
16751a653aa4SKuninori Morimoto 
1676f2ed6b07SMengdong Lin 			if (comp->driver->remove_order == order) {
1677f2ed6b07SMengdong Lin 				soc_remove_component(comp);
1678991454e1SKuninori Morimoto 				/* remove it from the card's aux_comp_list */
1679991454e1SKuninori Morimoto 				list_del(&comp->card_aux_list);
16802eea392dSJarkko Nikula 			}
16815f3484acSLars-Peter Clausen 		}
16822eea392dSJarkko Nikula 	}
16832eea392dSJarkko Nikula }
16842eea392dSJarkko Nikula 
1685ce64c8b9SLars-Peter Clausen /**
1686ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1687ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1688ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1689ce64c8b9SLars-Peter Clausen  *
1690ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1691ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1692ce64c8b9SLars-Peter Clausen  *
1693ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1694ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1695ce64c8b9SLars-Peter Clausen  *
1696ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1697ce64c8b9SLars-Peter Clausen  */
1698ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1699ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1700ce64c8b9SLars-Peter Clausen {
1701ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
17020b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1703ce64c8b9SLars-Peter Clausen 	unsigned int i;
1704ce64c8b9SLars-Peter Clausen 	int ret;
1705ce64c8b9SLars-Peter Clausen 
17060b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1707ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1708ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1709ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1710ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1711ce64c8b9SLars-Peter Clausen 			return ret;
1712ce64c8b9SLars-Peter Clausen 		}
1713ce64c8b9SLars-Peter Clausen 	}
1714ce64c8b9SLars-Peter Clausen 
17152c7b696aSMarcel Ziswiler 	/*
17162c7b696aSMarcel Ziswiler 	 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
17172c7b696aSMarcel Ziswiler 	 * the component which has non_legacy_dai_naming is Codec
17182c7b696aSMarcel Ziswiler 	 */
1719999f7f5aSKuninori Morimoto 	if (cpu_dai->component->driver->non_legacy_dai_naming) {
1720ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1721ce64c8b9SLars-Peter Clausen 
1722ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1723ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1724ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1725ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1726ce64c8b9SLars-Peter Clausen 			break;
1727ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1728ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1729ce64c8b9SLars-Peter Clausen 			break;
1730ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1731ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1732ce64c8b9SLars-Peter Clausen 			break;
1733ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1734ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1735ce64c8b9SLars-Peter Clausen 			break;
1736ce64c8b9SLars-Peter Clausen 		}
1737ce64c8b9SLars-Peter Clausen 
1738ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1739ce64c8b9SLars-Peter Clausen 	}
1740ce64c8b9SLars-Peter Clausen 
1741ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1742ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1743ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1744ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1745ce64c8b9SLars-Peter Clausen 		return ret;
1746ce64c8b9SLars-Peter Clausen 	}
1747ce64c8b9SLars-Peter Clausen 
1748ce64c8b9SLars-Peter Clausen 	return 0;
1749ce64c8b9SLars-Peter Clausen }
1750ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1751ce64c8b9SLars-Peter Clausen 
17521f5a4535STakashi Iwai #ifdef CONFIG_DMI
17532c7b696aSMarcel Ziswiler /*
17542c7b696aSMarcel Ziswiler  * Trim special characters, and replace '-' with '_' since '-' is used to
1755345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1756345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1757345233d7SLiam Girdwood  */
1758345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1759345233d7SLiam Girdwood {
1760345233d7SLiam Girdwood 	int i, j = 0;
1761345233d7SLiam Girdwood 
1762345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1763345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1764345233d7SLiam Girdwood 		    || (name[i] == '_'))
1765345233d7SLiam Girdwood 			name[j++] = name[i];
1766345233d7SLiam Girdwood 		else if (name[i] == '-')
1767345233d7SLiam Girdwood 			name[j++] = '_';
1768345233d7SLiam Girdwood 	}
1769345233d7SLiam Girdwood 
1770345233d7SLiam Girdwood 	name[j] = '\0';
1771345233d7SLiam Girdwood }
1772345233d7SLiam Girdwood 
17732c7b696aSMarcel Ziswiler /*
17742c7b696aSMarcel Ziswiler  * Check if a DMI field is valid, i.e. not containing any string
177598faf436SMengdong Lin  * in the black list.
177698faf436SMengdong Lin  */
177798faf436SMengdong Lin static int is_dmi_valid(const char *field)
177898faf436SMengdong Lin {
177998faf436SMengdong Lin 	int i = 0;
178098faf436SMengdong Lin 
178198faf436SMengdong Lin 	while (dmi_blacklist[i]) {
178298faf436SMengdong Lin 		if (strstr(field, dmi_blacklist[i]))
178398faf436SMengdong Lin 			return 0;
178498faf436SMengdong Lin 		i++;
178546b5a4d2SWu Fengguang 	}
178698faf436SMengdong Lin 
178798faf436SMengdong Lin 	return 1;
178898faf436SMengdong Lin }
178998faf436SMengdong Lin 
1790345233d7SLiam Girdwood /**
1791345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
1792345233d7SLiam Girdwood  * @card: The card to register DMI names
1793345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
1794345233d7SLiam Girdwood  *
1795345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
1796345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
1797345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
1798345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
1799345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
1800345233d7SLiam Girdwood  * as the sound card long name, in the format of
1801345233d7SLiam Girdwood  * "vendor-product-version-board"
1802345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
1803345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
1804345233d7SLiam Girdwood  * (UCM) configurations for the card.
1805345233d7SLiam Girdwood  *
1806345233d7SLiam Girdwood  * Possible card long names may be:
1807345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
1808345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1809345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1810345233d7SLiam Girdwood  *
1811345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
1812345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
1813345233d7SLiam Girdwood  *
1814345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
1815345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
1816345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
1817345233d7SLiam Girdwood  * charactors like SPACE.
1818345233d7SLiam Girdwood  *
1819345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
1820345233d7SLiam Girdwood  */
1821345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1822345233d7SLiam Girdwood {
1823345233d7SLiam Girdwood 	const char *vendor, *product, *product_version, *board;
1824345233d7SLiam Girdwood 	size_t longname_buf_size = sizeof(card->snd_card->longname);
1825345233d7SLiam Girdwood 	size_t len;
1826345233d7SLiam Girdwood 
1827345233d7SLiam Girdwood 	if (card->long_name)
1828345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
1829345233d7SLiam Girdwood 
1830345233d7SLiam Girdwood 	/* make up dmi long name as: vendor.product.version.board */
1831345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
183298faf436SMengdong Lin 	if (!vendor || !is_dmi_valid(vendor)) {
1833345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1834345233d7SLiam Girdwood 		return 0;
1835345233d7SLiam Girdwood 	}
1836345233d7SLiam Girdwood 
1837345233d7SLiam Girdwood 	snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1838345233d7SLiam Girdwood 			 "%s", vendor);
1839345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
1840345233d7SLiam Girdwood 
1841345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
184298faf436SMengdong Lin 	if (product && is_dmi_valid(product)) {
1843345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1844345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1845345233d7SLiam Girdwood 			 longname_buf_size - len,
1846345233d7SLiam Girdwood 			 "-%s", product);
1847345233d7SLiam Girdwood 
1848345233d7SLiam Girdwood 		len++;	/* skip the separator "-" */
1849345233d7SLiam Girdwood 		if (len < longname_buf_size)
1850345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1851345233d7SLiam Girdwood 
18522c7b696aSMarcel Ziswiler 		/*
18532c7b696aSMarcel Ziswiler 		 * some vendors like Lenovo may only put a self-explanatory
1854345233d7SLiam Girdwood 		 * name in the product version field
1855345233d7SLiam Girdwood 		 */
1856345233d7SLiam Girdwood 		product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
185798faf436SMengdong Lin 		if (product_version && is_dmi_valid(product_version)) {
1858345233d7SLiam Girdwood 			len = strlen(card->dmi_longname);
1859345233d7SLiam Girdwood 			snprintf(card->dmi_longname + len,
1860345233d7SLiam Girdwood 				 longname_buf_size - len,
1861345233d7SLiam Girdwood 				 "-%s", product_version);
1862345233d7SLiam Girdwood 
1863345233d7SLiam Girdwood 			len++;
1864345233d7SLiam Girdwood 			if (len < longname_buf_size)
1865345233d7SLiam Girdwood 				cleanup_dmi_name(card->dmi_longname + len);
1866345233d7SLiam Girdwood 		}
1867345233d7SLiam Girdwood 	}
1868345233d7SLiam Girdwood 
1869345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
187098faf436SMengdong Lin 	if (board && is_dmi_valid(board)) {
1871345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1872345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1873345233d7SLiam Girdwood 			 longname_buf_size - len,
1874345233d7SLiam Girdwood 			 "-%s", board);
1875345233d7SLiam Girdwood 
1876345233d7SLiam Girdwood 		len++;
1877345233d7SLiam Girdwood 		if (len < longname_buf_size)
1878345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1879345233d7SLiam Girdwood 	} else if (!product) {
1880345233d7SLiam Girdwood 		/* fall back to using legacy name */
1881345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1882345233d7SLiam Girdwood 		return 0;
1883345233d7SLiam Girdwood 	}
1884345233d7SLiam Girdwood 
1885345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
1886345233d7SLiam Girdwood 	if (flavour) {
1887345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1888345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1889345233d7SLiam Girdwood 			 longname_buf_size - len,
1890345233d7SLiam Girdwood 			 "-%s", flavour);
1891345233d7SLiam Girdwood 
1892345233d7SLiam Girdwood 		len++;
1893345233d7SLiam Girdwood 		if (len < longname_buf_size)
1894345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1895345233d7SLiam Girdwood 	}
1896345233d7SLiam Girdwood 
1897345233d7SLiam Girdwood 	/* set the card long name */
1898345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
1899345233d7SLiam Girdwood 
1900345233d7SLiam Girdwood 	return 0;
1901345233d7SLiam Girdwood }
1902345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
19031f5a4535STakashi Iwai #endif /* CONFIG_DMI */
1904345233d7SLiam Girdwood 
1905a655de80SLiam Girdwood static void soc_check_tplg_fes(struct snd_soc_card *card)
1906a655de80SLiam Girdwood {
1907a655de80SLiam Girdwood 	struct snd_soc_component *component;
1908a655de80SLiam Girdwood 	const struct snd_soc_component_driver *comp_drv;
1909a655de80SLiam Girdwood 	struct snd_soc_dai_link *dai_link;
1910a655de80SLiam Girdwood 	int i;
1911a655de80SLiam Girdwood 
1912368dee94SKuninori Morimoto 	for_each_component(component) {
1913a655de80SLiam Girdwood 
1914a655de80SLiam Girdwood 		/* does this component override FEs ? */
1915a655de80SLiam Girdwood 		if (!component->driver->ignore_machine)
1916a655de80SLiam Girdwood 			continue;
1917a655de80SLiam Girdwood 
1918a655de80SLiam Girdwood 		/* for this machine ? */
1919a655de80SLiam Girdwood 		if (strcmp(component->driver->ignore_machine,
1920a655de80SLiam Girdwood 			   card->dev->driver->name))
1921a655de80SLiam Girdwood 			continue;
1922a655de80SLiam Girdwood 
1923a655de80SLiam Girdwood 		/* machine matches, so override the rtd data */
19247fe072b4SKuninori Morimoto 		for_each_card_prelinks(card, i, dai_link) {
1925a655de80SLiam Girdwood 
1926a655de80SLiam Girdwood 			/* ignore this FE */
1927a655de80SLiam Girdwood 			if (dai_link->dynamic) {
1928a655de80SLiam Girdwood 				dai_link->ignore = true;
1929a655de80SLiam Girdwood 				continue;
1930a655de80SLiam Girdwood 			}
1931a655de80SLiam Girdwood 
1932a655de80SLiam Girdwood 			dev_info(card->dev, "info: override FE DAI link %s\n",
1933a655de80SLiam Girdwood 				 card->dai_link[i].name);
1934a655de80SLiam Girdwood 
1935a655de80SLiam Girdwood 			/* override platform component */
1936daecf46eSKuninori Morimoto 			if (snd_soc_init_platform(card, dai_link) < 0) {
1937daecf46eSKuninori Morimoto 				dev_err(card->dev, "init platform error");
1938daecf46eSKuninori Morimoto 				continue;
1939daecf46eSKuninori Morimoto 			}
1940daecf46eSKuninori Morimoto 			dai_link->platform->name = component->name;
1941a655de80SLiam Girdwood 
1942a655de80SLiam Girdwood 			/* convert non BE into BE */
1943a655de80SLiam Girdwood 			dai_link->no_pcm = 1;
1944a655de80SLiam Girdwood 
1945a655de80SLiam Girdwood 			/* override any BE fixups */
1946a655de80SLiam Girdwood 			dai_link->be_hw_params_fixup =
1947a655de80SLiam Girdwood 				component->driver->be_hw_params_fixup;
1948a655de80SLiam Girdwood 
19492c7b696aSMarcel Ziswiler 			/*
19502c7b696aSMarcel Ziswiler 			 * most BE links don't set stream name, so set it to
1951a655de80SLiam Girdwood 			 * dai link name if it's NULL to help bind widgets.
1952a655de80SLiam Girdwood 			 */
1953a655de80SLiam Girdwood 			if (!dai_link->stream_name)
1954a655de80SLiam Girdwood 				dai_link->stream_name = dai_link->name;
1955a655de80SLiam Girdwood 		}
1956a655de80SLiam Girdwood 
1957a655de80SLiam Girdwood 		/* Inform userspace we are using alternate topology */
1958a655de80SLiam Girdwood 		if (component->driver->topology_name_prefix) {
1959a655de80SLiam Girdwood 
1960a655de80SLiam Girdwood 			/* topology shortname created? */
1961a655de80SLiam Girdwood 			if (!card->topology_shortname_created) {
1962a655de80SLiam Girdwood 				comp_drv = component->driver;
1963a655de80SLiam Girdwood 
1964a655de80SLiam Girdwood 				snprintf(card->topology_shortname, 32, "%s-%s",
1965a655de80SLiam Girdwood 					 comp_drv->topology_name_prefix,
1966a655de80SLiam Girdwood 					 card->name);
1967a655de80SLiam Girdwood 				card->topology_shortname_created = true;
1968a655de80SLiam Girdwood 			}
1969a655de80SLiam Girdwood 
1970a655de80SLiam Girdwood 			/* use topology shortname */
1971a655de80SLiam Girdwood 			card->name = card->topology_shortname;
1972a655de80SLiam Girdwood 		}
1973a655de80SLiam Girdwood 	}
1974a655de80SLiam Girdwood }
1975a655de80SLiam Girdwood 
1976b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1977f0fba2adSLiam Girdwood {
19781a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
197961b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
1980ce64c8b9SLars-Peter Clausen 	int ret, i, order;
198196dd3622SMark Brown 
198234e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
198301b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1984f0fba2adSLiam Girdwood 
1985a655de80SLiam Girdwood 	/* check whether any platform is ignore machine FE and using topology */
1986a655de80SLiam Girdwood 	soc_check_tplg_fes(card);
1987a655de80SLiam Girdwood 
1988b19e6e7bSMark Brown 	/* bind DAIs */
19897fe072b4SKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link) {
19907fe072b4SKuninori Morimoto 		ret = soc_bind_dai_link(card, dai_link);
1991b19e6e7bSMark Brown 		if (ret != 0)
1992b19e6e7bSMark Brown 			goto base_error;
1993db2a4165SFrank Mandarino 	}
1994db2a4165SFrank Mandarino 
199544c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
1996b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
199744c69bb1SLars-Peter Clausen 		ret = soc_bind_aux_dev(card, i);
1998b19e6e7bSMark Brown 		if (ret != 0)
1999b19e6e7bSMark Brown 			goto base_error;
2000db2a4165SFrank Mandarino 	}
2001db2a4165SFrank Mandarino 
2002f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
20037fe072b4SKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link)
20047fe072b4SKuninori Morimoto 		snd_soc_add_dai_link(card, dai_link);
2005f8f80361SMengdong Lin 
2006f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
2007102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2008f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
2009f0fba2adSLiam Girdwood 	if (ret < 0) {
201010e8aa9aSMichał Mirosław 		dev_err(card->dev,
201110e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
201210e8aa9aSMichał Mirosław 			card->name, ret);
2013b19e6e7bSMark Brown 		goto base_error;
2014db2a4165SFrank Mandarino 	}
2015db2a4165SFrank Mandarino 
20160757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
20170757d834SLars-Peter Clausen 
2018e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
2019e37a4970SMark Brown 	card->dapm.dev = card->dev;
2020e37a4970SMark Brown 	card->dapm.card = card;
2021e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
2022e37a4970SMark Brown 
2023d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
2024d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2025d5d1e0beSLars-Peter Clausen #endif
2026d5d1e0beSLars-Peter Clausen 
202788ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
20286ed25978SAndy Green 	/* deferred resume work */
20296308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
20301301a964SRandy Dunlap #endif
20316ed25978SAndy Green 
20329a841ebbSMark Brown 	if (card->dapm_widgets)
20339a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
20349a841ebbSMark Brown 					  card->num_dapm_widgets);
20359a841ebbSMark Brown 
2036f23e860eSNicolin Chen 	if (card->of_dapm_widgets)
2037f23e860eSNicolin Chen 		snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2038f23e860eSNicolin Chen 					  card->num_of_dapm_widgets);
2039f23e860eSNicolin Chen 
2040f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
2041f0fba2adSLiam Girdwood 	if (card->probe) {
2042e7361ec4SMark Brown 		ret = card->probe(card);
2043f0fba2adSLiam Girdwood 		if (ret < 0)
2044f0fba2adSLiam Girdwood 			goto card_probe_error;
2045f0fba2adSLiam Girdwood 	}
2046f0fba2adSLiam Girdwood 
204762ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
20481a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
2049bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
20501a497983SMengdong Lin 			ret = soc_probe_link_components(card, rtd, order);
205162ae68faSStephen Warren 			if (ret < 0) {
2052f110bfc7SLiam Girdwood 				dev_err(card->dev,
2053f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
2054f110bfc7SLiam Girdwood 					ret);
205562ae68faSStephen Warren 				goto probe_dai_err;
205662ae68faSStephen Warren 			}
205762ae68faSStephen Warren 		}
205862ae68faSStephen Warren 	}
205962ae68faSStephen Warren 
2060f2ed6b07SMengdong Lin 	/* probe auxiliary components */
2061f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
2062f2ed6b07SMengdong Lin 	if (ret < 0)
2063f2ed6b07SMengdong Lin 		goto probe_dai_err;
2064f2ed6b07SMengdong Lin 
20652c7b696aSMarcel Ziswiler 	/*
20662c7b696aSMarcel Ziswiler 	 * Find new DAI links added during probing components and bind them.
206761b0088bSMengdong Lin 	 * Components with topology may bring new DAIs and DAI links.
206861b0088bSMengdong Lin 	 */
206998061fdbSKuninori Morimoto 	for_each_card_links(card, dai_link) {
207061b0088bSMengdong Lin 		if (soc_is_dai_link_bound(card, dai_link))
207161b0088bSMengdong Lin 			continue;
207261b0088bSMengdong Lin 
207361b0088bSMengdong Lin 		ret = soc_init_dai_link(card, dai_link);
207461b0088bSMengdong Lin 		if (ret)
207561b0088bSMengdong Lin 			goto probe_dai_err;
207661b0088bSMengdong Lin 		ret = soc_bind_dai_link(card, dai_link);
207761b0088bSMengdong Lin 		if (ret)
207861b0088bSMengdong Lin 			goto probe_dai_err;
207961b0088bSMengdong Lin 	}
208061b0088bSMengdong Lin 
208162ae68faSStephen Warren 	/* probe all DAI links on this card */
20821a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
2083bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
20841a497983SMengdong Lin 			ret = soc_probe_link_dais(card, rtd, order);
2085fe3e78e0SMark Brown 			if (ret < 0) {
2086f110bfc7SLiam Girdwood 				dev_err(card->dev,
2087f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
2088f110bfc7SLiam Girdwood 					ret);
2089f0fba2adSLiam Girdwood 				goto probe_dai_err;
2090fe3e78e0SMark Brown 			}
2091fe3e78e0SMark Brown 		}
20920168bf0dSLiam Girdwood 	}
2093fe3e78e0SMark Brown 
2094888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
2095b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
2096888df395SMark Brown 
2097b7af1dafSMark Brown 	if (card->controls)
20982c7b696aSMarcel Ziswiler 		snd_soc_add_card_controls(card, card->controls,
20992c7b696aSMarcel Ziswiler 					  card->num_controls);
2100b7af1dafSMark Brown 
2101b8ad29deSMark Brown 	if (card->dapm_routes)
2102b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2103b8ad29deSMark Brown 					card->num_dapm_routes);
2104b8ad29deSMark Brown 
2105f23e860eSNicolin Chen 	if (card->of_dapm_routes)
2106f23e860eSNicolin Chen 		snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2107f23e860eSNicolin Chen 					card->num_of_dapm_routes);
210875d9ac46SMark Brown 
2109861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
2110861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
2111861886d3STakashi Iwai 
2112f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
2113fe3e78e0SMark Brown 		 "%s", card->name);
2114f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
211522de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
2116f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2117f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
2118f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2119f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
2120f0e8ed85SMark Brown 		case '_':
2121f0e8ed85SMark Brown 		case '-':
2122f0e8ed85SMark Brown 		case '\0':
2123f0e8ed85SMark Brown 			break;
2124f0e8ed85SMark Brown 		default:
2125f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
2126f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
2127f0e8ed85SMark Brown 			break;
2128f0e8ed85SMark Brown 		}
2129f0e8ed85SMark Brown 	}
2130fe3e78e0SMark Brown 
213128e9ad92SMark Brown 	if (card->late_probe) {
213228e9ad92SMark Brown 		ret = card->late_probe(card);
213328e9ad92SMark Brown 		if (ret < 0) {
2134f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
213528e9ad92SMark Brown 				card->name, ret);
213628e9ad92SMark Brown 			goto probe_aux_dev_err;
213728e9ad92SMark Brown 		}
213828e9ad92SMark Brown 	}
213928e9ad92SMark Brown 
2140824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
21418c193b8dSLars-Peter Clausen 
2142f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2143fe3e78e0SMark Brown 	if (ret < 0) {
2144f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2145f110bfc7SLiam Girdwood 				ret);
21466b3ed785SAxel Lin 		goto probe_aux_dev_err;
2147fe3e78e0SMark Brown 	}
2148fe3e78e0SMark Brown 
2149435c5e25SMark Brown 	card->instantiated = 1;
2150882eab6cSTzung-Bi Shih 	dapm_mark_endpoints_dirty(card);
21514f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2152f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
215334e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2154b19e6e7bSMark Brown 
2155b19e6e7bSMark Brown 	return 0;
2156db2a4165SFrank Mandarino 
21572eea392dSJarkko Nikula probe_aux_dev_err:
2158f2ed6b07SMengdong Lin 	soc_remove_aux_devices(card);
21592eea392dSJarkko Nikula 
2160f0fba2adSLiam Girdwood probe_dai_err:
21610671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
2162fe3e78e0SMark Brown 
2163f0fba2adSLiam Girdwood card_probe_error:
216487506549SMark Brown 	if (card->remove)
2165e7361ec4SMark Brown 		card->remove(card);
2166f0fba2adSLiam Girdwood 
21672210438bSLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
21680757d834SLars-Peter Clausen 	soc_cleanup_card_debugfs(card);
2169f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
2170f0fba2adSLiam Girdwood 
2171b19e6e7bSMark Brown base_error:
21721a497983SMengdong Lin 	soc_remove_pcm_runtimes(card);
2173f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
217434e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2175db2a4165SFrank Mandarino 
2176b19e6e7bSMark Brown 	return ret;
2177435c5e25SMark Brown }
2178435c5e25SMark Brown 
2179435c5e25SMark Brown /* probes a new socdev */
2180435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2181435c5e25SMark Brown {
2182f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2183435c5e25SMark Brown 
218470a7ca34SVinod Koul 	/*
218570a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
218670a7ca34SVinod Koul 	 * we should not be here in that case so ret error
218770a7ca34SVinod Koul 	 */
218870a7ca34SVinod Koul 	if (!card)
218970a7ca34SVinod Koul 		return -EINVAL;
219070a7ca34SVinod Koul 
2191fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2192f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2193fe4085e8SMark Brown 		 card->name);
2194fe4085e8SMark Brown 
2195435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2196435c5e25SMark Brown 	card->dev = &pdev->dev;
2197f0fba2adSLiam Girdwood 
219828d528c8SMark Brown 	return snd_soc_register_card(card);
2199435c5e25SMark Brown }
2200435c5e25SMark Brown 
2201b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
2202db2a4165SFrank Mandarino {
22031a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
2204db2a4165SFrank Mandarino 
2205f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
2206bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd)
220743829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
2208db2a4165SFrank Mandarino 
22094efda5f2STakashi Iwai 	/* free the ALSA card at first; this syncs with pending operations */
22104efda5f2STakashi Iwai 	snd_card_free(card->snd_card);
22114efda5f2STakashi Iwai 
2212f0fba2adSLiam Girdwood 	/* remove and free each DAI */
22130671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
22141a497983SMengdong Lin 	soc_remove_pcm_runtimes(card);
2215f0fba2adSLiam Girdwood 
2216f2ed6b07SMengdong Lin 	/* remove auxiliary devices */
2217f2ed6b07SMengdong Lin 	soc_remove_aux_devices(card);
2218f2ed6b07SMengdong Lin 
2219d1e81428SMark Brown 	snd_soc_dapm_free(&card->dapm);
2220a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
2221a6052154SJarkko Nikula 
2222f0fba2adSLiam Girdwood 	/* remove the card */
222387506549SMark Brown 	if (card->remove)
2224e7361ec4SMark Brown 		card->remove(card);
2225f0fba2adSLiam Girdwood 
2226b0e26485SVinod Koul 	return 0;
2227b2dfa62cSGuennadi Liakhovetski }
2228b0e26485SVinod Koul 
2229b0e26485SVinod Koul /* removes a socdev */
2230b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2231b0e26485SVinod Koul {
2232b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2233b0e26485SVinod Koul 
2234c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2235db2a4165SFrank Mandarino 	return 0;
2236db2a4165SFrank Mandarino }
2237db2a4165SFrank Mandarino 
22386f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
223951737470SMark Brown {
22406f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
22411a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
224251737470SMark Brown 
224351737470SMark Brown 	if (!card->instantiated)
2244416356fcSMark Brown 		return 0;
224551737470SMark Brown 
22462c7b696aSMarcel Ziswiler 	/*
22472c7b696aSMarcel Ziswiler 	 * Flush out pmdown_time work - we actually do want to run it
22482c7b696aSMarcel Ziswiler 	 * now, we're shutting down so no imminent restart.
22492c7b696aSMarcel Ziswiler 	 */
2250bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd)
225143829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
225251737470SMark Brown 
2253f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2254416356fcSMark Brown 
2255988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
2256bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
225788bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
22580b7990e3SKuninori Morimoto 		struct snd_soc_dai *codec_dai;
22591a497983SMengdong Lin 		int i;
226088bd870fSBenoit Cousson 
2261988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
22620b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
226388bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
226488bd870fSBenoit Cousson 		}
2265988e8cc4SNicolin Chen 	}
2266988e8cc4SNicolin Chen 
2267416356fcSMark Brown 	return 0;
226851737470SMark Brown }
22696f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
227051737470SMark Brown 
22716f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2272b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2273b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2274b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2275b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
22766f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2277b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2278416356fcSMark Brown };
2279deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2280416356fcSMark Brown 
2281db2a4165SFrank Mandarino /* ASoC platform driver */
2282db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2283db2a4165SFrank Mandarino 	.driver		= {
2284db2a4165SFrank Mandarino 		.name		= "soc-audio",
22856f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2286db2a4165SFrank Mandarino 	},
2287db2a4165SFrank Mandarino 	.probe		= soc_probe,
2288db2a4165SFrank Mandarino 	.remove		= soc_remove,
2289db2a4165SFrank Mandarino };
2290db2a4165SFrank Mandarino 
2291096e49d5SMark Brown /**
2292db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2293db2a4165SFrank Mandarino  * @_template: control template
2294db2a4165SFrank Mandarino  * @data: control private data
2295ac11a2b3SMark Brown  * @long_name: control long name
2296efb7ac3fSMark Brown  * @prefix: control name prefix
2297db2a4165SFrank Mandarino  *
2298db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2299db2a4165SFrank Mandarino  *
2300db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2301db2a4165SFrank Mandarino  */
2302db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
23033056557fSMark Brown 				  void *data, const char *long_name,
2304efb7ac3fSMark Brown 				  const char *prefix)
2305db2a4165SFrank Mandarino {
2306db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2307efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2308efb7ac3fSMark Brown 	char *name = NULL;
2309db2a4165SFrank Mandarino 
2310db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2311db2a4165SFrank Mandarino 	template.index = 0;
2312db2a4165SFrank Mandarino 
2313efb7ac3fSMark Brown 	if (!long_name)
2314efb7ac3fSMark Brown 		long_name = template.name;
2315efb7ac3fSMark Brown 
2316efb7ac3fSMark Brown 	if (prefix) {
23172b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2318efb7ac3fSMark Brown 		if (!name)
2319efb7ac3fSMark Brown 			return NULL;
2320efb7ac3fSMark Brown 
2321efb7ac3fSMark Brown 		template.name = name;
2322efb7ac3fSMark Brown 	} else {
2323efb7ac3fSMark Brown 		template.name = long_name;
2324efb7ac3fSMark Brown 	}
2325efb7ac3fSMark Brown 
2326efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2327efb7ac3fSMark Brown 
2328efb7ac3fSMark Brown 	kfree(name);
2329efb7ac3fSMark Brown 
2330efb7ac3fSMark Brown 	return kcontrol;
2331db2a4165SFrank Mandarino }
2332db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2333db2a4165SFrank Mandarino 
2334022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2335022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2336022658beSLiam Girdwood 	const char *prefix, void *data)
2337022658beSLiam Girdwood {
2338022658beSLiam Girdwood 	int err, i;
2339022658beSLiam Girdwood 
2340022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2341022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
23422c7b696aSMarcel Ziswiler 
2343022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2344022658beSLiam Girdwood 						     control->name, prefix));
2345022658beSLiam Girdwood 		if (err < 0) {
2346f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2347f110bfc7SLiam Girdwood 				control->name, err);
2348022658beSLiam Girdwood 			return err;
2349022658beSLiam Girdwood 		}
2350022658beSLiam Girdwood 	}
2351022658beSLiam Girdwood 
2352022658beSLiam Girdwood 	return 0;
2353022658beSLiam Girdwood }
2354022658beSLiam Girdwood 
23554fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
23564fefd698SDimitris Papastamos 					       const char *name)
23574fefd698SDimitris Papastamos {
23584fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
23594fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
23604fefd698SDimitris Papastamos 
23614fefd698SDimitris Papastamos 	if (unlikely(!name))
23624fefd698SDimitris Papastamos 		return NULL;
23634fefd698SDimitris Papastamos 
23644fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
23654fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
23664fefd698SDimitris Papastamos 			return kctl;
23674fefd698SDimitris Papastamos 	return NULL;
23684fefd698SDimitris Papastamos }
23694fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
23704fefd698SDimitris Papastamos 
2371db2a4165SFrank Mandarino /**
23720f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
23730f2780adSLars-Peter Clausen  *
23740f2780adSLars-Peter Clausen  * @component: Component to add controls to
23750f2780adSLars-Peter Clausen  * @controls: Array of controls to add
23760f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
23770f2780adSLars-Peter Clausen  *
23780f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
23790f2780adSLars-Peter Clausen  */
23800f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
23810f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
23820f2780adSLars-Peter Clausen {
23830f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
23840f2780adSLars-Peter Clausen 
23850f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
23860f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
23870f2780adSLars-Peter Clausen }
23880f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
23890f2780adSLars-Peter Clausen 
23900f2780adSLars-Peter Clausen /**
2391022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2392022658beSLiam Girdwood  * Convenience function to add a list of controls.
2393022658beSLiam Girdwood  *
2394022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2395022658beSLiam Girdwood  * @controls: array of controls to add
2396022658beSLiam Girdwood  * @num_controls: number of elements in the array
2397022658beSLiam Girdwood  *
2398022658beSLiam Girdwood  * Return 0 for success, else error.
2399022658beSLiam Girdwood  */
2400022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2401022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2402022658beSLiam Girdwood {
2403022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2404022658beSLiam Girdwood 
2405022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2406022658beSLiam Girdwood 			NULL, soc_card);
2407022658beSLiam Girdwood }
2408022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2409022658beSLiam Girdwood 
2410022658beSLiam Girdwood /**
2411022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2412022658beSLiam Girdwood  * Convienience function to add a list of controls.
2413022658beSLiam Girdwood  *
2414022658beSLiam Girdwood  * @dai: DAI to add controls to
2415022658beSLiam Girdwood  * @controls: array of controls to add
2416022658beSLiam Girdwood  * @num_controls: number of elements in the array
2417022658beSLiam Girdwood  *
2418022658beSLiam Girdwood  * Return 0 for success, else error.
2419022658beSLiam Girdwood  */
2420022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2421022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2422022658beSLiam Girdwood {
2423313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2424022658beSLiam Girdwood 
2425022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2426022658beSLiam Girdwood 			NULL, dai);
2427022658beSLiam Girdwood }
2428022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2429022658beSLiam Girdwood 
2430022658beSLiam Girdwood /**
24318c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
24328c6529dbSLiam Girdwood  * @dai: DAI
24338c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
24348c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
24358c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
24368c6529dbSLiam Girdwood  *
24378c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
24388c6529dbSLiam Girdwood  */
24398c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
24408c6529dbSLiam Girdwood 	unsigned int freq, int dir)
24418c6529dbSLiam Girdwood {
244246471925SKuninori Morimoto 	if (dai->driver->ops->set_sysclk)
2443f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
244471ccef0dSKuninori Morimoto 
244571ccef0dSKuninori Morimoto 	return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2446ec4ee52aSMark Brown 					    freq, dir);
24478c6529dbSLiam Girdwood }
24488c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
24498c6529dbSLiam Girdwood 
24508c6529dbSLiam Girdwood /**
245171ccef0dSKuninori Morimoto  * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
245271ccef0dSKuninori Morimoto  * @component: COMPONENT
245371ccef0dSKuninori Morimoto  * @clk_id: DAI specific clock ID
245471ccef0dSKuninori Morimoto  * @source: Source for the clock
245571ccef0dSKuninori Morimoto  * @freq: new clock frequency in Hz
245671ccef0dSKuninori Morimoto  * @dir: new clock direction - input/output.
245771ccef0dSKuninori Morimoto  *
245871ccef0dSKuninori Morimoto  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
245971ccef0dSKuninori Morimoto  */
24602c7b696aSMarcel Ziswiler int snd_soc_component_set_sysclk(struct snd_soc_component *component,
24612c7b696aSMarcel Ziswiler 				 int clk_id, int source, unsigned int freq,
24622c7b696aSMarcel Ziswiler 				 int dir)
246371ccef0dSKuninori Morimoto {
246471ccef0dSKuninori Morimoto 	if (component->driver->set_sysclk)
246571ccef0dSKuninori Morimoto 		return component->driver->set_sysclk(component, clk_id, source,
246671ccef0dSKuninori Morimoto 						 freq, dir);
246771ccef0dSKuninori Morimoto 
246871ccef0dSKuninori Morimoto 	return -ENOTSUPP;
246971ccef0dSKuninori Morimoto }
247071ccef0dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
247171ccef0dSKuninori Morimoto 
247271ccef0dSKuninori Morimoto /**
24738c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
24748c6529dbSLiam Girdwood  * @dai: DAI
2475ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
24768c6529dbSLiam Girdwood  * @div: new clock divisor.
24778c6529dbSLiam Girdwood  *
24788c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
24798c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
24808c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
24818c6529dbSLiam Girdwood  */
24828c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
24838c6529dbSLiam Girdwood 	int div_id, int div)
24848c6529dbSLiam Girdwood {
248546471925SKuninori Morimoto 	if (dai->driver->ops->set_clkdiv)
2486f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
24878c6529dbSLiam Girdwood 	else
24888c6529dbSLiam Girdwood 		return -EINVAL;
24898c6529dbSLiam Girdwood }
24908c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
24918c6529dbSLiam Girdwood 
24928c6529dbSLiam Girdwood /**
24938c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
24948c6529dbSLiam Girdwood  * @dai: DAI
24958c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
249685488037SMark Brown  * @source: DAI specific source for the PLL
24978c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
24988c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
24998c6529dbSLiam Girdwood  *
25008c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
25018c6529dbSLiam Girdwood  */
250285488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
250385488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
25048c6529dbSLiam Girdwood {
250546471925SKuninori Morimoto 	if (dai->driver->ops->set_pll)
2506f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
250785488037SMark Brown 					 freq_in, freq_out);
2508ef641e5dSKuninori Morimoto 
2509ef641e5dSKuninori Morimoto 	return snd_soc_component_set_pll(dai->component, pll_id, source,
2510ec4ee52aSMark Brown 					 freq_in, freq_out);
25118c6529dbSLiam Girdwood }
25128c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
25138c6529dbSLiam Girdwood 
2514ec4ee52aSMark Brown /*
2515ef641e5dSKuninori Morimoto  * snd_soc_component_set_pll - configure component PLL.
2516ef641e5dSKuninori Morimoto  * @component: COMPONENT
2517ef641e5dSKuninori Morimoto  * @pll_id: DAI specific PLL ID
2518ef641e5dSKuninori Morimoto  * @source: DAI specific source for the PLL
2519ef641e5dSKuninori Morimoto  * @freq_in: PLL input clock frequency in Hz
2520ef641e5dSKuninori Morimoto  * @freq_out: requested PLL output clock frequency in Hz
2521ef641e5dSKuninori Morimoto  *
2522ef641e5dSKuninori Morimoto  * Configures and enables PLL to generate output clock based on input clock.
2523ef641e5dSKuninori Morimoto  */
2524ef641e5dSKuninori Morimoto int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2525ef641e5dSKuninori Morimoto 			      int source, unsigned int freq_in,
2526ef641e5dSKuninori Morimoto 			      unsigned int freq_out)
2527ef641e5dSKuninori Morimoto {
2528ef641e5dSKuninori Morimoto 	if (component->driver->set_pll)
2529ef641e5dSKuninori Morimoto 		return component->driver->set_pll(component, pll_id, source,
2530ef641e5dSKuninori Morimoto 						  freq_in, freq_out);
2531ef641e5dSKuninori Morimoto 
2532ef641e5dSKuninori Morimoto 	return -EINVAL;
2533ef641e5dSKuninori Morimoto }
2534ef641e5dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2535ef641e5dSKuninori Morimoto 
25368c6529dbSLiam Girdwood /**
2537e54cf76bSLiam Girdwood  * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2538e54cf76bSLiam Girdwood  * @dai: DAI
2539231b86b1SMasanari Iida  * @ratio: Ratio of BCLK to Sample rate.
2540e54cf76bSLiam Girdwood  *
2541e54cf76bSLiam Girdwood  * Configures the DAI for a preset BCLK to sample rate ratio.
2542e54cf76bSLiam Girdwood  */
2543e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2544e54cf76bSLiam Girdwood {
254546471925SKuninori Morimoto 	if (dai->driver->ops->set_bclk_ratio)
2546e54cf76bSLiam Girdwood 		return dai->driver->ops->set_bclk_ratio(dai, ratio);
2547e54cf76bSLiam Girdwood 	else
2548e54cf76bSLiam Girdwood 		return -EINVAL;
2549e54cf76bSLiam Girdwood }
2550e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2551e54cf76bSLiam Girdwood 
2552e54cf76bSLiam Girdwood /**
25538c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
25548c6529dbSLiam Girdwood  * @dai: DAI
2555bb19ba2aSRandy Dunlap  * @fmt: SND_SOC_DAIFMT_* format value.
25568c6529dbSLiam Girdwood  *
25578c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
25588c6529dbSLiam Girdwood  */
25598c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
25608c6529dbSLiam Girdwood {
25615e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
25625e4ba569SShawn Guo 		return -ENOTSUPP;
25635e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
25648c6529dbSLiam Girdwood }
25658c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
25668c6529dbSLiam Girdwood 
25678c6529dbSLiam Girdwood /**
2568e5c21514SXiubo Li  * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
256989c67857SXiubo Li  * @slots: Number of slots in use.
257089c67857SXiubo Li  * @tx_mask: bitmask representing active TX slots.
257189c67857SXiubo Li  * @rx_mask: bitmask representing active RX slots.
257289c67857SXiubo Li  *
257389c67857SXiubo Li  * Generates the TDM tx and rx slot default masks for DAI.
257489c67857SXiubo Li  */
2575e5c21514SXiubo Li static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
257689c67857SXiubo Li 				       unsigned int *tx_mask,
257789c67857SXiubo Li 				       unsigned int *rx_mask)
257889c67857SXiubo Li {
257989c67857SXiubo Li 	if (*tx_mask || *rx_mask)
258089c67857SXiubo Li 		return 0;
258189c67857SXiubo Li 
258289c67857SXiubo Li 	if (!slots)
258389c67857SXiubo Li 		return -EINVAL;
258489c67857SXiubo Li 
258589c67857SXiubo Li 	*tx_mask = (1 << slots) - 1;
258689c67857SXiubo Li 	*rx_mask = (1 << slots) - 1;
258789c67857SXiubo Li 
258889c67857SXiubo Li 	return 0;
258989c67857SXiubo Li }
259089c67857SXiubo Li 
259189c67857SXiubo Li /**
2592e46c9366SLars-Peter Clausen  * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2593e46c9366SLars-Peter Clausen  * @dai: The DAI to configure
2594a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2595a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
25968c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2597a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
25988c6529dbSLiam Girdwood  *
2599e46c9366SLars-Peter Clausen  * This function configures the specified DAI for TDM operation. @slot contains
2600e46c9366SLars-Peter Clausen  * the total number of slots of the TDM stream and @slot_with the width of each
2601e46c9366SLars-Peter Clausen  * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2602e46c9366SLars-Peter Clausen  * active slots of the TDM stream for the specified DAI, i.e. which slots the
2603e46c9366SLars-Peter Clausen  * DAI should write to or read from. If a bit is set the corresponding slot is
2604e46c9366SLars-Peter Clausen  * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2605e46c9366SLars-Peter Clausen  * the first slot, bit 1 to the second slot and so on. The first active slot
2606e46c9366SLars-Peter Clausen  * maps to the first channel of the DAI, the second active slot to the second
2607e46c9366SLars-Peter Clausen  * channel and so on.
2608e46c9366SLars-Peter Clausen  *
2609e46c9366SLars-Peter Clausen  * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2610e46c9366SLars-Peter Clausen  * @rx_mask and @slot_width will be ignored.
2611e46c9366SLars-Peter Clausen  *
2612e46c9366SLars-Peter Clausen  * Returns 0 on success, a negative error code otherwise.
26138c6529dbSLiam Girdwood  */
26148c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2615a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
26168c6529dbSLiam Girdwood {
261746471925SKuninori Morimoto 	if (dai->driver->ops->xlate_tdm_slot_mask)
2618e5c21514SXiubo Li 		dai->driver->ops->xlate_tdm_slot_mask(slots,
261989c67857SXiubo Li 						&tx_mask, &rx_mask);
262089c67857SXiubo Li 	else
2621e5c21514SXiubo Li 		snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
262289c67857SXiubo Li 
262388bd870fSBenoit Cousson 	dai->tx_mask = tx_mask;
262488bd870fSBenoit Cousson 	dai->rx_mask = rx_mask;
262588bd870fSBenoit Cousson 
262646471925SKuninori Morimoto 	if (dai->driver->ops->set_tdm_slot)
2627f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2628a5479e38SDaniel Ribeiro 				slots, slot_width);
26298c6529dbSLiam Girdwood 	else
2630b2cbb6e1SXiubo Li 		return -ENOTSUPP;
26318c6529dbSLiam Girdwood }
26328c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
26338c6529dbSLiam Girdwood 
26348c6529dbSLiam Girdwood /**
2635472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2636472df3cbSBarry Song  * @dai: DAI
2637472df3cbSBarry Song  * @tx_num: how many TX channels
2638472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2639472df3cbSBarry Song  *           0~num-1 uses
2640472df3cbSBarry Song  * @rx_num: how many RX channels
2641472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2642472df3cbSBarry Song  *           0~num-1 uses
2643472df3cbSBarry Song  *
2644472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2645472df3cbSBarry Song  */
2646472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2647472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
2648472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
2649472df3cbSBarry Song {
265046471925SKuninori Morimoto 	if (dai->driver->ops->set_channel_map)
2651f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2652472df3cbSBarry Song 			rx_num, rx_slot);
2653472df3cbSBarry Song 	else
2654472df3cbSBarry Song 		return -EINVAL;
2655472df3cbSBarry Song }
2656472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2657472df3cbSBarry Song 
2658472df3cbSBarry Song /**
2659467b061fSSrinivas Kandagatla  * snd_soc_dai_get_channel_map - Get DAI audio channel map
2660467b061fSSrinivas Kandagatla  * @dai: DAI
2661467b061fSSrinivas Kandagatla  * @tx_num: how many TX channels
2662467b061fSSrinivas Kandagatla  * @tx_slot: pointer to an array which imply the TX slot number channel
2663467b061fSSrinivas Kandagatla  *           0~num-1 uses
2664467b061fSSrinivas Kandagatla  * @rx_num: how many RX channels
2665467b061fSSrinivas Kandagatla  * @rx_slot: pointer to an array which imply the RX slot number channel
2666467b061fSSrinivas Kandagatla  *           0~num-1 uses
2667467b061fSSrinivas Kandagatla  */
2668467b061fSSrinivas Kandagatla int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
2669467b061fSSrinivas Kandagatla 	unsigned int *tx_num, unsigned int *tx_slot,
2670467b061fSSrinivas Kandagatla 	unsigned int *rx_num, unsigned int *rx_slot)
2671467b061fSSrinivas Kandagatla {
2672467b061fSSrinivas Kandagatla 	if (dai->driver->ops->get_channel_map)
2673467b061fSSrinivas Kandagatla 		return dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
2674467b061fSSrinivas Kandagatla 			rx_num, rx_slot);
2675467b061fSSrinivas Kandagatla 	else
2676467b061fSSrinivas Kandagatla 		return -ENOTSUPP;
2677467b061fSSrinivas Kandagatla }
2678467b061fSSrinivas Kandagatla EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
2679467b061fSSrinivas Kandagatla 
2680467b061fSSrinivas Kandagatla /**
26818c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
26828c6529dbSLiam Girdwood  * @dai: DAI
26838c6529dbSLiam Girdwood  * @tristate: tristate enable
26848c6529dbSLiam Girdwood  *
26858c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
26868c6529dbSLiam Girdwood  */
26878c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
26888c6529dbSLiam Girdwood {
268946471925SKuninori Morimoto 	if (dai->driver->ops->set_tristate)
2690f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
26918c6529dbSLiam Girdwood 	else
26928c6529dbSLiam Girdwood 		return -EINVAL;
26938c6529dbSLiam Girdwood }
26948c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
26958c6529dbSLiam Girdwood 
26968c6529dbSLiam Girdwood /**
26978c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
26988c6529dbSLiam Girdwood  * @dai: DAI
26998c6529dbSLiam Girdwood  * @mute: mute enable
2700da18396fSMark Brown  * @direction: stream to mute
27018c6529dbSLiam Girdwood  *
27028c6529dbSLiam Girdwood  * Mutes the DAI DAC.
27038c6529dbSLiam Girdwood  */
2704da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2705da18396fSMark Brown 			     int direction)
27068c6529dbSLiam Girdwood {
2707da18396fSMark Brown 	if (dai->driver->ops->mute_stream)
2708da18396fSMark Brown 		return dai->driver->ops->mute_stream(dai, mute, direction);
2709da18396fSMark Brown 	else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2710da18396fSMark Brown 		 dai->driver->ops->digital_mute)
2711f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
27128c6529dbSLiam Girdwood 	else
271304570c62SMark Brown 		return -ENOTSUPP;
27148c6529dbSLiam Girdwood }
27158c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
27168c6529dbSLiam Girdwood 
2717e894efefSSrinivas Kandagatla static int snd_soc_bind_card(struct snd_soc_card *card)
2718e894efefSSrinivas Kandagatla {
2719e894efefSSrinivas Kandagatla 	struct snd_soc_pcm_runtime *rtd;
2720e894efefSSrinivas Kandagatla 	int ret;
2721e894efefSSrinivas Kandagatla 
2722e894efefSSrinivas Kandagatla 	ret = snd_soc_instantiate_card(card);
2723e894efefSSrinivas Kandagatla 	if (ret != 0)
2724e894efefSSrinivas Kandagatla 		return ret;
2725e894efefSSrinivas Kandagatla 
2726e894efefSSrinivas Kandagatla 	/* deactivate pins to sleep state */
2727bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
2728e894efefSSrinivas Kandagatla 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2729e894efefSSrinivas Kandagatla 		struct snd_soc_dai *codec_dai;
2730e894efefSSrinivas Kandagatla 		int j;
2731e894efefSSrinivas Kandagatla 
2732e894efefSSrinivas Kandagatla 		for_each_rtd_codec_dai(rtd, j, codec_dai) {
2733e894efefSSrinivas Kandagatla 			if (!codec_dai->active)
2734e894efefSSrinivas Kandagatla 				pinctrl_pm_select_sleep_state(codec_dai->dev);
2735e894efefSSrinivas Kandagatla 		}
2736e894efefSSrinivas Kandagatla 
2737e894efefSSrinivas Kandagatla 		if (!cpu_dai->active)
2738e894efefSSrinivas Kandagatla 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
2739e894efefSSrinivas Kandagatla 	}
2740e894efefSSrinivas Kandagatla 
2741e894efefSSrinivas Kandagatla 	return ret;
2742e894efefSSrinivas Kandagatla }
2743e894efefSSrinivas Kandagatla 
2744c5af3a2eSMark Brown /**
2745c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2746c5af3a2eSMark Brown  *
2747ac11a2b3SMark Brown  * @card: Card to register
2748c5af3a2eSMark Brown  *
2749c5af3a2eSMark Brown  */
275070a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2751c5af3a2eSMark Brown {
2752923c5e61SMengdong Lin 	int i, ret;
27537fe072b4SKuninori Morimoto 	struct snd_soc_dai_link *link;
2754f0fba2adSLiam Girdwood 
2755c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2756c5af3a2eSMark Brown 		return -EINVAL;
2757c5af3a2eSMark Brown 
2758*04eb1efcSRohit kumar 	mutex_lock(&client_mutex);
27597fe072b4SKuninori Morimoto 	for_each_card_prelinks(card, i, link) {
27605a504963SStephen Warren 
2761923c5e61SMengdong Lin 		ret = soc_init_dai_link(card, link);
276288bd870fSBenoit Cousson 		if (ret) {
2763923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: failed to init link %s\n",
2764923c5e61SMengdong Lin 				link->name);
2765*04eb1efcSRohit kumar 			mutex_unlock(&client_mutex);
276688bd870fSBenoit Cousson 			return ret;
276788bd870fSBenoit Cousson 		}
27685a504963SStephen Warren 	}
2769*04eb1efcSRohit kumar 	mutex_unlock(&client_mutex);
27705a504963SStephen Warren 
2771ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2772ed77cc12SMark Brown 
2773111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
2774111c6419SStephen Warren 
2775f8f80361SMengdong Lin 	INIT_LIST_HEAD(&card->dai_link_list);
2776f0fba2adSLiam Girdwood 
27771a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
27789115171aSMark Brown 	card->num_rtd = 0;
2779db2a4165SFrank Mandarino 
2780db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
27818a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2782db2a4165SFrank Mandarino 	card->instantiated = 0;
2783db2a4165SFrank Mandarino 	mutex_init(&card->mutex);
2784a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
2785db2a4165SFrank Mandarino 
2786e894efefSSrinivas Kandagatla 	return snd_soc_bind_card(card);
2787db2a4165SFrank Mandarino }
278870a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2789db2a4165SFrank Mandarino 
2790e894efefSSrinivas Kandagatla static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2791e894efefSSrinivas Kandagatla {
2792e894efefSSrinivas Kandagatla 	if (card->instantiated) {
2793e894efefSSrinivas Kandagatla 		card->instantiated = false;
2794e894efefSSrinivas Kandagatla 		snd_soc_dapm_shutdown(card);
2795e894efefSSrinivas Kandagatla 		soc_cleanup_card_resources(card);
2796e894efefSSrinivas Kandagatla 		if (!unregister)
2797e894efefSSrinivas Kandagatla 			list_add(&card->list, &unbind_card_list);
2798e894efefSSrinivas Kandagatla 	} else {
2799e894efefSSrinivas Kandagatla 		if (unregister)
2800e894efefSSrinivas Kandagatla 			list_del(&card->list);
2801e894efefSSrinivas Kandagatla 	}
2802e894efefSSrinivas Kandagatla }
2803e894efefSSrinivas Kandagatla 
2804db2a4165SFrank Mandarino /**
2805db2a4165SFrank Mandarino  * snd_soc_unregister_card - Unregister a card with the ASoC core
2806db2a4165SFrank Mandarino  *
2807db2a4165SFrank Mandarino  * @card: Card to unregister
2808db2a4165SFrank Mandarino  *
2809db2a4165SFrank Mandarino  */
281070a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2811db2a4165SFrank Mandarino {
2812e894efefSSrinivas Kandagatla 	snd_soc_unbind_card(card, true);
2813f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2814db2a4165SFrank Mandarino 
2815db2a4165SFrank Mandarino 	return 0;
2816db2a4165SFrank Mandarino }
281770a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2818db2a4165SFrank Mandarino 
2819db2a4165SFrank Mandarino /*
2820db2a4165SFrank Mandarino  * Simplify DAI link configuration by removing ".-1" from device names
2821db2a4165SFrank Mandarino  * and sanitizing names.
2822db2a4165SFrank Mandarino  */
28230b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2824db2a4165SFrank Mandarino {
2825db2a4165SFrank Mandarino 	char *found, name[NAME_SIZE];
2826db2a4165SFrank Mandarino 	int id1, id2;
2827db2a4165SFrank Mandarino 
2828db2a4165SFrank Mandarino 	if (dev_name(dev) == NULL)
2829db2a4165SFrank Mandarino 		return NULL;
2830db2a4165SFrank Mandarino 
283158818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2832db2a4165SFrank Mandarino 
2833db2a4165SFrank Mandarino 	/* are we a "%s.%d" name (platform and SPI components) */
2834c5af3a2eSMark Brown 	found = strstr(name, dev->driver->name);
2835c5af3a2eSMark Brown 	if (found) {
2836c5af3a2eSMark Brown 		/* get ID */
2837c5af3a2eSMark Brown 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2838c5af3a2eSMark Brown 
2839c5af3a2eSMark Brown 			/* discard ID from name if ID == -1 */
2840c5af3a2eSMark Brown 			if (*id == -1)
2841c5af3a2eSMark Brown 				found[strlen(dev->driver->name)] = '\0';
2842c5af3a2eSMark Brown 		}
2843c5af3a2eSMark Brown 
2844c5af3a2eSMark Brown 	} else {
2845c5af3a2eSMark Brown 		/* I2C component devices are named "bus-addr" */
2846c5af3a2eSMark Brown 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2847c5af3a2eSMark Brown 			char tmp[NAME_SIZE];
2848c5af3a2eSMark Brown 
2849c5af3a2eSMark Brown 			/* create unique ID number from I2C addr and bus */
2850c5af3a2eSMark Brown 			*id = ((id1 & 0xffff) << 16) + id2;
2851c5af3a2eSMark Brown 
2852c5af3a2eSMark Brown 			/* sanitize component name for DAI link creation */
28532c7b696aSMarcel Ziswiler 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
28542c7b696aSMarcel Ziswiler 				 name);
285558818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2856c5af3a2eSMark Brown 		} else
2857c5af3a2eSMark Brown 			*id = 0;
2858c5af3a2eSMark Brown 	}
2859c5af3a2eSMark Brown 
2860c5af3a2eSMark Brown 	return kstrdup(name, GFP_KERNEL);
2861c5af3a2eSMark Brown }
2862c5af3a2eSMark Brown 
2863c5af3a2eSMark Brown /*
2864c5af3a2eSMark Brown  * Simplify DAI link naming for single devices with multiple DAIs by removing
2865c5af3a2eSMark Brown  * any ".-1" and using the DAI name (instead of device name).
2866c5af3a2eSMark Brown  */
2867c5af3a2eSMark Brown static inline char *fmt_multiple_name(struct device *dev,
2868c5af3a2eSMark Brown 		struct snd_soc_dai_driver *dai_drv)
2869c5af3a2eSMark Brown {
2870c5af3a2eSMark Brown 	if (dai_drv->name == NULL) {
287110e8aa9aSMichał Mirosław 		dev_err(dev,
287210e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
287310e8aa9aSMichał Mirosław 			dev_name(dev));
2874c5af3a2eSMark Brown 		return NULL;
2875c5af3a2eSMark Brown 	}
2876c5af3a2eSMark Brown 
2877c5af3a2eSMark Brown 	return kstrdup(dai_drv->name, GFP_KERNEL);
2878c5af3a2eSMark Brown }
2879c5af3a2eSMark Brown 
2880c5af3a2eSMark Brown /**
288132c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
28829115171aSMark Brown  *
288332c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
28849115171aSMark Brown  */
288532c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
28869115171aSMark Brown {
28875c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
28889115171aSMark Brown 
288915a0c645SKuninori Morimoto 	for_each_component_dais_safe(component, dai, _dai) {
289032c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
289132c9ba54SLars-Peter Clausen 			dai->name);
28929115171aSMark Brown 		list_del(&dai->list);
2893f0fba2adSLiam Girdwood 		kfree(dai->name);
2894f0fba2adSLiam Girdwood 		kfree(dai);
28959115171aSMark Brown 	}
289632c9ba54SLars-Peter Clausen }
28979115171aSMark Brown 
28985e4fb372SMengdong Lin /* Create a DAI and add it to the component's DAI list */
28995e4fb372SMengdong Lin static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
29005e4fb372SMengdong Lin 	struct snd_soc_dai_driver *dai_drv,
29015e4fb372SMengdong Lin 	bool legacy_dai_naming)
29025e4fb372SMengdong Lin {
29035e4fb372SMengdong Lin 	struct device *dev = component->dev;
29045e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
29055e4fb372SMengdong Lin 
29065e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
29075e4fb372SMengdong Lin 
29085e4fb372SMengdong Lin 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
29095e4fb372SMengdong Lin 	if (dai == NULL)
29105e4fb372SMengdong Lin 		return NULL;
29115e4fb372SMengdong Lin 
29125e4fb372SMengdong Lin 	/*
29135e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
29145e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
29155e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
29165e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
29175e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
29185e4fb372SMengdong Lin 	 * component-less anymore.
29195e4fb372SMengdong Lin 	 */
29205e4fb372SMengdong Lin 	if (legacy_dai_naming &&
29215e4fb372SMengdong Lin 	    (dai_drv->id == 0 || dai_drv->name == NULL)) {
29225e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
29235e4fb372SMengdong Lin 	} else {
29245e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
29255e4fb372SMengdong Lin 		if (dai_drv->id)
29265e4fb372SMengdong Lin 			dai->id = dai_drv->id;
29275e4fb372SMengdong Lin 		else
29285e4fb372SMengdong Lin 			dai->id = component->num_dai;
29295e4fb372SMengdong Lin 	}
29305e4fb372SMengdong Lin 	if (dai->name == NULL) {
29315e4fb372SMengdong Lin 		kfree(dai);
29325e4fb372SMengdong Lin 		return NULL;
29335e4fb372SMengdong Lin 	}
29345e4fb372SMengdong Lin 
29355e4fb372SMengdong Lin 	dai->component = component;
29365e4fb372SMengdong Lin 	dai->dev = dev;
29375e4fb372SMengdong Lin 	dai->driver = dai_drv;
29385e4fb372SMengdong Lin 	if (!dai->driver->ops)
29395e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
29405e4fb372SMengdong Lin 
294115a0c645SKuninori Morimoto 	/* see for_each_component_dais */
294258bf4179SKuninori Morimoto 	list_add_tail(&dai->list, &component->dai_list);
29435e4fb372SMengdong Lin 	component->num_dai++;
29445e4fb372SMengdong Lin 
29455e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
29465e4fb372SMengdong Lin 	return dai;
29475e4fb372SMengdong Lin }
29485e4fb372SMengdong Lin 
29499115171aSMark Brown /**
295032c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
29519115171aSMark Brown  *
29526106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
29536106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
2954ac11a2b3SMark Brown  * @count: Number of DAIs
29559115171aSMark Brown  */
295632c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
29572c7b696aSMarcel Ziswiler 				 struct snd_soc_dai_driver *dai_drv,
29582c7b696aSMarcel Ziswiler 				 size_t count)
29599115171aSMark Brown {
29606106d129SLars-Peter Clausen 	struct device *dev = component->dev;
2961f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
296232c9ba54SLars-Peter Clausen 	unsigned int i;
296332c9ba54SLars-Peter Clausen 	int ret;
2964f0fba2adSLiam Girdwood 
29655b5e0928SAlexey Dobriyan 	dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
29669115171aSMark Brown 
29679115171aSMark Brown 	for (i = 0; i < count; i++) {
2968f0fba2adSLiam Girdwood 
29692c7b696aSMarcel Ziswiler 		dai = soc_add_dai(component, dai_drv + i, count == 1 &&
29702c7b696aSMarcel Ziswiler 				  !component->driver->non_legacy_dai_naming);
2971c46e0079SAxel Lin 		if (dai == NULL) {
2972c46e0079SAxel Lin 			ret = -ENOMEM;
2973c46e0079SAxel Lin 			goto err;
2974c46e0079SAxel Lin 		}
2975f0fba2adSLiam Girdwood 	}
2976f0fba2adSLiam Girdwood 
29779115171aSMark Brown 	return 0;
29789115171aSMark Brown 
29799115171aSMark Brown err:
298032c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
29819115171aSMark Brown 
29829115171aSMark Brown 	return ret;
29839115171aSMark Brown }
29849115171aSMark Brown 
298568003e6cSMengdong Lin /**
298668003e6cSMengdong Lin  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
298768003e6cSMengdong Lin  *
298868003e6cSMengdong Lin  * @component: The component the DAIs are registered for
298968003e6cSMengdong Lin  * @dai_drv: DAI driver to use for the DAI
299068003e6cSMengdong Lin  *
299168003e6cSMengdong Lin  * Topology can use this API to register DAIs when probing a component.
299268003e6cSMengdong Lin  * These DAIs's widgets will be freed in the card cleanup and the DAIs
299368003e6cSMengdong Lin  * will be freed in the component cleanup.
299468003e6cSMengdong Lin  */
299568003e6cSMengdong Lin int snd_soc_register_dai(struct snd_soc_component *component,
299668003e6cSMengdong Lin 	struct snd_soc_dai_driver *dai_drv)
299768003e6cSMengdong Lin {
299868003e6cSMengdong Lin 	struct snd_soc_dapm_context *dapm =
299968003e6cSMengdong Lin 		snd_soc_component_get_dapm(component);
300068003e6cSMengdong Lin 	struct snd_soc_dai *dai;
300168003e6cSMengdong Lin 	int ret;
300268003e6cSMengdong Lin 
300368003e6cSMengdong Lin 	if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
300468003e6cSMengdong Lin 		dev_err(component->dev, "Invalid dai type %d\n",
300568003e6cSMengdong Lin 			dai_drv->dobj.type);
300668003e6cSMengdong Lin 		return -EINVAL;
300768003e6cSMengdong Lin 	}
300868003e6cSMengdong Lin 
300968003e6cSMengdong Lin 	lockdep_assert_held(&client_mutex);
301068003e6cSMengdong Lin 	dai = soc_add_dai(component, dai_drv, false);
301168003e6cSMengdong Lin 	if (!dai)
301268003e6cSMengdong Lin 		return -ENOMEM;
301368003e6cSMengdong Lin 
30142c7b696aSMarcel Ziswiler 	/*
30152c7b696aSMarcel Ziswiler 	 * Create the DAI widgets here. After adding DAIs, topology may
301668003e6cSMengdong Lin 	 * also add routes that need these widgets as source or sink.
301768003e6cSMengdong Lin 	 */
301868003e6cSMengdong Lin 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
301968003e6cSMengdong Lin 	if (ret != 0) {
302068003e6cSMengdong Lin 		dev_err(component->dev,
302168003e6cSMengdong Lin 			"Failed to create DAI widgets %d\n", ret);
302268003e6cSMengdong Lin 	}
302368003e6cSMengdong Lin 
302468003e6cSMengdong Lin 	return ret;
302568003e6cSMengdong Lin }
302668003e6cSMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_register_dai);
302768003e6cSMengdong Lin 
302814e8bdebSLars-Peter Clausen static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
302914e8bdebSLars-Peter Clausen 	enum snd_soc_dapm_type type, int subseq)
3030d191bd8dSKuninori Morimoto {
303114e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
3032d191bd8dSKuninori Morimoto 
303314e8bdebSLars-Peter Clausen 	component->driver->seq_notifier(component, type, subseq);
303414e8bdebSLars-Peter Clausen }
3035d191bd8dSKuninori Morimoto 
303614e8bdebSLars-Peter Clausen static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
303714e8bdebSLars-Peter Clausen 	int event)
303814e8bdebSLars-Peter Clausen {
303914e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
304014e8bdebSLars-Peter Clausen 
304114e8bdebSLars-Peter Clausen 	return component->driver->stream_event(component, event);
304214e8bdebSLars-Peter Clausen }
304314e8bdebSLars-Peter Clausen 
30447ba236ceSKuninori Morimoto static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
30457ba236ceSKuninori Morimoto 					enum snd_soc_bias_level level)
30467ba236ceSKuninori Morimoto {
30477ba236ceSKuninori Morimoto 	struct snd_soc_component *component = dapm->component;
30487ba236ceSKuninori Morimoto 
30497ba236ceSKuninori Morimoto 	return component->driver->set_bias_level(component, level);
30507ba236ceSKuninori Morimoto }
30517ba236ceSKuninori Morimoto 
3052bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
3053bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
3054d191bd8dSKuninori Morimoto {
3055ce0fc93aSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
3056ce0fc93aSLars-Peter Clausen 
3057bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
3058bb13109dSLars-Peter Clausen 	if (!component->name) {
3059bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
3060d191bd8dSKuninori Morimoto 		return -ENOMEM;
3061d191bd8dSKuninori Morimoto 	}
3062d191bd8dSKuninori Morimoto 
3063bb13109dSLars-Peter Clausen 	component->dev = dev;
3064bb13109dSLars-Peter Clausen 	component->driver = driver;
3065e2c330b9SLars-Peter Clausen 
306688c27465SKuninori Morimoto 	dapm = snd_soc_component_get_dapm(component);
3067ce0fc93aSLars-Peter Clausen 	dapm->dev = dev;
3068ce0fc93aSLars-Peter Clausen 	dapm->component = component;
3069ce0fc93aSLars-Peter Clausen 	dapm->bias_level = SND_SOC_BIAS_OFF;
30707ba236ceSKuninori Morimoto 	dapm->idle_bias_off = !driver->idle_bias_on;
30717ba236ceSKuninori Morimoto 	dapm->suspend_bias_off = driver->suspend_bias_off;
307214e8bdebSLars-Peter Clausen 	if (driver->seq_notifier)
307314e8bdebSLars-Peter Clausen 		dapm->seq_notifier = snd_soc_component_seq_notifier;
307414e8bdebSLars-Peter Clausen 	if (driver->stream_event)
307514e8bdebSLars-Peter Clausen 		dapm->stream_event = snd_soc_component_stream_event;
30767ba236ceSKuninori Morimoto 	if (driver->set_bias_level)
30777ba236ceSKuninori Morimoto 		dapm->set_bias_level = snd_soc_component_set_bias_level;
3078ce0fc93aSLars-Peter Clausen 
3079bb13109dSLars-Peter Clausen 	INIT_LIST_HEAD(&component->dai_list);
3080bb13109dSLars-Peter Clausen 	mutex_init(&component->io_mutex);
3081bb13109dSLars-Peter Clausen 
3082bb13109dSLars-Peter Clausen 	return 0;
3083d191bd8dSKuninori Morimoto }
3084d191bd8dSKuninori Morimoto 
308520feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
3086886f5692SLars-Peter Clausen {
3087886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
308820feb881SLars-Peter Clausen 
3089886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
3090886f5692SLars-Peter Clausen 	if (val_bytes > 0)
3091886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
3092886f5692SLars-Peter Clausen }
309320feb881SLars-Peter Clausen 
3094e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
3095e874bf5fSLars-Peter Clausen 
309620feb881SLars-Peter Clausen /**
30972c7b696aSMarcel Ziswiler  * snd_soc_component_init_regmap() - Initialize regmap instance for the
30982c7b696aSMarcel Ziswiler  *                                   component
309920feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
310020feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
310120feb881SLars-Peter Clausen  *
310220feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
310320feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
310420feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
310520feb881SLars-Peter Clausen  * before the first IO attempt of the component.
310620feb881SLars-Peter Clausen  */
310720feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
310820feb881SLars-Peter Clausen 	struct regmap *regmap)
310920feb881SLars-Peter Clausen {
311020feb881SLars-Peter Clausen 	component->regmap = regmap;
311120feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
3112886f5692SLars-Peter Clausen }
311320feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
311420feb881SLars-Peter Clausen 
311520feb881SLars-Peter Clausen /**
31162c7b696aSMarcel Ziswiler  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
31172c7b696aSMarcel Ziswiler  *                                   component
311820feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
311920feb881SLars-Peter Clausen  *
312020feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
312120feb881SLars-Peter Clausen  * removes the regmap instance from the component.
312220feb881SLars-Peter Clausen  *
312320feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
312420feb881SLars-Peter Clausen  * to initialize the regmap instance.
312520feb881SLars-Peter Clausen  */
312620feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
312720feb881SLars-Peter Clausen {
312820feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
312920feb881SLars-Peter Clausen 	component->regmap = NULL;
313020feb881SLars-Peter Clausen }
313120feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3132886f5692SLars-Peter Clausen 
3133e874bf5fSLars-Peter Clausen #endif
3134d191bd8dSKuninori Morimoto 
3135359c71eeSKuninori Morimoto static void snd_soc_component_add(struct snd_soc_component *component)
3136bb13109dSLars-Peter Clausen {
3137359c71eeSKuninori Morimoto 	mutex_lock(&client_mutex);
3138359c71eeSKuninori Morimoto 
3139999f7f5aSKuninori Morimoto 	if (!component->driver->write && !component->driver->read) {
314020feb881SLars-Peter Clausen 		if (!component->regmap)
31412c7b696aSMarcel Ziswiler 			component->regmap = dev_get_regmap(component->dev,
31422c7b696aSMarcel Ziswiler 							   NULL);
314320feb881SLars-Peter Clausen 		if (component->regmap)
314420feb881SLars-Peter Clausen 			snd_soc_component_setup_regmap(component);
314520feb881SLars-Peter Clausen 	}
3146886f5692SLars-Peter Clausen 
3147368dee94SKuninori Morimoto 	/* see for_each_component */
3148bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
31498a978234SLiam Girdwood 	INIT_LIST_HEAD(&component->dobj_list);
3150d191bd8dSKuninori Morimoto 
3151d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
3152bb13109dSLars-Peter Clausen }
3153d191bd8dSKuninori Morimoto 
3154bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
3155bb13109dSLars-Peter Clausen {
3156bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
3157bb13109dSLars-Peter Clausen 	kfree(component->name);
3158bb13109dSLars-Peter Clausen }
3159d191bd8dSKuninori Morimoto 
3160bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3161bb13109dSLars-Peter Clausen {
3162c12c1aadSKuninori Morimoto 	struct snd_soc_card *card = component->card;
3163c12c1aadSKuninori Morimoto 
3164c12c1aadSKuninori Morimoto 	if (card)
3165e894efefSSrinivas Kandagatla 		snd_soc_unbind_card(card, false);
3166c12c1aadSKuninori Morimoto 
3167bb13109dSLars-Peter Clausen 	list_del(&component->list);
3168bb13109dSLars-Peter Clausen }
3169d191bd8dSKuninori Morimoto 
3170273d778eSKuninori Morimoto #define ENDIANNESS_MAP(name) \
3171273d778eSKuninori Morimoto 	(SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3172273d778eSKuninori Morimoto static u64 endianness_format_map[] = {
3173273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S16_),
3174273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U16_),
3175273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_),
3176273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_),
3177273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S32_),
3178273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U32_),
3179273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_3),
3180273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_3),
3181273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S20_3),
3182273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U20_3),
3183273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S18_3),
3184273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U18_3),
3185273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT_),
3186273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT64_),
3187273d778eSKuninori Morimoto 	ENDIANNESS_MAP(IEC958_SUBFRAME_),
3188273d778eSKuninori Morimoto };
3189273d778eSKuninori Morimoto 
3190273d778eSKuninori Morimoto /*
3191273d778eSKuninori Morimoto  * Fix up the DAI formats for endianness: codecs don't actually see
3192273d778eSKuninori Morimoto  * the endianness of the data but we're using the CPU format
3193273d778eSKuninori Morimoto  * definitions which do need to include endianness so we ensure that
3194273d778eSKuninori Morimoto  * codec DAIs always have both big and little endian variants set.
3195273d778eSKuninori Morimoto  */
3196273d778eSKuninori Morimoto static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3197273d778eSKuninori Morimoto {
3198273d778eSKuninori Morimoto 	int i;
3199273d778eSKuninori Morimoto 
3200273d778eSKuninori Morimoto 	for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3201273d778eSKuninori Morimoto 		if (stream->formats & endianness_format_map[i])
3202273d778eSKuninori Morimoto 			stream->formats |= endianness_format_map[i];
3203273d778eSKuninori Morimoto }
3204273d778eSKuninori Morimoto 
3205e894efefSSrinivas Kandagatla static void snd_soc_try_rebind_card(void)
3206e894efefSSrinivas Kandagatla {
3207e894efefSSrinivas Kandagatla 	struct snd_soc_card *card, *c;
3208e894efefSSrinivas Kandagatla 
3209e894efefSSrinivas Kandagatla 	if (!list_empty(&unbind_card_list)) {
3210e894efefSSrinivas Kandagatla 		list_for_each_entry_safe(card, c, &unbind_card_list, list) {
3211e894efefSSrinivas Kandagatla 			if (!snd_soc_bind_card(card))
3212e894efefSSrinivas Kandagatla 				list_del(&card->list);
3213e894efefSSrinivas Kandagatla 		}
3214e894efefSSrinivas Kandagatla 	}
3215e894efefSSrinivas Kandagatla }
3216e894efefSSrinivas Kandagatla 
3217e0dac41bSKuninori Morimoto int snd_soc_add_component(struct device *dev,
3218e0dac41bSKuninori Morimoto 			struct snd_soc_component *component,
3219cf9e829eSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
3220d191bd8dSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
3221d191bd8dSKuninori Morimoto 			int num_dai)
3222d191bd8dSKuninori Morimoto {
3223bb13109dSLars-Peter Clausen 	int ret;
3224273d778eSKuninori Morimoto 	int i;
3225d191bd8dSKuninori Morimoto 
3226cf9e829eSKuninori Morimoto 	ret = snd_soc_component_initialize(component, component_driver, dev);
3227bb13109dSLars-Peter Clausen 	if (ret)
3228bb13109dSLars-Peter Clausen 		goto err_free;
3229bb13109dSLars-Peter Clausen 
3230273d778eSKuninori Morimoto 	if (component_driver->endianness) {
3231273d778eSKuninori Morimoto 		for (i = 0; i < num_dai; i++) {
3232273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].playback);
3233273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].capture);
3234273d778eSKuninori Morimoto 		}
3235273d778eSKuninori Morimoto 	}
3236273d778eSKuninori Morimoto 
32370e7b25c6SKuninori Morimoto 	ret = snd_soc_register_dais(component, dai_drv, num_dai);
3238bb13109dSLars-Peter Clausen 	if (ret < 0) {
3239f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3240bb13109dSLars-Peter Clausen 		goto err_cleanup;
3241bb13109dSLars-Peter Clausen 	}
3242bb13109dSLars-Peter Clausen 
3243cf9e829eSKuninori Morimoto 	snd_soc_component_add(component);
3244e894efefSSrinivas Kandagatla 	snd_soc_try_rebind_card();
3245bb13109dSLars-Peter Clausen 
3246bb13109dSLars-Peter Clausen 	return 0;
3247bb13109dSLars-Peter Clausen 
3248bb13109dSLars-Peter Clausen err_cleanup:
3249cf9e829eSKuninori Morimoto 	snd_soc_component_cleanup(component);
3250bb13109dSLars-Peter Clausen err_free:
3251bb13109dSLars-Peter Clausen 	return ret;
3252d191bd8dSKuninori Morimoto }
3253e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component);
3254e0dac41bSKuninori Morimoto 
3255e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev,
3256e0dac41bSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
3257e0dac41bSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
3258e0dac41bSKuninori Morimoto 			int num_dai)
3259e0dac41bSKuninori Morimoto {
3260e0dac41bSKuninori Morimoto 	struct snd_soc_component *component;
3261e0dac41bSKuninori Morimoto 
32627ecbd6a9SKuninori Morimoto 	component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
326308e61d03SKuninori Morimoto 	if (!component)
3264e0dac41bSKuninori Morimoto 		return -ENOMEM;
3265e0dac41bSKuninori Morimoto 
3266e0dac41bSKuninori Morimoto 	return snd_soc_add_component(dev, component, component_driver,
3267e0dac41bSKuninori Morimoto 				     dai_drv, num_dai);
3268e0dac41bSKuninori Morimoto }
3269d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
3270d191bd8dSKuninori Morimoto 
3271d191bd8dSKuninori Morimoto /**
32722eccea8cSKuninori Morimoto  * snd_soc_unregister_component - Unregister all related component
32732eccea8cSKuninori Morimoto  * from the ASoC core
3274d191bd8dSKuninori Morimoto  *
3275628536eaSJonathan Corbet  * @dev: The device to unregister
3276d191bd8dSKuninori Morimoto  */
32772eccea8cSKuninori Morimoto static int __snd_soc_unregister_component(struct device *dev)
3278d191bd8dSKuninori Morimoto {
3279cf9e829eSKuninori Morimoto 	struct snd_soc_component *component;
328021a03528SKuninori Morimoto 	int found = 0;
3281d191bd8dSKuninori Morimoto 
328234e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
3283368dee94SKuninori Morimoto 	for_each_component(component) {
3284999f7f5aSKuninori Morimoto 		if (dev != component->dev)
328521a03528SKuninori Morimoto 			continue;
3286d191bd8dSKuninori Morimoto 
32872c7b696aSMarcel Ziswiler 		snd_soc_tplg_component_remove(component,
32882c7b696aSMarcel Ziswiler 					      SND_SOC_TPLG_INDEX_ALL);
3289cf9e829eSKuninori Morimoto 		snd_soc_component_del_unlocked(component);
329021a03528SKuninori Morimoto 		found = 1;
329121a03528SKuninori Morimoto 		break;
3292d191bd8dSKuninori Morimoto 	}
3293d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
3294d191bd8dSKuninori Morimoto 
32952c7b696aSMarcel Ziswiler 	if (found)
3296cf9e829eSKuninori Morimoto 		snd_soc_component_cleanup(component);
32972eccea8cSKuninori Morimoto 
32982eccea8cSKuninori Morimoto 	return found;
32992eccea8cSKuninori Morimoto }
33002eccea8cSKuninori Morimoto 
33012eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
33022eccea8cSKuninori Morimoto {
33032c7b696aSMarcel Ziswiler 	while (__snd_soc_unregister_component(dev))
33042c7b696aSMarcel Ziswiler 		;
3305d191bd8dSKuninori Morimoto }
3306d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3307d191bd8dSKuninori Morimoto 
33087dd5d0d9SKuninori Morimoto struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
33097dd5d0d9SKuninori Morimoto 						   const char *driver_name)
33107dd5d0d9SKuninori Morimoto {
33117dd5d0d9SKuninori Morimoto 	struct snd_soc_component *component;
33127dd5d0d9SKuninori Morimoto 	struct snd_soc_component *ret;
33137dd5d0d9SKuninori Morimoto 
33147dd5d0d9SKuninori Morimoto 	ret = NULL;
33157dd5d0d9SKuninori Morimoto 	mutex_lock(&client_mutex);
3316368dee94SKuninori Morimoto 	for_each_component(component) {
33177dd5d0d9SKuninori Morimoto 		if (dev != component->dev)
33187dd5d0d9SKuninori Morimoto 			continue;
33197dd5d0d9SKuninori Morimoto 
33207dd5d0d9SKuninori Morimoto 		if (driver_name &&
33217dd5d0d9SKuninori Morimoto 		    (driver_name != component->driver->name) &&
33227dd5d0d9SKuninori Morimoto 		    (strcmp(component->driver->name, driver_name) != 0))
33237dd5d0d9SKuninori Morimoto 			continue;
33247dd5d0d9SKuninori Morimoto 
33257dd5d0d9SKuninori Morimoto 		ret = component;
33267dd5d0d9SKuninori Morimoto 		break;
33277dd5d0d9SKuninori Morimoto 	}
33287dd5d0d9SKuninori Morimoto 	mutex_unlock(&client_mutex);
33297dd5d0d9SKuninori Morimoto 
33307dd5d0d9SKuninori Morimoto 	return ret;
33317dd5d0d9SKuninori Morimoto }
33327dd5d0d9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
33337dd5d0d9SKuninori Morimoto 
3334bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
3335b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3336bec4fa05SStephen Warren 			       const char *propname)
3337bec4fa05SStephen Warren {
3338b07609ceSKuninori Morimoto 	struct device_node *np;
3339bec4fa05SStephen Warren 	int ret;
3340bec4fa05SStephen Warren 
33417e07e7c0STushar Behera 	if (!card->dev) {
33427e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
33437e07e7c0STushar Behera 		return -EINVAL;
33447e07e7c0STushar Behera 	}
33457e07e7c0STushar Behera 
33467e07e7c0STushar Behera 	np = card->dev->of_node;
33477e07e7c0STushar Behera 
3348bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
3349bec4fa05SStephen Warren 	/*
3350bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
3351bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
3352bec4fa05SStephen Warren 	 * snd_soc_register_card.
3353bec4fa05SStephen Warren 	 */
3354bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
3355bec4fa05SStephen Warren 		dev_err(card->dev,
3356f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
3357bec4fa05SStephen Warren 			propname, ret);
3358bec4fa05SStephen Warren 		return ret;
3359bec4fa05SStephen Warren 	}
3360bec4fa05SStephen Warren 
3361bec4fa05SStephen Warren 	return 0;
3362bec4fa05SStephen Warren }
3363b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3364bec4fa05SStephen Warren 
33659a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
33669a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
33679a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
33689a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
33699a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
33709a6d4860SXiubo Li };
33719a6d4860SXiubo Li 
337221efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
33739a6d4860SXiubo Li 					  const char *propname)
33749a6d4860SXiubo Li {
337521efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
33769a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
33779a6d4860SXiubo Li 	const char *template, *wname;
33789a6d4860SXiubo Li 	int i, j, num_widgets, ret;
33799a6d4860SXiubo Li 
33809a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
33819a6d4860SXiubo Li 	if (num_widgets < 0) {
33829a6d4860SXiubo Li 		dev_err(card->dev,
33839a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
33849a6d4860SXiubo Li 		return -EINVAL;
33859a6d4860SXiubo Li 	}
33869a6d4860SXiubo Li 	if (num_widgets & 1) {
33879a6d4860SXiubo Li 		dev_err(card->dev,
33889a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
33899a6d4860SXiubo Li 		return -EINVAL;
33909a6d4860SXiubo Li 	}
33919a6d4860SXiubo Li 
33929a6d4860SXiubo Li 	num_widgets /= 2;
33939a6d4860SXiubo Li 	if (!num_widgets) {
33949a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
33959a6d4860SXiubo Li 			propname);
33969a6d4860SXiubo Li 		return -EINVAL;
33979a6d4860SXiubo Li 	}
33989a6d4860SXiubo Li 
33999a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
34009a6d4860SXiubo Li 			       GFP_KERNEL);
34019a6d4860SXiubo Li 	if (!widgets) {
34029a6d4860SXiubo Li 		dev_err(card->dev,
34039a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
34049a6d4860SXiubo Li 		return -ENOMEM;
34059a6d4860SXiubo Li 	}
34069a6d4860SXiubo Li 
34079a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
34089a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
34099a6d4860SXiubo Li 			2 * i, &template);
34109a6d4860SXiubo Li 		if (ret) {
34119a6d4860SXiubo Li 			dev_err(card->dev,
34129a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
34139a6d4860SXiubo Li 				propname, 2 * i, ret);
34149a6d4860SXiubo Li 			return -EINVAL;
34159a6d4860SXiubo Li 		}
34169a6d4860SXiubo Li 
34179a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
34189a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
34199a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
34209a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
34219a6d4860SXiubo Li 				break;
34229a6d4860SXiubo Li 			}
34239a6d4860SXiubo Li 		}
34249a6d4860SXiubo Li 
34259a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
34269a6d4860SXiubo Li 			dev_err(card->dev,
34279a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
34289a6d4860SXiubo Li 				template);
34299a6d4860SXiubo Li 			return -EINVAL;
34309a6d4860SXiubo Li 		}
34319a6d4860SXiubo Li 
34329a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
34339a6d4860SXiubo Li 						    (2 * i) + 1,
34349a6d4860SXiubo Li 						    &wname);
34359a6d4860SXiubo Li 		if (ret) {
34369a6d4860SXiubo Li 			dev_err(card->dev,
34379a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
34389a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
34399a6d4860SXiubo Li 			return -EINVAL;
34409a6d4860SXiubo Li 		}
34419a6d4860SXiubo Li 
34429a6d4860SXiubo Li 		widgets[i].name = wname;
34439a6d4860SXiubo Li 	}
34449a6d4860SXiubo Li 
3445f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
3446f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
34479a6d4860SXiubo Li 
34489a6d4860SXiubo Li 	return 0;
34499a6d4860SXiubo Li }
345021efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
34519a6d4860SXiubo Li 
3452cbdfab3bSJerome Brunet int snd_soc_of_get_slot_mask(struct device_node *np,
34536131084aSJyri Sarha 			     const char *prop_name,
34546131084aSJyri Sarha 			     unsigned int *mask)
34556131084aSJyri Sarha {
34566131084aSJyri Sarha 	u32 val;
34576c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
34586131084aSJyri Sarha 	int i;
34596131084aSJyri Sarha 
34606131084aSJyri Sarha 	if (!of_slot_mask)
34616131084aSJyri Sarha 		return 0;
34626131084aSJyri Sarha 	val /= sizeof(u32);
34636131084aSJyri Sarha 	for (i = 0; i < val; i++)
34646131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
34656131084aSJyri Sarha 			*mask |= (1 << i);
34666131084aSJyri Sarha 
34676131084aSJyri Sarha 	return val;
34686131084aSJyri Sarha }
3469cbdfab3bSJerome Brunet EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
34706131084aSJyri Sarha 
347189c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
34726131084aSJyri Sarha 			      unsigned int *tx_mask,
34736131084aSJyri Sarha 			      unsigned int *rx_mask,
347489c67857SXiubo Li 			      unsigned int *slots,
347589c67857SXiubo Li 			      unsigned int *slot_width)
347689c67857SXiubo Li {
347789c67857SXiubo Li 	u32 val;
347889c67857SXiubo Li 	int ret;
347989c67857SXiubo Li 
34806131084aSJyri Sarha 	if (tx_mask)
34816131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
34826131084aSJyri Sarha 	if (rx_mask)
34836131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
34846131084aSJyri Sarha 
348589c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
348689c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
348789c67857SXiubo Li 		if (ret)
348889c67857SXiubo Li 			return ret;
348989c67857SXiubo Li 
349089c67857SXiubo Li 		if (slots)
349189c67857SXiubo Li 			*slots = val;
349289c67857SXiubo Li 	}
349389c67857SXiubo Li 
349489c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
349589c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
349689c67857SXiubo Li 		if (ret)
349789c67857SXiubo Li 			return ret;
349889c67857SXiubo Li 
349989c67857SXiubo Li 		if (slot_width)
350089c67857SXiubo Li 			*slot_width = val;
350189c67857SXiubo Li 	}
350289c67857SXiubo Li 
350389c67857SXiubo Li 	return 0;
350489c67857SXiubo Li }
350589c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
350689c67857SXiubo Li 
35073b710356SKuninori Morimoto void snd_soc_of_parse_node_prefix(struct device_node *np,
35085e3cdaa2SKuninori Morimoto 				  struct snd_soc_codec_conf *codec_conf,
35095e3cdaa2SKuninori Morimoto 				  struct device_node *of_node,
35105e3cdaa2SKuninori Morimoto 				  const char *propname)
35115e3cdaa2SKuninori Morimoto {
35125e3cdaa2SKuninori Morimoto 	const char *str;
35135e3cdaa2SKuninori Morimoto 	int ret;
35145e3cdaa2SKuninori Morimoto 
35155e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
35165e3cdaa2SKuninori Morimoto 	if (ret < 0) {
35175e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
35185e3cdaa2SKuninori Morimoto 		return;
35195e3cdaa2SKuninori Morimoto 	}
35205e3cdaa2SKuninori Morimoto 
35215e3cdaa2SKuninori Morimoto 	codec_conf->of_node	= of_node;
35225e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
35235e3cdaa2SKuninori Morimoto }
35243b710356SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
35255e3cdaa2SKuninori Morimoto 
35262bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3527a4a54dd5SStephen Warren 				   const char *propname)
3528a4a54dd5SStephen Warren {
35292bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
3530e3b1e6a1SMark Brown 	int num_routes;
3531a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3532a4a54dd5SStephen Warren 	int i, ret;
3533a4a54dd5SStephen Warren 
3534a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3535c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
353610e8aa9aSMichał Mirosław 		dev_err(card->dev,
353710e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
353810e8aa9aSMichał Mirosław 			propname);
3539a4a54dd5SStephen Warren 		return -EINVAL;
3540a4a54dd5SStephen Warren 	}
3541a4a54dd5SStephen Warren 	num_routes /= 2;
3542a4a54dd5SStephen Warren 	if (!num_routes) {
3543f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3544a4a54dd5SStephen Warren 			propname);
3545a4a54dd5SStephen Warren 		return -EINVAL;
3546a4a54dd5SStephen Warren 	}
3547a4a54dd5SStephen Warren 
3548a86854d0SKees Cook 	routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
3549a4a54dd5SStephen Warren 			      GFP_KERNEL);
3550a4a54dd5SStephen Warren 	if (!routes) {
3551a4a54dd5SStephen Warren 		dev_err(card->dev,
3552f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3553a4a54dd5SStephen Warren 		return -EINVAL;
3554a4a54dd5SStephen Warren 	}
3555a4a54dd5SStephen Warren 
3556a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3557a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3558e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
3559a4a54dd5SStephen Warren 		if (ret) {
3560c871bd0bSMark Brown 			dev_err(card->dev,
3561c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3562c871bd0bSMark Brown 				propname, 2 * i, ret);
3563a4a54dd5SStephen Warren 			return -EINVAL;
3564a4a54dd5SStephen Warren 		}
3565a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3566e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3567a4a54dd5SStephen Warren 		if (ret) {
3568a4a54dd5SStephen Warren 			dev_err(card->dev,
3569c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3570c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3571a4a54dd5SStephen Warren 			return -EINVAL;
3572a4a54dd5SStephen Warren 		}
3573a4a54dd5SStephen Warren 	}
3574a4a54dd5SStephen Warren 
3575f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
3576f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
3577a4a54dd5SStephen Warren 
3578a4a54dd5SStephen Warren 	return 0;
3579a4a54dd5SStephen Warren }
35802bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3581a4a54dd5SStephen Warren 
3582a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3583389cb834SJyri Sarha 				     const char *prefix,
3584389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3585389cb834SJyri Sarha 				     struct device_node **framemaster)
3586a7930ed4SKuninori Morimoto {
3587a7930ed4SKuninori Morimoto 	int ret, i;
3588a7930ed4SKuninori Morimoto 	char prop[128];
3589a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3590a7930ed4SKuninori Morimoto 	int bit, frame;
3591a7930ed4SKuninori Morimoto 	const char *str;
3592a7930ed4SKuninori Morimoto 	struct {
3593a7930ed4SKuninori Morimoto 		char *name;
3594a7930ed4SKuninori Morimoto 		unsigned int val;
3595a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3596a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3597a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3598a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3599a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3600a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3601a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3602a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3603a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3604a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3605a7930ed4SKuninori Morimoto 	};
3606a7930ed4SKuninori Morimoto 
3607a7930ed4SKuninori Morimoto 	if (!prefix)
3608a7930ed4SKuninori Morimoto 		prefix = "";
3609a7930ed4SKuninori Morimoto 
3610a7930ed4SKuninori Morimoto 	/*
36115711c979SKuninori Morimoto 	 * check "dai-format = xxx"
36125711c979SKuninori Morimoto 	 * or    "[prefix]format = xxx"
3613a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3614a7930ed4SKuninori Morimoto 	 */
36155711c979SKuninori Morimoto 	ret = of_property_read_string(np, "dai-format", &str);
36165711c979SKuninori Morimoto 	if (ret < 0) {
3617a7930ed4SKuninori Morimoto 		snprintf(prop, sizeof(prop), "%sformat", prefix);
3618a7930ed4SKuninori Morimoto 		ret = of_property_read_string(np, prop, &str);
36195711c979SKuninori Morimoto 	}
3620a7930ed4SKuninori Morimoto 	if (ret == 0) {
3621a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3622a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3623a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3624a7930ed4SKuninori Morimoto 				break;
3625a7930ed4SKuninori Morimoto 			}
3626a7930ed4SKuninori Morimoto 		}
3627a7930ed4SKuninori Morimoto 	}
3628a7930ed4SKuninori Morimoto 
3629a7930ed4SKuninori Morimoto 	/*
36308c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3631a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3632a7930ed4SKuninori Morimoto 	 */
36338c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
363451930295SJulia Lawall 	if (of_property_read_bool(np, prop))
36358c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
36368c2d6a9fSKuninori Morimoto 	else
36378c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3638a7930ed4SKuninori Morimoto 
3639a7930ed4SKuninori Morimoto 	/*
3640a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3641a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3642a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3643a7930ed4SKuninori Morimoto 	 */
3644a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3645a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3646a7930ed4SKuninori Morimoto 
3647a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3648a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3649a7930ed4SKuninori Morimoto 
3650a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3651a7930ed4SKuninori Morimoto 	case 0x11:
3652a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3653a7930ed4SKuninori Morimoto 		break;
3654a7930ed4SKuninori Morimoto 	case 0x10:
3655a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3656a7930ed4SKuninori Morimoto 		break;
3657a7930ed4SKuninori Morimoto 	case 0x01:
3658a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3659a7930ed4SKuninori Morimoto 		break;
3660a7930ed4SKuninori Morimoto 	default:
3661a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3662a7930ed4SKuninori Morimoto 		break;
3663a7930ed4SKuninori Morimoto 	}
3664a7930ed4SKuninori Morimoto 
3665a7930ed4SKuninori Morimoto 	/*
3666a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3667a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3668a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3669a7930ed4SKuninori Morimoto 	 */
3670a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3671a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3672389cb834SJyri Sarha 	if (bit && bitclkmaster)
3673389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3674a7930ed4SKuninori Morimoto 
3675a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3676a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3677389cb834SJyri Sarha 	if (frame && framemaster)
3678389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3679a7930ed4SKuninori Morimoto 
3680a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3681a7930ed4SKuninori Morimoto 	case 0x11:
3682a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3683a7930ed4SKuninori Morimoto 		break;
3684a7930ed4SKuninori Morimoto 	case 0x10:
3685a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3686a7930ed4SKuninori Morimoto 		break;
3687a7930ed4SKuninori Morimoto 	case 0x01:
3688a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3689a7930ed4SKuninori Morimoto 		break;
3690a7930ed4SKuninori Morimoto 	default:
3691a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3692a7930ed4SKuninori Morimoto 		break;
3693a7930ed4SKuninori Morimoto 	}
3694a7930ed4SKuninori Morimoto 
3695a7930ed4SKuninori Morimoto 	return format;
3696a7930ed4SKuninori Morimoto }
3697a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3698a7930ed4SKuninori Morimoto 
3699a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
3700a180e8b9SKuninori Morimoto {
3701a180e8b9SKuninori Morimoto 	struct snd_soc_component *pos;
3702a180e8b9SKuninori Morimoto 	struct device_node *node;
3703a180e8b9SKuninori Morimoto 	int ret;
3704a180e8b9SKuninori Morimoto 
3705a180e8b9SKuninori Morimoto 	node = of_graph_get_port_parent(ep);
3706a180e8b9SKuninori Morimoto 
3707a180e8b9SKuninori Morimoto 	/*
3708a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
3709a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
3710a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
3711a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
3712a180e8b9SKuninori Morimoto 	 */
3713a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
3714a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
3715368dee94SKuninori Morimoto 	for_each_component(pos) {
3716a180e8b9SKuninori Morimoto 		struct device_node *component_of_node = pos->dev->of_node;
3717a180e8b9SKuninori Morimoto 
3718a180e8b9SKuninori Morimoto 		if (!component_of_node && pos->dev->parent)
3719a180e8b9SKuninori Morimoto 			component_of_node = pos->dev->parent->of_node;
3720a180e8b9SKuninori Morimoto 
3721a180e8b9SKuninori Morimoto 		if (component_of_node != node)
3722a180e8b9SKuninori Morimoto 			continue;
3723a180e8b9SKuninori Morimoto 
3724a180e8b9SKuninori Morimoto 		if (pos->driver->of_xlate_dai_id)
3725a180e8b9SKuninori Morimoto 			ret = pos->driver->of_xlate_dai_id(pos, ep);
3726a180e8b9SKuninori Morimoto 
3727a180e8b9SKuninori Morimoto 		break;
3728a180e8b9SKuninori Morimoto 	}
3729a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
3730a180e8b9SKuninori Morimoto 
3731c0a480d1STony Lindgren 	of_node_put(node);
3732c0a480d1STony Lindgren 
3733a180e8b9SKuninori Morimoto 	return ret;
3734a180e8b9SKuninori Morimoto }
3735a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3736a180e8b9SKuninori Morimoto 
37371ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args,
3738cb470087SKuninori Morimoto 				const char **dai_name)
3739cb470087SKuninori Morimoto {
3740cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
37413e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
374293b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3743cb470087SKuninori Morimoto 
3744cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3745368dee94SKuninori Morimoto 	for_each_component(pos) {
37463e0aa8d8SJyri Sarha 		component_of_node = pos->dev->of_node;
37473e0aa8d8SJyri Sarha 		if (!component_of_node && pos->dev->parent)
37483e0aa8d8SJyri Sarha 			component_of_node = pos->dev->parent->of_node;
37493e0aa8d8SJyri Sarha 
37503e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
3751cb470087SKuninori Morimoto 			continue;
3752cb470087SKuninori Morimoto 
37536833c452SKuninori Morimoto 		if (pos->driver->of_xlate_dai_name) {
375493b0f3eeSJean-Francois Moine 			ret = pos->driver->of_xlate_dai_name(pos,
375593b0f3eeSJean-Francois Moine 							     args,
375693b0f3eeSJean-Francois Moine 							     dai_name);
37576833c452SKuninori Morimoto 		} else {
375858bf4179SKuninori Morimoto 			struct snd_soc_dai *dai;
37596833c452SKuninori Morimoto 			int id = -1;
37606833c452SKuninori Morimoto 
376193b0f3eeSJean-Francois Moine 			switch (args->args_count) {
37626833c452SKuninori Morimoto 			case 0:
37636833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
37646833c452SKuninori Morimoto 				break;
37656833c452SKuninori Morimoto 			case 1:
376693b0f3eeSJean-Francois Moine 				id = args->args[0];
37676833c452SKuninori Morimoto 				break;
37686833c452SKuninori Morimoto 			default:
37696833c452SKuninori Morimoto 				/* not supported */
3770cb470087SKuninori Morimoto 				break;
3771cb470087SKuninori Morimoto 			}
3772cb470087SKuninori Morimoto 
37736833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
37746833c452SKuninori Morimoto 				ret = -EINVAL;
37753dcba280SNicolin Chen 				continue;
37766833c452SKuninori Morimoto 			}
3777e41975edSXiubo Li 
3778e41975edSXiubo Li 			ret = 0;
3779e41975edSXiubo Li 
378058bf4179SKuninori Morimoto 			/* find target DAI */
378115a0c645SKuninori Morimoto 			for_each_component_dais(pos, dai) {
378258bf4179SKuninori Morimoto 				if (id == 0)
378358bf4179SKuninori Morimoto 					break;
378458bf4179SKuninori Morimoto 				id--;
378558bf4179SKuninori Morimoto 			}
378658bf4179SKuninori Morimoto 
378758bf4179SKuninori Morimoto 			*dai_name = dai->driver->name;
3788e41975edSXiubo Li 			if (!*dai_name)
3789e41975edSXiubo Li 				*dai_name = pos->name;
37906833c452SKuninori Morimoto 		}
37916833c452SKuninori Morimoto 
3792cb470087SKuninori Morimoto 		break;
3793cb470087SKuninori Morimoto 	}
3794cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
379593b0f3eeSJean-Francois Moine 	return ret;
379693b0f3eeSJean-Francois Moine }
37971ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
379893b0f3eeSJean-Francois Moine 
379993b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
380093b0f3eeSJean-Francois Moine 			    const char **dai_name)
380193b0f3eeSJean-Francois Moine {
380293b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
380393b0f3eeSJean-Francois Moine 	int ret;
380493b0f3eeSJean-Francois Moine 
380593b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
380693b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
380793b0f3eeSJean-Francois Moine 	if (ret)
380893b0f3eeSJean-Francois Moine 		return ret;
380993b0f3eeSJean-Francois Moine 
381093b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3811cb470087SKuninori Morimoto 
3812cb470087SKuninori Morimoto 	of_node_put(args.np);
3813cb470087SKuninori Morimoto 
3814cb470087SKuninori Morimoto 	return ret;
3815cb470087SKuninori Morimoto }
3816cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3817cb470087SKuninori Morimoto 
381893b0f3eeSJean-Francois Moine /*
381994685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
382094685763SSylwester Nawrocki  * @dai_link: DAI link
382194685763SSylwester Nawrocki  *
382294685763SSylwester Nawrocki  * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
382394685763SSylwester Nawrocki  */
382494685763SSylwester Nawrocki void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
382594685763SSylwester Nawrocki {
38263db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *component;
382794685763SSylwester Nawrocki 	int index;
382894685763SSylwester Nawrocki 
38293db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
383094685763SSylwester Nawrocki 		if (!component->of_node)
383194685763SSylwester Nawrocki 			break;
383294685763SSylwester Nawrocki 		of_node_put(component->of_node);
383394685763SSylwester Nawrocki 		component->of_node = NULL;
383494685763SSylwester Nawrocki 	}
383594685763SSylwester Nawrocki }
383694685763SSylwester Nawrocki EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
383794685763SSylwester Nawrocki 
383894685763SSylwester Nawrocki /*
383993b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
384093b0f3eeSJean-Francois Moine  * @dev: Card device
384193b0f3eeSJean-Francois Moine  * @of_node: Device node
384293b0f3eeSJean-Francois Moine  * @dai_link: DAI link
384393b0f3eeSJean-Francois Moine  *
384493b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
384593b0f3eeSJean-Francois Moine  * 'sound-dai'.
384693b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
384794685763SSylwester Nawrocki  * The device nodes in the array (of_node) must be dereferenced by calling
384894685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs() on @dai_link.
384993b0f3eeSJean-Francois Moine  *
385093b0f3eeSJean-Francois Moine  * Returns 0 for success
385193b0f3eeSJean-Francois Moine  */
385293b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
385393b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
385493b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
385593b0f3eeSJean-Francois Moine {
385693b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
385793b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
385893b0f3eeSJean-Francois Moine 	char *name;
385993b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
386093b0f3eeSJean-Francois Moine 
386193b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
386293b0f3eeSJean-Francois Moine 	name = "sound-dai";
386393b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
386493b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
386593b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
386693b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
386793b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
386893b0f3eeSJean-Francois Moine 		else
386993b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
387093b0f3eeSJean-Francois Moine 		return num_codecs;
387193b0f3eeSJean-Francois Moine 	}
3872a86854d0SKees Cook 	component = devm_kcalloc(dev,
3873a86854d0SKees Cook 				 num_codecs, sizeof(*component),
387493b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
387593b0f3eeSJean-Francois Moine 	if (!component)
387693b0f3eeSJean-Francois Moine 		return -ENOMEM;
387793b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
387893b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
387993b0f3eeSJean-Francois Moine 
388093b0f3eeSJean-Francois Moine 	/* Parse the list */
38813db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
388293b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
388393b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
388493b0f3eeSJean-Francois Moine 						 index, &args);
388593b0f3eeSJean-Francois Moine 		if (ret)
388693b0f3eeSJean-Francois Moine 			goto err;
388793b0f3eeSJean-Francois Moine 		component->of_node = args.np;
388893b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
388993b0f3eeSJean-Francois Moine 		if (ret < 0)
389093b0f3eeSJean-Francois Moine 			goto err;
389193b0f3eeSJean-Francois Moine 	}
389293b0f3eeSJean-Francois Moine 	return 0;
389393b0f3eeSJean-Francois Moine err:
389494685763SSylwester Nawrocki 	snd_soc_of_put_dai_link_codecs(dai_link);
389593b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
389693b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
389793b0f3eeSJean-Francois Moine 	return ret;
389893b0f3eeSJean-Francois Moine }
389993b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
390093b0f3eeSJean-Francois Moine 
3901c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3902db2a4165SFrank Mandarino {
39036553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
3904fb257897SMark Brown 	snd_soc_util_init();
3905fb257897SMark Brown 
3906db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3907db2a4165SFrank Mandarino }
39084abe8e16SMark Brown module_init(snd_soc_init);
3909db2a4165SFrank Mandarino 
39107d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3911db2a4165SFrank Mandarino {
3912fb257897SMark Brown 	snd_soc_util_exit();
39136553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3914fb257897SMark Brown 
3915db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3916db2a4165SFrank Mandarino }
3917db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3918db2a4165SFrank Mandarino 
3919db2a4165SFrank Mandarino /* Module information */
3920d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3921db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3922db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
3923db2a4165SFrank Mandarino MODULE_ALIAS("platform:soc-audio");
3924