xref: /openbmc/linux/sound/soc/soc-core.c (revision f09b6e96796056633453cb0d07b720d09f1efc68)
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>
347de14d58SJon Hunter #include <linux/acpi.h>
35db2a4165SFrank Mandarino #include <sound/core.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>
4102e75636SKuninori Morimoto #include <sound/soc-link.h>
42db2a4165SFrank Mandarino #include <sound/initval.h>
43db2a4165SFrank Mandarino 
44a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
45a8b1d34fSMark Brown #include <trace/events/asoc.h>
46a8b1d34fSMark Brown 
47c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
48030e79f6SKuninori Morimoto static LIST_HEAD(component_list);
49e894efefSSrinivas Kandagatla static LIST_HEAD(unbind_card_list);
50c5af3a2eSMark Brown 
51368dee94SKuninori Morimoto #define for_each_component(component)			\
52368dee94SKuninori Morimoto 	list_for_each_entry(component, &component_list, list)
53368dee94SKuninori Morimoto 
54db2a4165SFrank Mandarino /*
55587c9844SKuninori Morimoto  * This is used if driver don't need to have CPU/Codec/Platform
56587c9844SKuninori Morimoto  * dai_link. see soc.h
57587c9844SKuninori Morimoto  */
58587c9844SKuninori Morimoto struct snd_soc_dai_link_component null_dailink_component[0];
59587c9844SKuninori Morimoto EXPORT_SYMBOL_GPL(null_dailink_component);
60587c9844SKuninori Morimoto 
61587c9844SKuninori Morimoto /*
62db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
63db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
64db2a4165SFrank Mandarino  * between two audio tracks.
65db2a4165SFrank Mandarino  */
66db2a4165SFrank Mandarino static int pmdown_time = 5000;
67db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
68db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
69db2a4165SFrank Mandarino 
70dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
71dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
72dbe21408SMark Brown {
7336ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
74dbe21408SMark Brown 
75628d0f72STakashi Iwai 	return sysfs_emit(buf, "%ld\n", rtd->pmdown_time);
76dbe21408SMark Brown }
77dbe21408SMark Brown 
785a3f869cSYueHaibing static ssize_t pmdown_time_store(struct device *dev,
79dbe21408SMark Brown 				 struct device_attribute *attr,
80dbe21408SMark Brown 				 const char *buf, size_t count)
81dbe21408SMark Brown {
8236ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
83c593b520SMark Brown 	int ret;
84dbe21408SMark Brown 
85b785a492SJingoo Han 	ret = kstrtol(buf, 10, &rtd->pmdown_time);
86c593b520SMark Brown 	if (ret)
87c593b520SMark Brown 		return ret;
88dbe21408SMark Brown 
89dbe21408SMark Brown 	return count;
90dbe21408SMark Brown }
91dbe21408SMark Brown 
925a3f869cSYueHaibing static DEVICE_ATTR_RW(pmdown_time);
93dbe21408SMark Brown 
94d29697dcSTakashi Iwai static struct attribute *soc_dev_attrs[] = {
95d29697dcSTakashi Iwai 	&dev_attr_pmdown_time.attr,
96d29697dcSTakashi Iwai 	NULL
97d29697dcSTakashi Iwai };
98d29697dcSTakashi Iwai 
99d29697dcSTakashi Iwai static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
100d29697dcSTakashi Iwai 				       struct attribute *attr, int idx)
101d29697dcSTakashi Iwai {
102d29697dcSTakashi Iwai 	struct device *dev = kobj_to_dev(kobj);
103d29697dcSTakashi Iwai 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
104d29697dcSTakashi Iwai 
105d918a376SKuninori Morimoto 	if (!rtd)
106d918a376SKuninori Morimoto 		return 0;
107d918a376SKuninori Morimoto 
108d29697dcSTakashi Iwai 	if (attr == &dev_attr_pmdown_time.attr)
109d29697dcSTakashi Iwai 		return attr->mode; /* always visible */
1103989ade2SKuninori Morimoto 	return rtd->dai_link->num_codecs ? attr->mode : 0; /* enabled only with codec */
111d29697dcSTakashi Iwai }
112d29697dcSTakashi Iwai 
113d29697dcSTakashi Iwai static const struct attribute_group soc_dapm_dev_group = {
114d29697dcSTakashi Iwai 	.attrs = soc_dapm_dev_attrs,
115d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
116d29697dcSTakashi Iwai };
117d29697dcSTakashi Iwai 
118f7e73b26SMark Brown static const struct attribute_group soc_dev_group = {
119d29697dcSTakashi Iwai 	.attrs = soc_dev_attrs,
120d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
121d29697dcSTakashi Iwai };
122d29697dcSTakashi Iwai 
123d29697dcSTakashi Iwai static const struct attribute_group *soc_dev_attr_groups[] = {
124d29697dcSTakashi Iwai 	&soc_dapm_dev_group,
125f7e73b26SMark Brown 	&soc_dev_group,
126d29697dcSTakashi Iwai 	NULL
127d29697dcSTakashi Iwai };
128d29697dcSTakashi Iwai 
1292624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
130a4072cdfSKuninori Morimoto struct dentry *snd_soc_debugfs_root;
131a4072cdfSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
132a4072cdfSKuninori Morimoto 
13381c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component)
134e73f3de5SRussell King {
1356553bf06SLars-Peter Clausen 	if (!component->card->debugfs_card_root)
1366553bf06SLars-Peter Clausen 		return;
1376553bf06SLars-Peter Clausen 
13881c7cfd1SLars-Peter Clausen 	if (component->debugfs_prefix) {
13981c7cfd1SLars-Peter Clausen 		char *name;
140e73f3de5SRussell King 
14181c7cfd1SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s:%s",
14281c7cfd1SLars-Peter Clausen 			component->debugfs_prefix, component->name);
14381c7cfd1SLars-Peter Clausen 		if (name) {
14481c7cfd1SLars-Peter Clausen 			component->debugfs_root = debugfs_create_dir(name,
14581c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
14681c7cfd1SLars-Peter Clausen 			kfree(name);
14781c7cfd1SLars-Peter Clausen 		}
14881c7cfd1SLars-Peter Clausen 	} else {
14981c7cfd1SLars-Peter Clausen 		component->debugfs_root = debugfs_create_dir(component->name,
15081c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
151e73f3de5SRussell King 	}
152e73f3de5SRussell King 
15381c7cfd1SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
15481c7cfd1SLars-Peter Clausen 		component->debugfs_root);
15581c7cfd1SLars-Peter Clausen }
15681c7cfd1SLars-Peter Clausen 
15781c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
15881c7cfd1SLars-Peter Clausen {
159ad64bfbdSKuninori Morimoto 	if (!component->debugfs_root)
160ad64bfbdSKuninori Morimoto 		return;
16181c7cfd1SLars-Peter Clausen 	debugfs_remove_recursive(component->debugfs_root);
162ad64bfbdSKuninori Morimoto 	component->debugfs_root = NULL;
16381c7cfd1SLars-Peter Clausen }
16481c7cfd1SLars-Peter Clausen 
165c15b2a1dSPeng Donglin static int dai_list_show(struct seq_file *m, void *v)
166f3208780SMark Brown {
1671438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
168f3208780SMark Brown 	struct snd_soc_dai *dai;
169f3208780SMark Brown 
17034e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
17134e81ab4SLars-Peter Clausen 
172368dee94SKuninori Morimoto 	for_each_component(component)
17315a0c645SKuninori Morimoto 		for_each_component_dais(component, dai)
174700c17caSDonglin Peng 			seq_printf(m, "%s\n", dai->name);
175f3208780SMark Brown 
17634e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
17734e81ab4SLars-Peter Clausen 
178700c17caSDonglin Peng 	return 0;
179700c17caSDonglin Peng }
180c15b2a1dSPeng Donglin DEFINE_SHOW_ATTRIBUTE(dai_list);
181f3208780SMark Brown 
182db795f9bSKuninori Morimoto static int component_list_show(struct seq_file *m, void *v)
183db795f9bSKuninori Morimoto {
184db795f9bSKuninori Morimoto 	struct snd_soc_component *component;
185db795f9bSKuninori Morimoto 
186db795f9bSKuninori Morimoto 	mutex_lock(&client_mutex);
187db795f9bSKuninori Morimoto 
188368dee94SKuninori Morimoto 	for_each_component(component)
189db795f9bSKuninori Morimoto 		seq_printf(m, "%s\n", component->name);
190db795f9bSKuninori Morimoto 
191db795f9bSKuninori Morimoto 	mutex_unlock(&client_mutex);
192db795f9bSKuninori Morimoto 
193db795f9bSKuninori Morimoto 	return 0;
194db795f9bSKuninori Morimoto }
195db795f9bSKuninori Morimoto DEFINE_SHOW_ATTRIBUTE(component_list);
196db795f9bSKuninori Morimoto 
197a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
198a6052154SJarkko Nikula {
199a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
2008a9dab1aSMark Brown 						     snd_soc_debugfs_root);
2013a45b867SJarkko Nikula 
202fee531d6SGreg Kroah-Hartman 	debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root,
2033a45b867SJarkko Nikula 			   &card->pop_time);
204d8ca7a0aSKuninori Morimoto 
205d8ca7a0aSKuninori Morimoto 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
206a6052154SJarkko Nikula }
207a6052154SJarkko Nikula 
208a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
209a6052154SJarkko Nikula {
210a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
21129040d1aSKuninori Morimoto 	card->debugfs_card_root = NULL;
212a6052154SJarkko Nikula }
213a6052154SJarkko Nikula 
2146553bf06SLars-Peter Clausen static void snd_soc_debugfs_init(void)
2156553bf06SLars-Peter Clausen {
2166553bf06SLars-Peter Clausen 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
2176553bf06SLars-Peter Clausen 
218fee531d6SGreg Kroah-Hartman 	debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
219fee531d6SGreg Kroah-Hartman 			    &dai_list_fops);
220db795f9bSKuninori Morimoto 
221fee531d6SGreg Kroah-Hartman 	debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
222fee531d6SGreg Kroah-Hartman 			    &component_list_fops);
2236553bf06SLars-Peter Clausen }
2246553bf06SLars-Peter Clausen 
2256553bf06SLars-Peter Clausen static void snd_soc_debugfs_exit(void)
2266553bf06SLars-Peter Clausen {
2276553bf06SLars-Peter Clausen 	debugfs_remove_recursive(snd_soc_debugfs_root);
2286553bf06SLars-Peter Clausen }
2296553bf06SLars-Peter Clausen 
2302624d5faSMark Brown #else
2312624d5faSMark Brown 
23241b1774fSKuninori Morimoto static inline void soc_init_component_debugfs(struct snd_soc_component *component) { }
23341b1774fSKuninori Morimoto static inline void soc_cleanup_component_debugfs(struct snd_soc_component *component) { }
23441b1774fSKuninori Morimoto static inline void soc_init_card_debugfs(struct snd_soc_card *card) { }
23541b1774fSKuninori Morimoto static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card) { }
23641b1774fSKuninori Morimoto static inline void snd_soc_debugfs_init(void) { }
23741b1774fSKuninori Morimoto static inline void snd_soc_debugfs_exit(void) { }
2386553bf06SLars-Peter Clausen 
2392624d5faSMark Brown #endif
2402624d5faSMark Brown 
24112b05232SKuninori Morimoto static int snd_soc_rtd_add_component(struct snd_soc_pcm_runtime *rtd,
242a0ac4411SKuninori Morimoto 				     struct snd_soc_component *component)
243a0ac4411SKuninori Morimoto {
2442b544dd7SKuninori Morimoto 	struct snd_soc_component *comp;
245613fb500SKuninori Morimoto 	int i;
246a0ac4411SKuninori Morimoto 
247613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, comp) {
248a0ac4411SKuninori Morimoto 		/* already connected */
2492b544dd7SKuninori Morimoto 		if (comp == component)
250a0ac4411SKuninori Morimoto 			return 0;
251a0ac4411SKuninori Morimoto 	}
252a0ac4411SKuninori Morimoto 
253613fb500SKuninori Morimoto 	/* see for_each_rtd_components */
254613fb500SKuninori Morimoto 	rtd->components[rtd->num_components] = component;
255613fb500SKuninori Morimoto 	rtd->num_components++;
256a0ac4411SKuninori Morimoto 
257a0ac4411SKuninori Morimoto 	return 0;
258a0ac4411SKuninori Morimoto }
259a0ac4411SKuninori Morimoto 
260a0ac4411SKuninori Morimoto struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
261a0ac4411SKuninori Morimoto 						const char *driver_name)
262a0ac4411SKuninori Morimoto {
2632b544dd7SKuninori Morimoto 	struct snd_soc_component *component;
264613fb500SKuninori Morimoto 	int i;
265a0ac4411SKuninori Morimoto 
266971da24cSKuninori Morimoto 	if (!driver_name)
267971da24cSKuninori Morimoto 		return NULL;
268971da24cSKuninori Morimoto 
269a33c0d16SKuninori Morimoto 	/*
270a33c0d16SKuninori Morimoto 	 * NOTE
271a33c0d16SKuninori Morimoto 	 *
272a33c0d16SKuninori Morimoto 	 * snd_soc_rtdcom_lookup() will find component from rtd by using
273a33c0d16SKuninori Morimoto 	 * specified driver name.
274a33c0d16SKuninori Morimoto 	 * But, if many components which have same driver name are connected
275a33c0d16SKuninori Morimoto 	 * to 1 rtd, this function will return 1st found component.
276a33c0d16SKuninori Morimoto 	 */
277613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
2782b544dd7SKuninori Morimoto 		const char *component_name = component->driver->name;
279971da24cSKuninori Morimoto 
280971da24cSKuninori Morimoto 		if (!component_name)
281971da24cSKuninori Morimoto 			continue;
282971da24cSKuninori Morimoto 
283971da24cSKuninori Morimoto 		if ((component_name == driver_name) ||
284971da24cSKuninori Morimoto 		    strcmp(component_name, driver_name) == 0)
285613fb500SKuninori Morimoto 			return component;
286a0ac4411SKuninori Morimoto 	}
287a0ac4411SKuninori Morimoto 
288a0ac4411SKuninori Morimoto 	return NULL;
289a0ac4411SKuninori Morimoto }
290031734b7SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
291a0ac4411SKuninori Morimoto 
2926fbea6b6SShengjiu Wang struct snd_soc_component
29318dd66eaSKuninori Morimoto *snd_soc_lookup_component_nolocked(struct device *dev, const char *driver_name)
294b8132657SKuninori Morimoto {
295b8132657SKuninori Morimoto 	struct snd_soc_component *component;
2965bd7e08bSKuninori Morimoto 	struct snd_soc_component *found_component;
297b8132657SKuninori Morimoto 
2985bd7e08bSKuninori Morimoto 	found_component = NULL;
299b8132657SKuninori Morimoto 	for_each_component(component) {
3005bd7e08bSKuninori Morimoto 		if ((dev == component->dev) &&
3015bd7e08bSKuninori Morimoto 		    (!driver_name ||
3025bd7e08bSKuninori Morimoto 		     (driver_name == component->driver->name) ||
3035bd7e08bSKuninori Morimoto 		     (strcmp(component->driver->name, driver_name) == 0))) {
3045bd7e08bSKuninori Morimoto 			found_component = component;
305b8132657SKuninori Morimoto 			break;
306b8132657SKuninori Morimoto 		}
3075bd7e08bSKuninori Morimoto 	}
308b8132657SKuninori Morimoto 
3095bd7e08bSKuninori Morimoto 	return found_component;
310b8132657SKuninori Morimoto }
3116fbea6b6SShengjiu Wang EXPORT_SYMBOL_GPL(snd_soc_lookup_component_nolocked);
31218dd66eaSKuninori Morimoto 
31318dd66eaSKuninori Morimoto struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
31418dd66eaSKuninori Morimoto 						   const char *driver_name)
31518dd66eaSKuninori Morimoto {
31618dd66eaSKuninori Morimoto 	struct snd_soc_component *component;
31718dd66eaSKuninori Morimoto 
31818dd66eaSKuninori Morimoto 	mutex_lock(&client_mutex);
31918dd66eaSKuninori Morimoto 	component = snd_soc_lookup_component_nolocked(dev, driver_name);
32018dd66eaSKuninori Morimoto 	mutex_unlock(&client_mutex);
32118dd66eaSKuninori Morimoto 
32218dd66eaSKuninori Morimoto 	return component;
32318dd66eaSKuninori Morimoto }
324b8132657SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
325b8132657SKuninori Morimoto 
32694def8eaSKuninori Morimoto struct snd_soc_pcm_runtime
32794def8eaSKuninori Morimoto *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
3284468189fSKuninori Morimoto 			 struct snd_soc_dai_link *dai_link)
32994def8eaSKuninori Morimoto {
33094def8eaSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
33194def8eaSKuninori Morimoto 
33294def8eaSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
3334468189fSKuninori Morimoto 		if (rtd->dai_link == dai_link)
33494def8eaSKuninori Morimoto 			return rtd;
33594def8eaSKuninori Morimoto 	}
3364468189fSKuninori Morimoto 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link->name);
33794def8eaSKuninori Morimoto 	return NULL;
33894def8eaSKuninori Morimoto }
33994def8eaSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
34094def8eaSKuninori Morimoto 
34183f94a2eSKuninori Morimoto /*
34283f94a2eSKuninori Morimoto  * Power down the audio subsystem pmdown_time msecs after close is called.
34383f94a2eSKuninori Morimoto  * This is to ensure there are no pops or clicks in between any music tracks
34483f94a2eSKuninori Morimoto  * due to DAPM power cycling.
34583f94a2eSKuninori Morimoto  */
34683f94a2eSKuninori Morimoto void snd_soc_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
34783f94a2eSKuninori Morimoto {
348c2233a26SKuninori Morimoto 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
3490f6011fdSKuninori Morimoto 	int playback = SNDRV_PCM_STREAM_PLAYBACK;
35083f94a2eSKuninori Morimoto 
35138e42f6dSKuninori Morimoto 	snd_soc_dpcm_mutex_lock(rtd);
35283f94a2eSKuninori Morimoto 
35383f94a2eSKuninori Morimoto 	dev_dbg(rtd->dev,
35483f94a2eSKuninori Morimoto 		"ASoC: pop wq checking: %s status: %s waiting: %s\n",
35583f94a2eSKuninori Morimoto 		codec_dai->driver->playback.stream_name,
356b3dea624SKuninori Morimoto 		snd_soc_dai_stream_active(codec_dai, playback) ?
357b3dea624SKuninori Morimoto 		"active" : "inactive",
35883f94a2eSKuninori Morimoto 		rtd->pop_wait ? "yes" : "no");
35983f94a2eSKuninori Morimoto 
36083f94a2eSKuninori Morimoto 	/* are we waiting on this codec DAI stream */
36183f94a2eSKuninori Morimoto 	if (rtd->pop_wait == 1) {
36283f94a2eSKuninori Morimoto 		rtd->pop_wait = 0;
3630f6011fdSKuninori Morimoto 		snd_soc_dapm_stream_event(rtd, playback,
36483f94a2eSKuninori Morimoto 					  SND_SOC_DAPM_STREAM_STOP);
36583f94a2eSKuninori Morimoto 	}
36683f94a2eSKuninori Morimoto 
36738e42f6dSKuninori Morimoto 	snd_soc_dpcm_mutex_unlock(rtd);
36883f94a2eSKuninori Morimoto }
36983f94a2eSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_close_delayed_work);
37083f94a2eSKuninori Morimoto 
3716e864344SKuninori Morimoto static void soc_release_rtd_dev(struct device *dev)
3726e864344SKuninori Morimoto {
3736e864344SKuninori Morimoto 	/* "dev" means "rtd->dev" */
3746e864344SKuninori Morimoto 	kfree(dev);
3756e864344SKuninori Morimoto }
3766e864344SKuninori Morimoto 
3771c93a9e0SKuninori Morimoto static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
3781c93a9e0SKuninori Morimoto {
3796e864344SKuninori Morimoto 	if (!rtd)
3806e864344SKuninori Morimoto 		return;
3816e864344SKuninori Morimoto 
382753ace0aSKuninori Morimoto 	list_del(&rtd->list);
383b7c5bc45SKuninori Morimoto 
3849c9b6520SCurtis Malainey 	if (delayed_work_pending(&rtd->delayed_work))
3850ced7b05SKuninori Morimoto 		flush_delayed_work(&rtd->delayed_work);
3860ced7b05SKuninori Morimoto 	snd_soc_pcm_component_free(rtd);
3870ced7b05SKuninori Morimoto 
388b7c5bc45SKuninori Morimoto 	/*
389b7c5bc45SKuninori Morimoto 	 * we don't need to call kfree() for rtd->dev
390b7c5bc45SKuninori Morimoto 	 * see
391b7c5bc45SKuninori Morimoto 	 *	soc_release_rtd_dev()
392d918a376SKuninori Morimoto 	 *
393d918a376SKuninori Morimoto 	 * We don't need rtd->dev NULL check, because
394d918a376SKuninori Morimoto 	 * it is alloced *before* rtd.
395d918a376SKuninori Morimoto 	 * see
396d918a376SKuninori Morimoto 	 *	soc_new_pcm_runtime()
397f1b3ee78SKuninori Morimoto 	 *
398f1b3ee78SKuninori Morimoto 	 * We don't need to mind freeing for rtd,
399f1b3ee78SKuninori Morimoto 	 * because it was created from dev (= rtd->dev)
400f1b3ee78SKuninori Morimoto 	 * see
401f1b3ee78SKuninori Morimoto 	 *	soc_new_pcm_runtime()
402f1b3ee78SKuninori Morimoto 	 *
403f1b3ee78SKuninori Morimoto 	 *		rtd = devm_kzalloc(dev, ...);
404f1b3ee78SKuninori Morimoto 	 *		rtd->dev = dev
405b7c5bc45SKuninori Morimoto 	 */
406b7c5bc45SKuninori Morimoto 	device_unregister(rtd->dev);
4071c93a9e0SKuninori Morimoto }
4081c93a9e0SKuninori Morimoto 
4094bf2e385SCurtis Malainey static void close_delayed_work(struct work_struct *work) {
4104bf2e385SCurtis Malainey 	struct snd_soc_pcm_runtime *rtd =
4114bf2e385SCurtis Malainey 			container_of(work, struct snd_soc_pcm_runtime,
4124bf2e385SCurtis Malainey 				     delayed_work.work);
4134bf2e385SCurtis Malainey 
4144bf2e385SCurtis Malainey 	if (rtd->close_delayed_work_func)
4154bf2e385SCurtis Malainey 		rtd->close_delayed_work_func(rtd);
4164bf2e385SCurtis Malainey }
4174bf2e385SCurtis Malainey 
4181a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
4191a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
4201a497983SMengdong Lin {
4211a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
422613fb500SKuninori Morimoto 	struct snd_soc_component *component;
423d918a376SKuninori Morimoto 	struct device *dev;
4246e864344SKuninori Morimoto 	int ret;
425d74c2a15SKuninori Morimoto 	int stream;
4261a497983SMengdong Lin 
4276e864344SKuninori Morimoto 	/*
428d918a376SKuninori Morimoto 	 * for rtd->dev
429d918a376SKuninori Morimoto 	 */
430d918a376SKuninori Morimoto 	dev = kzalloc(sizeof(struct device), GFP_KERNEL);
431d918a376SKuninori Morimoto 	if (!dev)
432d918a376SKuninori Morimoto 		return NULL;
433d918a376SKuninori Morimoto 
434d918a376SKuninori Morimoto 	dev->parent	= card->dev;
435d918a376SKuninori Morimoto 	dev->release	= soc_release_rtd_dev;
436d918a376SKuninori Morimoto 
437d918a376SKuninori Morimoto 	dev_set_name(dev, "%s", dai_link->name);
438d918a376SKuninori Morimoto 
439d918a376SKuninori Morimoto 	ret = device_register(dev);
440d918a376SKuninori Morimoto 	if (ret < 0) {
441d918a376SKuninori Morimoto 		put_device(dev); /* soc_release_rtd_dev */
442d918a376SKuninori Morimoto 		return NULL;
443d918a376SKuninori Morimoto 	}
444d918a376SKuninori Morimoto 
445d918a376SKuninori Morimoto 	/*
4466e864344SKuninori Morimoto 	 * for rtd
4476e864344SKuninori Morimoto 	 */
448613fb500SKuninori Morimoto 	rtd = devm_kzalloc(dev,
449613fb500SKuninori Morimoto 			   sizeof(*rtd) +
45066a796c0Slishqchn 			   sizeof(component) * (dai_link->num_cpus +
451613fb500SKuninori Morimoto 						 dai_link->num_codecs +
452613fb500SKuninori Morimoto 						 dai_link->num_platforms),
453613fb500SKuninori Morimoto 			   GFP_KERNEL);
4545fa7553dSKuninori Morimoto 	if (!rtd) {
4555fa7553dSKuninori Morimoto 		device_unregister(dev);
4565fa7553dSKuninori Morimoto 		return NULL;
4575fa7553dSKuninori Morimoto 	}
4581a497983SMengdong Lin 
459d918a376SKuninori Morimoto 	rtd->dev = dev;
46007d22a9bSTakashi Iwai 	INIT_LIST_HEAD(&rtd->list);
461d74c2a15SKuninori Morimoto 	for_each_pcm_streams(stream) {
462d74c2a15SKuninori Morimoto 		INIT_LIST_HEAD(&rtd->dpcm[stream].be_clients);
463d74c2a15SKuninori Morimoto 		INIT_LIST_HEAD(&rtd->dpcm[stream].fe_clients);
464d74c2a15SKuninori Morimoto 	}
465d918a376SKuninori Morimoto 	dev_set_drvdata(dev, rtd);
4664bf2e385SCurtis Malainey 	INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
467d918a376SKuninori Morimoto 
4686e864344SKuninori Morimoto 	/*
46922a2fc81SKuninori Morimoto 	 * for rtd->dais
4706e864344SKuninori Morimoto 	 */
47122a2fc81SKuninori Morimoto 	rtd->dais = devm_kcalloc(dev, dai_link->num_cpus + dai_link->num_codecs,
4726396bb22SKees Cook 					sizeof(struct snd_soc_dai *),
4731a497983SMengdong Lin 					GFP_KERNEL);
47422a2fc81SKuninori Morimoto 	if (!rtd->dais)
4756e864344SKuninori Morimoto 		goto free_rtd;
4766e864344SKuninori Morimoto 
4776e864344SKuninori Morimoto 	/*
47822a2fc81SKuninori Morimoto 	 * dais = [][][][][][][][][][][][][][][][][][]
47922a2fc81SKuninori Morimoto 	 *	  ^cpu_dais         ^codec_dais
48022a2fc81SKuninori Morimoto 	 *	  |--- num_cpus ---|--- num_codecs --|
4811729025bSKuninori Morimoto 	 * see
4821729025bSKuninori Morimoto 	 *	asoc_rtd_to_cpu()
4831729025bSKuninori Morimoto 	 *	asoc_rtd_to_codec()
48476afa643SShreyas NC 	 */
485929deb84SKuninori Morimoto 	rtd->card	= card;
486929deb84SKuninori Morimoto 	rtd->dai_link	= dai_link;
48701faf501SKuninori Morimoto 	rtd->num	= card->num_rtd++;
488c403dcd8SKuninori Morimoto 	rtd->pmdown_time = pmdown_time;			/* default power off timeout */
489929deb84SKuninori Morimoto 
4906634e3d6SKuninori Morimoto 	/* see for_each_card_rtds */
4911a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
492a848125eSKuninori Morimoto 
4935c74c9d3SCharles Keepax 	ret = device_add_groups(dev, soc_dev_attr_groups);
4945c74c9d3SCharles Keepax 	if (ret < 0)
4955c74c9d3SCharles Keepax 		goto free_rtd;
4965c74c9d3SCharles Keepax 
497a848125eSKuninori Morimoto 	return rtd;
4986e864344SKuninori Morimoto 
4996e864344SKuninori Morimoto free_rtd:
5006e864344SKuninori Morimoto 	soc_free_pcm_runtime(rtd);
5016e864344SKuninori Morimoto 	return NULL;
5021a497983SMengdong Lin }
5031a497983SMengdong Lin 
50465462e44SKuninori Morimoto static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
50565462e44SKuninori Morimoto {
50665462e44SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
50765462e44SKuninori Morimoto 
50865462e44SKuninori Morimoto 	for_each_card_rtds(card, rtd)
50965462e44SKuninori Morimoto 		flush_delayed_work(&rtd->delayed_work);
51065462e44SKuninori Morimoto }
51165462e44SKuninori Morimoto 
5126f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
513d4c1d9ebSKuninori Morimoto static void soc_playback_digital_mute(struct snd_soc_card *card, int mute)
514d4c1d9ebSKuninori Morimoto {
515d4c1d9ebSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
516d4c1d9ebSKuninori Morimoto 	struct snd_soc_dai *dai;
517d4c1d9ebSKuninori Morimoto 	int playback = SNDRV_PCM_STREAM_PLAYBACK;
518d4c1d9ebSKuninori Morimoto 	int i;
519d4c1d9ebSKuninori Morimoto 
520d4c1d9ebSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
521d4c1d9ebSKuninori Morimoto 
522d4c1d9ebSKuninori Morimoto 		if (rtd->dai_link->ignore_suspend)
523d4c1d9ebSKuninori Morimoto 			continue;
524d4c1d9ebSKuninori Morimoto 
525d4c1d9ebSKuninori Morimoto 		for_each_rtd_dais(rtd, i, dai) {
526d4c1d9ebSKuninori Morimoto 			if (snd_soc_dai_stream_active(dai, playback))
527d4c1d9ebSKuninori Morimoto 				snd_soc_dai_digital_mute(dai, mute, playback);
528d4c1d9ebSKuninori Morimoto 		}
529d4c1d9ebSKuninori Morimoto 	}
530d4c1d9ebSKuninori Morimoto }
531d4c1d9ebSKuninori Morimoto 
532baed393eSKuninori Morimoto static void soc_dapm_suspend_resume(struct snd_soc_card *card, int event)
533baed393eSKuninori Morimoto {
534baed393eSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
535baed393eSKuninori Morimoto 	int stream;
536baed393eSKuninori Morimoto 
537baed393eSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
538baed393eSKuninori Morimoto 
539baed393eSKuninori Morimoto 		if (rtd->dai_link->ignore_suspend)
540baed393eSKuninori Morimoto 			continue;
541baed393eSKuninori Morimoto 
542baed393eSKuninori Morimoto 		for_each_pcm_streams(stream)
543baed393eSKuninori Morimoto 			snd_soc_dapm_stream_event(rtd, stream, event);
544baed393eSKuninori Morimoto 	}
545baed393eSKuninori Morimoto }
546baed393eSKuninori Morimoto 
547db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5486f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
549db2a4165SFrank Mandarino {
5506f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
551d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
5521a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
5531a497983SMengdong Lin 	int i;
554db2a4165SFrank Mandarino 
555c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
5569024bae4SKuninori Morimoto 	if (!snd_soc_card_is_instantiated(card))
557e3509ff0SDaniel Mack 		return 0;
558e3509ff0SDaniel Mack 
5592c7b696aSMarcel Ziswiler 	/*
5602c7b696aSMarcel Ziswiler 	 * Due to the resume being scheduled into a workqueue we could
5616ed25978SAndy Green 	 * suspend before that's finished - wait for it to complete.
5626ed25978SAndy Green 	 */
563b6cc78daSTakashi Iwai 	snd_power_wait(card->snd_card);
5646ed25978SAndy Green 
5656ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
566f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5676ed25978SAndy Green 
568a00f90f9SMark Brown 	/* mute any active DACs */
569d4c1d9ebSKuninori Morimoto 	soc_playback_digital_mute(card, 1);
570db2a4165SFrank Mandarino 
5714ccab3e7SLiam Girdwood 	/* suspend all pcms */
572bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5731a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5743efab7dcSMark Brown 			continue;
5753efab7dcSMark Brown 
5761a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
5773efab7dcSMark Brown 	}
5784ccab3e7SLiam Girdwood 
579130dc08cSKuninori Morimoto 	snd_soc_card_suspend_pre(card);
580db2a4165SFrank Mandarino 
58137660b6dSLars-Peter Clausen 	/* close any waiting streams */
58265462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
583db2a4165SFrank Mandarino 
584baed393eSKuninori Morimoto 	soc_dapm_suspend_resume(card, SND_SOC_DAPM_STREAM_SUSPEND);
585db2a4165SFrank Mandarino 
5868be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
5878be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
588e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
589e2d32ff6SMark Brown 
5909178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
5911272063aSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5921272063aSKuninori Morimoto 
5931272063aSKuninori Morimoto 		if (rtd->dai_link->ignore_suspend)
5941272063aSKuninori Morimoto 			continue;
5951272063aSKuninori Morimoto 
5961272063aSKuninori Morimoto 		for_each_rtd_components(rtd, i, component) {
5972c7b696aSMarcel Ziswiler 			struct snd_soc_dapm_context *dapm =
5982c7b696aSMarcel Ziswiler 				snd_soc_component_get_dapm(component);
599d9fc4063SKuninori Morimoto 
6002c7b696aSMarcel Ziswiler 			/*
6011272063aSKuninori Morimoto 			 * ignore if component was already suspended
6022c7b696aSMarcel Ziswiler 			 */
6031272063aSKuninori Morimoto 			if (snd_soc_component_is_suspended(component))
6041272063aSKuninori Morimoto 				continue;
6051272063aSKuninori Morimoto 
6061272063aSKuninori Morimoto 			/*
6071272063aSKuninori Morimoto 			 * If there are paths active then the COMPONENT will be
6081272063aSKuninori Morimoto 			 * held with bias _ON and should not be suspended.
6091272063aSKuninori Morimoto 			 */
6104890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
6111547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
612125a25daSMark Brown 				/*
6139178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
614125a25daSMark Brown 				 * bias off then being in STANDBY
615125a25daSMark Brown 				 * means it's doing something,
616125a25daSMark Brown 				 * otherwise fall through.
617125a25daSMark Brown 				 */
6184890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
6199178feb4SKuninori Morimoto 					dev_dbg(component->dev,
62010e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
621125a25daSMark Brown 					break;
622125a25daSMark Brown 				}
623df561f66SGustavo A. R. Silva 				fallthrough;
624a8093297SLars-Peter Clausen 
6251547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
62666c51573SKuninori Morimoto 				snd_soc_component_suspend(component);
6279178feb4SKuninori Morimoto 				if (component->regmap)
6289178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
629988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
6309178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
6311547aba9SMark Brown 				break;
6321547aba9SMark Brown 			default:
6339178feb4SKuninori Morimoto 				dev_dbg(component->dev,
6349178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
6351547aba9SMark Brown 				break;
6361547aba9SMark Brown 			}
6371547aba9SMark Brown 		}
638f0fba2adSLiam Girdwood 	}
639db2a4165SFrank Mandarino 
640d17b60b2SKuninori Morimoto 	snd_soc_card_suspend_post(card);
641db2a4165SFrank Mandarino 
642db2a4165SFrank Mandarino 	return 0;
643db2a4165SFrank Mandarino }
6446f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
645db2a4165SFrank Mandarino 
6462c7b696aSMarcel Ziswiler /*
6472c7b696aSMarcel Ziswiler  * deferred resume work, so resume can complete before we finished
6486ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6496ed25978SAndy Green  */
6506ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
651db2a4165SFrank Mandarino {
652f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
6532c7b696aSMarcel Ziswiler 			container_of(work, struct snd_soc_card,
6542c7b696aSMarcel Ziswiler 				     deferred_resume_work);
655d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
656db2a4165SFrank Mandarino 
6572c7b696aSMarcel Ziswiler 	/*
6582c7b696aSMarcel Ziswiler 	 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6596ed25978SAndy Green 	 * so userspace apps are blocked from touching us
6606ed25978SAndy Green 	 */
6616ed25978SAndy Green 
662f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
6636ed25978SAndy Green 
6649949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
665f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
6669949788bSMark Brown 
667934c752cSKuninori Morimoto 	snd_soc_card_resume_pre(card);
668db2a4165SFrank Mandarino 
669f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
670e40fadbcSKuninori Morimoto 		if (snd_soc_component_is_suspended(component))
6719a840cbaSKuninori Morimoto 			snd_soc_component_resume(component);
6721547aba9SMark Brown 	}
673db2a4165SFrank Mandarino 
674baed393eSKuninori Morimoto 	soc_dapm_suspend_resume(card, SND_SOC_DAPM_STREAM_RESUME);
675db2a4165SFrank Mandarino 
6763ff3f64bSMark Brown 	/* unmute any active DACs */
677d4c1d9ebSKuninori Morimoto 	soc_playback_digital_mute(card, 0);
678db2a4165SFrank Mandarino 
679739443d1SKuninori Morimoto 	snd_soc_card_resume_post(card);
680db2a4165SFrank Mandarino 
681f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
6826ed25978SAndy Green 
6838be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
6848be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
685e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
6861a7aaa58SJeeja KP 
6871a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
6881a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
6896ed25978SAndy Green }
6906ed25978SAndy Green 
6916ed25978SAndy Green /* powers up audio subsystem after a suspend */
6926f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
6936ed25978SAndy Green {
6946f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
69576c39e86SKuninori Morimoto 	struct snd_soc_component *component;
696b9dd94a8SPeter Ujfalusi 
697c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
6989024bae4SKuninori Morimoto 	if (!snd_soc_card_is_instantiated(card))
6995ff1ddf2SEric Miao 		return 0;
7005ff1ddf2SEric Miao 
701988e8cc4SNicolin Chen 	/* activate pins from sleep state */
70276c39e86SKuninori Morimoto 	for_each_card_components(card, component)
703b3dea624SKuninori Morimoto 		if (snd_soc_component_active(component))
70476c39e86SKuninori Morimoto 			pinctrl_pm_select_default_state(component->dev);
705988e8cc4SNicolin Chen 
706f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: Scheduling resume work\n");
7076308419aSMark Brown 	if (!schedule_work(&card->deferred_resume_work))
708f110bfc7SLiam Girdwood 		dev_err(dev, "ASoC: resume work item may be lost\n");
7096ed25978SAndy Green 
710db2a4165SFrank Mandarino 	return 0;
711db2a4165SFrank Mandarino }
7126f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
713b3da4251SKuninori Morimoto 
714b3da4251SKuninori Morimoto static void soc_resume_init(struct snd_soc_card *card)
715b3da4251SKuninori Morimoto {
716b3da4251SKuninori Morimoto 	/* deferred resume work */
717b3da4251SKuninori Morimoto 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
718b3da4251SKuninori Morimoto }
719db2a4165SFrank Mandarino #else
7206f8ab4acSMark Brown #define snd_soc_suspend NULL
7216f8ab4acSMark Brown #define snd_soc_resume NULL
72241b1774fSKuninori Morimoto static inline void soc_resume_init(struct snd_soc_card *card) { }
723db2a4165SFrank Mandarino #endif
724db2a4165SFrank Mandarino 
725c0834440SKuninori Morimoto static struct device_node
726c0834440SKuninori Morimoto *soc_component_to_node(struct snd_soc_component *component)
72712023a9aSMisael Lopez Cruz {
728c0834440SKuninori Morimoto 	struct device_node *of_node;
72912023a9aSMisael Lopez Cruz 
730c0834440SKuninori Morimoto 	of_node = component->dev->of_node;
731c0834440SKuninori Morimoto 	if (!of_node && component->dev->parent)
732c0834440SKuninori Morimoto 		of_node = component->dev->parent->of_node;
73334e81ab4SLars-Peter Clausen 
734c0834440SKuninori Morimoto 	return of_node;
73512023a9aSMisael Lopez Cruz }
73612023a9aSMisael Lopez Cruz 
737be6ac0a9SKuninori Morimoto static int snd_soc_is_matching_component(
738be6ac0a9SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc,
739be6ac0a9SKuninori Morimoto 	struct snd_soc_component *component)
740be6ac0a9SKuninori Morimoto {
741be6ac0a9SKuninori Morimoto 	struct device_node *component_of_node;
742be6ac0a9SKuninori Morimoto 
7437d7db5d3SKuninori Morimoto 	if (!dlc)
7447d7db5d3SKuninori Morimoto 		return 0;
7457d7db5d3SKuninori Morimoto 
7467d7db5d3SKuninori Morimoto 	component_of_node = soc_component_to_node(component);
747be6ac0a9SKuninori Morimoto 
748be6ac0a9SKuninori Morimoto 	if (dlc->of_node && component_of_node != dlc->of_node)
749be6ac0a9SKuninori Morimoto 		return 0;
750be6ac0a9SKuninori Morimoto 	if (dlc->name && strcmp(component->name, dlc->name))
751be6ac0a9SKuninori Morimoto 		return 0;
752be6ac0a9SKuninori Morimoto 
753be6ac0a9SKuninori Morimoto 	return 1;
754be6ac0a9SKuninori Morimoto }
755be6ac0a9SKuninori Morimoto 
756db2a4165SFrank Mandarino static struct snd_soc_component *soc_find_component(
757c1e230f0SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc)
758db2a4165SFrank Mandarino {
759db2a4165SFrank Mandarino 	struct snd_soc_component *component;
760db2a4165SFrank Mandarino 
761db2a4165SFrank Mandarino 	lockdep_assert_held(&client_mutex);
762db2a4165SFrank Mandarino 
763e3303268SKuninori Morimoto 	/*
764e3303268SKuninori Morimoto 	 * NOTE
765e3303268SKuninori Morimoto 	 *
766e3303268SKuninori Morimoto 	 * It returns *1st* found component, but some driver
767e3303268SKuninori Morimoto 	 * has few components by same of_node/name
768e3303268SKuninori Morimoto 	 * ex)
769e3303268SKuninori Morimoto 	 *	CPU component and generic DMAEngine component
770e3303268SKuninori Morimoto 	 */
771c1e230f0SKuninori Morimoto 	for_each_component(component)
772c1e230f0SKuninori Morimoto 		if (snd_soc_is_matching_component(dlc, component))
773db2a4165SFrank Mandarino 			return component;
77402a06d30SBarry Song 
77502a06d30SBarry Song 	return NULL;
77612023a9aSMisael Lopez Cruz }
77712023a9aSMisael Lopez Cruz 
778fbb88b5cSMengdong Lin /**
779fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
780fbb88b5cSMengdong Lin  *
7814958471bSJeffy Chen  * @dlc: name of the DAI or the DAI driver and optional component info to match
782fbb88b5cSMengdong Lin  *
783ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
784fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
785fbb88b5cSMengdong Lin  * should also match if being specified.
786fbb88b5cSMengdong Lin  *
787fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
788fbb88b5cSMengdong Lin  */
789305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
79014621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
79112023a9aSMisael Lopez Cruz {
79214621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
79314621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
79412023a9aSMisael Lopez Cruz 
79534e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
79634e81ab4SLars-Peter Clausen 
79714621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs */
798368dee94SKuninori Morimoto 	for_each_component(component) {
799be6ac0a9SKuninori Morimoto 		if (!snd_soc_is_matching_component(dlc, component))
80012023a9aSMisael Lopez Cruz 			continue;
80115a0c645SKuninori Morimoto 		for_each_component_dais(component, dai) {
8024958471bSJeffy Chen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
8036a6dafdaSJeffy Chen 			    && (!dai->driver->name
8046a6dafdaSJeffy Chen 				|| strcmp(dai->driver->name, dlc->dai_name)))
80514621c7eSLars-Peter Clausen 				continue;
80612023a9aSMisael Lopez Cruz 
80714621c7eSLars-Peter Clausen 			return dai;
80812023a9aSMisael Lopez Cruz 		}
80912023a9aSMisael Lopez Cruz 	}
81012023a9aSMisael Lopez Cruz 
81112023a9aSMisael Lopez Cruz 	return NULL;
81212023a9aSMisael Lopez Cruz }
813305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
81412023a9aSMisael Lopez Cruz 
815c1c277b2SKuninori Morimoto struct snd_soc_dai *snd_soc_find_dai_with_mutex(
816c1c277b2SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc)
817c1c277b2SKuninori Morimoto {
818c1c277b2SKuninori Morimoto 	struct snd_soc_dai *dai;
819c1c277b2SKuninori Morimoto 
820c1c277b2SKuninori Morimoto 	mutex_lock(&client_mutex);
821c1c277b2SKuninori Morimoto 	dai = snd_soc_find_dai(dlc);
822c1c277b2SKuninori Morimoto 	mutex_unlock(&client_mutex);
823c1c277b2SKuninori Morimoto 
824c1c277b2SKuninori Morimoto 	return dai;
825c1c277b2SKuninori Morimoto }
826c1c277b2SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_find_dai_with_mutex);
827c1c277b2SKuninori Morimoto 
828bfce78a5SKuninori Morimoto static int soc_dai_link_sanity_check(struct snd_soc_card *card,
82936794902SKuninori Morimoto 				     struct snd_soc_dai_link *link)
83036794902SKuninori Morimoto {
83136794902SKuninori Morimoto 	int i;
83276afa643SShreyas NC 	struct snd_soc_dai_link_component *cpu, *codec, *platform;
83336794902SKuninori Morimoto 
83436794902SKuninori Morimoto 	for_each_link_codecs(link, i, codec) {
83536794902SKuninori Morimoto 		/*
83636794902SKuninori Morimoto 		 * Codec must be specified by 1 of name or OF node,
83736794902SKuninori Morimoto 		 * not both or neither.
83836794902SKuninori Morimoto 		 */
83936794902SKuninori Morimoto 		if (!!codec->name == !!codec->of_node) {
84036794902SKuninori Morimoto 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
84136794902SKuninori Morimoto 				link->name);
84236794902SKuninori Morimoto 			return -EINVAL;
84336794902SKuninori Morimoto 		}
84436794902SKuninori Morimoto 
84536794902SKuninori Morimoto 		/* Codec DAI name must be specified */
84636794902SKuninori Morimoto 		if (!codec->dai_name) {
84736794902SKuninori Morimoto 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
84836794902SKuninori Morimoto 				link->name);
84936794902SKuninori Morimoto 			return -EINVAL;
85036794902SKuninori Morimoto 		}
85136794902SKuninori Morimoto 
85236794902SKuninori Morimoto 		/*
85336794902SKuninori Morimoto 		 * Defer card registration if codec component is not added to
85436794902SKuninori Morimoto 		 * component list.
85536794902SKuninori Morimoto 		 */
856b2acc24cSRanjani Sridharan 		if (!soc_find_component(codec)) {
857b2acc24cSRanjani Sridharan 			dev_dbg(card->dev,
858b2acc24cSRanjani Sridharan 				"ASoC: codec component %s not found for link %s\n",
859b2acc24cSRanjani Sridharan 				codec->name, link->name);
86036794902SKuninori Morimoto 			return -EPROBE_DEFER;
86136794902SKuninori Morimoto 		}
862b2acc24cSRanjani Sridharan 	}
86336794902SKuninori Morimoto 
86436794902SKuninori Morimoto 	for_each_link_platforms(link, i, platform) {
86536794902SKuninori Morimoto 		/*
86636794902SKuninori Morimoto 		 * Platform may be specified by either name or OF node, but it
86736794902SKuninori Morimoto 		 * can be left unspecified, then no components will be inserted
86836794902SKuninori Morimoto 		 * in the rtdcom list
86936794902SKuninori Morimoto 		 */
87036794902SKuninori Morimoto 		if (!!platform->name == !!platform->of_node) {
87136794902SKuninori Morimoto 			dev_err(card->dev,
87236794902SKuninori Morimoto 				"ASoC: Neither/both platform name/of_node are set for %s\n",
87336794902SKuninori Morimoto 				link->name);
87436794902SKuninori Morimoto 			return -EINVAL;
87536794902SKuninori Morimoto 		}
87636794902SKuninori Morimoto 
87736794902SKuninori Morimoto 		/*
87836794902SKuninori Morimoto 		 * Defer card registration if platform component is not added to
87936794902SKuninori Morimoto 		 * component list.
88036794902SKuninori Morimoto 		 */
881b2acc24cSRanjani Sridharan 		if (!soc_find_component(platform)) {
882b2acc24cSRanjani Sridharan 			dev_dbg(card->dev,
883b2acc24cSRanjani Sridharan 				"ASoC: platform component %s not found for link %s\n",
884b2acc24cSRanjani Sridharan 				platform->name, link->name);
88536794902SKuninori Morimoto 			return -EPROBE_DEFER;
88636794902SKuninori Morimoto 		}
887b2acc24cSRanjani Sridharan 	}
88836794902SKuninori Morimoto 
88976afa643SShreyas NC 	for_each_link_cpus(link, i, cpu) {
89036794902SKuninori Morimoto 		/*
89136794902SKuninori Morimoto 		 * CPU device may be specified by either name or OF node, but
89236794902SKuninori Morimoto 		 * can be left unspecified, and will be matched based on DAI
89336794902SKuninori Morimoto 		 * name alone..
89436794902SKuninori Morimoto 		 */
89576afa643SShreyas NC 		if (cpu->name && cpu->of_node) {
89636794902SKuninori Morimoto 			dev_err(card->dev,
89736794902SKuninori Morimoto 				"ASoC: Neither/both cpu name/of_node are set for %s\n",
89836794902SKuninori Morimoto 				link->name);
89936794902SKuninori Morimoto 			return -EINVAL;
90036794902SKuninori Morimoto 		}
90136794902SKuninori Morimoto 
90236794902SKuninori Morimoto 		/*
903cd3c5ad7SKuninori Morimoto 		 * Defer card registration if cpu dai component is not added to
90436794902SKuninori Morimoto 		 * component list.
90536794902SKuninori Morimoto 		 */
90676afa643SShreyas NC 		if ((cpu->of_node || cpu->name) &&
907b2acc24cSRanjani Sridharan 		    !soc_find_component(cpu)) {
908b2acc24cSRanjani Sridharan 			dev_dbg(card->dev,
909b2acc24cSRanjani Sridharan 				"ASoC: cpu component %s not found for link %s\n",
910b2acc24cSRanjani Sridharan 				cpu->name, link->name);
91136794902SKuninori Morimoto 			return -EPROBE_DEFER;
912b2acc24cSRanjani Sridharan 		}
91336794902SKuninori Morimoto 
91436794902SKuninori Morimoto 		/*
91536794902SKuninori Morimoto 		 * At least one of CPU DAI name or CPU device name/node must be
91636794902SKuninori Morimoto 		 * specified
91736794902SKuninori Morimoto 		 */
91876afa643SShreyas NC 		if (!cpu->dai_name &&
91976afa643SShreyas NC 		    !(cpu->name || cpu->of_node)) {
92036794902SKuninori Morimoto 			dev_err(card->dev,
92136794902SKuninori Morimoto 				"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
92236794902SKuninori Morimoto 				link->name);
92336794902SKuninori Morimoto 			return -EINVAL;
92436794902SKuninori Morimoto 		}
92576afa643SShreyas NC 	}
92636794902SKuninori Morimoto 
92736794902SKuninori Morimoto 	return 0;
92836794902SKuninori Morimoto }
92936794902SKuninori Morimoto 
930da704f26SKuninori Morimoto /**
93150cd9b53SKuninori Morimoto  * snd_soc_remove_pcm_runtime - Remove a pcm_runtime from card
93250cd9b53SKuninori Morimoto  * @card: The ASoC card to which the pcm_runtime has
93350cd9b53SKuninori Morimoto  * @rtd: The pcm_runtime to remove
934da704f26SKuninori Morimoto  *
93550cd9b53SKuninori Morimoto  * This function removes a pcm_runtime from the ASoC card.
936da704f26SKuninori Morimoto  */
93750cd9b53SKuninori Morimoto void snd_soc_remove_pcm_runtime(struct snd_soc_card *card,
93850cd9b53SKuninori Morimoto 				struct snd_soc_pcm_runtime *rtd)
939bc7a9091SKuninori Morimoto {
940da704f26SKuninori Morimoto 	lockdep_assert_held(&client_mutex);
941da704f26SKuninori Morimoto 
942da704f26SKuninori Morimoto 	/*
943da704f26SKuninori Morimoto 	 * Notify the machine driver for extra destruction
944da704f26SKuninori Morimoto 	 */
945fcbbcc32SKuninori Morimoto 	snd_soc_card_remove_dai_link(card, rtd->dai_link);
946da704f26SKuninori Morimoto 
947bc7a9091SKuninori Morimoto 	soc_free_pcm_runtime(rtd);
948bc7a9091SKuninori Morimoto }
94950cd9b53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_remove_pcm_runtime);
950bc7a9091SKuninori Morimoto 
95163dc47daSKuninori Morimoto /**
9520c048004SKuninori Morimoto  * snd_soc_add_pcm_runtime - Add a pcm_runtime dynamically via dai_link
9530c048004SKuninori Morimoto  * @card: The ASoC card to which the pcm_runtime is added
9540c048004SKuninori Morimoto  * @dai_link: The DAI link to find pcm_runtime
95563dc47daSKuninori Morimoto  *
9560c048004SKuninori Morimoto  * This function adds a pcm_runtime ASoC card by using dai_link.
95763dc47daSKuninori Morimoto  *
9580c048004SKuninori Morimoto  * Note: Topology can use this API to add pcm_runtime when probing the
95963dc47daSKuninori Morimoto  * topology component. And machine drivers can still define static
96063dc47daSKuninori Morimoto  * DAI links in dai_link array.
96163dc47daSKuninori Morimoto  */
962ffaf886eSKuninori Morimoto static int snd_soc_add_pcm_runtime(struct snd_soc_card *card,
9636f2f1ff0SMengdong Lin 				   struct snd_soc_dai_link *dai_link)
964db2a4165SFrank Mandarino {
9651a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
96676afa643SShreyas NC 	struct snd_soc_dai_link_component *codec, *platform, *cpu;
96790be711eSKuninori Morimoto 	struct snd_soc_component *component;
968bfce78a5SKuninori Morimoto 	int i, ret;
969db2a4165SFrank Mandarino 
97063dc47daSKuninori Morimoto 	lockdep_assert_held(&client_mutex);
97163dc47daSKuninori Morimoto 
97263dc47daSKuninori Morimoto 	/*
97363dc47daSKuninori Morimoto 	 * Notify the machine driver for extra initialization
97463dc47daSKuninori Morimoto 	 */
975cbc7a6b5SKuninori Morimoto 	ret = snd_soc_card_add_dai_link(card, dai_link);
976cbc7a6b5SKuninori Morimoto 	if (ret < 0)
977cbc7a6b5SKuninori Morimoto 		return ret;
97863dc47daSKuninori Morimoto 
979a655de80SLiam Girdwood 	if (dai_link->ignore)
980a655de80SLiam Girdwood 		return 0;
981a655de80SLiam Girdwood 
9826f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
9836308419aSMark Brown 
984bfce78a5SKuninori Morimoto 	ret = soc_dai_link_sanity_check(card, dai_link);
985bfce78a5SKuninori Morimoto 	if (ret < 0)
986bfce78a5SKuninori Morimoto 		return ret;
987bfce78a5SKuninori Morimoto 
988513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
989513cb311SSudip Mukherjee 	if (!rtd)
990513cb311SSudip Mukherjee 		return -ENOMEM;
991513cb311SSudip Mukherjee 
99276afa643SShreyas NC 	for_each_link_cpus(dai_link, i, cpu) {
993c2233a26SKuninori Morimoto 		asoc_rtd_to_cpu(rtd, i) = snd_soc_find_dai(cpu);
994c2233a26SKuninori Morimoto 		if (!asoc_rtd_to_cpu(rtd, i)) {
9956b490879SMartin Hundebøll 			dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
99676afa643SShreyas NC 				 cpu->dai_name);
9971a497983SMengdong Lin 			goto _err_defer;
998c5af3a2eSMark Brown 		}
999c2233a26SKuninori Morimoto 		snd_soc_rtd_add_component(rtd, asoc_rtd_to_cpu(rtd, i)->component);
100076afa643SShreyas NC 	}
100176afa643SShreyas NC 
100288bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
100334614739SJerome Brunet 	for_each_link_codecs(dai_link, i, codec) {
1004c2233a26SKuninori Morimoto 		asoc_rtd_to_codec(rtd, i) = snd_soc_find_dai(codec);
1005c2233a26SKuninori Morimoto 		if (!asoc_rtd_to_codec(rtd, i)) {
10067c7e2d6aSStefan Agner 			dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
100734614739SJerome Brunet 				 codec->dai_name);
10081a497983SMengdong Lin 			goto _err_defer;
100912023a9aSMisael Lopez Cruz 		}
101034614739SJerome Brunet 
1011c2233a26SKuninori Morimoto 		snd_soc_rtd_add_component(rtd, asoc_rtd_to_codec(rtd, i)->component);
101288bd870fSBenoit Cousson 	}
101388bd870fSBenoit Cousson 
1014e2b30edfSKuninori Morimoto 	/* Find PLATFORM from registered PLATFORMs */
101534614739SJerome Brunet 	for_each_link_platforms(dai_link, i, platform) {
1016368dee94SKuninori Morimoto 		for_each_component(component) {
101734614739SJerome Brunet 			if (!snd_soc_is_matching_component(platform, component))
101890be711eSKuninori Morimoto 				continue;
101990be711eSKuninori Morimoto 
102012b05232SKuninori Morimoto 			snd_soc_rtd_add_component(rtd, component);
102190be711eSKuninori Morimoto 		}
102234614739SJerome Brunet 	}
102390be711eSKuninori Morimoto 
1024b19e6e7bSMark Brown 	return 0;
10251a497983SMengdong Lin 
10261a497983SMengdong Lin _err_defer:
102750cd9b53SKuninori Morimoto 	snd_soc_remove_pcm_runtime(card, rtd);
10281a497983SMengdong Lin 	return -EPROBE_DEFER;
10296b05eda6SMark Brown }
1030ffaf886eSKuninori Morimoto 
1031ffaf886eSKuninori Morimoto int snd_soc_add_pcm_runtimes(struct snd_soc_card *card,
1032ffaf886eSKuninori Morimoto 			     struct snd_soc_dai_link *dai_link,
1033ffaf886eSKuninori Morimoto 			     int num_dai_link)
1034ffaf886eSKuninori Morimoto {
1035ffaf886eSKuninori Morimoto 	for (int i = 0; i < num_dai_link; i++) {
1036ffaf886eSKuninori Morimoto 		int ret = snd_soc_add_pcm_runtime(card, dai_link + i);
1037ffaf886eSKuninori Morimoto 
1038ffaf886eSKuninori Morimoto 		if (ret < 0)
1039ffaf886eSKuninori Morimoto 			return ret;
1040ffaf886eSKuninori Morimoto 	}
1041ffaf886eSKuninori Morimoto 
1042ffaf886eSKuninori Morimoto 	return 0;
1043ffaf886eSKuninori Morimoto }
1044ffaf886eSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_pcm_runtimes);
10456b05eda6SMark Brown 
1046ba9e82a1SKuninori Morimoto static void snd_soc_runtime_get_dai_fmt(struct snd_soc_pcm_runtime *rtd)
1047ba9e82a1SKuninori Morimoto {
1048ba9e82a1SKuninori Morimoto 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1049ba9e82a1SKuninori Morimoto 	struct snd_soc_dai *dai, *not_used;
1050ba9e82a1SKuninori Morimoto 	u64 pos, possible_fmt;
1051ba9e82a1SKuninori Morimoto 	unsigned int mask = 0, dai_fmt = 0;
1052ba9e82a1SKuninori Morimoto 	int i, j, priority, pri, until;
1053ba9e82a1SKuninori Morimoto 
1054ba9e82a1SKuninori Morimoto 	/*
1055ba9e82a1SKuninori Morimoto 	 * Get selectable format from each DAIs.
1056ba9e82a1SKuninori Morimoto 	 *
1057ba9e82a1SKuninori Morimoto 	 ****************************
1058ba9e82a1SKuninori Morimoto 	 *            NOTE
1059ba9e82a1SKuninori Morimoto 	 * Using .auto_selectable_formats is not mandatory,
1060ba9e82a1SKuninori Morimoto 	 * we can select format manually from Sound Card.
1061ba9e82a1SKuninori Morimoto 	 * When use it, driver should list well tested format only.
1062ba9e82a1SKuninori Morimoto 	 ****************************
1063ba9e82a1SKuninori Morimoto 	 *
1064ba9e82a1SKuninori Morimoto 	 * ex)
1065ba9e82a1SKuninori Morimoto 	 *	auto_selectable_formats (= SND_SOC_POSSIBLE_xxx)
1066ba9e82a1SKuninori Morimoto 	 *		 (A)	 (B)	 (C)
1067ba9e82a1SKuninori Morimoto 	 *	DAI0_: { 0x000F, 0x00F0, 0x0F00 };
1068ba9e82a1SKuninori Morimoto 	 *	DAI1 : { 0xF000, 0x0F00 };
1069ba9e82a1SKuninori Morimoto 	 *		 (X)	 (Y)
1070ba9e82a1SKuninori Morimoto 	 *
1071ba9e82a1SKuninori Morimoto 	 * "until" will be 3 in this case (MAX array size from DAI0 and DAI1)
1072ba9e82a1SKuninori Morimoto 	 * Here is dev_dbg() message and comments
1073ba9e82a1SKuninori Morimoto 	 *
1074ba9e82a1SKuninori Morimoto 	 * priority = 1
1075ba9e82a1SKuninori Morimoto 	 * DAI0: (pri, fmt) = (1, 000000000000000F) // 1st check (A) DAI1 is not selected
1076ba9e82a1SKuninori Morimoto 	 * DAI1: (pri, fmt) = (0, 0000000000000000) //               Necessary Waste
1077ba9e82a1SKuninori Morimoto 	 * DAI0: (pri, fmt) = (1, 000000000000000F) // 2nd check (A)
1078ba9e82a1SKuninori Morimoto 	 * DAI1: (pri, fmt) = (1, 000000000000F000) //           (X)
1079ba9e82a1SKuninori Morimoto 	 * priority = 2
1080ba9e82a1SKuninori Morimoto 	 * DAI0: (pri, fmt) = (2, 00000000000000FF) // 3rd check (A) + (B)
1081ba9e82a1SKuninori Morimoto 	 * DAI1: (pri, fmt) = (1, 000000000000F000) //           (X)
1082ba9e82a1SKuninori Morimoto 	 * DAI0: (pri, fmt) = (2, 00000000000000FF) // 4th check (A) + (B)
1083ba9e82a1SKuninori Morimoto 	 * DAI1: (pri, fmt) = (2, 000000000000FF00) //           (X) + (Y)
1084ba9e82a1SKuninori Morimoto 	 * priority = 3
1085ba9e82a1SKuninori Morimoto 	 * DAI0: (pri, fmt) = (3, 0000000000000FFF) // 5th check (A) + (B) + (C)
1086ba9e82a1SKuninori Morimoto 	 * DAI1: (pri, fmt) = (2, 000000000000FF00) //           (X) + (Y)
1087ba9e82a1SKuninori Morimoto 	 * found auto selected format: 0000000000000F00
1088ba9e82a1SKuninori Morimoto 	 */
1089ba9e82a1SKuninori Morimoto 	until = snd_soc_dai_get_fmt_max_priority(rtd);
1090ba9e82a1SKuninori Morimoto 	for (priority = 1; priority <= until; priority++) {
1091ba9e82a1SKuninori Morimoto 		for_each_rtd_dais(rtd, j, not_used) {
1092ba9e82a1SKuninori Morimoto 
1093ba9e82a1SKuninori Morimoto 			possible_fmt = ULLONG_MAX;
1094ba9e82a1SKuninori Morimoto 			for_each_rtd_dais(rtd, i, dai) {
1095ba9e82a1SKuninori Morimoto 				u64 fmt = 0;
1096ba9e82a1SKuninori Morimoto 
1097ba9e82a1SKuninori Morimoto 				pri = (j >= i) ? priority : priority - 1;
1098ba9e82a1SKuninori Morimoto 				fmt = snd_soc_dai_get_fmt(dai, pri);
1099ba9e82a1SKuninori Morimoto 				possible_fmt &= fmt;
1100ba9e82a1SKuninori Morimoto 			}
1101ba9e82a1SKuninori Morimoto 			if (possible_fmt)
1102ba9e82a1SKuninori Morimoto 				goto found;
1103ba9e82a1SKuninori Morimoto 		}
1104ba9e82a1SKuninori Morimoto 	}
1105ba9e82a1SKuninori Morimoto 	/* Not Found */
1106ba9e82a1SKuninori Morimoto 	return;
1107ba9e82a1SKuninori Morimoto found:
1108ba9e82a1SKuninori Morimoto 	/*
1109ba9e82a1SKuninori Morimoto 	 * convert POSSIBLE_DAIFMT to DAIFMT
1110ba9e82a1SKuninori Morimoto 	 *
1111ba9e82a1SKuninori Morimoto 	 * Some basic/default settings on each is defined as 0.
1112ba9e82a1SKuninori Morimoto 	 * see
1113ba9e82a1SKuninori Morimoto 	 *	SND_SOC_DAIFMT_NB_NF
1114ba9e82a1SKuninori Morimoto 	 *	SND_SOC_DAIFMT_GATED
1115ba9e82a1SKuninori Morimoto 	 *
1116ba9e82a1SKuninori Morimoto 	 * SND_SOC_DAIFMT_xxx_MASK can't notice it if Sound Card specify
1117ba9e82a1SKuninori Morimoto 	 * these value, and will be overwrite to auto selected value.
1118ba9e82a1SKuninori Morimoto 	 *
1119ba9e82a1SKuninori Morimoto 	 * To avoid such issue, loop from 63 to 0 here.
1120ba9e82a1SKuninori Morimoto 	 * Small number of SND_SOC_POSSIBLE_xxx will be Hi priority.
1121ba9e82a1SKuninori Morimoto 	 * Basic/Default settings of each part and aboves are defined
1122ba9e82a1SKuninori Morimoto 	 * as Hi priority (= small number) of SND_SOC_POSSIBLE_xxx.
1123ba9e82a1SKuninori Morimoto 	 */
1124ba9e82a1SKuninori Morimoto 	for (i = 63; i >= 0; i--) {
1125ba9e82a1SKuninori Morimoto 		pos = 1ULL << i;
1126ba9e82a1SKuninori Morimoto 		switch (possible_fmt & pos) {
1127ba9e82a1SKuninori Morimoto 		/*
1128ba9e82a1SKuninori Morimoto 		 * for format
1129ba9e82a1SKuninori Morimoto 		 */
1130ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_I2S:
1131ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_RIGHT_J:
1132ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_LEFT_J:
1133ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_DSP_A:
1134ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_DSP_B:
1135ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_AC97:
1136ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_PDM:
1137ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_FORMAT_MASK) | i;
1138ba9e82a1SKuninori Morimoto 			break;
1139ba9e82a1SKuninori Morimoto 		/*
1140ba9e82a1SKuninori Morimoto 		 * for clock
1141ba9e82a1SKuninori Morimoto 		 */
1142ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_CONT:
1143ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_MASK) | SND_SOC_DAIFMT_CONT;
1144ba9e82a1SKuninori Morimoto 			break;
1145ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_GATED:
1146ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_MASK) | SND_SOC_DAIFMT_GATED;
1147ba9e82a1SKuninori Morimoto 			break;
1148ba9e82a1SKuninori Morimoto 		/*
1149ba9e82a1SKuninori Morimoto 		 * for clock invert
1150ba9e82a1SKuninori Morimoto 		 */
1151ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_NB_NF:
1152ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_NB_NF;
1153ba9e82a1SKuninori Morimoto 			break;
1154ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_NB_IF:
1155ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_NB_IF;
1156ba9e82a1SKuninori Morimoto 			break;
1157ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_IB_NF:
1158ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_IB_NF;
1159ba9e82a1SKuninori Morimoto 			break;
1160ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_IB_IF:
1161ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_IB_IF;
1162ba9e82a1SKuninori Morimoto 			break;
1163ba9e82a1SKuninori Morimoto 		/*
1164ba9e82a1SKuninori Morimoto 		 * for clock provider / consumer
1165ba9e82a1SKuninori Morimoto 		 */
1166ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_CBP_CFP:
1167ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBP_CFP;
1168ba9e82a1SKuninori Morimoto 			break;
1169ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_CBC_CFP:
1170ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBC_CFP;
1171ba9e82a1SKuninori Morimoto 			break;
1172ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_CBP_CFC:
1173ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBP_CFC;
1174ba9e82a1SKuninori Morimoto 			break;
1175ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_CBC_CFC:
1176ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBC_CFC;
1177ba9e82a1SKuninori Morimoto 			break;
1178ba9e82a1SKuninori Morimoto 		}
1179ba9e82a1SKuninori Morimoto 	}
1180ba9e82a1SKuninori Morimoto 
1181ba9e82a1SKuninori Morimoto 	/*
1182ba9e82a1SKuninori Morimoto 	 * Some driver might have very complex limitation.
1183ba9e82a1SKuninori Morimoto 	 * In such case, user want to auto-select non-limitation part,
1184ba9e82a1SKuninori Morimoto 	 * and want to manually specify complex part.
1185ba9e82a1SKuninori Morimoto 	 *
1186ba9e82a1SKuninori Morimoto 	 * Or for example, if both CPU and Codec can be clock provider,
1187ba9e82a1SKuninori Morimoto 	 * but because of its quality, user want to specify it manually.
1188ba9e82a1SKuninori Morimoto 	 *
1189ba9e82a1SKuninori Morimoto 	 * Use manually specified settings if sound card did.
1190ba9e82a1SKuninori Morimoto 	 */
1191ba9e82a1SKuninori Morimoto 	if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK))
1192ba9e82a1SKuninori Morimoto 		mask |= SND_SOC_DAIFMT_FORMAT_MASK;
1193ba9e82a1SKuninori Morimoto 	if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_CLOCK_MASK))
1194ba9e82a1SKuninori Morimoto 		mask |= SND_SOC_DAIFMT_CLOCK_MASK;
1195ba9e82a1SKuninori Morimoto 	if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_INV_MASK))
1196ba9e82a1SKuninori Morimoto 		mask |= SND_SOC_DAIFMT_INV_MASK;
1197640eac4cSKuninori Morimoto 	if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK))
1198640eac4cSKuninori Morimoto 		mask |= SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK;
1199ba9e82a1SKuninori Morimoto 
1200ba9e82a1SKuninori Morimoto 	dai_link->dai_fmt |= (dai_fmt & mask);
1201ba9e82a1SKuninori Morimoto }
1202ba9e82a1SKuninori Morimoto 
12034d1a98b5SKuninori Morimoto /**
12044d1a98b5SKuninori Morimoto  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
12054d1a98b5SKuninori Morimoto  * @rtd: The runtime for which the DAI link format should be changed
12064d1a98b5SKuninori Morimoto  * @dai_fmt: The new DAI link format
12074d1a98b5SKuninori Morimoto  *
12084d1a98b5SKuninori Morimoto  * This function updates the DAI link format for all DAIs connected to the DAI
12094d1a98b5SKuninori Morimoto  * link for the specified runtime.
12104d1a98b5SKuninori Morimoto  *
12114d1a98b5SKuninori Morimoto  * Note: For setups with a static format set the dai_fmt field in the
12124d1a98b5SKuninori Morimoto  * corresponding snd_dai_link struct instead of using this function.
12134d1a98b5SKuninori Morimoto  *
12144d1a98b5SKuninori Morimoto  * Returns 0 on success, otherwise a negative error code.
12154d1a98b5SKuninori Morimoto  */
12164d1a98b5SKuninori Morimoto int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
12174d1a98b5SKuninori Morimoto 				unsigned int dai_fmt)
12184d1a98b5SKuninori Morimoto {
12194d1a98b5SKuninori Morimoto 	struct snd_soc_dai *cpu_dai;
12204d1a98b5SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
12214d1a98b5SKuninori Morimoto 	unsigned int i;
12224d1a98b5SKuninori Morimoto 	int ret;
12234d1a98b5SKuninori Morimoto 
12247db07e37SKuninori Morimoto 	if (!dai_fmt)
12257db07e37SKuninori Morimoto 		return 0;
12267db07e37SKuninori Morimoto 
12274d1a98b5SKuninori Morimoto 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
12284d1a98b5SKuninori Morimoto 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
12294d1a98b5SKuninori Morimoto 		if (ret != 0 && ret != -ENOTSUPP)
12304d1a98b5SKuninori Morimoto 			return ret;
12314d1a98b5SKuninori Morimoto 	}
12324d1a98b5SKuninori Morimoto 
12336c076273SCharles Keepax 	/* Flip the polarity for the "CPU" end of link */
12346c076273SCharles Keepax 	dai_fmt = snd_soc_daifmt_clock_provider_flipped(dai_fmt);
1235b44a67f8SKuninori Morimoto 
12364d1a98b5SKuninori Morimoto 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
12376c076273SCharles Keepax 		ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
12384d1a98b5SKuninori Morimoto 		if (ret != 0 && ret != -ENOTSUPP)
12394d1a98b5SKuninori Morimoto 			return ret;
12404d1a98b5SKuninori Morimoto 	}
12414d1a98b5SKuninori Morimoto 
12424d1a98b5SKuninori Morimoto 	return 0;
12434d1a98b5SKuninori Morimoto }
12444d1a98b5SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
12454d1a98b5SKuninori Morimoto 
1246eaffeefbSKuninori Morimoto static int soc_init_pcm_runtime(struct snd_soc_card *card,
124746496acbSKuninori Morimoto 				struct snd_soc_pcm_runtime *rtd)
124846496acbSKuninori Morimoto {
124946496acbSKuninori Morimoto 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1250c2233a26SKuninori Morimoto 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
125146496acbSKuninori Morimoto 	struct snd_soc_component *component;
1252613fb500SKuninori Morimoto 	int ret, num, i;
125346496acbSKuninori Morimoto 
125446496acbSKuninori Morimoto 	/* do machine specific initialization */
125502e75636SKuninori Morimoto 	ret = snd_soc_link_init(rtd);
125602e75636SKuninori Morimoto 	if (ret < 0)
125746496acbSKuninori Morimoto 		return ret;
125846496acbSKuninori Morimoto 
1259ba9e82a1SKuninori Morimoto 	snd_soc_runtime_get_dai_fmt(rtd);
126046496acbSKuninori Morimoto 	ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
126146496acbSKuninori Morimoto 	if (ret)
126246496acbSKuninori Morimoto 		return ret;
126346496acbSKuninori Morimoto 
126446496acbSKuninori Morimoto 	/* add DPCM sysfs entries */
126546496acbSKuninori Morimoto 	soc_dpcm_debugfs_add(rtd);
126646496acbSKuninori Morimoto 
126746496acbSKuninori Morimoto 	num = rtd->num;
126846496acbSKuninori Morimoto 
126946496acbSKuninori Morimoto 	/*
127046496acbSKuninori Morimoto 	 * most drivers will register their PCMs using DAI link ordering but
127146496acbSKuninori Morimoto 	 * topology based drivers can use the DAI link id field to set PCM
127246496acbSKuninori Morimoto 	 * device number and then use rtd + a base offset of the BEs.
127346496acbSKuninori Morimoto 	 */
1274613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
127546496acbSKuninori Morimoto 		if (!component->driver->use_dai_pcm_id)
127646496acbSKuninori Morimoto 			continue;
127746496acbSKuninori Morimoto 
127846496acbSKuninori Morimoto 		if (rtd->dai_link->no_pcm)
127946496acbSKuninori Morimoto 			num += component->driver->be_pcm_base;
128046496acbSKuninori Morimoto 		else
128146496acbSKuninori Morimoto 			num = rtd->dai_link->id;
128246496acbSKuninori Morimoto 	}
128346496acbSKuninori Morimoto 
128446496acbSKuninori Morimoto 	/* create compress_device if possible */
128546496acbSKuninori Morimoto 	ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
128662462e01SKuninori Morimoto 	if (ret != -ENOTSUPP)
128746496acbSKuninori Morimoto 		return ret;
128846496acbSKuninori Morimoto 
128946496acbSKuninori Morimoto 	/* create the pcm */
129046496acbSKuninori Morimoto 	ret = soc_new_pcm(rtd, num);
129146496acbSKuninori Morimoto 	if (ret < 0) {
129246496acbSKuninori Morimoto 		dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
129346496acbSKuninori Morimoto 			dai_link->stream_name, ret);
129446496acbSKuninori Morimoto 		return ret;
129546496acbSKuninori Morimoto 	}
1296d1eb6d11SKuninori Morimoto 
12970b73ba55SKuninori Morimoto 	return snd_soc_pcm_dai_new(rtd);
129846496acbSKuninori Morimoto }
129946496acbSKuninori Morimoto 
1300ffd60fbaSKuninori Morimoto static void soc_set_name_prefix(struct snd_soc_card *card,
1301ffd60fbaSKuninori Morimoto 				struct snd_soc_component *component)
1302ffd60fbaSKuninori Morimoto {
1303aec3ff99SKuninori Morimoto 	struct device_node *of_node = soc_component_to_node(component);
130429d9fc7aSKuninori Morimoto 	const char *str;
130529d9fc7aSKuninori Morimoto 	int ret, i;
1306ffd60fbaSKuninori Morimoto 
13074702f991SKuninori Morimoto 	for (i = 0; i < card->num_configs; i++) {
1308ffd60fbaSKuninori Morimoto 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1309ffd60fbaSKuninori Morimoto 
13103256ef98SSameer Pujar 		if (snd_soc_is_matching_component(&map->dlc, component) &&
13113256ef98SSameer Pujar 		    map->name_prefix) {
1312ffd60fbaSKuninori Morimoto 			component->name_prefix = map->name_prefix;
1313ffd60fbaSKuninori Morimoto 			return;
1314ffd60fbaSKuninori Morimoto 		}
1315c13493a2SKuninori Morimoto 	}
1316ffd60fbaSKuninori Morimoto 
1317ffd60fbaSKuninori Morimoto 	/*
1318ffd60fbaSKuninori Morimoto 	 * If there is no configuration table or no match in the table,
1319ffd60fbaSKuninori Morimoto 	 * check if a prefix is provided in the node
1320ffd60fbaSKuninori Morimoto 	 */
132129d9fc7aSKuninori Morimoto 	ret = of_property_read_string(of_node, "sound-name-prefix", &str);
132229d9fc7aSKuninori Morimoto 	if (ret < 0)
132329d9fc7aSKuninori Morimoto 		return;
132429d9fc7aSKuninori Morimoto 
132529d9fc7aSKuninori Morimoto 	component->name_prefix = str;
1326ffd60fbaSKuninori Morimoto }
1327ffd60fbaSKuninori Morimoto 
1328c6619b72SKuninori Morimoto static void soc_remove_component(struct snd_soc_component *component,
1329c6619b72SKuninori Morimoto 				 int probed)
133022d14231SKuninori Morimoto {
1331c6619b72SKuninori Morimoto 
1332c6619b72SKuninori Morimoto 	if (!component->card)
1333c6619b72SKuninori Morimoto 		return;
1334c6619b72SKuninori Morimoto 
1335c6619b72SKuninori Morimoto 	if (probed)
1336c6619b72SKuninori Morimoto 		snd_soc_component_remove(component);
1337c6619b72SKuninori Morimoto 
13387a5d9815SBard liao 	list_del_init(&component->card_list);
133922d14231SKuninori Morimoto 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
134022d14231SKuninori Morimoto 	soc_cleanup_component_debugfs(component);
134122d14231SKuninori Morimoto 	component->card = NULL;
13420e36f36bSPierre-Louis Bossart 	snd_soc_component_module_put_when_remove(component);
134322d14231SKuninori Morimoto }
134422d14231SKuninori Morimoto 
1345ffd60fbaSKuninori Morimoto static int soc_probe_component(struct snd_soc_card *card,
1346ffd60fbaSKuninori Morimoto 			       struct snd_soc_component *component)
1347ffd60fbaSKuninori Morimoto {
1348ffd60fbaSKuninori Morimoto 	struct snd_soc_dapm_context *dapm =
1349ffd60fbaSKuninori Morimoto 		snd_soc_component_get_dapm(component);
1350ffd60fbaSKuninori Morimoto 	struct snd_soc_dai *dai;
1351c6619b72SKuninori Morimoto 	int probed = 0;
1352ffd60fbaSKuninori Morimoto 	int ret;
1353ffd60fbaSKuninori Morimoto 
13548f1a1681SKuninori Morimoto 	if (snd_soc_component_is_dummy(component))
1355ffd60fbaSKuninori Morimoto 		return 0;
1356ffd60fbaSKuninori Morimoto 
1357ffd60fbaSKuninori Morimoto 	if (component->card) {
1358ffd60fbaSKuninori Morimoto 		if (component->card != card) {
1359ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1360ffd60fbaSKuninori Morimoto 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1361ffd60fbaSKuninori Morimoto 				card->name, component->card->name);
1362ffd60fbaSKuninori Morimoto 			return -ENODEV;
1363ffd60fbaSKuninori Morimoto 		}
1364ffd60fbaSKuninori Morimoto 		return 0;
1365ffd60fbaSKuninori Morimoto 	}
1366ffd60fbaSKuninori Morimoto 
1367ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_module_get_when_probe(component);
1368ffd60fbaSKuninori Morimoto 	if (ret < 0)
1369ffd60fbaSKuninori Morimoto 		return ret;
1370ffd60fbaSKuninori Morimoto 
1371ffd60fbaSKuninori Morimoto 	component->card = card;
1372ffd60fbaSKuninori Morimoto 	soc_set_name_prefix(card, component);
1373ffd60fbaSKuninori Morimoto 
1374ffd60fbaSKuninori Morimoto 	soc_init_component_debugfs(component);
1375ffd60fbaSKuninori Morimoto 
137695c267ddSKuninori Morimoto 	snd_soc_dapm_init(dapm, card, component);
1377b614beafSKuninori Morimoto 
1378ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_new_controls(dapm,
1379ffd60fbaSKuninori Morimoto 					component->driver->dapm_widgets,
1380ffd60fbaSKuninori Morimoto 					component->driver->num_dapm_widgets);
1381ffd60fbaSKuninori Morimoto 
1382ffd60fbaSKuninori Morimoto 	if (ret != 0) {
1383ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1384ffd60fbaSKuninori Morimoto 			"Failed to create new controls %d\n", ret);
1385ffd60fbaSKuninori Morimoto 		goto err_probe;
1386ffd60fbaSKuninori Morimoto 	}
1387ffd60fbaSKuninori Morimoto 
1388ffd60fbaSKuninori Morimoto 	for_each_component_dais(component, dai) {
1389ffd60fbaSKuninori Morimoto 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1390ffd60fbaSKuninori Morimoto 		if (ret != 0) {
1391ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1392ffd60fbaSKuninori Morimoto 				"Failed to create DAI widgets %d\n", ret);
1393ffd60fbaSKuninori Morimoto 			goto err_probe;
1394ffd60fbaSKuninori Morimoto 		}
1395ffd60fbaSKuninori Morimoto 	}
1396ffd60fbaSKuninori Morimoto 
1397ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_probe(component);
139860adbd8fSKuninori Morimoto 	if (ret < 0)
1399ffd60fbaSKuninori Morimoto 		goto err_probe;
140060adbd8fSKuninori Morimoto 
1401ffd60fbaSKuninori Morimoto 	WARN(dapm->idle_bias_off &&
1402ffd60fbaSKuninori Morimoto 	     dapm->bias_level != SND_SOC_BIAS_OFF,
1403ffd60fbaSKuninori Morimoto 	     "codec %s can not start from non-off bias with idle_bias_off==1\n",
1404ffd60fbaSKuninori Morimoto 	     component->name);
1405c6619b72SKuninori Morimoto 	probed = 1;
1406ffd60fbaSKuninori Morimoto 
1407257c4dacSKuninori Morimoto 	/*
1408257c4dacSKuninori Morimoto 	 * machine specific init
1409257c4dacSKuninori Morimoto 	 * see
1410257c4dacSKuninori Morimoto 	 *	snd_soc_component_set_aux()
1411257c4dacSKuninori Morimoto 	 */
1412257c4dacSKuninori Morimoto 	ret = snd_soc_component_init(component);
1413257c4dacSKuninori Morimoto 	if (ret < 0)
1414ffd60fbaSKuninori Morimoto 		goto err_probe;
1415ffd60fbaSKuninori Morimoto 
1416ffd60fbaSKuninori Morimoto 	ret = snd_soc_add_component_controls(component,
1417ffd60fbaSKuninori Morimoto 					     component->driver->controls,
1418ffd60fbaSKuninori Morimoto 					     component->driver->num_controls);
1419ffd60fbaSKuninori Morimoto 	if (ret < 0)
1420ffd60fbaSKuninori Morimoto 		goto err_probe;
1421ffd60fbaSKuninori Morimoto 
1422ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(dapm,
1423ffd60fbaSKuninori Morimoto 				      component->driver->dapm_routes,
1424ffd60fbaSKuninori Morimoto 				      component->driver->num_dapm_routes);
1425a22ae72bSPierre-Louis Bossart 	if (ret < 0) {
1426a22ae72bSPierre-Louis Bossart 		if (card->disable_route_checks) {
1427a22ae72bSPierre-Louis Bossart 			dev_info(card->dev,
1428a22ae72bSPierre-Louis Bossart 				 "%s: disable_route_checks set, ignoring errors on add_routes\n",
1429a22ae72bSPierre-Louis Bossart 				 __func__);
1430a22ae72bSPierre-Louis Bossart 		} else {
1431a22ae72bSPierre-Louis Bossart 			dev_err(card->dev,
1432a22ae72bSPierre-Louis Bossart 				"%s: snd_soc_dapm_add_routes failed: %d\n",
1433a22ae72bSPierre-Louis Bossart 				__func__, ret);
1434ffd60fbaSKuninori Morimoto 			goto err_probe;
1435a22ae72bSPierre-Louis Bossart 		}
1436a22ae72bSPierre-Louis Bossart 	}
1437ffd60fbaSKuninori Morimoto 
1438ffd60fbaSKuninori Morimoto 	/* see for_each_card_components */
1439ffd60fbaSKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1440ffd60fbaSKuninori Morimoto 
1441ffd60fbaSKuninori Morimoto err_probe:
1442ffd60fbaSKuninori Morimoto 	if (ret < 0)
1443c6619b72SKuninori Morimoto 		soc_remove_component(component, probed);
1444ffd60fbaSKuninori Morimoto 
1445ffd60fbaSKuninori Morimoto 	return ret;
1446ffd60fbaSKuninori Morimoto }
1447ffd60fbaSKuninori Morimoto 
14484ca47d21SKuninori Morimoto static void soc_remove_link_dais(struct snd_soc_card *card)
1449f0fba2adSLiam Girdwood {
14504ca47d21SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
14514ca47d21SKuninori Morimoto 	int order;
14524ca47d21SKuninori Morimoto 
14534ca47d21SKuninori Morimoto 	for_each_comp_order(order) {
14544ca47d21SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
14557eaa313bSKuninori Morimoto 			/* remove all rtd connected DAIs in good order */
14567eaa313bSKuninori Morimoto 			snd_soc_pcm_dai_remove(rtd, order);
1457f0fba2adSLiam Girdwood 		}
14584ca47d21SKuninori Morimoto 	}
14594ca47d21SKuninori Morimoto }
1460f0fba2adSLiam Girdwood 
1461bc7c16c2SKuninori Morimoto static int soc_probe_link_dais(struct snd_soc_card *card)
1462bc7c16c2SKuninori Morimoto {
1463bc7c16c2SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
146451801aeaSKuninori Morimoto 	int order, ret;
1465bc7c16c2SKuninori Morimoto 
1466bc7c16c2SKuninori Morimoto 	for_each_comp_order(order) {
1467bc7c16c2SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
146851801aeaSKuninori Morimoto 			/* probe all rtd connected DAIs in good order */
146951801aeaSKuninori Morimoto 			ret = snd_soc_pcm_dai_probe(rtd, order);
1470bc7c16c2SKuninori Morimoto 			if (ret)
1471bc7c16c2SKuninori Morimoto 				return ret;
1472bc7c16c2SKuninori Morimoto 		}
1473bc7c16c2SKuninori Morimoto 	}
1474bc7c16c2SKuninori Morimoto 
1475bc7c16c2SKuninori Morimoto 	return 0;
1476bc7c16c2SKuninori Morimoto }
1477bc7c16c2SKuninori Morimoto 
1478b006c0c6SKuninori Morimoto static void soc_remove_link_components(struct snd_soc_card *card)
147962ae68faSStephen Warren {
148061aca564SLars-Peter Clausen 	struct snd_soc_component *component;
1481b006c0c6SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
1482613fb500SKuninori Morimoto 	int i, order;
148362ae68faSStephen Warren 
1484b006c0c6SKuninori Morimoto 	for_each_comp_order(order) {
1485b006c0c6SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1486613fb500SKuninori Morimoto 			for_each_rtd_components(rtd, i, component) {
1487b006c0c6SKuninori Morimoto 				if (component->driver->remove_order != order)
1488b006c0c6SKuninori Morimoto 					continue;
1489b006c0c6SKuninori Morimoto 
1490c6619b72SKuninori Morimoto 				soc_remove_component(component, 1);
149162ae68faSStephen Warren 			}
149262ae68faSStephen Warren 		}
1493b006c0c6SKuninori Morimoto 	}
1494b006c0c6SKuninori Morimoto }
149562ae68faSStephen Warren 
149662f07a6bSKuninori Morimoto static int soc_probe_link_components(struct snd_soc_card *card)
14976fb03550SKuninori Morimoto {
14986fb03550SKuninori Morimoto 	struct snd_soc_component *component;
149962f07a6bSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
1500613fb500SKuninori Morimoto 	int i, ret, order;
15016fb03550SKuninori Morimoto 
150262f07a6bSKuninori Morimoto 	for_each_comp_order(order) {
150362f07a6bSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1504613fb500SKuninori Morimoto 			for_each_rtd_components(rtd, i, component) {
150562f07a6bSKuninori Morimoto 				if (component->driver->probe_order != order)
150662f07a6bSKuninori Morimoto 					continue;
150762f07a6bSKuninori Morimoto 
15086fb03550SKuninori Morimoto 				ret = soc_probe_component(card, component);
15096fb03550SKuninori Morimoto 				if (ret < 0)
15106fb03550SKuninori Morimoto 					return ret;
15116fb03550SKuninori Morimoto 			}
15126fb03550SKuninori Morimoto 		}
151362f07a6bSKuninori Morimoto 	}
15146fb03550SKuninori Morimoto 
15156fb03550SKuninori Morimoto 	return 0;
15166fb03550SKuninori Morimoto }
15176fb03550SKuninori Morimoto 
1518e8fbd250SKuninori Morimoto static void soc_unbind_aux_dev(struct snd_soc_card *card)
15194893a2ebSKuninori Morimoto {
1520e8fbd250SKuninori Morimoto 	struct snd_soc_component *component, *_component;
1521e8fbd250SKuninori Morimoto 
1522e8fbd250SKuninori Morimoto 	for_each_card_auxs_safe(card, component, _component) {
1523257c4dacSKuninori Morimoto 		/* for snd_soc_component_init() */
1524257c4dacSKuninori Morimoto 		snd_soc_component_set_aux(component, NULL);
15254893a2ebSKuninori Morimoto 		list_del(&component->card_aux_list);
15264893a2ebSKuninori Morimoto 	}
1527e8fbd250SKuninori Morimoto }
15284893a2ebSKuninori Morimoto 
1529bee886f1SKuninori Morimoto static int soc_bind_aux_dev(struct snd_soc_card *card)
1530b19e6e7bSMark Brown {
1531f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1532bee886f1SKuninori Morimoto 	struct snd_soc_aux_dev *aux;
1533bee886f1SKuninori Morimoto 	int i;
15343ca041edSSebastian Reichel 
1535bee886f1SKuninori Morimoto 	for_each_card_pre_auxs(card, i, aux) {
1536f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1537bee886f1SKuninori Morimoto 		component = soc_find_component(&aux->dlc);
1538f2ed6b07SMengdong Lin 		if (!component)
15393dc29b8bSKuninori Morimoto 			return -EPROBE_DEFER;
15403ca041edSSebastian Reichel 
1541257c4dacSKuninori Morimoto 		/* for snd_soc_component_init() */
1542257c4dacSKuninori Morimoto 		snd_soc_component_set_aux(component, aux);
1543c2b71c71SKuninori Morimoto 		/* see for_each_card_auxs */
1544d2e3a135SSylwester Nawrocki 		list_add(&component->card_aux_list, &card->aux_comp_list);
1545bee886f1SKuninori Morimoto 	}
1546f2ed6b07SMengdong Lin 	return 0;
1547b19e6e7bSMark Brown }
1548b19e6e7bSMark Brown 
1549f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1550f2ed6b07SMengdong Lin {
155174bd3f92SKuninori Morimoto 	struct snd_soc_component *component;
1552f2ed6b07SMengdong Lin 	int order;
1553f2ed6b07SMengdong Lin 	int ret;
1554f2ed6b07SMengdong Lin 
15551a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
155674bd3f92SKuninori Morimoto 		for_each_card_auxs(card, component) {
155774bd3f92SKuninori Morimoto 			if (component->driver->probe_order != order)
155874bd3f92SKuninori Morimoto 				continue;
155974bd3f92SKuninori Morimoto 
156074bd3f92SKuninori Morimoto 			ret = soc_probe_component(card,	component);
156174bd3f92SKuninori Morimoto 			if (ret < 0)
1562f2ed6b07SMengdong Lin 				return ret;
1563f2ed6b07SMengdong Lin 		}
1564f2ed6b07SMengdong Lin 	}
156565d9361fSLars-Peter Clausen 
156644c69bb1SLars-Peter Clausen 	return 0;
15673ca041edSSebastian Reichel }
15682eea392dSJarkko Nikula 
1569f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
157044c69bb1SLars-Peter Clausen {
1571f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1572f2ed6b07SMengdong Lin 	int order;
157344c69bb1SLars-Peter Clausen 
15741a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1575c2b71c71SKuninori Morimoto 		for_each_card_auxs_safe(card, comp, _comp) {
1576e8fbd250SKuninori Morimoto 			if (comp->driver->remove_order == order)
1577c6619b72SKuninori Morimoto 				soc_remove_component(comp, 1);
15785f3484acSLars-Peter Clausen 		}
15792eea392dSJarkko Nikula 	}
15802eea392dSJarkko Nikula }
15812eea392dSJarkko Nikula 
15821f5a4535STakashi Iwai #ifdef CONFIG_DMI
15832c7b696aSMarcel Ziswiler /*
15848a6a6a38SKuninori Morimoto  * If a DMI filed contain strings in this blacklist (e.g.
15858a6a6a38SKuninori Morimoto  * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
15868a6a6a38SKuninori Morimoto  * as invalid and dropped when setting the card long name from DMI info.
15878a6a6a38SKuninori Morimoto  */
15888a6a6a38SKuninori Morimoto static const char * const dmi_blacklist[] = {
15898a6a6a38SKuninori Morimoto 	"To be filled by OEM",
15908a6a6a38SKuninori Morimoto 	"TBD by OEM",
15918a6a6a38SKuninori Morimoto 	"Default String",
15928a6a6a38SKuninori Morimoto 	"Board Manufacturer",
15938a6a6a38SKuninori Morimoto 	"Board Vendor Name",
15948a6a6a38SKuninori Morimoto 	"Board Product Name",
15958a6a6a38SKuninori Morimoto 	NULL,	/* terminator */
15968a6a6a38SKuninori Morimoto };
15978a6a6a38SKuninori Morimoto 
15988a6a6a38SKuninori Morimoto /*
15992c7b696aSMarcel Ziswiler  * Trim special characters, and replace '-' with '_' since '-' is used to
1600345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1601345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1602345233d7SLiam Girdwood  */
1603345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1604345233d7SLiam Girdwood {
1605345233d7SLiam Girdwood 	int i, j = 0;
1606345233d7SLiam Girdwood 
1607345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1608345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1609345233d7SLiam Girdwood 		    || (name[i] == '_'))
1610345233d7SLiam Girdwood 			name[j++] = name[i];
1611345233d7SLiam Girdwood 		else if (name[i] == '-')
1612345233d7SLiam Girdwood 			name[j++] = '_';
1613345233d7SLiam Girdwood 	}
1614345233d7SLiam Girdwood 
1615345233d7SLiam Girdwood 	name[j] = '\0';
1616345233d7SLiam Girdwood }
1617345233d7SLiam Girdwood 
16182c7b696aSMarcel Ziswiler /*
16192c7b696aSMarcel Ziswiler  * Check if a DMI field is valid, i.e. not containing any string
162098faf436SMengdong Lin  * in the black list.
162198faf436SMengdong Lin  */
162298faf436SMengdong Lin static int is_dmi_valid(const char *field)
162398faf436SMengdong Lin {
162498faf436SMengdong Lin 	int i = 0;
162598faf436SMengdong Lin 
162698faf436SMengdong Lin 	while (dmi_blacklist[i]) {
162798faf436SMengdong Lin 		if (strstr(field, dmi_blacklist[i]))
162898faf436SMengdong Lin 			return 0;
162998faf436SMengdong Lin 		i++;
163046b5a4d2SWu Fengguang 	}
163198faf436SMengdong Lin 
163298faf436SMengdong Lin 	return 1;
163398faf436SMengdong Lin }
163498faf436SMengdong Lin 
16354e01e5dbSJaroslav Kysela /*
16364e01e5dbSJaroslav Kysela  * Append a string to card->dmi_longname with character cleanups.
16374e01e5dbSJaroslav Kysela  */
16384e01e5dbSJaroslav Kysela static void append_dmi_string(struct snd_soc_card *card, const char *str)
16394e01e5dbSJaroslav Kysela {
16404e01e5dbSJaroslav Kysela 	char *dst = card->dmi_longname;
16414e01e5dbSJaroslav Kysela 	size_t dst_len = sizeof(card->dmi_longname);
16424e01e5dbSJaroslav Kysela 	size_t len;
16434e01e5dbSJaroslav Kysela 
16444e01e5dbSJaroslav Kysela 	len = strlen(dst);
16454e01e5dbSJaroslav Kysela 	snprintf(dst + len, dst_len - len, "-%s", str);
16464e01e5dbSJaroslav Kysela 
16474e01e5dbSJaroslav Kysela 	len++;	/* skip the separator "-" */
16484e01e5dbSJaroslav Kysela 	if (len < dst_len)
16494e01e5dbSJaroslav Kysela 		cleanup_dmi_name(dst + len);
16504e01e5dbSJaroslav Kysela }
16514e01e5dbSJaroslav Kysela 
1652345233d7SLiam Girdwood /**
1653345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
1654345233d7SLiam Girdwood  * @card: The card to register DMI names
1655345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
1656345233d7SLiam Girdwood  *
1657345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
1658345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
1659345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
1660345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
1661345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
1662345233d7SLiam Girdwood  * as the sound card long name, in the format of
1663345233d7SLiam Girdwood  * "vendor-product-version-board"
1664345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
1665345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
1666345233d7SLiam Girdwood  * (UCM) configurations for the card.
1667345233d7SLiam Girdwood  *
1668345233d7SLiam Girdwood  * Possible card long names may be:
1669345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
1670345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1671345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1672345233d7SLiam Girdwood  *
1673345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
1674345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
1675345233d7SLiam Girdwood  *
1676345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
1677345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
1678345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
1679345233d7SLiam Girdwood  * charactors like SPACE.
1680345233d7SLiam Girdwood  *
1681345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
1682345233d7SLiam Girdwood  */
1683345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1684345233d7SLiam Girdwood {
1685cdb76568SKuninori Morimoto 	const char *vendor, *product, *board;
1686345233d7SLiam Girdwood 
1687345233d7SLiam Girdwood 	if (card->long_name)
1688345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
1689345233d7SLiam Girdwood 
1690c68fded7SPierre-Louis Bossart 	if (!dmi_available)
16917de14d58SJon Hunter 		return 0;
16927de14d58SJon Hunter 
16934e01e5dbSJaroslav Kysela 	/* make up dmi long name as: vendor-product-version-board */
1694345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
169598faf436SMengdong Lin 	if (!vendor || !is_dmi_valid(vendor)) {
1696345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1697345233d7SLiam Girdwood 		return 0;
1698345233d7SLiam Girdwood 	}
1699345233d7SLiam Girdwood 
17004e01e5dbSJaroslav Kysela 	snprintf(card->dmi_longname, sizeof(card->dmi_longname), "%s", vendor);
1701345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
1702345233d7SLiam Girdwood 
1703345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
170498faf436SMengdong Lin 	if (product && is_dmi_valid(product)) {
1705cdb76568SKuninori Morimoto 		const char *product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
1706cdb76568SKuninori Morimoto 
17074e01e5dbSJaroslav Kysela 		append_dmi_string(card, product);
1708345233d7SLiam Girdwood 
17092c7b696aSMarcel Ziswiler 		/*
17102c7b696aSMarcel Ziswiler 		 * some vendors like Lenovo may only put a self-explanatory
1711345233d7SLiam Girdwood 		 * name in the product version field
1712345233d7SLiam Girdwood 		 */
17134e01e5dbSJaroslav Kysela 		if (product_version && is_dmi_valid(product_version))
17144e01e5dbSJaroslav Kysela 			append_dmi_string(card, product_version);
1715345233d7SLiam Girdwood 	}
1716345233d7SLiam Girdwood 
1717345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
171898faf436SMengdong Lin 	if (board && is_dmi_valid(board)) {
171939870b0dSJaroslav Kysela 		if (!product || strcasecmp(board, product))
17204e01e5dbSJaroslav Kysela 			append_dmi_string(card, board);
1721345233d7SLiam Girdwood 	} else if (!product) {
1722345233d7SLiam Girdwood 		/* fall back to using legacy name */
1723345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1724345233d7SLiam Girdwood 		return 0;
1725345233d7SLiam Girdwood 	}
1726345233d7SLiam Girdwood 
1727345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
17284e01e5dbSJaroslav Kysela 	if (flavour)
17294e01e5dbSJaroslav Kysela 		append_dmi_string(card, flavour);
1730345233d7SLiam Girdwood 
1731345233d7SLiam Girdwood 	/* set the card long name */
1732345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
1733345233d7SLiam Girdwood 
1734345233d7SLiam Girdwood 	return 0;
1735345233d7SLiam Girdwood }
1736345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
17371f5a4535STakashi Iwai #endif /* CONFIG_DMI */
1738345233d7SLiam Girdwood 
1739a655de80SLiam Girdwood static void soc_check_tplg_fes(struct snd_soc_card *card)
1740a655de80SLiam Girdwood {
1741a655de80SLiam Girdwood 	struct snd_soc_component *component;
1742a655de80SLiam Girdwood 	const struct snd_soc_component_driver *comp_drv;
1743a655de80SLiam Girdwood 	struct snd_soc_dai_link *dai_link;
1744a655de80SLiam Girdwood 	int i;
1745a655de80SLiam Girdwood 
1746368dee94SKuninori Morimoto 	for_each_component(component) {
1747a655de80SLiam Girdwood 
174849f9c4f2SDaniel Baluta 		/* does this component override BEs ? */
1749a655de80SLiam Girdwood 		if (!component->driver->ignore_machine)
1750a655de80SLiam Girdwood 			continue;
1751a655de80SLiam Girdwood 
1752a655de80SLiam Girdwood 		/* for this machine ? */
1753e194098bSPierre-Louis Bossart 		if (!strcmp(component->driver->ignore_machine,
1754a655de80SLiam Girdwood 			    card->dev->driver->name))
1755e194098bSPierre-Louis Bossart 			goto match;
1756e194098bSPierre-Louis Bossart 		if (strcmp(component->driver->ignore_machine,
1757e194098bSPierre-Louis Bossart 			   dev_name(card->dev)))
1758a655de80SLiam Girdwood 			continue;
1759e194098bSPierre-Louis Bossart match:
1760a655de80SLiam Girdwood 		/* machine matches, so override the rtd data */
17617fe072b4SKuninori Morimoto 		for_each_card_prelinks(card, i, dai_link) {
1762a655de80SLiam Girdwood 
1763a655de80SLiam Girdwood 			/* ignore this FE */
1764a655de80SLiam Girdwood 			if (dai_link->dynamic) {
1765a655de80SLiam Girdwood 				dai_link->ignore = true;
1766a655de80SLiam Girdwood 				continue;
1767a655de80SLiam Girdwood 			}
1768a655de80SLiam Girdwood 
17691328948fSPierre-Louis Bossart 			dev_dbg(card->dev, "info: override BE DAI link %s\n",
1770a655de80SLiam Girdwood 				card->dai_link[i].name);
1771a655de80SLiam Girdwood 
1772a655de80SLiam Girdwood 			/* override platform component */
1773adb76b5bSKuninori Morimoto 			if (!dai_link->platforms) {
1774daecf46eSKuninori Morimoto 				dev_err(card->dev, "init platform error");
1775daecf46eSKuninori Morimoto 				continue;
1776daecf46eSKuninori Morimoto 			}
1777d9714003SDaniel Baluta 
1778d9714003SDaniel Baluta 			if (component->dev->of_node)
1779d9714003SDaniel Baluta 				dai_link->platforms->of_node = component->dev->of_node;
1780d9714003SDaniel Baluta 			else
1781910fdcabSKuninori Morimoto 				dai_link->platforms->name = component->name;
1782a655de80SLiam Girdwood 
1783a655de80SLiam Girdwood 			/* convert non BE into BE */
1784607fa205SBard Liao 			if (!dai_link->no_pcm) {
1785a655de80SLiam Girdwood 				dai_link->no_pcm = 1;
1786607fa205SBard Liao 
1787607fa205SBard Liao 				if (dai_link->dpcm_playback)
1788607fa205SBard Liao 					dev_warn(card->dev,
1789607fa205SBard Liao 						 "invalid configuration, dailink %s has flags no_pcm=0 and dpcm_playback=1\n",
1790607fa205SBard Liao 						 dai_link->name);
1791607fa205SBard Liao 				if (dai_link->dpcm_capture)
1792607fa205SBard Liao 					dev_warn(card->dev,
1793607fa205SBard Liao 						 "invalid configuration, dailink %s has flags no_pcm=0 and dpcm_capture=1\n",
1794607fa205SBard Liao 						 dai_link->name);
1795607fa205SBard Liao 
1796607fa205SBard Liao 				/* convert normal link into DPCM one */
1797607fa205SBard Liao 				if (!(dai_link->dpcm_playback ||
1798607fa205SBard Liao 				      dai_link->dpcm_capture)) {
1799607fa205SBard Liao 					dai_link->dpcm_playback = !dai_link->capture_only;
1800607fa205SBard Liao 					dai_link->dpcm_capture = !dai_link->playback_only;
1801607fa205SBard Liao 				}
1802607fa205SBard Liao 			}
1803a655de80SLiam Girdwood 
18040cbbf8a0SKuninori Morimoto 			/*
18050cbbf8a0SKuninori Morimoto 			 * override any BE fixups
18060cbbf8a0SKuninori Morimoto 			 * see
18070cbbf8a0SKuninori Morimoto 			 *	snd_soc_link_be_hw_params_fixup()
18080cbbf8a0SKuninori Morimoto 			 */
1809a655de80SLiam Girdwood 			dai_link->be_hw_params_fixup =
1810a655de80SLiam Girdwood 				component->driver->be_hw_params_fixup;
1811a655de80SLiam Girdwood 
18122c7b696aSMarcel Ziswiler 			/*
18132c7b696aSMarcel Ziswiler 			 * most BE links don't set stream name, so set it to
1814a655de80SLiam Girdwood 			 * dai link name if it's NULL to help bind widgets.
1815a655de80SLiam Girdwood 			 */
1816a655de80SLiam Girdwood 			if (!dai_link->stream_name)
1817a655de80SLiam Girdwood 				dai_link->stream_name = dai_link->name;
1818a655de80SLiam Girdwood 		}
1819a655de80SLiam Girdwood 
1820a655de80SLiam Girdwood 		/* Inform userspace we are using alternate topology */
1821a655de80SLiam Girdwood 		if (component->driver->topology_name_prefix) {
1822a655de80SLiam Girdwood 
1823a655de80SLiam Girdwood 			/* topology shortname created? */
1824a655de80SLiam Girdwood 			if (!card->topology_shortname_created) {
1825a655de80SLiam Girdwood 				comp_drv = component->driver;
1826a655de80SLiam Girdwood 
1827a655de80SLiam Girdwood 				snprintf(card->topology_shortname, 32, "%s-%s",
1828a655de80SLiam Girdwood 					 comp_drv->topology_name_prefix,
1829a655de80SLiam Girdwood 					 card->name);
1830a655de80SLiam Girdwood 				card->topology_shortname_created = true;
1831a655de80SLiam Girdwood 			}
1832a655de80SLiam Girdwood 
1833a655de80SLiam Girdwood 			/* use topology shortname */
1834a655de80SLiam Girdwood 			card->name = card->topology_shortname;
1835a655de80SLiam Girdwood 		}
1836a655de80SLiam Girdwood 	}
1837a655de80SLiam Girdwood }
1838a655de80SLiam Girdwood 
1839c8d18e44SJaroslav Kysela #define soc_setup_card_name(card, name, name1, name2) \
1840c8d18e44SJaroslav Kysela 	__soc_setup_card_name(card, name, sizeof(name), name1, name2)
1841c8d18e44SJaroslav Kysela static void __soc_setup_card_name(struct snd_soc_card *card,
1842c8d18e44SJaroslav Kysela 				  char *name, int len,
1843c8d18e44SJaroslav Kysela 				  const char *name1, const char *name2)
18440f23f718SKuninori Morimoto {
1845c8d18e44SJaroslav Kysela 	const char *src = name1 ? name1 : name2;
18460f23f718SKuninori Morimoto 	int i;
18470f23f718SKuninori Morimoto 
1848c8d18e44SJaroslav Kysela 	snprintf(name, len, "%s", src);
18490f23f718SKuninori Morimoto 
1850c8d18e44SJaroslav Kysela 	if (name != card->snd_card->driver)
18510f23f718SKuninori Morimoto 		return;
18520f23f718SKuninori Morimoto 
18530f23f718SKuninori Morimoto 	/*
1854c8d18e44SJaroslav Kysela 	 * Name normalization (driver field)
18550f23f718SKuninori Morimoto 	 *
18560f23f718SKuninori Morimoto 	 * The driver name is somewhat special, as it's used as a key for
18570f23f718SKuninori Morimoto 	 * searches in the user-space.
18580f23f718SKuninori Morimoto 	 *
18590f23f718SKuninori Morimoto 	 * ex)
18600f23f718SKuninori Morimoto 	 *	"abcd??efg" -> "abcd__efg"
18610f23f718SKuninori Morimoto 	 */
18620f23f718SKuninori Morimoto 	for (i = 0; i < len; i++) {
18630f23f718SKuninori Morimoto 		switch (name[i]) {
18640f23f718SKuninori Morimoto 		case '_':
18650f23f718SKuninori Morimoto 		case '-':
18660f23f718SKuninori Morimoto 		case '\0':
18670f23f718SKuninori Morimoto 			break;
18680f23f718SKuninori Morimoto 		default:
18690f23f718SKuninori Morimoto 			if (!isalnum(name[i]))
18700f23f718SKuninori Morimoto 				name[i] = '_';
18710f23f718SKuninori Morimoto 			break;
18720f23f718SKuninori Morimoto 		}
18730f23f718SKuninori Morimoto 	}
1874c8d18e44SJaroslav Kysela 
1875c8d18e44SJaroslav Kysela 	/*
1876c8d18e44SJaroslav Kysela 	 * The driver field should contain a valid string from the user view.
1877c8d18e44SJaroslav Kysela 	 * The wrapping usually does not work so well here. Set a smaller string
1878c8d18e44SJaroslav Kysela 	 * in the specific ASoC driver.
1879c8d18e44SJaroslav Kysela 	 */
1880c8d18e44SJaroslav Kysela 	if (strlen(src) > len - 1)
1881c8d18e44SJaroslav Kysela 		dev_err(card->dev, "ASoC: driver name too long '%s' -> '%s'\n", src, name);
18820f23f718SKuninori Morimoto }
18830f23f718SKuninori Morimoto 
188427f07cacSKuninori Morimoto static void soc_cleanup_card_resources(struct snd_soc_card *card)
188553e947a0SKuninori Morimoto {
1886cc733900SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd, *n;
18877ce6088fSKuninori Morimoto 
18880ced7b05SKuninori Morimoto 	if (card->snd_card)
18890ced7b05SKuninori Morimoto 		snd_card_disconnect_sync(card->snd_card);
189053e947a0SKuninori Morimoto 
18912a6f0892SKuninori Morimoto 	snd_soc_dapm_shutdown(card);
18922a6f0892SKuninori Morimoto 
1893c7eb967dSCezary Rojewski 	/* release machine specific resources */
1894c7eb967dSCezary Rojewski 	for_each_card_rtds(card, rtd)
1895c7eb967dSCezary Rojewski 		snd_soc_link_exit(rtd);
189653e947a0SKuninori Morimoto 	/* remove and free each DAI */
18977ce6088fSKuninori Morimoto 	soc_remove_link_dais(card);
18980ced7b05SKuninori Morimoto 	soc_remove_link_components(card);
18997ce6088fSKuninori Morimoto 
1900cc733900SKuninori Morimoto 	for_each_card_rtds_safe(card, rtd, n)
190150cd9b53SKuninori Morimoto 		snd_soc_remove_pcm_runtime(card, rtd);
19027ce6088fSKuninori Morimoto 
190353e947a0SKuninori Morimoto 	/* remove auxiliary devices */
190453e947a0SKuninori Morimoto 	soc_remove_aux_devices(card);
1905e8fbd250SKuninori Morimoto 	soc_unbind_aux_dev(card);
190653e947a0SKuninori Morimoto 
190753e947a0SKuninori Morimoto 	snd_soc_dapm_free(&card->dapm);
190853e947a0SKuninori Morimoto 	soc_cleanup_card_debugfs(card);
190953e947a0SKuninori Morimoto 
191053e947a0SKuninori Morimoto 	/* remove the card */
1911b0275d95SKuninori Morimoto 	snd_soc_card_remove(card);
19120ced7b05SKuninori Morimoto 
19130ced7b05SKuninori Morimoto 	if (card->snd_card) {
19140ced7b05SKuninori Morimoto 		snd_card_free(card->snd_card);
19150ced7b05SKuninori Morimoto 		card->snd_card = NULL;
19160ced7b05SKuninori Morimoto 	}
191753e947a0SKuninori Morimoto }
191853e947a0SKuninori Morimoto 
19192cc1afcfSKuninori Morimoto static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
19202cc1afcfSKuninori Morimoto {
19219024bae4SKuninori Morimoto 	if (snd_soc_card_is_instantiated(card)) {
19222cc1afcfSKuninori Morimoto 		card->instantiated = false;
19232cc1afcfSKuninori Morimoto 		snd_soc_flush_all_delayed_work(card);
19242cc1afcfSKuninori Morimoto 
192527f07cacSKuninori Morimoto 		soc_cleanup_card_resources(card);
19262cc1afcfSKuninori Morimoto 		if (!unregister)
19272cc1afcfSKuninori Morimoto 			list_add(&card->list, &unbind_card_list);
19282cc1afcfSKuninori Morimoto 	} else {
19292cc1afcfSKuninori Morimoto 		if (unregister)
19302cc1afcfSKuninori Morimoto 			list_del(&card->list);
19312cc1afcfSKuninori Morimoto 	}
19322cc1afcfSKuninori Morimoto }
19332cc1afcfSKuninori Morimoto 
1934ed90c013SKuninori Morimoto static int snd_soc_bind_card(struct snd_soc_card *card)
1935f0fba2adSLiam Girdwood {
19361a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
193776c39e86SKuninori Morimoto 	struct snd_soc_component *component;
1938ffaf886eSKuninori Morimoto 	int ret;
193996dd3622SMark Brown 
194034e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
19410f3b8184SKuninori Morimoto 	snd_soc_card_mutex_lock_root(card);
1942f0fba2adSLiam Girdwood 
194395c267ddSKuninori Morimoto 	snd_soc_dapm_init(&card->dapm, card, NULL);
194453e947a0SKuninori Morimoto 
1945a655de80SLiam Girdwood 	/* check whether any platform is ignore machine FE and using topology */
1946a655de80SLiam Girdwood 	soc_check_tplg_fes(card);
1947a655de80SLiam Girdwood 
194844c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
1949bee886f1SKuninori Morimoto 	ret = soc_bind_aux_dev(card);
1950bee886f1SKuninori Morimoto 	if (ret < 0)
195153e947a0SKuninori Morimoto 		goto probe_end;
1952db2a4165SFrank Mandarino 
1953f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
1954d8145989SKuninori Morimoto 	card->num_rtd = 0;
1955ffaf886eSKuninori Morimoto 	ret = snd_soc_add_pcm_runtimes(card, card->dai_link, card->num_links);
19565b99a0aaSKuninori Morimoto 	if (ret < 0)
19575b99a0aaSKuninori Morimoto 		goto probe_end;
1958f8f80361SMengdong Lin 
1959f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1960102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1961f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1962f0fba2adSLiam Girdwood 	if (ret < 0) {
196310e8aa9aSMichał Mirosław 		dev_err(card->dev,
196410e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
196510e8aa9aSMichał Mirosław 			card->name, ret);
196653e947a0SKuninori Morimoto 		goto probe_end;
1967db2a4165SFrank Mandarino 	}
1968db2a4165SFrank Mandarino 
19690757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
19700757d834SLars-Peter Clausen 
1971b3da4251SKuninori Morimoto 	soc_resume_init(card);
19726ed25978SAndy Green 
1973b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
19749a841ebbSMark Brown 					card->num_dapm_widgets);
1975b8ba3b57SKuninori Morimoto 	if (ret < 0)
1976b8ba3b57SKuninori Morimoto 		goto probe_end;
19779a841ebbSMark Brown 
1978b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1979f23e860eSNicolin Chen 					card->num_of_dapm_widgets);
1980b8ba3b57SKuninori Morimoto 	if (ret < 0)
1981b8ba3b57SKuninori Morimoto 		goto probe_end;
1982f23e860eSNicolin Chen 
1983f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
198473de4b02SKuninori Morimoto 	ret = snd_soc_card_probe(card);
1985f0fba2adSLiam Girdwood 	if (ret < 0)
198653e947a0SKuninori Morimoto 		goto probe_end;
1987f0fba2adSLiam Girdwood 
198862ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
198962f07a6bSKuninori Morimoto 	ret = soc_probe_link_components(card);
199062ae68faSStephen Warren 	if (ret < 0) {
1991*f09b6e96SJohan Hovold 		if (ret != -EPROBE_DEFER) {
1992f110bfc7SLiam Girdwood 			dev_err(card->dev,
199362f07a6bSKuninori Morimoto 				"ASoC: failed to instantiate card %d\n", ret);
1994*f09b6e96SJohan Hovold 		}
199553e947a0SKuninori Morimoto 		goto probe_end;
199662ae68faSStephen Warren 	}
199762ae68faSStephen Warren 
1998f2ed6b07SMengdong Lin 	/* probe auxiliary components */
1999f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
200074bd3f92SKuninori Morimoto 	if (ret < 0) {
200174bd3f92SKuninori Morimoto 		dev_err(card->dev,
200274bd3f92SKuninori Morimoto 			"ASoC: failed to probe aux component %d\n", ret);
200353e947a0SKuninori Morimoto 		goto probe_end;
200474bd3f92SKuninori Morimoto 	}
2005f2ed6b07SMengdong Lin 
200662ae68faSStephen Warren 	/* probe all DAI links on this card */
2007c7e73774SKuninori Morimoto 	ret = soc_probe_link_dais(card);
2008fe3e78e0SMark Brown 	if (ret < 0) {
2009f110bfc7SLiam Girdwood 		dev_err(card->dev,
2010c7e73774SKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
201153e947a0SKuninori Morimoto 		goto probe_end;
2012fe3e78e0SMark Brown 	}
2013fe3e78e0SMark Brown 
2014626c2e57SKuninori Morimoto 	for_each_card_rtds(card, rtd) {
2015eaffeefbSKuninori Morimoto 		ret = soc_init_pcm_runtime(card, rtd);
2016626c2e57SKuninori Morimoto 		if (ret < 0)
2017626c2e57SKuninori Morimoto 			goto probe_end;
2018626c2e57SKuninori Morimoto 	}
2019c4b46982SKuninori Morimoto 
2020888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
2021b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
2022888df395SMark Brown 
20239b98c7c2SKuninori Morimoto 	ret = snd_soc_add_card_controls(card, card->controls,
20242c7b696aSMarcel Ziswiler 					card->num_controls);
20259b98c7c2SKuninori Morimoto 	if (ret < 0)
20269b98c7c2SKuninori Morimoto 		goto probe_end;
2027b7af1dafSMark Brown 
2028daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2029b8ad29deSMark Brown 				      card->num_dapm_routes);
2030a22ae72bSPierre-Louis Bossart 	if (ret < 0) {
2031a22ae72bSPierre-Louis Bossart 		if (card->disable_route_checks) {
2032a22ae72bSPierre-Louis Bossart 			dev_info(card->dev,
2033a22ae72bSPierre-Louis Bossart 				 "%s: disable_route_checks set, ignoring errors on add_routes\n",
2034a22ae72bSPierre-Louis Bossart 				 __func__);
2035a22ae72bSPierre-Louis Bossart 		} else {
2036a22ae72bSPierre-Louis Bossart 			dev_err(card->dev,
2037a22ae72bSPierre-Louis Bossart 				 "%s: snd_soc_dapm_add_routes failed: %d\n",
2038a22ae72bSPierre-Louis Bossart 				 __func__, ret);
2039daa480bdSKuninori Morimoto 			goto probe_end;
2040a22ae72bSPierre-Louis Bossart 		}
2041a22ae72bSPierre-Louis Bossart 	}
2042b8ad29deSMark Brown 
2043daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2044f23e860eSNicolin Chen 				      card->num_of_dapm_routes);
2045daa480bdSKuninori Morimoto 	if (ret < 0)
2046daa480bdSKuninori Morimoto 		goto probe_end;
204775d9ac46SMark Brown 
2048861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
2049861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
2050861886d3STakashi Iwai 
2051c8d18e44SJaroslav Kysela 	soc_setup_card_name(card, card->snd_card->shortname,
2052c8d18e44SJaroslav Kysela 			    card->name, NULL);
2053c8d18e44SJaroslav Kysela 	soc_setup_card_name(card, card->snd_card->longname,
2054c8d18e44SJaroslav Kysela 			    card->long_name, card->name);
2055c8d18e44SJaroslav Kysela 	soc_setup_card_name(card, card->snd_card->driver,
2056c8d18e44SJaroslav Kysela 			    card->driver_name, card->name);
2057fe3e78e0SMark Brown 
2058dc73d73aSJaroslav Kysela 	if (card->components) {
2059dc73d73aSJaroslav Kysela 		/* the current implementation of snd_component_add() accepts */
2060dc73d73aSJaroslav Kysela 		/* multiple components in the string separated by space, */
2061dc73d73aSJaroslav Kysela 		/* but the string collision (identical string) check might */
2062dc73d73aSJaroslav Kysela 		/* not work correctly */
2063dc73d73aSJaroslav Kysela 		ret = snd_component_add(card->snd_card, card->components);
2064dc73d73aSJaroslav Kysela 		if (ret < 0) {
2065dc73d73aSJaroslav Kysela 			dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n",
2066dc73d73aSJaroslav Kysela 				card->name, ret);
2067dc73d73aSJaroslav Kysela 			goto probe_end;
2068dc73d73aSJaroslav Kysela 		}
2069dc73d73aSJaroslav Kysela 	}
2070dc73d73aSJaroslav Kysela 
20715c0eac03SKuninori Morimoto 	ret = snd_soc_card_late_probe(card);
20725c0eac03SKuninori Morimoto 	if (ret < 0)
207353e947a0SKuninori Morimoto 		goto probe_end;
207428e9ad92SMark Brown 
2075824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
2076df4d27b1SMartin Povišer 	snd_soc_card_fixup_controls(card);
20778c193b8dSLars-Peter Clausen 
2078f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2079fe3e78e0SMark Brown 	if (ret < 0) {
2080f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2081f110bfc7SLiam Girdwood 				ret);
208253e947a0SKuninori Morimoto 		goto probe_end;
2083fe3e78e0SMark Brown 	}
2084fe3e78e0SMark Brown 
2085435c5e25SMark Brown 	card->instantiated = 1;
2086882eab6cSTzung-Bi Shih 	dapm_mark_endpoints_dirty(card);
20874f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2088b19e6e7bSMark Brown 
2089ed90c013SKuninori Morimoto 	/* deactivate pins to sleep state */
209076c39e86SKuninori Morimoto 	for_each_card_components(card, component)
2091b3dea624SKuninori Morimoto 		if (!snd_soc_component_active(component))
209276c39e86SKuninori Morimoto 			pinctrl_pm_select_sleep_state(component->dev);
2093ed90c013SKuninori Morimoto 
209453e947a0SKuninori Morimoto probe_end:
209553e947a0SKuninori Morimoto 	if (ret < 0)
209627f07cacSKuninori Morimoto 		soc_cleanup_card_resources(card);
2097db2a4165SFrank Mandarino 
20980f3b8184SKuninori Morimoto 	snd_soc_card_mutex_unlock(card);
209934e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2100db2a4165SFrank Mandarino 
2101b19e6e7bSMark Brown 	return ret;
2102435c5e25SMark Brown }
2103435c5e25SMark Brown 
2104435c5e25SMark Brown /* probes a new socdev */
2105435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2106435c5e25SMark Brown {
2107f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2108435c5e25SMark Brown 
210970a7ca34SVinod Koul 	/*
211070a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
211170a7ca34SVinod Koul 	 * we should not be here in that case so ret error
211270a7ca34SVinod Koul 	 */
211370a7ca34SVinod Koul 	if (!card)
211470a7ca34SVinod Koul 		return -EINVAL;
211570a7ca34SVinod Koul 
2116fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2117f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2118fe4085e8SMark Brown 		 card->name);
2119fe4085e8SMark Brown 
2120435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2121435c5e25SMark Brown 	card->dev = &pdev->dev;
2122f0fba2adSLiam Girdwood 
2123ad61b78eSQinglang Miao 	return devm_snd_soc_register_card(&pdev->dev, card);
2124db2a4165SFrank Mandarino }
2125db2a4165SFrank Mandarino 
21266f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
212751737470SMark Brown {
21286f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
212976c39e86SKuninori Morimoto 	struct snd_soc_component *component;
213051737470SMark Brown 
21319024bae4SKuninori Morimoto 	if (!snd_soc_card_is_instantiated(card))
2132416356fcSMark Brown 		return 0;
213351737470SMark Brown 
21342c7b696aSMarcel Ziswiler 	/*
21352c7b696aSMarcel Ziswiler 	 * Flush out pmdown_time work - we actually do want to run it
21362c7b696aSMarcel Ziswiler 	 * now, we're shutting down so no imminent restart.
21372c7b696aSMarcel Ziswiler 	 */
213865462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
213951737470SMark Brown 
2140f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2141416356fcSMark Brown 
2142988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
214376c39e86SKuninori Morimoto 	for_each_card_components(card, component)
214476c39e86SKuninori Morimoto 		pinctrl_pm_select_sleep_state(component->dev);
2145988e8cc4SNicolin Chen 
2146416356fcSMark Brown 	return 0;
214751737470SMark Brown }
21486f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
214951737470SMark Brown 
21506f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2151b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2152b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2153b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2154b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
21556f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2156b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2157416356fcSMark Brown };
2158deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2159416356fcSMark Brown 
2160db2a4165SFrank Mandarino /* ASoC platform driver */
2161db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2162db2a4165SFrank Mandarino 	.driver		= {
2163db2a4165SFrank Mandarino 		.name		= "soc-audio",
21646f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2165db2a4165SFrank Mandarino 	},
2166db2a4165SFrank Mandarino 	.probe		= soc_probe,
2167db2a4165SFrank Mandarino };
2168db2a4165SFrank Mandarino 
2169096e49d5SMark Brown /**
2170db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2171db2a4165SFrank Mandarino  * @_template: control template
2172db2a4165SFrank Mandarino  * @data: control private data
2173ac11a2b3SMark Brown  * @long_name: control long name
2174efb7ac3fSMark Brown  * @prefix: control name prefix
2175db2a4165SFrank Mandarino  *
2176db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2177db2a4165SFrank Mandarino  *
2178db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2179db2a4165SFrank Mandarino  */
2180db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
21813056557fSMark Brown 				  void *data, const char *long_name,
2182efb7ac3fSMark Brown 				  const char *prefix)
2183db2a4165SFrank Mandarino {
2184db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2185efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2186efb7ac3fSMark Brown 	char *name = NULL;
2187db2a4165SFrank Mandarino 
2188db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2189db2a4165SFrank Mandarino 	template.index = 0;
2190db2a4165SFrank Mandarino 
2191efb7ac3fSMark Brown 	if (!long_name)
2192efb7ac3fSMark Brown 		long_name = template.name;
2193efb7ac3fSMark Brown 
2194efb7ac3fSMark Brown 	if (prefix) {
21952b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2196efb7ac3fSMark Brown 		if (!name)
2197efb7ac3fSMark Brown 			return NULL;
2198efb7ac3fSMark Brown 
2199efb7ac3fSMark Brown 		template.name = name;
2200efb7ac3fSMark Brown 	} else {
2201efb7ac3fSMark Brown 		template.name = long_name;
2202efb7ac3fSMark Brown 	}
2203efb7ac3fSMark Brown 
2204efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2205efb7ac3fSMark Brown 
2206efb7ac3fSMark Brown 	kfree(name);
2207efb7ac3fSMark Brown 
2208efb7ac3fSMark Brown 	return kcontrol;
2209db2a4165SFrank Mandarino }
2210db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2211db2a4165SFrank Mandarino 
2212022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2213022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2214022658beSLiam Girdwood 	const char *prefix, void *data)
2215022658beSLiam Girdwood {
2216eaf2469cSKuninori Morimoto 	int i;
2217022658beSLiam Girdwood 
2218022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2219022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2220eaf2469cSKuninori Morimoto 		int err = snd_ctl_add(card, snd_soc_cnew(control, data,
2221022658beSLiam Girdwood 							 control->name, prefix));
2222022658beSLiam Girdwood 		if (err < 0) {
2223f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2224f110bfc7SLiam Girdwood 				control->name, err);
2225022658beSLiam Girdwood 			return err;
2226022658beSLiam Girdwood 		}
2227022658beSLiam Girdwood 	}
2228022658beSLiam Girdwood 
2229022658beSLiam Girdwood 	return 0;
2230022658beSLiam Girdwood }
2231022658beSLiam Girdwood 
2232db2a4165SFrank Mandarino /**
22330f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
22340f2780adSLars-Peter Clausen  *
22350f2780adSLars-Peter Clausen  * @component: Component to add controls to
22360f2780adSLars-Peter Clausen  * @controls: Array of controls to add
22370f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
22380f2780adSLars-Peter Clausen  *
22390f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
22400f2780adSLars-Peter Clausen  */
22410f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
22420f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
22430f2780adSLars-Peter Clausen {
22440f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
22450f2780adSLars-Peter Clausen 
22460f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
22470f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
22480f2780adSLars-Peter Clausen }
22490f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
22500f2780adSLars-Peter Clausen 
22510f2780adSLars-Peter Clausen /**
2252022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2253022658beSLiam Girdwood  * Convenience function to add a list of controls.
2254022658beSLiam Girdwood  *
2255022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2256022658beSLiam Girdwood  * @controls: array of controls to add
2257022658beSLiam Girdwood  * @num_controls: number of elements in the array
2258022658beSLiam Girdwood  *
2259022658beSLiam Girdwood  * Return 0 for success, else error.
2260022658beSLiam Girdwood  */
2261022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2262022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2263022658beSLiam Girdwood {
2264022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2265022658beSLiam Girdwood 
2266022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2267022658beSLiam Girdwood 			NULL, soc_card);
2268022658beSLiam Girdwood }
2269022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2270022658beSLiam Girdwood 
2271022658beSLiam Girdwood /**
2272022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2273022658beSLiam Girdwood  * Convienience function to add a list of controls.
2274022658beSLiam Girdwood  *
2275022658beSLiam Girdwood  * @dai: DAI to add controls to
2276022658beSLiam Girdwood  * @controls: array of controls to add
2277022658beSLiam Girdwood  * @num_controls: number of elements in the array
2278022658beSLiam Girdwood  *
2279022658beSLiam Girdwood  * Return 0 for success, else error.
2280022658beSLiam Girdwood  */
2281022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2282022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2283022658beSLiam Girdwood {
2284313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2285022658beSLiam Girdwood 
2286022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2287022658beSLiam Girdwood 			NULL, dai);
2288022658beSLiam Girdwood }
2289022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2290022658beSLiam Girdwood 
2291c5af3a2eSMark Brown /**
2292c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2293c5af3a2eSMark Brown  *
2294ac11a2b3SMark Brown  * @card: Card to register
2295c5af3a2eSMark Brown  *
2296c5af3a2eSMark Brown  */
229770a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2298c5af3a2eSMark Brown {
2299c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2300c5af3a2eSMark Brown 		return -EINVAL;
2301c5af3a2eSMark Brown 
2302ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2303ed77cc12SMark Brown 
2304b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->widgets);
2305b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->paths);
2306b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->dapm_list);
2307b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->aux_comp_list);
2308b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->component_dev_list);
2309b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->list);
23101a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
2311db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
23128a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2313b03bfaecSKuninori Morimoto 
2314c5af3a2eSMark Brown 	card->instantiated = 0;
2315f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
2316a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
231772b745e3SPeter Ujfalusi 	mutex_init(&card->pcm_mutex);
2318c5af3a2eSMark Brown 
2319e894efefSSrinivas Kandagatla 	return snd_soc_bind_card(card);
2320c5af3a2eSMark Brown }
232170a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2322c5af3a2eSMark Brown 
2323c5af3a2eSMark Brown /**
2324c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2325c5af3a2eSMark Brown  *
2326ac11a2b3SMark Brown  * @card: Card to unregister
2327c5af3a2eSMark Brown  *
2328c5af3a2eSMark Brown  */
23291892a991SUwe Kleine-König void snd_soc_unregister_card(struct snd_soc_card *card)
2330c5af3a2eSMark Brown {
2331b545542aSKuninori Morimoto 	mutex_lock(&client_mutex);
2332e894efefSSrinivas Kandagatla 	snd_soc_unbind_card(card, true);
2333b545542aSKuninori Morimoto 	mutex_unlock(&client_mutex);
2334f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2335c5af3a2eSMark Brown }
233670a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2337c5af3a2eSMark Brown 
2338f0fba2adSLiam Girdwood /*
2339f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
2340f0fba2adSLiam Girdwood  * and sanitizing names.
2341f0fba2adSLiam Girdwood  */
23420b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2343f0fba2adSLiam Girdwood {
234445dd9943SDmitry Baryshkov 	const char *devname = dev_name(dev);
234545dd9943SDmitry Baryshkov 	char *found, *name;
2346d3b47a88SPierre-Louis Bossart 	unsigned int id1, id2;
2347f0fba2adSLiam Girdwood 
234845dd9943SDmitry Baryshkov 	if (devname == NULL)
2349f0fba2adSLiam Girdwood 		return NULL;
2350f0fba2adSLiam Girdwood 
235145dd9943SDmitry Baryshkov 	name = devm_kstrdup(dev, devname, GFP_KERNEL);
235241daf6baSKefeng Wang 	if (!name)
235341daf6baSKefeng Wang 		return NULL;
2354f0fba2adSLiam Girdwood 
2355f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
2356f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
2357f0fba2adSLiam Girdwood 	if (found) {
2358f0fba2adSLiam Girdwood 		/* get ID */
2359f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2360f0fba2adSLiam Girdwood 
2361f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
2362f0fba2adSLiam Girdwood 			if (*id == -1)
2363f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
2364f0fba2adSLiam Girdwood 		}
2365f0fba2adSLiam Girdwood 
2366f0fba2adSLiam Girdwood 	/* I2C component devices are named "bus-addr" */
236745dd9943SDmitry Baryshkov 	} else if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2368f0fba2adSLiam Girdwood 
2369f0fba2adSLiam Girdwood 		/* create unique ID number from I2C addr and bus */
237005899446SJarkko Nikula 		*id = ((id1 & 0xffff) << 16) + id2;
2371f0fba2adSLiam Girdwood 
237245dd9943SDmitry Baryshkov 		devm_kfree(dev, name);
237345dd9943SDmitry Baryshkov 
2374f0fba2adSLiam Girdwood 		/* sanitize component name for DAI link creation */
237545dd9943SDmitry Baryshkov 		name = devm_kasprintf(dev, GFP_KERNEL, "%s.%s", dev->driver->name, devname);
237645dd9943SDmitry Baryshkov 	} else {
2377f0fba2adSLiam Girdwood 		*id = 0;
2378f0fba2adSLiam Girdwood 	}
2379f0fba2adSLiam Girdwood 
238045dd9943SDmitry Baryshkov 	return name;
2381f0fba2adSLiam Girdwood }
2382f0fba2adSLiam Girdwood 
2383f0fba2adSLiam Girdwood /*
2384f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
2385f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
2386f0fba2adSLiam Girdwood  */
2387f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
2388f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
2389f0fba2adSLiam Girdwood {
2390f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
239110e8aa9aSMichał Mirosław 		dev_err(dev,
239210e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
239310e8aa9aSMichał Mirosław 			dev_name(dev));
2394f0fba2adSLiam Girdwood 		return NULL;
2395f0fba2adSLiam Girdwood 	}
2396f0fba2adSLiam Girdwood 
239750014499SKuninori Morimoto 	return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2398f0fba2adSLiam Girdwood }
2399f0fba2adSLiam Girdwood 
2400ffdbca0bSKuninori Morimoto void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2401e11381f3SKuninori Morimoto {
2402e11381f3SKuninori Morimoto 	dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2403e11381f3SKuninori Morimoto 	list_del(&dai->list);
2404e11381f3SKuninori Morimoto }
2405ffdbca0bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2406e11381f3SKuninori Morimoto 
24077ca24386SKuninori Morimoto /**
24087ca24386SKuninori Morimoto  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
24097ca24386SKuninori Morimoto  *
24107ca24386SKuninori Morimoto  * @component: The component the DAIs are registered for
24117ca24386SKuninori Morimoto  * @dai_drv: DAI driver to use for the DAI
2412bc9a6655SRandy Dunlap  * @legacy_dai_naming: if %true, use legacy single-name format;
2413bc9a6655SRandy Dunlap  * 	if %false, use multiple-name format;
24147ca24386SKuninori Morimoto  *
24157ca24386SKuninori Morimoto  * Topology can use this API to register DAIs when probing a component.
24167ca24386SKuninori Morimoto  * These DAIs's widgets will be freed in the card cleanup and the DAIs
24177ca24386SKuninori Morimoto  * will be freed in the component cleanup.
24187ca24386SKuninori Morimoto  */
24197ca24386SKuninori Morimoto struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
24205e4fb372SMengdong Lin 					 struct snd_soc_dai_driver *dai_drv,
24215e4fb372SMengdong Lin 					 bool legacy_dai_naming)
24225e4fb372SMengdong Lin {
24235e4fb372SMengdong Lin 	struct device *dev = component->dev;
24245e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
24255e4fb372SMengdong Lin 
24267ca24386SKuninori Morimoto 	lockdep_assert_held(&client_mutex);
24277ca24386SKuninori Morimoto 
242850014499SKuninori Morimoto 	dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
24295e4fb372SMengdong Lin 	if (dai == NULL)
24305e4fb372SMengdong Lin 		return NULL;
24315e4fb372SMengdong Lin 
24325e4fb372SMengdong Lin 	/*
24335e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
24345e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
24355e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
24365e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
24375e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
24385e4fb372SMengdong Lin 	 * component-less anymore.
24395e4fb372SMengdong Lin 	 */
24405e4fb372SMengdong Lin 	if (legacy_dai_naming &&
24415e4fb372SMengdong Lin 	    (dai_drv->id == 0 || dai_drv->name == NULL)) {
24425e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
24435e4fb372SMengdong Lin 	} else {
24445e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
24455e4fb372SMengdong Lin 		if (dai_drv->id)
24465e4fb372SMengdong Lin 			dai->id = dai_drv->id;
24475e4fb372SMengdong Lin 		else
24485e4fb372SMengdong Lin 			dai->id = component->num_dai;
24495e4fb372SMengdong Lin 	}
245050014499SKuninori Morimoto 	if (!dai->name)
24515e4fb372SMengdong Lin 		return NULL;
24525e4fb372SMengdong Lin 
24535e4fb372SMengdong Lin 	dai->component = component;
24545e4fb372SMengdong Lin 	dai->dev = dev;
24555e4fb372SMengdong Lin 	dai->driver = dai_drv;
24565e4fb372SMengdong Lin 
245715a0c645SKuninori Morimoto 	/* see for_each_component_dais */
245858bf4179SKuninori Morimoto 	list_add_tail(&dai->list, &component->dai_list);
24595e4fb372SMengdong Lin 	component->num_dai++;
24605e4fb372SMengdong Lin 
24615e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
24625e4fb372SMengdong Lin 	return dai;
24635e4fb372SMengdong Lin }
2464da039809SCezary Rojewski EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2465e11381f3SKuninori Morimoto 
24669115171aSMark Brown /**
2467f7b6603cSMauro Carvalho Chehab  * snd_soc_unregister_dais - Unregister DAIs from the ASoC core
24683f6674aeSKuninori Morimoto  *
24693f6674aeSKuninori Morimoto  * @component: The component for which the DAIs should be unregistered
24703f6674aeSKuninori Morimoto  */
24713f6674aeSKuninori Morimoto static void snd_soc_unregister_dais(struct snd_soc_component *component)
24723f6674aeSKuninori Morimoto {
24733f6674aeSKuninori Morimoto 	struct snd_soc_dai *dai, *_dai;
24743f6674aeSKuninori Morimoto 
2475e11381f3SKuninori Morimoto 	for_each_component_dais_safe(component, dai, _dai)
2476e11381f3SKuninori Morimoto 		snd_soc_unregister_dai(dai);
24773f6674aeSKuninori Morimoto }
24783f6674aeSKuninori Morimoto 
24793f6674aeSKuninori Morimoto /**
2480daf77373SKuninori Morimoto  * snd_soc_register_dais - Register a DAI with the ASoC core
2481daf77373SKuninori Morimoto  *
2482daf77373SKuninori Morimoto  * @component: The component the DAIs are registered for
2483daf77373SKuninori Morimoto  * @dai_drv: DAI driver to use for the DAIs
2484daf77373SKuninori Morimoto  * @count: Number of DAIs
2485daf77373SKuninori Morimoto  */
2486daf77373SKuninori Morimoto static int snd_soc_register_dais(struct snd_soc_component *component,
2487daf77373SKuninori Morimoto 				 struct snd_soc_dai_driver *dai_drv,
2488daf77373SKuninori Morimoto 				 size_t count)
2489daf77373SKuninori Morimoto {
2490daf77373SKuninori Morimoto 	struct snd_soc_dai *dai;
2491daf77373SKuninori Morimoto 	unsigned int i;
2492daf77373SKuninori Morimoto 	int ret;
2493daf77373SKuninori Morimoto 
2494daf77373SKuninori Morimoto 	for (i = 0; i < count; i++) {
249571cb85f5SKuninori Morimoto 		dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
2496129f055aSCharles Keepax 					   component->driver->legacy_dai_naming);
2497daf77373SKuninori Morimoto 		if (dai == NULL) {
2498daf77373SKuninori Morimoto 			ret = -ENOMEM;
2499daf77373SKuninori Morimoto 			goto err;
2500daf77373SKuninori Morimoto 		}
2501daf77373SKuninori Morimoto 	}
2502daf77373SKuninori Morimoto 
2503daf77373SKuninori Morimoto 	return 0;
2504daf77373SKuninori Morimoto 
2505daf77373SKuninori Morimoto err:
2506daf77373SKuninori Morimoto 	snd_soc_unregister_dais(component);
2507daf77373SKuninori Morimoto 
2508daf77373SKuninori Morimoto 	return ret;
2509daf77373SKuninori Morimoto }
2510daf77373SKuninori Morimoto 
2511273d778eSKuninori Morimoto #define ENDIANNESS_MAP(name) \
2512273d778eSKuninori Morimoto 	(SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2513273d778eSKuninori Morimoto static u64 endianness_format_map[] = {
2514273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S16_),
2515273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U16_),
2516273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_),
2517273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_),
2518273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S32_),
2519273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U32_),
2520273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_3),
2521273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_3),
2522273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S20_3),
2523273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U20_3),
2524273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S18_3),
2525273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U18_3),
2526273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT_),
2527273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT64_),
2528273d778eSKuninori Morimoto 	ENDIANNESS_MAP(IEC958_SUBFRAME_),
2529273d778eSKuninori Morimoto };
2530273d778eSKuninori Morimoto 
2531273d778eSKuninori Morimoto /*
2532273d778eSKuninori Morimoto  * Fix up the DAI formats for endianness: codecs don't actually see
2533273d778eSKuninori Morimoto  * the endianness of the data but we're using the CPU format
2534273d778eSKuninori Morimoto  * definitions which do need to include endianness so we ensure that
2535273d778eSKuninori Morimoto  * codec DAIs always have both big and little endian variants set.
2536273d778eSKuninori Morimoto  */
2537273d778eSKuninori Morimoto static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2538273d778eSKuninori Morimoto {
2539273d778eSKuninori Morimoto 	int i;
2540273d778eSKuninori Morimoto 
2541273d778eSKuninori Morimoto 	for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2542273d778eSKuninori Morimoto 		if (stream->formats & endianness_format_map[i])
2543273d778eSKuninori Morimoto 			stream->formats |= endianness_format_map[i];
2544273d778eSKuninori Morimoto }
2545273d778eSKuninori Morimoto 
2546e894efefSSrinivas Kandagatla static void snd_soc_try_rebind_card(void)
2547e894efefSSrinivas Kandagatla {
2548e894efefSSrinivas Kandagatla 	struct snd_soc_card *card, *c;
2549e894efefSSrinivas Kandagatla 
2550b245d273SKuninori Morimoto 	list_for_each_entry_safe(card, c, &unbind_card_list, list)
2551e894efefSSrinivas Kandagatla 		if (!snd_soc_bind_card(card))
2552e894efefSSrinivas Kandagatla 			list_del(&card->list);
2553e894efefSSrinivas Kandagatla }
2554e894efefSSrinivas Kandagatla 
2555486c7978SKuninori Morimoto static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2556486c7978SKuninori Morimoto {
2557b18768f5SKuninori Morimoto 	struct snd_soc_card *card = component->card;
2558b18768f5SKuninori Morimoto 
2559486c7978SKuninori Morimoto 	snd_soc_unregister_dais(component);
2560b18768f5SKuninori Morimoto 
2561b18768f5SKuninori Morimoto 	if (card)
2562b18768f5SKuninori Morimoto 		snd_soc_unbind_card(card, false);
2563b18768f5SKuninori Morimoto 
2564b18768f5SKuninori Morimoto 	list_del(&component->list);
2565486c7978SKuninori Morimoto }
2566486c7978SKuninori Morimoto 
256708ff7209SCezary Rojewski int snd_soc_component_initialize(struct snd_soc_component *component,
256808ff7209SCezary Rojewski 				 const struct snd_soc_component_driver *driver,
25697274d4cdSCezary Rojewski 				 struct device *dev)
257008ff7209SCezary Rojewski {
257108ff7209SCezary Rojewski 	INIT_LIST_HEAD(&component->dai_list);
257208ff7209SCezary Rojewski 	INIT_LIST_HEAD(&component->dobj_list);
257308ff7209SCezary Rojewski 	INIT_LIST_HEAD(&component->card_list);
2574c448b7aaSYang Yingliang 	INIT_LIST_HEAD(&component->list);
257508ff7209SCezary Rojewski 	mutex_init(&component->io_mutex);
257608ff7209SCezary Rojewski 
25777274d4cdSCezary Rojewski 	component->name = fmt_single_name(dev, &component->id);
25787274d4cdSCezary Rojewski 	if (!component->name) {
25797274d4cdSCezary Rojewski 		dev_err(dev, "ASoC: Failed to allocate name\n");
25807274d4cdSCezary Rojewski 		return -ENOMEM;
25817274d4cdSCezary Rojewski 	}
25827274d4cdSCezary Rojewski 
258308ff7209SCezary Rojewski 	component->dev		= dev;
258408ff7209SCezary Rojewski 	component->driver	= driver;
258508ff7209SCezary Rojewski 
2586d462f6edSHeiner Kallweit #ifdef CONFIG_DEBUG_FS
2587d462f6edSHeiner Kallweit 	if (!component->debugfs_prefix)
2588d462f6edSHeiner Kallweit 		component->debugfs_prefix = driver->debugfs_prefix;
2589d462f6edSHeiner Kallweit #endif
2590d462f6edSHeiner Kallweit 
259108ff7209SCezary Rojewski 	return 0;
259208ff7209SCezary Rojewski }
259308ff7209SCezary Rojewski EXPORT_SYMBOL_GPL(snd_soc_component_initialize);
259408ff7209SCezary Rojewski 
2595ea029dd8SCezary Rojewski int snd_soc_add_component(struct snd_soc_component *component,
2596d191bd8dSKuninori Morimoto 			  struct snd_soc_dai_driver *dai_drv,
2597d191bd8dSKuninori Morimoto 			  int num_dai)
2598d191bd8dSKuninori Morimoto {
2599bb13109dSLars-Peter Clausen 	int ret;
2600273d778eSKuninori Morimoto 	int i;
2601d191bd8dSKuninori Morimoto 
2602b18768f5SKuninori Morimoto 	mutex_lock(&client_mutex);
2603b18768f5SKuninori Morimoto 
2604ea029dd8SCezary Rojewski 	if (component->driver->endianness) {
2605273d778eSKuninori Morimoto 		for (i = 0; i < num_dai; i++) {
2606273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].playback);
2607273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].capture);
2608273d778eSKuninori Morimoto 		}
2609273d778eSKuninori Morimoto 	}
2610273d778eSKuninori Morimoto 
26110e7b25c6SKuninori Morimoto 	ret = snd_soc_register_dais(component, dai_drv, num_dai);
2612bb13109dSLars-Peter Clausen 	if (ret < 0) {
2613ea029dd8SCezary Rojewski 		dev_err(component->dev, "ASoC: Failed to register DAIs: %d\n",
2614ea029dd8SCezary Rojewski 			ret);
2615bb13109dSLars-Peter Clausen 		goto err_cleanup;
2616bb13109dSLars-Peter Clausen 	}
2617bb13109dSLars-Peter Clausen 
2618b18768f5SKuninori Morimoto 	if (!component->driver->write && !component->driver->read) {
2619b18768f5SKuninori Morimoto 		if (!component->regmap)
2620b18768f5SKuninori Morimoto 			component->regmap = dev_get_regmap(component->dev,
2621b18768f5SKuninori Morimoto 							   NULL);
2622b18768f5SKuninori Morimoto 		if (component->regmap)
2623b18768f5SKuninori Morimoto 			snd_soc_component_setup_regmap(component);
2624b18768f5SKuninori Morimoto 	}
2625bb13109dSLars-Peter Clausen 
2626b18768f5SKuninori Morimoto 	/* see for_each_component */
2627b18768f5SKuninori Morimoto 	list_add(&component->list, &component_list);
2628bb13109dSLars-Peter Clausen 
2629bb13109dSLars-Peter Clausen err_cleanup:
2630b18768f5SKuninori Morimoto 	if (ret < 0)
2631486c7978SKuninori Morimoto 		snd_soc_del_component_unlocked(component);
2632ea029dd8SCezary Rojewski 
2633b18768f5SKuninori Morimoto 	mutex_unlock(&client_mutex);
2634b18768f5SKuninori Morimoto 
2635b18768f5SKuninori Morimoto 	if (ret == 0)
2636b18768f5SKuninori Morimoto 		snd_soc_try_rebind_card();
2637b18768f5SKuninori Morimoto 
2638bb13109dSLars-Peter Clausen 	return ret;
2639d191bd8dSKuninori Morimoto }
2640e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component);
2641e0dac41bSKuninori Morimoto 
2642e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev,
2643e0dac41bSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2644e0dac41bSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2645e0dac41bSKuninori Morimoto 			int num_dai)
2646e0dac41bSKuninori Morimoto {
2647e0dac41bSKuninori Morimoto 	struct snd_soc_component *component;
2648ea029dd8SCezary Rojewski 	int ret;
2649e0dac41bSKuninori Morimoto 
26507ecbd6a9SKuninori Morimoto 	component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
265108e61d03SKuninori Morimoto 	if (!component)
2652e0dac41bSKuninori Morimoto 		return -ENOMEM;
2653e0dac41bSKuninori Morimoto 
2654ea029dd8SCezary Rojewski 	ret = snd_soc_component_initialize(component, component_driver, dev);
2655ea029dd8SCezary Rojewski 	if (ret < 0)
2656ea029dd8SCezary Rojewski 		return ret;
2657ea029dd8SCezary Rojewski 
2658ea029dd8SCezary Rojewski 	return snd_soc_add_component(component, dai_drv, num_dai);
2659e0dac41bSKuninori Morimoto }
2660d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
2661d191bd8dSKuninori Morimoto 
2662d191bd8dSKuninori Morimoto /**
266358f30150SMaxime Ripard  * snd_soc_unregister_component_by_driver - Unregister component using a given driver
266458f30150SMaxime Ripard  * from the ASoC core
266558f30150SMaxime Ripard  *
266658f30150SMaxime Ripard  * @dev: The device to unregister
266758f30150SMaxime Ripard  * @component_driver: The component driver to unregister
266858f30150SMaxime Ripard  */
266958f30150SMaxime Ripard void snd_soc_unregister_component_by_driver(struct device *dev,
267058f30150SMaxime Ripard 					    const struct snd_soc_component_driver *component_driver)
267158f30150SMaxime Ripard {
267258f30150SMaxime Ripard 	struct snd_soc_component *component;
267358f30150SMaxime Ripard 
267458f30150SMaxime Ripard 	if (!component_driver)
267558f30150SMaxime Ripard 		return;
267658f30150SMaxime Ripard 
267758f30150SMaxime Ripard 	mutex_lock(&client_mutex);
267858f30150SMaxime Ripard 	component = snd_soc_lookup_component_nolocked(dev, component_driver->name);
267958f30150SMaxime Ripard 	if (!component)
268058f30150SMaxime Ripard 		goto out;
268158f30150SMaxime Ripard 
268258f30150SMaxime Ripard 	snd_soc_del_component_unlocked(component);
268358f30150SMaxime Ripard 
268458f30150SMaxime Ripard out:
268558f30150SMaxime Ripard 	mutex_unlock(&client_mutex);
268658f30150SMaxime Ripard }
268758f30150SMaxime Ripard EXPORT_SYMBOL_GPL(snd_soc_unregister_component_by_driver);
268858f30150SMaxime Ripard 
268958f30150SMaxime Ripard /**
26902eccea8cSKuninori Morimoto  * snd_soc_unregister_component - Unregister all related component
26912eccea8cSKuninori Morimoto  * from the ASoC core
2692d191bd8dSKuninori Morimoto  *
2693628536eaSJonathan Corbet  * @dev: The device to unregister
2694d191bd8dSKuninori Morimoto  */
26952eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
26962eccea8cSKuninori Morimoto {
2697ac6a4dd3SKuninori Morimoto 	mutex_lock(&client_mutex);
2698ac6a4dd3SKuninori Morimoto 	while (1) {
26995600f3d5SKuninori Morimoto 		struct snd_soc_component *component = snd_soc_lookup_component_nolocked(dev, NULL);
27005600f3d5SKuninori Morimoto 
2701ac6a4dd3SKuninori Morimoto 		if (!component)
2702ac6a4dd3SKuninori Morimoto 			break;
2703ac6a4dd3SKuninori Morimoto 
2704ac6a4dd3SKuninori Morimoto 		snd_soc_del_component_unlocked(component);
2705ac6a4dd3SKuninori Morimoto 	}
2706ac6a4dd3SKuninori Morimoto 	mutex_unlock(&client_mutex);
2707d191bd8dSKuninori Morimoto }
2708d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2709d191bd8dSKuninori Morimoto 
2710bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
2711b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2712bec4fa05SStephen Warren 			       const char *propname)
2713bec4fa05SStephen Warren {
2714b07609ceSKuninori Morimoto 	struct device_node *np;
2715bec4fa05SStephen Warren 	int ret;
2716bec4fa05SStephen Warren 
27177e07e7c0STushar Behera 	if (!card->dev) {
27187e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
27197e07e7c0STushar Behera 		return -EINVAL;
27207e07e7c0STushar Behera 	}
27217e07e7c0STushar Behera 
27227e07e7c0STushar Behera 	np = card->dev->of_node;
27237e07e7c0STushar Behera 
2724bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
2725bec4fa05SStephen Warren 	/*
2726bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
2727bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
2728bec4fa05SStephen Warren 	 * snd_soc_register_card.
2729bec4fa05SStephen Warren 	 */
2730bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
2731bec4fa05SStephen Warren 		dev_err(card->dev,
2732f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
2733bec4fa05SStephen Warren 			propname, ret);
2734bec4fa05SStephen Warren 		return ret;
2735bec4fa05SStephen Warren 	}
2736bec4fa05SStephen Warren 
2737bec4fa05SStephen Warren 	return 0;
2738bec4fa05SStephen Warren }
2739b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2740bec4fa05SStephen Warren 
27419a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
27429a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
27439a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
27449a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
27459a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
27469a6d4860SXiubo Li };
27479a6d4860SXiubo Li 
274821efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
27499a6d4860SXiubo Li 					  const char *propname)
27509a6d4860SXiubo Li {
275121efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
27529a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
27539a6d4860SXiubo Li 	const char *template, *wname;
275499c68653SKuninori Morimoto 	int i, j, num_widgets;
27559a6d4860SXiubo Li 
27569a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
27579a6d4860SXiubo Li 	if (num_widgets < 0) {
27589a6d4860SXiubo Li 		dev_err(card->dev,
27599a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
27609a6d4860SXiubo Li 		return -EINVAL;
27619a6d4860SXiubo Li 	}
27628ad0b83eSPierre-Louis Bossart 	if (!num_widgets) {
27638ad0b83eSPierre-Louis Bossart 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
27648ad0b83eSPierre-Louis Bossart 			propname);
27658ad0b83eSPierre-Louis Bossart 		return -EINVAL;
27668ad0b83eSPierre-Louis Bossart 	}
27679a6d4860SXiubo Li 	if (num_widgets & 1) {
27689a6d4860SXiubo Li 		dev_err(card->dev,
27699a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
27709a6d4860SXiubo Li 		return -EINVAL;
27719a6d4860SXiubo Li 	}
27729a6d4860SXiubo Li 
27739a6d4860SXiubo Li 	num_widgets /= 2;
27749a6d4860SXiubo Li 
27759a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
27769a6d4860SXiubo Li 			       GFP_KERNEL);
27779a6d4860SXiubo Li 	if (!widgets) {
27789a6d4860SXiubo Li 		dev_err(card->dev,
27799a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
27809a6d4860SXiubo Li 		return -ENOMEM;
27819a6d4860SXiubo Li 	}
27829a6d4860SXiubo Li 
27839a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
278499c68653SKuninori Morimoto 		int ret = of_property_read_string_index(np, propname,
27859a6d4860SXiubo Li 							2 * i, &template);
27869a6d4860SXiubo Li 		if (ret) {
27879a6d4860SXiubo Li 			dev_err(card->dev,
27889a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
27899a6d4860SXiubo Li 				propname, 2 * i, ret);
27909a6d4860SXiubo Li 			return -EINVAL;
27919a6d4860SXiubo Li 		}
27929a6d4860SXiubo Li 
27939a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
27949a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
27959a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
27969a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
27979a6d4860SXiubo Li 				break;
27989a6d4860SXiubo Li 			}
27999a6d4860SXiubo Li 		}
28009a6d4860SXiubo Li 
28019a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
28029a6d4860SXiubo Li 			dev_err(card->dev,
28039a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
28049a6d4860SXiubo Li 				template);
28059a6d4860SXiubo Li 			return -EINVAL;
28069a6d4860SXiubo Li 		}
28079a6d4860SXiubo Li 
28089a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
28099a6d4860SXiubo Li 						    (2 * i) + 1,
28109a6d4860SXiubo Li 						    &wname);
28119a6d4860SXiubo Li 		if (ret) {
28129a6d4860SXiubo Li 			dev_err(card->dev,
28139a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
28149a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
28159a6d4860SXiubo Li 			return -EINVAL;
28169a6d4860SXiubo Li 		}
28179a6d4860SXiubo Li 
28189a6d4860SXiubo Li 		widgets[i].name = wname;
28199a6d4860SXiubo Li 	}
28209a6d4860SXiubo Li 
2821f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
2822f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
28239a6d4860SXiubo Li 
28249a6d4860SXiubo Li 	return 0;
28259a6d4860SXiubo Li }
282621efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
28279a6d4860SXiubo Li 
28283d4641a4SStephan Gerhold int snd_soc_of_parse_pin_switches(struct snd_soc_card *card, const char *prop)
28293d4641a4SStephan Gerhold {
28303d4641a4SStephan Gerhold 	const unsigned int nb_controls_max = 16;
28313d4641a4SStephan Gerhold 	const char **strings, *control_name;
28323d4641a4SStephan Gerhold 	struct snd_kcontrol_new *controls;
28333d4641a4SStephan Gerhold 	struct device *dev = card->dev;
28343d4641a4SStephan Gerhold 	unsigned int i, nb_controls;
28353d4641a4SStephan Gerhold 	int ret;
28363d4641a4SStephan Gerhold 
28373d4641a4SStephan Gerhold 	if (!of_property_read_bool(dev->of_node, prop))
28383d4641a4SStephan Gerhold 		return 0;
28393d4641a4SStephan Gerhold 
28403d4641a4SStephan Gerhold 	strings = devm_kcalloc(dev, nb_controls_max,
28413d4641a4SStephan Gerhold 			       sizeof(*strings), GFP_KERNEL);
28423d4641a4SStephan Gerhold 	if (!strings)
28433d4641a4SStephan Gerhold 		return -ENOMEM;
28443d4641a4SStephan Gerhold 
28453d4641a4SStephan Gerhold 	ret = of_property_read_string_array(dev->of_node, prop,
28463d4641a4SStephan Gerhold 					    strings, nb_controls_max);
28473d4641a4SStephan Gerhold 	if (ret < 0)
28483d4641a4SStephan Gerhold 		return ret;
28493d4641a4SStephan Gerhold 
28503d4641a4SStephan Gerhold 	nb_controls = (unsigned int)ret;
28513d4641a4SStephan Gerhold 
28523d4641a4SStephan Gerhold 	controls = devm_kcalloc(dev, nb_controls,
28533d4641a4SStephan Gerhold 				sizeof(*controls), GFP_KERNEL);
28543d4641a4SStephan Gerhold 	if (!controls)
28553d4641a4SStephan Gerhold 		return -ENOMEM;
28563d4641a4SStephan Gerhold 
28573d4641a4SStephan Gerhold 	for (i = 0; i < nb_controls; i++) {
28583d4641a4SStephan Gerhold 		control_name = devm_kasprintf(dev, GFP_KERNEL,
28593d4641a4SStephan Gerhold 					      "%s Switch", strings[i]);
28603d4641a4SStephan Gerhold 		if (!control_name)
28613d4641a4SStephan Gerhold 			return -ENOMEM;
28623d4641a4SStephan Gerhold 
28633d4641a4SStephan Gerhold 		controls[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
28643d4641a4SStephan Gerhold 		controls[i].name = control_name;
28653d4641a4SStephan Gerhold 		controls[i].info = snd_soc_dapm_info_pin_switch;
28663d4641a4SStephan Gerhold 		controls[i].get = snd_soc_dapm_get_pin_switch;
28673d4641a4SStephan Gerhold 		controls[i].put = snd_soc_dapm_put_pin_switch;
28683d4641a4SStephan Gerhold 		controls[i].private_value = (unsigned long)strings[i];
28693d4641a4SStephan Gerhold 	}
28703d4641a4SStephan Gerhold 
28713d4641a4SStephan Gerhold 	card->controls = controls;
28723d4641a4SStephan Gerhold 	card->num_controls = nb_controls;
28733d4641a4SStephan Gerhold 
28743d4641a4SStephan Gerhold 	return 0;
28753d4641a4SStephan Gerhold }
28763d4641a4SStephan Gerhold EXPORT_SYMBOL_GPL(snd_soc_of_parse_pin_switches);
28773d4641a4SStephan Gerhold 
2878cbdfab3bSJerome Brunet int snd_soc_of_get_slot_mask(struct device_node *np,
28796131084aSJyri Sarha 			     const char *prop_name,
28806131084aSJyri Sarha 			     unsigned int *mask)
28816131084aSJyri Sarha {
28826131084aSJyri Sarha 	u32 val;
28836c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
28846131084aSJyri Sarha 	int i;
28856131084aSJyri Sarha 
28866131084aSJyri Sarha 	if (!of_slot_mask)
28876131084aSJyri Sarha 		return 0;
28886131084aSJyri Sarha 	val /= sizeof(u32);
28896131084aSJyri Sarha 	for (i = 0; i < val; i++)
28906131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
28916131084aSJyri Sarha 			*mask |= (1 << i);
28926131084aSJyri Sarha 
28936131084aSJyri Sarha 	return val;
28946131084aSJyri Sarha }
2895cbdfab3bSJerome Brunet EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
28966131084aSJyri Sarha 
289789c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
28986131084aSJyri Sarha 			      unsigned int *tx_mask,
28996131084aSJyri Sarha 			      unsigned int *rx_mask,
290089c67857SXiubo Li 			      unsigned int *slots,
290189c67857SXiubo Li 			      unsigned int *slot_width)
290289c67857SXiubo Li {
290389c67857SXiubo Li 	u32 val;
290489c67857SXiubo Li 	int ret;
290589c67857SXiubo Li 
29066131084aSJyri Sarha 	if (tx_mask)
29076131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
29086131084aSJyri Sarha 	if (rx_mask)
29096131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
29106131084aSJyri Sarha 
291189c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
291289c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
291389c67857SXiubo Li 		if (ret)
291489c67857SXiubo Li 			return ret;
291589c67857SXiubo Li 
291689c67857SXiubo Li 		if (slots)
291789c67857SXiubo Li 			*slots = val;
291889c67857SXiubo Li 	}
291989c67857SXiubo Li 
292089c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
292189c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
292289c67857SXiubo Li 		if (ret)
292389c67857SXiubo Li 			return ret;
292489c67857SXiubo Li 
292589c67857SXiubo Li 		if (slot_width)
292689c67857SXiubo Li 			*slot_width = val;
292789c67857SXiubo Li 	}
292889c67857SXiubo Li 
292989c67857SXiubo Li 	return 0;
293089c67857SXiubo Li }
293189c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
293289c67857SXiubo Li 
29333b710356SKuninori Morimoto void snd_soc_of_parse_node_prefix(struct device_node *np,
29345e3cdaa2SKuninori Morimoto 				  struct snd_soc_codec_conf *codec_conf,
29355e3cdaa2SKuninori Morimoto 				  struct device_node *of_node,
29365e3cdaa2SKuninori Morimoto 				  const char *propname)
29375e3cdaa2SKuninori Morimoto {
29385e3cdaa2SKuninori Morimoto 	const char *str;
29395e3cdaa2SKuninori Morimoto 	int ret;
29405e3cdaa2SKuninori Morimoto 
29415e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
29425e3cdaa2SKuninori Morimoto 	if (ret < 0) {
29435e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
29445e3cdaa2SKuninori Morimoto 		return;
29455e3cdaa2SKuninori Morimoto 	}
29465e3cdaa2SKuninori Morimoto 
2947c13493a2SKuninori Morimoto 	codec_conf->dlc.of_node	= of_node;
29485e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
29495e3cdaa2SKuninori Morimoto }
29503b710356SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
29515e3cdaa2SKuninori Morimoto 
29522bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
2953a4a54dd5SStephen Warren 				   const char *propname)
2954a4a54dd5SStephen Warren {
29552bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
2956e3b1e6a1SMark Brown 	int num_routes;
2957a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
295851a3dd58SKuninori Morimoto 	int i;
2959a4a54dd5SStephen Warren 
2960a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
2961c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
296210e8aa9aSMichał Mirosław 		dev_err(card->dev,
296310e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
296410e8aa9aSMichał Mirosław 			propname);
2965a4a54dd5SStephen Warren 		return -EINVAL;
2966a4a54dd5SStephen Warren 	}
2967a4a54dd5SStephen Warren 	num_routes /= 2;
2968a4a54dd5SStephen Warren 
2969a86854d0SKees Cook 	routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
2970a4a54dd5SStephen Warren 			      GFP_KERNEL);
2971a4a54dd5SStephen Warren 	if (!routes) {
2972a4a54dd5SStephen Warren 		dev_err(card->dev,
2973f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
29747d3865a1SZhen Lei 		return -ENOMEM;
2975a4a54dd5SStephen Warren 	}
2976a4a54dd5SStephen Warren 
2977a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
297851a3dd58SKuninori Morimoto 		int ret = of_property_read_string_index(np, propname,
2979e3b1e6a1SMark Brown 							2 * i, &routes[i].sink);
2980a4a54dd5SStephen Warren 		if (ret) {
2981c871bd0bSMark Brown 			dev_err(card->dev,
2982c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
2983c871bd0bSMark Brown 				propname, 2 * i, ret);
2984a4a54dd5SStephen Warren 			return -EINVAL;
2985a4a54dd5SStephen Warren 		}
2986a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
2987e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
2988a4a54dd5SStephen Warren 		if (ret) {
2989a4a54dd5SStephen Warren 			dev_err(card->dev,
2990c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
2991c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
2992a4a54dd5SStephen Warren 			return -EINVAL;
2993a4a54dd5SStephen Warren 		}
2994a4a54dd5SStephen Warren 	}
2995a4a54dd5SStephen Warren 
2996f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
2997f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
2998a4a54dd5SStephen Warren 
2999a4a54dd5SStephen Warren 	return 0;
3000a4a54dd5SStephen Warren }
30012bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3002a4a54dd5SStephen Warren 
30031ae0965dSStephan Gerhold int snd_soc_of_parse_aux_devs(struct snd_soc_card *card, const char *propname)
30041ae0965dSStephan Gerhold {
30051ae0965dSStephan Gerhold 	struct device_node *node = card->dev->of_node;
30061ae0965dSStephan Gerhold 	struct snd_soc_aux_dev *aux;
30071ae0965dSStephan Gerhold 	int num, i;
30081ae0965dSStephan Gerhold 
30091ae0965dSStephan Gerhold 	num = of_count_phandle_with_args(node, propname, NULL);
30101ae0965dSStephan Gerhold 	if (num == -ENOENT) {
30111ae0965dSStephan Gerhold 		return 0;
30121ae0965dSStephan Gerhold 	} else if (num < 0) {
30131ae0965dSStephan Gerhold 		dev_err(card->dev, "ASOC: Property '%s' could not be read: %d\n",
30141ae0965dSStephan Gerhold 			propname, num);
30151ae0965dSStephan Gerhold 		return num;
30161ae0965dSStephan Gerhold 	}
30171ae0965dSStephan Gerhold 
30181ae0965dSStephan Gerhold 	aux = devm_kcalloc(card->dev, num, sizeof(*aux), GFP_KERNEL);
30191ae0965dSStephan Gerhold 	if (!aux)
30201ae0965dSStephan Gerhold 		return -ENOMEM;
30211ae0965dSStephan Gerhold 	card->aux_dev = aux;
30221ae0965dSStephan Gerhold 	card->num_aux_devs = num;
30231ae0965dSStephan Gerhold 
30241ae0965dSStephan Gerhold 	for_each_card_pre_auxs(card, i, aux) {
30251ae0965dSStephan Gerhold 		aux->dlc.of_node = of_parse_phandle(node, propname, i);
30261ae0965dSStephan Gerhold 		if (!aux->dlc.of_node)
30271ae0965dSStephan Gerhold 			return -EINVAL;
30281ae0965dSStephan Gerhold 	}
30291ae0965dSStephan Gerhold 
30301ae0965dSStephan Gerhold 	return 0;
30311ae0965dSStephan Gerhold }
30321ae0965dSStephan Gerhold EXPORT_SYMBOL_GPL(snd_soc_of_parse_aux_devs);
30331ae0965dSStephan Gerhold 
303464c917d1SCharles Keepax unsigned int snd_soc_daifmt_clock_provider_flipped(unsigned int dai_fmt)
3035b44a67f8SKuninori Morimoto {
3036b44a67f8SKuninori Morimoto 	unsigned int inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK;
3037b44a67f8SKuninori Morimoto 
3038b44a67f8SKuninori Morimoto 	switch (dai_fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
3039b44a67f8SKuninori Morimoto 	case SND_SOC_DAIFMT_CBP_CFP:
3040b44a67f8SKuninori Morimoto 		inv_dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
3041b44a67f8SKuninori Morimoto 		break;
3042b44a67f8SKuninori Morimoto 	case SND_SOC_DAIFMT_CBP_CFC:
3043b44a67f8SKuninori Morimoto 		inv_dai_fmt |= SND_SOC_DAIFMT_CBC_CFP;
3044b44a67f8SKuninori Morimoto 		break;
3045b44a67f8SKuninori Morimoto 	case SND_SOC_DAIFMT_CBC_CFP:
3046b44a67f8SKuninori Morimoto 		inv_dai_fmt |= SND_SOC_DAIFMT_CBP_CFC;
3047b44a67f8SKuninori Morimoto 		break;
3048b44a67f8SKuninori Morimoto 	case SND_SOC_DAIFMT_CBC_CFC:
3049b44a67f8SKuninori Morimoto 		inv_dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
3050b44a67f8SKuninori Morimoto 		break;
3051b44a67f8SKuninori Morimoto 	}
3052b44a67f8SKuninori Morimoto 
3053b44a67f8SKuninori Morimoto 	return inv_dai_fmt;
3054b44a67f8SKuninori Morimoto }
305564c917d1SCharles Keepax EXPORT_SYMBOL_GPL(snd_soc_daifmt_clock_provider_flipped);
3056b44a67f8SKuninori Morimoto 
305791ae4477SKuninori Morimoto unsigned int snd_soc_daifmt_clock_provider_from_bitmap(unsigned int bit_frame)
305891ae4477SKuninori Morimoto {
30597766861dSKuninori Morimoto 	/*
30607766861dSKuninori Morimoto 	 * bit_frame is return value from
30617766861dSKuninori Morimoto 	 *	snd_soc_daifmt_parse_clock_provider_raw()
30627766861dSKuninori Morimoto 	 */
30637766861dSKuninori Morimoto 
306491ae4477SKuninori Morimoto 	/* Codec base */
306591ae4477SKuninori Morimoto 	switch (bit_frame) {
306691ae4477SKuninori Morimoto 	case 0x11:
306791ae4477SKuninori Morimoto 		return SND_SOC_DAIFMT_CBP_CFP;
306891ae4477SKuninori Morimoto 	case 0x10:
306991ae4477SKuninori Morimoto 		return SND_SOC_DAIFMT_CBP_CFC;
307091ae4477SKuninori Morimoto 	case 0x01:
307191ae4477SKuninori Morimoto 		return SND_SOC_DAIFMT_CBC_CFP;
307291ae4477SKuninori Morimoto 	default:
307391ae4477SKuninori Morimoto 		return SND_SOC_DAIFMT_CBC_CFC;
307491ae4477SKuninori Morimoto 	}
307591ae4477SKuninori Morimoto 
307691ae4477SKuninori Morimoto 	return 0;
307791ae4477SKuninori Morimoto }
307891ae4477SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_daifmt_clock_provider_from_bitmap);
307991ae4477SKuninori Morimoto 
30807766861dSKuninori Morimoto unsigned int snd_soc_daifmt_parse_format(struct device_node *np,
30817766861dSKuninori Morimoto 					 const char *prefix)
3082a7930ed4SKuninori Morimoto {
30835ad76775SKuninori Morimoto 	int ret;
3084a7930ed4SKuninori Morimoto 	char prop[128];
3085a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3086a7930ed4SKuninori Morimoto 	int bit, frame;
3087a7930ed4SKuninori Morimoto 	const char *str;
3088a7930ed4SKuninori Morimoto 	struct {
3089a7930ed4SKuninori Morimoto 		char *name;
3090a7930ed4SKuninori Morimoto 		unsigned int val;
3091a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3092a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3093a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3094a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3095a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3096a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3097a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3098a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3099a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3100a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3101a7930ed4SKuninori Morimoto 	};
3102a7930ed4SKuninori Morimoto 
3103a7930ed4SKuninori Morimoto 	if (!prefix)
3104a7930ed4SKuninori Morimoto 		prefix = "";
3105a7930ed4SKuninori Morimoto 
3106a7930ed4SKuninori Morimoto 	/*
31075711c979SKuninori Morimoto 	 * check "dai-format = xxx"
31085711c979SKuninori Morimoto 	 * or    "[prefix]format = xxx"
3109a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3110a7930ed4SKuninori Morimoto 	 */
31115711c979SKuninori Morimoto 	ret = of_property_read_string(np, "dai-format", &str);
31125711c979SKuninori Morimoto 	if (ret < 0) {
3113a7930ed4SKuninori Morimoto 		snprintf(prop, sizeof(prop), "%sformat", prefix);
3114a7930ed4SKuninori Morimoto 		ret = of_property_read_string(np, prop, &str);
31155711c979SKuninori Morimoto 	}
3116a7930ed4SKuninori Morimoto 	if (ret == 0) {
31175ad76775SKuninori Morimoto 		int i;
31185ad76775SKuninori Morimoto 
3119a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3120a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3121a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3122a7930ed4SKuninori Morimoto 				break;
3123a7930ed4SKuninori Morimoto 			}
3124a7930ed4SKuninori Morimoto 		}
3125a7930ed4SKuninori Morimoto 	}
3126a7930ed4SKuninori Morimoto 
3127a7930ed4SKuninori Morimoto 	/*
31288c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3129a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3130a7930ed4SKuninori Morimoto 	 */
31318c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
313251930295SJulia Lawall 	if (of_property_read_bool(np, prop))
31338c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
31348c2d6a9fSKuninori Morimoto 	else
31358c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3136a7930ed4SKuninori Morimoto 
3137a7930ed4SKuninori Morimoto 	/*
3138a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3139a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3140a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3141a7930ed4SKuninori Morimoto 	 */
3142a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3143a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3144a7930ed4SKuninori Morimoto 
3145a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3146a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3147a7930ed4SKuninori Morimoto 
3148a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3149a7930ed4SKuninori Morimoto 	case 0x11:
3150a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3151a7930ed4SKuninori Morimoto 		break;
3152a7930ed4SKuninori Morimoto 	case 0x10:
3153a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3154a7930ed4SKuninori Morimoto 		break;
3155a7930ed4SKuninori Morimoto 	case 0x01:
3156a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3157a7930ed4SKuninori Morimoto 		break;
3158a7930ed4SKuninori Morimoto 	default:
3159a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3160a7930ed4SKuninori Morimoto 		break;
3161a7930ed4SKuninori Morimoto 	}
3162a7930ed4SKuninori Morimoto 
31637766861dSKuninori Morimoto 	return format;
31647766861dSKuninori Morimoto }
31657766861dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_daifmt_parse_format);
31667766861dSKuninori Morimoto 
31677766861dSKuninori Morimoto unsigned int snd_soc_daifmt_parse_clock_provider_raw(struct device_node *np,
31687766861dSKuninori Morimoto 						     const char *prefix,
31697766861dSKuninori Morimoto 						     struct device_node **bitclkmaster,
31707766861dSKuninori Morimoto 						     struct device_node **framemaster)
31717766861dSKuninori Morimoto {
31727766861dSKuninori Morimoto 	char prop[128];
31737766861dSKuninori Morimoto 	unsigned int bit, frame;
31747766861dSKuninori Morimoto 
31757766861dSKuninori Morimoto 	if (!prefix)
31767766861dSKuninori Morimoto 		prefix = "";
31777766861dSKuninori Morimoto 
3178a7930ed4SKuninori Morimoto 	/*
3179a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3180a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3181a7930ed4SKuninori Morimoto 	 */
3182a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3183a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3184389cb834SJyri Sarha 	if (bit && bitclkmaster)
3185389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3186a7930ed4SKuninori Morimoto 
3187a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3188a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3189389cb834SJyri Sarha 	if (frame && framemaster)
3190389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3191a7930ed4SKuninori Morimoto 
31927766861dSKuninori Morimoto 	/*
31937766861dSKuninori Morimoto 	 * return bitmap.
31947766861dSKuninori Morimoto 	 * It will be parameter of
31957766861dSKuninori Morimoto 	 *	snd_soc_daifmt_clock_provider_from_bitmap()
31967766861dSKuninori Morimoto 	 */
31977766861dSKuninori Morimoto 	return (bit << 4) + frame;
3198a7930ed4SKuninori Morimoto }
31997766861dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_daifmt_parse_clock_provider_raw);
3200a7930ed4SKuninori Morimoto 
32011c943f60SKuninori Morimoto int snd_soc_get_stream_cpu(struct snd_soc_dai_link *dai_link, int stream)
32021c943f60SKuninori Morimoto {
32031c943f60SKuninori Morimoto 	/*
32041c943f60SKuninori Morimoto 	 * [Normal]
32051c943f60SKuninori Morimoto 	 *
32061c943f60SKuninori Morimoto 	 * Playback
32071c943f60SKuninori Morimoto 	 *	CPU  : SNDRV_PCM_STREAM_PLAYBACK
32081c943f60SKuninori Morimoto 	 *	Codec: SNDRV_PCM_STREAM_PLAYBACK
32091c943f60SKuninori Morimoto 	 *
32101c943f60SKuninori Morimoto 	 * Capture
32111c943f60SKuninori Morimoto 	 *	CPU  : SNDRV_PCM_STREAM_CAPTURE
32121c943f60SKuninori Morimoto 	 *	Codec: SNDRV_PCM_STREAM_CAPTURE
32131c943f60SKuninori Morimoto 	 */
32141c943f60SKuninori Morimoto 	if (!dai_link->c2c_params)
32151c943f60SKuninori Morimoto 		return stream;
32161c943f60SKuninori Morimoto 
32171c943f60SKuninori Morimoto 	/*
32181c943f60SKuninori Morimoto 	 * [Codec2Codec]
32191c943f60SKuninori Morimoto 	 *
32201c943f60SKuninori Morimoto 	 * Playback
32211c943f60SKuninori Morimoto 	 *	CPU  : SNDRV_PCM_STREAM_CAPTURE
32221c943f60SKuninori Morimoto 	 *	Codec: SNDRV_PCM_STREAM_PLAYBACK
32231c943f60SKuninori Morimoto 	 *
32241c943f60SKuninori Morimoto 	 * Capture
32251c943f60SKuninori Morimoto 	 *	CPU  : SNDRV_PCM_STREAM_PLAYBACK
32261c943f60SKuninori Morimoto 	 *	Codec: SNDRV_PCM_STREAM_CAPTURE
32271c943f60SKuninori Morimoto 	 */
32281c943f60SKuninori Morimoto 	if (stream == SNDRV_PCM_STREAM_CAPTURE)
32291c943f60SKuninori Morimoto 		return SNDRV_PCM_STREAM_PLAYBACK;
32301c943f60SKuninori Morimoto 
32311c943f60SKuninori Morimoto 	return SNDRV_PCM_STREAM_CAPTURE;
32321c943f60SKuninori Morimoto }
32331c943f60SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_stream_cpu);
32341c943f60SKuninori Morimoto 
3235a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
3236a180e8b9SKuninori Morimoto {
323709d4cc03SKuninori Morimoto 	struct snd_soc_component *component;
3238c1e230f0SKuninori Morimoto 	struct snd_soc_dai_link_component dlc;
3239a180e8b9SKuninori Morimoto 	int ret;
3240a180e8b9SKuninori Morimoto 
3241c1e230f0SKuninori Morimoto 	dlc.of_node	= of_graph_get_port_parent(ep);
3242c1e230f0SKuninori Morimoto 	dlc.name	= NULL;
3243a180e8b9SKuninori Morimoto 	/*
3244a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
3245a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
3246a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
3247a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
3248a180e8b9SKuninori Morimoto 	 */
3249a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
3250a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
3251c1e230f0SKuninori Morimoto 	component = soc_find_component(&dlc);
32522c7b1704SKuninori Morimoto 	if (component)
32532c7b1704SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_id(component, ep);
3254a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
3255a180e8b9SKuninori Morimoto 
3256c1e230f0SKuninori Morimoto 	of_node_put(dlc.of_node);
3257c0a480d1STony Lindgren 
3258a180e8b9SKuninori Morimoto 	return ret;
3259a180e8b9SKuninori Morimoto }
3260a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3261a180e8b9SKuninori Morimoto 
326205722a0cSKuninori Morimoto int snd_soc_get_dlc(const struct of_phandle_args *args, struct snd_soc_dai_link_component *dlc)
3263cb470087SKuninori Morimoto {
3264cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
326593b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3266cb470087SKuninori Morimoto 
32672d0cad04SMark Brown 	dlc->of_node = args->np;
32682d0cad04SMark Brown 
3269cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3270368dee94SKuninori Morimoto 	for_each_component(pos) {
3271bce00560SKuninori Morimoto 		struct device_node *component_of_node = soc_component_to_node(pos);
32723e0aa8d8SJyri Sarha 
3273f7d344a2SShengjiu Wang 		if (component_of_node != args->np || !pos->num_dai)
3274cb470087SKuninori Morimoto 			continue;
3275cb470087SKuninori Morimoto 
327605722a0cSKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_name(pos, args, &dlc->dai_name);
3277a2a34175SKuninori Morimoto 		if (ret == -ENOTSUPP) {
327858bf4179SKuninori Morimoto 			struct snd_soc_dai *dai;
32796833c452SKuninori Morimoto 			int id = -1;
32806833c452SKuninori Morimoto 
328193b0f3eeSJean-Francois Moine 			switch (args->args_count) {
32826833c452SKuninori Morimoto 			case 0:
32836833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
32846833c452SKuninori Morimoto 				break;
32856833c452SKuninori Morimoto 			case 1:
328693b0f3eeSJean-Francois Moine 				id = args->args[0];
32876833c452SKuninori Morimoto 				break;
32886833c452SKuninori Morimoto 			default:
32896833c452SKuninori Morimoto 				/* not supported */
3290cb470087SKuninori Morimoto 				break;
3291cb470087SKuninori Morimoto 			}
3292cb470087SKuninori Morimoto 
32936833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
32946833c452SKuninori Morimoto 				ret = -EINVAL;
32953dcba280SNicolin Chen 				continue;
32966833c452SKuninori Morimoto 			}
3297e41975edSXiubo Li 
3298e41975edSXiubo Li 			ret = 0;
3299e41975edSXiubo Li 
330058bf4179SKuninori Morimoto 			/* find target DAI */
330115a0c645SKuninori Morimoto 			for_each_component_dais(pos, dai) {
330258bf4179SKuninori Morimoto 				if (id == 0)
330358bf4179SKuninori Morimoto 					break;
330458bf4179SKuninori Morimoto 				id--;
330558bf4179SKuninori Morimoto 			}
330658bf4179SKuninori Morimoto 
330705722a0cSKuninori Morimoto 			dlc->dai_name	= dai->driver->name;
330805722a0cSKuninori Morimoto 			if (!dlc->dai_name)
330905722a0cSKuninori Morimoto 				dlc->dai_name = pos->name;
33101dfa5a5aSJerome Brunet 		} else if (ret) {
33111dfa5a5aSJerome Brunet 			/*
33121dfa5a5aSJerome Brunet 			 * if another error than ENOTSUPP is returned go on and
33131dfa5a5aSJerome Brunet 			 * check if another component is provided with the same
33141dfa5a5aSJerome Brunet 			 * node. This may happen if a device provides several
33151dfa5a5aSJerome Brunet 			 * components
33161dfa5a5aSJerome Brunet 			 */
33171dfa5a5aSJerome Brunet 			continue;
33186833c452SKuninori Morimoto 		}
33196833c452SKuninori Morimoto 
3320cb470087SKuninori Morimoto 		break;
3321cb470087SKuninori Morimoto 	}
3322cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
332393b0f3eeSJean-Francois Moine 	return ret;
332493b0f3eeSJean-Francois Moine }
332505722a0cSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dlc);
332605722a0cSKuninori Morimoto 
332705722a0cSKuninori Morimoto int snd_soc_of_get_dlc(struct device_node *of_node,
332805722a0cSKuninori Morimoto 		       struct of_phandle_args *args,
332905722a0cSKuninori Morimoto 		       struct snd_soc_dai_link_component *dlc,
333005722a0cSKuninori Morimoto 		       int index)
333105722a0cSKuninori Morimoto {
333205722a0cSKuninori Morimoto 	struct of_phandle_args __args;
333305722a0cSKuninori Morimoto 	int ret;
333405722a0cSKuninori Morimoto 
333505722a0cSKuninori Morimoto 	if (!args)
333605722a0cSKuninori Morimoto 		args = &__args;
333705722a0cSKuninori Morimoto 
333805722a0cSKuninori Morimoto 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
333905722a0cSKuninori Morimoto 					 "#sound-dai-cells", index, args);
334005722a0cSKuninori Morimoto 	if (ret)
334105722a0cSKuninori Morimoto 		return ret;
334205722a0cSKuninori Morimoto 
334305722a0cSKuninori Morimoto 	return snd_soc_get_dlc(args, dlc);
334405722a0cSKuninori Morimoto }
334505722a0cSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dlc);
334605722a0cSKuninori Morimoto 
334705722a0cSKuninori Morimoto int snd_soc_get_dai_name(const struct of_phandle_args *args,
334805722a0cSKuninori Morimoto 			 const char **dai_name)
334905722a0cSKuninori Morimoto {
335005722a0cSKuninori Morimoto 	struct snd_soc_dai_link_component dlc;
335105722a0cSKuninori Morimoto 	int ret = snd_soc_get_dlc(args, &dlc);
335205722a0cSKuninori Morimoto 
335305722a0cSKuninori Morimoto 	if (ret == 0)
335405722a0cSKuninori Morimoto 		*dai_name = dlc.dai_name;
335505722a0cSKuninori Morimoto 
335605722a0cSKuninori Morimoto 	return ret;
335705722a0cSKuninori Morimoto }
33581ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
335993b0f3eeSJean-Francois Moine 
336093b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
33613c8b5861SKuninori Morimoto 			    const char **dai_name, int index)
336293b0f3eeSJean-Francois Moine {
336305722a0cSKuninori Morimoto 	struct snd_soc_dai_link_component dlc;
33643c8b5861SKuninori Morimoto 	int ret = snd_soc_of_get_dlc(of_node, NULL, &dlc, index);
336593b0f3eeSJean-Francois Moine 
336605722a0cSKuninori Morimoto 	if (ret == 0)
336705722a0cSKuninori Morimoto 		*dai_name = dlc.dai_name;
3368cb470087SKuninori Morimoto 
3369cb470087SKuninori Morimoto 	return ret;
3370cb470087SKuninori Morimoto }
3371cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3372cb470087SKuninori Morimoto 
33739cc69528SKuninori Morimoto static void __snd_soc_of_put_component(struct snd_soc_dai_link_component *component)
33749cc69528SKuninori Morimoto {
33759cc69528SKuninori Morimoto 	if (component->of_node) {
33769cc69528SKuninori Morimoto 		of_node_put(component->of_node);
33779cc69528SKuninori Morimoto 		component->of_node = NULL;
33789cc69528SKuninori Morimoto 	}
33799cc69528SKuninori Morimoto }
33809cc69528SKuninori Morimoto 
33819cc69528SKuninori Morimoto static int __snd_soc_of_get_dai_link_component_alloc(
33829cc69528SKuninori Morimoto 	struct device *dev, struct device_node *of_node,
33839cc69528SKuninori Morimoto 	struct snd_soc_dai_link_component **ret_component,
33849cc69528SKuninori Morimoto 	int *ret_num)
33859cc69528SKuninori Morimoto {
33869cc69528SKuninori Morimoto 	struct snd_soc_dai_link_component *component;
33879cc69528SKuninori Morimoto 	int num;
33889cc69528SKuninori Morimoto 
33899cc69528SKuninori Morimoto 	/* Count the number of CPUs/CODECs */
33909cc69528SKuninori Morimoto 	num = of_count_phandle_with_args(of_node, "sound-dai", "#sound-dai-cells");
33919cc69528SKuninori Morimoto 	if (num <= 0) {
33929cc69528SKuninori Morimoto 		if (num == -ENOENT)
33939cc69528SKuninori Morimoto 			dev_err(dev, "No 'sound-dai' property\n");
33949cc69528SKuninori Morimoto 		else
33959cc69528SKuninori Morimoto 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
33969cc69528SKuninori Morimoto 		return num;
33979cc69528SKuninori Morimoto 	}
33989cc69528SKuninori Morimoto 	component = devm_kcalloc(dev, num, sizeof(*component), GFP_KERNEL);
33999cc69528SKuninori Morimoto 	if (!component)
34009cc69528SKuninori Morimoto 		return -ENOMEM;
34019cc69528SKuninori Morimoto 
34029cc69528SKuninori Morimoto 	*ret_component	= component;
34039cc69528SKuninori Morimoto 	*ret_num	= num;
34049cc69528SKuninori Morimoto 
34059cc69528SKuninori Morimoto 	return 0;
34069cc69528SKuninori Morimoto }
34079cc69528SKuninori Morimoto 
340893b0f3eeSJean-Francois Moine /*
340994685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
341094685763SSylwester Nawrocki  * @dai_link: DAI link
341194685763SSylwester Nawrocki  *
341294685763SSylwester Nawrocki  * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
341394685763SSylwester Nawrocki  */
341494685763SSylwester Nawrocki void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
341594685763SSylwester Nawrocki {
34163db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *component;
341794685763SSylwester Nawrocki 	int index;
341894685763SSylwester Nawrocki 
34199cc69528SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component)
34209cc69528SKuninori Morimoto 		__snd_soc_of_put_component(component);
342194685763SSylwester Nawrocki }
342294685763SSylwester Nawrocki EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
342394685763SSylwester Nawrocki 
342494685763SSylwester Nawrocki /*
342593b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
342693b0f3eeSJean-Francois Moine  * @dev: Card device
342793b0f3eeSJean-Francois Moine  * @of_node: Device node
342893b0f3eeSJean-Francois Moine  * @dai_link: DAI link
342993b0f3eeSJean-Francois Moine  *
343093b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
343193b0f3eeSJean-Francois Moine  * 'sound-dai'.
343293b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
343394685763SSylwester Nawrocki  * The device nodes in the array (of_node) must be dereferenced by calling
343494685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs() on @dai_link.
343593b0f3eeSJean-Francois Moine  *
343693b0f3eeSJean-Francois Moine  * Returns 0 for success
343793b0f3eeSJean-Francois Moine  */
343893b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
343993b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
344093b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
344193b0f3eeSJean-Francois Moine {
344293b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
34439cc69528SKuninori Morimoto 	int index, ret;
344493b0f3eeSJean-Francois Moine 
34459cc69528SKuninori Morimoto 	ret = __snd_soc_of_get_dai_link_component_alloc(dev, of_node,
34469cc69528SKuninori Morimoto 					 &dai_link->codecs, &dai_link->num_codecs);
34479cc69528SKuninori Morimoto 	if (ret < 0)
34489cc69528SKuninori Morimoto 		return ret;
344993b0f3eeSJean-Francois Moine 
345093b0f3eeSJean-Francois Moine 	/* Parse the list */
34513db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
345214c9b25fSKuninori Morimoto 		ret = snd_soc_of_get_dlc(of_node, NULL, component, index);
345393b0f3eeSJean-Francois Moine 		if (ret)
345493b0f3eeSJean-Francois Moine 			goto err;
345593b0f3eeSJean-Francois Moine 	}
345693b0f3eeSJean-Francois Moine 	return 0;
345793b0f3eeSJean-Francois Moine err:
345894685763SSylwester Nawrocki 	snd_soc_of_put_dai_link_codecs(dai_link);
345993b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
346093b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
346193b0f3eeSJean-Francois Moine 	return ret;
346293b0f3eeSJean-Francois Moine }
346393b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
346493b0f3eeSJean-Francois Moine 
3465900dedd7SMartin Povišer /*
3466900dedd7SMartin Povišer  * snd_soc_of_put_dai_link_cpus - Dereference device nodes in the codecs array
3467900dedd7SMartin Povišer  * @dai_link: DAI link
3468900dedd7SMartin Povišer  *
3469900dedd7SMartin Povišer  * Dereference device nodes acquired by snd_soc_of_get_dai_link_cpus().
3470900dedd7SMartin Povišer  */
3471900dedd7SMartin Povišer void snd_soc_of_put_dai_link_cpus(struct snd_soc_dai_link *dai_link)
3472900dedd7SMartin Povišer {
3473900dedd7SMartin Povišer 	struct snd_soc_dai_link_component *component;
3474900dedd7SMartin Povišer 	int index;
3475900dedd7SMartin Povišer 
34769cc69528SKuninori Morimoto 	for_each_link_cpus(dai_link, index, component)
34779cc69528SKuninori Morimoto 		__snd_soc_of_put_component(component);
3478900dedd7SMartin Povišer }
3479900dedd7SMartin Povišer EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_cpus);
3480900dedd7SMartin Povišer 
3481900dedd7SMartin Povišer /*
3482900dedd7SMartin Povišer  * snd_soc_of_get_dai_link_cpus - Parse a list of CPU DAIs in the devicetree
3483900dedd7SMartin Povišer  * @dev: Card device
3484900dedd7SMartin Povišer  * @of_node: Device node
3485900dedd7SMartin Povišer  * @dai_link: DAI link
3486900dedd7SMartin Povišer  *
3487900dedd7SMartin Povišer  * Is analogous to snd_soc_of_get_dai_link_codecs but parses a list of CPU DAIs
3488900dedd7SMartin Povišer  * instead.
3489900dedd7SMartin Povišer  *
3490900dedd7SMartin Povišer  * Returns 0 for success
3491900dedd7SMartin Povišer  */
3492900dedd7SMartin Povišer int snd_soc_of_get_dai_link_cpus(struct device *dev,
3493900dedd7SMartin Povišer 				 struct device_node *of_node,
3494900dedd7SMartin Povišer 				 struct snd_soc_dai_link *dai_link)
3495900dedd7SMartin Povišer {
3496900dedd7SMartin Povišer 	struct snd_soc_dai_link_component *component;
34979cc69528SKuninori Morimoto 	int index, ret;
3498900dedd7SMartin Povišer 
3499586fb264SKuninori Morimoto 	/* Count the number of CPUs */
35009cc69528SKuninori Morimoto 	ret = __snd_soc_of_get_dai_link_component_alloc(dev, of_node,
35019cc69528SKuninori Morimoto 					 &dai_link->cpus, &dai_link->num_cpus);
35029cc69528SKuninori Morimoto 	if (ret < 0)
35039cc69528SKuninori Morimoto 		return ret;
3504900dedd7SMartin Povišer 
3505900dedd7SMartin Povišer 	/* Parse the list */
3506900dedd7SMartin Povišer 	for_each_link_cpus(dai_link, index, component) {
350714c9b25fSKuninori Morimoto 		ret = snd_soc_of_get_dlc(of_node, NULL, component, index);
3508900dedd7SMartin Povišer 		if (ret)
3509900dedd7SMartin Povišer 			goto err;
3510900dedd7SMartin Povišer 	}
3511900dedd7SMartin Povišer 	return 0;
3512900dedd7SMartin Povišer err:
3513586fb264SKuninori Morimoto 	snd_soc_of_put_dai_link_cpus(dai_link);
3514900dedd7SMartin Povišer 	dai_link->cpus = NULL;
3515900dedd7SMartin Povišer 	dai_link->num_cpus = 0;
3516900dedd7SMartin Povišer 	return ret;
3517900dedd7SMartin Povišer }
3518900dedd7SMartin Povišer EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_cpus);
3519900dedd7SMartin Povišer 
3520c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3521db2a4165SFrank Mandarino {
35226ec27c53SChen Zhongjin 	int ret;
3523fb257897SMark Brown 
35246ec27c53SChen Zhongjin 	snd_soc_debugfs_init();
35256ec27c53SChen Zhongjin 	ret = snd_soc_util_init();
35266ec27c53SChen Zhongjin 	if (ret)
35276ec27c53SChen Zhongjin 		goto err_util_init;
35286ec27c53SChen Zhongjin 
35296ec27c53SChen Zhongjin 	ret = platform_driver_register(&soc_driver);
35306ec27c53SChen Zhongjin 	if (ret)
35316ec27c53SChen Zhongjin 		goto err_register;
35326ec27c53SChen Zhongjin 	return 0;
35336ec27c53SChen Zhongjin 
35346ec27c53SChen Zhongjin err_register:
35356ec27c53SChen Zhongjin 	snd_soc_util_exit();
35366ec27c53SChen Zhongjin err_util_init:
35376ec27c53SChen Zhongjin 	snd_soc_debugfs_exit();
35386ec27c53SChen Zhongjin 	return ret;
3539db2a4165SFrank Mandarino }
35404abe8e16SMark Brown module_init(snd_soc_init);
3541db2a4165SFrank Mandarino 
35427d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3543db2a4165SFrank Mandarino {
3544fb257897SMark Brown 	snd_soc_util_exit();
35456553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3546fb257897SMark Brown 
3547db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3548db2a4165SFrank Mandarino }
3549db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3550db2a4165SFrank Mandarino 
3551db2a4165SFrank Mandarino /* Module information */
3552d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3553db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3554db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
35558b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3556