xref: /openbmc/linux/sound/soc/soc-core.c (revision df95a16d2a9626dcfc3f2b3671c9b91fa076c997)
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 
359b8132657SKuninori Morimoto struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
360b8132657SKuninori Morimoto 						   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 	mutex_lock(&client_mutex);
367b8132657SKuninori Morimoto 	for_each_component(component) {
3685bd7e08bSKuninori Morimoto 		if ((dev == component->dev) &&
3695bd7e08bSKuninori Morimoto 		    (!driver_name ||
3705bd7e08bSKuninori Morimoto 		     (driver_name == component->driver->name) ||
3715bd7e08bSKuninori Morimoto 		     (strcmp(component->driver->name, driver_name) == 0))) {
3725bd7e08bSKuninori Morimoto 			found_component = component;
373b8132657SKuninori Morimoto 			break;
374b8132657SKuninori Morimoto 		}
3755bd7e08bSKuninori Morimoto 	}
376b8132657SKuninori Morimoto 	mutex_unlock(&client_mutex);
377b8132657SKuninori Morimoto 
3785bd7e08bSKuninori Morimoto 	return found_component;
379b8132657SKuninori Morimoto }
380b8132657SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
381b8132657SKuninori Morimoto 
38247c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
38347c88fffSLiam Girdwood 		const char *dai_link, int stream)
38447c88fffSLiam Girdwood {
3851a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
38647c88fffSLiam Girdwood 
387bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
3881a497983SMengdong Lin 		if (rtd->dai_link->no_pcm &&
3891a497983SMengdong Lin 			!strcmp(rtd->dai_link->name, dai_link))
3901a497983SMengdong Lin 			return rtd->pcm->streams[stream].substream;
39147c88fffSLiam Girdwood 	}
392f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
39347c88fffSLiam Girdwood 	return NULL;
39447c88fffSLiam Girdwood }
39547c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
39647c88fffSLiam Girdwood 
39775ab9eb6SKuninori Morimoto static const struct snd_soc_ops null_snd_soc_ops;
39875ab9eb6SKuninori Morimoto 
3996e864344SKuninori Morimoto static void soc_release_rtd_dev(struct device *dev)
4006e864344SKuninori Morimoto {
4016e864344SKuninori Morimoto 	/* "dev" means "rtd->dev" */
4026e864344SKuninori Morimoto 	kfree(dev);
4036e864344SKuninori Morimoto }
4046e864344SKuninori Morimoto 
4051c93a9e0SKuninori Morimoto static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
4061c93a9e0SKuninori Morimoto {
4076e864344SKuninori Morimoto 	if (!rtd)
4086e864344SKuninori Morimoto 		return;
4096e864344SKuninori Morimoto 
410753ace0aSKuninori Morimoto 	list_del(&rtd->list);
411b7c5bc45SKuninori Morimoto 
412b7c5bc45SKuninori Morimoto 	/*
413b7c5bc45SKuninori Morimoto 	 * we don't need to call kfree() for rtd->dev
414b7c5bc45SKuninori Morimoto 	 * see
415b7c5bc45SKuninori Morimoto 	 *	soc_release_rtd_dev()
416d918a376SKuninori Morimoto 	 *
417d918a376SKuninori Morimoto 	 * We don't need rtd->dev NULL check, because
418d918a376SKuninori Morimoto 	 * it is alloced *before* rtd.
419d918a376SKuninori Morimoto 	 * see
420d918a376SKuninori Morimoto 	 *	soc_new_pcm_runtime()
421b7c5bc45SKuninori Morimoto 	 */
422b7c5bc45SKuninori Morimoto 	device_unregister(rtd->dev);
4231c93a9e0SKuninori Morimoto }
4241c93a9e0SKuninori Morimoto 
4251a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
4261a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
4271a497983SMengdong Lin {
4281a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
429d918a376SKuninori Morimoto 	struct device *dev;
4306e864344SKuninori Morimoto 	int ret;
4311a497983SMengdong Lin 
4326e864344SKuninori Morimoto 	/*
433d918a376SKuninori Morimoto 	 * for rtd->dev
434d918a376SKuninori Morimoto 	 */
435d918a376SKuninori Morimoto 	dev = kzalloc(sizeof(struct device), GFP_KERNEL);
436d918a376SKuninori Morimoto 	if (!dev)
437d918a376SKuninori Morimoto 		return NULL;
438d918a376SKuninori Morimoto 
439d918a376SKuninori Morimoto 	dev->parent	= card->dev;
440d918a376SKuninori Morimoto 	dev->release	= soc_release_rtd_dev;
441d918a376SKuninori Morimoto 	dev->groups	= soc_dev_attr_groups;
442d918a376SKuninori Morimoto 
443d918a376SKuninori Morimoto 	dev_set_name(dev, "%s", dai_link->name);
444d918a376SKuninori Morimoto 
445d918a376SKuninori Morimoto 	ret = device_register(dev);
446d918a376SKuninori Morimoto 	if (ret < 0) {
447d918a376SKuninori Morimoto 		put_device(dev); /* soc_release_rtd_dev */
448d918a376SKuninori Morimoto 		return NULL;
449d918a376SKuninori Morimoto 	}
450d918a376SKuninori Morimoto 
451d918a376SKuninori Morimoto 	/*
4526e864344SKuninori Morimoto 	 * for rtd
4536e864344SKuninori Morimoto 	 */
4544dc0e7dfSKuninori Morimoto 	rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL);
4551a497983SMengdong Lin 	if (!rtd)
4566e864344SKuninori Morimoto 		goto free_rtd;
4571a497983SMengdong Lin 
458d918a376SKuninori Morimoto 	rtd->dev = dev;
459d918a376SKuninori Morimoto 	dev_set_drvdata(dev, rtd);
460d918a376SKuninori Morimoto 
4616e864344SKuninori Morimoto 	/*
4626e864344SKuninori Morimoto 	 * for rtd->codec_dais
4636e864344SKuninori Morimoto 	 */
4644dc0e7dfSKuninori Morimoto 	rtd->codec_dais = devm_kcalloc(dev, dai_link->num_codecs,
4656396bb22SKees Cook 					sizeof(struct snd_soc_dai *),
4661a497983SMengdong Lin 					GFP_KERNEL);
4676e864344SKuninori Morimoto 	if (!rtd->codec_dais)
4686e864344SKuninori Morimoto 		goto free_rtd;
4696e864344SKuninori Morimoto 
4706e864344SKuninori Morimoto 	/*
4716e864344SKuninori Morimoto 	 * rtd remaining settings
4726e864344SKuninori Morimoto 	 */
473929deb84SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
4746e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
4756e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
4766e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
4776e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
4786e864344SKuninori Morimoto 
479929deb84SKuninori Morimoto 	rtd->card = card;
480929deb84SKuninori Morimoto 	rtd->dai_link = dai_link;
481929deb84SKuninori Morimoto 	if (!rtd->dai_link->ops)
482929deb84SKuninori Morimoto 		rtd->dai_link->ops = &null_snd_soc_ops;
483929deb84SKuninori Morimoto 
4846634e3d6SKuninori Morimoto 	/* see for_each_card_rtds */
4851a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
4861a497983SMengdong Lin 	rtd->num = card->num_rtd;
4871a497983SMengdong Lin 	card->num_rtd++;
488a848125eSKuninori Morimoto 
489a848125eSKuninori Morimoto 	return rtd;
4906e864344SKuninori Morimoto 
4916e864344SKuninori Morimoto free_rtd:
4926e864344SKuninori Morimoto 	soc_free_pcm_runtime(rtd);
4936e864344SKuninori Morimoto 	return NULL;
4941a497983SMengdong Lin }
4951a497983SMengdong Lin 
49647c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
49747c88fffSLiam Girdwood 		const char *dai_link)
49847c88fffSLiam Girdwood {
4991a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
50047c88fffSLiam Girdwood 
501bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5021a497983SMengdong Lin 		if (!strcmp(rtd->dai_link->name, dai_link))
5031a497983SMengdong Lin 			return rtd;
50447c88fffSLiam Girdwood 	}
505f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
50647c88fffSLiam Girdwood 	return NULL;
50747c88fffSLiam Girdwood }
50847c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
50947c88fffSLiam Girdwood 
51065462e44SKuninori Morimoto static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
51165462e44SKuninori Morimoto {
51265462e44SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
51365462e44SKuninori Morimoto 
51465462e44SKuninori Morimoto 	for_each_card_rtds(card, rtd)
51565462e44SKuninori Morimoto 		flush_delayed_work(&rtd->delayed_work);
51665462e44SKuninori Morimoto }
51765462e44SKuninori Morimoto 
5186f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
519db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5206f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
521db2a4165SFrank Mandarino {
5226f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
523d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
5241a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
5251a497983SMengdong Lin 	int i;
526db2a4165SFrank Mandarino 
527c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
528c5599b87SLars-Peter Clausen 	if (!card->instantiated)
529e3509ff0SDaniel Mack 		return 0;
530e3509ff0SDaniel Mack 
5312c7b696aSMarcel Ziswiler 	/*
5322c7b696aSMarcel Ziswiler 	 * Due to the resume being scheduled into a workqueue we could
5336ed25978SAndy Green 	 * suspend before that's finished - wait for it to complete.
5346ed25978SAndy Green 	 */
535f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
5366ed25978SAndy Green 
5376ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
538f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5396ed25978SAndy Green 
540a00f90f9SMark Brown 	/* mute any active DACs */
541bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5420b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
5433efab7dcSMark Brown 
5441a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5453efab7dcSMark Brown 			continue;
5463efab7dcSMark Brown 
5470b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
54888fdffa2SKuninori Morimoto 			if (dai->playback_active)
54988fdffa2SKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 1,
55088fdffa2SKuninori Morimoto 						SNDRV_PCM_STREAM_PLAYBACK);
551db2a4165SFrank Mandarino 		}
55288bd870fSBenoit Cousson 	}
553db2a4165SFrank Mandarino 
5544ccab3e7SLiam Girdwood 	/* suspend all pcms */
555bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5561a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5573efab7dcSMark Brown 			continue;
5583efab7dcSMark Brown 
5591a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
5603efab7dcSMark Brown 	}
5614ccab3e7SLiam Girdwood 
56287506549SMark Brown 	if (card->suspend_pre)
56370b2ac12SMark Brown 		card->suspend_pre(card);
564db2a4165SFrank Mandarino 
565bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5661a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
5673efab7dcSMark Brown 
5681a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5693efab7dcSMark Brown 			continue;
5703efab7dcSMark Brown 
571e0f22622SKuninori Morimoto 		if (!cpu_dai->driver->bus_control)
572e0f22622SKuninori Morimoto 			snd_soc_dai_suspend(cpu_dai);
573db2a4165SFrank Mandarino 	}
574db2a4165SFrank Mandarino 
57537660b6dSLars-Peter Clausen 	/* close any waiting streams */
57665462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
577db2a4165SFrank Mandarino 
578bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5793efab7dcSMark Brown 
5801a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5813efab7dcSMark Brown 			continue;
5823efab7dcSMark Brown 
5831a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
5847bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
585db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
586f0fba2adSLiam Girdwood 
5871a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
5887bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
589db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
590db2a4165SFrank Mandarino 	}
591db2a4165SFrank Mandarino 
5928be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
5938be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
594e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
595e2d32ff6SMark Brown 
5969178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
597f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
5982c7b696aSMarcel Ziswiler 		struct snd_soc_dapm_context *dapm =
5992c7b696aSMarcel Ziswiler 				snd_soc_component_get_dapm(component);
600d9fc4063SKuninori Morimoto 
6012c7b696aSMarcel Ziswiler 		/*
6022c7b696aSMarcel Ziswiler 		 * If there are paths active then the COMPONENT will be held
6032c7b696aSMarcel Ziswiler 		 * with bias _ON and should not be suspended.
6042c7b696aSMarcel Ziswiler 		 */
605e40fadbcSKuninori Morimoto 		if (!snd_soc_component_is_suspended(component)) {
6064890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
6071547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
608125a25daSMark Brown 				/*
6099178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
610125a25daSMark Brown 				 * bias off then being in STANDBY
611125a25daSMark Brown 				 * means it's doing something,
612125a25daSMark Brown 				 * otherwise fall through.
613125a25daSMark Brown 				 */
6144890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
6159178feb4SKuninori Morimoto 					dev_dbg(component->dev,
61610e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
617125a25daSMark Brown 					break;
618125a25daSMark Brown 				}
6191a12d5dcSGustavo A. R. Silva 				/* fall through */
620a8093297SLars-Peter Clausen 
6211547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
62266c51573SKuninori Morimoto 				snd_soc_component_suspend(component);
6239178feb4SKuninori Morimoto 				if (component->regmap)
6249178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
625988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
6269178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
6271547aba9SMark Brown 				break;
6281547aba9SMark Brown 			default:
6299178feb4SKuninori Morimoto 				dev_dbg(component->dev,
6309178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
6311547aba9SMark Brown 				break;
6321547aba9SMark Brown 			}
6331547aba9SMark Brown 		}
634f0fba2adSLiam Girdwood 	}
635db2a4165SFrank Mandarino 
636bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6371a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6383efab7dcSMark Brown 
6391a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6403efab7dcSMark Brown 			continue;
6413efab7dcSMark Brown 
642e0f22622SKuninori Morimoto 		if (cpu_dai->driver->bus_control)
643e0f22622SKuninori Morimoto 			snd_soc_dai_suspend(cpu_dai);
644988e8cc4SNicolin Chen 
645988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
646988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
647db2a4165SFrank Mandarino 	}
648db2a4165SFrank Mandarino 
64987506549SMark Brown 	if (card->suspend_post)
65070b2ac12SMark Brown 		card->suspend_post(card);
651db2a4165SFrank Mandarino 
652db2a4165SFrank Mandarino 	return 0;
653db2a4165SFrank Mandarino }
6546f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
655db2a4165SFrank Mandarino 
6562c7b696aSMarcel Ziswiler /*
6572c7b696aSMarcel Ziswiler  * deferred resume work, so resume can complete before we finished
6586ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6596ed25978SAndy Green  */
6606ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
661db2a4165SFrank Mandarino {
662f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
6632c7b696aSMarcel Ziswiler 			container_of(work, struct snd_soc_card,
6642c7b696aSMarcel Ziswiler 				     deferred_resume_work);
6651a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
666d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
6671a497983SMengdong Lin 	int i;
668db2a4165SFrank Mandarino 
6692c7b696aSMarcel Ziswiler 	/*
6702c7b696aSMarcel Ziswiler 	 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6716ed25978SAndy Green 	 * so userspace apps are blocked from touching us
6726ed25978SAndy Green 	 */
6736ed25978SAndy Green 
674f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
6756ed25978SAndy Green 
6769949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
677f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
6789949788bSMark Brown 
67987506549SMark Brown 	if (card->resume_pre)
68070b2ac12SMark Brown 		card->resume_pre(card);
681db2a4165SFrank Mandarino 
682bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
683bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6841a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6853efab7dcSMark Brown 
6861a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6873efab7dcSMark Brown 			continue;
6883efab7dcSMark Brown 
68924b09d05SKuninori Morimoto 		if (cpu_dai->driver->bus_control)
69024b09d05SKuninori Morimoto 			snd_soc_dai_resume(cpu_dai);
691db2a4165SFrank Mandarino 	}
692db2a4165SFrank Mandarino 
693f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
694e40fadbcSKuninori Morimoto 		if (snd_soc_component_is_suspended(component))
6959a840cbaSKuninori Morimoto 			snd_soc_component_resume(component);
6961547aba9SMark Brown 	}
697db2a4165SFrank Mandarino 
698bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6993efab7dcSMark Brown 
7001a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7013efab7dcSMark Brown 			continue;
7023efab7dcSMark Brown 
7031a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
704d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
705db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
706f0fba2adSLiam Girdwood 
7071a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
708d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
709db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
710db2a4165SFrank Mandarino 	}
711db2a4165SFrank Mandarino 
7123ff3f64bSMark Brown 	/* unmute any active DACs */
713bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7140b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
7153efab7dcSMark Brown 
7161a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7173efab7dcSMark Brown 			continue;
7183efab7dcSMark Brown 
7190b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
72088fdffa2SKuninori Morimoto 			if (dai->playback_active)
72188fdffa2SKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 0,
72288fdffa2SKuninori Morimoto 						SNDRV_PCM_STREAM_PLAYBACK);
723db2a4165SFrank Mandarino 		}
72488bd870fSBenoit Cousson 	}
725db2a4165SFrank Mandarino 
726bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7271a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7283efab7dcSMark Brown 
7291a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7303efab7dcSMark Brown 			continue;
7313efab7dcSMark Brown 
73224b09d05SKuninori Morimoto 		if (!cpu_dai->driver->bus_control)
73324b09d05SKuninori Morimoto 			snd_soc_dai_resume(cpu_dai);
734db2a4165SFrank Mandarino 	}
735db2a4165SFrank Mandarino 
73687506549SMark Brown 	if (card->resume_post)
73770b2ac12SMark Brown 		card->resume_post(card);
738db2a4165SFrank Mandarino 
739f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
7406ed25978SAndy Green 
7418be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
7428be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
743e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
7441a7aaa58SJeeja KP 
7451a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
7461a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
7476ed25978SAndy Green }
7486ed25978SAndy Green 
7496ed25978SAndy Green /* powers up audio subsystem after a suspend */
7506f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
7516ed25978SAndy Green {
7526f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
753bc263214SLars-Peter Clausen 	bool bus_control = false;
7541a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
75522d251a5SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
75622d251a5SKuninori Morimoto 	int i;
757b9dd94a8SPeter Ujfalusi 
758c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
759c5599b87SLars-Peter Clausen 	if (!card->instantiated)
7605ff1ddf2SEric Miao 		return 0;
7615ff1ddf2SEric Miao 
762988e8cc4SNicolin Chen 	/* activate pins from sleep state */
763bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
76488bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
76588bd870fSBenoit Cousson 
766988e8cc4SNicolin Chen 		if (cpu_dai->active)
767988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
76888bd870fSBenoit Cousson 
76922d251a5SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
770988e8cc4SNicolin Chen 			if (codec_dai->active)
771988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
772988e8cc4SNicolin Chen 		}
77388bd870fSBenoit Cousson 	}
774988e8cc4SNicolin Chen 
775bc263214SLars-Peter Clausen 	/*
776bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
777bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
778bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
77964ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
78064ab9baaSMark Brown 	 */
781bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7821a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7832c7b696aSMarcel Ziswiler 
784bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
78582e14e8bSStephen Warren 	}
786bc263214SLars-Peter Clausen 	if (bus_control) {
787bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
78864ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
78964ab9baaSMark Brown 	} else {
790f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
7916308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
792f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
793f0fba2adSLiam Girdwood 	}
7946ed25978SAndy Green 
795db2a4165SFrank Mandarino 	return 0;
796db2a4165SFrank Mandarino }
7976f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
798b3da4251SKuninori Morimoto 
799b3da4251SKuninori Morimoto static void soc_resume_init(struct snd_soc_card *card)
800b3da4251SKuninori Morimoto {
801b3da4251SKuninori Morimoto 	/* deferred resume work */
802b3da4251SKuninori Morimoto 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
803b3da4251SKuninori Morimoto }
804db2a4165SFrank Mandarino #else
8056f8ab4acSMark Brown #define snd_soc_suspend NULL
8066f8ab4acSMark Brown #define snd_soc_resume NULL
807b3da4251SKuninori Morimoto static inline void soc_resume_init(struct snd_soc_card *card)
808b3da4251SKuninori Morimoto {
809b3da4251SKuninori Morimoto }
810db2a4165SFrank Mandarino #endif
811db2a4165SFrank Mandarino 
81285e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
81302a06d30SBarry Song };
81402a06d30SBarry Song 
815c0834440SKuninori Morimoto static struct device_node
816c0834440SKuninori Morimoto *soc_component_to_node(struct snd_soc_component *component)
81712023a9aSMisael Lopez Cruz {
818c0834440SKuninori Morimoto 	struct device_node *of_node;
81912023a9aSMisael Lopez Cruz 
820c0834440SKuninori Morimoto 	of_node = component->dev->of_node;
821c0834440SKuninori Morimoto 	if (!of_node && component->dev->parent)
822c0834440SKuninori Morimoto 		of_node = component->dev->parent->of_node;
82334e81ab4SLars-Peter Clausen 
824c0834440SKuninori Morimoto 	return of_node;
82512023a9aSMisael Lopez Cruz }
82612023a9aSMisael Lopez Cruz 
827be6ac0a9SKuninori Morimoto static int snd_soc_is_matching_component(
828be6ac0a9SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc,
829be6ac0a9SKuninori Morimoto 	struct snd_soc_component *component)
830be6ac0a9SKuninori Morimoto {
831be6ac0a9SKuninori Morimoto 	struct device_node *component_of_node;
832be6ac0a9SKuninori Morimoto 
8337d7db5d3SKuninori Morimoto 	if (!dlc)
8347d7db5d3SKuninori Morimoto 		return 0;
8357d7db5d3SKuninori Morimoto 
8367d7db5d3SKuninori Morimoto 	component_of_node = soc_component_to_node(component);
837be6ac0a9SKuninori Morimoto 
838be6ac0a9SKuninori Morimoto 	if (dlc->of_node && component_of_node != dlc->of_node)
839be6ac0a9SKuninori Morimoto 		return 0;
840be6ac0a9SKuninori Morimoto 	if (dlc->name && strcmp(component->name, dlc->name))
841be6ac0a9SKuninori Morimoto 		return 0;
842be6ac0a9SKuninori Morimoto 
843be6ac0a9SKuninori Morimoto 	return 1;
844be6ac0a9SKuninori Morimoto }
845be6ac0a9SKuninori Morimoto 
846db2a4165SFrank Mandarino static struct snd_soc_component *soc_find_component(
847c1e230f0SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc)
848db2a4165SFrank Mandarino {
849db2a4165SFrank Mandarino 	struct snd_soc_component *component;
850db2a4165SFrank Mandarino 
851db2a4165SFrank Mandarino 	lockdep_assert_held(&client_mutex);
852db2a4165SFrank Mandarino 
853e3303268SKuninori Morimoto 	/*
854e3303268SKuninori Morimoto 	 * NOTE
855e3303268SKuninori Morimoto 	 *
856e3303268SKuninori Morimoto 	 * It returns *1st* found component, but some driver
857e3303268SKuninori Morimoto 	 * has few components by same of_node/name
858e3303268SKuninori Morimoto 	 * ex)
859e3303268SKuninori Morimoto 	 *	CPU component and generic DMAEngine component
860e3303268SKuninori Morimoto 	 */
861c1e230f0SKuninori Morimoto 	for_each_component(component)
862c1e230f0SKuninori Morimoto 		if (snd_soc_is_matching_component(dlc, component))
863db2a4165SFrank Mandarino 			return component;
864db2a4165SFrank Mandarino 
865db2a4165SFrank Mandarino 	return NULL;
86612023a9aSMisael Lopez Cruz }
86712023a9aSMisael Lopez Cruz 
868fbb88b5cSMengdong Lin /**
869fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
870fbb88b5cSMengdong Lin  *
8714958471bSJeffy Chen  * @dlc: name of the DAI or the DAI driver and optional component info to match
872fbb88b5cSMengdong Lin  *
873ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
874fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
875fbb88b5cSMengdong Lin  * should also match if being specified.
876fbb88b5cSMengdong Lin  *
877fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
878fbb88b5cSMengdong Lin  */
879305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
88014621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
88112023a9aSMisael Lopez Cruz {
88214621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
88314621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
88412023a9aSMisael Lopez Cruz 
88534e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
88634e81ab4SLars-Peter Clausen 
88714621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs */
888368dee94SKuninori Morimoto 	for_each_component(component) {
889be6ac0a9SKuninori Morimoto 		if (!snd_soc_is_matching_component(dlc, component))
89012023a9aSMisael Lopez Cruz 			continue;
89115a0c645SKuninori Morimoto 		for_each_component_dais(component, dai) {
8924958471bSJeffy Chen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
8936a6dafdaSJeffy Chen 			    && (!dai->driver->name
8946a6dafdaSJeffy Chen 				|| strcmp(dai->driver->name, dlc->dai_name)))
89514621c7eSLars-Peter Clausen 				continue;
89612023a9aSMisael Lopez Cruz 
89714621c7eSLars-Peter Clausen 			return dai;
89812023a9aSMisael Lopez Cruz 		}
89912023a9aSMisael Lopez Cruz 	}
90012023a9aSMisael Lopez Cruz 
90112023a9aSMisael Lopez Cruz 	return NULL;
90212023a9aSMisael Lopez Cruz }
903305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
90412023a9aSMisael Lopez Cruz 
90517fb1755SMengdong Lin /**
90617fb1755SMengdong Lin  * snd_soc_find_dai_link - Find a DAI link
90717fb1755SMengdong Lin  *
90817fb1755SMengdong Lin  * @card: soc card
90917fb1755SMengdong Lin  * @id: DAI link ID to match
91017fb1755SMengdong Lin  * @name: DAI link name to match, optional
9118abab35fSCharles Keepax  * @stream_name: DAI link stream name to match, optional
91217fb1755SMengdong Lin  *
91317fb1755SMengdong Lin  * This function will search all existing DAI links of the soc card to
91417fb1755SMengdong Lin  * find the link of the same ID. Since DAI links may not have their
91517fb1755SMengdong Lin  * unique ID, so name and stream name should also match if being
91617fb1755SMengdong Lin  * specified.
91717fb1755SMengdong Lin  *
91817fb1755SMengdong Lin  * Return: pointer of DAI link, or NULL if not found.
91917fb1755SMengdong Lin  */
92017fb1755SMengdong Lin struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
92117fb1755SMengdong Lin 					       int id, const char *name,
92217fb1755SMengdong Lin 					       const char *stream_name)
92317fb1755SMengdong Lin {
92442849064SKuninori Morimoto 	struct snd_soc_dai_link *link;
92517fb1755SMengdong Lin 
92617fb1755SMengdong Lin 	lockdep_assert_held(&client_mutex);
92717fb1755SMengdong Lin 
92842849064SKuninori Morimoto 	for_each_card_links(card, link) {
92917fb1755SMengdong Lin 		if (link->id != id)
93017fb1755SMengdong Lin 			continue;
93117fb1755SMengdong Lin 
93217fb1755SMengdong Lin 		if (name && (!link->name || strcmp(name, link->name)))
93317fb1755SMengdong Lin 			continue;
93417fb1755SMengdong Lin 
93517fb1755SMengdong Lin 		if (stream_name && (!link->stream_name
93617fb1755SMengdong Lin 			|| strcmp(stream_name, link->stream_name)))
93717fb1755SMengdong Lin 			continue;
93817fb1755SMengdong Lin 
93917fb1755SMengdong Lin 		return link;
94017fb1755SMengdong Lin 	}
94117fb1755SMengdong Lin 
94217fb1755SMengdong Lin 	return NULL;
94317fb1755SMengdong Lin }
94417fb1755SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
94517fb1755SMengdong Lin 
94649a5ba1cSMengdong Lin static bool soc_is_dai_link_bound(struct snd_soc_card *card,
94749a5ba1cSMengdong Lin 		struct snd_soc_dai_link *dai_link)
948db2a4165SFrank Mandarino {
94949a5ba1cSMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
95049a5ba1cSMengdong Lin 
951bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
95249a5ba1cSMengdong Lin 		if (rtd->dai_link == dai_link)
95349a5ba1cSMengdong Lin 			return true;
95449a5ba1cSMengdong Lin 	}
95549a5ba1cSMengdong Lin 
95649a5ba1cSMengdong Lin 	return false;
95749a5ba1cSMengdong Lin }
95849a5ba1cSMengdong Lin 
959bfce78a5SKuninori Morimoto static int soc_dai_link_sanity_check(struct snd_soc_card *card,
96036794902SKuninori Morimoto 				     struct snd_soc_dai_link *link)
96136794902SKuninori Morimoto {
96236794902SKuninori Morimoto 	int i;
96336794902SKuninori Morimoto 	struct snd_soc_dai_link_component *codec, *platform;
96436794902SKuninori Morimoto 
96536794902SKuninori Morimoto 	for_each_link_codecs(link, i, codec) {
96636794902SKuninori Morimoto 		/*
96736794902SKuninori Morimoto 		 * Codec must be specified by 1 of name or OF node,
96836794902SKuninori Morimoto 		 * not both or neither.
96936794902SKuninori Morimoto 		 */
97036794902SKuninori Morimoto 		if (!!codec->name == !!codec->of_node) {
97136794902SKuninori Morimoto 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
97236794902SKuninori Morimoto 				link->name);
97336794902SKuninori Morimoto 			return -EINVAL;
97436794902SKuninori Morimoto 		}
97536794902SKuninori Morimoto 
97636794902SKuninori Morimoto 		/* Codec DAI name must be specified */
97736794902SKuninori Morimoto 		if (!codec->dai_name) {
97836794902SKuninori Morimoto 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
97936794902SKuninori Morimoto 				link->name);
98036794902SKuninori Morimoto 			return -EINVAL;
98136794902SKuninori Morimoto 		}
98236794902SKuninori Morimoto 
98336794902SKuninori Morimoto 		/*
98436794902SKuninori Morimoto 		 * Defer card registration if codec component is not added to
98536794902SKuninori Morimoto 		 * component list.
98636794902SKuninori Morimoto 		 */
98736794902SKuninori Morimoto 		if (!soc_find_component(codec))
98836794902SKuninori Morimoto 			return -EPROBE_DEFER;
98936794902SKuninori Morimoto 	}
99036794902SKuninori Morimoto 
99136794902SKuninori Morimoto 	for_each_link_platforms(link, i, platform) {
99236794902SKuninori Morimoto 		/*
99336794902SKuninori Morimoto 		 * Platform may be specified by either name or OF node, but it
99436794902SKuninori Morimoto 		 * can be left unspecified, then no components will be inserted
99536794902SKuninori Morimoto 		 * in the rtdcom list
99636794902SKuninori Morimoto 		 */
99736794902SKuninori Morimoto 		if (!!platform->name == !!platform->of_node) {
99836794902SKuninori Morimoto 			dev_err(card->dev,
99936794902SKuninori Morimoto 				"ASoC: Neither/both platform name/of_node are set for %s\n",
100036794902SKuninori Morimoto 				link->name);
100136794902SKuninori Morimoto 			return -EINVAL;
100236794902SKuninori Morimoto 		}
100336794902SKuninori Morimoto 
100436794902SKuninori Morimoto 		/*
100536794902SKuninori Morimoto 		 * Defer card registration if platform component is not added to
100636794902SKuninori Morimoto 		 * component list.
100736794902SKuninori Morimoto 		 */
100836794902SKuninori Morimoto 		if (!soc_find_component(platform))
100936794902SKuninori Morimoto 			return -EPROBE_DEFER;
101036794902SKuninori Morimoto 	}
101136794902SKuninori Morimoto 
101236794902SKuninori Morimoto 	/* FIXME */
101336794902SKuninori Morimoto 	if (link->num_cpus > 1) {
101436794902SKuninori Morimoto 		dev_err(card->dev,
101536794902SKuninori Morimoto 			"ASoC: multi cpu is not yet supported %s\n",
101636794902SKuninori Morimoto 			link->name);
101736794902SKuninori Morimoto 		return -EINVAL;
101836794902SKuninori Morimoto 	}
101936794902SKuninori Morimoto 
102036794902SKuninori Morimoto 	/*
102136794902SKuninori Morimoto 	 * CPU device may be specified by either name or OF node, but
102236794902SKuninori Morimoto 	 * can be left unspecified, and will be matched based on DAI
102336794902SKuninori Morimoto 	 * name alone..
102436794902SKuninori Morimoto 	 */
102536794902SKuninori Morimoto 	if (link->cpus->name && link->cpus->of_node) {
102636794902SKuninori Morimoto 		dev_err(card->dev,
102736794902SKuninori Morimoto 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
102836794902SKuninori Morimoto 			link->name);
102936794902SKuninori Morimoto 		return -EINVAL;
103036794902SKuninori Morimoto 	}
103136794902SKuninori Morimoto 
103236794902SKuninori Morimoto 	/*
1033cd3c5ad7SKuninori Morimoto 	 * Defer card registration if cpu dai component is not added to
103436794902SKuninori Morimoto 	 * component list.
103536794902SKuninori Morimoto 	 */
103636794902SKuninori Morimoto 	if ((link->cpus->of_node || link->cpus->name) &&
103736794902SKuninori Morimoto 	    !soc_find_component(link->cpus))
103836794902SKuninori Morimoto 		return -EPROBE_DEFER;
103936794902SKuninori Morimoto 
104036794902SKuninori Morimoto 	/*
104136794902SKuninori Morimoto 	 * At least one of CPU DAI name or CPU device name/node must be
104236794902SKuninori Morimoto 	 * specified
104336794902SKuninori Morimoto 	 */
104436794902SKuninori Morimoto 	if (!link->cpus->dai_name &&
104536794902SKuninori Morimoto 	    !(link->cpus->name || link->cpus->of_node)) {
104636794902SKuninori Morimoto 		dev_err(card->dev,
104736794902SKuninori Morimoto 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
104836794902SKuninori Morimoto 			link->name);
104936794902SKuninori Morimoto 		return -EINVAL;
105036794902SKuninori Morimoto 	}
105136794902SKuninori Morimoto 
105236794902SKuninori Morimoto 	return 0;
105336794902SKuninori Morimoto }
105436794902SKuninori Morimoto 
1055bc7a9091SKuninori Morimoto static void soc_unbind_dai_link(struct snd_soc_card *card,
1056bc7a9091SKuninori Morimoto 				struct snd_soc_dai_link *dai_link)
1057bc7a9091SKuninori Morimoto {
1058bc7a9091SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
1059bc7a9091SKuninori Morimoto 
1060bc7a9091SKuninori Morimoto 	rtd = snd_soc_get_pcm_runtime(card, dai_link->name);
1061bc7a9091SKuninori Morimoto 	if (rtd)
1062bc7a9091SKuninori Morimoto 		soc_free_pcm_runtime(rtd);
1063bc7a9091SKuninori Morimoto }
1064bc7a9091SKuninori Morimoto 
10656f2f1ff0SMengdong Lin static int soc_bind_dai_link(struct snd_soc_card *card,
10666f2f1ff0SMengdong Lin 	struct snd_soc_dai_link *dai_link)
1067db2a4165SFrank Mandarino {
10681a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
106934614739SJerome Brunet 	struct snd_soc_dai_link_component *codec, *platform;
107090be711eSKuninori Morimoto 	struct snd_soc_component *component;
1071bfce78a5SKuninori Morimoto 	int i, ret;
1072db2a4165SFrank Mandarino 
1073a655de80SLiam Girdwood 	if (dai_link->ignore)
1074a655de80SLiam Girdwood 		return 0;
1075a655de80SLiam Girdwood 
10766f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
10776308419aSMark Brown 
107849a5ba1cSMengdong Lin 	if (soc_is_dai_link_bound(card, dai_link)) {
107949a5ba1cSMengdong Lin 		dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
108049a5ba1cSMengdong Lin 			dai_link->name);
108149a5ba1cSMengdong Lin 		return 0;
108249a5ba1cSMengdong Lin 	}
1083db2a4165SFrank Mandarino 
1084bfce78a5SKuninori Morimoto 	ret = soc_dai_link_sanity_check(card, dai_link);
1085bfce78a5SKuninori Morimoto 	if (ret < 0)
1086bfce78a5SKuninori Morimoto 		return ret;
1087bfce78a5SKuninori Morimoto 
1088513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
1089513cb311SSudip Mukherjee 	if (!rtd)
1090513cb311SSudip Mukherjee 		return -ENOMEM;
1091513cb311SSudip Mukherjee 
109208a5841eSKuninori Morimoto 	/* FIXME: we need multi CPU support in the future */
109308a5841eSKuninori Morimoto 	rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
1094b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
10956b490879SMartin Hundebøll 		dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
109608a5841eSKuninori Morimoto 			 dai_link->cpus->dai_name);
10971a497983SMengdong Lin 		goto _err_defer;
1098c5af3a2eSMark Brown 	}
109990be711eSKuninori Morimoto 	snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
1100c5af3a2eSMark Brown 
110188bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
1102e2b30edfSKuninori Morimoto 	rtd->num_codecs = dai_link->num_codecs;
110334614739SJerome Brunet 	for_each_link_codecs(dai_link, i, codec) {
110434614739SJerome Brunet 		rtd->codec_dais[i] = snd_soc_find_dai(codec);
11050a2cfcd9SKuninori Morimoto 		if (!rtd->codec_dais[i]) {
11067c7e2d6aSStefan Agner 			dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
110734614739SJerome Brunet 				 codec->dai_name);
11081a497983SMengdong Lin 			goto _err_defer;
110912023a9aSMisael Lopez Cruz 		}
111034614739SJerome Brunet 
11110a2cfcd9SKuninori Morimoto 		snd_soc_rtdcom_add(rtd, rtd->codec_dais[i]->component);
111288bd870fSBenoit Cousson 	}
111388bd870fSBenoit Cousson 
111488bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
11150a2cfcd9SKuninori Morimoto 	rtd->codec_dai = rtd->codec_dais[0];
111612023a9aSMisael Lopez Cruz 
1117e2b30edfSKuninori Morimoto 	/* Find PLATFORM from registered PLATFORMs */
111834614739SJerome Brunet 	for_each_link_platforms(dai_link, i, platform) {
1119368dee94SKuninori Morimoto 		for_each_component(component) {
112034614739SJerome Brunet 			if (!snd_soc_is_matching_component(platform, component))
112190be711eSKuninori Morimoto 				continue;
112290be711eSKuninori Morimoto 
112390be711eSKuninori Morimoto 			snd_soc_rtdcom_add(rtd, component);
112490be711eSKuninori Morimoto 		}
112534614739SJerome Brunet 	}
112690be711eSKuninori Morimoto 
1127b19e6e7bSMark Brown 	return 0;
11281a497983SMengdong Lin 
11291a497983SMengdong Lin _err_defer:
11301a497983SMengdong Lin 	soc_free_pcm_runtime(rtd);
11311a497983SMengdong Lin 	return -EPROBE_DEFER;
11326b05eda6SMark Brown }
11336b05eda6SMark Brown 
1134ffd60fbaSKuninori Morimoto static void soc_set_of_name_prefix(struct snd_soc_component *component)
1135ffd60fbaSKuninori Morimoto {
1136ffd60fbaSKuninori Morimoto 	struct device_node *of_node = soc_component_to_node(component);
1137ffd60fbaSKuninori Morimoto 	const char *str;
1138ffd60fbaSKuninori Morimoto 	int ret;
1139ffd60fbaSKuninori Morimoto 
1140ffd60fbaSKuninori Morimoto 	ret = of_property_read_string(of_node, "sound-name-prefix", &str);
1141ffd60fbaSKuninori Morimoto 	if (!ret)
1142ffd60fbaSKuninori Morimoto 		component->name_prefix = str;
1143ffd60fbaSKuninori Morimoto }
1144ffd60fbaSKuninori Morimoto 
1145ffd60fbaSKuninori Morimoto static void soc_set_name_prefix(struct snd_soc_card *card,
1146ffd60fbaSKuninori Morimoto 				struct snd_soc_component *component)
1147ffd60fbaSKuninori Morimoto {
1148ffd60fbaSKuninori Morimoto 	int i;
1149ffd60fbaSKuninori Morimoto 
1150ffd60fbaSKuninori Morimoto 	for (i = 0; i < card->num_configs && card->codec_conf; i++) {
1151ffd60fbaSKuninori Morimoto 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1152ffd60fbaSKuninori Morimoto 		struct device_node *of_node = soc_component_to_node(component);
1153ffd60fbaSKuninori Morimoto 
1154ffd60fbaSKuninori Morimoto 		if (map->of_node && of_node != map->of_node)
1155ffd60fbaSKuninori Morimoto 			continue;
1156ffd60fbaSKuninori Morimoto 		if (map->dev_name && strcmp(component->name, map->dev_name))
1157ffd60fbaSKuninori Morimoto 			continue;
1158ffd60fbaSKuninori Morimoto 		component->name_prefix = map->name_prefix;
1159ffd60fbaSKuninori Morimoto 		return;
1160ffd60fbaSKuninori Morimoto 	}
1161ffd60fbaSKuninori Morimoto 
1162ffd60fbaSKuninori Morimoto 	/*
1163ffd60fbaSKuninori Morimoto 	 * If there is no configuration table or no match in the table,
1164ffd60fbaSKuninori Morimoto 	 * check if a prefix is provided in the node
1165ffd60fbaSKuninori Morimoto 	 */
1166ffd60fbaSKuninori Morimoto 	soc_set_of_name_prefix(component);
1167ffd60fbaSKuninori Morimoto }
1168ffd60fbaSKuninori Morimoto 
116922d14231SKuninori Morimoto static void soc_cleanup_component(struct snd_soc_component *component)
117022d14231SKuninori Morimoto {
117104f770d9SKuninori Morimoto 	/* For framework level robustness */
11723bb936f5SAmadeusz Sławiński 	snd_soc_component_set_jack(component, NULL, NULL);
117304f770d9SKuninori Morimoto 
11747a5d9815SBard liao 	list_del_init(&component->card_list);
117522d14231SKuninori Morimoto 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
117622d14231SKuninori Morimoto 	soc_cleanup_component_debugfs(component);
117722d14231SKuninori Morimoto 	component->card = NULL;
11780e36f36bSPierre-Louis Bossart 	snd_soc_component_module_put_when_remove(component);
117922d14231SKuninori Morimoto }
118022d14231SKuninori Morimoto 
1181f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
1182d12cd198SStephen Warren {
1183abd31b32SLars-Peter Clausen 	if (!component->card)
118470090bbbSLars-Peter Clausen 		return;
1185d12cd198SStephen Warren 
118603b34dd7SKuninori Morimoto 	snd_soc_component_remove(component);
11877a5d9815SBard liao 
118822d14231SKuninori Morimoto 	soc_cleanup_component(component);
1189d12cd198SStephen Warren }
1190d12cd198SStephen Warren 
1191ffd60fbaSKuninori Morimoto static int soc_probe_component(struct snd_soc_card *card,
1192ffd60fbaSKuninori Morimoto 			       struct snd_soc_component *component)
1193ffd60fbaSKuninori Morimoto {
1194ffd60fbaSKuninori Morimoto 	struct snd_soc_dapm_context *dapm =
1195ffd60fbaSKuninori Morimoto 		snd_soc_component_get_dapm(component);
1196ffd60fbaSKuninori Morimoto 	struct snd_soc_dai *dai;
1197ffd60fbaSKuninori Morimoto 	int ret;
1198ffd60fbaSKuninori Morimoto 
1199ffd60fbaSKuninori Morimoto 	if (!strcmp(component->name, "snd-soc-dummy"))
1200ffd60fbaSKuninori Morimoto 		return 0;
1201ffd60fbaSKuninori Morimoto 
1202ffd60fbaSKuninori Morimoto 	if (component->card) {
1203ffd60fbaSKuninori Morimoto 		if (component->card != card) {
1204ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1205ffd60fbaSKuninori Morimoto 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1206ffd60fbaSKuninori Morimoto 				card->name, component->card->name);
1207ffd60fbaSKuninori Morimoto 			return -ENODEV;
1208ffd60fbaSKuninori Morimoto 		}
1209ffd60fbaSKuninori Morimoto 		return 0;
1210ffd60fbaSKuninori Morimoto 	}
1211ffd60fbaSKuninori Morimoto 
1212ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_module_get_when_probe(component);
1213ffd60fbaSKuninori Morimoto 	if (ret < 0)
1214ffd60fbaSKuninori Morimoto 		return ret;
1215ffd60fbaSKuninori Morimoto 
1216ffd60fbaSKuninori Morimoto 	component->card = card;
1217ffd60fbaSKuninori Morimoto 	soc_set_name_prefix(card, component);
1218ffd60fbaSKuninori Morimoto 
1219ffd60fbaSKuninori Morimoto 	soc_init_component_debugfs(component);
1220ffd60fbaSKuninori Morimoto 
122195c267ddSKuninori Morimoto 	snd_soc_dapm_init(dapm, card, component);
1222b614beafSKuninori Morimoto 
1223ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_new_controls(dapm,
1224ffd60fbaSKuninori Morimoto 					component->driver->dapm_widgets,
1225ffd60fbaSKuninori Morimoto 					component->driver->num_dapm_widgets);
1226ffd60fbaSKuninori Morimoto 
1227ffd60fbaSKuninori Morimoto 	if (ret != 0) {
1228ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1229ffd60fbaSKuninori Morimoto 			"Failed to create new controls %d\n", ret);
1230ffd60fbaSKuninori Morimoto 		goto err_probe;
1231ffd60fbaSKuninori Morimoto 	}
1232ffd60fbaSKuninori Morimoto 
1233ffd60fbaSKuninori Morimoto 	for_each_component_dais(component, dai) {
1234ffd60fbaSKuninori Morimoto 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1235ffd60fbaSKuninori Morimoto 		if (ret != 0) {
1236ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1237ffd60fbaSKuninori Morimoto 				"Failed to create DAI widgets %d\n", ret);
1238ffd60fbaSKuninori Morimoto 			goto err_probe;
1239ffd60fbaSKuninori Morimoto 		}
1240ffd60fbaSKuninori Morimoto 	}
1241ffd60fbaSKuninori Morimoto 
1242ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_probe(component);
1243ffd60fbaSKuninori Morimoto 	if (ret < 0) {
1244ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1245ffd60fbaSKuninori Morimoto 			"ASoC: failed to probe component %d\n", ret);
1246ffd60fbaSKuninori Morimoto 		goto err_probe;
1247ffd60fbaSKuninori Morimoto 	}
1248ffd60fbaSKuninori Morimoto 	WARN(dapm->idle_bias_off &&
1249ffd60fbaSKuninori Morimoto 	     dapm->bias_level != SND_SOC_BIAS_OFF,
1250ffd60fbaSKuninori Morimoto 	     "codec %s can not start from non-off bias with idle_bias_off==1\n",
1251ffd60fbaSKuninori Morimoto 	     component->name);
1252ffd60fbaSKuninori Morimoto 
1253ffd60fbaSKuninori Morimoto 	/* machine specific init */
1254ffd60fbaSKuninori Morimoto 	if (component->init) {
1255ffd60fbaSKuninori Morimoto 		ret = component->init(component);
1256ffd60fbaSKuninori Morimoto 		if (ret < 0) {
1257ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1258ffd60fbaSKuninori Morimoto 				"Failed to do machine specific init %d\n", ret);
1259ffd60fbaSKuninori Morimoto 			goto err_probe;
1260ffd60fbaSKuninori Morimoto 		}
1261ffd60fbaSKuninori Morimoto 	}
1262ffd60fbaSKuninori Morimoto 
1263ffd60fbaSKuninori Morimoto 	ret = snd_soc_add_component_controls(component,
1264ffd60fbaSKuninori Morimoto 					     component->driver->controls,
1265ffd60fbaSKuninori Morimoto 					     component->driver->num_controls);
1266ffd60fbaSKuninori Morimoto 	if (ret < 0)
1267ffd60fbaSKuninori Morimoto 		goto err_probe;
1268ffd60fbaSKuninori Morimoto 
1269ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(dapm,
1270ffd60fbaSKuninori Morimoto 				      component->driver->dapm_routes,
1271ffd60fbaSKuninori Morimoto 				      component->driver->num_dapm_routes);
1272ffd60fbaSKuninori Morimoto 	if (ret < 0)
1273ffd60fbaSKuninori Morimoto 		goto err_probe;
1274ffd60fbaSKuninori Morimoto 
1275ffd60fbaSKuninori Morimoto 	/* see for_each_card_components */
1276ffd60fbaSKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1277ffd60fbaSKuninori Morimoto 
1278ffd60fbaSKuninori Morimoto err_probe:
1279ffd60fbaSKuninori Morimoto 	if (ret < 0)
1280ffd60fbaSKuninori Morimoto 		soc_cleanup_component(component);
1281ffd60fbaSKuninori Morimoto 
1282ffd60fbaSKuninori Morimoto 	return ret;
1283ffd60fbaSKuninori Morimoto }
1284ffd60fbaSKuninori Morimoto 
1285e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1286589c3563SJarkko Nikula {
1287589c3563SJarkko Nikula 	int err;
1288589c3563SJarkko Nikula 
128952abe6ccSGuennadi Liakhovetski 	if (!dai || !dai->probed || !dai->driver ||
12902eda3cb1SKuninori Morimoto 	    dai->driver->remove_order != order)
12912eda3cb1SKuninori Morimoto 		return;
12922eda3cb1SKuninori Morimoto 
1293dcdab582SKuninori Morimoto 	err = snd_soc_dai_remove(dai);
1294589c3563SJarkko Nikula 	if (err < 0)
1295e60cd14fSLars-Peter Clausen 		dev_err(dai->dev,
1296b0aa88afSMisael Lopez Cruz 			"ASoC: failed to remove %s: %d\n",
1297e60cd14fSLars-Peter Clausen 			dai->name, err);
1298dcdab582SKuninori Morimoto 
1299e60cd14fSLars-Peter Clausen 	dai->probed = 0;
1300b0aa88afSMisael Lopez Cruz }
1301b0aa88afSMisael Lopez Cruz 
1302a7d44f78SKuninori Morimoto static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1303a7d44f78SKuninori Morimoto {
1304a7d44f78SKuninori Morimoto 	int ret;
1305a7d44f78SKuninori Morimoto 
1306a7d44f78SKuninori Morimoto 	if (dai->probed ||
1307a7d44f78SKuninori Morimoto 	    dai->driver->probe_order != order)
1308a7d44f78SKuninori Morimoto 		return 0;
1309a7d44f78SKuninori Morimoto 
1310a7d44f78SKuninori Morimoto 	ret = snd_soc_dai_probe(dai);
1311a7d44f78SKuninori Morimoto 	if (ret < 0) {
1312a7d44f78SKuninori Morimoto 		dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1313a7d44f78SKuninori Morimoto 			dai->name, ret);
1314a7d44f78SKuninori Morimoto 		return ret;
1315a7d44f78SKuninori Morimoto 	}
1316a7d44f78SKuninori Morimoto 
1317a7d44f78SKuninori Morimoto 	dai->probed = 1;
1318a7d44f78SKuninori Morimoto 
1319a7d44f78SKuninori Morimoto 	return 0;
1320a7d44f78SKuninori Morimoto }
1321a7d44f78SKuninori Morimoto 
13224ca47d21SKuninori Morimoto static void soc_remove_link_dais(struct snd_soc_card *card)
1323f0fba2adSLiam Girdwood {
1324e60cd14fSLars-Peter Clausen 	int i;
13250b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
13264ca47d21SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
13274ca47d21SKuninori Morimoto 	int order;
13284ca47d21SKuninori Morimoto 
13294ca47d21SKuninori Morimoto 	for_each_comp_order(order) {
13304ca47d21SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1331f0fba2adSLiam Girdwood 			/* remove the CODEC DAI */
13320b7990e3SKuninori Morimoto 			for_each_rtd_codec_dai(rtd, i, codec_dai)
13330b7990e3SKuninori Morimoto 				soc_remove_dai(codec_dai, order);
1334f0fba2adSLiam Girdwood 
1335e60cd14fSLars-Peter Clausen 			soc_remove_dai(rtd->cpu_dai, order);
1336f0fba2adSLiam Girdwood 		}
13374ca47d21SKuninori Morimoto 	}
13384ca47d21SKuninori Morimoto }
1339f0fba2adSLiam Girdwood 
1340bc7c16c2SKuninori Morimoto static int soc_probe_link_dais(struct snd_soc_card *card)
1341bc7c16c2SKuninori Morimoto {
1342bc7c16c2SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1343bc7c16c2SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
1344bc7c16c2SKuninori Morimoto 	int i, order, ret;
1345bc7c16c2SKuninori Morimoto 
1346bc7c16c2SKuninori Morimoto 	for_each_comp_order(order) {
1347bc7c16c2SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1348bc7c16c2SKuninori Morimoto 
1349bc7c16c2SKuninori Morimoto 			dev_dbg(card->dev,
1350bc7c16c2SKuninori Morimoto 				"ASoC: probe %s dai link %d late %d\n",
1351bc7c16c2SKuninori Morimoto 				card->name, rtd->num, order);
1352bc7c16c2SKuninori Morimoto 
1353bc7c16c2SKuninori Morimoto 			ret = soc_probe_dai(rtd->cpu_dai, order);
1354bc7c16c2SKuninori Morimoto 			if (ret)
1355bc7c16c2SKuninori Morimoto 				return ret;
1356bc7c16c2SKuninori Morimoto 
1357bc7c16c2SKuninori Morimoto 			/* probe the CODEC DAI */
1358bc7c16c2SKuninori Morimoto 			for_each_rtd_codec_dai(rtd, i, codec_dai) {
1359bc7c16c2SKuninori Morimoto 				ret = soc_probe_dai(codec_dai, order);
1360bc7c16c2SKuninori Morimoto 				if (ret)
1361bc7c16c2SKuninori Morimoto 					return ret;
1362bc7c16c2SKuninori Morimoto 			}
1363bc7c16c2SKuninori Morimoto 		}
1364bc7c16c2SKuninori Morimoto 	}
1365bc7c16c2SKuninori Morimoto 
1366bc7c16c2SKuninori Morimoto 	return 0;
1367bc7c16c2SKuninori Morimoto }
1368bc7c16c2SKuninori Morimoto 
1369b006c0c6SKuninori Morimoto static void soc_remove_link_components(struct snd_soc_card *card)
137062ae68faSStephen Warren {
137161aca564SLars-Peter Clausen 	struct snd_soc_component *component;
1372b006c0c6SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
137390be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
1374b006c0c6SKuninori Morimoto 	int order;
137562ae68faSStephen Warren 
1376b006c0c6SKuninori Morimoto 	for_each_comp_order(order) {
1377b006c0c6SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
13782b544dd7SKuninori Morimoto 			for_each_rtd_components(rtd, rtdcom, component) {
1379b006c0c6SKuninori Morimoto 				if (component->driver->remove_order != order)
1380b006c0c6SKuninori Morimoto 					continue;
1381b006c0c6SKuninori Morimoto 
138261aca564SLars-Peter Clausen 				soc_remove_component(component);
138362ae68faSStephen Warren 			}
138462ae68faSStephen Warren 		}
1385b006c0c6SKuninori Morimoto 	}
1386b006c0c6SKuninori Morimoto }
138762ae68faSStephen Warren 
138862f07a6bSKuninori Morimoto static int soc_probe_link_components(struct snd_soc_card *card)
13896fb03550SKuninori Morimoto {
13906fb03550SKuninori Morimoto 	struct snd_soc_component *component;
139162f07a6bSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
13926fb03550SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
139362f07a6bSKuninori Morimoto 	int ret, order;
13946fb03550SKuninori Morimoto 
139562f07a6bSKuninori Morimoto 	for_each_comp_order(order) {
139662f07a6bSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
13972b544dd7SKuninori Morimoto 			for_each_rtd_components(rtd, rtdcom, component) {
139862f07a6bSKuninori Morimoto 				if (component->driver->probe_order != order)
139962f07a6bSKuninori Morimoto 					continue;
140062f07a6bSKuninori Morimoto 
14016fb03550SKuninori Morimoto 				ret = soc_probe_component(card, component);
14026fb03550SKuninori Morimoto 				if (ret < 0)
14036fb03550SKuninori Morimoto 					return ret;
14046fb03550SKuninori Morimoto 			}
14056fb03550SKuninori Morimoto 		}
140662f07a6bSKuninori Morimoto 	}
14076fb03550SKuninori Morimoto 
14086fb03550SKuninori Morimoto 	return 0;
14096fb03550SKuninori Morimoto }
14106fb03550SKuninori Morimoto 
1411ef2e8175SKuninori Morimoto void snd_soc_disconnect_sync(struct device *dev)
1412ef2e8175SKuninori Morimoto {
14132c7b696aSMarcel Ziswiler 	struct snd_soc_component *component =
14142c7b696aSMarcel Ziswiler 			snd_soc_lookup_component(dev, NULL);
1415ef2e8175SKuninori Morimoto 
1416ef2e8175SKuninori Morimoto 	if (!component || !component->card)
1417ef2e8175SKuninori Morimoto 		return;
1418ef2e8175SKuninori Morimoto 
1419ef2e8175SKuninori Morimoto 	snd_card_disconnect_sync(component->card->snd_card);
1420ef2e8175SKuninori Morimoto }
1421df532185SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
1422ef2e8175SKuninori Morimoto 
1423f8f80361SMengdong Lin /**
1424f8f80361SMengdong Lin  * snd_soc_add_dai_link - Add a DAI link dynamically
1425f8f80361SMengdong Lin  * @card: The ASoC card to which the DAI link is added
1426f8f80361SMengdong Lin  * @dai_link: The new DAI link to add
1427f8f80361SMengdong Lin  *
1428f8f80361SMengdong Lin  * This function adds a DAI link to the ASoC card's link list.
1429f8f80361SMengdong Lin  *
1430f8f80361SMengdong Lin  * Note: Topology can use this API to add DAI links when probing the
1431f8f80361SMengdong Lin  * topology component. And machine drivers can still define static
1432f8f80361SMengdong Lin  * DAI links in dai_link array.
1433f8f80361SMengdong Lin  */
1434f8f80361SMengdong Lin int snd_soc_add_dai_link(struct snd_soc_card *card,
1435f8f80361SMengdong Lin 		struct snd_soc_dai_link *dai_link)
1436f8f80361SMengdong Lin {
14376b1dff02SKuninori Morimoto 	int ret;
14386b1dff02SKuninori Morimoto 
1439f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
1440237d1908SKuninori Morimoto 
14412c7b696aSMarcel Ziswiler 	/*
14422c7b696aSMarcel Ziswiler 	 * Notify the machine driver for extra initialization
1443d6f220eaSMengdong Lin 	 */
1444237d1908SKuninori Morimoto 	if (card->add_dai_link)
1445d6f220eaSMengdong Lin 		card->add_dai_link(card, dai_link);
1446d6f220eaSMengdong Lin 
14476b1dff02SKuninori Morimoto 	ret = soc_bind_dai_link(card, dai_link);
14486b1dff02SKuninori Morimoto 	if (ret < 0)
14496b1dff02SKuninori Morimoto 		return ret;
14506b1dff02SKuninori Morimoto 
14516634e3d6SKuninori Morimoto 	/* see for_each_card_links */
1452f8f80361SMengdong Lin 	list_add_tail(&dai_link->list, &card->dai_link_list);
1453f8f80361SMengdong Lin 
1454f8f80361SMengdong Lin 	return 0;
1455f8f80361SMengdong Lin }
1456f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1457f8f80361SMengdong Lin 
1458f8f80361SMengdong Lin /**
1459f8f80361SMengdong Lin  * snd_soc_remove_dai_link - Remove a DAI link from the list
1460f8f80361SMengdong Lin  * @card: The ASoC card that owns the link
1461f8f80361SMengdong Lin  * @dai_link: The DAI link to remove
1462f8f80361SMengdong Lin  *
1463f8f80361SMengdong Lin  * This function removes a DAI link from the ASoC card's link list.
1464f8f80361SMengdong Lin  *
1465f8f80361SMengdong Lin  * For DAI links previously added by topology, topology should
1466f8f80361SMengdong Lin  * remove them by using the dobj embedded in the link.
1467f8f80361SMengdong Lin  */
1468f8f80361SMengdong Lin void snd_soc_remove_dai_link(struct snd_soc_card *card,
1469f8f80361SMengdong Lin 			     struct snd_soc_dai_link *dai_link)
1470f8f80361SMengdong Lin {
1471f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
1472237d1908SKuninori Morimoto 
14732c7b696aSMarcel Ziswiler 	/*
14742c7b696aSMarcel Ziswiler 	 * Notify the machine driver for extra destruction
1475d6f220eaSMengdong Lin 	 */
1476237d1908SKuninori Morimoto 	if (card->remove_dai_link)
1477d6f220eaSMengdong Lin 		card->remove_dai_link(card, dai_link);
1478d6f220eaSMengdong Lin 
1479c26a8841SKuninori Morimoto 	list_del(&dai_link->list);
1480bc7a9091SKuninori Morimoto 
1481bc7a9091SKuninori Morimoto 	soc_unbind_dai_link(card, dai_link);
1482f8f80361SMengdong Lin }
1483f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
14840671fd8eSKuninori Morimoto 
148525f7b701SArnaud Pouliquen static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
148625f7b701SArnaud Pouliquen 				struct snd_soc_pcm_runtime *rtd)
148725f7b701SArnaud Pouliquen {
148825f7b701SArnaud Pouliquen 	int i, ret = 0;
148925f7b701SArnaud Pouliquen 
149025f7b701SArnaud Pouliquen 	for (i = 0; i < num_dais; ++i) {
149125f7b701SArnaud Pouliquen 		struct snd_soc_dai_driver *drv = dais[i]->driver;
149225f7b701SArnaud Pouliquen 
1493de17f14eSRohit kumar 		if (drv->pcm_new)
149425f7b701SArnaud Pouliquen 			ret = drv->pcm_new(rtd, dais[i]);
149525f7b701SArnaud Pouliquen 		if (ret < 0) {
149625f7b701SArnaud Pouliquen 			dev_err(dais[i]->dev,
149725f7b701SArnaud Pouliquen 				"ASoC: Failed to bind %s with pcm device\n",
149825f7b701SArnaud Pouliquen 				dais[i]->name);
149925f7b701SArnaud Pouliquen 			return ret;
150025f7b701SArnaud Pouliquen 		}
150125f7b701SArnaud Pouliquen 	}
150225f7b701SArnaud Pouliquen 
150325f7b701SArnaud Pouliquen 	return 0;
150425f7b701SArnaud Pouliquen }
150525f7b701SArnaud Pouliquen 
1506c4b46982SKuninori Morimoto static int soc_link_init(struct snd_soc_card *card,
1507c4b46982SKuninori Morimoto 			 struct snd_soc_pcm_runtime *rtd)
1508c4b46982SKuninori Morimoto {
1509c4b46982SKuninori Morimoto 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1510c4b46982SKuninori Morimoto 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1511c4b46982SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
1512c4b46982SKuninori Morimoto 	struct snd_soc_component *component;
1513c4b46982SKuninori Morimoto 	int ret, num;
1514c4b46982SKuninori Morimoto 
1515c4b46982SKuninori Morimoto 	/* set default power off timeout */
1516c4b46982SKuninori Morimoto 	rtd->pmdown_time = pmdown_time;
15170168bf0dSLiam Girdwood 
15185f3484acSLars-Peter Clausen 	/* do machine specific initialization */
15195f3484acSLars-Peter Clausen 	if (dai_link->init) {
15205f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
15215f3484acSLars-Peter Clausen 		if (ret < 0) {
15225f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
15235f3484acSLars-Peter Clausen 				dai_link->name, ret);
15245f3484acSLars-Peter Clausen 			return ret;
15255f3484acSLars-Peter Clausen 		}
15265f3484acSLars-Peter Clausen 	}
15275f3484acSLars-Peter Clausen 
152840aa5383SRicard Wanderlof 	if (dai_link->dai_fmt) {
152940aa5383SRicard Wanderlof 		ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
153040aa5383SRicard Wanderlof 		if (ret)
153140aa5383SRicard Wanderlof 			return ret;
153240aa5383SRicard Wanderlof 	}
1533a5053a8eSKuninori Morimoto 
15345f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
15352e55b90aSLars-Peter Clausen 	soc_dpcm_debugfs_add(rtd);
15365f3484acSLars-Peter Clausen 
1537a655de80SLiam Girdwood 	num = rtd->num;
1538a655de80SLiam Girdwood 
1539a655de80SLiam Girdwood 	/*
1540a655de80SLiam Girdwood 	 * most drivers will register their PCMs using DAI link ordering but
1541a655de80SLiam Girdwood 	 * topology based drivers can use the DAI link id field to set PCM
1542a655de80SLiam Girdwood 	 * device number and then use rtd + a base offset of the BEs.
1543a655de80SLiam Girdwood 	 */
15442b544dd7SKuninori Morimoto 	for_each_rtd_components(rtd, rtdcom, component) {
1545a655de80SLiam Girdwood 		if (!component->driver->use_dai_pcm_id)
1546a655de80SLiam Girdwood 			continue;
1547a655de80SLiam Girdwood 
1548a655de80SLiam Girdwood 		if (rtd->dai_link->no_pcm)
1549a655de80SLiam Girdwood 			num += component->driver->be_pcm_base;
1550a655de80SLiam Girdwood 		else
1551a655de80SLiam Girdwood 			num = rtd->dai_link->id;
1552a655de80SLiam Girdwood 	}
1553a655de80SLiam Girdwood 
1554b423c420SKuninori Morimoto 	/* create compress_device if possible */
1555b423c420SKuninori Morimoto 	ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1556b423c420SKuninori Morimoto 	if (ret != -ENOTSUPP) {
1557b423c420SKuninori Morimoto 		if (ret < 0)
1558f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
15591245b700SNamarta Kohli 					 dai_link->stream_name);
15601245b700SNamarta Kohli 		return ret;
15611245b700SNamarta Kohli 	}
1562b423c420SKuninori Morimoto 
1563f0fba2adSLiam Girdwood 	/* create the pcm */
1564a655de80SLiam Girdwood 	ret = soc_new_pcm(rtd, num);
1565f0fba2adSLiam Girdwood 	if (ret < 0) {
1566f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
15670837fc62SFabio Estevam 			dai_link->stream_name, ret);
1568f0fba2adSLiam Girdwood 		return ret;
1569f0fba2adSLiam Girdwood 	}
157025f7b701SArnaud Pouliquen 	ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
157125f7b701SArnaud Pouliquen 	if (ret < 0)
157225f7b701SArnaud Pouliquen 		return ret;
157325f7b701SArnaud Pouliquen 	ret = soc_link_dai_pcm_new(rtd->codec_dais,
157425f7b701SArnaud Pouliquen 				   rtd->num_codecs, rtd);
157525f7b701SArnaud Pouliquen 	return ret;
1576f0fba2adSLiam Girdwood }
1577f0fba2adSLiam Girdwood 
1578e8fbd250SKuninori Morimoto static void soc_unbind_aux_dev(struct snd_soc_card *card)
15794893a2ebSKuninori Morimoto {
1580e8fbd250SKuninori Morimoto 	struct snd_soc_component *component, *_component;
1581e8fbd250SKuninori Morimoto 
1582e8fbd250SKuninori Morimoto 	for_each_card_auxs_safe(card, component, _component) {
15834893a2ebSKuninori Morimoto 		component->init = NULL;
15844893a2ebSKuninori Morimoto 		list_del(&component->card_aux_list);
15854893a2ebSKuninori Morimoto 	}
1586e8fbd250SKuninori Morimoto }
15874893a2ebSKuninori Morimoto 
1588bee886f1SKuninori Morimoto static int soc_bind_aux_dev(struct snd_soc_card *card)
1589b19e6e7bSMark Brown {
1590f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1591bee886f1SKuninori Morimoto 	struct snd_soc_aux_dev *aux;
1592bee886f1SKuninori Morimoto 	int i;
15933ca041edSSebastian Reichel 
1594bee886f1SKuninori Morimoto 	for_each_card_pre_auxs(card, i, aux) {
1595f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1596bee886f1SKuninori Morimoto 		component = soc_find_component(&aux->dlc);
1597f2ed6b07SMengdong Lin 		if (!component)
15983dc29b8bSKuninori Morimoto 			return -EPROBE_DEFER;
15993ca041edSSebastian Reichel 
1600bee886f1SKuninori Morimoto 		component->init = aux->init;
1601c2b71c71SKuninori Morimoto 		/* see for_each_card_auxs */
1602d2e3a135SSylwester Nawrocki 		list_add(&component->card_aux_list, &card->aux_comp_list);
1603bee886f1SKuninori Morimoto 	}
1604f2ed6b07SMengdong Lin 	return 0;
1605b19e6e7bSMark Brown }
1606b19e6e7bSMark Brown 
1607f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1608f2ed6b07SMengdong Lin {
1609991454e1SKuninori Morimoto 	struct snd_soc_component *comp;
1610f2ed6b07SMengdong Lin 	int order;
1611f2ed6b07SMengdong Lin 	int ret;
1612f2ed6b07SMengdong Lin 
16131a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1614c2b71c71SKuninori Morimoto 		for_each_card_auxs(card, comp) {
1615f2ed6b07SMengdong Lin 			if (comp->driver->probe_order == order) {
1616f2ed6b07SMengdong Lin 				ret = soc_probe_component(card,	comp);
1617f2ed6b07SMengdong Lin 				if (ret < 0) {
1618f2ed6b07SMengdong Lin 					dev_err(card->dev,
1619f2ed6b07SMengdong Lin 						"ASoC: failed to probe aux component %s %d\n",
1620f2ed6b07SMengdong Lin 						comp->name, ret);
1621f2ed6b07SMengdong Lin 					return ret;
1622f2ed6b07SMengdong Lin 				}
1623f2ed6b07SMengdong Lin 			}
1624f2ed6b07SMengdong Lin 		}
1625f2ed6b07SMengdong Lin 	}
162665d9361fSLars-Peter Clausen 
162744c69bb1SLars-Peter Clausen 	return 0;
16283ca041edSSebastian Reichel }
16292eea392dSJarkko Nikula 
1630f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
163144c69bb1SLars-Peter Clausen {
1632f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1633f2ed6b07SMengdong Lin 	int order;
163444c69bb1SLars-Peter Clausen 
16351a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1636c2b71c71SKuninori Morimoto 		for_each_card_auxs_safe(card, comp, _comp) {
1637e8fbd250SKuninori Morimoto 			if (comp->driver->remove_order == order)
1638f2ed6b07SMengdong Lin 				soc_remove_component(comp);
16395f3484acSLars-Peter Clausen 		}
16402eea392dSJarkko Nikula 	}
16412eea392dSJarkko Nikula }
16422eea392dSJarkko Nikula 
1643ce64c8b9SLars-Peter Clausen /**
1644ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1645ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1646ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1647ce64c8b9SLars-Peter Clausen  *
1648ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1649ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1650ce64c8b9SLars-Peter Clausen  *
1651ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1652ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1653ce64c8b9SLars-Peter Clausen  *
1654ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1655ce64c8b9SLars-Peter Clausen  */
1656ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1657ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1658ce64c8b9SLars-Peter Clausen {
1659ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
16600b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1661ce64c8b9SLars-Peter Clausen 	unsigned int i;
1662ce64c8b9SLars-Peter Clausen 	int ret;
1663ce64c8b9SLars-Peter Clausen 
16640b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1665ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1666ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1667ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1668ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1669ce64c8b9SLars-Peter Clausen 			return ret;
1670ce64c8b9SLars-Peter Clausen 		}
1671ce64c8b9SLars-Peter Clausen 	}
1672ce64c8b9SLars-Peter Clausen 
16732c7b696aSMarcel Ziswiler 	/*
16742c7b696aSMarcel Ziswiler 	 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
16752c7b696aSMarcel Ziswiler 	 * the component which has non_legacy_dai_naming is Codec
16762c7b696aSMarcel Ziswiler 	 */
1677999f7f5aSKuninori Morimoto 	if (cpu_dai->component->driver->non_legacy_dai_naming) {
1678ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1679ce64c8b9SLars-Peter Clausen 
1680ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1681ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1682ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1683ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1684ce64c8b9SLars-Peter Clausen 			break;
1685ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1686ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1687ce64c8b9SLars-Peter Clausen 			break;
1688ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1689ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1690ce64c8b9SLars-Peter Clausen 			break;
1691ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1692ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1693ce64c8b9SLars-Peter Clausen 			break;
1694ce64c8b9SLars-Peter Clausen 		}
1695ce64c8b9SLars-Peter Clausen 
1696ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1697ce64c8b9SLars-Peter Clausen 	}
1698ce64c8b9SLars-Peter Clausen 
1699ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1700ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1701ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1702ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1703ce64c8b9SLars-Peter Clausen 		return ret;
1704ce64c8b9SLars-Peter Clausen 	}
1705ce64c8b9SLars-Peter Clausen 
1706ce64c8b9SLars-Peter Clausen 	return 0;
1707ce64c8b9SLars-Peter Clausen }
1708ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1709ce64c8b9SLars-Peter Clausen 
17101f5a4535STakashi Iwai #ifdef CONFIG_DMI
17112c7b696aSMarcel Ziswiler /*
17122c7b696aSMarcel Ziswiler  * Trim special characters, and replace '-' with '_' since '-' is used to
1713345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1714345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1715345233d7SLiam Girdwood  */
1716345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1717345233d7SLiam Girdwood {
1718345233d7SLiam Girdwood 	int i, j = 0;
1719345233d7SLiam Girdwood 
1720345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1721345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1722345233d7SLiam Girdwood 		    || (name[i] == '_'))
1723345233d7SLiam Girdwood 			name[j++] = name[i];
1724345233d7SLiam Girdwood 		else if (name[i] == '-')
1725345233d7SLiam Girdwood 			name[j++] = '_';
1726345233d7SLiam Girdwood 	}
1727345233d7SLiam Girdwood 
1728345233d7SLiam Girdwood 	name[j] = '\0';
1729345233d7SLiam Girdwood }
1730345233d7SLiam Girdwood 
17312c7b696aSMarcel Ziswiler /*
17322c7b696aSMarcel Ziswiler  * Check if a DMI field is valid, i.e. not containing any string
173398faf436SMengdong Lin  * in the black list.
173498faf436SMengdong Lin  */
173598faf436SMengdong Lin static int is_dmi_valid(const char *field)
173698faf436SMengdong Lin {
173798faf436SMengdong Lin 	int i = 0;
173898faf436SMengdong Lin 
173998faf436SMengdong Lin 	while (dmi_blacklist[i]) {
174098faf436SMengdong Lin 		if (strstr(field, dmi_blacklist[i]))
174198faf436SMengdong Lin 			return 0;
174298faf436SMengdong Lin 		i++;
174346b5a4d2SWu Fengguang 	}
174498faf436SMengdong Lin 
174598faf436SMengdong Lin 	return 1;
174698faf436SMengdong Lin }
174798faf436SMengdong Lin 
1748345233d7SLiam Girdwood /**
1749345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
1750345233d7SLiam Girdwood  * @card: The card to register DMI names
1751345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
1752345233d7SLiam Girdwood  *
1753345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
1754345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
1755345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
1756345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
1757345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
1758345233d7SLiam Girdwood  * as the sound card long name, in the format of
1759345233d7SLiam Girdwood  * "vendor-product-version-board"
1760345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
1761345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
1762345233d7SLiam Girdwood  * (UCM) configurations for the card.
1763345233d7SLiam Girdwood  *
1764345233d7SLiam Girdwood  * Possible card long names may be:
1765345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
1766345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1767345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1768345233d7SLiam Girdwood  *
1769345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
1770345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
1771345233d7SLiam Girdwood  *
1772345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
1773345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
1774345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
1775345233d7SLiam Girdwood  * charactors like SPACE.
1776345233d7SLiam Girdwood  *
1777345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
1778345233d7SLiam Girdwood  */
1779345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1780345233d7SLiam Girdwood {
1781345233d7SLiam Girdwood 	const char *vendor, *product, *product_version, *board;
1782345233d7SLiam Girdwood 	size_t longname_buf_size = sizeof(card->snd_card->longname);
1783345233d7SLiam Girdwood 	size_t len;
1784345233d7SLiam Girdwood 
1785345233d7SLiam Girdwood 	if (card->long_name)
1786345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
1787345233d7SLiam Girdwood 
1788345233d7SLiam Girdwood 	/* make up dmi long name as: vendor.product.version.board */
1789345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
179098faf436SMengdong Lin 	if (!vendor || !is_dmi_valid(vendor)) {
1791345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1792345233d7SLiam Girdwood 		return 0;
1793345233d7SLiam Girdwood 	}
1794345233d7SLiam Girdwood 
1795345233d7SLiam Girdwood 	snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1796345233d7SLiam Girdwood 			 "%s", vendor);
1797345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
1798345233d7SLiam Girdwood 
1799345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
180098faf436SMengdong Lin 	if (product && is_dmi_valid(product)) {
1801345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1802345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1803345233d7SLiam Girdwood 			 longname_buf_size - len,
1804345233d7SLiam Girdwood 			 "-%s", product);
1805345233d7SLiam Girdwood 
1806345233d7SLiam Girdwood 		len++;	/* skip the separator "-" */
1807345233d7SLiam Girdwood 		if (len < longname_buf_size)
1808345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1809345233d7SLiam Girdwood 
18102c7b696aSMarcel Ziswiler 		/*
18112c7b696aSMarcel Ziswiler 		 * some vendors like Lenovo may only put a self-explanatory
1812345233d7SLiam Girdwood 		 * name in the product version field
1813345233d7SLiam Girdwood 		 */
1814345233d7SLiam Girdwood 		product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
181598faf436SMengdong Lin 		if (product_version && is_dmi_valid(product_version)) {
1816345233d7SLiam Girdwood 			len = strlen(card->dmi_longname);
1817345233d7SLiam Girdwood 			snprintf(card->dmi_longname + len,
1818345233d7SLiam Girdwood 				 longname_buf_size - len,
1819345233d7SLiam Girdwood 				 "-%s", product_version);
1820345233d7SLiam Girdwood 
1821345233d7SLiam Girdwood 			len++;
1822345233d7SLiam Girdwood 			if (len < longname_buf_size)
1823345233d7SLiam Girdwood 				cleanup_dmi_name(card->dmi_longname + len);
1824345233d7SLiam Girdwood 		}
1825345233d7SLiam Girdwood 	}
1826345233d7SLiam Girdwood 
1827345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
182898faf436SMengdong Lin 	if (board && is_dmi_valid(board)) {
1829345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1830345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1831345233d7SLiam Girdwood 			 longname_buf_size - len,
1832345233d7SLiam Girdwood 			 "-%s", board);
1833345233d7SLiam Girdwood 
1834345233d7SLiam Girdwood 		len++;
1835345233d7SLiam Girdwood 		if (len < longname_buf_size)
1836345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1837345233d7SLiam Girdwood 	} else if (!product) {
1838345233d7SLiam Girdwood 		/* fall back to using legacy name */
1839345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1840345233d7SLiam Girdwood 		return 0;
1841345233d7SLiam Girdwood 	}
1842345233d7SLiam Girdwood 
1843345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
1844345233d7SLiam Girdwood 	if (flavour) {
1845345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1846345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1847345233d7SLiam Girdwood 			 longname_buf_size - len,
1848345233d7SLiam Girdwood 			 "-%s", flavour);
1849345233d7SLiam Girdwood 
1850345233d7SLiam Girdwood 		len++;
1851345233d7SLiam Girdwood 		if (len < longname_buf_size)
1852345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1853345233d7SLiam Girdwood 	}
1854345233d7SLiam Girdwood 
1855345233d7SLiam Girdwood 	/* set the card long name */
1856345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
1857345233d7SLiam Girdwood 
1858345233d7SLiam Girdwood 	return 0;
1859345233d7SLiam Girdwood }
1860345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
18611f5a4535STakashi Iwai #endif /* CONFIG_DMI */
1862345233d7SLiam Girdwood 
1863a655de80SLiam Girdwood static void soc_check_tplg_fes(struct snd_soc_card *card)
1864a655de80SLiam Girdwood {
1865a655de80SLiam Girdwood 	struct snd_soc_component *component;
1866a655de80SLiam Girdwood 	const struct snd_soc_component_driver *comp_drv;
1867a655de80SLiam Girdwood 	struct snd_soc_dai_link *dai_link;
1868a655de80SLiam Girdwood 	int i;
1869a655de80SLiam Girdwood 
1870368dee94SKuninori Morimoto 	for_each_component(component) {
1871a655de80SLiam Girdwood 
187249f9c4f2SDaniel Baluta 		/* does this component override BEs ? */
1873a655de80SLiam Girdwood 		if (!component->driver->ignore_machine)
1874a655de80SLiam Girdwood 			continue;
1875a655de80SLiam Girdwood 
1876a655de80SLiam Girdwood 		/* for this machine ? */
1877e194098bSPierre-Louis Bossart 		if (!strcmp(component->driver->ignore_machine,
1878a655de80SLiam Girdwood 			    card->dev->driver->name))
1879e194098bSPierre-Louis Bossart 			goto match;
1880e194098bSPierre-Louis Bossart 		if (strcmp(component->driver->ignore_machine,
1881e194098bSPierre-Louis Bossart 			   dev_name(card->dev)))
1882a655de80SLiam Girdwood 			continue;
1883e194098bSPierre-Louis Bossart match:
1884a655de80SLiam Girdwood 		/* machine matches, so override the rtd data */
18857fe072b4SKuninori Morimoto 		for_each_card_prelinks(card, i, dai_link) {
1886a655de80SLiam Girdwood 
1887a655de80SLiam Girdwood 			/* ignore this FE */
1888a655de80SLiam Girdwood 			if (dai_link->dynamic) {
1889a655de80SLiam Girdwood 				dai_link->ignore = true;
1890a655de80SLiam Girdwood 				continue;
1891a655de80SLiam Girdwood 			}
1892a655de80SLiam Girdwood 
189349f9c4f2SDaniel Baluta 			dev_info(card->dev, "info: override BE DAI link %s\n",
1894a655de80SLiam Girdwood 				 card->dai_link[i].name);
1895a655de80SLiam Girdwood 
1896a655de80SLiam Girdwood 			/* override platform component */
1897adb76b5bSKuninori Morimoto 			if (!dai_link->platforms) {
1898daecf46eSKuninori Morimoto 				dev_err(card->dev, "init platform error");
1899daecf46eSKuninori Morimoto 				continue;
1900daecf46eSKuninori Morimoto 			}
1901910fdcabSKuninori Morimoto 			dai_link->platforms->name = component->name;
1902a655de80SLiam Girdwood 
1903a655de80SLiam Girdwood 			/* convert non BE into BE */
1904a655de80SLiam Girdwood 			dai_link->no_pcm = 1;
1905a655de80SLiam Girdwood 
1906a655de80SLiam Girdwood 			/* override any BE fixups */
1907a655de80SLiam Girdwood 			dai_link->be_hw_params_fixup =
1908a655de80SLiam Girdwood 				component->driver->be_hw_params_fixup;
1909a655de80SLiam Girdwood 
19102c7b696aSMarcel Ziswiler 			/*
19112c7b696aSMarcel Ziswiler 			 * most BE links don't set stream name, so set it to
1912a655de80SLiam Girdwood 			 * dai link name if it's NULL to help bind widgets.
1913a655de80SLiam Girdwood 			 */
1914a655de80SLiam Girdwood 			if (!dai_link->stream_name)
1915a655de80SLiam Girdwood 				dai_link->stream_name = dai_link->name;
1916a655de80SLiam Girdwood 		}
1917a655de80SLiam Girdwood 
1918a655de80SLiam Girdwood 		/* Inform userspace we are using alternate topology */
1919a655de80SLiam Girdwood 		if (component->driver->topology_name_prefix) {
1920a655de80SLiam Girdwood 
1921a655de80SLiam Girdwood 			/* topology shortname created? */
1922a655de80SLiam Girdwood 			if (!card->topology_shortname_created) {
1923a655de80SLiam Girdwood 				comp_drv = component->driver;
1924a655de80SLiam Girdwood 
1925a655de80SLiam Girdwood 				snprintf(card->topology_shortname, 32, "%s-%s",
1926a655de80SLiam Girdwood 					 comp_drv->topology_name_prefix,
1927a655de80SLiam Girdwood 					 card->name);
1928a655de80SLiam Girdwood 				card->topology_shortname_created = true;
1929a655de80SLiam Girdwood 			}
1930a655de80SLiam Girdwood 
1931a655de80SLiam Girdwood 			/* use topology shortname */
1932a655de80SLiam Girdwood 			card->name = card->topology_shortname;
1933a655de80SLiam Girdwood 		}
1934a655de80SLiam Girdwood 	}
1935a655de80SLiam Girdwood }
1936a655de80SLiam Girdwood 
19370f23f718SKuninori Morimoto #define soc_setup_card_name(name, name1, name2, norm)		\
19380f23f718SKuninori Morimoto 	__soc_setup_card_name(name, sizeof(name), name1, name2, norm)
19390f23f718SKuninori Morimoto static void __soc_setup_card_name(char *name, int len,
19400f23f718SKuninori Morimoto 				  const char *name1, const char *name2,
19410f23f718SKuninori Morimoto 				  int normalization)
19420f23f718SKuninori Morimoto {
19430f23f718SKuninori Morimoto 	int i;
19440f23f718SKuninori Morimoto 
19450f23f718SKuninori Morimoto 	snprintf(name, len, "%s", name1 ? name1 : name2);
19460f23f718SKuninori Morimoto 
19470f23f718SKuninori Morimoto 	if (!normalization)
19480f23f718SKuninori Morimoto 		return;
19490f23f718SKuninori Morimoto 
19500f23f718SKuninori Morimoto 	/*
19510f23f718SKuninori Morimoto 	 * Name normalization
19520f23f718SKuninori Morimoto 	 *
19530f23f718SKuninori Morimoto 	 * The driver name is somewhat special, as it's used as a key for
19540f23f718SKuninori Morimoto 	 * searches in the user-space.
19550f23f718SKuninori Morimoto 	 *
19560f23f718SKuninori Morimoto 	 * ex)
19570f23f718SKuninori Morimoto 	 *	"abcd??efg" -> "abcd__efg"
19580f23f718SKuninori Morimoto 	 */
19590f23f718SKuninori Morimoto 	for (i = 0; i < len; i++) {
19600f23f718SKuninori Morimoto 		switch (name[i]) {
19610f23f718SKuninori Morimoto 		case '_':
19620f23f718SKuninori Morimoto 		case '-':
19630f23f718SKuninori Morimoto 		case '\0':
19640f23f718SKuninori Morimoto 			break;
19650f23f718SKuninori Morimoto 		default:
19660f23f718SKuninori Morimoto 			if (!isalnum(name[i]))
19670f23f718SKuninori Morimoto 				name[i] = '_';
19680f23f718SKuninori Morimoto 			break;
19690f23f718SKuninori Morimoto 		}
19700f23f718SKuninori Morimoto 	}
19710f23f718SKuninori Morimoto }
19720f23f718SKuninori Morimoto 
1973a4de83a3SKuninori Morimoto static void soc_cleanup_card_resources(struct snd_soc_card *card)
197453e947a0SKuninori Morimoto {
19757ce6088fSKuninori Morimoto 	struct snd_soc_dai_link *link, *_link;
19767ce6088fSKuninori Morimoto 
1977*df95a16dSPierre-Louis Bossart 	/* This should be called before snd_card_free() */
1978*df95a16dSPierre-Louis Bossart 	soc_remove_link_components(card);
1979*df95a16dSPierre-Louis Bossart 
198053e947a0SKuninori Morimoto 	/* free the ALSA card at first; this syncs with pending operations */
198129040d1aSKuninori Morimoto 	if (card->snd_card) {
198253e947a0SKuninori Morimoto 		snd_card_free(card->snd_card);
198329040d1aSKuninori Morimoto 		card->snd_card = NULL;
198429040d1aSKuninori Morimoto 	}
198553e947a0SKuninori Morimoto 
198653e947a0SKuninori Morimoto 	/* remove and free each DAI */
19877ce6088fSKuninori Morimoto 	soc_remove_link_dais(card);
19887ce6088fSKuninori Morimoto 
19897ce6088fSKuninori Morimoto 	for_each_card_links_safe(card, link, _link)
19907ce6088fSKuninori Morimoto 		snd_soc_remove_dai_link(card, link);
19917ce6088fSKuninori Morimoto 
199253e947a0SKuninori Morimoto 	/* remove auxiliary devices */
199353e947a0SKuninori Morimoto 	soc_remove_aux_devices(card);
1994e8fbd250SKuninori Morimoto 	soc_unbind_aux_dev(card);
199553e947a0SKuninori Morimoto 
199653e947a0SKuninori Morimoto 	snd_soc_dapm_free(&card->dapm);
199753e947a0SKuninori Morimoto 	soc_cleanup_card_debugfs(card);
199853e947a0SKuninori Morimoto 
199953e947a0SKuninori Morimoto 	/* remove the card */
200053e947a0SKuninori Morimoto 	if (card->remove)
200153e947a0SKuninori Morimoto 		card->remove(card);
200253e947a0SKuninori Morimoto }
200353e947a0SKuninori Morimoto 
2004b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
2005f0fba2adSLiam Girdwood {
20061a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
200761b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
2008c7e73774SKuninori Morimoto 	int ret, i;
200996dd3622SMark Brown 
201034e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
201101b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
2012f0fba2adSLiam Girdwood 
201395c267ddSKuninori Morimoto 	snd_soc_dapm_init(&card->dapm, card, NULL);
201453e947a0SKuninori Morimoto 
2015a655de80SLiam Girdwood 	/* check whether any platform is ignore machine FE and using topology */
2016a655de80SLiam Girdwood 	soc_check_tplg_fes(card);
2017a655de80SLiam Girdwood 
201844c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
2019bee886f1SKuninori Morimoto 	ret = soc_bind_aux_dev(card);
2020bee886f1SKuninori Morimoto 	if (ret < 0)
202153e947a0SKuninori Morimoto 		goto probe_end;
2022db2a4165SFrank Mandarino 
2023f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
2024d8145989SKuninori Morimoto 	card->num_rtd = 0;
20255b99a0aaSKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link) {
20265b99a0aaSKuninori Morimoto 		ret = snd_soc_add_dai_link(card, dai_link);
20275b99a0aaSKuninori Morimoto 		if (ret < 0)
20285b99a0aaSKuninori Morimoto 			goto probe_end;
20295b99a0aaSKuninori Morimoto 	}
2030f8f80361SMengdong Lin 
2031f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
2032102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2033f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
2034f0fba2adSLiam Girdwood 	if (ret < 0) {
203510e8aa9aSMichał Mirosław 		dev_err(card->dev,
203610e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
203710e8aa9aSMichał Mirosław 			card->name, ret);
203853e947a0SKuninori Morimoto 		goto probe_end;
2039db2a4165SFrank Mandarino 	}
2040db2a4165SFrank Mandarino 
20410757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
20420757d834SLars-Peter Clausen 
2043b3da4251SKuninori Morimoto 	soc_resume_init(card);
20446ed25978SAndy Green 
2045b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
20469a841ebbSMark Brown 					card->num_dapm_widgets);
2047b8ba3b57SKuninori Morimoto 	if (ret < 0)
2048b8ba3b57SKuninori Morimoto 		goto probe_end;
20499a841ebbSMark Brown 
2050b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2051f23e860eSNicolin Chen 					card->num_of_dapm_widgets);
2052b8ba3b57SKuninori Morimoto 	if (ret < 0)
2053b8ba3b57SKuninori Morimoto 		goto probe_end;
2054f23e860eSNicolin Chen 
2055f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
2056f0fba2adSLiam Girdwood 	if (card->probe) {
2057e7361ec4SMark Brown 		ret = card->probe(card);
2058f0fba2adSLiam Girdwood 		if (ret < 0)
205953e947a0SKuninori Morimoto 			goto probe_end;
2060f0fba2adSLiam Girdwood 	}
2061f0fba2adSLiam Girdwood 
206262ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
206362f07a6bSKuninori Morimoto 	ret = soc_probe_link_components(card);
206462ae68faSStephen Warren 	if (ret < 0) {
2065f110bfc7SLiam Girdwood 		dev_err(card->dev,
206662f07a6bSKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
206753e947a0SKuninori Morimoto 		goto probe_end;
206862ae68faSStephen Warren 	}
206962ae68faSStephen Warren 
2070f2ed6b07SMengdong Lin 	/* probe auxiliary components */
2071f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
2072f2ed6b07SMengdong Lin 	if (ret < 0)
207353e947a0SKuninori Morimoto 		goto probe_end;
2074f2ed6b07SMengdong Lin 
207562ae68faSStephen Warren 	/* probe all DAI links on this card */
2076c7e73774SKuninori Morimoto 	ret = soc_probe_link_dais(card);
2077fe3e78e0SMark Brown 	if (ret < 0) {
2078f110bfc7SLiam Girdwood 		dev_err(card->dev,
2079c7e73774SKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
208053e947a0SKuninori Morimoto 		goto probe_end;
2081fe3e78e0SMark Brown 	}
2082fe3e78e0SMark Brown 
2083c4b46982SKuninori Morimoto 	for_each_card_rtds(card, rtd)
2084c4b46982SKuninori Morimoto 		soc_link_init(card, rtd);
2085c4b46982SKuninori Morimoto 
2086888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
2087b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
2088888df395SMark Brown 
20899b98c7c2SKuninori Morimoto 	ret = snd_soc_add_card_controls(card, card->controls,
20902c7b696aSMarcel Ziswiler 					card->num_controls);
20919b98c7c2SKuninori Morimoto 	if (ret < 0)
20929b98c7c2SKuninori Morimoto 		goto probe_end;
2093b7af1dafSMark Brown 
2094daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2095b8ad29deSMark Brown 				      card->num_dapm_routes);
2096daa480bdSKuninori Morimoto 	if (ret < 0)
2097daa480bdSKuninori Morimoto 		goto probe_end;
2098b8ad29deSMark Brown 
2099daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2100f23e860eSNicolin Chen 				      card->num_of_dapm_routes);
2101daa480bdSKuninori Morimoto 	if (ret < 0)
2102daa480bdSKuninori Morimoto 		goto probe_end;
210375d9ac46SMark Brown 
2104861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
2105861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
2106861886d3STakashi Iwai 
21070f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->shortname,
21080f23f718SKuninori Morimoto 			    card->name, NULL, 0);
21090f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->longname,
21100f23f718SKuninori Morimoto 			    card->long_name, card->name, 0);
21110f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->driver,
21120f23f718SKuninori Morimoto 			    card->driver_name, card->name, 1);
2113fe3e78e0SMark Brown 
211428e9ad92SMark Brown 	if (card->late_probe) {
211528e9ad92SMark Brown 		ret = card->late_probe(card);
211628e9ad92SMark Brown 		if (ret < 0) {
2117f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
211828e9ad92SMark Brown 				card->name, ret);
211953e947a0SKuninori Morimoto 			goto probe_end;
212028e9ad92SMark Brown 		}
212128e9ad92SMark Brown 	}
212228e9ad92SMark Brown 
2123824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
21248c193b8dSLars-Peter Clausen 
2125f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2126fe3e78e0SMark Brown 	if (ret < 0) {
2127f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2128f110bfc7SLiam Girdwood 				ret);
212953e947a0SKuninori Morimoto 		goto probe_end;
2130fe3e78e0SMark Brown 	}
2131fe3e78e0SMark Brown 
2132435c5e25SMark Brown 	card->instantiated = 1;
2133882eab6cSTzung-Bi Shih 	dapm_mark_endpoints_dirty(card);
21344f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2135b19e6e7bSMark Brown 
213653e947a0SKuninori Morimoto probe_end:
213753e947a0SKuninori Morimoto 	if (ret < 0)
213853e947a0SKuninori Morimoto 		soc_cleanup_card_resources(card);
2139db2a4165SFrank Mandarino 
2140f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
214134e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2142db2a4165SFrank Mandarino 
2143b19e6e7bSMark Brown 	return ret;
2144435c5e25SMark Brown }
2145435c5e25SMark Brown 
2146435c5e25SMark Brown /* probes a new socdev */
2147435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2148435c5e25SMark Brown {
2149f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2150435c5e25SMark Brown 
215170a7ca34SVinod Koul 	/*
215270a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
215370a7ca34SVinod Koul 	 * we should not be here in that case so ret error
215470a7ca34SVinod Koul 	 */
215570a7ca34SVinod Koul 	if (!card)
215670a7ca34SVinod Koul 		return -EINVAL;
215770a7ca34SVinod Koul 
2158fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2159f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2160fe4085e8SMark Brown 		 card->name);
2161fe4085e8SMark Brown 
2162435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2163435c5e25SMark Brown 	card->dev = &pdev->dev;
2164f0fba2adSLiam Girdwood 
216528d528c8SMark Brown 	return snd_soc_register_card(card);
2166435c5e25SMark Brown }
2167435c5e25SMark Brown 
2168b0e26485SVinod Koul /* removes a socdev */
2169b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2170b0e26485SVinod Koul {
2171b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2172b0e26485SVinod Koul 
2173c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2174db2a4165SFrank Mandarino 	return 0;
2175db2a4165SFrank Mandarino }
2176db2a4165SFrank Mandarino 
21776f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
217851737470SMark Brown {
21796f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
21801a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
218151737470SMark Brown 
218251737470SMark Brown 	if (!card->instantiated)
2183416356fcSMark Brown 		return 0;
218451737470SMark Brown 
21852c7b696aSMarcel Ziswiler 	/*
21862c7b696aSMarcel Ziswiler 	 * Flush out pmdown_time work - we actually do want to run it
21872c7b696aSMarcel Ziswiler 	 * now, we're shutting down so no imminent restart.
21882c7b696aSMarcel Ziswiler 	 */
218965462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
219051737470SMark Brown 
2191f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2192416356fcSMark Brown 
2193988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
2194bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
219588bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
21960b7990e3SKuninori Morimoto 		struct snd_soc_dai *codec_dai;
21971a497983SMengdong Lin 		int i;
219888bd870fSBenoit Cousson 
2199988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
22000b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
220188bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
220288bd870fSBenoit Cousson 		}
2203988e8cc4SNicolin Chen 	}
2204988e8cc4SNicolin Chen 
2205416356fcSMark Brown 	return 0;
220651737470SMark Brown }
22076f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
220851737470SMark Brown 
22096f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2210b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2211b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2212b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2213b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
22146f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2215b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2216416356fcSMark Brown };
2217deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2218416356fcSMark Brown 
2219db2a4165SFrank Mandarino /* ASoC platform driver */
2220db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2221db2a4165SFrank Mandarino 	.driver		= {
2222db2a4165SFrank Mandarino 		.name		= "soc-audio",
22236f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2224db2a4165SFrank Mandarino 	},
2225db2a4165SFrank Mandarino 	.probe		= soc_probe,
2226db2a4165SFrank Mandarino 	.remove		= soc_remove,
2227db2a4165SFrank Mandarino };
2228db2a4165SFrank Mandarino 
2229096e49d5SMark Brown /**
2230db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2231db2a4165SFrank Mandarino  * @_template: control template
2232db2a4165SFrank Mandarino  * @data: control private data
2233ac11a2b3SMark Brown  * @long_name: control long name
2234efb7ac3fSMark Brown  * @prefix: control name prefix
2235db2a4165SFrank Mandarino  *
2236db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2237db2a4165SFrank Mandarino  *
2238db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2239db2a4165SFrank Mandarino  */
2240db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
22413056557fSMark Brown 				  void *data, const char *long_name,
2242efb7ac3fSMark Brown 				  const char *prefix)
2243db2a4165SFrank Mandarino {
2244db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2245efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2246efb7ac3fSMark Brown 	char *name = NULL;
2247db2a4165SFrank Mandarino 
2248db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2249db2a4165SFrank Mandarino 	template.index = 0;
2250db2a4165SFrank Mandarino 
2251efb7ac3fSMark Brown 	if (!long_name)
2252efb7ac3fSMark Brown 		long_name = template.name;
2253efb7ac3fSMark Brown 
2254efb7ac3fSMark Brown 	if (prefix) {
22552b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2256efb7ac3fSMark Brown 		if (!name)
2257efb7ac3fSMark Brown 			return NULL;
2258efb7ac3fSMark Brown 
2259efb7ac3fSMark Brown 		template.name = name;
2260efb7ac3fSMark Brown 	} else {
2261efb7ac3fSMark Brown 		template.name = long_name;
2262efb7ac3fSMark Brown 	}
2263efb7ac3fSMark Brown 
2264efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2265efb7ac3fSMark Brown 
2266efb7ac3fSMark Brown 	kfree(name);
2267efb7ac3fSMark Brown 
2268efb7ac3fSMark Brown 	return kcontrol;
2269db2a4165SFrank Mandarino }
2270db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2271db2a4165SFrank Mandarino 
2272022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2273022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2274022658beSLiam Girdwood 	const char *prefix, void *data)
2275022658beSLiam Girdwood {
2276022658beSLiam Girdwood 	int err, i;
2277022658beSLiam Girdwood 
2278022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2279022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
22802c7b696aSMarcel Ziswiler 
2281022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2282022658beSLiam Girdwood 						     control->name, prefix));
2283022658beSLiam Girdwood 		if (err < 0) {
2284f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2285f110bfc7SLiam Girdwood 				control->name, err);
2286022658beSLiam Girdwood 			return err;
2287022658beSLiam Girdwood 		}
2288022658beSLiam Girdwood 	}
2289022658beSLiam Girdwood 
2290022658beSLiam Girdwood 	return 0;
2291022658beSLiam Girdwood }
2292022658beSLiam Girdwood 
22934fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
22944fefd698SDimitris Papastamos 					       const char *name)
22954fefd698SDimitris Papastamos {
22964fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
22974fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
22984fefd698SDimitris Papastamos 
22994fefd698SDimitris Papastamos 	if (unlikely(!name))
23004fefd698SDimitris Papastamos 		return NULL;
23014fefd698SDimitris Papastamos 
23024fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
23034fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
23044fefd698SDimitris Papastamos 			return kctl;
23054fefd698SDimitris Papastamos 	return NULL;
23064fefd698SDimitris Papastamos }
23074fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
23084fefd698SDimitris Papastamos 
2309db2a4165SFrank Mandarino /**
23100f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
23110f2780adSLars-Peter Clausen  *
23120f2780adSLars-Peter Clausen  * @component: Component to add controls to
23130f2780adSLars-Peter Clausen  * @controls: Array of controls to add
23140f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
23150f2780adSLars-Peter Clausen  *
23160f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
23170f2780adSLars-Peter Clausen  */
23180f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
23190f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
23200f2780adSLars-Peter Clausen {
23210f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
23220f2780adSLars-Peter Clausen 
23230f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
23240f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
23250f2780adSLars-Peter Clausen }
23260f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
23270f2780adSLars-Peter Clausen 
23280f2780adSLars-Peter Clausen /**
2329022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2330022658beSLiam Girdwood  * Convenience function to add a list of controls.
2331022658beSLiam Girdwood  *
2332022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2333022658beSLiam Girdwood  * @controls: array of controls to add
2334022658beSLiam Girdwood  * @num_controls: number of elements in the array
2335022658beSLiam Girdwood  *
2336022658beSLiam Girdwood  * Return 0 for success, else error.
2337022658beSLiam Girdwood  */
2338022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2339022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2340022658beSLiam Girdwood {
2341022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2342022658beSLiam Girdwood 
2343022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2344022658beSLiam Girdwood 			NULL, soc_card);
2345022658beSLiam Girdwood }
2346022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2347022658beSLiam Girdwood 
2348022658beSLiam Girdwood /**
2349022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2350022658beSLiam Girdwood  * Convienience function to add a list of controls.
2351022658beSLiam Girdwood  *
2352022658beSLiam Girdwood  * @dai: DAI to add controls to
2353022658beSLiam Girdwood  * @controls: array of controls to add
2354022658beSLiam Girdwood  * @num_controls: number of elements in the array
2355022658beSLiam Girdwood  *
2356022658beSLiam Girdwood  * Return 0 for success, else error.
2357022658beSLiam Girdwood  */
2358022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2359022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2360022658beSLiam Girdwood {
2361313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2362022658beSLiam Girdwood 
2363022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2364022658beSLiam Girdwood 			NULL, dai);
2365022658beSLiam Girdwood }
2366022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2367022658beSLiam Girdwood 
2368e894efefSSrinivas Kandagatla static int snd_soc_bind_card(struct snd_soc_card *card)
2369e894efefSSrinivas Kandagatla {
2370e894efefSSrinivas Kandagatla 	struct snd_soc_pcm_runtime *rtd;
2371e894efefSSrinivas Kandagatla 	int ret;
2372e894efefSSrinivas Kandagatla 
2373e894efefSSrinivas Kandagatla 	ret = snd_soc_instantiate_card(card);
2374e894efefSSrinivas Kandagatla 	if (ret != 0)
2375e894efefSSrinivas Kandagatla 		return ret;
2376e894efefSSrinivas Kandagatla 
2377e894efefSSrinivas Kandagatla 	/* deactivate pins to sleep state */
2378bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
2379e894efefSSrinivas Kandagatla 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2380e894efefSSrinivas Kandagatla 		struct snd_soc_dai *codec_dai;
2381e894efefSSrinivas Kandagatla 		int j;
2382e894efefSSrinivas Kandagatla 
2383e894efefSSrinivas Kandagatla 		for_each_rtd_codec_dai(rtd, j, codec_dai) {
2384e894efefSSrinivas Kandagatla 			if (!codec_dai->active)
2385e894efefSSrinivas Kandagatla 				pinctrl_pm_select_sleep_state(codec_dai->dev);
2386e894efefSSrinivas Kandagatla 		}
2387e894efefSSrinivas Kandagatla 
2388e894efefSSrinivas Kandagatla 		if (!cpu_dai->active)
2389e894efefSSrinivas Kandagatla 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
2390e894efefSSrinivas Kandagatla 	}
2391e894efefSSrinivas Kandagatla 
2392e894efefSSrinivas Kandagatla 	return ret;
2393e894efefSSrinivas Kandagatla }
2394e894efefSSrinivas Kandagatla 
2395c5af3a2eSMark Brown /**
2396c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2397c5af3a2eSMark Brown  *
2398ac11a2b3SMark Brown  * @card: Card to register
2399c5af3a2eSMark Brown  *
2400c5af3a2eSMark Brown  */
240170a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2402c5af3a2eSMark Brown {
2403c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2404c5af3a2eSMark Brown 		return -EINVAL;
2405c5af3a2eSMark Brown 
2406ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2407ed77cc12SMark Brown 
2408b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->widgets);
2409b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->paths);
2410b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->dapm_list);
2411b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->aux_comp_list);
2412b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->component_dev_list);
2413b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->list);
2414f8f80361SMengdong Lin 	INIT_LIST_HEAD(&card->dai_link_list);
24151a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
2416db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
24178a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2418b03bfaecSKuninori Morimoto 
2419c5af3a2eSMark Brown 	card->instantiated = 0;
2420f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
2421a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
242272b745e3SPeter Ujfalusi 	mutex_init(&card->pcm_mutex);
2423a9764869SKaiChieh Chuang 	spin_lock_init(&card->dpcm_lock);
2424c5af3a2eSMark Brown 
2425e894efefSSrinivas Kandagatla 	return snd_soc_bind_card(card);
2426c5af3a2eSMark Brown }
242770a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2428c5af3a2eSMark Brown 
2429e894efefSSrinivas Kandagatla static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2430e894efefSSrinivas Kandagatla {
2431e894efefSSrinivas Kandagatla 	if (card->instantiated) {
2432e894efefSSrinivas Kandagatla 		card->instantiated = false;
2433e894efefSSrinivas Kandagatla 		snd_soc_dapm_shutdown(card);
243453e947a0SKuninori Morimoto 		snd_soc_flush_all_delayed_work(card);
2435f96fb7d1SRanjani Sridharan 
2436e894efefSSrinivas Kandagatla 		soc_cleanup_card_resources(card);
2437e894efefSSrinivas Kandagatla 		if (!unregister)
2438e894efefSSrinivas Kandagatla 			list_add(&card->list, &unbind_card_list);
2439e894efefSSrinivas Kandagatla 	} else {
2440e894efefSSrinivas Kandagatla 		if (unregister)
2441e894efefSSrinivas Kandagatla 			list_del(&card->list);
2442e894efefSSrinivas Kandagatla 	}
2443e894efefSSrinivas Kandagatla }
2444e894efefSSrinivas Kandagatla 
2445c5af3a2eSMark Brown /**
2446c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2447c5af3a2eSMark Brown  *
2448ac11a2b3SMark Brown  * @card: Card to unregister
2449c5af3a2eSMark Brown  *
2450c5af3a2eSMark Brown  */
245170a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2452c5af3a2eSMark Brown {
2453b545542aSKuninori Morimoto 	mutex_lock(&client_mutex);
2454e894efefSSrinivas Kandagatla 	snd_soc_unbind_card(card, true);
2455b545542aSKuninori Morimoto 	mutex_unlock(&client_mutex);
2456f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2457c5af3a2eSMark Brown 
2458c5af3a2eSMark Brown 	return 0;
2459c5af3a2eSMark Brown }
246070a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2461c5af3a2eSMark Brown 
2462f0fba2adSLiam Girdwood /*
2463f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
2464f0fba2adSLiam Girdwood  * and sanitizing names.
2465f0fba2adSLiam Girdwood  */
24660b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2467f0fba2adSLiam Girdwood {
2468f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
2469f0fba2adSLiam Girdwood 	int id1, id2;
2470f0fba2adSLiam Girdwood 
2471f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
2472f0fba2adSLiam Girdwood 		return NULL;
2473f0fba2adSLiam Girdwood 
247458818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2475f0fba2adSLiam Girdwood 
2476f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
2477f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
2478f0fba2adSLiam Girdwood 	if (found) {
2479f0fba2adSLiam Girdwood 		/* get ID */
2480f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2481f0fba2adSLiam Girdwood 
2482f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
2483f0fba2adSLiam Girdwood 			if (*id == -1)
2484f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
2485f0fba2adSLiam Girdwood 		}
2486f0fba2adSLiam Girdwood 
2487f0fba2adSLiam Girdwood 	} else {
2488f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr" */
2489f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2490f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
2491f0fba2adSLiam Girdwood 
2492f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
249305899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
2494f0fba2adSLiam Girdwood 
2495f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
24962c7b696aSMarcel Ziswiler 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
24972c7b696aSMarcel Ziswiler 				 name);
249858818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2499f0fba2adSLiam Girdwood 		} else
2500f0fba2adSLiam Girdwood 			*id = 0;
2501f0fba2adSLiam Girdwood 	}
2502f0fba2adSLiam Girdwood 
250350014499SKuninori Morimoto 	return devm_kstrdup(dev, name, GFP_KERNEL);
2504f0fba2adSLiam Girdwood }
2505f0fba2adSLiam Girdwood 
2506f0fba2adSLiam Girdwood /*
2507f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
2508f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
2509f0fba2adSLiam Girdwood  */
2510f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
2511f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
2512f0fba2adSLiam Girdwood {
2513f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
251410e8aa9aSMichał Mirosław 		dev_err(dev,
251510e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
251610e8aa9aSMichał Mirosław 			dev_name(dev));
2517f0fba2adSLiam Girdwood 		return NULL;
2518f0fba2adSLiam Girdwood 	}
2519f0fba2adSLiam Girdwood 
252050014499SKuninori Morimoto 	return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2521f0fba2adSLiam Girdwood }
2522f0fba2adSLiam Girdwood 
2523e11381f3SKuninori Morimoto static void soc_del_dai(struct snd_soc_dai *dai)
2524e11381f3SKuninori Morimoto {
2525e11381f3SKuninori Morimoto 	dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2526e11381f3SKuninori Morimoto 	list_del(&dai->list);
2527e11381f3SKuninori Morimoto }
2528e11381f3SKuninori Morimoto 
25295e4fb372SMengdong Lin /* Create a DAI and add it to the component's DAI list */
25305e4fb372SMengdong Lin static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
25315e4fb372SMengdong Lin 	struct snd_soc_dai_driver *dai_drv,
25325e4fb372SMengdong Lin 	bool legacy_dai_naming)
25335e4fb372SMengdong Lin {
25345e4fb372SMengdong Lin 	struct device *dev = component->dev;
25355e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
25365e4fb372SMengdong Lin 
25375e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
25385e4fb372SMengdong Lin 
253950014499SKuninori Morimoto 	dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
25405e4fb372SMengdong Lin 	if (dai == NULL)
25415e4fb372SMengdong Lin 		return NULL;
25425e4fb372SMengdong Lin 
25435e4fb372SMengdong Lin 	/*
25445e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
25455e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
25465e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
25475e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
25485e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
25495e4fb372SMengdong Lin 	 * component-less anymore.
25505e4fb372SMengdong Lin 	 */
25515e4fb372SMengdong Lin 	if (legacy_dai_naming &&
25525e4fb372SMengdong Lin 	    (dai_drv->id == 0 || dai_drv->name == NULL)) {
25535e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
25545e4fb372SMengdong Lin 	} else {
25555e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
25565e4fb372SMengdong Lin 		if (dai_drv->id)
25575e4fb372SMengdong Lin 			dai->id = dai_drv->id;
25585e4fb372SMengdong Lin 		else
25595e4fb372SMengdong Lin 			dai->id = component->num_dai;
25605e4fb372SMengdong Lin 	}
256150014499SKuninori Morimoto 	if (!dai->name)
25625e4fb372SMengdong Lin 		return NULL;
25635e4fb372SMengdong Lin 
25645e4fb372SMengdong Lin 	dai->component = component;
25655e4fb372SMengdong Lin 	dai->dev = dev;
25665e4fb372SMengdong Lin 	dai->driver = dai_drv;
25675e4fb372SMengdong Lin 	if (!dai->driver->ops)
25685e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
25695e4fb372SMengdong Lin 
257015a0c645SKuninori Morimoto 	/* see for_each_component_dais */
257158bf4179SKuninori Morimoto 	list_add_tail(&dai->list, &component->dai_list);
25725e4fb372SMengdong Lin 	component->num_dai++;
25735e4fb372SMengdong Lin 
25745e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
25755e4fb372SMengdong Lin 	return dai;
25765e4fb372SMengdong Lin }
25775e4fb372SMengdong Lin 
2578e11381f3SKuninori Morimoto void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2579e11381f3SKuninori Morimoto {
2580e11381f3SKuninori Morimoto 	soc_del_dai(dai);
2581e11381f3SKuninori Morimoto }
2582e11381f3SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2583e11381f3SKuninori Morimoto 
25849115171aSMark Brown /**
258568003e6cSMengdong Lin  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
258668003e6cSMengdong Lin  *
258768003e6cSMengdong Lin  * @component: The component the DAIs are registered for
258868003e6cSMengdong Lin  * @dai_drv: DAI driver to use for the DAI
258968003e6cSMengdong Lin  *
259068003e6cSMengdong Lin  * Topology can use this API to register DAIs when probing a component.
259168003e6cSMengdong Lin  * These DAIs's widgets will be freed in the card cleanup and the DAIs
259268003e6cSMengdong Lin  * will be freed in the component cleanup.
259368003e6cSMengdong Lin  */
2594e443c205SKuninori Morimoto struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
25955d075197SKuninori Morimoto 					 struct snd_soc_dai_driver *dai_drv,
25965d075197SKuninori Morimoto 					 bool legacy_dai_naming)
259768003e6cSMengdong Lin {
259871cb85f5SKuninori Morimoto 	struct device *dev = component->dev;
259971cb85f5SKuninori Morimoto 
260071cb85f5SKuninori Morimoto 	dev_dbg(dev, "ASoC: dai register %s\n", dai_drv->name);
260171cb85f5SKuninori Morimoto 
260268003e6cSMengdong Lin 	lockdep_assert_held(&client_mutex);
2603e443c205SKuninori Morimoto 	return soc_add_dai(component, dai_drv, legacy_dai_naming);
260468003e6cSMengdong Lin }
260568003e6cSMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_register_dai);
260668003e6cSMengdong Lin 
2607daf77373SKuninori Morimoto /**
26083f6674aeSKuninori Morimoto  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
26093f6674aeSKuninori Morimoto  *
26103f6674aeSKuninori Morimoto  * @component: The component for which the DAIs should be unregistered
26113f6674aeSKuninori Morimoto  */
26123f6674aeSKuninori Morimoto static void snd_soc_unregister_dais(struct snd_soc_component *component)
26133f6674aeSKuninori Morimoto {
26143f6674aeSKuninori Morimoto 	struct snd_soc_dai *dai, *_dai;
26153f6674aeSKuninori Morimoto 
2616e11381f3SKuninori Morimoto 	for_each_component_dais_safe(component, dai, _dai)
2617e11381f3SKuninori Morimoto 		snd_soc_unregister_dai(dai);
26183f6674aeSKuninori Morimoto }
26193f6674aeSKuninori Morimoto 
26203f6674aeSKuninori Morimoto /**
2621daf77373SKuninori Morimoto  * snd_soc_register_dais - Register a DAI with the ASoC core
2622daf77373SKuninori Morimoto  *
2623daf77373SKuninori Morimoto  * @component: The component the DAIs are registered for
2624daf77373SKuninori Morimoto  * @dai_drv: DAI driver to use for the DAIs
2625daf77373SKuninori Morimoto  * @count: Number of DAIs
2626daf77373SKuninori Morimoto  */
2627daf77373SKuninori Morimoto static int snd_soc_register_dais(struct snd_soc_component *component,
2628daf77373SKuninori Morimoto 				 struct snd_soc_dai_driver *dai_drv,
2629daf77373SKuninori Morimoto 				 size_t count)
2630daf77373SKuninori Morimoto {
2631daf77373SKuninori Morimoto 	struct snd_soc_dai *dai;
2632daf77373SKuninori Morimoto 	unsigned int i;
2633daf77373SKuninori Morimoto 	int ret;
2634daf77373SKuninori Morimoto 
2635daf77373SKuninori Morimoto 	for (i = 0; i < count; i++) {
263671cb85f5SKuninori Morimoto 		dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
2637daf77373SKuninori Morimoto 				  !component->driver->non_legacy_dai_naming);
2638daf77373SKuninori Morimoto 		if (dai == NULL) {
2639daf77373SKuninori Morimoto 			ret = -ENOMEM;
2640daf77373SKuninori Morimoto 			goto err;
2641daf77373SKuninori Morimoto 		}
2642daf77373SKuninori Morimoto 	}
2643daf77373SKuninori Morimoto 
2644daf77373SKuninori Morimoto 	return 0;
2645daf77373SKuninori Morimoto 
2646daf77373SKuninori Morimoto err:
2647daf77373SKuninori Morimoto 	snd_soc_unregister_dais(component);
2648daf77373SKuninori Morimoto 
2649daf77373SKuninori Morimoto 	return ret;
2650daf77373SKuninori Morimoto }
2651daf77373SKuninori Morimoto 
2652bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
2653bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
2654d191bd8dSKuninori Morimoto {
26558d92bb51SKuninori Morimoto 	INIT_LIST_HEAD(&component->dai_list);
2656495efdb0SKuninori Morimoto 	INIT_LIST_HEAD(&component->dobj_list);
2657495efdb0SKuninori Morimoto 	INIT_LIST_HEAD(&component->card_list);
26588d92bb51SKuninori Morimoto 	mutex_init(&component->io_mutex);
26598d92bb51SKuninori Morimoto 
2660bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
2661bb13109dSLars-Peter Clausen 	if (!component->name) {
2662bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
2663d191bd8dSKuninori Morimoto 		return -ENOMEM;
2664d191bd8dSKuninori Morimoto 	}
2665d191bd8dSKuninori Morimoto 
2666bb13109dSLars-Peter Clausen 	component->dev = dev;
2667bb13109dSLars-Peter Clausen 	component->driver = driver;
2668e2c330b9SLars-Peter Clausen 
2669bb13109dSLars-Peter Clausen 	return 0;
2670d191bd8dSKuninori Morimoto }
2671d191bd8dSKuninori Morimoto 
267220feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2673886f5692SLars-Peter Clausen {
2674886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
267520feb881SLars-Peter Clausen 
2676886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
2677886f5692SLars-Peter Clausen 	if (val_bytes > 0)
2678886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
2679886f5692SLars-Peter Clausen }
268020feb881SLars-Peter Clausen 
2681e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
2682e874bf5fSLars-Peter Clausen 
268320feb881SLars-Peter Clausen /**
26842c7b696aSMarcel Ziswiler  * snd_soc_component_init_regmap() - Initialize regmap instance for the
26852c7b696aSMarcel Ziswiler  *                                   component
268620feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
268720feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
268820feb881SLars-Peter Clausen  *
268920feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
269020feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
269120feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
269220feb881SLars-Peter Clausen  * before the first IO attempt of the component.
269320feb881SLars-Peter Clausen  */
269420feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
269520feb881SLars-Peter Clausen 	struct regmap *regmap)
269620feb881SLars-Peter Clausen {
269720feb881SLars-Peter Clausen 	component->regmap = regmap;
269820feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
2699886f5692SLars-Peter Clausen }
270020feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
270120feb881SLars-Peter Clausen 
270220feb881SLars-Peter Clausen /**
27032c7b696aSMarcel Ziswiler  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
27042c7b696aSMarcel Ziswiler  *                                   component
270520feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
270620feb881SLars-Peter Clausen  *
270720feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
270820feb881SLars-Peter Clausen  * removes the regmap instance from the component.
270920feb881SLars-Peter Clausen  *
271020feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
271120feb881SLars-Peter Clausen  * to initialize the regmap instance.
271220feb881SLars-Peter Clausen  */
271320feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
271420feb881SLars-Peter Clausen {
271520feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
271620feb881SLars-Peter Clausen 	component->regmap = NULL;
271720feb881SLars-Peter Clausen }
271820feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2719886f5692SLars-Peter Clausen 
2720e874bf5fSLars-Peter Clausen #endif
2721d191bd8dSKuninori Morimoto 
2722273d778eSKuninori Morimoto #define ENDIANNESS_MAP(name) \
2723273d778eSKuninori Morimoto 	(SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2724273d778eSKuninori Morimoto static u64 endianness_format_map[] = {
2725273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S16_),
2726273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U16_),
2727273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_),
2728273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_),
2729273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S32_),
2730273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U32_),
2731273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_3),
2732273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_3),
2733273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S20_3),
2734273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U20_3),
2735273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S18_3),
2736273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U18_3),
2737273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT_),
2738273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT64_),
2739273d778eSKuninori Morimoto 	ENDIANNESS_MAP(IEC958_SUBFRAME_),
2740273d778eSKuninori Morimoto };
2741273d778eSKuninori Morimoto 
2742273d778eSKuninori Morimoto /*
2743273d778eSKuninori Morimoto  * Fix up the DAI formats for endianness: codecs don't actually see
2744273d778eSKuninori Morimoto  * the endianness of the data but we're using the CPU format
2745273d778eSKuninori Morimoto  * definitions which do need to include endianness so we ensure that
2746273d778eSKuninori Morimoto  * codec DAIs always have both big and little endian variants set.
2747273d778eSKuninori Morimoto  */
2748273d778eSKuninori Morimoto static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2749273d778eSKuninori Morimoto {
2750273d778eSKuninori Morimoto 	int i;
2751273d778eSKuninori Morimoto 
2752273d778eSKuninori Morimoto 	for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2753273d778eSKuninori Morimoto 		if (stream->formats & endianness_format_map[i])
2754273d778eSKuninori Morimoto 			stream->formats |= endianness_format_map[i];
2755273d778eSKuninori Morimoto }
2756273d778eSKuninori Morimoto 
2757e894efefSSrinivas Kandagatla static void snd_soc_try_rebind_card(void)
2758e894efefSSrinivas Kandagatla {
2759e894efefSSrinivas Kandagatla 	struct snd_soc_card *card, *c;
2760e894efefSSrinivas Kandagatla 
2761b245d273SKuninori Morimoto 	list_for_each_entry_safe(card, c, &unbind_card_list, list)
2762e894efefSSrinivas Kandagatla 		if (!snd_soc_bind_card(card))
2763e894efefSSrinivas Kandagatla 			list_del(&card->list);
2764e894efefSSrinivas Kandagatla }
2765e894efefSSrinivas Kandagatla 
2766486c7978SKuninori Morimoto static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2767486c7978SKuninori Morimoto {
2768b18768f5SKuninori Morimoto 	struct snd_soc_card *card = component->card;
2769b18768f5SKuninori Morimoto 
2770486c7978SKuninori Morimoto 	snd_soc_unregister_dais(component);
2771b18768f5SKuninori Morimoto 
2772b18768f5SKuninori Morimoto 	if (card)
2773b18768f5SKuninori Morimoto 		snd_soc_unbind_card(card, false);
2774b18768f5SKuninori Morimoto 
2775b18768f5SKuninori Morimoto 	list_del(&component->list);
2776486c7978SKuninori Morimoto }
2777486c7978SKuninori Morimoto 
2778e0dac41bSKuninori Morimoto int snd_soc_add_component(struct device *dev,
2779e0dac41bSKuninori Morimoto 			struct snd_soc_component *component,
2780cf9e829eSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2781d191bd8dSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2782d191bd8dSKuninori Morimoto 			int num_dai)
2783d191bd8dSKuninori Morimoto {
2784bb13109dSLars-Peter Clausen 	int ret;
2785273d778eSKuninori Morimoto 	int i;
2786d191bd8dSKuninori Morimoto 
2787b18768f5SKuninori Morimoto 	mutex_lock(&client_mutex);
2788b18768f5SKuninori Morimoto 
2789cf9e829eSKuninori Morimoto 	ret = snd_soc_component_initialize(component, component_driver, dev);
2790bb13109dSLars-Peter Clausen 	if (ret)
2791bb13109dSLars-Peter Clausen 		goto err_free;
2792bb13109dSLars-Peter Clausen 
2793273d778eSKuninori Morimoto 	if (component_driver->endianness) {
2794273d778eSKuninori Morimoto 		for (i = 0; i < num_dai; i++) {
2795273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].playback);
2796273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].capture);
2797273d778eSKuninori Morimoto 		}
2798273d778eSKuninori Morimoto 	}
2799273d778eSKuninori Morimoto 
28000e7b25c6SKuninori Morimoto 	ret = snd_soc_register_dais(component, dai_drv, num_dai);
2801bb13109dSLars-Peter Clausen 	if (ret < 0) {
2802f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
2803bb13109dSLars-Peter Clausen 		goto err_cleanup;
2804bb13109dSLars-Peter Clausen 	}
2805bb13109dSLars-Peter Clausen 
2806b18768f5SKuninori Morimoto 	if (!component->driver->write && !component->driver->read) {
2807b18768f5SKuninori Morimoto 		if (!component->regmap)
2808b18768f5SKuninori Morimoto 			component->regmap = dev_get_regmap(component->dev,
2809b18768f5SKuninori Morimoto 							   NULL);
2810b18768f5SKuninori Morimoto 		if (component->regmap)
2811b18768f5SKuninori Morimoto 			snd_soc_component_setup_regmap(component);
2812b18768f5SKuninori Morimoto 	}
2813bb13109dSLars-Peter Clausen 
2814b18768f5SKuninori Morimoto 	/* see for_each_component */
2815b18768f5SKuninori Morimoto 	list_add(&component->list, &component_list);
2816bb13109dSLars-Peter Clausen 
2817bb13109dSLars-Peter Clausen err_cleanup:
2818b18768f5SKuninori Morimoto 	if (ret < 0)
2819486c7978SKuninori Morimoto 		snd_soc_del_component_unlocked(component);
2820bb13109dSLars-Peter Clausen err_free:
2821b18768f5SKuninori Morimoto 	mutex_unlock(&client_mutex);
2822b18768f5SKuninori Morimoto 
2823b18768f5SKuninori Morimoto 	if (ret == 0)
2824b18768f5SKuninori Morimoto 		snd_soc_try_rebind_card();
2825b18768f5SKuninori Morimoto 
2826bb13109dSLars-Peter Clausen 	return ret;
2827d191bd8dSKuninori Morimoto }
2828e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component);
2829e0dac41bSKuninori Morimoto 
2830e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev,
2831e0dac41bSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2832e0dac41bSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2833e0dac41bSKuninori Morimoto 			int num_dai)
2834e0dac41bSKuninori Morimoto {
2835e0dac41bSKuninori Morimoto 	struct snd_soc_component *component;
2836e0dac41bSKuninori Morimoto 
28377ecbd6a9SKuninori Morimoto 	component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
283808e61d03SKuninori Morimoto 	if (!component)
2839e0dac41bSKuninori Morimoto 		return -ENOMEM;
2840e0dac41bSKuninori Morimoto 
2841e0dac41bSKuninori Morimoto 	return snd_soc_add_component(dev, component, component_driver,
2842e0dac41bSKuninori Morimoto 				     dai_drv, num_dai);
2843e0dac41bSKuninori Morimoto }
2844d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
2845d191bd8dSKuninori Morimoto 
2846d191bd8dSKuninori Morimoto /**
28472eccea8cSKuninori Morimoto  * snd_soc_unregister_component - Unregister all related component
28482eccea8cSKuninori Morimoto  * from the ASoC core
2849d191bd8dSKuninori Morimoto  *
2850628536eaSJonathan Corbet  * @dev: The device to unregister
2851d191bd8dSKuninori Morimoto  */
28522eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
28532eccea8cSKuninori Morimoto {
2854ac6a4dd3SKuninori Morimoto 	struct snd_soc_component *component;
2855ac6a4dd3SKuninori Morimoto 
2856ac6a4dd3SKuninori Morimoto 	mutex_lock(&client_mutex);
2857ac6a4dd3SKuninori Morimoto 	while (1) {
2858ac6a4dd3SKuninori Morimoto 		component = snd_soc_lookup_component(dev, NULL);
2859ac6a4dd3SKuninori Morimoto 		if (!component)
2860ac6a4dd3SKuninori Morimoto 			break;
2861ac6a4dd3SKuninori Morimoto 
2862ac6a4dd3SKuninori Morimoto 		snd_soc_del_component_unlocked(component);
2863ac6a4dd3SKuninori Morimoto 	}
2864ac6a4dd3SKuninori Morimoto 	mutex_unlock(&client_mutex);
2865d191bd8dSKuninori Morimoto }
2866d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2867d191bd8dSKuninori Morimoto 
2868bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
2869b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2870bec4fa05SStephen Warren 			       const char *propname)
2871bec4fa05SStephen Warren {
2872b07609ceSKuninori Morimoto 	struct device_node *np;
2873bec4fa05SStephen Warren 	int ret;
2874bec4fa05SStephen Warren 
28757e07e7c0STushar Behera 	if (!card->dev) {
28767e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
28777e07e7c0STushar Behera 		return -EINVAL;
28787e07e7c0STushar Behera 	}
28797e07e7c0STushar Behera 
28807e07e7c0STushar Behera 	np = card->dev->of_node;
28817e07e7c0STushar Behera 
2882bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
2883bec4fa05SStephen Warren 	/*
2884bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
2885bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
2886bec4fa05SStephen Warren 	 * snd_soc_register_card.
2887bec4fa05SStephen Warren 	 */
2888bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
2889bec4fa05SStephen Warren 		dev_err(card->dev,
2890f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
2891bec4fa05SStephen Warren 			propname, ret);
2892bec4fa05SStephen Warren 		return ret;
2893bec4fa05SStephen Warren 	}
2894bec4fa05SStephen Warren 
2895bec4fa05SStephen Warren 	return 0;
2896bec4fa05SStephen Warren }
2897b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2898bec4fa05SStephen Warren 
28999a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
29009a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
29019a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
29029a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
29039a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
29049a6d4860SXiubo Li };
29059a6d4860SXiubo Li 
290621efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
29079a6d4860SXiubo Li 					  const char *propname)
29089a6d4860SXiubo Li {
290921efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
29109a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
29119a6d4860SXiubo Li 	const char *template, *wname;
29129a6d4860SXiubo Li 	int i, j, num_widgets, ret;
29139a6d4860SXiubo Li 
29149a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
29159a6d4860SXiubo Li 	if (num_widgets < 0) {
29169a6d4860SXiubo Li 		dev_err(card->dev,
29179a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
29189a6d4860SXiubo Li 		return -EINVAL;
29199a6d4860SXiubo Li 	}
29209a6d4860SXiubo Li 	if (num_widgets & 1) {
29219a6d4860SXiubo Li 		dev_err(card->dev,
29229a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
29239a6d4860SXiubo Li 		return -EINVAL;
29249a6d4860SXiubo Li 	}
29259a6d4860SXiubo Li 
29269a6d4860SXiubo Li 	num_widgets /= 2;
29279a6d4860SXiubo Li 	if (!num_widgets) {
29289a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
29299a6d4860SXiubo Li 			propname);
29309a6d4860SXiubo Li 		return -EINVAL;
29319a6d4860SXiubo Li 	}
29329a6d4860SXiubo Li 
29339a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
29349a6d4860SXiubo Li 			       GFP_KERNEL);
29359a6d4860SXiubo Li 	if (!widgets) {
29369a6d4860SXiubo Li 		dev_err(card->dev,
29379a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
29389a6d4860SXiubo Li 		return -ENOMEM;
29399a6d4860SXiubo Li 	}
29409a6d4860SXiubo Li 
29419a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
29429a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
29439a6d4860SXiubo Li 			2 * i, &template);
29449a6d4860SXiubo Li 		if (ret) {
29459a6d4860SXiubo Li 			dev_err(card->dev,
29469a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
29479a6d4860SXiubo Li 				propname, 2 * i, ret);
29489a6d4860SXiubo Li 			return -EINVAL;
29499a6d4860SXiubo Li 		}
29509a6d4860SXiubo Li 
29519a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
29529a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
29539a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
29549a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
29559a6d4860SXiubo Li 				break;
29569a6d4860SXiubo Li 			}
29579a6d4860SXiubo Li 		}
29589a6d4860SXiubo Li 
29599a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
29609a6d4860SXiubo Li 			dev_err(card->dev,
29619a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
29629a6d4860SXiubo Li 				template);
29639a6d4860SXiubo Li 			return -EINVAL;
29649a6d4860SXiubo Li 		}
29659a6d4860SXiubo Li 
29669a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
29679a6d4860SXiubo Li 						    (2 * i) + 1,
29689a6d4860SXiubo Li 						    &wname);
29699a6d4860SXiubo Li 		if (ret) {
29709a6d4860SXiubo Li 			dev_err(card->dev,
29719a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
29729a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
29739a6d4860SXiubo Li 			return -EINVAL;
29749a6d4860SXiubo Li 		}
29759a6d4860SXiubo Li 
29769a6d4860SXiubo Li 		widgets[i].name = wname;
29779a6d4860SXiubo Li 	}
29789a6d4860SXiubo Li 
2979f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
2980f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
29819a6d4860SXiubo Li 
29829a6d4860SXiubo Li 	return 0;
29839a6d4860SXiubo Li }
298421efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
29859a6d4860SXiubo Li 
2986cbdfab3bSJerome Brunet int snd_soc_of_get_slot_mask(struct device_node *np,
29876131084aSJyri Sarha 			     const char *prop_name,
29886131084aSJyri Sarha 			     unsigned int *mask)
29896131084aSJyri Sarha {
29906131084aSJyri Sarha 	u32 val;
29916c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
29926131084aSJyri Sarha 	int i;
29936131084aSJyri Sarha 
29946131084aSJyri Sarha 	if (!of_slot_mask)
29956131084aSJyri Sarha 		return 0;
29966131084aSJyri Sarha 	val /= sizeof(u32);
29976131084aSJyri Sarha 	for (i = 0; i < val; i++)
29986131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
29996131084aSJyri Sarha 			*mask |= (1 << i);
30006131084aSJyri Sarha 
30016131084aSJyri Sarha 	return val;
30026131084aSJyri Sarha }
3003cbdfab3bSJerome Brunet EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
30046131084aSJyri Sarha 
300589c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
30066131084aSJyri Sarha 			      unsigned int *tx_mask,
30076131084aSJyri Sarha 			      unsigned int *rx_mask,
300889c67857SXiubo Li 			      unsigned int *slots,
300989c67857SXiubo Li 			      unsigned int *slot_width)
301089c67857SXiubo Li {
301189c67857SXiubo Li 	u32 val;
301289c67857SXiubo Li 	int ret;
301389c67857SXiubo Li 
30146131084aSJyri Sarha 	if (tx_mask)
30156131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
30166131084aSJyri Sarha 	if (rx_mask)
30176131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
30186131084aSJyri Sarha 
301989c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
302089c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
302189c67857SXiubo Li 		if (ret)
302289c67857SXiubo Li 			return ret;
302389c67857SXiubo Li 
302489c67857SXiubo Li 		if (slots)
302589c67857SXiubo Li 			*slots = val;
302689c67857SXiubo Li 	}
302789c67857SXiubo Li 
302889c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
302989c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
303089c67857SXiubo Li 		if (ret)
303189c67857SXiubo Li 			return ret;
303289c67857SXiubo Li 
303389c67857SXiubo Li 		if (slot_width)
303489c67857SXiubo Li 			*slot_width = val;
303589c67857SXiubo Li 	}
303689c67857SXiubo Li 
303789c67857SXiubo Li 	return 0;
303889c67857SXiubo Li }
303989c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
304089c67857SXiubo Li 
30413b710356SKuninori Morimoto void snd_soc_of_parse_node_prefix(struct device_node *np,
30425e3cdaa2SKuninori Morimoto 				  struct snd_soc_codec_conf *codec_conf,
30435e3cdaa2SKuninori Morimoto 				  struct device_node *of_node,
30445e3cdaa2SKuninori Morimoto 				  const char *propname)
30455e3cdaa2SKuninori Morimoto {
30465e3cdaa2SKuninori Morimoto 	const char *str;
30475e3cdaa2SKuninori Morimoto 	int ret;
30485e3cdaa2SKuninori Morimoto 
30495e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
30505e3cdaa2SKuninori Morimoto 	if (ret < 0) {
30515e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
30525e3cdaa2SKuninori Morimoto 		return;
30535e3cdaa2SKuninori Morimoto 	}
30545e3cdaa2SKuninori Morimoto 
30555e3cdaa2SKuninori Morimoto 	codec_conf->of_node	= of_node;
30565e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
30575e3cdaa2SKuninori Morimoto }
30583b710356SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
30595e3cdaa2SKuninori Morimoto 
30602bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3061a4a54dd5SStephen Warren 				   const char *propname)
3062a4a54dd5SStephen Warren {
30632bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
3064e3b1e6a1SMark Brown 	int num_routes;
3065a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3066a4a54dd5SStephen Warren 	int i, ret;
3067a4a54dd5SStephen Warren 
3068a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3069c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
307010e8aa9aSMichał Mirosław 		dev_err(card->dev,
307110e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
307210e8aa9aSMichał Mirosław 			propname);
3073a4a54dd5SStephen Warren 		return -EINVAL;
3074a4a54dd5SStephen Warren 	}
3075a4a54dd5SStephen Warren 	num_routes /= 2;
3076a4a54dd5SStephen Warren 	if (!num_routes) {
3077f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3078a4a54dd5SStephen Warren 			propname);
3079a4a54dd5SStephen Warren 		return -EINVAL;
3080a4a54dd5SStephen Warren 	}
3081a4a54dd5SStephen Warren 
3082a86854d0SKees Cook 	routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
3083a4a54dd5SStephen Warren 			      GFP_KERNEL);
3084a4a54dd5SStephen Warren 	if (!routes) {
3085a4a54dd5SStephen Warren 		dev_err(card->dev,
3086f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3087a4a54dd5SStephen Warren 		return -EINVAL;
3088a4a54dd5SStephen Warren 	}
3089a4a54dd5SStephen Warren 
3090a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3091a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3092e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
3093a4a54dd5SStephen Warren 		if (ret) {
3094c871bd0bSMark Brown 			dev_err(card->dev,
3095c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3096c871bd0bSMark Brown 				propname, 2 * i, ret);
3097a4a54dd5SStephen Warren 			return -EINVAL;
3098a4a54dd5SStephen Warren 		}
3099a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3100e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3101a4a54dd5SStephen Warren 		if (ret) {
3102a4a54dd5SStephen Warren 			dev_err(card->dev,
3103c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3104c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3105a4a54dd5SStephen Warren 			return -EINVAL;
3106a4a54dd5SStephen Warren 		}
3107a4a54dd5SStephen Warren 	}
3108a4a54dd5SStephen Warren 
3109f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
3110f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
3111a4a54dd5SStephen Warren 
3112a4a54dd5SStephen Warren 	return 0;
3113a4a54dd5SStephen Warren }
31142bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3115a4a54dd5SStephen Warren 
3116a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3117389cb834SJyri Sarha 				     const char *prefix,
3118389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3119389cb834SJyri Sarha 				     struct device_node **framemaster)
3120a7930ed4SKuninori Morimoto {
3121a7930ed4SKuninori Morimoto 	int ret, i;
3122a7930ed4SKuninori Morimoto 	char prop[128];
3123a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3124a7930ed4SKuninori Morimoto 	int bit, frame;
3125a7930ed4SKuninori Morimoto 	const char *str;
3126a7930ed4SKuninori Morimoto 	struct {
3127a7930ed4SKuninori Morimoto 		char *name;
3128a7930ed4SKuninori Morimoto 		unsigned int val;
3129a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3130a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3131a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3132a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3133a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3134a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3135a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3136a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3137a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3138a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3139a7930ed4SKuninori Morimoto 	};
3140a7930ed4SKuninori Morimoto 
3141a7930ed4SKuninori Morimoto 	if (!prefix)
3142a7930ed4SKuninori Morimoto 		prefix = "";
3143a7930ed4SKuninori Morimoto 
3144a7930ed4SKuninori Morimoto 	/*
31455711c979SKuninori Morimoto 	 * check "dai-format = xxx"
31465711c979SKuninori Morimoto 	 * or    "[prefix]format = xxx"
3147a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3148a7930ed4SKuninori Morimoto 	 */
31495711c979SKuninori Morimoto 	ret = of_property_read_string(np, "dai-format", &str);
31505711c979SKuninori Morimoto 	if (ret < 0) {
3151a7930ed4SKuninori Morimoto 		snprintf(prop, sizeof(prop), "%sformat", prefix);
3152a7930ed4SKuninori Morimoto 		ret = of_property_read_string(np, prop, &str);
31535711c979SKuninori Morimoto 	}
3154a7930ed4SKuninori Morimoto 	if (ret == 0) {
3155a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3156a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3157a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3158a7930ed4SKuninori Morimoto 				break;
3159a7930ed4SKuninori Morimoto 			}
3160a7930ed4SKuninori Morimoto 		}
3161a7930ed4SKuninori Morimoto 	}
3162a7930ed4SKuninori Morimoto 
3163a7930ed4SKuninori Morimoto 	/*
31648c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3165a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3166a7930ed4SKuninori Morimoto 	 */
31678c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
316851930295SJulia Lawall 	if (of_property_read_bool(np, prop))
31698c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
31708c2d6a9fSKuninori Morimoto 	else
31718c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3172a7930ed4SKuninori Morimoto 
3173a7930ed4SKuninori Morimoto 	/*
3174a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3175a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3176a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3177a7930ed4SKuninori Morimoto 	 */
3178a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3179a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3180a7930ed4SKuninori Morimoto 
3181a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3182a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3183a7930ed4SKuninori Morimoto 
3184a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3185a7930ed4SKuninori Morimoto 	case 0x11:
3186a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3187a7930ed4SKuninori Morimoto 		break;
3188a7930ed4SKuninori Morimoto 	case 0x10:
3189a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3190a7930ed4SKuninori Morimoto 		break;
3191a7930ed4SKuninori Morimoto 	case 0x01:
3192a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3193a7930ed4SKuninori Morimoto 		break;
3194a7930ed4SKuninori Morimoto 	default:
3195a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3196a7930ed4SKuninori Morimoto 		break;
3197a7930ed4SKuninori Morimoto 	}
3198a7930ed4SKuninori Morimoto 
3199a7930ed4SKuninori Morimoto 	/*
3200a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3201a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3202a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3203a7930ed4SKuninori Morimoto 	 */
3204a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3205a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3206389cb834SJyri Sarha 	if (bit && bitclkmaster)
3207389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3208a7930ed4SKuninori Morimoto 
3209a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3210a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3211389cb834SJyri Sarha 	if (frame && framemaster)
3212389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3213a7930ed4SKuninori Morimoto 
3214a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3215a7930ed4SKuninori Morimoto 	case 0x11:
3216a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3217a7930ed4SKuninori Morimoto 		break;
3218a7930ed4SKuninori Morimoto 	case 0x10:
3219a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3220a7930ed4SKuninori Morimoto 		break;
3221a7930ed4SKuninori Morimoto 	case 0x01:
3222a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3223a7930ed4SKuninori Morimoto 		break;
3224a7930ed4SKuninori Morimoto 	default:
3225a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3226a7930ed4SKuninori Morimoto 		break;
3227a7930ed4SKuninori Morimoto 	}
3228a7930ed4SKuninori Morimoto 
3229a7930ed4SKuninori Morimoto 	return format;
3230a7930ed4SKuninori Morimoto }
3231a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3232a7930ed4SKuninori Morimoto 
3233a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
3234a180e8b9SKuninori Morimoto {
323509d4cc03SKuninori Morimoto 	struct snd_soc_component *component;
3236c1e230f0SKuninori Morimoto 	struct snd_soc_dai_link_component dlc;
3237a180e8b9SKuninori Morimoto 	int ret;
3238a180e8b9SKuninori Morimoto 
3239c1e230f0SKuninori Morimoto 	dlc.of_node	= of_graph_get_port_parent(ep);
3240c1e230f0SKuninori Morimoto 	dlc.name	= NULL;
3241a180e8b9SKuninori Morimoto 	/*
3242a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
3243a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
3244a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
3245a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
3246a180e8b9SKuninori Morimoto 	 */
3247a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
3248a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
3249c1e230f0SKuninori Morimoto 	component = soc_find_component(&dlc);
32502c7b1704SKuninori Morimoto 	if (component)
32512c7b1704SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_id(component, ep);
3252a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
3253a180e8b9SKuninori Morimoto 
3254c1e230f0SKuninori Morimoto 	of_node_put(dlc.of_node);
3255c0a480d1STony Lindgren 
3256a180e8b9SKuninori Morimoto 	return ret;
3257a180e8b9SKuninori Morimoto }
3258a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3259a180e8b9SKuninori Morimoto 
32601ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args,
3261cb470087SKuninori Morimoto 				const char **dai_name)
3262cb470087SKuninori Morimoto {
3263cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
32643e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
326593b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3266cb470087SKuninori Morimoto 
3267cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3268368dee94SKuninori Morimoto 	for_each_component(pos) {
3269c0834440SKuninori Morimoto 		component_of_node = soc_component_to_node(pos);
32703e0aa8d8SJyri Sarha 
32713e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
3272cb470087SKuninori Morimoto 			continue;
3273cb470087SKuninori Morimoto 
3274a2a34175SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3275a2a34175SKuninori Morimoto 		if (ret == -ENOTSUPP) {
327658bf4179SKuninori Morimoto 			struct snd_soc_dai *dai;
32776833c452SKuninori Morimoto 			int id = -1;
32786833c452SKuninori Morimoto 
327993b0f3eeSJean-Francois Moine 			switch (args->args_count) {
32806833c452SKuninori Morimoto 			case 0:
32816833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
32826833c452SKuninori Morimoto 				break;
32836833c452SKuninori Morimoto 			case 1:
328493b0f3eeSJean-Francois Moine 				id = args->args[0];
32856833c452SKuninori Morimoto 				break;
32866833c452SKuninori Morimoto 			default:
32876833c452SKuninori Morimoto 				/* not supported */
3288cb470087SKuninori Morimoto 				break;
3289cb470087SKuninori Morimoto 			}
3290cb470087SKuninori Morimoto 
32916833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
32926833c452SKuninori Morimoto 				ret = -EINVAL;
32933dcba280SNicolin Chen 				continue;
32946833c452SKuninori Morimoto 			}
3295e41975edSXiubo Li 
3296e41975edSXiubo Li 			ret = 0;
3297e41975edSXiubo Li 
329858bf4179SKuninori Morimoto 			/* find target DAI */
329915a0c645SKuninori Morimoto 			for_each_component_dais(pos, dai) {
330058bf4179SKuninori Morimoto 				if (id == 0)
330158bf4179SKuninori Morimoto 					break;
330258bf4179SKuninori Morimoto 				id--;
330358bf4179SKuninori Morimoto 			}
330458bf4179SKuninori Morimoto 
330558bf4179SKuninori Morimoto 			*dai_name = dai->driver->name;
3306e41975edSXiubo Li 			if (!*dai_name)
3307e41975edSXiubo Li 				*dai_name = pos->name;
33086833c452SKuninori Morimoto 		}
33096833c452SKuninori Morimoto 
3310cb470087SKuninori Morimoto 		break;
3311cb470087SKuninori Morimoto 	}
3312cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
331393b0f3eeSJean-Francois Moine 	return ret;
331493b0f3eeSJean-Francois Moine }
33151ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
331693b0f3eeSJean-Francois Moine 
331793b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
331893b0f3eeSJean-Francois Moine 			    const char **dai_name)
331993b0f3eeSJean-Francois Moine {
332093b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
332193b0f3eeSJean-Francois Moine 	int ret;
332293b0f3eeSJean-Francois Moine 
332393b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
332493b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
332593b0f3eeSJean-Francois Moine 	if (ret)
332693b0f3eeSJean-Francois Moine 		return ret;
332793b0f3eeSJean-Francois Moine 
332893b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3329cb470087SKuninori Morimoto 
3330cb470087SKuninori Morimoto 	of_node_put(args.np);
3331cb470087SKuninori Morimoto 
3332cb470087SKuninori Morimoto 	return ret;
3333cb470087SKuninori Morimoto }
3334cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3335cb470087SKuninori Morimoto 
333693b0f3eeSJean-Francois Moine /*
333794685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
333894685763SSylwester Nawrocki  * @dai_link: DAI link
333994685763SSylwester Nawrocki  *
334094685763SSylwester Nawrocki  * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
334194685763SSylwester Nawrocki  */
334294685763SSylwester Nawrocki void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
334394685763SSylwester Nawrocki {
33443db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *component;
334594685763SSylwester Nawrocki 	int index;
334694685763SSylwester Nawrocki 
33473db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
334894685763SSylwester Nawrocki 		if (!component->of_node)
334994685763SSylwester Nawrocki 			break;
335094685763SSylwester Nawrocki 		of_node_put(component->of_node);
335194685763SSylwester Nawrocki 		component->of_node = NULL;
335294685763SSylwester Nawrocki 	}
335394685763SSylwester Nawrocki }
335494685763SSylwester Nawrocki EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
335594685763SSylwester Nawrocki 
335694685763SSylwester Nawrocki /*
335793b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
335893b0f3eeSJean-Francois Moine  * @dev: Card device
335993b0f3eeSJean-Francois Moine  * @of_node: Device node
336093b0f3eeSJean-Francois Moine  * @dai_link: DAI link
336193b0f3eeSJean-Francois Moine  *
336293b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
336393b0f3eeSJean-Francois Moine  * 'sound-dai'.
336493b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
336594685763SSylwester Nawrocki  * The device nodes in the array (of_node) must be dereferenced by calling
336694685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs() on @dai_link.
336793b0f3eeSJean-Francois Moine  *
336893b0f3eeSJean-Francois Moine  * Returns 0 for success
336993b0f3eeSJean-Francois Moine  */
337093b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
337193b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
337293b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
337393b0f3eeSJean-Francois Moine {
337493b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
337593b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
337693b0f3eeSJean-Francois Moine 	char *name;
337793b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
337893b0f3eeSJean-Francois Moine 
337993b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
338093b0f3eeSJean-Francois Moine 	name = "sound-dai";
338193b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
338293b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
338393b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
338493b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
338593b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
338693b0f3eeSJean-Francois Moine 		else
338793b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
338893b0f3eeSJean-Francois Moine 		return num_codecs;
338993b0f3eeSJean-Francois Moine 	}
3390a86854d0SKees Cook 	component = devm_kcalloc(dev,
3391a86854d0SKees Cook 				 num_codecs, sizeof(*component),
339293b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
339393b0f3eeSJean-Francois Moine 	if (!component)
339493b0f3eeSJean-Francois Moine 		return -ENOMEM;
339593b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
339693b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
339793b0f3eeSJean-Francois Moine 
339893b0f3eeSJean-Francois Moine 	/* Parse the list */
33993db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
340093b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
340193b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
340293b0f3eeSJean-Francois Moine 						 index, &args);
340393b0f3eeSJean-Francois Moine 		if (ret)
340493b0f3eeSJean-Francois Moine 			goto err;
340593b0f3eeSJean-Francois Moine 		component->of_node = args.np;
340693b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
340793b0f3eeSJean-Francois Moine 		if (ret < 0)
340893b0f3eeSJean-Francois Moine 			goto err;
340993b0f3eeSJean-Francois Moine 	}
341093b0f3eeSJean-Francois Moine 	return 0;
341193b0f3eeSJean-Francois Moine err:
341294685763SSylwester Nawrocki 	snd_soc_of_put_dai_link_codecs(dai_link);
341393b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
341493b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
341593b0f3eeSJean-Francois Moine 	return ret;
341693b0f3eeSJean-Francois Moine }
341793b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
341893b0f3eeSJean-Francois Moine 
3419c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3420db2a4165SFrank Mandarino {
34216553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
3422fb257897SMark Brown 	snd_soc_util_init();
3423fb257897SMark Brown 
3424db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3425db2a4165SFrank Mandarino }
34264abe8e16SMark Brown module_init(snd_soc_init);
3427db2a4165SFrank Mandarino 
34287d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3429db2a4165SFrank Mandarino {
3430fb257897SMark Brown 	snd_soc_util_exit();
34316553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3432fb257897SMark Brown 
3433db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3434db2a4165SFrank Mandarino }
3435db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3436db2a4165SFrank Mandarino 
3437db2a4165SFrank Mandarino /* Module information */
3438d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3439db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3440db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
34418b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3442