xref: /openbmc/linux/sound/soc/soc-core.c (revision 6b1dff0266a30df16846a20d1109ab25b985f0d7)
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 
35947c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
36047c88fffSLiam Girdwood 		const char *dai_link, int stream)
36147c88fffSLiam Girdwood {
3621a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
36347c88fffSLiam Girdwood 
364bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
3651a497983SMengdong Lin 		if (rtd->dai_link->no_pcm &&
3661a497983SMengdong Lin 			!strcmp(rtd->dai_link->name, dai_link))
3671a497983SMengdong Lin 			return rtd->pcm->streams[stream].substream;
36847c88fffSLiam Girdwood 	}
369f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
37047c88fffSLiam Girdwood 	return NULL;
37147c88fffSLiam Girdwood }
37247c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
37347c88fffSLiam Girdwood 
37475ab9eb6SKuninori Morimoto static const struct snd_soc_ops null_snd_soc_ops;
37575ab9eb6SKuninori Morimoto 
3766e864344SKuninori Morimoto static void soc_release_rtd_dev(struct device *dev)
3776e864344SKuninori Morimoto {
3786e864344SKuninori Morimoto 	/* "dev" means "rtd->dev" */
3796e864344SKuninori Morimoto 	kfree(dev);
3806e864344SKuninori Morimoto }
3816e864344SKuninori Morimoto 
3821c93a9e0SKuninori Morimoto static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
3831c93a9e0SKuninori Morimoto {
3846e864344SKuninori Morimoto 	if (!rtd)
3856e864344SKuninori Morimoto 		return;
3866e864344SKuninori Morimoto 
387753ace0aSKuninori Morimoto 	list_del(&rtd->list);
388b7c5bc45SKuninori Morimoto 
389b7c5bc45SKuninori Morimoto 	/*
390b7c5bc45SKuninori Morimoto 	 * we don't need to call kfree() for rtd->dev
391b7c5bc45SKuninori Morimoto 	 * see
392b7c5bc45SKuninori Morimoto 	 *	soc_release_rtd_dev()
393d918a376SKuninori Morimoto 	 *
394d918a376SKuninori Morimoto 	 * We don't need rtd->dev NULL check, because
395d918a376SKuninori Morimoto 	 * it is alloced *before* rtd.
396d918a376SKuninori Morimoto 	 * see
397d918a376SKuninori Morimoto 	 *	soc_new_pcm_runtime()
398b7c5bc45SKuninori Morimoto 	 */
399b7c5bc45SKuninori Morimoto 	device_unregister(rtd->dev);
4001c93a9e0SKuninori Morimoto }
4011c93a9e0SKuninori Morimoto 
4021a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
4031a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
4041a497983SMengdong Lin {
4051a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
406d918a376SKuninori Morimoto 	struct device *dev;
4076e864344SKuninori Morimoto 	int ret;
4081a497983SMengdong Lin 
4096e864344SKuninori Morimoto 	/*
410d918a376SKuninori Morimoto 	 * for rtd->dev
411d918a376SKuninori Morimoto 	 */
412d918a376SKuninori Morimoto 	dev = kzalloc(sizeof(struct device), GFP_KERNEL);
413d918a376SKuninori Morimoto 	if (!dev)
414d918a376SKuninori Morimoto 		return NULL;
415d918a376SKuninori Morimoto 
416d918a376SKuninori Morimoto 	dev->parent	= card->dev;
417d918a376SKuninori Morimoto 	dev->release	= soc_release_rtd_dev;
418d918a376SKuninori Morimoto 	dev->groups	= soc_dev_attr_groups;
419d918a376SKuninori Morimoto 
420d918a376SKuninori Morimoto 	dev_set_name(dev, "%s", dai_link->name);
421d918a376SKuninori Morimoto 
422d918a376SKuninori Morimoto 	ret = device_register(dev);
423d918a376SKuninori Morimoto 	if (ret < 0) {
424d918a376SKuninori Morimoto 		put_device(dev); /* soc_release_rtd_dev */
425d918a376SKuninori Morimoto 		return NULL;
426d918a376SKuninori Morimoto 	}
427d918a376SKuninori Morimoto 
428d918a376SKuninori Morimoto 	/*
4296e864344SKuninori Morimoto 	 * for rtd
4306e864344SKuninori Morimoto 	 */
4314dc0e7dfSKuninori Morimoto 	rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL);
4321a497983SMengdong Lin 	if (!rtd)
4336e864344SKuninori Morimoto 		goto free_rtd;
4341a497983SMengdong Lin 
435d918a376SKuninori Morimoto 	rtd->dev = dev;
436d918a376SKuninori Morimoto 	dev_set_drvdata(dev, rtd);
437d918a376SKuninori Morimoto 
4386e864344SKuninori Morimoto 	/*
4396e864344SKuninori Morimoto 	 * for rtd->codec_dais
4406e864344SKuninori Morimoto 	 */
4414dc0e7dfSKuninori Morimoto 	rtd->codec_dais = devm_kcalloc(dev, dai_link->num_codecs,
4426396bb22SKees Cook 					sizeof(struct snd_soc_dai *),
4431a497983SMengdong Lin 					GFP_KERNEL);
4446e864344SKuninori Morimoto 	if (!rtd->codec_dais)
4456e864344SKuninori Morimoto 		goto free_rtd;
4466e864344SKuninori Morimoto 
4476e864344SKuninori Morimoto 	/*
4486e864344SKuninori Morimoto 	 * rtd remaining settings
4496e864344SKuninori Morimoto 	 */
450929deb84SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
4516e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
4526e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
4536e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
4546e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
4556e864344SKuninori Morimoto 
456929deb84SKuninori Morimoto 	rtd->card = card;
457929deb84SKuninori Morimoto 	rtd->dai_link = dai_link;
458929deb84SKuninori Morimoto 	if (!rtd->dai_link->ops)
459929deb84SKuninori Morimoto 		rtd->dai_link->ops = &null_snd_soc_ops;
460929deb84SKuninori Morimoto 
4616634e3d6SKuninori Morimoto 	/* see for_each_card_rtds */
4621a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
4631a497983SMengdong Lin 	rtd->num = card->num_rtd;
4641a497983SMengdong Lin 	card->num_rtd++;
465a848125eSKuninori Morimoto 
466a848125eSKuninori Morimoto 	return rtd;
4676e864344SKuninori Morimoto 
4686e864344SKuninori Morimoto free_rtd:
4696e864344SKuninori Morimoto 	soc_free_pcm_runtime(rtd);
4706e864344SKuninori Morimoto 	return NULL;
4711a497983SMengdong Lin }
4721a497983SMengdong Lin 
4731a497983SMengdong Lin static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
4741a497983SMengdong Lin {
4751a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, *_rtd;
4761a497983SMengdong Lin 
477753ace0aSKuninori Morimoto 	for_each_card_rtds_safe(card, rtd, _rtd)
4781a497983SMengdong Lin 		soc_free_pcm_runtime(rtd);
4791a497983SMengdong Lin }
4801a497983SMengdong Lin 
48147c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
48247c88fffSLiam Girdwood 		const char *dai_link)
48347c88fffSLiam Girdwood {
4841a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
48547c88fffSLiam Girdwood 
486bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4871a497983SMengdong Lin 		if (!strcmp(rtd->dai_link->name, dai_link))
4881a497983SMengdong Lin 			return rtd;
48947c88fffSLiam Girdwood 	}
490f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
49147c88fffSLiam Girdwood 	return NULL;
49247c88fffSLiam Girdwood }
49347c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
49447c88fffSLiam Girdwood 
49565462e44SKuninori Morimoto static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
49665462e44SKuninori Morimoto {
49765462e44SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
49865462e44SKuninori Morimoto 
49965462e44SKuninori Morimoto 	for_each_card_rtds(card, rtd)
50065462e44SKuninori Morimoto 		flush_delayed_work(&rtd->delayed_work);
50165462e44SKuninori Morimoto }
50265462e44SKuninori Morimoto 
5036f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
504db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5056f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
506db2a4165SFrank Mandarino {
5076f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
508d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
5091a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
5101a497983SMengdong Lin 	int i;
511db2a4165SFrank Mandarino 
512c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
513c5599b87SLars-Peter Clausen 	if (!card->instantiated)
514e3509ff0SDaniel Mack 		return 0;
515e3509ff0SDaniel Mack 
5162c7b696aSMarcel Ziswiler 	/*
5172c7b696aSMarcel Ziswiler 	 * Due to the resume being scheduled into a workqueue we could
5186ed25978SAndy Green 	 * suspend before that's finished - wait for it to complete.
5196ed25978SAndy Green 	 */
520f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
5216ed25978SAndy Green 
5226ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
523f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5246ed25978SAndy Green 
525a00f90f9SMark Brown 	/* mute any active DACs */
526bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5270b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
5283efab7dcSMark Brown 
5291a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5303efab7dcSMark Brown 			continue;
5313efab7dcSMark Brown 
5320b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
53388fdffa2SKuninori Morimoto 			if (dai->playback_active)
53488fdffa2SKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 1,
53588fdffa2SKuninori Morimoto 						SNDRV_PCM_STREAM_PLAYBACK);
536db2a4165SFrank Mandarino 		}
53788bd870fSBenoit Cousson 	}
538db2a4165SFrank Mandarino 
5394ccab3e7SLiam Girdwood 	/* suspend all pcms */
540bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5411a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5423efab7dcSMark Brown 			continue;
5433efab7dcSMark Brown 
5441a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
5453efab7dcSMark Brown 	}
5464ccab3e7SLiam Girdwood 
54787506549SMark Brown 	if (card->suspend_pre)
54870b2ac12SMark Brown 		card->suspend_pre(card);
549db2a4165SFrank Mandarino 
550bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5511a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
5523efab7dcSMark Brown 
5531a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5543efab7dcSMark Brown 			continue;
5553efab7dcSMark Brown 
556e0f22622SKuninori Morimoto 		if (!cpu_dai->driver->bus_control)
557e0f22622SKuninori Morimoto 			snd_soc_dai_suspend(cpu_dai);
558db2a4165SFrank Mandarino 	}
559db2a4165SFrank Mandarino 
56037660b6dSLars-Peter Clausen 	/* close any waiting streams */
56165462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
562db2a4165SFrank Mandarino 
563bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5643efab7dcSMark Brown 
5651a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5663efab7dcSMark Brown 			continue;
5673efab7dcSMark Brown 
5681a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
5697bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
570db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
571f0fba2adSLiam Girdwood 
5721a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
5737bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
574db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
575db2a4165SFrank Mandarino 	}
576db2a4165SFrank Mandarino 
5778be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
5788be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
579e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
580e2d32ff6SMark Brown 
5819178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
582f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
5832c7b696aSMarcel Ziswiler 		struct snd_soc_dapm_context *dapm =
5842c7b696aSMarcel Ziswiler 				snd_soc_component_get_dapm(component);
585d9fc4063SKuninori Morimoto 
5862c7b696aSMarcel Ziswiler 		/*
5872c7b696aSMarcel Ziswiler 		 * If there are paths active then the COMPONENT will be held
5882c7b696aSMarcel Ziswiler 		 * with bias _ON and should not be suspended.
5892c7b696aSMarcel Ziswiler 		 */
590e40fadbcSKuninori Morimoto 		if (!snd_soc_component_is_suspended(component)) {
5914890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
5921547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
593125a25daSMark Brown 				/*
5949178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
595125a25daSMark Brown 				 * bias off then being in STANDBY
596125a25daSMark Brown 				 * means it's doing something,
597125a25daSMark Brown 				 * otherwise fall through.
598125a25daSMark Brown 				 */
5994890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
6009178feb4SKuninori Morimoto 					dev_dbg(component->dev,
60110e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
602125a25daSMark Brown 					break;
603125a25daSMark Brown 				}
6041a12d5dcSGustavo A. R. Silva 				/* fall through */
605a8093297SLars-Peter Clausen 
6061547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
60766c51573SKuninori Morimoto 				snd_soc_component_suspend(component);
6089178feb4SKuninori Morimoto 				if (component->regmap)
6099178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
610988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
6119178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
6121547aba9SMark Brown 				break;
6131547aba9SMark Brown 			default:
6149178feb4SKuninori Morimoto 				dev_dbg(component->dev,
6159178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
6161547aba9SMark Brown 				break;
6171547aba9SMark Brown 			}
6181547aba9SMark Brown 		}
619f0fba2adSLiam Girdwood 	}
620db2a4165SFrank Mandarino 
621bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6221a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6233efab7dcSMark Brown 
6241a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6253efab7dcSMark Brown 			continue;
6263efab7dcSMark Brown 
627e0f22622SKuninori Morimoto 		if (cpu_dai->driver->bus_control)
628e0f22622SKuninori Morimoto 			snd_soc_dai_suspend(cpu_dai);
629988e8cc4SNicolin Chen 
630988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
631988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
632db2a4165SFrank Mandarino 	}
633db2a4165SFrank Mandarino 
63487506549SMark Brown 	if (card->suspend_post)
63570b2ac12SMark Brown 		card->suspend_post(card);
636db2a4165SFrank Mandarino 
637db2a4165SFrank Mandarino 	return 0;
638db2a4165SFrank Mandarino }
6396f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
640db2a4165SFrank Mandarino 
6412c7b696aSMarcel Ziswiler /*
6422c7b696aSMarcel Ziswiler  * deferred resume work, so resume can complete before we finished
6436ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6446ed25978SAndy Green  */
6456ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
646db2a4165SFrank Mandarino {
647f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
6482c7b696aSMarcel Ziswiler 			container_of(work, struct snd_soc_card,
6492c7b696aSMarcel Ziswiler 				     deferred_resume_work);
6501a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
651d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
6521a497983SMengdong Lin 	int i;
653db2a4165SFrank Mandarino 
6542c7b696aSMarcel Ziswiler 	/*
6552c7b696aSMarcel Ziswiler 	 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6566ed25978SAndy Green 	 * so userspace apps are blocked from touching us
6576ed25978SAndy Green 	 */
6586ed25978SAndy Green 
659f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
6606ed25978SAndy Green 
6619949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
662f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
6639949788bSMark Brown 
66487506549SMark Brown 	if (card->resume_pre)
66570b2ac12SMark Brown 		card->resume_pre(card);
666db2a4165SFrank Mandarino 
667bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
668bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6691a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6703efab7dcSMark Brown 
6711a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6723efab7dcSMark Brown 			continue;
6733efab7dcSMark Brown 
67424b09d05SKuninori Morimoto 		if (cpu_dai->driver->bus_control)
67524b09d05SKuninori Morimoto 			snd_soc_dai_resume(cpu_dai);
676db2a4165SFrank Mandarino 	}
677db2a4165SFrank Mandarino 
678f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
679e40fadbcSKuninori Morimoto 		if (snd_soc_component_is_suspended(component))
6809a840cbaSKuninori Morimoto 			snd_soc_component_resume(component);
6811547aba9SMark Brown 	}
682db2a4165SFrank Mandarino 
683bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6843efab7dcSMark Brown 
6851a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6863efab7dcSMark Brown 			continue;
6873efab7dcSMark Brown 
6881a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
689d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
690db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
691f0fba2adSLiam Girdwood 
6921a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
693d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
694db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
695db2a4165SFrank Mandarino 	}
696db2a4165SFrank Mandarino 
6973ff3f64bSMark Brown 	/* unmute any active DACs */
698bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6990b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
7003efab7dcSMark Brown 
7011a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7023efab7dcSMark Brown 			continue;
7033efab7dcSMark Brown 
7040b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
70588fdffa2SKuninori Morimoto 			if (dai->playback_active)
70688fdffa2SKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 0,
70788fdffa2SKuninori Morimoto 						SNDRV_PCM_STREAM_PLAYBACK);
708db2a4165SFrank Mandarino 		}
70988bd870fSBenoit Cousson 	}
710db2a4165SFrank Mandarino 
711bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7121a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7133efab7dcSMark Brown 
7141a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7153efab7dcSMark Brown 			continue;
7163efab7dcSMark Brown 
71724b09d05SKuninori Morimoto 		if (!cpu_dai->driver->bus_control)
71824b09d05SKuninori Morimoto 			snd_soc_dai_resume(cpu_dai);
719db2a4165SFrank Mandarino 	}
720db2a4165SFrank Mandarino 
72187506549SMark Brown 	if (card->resume_post)
72270b2ac12SMark Brown 		card->resume_post(card);
723db2a4165SFrank Mandarino 
724f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
7256ed25978SAndy Green 
7268be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
7278be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
728e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
7291a7aaa58SJeeja KP 
7301a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
7311a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
7326ed25978SAndy Green }
7336ed25978SAndy Green 
7346ed25978SAndy Green /* powers up audio subsystem after a suspend */
7356f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
7366ed25978SAndy Green {
7376f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
738bc263214SLars-Peter Clausen 	bool bus_control = false;
7391a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
74022d251a5SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
74122d251a5SKuninori Morimoto 	int i;
742b9dd94a8SPeter Ujfalusi 
743c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
744c5599b87SLars-Peter Clausen 	if (!card->instantiated)
7455ff1ddf2SEric Miao 		return 0;
7465ff1ddf2SEric Miao 
747988e8cc4SNicolin Chen 	/* activate pins from sleep state */
748bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
74988bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
75088bd870fSBenoit Cousson 
751988e8cc4SNicolin Chen 		if (cpu_dai->active)
752988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
75388bd870fSBenoit Cousson 
75422d251a5SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
755988e8cc4SNicolin Chen 			if (codec_dai->active)
756988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
757988e8cc4SNicolin Chen 		}
75888bd870fSBenoit Cousson 	}
759988e8cc4SNicolin Chen 
760bc263214SLars-Peter Clausen 	/*
761bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
762bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
763bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
76464ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
76564ab9baaSMark Brown 	 */
766bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7671a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7682c7b696aSMarcel Ziswiler 
769bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
77082e14e8bSStephen Warren 	}
771bc263214SLars-Peter Clausen 	if (bus_control) {
772bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
77364ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
77464ab9baaSMark Brown 	} else {
775f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
7766308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
777f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
778f0fba2adSLiam Girdwood 	}
7796ed25978SAndy Green 
780db2a4165SFrank Mandarino 	return 0;
781db2a4165SFrank Mandarino }
7826f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
783b3da4251SKuninori Morimoto 
784b3da4251SKuninori Morimoto static void soc_resume_init(struct snd_soc_card *card)
785b3da4251SKuninori Morimoto {
786b3da4251SKuninori Morimoto 	/* deferred resume work */
787b3da4251SKuninori Morimoto 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
788b3da4251SKuninori Morimoto }
789db2a4165SFrank Mandarino #else
7906f8ab4acSMark Brown #define snd_soc_suspend NULL
7916f8ab4acSMark Brown #define snd_soc_resume NULL
792b3da4251SKuninori Morimoto static inline void soc_resume_init(struct snd_soc_card *card)
793b3da4251SKuninori Morimoto {
794b3da4251SKuninori Morimoto }
795db2a4165SFrank Mandarino #endif
796db2a4165SFrank Mandarino 
79785e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
79802a06d30SBarry Song };
79902a06d30SBarry Song 
800c0834440SKuninori Morimoto static struct device_node
801c0834440SKuninori Morimoto *soc_component_to_node(struct snd_soc_component *component)
80212023a9aSMisael Lopez Cruz {
803c0834440SKuninori Morimoto 	struct device_node *of_node;
80412023a9aSMisael Lopez Cruz 
805c0834440SKuninori Morimoto 	of_node = component->dev->of_node;
806c0834440SKuninori Morimoto 	if (!of_node && component->dev->parent)
807c0834440SKuninori Morimoto 		of_node = component->dev->parent->of_node;
80834e81ab4SLars-Peter Clausen 
809c0834440SKuninori Morimoto 	return of_node;
81012023a9aSMisael Lopez Cruz }
81112023a9aSMisael Lopez Cruz 
812be6ac0a9SKuninori Morimoto static int snd_soc_is_matching_component(
813be6ac0a9SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc,
814be6ac0a9SKuninori Morimoto 	struct snd_soc_component *component)
815be6ac0a9SKuninori Morimoto {
816be6ac0a9SKuninori Morimoto 	struct device_node *component_of_node;
817be6ac0a9SKuninori Morimoto 
8187d7db5d3SKuninori Morimoto 	if (!dlc)
8197d7db5d3SKuninori Morimoto 		return 0;
8207d7db5d3SKuninori Morimoto 
8217d7db5d3SKuninori Morimoto 	component_of_node = soc_component_to_node(component);
822be6ac0a9SKuninori Morimoto 
823be6ac0a9SKuninori Morimoto 	if (dlc->of_node && component_of_node != dlc->of_node)
824be6ac0a9SKuninori Morimoto 		return 0;
825be6ac0a9SKuninori Morimoto 	if (dlc->name && strcmp(component->name, dlc->name))
826be6ac0a9SKuninori Morimoto 		return 0;
827be6ac0a9SKuninori Morimoto 
828be6ac0a9SKuninori Morimoto 	return 1;
829be6ac0a9SKuninori Morimoto }
830be6ac0a9SKuninori Morimoto 
831db2a4165SFrank Mandarino static struct snd_soc_component *soc_find_component(
832c1e230f0SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc)
833db2a4165SFrank Mandarino {
834db2a4165SFrank Mandarino 	struct snd_soc_component *component;
835db2a4165SFrank Mandarino 
836db2a4165SFrank Mandarino 	lockdep_assert_held(&client_mutex);
837db2a4165SFrank Mandarino 
838e3303268SKuninori Morimoto 	/*
839e3303268SKuninori Morimoto 	 * NOTE
840e3303268SKuninori Morimoto 	 *
841e3303268SKuninori Morimoto 	 * It returns *1st* found component, but some driver
842e3303268SKuninori Morimoto 	 * has few components by same of_node/name
843e3303268SKuninori Morimoto 	 * ex)
844e3303268SKuninori Morimoto 	 *	CPU component and generic DMAEngine component
845e3303268SKuninori Morimoto 	 */
846c1e230f0SKuninori Morimoto 	for_each_component(component)
847c1e230f0SKuninori Morimoto 		if (snd_soc_is_matching_component(dlc, component))
848db2a4165SFrank Mandarino 			return component;
849db2a4165SFrank Mandarino 
850db2a4165SFrank Mandarino 	return NULL;
85112023a9aSMisael Lopez Cruz }
85212023a9aSMisael Lopez Cruz 
853fbb88b5cSMengdong Lin /**
854fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
855fbb88b5cSMengdong Lin  *
8564958471bSJeffy Chen  * @dlc: name of the DAI or the DAI driver and optional component info to match
857fbb88b5cSMengdong Lin  *
858ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
859fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
860fbb88b5cSMengdong Lin  * should also match if being specified.
861fbb88b5cSMengdong Lin  *
862fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
863fbb88b5cSMengdong Lin  */
864305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
86514621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
86612023a9aSMisael Lopez Cruz {
86714621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
86814621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
86912023a9aSMisael Lopez Cruz 
87034e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
87134e81ab4SLars-Peter Clausen 
87214621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs */
873368dee94SKuninori Morimoto 	for_each_component(component) {
874be6ac0a9SKuninori Morimoto 		if (!snd_soc_is_matching_component(dlc, component))
87512023a9aSMisael Lopez Cruz 			continue;
87615a0c645SKuninori Morimoto 		for_each_component_dais(component, dai) {
8774958471bSJeffy Chen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
8786a6dafdaSJeffy Chen 			    && (!dai->driver->name
8796a6dafdaSJeffy Chen 				|| strcmp(dai->driver->name, dlc->dai_name)))
88014621c7eSLars-Peter Clausen 				continue;
88112023a9aSMisael Lopez Cruz 
88214621c7eSLars-Peter Clausen 			return dai;
88312023a9aSMisael Lopez Cruz 		}
88412023a9aSMisael Lopez Cruz 	}
88512023a9aSMisael Lopez Cruz 
88612023a9aSMisael Lopez Cruz 	return NULL;
88712023a9aSMisael Lopez Cruz }
888305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
88912023a9aSMisael Lopez Cruz 
89017fb1755SMengdong Lin /**
89117fb1755SMengdong Lin  * snd_soc_find_dai_link - Find a DAI link
89217fb1755SMengdong Lin  *
89317fb1755SMengdong Lin  * @card: soc card
89417fb1755SMengdong Lin  * @id: DAI link ID to match
89517fb1755SMengdong Lin  * @name: DAI link name to match, optional
8968abab35fSCharles Keepax  * @stream_name: DAI link stream name to match, optional
89717fb1755SMengdong Lin  *
89817fb1755SMengdong Lin  * This function will search all existing DAI links of the soc card to
89917fb1755SMengdong Lin  * find the link of the same ID. Since DAI links may not have their
90017fb1755SMengdong Lin  * unique ID, so name and stream name should also match if being
90117fb1755SMengdong Lin  * specified.
90217fb1755SMengdong Lin  *
90317fb1755SMengdong Lin  * Return: pointer of DAI link, or NULL if not found.
90417fb1755SMengdong Lin  */
90517fb1755SMengdong Lin struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
90617fb1755SMengdong Lin 					       int id, const char *name,
90717fb1755SMengdong Lin 					       const char *stream_name)
90817fb1755SMengdong Lin {
90942849064SKuninori Morimoto 	struct snd_soc_dai_link *link;
91017fb1755SMengdong Lin 
91117fb1755SMengdong Lin 	lockdep_assert_held(&client_mutex);
91217fb1755SMengdong Lin 
91342849064SKuninori Morimoto 	for_each_card_links(card, link) {
91417fb1755SMengdong Lin 		if (link->id != id)
91517fb1755SMengdong Lin 			continue;
91617fb1755SMengdong Lin 
91717fb1755SMengdong Lin 		if (name && (!link->name || strcmp(name, link->name)))
91817fb1755SMengdong Lin 			continue;
91917fb1755SMengdong Lin 
92017fb1755SMengdong Lin 		if (stream_name && (!link->stream_name
92117fb1755SMengdong Lin 			|| strcmp(stream_name, link->stream_name)))
92217fb1755SMengdong Lin 			continue;
92317fb1755SMengdong Lin 
92417fb1755SMengdong Lin 		return link;
92517fb1755SMengdong Lin 	}
92617fb1755SMengdong Lin 
92717fb1755SMengdong Lin 	return NULL;
92817fb1755SMengdong Lin }
92917fb1755SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
93017fb1755SMengdong Lin 
93149a5ba1cSMengdong Lin static bool soc_is_dai_link_bound(struct snd_soc_card *card,
93249a5ba1cSMengdong Lin 		struct snd_soc_dai_link *dai_link)
933db2a4165SFrank Mandarino {
93449a5ba1cSMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
93549a5ba1cSMengdong Lin 
936bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
93749a5ba1cSMengdong Lin 		if (rtd->dai_link == dai_link)
93849a5ba1cSMengdong Lin 			return true;
93949a5ba1cSMengdong Lin 	}
94049a5ba1cSMengdong Lin 
94149a5ba1cSMengdong Lin 	return false;
94249a5ba1cSMengdong Lin }
94349a5ba1cSMengdong Lin 
944bfce78a5SKuninori Morimoto static int soc_dai_link_sanity_check(struct snd_soc_card *card,
94536794902SKuninori Morimoto 				     struct snd_soc_dai_link *link)
94636794902SKuninori Morimoto {
94736794902SKuninori Morimoto 	int i;
94836794902SKuninori Morimoto 	struct snd_soc_dai_link_component *codec, *platform;
94936794902SKuninori Morimoto 
95036794902SKuninori Morimoto 	for_each_link_codecs(link, i, codec) {
95136794902SKuninori Morimoto 		/*
95236794902SKuninori Morimoto 		 * Codec must be specified by 1 of name or OF node,
95336794902SKuninori Morimoto 		 * not both or neither.
95436794902SKuninori Morimoto 		 */
95536794902SKuninori Morimoto 		if (!!codec->name == !!codec->of_node) {
95636794902SKuninori Morimoto 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
95736794902SKuninori Morimoto 				link->name);
95836794902SKuninori Morimoto 			return -EINVAL;
95936794902SKuninori Morimoto 		}
96036794902SKuninori Morimoto 
96136794902SKuninori Morimoto 		/* Codec DAI name must be specified */
96236794902SKuninori Morimoto 		if (!codec->dai_name) {
96336794902SKuninori Morimoto 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
96436794902SKuninori Morimoto 				link->name);
96536794902SKuninori Morimoto 			return -EINVAL;
96636794902SKuninori Morimoto 		}
96736794902SKuninori Morimoto 
96836794902SKuninori Morimoto 		/*
96936794902SKuninori Morimoto 		 * Defer card registration if codec component is not added to
97036794902SKuninori Morimoto 		 * component list.
97136794902SKuninori Morimoto 		 */
97236794902SKuninori Morimoto 		if (!soc_find_component(codec))
97336794902SKuninori Morimoto 			return -EPROBE_DEFER;
97436794902SKuninori Morimoto 	}
97536794902SKuninori Morimoto 
97636794902SKuninori Morimoto 	for_each_link_platforms(link, i, platform) {
97736794902SKuninori Morimoto 		/*
97836794902SKuninori Morimoto 		 * Platform may be specified by either name or OF node, but it
97936794902SKuninori Morimoto 		 * can be left unspecified, then no components will be inserted
98036794902SKuninori Morimoto 		 * in the rtdcom list
98136794902SKuninori Morimoto 		 */
98236794902SKuninori Morimoto 		if (!!platform->name == !!platform->of_node) {
98336794902SKuninori Morimoto 			dev_err(card->dev,
98436794902SKuninori Morimoto 				"ASoC: Neither/both platform name/of_node are set for %s\n",
98536794902SKuninori Morimoto 				link->name);
98636794902SKuninori Morimoto 			return -EINVAL;
98736794902SKuninori Morimoto 		}
98836794902SKuninori Morimoto 
98936794902SKuninori Morimoto 		/*
99036794902SKuninori Morimoto 		 * Defer card registration if platform component is not added to
99136794902SKuninori Morimoto 		 * component list.
99236794902SKuninori Morimoto 		 */
99336794902SKuninori Morimoto 		if (!soc_find_component(platform))
99436794902SKuninori Morimoto 			return -EPROBE_DEFER;
99536794902SKuninori Morimoto 	}
99636794902SKuninori Morimoto 
99736794902SKuninori Morimoto 	/* FIXME */
99836794902SKuninori Morimoto 	if (link->num_cpus > 1) {
99936794902SKuninori Morimoto 		dev_err(card->dev,
100036794902SKuninori Morimoto 			"ASoC: multi cpu is not yet supported %s\n",
100136794902SKuninori Morimoto 			link->name);
100236794902SKuninori Morimoto 		return -EINVAL;
100336794902SKuninori Morimoto 	}
100436794902SKuninori Morimoto 
100536794902SKuninori Morimoto 	/*
100636794902SKuninori Morimoto 	 * CPU device may be specified by either name or OF node, but
100736794902SKuninori Morimoto 	 * can be left unspecified, and will be matched based on DAI
100836794902SKuninori Morimoto 	 * name alone..
100936794902SKuninori Morimoto 	 */
101036794902SKuninori Morimoto 	if (link->cpus->name && link->cpus->of_node) {
101136794902SKuninori Morimoto 		dev_err(card->dev,
101236794902SKuninori Morimoto 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
101336794902SKuninori Morimoto 			link->name);
101436794902SKuninori Morimoto 		return -EINVAL;
101536794902SKuninori Morimoto 	}
101636794902SKuninori Morimoto 
101736794902SKuninori Morimoto 	/*
1018cd3c5ad7SKuninori Morimoto 	 * Defer card registration if cpu dai component is not added to
101936794902SKuninori Morimoto 	 * component list.
102036794902SKuninori Morimoto 	 */
102136794902SKuninori Morimoto 	if ((link->cpus->of_node || link->cpus->name) &&
102236794902SKuninori Morimoto 	    !soc_find_component(link->cpus))
102336794902SKuninori Morimoto 		return -EPROBE_DEFER;
102436794902SKuninori Morimoto 
102536794902SKuninori Morimoto 	/*
102636794902SKuninori Morimoto 	 * At least one of CPU DAI name or CPU device name/node must be
102736794902SKuninori Morimoto 	 * specified
102836794902SKuninori Morimoto 	 */
102936794902SKuninori Morimoto 	if (!link->cpus->dai_name &&
103036794902SKuninori Morimoto 	    !(link->cpus->name || link->cpus->of_node)) {
103136794902SKuninori Morimoto 		dev_err(card->dev,
103236794902SKuninori Morimoto 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
103336794902SKuninori Morimoto 			link->name);
103436794902SKuninori Morimoto 		return -EINVAL;
103536794902SKuninori Morimoto 	}
103636794902SKuninori Morimoto 
103736794902SKuninori Morimoto 	return 0;
103836794902SKuninori Morimoto }
103936794902SKuninori Morimoto 
10406f2f1ff0SMengdong Lin static int soc_bind_dai_link(struct snd_soc_card *card,
10416f2f1ff0SMengdong Lin 	struct snd_soc_dai_link *dai_link)
1042db2a4165SFrank Mandarino {
10431a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
104434614739SJerome Brunet 	struct snd_soc_dai_link_component *codec, *platform;
104590be711eSKuninori Morimoto 	struct snd_soc_component *component;
1046bfce78a5SKuninori Morimoto 	int i, ret;
1047db2a4165SFrank Mandarino 
1048a655de80SLiam Girdwood 	if (dai_link->ignore)
1049a655de80SLiam Girdwood 		return 0;
1050a655de80SLiam Girdwood 
10516f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
10526308419aSMark Brown 
105349a5ba1cSMengdong Lin 	if (soc_is_dai_link_bound(card, dai_link)) {
105449a5ba1cSMengdong Lin 		dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
105549a5ba1cSMengdong Lin 			dai_link->name);
105649a5ba1cSMengdong Lin 		return 0;
105749a5ba1cSMengdong Lin 	}
1058db2a4165SFrank Mandarino 
1059bfce78a5SKuninori Morimoto 	ret = soc_dai_link_sanity_check(card, dai_link);
1060bfce78a5SKuninori Morimoto 	if (ret < 0)
1061bfce78a5SKuninori Morimoto 		return ret;
1062bfce78a5SKuninori Morimoto 
1063513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
1064513cb311SSudip Mukherjee 	if (!rtd)
1065513cb311SSudip Mukherjee 		return -ENOMEM;
1066513cb311SSudip Mukherjee 
106708a5841eSKuninori Morimoto 	/* FIXME: we need multi CPU support in the future */
106808a5841eSKuninori Morimoto 	rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
1069b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
10706b490879SMartin Hundebøll 		dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
107108a5841eSKuninori Morimoto 			 dai_link->cpus->dai_name);
10721a497983SMengdong Lin 		goto _err_defer;
1073c5af3a2eSMark Brown 	}
107490be711eSKuninori Morimoto 	snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
1075c5af3a2eSMark Brown 
107688bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
1077e2b30edfSKuninori Morimoto 	rtd->num_codecs = dai_link->num_codecs;
107834614739SJerome Brunet 	for_each_link_codecs(dai_link, i, codec) {
107934614739SJerome Brunet 		rtd->codec_dais[i] = snd_soc_find_dai(codec);
10800a2cfcd9SKuninori Morimoto 		if (!rtd->codec_dais[i]) {
10817c7e2d6aSStefan Agner 			dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
108234614739SJerome Brunet 				 codec->dai_name);
10831a497983SMengdong Lin 			goto _err_defer;
108412023a9aSMisael Lopez Cruz 		}
108534614739SJerome Brunet 
10860a2cfcd9SKuninori Morimoto 		snd_soc_rtdcom_add(rtd, rtd->codec_dais[i]->component);
108788bd870fSBenoit Cousson 	}
108888bd870fSBenoit Cousson 
108988bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
10900a2cfcd9SKuninori Morimoto 	rtd->codec_dai = rtd->codec_dais[0];
109112023a9aSMisael Lopez Cruz 
1092e2b30edfSKuninori Morimoto 	/* Find PLATFORM from registered PLATFORMs */
109334614739SJerome Brunet 	for_each_link_platforms(dai_link, i, platform) {
1094368dee94SKuninori Morimoto 		for_each_component(component) {
109534614739SJerome Brunet 			if (!snd_soc_is_matching_component(platform, component))
109690be711eSKuninori Morimoto 				continue;
109790be711eSKuninori Morimoto 
109890be711eSKuninori Morimoto 			snd_soc_rtdcom_add(rtd, component);
109990be711eSKuninori Morimoto 		}
110034614739SJerome Brunet 	}
110190be711eSKuninori Morimoto 
1102b19e6e7bSMark Brown 	return 0;
11031a497983SMengdong Lin 
11041a497983SMengdong Lin _err_defer:
11051a497983SMengdong Lin 	soc_free_pcm_runtime(rtd);
11061a497983SMengdong Lin 	return -EPROBE_DEFER;
11076b05eda6SMark Brown }
11086b05eda6SMark Brown 
1109ffd60fbaSKuninori Morimoto static void soc_set_of_name_prefix(struct snd_soc_component *component)
1110ffd60fbaSKuninori Morimoto {
1111ffd60fbaSKuninori Morimoto 	struct device_node *of_node = soc_component_to_node(component);
1112ffd60fbaSKuninori Morimoto 	const char *str;
1113ffd60fbaSKuninori Morimoto 	int ret;
1114ffd60fbaSKuninori Morimoto 
1115ffd60fbaSKuninori Morimoto 	ret = of_property_read_string(of_node, "sound-name-prefix", &str);
1116ffd60fbaSKuninori Morimoto 	if (!ret)
1117ffd60fbaSKuninori Morimoto 		component->name_prefix = str;
1118ffd60fbaSKuninori Morimoto }
1119ffd60fbaSKuninori Morimoto 
1120ffd60fbaSKuninori Morimoto static void soc_set_name_prefix(struct snd_soc_card *card,
1121ffd60fbaSKuninori Morimoto 				struct snd_soc_component *component)
1122ffd60fbaSKuninori Morimoto {
1123ffd60fbaSKuninori Morimoto 	int i;
1124ffd60fbaSKuninori Morimoto 
1125ffd60fbaSKuninori Morimoto 	for (i = 0; i < card->num_configs && card->codec_conf; i++) {
1126ffd60fbaSKuninori Morimoto 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1127ffd60fbaSKuninori Morimoto 		struct device_node *of_node = soc_component_to_node(component);
1128ffd60fbaSKuninori Morimoto 
1129ffd60fbaSKuninori Morimoto 		if (map->of_node && of_node != map->of_node)
1130ffd60fbaSKuninori Morimoto 			continue;
1131ffd60fbaSKuninori Morimoto 		if (map->dev_name && strcmp(component->name, map->dev_name))
1132ffd60fbaSKuninori Morimoto 			continue;
1133ffd60fbaSKuninori Morimoto 		component->name_prefix = map->name_prefix;
1134ffd60fbaSKuninori Morimoto 		return;
1135ffd60fbaSKuninori Morimoto 	}
1136ffd60fbaSKuninori Morimoto 
1137ffd60fbaSKuninori Morimoto 	/*
1138ffd60fbaSKuninori Morimoto 	 * If there is no configuration table or no match in the table,
1139ffd60fbaSKuninori Morimoto 	 * check if a prefix is provided in the node
1140ffd60fbaSKuninori Morimoto 	 */
1141ffd60fbaSKuninori Morimoto 	soc_set_of_name_prefix(component);
1142ffd60fbaSKuninori Morimoto }
1143ffd60fbaSKuninori Morimoto 
114422d14231SKuninori Morimoto static void soc_cleanup_component(struct snd_soc_component *component)
114522d14231SKuninori Morimoto {
114604f770d9SKuninori Morimoto 	/* For framework level robustness */
11473bb936f5SAmadeusz Sławiński 	snd_soc_component_set_jack(component, NULL, NULL);
114804f770d9SKuninori Morimoto 
11497a5d9815SBard liao 	list_del_init(&component->card_list);
115022d14231SKuninori Morimoto 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
115122d14231SKuninori Morimoto 	soc_cleanup_component_debugfs(component);
115222d14231SKuninori Morimoto 	component->card = NULL;
11530e36f36bSPierre-Louis Bossart 	snd_soc_component_module_put_when_remove(component);
115422d14231SKuninori Morimoto }
115522d14231SKuninori Morimoto 
1156f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
1157d12cd198SStephen Warren {
1158abd31b32SLars-Peter Clausen 	if (!component->card)
115970090bbbSLars-Peter Clausen 		return;
1160d12cd198SStephen Warren 
116103b34dd7SKuninori Morimoto 	snd_soc_component_remove(component);
11627a5d9815SBard liao 
116322d14231SKuninori Morimoto 	soc_cleanup_component(component);
1164d12cd198SStephen Warren }
1165d12cd198SStephen Warren 
1166ffd60fbaSKuninori Morimoto static int soc_probe_component(struct snd_soc_card *card,
1167ffd60fbaSKuninori Morimoto 			       struct snd_soc_component *component)
1168ffd60fbaSKuninori Morimoto {
1169ffd60fbaSKuninori Morimoto 	struct snd_soc_dapm_context *dapm =
1170ffd60fbaSKuninori Morimoto 		snd_soc_component_get_dapm(component);
1171ffd60fbaSKuninori Morimoto 	struct snd_soc_dai *dai;
1172ffd60fbaSKuninori Morimoto 	int ret;
1173ffd60fbaSKuninori Morimoto 
1174ffd60fbaSKuninori Morimoto 	if (!strcmp(component->name, "snd-soc-dummy"))
1175ffd60fbaSKuninori Morimoto 		return 0;
1176ffd60fbaSKuninori Morimoto 
1177ffd60fbaSKuninori Morimoto 	if (component->card) {
1178ffd60fbaSKuninori Morimoto 		if (component->card != card) {
1179ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1180ffd60fbaSKuninori Morimoto 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1181ffd60fbaSKuninori Morimoto 				card->name, component->card->name);
1182ffd60fbaSKuninori Morimoto 			return -ENODEV;
1183ffd60fbaSKuninori Morimoto 		}
1184ffd60fbaSKuninori Morimoto 		return 0;
1185ffd60fbaSKuninori Morimoto 	}
1186ffd60fbaSKuninori Morimoto 
1187ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_module_get_when_probe(component);
1188ffd60fbaSKuninori Morimoto 	if (ret < 0)
1189ffd60fbaSKuninori Morimoto 		return ret;
1190ffd60fbaSKuninori Morimoto 
1191ffd60fbaSKuninori Morimoto 	component->card = card;
1192ffd60fbaSKuninori Morimoto 	soc_set_name_prefix(card, component);
1193ffd60fbaSKuninori Morimoto 
1194ffd60fbaSKuninori Morimoto 	soc_init_component_debugfs(component);
1195ffd60fbaSKuninori Morimoto 
119695c267ddSKuninori Morimoto 	snd_soc_dapm_init(dapm, card, component);
1197b614beafSKuninori Morimoto 
1198ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_new_controls(dapm,
1199ffd60fbaSKuninori Morimoto 					component->driver->dapm_widgets,
1200ffd60fbaSKuninori Morimoto 					component->driver->num_dapm_widgets);
1201ffd60fbaSKuninori Morimoto 
1202ffd60fbaSKuninori Morimoto 	if (ret != 0) {
1203ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1204ffd60fbaSKuninori Morimoto 			"Failed to create new controls %d\n", ret);
1205ffd60fbaSKuninori Morimoto 		goto err_probe;
1206ffd60fbaSKuninori Morimoto 	}
1207ffd60fbaSKuninori Morimoto 
1208ffd60fbaSKuninori Morimoto 	for_each_component_dais(component, dai) {
1209ffd60fbaSKuninori Morimoto 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1210ffd60fbaSKuninori Morimoto 		if (ret != 0) {
1211ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1212ffd60fbaSKuninori Morimoto 				"Failed to create DAI widgets %d\n", ret);
1213ffd60fbaSKuninori Morimoto 			goto err_probe;
1214ffd60fbaSKuninori Morimoto 		}
1215ffd60fbaSKuninori Morimoto 	}
1216ffd60fbaSKuninori Morimoto 
1217ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_probe(component);
1218ffd60fbaSKuninori Morimoto 	if (ret < 0) {
1219ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1220ffd60fbaSKuninori Morimoto 			"ASoC: failed to probe component %d\n", ret);
1221ffd60fbaSKuninori Morimoto 		goto err_probe;
1222ffd60fbaSKuninori Morimoto 	}
1223ffd60fbaSKuninori Morimoto 	WARN(dapm->idle_bias_off &&
1224ffd60fbaSKuninori Morimoto 	     dapm->bias_level != SND_SOC_BIAS_OFF,
1225ffd60fbaSKuninori Morimoto 	     "codec %s can not start from non-off bias with idle_bias_off==1\n",
1226ffd60fbaSKuninori Morimoto 	     component->name);
1227ffd60fbaSKuninori Morimoto 
1228ffd60fbaSKuninori Morimoto 	/* machine specific init */
1229ffd60fbaSKuninori Morimoto 	if (component->init) {
1230ffd60fbaSKuninori Morimoto 		ret = component->init(component);
1231ffd60fbaSKuninori Morimoto 		if (ret < 0) {
1232ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1233ffd60fbaSKuninori Morimoto 				"Failed to do machine specific init %d\n", ret);
1234ffd60fbaSKuninori Morimoto 			goto err_probe;
1235ffd60fbaSKuninori Morimoto 		}
1236ffd60fbaSKuninori Morimoto 	}
1237ffd60fbaSKuninori Morimoto 
1238ffd60fbaSKuninori Morimoto 	ret = snd_soc_add_component_controls(component,
1239ffd60fbaSKuninori Morimoto 					     component->driver->controls,
1240ffd60fbaSKuninori Morimoto 					     component->driver->num_controls);
1241ffd60fbaSKuninori Morimoto 	if (ret < 0)
1242ffd60fbaSKuninori Morimoto 		goto err_probe;
1243ffd60fbaSKuninori Morimoto 
1244ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(dapm,
1245ffd60fbaSKuninori Morimoto 				      component->driver->dapm_routes,
1246ffd60fbaSKuninori Morimoto 				      component->driver->num_dapm_routes);
1247ffd60fbaSKuninori Morimoto 	if (ret < 0)
1248ffd60fbaSKuninori Morimoto 		goto err_probe;
1249ffd60fbaSKuninori Morimoto 
1250ffd60fbaSKuninori Morimoto 	/* see for_each_card_components */
1251ffd60fbaSKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1252ffd60fbaSKuninori Morimoto 
1253ffd60fbaSKuninori Morimoto err_probe:
1254ffd60fbaSKuninori Morimoto 	if (ret < 0)
1255ffd60fbaSKuninori Morimoto 		soc_cleanup_component(component);
1256ffd60fbaSKuninori Morimoto 
1257ffd60fbaSKuninori Morimoto 	return ret;
1258ffd60fbaSKuninori Morimoto }
1259ffd60fbaSKuninori Morimoto 
1260e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1261589c3563SJarkko Nikula {
1262589c3563SJarkko Nikula 	int err;
1263589c3563SJarkko Nikula 
126452abe6ccSGuennadi Liakhovetski 	if (!dai || !dai->probed || !dai->driver ||
12652eda3cb1SKuninori Morimoto 	    dai->driver->remove_order != order)
12662eda3cb1SKuninori Morimoto 		return;
12672eda3cb1SKuninori Morimoto 
1268dcdab582SKuninori Morimoto 	err = snd_soc_dai_remove(dai);
1269589c3563SJarkko Nikula 	if (err < 0)
1270e60cd14fSLars-Peter Clausen 		dev_err(dai->dev,
1271b0aa88afSMisael Lopez Cruz 			"ASoC: failed to remove %s: %d\n",
1272e60cd14fSLars-Peter Clausen 			dai->name, err);
1273dcdab582SKuninori Morimoto 
1274e60cd14fSLars-Peter Clausen 	dai->probed = 0;
1275b0aa88afSMisael Lopez Cruz }
1276b0aa88afSMisael Lopez Cruz 
1277a7d44f78SKuninori Morimoto static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1278a7d44f78SKuninori Morimoto {
1279a7d44f78SKuninori Morimoto 	int ret;
1280a7d44f78SKuninori Morimoto 
1281a7d44f78SKuninori Morimoto 	if (dai->probed ||
1282a7d44f78SKuninori Morimoto 	    dai->driver->probe_order != order)
1283a7d44f78SKuninori Morimoto 		return 0;
1284a7d44f78SKuninori Morimoto 
1285a7d44f78SKuninori Morimoto 	ret = snd_soc_dai_probe(dai);
1286a7d44f78SKuninori Morimoto 	if (ret < 0) {
1287a7d44f78SKuninori Morimoto 		dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1288a7d44f78SKuninori Morimoto 			dai->name, ret);
1289a7d44f78SKuninori Morimoto 		return ret;
1290a7d44f78SKuninori Morimoto 	}
1291a7d44f78SKuninori Morimoto 
1292a7d44f78SKuninori Morimoto 	dai->probed = 1;
1293a7d44f78SKuninori Morimoto 
1294a7d44f78SKuninori Morimoto 	return 0;
1295a7d44f78SKuninori Morimoto }
1296a7d44f78SKuninori Morimoto 
12974ca47d21SKuninori Morimoto static void soc_remove_link_dais(struct snd_soc_card *card)
1298f0fba2adSLiam Girdwood {
1299e60cd14fSLars-Peter Clausen 	int i;
13000b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
13014ca47d21SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
13024ca47d21SKuninori Morimoto 	int order;
13034ca47d21SKuninori Morimoto 
13044ca47d21SKuninori Morimoto 	for_each_comp_order(order) {
13054ca47d21SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1306f0fba2adSLiam Girdwood 			/* remove the CODEC DAI */
13070b7990e3SKuninori Morimoto 			for_each_rtd_codec_dai(rtd, i, codec_dai)
13080b7990e3SKuninori Morimoto 				soc_remove_dai(codec_dai, order);
1309f0fba2adSLiam Girdwood 
1310e60cd14fSLars-Peter Clausen 			soc_remove_dai(rtd->cpu_dai, order);
1311f0fba2adSLiam Girdwood 		}
13124ca47d21SKuninori Morimoto 	}
13134ca47d21SKuninori Morimoto }
1314f0fba2adSLiam Girdwood 
1315bc7c16c2SKuninori Morimoto static int soc_probe_link_dais(struct snd_soc_card *card)
1316bc7c16c2SKuninori Morimoto {
1317bc7c16c2SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1318bc7c16c2SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
1319bc7c16c2SKuninori Morimoto 	int i, order, ret;
1320bc7c16c2SKuninori Morimoto 
1321bc7c16c2SKuninori Morimoto 	for_each_comp_order(order) {
1322bc7c16c2SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1323bc7c16c2SKuninori Morimoto 
1324bc7c16c2SKuninori Morimoto 			dev_dbg(card->dev,
1325bc7c16c2SKuninori Morimoto 				"ASoC: probe %s dai link %d late %d\n",
1326bc7c16c2SKuninori Morimoto 				card->name, rtd->num, order);
1327bc7c16c2SKuninori Morimoto 
1328bc7c16c2SKuninori Morimoto 			ret = soc_probe_dai(rtd->cpu_dai, order);
1329bc7c16c2SKuninori Morimoto 			if (ret)
1330bc7c16c2SKuninori Morimoto 				return ret;
1331bc7c16c2SKuninori Morimoto 
1332bc7c16c2SKuninori Morimoto 			/* probe the CODEC DAI */
1333bc7c16c2SKuninori Morimoto 			for_each_rtd_codec_dai(rtd, i, codec_dai) {
1334bc7c16c2SKuninori Morimoto 				ret = soc_probe_dai(codec_dai, order);
1335bc7c16c2SKuninori Morimoto 				if (ret)
1336bc7c16c2SKuninori Morimoto 					return ret;
1337bc7c16c2SKuninori Morimoto 			}
1338bc7c16c2SKuninori Morimoto 		}
1339bc7c16c2SKuninori Morimoto 	}
1340bc7c16c2SKuninori Morimoto 
1341bc7c16c2SKuninori Morimoto 	return 0;
1342bc7c16c2SKuninori Morimoto }
1343bc7c16c2SKuninori Morimoto 
1344b006c0c6SKuninori Morimoto static void soc_remove_link_components(struct snd_soc_card *card)
134562ae68faSStephen Warren {
134661aca564SLars-Peter Clausen 	struct snd_soc_component *component;
1347b006c0c6SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
134890be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
1349b006c0c6SKuninori Morimoto 	int order;
135062ae68faSStephen Warren 
1351b006c0c6SKuninori Morimoto 	for_each_comp_order(order) {
1352b006c0c6SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
13532b544dd7SKuninori Morimoto 			for_each_rtd_components(rtd, rtdcom, component) {
1354b006c0c6SKuninori Morimoto 				if (component->driver->remove_order != order)
1355b006c0c6SKuninori Morimoto 					continue;
1356b006c0c6SKuninori Morimoto 
135761aca564SLars-Peter Clausen 				soc_remove_component(component);
135862ae68faSStephen Warren 			}
135962ae68faSStephen Warren 		}
1360b006c0c6SKuninori Morimoto 	}
1361b006c0c6SKuninori Morimoto }
136262ae68faSStephen Warren 
136362f07a6bSKuninori Morimoto static int soc_probe_link_components(struct snd_soc_card *card)
13646fb03550SKuninori Morimoto {
13656fb03550SKuninori Morimoto 	struct snd_soc_component *component;
136662f07a6bSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
13676fb03550SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
136862f07a6bSKuninori Morimoto 	int ret, order;
13696fb03550SKuninori Morimoto 
137062f07a6bSKuninori Morimoto 	for_each_comp_order(order) {
137162f07a6bSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
13722b544dd7SKuninori Morimoto 			for_each_rtd_components(rtd, rtdcom, component) {
137362f07a6bSKuninori Morimoto 				if (component->driver->probe_order != order)
137462f07a6bSKuninori Morimoto 					continue;
137562f07a6bSKuninori Morimoto 
13766fb03550SKuninori Morimoto 				ret = soc_probe_component(card, component);
13776fb03550SKuninori Morimoto 				if (ret < 0)
13786fb03550SKuninori Morimoto 					return ret;
13796fb03550SKuninori Morimoto 			}
13806fb03550SKuninori Morimoto 		}
138162f07a6bSKuninori Morimoto 	}
13826fb03550SKuninori Morimoto 
13836fb03550SKuninori Morimoto 	return 0;
13846fb03550SKuninori Morimoto }
13856fb03550SKuninori Morimoto 
1386ef2e8175SKuninori Morimoto void snd_soc_disconnect_sync(struct device *dev)
1387ef2e8175SKuninori Morimoto {
13882c7b696aSMarcel Ziswiler 	struct snd_soc_component *component =
13892c7b696aSMarcel Ziswiler 			snd_soc_lookup_component(dev, NULL);
1390ef2e8175SKuninori Morimoto 
1391ef2e8175SKuninori Morimoto 	if (!component || !component->card)
1392ef2e8175SKuninori Morimoto 		return;
1393ef2e8175SKuninori Morimoto 
1394ef2e8175SKuninori Morimoto 	snd_card_disconnect_sync(component->card->snd_card);
1395ef2e8175SKuninori Morimoto }
1396df532185SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
1397ef2e8175SKuninori Morimoto 
1398f8f80361SMengdong Lin /**
1399f8f80361SMengdong Lin  * snd_soc_add_dai_link - Add a DAI link dynamically
1400f8f80361SMengdong Lin  * @card: The ASoC card to which the DAI link is added
1401f8f80361SMengdong Lin  * @dai_link: The new DAI link to add
1402f8f80361SMengdong Lin  *
1403f8f80361SMengdong Lin  * This function adds a DAI link to the ASoC card's link list.
1404f8f80361SMengdong Lin  *
1405f8f80361SMengdong Lin  * Note: Topology can use this API to add DAI links when probing the
1406f8f80361SMengdong Lin  * topology component. And machine drivers can still define static
1407f8f80361SMengdong Lin  * DAI links in dai_link array.
1408f8f80361SMengdong Lin  */
1409f8f80361SMengdong Lin int snd_soc_add_dai_link(struct snd_soc_card *card,
1410f8f80361SMengdong Lin 		struct snd_soc_dai_link *dai_link)
1411f8f80361SMengdong Lin {
1412*6b1dff02SKuninori Morimoto 	int ret;
1413*6b1dff02SKuninori Morimoto 
1414f8f80361SMengdong Lin 	if (dai_link->dobj.type
1415f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1416f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1417f8f80361SMengdong Lin 			dai_link->dobj.type);
1418f8f80361SMengdong Lin 		return -EINVAL;
1419f8f80361SMengdong Lin 	}
1420f8f80361SMengdong Lin 
1421f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
14222c7b696aSMarcel Ziswiler 	/*
14232c7b696aSMarcel Ziswiler 	 * Notify the machine driver for extra initialization
1424d6f220eaSMengdong Lin 	 * on the link created by topology.
1425d6f220eaSMengdong Lin 	 */
1426d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->add_dai_link)
1427d6f220eaSMengdong Lin 		card->add_dai_link(card, dai_link);
1428d6f220eaSMengdong Lin 
1429*6b1dff02SKuninori Morimoto 	ret = soc_bind_dai_link(card, dai_link);
1430*6b1dff02SKuninori Morimoto 	if (ret < 0)
1431*6b1dff02SKuninori Morimoto 		return ret;
1432*6b1dff02SKuninori Morimoto 
14336634e3d6SKuninori Morimoto 	/* see for_each_card_links */
1434f8f80361SMengdong Lin 	list_add_tail(&dai_link->list, &card->dai_link_list);
1435f8f80361SMengdong Lin 
1436f8f80361SMengdong Lin 	return 0;
1437f8f80361SMengdong Lin }
1438f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1439f8f80361SMengdong Lin 
1440f8f80361SMengdong Lin /**
1441f8f80361SMengdong Lin  * snd_soc_remove_dai_link - Remove a DAI link from the list
1442f8f80361SMengdong Lin  * @card: The ASoC card that owns the link
1443f8f80361SMengdong Lin  * @dai_link: The DAI link to remove
1444f8f80361SMengdong Lin  *
1445f8f80361SMengdong Lin  * This function removes a DAI link from the ASoC card's link list.
1446f8f80361SMengdong Lin  *
1447f8f80361SMengdong Lin  * For DAI links previously added by topology, topology should
1448f8f80361SMengdong Lin  * remove them by using the dobj embedded in the link.
1449f8f80361SMengdong Lin  */
1450f8f80361SMengdong Lin void snd_soc_remove_dai_link(struct snd_soc_card *card,
1451f8f80361SMengdong Lin 			     struct snd_soc_dai_link *dai_link)
1452f8f80361SMengdong Lin {
1453f8f80361SMengdong Lin 	if (dai_link->dobj.type
1454f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1455f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1456f8f80361SMengdong Lin 			dai_link->dobj.type);
1457f8f80361SMengdong Lin 		return;
1458f8f80361SMengdong Lin 	}
1459f8f80361SMengdong Lin 
1460f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
14612c7b696aSMarcel Ziswiler 	/*
14622c7b696aSMarcel Ziswiler 	 * Notify the machine driver for extra destruction
1463d6f220eaSMengdong Lin 	 * on the link created by topology.
1464d6f220eaSMengdong Lin 	 */
1465d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->remove_dai_link)
1466d6f220eaSMengdong Lin 		card->remove_dai_link(card, dai_link);
1467d6f220eaSMengdong Lin 
1468c26a8841SKuninori Morimoto 	list_del(&dai_link->list);
1469f8f80361SMengdong Lin }
1470f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
14710671fd8eSKuninori Morimoto 
147225f7b701SArnaud Pouliquen static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
147325f7b701SArnaud Pouliquen 				struct snd_soc_pcm_runtime *rtd)
147425f7b701SArnaud Pouliquen {
147525f7b701SArnaud Pouliquen 	int i, ret = 0;
147625f7b701SArnaud Pouliquen 
147725f7b701SArnaud Pouliquen 	for (i = 0; i < num_dais; ++i) {
147825f7b701SArnaud Pouliquen 		struct snd_soc_dai_driver *drv = dais[i]->driver;
147925f7b701SArnaud Pouliquen 
1480de17f14eSRohit kumar 		if (drv->pcm_new)
148125f7b701SArnaud Pouliquen 			ret = drv->pcm_new(rtd, dais[i]);
148225f7b701SArnaud Pouliquen 		if (ret < 0) {
148325f7b701SArnaud Pouliquen 			dev_err(dais[i]->dev,
148425f7b701SArnaud Pouliquen 				"ASoC: Failed to bind %s with pcm device\n",
148525f7b701SArnaud Pouliquen 				dais[i]->name);
148625f7b701SArnaud Pouliquen 			return ret;
148725f7b701SArnaud Pouliquen 		}
148825f7b701SArnaud Pouliquen 	}
148925f7b701SArnaud Pouliquen 
149025f7b701SArnaud Pouliquen 	return 0;
149125f7b701SArnaud Pouliquen }
149225f7b701SArnaud Pouliquen 
1493c4b46982SKuninori Morimoto static int soc_link_init(struct snd_soc_card *card,
1494c4b46982SKuninori Morimoto 			 struct snd_soc_pcm_runtime *rtd)
1495c4b46982SKuninori Morimoto {
1496c4b46982SKuninori Morimoto 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1497c4b46982SKuninori Morimoto 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1498c4b46982SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
1499c4b46982SKuninori Morimoto 	struct snd_soc_component *component;
1500c4b46982SKuninori Morimoto 	int ret, num;
1501c4b46982SKuninori Morimoto 
1502c4b46982SKuninori Morimoto 	/* set default power off timeout */
1503c4b46982SKuninori Morimoto 	rtd->pmdown_time = pmdown_time;
15040168bf0dSLiam Girdwood 
15055f3484acSLars-Peter Clausen 	/* do machine specific initialization */
15065f3484acSLars-Peter Clausen 	if (dai_link->init) {
15075f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
15085f3484acSLars-Peter Clausen 		if (ret < 0) {
15095f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
15105f3484acSLars-Peter Clausen 				dai_link->name, ret);
15115f3484acSLars-Peter Clausen 			return ret;
15125f3484acSLars-Peter Clausen 		}
15135f3484acSLars-Peter Clausen 	}
15145f3484acSLars-Peter Clausen 
151540aa5383SRicard Wanderlof 	if (dai_link->dai_fmt) {
151640aa5383SRicard Wanderlof 		ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
151740aa5383SRicard Wanderlof 		if (ret)
151840aa5383SRicard Wanderlof 			return ret;
151940aa5383SRicard Wanderlof 	}
1520a5053a8eSKuninori Morimoto 
15215f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
15222e55b90aSLars-Peter Clausen 	soc_dpcm_debugfs_add(rtd);
15235f3484acSLars-Peter Clausen 
1524a655de80SLiam Girdwood 	num = rtd->num;
1525a655de80SLiam Girdwood 
1526a655de80SLiam Girdwood 	/*
1527a655de80SLiam Girdwood 	 * most drivers will register their PCMs using DAI link ordering but
1528a655de80SLiam Girdwood 	 * topology based drivers can use the DAI link id field to set PCM
1529a655de80SLiam Girdwood 	 * device number and then use rtd + a base offset of the BEs.
1530a655de80SLiam Girdwood 	 */
15312b544dd7SKuninori Morimoto 	for_each_rtd_components(rtd, rtdcom, component) {
1532a655de80SLiam Girdwood 		if (!component->driver->use_dai_pcm_id)
1533a655de80SLiam Girdwood 			continue;
1534a655de80SLiam Girdwood 
1535a655de80SLiam Girdwood 		if (rtd->dai_link->no_pcm)
1536a655de80SLiam Girdwood 			num += component->driver->be_pcm_base;
1537a655de80SLiam Girdwood 		else
1538a655de80SLiam Girdwood 			num = rtd->dai_link->id;
1539a655de80SLiam Girdwood 	}
1540a655de80SLiam Girdwood 
1541b423c420SKuninori Morimoto 	/* create compress_device if possible */
1542b423c420SKuninori Morimoto 	ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1543b423c420SKuninori Morimoto 	if (ret != -ENOTSUPP) {
1544b423c420SKuninori Morimoto 		if (ret < 0)
1545f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
15461245b700SNamarta Kohli 					 dai_link->stream_name);
15471245b700SNamarta Kohli 		return ret;
15481245b700SNamarta Kohli 	}
1549b423c420SKuninori Morimoto 
1550f0fba2adSLiam Girdwood 	/* create the pcm */
1551a655de80SLiam Girdwood 	ret = soc_new_pcm(rtd, num);
1552f0fba2adSLiam Girdwood 	if (ret < 0) {
1553f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
15540837fc62SFabio Estevam 			dai_link->stream_name, ret);
1555f0fba2adSLiam Girdwood 		return ret;
1556f0fba2adSLiam Girdwood 	}
155725f7b701SArnaud Pouliquen 	ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
155825f7b701SArnaud Pouliquen 	if (ret < 0)
155925f7b701SArnaud Pouliquen 		return ret;
156025f7b701SArnaud Pouliquen 	ret = soc_link_dai_pcm_new(rtd->codec_dais,
156125f7b701SArnaud Pouliquen 				   rtd->num_codecs, rtd);
156225f7b701SArnaud Pouliquen 	return ret;
1563f0fba2adSLiam Girdwood }
1564f0fba2adSLiam Girdwood 
1565e8fbd250SKuninori Morimoto static void soc_unbind_aux_dev(struct snd_soc_card *card)
15664893a2ebSKuninori Morimoto {
1567e8fbd250SKuninori Morimoto 	struct snd_soc_component *component, *_component;
1568e8fbd250SKuninori Morimoto 
1569e8fbd250SKuninori Morimoto 	for_each_card_auxs_safe(card, component, _component) {
15704893a2ebSKuninori Morimoto 		component->init = NULL;
15714893a2ebSKuninori Morimoto 		list_del(&component->card_aux_list);
15724893a2ebSKuninori Morimoto 	}
1573e8fbd250SKuninori Morimoto }
15744893a2ebSKuninori Morimoto 
1575bee886f1SKuninori Morimoto static int soc_bind_aux_dev(struct snd_soc_card *card)
1576b19e6e7bSMark Brown {
1577f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1578bee886f1SKuninori Morimoto 	struct snd_soc_aux_dev *aux;
1579bee886f1SKuninori Morimoto 	int i;
15803ca041edSSebastian Reichel 
1581bee886f1SKuninori Morimoto 	for_each_card_pre_auxs(card, i, aux) {
1582f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1583bee886f1SKuninori Morimoto 		component = soc_find_component(&aux->dlc);
1584f2ed6b07SMengdong Lin 		if (!component)
15853dc29b8bSKuninori Morimoto 			return -EPROBE_DEFER;
15863ca041edSSebastian Reichel 
1587bee886f1SKuninori Morimoto 		component->init = aux->init;
1588c2b71c71SKuninori Morimoto 		/* see for_each_card_auxs */
1589d2e3a135SSylwester Nawrocki 		list_add(&component->card_aux_list, &card->aux_comp_list);
1590bee886f1SKuninori Morimoto 	}
1591f2ed6b07SMengdong Lin 	return 0;
1592b19e6e7bSMark Brown }
1593b19e6e7bSMark Brown 
1594f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1595f2ed6b07SMengdong Lin {
1596991454e1SKuninori Morimoto 	struct snd_soc_component *comp;
1597f2ed6b07SMengdong Lin 	int order;
1598f2ed6b07SMengdong Lin 	int ret;
1599f2ed6b07SMengdong Lin 
16001a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1601c2b71c71SKuninori Morimoto 		for_each_card_auxs(card, comp) {
1602f2ed6b07SMengdong Lin 			if (comp->driver->probe_order == order) {
1603f2ed6b07SMengdong Lin 				ret = soc_probe_component(card,	comp);
1604f2ed6b07SMengdong Lin 				if (ret < 0) {
1605f2ed6b07SMengdong Lin 					dev_err(card->dev,
1606f2ed6b07SMengdong Lin 						"ASoC: failed to probe aux component %s %d\n",
1607f2ed6b07SMengdong Lin 						comp->name, ret);
1608f2ed6b07SMengdong Lin 					return ret;
1609f2ed6b07SMengdong Lin 				}
1610f2ed6b07SMengdong Lin 			}
1611f2ed6b07SMengdong Lin 		}
1612f2ed6b07SMengdong Lin 	}
161365d9361fSLars-Peter Clausen 
161444c69bb1SLars-Peter Clausen 	return 0;
16153ca041edSSebastian Reichel }
16162eea392dSJarkko Nikula 
1617f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
161844c69bb1SLars-Peter Clausen {
1619f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1620f2ed6b07SMengdong Lin 	int order;
162144c69bb1SLars-Peter Clausen 
16221a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1623c2b71c71SKuninori Morimoto 		for_each_card_auxs_safe(card, comp, _comp) {
1624e8fbd250SKuninori Morimoto 			if (comp->driver->remove_order == order)
1625f2ed6b07SMengdong Lin 				soc_remove_component(comp);
16265f3484acSLars-Peter Clausen 		}
16272eea392dSJarkko Nikula 	}
16282eea392dSJarkko Nikula }
16292eea392dSJarkko Nikula 
1630ce64c8b9SLars-Peter Clausen /**
1631ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1632ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1633ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1634ce64c8b9SLars-Peter Clausen  *
1635ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1636ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1637ce64c8b9SLars-Peter Clausen  *
1638ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1639ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1640ce64c8b9SLars-Peter Clausen  *
1641ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1642ce64c8b9SLars-Peter Clausen  */
1643ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1644ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1645ce64c8b9SLars-Peter Clausen {
1646ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
16470b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1648ce64c8b9SLars-Peter Clausen 	unsigned int i;
1649ce64c8b9SLars-Peter Clausen 	int ret;
1650ce64c8b9SLars-Peter Clausen 
16510b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1652ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1653ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1654ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1655ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1656ce64c8b9SLars-Peter Clausen 			return ret;
1657ce64c8b9SLars-Peter Clausen 		}
1658ce64c8b9SLars-Peter Clausen 	}
1659ce64c8b9SLars-Peter Clausen 
16602c7b696aSMarcel Ziswiler 	/*
16612c7b696aSMarcel Ziswiler 	 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
16622c7b696aSMarcel Ziswiler 	 * the component which has non_legacy_dai_naming is Codec
16632c7b696aSMarcel Ziswiler 	 */
1664999f7f5aSKuninori Morimoto 	if (cpu_dai->component->driver->non_legacy_dai_naming) {
1665ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1666ce64c8b9SLars-Peter Clausen 
1667ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1668ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1669ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1670ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1671ce64c8b9SLars-Peter Clausen 			break;
1672ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1673ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1674ce64c8b9SLars-Peter Clausen 			break;
1675ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1676ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1677ce64c8b9SLars-Peter Clausen 			break;
1678ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1679ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1680ce64c8b9SLars-Peter Clausen 			break;
1681ce64c8b9SLars-Peter Clausen 		}
1682ce64c8b9SLars-Peter Clausen 
1683ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1684ce64c8b9SLars-Peter Clausen 	}
1685ce64c8b9SLars-Peter Clausen 
1686ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1687ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1688ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1689ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1690ce64c8b9SLars-Peter Clausen 		return ret;
1691ce64c8b9SLars-Peter Clausen 	}
1692ce64c8b9SLars-Peter Clausen 
1693ce64c8b9SLars-Peter Clausen 	return 0;
1694ce64c8b9SLars-Peter Clausen }
1695ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1696ce64c8b9SLars-Peter Clausen 
16971f5a4535STakashi Iwai #ifdef CONFIG_DMI
16982c7b696aSMarcel Ziswiler /*
16992c7b696aSMarcel Ziswiler  * Trim special characters, and replace '-' with '_' since '-' is used to
1700345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1701345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1702345233d7SLiam Girdwood  */
1703345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1704345233d7SLiam Girdwood {
1705345233d7SLiam Girdwood 	int i, j = 0;
1706345233d7SLiam Girdwood 
1707345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1708345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1709345233d7SLiam Girdwood 		    || (name[i] == '_'))
1710345233d7SLiam Girdwood 			name[j++] = name[i];
1711345233d7SLiam Girdwood 		else if (name[i] == '-')
1712345233d7SLiam Girdwood 			name[j++] = '_';
1713345233d7SLiam Girdwood 	}
1714345233d7SLiam Girdwood 
1715345233d7SLiam Girdwood 	name[j] = '\0';
1716345233d7SLiam Girdwood }
1717345233d7SLiam Girdwood 
17182c7b696aSMarcel Ziswiler /*
17192c7b696aSMarcel Ziswiler  * Check if a DMI field is valid, i.e. not containing any string
172098faf436SMengdong Lin  * in the black list.
172198faf436SMengdong Lin  */
172298faf436SMengdong Lin static int is_dmi_valid(const char *field)
172398faf436SMengdong Lin {
172498faf436SMengdong Lin 	int i = 0;
172598faf436SMengdong Lin 
172698faf436SMengdong Lin 	while (dmi_blacklist[i]) {
172798faf436SMengdong Lin 		if (strstr(field, dmi_blacklist[i]))
172898faf436SMengdong Lin 			return 0;
172998faf436SMengdong Lin 		i++;
173046b5a4d2SWu Fengguang 	}
173198faf436SMengdong Lin 
173298faf436SMengdong Lin 	return 1;
173398faf436SMengdong Lin }
173498faf436SMengdong Lin 
1735345233d7SLiam Girdwood /**
1736345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
1737345233d7SLiam Girdwood  * @card: The card to register DMI names
1738345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
1739345233d7SLiam Girdwood  *
1740345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
1741345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
1742345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
1743345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
1744345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
1745345233d7SLiam Girdwood  * as the sound card long name, in the format of
1746345233d7SLiam Girdwood  * "vendor-product-version-board"
1747345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
1748345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
1749345233d7SLiam Girdwood  * (UCM) configurations for the card.
1750345233d7SLiam Girdwood  *
1751345233d7SLiam Girdwood  * Possible card long names may be:
1752345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
1753345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1754345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1755345233d7SLiam Girdwood  *
1756345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
1757345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
1758345233d7SLiam Girdwood  *
1759345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
1760345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
1761345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
1762345233d7SLiam Girdwood  * charactors like SPACE.
1763345233d7SLiam Girdwood  *
1764345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
1765345233d7SLiam Girdwood  */
1766345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1767345233d7SLiam Girdwood {
1768345233d7SLiam Girdwood 	const char *vendor, *product, *product_version, *board;
1769345233d7SLiam Girdwood 	size_t longname_buf_size = sizeof(card->snd_card->longname);
1770345233d7SLiam Girdwood 	size_t len;
1771345233d7SLiam Girdwood 
1772345233d7SLiam Girdwood 	if (card->long_name)
1773345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
1774345233d7SLiam Girdwood 
1775345233d7SLiam Girdwood 	/* make up dmi long name as: vendor.product.version.board */
1776345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
177798faf436SMengdong Lin 	if (!vendor || !is_dmi_valid(vendor)) {
1778345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1779345233d7SLiam Girdwood 		return 0;
1780345233d7SLiam Girdwood 	}
1781345233d7SLiam Girdwood 
1782345233d7SLiam Girdwood 	snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1783345233d7SLiam Girdwood 			 "%s", vendor);
1784345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
1785345233d7SLiam Girdwood 
1786345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
178798faf436SMengdong Lin 	if (product && is_dmi_valid(product)) {
1788345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1789345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1790345233d7SLiam Girdwood 			 longname_buf_size - len,
1791345233d7SLiam Girdwood 			 "-%s", product);
1792345233d7SLiam Girdwood 
1793345233d7SLiam Girdwood 		len++;	/* skip the separator "-" */
1794345233d7SLiam Girdwood 		if (len < longname_buf_size)
1795345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1796345233d7SLiam Girdwood 
17972c7b696aSMarcel Ziswiler 		/*
17982c7b696aSMarcel Ziswiler 		 * some vendors like Lenovo may only put a self-explanatory
1799345233d7SLiam Girdwood 		 * name in the product version field
1800345233d7SLiam Girdwood 		 */
1801345233d7SLiam Girdwood 		product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
180298faf436SMengdong Lin 		if (product_version && is_dmi_valid(product_version)) {
1803345233d7SLiam Girdwood 			len = strlen(card->dmi_longname);
1804345233d7SLiam Girdwood 			snprintf(card->dmi_longname + len,
1805345233d7SLiam Girdwood 				 longname_buf_size - len,
1806345233d7SLiam Girdwood 				 "-%s", product_version);
1807345233d7SLiam Girdwood 
1808345233d7SLiam Girdwood 			len++;
1809345233d7SLiam Girdwood 			if (len < longname_buf_size)
1810345233d7SLiam Girdwood 				cleanup_dmi_name(card->dmi_longname + len);
1811345233d7SLiam Girdwood 		}
1812345233d7SLiam Girdwood 	}
1813345233d7SLiam Girdwood 
1814345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
181598faf436SMengdong Lin 	if (board && is_dmi_valid(board)) {
1816345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1817345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1818345233d7SLiam Girdwood 			 longname_buf_size - len,
1819345233d7SLiam Girdwood 			 "-%s", board);
1820345233d7SLiam Girdwood 
1821345233d7SLiam Girdwood 		len++;
1822345233d7SLiam Girdwood 		if (len < longname_buf_size)
1823345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1824345233d7SLiam Girdwood 	} else if (!product) {
1825345233d7SLiam Girdwood 		/* fall back to using legacy name */
1826345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1827345233d7SLiam Girdwood 		return 0;
1828345233d7SLiam Girdwood 	}
1829345233d7SLiam Girdwood 
1830345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
1831345233d7SLiam Girdwood 	if (flavour) {
1832345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1833345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1834345233d7SLiam Girdwood 			 longname_buf_size - len,
1835345233d7SLiam Girdwood 			 "-%s", flavour);
1836345233d7SLiam Girdwood 
1837345233d7SLiam Girdwood 		len++;
1838345233d7SLiam Girdwood 		if (len < longname_buf_size)
1839345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1840345233d7SLiam Girdwood 	}
1841345233d7SLiam Girdwood 
1842345233d7SLiam Girdwood 	/* set the card long name */
1843345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
1844345233d7SLiam Girdwood 
1845345233d7SLiam Girdwood 	return 0;
1846345233d7SLiam Girdwood }
1847345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
18481f5a4535STakashi Iwai #endif /* CONFIG_DMI */
1849345233d7SLiam Girdwood 
1850a655de80SLiam Girdwood static void soc_check_tplg_fes(struct snd_soc_card *card)
1851a655de80SLiam Girdwood {
1852a655de80SLiam Girdwood 	struct snd_soc_component *component;
1853a655de80SLiam Girdwood 	const struct snd_soc_component_driver *comp_drv;
1854a655de80SLiam Girdwood 	struct snd_soc_dai_link *dai_link;
1855a655de80SLiam Girdwood 	int i;
1856a655de80SLiam Girdwood 
1857368dee94SKuninori Morimoto 	for_each_component(component) {
1858a655de80SLiam Girdwood 
185949f9c4f2SDaniel Baluta 		/* does this component override BEs ? */
1860a655de80SLiam Girdwood 		if (!component->driver->ignore_machine)
1861a655de80SLiam Girdwood 			continue;
1862a655de80SLiam Girdwood 
1863a655de80SLiam Girdwood 		/* for this machine ? */
1864e194098bSPierre-Louis Bossart 		if (!strcmp(component->driver->ignore_machine,
1865a655de80SLiam Girdwood 			    card->dev->driver->name))
1866e194098bSPierre-Louis Bossart 			goto match;
1867e194098bSPierre-Louis Bossart 		if (strcmp(component->driver->ignore_machine,
1868e194098bSPierre-Louis Bossart 			   dev_name(card->dev)))
1869a655de80SLiam Girdwood 			continue;
1870e194098bSPierre-Louis Bossart match:
1871a655de80SLiam Girdwood 		/* machine matches, so override the rtd data */
18727fe072b4SKuninori Morimoto 		for_each_card_prelinks(card, i, dai_link) {
1873a655de80SLiam Girdwood 
1874a655de80SLiam Girdwood 			/* ignore this FE */
1875a655de80SLiam Girdwood 			if (dai_link->dynamic) {
1876a655de80SLiam Girdwood 				dai_link->ignore = true;
1877a655de80SLiam Girdwood 				continue;
1878a655de80SLiam Girdwood 			}
1879a655de80SLiam Girdwood 
188049f9c4f2SDaniel Baluta 			dev_info(card->dev, "info: override BE DAI link %s\n",
1881a655de80SLiam Girdwood 				 card->dai_link[i].name);
1882a655de80SLiam Girdwood 
1883a655de80SLiam Girdwood 			/* override platform component */
1884adb76b5bSKuninori Morimoto 			if (!dai_link->platforms) {
1885daecf46eSKuninori Morimoto 				dev_err(card->dev, "init platform error");
1886daecf46eSKuninori Morimoto 				continue;
1887daecf46eSKuninori Morimoto 			}
1888910fdcabSKuninori Morimoto 			dai_link->platforms->name = component->name;
1889a655de80SLiam Girdwood 
1890a655de80SLiam Girdwood 			/* convert non BE into BE */
1891a655de80SLiam Girdwood 			dai_link->no_pcm = 1;
1892a655de80SLiam Girdwood 
1893a655de80SLiam Girdwood 			/* override any BE fixups */
1894a655de80SLiam Girdwood 			dai_link->be_hw_params_fixup =
1895a655de80SLiam Girdwood 				component->driver->be_hw_params_fixup;
1896a655de80SLiam Girdwood 
18972c7b696aSMarcel Ziswiler 			/*
18982c7b696aSMarcel Ziswiler 			 * most BE links don't set stream name, so set it to
1899a655de80SLiam Girdwood 			 * dai link name if it's NULL to help bind widgets.
1900a655de80SLiam Girdwood 			 */
1901a655de80SLiam Girdwood 			if (!dai_link->stream_name)
1902a655de80SLiam Girdwood 				dai_link->stream_name = dai_link->name;
1903a655de80SLiam Girdwood 		}
1904a655de80SLiam Girdwood 
1905a655de80SLiam Girdwood 		/* Inform userspace we are using alternate topology */
1906a655de80SLiam Girdwood 		if (component->driver->topology_name_prefix) {
1907a655de80SLiam Girdwood 
1908a655de80SLiam Girdwood 			/* topology shortname created? */
1909a655de80SLiam Girdwood 			if (!card->topology_shortname_created) {
1910a655de80SLiam Girdwood 				comp_drv = component->driver;
1911a655de80SLiam Girdwood 
1912a655de80SLiam Girdwood 				snprintf(card->topology_shortname, 32, "%s-%s",
1913a655de80SLiam Girdwood 					 comp_drv->topology_name_prefix,
1914a655de80SLiam Girdwood 					 card->name);
1915a655de80SLiam Girdwood 				card->topology_shortname_created = true;
1916a655de80SLiam Girdwood 			}
1917a655de80SLiam Girdwood 
1918a655de80SLiam Girdwood 			/* use topology shortname */
1919a655de80SLiam Girdwood 			card->name = card->topology_shortname;
1920a655de80SLiam Girdwood 		}
1921a655de80SLiam Girdwood 	}
1922a655de80SLiam Girdwood }
1923a655de80SLiam Girdwood 
19240f23f718SKuninori Morimoto #define soc_setup_card_name(name, name1, name2, norm)		\
19250f23f718SKuninori Morimoto 	__soc_setup_card_name(name, sizeof(name), name1, name2, norm)
19260f23f718SKuninori Morimoto static void __soc_setup_card_name(char *name, int len,
19270f23f718SKuninori Morimoto 				  const char *name1, const char *name2,
19280f23f718SKuninori Morimoto 				  int normalization)
19290f23f718SKuninori Morimoto {
19300f23f718SKuninori Morimoto 	int i;
19310f23f718SKuninori Morimoto 
19320f23f718SKuninori Morimoto 	snprintf(name, len, "%s", name1 ? name1 : name2);
19330f23f718SKuninori Morimoto 
19340f23f718SKuninori Morimoto 	if (!normalization)
19350f23f718SKuninori Morimoto 		return;
19360f23f718SKuninori Morimoto 
19370f23f718SKuninori Morimoto 	/*
19380f23f718SKuninori Morimoto 	 * Name normalization
19390f23f718SKuninori Morimoto 	 *
19400f23f718SKuninori Morimoto 	 * The driver name is somewhat special, as it's used as a key for
19410f23f718SKuninori Morimoto 	 * searches in the user-space.
19420f23f718SKuninori Morimoto 	 *
19430f23f718SKuninori Morimoto 	 * ex)
19440f23f718SKuninori Morimoto 	 *	"abcd??efg" -> "abcd__efg"
19450f23f718SKuninori Morimoto 	 */
19460f23f718SKuninori Morimoto 	for (i = 0; i < len; i++) {
19470f23f718SKuninori Morimoto 		switch (name[i]) {
19480f23f718SKuninori Morimoto 		case '_':
19490f23f718SKuninori Morimoto 		case '-':
19500f23f718SKuninori Morimoto 		case '\0':
19510f23f718SKuninori Morimoto 			break;
19520f23f718SKuninori Morimoto 		default:
19530f23f718SKuninori Morimoto 			if (!isalnum(name[i]))
19540f23f718SKuninori Morimoto 				name[i] = '_';
19550f23f718SKuninori Morimoto 			break;
19560f23f718SKuninori Morimoto 		}
19570f23f718SKuninori Morimoto 	}
19580f23f718SKuninori Morimoto }
19590f23f718SKuninori Morimoto 
1960a4de83a3SKuninori Morimoto static void soc_cleanup_card_resources(struct snd_soc_card *card)
196153e947a0SKuninori Morimoto {
19627ce6088fSKuninori Morimoto 	struct snd_soc_dai_link *link, *_link;
19637ce6088fSKuninori Morimoto 
196453e947a0SKuninori Morimoto 	/* free the ALSA card at first; this syncs with pending operations */
196529040d1aSKuninori Morimoto 	if (card->snd_card) {
196653e947a0SKuninori Morimoto 		snd_card_free(card->snd_card);
196729040d1aSKuninori Morimoto 		card->snd_card = NULL;
196829040d1aSKuninori Morimoto 	}
196953e947a0SKuninori Morimoto 
197053e947a0SKuninori Morimoto 	/* remove and free each DAI */
19717ce6088fSKuninori Morimoto 	soc_remove_link_dais(card);
19727ce6088fSKuninori Morimoto 	soc_remove_link_components(card);
19737ce6088fSKuninori Morimoto 
19747ce6088fSKuninori Morimoto 	for_each_card_links_safe(card, link, _link)
19757ce6088fSKuninori Morimoto 		snd_soc_remove_dai_link(card, link);
19767ce6088fSKuninori Morimoto 
197753e947a0SKuninori Morimoto 	soc_remove_pcm_runtimes(card);
197853e947a0SKuninori Morimoto 
197953e947a0SKuninori Morimoto 	/* remove auxiliary devices */
198053e947a0SKuninori Morimoto 	soc_remove_aux_devices(card);
1981e8fbd250SKuninori Morimoto 	soc_unbind_aux_dev(card);
198253e947a0SKuninori Morimoto 
198353e947a0SKuninori Morimoto 	snd_soc_dapm_free(&card->dapm);
198453e947a0SKuninori Morimoto 	soc_cleanup_card_debugfs(card);
198553e947a0SKuninori Morimoto 
198653e947a0SKuninori Morimoto 	/* remove the card */
198753e947a0SKuninori Morimoto 	if (card->remove)
198853e947a0SKuninori Morimoto 		card->remove(card);
198953e947a0SKuninori Morimoto }
199053e947a0SKuninori Morimoto 
1991b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1992f0fba2adSLiam Girdwood {
19931a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
199461b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
1995c7e73774SKuninori Morimoto 	int ret, i;
199696dd3622SMark Brown 
199734e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
199801b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1999f0fba2adSLiam Girdwood 
200095c267ddSKuninori Morimoto 	snd_soc_dapm_init(&card->dapm, card, NULL);
200153e947a0SKuninori Morimoto 
2002a655de80SLiam Girdwood 	/* check whether any platform is ignore machine FE and using topology */
2003a655de80SLiam Girdwood 	soc_check_tplg_fes(card);
2004a655de80SLiam Girdwood 
200544c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
2006bee886f1SKuninori Morimoto 	ret = soc_bind_aux_dev(card);
2007bee886f1SKuninori Morimoto 	if (ret < 0)
200853e947a0SKuninori Morimoto 		goto probe_end;
2009db2a4165SFrank Mandarino 
2010f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
2011d8145989SKuninori Morimoto 	card->num_rtd = 0;
20125b99a0aaSKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link) {
20135b99a0aaSKuninori Morimoto 		ret = snd_soc_add_dai_link(card, dai_link);
20145b99a0aaSKuninori Morimoto 		if (ret < 0)
20155b99a0aaSKuninori Morimoto 			goto probe_end;
20165b99a0aaSKuninori Morimoto 	}
2017f8f80361SMengdong Lin 
2018f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
2019102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2020f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
2021f0fba2adSLiam Girdwood 	if (ret < 0) {
202210e8aa9aSMichał Mirosław 		dev_err(card->dev,
202310e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
202410e8aa9aSMichał Mirosław 			card->name, ret);
202553e947a0SKuninori Morimoto 		goto probe_end;
2026db2a4165SFrank Mandarino 	}
2027db2a4165SFrank Mandarino 
20280757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
20290757d834SLars-Peter Clausen 
2030b3da4251SKuninori Morimoto 	soc_resume_init(card);
20316ed25978SAndy Green 
2032b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
20339a841ebbSMark Brown 					card->num_dapm_widgets);
2034b8ba3b57SKuninori Morimoto 	if (ret < 0)
2035b8ba3b57SKuninori Morimoto 		goto probe_end;
20369a841ebbSMark Brown 
2037b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2038f23e860eSNicolin Chen 					card->num_of_dapm_widgets);
2039b8ba3b57SKuninori Morimoto 	if (ret < 0)
2040b8ba3b57SKuninori Morimoto 		goto probe_end;
2041f23e860eSNicolin Chen 
2042f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
2043f0fba2adSLiam Girdwood 	if (card->probe) {
2044e7361ec4SMark Brown 		ret = card->probe(card);
2045f0fba2adSLiam Girdwood 		if (ret < 0)
204653e947a0SKuninori Morimoto 			goto probe_end;
2047f0fba2adSLiam Girdwood 	}
2048f0fba2adSLiam Girdwood 
204962ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
205062f07a6bSKuninori Morimoto 	ret = soc_probe_link_components(card);
205162ae68faSStephen Warren 	if (ret < 0) {
2052f110bfc7SLiam Girdwood 		dev_err(card->dev,
205362f07a6bSKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
205453e947a0SKuninori Morimoto 		goto probe_end;
205562ae68faSStephen Warren 	}
205662ae68faSStephen Warren 
2057f2ed6b07SMengdong Lin 	/* probe auxiliary components */
2058f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
2059f2ed6b07SMengdong Lin 	if (ret < 0)
206053e947a0SKuninori Morimoto 		goto probe_end;
2061f2ed6b07SMengdong Lin 
206262ae68faSStephen Warren 	/* probe all DAI links on this card */
2063c7e73774SKuninori Morimoto 	ret = soc_probe_link_dais(card);
2064fe3e78e0SMark Brown 	if (ret < 0) {
2065f110bfc7SLiam Girdwood 		dev_err(card->dev,
2066c7e73774SKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
206753e947a0SKuninori Morimoto 		goto probe_end;
2068fe3e78e0SMark Brown 	}
2069fe3e78e0SMark Brown 
2070c4b46982SKuninori Morimoto 	for_each_card_rtds(card, rtd)
2071c4b46982SKuninori Morimoto 		soc_link_init(card, rtd);
2072c4b46982SKuninori Morimoto 
2073888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
2074b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
2075888df395SMark Brown 
20769b98c7c2SKuninori Morimoto 	ret = snd_soc_add_card_controls(card, card->controls,
20772c7b696aSMarcel Ziswiler 					card->num_controls);
20789b98c7c2SKuninori Morimoto 	if (ret < 0)
20799b98c7c2SKuninori Morimoto 		goto probe_end;
2080b7af1dafSMark Brown 
2081daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2082b8ad29deSMark Brown 				      card->num_dapm_routes);
2083daa480bdSKuninori Morimoto 	if (ret < 0)
2084daa480bdSKuninori Morimoto 		goto probe_end;
2085b8ad29deSMark Brown 
2086daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2087f23e860eSNicolin Chen 				      card->num_of_dapm_routes);
2088daa480bdSKuninori Morimoto 	if (ret < 0)
2089daa480bdSKuninori Morimoto 		goto probe_end;
209075d9ac46SMark Brown 
2091861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
2092861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
2093861886d3STakashi Iwai 
20940f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->shortname,
20950f23f718SKuninori Morimoto 			    card->name, NULL, 0);
20960f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->longname,
20970f23f718SKuninori Morimoto 			    card->long_name, card->name, 0);
20980f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->driver,
20990f23f718SKuninori Morimoto 			    card->driver_name, card->name, 1);
2100fe3e78e0SMark Brown 
210128e9ad92SMark Brown 	if (card->late_probe) {
210228e9ad92SMark Brown 		ret = card->late_probe(card);
210328e9ad92SMark Brown 		if (ret < 0) {
2104f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
210528e9ad92SMark Brown 				card->name, ret);
210653e947a0SKuninori Morimoto 			goto probe_end;
210728e9ad92SMark Brown 		}
210828e9ad92SMark Brown 	}
210928e9ad92SMark Brown 
2110824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
21118c193b8dSLars-Peter Clausen 
2112f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2113fe3e78e0SMark Brown 	if (ret < 0) {
2114f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2115f110bfc7SLiam Girdwood 				ret);
211653e947a0SKuninori Morimoto 		goto probe_end;
2117fe3e78e0SMark Brown 	}
2118fe3e78e0SMark Brown 
2119435c5e25SMark Brown 	card->instantiated = 1;
2120882eab6cSTzung-Bi Shih 	dapm_mark_endpoints_dirty(card);
21214f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2122b19e6e7bSMark Brown 
212353e947a0SKuninori Morimoto probe_end:
212453e947a0SKuninori Morimoto 	if (ret < 0)
212553e947a0SKuninori Morimoto 		soc_cleanup_card_resources(card);
2126db2a4165SFrank Mandarino 
2127f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
212834e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2129db2a4165SFrank Mandarino 
2130b19e6e7bSMark Brown 	return ret;
2131435c5e25SMark Brown }
2132435c5e25SMark Brown 
2133435c5e25SMark Brown /* probes a new socdev */
2134435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2135435c5e25SMark Brown {
2136f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2137435c5e25SMark Brown 
213870a7ca34SVinod Koul 	/*
213970a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
214070a7ca34SVinod Koul 	 * we should not be here in that case so ret error
214170a7ca34SVinod Koul 	 */
214270a7ca34SVinod Koul 	if (!card)
214370a7ca34SVinod Koul 		return -EINVAL;
214470a7ca34SVinod Koul 
2145fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2146f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2147fe4085e8SMark Brown 		 card->name);
2148fe4085e8SMark Brown 
2149435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2150435c5e25SMark Brown 	card->dev = &pdev->dev;
2151f0fba2adSLiam Girdwood 
215228d528c8SMark Brown 	return snd_soc_register_card(card);
2153435c5e25SMark Brown }
2154435c5e25SMark Brown 
2155b0e26485SVinod Koul /* removes a socdev */
2156b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2157b0e26485SVinod Koul {
2158b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2159b0e26485SVinod Koul 
2160c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2161db2a4165SFrank Mandarino 	return 0;
2162db2a4165SFrank Mandarino }
2163db2a4165SFrank Mandarino 
21646f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
216551737470SMark Brown {
21666f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
21671a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
216851737470SMark Brown 
216951737470SMark Brown 	if (!card->instantiated)
2170416356fcSMark Brown 		return 0;
217151737470SMark Brown 
21722c7b696aSMarcel Ziswiler 	/*
21732c7b696aSMarcel Ziswiler 	 * Flush out pmdown_time work - we actually do want to run it
21742c7b696aSMarcel Ziswiler 	 * now, we're shutting down so no imminent restart.
21752c7b696aSMarcel Ziswiler 	 */
217665462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
217751737470SMark Brown 
2178f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2179416356fcSMark Brown 
2180988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
2181bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
218288bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
21830b7990e3SKuninori Morimoto 		struct snd_soc_dai *codec_dai;
21841a497983SMengdong Lin 		int i;
218588bd870fSBenoit Cousson 
2186988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
21870b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
218888bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
218988bd870fSBenoit Cousson 		}
2190988e8cc4SNicolin Chen 	}
2191988e8cc4SNicolin Chen 
2192416356fcSMark Brown 	return 0;
219351737470SMark Brown }
21946f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
219551737470SMark Brown 
21966f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2197b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2198b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2199b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2200b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
22016f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2202b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2203416356fcSMark Brown };
2204deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2205416356fcSMark Brown 
2206db2a4165SFrank Mandarino /* ASoC platform driver */
2207db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2208db2a4165SFrank Mandarino 	.driver		= {
2209db2a4165SFrank Mandarino 		.name		= "soc-audio",
22106f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2211db2a4165SFrank Mandarino 	},
2212db2a4165SFrank Mandarino 	.probe		= soc_probe,
2213db2a4165SFrank Mandarino 	.remove		= soc_remove,
2214db2a4165SFrank Mandarino };
2215db2a4165SFrank Mandarino 
2216096e49d5SMark Brown /**
2217db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2218db2a4165SFrank Mandarino  * @_template: control template
2219db2a4165SFrank Mandarino  * @data: control private data
2220ac11a2b3SMark Brown  * @long_name: control long name
2221efb7ac3fSMark Brown  * @prefix: control name prefix
2222db2a4165SFrank Mandarino  *
2223db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2224db2a4165SFrank Mandarino  *
2225db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2226db2a4165SFrank Mandarino  */
2227db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
22283056557fSMark Brown 				  void *data, const char *long_name,
2229efb7ac3fSMark Brown 				  const char *prefix)
2230db2a4165SFrank Mandarino {
2231db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2232efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2233efb7ac3fSMark Brown 	char *name = NULL;
2234db2a4165SFrank Mandarino 
2235db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2236db2a4165SFrank Mandarino 	template.index = 0;
2237db2a4165SFrank Mandarino 
2238efb7ac3fSMark Brown 	if (!long_name)
2239efb7ac3fSMark Brown 		long_name = template.name;
2240efb7ac3fSMark Brown 
2241efb7ac3fSMark Brown 	if (prefix) {
22422b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2243efb7ac3fSMark Brown 		if (!name)
2244efb7ac3fSMark Brown 			return NULL;
2245efb7ac3fSMark Brown 
2246efb7ac3fSMark Brown 		template.name = name;
2247efb7ac3fSMark Brown 	} else {
2248efb7ac3fSMark Brown 		template.name = long_name;
2249efb7ac3fSMark Brown 	}
2250efb7ac3fSMark Brown 
2251efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2252efb7ac3fSMark Brown 
2253efb7ac3fSMark Brown 	kfree(name);
2254efb7ac3fSMark Brown 
2255efb7ac3fSMark Brown 	return kcontrol;
2256db2a4165SFrank Mandarino }
2257db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2258db2a4165SFrank Mandarino 
2259022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2260022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2261022658beSLiam Girdwood 	const char *prefix, void *data)
2262022658beSLiam Girdwood {
2263022658beSLiam Girdwood 	int err, i;
2264022658beSLiam Girdwood 
2265022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2266022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
22672c7b696aSMarcel Ziswiler 
2268022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2269022658beSLiam Girdwood 						     control->name, prefix));
2270022658beSLiam Girdwood 		if (err < 0) {
2271f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2272f110bfc7SLiam Girdwood 				control->name, err);
2273022658beSLiam Girdwood 			return err;
2274022658beSLiam Girdwood 		}
2275022658beSLiam Girdwood 	}
2276022658beSLiam Girdwood 
2277022658beSLiam Girdwood 	return 0;
2278022658beSLiam Girdwood }
2279022658beSLiam Girdwood 
22804fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
22814fefd698SDimitris Papastamos 					       const char *name)
22824fefd698SDimitris Papastamos {
22834fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
22844fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
22854fefd698SDimitris Papastamos 
22864fefd698SDimitris Papastamos 	if (unlikely(!name))
22874fefd698SDimitris Papastamos 		return NULL;
22884fefd698SDimitris Papastamos 
22894fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
22904fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
22914fefd698SDimitris Papastamos 			return kctl;
22924fefd698SDimitris Papastamos 	return NULL;
22934fefd698SDimitris Papastamos }
22944fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
22954fefd698SDimitris Papastamos 
2296db2a4165SFrank Mandarino /**
22970f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
22980f2780adSLars-Peter Clausen  *
22990f2780adSLars-Peter Clausen  * @component: Component to add controls to
23000f2780adSLars-Peter Clausen  * @controls: Array of controls to add
23010f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
23020f2780adSLars-Peter Clausen  *
23030f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
23040f2780adSLars-Peter Clausen  */
23050f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
23060f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
23070f2780adSLars-Peter Clausen {
23080f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
23090f2780adSLars-Peter Clausen 
23100f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
23110f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
23120f2780adSLars-Peter Clausen }
23130f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
23140f2780adSLars-Peter Clausen 
23150f2780adSLars-Peter Clausen /**
2316022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2317022658beSLiam Girdwood  * Convenience function to add a list of controls.
2318022658beSLiam Girdwood  *
2319022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2320022658beSLiam Girdwood  * @controls: array of controls to add
2321022658beSLiam Girdwood  * @num_controls: number of elements in the array
2322022658beSLiam Girdwood  *
2323022658beSLiam Girdwood  * Return 0 for success, else error.
2324022658beSLiam Girdwood  */
2325022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2326022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2327022658beSLiam Girdwood {
2328022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2329022658beSLiam Girdwood 
2330022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2331022658beSLiam Girdwood 			NULL, soc_card);
2332022658beSLiam Girdwood }
2333022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2334022658beSLiam Girdwood 
2335022658beSLiam Girdwood /**
2336022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2337022658beSLiam Girdwood  * Convienience function to add a list of controls.
2338022658beSLiam Girdwood  *
2339022658beSLiam Girdwood  * @dai: DAI to add controls to
2340022658beSLiam Girdwood  * @controls: array of controls to add
2341022658beSLiam Girdwood  * @num_controls: number of elements in the array
2342022658beSLiam Girdwood  *
2343022658beSLiam Girdwood  * Return 0 for success, else error.
2344022658beSLiam Girdwood  */
2345022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2346022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2347022658beSLiam Girdwood {
2348313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2349022658beSLiam Girdwood 
2350022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2351022658beSLiam Girdwood 			NULL, dai);
2352022658beSLiam Girdwood }
2353022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2354022658beSLiam Girdwood 
2355e894efefSSrinivas Kandagatla static int snd_soc_bind_card(struct snd_soc_card *card)
2356e894efefSSrinivas Kandagatla {
2357e894efefSSrinivas Kandagatla 	struct snd_soc_pcm_runtime *rtd;
2358e894efefSSrinivas Kandagatla 	int ret;
2359e894efefSSrinivas Kandagatla 
2360e894efefSSrinivas Kandagatla 	ret = snd_soc_instantiate_card(card);
2361e894efefSSrinivas Kandagatla 	if (ret != 0)
2362e894efefSSrinivas Kandagatla 		return ret;
2363e894efefSSrinivas Kandagatla 
2364e894efefSSrinivas Kandagatla 	/* deactivate pins to sleep state */
2365bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
2366e894efefSSrinivas Kandagatla 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2367e894efefSSrinivas Kandagatla 		struct snd_soc_dai *codec_dai;
2368e894efefSSrinivas Kandagatla 		int j;
2369e894efefSSrinivas Kandagatla 
2370e894efefSSrinivas Kandagatla 		for_each_rtd_codec_dai(rtd, j, codec_dai) {
2371e894efefSSrinivas Kandagatla 			if (!codec_dai->active)
2372e894efefSSrinivas Kandagatla 				pinctrl_pm_select_sleep_state(codec_dai->dev);
2373e894efefSSrinivas Kandagatla 		}
2374e894efefSSrinivas Kandagatla 
2375e894efefSSrinivas Kandagatla 		if (!cpu_dai->active)
2376e894efefSSrinivas Kandagatla 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
2377e894efefSSrinivas Kandagatla 	}
2378e894efefSSrinivas Kandagatla 
2379e894efefSSrinivas Kandagatla 	return ret;
2380e894efefSSrinivas Kandagatla }
2381e894efefSSrinivas Kandagatla 
2382c5af3a2eSMark Brown /**
2383c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2384c5af3a2eSMark Brown  *
2385ac11a2b3SMark Brown  * @card: Card to register
2386c5af3a2eSMark Brown  *
2387c5af3a2eSMark Brown  */
238870a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2389c5af3a2eSMark Brown {
2390c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2391c5af3a2eSMark Brown 		return -EINVAL;
2392c5af3a2eSMark Brown 
2393ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2394ed77cc12SMark Brown 
2395b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->widgets);
2396b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->paths);
2397b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->dapm_list);
2398b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->aux_comp_list);
2399b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->component_dev_list);
2400b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->list);
2401f8f80361SMengdong Lin 	INIT_LIST_HEAD(&card->dai_link_list);
24021a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
2403db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
24048a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2405b03bfaecSKuninori Morimoto 
2406c5af3a2eSMark Brown 	card->instantiated = 0;
2407f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
2408a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
240972b745e3SPeter Ujfalusi 	mutex_init(&card->pcm_mutex);
2410a9764869SKaiChieh Chuang 	spin_lock_init(&card->dpcm_lock);
2411c5af3a2eSMark Brown 
2412e894efefSSrinivas Kandagatla 	return snd_soc_bind_card(card);
2413c5af3a2eSMark Brown }
241470a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2415c5af3a2eSMark Brown 
2416e894efefSSrinivas Kandagatla static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2417e894efefSSrinivas Kandagatla {
2418e894efefSSrinivas Kandagatla 	if (card->instantiated) {
2419e894efefSSrinivas Kandagatla 		card->instantiated = false;
2420e894efefSSrinivas Kandagatla 		snd_soc_dapm_shutdown(card);
242153e947a0SKuninori Morimoto 		snd_soc_flush_all_delayed_work(card);
2422f96fb7d1SRanjani Sridharan 
2423e894efefSSrinivas Kandagatla 		soc_cleanup_card_resources(card);
2424e894efefSSrinivas Kandagatla 		if (!unregister)
2425e894efefSSrinivas Kandagatla 			list_add(&card->list, &unbind_card_list);
2426e894efefSSrinivas Kandagatla 	} else {
2427e894efefSSrinivas Kandagatla 		if (unregister)
2428e894efefSSrinivas Kandagatla 			list_del(&card->list);
2429e894efefSSrinivas Kandagatla 	}
2430e894efefSSrinivas Kandagatla }
2431e894efefSSrinivas Kandagatla 
2432c5af3a2eSMark Brown /**
2433c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2434c5af3a2eSMark Brown  *
2435ac11a2b3SMark Brown  * @card: Card to unregister
2436c5af3a2eSMark Brown  *
2437c5af3a2eSMark Brown  */
243870a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2439c5af3a2eSMark Brown {
2440b545542aSKuninori Morimoto 	mutex_lock(&client_mutex);
2441e894efefSSrinivas Kandagatla 	snd_soc_unbind_card(card, true);
2442b545542aSKuninori Morimoto 	mutex_unlock(&client_mutex);
2443f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2444c5af3a2eSMark Brown 
2445c5af3a2eSMark Brown 	return 0;
2446c5af3a2eSMark Brown }
244770a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2448c5af3a2eSMark Brown 
2449f0fba2adSLiam Girdwood /*
2450f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
2451f0fba2adSLiam Girdwood  * and sanitizing names.
2452f0fba2adSLiam Girdwood  */
24530b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2454f0fba2adSLiam Girdwood {
2455f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
2456f0fba2adSLiam Girdwood 	int id1, id2;
2457f0fba2adSLiam Girdwood 
2458f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
2459f0fba2adSLiam Girdwood 		return NULL;
2460f0fba2adSLiam Girdwood 
246158818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2462f0fba2adSLiam Girdwood 
2463f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
2464f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
2465f0fba2adSLiam Girdwood 	if (found) {
2466f0fba2adSLiam Girdwood 		/* get ID */
2467f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2468f0fba2adSLiam Girdwood 
2469f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
2470f0fba2adSLiam Girdwood 			if (*id == -1)
2471f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
2472f0fba2adSLiam Girdwood 		}
2473f0fba2adSLiam Girdwood 
2474f0fba2adSLiam Girdwood 	} else {
2475f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr" */
2476f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2477f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
2478f0fba2adSLiam Girdwood 
2479f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
248005899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
2481f0fba2adSLiam Girdwood 
2482f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
24832c7b696aSMarcel Ziswiler 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
24842c7b696aSMarcel Ziswiler 				 name);
248558818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2486f0fba2adSLiam Girdwood 		} else
2487f0fba2adSLiam Girdwood 			*id = 0;
2488f0fba2adSLiam Girdwood 	}
2489f0fba2adSLiam Girdwood 
249050014499SKuninori Morimoto 	return devm_kstrdup(dev, name, GFP_KERNEL);
2491f0fba2adSLiam Girdwood }
2492f0fba2adSLiam Girdwood 
2493f0fba2adSLiam Girdwood /*
2494f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
2495f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
2496f0fba2adSLiam Girdwood  */
2497f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
2498f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
2499f0fba2adSLiam Girdwood {
2500f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
250110e8aa9aSMichał Mirosław 		dev_err(dev,
250210e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
250310e8aa9aSMichał Mirosław 			dev_name(dev));
2504f0fba2adSLiam Girdwood 		return NULL;
2505f0fba2adSLiam Girdwood 	}
2506f0fba2adSLiam Girdwood 
250750014499SKuninori Morimoto 	return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2508f0fba2adSLiam Girdwood }
2509f0fba2adSLiam Girdwood 
25109115171aSMark Brown /**
251132c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
25129115171aSMark Brown  *
251332c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
25149115171aSMark Brown  */
251532c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
25169115171aSMark Brown {
25175c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
25189115171aSMark Brown 
251915a0c645SKuninori Morimoto 	for_each_component_dais_safe(component, dai, _dai) {
252032c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
252132c9ba54SLars-Peter Clausen 			dai->name);
25229115171aSMark Brown 		list_del(&dai->list);
25239115171aSMark Brown 	}
252432c9ba54SLars-Peter Clausen }
25259115171aSMark Brown 
25265e4fb372SMengdong Lin /* Create a DAI and add it to the component's DAI list */
25275e4fb372SMengdong Lin static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
25285e4fb372SMengdong Lin 	struct snd_soc_dai_driver *dai_drv,
25295e4fb372SMengdong Lin 	bool legacy_dai_naming)
25305e4fb372SMengdong Lin {
25315e4fb372SMengdong Lin 	struct device *dev = component->dev;
25325e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
25335e4fb372SMengdong Lin 
25345e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
25355e4fb372SMengdong Lin 
253650014499SKuninori Morimoto 	dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
25375e4fb372SMengdong Lin 	if (dai == NULL)
25385e4fb372SMengdong Lin 		return NULL;
25395e4fb372SMengdong Lin 
25405e4fb372SMengdong Lin 	/*
25415e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
25425e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
25435e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
25445e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
25455e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
25465e4fb372SMengdong Lin 	 * component-less anymore.
25475e4fb372SMengdong Lin 	 */
25485e4fb372SMengdong Lin 	if (legacy_dai_naming &&
25495e4fb372SMengdong Lin 	    (dai_drv->id == 0 || dai_drv->name == NULL)) {
25505e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
25515e4fb372SMengdong Lin 	} else {
25525e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
25535e4fb372SMengdong Lin 		if (dai_drv->id)
25545e4fb372SMengdong Lin 			dai->id = dai_drv->id;
25555e4fb372SMengdong Lin 		else
25565e4fb372SMengdong Lin 			dai->id = component->num_dai;
25575e4fb372SMengdong Lin 	}
255850014499SKuninori Morimoto 	if (!dai->name)
25595e4fb372SMengdong Lin 		return NULL;
25605e4fb372SMengdong Lin 
25615e4fb372SMengdong Lin 	dai->component = component;
25625e4fb372SMengdong Lin 	dai->dev = dev;
25635e4fb372SMengdong Lin 	dai->driver = dai_drv;
25645e4fb372SMengdong Lin 	if (!dai->driver->ops)
25655e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
25665e4fb372SMengdong Lin 
256715a0c645SKuninori Morimoto 	/* see for_each_component_dais */
256858bf4179SKuninori Morimoto 	list_add_tail(&dai->list, &component->dai_list);
25695e4fb372SMengdong Lin 	component->num_dai++;
25705e4fb372SMengdong Lin 
25715e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
25725e4fb372SMengdong Lin 	return dai;
25735e4fb372SMengdong Lin }
25745e4fb372SMengdong Lin 
25759115171aSMark Brown /**
257632c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
25779115171aSMark Brown  *
25786106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
25796106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
2580ac11a2b3SMark Brown  * @count: Number of DAIs
25819115171aSMark Brown  */
258232c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
25832c7b696aSMarcel Ziswiler 				 struct snd_soc_dai_driver *dai_drv,
25842c7b696aSMarcel Ziswiler 				 size_t count)
25859115171aSMark Brown {
25866106d129SLars-Peter Clausen 	struct device *dev = component->dev;
2587f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
258832c9ba54SLars-Peter Clausen 	unsigned int i;
258932c9ba54SLars-Peter Clausen 	int ret;
2590f0fba2adSLiam Girdwood 
25915b5e0928SAlexey Dobriyan 	dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
25929115171aSMark Brown 
25939115171aSMark Brown 	for (i = 0; i < count; i++) {
2594f0fba2adSLiam Girdwood 
25952c7b696aSMarcel Ziswiler 		dai = soc_add_dai(component, dai_drv + i, count == 1 &&
25962c7b696aSMarcel Ziswiler 				  !component->driver->non_legacy_dai_naming);
2597c46e0079SAxel Lin 		if (dai == NULL) {
2598c46e0079SAxel Lin 			ret = -ENOMEM;
2599c46e0079SAxel Lin 			goto err;
2600c46e0079SAxel Lin 		}
2601f0fba2adSLiam Girdwood 	}
2602f0fba2adSLiam Girdwood 
26039115171aSMark Brown 	return 0;
26049115171aSMark Brown 
26059115171aSMark Brown err:
260632c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
26079115171aSMark Brown 
26089115171aSMark Brown 	return ret;
26099115171aSMark Brown }
26109115171aSMark Brown 
261168003e6cSMengdong Lin /**
261268003e6cSMengdong Lin  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
261368003e6cSMengdong Lin  *
261468003e6cSMengdong Lin  * @component: The component the DAIs are registered for
261568003e6cSMengdong Lin  * @dai_drv: DAI driver to use for the DAI
261668003e6cSMengdong Lin  *
261768003e6cSMengdong Lin  * Topology can use this API to register DAIs when probing a component.
261868003e6cSMengdong Lin  * These DAIs's widgets will be freed in the card cleanup and the DAIs
261968003e6cSMengdong Lin  * will be freed in the component cleanup.
262068003e6cSMengdong Lin  */
262168003e6cSMengdong Lin int snd_soc_register_dai(struct snd_soc_component *component,
262268003e6cSMengdong Lin 	struct snd_soc_dai_driver *dai_drv)
262368003e6cSMengdong Lin {
262468003e6cSMengdong Lin 	struct snd_soc_dapm_context *dapm =
262568003e6cSMengdong Lin 		snd_soc_component_get_dapm(component);
262668003e6cSMengdong Lin 	struct snd_soc_dai *dai;
262768003e6cSMengdong Lin 	int ret;
262868003e6cSMengdong Lin 
262968003e6cSMengdong Lin 	if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
263068003e6cSMengdong Lin 		dev_err(component->dev, "Invalid dai type %d\n",
263168003e6cSMengdong Lin 			dai_drv->dobj.type);
263268003e6cSMengdong Lin 		return -EINVAL;
263368003e6cSMengdong Lin 	}
263468003e6cSMengdong Lin 
263568003e6cSMengdong Lin 	lockdep_assert_held(&client_mutex);
263668003e6cSMengdong Lin 	dai = soc_add_dai(component, dai_drv, false);
263768003e6cSMengdong Lin 	if (!dai)
263868003e6cSMengdong Lin 		return -ENOMEM;
263968003e6cSMengdong Lin 
26402c7b696aSMarcel Ziswiler 	/*
26412c7b696aSMarcel Ziswiler 	 * Create the DAI widgets here. After adding DAIs, topology may
264268003e6cSMengdong Lin 	 * also add routes that need these widgets as source or sink.
264368003e6cSMengdong Lin 	 */
264468003e6cSMengdong Lin 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
264568003e6cSMengdong Lin 	if (ret != 0) {
264668003e6cSMengdong Lin 		dev_err(component->dev,
264768003e6cSMengdong Lin 			"Failed to create DAI widgets %d\n", ret);
264868003e6cSMengdong Lin 	}
264968003e6cSMengdong Lin 
265068003e6cSMengdong Lin 	return ret;
265168003e6cSMengdong Lin }
265268003e6cSMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_register_dai);
265368003e6cSMengdong Lin 
2654bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
2655bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
2656d191bd8dSKuninori Morimoto {
26578d92bb51SKuninori Morimoto 	INIT_LIST_HEAD(&component->dai_list);
2658495efdb0SKuninori Morimoto 	INIT_LIST_HEAD(&component->dobj_list);
2659495efdb0SKuninori Morimoto 	INIT_LIST_HEAD(&component->card_list);
26608d92bb51SKuninori Morimoto 	mutex_init(&component->io_mutex);
26618d92bb51SKuninori Morimoto 
2662bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
2663bb13109dSLars-Peter Clausen 	if (!component->name) {
2664bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
2665d191bd8dSKuninori Morimoto 		return -ENOMEM;
2666d191bd8dSKuninori Morimoto 	}
2667d191bd8dSKuninori Morimoto 
2668bb13109dSLars-Peter Clausen 	component->dev = dev;
2669bb13109dSLars-Peter Clausen 	component->driver = driver;
2670e2c330b9SLars-Peter Clausen 
2671bb13109dSLars-Peter Clausen 	return 0;
2672d191bd8dSKuninori Morimoto }
2673d191bd8dSKuninori Morimoto 
267420feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2675886f5692SLars-Peter Clausen {
2676886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
267720feb881SLars-Peter Clausen 
2678886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
2679886f5692SLars-Peter Clausen 	if (val_bytes > 0)
2680886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
2681886f5692SLars-Peter Clausen }
268220feb881SLars-Peter Clausen 
2683e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
2684e874bf5fSLars-Peter Clausen 
268520feb881SLars-Peter Clausen /**
26862c7b696aSMarcel Ziswiler  * snd_soc_component_init_regmap() - Initialize regmap instance for the
26872c7b696aSMarcel Ziswiler  *                                   component
268820feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
268920feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
269020feb881SLars-Peter Clausen  *
269120feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
269220feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
269320feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
269420feb881SLars-Peter Clausen  * before the first IO attempt of the component.
269520feb881SLars-Peter Clausen  */
269620feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
269720feb881SLars-Peter Clausen 	struct regmap *regmap)
269820feb881SLars-Peter Clausen {
269920feb881SLars-Peter Clausen 	component->regmap = regmap;
270020feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
2701886f5692SLars-Peter Clausen }
270220feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
270320feb881SLars-Peter Clausen 
270420feb881SLars-Peter Clausen /**
27052c7b696aSMarcel Ziswiler  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
27062c7b696aSMarcel Ziswiler  *                                   component
270720feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
270820feb881SLars-Peter Clausen  *
270920feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
271020feb881SLars-Peter Clausen  * removes the regmap instance from the component.
271120feb881SLars-Peter Clausen  *
271220feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
271320feb881SLars-Peter Clausen  * to initialize the regmap instance.
271420feb881SLars-Peter Clausen  */
271520feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
271620feb881SLars-Peter Clausen {
271720feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
271820feb881SLars-Peter Clausen 	component->regmap = NULL;
271920feb881SLars-Peter Clausen }
272020feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2721886f5692SLars-Peter Clausen 
2722e874bf5fSLars-Peter Clausen #endif
2723d191bd8dSKuninori Morimoto 
2724359c71eeSKuninori Morimoto static void snd_soc_component_add(struct snd_soc_component *component)
2725bb13109dSLars-Peter Clausen {
2726359c71eeSKuninori Morimoto 	mutex_lock(&client_mutex);
2727359c71eeSKuninori Morimoto 
2728999f7f5aSKuninori Morimoto 	if (!component->driver->write && !component->driver->read) {
272920feb881SLars-Peter Clausen 		if (!component->regmap)
27302c7b696aSMarcel Ziswiler 			component->regmap = dev_get_regmap(component->dev,
27312c7b696aSMarcel Ziswiler 							   NULL);
273220feb881SLars-Peter Clausen 		if (component->regmap)
273320feb881SLars-Peter Clausen 			snd_soc_component_setup_regmap(component);
273420feb881SLars-Peter Clausen 	}
2735886f5692SLars-Peter Clausen 
2736368dee94SKuninori Morimoto 	/* see for_each_component */
2737bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
2738d191bd8dSKuninori Morimoto 
2739d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
2740bb13109dSLars-Peter Clausen }
2741d191bd8dSKuninori Morimoto 
2742bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
2743bb13109dSLars-Peter Clausen {
2744bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
2745bb13109dSLars-Peter Clausen }
2746d191bd8dSKuninori Morimoto 
2747bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2748bb13109dSLars-Peter Clausen {
2749c12c1aadSKuninori Morimoto 	struct snd_soc_card *card = component->card;
2750c12c1aadSKuninori Morimoto 
2751c12c1aadSKuninori Morimoto 	if (card)
2752e894efefSSrinivas Kandagatla 		snd_soc_unbind_card(card, false);
2753c12c1aadSKuninori Morimoto 
2754bb13109dSLars-Peter Clausen 	list_del(&component->list);
2755bb13109dSLars-Peter Clausen }
2756d191bd8dSKuninori Morimoto 
2757273d778eSKuninori Morimoto #define ENDIANNESS_MAP(name) \
2758273d778eSKuninori Morimoto 	(SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2759273d778eSKuninori Morimoto static u64 endianness_format_map[] = {
2760273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S16_),
2761273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U16_),
2762273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_),
2763273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_),
2764273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S32_),
2765273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U32_),
2766273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_3),
2767273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_3),
2768273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S20_3),
2769273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U20_3),
2770273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S18_3),
2771273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U18_3),
2772273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT_),
2773273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT64_),
2774273d778eSKuninori Morimoto 	ENDIANNESS_MAP(IEC958_SUBFRAME_),
2775273d778eSKuninori Morimoto };
2776273d778eSKuninori Morimoto 
2777273d778eSKuninori Morimoto /*
2778273d778eSKuninori Morimoto  * Fix up the DAI formats for endianness: codecs don't actually see
2779273d778eSKuninori Morimoto  * the endianness of the data but we're using the CPU format
2780273d778eSKuninori Morimoto  * definitions which do need to include endianness so we ensure that
2781273d778eSKuninori Morimoto  * codec DAIs always have both big and little endian variants set.
2782273d778eSKuninori Morimoto  */
2783273d778eSKuninori Morimoto static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2784273d778eSKuninori Morimoto {
2785273d778eSKuninori Morimoto 	int i;
2786273d778eSKuninori Morimoto 
2787273d778eSKuninori Morimoto 	for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2788273d778eSKuninori Morimoto 		if (stream->formats & endianness_format_map[i])
2789273d778eSKuninori Morimoto 			stream->formats |= endianness_format_map[i];
2790273d778eSKuninori Morimoto }
2791273d778eSKuninori Morimoto 
2792e894efefSSrinivas Kandagatla static void snd_soc_try_rebind_card(void)
2793e894efefSSrinivas Kandagatla {
2794e894efefSSrinivas Kandagatla 	struct snd_soc_card *card, *c;
2795e894efefSSrinivas Kandagatla 
2796b245d273SKuninori Morimoto 	list_for_each_entry_safe(card, c, &unbind_card_list, list)
2797e894efefSSrinivas Kandagatla 		if (!snd_soc_bind_card(card))
2798e894efefSSrinivas Kandagatla 			list_del(&card->list);
2799e894efefSSrinivas Kandagatla }
2800e894efefSSrinivas Kandagatla 
2801e0dac41bSKuninori Morimoto int snd_soc_add_component(struct device *dev,
2802e0dac41bSKuninori Morimoto 			struct snd_soc_component *component,
2803cf9e829eSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2804d191bd8dSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2805d191bd8dSKuninori Morimoto 			int num_dai)
2806d191bd8dSKuninori Morimoto {
2807bb13109dSLars-Peter Clausen 	int ret;
2808273d778eSKuninori Morimoto 	int i;
2809d191bd8dSKuninori Morimoto 
2810cf9e829eSKuninori Morimoto 	ret = snd_soc_component_initialize(component, component_driver, dev);
2811bb13109dSLars-Peter Clausen 	if (ret)
2812bb13109dSLars-Peter Clausen 		goto err_free;
2813bb13109dSLars-Peter Clausen 
2814273d778eSKuninori Morimoto 	if (component_driver->endianness) {
2815273d778eSKuninori Morimoto 		for (i = 0; i < num_dai; i++) {
2816273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].playback);
2817273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].capture);
2818273d778eSKuninori Morimoto 		}
2819273d778eSKuninori Morimoto 	}
2820273d778eSKuninori Morimoto 
28210e7b25c6SKuninori Morimoto 	ret = snd_soc_register_dais(component, dai_drv, num_dai);
2822bb13109dSLars-Peter Clausen 	if (ret < 0) {
2823f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
2824bb13109dSLars-Peter Clausen 		goto err_cleanup;
2825bb13109dSLars-Peter Clausen 	}
2826bb13109dSLars-Peter Clausen 
2827cf9e829eSKuninori Morimoto 	snd_soc_component_add(component);
2828e894efefSSrinivas Kandagatla 	snd_soc_try_rebind_card();
2829bb13109dSLars-Peter Clausen 
2830bb13109dSLars-Peter Clausen 	return 0;
2831bb13109dSLars-Peter Clausen 
2832bb13109dSLars-Peter Clausen err_cleanup:
2833cf9e829eSKuninori Morimoto 	snd_soc_component_cleanup(component);
2834bb13109dSLars-Peter Clausen err_free:
2835bb13109dSLars-Peter Clausen 	return ret;
2836d191bd8dSKuninori Morimoto }
2837e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component);
2838e0dac41bSKuninori Morimoto 
2839e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev,
2840e0dac41bSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2841e0dac41bSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2842e0dac41bSKuninori Morimoto 			int num_dai)
2843e0dac41bSKuninori Morimoto {
2844e0dac41bSKuninori Morimoto 	struct snd_soc_component *component;
2845e0dac41bSKuninori Morimoto 
28467ecbd6a9SKuninori Morimoto 	component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
284708e61d03SKuninori Morimoto 	if (!component)
2848e0dac41bSKuninori Morimoto 		return -ENOMEM;
2849e0dac41bSKuninori Morimoto 
2850e0dac41bSKuninori Morimoto 	return snd_soc_add_component(dev, component, component_driver,
2851e0dac41bSKuninori Morimoto 				     dai_drv, num_dai);
2852e0dac41bSKuninori Morimoto }
2853d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
2854d191bd8dSKuninori Morimoto 
2855d191bd8dSKuninori Morimoto /**
28562eccea8cSKuninori Morimoto  * snd_soc_unregister_component - Unregister all related component
28572eccea8cSKuninori Morimoto  * from the ASoC core
2858d191bd8dSKuninori Morimoto  *
2859628536eaSJonathan Corbet  * @dev: The device to unregister
2860d191bd8dSKuninori Morimoto  */
28612eccea8cSKuninori Morimoto static int __snd_soc_unregister_component(struct device *dev)
2862d191bd8dSKuninori Morimoto {
2863cf9e829eSKuninori Morimoto 	struct snd_soc_component *component;
286421a03528SKuninori Morimoto 	int found = 0;
2865d191bd8dSKuninori Morimoto 
286634e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
2867368dee94SKuninori Morimoto 	for_each_component(component) {
2868999f7f5aSKuninori Morimoto 		if (dev != component->dev)
286921a03528SKuninori Morimoto 			continue;
2870d191bd8dSKuninori Morimoto 
2871cf9e829eSKuninori Morimoto 		snd_soc_component_del_unlocked(component);
287221a03528SKuninori Morimoto 		found = 1;
287321a03528SKuninori Morimoto 		break;
2874d191bd8dSKuninori Morimoto 	}
2875d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
2876d191bd8dSKuninori Morimoto 
28772c7b696aSMarcel Ziswiler 	if (found)
2878cf9e829eSKuninori Morimoto 		snd_soc_component_cleanup(component);
28792eccea8cSKuninori Morimoto 
28802eccea8cSKuninori Morimoto 	return found;
28812eccea8cSKuninori Morimoto }
28822eccea8cSKuninori Morimoto 
28832eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
28842eccea8cSKuninori Morimoto {
28852c7b696aSMarcel Ziswiler 	while (__snd_soc_unregister_component(dev))
28862c7b696aSMarcel Ziswiler 		;
2887d191bd8dSKuninori Morimoto }
2888d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2889d191bd8dSKuninori Morimoto 
28907dd5d0d9SKuninori Morimoto struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
28917dd5d0d9SKuninori Morimoto 						   const char *driver_name)
28927dd5d0d9SKuninori Morimoto {
28937dd5d0d9SKuninori Morimoto 	struct snd_soc_component *component;
28947dd5d0d9SKuninori Morimoto 	struct snd_soc_component *ret;
28957dd5d0d9SKuninori Morimoto 
28967dd5d0d9SKuninori Morimoto 	ret = NULL;
28977dd5d0d9SKuninori Morimoto 	mutex_lock(&client_mutex);
2898368dee94SKuninori Morimoto 	for_each_component(component) {
28997dd5d0d9SKuninori Morimoto 		if (dev != component->dev)
29007dd5d0d9SKuninori Morimoto 			continue;
29017dd5d0d9SKuninori Morimoto 
29027dd5d0d9SKuninori Morimoto 		if (driver_name &&
29037dd5d0d9SKuninori Morimoto 		    (driver_name != component->driver->name) &&
29047dd5d0d9SKuninori Morimoto 		    (strcmp(component->driver->name, driver_name) != 0))
29057dd5d0d9SKuninori Morimoto 			continue;
29067dd5d0d9SKuninori Morimoto 
29077dd5d0d9SKuninori Morimoto 		ret = component;
29087dd5d0d9SKuninori Morimoto 		break;
29097dd5d0d9SKuninori Morimoto 	}
29107dd5d0d9SKuninori Morimoto 	mutex_unlock(&client_mutex);
29117dd5d0d9SKuninori Morimoto 
29127dd5d0d9SKuninori Morimoto 	return ret;
29137dd5d0d9SKuninori Morimoto }
29147dd5d0d9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
29157dd5d0d9SKuninori Morimoto 
2916bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
2917b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2918bec4fa05SStephen Warren 			       const char *propname)
2919bec4fa05SStephen Warren {
2920b07609ceSKuninori Morimoto 	struct device_node *np;
2921bec4fa05SStephen Warren 	int ret;
2922bec4fa05SStephen Warren 
29237e07e7c0STushar Behera 	if (!card->dev) {
29247e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
29257e07e7c0STushar Behera 		return -EINVAL;
29267e07e7c0STushar Behera 	}
29277e07e7c0STushar Behera 
29287e07e7c0STushar Behera 	np = card->dev->of_node;
29297e07e7c0STushar Behera 
2930bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
2931bec4fa05SStephen Warren 	/*
2932bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
2933bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
2934bec4fa05SStephen Warren 	 * snd_soc_register_card.
2935bec4fa05SStephen Warren 	 */
2936bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
2937bec4fa05SStephen Warren 		dev_err(card->dev,
2938f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
2939bec4fa05SStephen Warren 			propname, ret);
2940bec4fa05SStephen Warren 		return ret;
2941bec4fa05SStephen Warren 	}
2942bec4fa05SStephen Warren 
2943bec4fa05SStephen Warren 	return 0;
2944bec4fa05SStephen Warren }
2945b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2946bec4fa05SStephen Warren 
29479a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
29489a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
29499a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
29509a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
29519a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
29529a6d4860SXiubo Li };
29539a6d4860SXiubo Li 
295421efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
29559a6d4860SXiubo Li 					  const char *propname)
29569a6d4860SXiubo Li {
295721efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
29589a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
29599a6d4860SXiubo Li 	const char *template, *wname;
29609a6d4860SXiubo Li 	int i, j, num_widgets, ret;
29619a6d4860SXiubo Li 
29629a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
29639a6d4860SXiubo Li 	if (num_widgets < 0) {
29649a6d4860SXiubo Li 		dev_err(card->dev,
29659a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
29669a6d4860SXiubo Li 		return -EINVAL;
29679a6d4860SXiubo Li 	}
29689a6d4860SXiubo Li 	if (num_widgets & 1) {
29699a6d4860SXiubo Li 		dev_err(card->dev,
29709a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
29719a6d4860SXiubo Li 		return -EINVAL;
29729a6d4860SXiubo Li 	}
29739a6d4860SXiubo Li 
29749a6d4860SXiubo Li 	num_widgets /= 2;
29759a6d4860SXiubo Li 	if (!num_widgets) {
29769a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
29779a6d4860SXiubo Li 			propname);
29789a6d4860SXiubo Li 		return -EINVAL;
29799a6d4860SXiubo Li 	}
29809a6d4860SXiubo Li 
29819a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
29829a6d4860SXiubo Li 			       GFP_KERNEL);
29839a6d4860SXiubo Li 	if (!widgets) {
29849a6d4860SXiubo Li 		dev_err(card->dev,
29859a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
29869a6d4860SXiubo Li 		return -ENOMEM;
29879a6d4860SXiubo Li 	}
29889a6d4860SXiubo Li 
29899a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
29909a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
29919a6d4860SXiubo Li 			2 * i, &template);
29929a6d4860SXiubo Li 		if (ret) {
29939a6d4860SXiubo Li 			dev_err(card->dev,
29949a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
29959a6d4860SXiubo Li 				propname, 2 * i, ret);
29969a6d4860SXiubo Li 			return -EINVAL;
29979a6d4860SXiubo Li 		}
29989a6d4860SXiubo Li 
29999a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
30009a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
30019a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
30029a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
30039a6d4860SXiubo Li 				break;
30049a6d4860SXiubo Li 			}
30059a6d4860SXiubo Li 		}
30069a6d4860SXiubo Li 
30079a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
30089a6d4860SXiubo Li 			dev_err(card->dev,
30099a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
30109a6d4860SXiubo Li 				template);
30119a6d4860SXiubo Li 			return -EINVAL;
30129a6d4860SXiubo Li 		}
30139a6d4860SXiubo Li 
30149a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
30159a6d4860SXiubo Li 						    (2 * i) + 1,
30169a6d4860SXiubo Li 						    &wname);
30179a6d4860SXiubo Li 		if (ret) {
30189a6d4860SXiubo Li 			dev_err(card->dev,
30199a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
30209a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
30219a6d4860SXiubo Li 			return -EINVAL;
30229a6d4860SXiubo Li 		}
30239a6d4860SXiubo Li 
30249a6d4860SXiubo Li 		widgets[i].name = wname;
30259a6d4860SXiubo Li 	}
30269a6d4860SXiubo Li 
3027f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
3028f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
30299a6d4860SXiubo Li 
30309a6d4860SXiubo Li 	return 0;
30319a6d4860SXiubo Li }
303221efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
30339a6d4860SXiubo Li 
3034cbdfab3bSJerome Brunet int snd_soc_of_get_slot_mask(struct device_node *np,
30356131084aSJyri Sarha 			     const char *prop_name,
30366131084aSJyri Sarha 			     unsigned int *mask)
30376131084aSJyri Sarha {
30386131084aSJyri Sarha 	u32 val;
30396c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
30406131084aSJyri Sarha 	int i;
30416131084aSJyri Sarha 
30426131084aSJyri Sarha 	if (!of_slot_mask)
30436131084aSJyri Sarha 		return 0;
30446131084aSJyri Sarha 	val /= sizeof(u32);
30456131084aSJyri Sarha 	for (i = 0; i < val; i++)
30466131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
30476131084aSJyri Sarha 			*mask |= (1 << i);
30486131084aSJyri Sarha 
30496131084aSJyri Sarha 	return val;
30506131084aSJyri Sarha }
3051cbdfab3bSJerome Brunet EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
30526131084aSJyri Sarha 
305389c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
30546131084aSJyri Sarha 			      unsigned int *tx_mask,
30556131084aSJyri Sarha 			      unsigned int *rx_mask,
305689c67857SXiubo Li 			      unsigned int *slots,
305789c67857SXiubo Li 			      unsigned int *slot_width)
305889c67857SXiubo Li {
305989c67857SXiubo Li 	u32 val;
306089c67857SXiubo Li 	int ret;
306189c67857SXiubo Li 
30626131084aSJyri Sarha 	if (tx_mask)
30636131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
30646131084aSJyri Sarha 	if (rx_mask)
30656131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
30666131084aSJyri Sarha 
306789c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
306889c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
306989c67857SXiubo Li 		if (ret)
307089c67857SXiubo Li 			return ret;
307189c67857SXiubo Li 
307289c67857SXiubo Li 		if (slots)
307389c67857SXiubo Li 			*slots = val;
307489c67857SXiubo Li 	}
307589c67857SXiubo Li 
307689c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
307789c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
307889c67857SXiubo Li 		if (ret)
307989c67857SXiubo Li 			return ret;
308089c67857SXiubo Li 
308189c67857SXiubo Li 		if (slot_width)
308289c67857SXiubo Li 			*slot_width = val;
308389c67857SXiubo Li 	}
308489c67857SXiubo Li 
308589c67857SXiubo Li 	return 0;
308689c67857SXiubo Li }
308789c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
308889c67857SXiubo Li 
30893b710356SKuninori Morimoto void snd_soc_of_parse_node_prefix(struct device_node *np,
30905e3cdaa2SKuninori Morimoto 				  struct snd_soc_codec_conf *codec_conf,
30915e3cdaa2SKuninori Morimoto 				  struct device_node *of_node,
30925e3cdaa2SKuninori Morimoto 				  const char *propname)
30935e3cdaa2SKuninori Morimoto {
30945e3cdaa2SKuninori Morimoto 	const char *str;
30955e3cdaa2SKuninori Morimoto 	int ret;
30965e3cdaa2SKuninori Morimoto 
30975e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
30985e3cdaa2SKuninori Morimoto 	if (ret < 0) {
30995e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
31005e3cdaa2SKuninori Morimoto 		return;
31015e3cdaa2SKuninori Morimoto 	}
31025e3cdaa2SKuninori Morimoto 
31035e3cdaa2SKuninori Morimoto 	codec_conf->of_node	= of_node;
31045e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
31055e3cdaa2SKuninori Morimoto }
31063b710356SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
31075e3cdaa2SKuninori Morimoto 
31082bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3109a4a54dd5SStephen Warren 				   const char *propname)
3110a4a54dd5SStephen Warren {
31112bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
3112e3b1e6a1SMark Brown 	int num_routes;
3113a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3114a4a54dd5SStephen Warren 	int i, ret;
3115a4a54dd5SStephen Warren 
3116a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3117c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
311810e8aa9aSMichał Mirosław 		dev_err(card->dev,
311910e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
312010e8aa9aSMichał Mirosław 			propname);
3121a4a54dd5SStephen Warren 		return -EINVAL;
3122a4a54dd5SStephen Warren 	}
3123a4a54dd5SStephen Warren 	num_routes /= 2;
3124a4a54dd5SStephen Warren 	if (!num_routes) {
3125f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3126a4a54dd5SStephen Warren 			propname);
3127a4a54dd5SStephen Warren 		return -EINVAL;
3128a4a54dd5SStephen Warren 	}
3129a4a54dd5SStephen Warren 
3130a86854d0SKees Cook 	routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
3131a4a54dd5SStephen Warren 			      GFP_KERNEL);
3132a4a54dd5SStephen Warren 	if (!routes) {
3133a4a54dd5SStephen Warren 		dev_err(card->dev,
3134f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3135a4a54dd5SStephen Warren 		return -EINVAL;
3136a4a54dd5SStephen Warren 	}
3137a4a54dd5SStephen Warren 
3138a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3139a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3140e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
3141a4a54dd5SStephen Warren 		if (ret) {
3142c871bd0bSMark Brown 			dev_err(card->dev,
3143c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3144c871bd0bSMark Brown 				propname, 2 * i, ret);
3145a4a54dd5SStephen Warren 			return -EINVAL;
3146a4a54dd5SStephen Warren 		}
3147a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3148e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3149a4a54dd5SStephen Warren 		if (ret) {
3150a4a54dd5SStephen Warren 			dev_err(card->dev,
3151c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3152c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3153a4a54dd5SStephen Warren 			return -EINVAL;
3154a4a54dd5SStephen Warren 		}
3155a4a54dd5SStephen Warren 	}
3156a4a54dd5SStephen Warren 
3157f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
3158f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
3159a4a54dd5SStephen Warren 
3160a4a54dd5SStephen Warren 	return 0;
3161a4a54dd5SStephen Warren }
31622bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3163a4a54dd5SStephen Warren 
3164a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3165389cb834SJyri Sarha 				     const char *prefix,
3166389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3167389cb834SJyri Sarha 				     struct device_node **framemaster)
3168a7930ed4SKuninori Morimoto {
3169a7930ed4SKuninori Morimoto 	int ret, i;
3170a7930ed4SKuninori Morimoto 	char prop[128];
3171a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3172a7930ed4SKuninori Morimoto 	int bit, frame;
3173a7930ed4SKuninori Morimoto 	const char *str;
3174a7930ed4SKuninori Morimoto 	struct {
3175a7930ed4SKuninori Morimoto 		char *name;
3176a7930ed4SKuninori Morimoto 		unsigned int val;
3177a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3178a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3179a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3180a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3181a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3182a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3183a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3184a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3185a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3186a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3187a7930ed4SKuninori Morimoto 	};
3188a7930ed4SKuninori Morimoto 
3189a7930ed4SKuninori Morimoto 	if (!prefix)
3190a7930ed4SKuninori Morimoto 		prefix = "";
3191a7930ed4SKuninori Morimoto 
3192a7930ed4SKuninori Morimoto 	/*
31935711c979SKuninori Morimoto 	 * check "dai-format = xxx"
31945711c979SKuninori Morimoto 	 * or    "[prefix]format = xxx"
3195a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3196a7930ed4SKuninori Morimoto 	 */
31975711c979SKuninori Morimoto 	ret = of_property_read_string(np, "dai-format", &str);
31985711c979SKuninori Morimoto 	if (ret < 0) {
3199a7930ed4SKuninori Morimoto 		snprintf(prop, sizeof(prop), "%sformat", prefix);
3200a7930ed4SKuninori Morimoto 		ret = of_property_read_string(np, prop, &str);
32015711c979SKuninori Morimoto 	}
3202a7930ed4SKuninori Morimoto 	if (ret == 0) {
3203a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3204a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3205a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3206a7930ed4SKuninori Morimoto 				break;
3207a7930ed4SKuninori Morimoto 			}
3208a7930ed4SKuninori Morimoto 		}
3209a7930ed4SKuninori Morimoto 	}
3210a7930ed4SKuninori Morimoto 
3211a7930ed4SKuninori Morimoto 	/*
32128c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3213a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3214a7930ed4SKuninori Morimoto 	 */
32158c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
321651930295SJulia Lawall 	if (of_property_read_bool(np, prop))
32178c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
32188c2d6a9fSKuninori Morimoto 	else
32198c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3220a7930ed4SKuninori Morimoto 
3221a7930ed4SKuninori Morimoto 	/*
3222a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3223a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3224a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3225a7930ed4SKuninori Morimoto 	 */
3226a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3227a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3228a7930ed4SKuninori Morimoto 
3229a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3230a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3231a7930ed4SKuninori Morimoto 
3232a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3233a7930ed4SKuninori Morimoto 	case 0x11:
3234a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3235a7930ed4SKuninori Morimoto 		break;
3236a7930ed4SKuninori Morimoto 	case 0x10:
3237a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3238a7930ed4SKuninori Morimoto 		break;
3239a7930ed4SKuninori Morimoto 	case 0x01:
3240a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3241a7930ed4SKuninori Morimoto 		break;
3242a7930ed4SKuninori Morimoto 	default:
3243a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3244a7930ed4SKuninori Morimoto 		break;
3245a7930ed4SKuninori Morimoto 	}
3246a7930ed4SKuninori Morimoto 
3247a7930ed4SKuninori Morimoto 	/*
3248a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3249a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3250a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3251a7930ed4SKuninori Morimoto 	 */
3252a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3253a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3254389cb834SJyri Sarha 	if (bit && bitclkmaster)
3255389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3256a7930ed4SKuninori Morimoto 
3257a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3258a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3259389cb834SJyri Sarha 	if (frame && framemaster)
3260389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3261a7930ed4SKuninori Morimoto 
3262a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3263a7930ed4SKuninori Morimoto 	case 0x11:
3264a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3265a7930ed4SKuninori Morimoto 		break;
3266a7930ed4SKuninori Morimoto 	case 0x10:
3267a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3268a7930ed4SKuninori Morimoto 		break;
3269a7930ed4SKuninori Morimoto 	case 0x01:
3270a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3271a7930ed4SKuninori Morimoto 		break;
3272a7930ed4SKuninori Morimoto 	default:
3273a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3274a7930ed4SKuninori Morimoto 		break;
3275a7930ed4SKuninori Morimoto 	}
3276a7930ed4SKuninori Morimoto 
3277a7930ed4SKuninori Morimoto 	return format;
3278a7930ed4SKuninori Morimoto }
3279a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3280a7930ed4SKuninori Morimoto 
3281a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
3282a180e8b9SKuninori Morimoto {
328309d4cc03SKuninori Morimoto 	struct snd_soc_component *component;
3284c1e230f0SKuninori Morimoto 	struct snd_soc_dai_link_component dlc;
3285a180e8b9SKuninori Morimoto 	int ret;
3286a180e8b9SKuninori Morimoto 
3287c1e230f0SKuninori Morimoto 	dlc.of_node	= of_graph_get_port_parent(ep);
3288c1e230f0SKuninori Morimoto 	dlc.name	= NULL;
3289a180e8b9SKuninori Morimoto 	/*
3290a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
3291a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
3292a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
3293a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
3294a180e8b9SKuninori Morimoto 	 */
3295a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
3296a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
3297c1e230f0SKuninori Morimoto 	component = soc_find_component(&dlc);
32982c7b1704SKuninori Morimoto 	if (component)
32992c7b1704SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_id(component, ep);
3300a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
3301a180e8b9SKuninori Morimoto 
3302c1e230f0SKuninori Morimoto 	of_node_put(dlc.of_node);
3303c0a480d1STony Lindgren 
3304a180e8b9SKuninori Morimoto 	return ret;
3305a180e8b9SKuninori Morimoto }
3306a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3307a180e8b9SKuninori Morimoto 
33081ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args,
3309cb470087SKuninori Morimoto 				const char **dai_name)
3310cb470087SKuninori Morimoto {
3311cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
33123e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
331393b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3314cb470087SKuninori Morimoto 
3315cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3316368dee94SKuninori Morimoto 	for_each_component(pos) {
3317c0834440SKuninori Morimoto 		component_of_node = soc_component_to_node(pos);
33183e0aa8d8SJyri Sarha 
33193e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
3320cb470087SKuninori Morimoto 			continue;
3321cb470087SKuninori Morimoto 
3322a2a34175SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3323a2a34175SKuninori Morimoto 		if (ret == -ENOTSUPP) {
332458bf4179SKuninori Morimoto 			struct snd_soc_dai *dai;
33256833c452SKuninori Morimoto 			int id = -1;
33266833c452SKuninori Morimoto 
332793b0f3eeSJean-Francois Moine 			switch (args->args_count) {
33286833c452SKuninori Morimoto 			case 0:
33296833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
33306833c452SKuninori Morimoto 				break;
33316833c452SKuninori Morimoto 			case 1:
333293b0f3eeSJean-Francois Moine 				id = args->args[0];
33336833c452SKuninori Morimoto 				break;
33346833c452SKuninori Morimoto 			default:
33356833c452SKuninori Morimoto 				/* not supported */
3336cb470087SKuninori Morimoto 				break;
3337cb470087SKuninori Morimoto 			}
3338cb470087SKuninori Morimoto 
33396833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
33406833c452SKuninori Morimoto 				ret = -EINVAL;
33413dcba280SNicolin Chen 				continue;
33426833c452SKuninori Morimoto 			}
3343e41975edSXiubo Li 
3344e41975edSXiubo Li 			ret = 0;
3345e41975edSXiubo Li 
334658bf4179SKuninori Morimoto 			/* find target DAI */
334715a0c645SKuninori Morimoto 			for_each_component_dais(pos, dai) {
334858bf4179SKuninori Morimoto 				if (id == 0)
334958bf4179SKuninori Morimoto 					break;
335058bf4179SKuninori Morimoto 				id--;
335158bf4179SKuninori Morimoto 			}
335258bf4179SKuninori Morimoto 
335358bf4179SKuninori Morimoto 			*dai_name = dai->driver->name;
3354e41975edSXiubo Li 			if (!*dai_name)
3355e41975edSXiubo Li 				*dai_name = pos->name;
33566833c452SKuninori Morimoto 		}
33576833c452SKuninori Morimoto 
3358cb470087SKuninori Morimoto 		break;
3359cb470087SKuninori Morimoto 	}
3360cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
336193b0f3eeSJean-Francois Moine 	return ret;
336293b0f3eeSJean-Francois Moine }
33631ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
336493b0f3eeSJean-Francois Moine 
336593b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
336693b0f3eeSJean-Francois Moine 			    const char **dai_name)
336793b0f3eeSJean-Francois Moine {
336893b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
336993b0f3eeSJean-Francois Moine 	int ret;
337093b0f3eeSJean-Francois Moine 
337193b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
337293b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
337393b0f3eeSJean-Francois Moine 	if (ret)
337493b0f3eeSJean-Francois Moine 		return ret;
337593b0f3eeSJean-Francois Moine 
337693b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3377cb470087SKuninori Morimoto 
3378cb470087SKuninori Morimoto 	of_node_put(args.np);
3379cb470087SKuninori Morimoto 
3380cb470087SKuninori Morimoto 	return ret;
3381cb470087SKuninori Morimoto }
3382cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3383cb470087SKuninori Morimoto 
338493b0f3eeSJean-Francois Moine /*
338594685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
338694685763SSylwester Nawrocki  * @dai_link: DAI link
338794685763SSylwester Nawrocki  *
338894685763SSylwester Nawrocki  * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
338994685763SSylwester Nawrocki  */
339094685763SSylwester Nawrocki void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
339194685763SSylwester Nawrocki {
33923db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *component;
339394685763SSylwester Nawrocki 	int index;
339494685763SSylwester Nawrocki 
33953db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
339694685763SSylwester Nawrocki 		if (!component->of_node)
339794685763SSylwester Nawrocki 			break;
339894685763SSylwester Nawrocki 		of_node_put(component->of_node);
339994685763SSylwester Nawrocki 		component->of_node = NULL;
340094685763SSylwester Nawrocki 	}
340194685763SSylwester Nawrocki }
340294685763SSylwester Nawrocki EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
340394685763SSylwester Nawrocki 
340494685763SSylwester Nawrocki /*
340593b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
340693b0f3eeSJean-Francois Moine  * @dev: Card device
340793b0f3eeSJean-Francois Moine  * @of_node: Device node
340893b0f3eeSJean-Francois Moine  * @dai_link: DAI link
340993b0f3eeSJean-Francois Moine  *
341093b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
341193b0f3eeSJean-Francois Moine  * 'sound-dai'.
341293b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
341394685763SSylwester Nawrocki  * The device nodes in the array (of_node) must be dereferenced by calling
341494685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs() on @dai_link.
341593b0f3eeSJean-Francois Moine  *
341693b0f3eeSJean-Francois Moine  * Returns 0 for success
341793b0f3eeSJean-Francois Moine  */
341893b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
341993b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
342093b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
342193b0f3eeSJean-Francois Moine {
342293b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
342393b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
342493b0f3eeSJean-Francois Moine 	char *name;
342593b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
342693b0f3eeSJean-Francois Moine 
342793b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
342893b0f3eeSJean-Francois Moine 	name = "sound-dai";
342993b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
343093b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
343193b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
343293b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
343393b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
343493b0f3eeSJean-Francois Moine 		else
343593b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
343693b0f3eeSJean-Francois Moine 		return num_codecs;
343793b0f3eeSJean-Francois Moine 	}
3438a86854d0SKees Cook 	component = devm_kcalloc(dev,
3439a86854d0SKees Cook 				 num_codecs, sizeof(*component),
344093b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
344193b0f3eeSJean-Francois Moine 	if (!component)
344293b0f3eeSJean-Francois Moine 		return -ENOMEM;
344393b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
344493b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
344593b0f3eeSJean-Francois Moine 
344693b0f3eeSJean-Francois Moine 	/* Parse the list */
34473db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
344893b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
344993b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
345093b0f3eeSJean-Francois Moine 						 index, &args);
345193b0f3eeSJean-Francois Moine 		if (ret)
345293b0f3eeSJean-Francois Moine 			goto err;
345393b0f3eeSJean-Francois Moine 		component->of_node = args.np;
345493b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
345593b0f3eeSJean-Francois Moine 		if (ret < 0)
345693b0f3eeSJean-Francois Moine 			goto err;
345793b0f3eeSJean-Francois Moine 	}
345893b0f3eeSJean-Francois Moine 	return 0;
345993b0f3eeSJean-Francois Moine err:
346094685763SSylwester Nawrocki 	snd_soc_of_put_dai_link_codecs(dai_link);
346193b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
346293b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
346393b0f3eeSJean-Francois Moine 	return ret;
346493b0f3eeSJean-Francois Moine }
346593b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
346693b0f3eeSJean-Francois Moine 
3467c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3468db2a4165SFrank Mandarino {
34696553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
3470fb257897SMark Brown 	snd_soc_util_init();
3471fb257897SMark Brown 
3472db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3473db2a4165SFrank Mandarino }
34744abe8e16SMark Brown module_init(snd_soc_init);
3475db2a4165SFrank Mandarino 
34767d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3477db2a4165SFrank Mandarino {
3478fb257897SMark Brown 	snd_soc_util_exit();
34796553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3480fb257897SMark Brown 
3481db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3482db2a4165SFrank Mandarino }
3483db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3484db2a4165SFrank Mandarino 
3485db2a4165SFrank Mandarino /* Module information */
3486d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3487db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3488db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
34898b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3490