xref: /openbmc/linux/sound/soc/soc-core.c (revision cc73390008c9a47c49ad73c459b5590fd4c4c890)
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 
4220ced7b05SKuninori Morimoto 	flush_delayed_work(&rtd->delayed_work);
4230ced7b05SKuninori Morimoto 	snd_soc_pcm_component_free(rtd);
4240ced7b05SKuninori Morimoto 
425b7c5bc45SKuninori Morimoto 	/*
426b7c5bc45SKuninori Morimoto 	 * we don't need to call kfree() for rtd->dev
427b7c5bc45SKuninori Morimoto 	 * see
428b7c5bc45SKuninori Morimoto 	 *	soc_release_rtd_dev()
429d918a376SKuninori Morimoto 	 *
430d918a376SKuninori Morimoto 	 * We don't need rtd->dev NULL check, because
431d918a376SKuninori Morimoto 	 * it is alloced *before* rtd.
432d918a376SKuninori Morimoto 	 * see
433d918a376SKuninori Morimoto 	 *	soc_new_pcm_runtime()
434b7c5bc45SKuninori Morimoto 	 */
435b7c5bc45SKuninori Morimoto 	device_unregister(rtd->dev);
4361c93a9e0SKuninori Morimoto }
4371c93a9e0SKuninori Morimoto 
4381a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
4391a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
4401a497983SMengdong Lin {
4411a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
442d918a376SKuninori Morimoto 	struct device *dev;
4436e864344SKuninori Morimoto 	int ret;
4441a497983SMengdong Lin 
4456e864344SKuninori Morimoto 	/*
446d918a376SKuninori Morimoto 	 * for rtd->dev
447d918a376SKuninori Morimoto 	 */
448d918a376SKuninori Morimoto 	dev = kzalloc(sizeof(struct device), GFP_KERNEL);
449d918a376SKuninori Morimoto 	if (!dev)
450d918a376SKuninori Morimoto 		return NULL;
451d918a376SKuninori Morimoto 
452d918a376SKuninori Morimoto 	dev->parent	= card->dev;
453d918a376SKuninori Morimoto 	dev->release	= soc_release_rtd_dev;
454d918a376SKuninori Morimoto 	dev->groups	= soc_dev_attr_groups;
455d918a376SKuninori Morimoto 
456d918a376SKuninori Morimoto 	dev_set_name(dev, "%s", dai_link->name);
457d918a376SKuninori Morimoto 
458d918a376SKuninori Morimoto 	ret = device_register(dev);
459d918a376SKuninori Morimoto 	if (ret < 0) {
460d918a376SKuninori Morimoto 		put_device(dev); /* soc_release_rtd_dev */
461d918a376SKuninori Morimoto 		return NULL;
462d918a376SKuninori Morimoto 	}
463d918a376SKuninori Morimoto 
464d918a376SKuninori Morimoto 	/*
4656e864344SKuninori Morimoto 	 * for rtd
4666e864344SKuninori Morimoto 	 */
4674dc0e7dfSKuninori Morimoto 	rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL);
4681a497983SMengdong Lin 	if (!rtd)
4696e864344SKuninori Morimoto 		goto free_rtd;
4701a497983SMengdong Lin 
471d918a376SKuninori Morimoto 	rtd->dev = dev;
472d918a376SKuninori Morimoto 	dev_set_drvdata(dev, rtd);
473d918a376SKuninori Morimoto 
4746e864344SKuninori Morimoto 	/*
4756e864344SKuninori Morimoto 	 * for rtd->codec_dais
4766e864344SKuninori Morimoto 	 */
4774dc0e7dfSKuninori Morimoto 	rtd->codec_dais = devm_kcalloc(dev, dai_link->num_codecs,
4786396bb22SKees Cook 					sizeof(struct snd_soc_dai *),
4791a497983SMengdong Lin 					GFP_KERNEL);
4806e864344SKuninori Morimoto 	if (!rtd->codec_dais)
4816e864344SKuninori Morimoto 		goto free_rtd;
4826e864344SKuninori Morimoto 
4836e864344SKuninori Morimoto 	/*
4846e864344SKuninori Morimoto 	 * rtd remaining settings
4856e864344SKuninori Morimoto 	 */
486929deb84SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
4876e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
4886e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
4896e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
4906e864344SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
4916e864344SKuninori Morimoto 
492929deb84SKuninori Morimoto 	rtd->card = card;
493929deb84SKuninori Morimoto 	rtd->dai_link = dai_link;
494929deb84SKuninori Morimoto 	if (!rtd->dai_link->ops)
495929deb84SKuninori Morimoto 		rtd->dai_link->ops = &null_snd_soc_ops;
496929deb84SKuninori Morimoto 
4976634e3d6SKuninori Morimoto 	/* see for_each_card_rtds */
4981a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
4991a497983SMengdong Lin 	rtd->num = card->num_rtd;
5001a497983SMengdong Lin 	card->num_rtd++;
501a848125eSKuninori Morimoto 
502a848125eSKuninori Morimoto 	return rtd;
5036e864344SKuninori Morimoto 
5046e864344SKuninori Morimoto free_rtd:
5056e864344SKuninori Morimoto 	soc_free_pcm_runtime(rtd);
5066e864344SKuninori Morimoto 	return NULL;
5071a497983SMengdong Lin }
5081a497983SMengdong Lin 
50947c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
51047c88fffSLiam Girdwood 		const char *dai_link)
51147c88fffSLiam Girdwood {
5121a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
51347c88fffSLiam Girdwood 
514bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5151a497983SMengdong Lin 		if (!strcmp(rtd->dai_link->name, dai_link))
5161a497983SMengdong Lin 			return rtd;
51747c88fffSLiam Girdwood 	}
518f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
51947c88fffSLiam Girdwood 	return NULL;
52047c88fffSLiam Girdwood }
52147c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
52247c88fffSLiam Girdwood 
52365462e44SKuninori Morimoto static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
52465462e44SKuninori Morimoto {
52565462e44SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
52665462e44SKuninori Morimoto 
52765462e44SKuninori Morimoto 	for_each_card_rtds(card, rtd)
52865462e44SKuninori Morimoto 		flush_delayed_work(&rtd->delayed_work);
52965462e44SKuninori Morimoto }
53065462e44SKuninori Morimoto 
5316f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
532db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5336f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
534db2a4165SFrank Mandarino {
5356f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
536d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
5371a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
5381a497983SMengdong Lin 	int i;
539db2a4165SFrank Mandarino 
540c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
541c5599b87SLars-Peter Clausen 	if (!card->instantiated)
542e3509ff0SDaniel Mack 		return 0;
543e3509ff0SDaniel Mack 
5442c7b696aSMarcel Ziswiler 	/*
5452c7b696aSMarcel Ziswiler 	 * Due to the resume being scheduled into a workqueue we could
5466ed25978SAndy Green 	 * suspend before that's finished - wait for it to complete.
5476ed25978SAndy Green 	 */
548f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
5496ed25978SAndy Green 
5506ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
551f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5526ed25978SAndy Green 
553a00f90f9SMark Brown 	/* mute any active DACs */
554bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5550b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
5563efab7dcSMark Brown 
5571a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5583efab7dcSMark Brown 			continue;
5593efab7dcSMark Brown 
5600b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
56188fdffa2SKuninori Morimoto 			if (dai->playback_active)
56288fdffa2SKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 1,
56388fdffa2SKuninori Morimoto 						SNDRV_PCM_STREAM_PLAYBACK);
564db2a4165SFrank Mandarino 		}
56588bd870fSBenoit Cousson 	}
566db2a4165SFrank Mandarino 
5674ccab3e7SLiam Girdwood 	/* suspend all pcms */
568bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5691a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5703efab7dcSMark Brown 			continue;
5713efab7dcSMark Brown 
5721a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
5733efab7dcSMark Brown 	}
5744ccab3e7SLiam Girdwood 
57587506549SMark Brown 	if (card->suspend_pre)
57670b2ac12SMark Brown 		card->suspend_pre(card);
577db2a4165SFrank Mandarino 
578bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5791a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
5803efab7dcSMark Brown 
5811a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5823efab7dcSMark Brown 			continue;
5833efab7dcSMark Brown 
584e0f22622SKuninori Morimoto 		if (!cpu_dai->driver->bus_control)
585e0f22622SKuninori Morimoto 			snd_soc_dai_suspend(cpu_dai);
586db2a4165SFrank Mandarino 	}
587db2a4165SFrank Mandarino 
58837660b6dSLars-Peter Clausen 	/* close any waiting streams */
58965462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
590db2a4165SFrank Mandarino 
591bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5923efab7dcSMark Brown 
5931a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5943efab7dcSMark Brown 			continue;
5953efab7dcSMark Brown 
5961a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
5977bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
598db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
599f0fba2adSLiam Girdwood 
6001a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
6017bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
602db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
603db2a4165SFrank Mandarino 	}
604db2a4165SFrank Mandarino 
6058be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
6068be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
607e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
608e2d32ff6SMark Brown 
6099178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
610f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
6112c7b696aSMarcel Ziswiler 		struct snd_soc_dapm_context *dapm =
6122c7b696aSMarcel Ziswiler 				snd_soc_component_get_dapm(component);
613d9fc4063SKuninori Morimoto 
6142c7b696aSMarcel Ziswiler 		/*
6152c7b696aSMarcel Ziswiler 		 * If there are paths active then the COMPONENT will be held
6162c7b696aSMarcel Ziswiler 		 * with bias _ON and should not be suspended.
6172c7b696aSMarcel Ziswiler 		 */
618e40fadbcSKuninori Morimoto 		if (!snd_soc_component_is_suspended(component)) {
6194890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
6201547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
621125a25daSMark Brown 				/*
6229178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
623125a25daSMark Brown 				 * bias off then being in STANDBY
624125a25daSMark Brown 				 * means it's doing something,
625125a25daSMark Brown 				 * otherwise fall through.
626125a25daSMark Brown 				 */
6274890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
6289178feb4SKuninori Morimoto 					dev_dbg(component->dev,
62910e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
630125a25daSMark Brown 					break;
631125a25daSMark Brown 				}
6321a12d5dcSGustavo A. R. Silva 				/* fall through */
633a8093297SLars-Peter Clausen 
6341547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
63566c51573SKuninori Morimoto 				snd_soc_component_suspend(component);
6369178feb4SKuninori Morimoto 				if (component->regmap)
6379178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
638988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
6399178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
6401547aba9SMark Brown 				break;
6411547aba9SMark Brown 			default:
6429178feb4SKuninori Morimoto 				dev_dbg(component->dev,
6439178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
6441547aba9SMark Brown 				break;
6451547aba9SMark Brown 			}
6461547aba9SMark Brown 		}
647f0fba2adSLiam Girdwood 	}
648db2a4165SFrank Mandarino 
649bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6501a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6513efab7dcSMark Brown 
6521a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6533efab7dcSMark Brown 			continue;
6543efab7dcSMark Brown 
655e0f22622SKuninori Morimoto 		if (cpu_dai->driver->bus_control)
656e0f22622SKuninori Morimoto 			snd_soc_dai_suspend(cpu_dai);
657988e8cc4SNicolin Chen 
658988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
659988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
660db2a4165SFrank Mandarino 	}
661db2a4165SFrank Mandarino 
66287506549SMark Brown 	if (card->suspend_post)
66370b2ac12SMark Brown 		card->suspend_post(card);
664db2a4165SFrank Mandarino 
665db2a4165SFrank Mandarino 	return 0;
666db2a4165SFrank Mandarino }
6676f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
668db2a4165SFrank Mandarino 
6692c7b696aSMarcel Ziswiler /*
6702c7b696aSMarcel Ziswiler  * deferred resume work, so resume can complete before we finished
6716ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6726ed25978SAndy Green  */
6736ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
674db2a4165SFrank Mandarino {
675f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
6762c7b696aSMarcel Ziswiler 			container_of(work, struct snd_soc_card,
6772c7b696aSMarcel Ziswiler 				     deferred_resume_work);
6781a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
679d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
6801a497983SMengdong Lin 	int i;
681db2a4165SFrank Mandarino 
6822c7b696aSMarcel Ziswiler 	/*
6832c7b696aSMarcel Ziswiler 	 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6846ed25978SAndy Green 	 * so userspace apps are blocked from touching us
6856ed25978SAndy Green 	 */
6866ed25978SAndy Green 
687f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
6886ed25978SAndy Green 
6899949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
690f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
6919949788bSMark Brown 
69287506549SMark Brown 	if (card->resume_pre)
69370b2ac12SMark Brown 		card->resume_pre(card);
694db2a4165SFrank Mandarino 
695bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
696bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6971a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6983efab7dcSMark Brown 
6991a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7003efab7dcSMark Brown 			continue;
7013efab7dcSMark Brown 
70224b09d05SKuninori Morimoto 		if (cpu_dai->driver->bus_control)
70324b09d05SKuninori Morimoto 			snd_soc_dai_resume(cpu_dai);
704db2a4165SFrank Mandarino 	}
705db2a4165SFrank Mandarino 
706f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
707e40fadbcSKuninori Morimoto 		if (snd_soc_component_is_suspended(component))
7089a840cbaSKuninori Morimoto 			snd_soc_component_resume(component);
7091547aba9SMark Brown 	}
710db2a4165SFrank Mandarino 
711bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7123efab7dcSMark Brown 
7131a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7143efab7dcSMark Brown 			continue;
7153efab7dcSMark Brown 
7161a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
717d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
718db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
719f0fba2adSLiam Girdwood 
7201a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
721d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
722db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
723db2a4165SFrank Mandarino 	}
724db2a4165SFrank Mandarino 
7253ff3f64bSMark Brown 	/* unmute any active DACs */
726bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7270b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
7283efab7dcSMark Brown 
7291a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7303efab7dcSMark Brown 			continue;
7313efab7dcSMark Brown 
7320b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
73388fdffa2SKuninori Morimoto 			if (dai->playback_active)
73488fdffa2SKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 0,
73588fdffa2SKuninori Morimoto 						SNDRV_PCM_STREAM_PLAYBACK);
736db2a4165SFrank Mandarino 		}
73788bd870fSBenoit Cousson 	}
738db2a4165SFrank Mandarino 
739bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7401a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7413efab7dcSMark Brown 
7421a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
7433efab7dcSMark Brown 			continue;
7443efab7dcSMark Brown 
74524b09d05SKuninori Morimoto 		if (!cpu_dai->driver->bus_control)
74624b09d05SKuninori Morimoto 			snd_soc_dai_resume(cpu_dai);
747db2a4165SFrank Mandarino 	}
748db2a4165SFrank Mandarino 
74987506549SMark Brown 	if (card->resume_post)
75070b2ac12SMark Brown 		card->resume_post(card);
751db2a4165SFrank Mandarino 
752f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
7536ed25978SAndy Green 
7548be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
7558be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
756e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
7571a7aaa58SJeeja KP 
7581a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
7591a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
7606ed25978SAndy Green }
7616ed25978SAndy Green 
7626ed25978SAndy Green /* powers up audio subsystem after a suspend */
7636f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
7646ed25978SAndy Green {
7656f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
766bc263214SLars-Peter Clausen 	bool bus_control = false;
7671a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
76822d251a5SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
76922d251a5SKuninori Morimoto 	int i;
770b9dd94a8SPeter Ujfalusi 
771c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
772c5599b87SLars-Peter Clausen 	if (!card->instantiated)
7735ff1ddf2SEric Miao 		return 0;
7745ff1ddf2SEric Miao 
775988e8cc4SNicolin Chen 	/* activate pins from sleep state */
776bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
77788bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
77888bd870fSBenoit Cousson 
779988e8cc4SNicolin Chen 		if (cpu_dai->active)
780988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
78188bd870fSBenoit Cousson 
78222d251a5SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
783988e8cc4SNicolin Chen 			if (codec_dai->active)
784988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
785988e8cc4SNicolin Chen 		}
78688bd870fSBenoit Cousson 	}
787988e8cc4SNicolin Chen 
788bc263214SLars-Peter Clausen 	/*
789bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
790bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
791bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
79264ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
79364ab9baaSMark Brown 	 */
794bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7951a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7962c7b696aSMarcel Ziswiler 
797bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
79882e14e8bSStephen Warren 	}
799bc263214SLars-Peter Clausen 	if (bus_control) {
800bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
80164ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
80264ab9baaSMark Brown 	} else {
803f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
8046308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
805f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
806f0fba2adSLiam Girdwood 	}
8076ed25978SAndy Green 
808db2a4165SFrank Mandarino 	return 0;
809db2a4165SFrank Mandarino }
8106f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
811b3da4251SKuninori Morimoto 
812b3da4251SKuninori Morimoto static void soc_resume_init(struct snd_soc_card *card)
813b3da4251SKuninori Morimoto {
814b3da4251SKuninori Morimoto 	/* deferred resume work */
815b3da4251SKuninori Morimoto 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
816b3da4251SKuninori Morimoto }
817db2a4165SFrank Mandarino #else
8186f8ab4acSMark Brown #define snd_soc_suspend NULL
8196f8ab4acSMark Brown #define snd_soc_resume NULL
820b3da4251SKuninori Morimoto static inline void soc_resume_init(struct snd_soc_card *card)
821b3da4251SKuninori Morimoto {
822b3da4251SKuninori Morimoto }
823db2a4165SFrank Mandarino #endif
824db2a4165SFrank Mandarino 
82585e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
82602a06d30SBarry Song };
82702a06d30SBarry Song 
828c0834440SKuninori Morimoto static struct device_node
829c0834440SKuninori Morimoto *soc_component_to_node(struct snd_soc_component *component)
83012023a9aSMisael Lopez Cruz {
831c0834440SKuninori Morimoto 	struct device_node *of_node;
83212023a9aSMisael Lopez Cruz 
833c0834440SKuninori Morimoto 	of_node = component->dev->of_node;
834c0834440SKuninori Morimoto 	if (!of_node && component->dev->parent)
835c0834440SKuninori Morimoto 		of_node = component->dev->parent->of_node;
83634e81ab4SLars-Peter Clausen 
837c0834440SKuninori Morimoto 	return of_node;
83812023a9aSMisael Lopez Cruz }
83912023a9aSMisael Lopez Cruz 
840be6ac0a9SKuninori Morimoto static int snd_soc_is_matching_component(
841be6ac0a9SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc,
842be6ac0a9SKuninori Morimoto 	struct snd_soc_component *component)
843be6ac0a9SKuninori Morimoto {
844be6ac0a9SKuninori Morimoto 	struct device_node *component_of_node;
845be6ac0a9SKuninori Morimoto 
8467d7db5d3SKuninori Morimoto 	if (!dlc)
8477d7db5d3SKuninori Morimoto 		return 0;
8487d7db5d3SKuninori Morimoto 
8497d7db5d3SKuninori Morimoto 	component_of_node = soc_component_to_node(component);
850be6ac0a9SKuninori Morimoto 
851be6ac0a9SKuninori Morimoto 	if (dlc->of_node && component_of_node != dlc->of_node)
852be6ac0a9SKuninori Morimoto 		return 0;
853be6ac0a9SKuninori Morimoto 	if (dlc->name && strcmp(component->name, dlc->name))
854be6ac0a9SKuninori Morimoto 		return 0;
855be6ac0a9SKuninori Morimoto 
856be6ac0a9SKuninori Morimoto 	return 1;
857be6ac0a9SKuninori Morimoto }
858be6ac0a9SKuninori Morimoto 
859db2a4165SFrank Mandarino static struct snd_soc_component *soc_find_component(
860c1e230f0SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc)
861db2a4165SFrank Mandarino {
862db2a4165SFrank Mandarino 	struct snd_soc_component *component;
863db2a4165SFrank Mandarino 
864db2a4165SFrank Mandarino 	lockdep_assert_held(&client_mutex);
865db2a4165SFrank Mandarino 
866e3303268SKuninori Morimoto 	/*
867e3303268SKuninori Morimoto 	 * NOTE
868e3303268SKuninori Morimoto 	 *
869e3303268SKuninori Morimoto 	 * It returns *1st* found component, but some driver
870e3303268SKuninori Morimoto 	 * has few components by same of_node/name
871e3303268SKuninori Morimoto 	 * ex)
872e3303268SKuninori Morimoto 	 *	CPU component and generic DMAEngine component
873e3303268SKuninori Morimoto 	 */
874c1e230f0SKuninori Morimoto 	for_each_component(component)
875c1e230f0SKuninori Morimoto 		if (snd_soc_is_matching_component(dlc, component))
876db2a4165SFrank Mandarino 			return component;
877db2a4165SFrank Mandarino 
878db2a4165SFrank Mandarino 	return NULL;
87912023a9aSMisael Lopez Cruz }
88012023a9aSMisael Lopez Cruz 
881fbb88b5cSMengdong Lin /**
882fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
883fbb88b5cSMengdong Lin  *
8844958471bSJeffy Chen  * @dlc: name of the DAI or the DAI driver and optional component info to match
885fbb88b5cSMengdong Lin  *
886ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
887fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
888fbb88b5cSMengdong Lin  * should also match if being specified.
889fbb88b5cSMengdong Lin  *
890fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
891fbb88b5cSMengdong Lin  */
892305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
89314621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
89412023a9aSMisael Lopez Cruz {
89514621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
89614621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
89712023a9aSMisael Lopez Cruz 
89834e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
89934e81ab4SLars-Peter Clausen 
90014621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs */
901368dee94SKuninori Morimoto 	for_each_component(component) {
902be6ac0a9SKuninori Morimoto 		if (!snd_soc_is_matching_component(dlc, component))
90312023a9aSMisael Lopez Cruz 			continue;
90415a0c645SKuninori Morimoto 		for_each_component_dais(component, dai) {
9054958471bSJeffy Chen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
9066a6dafdaSJeffy Chen 			    && (!dai->driver->name
9076a6dafdaSJeffy Chen 				|| strcmp(dai->driver->name, dlc->dai_name)))
90814621c7eSLars-Peter Clausen 				continue;
90912023a9aSMisael Lopez Cruz 
91014621c7eSLars-Peter Clausen 			return dai;
91112023a9aSMisael Lopez Cruz 		}
91212023a9aSMisael Lopez Cruz 	}
91312023a9aSMisael Lopez Cruz 
91412023a9aSMisael Lopez Cruz 	return NULL;
91512023a9aSMisael Lopez Cruz }
916305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
91712023a9aSMisael Lopez Cruz 
91817fb1755SMengdong Lin /**
91917fb1755SMengdong Lin  * snd_soc_find_dai_link - Find a DAI link
92017fb1755SMengdong Lin  *
92117fb1755SMengdong Lin  * @card: soc card
92217fb1755SMengdong Lin  * @id: DAI link ID to match
92317fb1755SMengdong Lin  * @name: DAI link name to match, optional
9248abab35fSCharles Keepax  * @stream_name: DAI link stream name to match, optional
92517fb1755SMengdong Lin  *
92617fb1755SMengdong Lin  * This function will search all existing DAI links of the soc card to
92717fb1755SMengdong Lin  * find the link of the same ID. Since DAI links may not have their
92817fb1755SMengdong Lin  * unique ID, so name and stream name should also match if being
92917fb1755SMengdong Lin  * specified.
93017fb1755SMengdong Lin  *
93117fb1755SMengdong Lin  * Return: pointer of DAI link, or NULL if not found.
93217fb1755SMengdong Lin  */
93317fb1755SMengdong Lin struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
93417fb1755SMengdong Lin 					       int id, const char *name,
93517fb1755SMengdong Lin 					       const char *stream_name)
93617fb1755SMengdong Lin {
937*cc733900SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
93842849064SKuninori Morimoto 	struct snd_soc_dai_link *link;
93917fb1755SMengdong Lin 
94017fb1755SMengdong Lin 	lockdep_assert_held(&client_mutex);
94117fb1755SMengdong Lin 
942*cc733900SKuninori Morimoto 	for_each_card_rtds(card, rtd) {
943*cc733900SKuninori Morimoto 		link = rtd->dai_link;
944*cc733900SKuninori Morimoto 
94517fb1755SMengdong Lin 		if (link->id != id)
94617fb1755SMengdong Lin 			continue;
94717fb1755SMengdong Lin 
94817fb1755SMengdong Lin 		if (name && (!link->name || strcmp(name, link->name)))
94917fb1755SMengdong Lin 			continue;
95017fb1755SMengdong Lin 
95117fb1755SMengdong Lin 		if (stream_name && (!link->stream_name
95217fb1755SMengdong Lin 			|| strcmp(stream_name, link->stream_name)))
95317fb1755SMengdong Lin 			continue;
95417fb1755SMengdong Lin 
95517fb1755SMengdong Lin 		return link;
95617fb1755SMengdong Lin 	}
95717fb1755SMengdong Lin 
95817fb1755SMengdong Lin 	return NULL;
95917fb1755SMengdong Lin }
96017fb1755SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
96117fb1755SMengdong Lin 
962bfce78a5SKuninori Morimoto static int soc_dai_link_sanity_check(struct snd_soc_card *card,
96336794902SKuninori Morimoto 				     struct snd_soc_dai_link *link)
96436794902SKuninori Morimoto {
96536794902SKuninori Morimoto 	int i;
96636794902SKuninori Morimoto 	struct snd_soc_dai_link_component *codec, *platform;
96736794902SKuninori Morimoto 
96836794902SKuninori Morimoto 	for_each_link_codecs(link, i, codec) {
96936794902SKuninori Morimoto 		/*
97036794902SKuninori Morimoto 		 * Codec must be specified by 1 of name or OF node,
97136794902SKuninori Morimoto 		 * not both or neither.
97236794902SKuninori Morimoto 		 */
97336794902SKuninori Morimoto 		if (!!codec->name == !!codec->of_node) {
97436794902SKuninori Morimoto 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
97536794902SKuninori Morimoto 				link->name);
97636794902SKuninori Morimoto 			return -EINVAL;
97736794902SKuninori Morimoto 		}
97836794902SKuninori Morimoto 
97936794902SKuninori Morimoto 		/* Codec DAI name must be specified */
98036794902SKuninori Morimoto 		if (!codec->dai_name) {
98136794902SKuninori Morimoto 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
98236794902SKuninori Morimoto 				link->name);
98336794902SKuninori Morimoto 			return -EINVAL;
98436794902SKuninori Morimoto 		}
98536794902SKuninori Morimoto 
98636794902SKuninori Morimoto 		/*
98736794902SKuninori Morimoto 		 * Defer card registration if codec component is not added to
98836794902SKuninori Morimoto 		 * component list.
98936794902SKuninori Morimoto 		 */
99036794902SKuninori Morimoto 		if (!soc_find_component(codec))
99136794902SKuninori Morimoto 			return -EPROBE_DEFER;
99236794902SKuninori Morimoto 	}
99336794902SKuninori Morimoto 
99436794902SKuninori Morimoto 	for_each_link_platforms(link, i, platform) {
99536794902SKuninori Morimoto 		/*
99636794902SKuninori Morimoto 		 * Platform may be specified by either name or OF node, but it
99736794902SKuninori Morimoto 		 * can be left unspecified, then no components will be inserted
99836794902SKuninori Morimoto 		 * in the rtdcom list
99936794902SKuninori Morimoto 		 */
100036794902SKuninori Morimoto 		if (!!platform->name == !!platform->of_node) {
100136794902SKuninori Morimoto 			dev_err(card->dev,
100236794902SKuninori Morimoto 				"ASoC: Neither/both platform name/of_node are set for %s\n",
100336794902SKuninori Morimoto 				link->name);
100436794902SKuninori Morimoto 			return -EINVAL;
100536794902SKuninori Morimoto 		}
100636794902SKuninori Morimoto 
100736794902SKuninori Morimoto 		/*
100836794902SKuninori Morimoto 		 * Defer card registration if platform component is not added to
100936794902SKuninori Morimoto 		 * component list.
101036794902SKuninori Morimoto 		 */
101136794902SKuninori Morimoto 		if (!soc_find_component(platform))
101236794902SKuninori Morimoto 			return -EPROBE_DEFER;
101336794902SKuninori Morimoto 	}
101436794902SKuninori Morimoto 
101536794902SKuninori Morimoto 	/* FIXME */
101636794902SKuninori Morimoto 	if (link->num_cpus > 1) {
101736794902SKuninori Morimoto 		dev_err(card->dev,
101836794902SKuninori Morimoto 			"ASoC: multi cpu is not yet supported %s\n",
101936794902SKuninori Morimoto 			link->name);
102036794902SKuninori Morimoto 		return -EINVAL;
102136794902SKuninori Morimoto 	}
102236794902SKuninori Morimoto 
102336794902SKuninori Morimoto 	/*
102436794902SKuninori Morimoto 	 * CPU device may be specified by either name or OF node, but
102536794902SKuninori Morimoto 	 * can be left unspecified, and will be matched based on DAI
102636794902SKuninori Morimoto 	 * name alone..
102736794902SKuninori Morimoto 	 */
102836794902SKuninori Morimoto 	if (link->cpus->name && link->cpus->of_node) {
102936794902SKuninori Morimoto 		dev_err(card->dev,
103036794902SKuninori Morimoto 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
103136794902SKuninori Morimoto 			link->name);
103236794902SKuninori Morimoto 		return -EINVAL;
103336794902SKuninori Morimoto 	}
103436794902SKuninori Morimoto 
103536794902SKuninori Morimoto 	/*
1036cd3c5ad7SKuninori Morimoto 	 * Defer card registration if cpu dai component is not added to
103736794902SKuninori Morimoto 	 * component list.
103836794902SKuninori Morimoto 	 */
103936794902SKuninori Morimoto 	if ((link->cpus->of_node || link->cpus->name) &&
104036794902SKuninori Morimoto 	    !soc_find_component(link->cpus))
104136794902SKuninori Morimoto 		return -EPROBE_DEFER;
104236794902SKuninori Morimoto 
104336794902SKuninori Morimoto 	/*
104436794902SKuninori Morimoto 	 * At least one of CPU DAI name or CPU device name/node must be
104536794902SKuninori Morimoto 	 * specified
104636794902SKuninori Morimoto 	 */
104736794902SKuninori Morimoto 	if (!link->cpus->dai_name &&
104836794902SKuninori Morimoto 	    !(link->cpus->name || link->cpus->of_node)) {
104936794902SKuninori Morimoto 		dev_err(card->dev,
105036794902SKuninori Morimoto 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
105136794902SKuninori Morimoto 			link->name);
105236794902SKuninori Morimoto 		return -EINVAL;
105336794902SKuninori Morimoto 	}
105436794902SKuninori Morimoto 
105536794902SKuninori Morimoto 	return 0;
105636794902SKuninori Morimoto }
105736794902SKuninori Morimoto 
1058da704f26SKuninori Morimoto /**
1059da704f26SKuninori Morimoto  * snd_soc_remove_dai_link - Remove a DAI link from the list
1060da704f26SKuninori Morimoto  * @card: The ASoC card that owns the link
1061da704f26SKuninori Morimoto  * @dai_link: The DAI link to remove
1062da704f26SKuninori Morimoto  *
1063da704f26SKuninori Morimoto  * This function removes a DAI link from the ASoC card's link list.
1064da704f26SKuninori Morimoto  *
1065da704f26SKuninori Morimoto  * For DAI links previously added by topology, topology should
1066da704f26SKuninori Morimoto  * remove them by using the dobj embedded in the link.
1067da704f26SKuninori Morimoto  */
1068da704f26SKuninori Morimoto void snd_soc_remove_dai_link(struct snd_soc_card *card,
1069bc7a9091SKuninori Morimoto 			     struct snd_soc_dai_link *dai_link)
1070bc7a9091SKuninori Morimoto {
1071bc7a9091SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
1072bc7a9091SKuninori Morimoto 
1073da704f26SKuninori Morimoto 	lockdep_assert_held(&client_mutex);
1074da704f26SKuninori Morimoto 
1075da704f26SKuninori Morimoto 	/*
1076da704f26SKuninori Morimoto 	 * Notify the machine driver for extra destruction
1077da704f26SKuninori Morimoto 	 */
1078da704f26SKuninori Morimoto 	if (card->remove_dai_link)
1079da704f26SKuninori Morimoto 		card->remove_dai_link(card, dai_link);
1080da704f26SKuninori Morimoto 
1081bc7a9091SKuninori Morimoto 	rtd = snd_soc_get_pcm_runtime(card, dai_link->name);
1082bc7a9091SKuninori Morimoto 	if (rtd)
1083bc7a9091SKuninori Morimoto 		soc_free_pcm_runtime(rtd);
1084bc7a9091SKuninori Morimoto }
1085da704f26SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1086bc7a9091SKuninori Morimoto 
108763dc47daSKuninori Morimoto /**
108863dc47daSKuninori Morimoto  * snd_soc_add_dai_link - Add a DAI link dynamically
108963dc47daSKuninori Morimoto  * @card: The ASoC card to which the DAI link is added
109063dc47daSKuninori Morimoto  * @dai_link: The new DAI link to add
109163dc47daSKuninori Morimoto  *
109263dc47daSKuninori Morimoto  * This function adds a DAI link to the ASoC card's link list.
109363dc47daSKuninori Morimoto  *
109463dc47daSKuninori Morimoto  * Note: Topology can use this API to add DAI links when probing the
109563dc47daSKuninori Morimoto  * topology component. And machine drivers can still define static
109663dc47daSKuninori Morimoto  * DAI links in dai_link array.
109763dc47daSKuninori Morimoto  */
109863dc47daSKuninori Morimoto int snd_soc_add_dai_link(struct snd_soc_card *card,
10996f2f1ff0SMengdong Lin 			 struct snd_soc_dai_link *dai_link)
1100db2a4165SFrank Mandarino {
11011a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
110234614739SJerome Brunet 	struct snd_soc_dai_link_component *codec, *platform;
110390be711eSKuninori Morimoto 	struct snd_soc_component *component;
1104bfce78a5SKuninori Morimoto 	int i, ret;
1105db2a4165SFrank Mandarino 
110663dc47daSKuninori Morimoto 	lockdep_assert_held(&client_mutex);
110763dc47daSKuninori Morimoto 
110863dc47daSKuninori Morimoto 	/*
110963dc47daSKuninori Morimoto 	 * Notify the machine driver for extra initialization
111063dc47daSKuninori Morimoto 	 */
111163dc47daSKuninori Morimoto 	if (card->add_dai_link)
111263dc47daSKuninori Morimoto 		card->add_dai_link(card, dai_link);
111363dc47daSKuninori Morimoto 
1114a655de80SLiam Girdwood 	if (dai_link->ignore)
1115a655de80SLiam Girdwood 		return 0;
1116a655de80SLiam Girdwood 
11176f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
11186308419aSMark Brown 
1119bfce78a5SKuninori Morimoto 	ret = soc_dai_link_sanity_check(card, dai_link);
1120bfce78a5SKuninori Morimoto 	if (ret < 0)
1121bfce78a5SKuninori Morimoto 		return ret;
1122bfce78a5SKuninori Morimoto 
1123513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
1124513cb311SSudip Mukherjee 	if (!rtd)
1125513cb311SSudip Mukherjee 		return -ENOMEM;
1126513cb311SSudip Mukherjee 
112708a5841eSKuninori Morimoto 	/* FIXME: we need multi CPU support in the future */
112808a5841eSKuninori Morimoto 	rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
1129b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
11306b490879SMartin Hundebøll 		dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
113108a5841eSKuninori Morimoto 			 dai_link->cpus->dai_name);
11321a497983SMengdong Lin 		goto _err_defer;
1133c5af3a2eSMark Brown 	}
113490be711eSKuninori Morimoto 	snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
1135c5af3a2eSMark Brown 
113688bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
1137e2b30edfSKuninori Morimoto 	rtd->num_codecs = dai_link->num_codecs;
113834614739SJerome Brunet 	for_each_link_codecs(dai_link, i, codec) {
113934614739SJerome Brunet 		rtd->codec_dais[i] = snd_soc_find_dai(codec);
11400a2cfcd9SKuninori Morimoto 		if (!rtd->codec_dais[i]) {
11417c7e2d6aSStefan Agner 			dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
114234614739SJerome Brunet 				 codec->dai_name);
11431a497983SMengdong Lin 			goto _err_defer;
114412023a9aSMisael Lopez Cruz 		}
114534614739SJerome Brunet 
11460a2cfcd9SKuninori Morimoto 		snd_soc_rtdcom_add(rtd, rtd->codec_dais[i]->component);
114788bd870fSBenoit Cousson 	}
114888bd870fSBenoit Cousson 
114988bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
11500a2cfcd9SKuninori Morimoto 	rtd->codec_dai = rtd->codec_dais[0];
115112023a9aSMisael Lopez Cruz 
1152e2b30edfSKuninori Morimoto 	/* Find PLATFORM from registered PLATFORMs */
115334614739SJerome Brunet 	for_each_link_platforms(dai_link, i, platform) {
1154368dee94SKuninori Morimoto 		for_each_component(component) {
115534614739SJerome Brunet 			if (!snd_soc_is_matching_component(platform, component))
115690be711eSKuninori Morimoto 				continue;
115790be711eSKuninori Morimoto 
115890be711eSKuninori Morimoto 			snd_soc_rtdcom_add(rtd, component);
115990be711eSKuninori Morimoto 		}
116034614739SJerome Brunet 	}
116190be711eSKuninori Morimoto 
1162b19e6e7bSMark Brown 	return 0;
11631a497983SMengdong Lin 
11641a497983SMengdong Lin _err_defer:
11651a497983SMengdong Lin 	soc_free_pcm_runtime(rtd);
11661a497983SMengdong Lin 	return -EPROBE_DEFER;
11676b05eda6SMark Brown }
116863dc47daSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
11696b05eda6SMark Brown 
1170ffd60fbaSKuninori Morimoto static void soc_set_of_name_prefix(struct snd_soc_component *component)
1171ffd60fbaSKuninori Morimoto {
1172ffd60fbaSKuninori Morimoto 	struct device_node *of_node = soc_component_to_node(component);
1173ffd60fbaSKuninori Morimoto 	const char *str;
1174ffd60fbaSKuninori Morimoto 	int ret;
1175ffd60fbaSKuninori Morimoto 
1176ffd60fbaSKuninori Morimoto 	ret = of_property_read_string(of_node, "sound-name-prefix", &str);
1177ffd60fbaSKuninori Morimoto 	if (!ret)
1178ffd60fbaSKuninori Morimoto 		component->name_prefix = str;
1179ffd60fbaSKuninori Morimoto }
1180ffd60fbaSKuninori Morimoto 
1181ffd60fbaSKuninori Morimoto static void soc_set_name_prefix(struct snd_soc_card *card,
1182ffd60fbaSKuninori Morimoto 				struct snd_soc_component *component)
1183ffd60fbaSKuninori Morimoto {
1184ffd60fbaSKuninori Morimoto 	int i;
1185ffd60fbaSKuninori Morimoto 
1186ffd60fbaSKuninori Morimoto 	for (i = 0; i < card->num_configs && card->codec_conf; i++) {
1187ffd60fbaSKuninori Morimoto 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1188ffd60fbaSKuninori Morimoto 		struct device_node *of_node = soc_component_to_node(component);
1189ffd60fbaSKuninori Morimoto 
1190ffd60fbaSKuninori Morimoto 		if (map->of_node && of_node != map->of_node)
1191ffd60fbaSKuninori Morimoto 			continue;
1192ffd60fbaSKuninori Morimoto 		if (map->dev_name && strcmp(component->name, map->dev_name))
1193ffd60fbaSKuninori Morimoto 			continue;
1194ffd60fbaSKuninori Morimoto 		component->name_prefix = map->name_prefix;
1195ffd60fbaSKuninori Morimoto 		return;
1196ffd60fbaSKuninori Morimoto 	}
1197ffd60fbaSKuninori Morimoto 
1198ffd60fbaSKuninori Morimoto 	/*
1199ffd60fbaSKuninori Morimoto 	 * If there is no configuration table or no match in the table,
1200ffd60fbaSKuninori Morimoto 	 * check if a prefix is provided in the node
1201ffd60fbaSKuninori Morimoto 	 */
1202ffd60fbaSKuninori Morimoto 	soc_set_of_name_prefix(component);
1203ffd60fbaSKuninori Morimoto }
1204ffd60fbaSKuninori Morimoto 
1205c6619b72SKuninori Morimoto static void soc_remove_component(struct snd_soc_component *component,
1206c6619b72SKuninori Morimoto 				 int probed)
120722d14231SKuninori Morimoto {
1208c6619b72SKuninori Morimoto 
1209c6619b72SKuninori Morimoto 	if (!component->card)
1210c6619b72SKuninori Morimoto 		return;
1211c6619b72SKuninori Morimoto 
1212c6619b72SKuninori Morimoto 	if (probed)
1213c6619b72SKuninori Morimoto 		snd_soc_component_remove(component);
1214c6619b72SKuninori Morimoto 
121504f770d9SKuninori Morimoto 	/* For framework level robustness */
12163bb936f5SAmadeusz Sławiński 	snd_soc_component_set_jack(component, NULL, NULL);
121704f770d9SKuninori Morimoto 
12187a5d9815SBard liao 	list_del_init(&component->card_list);
121922d14231SKuninori Morimoto 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
122022d14231SKuninori Morimoto 	soc_cleanup_component_debugfs(component);
122122d14231SKuninori Morimoto 	component->card = NULL;
12220e36f36bSPierre-Louis Bossart 	snd_soc_component_module_put_when_remove(component);
122322d14231SKuninori Morimoto }
122422d14231SKuninori Morimoto 
1225ffd60fbaSKuninori Morimoto static int soc_probe_component(struct snd_soc_card *card,
1226ffd60fbaSKuninori Morimoto 			       struct snd_soc_component *component)
1227ffd60fbaSKuninori Morimoto {
1228ffd60fbaSKuninori Morimoto 	struct snd_soc_dapm_context *dapm =
1229ffd60fbaSKuninori Morimoto 		snd_soc_component_get_dapm(component);
1230ffd60fbaSKuninori Morimoto 	struct snd_soc_dai *dai;
1231c6619b72SKuninori Morimoto 	int probed = 0;
1232ffd60fbaSKuninori Morimoto 	int ret;
1233ffd60fbaSKuninori Morimoto 
1234ffd60fbaSKuninori Morimoto 	if (!strcmp(component->name, "snd-soc-dummy"))
1235ffd60fbaSKuninori Morimoto 		return 0;
1236ffd60fbaSKuninori Morimoto 
1237ffd60fbaSKuninori Morimoto 	if (component->card) {
1238ffd60fbaSKuninori Morimoto 		if (component->card != card) {
1239ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1240ffd60fbaSKuninori Morimoto 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1241ffd60fbaSKuninori Morimoto 				card->name, component->card->name);
1242ffd60fbaSKuninori Morimoto 			return -ENODEV;
1243ffd60fbaSKuninori Morimoto 		}
1244ffd60fbaSKuninori Morimoto 		return 0;
1245ffd60fbaSKuninori Morimoto 	}
1246ffd60fbaSKuninori Morimoto 
1247ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_module_get_when_probe(component);
1248ffd60fbaSKuninori Morimoto 	if (ret < 0)
1249ffd60fbaSKuninori Morimoto 		return ret;
1250ffd60fbaSKuninori Morimoto 
1251ffd60fbaSKuninori Morimoto 	component->card = card;
1252ffd60fbaSKuninori Morimoto 	soc_set_name_prefix(card, component);
1253ffd60fbaSKuninori Morimoto 
1254ffd60fbaSKuninori Morimoto 	soc_init_component_debugfs(component);
1255ffd60fbaSKuninori Morimoto 
125695c267ddSKuninori Morimoto 	snd_soc_dapm_init(dapm, card, component);
1257b614beafSKuninori Morimoto 
1258ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_new_controls(dapm,
1259ffd60fbaSKuninori Morimoto 					component->driver->dapm_widgets,
1260ffd60fbaSKuninori Morimoto 					component->driver->num_dapm_widgets);
1261ffd60fbaSKuninori Morimoto 
1262ffd60fbaSKuninori Morimoto 	if (ret != 0) {
1263ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1264ffd60fbaSKuninori Morimoto 			"Failed to create new controls %d\n", ret);
1265ffd60fbaSKuninori Morimoto 		goto err_probe;
1266ffd60fbaSKuninori Morimoto 	}
1267ffd60fbaSKuninori Morimoto 
1268ffd60fbaSKuninori Morimoto 	for_each_component_dais(component, dai) {
1269ffd60fbaSKuninori Morimoto 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1270ffd60fbaSKuninori Morimoto 		if (ret != 0) {
1271ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1272ffd60fbaSKuninori Morimoto 				"Failed to create DAI widgets %d\n", ret);
1273ffd60fbaSKuninori Morimoto 			goto err_probe;
1274ffd60fbaSKuninori Morimoto 		}
1275ffd60fbaSKuninori Morimoto 	}
1276ffd60fbaSKuninori Morimoto 
1277ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_probe(component);
1278ffd60fbaSKuninori Morimoto 	if (ret < 0) {
1279ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1280ffd60fbaSKuninori Morimoto 			"ASoC: failed to probe component %d\n", ret);
1281ffd60fbaSKuninori Morimoto 		goto err_probe;
1282ffd60fbaSKuninori Morimoto 	}
1283ffd60fbaSKuninori Morimoto 	WARN(dapm->idle_bias_off &&
1284ffd60fbaSKuninori Morimoto 	     dapm->bias_level != SND_SOC_BIAS_OFF,
1285ffd60fbaSKuninori Morimoto 	     "codec %s can not start from non-off bias with idle_bias_off==1\n",
1286ffd60fbaSKuninori Morimoto 	     component->name);
1287c6619b72SKuninori Morimoto 	probed = 1;
1288ffd60fbaSKuninori Morimoto 
1289ffd60fbaSKuninori Morimoto 	/* machine specific init */
1290ffd60fbaSKuninori Morimoto 	if (component->init) {
1291ffd60fbaSKuninori Morimoto 		ret = component->init(component);
1292ffd60fbaSKuninori Morimoto 		if (ret < 0) {
1293ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1294ffd60fbaSKuninori Morimoto 				"Failed to do machine specific init %d\n", ret);
1295ffd60fbaSKuninori Morimoto 			goto err_probe;
1296ffd60fbaSKuninori Morimoto 		}
1297ffd60fbaSKuninori Morimoto 	}
1298ffd60fbaSKuninori Morimoto 
1299ffd60fbaSKuninori Morimoto 	ret = snd_soc_add_component_controls(component,
1300ffd60fbaSKuninori Morimoto 					     component->driver->controls,
1301ffd60fbaSKuninori Morimoto 					     component->driver->num_controls);
1302ffd60fbaSKuninori Morimoto 	if (ret < 0)
1303ffd60fbaSKuninori Morimoto 		goto err_probe;
1304ffd60fbaSKuninori Morimoto 
1305ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(dapm,
1306ffd60fbaSKuninori Morimoto 				      component->driver->dapm_routes,
1307ffd60fbaSKuninori Morimoto 				      component->driver->num_dapm_routes);
1308ffd60fbaSKuninori Morimoto 	if (ret < 0)
1309ffd60fbaSKuninori Morimoto 		goto err_probe;
1310ffd60fbaSKuninori Morimoto 
1311ffd60fbaSKuninori Morimoto 	/* see for_each_card_components */
1312ffd60fbaSKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1313ffd60fbaSKuninori Morimoto 
1314ffd60fbaSKuninori Morimoto err_probe:
1315ffd60fbaSKuninori Morimoto 	if (ret < 0)
1316c6619b72SKuninori Morimoto 		soc_remove_component(component, probed);
1317ffd60fbaSKuninori Morimoto 
1318ffd60fbaSKuninori Morimoto 	return ret;
1319ffd60fbaSKuninori Morimoto }
1320ffd60fbaSKuninori Morimoto 
1321e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1322589c3563SJarkko Nikula {
1323589c3563SJarkko Nikula 	int err;
1324589c3563SJarkko Nikula 
132552abe6ccSGuennadi Liakhovetski 	if (!dai || !dai->probed || !dai->driver ||
13262eda3cb1SKuninori Morimoto 	    dai->driver->remove_order != order)
13272eda3cb1SKuninori Morimoto 		return;
13282eda3cb1SKuninori Morimoto 
1329dcdab582SKuninori Morimoto 	err = snd_soc_dai_remove(dai);
1330589c3563SJarkko Nikula 	if (err < 0)
1331e60cd14fSLars-Peter Clausen 		dev_err(dai->dev,
1332b0aa88afSMisael Lopez Cruz 			"ASoC: failed to remove %s: %d\n",
1333e60cd14fSLars-Peter Clausen 			dai->name, err);
1334dcdab582SKuninori Morimoto 
1335e60cd14fSLars-Peter Clausen 	dai->probed = 0;
1336b0aa88afSMisael Lopez Cruz }
1337b0aa88afSMisael Lopez Cruz 
1338a7d44f78SKuninori Morimoto static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1339a7d44f78SKuninori Morimoto {
1340a7d44f78SKuninori Morimoto 	int ret;
1341a7d44f78SKuninori Morimoto 
1342a7d44f78SKuninori Morimoto 	if (dai->probed ||
1343a7d44f78SKuninori Morimoto 	    dai->driver->probe_order != order)
1344a7d44f78SKuninori Morimoto 		return 0;
1345a7d44f78SKuninori Morimoto 
1346a7d44f78SKuninori Morimoto 	ret = snd_soc_dai_probe(dai);
1347a7d44f78SKuninori Morimoto 	if (ret < 0) {
1348a7d44f78SKuninori Morimoto 		dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1349a7d44f78SKuninori Morimoto 			dai->name, ret);
1350a7d44f78SKuninori Morimoto 		return ret;
1351a7d44f78SKuninori Morimoto 	}
1352a7d44f78SKuninori Morimoto 
1353a7d44f78SKuninori Morimoto 	dai->probed = 1;
1354a7d44f78SKuninori Morimoto 
1355a7d44f78SKuninori Morimoto 	return 0;
1356a7d44f78SKuninori Morimoto }
1357a7d44f78SKuninori Morimoto 
13584ca47d21SKuninori Morimoto static void soc_remove_link_dais(struct snd_soc_card *card)
1359f0fba2adSLiam Girdwood {
1360e60cd14fSLars-Peter Clausen 	int i;
13610b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
13624ca47d21SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
13634ca47d21SKuninori Morimoto 	int order;
13644ca47d21SKuninori Morimoto 
13654ca47d21SKuninori Morimoto 	for_each_comp_order(order) {
13664ca47d21SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1367f0fba2adSLiam Girdwood 			/* remove the CODEC DAI */
13680b7990e3SKuninori Morimoto 			for_each_rtd_codec_dai(rtd, i, codec_dai)
13690b7990e3SKuninori Morimoto 				soc_remove_dai(codec_dai, order);
1370f0fba2adSLiam Girdwood 
1371e60cd14fSLars-Peter Clausen 			soc_remove_dai(rtd->cpu_dai, order);
1372f0fba2adSLiam Girdwood 		}
13734ca47d21SKuninori Morimoto 	}
13744ca47d21SKuninori Morimoto }
1375f0fba2adSLiam Girdwood 
1376bc7c16c2SKuninori Morimoto static int soc_probe_link_dais(struct snd_soc_card *card)
1377bc7c16c2SKuninori Morimoto {
1378bc7c16c2SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1379bc7c16c2SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
1380bc7c16c2SKuninori Morimoto 	int i, order, ret;
1381bc7c16c2SKuninori Morimoto 
1382bc7c16c2SKuninori Morimoto 	for_each_comp_order(order) {
1383bc7c16c2SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1384bc7c16c2SKuninori Morimoto 
1385bc7c16c2SKuninori Morimoto 			dev_dbg(card->dev,
1386bc7c16c2SKuninori Morimoto 				"ASoC: probe %s dai link %d late %d\n",
1387bc7c16c2SKuninori Morimoto 				card->name, rtd->num, order);
1388bc7c16c2SKuninori Morimoto 
1389bc7c16c2SKuninori Morimoto 			ret = soc_probe_dai(rtd->cpu_dai, order);
1390bc7c16c2SKuninori Morimoto 			if (ret)
1391bc7c16c2SKuninori Morimoto 				return ret;
1392bc7c16c2SKuninori Morimoto 
1393bc7c16c2SKuninori Morimoto 			/* probe the CODEC DAI */
1394bc7c16c2SKuninori Morimoto 			for_each_rtd_codec_dai(rtd, i, codec_dai) {
1395bc7c16c2SKuninori Morimoto 				ret = soc_probe_dai(codec_dai, order);
1396bc7c16c2SKuninori Morimoto 				if (ret)
1397bc7c16c2SKuninori Morimoto 					return ret;
1398bc7c16c2SKuninori Morimoto 			}
1399bc7c16c2SKuninori Morimoto 		}
1400bc7c16c2SKuninori Morimoto 	}
1401bc7c16c2SKuninori Morimoto 
1402bc7c16c2SKuninori Morimoto 	return 0;
1403bc7c16c2SKuninori Morimoto }
1404bc7c16c2SKuninori Morimoto 
1405b006c0c6SKuninori Morimoto static void soc_remove_link_components(struct snd_soc_card *card)
140662ae68faSStephen Warren {
140761aca564SLars-Peter Clausen 	struct snd_soc_component *component;
1408b006c0c6SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
140990be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
1410b006c0c6SKuninori Morimoto 	int order;
141162ae68faSStephen Warren 
1412b006c0c6SKuninori Morimoto 	for_each_comp_order(order) {
1413b006c0c6SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
14142b544dd7SKuninori Morimoto 			for_each_rtd_components(rtd, rtdcom, component) {
1415b006c0c6SKuninori Morimoto 				if (component->driver->remove_order != order)
1416b006c0c6SKuninori Morimoto 					continue;
1417b006c0c6SKuninori Morimoto 
1418c6619b72SKuninori Morimoto 				soc_remove_component(component, 1);
141962ae68faSStephen Warren 			}
142062ae68faSStephen Warren 		}
1421b006c0c6SKuninori Morimoto 	}
1422b006c0c6SKuninori Morimoto }
142362ae68faSStephen Warren 
142462f07a6bSKuninori Morimoto static int soc_probe_link_components(struct snd_soc_card *card)
14256fb03550SKuninori Morimoto {
14266fb03550SKuninori Morimoto 	struct snd_soc_component *component;
142762f07a6bSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
14286fb03550SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
142962f07a6bSKuninori Morimoto 	int ret, order;
14306fb03550SKuninori Morimoto 
143162f07a6bSKuninori Morimoto 	for_each_comp_order(order) {
143262f07a6bSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
14332b544dd7SKuninori Morimoto 			for_each_rtd_components(rtd, rtdcom, component) {
143462f07a6bSKuninori Morimoto 				if (component->driver->probe_order != order)
143562f07a6bSKuninori Morimoto 					continue;
143662f07a6bSKuninori Morimoto 
14376fb03550SKuninori Morimoto 				ret = soc_probe_component(card, component);
14386fb03550SKuninori Morimoto 				if (ret < 0)
14396fb03550SKuninori Morimoto 					return ret;
14406fb03550SKuninori Morimoto 			}
14416fb03550SKuninori Morimoto 		}
144262f07a6bSKuninori Morimoto 	}
14436fb03550SKuninori Morimoto 
14446fb03550SKuninori Morimoto 	return 0;
14456fb03550SKuninori Morimoto }
14466fb03550SKuninori Morimoto 
1447ef2e8175SKuninori Morimoto void snd_soc_disconnect_sync(struct device *dev)
1448ef2e8175SKuninori Morimoto {
14492c7b696aSMarcel Ziswiler 	struct snd_soc_component *component =
14502c7b696aSMarcel Ziswiler 			snd_soc_lookup_component(dev, NULL);
1451ef2e8175SKuninori Morimoto 
1452ef2e8175SKuninori Morimoto 	if (!component || !component->card)
1453ef2e8175SKuninori Morimoto 		return;
1454ef2e8175SKuninori Morimoto 
1455ef2e8175SKuninori Morimoto 	snd_card_disconnect_sync(component->card->snd_card);
1456ef2e8175SKuninori Morimoto }
1457df532185SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
1458ef2e8175SKuninori Morimoto 
145925f7b701SArnaud Pouliquen static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
146025f7b701SArnaud Pouliquen 				struct snd_soc_pcm_runtime *rtd)
146125f7b701SArnaud Pouliquen {
146225f7b701SArnaud Pouliquen 	int i, ret = 0;
146325f7b701SArnaud Pouliquen 
146425f7b701SArnaud Pouliquen 	for (i = 0; i < num_dais; ++i) {
146525f7b701SArnaud Pouliquen 		struct snd_soc_dai_driver *drv = dais[i]->driver;
146625f7b701SArnaud Pouliquen 
1467de17f14eSRohit kumar 		if (drv->pcm_new)
146825f7b701SArnaud Pouliquen 			ret = drv->pcm_new(rtd, dais[i]);
146925f7b701SArnaud Pouliquen 		if (ret < 0) {
147025f7b701SArnaud Pouliquen 			dev_err(dais[i]->dev,
147125f7b701SArnaud Pouliquen 				"ASoC: Failed to bind %s with pcm device\n",
147225f7b701SArnaud Pouliquen 				dais[i]->name);
147325f7b701SArnaud Pouliquen 			return ret;
147425f7b701SArnaud Pouliquen 		}
147525f7b701SArnaud Pouliquen 	}
147625f7b701SArnaud Pouliquen 
147725f7b701SArnaud Pouliquen 	return 0;
147825f7b701SArnaud Pouliquen }
147925f7b701SArnaud Pouliquen 
1480c4b46982SKuninori Morimoto static int soc_link_init(struct snd_soc_card *card,
1481c4b46982SKuninori Morimoto 			 struct snd_soc_pcm_runtime *rtd)
1482c4b46982SKuninori Morimoto {
1483c4b46982SKuninori Morimoto 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1484c4b46982SKuninori Morimoto 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1485c4b46982SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
1486c4b46982SKuninori Morimoto 	struct snd_soc_component *component;
1487c4b46982SKuninori Morimoto 	int ret, num;
1488c4b46982SKuninori Morimoto 
1489c4b46982SKuninori Morimoto 	/* set default power off timeout */
1490c4b46982SKuninori Morimoto 	rtd->pmdown_time = pmdown_time;
14910168bf0dSLiam Girdwood 
14925f3484acSLars-Peter Clausen 	/* do machine specific initialization */
14935f3484acSLars-Peter Clausen 	if (dai_link->init) {
14945f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
14955f3484acSLars-Peter Clausen 		if (ret < 0) {
14965f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
14975f3484acSLars-Peter Clausen 				dai_link->name, ret);
14985f3484acSLars-Peter Clausen 			return ret;
14995f3484acSLars-Peter Clausen 		}
15005f3484acSLars-Peter Clausen 	}
15015f3484acSLars-Peter Clausen 
150240aa5383SRicard Wanderlof 	if (dai_link->dai_fmt) {
150340aa5383SRicard Wanderlof 		ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
150440aa5383SRicard Wanderlof 		if (ret)
150540aa5383SRicard Wanderlof 			return ret;
150640aa5383SRicard Wanderlof 	}
1507a5053a8eSKuninori Morimoto 
15085f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
15092e55b90aSLars-Peter Clausen 	soc_dpcm_debugfs_add(rtd);
15105f3484acSLars-Peter Clausen 
1511a655de80SLiam Girdwood 	num = rtd->num;
1512a655de80SLiam Girdwood 
1513a655de80SLiam Girdwood 	/*
1514a655de80SLiam Girdwood 	 * most drivers will register their PCMs using DAI link ordering but
1515a655de80SLiam Girdwood 	 * topology based drivers can use the DAI link id field to set PCM
1516a655de80SLiam Girdwood 	 * device number and then use rtd + a base offset of the BEs.
1517a655de80SLiam Girdwood 	 */
15182b544dd7SKuninori Morimoto 	for_each_rtd_components(rtd, rtdcom, component) {
1519a655de80SLiam Girdwood 		if (!component->driver->use_dai_pcm_id)
1520a655de80SLiam Girdwood 			continue;
1521a655de80SLiam Girdwood 
1522a655de80SLiam Girdwood 		if (rtd->dai_link->no_pcm)
1523a655de80SLiam Girdwood 			num += component->driver->be_pcm_base;
1524a655de80SLiam Girdwood 		else
1525a655de80SLiam Girdwood 			num = rtd->dai_link->id;
1526a655de80SLiam Girdwood 	}
1527a655de80SLiam Girdwood 
1528b423c420SKuninori Morimoto 	/* create compress_device if possible */
1529b423c420SKuninori Morimoto 	ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1530b423c420SKuninori Morimoto 	if (ret != -ENOTSUPP) {
1531b423c420SKuninori Morimoto 		if (ret < 0)
1532f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
15331245b700SNamarta Kohli 					 dai_link->stream_name);
15341245b700SNamarta Kohli 		return ret;
15351245b700SNamarta Kohli 	}
1536b423c420SKuninori Morimoto 
1537f0fba2adSLiam Girdwood 	/* create the pcm */
1538a655de80SLiam Girdwood 	ret = soc_new_pcm(rtd, num);
1539f0fba2adSLiam Girdwood 	if (ret < 0) {
1540f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
15410837fc62SFabio Estevam 			dai_link->stream_name, ret);
1542f0fba2adSLiam Girdwood 		return ret;
1543f0fba2adSLiam Girdwood 	}
154425f7b701SArnaud Pouliquen 	ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
154525f7b701SArnaud Pouliquen 	if (ret < 0)
154625f7b701SArnaud Pouliquen 		return ret;
154725f7b701SArnaud Pouliquen 	ret = soc_link_dai_pcm_new(rtd->codec_dais,
154825f7b701SArnaud Pouliquen 				   rtd->num_codecs, rtd);
154925f7b701SArnaud Pouliquen 	return ret;
1550f0fba2adSLiam Girdwood }
1551f0fba2adSLiam Girdwood 
1552e8fbd250SKuninori Morimoto static void soc_unbind_aux_dev(struct snd_soc_card *card)
15534893a2ebSKuninori Morimoto {
1554e8fbd250SKuninori Morimoto 	struct snd_soc_component *component, *_component;
1555e8fbd250SKuninori Morimoto 
1556e8fbd250SKuninori Morimoto 	for_each_card_auxs_safe(card, component, _component) {
15574893a2ebSKuninori Morimoto 		component->init = NULL;
15584893a2ebSKuninori Morimoto 		list_del(&component->card_aux_list);
15594893a2ebSKuninori Morimoto 	}
1560e8fbd250SKuninori Morimoto }
15614893a2ebSKuninori Morimoto 
1562bee886f1SKuninori Morimoto static int soc_bind_aux_dev(struct snd_soc_card *card)
1563b19e6e7bSMark Brown {
1564f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1565bee886f1SKuninori Morimoto 	struct snd_soc_aux_dev *aux;
1566bee886f1SKuninori Morimoto 	int i;
15673ca041edSSebastian Reichel 
1568bee886f1SKuninori Morimoto 	for_each_card_pre_auxs(card, i, aux) {
1569f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1570bee886f1SKuninori Morimoto 		component = soc_find_component(&aux->dlc);
1571f2ed6b07SMengdong Lin 		if (!component)
15723dc29b8bSKuninori Morimoto 			return -EPROBE_DEFER;
15733ca041edSSebastian Reichel 
1574bee886f1SKuninori Morimoto 		component->init = aux->init;
1575c2b71c71SKuninori Morimoto 		/* see for_each_card_auxs */
1576d2e3a135SSylwester Nawrocki 		list_add(&component->card_aux_list, &card->aux_comp_list);
1577bee886f1SKuninori Morimoto 	}
1578f2ed6b07SMengdong Lin 	return 0;
1579b19e6e7bSMark Brown }
1580b19e6e7bSMark Brown 
1581f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1582f2ed6b07SMengdong Lin {
158374bd3f92SKuninori Morimoto 	struct snd_soc_component *component;
1584f2ed6b07SMengdong Lin 	int order;
1585f2ed6b07SMengdong Lin 	int ret;
1586f2ed6b07SMengdong Lin 
15871a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
158874bd3f92SKuninori Morimoto 		for_each_card_auxs(card, component) {
158974bd3f92SKuninori Morimoto 			if (component->driver->probe_order != order)
159074bd3f92SKuninori Morimoto 				continue;
159174bd3f92SKuninori Morimoto 
159274bd3f92SKuninori Morimoto 			ret = soc_probe_component(card,	component);
159374bd3f92SKuninori Morimoto 			if (ret < 0)
1594f2ed6b07SMengdong Lin 				return ret;
1595f2ed6b07SMengdong Lin 		}
1596f2ed6b07SMengdong Lin 	}
159765d9361fSLars-Peter Clausen 
159844c69bb1SLars-Peter Clausen 	return 0;
15993ca041edSSebastian Reichel }
16002eea392dSJarkko Nikula 
1601f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
160244c69bb1SLars-Peter Clausen {
1603f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1604f2ed6b07SMengdong Lin 	int order;
160544c69bb1SLars-Peter Clausen 
16061a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1607c2b71c71SKuninori Morimoto 		for_each_card_auxs_safe(card, comp, _comp) {
1608e8fbd250SKuninori Morimoto 			if (comp->driver->remove_order == order)
1609c6619b72SKuninori Morimoto 				soc_remove_component(comp, 1);
16105f3484acSLars-Peter Clausen 		}
16112eea392dSJarkko Nikula 	}
16122eea392dSJarkko Nikula }
16132eea392dSJarkko Nikula 
1614ce64c8b9SLars-Peter Clausen /**
1615ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1616ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1617ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1618ce64c8b9SLars-Peter Clausen  *
1619ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1620ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1621ce64c8b9SLars-Peter Clausen  *
1622ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1623ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1624ce64c8b9SLars-Peter Clausen  *
1625ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1626ce64c8b9SLars-Peter Clausen  */
1627ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1628ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1629ce64c8b9SLars-Peter Clausen {
1630ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
16310b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1632ce64c8b9SLars-Peter Clausen 	unsigned int i;
1633ce64c8b9SLars-Peter Clausen 	int ret;
1634ce64c8b9SLars-Peter Clausen 
16350b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1636ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1637ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1638ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1639ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1640ce64c8b9SLars-Peter Clausen 			return ret;
1641ce64c8b9SLars-Peter Clausen 		}
1642ce64c8b9SLars-Peter Clausen 	}
1643ce64c8b9SLars-Peter Clausen 
16442c7b696aSMarcel Ziswiler 	/*
16452c7b696aSMarcel Ziswiler 	 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
16462c7b696aSMarcel Ziswiler 	 * the component which has non_legacy_dai_naming is Codec
16472c7b696aSMarcel Ziswiler 	 */
1648999f7f5aSKuninori Morimoto 	if (cpu_dai->component->driver->non_legacy_dai_naming) {
1649ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1650ce64c8b9SLars-Peter Clausen 
1651ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1652ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1653ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1654ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1655ce64c8b9SLars-Peter Clausen 			break;
1656ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1657ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1658ce64c8b9SLars-Peter Clausen 			break;
1659ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1660ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1661ce64c8b9SLars-Peter Clausen 			break;
1662ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1663ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1664ce64c8b9SLars-Peter Clausen 			break;
1665ce64c8b9SLars-Peter Clausen 		}
1666ce64c8b9SLars-Peter Clausen 
1667ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1668ce64c8b9SLars-Peter Clausen 	}
1669ce64c8b9SLars-Peter Clausen 
1670ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1671ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1672ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1673ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1674ce64c8b9SLars-Peter Clausen 		return ret;
1675ce64c8b9SLars-Peter Clausen 	}
1676ce64c8b9SLars-Peter Clausen 
1677ce64c8b9SLars-Peter Clausen 	return 0;
1678ce64c8b9SLars-Peter Clausen }
1679ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1680ce64c8b9SLars-Peter Clausen 
16811f5a4535STakashi Iwai #ifdef CONFIG_DMI
16822c7b696aSMarcel Ziswiler /*
16832c7b696aSMarcel Ziswiler  * Trim special characters, and replace '-' with '_' since '-' is used to
1684345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1685345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1686345233d7SLiam Girdwood  */
1687345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1688345233d7SLiam Girdwood {
1689345233d7SLiam Girdwood 	int i, j = 0;
1690345233d7SLiam Girdwood 
1691345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1692345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1693345233d7SLiam Girdwood 		    || (name[i] == '_'))
1694345233d7SLiam Girdwood 			name[j++] = name[i];
1695345233d7SLiam Girdwood 		else if (name[i] == '-')
1696345233d7SLiam Girdwood 			name[j++] = '_';
1697345233d7SLiam Girdwood 	}
1698345233d7SLiam Girdwood 
1699345233d7SLiam Girdwood 	name[j] = '\0';
1700345233d7SLiam Girdwood }
1701345233d7SLiam Girdwood 
17022c7b696aSMarcel Ziswiler /*
17032c7b696aSMarcel Ziswiler  * Check if a DMI field is valid, i.e. not containing any string
170498faf436SMengdong Lin  * in the black list.
170598faf436SMengdong Lin  */
170698faf436SMengdong Lin static int is_dmi_valid(const char *field)
170798faf436SMengdong Lin {
170898faf436SMengdong Lin 	int i = 0;
170998faf436SMengdong Lin 
171098faf436SMengdong Lin 	while (dmi_blacklist[i]) {
171198faf436SMengdong Lin 		if (strstr(field, dmi_blacklist[i]))
171298faf436SMengdong Lin 			return 0;
171398faf436SMengdong Lin 		i++;
171446b5a4d2SWu Fengguang 	}
171598faf436SMengdong Lin 
171698faf436SMengdong Lin 	return 1;
171798faf436SMengdong Lin }
171898faf436SMengdong Lin 
17194e01e5dbSJaroslav Kysela /*
17204e01e5dbSJaroslav Kysela  * Append a string to card->dmi_longname with character cleanups.
17214e01e5dbSJaroslav Kysela  */
17224e01e5dbSJaroslav Kysela static void append_dmi_string(struct snd_soc_card *card, const char *str)
17234e01e5dbSJaroslav Kysela {
17244e01e5dbSJaroslav Kysela 	char *dst = card->dmi_longname;
17254e01e5dbSJaroslav Kysela 	size_t dst_len = sizeof(card->dmi_longname);
17264e01e5dbSJaroslav Kysela 	size_t len;
17274e01e5dbSJaroslav Kysela 
17284e01e5dbSJaroslav Kysela 	len = strlen(dst);
17294e01e5dbSJaroslav Kysela 	snprintf(dst + len, dst_len - len, "-%s", str);
17304e01e5dbSJaroslav Kysela 
17314e01e5dbSJaroslav Kysela 	len++;	/* skip the separator "-" */
17324e01e5dbSJaroslav Kysela 	if (len < dst_len)
17334e01e5dbSJaroslav Kysela 		cleanup_dmi_name(dst + len);
17344e01e5dbSJaroslav Kysela }
17354e01e5dbSJaroslav Kysela 
1736345233d7SLiam Girdwood /**
1737345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
1738345233d7SLiam Girdwood  * @card: The card to register DMI names
1739345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
1740345233d7SLiam Girdwood  *
1741345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
1742345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
1743345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
1744345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
1745345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
1746345233d7SLiam Girdwood  * as the sound card long name, in the format of
1747345233d7SLiam Girdwood  * "vendor-product-version-board"
1748345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
1749345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
1750345233d7SLiam Girdwood  * (UCM) configurations for the card.
1751345233d7SLiam Girdwood  *
1752345233d7SLiam Girdwood  * Possible card long names may be:
1753345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
1754345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1755345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1756345233d7SLiam Girdwood  *
1757345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
1758345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
1759345233d7SLiam Girdwood  *
1760345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
1761345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
1762345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
1763345233d7SLiam Girdwood  * charactors like SPACE.
1764345233d7SLiam Girdwood  *
1765345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
1766345233d7SLiam Girdwood  */
1767345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1768345233d7SLiam Girdwood {
1769345233d7SLiam Girdwood 	const char *vendor, *product, *product_version, *board;
1770345233d7SLiam Girdwood 
1771345233d7SLiam Girdwood 	if (card->long_name)
1772345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
1773345233d7SLiam Girdwood 
17744e01e5dbSJaroslav Kysela 	/* make up dmi long name as: vendor-product-version-board */
1775345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
177698faf436SMengdong Lin 	if (!vendor || !is_dmi_valid(vendor)) {
1777345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1778345233d7SLiam Girdwood 		return 0;
1779345233d7SLiam Girdwood 	}
1780345233d7SLiam Girdwood 
17814e01e5dbSJaroslav Kysela 	snprintf(card->dmi_longname, sizeof(card->dmi_longname), "%s", vendor);
1782345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
1783345233d7SLiam Girdwood 
1784345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
178598faf436SMengdong Lin 	if (product && is_dmi_valid(product)) {
17864e01e5dbSJaroslav Kysela 		append_dmi_string(card, product);
1787345233d7SLiam Girdwood 
17882c7b696aSMarcel Ziswiler 		/*
17892c7b696aSMarcel Ziswiler 		 * some vendors like Lenovo may only put a self-explanatory
1790345233d7SLiam Girdwood 		 * name in the product version field
1791345233d7SLiam Girdwood 		 */
1792345233d7SLiam Girdwood 		product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
17934e01e5dbSJaroslav Kysela 		if (product_version && is_dmi_valid(product_version))
17944e01e5dbSJaroslav Kysela 			append_dmi_string(card, product_version);
1795345233d7SLiam Girdwood 	}
1796345233d7SLiam Girdwood 
1797345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
179898faf436SMengdong Lin 	if (board && is_dmi_valid(board)) {
179939870b0dSJaroslav Kysela 		if (!product || strcasecmp(board, product))
18004e01e5dbSJaroslav Kysela 			append_dmi_string(card, board);
1801345233d7SLiam Girdwood 	} else if (!product) {
1802345233d7SLiam Girdwood 		/* fall back to using legacy name */
1803345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1804345233d7SLiam Girdwood 		return 0;
1805345233d7SLiam Girdwood 	}
1806345233d7SLiam Girdwood 
1807345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
18084e01e5dbSJaroslav Kysela 	if (flavour)
18094e01e5dbSJaroslav Kysela 		append_dmi_string(card, flavour);
1810345233d7SLiam Girdwood 
1811345233d7SLiam Girdwood 	/* set the card long name */
1812345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
1813345233d7SLiam Girdwood 
1814345233d7SLiam Girdwood 	return 0;
1815345233d7SLiam Girdwood }
1816345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
18171f5a4535STakashi Iwai #endif /* CONFIG_DMI */
1818345233d7SLiam Girdwood 
1819a655de80SLiam Girdwood static void soc_check_tplg_fes(struct snd_soc_card *card)
1820a655de80SLiam Girdwood {
1821a655de80SLiam Girdwood 	struct snd_soc_component *component;
1822a655de80SLiam Girdwood 	const struct snd_soc_component_driver *comp_drv;
1823a655de80SLiam Girdwood 	struct snd_soc_dai_link *dai_link;
1824a655de80SLiam Girdwood 	int i;
1825a655de80SLiam Girdwood 
1826368dee94SKuninori Morimoto 	for_each_component(component) {
1827a655de80SLiam Girdwood 
182849f9c4f2SDaniel Baluta 		/* does this component override BEs ? */
1829a655de80SLiam Girdwood 		if (!component->driver->ignore_machine)
1830a655de80SLiam Girdwood 			continue;
1831a655de80SLiam Girdwood 
1832a655de80SLiam Girdwood 		/* for this machine ? */
1833e194098bSPierre-Louis Bossart 		if (!strcmp(component->driver->ignore_machine,
1834a655de80SLiam Girdwood 			    card->dev->driver->name))
1835e194098bSPierre-Louis Bossart 			goto match;
1836e194098bSPierre-Louis Bossart 		if (strcmp(component->driver->ignore_machine,
1837e194098bSPierre-Louis Bossart 			   dev_name(card->dev)))
1838a655de80SLiam Girdwood 			continue;
1839e194098bSPierre-Louis Bossart match:
1840a655de80SLiam Girdwood 		/* machine matches, so override the rtd data */
18417fe072b4SKuninori Morimoto 		for_each_card_prelinks(card, i, dai_link) {
1842a655de80SLiam Girdwood 
1843a655de80SLiam Girdwood 			/* ignore this FE */
1844a655de80SLiam Girdwood 			if (dai_link->dynamic) {
1845a655de80SLiam Girdwood 				dai_link->ignore = true;
1846a655de80SLiam Girdwood 				continue;
1847a655de80SLiam Girdwood 			}
1848a655de80SLiam Girdwood 
184949f9c4f2SDaniel Baluta 			dev_info(card->dev, "info: override BE DAI link %s\n",
1850a655de80SLiam Girdwood 				 card->dai_link[i].name);
1851a655de80SLiam Girdwood 
1852a655de80SLiam Girdwood 			/* override platform component */
1853adb76b5bSKuninori Morimoto 			if (!dai_link->platforms) {
1854daecf46eSKuninori Morimoto 				dev_err(card->dev, "init platform error");
1855daecf46eSKuninori Morimoto 				continue;
1856daecf46eSKuninori Morimoto 			}
1857910fdcabSKuninori Morimoto 			dai_link->platforms->name = component->name;
1858a655de80SLiam Girdwood 
1859a655de80SLiam Girdwood 			/* convert non BE into BE */
1860a655de80SLiam Girdwood 			dai_link->no_pcm = 1;
1861a655de80SLiam Girdwood 
1862a655de80SLiam Girdwood 			/* override any BE fixups */
1863a655de80SLiam Girdwood 			dai_link->be_hw_params_fixup =
1864a655de80SLiam Girdwood 				component->driver->be_hw_params_fixup;
1865a655de80SLiam Girdwood 
18662c7b696aSMarcel Ziswiler 			/*
18672c7b696aSMarcel Ziswiler 			 * most BE links don't set stream name, so set it to
1868a655de80SLiam Girdwood 			 * dai link name if it's NULL to help bind widgets.
1869a655de80SLiam Girdwood 			 */
1870a655de80SLiam Girdwood 			if (!dai_link->stream_name)
1871a655de80SLiam Girdwood 				dai_link->stream_name = dai_link->name;
1872a655de80SLiam Girdwood 		}
1873a655de80SLiam Girdwood 
1874a655de80SLiam Girdwood 		/* Inform userspace we are using alternate topology */
1875a655de80SLiam Girdwood 		if (component->driver->topology_name_prefix) {
1876a655de80SLiam Girdwood 
1877a655de80SLiam Girdwood 			/* topology shortname created? */
1878a655de80SLiam Girdwood 			if (!card->topology_shortname_created) {
1879a655de80SLiam Girdwood 				comp_drv = component->driver;
1880a655de80SLiam Girdwood 
1881a655de80SLiam Girdwood 				snprintf(card->topology_shortname, 32, "%s-%s",
1882a655de80SLiam Girdwood 					 comp_drv->topology_name_prefix,
1883a655de80SLiam Girdwood 					 card->name);
1884a655de80SLiam Girdwood 				card->topology_shortname_created = true;
1885a655de80SLiam Girdwood 			}
1886a655de80SLiam Girdwood 
1887a655de80SLiam Girdwood 			/* use topology shortname */
1888a655de80SLiam Girdwood 			card->name = card->topology_shortname;
1889a655de80SLiam Girdwood 		}
1890a655de80SLiam Girdwood 	}
1891a655de80SLiam Girdwood }
1892a655de80SLiam Girdwood 
18930f23f718SKuninori Morimoto #define soc_setup_card_name(name, name1, name2, norm)		\
18940f23f718SKuninori Morimoto 	__soc_setup_card_name(name, sizeof(name), name1, name2, norm)
18950f23f718SKuninori Morimoto static void __soc_setup_card_name(char *name, int len,
18960f23f718SKuninori Morimoto 				  const char *name1, const char *name2,
18970f23f718SKuninori Morimoto 				  int normalization)
18980f23f718SKuninori Morimoto {
18990f23f718SKuninori Morimoto 	int i;
19000f23f718SKuninori Morimoto 
19010f23f718SKuninori Morimoto 	snprintf(name, len, "%s", name1 ? name1 : name2);
19020f23f718SKuninori Morimoto 
19030f23f718SKuninori Morimoto 	if (!normalization)
19040f23f718SKuninori Morimoto 		return;
19050f23f718SKuninori Morimoto 
19060f23f718SKuninori Morimoto 	/*
19070f23f718SKuninori Morimoto 	 * Name normalization
19080f23f718SKuninori Morimoto 	 *
19090f23f718SKuninori Morimoto 	 * The driver name is somewhat special, as it's used as a key for
19100f23f718SKuninori Morimoto 	 * searches in the user-space.
19110f23f718SKuninori Morimoto 	 *
19120f23f718SKuninori Morimoto 	 * ex)
19130f23f718SKuninori Morimoto 	 *	"abcd??efg" -> "abcd__efg"
19140f23f718SKuninori Morimoto 	 */
19150f23f718SKuninori Morimoto 	for (i = 0; i < len; i++) {
19160f23f718SKuninori Morimoto 		switch (name[i]) {
19170f23f718SKuninori Morimoto 		case '_':
19180f23f718SKuninori Morimoto 		case '-':
19190f23f718SKuninori Morimoto 		case '\0':
19200f23f718SKuninori Morimoto 			break;
19210f23f718SKuninori Morimoto 		default:
19220f23f718SKuninori Morimoto 			if (!isalnum(name[i]))
19230f23f718SKuninori Morimoto 				name[i] = '_';
19240f23f718SKuninori Morimoto 			break;
19250f23f718SKuninori Morimoto 		}
19260f23f718SKuninori Morimoto 	}
19270f23f718SKuninori Morimoto }
19280f23f718SKuninori Morimoto 
1929ce21401cSKuninori Morimoto static void soc_cleanup_card_resources(struct snd_soc_card *card,
1930ce21401cSKuninori Morimoto 				       int card_probed)
193153e947a0SKuninori Morimoto {
1932*cc733900SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd, *n;
19337ce6088fSKuninori Morimoto 
19340ced7b05SKuninori Morimoto 	if (card->snd_card)
19350ced7b05SKuninori Morimoto 		snd_card_disconnect_sync(card->snd_card);
193653e947a0SKuninori Morimoto 
19372a6f0892SKuninori Morimoto 	snd_soc_dapm_shutdown(card);
19382a6f0892SKuninori Morimoto 
193953e947a0SKuninori Morimoto 	/* remove and free each DAI */
19407ce6088fSKuninori Morimoto 	soc_remove_link_dais(card);
19410ced7b05SKuninori Morimoto 	soc_remove_link_components(card);
19427ce6088fSKuninori Morimoto 
1943*cc733900SKuninori Morimoto 	for_each_card_rtds_safe(card, rtd, n)
1944*cc733900SKuninori Morimoto 		snd_soc_remove_dai_link(card, rtd->dai_link);
19457ce6088fSKuninori Morimoto 
194653e947a0SKuninori Morimoto 	/* remove auxiliary devices */
194753e947a0SKuninori Morimoto 	soc_remove_aux_devices(card);
1948e8fbd250SKuninori Morimoto 	soc_unbind_aux_dev(card);
194953e947a0SKuninori Morimoto 
195053e947a0SKuninori Morimoto 	snd_soc_dapm_free(&card->dapm);
195153e947a0SKuninori Morimoto 	soc_cleanup_card_debugfs(card);
195253e947a0SKuninori Morimoto 
195353e947a0SKuninori Morimoto 	/* remove the card */
1954ce21401cSKuninori Morimoto 	if (card_probed && card->remove)
195553e947a0SKuninori Morimoto 		card->remove(card);
19560ced7b05SKuninori Morimoto 
19570ced7b05SKuninori Morimoto 	if (card->snd_card) {
19580ced7b05SKuninori Morimoto 		snd_card_free(card->snd_card);
19590ced7b05SKuninori Morimoto 		card->snd_card = NULL;
19600ced7b05SKuninori Morimoto 	}
196153e947a0SKuninori Morimoto }
196253e947a0SKuninori Morimoto 
19632cc1afcfSKuninori Morimoto static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
19642cc1afcfSKuninori Morimoto {
19652cc1afcfSKuninori Morimoto 	if (card->instantiated) {
1966ce21401cSKuninori Morimoto 		int card_probed = 1;
1967ce21401cSKuninori Morimoto 
19682cc1afcfSKuninori Morimoto 		card->instantiated = false;
19692cc1afcfSKuninori Morimoto 		snd_soc_flush_all_delayed_work(card);
19702cc1afcfSKuninori Morimoto 
1971ce21401cSKuninori Morimoto 		soc_cleanup_card_resources(card, card_probed);
19722cc1afcfSKuninori Morimoto 		if (!unregister)
19732cc1afcfSKuninori Morimoto 			list_add(&card->list, &unbind_card_list);
19742cc1afcfSKuninori Morimoto 	} else {
19752cc1afcfSKuninori Morimoto 		if (unregister)
19762cc1afcfSKuninori Morimoto 			list_del(&card->list);
19772cc1afcfSKuninori Morimoto 	}
19782cc1afcfSKuninori Morimoto }
19792cc1afcfSKuninori Morimoto 
1980ed90c013SKuninori Morimoto static int snd_soc_bind_card(struct snd_soc_card *card)
1981f0fba2adSLiam Girdwood {
19821a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
198361b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
1984ce21401cSKuninori Morimoto 	int ret, i, card_probed = 0;
198596dd3622SMark Brown 
198634e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
198701b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1988f0fba2adSLiam Girdwood 
198995c267ddSKuninori Morimoto 	snd_soc_dapm_init(&card->dapm, card, NULL);
199053e947a0SKuninori Morimoto 
1991a655de80SLiam Girdwood 	/* check whether any platform is ignore machine FE and using topology */
1992a655de80SLiam Girdwood 	soc_check_tplg_fes(card);
1993a655de80SLiam Girdwood 
199444c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
1995bee886f1SKuninori Morimoto 	ret = soc_bind_aux_dev(card);
1996bee886f1SKuninori Morimoto 	if (ret < 0)
199753e947a0SKuninori Morimoto 		goto probe_end;
1998db2a4165SFrank Mandarino 
1999f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
2000d8145989SKuninori Morimoto 	card->num_rtd = 0;
20015b99a0aaSKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link) {
20025b99a0aaSKuninori Morimoto 		ret = snd_soc_add_dai_link(card, dai_link);
20035b99a0aaSKuninori Morimoto 		if (ret < 0)
20045b99a0aaSKuninori Morimoto 			goto probe_end;
20055b99a0aaSKuninori Morimoto 	}
2006f8f80361SMengdong Lin 
2007f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
2008102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2009f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
2010f0fba2adSLiam Girdwood 	if (ret < 0) {
201110e8aa9aSMichał Mirosław 		dev_err(card->dev,
201210e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
201310e8aa9aSMichał Mirosław 			card->name, ret);
201453e947a0SKuninori Morimoto 		goto probe_end;
2015db2a4165SFrank Mandarino 	}
2016db2a4165SFrank Mandarino 
20170757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
20180757d834SLars-Peter Clausen 
2019b3da4251SKuninori Morimoto 	soc_resume_init(card);
20206ed25978SAndy Green 
2021b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
20229a841ebbSMark Brown 					card->num_dapm_widgets);
2023b8ba3b57SKuninori Morimoto 	if (ret < 0)
2024b8ba3b57SKuninori Morimoto 		goto probe_end;
20259a841ebbSMark Brown 
2026b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2027f23e860eSNicolin Chen 					card->num_of_dapm_widgets);
2028b8ba3b57SKuninori Morimoto 	if (ret < 0)
2029b8ba3b57SKuninori Morimoto 		goto probe_end;
2030f23e860eSNicolin Chen 
2031f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
2032f0fba2adSLiam Girdwood 	if (card->probe) {
2033e7361ec4SMark Brown 		ret = card->probe(card);
2034f0fba2adSLiam Girdwood 		if (ret < 0)
203553e947a0SKuninori Morimoto 			goto probe_end;
2036ce21401cSKuninori Morimoto 		card_probed = 1;
2037f0fba2adSLiam Girdwood 	}
2038f0fba2adSLiam Girdwood 
203962ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
204062f07a6bSKuninori Morimoto 	ret = soc_probe_link_components(card);
204162ae68faSStephen Warren 	if (ret < 0) {
2042f110bfc7SLiam Girdwood 		dev_err(card->dev,
204362f07a6bSKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
204453e947a0SKuninori Morimoto 		goto probe_end;
204562ae68faSStephen Warren 	}
204662ae68faSStephen Warren 
2047f2ed6b07SMengdong Lin 	/* probe auxiliary components */
2048f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
204974bd3f92SKuninori Morimoto 	if (ret < 0) {
205074bd3f92SKuninori Morimoto 		dev_err(card->dev,
205174bd3f92SKuninori Morimoto 			"ASoC: failed to probe aux component %d\n", ret);
205253e947a0SKuninori Morimoto 		goto probe_end;
205374bd3f92SKuninori Morimoto 	}
2054f2ed6b07SMengdong Lin 
205562ae68faSStephen Warren 	/* probe all DAI links on this card */
2056c7e73774SKuninori Morimoto 	ret = soc_probe_link_dais(card);
2057fe3e78e0SMark Brown 	if (ret < 0) {
2058f110bfc7SLiam Girdwood 		dev_err(card->dev,
2059c7e73774SKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
206053e947a0SKuninori Morimoto 		goto probe_end;
2061fe3e78e0SMark Brown 	}
2062fe3e78e0SMark Brown 
2063c4b46982SKuninori Morimoto 	for_each_card_rtds(card, rtd)
2064c4b46982SKuninori Morimoto 		soc_link_init(card, rtd);
2065c4b46982SKuninori Morimoto 
2066888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
2067b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
2068888df395SMark Brown 
20699b98c7c2SKuninori Morimoto 	ret = snd_soc_add_card_controls(card, card->controls,
20702c7b696aSMarcel Ziswiler 					card->num_controls);
20719b98c7c2SKuninori Morimoto 	if (ret < 0)
20729b98c7c2SKuninori Morimoto 		goto probe_end;
2073b7af1dafSMark Brown 
2074daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2075b8ad29deSMark Brown 				      card->num_dapm_routes);
2076daa480bdSKuninori Morimoto 	if (ret < 0)
2077daa480bdSKuninori Morimoto 		goto probe_end;
2078b8ad29deSMark Brown 
2079daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2080f23e860eSNicolin Chen 				      card->num_of_dapm_routes);
2081daa480bdSKuninori Morimoto 	if (ret < 0)
2082daa480bdSKuninori Morimoto 		goto probe_end;
208375d9ac46SMark Brown 
2084861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
2085861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
2086861886d3STakashi Iwai 
20870f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->shortname,
20880f23f718SKuninori Morimoto 			    card->name, NULL, 0);
20890f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->longname,
20900f23f718SKuninori Morimoto 			    card->long_name, card->name, 0);
20910f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->driver,
20920f23f718SKuninori Morimoto 			    card->driver_name, card->name, 1);
2093fe3e78e0SMark Brown 
2094dc73d73aSJaroslav Kysela 	if (card->components) {
2095dc73d73aSJaroslav Kysela 		/* the current implementation of snd_component_add() accepts */
2096dc73d73aSJaroslav Kysela 		/* multiple components in the string separated by space, */
2097dc73d73aSJaroslav Kysela 		/* but the string collision (identical string) check might */
2098dc73d73aSJaroslav Kysela 		/* not work correctly */
2099dc73d73aSJaroslav Kysela 		ret = snd_component_add(card->snd_card, card->components);
2100dc73d73aSJaroslav Kysela 		if (ret < 0) {
2101dc73d73aSJaroslav Kysela 			dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n",
2102dc73d73aSJaroslav Kysela 				card->name, ret);
2103dc73d73aSJaroslav Kysela 			goto probe_end;
2104dc73d73aSJaroslav Kysela 		}
2105dc73d73aSJaroslav Kysela 	}
2106dc73d73aSJaroslav Kysela 
210728e9ad92SMark Brown 	if (card->late_probe) {
210828e9ad92SMark Brown 		ret = card->late_probe(card);
210928e9ad92SMark Brown 		if (ret < 0) {
2110f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
211128e9ad92SMark Brown 				card->name, ret);
211253e947a0SKuninori Morimoto 			goto probe_end;
211328e9ad92SMark Brown 		}
211428e9ad92SMark Brown 	}
2115ce21401cSKuninori Morimoto 	card_probed = 1;
211628e9ad92SMark Brown 
2117824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
21188c193b8dSLars-Peter Clausen 
2119f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2120fe3e78e0SMark Brown 	if (ret < 0) {
2121f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2122f110bfc7SLiam Girdwood 				ret);
212353e947a0SKuninori Morimoto 		goto probe_end;
2124fe3e78e0SMark Brown 	}
2125fe3e78e0SMark Brown 
2126435c5e25SMark Brown 	card->instantiated = 1;
2127882eab6cSTzung-Bi Shih 	dapm_mark_endpoints_dirty(card);
21284f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2129b19e6e7bSMark Brown 
2130ed90c013SKuninori Morimoto 	/* deactivate pins to sleep state */
2131ed90c013SKuninori Morimoto 	for_each_card_rtds(card, rtd) {
2132ed90c013SKuninori Morimoto 		struct snd_soc_dai *dai;
2133ed90c013SKuninori Morimoto 
2134ed90c013SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
2135ed90c013SKuninori Morimoto 			if (!dai->active)
2136ed90c013SKuninori Morimoto 				pinctrl_pm_select_sleep_state(dai->dev);
2137ed90c013SKuninori Morimoto 		}
2138ed90c013SKuninori Morimoto 
2139ed90c013SKuninori Morimoto 		if (!rtd->cpu_dai->active)
2140ed90c013SKuninori Morimoto 			pinctrl_pm_select_sleep_state(rtd->cpu_dai->dev);
2141ed90c013SKuninori Morimoto 	}
2142ed90c013SKuninori Morimoto 
214353e947a0SKuninori Morimoto probe_end:
214453e947a0SKuninori Morimoto 	if (ret < 0)
2145ce21401cSKuninori Morimoto 		soc_cleanup_card_resources(card, card_probed);
2146db2a4165SFrank Mandarino 
2147f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
214834e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2149db2a4165SFrank Mandarino 
2150b19e6e7bSMark Brown 	return ret;
2151435c5e25SMark Brown }
2152435c5e25SMark Brown 
2153435c5e25SMark Brown /* probes a new socdev */
2154435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2155435c5e25SMark Brown {
2156f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2157435c5e25SMark Brown 
215870a7ca34SVinod Koul 	/*
215970a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
216070a7ca34SVinod Koul 	 * we should not be here in that case so ret error
216170a7ca34SVinod Koul 	 */
216270a7ca34SVinod Koul 	if (!card)
216370a7ca34SVinod Koul 		return -EINVAL;
216470a7ca34SVinod Koul 
2165fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2166f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2167fe4085e8SMark Brown 		 card->name);
2168fe4085e8SMark Brown 
2169435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2170435c5e25SMark Brown 	card->dev = &pdev->dev;
2171f0fba2adSLiam Girdwood 
217228d528c8SMark Brown 	return snd_soc_register_card(card);
2173435c5e25SMark Brown }
2174435c5e25SMark Brown 
2175b0e26485SVinod Koul /* removes a socdev */
2176b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2177b0e26485SVinod Koul {
2178b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2179b0e26485SVinod Koul 
2180c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2181db2a4165SFrank Mandarino 	return 0;
2182db2a4165SFrank Mandarino }
2183db2a4165SFrank Mandarino 
21846f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
218551737470SMark Brown {
21866f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
21871a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
218851737470SMark Brown 
218951737470SMark Brown 	if (!card->instantiated)
2190416356fcSMark Brown 		return 0;
219151737470SMark Brown 
21922c7b696aSMarcel Ziswiler 	/*
21932c7b696aSMarcel Ziswiler 	 * Flush out pmdown_time work - we actually do want to run it
21942c7b696aSMarcel Ziswiler 	 * now, we're shutting down so no imminent restart.
21952c7b696aSMarcel Ziswiler 	 */
219665462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
219751737470SMark Brown 
2198f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2199416356fcSMark Brown 
2200988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
2201bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
220288bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
22030b7990e3SKuninori Morimoto 		struct snd_soc_dai *codec_dai;
22041a497983SMengdong Lin 		int i;
220588bd870fSBenoit Cousson 
2206988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
22070b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
220888bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
220988bd870fSBenoit Cousson 		}
2210988e8cc4SNicolin Chen 	}
2211988e8cc4SNicolin Chen 
2212416356fcSMark Brown 	return 0;
221351737470SMark Brown }
22146f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
221551737470SMark Brown 
22166f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2217b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2218b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2219b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2220b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
22216f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2222b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2223416356fcSMark Brown };
2224deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2225416356fcSMark Brown 
2226db2a4165SFrank Mandarino /* ASoC platform driver */
2227db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2228db2a4165SFrank Mandarino 	.driver		= {
2229db2a4165SFrank Mandarino 		.name		= "soc-audio",
22306f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2231db2a4165SFrank Mandarino 	},
2232db2a4165SFrank Mandarino 	.probe		= soc_probe,
2233db2a4165SFrank Mandarino 	.remove		= soc_remove,
2234db2a4165SFrank Mandarino };
2235db2a4165SFrank Mandarino 
2236096e49d5SMark Brown /**
2237db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2238db2a4165SFrank Mandarino  * @_template: control template
2239db2a4165SFrank Mandarino  * @data: control private data
2240ac11a2b3SMark Brown  * @long_name: control long name
2241efb7ac3fSMark Brown  * @prefix: control name prefix
2242db2a4165SFrank Mandarino  *
2243db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2244db2a4165SFrank Mandarino  *
2245db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2246db2a4165SFrank Mandarino  */
2247db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
22483056557fSMark Brown 				  void *data, const char *long_name,
2249efb7ac3fSMark Brown 				  const char *prefix)
2250db2a4165SFrank Mandarino {
2251db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2252efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2253efb7ac3fSMark Brown 	char *name = NULL;
2254db2a4165SFrank Mandarino 
2255db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2256db2a4165SFrank Mandarino 	template.index = 0;
2257db2a4165SFrank Mandarino 
2258efb7ac3fSMark Brown 	if (!long_name)
2259efb7ac3fSMark Brown 		long_name = template.name;
2260efb7ac3fSMark Brown 
2261efb7ac3fSMark Brown 	if (prefix) {
22622b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2263efb7ac3fSMark Brown 		if (!name)
2264efb7ac3fSMark Brown 			return NULL;
2265efb7ac3fSMark Brown 
2266efb7ac3fSMark Brown 		template.name = name;
2267efb7ac3fSMark Brown 	} else {
2268efb7ac3fSMark Brown 		template.name = long_name;
2269efb7ac3fSMark Brown 	}
2270efb7ac3fSMark Brown 
2271efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2272efb7ac3fSMark Brown 
2273efb7ac3fSMark Brown 	kfree(name);
2274efb7ac3fSMark Brown 
2275efb7ac3fSMark Brown 	return kcontrol;
2276db2a4165SFrank Mandarino }
2277db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2278db2a4165SFrank Mandarino 
2279022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2280022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2281022658beSLiam Girdwood 	const char *prefix, void *data)
2282022658beSLiam Girdwood {
2283022658beSLiam Girdwood 	int err, i;
2284022658beSLiam Girdwood 
2285022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2286022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
22872c7b696aSMarcel Ziswiler 
2288022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2289022658beSLiam Girdwood 						     control->name, prefix));
2290022658beSLiam Girdwood 		if (err < 0) {
2291f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2292f110bfc7SLiam Girdwood 				control->name, err);
2293022658beSLiam Girdwood 			return err;
2294022658beSLiam Girdwood 		}
2295022658beSLiam Girdwood 	}
2296022658beSLiam Girdwood 
2297022658beSLiam Girdwood 	return 0;
2298022658beSLiam Girdwood }
2299022658beSLiam Girdwood 
23004fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
23014fefd698SDimitris Papastamos 					       const char *name)
23024fefd698SDimitris Papastamos {
23034fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
23044fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
23054fefd698SDimitris Papastamos 
23064fefd698SDimitris Papastamos 	if (unlikely(!name))
23074fefd698SDimitris Papastamos 		return NULL;
23084fefd698SDimitris Papastamos 
23094fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
23104fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
23114fefd698SDimitris Papastamos 			return kctl;
23124fefd698SDimitris Papastamos 	return NULL;
23134fefd698SDimitris Papastamos }
23144fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
23154fefd698SDimitris Papastamos 
2316db2a4165SFrank Mandarino /**
23170f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
23180f2780adSLars-Peter Clausen  *
23190f2780adSLars-Peter Clausen  * @component: Component to add controls to
23200f2780adSLars-Peter Clausen  * @controls: Array of controls to add
23210f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
23220f2780adSLars-Peter Clausen  *
23230f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
23240f2780adSLars-Peter Clausen  */
23250f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
23260f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
23270f2780adSLars-Peter Clausen {
23280f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
23290f2780adSLars-Peter Clausen 
23300f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
23310f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
23320f2780adSLars-Peter Clausen }
23330f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
23340f2780adSLars-Peter Clausen 
23350f2780adSLars-Peter Clausen /**
2336022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2337022658beSLiam Girdwood  * Convenience function to add a list of controls.
2338022658beSLiam Girdwood  *
2339022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2340022658beSLiam Girdwood  * @controls: array of controls to add
2341022658beSLiam Girdwood  * @num_controls: number of elements in the array
2342022658beSLiam Girdwood  *
2343022658beSLiam Girdwood  * Return 0 for success, else error.
2344022658beSLiam Girdwood  */
2345022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2346022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2347022658beSLiam Girdwood {
2348022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2349022658beSLiam Girdwood 
2350022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2351022658beSLiam Girdwood 			NULL, soc_card);
2352022658beSLiam Girdwood }
2353022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2354022658beSLiam Girdwood 
2355022658beSLiam Girdwood /**
2356022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2357022658beSLiam Girdwood  * Convienience function to add a list of controls.
2358022658beSLiam Girdwood  *
2359022658beSLiam Girdwood  * @dai: DAI to add controls to
2360022658beSLiam Girdwood  * @controls: array of controls to add
2361022658beSLiam Girdwood  * @num_controls: number of elements in the array
2362022658beSLiam Girdwood  *
2363022658beSLiam Girdwood  * Return 0 for success, else error.
2364022658beSLiam Girdwood  */
2365022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2366022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2367022658beSLiam Girdwood {
2368313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2369022658beSLiam Girdwood 
2370022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2371022658beSLiam Girdwood 			NULL, dai);
2372022658beSLiam Girdwood }
2373022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2374022658beSLiam Girdwood 
2375c5af3a2eSMark Brown /**
2376c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2377c5af3a2eSMark Brown  *
2378ac11a2b3SMark Brown  * @card: Card to register
2379c5af3a2eSMark Brown  *
2380c5af3a2eSMark Brown  */
238170a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2382c5af3a2eSMark Brown {
2383c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2384c5af3a2eSMark Brown 		return -EINVAL;
2385c5af3a2eSMark Brown 
2386ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2387ed77cc12SMark Brown 
2388b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->widgets);
2389b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->paths);
2390b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->dapm_list);
2391b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->aux_comp_list);
2392b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->component_dev_list);
2393b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->list);
23941a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
2395db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
23968a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2397b03bfaecSKuninori Morimoto 
2398c5af3a2eSMark Brown 	card->instantiated = 0;
2399f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
2400a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
240172b745e3SPeter Ujfalusi 	mutex_init(&card->pcm_mutex);
2402a9764869SKaiChieh Chuang 	spin_lock_init(&card->dpcm_lock);
2403c5af3a2eSMark Brown 
2404e894efefSSrinivas Kandagatla 	return snd_soc_bind_card(card);
2405c5af3a2eSMark Brown }
240670a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2407c5af3a2eSMark Brown 
2408c5af3a2eSMark Brown /**
2409c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2410c5af3a2eSMark Brown  *
2411ac11a2b3SMark Brown  * @card: Card to unregister
2412c5af3a2eSMark Brown  *
2413c5af3a2eSMark Brown  */
241470a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2415c5af3a2eSMark Brown {
2416b545542aSKuninori Morimoto 	mutex_lock(&client_mutex);
2417e894efefSSrinivas Kandagatla 	snd_soc_unbind_card(card, true);
2418b545542aSKuninori Morimoto 	mutex_unlock(&client_mutex);
2419f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2420c5af3a2eSMark Brown 
2421c5af3a2eSMark Brown 	return 0;
2422c5af3a2eSMark Brown }
242370a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2424c5af3a2eSMark Brown 
2425f0fba2adSLiam Girdwood /*
2426f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
2427f0fba2adSLiam Girdwood  * and sanitizing names.
2428f0fba2adSLiam Girdwood  */
24290b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2430f0fba2adSLiam Girdwood {
2431f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
2432f0fba2adSLiam Girdwood 	int id1, id2;
2433f0fba2adSLiam Girdwood 
2434f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
2435f0fba2adSLiam Girdwood 		return NULL;
2436f0fba2adSLiam Girdwood 
243758818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2438f0fba2adSLiam Girdwood 
2439f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
2440f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
2441f0fba2adSLiam Girdwood 	if (found) {
2442f0fba2adSLiam Girdwood 		/* get ID */
2443f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2444f0fba2adSLiam Girdwood 
2445f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
2446f0fba2adSLiam Girdwood 			if (*id == -1)
2447f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
2448f0fba2adSLiam Girdwood 		}
2449f0fba2adSLiam Girdwood 
2450f0fba2adSLiam Girdwood 	} else {
2451f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr" */
2452f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2453f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
2454f0fba2adSLiam Girdwood 
2455f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
245605899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
2457f0fba2adSLiam Girdwood 
2458f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
24592c7b696aSMarcel Ziswiler 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
24602c7b696aSMarcel Ziswiler 				 name);
246158818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2462f0fba2adSLiam Girdwood 		} else
2463f0fba2adSLiam Girdwood 			*id = 0;
2464f0fba2adSLiam Girdwood 	}
2465f0fba2adSLiam Girdwood 
246650014499SKuninori Morimoto 	return devm_kstrdup(dev, name, GFP_KERNEL);
2467f0fba2adSLiam Girdwood }
2468f0fba2adSLiam Girdwood 
2469f0fba2adSLiam Girdwood /*
2470f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
2471f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
2472f0fba2adSLiam Girdwood  */
2473f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
2474f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
2475f0fba2adSLiam Girdwood {
2476f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
247710e8aa9aSMichał Mirosław 		dev_err(dev,
247810e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
247910e8aa9aSMichał Mirosław 			dev_name(dev));
2480f0fba2adSLiam Girdwood 		return NULL;
2481f0fba2adSLiam Girdwood 	}
2482f0fba2adSLiam Girdwood 
248350014499SKuninori Morimoto 	return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2484f0fba2adSLiam Girdwood }
2485f0fba2adSLiam Girdwood 
2486ffdbca0bSKuninori Morimoto void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2487e11381f3SKuninori Morimoto {
2488e11381f3SKuninori Morimoto 	dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2489e11381f3SKuninori Morimoto 	list_del(&dai->list);
2490e11381f3SKuninori Morimoto }
2491ffdbca0bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2492e11381f3SKuninori Morimoto 
24937ca24386SKuninori Morimoto /**
24947ca24386SKuninori Morimoto  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
24957ca24386SKuninori Morimoto  *
24967ca24386SKuninori Morimoto  * @component: The component the DAIs are registered for
24977ca24386SKuninori Morimoto  * @dai_drv: DAI driver to use for the DAI
2498bc9a6655SRandy Dunlap  * @legacy_dai_naming: if %true, use legacy single-name format;
2499bc9a6655SRandy Dunlap  * 	if %false, use multiple-name format;
25007ca24386SKuninori Morimoto  *
25017ca24386SKuninori Morimoto  * Topology can use this API to register DAIs when probing a component.
25027ca24386SKuninori Morimoto  * These DAIs's widgets will be freed in the card cleanup and the DAIs
25037ca24386SKuninori Morimoto  * will be freed in the component cleanup.
25047ca24386SKuninori Morimoto  */
25057ca24386SKuninori Morimoto struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
25065e4fb372SMengdong Lin 					 struct snd_soc_dai_driver *dai_drv,
25075e4fb372SMengdong Lin 					 bool legacy_dai_naming)
25085e4fb372SMengdong Lin {
25095e4fb372SMengdong Lin 	struct device *dev = component->dev;
25105e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
25115e4fb372SMengdong Lin 
25125e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
25135e4fb372SMengdong Lin 
25147ca24386SKuninori Morimoto 	lockdep_assert_held(&client_mutex);
25157ca24386SKuninori Morimoto 
251650014499SKuninori Morimoto 	dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
25175e4fb372SMengdong Lin 	if (dai == NULL)
25185e4fb372SMengdong Lin 		return NULL;
25195e4fb372SMengdong Lin 
25205e4fb372SMengdong Lin 	/*
25215e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
25225e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
25235e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
25245e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
25255e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
25265e4fb372SMengdong Lin 	 * component-less anymore.
25275e4fb372SMengdong Lin 	 */
25285e4fb372SMengdong Lin 	if (legacy_dai_naming &&
25295e4fb372SMengdong Lin 	    (dai_drv->id == 0 || dai_drv->name == NULL)) {
25305e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
25315e4fb372SMengdong Lin 	} else {
25325e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
25335e4fb372SMengdong Lin 		if (dai_drv->id)
25345e4fb372SMengdong Lin 			dai->id = dai_drv->id;
25355e4fb372SMengdong Lin 		else
25365e4fb372SMengdong Lin 			dai->id = component->num_dai;
25375e4fb372SMengdong Lin 	}
253850014499SKuninori Morimoto 	if (!dai->name)
25395e4fb372SMengdong Lin 		return NULL;
25405e4fb372SMengdong Lin 
25415e4fb372SMengdong Lin 	dai->component = component;
25425e4fb372SMengdong Lin 	dai->dev = dev;
25435e4fb372SMengdong Lin 	dai->driver = dai_drv;
25445e4fb372SMengdong Lin 	if (!dai->driver->ops)
25455e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
25465e4fb372SMengdong Lin 
254715a0c645SKuninori Morimoto 	/* see for_each_component_dais */
254858bf4179SKuninori Morimoto 	list_add_tail(&dai->list, &component->dai_list);
25495e4fb372SMengdong Lin 	component->num_dai++;
25505e4fb372SMengdong Lin 
25515e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
25525e4fb372SMengdong Lin 	return dai;
25535e4fb372SMengdong Lin }
2554e11381f3SKuninori Morimoto 
25559115171aSMark Brown /**
25563f6674aeSKuninori Morimoto  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
25573f6674aeSKuninori Morimoto  *
25583f6674aeSKuninori Morimoto  * @component: The component for which the DAIs should be unregistered
25593f6674aeSKuninori Morimoto  */
25603f6674aeSKuninori Morimoto static void snd_soc_unregister_dais(struct snd_soc_component *component)
25613f6674aeSKuninori Morimoto {
25623f6674aeSKuninori Morimoto 	struct snd_soc_dai *dai, *_dai;
25633f6674aeSKuninori Morimoto 
2564e11381f3SKuninori Morimoto 	for_each_component_dais_safe(component, dai, _dai)
2565e11381f3SKuninori Morimoto 		snd_soc_unregister_dai(dai);
25663f6674aeSKuninori Morimoto }
25673f6674aeSKuninori Morimoto 
25683f6674aeSKuninori Morimoto /**
2569daf77373SKuninori Morimoto  * snd_soc_register_dais - Register a DAI with the ASoC core
2570daf77373SKuninori Morimoto  *
2571daf77373SKuninori Morimoto  * @component: The component the DAIs are registered for
2572daf77373SKuninori Morimoto  * @dai_drv: DAI driver to use for the DAIs
2573daf77373SKuninori Morimoto  * @count: Number of DAIs
2574daf77373SKuninori Morimoto  */
2575daf77373SKuninori Morimoto static int snd_soc_register_dais(struct snd_soc_component *component,
2576daf77373SKuninori Morimoto 				 struct snd_soc_dai_driver *dai_drv,
2577daf77373SKuninori Morimoto 				 size_t count)
2578daf77373SKuninori Morimoto {
2579daf77373SKuninori Morimoto 	struct snd_soc_dai *dai;
2580daf77373SKuninori Morimoto 	unsigned int i;
2581daf77373SKuninori Morimoto 	int ret;
2582daf77373SKuninori Morimoto 
2583daf77373SKuninori Morimoto 	for (i = 0; i < count; i++) {
258471cb85f5SKuninori Morimoto 		dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
2585daf77373SKuninori Morimoto 				  !component->driver->non_legacy_dai_naming);
2586daf77373SKuninori Morimoto 		if (dai == NULL) {
2587daf77373SKuninori Morimoto 			ret = -ENOMEM;
2588daf77373SKuninori Morimoto 			goto err;
2589daf77373SKuninori Morimoto 		}
2590daf77373SKuninori Morimoto 	}
2591daf77373SKuninori Morimoto 
2592daf77373SKuninori Morimoto 	return 0;
2593daf77373SKuninori Morimoto 
2594daf77373SKuninori Morimoto err:
2595daf77373SKuninori Morimoto 	snd_soc_unregister_dais(component);
2596daf77373SKuninori Morimoto 
2597daf77373SKuninori Morimoto 	return ret;
2598daf77373SKuninori Morimoto }
2599daf77373SKuninori Morimoto 
2600bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
2601bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
2602d191bd8dSKuninori Morimoto {
26038d92bb51SKuninori Morimoto 	INIT_LIST_HEAD(&component->dai_list);
2604495efdb0SKuninori Morimoto 	INIT_LIST_HEAD(&component->dobj_list);
2605495efdb0SKuninori Morimoto 	INIT_LIST_HEAD(&component->card_list);
26068d92bb51SKuninori Morimoto 	mutex_init(&component->io_mutex);
26078d92bb51SKuninori Morimoto 
2608bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
2609bb13109dSLars-Peter Clausen 	if (!component->name) {
2610bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
2611d191bd8dSKuninori Morimoto 		return -ENOMEM;
2612d191bd8dSKuninori Morimoto 	}
2613d191bd8dSKuninori Morimoto 
2614bb13109dSLars-Peter Clausen 	component->dev = dev;
2615bb13109dSLars-Peter Clausen 	component->driver = driver;
2616e2c330b9SLars-Peter Clausen 
2617bb13109dSLars-Peter Clausen 	return 0;
2618d191bd8dSKuninori Morimoto }
2619d191bd8dSKuninori Morimoto 
262020feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2621886f5692SLars-Peter Clausen {
2622886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
262320feb881SLars-Peter Clausen 
2624886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
2625886f5692SLars-Peter Clausen 	if (val_bytes > 0)
2626886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
2627886f5692SLars-Peter Clausen }
262820feb881SLars-Peter Clausen 
2629e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
2630e874bf5fSLars-Peter Clausen 
263120feb881SLars-Peter Clausen /**
26322c7b696aSMarcel Ziswiler  * snd_soc_component_init_regmap() - Initialize regmap instance for the
26332c7b696aSMarcel Ziswiler  *                                   component
263420feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
263520feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
263620feb881SLars-Peter Clausen  *
263720feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
263820feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
263920feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
264020feb881SLars-Peter Clausen  * before the first IO attempt of the component.
264120feb881SLars-Peter Clausen  */
264220feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
264320feb881SLars-Peter Clausen 	struct regmap *regmap)
264420feb881SLars-Peter Clausen {
264520feb881SLars-Peter Clausen 	component->regmap = regmap;
264620feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
2647886f5692SLars-Peter Clausen }
264820feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
264920feb881SLars-Peter Clausen 
265020feb881SLars-Peter Clausen /**
26512c7b696aSMarcel Ziswiler  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
26522c7b696aSMarcel Ziswiler  *                                   component
265320feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
265420feb881SLars-Peter Clausen  *
265520feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
265620feb881SLars-Peter Clausen  * removes the regmap instance from the component.
265720feb881SLars-Peter Clausen  *
265820feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
265920feb881SLars-Peter Clausen  * to initialize the regmap instance.
266020feb881SLars-Peter Clausen  */
266120feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
266220feb881SLars-Peter Clausen {
266320feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
266420feb881SLars-Peter Clausen 	component->regmap = NULL;
266520feb881SLars-Peter Clausen }
266620feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2667886f5692SLars-Peter Clausen 
2668e874bf5fSLars-Peter Clausen #endif
2669d191bd8dSKuninori Morimoto 
2670273d778eSKuninori Morimoto #define ENDIANNESS_MAP(name) \
2671273d778eSKuninori Morimoto 	(SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2672273d778eSKuninori Morimoto static u64 endianness_format_map[] = {
2673273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S16_),
2674273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U16_),
2675273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_),
2676273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_),
2677273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S32_),
2678273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U32_),
2679273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_3),
2680273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_3),
2681273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S20_3),
2682273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U20_3),
2683273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S18_3),
2684273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U18_3),
2685273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT_),
2686273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT64_),
2687273d778eSKuninori Morimoto 	ENDIANNESS_MAP(IEC958_SUBFRAME_),
2688273d778eSKuninori Morimoto };
2689273d778eSKuninori Morimoto 
2690273d778eSKuninori Morimoto /*
2691273d778eSKuninori Morimoto  * Fix up the DAI formats for endianness: codecs don't actually see
2692273d778eSKuninori Morimoto  * the endianness of the data but we're using the CPU format
2693273d778eSKuninori Morimoto  * definitions which do need to include endianness so we ensure that
2694273d778eSKuninori Morimoto  * codec DAIs always have both big and little endian variants set.
2695273d778eSKuninori Morimoto  */
2696273d778eSKuninori Morimoto static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2697273d778eSKuninori Morimoto {
2698273d778eSKuninori Morimoto 	int i;
2699273d778eSKuninori Morimoto 
2700273d778eSKuninori Morimoto 	for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2701273d778eSKuninori Morimoto 		if (stream->formats & endianness_format_map[i])
2702273d778eSKuninori Morimoto 			stream->formats |= endianness_format_map[i];
2703273d778eSKuninori Morimoto }
2704273d778eSKuninori Morimoto 
2705e894efefSSrinivas Kandagatla static void snd_soc_try_rebind_card(void)
2706e894efefSSrinivas Kandagatla {
2707e894efefSSrinivas Kandagatla 	struct snd_soc_card *card, *c;
2708e894efefSSrinivas Kandagatla 
2709b245d273SKuninori Morimoto 	list_for_each_entry_safe(card, c, &unbind_card_list, list)
2710e894efefSSrinivas Kandagatla 		if (!snd_soc_bind_card(card))
2711e894efefSSrinivas Kandagatla 			list_del(&card->list);
2712e894efefSSrinivas Kandagatla }
2713e894efefSSrinivas Kandagatla 
2714486c7978SKuninori Morimoto static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2715486c7978SKuninori Morimoto {
2716b18768f5SKuninori Morimoto 	struct snd_soc_card *card = component->card;
2717b18768f5SKuninori Morimoto 
2718486c7978SKuninori Morimoto 	snd_soc_unregister_dais(component);
2719b18768f5SKuninori Morimoto 
2720b18768f5SKuninori Morimoto 	if (card)
2721b18768f5SKuninori Morimoto 		snd_soc_unbind_card(card, false);
2722b18768f5SKuninori Morimoto 
2723b18768f5SKuninori Morimoto 	list_del(&component->list);
2724486c7978SKuninori Morimoto }
2725486c7978SKuninori Morimoto 
2726e0dac41bSKuninori Morimoto int snd_soc_add_component(struct device *dev,
2727e0dac41bSKuninori Morimoto 			struct snd_soc_component *component,
2728cf9e829eSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2729d191bd8dSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2730d191bd8dSKuninori Morimoto 			int num_dai)
2731d191bd8dSKuninori Morimoto {
2732bb13109dSLars-Peter Clausen 	int ret;
2733273d778eSKuninori Morimoto 	int i;
2734d191bd8dSKuninori Morimoto 
2735b18768f5SKuninori Morimoto 	mutex_lock(&client_mutex);
2736b18768f5SKuninori Morimoto 
2737cf9e829eSKuninori Morimoto 	ret = snd_soc_component_initialize(component, component_driver, dev);
2738bb13109dSLars-Peter Clausen 	if (ret)
2739bb13109dSLars-Peter Clausen 		goto err_free;
2740bb13109dSLars-Peter Clausen 
2741273d778eSKuninori Morimoto 	if (component_driver->endianness) {
2742273d778eSKuninori Morimoto 		for (i = 0; i < num_dai; i++) {
2743273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].playback);
2744273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].capture);
2745273d778eSKuninori Morimoto 		}
2746273d778eSKuninori Morimoto 	}
2747273d778eSKuninori Morimoto 
27480e7b25c6SKuninori Morimoto 	ret = snd_soc_register_dais(component, dai_drv, num_dai);
2749bb13109dSLars-Peter Clausen 	if (ret < 0) {
2750f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
2751bb13109dSLars-Peter Clausen 		goto err_cleanup;
2752bb13109dSLars-Peter Clausen 	}
2753bb13109dSLars-Peter Clausen 
2754b18768f5SKuninori Morimoto 	if (!component->driver->write && !component->driver->read) {
2755b18768f5SKuninori Morimoto 		if (!component->regmap)
2756b18768f5SKuninori Morimoto 			component->regmap = dev_get_regmap(component->dev,
2757b18768f5SKuninori Morimoto 							   NULL);
2758b18768f5SKuninori Morimoto 		if (component->regmap)
2759b18768f5SKuninori Morimoto 			snd_soc_component_setup_regmap(component);
2760b18768f5SKuninori Morimoto 	}
2761bb13109dSLars-Peter Clausen 
2762b18768f5SKuninori Morimoto 	/* see for_each_component */
2763b18768f5SKuninori Morimoto 	list_add(&component->list, &component_list);
2764bb13109dSLars-Peter Clausen 
2765bb13109dSLars-Peter Clausen err_cleanup:
2766b18768f5SKuninori Morimoto 	if (ret < 0)
2767486c7978SKuninori Morimoto 		snd_soc_del_component_unlocked(component);
2768bb13109dSLars-Peter Clausen err_free:
2769b18768f5SKuninori Morimoto 	mutex_unlock(&client_mutex);
2770b18768f5SKuninori Morimoto 
2771b18768f5SKuninori Morimoto 	if (ret == 0)
2772b18768f5SKuninori Morimoto 		snd_soc_try_rebind_card();
2773b18768f5SKuninori Morimoto 
2774bb13109dSLars-Peter Clausen 	return ret;
2775d191bd8dSKuninori Morimoto }
2776e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component);
2777e0dac41bSKuninori Morimoto 
2778e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev,
2779e0dac41bSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2780e0dac41bSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2781e0dac41bSKuninori Morimoto 			int num_dai)
2782e0dac41bSKuninori Morimoto {
2783e0dac41bSKuninori Morimoto 	struct snd_soc_component *component;
2784e0dac41bSKuninori Morimoto 
27857ecbd6a9SKuninori Morimoto 	component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
278608e61d03SKuninori Morimoto 	if (!component)
2787e0dac41bSKuninori Morimoto 		return -ENOMEM;
2788e0dac41bSKuninori Morimoto 
2789e0dac41bSKuninori Morimoto 	return snd_soc_add_component(dev, component, component_driver,
2790e0dac41bSKuninori Morimoto 				     dai_drv, num_dai);
2791e0dac41bSKuninori Morimoto }
2792d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
2793d191bd8dSKuninori Morimoto 
2794d191bd8dSKuninori Morimoto /**
27952eccea8cSKuninori Morimoto  * snd_soc_unregister_component - Unregister all related component
27962eccea8cSKuninori Morimoto  * from the ASoC core
2797d191bd8dSKuninori Morimoto  *
2798628536eaSJonathan Corbet  * @dev: The device to unregister
2799d191bd8dSKuninori Morimoto  */
28002eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
28012eccea8cSKuninori Morimoto {
2802ac6a4dd3SKuninori Morimoto 	struct snd_soc_component *component;
2803ac6a4dd3SKuninori Morimoto 
2804ac6a4dd3SKuninori Morimoto 	mutex_lock(&client_mutex);
2805ac6a4dd3SKuninori Morimoto 	while (1) {
280618dd66eaSKuninori Morimoto 		component = snd_soc_lookup_component_nolocked(dev, NULL);
2807ac6a4dd3SKuninori Morimoto 		if (!component)
2808ac6a4dd3SKuninori Morimoto 			break;
2809ac6a4dd3SKuninori Morimoto 
2810ac6a4dd3SKuninori Morimoto 		snd_soc_del_component_unlocked(component);
2811ac6a4dd3SKuninori Morimoto 	}
2812ac6a4dd3SKuninori Morimoto 	mutex_unlock(&client_mutex);
2813d191bd8dSKuninori Morimoto }
2814d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2815d191bd8dSKuninori Morimoto 
2816bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
2817b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2818bec4fa05SStephen Warren 			       const char *propname)
2819bec4fa05SStephen Warren {
2820b07609ceSKuninori Morimoto 	struct device_node *np;
2821bec4fa05SStephen Warren 	int ret;
2822bec4fa05SStephen Warren 
28237e07e7c0STushar Behera 	if (!card->dev) {
28247e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
28257e07e7c0STushar Behera 		return -EINVAL;
28267e07e7c0STushar Behera 	}
28277e07e7c0STushar Behera 
28287e07e7c0STushar Behera 	np = card->dev->of_node;
28297e07e7c0STushar Behera 
2830bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
2831bec4fa05SStephen Warren 	/*
2832bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
2833bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
2834bec4fa05SStephen Warren 	 * snd_soc_register_card.
2835bec4fa05SStephen Warren 	 */
2836bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
2837bec4fa05SStephen Warren 		dev_err(card->dev,
2838f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
2839bec4fa05SStephen Warren 			propname, ret);
2840bec4fa05SStephen Warren 		return ret;
2841bec4fa05SStephen Warren 	}
2842bec4fa05SStephen Warren 
2843bec4fa05SStephen Warren 	return 0;
2844bec4fa05SStephen Warren }
2845b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2846bec4fa05SStephen Warren 
28479a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
28489a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
28499a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
28509a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
28519a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
28529a6d4860SXiubo Li };
28539a6d4860SXiubo Li 
285421efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
28559a6d4860SXiubo Li 					  const char *propname)
28569a6d4860SXiubo Li {
285721efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
28589a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
28599a6d4860SXiubo Li 	const char *template, *wname;
28609a6d4860SXiubo Li 	int i, j, num_widgets, ret;
28619a6d4860SXiubo Li 
28629a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
28639a6d4860SXiubo Li 	if (num_widgets < 0) {
28649a6d4860SXiubo Li 		dev_err(card->dev,
28659a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
28669a6d4860SXiubo Li 		return -EINVAL;
28679a6d4860SXiubo Li 	}
28689a6d4860SXiubo Li 	if (num_widgets & 1) {
28699a6d4860SXiubo Li 		dev_err(card->dev,
28709a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
28719a6d4860SXiubo Li 		return -EINVAL;
28729a6d4860SXiubo Li 	}
28739a6d4860SXiubo Li 
28749a6d4860SXiubo Li 	num_widgets /= 2;
28759a6d4860SXiubo Li 	if (!num_widgets) {
28769a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
28779a6d4860SXiubo Li 			propname);
28789a6d4860SXiubo Li 		return -EINVAL;
28799a6d4860SXiubo Li 	}
28809a6d4860SXiubo Li 
28819a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
28829a6d4860SXiubo Li 			       GFP_KERNEL);
28839a6d4860SXiubo Li 	if (!widgets) {
28849a6d4860SXiubo Li 		dev_err(card->dev,
28859a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
28869a6d4860SXiubo Li 		return -ENOMEM;
28879a6d4860SXiubo Li 	}
28889a6d4860SXiubo Li 
28899a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
28909a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
28919a6d4860SXiubo Li 			2 * i, &template);
28929a6d4860SXiubo Li 		if (ret) {
28939a6d4860SXiubo Li 			dev_err(card->dev,
28949a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
28959a6d4860SXiubo Li 				propname, 2 * i, ret);
28969a6d4860SXiubo Li 			return -EINVAL;
28979a6d4860SXiubo Li 		}
28989a6d4860SXiubo Li 
28999a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
29009a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
29019a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
29029a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
29039a6d4860SXiubo Li 				break;
29049a6d4860SXiubo Li 			}
29059a6d4860SXiubo Li 		}
29069a6d4860SXiubo Li 
29079a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
29089a6d4860SXiubo Li 			dev_err(card->dev,
29099a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
29109a6d4860SXiubo Li 				template);
29119a6d4860SXiubo Li 			return -EINVAL;
29129a6d4860SXiubo Li 		}
29139a6d4860SXiubo Li 
29149a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
29159a6d4860SXiubo Li 						    (2 * i) + 1,
29169a6d4860SXiubo Li 						    &wname);
29179a6d4860SXiubo Li 		if (ret) {
29189a6d4860SXiubo Li 			dev_err(card->dev,
29199a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
29209a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
29219a6d4860SXiubo Li 			return -EINVAL;
29229a6d4860SXiubo Li 		}
29239a6d4860SXiubo Li 
29249a6d4860SXiubo Li 		widgets[i].name = wname;
29259a6d4860SXiubo Li 	}
29269a6d4860SXiubo Li 
2927f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
2928f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
29299a6d4860SXiubo Li 
29309a6d4860SXiubo Li 	return 0;
29319a6d4860SXiubo Li }
293221efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
29339a6d4860SXiubo Li 
2934cbdfab3bSJerome Brunet int snd_soc_of_get_slot_mask(struct device_node *np,
29356131084aSJyri Sarha 			     const char *prop_name,
29366131084aSJyri Sarha 			     unsigned int *mask)
29376131084aSJyri Sarha {
29386131084aSJyri Sarha 	u32 val;
29396c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
29406131084aSJyri Sarha 	int i;
29416131084aSJyri Sarha 
29426131084aSJyri Sarha 	if (!of_slot_mask)
29436131084aSJyri Sarha 		return 0;
29446131084aSJyri Sarha 	val /= sizeof(u32);
29456131084aSJyri Sarha 	for (i = 0; i < val; i++)
29466131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
29476131084aSJyri Sarha 			*mask |= (1 << i);
29486131084aSJyri Sarha 
29496131084aSJyri Sarha 	return val;
29506131084aSJyri Sarha }
2951cbdfab3bSJerome Brunet EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
29526131084aSJyri Sarha 
295389c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
29546131084aSJyri Sarha 			      unsigned int *tx_mask,
29556131084aSJyri Sarha 			      unsigned int *rx_mask,
295689c67857SXiubo Li 			      unsigned int *slots,
295789c67857SXiubo Li 			      unsigned int *slot_width)
295889c67857SXiubo Li {
295989c67857SXiubo Li 	u32 val;
296089c67857SXiubo Li 	int ret;
296189c67857SXiubo Li 
29626131084aSJyri Sarha 	if (tx_mask)
29636131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
29646131084aSJyri Sarha 	if (rx_mask)
29656131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
29666131084aSJyri Sarha 
296789c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
296889c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
296989c67857SXiubo Li 		if (ret)
297089c67857SXiubo Li 			return ret;
297189c67857SXiubo Li 
297289c67857SXiubo Li 		if (slots)
297389c67857SXiubo Li 			*slots = val;
297489c67857SXiubo Li 	}
297589c67857SXiubo Li 
297689c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
297789c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
297889c67857SXiubo Li 		if (ret)
297989c67857SXiubo Li 			return ret;
298089c67857SXiubo Li 
298189c67857SXiubo Li 		if (slot_width)
298289c67857SXiubo Li 			*slot_width = val;
298389c67857SXiubo Li 	}
298489c67857SXiubo Li 
298589c67857SXiubo Li 	return 0;
298689c67857SXiubo Li }
298789c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
298889c67857SXiubo Li 
29893b710356SKuninori Morimoto void snd_soc_of_parse_node_prefix(struct device_node *np,
29905e3cdaa2SKuninori Morimoto 				  struct snd_soc_codec_conf *codec_conf,
29915e3cdaa2SKuninori Morimoto 				  struct device_node *of_node,
29925e3cdaa2SKuninori Morimoto 				  const char *propname)
29935e3cdaa2SKuninori Morimoto {
29945e3cdaa2SKuninori Morimoto 	const char *str;
29955e3cdaa2SKuninori Morimoto 	int ret;
29965e3cdaa2SKuninori Morimoto 
29975e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
29985e3cdaa2SKuninori Morimoto 	if (ret < 0) {
29995e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
30005e3cdaa2SKuninori Morimoto 		return;
30015e3cdaa2SKuninori Morimoto 	}
30025e3cdaa2SKuninori Morimoto 
30035e3cdaa2SKuninori Morimoto 	codec_conf->of_node	= of_node;
30045e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
30055e3cdaa2SKuninori Morimoto }
30063b710356SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
30075e3cdaa2SKuninori Morimoto 
30082bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3009a4a54dd5SStephen Warren 				   const char *propname)
3010a4a54dd5SStephen Warren {
30112bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
3012e3b1e6a1SMark Brown 	int num_routes;
3013a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3014a4a54dd5SStephen Warren 	int i, ret;
3015a4a54dd5SStephen Warren 
3016a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3017c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
301810e8aa9aSMichał Mirosław 		dev_err(card->dev,
301910e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
302010e8aa9aSMichał Mirosław 			propname);
3021a4a54dd5SStephen Warren 		return -EINVAL;
3022a4a54dd5SStephen Warren 	}
3023a4a54dd5SStephen Warren 	num_routes /= 2;
3024a4a54dd5SStephen Warren 	if (!num_routes) {
3025f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3026a4a54dd5SStephen Warren 			propname);
3027a4a54dd5SStephen Warren 		return -EINVAL;
3028a4a54dd5SStephen Warren 	}
3029a4a54dd5SStephen Warren 
3030a86854d0SKees Cook 	routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
3031a4a54dd5SStephen Warren 			      GFP_KERNEL);
3032a4a54dd5SStephen Warren 	if (!routes) {
3033a4a54dd5SStephen Warren 		dev_err(card->dev,
3034f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3035a4a54dd5SStephen Warren 		return -EINVAL;
3036a4a54dd5SStephen Warren 	}
3037a4a54dd5SStephen Warren 
3038a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3039a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3040e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
3041a4a54dd5SStephen Warren 		if (ret) {
3042c871bd0bSMark Brown 			dev_err(card->dev,
3043c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3044c871bd0bSMark Brown 				propname, 2 * i, ret);
3045a4a54dd5SStephen Warren 			return -EINVAL;
3046a4a54dd5SStephen Warren 		}
3047a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3048e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3049a4a54dd5SStephen Warren 		if (ret) {
3050a4a54dd5SStephen Warren 			dev_err(card->dev,
3051c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3052c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3053a4a54dd5SStephen Warren 			return -EINVAL;
3054a4a54dd5SStephen Warren 		}
3055a4a54dd5SStephen Warren 	}
3056a4a54dd5SStephen Warren 
3057f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
3058f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
3059a4a54dd5SStephen Warren 
3060a4a54dd5SStephen Warren 	return 0;
3061a4a54dd5SStephen Warren }
30622bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3063a4a54dd5SStephen Warren 
3064a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3065389cb834SJyri Sarha 				     const char *prefix,
3066389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3067389cb834SJyri Sarha 				     struct device_node **framemaster)
3068a7930ed4SKuninori Morimoto {
3069a7930ed4SKuninori Morimoto 	int ret, i;
3070a7930ed4SKuninori Morimoto 	char prop[128];
3071a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3072a7930ed4SKuninori Morimoto 	int bit, frame;
3073a7930ed4SKuninori Morimoto 	const char *str;
3074a7930ed4SKuninori Morimoto 	struct {
3075a7930ed4SKuninori Morimoto 		char *name;
3076a7930ed4SKuninori Morimoto 		unsigned int val;
3077a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3078a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3079a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3080a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3081a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3082a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3083a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3084a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3085a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3086a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3087a7930ed4SKuninori Morimoto 	};
3088a7930ed4SKuninori Morimoto 
3089a7930ed4SKuninori Morimoto 	if (!prefix)
3090a7930ed4SKuninori Morimoto 		prefix = "";
3091a7930ed4SKuninori Morimoto 
3092a7930ed4SKuninori Morimoto 	/*
30935711c979SKuninori Morimoto 	 * check "dai-format = xxx"
30945711c979SKuninori Morimoto 	 * or    "[prefix]format = xxx"
3095a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3096a7930ed4SKuninori Morimoto 	 */
30975711c979SKuninori Morimoto 	ret = of_property_read_string(np, "dai-format", &str);
30985711c979SKuninori Morimoto 	if (ret < 0) {
3099a7930ed4SKuninori Morimoto 		snprintf(prop, sizeof(prop), "%sformat", prefix);
3100a7930ed4SKuninori Morimoto 		ret = of_property_read_string(np, prop, &str);
31015711c979SKuninori Morimoto 	}
3102a7930ed4SKuninori Morimoto 	if (ret == 0) {
3103a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3104a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3105a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3106a7930ed4SKuninori Morimoto 				break;
3107a7930ed4SKuninori Morimoto 			}
3108a7930ed4SKuninori Morimoto 		}
3109a7930ed4SKuninori Morimoto 	}
3110a7930ed4SKuninori Morimoto 
3111a7930ed4SKuninori Morimoto 	/*
31128c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3113a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3114a7930ed4SKuninori Morimoto 	 */
31158c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
311651930295SJulia Lawall 	if (of_property_read_bool(np, prop))
31178c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
31188c2d6a9fSKuninori Morimoto 	else
31198c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3120a7930ed4SKuninori Morimoto 
3121a7930ed4SKuninori Morimoto 	/*
3122a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3123a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3124a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3125a7930ed4SKuninori Morimoto 	 */
3126a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3127a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3128a7930ed4SKuninori Morimoto 
3129a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3130a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3131a7930ed4SKuninori Morimoto 
3132a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3133a7930ed4SKuninori Morimoto 	case 0x11:
3134a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3135a7930ed4SKuninori Morimoto 		break;
3136a7930ed4SKuninori Morimoto 	case 0x10:
3137a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3138a7930ed4SKuninori Morimoto 		break;
3139a7930ed4SKuninori Morimoto 	case 0x01:
3140a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3141a7930ed4SKuninori Morimoto 		break;
3142a7930ed4SKuninori Morimoto 	default:
3143a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3144a7930ed4SKuninori Morimoto 		break;
3145a7930ed4SKuninori Morimoto 	}
3146a7930ed4SKuninori Morimoto 
3147a7930ed4SKuninori Morimoto 	/*
3148a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3149a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3150a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3151a7930ed4SKuninori Morimoto 	 */
3152a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3153a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3154389cb834SJyri Sarha 	if (bit && bitclkmaster)
3155389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3156a7930ed4SKuninori Morimoto 
3157a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3158a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3159389cb834SJyri Sarha 	if (frame && framemaster)
3160389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3161a7930ed4SKuninori Morimoto 
3162a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3163a7930ed4SKuninori Morimoto 	case 0x11:
3164a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3165a7930ed4SKuninori Morimoto 		break;
3166a7930ed4SKuninori Morimoto 	case 0x10:
3167a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3168a7930ed4SKuninori Morimoto 		break;
3169a7930ed4SKuninori Morimoto 	case 0x01:
3170a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3171a7930ed4SKuninori Morimoto 		break;
3172a7930ed4SKuninori Morimoto 	default:
3173a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3174a7930ed4SKuninori Morimoto 		break;
3175a7930ed4SKuninori Morimoto 	}
3176a7930ed4SKuninori Morimoto 
3177a7930ed4SKuninori Morimoto 	return format;
3178a7930ed4SKuninori Morimoto }
3179a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3180a7930ed4SKuninori Morimoto 
3181a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
3182a180e8b9SKuninori Morimoto {
318309d4cc03SKuninori Morimoto 	struct snd_soc_component *component;
3184c1e230f0SKuninori Morimoto 	struct snd_soc_dai_link_component dlc;
3185a180e8b9SKuninori Morimoto 	int ret;
3186a180e8b9SKuninori Morimoto 
3187c1e230f0SKuninori Morimoto 	dlc.of_node	= of_graph_get_port_parent(ep);
3188c1e230f0SKuninori Morimoto 	dlc.name	= NULL;
3189a180e8b9SKuninori Morimoto 	/*
3190a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
3191a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
3192a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
3193a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
3194a180e8b9SKuninori Morimoto 	 */
3195a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
3196a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
3197c1e230f0SKuninori Morimoto 	component = soc_find_component(&dlc);
31982c7b1704SKuninori Morimoto 	if (component)
31992c7b1704SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_id(component, ep);
3200a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
3201a180e8b9SKuninori Morimoto 
3202c1e230f0SKuninori Morimoto 	of_node_put(dlc.of_node);
3203c0a480d1STony Lindgren 
3204a180e8b9SKuninori Morimoto 	return ret;
3205a180e8b9SKuninori Morimoto }
3206a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3207a180e8b9SKuninori Morimoto 
32081ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args,
3209cb470087SKuninori Morimoto 				const char **dai_name)
3210cb470087SKuninori Morimoto {
3211cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
32123e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
321393b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3214cb470087SKuninori Morimoto 
3215cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3216368dee94SKuninori Morimoto 	for_each_component(pos) {
3217c0834440SKuninori Morimoto 		component_of_node = soc_component_to_node(pos);
32183e0aa8d8SJyri Sarha 
32193e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
3220cb470087SKuninori Morimoto 			continue;
3221cb470087SKuninori Morimoto 
3222a2a34175SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3223a2a34175SKuninori Morimoto 		if (ret == -ENOTSUPP) {
322458bf4179SKuninori Morimoto 			struct snd_soc_dai *dai;
32256833c452SKuninori Morimoto 			int id = -1;
32266833c452SKuninori Morimoto 
322793b0f3eeSJean-Francois Moine 			switch (args->args_count) {
32286833c452SKuninori Morimoto 			case 0:
32296833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
32306833c452SKuninori Morimoto 				break;
32316833c452SKuninori Morimoto 			case 1:
323293b0f3eeSJean-Francois Moine 				id = args->args[0];
32336833c452SKuninori Morimoto 				break;
32346833c452SKuninori Morimoto 			default:
32356833c452SKuninori Morimoto 				/* not supported */
3236cb470087SKuninori Morimoto 				break;
3237cb470087SKuninori Morimoto 			}
3238cb470087SKuninori Morimoto 
32396833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
32406833c452SKuninori Morimoto 				ret = -EINVAL;
32413dcba280SNicolin Chen 				continue;
32426833c452SKuninori Morimoto 			}
3243e41975edSXiubo Li 
3244e41975edSXiubo Li 			ret = 0;
3245e41975edSXiubo Li 
324658bf4179SKuninori Morimoto 			/* find target DAI */
324715a0c645SKuninori Morimoto 			for_each_component_dais(pos, dai) {
324858bf4179SKuninori Morimoto 				if (id == 0)
324958bf4179SKuninori Morimoto 					break;
325058bf4179SKuninori Morimoto 				id--;
325158bf4179SKuninori Morimoto 			}
325258bf4179SKuninori Morimoto 
325358bf4179SKuninori Morimoto 			*dai_name = dai->driver->name;
3254e41975edSXiubo Li 			if (!*dai_name)
3255e41975edSXiubo Li 				*dai_name = pos->name;
32566833c452SKuninori Morimoto 		}
32576833c452SKuninori Morimoto 
3258cb470087SKuninori Morimoto 		break;
3259cb470087SKuninori Morimoto 	}
3260cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
326193b0f3eeSJean-Francois Moine 	return ret;
326293b0f3eeSJean-Francois Moine }
32631ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
326493b0f3eeSJean-Francois Moine 
326593b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
326693b0f3eeSJean-Francois Moine 			    const char **dai_name)
326793b0f3eeSJean-Francois Moine {
326893b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
326993b0f3eeSJean-Francois Moine 	int ret;
327093b0f3eeSJean-Francois Moine 
327193b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
327293b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
327393b0f3eeSJean-Francois Moine 	if (ret)
327493b0f3eeSJean-Francois Moine 		return ret;
327593b0f3eeSJean-Francois Moine 
327693b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3277cb470087SKuninori Morimoto 
3278cb470087SKuninori Morimoto 	of_node_put(args.np);
3279cb470087SKuninori Morimoto 
3280cb470087SKuninori Morimoto 	return ret;
3281cb470087SKuninori Morimoto }
3282cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3283cb470087SKuninori Morimoto 
328493b0f3eeSJean-Francois Moine /*
328594685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
328694685763SSylwester Nawrocki  * @dai_link: DAI link
328794685763SSylwester Nawrocki  *
328894685763SSylwester Nawrocki  * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
328994685763SSylwester Nawrocki  */
329094685763SSylwester Nawrocki void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
329194685763SSylwester Nawrocki {
32923db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *component;
329394685763SSylwester Nawrocki 	int index;
329494685763SSylwester Nawrocki 
32953db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
329694685763SSylwester Nawrocki 		if (!component->of_node)
329794685763SSylwester Nawrocki 			break;
329894685763SSylwester Nawrocki 		of_node_put(component->of_node);
329994685763SSylwester Nawrocki 		component->of_node = NULL;
330094685763SSylwester Nawrocki 	}
330194685763SSylwester Nawrocki }
330294685763SSylwester Nawrocki EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
330394685763SSylwester Nawrocki 
330494685763SSylwester Nawrocki /*
330593b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
330693b0f3eeSJean-Francois Moine  * @dev: Card device
330793b0f3eeSJean-Francois Moine  * @of_node: Device node
330893b0f3eeSJean-Francois Moine  * @dai_link: DAI link
330993b0f3eeSJean-Francois Moine  *
331093b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
331193b0f3eeSJean-Francois Moine  * 'sound-dai'.
331293b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
331394685763SSylwester Nawrocki  * The device nodes in the array (of_node) must be dereferenced by calling
331494685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs() on @dai_link.
331593b0f3eeSJean-Francois Moine  *
331693b0f3eeSJean-Francois Moine  * Returns 0 for success
331793b0f3eeSJean-Francois Moine  */
331893b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
331993b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
332093b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
332193b0f3eeSJean-Francois Moine {
332293b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
332393b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
332493b0f3eeSJean-Francois Moine 	char *name;
332593b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
332693b0f3eeSJean-Francois Moine 
332793b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
332893b0f3eeSJean-Francois Moine 	name = "sound-dai";
332993b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
333093b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
333193b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
333293b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
333393b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
333493b0f3eeSJean-Francois Moine 		else
333593b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
333693b0f3eeSJean-Francois Moine 		return num_codecs;
333793b0f3eeSJean-Francois Moine 	}
3338a86854d0SKees Cook 	component = devm_kcalloc(dev,
3339a86854d0SKees Cook 				 num_codecs, sizeof(*component),
334093b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
334193b0f3eeSJean-Francois Moine 	if (!component)
334293b0f3eeSJean-Francois Moine 		return -ENOMEM;
334393b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
334493b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
334593b0f3eeSJean-Francois Moine 
334693b0f3eeSJean-Francois Moine 	/* Parse the list */
33473db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
334893b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
334993b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
335093b0f3eeSJean-Francois Moine 						 index, &args);
335193b0f3eeSJean-Francois Moine 		if (ret)
335293b0f3eeSJean-Francois Moine 			goto err;
335393b0f3eeSJean-Francois Moine 		component->of_node = args.np;
335493b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
335593b0f3eeSJean-Francois Moine 		if (ret < 0)
335693b0f3eeSJean-Francois Moine 			goto err;
335793b0f3eeSJean-Francois Moine 	}
335893b0f3eeSJean-Francois Moine 	return 0;
335993b0f3eeSJean-Francois Moine err:
336094685763SSylwester Nawrocki 	snd_soc_of_put_dai_link_codecs(dai_link);
336193b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
336293b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
336393b0f3eeSJean-Francois Moine 	return ret;
336493b0f3eeSJean-Francois Moine }
336593b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
336693b0f3eeSJean-Francois Moine 
3367c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3368db2a4165SFrank Mandarino {
33696553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
3370fb257897SMark Brown 	snd_soc_util_init();
3371fb257897SMark Brown 
3372db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3373db2a4165SFrank Mandarino }
33744abe8e16SMark Brown module_init(snd_soc_init);
3375db2a4165SFrank Mandarino 
33767d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3377db2a4165SFrank Mandarino {
3378fb257897SMark Brown 	snd_soc_util_exit();
33796553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3380fb257897SMark Brown 
3381db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3382db2a4165SFrank Mandarino }
3383db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3384db2a4165SFrank Mandarino 
3385db2a4165SFrank Mandarino /* Module information */
3386d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3387db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3388db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
33898b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3390