xref: /openbmc/linux/sound/soc/soc-core.c (revision 29040d1ac569606fece70966179de272cfc0d4db)
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 {
231*29040d1aSKuninori Morimoto 	if (!card->debugfs_card_root)
232*29040d1aSKuninori Morimoto 		return;
233a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
234*29040d1aSKuninori Morimoto 	card->debugfs_card_root = NULL;
235a6052154SJarkko Nikula }
236a6052154SJarkko Nikula 
2376553bf06SLars-Peter Clausen static void snd_soc_debugfs_init(void)
2386553bf06SLars-Peter Clausen {
2396553bf06SLars-Peter Clausen 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
240d9a02c55SFabio Estevam 	if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) {
2416553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create debugfs directory\n");
2426553bf06SLars-Peter Clausen 		snd_soc_debugfs_root = NULL;
2436553bf06SLars-Peter Clausen 		return;
2446553bf06SLars-Peter Clausen 	}
2456553bf06SLars-Peter Clausen 
2466553bf06SLars-Peter Clausen 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
2476553bf06SLars-Peter Clausen 				 &dai_list_fops))
2486553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
249db795f9bSKuninori Morimoto 
250db795f9bSKuninori Morimoto 	if (!debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
251db795f9bSKuninori Morimoto 				 &component_list_fops))
252db795f9bSKuninori Morimoto 		pr_warn("ASoC: Failed to create component list debugfs file\n");
2536553bf06SLars-Peter Clausen }
2546553bf06SLars-Peter Clausen 
2556553bf06SLars-Peter Clausen static void snd_soc_debugfs_exit(void)
2566553bf06SLars-Peter Clausen {
2576553bf06SLars-Peter Clausen 	debugfs_remove_recursive(snd_soc_debugfs_root);
2586553bf06SLars-Peter Clausen }
2596553bf06SLars-Peter Clausen 
2602624d5faSMark Brown #else
2612624d5faSMark Brown 
26281c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs(
26381c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
2642624d5faSMark Brown {
2652624d5faSMark Brown }
2662624d5faSMark Brown 
26781c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs(
26881c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
269731f1ab2SSebastien Guiriec {
270731f1ab2SSebastien Guiriec }
271731f1ab2SSebastien Guiriec 
272b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
273b95fccbcSAxel Lin {
274b95fccbcSAxel Lin }
275b95fccbcSAxel Lin 
276b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
277b95fccbcSAxel Lin {
278b95fccbcSAxel Lin }
2796553bf06SLars-Peter Clausen 
2806553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_init(void)
2816553bf06SLars-Peter Clausen {
2826553bf06SLars-Peter Clausen }
2836553bf06SLars-Peter Clausen 
2846553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_exit(void)
2856553bf06SLars-Peter Clausen {
2866553bf06SLars-Peter Clausen }
2876553bf06SLars-Peter Clausen 
2882624d5faSMark Brown #endif
2892624d5faSMark Brown 
290a0ac4411SKuninori Morimoto static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
291a0ac4411SKuninori Morimoto 			      struct snd_soc_component *component)
292a0ac4411SKuninori Morimoto {
293a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
294a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *new_rtdcom;
295a0ac4411SKuninori Morimoto 
296a0ac4411SKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
297a0ac4411SKuninori Morimoto 		/* already connected */
298a0ac4411SKuninori Morimoto 		if (rtdcom->component == component)
299a0ac4411SKuninori Morimoto 			return 0;
300a0ac4411SKuninori Morimoto 	}
301a0ac4411SKuninori Morimoto 
302a0ac4411SKuninori Morimoto 	new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
303a0ac4411SKuninori Morimoto 	if (!new_rtdcom)
304a0ac4411SKuninori Morimoto 		return -ENOMEM;
305a0ac4411SKuninori Morimoto 
306a0ac4411SKuninori Morimoto 	new_rtdcom->component = component;
307a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&new_rtdcom->list);
308a0ac4411SKuninori Morimoto 
309a0ac4411SKuninori Morimoto 	list_add_tail(&new_rtdcom->list, &rtd->component_list);
310a0ac4411SKuninori Morimoto 
311a0ac4411SKuninori Morimoto 	return 0;
312a0ac4411SKuninori Morimoto }
313a0ac4411SKuninori Morimoto 
314a0ac4411SKuninori Morimoto static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
315a0ac4411SKuninori Morimoto {
316a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
317a0ac4411SKuninori Morimoto 
318a0ac4411SKuninori Morimoto 	for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
319a0ac4411SKuninori Morimoto 		kfree(rtdcom1);
320a0ac4411SKuninori Morimoto 
321a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
322a0ac4411SKuninori Morimoto }
323a0ac4411SKuninori Morimoto 
324a0ac4411SKuninori Morimoto struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
325a0ac4411SKuninori Morimoto 						const char *driver_name)
326a0ac4411SKuninori Morimoto {
327a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
328a0ac4411SKuninori Morimoto 
329971da24cSKuninori Morimoto 	if (!driver_name)
330971da24cSKuninori Morimoto 		return NULL;
331971da24cSKuninori Morimoto 
332a0ac4411SKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
333971da24cSKuninori Morimoto 		const char *component_name = rtdcom->component->driver->name;
334971da24cSKuninori Morimoto 
335971da24cSKuninori Morimoto 		if (!component_name)
336971da24cSKuninori Morimoto 			continue;
337971da24cSKuninori Morimoto 
338971da24cSKuninori Morimoto 		if ((component_name == driver_name) ||
339971da24cSKuninori Morimoto 		    strcmp(component_name, driver_name) == 0)
340a0ac4411SKuninori Morimoto 			return rtdcom->component;
341a0ac4411SKuninori Morimoto 	}
342a0ac4411SKuninori Morimoto 
343a0ac4411SKuninori Morimoto 	return NULL;
344a0ac4411SKuninori Morimoto }
345031734b7SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
346a0ac4411SKuninori Morimoto 
34747c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
34847c88fffSLiam Girdwood 		const char *dai_link, int stream)
34947c88fffSLiam Girdwood {
3501a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
35147c88fffSLiam Girdwood 
352bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
3531a497983SMengdong Lin 		if (rtd->dai_link->no_pcm &&
3541a497983SMengdong Lin 			!strcmp(rtd->dai_link->name, dai_link))
3551a497983SMengdong Lin 			return rtd->pcm->streams[stream].substream;
35647c88fffSLiam Girdwood 	}
357f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
35847c88fffSLiam Girdwood 	return NULL;
35947c88fffSLiam Girdwood }
36047c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
36147c88fffSLiam Girdwood 
36275ab9eb6SKuninori Morimoto static const struct snd_soc_ops null_snd_soc_ops;
36375ab9eb6SKuninori Morimoto 
3641a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
3651a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
3661a497983SMengdong Lin {
3671a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
3681a497983SMengdong Lin 
3691a497983SMengdong Lin 	rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
3701a497983SMengdong Lin 	if (!rtd)
3711a497983SMengdong Lin 		return NULL;
3721a497983SMengdong Lin 
373a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
3741a497983SMengdong Lin 	rtd->card = card;
3751a497983SMengdong Lin 	rtd->dai_link = dai_link;
37675ab9eb6SKuninori Morimoto 	if (!rtd->dai_link->ops)
37775ab9eb6SKuninori Morimoto 		rtd->dai_link->ops = &null_snd_soc_ops;
37875ab9eb6SKuninori Morimoto 
3796396bb22SKees Cook 	rtd->codec_dais = kcalloc(dai_link->num_codecs,
3806396bb22SKees Cook 					sizeof(struct snd_soc_dai *),
3811a497983SMengdong Lin 					GFP_KERNEL);
3821a497983SMengdong Lin 	if (!rtd->codec_dais) {
3831a497983SMengdong Lin 		kfree(rtd);
3841a497983SMengdong Lin 		return NULL;
3851a497983SMengdong Lin 	}
3861a497983SMengdong Lin 
3871a497983SMengdong Lin 	return rtd;
3881a497983SMengdong Lin }
3891a497983SMengdong Lin 
3901a497983SMengdong Lin static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
3911a497983SMengdong Lin {
3921a497983SMengdong Lin 	kfree(rtd->codec_dais);
393a0ac4411SKuninori Morimoto 	snd_soc_rtdcom_del_all(rtd);
3941a497983SMengdong Lin 	kfree(rtd);
3951a497983SMengdong Lin }
3961a497983SMengdong Lin 
3971a497983SMengdong Lin static void soc_add_pcm_runtime(struct snd_soc_card *card,
3981a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd)
3991a497983SMengdong Lin {
4001a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
4011a497983SMengdong Lin 	rtd->num = card->num_rtd;
4021a497983SMengdong Lin 	card->num_rtd++;
4031a497983SMengdong Lin }
4041a497983SMengdong Lin 
4051a497983SMengdong Lin static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
4061a497983SMengdong Lin {
4071a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, *_rtd;
4081a497983SMengdong Lin 
409bcb1fd1fSKuninori Morimoto 	for_each_card_rtds_safe(card, rtd, _rtd) {
4101a497983SMengdong Lin 		list_del(&rtd->list);
4111a497983SMengdong Lin 		soc_free_pcm_runtime(rtd);
4121a497983SMengdong Lin 	}
4131a497983SMengdong Lin 
4141a497983SMengdong Lin 	card->num_rtd = 0;
4151a497983SMengdong Lin }
4161a497983SMengdong Lin 
41747c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
41847c88fffSLiam Girdwood 		const char *dai_link)
41947c88fffSLiam Girdwood {
4201a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
42147c88fffSLiam Girdwood 
422bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4231a497983SMengdong Lin 		if (!strcmp(rtd->dai_link->name, dai_link))
4241a497983SMengdong Lin 			return rtd;
42547c88fffSLiam Girdwood 	}
426f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
42747c88fffSLiam Girdwood 	return NULL;
42847c88fffSLiam Girdwood }
42947c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
43047c88fffSLiam Girdwood 
43165462e44SKuninori Morimoto static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
43265462e44SKuninori Morimoto {
43365462e44SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
43465462e44SKuninori Morimoto 
43565462e44SKuninori Morimoto 	for_each_card_rtds(card, rtd)
43665462e44SKuninori Morimoto 		flush_delayed_work(&rtd->delayed_work);
43765462e44SKuninori Morimoto }
43865462e44SKuninori Morimoto 
4399d58a077SRichard Fitzgerald static void codec2codec_close_delayed_work(struct work_struct *work)
4409d58a077SRichard Fitzgerald {
4412c7b696aSMarcel Ziswiler 	/*
4422c7b696aSMarcel Ziswiler 	 * Currently nothing to do for c2c links
4439d58a077SRichard Fitzgerald 	 * Since c2c links are internal nodes in the DAPM graph and
4449d58a077SRichard Fitzgerald 	 * don't interface with the outside world or application layer
4459d58a077SRichard Fitzgerald 	 * we don't have to do any special handling on close.
4469d58a077SRichard Fitzgerald 	 */
4479d58a077SRichard Fitzgerald }
4489d58a077SRichard Fitzgerald 
4496f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
450db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
4516f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
452db2a4165SFrank Mandarino {
4536f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
454d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
4551a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
4561a497983SMengdong Lin 	int i;
457db2a4165SFrank Mandarino 
458c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
459c5599b87SLars-Peter Clausen 	if (!card->instantiated)
460e3509ff0SDaniel Mack 		return 0;
461e3509ff0SDaniel Mack 
4622c7b696aSMarcel Ziswiler 	/*
4632c7b696aSMarcel Ziswiler 	 * Due to the resume being scheduled into a workqueue we could
4646ed25978SAndy Green 	 * suspend before that's finished - wait for it to complete.
4656ed25978SAndy Green 	 */
466f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
4676ed25978SAndy Green 
4686ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
469f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
4706ed25978SAndy Green 
471a00f90f9SMark Brown 	/* mute any active DACs */
472bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4730b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
4743efab7dcSMark Brown 
4751a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4763efab7dcSMark Brown 			continue;
4773efab7dcSMark Brown 
4780b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
47988bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
48088bd870fSBenoit Cousson 
481f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
482f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 1);
483db2a4165SFrank Mandarino 		}
48488bd870fSBenoit Cousson 	}
485db2a4165SFrank Mandarino 
4864ccab3e7SLiam Girdwood 	/* suspend all pcms */
487bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4881a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4893efab7dcSMark Brown 			continue;
4903efab7dcSMark Brown 
4911a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
4923efab7dcSMark Brown 	}
4934ccab3e7SLiam Girdwood 
49487506549SMark Brown 	if (card->suspend_pre)
49570b2ac12SMark Brown 		card->suspend_pre(card);
496db2a4165SFrank Mandarino 
497bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4981a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
4993efab7dcSMark Brown 
5001a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5013efab7dcSMark Brown 			continue;
5023efab7dcSMark Brown 
503bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
504f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
505db2a4165SFrank Mandarino 	}
506db2a4165SFrank Mandarino 
50737660b6dSLars-Peter Clausen 	/* close any waiting streams */
50865462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
509db2a4165SFrank Mandarino 
510bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5113efab7dcSMark Brown 
5121a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5133efab7dcSMark Brown 			continue;
5143efab7dcSMark Brown 
5151a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
5167bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
517db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
518f0fba2adSLiam Girdwood 
5191a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
5207bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
521db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
522db2a4165SFrank Mandarino 	}
523db2a4165SFrank Mandarino 
5248be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
5258be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
526e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
527e2d32ff6SMark Brown 
5289178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
529f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
5302c7b696aSMarcel Ziswiler 		struct snd_soc_dapm_context *dapm =
5312c7b696aSMarcel Ziswiler 				snd_soc_component_get_dapm(component);
532d9fc4063SKuninori Morimoto 
5332c7b696aSMarcel Ziswiler 		/*
5342c7b696aSMarcel Ziswiler 		 * If there are paths active then the COMPONENT will be held
5352c7b696aSMarcel Ziswiler 		 * with bias _ON and should not be suspended.
5362c7b696aSMarcel Ziswiler 		 */
5379178feb4SKuninori Morimoto 		if (!component->suspended) {
5384890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
5391547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
540125a25daSMark Brown 				/*
5419178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
542125a25daSMark Brown 				 * bias off then being in STANDBY
543125a25daSMark Brown 				 * means it's doing something,
544125a25daSMark Brown 				 * otherwise fall through.
545125a25daSMark Brown 				 */
5464890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
5479178feb4SKuninori Morimoto 					dev_dbg(component->dev,
54810e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
549125a25daSMark Brown 					break;
550125a25daSMark Brown 				}
5511a12d5dcSGustavo A. R. Silva 				/* fall through */
552a8093297SLars-Peter Clausen 
5531547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
554999f7f5aSKuninori Morimoto 				if (component->driver->suspend)
555999f7f5aSKuninori Morimoto 					component->driver->suspend(component);
5569178feb4SKuninori Morimoto 				component->suspended = 1;
5579178feb4SKuninori Morimoto 				if (component->regmap)
5589178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
559988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
5609178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
5611547aba9SMark Brown 				break;
5621547aba9SMark Brown 			default:
5639178feb4SKuninori Morimoto 				dev_dbg(component->dev,
5649178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
5651547aba9SMark Brown 				break;
5661547aba9SMark Brown 			}
5671547aba9SMark Brown 		}
568f0fba2adSLiam Girdwood 	}
569db2a4165SFrank Mandarino 
570bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5711a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
5723efab7dcSMark Brown 
5731a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5743efab7dcSMark Brown 			continue;
5753efab7dcSMark Brown 
576bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
577f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
578988e8cc4SNicolin Chen 
579988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
580988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
581db2a4165SFrank Mandarino 	}
582db2a4165SFrank Mandarino 
58387506549SMark Brown 	if (card->suspend_post)
58470b2ac12SMark Brown 		card->suspend_post(card);
585db2a4165SFrank Mandarino 
586db2a4165SFrank Mandarino 	return 0;
587db2a4165SFrank Mandarino }
5886f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
589db2a4165SFrank Mandarino 
5902c7b696aSMarcel Ziswiler /*
5912c7b696aSMarcel Ziswiler  * deferred resume work, so resume can complete before we finished
5926ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
5936ed25978SAndy Green  */
5946ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
595db2a4165SFrank Mandarino {
596f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
5972c7b696aSMarcel Ziswiler 			container_of(work, struct snd_soc_card,
5982c7b696aSMarcel Ziswiler 				     deferred_resume_work);
5991a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
600d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
6011a497983SMengdong Lin 	int i;
602db2a4165SFrank Mandarino 
6032c7b696aSMarcel Ziswiler 	/*
6042c7b696aSMarcel Ziswiler 	 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6056ed25978SAndy Green 	 * so userspace apps are blocked from touching us
6066ed25978SAndy Green 	 */
6076ed25978SAndy Green 
608f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
6096ed25978SAndy Green 
6109949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
611f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
6129949788bSMark Brown 
61387506549SMark Brown 	if (card->resume_pre)
61470b2ac12SMark Brown 		card->resume_pre(card);
615db2a4165SFrank Mandarino 
616bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
617bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6181a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6193efab7dcSMark Brown 
6201a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6213efab7dcSMark Brown 			continue;
6223efab7dcSMark Brown 
623bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
624f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
625db2a4165SFrank Mandarino 	}
626db2a4165SFrank Mandarino 
627f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
6289178feb4SKuninori Morimoto 		if (component->suspended) {
629999f7f5aSKuninori Morimoto 			if (component->driver->resume)
630999f7f5aSKuninori Morimoto 				component->driver->resume(component);
6319178feb4SKuninori Morimoto 			component->suspended = 0;
6321547aba9SMark Brown 		}
633f0fba2adSLiam Girdwood 	}
634db2a4165SFrank Mandarino 
635bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6363efab7dcSMark Brown 
6371a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6383efab7dcSMark Brown 			continue;
6393efab7dcSMark Brown 
6401a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
641d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
642db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
643f0fba2adSLiam Girdwood 
6441a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
645d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
646db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
647db2a4165SFrank Mandarino 	}
648db2a4165SFrank Mandarino 
6493ff3f64bSMark Brown 	/* unmute any active DACs */
650bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6510b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
6523efab7dcSMark Brown 
6531a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6543efab7dcSMark Brown 			continue;
6553efab7dcSMark Brown 
6560b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
65788bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
65888bd870fSBenoit Cousson 
659f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
660f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 0);
661db2a4165SFrank Mandarino 		}
66288bd870fSBenoit Cousson 	}
663db2a4165SFrank Mandarino 
664bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6651a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6663efab7dcSMark Brown 
6671a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6683efab7dcSMark Brown 			continue;
6693efab7dcSMark Brown 
670bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
671f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
672db2a4165SFrank Mandarino 	}
673db2a4165SFrank Mandarino 
67487506549SMark Brown 	if (card->resume_post)
67570b2ac12SMark Brown 		card->resume_post(card);
676db2a4165SFrank Mandarino 
677f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
6786ed25978SAndy Green 
6798be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
6808be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
681e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
6821a7aaa58SJeeja KP 
6831a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
6841a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
6856ed25978SAndy Green }
6866ed25978SAndy Green 
6876ed25978SAndy Green /* powers up audio subsystem after a suspend */
6886f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
6896ed25978SAndy Green {
6906f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
691bc263214SLars-Peter Clausen 	bool bus_control = false;
6921a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
693b9dd94a8SPeter Ujfalusi 
694c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
695c5599b87SLars-Peter Clausen 	if (!card->instantiated)
6965ff1ddf2SEric Miao 		return 0;
6975ff1ddf2SEric Miao 
698988e8cc4SNicolin Chen 	/* activate pins from sleep state */
699bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7000b7990e3SKuninori Morimoto 		struct snd_soc_dai *codec_dai;
70188bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
70288bd870fSBenoit Cousson 		int j;
70388bd870fSBenoit Cousson 
704988e8cc4SNicolin Chen 		if (cpu_dai->active)
705988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
70688bd870fSBenoit Cousson 
7070b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, j, codec_dai) {
708988e8cc4SNicolin Chen 			if (codec_dai->active)
709988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
710988e8cc4SNicolin Chen 		}
71188bd870fSBenoit Cousson 	}
712988e8cc4SNicolin Chen 
713bc263214SLars-Peter Clausen 	/*
714bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
715bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
716bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
71764ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
71864ab9baaSMark Brown 	 */
719bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7201a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7212c7b696aSMarcel Ziswiler 
722bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
72382e14e8bSStephen Warren 	}
724bc263214SLars-Peter Clausen 	if (bus_control) {
725bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
72664ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
72764ab9baaSMark Brown 	} else {
728f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
7296308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
730f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
731f0fba2adSLiam Girdwood 	}
7326ed25978SAndy Green 
733db2a4165SFrank Mandarino 	return 0;
734db2a4165SFrank Mandarino }
7356f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
736db2a4165SFrank Mandarino #else
7376f8ab4acSMark Brown #define snd_soc_suspend NULL
7386f8ab4acSMark Brown #define snd_soc_resume NULL
739db2a4165SFrank Mandarino #endif
740db2a4165SFrank Mandarino 
74185e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
74202a06d30SBarry Song };
74302a06d30SBarry Song 
74465d9361fSLars-Peter Clausen static struct snd_soc_component *soc_find_component(
74565d9361fSLars-Peter Clausen 	const struct device_node *of_node, const char *name)
74612023a9aSMisael Lopez Cruz {
74765d9361fSLars-Peter Clausen 	struct snd_soc_component *component;
748d0b95e6cSCharles Keepax 	struct device_node *component_of_node;
74912023a9aSMisael Lopez Cruz 
75034e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
75134e81ab4SLars-Peter Clausen 
752368dee94SKuninori Morimoto 	for_each_component(component) {
75365d9361fSLars-Peter Clausen 		if (of_node) {
754d0b95e6cSCharles Keepax 			component_of_node = component->dev->of_node;
755d0b95e6cSCharles Keepax 			if (!component_of_node && component->dev->parent)
756d0b95e6cSCharles Keepax 				component_of_node = component->dev->parent->of_node;
757d0b95e6cSCharles Keepax 
758d0b95e6cSCharles Keepax 			if (component_of_node == of_node)
75965d9361fSLars-Peter Clausen 				return component;
7605a7b2aabSMark Brown 		} else if (name && strcmp(component->name, name) == 0) {
76165d9361fSLars-Peter Clausen 			return component;
76212023a9aSMisael Lopez Cruz 		}
76312023a9aSMisael Lopez Cruz 	}
76412023a9aSMisael Lopez Cruz 
76512023a9aSMisael Lopez Cruz 	return NULL;
76612023a9aSMisael Lopez Cruz }
76712023a9aSMisael Lopez Cruz 
768be6ac0a9SKuninori Morimoto static int snd_soc_is_matching_component(
769be6ac0a9SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc,
770be6ac0a9SKuninori Morimoto 	struct snd_soc_component *component)
771be6ac0a9SKuninori Morimoto {
772be6ac0a9SKuninori Morimoto 	struct device_node *component_of_node;
773be6ac0a9SKuninori Morimoto 
774be6ac0a9SKuninori Morimoto 	component_of_node = component->dev->of_node;
775be6ac0a9SKuninori Morimoto 	if (!component_of_node && component->dev->parent)
776be6ac0a9SKuninori Morimoto 		component_of_node = component->dev->parent->of_node;
777be6ac0a9SKuninori Morimoto 
778be6ac0a9SKuninori Morimoto 	if (dlc->of_node && component_of_node != dlc->of_node)
779be6ac0a9SKuninori Morimoto 		return 0;
780be6ac0a9SKuninori Morimoto 	if (dlc->name && strcmp(component->name, dlc->name))
781be6ac0a9SKuninori Morimoto 		return 0;
782be6ac0a9SKuninori Morimoto 
783be6ac0a9SKuninori Morimoto 	return 1;
784be6ac0a9SKuninori Morimoto }
785be6ac0a9SKuninori Morimoto 
786fbb88b5cSMengdong Lin /**
787fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
788fbb88b5cSMengdong Lin  *
7894958471bSJeffy Chen  * @dlc: name of the DAI or the DAI driver and optional component info to match
790fbb88b5cSMengdong Lin  *
791ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
792fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
793fbb88b5cSMengdong Lin  * should also match if being specified.
794fbb88b5cSMengdong Lin  *
795fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
796fbb88b5cSMengdong Lin  */
797305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
79814621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
79912023a9aSMisael Lopez Cruz {
80014621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
80114621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
80212023a9aSMisael Lopez Cruz 
80334e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
80434e81ab4SLars-Peter Clausen 
80514621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs */
806368dee94SKuninori Morimoto 	for_each_component(component) {
807be6ac0a9SKuninori Morimoto 		if (!snd_soc_is_matching_component(dlc, component))
80812023a9aSMisael Lopez Cruz 			continue;
80915a0c645SKuninori Morimoto 		for_each_component_dais(component, dai) {
8104958471bSJeffy Chen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
8116a6dafdaSJeffy Chen 			    && (!dai->driver->name
8126a6dafdaSJeffy Chen 				|| strcmp(dai->driver->name, dlc->dai_name)))
81314621c7eSLars-Peter Clausen 				continue;
81412023a9aSMisael Lopez Cruz 
81514621c7eSLars-Peter Clausen 			return dai;
81612023a9aSMisael Lopez Cruz 		}
81712023a9aSMisael Lopez Cruz 	}
81812023a9aSMisael Lopez Cruz 
81912023a9aSMisael Lopez Cruz 	return NULL;
82012023a9aSMisael Lopez Cruz }
821305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
82212023a9aSMisael Lopez Cruz 
82317fb1755SMengdong Lin /**
82417fb1755SMengdong Lin  * snd_soc_find_dai_link - Find a DAI link
82517fb1755SMengdong Lin  *
82617fb1755SMengdong Lin  * @card: soc card
82717fb1755SMengdong Lin  * @id: DAI link ID to match
82817fb1755SMengdong Lin  * @name: DAI link name to match, optional
8298abab35fSCharles Keepax  * @stream_name: DAI link stream name to match, optional
83017fb1755SMengdong Lin  *
83117fb1755SMengdong Lin  * This function will search all existing DAI links of the soc card to
83217fb1755SMengdong Lin  * find the link of the same ID. Since DAI links may not have their
83317fb1755SMengdong Lin  * unique ID, so name and stream name should also match if being
83417fb1755SMengdong Lin  * specified.
83517fb1755SMengdong Lin  *
83617fb1755SMengdong Lin  * Return: pointer of DAI link, or NULL if not found.
83717fb1755SMengdong Lin  */
83817fb1755SMengdong Lin struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
83917fb1755SMengdong Lin 					       int id, const char *name,
84017fb1755SMengdong Lin 					       const char *stream_name)
84117fb1755SMengdong Lin {
84217fb1755SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
84317fb1755SMengdong Lin 
84417fb1755SMengdong Lin 	lockdep_assert_held(&client_mutex);
84517fb1755SMengdong Lin 
84698061fdbSKuninori Morimoto 	for_each_card_links_safe(card, link, _link) {
84717fb1755SMengdong Lin 		if (link->id != id)
84817fb1755SMengdong Lin 			continue;
84917fb1755SMengdong Lin 
85017fb1755SMengdong Lin 		if (name && (!link->name || strcmp(name, link->name)))
85117fb1755SMengdong Lin 			continue;
85217fb1755SMengdong Lin 
85317fb1755SMengdong Lin 		if (stream_name && (!link->stream_name
85417fb1755SMengdong Lin 			|| strcmp(stream_name, link->stream_name)))
85517fb1755SMengdong Lin 			continue;
85617fb1755SMengdong Lin 
85717fb1755SMengdong Lin 		return link;
85817fb1755SMengdong Lin 	}
85917fb1755SMengdong Lin 
86017fb1755SMengdong Lin 	return NULL;
86117fb1755SMengdong Lin }
86217fb1755SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
86317fb1755SMengdong Lin 
86449a5ba1cSMengdong Lin static bool soc_is_dai_link_bound(struct snd_soc_card *card,
86549a5ba1cSMengdong Lin 		struct snd_soc_dai_link *dai_link)
866db2a4165SFrank Mandarino {
86749a5ba1cSMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
86849a5ba1cSMengdong Lin 
869bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
87049a5ba1cSMengdong Lin 		if (rtd->dai_link == dai_link)
87149a5ba1cSMengdong Lin 			return true;
87249a5ba1cSMengdong Lin 	}
87349a5ba1cSMengdong Lin 
87449a5ba1cSMengdong Lin 	return false;
87549a5ba1cSMengdong Lin }
87649a5ba1cSMengdong Lin 
8776f2f1ff0SMengdong Lin static int soc_bind_dai_link(struct snd_soc_card *card,
8786f2f1ff0SMengdong Lin 	struct snd_soc_dai_link *dai_link)
879db2a4165SFrank Mandarino {
8801a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
881720734a0SKuninori Morimoto 	struct snd_soc_dai_link_component *codecs;
88214621c7eSLars-Peter Clausen 	struct snd_soc_dai_link_component cpu_dai_component;
88390be711eSKuninori Morimoto 	struct snd_soc_component *component;
8841a497983SMengdong Lin 	struct snd_soc_dai **codec_dais;
88588bd870fSBenoit Cousson 	int i;
886db2a4165SFrank Mandarino 
887a655de80SLiam Girdwood 	if (dai_link->ignore)
888a655de80SLiam Girdwood 		return 0;
889a655de80SLiam Girdwood 
8906f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
8916308419aSMark Brown 
89249a5ba1cSMengdong Lin 	if (soc_is_dai_link_bound(card, dai_link)) {
89349a5ba1cSMengdong Lin 		dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
89449a5ba1cSMengdong Lin 			dai_link->name);
89549a5ba1cSMengdong Lin 		return 0;
89649a5ba1cSMengdong Lin 	}
897db2a4165SFrank Mandarino 
898513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
899513cb311SSudip Mukherjee 	if (!rtd)
900513cb311SSudip Mukherjee 		return -ENOMEM;
901513cb311SSudip Mukherjee 
90214621c7eSLars-Peter Clausen 	cpu_dai_component.name = dai_link->cpu_name;
90314621c7eSLars-Peter Clausen 	cpu_dai_component.of_node = dai_link->cpu_of_node;
90414621c7eSLars-Peter Clausen 	cpu_dai_component.dai_name = dai_link->cpu_dai_name;
90514621c7eSLars-Peter Clausen 	rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
906b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
9076b490879SMartin Hundebøll 		dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
908f0fba2adSLiam Girdwood 			 dai_link->cpu_dai_name);
9091a497983SMengdong Lin 		goto _err_defer;
910c5af3a2eSMark Brown 	}
91190be711eSKuninori Morimoto 	snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
912c5af3a2eSMark Brown 
91388bd870fSBenoit Cousson 	rtd->num_codecs = dai_link->num_codecs;
91488bd870fSBenoit Cousson 
91588bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
9161a497983SMengdong Lin 	codec_dais = rtd->codec_dais;
917720734a0SKuninori Morimoto 	for_each_link_codecs(dai_link, i, codecs) {
918720734a0SKuninori Morimoto 		codec_dais[i] = snd_soc_find_dai(codecs);
91988bd870fSBenoit Cousson 		if (!codec_dais[i]) {
9207c7e2d6aSStefan Agner 			dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
921720734a0SKuninori Morimoto 				 codecs->dai_name);
9221a497983SMengdong Lin 			goto _err_defer;
92312023a9aSMisael Lopez Cruz 		}
92490be711eSKuninori Morimoto 		snd_soc_rtdcom_add(rtd, codec_dais[i]->component);
92588bd870fSBenoit Cousson 	}
92688bd870fSBenoit Cousson 
92788bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
92888bd870fSBenoit Cousson 	rtd->codec_dai = codec_dais[0];
92912023a9aSMisael Lopez Cruz 
930b19e6e7bSMark Brown 	/* find one from the set of registered platforms */
931368dee94SKuninori Morimoto 	for_each_component(component) {
932910fdcabSKuninori Morimoto 		if (!snd_soc_is_matching_component(dai_link->platforms,
933be6ac0a9SKuninori Morimoto 						   component))
93490be711eSKuninori Morimoto 			continue;
93590be711eSKuninori Morimoto 
93690be711eSKuninori Morimoto 		snd_soc_rtdcom_add(rtd, component);
93790be711eSKuninori Morimoto 	}
93890be711eSKuninori Morimoto 
9391a497983SMengdong Lin 	soc_add_pcm_runtime(card, rtd);
940b19e6e7bSMark Brown 	return 0;
9411a497983SMengdong Lin 
9421a497983SMengdong Lin _err_defer:
9431a497983SMengdong Lin 	soc_free_pcm_runtime(rtd);
9441a497983SMengdong Lin 	return -EPROBE_DEFER;
9456b05eda6SMark Brown }
9466b05eda6SMark Brown 
94722d14231SKuninori Morimoto static void soc_cleanup_component(struct snd_soc_component *component)
94822d14231SKuninori Morimoto {
94922d14231SKuninori Morimoto 	list_del(&component->card_list);
95022d14231SKuninori Morimoto 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
95122d14231SKuninori Morimoto 	soc_cleanup_component_debugfs(component);
95222d14231SKuninori Morimoto 	component->card = NULL;
953b4ed6b51SRanjani Sridharan 	if (!component->driver->module_get_upon_open)
95422d14231SKuninori Morimoto 		module_put(component->dev->driver->owner);
95522d14231SKuninori Morimoto }
95622d14231SKuninori Morimoto 
957f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
958d12cd198SStephen Warren {
959abd31b32SLars-Peter Clausen 	if (!component->card)
96070090bbbSLars-Peter Clausen 		return;
961d12cd198SStephen Warren 
962999f7f5aSKuninori Morimoto 	if (component->driver->remove)
963999f7f5aSKuninori Morimoto 		component->driver->remove(component);
964d12cd198SStephen Warren 
96522d14231SKuninori Morimoto 	soc_cleanup_component(component);
966d12cd198SStephen Warren }
967d12cd198SStephen Warren 
968e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
969589c3563SJarkko Nikula {
970589c3563SJarkko Nikula 	int err;
971589c3563SJarkko Nikula 
97252abe6ccSGuennadi Liakhovetski 	if (!dai || !dai->probed || !dai->driver ||
9732eda3cb1SKuninori Morimoto 	    dai->driver->remove_order != order)
9742eda3cb1SKuninori Morimoto 		return;
9752eda3cb1SKuninori Morimoto 
976e60cd14fSLars-Peter Clausen 	if (dai->driver->remove) {
977e60cd14fSLars-Peter Clausen 		err = dai->driver->remove(dai);
978589c3563SJarkko Nikula 		if (err < 0)
979e60cd14fSLars-Peter Clausen 			dev_err(dai->dev,
980b0aa88afSMisael Lopez Cruz 				"ASoC: failed to remove %s: %d\n",
981e60cd14fSLars-Peter Clausen 				dai->name, err);
982b0aa88afSMisael Lopez Cruz 	}
983e60cd14fSLars-Peter Clausen 	dai->probed = 0;
984b0aa88afSMisael Lopez Cruz }
985b0aa88afSMisael Lopez Cruz 
9861a497983SMengdong Lin static void soc_remove_link_dais(struct snd_soc_card *card,
9871a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
988f0fba2adSLiam Girdwood {
989e60cd14fSLars-Peter Clausen 	int i;
9900b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
991f0fba2adSLiam Girdwood 
992f0fba2adSLiam Girdwood 	/* unregister the rtd device */
993f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
99436ae1a96SMark Brown 		device_unregister(rtd->dev);
995f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
99602a06d30SBarry Song 	}
99702a06d30SBarry Song 
998f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
9990b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
10000b7990e3SKuninori Morimoto 		soc_remove_dai(codec_dai, order);
1001f0fba2adSLiam Girdwood 
1002e60cd14fSLars-Peter Clausen 	soc_remove_dai(rtd->cpu_dai, order);
1003f0fba2adSLiam Girdwood }
1004f0fba2adSLiam Girdwood 
10051a497983SMengdong Lin static void soc_remove_link_components(struct snd_soc_card *card,
10061a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, int order)
100762ae68faSStephen Warren {
100861aca564SLars-Peter Clausen 	struct snd_soc_component *component;
100990be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
101062ae68faSStephen Warren 
101134ac3c3eSRanjani Sridharan 	mutex_lock(&client_mutex);
101290be711eSKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
101390be711eSKuninori Morimoto 		component = rtdcom->component;
101462ae68faSStephen Warren 
101570090bbbSLars-Peter Clausen 		if (component->driver->remove_order == order)
101661aca564SLars-Peter Clausen 			soc_remove_component(component);
101762ae68faSStephen Warren 	}
101834ac3c3eSRanjani Sridharan 	mutex_unlock(&client_mutex);
101962ae68faSStephen Warren }
102062ae68faSStephen Warren 
10210671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
10220671fd8eSKuninori Morimoto {
10231a497983SMengdong Lin 	int order;
10241a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
1025f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
10260671fd8eSKuninori Morimoto 
10271a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1028bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd)
10291a497983SMengdong Lin 			soc_remove_link_dais(card, rtd, order);
10300168bf0dSLiam Girdwood 	}
103162ae68faSStephen Warren 
10321a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1033bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd)
10341a497983SMengdong Lin 			soc_remove_link_components(card, rtd, order);
103562ae68faSStephen Warren 	}
103662ae68faSStephen Warren 
103798061fdbSKuninori Morimoto 	for_each_card_links_safe(card, link, _link) {
1038f8f80361SMengdong Lin 		if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1039f8f80361SMengdong Lin 			dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1040f8f80361SMengdong Lin 				link->name);
1041f8f80361SMengdong Lin 
1042f8f80361SMengdong Lin 		list_del(&link->list);
10430671fd8eSKuninori Morimoto 	}
10440671fd8eSKuninori Morimoto }
10450671fd8eSKuninori Morimoto 
1046daecf46eSKuninori Morimoto static int snd_soc_init_platform(struct snd_soc_card *card,
1047daecf46eSKuninori Morimoto 				 struct snd_soc_dai_link *dai_link)
1048daecf46eSKuninori Morimoto {
1049910fdcabSKuninori Morimoto 	struct snd_soc_dai_link_component *platform = dai_link->platforms;
10504a9ed394SKuninori Morimoto 
1051daecf46eSKuninori Morimoto 	/*
105262bc79d3SKuninori Morimoto 	 * REMOVE ME
1053daecf46eSKuninori Morimoto 	 *
105462bc79d3SKuninori Morimoto 	 * This is glue code for Legacy vs Modern dai_link.
105562bc79d3SKuninori Morimoto 	 * This function will be removed if all derivers are switched to
105662bc79d3SKuninori Morimoto 	 * modern style dai_link.
105762bc79d3SKuninori Morimoto 	 * Driver shouldn't use both legacy and modern style in the same time.
105862bc79d3SKuninori Morimoto 	 * see
105962bc79d3SKuninori Morimoto 	 *	soc.h :: struct snd_soc_dai_link
1060daecf46eSKuninori Morimoto 	 */
1061daecf46eSKuninori Morimoto 	/* convert Legacy platform link */
106278a24e10SCurtis Malainey 	if (!platform) {
10634a9ed394SKuninori Morimoto 		platform = devm_kzalloc(card->dev,
1064daecf46eSKuninori Morimoto 				sizeof(struct snd_soc_dai_link_component),
1065daecf46eSKuninori Morimoto 				GFP_KERNEL);
10664a9ed394SKuninori Morimoto 		if (!platform)
1067daecf46eSKuninori Morimoto 			return -ENOMEM;
1068daecf46eSKuninori Morimoto 
1069910fdcabSKuninori Morimoto 		dai_link->platforms	  = platform;
1070910fdcabSKuninori Morimoto 		dai_link->num_platforms	  = 1;
107109ac6a81SCurtis Malainey 		dai_link->legacy_platform = 1;
10724a9ed394SKuninori Morimoto 		platform->name		  = dai_link->platform_name;
10734a9ed394SKuninori Morimoto 		platform->of_node	  = dai_link->platform_of_node;
10744a9ed394SKuninori Morimoto 		platform->dai_name	  = NULL;
10754a9ed394SKuninori Morimoto 	}
10764a9ed394SKuninori Morimoto 
10774a9ed394SKuninori Morimoto 	/* if there's no platform we match on the empty platform */
10784a9ed394SKuninori Morimoto 	if (!platform->name &&
10794a9ed394SKuninori Morimoto 	    !platform->of_node)
10804a9ed394SKuninori Morimoto 		platform->name = "snd-soc-dummy";
1081daecf46eSKuninori Morimoto 
1082daecf46eSKuninori Morimoto 	return 0;
1083daecf46eSKuninori Morimoto }
1084daecf46eSKuninori Morimoto 
108578a24e10SCurtis Malainey static void soc_cleanup_platform(struct snd_soc_card *card)
108678a24e10SCurtis Malainey {
108778a24e10SCurtis Malainey 	struct snd_soc_dai_link *link;
108878a24e10SCurtis Malainey 	int i;
108978a24e10SCurtis Malainey 	/*
109078a24e10SCurtis Malainey 	 * FIXME
109178a24e10SCurtis Malainey 	 *
109278a24e10SCurtis Malainey 	 * this function should be removed with snd_soc_init_platform
109378a24e10SCurtis Malainey 	 */
109478a24e10SCurtis Malainey 
109578a24e10SCurtis Malainey 	for_each_card_prelinks(card, i, link) {
109678a24e10SCurtis Malainey 		if (link->legacy_platform) {
109778a24e10SCurtis Malainey 			link->legacy_platform = 0;
109878a24e10SCurtis Malainey 			link->platforms       = NULL;
109978a24e10SCurtis Malainey 		}
110078a24e10SCurtis Malainey 	}
110178a24e10SCurtis Malainey }
110278a24e10SCurtis Malainey 
1103923c5e61SMengdong Lin static int snd_soc_init_multicodec(struct snd_soc_card *card,
1104923c5e61SMengdong Lin 				   struct snd_soc_dai_link *dai_link)
1105923c5e61SMengdong Lin {
110662bc79d3SKuninori Morimoto 	/*
110762bc79d3SKuninori Morimoto 	 * REMOVE ME
110862bc79d3SKuninori Morimoto 	 *
110962bc79d3SKuninori Morimoto 	 * This is glue code for Legacy vs Modern dai_link.
111062bc79d3SKuninori Morimoto 	 * This function will be removed if all derivers are switched to
111162bc79d3SKuninori Morimoto 	 * modern style dai_link.
111262bc79d3SKuninori Morimoto 	 * Driver shouldn't use both legacy and modern style in the same time.
111362bc79d3SKuninori Morimoto 	 * see
111462bc79d3SKuninori Morimoto 	 *	soc.h :: struct snd_soc_dai_link
111562bc79d3SKuninori Morimoto 	 */
111662bc79d3SKuninori Morimoto 
1117923c5e61SMengdong Lin 	/* Legacy codec/codec_dai link is a single entry in multicodec */
1118923c5e61SMengdong Lin 	if (dai_link->codec_name || dai_link->codec_of_node ||
1119923c5e61SMengdong Lin 	    dai_link->codec_dai_name) {
1120923c5e61SMengdong Lin 		dai_link->num_codecs = 1;
1121923c5e61SMengdong Lin 
1122923c5e61SMengdong Lin 		dai_link->codecs = devm_kzalloc(card->dev,
1123923c5e61SMengdong Lin 				sizeof(struct snd_soc_dai_link_component),
1124923c5e61SMengdong Lin 				GFP_KERNEL);
1125923c5e61SMengdong Lin 		if (!dai_link->codecs)
1126923c5e61SMengdong Lin 			return -ENOMEM;
1127923c5e61SMengdong Lin 
1128923c5e61SMengdong Lin 		dai_link->codecs[0].name = dai_link->codec_name;
1129923c5e61SMengdong Lin 		dai_link->codecs[0].of_node = dai_link->codec_of_node;
1130923c5e61SMengdong Lin 		dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1131923c5e61SMengdong Lin 	}
1132923c5e61SMengdong Lin 
1133923c5e61SMengdong Lin 	if (!dai_link->codecs) {
1134923c5e61SMengdong Lin 		dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1135923c5e61SMengdong Lin 		return -EINVAL;
1136923c5e61SMengdong Lin 	}
1137923c5e61SMengdong Lin 
1138923c5e61SMengdong Lin 	return 0;
1139923c5e61SMengdong Lin }
1140923c5e61SMengdong Lin 
1141923c5e61SMengdong Lin static int soc_init_dai_link(struct snd_soc_card *card,
1142923c5e61SMengdong Lin 			     struct snd_soc_dai_link *link)
1143923c5e61SMengdong Lin {
1144923c5e61SMengdong Lin 	int i, ret;
11453db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *codec;
1146923c5e61SMengdong Lin 
1147daecf46eSKuninori Morimoto 	ret = snd_soc_init_platform(card, link);
1148daecf46eSKuninori Morimoto 	if (ret) {
1149daecf46eSKuninori Morimoto 		dev_err(card->dev, "ASoC: failed to init multiplatform\n");
1150daecf46eSKuninori Morimoto 		return ret;
1151daecf46eSKuninori Morimoto 	}
1152daecf46eSKuninori Morimoto 
1153923c5e61SMengdong Lin 	ret = snd_soc_init_multicodec(card, link);
1154923c5e61SMengdong Lin 	if (ret) {
1155923c5e61SMengdong Lin 		dev_err(card->dev, "ASoC: failed to init multicodec\n");
1156923c5e61SMengdong Lin 		return ret;
1157923c5e61SMengdong Lin 	}
1158923c5e61SMengdong Lin 
11593db769f1SKuninori Morimoto 	for_each_link_codecs(link, i, codec) {
1160923c5e61SMengdong Lin 		/*
1161923c5e61SMengdong Lin 		 * Codec must be specified by 1 of name or OF node,
1162923c5e61SMengdong Lin 		 * not both or neither.
1163923c5e61SMengdong Lin 		 */
11643db769f1SKuninori Morimoto 		if (!!codec->name ==
11653db769f1SKuninori Morimoto 		    !!codec->of_node) {
1166923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1167923c5e61SMengdong Lin 				link->name);
1168923c5e61SMengdong Lin 			return -EINVAL;
1169923c5e61SMengdong Lin 		}
1170923c5e61SMengdong Lin 		/* Codec DAI name must be specified */
11713db769f1SKuninori Morimoto 		if (!codec->dai_name) {
1172923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1173923c5e61SMengdong Lin 				link->name);
1174923c5e61SMengdong Lin 			return -EINVAL;
1175923c5e61SMengdong Lin 		}
1176923c5e61SMengdong Lin 	}
1177923c5e61SMengdong Lin 
1178910fdcabSKuninori Morimoto 	/* FIXME */
1179910fdcabSKuninori Morimoto 	if (link->num_platforms > 1) {
1180910fdcabSKuninori Morimoto 		dev_err(card->dev,
1181910fdcabSKuninori Morimoto 			"ASoC: multi platform is not yet supported %s\n",
1182910fdcabSKuninori Morimoto 			link->name);
1183910fdcabSKuninori Morimoto 		return -EINVAL;
1184910fdcabSKuninori Morimoto 	}
1185910fdcabSKuninori Morimoto 
1186923c5e61SMengdong Lin 	/*
1187923c5e61SMengdong Lin 	 * Platform may be specified by either name or OF node, but
1188923c5e61SMengdong Lin 	 * can be left unspecified, and a dummy platform will be used.
1189923c5e61SMengdong Lin 	 */
1190910fdcabSKuninori Morimoto 	if (link->platforms->name && link->platforms->of_node) {
1191923c5e61SMengdong Lin 		dev_err(card->dev,
1192923c5e61SMengdong Lin 			"ASoC: Both platform name/of_node are set for %s\n",
1193923c5e61SMengdong Lin 			link->name);
1194923c5e61SMengdong Lin 		return -EINVAL;
1195923c5e61SMengdong Lin 	}
11968780cf11SAjit Pandey 
11978780cf11SAjit Pandey 	/*
11988780cf11SAjit Pandey 	 * Defer card registartion if platform dai component is not added to
11998780cf11SAjit Pandey 	 * component list.
12008780cf11SAjit Pandey 	 */
1201910fdcabSKuninori Morimoto 	if ((link->platforms->of_node || link->platforms->name) &&
1202910fdcabSKuninori Morimoto 	    !soc_find_component(link->platforms->of_node, link->platforms->name))
12038780cf11SAjit Pandey 		return -EPROBE_DEFER;
12048780cf11SAjit Pandey 
1205923c5e61SMengdong Lin 	/*
1206923c5e61SMengdong Lin 	 * CPU device may be specified by either name or OF node, but
1207923c5e61SMengdong Lin 	 * can be left unspecified, and will be matched based on DAI
1208923c5e61SMengdong Lin 	 * name alone..
1209923c5e61SMengdong Lin 	 */
1210923c5e61SMengdong Lin 	if (link->cpu_name && link->cpu_of_node) {
1211923c5e61SMengdong Lin 		dev_err(card->dev,
1212923c5e61SMengdong Lin 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
1213923c5e61SMengdong Lin 			link->name);
1214923c5e61SMengdong Lin 		return -EINVAL;
1215923c5e61SMengdong Lin 	}
12168780cf11SAjit Pandey 
12178780cf11SAjit Pandey 	/*
12188780cf11SAjit Pandey 	 * Defer card registartion if cpu dai component is not added to
12198780cf11SAjit Pandey 	 * component list.
12208780cf11SAjit Pandey 	 */
12212833548eSMatthias Reichl 	if ((link->cpu_of_node || link->cpu_name) &&
12222833548eSMatthias Reichl 	    !soc_find_component(link->cpu_of_node, link->cpu_name))
12238780cf11SAjit Pandey 		return -EPROBE_DEFER;
12248780cf11SAjit Pandey 
1225923c5e61SMengdong Lin 	/*
1226923c5e61SMengdong Lin 	 * At least one of CPU DAI name or CPU device name/node must be
1227923c5e61SMengdong Lin 	 * specified
1228923c5e61SMengdong Lin 	 */
1229923c5e61SMengdong Lin 	if (!link->cpu_dai_name &&
1230923c5e61SMengdong Lin 	    !(link->cpu_name || link->cpu_of_node)) {
1231923c5e61SMengdong Lin 		dev_err(card->dev,
1232923c5e61SMengdong Lin 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1233923c5e61SMengdong Lin 			link->name);
1234923c5e61SMengdong Lin 		return -EINVAL;
1235923c5e61SMengdong Lin 	}
1236923c5e61SMengdong Lin 
1237923c5e61SMengdong Lin 	return 0;
1238923c5e61SMengdong Lin }
1239923c5e61SMengdong Lin 
1240ef2e8175SKuninori Morimoto void snd_soc_disconnect_sync(struct device *dev)
1241ef2e8175SKuninori Morimoto {
12422c7b696aSMarcel Ziswiler 	struct snd_soc_component *component =
12432c7b696aSMarcel Ziswiler 			snd_soc_lookup_component(dev, NULL);
1244ef2e8175SKuninori Morimoto 
1245ef2e8175SKuninori Morimoto 	if (!component || !component->card)
1246ef2e8175SKuninori Morimoto 		return;
1247ef2e8175SKuninori Morimoto 
1248ef2e8175SKuninori Morimoto 	snd_card_disconnect_sync(component->card->snd_card);
1249ef2e8175SKuninori Morimoto }
1250df532185SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
1251ef2e8175SKuninori Morimoto 
1252f8f80361SMengdong Lin /**
1253f8f80361SMengdong Lin  * snd_soc_add_dai_link - Add a DAI link dynamically
1254f8f80361SMengdong Lin  * @card: The ASoC card to which the DAI link is added
1255f8f80361SMengdong Lin  * @dai_link: The new DAI link to add
1256f8f80361SMengdong Lin  *
1257f8f80361SMengdong Lin  * This function adds a DAI link to the ASoC card's link list.
1258f8f80361SMengdong Lin  *
1259f8f80361SMengdong Lin  * Note: Topology can use this API to add DAI links when probing the
1260f8f80361SMengdong Lin  * topology component. And machine drivers can still define static
1261f8f80361SMengdong Lin  * DAI links in dai_link array.
1262f8f80361SMengdong Lin  */
1263f8f80361SMengdong Lin int snd_soc_add_dai_link(struct snd_soc_card *card,
1264f8f80361SMengdong Lin 		struct snd_soc_dai_link *dai_link)
1265f8f80361SMengdong Lin {
1266f8f80361SMengdong Lin 	if (dai_link->dobj.type
1267f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1268f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1269f8f80361SMengdong Lin 			dai_link->dobj.type);
1270f8f80361SMengdong Lin 		return -EINVAL;
1271f8f80361SMengdong Lin 	}
1272f8f80361SMengdong Lin 
1273f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
12742c7b696aSMarcel Ziswiler 	/*
12752c7b696aSMarcel Ziswiler 	 * Notify the machine driver for extra initialization
1276d6f220eaSMengdong Lin 	 * on the link created by topology.
1277d6f220eaSMengdong Lin 	 */
1278d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->add_dai_link)
1279d6f220eaSMengdong Lin 		card->add_dai_link(card, dai_link);
1280d6f220eaSMengdong Lin 
1281f8f80361SMengdong Lin 	list_add_tail(&dai_link->list, &card->dai_link_list);
1282f8f80361SMengdong Lin 
1283f8f80361SMengdong Lin 	return 0;
1284f8f80361SMengdong Lin }
1285f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1286f8f80361SMengdong Lin 
1287f8f80361SMengdong Lin /**
1288f8f80361SMengdong Lin  * snd_soc_remove_dai_link - Remove a DAI link from the list
1289f8f80361SMengdong Lin  * @card: The ASoC card that owns the link
1290f8f80361SMengdong Lin  * @dai_link: The DAI link to remove
1291f8f80361SMengdong Lin  *
1292f8f80361SMengdong Lin  * This function removes a DAI link from the ASoC card's link list.
1293f8f80361SMengdong Lin  *
1294f8f80361SMengdong Lin  * For DAI links previously added by topology, topology should
1295f8f80361SMengdong Lin  * remove them by using the dobj embedded in the link.
1296f8f80361SMengdong Lin  */
1297f8f80361SMengdong Lin void snd_soc_remove_dai_link(struct snd_soc_card *card,
1298f8f80361SMengdong Lin 			     struct snd_soc_dai_link *dai_link)
1299f8f80361SMengdong Lin {
1300f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
1301f8f80361SMengdong Lin 
1302f8f80361SMengdong Lin 	if (dai_link->dobj.type
1303f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1304f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1305f8f80361SMengdong Lin 			dai_link->dobj.type);
1306f8f80361SMengdong Lin 		return;
1307f8f80361SMengdong Lin 	}
1308f8f80361SMengdong Lin 
1309f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
13102c7b696aSMarcel Ziswiler 	/*
13112c7b696aSMarcel Ziswiler 	 * Notify the machine driver for extra destruction
1312d6f220eaSMengdong Lin 	 * on the link created by topology.
1313d6f220eaSMengdong Lin 	 */
1314d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->remove_dai_link)
1315d6f220eaSMengdong Lin 		card->remove_dai_link(card, dai_link);
1316d6f220eaSMengdong Lin 
131798061fdbSKuninori Morimoto 	for_each_card_links_safe(card, link, _link) {
1318f8f80361SMengdong Lin 		if (link == dai_link) {
1319f8f80361SMengdong Lin 			list_del(&link->list);
1320f8f80361SMengdong Lin 			return;
1321f8f80361SMengdong Lin 		}
1322f8f80361SMengdong Lin 	}
1323f8f80361SMengdong Lin }
1324f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1325f0fba2adSLiam Girdwood 
1326aefba455SJerome Brunet static void soc_set_of_name_prefix(struct snd_soc_component *component)
1327aefba455SJerome Brunet {
1328aefba455SJerome Brunet 	struct device_node *component_of_node = component->dev->of_node;
1329aefba455SJerome Brunet 	const char *str;
1330aefba455SJerome Brunet 	int ret;
1331aefba455SJerome Brunet 
1332aefba455SJerome Brunet 	if (!component_of_node && component->dev->parent)
1333aefba455SJerome Brunet 		component_of_node = component->dev->parent->of_node;
1334aefba455SJerome Brunet 
1335aefba455SJerome Brunet 	ret = of_property_read_string(component_of_node, "sound-name-prefix",
1336aefba455SJerome Brunet 				      &str);
1337aefba455SJerome Brunet 	if (!ret)
1338aefba455SJerome Brunet 		component->name_prefix = str;
1339aefba455SJerome Brunet }
1340aefba455SJerome Brunet 
1341ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
134294f99c87SLars-Peter Clausen 				struct snd_soc_component *component)
1343ead9b919SJarkko Nikula {
1344ead9b919SJarkko Nikula 	int i;
1345ead9b919SJarkko Nikula 
1346aefba455SJerome Brunet 	for (i = 0; i < card->num_configs && card->codec_conf; i++) {
1347ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1348b24c539bSCharles Keepax 		struct device_node *component_of_node = component->dev->of_node;
1349b24c539bSCharles Keepax 
1350b24c539bSCharles Keepax 		if (!component_of_node && component->dev->parent)
1351b24c539bSCharles Keepax 			component_of_node = component->dev->parent->of_node;
1352b24c539bSCharles Keepax 
1353b24c539bSCharles Keepax 		if (map->of_node && component_of_node != map->of_node)
13543ca041edSSebastian Reichel 			continue;
135594f99c87SLars-Peter Clausen 		if (map->dev_name && strcmp(component->name, map->dev_name))
13563ca041edSSebastian Reichel 			continue;
135794f99c87SLars-Peter Clausen 		component->name_prefix = map->name_prefix;
1358aefba455SJerome Brunet 		return;
1359ead9b919SJarkko Nikula 	}
1360aefba455SJerome Brunet 
1361aefba455SJerome Brunet 	/*
1362aefba455SJerome Brunet 	 * If there is no configuration table or no match in the table,
1363aefba455SJerome Brunet 	 * check if a prefix is provided in the node
1364aefba455SJerome Brunet 	 */
1365aefba455SJerome Brunet 	soc_set_of_name_prefix(component);
1366ead9b919SJarkko Nikula }
1367ead9b919SJarkko Nikula 
1368f1d45cc3SLars-Peter Clausen static int soc_probe_component(struct snd_soc_card *card,
1369f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component)
1370589c3563SJarkko Nikula {
13712c7b696aSMarcel Ziswiler 	struct snd_soc_dapm_context *dapm =
13722c7b696aSMarcel Ziswiler 			snd_soc_component_get_dapm(component);
1373888df395SMark Brown 	struct snd_soc_dai *dai;
1374f1d45cc3SLars-Peter Clausen 	int ret;
1375589c3563SJarkko Nikula 
13761b7c1231SLars-Peter Clausen 	if (!strcmp(component->name, "snd-soc-dummy"))
137770090bbbSLars-Peter Clausen 		return 0;
1378589c3563SJarkko Nikula 
1379abd31b32SLars-Peter Clausen 	if (component->card) {
13801b7c1231SLars-Peter Clausen 		if (component->card != card) {
13811b7c1231SLars-Peter Clausen 			dev_err(component->dev,
13821b7c1231SLars-Peter Clausen 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
13831b7c1231SLars-Peter Clausen 				card->name, component->card->name);
13841b7c1231SLars-Peter Clausen 			return -ENODEV;
13851b7c1231SLars-Peter Clausen 		}
13861b7c1231SLars-Peter Clausen 		return 0;
13871b7c1231SLars-Peter Clausen 	}
13881b7c1231SLars-Peter Clausen 
1389b4ed6b51SRanjani Sridharan 	if (!component->driver->module_get_upon_open &&
1390b450b878SPierre-Louis Bossart 	    !try_module_get(component->dev->driver->owner))
1391abd31b32SLars-Peter Clausen 		return -ENODEV;
1392abd31b32SLars-Peter Clausen 
1393f1d45cc3SLars-Peter Clausen 	component->card = card;
1394f1d45cc3SLars-Peter Clausen 	dapm->card = card;
139522d14231SKuninori Morimoto 	INIT_LIST_HEAD(&component->card_list);
139622d14231SKuninori Morimoto 	INIT_LIST_HEAD(&dapm->list);
1397f1d45cc3SLars-Peter Clausen 	soc_set_name_prefix(card, component);
1398589c3563SJarkko Nikula 
1399f1d45cc3SLars-Peter Clausen 	soc_init_component_debugfs(component);
1400d5d1e0beSLars-Peter Clausen 
1401688d0ebfSKuninori Morimoto 	if (component->driver->dapm_widgets) {
1402688d0ebfSKuninori Morimoto 		ret = snd_soc_dapm_new_controls(dapm,
1403688d0ebfSKuninori Morimoto 					component->driver->dapm_widgets,
1404688d0ebfSKuninori Morimoto 					component->driver->num_dapm_widgets);
140577530150SLars-Peter Clausen 
1406b318ad50SNariman Poushin 		if (ret != 0) {
1407f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1408b318ad50SNariman Poushin 				"Failed to create new controls %d\n", ret);
1409b318ad50SNariman Poushin 			goto err_probe;
1410b318ad50SNariman Poushin 		}
1411b318ad50SNariman Poushin 	}
1412b318ad50SNariman Poushin 
141315a0c645SKuninori Morimoto 	for_each_component_dais(component, dai) {
14140634814fSLars-Peter Clausen 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1415261edc70SNariman Poushin 		if (ret != 0) {
1416f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1417261edc70SNariman Poushin 				"Failed to create DAI widgets %d\n", ret);
1418261edc70SNariman Poushin 			goto err_probe;
1419261edc70SNariman Poushin 		}
1420261edc70SNariman Poushin 	}
1421888df395SMark Brown 
1422999f7f5aSKuninori Morimoto 	if (component->driver->probe) {
1423999f7f5aSKuninori Morimoto 		ret = component->driver->probe(component);
1424589c3563SJarkko Nikula 		if (ret < 0) {
1425f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1426f1d45cc3SLars-Peter Clausen 				"ASoC: failed to probe component %d\n", ret);
142770d29331SJarkko Nikula 			goto err_probe;
1428589c3563SJarkko Nikula 		}
1429cb2cf612SLiam Girdwood 
1430f1d45cc3SLars-Peter Clausen 		WARN(dapm->idle_bias_off &&
1431f1d45cc3SLars-Peter Clausen 			dapm->bias_level != SND_SOC_BIAS_OFF,
1432956245e9SLiam Girdwood 			"codec %s can not start from non-off bias with idle_bias_off==1\n",
1433f1d45cc3SLars-Peter Clausen 			component->name);
1434956245e9SLiam Girdwood 	}
1435956245e9SLiam Girdwood 
1436f2ed6b07SMengdong Lin 	/* machine specific init */
1437f2ed6b07SMengdong Lin 	if (component->init) {
1438f2ed6b07SMengdong Lin 		ret = component->init(component);
1439f2ed6b07SMengdong Lin 		if (ret < 0) {
1440f2ed6b07SMengdong Lin 			dev_err(component->dev,
1441f2ed6b07SMengdong Lin 				"Failed to do machine specific init %d\n", ret);
1442f2ed6b07SMengdong Lin 			goto err_probe;
1443f2ed6b07SMengdong Lin 		}
1444f2ed6b07SMengdong Lin 	}
1445f2ed6b07SMengdong Lin 
1446b8972bf0SKuninori Morimoto 	if (component->driver->controls)
1447b8972bf0SKuninori Morimoto 		snd_soc_add_component_controls(component,
1448b8972bf0SKuninori Morimoto 					       component->driver->controls,
1449b8972bf0SKuninori Morimoto 					       component->driver->num_controls);
14506969b2baSKuninori Morimoto 	if (component->driver->dapm_routes)
14516969b2baSKuninori Morimoto 		snd_soc_dapm_add_routes(dapm,
14526969b2baSKuninori Morimoto 					component->driver->dapm_routes,
14536969b2baSKuninori Morimoto 					component->driver->num_dapm_routes);
1454956245e9SLiam Girdwood 
1455f1d45cc3SLars-Peter Clausen 	list_add(&dapm->list, &card->dapm_list);
1456f70f18f7SKuninori Morimoto 	/* see for_each_card_components */
1457d9fc4063SKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1458956245e9SLiam Girdwood 
1459956245e9SLiam Girdwood err_probe:
146022d14231SKuninori Morimoto 	if (ret < 0)
146122d14231SKuninori Morimoto 		soc_cleanup_component(component);
1462956245e9SLiam Girdwood 
1463956245e9SLiam Girdwood 	return ret;
1464956245e9SLiam Girdwood }
1465956245e9SLiam Girdwood 
146636ae1a96SMark Brown static void rtd_release(struct device *dev)
146736ae1a96SMark Brown {
146836ae1a96SMark Brown 	kfree(dev);
146936ae1a96SMark Brown }
1470f0fba2adSLiam Girdwood 
14715f3484acSLars-Peter Clausen static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
14725f3484acSLars-Peter Clausen 	const char *name)
1473503ae5e0SMisael Lopez Cruz {
1474589c3563SJarkko Nikula 	int ret = 0;
1475589c3563SJarkko Nikula 
1476589c3563SJarkko Nikula 	/* register the rtd device */
147736ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
147836ae1a96SMark Brown 	if (!rtd->dev)
147936ae1a96SMark Brown 		return -ENOMEM;
148036ae1a96SMark Brown 	device_initialize(rtd->dev);
14815f3484acSLars-Peter Clausen 	rtd->dev->parent = rtd->card->dev;
148236ae1a96SMark Brown 	rtd->dev->release = rtd_release;
1483d29697dcSTakashi Iwai 	rtd->dev->groups = soc_dev_attr_groups;
1484f294afedSLars-Peter Clausen 	dev_set_name(rtd->dev, "%s", name);
148536ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1486b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
148701d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
148801d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
148901d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
149001d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
149136ae1a96SMark Brown 	ret = device_add(rtd->dev);
1492589c3563SJarkko Nikula 	if (ret < 0) {
1493865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1494865df9cbSChuansheng Liu 		put_device(rtd->dev);
14955f3484acSLars-Peter Clausen 		dev_err(rtd->card->dev,
1496f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1497589c3563SJarkko Nikula 		return ret;
1498589c3563SJarkko Nikula 	}
1499589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1500589c3563SJarkko Nikula 	return 0;
1501589c3563SJarkko Nikula }
1502589c3563SJarkko Nikula 
15031a497983SMengdong Lin static int soc_probe_link_components(struct snd_soc_card *card,
15042c7b696aSMarcel Ziswiler 				     struct snd_soc_pcm_runtime *rtd, int order)
150562ae68faSStephen Warren {
1506f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component;
150790be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
150890be711eSKuninori Morimoto 	int ret;
150962ae68faSStephen Warren 
151090be711eSKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
151190be711eSKuninori Morimoto 		component = rtdcom->component;
151290be711eSKuninori Morimoto 
151370090bbbSLars-Peter Clausen 		if (component->driver->probe_order == order) {
1514f1d45cc3SLars-Peter Clausen 			ret = soc_probe_component(card, component);
151562ae68faSStephen Warren 			if (ret < 0)
151662ae68faSStephen Warren 				return ret;
151762ae68faSStephen Warren 		}
151862ae68faSStephen Warren 	}
151962ae68faSStephen Warren 
152062ae68faSStephen Warren 	return 0;
152162ae68faSStephen Warren }
152262ae68faSStephen Warren 
15238e2be562SLars-Peter Clausen static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1524b0aa88afSMisael Lopez Cruz {
15257a2ccad5SKuninori Morimoto 	if (dai->probed ||
15267a2ccad5SKuninori Morimoto 	    dai->driver->probe_order != order)
15277a2ccad5SKuninori Morimoto 		return 0;
1528b0aa88afSMisael Lopez Cruz 
15298e2be562SLars-Peter Clausen 	if (dai->driver->probe) {
15307a2ccad5SKuninori Morimoto 		int ret = dai->driver->probe(dai);
15312c7b696aSMarcel Ziswiler 
1532b0aa88afSMisael Lopez Cruz 		if (ret < 0) {
15337a2ccad5SKuninori Morimoto 			dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
15348e2be562SLars-Peter Clausen 				dai->name, ret);
1535b0aa88afSMisael Lopez Cruz 			return ret;
1536b0aa88afSMisael Lopez Cruz 		}
1537b0aa88afSMisael Lopez Cruz 	}
1538b0aa88afSMisael Lopez Cruz 
15398e2be562SLars-Peter Clausen 	dai->probed = 1;
1540b0aa88afSMisael Lopez Cruz 
1541b0aa88afSMisael Lopez Cruz 	return 0;
1542b0aa88afSMisael Lopez Cruz }
1543b0aa88afSMisael Lopez Cruz 
154425f7b701SArnaud Pouliquen static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
154525f7b701SArnaud Pouliquen 				struct snd_soc_pcm_runtime *rtd)
154625f7b701SArnaud Pouliquen {
154725f7b701SArnaud Pouliquen 	int i, ret = 0;
154825f7b701SArnaud Pouliquen 
154925f7b701SArnaud Pouliquen 	for (i = 0; i < num_dais; ++i) {
155025f7b701SArnaud Pouliquen 		struct snd_soc_dai_driver *drv = dais[i]->driver;
155125f7b701SArnaud Pouliquen 
1552de17f14eSRohit kumar 		if (drv->pcm_new)
155325f7b701SArnaud Pouliquen 			ret = drv->pcm_new(rtd, dais[i]);
155425f7b701SArnaud Pouliquen 		if (ret < 0) {
155525f7b701SArnaud Pouliquen 			dev_err(dais[i]->dev,
155625f7b701SArnaud Pouliquen 				"ASoC: Failed to bind %s with pcm device\n",
155725f7b701SArnaud Pouliquen 				dais[i]->name);
155825f7b701SArnaud Pouliquen 			return ret;
155925f7b701SArnaud Pouliquen 		}
156025f7b701SArnaud Pouliquen 	}
156125f7b701SArnaud Pouliquen 
156225f7b701SArnaud Pouliquen 	return 0;
156325f7b701SArnaud Pouliquen }
156425f7b701SArnaud Pouliquen 
15651a497983SMengdong Lin static int soc_probe_link_dais(struct snd_soc_card *card,
15661a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1567f0fba2adSLiam Girdwood {
15681a497983SMengdong Lin 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1569c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1570a655de80SLiam Girdwood 	struct snd_soc_rtdcom_list *rtdcom;
1571a655de80SLiam Girdwood 	struct snd_soc_component *component;
15720b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1573a655de80SLiam Girdwood 	int i, ret, num;
1574f0fba2adSLiam Girdwood 
1575f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
15761a497983SMengdong Lin 			card->name, rtd->num, order);
1577f0fba2adSLiam Girdwood 
1578f0fba2adSLiam Girdwood 	/* set default power off timeout */
1579f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1580f0fba2adSLiam Girdwood 
15818e2be562SLars-Peter Clausen 	ret = soc_probe_dai(cpu_dai, order);
15828e2be562SLars-Peter Clausen 	if (ret)
1583f0fba2adSLiam Girdwood 		return ret;
1584f0fba2adSLiam Girdwood 
1585f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
15860b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
15870b7990e3SKuninori Morimoto 		ret = soc_probe_dai(codec_dai, order);
1588b0aa88afSMisael Lopez Cruz 		if (ret)
1589f0fba2adSLiam Girdwood 			return ret;
159088bd870fSBenoit Cousson 	}
1591f0fba2adSLiam Girdwood 
15920168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
15930168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
15940168bf0dSLiam Girdwood 		return 0;
15950168bf0dSLiam Girdwood 
15965f3484acSLars-Peter Clausen 	/* do machine specific initialization */
15975f3484acSLars-Peter Clausen 	if (dai_link->init) {
15985f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
15995f3484acSLars-Peter Clausen 		if (ret < 0) {
16005f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
16015f3484acSLars-Peter Clausen 				dai_link->name, ret);
16025f3484acSLars-Peter Clausen 			return ret;
16035f3484acSLars-Peter Clausen 		}
16045f3484acSLars-Peter Clausen 	}
16055f3484acSLars-Peter Clausen 
1606a5053a8eSKuninori Morimoto 	if (dai_link->dai_fmt)
1607a5053a8eSKuninori Morimoto 		snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1608a5053a8eSKuninori Morimoto 
16095f3484acSLars-Peter Clausen 	ret = soc_post_component_init(rtd, dai_link->name);
1610589c3563SJarkko Nikula 	if (ret)
1611f0fba2adSLiam Girdwood 		return ret;
1612f0fba2adSLiam Girdwood 
16135f3484acSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
16145f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
16152e55b90aSLars-Peter Clausen 	if (dai_link->dynamic)
16162e55b90aSLars-Peter Clausen 		soc_dpcm_debugfs_add(rtd);
16175f3484acSLars-Peter Clausen #endif
16185f3484acSLars-Peter Clausen 
1619a655de80SLiam Girdwood 	num = rtd->num;
1620a655de80SLiam Girdwood 
1621a655de80SLiam Girdwood 	/*
1622a655de80SLiam Girdwood 	 * most drivers will register their PCMs using DAI link ordering but
1623a655de80SLiam Girdwood 	 * topology based drivers can use the DAI link id field to set PCM
1624a655de80SLiam Girdwood 	 * device number and then use rtd + a base offset of the BEs.
1625a655de80SLiam Girdwood 	 */
1626a655de80SLiam Girdwood 	for_each_rtdcom(rtd, rtdcom) {
1627a655de80SLiam Girdwood 		component = rtdcom->component;
1628a655de80SLiam Girdwood 
1629a655de80SLiam Girdwood 		if (!component->driver->use_dai_pcm_id)
1630a655de80SLiam Girdwood 			continue;
1631a655de80SLiam Girdwood 
1632a655de80SLiam Girdwood 		if (rtd->dai_link->no_pcm)
1633a655de80SLiam Girdwood 			num += component->driver->be_pcm_base;
1634a655de80SLiam Girdwood 		else
1635a655de80SLiam Girdwood 			num = rtd->dai_link->id;
1636a655de80SLiam Girdwood 	}
1637a655de80SLiam Girdwood 
16386f0c4226SJie Yang 	if (cpu_dai->driver->compress_new) {
16391245b700SNamarta Kohli 		/* create compress_device" */
1640a655de80SLiam Girdwood 		ret = cpu_dai->driver->compress_new(rtd, num);
16411245b700SNamarta Kohli 		if (ret < 0) {
1642f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
16431245b700SNamarta Kohli 					 dai_link->stream_name);
16441245b700SNamarta Kohli 			return ret;
16451245b700SNamarta Kohli 		}
164652293596SKuninori Morimoto 	} else if (!dai_link->params) {
1647f0fba2adSLiam Girdwood 		/* create the pcm */
1648a655de80SLiam Girdwood 		ret = soc_new_pcm(rtd, num);
1649f0fba2adSLiam Girdwood 		if (ret < 0) {
1650f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
16510837fc62SFabio Estevam 				dai_link->stream_name, ret);
1652f0fba2adSLiam Girdwood 			return ret;
1653f0fba2adSLiam Girdwood 		}
165425f7b701SArnaud Pouliquen 		ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
165525f7b701SArnaud Pouliquen 		if (ret < 0)
165625f7b701SArnaud Pouliquen 			return ret;
165725f7b701SArnaud Pouliquen 		ret = soc_link_dai_pcm_new(rtd->codec_dais,
165825f7b701SArnaud Pouliquen 					   rtd->num_codecs, rtd);
165925f7b701SArnaud Pouliquen 		if (ret < 0)
166025f7b701SArnaud Pouliquen 			return ret;
1661c74184edSMark Brown 	} else {
16629d58a077SRichard Fitzgerald 		INIT_DELAYED_WORK(&rtd->delayed_work,
16639d58a077SRichard Fitzgerald 				  codec2codec_close_delayed_work);
1664c74184edSMark Brown 	}
1665c74184edSMark Brown 
1666f0fba2adSLiam Girdwood 	return 0;
1667f0fba2adSLiam Girdwood }
1668f0fba2adSLiam Girdwood 
166944c69bb1SLars-Peter Clausen static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1670b19e6e7bSMark Brown {
16713ca041edSSebastian Reichel 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1672f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1673f2ed6b07SMengdong Lin 	const char *name;
1674f2ed6b07SMengdong Lin 	struct device_node *codec_of_node;
16753ca041edSSebastian Reichel 
1676f2ed6b07SMengdong Lin 	if (aux_dev->codec_of_node || aux_dev->codec_name) {
1677f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1678f2ed6b07SMengdong Lin 		name = aux_dev->codec_name;
1679f2ed6b07SMengdong Lin 		codec_of_node = aux_dev->codec_of_node;
1680f2ed6b07SMengdong Lin 		component = soc_find_component(codec_of_node, name);
1681f2ed6b07SMengdong Lin 		if (!component) {
1682f2ed6b07SMengdong Lin 			if (codec_of_node)
1683f2ed6b07SMengdong Lin 				name = of_node_full_name(codec_of_node);
1684f2ed6b07SMengdong Lin 			goto err_defer;
1685f2ed6b07SMengdong Lin 		}
1686f2ed6b07SMengdong Lin 	} else if (aux_dev->name) {
1687f2ed6b07SMengdong Lin 		/* generic components */
1688f2ed6b07SMengdong Lin 		name = aux_dev->name;
1689f2ed6b07SMengdong Lin 		component = soc_find_component(NULL, name);
1690f2ed6b07SMengdong Lin 		if (!component)
1691f2ed6b07SMengdong Lin 			goto err_defer;
1692f2ed6b07SMengdong Lin 	} else {
1693f2ed6b07SMengdong Lin 		dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1694f2ed6b07SMengdong Lin 		return -EINVAL;
1695f2ed6b07SMengdong Lin 	}
16963ca041edSSebastian Reichel 
1697f2ed6b07SMengdong Lin 	component->init = aux_dev->init;
1698d2e3a135SSylwester Nawrocki 	list_add(&component->card_aux_list, &card->aux_comp_list);
16991a653aa4SKuninori Morimoto 
1700f2ed6b07SMengdong Lin 	return 0;
1701f2ed6b07SMengdong Lin 
1702f2ed6b07SMengdong Lin err_defer:
170365d9361fSLars-Peter Clausen 	dev_err(card->dev, "ASoC: %s not registered\n", name);
1704b19e6e7bSMark Brown 	return -EPROBE_DEFER;
1705b19e6e7bSMark Brown }
1706b19e6e7bSMark Brown 
1707f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1708f2ed6b07SMengdong Lin {
1709991454e1SKuninori Morimoto 	struct snd_soc_component *comp;
1710f2ed6b07SMengdong Lin 	int order;
1711f2ed6b07SMengdong Lin 	int ret;
1712f2ed6b07SMengdong Lin 
17131a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1714991454e1SKuninori Morimoto 		list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
1715f2ed6b07SMengdong Lin 			if (comp->driver->probe_order == order) {
1716f2ed6b07SMengdong Lin 				ret = soc_probe_component(card,	comp);
1717f2ed6b07SMengdong Lin 				if (ret < 0) {
1718f2ed6b07SMengdong Lin 					dev_err(card->dev,
1719f2ed6b07SMengdong Lin 						"ASoC: failed to probe aux component %s %d\n",
1720f2ed6b07SMengdong Lin 						comp->name, ret);
1721f2ed6b07SMengdong Lin 					return ret;
1722f2ed6b07SMengdong Lin 				}
1723f2ed6b07SMengdong Lin 			}
1724f2ed6b07SMengdong Lin 		}
1725f2ed6b07SMengdong Lin 	}
172665d9361fSLars-Peter Clausen 
172744c69bb1SLars-Peter Clausen 	return 0;
17283ca041edSSebastian Reichel }
17292eea392dSJarkko Nikula 
1730f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
173144c69bb1SLars-Peter Clausen {
1732f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1733f2ed6b07SMengdong Lin 	int order;
173444c69bb1SLars-Peter Clausen 
17351a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1736f2ed6b07SMengdong Lin 		list_for_each_entry_safe(comp, _comp,
1737991454e1SKuninori Morimoto 			&card->aux_comp_list, card_aux_list) {
17381a653aa4SKuninori Morimoto 
1739f2ed6b07SMengdong Lin 			if (comp->driver->remove_order == order) {
1740f2ed6b07SMengdong Lin 				soc_remove_component(comp);
1741991454e1SKuninori Morimoto 				/* remove it from the card's aux_comp_list */
1742991454e1SKuninori Morimoto 				list_del(&comp->card_aux_list);
17432eea392dSJarkko Nikula 			}
17445f3484acSLars-Peter Clausen 		}
17452eea392dSJarkko Nikula 	}
17462eea392dSJarkko Nikula }
17472eea392dSJarkko Nikula 
1748ce64c8b9SLars-Peter Clausen /**
1749ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1750ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1751ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1752ce64c8b9SLars-Peter Clausen  *
1753ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1754ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1755ce64c8b9SLars-Peter Clausen  *
1756ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1757ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1758ce64c8b9SLars-Peter Clausen  *
1759ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1760ce64c8b9SLars-Peter Clausen  */
1761ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1762ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1763ce64c8b9SLars-Peter Clausen {
1764ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
17650b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1766ce64c8b9SLars-Peter Clausen 	unsigned int i;
1767ce64c8b9SLars-Peter Clausen 	int ret;
1768ce64c8b9SLars-Peter Clausen 
17690b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1770ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1771ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1772ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1773ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1774ce64c8b9SLars-Peter Clausen 			return ret;
1775ce64c8b9SLars-Peter Clausen 		}
1776ce64c8b9SLars-Peter Clausen 	}
1777ce64c8b9SLars-Peter Clausen 
17782c7b696aSMarcel Ziswiler 	/*
17792c7b696aSMarcel Ziswiler 	 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
17802c7b696aSMarcel Ziswiler 	 * the component which has non_legacy_dai_naming is Codec
17812c7b696aSMarcel Ziswiler 	 */
1782999f7f5aSKuninori Morimoto 	if (cpu_dai->component->driver->non_legacy_dai_naming) {
1783ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1784ce64c8b9SLars-Peter Clausen 
1785ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1786ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1787ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1788ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1789ce64c8b9SLars-Peter Clausen 			break;
1790ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1791ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1792ce64c8b9SLars-Peter Clausen 			break;
1793ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1794ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1795ce64c8b9SLars-Peter Clausen 			break;
1796ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1797ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1798ce64c8b9SLars-Peter Clausen 			break;
1799ce64c8b9SLars-Peter Clausen 		}
1800ce64c8b9SLars-Peter Clausen 
1801ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1802ce64c8b9SLars-Peter Clausen 	}
1803ce64c8b9SLars-Peter Clausen 
1804ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1805ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1806ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1807ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1808ce64c8b9SLars-Peter Clausen 		return ret;
1809ce64c8b9SLars-Peter Clausen 	}
1810ce64c8b9SLars-Peter Clausen 
1811ce64c8b9SLars-Peter Clausen 	return 0;
1812ce64c8b9SLars-Peter Clausen }
1813ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1814ce64c8b9SLars-Peter Clausen 
18151f5a4535STakashi Iwai #ifdef CONFIG_DMI
18162c7b696aSMarcel Ziswiler /*
18172c7b696aSMarcel Ziswiler  * Trim special characters, and replace '-' with '_' since '-' is used to
1818345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1819345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1820345233d7SLiam Girdwood  */
1821345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1822345233d7SLiam Girdwood {
1823345233d7SLiam Girdwood 	int i, j = 0;
1824345233d7SLiam Girdwood 
1825345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1826345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1827345233d7SLiam Girdwood 		    || (name[i] == '_'))
1828345233d7SLiam Girdwood 			name[j++] = name[i];
1829345233d7SLiam Girdwood 		else if (name[i] == '-')
1830345233d7SLiam Girdwood 			name[j++] = '_';
1831345233d7SLiam Girdwood 	}
1832345233d7SLiam Girdwood 
1833345233d7SLiam Girdwood 	name[j] = '\0';
1834345233d7SLiam Girdwood }
1835345233d7SLiam Girdwood 
18362c7b696aSMarcel Ziswiler /*
18372c7b696aSMarcel Ziswiler  * Check if a DMI field is valid, i.e. not containing any string
183898faf436SMengdong Lin  * in the black list.
183998faf436SMengdong Lin  */
184098faf436SMengdong Lin static int is_dmi_valid(const char *field)
184198faf436SMengdong Lin {
184298faf436SMengdong Lin 	int i = 0;
184398faf436SMengdong Lin 
184498faf436SMengdong Lin 	while (dmi_blacklist[i]) {
184598faf436SMengdong Lin 		if (strstr(field, dmi_blacklist[i]))
184698faf436SMengdong Lin 			return 0;
184798faf436SMengdong Lin 		i++;
184846b5a4d2SWu Fengguang 	}
184998faf436SMengdong Lin 
185098faf436SMengdong Lin 	return 1;
185198faf436SMengdong Lin }
185298faf436SMengdong Lin 
1853345233d7SLiam Girdwood /**
1854345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
1855345233d7SLiam Girdwood  * @card: The card to register DMI names
1856345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
1857345233d7SLiam Girdwood  *
1858345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
1859345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
1860345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
1861345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
1862345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
1863345233d7SLiam Girdwood  * as the sound card long name, in the format of
1864345233d7SLiam Girdwood  * "vendor-product-version-board"
1865345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
1866345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
1867345233d7SLiam Girdwood  * (UCM) configurations for the card.
1868345233d7SLiam Girdwood  *
1869345233d7SLiam Girdwood  * Possible card long names may be:
1870345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
1871345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1872345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1873345233d7SLiam Girdwood  *
1874345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
1875345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
1876345233d7SLiam Girdwood  *
1877345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
1878345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
1879345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
1880345233d7SLiam Girdwood  * charactors like SPACE.
1881345233d7SLiam Girdwood  *
1882345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
1883345233d7SLiam Girdwood  */
1884345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1885345233d7SLiam Girdwood {
1886345233d7SLiam Girdwood 	const char *vendor, *product, *product_version, *board;
1887345233d7SLiam Girdwood 	size_t longname_buf_size = sizeof(card->snd_card->longname);
1888345233d7SLiam Girdwood 	size_t len;
1889345233d7SLiam Girdwood 
1890345233d7SLiam Girdwood 	if (card->long_name)
1891345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
1892345233d7SLiam Girdwood 
1893345233d7SLiam Girdwood 	/* make up dmi long name as: vendor.product.version.board */
1894345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
189598faf436SMengdong Lin 	if (!vendor || !is_dmi_valid(vendor)) {
1896345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1897345233d7SLiam Girdwood 		return 0;
1898345233d7SLiam Girdwood 	}
1899345233d7SLiam Girdwood 
1900345233d7SLiam Girdwood 	snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1901345233d7SLiam Girdwood 			 "%s", vendor);
1902345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
1903345233d7SLiam Girdwood 
1904345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
190598faf436SMengdong Lin 	if (product && is_dmi_valid(product)) {
1906345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1907345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1908345233d7SLiam Girdwood 			 longname_buf_size - len,
1909345233d7SLiam Girdwood 			 "-%s", product);
1910345233d7SLiam Girdwood 
1911345233d7SLiam Girdwood 		len++;	/* skip the separator "-" */
1912345233d7SLiam Girdwood 		if (len < longname_buf_size)
1913345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1914345233d7SLiam Girdwood 
19152c7b696aSMarcel Ziswiler 		/*
19162c7b696aSMarcel Ziswiler 		 * some vendors like Lenovo may only put a self-explanatory
1917345233d7SLiam Girdwood 		 * name in the product version field
1918345233d7SLiam Girdwood 		 */
1919345233d7SLiam Girdwood 		product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
192098faf436SMengdong Lin 		if (product_version && is_dmi_valid(product_version)) {
1921345233d7SLiam Girdwood 			len = strlen(card->dmi_longname);
1922345233d7SLiam Girdwood 			snprintf(card->dmi_longname + len,
1923345233d7SLiam Girdwood 				 longname_buf_size - len,
1924345233d7SLiam Girdwood 				 "-%s", product_version);
1925345233d7SLiam Girdwood 
1926345233d7SLiam Girdwood 			len++;
1927345233d7SLiam Girdwood 			if (len < longname_buf_size)
1928345233d7SLiam Girdwood 				cleanup_dmi_name(card->dmi_longname + len);
1929345233d7SLiam Girdwood 		}
1930345233d7SLiam Girdwood 	}
1931345233d7SLiam Girdwood 
1932345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
193398faf436SMengdong Lin 	if (board && is_dmi_valid(board)) {
1934345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1935345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1936345233d7SLiam Girdwood 			 longname_buf_size - len,
1937345233d7SLiam Girdwood 			 "-%s", board);
1938345233d7SLiam Girdwood 
1939345233d7SLiam Girdwood 		len++;
1940345233d7SLiam Girdwood 		if (len < longname_buf_size)
1941345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1942345233d7SLiam Girdwood 	} else if (!product) {
1943345233d7SLiam Girdwood 		/* fall back to using legacy name */
1944345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1945345233d7SLiam Girdwood 		return 0;
1946345233d7SLiam Girdwood 	}
1947345233d7SLiam Girdwood 
1948345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
1949345233d7SLiam Girdwood 	if (flavour) {
1950345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1951345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1952345233d7SLiam Girdwood 			 longname_buf_size - len,
1953345233d7SLiam Girdwood 			 "-%s", flavour);
1954345233d7SLiam Girdwood 
1955345233d7SLiam Girdwood 		len++;
1956345233d7SLiam Girdwood 		if (len < longname_buf_size)
1957345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1958345233d7SLiam Girdwood 	}
1959345233d7SLiam Girdwood 
1960345233d7SLiam Girdwood 	/* set the card long name */
1961345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
1962345233d7SLiam Girdwood 
1963345233d7SLiam Girdwood 	return 0;
1964345233d7SLiam Girdwood }
1965345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
19661f5a4535STakashi Iwai #endif /* CONFIG_DMI */
1967345233d7SLiam Girdwood 
1968a655de80SLiam Girdwood static void soc_check_tplg_fes(struct snd_soc_card *card)
1969a655de80SLiam Girdwood {
1970a655de80SLiam Girdwood 	struct snd_soc_component *component;
1971a655de80SLiam Girdwood 	const struct snd_soc_component_driver *comp_drv;
1972a655de80SLiam Girdwood 	struct snd_soc_dai_link *dai_link;
1973a655de80SLiam Girdwood 	int i;
1974a655de80SLiam Girdwood 
1975368dee94SKuninori Morimoto 	for_each_component(component) {
1976a655de80SLiam Girdwood 
1977a655de80SLiam Girdwood 		/* does this component override FEs ? */
1978a655de80SLiam Girdwood 		if (!component->driver->ignore_machine)
1979a655de80SLiam Girdwood 			continue;
1980a655de80SLiam Girdwood 
1981a655de80SLiam Girdwood 		/* for this machine ? */
1982e194098bSPierre-Louis Bossart 		if (!strcmp(component->driver->ignore_machine,
1983a655de80SLiam Girdwood 			    card->dev->driver->name))
1984e194098bSPierre-Louis Bossart 			goto match;
1985e194098bSPierre-Louis Bossart 		if (strcmp(component->driver->ignore_machine,
1986e194098bSPierre-Louis Bossart 			   dev_name(card->dev)))
1987a655de80SLiam Girdwood 			continue;
1988e194098bSPierre-Louis Bossart match:
1989a655de80SLiam Girdwood 		/* machine matches, so override the rtd data */
19907fe072b4SKuninori Morimoto 		for_each_card_prelinks(card, i, dai_link) {
1991a655de80SLiam Girdwood 
1992a655de80SLiam Girdwood 			/* ignore this FE */
1993a655de80SLiam Girdwood 			if (dai_link->dynamic) {
1994a655de80SLiam Girdwood 				dai_link->ignore = true;
1995a655de80SLiam Girdwood 				continue;
1996a655de80SLiam Girdwood 			}
1997a655de80SLiam Girdwood 
1998a655de80SLiam Girdwood 			dev_info(card->dev, "info: override FE DAI link %s\n",
1999a655de80SLiam Girdwood 				 card->dai_link[i].name);
2000a655de80SLiam Girdwood 
2001a655de80SLiam Girdwood 			/* override platform component */
2002daecf46eSKuninori Morimoto 			if (snd_soc_init_platform(card, dai_link) < 0) {
2003daecf46eSKuninori Morimoto 				dev_err(card->dev, "init platform error");
2004daecf46eSKuninori Morimoto 				continue;
2005daecf46eSKuninori Morimoto 			}
2006910fdcabSKuninori Morimoto 			dai_link->platforms->name = component->name;
2007a655de80SLiam Girdwood 
2008a655de80SLiam Girdwood 			/* convert non BE into BE */
2009a655de80SLiam Girdwood 			dai_link->no_pcm = 1;
2010a655de80SLiam Girdwood 
2011a655de80SLiam Girdwood 			/* override any BE fixups */
2012a655de80SLiam Girdwood 			dai_link->be_hw_params_fixup =
2013a655de80SLiam Girdwood 				component->driver->be_hw_params_fixup;
2014a655de80SLiam Girdwood 
20152c7b696aSMarcel Ziswiler 			/*
20162c7b696aSMarcel Ziswiler 			 * most BE links don't set stream name, so set it to
2017a655de80SLiam Girdwood 			 * dai link name if it's NULL to help bind widgets.
2018a655de80SLiam Girdwood 			 */
2019a655de80SLiam Girdwood 			if (!dai_link->stream_name)
2020a655de80SLiam Girdwood 				dai_link->stream_name = dai_link->name;
2021a655de80SLiam Girdwood 		}
2022a655de80SLiam Girdwood 
2023a655de80SLiam Girdwood 		/* Inform userspace we are using alternate topology */
2024a655de80SLiam Girdwood 		if (component->driver->topology_name_prefix) {
2025a655de80SLiam Girdwood 
2026a655de80SLiam Girdwood 			/* topology shortname created? */
2027a655de80SLiam Girdwood 			if (!card->topology_shortname_created) {
2028a655de80SLiam Girdwood 				comp_drv = component->driver;
2029a655de80SLiam Girdwood 
2030a655de80SLiam Girdwood 				snprintf(card->topology_shortname, 32, "%s-%s",
2031a655de80SLiam Girdwood 					 comp_drv->topology_name_prefix,
2032a655de80SLiam Girdwood 					 card->name);
2033a655de80SLiam Girdwood 				card->topology_shortname_created = true;
2034a655de80SLiam Girdwood 			}
2035a655de80SLiam Girdwood 
2036a655de80SLiam Girdwood 			/* use topology shortname */
2037a655de80SLiam Girdwood 			card->name = card->topology_shortname;
2038a655de80SLiam Girdwood 		}
2039a655de80SLiam Girdwood 	}
2040a655de80SLiam Girdwood }
2041a655de80SLiam Girdwood 
204253e947a0SKuninori Morimoto static int soc_cleanup_card_resources(struct snd_soc_card *card)
204353e947a0SKuninori Morimoto {
204453e947a0SKuninori Morimoto 	/* free the ALSA card at first; this syncs with pending operations */
2045*29040d1aSKuninori Morimoto 	if (card->snd_card) {
204653e947a0SKuninori Morimoto 		snd_card_free(card->snd_card);
2047*29040d1aSKuninori Morimoto 		card->snd_card = NULL;
2048*29040d1aSKuninori Morimoto 	}
204953e947a0SKuninori Morimoto 
205053e947a0SKuninori Morimoto 	/* remove and free each DAI */
205153e947a0SKuninori Morimoto 	soc_remove_dai_links(card);
205253e947a0SKuninori Morimoto 	soc_remove_pcm_runtimes(card);
205378a24e10SCurtis Malainey 	soc_cleanup_platform(card);
205453e947a0SKuninori Morimoto 
205553e947a0SKuninori Morimoto 	/* remove auxiliary devices */
205653e947a0SKuninori Morimoto 	soc_remove_aux_devices(card);
205753e947a0SKuninori Morimoto 
205853e947a0SKuninori Morimoto 	snd_soc_dapm_free(&card->dapm);
205953e947a0SKuninori Morimoto 	soc_cleanup_card_debugfs(card);
206053e947a0SKuninori Morimoto 
206153e947a0SKuninori Morimoto 	/* remove the card */
206253e947a0SKuninori Morimoto 	if (card->remove)
206353e947a0SKuninori Morimoto 		card->remove(card);
206453e947a0SKuninori Morimoto 
206553e947a0SKuninori Morimoto 	return 0;
206653e947a0SKuninori Morimoto }
206753e947a0SKuninori Morimoto 
2068b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
2069f0fba2adSLiam Girdwood {
20701a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
207161b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
2072ce64c8b9SLars-Peter Clausen 	int ret, i, order;
207396dd3622SMark Brown 
207434e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
207501b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
2076f0fba2adSLiam Girdwood 
207753e947a0SKuninori Morimoto 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
207853e947a0SKuninori Morimoto 	card->dapm.dev = card->dev;
207953e947a0SKuninori Morimoto 	card->dapm.card = card;
208053e947a0SKuninori Morimoto 	list_add(&card->dapm.list, &card->dapm_list);
208153e947a0SKuninori Morimoto 
2082a655de80SLiam Girdwood 	/* check whether any platform is ignore machine FE and using topology */
2083a655de80SLiam Girdwood 	soc_check_tplg_fes(card);
2084a655de80SLiam Girdwood 
2085b19e6e7bSMark Brown 	/* bind DAIs */
20867fe072b4SKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link) {
20877fe072b4SKuninori Morimoto 		ret = soc_bind_dai_link(card, dai_link);
2088b19e6e7bSMark Brown 		if (ret != 0)
208953e947a0SKuninori Morimoto 			goto probe_end;
2090db2a4165SFrank Mandarino 	}
2091db2a4165SFrank Mandarino 
209244c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
2093b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
209444c69bb1SLars-Peter Clausen 		ret = soc_bind_aux_dev(card, i);
2095b19e6e7bSMark Brown 		if (ret != 0)
209653e947a0SKuninori Morimoto 			goto probe_end;
2097db2a4165SFrank Mandarino 	}
2098db2a4165SFrank Mandarino 
2099f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
21007fe072b4SKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link)
21017fe072b4SKuninori Morimoto 		snd_soc_add_dai_link(card, dai_link);
2102f8f80361SMengdong Lin 
2103f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
2104102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2105f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
2106f0fba2adSLiam Girdwood 	if (ret < 0) {
210710e8aa9aSMichał Mirosław 		dev_err(card->dev,
210810e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
210910e8aa9aSMichał Mirosław 			card->name, ret);
211053e947a0SKuninori Morimoto 		goto probe_end;
2111db2a4165SFrank Mandarino 	}
2112db2a4165SFrank Mandarino 
21130757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
21140757d834SLars-Peter Clausen 
2115d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
2116d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2117d5d1e0beSLars-Peter Clausen #endif
2118d5d1e0beSLars-Peter Clausen 
211988ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
21206ed25978SAndy Green 	/* deferred resume work */
21216308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
21221301a964SRandy Dunlap #endif
21236ed25978SAndy Green 
21249a841ebbSMark Brown 	if (card->dapm_widgets)
21259a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
21269a841ebbSMark Brown 					  card->num_dapm_widgets);
21279a841ebbSMark Brown 
2128f23e860eSNicolin Chen 	if (card->of_dapm_widgets)
2129f23e860eSNicolin Chen 		snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2130f23e860eSNicolin Chen 					  card->num_of_dapm_widgets);
2131f23e860eSNicolin Chen 
2132f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
2133f0fba2adSLiam Girdwood 	if (card->probe) {
2134e7361ec4SMark Brown 		ret = card->probe(card);
2135f0fba2adSLiam Girdwood 		if (ret < 0)
213653e947a0SKuninori Morimoto 			goto probe_end;
2137f0fba2adSLiam Girdwood 	}
2138f0fba2adSLiam Girdwood 
213962ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
21401a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
2141bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
21421a497983SMengdong Lin 			ret = soc_probe_link_components(card, rtd, order);
214362ae68faSStephen Warren 			if (ret < 0) {
2144f110bfc7SLiam Girdwood 				dev_err(card->dev,
2145f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
2146f110bfc7SLiam Girdwood 					ret);
214753e947a0SKuninori Morimoto 				goto probe_end;
214862ae68faSStephen Warren 			}
214962ae68faSStephen Warren 		}
215062ae68faSStephen Warren 	}
215162ae68faSStephen Warren 
2152f2ed6b07SMengdong Lin 	/* probe auxiliary components */
2153f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
2154f2ed6b07SMengdong Lin 	if (ret < 0)
215553e947a0SKuninori Morimoto 		goto probe_end;
2156f2ed6b07SMengdong Lin 
21572c7b696aSMarcel Ziswiler 	/*
21582c7b696aSMarcel Ziswiler 	 * Find new DAI links added during probing components and bind them.
215961b0088bSMengdong Lin 	 * Components with topology may bring new DAIs and DAI links.
216061b0088bSMengdong Lin 	 */
216198061fdbSKuninori Morimoto 	for_each_card_links(card, dai_link) {
216261b0088bSMengdong Lin 		if (soc_is_dai_link_bound(card, dai_link))
216361b0088bSMengdong Lin 			continue;
216461b0088bSMengdong Lin 
216561b0088bSMengdong Lin 		ret = soc_init_dai_link(card, dai_link);
216661b0088bSMengdong Lin 		if (ret)
216753e947a0SKuninori Morimoto 			goto probe_end;
216861b0088bSMengdong Lin 		ret = soc_bind_dai_link(card, dai_link);
216961b0088bSMengdong Lin 		if (ret)
217053e947a0SKuninori Morimoto 			goto probe_end;
217161b0088bSMengdong Lin 	}
217261b0088bSMengdong Lin 
217362ae68faSStephen Warren 	/* probe all DAI links on this card */
21741a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
2175bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
21761a497983SMengdong Lin 			ret = soc_probe_link_dais(card, rtd, order);
2177fe3e78e0SMark Brown 			if (ret < 0) {
2178f110bfc7SLiam Girdwood 				dev_err(card->dev,
2179f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
2180f110bfc7SLiam Girdwood 					ret);
218153e947a0SKuninori Morimoto 				goto probe_end;
2182fe3e78e0SMark Brown 			}
2183fe3e78e0SMark Brown 		}
21840168bf0dSLiam Girdwood 	}
2185fe3e78e0SMark Brown 
2186888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
2187b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
2188888df395SMark Brown 
2189b7af1dafSMark Brown 	if (card->controls)
21902c7b696aSMarcel Ziswiler 		snd_soc_add_card_controls(card, card->controls,
21912c7b696aSMarcel Ziswiler 					  card->num_controls);
2192b7af1dafSMark Brown 
2193b8ad29deSMark Brown 	if (card->dapm_routes)
2194b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2195b8ad29deSMark Brown 					card->num_dapm_routes);
2196b8ad29deSMark Brown 
2197f23e860eSNicolin Chen 	if (card->of_dapm_routes)
2198f23e860eSNicolin Chen 		snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2199f23e860eSNicolin Chen 					card->num_of_dapm_routes);
220075d9ac46SMark Brown 
2201861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
2202861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
2203861886d3STakashi Iwai 
2204f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
2205fe3e78e0SMark Brown 		 "%s", card->name);
2206f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
220722de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
2208f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2209f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
2210f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2211f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
2212f0e8ed85SMark Brown 		case '_':
2213f0e8ed85SMark Brown 		case '-':
2214f0e8ed85SMark Brown 		case '\0':
2215f0e8ed85SMark Brown 			break;
2216f0e8ed85SMark Brown 		default:
2217f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
2218f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
2219f0e8ed85SMark Brown 			break;
2220f0e8ed85SMark Brown 		}
2221f0e8ed85SMark Brown 	}
2222fe3e78e0SMark Brown 
222328e9ad92SMark Brown 	if (card->late_probe) {
222428e9ad92SMark Brown 		ret = card->late_probe(card);
222528e9ad92SMark Brown 		if (ret < 0) {
2226f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
222728e9ad92SMark Brown 				card->name, ret);
222853e947a0SKuninori Morimoto 			goto probe_end;
222928e9ad92SMark Brown 		}
223028e9ad92SMark Brown 	}
223128e9ad92SMark Brown 
2232824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
22338c193b8dSLars-Peter Clausen 
2234f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2235fe3e78e0SMark Brown 	if (ret < 0) {
2236f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2237f110bfc7SLiam Girdwood 				ret);
223853e947a0SKuninori Morimoto 		goto probe_end;
2239fe3e78e0SMark Brown 	}
2240fe3e78e0SMark Brown 
2241435c5e25SMark Brown 	card->instantiated = 1;
2242882eab6cSTzung-Bi Shih 	dapm_mark_endpoints_dirty(card);
22434f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2244b19e6e7bSMark Brown 
224553e947a0SKuninori Morimoto probe_end:
224653e947a0SKuninori Morimoto 	if (ret < 0)
224753e947a0SKuninori Morimoto 		soc_cleanup_card_resources(card);
2248db2a4165SFrank Mandarino 
2249f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
225034e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2251db2a4165SFrank Mandarino 
2252b19e6e7bSMark Brown 	return ret;
2253435c5e25SMark Brown }
2254435c5e25SMark Brown 
2255435c5e25SMark Brown /* probes a new socdev */
2256435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2257435c5e25SMark Brown {
2258f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2259435c5e25SMark Brown 
226070a7ca34SVinod Koul 	/*
226170a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
226270a7ca34SVinod Koul 	 * we should not be here in that case so ret error
226370a7ca34SVinod Koul 	 */
226470a7ca34SVinod Koul 	if (!card)
226570a7ca34SVinod Koul 		return -EINVAL;
226670a7ca34SVinod Koul 
2267fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2268f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2269fe4085e8SMark Brown 		 card->name);
2270fe4085e8SMark Brown 
2271435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2272435c5e25SMark Brown 	card->dev = &pdev->dev;
2273f0fba2adSLiam Girdwood 
227428d528c8SMark Brown 	return snd_soc_register_card(card);
2275435c5e25SMark Brown }
2276435c5e25SMark Brown 
2277b0e26485SVinod Koul /* removes a socdev */
2278b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2279b0e26485SVinod Koul {
2280b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2281b0e26485SVinod Koul 
2282c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2283db2a4165SFrank Mandarino 	return 0;
2284db2a4165SFrank Mandarino }
2285db2a4165SFrank Mandarino 
22866f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
228751737470SMark Brown {
22886f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
22891a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
229051737470SMark Brown 
229151737470SMark Brown 	if (!card->instantiated)
2292416356fcSMark Brown 		return 0;
229351737470SMark Brown 
22942c7b696aSMarcel Ziswiler 	/*
22952c7b696aSMarcel Ziswiler 	 * Flush out pmdown_time work - we actually do want to run it
22962c7b696aSMarcel Ziswiler 	 * now, we're shutting down so no imminent restart.
22972c7b696aSMarcel Ziswiler 	 */
229865462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
229951737470SMark Brown 
2300f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2301416356fcSMark Brown 
2302988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
2303bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
230488bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
23050b7990e3SKuninori Morimoto 		struct snd_soc_dai *codec_dai;
23061a497983SMengdong Lin 		int i;
230788bd870fSBenoit Cousson 
2308988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
23090b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
231088bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
231188bd870fSBenoit Cousson 		}
2312988e8cc4SNicolin Chen 	}
2313988e8cc4SNicolin Chen 
2314416356fcSMark Brown 	return 0;
231551737470SMark Brown }
23166f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
231751737470SMark Brown 
23186f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2319b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2320b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2321b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2322b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
23236f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2324b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2325416356fcSMark Brown };
2326deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2327416356fcSMark Brown 
2328db2a4165SFrank Mandarino /* ASoC platform driver */
2329db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2330db2a4165SFrank Mandarino 	.driver		= {
2331db2a4165SFrank Mandarino 		.name		= "soc-audio",
23326f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2333db2a4165SFrank Mandarino 	},
2334db2a4165SFrank Mandarino 	.probe		= soc_probe,
2335db2a4165SFrank Mandarino 	.remove		= soc_remove,
2336db2a4165SFrank Mandarino };
2337db2a4165SFrank Mandarino 
2338096e49d5SMark Brown /**
2339db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2340db2a4165SFrank Mandarino  * @_template: control template
2341db2a4165SFrank Mandarino  * @data: control private data
2342ac11a2b3SMark Brown  * @long_name: control long name
2343efb7ac3fSMark Brown  * @prefix: control name prefix
2344db2a4165SFrank Mandarino  *
2345db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2346db2a4165SFrank Mandarino  *
2347db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2348db2a4165SFrank Mandarino  */
2349db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
23503056557fSMark Brown 				  void *data, const char *long_name,
2351efb7ac3fSMark Brown 				  const char *prefix)
2352db2a4165SFrank Mandarino {
2353db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2354efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2355efb7ac3fSMark Brown 	char *name = NULL;
2356db2a4165SFrank Mandarino 
2357db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2358db2a4165SFrank Mandarino 	template.index = 0;
2359db2a4165SFrank Mandarino 
2360efb7ac3fSMark Brown 	if (!long_name)
2361efb7ac3fSMark Brown 		long_name = template.name;
2362efb7ac3fSMark Brown 
2363efb7ac3fSMark Brown 	if (prefix) {
23642b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2365efb7ac3fSMark Brown 		if (!name)
2366efb7ac3fSMark Brown 			return NULL;
2367efb7ac3fSMark Brown 
2368efb7ac3fSMark Brown 		template.name = name;
2369efb7ac3fSMark Brown 	} else {
2370efb7ac3fSMark Brown 		template.name = long_name;
2371efb7ac3fSMark Brown 	}
2372efb7ac3fSMark Brown 
2373efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2374efb7ac3fSMark Brown 
2375efb7ac3fSMark Brown 	kfree(name);
2376efb7ac3fSMark Brown 
2377efb7ac3fSMark Brown 	return kcontrol;
2378db2a4165SFrank Mandarino }
2379db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2380db2a4165SFrank Mandarino 
2381022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2382022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2383022658beSLiam Girdwood 	const char *prefix, void *data)
2384022658beSLiam Girdwood {
2385022658beSLiam Girdwood 	int err, i;
2386022658beSLiam Girdwood 
2387022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2388022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
23892c7b696aSMarcel Ziswiler 
2390022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2391022658beSLiam Girdwood 						     control->name, prefix));
2392022658beSLiam Girdwood 		if (err < 0) {
2393f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2394f110bfc7SLiam Girdwood 				control->name, err);
2395022658beSLiam Girdwood 			return err;
2396022658beSLiam Girdwood 		}
2397022658beSLiam Girdwood 	}
2398022658beSLiam Girdwood 
2399022658beSLiam Girdwood 	return 0;
2400022658beSLiam Girdwood }
2401022658beSLiam Girdwood 
24024fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
24034fefd698SDimitris Papastamos 					       const char *name)
24044fefd698SDimitris Papastamos {
24054fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
24064fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
24074fefd698SDimitris Papastamos 
24084fefd698SDimitris Papastamos 	if (unlikely(!name))
24094fefd698SDimitris Papastamos 		return NULL;
24104fefd698SDimitris Papastamos 
24114fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
24124fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
24134fefd698SDimitris Papastamos 			return kctl;
24144fefd698SDimitris Papastamos 	return NULL;
24154fefd698SDimitris Papastamos }
24164fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
24174fefd698SDimitris Papastamos 
2418db2a4165SFrank Mandarino /**
24190f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
24200f2780adSLars-Peter Clausen  *
24210f2780adSLars-Peter Clausen  * @component: Component to add controls to
24220f2780adSLars-Peter Clausen  * @controls: Array of controls to add
24230f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
24240f2780adSLars-Peter Clausen  *
24250f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
24260f2780adSLars-Peter Clausen  */
24270f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
24280f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
24290f2780adSLars-Peter Clausen {
24300f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
24310f2780adSLars-Peter Clausen 
24320f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
24330f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
24340f2780adSLars-Peter Clausen }
24350f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
24360f2780adSLars-Peter Clausen 
24370f2780adSLars-Peter Clausen /**
2438022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2439022658beSLiam Girdwood  * Convenience function to add a list of controls.
2440022658beSLiam Girdwood  *
2441022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2442022658beSLiam Girdwood  * @controls: array of controls to add
2443022658beSLiam Girdwood  * @num_controls: number of elements in the array
2444022658beSLiam Girdwood  *
2445022658beSLiam Girdwood  * Return 0 for success, else error.
2446022658beSLiam Girdwood  */
2447022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2448022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2449022658beSLiam Girdwood {
2450022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2451022658beSLiam Girdwood 
2452022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2453022658beSLiam Girdwood 			NULL, soc_card);
2454022658beSLiam Girdwood }
2455022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2456022658beSLiam Girdwood 
2457022658beSLiam Girdwood /**
2458022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2459022658beSLiam Girdwood  * Convienience function to add a list of controls.
2460022658beSLiam Girdwood  *
2461022658beSLiam Girdwood  * @dai: DAI to add controls to
2462022658beSLiam Girdwood  * @controls: array of controls to add
2463022658beSLiam Girdwood  * @num_controls: number of elements in the array
2464022658beSLiam Girdwood  *
2465022658beSLiam Girdwood  * Return 0 for success, else error.
2466022658beSLiam Girdwood  */
2467022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2468022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2469022658beSLiam Girdwood {
2470313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2471022658beSLiam Girdwood 
2472022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2473022658beSLiam Girdwood 			NULL, dai);
2474022658beSLiam Girdwood }
2475022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2476022658beSLiam Girdwood 
2477022658beSLiam Girdwood /**
24788c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
24798c6529dbSLiam Girdwood  * @dai: DAI
24808c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
24818c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
24828c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
24838c6529dbSLiam Girdwood  *
24848c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
24858c6529dbSLiam Girdwood  */
24868c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
24878c6529dbSLiam Girdwood 	unsigned int freq, int dir)
24888c6529dbSLiam Girdwood {
248946471925SKuninori Morimoto 	if (dai->driver->ops->set_sysclk)
2490f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
249171ccef0dSKuninori Morimoto 
249271ccef0dSKuninori Morimoto 	return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2493ec4ee52aSMark Brown 					    freq, dir);
24948c6529dbSLiam Girdwood }
24958c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
24968c6529dbSLiam Girdwood 
24978c6529dbSLiam Girdwood /**
249871ccef0dSKuninori Morimoto  * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
249971ccef0dSKuninori Morimoto  * @component: COMPONENT
250071ccef0dSKuninori Morimoto  * @clk_id: DAI specific clock ID
250171ccef0dSKuninori Morimoto  * @source: Source for the clock
250271ccef0dSKuninori Morimoto  * @freq: new clock frequency in Hz
250371ccef0dSKuninori Morimoto  * @dir: new clock direction - input/output.
250471ccef0dSKuninori Morimoto  *
250571ccef0dSKuninori Morimoto  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
250671ccef0dSKuninori Morimoto  */
25072c7b696aSMarcel Ziswiler int snd_soc_component_set_sysclk(struct snd_soc_component *component,
25082c7b696aSMarcel Ziswiler 				 int clk_id, int source, unsigned int freq,
25092c7b696aSMarcel Ziswiler 				 int dir)
251071ccef0dSKuninori Morimoto {
251171ccef0dSKuninori Morimoto 	if (component->driver->set_sysclk)
251271ccef0dSKuninori Morimoto 		return component->driver->set_sysclk(component, clk_id, source,
251371ccef0dSKuninori Morimoto 						 freq, dir);
251471ccef0dSKuninori Morimoto 
251571ccef0dSKuninori Morimoto 	return -ENOTSUPP;
251671ccef0dSKuninori Morimoto }
251771ccef0dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
251871ccef0dSKuninori Morimoto 
251971ccef0dSKuninori Morimoto /**
25208c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
25218c6529dbSLiam Girdwood  * @dai: DAI
2522ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
25238c6529dbSLiam Girdwood  * @div: new clock divisor.
25248c6529dbSLiam Girdwood  *
25258c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
25268c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
25278c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
25288c6529dbSLiam Girdwood  */
25298c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
25308c6529dbSLiam Girdwood 	int div_id, int div)
25318c6529dbSLiam Girdwood {
253246471925SKuninori Morimoto 	if (dai->driver->ops->set_clkdiv)
2533f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
25348c6529dbSLiam Girdwood 	else
25358c6529dbSLiam Girdwood 		return -EINVAL;
25368c6529dbSLiam Girdwood }
25378c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
25388c6529dbSLiam Girdwood 
25398c6529dbSLiam Girdwood /**
25408c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
25418c6529dbSLiam Girdwood  * @dai: DAI
25428c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
254385488037SMark Brown  * @source: DAI specific source for the PLL
25448c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
25458c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
25468c6529dbSLiam Girdwood  *
25478c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
25488c6529dbSLiam Girdwood  */
254985488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
255085488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
25518c6529dbSLiam Girdwood {
255246471925SKuninori Morimoto 	if (dai->driver->ops->set_pll)
2553f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
255485488037SMark Brown 					 freq_in, freq_out);
2555ef641e5dSKuninori Morimoto 
2556ef641e5dSKuninori Morimoto 	return snd_soc_component_set_pll(dai->component, pll_id, source,
2557ec4ee52aSMark Brown 					 freq_in, freq_out);
25588c6529dbSLiam Girdwood }
25598c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
25608c6529dbSLiam Girdwood 
2561ec4ee52aSMark Brown /*
2562ef641e5dSKuninori Morimoto  * snd_soc_component_set_pll - configure component PLL.
2563ef641e5dSKuninori Morimoto  * @component: COMPONENT
2564ef641e5dSKuninori Morimoto  * @pll_id: DAI specific PLL ID
2565ef641e5dSKuninori Morimoto  * @source: DAI specific source for the PLL
2566ef641e5dSKuninori Morimoto  * @freq_in: PLL input clock frequency in Hz
2567ef641e5dSKuninori Morimoto  * @freq_out: requested PLL output clock frequency in Hz
2568ef641e5dSKuninori Morimoto  *
2569ef641e5dSKuninori Morimoto  * Configures and enables PLL to generate output clock based on input clock.
2570ef641e5dSKuninori Morimoto  */
2571ef641e5dSKuninori Morimoto int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2572ef641e5dSKuninori Morimoto 			      int source, unsigned int freq_in,
2573ef641e5dSKuninori Morimoto 			      unsigned int freq_out)
2574ef641e5dSKuninori Morimoto {
2575ef641e5dSKuninori Morimoto 	if (component->driver->set_pll)
2576ef641e5dSKuninori Morimoto 		return component->driver->set_pll(component, pll_id, source,
2577ef641e5dSKuninori Morimoto 						  freq_in, freq_out);
2578ef641e5dSKuninori Morimoto 
2579ef641e5dSKuninori Morimoto 	return -EINVAL;
2580ef641e5dSKuninori Morimoto }
2581ef641e5dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2582ef641e5dSKuninori Morimoto 
25838c6529dbSLiam Girdwood /**
2584e54cf76bSLiam Girdwood  * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2585e54cf76bSLiam Girdwood  * @dai: DAI
2586231b86b1SMasanari Iida  * @ratio: Ratio of BCLK to Sample rate.
2587e54cf76bSLiam Girdwood  *
2588e54cf76bSLiam Girdwood  * Configures the DAI for a preset BCLK to sample rate ratio.
2589e54cf76bSLiam Girdwood  */
2590e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2591e54cf76bSLiam Girdwood {
259246471925SKuninori Morimoto 	if (dai->driver->ops->set_bclk_ratio)
2593e54cf76bSLiam Girdwood 		return dai->driver->ops->set_bclk_ratio(dai, ratio);
2594e54cf76bSLiam Girdwood 	else
2595e54cf76bSLiam Girdwood 		return -EINVAL;
2596e54cf76bSLiam Girdwood }
2597e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2598e54cf76bSLiam Girdwood 
2599e54cf76bSLiam Girdwood /**
26008c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
26018c6529dbSLiam Girdwood  * @dai: DAI
2602bb19ba2aSRandy Dunlap  * @fmt: SND_SOC_DAIFMT_* format value.
26038c6529dbSLiam Girdwood  *
26048c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
26058c6529dbSLiam Girdwood  */
26068c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
26078c6529dbSLiam Girdwood {
26085e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
26095e4ba569SShawn Guo 		return -ENOTSUPP;
26105e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
26118c6529dbSLiam Girdwood }
26128c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
26138c6529dbSLiam Girdwood 
26148c6529dbSLiam Girdwood /**
2615e5c21514SXiubo Li  * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
261689c67857SXiubo Li  * @slots: Number of slots in use.
261789c67857SXiubo Li  * @tx_mask: bitmask representing active TX slots.
261889c67857SXiubo Li  * @rx_mask: bitmask representing active RX slots.
261989c67857SXiubo Li  *
262089c67857SXiubo Li  * Generates the TDM tx and rx slot default masks for DAI.
262189c67857SXiubo Li  */
2622e5c21514SXiubo Li static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
262389c67857SXiubo Li 				       unsigned int *tx_mask,
262489c67857SXiubo Li 				       unsigned int *rx_mask)
262589c67857SXiubo Li {
262689c67857SXiubo Li 	if (*tx_mask || *rx_mask)
262789c67857SXiubo Li 		return 0;
262889c67857SXiubo Li 
262989c67857SXiubo Li 	if (!slots)
263089c67857SXiubo Li 		return -EINVAL;
263189c67857SXiubo Li 
263289c67857SXiubo Li 	*tx_mask = (1 << slots) - 1;
263389c67857SXiubo Li 	*rx_mask = (1 << slots) - 1;
263489c67857SXiubo Li 
263589c67857SXiubo Li 	return 0;
263689c67857SXiubo Li }
263789c67857SXiubo Li 
263889c67857SXiubo Li /**
2639e46c9366SLars-Peter Clausen  * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2640e46c9366SLars-Peter Clausen  * @dai: The DAI to configure
2641a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2642a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
26438c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2644a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
26458c6529dbSLiam Girdwood  *
2646e46c9366SLars-Peter Clausen  * This function configures the specified DAI for TDM operation. @slot contains
2647e46c9366SLars-Peter Clausen  * the total number of slots of the TDM stream and @slot_with the width of each
2648e46c9366SLars-Peter Clausen  * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2649e46c9366SLars-Peter Clausen  * active slots of the TDM stream for the specified DAI, i.e. which slots the
2650e46c9366SLars-Peter Clausen  * DAI should write to or read from. If a bit is set the corresponding slot is
2651e46c9366SLars-Peter Clausen  * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2652e46c9366SLars-Peter Clausen  * the first slot, bit 1 to the second slot and so on. The first active slot
2653e46c9366SLars-Peter Clausen  * maps to the first channel of the DAI, the second active slot to the second
2654e46c9366SLars-Peter Clausen  * channel and so on.
2655e46c9366SLars-Peter Clausen  *
2656e46c9366SLars-Peter Clausen  * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2657e46c9366SLars-Peter Clausen  * @rx_mask and @slot_width will be ignored.
2658e46c9366SLars-Peter Clausen  *
2659e46c9366SLars-Peter Clausen  * Returns 0 on success, a negative error code otherwise.
26608c6529dbSLiam Girdwood  */
26618c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2662a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
26638c6529dbSLiam Girdwood {
266446471925SKuninori Morimoto 	if (dai->driver->ops->xlate_tdm_slot_mask)
2665e5c21514SXiubo Li 		dai->driver->ops->xlate_tdm_slot_mask(slots,
266689c67857SXiubo Li 						&tx_mask, &rx_mask);
266789c67857SXiubo Li 	else
2668e5c21514SXiubo Li 		snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
266989c67857SXiubo Li 
267088bd870fSBenoit Cousson 	dai->tx_mask = tx_mask;
267188bd870fSBenoit Cousson 	dai->rx_mask = rx_mask;
267288bd870fSBenoit Cousson 
267346471925SKuninori Morimoto 	if (dai->driver->ops->set_tdm_slot)
2674f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2675a5479e38SDaniel Ribeiro 				slots, slot_width);
26768c6529dbSLiam Girdwood 	else
2677b2cbb6e1SXiubo Li 		return -ENOTSUPP;
26788c6529dbSLiam Girdwood }
26798c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
26808c6529dbSLiam Girdwood 
26818c6529dbSLiam Girdwood /**
2682472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2683472df3cbSBarry Song  * @dai: DAI
2684472df3cbSBarry Song  * @tx_num: how many TX channels
2685472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2686472df3cbSBarry Song  *           0~num-1 uses
2687472df3cbSBarry Song  * @rx_num: how many RX channels
2688472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2689472df3cbSBarry Song  *           0~num-1 uses
2690472df3cbSBarry Song  *
2691472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2692472df3cbSBarry Song  */
2693472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2694472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
2695472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
2696472df3cbSBarry Song {
269746471925SKuninori Morimoto 	if (dai->driver->ops->set_channel_map)
2698f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2699472df3cbSBarry Song 			rx_num, rx_slot);
2700472df3cbSBarry Song 	else
2701472df3cbSBarry Song 		return -EINVAL;
2702472df3cbSBarry Song }
2703472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2704472df3cbSBarry Song 
2705472df3cbSBarry Song /**
2706467b061fSSrinivas Kandagatla  * snd_soc_dai_get_channel_map - Get DAI audio channel map
2707467b061fSSrinivas Kandagatla  * @dai: DAI
2708467b061fSSrinivas Kandagatla  * @tx_num: how many TX channels
2709467b061fSSrinivas Kandagatla  * @tx_slot: pointer to an array which imply the TX slot number channel
2710467b061fSSrinivas Kandagatla  *           0~num-1 uses
2711467b061fSSrinivas Kandagatla  * @rx_num: how many RX channels
2712467b061fSSrinivas Kandagatla  * @rx_slot: pointer to an array which imply the RX slot number channel
2713467b061fSSrinivas Kandagatla  *           0~num-1 uses
2714467b061fSSrinivas Kandagatla  */
2715467b061fSSrinivas Kandagatla int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
2716467b061fSSrinivas Kandagatla 	unsigned int *tx_num, unsigned int *tx_slot,
2717467b061fSSrinivas Kandagatla 	unsigned int *rx_num, unsigned int *rx_slot)
2718467b061fSSrinivas Kandagatla {
2719467b061fSSrinivas Kandagatla 	if (dai->driver->ops->get_channel_map)
2720467b061fSSrinivas Kandagatla 		return dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
2721467b061fSSrinivas Kandagatla 			rx_num, rx_slot);
2722467b061fSSrinivas Kandagatla 	else
2723467b061fSSrinivas Kandagatla 		return -ENOTSUPP;
2724467b061fSSrinivas Kandagatla }
2725467b061fSSrinivas Kandagatla EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
2726467b061fSSrinivas Kandagatla 
2727467b061fSSrinivas Kandagatla /**
27288c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
27298c6529dbSLiam Girdwood  * @dai: DAI
27308c6529dbSLiam Girdwood  * @tristate: tristate enable
27318c6529dbSLiam Girdwood  *
27328c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
27338c6529dbSLiam Girdwood  */
27348c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
27358c6529dbSLiam Girdwood {
273646471925SKuninori Morimoto 	if (dai->driver->ops->set_tristate)
2737f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
27388c6529dbSLiam Girdwood 	else
27398c6529dbSLiam Girdwood 		return -EINVAL;
27408c6529dbSLiam Girdwood }
27418c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
27428c6529dbSLiam Girdwood 
27438c6529dbSLiam Girdwood /**
27448c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
27458c6529dbSLiam Girdwood  * @dai: DAI
27468c6529dbSLiam Girdwood  * @mute: mute enable
2747da18396fSMark Brown  * @direction: stream to mute
27488c6529dbSLiam Girdwood  *
27498c6529dbSLiam Girdwood  * Mutes the DAI DAC.
27508c6529dbSLiam Girdwood  */
2751da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2752da18396fSMark Brown 			     int direction)
27538c6529dbSLiam Girdwood {
2754da18396fSMark Brown 	if (dai->driver->ops->mute_stream)
2755da18396fSMark Brown 		return dai->driver->ops->mute_stream(dai, mute, direction);
2756da18396fSMark Brown 	else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2757da18396fSMark Brown 		 dai->driver->ops->digital_mute)
2758f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
27598c6529dbSLiam Girdwood 	else
276004570c62SMark Brown 		return -ENOTSUPP;
27618c6529dbSLiam Girdwood }
27628c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
27638c6529dbSLiam Girdwood 
2764e894efefSSrinivas Kandagatla static int snd_soc_bind_card(struct snd_soc_card *card)
2765e894efefSSrinivas Kandagatla {
2766e894efefSSrinivas Kandagatla 	struct snd_soc_pcm_runtime *rtd;
2767e894efefSSrinivas Kandagatla 	int ret;
2768e894efefSSrinivas Kandagatla 
2769e894efefSSrinivas Kandagatla 	ret = snd_soc_instantiate_card(card);
2770e894efefSSrinivas Kandagatla 	if (ret != 0)
2771e894efefSSrinivas Kandagatla 		return ret;
2772e894efefSSrinivas Kandagatla 
2773e894efefSSrinivas Kandagatla 	/* deactivate pins to sleep state */
2774bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
2775e894efefSSrinivas Kandagatla 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2776e894efefSSrinivas Kandagatla 		struct snd_soc_dai *codec_dai;
2777e894efefSSrinivas Kandagatla 		int j;
2778e894efefSSrinivas Kandagatla 
2779e894efefSSrinivas Kandagatla 		for_each_rtd_codec_dai(rtd, j, codec_dai) {
2780e894efefSSrinivas Kandagatla 			if (!codec_dai->active)
2781e894efefSSrinivas Kandagatla 				pinctrl_pm_select_sleep_state(codec_dai->dev);
2782e894efefSSrinivas Kandagatla 		}
2783e894efefSSrinivas Kandagatla 
2784e894efefSSrinivas Kandagatla 		if (!cpu_dai->active)
2785e894efefSSrinivas Kandagatla 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
2786e894efefSSrinivas Kandagatla 	}
2787e894efefSSrinivas Kandagatla 
2788e894efefSSrinivas Kandagatla 	return ret;
2789e894efefSSrinivas Kandagatla }
2790e894efefSSrinivas Kandagatla 
2791c5af3a2eSMark Brown /**
2792c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2793c5af3a2eSMark Brown  *
2794ac11a2b3SMark Brown  * @card: Card to register
2795c5af3a2eSMark Brown  *
2796c5af3a2eSMark Brown  */
279770a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2798c5af3a2eSMark Brown {
2799923c5e61SMengdong Lin 	int i, ret;
28007fe072b4SKuninori Morimoto 	struct snd_soc_dai_link *link;
2801f0fba2adSLiam Girdwood 
2802c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2803c5af3a2eSMark Brown 		return -EINVAL;
2804c5af3a2eSMark Brown 
280504eb1efcSRohit kumar 	mutex_lock(&client_mutex);
28067fe072b4SKuninori Morimoto 	for_each_card_prelinks(card, i, link) {
28075a504963SStephen Warren 
2808923c5e61SMengdong Lin 		ret = soc_init_dai_link(card, link);
280988bd870fSBenoit Cousson 		if (ret) {
2810c342febcSJonathan Hunter 			soc_cleanup_platform(card);
2811923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: failed to init link %s\n",
2812923c5e61SMengdong Lin 				link->name);
281304eb1efcSRohit kumar 			mutex_unlock(&client_mutex);
281488bd870fSBenoit Cousson 			return ret;
281588bd870fSBenoit Cousson 		}
28165a504963SStephen Warren 	}
281704eb1efcSRohit kumar 	mutex_unlock(&client_mutex);
28185a504963SStephen Warren 
2819ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2820ed77cc12SMark Brown 
2821111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
2822111c6419SStephen Warren 
2823f8f80361SMengdong Lin 	INIT_LIST_HEAD(&card->dai_link_list);
2824f0fba2adSLiam Girdwood 
28251a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
28269115171aSMark Brown 	card->num_rtd = 0;
2827db2a4165SFrank Mandarino 
2828db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
28298a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2830db2a4165SFrank Mandarino 	card->instantiated = 0;
2831db2a4165SFrank Mandarino 	mutex_init(&card->mutex);
2832a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
2833a9764869SKaiChieh Chuang 	spin_lock_init(&card->dpcm_lock);
2834db2a4165SFrank Mandarino 
2835e894efefSSrinivas Kandagatla 	return snd_soc_bind_card(card);
2836db2a4165SFrank Mandarino }
283770a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2838db2a4165SFrank Mandarino 
2839e894efefSSrinivas Kandagatla static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2840e894efefSSrinivas Kandagatla {
2841f96fb7d1SRanjani Sridharan 	struct snd_soc_pcm_runtime *rtd;
2842f96fb7d1SRanjani Sridharan 	int order;
2843f96fb7d1SRanjani Sridharan 
2844e894efefSSrinivas Kandagatla 	if (card->instantiated) {
2845e894efefSSrinivas Kandagatla 		card->instantiated = false;
2846e894efefSSrinivas Kandagatla 		snd_soc_dapm_shutdown(card);
284753e947a0SKuninori Morimoto 		snd_soc_flush_all_delayed_work(card);
2848f96fb7d1SRanjani Sridharan 
2849f96fb7d1SRanjani Sridharan 		/* remove all components used by DAI links on this card */
2850f96fb7d1SRanjani Sridharan 		for_each_comp_order(order) {
2851f96fb7d1SRanjani Sridharan 			for_each_card_rtds(card, rtd) {
2852f96fb7d1SRanjani Sridharan 				soc_remove_link_components(card, rtd, order);
2853f96fb7d1SRanjani Sridharan 			}
2854f96fb7d1SRanjani Sridharan 		}
2855f96fb7d1SRanjani Sridharan 
2856e894efefSSrinivas Kandagatla 		soc_cleanup_card_resources(card);
2857e894efefSSrinivas Kandagatla 		if (!unregister)
2858e894efefSSrinivas Kandagatla 			list_add(&card->list, &unbind_card_list);
2859e894efefSSrinivas Kandagatla 	} else {
2860e894efefSSrinivas Kandagatla 		if (unregister)
2861e894efefSSrinivas Kandagatla 			list_del(&card->list);
2862e894efefSSrinivas Kandagatla 	}
2863e894efefSSrinivas Kandagatla }
2864e894efefSSrinivas Kandagatla 
2865db2a4165SFrank Mandarino /**
2866db2a4165SFrank Mandarino  * snd_soc_unregister_card - Unregister a card with the ASoC core
2867db2a4165SFrank Mandarino  *
2868db2a4165SFrank Mandarino  * @card: Card to unregister
2869db2a4165SFrank Mandarino  *
2870db2a4165SFrank Mandarino  */
287170a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2872db2a4165SFrank Mandarino {
2873e894efefSSrinivas Kandagatla 	snd_soc_unbind_card(card, true);
2874f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2875db2a4165SFrank Mandarino 
2876db2a4165SFrank Mandarino 	return 0;
2877db2a4165SFrank Mandarino }
287870a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2879db2a4165SFrank Mandarino 
2880db2a4165SFrank Mandarino /*
2881db2a4165SFrank Mandarino  * Simplify DAI link configuration by removing ".-1" from device names
2882db2a4165SFrank Mandarino  * and sanitizing names.
2883db2a4165SFrank Mandarino  */
28840b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2885db2a4165SFrank Mandarino {
2886db2a4165SFrank Mandarino 	char *found, name[NAME_SIZE];
2887db2a4165SFrank Mandarino 	int id1, id2;
2888db2a4165SFrank Mandarino 
2889db2a4165SFrank Mandarino 	if (dev_name(dev) == NULL)
2890db2a4165SFrank Mandarino 		return NULL;
2891db2a4165SFrank Mandarino 
289258818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2893db2a4165SFrank Mandarino 
2894db2a4165SFrank Mandarino 	/* are we a "%s.%d" name (platform and SPI components) */
2895c5af3a2eSMark Brown 	found = strstr(name, dev->driver->name);
2896c5af3a2eSMark Brown 	if (found) {
2897c5af3a2eSMark Brown 		/* get ID */
2898c5af3a2eSMark Brown 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2899c5af3a2eSMark Brown 
2900c5af3a2eSMark Brown 			/* discard ID from name if ID == -1 */
2901c5af3a2eSMark Brown 			if (*id == -1)
2902c5af3a2eSMark Brown 				found[strlen(dev->driver->name)] = '\0';
2903c5af3a2eSMark Brown 		}
2904c5af3a2eSMark Brown 
2905c5af3a2eSMark Brown 	} else {
2906c5af3a2eSMark Brown 		/* I2C component devices are named "bus-addr" */
2907c5af3a2eSMark Brown 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2908c5af3a2eSMark Brown 			char tmp[NAME_SIZE];
2909c5af3a2eSMark Brown 
2910c5af3a2eSMark Brown 			/* create unique ID number from I2C addr and bus */
2911c5af3a2eSMark Brown 			*id = ((id1 & 0xffff) << 16) + id2;
2912c5af3a2eSMark Brown 
2913c5af3a2eSMark Brown 			/* sanitize component name for DAI link creation */
29142c7b696aSMarcel Ziswiler 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
29152c7b696aSMarcel Ziswiler 				 name);
291658818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2917c5af3a2eSMark Brown 		} else
2918c5af3a2eSMark Brown 			*id = 0;
2919c5af3a2eSMark Brown 	}
2920c5af3a2eSMark Brown 
2921c5af3a2eSMark Brown 	return kstrdup(name, GFP_KERNEL);
2922c5af3a2eSMark Brown }
2923c5af3a2eSMark Brown 
2924c5af3a2eSMark Brown /*
2925c5af3a2eSMark Brown  * Simplify DAI link naming for single devices with multiple DAIs by removing
2926c5af3a2eSMark Brown  * any ".-1" and using the DAI name (instead of device name).
2927c5af3a2eSMark Brown  */
2928c5af3a2eSMark Brown static inline char *fmt_multiple_name(struct device *dev,
2929c5af3a2eSMark Brown 		struct snd_soc_dai_driver *dai_drv)
2930c5af3a2eSMark Brown {
2931c5af3a2eSMark Brown 	if (dai_drv->name == NULL) {
293210e8aa9aSMichał Mirosław 		dev_err(dev,
293310e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
293410e8aa9aSMichał Mirosław 			dev_name(dev));
2935c5af3a2eSMark Brown 		return NULL;
2936c5af3a2eSMark Brown 	}
2937c5af3a2eSMark Brown 
2938c5af3a2eSMark Brown 	return kstrdup(dai_drv->name, GFP_KERNEL);
2939c5af3a2eSMark Brown }
2940c5af3a2eSMark Brown 
2941c5af3a2eSMark Brown /**
294232c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
29439115171aSMark Brown  *
294432c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
29459115171aSMark Brown  */
294632c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
29479115171aSMark Brown {
29485c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
29499115171aSMark Brown 
295015a0c645SKuninori Morimoto 	for_each_component_dais_safe(component, dai, _dai) {
295132c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
295232c9ba54SLars-Peter Clausen 			dai->name);
29539115171aSMark Brown 		list_del(&dai->list);
2954f0fba2adSLiam Girdwood 		kfree(dai->name);
2955f0fba2adSLiam Girdwood 		kfree(dai);
29569115171aSMark Brown 	}
295732c9ba54SLars-Peter Clausen }
29589115171aSMark Brown 
29595e4fb372SMengdong Lin /* Create a DAI and add it to the component's DAI list */
29605e4fb372SMengdong Lin static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
29615e4fb372SMengdong Lin 	struct snd_soc_dai_driver *dai_drv,
29625e4fb372SMengdong Lin 	bool legacy_dai_naming)
29635e4fb372SMengdong Lin {
29645e4fb372SMengdong Lin 	struct device *dev = component->dev;
29655e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
29665e4fb372SMengdong Lin 
29675e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
29685e4fb372SMengdong Lin 
29695e4fb372SMengdong Lin 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
29705e4fb372SMengdong Lin 	if (dai == NULL)
29715e4fb372SMengdong Lin 		return NULL;
29725e4fb372SMengdong Lin 
29735e4fb372SMengdong Lin 	/*
29745e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
29755e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
29765e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
29775e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
29785e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
29795e4fb372SMengdong Lin 	 * component-less anymore.
29805e4fb372SMengdong Lin 	 */
29815e4fb372SMengdong Lin 	if (legacy_dai_naming &&
29825e4fb372SMengdong Lin 	    (dai_drv->id == 0 || dai_drv->name == NULL)) {
29835e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
29845e4fb372SMengdong Lin 	} else {
29855e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
29865e4fb372SMengdong Lin 		if (dai_drv->id)
29875e4fb372SMengdong Lin 			dai->id = dai_drv->id;
29885e4fb372SMengdong Lin 		else
29895e4fb372SMengdong Lin 			dai->id = component->num_dai;
29905e4fb372SMengdong Lin 	}
29915e4fb372SMengdong Lin 	if (dai->name == NULL) {
29925e4fb372SMengdong Lin 		kfree(dai);
29935e4fb372SMengdong Lin 		return NULL;
29945e4fb372SMengdong Lin 	}
29955e4fb372SMengdong Lin 
29965e4fb372SMengdong Lin 	dai->component = component;
29975e4fb372SMengdong Lin 	dai->dev = dev;
29985e4fb372SMengdong Lin 	dai->driver = dai_drv;
29995e4fb372SMengdong Lin 	if (!dai->driver->ops)
30005e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
30015e4fb372SMengdong Lin 
300215a0c645SKuninori Morimoto 	/* see for_each_component_dais */
300358bf4179SKuninori Morimoto 	list_add_tail(&dai->list, &component->dai_list);
30045e4fb372SMengdong Lin 	component->num_dai++;
30055e4fb372SMengdong Lin 
30065e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
30075e4fb372SMengdong Lin 	return dai;
30085e4fb372SMengdong Lin }
30095e4fb372SMengdong Lin 
30109115171aSMark Brown /**
301132c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
30129115171aSMark Brown  *
30136106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
30146106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
3015ac11a2b3SMark Brown  * @count: Number of DAIs
30169115171aSMark Brown  */
301732c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
30182c7b696aSMarcel Ziswiler 				 struct snd_soc_dai_driver *dai_drv,
30192c7b696aSMarcel Ziswiler 				 size_t count)
30209115171aSMark Brown {
30216106d129SLars-Peter Clausen 	struct device *dev = component->dev;
3022f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
302332c9ba54SLars-Peter Clausen 	unsigned int i;
302432c9ba54SLars-Peter Clausen 	int ret;
3025f0fba2adSLiam Girdwood 
30265b5e0928SAlexey Dobriyan 	dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
30279115171aSMark Brown 
30289115171aSMark Brown 	for (i = 0; i < count; i++) {
3029f0fba2adSLiam Girdwood 
30302c7b696aSMarcel Ziswiler 		dai = soc_add_dai(component, dai_drv + i, count == 1 &&
30312c7b696aSMarcel Ziswiler 				  !component->driver->non_legacy_dai_naming);
3032c46e0079SAxel Lin 		if (dai == NULL) {
3033c46e0079SAxel Lin 			ret = -ENOMEM;
3034c46e0079SAxel Lin 			goto err;
3035c46e0079SAxel Lin 		}
3036f0fba2adSLiam Girdwood 	}
3037f0fba2adSLiam Girdwood 
30389115171aSMark Brown 	return 0;
30399115171aSMark Brown 
30409115171aSMark Brown err:
304132c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
30429115171aSMark Brown 
30439115171aSMark Brown 	return ret;
30449115171aSMark Brown }
30459115171aSMark Brown 
304668003e6cSMengdong Lin /**
304768003e6cSMengdong Lin  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
304868003e6cSMengdong Lin  *
304968003e6cSMengdong Lin  * @component: The component the DAIs are registered for
305068003e6cSMengdong Lin  * @dai_drv: DAI driver to use for the DAI
305168003e6cSMengdong Lin  *
305268003e6cSMengdong Lin  * Topology can use this API to register DAIs when probing a component.
305368003e6cSMengdong Lin  * These DAIs's widgets will be freed in the card cleanup and the DAIs
305468003e6cSMengdong Lin  * will be freed in the component cleanup.
305568003e6cSMengdong Lin  */
305668003e6cSMengdong Lin int snd_soc_register_dai(struct snd_soc_component *component,
305768003e6cSMengdong Lin 	struct snd_soc_dai_driver *dai_drv)
305868003e6cSMengdong Lin {
305968003e6cSMengdong Lin 	struct snd_soc_dapm_context *dapm =
306068003e6cSMengdong Lin 		snd_soc_component_get_dapm(component);
306168003e6cSMengdong Lin 	struct snd_soc_dai *dai;
306268003e6cSMengdong Lin 	int ret;
306368003e6cSMengdong Lin 
306468003e6cSMengdong Lin 	if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
306568003e6cSMengdong Lin 		dev_err(component->dev, "Invalid dai type %d\n",
306668003e6cSMengdong Lin 			dai_drv->dobj.type);
306768003e6cSMengdong Lin 		return -EINVAL;
306868003e6cSMengdong Lin 	}
306968003e6cSMengdong Lin 
307068003e6cSMengdong Lin 	lockdep_assert_held(&client_mutex);
307168003e6cSMengdong Lin 	dai = soc_add_dai(component, dai_drv, false);
307268003e6cSMengdong Lin 	if (!dai)
307368003e6cSMengdong Lin 		return -ENOMEM;
307468003e6cSMengdong Lin 
30752c7b696aSMarcel Ziswiler 	/*
30762c7b696aSMarcel Ziswiler 	 * Create the DAI widgets here. After adding DAIs, topology may
307768003e6cSMengdong Lin 	 * also add routes that need these widgets as source or sink.
307868003e6cSMengdong Lin 	 */
307968003e6cSMengdong Lin 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
308068003e6cSMengdong Lin 	if (ret != 0) {
308168003e6cSMengdong Lin 		dev_err(component->dev,
308268003e6cSMengdong Lin 			"Failed to create DAI widgets %d\n", ret);
308368003e6cSMengdong Lin 	}
308468003e6cSMengdong Lin 
308568003e6cSMengdong Lin 	return ret;
308668003e6cSMengdong Lin }
308768003e6cSMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_register_dai);
308868003e6cSMengdong Lin 
308914e8bdebSLars-Peter Clausen static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
309014e8bdebSLars-Peter Clausen 	enum snd_soc_dapm_type type, int subseq)
3091d191bd8dSKuninori Morimoto {
309214e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
3093d191bd8dSKuninori Morimoto 
309414e8bdebSLars-Peter Clausen 	component->driver->seq_notifier(component, type, subseq);
309514e8bdebSLars-Peter Clausen }
3096d191bd8dSKuninori Morimoto 
309714e8bdebSLars-Peter Clausen static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
309814e8bdebSLars-Peter Clausen 	int event)
309914e8bdebSLars-Peter Clausen {
310014e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
310114e8bdebSLars-Peter Clausen 
310214e8bdebSLars-Peter Clausen 	return component->driver->stream_event(component, event);
310314e8bdebSLars-Peter Clausen }
310414e8bdebSLars-Peter Clausen 
31057ba236ceSKuninori Morimoto static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
31067ba236ceSKuninori Morimoto 					enum snd_soc_bias_level level)
31077ba236ceSKuninori Morimoto {
31087ba236ceSKuninori Morimoto 	struct snd_soc_component *component = dapm->component;
31097ba236ceSKuninori Morimoto 
31107ba236ceSKuninori Morimoto 	return component->driver->set_bias_level(component, level);
31117ba236ceSKuninori Morimoto }
31127ba236ceSKuninori Morimoto 
3113bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
3114bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
3115d191bd8dSKuninori Morimoto {
3116ce0fc93aSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
3117ce0fc93aSLars-Peter Clausen 
3118bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
3119bb13109dSLars-Peter Clausen 	if (!component->name) {
3120bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
3121d191bd8dSKuninori Morimoto 		return -ENOMEM;
3122d191bd8dSKuninori Morimoto 	}
3123d191bd8dSKuninori Morimoto 
3124bb13109dSLars-Peter Clausen 	component->dev = dev;
3125bb13109dSLars-Peter Clausen 	component->driver = driver;
3126e2c330b9SLars-Peter Clausen 
312788c27465SKuninori Morimoto 	dapm = snd_soc_component_get_dapm(component);
3128ce0fc93aSLars-Peter Clausen 	dapm->dev = dev;
3129ce0fc93aSLars-Peter Clausen 	dapm->component = component;
3130ce0fc93aSLars-Peter Clausen 	dapm->bias_level = SND_SOC_BIAS_OFF;
31317ba236ceSKuninori Morimoto 	dapm->idle_bias_off = !driver->idle_bias_on;
31327ba236ceSKuninori Morimoto 	dapm->suspend_bias_off = driver->suspend_bias_off;
313314e8bdebSLars-Peter Clausen 	if (driver->seq_notifier)
313414e8bdebSLars-Peter Clausen 		dapm->seq_notifier = snd_soc_component_seq_notifier;
313514e8bdebSLars-Peter Clausen 	if (driver->stream_event)
313614e8bdebSLars-Peter Clausen 		dapm->stream_event = snd_soc_component_stream_event;
31377ba236ceSKuninori Morimoto 	if (driver->set_bias_level)
31387ba236ceSKuninori Morimoto 		dapm->set_bias_level = snd_soc_component_set_bias_level;
3139ce0fc93aSLars-Peter Clausen 
3140bb13109dSLars-Peter Clausen 	INIT_LIST_HEAD(&component->dai_list);
3141bb13109dSLars-Peter Clausen 	mutex_init(&component->io_mutex);
3142bb13109dSLars-Peter Clausen 
3143bb13109dSLars-Peter Clausen 	return 0;
3144d191bd8dSKuninori Morimoto }
3145d191bd8dSKuninori Morimoto 
314620feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
3147886f5692SLars-Peter Clausen {
3148886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
314920feb881SLars-Peter Clausen 
3150886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
3151886f5692SLars-Peter Clausen 	if (val_bytes > 0)
3152886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
3153886f5692SLars-Peter Clausen }
315420feb881SLars-Peter Clausen 
3155e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
3156e874bf5fSLars-Peter Clausen 
315720feb881SLars-Peter Clausen /**
31582c7b696aSMarcel Ziswiler  * snd_soc_component_init_regmap() - Initialize regmap instance for the
31592c7b696aSMarcel Ziswiler  *                                   component
316020feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
316120feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
316220feb881SLars-Peter Clausen  *
316320feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
316420feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
316520feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
316620feb881SLars-Peter Clausen  * before the first IO attempt of the component.
316720feb881SLars-Peter Clausen  */
316820feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
316920feb881SLars-Peter Clausen 	struct regmap *regmap)
317020feb881SLars-Peter Clausen {
317120feb881SLars-Peter Clausen 	component->regmap = regmap;
317220feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
3173886f5692SLars-Peter Clausen }
317420feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
317520feb881SLars-Peter Clausen 
317620feb881SLars-Peter Clausen /**
31772c7b696aSMarcel Ziswiler  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
31782c7b696aSMarcel Ziswiler  *                                   component
317920feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
318020feb881SLars-Peter Clausen  *
318120feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
318220feb881SLars-Peter Clausen  * removes the regmap instance from the component.
318320feb881SLars-Peter Clausen  *
318420feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
318520feb881SLars-Peter Clausen  * to initialize the regmap instance.
318620feb881SLars-Peter Clausen  */
318720feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
318820feb881SLars-Peter Clausen {
318920feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
319020feb881SLars-Peter Clausen 	component->regmap = NULL;
319120feb881SLars-Peter Clausen }
319220feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3193886f5692SLars-Peter Clausen 
3194e874bf5fSLars-Peter Clausen #endif
3195d191bd8dSKuninori Morimoto 
3196359c71eeSKuninori Morimoto static void snd_soc_component_add(struct snd_soc_component *component)
3197bb13109dSLars-Peter Clausen {
3198359c71eeSKuninori Morimoto 	mutex_lock(&client_mutex);
3199359c71eeSKuninori Morimoto 
3200999f7f5aSKuninori Morimoto 	if (!component->driver->write && !component->driver->read) {
320120feb881SLars-Peter Clausen 		if (!component->regmap)
32022c7b696aSMarcel Ziswiler 			component->regmap = dev_get_regmap(component->dev,
32032c7b696aSMarcel Ziswiler 							   NULL);
320420feb881SLars-Peter Clausen 		if (component->regmap)
320520feb881SLars-Peter Clausen 			snd_soc_component_setup_regmap(component);
320620feb881SLars-Peter Clausen 	}
3207886f5692SLars-Peter Clausen 
3208368dee94SKuninori Morimoto 	/* see for_each_component */
3209bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
32108a978234SLiam Girdwood 	INIT_LIST_HEAD(&component->dobj_list);
3211d191bd8dSKuninori Morimoto 
3212d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
3213bb13109dSLars-Peter Clausen }
3214d191bd8dSKuninori Morimoto 
3215bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
3216bb13109dSLars-Peter Clausen {
3217bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
3218bb13109dSLars-Peter Clausen 	kfree(component->name);
3219bb13109dSLars-Peter Clausen }
3220d191bd8dSKuninori Morimoto 
3221bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3222bb13109dSLars-Peter Clausen {
3223c12c1aadSKuninori Morimoto 	struct snd_soc_card *card = component->card;
3224c12c1aadSKuninori Morimoto 
3225c12c1aadSKuninori Morimoto 	if (card)
3226e894efefSSrinivas Kandagatla 		snd_soc_unbind_card(card, false);
3227c12c1aadSKuninori Morimoto 
3228bb13109dSLars-Peter Clausen 	list_del(&component->list);
3229bb13109dSLars-Peter Clausen }
3230d191bd8dSKuninori Morimoto 
3231273d778eSKuninori Morimoto #define ENDIANNESS_MAP(name) \
3232273d778eSKuninori Morimoto 	(SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3233273d778eSKuninori Morimoto static u64 endianness_format_map[] = {
3234273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S16_),
3235273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U16_),
3236273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_),
3237273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_),
3238273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S32_),
3239273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U32_),
3240273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_3),
3241273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_3),
3242273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S20_3),
3243273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U20_3),
3244273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S18_3),
3245273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U18_3),
3246273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT_),
3247273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT64_),
3248273d778eSKuninori Morimoto 	ENDIANNESS_MAP(IEC958_SUBFRAME_),
3249273d778eSKuninori Morimoto };
3250273d778eSKuninori Morimoto 
3251273d778eSKuninori Morimoto /*
3252273d778eSKuninori Morimoto  * Fix up the DAI formats for endianness: codecs don't actually see
3253273d778eSKuninori Morimoto  * the endianness of the data but we're using the CPU format
3254273d778eSKuninori Morimoto  * definitions which do need to include endianness so we ensure that
3255273d778eSKuninori Morimoto  * codec DAIs always have both big and little endian variants set.
3256273d778eSKuninori Morimoto  */
3257273d778eSKuninori Morimoto static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3258273d778eSKuninori Morimoto {
3259273d778eSKuninori Morimoto 	int i;
3260273d778eSKuninori Morimoto 
3261273d778eSKuninori Morimoto 	for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3262273d778eSKuninori Morimoto 		if (stream->formats & endianness_format_map[i])
3263273d778eSKuninori Morimoto 			stream->formats |= endianness_format_map[i];
3264273d778eSKuninori Morimoto }
3265273d778eSKuninori Morimoto 
3266e894efefSSrinivas Kandagatla static void snd_soc_try_rebind_card(void)
3267e894efefSSrinivas Kandagatla {
3268e894efefSSrinivas Kandagatla 	struct snd_soc_card *card, *c;
3269e894efefSSrinivas Kandagatla 
3270e894efefSSrinivas Kandagatla 	if (!list_empty(&unbind_card_list)) {
3271e894efefSSrinivas Kandagatla 		list_for_each_entry_safe(card, c, &unbind_card_list, list) {
3272e894efefSSrinivas Kandagatla 			if (!snd_soc_bind_card(card))
3273e894efefSSrinivas Kandagatla 				list_del(&card->list);
3274e894efefSSrinivas Kandagatla 		}
3275e894efefSSrinivas Kandagatla 	}
3276e894efefSSrinivas Kandagatla }
3277e894efefSSrinivas Kandagatla 
3278e0dac41bSKuninori Morimoto int snd_soc_add_component(struct device *dev,
3279e0dac41bSKuninori Morimoto 			struct snd_soc_component *component,
3280cf9e829eSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
3281d191bd8dSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
3282d191bd8dSKuninori Morimoto 			int num_dai)
3283d191bd8dSKuninori Morimoto {
3284bb13109dSLars-Peter Clausen 	int ret;
3285273d778eSKuninori Morimoto 	int i;
3286d191bd8dSKuninori Morimoto 
3287cf9e829eSKuninori Morimoto 	ret = snd_soc_component_initialize(component, component_driver, dev);
3288bb13109dSLars-Peter Clausen 	if (ret)
3289bb13109dSLars-Peter Clausen 		goto err_free;
3290bb13109dSLars-Peter Clausen 
3291273d778eSKuninori Morimoto 	if (component_driver->endianness) {
3292273d778eSKuninori Morimoto 		for (i = 0; i < num_dai; i++) {
3293273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].playback);
3294273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].capture);
3295273d778eSKuninori Morimoto 		}
3296273d778eSKuninori Morimoto 	}
3297273d778eSKuninori Morimoto 
32980e7b25c6SKuninori Morimoto 	ret = snd_soc_register_dais(component, dai_drv, num_dai);
3299bb13109dSLars-Peter Clausen 	if (ret < 0) {
3300f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3301bb13109dSLars-Peter Clausen 		goto err_cleanup;
3302bb13109dSLars-Peter Clausen 	}
3303bb13109dSLars-Peter Clausen 
3304cf9e829eSKuninori Morimoto 	snd_soc_component_add(component);
3305e894efefSSrinivas Kandagatla 	snd_soc_try_rebind_card();
3306bb13109dSLars-Peter Clausen 
3307bb13109dSLars-Peter Clausen 	return 0;
3308bb13109dSLars-Peter Clausen 
3309bb13109dSLars-Peter Clausen err_cleanup:
3310cf9e829eSKuninori Morimoto 	snd_soc_component_cleanup(component);
3311bb13109dSLars-Peter Clausen err_free:
3312bb13109dSLars-Peter Clausen 	return ret;
3313d191bd8dSKuninori Morimoto }
3314e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component);
3315e0dac41bSKuninori Morimoto 
3316e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev,
3317e0dac41bSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
3318e0dac41bSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
3319e0dac41bSKuninori Morimoto 			int num_dai)
3320e0dac41bSKuninori Morimoto {
3321e0dac41bSKuninori Morimoto 	struct snd_soc_component *component;
3322e0dac41bSKuninori Morimoto 
33237ecbd6a9SKuninori Morimoto 	component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
332408e61d03SKuninori Morimoto 	if (!component)
3325e0dac41bSKuninori Morimoto 		return -ENOMEM;
3326e0dac41bSKuninori Morimoto 
3327e0dac41bSKuninori Morimoto 	return snd_soc_add_component(dev, component, component_driver,
3328e0dac41bSKuninori Morimoto 				     dai_drv, num_dai);
3329e0dac41bSKuninori Morimoto }
3330d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
3331d191bd8dSKuninori Morimoto 
3332d191bd8dSKuninori Morimoto /**
33332eccea8cSKuninori Morimoto  * snd_soc_unregister_component - Unregister all related component
33342eccea8cSKuninori Morimoto  * from the ASoC core
3335d191bd8dSKuninori Morimoto  *
3336628536eaSJonathan Corbet  * @dev: The device to unregister
3337d191bd8dSKuninori Morimoto  */
33382eccea8cSKuninori Morimoto static int __snd_soc_unregister_component(struct device *dev)
3339d191bd8dSKuninori Morimoto {
3340cf9e829eSKuninori Morimoto 	struct snd_soc_component *component;
334121a03528SKuninori Morimoto 	int found = 0;
3342d191bd8dSKuninori Morimoto 
334334e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
3344368dee94SKuninori Morimoto 	for_each_component(component) {
3345999f7f5aSKuninori Morimoto 		if (dev != component->dev)
334621a03528SKuninori Morimoto 			continue;
3347d191bd8dSKuninori Morimoto 
33482c7b696aSMarcel Ziswiler 		snd_soc_tplg_component_remove(component,
33492c7b696aSMarcel Ziswiler 					      SND_SOC_TPLG_INDEX_ALL);
3350cf9e829eSKuninori Morimoto 		snd_soc_component_del_unlocked(component);
335121a03528SKuninori Morimoto 		found = 1;
335221a03528SKuninori Morimoto 		break;
3353d191bd8dSKuninori Morimoto 	}
3354d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
3355d191bd8dSKuninori Morimoto 
33562c7b696aSMarcel Ziswiler 	if (found)
3357cf9e829eSKuninori Morimoto 		snd_soc_component_cleanup(component);
33582eccea8cSKuninori Morimoto 
33592eccea8cSKuninori Morimoto 	return found;
33602eccea8cSKuninori Morimoto }
33612eccea8cSKuninori Morimoto 
33622eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
33632eccea8cSKuninori Morimoto {
33642c7b696aSMarcel Ziswiler 	while (__snd_soc_unregister_component(dev))
33652c7b696aSMarcel Ziswiler 		;
3366d191bd8dSKuninori Morimoto }
3367d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3368d191bd8dSKuninori Morimoto 
33697dd5d0d9SKuninori Morimoto struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
33707dd5d0d9SKuninori Morimoto 						   const char *driver_name)
33717dd5d0d9SKuninori Morimoto {
33727dd5d0d9SKuninori Morimoto 	struct snd_soc_component *component;
33737dd5d0d9SKuninori Morimoto 	struct snd_soc_component *ret;
33747dd5d0d9SKuninori Morimoto 
33757dd5d0d9SKuninori Morimoto 	ret = NULL;
33767dd5d0d9SKuninori Morimoto 	mutex_lock(&client_mutex);
3377368dee94SKuninori Morimoto 	for_each_component(component) {
33787dd5d0d9SKuninori Morimoto 		if (dev != component->dev)
33797dd5d0d9SKuninori Morimoto 			continue;
33807dd5d0d9SKuninori Morimoto 
33817dd5d0d9SKuninori Morimoto 		if (driver_name &&
33827dd5d0d9SKuninori Morimoto 		    (driver_name != component->driver->name) &&
33837dd5d0d9SKuninori Morimoto 		    (strcmp(component->driver->name, driver_name) != 0))
33847dd5d0d9SKuninori Morimoto 			continue;
33857dd5d0d9SKuninori Morimoto 
33867dd5d0d9SKuninori Morimoto 		ret = component;
33877dd5d0d9SKuninori Morimoto 		break;
33887dd5d0d9SKuninori Morimoto 	}
33897dd5d0d9SKuninori Morimoto 	mutex_unlock(&client_mutex);
33907dd5d0d9SKuninori Morimoto 
33917dd5d0d9SKuninori Morimoto 	return ret;
33927dd5d0d9SKuninori Morimoto }
33937dd5d0d9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
33947dd5d0d9SKuninori Morimoto 
3395bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
3396b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3397bec4fa05SStephen Warren 			       const char *propname)
3398bec4fa05SStephen Warren {
3399b07609ceSKuninori Morimoto 	struct device_node *np;
3400bec4fa05SStephen Warren 	int ret;
3401bec4fa05SStephen Warren 
34027e07e7c0STushar Behera 	if (!card->dev) {
34037e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
34047e07e7c0STushar Behera 		return -EINVAL;
34057e07e7c0STushar Behera 	}
34067e07e7c0STushar Behera 
34077e07e7c0STushar Behera 	np = card->dev->of_node;
34087e07e7c0STushar Behera 
3409bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
3410bec4fa05SStephen Warren 	/*
3411bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
3412bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
3413bec4fa05SStephen Warren 	 * snd_soc_register_card.
3414bec4fa05SStephen Warren 	 */
3415bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
3416bec4fa05SStephen Warren 		dev_err(card->dev,
3417f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
3418bec4fa05SStephen Warren 			propname, ret);
3419bec4fa05SStephen Warren 		return ret;
3420bec4fa05SStephen Warren 	}
3421bec4fa05SStephen Warren 
3422bec4fa05SStephen Warren 	return 0;
3423bec4fa05SStephen Warren }
3424b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3425bec4fa05SStephen Warren 
34269a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
34279a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
34289a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
34299a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
34309a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
34319a6d4860SXiubo Li };
34329a6d4860SXiubo Li 
343321efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
34349a6d4860SXiubo Li 					  const char *propname)
34359a6d4860SXiubo Li {
343621efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
34379a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
34389a6d4860SXiubo Li 	const char *template, *wname;
34399a6d4860SXiubo Li 	int i, j, num_widgets, ret;
34409a6d4860SXiubo Li 
34419a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
34429a6d4860SXiubo Li 	if (num_widgets < 0) {
34439a6d4860SXiubo Li 		dev_err(card->dev,
34449a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
34459a6d4860SXiubo Li 		return -EINVAL;
34469a6d4860SXiubo Li 	}
34479a6d4860SXiubo Li 	if (num_widgets & 1) {
34489a6d4860SXiubo Li 		dev_err(card->dev,
34499a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
34509a6d4860SXiubo Li 		return -EINVAL;
34519a6d4860SXiubo Li 	}
34529a6d4860SXiubo Li 
34539a6d4860SXiubo Li 	num_widgets /= 2;
34549a6d4860SXiubo Li 	if (!num_widgets) {
34559a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
34569a6d4860SXiubo Li 			propname);
34579a6d4860SXiubo Li 		return -EINVAL;
34589a6d4860SXiubo Li 	}
34599a6d4860SXiubo Li 
34609a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
34619a6d4860SXiubo Li 			       GFP_KERNEL);
34629a6d4860SXiubo Li 	if (!widgets) {
34639a6d4860SXiubo Li 		dev_err(card->dev,
34649a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
34659a6d4860SXiubo Li 		return -ENOMEM;
34669a6d4860SXiubo Li 	}
34679a6d4860SXiubo Li 
34689a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
34699a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
34709a6d4860SXiubo Li 			2 * i, &template);
34719a6d4860SXiubo Li 		if (ret) {
34729a6d4860SXiubo Li 			dev_err(card->dev,
34739a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
34749a6d4860SXiubo Li 				propname, 2 * i, ret);
34759a6d4860SXiubo Li 			return -EINVAL;
34769a6d4860SXiubo Li 		}
34779a6d4860SXiubo Li 
34789a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
34799a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
34809a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
34819a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
34829a6d4860SXiubo Li 				break;
34839a6d4860SXiubo Li 			}
34849a6d4860SXiubo Li 		}
34859a6d4860SXiubo Li 
34869a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
34879a6d4860SXiubo Li 			dev_err(card->dev,
34889a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
34899a6d4860SXiubo Li 				template);
34909a6d4860SXiubo Li 			return -EINVAL;
34919a6d4860SXiubo Li 		}
34929a6d4860SXiubo Li 
34939a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
34949a6d4860SXiubo Li 						    (2 * i) + 1,
34959a6d4860SXiubo Li 						    &wname);
34969a6d4860SXiubo Li 		if (ret) {
34979a6d4860SXiubo Li 			dev_err(card->dev,
34989a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
34999a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
35009a6d4860SXiubo Li 			return -EINVAL;
35019a6d4860SXiubo Li 		}
35029a6d4860SXiubo Li 
35039a6d4860SXiubo Li 		widgets[i].name = wname;
35049a6d4860SXiubo Li 	}
35059a6d4860SXiubo Li 
3506f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
3507f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
35089a6d4860SXiubo Li 
35099a6d4860SXiubo Li 	return 0;
35109a6d4860SXiubo Li }
351121efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
35129a6d4860SXiubo Li 
3513cbdfab3bSJerome Brunet int snd_soc_of_get_slot_mask(struct device_node *np,
35146131084aSJyri Sarha 			     const char *prop_name,
35156131084aSJyri Sarha 			     unsigned int *mask)
35166131084aSJyri Sarha {
35176131084aSJyri Sarha 	u32 val;
35186c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
35196131084aSJyri Sarha 	int i;
35206131084aSJyri Sarha 
35216131084aSJyri Sarha 	if (!of_slot_mask)
35226131084aSJyri Sarha 		return 0;
35236131084aSJyri Sarha 	val /= sizeof(u32);
35246131084aSJyri Sarha 	for (i = 0; i < val; i++)
35256131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
35266131084aSJyri Sarha 			*mask |= (1 << i);
35276131084aSJyri Sarha 
35286131084aSJyri Sarha 	return val;
35296131084aSJyri Sarha }
3530cbdfab3bSJerome Brunet EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
35316131084aSJyri Sarha 
353289c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
35336131084aSJyri Sarha 			      unsigned int *tx_mask,
35346131084aSJyri Sarha 			      unsigned int *rx_mask,
353589c67857SXiubo Li 			      unsigned int *slots,
353689c67857SXiubo Li 			      unsigned int *slot_width)
353789c67857SXiubo Li {
353889c67857SXiubo Li 	u32 val;
353989c67857SXiubo Li 	int ret;
354089c67857SXiubo Li 
35416131084aSJyri Sarha 	if (tx_mask)
35426131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
35436131084aSJyri Sarha 	if (rx_mask)
35446131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
35456131084aSJyri Sarha 
354689c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
354789c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
354889c67857SXiubo Li 		if (ret)
354989c67857SXiubo Li 			return ret;
355089c67857SXiubo Li 
355189c67857SXiubo Li 		if (slots)
355289c67857SXiubo Li 			*slots = val;
355389c67857SXiubo Li 	}
355489c67857SXiubo Li 
355589c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
355689c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
355789c67857SXiubo Li 		if (ret)
355889c67857SXiubo Li 			return ret;
355989c67857SXiubo Li 
356089c67857SXiubo Li 		if (slot_width)
356189c67857SXiubo Li 			*slot_width = val;
356289c67857SXiubo Li 	}
356389c67857SXiubo Li 
356489c67857SXiubo Li 	return 0;
356589c67857SXiubo Li }
356689c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
356789c67857SXiubo Li 
35683b710356SKuninori Morimoto void snd_soc_of_parse_node_prefix(struct device_node *np,
35695e3cdaa2SKuninori Morimoto 				  struct snd_soc_codec_conf *codec_conf,
35705e3cdaa2SKuninori Morimoto 				  struct device_node *of_node,
35715e3cdaa2SKuninori Morimoto 				  const char *propname)
35725e3cdaa2SKuninori Morimoto {
35735e3cdaa2SKuninori Morimoto 	const char *str;
35745e3cdaa2SKuninori Morimoto 	int ret;
35755e3cdaa2SKuninori Morimoto 
35765e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
35775e3cdaa2SKuninori Morimoto 	if (ret < 0) {
35785e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
35795e3cdaa2SKuninori Morimoto 		return;
35805e3cdaa2SKuninori Morimoto 	}
35815e3cdaa2SKuninori Morimoto 
35825e3cdaa2SKuninori Morimoto 	codec_conf->of_node	= of_node;
35835e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
35845e3cdaa2SKuninori Morimoto }
35853b710356SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
35865e3cdaa2SKuninori Morimoto 
35872bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3588a4a54dd5SStephen Warren 				   const char *propname)
3589a4a54dd5SStephen Warren {
35902bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
3591e3b1e6a1SMark Brown 	int num_routes;
3592a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3593a4a54dd5SStephen Warren 	int i, ret;
3594a4a54dd5SStephen Warren 
3595a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3596c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
359710e8aa9aSMichał Mirosław 		dev_err(card->dev,
359810e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
359910e8aa9aSMichał Mirosław 			propname);
3600a4a54dd5SStephen Warren 		return -EINVAL;
3601a4a54dd5SStephen Warren 	}
3602a4a54dd5SStephen Warren 	num_routes /= 2;
3603a4a54dd5SStephen Warren 	if (!num_routes) {
3604f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3605a4a54dd5SStephen Warren 			propname);
3606a4a54dd5SStephen Warren 		return -EINVAL;
3607a4a54dd5SStephen Warren 	}
3608a4a54dd5SStephen Warren 
3609a86854d0SKees Cook 	routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
3610a4a54dd5SStephen Warren 			      GFP_KERNEL);
3611a4a54dd5SStephen Warren 	if (!routes) {
3612a4a54dd5SStephen Warren 		dev_err(card->dev,
3613f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3614a4a54dd5SStephen Warren 		return -EINVAL;
3615a4a54dd5SStephen Warren 	}
3616a4a54dd5SStephen Warren 
3617a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3618a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3619e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
3620a4a54dd5SStephen Warren 		if (ret) {
3621c871bd0bSMark Brown 			dev_err(card->dev,
3622c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3623c871bd0bSMark Brown 				propname, 2 * i, ret);
3624a4a54dd5SStephen Warren 			return -EINVAL;
3625a4a54dd5SStephen Warren 		}
3626a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3627e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3628a4a54dd5SStephen Warren 		if (ret) {
3629a4a54dd5SStephen Warren 			dev_err(card->dev,
3630c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3631c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3632a4a54dd5SStephen Warren 			return -EINVAL;
3633a4a54dd5SStephen Warren 		}
3634a4a54dd5SStephen Warren 	}
3635a4a54dd5SStephen Warren 
3636f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
3637f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
3638a4a54dd5SStephen Warren 
3639a4a54dd5SStephen Warren 	return 0;
3640a4a54dd5SStephen Warren }
36412bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3642a4a54dd5SStephen Warren 
3643a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3644389cb834SJyri Sarha 				     const char *prefix,
3645389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3646389cb834SJyri Sarha 				     struct device_node **framemaster)
3647a7930ed4SKuninori Morimoto {
3648a7930ed4SKuninori Morimoto 	int ret, i;
3649a7930ed4SKuninori Morimoto 	char prop[128];
3650a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3651a7930ed4SKuninori Morimoto 	int bit, frame;
3652a7930ed4SKuninori Morimoto 	const char *str;
3653a7930ed4SKuninori Morimoto 	struct {
3654a7930ed4SKuninori Morimoto 		char *name;
3655a7930ed4SKuninori Morimoto 		unsigned int val;
3656a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3657a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3658a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3659a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3660a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3661a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3662a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3663a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3664a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3665a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3666a7930ed4SKuninori Morimoto 	};
3667a7930ed4SKuninori Morimoto 
3668a7930ed4SKuninori Morimoto 	if (!prefix)
3669a7930ed4SKuninori Morimoto 		prefix = "";
3670a7930ed4SKuninori Morimoto 
3671a7930ed4SKuninori Morimoto 	/*
36725711c979SKuninori Morimoto 	 * check "dai-format = xxx"
36735711c979SKuninori Morimoto 	 * or    "[prefix]format = xxx"
3674a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3675a7930ed4SKuninori Morimoto 	 */
36765711c979SKuninori Morimoto 	ret = of_property_read_string(np, "dai-format", &str);
36775711c979SKuninori Morimoto 	if (ret < 0) {
3678a7930ed4SKuninori Morimoto 		snprintf(prop, sizeof(prop), "%sformat", prefix);
3679a7930ed4SKuninori Morimoto 		ret = of_property_read_string(np, prop, &str);
36805711c979SKuninori Morimoto 	}
3681a7930ed4SKuninori Morimoto 	if (ret == 0) {
3682a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3683a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3684a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3685a7930ed4SKuninori Morimoto 				break;
3686a7930ed4SKuninori Morimoto 			}
3687a7930ed4SKuninori Morimoto 		}
3688a7930ed4SKuninori Morimoto 	}
3689a7930ed4SKuninori Morimoto 
3690a7930ed4SKuninori Morimoto 	/*
36918c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3692a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3693a7930ed4SKuninori Morimoto 	 */
36948c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
369551930295SJulia Lawall 	if (of_property_read_bool(np, prop))
36968c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
36978c2d6a9fSKuninori Morimoto 	else
36988c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3699a7930ed4SKuninori Morimoto 
3700a7930ed4SKuninori Morimoto 	/*
3701a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3702a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3703a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3704a7930ed4SKuninori Morimoto 	 */
3705a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3706a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3707a7930ed4SKuninori Morimoto 
3708a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3709a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3710a7930ed4SKuninori Morimoto 
3711a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3712a7930ed4SKuninori Morimoto 	case 0x11:
3713a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3714a7930ed4SKuninori Morimoto 		break;
3715a7930ed4SKuninori Morimoto 	case 0x10:
3716a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3717a7930ed4SKuninori Morimoto 		break;
3718a7930ed4SKuninori Morimoto 	case 0x01:
3719a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3720a7930ed4SKuninori Morimoto 		break;
3721a7930ed4SKuninori Morimoto 	default:
3722a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3723a7930ed4SKuninori Morimoto 		break;
3724a7930ed4SKuninori Morimoto 	}
3725a7930ed4SKuninori Morimoto 
3726a7930ed4SKuninori Morimoto 	/*
3727a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3728a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3729a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3730a7930ed4SKuninori Morimoto 	 */
3731a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3732a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3733389cb834SJyri Sarha 	if (bit && bitclkmaster)
3734389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3735a7930ed4SKuninori Morimoto 
3736a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3737a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3738389cb834SJyri Sarha 	if (frame && framemaster)
3739389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3740a7930ed4SKuninori Morimoto 
3741a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3742a7930ed4SKuninori Morimoto 	case 0x11:
3743a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3744a7930ed4SKuninori Morimoto 		break;
3745a7930ed4SKuninori Morimoto 	case 0x10:
3746a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3747a7930ed4SKuninori Morimoto 		break;
3748a7930ed4SKuninori Morimoto 	case 0x01:
3749a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3750a7930ed4SKuninori Morimoto 		break;
3751a7930ed4SKuninori Morimoto 	default:
3752a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3753a7930ed4SKuninori Morimoto 		break;
3754a7930ed4SKuninori Morimoto 	}
3755a7930ed4SKuninori Morimoto 
3756a7930ed4SKuninori Morimoto 	return format;
3757a7930ed4SKuninori Morimoto }
3758a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3759a7930ed4SKuninori Morimoto 
3760a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
3761a180e8b9SKuninori Morimoto {
3762a180e8b9SKuninori Morimoto 	struct snd_soc_component *pos;
3763a180e8b9SKuninori Morimoto 	struct device_node *node;
3764a180e8b9SKuninori Morimoto 	int ret;
3765a180e8b9SKuninori Morimoto 
3766a180e8b9SKuninori Morimoto 	node = of_graph_get_port_parent(ep);
3767a180e8b9SKuninori Morimoto 
3768a180e8b9SKuninori Morimoto 	/*
3769a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
3770a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
3771a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
3772a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
3773a180e8b9SKuninori Morimoto 	 */
3774a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
3775a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
3776368dee94SKuninori Morimoto 	for_each_component(pos) {
3777a180e8b9SKuninori Morimoto 		struct device_node *component_of_node = pos->dev->of_node;
3778a180e8b9SKuninori Morimoto 
3779a180e8b9SKuninori Morimoto 		if (!component_of_node && pos->dev->parent)
3780a180e8b9SKuninori Morimoto 			component_of_node = pos->dev->parent->of_node;
3781a180e8b9SKuninori Morimoto 
3782a180e8b9SKuninori Morimoto 		if (component_of_node != node)
3783a180e8b9SKuninori Morimoto 			continue;
3784a180e8b9SKuninori Morimoto 
3785a180e8b9SKuninori Morimoto 		if (pos->driver->of_xlate_dai_id)
3786a180e8b9SKuninori Morimoto 			ret = pos->driver->of_xlate_dai_id(pos, ep);
3787a180e8b9SKuninori Morimoto 
3788a180e8b9SKuninori Morimoto 		break;
3789a180e8b9SKuninori Morimoto 	}
3790a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
3791a180e8b9SKuninori Morimoto 
3792c0a480d1STony Lindgren 	of_node_put(node);
3793c0a480d1STony Lindgren 
3794a180e8b9SKuninori Morimoto 	return ret;
3795a180e8b9SKuninori Morimoto }
3796a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3797a180e8b9SKuninori Morimoto 
37981ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args,
3799cb470087SKuninori Morimoto 				const char **dai_name)
3800cb470087SKuninori Morimoto {
3801cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
38023e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
380393b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3804cb470087SKuninori Morimoto 
3805cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3806368dee94SKuninori Morimoto 	for_each_component(pos) {
38073e0aa8d8SJyri Sarha 		component_of_node = pos->dev->of_node;
38083e0aa8d8SJyri Sarha 		if (!component_of_node && pos->dev->parent)
38093e0aa8d8SJyri Sarha 			component_of_node = pos->dev->parent->of_node;
38103e0aa8d8SJyri Sarha 
38113e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
3812cb470087SKuninori Morimoto 			continue;
3813cb470087SKuninori Morimoto 
38146833c452SKuninori Morimoto 		if (pos->driver->of_xlate_dai_name) {
381593b0f3eeSJean-Francois Moine 			ret = pos->driver->of_xlate_dai_name(pos,
381693b0f3eeSJean-Francois Moine 							     args,
381793b0f3eeSJean-Francois Moine 							     dai_name);
38186833c452SKuninori Morimoto 		} else {
381958bf4179SKuninori Morimoto 			struct snd_soc_dai *dai;
38206833c452SKuninori Morimoto 			int id = -1;
38216833c452SKuninori Morimoto 
382293b0f3eeSJean-Francois Moine 			switch (args->args_count) {
38236833c452SKuninori Morimoto 			case 0:
38246833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
38256833c452SKuninori Morimoto 				break;
38266833c452SKuninori Morimoto 			case 1:
382793b0f3eeSJean-Francois Moine 				id = args->args[0];
38286833c452SKuninori Morimoto 				break;
38296833c452SKuninori Morimoto 			default:
38306833c452SKuninori Morimoto 				/* not supported */
3831cb470087SKuninori Morimoto 				break;
3832cb470087SKuninori Morimoto 			}
3833cb470087SKuninori Morimoto 
38346833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
38356833c452SKuninori Morimoto 				ret = -EINVAL;
38363dcba280SNicolin Chen 				continue;
38376833c452SKuninori Morimoto 			}
3838e41975edSXiubo Li 
3839e41975edSXiubo Li 			ret = 0;
3840e41975edSXiubo Li 
384158bf4179SKuninori Morimoto 			/* find target DAI */
384215a0c645SKuninori Morimoto 			for_each_component_dais(pos, dai) {
384358bf4179SKuninori Morimoto 				if (id == 0)
384458bf4179SKuninori Morimoto 					break;
384558bf4179SKuninori Morimoto 				id--;
384658bf4179SKuninori Morimoto 			}
384758bf4179SKuninori Morimoto 
384858bf4179SKuninori Morimoto 			*dai_name = dai->driver->name;
3849e41975edSXiubo Li 			if (!*dai_name)
3850e41975edSXiubo Li 				*dai_name = pos->name;
38516833c452SKuninori Morimoto 		}
38526833c452SKuninori Morimoto 
3853cb470087SKuninori Morimoto 		break;
3854cb470087SKuninori Morimoto 	}
3855cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
385693b0f3eeSJean-Francois Moine 	return ret;
385793b0f3eeSJean-Francois Moine }
38581ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
385993b0f3eeSJean-Francois Moine 
386093b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
386193b0f3eeSJean-Francois Moine 			    const char **dai_name)
386293b0f3eeSJean-Francois Moine {
386393b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
386493b0f3eeSJean-Francois Moine 	int ret;
386593b0f3eeSJean-Francois Moine 
386693b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
386793b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
386893b0f3eeSJean-Francois Moine 	if (ret)
386993b0f3eeSJean-Francois Moine 		return ret;
387093b0f3eeSJean-Francois Moine 
387193b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3872cb470087SKuninori Morimoto 
3873cb470087SKuninori Morimoto 	of_node_put(args.np);
3874cb470087SKuninori Morimoto 
3875cb470087SKuninori Morimoto 	return ret;
3876cb470087SKuninori Morimoto }
3877cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3878cb470087SKuninori Morimoto 
387993b0f3eeSJean-Francois Moine /*
388094685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
388194685763SSylwester Nawrocki  * @dai_link: DAI link
388294685763SSylwester Nawrocki  *
388394685763SSylwester Nawrocki  * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
388494685763SSylwester Nawrocki  */
388594685763SSylwester Nawrocki void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
388694685763SSylwester Nawrocki {
38873db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *component;
388894685763SSylwester Nawrocki 	int index;
388994685763SSylwester Nawrocki 
38903db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
389194685763SSylwester Nawrocki 		if (!component->of_node)
389294685763SSylwester Nawrocki 			break;
389394685763SSylwester Nawrocki 		of_node_put(component->of_node);
389494685763SSylwester Nawrocki 		component->of_node = NULL;
389594685763SSylwester Nawrocki 	}
389694685763SSylwester Nawrocki }
389794685763SSylwester Nawrocki EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
389894685763SSylwester Nawrocki 
389994685763SSylwester Nawrocki /*
390093b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
390193b0f3eeSJean-Francois Moine  * @dev: Card device
390293b0f3eeSJean-Francois Moine  * @of_node: Device node
390393b0f3eeSJean-Francois Moine  * @dai_link: DAI link
390493b0f3eeSJean-Francois Moine  *
390593b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
390693b0f3eeSJean-Francois Moine  * 'sound-dai'.
390793b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
390894685763SSylwester Nawrocki  * The device nodes in the array (of_node) must be dereferenced by calling
390994685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs() on @dai_link.
391093b0f3eeSJean-Francois Moine  *
391193b0f3eeSJean-Francois Moine  * Returns 0 for success
391293b0f3eeSJean-Francois Moine  */
391393b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
391493b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
391593b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
391693b0f3eeSJean-Francois Moine {
391793b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
391893b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
391993b0f3eeSJean-Francois Moine 	char *name;
392093b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
392193b0f3eeSJean-Francois Moine 
392293b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
392393b0f3eeSJean-Francois Moine 	name = "sound-dai";
392493b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
392593b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
392693b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
392793b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
392893b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
392993b0f3eeSJean-Francois Moine 		else
393093b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
393193b0f3eeSJean-Francois Moine 		return num_codecs;
393293b0f3eeSJean-Francois Moine 	}
3933a86854d0SKees Cook 	component = devm_kcalloc(dev,
3934a86854d0SKees Cook 				 num_codecs, sizeof(*component),
393593b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
393693b0f3eeSJean-Francois Moine 	if (!component)
393793b0f3eeSJean-Francois Moine 		return -ENOMEM;
393893b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
393993b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
394093b0f3eeSJean-Francois Moine 
394193b0f3eeSJean-Francois Moine 	/* Parse the list */
39423db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
394393b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
394493b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
394593b0f3eeSJean-Francois Moine 						 index, &args);
394693b0f3eeSJean-Francois Moine 		if (ret)
394793b0f3eeSJean-Francois Moine 			goto err;
394893b0f3eeSJean-Francois Moine 		component->of_node = args.np;
394993b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
395093b0f3eeSJean-Francois Moine 		if (ret < 0)
395193b0f3eeSJean-Francois Moine 			goto err;
395293b0f3eeSJean-Francois Moine 	}
395393b0f3eeSJean-Francois Moine 	return 0;
395493b0f3eeSJean-Francois Moine err:
395594685763SSylwester Nawrocki 	snd_soc_of_put_dai_link_codecs(dai_link);
395693b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
395793b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
395893b0f3eeSJean-Francois Moine 	return ret;
395993b0f3eeSJean-Francois Moine }
396093b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
396193b0f3eeSJean-Francois Moine 
3962c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3963db2a4165SFrank Mandarino {
39646553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
3965fb257897SMark Brown 	snd_soc_util_init();
3966fb257897SMark Brown 
3967db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3968db2a4165SFrank Mandarino }
39694abe8e16SMark Brown module_init(snd_soc_init);
3970db2a4165SFrank Mandarino 
39717d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3972db2a4165SFrank Mandarino {
3973fb257897SMark Brown 	snd_soc_util_exit();
39746553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3975fb257897SMark Brown 
3976db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3977db2a4165SFrank Mandarino }
3978db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3979db2a4165SFrank Mandarino 
3980db2a4165SFrank Mandarino /* Module information */
3981d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3982db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3983db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
3984db2a4165SFrank Mandarino MODULE_ALIAS("platform:soc-audio");
3985