xref: /openbmc/linux/sound/soc/soc-core.c (revision 0c04800424c42ec3fbe87422d3e04b5c978fc177)
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 
2808ec241c4SKuninori Morimoto /*
2818ec241c4SKuninori Morimoto  * This is glue code between snd_pcm_lib_ioctl() and
2828ec241c4SKuninori Morimoto  * snd_soc_component_driver :: ioctl
2838ec241c4SKuninori Morimoto  */
2848ec241c4SKuninori Morimoto int snd_soc_pcm_lib_ioctl(struct snd_soc_component *component,
2858ec241c4SKuninori Morimoto 			  struct snd_pcm_substream *substream,
2868ec241c4SKuninori Morimoto 			  unsigned int cmd, void *arg)
2878ec241c4SKuninori Morimoto {
2888ec241c4SKuninori Morimoto 	return snd_pcm_lib_ioctl(substream, cmd, arg);
2898ec241c4SKuninori Morimoto }
2908ec241c4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_pcm_lib_ioctl);
2918ec241c4SKuninori 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;
2962b544dd7SKuninori Morimoto 	struct snd_soc_component *comp;
297a0ac4411SKuninori Morimoto 
2982b544dd7SKuninori Morimoto 	for_each_rtd_components(rtd, rtdcom, comp) {
299a0ac4411SKuninori Morimoto 		/* already connected */
3002b544dd7SKuninori Morimoto 		if (comp == component)
301a0ac4411SKuninori Morimoto 			return 0;
302a0ac4411SKuninori Morimoto 	}
303a0ac4411SKuninori Morimoto 
304353e16bfSKuninori Morimoto 	/*
305353e16bfSKuninori Morimoto 	 * created rtdcom here will be freed when rtd->dev was freed.
306353e16bfSKuninori Morimoto 	 * see
307353e16bfSKuninori Morimoto 	 *	soc_free_pcm_runtime() :: device_unregister(rtd->dev)
308353e16bfSKuninori Morimoto 	 */
309353e16bfSKuninori Morimoto 	rtdcom = devm_kzalloc(rtd->dev, sizeof(*rtdcom), GFP_KERNEL);
31032d2c172SKuninori Morimoto 	if (!rtdcom)
311a0ac4411SKuninori Morimoto 		return -ENOMEM;
312a0ac4411SKuninori Morimoto 
31332d2c172SKuninori Morimoto 	rtdcom->component = component;
31432d2c172SKuninori Morimoto 	INIT_LIST_HEAD(&rtdcom->list);
315a0ac4411SKuninori Morimoto 
316353e16bfSKuninori Morimoto 	/*
317353e16bfSKuninori Morimoto 	 * When rtd was freed, created rtdcom here will be
318353e16bfSKuninori Morimoto 	 * also freed.
319353e16bfSKuninori Morimoto 	 * And we don't need to call list_del(&rtdcom->list)
320353e16bfSKuninori Morimoto 	 * when freed, because rtd is also freed.
321353e16bfSKuninori Morimoto 	 */
32232d2c172SKuninori Morimoto 	list_add_tail(&rtdcom->list, &rtd->component_list);
323a0ac4411SKuninori Morimoto 
324a0ac4411SKuninori Morimoto 	return 0;
325a0ac4411SKuninori Morimoto }
326a0ac4411SKuninori Morimoto 
327a0ac4411SKuninori Morimoto struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
328a0ac4411SKuninori Morimoto 						const char *driver_name)
329a0ac4411SKuninori Morimoto {
330a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
3312b544dd7SKuninori Morimoto 	struct snd_soc_component *component;
332a0ac4411SKuninori Morimoto 
333971da24cSKuninori Morimoto 	if (!driver_name)
334971da24cSKuninori Morimoto 		return NULL;
335971da24cSKuninori Morimoto 
336a33c0d16SKuninori Morimoto 	/*
337a33c0d16SKuninori Morimoto 	 * NOTE
338a33c0d16SKuninori Morimoto 	 *
339a33c0d16SKuninori Morimoto 	 * snd_soc_rtdcom_lookup() will find component from rtd by using
340a33c0d16SKuninori Morimoto 	 * specified driver name.
341a33c0d16SKuninori Morimoto 	 * But, if many components which have same driver name are connected
342a33c0d16SKuninori Morimoto 	 * to 1 rtd, this function will return 1st found component.
343a33c0d16SKuninori Morimoto 	 */
3442b544dd7SKuninori Morimoto 	for_each_rtd_components(rtd, rtdcom, component) {
3452b544dd7SKuninori Morimoto 		const char *component_name = component->driver->name;
346971da24cSKuninori Morimoto 
347971da24cSKuninori Morimoto 		if (!component_name)
348971da24cSKuninori Morimoto 			continue;
349971da24cSKuninori Morimoto 
350971da24cSKuninori Morimoto 		if ((component_name == driver_name) ||
351971da24cSKuninori Morimoto 		    strcmp(component_name, driver_name) == 0)
352a0ac4411SKuninori Morimoto 			return rtdcom->component;
353a0ac4411SKuninori Morimoto 	}
354a0ac4411SKuninori Morimoto 
355a0ac4411SKuninori Morimoto 	return NULL;
356a0ac4411SKuninori Morimoto }
357031734b7SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
358a0ac4411SKuninori Morimoto 
35918dd66eaSKuninori Morimoto static struct snd_soc_component
36018dd66eaSKuninori Morimoto *snd_soc_lookup_component_nolocked(struct device *dev, const char *driver_name)
361b8132657SKuninori Morimoto {
362b8132657SKuninori Morimoto 	struct snd_soc_component *component;
3635bd7e08bSKuninori Morimoto 	struct snd_soc_component *found_component;
364b8132657SKuninori Morimoto 
3655bd7e08bSKuninori Morimoto 	found_component = NULL;
366b8132657SKuninori Morimoto 	for_each_component(component) {
3675bd7e08bSKuninori Morimoto 		if ((dev == component->dev) &&
3685bd7e08bSKuninori Morimoto 		    (!driver_name ||
3695bd7e08bSKuninori Morimoto 		     (driver_name == component->driver->name) ||
3705bd7e08bSKuninori Morimoto 		     (strcmp(component->driver->name, driver_name) == 0))) {
3715bd7e08bSKuninori Morimoto 			found_component = component;
372b8132657SKuninori Morimoto 			break;
373b8132657SKuninori Morimoto 		}
3745bd7e08bSKuninori Morimoto 	}
375b8132657SKuninori Morimoto 
3765bd7e08bSKuninori Morimoto 	return found_component;
377b8132657SKuninori Morimoto }
37818dd66eaSKuninori Morimoto 
37918dd66eaSKuninori Morimoto struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
38018dd66eaSKuninori Morimoto 						   const char *driver_name)
38118dd66eaSKuninori Morimoto {
38218dd66eaSKuninori Morimoto 	struct snd_soc_component *component;
38318dd66eaSKuninori Morimoto 
38418dd66eaSKuninori Morimoto 	mutex_lock(&client_mutex);
38518dd66eaSKuninori Morimoto 	component = snd_soc_lookup_component_nolocked(dev, driver_name);
38618dd66eaSKuninori Morimoto 	mutex_unlock(&client_mutex);
38718dd66eaSKuninori Morimoto 
38818dd66eaSKuninori Morimoto 	return component;
38918dd66eaSKuninori Morimoto }
390b8132657SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
391b8132657SKuninori Morimoto 
39275ab9eb6SKuninori Morimoto static const struct snd_soc_ops null_snd_soc_ops;
39375ab9eb6SKuninori Morimoto 
39494def8eaSKuninori Morimoto struct snd_soc_pcm_runtime
39594def8eaSKuninori Morimoto *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
3964468189fSKuninori Morimoto 			 struct snd_soc_dai_link *dai_link)
39794def8eaSKuninori Morimoto {
39894def8eaSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
39994def8eaSKuninori Morimoto 
40094def8eaSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4014468189fSKuninori Morimoto 		if (rtd->dai_link == dai_link)
40294def8eaSKuninori Morimoto 			return rtd;
40394def8eaSKuninori Morimoto 	}
4044468189fSKuninori Morimoto 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link->name);
40594def8eaSKuninori Morimoto 	return NULL;
40694def8eaSKuninori Morimoto }
40794def8eaSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
40894def8eaSKuninori Morimoto 
4096e864344SKuninori Morimoto static void soc_release_rtd_dev(struct device *dev)
4106e864344SKuninori Morimoto {
4116e864344SKuninori Morimoto 	/* "dev" means "rtd->dev" */
4126e864344SKuninori Morimoto 	kfree(dev);
4136e864344SKuninori Morimoto }
4146e864344SKuninori Morimoto 
4151c93a9e0SKuninori Morimoto static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
4161c93a9e0SKuninori Morimoto {
4176e864344SKuninori Morimoto 	if (!rtd)
4186e864344SKuninori Morimoto 		return;
4196e864344SKuninori Morimoto 
420753ace0aSKuninori Morimoto 	list_del(&rtd->list);
421b7c5bc45SKuninori Morimoto 
4220ced7b05SKuninori Morimoto 	flush_delayed_work(&rtd->delayed_work);
4230ced7b05SKuninori Morimoto 	snd_soc_pcm_component_free(rtd);
4240ced7b05SKuninori Morimoto 
425b7c5bc45SKuninori Morimoto 	/*
426b7c5bc45SKuninori Morimoto 	 * we don't need to call kfree() for rtd->dev
427b7c5bc45SKuninori Morimoto 	 * see
428b7c5bc45SKuninori Morimoto 	 *	soc_release_rtd_dev()
429d918a376SKuninori Morimoto 	 *
430d918a376SKuninori Morimoto 	 * We don't need rtd->dev NULL check, because
431d918a376SKuninori Morimoto 	 * it is alloced *before* rtd.
432d918a376SKuninori Morimoto 	 * see
433d918a376SKuninori Morimoto 	 *	soc_new_pcm_runtime()
434b7c5bc45SKuninori Morimoto 	 */
435b7c5bc45SKuninori Morimoto 	device_unregister(rtd->dev);
4361c93a9e0SKuninori Morimoto }
4371c93a9e0SKuninori Morimoto 
4381a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
4391a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
4401a497983SMengdong Lin {
4411a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
442d918a376SKuninori Morimoto 	struct device *dev;
4436e864344SKuninori Morimoto 	int ret;
4441a497983SMengdong Lin 
4456e864344SKuninori Morimoto 	/*
446d918a376SKuninori Morimoto 	 * for rtd->dev
447d918a376SKuninori Morimoto 	 */
448d918a376SKuninori Morimoto 	dev = kzalloc(sizeof(struct device), GFP_KERNEL);
449d918a376SKuninori Morimoto 	if (!dev)
450d918a376SKuninori Morimoto 		return NULL;
451d918a376SKuninori Morimoto 
452d918a376SKuninori Morimoto 	dev->parent	= card->dev;
453d918a376SKuninori Morimoto 	dev->release	= soc_release_rtd_dev;
454d918a376SKuninori Morimoto 	dev->groups	= soc_dev_attr_groups;
455d918a376SKuninori Morimoto 
456d918a376SKuninori Morimoto 	dev_set_name(dev, "%s", dai_link->name);
457d918a376SKuninori Morimoto 
458d918a376SKuninori Morimoto 	ret = device_register(dev);
459d918a376SKuninori Morimoto 	if (ret < 0) {
460d918a376SKuninori Morimoto 		put_device(dev); /* soc_release_rtd_dev */
461d918a376SKuninori Morimoto 		return NULL;
462d918a376SKuninori Morimoto 	}
463d918a376SKuninori Morimoto 
464d918a376SKuninori Morimoto 	/*
4656e864344SKuninori Morimoto 	 * for rtd
4666e864344SKuninori Morimoto 	 */
4674dc0e7dfSKuninori Morimoto 	rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL);
4681a497983SMengdong Lin 	if (!rtd)
4696e864344SKuninori Morimoto 		goto free_rtd;
4701a497983SMengdong Lin 
471d918a376SKuninori Morimoto 	rtd->dev = dev;
472d918a376SKuninori Morimoto 	dev_set_drvdata(dev, rtd);
473d918a376SKuninori Morimoto 
4746e864344SKuninori Morimoto 	/*
4756e864344SKuninori Morimoto 	 * for rtd->codec_dais
4766e864344SKuninori Morimoto 	 */
4774dc0e7dfSKuninori Morimoto 	rtd->codec_dais = devm_kcalloc(dev, dai_link->num_codecs,
4786396bb22SKees Cook 					sizeof(struct snd_soc_dai *),
4791a497983SMengdong Lin 					GFP_KERNEL);
4806e864344SKuninori Morimoto 	if (!rtd->codec_dais)
4816e864344SKuninori Morimoto 		goto free_rtd;
4826e864344SKuninori Morimoto 
4836e864344SKuninori Morimoto 	/*
4846e864344SKuninori Morimoto 	 * rtd remaining settings
4856e864344SKuninori Morimoto 	 */
486929deb84SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
4876e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
4886e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
4896e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
4906e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
4916e864344SKuninori Morimoto 
492929deb84SKuninori Morimoto 	rtd->card = card;
493929deb84SKuninori Morimoto 	rtd->dai_link = dai_link;
494929deb84SKuninori Morimoto 	if (!rtd->dai_link->ops)
495929deb84SKuninori Morimoto 		rtd->dai_link->ops = &null_snd_soc_ops;
496929deb84SKuninori Morimoto 
4976634e3d6SKuninori Morimoto 	/* see for_each_card_rtds */
4981a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
4991a497983SMengdong Lin 	rtd->num = card->num_rtd;
5001a497983SMengdong Lin 	card->num_rtd++;
501a848125eSKuninori Morimoto 
502a848125eSKuninori Morimoto 	return rtd;
5036e864344SKuninori Morimoto 
5046e864344SKuninori Morimoto free_rtd:
5056e864344SKuninori Morimoto 	soc_free_pcm_runtime(rtd);
5066e864344SKuninori Morimoto 	return NULL;
5071a497983SMengdong Lin }
5081a497983SMengdong Lin 
50965462e44SKuninori Morimoto static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
51065462e44SKuninori Morimoto {
51165462e44SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
51265462e44SKuninori Morimoto 
51365462e44SKuninori Morimoto 	for_each_card_rtds(card, rtd)
51465462e44SKuninori Morimoto 		flush_delayed_work(&rtd->delayed_work);
51565462e44SKuninori Morimoto }
51665462e44SKuninori Morimoto 
5176f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
518db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5196f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
520db2a4165SFrank Mandarino {
5216f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
522d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
5231a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
5241a497983SMengdong Lin 	int i;
525db2a4165SFrank Mandarino 
526c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
527c5599b87SLars-Peter Clausen 	if (!card->instantiated)
528e3509ff0SDaniel Mack 		return 0;
529e3509ff0SDaniel Mack 
5302c7b696aSMarcel Ziswiler 	/*
5312c7b696aSMarcel Ziswiler 	 * Due to the resume being scheduled into a workqueue we could
5326ed25978SAndy Green 	 * suspend before that's finished - wait for it to complete.
5336ed25978SAndy Green 	 */
534f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
5356ed25978SAndy Green 
5366ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
537f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5386ed25978SAndy Green 
539a00f90f9SMark Brown 	/* mute any active DACs */
540bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5410b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
5423efab7dcSMark Brown 
5431a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5443efab7dcSMark Brown 			continue;
5453efab7dcSMark Brown 
5460b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
54788fdffa2SKuninori Morimoto 			if (dai->playback_active)
54888fdffa2SKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 1,
54988fdffa2SKuninori Morimoto 						SNDRV_PCM_STREAM_PLAYBACK);
550db2a4165SFrank Mandarino 		}
55188bd870fSBenoit Cousson 	}
552db2a4165SFrank Mandarino 
5534ccab3e7SLiam Girdwood 	/* suspend all pcms */
554bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5551a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5563efab7dcSMark Brown 			continue;
5573efab7dcSMark Brown 
5581a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
5593efab7dcSMark Brown 	}
5604ccab3e7SLiam Girdwood 
56187506549SMark Brown 	if (card->suspend_pre)
56270b2ac12SMark Brown 		card->suspend_pre(card);
563db2a4165SFrank Mandarino 
564bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5651a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
5663efab7dcSMark Brown 
5671a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5683efab7dcSMark Brown 			continue;
5693efab7dcSMark Brown 
570e0f22622SKuninori Morimoto 		if (!cpu_dai->driver->bus_control)
571e0f22622SKuninori Morimoto 			snd_soc_dai_suspend(cpu_dai);
572db2a4165SFrank Mandarino 	}
573db2a4165SFrank Mandarino 
57437660b6dSLars-Peter Clausen 	/* close any waiting streams */
57565462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
576db2a4165SFrank Mandarino 
577bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5783efab7dcSMark Brown 
5791a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5803efab7dcSMark Brown 			continue;
5813efab7dcSMark Brown 
5821a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
5837bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
584db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
585f0fba2adSLiam Girdwood 
5861a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
5877bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
588db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
589db2a4165SFrank Mandarino 	}
590db2a4165SFrank Mandarino 
5918be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
5928be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
593e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
594e2d32ff6SMark Brown 
5959178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
596f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
5972c7b696aSMarcel Ziswiler 		struct snd_soc_dapm_context *dapm =
5982c7b696aSMarcel Ziswiler 				snd_soc_component_get_dapm(component);
599d9fc4063SKuninori Morimoto 
6002c7b696aSMarcel Ziswiler 		/*
6012c7b696aSMarcel Ziswiler 		 * If there are paths active then the COMPONENT will be held
6022c7b696aSMarcel Ziswiler 		 * with bias _ON and should not be suspended.
6032c7b696aSMarcel Ziswiler 		 */
604e40fadbcSKuninori Morimoto 		if (!snd_soc_component_is_suspended(component)) {
6054890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
6061547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
607125a25daSMark Brown 				/*
6089178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
609125a25daSMark Brown 				 * bias off then being in STANDBY
610125a25daSMark Brown 				 * means it's doing something,
611125a25daSMark Brown 				 * otherwise fall through.
612125a25daSMark Brown 				 */
6134890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
6149178feb4SKuninori Morimoto 					dev_dbg(component->dev,
61510e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
616125a25daSMark Brown 					break;
617125a25daSMark Brown 				}
6181a12d5dcSGustavo A. R. Silva 				/* fall through */
619a8093297SLars-Peter Clausen 
6201547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
62166c51573SKuninori Morimoto 				snd_soc_component_suspend(component);
6229178feb4SKuninori Morimoto 				if (component->regmap)
6239178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
624988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
6259178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
6261547aba9SMark Brown 				break;
6271547aba9SMark Brown 			default:
6289178feb4SKuninori Morimoto 				dev_dbg(component->dev,
6299178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
6301547aba9SMark Brown 				break;
6311547aba9SMark Brown 			}
6321547aba9SMark Brown 		}
633f0fba2adSLiam Girdwood 	}
634db2a4165SFrank Mandarino 
635bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6361a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6373efab7dcSMark Brown 
6381a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6393efab7dcSMark Brown 			continue;
6403efab7dcSMark Brown 
641e0f22622SKuninori Morimoto 		if (cpu_dai->driver->bus_control)
642e0f22622SKuninori Morimoto 			snd_soc_dai_suspend(cpu_dai);
643988e8cc4SNicolin Chen 
644988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
645988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
646db2a4165SFrank Mandarino 	}
647db2a4165SFrank Mandarino 
64887506549SMark Brown 	if (card->suspend_post)
64970b2ac12SMark Brown 		card->suspend_post(card);
650db2a4165SFrank Mandarino 
651db2a4165SFrank Mandarino 	return 0;
652db2a4165SFrank Mandarino }
6536f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
654db2a4165SFrank Mandarino 
6552c7b696aSMarcel Ziswiler /*
6562c7b696aSMarcel Ziswiler  * deferred resume work, so resume can complete before we finished
6576ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6586ed25978SAndy Green  */
6596ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
660db2a4165SFrank Mandarino {
661f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
6622c7b696aSMarcel Ziswiler 			container_of(work, struct snd_soc_card,
6632c7b696aSMarcel Ziswiler 				     deferred_resume_work);
6641a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
665d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
6661a497983SMengdong Lin 	int i;
667db2a4165SFrank Mandarino 
6682c7b696aSMarcel Ziswiler 	/*
6692c7b696aSMarcel Ziswiler 	 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6706ed25978SAndy Green 	 * so userspace apps are blocked from touching us
6716ed25978SAndy Green 	 */
6726ed25978SAndy Green 
673f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
6746ed25978SAndy Green 
6759949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
676f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
6779949788bSMark Brown 
67887506549SMark Brown 	if (card->resume_pre)
67970b2ac12SMark Brown 		card->resume_pre(card);
680db2a4165SFrank Mandarino 
681bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
682bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6831a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6843efab7dcSMark Brown 
6851a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6863efab7dcSMark Brown 			continue;
6873efab7dcSMark Brown 
68824b09d05SKuninori Morimoto 		if (cpu_dai->driver->bus_control)
68924b09d05SKuninori Morimoto 			snd_soc_dai_resume(cpu_dai);
690db2a4165SFrank Mandarino 	}
691db2a4165SFrank Mandarino 
692f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
693e40fadbcSKuninori Morimoto 		if (snd_soc_component_is_suspended(component))
6949a840cbaSKuninori Morimoto 			snd_soc_component_resume(component);
6951547aba9SMark Brown 	}
696db2a4165SFrank Mandarino 
697bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6983efab7dcSMark Brown 
6991a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7003efab7dcSMark Brown 			continue;
7013efab7dcSMark Brown 
7021a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
703d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
704db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
705f0fba2adSLiam Girdwood 
7061a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
707d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
708db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
709db2a4165SFrank Mandarino 	}
710db2a4165SFrank Mandarino 
7113ff3f64bSMark Brown 	/* unmute any active DACs */
712bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7130b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
7143efab7dcSMark Brown 
7151a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7163efab7dcSMark Brown 			continue;
7173efab7dcSMark Brown 
7180b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
71988fdffa2SKuninori Morimoto 			if (dai->playback_active)
72088fdffa2SKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 0,
72188fdffa2SKuninori Morimoto 						SNDRV_PCM_STREAM_PLAYBACK);
722db2a4165SFrank Mandarino 		}
72388bd870fSBenoit Cousson 	}
724db2a4165SFrank Mandarino 
725bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7261a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7273efab7dcSMark Brown 
7281a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7293efab7dcSMark Brown 			continue;
7303efab7dcSMark Brown 
73124b09d05SKuninori Morimoto 		if (!cpu_dai->driver->bus_control)
73224b09d05SKuninori Morimoto 			snd_soc_dai_resume(cpu_dai);
733db2a4165SFrank Mandarino 	}
734db2a4165SFrank Mandarino 
73587506549SMark Brown 	if (card->resume_post)
73670b2ac12SMark Brown 		card->resume_post(card);
737db2a4165SFrank Mandarino 
738f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
7396ed25978SAndy Green 
7408be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
7418be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
742e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
7431a7aaa58SJeeja KP 
7441a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
7451a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
7466ed25978SAndy Green }
7476ed25978SAndy Green 
7486ed25978SAndy Green /* powers up audio subsystem after a suspend */
7496f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
7506ed25978SAndy Green {
7516f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
752bc263214SLars-Peter Clausen 	bool bus_control = false;
7531a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
75422d251a5SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
75522d251a5SKuninori Morimoto 	int i;
756b9dd94a8SPeter Ujfalusi 
757c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
758c5599b87SLars-Peter Clausen 	if (!card->instantiated)
7595ff1ddf2SEric Miao 		return 0;
7605ff1ddf2SEric Miao 
761988e8cc4SNicolin Chen 	/* activate pins from sleep state */
762bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
76388bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
76488bd870fSBenoit Cousson 
765988e8cc4SNicolin Chen 		if (cpu_dai->active)
766988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
76788bd870fSBenoit Cousson 
76822d251a5SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
769988e8cc4SNicolin Chen 			if (codec_dai->active)
770988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
771988e8cc4SNicolin Chen 		}
77288bd870fSBenoit Cousson 	}
773988e8cc4SNicolin Chen 
774bc263214SLars-Peter Clausen 	/*
775bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
776bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
777bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
77864ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
77964ab9baaSMark Brown 	 */
780bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7811a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7822c7b696aSMarcel Ziswiler 
783bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
78482e14e8bSStephen Warren 	}
785bc263214SLars-Peter Clausen 	if (bus_control) {
786bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
78764ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
78864ab9baaSMark Brown 	} else {
789f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
7906308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
791f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
792f0fba2adSLiam Girdwood 	}
7936ed25978SAndy Green 
794db2a4165SFrank Mandarino 	return 0;
795db2a4165SFrank Mandarino }
7966f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
797b3da4251SKuninori Morimoto 
798b3da4251SKuninori Morimoto static void soc_resume_init(struct snd_soc_card *card)
799b3da4251SKuninori Morimoto {
800b3da4251SKuninori Morimoto 	/* deferred resume work */
801b3da4251SKuninori Morimoto 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
802b3da4251SKuninori Morimoto }
803db2a4165SFrank Mandarino #else
8046f8ab4acSMark Brown #define snd_soc_suspend NULL
8056f8ab4acSMark Brown #define snd_soc_resume NULL
806b3da4251SKuninori Morimoto static inline void soc_resume_init(struct snd_soc_card *card)
807b3da4251SKuninori Morimoto {
808b3da4251SKuninori Morimoto }
809db2a4165SFrank Mandarino #endif
810db2a4165SFrank Mandarino 
81185e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
81202a06d30SBarry Song };
81302a06d30SBarry Song 
814c0834440SKuninori Morimoto static struct device_node
815c0834440SKuninori Morimoto *soc_component_to_node(struct snd_soc_component *component)
81612023a9aSMisael Lopez Cruz {
817c0834440SKuninori Morimoto 	struct device_node *of_node;
81812023a9aSMisael Lopez Cruz 
819c0834440SKuninori Morimoto 	of_node = component->dev->of_node;
820c0834440SKuninori Morimoto 	if (!of_node && component->dev->parent)
821c0834440SKuninori Morimoto 		of_node = component->dev->parent->of_node;
82234e81ab4SLars-Peter Clausen 
823c0834440SKuninori Morimoto 	return of_node;
82412023a9aSMisael Lopez Cruz }
82512023a9aSMisael Lopez Cruz 
826be6ac0a9SKuninori Morimoto static int snd_soc_is_matching_component(
827be6ac0a9SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc,
828be6ac0a9SKuninori Morimoto 	struct snd_soc_component *component)
829be6ac0a9SKuninori Morimoto {
830be6ac0a9SKuninori Morimoto 	struct device_node *component_of_node;
831be6ac0a9SKuninori Morimoto 
8327d7db5d3SKuninori Morimoto 	if (!dlc)
8337d7db5d3SKuninori Morimoto 		return 0;
8347d7db5d3SKuninori Morimoto 
8357d7db5d3SKuninori Morimoto 	component_of_node = soc_component_to_node(component);
836be6ac0a9SKuninori Morimoto 
837be6ac0a9SKuninori Morimoto 	if (dlc->of_node && component_of_node != dlc->of_node)
838be6ac0a9SKuninori Morimoto 		return 0;
839be6ac0a9SKuninori Morimoto 	if (dlc->name && strcmp(component->name, dlc->name))
840be6ac0a9SKuninori Morimoto 		return 0;
841be6ac0a9SKuninori Morimoto 
842be6ac0a9SKuninori Morimoto 	return 1;
843be6ac0a9SKuninori Morimoto }
844be6ac0a9SKuninori Morimoto 
845db2a4165SFrank Mandarino static struct snd_soc_component *soc_find_component(
846c1e230f0SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc)
847db2a4165SFrank Mandarino {
848db2a4165SFrank Mandarino 	struct snd_soc_component *component;
849db2a4165SFrank Mandarino 
850db2a4165SFrank Mandarino 	lockdep_assert_held(&client_mutex);
851db2a4165SFrank Mandarino 
852e3303268SKuninori Morimoto 	/*
853e3303268SKuninori Morimoto 	 * NOTE
854e3303268SKuninori Morimoto 	 *
855e3303268SKuninori Morimoto 	 * It returns *1st* found component, but some driver
856e3303268SKuninori Morimoto 	 * has few components by same of_node/name
857e3303268SKuninori Morimoto 	 * ex)
858e3303268SKuninori Morimoto 	 *	CPU component and generic DMAEngine component
859e3303268SKuninori Morimoto 	 */
860c1e230f0SKuninori Morimoto 	for_each_component(component)
861c1e230f0SKuninori Morimoto 		if (snd_soc_is_matching_component(dlc, component))
862db2a4165SFrank Mandarino 			return component;
863db2a4165SFrank Mandarino 
864db2a4165SFrank Mandarino 	return NULL;
86512023a9aSMisael Lopez Cruz }
86612023a9aSMisael Lopez Cruz 
867fbb88b5cSMengdong Lin /**
868fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
869fbb88b5cSMengdong Lin  *
8704958471bSJeffy Chen  * @dlc: name of the DAI or the DAI driver and optional component info to match
871fbb88b5cSMengdong Lin  *
872ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
873fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
874fbb88b5cSMengdong Lin  * should also match if being specified.
875fbb88b5cSMengdong Lin  *
876fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
877fbb88b5cSMengdong Lin  */
878305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
87914621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
88012023a9aSMisael Lopez Cruz {
88114621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
88214621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
88312023a9aSMisael Lopez Cruz 
88434e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
88534e81ab4SLars-Peter Clausen 
88614621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs */
887368dee94SKuninori Morimoto 	for_each_component(component) {
888be6ac0a9SKuninori Morimoto 		if (!snd_soc_is_matching_component(dlc, component))
88912023a9aSMisael Lopez Cruz 			continue;
89015a0c645SKuninori Morimoto 		for_each_component_dais(component, dai) {
8914958471bSJeffy Chen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
8926a6dafdaSJeffy Chen 			    && (!dai->driver->name
8936a6dafdaSJeffy Chen 				|| strcmp(dai->driver->name, dlc->dai_name)))
89414621c7eSLars-Peter Clausen 				continue;
89512023a9aSMisael Lopez Cruz 
89614621c7eSLars-Peter Clausen 			return dai;
89712023a9aSMisael Lopez Cruz 		}
89812023a9aSMisael Lopez Cruz 	}
89912023a9aSMisael Lopez Cruz 
90012023a9aSMisael Lopez Cruz 	return NULL;
90112023a9aSMisael Lopez Cruz }
902305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
90312023a9aSMisael Lopez Cruz 
904bfce78a5SKuninori Morimoto static int soc_dai_link_sanity_check(struct snd_soc_card *card,
90536794902SKuninori Morimoto 				     struct snd_soc_dai_link *link)
90636794902SKuninori Morimoto {
90736794902SKuninori Morimoto 	int i;
90836794902SKuninori Morimoto 	struct snd_soc_dai_link_component *codec, *platform;
90936794902SKuninori Morimoto 
91036794902SKuninori Morimoto 	for_each_link_codecs(link, i, codec) {
91136794902SKuninori Morimoto 		/*
91236794902SKuninori Morimoto 		 * Codec must be specified by 1 of name or OF node,
91336794902SKuninori Morimoto 		 * not both or neither.
91436794902SKuninori Morimoto 		 */
91536794902SKuninori Morimoto 		if (!!codec->name == !!codec->of_node) {
91636794902SKuninori Morimoto 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
91736794902SKuninori Morimoto 				link->name);
91836794902SKuninori Morimoto 			return -EINVAL;
91936794902SKuninori Morimoto 		}
92036794902SKuninori Morimoto 
92136794902SKuninori Morimoto 		/* Codec DAI name must be specified */
92236794902SKuninori Morimoto 		if (!codec->dai_name) {
92336794902SKuninori Morimoto 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
92436794902SKuninori Morimoto 				link->name);
92536794902SKuninori Morimoto 			return -EINVAL;
92636794902SKuninori Morimoto 		}
92736794902SKuninori Morimoto 
92836794902SKuninori Morimoto 		/*
92936794902SKuninori Morimoto 		 * Defer card registration if codec component is not added to
93036794902SKuninori Morimoto 		 * component list.
93136794902SKuninori Morimoto 		 */
93236794902SKuninori Morimoto 		if (!soc_find_component(codec))
93336794902SKuninori Morimoto 			return -EPROBE_DEFER;
93436794902SKuninori Morimoto 	}
93536794902SKuninori Morimoto 
93636794902SKuninori Morimoto 	for_each_link_platforms(link, i, platform) {
93736794902SKuninori Morimoto 		/*
93836794902SKuninori Morimoto 		 * Platform may be specified by either name or OF node, but it
93936794902SKuninori Morimoto 		 * can be left unspecified, then no components will be inserted
94036794902SKuninori Morimoto 		 * in the rtdcom list
94136794902SKuninori Morimoto 		 */
94236794902SKuninori Morimoto 		if (!!platform->name == !!platform->of_node) {
94336794902SKuninori Morimoto 			dev_err(card->dev,
94436794902SKuninori Morimoto 				"ASoC: Neither/both platform name/of_node are set for %s\n",
94536794902SKuninori Morimoto 				link->name);
94636794902SKuninori Morimoto 			return -EINVAL;
94736794902SKuninori Morimoto 		}
94836794902SKuninori Morimoto 
94936794902SKuninori Morimoto 		/*
95036794902SKuninori Morimoto 		 * Defer card registration if platform component is not added to
95136794902SKuninori Morimoto 		 * component list.
95236794902SKuninori Morimoto 		 */
95336794902SKuninori Morimoto 		if (!soc_find_component(platform))
95436794902SKuninori Morimoto 			return -EPROBE_DEFER;
95536794902SKuninori Morimoto 	}
95636794902SKuninori Morimoto 
95736794902SKuninori Morimoto 	/* FIXME */
95836794902SKuninori Morimoto 	if (link->num_cpus > 1) {
95936794902SKuninori Morimoto 		dev_err(card->dev,
96036794902SKuninori Morimoto 			"ASoC: multi cpu is not yet supported %s\n",
96136794902SKuninori Morimoto 			link->name);
96236794902SKuninori Morimoto 		return -EINVAL;
96336794902SKuninori Morimoto 	}
96436794902SKuninori Morimoto 
96536794902SKuninori Morimoto 	/*
96636794902SKuninori Morimoto 	 * CPU device may be specified by either name or OF node, but
96736794902SKuninori Morimoto 	 * can be left unspecified, and will be matched based on DAI
96836794902SKuninori Morimoto 	 * name alone..
96936794902SKuninori Morimoto 	 */
97036794902SKuninori Morimoto 	if (link->cpus->name && link->cpus->of_node) {
97136794902SKuninori Morimoto 		dev_err(card->dev,
97236794902SKuninori Morimoto 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
97336794902SKuninori Morimoto 			link->name);
97436794902SKuninori Morimoto 		return -EINVAL;
97536794902SKuninori Morimoto 	}
97636794902SKuninori Morimoto 
97736794902SKuninori Morimoto 	/*
978cd3c5ad7SKuninori Morimoto 	 * Defer card registration if cpu dai component is not added to
97936794902SKuninori Morimoto 	 * component list.
98036794902SKuninori Morimoto 	 */
98136794902SKuninori Morimoto 	if ((link->cpus->of_node || link->cpus->name) &&
98236794902SKuninori Morimoto 	    !soc_find_component(link->cpus))
98336794902SKuninori Morimoto 		return -EPROBE_DEFER;
98436794902SKuninori Morimoto 
98536794902SKuninori Morimoto 	/*
98636794902SKuninori Morimoto 	 * At least one of CPU DAI name or CPU device name/node must be
98736794902SKuninori Morimoto 	 * specified
98836794902SKuninori Morimoto 	 */
98936794902SKuninori Morimoto 	if (!link->cpus->dai_name &&
99036794902SKuninori Morimoto 	    !(link->cpus->name || link->cpus->of_node)) {
99136794902SKuninori Morimoto 		dev_err(card->dev,
99236794902SKuninori Morimoto 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
99336794902SKuninori Morimoto 			link->name);
99436794902SKuninori Morimoto 		return -EINVAL;
99536794902SKuninori Morimoto 	}
99636794902SKuninori Morimoto 
99736794902SKuninori Morimoto 	return 0;
99836794902SKuninori Morimoto }
99936794902SKuninori Morimoto 
1000da704f26SKuninori Morimoto /**
1001da704f26SKuninori Morimoto  * snd_soc_remove_dai_link - Remove a DAI link from the list
1002da704f26SKuninori Morimoto  * @card: The ASoC card that owns the link
1003da704f26SKuninori Morimoto  * @dai_link: The DAI link to remove
1004da704f26SKuninori Morimoto  *
1005da704f26SKuninori Morimoto  * This function removes a DAI link from the ASoC card's link list.
1006da704f26SKuninori Morimoto  *
1007da704f26SKuninori Morimoto  * For DAI links previously added by topology, topology should
1008da704f26SKuninori Morimoto  * remove them by using the dobj embedded in the link.
1009da704f26SKuninori Morimoto  */
1010da704f26SKuninori Morimoto void snd_soc_remove_dai_link(struct snd_soc_card *card,
1011bc7a9091SKuninori Morimoto 			     struct snd_soc_dai_link *dai_link)
1012bc7a9091SKuninori Morimoto {
1013bc7a9091SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
1014bc7a9091SKuninori Morimoto 
1015da704f26SKuninori Morimoto 	lockdep_assert_held(&client_mutex);
1016da704f26SKuninori Morimoto 
1017da704f26SKuninori Morimoto 	/*
1018da704f26SKuninori Morimoto 	 * Notify the machine driver for extra destruction
1019da704f26SKuninori Morimoto 	 */
1020da704f26SKuninori Morimoto 	if (card->remove_dai_link)
1021da704f26SKuninori Morimoto 		card->remove_dai_link(card, dai_link);
1022da704f26SKuninori Morimoto 
10234468189fSKuninori Morimoto 	rtd = snd_soc_get_pcm_runtime(card, dai_link);
1024bc7a9091SKuninori Morimoto 	if (rtd)
1025bc7a9091SKuninori Morimoto 		soc_free_pcm_runtime(rtd);
1026bc7a9091SKuninori Morimoto }
1027da704f26SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1028bc7a9091SKuninori Morimoto 
102963dc47daSKuninori Morimoto /**
1030*0c048004SKuninori Morimoto  * snd_soc_add_pcm_runtime - Add a pcm_runtime dynamically via dai_link
1031*0c048004SKuninori Morimoto  * @card: The ASoC card to which the pcm_runtime is added
1032*0c048004SKuninori Morimoto  * @dai_link: The DAI link to find pcm_runtime
103363dc47daSKuninori Morimoto  *
1034*0c048004SKuninori Morimoto  * This function adds a pcm_runtime ASoC card by using dai_link.
103563dc47daSKuninori Morimoto  *
1036*0c048004SKuninori Morimoto  * Note: Topology can use this API to add pcm_runtime when probing the
103763dc47daSKuninori Morimoto  * topology component. And machine drivers can still define static
103863dc47daSKuninori Morimoto  * DAI links in dai_link array.
103963dc47daSKuninori Morimoto  */
1040*0c048004SKuninori Morimoto int snd_soc_add_pcm_runtime(struct snd_soc_card *card,
10416f2f1ff0SMengdong Lin 			    struct snd_soc_dai_link *dai_link)
1042db2a4165SFrank Mandarino {
10431a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
104434614739SJerome Brunet 	struct snd_soc_dai_link_component *codec, *platform;
104590be711eSKuninori Morimoto 	struct snd_soc_component *component;
1046bfce78a5SKuninori Morimoto 	int i, ret;
1047db2a4165SFrank Mandarino 
104863dc47daSKuninori Morimoto 	lockdep_assert_held(&client_mutex);
104963dc47daSKuninori Morimoto 
105063dc47daSKuninori Morimoto 	/*
105163dc47daSKuninori Morimoto 	 * Notify the machine driver for extra initialization
105263dc47daSKuninori Morimoto 	 */
105363dc47daSKuninori Morimoto 	if (card->add_dai_link)
105463dc47daSKuninori Morimoto 		card->add_dai_link(card, dai_link);
105563dc47daSKuninori Morimoto 
1056a655de80SLiam Girdwood 	if (dai_link->ignore)
1057a655de80SLiam Girdwood 		return 0;
1058a655de80SLiam Girdwood 
10596f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
10606308419aSMark Brown 
1061bfce78a5SKuninori Morimoto 	ret = soc_dai_link_sanity_check(card, dai_link);
1062bfce78a5SKuninori Morimoto 	if (ret < 0)
1063bfce78a5SKuninori Morimoto 		return ret;
1064bfce78a5SKuninori Morimoto 
1065513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
1066513cb311SSudip Mukherjee 	if (!rtd)
1067513cb311SSudip Mukherjee 		return -ENOMEM;
1068513cb311SSudip Mukherjee 
106908a5841eSKuninori Morimoto 	/* FIXME: we need multi CPU support in the future */
107008a5841eSKuninori Morimoto 	rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
1071b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
10726b490879SMartin Hundebøll 		dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
107308a5841eSKuninori Morimoto 			 dai_link->cpus->dai_name);
10741a497983SMengdong Lin 		goto _err_defer;
1075c5af3a2eSMark Brown 	}
107690be711eSKuninori Morimoto 	snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
1077c5af3a2eSMark Brown 
107888bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
1079e2b30edfSKuninori Morimoto 	rtd->num_codecs = dai_link->num_codecs;
108034614739SJerome Brunet 	for_each_link_codecs(dai_link, i, codec) {
108134614739SJerome Brunet 		rtd->codec_dais[i] = snd_soc_find_dai(codec);
10820a2cfcd9SKuninori Morimoto 		if (!rtd->codec_dais[i]) {
10837c7e2d6aSStefan Agner 			dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
108434614739SJerome Brunet 				 codec->dai_name);
10851a497983SMengdong Lin 			goto _err_defer;
108612023a9aSMisael Lopez Cruz 		}
108734614739SJerome Brunet 
10880a2cfcd9SKuninori Morimoto 		snd_soc_rtdcom_add(rtd, rtd->codec_dais[i]->component);
108988bd870fSBenoit Cousson 	}
109088bd870fSBenoit Cousson 
109188bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
10920a2cfcd9SKuninori Morimoto 	rtd->codec_dai = rtd->codec_dais[0];
109312023a9aSMisael Lopez Cruz 
1094e2b30edfSKuninori Morimoto 	/* Find PLATFORM from registered PLATFORMs */
109534614739SJerome Brunet 	for_each_link_platforms(dai_link, i, platform) {
1096368dee94SKuninori Morimoto 		for_each_component(component) {
109734614739SJerome Brunet 			if (!snd_soc_is_matching_component(platform, component))
109890be711eSKuninori Morimoto 				continue;
109990be711eSKuninori Morimoto 
110090be711eSKuninori Morimoto 			snd_soc_rtdcom_add(rtd, component);
110190be711eSKuninori Morimoto 		}
110234614739SJerome Brunet 	}
110390be711eSKuninori Morimoto 
1104b19e6e7bSMark Brown 	return 0;
11051a497983SMengdong Lin 
11061a497983SMengdong Lin _err_defer:
11071a497983SMengdong Lin 	soc_free_pcm_runtime(rtd);
11081a497983SMengdong Lin 	return -EPROBE_DEFER;
11096b05eda6SMark Brown }
1110*0c048004SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_pcm_runtime);
11116b05eda6SMark Brown 
1112ffd60fbaSKuninori Morimoto static void soc_set_of_name_prefix(struct snd_soc_component *component)
1113ffd60fbaSKuninori Morimoto {
1114ffd60fbaSKuninori Morimoto 	struct device_node *of_node = soc_component_to_node(component);
1115ffd60fbaSKuninori Morimoto 	const char *str;
1116ffd60fbaSKuninori Morimoto 	int ret;
1117ffd60fbaSKuninori Morimoto 
1118ffd60fbaSKuninori Morimoto 	ret = of_property_read_string(of_node, "sound-name-prefix", &str);
1119ffd60fbaSKuninori Morimoto 	if (!ret)
1120ffd60fbaSKuninori Morimoto 		component->name_prefix = str;
1121ffd60fbaSKuninori Morimoto }
1122ffd60fbaSKuninori Morimoto 
1123ffd60fbaSKuninori Morimoto static void soc_set_name_prefix(struct snd_soc_card *card,
1124ffd60fbaSKuninori Morimoto 				struct snd_soc_component *component)
1125ffd60fbaSKuninori Morimoto {
1126ffd60fbaSKuninori Morimoto 	int i;
1127ffd60fbaSKuninori Morimoto 
1128ffd60fbaSKuninori Morimoto 	for (i = 0; i < card->num_configs && card->codec_conf; i++) {
1129ffd60fbaSKuninori Morimoto 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1130ffd60fbaSKuninori Morimoto 		struct device_node *of_node = soc_component_to_node(component);
1131ffd60fbaSKuninori Morimoto 
1132ffd60fbaSKuninori Morimoto 		if (map->of_node && of_node != map->of_node)
1133ffd60fbaSKuninori Morimoto 			continue;
1134ffd60fbaSKuninori Morimoto 		if (map->dev_name && strcmp(component->name, map->dev_name))
1135ffd60fbaSKuninori Morimoto 			continue;
1136ffd60fbaSKuninori Morimoto 		component->name_prefix = map->name_prefix;
1137ffd60fbaSKuninori Morimoto 		return;
1138ffd60fbaSKuninori Morimoto 	}
1139ffd60fbaSKuninori Morimoto 
1140ffd60fbaSKuninori Morimoto 	/*
1141ffd60fbaSKuninori Morimoto 	 * If there is no configuration table or no match in the table,
1142ffd60fbaSKuninori Morimoto 	 * check if a prefix is provided in the node
1143ffd60fbaSKuninori Morimoto 	 */
1144ffd60fbaSKuninori Morimoto 	soc_set_of_name_prefix(component);
1145ffd60fbaSKuninori Morimoto }
1146ffd60fbaSKuninori Morimoto 
1147c6619b72SKuninori Morimoto static void soc_remove_component(struct snd_soc_component *component,
1148c6619b72SKuninori Morimoto 				 int probed)
114922d14231SKuninori Morimoto {
1150c6619b72SKuninori Morimoto 
1151c6619b72SKuninori Morimoto 	if (!component->card)
1152c6619b72SKuninori Morimoto 		return;
1153c6619b72SKuninori Morimoto 
1154c6619b72SKuninori Morimoto 	if (probed)
1155c6619b72SKuninori Morimoto 		snd_soc_component_remove(component);
1156c6619b72SKuninori Morimoto 
115704f770d9SKuninori Morimoto 	/* For framework level robustness */
11583bb936f5SAmadeusz Sławiński 	snd_soc_component_set_jack(component, NULL, NULL);
115904f770d9SKuninori Morimoto 
11607a5d9815SBard liao 	list_del_init(&component->card_list);
116122d14231SKuninori Morimoto 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
116222d14231SKuninori Morimoto 	soc_cleanup_component_debugfs(component);
116322d14231SKuninori Morimoto 	component->card = NULL;
11640e36f36bSPierre-Louis Bossart 	snd_soc_component_module_put_when_remove(component);
116522d14231SKuninori Morimoto }
116622d14231SKuninori Morimoto 
1167ffd60fbaSKuninori Morimoto static int soc_probe_component(struct snd_soc_card *card,
1168ffd60fbaSKuninori Morimoto 			       struct snd_soc_component *component)
1169ffd60fbaSKuninori Morimoto {
1170ffd60fbaSKuninori Morimoto 	struct snd_soc_dapm_context *dapm =
1171ffd60fbaSKuninori Morimoto 		snd_soc_component_get_dapm(component);
1172ffd60fbaSKuninori Morimoto 	struct snd_soc_dai *dai;
1173c6619b72SKuninori Morimoto 	int probed = 0;
1174ffd60fbaSKuninori Morimoto 	int ret;
1175ffd60fbaSKuninori Morimoto 
1176ffd60fbaSKuninori Morimoto 	if (!strcmp(component->name, "snd-soc-dummy"))
1177ffd60fbaSKuninori Morimoto 		return 0;
1178ffd60fbaSKuninori Morimoto 
1179ffd60fbaSKuninori Morimoto 	if (component->card) {
1180ffd60fbaSKuninori Morimoto 		if (component->card != card) {
1181ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1182ffd60fbaSKuninori Morimoto 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1183ffd60fbaSKuninori Morimoto 				card->name, component->card->name);
1184ffd60fbaSKuninori Morimoto 			return -ENODEV;
1185ffd60fbaSKuninori Morimoto 		}
1186ffd60fbaSKuninori Morimoto 		return 0;
1187ffd60fbaSKuninori Morimoto 	}
1188ffd60fbaSKuninori Morimoto 
1189ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_module_get_when_probe(component);
1190ffd60fbaSKuninori Morimoto 	if (ret < 0)
1191ffd60fbaSKuninori Morimoto 		return ret;
1192ffd60fbaSKuninori Morimoto 
1193ffd60fbaSKuninori Morimoto 	component->card = card;
1194ffd60fbaSKuninori Morimoto 	soc_set_name_prefix(card, component);
1195ffd60fbaSKuninori Morimoto 
1196ffd60fbaSKuninori Morimoto 	soc_init_component_debugfs(component);
1197ffd60fbaSKuninori Morimoto 
119895c267ddSKuninori Morimoto 	snd_soc_dapm_init(dapm, card, component);
1199b614beafSKuninori Morimoto 
1200ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_new_controls(dapm,
1201ffd60fbaSKuninori Morimoto 					component->driver->dapm_widgets,
1202ffd60fbaSKuninori Morimoto 					component->driver->num_dapm_widgets);
1203ffd60fbaSKuninori Morimoto 
1204ffd60fbaSKuninori Morimoto 	if (ret != 0) {
1205ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1206ffd60fbaSKuninori Morimoto 			"Failed to create new controls %d\n", ret);
1207ffd60fbaSKuninori Morimoto 		goto err_probe;
1208ffd60fbaSKuninori Morimoto 	}
1209ffd60fbaSKuninori Morimoto 
1210ffd60fbaSKuninori Morimoto 	for_each_component_dais(component, dai) {
1211ffd60fbaSKuninori Morimoto 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1212ffd60fbaSKuninori Morimoto 		if (ret != 0) {
1213ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1214ffd60fbaSKuninori Morimoto 				"Failed to create DAI widgets %d\n", ret);
1215ffd60fbaSKuninori Morimoto 			goto err_probe;
1216ffd60fbaSKuninori Morimoto 		}
1217ffd60fbaSKuninori Morimoto 	}
1218ffd60fbaSKuninori Morimoto 
1219ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_probe(component);
1220ffd60fbaSKuninori Morimoto 	if (ret < 0) {
1221ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1222ffd60fbaSKuninori Morimoto 			"ASoC: failed to probe component %d\n", ret);
1223ffd60fbaSKuninori Morimoto 		goto err_probe;
1224ffd60fbaSKuninori Morimoto 	}
1225ffd60fbaSKuninori Morimoto 	WARN(dapm->idle_bias_off &&
1226ffd60fbaSKuninori Morimoto 	     dapm->bias_level != SND_SOC_BIAS_OFF,
1227ffd60fbaSKuninori Morimoto 	     "codec %s can not start from non-off bias with idle_bias_off==1\n",
1228ffd60fbaSKuninori Morimoto 	     component->name);
1229c6619b72SKuninori Morimoto 	probed = 1;
1230ffd60fbaSKuninori Morimoto 
1231ffd60fbaSKuninori Morimoto 	/* machine specific init */
1232ffd60fbaSKuninori Morimoto 	if (component->init) {
1233ffd60fbaSKuninori Morimoto 		ret = component->init(component);
1234ffd60fbaSKuninori Morimoto 		if (ret < 0) {
1235ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1236ffd60fbaSKuninori Morimoto 				"Failed to do machine specific init %d\n", ret);
1237ffd60fbaSKuninori Morimoto 			goto err_probe;
1238ffd60fbaSKuninori Morimoto 		}
1239ffd60fbaSKuninori Morimoto 	}
1240ffd60fbaSKuninori Morimoto 
1241ffd60fbaSKuninori Morimoto 	ret = snd_soc_add_component_controls(component,
1242ffd60fbaSKuninori Morimoto 					     component->driver->controls,
1243ffd60fbaSKuninori Morimoto 					     component->driver->num_controls);
1244ffd60fbaSKuninori Morimoto 	if (ret < 0)
1245ffd60fbaSKuninori Morimoto 		goto err_probe;
1246ffd60fbaSKuninori Morimoto 
1247ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(dapm,
1248ffd60fbaSKuninori Morimoto 				      component->driver->dapm_routes,
1249ffd60fbaSKuninori Morimoto 				      component->driver->num_dapm_routes);
1250ffd60fbaSKuninori Morimoto 	if (ret < 0)
1251ffd60fbaSKuninori Morimoto 		goto err_probe;
1252ffd60fbaSKuninori Morimoto 
1253ffd60fbaSKuninori Morimoto 	/* see for_each_card_components */
1254ffd60fbaSKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1255ffd60fbaSKuninori Morimoto 
1256ffd60fbaSKuninori Morimoto err_probe:
1257ffd60fbaSKuninori Morimoto 	if (ret < 0)
1258c6619b72SKuninori Morimoto 		soc_remove_component(component, probed);
1259ffd60fbaSKuninori Morimoto 
1260ffd60fbaSKuninori Morimoto 	return ret;
1261ffd60fbaSKuninori Morimoto }
1262ffd60fbaSKuninori Morimoto 
1263e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1264589c3563SJarkko Nikula {
1265589c3563SJarkko Nikula 	int err;
1266589c3563SJarkko Nikula 
126752abe6ccSGuennadi Liakhovetski 	if (!dai || !dai->probed || !dai->driver ||
12682eda3cb1SKuninori Morimoto 	    dai->driver->remove_order != order)
12692eda3cb1SKuninori Morimoto 		return;
12702eda3cb1SKuninori Morimoto 
1271dcdab582SKuninori Morimoto 	err = snd_soc_dai_remove(dai);
1272589c3563SJarkko Nikula 	if (err < 0)
1273e60cd14fSLars-Peter Clausen 		dev_err(dai->dev,
1274b0aa88afSMisael Lopez Cruz 			"ASoC: failed to remove %s: %d\n",
1275e60cd14fSLars-Peter Clausen 			dai->name, err);
1276dcdab582SKuninori Morimoto 
1277e60cd14fSLars-Peter Clausen 	dai->probed = 0;
1278b0aa88afSMisael Lopez Cruz }
1279b0aa88afSMisael Lopez Cruz 
1280a7d44f78SKuninori Morimoto static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1281a7d44f78SKuninori Morimoto {
1282a7d44f78SKuninori Morimoto 	int ret;
1283a7d44f78SKuninori Morimoto 
1284a7d44f78SKuninori Morimoto 	if (dai->probed ||
1285a7d44f78SKuninori Morimoto 	    dai->driver->probe_order != order)
1286a7d44f78SKuninori Morimoto 		return 0;
1287a7d44f78SKuninori Morimoto 
1288a7d44f78SKuninori Morimoto 	ret = snd_soc_dai_probe(dai);
1289a7d44f78SKuninori Morimoto 	if (ret < 0) {
1290a7d44f78SKuninori Morimoto 		dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1291a7d44f78SKuninori Morimoto 			dai->name, ret);
1292a7d44f78SKuninori Morimoto 		return ret;
1293a7d44f78SKuninori Morimoto 	}
1294a7d44f78SKuninori Morimoto 
1295a7d44f78SKuninori Morimoto 	dai->probed = 1;
1296a7d44f78SKuninori Morimoto 
1297a7d44f78SKuninori Morimoto 	return 0;
1298a7d44f78SKuninori Morimoto }
1299a7d44f78SKuninori Morimoto 
13004ca47d21SKuninori Morimoto static void soc_remove_link_dais(struct snd_soc_card *card)
1301f0fba2adSLiam Girdwood {
1302e60cd14fSLars-Peter Clausen 	int i;
13030b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
13044ca47d21SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
13054ca47d21SKuninori Morimoto 	int order;
13064ca47d21SKuninori Morimoto 
13074ca47d21SKuninori Morimoto 	for_each_comp_order(order) {
13084ca47d21SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1309f0fba2adSLiam Girdwood 			/* remove the CODEC DAI */
13100b7990e3SKuninori Morimoto 			for_each_rtd_codec_dai(rtd, i, codec_dai)
13110b7990e3SKuninori Morimoto 				soc_remove_dai(codec_dai, order);
1312f0fba2adSLiam Girdwood 
1313e60cd14fSLars-Peter Clausen 			soc_remove_dai(rtd->cpu_dai, order);
1314f0fba2adSLiam Girdwood 		}
13154ca47d21SKuninori Morimoto 	}
13164ca47d21SKuninori Morimoto }
1317f0fba2adSLiam Girdwood 
1318bc7c16c2SKuninori Morimoto static int soc_probe_link_dais(struct snd_soc_card *card)
1319bc7c16c2SKuninori Morimoto {
1320bc7c16c2SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1321bc7c16c2SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
1322bc7c16c2SKuninori Morimoto 	int i, order, ret;
1323bc7c16c2SKuninori Morimoto 
1324bc7c16c2SKuninori Morimoto 	for_each_comp_order(order) {
1325bc7c16c2SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1326bc7c16c2SKuninori Morimoto 
1327bc7c16c2SKuninori Morimoto 			dev_dbg(card->dev,
1328bc7c16c2SKuninori Morimoto 				"ASoC: probe %s dai link %d late %d\n",
1329bc7c16c2SKuninori Morimoto 				card->name, rtd->num, order);
1330bc7c16c2SKuninori Morimoto 
1331bc7c16c2SKuninori Morimoto 			ret = soc_probe_dai(rtd->cpu_dai, order);
1332bc7c16c2SKuninori Morimoto 			if (ret)
1333bc7c16c2SKuninori Morimoto 				return ret;
1334bc7c16c2SKuninori Morimoto 
1335bc7c16c2SKuninori Morimoto 			/* probe the CODEC DAI */
1336bc7c16c2SKuninori Morimoto 			for_each_rtd_codec_dai(rtd, i, codec_dai) {
1337bc7c16c2SKuninori Morimoto 				ret = soc_probe_dai(codec_dai, order);
1338bc7c16c2SKuninori Morimoto 				if (ret)
1339bc7c16c2SKuninori Morimoto 					return ret;
1340bc7c16c2SKuninori Morimoto 			}
1341bc7c16c2SKuninori Morimoto 		}
1342bc7c16c2SKuninori Morimoto 	}
1343bc7c16c2SKuninori Morimoto 
1344bc7c16c2SKuninori Morimoto 	return 0;
1345bc7c16c2SKuninori Morimoto }
1346bc7c16c2SKuninori Morimoto 
1347b006c0c6SKuninori Morimoto static void soc_remove_link_components(struct snd_soc_card *card)
134862ae68faSStephen Warren {
134961aca564SLars-Peter Clausen 	struct snd_soc_component *component;
1350b006c0c6SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
135190be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
1352b006c0c6SKuninori Morimoto 	int order;
135362ae68faSStephen Warren 
1354b006c0c6SKuninori Morimoto 	for_each_comp_order(order) {
1355b006c0c6SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
13562b544dd7SKuninori Morimoto 			for_each_rtd_components(rtd, rtdcom, component) {
1357b006c0c6SKuninori Morimoto 				if (component->driver->remove_order != order)
1358b006c0c6SKuninori Morimoto 					continue;
1359b006c0c6SKuninori Morimoto 
1360c6619b72SKuninori Morimoto 				soc_remove_component(component, 1);
136162ae68faSStephen Warren 			}
136262ae68faSStephen Warren 		}
1363b006c0c6SKuninori Morimoto 	}
1364b006c0c6SKuninori Morimoto }
136562ae68faSStephen Warren 
136662f07a6bSKuninori Morimoto static int soc_probe_link_components(struct snd_soc_card *card)
13676fb03550SKuninori Morimoto {
13686fb03550SKuninori Morimoto 	struct snd_soc_component *component;
136962f07a6bSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
13706fb03550SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
137162f07a6bSKuninori Morimoto 	int ret, order;
13726fb03550SKuninori Morimoto 
137362f07a6bSKuninori Morimoto 	for_each_comp_order(order) {
137462f07a6bSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
13752b544dd7SKuninori Morimoto 			for_each_rtd_components(rtd, rtdcom, component) {
137662f07a6bSKuninori Morimoto 				if (component->driver->probe_order != order)
137762f07a6bSKuninori Morimoto 					continue;
137862f07a6bSKuninori Morimoto 
13796fb03550SKuninori Morimoto 				ret = soc_probe_component(card, component);
13806fb03550SKuninori Morimoto 				if (ret < 0)
13816fb03550SKuninori Morimoto 					return ret;
13826fb03550SKuninori Morimoto 			}
13836fb03550SKuninori Morimoto 		}
138462f07a6bSKuninori Morimoto 	}
13856fb03550SKuninori Morimoto 
13866fb03550SKuninori Morimoto 	return 0;
13876fb03550SKuninori Morimoto }
13886fb03550SKuninori Morimoto 
138925f7b701SArnaud Pouliquen static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
139025f7b701SArnaud Pouliquen 				struct snd_soc_pcm_runtime *rtd)
139125f7b701SArnaud Pouliquen {
139225f7b701SArnaud Pouliquen 	int i, ret = 0;
139325f7b701SArnaud Pouliquen 
139425f7b701SArnaud Pouliquen 	for (i = 0; i < num_dais; ++i) {
139525f7b701SArnaud Pouliquen 		struct snd_soc_dai_driver *drv = dais[i]->driver;
139625f7b701SArnaud Pouliquen 
1397de17f14eSRohit kumar 		if (drv->pcm_new)
139825f7b701SArnaud Pouliquen 			ret = drv->pcm_new(rtd, dais[i]);
139925f7b701SArnaud Pouliquen 		if (ret < 0) {
140025f7b701SArnaud Pouliquen 			dev_err(dais[i]->dev,
140125f7b701SArnaud Pouliquen 				"ASoC: Failed to bind %s with pcm device\n",
140225f7b701SArnaud Pouliquen 				dais[i]->name);
140325f7b701SArnaud Pouliquen 			return ret;
140425f7b701SArnaud Pouliquen 		}
140525f7b701SArnaud Pouliquen 	}
140625f7b701SArnaud Pouliquen 
140725f7b701SArnaud Pouliquen 	return 0;
140825f7b701SArnaud Pouliquen }
140925f7b701SArnaud Pouliquen 
1410c4b46982SKuninori Morimoto static int soc_link_init(struct snd_soc_card *card,
1411c4b46982SKuninori Morimoto 			 struct snd_soc_pcm_runtime *rtd)
1412c4b46982SKuninori Morimoto {
1413c4b46982SKuninori Morimoto 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1414c4b46982SKuninori Morimoto 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1415c4b46982SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
1416c4b46982SKuninori Morimoto 	struct snd_soc_component *component;
1417c4b46982SKuninori Morimoto 	int ret, num;
1418c4b46982SKuninori Morimoto 
1419c4b46982SKuninori Morimoto 	/* set default power off timeout */
1420c4b46982SKuninori Morimoto 	rtd->pmdown_time = pmdown_time;
14210168bf0dSLiam Girdwood 
14225f3484acSLars-Peter Clausen 	/* do machine specific initialization */
14235f3484acSLars-Peter Clausen 	if (dai_link->init) {
14245f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
14255f3484acSLars-Peter Clausen 		if (ret < 0) {
14265f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
14275f3484acSLars-Peter Clausen 				dai_link->name, ret);
14285f3484acSLars-Peter Clausen 			return ret;
14295f3484acSLars-Peter Clausen 		}
14305f3484acSLars-Peter Clausen 	}
14315f3484acSLars-Peter Clausen 
143240aa5383SRicard Wanderlof 	if (dai_link->dai_fmt) {
143340aa5383SRicard Wanderlof 		ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
143440aa5383SRicard Wanderlof 		if (ret)
143540aa5383SRicard Wanderlof 			return ret;
143640aa5383SRicard Wanderlof 	}
1437a5053a8eSKuninori Morimoto 
14385f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
14392e55b90aSLars-Peter Clausen 	soc_dpcm_debugfs_add(rtd);
14405f3484acSLars-Peter Clausen 
1441a655de80SLiam Girdwood 	num = rtd->num;
1442a655de80SLiam Girdwood 
1443a655de80SLiam Girdwood 	/*
1444a655de80SLiam Girdwood 	 * most drivers will register their PCMs using DAI link ordering but
1445a655de80SLiam Girdwood 	 * topology based drivers can use the DAI link id field to set PCM
1446a655de80SLiam Girdwood 	 * device number and then use rtd + a base offset of the BEs.
1447a655de80SLiam Girdwood 	 */
14482b544dd7SKuninori Morimoto 	for_each_rtd_components(rtd, rtdcom, component) {
1449a655de80SLiam Girdwood 		if (!component->driver->use_dai_pcm_id)
1450a655de80SLiam Girdwood 			continue;
1451a655de80SLiam Girdwood 
1452a655de80SLiam Girdwood 		if (rtd->dai_link->no_pcm)
1453a655de80SLiam Girdwood 			num += component->driver->be_pcm_base;
1454a655de80SLiam Girdwood 		else
1455a655de80SLiam Girdwood 			num = rtd->dai_link->id;
1456a655de80SLiam Girdwood 	}
1457a655de80SLiam Girdwood 
1458b423c420SKuninori Morimoto 	/* create compress_device if possible */
1459b423c420SKuninori Morimoto 	ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1460b423c420SKuninori Morimoto 	if (ret != -ENOTSUPP) {
1461b423c420SKuninori Morimoto 		if (ret < 0)
1462f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
14631245b700SNamarta Kohli 					 dai_link->stream_name);
14641245b700SNamarta Kohli 		return ret;
14651245b700SNamarta Kohli 	}
1466b423c420SKuninori Morimoto 
1467f0fba2adSLiam Girdwood 	/* create the pcm */
1468a655de80SLiam Girdwood 	ret = soc_new_pcm(rtd, num);
1469f0fba2adSLiam Girdwood 	if (ret < 0) {
1470f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
14710837fc62SFabio Estevam 			dai_link->stream_name, ret);
1472f0fba2adSLiam Girdwood 		return ret;
1473f0fba2adSLiam Girdwood 	}
147425f7b701SArnaud Pouliquen 	ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
147525f7b701SArnaud Pouliquen 	if (ret < 0)
147625f7b701SArnaud Pouliquen 		return ret;
147725f7b701SArnaud Pouliquen 	ret = soc_link_dai_pcm_new(rtd->codec_dais,
147825f7b701SArnaud Pouliquen 				   rtd->num_codecs, rtd);
147925f7b701SArnaud Pouliquen 	return ret;
1480f0fba2adSLiam Girdwood }
1481f0fba2adSLiam Girdwood 
1482e8fbd250SKuninori Morimoto static void soc_unbind_aux_dev(struct snd_soc_card *card)
14834893a2ebSKuninori Morimoto {
1484e8fbd250SKuninori Morimoto 	struct snd_soc_component *component, *_component;
1485e8fbd250SKuninori Morimoto 
1486e8fbd250SKuninori Morimoto 	for_each_card_auxs_safe(card, component, _component) {
14874893a2ebSKuninori Morimoto 		component->init = NULL;
14884893a2ebSKuninori Morimoto 		list_del(&component->card_aux_list);
14894893a2ebSKuninori Morimoto 	}
1490e8fbd250SKuninori Morimoto }
14914893a2ebSKuninori Morimoto 
1492bee886f1SKuninori Morimoto static int soc_bind_aux_dev(struct snd_soc_card *card)
1493b19e6e7bSMark Brown {
1494f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1495bee886f1SKuninori Morimoto 	struct snd_soc_aux_dev *aux;
1496bee886f1SKuninori Morimoto 	int i;
14973ca041edSSebastian Reichel 
1498bee886f1SKuninori Morimoto 	for_each_card_pre_auxs(card, i, aux) {
1499f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1500bee886f1SKuninori Morimoto 		component = soc_find_component(&aux->dlc);
1501f2ed6b07SMengdong Lin 		if (!component)
15023dc29b8bSKuninori Morimoto 			return -EPROBE_DEFER;
15033ca041edSSebastian Reichel 
1504bee886f1SKuninori Morimoto 		component->init = aux->init;
1505c2b71c71SKuninori Morimoto 		/* see for_each_card_auxs */
1506d2e3a135SSylwester Nawrocki 		list_add(&component->card_aux_list, &card->aux_comp_list);
1507bee886f1SKuninori Morimoto 	}
1508f2ed6b07SMengdong Lin 	return 0;
1509b19e6e7bSMark Brown }
1510b19e6e7bSMark Brown 
1511f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1512f2ed6b07SMengdong Lin {
151374bd3f92SKuninori Morimoto 	struct snd_soc_component *component;
1514f2ed6b07SMengdong Lin 	int order;
1515f2ed6b07SMengdong Lin 	int ret;
1516f2ed6b07SMengdong Lin 
15171a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
151874bd3f92SKuninori Morimoto 		for_each_card_auxs(card, component) {
151974bd3f92SKuninori Morimoto 			if (component->driver->probe_order != order)
152074bd3f92SKuninori Morimoto 				continue;
152174bd3f92SKuninori Morimoto 
152274bd3f92SKuninori Morimoto 			ret = soc_probe_component(card,	component);
152374bd3f92SKuninori Morimoto 			if (ret < 0)
1524f2ed6b07SMengdong Lin 				return ret;
1525f2ed6b07SMengdong Lin 		}
1526f2ed6b07SMengdong Lin 	}
152765d9361fSLars-Peter Clausen 
152844c69bb1SLars-Peter Clausen 	return 0;
15293ca041edSSebastian Reichel }
15302eea392dSJarkko Nikula 
1531f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
153244c69bb1SLars-Peter Clausen {
1533f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1534f2ed6b07SMengdong Lin 	int order;
153544c69bb1SLars-Peter Clausen 
15361a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1537c2b71c71SKuninori Morimoto 		for_each_card_auxs_safe(card, comp, _comp) {
1538e8fbd250SKuninori Morimoto 			if (comp->driver->remove_order == order)
1539c6619b72SKuninori Morimoto 				soc_remove_component(comp, 1);
15405f3484acSLars-Peter Clausen 		}
15412eea392dSJarkko Nikula 	}
15422eea392dSJarkko Nikula }
15432eea392dSJarkko Nikula 
1544ce64c8b9SLars-Peter Clausen /**
1545ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1546ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1547ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1548ce64c8b9SLars-Peter Clausen  *
1549ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1550ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1551ce64c8b9SLars-Peter Clausen  *
1552ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1553ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1554ce64c8b9SLars-Peter Clausen  *
1555ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1556ce64c8b9SLars-Peter Clausen  */
1557ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1558ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1559ce64c8b9SLars-Peter Clausen {
1560ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
15610b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1562ce64c8b9SLars-Peter Clausen 	unsigned int i;
1563ce64c8b9SLars-Peter Clausen 	int ret;
1564ce64c8b9SLars-Peter Clausen 
15650b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1566ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1567ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1568ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1569ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1570ce64c8b9SLars-Peter Clausen 			return ret;
1571ce64c8b9SLars-Peter Clausen 		}
1572ce64c8b9SLars-Peter Clausen 	}
1573ce64c8b9SLars-Peter Clausen 
15742c7b696aSMarcel Ziswiler 	/*
15752c7b696aSMarcel Ziswiler 	 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
15762c7b696aSMarcel Ziswiler 	 * the component which has non_legacy_dai_naming is Codec
15772c7b696aSMarcel Ziswiler 	 */
1578999f7f5aSKuninori Morimoto 	if (cpu_dai->component->driver->non_legacy_dai_naming) {
1579ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1580ce64c8b9SLars-Peter Clausen 
1581ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1582ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1583ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1584ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1585ce64c8b9SLars-Peter Clausen 			break;
1586ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1587ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1588ce64c8b9SLars-Peter Clausen 			break;
1589ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1590ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1591ce64c8b9SLars-Peter Clausen 			break;
1592ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1593ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1594ce64c8b9SLars-Peter Clausen 			break;
1595ce64c8b9SLars-Peter Clausen 		}
1596ce64c8b9SLars-Peter Clausen 
1597ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1598ce64c8b9SLars-Peter Clausen 	}
1599ce64c8b9SLars-Peter Clausen 
1600ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1601ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1602ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1603ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1604ce64c8b9SLars-Peter Clausen 		return ret;
1605ce64c8b9SLars-Peter Clausen 	}
1606ce64c8b9SLars-Peter Clausen 
1607ce64c8b9SLars-Peter Clausen 	return 0;
1608ce64c8b9SLars-Peter Clausen }
1609ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1610ce64c8b9SLars-Peter Clausen 
16111f5a4535STakashi Iwai #ifdef CONFIG_DMI
16122c7b696aSMarcel Ziswiler /*
16132c7b696aSMarcel Ziswiler  * Trim special characters, and replace '-' with '_' since '-' is used to
1614345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1615345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1616345233d7SLiam Girdwood  */
1617345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1618345233d7SLiam Girdwood {
1619345233d7SLiam Girdwood 	int i, j = 0;
1620345233d7SLiam Girdwood 
1621345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1622345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1623345233d7SLiam Girdwood 		    || (name[i] == '_'))
1624345233d7SLiam Girdwood 			name[j++] = name[i];
1625345233d7SLiam Girdwood 		else if (name[i] == '-')
1626345233d7SLiam Girdwood 			name[j++] = '_';
1627345233d7SLiam Girdwood 	}
1628345233d7SLiam Girdwood 
1629345233d7SLiam Girdwood 	name[j] = '\0';
1630345233d7SLiam Girdwood }
1631345233d7SLiam Girdwood 
16322c7b696aSMarcel Ziswiler /*
16332c7b696aSMarcel Ziswiler  * Check if a DMI field is valid, i.e. not containing any string
163498faf436SMengdong Lin  * in the black list.
163598faf436SMengdong Lin  */
163698faf436SMengdong Lin static int is_dmi_valid(const char *field)
163798faf436SMengdong Lin {
163898faf436SMengdong Lin 	int i = 0;
163998faf436SMengdong Lin 
164098faf436SMengdong Lin 	while (dmi_blacklist[i]) {
164198faf436SMengdong Lin 		if (strstr(field, dmi_blacklist[i]))
164298faf436SMengdong Lin 			return 0;
164398faf436SMengdong Lin 		i++;
164446b5a4d2SWu Fengguang 	}
164598faf436SMengdong Lin 
164698faf436SMengdong Lin 	return 1;
164798faf436SMengdong Lin }
164898faf436SMengdong Lin 
16494e01e5dbSJaroslav Kysela /*
16504e01e5dbSJaroslav Kysela  * Append a string to card->dmi_longname with character cleanups.
16514e01e5dbSJaroslav Kysela  */
16524e01e5dbSJaroslav Kysela static void append_dmi_string(struct snd_soc_card *card, const char *str)
16534e01e5dbSJaroslav Kysela {
16544e01e5dbSJaroslav Kysela 	char *dst = card->dmi_longname;
16554e01e5dbSJaroslav Kysela 	size_t dst_len = sizeof(card->dmi_longname);
16564e01e5dbSJaroslav Kysela 	size_t len;
16574e01e5dbSJaroslav Kysela 
16584e01e5dbSJaroslav Kysela 	len = strlen(dst);
16594e01e5dbSJaroslav Kysela 	snprintf(dst + len, dst_len - len, "-%s", str);
16604e01e5dbSJaroslav Kysela 
16614e01e5dbSJaroslav Kysela 	len++;	/* skip the separator "-" */
16624e01e5dbSJaroslav Kysela 	if (len < dst_len)
16634e01e5dbSJaroslav Kysela 		cleanup_dmi_name(dst + len);
16644e01e5dbSJaroslav Kysela }
16654e01e5dbSJaroslav Kysela 
1666345233d7SLiam Girdwood /**
1667345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
1668345233d7SLiam Girdwood  * @card: The card to register DMI names
1669345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
1670345233d7SLiam Girdwood  *
1671345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
1672345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
1673345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
1674345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
1675345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
1676345233d7SLiam Girdwood  * as the sound card long name, in the format of
1677345233d7SLiam Girdwood  * "vendor-product-version-board"
1678345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
1679345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
1680345233d7SLiam Girdwood  * (UCM) configurations for the card.
1681345233d7SLiam Girdwood  *
1682345233d7SLiam Girdwood  * Possible card long names may be:
1683345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
1684345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1685345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1686345233d7SLiam Girdwood  *
1687345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
1688345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
1689345233d7SLiam Girdwood  *
1690345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
1691345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
1692345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
1693345233d7SLiam Girdwood  * charactors like SPACE.
1694345233d7SLiam Girdwood  *
1695345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
1696345233d7SLiam Girdwood  */
1697345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1698345233d7SLiam Girdwood {
1699345233d7SLiam Girdwood 	const char *vendor, *product, *product_version, *board;
1700345233d7SLiam Girdwood 
1701345233d7SLiam Girdwood 	if (card->long_name)
1702345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
1703345233d7SLiam Girdwood 
17044e01e5dbSJaroslav Kysela 	/* make up dmi long name as: vendor-product-version-board */
1705345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
170698faf436SMengdong Lin 	if (!vendor || !is_dmi_valid(vendor)) {
1707345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1708345233d7SLiam Girdwood 		return 0;
1709345233d7SLiam Girdwood 	}
1710345233d7SLiam Girdwood 
17114e01e5dbSJaroslav Kysela 	snprintf(card->dmi_longname, sizeof(card->dmi_longname), "%s", vendor);
1712345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
1713345233d7SLiam Girdwood 
1714345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
171598faf436SMengdong Lin 	if (product && is_dmi_valid(product)) {
17164e01e5dbSJaroslav Kysela 		append_dmi_string(card, product);
1717345233d7SLiam Girdwood 
17182c7b696aSMarcel Ziswiler 		/*
17192c7b696aSMarcel Ziswiler 		 * some vendors like Lenovo may only put a self-explanatory
1720345233d7SLiam Girdwood 		 * name in the product version field
1721345233d7SLiam Girdwood 		 */
1722345233d7SLiam Girdwood 		product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
17234e01e5dbSJaroslav Kysela 		if (product_version && is_dmi_valid(product_version))
17244e01e5dbSJaroslav Kysela 			append_dmi_string(card, product_version);
1725345233d7SLiam Girdwood 	}
1726345233d7SLiam Girdwood 
1727345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
172898faf436SMengdong Lin 	if (board && is_dmi_valid(board)) {
172939870b0dSJaroslav Kysela 		if (!product || strcasecmp(board, product))
17304e01e5dbSJaroslav Kysela 			append_dmi_string(card, board);
1731345233d7SLiam Girdwood 	} else if (!product) {
1732345233d7SLiam Girdwood 		/* fall back to using legacy name */
1733345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1734345233d7SLiam Girdwood 		return 0;
1735345233d7SLiam Girdwood 	}
1736345233d7SLiam Girdwood 
1737345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
17384e01e5dbSJaroslav Kysela 	if (flavour)
17394e01e5dbSJaroslav Kysela 		append_dmi_string(card, flavour);
1740345233d7SLiam Girdwood 
1741345233d7SLiam Girdwood 	/* set the card long name */
1742345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
1743345233d7SLiam Girdwood 
1744345233d7SLiam Girdwood 	return 0;
1745345233d7SLiam Girdwood }
1746345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
17471f5a4535STakashi Iwai #endif /* CONFIG_DMI */
1748345233d7SLiam Girdwood 
1749a655de80SLiam Girdwood static void soc_check_tplg_fes(struct snd_soc_card *card)
1750a655de80SLiam Girdwood {
1751a655de80SLiam Girdwood 	struct snd_soc_component *component;
1752a655de80SLiam Girdwood 	const struct snd_soc_component_driver *comp_drv;
1753a655de80SLiam Girdwood 	struct snd_soc_dai_link *dai_link;
1754a655de80SLiam Girdwood 	int i;
1755a655de80SLiam Girdwood 
1756368dee94SKuninori Morimoto 	for_each_component(component) {
1757a655de80SLiam Girdwood 
175849f9c4f2SDaniel Baluta 		/* does this component override BEs ? */
1759a655de80SLiam Girdwood 		if (!component->driver->ignore_machine)
1760a655de80SLiam Girdwood 			continue;
1761a655de80SLiam Girdwood 
1762a655de80SLiam Girdwood 		/* for this machine ? */
1763e194098bSPierre-Louis Bossart 		if (!strcmp(component->driver->ignore_machine,
1764a655de80SLiam Girdwood 			    card->dev->driver->name))
1765e194098bSPierre-Louis Bossart 			goto match;
1766e194098bSPierre-Louis Bossart 		if (strcmp(component->driver->ignore_machine,
1767e194098bSPierre-Louis Bossart 			   dev_name(card->dev)))
1768a655de80SLiam Girdwood 			continue;
1769e194098bSPierre-Louis Bossart match:
1770a655de80SLiam Girdwood 		/* machine matches, so override the rtd data */
17717fe072b4SKuninori Morimoto 		for_each_card_prelinks(card, i, dai_link) {
1772a655de80SLiam Girdwood 
1773a655de80SLiam Girdwood 			/* ignore this FE */
1774a655de80SLiam Girdwood 			if (dai_link->dynamic) {
1775a655de80SLiam Girdwood 				dai_link->ignore = true;
1776a655de80SLiam Girdwood 				continue;
1777a655de80SLiam Girdwood 			}
1778a655de80SLiam Girdwood 
177949f9c4f2SDaniel Baluta 			dev_info(card->dev, "info: override BE DAI link %s\n",
1780a655de80SLiam Girdwood 				 card->dai_link[i].name);
1781a655de80SLiam Girdwood 
1782a655de80SLiam Girdwood 			/* override platform component */
1783adb76b5bSKuninori Morimoto 			if (!dai_link->platforms) {
1784daecf46eSKuninori Morimoto 				dev_err(card->dev, "init platform error");
1785daecf46eSKuninori Morimoto 				continue;
1786daecf46eSKuninori Morimoto 			}
1787910fdcabSKuninori Morimoto 			dai_link->platforms->name = component->name;
1788a655de80SLiam Girdwood 
1789a655de80SLiam Girdwood 			/* convert non BE into BE */
1790a655de80SLiam Girdwood 			dai_link->no_pcm = 1;
1791a655de80SLiam Girdwood 
1792a655de80SLiam Girdwood 			/* override any BE fixups */
1793a655de80SLiam Girdwood 			dai_link->be_hw_params_fixup =
1794a655de80SLiam Girdwood 				component->driver->be_hw_params_fixup;
1795a655de80SLiam Girdwood 
17962c7b696aSMarcel Ziswiler 			/*
17972c7b696aSMarcel Ziswiler 			 * most BE links don't set stream name, so set it to
1798a655de80SLiam Girdwood 			 * dai link name if it's NULL to help bind widgets.
1799a655de80SLiam Girdwood 			 */
1800a655de80SLiam Girdwood 			if (!dai_link->stream_name)
1801a655de80SLiam Girdwood 				dai_link->stream_name = dai_link->name;
1802a655de80SLiam Girdwood 		}
1803a655de80SLiam Girdwood 
1804a655de80SLiam Girdwood 		/* Inform userspace we are using alternate topology */
1805a655de80SLiam Girdwood 		if (component->driver->topology_name_prefix) {
1806a655de80SLiam Girdwood 
1807a655de80SLiam Girdwood 			/* topology shortname created? */
1808a655de80SLiam Girdwood 			if (!card->topology_shortname_created) {
1809a655de80SLiam Girdwood 				comp_drv = component->driver;
1810a655de80SLiam Girdwood 
1811a655de80SLiam Girdwood 				snprintf(card->topology_shortname, 32, "%s-%s",
1812a655de80SLiam Girdwood 					 comp_drv->topology_name_prefix,
1813a655de80SLiam Girdwood 					 card->name);
1814a655de80SLiam Girdwood 				card->topology_shortname_created = true;
1815a655de80SLiam Girdwood 			}
1816a655de80SLiam Girdwood 
1817a655de80SLiam Girdwood 			/* use topology shortname */
1818a655de80SLiam Girdwood 			card->name = card->topology_shortname;
1819a655de80SLiam Girdwood 		}
1820a655de80SLiam Girdwood 	}
1821a655de80SLiam Girdwood }
1822a655de80SLiam Girdwood 
18230f23f718SKuninori Morimoto #define soc_setup_card_name(name, name1, name2, norm)		\
18240f23f718SKuninori Morimoto 	__soc_setup_card_name(name, sizeof(name), name1, name2, norm)
18250f23f718SKuninori Morimoto static void __soc_setup_card_name(char *name, int len,
18260f23f718SKuninori Morimoto 				  const char *name1, const char *name2,
18270f23f718SKuninori Morimoto 				  int normalization)
18280f23f718SKuninori Morimoto {
18290f23f718SKuninori Morimoto 	int i;
18300f23f718SKuninori Morimoto 
18310f23f718SKuninori Morimoto 	snprintf(name, len, "%s", name1 ? name1 : name2);
18320f23f718SKuninori Morimoto 
18330f23f718SKuninori Morimoto 	if (!normalization)
18340f23f718SKuninori Morimoto 		return;
18350f23f718SKuninori Morimoto 
18360f23f718SKuninori Morimoto 	/*
18370f23f718SKuninori Morimoto 	 * Name normalization
18380f23f718SKuninori Morimoto 	 *
18390f23f718SKuninori Morimoto 	 * The driver name is somewhat special, as it's used as a key for
18400f23f718SKuninori Morimoto 	 * searches in the user-space.
18410f23f718SKuninori Morimoto 	 *
18420f23f718SKuninori Morimoto 	 * ex)
18430f23f718SKuninori Morimoto 	 *	"abcd??efg" -> "abcd__efg"
18440f23f718SKuninori Morimoto 	 */
18450f23f718SKuninori Morimoto 	for (i = 0; i < len; i++) {
18460f23f718SKuninori Morimoto 		switch (name[i]) {
18470f23f718SKuninori Morimoto 		case '_':
18480f23f718SKuninori Morimoto 		case '-':
18490f23f718SKuninori Morimoto 		case '\0':
18500f23f718SKuninori Morimoto 			break;
18510f23f718SKuninori Morimoto 		default:
18520f23f718SKuninori Morimoto 			if (!isalnum(name[i]))
18530f23f718SKuninori Morimoto 				name[i] = '_';
18540f23f718SKuninori Morimoto 			break;
18550f23f718SKuninori Morimoto 		}
18560f23f718SKuninori Morimoto 	}
18570f23f718SKuninori Morimoto }
18580f23f718SKuninori Morimoto 
1859ce21401cSKuninori Morimoto static void soc_cleanup_card_resources(struct snd_soc_card *card,
1860ce21401cSKuninori Morimoto 				       int card_probed)
186153e947a0SKuninori Morimoto {
1862cc733900SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd, *n;
18637ce6088fSKuninori Morimoto 
18640ced7b05SKuninori Morimoto 	if (card->snd_card)
18650ced7b05SKuninori Morimoto 		snd_card_disconnect_sync(card->snd_card);
186653e947a0SKuninori Morimoto 
18672a6f0892SKuninori Morimoto 	snd_soc_dapm_shutdown(card);
18682a6f0892SKuninori Morimoto 
186953e947a0SKuninori Morimoto 	/* remove and free each DAI */
18707ce6088fSKuninori Morimoto 	soc_remove_link_dais(card);
18710ced7b05SKuninori Morimoto 	soc_remove_link_components(card);
18727ce6088fSKuninori Morimoto 
1873cc733900SKuninori Morimoto 	for_each_card_rtds_safe(card, rtd, n)
1874cc733900SKuninori Morimoto 		snd_soc_remove_dai_link(card, rtd->dai_link);
18757ce6088fSKuninori Morimoto 
187653e947a0SKuninori Morimoto 	/* remove auxiliary devices */
187753e947a0SKuninori Morimoto 	soc_remove_aux_devices(card);
1878e8fbd250SKuninori Morimoto 	soc_unbind_aux_dev(card);
187953e947a0SKuninori Morimoto 
188053e947a0SKuninori Morimoto 	snd_soc_dapm_free(&card->dapm);
188153e947a0SKuninori Morimoto 	soc_cleanup_card_debugfs(card);
188253e947a0SKuninori Morimoto 
188353e947a0SKuninori Morimoto 	/* remove the card */
1884ce21401cSKuninori Morimoto 	if (card_probed && card->remove)
188553e947a0SKuninori Morimoto 		card->remove(card);
18860ced7b05SKuninori Morimoto 
18870ced7b05SKuninori Morimoto 	if (card->snd_card) {
18880ced7b05SKuninori Morimoto 		snd_card_free(card->snd_card);
18890ced7b05SKuninori Morimoto 		card->snd_card = NULL;
18900ced7b05SKuninori Morimoto 	}
189153e947a0SKuninori Morimoto }
189253e947a0SKuninori Morimoto 
18932cc1afcfSKuninori Morimoto static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
18942cc1afcfSKuninori Morimoto {
18952cc1afcfSKuninori Morimoto 	if (card->instantiated) {
1896ce21401cSKuninori Morimoto 		int card_probed = 1;
1897ce21401cSKuninori Morimoto 
18982cc1afcfSKuninori Morimoto 		card->instantiated = false;
18992cc1afcfSKuninori Morimoto 		snd_soc_flush_all_delayed_work(card);
19002cc1afcfSKuninori Morimoto 
1901ce21401cSKuninori Morimoto 		soc_cleanup_card_resources(card, card_probed);
19022cc1afcfSKuninori Morimoto 		if (!unregister)
19032cc1afcfSKuninori Morimoto 			list_add(&card->list, &unbind_card_list);
19042cc1afcfSKuninori Morimoto 	} else {
19052cc1afcfSKuninori Morimoto 		if (unregister)
19062cc1afcfSKuninori Morimoto 			list_del(&card->list);
19072cc1afcfSKuninori Morimoto 	}
19082cc1afcfSKuninori Morimoto }
19092cc1afcfSKuninori Morimoto 
1910ed90c013SKuninori Morimoto static int snd_soc_bind_card(struct snd_soc_card *card)
1911f0fba2adSLiam Girdwood {
19121a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
191361b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
1914ce21401cSKuninori Morimoto 	int ret, i, card_probed = 0;
191596dd3622SMark Brown 
191634e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
191701b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1918f0fba2adSLiam Girdwood 
191995c267ddSKuninori Morimoto 	snd_soc_dapm_init(&card->dapm, card, NULL);
192053e947a0SKuninori Morimoto 
1921a655de80SLiam Girdwood 	/* check whether any platform is ignore machine FE and using topology */
1922a655de80SLiam Girdwood 	soc_check_tplg_fes(card);
1923a655de80SLiam Girdwood 
192444c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
1925bee886f1SKuninori Morimoto 	ret = soc_bind_aux_dev(card);
1926bee886f1SKuninori Morimoto 	if (ret < 0)
192753e947a0SKuninori Morimoto 		goto probe_end;
1928db2a4165SFrank Mandarino 
1929f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
1930d8145989SKuninori Morimoto 	card->num_rtd = 0;
19315b99a0aaSKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link) {
1932*0c048004SKuninori Morimoto 		ret = snd_soc_add_pcm_runtime(card, dai_link);
19335b99a0aaSKuninori Morimoto 		if (ret < 0)
19345b99a0aaSKuninori Morimoto 			goto probe_end;
19355b99a0aaSKuninori Morimoto 	}
1936f8f80361SMengdong Lin 
1937f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1938102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1939f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1940f0fba2adSLiam Girdwood 	if (ret < 0) {
194110e8aa9aSMichał Mirosław 		dev_err(card->dev,
194210e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
194310e8aa9aSMichał Mirosław 			card->name, ret);
194453e947a0SKuninori Morimoto 		goto probe_end;
1945db2a4165SFrank Mandarino 	}
1946db2a4165SFrank Mandarino 
19470757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
19480757d834SLars-Peter Clausen 
1949b3da4251SKuninori Morimoto 	soc_resume_init(card);
19506ed25978SAndy Green 
1951b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
19529a841ebbSMark Brown 					card->num_dapm_widgets);
1953b8ba3b57SKuninori Morimoto 	if (ret < 0)
1954b8ba3b57SKuninori Morimoto 		goto probe_end;
19559a841ebbSMark Brown 
1956b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1957f23e860eSNicolin Chen 					card->num_of_dapm_widgets);
1958b8ba3b57SKuninori Morimoto 	if (ret < 0)
1959b8ba3b57SKuninori Morimoto 		goto probe_end;
1960f23e860eSNicolin Chen 
1961f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1962f0fba2adSLiam Girdwood 	if (card->probe) {
1963e7361ec4SMark Brown 		ret = card->probe(card);
1964f0fba2adSLiam Girdwood 		if (ret < 0)
196553e947a0SKuninori Morimoto 			goto probe_end;
1966ce21401cSKuninori Morimoto 		card_probed = 1;
1967f0fba2adSLiam Girdwood 	}
1968f0fba2adSLiam Girdwood 
196962ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
197062f07a6bSKuninori Morimoto 	ret = soc_probe_link_components(card);
197162ae68faSStephen Warren 	if (ret < 0) {
1972f110bfc7SLiam Girdwood 		dev_err(card->dev,
197362f07a6bSKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
197453e947a0SKuninori Morimoto 		goto probe_end;
197562ae68faSStephen Warren 	}
197662ae68faSStephen Warren 
1977f2ed6b07SMengdong Lin 	/* probe auxiliary components */
1978f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
197974bd3f92SKuninori Morimoto 	if (ret < 0) {
198074bd3f92SKuninori Morimoto 		dev_err(card->dev,
198174bd3f92SKuninori Morimoto 			"ASoC: failed to probe aux component %d\n", ret);
198253e947a0SKuninori Morimoto 		goto probe_end;
198374bd3f92SKuninori Morimoto 	}
1984f2ed6b07SMengdong Lin 
198562ae68faSStephen Warren 	/* probe all DAI links on this card */
1986c7e73774SKuninori Morimoto 	ret = soc_probe_link_dais(card);
1987fe3e78e0SMark Brown 	if (ret < 0) {
1988f110bfc7SLiam Girdwood 		dev_err(card->dev,
1989c7e73774SKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
199053e947a0SKuninori Morimoto 		goto probe_end;
1991fe3e78e0SMark Brown 	}
1992fe3e78e0SMark Brown 
1993c4b46982SKuninori Morimoto 	for_each_card_rtds(card, rtd)
1994c4b46982SKuninori Morimoto 		soc_link_init(card, rtd);
1995c4b46982SKuninori Morimoto 
1996888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
1997b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
1998888df395SMark Brown 
19999b98c7c2SKuninori Morimoto 	ret = snd_soc_add_card_controls(card, card->controls,
20002c7b696aSMarcel Ziswiler 					card->num_controls);
20019b98c7c2SKuninori Morimoto 	if (ret < 0)
20029b98c7c2SKuninori Morimoto 		goto probe_end;
2003b7af1dafSMark Brown 
2004daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2005b8ad29deSMark Brown 				      card->num_dapm_routes);
2006daa480bdSKuninori Morimoto 	if (ret < 0)
2007daa480bdSKuninori Morimoto 		goto probe_end;
2008b8ad29deSMark Brown 
2009daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2010f23e860eSNicolin Chen 				      card->num_of_dapm_routes);
2011daa480bdSKuninori Morimoto 	if (ret < 0)
2012daa480bdSKuninori Morimoto 		goto probe_end;
201375d9ac46SMark Brown 
2014861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
2015861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
2016861886d3STakashi Iwai 
20170f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->shortname,
20180f23f718SKuninori Morimoto 			    card->name, NULL, 0);
20190f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->longname,
20200f23f718SKuninori Morimoto 			    card->long_name, card->name, 0);
20210f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->driver,
20220f23f718SKuninori Morimoto 			    card->driver_name, card->name, 1);
2023fe3e78e0SMark Brown 
2024dc73d73aSJaroslav Kysela 	if (card->components) {
2025dc73d73aSJaroslav Kysela 		/* the current implementation of snd_component_add() accepts */
2026dc73d73aSJaroslav Kysela 		/* multiple components in the string separated by space, */
2027dc73d73aSJaroslav Kysela 		/* but the string collision (identical string) check might */
2028dc73d73aSJaroslav Kysela 		/* not work correctly */
2029dc73d73aSJaroslav Kysela 		ret = snd_component_add(card->snd_card, card->components);
2030dc73d73aSJaroslav Kysela 		if (ret < 0) {
2031dc73d73aSJaroslav Kysela 			dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n",
2032dc73d73aSJaroslav Kysela 				card->name, ret);
2033dc73d73aSJaroslav Kysela 			goto probe_end;
2034dc73d73aSJaroslav Kysela 		}
2035dc73d73aSJaroslav Kysela 	}
2036dc73d73aSJaroslav Kysela 
203728e9ad92SMark Brown 	if (card->late_probe) {
203828e9ad92SMark Brown 		ret = card->late_probe(card);
203928e9ad92SMark Brown 		if (ret < 0) {
2040f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
204128e9ad92SMark Brown 				card->name, ret);
204253e947a0SKuninori Morimoto 			goto probe_end;
204328e9ad92SMark Brown 		}
204428e9ad92SMark Brown 	}
2045ce21401cSKuninori Morimoto 	card_probed = 1;
204628e9ad92SMark Brown 
2047824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
20488c193b8dSLars-Peter Clausen 
2049f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2050fe3e78e0SMark Brown 	if (ret < 0) {
2051f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2052f110bfc7SLiam Girdwood 				ret);
205353e947a0SKuninori Morimoto 		goto probe_end;
2054fe3e78e0SMark Brown 	}
2055fe3e78e0SMark Brown 
2056435c5e25SMark Brown 	card->instantiated = 1;
2057882eab6cSTzung-Bi Shih 	dapm_mark_endpoints_dirty(card);
20584f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2059b19e6e7bSMark Brown 
2060ed90c013SKuninori Morimoto 	/* deactivate pins to sleep state */
2061ed90c013SKuninori Morimoto 	for_each_card_rtds(card, rtd) {
2062ed90c013SKuninori Morimoto 		struct snd_soc_dai *dai;
2063ed90c013SKuninori Morimoto 
2064ed90c013SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
2065ed90c013SKuninori Morimoto 			if (!dai->active)
2066ed90c013SKuninori Morimoto 				pinctrl_pm_select_sleep_state(dai->dev);
2067ed90c013SKuninori Morimoto 		}
2068ed90c013SKuninori Morimoto 
2069ed90c013SKuninori Morimoto 		if (!rtd->cpu_dai->active)
2070ed90c013SKuninori Morimoto 			pinctrl_pm_select_sleep_state(rtd->cpu_dai->dev);
2071ed90c013SKuninori Morimoto 	}
2072ed90c013SKuninori Morimoto 
207353e947a0SKuninori Morimoto probe_end:
207453e947a0SKuninori Morimoto 	if (ret < 0)
2075ce21401cSKuninori Morimoto 		soc_cleanup_card_resources(card, card_probed);
2076db2a4165SFrank Mandarino 
2077f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
207834e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2079db2a4165SFrank Mandarino 
2080b19e6e7bSMark Brown 	return ret;
2081435c5e25SMark Brown }
2082435c5e25SMark Brown 
2083435c5e25SMark Brown /* probes a new socdev */
2084435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2085435c5e25SMark Brown {
2086f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2087435c5e25SMark Brown 
208870a7ca34SVinod Koul 	/*
208970a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
209070a7ca34SVinod Koul 	 * we should not be here in that case so ret error
209170a7ca34SVinod Koul 	 */
209270a7ca34SVinod Koul 	if (!card)
209370a7ca34SVinod Koul 		return -EINVAL;
209470a7ca34SVinod Koul 
2095fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2096f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2097fe4085e8SMark Brown 		 card->name);
2098fe4085e8SMark Brown 
2099435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2100435c5e25SMark Brown 	card->dev = &pdev->dev;
2101f0fba2adSLiam Girdwood 
210228d528c8SMark Brown 	return snd_soc_register_card(card);
2103435c5e25SMark Brown }
2104435c5e25SMark Brown 
2105b0e26485SVinod Koul /* removes a socdev */
2106b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2107b0e26485SVinod Koul {
2108b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2109b0e26485SVinod Koul 
2110c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2111db2a4165SFrank Mandarino 	return 0;
2112db2a4165SFrank Mandarino }
2113db2a4165SFrank Mandarino 
21146f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
211551737470SMark Brown {
21166f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
21171a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
211851737470SMark Brown 
211951737470SMark Brown 	if (!card->instantiated)
2120416356fcSMark Brown 		return 0;
212151737470SMark Brown 
21222c7b696aSMarcel Ziswiler 	/*
21232c7b696aSMarcel Ziswiler 	 * Flush out pmdown_time work - we actually do want to run it
21242c7b696aSMarcel Ziswiler 	 * now, we're shutting down so no imminent restart.
21252c7b696aSMarcel Ziswiler 	 */
212665462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
212751737470SMark Brown 
2128f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2129416356fcSMark Brown 
2130988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
2131bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
213288bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
21330b7990e3SKuninori Morimoto 		struct snd_soc_dai *codec_dai;
21341a497983SMengdong Lin 		int i;
213588bd870fSBenoit Cousson 
2136988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
21370b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
213888bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
213988bd870fSBenoit Cousson 		}
2140988e8cc4SNicolin Chen 	}
2141988e8cc4SNicolin Chen 
2142416356fcSMark Brown 	return 0;
214351737470SMark Brown }
21446f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
214551737470SMark Brown 
21466f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2147b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2148b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2149b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2150b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
21516f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2152b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2153416356fcSMark Brown };
2154deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2155416356fcSMark Brown 
2156db2a4165SFrank Mandarino /* ASoC platform driver */
2157db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2158db2a4165SFrank Mandarino 	.driver		= {
2159db2a4165SFrank Mandarino 		.name		= "soc-audio",
21606f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2161db2a4165SFrank Mandarino 	},
2162db2a4165SFrank Mandarino 	.probe		= soc_probe,
2163db2a4165SFrank Mandarino 	.remove		= soc_remove,
2164db2a4165SFrank Mandarino };
2165db2a4165SFrank Mandarino 
2166096e49d5SMark Brown /**
2167db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2168db2a4165SFrank Mandarino  * @_template: control template
2169db2a4165SFrank Mandarino  * @data: control private data
2170ac11a2b3SMark Brown  * @long_name: control long name
2171efb7ac3fSMark Brown  * @prefix: control name prefix
2172db2a4165SFrank Mandarino  *
2173db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2174db2a4165SFrank Mandarino  *
2175db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2176db2a4165SFrank Mandarino  */
2177db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
21783056557fSMark Brown 				  void *data, const char *long_name,
2179efb7ac3fSMark Brown 				  const char *prefix)
2180db2a4165SFrank Mandarino {
2181db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2182efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2183efb7ac3fSMark Brown 	char *name = NULL;
2184db2a4165SFrank Mandarino 
2185db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2186db2a4165SFrank Mandarino 	template.index = 0;
2187db2a4165SFrank Mandarino 
2188efb7ac3fSMark Brown 	if (!long_name)
2189efb7ac3fSMark Brown 		long_name = template.name;
2190efb7ac3fSMark Brown 
2191efb7ac3fSMark Brown 	if (prefix) {
21922b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2193efb7ac3fSMark Brown 		if (!name)
2194efb7ac3fSMark Brown 			return NULL;
2195efb7ac3fSMark Brown 
2196efb7ac3fSMark Brown 		template.name = name;
2197efb7ac3fSMark Brown 	} else {
2198efb7ac3fSMark Brown 		template.name = long_name;
2199efb7ac3fSMark Brown 	}
2200efb7ac3fSMark Brown 
2201efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2202efb7ac3fSMark Brown 
2203efb7ac3fSMark Brown 	kfree(name);
2204efb7ac3fSMark Brown 
2205efb7ac3fSMark Brown 	return kcontrol;
2206db2a4165SFrank Mandarino }
2207db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2208db2a4165SFrank Mandarino 
2209022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2210022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2211022658beSLiam Girdwood 	const char *prefix, void *data)
2212022658beSLiam Girdwood {
2213022658beSLiam Girdwood 	int err, i;
2214022658beSLiam Girdwood 
2215022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2216022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
22172c7b696aSMarcel Ziswiler 
2218022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2219022658beSLiam Girdwood 						     control->name, prefix));
2220022658beSLiam Girdwood 		if (err < 0) {
2221f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2222f110bfc7SLiam Girdwood 				control->name, err);
2223022658beSLiam Girdwood 			return err;
2224022658beSLiam Girdwood 		}
2225022658beSLiam Girdwood 	}
2226022658beSLiam Girdwood 
2227022658beSLiam Girdwood 	return 0;
2228022658beSLiam Girdwood }
2229022658beSLiam Girdwood 
22304fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
22314fefd698SDimitris Papastamos 					       const char *name)
22324fefd698SDimitris Papastamos {
22334fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
22344fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
22354fefd698SDimitris Papastamos 
22364fefd698SDimitris Papastamos 	if (unlikely(!name))
22374fefd698SDimitris Papastamos 		return NULL;
22384fefd698SDimitris Papastamos 
22394fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
22404fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
22414fefd698SDimitris Papastamos 			return kctl;
22424fefd698SDimitris Papastamos 	return NULL;
22434fefd698SDimitris Papastamos }
22444fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
22454fefd698SDimitris Papastamos 
2246db2a4165SFrank Mandarino /**
22470f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
22480f2780adSLars-Peter Clausen  *
22490f2780adSLars-Peter Clausen  * @component: Component to add controls to
22500f2780adSLars-Peter Clausen  * @controls: Array of controls to add
22510f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
22520f2780adSLars-Peter Clausen  *
22530f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
22540f2780adSLars-Peter Clausen  */
22550f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
22560f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
22570f2780adSLars-Peter Clausen {
22580f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
22590f2780adSLars-Peter Clausen 
22600f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
22610f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
22620f2780adSLars-Peter Clausen }
22630f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
22640f2780adSLars-Peter Clausen 
22650f2780adSLars-Peter Clausen /**
2266022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2267022658beSLiam Girdwood  * Convenience function to add a list of controls.
2268022658beSLiam Girdwood  *
2269022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2270022658beSLiam Girdwood  * @controls: array of controls to add
2271022658beSLiam Girdwood  * @num_controls: number of elements in the array
2272022658beSLiam Girdwood  *
2273022658beSLiam Girdwood  * Return 0 for success, else error.
2274022658beSLiam Girdwood  */
2275022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2276022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2277022658beSLiam Girdwood {
2278022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2279022658beSLiam Girdwood 
2280022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2281022658beSLiam Girdwood 			NULL, soc_card);
2282022658beSLiam Girdwood }
2283022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2284022658beSLiam Girdwood 
2285022658beSLiam Girdwood /**
2286022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2287022658beSLiam Girdwood  * Convienience function to add a list of controls.
2288022658beSLiam Girdwood  *
2289022658beSLiam Girdwood  * @dai: DAI to add controls to
2290022658beSLiam Girdwood  * @controls: array of controls to add
2291022658beSLiam Girdwood  * @num_controls: number of elements in the array
2292022658beSLiam Girdwood  *
2293022658beSLiam Girdwood  * Return 0 for success, else error.
2294022658beSLiam Girdwood  */
2295022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2296022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2297022658beSLiam Girdwood {
2298313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2299022658beSLiam Girdwood 
2300022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2301022658beSLiam Girdwood 			NULL, dai);
2302022658beSLiam Girdwood }
2303022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2304022658beSLiam Girdwood 
2305c5af3a2eSMark Brown /**
2306c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2307c5af3a2eSMark Brown  *
2308ac11a2b3SMark Brown  * @card: Card to register
2309c5af3a2eSMark Brown  *
2310c5af3a2eSMark Brown  */
231170a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2312c5af3a2eSMark Brown {
2313c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2314c5af3a2eSMark Brown 		return -EINVAL;
2315c5af3a2eSMark Brown 
2316ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2317ed77cc12SMark Brown 
2318b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->widgets);
2319b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->paths);
2320b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->dapm_list);
2321b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->aux_comp_list);
2322b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->component_dev_list);
2323b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->list);
23241a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
2325db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
23268a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2327b03bfaecSKuninori Morimoto 
2328c5af3a2eSMark Brown 	card->instantiated = 0;
2329f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
2330a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
233172b745e3SPeter Ujfalusi 	mutex_init(&card->pcm_mutex);
2332a9764869SKaiChieh Chuang 	spin_lock_init(&card->dpcm_lock);
2333c5af3a2eSMark Brown 
2334e894efefSSrinivas Kandagatla 	return snd_soc_bind_card(card);
2335c5af3a2eSMark Brown }
233670a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2337c5af3a2eSMark Brown 
2338c5af3a2eSMark Brown /**
2339c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2340c5af3a2eSMark Brown  *
2341ac11a2b3SMark Brown  * @card: Card to unregister
2342c5af3a2eSMark Brown  *
2343c5af3a2eSMark Brown  */
234470a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2345c5af3a2eSMark Brown {
2346b545542aSKuninori Morimoto 	mutex_lock(&client_mutex);
2347e894efefSSrinivas Kandagatla 	snd_soc_unbind_card(card, true);
2348b545542aSKuninori Morimoto 	mutex_unlock(&client_mutex);
2349f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2350c5af3a2eSMark Brown 
2351c5af3a2eSMark Brown 	return 0;
2352c5af3a2eSMark Brown }
235370a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2354c5af3a2eSMark Brown 
2355f0fba2adSLiam Girdwood /*
2356f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
2357f0fba2adSLiam Girdwood  * and sanitizing names.
2358f0fba2adSLiam Girdwood  */
23590b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2360f0fba2adSLiam Girdwood {
2361f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
2362f0fba2adSLiam Girdwood 	int id1, id2;
2363f0fba2adSLiam Girdwood 
2364f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
2365f0fba2adSLiam Girdwood 		return NULL;
2366f0fba2adSLiam Girdwood 
236758818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2368f0fba2adSLiam Girdwood 
2369f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
2370f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
2371f0fba2adSLiam Girdwood 	if (found) {
2372f0fba2adSLiam Girdwood 		/* get ID */
2373f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2374f0fba2adSLiam Girdwood 
2375f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
2376f0fba2adSLiam Girdwood 			if (*id == -1)
2377f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
2378f0fba2adSLiam Girdwood 		}
2379f0fba2adSLiam Girdwood 
2380f0fba2adSLiam Girdwood 	} else {
2381f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr" */
2382f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2383f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
2384f0fba2adSLiam Girdwood 
2385f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
238605899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
2387f0fba2adSLiam Girdwood 
2388f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
23892c7b696aSMarcel Ziswiler 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
23902c7b696aSMarcel Ziswiler 				 name);
239158818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2392f0fba2adSLiam Girdwood 		} else
2393f0fba2adSLiam Girdwood 			*id = 0;
2394f0fba2adSLiam Girdwood 	}
2395f0fba2adSLiam Girdwood 
239650014499SKuninori Morimoto 	return devm_kstrdup(dev, name, GFP_KERNEL);
2397f0fba2adSLiam Girdwood }
2398f0fba2adSLiam Girdwood 
2399f0fba2adSLiam Girdwood /*
2400f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
2401f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
2402f0fba2adSLiam Girdwood  */
2403f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
2404f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
2405f0fba2adSLiam Girdwood {
2406f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
240710e8aa9aSMichał Mirosław 		dev_err(dev,
240810e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
240910e8aa9aSMichał Mirosław 			dev_name(dev));
2410f0fba2adSLiam Girdwood 		return NULL;
2411f0fba2adSLiam Girdwood 	}
2412f0fba2adSLiam Girdwood 
241350014499SKuninori Morimoto 	return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2414f0fba2adSLiam Girdwood }
2415f0fba2adSLiam Girdwood 
2416ffdbca0bSKuninori Morimoto void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2417e11381f3SKuninori Morimoto {
2418e11381f3SKuninori Morimoto 	dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2419e11381f3SKuninori Morimoto 	list_del(&dai->list);
2420e11381f3SKuninori Morimoto }
2421ffdbca0bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2422e11381f3SKuninori Morimoto 
24237ca24386SKuninori Morimoto /**
24247ca24386SKuninori Morimoto  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
24257ca24386SKuninori Morimoto  *
24267ca24386SKuninori Morimoto  * @component: The component the DAIs are registered for
24277ca24386SKuninori Morimoto  * @dai_drv: DAI driver to use for the DAI
2428bc9a6655SRandy Dunlap  * @legacy_dai_naming: if %true, use legacy single-name format;
2429bc9a6655SRandy Dunlap  * 	if %false, use multiple-name format;
24307ca24386SKuninori Morimoto  *
24317ca24386SKuninori Morimoto  * Topology can use this API to register DAIs when probing a component.
24327ca24386SKuninori Morimoto  * These DAIs's widgets will be freed in the card cleanup and the DAIs
24337ca24386SKuninori Morimoto  * will be freed in the component cleanup.
24347ca24386SKuninori Morimoto  */
24357ca24386SKuninori Morimoto struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
24365e4fb372SMengdong Lin 					 struct snd_soc_dai_driver *dai_drv,
24375e4fb372SMengdong Lin 					 bool legacy_dai_naming)
24385e4fb372SMengdong Lin {
24395e4fb372SMengdong Lin 	struct device *dev = component->dev;
24405e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
24415e4fb372SMengdong Lin 
24425e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
24435e4fb372SMengdong Lin 
24447ca24386SKuninori Morimoto 	lockdep_assert_held(&client_mutex);
24457ca24386SKuninori Morimoto 
244650014499SKuninori Morimoto 	dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
24475e4fb372SMengdong Lin 	if (dai == NULL)
24485e4fb372SMengdong Lin 		return NULL;
24495e4fb372SMengdong Lin 
24505e4fb372SMengdong Lin 	/*
24515e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
24525e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
24535e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
24545e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
24555e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
24565e4fb372SMengdong Lin 	 * component-less anymore.
24575e4fb372SMengdong Lin 	 */
24585e4fb372SMengdong Lin 	if (legacy_dai_naming &&
24595e4fb372SMengdong Lin 	    (dai_drv->id == 0 || dai_drv->name == NULL)) {
24605e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
24615e4fb372SMengdong Lin 	} else {
24625e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
24635e4fb372SMengdong Lin 		if (dai_drv->id)
24645e4fb372SMengdong Lin 			dai->id = dai_drv->id;
24655e4fb372SMengdong Lin 		else
24665e4fb372SMengdong Lin 			dai->id = component->num_dai;
24675e4fb372SMengdong Lin 	}
246850014499SKuninori Morimoto 	if (!dai->name)
24695e4fb372SMengdong Lin 		return NULL;
24705e4fb372SMengdong Lin 
24715e4fb372SMengdong Lin 	dai->component = component;
24725e4fb372SMengdong Lin 	dai->dev = dev;
24735e4fb372SMengdong Lin 	dai->driver = dai_drv;
24745e4fb372SMengdong Lin 	if (!dai->driver->ops)
24755e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
24765e4fb372SMengdong Lin 
247715a0c645SKuninori Morimoto 	/* see for_each_component_dais */
247858bf4179SKuninori Morimoto 	list_add_tail(&dai->list, &component->dai_list);
24795e4fb372SMengdong Lin 	component->num_dai++;
24805e4fb372SMengdong Lin 
24815e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
24825e4fb372SMengdong Lin 	return dai;
24835e4fb372SMengdong Lin }
2484e11381f3SKuninori Morimoto 
24859115171aSMark Brown /**
24863f6674aeSKuninori Morimoto  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
24873f6674aeSKuninori Morimoto  *
24883f6674aeSKuninori Morimoto  * @component: The component for which the DAIs should be unregistered
24893f6674aeSKuninori Morimoto  */
24903f6674aeSKuninori Morimoto static void snd_soc_unregister_dais(struct snd_soc_component *component)
24913f6674aeSKuninori Morimoto {
24923f6674aeSKuninori Morimoto 	struct snd_soc_dai *dai, *_dai;
24933f6674aeSKuninori Morimoto 
2494e11381f3SKuninori Morimoto 	for_each_component_dais_safe(component, dai, _dai)
2495e11381f3SKuninori Morimoto 		snd_soc_unregister_dai(dai);
24963f6674aeSKuninori Morimoto }
24973f6674aeSKuninori Morimoto 
24983f6674aeSKuninori Morimoto /**
2499daf77373SKuninori Morimoto  * snd_soc_register_dais - Register a DAI with the ASoC core
2500daf77373SKuninori Morimoto  *
2501daf77373SKuninori Morimoto  * @component: The component the DAIs are registered for
2502daf77373SKuninori Morimoto  * @dai_drv: DAI driver to use for the DAIs
2503daf77373SKuninori Morimoto  * @count: Number of DAIs
2504daf77373SKuninori Morimoto  */
2505daf77373SKuninori Morimoto static int snd_soc_register_dais(struct snd_soc_component *component,
2506daf77373SKuninori Morimoto 				 struct snd_soc_dai_driver *dai_drv,
2507daf77373SKuninori Morimoto 				 size_t count)
2508daf77373SKuninori Morimoto {
2509daf77373SKuninori Morimoto 	struct snd_soc_dai *dai;
2510daf77373SKuninori Morimoto 	unsigned int i;
2511daf77373SKuninori Morimoto 	int ret;
2512daf77373SKuninori Morimoto 
2513daf77373SKuninori Morimoto 	for (i = 0; i < count; i++) {
251471cb85f5SKuninori Morimoto 		dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
2515daf77373SKuninori Morimoto 				  !component->driver->non_legacy_dai_naming);
2516daf77373SKuninori Morimoto 		if (dai == NULL) {
2517daf77373SKuninori Morimoto 			ret = -ENOMEM;
2518daf77373SKuninori Morimoto 			goto err;
2519daf77373SKuninori Morimoto 		}
2520daf77373SKuninori Morimoto 	}
2521daf77373SKuninori Morimoto 
2522daf77373SKuninori Morimoto 	return 0;
2523daf77373SKuninori Morimoto 
2524daf77373SKuninori Morimoto err:
2525daf77373SKuninori Morimoto 	snd_soc_unregister_dais(component);
2526daf77373SKuninori Morimoto 
2527daf77373SKuninori Morimoto 	return ret;
2528daf77373SKuninori Morimoto }
2529daf77373SKuninori Morimoto 
2530bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
2531bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
2532d191bd8dSKuninori Morimoto {
25338d92bb51SKuninori Morimoto 	INIT_LIST_HEAD(&component->dai_list);
2534495efdb0SKuninori Morimoto 	INIT_LIST_HEAD(&component->dobj_list);
2535495efdb0SKuninori Morimoto 	INIT_LIST_HEAD(&component->card_list);
25368d92bb51SKuninori Morimoto 	mutex_init(&component->io_mutex);
25378d92bb51SKuninori Morimoto 
2538bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
2539bb13109dSLars-Peter Clausen 	if (!component->name) {
2540bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
2541d191bd8dSKuninori Morimoto 		return -ENOMEM;
2542d191bd8dSKuninori Morimoto 	}
2543d191bd8dSKuninori Morimoto 
2544bb13109dSLars-Peter Clausen 	component->dev = dev;
2545bb13109dSLars-Peter Clausen 	component->driver = driver;
2546e2c330b9SLars-Peter Clausen 
2547bb13109dSLars-Peter Clausen 	return 0;
2548d191bd8dSKuninori Morimoto }
2549d191bd8dSKuninori Morimoto 
255020feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2551886f5692SLars-Peter Clausen {
2552886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
255320feb881SLars-Peter Clausen 
2554886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
2555886f5692SLars-Peter Clausen 	if (val_bytes > 0)
2556886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
2557886f5692SLars-Peter Clausen }
255820feb881SLars-Peter Clausen 
2559e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
2560e874bf5fSLars-Peter Clausen 
256120feb881SLars-Peter Clausen /**
25622c7b696aSMarcel Ziswiler  * snd_soc_component_init_regmap() - Initialize regmap instance for the
25632c7b696aSMarcel Ziswiler  *                                   component
256420feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
256520feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
256620feb881SLars-Peter Clausen  *
256720feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
256820feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
256920feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
257020feb881SLars-Peter Clausen  * before the first IO attempt of the component.
257120feb881SLars-Peter Clausen  */
257220feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
257320feb881SLars-Peter Clausen 	struct regmap *regmap)
257420feb881SLars-Peter Clausen {
257520feb881SLars-Peter Clausen 	component->regmap = regmap;
257620feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
2577886f5692SLars-Peter Clausen }
257820feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
257920feb881SLars-Peter Clausen 
258020feb881SLars-Peter Clausen /**
25812c7b696aSMarcel Ziswiler  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
25822c7b696aSMarcel Ziswiler  *                                   component
258320feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
258420feb881SLars-Peter Clausen  *
258520feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
258620feb881SLars-Peter Clausen  * removes the regmap instance from the component.
258720feb881SLars-Peter Clausen  *
258820feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
258920feb881SLars-Peter Clausen  * to initialize the regmap instance.
259020feb881SLars-Peter Clausen  */
259120feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
259220feb881SLars-Peter Clausen {
259320feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
259420feb881SLars-Peter Clausen 	component->regmap = NULL;
259520feb881SLars-Peter Clausen }
259620feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2597886f5692SLars-Peter Clausen 
2598e874bf5fSLars-Peter Clausen #endif
2599d191bd8dSKuninori Morimoto 
2600273d778eSKuninori Morimoto #define ENDIANNESS_MAP(name) \
2601273d778eSKuninori Morimoto 	(SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2602273d778eSKuninori Morimoto static u64 endianness_format_map[] = {
2603273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S16_),
2604273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U16_),
2605273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_),
2606273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_),
2607273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S32_),
2608273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U32_),
2609273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_3),
2610273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_3),
2611273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S20_3),
2612273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U20_3),
2613273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S18_3),
2614273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U18_3),
2615273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT_),
2616273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT64_),
2617273d778eSKuninori Morimoto 	ENDIANNESS_MAP(IEC958_SUBFRAME_),
2618273d778eSKuninori Morimoto };
2619273d778eSKuninori Morimoto 
2620273d778eSKuninori Morimoto /*
2621273d778eSKuninori Morimoto  * Fix up the DAI formats for endianness: codecs don't actually see
2622273d778eSKuninori Morimoto  * the endianness of the data but we're using the CPU format
2623273d778eSKuninori Morimoto  * definitions which do need to include endianness so we ensure that
2624273d778eSKuninori Morimoto  * codec DAIs always have both big and little endian variants set.
2625273d778eSKuninori Morimoto  */
2626273d778eSKuninori Morimoto static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2627273d778eSKuninori Morimoto {
2628273d778eSKuninori Morimoto 	int i;
2629273d778eSKuninori Morimoto 
2630273d778eSKuninori Morimoto 	for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2631273d778eSKuninori Morimoto 		if (stream->formats & endianness_format_map[i])
2632273d778eSKuninori Morimoto 			stream->formats |= endianness_format_map[i];
2633273d778eSKuninori Morimoto }
2634273d778eSKuninori Morimoto 
2635e894efefSSrinivas Kandagatla static void snd_soc_try_rebind_card(void)
2636e894efefSSrinivas Kandagatla {
2637e894efefSSrinivas Kandagatla 	struct snd_soc_card *card, *c;
2638e894efefSSrinivas Kandagatla 
2639b245d273SKuninori Morimoto 	list_for_each_entry_safe(card, c, &unbind_card_list, list)
2640e894efefSSrinivas Kandagatla 		if (!snd_soc_bind_card(card))
2641e894efefSSrinivas Kandagatla 			list_del(&card->list);
2642e894efefSSrinivas Kandagatla }
2643e894efefSSrinivas Kandagatla 
2644486c7978SKuninori Morimoto static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2645486c7978SKuninori Morimoto {
2646b18768f5SKuninori Morimoto 	struct snd_soc_card *card = component->card;
2647b18768f5SKuninori Morimoto 
2648486c7978SKuninori Morimoto 	snd_soc_unregister_dais(component);
2649b18768f5SKuninori Morimoto 
2650b18768f5SKuninori Morimoto 	if (card)
2651b18768f5SKuninori Morimoto 		snd_soc_unbind_card(card, false);
2652b18768f5SKuninori Morimoto 
2653b18768f5SKuninori Morimoto 	list_del(&component->list);
2654486c7978SKuninori Morimoto }
2655486c7978SKuninori Morimoto 
2656e0dac41bSKuninori Morimoto int snd_soc_add_component(struct device *dev,
2657e0dac41bSKuninori Morimoto 			struct snd_soc_component *component,
2658cf9e829eSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2659d191bd8dSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2660d191bd8dSKuninori Morimoto 			int num_dai)
2661d191bd8dSKuninori Morimoto {
2662bb13109dSLars-Peter Clausen 	int ret;
2663273d778eSKuninori Morimoto 	int i;
2664d191bd8dSKuninori Morimoto 
2665b18768f5SKuninori Morimoto 	mutex_lock(&client_mutex);
2666b18768f5SKuninori Morimoto 
2667cf9e829eSKuninori Morimoto 	ret = snd_soc_component_initialize(component, component_driver, dev);
2668bb13109dSLars-Peter Clausen 	if (ret)
2669bb13109dSLars-Peter Clausen 		goto err_free;
2670bb13109dSLars-Peter Clausen 
2671273d778eSKuninori Morimoto 	if (component_driver->endianness) {
2672273d778eSKuninori Morimoto 		for (i = 0; i < num_dai; i++) {
2673273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].playback);
2674273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].capture);
2675273d778eSKuninori Morimoto 		}
2676273d778eSKuninori Morimoto 	}
2677273d778eSKuninori Morimoto 
26780e7b25c6SKuninori Morimoto 	ret = snd_soc_register_dais(component, dai_drv, num_dai);
2679bb13109dSLars-Peter Clausen 	if (ret < 0) {
2680f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
2681bb13109dSLars-Peter Clausen 		goto err_cleanup;
2682bb13109dSLars-Peter Clausen 	}
2683bb13109dSLars-Peter Clausen 
2684b18768f5SKuninori Morimoto 	if (!component->driver->write && !component->driver->read) {
2685b18768f5SKuninori Morimoto 		if (!component->regmap)
2686b18768f5SKuninori Morimoto 			component->regmap = dev_get_regmap(component->dev,
2687b18768f5SKuninori Morimoto 							   NULL);
2688b18768f5SKuninori Morimoto 		if (component->regmap)
2689b18768f5SKuninori Morimoto 			snd_soc_component_setup_regmap(component);
2690b18768f5SKuninori Morimoto 	}
2691bb13109dSLars-Peter Clausen 
2692b18768f5SKuninori Morimoto 	/* see for_each_component */
2693b18768f5SKuninori Morimoto 	list_add(&component->list, &component_list);
2694bb13109dSLars-Peter Clausen 
2695bb13109dSLars-Peter Clausen err_cleanup:
2696b18768f5SKuninori Morimoto 	if (ret < 0)
2697486c7978SKuninori Morimoto 		snd_soc_del_component_unlocked(component);
2698bb13109dSLars-Peter Clausen err_free:
2699b18768f5SKuninori Morimoto 	mutex_unlock(&client_mutex);
2700b18768f5SKuninori Morimoto 
2701b18768f5SKuninori Morimoto 	if (ret == 0)
2702b18768f5SKuninori Morimoto 		snd_soc_try_rebind_card();
2703b18768f5SKuninori Morimoto 
2704bb13109dSLars-Peter Clausen 	return ret;
2705d191bd8dSKuninori Morimoto }
2706e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component);
2707e0dac41bSKuninori Morimoto 
2708e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev,
2709e0dac41bSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2710e0dac41bSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2711e0dac41bSKuninori Morimoto 			int num_dai)
2712e0dac41bSKuninori Morimoto {
2713e0dac41bSKuninori Morimoto 	struct snd_soc_component *component;
2714e0dac41bSKuninori Morimoto 
27157ecbd6a9SKuninori Morimoto 	component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
271608e61d03SKuninori Morimoto 	if (!component)
2717e0dac41bSKuninori Morimoto 		return -ENOMEM;
2718e0dac41bSKuninori Morimoto 
2719e0dac41bSKuninori Morimoto 	return snd_soc_add_component(dev, component, component_driver,
2720e0dac41bSKuninori Morimoto 				     dai_drv, num_dai);
2721e0dac41bSKuninori Morimoto }
2722d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
2723d191bd8dSKuninori Morimoto 
2724d191bd8dSKuninori Morimoto /**
27252eccea8cSKuninori Morimoto  * snd_soc_unregister_component - Unregister all related component
27262eccea8cSKuninori Morimoto  * from the ASoC core
2727d191bd8dSKuninori Morimoto  *
2728628536eaSJonathan Corbet  * @dev: The device to unregister
2729d191bd8dSKuninori Morimoto  */
27302eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
27312eccea8cSKuninori Morimoto {
2732ac6a4dd3SKuninori Morimoto 	struct snd_soc_component *component;
2733ac6a4dd3SKuninori Morimoto 
2734ac6a4dd3SKuninori Morimoto 	mutex_lock(&client_mutex);
2735ac6a4dd3SKuninori Morimoto 	while (1) {
273618dd66eaSKuninori Morimoto 		component = snd_soc_lookup_component_nolocked(dev, NULL);
2737ac6a4dd3SKuninori Morimoto 		if (!component)
2738ac6a4dd3SKuninori Morimoto 			break;
2739ac6a4dd3SKuninori Morimoto 
2740ac6a4dd3SKuninori Morimoto 		snd_soc_del_component_unlocked(component);
2741ac6a4dd3SKuninori Morimoto 	}
2742ac6a4dd3SKuninori Morimoto 	mutex_unlock(&client_mutex);
2743d191bd8dSKuninori Morimoto }
2744d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2745d191bd8dSKuninori Morimoto 
2746bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
2747b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2748bec4fa05SStephen Warren 			       const char *propname)
2749bec4fa05SStephen Warren {
2750b07609ceSKuninori Morimoto 	struct device_node *np;
2751bec4fa05SStephen Warren 	int ret;
2752bec4fa05SStephen Warren 
27537e07e7c0STushar Behera 	if (!card->dev) {
27547e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
27557e07e7c0STushar Behera 		return -EINVAL;
27567e07e7c0STushar Behera 	}
27577e07e7c0STushar Behera 
27587e07e7c0STushar Behera 	np = card->dev->of_node;
27597e07e7c0STushar Behera 
2760bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
2761bec4fa05SStephen Warren 	/*
2762bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
2763bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
2764bec4fa05SStephen Warren 	 * snd_soc_register_card.
2765bec4fa05SStephen Warren 	 */
2766bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
2767bec4fa05SStephen Warren 		dev_err(card->dev,
2768f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
2769bec4fa05SStephen Warren 			propname, ret);
2770bec4fa05SStephen Warren 		return ret;
2771bec4fa05SStephen Warren 	}
2772bec4fa05SStephen Warren 
2773bec4fa05SStephen Warren 	return 0;
2774bec4fa05SStephen Warren }
2775b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2776bec4fa05SStephen Warren 
27779a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
27789a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
27799a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
27809a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
27819a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
27829a6d4860SXiubo Li };
27839a6d4860SXiubo Li 
278421efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
27859a6d4860SXiubo Li 					  const char *propname)
27869a6d4860SXiubo Li {
278721efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
27889a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
27899a6d4860SXiubo Li 	const char *template, *wname;
27909a6d4860SXiubo Li 	int i, j, num_widgets, ret;
27919a6d4860SXiubo Li 
27929a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
27939a6d4860SXiubo Li 	if (num_widgets < 0) {
27949a6d4860SXiubo Li 		dev_err(card->dev,
27959a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
27969a6d4860SXiubo Li 		return -EINVAL;
27979a6d4860SXiubo Li 	}
27989a6d4860SXiubo Li 	if (num_widgets & 1) {
27999a6d4860SXiubo Li 		dev_err(card->dev,
28009a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
28019a6d4860SXiubo Li 		return -EINVAL;
28029a6d4860SXiubo Li 	}
28039a6d4860SXiubo Li 
28049a6d4860SXiubo Li 	num_widgets /= 2;
28059a6d4860SXiubo Li 	if (!num_widgets) {
28069a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
28079a6d4860SXiubo Li 			propname);
28089a6d4860SXiubo Li 		return -EINVAL;
28099a6d4860SXiubo Li 	}
28109a6d4860SXiubo Li 
28119a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
28129a6d4860SXiubo Li 			       GFP_KERNEL);
28139a6d4860SXiubo Li 	if (!widgets) {
28149a6d4860SXiubo Li 		dev_err(card->dev,
28159a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
28169a6d4860SXiubo Li 		return -ENOMEM;
28179a6d4860SXiubo Li 	}
28189a6d4860SXiubo Li 
28199a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
28209a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
28219a6d4860SXiubo Li 			2 * i, &template);
28229a6d4860SXiubo Li 		if (ret) {
28239a6d4860SXiubo Li 			dev_err(card->dev,
28249a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
28259a6d4860SXiubo Li 				propname, 2 * i, ret);
28269a6d4860SXiubo Li 			return -EINVAL;
28279a6d4860SXiubo Li 		}
28289a6d4860SXiubo Li 
28299a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
28309a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
28319a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
28329a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
28339a6d4860SXiubo Li 				break;
28349a6d4860SXiubo Li 			}
28359a6d4860SXiubo Li 		}
28369a6d4860SXiubo Li 
28379a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
28389a6d4860SXiubo Li 			dev_err(card->dev,
28399a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
28409a6d4860SXiubo Li 				template);
28419a6d4860SXiubo Li 			return -EINVAL;
28429a6d4860SXiubo Li 		}
28439a6d4860SXiubo Li 
28449a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
28459a6d4860SXiubo Li 						    (2 * i) + 1,
28469a6d4860SXiubo Li 						    &wname);
28479a6d4860SXiubo Li 		if (ret) {
28489a6d4860SXiubo Li 			dev_err(card->dev,
28499a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
28509a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
28519a6d4860SXiubo Li 			return -EINVAL;
28529a6d4860SXiubo Li 		}
28539a6d4860SXiubo Li 
28549a6d4860SXiubo Li 		widgets[i].name = wname;
28559a6d4860SXiubo Li 	}
28569a6d4860SXiubo Li 
2857f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
2858f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
28599a6d4860SXiubo Li 
28609a6d4860SXiubo Li 	return 0;
28619a6d4860SXiubo Li }
286221efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
28639a6d4860SXiubo Li 
2864cbdfab3bSJerome Brunet int snd_soc_of_get_slot_mask(struct device_node *np,
28656131084aSJyri Sarha 			     const char *prop_name,
28666131084aSJyri Sarha 			     unsigned int *mask)
28676131084aSJyri Sarha {
28686131084aSJyri Sarha 	u32 val;
28696c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
28706131084aSJyri Sarha 	int i;
28716131084aSJyri Sarha 
28726131084aSJyri Sarha 	if (!of_slot_mask)
28736131084aSJyri Sarha 		return 0;
28746131084aSJyri Sarha 	val /= sizeof(u32);
28756131084aSJyri Sarha 	for (i = 0; i < val; i++)
28766131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
28776131084aSJyri Sarha 			*mask |= (1 << i);
28786131084aSJyri Sarha 
28796131084aSJyri Sarha 	return val;
28806131084aSJyri Sarha }
2881cbdfab3bSJerome Brunet EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
28826131084aSJyri Sarha 
288389c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
28846131084aSJyri Sarha 			      unsigned int *tx_mask,
28856131084aSJyri Sarha 			      unsigned int *rx_mask,
288689c67857SXiubo Li 			      unsigned int *slots,
288789c67857SXiubo Li 			      unsigned int *slot_width)
288889c67857SXiubo Li {
288989c67857SXiubo Li 	u32 val;
289089c67857SXiubo Li 	int ret;
289189c67857SXiubo Li 
28926131084aSJyri Sarha 	if (tx_mask)
28936131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
28946131084aSJyri Sarha 	if (rx_mask)
28956131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
28966131084aSJyri Sarha 
289789c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
289889c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
289989c67857SXiubo Li 		if (ret)
290089c67857SXiubo Li 			return ret;
290189c67857SXiubo Li 
290289c67857SXiubo Li 		if (slots)
290389c67857SXiubo Li 			*slots = val;
290489c67857SXiubo Li 	}
290589c67857SXiubo Li 
290689c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
290789c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
290889c67857SXiubo Li 		if (ret)
290989c67857SXiubo Li 			return ret;
291089c67857SXiubo Li 
291189c67857SXiubo Li 		if (slot_width)
291289c67857SXiubo Li 			*slot_width = val;
291389c67857SXiubo Li 	}
291489c67857SXiubo Li 
291589c67857SXiubo Li 	return 0;
291689c67857SXiubo Li }
291789c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
291889c67857SXiubo Li 
29193b710356SKuninori Morimoto void snd_soc_of_parse_node_prefix(struct device_node *np,
29205e3cdaa2SKuninori Morimoto 				  struct snd_soc_codec_conf *codec_conf,
29215e3cdaa2SKuninori Morimoto 				  struct device_node *of_node,
29225e3cdaa2SKuninori Morimoto 				  const char *propname)
29235e3cdaa2SKuninori Morimoto {
29245e3cdaa2SKuninori Morimoto 	const char *str;
29255e3cdaa2SKuninori Morimoto 	int ret;
29265e3cdaa2SKuninori Morimoto 
29275e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
29285e3cdaa2SKuninori Morimoto 	if (ret < 0) {
29295e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
29305e3cdaa2SKuninori Morimoto 		return;
29315e3cdaa2SKuninori Morimoto 	}
29325e3cdaa2SKuninori Morimoto 
29335e3cdaa2SKuninori Morimoto 	codec_conf->of_node	= of_node;
29345e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
29355e3cdaa2SKuninori Morimoto }
29363b710356SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
29375e3cdaa2SKuninori Morimoto 
29382bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
2939a4a54dd5SStephen Warren 				   const char *propname)
2940a4a54dd5SStephen Warren {
29412bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
2942e3b1e6a1SMark Brown 	int num_routes;
2943a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
2944a4a54dd5SStephen Warren 	int i, ret;
2945a4a54dd5SStephen Warren 
2946a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
2947c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
294810e8aa9aSMichał Mirosław 		dev_err(card->dev,
294910e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
295010e8aa9aSMichał Mirosław 			propname);
2951a4a54dd5SStephen Warren 		return -EINVAL;
2952a4a54dd5SStephen Warren 	}
2953a4a54dd5SStephen Warren 	num_routes /= 2;
2954a4a54dd5SStephen Warren 	if (!num_routes) {
2955f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
2956a4a54dd5SStephen Warren 			propname);
2957a4a54dd5SStephen Warren 		return -EINVAL;
2958a4a54dd5SStephen Warren 	}
2959a4a54dd5SStephen Warren 
2960a86854d0SKees Cook 	routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
2961a4a54dd5SStephen Warren 			      GFP_KERNEL);
2962a4a54dd5SStephen Warren 	if (!routes) {
2963a4a54dd5SStephen Warren 		dev_err(card->dev,
2964f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
2965a4a54dd5SStephen Warren 		return -EINVAL;
2966a4a54dd5SStephen Warren 	}
2967a4a54dd5SStephen Warren 
2968a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
2969a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
2970e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
2971a4a54dd5SStephen Warren 		if (ret) {
2972c871bd0bSMark Brown 			dev_err(card->dev,
2973c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
2974c871bd0bSMark Brown 				propname, 2 * i, ret);
2975a4a54dd5SStephen Warren 			return -EINVAL;
2976a4a54dd5SStephen Warren 		}
2977a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
2978e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
2979a4a54dd5SStephen Warren 		if (ret) {
2980a4a54dd5SStephen Warren 			dev_err(card->dev,
2981c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
2982c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
2983a4a54dd5SStephen Warren 			return -EINVAL;
2984a4a54dd5SStephen Warren 		}
2985a4a54dd5SStephen Warren 	}
2986a4a54dd5SStephen Warren 
2987f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
2988f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
2989a4a54dd5SStephen Warren 
2990a4a54dd5SStephen Warren 	return 0;
2991a4a54dd5SStephen Warren }
29922bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
2993a4a54dd5SStephen Warren 
2994a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
2995389cb834SJyri Sarha 				     const char *prefix,
2996389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
2997389cb834SJyri Sarha 				     struct device_node **framemaster)
2998a7930ed4SKuninori Morimoto {
2999a7930ed4SKuninori Morimoto 	int ret, i;
3000a7930ed4SKuninori Morimoto 	char prop[128];
3001a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3002a7930ed4SKuninori Morimoto 	int bit, frame;
3003a7930ed4SKuninori Morimoto 	const char *str;
3004a7930ed4SKuninori Morimoto 	struct {
3005a7930ed4SKuninori Morimoto 		char *name;
3006a7930ed4SKuninori Morimoto 		unsigned int val;
3007a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3008a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3009a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3010a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3011a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3012a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3013a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3014a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3015a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3016a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3017a7930ed4SKuninori Morimoto 	};
3018a7930ed4SKuninori Morimoto 
3019a7930ed4SKuninori Morimoto 	if (!prefix)
3020a7930ed4SKuninori Morimoto 		prefix = "";
3021a7930ed4SKuninori Morimoto 
3022a7930ed4SKuninori Morimoto 	/*
30235711c979SKuninori Morimoto 	 * check "dai-format = xxx"
30245711c979SKuninori Morimoto 	 * or    "[prefix]format = xxx"
3025a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3026a7930ed4SKuninori Morimoto 	 */
30275711c979SKuninori Morimoto 	ret = of_property_read_string(np, "dai-format", &str);
30285711c979SKuninori Morimoto 	if (ret < 0) {
3029a7930ed4SKuninori Morimoto 		snprintf(prop, sizeof(prop), "%sformat", prefix);
3030a7930ed4SKuninori Morimoto 		ret = of_property_read_string(np, prop, &str);
30315711c979SKuninori Morimoto 	}
3032a7930ed4SKuninori Morimoto 	if (ret == 0) {
3033a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3034a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3035a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3036a7930ed4SKuninori Morimoto 				break;
3037a7930ed4SKuninori Morimoto 			}
3038a7930ed4SKuninori Morimoto 		}
3039a7930ed4SKuninori Morimoto 	}
3040a7930ed4SKuninori Morimoto 
3041a7930ed4SKuninori Morimoto 	/*
30428c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3043a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3044a7930ed4SKuninori Morimoto 	 */
30458c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
304651930295SJulia Lawall 	if (of_property_read_bool(np, prop))
30478c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
30488c2d6a9fSKuninori Morimoto 	else
30498c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3050a7930ed4SKuninori Morimoto 
3051a7930ed4SKuninori Morimoto 	/*
3052a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3053a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3054a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3055a7930ed4SKuninori Morimoto 	 */
3056a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3057a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3058a7930ed4SKuninori Morimoto 
3059a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3060a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3061a7930ed4SKuninori Morimoto 
3062a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3063a7930ed4SKuninori Morimoto 	case 0x11:
3064a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3065a7930ed4SKuninori Morimoto 		break;
3066a7930ed4SKuninori Morimoto 	case 0x10:
3067a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3068a7930ed4SKuninori Morimoto 		break;
3069a7930ed4SKuninori Morimoto 	case 0x01:
3070a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3071a7930ed4SKuninori Morimoto 		break;
3072a7930ed4SKuninori Morimoto 	default:
3073a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3074a7930ed4SKuninori Morimoto 		break;
3075a7930ed4SKuninori Morimoto 	}
3076a7930ed4SKuninori Morimoto 
3077a7930ed4SKuninori Morimoto 	/*
3078a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3079a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3080a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3081a7930ed4SKuninori Morimoto 	 */
3082a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3083a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3084389cb834SJyri Sarha 	if (bit && bitclkmaster)
3085389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3086a7930ed4SKuninori Morimoto 
3087a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3088a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3089389cb834SJyri Sarha 	if (frame && framemaster)
3090389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3091a7930ed4SKuninori Morimoto 
3092a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3093a7930ed4SKuninori Morimoto 	case 0x11:
3094a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3095a7930ed4SKuninori Morimoto 		break;
3096a7930ed4SKuninori Morimoto 	case 0x10:
3097a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3098a7930ed4SKuninori Morimoto 		break;
3099a7930ed4SKuninori Morimoto 	case 0x01:
3100a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3101a7930ed4SKuninori Morimoto 		break;
3102a7930ed4SKuninori Morimoto 	default:
3103a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3104a7930ed4SKuninori Morimoto 		break;
3105a7930ed4SKuninori Morimoto 	}
3106a7930ed4SKuninori Morimoto 
3107a7930ed4SKuninori Morimoto 	return format;
3108a7930ed4SKuninori Morimoto }
3109a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3110a7930ed4SKuninori Morimoto 
3111a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
3112a180e8b9SKuninori Morimoto {
311309d4cc03SKuninori Morimoto 	struct snd_soc_component *component;
3114c1e230f0SKuninori Morimoto 	struct snd_soc_dai_link_component dlc;
3115a180e8b9SKuninori Morimoto 	int ret;
3116a180e8b9SKuninori Morimoto 
3117c1e230f0SKuninori Morimoto 	dlc.of_node	= of_graph_get_port_parent(ep);
3118c1e230f0SKuninori Morimoto 	dlc.name	= NULL;
3119a180e8b9SKuninori Morimoto 	/*
3120a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
3121a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
3122a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
3123a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
3124a180e8b9SKuninori Morimoto 	 */
3125a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
3126a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
3127c1e230f0SKuninori Morimoto 	component = soc_find_component(&dlc);
31282c7b1704SKuninori Morimoto 	if (component)
31292c7b1704SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_id(component, ep);
3130a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
3131a180e8b9SKuninori Morimoto 
3132c1e230f0SKuninori Morimoto 	of_node_put(dlc.of_node);
3133c0a480d1STony Lindgren 
3134a180e8b9SKuninori Morimoto 	return ret;
3135a180e8b9SKuninori Morimoto }
3136a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3137a180e8b9SKuninori Morimoto 
31381ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args,
3139cb470087SKuninori Morimoto 				const char **dai_name)
3140cb470087SKuninori Morimoto {
3141cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
31423e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
314393b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3144cb470087SKuninori Morimoto 
3145cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3146368dee94SKuninori Morimoto 	for_each_component(pos) {
3147c0834440SKuninori Morimoto 		component_of_node = soc_component_to_node(pos);
31483e0aa8d8SJyri Sarha 
31493e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
3150cb470087SKuninori Morimoto 			continue;
3151cb470087SKuninori Morimoto 
3152a2a34175SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3153a2a34175SKuninori Morimoto 		if (ret == -ENOTSUPP) {
315458bf4179SKuninori Morimoto 			struct snd_soc_dai *dai;
31556833c452SKuninori Morimoto 			int id = -1;
31566833c452SKuninori Morimoto 
315793b0f3eeSJean-Francois Moine 			switch (args->args_count) {
31586833c452SKuninori Morimoto 			case 0:
31596833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
31606833c452SKuninori Morimoto 				break;
31616833c452SKuninori Morimoto 			case 1:
316293b0f3eeSJean-Francois Moine 				id = args->args[0];
31636833c452SKuninori Morimoto 				break;
31646833c452SKuninori Morimoto 			default:
31656833c452SKuninori Morimoto 				/* not supported */
3166cb470087SKuninori Morimoto 				break;
3167cb470087SKuninori Morimoto 			}
3168cb470087SKuninori Morimoto 
31696833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
31706833c452SKuninori Morimoto 				ret = -EINVAL;
31713dcba280SNicolin Chen 				continue;
31726833c452SKuninori Morimoto 			}
3173e41975edSXiubo Li 
3174e41975edSXiubo Li 			ret = 0;
3175e41975edSXiubo Li 
317658bf4179SKuninori Morimoto 			/* find target DAI */
317715a0c645SKuninori Morimoto 			for_each_component_dais(pos, dai) {
317858bf4179SKuninori Morimoto 				if (id == 0)
317958bf4179SKuninori Morimoto 					break;
318058bf4179SKuninori Morimoto 				id--;
318158bf4179SKuninori Morimoto 			}
318258bf4179SKuninori Morimoto 
318358bf4179SKuninori Morimoto 			*dai_name = dai->driver->name;
3184e41975edSXiubo Li 			if (!*dai_name)
3185e41975edSXiubo Li 				*dai_name = pos->name;
31866833c452SKuninori Morimoto 		}
31876833c452SKuninori Morimoto 
3188cb470087SKuninori Morimoto 		break;
3189cb470087SKuninori Morimoto 	}
3190cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
319193b0f3eeSJean-Francois Moine 	return ret;
319293b0f3eeSJean-Francois Moine }
31931ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
319493b0f3eeSJean-Francois Moine 
319593b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
319693b0f3eeSJean-Francois Moine 			    const char **dai_name)
319793b0f3eeSJean-Francois Moine {
319893b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
319993b0f3eeSJean-Francois Moine 	int ret;
320093b0f3eeSJean-Francois Moine 
320193b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
320293b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
320393b0f3eeSJean-Francois Moine 	if (ret)
320493b0f3eeSJean-Francois Moine 		return ret;
320593b0f3eeSJean-Francois Moine 
320693b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3207cb470087SKuninori Morimoto 
3208cb470087SKuninori Morimoto 	of_node_put(args.np);
3209cb470087SKuninori Morimoto 
3210cb470087SKuninori Morimoto 	return ret;
3211cb470087SKuninori Morimoto }
3212cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3213cb470087SKuninori Morimoto 
321493b0f3eeSJean-Francois Moine /*
321594685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
321694685763SSylwester Nawrocki  * @dai_link: DAI link
321794685763SSylwester Nawrocki  *
321894685763SSylwester Nawrocki  * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
321994685763SSylwester Nawrocki  */
322094685763SSylwester Nawrocki void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
322194685763SSylwester Nawrocki {
32223db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *component;
322394685763SSylwester Nawrocki 	int index;
322494685763SSylwester Nawrocki 
32253db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
322694685763SSylwester Nawrocki 		if (!component->of_node)
322794685763SSylwester Nawrocki 			break;
322894685763SSylwester Nawrocki 		of_node_put(component->of_node);
322994685763SSylwester Nawrocki 		component->of_node = NULL;
323094685763SSylwester Nawrocki 	}
323194685763SSylwester Nawrocki }
323294685763SSylwester Nawrocki EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
323394685763SSylwester Nawrocki 
323494685763SSylwester Nawrocki /*
323593b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
323693b0f3eeSJean-Francois Moine  * @dev: Card device
323793b0f3eeSJean-Francois Moine  * @of_node: Device node
323893b0f3eeSJean-Francois Moine  * @dai_link: DAI link
323993b0f3eeSJean-Francois Moine  *
324093b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
324193b0f3eeSJean-Francois Moine  * 'sound-dai'.
324293b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
324394685763SSylwester Nawrocki  * The device nodes in the array (of_node) must be dereferenced by calling
324494685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs() on @dai_link.
324593b0f3eeSJean-Francois Moine  *
324693b0f3eeSJean-Francois Moine  * Returns 0 for success
324793b0f3eeSJean-Francois Moine  */
324893b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
324993b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
325093b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
325193b0f3eeSJean-Francois Moine {
325293b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
325393b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
325493b0f3eeSJean-Francois Moine 	char *name;
325593b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
325693b0f3eeSJean-Francois Moine 
325793b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
325893b0f3eeSJean-Francois Moine 	name = "sound-dai";
325993b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
326093b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
326193b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
326293b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
326393b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
326493b0f3eeSJean-Francois Moine 		else
326593b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
326693b0f3eeSJean-Francois Moine 		return num_codecs;
326793b0f3eeSJean-Francois Moine 	}
3268a86854d0SKees Cook 	component = devm_kcalloc(dev,
3269a86854d0SKees Cook 				 num_codecs, sizeof(*component),
327093b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
327193b0f3eeSJean-Francois Moine 	if (!component)
327293b0f3eeSJean-Francois Moine 		return -ENOMEM;
327393b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
327493b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
327593b0f3eeSJean-Francois Moine 
327693b0f3eeSJean-Francois Moine 	/* Parse the list */
32773db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
327893b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
327993b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
328093b0f3eeSJean-Francois Moine 						 index, &args);
328193b0f3eeSJean-Francois Moine 		if (ret)
328293b0f3eeSJean-Francois Moine 			goto err;
328393b0f3eeSJean-Francois Moine 		component->of_node = args.np;
328493b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
328593b0f3eeSJean-Francois Moine 		if (ret < 0)
328693b0f3eeSJean-Francois Moine 			goto err;
328793b0f3eeSJean-Francois Moine 	}
328893b0f3eeSJean-Francois Moine 	return 0;
328993b0f3eeSJean-Francois Moine err:
329094685763SSylwester Nawrocki 	snd_soc_of_put_dai_link_codecs(dai_link);
329193b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
329293b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
329393b0f3eeSJean-Francois Moine 	return ret;
329493b0f3eeSJean-Francois Moine }
329593b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
329693b0f3eeSJean-Francois Moine 
3297c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3298db2a4165SFrank Mandarino {
32996553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
3300fb257897SMark Brown 	snd_soc_util_init();
3301fb257897SMark Brown 
3302db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3303db2a4165SFrank Mandarino }
33044abe8e16SMark Brown module_init(snd_soc_init);
3305db2a4165SFrank Mandarino 
33067d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3307db2a4165SFrank Mandarino {
3308fb257897SMark Brown 	snd_soc_util_exit();
33096553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3310fb257897SMark Brown 
3311db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3312db2a4165SFrank Mandarino }
3313db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3314db2a4165SFrank Mandarino 
3315db2a4165SFrank Mandarino /* Module information */
3316d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3317db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3318db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
33198b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3320