xref: /openbmc/linux/sound/soc/soc-core.c (revision 50014499e6a45edd7ba1facf2133c03bbc7d8266)
1873486edSKuninori Morimoto // SPDX-License-Identifier: GPL-2.0+
2873486edSKuninori Morimoto //
3873486edSKuninori Morimoto // soc-core.c  --  ALSA SoC Audio Layer
4873486edSKuninori Morimoto //
5873486edSKuninori Morimoto // Copyright 2005 Wolfson Microelectronics PLC.
6873486edSKuninori Morimoto // Copyright 2005 Openedhand Ltd.
7873486edSKuninori Morimoto // Copyright (C) 2010 Slimlogic Ltd.
8873486edSKuninori Morimoto // Copyright (C) 2010 Texas Instruments Inc.
9873486edSKuninori Morimoto //
10873486edSKuninori Morimoto // Author: Liam Girdwood <lrg@slimlogic.co.uk>
11873486edSKuninori Morimoto //         with code, comments and ideas from :-
12873486edSKuninori Morimoto //         Richard Purdie <richard@openedhand.com>
13873486edSKuninori Morimoto //
14873486edSKuninori Morimoto //  TODO:
15873486edSKuninori Morimoto //   o Add hw rules to enforce rates, etc.
16873486edSKuninori Morimoto //   o More testing with other codecs/machines.
17873486edSKuninori Morimoto //   o Add more codecs and platforms to ensure good API coverage.
18873486edSKuninori Morimoto //   o Support TDM on PCM and I2S
19db2a4165SFrank Mandarino 
20db2a4165SFrank Mandarino #include <linux/module.h>
21db2a4165SFrank Mandarino #include <linux/moduleparam.h>
22db2a4165SFrank Mandarino #include <linux/init.h>
23db2a4165SFrank Mandarino #include <linux/delay.h>
24db2a4165SFrank Mandarino #include <linux/pm.h>
25db2a4165SFrank Mandarino #include <linux/bitops.h>
2612ef193dSTroy Kisky #include <linux/debugfs.h>
27db2a4165SFrank Mandarino #include <linux/platform_device.h>
28741a509fSMarkus Pargmann #include <linux/pinctrl/consumer.h>
29f0e8ed85SMark Brown #include <linux/ctype.h>
305a0e3ad6STejun Heo #include <linux/slab.h>
31bec4fa05SStephen Warren #include <linux/of.h>
32a180e8b9SKuninori Morimoto #include <linux/of_graph.h>
33345233d7SLiam Girdwood #include <linux/dmi.h>
34db2a4165SFrank Mandarino #include <sound/core.h>
353028eb8cSMark Brown #include <sound/jack.h>
36db2a4165SFrank Mandarino #include <sound/pcm.h>
37db2a4165SFrank Mandarino #include <sound/pcm_params.h>
38db2a4165SFrank Mandarino #include <sound/soc.h>
3901d7584cSLiam Girdwood #include <sound/soc-dpcm.h>
408a978234SLiam Girdwood #include <sound/soc-topology.h>
41db2a4165SFrank Mandarino #include <sound/initval.h>
42db2a4165SFrank Mandarino 
43a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
44a8b1d34fSMark Brown #include <trace/events/asoc.h>
45a8b1d34fSMark Brown 
46f0fba2adSLiam Girdwood #define NAME_SIZE	32
47f0fba2adSLiam Girdwood 
48384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
498a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root;
508a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
51384c89e2SMark Brown #endif
52384c89e2SMark Brown 
53c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
54030e79f6SKuninori Morimoto static LIST_HEAD(component_list);
55e894efefSSrinivas Kandagatla static LIST_HEAD(unbind_card_list);
56c5af3a2eSMark Brown 
57368dee94SKuninori Morimoto #define for_each_component(component)			\
58368dee94SKuninori Morimoto 	list_for_each_entry(component, &component_list, list)
59368dee94SKuninori Morimoto 
60db2a4165SFrank Mandarino /*
61587c9844SKuninori Morimoto  * This is used if driver don't need to have CPU/Codec/Platform
62587c9844SKuninori Morimoto  * dai_link. see soc.h
63587c9844SKuninori Morimoto  */
64587c9844SKuninori Morimoto struct snd_soc_dai_link_component null_dailink_component[0];
65587c9844SKuninori Morimoto EXPORT_SYMBOL_GPL(null_dailink_component);
66587c9844SKuninori Morimoto 
67587c9844SKuninori Morimoto /*
68db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
69db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
70db2a4165SFrank Mandarino  * between two audio tracks.
71db2a4165SFrank Mandarino  */
72db2a4165SFrank Mandarino static int pmdown_time = 5000;
73db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
74db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
75db2a4165SFrank Mandarino 
760faf1237SYueHaibing #ifdef CONFIG_DMI
772c7b696aSMarcel Ziswiler /*
782c7b696aSMarcel Ziswiler  * If a DMI filed contain strings in this blacklist (e.g.
7998faf436SMengdong Lin  * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
8098faf436SMengdong Lin  * as invalid and dropped when setting the card long name from DMI info.
8198faf436SMengdong Lin  */
8298faf436SMengdong Lin static const char * const dmi_blacklist[] = {
8398faf436SMengdong Lin 	"To be filled by OEM",
8498faf436SMengdong Lin 	"TBD by OEM",
8598faf436SMengdong Lin 	"Default String",
8698faf436SMengdong Lin 	"Board Manufacturer",
8798faf436SMengdong Lin 	"Board Vendor Name",
8898faf436SMengdong Lin 	"Board Product Name",
8998faf436SMengdong Lin 	NULL,	/* terminator */
9098faf436SMengdong Lin };
910faf1237SYueHaibing #endif
9298faf436SMengdong Lin 
93dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
94dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
95dbe21408SMark Brown {
9636ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
97dbe21408SMark Brown 
98f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
99dbe21408SMark Brown }
100dbe21408SMark Brown 
101dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
102dbe21408SMark Brown 			       struct device_attribute *attr,
103dbe21408SMark Brown 			       const char *buf, size_t count)
104dbe21408SMark Brown {
10536ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
106c593b520SMark Brown 	int ret;
107dbe21408SMark Brown 
108b785a492SJingoo Han 	ret = kstrtol(buf, 10, &rtd->pmdown_time);
109c593b520SMark Brown 	if (ret)
110c593b520SMark Brown 		return ret;
111dbe21408SMark Brown 
112dbe21408SMark Brown 	return count;
113dbe21408SMark Brown }
114dbe21408SMark Brown 
115dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
116dbe21408SMark Brown 
117d29697dcSTakashi Iwai static struct attribute *soc_dev_attrs[] = {
118d29697dcSTakashi Iwai 	&dev_attr_pmdown_time.attr,
119d29697dcSTakashi Iwai 	NULL
120d29697dcSTakashi Iwai };
121d29697dcSTakashi Iwai 
122d29697dcSTakashi Iwai static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
123d29697dcSTakashi Iwai 				       struct attribute *attr, int idx)
124d29697dcSTakashi Iwai {
125d29697dcSTakashi Iwai 	struct device *dev = kobj_to_dev(kobj);
126d29697dcSTakashi Iwai 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
127d29697dcSTakashi Iwai 
128d918a376SKuninori Morimoto 	if (!rtd)
129d918a376SKuninori Morimoto 		return 0;
130d918a376SKuninori Morimoto 
131d29697dcSTakashi Iwai 	if (attr == &dev_attr_pmdown_time.attr)
132d29697dcSTakashi Iwai 		return attr->mode; /* always visible */
1333b6eed8dSKuninori Morimoto 	return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
134d29697dcSTakashi Iwai }
135d29697dcSTakashi Iwai 
136d29697dcSTakashi Iwai static const struct attribute_group soc_dapm_dev_group = {
137d29697dcSTakashi Iwai 	.attrs = soc_dapm_dev_attrs,
138d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
139d29697dcSTakashi Iwai };
140d29697dcSTakashi Iwai 
141f7e73b26SMark Brown static const struct attribute_group soc_dev_group = {
142d29697dcSTakashi Iwai 	.attrs = soc_dev_attrs,
143d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
144d29697dcSTakashi Iwai };
145d29697dcSTakashi Iwai 
146d29697dcSTakashi Iwai static const struct attribute_group *soc_dev_attr_groups[] = {
147d29697dcSTakashi Iwai 	&soc_dapm_dev_group,
148f7e73b26SMark Brown 	&soc_dev_group,
149d29697dcSTakashi Iwai 	NULL
150d29697dcSTakashi Iwai };
151d29697dcSTakashi Iwai 
1522624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
15381c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component)
154e73f3de5SRussell King {
1556553bf06SLars-Peter Clausen 	if (!component->card->debugfs_card_root)
1566553bf06SLars-Peter Clausen 		return;
1576553bf06SLars-Peter Clausen 
15881c7cfd1SLars-Peter Clausen 	if (component->debugfs_prefix) {
15981c7cfd1SLars-Peter Clausen 		char *name;
160e73f3de5SRussell King 
16181c7cfd1SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s:%s",
16281c7cfd1SLars-Peter Clausen 			component->debugfs_prefix, component->name);
16381c7cfd1SLars-Peter Clausen 		if (name) {
16481c7cfd1SLars-Peter Clausen 			component->debugfs_root = debugfs_create_dir(name,
16581c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
16681c7cfd1SLars-Peter Clausen 			kfree(name);
16781c7cfd1SLars-Peter Clausen 		}
16881c7cfd1SLars-Peter Clausen 	} else {
16981c7cfd1SLars-Peter Clausen 		component->debugfs_root = debugfs_create_dir(component->name,
17081c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
171e73f3de5SRussell King 	}
172e73f3de5SRussell King 
17381c7cfd1SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
17481c7cfd1SLars-Peter Clausen 		component->debugfs_root);
17581c7cfd1SLars-Peter Clausen }
17681c7cfd1SLars-Peter Clausen 
17781c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
17881c7cfd1SLars-Peter Clausen {
179ad64bfbdSKuninori Morimoto 	if (!component->debugfs_root)
180ad64bfbdSKuninori Morimoto 		return;
18181c7cfd1SLars-Peter Clausen 	debugfs_remove_recursive(component->debugfs_root);
182ad64bfbdSKuninori Morimoto 	component->debugfs_root = NULL;
18381c7cfd1SLars-Peter Clausen }
18481c7cfd1SLars-Peter Clausen 
185c15b2a1dSPeng Donglin static int dai_list_show(struct seq_file *m, void *v)
186f3208780SMark Brown {
1871438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
188f3208780SMark Brown 	struct snd_soc_dai *dai;
189f3208780SMark Brown 
19034e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
19134e81ab4SLars-Peter Clausen 
192368dee94SKuninori Morimoto 	for_each_component(component)
19315a0c645SKuninori Morimoto 		for_each_component_dais(component, dai)
194700c17caSDonglin Peng 			seq_printf(m, "%s\n", dai->name);
195f3208780SMark Brown 
19634e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
19734e81ab4SLars-Peter Clausen 
198700c17caSDonglin Peng 	return 0;
199700c17caSDonglin Peng }
200c15b2a1dSPeng Donglin DEFINE_SHOW_ATTRIBUTE(dai_list);
201f3208780SMark Brown 
202db795f9bSKuninori Morimoto static int component_list_show(struct seq_file *m, void *v)
203db795f9bSKuninori Morimoto {
204db795f9bSKuninori Morimoto 	struct snd_soc_component *component;
205db795f9bSKuninori Morimoto 
206db795f9bSKuninori Morimoto 	mutex_lock(&client_mutex);
207db795f9bSKuninori Morimoto 
208368dee94SKuninori Morimoto 	for_each_component(component)
209db795f9bSKuninori Morimoto 		seq_printf(m, "%s\n", component->name);
210db795f9bSKuninori Morimoto 
211db795f9bSKuninori Morimoto 	mutex_unlock(&client_mutex);
212db795f9bSKuninori Morimoto 
213db795f9bSKuninori Morimoto 	return 0;
214db795f9bSKuninori Morimoto }
215db795f9bSKuninori Morimoto DEFINE_SHOW_ATTRIBUTE(component_list);
216db795f9bSKuninori Morimoto 
217a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
218a6052154SJarkko Nikula {
219a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
2208a9dab1aSMark Brown 						     snd_soc_debugfs_root);
2213a45b867SJarkko Nikula 
222fee531d6SGreg Kroah-Hartman 	debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root,
2233a45b867SJarkko Nikula 			   &card->pop_time);
224d8ca7a0aSKuninori Morimoto 
225d8ca7a0aSKuninori Morimoto 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
226a6052154SJarkko Nikula }
227a6052154SJarkko Nikula 
228a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
229a6052154SJarkko Nikula {
230a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
23129040d1aSKuninori Morimoto 	card->debugfs_card_root = NULL;
232a6052154SJarkko Nikula }
233a6052154SJarkko Nikula 
2346553bf06SLars-Peter Clausen static void snd_soc_debugfs_init(void)
2356553bf06SLars-Peter Clausen {
2366553bf06SLars-Peter Clausen 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
2376553bf06SLars-Peter Clausen 
238fee531d6SGreg Kroah-Hartman 	debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
239fee531d6SGreg Kroah-Hartman 			    &dai_list_fops);
240db795f9bSKuninori Morimoto 
241fee531d6SGreg Kroah-Hartman 	debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
242fee531d6SGreg Kroah-Hartman 			    &component_list_fops);
2436553bf06SLars-Peter Clausen }
2446553bf06SLars-Peter Clausen 
2456553bf06SLars-Peter Clausen static void snd_soc_debugfs_exit(void)
2466553bf06SLars-Peter Clausen {
2476553bf06SLars-Peter Clausen 	debugfs_remove_recursive(snd_soc_debugfs_root);
2486553bf06SLars-Peter Clausen }
2496553bf06SLars-Peter Clausen 
2502624d5faSMark Brown #else
2512624d5faSMark Brown 
25281c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs(
25381c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
2542624d5faSMark Brown {
2552624d5faSMark Brown }
2562624d5faSMark Brown 
25781c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs(
25881c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
259731f1ab2SSebastien Guiriec {
260731f1ab2SSebastien Guiriec }
261731f1ab2SSebastien Guiriec 
262b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
263b95fccbcSAxel Lin {
264b95fccbcSAxel Lin }
265b95fccbcSAxel Lin 
266b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
267b95fccbcSAxel Lin {
268b95fccbcSAxel Lin }
2696553bf06SLars-Peter Clausen 
2706553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_init(void)
2716553bf06SLars-Peter Clausen {
2726553bf06SLars-Peter Clausen }
2736553bf06SLars-Peter Clausen 
2746553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_exit(void)
2756553bf06SLars-Peter Clausen {
2766553bf06SLars-Peter Clausen }
2776553bf06SLars-Peter Clausen 
2782624d5faSMark Brown #endif
2792624d5faSMark Brown 
280a0ac4411SKuninori Morimoto static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
281a0ac4411SKuninori Morimoto 			      struct snd_soc_component *component)
282a0ac4411SKuninori Morimoto {
283a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
284a0ac4411SKuninori Morimoto 
285a0ac4411SKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
286a0ac4411SKuninori Morimoto 		/* already connected */
287a0ac4411SKuninori Morimoto 		if (rtdcom->component == component)
288a0ac4411SKuninori Morimoto 			return 0;
289a0ac4411SKuninori Morimoto 	}
290a0ac4411SKuninori Morimoto 
291353e16bfSKuninori Morimoto 	/*
292353e16bfSKuninori Morimoto 	 * created rtdcom here will be freed when rtd->dev was freed.
293353e16bfSKuninori Morimoto 	 * see
294353e16bfSKuninori Morimoto 	 *	soc_free_pcm_runtime() :: device_unregister(rtd->dev)
295353e16bfSKuninori Morimoto 	 */
296353e16bfSKuninori Morimoto 	rtdcom = devm_kzalloc(rtd->dev, sizeof(*rtdcom), GFP_KERNEL);
29732d2c172SKuninori Morimoto 	if (!rtdcom)
298a0ac4411SKuninori Morimoto 		return -ENOMEM;
299a0ac4411SKuninori Morimoto 
30032d2c172SKuninori Morimoto 	rtdcom->component = component;
30132d2c172SKuninori Morimoto 	INIT_LIST_HEAD(&rtdcom->list);
302a0ac4411SKuninori Morimoto 
303353e16bfSKuninori Morimoto 	/*
304353e16bfSKuninori Morimoto 	 * When rtd was freed, created rtdcom here will be
305353e16bfSKuninori Morimoto 	 * also freed.
306353e16bfSKuninori Morimoto 	 * And we don't need to call list_del(&rtdcom->list)
307353e16bfSKuninori Morimoto 	 * when freed, because rtd is also freed.
308353e16bfSKuninori Morimoto 	 */
30932d2c172SKuninori Morimoto 	list_add_tail(&rtdcom->list, &rtd->component_list);
310a0ac4411SKuninori Morimoto 
311a0ac4411SKuninori Morimoto 	return 0;
312a0ac4411SKuninori Morimoto }
313a0ac4411SKuninori Morimoto 
314a0ac4411SKuninori Morimoto struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
315a0ac4411SKuninori Morimoto 						const char *driver_name)
316a0ac4411SKuninori Morimoto {
317a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
318a0ac4411SKuninori Morimoto 
319971da24cSKuninori Morimoto 	if (!driver_name)
320971da24cSKuninori Morimoto 		return NULL;
321971da24cSKuninori Morimoto 
322a33c0d16SKuninori Morimoto 	/*
323a33c0d16SKuninori Morimoto 	 * NOTE
324a33c0d16SKuninori Morimoto 	 *
325a33c0d16SKuninori Morimoto 	 * snd_soc_rtdcom_lookup() will find component from rtd by using
326a33c0d16SKuninori Morimoto 	 * specified driver name.
327a33c0d16SKuninori Morimoto 	 * But, if many components which have same driver name are connected
328a33c0d16SKuninori Morimoto 	 * to 1 rtd, this function will return 1st found component.
329a33c0d16SKuninori Morimoto 	 */
330a0ac4411SKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
331971da24cSKuninori Morimoto 		const char *component_name = rtdcom->component->driver->name;
332971da24cSKuninori Morimoto 
333971da24cSKuninori Morimoto 		if (!component_name)
334971da24cSKuninori Morimoto 			continue;
335971da24cSKuninori Morimoto 
336971da24cSKuninori Morimoto 		if ((component_name == driver_name) ||
337971da24cSKuninori Morimoto 		    strcmp(component_name, driver_name) == 0)
338a0ac4411SKuninori Morimoto 			return rtdcom->component;
339a0ac4411SKuninori Morimoto 	}
340a0ac4411SKuninori Morimoto 
341a0ac4411SKuninori Morimoto 	return NULL;
342a0ac4411SKuninori Morimoto }
343031734b7SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
344a0ac4411SKuninori Morimoto 
34547c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
34647c88fffSLiam Girdwood 		const char *dai_link, int stream)
34747c88fffSLiam Girdwood {
3481a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
34947c88fffSLiam Girdwood 
350bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
3511a497983SMengdong Lin 		if (rtd->dai_link->no_pcm &&
3521a497983SMengdong Lin 			!strcmp(rtd->dai_link->name, dai_link))
3531a497983SMengdong Lin 			return rtd->pcm->streams[stream].substream;
35447c88fffSLiam Girdwood 	}
355f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
35647c88fffSLiam Girdwood 	return NULL;
35747c88fffSLiam Girdwood }
35847c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
35947c88fffSLiam Girdwood 
36075ab9eb6SKuninori Morimoto static const struct snd_soc_ops null_snd_soc_ops;
36175ab9eb6SKuninori Morimoto 
3626e864344SKuninori Morimoto static void soc_release_rtd_dev(struct device *dev)
3636e864344SKuninori Morimoto {
3646e864344SKuninori Morimoto 	/* "dev" means "rtd->dev" */
3656e864344SKuninori Morimoto 	kfree(dev);
3666e864344SKuninori Morimoto }
3676e864344SKuninori Morimoto 
3681c93a9e0SKuninori Morimoto static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
3691c93a9e0SKuninori Morimoto {
3706e864344SKuninori Morimoto 	if (!rtd)
3716e864344SKuninori Morimoto 		return;
3726e864344SKuninori Morimoto 
373753ace0aSKuninori Morimoto 	list_del(&rtd->list);
374b7c5bc45SKuninori Morimoto 
375b7c5bc45SKuninori Morimoto 	/*
376b7c5bc45SKuninori Morimoto 	 * we don't need to call kfree() for rtd->dev
377b7c5bc45SKuninori Morimoto 	 * see
378b7c5bc45SKuninori Morimoto 	 *	soc_release_rtd_dev()
379d918a376SKuninori Morimoto 	 *
380d918a376SKuninori Morimoto 	 * We don't need rtd->dev NULL check, because
381d918a376SKuninori Morimoto 	 * it is alloced *before* rtd.
382d918a376SKuninori Morimoto 	 * see
383d918a376SKuninori Morimoto 	 *	soc_new_pcm_runtime()
384b7c5bc45SKuninori Morimoto 	 */
385b7c5bc45SKuninori Morimoto 	device_unregister(rtd->dev);
3861c93a9e0SKuninori Morimoto }
3871c93a9e0SKuninori Morimoto 
3881a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
3891a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
3901a497983SMengdong Lin {
3911a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
392d918a376SKuninori Morimoto 	struct device *dev;
3936e864344SKuninori Morimoto 	int ret;
3941a497983SMengdong Lin 
3956e864344SKuninori Morimoto 	/*
396d918a376SKuninori Morimoto 	 * for rtd->dev
397d918a376SKuninori Morimoto 	 */
398d918a376SKuninori Morimoto 	dev = kzalloc(sizeof(struct device), GFP_KERNEL);
399d918a376SKuninori Morimoto 	if (!dev)
400d918a376SKuninori Morimoto 		return NULL;
401d918a376SKuninori Morimoto 
402d918a376SKuninori Morimoto 	dev->parent	= card->dev;
403d918a376SKuninori Morimoto 	dev->release	= soc_release_rtd_dev;
404d918a376SKuninori Morimoto 	dev->groups	= soc_dev_attr_groups;
405d918a376SKuninori Morimoto 
406d918a376SKuninori Morimoto 	dev_set_name(dev, "%s", dai_link->name);
407d918a376SKuninori Morimoto 
408d918a376SKuninori Morimoto 	ret = device_register(dev);
409d918a376SKuninori Morimoto 	if (ret < 0) {
410d918a376SKuninori Morimoto 		put_device(dev); /* soc_release_rtd_dev */
411d918a376SKuninori Morimoto 		return NULL;
412d918a376SKuninori Morimoto 	}
413d918a376SKuninori Morimoto 
414d918a376SKuninori Morimoto 	/*
4156e864344SKuninori Morimoto 	 * for rtd
4166e864344SKuninori Morimoto 	 */
4174dc0e7dfSKuninori Morimoto 	rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL);
4181a497983SMengdong Lin 	if (!rtd)
4196e864344SKuninori Morimoto 		goto free_rtd;
4201a497983SMengdong Lin 
421d918a376SKuninori Morimoto 	rtd->dev = dev;
422d918a376SKuninori Morimoto 	dev_set_drvdata(dev, rtd);
423d918a376SKuninori Morimoto 
4246e864344SKuninori Morimoto 	/*
4256e864344SKuninori Morimoto 	 * for rtd->codec_dais
4266e864344SKuninori Morimoto 	 */
4274dc0e7dfSKuninori Morimoto 	rtd->codec_dais = devm_kcalloc(dev, dai_link->num_codecs,
4286396bb22SKees Cook 					sizeof(struct snd_soc_dai *),
4291a497983SMengdong Lin 					GFP_KERNEL);
4306e864344SKuninori Morimoto 	if (!rtd->codec_dais)
4316e864344SKuninori Morimoto 		goto free_rtd;
4326e864344SKuninori Morimoto 
4336e864344SKuninori Morimoto 	/*
4346e864344SKuninori Morimoto 	 * rtd remaining settings
4356e864344SKuninori Morimoto 	 */
436929deb84SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
4376e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
4386e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
4396e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
4406e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
4416e864344SKuninori Morimoto 
442929deb84SKuninori Morimoto 	rtd->card = card;
443929deb84SKuninori Morimoto 	rtd->dai_link = dai_link;
444929deb84SKuninori Morimoto 	if (!rtd->dai_link->ops)
445929deb84SKuninori Morimoto 		rtd->dai_link->ops = &null_snd_soc_ops;
446929deb84SKuninori Morimoto 
4476634e3d6SKuninori Morimoto 	/* see for_each_card_rtds */
4481a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
4491a497983SMengdong Lin 	rtd->num = card->num_rtd;
4501a497983SMengdong Lin 	card->num_rtd++;
451a848125eSKuninori Morimoto 
452a848125eSKuninori Morimoto 	return rtd;
4536e864344SKuninori Morimoto 
4546e864344SKuninori Morimoto free_rtd:
4556e864344SKuninori Morimoto 	soc_free_pcm_runtime(rtd);
4566e864344SKuninori Morimoto 	return NULL;
4571a497983SMengdong Lin }
4581a497983SMengdong Lin 
4591a497983SMengdong Lin static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
4601a497983SMengdong Lin {
4611a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, *_rtd;
4621a497983SMengdong Lin 
463753ace0aSKuninori Morimoto 	for_each_card_rtds_safe(card, rtd, _rtd)
4641a497983SMengdong Lin 		soc_free_pcm_runtime(rtd);
4651a497983SMengdong Lin 
4661a497983SMengdong Lin 	card->num_rtd = 0;
4671a497983SMengdong Lin }
4681a497983SMengdong Lin 
46947c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
47047c88fffSLiam Girdwood 		const char *dai_link)
47147c88fffSLiam Girdwood {
4721a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
47347c88fffSLiam Girdwood 
474bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4751a497983SMengdong Lin 		if (!strcmp(rtd->dai_link->name, dai_link))
4761a497983SMengdong Lin 			return rtd;
47747c88fffSLiam Girdwood 	}
478f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
47947c88fffSLiam Girdwood 	return NULL;
48047c88fffSLiam Girdwood }
48147c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
48247c88fffSLiam Girdwood 
48365462e44SKuninori Morimoto static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
48465462e44SKuninori Morimoto {
48565462e44SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
48665462e44SKuninori Morimoto 
48765462e44SKuninori Morimoto 	for_each_card_rtds(card, rtd)
48865462e44SKuninori Morimoto 		flush_delayed_work(&rtd->delayed_work);
48965462e44SKuninori Morimoto }
49065462e44SKuninori Morimoto 
4916f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
492db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
4936f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
494db2a4165SFrank Mandarino {
4956f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
496d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
4971a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
4981a497983SMengdong Lin 	int i;
499db2a4165SFrank Mandarino 
500c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
501c5599b87SLars-Peter Clausen 	if (!card->instantiated)
502e3509ff0SDaniel Mack 		return 0;
503e3509ff0SDaniel Mack 
5042c7b696aSMarcel Ziswiler 	/*
5052c7b696aSMarcel Ziswiler 	 * Due to the resume being scheduled into a workqueue we could
5066ed25978SAndy Green 	 * suspend before that's finished - wait for it to complete.
5076ed25978SAndy Green 	 */
508f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
5096ed25978SAndy Green 
5106ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
511f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5126ed25978SAndy Green 
513a00f90f9SMark Brown 	/* mute any active DACs */
514bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5150b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
5163efab7dcSMark Brown 
5171a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5183efab7dcSMark Brown 			continue;
5193efab7dcSMark Brown 
5200b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
52188fdffa2SKuninori Morimoto 			if (dai->playback_active)
52288fdffa2SKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 1,
52388fdffa2SKuninori Morimoto 						SNDRV_PCM_STREAM_PLAYBACK);
524db2a4165SFrank Mandarino 		}
52588bd870fSBenoit Cousson 	}
526db2a4165SFrank Mandarino 
5274ccab3e7SLiam Girdwood 	/* suspend all pcms */
528bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5291a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5303efab7dcSMark Brown 			continue;
5313efab7dcSMark Brown 
5321a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
5333efab7dcSMark Brown 	}
5344ccab3e7SLiam Girdwood 
53587506549SMark Brown 	if (card->suspend_pre)
53670b2ac12SMark Brown 		card->suspend_pre(card);
537db2a4165SFrank Mandarino 
538bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5391a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
5403efab7dcSMark Brown 
5411a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5423efab7dcSMark Brown 			continue;
5433efab7dcSMark Brown 
544e0f22622SKuninori Morimoto 		if (!cpu_dai->driver->bus_control)
545e0f22622SKuninori Morimoto 			snd_soc_dai_suspend(cpu_dai);
546db2a4165SFrank Mandarino 	}
547db2a4165SFrank Mandarino 
54837660b6dSLars-Peter Clausen 	/* close any waiting streams */
54965462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
550db2a4165SFrank Mandarino 
551bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5523efab7dcSMark Brown 
5531a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5543efab7dcSMark Brown 			continue;
5553efab7dcSMark Brown 
5561a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
5577bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
558db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
559f0fba2adSLiam Girdwood 
5601a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
5617bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
562db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
563db2a4165SFrank Mandarino 	}
564db2a4165SFrank Mandarino 
5658be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
5668be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
567e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
568e2d32ff6SMark Brown 
5699178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
570f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
5712c7b696aSMarcel Ziswiler 		struct snd_soc_dapm_context *dapm =
5722c7b696aSMarcel Ziswiler 				snd_soc_component_get_dapm(component);
573d9fc4063SKuninori Morimoto 
5742c7b696aSMarcel Ziswiler 		/*
5752c7b696aSMarcel Ziswiler 		 * If there are paths active then the COMPONENT will be held
5762c7b696aSMarcel Ziswiler 		 * with bias _ON and should not be suspended.
5772c7b696aSMarcel Ziswiler 		 */
578e40fadbcSKuninori Morimoto 		if (!snd_soc_component_is_suspended(component)) {
5794890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
5801547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
581125a25daSMark Brown 				/*
5829178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
583125a25daSMark Brown 				 * bias off then being in STANDBY
584125a25daSMark Brown 				 * means it's doing something,
585125a25daSMark Brown 				 * otherwise fall through.
586125a25daSMark Brown 				 */
5874890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
5889178feb4SKuninori Morimoto 					dev_dbg(component->dev,
58910e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
590125a25daSMark Brown 					break;
591125a25daSMark Brown 				}
5921a12d5dcSGustavo A. R. Silva 				/* fall through */
593a8093297SLars-Peter Clausen 
5941547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
59566c51573SKuninori Morimoto 				snd_soc_component_suspend(component);
5969178feb4SKuninori Morimoto 				if (component->regmap)
5979178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
598988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
5999178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
6001547aba9SMark Brown 				break;
6011547aba9SMark Brown 			default:
6029178feb4SKuninori Morimoto 				dev_dbg(component->dev,
6039178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
6041547aba9SMark Brown 				break;
6051547aba9SMark Brown 			}
6061547aba9SMark Brown 		}
607f0fba2adSLiam Girdwood 	}
608db2a4165SFrank Mandarino 
609bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6101a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6113efab7dcSMark Brown 
6121a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6133efab7dcSMark Brown 			continue;
6143efab7dcSMark Brown 
615e0f22622SKuninori Morimoto 		if (cpu_dai->driver->bus_control)
616e0f22622SKuninori Morimoto 			snd_soc_dai_suspend(cpu_dai);
617988e8cc4SNicolin Chen 
618988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
619988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
620db2a4165SFrank Mandarino 	}
621db2a4165SFrank Mandarino 
62287506549SMark Brown 	if (card->suspend_post)
62370b2ac12SMark Brown 		card->suspend_post(card);
624db2a4165SFrank Mandarino 
625db2a4165SFrank Mandarino 	return 0;
626db2a4165SFrank Mandarino }
6276f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
628db2a4165SFrank Mandarino 
6292c7b696aSMarcel Ziswiler /*
6302c7b696aSMarcel Ziswiler  * deferred resume work, so resume can complete before we finished
6316ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6326ed25978SAndy Green  */
6336ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
634db2a4165SFrank Mandarino {
635f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
6362c7b696aSMarcel Ziswiler 			container_of(work, struct snd_soc_card,
6372c7b696aSMarcel Ziswiler 				     deferred_resume_work);
6381a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
639d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
6401a497983SMengdong Lin 	int i;
641db2a4165SFrank Mandarino 
6422c7b696aSMarcel Ziswiler 	/*
6432c7b696aSMarcel Ziswiler 	 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6446ed25978SAndy Green 	 * so userspace apps are blocked from touching us
6456ed25978SAndy Green 	 */
6466ed25978SAndy Green 
647f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
6486ed25978SAndy Green 
6499949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
650f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
6519949788bSMark Brown 
65287506549SMark Brown 	if (card->resume_pre)
65370b2ac12SMark Brown 		card->resume_pre(card);
654db2a4165SFrank Mandarino 
655bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
656bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6571a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6583efab7dcSMark Brown 
6591a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6603efab7dcSMark Brown 			continue;
6613efab7dcSMark Brown 
66224b09d05SKuninori Morimoto 		if (cpu_dai->driver->bus_control)
66324b09d05SKuninori Morimoto 			snd_soc_dai_resume(cpu_dai);
664db2a4165SFrank Mandarino 	}
665db2a4165SFrank Mandarino 
666f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
667e40fadbcSKuninori Morimoto 		if (snd_soc_component_is_suspended(component))
6689a840cbaSKuninori Morimoto 			snd_soc_component_resume(component);
6691547aba9SMark Brown 	}
670db2a4165SFrank Mandarino 
671bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6723efab7dcSMark Brown 
6731a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6743efab7dcSMark Brown 			continue;
6753efab7dcSMark Brown 
6761a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
677d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
678db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
679f0fba2adSLiam Girdwood 
6801a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
681d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
682db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
683db2a4165SFrank Mandarino 	}
684db2a4165SFrank Mandarino 
6853ff3f64bSMark Brown 	/* unmute any active DACs */
686bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6870b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
6883efab7dcSMark Brown 
6891a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6903efab7dcSMark Brown 			continue;
6913efab7dcSMark Brown 
6920b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
69388fdffa2SKuninori Morimoto 			if (dai->playback_active)
69488fdffa2SKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 0,
69588fdffa2SKuninori Morimoto 						SNDRV_PCM_STREAM_PLAYBACK);
696db2a4165SFrank Mandarino 		}
69788bd870fSBenoit Cousson 	}
698db2a4165SFrank Mandarino 
699bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7001a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7013efab7dcSMark Brown 
7021a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7033efab7dcSMark Brown 			continue;
7043efab7dcSMark Brown 
70524b09d05SKuninori Morimoto 		if (!cpu_dai->driver->bus_control)
70624b09d05SKuninori Morimoto 			snd_soc_dai_resume(cpu_dai);
707db2a4165SFrank Mandarino 	}
708db2a4165SFrank Mandarino 
70987506549SMark Brown 	if (card->resume_post)
71070b2ac12SMark Brown 		card->resume_post(card);
711db2a4165SFrank Mandarino 
712f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
7136ed25978SAndy Green 
7148be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
7158be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
716e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
7171a7aaa58SJeeja KP 
7181a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
7191a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
7206ed25978SAndy Green }
7216ed25978SAndy Green 
7226ed25978SAndy Green /* powers up audio subsystem after a suspend */
7236f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
7246ed25978SAndy Green {
7256f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
726bc263214SLars-Peter Clausen 	bool bus_control = false;
7271a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
72822d251a5SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
72922d251a5SKuninori Morimoto 	int i;
730b9dd94a8SPeter Ujfalusi 
731c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
732c5599b87SLars-Peter Clausen 	if (!card->instantiated)
7335ff1ddf2SEric Miao 		return 0;
7345ff1ddf2SEric Miao 
735988e8cc4SNicolin Chen 	/* activate pins from sleep state */
736bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
73788bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
73888bd870fSBenoit Cousson 
739988e8cc4SNicolin Chen 		if (cpu_dai->active)
740988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
74188bd870fSBenoit Cousson 
74222d251a5SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
743988e8cc4SNicolin Chen 			if (codec_dai->active)
744988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
745988e8cc4SNicolin Chen 		}
74688bd870fSBenoit Cousson 	}
747988e8cc4SNicolin Chen 
748bc263214SLars-Peter Clausen 	/*
749bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
750bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
751bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
75264ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
75364ab9baaSMark Brown 	 */
754bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7551a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7562c7b696aSMarcel Ziswiler 
757bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
75882e14e8bSStephen Warren 	}
759bc263214SLars-Peter Clausen 	if (bus_control) {
760bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
76164ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
76264ab9baaSMark Brown 	} else {
763f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
7646308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
765f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
766f0fba2adSLiam Girdwood 	}
7676ed25978SAndy Green 
768db2a4165SFrank Mandarino 	return 0;
769db2a4165SFrank Mandarino }
7706f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
771b3da4251SKuninori Morimoto 
772b3da4251SKuninori Morimoto static void soc_resume_init(struct snd_soc_card *card)
773b3da4251SKuninori Morimoto {
774b3da4251SKuninori Morimoto 	/* deferred resume work */
775b3da4251SKuninori Morimoto 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
776b3da4251SKuninori Morimoto }
777db2a4165SFrank Mandarino #else
7786f8ab4acSMark Brown #define snd_soc_suspend NULL
7796f8ab4acSMark Brown #define snd_soc_resume NULL
780b3da4251SKuninori Morimoto static inline void soc_resume_init(struct snd_soc_card *card)
781b3da4251SKuninori Morimoto {
782b3da4251SKuninori Morimoto }
783db2a4165SFrank Mandarino #endif
784db2a4165SFrank Mandarino 
78585e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
78602a06d30SBarry Song };
78702a06d30SBarry Song 
788c0834440SKuninori Morimoto static struct device_node
789c0834440SKuninori Morimoto *soc_component_to_node(struct snd_soc_component *component)
79012023a9aSMisael Lopez Cruz {
791c0834440SKuninori Morimoto 	struct device_node *of_node;
79212023a9aSMisael Lopez Cruz 
793c0834440SKuninori Morimoto 	of_node = component->dev->of_node;
794c0834440SKuninori Morimoto 	if (!of_node && component->dev->parent)
795c0834440SKuninori Morimoto 		of_node = component->dev->parent->of_node;
79634e81ab4SLars-Peter Clausen 
797c0834440SKuninori Morimoto 	return of_node;
79812023a9aSMisael Lopez Cruz }
79912023a9aSMisael Lopez Cruz 
800be6ac0a9SKuninori Morimoto static int snd_soc_is_matching_component(
801be6ac0a9SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc,
802be6ac0a9SKuninori Morimoto 	struct snd_soc_component *component)
803be6ac0a9SKuninori Morimoto {
804be6ac0a9SKuninori Morimoto 	struct device_node *component_of_node;
805be6ac0a9SKuninori Morimoto 
8067d7db5d3SKuninori Morimoto 	if (!dlc)
8077d7db5d3SKuninori Morimoto 		return 0;
8087d7db5d3SKuninori Morimoto 
8097d7db5d3SKuninori Morimoto 	component_of_node = soc_component_to_node(component);
810be6ac0a9SKuninori Morimoto 
811be6ac0a9SKuninori Morimoto 	if (dlc->of_node && component_of_node != dlc->of_node)
812be6ac0a9SKuninori Morimoto 		return 0;
813be6ac0a9SKuninori Morimoto 	if (dlc->name && strcmp(component->name, dlc->name))
814be6ac0a9SKuninori Morimoto 		return 0;
815be6ac0a9SKuninori Morimoto 
816be6ac0a9SKuninori Morimoto 	return 1;
817be6ac0a9SKuninori Morimoto }
818be6ac0a9SKuninori Morimoto 
819db2a4165SFrank Mandarino static struct snd_soc_component *soc_find_component(
820c1e230f0SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc)
821db2a4165SFrank Mandarino {
822db2a4165SFrank Mandarino 	struct snd_soc_component *component;
823db2a4165SFrank Mandarino 
824db2a4165SFrank Mandarino 	lockdep_assert_held(&client_mutex);
825db2a4165SFrank Mandarino 
826e3303268SKuninori Morimoto 	/*
827e3303268SKuninori Morimoto 	 * NOTE
828e3303268SKuninori Morimoto 	 *
829e3303268SKuninori Morimoto 	 * It returns *1st* found component, but some driver
830e3303268SKuninori Morimoto 	 * has few components by same of_node/name
831e3303268SKuninori Morimoto 	 * ex)
832e3303268SKuninori Morimoto 	 *	CPU component and generic DMAEngine component
833e3303268SKuninori Morimoto 	 */
834c1e230f0SKuninori Morimoto 	for_each_component(component)
835c1e230f0SKuninori Morimoto 		if (snd_soc_is_matching_component(dlc, component))
836db2a4165SFrank Mandarino 			return component;
837db2a4165SFrank Mandarino 
838db2a4165SFrank Mandarino 	return NULL;
83912023a9aSMisael Lopez Cruz }
84012023a9aSMisael Lopez Cruz 
841fbb88b5cSMengdong Lin /**
842fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
843fbb88b5cSMengdong Lin  *
8444958471bSJeffy Chen  * @dlc: name of the DAI or the DAI driver and optional component info to match
845fbb88b5cSMengdong Lin  *
846ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
847fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
848fbb88b5cSMengdong Lin  * should also match if being specified.
849fbb88b5cSMengdong Lin  *
850fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
851fbb88b5cSMengdong Lin  */
852305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
85314621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
85412023a9aSMisael Lopez Cruz {
85514621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
85614621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
85712023a9aSMisael Lopez Cruz 
85834e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
85934e81ab4SLars-Peter Clausen 
86014621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs */
861368dee94SKuninori Morimoto 	for_each_component(component) {
862be6ac0a9SKuninori Morimoto 		if (!snd_soc_is_matching_component(dlc, component))
86312023a9aSMisael Lopez Cruz 			continue;
86415a0c645SKuninori Morimoto 		for_each_component_dais(component, dai) {
8654958471bSJeffy Chen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
8666a6dafdaSJeffy Chen 			    && (!dai->driver->name
8676a6dafdaSJeffy Chen 				|| strcmp(dai->driver->name, dlc->dai_name)))
86814621c7eSLars-Peter Clausen 				continue;
86912023a9aSMisael Lopez Cruz 
87014621c7eSLars-Peter Clausen 			return dai;
87112023a9aSMisael Lopez Cruz 		}
87212023a9aSMisael Lopez Cruz 	}
87312023a9aSMisael Lopez Cruz 
87412023a9aSMisael Lopez Cruz 	return NULL;
87512023a9aSMisael Lopez Cruz }
876305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
87712023a9aSMisael Lopez Cruz 
87817fb1755SMengdong Lin /**
87917fb1755SMengdong Lin  * snd_soc_find_dai_link - Find a DAI link
88017fb1755SMengdong Lin  *
88117fb1755SMengdong Lin  * @card: soc card
88217fb1755SMengdong Lin  * @id: DAI link ID to match
88317fb1755SMengdong Lin  * @name: DAI link name to match, optional
8848abab35fSCharles Keepax  * @stream_name: DAI link stream name to match, optional
88517fb1755SMengdong Lin  *
88617fb1755SMengdong Lin  * This function will search all existing DAI links of the soc card to
88717fb1755SMengdong Lin  * find the link of the same ID. Since DAI links may not have their
88817fb1755SMengdong Lin  * unique ID, so name and stream name should also match if being
88917fb1755SMengdong Lin  * specified.
89017fb1755SMengdong Lin  *
89117fb1755SMengdong Lin  * Return: pointer of DAI link, or NULL if not found.
89217fb1755SMengdong Lin  */
89317fb1755SMengdong Lin struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
89417fb1755SMengdong Lin 					       int id, const char *name,
89517fb1755SMengdong Lin 					       const char *stream_name)
89617fb1755SMengdong Lin {
89742849064SKuninori Morimoto 	struct snd_soc_dai_link *link;
89817fb1755SMengdong Lin 
89917fb1755SMengdong Lin 	lockdep_assert_held(&client_mutex);
90017fb1755SMengdong Lin 
90142849064SKuninori Morimoto 	for_each_card_links(card, link) {
90217fb1755SMengdong Lin 		if (link->id != id)
90317fb1755SMengdong Lin 			continue;
90417fb1755SMengdong Lin 
90517fb1755SMengdong Lin 		if (name && (!link->name || strcmp(name, link->name)))
90617fb1755SMengdong Lin 			continue;
90717fb1755SMengdong Lin 
90817fb1755SMengdong Lin 		if (stream_name && (!link->stream_name
90917fb1755SMengdong Lin 			|| strcmp(stream_name, link->stream_name)))
91017fb1755SMengdong Lin 			continue;
91117fb1755SMengdong Lin 
91217fb1755SMengdong Lin 		return link;
91317fb1755SMengdong Lin 	}
91417fb1755SMengdong Lin 
91517fb1755SMengdong Lin 	return NULL;
91617fb1755SMengdong Lin }
91717fb1755SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
91817fb1755SMengdong Lin 
91949a5ba1cSMengdong Lin static bool soc_is_dai_link_bound(struct snd_soc_card *card,
92049a5ba1cSMengdong Lin 		struct snd_soc_dai_link *dai_link)
921db2a4165SFrank Mandarino {
92249a5ba1cSMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
92349a5ba1cSMengdong Lin 
924bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
92549a5ba1cSMengdong Lin 		if (rtd->dai_link == dai_link)
92649a5ba1cSMengdong Lin 			return true;
92749a5ba1cSMengdong Lin 	}
92849a5ba1cSMengdong Lin 
92949a5ba1cSMengdong Lin 	return false;
93049a5ba1cSMengdong Lin }
93149a5ba1cSMengdong Lin 
9326f2f1ff0SMengdong Lin static int soc_bind_dai_link(struct snd_soc_card *card,
9336f2f1ff0SMengdong Lin 	struct snd_soc_dai_link *dai_link)
934db2a4165SFrank Mandarino {
9351a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
93634614739SJerome Brunet 	struct snd_soc_dai_link_component *codec, *platform;
93790be711eSKuninori Morimoto 	struct snd_soc_component *component;
93888bd870fSBenoit Cousson 	int i;
939db2a4165SFrank Mandarino 
940a655de80SLiam Girdwood 	if (dai_link->ignore)
941a655de80SLiam Girdwood 		return 0;
942a655de80SLiam Girdwood 
9436f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
9446308419aSMark Brown 
94549a5ba1cSMengdong Lin 	if (soc_is_dai_link_bound(card, dai_link)) {
94649a5ba1cSMengdong Lin 		dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
94749a5ba1cSMengdong Lin 			dai_link->name);
94849a5ba1cSMengdong Lin 		return 0;
94949a5ba1cSMengdong Lin 	}
950db2a4165SFrank Mandarino 
951513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
952513cb311SSudip Mukherjee 	if (!rtd)
953513cb311SSudip Mukherjee 		return -ENOMEM;
954513cb311SSudip Mukherjee 
95508a5841eSKuninori Morimoto 	/* FIXME: we need multi CPU support in the future */
95608a5841eSKuninori Morimoto 	rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
957b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
9586b490879SMartin Hundebøll 		dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
95908a5841eSKuninori Morimoto 			 dai_link->cpus->dai_name);
9601a497983SMengdong Lin 		goto _err_defer;
961c5af3a2eSMark Brown 	}
96290be711eSKuninori Morimoto 	snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
963c5af3a2eSMark Brown 
96488bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
965e2b30edfSKuninori Morimoto 	rtd->num_codecs = dai_link->num_codecs;
96634614739SJerome Brunet 	for_each_link_codecs(dai_link, i, codec) {
96734614739SJerome Brunet 		rtd->codec_dais[i] = snd_soc_find_dai(codec);
9680a2cfcd9SKuninori Morimoto 		if (!rtd->codec_dais[i]) {
9697c7e2d6aSStefan Agner 			dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
97034614739SJerome Brunet 				 codec->dai_name);
9711a497983SMengdong Lin 			goto _err_defer;
97212023a9aSMisael Lopez Cruz 		}
97334614739SJerome Brunet 
9740a2cfcd9SKuninori Morimoto 		snd_soc_rtdcom_add(rtd, rtd->codec_dais[i]->component);
97588bd870fSBenoit Cousson 	}
97688bd870fSBenoit Cousson 
97788bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
9780a2cfcd9SKuninori Morimoto 	rtd->codec_dai = rtd->codec_dais[0];
97912023a9aSMisael Lopez Cruz 
980e2b30edfSKuninori Morimoto 	/* Find PLATFORM from registered PLATFORMs */
98134614739SJerome Brunet 	for_each_link_platforms(dai_link, i, platform) {
982368dee94SKuninori Morimoto 		for_each_component(component) {
98334614739SJerome Brunet 			if (!snd_soc_is_matching_component(platform, component))
98490be711eSKuninori Morimoto 				continue;
98590be711eSKuninori Morimoto 
98690be711eSKuninori Morimoto 			snd_soc_rtdcom_add(rtd, component);
98790be711eSKuninori Morimoto 		}
98834614739SJerome Brunet 	}
98990be711eSKuninori Morimoto 
990b19e6e7bSMark Brown 	return 0;
9911a497983SMengdong Lin 
9921a497983SMengdong Lin _err_defer:
9931a497983SMengdong Lin 	soc_free_pcm_runtime(rtd);
9941a497983SMengdong Lin 	return -EPROBE_DEFER;
9956b05eda6SMark Brown }
9966b05eda6SMark Brown 
997ffd60fbaSKuninori Morimoto static void soc_set_of_name_prefix(struct snd_soc_component *component)
998ffd60fbaSKuninori Morimoto {
999ffd60fbaSKuninori Morimoto 	struct device_node *of_node = soc_component_to_node(component);
1000ffd60fbaSKuninori Morimoto 	const char *str;
1001ffd60fbaSKuninori Morimoto 	int ret;
1002ffd60fbaSKuninori Morimoto 
1003ffd60fbaSKuninori Morimoto 	ret = of_property_read_string(of_node, "sound-name-prefix", &str);
1004ffd60fbaSKuninori Morimoto 	if (!ret)
1005ffd60fbaSKuninori Morimoto 		component->name_prefix = str;
1006ffd60fbaSKuninori Morimoto }
1007ffd60fbaSKuninori Morimoto 
1008ffd60fbaSKuninori Morimoto static void soc_set_name_prefix(struct snd_soc_card *card,
1009ffd60fbaSKuninori Morimoto 				struct snd_soc_component *component)
1010ffd60fbaSKuninori Morimoto {
1011ffd60fbaSKuninori Morimoto 	int i;
1012ffd60fbaSKuninori Morimoto 
1013ffd60fbaSKuninori Morimoto 	for (i = 0; i < card->num_configs && card->codec_conf; i++) {
1014ffd60fbaSKuninori Morimoto 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1015ffd60fbaSKuninori Morimoto 		struct device_node *of_node = soc_component_to_node(component);
1016ffd60fbaSKuninori Morimoto 
1017ffd60fbaSKuninori Morimoto 		if (map->of_node && of_node != map->of_node)
1018ffd60fbaSKuninori Morimoto 			continue;
1019ffd60fbaSKuninori Morimoto 		if (map->dev_name && strcmp(component->name, map->dev_name))
1020ffd60fbaSKuninori Morimoto 			continue;
1021ffd60fbaSKuninori Morimoto 		component->name_prefix = map->name_prefix;
1022ffd60fbaSKuninori Morimoto 		return;
1023ffd60fbaSKuninori Morimoto 	}
1024ffd60fbaSKuninori Morimoto 
1025ffd60fbaSKuninori Morimoto 	/*
1026ffd60fbaSKuninori Morimoto 	 * If there is no configuration table or no match in the table,
1027ffd60fbaSKuninori Morimoto 	 * check if a prefix is provided in the node
1028ffd60fbaSKuninori Morimoto 	 */
1029ffd60fbaSKuninori Morimoto 	soc_set_of_name_prefix(component);
1030ffd60fbaSKuninori Morimoto }
1031ffd60fbaSKuninori Morimoto 
103222d14231SKuninori Morimoto static void soc_cleanup_component(struct snd_soc_component *component)
103322d14231SKuninori Morimoto {
103404f770d9SKuninori Morimoto 	/* For framework level robustness */
10353bb936f5SAmadeusz Sławiński 	snd_soc_component_set_jack(component, NULL, NULL);
103604f770d9SKuninori Morimoto 
10377a5d9815SBard liao 	list_del_init(&component->card_list);
103822d14231SKuninori Morimoto 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
103922d14231SKuninori Morimoto 	soc_cleanup_component_debugfs(component);
104022d14231SKuninori Morimoto 	component->card = NULL;
10410e36f36bSPierre-Louis Bossart 	snd_soc_component_module_put_when_remove(component);
104222d14231SKuninori Morimoto }
104322d14231SKuninori Morimoto 
1044f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
1045d12cd198SStephen Warren {
1046abd31b32SLars-Peter Clausen 	if (!component->card)
104770090bbbSLars-Peter Clausen 		return;
1048d12cd198SStephen Warren 
104903b34dd7SKuninori Morimoto 	snd_soc_component_remove(component);
10507a5d9815SBard liao 
105122d14231SKuninori Morimoto 	soc_cleanup_component(component);
1052d12cd198SStephen Warren }
1053d12cd198SStephen Warren 
1054ffd60fbaSKuninori Morimoto static int soc_probe_component(struct snd_soc_card *card,
1055ffd60fbaSKuninori Morimoto 			       struct snd_soc_component *component)
1056ffd60fbaSKuninori Morimoto {
1057ffd60fbaSKuninori Morimoto 	struct snd_soc_dapm_context *dapm =
1058ffd60fbaSKuninori Morimoto 		snd_soc_component_get_dapm(component);
1059ffd60fbaSKuninori Morimoto 	struct snd_soc_dai *dai;
1060ffd60fbaSKuninori Morimoto 	int ret;
1061ffd60fbaSKuninori Morimoto 
1062ffd60fbaSKuninori Morimoto 	if (!strcmp(component->name, "snd-soc-dummy"))
1063ffd60fbaSKuninori Morimoto 		return 0;
1064ffd60fbaSKuninori Morimoto 
1065ffd60fbaSKuninori Morimoto 	if (component->card) {
1066ffd60fbaSKuninori Morimoto 		if (component->card != card) {
1067ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1068ffd60fbaSKuninori Morimoto 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1069ffd60fbaSKuninori Morimoto 				card->name, component->card->name);
1070ffd60fbaSKuninori Morimoto 			return -ENODEV;
1071ffd60fbaSKuninori Morimoto 		}
1072ffd60fbaSKuninori Morimoto 		return 0;
1073ffd60fbaSKuninori Morimoto 	}
1074ffd60fbaSKuninori Morimoto 
1075ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_module_get_when_probe(component);
1076ffd60fbaSKuninori Morimoto 	if (ret < 0)
1077ffd60fbaSKuninori Morimoto 		return ret;
1078ffd60fbaSKuninori Morimoto 
1079ffd60fbaSKuninori Morimoto 	component->card = card;
1080ffd60fbaSKuninori Morimoto 	soc_set_name_prefix(card, component);
1081ffd60fbaSKuninori Morimoto 
1082ffd60fbaSKuninori Morimoto 	soc_init_component_debugfs(component);
1083ffd60fbaSKuninori Morimoto 
108495c267ddSKuninori Morimoto 	snd_soc_dapm_init(dapm, card, component);
1085b614beafSKuninori Morimoto 
1086ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_new_controls(dapm,
1087ffd60fbaSKuninori Morimoto 					component->driver->dapm_widgets,
1088ffd60fbaSKuninori Morimoto 					component->driver->num_dapm_widgets);
1089ffd60fbaSKuninori Morimoto 
1090ffd60fbaSKuninori Morimoto 	if (ret != 0) {
1091ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1092ffd60fbaSKuninori Morimoto 			"Failed to create new controls %d\n", ret);
1093ffd60fbaSKuninori Morimoto 		goto err_probe;
1094ffd60fbaSKuninori Morimoto 	}
1095ffd60fbaSKuninori Morimoto 
1096ffd60fbaSKuninori Morimoto 	for_each_component_dais(component, dai) {
1097ffd60fbaSKuninori Morimoto 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1098ffd60fbaSKuninori Morimoto 		if (ret != 0) {
1099ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1100ffd60fbaSKuninori Morimoto 				"Failed to create DAI widgets %d\n", ret);
1101ffd60fbaSKuninori Morimoto 			goto err_probe;
1102ffd60fbaSKuninori Morimoto 		}
1103ffd60fbaSKuninori Morimoto 	}
1104ffd60fbaSKuninori Morimoto 
1105ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_probe(component);
1106ffd60fbaSKuninori Morimoto 	if (ret < 0) {
1107ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1108ffd60fbaSKuninori Morimoto 			"ASoC: failed to probe component %d\n", ret);
1109ffd60fbaSKuninori Morimoto 		goto err_probe;
1110ffd60fbaSKuninori Morimoto 	}
1111ffd60fbaSKuninori Morimoto 	WARN(dapm->idle_bias_off &&
1112ffd60fbaSKuninori Morimoto 	     dapm->bias_level != SND_SOC_BIAS_OFF,
1113ffd60fbaSKuninori Morimoto 	     "codec %s can not start from non-off bias with idle_bias_off==1\n",
1114ffd60fbaSKuninori Morimoto 	     component->name);
1115ffd60fbaSKuninori Morimoto 
1116ffd60fbaSKuninori Morimoto 	/* machine specific init */
1117ffd60fbaSKuninori Morimoto 	if (component->init) {
1118ffd60fbaSKuninori Morimoto 		ret = component->init(component);
1119ffd60fbaSKuninori Morimoto 		if (ret < 0) {
1120ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1121ffd60fbaSKuninori Morimoto 				"Failed to do machine specific init %d\n", ret);
1122ffd60fbaSKuninori Morimoto 			goto err_probe;
1123ffd60fbaSKuninori Morimoto 		}
1124ffd60fbaSKuninori Morimoto 	}
1125ffd60fbaSKuninori Morimoto 
1126ffd60fbaSKuninori Morimoto 	ret = snd_soc_add_component_controls(component,
1127ffd60fbaSKuninori Morimoto 					     component->driver->controls,
1128ffd60fbaSKuninori Morimoto 					     component->driver->num_controls);
1129ffd60fbaSKuninori Morimoto 	if (ret < 0)
1130ffd60fbaSKuninori Morimoto 		goto err_probe;
1131ffd60fbaSKuninori Morimoto 
1132ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(dapm,
1133ffd60fbaSKuninori Morimoto 				      component->driver->dapm_routes,
1134ffd60fbaSKuninori Morimoto 				      component->driver->num_dapm_routes);
1135ffd60fbaSKuninori Morimoto 	if (ret < 0)
1136ffd60fbaSKuninori Morimoto 		goto err_probe;
1137ffd60fbaSKuninori Morimoto 
1138ffd60fbaSKuninori Morimoto 	/* see for_each_card_components */
1139ffd60fbaSKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1140ffd60fbaSKuninori Morimoto 
1141ffd60fbaSKuninori Morimoto err_probe:
1142ffd60fbaSKuninori Morimoto 	if (ret < 0)
1143ffd60fbaSKuninori Morimoto 		soc_cleanup_component(component);
1144ffd60fbaSKuninori Morimoto 
1145ffd60fbaSKuninori Morimoto 	return ret;
1146ffd60fbaSKuninori Morimoto }
1147ffd60fbaSKuninori Morimoto 
1148e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1149589c3563SJarkko Nikula {
1150589c3563SJarkko Nikula 	int err;
1151589c3563SJarkko Nikula 
115252abe6ccSGuennadi Liakhovetski 	if (!dai || !dai->probed || !dai->driver ||
11532eda3cb1SKuninori Morimoto 	    dai->driver->remove_order != order)
11542eda3cb1SKuninori Morimoto 		return;
11552eda3cb1SKuninori Morimoto 
1156dcdab582SKuninori Morimoto 	err = snd_soc_dai_remove(dai);
1157589c3563SJarkko Nikula 	if (err < 0)
1158e60cd14fSLars-Peter Clausen 		dev_err(dai->dev,
1159b0aa88afSMisael Lopez Cruz 			"ASoC: failed to remove %s: %d\n",
1160e60cd14fSLars-Peter Clausen 			dai->name, err);
1161dcdab582SKuninori Morimoto 
1162e60cd14fSLars-Peter Clausen 	dai->probed = 0;
1163b0aa88afSMisael Lopez Cruz }
1164b0aa88afSMisael Lopez Cruz 
1165a7d44f78SKuninori Morimoto static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1166a7d44f78SKuninori Morimoto {
1167a7d44f78SKuninori Morimoto 	int ret;
1168a7d44f78SKuninori Morimoto 
1169a7d44f78SKuninori Morimoto 	if (dai->probed ||
1170a7d44f78SKuninori Morimoto 	    dai->driver->probe_order != order)
1171a7d44f78SKuninori Morimoto 		return 0;
1172a7d44f78SKuninori Morimoto 
1173a7d44f78SKuninori Morimoto 	ret = snd_soc_dai_probe(dai);
1174a7d44f78SKuninori Morimoto 	if (ret < 0) {
1175a7d44f78SKuninori Morimoto 		dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1176a7d44f78SKuninori Morimoto 			dai->name, ret);
1177a7d44f78SKuninori Morimoto 		return ret;
1178a7d44f78SKuninori Morimoto 	}
1179a7d44f78SKuninori Morimoto 
1180a7d44f78SKuninori Morimoto 	dai->probed = 1;
1181a7d44f78SKuninori Morimoto 
1182a7d44f78SKuninori Morimoto 	return 0;
1183a7d44f78SKuninori Morimoto }
1184a7d44f78SKuninori Morimoto 
11854ca47d21SKuninori Morimoto static void soc_remove_link_dais(struct snd_soc_card *card)
1186f0fba2adSLiam Girdwood {
1187e60cd14fSLars-Peter Clausen 	int i;
11880b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
11894ca47d21SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
11904ca47d21SKuninori Morimoto 	int order;
11914ca47d21SKuninori Morimoto 
11924ca47d21SKuninori Morimoto 	for_each_comp_order(order) {
11934ca47d21SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1194f0fba2adSLiam Girdwood 			/* remove the CODEC DAI */
11950b7990e3SKuninori Morimoto 			for_each_rtd_codec_dai(rtd, i, codec_dai)
11960b7990e3SKuninori Morimoto 				soc_remove_dai(codec_dai, order);
1197f0fba2adSLiam Girdwood 
1198e60cd14fSLars-Peter Clausen 			soc_remove_dai(rtd->cpu_dai, order);
1199f0fba2adSLiam Girdwood 		}
12004ca47d21SKuninori Morimoto 	}
12014ca47d21SKuninori Morimoto }
1202f0fba2adSLiam Girdwood 
1203bc7c16c2SKuninori Morimoto static int soc_probe_link_dais(struct snd_soc_card *card)
1204bc7c16c2SKuninori Morimoto {
1205bc7c16c2SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1206bc7c16c2SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
1207bc7c16c2SKuninori Morimoto 	int i, order, ret;
1208bc7c16c2SKuninori Morimoto 
1209bc7c16c2SKuninori Morimoto 	for_each_comp_order(order) {
1210bc7c16c2SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1211bc7c16c2SKuninori Morimoto 
1212bc7c16c2SKuninori Morimoto 			dev_dbg(card->dev,
1213bc7c16c2SKuninori Morimoto 				"ASoC: probe %s dai link %d late %d\n",
1214bc7c16c2SKuninori Morimoto 				card->name, rtd->num, order);
1215bc7c16c2SKuninori Morimoto 
1216bc7c16c2SKuninori Morimoto 			ret = soc_probe_dai(rtd->cpu_dai, order);
1217bc7c16c2SKuninori Morimoto 			if (ret)
1218bc7c16c2SKuninori Morimoto 				return ret;
1219bc7c16c2SKuninori Morimoto 
1220bc7c16c2SKuninori Morimoto 			/* probe the CODEC DAI */
1221bc7c16c2SKuninori Morimoto 			for_each_rtd_codec_dai(rtd, i, codec_dai) {
1222bc7c16c2SKuninori Morimoto 				ret = soc_probe_dai(codec_dai, order);
1223bc7c16c2SKuninori Morimoto 				if (ret)
1224bc7c16c2SKuninori Morimoto 					return ret;
1225bc7c16c2SKuninori Morimoto 			}
1226bc7c16c2SKuninori Morimoto 		}
1227bc7c16c2SKuninori Morimoto 	}
1228bc7c16c2SKuninori Morimoto 
1229bc7c16c2SKuninori Morimoto 	return 0;
1230bc7c16c2SKuninori Morimoto }
1231bc7c16c2SKuninori Morimoto 
1232b006c0c6SKuninori Morimoto static void soc_remove_link_components(struct snd_soc_card *card)
123362ae68faSStephen Warren {
123461aca564SLars-Peter Clausen 	struct snd_soc_component *component;
1235b006c0c6SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
123690be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
1237b006c0c6SKuninori Morimoto 	int order;
123862ae68faSStephen Warren 
1239b006c0c6SKuninori Morimoto 	for_each_comp_order(order) {
1240b006c0c6SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
124190be711eSKuninori Morimoto 			for_each_rtdcom(rtd, rtdcom) {
124290be711eSKuninori Morimoto 				component = rtdcom->component;
124362ae68faSStephen Warren 
1244b006c0c6SKuninori Morimoto 				if (component->driver->remove_order != order)
1245b006c0c6SKuninori Morimoto 					continue;
1246b006c0c6SKuninori Morimoto 
124761aca564SLars-Peter Clausen 				soc_remove_component(component);
124862ae68faSStephen Warren 			}
124962ae68faSStephen Warren 		}
1250b006c0c6SKuninori Morimoto 	}
1251b006c0c6SKuninori Morimoto }
125262ae68faSStephen Warren 
125362f07a6bSKuninori Morimoto static int soc_probe_link_components(struct snd_soc_card *card)
12546fb03550SKuninori Morimoto {
12556fb03550SKuninori Morimoto 	struct snd_soc_component *component;
125662f07a6bSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
12576fb03550SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
125862f07a6bSKuninori Morimoto 	int ret, order;
12596fb03550SKuninori Morimoto 
126062f07a6bSKuninori Morimoto 	for_each_comp_order(order) {
126162f07a6bSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
12626fb03550SKuninori Morimoto 			for_each_rtdcom(rtd, rtdcom) {
12636fb03550SKuninori Morimoto 				component = rtdcom->component;
12646fb03550SKuninori Morimoto 
126562f07a6bSKuninori Morimoto 				if (component->driver->probe_order != order)
126662f07a6bSKuninori Morimoto 					continue;
126762f07a6bSKuninori Morimoto 
12686fb03550SKuninori Morimoto 				ret = soc_probe_component(card, component);
12696fb03550SKuninori Morimoto 				if (ret < 0)
12706fb03550SKuninori Morimoto 					return ret;
12716fb03550SKuninori Morimoto 			}
12726fb03550SKuninori Morimoto 		}
127362f07a6bSKuninori Morimoto 	}
12746fb03550SKuninori Morimoto 
12756fb03550SKuninori Morimoto 	return 0;
12766fb03550SKuninori Morimoto }
12776fb03550SKuninori Morimoto 
1278923c5e61SMengdong Lin static int soc_init_dai_link(struct snd_soc_card *card,
1279923c5e61SMengdong Lin 			     struct snd_soc_dai_link *link)
1280923c5e61SMengdong Lin {
1281adb76b5bSKuninori Morimoto 	int i;
128234614739SJerome Brunet 	struct snd_soc_dai_link_component *codec, *platform;
1283923c5e61SMengdong Lin 
12843db769f1SKuninori Morimoto 	for_each_link_codecs(link, i, codec) {
1285923c5e61SMengdong Lin 		/*
1286923c5e61SMengdong Lin 		 * Codec must be specified by 1 of name or OF node,
1287923c5e61SMengdong Lin 		 * not both or neither.
1288923c5e61SMengdong Lin 		 */
128934614739SJerome Brunet 		if (!!codec->name == !!codec->of_node) {
1290923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1291923c5e61SMengdong Lin 				link->name);
1292923c5e61SMengdong Lin 			return -EINVAL;
1293923c5e61SMengdong Lin 		}
1294af18b13fSJerome Brunet 
1295923c5e61SMengdong Lin 		/* Codec DAI name must be specified */
12963db769f1SKuninori Morimoto 		if (!codec->dai_name) {
1297923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1298923c5e61SMengdong Lin 				link->name);
1299923c5e61SMengdong Lin 			return -EINVAL;
1300923c5e61SMengdong Lin 		}
1301af18b13fSJerome Brunet 
1302af18b13fSJerome Brunet 		/*
1303af18b13fSJerome Brunet 		 * Defer card registration if codec component is not added to
1304af18b13fSJerome Brunet 		 * component list.
1305af18b13fSJerome Brunet 		 */
1306af18b13fSJerome Brunet 		if (!soc_find_component(codec))
1307af18b13fSJerome Brunet 			return -EPROBE_DEFER;
1308923c5e61SMengdong Lin 	}
1309923c5e61SMengdong Lin 
131034614739SJerome Brunet 	for_each_link_platforms(link, i, platform) {
13111d768989SKuninori Morimoto 		/*
131234614739SJerome Brunet 		 * Platform may be specified by either name or OF node, but it
131334614739SJerome Brunet 		 * can be left unspecified, then no components will be inserted
131434614739SJerome Brunet 		 * in the rtdcom list
13151d768989SKuninori Morimoto 		 */
131634614739SJerome Brunet 		if (!!platform->name == !!platform->of_node) {
1317910fdcabSKuninori Morimoto 			dev_err(card->dev,
131834614739SJerome Brunet 				"ASoC: Neither/both platform name/of_node are set for %s\n",
1319923c5e61SMengdong Lin 				link->name);
1320923c5e61SMengdong Lin 			return -EINVAL;
1321923c5e61SMengdong Lin 		}
13228780cf11SAjit Pandey 
13238780cf11SAjit Pandey 		/*
132434614739SJerome Brunet 		 * Defer card registration if platform component is not added to
132534614739SJerome Brunet 		 * component list.
13268780cf11SAjit Pandey 		 */
132734614739SJerome Brunet 		if (!soc_find_component(platform))
13288780cf11SAjit Pandey 			return -EPROBE_DEFER;
1329923c5e61SMengdong Lin 	}
1330923c5e61SMengdong Lin 
1331923c5e61SMengdong Lin 	/* FIXME */
133208a5841eSKuninori Morimoto 	if (link->num_cpus > 1) {
1333923c5e61SMengdong Lin 		dev_err(card->dev,
133408a5841eSKuninori Morimoto 			"ASoC: multi cpu is not yet supported %s\n",
1335923c5e61SMengdong Lin 			link->name);
1336923c5e61SMengdong Lin 		return -EINVAL;
1337923c5e61SMengdong Lin 	}
1338923c5e61SMengdong Lin 
1339923c5e61SMengdong Lin 	/*
1340923c5e61SMengdong Lin 	 * CPU device may be specified by either name or OF node, but
1341923c5e61SMengdong Lin 	 * can be left unspecified, and will be matched based on DAI
1342923c5e61SMengdong Lin 	 * name alone..
1343923c5e61SMengdong Lin 	 */
134408a5841eSKuninori Morimoto 	if (link->cpus->name && link->cpus->of_node) {
1345923c5e61SMengdong Lin 		dev_err(card->dev,
1346923c5e61SMengdong Lin 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
1347923c5e61SMengdong Lin 			link->name);
1348923c5e61SMengdong Lin 		return -EINVAL;
1349923c5e61SMengdong Lin 	}
13508780cf11SAjit Pandey 
13518780cf11SAjit Pandey 	/*
13528780cf11SAjit Pandey 	 * Defer card registartion if cpu dai component is not added to
13538780cf11SAjit Pandey 	 * component list.
13548780cf11SAjit Pandey 	 */
135508a5841eSKuninori Morimoto 	if ((link->cpus->of_node || link->cpus->name) &&
1356c1e230f0SKuninori Morimoto 	    !soc_find_component(link->cpus))
13578780cf11SAjit Pandey 		return -EPROBE_DEFER;
13588780cf11SAjit Pandey 
1359923c5e61SMengdong Lin 	/*
1360923c5e61SMengdong Lin 	 * At least one of CPU DAI name or CPU device name/node must be
1361923c5e61SMengdong Lin 	 * specified
1362923c5e61SMengdong Lin 	 */
136308a5841eSKuninori Morimoto 	if (!link->cpus->dai_name &&
136408a5841eSKuninori Morimoto 	    !(link->cpus->name || link->cpus->of_node)) {
1365923c5e61SMengdong Lin 		dev_err(card->dev,
1366923c5e61SMengdong Lin 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1367923c5e61SMengdong Lin 			link->name);
1368923c5e61SMengdong Lin 		return -EINVAL;
1369923c5e61SMengdong Lin 	}
1370923c5e61SMengdong Lin 
1371923c5e61SMengdong Lin 	return 0;
1372923c5e61SMengdong Lin }
1373923c5e61SMengdong Lin 
1374ef2e8175SKuninori Morimoto void snd_soc_disconnect_sync(struct device *dev)
1375ef2e8175SKuninori Morimoto {
13762c7b696aSMarcel Ziswiler 	struct snd_soc_component *component =
13772c7b696aSMarcel Ziswiler 			snd_soc_lookup_component(dev, NULL);
1378ef2e8175SKuninori Morimoto 
1379ef2e8175SKuninori Morimoto 	if (!component || !component->card)
1380ef2e8175SKuninori Morimoto 		return;
1381ef2e8175SKuninori Morimoto 
1382ef2e8175SKuninori Morimoto 	snd_card_disconnect_sync(component->card->snd_card);
1383ef2e8175SKuninori Morimoto }
1384df532185SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
1385ef2e8175SKuninori Morimoto 
1386f8f80361SMengdong Lin /**
1387f8f80361SMengdong Lin  * snd_soc_add_dai_link - Add a DAI link dynamically
1388f8f80361SMengdong Lin  * @card: The ASoC card to which the DAI link is added
1389f8f80361SMengdong Lin  * @dai_link: The new DAI link to add
1390f8f80361SMengdong Lin  *
1391f8f80361SMengdong Lin  * This function adds a DAI link to the ASoC card's link list.
1392f8f80361SMengdong Lin  *
1393f8f80361SMengdong Lin  * Note: Topology can use this API to add DAI links when probing the
1394f8f80361SMengdong Lin  * topology component. And machine drivers can still define static
1395f8f80361SMengdong Lin  * DAI links in dai_link array.
1396f8f80361SMengdong Lin  */
1397f8f80361SMengdong Lin int snd_soc_add_dai_link(struct snd_soc_card *card,
1398f8f80361SMengdong Lin 		struct snd_soc_dai_link *dai_link)
1399f8f80361SMengdong Lin {
1400f8f80361SMengdong Lin 	if (dai_link->dobj.type
1401f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1402f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1403f8f80361SMengdong Lin 			dai_link->dobj.type);
1404f8f80361SMengdong Lin 		return -EINVAL;
1405f8f80361SMengdong Lin 	}
1406f8f80361SMengdong Lin 
1407f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
14082c7b696aSMarcel Ziswiler 	/*
14092c7b696aSMarcel Ziswiler 	 * Notify the machine driver for extra initialization
1410d6f220eaSMengdong Lin 	 * on the link created by topology.
1411d6f220eaSMengdong Lin 	 */
1412d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->add_dai_link)
1413d6f220eaSMengdong Lin 		card->add_dai_link(card, dai_link);
1414d6f220eaSMengdong Lin 
14156634e3d6SKuninori Morimoto 	/* see for_each_card_links */
1416f8f80361SMengdong Lin 	list_add_tail(&dai_link->list, &card->dai_link_list);
1417f8f80361SMengdong Lin 
1418f8f80361SMengdong Lin 	return 0;
1419f8f80361SMengdong Lin }
1420f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1421f8f80361SMengdong Lin 
1422f8f80361SMengdong Lin /**
1423f8f80361SMengdong Lin  * snd_soc_remove_dai_link - Remove a DAI link from the list
1424f8f80361SMengdong Lin  * @card: The ASoC card that owns the link
1425f8f80361SMengdong Lin  * @dai_link: The DAI link to remove
1426f8f80361SMengdong Lin  *
1427f8f80361SMengdong Lin  * This function removes a DAI link from the ASoC card's link list.
1428f8f80361SMengdong Lin  *
1429f8f80361SMengdong Lin  * For DAI links previously added by topology, topology should
1430f8f80361SMengdong Lin  * remove them by using the dobj embedded in the link.
1431f8f80361SMengdong Lin  */
1432f8f80361SMengdong Lin void snd_soc_remove_dai_link(struct snd_soc_card *card,
1433f8f80361SMengdong Lin 			     struct snd_soc_dai_link *dai_link)
1434f8f80361SMengdong Lin {
1435f8f80361SMengdong Lin 	if (dai_link->dobj.type
1436f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1437f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1438f8f80361SMengdong Lin 			dai_link->dobj.type);
1439f8f80361SMengdong Lin 		return;
1440f8f80361SMengdong Lin 	}
1441f8f80361SMengdong Lin 
1442f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
14432c7b696aSMarcel Ziswiler 	/*
14442c7b696aSMarcel Ziswiler 	 * Notify the machine driver for extra destruction
1445d6f220eaSMengdong Lin 	 * on the link created by topology.
1446d6f220eaSMengdong Lin 	 */
1447d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->remove_dai_link)
1448d6f220eaSMengdong Lin 		card->remove_dai_link(card, dai_link);
1449d6f220eaSMengdong Lin 
1450c26a8841SKuninori Morimoto 	list_del(&dai_link->list);
1451f8f80361SMengdong Lin }
1452f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
14530671fd8eSKuninori Morimoto 
145425f7b701SArnaud Pouliquen static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
145525f7b701SArnaud Pouliquen 				struct snd_soc_pcm_runtime *rtd)
145625f7b701SArnaud Pouliquen {
145725f7b701SArnaud Pouliquen 	int i, ret = 0;
145825f7b701SArnaud Pouliquen 
145925f7b701SArnaud Pouliquen 	for (i = 0; i < num_dais; ++i) {
146025f7b701SArnaud Pouliquen 		struct snd_soc_dai_driver *drv = dais[i]->driver;
146125f7b701SArnaud Pouliquen 
1462de17f14eSRohit kumar 		if (drv->pcm_new)
146325f7b701SArnaud Pouliquen 			ret = drv->pcm_new(rtd, dais[i]);
146425f7b701SArnaud Pouliquen 		if (ret < 0) {
146525f7b701SArnaud Pouliquen 			dev_err(dais[i]->dev,
146625f7b701SArnaud Pouliquen 				"ASoC: Failed to bind %s with pcm device\n",
146725f7b701SArnaud Pouliquen 				dais[i]->name);
146825f7b701SArnaud Pouliquen 			return ret;
146925f7b701SArnaud Pouliquen 		}
147025f7b701SArnaud Pouliquen 	}
147125f7b701SArnaud Pouliquen 
147225f7b701SArnaud Pouliquen 	return 0;
147325f7b701SArnaud Pouliquen }
147425f7b701SArnaud Pouliquen 
1475c4b46982SKuninori Morimoto static int soc_link_init(struct snd_soc_card *card,
1476c4b46982SKuninori Morimoto 			 struct snd_soc_pcm_runtime *rtd)
1477c4b46982SKuninori Morimoto {
1478c4b46982SKuninori Morimoto 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1479c4b46982SKuninori Morimoto 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1480c4b46982SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
1481c4b46982SKuninori Morimoto 	struct snd_soc_component *component;
1482c4b46982SKuninori Morimoto 	int ret, num;
1483c4b46982SKuninori Morimoto 
1484c4b46982SKuninori Morimoto 	/* set default power off timeout */
1485c4b46982SKuninori Morimoto 	rtd->pmdown_time = pmdown_time;
14860168bf0dSLiam Girdwood 
14875f3484acSLars-Peter Clausen 	/* do machine specific initialization */
14885f3484acSLars-Peter Clausen 	if (dai_link->init) {
14895f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
14905f3484acSLars-Peter Clausen 		if (ret < 0) {
14915f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
14925f3484acSLars-Peter Clausen 				dai_link->name, ret);
14935f3484acSLars-Peter Clausen 			return ret;
14945f3484acSLars-Peter Clausen 		}
14955f3484acSLars-Peter Clausen 	}
14965f3484acSLars-Peter Clausen 
149740aa5383SRicard Wanderlof 	if (dai_link->dai_fmt) {
149840aa5383SRicard Wanderlof 		ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
149940aa5383SRicard Wanderlof 		if (ret)
150040aa5383SRicard Wanderlof 			return ret;
150140aa5383SRicard Wanderlof 	}
1502a5053a8eSKuninori Morimoto 
15035f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
15042e55b90aSLars-Peter Clausen 	soc_dpcm_debugfs_add(rtd);
15055f3484acSLars-Peter Clausen 
1506a655de80SLiam Girdwood 	num = rtd->num;
1507a655de80SLiam Girdwood 
1508a655de80SLiam Girdwood 	/*
1509a655de80SLiam Girdwood 	 * most drivers will register their PCMs using DAI link ordering but
1510a655de80SLiam Girdwood 	 * topology based drivers can use the DAI link id field to set PCM
1511a655de80SLiam Girdwood 	 * device number and then use rtd + a base offset of the BEs.
1512a655de80SLiam Girdwood 	 */
1513a655de80SLiam Girdwood 	for_each_rtdcom(rtd, rtdcom) {
1514a655de80SLiam Girdwood 		component = rtdcom->component;
1515a655de80SLiam Girdwood 
1516a655de80SLiam Girdwood 		if (!component->driver->use_dai_pcm_id)
1517a655de80SLiam Girdwood 			continue;
1518a655de80SLiam Girdwood 
1519a655de80SLiam Girdwood 		if (rtd->dai_link->no_pcm)
1520a655de80SLiam Girdwood 			num += component->driver->be_pcm_base;
1521a655de80SLiam Girdwood 		else
1522a655de80SLiam Girdwood 			num = rtd->dai_link->id;
1523a655de80SLiam Girdwood 	}
1524a655de80SLiam Girdwood 
1525b423c420SKuninori Morimoto 	/* create compress_device if possible */
1526b423c420SKuninori Morimoto 	ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1527b423c420SKuninori Morimoto 	if (ret != -ENOTSUPP) {
1528b423c420SKuninori Morimoto 		if (ret < 0)
1529f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
15301245b700SNamarta Kohli 					 dai_link->stream_name);
15311245b700SNamarta Kohli 		return ret;
15321245b700SNamarta Kohli 	}
1533b423c420SKuninori Morimoto 
1534f0fba2adSLiam Girdwood 	/* create the pcm */
1535a655de80SLiam Girdwood 	ret = soc_new_pcm(rtd, num);
1536f0fba2adSLiam Girdwood 	if (ret < 0) {
1537f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
15380837fc62SFabio Estevam 			dai_link->stream_name, ret);
1539f0fba2adSLiam Girdwood 		return ret;
1540f0fba2adSLiam Girdwood 	}
154125f7b701SArnaud Pouliquen 	ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
154225f7b701SArnaud Pouliquen 	if (ret < 0)
154325f7b701SArnaud Pouliquen 		return ret;
154425f7b701SArnaud Pouliquen 	ret = soc_link_dai_pcm_new(rtd->codec_dais,
154525f7b701SArnaud Pouliquen 				   rtd->num_codecs, rtd);
154625f7b701SArnaud Pouliquen 	return ret;
1547f0fba2adSLiam Girdwood }
1548f0fba2adSLiam Girdwood 
1549e8fbd250SKuninori Morimoto static void soc_unbind_aux_dev(struct snd_soc_card *card)
15504893a2ebSKuninori Morimoto {
1551e8fbd250SKuninori Morimoto 	struct snd_soc_component *component, *_component;
1552e8fbd250SKuninori Morimoto 
1553e8fbd250SKuninori Morimoto 	for_each_card_auxs_safe(card, component, _component) {
15544893a2ebSKuninori Morimoto 		component->init = NULL;
15554893a2ebSKuninori Morimoto 		list_del(&component->card_aux_list);
15564893a2ebSKuninori Morimoto 	}
1557e8fbd250SKuninori Morimoto }
15584893a2ebSKuninori Morimoto 
1559bee886f1SKuninori Morimoto static int soc_bind_aux_dev(struct snd_soc_card *card)
1560b19e6e7bSMark Brown {
1561f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1562bee886f1SKuninori Morimoto 	struct snd_soc_aux_dev *aux;
1563bee886f1SKuninori Morimoto 	int i;
15643ca041edSSebastian Reichel 
1565bee886f1SKuninori Morimoto 	for_each_card_pre_auxs(card, i, aux) {
1566f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1567bee886f1SKuninori Morimoto 		component = soc_find_component(&aux->dlc);
1568f2ed6b07SMengdong Lin 		if (!component)
15693dc29b8bSKuninori Morimoto 			return -EPROBE_DEFER;
15703ca041edSSebastian Reichel 
1571bee886f1SKuninori Morimoto 		component->init = aux->init;
1572c2b71c71SKuninori Morimoto 		/* see for_each_card_auxs */
1573d2e3a135SSylwester Nawrocki 		list_add(&component->card_aux_list, &card->aux_comp_list);
1574bee886f1SKuninori Morimoto 	}
1575f2ed6b07SMengdong Lin 	return 0;
1576b19e6e7bSMark Brown }
1577b19e6e7bSMark Brown 
1578f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1579f2ed6b07SMengdong Lin {
1580991454e1SKuninori Morimoto 	struct snd_soc_component *comp;
1581f2ed6b07SMengdong Lin 	int order;
1582f2ed6b07SMengdong Lin 	int ret;
1583f2ed6b07SMengdong Lin 
15841a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1585c2b71c71SKuninori Morimoto 		for_each_card_auxs(card, comp) {
1586f2ed6b07SMengdong Lin 			if (comp->driver->probe_order == order) {
1587f2ed6b07SMengdong Lin 				ret = soc_probe_component(card,	comp);
1588f2ed6b07SMengdong Lin 				if (ret < 0) {
1589f2ed6b07SMengdong Lin 					dev_err(card->dev,
1590f2ed6b07SMengdong Lin 						"ASoC: failed to probe aux component %s %d\n",
1591f2ed6b07SMengdong Lin 						comp->name, ret);
1592f2ed6b07SMengdong Lin 					return ret;
1593f2ed6b07SMengdong Lin 				}
1594f2ed6b07SMengdong Lin 			}
1595f2ed6b07SMengdong Lin 		}
1596f2ed6b07SMengdong Lin 	}
159765d9361fSLars-Peter Clausen 
159844c69bb1SLars-Peter Clausen 	return 0;
15993ca041edSSebastian Reichel }
16002eea392dSJarkko Nikula 
1601f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
160244c69bb1SLars-Peter Clausen {
1603f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1604f2ed6b07SMengdong Lin 	int order;
160544c69bb1SLars-Peter Clausen 
16061a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1607c2b71c71SKuninori Morimoto 		for_each_card_auxs_safe(card, comp, _comp) {
1608e8fbd250SKuninori Morimoto 			if (comp->driver->remove_order == order)
1609f2ed6b07SMengdong Lin 				soc_remove_component(comp);
16105f3484acSLars-Peter Clausen 		}
16112eea392dSJarkko Nikula 	}
16122eea392dSJarkko Nikula }
16132eea392dSJarkko Nikula 
1614ce64c8b9SLars-Peter Clausen /**
1615ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1616ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1617ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1618ce64c8b9SLars-Peter Clausen  *
1619ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1620ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1621ce64c8b9SLars-Peter Clausen  *
1622ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1623ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1624ce64c8b9SLars-Peter Clausen  *
1625ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1626ce64c8b9SLars-Peter Clausen  */
1627ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1628ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1629ce64c8b9SLars-Peter Clausen {
1630ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
16310b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1632ce64c8b9SLars-Peter Clausen 	unsigned int i;
1633ce64c8b9SLars-Peter Clausen 	int ret;
1634ce64c8b9SLars-Peter Clausen 
16350b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1636ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1637ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1638ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1639ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1640ce64c8b9SLars-Peter Clausen 			return ret;
1641ce64c8b9SLars-Peter Clausen 		}
1642ce64c8b9SLars-Peter Clausen 	}
1643ce64c8b9SLars-Peter Clausen 
16442c7b696aSMarcel Ziswiler 	/*
16452c7b696aSMarcel Ziswiler 	 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
16462c7b696aSMarcel Ziswiler 	 * the component which has non_legacy_dai_naming is Codec
16472c7b696aSMarcel Ziswiler 	 */
1648999f7f5aSKuninori Morimoto 	if (cpu_dai->component->driver->non_legacy_dai_naming) {
1649ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1650ce64c8b9SLars-Peter Clausen 
1651ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1652ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1653ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1654ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1655ce64c8b9SLars-Peter Clausen 			break;
1656ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1657ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1658ce64c8b9SLars-Peter Clausen 			break;
1659ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1660ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1661ce64c8b9SLars-Peter Clausen 			break;
1662ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1663ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1664ce64c8b9SLars-Peter Clausen 			break;
1665ce64c8b9SLars-Peter Clausen 		}
1666ce64c8b9SLars-Peter Clausen 
1667ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1668ce64c8b9SLars-Peter Clausen 	}
1669ce64c8b9SLars-Peter Clausen 
1670ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1671ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1672ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1673ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1674ce64c8b9SLars-Peter Clausen 		return ret;
1675ce64c8b9SLars-Peter Clausen 	}
1676ce64c8b9SLars-Peter Clausen 
1677ce64c8b9SLars-Peter Clausen 	return 0;
1678ce64c8b9SLars-Peter Clausen }
1679ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1680ce64c8b9SLars-Peter Clausen 
16811f5a4535STakashi Iwai #ifdef CONFIG_DMI
16822c7b696aSMarcel Ziswiler /*
16832c7b696aSMarcel Ziswiler  * Trim special characters, and replace '-' with '_' since '-' is used to
1684345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1685345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1686345233d7SLiam Girdwood  */
1687345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1688345233d7SLiam Girdwood {
1689345233d7SLiam Girdwood 	int i, j = 0;
1690345233d7SLiam Girdwood 
1691345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1692345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1693345233d7SLiam Girdwood 		    || (name[i] == '_'))
1694345233d7SLiam Girdwood 			name[j++] = name[i];
1695345233d7SLiam Girdwood 		else if (name[i] == '-')
1696345233d7SLiam Girdwood 			name[j++] = '_';
1697345233d7SLiam Girdwood 	}
1698345233d7SLiam Girdwood 
1699345233d7SLiam Girdwood 	name[j] = '\0';
1700345233d7SLiam Girdwood }
1701345233d7SLiam Girdwood 
17022c7b696aSMarcel Ziswiler /*
17032c7b696aSMarcel Ziswiler  * Check if a DMI field is valid, i.e. not containing any string
170498faf436SMengdong Lin  * in the black list.
170598faf436SMengdong Lin  */
170698faf436SMengdong Lin static int is_dmi_valid(const char *field)
170798faf436SMengdong Lin {
170898faf436SMengdong Lin 	int i = 0;
170998faf436SMengdong Lin 
171098faf436SMengdong Lin 	while (dmi_blacklist[i]) {
171198faf436SMengdong Lin 		if (strstr(field, dmi_blacklist[i]))
171298faf436SMengdong Lin 			return 0;
171398faf436SMengdong Lin 		i++;
171446b5a4d2SWu Fengguang 	}
171598faf436SMengdong Lin 
171698faf436SMengdong Lin 	return 1;
171798faf436SMengdong Lin }
171898faf436SMengdong Lin 
1719345233d7SLiam Girdwood /**
1720345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
1721345233d7SLiam Girdwood  * @card: The card to register DMI names
1722345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
1723345233d7SLiam Girdwood  *
1724345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
1725345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
1726345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
1727345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
1728345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
1729345233d7SLiam Girdwood  * as the sound card long name, in the format of
1730345233d7SLiam Girdwood  * "vendor-product-version-board"
1731345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
1732345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
1733345233d7SLiam Girdwood  * (UCM) configurations for the card.
1734345233d7SLiam Girdwood  *
1735345233d7SLiam Girdwood  * Possible card long names may be:
1736345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
1737345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1738345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1739345233d7SLiam Girdwood  *
1740345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
1741345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
1742345233d7SLiam Girdwood  *
1743345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
1744345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
1745345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
1746345233d7SLiam Girdwood  * charactors like SPACE.
1747345233d7SLiam Girdwood  *
1748345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
1749345233d7SLiam Girdwood  */
1750345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1751345233d7SLiam Girdwood {
1752345233d7SLiam Girdwood 	const char *vendor, *product, *product_version, *board;
1753345233d7SLiam Girdwood 	size_t longname_buf_size = sizeof(card->snd_card->longname);
1754345233d7SLiam Girdwood 	size_t len;
1755345233d7SLiam Girdwood 
1756345233d7SLiam Girdwood 	if (card->long_name)
1757345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
1758345233d7SLiam Girdwood 
1759345233d7SLiam Girdwood 	/* make up dmi long name as: vendor.product.version.board */
1760345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
176198faf436SMengdong Lin 	if (!vendor || !is_dmi_valid(vendor)) {
1762345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1763345233d7SLiam Girdwood 		return 0;
1764345233d7SLiam Girdwood 	}
1765345233d7SLiam Girdwood 
1766345233d7SLiam Girdwood 	snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1767345233d7SLiam Girdwood 			 "%s", vendor);
1768345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
1769345233d7SLiam Girdwood 
1770345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
177198faf436SMengdong Lin 	if (product && is_dmi_valid(product)) {
1772345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1773345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1774345233d7SLiam Girdwood 			 longname_buf_size - len,
1775345233d7SLiam Girdwood 			 "-%s", product);
1776345233d7SLiam Girdwood 
1777345233d7SLiam Girdwood 		len++;	/* skip the separator "-" */
1778345233d7SLiam Girdwood 		if (len < longname_buf_size)
1779345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1780345233d7SLiam Girdwood 
17812c7b696aSMarcel Ziswiler 		/*
17822c7b696aSMarcel Ziswiler 		 * some vendors like Lenovo may only put a self-explanatory
1783345233d7SLiam Girdwood 		 * name in the product version field
1784345233d7SLiam Girdwood 		 */
1785345233d7SLiam Girdwood 		product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
178698faf436SMengdong Lin 		if (product_version && is_dmi_valid(product_version)) {
1787345233d7SLiam Girdwood 			len = strlen(card->dmi_longname);
1788345233d7SLiam Girdwood 			snprintf(card->dmi_longname + len,
1789345233d7SLiam Girdwood 				 longname_buf_size - len,
1790345233d7SLiam Girdwood 				 "-%s", product_version);
1791345233d7SLiam Girdwood 
1792345233d7SLiam Girdwood 			len++;
1793345233d7SLiam Girdwood 			if (len < longname_buf_size)
1794345233d7SLiam Girdwood 				cleanup_dmi_name(card->dmi_longname + len);
1795345233d7SLiam Girdwood 		}
1796345233d7SLiam Girdwood 	}
1797345233d7SLiam Girdwood 
1798345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
179998faf436SMengdong Lin 	if (board && is_dmi_valid(board)) {
1800345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1801345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1802345233d7SLiam Girdwood 			 longname_buf_size - len,
1803345233d7SLiam Girdwood 			 "-%s", board);
1804345233d7SLiam Girdwood 
1805345233d7SLiam Girdwood 		len++;
1806345233d7SLiam Girdwood 		if (len < longname_buf_size)
1807345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1808345233d7SLiam Girdwood 	} else if (!product) {
1809345233d7SLiam Girdwood 		/* fall back to using legacy name */
1810345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1811345233d7SLiam Girdwood 		return 0;
1812345233d7SLiam Girdwood 	}
1813345233d7SLiam Girdwood 
1814345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
1815345233d7SLiam Girdwood 	if (flavour) {
1816345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1817345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1818345233d7SLiam Girdwood 			 longname_buf_size - len,
1819345233d7SLiam Girdwood 			 "-%s", flavour);
1820345233d7SLiam Girdwood 
1821345233d7SLiam Girdwood 		len++;
1822345233d7SLiam Girdwood 		if (len < longname_buf_size)
1823345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1824345233d7SLiam Girdwood 	}
1825345233d7SLiam Girdwood 
1826345233d7SLiam Girdwood 	/* set the card long name */
1827345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
1828345233d7SLiam Girdwood 
1829345233d7SLiam Girdwood 	return 0;
1830345233d7SLiam Girdwood }
1831345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
18321f5a4535STakashi Iwai #endif /* CONFIG_DMI */
1833345233d7SLiam Girdwood 
1834a655de80SLiam Girdwood static void soc_check_tplg_fes(struct snd_soc_card *card)
1835a655de80SLiam Girdwood {
1836a655de80SLiam Girdwood 	struct snd_soc_component *component;
1837a655de80SLiam Girdwood 	const struct snd_soc_component_driver *comp_drv;
1838a655de80SLiam Girdwood 	struct snd_soc_dai_link *dai_link;
1839a655de80SLiam Girdwood 	int i;
1840a655de80SLiam Girdwood 
1841368dee94SKuninori Morimoto 	for_each_component(component) {
1842a655de80SLiam Girdwood 
184349f9c4f2SDaniel Baluta 		/* does this component override BEs ? */
1844a655de80SLiam Girdwood 		if (!component->driver->ignore_machine)
1845a655de80SLiam Girdwood 			continue;
1846a655de80SLiam Girdwood 
1847a655de80SLiam Girdwood 		/* for this machine ? */
1848e194098bSPierre-Louis Bossart 		if (!strcmp(component->driver->ignore_machine,
1849a655de80SLiam Girdwood 			    card->dev->driver->name))
1850e194098bSPierre-Louis Bossart 			goto match;
1851e194098bSPierre-Louis Bossart 		if (strcmp(component->driver->ignore_machine,
1852e194098bSPierre-Louis Bossart 			   dev_name(card->dev)))
1853a655de80SLiam Girdwood 			continue;
1854e194098bSPierre-Louis Bossart match:
1855a655de80SLiam Girdwood 		/* machine matches, so override the rtd data */
18567fe072b4SKuninori Morimoto 		for_each_card_prelinks(card, i, dai_link) {
1857a655de80SLiam Girdwood 
1858a655de80SLiam Girdwood 			/* ignore this FE */
1859a655de80SLiam Girdwood 			if (dai_link->dynamic) {
1860a655de80SLiam Girdwood 				dai_link->ignore = true;
1861a655de80SLiam Girdwood 				continue;
1862a655de80SLiam Girdwood 			}
1863a655de80SLiam Girdwood 
186449f9c4f2SDaniel Baluta 			dev_info(card->dev, "info: override BE DAI link %s\n",
1865a655de80SLiam Girdwood 				 card->dai_link[i].name);
1866a655de80SLiam Girdwood 
1867a655de80SLiam Girdwood 			/* override platform component */
1868adb76b5bSKuninori Morimoto 			if (!dai_link->platforms) {
1869daecf46eSKuninori Morimoto 				dev_err(card->dev, "init platform error");
1870daecf46eSKuninori Morimoto 				continue;
1871daecf46eSKuninori Morimoto 			}
1872910fdcabSKuninori Morimoto 			dai_link->platforms->name = component->name;
1873a655de80SLiam Girdwood 
1874a655de80SLiam Girdwood 			/* convert non BE into BE */
1875a655de80SLiam Girdwood 			dai_link->no_pcm = 1;
1876a655de80SLiam Girdwood 
1877a655de80SLiam Girdwood 			/* override any BE fixups */
1878a655de80SLiam Girdwood 			dai_link->be_hw_params_fixup =
1879a655de80SLiam Girdwood 				component->driver->be_hw_params_fixup;
1880a655de80SLiam Girdwood 
18812c7b696aSMarcel Ziswiler 			/*
18822c7b696aSMarcel Ziswiler 			 * most BE links don't set stream name, so set it to
1883a655de80SLiam Girdwood 			 * dai link name if it's NULL to help bind widgets.
1884a655de80SLiam Girdwood 			 */
1885a655de80SLiam Girdwood 			if (!dai_link->stream_name)
1886a655de80SLiam Girdwood 				dai_link->stream_name = dai_link->name;
1887a655de80SLiam Girdwood 		}
1888a655de80SLiam Girdwood 
1889a655de80SLiam Girdwood 		/* Inform userspace we are using alternate topology */
1890a655de80SLiam Girdwood 		if (component->driver->topology_name_prefix) {
1891a655de80SLiam Girdwood 
1892a655de80SLiam Girdwood 			/* topology shortname created? */
1893a655de80SLiam Girdwood 			if (!card->topology_shortname_created) {
1894a655de80SLiam Girdwood 				comp_drv = component->driver;
1895a655de80SLiam Girdwood 
1896a655de80SLiam Girdwood 				snprintf(card->topology_shortname, 32, "%s-%s",
1897a655de80SLiam Girdwood 					 comp_drv->topology_name_prefix,
1898a655de80SLiam Girdwood 					 card->name);
1899a655de80SLiam Girdwood 				card->topology_shortname_created = true;
1900a655de80SLiam Girdwood 			}
1901a655de80SLiam Girdwood 
1902a655de80SLiam Girdwood 			/* use topology shortname */
1903a655de80SLiam Girdwood 			card->name = card->topology_shortname;
1904a655de80SLiam Girdwood 		}
1905a655de80SLiam Girdwood 	}
1906a655de80SLiam Girdwood }
1907a655de80SLiam Girdwood 
19080f23f718SKuninori Morimoto #define soc_setup_card_name(name, name1, name2, norm)		\
19090f23f718SKuninori Morimoto 	__soc_setup_card_name(name, sizeof(name), name1, name2, norm)
19100f23f718SKuninori Morimoto static void __soc_setup_card_name(char *name, int len,
19110f23f718SKuninori Morimoto 				  const char *name1, const char *name2,
19120f23f718SKuninori Morimoto 				  int normalization)
19130f23f718SKuninori Morimoto {
19140f23f718SKuninori Morimoto 	int i;
19150f23f718SKuninori Morimoto 
19160f23f718SKuninori Morimoto 	snprintf(name, len, "%s", name1 ? name1 : name2);
19170f23f718SKuninori Morimoto 
19180f23f718SKuninori Morimoto 	if (!normalization)
19190f23f718SKuninori Morimoto 		return;
19200f23f718SKuninori Morimoto 
19210f23f718SKuninori Morimoto 	/*
19220f23f718SKuninori Morimoto 	 * Name normalization
19230f23f718SKuninori Morimoto 	 *
19240f23f718SKuninori Morimoto 	 * The driver name is somewhat special, as it's used as a key for
19250f23f718SKuninori Morimoto 	 * searches in the user-space.
19260f23f718SKuninori Morimoto 	 *
19270f23f718SKuninori Morimoto 	 * ex)
19280f23f718SKuninori Morimoto 	 *	"abcd??efg" -> "abcd__efg"
19290f23f718SKuninori Morimoto 	 */
19300f23f718SKuninori Morimoto 	for (i = 0; i < len; i++) {
19310f23f718SKuninori Morimoto 		switch (name[i]) {
19320f23f718SKuninori Morimoto 		case '_':
19330f23f718SKuninori Morimoto 		case '-':
19340f23f718SKuninori Morimoto 		case '\0':
19350f23f718SKuninori Morimoto 			break;
19360f23f718SKuninori Morimoto 		default:
19370f23f718SKuninori Morimoto 			if (!isalnum(name[i]))
19380f23f718SKuninori Morimoto 				name[i] = '_';
19390f23f718SKuninori Morimoto 			break;
19400f23f718SKuninori Morimoto 		}
19410f23f718SKuninori Morimoto 	}
19420f23f718SKuninori Morimoto }
19430f23f718SKuninori Morimoto 
1944a4de83a3SKuninori Morimoto static void soc_cleanup_card_resources(struct snd_soc_card *card)
194553e947a0SKuninori Morimoto {
19467ce6088fSKuninori Morimoto 	struct snd_soc_dai_link *link, *_link;
19477ce6088fSKuninori Morimoto 
194853e947a0SKuninori Morimoto 	/* free the ALSA card at first; this syncs with pending operations */
194929040d1aSKuninori Morimoto 	if (card->snd_card) {
195053e947a0SKuninori Morimoto 		snd_card_free(card->snd_card);
195129040d1aSKuninori Morimoto 		card->snd_card = NULL;
195229040d1aSKuninori Morimoto 	}
195353e947a0SKuninori Morimoto 
195453e947a0SKuninori Morimoto 	/* remove and free each DAI */
19557ce6088fSKuninori Morimoto 	soc_remove_link_dais(card);
19567ce6088fSKuninori Morimoto 	soc_remove_link_components(card);
19577ce6088fSKuninori Morimoto 
19587ce6088fSKuninori Morimoto 	for_each_card_links_safe(card, link, _link)
19597ce6088fSKuninori Morimoto 		snd_soc_remove_dai_link(card, link);
19607ce6088fSKuninori Morimoto 
196153e947a0SKuninori Morimoto 	soc_remove_pcm_runtimes(card);
196253e947a0SKuninori Morimoto 
196353e947a0SKuninori Morimoto 	/* remove auxiliary devices */
196453e947a0SKuninori Morimoto 	soc_remove_aux_devices(card);
1965e8fbd250SKuninori Morimoto 	soc_unbind_aux_dev(card);
196653e947a0SKuninori Morimoto 
196753e947a0SKuninori Morimoto 	snd_soc_dapm_free(&card->dapm);
196853e947a0SKuninori Morimoto 	soc_cleanup_card_debugfs(card);
196953e947a0SKuninori Morimoto 
197053e947a0SKuninori Morimoto 	/* remove the card */
197153e947a0SKuninori Morimoto 	if (card->remove)
197253e947a0SKuninori Morimoto 		card->remove(card);
197353e947a0SKuninori Morimoto }
197453e947a0SKuninori Morimoto 
1975b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1976f0fba2adSLiam Girdwood {
19771a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
197861b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
1979c7e73774SKuninori Morimoto 	int ret, i;
198096dd3622SMark Brown 
198134e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
198270fc5373STzung-Bi Shih 	for_each_card_prelinks(card, i, dai_link) {
198370fc5373STzung-Bi Shih 		ret = soc_init_dai_link(card, dai_link);
198470fc5373STzung-Bi Shih 		if (ret) {
198570fc5373STzung-Bi Shih 			dev_err(card->dev, "ASoC: failed to init link %s: %d\n",
198670fc5373STzung-Bi Shih 				dai_link->name, ret);
198770fc5373STzung-Bi Shih 			mutex_unlock(&client_mutex);
198870fc5373STzung-Bi Shih 			return ret;
198970fc5373STzung-Bi Shih 		}
199070fc5373STzung-Bi Shih 	}
199101b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1992f0fba2adSLiam Girdwood 
199395c267ddSKuninori Morimoto 	snd_soc_dapm_init(&card->dapm, card, NULL);
199453e947a0SKuninori Morimoto 
1995a655de80SLiam Girdwood 	/* check whether any platform is ignore machine FE and using topology */
1996a655de80SLiam Girdwood 	soc_check_tplg_fes(card);
1997a655de80SLiam Girdwood 
1998b19e6e7bSMark Brown 	/* bind DAIs */
19997fe072b4SKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link) {
20007fe072b4SKuninori Morimoto 		ret = soc_bind_dai_link(card, dai_link);
2001b19e6e7bSMark Brown 		if (ret != 0)
200253e947a0SKuninori Morimoto 			goto probe_end;
2003db2a4165SFrank Mandarino 	}
2004db2a4165SFrank Mandarino 
200544c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
2006bee886f1SKuninori Morimoto 	ret = soc_bind_aux_dev(card);
2007bee886f1SKuninori Morimoto 	if (ret < 0)
200853e947a0SKuninori Morimoto 		goto probe_end;
2009db2a4165SFrank Mandarino 
2010f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
20115b99a0aaSKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link) {
20125b99a0aaSKuninori Morimoto 		ret = snd_soc_add_dai_link(card, dai_link);
20135b99a0aaSKuninori Morimoto 		if (ret < 0)
20145b99a0aaSKuninori Morimoto 			goto probe_end;
20155b99a0aaSKuninori Morimoto 	}
2016f8f80361SMengdong Lin 
2017f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
2018102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2019f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
2020f0fba2adSLiam Girdwood 	if (ret < 0) {
202110e8aa9aSMichał Mirosław 		dev_err(card->dev,
202210e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
202310e8aa9aSMichał Mirosław 			card->name, ret);
202453e947a0SKuninori Morimoto 		goto probe_end;
2025db2a4165SFrank Mandarino 	}
2026db2a4165SFrank Mandarino 
20270757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
20280757d834SLars-Peter Clausen 
2029b3da4251SKuninori Morimoto 	soc_resume_init(card);
20306ed25978SAndy Green 
2031b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
20329a841ebbSMark Brown 					card->num_dapm_widgets);
2033b8ba3b57SKuninori Morimoto 	if (ret < 0)
2034b8ba3b57SKuninori Morimoto 		goto probe_end;
20359a841ebbSMark Brown 
2036b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2037f23e860eSNicolin Chen 					card->num_of_dapm_widgets);
2038b8ba3b57SKuninori Morimoto 	if (ret < 0)
2039b8ba3b57SKuninori Morimoto 		goto probe_end;
2040f23e860eSNicolin Chen 
2041f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
2042f0fba2adSLiam Girdwood 	if (card->probe) {
2043e7361ec4SMark Brown 		ret = card->probe(card);
2044f0fba2adSLiam Girdwood 		if (ret < 0)
204553e947a0SKuninori Morimoto 			goto probe_end;
2046f0fba2adSLiam Girdwood 	}
2047f0fba2adSLiam Girdwood 
204862ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
204962f07a6bSKuninori Morimoto 	ret = soc_probe_link_components(card);
205062ae68faSStephen Warren 	if (ret < 0) {
2051f110bfc7SLiam Girdwood 		dev_err(card->dev,
205262f07a6bSKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
205353e947a0SKuninori Morimoto 		goto probe_end;
205462ae68faSStephen Warren 	}
205562ae68faSStephen Warren 
2056f2ed6b07SMengdong Lin 	/* probe auxiliary components */
2057f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
2058f2ed6b07SMengdong Lin 	if (ret < 0)
205953e947a0SKuninori Morimoto 		goto probe_end;
2060f2ed6b07SMengdong Lin 
20612c7b696aSMarcel Ziswiler 	/*
20622c7b696aSMarcel Ziswiler 	 * Find new DAI links added during probing components and bind them.
206361b0088bSMengdong Lin 	 * Components with topology may bring new DAIs and DAI links.
206461b0088bSMengdong Lin 	 */
206598061fdbSKuninori Morimoto 	for_each_card_links(card, dai_link) {
206661b0088bSMengdong Lin 		if (soc_is_dai_link_bound(card, dai_link))
206761b0088bSMengdong Lin 			continue;
206861b0088bSMengdong Lin 
206961b0088bSMengdong Lin 		ret = soc_init_dai_link(card, dai_link);
207061b0088bSMengdong Lin 		if (ret)
207153e947a0SKuninori Morimoto 			goto probe_end;
207261b0088bSMengdong Lin 		ret = soc_bind_dai_link(card, dai_link);
207361b0088bSMengdong Lin 		if (ret)
207453e947a0SKuninori Morimoto 			goto probe_end;
207561b0088bSMengdong Lin 	}
207661b0088bSMengdong Lin 
207762ae68faSStephen Warren 	/* probe all DAI links on this card */
2078c7e73774SKuninori Morimoto 	ret = soc_probe_link_dais(card);
2079fe3e78e0SMark Brown 	if (ret < 0) {
2080f110bfc7SLiam Girdwood 		dev_err(card->dev,
2081c7e73774SKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
208253e947a0SKuninori Morimoto 		goto probe_end;
2083fe3e78e0SMark Brown 	}
2084fe3e78e0SMark Brown 
2085c4b46982SKuninori Morimoto 	for_each_card_rtds(card, rtd)
2086c4b46982SKuninori Morimoto 		soc_link_init(card, rtd);
2087c4b46982SKuninori Morimoto 
2088888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
2089b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
2090888df395SMark Brown 
20919b98c7c2SKuninori Morimoto 	ret = snd_soc_add_card_controls(card, card->controls,
20922c7b696aSMarcel Ziswiler 					card->num_controls);
20939b98c7c2SKuninori Morimoto 	if (ret < 0)
20949b98c7c2SKuninori Morimoto 		goto probe_end;
2095b7af1dafSMark Brown 
2096daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2097b8ad29deSMark Brown 				      card->num_dapm_routes);
2098daa480bdSKuninori Morimoto 	if (ret < 0)
2099daa480bdSKuninori Morimoto 		goto probe_end;
2100b8ad29deSMark Brown 
2101daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2102f23e860eSNicolin Chen 				      card->num_of_dapm_routes);
2103daa480bdSKuninori Morimoto 	if (ret < 0)
2104daa480bdSKuninori Morimoto 		goto probe_end;
210575d9ac46SMark Brown 
2106861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
2107861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
2108861886d3STakashi Iwai 
21090f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->shortname,
21100f23f718SKuninori Morimoto 			    card->name, NULL, 0);
21110f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->longname,
21120f23f718SKuninori Morimoto 			    card->long_name, card->name, 0);
21130f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->driver,
21140f23f718SKuninori Morimoto 			    card->driver_name, card->name, 1);
2115fe3e78e0SMark Brown 
211628e9ad92SMark Brown 	if (card->late_probe) {
211728e9ad92SMark Brown 		ret = card->late_probe(card);
211828e9ad92SMark Brown 		if (ret < 0) {
2119f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
212028e9ad92SMark Brown 				card->name, ret);
212153e947a0SKuninori Morimoto 			goto probe_end;
212228e9ad92SMark Brown 		}
212328e9ad92SMark Brown 	}
212428e9ad92SMark Brown 
2125824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
21268c193b8dSLars-Peter Clausen 
2127f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2128fe3e78e0SMark Brown 	if (ret < 0) {
2129f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2130f110bfc7SLiam Girdwood 				ret);
213153e947a0SKuninori Morimoto 		goto probe_end;
2132fe3e78e0SMark Brown 	}
2133fe3e78e0SMark Brown 
2134435c5e25SMark Brown 	card->instantiated = 1;
2135882eab6cSTzung-Bi Shih 	dapm_mark_endpoints_dirty(card);
21364f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2137b19e6e7bSMark Brown 
213853e947a0SKuninori Morimoto probe_end:
213953e947a0SKuninori Morimoto 	if (ret < 0)
214053e947a0SKuninori Morimoto 		soc_cleanup_card_resources(card);
2141db2a4165SFrank Mandarino 
2142f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
214334e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2144db2a4165SFrank Mandarino 
2145b19e6e7bSMark Brown 	return ret;
2146435c5e25SMark Brown }
2147435c5e25SMark Brown 
2148435c5e25SMark Brown /* probes a new socdev */
2149435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2150435c5e25SMark Brown {
2151f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2152435c5e25SMark Brown 
215370a7ca34SVinod Koul 	/*
215470a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
215570a7ca34SVinod Koul 	 * we should not be here in that case so ret error
215670a7ca34SVinod Koul 	 */
215770a7ca34SVinod Koul 	if (!card)
215870a7ca34SVinod Koul 		return -EINVAL;
215970a7ca34SVinod Koul 
2160fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2161f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2162fe4085e8SMark Brown 		 card->name);
2163fe4085e8SMark Brown 
2164435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2165435c5e25SMark Brown 	card->dev = &pdev->dev;
2166f0fba2adSLiam Girdwood 
216728d528c8SMark Brown 	return snd_soc_register_card(card);
2168435c5e25SMark Brown }
2169435c5e25SMark Brown 
2170b0e26485SVinod Koul /* removes a socdev */
2171b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2172b0e26485SVinod Koul {
2173b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2174b0e26485SVinod Koul 
2175c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2176db2a4165SFrank Mandarino 	return 0;
2177db2a4165SFrank Mandarino }
2178db2a4165SFrank Mandarino 
21796f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
218051737470SMark Brown {
21816f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
21821a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
218351737470SMark Brown 
218451737470SMark Brown 	if (!card->instantiated)
2185416356fcSMark Brown 		return 0;
218651737470SMark Brown 
21872c7b696aSMarcel Ziswiler 	/*
21882c7b696aSMarcel Ziswiler 	 * Flush out pmdown_time work - we actually do want to run it
21892c7b696aSMarcel Ziswiler 	 * now, we're shutting down so no imminent restart.
21902c7b696aSMarcel Ziswiler 	 */
219165462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
219251737470SMark Brown 
2193f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2194416356fcSMark Brown 
2195988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
2196bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
219788bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
21980b7990e3SKuninori Morimoto 		struct snd_soc_dai *codec_dai;
21991a497983SMengdong Lin 		int i;
220088bd870fSBenoit Cousson 
2201988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
22020b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
220388bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
220488bd870fSBenoit Cousson 		}
2205988e8cc4SNicolin Chen 	}
2206988e8cc4SNicolin Chen 
2207416356fcSMark Brown 	return 0;
220851737470SMark Brown }
22096f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
221051737470SMark Brown 
22116f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2212b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2213b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2214b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2215b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
22166f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2217b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2218416356fcSMark Brown };
2219deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2220416356fcSMark Brown 
2221db2a4165SFrank Mandarino /* ASoC platform driver */
2222db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2223db2a4165SFrank Mandarino 	.driver		= {
2224db2a4165SFrank Mandarino 		.name		= "soc-audio",
22256f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2226db2a4165SFrank Mandarino 	},
2227db2a4165SFrank Mandarino 	.probe		= soc_probe,
2228db2a4165SFrank Mandarino 	.remove		= soc_remove,
2229db2a4165SFrank Mandarino };
2230db2a4165SFrank Mandarino 
2231096e49d5SMark Brown /**
2232db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2233db2a4165SFrank Mandarino  * @_template: control template
2234db2a4165SFrank Mandarino  * @data: control private data
2235ac11a2b3SMark Brown  * @long_name: control long name
2236efb7ac3fSMark Brown  * @prefix: control name prefix
2237db2a4165SFrank Mandarino  *
2238db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2239db2a4165SFrank Mandarino  *
2240db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2241db2a4165SFrank Mandarino  */
2242db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
22433056557fSMark Brown 				  void *data, const char *long_name,
2244efb7ac3fSMark Brown 				  const char *prefix)
2245db2a4165SFrank Mandarino {
2246db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2247efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2248efb7ac3fSMark Brown 	char *name = NULL;
2249db2a4165SFrank Mandarino 
2250db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2251db2a4165SFrank Mandarino 	template.index = 0;
2252db2a4165SFrank Mandarino 
2253efb7ac3fSMark Brown 	if (!long_name)
2254efb7ac3fSMark Brown 		long_name = template.name;
2255efb7ac3fSMark Brown 
2256efb7ac3fSMark Brown 	if (prefix) {
22572b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2258efb7ac3fSMark Brown 		if (!name)
2259efb7ac3fSMark Brown 			return NULL;
2260efb7ac3fSMark Brown 
2261efb7ac3fSMark Brown 		template.name = name;
2262efb7ac3fSMark Brown 	} else {
2263efb7ac3fSMark Brown 		template.name = long_name;
2264efb7ac3fSMark Brown 	}
2265efb7ac3fSMark Brown 
2266efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2267efb7ac3fSMark Brown 
2268efb7ac3fSMark Brown 	kfree(name);
2269efb7ac3fSMark Brown 
2270efb7ac3fSMark Brown 	return kcontrol;
2271db2a4165SFrank Mandarino }
2272db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2273db2a4165SFrank Mandarino 
2274022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2275022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2276022658beSLiam Girdwood 	const char *prefix, void *data)
2277022658beSLiam Girdwood {
2278022658beSLiam Girdwood 	int err, i;
2279022658beSLiam Girdwood 
2280022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2281022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
22822c7b696aSMarcel Ziswiler 
2283022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2284022658beSLiam Girdwood 						     control->name, prefix));
2285022658beSLiam Girdwood 		if (err < 0) {
2286f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2287f110bfc7SLiam Girdwood 				control->name, err);
2288022658beSLiam Girdwood 			return err;
2289022658beSLiam Girdwood 		}
2290022658beSLiam Girdwood 	}
2291022658beSLiam Girdwood 
2292022658beSLiam Girdwood 	return 0;
2293022658beSLiam Girdwood }
2294022658beSLiam Girdwood 
22954fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
22964fefd698SDimitris Papastamos 					       const char *name)
22974fefd698SDimitris Papastamos {
22984fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
22994fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
23004fefd698SDimitris Papastamos 
23014fefd698SDimitris Papastamos 	if (unlikely(!name))
23024fefd698SDimitris Papastamos 		return NULL;
23034fefd698SDimitris Papastamos 
23044fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
23054fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
23064fefd698SDimitris Papastamos 			return kctl;
23074fefd698SDimitris Papastamos 	return NULL;
23084fefd698SDimitris Papastamos }
23094fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
23104fefd698SDimitris Papastamos 
2311db2a4165SFrank Mandarino /**
23120f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
23130f2780adSLars-Peter Clausen  *
23140f2780adSLars-Peter Clausen  * @component: Component to add controls to
23150f2780adSLars-Peter Clausen  * @controls: Array of controls to add
23160f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
23170f2780adSLars-Peter Clausen  *
23180f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
23190f2780adSLars-Peter Clausen  */
23200f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
23210f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
23220f2780adSLars-Peter Clausen {
23230f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
23240f2780adSLars-Peter Clausen 
23250f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
23260f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
23270f2780adSLars-Peter Clausen }
23280f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
23290f2780adSLars-Peter Clausen 
23300f2780adSLars-Peter Clausen /**
2331022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2332022658beSLiam Girdwood  * Convenience function to add a list of controls.
2333022658beSLiam Girdwood  *
2334022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2335022658beSLiam Girdwood  * @controls: array of controls to add
2336022658beSLiam Girdwood  * @num_controls: number of elements in the array
2337022658beSLiam Girdwood  *
2338022658beSLiam Girdwood  * Return 0 for success, else error.
2339022658beSLiam Girdwood  */
2340022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2341022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2342022658beSLiam Girdwood {
2343022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2344022658beSLiam Girdwood 
2345022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2346022658beSLiam Girdwood 			NULL, soc_card);
2347022658beSLiam Girdwood }
2348022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2349022658beSLiam Girdwood 
2350022658beSLiam Girdwood /**
2351022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2352022658beSLiam Girdwood  * Convienience function to add a list of controls.
2353022658beSLiam Girdwood  *
2354022658beSLiam Girdwood  * @dai: DAI to add controls to
2355022658beSLiam Girdwood  * @controls: array of controls to add
2356022658beSLiam Girdwood  * @num_controls: number of elements in the array
2357022658beSLiam Girdwood  *
2358022658beSLiam Girdwood  * Return 0 for success, else error.
2359022658beSLiam Girdwood  */
2360022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2361022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2362022658beSLiam Girdwood {
2363313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2364022658beSLiam Girdwood 
2365022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2366022658beSLiam Girdwood 			NULL, dai);
2367022658beSLiam Girdwood }
2368022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2369022658beSLiam Girdwood 
2370e894efefSSrinivas Kandagatla static int snd_soc_bind_card(struct snd_soc_card *card)
2371e894efefSSrinivas Kandagatla {
2372e894efefSSrinivas Kandagatla 	struct snd_soc_pcm_runtime *rtd;
2373e894efefSSrinivas Kandagatla 	int ret;
2374e894efefSSrinivas Kandagatla 
2375e894efefSSrinivas Kandagatla 	ret = snd_soc_instantiate_card(card);
2376e894efefSSrinivas Kandagatla 	if (ret != 0)
2377e894efefSSrinivas Kandagatla 		return ret;
2378e894efefSSrinivas Kandagatla 
2379e894efefSSrinivas Kandagatla 	/* deactivate pins to sleep state */
2380bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
2381e894efefSSrinivas Kandagatla 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2382e894efefSSrinivas Kandagatla 		struct snd_soc_dai *codec_dai;
2383e894efefSSrinivas Kandagatla 		int j;
2384e894efefSSrinivas Kandagatla 
2385e894efefSSrinivas Kandagatla 		for_each_rtd_codec_dai(rtd, j, codec_dai) {
2386e894efefSSrinivas Kandagatla 			if (!codec_dai->active)
2387e894efefSSrinivas Kandagatla 				pinctrl_pm_select_sleep_state(codec_dai->dev);
2388e894efefSSrinivas Kandagatla 		}
2389e894efefSSrinivas Kandagatla 
2390e894efefSSrinivas Kandagatla 		if (!cpu_dai->active)
2391e894efefSSrinivas Kandagatla 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
2392e894efefSSrinivas Kandagatla 	}
2393e894efefSSrinivas Kandagatla 
2394e894efefSSrinivas Kandagatla 	return ret;
2395e894efefSSrinivas Kandagatla }
2396e894efefSSrinivas Kandagatla 
2397c5af3a2eSMark Brown /**
2398c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2399c5af3a2eSMark Brown  *
2400ac11a2b3SMark Brown  * @card: Card to register
2401c5af3a2eSMark Brown  *
2402c5af3a2eSMark Brown  */
240370a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2404c5af3a2eSMark Brown {
2405c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2406c5af3a2eSMark Brown 		return -EINVAL;
2407c5af3a2eSMark Brown 
2408ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2409ed77cc12SMark Brown 
2410b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->widgets);
2411b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->paths);
2412b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->dapm_list);
2413b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->aux_comp_list);
2414b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->component_dev_list);
2415b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->list);
2416f8f80361SMengdong Lin 	INIT_LIST_HEAD(&card->dai_link_list);
24171a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
2418db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
24198a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2420b03bfaecSKuninori Morimoto 
2421b03bfaecSKuninori Morimoto 	card->num_rtd = 0;
2422c5af3a2eSMark Brown 	card->instantiated = 0;
2423f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
2424a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
242572b745e3SPeter Ujfalusi 	mutex_init(&card->pcm_mutex);
2426a9764869SKaiChieh Chuang 	spin_lock_init(&card->dpcm_lock);
2427c5af3a2eSMark Brown 
2428e894efefSSrinivas Kandagatla 	return snd_soc_bind_card(card);
2429c5af3a2eSMark Brown }
243070a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2431c5af3a2eSMark Brown 
2432e894efefSSrinivas Kandagatla static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2433e894efefSSrinivas Kandagatla {
2434e894efefSSrinivas Kandagatla 	if (card->instantiated) {
2435e894efefSSrinivas Kandagatla 		card->instantiated = false;
2436e894efefSSrinivas Kandagatla 		snd_soc_dapm_shutdown(card);
243753e947a0SKuninori Morimoto 		snd_soc_flush_all_delayed_work(card);
2438f96fb7d1SRanjani Sridharan 
2439f96fb7d1SRanjani Sridharan 		/* remove all components used by DAI links on this card */
2440b006c0c6SKuninori Morimoto 		soc_remove_link_components(card);
2441f96fb7d1SRanjani Sridharan 
2442e894efefSSrinivas Kandagatla 		soc_cleanup_card_resources(card);
2443e894efefSSrinivas Kandagatla 		if (!unregister)
2444e894efefSSrinivas Kandagatla 			list_add(&card->list, &unbind_card_list);
2445e894efefSSrinivas Kandagatla 	} else {
2446e894efefSSrinivas Kandagatla 		if (unregister)
2447e894efefSSrinivas Kandagatla 			list_del(&card->list);
2448e894efefSSrinivas Kandagatla 	}
2449e894efefSSrinivas Kandagatla }
2450e894efefSSrinivas Kandagatla 
2451c5af3a2eSMark Brown /**
2452c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2453c5af3a2eSMark Brown  *
2454ac11a2b3SMark Brown  * @card: Card to unregister
2455c5af3a2eSMark Brown  *
2456c5af3a2eSMark Brown  */
245770a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2458c5af3a2eSMark Brown {
2459b545542aSKuninori Morimoto 	mutex_lock(&client_mutex);
2460e894efefSSrinivas Kandagatla 	snd_soc_unbind_card(card, true);
2461b545542aSKuninori Morimoto 	mutex_unlock(&client_mutex);
2462f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2463c5af3a2eSMark Brown 
2464c5af3a2eSMark Brown 	return 0;
2465c5af3a2eSMark Brown }
246670a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2467c5af3a2eSMark Brown 
2468f0fba2adSLiam Girdwood /*
2469f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
2470f0fba2adSLiam Girdwood  * and sanitizing names.
2471f0fba2adSLiam Girdwood  */
24720b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2473f0fba2adSLiam Girdwood {
2474f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
2475f0fba2adSLiam Girdwood 	int id1, id2;
2476f0fba2adSLiam Girdwood 
2477f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
2478f0fba2adSLiam Girdwood 		return NULL;
2479f0fba2adSLiam Girdwood 
248058818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2481f0fba2adSLiam Girdwood 
2482f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
2483f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
2484f0fba2adSLiam Girdwood 	if (found) {
2485f0fba2adSLiam Girdwood 		/* get ID */
2486f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2487f0fba2adSLiam Girdwood 
2488f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
2489f0fba2adSLiam Girdwood 			if (*id == -1)
2490f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
2491f0fba2adSLiam Girdwood 		}
2492f0fba2adSLiam Girdwood 
2493f0fba2adSLiam Girdwood 	} else {
2494f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr" */
2495f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2496f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
2497f0fba2adSLiam Girdwood 
2498f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
249905899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
2500f0fba2adSLiam Girdwood 
2501f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
25022c7b696aSMarcel Ziswiler 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
25032c7b696aSMarcel Ziswiler 				 name);
250458818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2505f0fba2adSLiam Girdwood 		} else
2506f0fba2adSLiam Girdwood 			*id = 0;
2507f0fba2adSLiam Girdwood 	}
2508f0fba2adSLiam Girdwood 
2509*50014499SKuninori Morimoto 	return devm_kstrdup(dev, name, GFP_KERNEL);
2510f0fba2adSLiam Girdwood }
2511f0fba2adSLiam Girdwood 
2512f0fba2adSLiam Girdwood /*
2513f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
2514f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
2515f0fba2adSLiam Girdwood  */
2516f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
2517f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
2518f0fba2adSLiam Girdwood {
2519f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
252010e8aa9aSMichał Mirosław 		dev_err(dev,
252110e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
252210e8aa9aSMichał Mirosław 			dev_name(dev));
2523f0fba2adSLiam Girdwood 		return NULL;
2524f0fba2adSLiam Girdwood 	}
2525f0fba2adSLiam Girdwood 
2526*50014499SKuninori Morimoto 	return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2527f0fba2adSLiam Girdwood }
2528f0fba2adSLiam Girdwood 
25299115171aSMark Brown /**
253032c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
25319115171aSMark Brown  *
253232c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
25339115171aSMark Brown  */
253432c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
25359115171aSMark Brown {
25365c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
25379115171aSMark Brown 
253815a0c645SKuninori Morimoto 	for_each_component_dais_safe(component, dai, _dai) {
253932c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
254032c9ba54SLars-Peter Clausen 			dai->name);
25419115171aSMark Brown 		list_del(&dai->list);
25429115171aSMark Brown 	}
254332c9ba54SLars-Peter Clausen }
25449115171aSMark Brown 
25455e4fb372SMengdong Lin /* Create a DAI and add it to the component's DAI list */
25465e4fb372SMengdong Lin static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
25475e4fb372SMengdong Lin 	struct snd_soc_dai_driver *dai_drv,
25485e4fb372SMengdong Lin 	bool legacy_dai_naming)
25495e4fb372SMengdong Lin {
25505e4fb372SMengdong Lin 	struct device *dev = component->dev;
25515e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
25525e4fb372SMengdong Lin 
25535e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
25545e4fb372SMengdong Lin 
2555*50014499SKuninori Morimoto 	dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
25565e4fb372SMengdong Lin 	if (dai == NULL)
25575e4fb372SMengdong Lin 		return NULL;
25585e4fb372SMengdong Lin 
25595e4fb372SMengdong Lin 	/*
25605e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
25615e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
25625e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
25635e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
25645e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
25655e4fb372SMengdong Lin 	 * component-less anymore.
25665e4fb372SMengdong Lin 	 */
25675e4fb372SMengdong Lin 	if (legacy_dai_naming &&
25685e4fb372SMengdong Lin 	    (dai_drv->id == 0 || dai_drv->name == NULL)) {
25695e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
25705e4fb372SMengdong Lin 	} else {
25715e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
25725e4fb372SMengdong Lin 		if (dai_drv->id)
25735e4fb372SMengdong Lin 			dai->id = dai_drv->id;
25745e4fb372SMengdong Lin 		else
25755e4fb372SMengdong Lin 			dai->id = component->num_dai;
25765e4fb372SMengdong Lin 	}
2577*50014499SKuninori Morimoto 	if (!dai->name)
25785e4fb372SMengdong Lin 		return NULL;
25795e4fb372SMengdong Lin 
25805e4fb372SMengdong Lin 	dai->component = component;
25815e4fb372SMengdong Lin 	dai->dev = dev;
25825e4fb372SMengdong Lin 	dai->driver = dai_drv;
25835e4fb372SMengdong Lin 	if (!dai->driver->ops)
25845e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
25855e4fb372SMengdong Lin 
258615a0c645SKuninori Morimoto 	/* see for_each_component_dais */
258758bf4179SKuninori Morimoto 	list_add_tail(&dai->list, &component->dai_list);
25885e4fb372SMengdong Lin 	component->num_dai++;
25895e4fb372SMengdong Lin 
25905e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
25915e4fb372SMengdong Lin 	return dai;
25925e4fb372SMengdong Lin }
25935e4fb372SMengdong Lin 
25949115171aSMark Brown /**
259532c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
25969115171aSMark Brown  *
25976106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
25986106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
2599ac11a2b3SMark Brown  * @count: Number of DAIs
26009115171aSMark Brown  */
260132c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
26022c7b696aSMarcel Ziswiler 				 struct snd_soc_dai_driver *dai_drv,
26032c7b696aSMarcel Ziswiler 				 size_t count)
26049115171aSMark Brown {
26056106d129SLars-Peter Clausen 	struct device *dev = component->dev;
2606f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
260732c9ba54SLars-Peter Clausen 	unsigned int i;
260832c9ba54SLars-Peter Clausen 	int ret;
2609f0fba2adSLiam Girdwood 
26105b5e0928SAlexey Dobriyan 	dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
26119115171aSMark Brown 
26129115171aSMark Brown 	for (i = 0; i < count; i++) {
2613f0fba2adSLiam Girdwood 
26142c7b696aSMarcel Ziswiler 		dai = soc_add_dai(component, dai_drv + i, count == 1 &&
26152c7b696aSMarcel Ziswiler 				  !component->driver->non_legacy_dai_naming);
2616c46e0079SAxel Lin 		if (dai == NULL) {
2617c46e0079SAxel Lin 			ret = -ENOMEM;
2618c46e0079SAxel Lin 			goto err;
2619c46e0079SAxel Lin 		}
2620f0fba2adSLiam Girdwood 	}
2621f0fba2adSLiam Girdwood 
26229115171aSMark Brown 	return 0;
26239115171aSMark Brown 
26249115171aSMark Brown err:
262532c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
26269115171aSMark Brown 
26279115171aSMark Brown 	return ret;
26289115171aSMark Brown }
26299115171aSMark Brown 
263068003e6cSMengdong Lin /**
263168003e6cSMengdong Lin  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
263268003e6cSMengdong Lin  *
263368003e6cSMengdong Lin  * @component: The component the DAIs are registered for
263468003e6cSMengdong Lin  * @dai_drv: DAI driver to use for the DAI
263568003e6cSMengdong Lin  *
263668003e6cSMengdong Lin  * Topology can use this API to register DAIs when probing a component.
263768003e6cSMengdong Lin  * These DAIs's widgets will be freed in the card cleanup and the DAIs
263868003e6cSMengdong Lin  * will be freed in the component cleanup.
263968003e6cSMengdong Lin  */
264068003e6cSMengdong Lin int snd_soc_register_dai(struct snd_soc_component *component,
264168003e6cSMengdong Lin 	struct snd_soc_dai_driver *dai_drv)
264268003e6cSMengdong Lin {
264368003e6cSMengdong Lin 	struct snd_soc_dapm_context *dapm =
264468003e6cSMengdong Lin 		snd_soc_component_get_dapm(component);
264568003e6cSMengdong Lin 	struct snd_soc_dai *dai;
264668003e6cSMengdong Lin 	int ret;
264768003e6cSMengdong Lin 
264868003e6cSMengdong Lin 	if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
264968003e6cSMengdong Lin 		dev_err(component->dev, "Invalid dai type %d\n",
265068003e6cSMengdong Lin 			dai_drv->dobj.type);
265168003e6cSMengdong Lin 		return -EINVAL;
265268003e6cSMengdong Lin 	}
265368003e6cSMengdong Lin 
265468003e6cSMengdong Lin 	lockdep_assert_held(&client_mutex);
265568003e6cSMengdong Lin 	dai = soc_add_dai(component, dai_drv, false);
265668003e6cSMengdong Lin 	if (!dai)
265768003e6cSMengdong Lin 		return -ENOMEM;
265868003e6cSMengdong Lin 
26592c7b696aSMarcel Ziswiler 	/*
26602c7b696aSMarcel Ziswiler 	 * Create the DAI widgets here. After adding DAIs, topology may
266168003e6cSMengdong Lin 	 * also add routes that need these widgets as source or sink.
266268003e6cSMengdong Lin 	 */
266368003e6cSMengdong Lin 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
266468003e6cSMengdong Lin 	if (ret != 0) {
266568003e6cSMengdong Lin 		dev_err(component->dev,
266668003e6cSMengdong Lin 			"Failed to create DAI widgets %d\n", ret);
266768003e6cSMengdong Lin 	}
266868003e6cSMengdong Lin 
266968003e6cSMengdong Lin 	return ret;
267068003e6cSMengdong Lin }
267168003e6cSMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_register_dai);
267268003e6cSMengdong Lin 
2673bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
2674bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
2675d191bd8dSKuninori Morimoto {
26768d92bb51SKuninori Morimoto 	INIT_LIST_HEAD(&component->dai_list);
2677495efdb0SKuninori Morimoto 	INIT_LIST_HEAD(&component->dobj_list);
2678495efdb0SKuninori Morimoto 	INIT_LIST_HEAD(&component->card_list);
26798d92bb51SKuninori Morimoto 	mutex_init(&component->io_mutex);
26808d92bb51SKuninori Morimoto 
2681bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
2682bb13109dSLars-Peter Clausen 	if (!component->name) {
2683bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
2684d191bd8dSKuninori Morimoto 		return -ENOMEM;
2685d191bd8dSKuninori Morimoto 	}
2686d191bd8dSKuninori Morimoto 
2687bb13109dSLars-Peter Clausen 	component->dev = dev;
2688bb13109dSLars-Peter Clausen 	component->driver = driver;
2689e2c330b9SLars-Peter Clausen 
2690bb13109dSLars-Peter Clausen 	return 0;
2691d191bd8dSKuninori Morimoto }
2692d191bd8dSKuninori Morimoto 
269320feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2694886f5692SLars-Peter Clausen {
2695886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
269620feb881SLars-Peter Clausen 
2697886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
2698886f5692SLars-Peter Clausen 	if (val_bytes > 0)
2699886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
2700886f5692SLars-Peter Clausen }
270120feb881SLars-Peter Clausen 
2702e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
2703e874bf5fSLars-Peter Clausen 
270420feb881SLars-Peter Clausen /**
27052c7b696aSMarcel Ziswiler  * snd_soc_component_init_regmap() - Initialize regmap instance for the
27062c7b696aSMarcel Ziswiler  *                                   component
270720feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
270820feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
270920feb881SLars-Peter Clausen  *
271020feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
271120feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
271220feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
271320feb881SLars-Peter Clausen  * before the first IO attempt of the component.
271420feb881SLars-Peter Clausen  */
271520feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
271620feb881SLars-Peter Clausen 	struct regmap *regmap)
271720feb881SLars-Peter Clausen {
271820feb881SLars-Peter Clausen 	component->regmap = regmap;
271920feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
2720886f5692SLars-Peter Clausen }
272120feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
272220feb881SLars-Peter Clausen 
272320feb881SLars-Peter Clausen /**
27242c7b696aSMarcel Ziswiler  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
27252c7b696aSMarcel Ziswiler  *                                   component
272620feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
272720feb881SLars-Peter Clausen  *
272820feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
272920feb881SLars-Peter Clausen  * removes the regmap instance from the component.
273020feb881SLars-Peter Clausen  *
273120feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
273220feb881SLars-Peter Clausen  * to initialize the regmap instance.
273320feb881SLars-Peter Clausen  */
273420feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
273520feb881SLars-Peter Clausen {
273620feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
273720feb881SLars-Peter Clausen 	component->regmap = NULL;
273820feb881SLars-Peter Clausen }
273920feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2740886f5692SLars-Peter Clausen 
2741e874bf5fSLars-Peter Clausen #endif
2742d191bd8dSKuninori Morimoto 
2743359c71eeSKuninori Morimoto static void snd_soc_component_add(struct snd_soc_component *component)
2744bb13109dSLars-Peter Clausen {
2745359c71eeSKuninori Morimoto 	mutex_lock(&client_mutex);
2746359c71eeSKuninori Morimoto 
2747999f7f5aSKuninori Morimoto 	if (!component->driver->write && !component->driver->read) {
274820feb881SLars-Peter Clausen 		if (!component->regmap)
27492c7b696aSMarcel Ziswiler 			component->regmap = dev_get_regmap(component->dev,
27502c7b696aSMarcel Ziswiler 							   NULL);
275120feb881SLars-Peter Clausen 		if (component->regmap)
275220feb881SLars-Peter Clausen 			snd_soc_component_setup_regmap(component);
275320feb881SLars-Peter Clausen 	}
2754886f5692SLars-Peter Clausen 
2755368dee94SKuninori Morimoto 	/* see for_each_component */
2756bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
2757d191bd8dSKuninori Morimoto 
2758d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
2759bb13109dSLars-Peter Clausen }
2760d191bd8dSKuninori Morimoto 
2761bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
2762bb13109dSLars-Peter Clausen {
2763bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
2764bb13109dSLars-Peter Clausen }
2765d191bd8dSKuninori Morimoto 
2766bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2767bb13109dSLars-Peter Clausen {
2768c12c1aadSKuninori Morimoto 	struct snd_soc_card *card = component->card;
2769c12c1aadSKuninori Morimoto 
2770c12c1aadSKuninori Morimoto 	if (card)
2771e894efefSSrinivas Kandagatla 		snd_soc_unbind_card(card, false);
2772c12c1aadSKuninori Morimoto 
2773bb13109dSLars-Peter Clausen 	list_del(&component->list);
2774bb13109dSLars-Peter Clausen }
2775d191bd8dSKuninori Morimoto 
2776273d778eSKuninori Morimoto #define ENDIANNESS_MAP(name) \
2777273d778eSKuninori Morimoto 	(SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2778273d778eSKuninori Morimoto static u64 endianness_format_map[] = {
2779273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S16_),
2780273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U16_),
2781273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_),
2782273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_),
2783273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S32_),
2784273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U32_),
2785273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_3),
2786273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_3),
2787273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S20_3),
2788273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U20_3),
2789273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S18_3),
2790273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U18_3),
2791273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT_),
2792273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT64_),
2793273d778eSKuninori Morimoto 	ENDIANNESS_MAP(IEC958_SUBFRAME_),
2794273d778eSKuninori Morimoto };
2795273d778eSKuninori Morimoto 
2796273d778eSKuninori Morimoto /*
2797273d778eSKuninori Morimoto  * Fix up the DAI formats for endianness: codecs don't actually see
2798273d778eSKuninori Morimoto  * the endianness of the data but we're using the CPU format
2799273d778eSKuninori Morimoto  * definitions which do need to include endianness so we ensure that
2800273d778eSKuninori Morimoto  * codec DAIs always have both big and little endian variants set.
2801273d778eSKuninori Morimoto  */
2802273d778eSKuninori Morimoto static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2803273d778eSKuninori Morimoto {
2804273d778eSKuninori Morimoto 	int i;
2805273d778eSKuninori Morimoto 
2806273d778eSKuninori Morimoto 	for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2807273d778eSKuninori Morimoto 		if (stream->formats & endianness_format_map[i])
2808273d778eSKuninori Morimoto 			stream->formats |= endianness_format_map[i];
2809273d778eSKuninori Morimoto }
2810273d778eSKuninori Morimoto 
2811e894efefSSrinivas Kandagatla static void snd_soc_try_rebind_card(void)
2812e894efefSSrinivas Kandagatla {
2813e894efefSSrinivas Kandagatla 	struct snd_soc_card *card, *c;
2814e894efefSSrinivas Kandagatla 
2815b245d273SKuninori Morimoto 	list_for_each_entry_safe(card, c, &unbind_card_list, list)
2816e894efefSSrinivas Kandagatla 		if (!snd_soc_bind_card(card))
2817e894efefSSrinivas Kandagatla 			list_del(&card->list);
2818e894efefSSrinivas Kandagatla }
2819e894efefSSrinivas Kandagatla 
2820e0dac41bSKuninori Morimoto int snd_soc_add_component(struct device *dev,
2821e0dac41bSKuninori Morimoto 			struct snd_soc_component *component,
2822cf9e829eSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2823d191bd8dSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2824d191bd8dSKuninori Morimoto 			int num_dai)
2825d191bd8dSKuninori Morimoto {
2826bb13109dSLars-Peter Clausen 	int ret;
2827273d778eSKuninori Morimoto 	int i;
2828d191bd8dSKuninori Morimoto 
2829cf9e829eSKuninori Morimoto 	ret = snd_soc_component_initialize(component, component_driver, dev);
2830bb13109dSLars-Peter Clausen 	if (ret)
2831bb13109dSLars-Peter Clausen 		goto err_free;
2832bb13109dSLars-Peter Clausen 
2833273d778eSKuninori Morimoto 	if (component_driver->endianness) {
2834273d778eSKuninori Morimoto 		for (i = 0; i < num_dai; i++) {
2835273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].playback);
2836273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].capture);
2837273d778eSKuninori Morimoto 		}
2838273d778eSKuninori Morimoto 	}
2839273d778eSKuninori Morimoto 
28400e7b25c6SKuninori Morimoto 	ret = snd_soc_register_dais(component, dai_drv, num_dai);
2841bb13109dSLars-Peter Clausen 	if (ret < 0) {
2842f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
2843bb13109dSLars-Peter Clausen 		goto err_cleanup;
2844bb13109dSLars-Peter Clausen 	}
2845bb13109dSLars-Peter Clausen 
2846cf9e829eSKuninori Morimoto 	snd_soc_component_add(component);
2847e894efefSSrinivas Kandagatla 	snd_soc_try_rebind_card();
2848bb13109dSLars-Peter Clausen 
2849bb13109dSLars-Peter Clausen 	return 0;
2850bb13109dSLars-Peter Clausen 
2851bb13109dSLars-Peter Clausen err_cleanup:
2852cf9e829eSKuninori Morimoto 	snd_soc_component_cleanup(component);
2853bb13109dSLars-Peter Clausen err_free:
2854bb13109dSLars-Peter Clausen 	return ret;
2855d191bd8dSKuninori Morimoto }
2856e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component);
2857e0dac41bSKuninori Morimoto 
2858e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev,
2859e0dac41bSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2860e0dac41bSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2861e0dac41bSKuninori Morimoto 			int num_dai)
2862e0dac41bSKuninori Morimoto {
2863e0dac41bSKuninori Morimoto 	struct snd_soc_component *component;
2864e0dac41bSKuninori Morimoto 
28657ecbd6a9SKuninori Morimoto 	component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
286608e61d03SKuninori Morimoto 	if (!component)
2867e0dac41bSKuninori Morimoto 		return -ENOMEM;
2868e0dac41bSKuninori Morimoto 
2869e0dac41bSKuninori Morimoto 	return snd_soc_add_component(dev, component, component_driver,
2870e0dac41bSKuninori Morimoto 				     dai_drv, num_dai);
2871e0dac41bSKuninori Morimoto }
2872d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
2873d191bd8dSKuninori Morimoto 
2874d191bd8dSKuninori Morimoto /**
28752eccea8cSKuninori Morimoto  * snd_soc_unregister_component - Unregister all related component
28762eccea8cSKuninori Morimoto  * from the ASoC core
2877d191bd8dSKuninori Morimoto  *
2878628536eaSJonathan Corbet  * @dev: The device to unregister
2879d191bd8dSKuninori Morimoto  */
28802eccea8cSKuninori Morimoto static int __snd_soc_unregister_component(struct device *dev)
2881d191bd8dSKuninori Morimoto {
2882cf9e829eSKuninori Morimoto 	struct snd_soc_component *component;
288321a03528SKuninori Morimoto 	int found = 0;
2884d191bd8dSKuninori Morimoto 
288534e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
2886368dee94SKuninori Morimoto 	for_each_component(component) {
2887999f7f5aSKuninori Morimoto 		if (dev != component->dev)
288821a03528SKuninori Morimoto 			continue;
2889d191bd8dSKuninori Morimoto 
28902c7b696aSMarcel Ziswiler 		snd_soc_tplg_component_remove(component,
28912c7b696aSMarcel Ziswiler 					      SND_SOC_TPLG_INDEX_ALL);
2892cf9e829eSKuninori Morimoto 		snd_soc_component_del_unlocked(component);
289321a03528SKuninori Morimoto 		found = 1;
289421a03528SKuninori Morimoto 		break;
2895d191bd8dSKuninori Morimoto 	}
2896d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
2897d191bd8dSKuninori Morimoto 
28982c7b696aSMarcel Ziswiler 	if (found)
2899cf9e829eSKuninori Morimoto 		snd_soc_component_cleanup(component);
29002eccea8cSKuninori Morimoto 
29012eccea8cSKuninori Morimoto 	return found;
29022eccea8cSKuninori Morimoto }
29032eccea8cSKuninori Morimoto 
29042eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
29052eccea8cSKuninori Morimoto {
29062c7b696aSMarcel Ziswiler 	while (__snd_soc_unregister_component(dev))
29072c7b696aSMarcel Ziswiler 		;
2908d191bd8dSKuninori Morimoto }
2909d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2910d191bd8dSKuninori Morimoto 
29117dd5d0d9SKuninori Morimoto struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
29127dd5d0d9SKuninori Morimoto 						   const char *driver_name)
29137dd5d0d9SKuninori Morimoto {
29147dd5d0d9SKuninori Morimoto 	struct snd_soc_component *component;
29157dd5d0d9SKuninori Morimoto 	struct snd_soc_component *ret;
29167dd5d0d9SKuninori Morimoto 
29177dd5d0d9SKuninori Morimoto 	ret = NULL;
29187dd5d0d9SKuninori Morimoto 	mutex_lock(&client_mutex);
2919368dee94SKuninori Morimoto 	for_each_component(component) {
29207dd5d0d9SKuninori Morimoto 		if (dev != component->dev)
29217dd5d0d9SKuninori Morimoto 			continue;
29227dd5d0d9SKuninori Morimoto 
29237dd5d0d9SKuninori Morimoto 		if (driver_name &&
29247dd5d0d9SKuninori Morimoto 		    (driver_name != component->driver->name) &&
29257dd5d0d9SKuninori Morimoto 		    (strcmp(component->driver->name, driver_name) != 0))
29267dd5d0d9SKuninori Morimoto 			continue;
29277dd5d0d9SKuninori Morimoto 
29287dd5d0d9SKuninori Morimoto 		ret = component;
29297dd5d0d9SKuninori Morimoto 		break;
29307dd5d0d9SKuninori Morimoto 	}
29317dd5d0d9SKuninori Morimoto 	mutex_unlock(&client_mutex);
29327dd5d0d9SKuninori Morimoto 
29337dd5d0d9SKuninori Morimoto 	return ret;
29347dd5d0d9SKuninori Morimoto }
29357dd5d0d9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
29367dd5d0d9SKuninori Morimoto 
2937bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
2938b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2939bec4fa05SStephen Warren 			       const char *propname)
2940bec4fa05SStephen Warren {
2941b07609ceSKuninori Morimoto 	struct device_node *np;
2942bec4fa05SStephen Warren 	int ret;
2943bec4fa05SStephen Warren 
29447e07e7c0STushar Behera 	if (!card->dev) {
29457e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
29467e07e7c0STushar Behera 		return -EINVAL;
29477e07e7c0STushar Behera 	}
29487e07e7c0STushar Behera 
29497e07e7c0STushar Behera 	np = card->dev->of_node;
29507e07e7c0STushar Behera 
2951bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
2952bec4fa05SStephen Warren 	/*
2953bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
2954bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
2955bec4fa05SStephen Warren 	 * snd_soc_register_card.
2956bec4fa05SStephen Warren 	 */
2957bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
2958bec4fa05SStephen Warren 		dev_err(card->dev,
2959f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
2960bec4fa05SStephen Warren 			propname, ret);
2961bec4fa05SStephen Warren 		return ret;
2962bec4fa05SStephen Warren 	}
2963bec4fa05SStephen Warren 
2964bec4fa05SStephen Warren 	return 0;
2965bec4fa05SStephen Warren }
2966b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2967bec4fa05SStephen Warren 
29689a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
29699a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
29709a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
29719a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
29729a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
29739a6d4860SXiubo Li };
29749a6d4860SXiubo Li 
297521efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
29769a6d4860SXiubo Li 					  const char *propname)
29779a6d4860SXiubo Li {
297821efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
29799a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
29809a6d4860SXiubo Li 	const char *template, *wname;
29819a6d4860SXiubo Li 	int i, j, num_widgets, ret;
29829a6d4860SXiubo Li 
29839a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
29849a6d4860SXiubo Li 	if (num_widgets < 0) {
29859a6d4860SXiubo Li 		dev_err(card->dev,
29869a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
29879a6d4860SXiubo Li 		return -EINVAL;
29889a6d4860SXiubo Li 	}
29899a6d4860SXiubo Li 	if (num_widgets & 1) {
29909a6d4860SXiubo Li 		dev_err(card->dev,
29919a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
29929a6d4860SXiubo Li 		return -EINVAL;
29939a6d4860SXiubo Li 	}
29949a6d4860SXiubo Li 
29959a6d4860SXiubo Li 	num_widgets /= 2;
29969a6d4860SXiubo Li 	if (!num_widgets) {
29979a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
29989a6d4860SXiubo Li 			propname);
29999a6d4860SXiubo Li 		return -EINVAL;
30009a6d4860SXiubo Li 	}
30019a6d4860SXiubo Li 
30029a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
30039a6d4860SXiubo Li 			       GFP_KERNEL);
30049a6d4860SXiubo Li 	if (!widgets) {
30059a6d4860SXiubo Li 		dev_err(card->dev,
30069a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
30079a6d4860SXiubo Li 		return -ENOMEM;
30089a6d4860SXiubo Li 	}
30099a6d4860SXiubo Li 
30109a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
30119a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
30129a6d4860SXiubo Li 			2 * i, &template);
30139a6d4860SXiubo Li 		if (ret) {
30149a6d4860SXiubo Li 			dev_err(card->dev,
30159a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
30169a6d4860SXiubo Li 				propname, 2 * i, ret);
30179a6d4860SXiubo Li 			return -EINVAL;
30189a6d4860SXiubo Li 		}
30199a6d4860SXiubo Li 
30209a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
30219a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
30229a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
30239a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
30249a6d4860SXiubo Li 				break;
30259a6d4860SXiubo Li 			}
30269a6d4860SXiubo Li 		}
30279a6d4860SXiubo Li 
30289a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
30299a6d4860SXiubo Li 			dev_err(card->dev,
30309a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
30319a6d4860SXiubo Li 				template);
30329a6d4860SXiubo Li 			return -EINVAL;
30339a6d4860SXiubo Li 		}
30349a6d4860SXiubo Li 
30359a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
30369a6d4860SXiubo Li 						    (2 * i) + 1,
30379a6d4860SXiubo Li 						    &wname);
30389a6d4860SXiubo Li 		if (ret) {
30399a6d4860SXiubo Li 			dev_err(card->dev,
30409a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
30419a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
30429a6d4860SXiubo Li 			return -EINVAL;
30439a6d4860SXiubo Li 		}
30449a6d4860SXiubo Li 
30459a6d4860SXiubo Li 		widgets[i].name = wname;
30469a6d4860SXiubo Li 	}
30479a6d4860SXiubo Li 
3048f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
3049f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
30509a6d4860SXiubo Li 
30519a6d4860SXiubo Li 	return 0;
30529a6d4860SXiubo Li }
305321efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
30549a6d4860SXiubo Li 
3055cbdfab3bSJerome Brunet int snd_soc_of_get_slot_mask(struct device_node *np,
30566131084aSJyri Sarha 			     const char *prop_name,
30576131084aSJyri Sarha 			     unsigned int *mask)
30586131084aSJyri Sarha {
30596131084aSJyri Sarha 	u32 val;
30606c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
30616131084aSJyri Sarha 	int i;
30626131084aSJyri Sarha 
30636131084aSJyri Sarha 	if (!of_slot_mask)
30646131084aSJyri Sarha 		return 0;
30656131084aSJyri Sarha 	val /= sizeof(u32);
30666131084aSJyri Sarha 	for (i = 0; i < val; i++)
30676131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
30686131084aSJyri Sarha 			*mask |= (1 << i);
30696131084aSJyri Sarha 
30706131084aSJyri Sarha 	return val;
30716131084aSJyri Sarha }
3072cbdfab3bSJerome Brunet EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
30736131084aSJyri Sarha 
307489c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
30756131084aSJyri Sarha 			      unsigned int *tx_mask,
30766131084aSJyri Sarha 			      unsigned int *rx_mask,
307789c67857SXiubo Li 			      unsigned int *slots,
307889c67857SXiubo Li 			      unsigned int *slot_width)
307989c67857SXiubo Li {
308089c67857SXiubo Li 	u32 val;
308189c67857SXiubo Li 	int ret;
308289c67857SXiubo Li 
30836131084aSJyri Sarha 	if (tx_mask)
30846131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
30856131084aSJyri Sarha 	if (rx_mask)
30866131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
30876131084aSJyri Sarha 
308889c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
308989c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
309089c67857SXiubo Li 		if (ret)
309189c67857SXiubo Li 			return ret;
309289c67857SXiubo Li 
309389c67857SXiubo Li 		if (slots)
309489c67857SXiubo Li 			*slots = val;
309589c67857SXiubo Li 	}
309689c67857SXiubo Li 
309789c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
309889c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
309989c67857SXiubo Li 		if (ret)
310089c67857SXiubo Li 			return ret;
310189c67857SXiubo Li 
310289c67857SXiubo Li 		if (slot_width)
310389c67857SXiubo Li 			*slot_width = val;
310489c67857SXiubo Li 	}
310589c67857SXiubo Li 
310689c67857SXiubo Li 	return 0;
310789c67857SXiubo Li }
310889c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
310989c67857SXiubo Li 
31103b710356SKuninori Morimoto void snd_soc_of_parse_node_prefix(struct device_node *np,
31115e3cdaa2SKuninori Morimoto 				  struct snd_soc_codec_conf *codec_conf,
31125e3cdaa2SKuninori Morimoto 				  struct device_node *of_node,
31135e3cdaa2SKuninori Morimoto 				  const char *propname)
31145e3cdaa2SKuninori Morimoto {
31155e3cdaa2SKuninori Morimoto 	const char *str;
31165e3cdaa2SKuninori Morimoto 	int ret;
31175e3cdaa2SKuninori Morimoto 
31185e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
31195e3cdaa2SKuninori Morimoto 	if (ret < 0) {
31205e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
31215e3cdaa2SKuninori Morimoto 		return;
31225e3cdaa2SKuninori Morimoto 	}
31235e3cdaa2SKuninori Morimoto 
31245e3cdaa2SKuninori Morimoto 	codec_conf->of_node	= of_node;
31255e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
31265e3cdaa2SKuninori Morimoto }
31273b710356SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
31285e3cdaa2SKuninori Morimoto 
31292bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3130a4a54dd5SStephen Warren 				   const char *propname)
3131a4a54dd5SStephen Warren {
31322bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
3133e3b1e6a1SMark Brown 	int num_routes;
3134a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3135a4a54dd5SStephen Warren 	int i, ret;
3136a4a54dd5SStephen Warren 
3137a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3138c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
313910e8aa9aSMichał Mirosław 		dev_err(card->dev,
314010e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
314110e8aa9aSMichał Mirosław 			propname);
3142a4a54dd5SStephen Warren 		return -EINVAL;
3143a4a54dd5SStephen Warren 	}
3144a4a54dd5SStephen Warren 	num_routes /= 2;
3145a4a54dd5SStephen Warren 	if (!num_routes) {
3146f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3147a4a54dd5SStephen Warren 			propname);
3148a4a54dd5SStephen Warren 		return -EINVAL;
3149a4a54dd5SStephen Warren 	}
3150a4a54dd5SStephen Warren 
3151a86854d0SKees Cook 	routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
3152a4a54dd5SStephen Warren 			      GFP_KERNEL);
3153a4a54dd5SStephen Warren 	if (!routes) {
3154a4a54dd5SStephen Warren 		dev_err(card->dev,
3155f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3156a4a54dd5SStephen Warren 		return -EINVAL;
3157a4a54dd5SStephen Warren 	}
3158a4a54dd5SStephen Warren 
3159a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3160a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3161e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
3162a4a54dd5SStephen Warren 		if (ret) {
3163c871bd0bSMark Brown 			dev_err(card->dev,
3164c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3165c871bd0bSMark Brown 				propname, 2 * i, ret);
3166a4a54dd5SStephen Warren 			return -EINVAL;
3167a4a54dd5SStephen Warren 		}
3168a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3169e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3170a4a54dd5SStephen Warren 		if (ret) {
3171a4a54dd5SStephen Warren 			dev_err(card->dev,
3172c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3173c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3174a4a54dd5SStephen Warren 			return -EINVAL;
3175a4a54dd5SStephen Warren 		}
3176a4a54dd5SStephen Warren 	}
3177a4a54dd5SStephen Warren 
3178f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
3179f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
3180a4a54dd5SStephen Warren 
3181a4a54dd5SStephen Warren 	return 0;
3182a4a54dd5SStephen Warren }
31832bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3184a4a54dd5SStephen Warren 
3185a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3186389cb834SJyri Sarha 				     const char *prefix,
3187389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3188389cb834SJyri Sarha 				     struct device_node **framemaster)
3189a7930ed4SKuninori Morimoto {
3190a7930ed4SKuninori Morimoto 	int ret, i;
3191a7930ed4SKuninori Morimoto 	char prop[128];
3192a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3193a7930ed4SKuninori Morimoto 	int bit, frame;
3194a7930ed4SKuninori Morimoto 	const char *str;
3195a7930ed4SKuninori Morimoto 	struct {
3196a7930ed4SKuninori Morimoto 		char *name;
3197a7930ed4SKuninori Morimoto 		unsigned int val;
3198a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3199a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3200a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3201a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3202a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3203a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3204a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3205a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3206a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3207a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3208a7930ed4SKuninori Morimoto 	};
3209a7930ed4SKuninori Morimoto 
3210a7930ed4SKuninori Morimoto 	if (!prefix)
3211a7930ed4SKuninori Morimoto 		prefix = "";
3212a7930ed4SKuninori Morimoto 
3213a7930ed4SKuninori Morimoto 	/*
32145711c979SKuninori Morimoto 	 * check "dai-format = xxx"
32155711c979SKuninori Morimoto 	 * or    "[prefix]format = xxx"
3216a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3217a7930ed4SKuninori Morimoto 	 */
32185711c979SKuninori Morimoto 	ret = of_property_read_string(np, "dai-format", &str);
32195711c979SKuninori Morimoto 	if (ret < 0) {
3220a7930ed4SKuninori Morimoto 		snprintf(prop, sizeof(prop), "%sformat", prefix);
3221a7930ed4SKuninori Morimoto 		ret = of_property_read_string(np, prop, &str);
32225711c979SKuninori Morimoto 	}
3223a7930ed4SKuninori Morimoto 	if (ret == 0) {
3224a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3225a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3226a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3227a7930ed4SKuninori Morimoto 				break;
3228a7930ed4SKuninori Morimoto 			}
3229a7930ed4SKuninori Morimoto 		}
3230a7930ed4SKuninori Morimoto 	}
3231a7930ed4SKuninori Morimoto 
3232a7930ed4SKuninori Morimoto 	/*
32338c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3234a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3235a7930ed4SKuninori Morimoto 	 */
32368c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
323751930295SJulia Lawall 	if (of_property_read_bool(np, prop))
32388c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
32398c2d6a9fSKuninori Morimoto 	else
32408c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3241a7930ed4SKuninori Morimoto 
3242a7930ed4SKuninori Morimoto 	/*
3243a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3244a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3245a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3246a7930ed4SKuninori Morimoto 	 */
3247a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3248a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3249a7930ed4SKuninori Morimoto 
3250a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3251a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3252a7930ed4SKuninori Morimoto 
3253a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3254a7930ed4SKuninori Morimoto 	case 0x11:
3255a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3256a7930ed4SKuninori Morimoto 		break;
3257a7930ed4SKuninori Morimoto 	case 0x10:
3258a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3259a7930ed4SKuninori Morimoto 		break;
3260a7930ed4SKuninori Morimoto 	case 0x01:
3261a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3262a7930ed4SKuninori Morimoto 		break;
3263a7930ed4SKuninori Morimoto 	default:
3264a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3265a7930ed4SKuninori Morimoto 		break;
3266a7930ed4SKuninori Morimoto 	}
3267a7930ed4SKuninori Morimoto 
3268a7930ed4SKuninori Morimoto 	/*
3269a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3270a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3271a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3272a7930ed4SKuninori Morimoto 	 */
3273a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3274a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3275389cb834SJyri Sarha 	if (bit && bitclkmaster)
3276389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3277a7930ed4SKuninori Morimoto 
3278a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3279a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3280389cb834SJyri Sarha 	if (frame && framemaster)
3281389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3282a7930ed4SKuninori Morimoto 
3283a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3284a7930ed4SKuninori Morimoto 	case 0x11:
3285a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3286a7930ed4SKuninori Morimoto 		break;
3287a7930ed4SKuninori Morimoto 	case 0x10:
3288a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3289a7930ed4SKuninori Morimoto 		break;
3290a7930ed4SKuninori Morimoto 	case 0x01:
3291a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3292a7930ed4SKuninori Morimoto 		break;
3293a7930ed4SKuninori Morimoto 	default:
3294a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3295a7930ed4SKuninori Morimoto 		break;
3296a7930ed4SKuninori Morimoto 	}
3297a7930ed4SKuninori Morimoto 
3298a7930ed4SKuninori Morimoto 	return format;
3299a7930ed4SKuninori Morimoto }
3300a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3301a7930ed4SKuninori Morimoto 
3302a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
3303a180e8b9SKuninori Morimoto {
330409d4cc03SKuninori Morimoto 	struct snd_soc_component *component;
3305c1e230f0SKuninori Morimoto 	struct snd_soc_dai_link_component dlc;
3306a180e8b9SKuninori Morimoto 	int ret;
3307a180e8b9SKuninori Morimoto 
3308c1e230f0SKuninori Morimoto 	dlc.of_node	= of_graph_get_port_parent(ep);
3309c1e230f0SKuninori Morimoto 	dlc.name	= NULL;
3310a180e8b9SKuninori Morimoto 	/*
3311a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
3312a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
3313a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
3314a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
3315a180e8b9SKuninori Morimoto 	 */
3316a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
3317a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
3318c1e230f0SKuninori Morimoto 	component = soc_find_component(&dlc);
33192c7b1704SKuninori Morimoto 	if (component)
33202c7b1704SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_id(component, ep);
3321a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
3322a180e8b9SKuninori Morimoto 
3323c1e230f0SKuninori Morimoto 	of_node_put(dlc.of_node);
3324c0a480d1STony Lindgren 
3325a180e8b9SKuninori Morimoto 	return ret;
3326a180e8b9SKuninori Morimoto }
3327a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3328a180e8b9SKuninori Morimoto 
33291ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args,
3330cb470087SKuninori Morimoto 				const char **dai_name)
3331cb470087SKuninori Morimoto {
3332cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
33333e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
333493b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3335cb470087SKuninori Morimoto 
3336cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3337368dee94SKuninori Morimoto 	for_each_component(pos) {
3338c0834440SKuninori Morimoto 		component_of_node = soc_component_to_node(pos);
33393e0aa8d8SJyri Sarha 
33403e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
3341cb470087SKuninori Morimoto 			continue;
3342cb470087SKuninori Morimoto 
3343a2a34175SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3344a2a34175SKuninori Morimoto 		if (ret == -ENOTSUPP) {
334558bf4179SKuninori Morimoto 			struct snd_soc_dai *dai;
33466833c452SKuninori Morimoto 			int id = -1;
33476833c452SKuninori Morimoto 
334893b0f3eeSJean-Francois Moine 			switch (args->args_count) {
33496833c452SKuninori Morimoto 			case 0:
33506833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
33516833c452SKuninori Morimoto 				break;
33526833c452SKuninori Morimoto 			case 1:
335393b0f3eeSJean-Francois Moine 				id = args->args[0];
33546833c452SKuninori Morimoto 				break;
33556833c452SKuninori Morimoto 			default:
33566833c452SKuninori Morimoto 				/* not supported */
3357cb470087SKuninori Morimoto 				break;
3358cb470087SKuninori Morimoto 			}
3359cb470087SKuninori Morimoto 
33606833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
33616833c452SKuninori Morimoto 				ret = -EINVAL;
33623dcba280SNicolin Chen 				continue;
33636833c452SKuninori Morimoto 			}
3364e41975edSXiubo Li 
3365e41975edSXiubo Li 			ret = 0;
3366e41975edSXiubo Li 
336758bf4179SKuninori Morimoto 			/* find target DAI */
336815a0c645SKuninori Morimoto 			for_each_component_dais(pos, dai) {
336958bf4179SKuninori Morimoto 				if (id == 0)
337058bf4179SKuninori Morimoto 					break;
337158bf4179SKuninori Morimoto 				id--;
337258bf4179SKuninori Morimoto 			}
337358bf4179SKuninori Morimoto 
337458bf4179SKuninori Morimoto 			*dai_name = dai->driver->name;
3375e41975edSXiubo Li 			if (!*dai_name)
3376e41975edSXiubo Li 				*dai_name = pos->name;
33776833c452SKuninori Morimoto 		}
33786833c452SKuninori Morimoto 
3379cb470087SKuninori Morimoto 		break;
3380cb470087SKuninori Morimoto 	}
3381cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
338293b0f3eeSJean-Francois Moine 	return ret;
338393b0f3eeSJean-Francois Moine }
33841ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
338593b0f3eeSJean-Francois Moine 
338693b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
338793b0f3eeSJean-Francois Moine 			    const char **dai_name)
338893b0f3eeSJean-Francois Moine {
338993b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
339093b0f3eeSJean-Francois Moine 	int ret;
339193b0f3eeSJean-Francois Moine 
339293b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
339393b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
339493b0f3eeSJean-Francois Moine 	if (ret)
339593b0f3eeSJean-Francois Moine 		return ret;
339693b0f3eeSJean-Francois Moine 
339793b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3398cb470087SKuninori Morimoto 
3399cb470087SKuninori Morimoto 	of_node_put(args.np);
3400cb470087SKuninori Morimoto 
3401cb470087SKuninori Morimoto 	return ret;
3402cb470087SKuninori Morimoto }
3403cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3404cb470087SKuninori Morimoto 
340593b0f3eeSJean-Francois Moine /*
340694685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
340794685763SSylwester Nawrocki  * @dai_link: DAI link
340894685763SSylwester Nawrocki  *
340994685763SSylwester Nawrocki  * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
341094685763SSylwester Nawrocki  */
341194685763SSylwester Nawrocki void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
341294685763SSylwester Nawrocki {
34133db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *component;
341494685763SSylwester Nawrocki 	int index;
341594685763SSylwester Nawrocki 
34163db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
341794685763SSylwester Nawrocki 		if (!component->of_node)
341894685763SSylwester Nawrocki 			break;
341994685763SSylwester Nawrocki 		of_node_put(component->of_node);
342094685763SSylwester Nawrocki 		component->of_node = NULL;
342194685763SSylwester Nawrocki 	}
342294685763SSylwester Nawrocki }
342394685763SSylwester Nawrocki EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
342494685763SSylwester Nawrocki 
342594685763SSylwester Nawrocki /*
342693b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
342793b0f3eeSJean-Francois Moine  * @dev: Card device
342893b0f3eeSJean-Francois Moine  * @of_node: Device node
342993b0f3eeSJean-Francois Moine  * @dai_link: DAI link
343093b0f3eeSJean-Francois Moine  *
343193b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
343293b0f3eeSJean-Francois Moine  * 'sound-dai'.
343393b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
343494685763SSylwester Nawrocki  * The device nodes in the array (of_node) must be dereferenced by calling
343594685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs() on @dai_link.
343693b0f3eeSJean-Francois Moine  *
343793b0f3eeSJean-Francois Moine  * Returns 0 for success
343893b0f3eeSJean-Francois Moine  */
343993b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
344093b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
344193b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
344293b0f3eeSJean-Francois Moine {
344393b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
344493b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
344593b0f3eeSJean-Francois Moine 	char *name;
344693b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
344793b0f3eeSJean-Francois Moine 
344893b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
344993b0f3eeSJean-Francois Moine 	name = "sound-dai";
345093b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
345193b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
345293b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
345393b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
345493b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
345593b0f3eeSJean-Francois Moine 		else
345693b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
345793b0f3eeSJean-Francois Moine 		return num_codecs;
345893b0f3eeSJean-Francois Moine 	}
3459a86854d0SKees Cook 	component = devm_kcalloc(dev,
3460a86854d0SKees Cook 				 num_codecs, sizeof(*component),
346193b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
346293b0f3eeSJean-Francois Moine 	if (!component)
346393b0f3eeSJean-Francois Moine 		return -ENOMEM;
346493b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
346593b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
346693b0f3eeSJean-Francois Moine 
346793b0f3eeSJean-Francois Moine 	/* Parse the list */
34683db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
346993b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
347093b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
347193b0f3eeSJean-Francois Moine 						 index, &args);
347293b0f3eeSJean-Francois Moine 		if (ret)
347393b0f3eeSJean-Francois Moine 			goto err;
347493b0f3eeSJean-Francois Moine 		component->of_node = args.np;
347593b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
347693b0f3eeSJean-Francois Moine 		if (ret < 0)
347793b0f3eeSJean-Francois Moine 			goto err;
347893b0f3eeSJean-Francois Moine 	}
347993b0f3eeSJean-Francois Moine 	return 0;
348093b0f3eeSJean-Francois Moine err:
348194685763SSylwester Nawrocki 	snd_soc_of_put_dai_link_codecs(dai_link);
348293b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
348393b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
348493b0f3eeSJean-Francois Moine 	return ret;
348593b0f3eeSJean-Francois Moine }
348693b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
348793b0f3eeSJean-Francois Moine 
3488c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3489db2a4165SFrank Mandarino {
34906553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
3491fb257897SMark Brown 	snd_soc_util_init();
3492fb257897SMark Brown 
3493db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3494db2a4165SFrank Mandarino }
34954abe8e16SMark Brown module_init(snd_soc_init);
3496db2a4165SFrank Mandarino 
34977d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3498db2a4165SFrank Mandarino {
3499fb257897SMark Brown 	snd_soc_util_exit();
35006553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3501fb257897SMark Brown 
3502db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3503db2a4165SFrank Mandarino }
3504db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3505db2a4165SFrank Mandarino 
3506db2a4165SFrank Mandarino /* Module information */
3507d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3508db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3509db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
35108b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3511