xref: /openbmc/linux/sound/soc/soc-core.c (revision 8ec241c495dde3d19a0459304298c2468c60182b)
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 
280*8ec241c4SKuninori Morimoto /*
281*8ec241c4SKuninori Morimoto  * This is glue code between snd_pcm_lib_ioctl() and
282*8ec241c4SKuninori Morimoto  * snd_soc_component_driver :: ioctl
283*8ec241c4SKuninori Morimoto  */
284*8ec241c4SKuninori Morimoto int snd_soc_pcm_lib_ioctl(struct snd_soc_component *component,
285*8ec241c4SKuninori Morimoto 			  struct snd_pcm_substream *substream,
286*8ec241c4SKuninori Morimoto 			  unsigned int cmd, void *arg)
287*8ec241c4SKuninori Morimoto {
288*8ec241c4SKuninori Morimoto 	return snd_pcm_lib_ioctl(substream, cmd, arg);
289*8ec241c4SKuninori Morimoto }
290*8ec241c4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_pcm_lib_ioctl);
291*8ec241c4SKuninori Morimoto 
292a0ac4411SKuninori Morimoto static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
293a0ac4411SKuninori Morimoto 			      struct snd_soc_component *component)
294a0ac4411SKuninori Morimoto {
295a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
296a0ac4411SKuninori Morimoto 
297a0ac4411SKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
298a0ac4411SKuninori Morimoto 		/* already connected */
299a0ac4411SKuninori Morimoto 		if (rtdcom->component == component)
300a0ac4411SKuninori Morimoto 			return 0;
301a0ac4411SKuninori Morimoto 	}
302a0ac4411SKuninori Morimoto 
303353e16bfSKuninori Morimoto 	/*
304353e16bfSKuninori Morimoto 	 * created rtdcom here will be freed when rtd->dev was freed.
305353e16bfSKuninori Morimoto 	 * see
306353e16bfSKuninori Morimoto 	 *	soc_free_pcm_runtime() :: device_unregister(rtd->dev)
307353e16bfSKuninori Morimoto 	 */
308353e16bfSKuninori Morimoto 	rtdcom = devm_kzalloc(rtd->dev, sizeof(*rtdcom), GFP_KERNEL);
30932d2c172SKuninori Morimoto 	if (!rtdcom)
310a0ac4411SKuninori Morimoto 		return -ENOMEM;
311a0ac4411SKuninori Morimoto 
31232d2c172SKuninori Morimoto 	rtdcom->component = component;
31332d2c172SKuninori Morimoto 	INIT_LIST_HEAD(&rtdcom->list);
314a0ac4411SKuninori Morimoto 
315353e16bfSKuninori Morimoto 	/*
316353e16bfSKuninori Morimoto 	 * When rtd was freed, created rtdcom here will be
317353e16bfSKuninori Morimoto 	 * also freed.
318353e16bfSKuninori Morimoto 	 * And we don't need to call list_del(&rtdcom->list)
319353e16bfSKuninori Morimoto 	 * when freed, because rtd is also freed.
320353e16bfSKuninori Morimoto 	 */
32132d2c172SKuninori Morimoto 	list_add_tail(&rtdcom->list, &rtd->component_list);
322a0ac4411SKuninori Morimoto 
323a0ac4411SKuninori Morimoto 	return 0;
324a0ac4411SKuninori Morimoto }
325a0ac4411SKuninori Morimoto 
326a0ac4411SKuninori Morimoto struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
327a0ac4411SKuninori Morimoto 						const char *driver_name)
328a0ac4411SKuninori Morimoto {
329a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
330a0ac4411SKuninori Morimoto 
331971da24cSKuninori Morimoto 	if (!driver_name)
332971da24cSKuninori Morimoto 		return NULL;
333971da24cSKuninori Morimoto 
334a33c0d16SKuninori Morimoto 	/*
335a33c0d16SKuninori Morimoto 	 * NOTE
336a33c0d16SKuninori Morimoto 	 *
337a33c0d16SKuninori Morimoto 	 * snd_soc_rtdcom_lookup() will find component from rtd by using
338a33c0d16SKuninori Morimoto 	 * specified driver name.
339a33c0d16SKuninori Morimoto 	 * But, if many components which have same driver name are connected
340a33c0d16SKuninori Morimoto 	 * to 1 rtd, this function will return 1st found component.
341a33c0d16SKuninori Morimoto 	 */
342a0ac4411SKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
343971da24cSKuninori Morimoto 		const char *component_name = rtdcom->component->driver->name;
344971da24cSKuninori Morimoto 
345971da24cSKuninori Morimoto 		if (!component_name)
346971da24cSKuninori Morimoto 			continue;
347971da24cSKuninori Morimoto 
348971da24cSKuninori Morimoto 		if ((component_name == driver_name) ||
349971da24cSKuninori Morimoto 		    strcmp(component_name, driver_name) == 0)
350a0ac4411SKuninori Morimoto 			return rtdcom->component;
351a0ac4411SKuninori Morimoto 	}
352a0ac4411SKuninori Morimoto 
353a0ac4411SKuninori Morimoto 	return NULL;
354a0ac4411SKuninori Morimoto }
355031734b7SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
356a0ac4411SKuninori Morimoto 
35747c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
35847c88fffSLiam Girdwood 		const char *dai_link, int stream)
35947c88fffSLiam Girdwood {
3601a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
36147c88fffSLiam Girdwood 
362bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
3631a497983SMengdong Lin 		if (rtd->dai_link->no_pcm &&
3641a497983SMengdong Lin 			!strcmp(rtd->dai_link->name, dai_link))
3651a497983SMengdong Lin 			return rtd->pcm->streams[stream].substream;
36647c88fffSLiam Girdwood 	}
367f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
36847c88fffSLiam Girdwood 	return NULL;
36947c88fffSLiam Girdwood }
37047c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
37147c88fffSLiam Girdwood 
37275ab9eb6SKuninori Morimoto static const struct snd_soc_ops null_snd_soc_ops;
37375ab9eb6SKuninori Morimoto 
3746e864344SKuninori Morimoto static void soc_release_rtd_dev(struct device *dev)
3756e864344SKuninori Morimoto {
3766e864344SKuninori Morimoto 	/* "dev" means "rtd->dev" */
3776e864344SKuninori Morimoto 	kfree(dev);
3786e864344SKuninori Morimoto }
3796e864344SKuninori Morimoto 
3801c93a9e0SKuninori Morimoto static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
3811c93a9e0SKuninori Morimoto {
3826e864344SKuninori Morimoto 	if (!rtd)
3836e864344SKuninori Morimoto 		return;
3846e864344SKuninori Morimoto 
385753ace0aSKuninori Morimoto 	list_del(&rtd->list);
386b7c5bc45SKuninori Morimoto 
387b7c5bc45SKuninori Morimoto 	/*
388b7c5bc45SKuninori Morimoto 	 * we don't need to call kfree() for rtd->dev
389b7c5bc45SKuninori Morimoto 	 * see
390b7c5bc45SKuninori Morimoto 	 *	soc_release_rtd_dev()
391d918a376SKuninori Morimoto 	 *
392d918a376SKuninori Morimoto 	 * We don't need rtd->dev NULL check, because
393d918a376SKuninori Morimoto 	 * it is alloced *before* rtd.
394d918a376SKuninori Morimoto 	 * see
395d918a376SKuninori Morimoto 	 *	soc_new_pcm_runtime()
396b7c5bc45SKuninori Morimoto 	 */
397b7c5bc45SKuninori Morimoto 	device_unregister(rtd->dev);
3981c93a9e0SKuninori Morimoto }
3991c93a9e0SKuninori Morimoto 
4001a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
4011a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
4021a497983SMengdong Lin {
4031a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
404d918a376SKuninori Morimoto 	struct device *dev;
4056e864344SKuninori Morimoto 	int ret;
4061a497983SMengdong Lin 
4076e864344SKuninori Morimoto 	/*
408d918a376SKuninori Morimoto 	 * for rtd->dev
409d918a376SKuninori Morimoto 	 */
410d918a376SKuninori Morimoto 	dev = kzalloc(sizeof(struct device), GFP_KERNEL);
411d918a376SKuninori Morimoto 	if (!dev)
412d918a376SKuninori Morimoto 		return NULL;
413d918a376SKuninori Morimoto 
414d918a376SKuninori Morimoto 	dev->parent	= card->dev;
415d918a376SKuninori Morimoto 	dev->release	= soc_release_rtd_dev;
416d918a376SKuninori Morimoto 	dev->groups	= soc_dev_attr_groups;
417d918a376SKuninori Morimoto 
418d918a376SKuninori Morimoto 	dev_set_name(dev, "%s", dai_link->name);
419d918a376SKuninori Morimoto 
420d918a376SKuninori Morimoto 	ret = device_register(dev);
421d918a376SKuninori Morimoto 	if (ret < 0) {
422d918a376SKuninori Morimoto 		put_device(dev); /* soc_release_rtd_dev */
423d918a376SKuninori Morimoto 		return NULL;
424d918a376SKuninori Morimoto 	}
425d918a376SKuninori Morimoto 
426d918a376SKuninori Morimoto 	/*
4276e864344SKuninori Morimoto 	 * for rtd
4286e864344SKuninori Morimoto 	 */
4294dc0e7dfSKuninori Morimoto 	rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL);
4301a497983SMengdong Lin 	if (!rtd)
4316e864344SKuninori Morimoto 		goto free_rtd;
4321a497983SMengdong Lin 
433d918a376SKuninori Morimoto 	rtd->dev = dev;
434d918a376SKuninori Morimoto 	dev_set_drvdata(dev, rtd);
435d918a376SKuninori Morimoto 
4366e864344SKuninori Morimoto 	/*
4376e864344SKuninori Morimoto 	 * for rtd->codec_dais
4386e864344SKuninori Morimoto 	 */
4394dc0e7dfSKuninori Morimoto 	rtd->codec_dais = devm_kcalloc(dev, dai_link->num_codecs,
4406396bb22SKees Cook 					sizeof(struct snd_soc_dai *),
4411a497983SMengdong Lin 					GFP_KERNEL);
4426e864344SKuninori Morimoto 	if (!rtd->codec_dais)
4436e864344SKuninori Morimoto 		goto free_rtd;
4446e864344SKuninori Morimoto 
4456e864344SKuninori Morimoto 	/*
4466e864344SKuninori Morimoto 	 * rtd remaining settings
4476e864344SKuninori Morimoto 	 */
448929deb84SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
4496e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
4506e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
4516e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
4526e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
4536e864344SKuninori Morimoto 
454929deb84SKuninori Morimoto 	rtd->card = card;
455929deb84SKuninori Morimoto 	rtd->dai_link = dai_link;
456929deb84SKuninori Morimoto 	if (!rtd->dai_link->ops)
457929deb84SKuninori Morimoto 		rtd->dai_link->ops = &null_snd_soc_ops;
458929deb84SKuninori Morimoto 
4596634e3d6SKuninori Morimoto 	/* see for_each_card_rtds */
4601a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
4611a497983SMengdong Lin 	rtd->num = card->num_rtd;
4621a497983SMengdong Lin 	card->num_rtd++;
463a848125eSKuninori Morimoto 
464a848125eSKuninori Morimoto 	return rtd;
4656e864344SKuninori Morimoto 
4666e864344SKuninori Morimoto free_rtd:
4676e864344SKuninori Morimoto 	soc_free_pcm_runtime(rtd);
4686e864344SKuninori Morimoto 	return NULL;
4691a497983SMengdong Lin }
4701a497983SMengdong Lin 
4711a497983SMengdong Lin static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
4721a497983SMengdong Lin {
4731a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, *_rtd;
4741a497983SMengdong Lin 
475753ace0aSKuninori Morimoto 	for_each_card_rtds_safe(card, rtd, _rtd)
4761a497983SMengdong Lin 		soc_free_pcm_runtime(rtd);
4771a497983SMengdong Lin }
4781a497983SMengdong Lin 
47947c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
48047c88fffSLiam Girdwood 		const char *dai_link)
48147c88fffSLiam Girdwood {
4821a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
48347c88fffSLiam Girdwood 
484bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4851a497983SMengdong Lin 		if (!strcmp(rtd->dai_link->name, dai_link))
4861a497983SMengdong Lin 			return rtd;
48747c88fffSLiam Girdwood 	}
488f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
48947c88fffSLiam Girdwood 	return NULL;
49047c88fffSLiam Girdwood }
49147c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
49247c88fffSLiam Girdwood 
49365462e44SKuninori Morimoto static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
49465462e44SKuninori Morimoto {
49565462e44SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
49665462e44SKuninori Morimoto 
49765462e44SKuninori Morimoto 	for_each_card_rtds(card, rtd)
49865462e44SKuninori Morimoto 		flush_delayed_work(&rtd->delayed_work);
49965462e44SKuninori Morimoto }
50065462e44SKuninori Morimoto 
5016f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
502db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5036f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
504db2a4165SFrank Mandarino {
5056f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
506d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
5071a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
5081a497983SMengdong Lin 	int i;
509db2a4165SFrank Mandarino 
510c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
511c5599b87SLars-Peter Clausen 	if (!card->instantiated)
512e3509ff0SDaniel Mack 		return 0;
513e3509ff0SDaniel Mack 
5142c7b696aSMarcel Ziswiler 	/*
5152c7b696aSMarcel Ziswiler 	 * Due to the resume being scheduled into a workqueue we could
5166ed25978SAndy Green 	 * suspend before that's finished - wait for it to complete.
5176ed25978SAndy Green 	 */
518f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
5196ed25978SAndy Green 
5206ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
521f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5226ed25978SAndy Green 
523a00f90f9SMark Brown 	/* mute any active DACs */
524bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5250b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
5263efab7dcSMark Brown 
5271a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5283efab7dcSMark Brown 			continue;
5293efab7dcSMark Brown 
5300b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
53188fdffa2SKuninori Morimoto 			if (dai->playback_active)
53288fdffa2SKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 1,
53388fdffa2SKuninori Morimoto 						SNDRV_PCM_STREAM_PLAYBACK);
534db2a4165SFrank Mandarino 		}
53588bd870fSBenoit Cousson 	}
536db2a4165SFrank Mandarino 
5374ccab3e7SLiam Girdwood 	/* suspend all pcms */
538bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5391a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5403efab7dcSMark Brown 			continue;
5413efab7dcSMark Brown 
5421a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
5433efab7dcSMark Brown 	}
5444ccab3e7SLiam Girdwood 
54587506549SMark Brown 	if (card->suspend_pre)
54670b2ac12SMark Brown 		card->suspend_pre(card);
547db2a4165SFrank Mandarino 
548bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5491a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
5503efab7dcSMark Brown 
5511a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5523efab7dcSMark Brown 			continue;
5533efab7dcSMark Brown 
554e0f22622SKuninori Morimoto 		if (!cpu_dai->driver->bus_control)
555e0f22622SKuninori Morimoto 			snd_soc_dai_suspend(cpu_dai);
556db2a4165SFrank Mandarino 	}
557db2a4165SFrank Mandarino 
55837660b6dSLars-Peter Clausen 	/* close any waiting streams */
55965462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
560db2a4165SFrank Mandarino 
561bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5623efab7dcSMark Brown 
5631a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5643efab7dcSMark Brown 			continue;
5653efab7dcSMark Brown 
5661a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
5677bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
568db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
569f0fba2adSLiam Girdwood 
5701a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
5717bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
572db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
573db2a4165SFrank Mandarino 	}
574db2a4165SFrank Mandarino 
5758be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
5768be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
577e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
578e2d32ff6SMark Brown 
5799178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
580f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
5812c7b696aSMarcel Ziswiler 		struct snd_soc_dapm_context *dapm =
5822c7b696aSMarcel Ziswiler 				snd_soc_component_get_dapm(component);
583d9fc4063SKuninori Morimoto 
5842c7b696aSMarcel Ziswiler 		/*
5852c7b696aSMarcel Ziswiler 		 * If there are paths active then the COMPONENT will be held
5862c7b696aSMarcel Ziswiler 		 * with bias _ON and should not be suspended.
5872c7b696aSMarcel Ziswiler 		 */
588e40fadbcSKuninori Morimoto 		if (!snd_soc_component_is_suspended(component)) {
5894890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
5901547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
591125a25daSMark Brown 				/*
5929178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
593125a25daSMark Brown 				 * bias off then being in STANDBY
594125a25daSMark Brown 				 * means it's doing something,
595125a25daSMark Brown 				 * otherwise fall through.
596125a25daSMark Brown 				 */
5974890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
5989178feb4SKuninori Morimoto 					dev_dbg(component->dev,
59910e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
600125a25daSMark Brown 					break;
601125a25daSMark Brown 				}
6021a12d5dcSGustavo A. R. Silva 				/* fall through */
603a8093297SLars-Peter Clausen 
6041547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
60566c51573SKuninori Morimoto 				snd_soc_component_suspend(component);
6069178feb4SKuninori Morimoto 				if (component->regmap)
6079178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
608988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
6099178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
6101547aba9SMark Brown 				break;
6111547aba9SMark Brown 			default:
6129178feb4SKuninori Morimoto 				dev_dbg(component->dev,
6139178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
6141547aba9SMark Brown 				break;
6151547aba9SMark Brown 			}
6161547aba9SMark Brown 		}
617f0fba2adSLiam Girdwood 	}
618db2a4165SFrank Mandarino 
619bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6201a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6213efab7dcSMark Brown 
6221a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6233efab7dcSMark Brown 			continue;
6243efab7dcSMark Brown 
625e0f22622SKuninori Morimoto 		if (cpu_dai->driver->bus_control)
626e0f22622SKuninori Morimoto 			snd_soc_dai_suspend(cpu_dai);
627988e8cc4SNicolin Chen 
628988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
629988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
630db2a4165SFrank Mandarino 	}
631db2a4165SFrank Mandarino 
63287506549SMark Brown 	if (card->suspend_post)
63370b2ac12SMark Brown 		card->suspend_post(card);
634db2a4165SFrank Mandarino 
635db2a4165SFrank Mandarino 	return 0;
636db2a4165SFrank Mandarino }
6376f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
638db2a4165SFrank Mandarino 
6392c7b696aSMarcel Ziswiler /*
6402c7b696aSMarcel Ziswiler  * deferred resume work, so resume can complete before we finished
6416ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6426ed25978SAndy Green  */
6436ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
644db2a4165SFrank Mandarino {
645f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
6462c7b696aSMarcel Ziswiler 			container_of(work, struct snd_soc_card,
6472c7b696aSMarcel Ziswiler 				     deferred_resume_work);
6481a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
649d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
6501a497983SMengdong Lin 	int i;
651db2a4165SFrank Mandarino 
6522c7b696aSMarcel Ziswiler 	/*
6532c7b696aSMarcel Ziswiler 	 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6546ed25978SAndy Green 	 * so userspace apps are blocked from touching us
6556ed25978SAndy Green 	 */
6566ed25978SAndy Green 
657f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
6586ed25978SAndy Green 
6599949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
660f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
6619949788bSMark Brown 
66287506549SMark Brown 	if (card->resume_pre)
66370b2ac12SMark Brown 		card->resume_pre(card);
664db2a4165SFrank Mandarino 
665bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
666bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6671a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6683efab7dcSMark Brown 
6691a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6703efab7dcSMark Brown 			continue;
6713efab7dcSMark Brown 
67224b09d05SKuninori Morimoto 		if (cpu_dai->driver->bus_control)
67324b09d05SKuninori Morimoto 			snd_soc_dai_resume(cpu_dai);
674db2a4165SFrank Mandarino 	}
675db2a4165SFrank Mandarino 
676f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
677e40fadbcSKuninori Morimoto 		if (snd_soc_component_is_suspended(component))
6789a840cbaSKuninori Morimoto 			snd_soc_component_resume(component);
6791547aba9SMark Brown 	}
680db2a4165SFrank Mandarino 
681bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6823efab7dcSMark Brown 
6831a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6843efab7dcSMark Brown 			continue;
6853efab7dcSMark Brown 
6861a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
687d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
688db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
689f0fba2adSLiam Girdwood 
6901a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
691d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
692db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
693db2a4165SFrank Mandarino 	}
694db2a4165SFrank Mandarino 
6953ff3f64bSMark Brown 	/* unmute any active DACs */
696bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6970b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
6983efab7dcSMark Brown 
6991a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7003efab7dcSMark Brown 			continue;
7013efab7dcSMark Brown 
7020b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
70388fdffa2SKuninori Morimoto 			if (dai->playback_active)
70488fdffa2SKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 0,
70588fdffa2SKuninori Morimoto 						SNDRV_PCM_STREAM_PLAYBACK);
706db2a4165SFrank Mandarino 		}
70788bd870fSBenoit Cousson 	}
708db2a4165SFrank Mandarino 
709bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7101a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7113efab7dcSMark Brown 
7121a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7133efab7dcSMark Brown 			continue;
7143efab7dcSMark Brown 
71524b09d05SKuninori Morimoto 		if (!cpu_dai->driver->bus_control)
71624b09d05SKuninori Morimoto 			snd_soc_dai_resume(cpu_dai);
717db2a4165SFrank Mandarino 	}
718db2a4165SFrank Mandarino 
71987506549SMark Brown 	if (card->resume_post)
72070b2ac12SMark Brown 		card->resume_post(card);
721db2a4165SFrank Mandarino 
722f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
7236ed25978SAndy Green 
7248be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
7258be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
726e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
7271a7aaa58SJeeja KP 
7281a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
7291a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
7306ed25978SAndy Green }
7316ed25978SAndy Green 
7326ed25978SAndy Green /* powers up audio subsystem after a suspend */
7336f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
7346ed25978SAndy Green {
7356f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
736bc263214SLars-Peter Clausen 	bool bus_control = false;
7371a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
73822d251a5SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
73922d251a5SKuninori Morimoto 	int i;
740b9dd94a8SPeter Ujfalusi 
741c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
742c5599b87SLars-Peter Clausen 	if (!card->instantiated)
7435ff1ddf2SEric Miao 		return 0;
7445ff1ddf2SEric Miao 
745988e8cc4SNicolin Chen 	/* activate pins from sleep state */
746bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
74788bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
74888bd870fSBenoit Cousson 
749988e8cc4SNicolin Chen 		if (cpu_dai->active)
750988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
75188bd870fSBenoit Cousson 
75222d251a5SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
753988e8cc4SNicolin Chen 			if (codec_dai->active)
754988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
755988e8cc4SNicolin Chen 		}
75688bd870fSBenoit Cousson 	}
757988e8cc4SNicolin Chen 
758bc263214SLars-Peter Clausen 	/*
759bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
760bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
761bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
76264ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
76364ab9baaSMark Brown 	 */
764bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7651a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7662c7b696aSMarcel Ziswiler 
767bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
76882e14e8bSStephen Warren 	}
769bc263214SLars-Peter Clausen 	if (bus_control) {
770bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
77164ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
77264ab9baaSMark Brown 	} else {
773f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
7746308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
775f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
776f0fba2adSLiam Girdwood 	}
7776ed25978SAndy Green 
778db2a4165SFrank Mandarino 	return 0;
779db2a4165SFrank Mandarino }
7806f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
781b3da4251SKuninori Morimoto 
782b3da4251SKuninori Morimoto static void soc_resume_init(struct snd_soc_card *card)
783b3da4251SKuninori Morimoto {
784b3da4251SKuninori Morimoto 	/* deferred resume work */
785b3da4251SKuninori Morimoto 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
786b3da4251SKuninori Morimoto }
787db2a4165SFrank Mandarino #else
7886f8ab4acSMark Brown #define snd_soc_suspend NULL
7896f8ab4acSMark Brown #define snd_soc_resume NULL
790b3da4251SKuninori Morimoto static inline void soc_resume_init(struct snd_soc_card *card)
791b3da4251SKuninori Morimoto {
792b3da4251SKuninori Morimoto }
793db2a4165SFrank Mandarino #endif
794db2a4165SFrank Mandarino 
79585e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
79602a06d30SBarry Song };
79702a06d30SBarry Song 
798c0834440SKuninori Morimoto static struct device_node
799c0834440SKuninori Morimoto *soc_component_to_node(struct snd_soc_component *component)
80012023a9aSMisael Lopez Cruz {
801c0834440SKuninori Morimoto 	struct device_node *of_node;
80212023a9aSMisael Lopez Cruz 
803c0834440SKuninori Morimoto 	of_node = component->dev->of_node;
804c0834440SKuninori Morimoto 	if (!of_node && component->dev->parent)
805c0834440SKuninori Morimoto 		of_node = component->dev->parent->of_node;
80634e81ab4SLars-Peter Clausen 
807c0834440SKuninori Morimoto 	return of_node;
80812023a9aSMisael Lopez Cruz }
80912023a9aSMisael Lopez Cruz 
810be6ac0a9SKuninori Morimoto static int snd_soc_is_matching_component(
811be6ac0a9SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc,
812be6ac0a9SKuninori Morimoto 	struct snd_soc_component *component)
813be6ac0a9SKuninori Morimoto {
814be6ac0a9SKuninori Morimoto 	struct device_node *component_of_node;
815be6ac0a9SKuninori Morimoto 
8167d7db5d3SKuninori Morimoto 	if (!dlc)
8177d7db5d3SKuninori Morimoto 		return 0;
8187d7db5d3SKuninori Morimoto 
8197d7db5d3SKuninori Morimoto 	component_of_node = soc_component_to_node(component);
820be6ac0a9SKuninori Morimoto 
821be6ac0a9SKuninori Morimoto 	if (dlc->of_node && component_of_node != dlc->of_node)
822be6ac0a9SKuninori Morimoto 		return 0;
823be6ac0a9SKuninori Morimoto 	if (dlc->name && strcmp(component->name, dlc->name))
824be6ac0a9SKuninori Morimoto 		return 0;
825be6ac0a9SKuninori Morimoto 
826be6ac0a9SKuninori Morimoto 	return 1;
827be6ac0a9SKuninori Morimoto }
828be6ac0a9SKuninori Morimoto 
829db2a4165SFrank Mandarino static struct snd_soc_component *soc_find_component(
830c1e230f0SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc)
831db2a4165SFrank Mandarino {
832db2a4165SFrank Mandarino 	struct snd_soc_component *component;
833db2a4165SFrank Mandarino 
834db2a4165SFrank Mandarino 	lockdep_assert_held(&client_mutex);
835db2a4165SFrank Mandarino 
836e3303268SKuninori Morimoto 	/*
837e3303268SKuninori Morimoto 	 * NOTE
838e3303268SKuninori Morimoto 	 *
839e3303268SKuninori Morimoto 	 * It returns *1st* found component, but some driver
840e3303268SKuninori Morimoto 	 * has few components by same of_node/name
841e3303268SKuninori Morimoto 	 * ex)
842e3303268SKuninori Morimoto 	 *	CPU component and generic DMAEngine component
843e3303268SKuninori Morimoto 	 */
844c1e230f0SKuninori Morimoto 	for_each_component(component)
845c1e230f0SKuninori Morimoto 		if (snd_soc_is_matching_component(dlc, component))
846db2a4165SFrank Mandarino 			return component;
847db2a4165SFrank Mandarino 
848db2a4165SFrank Mandarino 	return NULL;
84912023a9aSMisael Lopez Cruz }
85012023a9aSMisael Lopez Cruz 
851fbb88b5cSMengdong Lin /**
852fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
853fbb88b5cSMengdong Lin  *
8544958471bSJeffy Chen  * @dlc: name of the DAI or the DAI driver and optional component info to match
855fbb88b5cSMengdong Lin  *
856ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
857fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
858fbb88b5cSMengdong Lin  * should also match if being specified.
859fbb88b5cSMengdong Lin  *
860fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
861fbb88b5cSMengdong Lin  */
862305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
86314621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
86412023a9aSMisael Lopez Cruz {
86514621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
86614621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
86712023a9aSMisael Lopez Cruz 
86834e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
86934e81ab4SLars-Peter Clausen 
87014621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs */
871368dee94SKuninori Morimoto 	for_each_component(component) {
872be6ac0a9SKuninori Morimoto 		if (!snd_soc_is_matching_component(dlc, component))
87312023a9aSMisael Lopez Cruz 			continue;
87415a0c645SKuninori Morimoto 		for_each_component_dais(component, dai) {
8754958471bSJeffy Chen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
8766a6dafdaSJeffy Chen 			    && (!dai->driver->name
8776a6dafdaSJeffy Chen 				|| strcmp(dai->driver->name, dlc->dai_name)))
87814621c7eSLars-Peter Clausen 				continue;
87912023a9aSMisael Lopez Cruz 
88014621c7eSLars-Peter Clausen 			return dai;
88112023a9aSMisael Lopez Cruz 		}
88212023a9aSMisael Lopez Cruz 	}
88312023a9aSMisael Lopez Cruz 
88412023a9aSMisael Lopez Cruz 	return NULL;
88512023a9aSMisael Lopez Cruz }
886305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
88712023a9aSMisael Lopez Cruz 
88817fb1755SMengdong Lin /**
88917fb1755SMengdong Lin  * snd_soc_find_dai_link - Find a DAI link
89017fb1755SMengdong Lin  *
89117fb1755SMengdong Lin  * @card: soc card
89217fb1755SMengdong Lin  * @id: DAI link ID to match
89317fb1755SMengdong Lin  * @name: DAI link name to match, optional
8948abab35fSCharles Keepax  * @stream_name: DAI link stream name to match, optional
89517fb1755SMengdong Lin  *
89617fb1755SMengdong Lin  * This function will search all existing DAI links of the soc card to
89717fb1755SMengdong Lin  * find the link of the same ID. Since DAI links may not have their
89817fb1755SMengdong Lin  * unique ID, so name and stream name should also match if being
89917fb1755SMengdong Lin  * specified.
90017fb1755SMengdong Lin  *
90117fb1755SMengdong Lin  * Return: pointer of DAI link, or NULL if not found.
90217fb1755SMengdong Lin  */
90317fb1755SMengdong Lin struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
90417fb1755SMengdong Lin 					       int id, const char *name,
90517fb1755SMengdong Lin 					       const char *stream_name)
90617fb1755SMengdong Lin {
90742849064SKuninori Morimoto 	struct snd_soc_dai_link *link;
90817fb1755SMengdong Lin 
90917fb1755SMengdong Lin 	lockdep_assert_held(&client_mutex);
91017fb1755SMengdong Lin 
91142849064SKuninori Morimoto 	for_each_card_links(card, link) {
91217fb1755SMengdong Lin 		if (link->id != id)
91317fb1755SMengdong Lin 			continue;
91417fb1755SMengdong Lin 
91517fb1755SMengdong Lin 		if (name && (!link->name || strcmp(name, link->name)))
91617fb1755SMengdong Lin 			continue;
91717fb1755SMengdong Lin 
91817fb1755SMengdong Lin 		if (stream_name && (!link->stream_name
91917fb1755SMengdong Lin 			|| strcmp(stream_name, link->stream_name)))
92017fb1755SMengdong Lin 			continue;
92117fb1755SMengdong Lin 
92217fb1755SMengdong Lin 		return link;
92317fb1755SMengdong Lin 	}
92417fb1755SMengdong Lin 
92517fb1755SMengdong Lin 	return NULL;
92617fb1755SMengdong Lin }
92717fb1755SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
92817fb1755SMengdong Lin 
92949a5ba1cSMengdong Lin static bool soc_is_dai_link_bound(struct snd_soc_card *card,
93049a5ba1cSMengdong Lin 		struct snd_soc_dai_link *dai_link)
931db2a4165SFrank Mandarino {
93249a5ba1cSMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
93349a5ba1cSMengdong Lin 
934bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
93549a5ba1cSMengdong Lin 		if (rtd->dai_link == dai_link)
93649a5ba1cSMengdong Lin 			return true;
93749a5ba1cSMengdong Lin 	}
93849a5ba1cSMengdong Lin 
93949a5ba1cSMengdong Lin 	return false;
94049a5ba1cSMengdong Lin }
94149a5ba1cSMengdong Lin 
9426f2f1ff0SMengdong Lin static int soc_bind_dai_link(struct snd_soc_card *card,
9436f2f1ff0SMengdong Lin 	struct snd_soc_dai_link *dai_link)
944db2a4165SFrank Mandarino {
9451a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
94634614739SJerome Brunet 	struct snd_soc_dai_link_component *codec, *platform;
94790be711eSKuninori Morimoto 	struct snd_soc_component *component;
94888bd870fSBenoit Cousson 	int i;
949db2a4165SFrank Mandarino 
950a655de80SLiam Girdwood 	if (dai_link->ignore)
951a655de80SLiam Girdwood 		return 0;
952a655de80SLiam Girdwood 
9536f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
9546308419aSMark Brown 
95549a5ba1cSMengdong Lin 	if (soc_is_dai_link_bound(card, dai_link)) {
95649a5ba1cSMengdong Lin 		dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
95749a5ba1cSMengdong Lin 			dai_link->name);
95849a5ba1cSMengdong Lin 		return 0;
95949a5ba1cSMengdong Lin 	}
960db2a4165SFrank Mandarino 
961513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
962513cb311SSudip Mukherjee 	if (!rtd)
963513cb311SSudip Mukherjee 		return -ENOMEM;
964513cb311SSudip Mukherjee 
96508a5841eSKuninori Morimoto 	/* FIXME: we need multi CPU support in the future */
96608a5841eSKuninori Morimoto 	rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
967b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
9686b490879SMartin Hundebøll 		dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
96908a5841eSKuninori Morimoto 			 dai_link->cpus->dai_name);
9701a497983SMengdong Lin 		goto _err_defer;
971c5af3a2eSMark Brown 	}
97290be711eSKuninori Morimoto 	snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
973c5af3a2eSMark Brown 
97488bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
975e2b30edfSKuninori Morimoto 	rtd->num_codecs = dai_link->num_codecs;
97634614739SJerome Brunet 	for_each_link_codecs(dai_link, i, codec) {
97734614739SJerome Brunet 		rtd->codec_dais[i] = snd_soc_find_dai(codec);
9780a2cfcd9SKuninori Morimoto 		if (!rtd->codec_dais[i]) {
9797c7e2d6aSStefan Agner 			dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
98034614739SJerome Brunet 				 codec->dai_name);
9811a497983SMengdong Lin 			goto _err_defer;
98212023a9aSMisael Lopez Cruz 		}
98334614739SJerome Brunet 
9840a2cfcd9SKuninori Morimoto 		snd_soc_rtdcom_add(rtd, rtd->codec_dais[i]->component);
98588bd870fSBenoit Cousson 	}
98688bd870fSBenoit Cousson 
98788bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
9880a2cfcd9SKuninori Morimoto 	rtd->codec_dai = rtd->codec_dais[0];
98912023a9aSMisael Lopez Cruz 
990e2b30edfSKuninori Morimoto 	/* Find PLATFORM from registered PLATFORMs */
99134614739SJerome Brunet 	for_each_link_platforms(dai_link, i, platform) {
992368dee94SKuninori Morimoto 		for_each_component(component) {
99334614739SJerome Brunet 			if (!snd_soc_is_matching_component(platform, component))
99490be711eSKuninori Morimoto 				continue;
99590be711eSKuninori Morimoto 
99690be711eSKuninori Morimoto 			snd_soc_rtdcom_add(rtd, component);
99790be711eSKuninori Morimoto 		}
99834614739SJerome Brunet 	}
99990be711eSKuninori Morimoto 
1000b19e6e7bSMark Brown 	return 0;
10011a497983SMengdong Lin 
10021a497983SMengdong Lin _err_defer:
10031a497983SMengdong Lin 	soc_free_pcm_runtime(rtd);
10041a497983SMengdong Lin 	return -EPROBE_DEFER;
10056b05eda6SMark Brown }
10066b05eda6SMark Brown 
1007ffd60fbaSKuninori Morimoto static void soc_set_of_name_prefix(struct snd_soc_component *component)
1008ffd60fbaSKuninori Morimoto {
1009ffd60fbaSKuninori Morimoto 	struct device_node *of_node = soc_component_to_node(component);
1010ffd60fbaSKuninori Morimoto 	const char *str;
1011ffd60fbaSKuninori Morimoto 	int ret;
1012ffd60fbaSKuninori Morimoto 
1013ffd60fbaSKuninori Morimoto 	ret = of_property_read_string(of_node, "sound-name-prefix", &str);
1014ffd60fbaSKuninori Morimoto 	if (!ret)
1015ffd60fbaSKuninori Morimoto 		component->name_prefix = str;
1016ffd60fbaSKuninori Morimoto }
1017ffd60fbaSKuninori Morimoto 
1018ffd60fbaSKuninori Morimoto static void soc_set_name_prefix(struct snd_soc_card *card,
1019ffd60fbaSKuninori Morimoto 				struct snd_soc_component *component)
1020ffd60fbaSKuninori Morimoto {
1021ffd60fbaSKuninori Morimoto 	int i;
1022ffd60fbaSKuninori Morimoto 
1023ffd60fbaSKuninori Morimoto 	for (i = 0; i < card->num_configs && card->codec_conf; i++) {
1024ffd60fbaSKuninori Morimoto 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1025ffd60fbaSKuninori Morimoto 		struct device_node *of_node = soc_component_to_node(component);
1026ffd60fbaSKuninori Morimoto 
1027ffd60fbaSKuninori Morimoto 		if (map->of_node && of_node != map->of_node)
1028ffd60fbaSKuninori Morimoto 			continue;
1029ffd60fbaSKuninori Morimoto 		if (map->dev_name && strcmp(component->name, map->dev_name))
1030ffd60fbaSKuninori Morimoto 			continue;
1031ffd60fbaSKuninori Morimoto 		component->name_prefix = map->name_prefix;
1032ffd60fbaSKuninori Morimoto 		return;
1033ffd60fbaSKuninori Morimoto 	}
1034ffd60fbaSKuninori Morimoto 
1035ffd60fbaSKuninori Morimoto 	/*
1036ffd60fbaSKuninori Morimoto 	 * If there is no configuration table or no match in the table,
1037ffd60fbaSKuninori Morimoto 	 * check if a prefix is provided in the node
1038ffd60fbaSKuninori Morimoto 	 */
1039ffd60fbaSKuninori Morimoto 	soc_set_of_name_prefix(component);
1040ffd60fbaSKuninori Morimoto }
1041ffd60fbaSKuninori Morimoto 
104222d14231SKuninori Morimoto static void soc_cleanup_component(struct snd_soc_component *component)
104322d14231SKuninori Morimoto {
104404f770d9SKuninori Morimoto 	/* For framework level robustness */
10453bb936f5SAmadeusz Sławiński 	snd_soc_component_set_jack(component, NULL, NULL);
104604f770d9SKuninori Morimoto 
10477a5d9815SBard liao 	list_del_init(&component->card_list);
104822d14231SKuninori Morimoto 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
104922d14231SKuninori Morimoto 	soc_cleanup_component_debugfs(component);
105022d14231SKuninori Morimoto 	component->card = NULL;
10510e36f36bSPierre-Louis Bossart 	snd_soc_component_module_put_when_remove(component);
105222d14231SKuninori Morimoto }
105322d14231SKuninori Morimoto 
1054f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
1055d12cd198SStephen Warren {
1056abd31b32SLars-Peter Clausen 	if (!component->card)
105770090bbbSLars-Peter Clausen 		return;
1058d12cd198SStephen Warren 
105903b34dd7SKuninori Morimoto 	snd_soc_component_remove(component);
10607a5d9815SBard liao 
106122d14231SKuninori Morimoto 	soc_cleanup_component(component);
1062d12cd198SStephen Warren }
1063d12cd198SStephen Warren 
1064ffd60fbaSKuninori Morimoto static int soc_probe_component(struct snd_soc_card *card,
1065ffd60fbaSKuninori Morimoto 			       struct snd_soc_component *component)
1066ffd60fbaSKuninori Morimoto {
1067ffd60fbaSKuninori Morimoto 	struct snd_soc_dapm_context *dapm =
1068ffd60fbaSKuninori Morimoto 		snd_soc_component_get_dapm(component);
1069ffd60fbaSKuninori Morimoto 	struct snd_soc_dai *dai;
1070ffd60fbaSKuninori Morimoto 	int ret;
1071ffd60fbaSKuninori Morimoto 
1072ffd60fbaSKuninori Morimoto 	if (!strcmp(component->name, "snd-soc-dummy"))
1073ffd60fbaSKuninori Morimoto 		return 0;
1074ffd60fbaSKuninori Morimoto 
1075ffd60fbaSKuninori Morimoto 	if (component->card) {
1076ffd60fbaSKuninori Morimoto 		if (component->card != card) {
1077ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1078ffd60fbaSKuninori Morimoto 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1079ffd60fbaSKuninori Morimoto 				card->name, component->card->name);
1080ffd60fbaSKuninori Morimoto 			return -ENODEV;
1081ffd60fbaSKuninori Morimoto 		}
1082ffd60fbaSKuninori Morimoto 		return 0;
1083ffd60fbaSKuninori Morimoto 	}
1084ffd60fbaSKuninori Morimoto 
1085ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_module_get_when_probe(component);
1086ffd60fbaSKuninori Morimoto 	if (ret < 0)
1087ffd60fbaSKuninori Morimoto 		return ret;
1088ffd60fbaSKuninori Morimoto 
1089ffd60fbaSKuninori Morimoto 	component->card = card;
1090ffd60fbaSKuninori Morimoto 	soc_set_name_prefix(card, component);
1091ffd60fbaSKuninori Morimoto 
1092ffd60fbaSKuninori Morimoto 	soc_init_component_debugfs(component);
1093ffd60fbaSKuninori Morimoto 
109495c267ddSKuninori Morimoto 	snd_soc_dapm_init(dapm, card, component);
1095b614beafSKuninori Morimoto 
1096ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_new_controls(dapm,
1097ffd60fbaSKuninori Morimoto 					component->driver->dapm_widgets,
1098ffd60fbaSKuninori Morimoto 					component->driver->num_dapm_widgets);
1099ffd60fbaSKuninori Morimoto 
1100ffd60fbaSKuninori Morimoto 	if (ret != 0) {
1101ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1102ffd60fbaSKuninori Morimoto 			"Failed to create new controls %d\n", ret);
1103ffd60fbaSKuninori Morimoto 		goto err_probe;
1104ffd60fbaSKuninori Morimoto 	}
1105ffd60fbaSKuninori Morimoto 
1106ffd60fbaSKuninori Morimoto 	for_each_component_dais(component, dai) {
1107ffd60fbaSKuninori Morimoto 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1108ffd60fbaSKuninori Morimoto 		if (ret != 0) {
1109ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1110ffd60fbaSKuninori Morimoto 				"Failed to create DAI widgets %d\n", ret);
1111ffd60fbaSKuninori Morimoto 			goto err_probe;
1112ffd60fbaSKuninori Morimoto 		}
1113ffd60fbaSKuninori Morimoto 	}
1114ffd60fbaSKuninori Morimoto 
1115ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_probe(component);
1116ffd60fbaSKuninori Morimoto 	if (ret < 0) {
1117ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1118ffd60fbaSKuninori Morimoto 			"ASoC: failed to probe component %d\n", ret);
1119ffd60fbaSKuninori Morimoto 		goto err_probe;
1120ffd60fbaSKuninori Morimoto 	}
1121ffd60fbaSKuninori Morimoto 	WARN(dapm->idle_bias_off &&
1122ffd60fbaSKuninori Morimoto 	     dapm->bias_level != SND_SOC_BIAS_OFF,
1123ffd60fbaSKuninori Morimoto 	     "codec %s can not start from non-off bias with idle_bias_off==1\n",
1124ffd60fbaSKuninori Morimoto 	     component->name);
1125ffd60fbaSKuninori Morimoto 
1126ffd60fbaSKuninori Morimoto 	/* machine specific init */
1127ffd60fbaSKuninori Morimoto 	if (component->init) {
1128ffd60fbaSKuninori Morimoto 		ret = component->init(component);
1129ffd60fbaSKuninori Morimoto 		if (ret < 0) {
1130ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1131ffd60fbaSKuninori Morimoto 				"Failed to do machine specific init %d\n", ret);
1132ffd60fbaSKuninori Morimoto 			goto err_probe;
1133ffd60fbaSKuninori Morimoto 		}
1134ffd60fbaSKuninori Morimoto 	}
1135ffd60fbaSKuninori Morimoto 
1136ffd60fbaSKuninori Morimoto 	ret = snd_soc_add_component_controls(component,
1137ffd60fbaSKuninori Morimoto 					     component->driver->controls,
1138ffd60fbaSKuninori Morimoto 					     component->driver->num_controls);
1139ffd60fbaSKuninori Morimoto 	if (ret < 0)
1140ffd60fbaSKuninori Morimoto 		goto err_probe;
1141ffd60fbaSKuninori Morimoto 
1142ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(dapm,
1143ffd60fbaSKuninori Morimoto 				      component->driver->dapm_routes,
1144ffd60fbaSKuninori Morimoto 				      component->driver->num_dapm_routes);
1145ffd60fbaSKuninori Morimoto 	if (ret < 0)
1146ffd60fbaSKuninori Morimoto 		goto err_probe;
1147ffd60fbaSKuninori Morimoto 
1148ffd60fbaSKuninori Morimoto 	/* see for_each_card_components */
1149ffd60fbaSKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1150ffd60fbaSKuninori Morimoto 
1151ffd60fbaSKuninori Morimoto err_probe:
1152ffd60fbaSKuninori Morimoto 	if (ret < 0)
1153ffd60fbaSKuninori Morimoto 		soc_cleanup_component(component);
1154ffd60fbaSKuninori Morimoto 
1155ffd60fbaSKuninori Morimoto 	return ret;
1156ffd60fbaSKuninori Morimoto }
1157ffd60fbaSKuninori Morimoto 
1158e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1159589c3563SJarkko Nikula {
1160589c3563SJarkko Nikula 	int err;
1161589c3563SJarkko Nikula 
116252abe6ccSGuennadi Liakhovetski 	if (!dai || !dai->probed || !dai->driver ||
11632eda3cb1SKuninori Morimoto 	    dai->driver->remove_order != order)
11642eda3cb1SKuninori Morimoto 		return;
11652eda3cb1SKuninori Morimoto 
1166dcdab582SKuninori Morimoto 	err = snd_soc_dai_remove(dai);
1167589c3563SJarkko Nikula 	if (err < 0)
1168e60cd14fSLars-Peter Clausen 		dev_err(dai->dev,
1169b0aa88afSMisael Lopez Cruz 			"ASoC: failed to remove %s: %d\n",
1170e60cd14fSLars-Peter Clausen 			dai->name, err);
1171dcdab582SKuninori Morimoto 
1172e60cd14fSLars-Peter Clausen 	dai->probed = 0;
1173b0aa88afSMisael Lopez Cruz }
1174b0aa88afSMisael Lopez Cruz 
1175a7d44f78SKuninori Morimoto static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1176a7d44f78SKuninori Morimoto {
1177a7d44f78SKuninori Morimoto 	int ret;
1178a7d44f78SKuninori Morimoto 
1179a7d44f78SKuninori Morimoto 	if (dai->probed ||
1180a7d44f78SKuninori Morimoto 	    dai->driver->probe_order != order)
1181a7d44f78SKuninori Morimoto 		return 0;
1182a7d44f78SKuninori Morimoto 
1183a7d44f78SKuninori Morimoto 	ret = snd_soc_dai_probe(dai);
1184a7d44f78SKuninori Morimoto 	if (ret < 0) {
1185a7d44f78SKuninori Morimoto 		dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1186a7d44f78SKuninori Morimoto 			dai->name, ret);
1187a7d44f78SKuninori Morimoto 		return ret;
1188a7d44f78SKuninori Morimoto 	}
1189a7d44f78SKuninori Morimoto 
1190a7d44f78SKuninori Morimoto 	dai->probed = 1;
1191a7d44f78SKuninori Morimoto 
1192a7d44f78SKuninori Morimoto 	return 0;
1193a7d44f78SKuninori Morimoto }
1194a7d44f78SKuninori Morimoto 
11954ca47d21SKuninori Morimoto static void soc_remove_link_dais(struct snd_soc_card *card)
1196f0fba2adSLiam Girdwood {
1197e60cd14fSLars-Peter Clausen 	int i;
11980b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
11994ca47d21SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
12004ca47d21SKuninori Morimoto 	int order;
12014ca47d21SKuninori Morimoto 
12024ca47d21SKuninori Morimoto 	for_each_comp_order(order) {
12034ca47d21SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1204f0fba2adSLiam Girdwood 			/* remove the CODEC DAI */
12050b7990e3SKuninori Morimoto 			for_each_rtd_codec_dai(rtd, i, codec_dai)
12060b7990e3SKuninori Morimoto 				soc_remove_dai(codec_dai, order);
1207f0fba2adSLiam Girdwood 
1208e60cd14fSLars-Peter Clausen 			soc_remove_dai(rtd->cpu_dai, order);
1209f0fba2adSLiam Girdwood 		}
12104ca47d21SKuninori Morimoto 	}
12114ca47d21SKuninori Morimoto }
1212f0fba2adSLiam Girdwood 
1213bc7c16c2SKuninori Morimoto static int soc_probe_link_dais(struct snd_soc_card *card)
1214bc7c16c2SKuninori Morimoto {
1215bc7c16c2SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1216bc7c16c2SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
1217bc7c16c2SKuninori Morimoto 	int i, order, ret;
1218bc7c16c2SKuninori Morimoto 
1219bc7c16c2SKuninori Morimoto 	for_each_comp_order(order) {
1220bc7c16c2SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1221bc7c16c2SKuninori Morimoto 
1222bc7c16c2SKuninori Morimoto 			dev_dbg(card->dev,
1223bc7c16c2SKuninori Morimoto 				"ASoC: probe %s dai link %d late %d\n",
1224bc7c16c2SKuninori Morimoto 				card->name, rtd->num, order);
1225bc7c16c2SKuninori Morimoto 
1226bc7c16c2SKuninori Morimoto 			ret = soc_probe_dai(rtd->cpu_dai, order);
1227bc7c16c2SKuninori Morimoto 			if (ret)
1228bc7c16c2SKuninori Morimoto 				return ret;
1229bc7c16c2SKuninori Morimoto 
1230bc7c16c2SKuninori Morimoto 			/* probe the CODEC DAI */
1231bc7c16c2SKuninori Morimoto 			for_each_rtd_codec_dai(rtd, i, codec_dai) {
1232bc7c16c2SKuninori Morimoto 				ret = soc_probe_dai(codec_dai, order);
1233bc7c16c2SKuninori Morimoto 				if (ret)
1234bc7c16c2SKuninori Morimoto 					return ret;
1235bc7c16c2SKuninori Morimoto 			}
1236bc7c16c2SKuninori Morimoto 		}
1237bc7c16c2SKuninori Morimoto 	}
1238bc7c16c2SKuninori Morimoto 
1239bc7c16c2SKuninori Morimoto 	return 0;
1240bc7c16c2SKuninori Morimoto }
1241bc7c16c2SKuninori Morimoto 
1242b006c0c6SKuninori Morimoto static void soc_remove_link_components(struct snd_soc_card *card)
124362ae68faSStephen Warren {
124461aca564SLars-Peter Clausen 	struct snd_soc_component *component;
1245b006c0c6SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
124690be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
1247b006c0c6SKuninori Morimoto 	int order;
124862ae68faSStephen Warren 
1249b006c0c6SKuninori Morimoto 	for_each_comp_order(order) {
1250b006c0c6SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
125190be711eSKuninori Morimoto 			for_each_rtdcom(rtd, rtdcom) {
125290be711eSKuninori Morimoto 				component = rtdcom->component;
125362ae68faSStephen Warren 
1254b006c0c6SKuninori Morimoto 				if (component->driver->remove_order != order)
1255b006c0c6SKuninori Morimoto 					continue;
1256b006c0c6SKuninori Morimoto 
125761aca564SLars-Peter Clausen 				soc_remove_component(component);
125862ae68faSStephen Warren 			}
125962ae68faSStephen Warren 		}
1260b006c0c6SKuninori Morimoto 	}
1261b006c0c6SKuninori Morimoto }
126262ae68faSStephen Warren 
126362f07a6bSKuninori Morimoto static int soc_probe_link_components(struct snd_soc_card *card)
12646fb03550SKuninori Morimoto {
12656fb03550SKuninori Morimoto 	struct snd_soc_component *component;
126662f07a6bSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
12676fb03550SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
126862f07a6bSKuninori Morimoto 	int ret, order;
12696fb03550SKuninori Morimoto 
127062f07a6bSKuninori Morimoto 	for_each_comp_order(order) {
127162f07a6bSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
12726fb03550SKuninori Morimoto 			for_each_rtdcom(rtd, rtdcom) {
12736fb03550SKuninori Morimoto 				component = rtdcom->component;
12746fb03550SKuninori Morimoto 
127562f07a6bSKuninori Morimoto 				if (component->driver->probe_order != order)
127662f07a6bSKuninori Morimoto 					continue;
127762f07a6bSKuninori Morimoto 
12786fb03550SKuninori Morimoto 				ret = soc_probe_component(card, component);
12796fb03550SKuninori Morimoto 				if (ret < 0)
12806fb03550SKuninori Morimoto 					return ret;
12816fb03550SKuninori Morimoto 			}
12826fb03550SKuninori Morimoto 		}
128362f07a6bSKuninori Morimoto 	}
12846fb03550SKuninori Morimoto 
12856fb03550SKuninori Morimoto 	return 0;
12866fb03550SKuninori Morimoto }
12876fb03550SKuninori Morimoto 
1288923c5e61SMengdong Lin static int soc_init_dai_link(struct snd_soc_card *card,
1289923c5e61SMengdong Lin 			     struct snd_soc_dai_link *link)
1290923c5e61SMengdong Lin {
1291adb76b5bSKuninori Morimoto 	int i;
129234614739SJerome Brunet 	struct snd_soc_dai_link_component *codec, *platform;
1293923c5e61SMengdong Lin 
12943db769f1SKuninori Morimoto 	for_each_link_codecs(link, i, codec) {
1295923c5e61SMengdong Lin 		/*
1296923c5e61SMengdong Lin 		 * Codec must be specified by 1 of name or OF node,
1297923c5e61SMengdong Lin 		 * not both or neither.
1298923c5e61SMengdong Lin 		 */
129934614739SJerome Brunet 		if (!!codec->name == !!codec->of_node) {
1300923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1301923c5e61SMengdong Lin 				link->name);
1302923c5e61SMengdong Lin 			return -EINVAL;
1303923c5e61SMengdong Lin 		}
1304af18b13fSJerome Brunet 
1305923c5e61SMengdong Lin 		/* Codec DAI name must be specified */
13063db769f1SKuninori Morimoto 		if (!codec->dai_name) {
1307923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1308923c5e61SMengdong Lin 				link->name);
1309923c5e61SMengdong Lin 			return -EINVAL;
1310923c5e61SMengdong Lin 		}
1311af18b13fSJerome Brunet 
1312af18b13fSJerome Brunet 		/*
1313af18b13fSJerome Brunet 		 * Defer card registration if codec component is not added to
1314af18b13fSJerome Brunet 		 * component list.
1315af18b13fSJerome Brunet 		 */
1316af18b13fSJerome Brunet 		if (!soc_find_component(codec))
1317af18b13fSJerome Brunet 			return -EPROBE_DEFER;
1318923c5e61SMengdong Lin 	}
1319923c5e61SMengdong Lin 
132034614739SJerome Brunet 	for_each_link_platforms(link, i, platform) {
13211d768989SKuninori Morimoto 		/*
132234614739SJerome Brunet 		 * Platform may be specified by either name or OF node, but it
132334614739SJerome Brunet 		 * can be left unspecified, then no components will be inserted
132434614739SJerome Brunet 		 * in the rtdcom list
13251d768989SKuninori Morimoto 		 */
132634614739SJerome Brunet 		if (!!platform->name == !!platform->of_node) {
1327910fdcabSKuninori Morimoto 			dev_err(card->dev,
132834614739SJerome Brunet 				"ASoC: Neither/both platform name/of_node are set for %s\n",
1329923c5e61SMengdong Lin 				link->name);
1330923c5e61SMengdong Lin 			return -EINVAL;
1331923c5e61SMengdong Lin 		}
13328780cf11SAjit Pandey 
13338780cf11SAjit Pandey 		/*
133434614739SJerome Brunet 		 * Defer card registration if platform component is not added to
133534614739SJerome Brunet 		 * component list.
13368780cf11SAjit Pandey 		 */
133734614739SJerome Brunet 		if (!soc_find_component(platform))
13388780cf11SAjit Pandey 			return -EPROBE_DEFER;
1339923c5e61SMengdong Lin 	}
1340923c5e61SMengdong Lin 
1341923c5e61SMengdong Lin 	/* FIXME */
134208a5841eSKuninori Morimoto 	if (link->num_cpus > 1) {
1343923c5e61SMengdong Lin 		dev_err(card->dev,
134408a5841eSKuninori Morimoto 			"ASoC: multi cpu is not yet supported %s\n",
1345923c5e61SMengdong Lin 			link->name);
1346923c5e61SMengdong Lin 		return -EINVAL;
1347923c5e61SMengdong Lin 	}
1348923c5e61SMengdong Lin 
1349923c5e61SMengdong Lin 	/*
1350923c5e61SMengdong Lin 	 * CPU device may be specified by either name or OF node, but
1351923c5e61SMengdong Lin 	 * can be left unspecified, and will be matched based on DAI
1352923c5e61SMengdong Lin 	 * name alone..
1353923c5e61SMengdong Lin 	 */
135408a5841eSKuninori Morimoto 	if (link->cpus->name && link->cpus->of_node) {
1355923c5e61SMengdong Lin 		dev_err(card->dev,
1356923c5e61SMengdong Lin 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
1357923c5e61SMengdong Lin 			link->name);
1358923c5e61SMengdong Lin 		return -EINVAL;
1359923c5e61SMengdong Lin 	}
13608780cf11SAjit Pandey 
13618780cf11SAjit Pandey 	/*
13628780cf11SAjit Pandey 	 * Defer card registartion if cpu dai component is not added to
13638780cf11SAjit Pandey 	 * component list.
13648780cf11SAjit Pandey 	 */
136508a5841eSKuninori Morimoto 	if ((link->cpus->of_node || link->cpus->name) &&
1366c1e230f0SKuninori Morimoto 	    !soc_find_component(link->cpus))
13678780cf11SAjit Pandey 		return -EPROBE_DEFER;
13688780cf11SAjit Pandey 
1369923c5e61SMengdong Lin 	/*
1370923c5e61SMengdong Lin 	 * At least one of CPU DAI name or CPU device name/node must be
1371923c5e61SMengdong Lin 	 * specified
1372923c5e61SMengdong Lin 	 */
137308a5841eSKuninori Morimoto 	if (!link->cpus->dai_name &&
137408a5841eSKuninori Morimoto 	    !(link->cpus->name || link->cpus->of_node)) {
1375923c5e61SMengdong Lin 		dev_err(card->dev,
1376923c5e61SMengdong Lin 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1377923c5e61SMengdong Lin 			link->name);
1378923c5e61SMengdong Lin 		return -EINVAL;
1379923c5e61SMengdong Lin 	}
1380923c5e61SMengdong Lin 
1381923c5e61SMengdong Lin 	return 0;
1382923c5e61SMengdong Lin }
1383923c5e61SMengdong Lin 
1384ef2e8175SKuninori Morimoto void snd_soc_disconnect_sync(struct device *dev)
1385ef2e8175SKuninori Morimoto {
13862c7b696aSMarcel Ziswiler 	struct snd_soc_component *component =
13872c7b696aSMarcel Ziswiler 			snd_soc_lookup_component(dev, NULL);
1388ef2e8175SKuninori Morimoto 
1389ef2e8175SKuninori Morimoto 	if (!component || !component->card)
1390ef2e8175SKuninori Morimoto 		return;
1391ef2e8175SKuninori Morimoto 
1392ef2e8175SKuninori Morimoto 	snd_card_disconnect_sync(component->card->snd_card);
1393ef2e8175SKuninori Morimoto }
1394df532185SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
1395ef2e8175SKuninori Morimoto 
1396f8f80361SMengdong Lin /**
1397f8f80361SMengdong Lin  * snd_soc_add_dai_link - Add a DAI link dynamically
1398f8f80361SMengdong Lin  * @card: The ASoC card to which the DAI link is added
1399f8f80361SMengdong Lin  * @dai_link: The new DAI link to add
1400f8f80361SMengdong Lin  *
1401f8f80361SMengdong Lin  * This function adds a DAI link to the ASoC card's link list.
1402f8f80361SMengdong Lin  *
1403f8f80361SMengdong Lin  * Note: Topology can use this API to add DAI links when probing the
1404f8f80361SMengdong Lin  * topology component. And machine drivers can still define static
1405f8f80361SMengdong Lin  * DAI links in dai_link array.
1406f8f80361SMengdong Lin  */
1407f8f80361SMengdong Lin int snd_soc_add_dai_link(struct snd_soc_card *card,
1408f8f80361SMengdong Lin 		struct snd_soc_dai_link *dai_link)
1409f8f80361SMengdong Lin {
1410f8f80361SMengdong Lin 	if (dai_link->dobj.type
1411f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1412f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1413f8f80361SMengdong Lin 			dai_link->dobj.type);
1414f8f80361SMengdong Lin 		return -EINVAL;
1415f8f80361SMengdong Lin 	}
1416f8f80361SMengdong Lin 
1417f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
14182c7b696aSMarcel Ziswiler 	/*
14192c7b696aSMarcel Ziswiler 	 * Notify the machine driver for extra initialization
1420d6f220eaSMengdong Lin 	 * on the link created by topology.
1421d6f220eaSMengdong Lin 	 */
1422d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->add_dai_link)
1423d6f220eaSMengdong Lin 		card->add_dai_link(card, dai_link);
1424d6f220eaSMengdong Lin 
14256634e3d6SKuninori Morimoto 	/* see for_each_card_links */
1426f8f80361SMengdong Lin 	list_add_tail(&dai_link->list, &card->dai_link_list);
1427f8f80361SMengdong Lin 
1428f8f80361SMengdong Lin 	return 0;
1429f8f80361SMengdong Lin }
1430f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1431f8f80361SMengdong Lin 
1432f8f80361SMengdong Lin /**
1433f8f80361SMengdong Lin  * snd_soc_remove_dai_link - Remove a DAI link from the list
1434f8f80361SMengdong Lin  * @card: The ASoC card that owns the link
1435f8f80361SMengdong Lin  * @dai_link: The DAI link to remove
1436f8f80361SMengdong Lin  *
1437f8f80361SMengdong Lin  * This function removes a DAI link from the ASoC card's link list.
1438f8f80361SMengdong Lin  *
1439f8f80361SMengdong Lin  * For DAI links previously added by topology, topology should
1440f8f80361SMengdong Lin  * remove them by using the dobj embedded in the link.
1441f8f80361SMengdong Lin  */
1442f8f80361SMengdong Lin void snd_soc_remove_dai_link(struct snd_soc_card *card,
1443f8f80361SMengdong Lin 			     struct snd_soc_dai_link *dai_link)
1444f8f80361SMengdong Lin {
1445f8f80361SMengdong Lin 	if (dai_link->dobj.type
1446f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1447f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1448f8f80361SMengdong Lin 			dai_link->dobj.type);
1449f8f80361SMengdong Lin 		return;
1450f8f80361SMengdong Lin 	}
1451f8f80361SMengdong Lin 
1452f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
14532c7b696aSMarcel Ziswiler 	/*
14542c7b696aSMarcel Ziswiler 	 * Notify the machine driver for extra destruction
1455d6f220eaSMengdong Lin 	 * on the link created by topology.
1456d6f220eaSMengdong Lin 	 */
1457d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->remove_dai_link)
1458d6f220eaSMengdong Lin 		card->remove_dai_link(card, dai_link);
1459d6f220eaSMengdong Lin 
1460c26a8841SKuninori Morimoto 	list_del(&dai_link->list);
1461f8f80361SMengdong Lin }
1462f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
14630671fd8eSKuninori Morimoto 
146425f7b701SArnaud Pouliquen static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
146525f7b701SArnaud Pouliquen 				struct snd_soc_pcm_runtime *rtd)
146625f7b701SArnaud Pouliquen {
146725f7b701SArnaud Pouliquen 	int i, ret = 0;
146825f7b701SArnaud Pouliquen 
146925f7b701SArnaud Pouliquen 	for (i = 0; i < num_dais; ++i) {
147025f7b701SArnaud Pouliquen 		struct snd_soc_dai_driver *drv = dais[i]->driver;
147125f7b701SArnaud Pouliquen 
1472de17f14eSRohit kumar 		if (drv->pcm_new)
147325f7b701SArnaud Pouliquen 			ret = drv->pcm_new(rtd, dais[i]);
147425f7b701SArnaud Pouliquen 		if (ret < 0) {
147525f7b701SArnaud Pouliquen 			dev_err(dais[i]->dev,
147625f7b701SArnaud Pouliquen 				"ASoC: Failed to bind %s with pcm device\n",
147725f7b701SArnaud Pouliquen 				dais[i]->name);
147825f7b701SArnaud Pouliquen 			return ret;
147925f7b701SArnaud Pouliquen 		}
148025f7b701SArnaud Pouliquen 	}
148125f7b701SArnaud Pouliquen 
148225f7b701SArnaud Pouliquen 	return 0;
148325f7b701SArnaud Pouliquen }
148425f7b701SArnaud Pouliquen 
1485c4b46982SKuninori Morimoto static int soc_link_init(struct snd_soc_card *card,
1486c4b46982SKuninori Morimoto 			 struct snd_soc_pcm_runtime *rtd)
1487c4b46982SKuninori Morimoto {
1488c4b46982SKuninori Morimoto 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1489c4b46982SKuninori Morimoto 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1490c4b46982SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
1491c4b46982SKuninori Morimoto 	struct snd_soc_component *component;
1492c4b46982SKuninori Morimoto 	int ret, num;
1493c4b46982SKuninori Morimoto 
1494c4b46982SKuninori Morimoto 	/* set default power off timeout */
1495c4b46982SKuninori Morimoto 	rtd->pmdown_time = pmdown_time;
14960168bf0dSLiam Girdwood 
14975f3484acSLars-Peter Clausen 	/* do machine specific initialization */
14985f3484acSLars-Peter Clausen 	if (dai_link->init) {
14995f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
15005f3484acSLars-Peter Clausen 		if (ret < 0) {
15015f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
15025f3484acSLars-Peter Clausen 				dai_link->name, ret);
15035f3484acSLars-Peter Clausen 			return ret;
15045f3484acSLars-Peter Clausen 		}
15055f3484acSLars-Peter Clausen 	}
15065f3484acSLars-Peter Clausen 
150740aa5383SRicard Wanderlof 	if (dai_link->dai_fmt) {
150840aa5383SRicard Wanderlof 		ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
150940aa5383SRicard Wanderlof 		if (ret)
151040aa5383SRicard Wanderlof 			return ret;
151140aa5383SRicard Wanderlof 	}
1512a5053a8eSKuninori Morimoto 
15135f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
15142e55b90aSLars-Peter Clausen 	soc_dpcm_debugfs_add(rtd);
15155f3484acSLars-Peter Clausen 
1516a655de80SLiam Girdwood 	num = rtd->num;
1517a655de80SLiam Girdwood 
1518a655de80SLiam Girdwood 	/*
1519a655de80SLiam Girdwood 	 * most drivers will register their PCMs using DAI link ordering but
1520a655de80SLiam Girdwood 	 * topology based drivers can use the DAI link id field to set PCM
1521a655de80SLiam Girdwood 	 * device number and then use rtd + a base offset of the BEs.
1522a655de80SLiam Girdwood 	 */
1523a655de80SLiam Girdwood 	for_each_rtdcom(rtd, rtdcom) {
1524a655de80SLiam Girdwood 		component = rtdcom->component;
1525a655de80SLiam Girdwood 
1526a655de80SLiam Girdwood 		if (!component->driver->use_dai_pcm_id)
1527a655de80SLiam Girdwood 			continue;
1528a655de80SLiam Girdwood 
1529a655de80SLiam Girdwood 		if (rtd->dai_link->no_pcm)
1530a655de80SLiam Girdwood 			num += component->driver->be_pcm_base;
1531a655de80SLiam Girdwood 		else
1532a655de80SLiam Girdwood 			num = rtd->dai_link->id;
1533a655de80SLiam Girdwood 	}
1534a655de80SLiam Girdwood 
1535b423c420SKuninori Morimoto 	/* create compress_device if possible */
1536b423c420SKuninori Morimoto 	ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1537b423c420SKuninori Morimoto 	if (ret != -ENOTSUPP) {
1538b423c420SKuninori Morimoto 		if (ret < 0)
1539f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
15401245b700SNamarta Kohli 					 dai_link->stream_name);
15411245b700SNamarta Kohli 		return ret;
15421245b700SNamarta Kohli 	}
1543b423c420SKuninori Morimoto 
1544f0fba2adSLiam Girdwood 	/* create the pcm */
1545a655de80SLiam Girdwood 	ret = soc_new_pcm(rtd, num);
1546f0fba2adSLiam Girdwood 	if (ret < 0) {
1547f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
15480837fc62SFabio Estevam 			dai_link->stream_name, ret);
1549f0fba2adSLiam Girdwood 		return ret;
1550f0fba2adSLiam Girdwood 	}
155125f7b701SArnaud Pouliquen 	ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
155225f7b701SArnaud Pouliquen 	if (ret < 0)
155325f7b701SArnaud Pouliquen 		return ret;
155425f7b701SArnaud Pouliquen 	ret = soc_link_dai_pcm_new(rtd->codec_dais,
155525f7b701SArnaud Pouliquen 				   rtd->num_codecs, rtd);
155625f7b701SArnaud Pouliquen 	return ret;
1557f0fba2adSLiam Girdwood }
1558f0fba2adSLiam Girdwood 
1559e8fbd250SKuninori Morimoto static void soc_unbind_aux_dev(struct snd_soc_card *card)
15604893a2ebSKuninori Morimoto {
1561e8fbd250SKuninori Morimoto 	struct snd_soc_component *component, *_component;
1562e8fbd250SKuninori Morimoto 
1563e8fbd250SKuninori Morimoto 	for_each_card_auxs_safe(card, component, _component) {
15644893a2ebSKuninori Morimoto 		component->init = NULL;
15654893a2ebSKuninori Morimoto 		list_del(&component->card_aux_list);
15664893a2ebSKuninori Morimoto 	}
1567e8fbd250SKuninori Morimoto }
15684893a2ebSKuninori Morimoto 
1569bee886f1SKuninori Morimoto static int soc_bind_aux_dev(struct snd_soc_card *card)
1570b19e6e7bSMark Brown {
1571f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1572bee886f1SKuninori Morimoto 	struct snd_soc_aux_dev *aux;
1573bee886f1SKuninori Morimoto 	int i;
15743ca041edSSebastian Reichel 
1575bee886f1SKuninori Morimoto 	for_each_card_pre_auxs(card, i, aux) {
1576f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1577bee886f1SKuninori Morimoto 		component = soc_find_component(&aux->dlc);
1578f2ed6b07SMengdong Lin 		if (!component)
15793dc29b8bSKuninori Morimoto 			return -EPROBE_DEFER;
15803ca041edSSebastian Reichel 
1581bee886f1SKuninori Morimoto 		component->init = aux->init;
1582c2b71c71SKuninori Morimoto 		/* see for_each_card_auxs */
1583d2e3a135SSylwester Nawrocki 		list_add(&component->card_aux_list, &card->aux_comp_list);
1584bee886f1SKuninori Morimoto 	}
1585f2ed6b07SMengdong Lin 	return 0;
1586b19e6e7bSMark Brown }
1587b19e6e7bSMark Brown 
1588f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1589f2ed6b07SMengdong Lin {
1590991454e1SKuninori Morimoto 	struct snd_soc_component *comp;
1591f2ed6b07SMengdong Lin 	int order;
1592f2ed6b07SMengdong Lin 	int ret;
1593f2ed6b07SMengdong Lin 
15941a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1595c2b71c71SKuninori Morimoto 		for_each_card_auxs(card, comp) {
1596f2ed6b07SMengdong Lin 			if (comp->driver->probe_order == order) {
1597f2ed6b07SMengdong Lin 				ret = soc_probe_component(card,	comp);
1598f2ed6b07SMengdong Lin 				if (ret < 0) {
1599f2ed6b07SMengdong Lin 					dev_err(card->dev,
1600f2ed6b07SMengdong Lin 						"ASoC: failed to probe aux component %s %d\n",
1601f2ed6b07SMengdong Lin 						comp->name, ret);
1602f2ed6b07SMengdong Lin 					return ret;
1603f2ed6b07SMengdong Lin 				}
1604f2ed6b07SMengdong Lin 			}
1605f2ed6b07SMengdong Lin 		}
1606f2ed6b07SMengdong Lin 	}
160765d9361fSLars-Peter Clausen 
160844c69bb1SLars-Peter Clausen 	return 0;
16093ca041edSSebastian Reichel }
16102eea392dSJarkko Nikula 
1611f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
161244c69bb1SLars-Peter Clausen {
1613f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1614f2ed6b07SMengdong Lin 	int order;
161544c69bb1SLars-Peter Clausen 
16161a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1617c2b71c71SKuninori Morimoto 		for_each_card_auxs_safe(card, comp, _comp) {
1618e8fbd250SKuninori Morimoto 			if (comp->driver->remove_order == order)
1619f2ed6b07SMengdong Lin 				soc_remove_component(comp);
16205f3484acSLars-Peter Clausen 		}
16212eea392dSJarkko Nikula 	}
16222eea392dSJarkko Nikula }
16232eea392dSJarkko Nikula 
1624ce64c8b9SLars-Peter Clausen /**
1625ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1626ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1627ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1628ce64c8b9SLars-Peter Clausen  *
1629ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1630ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1631ce64c8b9SLars-Peter Clausen  *
1632ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1633ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1634ce64c8b9SLars-Peter Clausen  *
1635ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1636ce64c8b9SLars-Peter Clausen  */
1637ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1638ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1639ce64c8b9SLars-Peter Clausen {
1640ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
16410b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1642ce64c8b9SLars-Peter Clausen 	unsigned int i;
1643ce64c8b9SLars-Peter Clausen 	int ret;
1644ce64c8b9SLars-Peter Clausen 
16450b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1646ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1647ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1648ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1649ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1650ce64c8b9SLars-Peter Clausen 			return ret;
1651ce64c8b9SLars-Peter Clausen 		}
1652ce64c8b9SLars-Peter Clausen 	}
1653ce64c8b9SLars-Peter Clausen 
16542c7b696aSMarcel Ziswiler 	/*
16552c7b696aSMarcel Ziswiler 	 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
16562c7b696aSMarcel Ziswiler 	 * the component which has non_legacy_dai_naming is Codec
16572c7b696aSMarcel Ziswiler 	 */
1658999f7f5aSKuninori Morimoto 	if (cpu_dai->component->driver->non_legacy_dai_naming) {
1659ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1660ce64c8b9SLars-Peter Clausen 
1661ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1662ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1663ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1664ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1665ce64c8b9SLars-Peter Clausen 			break;
1666ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1667ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1668ce64c8b9SLars-Peter Clausen 			break;
1669ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1670ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1671ce64c8b9SLars-Peter Clausen 			break;
1672ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1673ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1674ce64c8b9SLars-Peter Clausen 			break;
1675ce64c8b9SLars-Peter Clausen 		}
1676ce64c8b9SLars-Peter Clausen 
1677ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1678ce64c8b9SLars-Peter Clausen 	}
1679ce64c8b9SLars-Peter Clausen 
1680ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1681ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1682ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1683ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1684ce64c8b9SLars-Peter Clausen 		return ret;
1685ce64c8b9SLars-Peter Clausen 	}
1686ce64c8b9SLars-Peter Clausen 
1687ce64c8b9SLars-Peter Clausen 	return 0;
1688ce64c8b9SLars-Peter Clausen }
1689ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1690ce64c8b9SLars-Peter Clausen 
16911f5a4535STakashi Iwai #ifdef CONFIG_DMI
16922c7b696aSMarcel Ziswiler /*
16932c7b696aSMarcel Ziswiler  * Trim special characters, and replace '-' with '_' since '-' is used to
1694345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1695345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1696345233d7SLiam Girdwood  */
1697345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1698345233d7SLiam Girdwood {
1699345233d7SLiam Girdwood 	int i, j = 0;
1700345233d7SLiam Girdwood 
1701345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1702345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1703345233d7SLiam Girdwood 		    || (name[i] == '_'))
1704345233d7SLiam Girdwood 			name[j++] = name[i];
1705345233d7SLiam Girdwood 		else if (name[i] == '-')
1706345233d7SLiam Girdwood 			name[j++] = '_';
1707345233d7SLiam Girdwood 	}
1708345233d7SLiam Girdwood 
1709345233d7SLiam Girdwood 	name[j] = '\0';
1710345233d7SLiam Girdwood }
1711345233d7SLiam Girdwood 
17122c7b696aSMarcel Ziswiler /*
17132c7b696aSMarcel Ziswiler  * Check if a DMI field is valid, i.e. not containing any string
171498faf436SMengdong Lin  * in the black list.
171598faf436SMengdong Lin  */
171698faf436SMengdong Lin static int is_dmi_valid(const char *field)
171798faf436SMengdong Lin {
171898faf436SMengdong Lin 	int i = 0;
171998faf436SMengdong Lin 
172098faf436SMengdong Lin 	while (dmi_blacklist[i]) {
172198faf436SMengdong Lin 		if (strstr(field, dmi_blacklist[i]))
172298faf436SMengdong Lin 			return 0;
172398faf436SMengdong Lin 		i++;
172446b5a4d2SWu Fengguang 	}
172598faf436SMengdong Lin 
172698faf436SMengdong Lin 	return 1;
172798faf436SMengdong Lin }
172898faf436SMengdong Lin 
1729345233d7SLiam Girdwood /**
1730345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
1731345233d7SLiam Girdwood  * @card: The card to register DMI names
1732345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
1733345233d7SLiam Girdwood  *
1734345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
1735345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
1736345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
1737345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
1738345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
1739345233d7SLiam Girdwood  * as the sound card long name, in the format of
1740345233d7SLiam Girdwood  * "vendor-product-version-board"
1741345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
1742345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
1743345233d7SLiam Girdwood  * (UCM) configurations for the card.
1744345233d7SLiam Girdwood  *
1745345233d7SLiam Girdwood  * Possible card long names may be:
1746345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
1747345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1748345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1749345233d7SLiam Girdwood  *
1750345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
1751345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
1752345233d7SLiam Girdwood  *
1753345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
1754345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
1755345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
1756345233d7SLiam Girdwood  * charactors like SPACE.
1757345233d7SLiam Girdwood  *
1758345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
1759345233d7SLiam Girdwood  */
1760345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1761345233d7SLiam Girdwood {
1762345233d7SLiam Girdwood 	const char *vendor, *product, *product_version, *board;
1763345233d7SLiam Girdwood 	size_t longname_buf_size = sizeof(card->snd_card->longname);
1764345233d7SLiam Girdwood 	size_t len;
1765345233d7SLiam Girdwood 
1766345233d7SLiam Girdwood 	if (card->long_name)
1767345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
1768345233d7SLiam Girdwood 
1769345233d7SLiam Girdwood 	/* make up dmi long name as: vendor.product.version.board */
1770345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
177198faf436SMengdong Lin 	if (!vendor || !is_dmi_valid(vendor)) {
1772345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1773345233d7SLiam Girdwood 		return 0;
1774345233d7SLiam Girdwood 	}
1775345233d7SLiam Girdwood 
1776345233d7SLiam Girdwood 	snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1777345233d7SLiam Girdwood 			 "%s", vendor);
1778345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
1779345233d7SLiam Girdwood 
1780345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
178198faf436SMengdong Lin 	if (product && is_dmi_valid(product)) {
1782345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1783345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1784345233d7SLiam Girdwood 			 longname_buf_size - len,
1785345233d7SLiam Girdwood 			 "-%s", product);
1786345233d7SLiam Girdwood 
1787345233d7SLiam Girdwood 		len++;	/* skip the separator "-" */
1788345233d7SLiam Girdwood 		if (len < longname_buf_size)
1789345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1790345233d7SLiam Girdwood 
17912c7b696aSMarcel Ziswiler 		/*
17922c7b696aSMarcel Ziswiler 		 * some vendors like Lenovo may only put a self-explanatory
1793345233d7SLiam Girdwood 		 * name in the product version field
1794345233d7SLiam Girdwood 		 */
1795345233d7SLiam Girdwood 		product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
179698faf436SMengdong Lin 		if (product_version && is_dmi_valid(product_version)) {
1797345233d7SLiam Girdwood 			len = strlen(card->dmi_longname);
1798345233d7SLiam Girdwood 			snprintf(card->dmi_longname + len,
1799345233d7SLiam Girdwood 				 longname_buf_size - len,
1800345233d7SLiam Girdwood 				 "-%s", product_version);
1801345233d7SLiam Girdwood 
1802345233d7SLiam Girdwood 			len++;
1803345233d7SLiam Girdwood 			if (len < longname_buf_size)
1804345233d7SLiam Girdwood 				cleanup_dmi_name(card->dmi_longname + len);
1805345233d7SLiam Girdwood 		}
1806345233d7SLiam Girdwood 	}
1807345233d7SLiam Girdwood 
1808345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
180998faf436SMengdong Lin 	if (board && is_dmi_valid(board)) {
1810345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1811345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1812345233d7SLiam Girdwood 			 longname_buf_size - len,
1813345233d7SLiam Girdwood 			 "-%s", board);
1814345233d7SLiam Girdwood 
1815345233d7SLiam Girdwood 		len++;
1816345233d7SLiam Girdwood 		if (len < longname_buf_size)
1817345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1818345233d7SLiam Girdwood 	} else if (!product) {
1819345233d7SLiam Girdwood 		/* fall back to using legacy name */
1820345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1821345233d7SLiam Girdwood 		return 0;
1822345233d7SLiam Girdwood 	}
1823345233d7SLiam Girdwood 
1824345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
1825345233d7SLiam Girdwood 	if (flavour) {
1826345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1827345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1828345233d7SLiam Girdwood 			 longname_buf_size - len,
1829345233d7SLiam Girdwood 			 "-%s", flavour);
1830345233d7SLiam Girdwood 
1831345233d7SLiam Girdwood 		len++;
1832345233d7SLiam Girdwood 		if (len < longname_buf_size)
1833345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1834345233d7SLiam Girdwood 	}
1835345233d7SLiam Girdwood 
1836345233d7SLiam Girdwood 	/* set the card long name */
1837345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
1838345233d7SLiam Girdwood 
1839345233d7SLiam Girdwood 	return 0;
1840345233d7SLiam Girdwood }
1841345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
18421f5a4535STakashi Iwai #endif /* CONFIG_DMI */
1843345233d7SLiam Girdwood 
1844a655de80SLiam Girdwood static void soc_check_tplg_fes(struct snd_soc_card *card)
1845a655de80SLiam Girdwood {
1846a655de80SLiam Girdwood 	struct snd_soc_component *component;
1847a655de80SLiam Girdwood 	const struct snd_soc_component_driver *comp_drv;
1848a655de80SLiam Girdwood 	struct snd_soc_dai_link *dai_link;
1849a655de80SLiam Girdwood 	int i;
1850a655de80SLiam Girdwood 
1851368dee94SKuninori Morimoto 	for_each_component(component) {
1852a655de80SLiam Girdwood 
185349f9c4f2SDaniel Baluta 		/* does this component override BEs ? */
1854a655de80SLiam Girdwood 		if (!component->driver->ignore_machine)
1855a655de80SLiam Girdwood 			continue;
1856a655de80SLiam Girdwood 
1857a655de80SLiam Girdwood 		/* for this machine ? */
1858e194098bSPierre-Louis Bossart 		if (!strcmp(component->driver->ignore_machine,
1859a655de80SLiam Girdwood 			    card->dev->driver->name))
1860e194098bSPierre-Louis Bossart 			goto match;
1861e194098bSPierre-Louis Bossart 		if (strcmp(component->driver->ignore_machine,
1862e194098bSPierre-Louis Bossart 			   dev_name(card->dev)))
1863a655de80SLiam Girdwood 			continue;
1864e194098bSPierre-Louis Bossart match:
1865a655de80SLiam Girdwood 		/* machine matches, so override the rtd data */
18667fe072b4SKuninori Morimoto 		for_each_card_prelinks(card, i, dai_link) {
1867a655de80SLiam Girdwood 
1868a655de80SLiam Girdwood 			/* ignore this FE */
1869a655de80SLiam Girdwood 			if (dai_link->dynamic) {
1870a655de80SLiam Girdwood 				dai_link->ignore = true;
1871a655de80SLiam Girdwood 				continue;
1872a655de80SLiam Girdwood 			}
1873a655de80SLiam Girdwood 
187449f9c4f2SDaniel Baluta 			dev_info(card->dev, "info: override BE DAI link %s\n",
1875a655de80SLiam Girdwood 				 card->dai_link[i].name);
1876a655de80SLiam Girdwood 
1877a655de80SLiam Girdwood 			/* override platform component */
1878adb76b5bSKuninori Morimoto 			if (!dai_link->platforms) {
1879daecf46eSKuninori Morimoto 				dev_err(card->dev, "init platform error");
1880daecf46eSKuninori Morimoto 				continue;
1881daecf46eSKuninori Morimoto 			}
1882910fdcabSKuninori Morimoto 			dai_link->platforms->name = component->name;
1883a655de80SLiam Girdwood 
1884a655de80SLiam Girdwood 			/* convert non BE into BE */
1885a655de80SLiam Girdwood 			dai_link->no_pcm = 1;
1886a655de80SLiam Girdwood 
1887a655de80SLiam Girdwood 			/* override any BE fixups */
1888a655de80SLiam Girdwood 			dai_link->be_hw_params_fixup =
1889a655de80SLiam Girdwood 				component->driver->be_hw_params_fixup;
1890a655de80SLiam Girdwood 
18912c7b696aSMarcel Ziswiler 			/*
18922c7b696aSMarcel Ziswiler 			 * most BE links don't set stream name, so set it to
1893a655de80SLiam Girdwood 			 * dai link name if it's NULL to help bind widgets.
1894a655de80SLiam Girdwood 			 */
1895a655de80SLiam Girdwood 			if (!dai_link->stream_name)
1896a655de80SLiam Girdwood 				dai_link->stream_name = dai_link->name;
1897a655de80SLiam Girdwood 		}
1898a655de80SLiam Girdwood 
1899a655de80SLiam Girdwood 		/* Inform userspace we are using alternate topology */
1900a655de80SLiam Girdwood 		if (component->driver->topology_name_prefix) {
1901a655de80SLiam Girdwood 
1902a655de80SLiam Girdwood 			/* topology shortname created? */
1903a655de80SLiam Girdwood 			if (!card->topology_shortname_created) {
1904a655de80SLiam Girdwood 				comp_drv = component->driver;
1905a655de80SLiam Girdwood 
1906a655de80SLiam Girdwood 				snprintf(card->topology_shortname, 32, "%s-%s",
1907a655de80SLiam Girdwood 					 comp_drv->topology_name_prefix,
1908a655de80SLiam Girdwood 					 card->name);
1909a655de80SLiam Girdwood 				card->topology_shortname_created = true;
1910a655de80SLiam Girdwood 			}
1911a655de80SLiam Girdwood 
1912a655de80SLiam Girdwood 			/* use topology shortname */
1913a655de80SLiam Girdwood 			card->name = card->topology_shortname;
1914a655de80SLiam Girdwood 		}
1915a655de80SLiam Girdwood 	}
1916a655de80SLiam Girdwood }
1917a655de80SLiam Girdwood 
19180f23f718SKuninori Morimoto #define soc_setup_card_name(name, name1, name2, norm)		\
19190f23f718SKuninori Morimoto 	__soc_setup_card_name(name, sizeof(name), name1, name2, norm)
19200f23f718SKuninori Morimoto static void __soc_setup_card_name(char *name, int len,
19210f23f718SKuninori Morimoto 				  const char *name1, const char *name2,
19220f23f718SKuninori Morimoto 				  int normalization)
19230f23f718SKuninori Morimoto {
19240f23f718SKuninori Morimoto 	int i;
19250f23f718SKuninori Morimoto 
19260f23f718SKuninori Morimoto 	snprintf(name, len, "%s", name1 ? name1 : name2);
19270f23f718SKuninori Morimoto 
19280f23f718SKuninori Morimoto 	if (!normalization)
19290f23f718SKuninori Morimoto 		return;
19300f23f718SKuninori Morimoto 
19310f23f718SKuninori Morimoto 	/*
19320f23f718SKuninori Morimoto 	 * Name normalization
19330f23f718SKuninori Morimoto 	 *
19340f23f718SKuninori Morimoto 	 * The driver name is somewhat special, as it's used as a key for
19350f23f718SKuninori Morimoto 	 * searches in the user-space.
19360f23f718SKuninori Morimoto 	 *
19370f23f718SKuninori Morimoto 	 * ex)
19380f23f718SKuninori Morimoto 	 *	"abcd??efg" -> "abcd__efg"
19390f23f718SKuninori Morimoto 	 */
19400f23f718SKuninori Morimoto 	for (i = 0; i < len; i++) {
19410f23f718SKuninori Morimoto 		switch (name[i]) {
19420f23f718SKuninori Morimoto 		case '_':
19430f23f718SKuninori Morimoto 		case '-':
19440f23f718SKuninori Morimoto 		case '\0':
19450f23f718SKuninori Morimoto 			break;
19460f23f718SKuninori Morimoto 		default:
19470f23f718SKuninori Morimoto 			if (!isalnum(name[i]))
19480f23f718SKuninori Morimoto 				name[i] = '_';
19490f23f718SKuninori Morimoto 			break;
19500f23f718SKuninori Morimoto 		}
19510f23f718SKuninori Morimoto 	}
19520f23f718SKuninori Morimoto }
19530f23f718SKuninori Morimoto 
1954a4de83a3SKuninori Morimoto static void soc_cleanup_card_resources(struct snd_soc_card *card)
195553e947a0SKuninori Morimoto {
19567ce6088fSKuninori Morimoto 	struct snd_soc_dai_link *link, *_link;
19577ce6088fSKuninori Morimoto 
195853e947a0SKuninori Morimoto 	/* free the ALSA card at first; this syncs with pending operations */
195929040d1aSKuninori Morimoto 	if (card->snd_card) {
196053e947a0SKuninori Morimoto 		snd_card_free(card->snd_card);
196129040d1aSKuninori Morimoto 		card->snd_card = NULL;
196229040d1aSKuninori Morimoto 	}
196353e947a0SKuninori Morimoto 
196453e947a0SKuninori Morimoto 	/* remove and free each DAI */
19657ce6088fSKuninori Morimoto 	soc_remove_link_dais(card);
19667ce6088fSKuninori Morimoto 	soc_remove_link_components(card);
19677ce6088fSKuninori Morimoto 
19687ce6088fSKuninori Morimoto 	for_each_card_links_safe(card, link, _link)
19697ce6088fSKuninori Morimoto 		snd_soc_remove_dai_link(card, link);
19707ce6088fSKuninori Morimoto 
197153e947a0SKuninori Morimoto 	soc_remove_pcm_runtimes(card);
197253e947a0SKuninori Morimoto 
197353e947a0SKuninori Morimoto 	/* remove auxiliary devices */
197453e947a0SKuninori Morimoto 	soc_remove_aux_devices(card);
1975e8fbd250SKuninori Morimoto 	soc_unbind_aux_dev(card);
197653e947a0SKuninori Morimoto 
197753e947a0SKuninori Morimoto 	snd_soc_dapm_free(&card->dapm);
197853e947a0SKuninori Morimoto 	soc_cleanup_card_debugfs(card);
197953e947a0SKuninori Morimoto 
198053e947a0SKuninori Morimoto 	/* remove the card */
198153e947a0SKuninori Morimoto 	if (card->remove)
198253e947a0SKuninori Morimoto 		card->remove(card);
198353e947a0SKuninori Morimoto }
198453e947a0SKuninori Morimoto 
1985b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1986f0fba2adSLiam Girdwood {
19871a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
198861b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
1989c7e73774SKuninori Morimoto 	int ret, i;
199096dd3622SMark Brown 
199134e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
199270fc5373STzung-Bi Shih 	for_each_card_prelinks(card, i, dai_link) {
199370fc5373STzung-Bi Shih 		ret = soc_init_dai_link(card, dai_link);
199470fc5373STzung-Bi Shih 		if (ret) {
199570fc5373STzung-Bi Shih 			dev_err(card->dev, "ASoC: failed to init link %s: %d\n",
199670fc5373STzung-Bi Shih 				dai_link->name, ret);
199770fc5373STzung-Bi Shih 			mutex_unlock(&client_mutex);
199870fc5373STzung-Bi Shih 			return ret;
199970fc5373STzung-Bi Shih 		}
200070fc5373STzung-Bi Shih 	}
200101b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
2002f0fba2adSLiam Girdwood 
200395c267ddSKuninori Morimoto 	snd_soc_dapm_init(&card->dapm, card, NULL);
200453e947a0SKuninori Morimoto 
2005a655de80SLiam Girdwood 	/* check whether any platform is ignore machine FE and using topology */
2006a655de80SLiam Girdwood 	soc_check_tplg_fes(card);
2007a655de80SLiam Girdwood 
2008b19e6e7bSMark Brown 	/* bind DAIs */
20097fe072b4SKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link) {
20107fe072b4SKuninori Morimoto 		ret = soc_bind_dai_link(card, dai_link);
2011b19e6e7bSMark Brown 		if (ret != 0)
201253e947a0SKuninori Morimoto 			goto probe_end;
2013db2a4165SFrank Mandarino 	}
2014db2a4165SFrank Mandarino 
201544c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
2016bee886f1SKuninori Morimoto 	ret = soc_bind_aux_dev(card);
2017bee886f1SKuninori Morimoto 	if (ret < 0)
201853e947a0SKuninori Morimoto 		goto probe_end;
2019db2a4165SFrank Mandarino 
2020f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
2021d8145989SKuninori Morimoto 	card->num_rtd = 0;
20225b99a0aaSKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link) {
20235b99a0aaSKuninori Morimoto 		ret = snd_soc_add_dai_link(card, dai_link);
20245b99a0aaSKuninori Morimoto 		if (ret < 0)
20255b99a0aaSKuninori Morimoto 			goto probe_end;
20265b99a0aaSKuninori Morimoto 	}
2027f8f80361SMengdong Lin 
2028f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
2029102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2030f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
2031f0fba2adSLiam Girdwood 	if (ret < 0) {
203210e8aa9aSMichał Mirosław 		dev_err(card->dev,
203310e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
203410e8aa9aSMichał Mirosław 			card->name, ret);
203553e947a0SKuninori Morimoto 		goto probe_end;
2036db2a4165SFrank Mandarino 	}
2037db2a4165SFrank Mandarino 
20380757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
20390757d834SLars-Peter Clausen 
2040b3da4251SKuninori Morimoto 	soc_resume_init(card);
20416ed25978SAndy Green 
2042b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
20439a841ebbSMark Brown 					card->num_dapm_widgets);
2044b8ba3b57SKuninori Morimoto 	if (ret < 0)
2045b8ba3b57SKuninori Morimoto 		goto probe_end;
20469a841ebbSMark Brown 
2047b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2048f23e860eSNicolin Chen 					card->num_of_dapm_widgets);
2049b8ba3b57SKuninori Morimoto 	if (ret < 0)
2050b8ba3b57SKuninori Morimoto 		goto probe_end;
2051f23e860eSNicolin Chen 
2052f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
2053f0fba2adSLiam Girdwood 	if (card->probe) {
2054e7361ec4SMark Brown 		ret = card->probe(card);
2055f0fba2adSLiam Girdwood 		if (ret < 0)
205653e947a0SKuninori Morimoto 			goto probe_end;
2057f0fba2adSLiam Girdwood 	}
2058f0fba2adSLiam Girdwood 
205962ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
206062f07a6bSKuninori Morimoto 	ret = soc_probe_link_components(card);
206162ae68faSStephen Warren 	if (ret < 0) {
2062f110bfc7SLiam Girdwood 		dev_err(card->dev,
206362f07a6bSKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
206453e947a0SKuninori Morimoto 		goto probe_end;
206562ae68faSStephen Warren 	}
206662ae68faSStephen Warren 
2067f2ed6b07SMengdong Lin 	/* probe auxiliary components */
2068f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
2069f2ed6b07SMengdong Lin 	if (ret < 0)
207053e947a0SKuninori Morimoto 		goto probe_end;
2071f2ed6b07SMengdong Lin 
20722c7b696aSMarcel Ziswiler 	/*
20732c7b696aSMarcel Ziswiler 	 * Find new DAI links added during probing components and bind them.
207461b0088bSMengdong Lin 	 * Components with topology may bring new DAIs and DAI links.
207561b0088bSMengdong Lin 	 */
207698061fdbSKuninori Morimoto 	for_each_card_links(card, dai_link) {
207761b0088bSMengdong Lin 		if (soc_is_dai_link_bound(card, dai_link))
207861b0088bSMengdong Lin 			continue;
207961b0088bSMengdong Lin 
208061b0088bSMengdong Lin 		ret = soc_init_dai_link(card, dai_link);
208161b0088bSMengdong Lin 		if (ret)
208253e947a0SKuninori Morimoto 			goto probe_end;
208361b0088bSMengdong Lin 		ret = soc_bind_dai_link(card, dai_link);
208461b0088bSMengdong Lin 		if (ret)
208553e947a0SKuninori Morimoto 			goto probe_end;
208661b0088bSMengdong Lin 	}
208761b0088bSMengdong Lin 
208862ae68faSStephen Warren 	/* probe all DAI links on this card */
2089c7e73774SKuninori Morimoto 	ret = soc_probe_link_dais(card);
2090fe3e78e0SMark Brown 	if (ret < 0) {
2091f110bfc7SLiam Girdwood 		dev_err(card->dev,
2092c7e73774SKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
209353e947a0SKuninori Morimoto 		goto probe_end;
2094fe3e78e0SMark Brown 	}
2095fe3e78e0SMark Brown 
2096c4b46982SKuninori Morimoto 	for_each_card_rtds(card, rtd)
2097c4b46982SKuninori Morimoto 		soc_link_init(card, rtd);
2098c4b46982SKuninori Morimoto 
2099888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
2100b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
2101888df395SMark Brown 
21029b98c7c2SKuninori Morimoto 	ret = snd_soc_add_card_controls(card, card->controls,
21032c7b696aSMarcel Ziswiler 					card->num_controls);
21049b98c7c2SKuninori Morimoto 	if (ret < 0)
21059b98c7c2SKuninori Morimoto 		goto probe_end;
2106b7af1dafSMark Brown 
2107daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2108b8ad29deSMark Brown 				      card->num_dapm_routes);
2109daa480bdSKuninori Morimoto 	if (ret < 0)
2110daa480bdSKuninori Morimoto 		goto probe_end;
2111b8ad29deSMark Brown 
2112daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2113f23e860eSNicolin Chen 				      card->num_of_dapm_routes);
2114daa480bdSKuninori Morimoto 	if (ret < 0)
2115daa480bdSKuninori Morimoto 		goto probe_end;
211675d9ac46SMark Brown 
2117861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
2118861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
2119861886d3STakashi Iwai 
21200f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->shortname,
21210f23f718SKuninori Morimoto 			    card->name, NULL, 0);
21220f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->longname,
21230f23f718SKuninori Morimoto 			    card->long_name, card->name, 0);
21240f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->driver,
21250f23f718SKuninori Morimoto 			    card->driver_name, card->name, 1);
2126fe3e78e0SMark Brown 
212728e9ad92SMark Brown 	if (card->late_probe) {
212828e9ad92SMark Brown 		ret = card->late_probe(card);
212928e9ad92SMark Brown 		if (ret < 0) {
2130f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
213128e9ad92SMark Brown 				card->name, ret);
213253e947a0SKuninori Morimoto 			goto probe_end;
213328e9ad92SMark Brown 		}
213428e9ad92SMark Brown 	}
213528e9ad92SMark Brown 
2136824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
21378c193b8dSLars-Peter Clausen 
2138f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2139fe3e78e0SMark Brown 	if (ret < 0) {
2140f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2141f110bfc7SLiam Girdwood 				ret);
214253e947a0SKuninori Morimoto 		goto probe_end;
2143fe3e78e0SMark Brown 	}
2144fe3e78e0SMark Brown 
2145435c5e25SMark Brown 	card->instantiated = 1;
2146882eab6cSTzung-Bi Shih 	dapm_mark_endpoints_dirty(card);
21474f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2148b19e6e7bSMark Brown 
214953e947a0SKuninori Morimoto probe_end:
215053e947a0SKuninori Morimoto 	if (ret < 0)
215153e947a0SKuninori Morimoto 		soc_cleanup_card_resources(card);
2152db2a4165SFrank Mandarino 
2153f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
215434e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2155db2a4165SFrank Mandarino 
2156b19e6e7bSMark Brown 	return ret;
2157435c5e25SMark Brown }
2158435c5e25SMark Brown 
2159435c5e25SMark Brown /* probes a new socdev */
2160435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2161435c5e25SMark Brown {
2162f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2163435c5e25SMark Brown 
216470a7ca34SVinod Koul 	/*
216570a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
216670a7ca34SVinod Koul 	 * we should not be here in that case so ret error
216770a7ca34SVinod Koul 	 */
216870a7ca34SVinod Koul 	if (!card)
216970a7ca34SVinod Koul 		return -EINVAL;
217070a7ca34SVinod Koul 
2171fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2172f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2173fe4085e8SMark Brown 		 card->name);
2174fe4085e8SMark Brown 
2175435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2176435c5e25SMark Brown 	card->dev = &pdev->dev;
2177f0fba2adSLiam Girdwood 
217828d528c8SMark Brown 	return snd_soc_register_card(card);
2179435c5e25SMark Brown }
2180435c5e25SMark Brown 
2181b0e26485SVinod Koul /* removes a socdev */
2182b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2183b0e26485SVinod Koul {
2184b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2185b0e26485SVinod Koul 
2186c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2187db2a4165SFrank Mandarino 	return 0;
2188db2a4165SFrank Mandarino }
2189db2a4165SFrank Mandarino 
21906f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
219151737470SMark Brown {
21926f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
21931a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
219451737470SMark Brown 
219551737470SMark Brown 	if (!card->instantiated)
2196416356fcSMark Brown 		return 0;
219751737470SMark Brown 
21982c7b696aSMarcel Ziswiler 	/*
21992c7b696aSMarcel Ziswiler 	 * Flush out pmdown_time work - we actually do want to run it
22002c7b696aSMarcel Ziswiler 	 * now, we're shutting down so no imminent restart.
22012c7b696aSMarcel Ziswiler 	 */
220265462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
220351737470SMark Brown 
2204f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2205416356fcSMark Brown 
2206988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
2207bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
220888bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
22090b7990e3SKuninori Morimoto 		struct snd_soc_dai *codec_dai;
22101a497983SMengdong Lin 		int i;
221188bd870fSBenoit Cousson 
2212988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
22130b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
221488bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
221588bd870fSBenoit Cousson 		}
2216988e8cc4SNicolin Chen 	}
2217988e8cc4SNicolin Chen 
2218416356fcSMark Brown 	return 0;
221951737470SMark Brown }
22206f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
222151737470SMark Brown 
22226f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2223b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2224b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2225b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2226b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
22276f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2228b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2229416356fcSMark Brown };
2230deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2231416356fcSMark Brown 
2232db2a4165SFrank Mandarino /* ASoC platform driver */
2233db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2234db2a4165SFrank Mandarino 	.driver		= {
2235db2a4165SFrank Mandarino 		.name		= "soc-audio",
22366f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2237db2a4165SFrank Mandarino 	},
2238db2a4165SFrank Mandarino 	.probe		= soc_probe,
2239db2a4165SFrank Mandarino 	.remove		= soc_remove,
2240db2a4165SFrank Mandarino };
2241db2a4165SFrank Mandarino 
2242096e49d5SMark Brown /**
2243db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2244db2a4165SFrank Mandarino  * @_template: control template
2245db2a4165SFrank Mandarino  * @data: control private data
2246ac11a2b3SMark Brown  * @long_name: control long name
2247efb7ac3fSMark Brown  * @prefix: control name prefix
2248db2a4165SFrank Mandarino  *
2249db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2250db2a4165SFrank Mandarino  *
2251db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2252db2a4165SFrank Mandarino  */
2253db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
22543056557fSMark Brown 				  void *data, const char *long_name,
2255efb7ac3fSMark Brown 				  const char *prefix)
2256db2a4165SFrank Mandarino {
2257db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2258efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2259efb7ac3fSMark Brown 	char *name = NULL;
2260db2a4165SFrank Mandarino 
2261db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2262db2a4165SFrank Mandarino 	template.index = 0;
2263db2a4165SFrank Mandarino 
2264efb7ac3fSMark Brown 	if (!long_name)
2265efb7ac3fSMark Brown 		long_name = template.name;
2266efb7ac3fSMark Brown 
2267efb7ac3fSMark Brown 	if (prefix) {
22682b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2269efb7ac3fSMark Brown 		if (!name)
2270efb7ac3fSMark Brown 			return NULL;
2271efb7ac3fSMark Brown 
2272efb7ac3fSMark Brown 		template.name = name;
2273efb7ac3fSMark Brown 	} else {
2274efb7ac3fSMark Brown 		template.name = long_name;
2275efb7ac3fSMark Brown 	}
2276efb7ac3fSMark Brown 
2277efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2278efb7ac3fSMark Brown 
2279efb7ac3fSMark Brown 	kfree(name);
2280efb7ac3fSMark Brown 
2281efb7ac3fSMark Brown 	return kcontrol;
2282db2a4165SFrank Mandarino }
2283db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2284db2a4165SFrank Mandarino 
2285022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2286022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2287022658beSLiam Girdwood 	const char *prefix, void *data)
2288022658beSLiam Girdwood {
2289022658beSLiam Girdwood 	int err, i;
2290022658beSLiam Girdwood 
2291022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2292022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
22932c7b696aSMarcel Ziswiler 
2294022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2295022658beSLiam Girdwood 						     control->name, prefix));
2296022658beSLiam Girdwood 		if (err < 0) {
2297f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2298f110bfc7SLiam Girdwood 				control->name, err);
2299022658beSLiam Girdwood 			return err;
2300022658beSLiam Girdwood 		}
2301022658beSLiam Girdwood 	}
2302022658beSLiam Girdwood 
2303022658beSLiam Girdwood 	return 0;
2304022658beSLiam Girdwood }
2305022658beSLiam Girdwood 
23064fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
23074fefd698SDimitris Papastamos 					       const char *name)
23084fefd698SDimitris Papastamos {
23094fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
23104fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
23114fefd698SDimitris Papastamos 
23124fefd698SDimitris Papastamos 	if (unlikely(!name))
23134fefd698SDimitris Papastamos 		return NULL;
23144fefd698SDimitris Papastamos 
23154fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
23164fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
23174fefd698SDimitris Papastamos 			return kctl;
23184fefd698SDimitris Papastamos 	return NULL;
23194fefd698SDimitris Papastamos }
23204fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
23214fefd698SDimitris Papastamos 
2322db2a4165SFrank Mandarino /**
23230f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
23240f2780adSLars-Peter Clausen  *
23250f2780adSLars-Peter Clausen  * @component: Component to add controls to
23260f2780adSLars-Peter Clausen  * @controls: Array of controls to add
23270f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
23280f2780adSLars-Peter Clausen  *
23290f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
23300f2780adSLars-Peter Clausen  */
23310f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
23320f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
23330f2780adSLars-Peter Clausen {
23340f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
23350f2780adSLars-Peter Clausen 
23360f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
23370f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
23380f2780adSLars-Peter Clausen }
23390f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
23400f2780adSLars-Peter Clausen 
23410f2780adSLars-Peter Clausen /**
2342022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2343022658beSLiam Girdwood  * Convenience function to add a list of controls.
2344022658beSLiam Girdwood  *
2345022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2346022658beSLiam Girdwood  * @controls: array of controls to add
2347022658beSLiam Girdwood  * @num_controls: number of elements in the array
2348022658beSLiam Girdwood  *
2349022658beSLiam Girdwood  * Return 0 for success, else error.
2350022658beSLiam Girdwood  */
2351022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2352022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2353022658beSLiam Girdwood {
2354022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2355022658beSLiam Girdwood 
2356022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2357022658beSLiam Girdwood 			NULL, soc_card);
2358022658beSLiam Girdwood }
2359022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2360022658beSLiam Girdwood 
2361022658beSLiam Girdwood /**
2362022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2363022658beSLiam Girdwood  * Convienience function to add a list of controls.
2364022658beSLiam Girdwood  *
2365022658beSLiam Girdwood  * @dai: DAI to add controls to
2366022658beSLiam Girdwood  * @controls: array of controls to add
2367022658beSLiam Girdwood  * @num_controls: number of elements in the array
2368022658beSLiam Girdwood  *
2369022658beSLiam Girdwood  * Return 0 for success, else error.
2370022658beSLiam Girdwood  */
2371022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2372022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2373022658beSLiam Girdwood {
2374313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2375022658beSLiam Girdwood 
2376022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2377022658beSLiam Girdwood 			NULL, dai);
2378022658beSLiam Girdwood }
2379022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2380022658beSLiam Girdwood 
2381e894efefSSrinivas Kandagatla static int snd_soc_bind_card(struct snd_soc_card *card)
2382e894efefSSrinivas Kandagatla {
2383e894efefSSrinivas Kandagatla 	struct snd_soc_pcm_runtime *rtd;
2384e894efefSSrinivas Kandagatla 	int ret;
2385e894efefSSrinivas Kandagatla 
2386e894efefSSrinivas Kandagatla 	ret = snd_soc_instantiate_card(card);
2387e894efefSSrinivas Kandagatla 	if (ret != 0)
2388e894efefSSrinivas Kandagatla 		return ret;
2389e894efefSSrinivas Kandagatla 
2390e894efefSSrinivas Kandagatla 	/* deactivate pins to sleep state */
2391bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
2392e894efefSSrinivas Kandagatla 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2393e894efefSSrinivas Kandagatla 		struct snd_soc_dai *codec_dai;
2394e894efefSSrinivas Kandagatla 		int j;
2395e894efefSSrinivas Kandagatla 
2396e894efefSSrinivas Kandagatla 		for_each_rtd_codec_dai(rtd, j, codec_dai) {
2397e894efefSSrinivas Kandagatla 			if (!codec_dai->active)
2398e894efefSSrinivas Kandagatla 				pinctrl_pm_select_sleep_state(codec_dai->dev);
2399e894efefSSrinivas Kandagatla 		}
2400e894efefSSrinivas Kandagatla 
2401e894efefSSrinivas Kandagatla 		if (!cpu_dai->active)
2402e894efefSSrinivas Kandagatla 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
2403e894efefSSrinivas Kandagatla 	}
2404e894efefSSrinivas Kandagatla 
2405e894efefSSrinivas Kandagatla 	return ret;
2406e894efefSSrinivas Kandagatla }
2407e894efefSSrinivas Kandagatla 
2408c5af3a2eSMark Brown /**
2409c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2410c5af3a2eSMark Brown  *
2411ac11a2b3SMark Brown  * @card: Card to register
2412c5af3a2eSMark Brown  *
2413c5af3a2eSMark Brown  */
241470a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2415c5af3a2eSMark Brown {
2416c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2417c5af3a2eSMark Brown 		return -EINVAL;
2418c5af3a2eSMark Brown 
2419ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2420ed77cc12SMark Brown 
2421b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->widgets);
2422b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->paths);
2423b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->dapm_list);
2424b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->aux_comp_list);
2425b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->component_dev_list);
2426b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->list);
2427f8f80361SMengdong Lin 	INIT_LIST_HEAD(&card->dai_link_list);
24281a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
2429db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
24308a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2431b03bfaecSKuninori Morimoto 
2432c5af3a2eSMark Brown 	card->instantiated = 0;
2433f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
2434a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
243572b745e3SPeter Ujfalusi 	mutex_init(&card->pcm_mutex);
2436a9764869SKaiChieh Chuang 	spin_lock_init(&card->dpcm_lock);
2437c5af3a2eSMark Brown 
2438e894efefSSrinivas Kandagatla 	return snd_soc_bind_card(card);
2439c5af3a2eSMark Brown }
244070a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2441c5af3a2eSMark Brown 
2442e894efefSSrinivas Kandagatla static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2443e894efefSSrinivas Kandagatla {
2444e894efefSSrinivas Kandagatla 	if (card->instantiated) {
2445e894efefSSrinivas Kandagatla 		card->instantiated = false;
2446e894efefSSrinivas Kandagatla 		snd_soc_dapm_shutdown(card);
244753e947a0SKuninori Morimoto 		snd_soc_flush_all_delayed_work(card);
2448f96fb7d1SRanjani Sridharan 
2449f96fb7d1SRanjani Sridharan 		/* remove all components used by DAI links on this card */
2450b006c0c6SKuninori Morimoto 		soc_remove_link_components(card);
2451f96fb7d1SRanjani Sridharan 
2452e894efefSSrinivas Kandagatla 		soc_cleanup_card_resources(card);
2453e894efefSSrinivas Kandagatla 		if (!unregister)
2454e894efefSSrinivas Kandagatla 			list_add(&card->list, &unbind_card_list);
2455e894efefSSrinivas Kandagatla 	} else {
2456e894efefSSrinivas Kandagatla 		if (unregister)
2457e894efefSSrinivas Kandagatla 			list_del(&card->list);
2458e894efefSSrinivas Kandagatla 	}
2459e894efefSSrinivas Kandagatla }
2460e894efefSSrinivas Kandagatla 
2461c5af3a2eSMark Brown /**
2462c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2463c5af3a2eSMark Brown  *
2464ac11a2b3SMark Brown  * @card: Card to unregister
2465c5af3a2eSMark Brown  *
2466c5af3a2eSMark Brown  */
246770a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2468c5af3a2eSMark Brown {
2469b545542aSKuninori Morimoto 	mutex_lock(&client_mutex);
2470e894efefSSrinivas Kandagatla 	snd_soc_unbind_card(card, true);
2471b545542aSKuninori Morimoto 	mutex_unlock(&client_mutex);
2472f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2473c5af3a2eSMark Brown 
2474c5af3a2eSMark Brown 	return 0;
2475c5af3a2eSMark Brown }
247670a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2477c5af3a2eSMark Brown 
2478f0fba2adSLiam Girdwood /*
2479f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
2480f0fba2adSLiam Girdwood  * and sanitizing names.
2481f0fba2adSLiam Girdwood  */
24820b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2483f0fba2adSLiam Girdwood {
2484f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
2485f0fba2adSLiam Girdwood 	int id1, id2;
2486f0fba2adSLiam Girdwood 
2487f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
2488f0fba2adSLiam Girdwood 		return NULL;
2489f0fba2adSLiam Girdwood 
249058818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2491f0fba2adSLiam Girdwood 
2492f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
2493f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
2494f0fba2adSLiam Girdwood 	if (found) {
2495f0fba2adSLiam Girdwood 		/* get ID */
2496f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2497f0fba2adSLiam Girdwood 
2498f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
2499f0fba2adSLiam Girdwood 			if (*id == -1)
2500f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
2501f0fba2adSLiam Girdwood 		}
2502f0fba2adSLiam Girdwood 
2503f0fba2adSLiam Girdwood 	} else {
2504f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr" */
2505f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2506f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
2507f0fba2adSLiam Girdwood 
2508f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
250905899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
2510f0fba2adSLiam Girdwood 
2511f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
25122c7b696aSMarcel Ziswiler 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
25132c7b696aSMarcel Ziswiler 				 name);
251458818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2515f0fba2adSLiam Girdwood 		} else
2516f0fba2adSLiam Girdwood 			*id = 0;
2517f0fba2adSLiam Girdwood 	}
2518f0fba2adSLiam Girdwood 
251950014499SKuninori Morimoto 	return devm_kstrdup(dev, name, GFP_KERNEL);
2520f0fba2adSLiam Girdwood }
2521f0fba2adSLiam Girdwood 
2522f0fba2adSLiam Girdwood /*
2523f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
2524f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
2525f0fba2adSLiam Girdwood  */
2526f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
2527f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
2528f0fba2adSLiam Girdwood {
2529f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
253010e8aa9aSMichał Mirosław 		dev_err(dev,
253110e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
253210e8aa9aSMichał Mirosław 			dev_name(dev));
2533f0fba2adSLiam Girdwood 		return NULL;
2534f0fba2adSLiam Girdwood 	}
2535f0fba2adSLiam Girdwood 
253650014499SKuninori Morimoto 	return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2537f0fba2adSLiam Girdwood }
2538f0fba2adSLiam Girdwood 
25399115171aSMark Brown /**
254032c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
25419115171aSMark Brown  *
254232c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
25439115171aSMark Brown  */
254432c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
25459115171aSMark Brown {
25465c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
25479115171aSMark Brown 
254815a0c645SKuninori Morimoto 	for_each_component_dais_safe(component, dai, _dai) {
254932c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
255032c9ba54SLars-Peter Clausen 			dai->name);
25519115171aSMark Brown 		list_del(&dai->list);
25529115171aSMark Brown 	}
255332c9ba54SLars-Peter Clausen }
25549115171aSMark Brown 
25555e4fb372SMengdong Lin /* Create a DAI and add it to the component's DAI list */
25565e4fb372SMengdong Lin static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
25575e4fb372SMengdong Lin 	struct snd_soc_dai_driver *dai_drv,
25585e4fb372SMengdong Lin 	bool legacy_dai_naming)
25595e4fb372SMengdong Lin {
25605e4fb372SMengdong Lin 	struct device *dev = component->dev;
25615e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
25625e4fb372SMengdong Lin 
25635e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
25645e4fb372SMengdong Lin 
256550014499SKuninori Morimoto 	dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
25665e4fb372SMengdong Lin 	if (dai == NULL)
25675e4fb372SMengdong Lin 		return NULL;
25685e4fb372SMengdong Lin 
25695e4fb372SMengdong Lin 	/*
25705e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
25715e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
25725e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
25735e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
25745e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
25755e4fb372SMengdong Lin 	 * component-less anymore.
25765e4fb372SMengdong Lin 	 */
25775e4fb372SMengdong Lin 	if (legacy_dai_naming &&
25785e4fb372SMengdong Lin 	    (dai_drv->id == 0 || dai_drv->name == NULL)) {
25795e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
25805e4fb372SMengdong Lin 	} else {
25815e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
25825e4fb372SMengdong Lin 		if (dai_drv->id)
25835e4fb372SMengdong Lin 			dai->id = dai_drv->id;
25845e4fb372SMengdong Lin 		else
25855e4fb372SMengdong Lin 			dai->id = component->num_dai;
25865e4fb372SMengdong Lin 	}
258750014499SKuninori Morimoto 	if (!dai->name)
25885e4fb372SMengdong Lin 		return NULL;
25895e4fb372SMengdong Lin 
25905e4fb372SMengdong Lin 	dai->component = component;
25915e4fb372SMengdong Lin 	dai->dev = dev;
25925e4fb372SMengdong Lin 	dai->driver = dai_drv;
25935e4fb372SMengdong Lin 	if (!dai->driver->ops)
25945e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
25955e4fb372SMengdong Lin 
259615a0c645SKuninori Morimoto 	/* see for_each_component_dais */
259758bf4179SKuninori Morimoto 	list_add_tail(&dai->list, &component->dai_list);
25985e4fb372SMengdong Lin 	component->num_dai++;
25995e4fb372SMengdong Lin 
26005e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
26015e4fb372SMengdong Lin 	return dai;
26025e4fb372SMengdong Lin }
26035e4fb372SMengdong Lin 
26049115171aSMark Brown /**
260532c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
26069115171aSMark Brown  *
26076106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
26086106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
2609ac11a2b3SMark Brown  * @count: Number of DAIs
26109115171aSMark Brown  */
261132c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
26122c7b696aSMarcel Ziswiler 				 struct snd_soc_dai_driver *dai_drv,
26132c7b696aSMarcel Ziswiler 				 size_t count)
26149115171aSMark Brown {
26156106d129SLars-Peter Clausen 	struct device *dev = component->dev;
2616f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
261732c9ba54SLars-Peter Clausen 	unsigned int i;
261832c9ba54SLars-Peter Clausen 	int ret;
2619f0fba2adSLiam Girdwood 
26205b5e0928SAlexey Dobriyan 	dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
26219115171aSMark Brown 
26229115171aSMark Brown 	for (i = 0; i < count; i++) {
2623f0fba2adSLiam Girdwood 
26242c7b696aSMarcel Ziswiler 		dai = soc_add_dai(component, dai_drv + i, count == 1 &&
26252c7b696aSMarcel Ziswiler 				  !component->driver->non_legacy_dai_naming);
2626c46e0079SAxel Lin 		if (dai == NULL) {
2627c46e0079SAxel Lin 			ret = -ENOMEM;
2628c46e0079SAxel Lin 			goto err;
2629c46e0079SAxel Lin 		}
2630f0fba2adSLiam Girdwood 	}
2631f0fba2adSLiam Girdwood 
26329115171aSMark Brown 	return 0;
26339115171aSMark Brown 
26349115171aSMark Brown err:
263532c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
26369115171aSMark Brown 
26379115171aSMark Brown 	return ret;
26389115171aSMark Brown }
26399115171aSMark Brown 
264068003e6cSMengdong Lin /**
264168003e6cSMengdong Lin  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
264268003e6cSMengdong Lin  *
264368003e6cSMengdong Lin  * @component: The component the DAIs are registered for
264468003e6cSMengdong Lin  * @dai_drv: DAI driver to use for the DAI
264568003e6cSMengdong Lin  *
264668003e6cSMengdong Lin  * Topology can use this API to register DAIs when probing a component.
264768003e6cSMengdong Lin  * These DAIs's widgets will be freed in the card cleanup and the DAIs
264868003e6cSMengdong Lin  * will be freed in the component cleanup.
264968003e6cSMengdong Lin  */
265068003e6cSMengdong Lin int snd_soc_register_dai(struct snd_soc_component *component,
265168003e6cSMengdong Lin 	struct snd_soc_dai_driver *dai_drv)
265268003e6cSMengdong Lin {
265368003e6cSMengdong Lin 	struct snd_soc_dapm_context *dapm =
265468003e6cSMengdong Lin 		snd_soc_component_get_dapm(component);
265568003e6cSMengdong Lin 	struct snd_soc_dai *dai;
265668003e6cSMengdong Lin 	int ret;
265768003e6cSMengdong Lin 
265868003e6cSMengdong Lin 	if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
265968003e6cSMengdong Lin 		dev_err(component->dev, "Invalid dai type %d\n",
266068003e6cSMengdong Lin 			dai_drv->dobj.type);
266168003e6cSMengdong Lin 		return -EINVAL;
266268003e6cSMengdong Lin 	}
266368003e6cSMengdong Lin 
266468003e6cSMengdong Lin 	lockdep_assert_held(&client_mutex);
266568003e6cSMengdong Lin 	dai = soc_add_dai(component, dai_drv, false);
266668003e6cSMengdong Lin 	if (!dai)
266768003e6cSMengdong Lin 		return -ENOMEM;
266868003e6cSMengdong Lin 
26692c7b696aSMarcel Ziswiler 	/*
26702c7b696aSMarcel Ziswiler 	 * Create the DAI widgets here. After adding DAIs, topology may
267168003e6cSMengdong Lin 	 * also add routes that need these widgets as source or sink.
267268003e6cSMengdong Lin 	 */
267368003e6cSMengdong Lin 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
267468003e6cSMengdong Lin 	if (ret != 0) {
267568003e6cSMengdong Lin 		dev_err(component->dev,
267668003e6cSMengdong Lin 			"Failed to create DAI widgets %d\n", ret);
267768003e6cSMengdong Lin 	}
267868003e6cSMengdong Lin 
267968003e6cSMengdong Lin 	return ret;
268068003e6cSMengdong Lin }
268168003e6cSMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_register_dai);
268268003e6cSMengdong Lin 
2683bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
2684bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
2685d191bd8dSKuninori Morimoto {
26868d92bb51SKuninori Morimoto 	INIT_LIST_HEAD(&component->dai_list);
2687495efdb0SKuninori Morimoto 	INIT_LIST_HEAD(&component->dobj_list);
2688495efdb0SKuninori Morimoto 	INIT_LIST_HEAD(&component->card_list);
26898d92bb51SKuninori Morimoto 	mutex_init(&component->io_mutex);
26908d92bb51SKuninori Morimoto 
2691bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
2692bb13109dSLars-Peter Clausen 	if (!component->name) {
2693bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
2694d191bd8dSKuninori Morimoto 		return -ENOMEM;
2695d191bd8dSKuninori Morimoto 	}
2696d191bd8dSKuninori Morimoto 
2697bb13109dSLars-Peter Clausen 	component->dev = dev;
2698bb13109dSLars-Peter Clausen 	component->driver = driver;
2699e2c330b9SLars-Peter Clausen 
2700bb13109dSLars-Peter Clausen 	return 0;
2701d191bd8dSKuninori Morimoto }
2702d191bd8dSKuninori Morimoto 
270320feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2704886f5692SLars-Peter Clausen {
2705886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
270620feb881SLars-Peter Clausen 
2707886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
2708886f5692SLars-Peter Clausen 	if (val_bytes > 0)
2709886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
2710886f5692SLars-Peter Clausen }
271120feb881SLars-Peter Clausen 
2712e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
2713e874bf5fSLars-Peter Clausen 
271420feb881SLars-Peter Clausen /**
27152c7b696aSMarcel Ziswiler  * snd_soc_component_init_regmap() - Initialize regmap instance for the
27162c7b696aSMarcel Ziswiler  *                                   component
271720feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
271820feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
271920feb881SLars-Peter Clausen  *
272020feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
272120feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
272220feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
272320feb881SLars-Peter Clausen  * before the first IO attempt of the component.
272420feb881SLars-Peter Clausen  */
272520feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
272620feb881SLars-Peter Clausen 	struct regmap *regmap)
272720feb881SLars-Peter Clausen {
272820feb881SLars-Peter Clausen 	component->regmap = regmap;
272920feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
2730886f5692SLars-Peter Clausen }
273120feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
273220feb881SLars-Peter Clausen 
273320feb881SLars-Peter Clausen /**
27342c7b696aSMarcel Ziswiler  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
27352c7b696aSMarcel Ziswiler  *                                   component
273620feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
273720feb881SLars-Peter Clausen  *
273820feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
273920feb881SLars-Peter Clausen  * removes the regmap instance from the component.
274020feb881SLars-Peter Clausen  *
274120feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
274220feb881SLars-Peter Clausen  * to initialize the regmap instance.
274320feb881SLars-Peter Clausen  */
274420feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
274520feb881SLars-Peter Clausen {
274620feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
274720feb881SLars-Peter Clausen 	component->regmap = NULL;
274820feb881SLars-Peter Clausen }
274920feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2750886f5692SLars-Peter Clausen 
2751e874bf5fSLars-Peter Clausen #endif
2752d191bd8dSKuninori Morimoto 
2753359c71eeSKuninori Morimoto static void snd_soc_component_add(struct snd_soc_component *component)
2754bb13109dSLars-Peter Clausen {
2755359c71eeSKuninori Morimoto 	mutex_lock(&client_mutex);
2756359c71eeSKuninori Morimoto 
2757999f7f5aSKuninori Morimoto 	if (!component->driver->write && !component->driver->read) {
275820feb881SLars-Peter Clausen 		if (!component->regmap)
27592c7b696aSMarcel Ziswiler 			component->regmap = dev_get_regmap(component->dev,
27602c7b696aSMarcel Ziswiler 							   NULL);
276120feb881SLars-Peter Clausen 		if (component->regmap)
276220feb881SLars-Peter Clausen 			snd_soc_component_setup_regmap(component);
276320feb881SLars-Peter Clausen 	}
2764886f5692SLars-Peter Clausen 
2765368dee94SKuninori Morimoto 	/* see for_each_component */
2766bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
2767d191bd8dSKuninori Morimoto 
2768d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
2769bb13109dSLars-Peter Clausen }
2770d191bd8dSKuninori Morimoto 
2771bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
2772bb13109dSLars-Peter Clausen {
2773bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
2774bb13109dSLars-Peter Clausen }
2775d191bd8dSKuninori Morimoto 
2776bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2777bb13109dSLars-Peter Clausen {
2778c12c1aadSKuninori Morimoto 	struct snd_soc_card *card = component->card;
2779c12c1aadSKuninori Morimoto 
2780c12c1aadSKuninori Morimoto 	if (card)
2781e894efefSSrinivas Kandagatla 		snd_soc_unbind_card(card, false);
2782c12c1aadSKuninori Morimoto 
2783bb13109dSLars-Peter Clausen 	list_del(&component->list);
2784bb13109dSLars-Peter Clausen }
2785d191bd8dSKuninori Morimoto 
2786273d778eSKuninori Morimoto #define ENDIANNESS_MAP(name) \
2787273d778eSKuninori Morimoto 	(SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2788273d778eSKuninori Morimoto static u64 endianness_format_map[] = {
2789273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S16_),
2790273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U16_),
2791273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_),
2792273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_),
2793273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S32_),
2794273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U32_),
2795273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_3),
2796273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_3),
2797273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S20_3),
2798273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U20_3),
2799273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S18_3),
2800273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U18_3),
2801273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT_),
2802273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT64_),
2803273d778eSKuninori Morimoto 	ENDIANNESS_MAP(IEC958_SUBFRAME_),
2804273d778eSKuninori Morimoto };
2805273d778eSKuninori Morimoto 
2806273d778eSKuninori Morimoto /*
2807273d778eSKuninori Morimoto  * Fix up the DAI formats for endianness: codecs don't actually see
2808273d778eSKuninori Morimoto  * the endianness of the data but we're using the CPU format
2809273d778eSKuninori Morimoto  * definitions which do need to include endianness so we ensure that
2810273d778eSKuninori Morimoto  * codec DAIs always have both big and little endian variants set.
2811273d778eSKuninori Morimoto  */
2812273d778eSKuninori Morimoto static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2813273d778eSKuninori Morimoto {
2814273d778eSKuninori Morimoto 	int i;
2815273d778eSKuninori Morimoto 
2816273d778eSKuninori Morimoto 	for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2817273d778eSKuninori Morimoto 		if (stream->formats & endianness_format_map[i])
2818273d778eSKuninori Morimoto 			stream->formats |= endianness_format_map[i];
2819273d778eSKuninori Morimoto }
2820273d778eSKuninori Morimoto 
2821e894efefSSrinivas Kandagatla static void snd_soc_try_rebind_card(void)
2822e894efefSSrinivas Kandagatla {
2823e894efefSSrinivas Kandagatla 	struct snd_soc_card *card, *c;
2824e894efefSSrinivas Kandagatla 
2825b245d273SKuninori Morimoto 	list_for_each_entry_safe(card, c, &unbind_card_list, list)
2826e894efefSSrinivas Kandagatla 		if (!snd_soc_bind_card(card))
2827e894efefSSrinivas Kandagatla 			list_del(&card->list);
2828e894efefSSrinivas Kandagatla }
2829e894efefSSrinivas Kandagatla 
2830e0dac41bSKuninori Morimoto int snd_soc_add_component(struct device *dev,
2831e0dac41bSKuninori Morimoto 			struct snd_soc_component *component,
2832cf9e829eSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2833d191bd8dSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2834d191bd8dSKuninori Morimoto 			int num_dai)
2835d191bd8dSKuninori Morimoto {
2836bb13109dSLars-Peter Clausen 	int ret;
2837273d778eSKuninori Morimoto 	int i;
2838d191bd8dSKuninori Morimoto 
2839cf9e829eSKuninori Morimoto 	ret = snd_soc_component_initialize(component, component_driver, dev);
2840bb13109dSLars-Peter Clausen 	if (ret)
2841bb13109dSLars-Peter Clausen 		goto err_free;
2842bb13109dSLars-Peter Clausen 
2843273d778eSKuninori Morimoto 	if (component_driver->endianness) {
2844273d778eSKuninori Morimoto 		for (i = 0; i < num_dai; i++) {
2845273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].playback);
2846273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].capture);
2847273d778eSKuninori Morimoto 		}
2848273d778eSKuninori Morimoto 	}
2849273d778eSKuninori Morimoto 
28500e7b25c6SKuninori Morimoto 	ret = snd_soc_register_dais(component, dai_drv, num_dai);
2851bb13109dSLars-Peter Clausen 	if (ret < 0) {
2852f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
2853bb13109dSLars-Peter Clausen 		goto err_cleanup;
2854bb13109dSLars-Peter Clausen 	}
2855bb13109dSLars-Peter Clausen 
2856cf9e829eSKuninori Morimoto 	snd_soc_component_add(component);
2857e894efefSSrinivas Kandagatla 	snd_soc_try_rebind_card();
2858bb13109dSLars-Peter Clausen 
2859bb13109dSLars-Peter Clausen 	return 0;
2860bb13109dSLars-Peter Clausen 
2861bb13109dSLars-Peter Clausen err_cleanup:
2862cf9e829eSKuninori Morimoto 	snd_soc_component_cleanup(component);
2863bb13109dSLars-Peter Clausen err_free:
2864bb13109dSLars-Peter Clausen 	return ret;
2865d191bd8dSKuninori Morimoto }
2866e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component);
2867e0dac41bSKuninori Morimoto 
2868e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev,
2869e0dac41bSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2870e0dac41bSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2871e0dac41bSKuninori Morimoto 			int num_dai)
2872e0dac41bSKuninori Morimoto {
2873e0dac41bSKuninori Morimoto 	struct snd_soc_component *component;
2874e0dac41bSKuninori Morimoto 
28757ecbd6a9SKuninori Morimoto 	component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
287608e61d03SKuninori Morimoto 	if (!component)
2877e0dac41bSKuninori Morimoto 		return -ENOMEM;
2878e0dac41bSKuninori Morimoto 
2879e0dac41bSKuninori Morimoto 	return snd_soc_add_component(dev, component, component_driver,
2880e0dac41bSKuninori Morimoto 				     dai_drv, num_dai);
2881e0dac41bSKuninori Morimoto }
2882d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
2883d191bd8dSKuninori Morimoto 
2884d191bd8dSKuninori Morimoto /**
28852eccea8cSKuninori Morimoto  * snd_soc_unregister_component - Unregister all related component
28862eccea8cSKuninori Morimoto  * from the ASoC core
2887d191bd8dSKuninori Morimoto  *
2888628536eaSJonathan Corbet  * @dev: The device to unregister
2889d191bd8dSKuninori Morimoto  */
28902eccea8cSKuninori Morimoto static int __snd_soc_unregister_component(struct device *dev)
2891d191bd8dSKuninori Morimoto {
2892cf9e829eSKuninori Morimoto 	struct snd_soc_component *component;
289321a03528SKuninori Morimoto 	int found = 0;
2894d191bd8dSKuninori Morimoto 
289534e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
2896368dee94SKuninori Morimoto 	for_each_component(component) {
2897999f7f5aSKuninori Morimoto 		if (dev != component->dev)
289821a03528SKuninori Morimoto 			continue;
2899d191bd8dSKuninori Morimoto 
29002c7b696aSMarcel Ziswiler 		snd_soc_tplg_component_remove(component,
29012c7b696aSMarcel Ziswiler 					      SND_SOC_TPLG_INDEX_ALL);
2902cf9e829eSKuninori Morimoto 		snd_soc_component_del_unlocked(component);
290321a03528SKuninori Morimoto 		found = 1;
290421a03528SKuninori Morimoto 		break;
2905d191bd8dSKuninori Morimoto 	}
2906d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
2907d191bd8dSKuninori Morimoto 
29082c7b696aSMarcel Ziswiler 	if (found)
2909cf9e829eSKuninori Morimoto 		snd_soc_component_cleanup(component);
29102eccea8cSKuninori Morimoto 
29112eccea8cSKuninori Morimoto 	return found;
29122eccea8cSKuninori Morimoto }
29132eccea8cSKuninori Morimoto 
29142eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
29152eccea8cSKuninori Morimoto {
29162c7b696aSMarcel Ziswiler 	while (__snd_soc_unregister_component(dev))
29172c7b696aSMarcel Ziswiler 		;
2918d191bd8dSKuninori Morimoto }
2919d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2920d191bd8dSKuninori Morimoto 
29217dd5d0d9SKuninori Morimoto struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
29227dd5d0d9SKuninori Morimoto 						   const char *driver_name)
29237dd5d0d9SKuninori Morimoto {
29247dd5d0d9SKuninori Morimoto 	struct snd_soc_component *component;
29257dd5d0d9SKuninori Morimoto 	struct snd_soc_component *ret;
29267dd5d0d9SKuninori Morimoto 
29277dd5d0d9SKuninori Morimoto 	ret = NULL;
29287dd5d0d9SKuninori Morimoto 	mutex_lock(&client_mutex);
2929368dee94SKuninori Morimoto 	for_each_component(component) {
29307dd5d0d9SKuninori Morimoto 		if (dev != component->dev)
29317dd5d0d9SKuninori Morimoto 			continue;
29327dd5d0d9SKuninori Morimoto 
29337dd5d0d9SKuninori Morimoto 		if (driver_name &&
29347dd5d0d9SKuninori Morimoto 		    (driver_name != component->driver->name) &&
29357dd5d0d9SKuninori Morimoto 		    (strcmp(component->driver->name, driver_name) != 0))
29367dd5d0d9SKuninori Morimoto 			continue;
29377dd5d0d9SKuninori Morimoto 
29387dd5d0d9SKuninori Morimoto 		ret = component;
29397dd5d0d9SKuninori Morimoto 		break;
29407dd5d0d9SKuninori Morimoto 	}
29417dd5d0d9SKuninori Morimoto 	mutex_unlock(&client_mutex);
29427dd5d0d9SKuninori Morimoto 
29437dd5d0d9SKuninori Morimoto 	return ret;
29447dd5d0d9SKuninori Morimoto }
29457dd5d0d9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
29467dd5d0d9SKuninori Morimoto 
2947bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
2948b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2949bec4fa05SStephen Warren 			       const char *propname)
2950bec4fa05SStephen Warren {
2951b07609ceSKuninori Morimoto 	struct device_node *np;
2952bec4fa05SStephen Warren 	int ret;
2953bec4fa05SStephen Warren 
29547e07e7c0STushar Behera 	if (!card->dev) {
29557e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
29567e07e7c0STushar Behera 		return -EINVAL;
29577e07e7c0STushar Behera 	}
29587e07e7c0STushar Behera 
29597e07e7c0STushar Behera 	np = card->dev->of_node;
29607e07e7c0STushar Behera 
2961bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
2962bec4fa05SStephen Warren 	/*
2963bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
2964bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
2965bec4fa05SStephen Warren 	 * snd_soc_register_card.
2966bec4fa05SStephen Warren 	 */
2967bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
2968bec4fa05SStephen Warren 		dev_err(card->dev,
2969f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
2970bec4fa05SStephen Warren 			propname, ret);
2971bec4fa05SStephen Warren 		return ret;
2972bec4fa05SStephen Warren 	}
2973bec4fa05SStephen Warren 
2974bec4fa05SStephen Warren 	return 0;
2975bec4fa05SStephen Warren }
2976b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2977bec4fa05SStephen Warren 
29789a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
29799a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
29809a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
29819a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
29829a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
29839a6d4860SXiubo Li };
29849a6d4860SXiubo Li 
298521efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
29869a6d4860SXiubo Li 					  const char *propname)
29879a6d4860SXiubo Li {
298821efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
29899a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
29909a6d4860SXiubo Li 	const char *template, *wname;
29919a6d4860SXiubo Li 	int i, j, num_widgets, ret;
29929a6d4860SXiubo Li 
29939a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
29949a6d4860SXiubo Li 	if (num_widgets < 0) {
29959a6d4860SXiubo Li 		dev_err(card->dev,
29969a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
29979a6d4860SXiubo Li 		return -EINVAL;
29989a6d4860SXiubo Li 	}
29999a6d4860SXiubo Li 	if (num_widgets & 1) {
30009a6d4860SXiubo Li 		dev_err(card->dev,
30019a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
30029a6d4860SXiubo Li 		return -EINVAL;
30039a6d4860SXiubo Li 	}
30049a6d4860SXiubo Li 
30059a6d4860SXiubo Li 	num_widgets /= 2;
30069a6d4860SXiubo Li 	if (!num_widgets) {
30079a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
30089a6d4860SXiubo Li 			propname);
30099a6d4860SXiubo Li 		return -EINVAL;
30109a6d4860SXiubo Li 	}
30119a6d4860SXiubo Li 
30129a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
30139a6d4860SXiubo Li 			       GFP_KERNEL);
30149a6d4860SXiubo Li 	if (!widgets) {
30159a6d4860SXiubo Li 		dev_err(card->dev,
30169a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
30179a6d4860SXiubo Li 		return -ENOMEM;
30189a6d4860SXiubo Li 	}
30199a6d4860SXiubo Li 
30209a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
30219a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
30229a6d4860SXiubo Li 			2 * i, &template);
30239a6d4860SXiubo Li 		if (ret) {
30249a6d4860SXiubo Li 			dev_err(card->dev,
30259a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
30269a6d4860SXiubo Li 				propname, 2 * i, ret);
30279a6d4860SXiubo Li 			return -EINVAL;
30289a6d4860SXiubo Li 		}
30299a6d4860SXiubo Li 
30309a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
30319a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
30329a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
30339a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
30349a6d4860SXiubo Li 				break;
30359a6d4860SXiubo Li 			}
30369a6d4860SXiubo Li 		}
30379a6d4860SXiubo Li 
30389a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
30399a6d4860SXiubo Li 			dev_err(card->dev,
30409a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
30419a6d4860SXiubo Li 				template);
30429a6d4860SXiubo Li 			return -EINVAL;
30439a6d4860SXiubo Li 		}
30449a6d4860SXiubo Li 
30459a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
30469a6d4860SXiubo Li 						    (2 * i) + 1,
30479a6d4860SXiubo Li 						    &wname);
30489a6d4860SXiubo Li 		if (ret) {
30499a6d4860SXiubo Li 			dev_err(card->dev,
30509a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
30519a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
30529a6d4860SXiubo Li 			return -EINVAL;
30539a6d4860SXiubo Li 		}
30549a6d4860SXiubo Li 
30559a6d4860SXiubo Li 		widgets[i].name = wname;
30569a6d4860SXiubo Li 	}
30579a6d4860SXiubo Li 
3058f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
3059f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
30609a6d4860SXiubo Li 
30619a6d4860SXiubo Li 	return 0;
30629a6d4860SXiubo Li }
306321efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
30649a6d4860SXiubo Li 
3065cbdfab3bSJerome Brunet int snd_soc_of_get_slot_mask(struct device_node *np,
30666131084aSJyri Sarha 			     const char *prop_name,
30676131084aSJyri Sarha 			     unsigned int *mask)
30686131084aSJyri Sarha {
30696131084aSJyri Sarha 	u32 val;
30706c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
30716131084aSJyri Sarha 	int i;
30726131084aSJyri Sarha 
30736131084aSJyri Sarha 	if (!of_slot_mask)
30746131084aSJyri Sarha 		return 0;
30756131084aSJyri Sarha 	val /= sizeof(u32);
30766131084aSJyri Sarha 	for (i = 0; i < val; i++)
30776131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
30786131084aSJyri Sarha 			*mask |= (1 << i);
30796131084aSJyri Sarha 
30806131084aSJyri Sarha 	return val;
30816131084aSJyri Sarha }
3082cbdfab3bSJerome Brunet EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
30836131084aSJyri Sarha 
308489c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
30856131084aSJyri Sarha 			      unsigned int *tx_mask,
30866131084aSJyri Sarha 			      unsigned int *rx_mask,
308789c67857SXiubo Li 			      unsigned int *slots,
308889c67857SXiubo Li 			      unsigned int *slot_width)
308989c67857SXiubo Li {
309089c67857SXiubo Li 	u32 val;
309189c67857SXiubo Li 	int ret;
309289c67857SXiubo Li 
30936131084aSJyri Sarha 	if (tx_mask)
30946131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
30956131084aSJyri Sarha 	if (rx_mask)
30966131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
30976131084aSJyri Sarha 
309889c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
309989c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
310089c67857SXiubo Li 		if (ret)
310189c67857SXiubo Li 			return ret;
310289c67857SXiubo Li 
310389c67857SXiubo Li 		if (slots)
310489c67857SXiubo Li 			*slots = val;
310589c67857SXiubo Li 	}
310689c67857SXiubo Li 
310789c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
310889c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
310989c67857SXiubo Li 		if (ret)
311089c67857SXiubo Li 			return ret;
311189c67857SXiubo Li 
311289c67857SXiubo Li 		if (slot_width)
311389c67857SXiubo Li 			*slot_width = val;
311489c67857SXiubo Li 	}
311589c67857SXiubo Li 
311689c67857SXiubo Li 	return 0;
311789c67857SXiubo Li }
311889c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
311989c67857SXiubo Li 
31203b710356SKuninori Morimoto void snd_soc_of_parse_node_prefix(struct device_node *np,
31215e3cdaa2SKuninori Morimoto 				  struct snd_soc_codec_conf *codec_conf,
31225e3cdaa2SKuninori Morimoto 				  struct device_node *of_node,
31235e3cdaa2SKuninori Morimoto 				  const char *propname)
31245e3cdaa2SKuninori Morimoto {
31255e3cdaa2SKuninori Morimoto 	const char *str;
31265e3cdaa2SKuninori Morimoto 	int ret;
31275e3cdaa2SKuninori Morimoto 
31285e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
31295e3cdaa2SKuninori Morimoto 	if (ret < 0) {
31305e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
31315e3cdaa2SKuninori Morimoto 		return;
31325e3cdaa2SKuninori Morimoto 	}
31335e3cdaa2SKuninori Morimoto 
31345e3cdaa2SKuninori Morimoto 	codec_conf->of_node	= of_node;
31355e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
31365e3cdaa2SKuninori Morimoto }
31373b710356SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
31385e3cdaa2SKuninori Morimoto 
31392bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3140a4a54dd5SStephen Warren 				   const char *propname)
3141a4a54dd5SStephen Warren {
31422bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
3143e3b1e6a1SMark Brown 	int num_routes;
3144a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3145a4a54dd5SStephen Warren 	int i, ret;
3146a4a54dd5SStephen Warren 
3147a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3148c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
314910e8aa9aSMichał Mirosław 		dev_err(card->dev,
315010e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
315110e8aa9aSMichał Mirosław 			propname);
3152a4a54dd5SStephen Warren 		return -EINVAL;
3153a4a54dd5SStephen Warren 	}
3154a4a54dd5SStephen Warren 	num_routes /= 2;
3155a4a54dd5SStephen Warren 	if (!num_routes) {
3156f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3157a4a54dd5SStephen Warren 			propname);
3158a4a54dd5SStephen Warren 		return -EINVAL;
3159a4a54dd5SStephen Warren 	}
3160a4a54dd5SStephen Warren 
3161a86854d0SKees Cook 	routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
3162a4a54dd5SStephen Warren 			      GFP_KERNEL);
3163a4a54dd5SStephen Warren 	if (!routes) {
3164a4a54dd5SStephen Warren 		dev_err(card->dev,
3165f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3166a4a54dd5SStephen Warren 		return -EINVAL;
3167a4a54dd5SStephen Warren 	}
3168a4a54dd5SStephen Warren 
3169a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3170a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3171e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
3172a4a54dd5SStephen Warren 		if (ret) {
3173c871bd0bSMark Brown 			dev_err(card->dev,
3174c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3175c871bd0bSMark Brown 				propname, 2 * i, ret);
3176a4a54dd5SStephen Warren 			return -EINVAL;
3177a4a54dd5SStephen Warren 		}
3178a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3179e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3180a4a54dd5SStephen Warren 		if (ret) {
3181a4a54dd5SStephen Warren 			dev_err(card->dev,
3182c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3183c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3184a4a54dd5SStephen Warren 			return -EINVAL;
3185a4a54dd5SStephen Warren 		}
3186a4a54dd5SStephen Warren 	}
3187a4a54dd5SStephen Warren 
3188f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
3189f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
3190a4a54dd5SStephen Warren 
3191a4a54dd5SStephen Warren 	return 0;
3192a4a54dd5SStephen Warren }
31932bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3194a4a54dd5SStephen Warren 
3195a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3196389cb834SJyri Sarha 				     const char *prefix,
3197389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3198389cb834SJyri Sarha 				     struct device_node **framemaster)
3199a7930ed4SKuninori Morimoto {
3200a7930ed4SKuninori Morimoto 	int ret, i;
3201a7930ed4SKuninori Morimoto 	char prop[128];
3202a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3203a7930ed4SKuninori Morimoto 	int bit, frame;
3204a7930ed4SKuninori Morimoto 	const char *str;
3205a7930ed4SKuninori Morimoto 	struct {
3206a7930ed4SKuninori Morimoto 		char *name;
3207a7930ed4SKuninori Morimoto 		unsigned int val;
3208a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3209a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3210a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3211a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3212a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3213a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3214a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3215a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3216a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3217a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3218a7930ed4SKuninori Morimoto 	};
3219a7930ed4SKuninori Morimoto 
3220a7930ed4SKuninori Morimoto 	if (!prefix)
3221a7930ed4SKuninori Morimoto 		prefix = "";
3222a7930ed4SKuninori Morimoto 
3223a7930ed4SKuninori Morimoto 	/*
32245711c979SKuninori Morimoto 	 * check "dai-format = xxx"
32255711c979SKuninori Morimoto 	 * or    "[prefix]format = xxx"
3226a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3227a7930ed4SKuninori Morimoto 	 */
32285711c979SKuninori Morimoto 	ret = of_property_read_string(np, "dai-format", &str);
32295711c979SKuninori Morimoto 	if (ret < 0) {
3230a7930ed4SKuninori Morimoto 		snprintf(prop, sizeof(prop), "%sformat", prefix);
3231a7930ed4SKuninori Morimoto 		ret = of_property_read_string(np, prop, &str);
32325711c979SKuninori Morimoto 	}
3233a7930ed4SKuninori Morimoto 	if (ret == 0) {
3234a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3235a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3236a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3237a7930ed4SKuninori Morimoto 				break;
3238a7930ed4SKuninori Morimoto 			}
3239a7930ed4SKuninori Morimoto 		}
3240a7930ed4SKuninori Morimoto 	}
3241a7930ed4SKuninori Morimoto 
3242a7930ed4SKuninori Morimoto 	/*
32438c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3244a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3245a7930ed4SKuninori Morimoto 	 */
32468c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
324751930295SJulia Lawall 	if (of_property_read_bool(np, prop))
32488c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
32498c2d6a9fSKuninori Morimoto 	else
32508c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3251a7930ed4SKuninori Morimoto 
3252a7930ed4SKuninori Morimoto 	/*
3253a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3254a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3255a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3256a7930ed4SKuninori Morimoto 	 */
3257a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3258a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3259a7930ed4SKuninori Morimoto 
3260a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3261a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3262a7930ed4SKuninori Morimoto 
3263a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3264a7930ed4SKuninori Morimoto 	case 0x11:
3265a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3266a7930ed4SKuninori Morimoto 		break;
3267a7930ed4SKuninori Morimoto 	case 0x10:
3268a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3269a7930ed4SKuninori Morimoto 		break;
3270a7930ed4SKuninori Morimoto 	case 0x01:
3271a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3272a7930ed4SKuninori Morimoto 		break;
3273a7930ed4SKuninori Morimoto 	default:
3274a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3275a7930ed4SKuninori Morimoto 		break;
3276a7930ed4SKuninori Morimoto 	}
3277a7930ed4SKuninori Morimoto 
3278a7930ed4SKuninori Morimoto 	/*
3279a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3280a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3281a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3282a7930ed4SKuninori Morimoto 	 */
3283a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3284a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3285389cb834SJyri Sarha 	if (bit && bitclkmaster)
3286389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3287a7930ed4SKuninori Morimoto 
3288a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3289a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3290389cb834SJyri Sarha 	if (frame && framemaster)
3291389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3292a7930ed4SKuninori Morimoto 
3293a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3294a7930ed4SKuninori Morimoto 	case 0x11:
3295a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3296a7930ed4SKuninori Morimoto 		break;
3297a7930ed4SKuninori Morimoto 	case 0x10:
3298a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3299a7930ed4SKuninori Morimoto 		break;
3300a7930ed4SKuninori Morimoto 	case 0x01:
3301a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3302a7930ed4SKuninori Morimoto 		break;
3303a7930ed4SKuninori Morimoto 	default:
3304a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3305a7930ed4SKuninori Morimoto 		break;
3306a7930ed4SKuninori Morimoto 	}
3307a7930ed4SKuninori Morimoto 
3308a7930ed4SKuninori Morimoto 	return format;
3309a7930ed4SKuninori Morimoto }
3310a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3311a7930ed4SKuninori Morimoto 
3312a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
3313a180e8b9SKuninori Morimoto {
331409d4cc03SKuninori Morimoto 	struct snd_soc_component *component;
3315c1e230f0SKuninori Morimoto 	struct snd_soc_dai_link_component dlc;
3316a180e8b9SKuninori Morimoto 	int ret;
3317a180e8b9SKuninori Morimoto 
3318c1e230f0SKuninori Morimoto 	dlc.of_node	= of_graph_get_port_parent(ep);
3319c1e230f0SKuninori Morimoto 	dlc.name	= NULL;
3320a180e8b9SKuninori Morimoto 	/*
3321a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
3322a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
3323a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
3324a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
3325a180e8b9SKuninori Morimoto 	 */
3326a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
3327a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
3328c1e230f0SKuninori Morimoto 	component = soc_find_component(&dlc);
33292c7b1704SKuninori Morimoto 	if (component)
33302c7b1704SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_id(component, ep);
3331a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
3332a180e8b9SKuninori Morimoto 
3333c1e230f0SKuninori Morimoto 	of_node_put(dlc.of_node);
3334c0a480d1STony Lindgren 
3335a180e8b9SKuninori Morimoto 	return ret;
3336a180e8b9SKuninori Morimoto }
3337a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3338a180e8b9SKuninori Morimoto 
33391ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args,
3340cb470087SKuninori Morimoto 				const char **dai_name)
3341cb470087SKuninori Morimoto {
3342cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
33433e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
334493b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3345cb470087SKuninori Morimoto 
3346cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3347368dee94SKuninori Morimoto 	for_each_component(pos) {
3348c0834440SKuninori Morimoto 		component_of_node = soc_component_to_node(pos);
33493e0aa8d8SJyri Sarha 
33503e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
3351cb470087SKuninori Morimoto 			continue;
3352cb470087SKuninori Morimoto 
3353a2a34175SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3354a2a34175SKuninori Morimoto 		if (ret == -ENOTSUPP) {
335558bf4179SKuninori Morimoto 			struct snd_soc_dai *dai;
33566833c452SKuninori Morimoto 			int id = -1;
33576833c452SKuninori Morimoto 
335893b0f3eeSJean-Francois Moine 			switch (args->args_count) {
33596833c452SKuninori Morimoto 			case 0:
33606833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
33616833c452SKuninori Morimoto 				break;
33626833c452SKuninori Morimoto 			case 1:
336393b0f3eeSJean-Francois Moine 				id = args->args[0];
33646833c452SKuninori Morimoto 				break;
33656833c452SKuninori Morimoto 			default:
33666833c452SKuninori Morimoto 				/* not supported */
3367cb470087SKuninori Morimoto 				break;
3368cb470087SKuninori Morimoto 			}
3369cb470087SKuninori Morimoto 
33706833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
33716833c452SKuninori Morimoto 				ret = -EINVAL;
33723dcba280SNicolin Chen 				continue;
33736833c452SKuninori Morimoto 			}
3374e41975edSXiubo Li 
3375e41975edSXiubo Li 			ret = 0;
3376e41975edSXiubo Li 
337758bf4179SKuninori Morimoto 			/* find target DAI */
337815a0c645SKuninori Morimoto 			for_each_component_dais(pos, dai) {
337958bf4179SKuninori Morimoto 				if (id == 0)
338058bf4179SKuninori Morimoto 					break;
338158bf4179SKuninori Morimoto 				id--;
338258bf4179SKuninori Morimoto 			}
338358bf4179SKuninori Morimoto 
338458bf4179SKuninori Morimoto 			*dai_name = dai->driver->name;
3385e41975edSXiubo Li 			if (!*dai_name)
3386e41975edSXiubo Li 				*dai_name = pos->name;
33876833c452SKuninori Morimoto 		}
33886833c452SKuninori Morimoto 
3389cb470087SKuninori Morimoto 		break;
3390cb470087SKuninori Morimoto 	}
3391cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
339293b0f3eeSJean-Francois Moine 	return ret;
339393b0f3eeSJean-Francois Moine }
33941ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
339593b0f3eeSJean-Francois Moine 
339693b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
339793b0f3eeSJean-Francois Moine 			    const char **dai_name)
339893b0f3eeSJean-Francois Moine {
339993b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
340093b0f3eeSJean-Francois Moine 	int ret;
340193b0f3eeSJean-Francois Moine 
340293b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
340393b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
340493b0f3eeSJean-Francois Moine 	if (ret)
340593b0f3eeSJean-Francois Moine 		return ret;
340693b0f3eeSJean-Francois Moine 
340793b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3408cb470087SKuninori Morimoto 
3409cb470087SKuninori Morimoto 	of_node_put(args.np);
3410cb470087SKuninori Morimoto 
3411cb470087SKuninori Morimoto 	return ret;
3412cb470087SKuninori Morimoto }
3413cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3414cb470087SKuninori Morimoto 
341593b0f3eeSJean-Francois Moine /*
341694685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
341794685763SSylwester Nawrocki  * @dai_link: DAI link
341894685763SSylwester Nawrocki  *
341994685763SSylwester Nawrocki  * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
342094685763SSylwester Nawrocki  */
342194685763SSylwester Nawrocki void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
342294685763SSylwester Nawrocki {
34233db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *component;
342494685763SSylwester Nawrocki 	int index;
342594685763SSylwester Nawrocki 
34263db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
342794685763SSylwester Nawrocki 		if (!component->of_node)
342894685763SSylwester Nawrocki 			break;
342994685763SSylwester Nawrocki 		of_node_put(component->of_node);
343094685763SSylwester Nawrocki 		component->of_node = NULL;
343194685763SSylwester Nawrocki 	}
343294685763SSylwester Nawrocki }
343394685763SSylwester Nawrocki EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
343494685763SSylwester Nawrocki 
343594685763SSylwester Nawrocki /*
343693b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
343793b0f3eeSJean-Francois Moine  * @dev: Card device
343893b0f3eeSJean-Francois Moine  * @of_node: Device node
343993b0f3eeSJean-Francois Moine  * @dai_link: DAI link
344093b0f3eeSJean-Francois Moine  *
344193b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
344293b0f3eeSJean-Francois Moine  * 'sound-dai'.
344393b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
344494685763SSylwester Nawrocki  * The device nodes in the array (of_node) must be dereferenced by calling
344594685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs() on @dai_link.
344693b0f3eeSJean-Francois Moine  *
344793b0f3eeSJean-Francois Moine  * Returns 0 for success
344893b0f3eeSJean-Francois Moine  */
344993b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
345093b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
345193b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
345293b0f3eeSJean-Francois Moine {
345393b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
345493b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
345593b0f3eeSJean-Francois Moine 	char *name;
345693b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
345793b0f3eeSJean-Francois Moine 
345893b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
345993b0f3eeSJean-Francois Moine 	name = "sound-dai";
346093b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
346193b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
346293b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
346393b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
346493b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
346593b0f3eeSJean-Francois Moine 		else
346693b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
346793b0f3eeSJean-Francois Moine 		return num_codecs;
346893b0f3eeSJean-Francois Moine 	}
3469a86854d0SKees Cook 	component = devm_kcalloc(dev,
3470a86854d0SKees Cook 				 num_codecs, sizeof(*component),
347193b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
347293b0f3eeSJean-Francois Moine 	if (!component)
347393b0f3eeSJean-Francois Moine 		return -ENOMEM;
347493b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
347593b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
347693b0f3eeSJean-Francois Moine 
347793b0f3eeSJean-Francois Moine 	/* Parse the list */
34783db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
347993b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
348093b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
348193b0f3eeSJean-Francois Moine 						 index, &args);
348293b0f3eeSJean-Francois Moine 		if (ret)
348393b0f3eeSJean-Francois Moine 			goto err;
348493b0f3eeSJean-Francois Moine 		component->of_node = args.np;
348593b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
348693b0f3eeSJean-Francois Moine 		if (ret < 0)
348793b0f3eeSJean-Francois Moine 			goto err;
348893b0f3eeSJean-Francois Moine 	}
348993b0f3eeSJean-Francois Moine 	return 0;
349093b0f3eeSJean-Francois Moine err:
349194685763SSylwester Nawrocki 	snd_soc_of_put_dai_link_codecs(dai_link);
349293b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
349393b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
349493b0f3eeSJean-Francois Moine 	return ret;
349593b0f3eeSJean-Francois Moine }
349693b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
349793b0f3eeSJean-Francois Moine 
3498c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3499db2a4165SFrank Mandarino {
35006553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
3501fb257897SMark Brown 	snd_soc_util_init();
3502fb257897SMark Brown 
3503db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3504db2a4165SFrank Mandarino }
35054abe8e16SMark Brown module_init(snd_soc_init);
3506db2a4165SFrank Mandarino 
35077d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3508db2a4165SFrank Mandarino {
3509fb257897SMark Brown 	snd_soc_util_exit();
35106553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3511fb257897SMark Brown 
3512db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3513db2a4165SFrank Mandarino }
3514db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3515db2a4165SFrank Mandarino 
3516db2a4165SFrank Mandarino /* Module information */
3517d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3518db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3519db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
35208b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3521