xref: /openbmc/linux/sound/soc/soc-core.c (revision 218fe9b7ec7f32c10a07539365488d80af7b0084)
1873486edSKuninori Morimoto // SPDX-License-Identifier: GPL-2.0+
2873486edSKuninori Morimoto //
3873486edSKuninori Morimoto // soc-core.c  --  ALSA SoC Audio Layer
4873486edSKuninori Morimoto //
5873486edSKuninori Morimoto // Copyright 2005 Wolfson Microelectronics PLC.
6873486edSKuninori Morimoto // Copyright 2005 Openedhand Ltd.
7873486edSKuninori Morimoto // Copyright (C) 2010 Slimlogic Ltd.
8873486edSKuninori Morimoto // Copyright (C) 2010 Texas Instruments Inc.
9873486edSKuninori Morimoto //
10873486edSKuninori Morimoto // Author: Liam Girdwood <lrg@slimlogic.co.uk>
11873486edSKuninori Morimoto //         with code, comments and ideas from :-
12873486edSKuninori Morimoto //         Richard Purdie <richard@openedhand.com>
13873486edSKuninori Morimoto //
14873486edSKuninori Morimoto //  TODO:
15873486edSKuninori Morimoto //   o Add hw rules to enforce rates, etc.
16873486edSKuninori Morimoto //   o More testing with other codecs/machines.
17873486edSKuninori Morimoto //   o Add more codecs and platforms to ensure good API coverage.
18873486edSKuninori Morimoto //   o Support TDM on PCM and I2S
19db2a4165SFrank Mandarino 
20db2a4165SFrank Mandarino #include <linux/module.h>
21db2a4165SFrank Mandarino #include <linux/moduleparam.h>
22db2a4165SFrank Mandarino #include <linux/init.h>
23db2a4165SFrank Mandarino #include <linux/delay.h>
24db2a4165SFrank Mandarino #include <linux/pm.h>
25db2a4165SFrank Mandarino #include <linux/bitops.h>
2612ef193dSTroy Kisky #include <linux/debugfs.h>
27db2a4165SFrank Mandarino #include <linux/platform_device.h>
28741a509fSMarkus Pargmann #include <linux/pinctrl/consumer.h>
29f0e8ed85SMark Brown #include <linux/ctype.h>
305a0e3ad6STejun Heo #include <linux/slab.h>
31bec4fa05SStephen Warren #include <linux/of.h>
32a180e8b9SKuninori Morimoto #include <linux/of_graph.h>
33345233d7SLiam Girdwood #include <linux/dmi.h>
34db2a4165SFrank Mandarino #include <sound/core.h>
353028eb8cSMark Brown #include <sound/jack.h>
36db2a4165SFrank Mandarino #include <sound/pcm.h>
37db2a4165SFrank Mandarino #include <sound/pcm_params.h>
38db2a4165SFrank Mandarino #include <sound/soc.h>
3901d7584cSLiam Girdwood #include <sound/soc-dpcm.h>
408a978234SLiam Girdwood #include <sound/soc-topology.h>
41db2a4165SFrank Mandarino #include <sound/initval.h>
42db2a4165SFrank Mandarino 
43a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
44a8b1d34fSMark Brown #include <trace/events/asoc.h>
45a8b1d34fSMark Brown 
46f0fba2adSLiam Girdwood #define NAME_SIZE	32
47f0fba2adSLiam Girdwood 
48384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
498a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root;
508a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
51384c89e2SMark Brown #endif
52384c89e2SMark Brown 
53c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
54030e79f6SKuninori Morimoto static LIST_HEAD(component_list);
55e894efefSSrinivas Kandagatla static LIST_HEAD(unbind_card_list);
56c5af3a2eSMark Brown 
57368dee94SKuninori Morimoto #define for_each_component(component)			\
58368dee94SKuninori Morimoto 	list_for_each_entry(component, &component_list, list)
59368dee94SKuninori Morimoto 
60db2a4165SFrank Mandarino /*
61587c9844SKuninori Morimoto  * This is used if driver don't need to have CPU/Codec/Platform
62587c9844SKuninori Morimoto  * dai_link. see soc.h
63587c9844SKuninori Morimoto  */
64587c9844SKuninori Morimoto struct snd_soc_dai_link_component null_dailink_component[0];
65587c9844SKuninori Morimoto EXPORT_SYMBOL_GPL(null_dailink_component);
66587c9844SKuninori Morimoto 
67587c9844SKuninori Morimoto /*
68db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
69db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
70db2a4165SFrank Mandarino  * between two audio tracks.
71db2a4165SFrank Mandarino  */
72db2a4165SFrank Mandarino static int pmdown_time = 5000;
73db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
74db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
75db2a4165SFrank Mandarino 
760faf1237SYueHaibing #ifdef CONFIG_DMI
772c7b696aSMarcel Ziswiler /*
782c7b696aSMarcel Ziswiler  * If a DMI filed contain strings in this blacklist (e.g.
7998faf436SMengdong Lin  * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
8098faf436SMengdong Lin  * as invalid and dropped when setting the card long name from DMI info.
8198faf436SMengdong Lin  */
8298faf436SMengdong Lin static const char * const dmi_blacklist[] = {
8398faf436SMengdong Lin 	"To be filled by OEM",
8498faf436SMengdong Lin 	"TBD by OEM",
8598faf436SMengdong Lin 	"Default String",
8698faf436SMengdong Lin 	"Board Manufacturer",
8798faf436SMengdong Lin 	"Board Vendor Name",
8898faf436SMengdong Lin 	"Board Product Name",
8998faf436SMengdong Lin 	NULL,	/* terminator */
9098faf436SMengdong Lin };
910faf1237SYueHaibing #endif
9298faf436SMengdong Lin 
93dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
94dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
95dbe21408SMark Brown {
9636ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
97dbe21408SMark Brown 
98f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
99dbe21408SMark Brown }
100dbe21408SMark Brown 
101dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
102dbe21408SMark Brown 			       struct device_attribute *attr,
103dbe21408SMark Brown 			       const char *buf, size_t count)
104dbe21408SMark Brown {
10536ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
106c593b520SMark Brown 	int ret;
107dbe21408SMark Brown 
108b785a492SJingoo Han 	ret = kstrtol(buf, 10, &rtd->pmdown_time);
109c593b520SMark Brown 	if (ret)
110c593b520SMark Brown 		return ret;
111dbe21408SMark Brown 
112dbe21408SMark Brown 	return count;
113dbe21408SMark Brown }
114dbe21408SMark Brown 
115dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
116dbe21408SMark Brown 
117d29697dcSTakashi Iwai static struct attribute *soc_dev_attrs[] = {
118d29697dcSTakashi Iwai 	&dev_attr_pmdown_time.attr,
119d29697dcSTakashi Iwai 	NULL
120d29697dcSTakashi Iwai };
121d29697dcSTakashi Iwai 
122d29697dcSTakashi Iwai static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
123d29697dcSTakashi Iwai 				       struct attribute *attr, int idx)
124d29697dcSTakashi Iwai {
125d29697dcSTakashi Iwai 	struct device *dev = kobj_to_dev(kobj);
126d29697dcSTakashi Iwai 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
127d29697dcSTakashi Iwai 
128d918a376SKuninori Morimoto 	if (!rtd)
129d918a376SKuninori Morimoto 		return 0;
130d918a376SKuninori Morimoto 
131d29697dcSTakashi Iwai 	if (attr == &dev_attr_pmdown_time.attr)
132d29697dcSTakashi Iwai 		return attr->mode; /* always visible */
1333b6eed8dSKuninori Morimoto 	return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
134d29697dcSTakashi Iwai }
135d29697dcSTakashi Iwai 
136d29697dcSTakashi Iwai static const struct attribute_group soc_dapm_dev_group = {
137d29697dcSTakashi Iwai 	.attrs = soc_dapm_dev_attrs,
138d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
139d29697dcSTakashi Iwai };
140d29697dcSTakashi Iwai 
141f7e73b26SMark Brown static const struct attribute_group soc_dev_group = {
142d29697dcSTakashi Iwai 	.attrs = soc_dev_attrs,
143d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
144d29697dcSTakashi Iwai };
145d29697dcSTakashi Iwai 
146d29697dcSTakashi Iwai static const struct attribute_group *soc_dev_attr_groups[] = {
147d29697dcSTakashi Iwai 	&soc_dapm_dev_group,
148f7e73b26SMark Brown 	&soc_dev_group,
149d29697dcSTakashi Iwai 	NULL
150d29697dcSTakashi Iwai };
151d29697dcSTakashi Iwai 
1522624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
15381c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component)
154e73f3de5SRussell King {
1556553bf06SLars-Peter Clausen 	if (!component->card->debugfs_card_root)
1566553bf06SLars-Peter Clausen 		return;
1576553bf06SLars-Peter Clausen 
15881c7cfd1SLars-Peter Clausen 	if (component->debugfs_prefix) {
15981c7cfd1SLars-Peter Clausen 		char *name;
160e73f3de5SRussell King 
16181c7cfd1SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s:%s",
16281c7cfd1SLars-Peter Clausen 			component->debugfs_prefix, component->name);
16381c7cfd1SLars-Peter Clausen 		if (name) {
16481c7cfd1SLars-Peter Clausen 			component->debugfs_root = debugfs_create_dir(name,
16581c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
16681c7cfd1SLars-Peter Clausen 			kfree(name);
16781c7cfd1SLars-Peter Clausen 		}
16881c7cfd1SLars-Peter Clausen 	} else {
16981c7cfd1SLars-Peter Clausen 		component->debugfs_root = debugfs_create_dir(component->name,
17081c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
171e73f3de5SRussell King 	}
172e73f3de5SRussell King 
17381c7cfd1SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
17481c7cfd1SLars-Peter Clausen 		component->debugfs_root);
17581c7cfd1SLars-Peter Clausen }
17681c7cfd1SLars-Peter Clausen 
17781c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
17881c7cfd1SLars-Peter Clausen {
179ad64bfbdSKuninori Morimoto 	if (!component->debugfs_root)
180ad64bfbdSKuninori Morimoto 		return;
18181c7cfd1SLars-Peter Clausen 	debugfs_remove_recursive(component->debugfs_root);
182ad64bfbdSKuninori Morimoto 	component->debugfs_root = NULL;
18381c7cfd1SLars-Peter Clausen }
18481c7cfd1SLars-Peter Clausen 
185c15b2a1dSPeng Donglin static int dai_list_show(struct seq_file *m, void *v)
186f3208780SMark Brown {
1871438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
188f3208780SMark Brown 	struct snd_soc_dai *dai;
189f3208780SMark Brown 
19034e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
19134e81ab4SLars-Peter Clausen 
192368dee94SKuninori Morimoto 	for_each_component(component)
19315a0c645SKuninori Morimoto 		for_each_component_dais(component, dai)
194700c17caSDonglin Peng 			seq_printf(m, "%s\n", dai->name);
195f3208780SMark Brown 
19634e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
19734e81ab4SLars-Peter Clausen 
198700c17caSDonglin Peng 	return 0;
199700c17caSDonglin Peng }
200c15b2a1dSPeng Donglin DEFINE_SHOW_ATTRIBUTE(dai_list);
201f3208780SMark Brown 
202db795f9bSKuninori Morimoto static int component_list_show(struct seq_file *m, void *v)
203db795f9bSKuninori Morimoto {
204db795f9bSKuninori Morimoto 	struct snd_soc_component *component;
205db795f9bSKuninori Morimoto 
206db795f9bSKuninori Morimoto 	mutex_lock(&client_mutex);
207db795f9bSKuninori Morimoto 
208368dee94SKuninori Morimoto 	for_each_component(component)
209db795f9bSKuninori Morimoto 		seq_printf(m, "%s\n", component->name);
210db795f9bSKuninori Morimoto 
211db795f9bSKuninori Morimoto 	mutex_unlock(&client_mutex);
212db795f9bSKuninori Morimoto 
213db795f9bSKuninori Morimoto 	return 0;
214db795f9bSKuninori Morimoto }
215db795f9bSKuninori Morimoto DEFINE_SHOW_ATTRIBUTE(component_list);
216db795f9bSKuninori Morimoto 
217a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
218a6052154SJarkko Nikula {
219a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
2208a9dab1aSMark Brown 						     snd_soc_debugfs_root);
2213a45b867SJarkko Nikula 
222fee531d6SGreg Kroah-Hartman 	debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root,
2233a45b867SJarkko Nikula 			   &card->pop_time);
224d8ca7a0aSKuninori Morimoto 
225d8ca7a0aSKuninori Morimoto 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
226a6052154SJarkko Nikula }
227a6052154SJarkko Nikula 
228a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
229a6052154SJarkko Nikula {
230a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
23129040d1aSKuninori Morimoto 	card->debugfs_card_root = NULL;
232a6052154SJarkko Nikula }
233a6052154SJarkko Nikula 
2346553bf06SLars-Peter Clausen static void snd_soc_debugfs_init(void)
2356553bf06SLars-Peter Clausen {
2366553bf06SLars-Peter Clausen 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
2376553bf06SLars-Peter Clausen 
238fee531d6SGreg Kroah-Hartman 	debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
239fee531d6SGreg Kroah-Hartman 			    &dai_list_fops);
240db795f9bSKuninori Morimoto 
241fee531d6SGreg Kroah-Hartman 	debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
242fee531d6SGreg Kroah-Hartman 			    &component_list_fops);
2436553bf06SLars-Peter Clausen }
2446553bf06SLars-Peter Clausen 
2456553bf06SLars-Peter Clausen static void snd_soc_debugfs_exit(void)
2466553bf06SLars-Peter Clausen {
2476553bf06SLars-Peter Clausen 	debugfs_remove_recursive(snd_soc_debugfs_root);
2486553bf06SLars-Peter Clausen }
2496553bf06SLars-Peter Clausen 
2502624d5faSMark Brown #else
2512624d5faSMark Brown 
25281c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs(
25381c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
2542624d5faSMark Brown {
2552624d5faSMark Brown }
2562624d5faSMark Brown 
25781c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs(
25881c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
259731f1ab2SSebastien Guiriec {
260731f1ab2SSebastien Guiriec }
261731f1ab2SSebastien Guiriec 
262b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
263b95fccbcSAxel Lin {
264b95fccbcSAxel Lin }
265b95fccbcSAxel Lin 
266b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
267b95fccbcSAxel Lin {
268b95fccbcSAxel Lin }
2696553bf06SLars-Peter Clausen 
2706553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_init(void)
2716553bf06SLars-Peter Clausen {
2726553bf06SLars-Peter Clausen }
2736553bf06SLars-Peter Clausen 
2746553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_exit(void)
2756553bf06SLars-Peter Clausen {
2766553bf06SLars-Peter Clausen }
2776553bf06SLars-Peter Clausen 
2782624d5faSMark Brown #endif
2792624d5faSMark Brown 
2808ec241c4SKuninori Morimoto /*
2818ec241c4SKuninori Morimoto  * This is glue code between snd_pcm_lib_ioctl() and
2828ec241c4SKuninori Morimoto  * snd_soc_component_driver :: ioctl
2838ec241c4SKuninori Morimoto  */
2848ec241c4SKuninori Morimoto int snd_soc_pcm_lib_ioctl(struct snd_soc_component *component,
2858ec241c4SKuninori Morimoto 			  struct snd_pcm_substream *substream,
2868ec241c4SKuninori Morimoto 			  unsigned int cmd, void *arg)
2878ec241c4SKuninori Morimoto {
2888ec241c4SKuninori Morimoto 	return snd_pcm_lib_ioctl(substream, cmd, arg);
2898ec241c4SKuninori Morimoto }
2908ec241c4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_pcm_lib_ioctl);
2918ec241c4SKuninori Morimoto 
292a0ac4411SKuninori Morimoto static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
293a0ac4411SKuninori Morimoto 			      struct snd_soc_component *component)
294a0ac4411SKuninori Morimoto {
295a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
2962b544dd7SKuninori Morimoto 	struct snd_soc_component *comp;
297a0ac4411SKuninori Morimoto 
2982b544dd7SKuninori Morimoto 	for_each_rtd_components(rtd, rtdcom, comp) {
299a0ac4411SKuninori Morimoto 		/* already connected */
3002b544dd7SKuninori Morimoto 		if (comp == component)
301a0ac4411SKuninori Morimoto 			return 0;
302a0ac4411SKuninori Morimoto 	}
303a0ac4411SKuninori Morimoto 
304353e16bfSKuninori Morimoto 	/*
305353e16bfSKuninori Morimoto 	 * created rtdcom here will be freed when rtd->dev was freed.
306353e16bfSKuninori Morimoto 	 * see
307353e16bfSKuninori Morimoto 	 *	soc_free_pcm_runtime() :: device_unregister(rtd->dev)
308353e16bfSKuninori Morimoto 	 */
309353e16bfSKuninori Morimoto 	rtdcom = devm_kzalloc(rtd->dev, sizeof(*rtdcom), GFP_KERNEL);
31032d2c172SKuninori Morimoto 	if (!rtdcom)
311a0ac4411SKuninori Morimoto 		return -ENOMEM;
312a0ac4411SKuninori Morimoto 
31332d2c172SKuninori Morimoto 	rtdcom->component = component;
31432d2c172SKuninori Morimoto 	INIT_LIST_HEAD(&rtdcom->list);
315a0ac4411SKuninori Morimoto 
316353e16bfSKuninori Morimoto 	/*
317353e16bfSKuninori Morimoto 	 * When rtd was freed, created rtdcom here will be
318353e16bfSKuninori Morimoto 	 * also freed.
319353e16bfSKuninori Morimoto 	 * And we don't need to call list_del(&rtdcom->list)
320353e16bfSKuninori Morimoto 	 * when freed, because rtd is also freed.
321353e16bfSKuninori Morimoto 	 */
32232d2c172SKuninori Morimoto 	list_add_tail(&rtdcom->list, &rtd->component_list);
323a0ac4411SKuninori Morimoto 
324a0ac4411SKuninori Morimoto 	return 0;
325a0ac4411SKuninori Morimoto }
326a0ac4411SKuninori Morimoto 
327a0ac4411SKuninori Morimoto struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
328a0ac4411SKuninori Morimoto 						const char *driver_name)
329a0ac4411SKuninori Morimoto {
330a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
3312b544dd7SKuninori Morimoto 	struct snd_soc_component *component;
332a0ac4411SKuninori Morimoto 
333971da24cSKuninori Morimoto 	if (!driver_name)
334971da24cSKuninori Morimoto 		return NULL;
335971da24cSKuninori Morimoto 
336a33c0d16SKuninori Morimoto 	/*
337a33c0d16SKuninori Morimoto 	 * NOTE
338a33c0d16SKuninori Morimoto 	 *
339a33c0d16SKuninori Morimoto 	 * snd_soc_rtdcom_lookup() will find component from rtd by using
340a33c0d16SKuninori Morimoto 	 * specified driver name.
341a33c0d16SKuninori Morimoto 	 * But, if many components which have same driver name are connected
342a33c0d16SKuninori Morimoto 	 * to 1 rtd, this function will return 1st found component.
343a33c0d16SKuninori Morimoto 	 */
3442b544dd7SKuninori Morimoto 	for_each_rtd_components(rtd, rtdcom, component) {
3452b544dd7SKuninori Morimoto 		const char *component_name = component->driver->name;
346971da24cSKuninori Morimoto 
347971da24cSKuninori Morimoto 		if (!component_name)
348971da24cSKuninori Morimoto 			continue;
349971da24cSKuninori Morimoto 
350971da24cSKuninori Morimoto 		if ((component_name == driver_name) ||
351971da24cSKuninori Morimoto 		    strcmp(component_name, driver_name) == 0)
352a0ac4411SKuninori Morimoto 			return rtdcom->component;
353a0ac4411SKuninori Morimoto 	}
354a0ac4411SKuninori Morimoto 
355a0ac4411SKuninori Morimoto 	return NULL;
356a0ac4411SKuninori Morimoto }
357031734b7SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
358a0ac4411SKuninori Morimoto 
35918dd66eaSKuninori Morimoto static struct snd_soc_component
36018dd66eaSKuninori Morimoto *snd_soc_lookup_component_nolocked(struct device *dev, const char *driver_name)
361b8132657SKuninori Morimoto {
362b8132657SKuninori Morimoto 	struct snd_soc_component *component;
3635bd7e08bSKuninori Morimoto 	struct snd_soc_component *found_component;
364b8132657SKuninori Morimoto 
3655bd7e08bSKuninori Morimoto 	found_component = NULL;
366b8132657SKuninori Morimoto 	for_each_component(component) {
3675bd7e08bSKuninori Morimoto 		if ((dev == component->dev) &&
3685bd7e08bSKuninori Morimoto 		    (!driver_name ||
3695bd7e08bSKuninori Morimoto 		     (driver_name == component->driver->name) ||
3705bd7e08bSKuninori Morimoto 		     (strcmp(component->driver->name, driver_name) == 0))) {
3715bd7e08bSKuninori Morimoto 			found_component = component;
372b8132657SKuninori Morimoto 			break;
373b8132657SKuninori Morimoto 		}
3745bd7e08bSKuninori Morimoto 	}
375b8132657SKuninori Morimoto 
3765bd7e08bSKuninori Morimoto 	return found_component;
377b8132657SKuninori Morimoto }
37818dd66eaSKuninori Morimoto 
37918dd66eaSKuninori Morimoto struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
38018dd66eaSKuninori Morimoto 						   const char *driver_name)
38118dd66eaSKuninori Morimoto {
38218dd66eaSKuninori Morimoto 	struct snd_soc_component *component;
38318dd66eaSKuninori Morimoto 
38418dd66eaSKuninori Morimoto 	mutex_lock(&client_mutex);
38518dd66eaSKuninori Morimoto 	component = snd_soc_lookup_component_nolocked(dev, driver_name);
38618dd66eaSKuninori Morimoto 	mutex_unlock(&client_mutex);
38718dd66eaSKuninori Morimoto 
38818dd66eaSKuninori Morimoto 	return component;
38918dd66eaSKuninori Morimoto }
390b8132657SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
391b8132657SKuninori Morimoto 
39247c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
39347c88fffSLiam Girdwood 		const char *dai_link, int stream)
39447c88fffSLiam Girdwood {
3951a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
39647c88fffSLiam Girdwood 
397bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
3981a497983SMengdong Lin 		if (rtd->dai_link->no_pcm &&
3991a497983SMengdong Lin 			!strcmp(rtd->dai_link->name, dai_link))
4001a497983SMengdong Lin 			return rtd->pcm->streams[stream].substream;
40147c88fffSLiam Girdwood 	}
402f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
40347c88fffSLiam Girdwood 	return NULL;
40447c88fffSLiam Girdwood }
40547c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
40647c88fffSLiam Girdwood 
40775ab9eb6SKuninori Morimoto static const struct snd_soc_ops null_snd_soc_ops;
40875ab9eb6SKuninori Morimoto 
4096e864344SKuninori Morimoto static void soc_release_rtd_dev(struct device *dev)
4106e864344SKuninori Morimoto {
4116e864344SKuninori Morimoto 	/* "dev" means "rtd->dev" */
4126e864344SKuninori Morimoto 	kfree(dev);
4136e864344SKuninori Morimoto }
4146e864344SKuninori Morimoto 
4151c93a9e0SKuninori Morimoto static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
4161c93a9e0SKuninori Morimoto {
4176e864344SKuninori Morimoto 	if (!rtd)
4186e864344SKuninori Morimoto 		return;
4196e864344SKuninori Morimoto 
420753ace0aSKuninori Morimoto 	list_del(&rtd->list);
421b7c5bc45SKuninori Morimoto 
4229c9b6520SCurtis Malainey 	if (delayed_work_pending(&rtd->delayed_work))
4230ced7b05SKuninori Morimoto 		flush_delayed_work(&rtd->delayed_work);
4240ced7b05SKuninori Morimoto 	snd_soc_pcm_component_free(rtd);
4250ced7b05SKuninori Morimoto 
426b7c5bc45SKuninori Morimoto 	/*
427b7c5bc45SKuninori Morimoto 	 * we don't need to call kfree() for rtd->dev
428b7c5bc45SKuninori Morimoto 	 * see
429b7c5bc45SKuninori Morimoto 	 *	soc_release_rtd_dev()
430d918a376SKuninori Morimoto 	 *
431d918a376SKuninori Morimoto 	 * We don't need rtd->dev NULL check, because
432d918a376SKuninori Morimoto 	 * it is alloced *before* rtd.
433d918a376SKuninori Morimoto 	 * see
434d918a376SKuninori Morimoto 	 *	soc_new_pcm_runtime()
435b7c5bc45SKuninori Morimoto 	 */
436b7c5bc45SKuninori Morimoto 	device_unregister(rtd->dev);
4371c93a9e0SKuninori Morimoto }
4381c93a9e0SKuninori Morimoto 
4394bf2e385SCurtis Malainey static void close_delayed_work(struct work_struct *work) {
4404bf2e385SCurtis Malainey 	struct snd_soc_pcm_runtime *rtd =
4414bf2e385SCurtis Malainey 			container_of(work, struct snd_soc_pcm_runtime,
4424bf2e385SCurtis Malainey 				     delayed_work.work);
4434bf2e385SCurtis Malainey 
4444bf2e385SCurtis Malainey 	if (rtd->close_delayed_work_func)
4454bf2e385SCurtis Malainey 		rtd->close_delayed_work_func(rtd);
4464bf2e385SCurtis Malainey }
4474bf2e385SCurtis Malainey 
4481a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
4491a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
4501a497983SMengdong Lin {
4511a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
452d918a376SKuninori Morimoto 	struct device *dev;
4536e864344SKuninori Morimoto 	int ret;
4541a497983SMengdong Lin 
4556e864344SKuninori Morimoto 	/*
456d918a376SKuninori Morimoto 	 * for rtd->dev
457d918a376SKuninori Morimoto 	 */
458d918a376SKuninori Morimoto 	dev = kzalloc(sizeof(struct device), GFP_KERNEL);
459d918a376SKuninori Morimoto 	if (!dev)
460d918a376SKuninori Morimoto 		return NULL;
461d918a376SKuninori Morimoto 
462d918a376SKuninori Morimoto 	dev->parent	= card->dev;
463d918a376SKuninori Morimoto 	dev->release	= soc_release_rtd_dev;
464d918a376SKuninori Morimoto 	dev->groups	= soc_dev_attr_groups;
465d918a376SKuninori Morimoto 
466d918a376SKuninori Morimoto 	dev_set_name(dev, "%s", dai_link->name);
467d918a376SKuninori Morimoto 
468d918a376SKuninori Morimoto 	ret = device_register(dev);
469d918a376SKuninori Morimoto 	if (ret < 0) {
470d918a376SKuninori Morimoto 		put_device(dev); /* soc_release_rtd_dev */
471d918a376SKuninori Morimoto 		return NULL;
472d918a376SKuninori Morimoto 	}
473d918a376SKuninori Morimoto 
474d918a376SKuninori Morimoto 	/*
4756e864344SKuninori Morimoto 	 * for rtd
4766e864344SKuninori Morimoto 	 */
4774dc0e7dfSKuninori Morimoto 	rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL);
4781a497983SMengdong Lin 	if (!rtd)
4796e864344SKuninori Morimoto 		goto free_rtd;
4801a497983SMengdong Lin 
481d918a376SKuninori Morimoto 	rtd->dev = dev;
482d918a376SKuninori Morimoto 	dev_set_drvdata(dev, rtd);
4834bf2e385SCurtis Malainey 	INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
484d918a376SKuninori Morimoto 
4856e864344SKuninori Morimoto 	/*
4866e864344SKuninori Morimoto 	 * for rtd->codec_dais
4876e864344SKuninori Morimoto 	 */
4884dc0e7dfSKuninori Morimoto 	rtd->codec_dais = devm_kcalloc(dev, dai_link->num_codecs,
4896396bb22SKees Cook 					sizeof(struct snd_soc_dai *),
4901a497983SMengdong Lin 					GFP_KERNEL);
4916e864344SKuninori Morimoto 	if (!rtd->codec_dais)
4926e864344SKuninori Morimoto 		goto free_rtd;
4936e864344SKuninori Morimoto 
4946e864344SKuninori Morimoto 	/*
4956e864344SKuninori Morimoto 	 * rtd remaining settings
4966e864344SKuninori Morimoto 	 */
497929deb84SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
4986e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
4996e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
5006e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
5016e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
5026e864344SKuninori Morimoto 
503929deb84SKuninori Morimoto 	rtd->card = card;
504929deb84SKuninori Morimoto 	rtd->dai_link = dai_link;
505929deb84SKuninori Morimoto 	if (!rtd->dai_link->ops)
506929deb84SKuninori Morimoto 		rtd->dai_link->ops = &null_snd_soc_ops;
507929deb84SKuninori Morimoto 
5086634e3d6SKuninori Morimoto 	/* see for_each_card_rtds */
5091a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
5101a497983SMengdong Lin 	rtd->num = card->num_rtd;
5111a497983SMengdong Lin 	card->num_rtd++;
512a848125eSKuninori Morimoto 
513a848125eSKuninori Morimoto 	return rtd;
5146e864344SKuninori Morimoto 
5156e864344SKuninori Morimoto free_rtd:
5166e864344SKuninori Morimoto 	soc_free_pcm_runtime(rtd);
5176e864344SKuninori Morimoto 	return NULL;
5181a497983SMengdong Lin }
5191a497983SMengdong Lin 
52047c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
52147c88fffSLiam Girdwood 		const char *dai_link)
52247c88fffSLiam Girdwood {
5231a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
52447c88fffSLiam Girdwood 
525bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5261a497983SMengdong Lin 		if (!strcmp(rtd->dai_link->name, dai_link))
5271a497983SMengdong Lin 			return rtd;
52847c88fffSLiam Girdwood 	}
529f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
53047c88fffSLiam Girdwood 	return NULL;
53147c88fffSLiam Girdwood }
53247c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
53347c88fffSLiam Girdwood 
53465462e44SKuninori Morimoto static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
53565462e44SKuninori Morimoto {
53665462e44SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
53765462e44SKuninori Morimoto 
53865462e44SKuninori Morimoto 	for_each_card_rtds(card, rtd)
53965462e44SKuninori Morimoto 		flush_delayed_work(&rtd->delayed_work);
54065462e44SKuninori Morimoto }
54165462e44SKuninori Morimoto 
5426f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
543db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5446f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
545db2a4165SFrank Mandarino {
5466f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
547d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
5481a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
5491a497983SMengdong Lin 	int i;
550db2a4165SFrank Mandarino 
551c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
552c5599b87SLars-Peter Clausen 	if (!card->instantiated)
553e3509ff0SDaniel Mack 		return 0;
554e3509ff0SDaniel Mack 
5552c7b696aSMarcel Ziswiler 	/*
5562c7b696aSMarcel Ziswiler 	 * Due to the resume being scheduled into a workqueue we could
5576ed25978SAndy Green 	 * suspend before that's finished - wait for it to complete.
5586ed25978SAndy Green 	 */
559f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
5606ed25978SAndy Green 
5616ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
562f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5636ed25978SAndy Green 
564a00f90f9SMark Brown 	/* mute any active DACs */
565bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5660b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
5673efab7dcSMark Brown 
5681a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5693efab7dcSMark Brown 			continue;
5703efab7dcSMark Brown 
5710b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
57288fdffa2SKuninori Morimoto 			if (dai->playback_active)
57388fdffa2SKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 1,
57488fdffa2SKuninori Morimoto 						SNDRV_PCM_STREAM_PLAYBACK);
575db2a4165SFrank Mandarino 		}
57688bd870fSBenoit Cousson 	}
577db2a4165SFrank Mandarino 
5784ccab3e7SLiam Girdwood 	/* suspend all pcms */
579bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5801a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5813efab7dcSMark Brown 			continue;
5823efab7dcSMark Brown 
5831a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
5843efab7dcSMark Brown 	}
5854ccab3e7SLiam Girdwood 
58687506549SMark Brown 	if (card->suspend_pre)
58770b2ac12SMark Brown 		card->suspend_pre(card);
588db2a4165SFrank Mandarino 
589bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5901a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
5913efab7dcSMark Brown 
5921a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5933efab7dcSMark Brown 			continue;
5943efab7dcSMark Brown 
595e0f22622SKuninori Morimoto 		if (!cpu_dai->driver->bus_control)
596e0f22622SKuninori Morimoto 			snd_soc_dai_suspend(cpu_dai);
597db2a4165SFrank Mandarino 	}
598db2a4165SFrank Mandarino 
59937660b6dSLars-Peter Clausen 	/* close any waiting streams */
60065462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
601db2a4165SFrank Mandarino 
602bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6033efab7dcSMark Brown 
6041a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6053efab7dcSMark Brown 			continue;
6063efab7dcSMark Brown 
6071a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
6087bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
609db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
610f0fba2adSLiam Girdwood 
6111a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
6127bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
613db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
614db2a4165SFrank Mandarino 	}
615db2a4165SFrank Mandarino 
6168be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
6178be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
618e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
619e2d32ff6SMark Brown 
6209178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
621f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
6222c7b696aSMarcel Ziswiler 		struct snd_soc_dapm_context *dapm =
6232c7b696aSMarcel Ziswiler 				snd_soc_component_get_dapm(component);
624d9fc4063SKuninori Morimoto 
6252c7b696aSMarcel Ziswiler 		/*
6262c7b696aSMarcel Ziswiler 		 * If there are paths active then the COMPONENT will be held
6272c7b696aSMarcel Ziswiler 		 * with bias _ON and should not be suspended.
6282c7b696aSMarcel Ziswiler 		 */
629e40fadbcSKuninori Morimoto 		if (!snd_soc_component_is_suspended(component)) {
6304890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
6311547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
632125a25daSMark Brown 				/*
6339178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
634125a25daSMark Brown 				 * bias off then being in STANDBY
635125a25daSMark Brown 				 * means it's doing something,
636125a25daSMark Brown 				 * otherwise fall through.
637125a25daSMark Brown 				 */
6384890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
6399178feb4SKuninori Morimoto 					dev_dbg(component->dev,
64010e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
641125a25daSMark Brown 					break;
642125a25daSMark Brown 				}
6431a12d5dcSGustavo A. R. Silva 				/* fall through */
644a8093297SLars-Peter Clausen 
6451547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
64666c51573SKuninori Morimoto 				snd_soc_component_suspend(component);
6479178feb4SKuninori Morimoto 				if (component->regmap)
6489178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
649988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
6509178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
6511547aba9SMark Brown 				break;
6521547aba9SMark Brown 			default:
6539178feb4SKuninori Morimoto 				dev_dbg(component->dev,
6549178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
6551547aba9SMark Brown 				break;
6561547aba9SMark Brown 			}
6571547aba9SMark Brown 		}
658f0fba2adSLiam Girdwood 	}
659db2a4165SFrank Mandarino 
660bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6611a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6623efab7dcSMark Brown 
6631a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6643efab7dcSMark Brown 			continue;
6653efab7dcSMark Brown 
666e0f22622SKuninori Morimoto 		if (cpu_dai->driver->bus_control)
667e0f22622SKuninori Morimoto 			snd_soc_dai_suspend(cpu_dai);
668988e8cc4SNicolin Chen 
669988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
670988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
671db2a4165SFrank Mandarino 	}
672db2a4165SFrank Mandarino 
67387506549SMark Brown 	if (card->suspend_post)
67470b2ac12SMark Brown 		card->suspend_post(card);
675db2a4165SFrank Mandarino 
676db2a4165SFrank Mandarino 	return 0;
677db2a4165SFrank Mandarino }
6786f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
679db2a4165SFrank Mandarino 
6802c7b696aSMarcel Ziswiler /*
6812c7b696aSMarcel Ziswiler  * deferred resume work, so resume can complete before we finished
6826ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6836ed25978SAndy Green  */
6846ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
685db2a4165SFrank Mandarino {
686f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
6872c7b696aSMarcel Ziswiler 			container_of(work, struct snd_soc_card,
6882c7b696aSMarcel Ziswiler 				     deferred_resume_work);
6891a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
690d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
6911a497983SMengdong Lin 	int i;
692db2a4165SFrank Mandarino 
6932c7b696aSMarcel Ziswiler 	/*
6942c7b696aSMarcel Ziswiler 	 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6956ed25978SAndy Green 	 * so userspace apps are blocked from touching us
6966ed25978SAndy Green 	 */
6976ed25978SAndy Green 
698f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
6996ed25978SAndy Green 
7009949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
701f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
7029949788bSMark Brown 
70387506549SMark Brown 	if (card->resume_pre)
70470b2ac12SMark Brown 		card->resume_pre(card);
705db2a4165SFrank Mandarino 
706bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
707bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7081a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7093efab7dcSMark Brown 
7101a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7113efab7dcSMark Brown 			continue;
7123efab7dcSMark Brown 
71324b09d05SKuninori Morimoto 		if (cpu_dai->driver->bus_control)
71424b09d05SKuninori Morimoto 			snd_soc_dai_resume(cpu_dai);
715db2a4165SFrank Mandarino 	}
716db2a4165SFrank Mandarino 
717f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
718e40fadbcSKuninori Morimoto 		if (snd_soc_component_is_suspended(component))
7199a840cbaSKuninori Morimoto 			snd_soc_component_resume(component);
7201547aba9SMark Brown 	}
721db2a4165SFrank Mandarino 
722bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7233efab7dcSMark Brown 
7241a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7253efab7dcSMark Brown 			continue;
7263efab7dcSMark Brown 
7271a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
728d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
729db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
730f0fba2adSLiam Girdwood 
7311a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
732d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
733db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
734db2a4165SFrank Mandarino 	}
735db2a4165SFrank Mandarino 
7363ff3f64bSMark Brown 	/* unmute any active DACs */
737bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7380b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
7393efab7dcSMark Brown 
7401a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7413efab7dcSMark Brown 			continue;
7423efab7dcSMark Brown 
7430b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
74488fdffa2SKuninori Morimoto 			if (dai->playback_active)
74588fdffa2SKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 0,
74688fdffa2SKuninori Morimoto 						SNDRV_PCM_STREAM_PLAYBACK);
747db2a4165SFrank Mandarino 		}
74888bd870fSBenoit Cousson 	}
749db2a4165SFrank Mandarino 
750bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7511a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7523efab7dcSMark Brown 
7531a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7543efab7dcSMark Brown 			continue;
7553efab7dcSMark Brown 
75624b09d05SKuninori Morimoto 		if (!cpu_dai->driver->bus_control)
75724b09d05SKuninori Morimoto 			snd_soc_dai_resume(cpu_dai);
758db2a4165SFrank Mandarino 	}
759db2a4165SFrank Mandarino 
76087506549SMark Brown 	if (card->resume_post)
76170b2ac12SMark Brown 		card->resume_post(card);
762db2a4165SFrank Mandarino 
763f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
7646ed25978SAndy Green 
7658be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
7668be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
767e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
7681a7aaa58SJeeja KP 
7691a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
7701a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
7716ed25978SAndy Green }
7726ed25978SAndy Green 
7736ed25978SAndy Green /* powers up audio subsystem after a suspend */
7746f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
7756ed25978SAndy Green {
7766f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
777bc263214SLars-Peter Clausen 	bool bus_control = false;
7781a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
77922d251a5SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
78022d251a5SKuninori Morimoto 	int i;
781b9dd94a8SPeter Ujfalusi 
782c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
783c5599b87SLars-Peter Clausen 	if (!card->instantiated)
7845ff1ddf2SEric Miao 		return 0;
7855ff1ddf2SEric Miao 
786988e8cc4SNicolin Chen 	/* activate pins from sleep state */
787bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
78888bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
78988bd870fSBenoit Cousson 
790988e8cc4SNicolin Chen 		if (cpu_dai->active)
791988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
79288bd870fSBenoit Cousson 
79322d251a5SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
794988e8cc4SNicolin Chen 			if (codec_dai->active)
795988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
796988e8cc4SNicolin Chen 		}
79788bd870fSBenoit Cousson 	}
798988e8cc4SNicolin Chen 
799bc263214SLars-Peter Clausen 	/*
800bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
801bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
802bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
80364ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
80464ab9baaSMark Brown 	 */
805bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
8061a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
8072c7b696aSMarcel Ziswiler 
808bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
80982e14e8bSStephen Warren 	}
810bc263214SLars-Peter Clausen 	if (bus_control) {
811bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
81264ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
81364ab9baaSMark Brown 	} else {
814f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
8156308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
816f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
817f0fba2adSLiam Girdwood 	}
8186ed25978SAndy Green 
819db2a4165SFrank Mandarino 	return 0;
820db2a4165SFrank Mandarino }
8216f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
822b3da4251SKuninori Morimoto 
823b3da4251SKuninori Morimoto static void soc_resume_init(struct snd_soc_card *card)
824b3da4251SKuninori Morimoto {
825b3da4251SKuninori Morimoto 	/* deferred resume work */
826b3da4251SKuninori Morimoto 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
827b3da4251SKuninori Morimoto }
828db2a4165SFrank Mandarino #else
8296f8ab4acSMark Brown #define snd_soc_suspend NULL
8306f8ab4acSMark Brown #define snd_soc_resume NULL
831b3da4251SKuninori Morimoto static inline void soc_resume_init(struct snd_soc_card *card)
832b3da4251SKuninori Morimoto {
833b3da4251SKuninori Morimoto }
834db2a4165SFrank Mandarino #endif
835db2a4165SFrank Mandarino 
83685e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
83702a06d30SBarry Song };
83802a06d30SBarry Song 
839c0834440SKuninori Morimoto static struct device_node
840c0834440SKuninori Morimoto *soc_component_to_node(struct snd_soc_component *component)
84112023a9aSMisael Lopez Cruz {
842c0834440SKuninori Morimoto 	struct device_node *of_node;
84312023a9aSMisael Lopez Cruz 
844c0834440SKuninori Morimoto 	of_node = component->dev->of_node;
845c0834440SKuninori Morimoto 	if (!of_node && component->dev->parent)
846c0834440SKuninori Morimoto 		of_node = component->dev->parent->of_node;
84734e81ab4SLars-Peter Clausen 
848c0834440SKuninori Morimoto 	return of_node;
84912023a9aSMisael Lopez Cruz }
85012023a9aSMisael Lopez Cruz 
851be6ac0a9SKuninori Morimoto static int snd_soc_is_matching_component(
852be6ac0a9SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc,
853be6ac0a9SKuninori Morimoto 	struct snd_soc_component *component)
854be6ac0a9SKuninori Morimoto {
855be6ac0a9SKuninori Morimoto 	struct device_node *component_of_node;
856be6ac0a9SKuninori Morimoto 
8577d7db5d3SKuninori Morimoto 	if (!dlc)
8587d7db5d3SKuninori Morimoto 		return 0;
8597d7db5d3SKuninori Morimoto 
8607d7db5d3SKuninori Morimoto 	component_of_node = soc_component_to_node(component);
861be6ac0a9SKuninori Morimoto 
862be6ac0a9SKuninori Morimoto 	if (dlc->of_node && component_of_node != dlc->of_node)
863be6ac0a9SKuninori Morimoto 		return 0;
864be6ac0a9SKuninori Morimoto 	if (dlc->name && strcmp(component->name, dlc->name))
865be6ac0a9SKuninori Morimoto 		return 0;
866be6ac0a9SKuninori Morimoto 
867be6ac0a9SKuninori Morimoto 	return 1;
868be6ac0a9SKuninori Morimoto }
869be6ac0a9SKuninori Morimoto 
870db2a4165SFrank Mandarino static struct snd_soc_component *soc_find_component(
871c1e230f0SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc)
872db2a4165SFrank Mandarino {
873db2a4165SFrank Mandarino 	struct snd_soc_component *component;
874db2a4165SFrank Mandarino 
875db2a4165SFrank Mandarino 	lockdep_assert_held(&client_mutex);
876db2a4165SFrank Mandarino 
877e3303268SKuninori Morimoto 	/*
878e3303268SKuninori Morimoto 	 * NOTE
879e3303268SKuninori Morimoto 	 *
880e3303268SKuninori Morimoto 	 * It returns *1st* found component, but some driver
881e3303268SKuninori Morimoto 	 * has few components by same of_node/name
882e3303268SKuninori Morimoto 	 * ex)
883e3303268SKuninori Morimoto 	 *	CPU component and generic DMAEngine component
884e3303268SKuninori Morimoto 	 */
885c1e230f0SKuninori Morimoto 	for_each_component(component)
886c1e230f0SKuninori Morimoto 		if (snd_soc_is_matching_component(dlc, component))
887db2a4165SFrank Mandarino 			return component;
888db2a4165SFrank Mandarino 
889db2a4165SFrank Mandarino 	return NULL;
89012023a9aSMisael Lopez Cruz }
89112023a9aSMisael Lopez Cruz 
892fbb88b5cSMengdong Lin /**
893fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
894fbb88b5cSMengdong Lin  *
8954958471bSJeffy Chen  * @dlc: name of the DAI or the DAI driver and optional component info to match
896fbb88b5cSMengdong Lin  *
897ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
898fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
899fbb88b5cSMengdong Lin  * should also match if being specified.
900fbb88b5cSMengdong Lin  *
901fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
902fbb88b5cSMengdong Lin  */
903305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
90414621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
90512023a9aSMisael Lopez Cruz {
90614621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
90714621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
90812023a9aSMisael Lopez Cruz 
90934e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
91034e81ab4SLars-Peter Clausen 
91114621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs */
912368dee94SKuninori Morimoto 	for_each_component(component) {
913be6ac0a9SKuninori Morimoto 		if (!snd_soc_is_matching_component(dlc, component))
91412023a9aSMisael Lopez Cruz 			continue;
91515a0c645SKuninori Morimoto 		for_each_component_dais(component, dai) {
9164958471bSJeffy Chen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
9176a6dafdaSJeffy Chen 			    && (!dai->driver->name
9186a6dafdaSJeffy Chen 				|| strcmp(dai->driver->name, dlc->dai_name)))
91914621c7eSLars-Peter Clausen 				continue;
92012023a9aSMisael Lopez Cruz 
92114621c7eSLars-Peter Clausen 			return dai;
92212023a9aSMisael Lopez Cruz 		}
92312023a9aSMisael Lopez Cruz 	}
92412023a9aSMisael Lopez Cruz 
92512023a9aSMisael Lopez Cruz 	return NULL;
92612023a9aSMisael Lopez Cruz }
927305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
92812023a9aSMisael Lopez Cruz 
92917fb1755SMengdong Lin /**
93017fb1755SMengdong Lin  * snd_soc_find_dai_link - Find a DAI link
93117fb1755SMengdong Lin  *
93217fb1755SMengdong Lin  * @card: soc card
93317fb1755SMengdong Lin  * @id: DAI link ID to match
93417fb1755SMengdong Lin  * @name: DAI link name to match, optional
9358abab35fSCharles Keepax  * @stream_name: DAI link stream name to match, optional
93617fb1755SMengdong Lin  *
93717fb1755SMengdong Lin  * This function will search all existing DAI links of the soc card to
93817fb1755SMengdong Lin  * find the link of the same ID. Since DAI links may not have their
93917fb1755SMengdong Lin  * unique ID, so name and stream name should also match if being
94017fb1755SMengdong Lin  * specified.
94117fb1755SMengdong Lin  *
94217fb1755SMengdong Lin  * Return: pointer of DAI link, or NULL if not found.
94317fb1755SMengdong Lin  */
94417fb1755SMengdong Lin struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
94517fb1755SMengdong Lin 					       int id, const char *name,
94617fb1755SMengdong Lin 					       const char *stream_name)
94717fb1755SMengdong Lin {
94842849064SKuninori Morimoto 	struct snd_soc_dai_link *link;
94917fb1755SMengdong Lin 
95017fb1755SMengdong Lin 	lockdep_assert_held(&client_mutex);
95117fb1755SMengdong Lin 
95242849064SKuninori Morimoto 	for_each_card_links(card, link) {
95317fb1755SMengdong Lin 		if (link->id != id)
95417fb1755SMengdong Lin 			continue;
95517fb1755SMengdong Lin 
95617fb1755SMengdong Lin 		if (name && (!link->name || strcmp(name, link->name)))
95717fb1755SMengdong Lin 			continue;
95817fb1755SMengdong Lin 
95917fb1755SMengdong Lin 		if (stream_name && (!link->stream_name
96017fb1755SMengdong Lin 			|| strcmp(stream_name, link->stream_name)))
96117fb1755SMengdong Lin 			continue;
96217fb1755SMengdong Lin 
96317fb1755SMengdong Lin 		return link;
96417fb1755SMengdong Lin 	}
96517fb1755SMengdong Lin 
96617fb1755SMengdong Lin 	return NULL;
96717fb1755SMengdong Lin }
96817fb1755SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
96917fb1755SMengdong Lin 
970bfce78a5SKuninori Morimoto static int soc_dai_link_sanity_check(struct snd_soc_card *card,
97136794902SKuninori Morimoto 				     struct snd_soc_dai_link *link)
97236794902SKuninori Morimoto {
97336794902SKuninori Morimoto 	int i;
97436794902SKuninori Morimoto 	struct snd_soc_dai_link_component *codec, *platform;
97536794902SKuninori Morimoto 
97636794902SKuninori Morimoto 	for_each_link_codecs(link, i, codec) {
97736794902SKuninori Morimoto 		/*
97836794902SKuninori Morimoto 		 * Codec must be specified by 1 of name or OF node,
97936794902SKuninori Morimoto 		 * not both or neither.
98036794902SKuninori Morimoto 		 */
98136794902SKuninori Morimoto 		if (!!codec->name == !!codec->of_node) {
98236794902SKuninori Morimoto 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
98336794902SKuninori Morimoto 				link->name);
98436794902SKuninori Morimoto 			return -EINVAL;
98536794902SKuninori Morimoto 		}
98636794902SKuninori Morimoto 
98736794902SKuninori Morimoto 		/* Codec DAI name must be specified */
98836794902SKuninori Morimoto 		if (!codec->dai_name) {
98936794902SKuninori Morimoto 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
99036794902SKuninori Morimoto 				link->name);
99136794902SKuninori Morimoto 			return -EINVAL;
99236794902SKuninori Morimoto 		}
99336794902SKuninori Morimoto 
99436794902SKuninori Morimoto 		/*
99536794902SKuninori Morimoto 		 * Defer card registration if codec component is not added to
99636794902SKuninori Morimoto 		 * component list.
99736794902SKuninori Morimoto 		 */
99836794902SKuninori Morimoto 		if (!soc_find_component(codec))
99936794902SKuninori Morimoto 			return -EPROBE_DEFER;
100036794902SKuninori Morimoto 	}
100136794902SKuninori Morimoto 
100236794902SKuninori Morimoto 	for_each_link_platforms(link, i, platform) {
100336794902SKuninori Morimoto 		/*
100436794902SKuninori Morimoto 		 * Platform may be specified by either name or OF node, but it
100536794902SKuninori Morimoto 		 * can be left unspecified, then no components will be inserted
100636794902SKuninori Morimoto 		 * in the rtdcom list
100736794902SKuninori Morimoto 		 */
100836794902SKuninori Morimoto 		if (!!platform->name == !!platform->of_node) {
100936794902SKuninori Morimoto 			dev_err(card->dev,
101036794902SKuninori Morimoto 				"ASoC: Neither/both platform name/of_node are set for %s\n",
101136794902SKuninori Morimoto 				link->name);
101236794902SKuninori Morimoto 			return -EINVAL;
101336794902SKuninori Morimoto 		}
101436794902SKuninori Morimoto 
101536794902SKuninori Morimoto 		/*
101636794902SKuninori Morimoto 		 * Defer card registration if platform component is not added to
101736794902SKuninori Morimoto 		 * component list.
101836794902SKuninori Morimoto 		 */
101936794902SKuninori Morimoto 		if (!soc_find_component(platform))
102036794902SKuninori Morimoto 			return -EPROBE_DEFER;
102136794902SKuninori Morimoto 	}
102236794902SKuninori Morimoto 
102336794902SKuninori Morimoto 	/* FIXME */
102436794902SKuninori Morimoto 	if (link->num_cpus > 1) {
102536794902SKuninori Morimoto 		dev_err(card->dev,
102636794902SKuninori Morimoto 			"ASoC: multi cpu is not yet supported %s\n",
102736794902SKuninori Morimoto 			link->name);
102836794902SKuninori Morimoto 		return -EINVAL;
102936794902SKuninori Morimoto 	}
103036794902SKuninori Morimoto 
103136794902SKuninori Morimoto 	/*
103236794902SKuninori Morimoto 	 * CPU device may be specified by either name or OF node, but
103336794902SKuninori Morimoto 	 * can be left unspecified, and will be matched based on DAI
103436794902SKuninori Morimoto 	 * name alone..
103536794902SKuninori Morimoto 	 */
103636794902SKuninori Morimoto 	if (link->cpus->name && link->cpus->of_node) {
103736794902SKuninori Morimoto 		dev_err(card->dev,
103836794902SKuninori Morimoto 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
103936794902SKuninori Morimoto 			link->name);
104036794902SKuninori Morimoto 		return -EINVAL;
104136794902SKuninori Morimoto 	}
104236794902SKuninori Morimoto 
104336794902SKuninori Morimoto 	/*
1044cd3c5ad7SKuninori Morimoto 	 * Defer card registration if cpu dai component is not added to
104536794902SKuninori Morimoto 	 * component list.
104636794902SKuninori Morimoto 	 */
104736794902SKuninori Morimoto 	if ((link->cpus->of_node || link->cpus->name) &&
104836794902SKuninori Morimoto 	    !soc_find_component(link->cpus))
104936794902SKuninori Morimoto 		return -EPROBE_DEFER;
105036794902SKuninori Morimoto 
105136794902SKuninori Morimoto 	/*
105236794902SKuninori Morimoto 	 * At least one of CPU DAI name or CPU device name/node must be
105336794902SKuninori Morimoto 	 * specified
105436794902SKuninori Morimoto 	 */
105536794902SKuninori Morimoto 	if (!link->cpus->dai_name &&
105636794902SKuninori Morimoto 	    !(link->cpus->name || link->cpus->of_node)) {
105736794902SKuninori Morimoto 		dev_err(card->dev,
105836794902SKuninori Morimoto 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
105936794902SKuninori Morimoto 			link->name);
106036794902SKuninori Morimoto 		return -EINVAL;
106136794902SKuninori Morimoto 	}
106236794902SKuninori Morimoto 
106336794902SKuninori Morimoto 	return 0;
106436794902SKuninori Morimoto }
106536794902SKuninori Morimoto 
1066da704f26SKuninori Morimoto /**
1067da704f26SKuninori Morimoto  * snd_soc_remove_dai_link - Remove a DAI link from the list
1068da704f26SKuninori Morimoto  * @card: The ASoC card that owns the link
1069da704f26SKuninori Morimoto  * @dai_link: The DAI link to remove
1070da704f26SKuninori Morimoto  *
1071da704f26SKuninori Morimoto  * This function removes a DAI link from the ASoC card's link list.
1072da704f26SKuninori Morimoto  *
1073da704f26SKuninori Morimoto  * For DAI links previously added by topology, topology should
1074da704f26SKuninori Morimoto  * remove them by using the dobj embedded in the link.
1075da704f26SKuninori Morimoto  */
1076da704f26SKuninori Morimoto void snd_soc_remove_dai_link(struct snd_soc_card *card,
1077bc7a9091SKuninori Morimoto 			     struct snd_soc_dai_link *dai_link)
1078bc7a9091SKuninori Morimoto {
1079bc7a9091SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
1080bc7a9091SKuninori Morimoto 
1081da704f26SKuninori Morimoto 	lockdep_assert_held(&client_mutex);
1082da704f26SKuninori Morimoto 
1083da704f26SKuninori Morimoto 	/*
1084da704f26SKuninori Morimoto 	 * Notify the machine driver for extra destruction
1085da704f26SKuninori Morimoto 	 */
1086da704f26SKuninori Morimoto 	if (card->remove_dai_link)
1087da704f26SKuninori Morimoto 		card->remove_dai_link(card, dai_link);
1088da704f26SKuninori Morimoto 
1089da704f26SKuninori Morimoto 	list_del(&dai_link->list);
1090da704f26SKuninori Morimoto 
1091bc7a9091SKuninori Morimoto 	rtd = snd_soc_get_pcm_runtime(card, dai_link->name);
1092bc7a9091SKuninori Morimoto 	if (rtd)
1093bc7a9091SKuninori Morimoto 		soc_free_pcm_runtime(rtd);
1094bc7a9091SKuninori Morimoto }
1095da704f26SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1096bc7a9091SKuninori Morimoto 
109763dc47daSKuninori Morimoto /**
109863dc47daSKuninori Morimoto  * snd_soc_add_dai_link - Add a DAI link dynamically
109963dc47daSKuninori Morimoto  * @card: The ASoC card to which the DAI link is added
110063dc47daSKuninori Morimoto  * @dai_link: The new DAI link to add
110163dc47daSKuninori Morimoto  *
110263dc47daSKuninori Morimoto  * This function adds a DAI link to the ASoC card's link list.
110363dc47daSKuninori Morimoto  *
110463dc47daSKuninori Morimoto  * Note: Topology can use this API to add DAI links when probing the
110563dc47daSKuninori Morimoto  * topology component. And machine drivers can still define static
110663dc47daSKuninori Morimoto  * DAI links in dai_link array.
110763dc47daSKuninori Morimoto  */
110863dc47daSKuninori Morimoto int snd_soc_add_dai_link(struct snd_soc_card *card,
11096f2f1ff0SMengdong Lin 			 struct snd_soc_dai_link *dai_link)
1110db2a4165SFrank Mandarino {
11111a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
111234614739SJerome Brunet 	struct snd_soc_dai_link_component *codec, *platform;
111390be711eSKuninori Morimoto 	struct snd_soc_component *component;
1114bfce78a5SKuninori Morimoto 	int i, ret;
1115db2a4165SFrank Mandarino 
111663dc47daSKuninori Morimoto 	lockdep_assert_held(&client_mutex);
111763dc47daSKuninori Morimoto 
111863dc47daSKuninori Morimoto 	/*
111963dc47daSKuninori Morimoto 	 * Notify the machine driver for extra initialization
112063dc47daSKuninori Morimoto 	 */
112163dc47daSKuninori Morimoto 	if (card->add_dai_link)
112263dc47daSKuninori Morimoto 		card->add_dai_link(card, dai_link);
112363dc47daSKuninori Morimoto 
1124a655de80SLiam Girdwood 	if (dai_link->ignore)
1125a655de80SLiam Girdwood 		return 0;
1126a655de80SLiam Girdwood 
11276f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
11286308419aSMark Brown 
1129bfce78a5SKuninori Morimoto 	ret = soc_dai_link_sanity_check(card, dai_link);
1130bfce78a5SKuninori Morimoto 	if (ret < 0)
1131bfce78a5SKuninori Morimoto 		return ret;
1132bfce78a5SKuninori Morimoto 
1133513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
1134513cb311SSudip Mukherjee 	if (!rtd)
1135513cb311SSudip Mukherjee 		return -ENOMEM;
1136513cb311SSudip Mukherjee 
113708a5841eSKuninori Morimoto 	/* FIXME: we need multi CPU support in the future */
113808a5841eSKuninori Morimoto 	rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
1139b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
11406b490879SMartin Hundebøll 		dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
114108a5841eSKuninori Morimoto 			 dai_link->cpus->dai_name);
11421a497983SMengdong Lin 		goto _err_defer;
1143c5af3a2eSMark Brown 	}
114490be711eSKuninori Morimoto 	snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
1145c5af3a2eSMark Brown 
114688bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
1147e2b30edfSKuninori Morimoto 	rtd->num_codecs = dai_link->num_codecs;
114834614739SJerome Brunet 	for_each_link_codecs(dai_link, i, codec) {
114934614739SJerome Brunet 		rtd->codec_dais[i] = snd_soc_find_dai(codec);
11500a2cfcd9SKuninori Morimoto 		if (!rtd->codec_dais[i]) {
11517c7e2d6aSStefan Agner 			dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
115234614739SJerome Brunet 				 codec->dai_name);
11531a497983SMengdong Lin 			goto _err_defer;
115412023a9aSMisael Lopez Cruz 		}
115534614739SJerome Brunet 
11560a2cfcd9SKuninori Morimoto 		snd_soc_rtdcom_add(rtd, rtd->codec_dais[i]->component);
115788bd870fSBenoit Cousson 	}
115888bd870fSBenoit Cousson 
115988bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
11600a2cfcd9SKuninori Morimoto 	rtd->codec_dai = rtd->codec_dais[0];
116112023a9aSMisael Lopez Cruz 
1162e2b30edfSKuninori Morimoto 	/* Find PLATFORM from registered PLATFORMs */
116334614739SJerome Brunet 	for_each_link_platforms(dai_link, i, platform) {
1164368dee94SKuninori Morimoto 		for_each_component(component) {
116534614739SJerome Brunet 			if (!snd_soc_is_matching_component(platform, component))
116690be711eSKuninori Morimoto 				continue;
116790be711eSKuninori Morimoto 
116890be711eSKuninori Morimoto 			snd_soc_rtdcom_add(rtd, component);
116990be711eSKuninori Morimoto 		}
117034614739SJerome Brunet 	}
117190be711eSKuninori Morimoto 
117263dc47daSKuninori Morimoto 	/* see for_each_card_links */
117363dc47daSKuninori Morimoto 	list_add_tail(&dai_link->list, &card->dai_link_list);
117463dc47daSKuninori Morimoto 
1175b19e6e7bSMark Brown 	return 0;
11761a497983SMengdong Lin 
11771a497983SMengdong Lin _err_defer:
11781a497983SMengdong Lin 	soc_free_pcm_runtime(rtd);
11791a497983SMengdong Lin 	return -EPROBE_DEFER;
11806b05eda6SMark Brown }
118163dc47daSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
11826b05eda6SMark Brown 
1183ffd60fbaSKuninori Morimoto static void soc_set_of_name_prefix(struct snd_soc_component *component)
1184ffd60fbaSKuninori Morimoto {
1185ffd60fbaSKuninori Morimoto 	struct device_node *of_node = soc_component_to_node(component);
1186ffd60fbaSKuninori Morimoto 	const char *str;
1187ffd60fbaSKuninori Morimoto 	int ret;
1188ffd60fbaSKuninori Morimoto 
1189ffd60fbaSKuninori Morimoto 	ret = of_property_read_string(of_node, "sound-name-prefix", &str);
1190ffd60fbaSKuninori Morimoto 	if (!ret)
1191ffd60fbaSKuninori Morimoto 		component->name_prefix = str;
1192ffd60fbaSKuninori Morimoto }
1193ffd60fbaSKuninori Morimoto 
1194ffd60fbaSKuninori Morimoto static void soc_set_name_prefix(struct snd_soc_card *card,
1195ffd60fbaSKuninori Morimoto 				struct snd_soc_component *component)
1196ffd60fbaSKuninori Morimoto {
1197ffd60fbaSKuninori Morimoto 	int i;
1198ffd60fbaSKuninori Morimoto 
1199ffd60fbaSKuninori Morimoto 	for (i = 0; i < card->num_configs && card->codec_conf; i++) {
1200ffd60fbaSKuninori Morimoto 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1201ffd60fbaSKuninori Morimoto 		struct device_node *of_node = soc_component_to_node(component);
1202ffd60fbaSKuninori Morimoto 
1203ffd60fbaSKuninori Morimoto 		if (map->of_node && of_node != map->of_node)
1204ffd60fbaSKuninori Morimoto 			continue;
1205ffd60fbaSKuninori Morimoto 		if (map->dev_name && strcmp(component->name, map->dev_name))
1206ffd60fbaSKuninori Morimoto 			continue;
1207ffd60fbaSKuninori Morimoto 		component->name_prefix = map->name_prefix;
1208ffd60fbaSKuninori Morimoto 		return;
1209ffd60fbaSKuninori Morimoto 	}
1210ffd60fbaSKuninori Morimoto 
1211ffd60fbaSKuninori Morimoto 	/*
1212ffd60fbaSKuninori Morimoto 	 * If there is no configuration table or no match in the table,
1213ffd60fbaSKuninori Morimoto 	 * check if a prefix is provided in the node
1214ffd60fbaSKuninori Morimoto 	 */
1215ffd60fbaSKuninori Morimoto 	soc_set_of_name_prefix(component);
1216ffd60fbaSKuninori Morimoto }
1217ffd60fbaSKuninori Morimoto 
1218c6619b72SKuninori Morimoto static void soc_remove_component(struct snd_soc_component *component,
1219c6619b72SKuninori Morimoto 				 int probed)
122022d14231SKuninori Morimoto {
1221c6619b72SKuninori Morimoto 
1222c6619b72SKuninori Morimoto 	if (!component->card)
1223c6619b72SKuninori Morimoto 		return;
1224c6619b72SKuninori Morimoto 
1225c6619b72SKuninori Morimoto 	if (probed)
1226c6619b72SKuninori Morimoto 		snd_soc_component_remove(component);
1227c6619b72SKuninori Morimoto 
122804f770d9SKuninori Morimoto 	/* For framework level robustness */
12293bb936f5SAmadeusz Sławiński 	snd_soc_component_set_jack(component, NULL, NULL);
123004f770d9SKuninori Morimoto 
12317a5d9815SBard liao 	list_del_init(&component->card_list);
123222d14231SKuninori Morimoto 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
123322d14231SKuninori Morimoto 	soc_cleanup_component_debugfs(component);
123422d14231SKuninori Morimoto 	component->card = NULL;
12350e36f36bSPierre-Louis Bossart 	snd_soc_component_module_put_when_remove(component);
123622d14231SKuninori Morimoto }
123722d14231SKuninori Morimoto 
1238ffd60fbaSKuninori Morimoto static int soc_probe_component(struct snd_soc_card *card,
1239ffd60fbaSKuninori Morimoto 			       struct snd_soc_component *component)
1240ffd60fbaSKuninori Morimoto {
1241ffd60fbaSKuninori Morimoto 	struct snd_soc_dapm_context *dapm =
1242ffd60fbaSKuninori Morimoto 		snd_soc_component_get_dapm(component);
1243ffd60fbaSKuninori Morimoto 	struct snd_soc_dai *dai;
1244c6619b72SKuninori Morimoto 	int probed = 0;
1245ffd60fbaSKuninori Morimoto 	int ret;
1246ffd60fbaSKuninori Morimoto 
1247ffd60fbaSKuninori Morimoto 	if (!strcmp(component->name, "snd-soc-dummy"))
1248ffd60fbaSKuninori Morimoto 		return 0;
1249ffd60fbaSKuninori Morimoto 
1250ffd60fbaSKuninori Morimoto 	if (component->card) {
1251ffd60fbaSKuninori Morimoto 		if (component->card != card) {
1252ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1253ffd60fbaSKuninori Morimoto 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1254ffd60fbaSKuninori Morimoto 				card->name, component->card->name);
1255ffd60fbaSKuninori Morimoto 			return -ENODEV;
1256ffd60fbaSKuninori Morimoto 		}
1257ffd60fbaSKuninori Morimoto 		return 0;
1258ffd60fbaSKuninori Morimoto 	}
1259ffd60fbaSKuninori Morimoto 
1260ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_module_get_when_probe(component);
1261ffd60fbaSKuninori Morimoto 	if (ret < 0)
1262ffd60fbaSKuninori Morimoto 		return ret;
1263ffd60fbaSKuninori Morimoto 
1264ffd60fbaSKuninori Morimoto 	component->card = card;
1265ffd60fbaSKuninori Morimoto 	soc_set_name_prefix(card, component);
1266ffd60fbaSKuninori Morimoto 
1267ffd60fbaSKuninori Morimoto 	soc_init_component_debugfs(component);
1268ffd60fbaSKuninori Morimoto 
126995c267ddSKuninori Morimoto 	snd_soc_dapm_init(dapm, card, component);
1270b614beafSKuninori Morimoto 
1271ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_new_controls(dapm,
1272ffd60fbaSKuninori Morimoto 					component->driver->dapm_widgets,
1273ffd60fbaSKuninori Morimoto 					component->driver->num_dapm_widgets);
1274ffd60fbaSKuninori Morimoto 
1275ffd60fbaSKuninori Morimoto 	if (ret != 0) {
1276ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1277ffd60fbaSKuninori Morimoto 			"Failed to create new controls %d\n", ret);
1278ffd60fbaSKuninori Morimoto 		goto err_probe;
1279ffd60fbaSKuninori Morimoto 	}
1280ffd60fbaSKuninori Morimoto 
1281ffd60fbaSKuninori Morimoto 	for_each_component_dais(component, dai) {
1282ffd60fbaSKuninori Morimoto 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1283ffd60fbaSKuninori Morimoto 		if (ret != 0) {
1284ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1285ffd60fbaSKuninori Morimoto 				"Failed to create DAI widgets %d\n", ret);
1286ffd60fbaSKuninori Morimoto 			goto err_probe;
1287ffd60fbaSKuninori Morimoto 		}
1288ffd60fbaSKuninori Morimoto 	}
1289ffd60fbaSKuninori Morimoto 
1290ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_probe(component);
1291ffd60fbaSKuninori Morimoto 	if (ret < 0) {
1292ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1293ffd60fbaSKuninori Morimoto 			"ASoC: failed to probe component %d\n", ret);
1294ffd60fbaSKuninori Morimoto 		goto err_probe;
1295ffd60fbaSKuninori Morimoto 	}
1296ffd60fbaSKuninori Morimoto 	WARN(dapm->idle_bias_off &&
1297ffd60fbaSKuninori Morimoto 	     dapm->bias_level != SND_SOC_BIAS_OFF,
1298ffd60fbaSKuninori Morimoto 	     "codec %s can not start from non-off bias with idle_bias_off==1\n",
1299ffd60fbaSKuninori Morimoto 	     component->name);
1300c6619b72SKuninori Morimoto 	probed = 1;
1301ffd60fbaSKuninori Morimoto 
1302ffd60fbaSKuninori Morimoto 	/* machine specific init */
1303ffd60fbaSKuninori Morimoto 	if (component->init) {
1304ffd60fbaSKuninori Morimoto 		ret = component->init(component);
1305ffd60fbaSKuninori Morimoto 		if (ret < 0) {
1306ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1307ffd60fbaSKuninori Morimoto 				"Failed to do machine specific init %d\n", ret);
1308ffd60fbaSKuninori Morimoto 			goto err_probe;
1309ffd60fbaSKuninori Morimoto 		}
1310ffd60fbaSKuninori Morimoto 	}
1311ffd60fbaSKuninori Morimoto 
1312ffd60fbaSKuninori Morimoto 	ret = snd_soc_add_component_controls(component,
1313ffd60fbaSKuninori Morimoto 					     component->driver->controls,
1314ffd60fbaSKuninori Morimoto 					     component->driver->num_controls);
1315ffd60fbaSKuninori Morimoto 	if (ret < 0)
1316ffd60fbaSKuninori Morimoto 		goto err_probe;
1317ffd60fbaSKuninori Morimoto 
1318ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(dapm,
1319ffd60fbaSKuninori Morimoto 				      component->driver->dapm_routes,
1320ffd60fbaSKuninori Morimoto 				      component->driver->num_dapm_routes);
1321ffd60fbaSKuninori Morimoto 	if (ret < 0)
1322ffd60fbaSKuninori Morimoto 		goto err_probe;
1323ffd60fbaSKuninori Morimoto 
1324ffd60fbaSKuninori Morimoto 	/* see for_each_card_components */
1325ffd60fbaSKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1326ffd60fbaSKuninori Morimoto 
1327ffd60fbaSKuninori Morimoto err_probe:
1328ffd60fbaSKuninori Morimoto 	if (ret < 0)
1329c6619b72SKuninori Morimoto 		soc_remove_component(component, probed);
1330ffd60fbaSKuninori Morimoto 
1331ffd60fbaSKuninori Morimoto 	return ret;
1332ffd60fbaSKuninori Morimoto }
1333ffd60fbaSKuninori Morimoto 
1334e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1335589c3563SJarkko Nikula {
1336589c3563SJarkko Nikula 	int err;
1337589c3563SJarkko Nikula 
133852abe6ccSGuennadi Liakhovetski 	if (!dai || !dai->probed || !dai->driver ||
13392eda3cb1SKuninori Morimoto 	    dai->driver->remove_order != order)
13402eda3cb1SKuninori Morimoto 		return;
13412eda3cb1SKuninori Morimoto 
1342dcdab582SKuninori Morimoto 	err = snd_soc_dai_remove(dai);
1343589c3563SJarkko Nikula 	if (err < 0)
1344e60cd14fSLars-Peter Clausen 		dev_err(dai->dev,
1345b0aa88afSMisael Lopez Cruz 			"ASoC: failed to remove %s: %d\n",
1346e60cd14fSLars-Peter Clausen 			dai->name, err);
1347dcdab582SKuninori Morimoto 
1348e60cd14fSLars-Peter Clausen 	dai->probed = 0;
1349b0aa88afSMisael Lopez Cruz }
1350b0aa88afSMisael Lopez Cruz 
1351a7d44f78SKuninori Morimoto static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1352a7d44f78SKuninori Morimoto {
1353a7d44f78SKuninori Morimoto 	int ret;
1354a7d44f78SKuninori Morimoto 
1355a7d44f78SKuninori Morimoto 	if (dai->probed ||
1356a7d44f78SKuninori Morimoto 	    dai->driver->probe_order != order)
1357a7d44f78SKuninori Morimoto 		return 0;
1358a7d44f78SKuninori Morimoto 
1359a7d44f78SKuninori Morimoto 	ret = snd_soc_dai_probe(dai);
1360a7d44f78SKuninori Morimoto 	if (ret < 0) {
1361a7d44f78SKuninori Morimoto 		dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1362a7d44f78SKuninori Morimoto 			dai->name, ret);
1363a7d44f78SKuninori Morimoto 		return ret;
1364a7d44f78SKuninori Morimoto 	}
1365a7d44f78SKuninori Morimoto 
1366a7d44f78SKuninori Morimoto 	dai->probed = 1;
1367a7d44f78SKuninori Morimoto 
1368a7d44f78SKuninori Morimoto 	return 0;
1369a7d44f78SKuninori Morimoto }
1370a7d44f78SKuninori Morimoto 
13714ca47d21SKuninori Morimoto static void soc_remove_link_dais(struct snd_soc_card *card)
1372f0fba2adSLiam Girdwood {
1373e60cd14fSLars-Peter Clausen 	int i;
13740b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
13754ca47d21SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
13764ca47d21SKuninori Morimoto 	int order;
13774ca47d21SKuninori Morimoto 
13784ca47d21SKuninori Morimoto 	for_each_comp_order(order) {
13794ca47d21SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1380f0fba2adSLiam Girdwood 			/* remove the CODEC DAI */
13810b7990e3SKuninori Morimoto 			for_each_rtd_codec_dai(rtd, i, codec_dai)
13820b7990e3SKuninori Morimoto 				soc_remove_dai(codec_dai, order);
1383f0fba2adSLiam Girdwood 
1384e60cd14fSLars-Peter Clausen 			soc_remove_dai(rtd->cpu_dai, order);
1385f0fba2adSLiam Girdwood 		}
13864ca47d21SKuninori Morimoto 	}
13874ca47d21SKuninori Morimoto }
1388f0fba2adSLiam Girdwood 
1389bc7c16c2SKuninori Morimoto static int soc_probe_link_dais(struct snd_soc_card *card)
1390bc7c16c2SKuninori Morimoto {
1391bc7c16c2SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1392bc7c16c2SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
1393bc7c16c2SKuninori Morimoto 	int i, order, ret;
1394bc7c16c2SKuninori Morimoto 
1395bc7c16c2SKuninori Morimoto 	for_each_comp_order(order) {
1396bc7c16c2SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1397bc7c16c2SKuninori Morimoto 
1398bc7c16c2SKuninori Morimoto 			dev_dbg(card->dev,
1399bc7c16c2SKuninori Morimoto 				"ASoC: probe %s dai link %d late %d\n",
1400bc7c16c2SKuninori Morimoto 				card->name, rtd->num, order);
1401bc7c16c2SKuninori Morimoto 
1402bc7c16c2SKuninori Morimoto 			ret = soc_probe_dai(rtd->cpu_dai, order);
1403bc7c16c2SKuninori Morimoto 			if (ret)
1404bc7c16c2SKuninori Morimoto 				return ret;
1405bc7c16c2SKuninori Morimoto 
1406bc7c16c2SKuninori Morimoto 			/* probe the CODEC DAI */
1407bc7c16c2SKuninori Morimoto 			for_each_rtd_codec_dai(rtd, i, codec_dai) {
1408bc7c16c2SKuninori Morimoto 				ret = soc_probe_dai(codec_dai, order);
1409bc7c16c2SKuninori Morimoto 				if (ret)
1410bc7c16c2SKuninori Morimoto 					return ret;
1411bc7c16c2SKuninori Morimoto 			}
1412bc7c16c2SKuninori Morimoto 		}
1413bc7c16c2SKuninori Morimoto 	}
1414bc7c16c2SKuninori Morimoto 
1415bc7c16c2SKuninori Morimoto 	return 0;
1416bc7c16c2SKuninori Morimoto }
1417bc7c16c2SKuninori Morimoto 
1418b006c0c6SKuninori Morimoto static void soc_remove_link_components(struct snd_soc_card *card)
141962ae68faSStephen Warren {
142061aca564SLars-Peter Clausen 	struct snd_soc_component *component;
1421b006c0c6SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
142290be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
1423b006c0c6SKuninori Morimoto 	int order;
142462ae68faSStephen Warren 
1425b006c0c6SKuninori Morimoto 	for_each_comp_order(order) {
1426b006c0c6SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
14272b544dd7SKuninori Morimoto 			for_each_rtd_components(rtd, rtdcom, component) {
1428b006c0c6SKuninori Morimoto 				if (component->driver->remove_order != order)
1429b006c0c6SKuninori Morimoto 					continue;
1430b006c0c6SKuninori Morimoto 
1431c6619b72SKuninori Morimoto 				soc_remove_component(component, 1);
143262ae68faSStephen Warren 			}
143362ae68faSStephen Warren 		}
1434b006c0c6SKuninori Morimoto 	}
1435b006c0c6SKuninori Morimoto }
143662ae68faSStephen Warren 
143762f07a6bSKuninori Morimoto static int soc_probe_link_components(struct snd_soc_card *card)
14386fb03550SKuninori Morimoto {
14396fb03550SKuninori Morimoto 	struct snd_soc_component *component;
144062f07a6bSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
14416fb03550SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
144262f07a6bSKuninori Morimoto 	int ret, order;
14436fb03550SKuninori Morimoto 
144462f07a6bSKuninori Morimoto 	for_each_comp_order(order) {
144562f07a6bSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
14462b544dd7SKuninori Morimoto 			for_each_rtd_components(rtd, rtdcom, component) {
144762f07a6bSKuninori Morimoto 				if (component->driver->probe_order != order)
144862f07a6bSKuninori Morimoto 					continue;
144962f07a6bSKuninori Morimoto 
14506fb03550SKuninori Morimoto 				ret = soc_probe_component(card, component);
14516fb03550SKuninori Morimoto 				if (ret < 0)
14526fb03550SKuninori Morimoto 					return ret;
14536fb03550SKuninori Morimoto 			}
14546fb03550SKuninori Morimoto 		}
145562f07a6bSKuninori Morimoto 	}
14566fb03550SKuninori Morimoto 
14576fb03550SKuninori Morimoto 	return 0;
14586fb03550SKuninori Morimoto }
14596fb03550SKuninori Morimoto 
1460ef2e8175SKuninori Morimoto void snd_soc_disconnect_sync(struct device *dev)
1461ef2e8175SKuninori Morimoto {
14622c7b696aSMarcel Ziswiler 	struct snd_soc_component *component =
14632c7b696aSMarcel Ziswiler 			snd_soc_lookup_component(dev, NULL);
1464ef2e8175SKuninori Morimoto 
1465ef2e8175SKuninori Morimoto 	if (!component || !component->card)
1466ef2e8175SKuninori Morimoto 		return;
1467ef2e8175SKuninori Morimoto 
1468ef2e8175SKuninori Morimoto 	snd_card_disconnect_sync(component->card->snd_card);
1469ef2e8175SKuninori Morimoto }
1470df532185SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
1471ef2e8175SKuninori 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 {
159674bd3f92SKuninori Morimoto 	struct snd_soc_component *component;
1597f2ed6b07SMengdong Lin 	int order;
1598f2ed6b07SMengdong Lin 	int ret;
1599f2ed6b07SMengdong Lin 
16001a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
160174bd3f92SKuninori Morimoto 		for_each_card_auxs(card, component) {
160274bd3f92SKuninori Morimoto 			if (component->driver->probe_order != order)
160374bd3f92SKuninori Morimoto 				continue;
160474bd3f92SKuninori Morimoto 
160574bd3f92SKuninori Morimoto 			ret = soc_probe_component(card,	component);
160674bd3f92SKuninori Morimoto 			if (ret < 0)
1607f2ed6b07SMengdong Lin 				return ret;
1608f2ed6b07SMengdong Lin 		}
1609f2ed6b07SMengdong Lin 	}
161065d9361fSLars-Peter Clausen 
161144c69bb1SLars-Peter Clausen 	return 0;
16123ca041edSSebastian Reichel }
16132eea392dSJarkko Nikula 
1614f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
161544c69bb1SLars-Peter Clausen {
1616f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1617f2ed6b07SMengdong Lin 	int order;
161844c69bb1SLars-Peter Clausen 
16191a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1620c2b71c71SKuninori Morimoto 		for_each_card_auxs_safe(card, comp, _comp) {
1621e8fbd250SKuninori Morimoto 			if (comp->driver->remove_order == order)
1622c6619b72SKuninori Morimoto 				soc_remove_component(comp, 1);
16235f3484acSLars-Peter Clausen 		}
16242eea392dSJarkko Nikula 	}
16252eea392dSJarkko Nikula }
16262eea392dSJarkko Nikula 
1627ce64c8b9SLars-Peter Clausen /**
1628ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1629ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1630ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1631ce64c8b9SLars-Peter Clausen  *
1632ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1633ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1634ce64c8b9SLars-Peter Clausen  *
1635ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1636ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1637ce64c8b9SLars-Peter Clausen  *
1638ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1639ce64c8b9SLars-Peter Clausen  */
1640ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1641ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1642ce64c8b9SLars-Peter Clausen {
1643ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
16440b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1645ce64c8b9SLars-Peter Clausen 	unsigned int i;
1646ce64c8b9SLars-Peter Clausen 	int ret;
1647ce64c8b9SLars-Peter Clausen 
16480b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1649ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1650ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1651ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1652ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1653ce64c8b9SLars-Peter Clausen 			return ret;
1654ce64c8b9SLars-Peter Clausen 		}
1655ce64c8b9SLars-Peter Clausen 	}
1656ce64c8b9SLars-Peter Clausen 
16572c7b696aSMarcel Ziswiler 	/*
16582c7b696aSMarcel Ziswiler 	 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
16592c7b696aSMarcel Ziswiler 	 * the component which has non_legacy_dai_naming is Codec
16602c7b696aSMarcel Ziswiler 	 */
1661999f7f5aSKuninori Morimoto 	if (cpu_dai->component->driver->non_legacy_dai_naming) {
1662ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1663ce64c8b9SLars-Peter Clausen 
1664ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1665ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1666ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1667ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1668ce64c8b9SLars-Peter Clausen 			break;
1669ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1670ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1671ce64c8b9SLars-Peter Clausen 			break;
1672ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1673ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1674ce64c8b9SLars-Peter Clausen 			break;
1675ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1676ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1677ce64c8b9SLars-Peter Clausen 			break;
1678ce64c8b9SLars-Peter Clausen 		}
1679ce64c8b9SLars-Peter Clausen 
1680ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1681ce64c8b9SLars-Peter Clausen 	}
1682ce64c8b9SLars-Peter Clausen 
1683ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1684ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1685ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1686ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1687ce64c8b9SLars-Peter Clausen 		return ret;
1688ce64c8b9SLars-Peter Clausen 	}
1689ce64c8b9SLars-Peter Clausen 
1690ce64c8b9SLars-Peter Clausen 	return 0;
1691ce64c8b9SLars-Peter Clausen }
1692ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1693ce64c8b9SLars-Peter Clausen 
16941f5a4535STakashi Iwai #ifdef CONFIG_DMI
16952c7b696aSMarcel Ziswiler /*
16962c7b696aSMarcel Ziswiler  * Trim special characters, and replace '-' with '_' since '-' is used to
1697345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1698345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1699345233d7SLiam Girdwood  */
1700345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1701345233d7SLiam Girdwood {
1702345233d7SLiam Girdwood 	int i, j = 0;
1703345233d7SLiam Girdwood 
1704345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1705345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1706345233d7SLiam Girdwood 		    || (name[i] == '_'))
1707345233d7SLiam Girdwood 			name[j++] = name[i];
1708345233d7SLiam Girdwood 		else if (name[i] == '-')
1709345233d7SLiam Girdwood 			name[j++] = '_';
1710345233d7SLiam Girdwood 	}
1711345233d7SLiam Girdwood 
1712345233d7SLiam Girdwood 	name[j] = '\0';
1713345233d7SLiam Girdwood }
1714345233d7SLiam Girdwood 
17152c7b696aSMarcel Ziswiler /*
17162c7b696aSMarcel Ziswiler  * Check if a DMI field is valid, i.e. not containing any string
171798faf436SMengdong Lin  * in the black list.
171898faf436SMengdong Lin  */
171998faf436SMengdong Lin static int is_dmi_valid(const char *field)
172098faf436SMengdong Lin {
172198faf436SMengdong Lin 	int i = 0;
172298faf436SMengdong Lin 
172398faf436SMengdong Lin 	while (dmi_blacklist[i]) {
172498faf436SMengdong Lin 		if (strstr(field, dmi_blacklist[i]))
172598faf436SMengdong Lin 			return 0;
172698faf436SMengdong Lin 		i++;
172746b5a4d2SWu Fengguang 	}
172898faf436SMengdong Lin 
172998faf436SMengdong Lin 	return 1;
173098faf436SMengdong Lin }
173198faf436SMengdong Lin 
17324e01e5dbSJaroslav Kysela /*
17334e01e5dbSJaroslav Kysela  * Append a string to card->dmi_longname with character cleanups.
17344e01e5dbSJaroslav Kysela  */
17354e01e5dbSJaroslav Kysela static void append_dmi_string(struct snd_soc_card *card, const char *str)
17364e01e5dbSJaroslav Kysela {
17374e01e5dbSJaroslav Kysela 	char *dst = card->dmi_longname;
17384e01e5dbSJaroslav Kysela 	size_t dst_len = sizeof(card->dmi_longname);
17394e01e5dbSJaroslav Kysela 	size_t len;
17404e01e5dbSJaroslav Kysela 
17414e01e5dbSJaroslav Kysela 	len = strlen(dst);
17424e01e5dbSJaroslav Kysela 	snprintf(dst + len, dst_len - len, "-%s", str);
17434e01e5dbSJaroslav Kysela 
17444e01e5dbSJaroslav Kysela 	len++;	/* skip the separator "-" */
17454e01e5dbSJaroslav Kysela 	if (len < dst_len)
17464e01e5dbSJaroslav Kysela 		cleanup_dmi_name(dst + len);
17474e01e5dbSJaroslav Kysela }
17484e01e5dbSJaroslav Kysela 
1749345233d7SLiam Girdwood /**
1750345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
1751345233d7SLiam Girdwood  * @card: The card to register DMI names
1752345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
1753345233d7SLiam Girdwood  *
1754345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
1755345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
1756345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
1757345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
1758345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
1759345233d7SLiam Girdwood  * as the sound card long name, in the format of
1760345233d7SLiam Girdwood  * "vendor-product-version-board"
1761345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
1762345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
1763345233d7SLiam Girdwood  * (UCM) configurations for the card.
1764345233d7SLiam Girdwood  *
1765345233d7SLiam Girdwood  * Possible card long names may be:
1766345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
1767345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1768345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1769345233d7SLiam Girdwood  *
1770345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
1771345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
1772345233d7SLiam Girdwood  *
1773345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
1774345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
1775345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
1776345233d7SLiam Girdwood  * charactors like SPACE.
1777345233d7SLiam Girdwood  *
1778345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
1779345233d7SLiam Girdwood  */
1780345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1781345233d7SLiam Girdwood {
1782345233d7SLiam Girdwood 	const char *vendor, *product, *product_version, *board;
1783345233d7SLiam Girdwood 
1784345233d7SLiam Girdwood 	if (card->long_name)
1785345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
1786345233d7SLiam Girdwood 
17874e01e5dbSJaroslav Kysela 	/* make up dmi long name as: vendor-product-version-board */
1788345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
178998faf436SMengdong Lin 	if (!vendor || !is_dmi_valid(vendor)) {
1790345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1791345233d7SLiam Girdwood 		return 0;
1792345233d7SLiam Girdwood 	}
1793345233d7SLiam Girdwood 
17944e01e5dbSJaroslav Kysela 	snprintf(card->dmi_longname, sizeof(card->dmi_longname), "%s", vendor);
1795345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
1796345233d7SLiam Girdwood 
1797345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
179898faf436SMengdong Lin 	if (product && is_dmi_valid(product)) {
17994e01e5dbSJaroslav Kysela 		append_dmi_string(card, product);
1800345233d7SLiam Girdwood 
18012c7b696aSMarcel Ziswiler 		/*
18022c7b696aSMarcel Ziswiler 		 * some vendors like Lenovo may only put a self-explanatory
1803345233d7SLiam Girdwood 		 * name in the product version field
1804345233d7SLiam Girdwood 		 */
1805345233d7SLiam Girdwood 		product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
18064e01e5dbSJaroslav Kysela 		if (product_version && is_dmi_valid(product_version))
18074e01e5dbSJaroslav Kysela 			append_dmi_string(card, product_version);
1808345233d7SLiam Girdwood 	}
1809345233d7SLiam Girdwood 
1810345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
181198faf436SMengdong Lin 	if (board && is_dmi_valid(board)) {
181239870b0dSJaroslav Kysela 		if (!product || strcasecmp(board, product))
18134e01e5dbSJaroslav Kysela 			append_dmi_string(card, board);
1814345233d7SLiam Girdwood 	} else if (!product) {
1815345233d7SLiam Girdwood 		/* fall back to using legacy name */
1816345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1817345233d7SLiam Girdwood 		return 0;
1818345233d7SLiam Girdwood 	}
1819345233d7SLiam Girdwood 
1820345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
18214e01e5dbSJaroslav Kysela 	if (flavour)
18224e01e5dbSJaroslav Kysela 		append_dmi_string(card, flavour);
1823345233d7SLiam Girdwood 
1824345233d7SLiam Girdwood 	/* set the card long name */
1825345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
1826345233d7SLiam Girdwood 
1827345233d7SLiam Girdwood 	return 0;
1828345233d7SLiam Girdwood }
1829345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
18301f5a4535STakashi Iwai #endif /* CONFIG_DMI */
1831345233d7SLiam Girdwood 
1832a655de80SLiam Girdwood static void soc_check_tplg_fes(struct snd_soc_card *card)
1833a655de80SLiam Girdwood {
1834a655de80SLiam Girdwood 	struct snd_soc_component *component;
1835a655de80SLiam Girdwood 	const struct snd_soc_component_driver *comp_drv;
1836a655de80SLiam Girdwood 	struct snd_soc_dai_link *dai_link;
1837a655de80SLiam Girdwood 	int i;
1838a655de80SLiam Girdwood 
1839368dee94SKuninori Morimoto 	for_each_component(component) {
1840a655de80SLiam Girdwood 
184149f9c4f2SDaniel Baluta 		/* does this component override BEs ? */
1842a655de80SLiam Girdwood 		if (!component->driver->ignore_machine)
1843a655de80SLiam Girdwood 			continue;
1844a655de80SLiam Girdwood 
1845a655de80SLiam Girdwood 		/* for this machine ? */
1846e194098bSPierre-Louis Bossart 		if (!strcmp(component->driver->ignore_machine,
1847a655de80SLiam Girdwood 			    card->dev->driver->name))
1848e194098bSPierre-Louis Bossart 			goto match;
1849e194098bSPierre-Louis Bossart 		if (strcmp(component->driver->ignore_machine,
1850e194098bSPierre-Louis Bossart 			   dev_name(card->dev)))
1851a655de80SLiam Girdwood 			continue;
1852e194098bSPierre-Louis Bossart match:
1853a655de80SLiam Girdwood 		/* machine matches, so override the rtd data */
18547fe072b4SKuninori Morimoto 		for_each_card_prelinks(card, i, dai_link) {
1855a655de80SLiam Girdwood 
1856a655de80SLiam Girdwood 			/* ignore this FE */
1857a655de80SLiam Girdwood 			if (dai_link->dynamic) {
1858a655de80SLiam Girdwood 				dai_link->ignore = true;
1859a655de80SLiam Girdwood 				continue;
1860a655de80SLiam Girdwood 			}
1861a655de80SLiam Girdwood 
186249f9c4f2SDaniel Baluta 			dev_info(card->dev, "info: override BE DAI link %s\n",
1863a655de80SLiam Girdwood 				 card->dai_link[i].name);
1864a655de80SLiam Girdwood 
1865a655de80SLiam Girdwood 			/* override platform component */
1866adb76b5bSKuninori Morimoto 			if (!dai_link->platforms) {
1867daecf46eSKuninori Morimoto 				dev_err(card->dev, "init platform error");
1868daecf46eSKuninori Morimoto 				continue;
1869daecf46eSKuninori Morimoto 			}
1870910fdcabSKuninori Morimoto 			dai_link->platforms->name = component->name;
1871a655de80SLiam Girdwood 
1872a655de80SLiam Girdwood 			/* convert non BE into BE */
1873a655de80SLiam Girdwood 			dai_link->no_pcm = 1;
1874*218fe9b7SDaniel Baluta 			dai_link->dpcm_playback = 1;
1875*218fe9b7SDaniel Baluta 			dai_link->dpcm_capture = 1;
1876a655de80SLiam Girdwood 
1877a655de80SLiam Girdwood 			/* override any BE fixups */
1878a655de80SLiam Girdwood 			dai_link->be_hw_params_fixup =
1879a655de80SLiam Girdwood 				component->driver->be_hw_params_fixup;
1880a655de80SLiam Girdwood 
18812c7b696aSMarcel Ziswiler 			/*
18822c7b696aSMarcel Ziswiler 			 * most BE links don't set stream name, so set it to
1883a655de80SLiam Girdwood 			 * dai link name if it's NULL to help bind widgets.
1884a655de80SLiam Girdwood 			 */
1885a655de80SLiam Girdwood 			if (!dai_link->stream_name)
1886a655de80SLiam Girdwood 				dai_link->stream_name = dai_link->name;
1887a655de80SLiam Girdwood 		}
1888a655de80SLiam Girdwood 
1889a655de80SLiam Girdwood 		/* Inform userspace we are using alternate topology */
1890a655de80SLiam Girdwood 		if (component->driver->topology_name_prefix) {
1891a655de80SLiam Girdwood 
1892a655de80SLiam Girdwood 			/* topology shortname created? */
1893a655de80SLiam Girdwood 			if (!card->topology_shortname_created) {
1894a655de80SLiam Girdwood 				comp_drv = component->driver;
1895a655de80SLiam Girdwood 
1896a655de80SLiam Girdwood 				snprintf(card->topology_shortname, 32, "%s-%s",
1897a655de80SLiam Girdwood 					 comp_drv->topology_name_prefix,
1898a655de80SLiam Girdwood 					 card->name);
1899a655de80SLiam Girdwood 				card->topology_shortname_created = true;
1900a655de80SLiam Girdwood 			}
1901a655de80SLiam Girdwood 
1902a655de80SLiam Girdwood 			/* use topology shortname */
1903a655de80SLiam Girdwood 			card->name = card->topology_shortname;
1904a655de80SLiam Girdwood 		}
1905a655de80SLiam Girdwood 	}
1906a655de80SLiam Girdwood }
1907a655de80SLiam Girdwood 
19080f23f718SKuninori Morimoto #define soc_setup_card_name(name, name1, name2, norm)		\
19090f23f718SKuninori Morimoto 	__soc_setup_card_name(name, sizeof(name), name1, name2, norm)
19100f23f718SKuninori Morimoto static void __soc_setup_card_name(char *name, int len,
19110f23f718SKuninori Morimoto 				  const char *name1, const char *name2,
19120f23f718SKuninori Morimoto 				  int normalization)
19130f23f718SKuninori Morimoto {
19140f23f718SKuninori Morimoto 	int i;
19150f23f718SKuninori Morimoto 
19160f23f718SKuninori Morimoto 	snprintf(name, len, "%s", name1 ? name1 : name2);
19170f23f718SKuninori Morimoto 
19180f23f718SKuninori Morimoto 	if (!normalization)
19190f23f718SKuninori Morimoto 		return;
19200f23f718SKuninori Morimoto 
19210f23f718SKuninori Morimoto 	/*
19220f23f718SKuninori Morimoto 	 * Name normalization
19230f23f718SKuninori Morimoto 	 *
19240f23f718SKuninori Morimoto 	 * The driver name is somewhat special, as it's used as a key for
19250f23f718SKuninori Morimoto 	 * searches in the user-space.
19260f23f718SKuninori Morimoto 	 *
19270f23f718SKuninori Morimoto 	 * ex)
19280f23f718SKuninori Morimoto 	 *	"abcd??efg" -> "abcd__efg"
19290f23f718SKuninori Morimoto 	 */
19300f23f718SKuninori Morimoto 	for (i = 0; i < len; i++) {
19310f23f718SKuninori Morimoto 		switch (name[i]) {
19320f23f718SKuninori Morimoto 		case '_':
19330f23f718SKuninori Morimoto 		case '-':
19340f23f718SKuninori Morimoto 		case '\0':
19350f23f718SKuninori Morimoto 			break;
19360f23f718SKuninori Morimoto 		default:
19370f23f718SKuninori Morimoto 			if (!isalnum(name[i]))
19380f23f718SKuninori Morimoto 				name[i] = '_';
19390f23f718SKuninori Morimoto 			break;
19400f23f718SKuninori Morimoto 		}
19410f23f718SKuninori Morimoto 	}
19420f23f718SKuninori Morimoto }
19430f23f718SKuninori Morimoto 
1944ce21401cSKuninori Morimoto static void soc_cleanup_card_resources(struct snd_soc_card *card,
1945ce21401cSKuninori Morimoto 				       int card_probed)
194653e947a0SKuninori Morimoto {
19477ce6088fSKuninori Morimoto 	struct snd_soc_dai_link *link, *_link;
19487ce6088fSKuninori Morimoto 
19490ced7b05SKuninori Morimoto 	if (card->snd_card)
19500ced7b05SKuninori Morimoto 		snd_card_disconnect_sync(card->snd_card);
195153e947a0SKuninori Morimoto 
19522a6f0892SKuninori Morimoto 	snd_soc_dapm_shutdown(card);
19532a6f0892SKuninori Morimoto 
195453e947a0SKuninori Morimoto 	/* remove and free each DAI */
19557ce6088fSKuninori Morimoto 	soc_remove_link_dais(card);
19560ced7b05SKuninori Morimoto 	soc_remove_link_components(card);
19577ce6088fSKuninori Morimoto 
19587ce6088fSKuninori Morimoto 	for_each_card_links_safe(card, link, _link)
19597ce6088fSKuninori Morimoto 		snd_soc_remove_dai_link(card, link);
19607ce6088fSKuninori Morimoto 
196153e947a0SKuninori Morimoto 	/* remove auxiliary devices */
196253e947a0SKuninori Morimoto 	soc_remove_aux_devices(card);
1963e8fbd250SKuninori Morimoto 	soc_unbind_aux_dev(card);
196453e947a0SKuninori Morimoto 
196553e947a0SKuninori Morimoto 	snd_soc_dapm_free(&card->dapm);
196653e947a0SKuninori Morimoto 	soc_cleanup_card_debugfs(card);
196753e947a0SKuninori Morimoto 
196853e947a0SKuninori Morimoto 	/* remove the card */
1969ce21401cSKuninori Morimoto 	if (card_probed && card->remove)
197053e947a0SKuninori Morimoto 		card->remove(card);
19710ced7b05SKuninori Morimoto 
19720ced7b05SKuninori Morimoto 	if (card->snd_card) {
19730ced7b05SKuninori Morimoto 		snd_card_free(card->snd_card);
19740ced7b05SKuninori Morimoto 		card->snd_card = NULL;
19750ced7b05SKuninori Morimoto 	}
197653e947a0SKuninori Morimoto }
197753e947a0SKuninori Morimoto 
19782cc1afcfSKuninori Morimoto static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
19792cc1afcfSKuninori Morimoto {
19802cc1afcfSKuninori Morimoto 	if (card->instantiated) {
1981ce21401cSKuninori Morimoto 		int card_probed = 1;
1982ce21401cSKuninori Morimoto 
19832cc1afcfSKuninori Morimoto 		card->instantiated = false;
19842cc1afcfSKuninori Morimoto 		snd_soc_flush_all_delayed_work(card);
19852cc1afcfSKuninori Morimoto 
1986ce21401cSKuninori Morimoto 		soc_cleanup_card_resources(card, card_probed);
19872cc1afcfSKuninori Morimoto 		if (!unregister)
19882cc1afcfSKuninori Morimoto 			list_add(&card->list, &unbind_card_list);
19892cc1afcfSKuninori Morimoto 	} else {
19902cc1afcfSKuninori Morimoto 		if (unregister)
19912cc1afcfSKuninori Morimoto 			list_del(&card->list);
19922cc1afcfSKuninori Morimoto 	}
19932cc1afcfSKuninori Morimoto }
19942cc1afcfSKuninori Morimoto 
1995ed90c013SKuninori Morimoto static int snd_soc_bind_card(struct snd_soc_card *card)
1996f0fba2adSLiam Girdwood {
19971a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
199861b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
1999ce21401cSKuninori Morimoto 	int ret, i, card_probed = 0;
200096dd3622SMark Brown 
200134e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
200201b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
2003f0fba2adSLiam Girdwood 
200495c267ddSKuninori Morimoto 	snd_soc_dapm_init(&card->dapm, card, NULL);
200553e947a0SKuninori Morimoto 
2006a655de80SLiam Girdwood 	/* check whether any platform is ignore machine FE and using topology */
2007a655de80SLiam Girdwood 	soc_check_tplg_fes(card);
2008a655de80SLiam Girdwood 
200944c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
2010bee886f1SKuninori Morimoto 	ret = soc_bind_aux_dev(card);
2011bee886f1SKuninori Morimoto 	if (ret < 0)
201253e947a0SKuninori Morimoto 		goto probe_end;
2013db2a4165SFrank Mandarino 
2014f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
2015d8145989SKuninori Morimoto 	card->num_rtd = 0;
20165b99a0aaSKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link) {
20175b99a0aaSKuninori Morimoto 		ret = snd_soc_add_dai_link(card, dai_link);
20185b99a0aaSKuninori Morimoto 		if (ret < 0)
20195b99a0aaSKuninori Morimoto 			goto probe_end;
20205b99a0aaSKuninori Morimoto 	}
2021f8f80361SMengdong Lin 
2022f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
2023102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2024f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
2025f0fba2adSLiam Girdwood 	if (ret < 0) {
202610e8aa9aSMichał Mirosław 		dev_err(card->dev,
202710e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
202810e8aa9aSMichał Mirosław 			card->name, ret);
202953e947a0SKuninori Morimoto 		goto probe_end;
2030db2a4165SFrank Mandarino 	}
2031db2a4165SFrank Mandarino 
20320757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
20330757d834SLars-Peter Clausen 
2034b3da4251SKuninori Morimoto 	soc_resume_init(card);
20356ed25978SAndy Green 
2036b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
20379a841ebbSMark Brown 					card->num_dapm_widgets);
2038b8ba3b57SKuninori Morimoto 	if (ret < 0)
2039b8ba3b57SKuninori Morimoto 		goto probe_end;
20409a841ebbSMark Brown 
2041b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2042f23e860eSNicolin Chen 					card->num_of_dapm_widgets);
2043b8ba3b57SKuninori Morimoto 	if (ret < 0)
2044b8ba3b57SKuninori Morimoto 		goto probe_end;
2045f23e860eSNicolin Chen 
2046f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
2047f0fba2adSLiam Girdwood 	if (card->probe) {
2048e7361ec4SMark Brown 		ret = card->probe(card);
2049f0fba2adSLiam Girdwood 		if (ret < 0)
205053e947a0SKuninori Morimoto 			goto probe_end;
2051ce21401cSKuninori Morimoto 		card_probed = 1;
2052f0fba2adSLiam Girdwood 	}
2053f0fba2adSLiam Girdwood 
205462ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
205562f07a6bSKuninori Morimoto 	ret = soc_probe_link_components(card);
205662ae68faSStephen Warren 	if (ret < 0) {
2057f110bfc7SLiam Girdwood 		dev_err(card->dev,
205862f07a6bSKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
205953e947a0SKuninori Morimoto 		goto probe_end;
206062ae68faSStephen Warren 	}
206162ae68faSStephen Warren 
2062f2ed6b07SMengdong Lin 	/* probe auxiliary components */
2063f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
206474bd3f92SKuninori Morimoto 	if (ret < 0) {
206574bd3f92SKuninori Morimoto 		dev_err(card->dev,
206674bd3f92SKuninori Morimoto 			"ASoC: failed to probe aux component %d\n", ret);
206753e947a0SKuninori Morimoto 		goto probe_end;
206874bd3f92SKuninori Morimoto 	}
2069f2ed6b07SMengdong Lin 
207062ae68faSStephen Warren 	/* probe all DAI links on this card */
2071c7e73774SKuninori Morimoto 	ret = soc_probe_link_dais(card);
2072fe3e78e0SMark Brown 	if (ret < 0) {
2073f110bfc7SLiam Girdwood 		dev_err(card->dev,
2074c7e73774SKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
207553e947a0SKuninori Morimoto 		goto probe_end;
2076fe3e78e0SMark Brown 	}
2077fe3e78e0SMark Brown 
2078c4b46982SKuninori Morimoto 	for_each_card_rtds(card, rtd)
2079c4b46982SKuninori Morimoto 		soc_link_init(card, rtd);
2080c4b46982SKuninori Morimoto 
2081888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
2082b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
2083888df395SMark Brown 
20849b98c7c2SKuninori Morimoto 	ret = snd_soc_add_card_controls(card, card->controls,
20852c7b696aSMarcel Ziswiler 					card->num_controls);
20869b98c7c2SKuninori Morimoto 	if (ret < 0)
20879b98c7c2SKuninori Morimoto 		goto probe_end;
2088b7af1dafSMark Brown 
2089daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2090b8ad29deSMark Brown 				      card->num_dapm_routes);
2091daa480bdSKuninori Morimoto 	if (ret < 0)
2092daa480bdSKuninori Morimoto 		goto probe_end;
2093b8ad29deSMark Brown 
2094daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2095f23e860eSNicolin Chen 				      card->num_of_dapm_routes);
2096daa480bdSKuninori Morimoto 	if (ret < 0)
2097daa480bdSKuninori Morimoto 		goto probe_end;
209875d9ac46SMark Brown 
2099861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
2100861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
2101861886d3STakashi Iwai 
21020f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->shortname,
21030f23f718SKuninori Morimoto 			    card->name, NULL, 0);
21040f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->longname,
21050f23f718SKuninori Morimoto 			    card->long_name, card->name, 0);
21060f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->driver,
21070f23f718SKuninori Morimoto 			    card->driver_name, card->name, 1);
2108fe3e78e0SMark Brown 
2109dc73d73aSJaroslav Kysela 	if (card->components) {
2110dc73d73aSJaroslav Kysela 		/* the current implementation of snd_component_add() accepts */
2111dc73d73aSJaroslav Kysela 		/* multiple components in the string separated by space, */
2112dc73d73aSJaroslav Kysela 		/* but the string collision (identical string) check might */
2113dc73d73aSJaroslav Kysela 		/* not work correctly */
2114dc73d73aSJaroslav Kysela 		ret = snd_component_add(card->snd_card, card->components);
2115dc73d73aSJaroslav Kysela 		if (ret < 0) {
2116dc73d73aSJaroslav Kysela 			dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n",
2117dc73d73aSJaroslav Kysela 				card->name, ret);
2118dc73d73aSJaroslav Kysela 			goto probe_end;
2119dc73d73aSJaroslav Kysela 		}
2120dc73d73aSJaroslav Kysela 	}
2121dc73d73aSJaroslav Kysela 
212228e9ad92SMark Brown 	if (card->late_probe) {
212328e9ad92SMark Brown 		ret = card->late_probe(card);
212428e9ad92SMark Brown 		if (ret < 0) {
2125f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
212628e9ad92SMark Brown 				card->name, ret);
212753e947a0SKuninori Morimoto 			goto probe_end;
212828e9ad92SMark Brown 		}
212928e9ad92SMark Brown 	}
2130ce21401cSKuninori Morimoto 	card_probed = 1;
213128e9ad92SMark Brown 
2132824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
21338c193b8dSLars-Peter Clausen 
2134f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2135fe3e78e0SMark Brown 	if (ret < 0) {
2136f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2137f110bfc7SLiam Girdwood 				ret);
213853e947a0SKuninori Morimoto 		goto probe_end;
2139fe3e78e0SMark Brown 	}
2140fe3e78e0SMark Brown 
2141435c5e25SMark Brown 	card->instantiated = 1;
2142882eab6cSTzung-Bi Shih 	dapm_mark_endpoints_dirty(card);
21434f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2144b19e6e7bSMark Brown 
2145ed90c013SKuninori Morimoto 	/* deactivate pins to sleep state */
2146ed90c013SKuninori Morimoto 	for_each_card_rtds(card, rtd) {
2147ed90c013SKuninori Morimoto 		struct snd_soc_dai *dai;
2148ed90c013SKuninori Morimoto 
2149ed90c013SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
2150ed90c013SKuninori Morimoto 			if (!dai->active)
2151ed90c013SKuninori Morimoto 				pinctrl_pm_select_sleep_state(dai->dev);
2152ed90c013SKuninori Morimoto 		}
2153ed90c013SKuninori Morimoto 
2154ed90c013SKuninori Morimoto 		if (!rtd->cpu_dai->active)
2155ed90c013SKuninori Morimoto 			pinctrl_pm_select_sleep_state(rtd->cpu_dai->dev);
2156ed90c013SKuninori Morimoto 	}
2157ed90c013SKuninori Morimoto 
215853e947a0SKuninori Morimoto probe_end:
215953e947a0SKuninori Morimoto 	if (ret < 0)
2160ce21401cSKuninori Morimoto 		soc_cleanup_card_resources(card, card_probed);
2161db2a4165SFrank Mandarino 
2162f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
216334e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2164db2a4165SFrank Mandarino 
2165b19e6e7bSMark Brown 	return ret;
2166435c5e25SMark Brown }
2167435c5e25SMark Brown 
2168435c5e25SMark Brown /* probes a new socdev */
2169435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2170435c5e25SMark Brown {
2171f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2172435c5e25SMark Brown 
217370a7ca34SVinod Koul 	/*
217470a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
217570a7ca34SVinod Koul 	 * we should not be here in that case so ret error
217670a7ca34SVinod Koul 	 */
217770a7ca34SVinod Koul 	if (!card)
217870a7ca34SVinod Koul 		return -EINVAL;
217970a7ca34SVinod Koul 
2180fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2181f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2182fe4085e8SMark Brown 		 card->name);
2183fe4085e8SMark Brown 
2184435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2185435c5e25SMark Brown 	card->dev = &pdev->dev;
2186f0fba2adSLiam Girdwood 
218728d528c8SMark Brown 	return snd_soc_register_card(card);
2188435c5e25SMark Brown }
2189435c5e25SMark Brown 
2190b0e26485SVinod Koul /* removes a socdev */
2191b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2192b0e26485SVinod Koul {
2193b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2194b0e26485SVinod Koul 
2195c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2196db2a4165SFrank Mandarino 	return 0;
2197db2a4165SFrank Mandarino }
2198db2a4165SFrank Mandarino 
21996f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
220051737470SMark Brown {
22016f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
22021a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
220351737470SMark Brown 
220451737470SMark Brown 	if (!card->instantiated)
2205416356fcSMark Brown 		return 0;
220651737470SMark Brown 
22072c7b696aSMarcel Ziswiler 	/*
22082c7b696aSMarcel Ziswiler 	 * Flush out pmdown_time work - we actually do want to run it
22092c7b696aSMarcel Ziswiler 	 * now, we're shutting down so no imminent restart.
22102c7b696aSMarcel Ziswiler 	 */
221165462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
221251737470SMark Brown 
2213f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2214416356fcSMark Brown 
2215988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
2216bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
221788bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
22180b7990e3SKuninori Morimoto 		struct snd_soc_dai *codec_dai;
22191a497983SMengdong Lin 		int i;
222088bd870fSBenoit Cousson 
2221988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
22220b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
222388bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
222488bd870fSBenoit Cousson 		}
2225988e8cc4SNicolin Chen 	}
2226988e8cc4SNicolin Chen 
2227416356fcSMark Brown 	return 0;
222851737470SMark Brown }
22296f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
223051737470SMark Brown 
22316f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2232b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2233b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2234b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2235b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
22366f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2237b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2238416356fcSMark Brown };
2239deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2240416356fcSMark Brown 
2241db2a4165SFrank Mandarino /* ASoC platform driver */
2242db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2243db2a4165SFrank Mandarino 	.driver		= {
2244db2a4165SFrank Mandarino 		.name		= "soc-audio",
22456f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2246db2a4165SFrank Mandarino 	},
2247db2a4165SFrank Mandarino 	.probe		= soc_probe,
2248db2a4165SFrank Mandarino 	.remove		= soc_remove,
2249db2a4165SFrank Mandarino };
2250db2a4165SFrank Mandarino 
2251096e49d5SMark Brown /**
2252db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2253db2a4165SFrank Mandarino  * @_template: control template
2254db2a4165SFrank Mandarino  * @data: control private data
2255ac11a2b3SMark Brown  * @long_name: control long name
2256efb7ac3fSMark Brown  * @prefix: control name prefix
2257db2a4165SFrank Mandarino  *
2258db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2259db2a4165SFrank Mandarino  *
2260db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2261db2a4165SFrank Mandarino  */
2262db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
22633056557fSMark Brown 				  void *data, const char *long_name,
2264efb7ac3fSMark Brown 				  const char *prefix)
2265db2a4165SFrank Mandarino {
2266db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2267efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2268efb7ac3fSMark Brown 	char *name = NULL;
2269db2a4165SFrank Mandarino 
2270db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2271db2a4165SFrank Mandarino 	template.index = 0;
2272db2a4165SFrank Mandarino 
2273efb7ac3fSMark Brown 	if (!long_name)
2274efb7ac3fSMark Brown 		long_name = template.name;
2275efb7ac3fSMark Brown 
2276efb7ac3fSMark Brown 	if (prefix) {
22772b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2278efb7ac3fSMark Brown 		if (!name)
2279efb7ac3fSMark Brown 			return NULL;
2280efb7ac3fSMark Brown 
2281efb7ac3fSMark Brown 		template.name = name;
2282efb7ac3fSMark Brown 	} else {
2283efb7ac3fSMark Brown 		template.name = long_name;
2284efb7ac3fSMark Brown 	}
2285efb7ac3fSMark Brown 
2286efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2287efb7ac3fSMark Brown 
2288efb7ac3fSMark Brown 	kfree(name);
2289efb7ac3fSMark Brown 
2290efb7ac3fSMark Brown 	return kcontrol;
2291db2a4165SFrank Mandarino }
2292db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2293db2a4165SFrank Mandarino 
2294022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2295022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2296022658beSLiam Girdwood 	const char *prefix, void *data)
2297022658beSLiam Girdwood {
2298022658beSLiam Girdwood 	int err, i;
2299022658beSLiam Girdwood 
2300022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2301022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
23022c7b696aSMarcel Ziswiler 
2303022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2304022658beSLiam Girdwood 						     control->name, prefix));
2305022658beSLiam Girdwood 		if (err < 0) {
2306f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2307f110bfc7SLiam Girdwood 				control->name, err);
2308022658beSLiam Girdwood 			return err;
2309022658beSLiam Girdwood 		}
2310022658beSLiam Girdwood 	}
2311022658beSLiam Girdwood 
2312022658beSLiam Girdwood 	return 0;
2313022658beSLiam Girdwood }
2314022658beSLiam Girdwood 
23154fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
23164fefd698SDimitris Papastamos 					       const char *name)
23174fefd698SDimitris Papastamos {
23184fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
23194fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
23204fefd698SDimitris Papastamos 
23214fefd698SDimitris Papastamos 	if (unlikely(!name))
23224fefd698SDimitris Papastamos 		return NULL;
23234fefd698SDimitris Papastamos 
23244fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
23254fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
23264fefd698SDimitris Papastamos 			return kctl;
23274fefd698SDimitris Papastamos 	return NULL;
23284fefd698SDimitris Papastamos }
23294fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
23304fefd698SDimitris Papastamos 
2331db2a4165SFrank Mandarino /**
23320f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
23330f2780adSLars-Peter Clausen  *
23340f2780adSLars-Peter Clausen  * @component: Component to add controls to
23350f2780adSLars-Peter Clausen  * @controls: Array of controls to add
23360f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
23370f2780adSLars-Peter Clausen  *
23380f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
23390f2780adSLars-Peter Clausen  */
23400f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
23410f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
23420f2780adSLars-Peter Clausen {
23430f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
23440f2780adSLars-Peter Clausen 
23450f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
23460f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
23470f2780adSLars-Peter Clausen }
23480f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
23490f2780adSLars-Peter Clausen 
23500f2780adSLars-Peter Clausen /**
2351022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2352022658beSLiam Girdwood  * Convenience function to add a list of controls.
2353022658beSLiam Girdwood  *
2354022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2355022658beSLiam Girdwood  * @controls: array of controls to add
2356022658beSLiam Girdwood  * @num_controls: number of elements in the array
2357022658beSLiam Girdwood  *
2358022658beSLiam Girdwood  * Return 0 for success, else error.
2359022658beSLiam Girdwood  */
2360022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2361022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2362022658beSLiam Girdwood {
2363022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2364022658beSLiam Girdwood 
2365022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2366022658beSLiam Girdwood 			NULL, soc_card);
2367022658beSLiam Girdwood }
2368022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2369022658beSLiam Girdwood 
2370022658beSLiam Girdwood /**
2371022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2372022658beSLiam Girdwood  * Convienience function to add a list of controls.
2373022658beSLiam Girdwood  *
2374022658beSLiam Girdwood  * @dai: DAI to add controls to
2375022658beSLiam Girdwood  * @controls: array of controls to add
2376022658beSLiam Girdwood  * @num_controls: number of elements in the array
2377022658beSLiam Girdwood  *
2378022658beSLiam Girdwood  * Return 0 for success, else error.
2379022658beSLiam Girdwood  */
2380022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2381022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2382022658beSLiam Girdwood {
2383313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2384022658beSLiam Girdwood 
2385022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2386022658beSLiam Girdwood 			NULL, dai);
2387022658beSLiam Girdwood }
2388022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2389022658beSLiam Girdwood 
2390c5af3a2eSMark Brown /**
2391c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2392c5af3a2eSMark Brown  *
2393ac11a2b3SMark Brown  * @card: Card to register
2394c5af3a2eSMark Brown  *
2395c5af3a2eSMark Brown  */
239670a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2397c5af3a2eSMark Brown {
2398c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2399c5af3a2eSMark Brown 		return -EINVAL;
2400c5af3a2eSMark Brown 
2401ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2402ed77cc12SMark Brown 
2403b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->widgets);
2404b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->paths);
2405b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->dapm_list);
2406b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->aux_comp_list);
2407b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->component_dev_list);
2408b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->list);
2409f8f80361SMengdong Lin 	INIT_LIST_HEAD(&card->dai_link_list);
24101a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
2411db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
24128a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2413b03bfaecSKuninori Morimoto 
2414c5af3a2eSMark Brown 	card->instantiated = 0;
2415f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
2416a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
241772b745e3SPeter Ujfalusi 	mutex_init(&card->pcm_mutex);
2418a9764869SKaiChieh Chuang 	spin_lock_init(&card->dpcm_lock);
2419c5af3a2eSMark Brown 
2420e894efefSSrinivas Kandagatla 	return snd_soc_bind_card(card);
2421c5af3a2eSMark Brown }
242270a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2423c5af3a2eSMark Brown 
2424c5af3a2eSMark Brown /**
2425c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2426c5af3a2eSMark Brown  *
2427ac11a2b3SMark Brown  * @card: Card to unregister
2428c5af3a2eSMark Brown  *
2429c5af3a2eSMark Brown  */
243070a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2431c5af3a2eSMark Brown {
2432b545542aSKuninori Morimoto 	mutex_lock(&client_mutex);
2433e894efefSSrinivas Kandagatla 	snd_soc_unbind_card(card, true);
2434b545542aSKuninori Morimoto 	mutex_unlock(&client_mutex);
2435f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2436c5af3a2eSMark Brown 
2437c5af3a2eSMark Brown 	return 0;
2438c5af3a2eSMark Brown }
243970a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2440c5af3a2eSMark Brown 
2441f0fba2adSLiam Girdwood /*
2442f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
2443f0fba2adSLiam Girdwood  * and sanitizing names.
2444f0fba2adSLiam Girdwood  */
24450b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2446f0fba2adSLiam Girdwood {
2447f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
2448f0fba2adSLiam Girdwood 	int id1, id2;
2449f0fba2adSLiam Girdwood 
2450f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
2451f0fba2adSLiam Girdwood 		return NULL;
2452f0fba2adSLiam Girdwood 
245358818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2454f0fba2adSLiam Girdwood 
2455f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
2456f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
2457f0fba2adSLiam Girdwood 	if (found) {
2458f0fba2adSLiam Girdwood 		/* get ID */
2459f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2460f0fba2adSLiam Girdwood 
2461f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
2462f0fba2adSLiam Girdwood 			if (*id == -1)
2463f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
2464f0fba2adSLiam Girdwood 		}
2465f0fba2adSLiam Girdwood 
2466f0fba2adSLiam Girdwood 	} else {
2467f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr" */
2468f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2469f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
2470f0fba2adSLiam Girdwood 
2471f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
247205899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
2473f0fba2adSLiam Girdwood 
2474f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
24752c7b696aSMarcel Ziswiler 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
24762c7b696aSMarcel Ziswiler 				 name);
247758818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2478f0fba2adSLiam Girdwood 		} else
2479f0fba2adSLiam Girdwood 			*id = 0;
2480f0fba2adSLiam Girdwood 	}
2481f0fba2adSLiam Girdwood 
248250014499SKuninori Morimoto 	return devm_kstrdup(dev, name, GFP_KERNEL);
2483f0fba2adSLiam Girdwood }
2484f0fba2adSLiam Girdwood 
2485f0fba2adSLiam Girdwood /*
2486f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
2487f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
2488f0fba2adSLiam Girdwood  */
2489f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
2490f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
2491f0fba2adSLiam Girdwood {
2492f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
249310e8aa9aSMichał Mirosław 		dev_err(dev,
249410e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
249510e8aa9aSMichał Mirosław 			dev_name(dev));
2496f0fba2adSLiam Girdwood 		return NULL;
2497f0fba2adSLiam Girdwood 	}
2498f0fba2adSLiam Girdwood 
249950014499SKuninori Morimoto 	return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2500f0fba2adSLiam Girdwood }
2501f0fba2adSLiam Girdwood 
2502ffdbca0bSKuninori Morimoto void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2503e11381f3SKuninori Morimoto {
2504e11381f3SKuninori Morimoto 	dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2505e11381f3SKuninori Morimoto 	list_del(&dai->list);
2506e11381f3SKuninori Morimoto }
2507ffdbca0bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2508e11381f3SKuninori Morimoto 
25097ca24386SKuninori Morimoto /**
25107ca24386SKuninori Morimoto  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
25117ca24386SKuninori Morimoto  *
25127ca24386SKuninori Morimoto  * @component: The component the DAIs are registered for
25137ca24386SKuninori Morimoto  * @dai_drv: DAI driver to use for the DAI
25147ca24386SKuninori Morimoto  *
25157ca24386SKuninori Morimoto  * Topology can use this API to register DAIs when probing a component.
25167ca24386SKuninori Morimoto  * These DAIs's widgets will be freed in the card cleanup and the DAIs
25177ca24386SKuninori Morimoto  * will be freed in the component cleanup.
25187ca24386SKuninori Morimoto  */
25197ca24386SKuninori Morimoto struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
25205e4fb372SMengdong Lin 					 struct snd_soc_dai_driver *dai_drv,
25215e4fb372SMengdong Lin 					 bool legacy_dai_naming)
25225e4fb372SMengdong Lin {
25235e4fb372SMengdong Lin 	struct device *dev = component->dev;
25245e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
25255e4fb372SMengdong Lin 
25265e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
25275e4fb372SMengdong Lin 
25287ca24386SKuninori Morimoto 	lockdep_assert_held(&client_mutex);
25297ca24386SKuninori Morimoto 
253050014499SKuninori Morimoto 	dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
25315e4fb372SMengdong Lin 	if (dai == NULL)
25325e4fb372SMengdong Lin 		return NULL;
25335e4fb372SMengdong Lin 
25345e4fb372SMengdong Lin 	/*
25355e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
25365e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
25375e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
25385e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
25395e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
25405e4fb372SMengdong Lin 	 * component-less anymore.
25415e4fb372SMengdong Lin 	 */
25425e4fb372SMengdong Lin 	if (legacy_dai_naming &&
25435e4fb372SMengdong Lin 	    (dai_drv->id == 0 || dai_drv->name == NULL)) {
25445e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
25455e4fb372SMengdong Lin 	} else {
25465e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
25475e4fb372SMengdong Lin 		if (dai_drv->id)
25485e4fb372SMengdong Lin 			dai->id = dai_drv->id;
25495e4fb372SMengdong Lin 		else
25505e4fb372SMengdong Lin 			dai->id = component->num_dai;
25515e4fb372SMengdong Lin 	}
255250014499SKuninori Morimoto 	if (!dai->name)
25535e4fb372SMengdong Lin 		return NULL;
25545e4fb372SMengdong Lin 
25555e4fb372SMengdong Lin 	dai->component = component;
25565e4fb372SMengdong Lin 	dai->dev = dev;
25575e4fb372SMengdong Lin 	dai->driver = dai_drv;
25585e4fb372SMengdong Lin 	if (!dai->driver->ops)
25595e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
25605e4fb372SMengdong Lin 
256115a0c645SKuninori Morimoto 	/* see for_each_component_dais */
256258bf4179SKuninori Morimoto 	list_add_tail(&dai->list, &component->dai_list);
25635e4fb372SMengdong Lin 	component->num_dai++;
25645e4fb372SMengdong Lin 
25655e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
25665e4fb372SMengdong Lin 	return dai;
25675e4fb372SMengdong Lin }
2568e11381f3SKuninori Morimoto 
25699115171aSMark Brown /**
25703f6674aeSKuninori Morimoto  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
25713f6674aeSKuninori Morimoto  *
25723f6674aeSKuninori Morimoto  * @component: The component for which the DAIs should be unregistered
25733f6674aeSKuninori Morimoto  */
25743f6674aeSKuninori Morimoto static void snd_soc_unregister_dais(struct snd_soc_component *component)
25753f6674aeSKuninori Morimoto {
25763f6674aeSKuninori Morimoto 	struct snd_soc_dai *dai, *_dai;
25773f6674aeSKuninori Morimoto 
2578e11381f3SKuninori Morimoto 	for_each_component_dais_safe(component, dai, _dai)
2579e11381f3SKuninori Morimoto 		snd_soc_unregister_dai(dai);
25803f6674aeSKuninori Morimoto }
25813f6674aeSKuninori Morimoto 
25823f6674aeSKuninori Morimoto /**
2583daf77373SKuninori Morimoto  * snd_soc_register_dais - Register a DAI with the ASoC core
2584daf77373SKuninori Morimoto  *
2585daf77373SKuninori Morimoto  * @component: The component the DAIs are registered for
2586daf77373SKuninori Morimoto  * @dai_drv: DAI driver to use for the DAIs
2587daf77373SKuninori Morimoto  * @count: Number of DAIs
2588daf77373SKuninori Morimoto  */
2589daf77373SKuninori Morimoto static int snd_soc_register_dais(struct snd_soc_component *component,
2590daf77373SKuninori Morimoto 				 struct snd_soc_dai_driver *dai_drv,
2591daf77373SKuninori Morimoto 				 size_t count)
2592daf77373SKuninori Morimoto {
2593daf77373SKuninori Morimoto 	struct snd_soc_dai *dai;
2594daf77373SKuninori Morimoto 	unsigned int i;
2595daf77373SKuninori Morimoto 	int ret;
2596daf77373SKuninori Morimoto 
2597daf77373SKuninori Morimoto 	for (i = 0; i < count; i++) {
259871cb85f5SKuninori Morimoto 		dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
2599daf77373SKuninori Morimoto 				  !component->driver->non_legacy_dai_naming);
2600daf77373SKuninori Morimoto 		if (dai == NULL) {
2601daf77373SKuninori Morimoto 			ret = -ENOMEM;
2602daf77373SKuninori Morimoto 			goto err;
2603daf77373SKuninori Morimoto 		}
2604daf77373SKuninori Morimoto 	}
2605daf77373SKuninori Morimoto 
2606daf77373SKuninori Morimoto 	return 0;
2607daf77373SKuninori Morimoto 
2608daf77373SKuninori Morimoto err:
2609daf77373SKuninori Morimoto 	snd_soc_unregister_dais(component);
2610daf77373SKuninori Morimoto 
2611daf77373SKuninori Morimoto 	return ret;
2612daf77373SKuninori Morimoto }
2613daf77373SKuninori Morimoto 
2614bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
2615bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
2616d191bd8dSKuninori Morimoto {
26178d92bb51SKuninori Morimoto 	INIT_LIST_HEAD(&component->dai_list);
2618495efdb0SKuninori Morimoto 	INIT_LIST_HEAD(&component->dobj_list);
2619495efdb0SKuninori Morimoto 	INIT_LIST_HEAD(&component->card_list);
26208d92bb51SKuninori Morimoto 	mutex_init(&component->io_mutex);
26218d92bb51SKuninori Morimoto 
2622bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
2623bb13109dSLars-Peter Clausen 	if (!component->name) {
2624bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
2625d191bd8dSKuninori Morimoto 		return -ENOMEM;
2626d191bd8dSKuninori Morimoto 	}
2627d191bd8dSKuninori Morimoto 
2628bb13109dSLars-Peter Clausen 	component->dev = dev;
2629bb13109dSLars-Peter Clausen 	component->driver = driver;
2630e2c330b9SLars-Peter Clausen 
2631bb13109dSLars-Peter Clausen 	return 0;
2632d191bd8dSKuninori Morimoto }
2633d191bd8dSKuninori Morimoto 
263420feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2635886f5692SLars-Peter Clausen {
2636886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
263720feb881SLars-Peter Clausen 
2638886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
2639886f5692SLars-Peter Clausen 	if (val_bytes > 0)
2640886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
2641886f5692SLars-Peter Clausen }
264220feb881SLars-Peter Clausen 
2643e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
2644e874bf5fSLars-Peter Clausen 
264520feb881SLars-Peter Clausen /**
26462c7b696aSMarcel Ziswiler  * snd_soc_component_init_regmap() - Initialize regmap instance for the
26472c7b696aSMarcel Ziswiler  *                                   component
264820feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
264920feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
265020feb881SLars-Peter Clausen  *
265120feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
265220feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
265320feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
265420feb881SLars-Peter Clausen  * before the first IO attempt of the component.
265520feb881SLars-Peter Clausen  */
265620feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
265720feb881SLars-Peter Clausen 	struct regmap *regmap)
265820feb881SLars-Peter Clausen {
265920feb881SLars-Peter Clausen 	component->regmap = regmap;
266020feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
2661886f5692SLars-Peter Clausen }
266220feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
266320feb881SLars-Peter Clausen 
266420feb881SLars-Peter Clausen /**
26652c7b696aSMarcel Ziswiler  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
26662c7b696aSMarcel Ziswiler  *                                   component
266720feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
266820feb881SLars-Peter Clausen  *
266920feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
267020feb881SLars-Peter Clausen  * removes the regmap instance from the component.
267120feb881SLars-Peter Clausen  *
267220feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
267320feb881SLars-Peter Clausen  * to initialize the regmap instance.
267420feb881SLars-Peter Clausen  */
267520feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
267620feb881SLars-Peter Clausen {
267720feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
267820feb881SLars-Peter Clausen 	component->regmap = NULL;
267920feb881SLars-Peter Clausen }
268020feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2681886f5692SLars-Peter Clausen 
2682e874bf5fSLars-Peter Clausen #endif
2683d191bd8dSKuninori Morimoto 
2684273d778eSKuninori Morimoto #define ENDIANNESS_MAP(name) \
2685273d778eSKuninori Morimoto 	(SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2686273d778eSKuninori Morimoto static u64 endianness_format_map[] = {
2687273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S16_),
2688273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U16_),
2689273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_),
2690273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_),
2691273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S32_),
2692273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U32_),
2693273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_3),
2694273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_3),
2695273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S20_3),
2696273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U20_3),
2697273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S18_3),
2698273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U18_3),
2699273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT_),
2700273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT64_),
2701273d778eSKuninori Morimoto 	ENDIANNESS_MAP(IEC958_SUBFRAME_),
2702273d778eSKuninori Morimoto };
2703273d778eSKuninori Morimoto 
2704273d778eSKuninori Morimoto /*
2705273d778eSKuninori Morimoto  * Fix up the DAI formats for endianness: codecs don't actually see
2706273d778eSKuninori Morimoto  * the endianness of the data but we're using the CPU format
2707273d778eSKuninori Morimoto  * definitions which do need to include endianness so we ensure that
2708273d778eSKuninori Morimoto  * codec DAIs always have both big and little endian variants set.
2709273d778eSKuninori Morimoto  */
2710273d778eSKuninori Morimoto static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2711273d778eSKuninori Morimoto {
2712273d778eSKuninori Morimoto 	int i;
2713273d778eSKuninori Morimoto 
2714273d778eSKuninori Morimoto 	for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2715273d778eSKuninori Morimoto 		if (stream->formats & endianness_format_map[i])
2716273d778eSKuninori Morimoto 			stream->formats |= endianness_format_map[i];
2717273d778eSKuninori Morimoto }
2718273d778eSKuninori Morimoto 
2719e894efefSSrinivas Kandagatla static void snd_soc_try_rebind_card(void)
2720e894efefSSrinivas Kandagatla {
2721e894efefSSrinivas Kandagatla 	struct snd_soc_card *card, *c;
2722e894efefSSrinivas Kandagatla 
2723b245d273SKuninori Morimoto 	list_for_each_entry_safe(card, c, &unbind_card_list, list)
2724e894efefSSrinivas Kandagatla 		if (!snd_soc_bind_card(card))
2725e894efefSSrinivas Kandagatla 			list_del(&card->list);
2726e894efefSSrinivas Kandagatla }
2727e894efefSSrinivas Kandagatla 
2728486c7978SKuninori Morimoto static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2729486c7978SKuninori Morimoto {
2730b18768f5SKuninori Morimoto 	struct snd_soc_card *card = component->card;
2731b18768f5SKuninori Morimoto 
2732486c7978SKuninori Morimoto 	snd_soc_unregister_dais(component);
2733b18768f5SKuninori Morimoto 
2734b18768f5SKuninori Morimoto 	if (card)
2735b18768f5SKuninori Morimoto 		snd_soc_unbind_card(card, false);
2736b18768f5SKuninori Morimoto 
2737b18768f5SKuninori Morimoto 	list_del(&component->list);
2738486c7978SKuninori Morimoto }
2739486c7978SKuninori Morimoto 
2740e0dac41bSKuninori Morimoto int snd_soc_add_component(struct device *dev,
2741e0dac41bSKuninori Morimoto 			struct snd_soc_component *component,
2742cf9e829eSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2743d191bd8dSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2744d191bd8dSKuninori Morimoto 			int num_dai)
2745d191bd8dSKuninori Morimoto {
2746bb13109dSLars-Peter Clausen 	int ret;
2747273d778eSKuninori Morimoto 	int i;
2748d191bd8dSKuninori Morimoto 
2749b18768f5SKuninori Morimoto 	mutex_lock(&client_mutex);
2750b18768f5SKuninori Morimoto 
2751cf9e829eSKuninori Morimoto 	ret = snd_soc_component_initialize(component, component_driver, dev);
2752bb13109dSLars-Peter Clausen 	if (ret)
2753bb13109dSLars-Peter Clausen 		goto err_free;
2754bb13109dSLars-Peter Clausen 
2755273d778eSKuninori Morimoto 	if (component_driver->endianness) {
2756273d778eSKuninori Morimoto 		for (i = 0; i < num_dai; i++) {
2757273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].playback);
2758273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].capture);
2759273d778eSKuninori Morimoto 		}
2760273d778eSKuninori Morimoto 	}
2761273d778eSKuninori Morimoto 
27620e7b25c6SKuninori Morimoto 	ret = snd_soc_register_dais(component, dai_drv, num_dai);
2763bb13109dSLars-Peter Clausen 	if (ret < 0) {
2764f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
2765bb13109dSLars-Peter Clausen 		goto err_cleanup;
2766bb13109dSLars-Peter Clausen 	}
2767bb13109dSLars-Peter Clausen 
2768b18768f5SKuninori Morimoto 	if (!component->driver->write && !component->driver->read) {
2769b18768f5SKuninori Morimoto 		if (!component->regmap)
2770b18768f5SKuninori Morimoto 			component->regmap = dev_get_regmap(component->dev,
2771b18768f5SKuninori Morimoto 							   NULL);
2772b18768f5SKuninori Morimoto 		if (component->regmap)
2773b18768f5SKuninori Morimoto 			snd_soc_component_setup_regmap(component);
2774b18768f5SKuninori Morimoto 	}
2775bb13109dSLars-Peter Clausen 
2776b18768f5SKuninori Morimoto 	/* see for_each_component */
2777b18768f5SKuninori Morimoto 	list_add(&component->list, &component_list);
2778bb13109dSLars-Peter Clausen 
2779bb13109dSLars-Peter Clausen err_cleanup:
2780b18768f5SKuninori Morimoto 	if (ret < 0)
2781486c7978SKuninori Morimoto 		snd_soc_del_component_unlocked(component);
2782bb13109dSLars-Peter Clausen err_free:
2783b18768f5SKuninori Morimoto 	mutex_unlock(&client_mutex);
2784b18768f5SKuninori Morimoto 
2785b18768f5SKuninori Morimoto 	if (ret == 0)
2786b18768f5SKuninori Morimoto 		snd_soc_try_rebind_card();
2787b18768f5SKuninori Morimoto 
2788bb13109dSLars-Peter Clausen 	return ret;
2789d191bd8dSKuninori Morimoto }
2790e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component);
2791e0dac41bSKuninori Morimoto 
2792e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev,
2793e0dac41bSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2794e0dac41bSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2795e0dac41bSKuninori Morimoto 			int num_dai)
2796e0dac41bSKuninori Morimoto {
2797e0dac41bSKuninori Morimoto 	struct snd_soc_component *component;
2798e0dac41bSKuninori Morimoto 
27997ecbd6a9SKuninori Morimoto 	component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
280008e61d03SKuninori Morimoto 	if (!component)
2801e0dac41bSKuninori Morimoto 		return -ENOMEM;
2802e0dac41bSKuninori Morimoto 
2803e0dac41bSKuninori Morimoto 	return snd_soc_add_component(dev, component, component_driver,
2804e0dac41bSKuninori Morimoto 				     dai_drv, num_dai);
2805e0dac41bSKuninori Morimoto }
2806d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
2807d191bd8dSKuninori Morimoto 
2808d191bd8dSKuninori Morimoto /**
28092eccea8cSKuninori Morimoto  * snd_soc_unregister_component - Unregister all related component
28102eccea8cSKuninori Morimoto  * from the ASoC core
2811d191bd8dSKuninori Morimoto  *
2812628536eaSJonathan Corbet  * @dev: The device to unregister
2813d191bd8dSKuninori Morimoto  */
28142eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
28152eccea8cSKuninori Morimoto {
2816ac6a4dd3SKuninori Morimoto 	struct snd_soc_component *component;
2817ac6a4dd3SKuninori Morimoto 
2818ac6a4dd3SKuninori Morimoto 	mutex_lock(&client_mutex);
2819ac6a4dd3SKuninori Morimoto 	while (1) {
282018dd66eaSKuninori Morimoto 		component = snd_soc_lookup_component_nolocked(dev, NULL);
2821ac6a4dd3SKuninori Morimoto 		if (!component)
2822ac6a4dd3SKuninori Morimoto 			break;
2823ac6a4dd3SKuninori Morimoto 
2824ac6a4dd3SKuninori Morimoto 		snd_soc_del_component_unlocked(component);
2825ac6a4dd3SKuninori Morimoto 	}
2826ac6a4dd3SKuninori Morimoto 	mutex_unlock(&client_mutex);
2827d191bd8dSKuninori Morimoto }
2828d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2829d191bd8dSKuninori Morimoto 
2830bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
2831b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2832bec4fa05SStephen Warren 			       const char *propname)
2833bec4fa05SStephen Warren {
2834b07609ceSKuninori Morimoto 	struct device_node *np;
2835bec4fa05SStephen Warren 	int ret;
2836bec4fa05SStephen Warren 
28377e07e7c0STushar Behera 	if (!card->dev) {
28387e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
28397e07e7c0STushar Behera 		return -EINVAL;
28407e07e7c0STushar Behera 	}
28417e07e7c0STushar Behera 
28427e07e7c0STushar Behera 	np = card->dev->of_node;
28437e07e7c0STushar Behera 
2844bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
2845bec4fa05SStephen Warren 	/*
2846bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
2847bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
2848bec4fa05SStephen Warren 	 * snd_soc_register_card.
2849bec4fa05SStephen Warren 	 */
2850bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
2851bec4fa05SStephen Warren 		dev_err(card->dev,
2852f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
2853bec4fa05SStephen Warren 			propname, ret);
2854bec4fa05SStephen Warren 		return ret;
2855bec4fa05SStephen Warren 	}
2856bec4fa05SStephen Warren 
2857bec4fa05SStephen Warren 	return 0;
2858bec4fa05SStephen Warren }
2859b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2860bec4fa05SStephen Warren 
28619a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
28629a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
28639a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
28649a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
28659a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
28669a6d4860SXiubo Li };
28679a6d4860SXiubo Li 
286821efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
28699a6d4860SXiubo Li 					  const char *propname)
28709a6d4860SXiubo Li {
287121efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
28729a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
28739a6d4860SXiubo Li 	const char *template, *wname;
28749a6d4860SXiubo Li 	int i, j, num_widgets, ret;
28759a6d4860SXiubo Li 
28769a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
28779a6d4860SXiubo Li 	if (num_widgets < 0) {
28789a6d4860SXiubo Li 		dev_err(card->dev,
28799a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
28809a6d4860SXiubo Li 		return -EINVAL;
28819a6d4860SXiubo Li 	}
28829a6d4860SXiubo Li 	if (num_widgets & 1) {
28839a6d4860SXiubo Li 		dev_err(card->dev,
28849a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
28859a6d4860SXiubo Li 		return -EINVAL;
28869a6d4860SXiubo Li 	}
28879a6d4860SXiubo Li 
28889a6d4860SXiubo Li 	num_widgets /= 2;
28899a6d4860SXiubo Li 	if (!num_widgets) {
28909a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
28919a6d4860SXiubo Li 			propname);
28929a6d4860SXiubo Li 		return -EINVAL;
28939a6d4860SXiubo Li 	}
28949a6d4860SXiubo Li 
28959a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
28969a6d4860SXiubo Li 			       GFP_KERNEL);
28979a6d4860SXiubo Li 	if (!widgets) {
28989a6d4860SXiubo Li 		dev_err(card->dev,
28999a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
29009a6d4860SXiubo Li 		return -ENOMEM;
29019a6d4860SXiubo Li 	}
29029a6d4860SXiubo Li 
29039a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
29049a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
29059a6d4860SXiubo Li 			2 * i, &template);
29069a6d4860SXiubo Li 		if (ret) {
29079a6d4860SXiubo Li 			dev_err(card->dev,
29089a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
29099a6d4860SXiubo Li 				propname, 2 * i, ret);
29109a6d4860SXiubo Li 			return -EINVAL;
29119a6d4860SXiubo Li 		}
29129a6d4860SXiubo Li 
29139a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
29149a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
29159a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
29169a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
29179a6d4860SXiubo Li 				break;
29189a6d4860SXiubo Li 			}
29199a6d4860SXiubo Li 		}
29209a6d4860SXiubo Li 
29219a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
29229a6d4860SXiubo Li 			dev_err(card->dev,
29239a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
29249a6d4860SXiubo Li 				template);
29259a6d4860SXiubo Li 			return -EINVAL;
29269a6d4860SXiubo Li 		}
29279a6d4860SXiubo Li 
29289a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
29299a6d4860SXiubo Li 						    (2 * i) + 1,
29309a6d4860SXiubo Li 						    &wname);
29319a6d4860SXiubo Li 		if (ret) {
29329a6d4860SXiubo Li 			dev_err(card->dev,
29339a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
29349a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
29359a6d4860SXiubo Li 			return -EINVAL;
29369a6d4860SXiubo Li 		}
29379a6d4860SXiubo Li 
29389a6d4860SXiubo Li 		widgets[i].name = wname;
29399a6d4860SXiubo Li 	}
29409a6d4860SXiubo Li 
2941f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
2942f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
29439a6d4860SXiubo Li 
29449a6d4860SXiubo Li 	return 0;
29459a6d4860SXiubo Li }
294621efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
29479a6d4860SXiubo Li 
2948cbdfab3bSJerome Brunet int snd_soc_of_get_slot_mask(struct device_node *np,
29496131084aSJyri Sarha 			     const char *prop_name,
29506131084aSJyri Sarha 			     unsigned int *mask)
29516131084aSJyri Sarha {
29526131084aSJyri Sarha 	u32 val;
29536c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
29546131084aSJyri Sarha 	int i;
29556131084aSJyri Sarha 
29566131084aSJyri Sarha 	if (!of_slot_mask)
29576131084aSJyri Sarha 		return 0;
29586131084aSJyri Sarha 	val /= sizeof(u32);
29596131084aSJyri Sarha 	for (i = 0; i < val; i++)
29606131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
29616131084aSJyri Sarha 			*mask |= (1 << i);
29626131084aSJyri Sarha 
29636131084aSJyri Sarha 	return val;
29646131084aSJyri Sarha }
2965cbdfab3bSJerome Brunet EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
29666131084aSJyri Sarha 
296789c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
29686131084aSJyri Sarha 			      unsigned int *tx_mask,
29696131084aSJyri Sarha 			      unsigned int *rx_mask,
297089c67857SXiubo Li 			      unsigned int *slots,
297189c67857SXiubo Li 			      unsigned int *slot_width)
297289c67857SXiubo Li {
297389c67857SXiubo Li 	u32 val;
297489c67857SXiubo Li 	int ret;
297589c67857SXiubo Li 
29766131084aSJyri Sarha 	if (tx_mask)
29776131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
29786131084aSJyri Sarha 	if (rx_mask)
29796131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
29806131084aSJyri Sarha 
298189c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
298289c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
298389c67857SXiubo Li 		if (ret)
298489c67857SXiubo Li 			return ret;
298589c67857SXiubo Li 
298689c67857SXiubo Li 		if (slots)
298789c67857SXiubo Li 			*slots = val;
298889c67857SXiubo Li 	}
298989c67857SXiubo Li 
299089c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
299189c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
299289c67857SXiubo Li 		if (ret)
299389c67857SXiubo Li 			return ret;
299489c67857SXiubo Li 
299589c67857SXiubo Li 		if (slot_width)
299689c67857SXiubo Li 			*slot_width = val;
299789c67857SXiubo Li 	}
299889c67857SXiubo Li 
299989c67857SXiubo Li 	return 0;
300089c67857SXiubo Li }
300189c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
300289c67857SXiubo Li 
30033b710356SKuninori Morimoto void snd_soc_of_parse_node_prefix(struct device_node *np,
30045e3cdaa2SKuninori Morimoto 				  struct snd_soc_codec_conf *codec_conf,
30055e3cdaa2SKuninori Morimoto 				  struct device_node *of_node,
30065e3cdaa2SKuninori Morimoto 				  const char *propname)
30075e3cdaa2SKuninori Morimoto {
30085e3cdaa2SKuninori Morimoto 	const char *str;
30095e3cdaa2SKuninori Morimoto 	int ret;
30105e3cdaa2SKuninori Morimoto 
30115e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
30125e3cdaa2SKuninori Morimoto 	if (ret < 0) {
30135e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
30145e3cdaa2SKuninori Morimoto 		return;
30155e3cdaa2SKuninori Morimoto 	}
30165e3cdaa2SKuninori Morimoto 
30175e3cdaa2SKuninori Morimoto 	codec_conf->of_node	= of_node;
30185e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
30195e3cdaa2SKuninori Morimoto }
30203b710356SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
30215e3cdaa2SKuninori Morimoto 
30222bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3023a4a54dd5SStephen Warren 				   const char *propname)
3024a4a54dd5SStephen Warren {
30252bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
3026e3b1e6a1SMark Brown 	int num_routes;
3027a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3028a4a54dd5SStephen Warren 	int i, ret;
3029a4a54dd5SStephen Warren 
3030a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3031c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
303210e8aa9aSMichał Mirosław 		dev_err(card->dev,
303310e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
303410e8aa9aSMichał Mirosław 			propname);
3035a4a54dd5SStephen Warren 		return -EINVAL;
3036a4a54dd5SStephen Warren 	}
3037a4a54dd5SStephen Warren 	num_routes /= 2;
3038a4a54dd5SStephen Warren 	if (!num_routes) {
3039f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3040a4a54dd5SStephen Warren 			propname);
3041a4a54dd5SStephen Warren 		return -EINVAL;
3042a4a54dd5SStephen Warren 	}
3043a4a54dd5SStephen Warren 
3044a86854d0SKees Cook 	routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
3045a4a54dd5SStephen Warren 			      GFP_KERNEL);
3046a4a54dd5SStephen Warren 	if (!routes) {
3047a4a54dd5SStephen Warren 		dev_err(card->dev,
3048f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3049a4a54dd5SStephen Warren 		return -EINVAL;
3050a4a54dd5SStephen Warren 	}
3051a4a54dd5SStephen Warren 
3052a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3053a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3054e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
3055a4a54dd5SStephen Warren 		if (ret) {
3056c871bd0bSMark Brown 			dev_err(card->dev,
3057c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3058c871bd0bSMark Brown 				propname, 2 * i, ret);
3059a4a54dd5SStephen Warren 			return -EINVAL;
3060a4a54dd5SStephen Warren 		}
3061a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3062e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3063a4a54dd5SStephen Warren 		if (ret) {
3064a4a54dd5SStephen Warren 			dev_err(card->dev,
3065c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3066c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3067a4a54dd5SStephen Warren 			return -EINVAL;
3068a4a54dd5SStephen Warren 		}
3069a4a54dd5SStephen Warren 	}
3070a4a54dd5SStephen Warren 
3071f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
3072f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
3073a4a54dd5SStephen Warren 
3074a4a54dd5SStephen Warren 	return 0;
3075a4a54dd5SStephen Warren }
30762bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3077a4a54dd5SStephen Warren 
3078a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3079389cb834SJyri Sarha 				     const char *prefix,
3080389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3081389cb834SJyri Sarha 				     struct device_node **framemaster)
3082a7930ed4SKuninori Morimoto {
3083a7930ed4SKuninori Morimoto 	int ret, i;
3084a7930ed4SKuninori Morimoto 	char prop[128];
3085a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3086a7930ed4SKuninori Morimoto 	int bit, frame;
3087a7930ed4SKuninori Morimoto 	const char *str;
3088a7930ed4SKuninori Morimoto 	struct {
3089a7930ed4SKuninori Morimoto 		char *name;
3090a7930ed4SKuninori Morimoto 		unsigned int val;
3091a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3092a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3093a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3094a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3095a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3096a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3097a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3098a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3099a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3100a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3101a7930ed4SKuninori Morimoto 	};
3102a7930ed4SKuninori Morimoto 
3103a7930ed4SKuninori Morimoto 	if (!prefix)
3104a7930ed4SKuninori Morimoto 		prefix = "";
3105a7930ed4SKuninori Morimoto 
3106a7930ed4SKuninori Morimoto 	/*
31075711c979SKuninori Morimoto 	 * check "dai-format = xxx"
31085711c979SKuninori Morimoto 	 * or    "[prefix]format = xxx"
3109a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3110a7930ed4SKuninori Morimoto 	 */
31115711c979SKuninori Morimoto 	ret = of_property_read_string(np, "dai-format", &str);
31125711c979SKuninori Morimoto 	if (ret < 0) {
3113a7930ed4SKuninori Morimoto 		snprintf(prop, sizeof(prop), "%sformat", prefix);
3114a7930ed4SKuninori Morimoto 		ret = of_property_read_string(np, prop, &str);
31155711c979SKuninori Morimoto 	}
3116a7930ed4SKuninori Morimoto 	if (ret == 0) {
3117a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3118a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3119a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3120a7930ed4SKuninori Morimoto 				break;
3121a7930ed4SKuninori Morimoto 			}
3122a7930ed4SKuninori Morimoto 		}
3123a7930ed4SKuninori Morimoto 	}
3124a7930ed4SKuninori Morimoto 
3125a7930ed4SKuninori Morimoto 	/*
31268c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3127a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3128a7930ed4SKuninori Morimoto 	 */
31298c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
313051930295SJulia Lawall 	if (of_property_read_bool(np, prop))
31318c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
31328c2d6a9fSKuninori Morimoto 	else
31338c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3134a7930ed4SKuninori Morimoto 
3135a7930ed4SKuninori Morimoto 	/*
3136a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3137a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3138a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3139a7930ed4SKuninori Morimoto 	 */
3140a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3141a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3142a7930ed4SKuninori Morimoto 
3143a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3144a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3145a7930ed4SKuninori Morimoto 
3146a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3147a7930ed4SKuninori Morimoto 	case 0x11:
3148a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3149a7930ed4SKuninori Morimoto 		break;
3150a7930ed4SKuninori Morimoto 	case 0x10:
3151a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3152a7930ed4SKuninori Morimoto 		break;
3153a7930ed4SKuninori Morimoto 	case 0x01:
3154a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3155a7930ed4SKuninori Morimoto 		break;
3156a7930ed4SKuninori Morimoto 	default:
3157a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3158a7930ed4SKuninori Morimoto 		break;
3159a7930ed4SKuninori Morimoto 	}
3160a7930ed4SKuninori Morimoto 
3161a7930ed4SKuninori Morimoto 	/*
3162a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3163a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3164a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3165a7930ed4SKuninori Morimoto 	 */
3166a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3167a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3168389cb834SJyri Sarha 	if (bit && bitclkmaster)
3169389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3170a7930ed4SKuninori Morimoto 
3171a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3172a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3173389cb834SJyri Sarha 	if (frame && framemaster)
3174389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3175a7930ed4SKuninori Morimoto 
3176a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3177a7930ed4SKuninori Morimoto 	case 0x11:
3178a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3179a7930ed4SKuninori Morimoto 		break;
3180a7930ed4SKuninori Morimoto 	case 0x10:
3181a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3182a7930ed4SKuninori Morimoto 		break;
3183a7930ed4SKuninori Morimoto 	case 0x01:
3184a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3185a7930ed4SKuninori Morimoto 		break;
3186a7930ed4SKuninori Morimoto 	default:
3187a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3188a7930ed4SKuninori Morimoto 		break;
3189a7930ed4SKuninori Morimoto 	}
3190a7930ed4SKuninori Morimoto 
3191a7930ed4SKuninori Morimoto 	return format;
3192a7930ed4SKuninori Morimoto }
3193a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3194a7930ed4SKuninori Morimoto 
3195a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
3196a180e8b9SKuninori Morimoto {
319709d4cc03SKuninori Morimoto 	struct snd_soc_component *component;
3198c1e230f0SKuninori Morimoto 	struct snd_soc_dai_link_component dlc;
3199a180e8b9SKuninori Morimoto 	int ret;
3200a180e8b9SKuninori Morimoto 
3201c1e230f0SKuninori Morimoto 	dlc.of_node	= of_graph_get_port_parent(ep);
3202c1e230f0SKuninori Morimoto 	dlc.name	= NULL;
3203a180e8b9SKuninori Morimoto 	/*
3204a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
3205a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
3206a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
3207a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
3208a180e8b9SKuninori Morimoto 	 */
3209a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
3210a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
3211c1e230f0SKuninori Morimoto 	component = soc_find_component(&dlc);
32122c7b1704SKuninori Morimoto 	if (component)
32132c7b1704SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_id(component, ep);
3214a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
3215a180e8b9SKuninori Morimoto 
3216c1e230f0SKuninori Morimoto 	of_node_put(dlc.of_node);
3217c0a480d1STony Lindgren 
3218a180e8b9SKuninori Morimoto 	return ret;
3219a180e8b9SKuninori Morimoto }
3220a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3221a180e8b9SKuninori Morimoto 
32221ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args,
3223cb470087SKuninori Morimoto 				const char **dai_name)
3224cb470087SKuninori Morimoto {
3225cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
32263e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
322793b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3228cb470087SKuninori Morimoto 
3229cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3230368dee94SKuninori Morimoto 	for_each_component(pos) {
3231c0834440SKuninori Morimoto 		component_of_node = soc_component_to_node(pos);
32323e0aa8d8SJyri Sarha 
32333e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
3234cb470087SKuninori Morimoto 			continue;
3235cb470087SKuninori Morimoto 
3236a2a34175SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3237a2a34175SKuninori Morimoto 		if (ret == -ENOTSUPP) {
323858bf4179SKuninori Morimoto 			struct snd_soc_dai *dai;
32396833c452SKuninori Morimoto 			int id = -1;
32406833c452SKuninori Morimoto 
324193b0f3eeSJean-Francois Moine 			switch (args->args_count) {
32426833c452SKuninori Morimoto 			case 0:
32436833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
32446833c452SKuninori Morimoto 				break;
32456833c452SKuninori Morimoto 			case 1:
324693b0f3eeSJean-Francois Moine 				id = args->args[0];
32476833c452SKuninori Morimoto 				break;
32486833c452SKuninori Morimoto 			default:
32496833c452SKuninori Morimoto 				/* not supported */
3250cb470087SKuninori Morimoto 				break;
3251cb470087SKuninori Morimoto 			}
3252cb470087SKuninori Morimoto 
32536833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
32546833c452SKuninori Morimoto 				ret = -EINVAL;
32553dcba280SNicolin Chen 				continue;
32566833c452SKuninori Morimoto 			}
3257e41975edSXiubo Li 
3258e41975edSXiubo Li 			ret = 0;
3259e41975edSXiubo Li 
326058bf4179SKuninori Morimoto 			/* find target DAI */
326115a0c645SKuninori Morimoto 			for_each_component_dais(pos, dai) {
326258bf4179SKuninori Morimoto 				if (id == 0)
326358bf4179SKuninori Morimoto 					break;
326458bf4179SKuninori Morimoto 				id--;
326558bf4179SKuninori Morimoto 			}
326658bf4179SKuninori Morimoto 
326758bf4179SKuninori Morimoto 			*dai_name = dai->driver->name;
3268e41975edSXiubo Li 			if (!*dai_name)
3269e41975edSXiubo Li 				*dai_name = pos->name;
32706833c452SKuninori Morimoto 		}
32716833c452SKuninori Morimoto 
3272cb470087SKuninori Morimoto 		break;
3273cb470087SKuninori Morimoto 	}
3274cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
327593b0f3eeSJean-Francois Moine 	return ret;
327693b0f3eeSJean-Francois Moine }
32771ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
327893b0f3eeSJean-Francois Moine 
327993b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
328093b0f3eeSJean-Francois Moine 			    const char **dai_name)
328193b0f3eeSJean-Francois Moine {
328293b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
328393b0f3eeSJean-Francois Moine 	int ret;
328493b0f3eeSJean-Francois Moine 
328593b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
328693b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
328793b0f3eeSJean-Francois Moine 	if (ret)
328893b0f3eeSJean-Francois Moine 		return ret;
328993b0f3eeSJean-Francois Moine 
329093b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3291cb470087SKuninori Morimoto 
3292cb470087SKuninori Morimoto 	of_node_put(args.np);
3293cb470087SKuninori Morimoto 
3294cb470087SKuninori Morimoto 	return ret;
3295cb470087SKuninori Morimoto }
3296cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3297cb470087SKuninori Morimoto 
329893b0f3eeSJean-Francois Moine /*
329994685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
330094685763SSylwester Nawrocki  * @dai_link: DAI link
330194685763SSylwester Nawrocki  *
330294685763SSylwester Nawrocki  * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
330394685763SSylwester Nawrocki  */
330494685763SSylwester Nawrocki void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
330594685763SSylwester Nawrocki {
33063db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *component;
330794685763SSylwester Nawrocki 	int index;
330894685763SSylwester Nawrocki 
33093db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
331094685763SSylwester Nawrocki 		if (!component->of_node)
331194685763SSylwester Nawrocki 			break;
331294685763SSylwester Nawrocki 		of_node_put(component->of_node);
331394685763SSylwester Nawrocki 		component->of_node = NULL;
331494685763SSylwester Nawrocki 	}
331594685763SSylwester Nawrocki }
331694685763SSylwester Nawrocki EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
331794685763SSylwester Nawrocki 
331894685763SSylwester Nawrocki /*
331993b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
332093b0f3eeSJean-Francois Moine  * @dev: Card device
332193b0f3eeSJean-Francois Moine  * @of_node: Device node
332293b0f3eeSJean-Francois Moine  * @dai_link: DAI link
332393b0f3eeSJean-Francois Moine  *
332493b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
332593b0f3eeSJean-Francois Moine  * 'sound-dai'.
332693b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
332794685763SSylwester Nawrocki  * The device nodes in the array (of_node) must be dereferenced by calling
332894685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs() on @dai_link.
332993b0f3eeSJean-Francois Moine  *
333093b0f3eeSJean-Francois Moine  * Returns 0 for success
333193b0f3eeSJean-Francois Moine  */
333293b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
333393b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
333493b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
333593b0f3eeSJean-Francois Moine {
333693b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
333793b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
333893b0f3eeSJean-Francois Moine 	char *name;
333993b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
334093b0f3eeSJean-Francois Moine 
334193b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
334293b0f3eeSJean-Francois Moine 	name = "sound-dai";
334393b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
334493b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
334593b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
334693b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
334793b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
334893b0f3eeSJean-Francois Moine 		else
334993b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
335093b0f3eeSJean-Francois Moine 		return num_codecs;
335193b0f3eeSJean-Francois Moine 	}
3352a86854d0SKees Cook 	component = devm_kcalloc(dev,
3353a86854d0SKees Cook 				 num_codecs, sizeof(*component),
335493b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
335593b0f3eeSJean-Francois Moine 	if (!component)
335693b0f3eeSJean-Francois Moine 		return -ENOMEM;
335793b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
335893b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
335993b0f3eeSJean-Francois Moine 
336093b0f3eeSJean-Francois Moine 	/* Parse the list */
33613db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
336293b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
336393b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
336493b0f3eeSJean-Francois Moine 						 index, &args);
336593b0f3eeSJean-Francois Moine 		if (ret)
336693b0f3eeSJean-Francois Moine 			goto err;
336793b0f3eeSJean-Francois Moine 		component->of_node = args.np;
336893b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
336993b0f3eeSJean-Francois Moine 		if (ret < 0)
337093b0f3eeSJean-Francois Moine 			goto err;
337193b0f3eeSJean-Francois Moine 	}
337293b0f3eeSJean-Francois Moine 	return 0;
337393b0f3eeSJean-Francois Moine err:
337494685763SSylwester Nawrocki 	snd_soc_of_put_dai_link_codecs(dai_link);
337593b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
337693b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
337793b0f3eeSJean-Francois Moine 	return ret;
337893b0f3eeSJean-Francois Moine }
337993b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
338093b0f3eeSJean-Francois Moine 
3381c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3382db2a4165SFrank Mandarino {
33836553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
3384fb257897SMark Brown 	snd_soc_util_init();
3385fb257897SMark Brown 
3386db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3387db2a4165SFrank Mandarino }
33884abe8e16SMark Brown module_init(snd_soc_init);
3389db2a4165SFrank Mandarino 
33907d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3391db2a4165SFrank Mandarino {
3392fb257897SMark Brown 	snd_soc_util_exit();
33936553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3394fb257897SMark Brown 
3395db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3396db2a4165SFrank Mandarino }
3397db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3398db2a4165SFrank Mandarino 
3399db2a4165SFrank Mandarino /* Module information */
3400d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3401db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3402db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
34038b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3404