xref: /openbmc/linux/sound/soc/soc-core.c (revision 6ec27c53886c8963729885bcf2dd996eba2767a7)
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 
35183f94a2eSKuninori Morimoto 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
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 
36783f94a2eSKuninori Morimoto 	mutex_unlock(&rtd->card->pcm_mutex);
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) +
450613fb500SKuninori Morimoto 			   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 */
556c5599b87SLars-Peter Clausen 	if (!card->instantiated)
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 */
698c5599b87SLars-Peter Clausen 	if (!card->instantiated)
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 
94221a00fb3SPierre-Louis Bossart 	/* release machine specific resources */
94321a00fb3SPierre-Louis Bossart 	snd_soc_link_exit(rtd);
94421a00fb3SPierre-Louis Bossart 
945da704f26SKuninori Morimoto 	/*
946da704f26SKuninori Morimoto 	 * Notify the machine driver for extra destruction
947da704f26SKuninori Morimoto 	 */
948fcbbcc32SKuninori Morimoto 	snd_soc_card_remove_dai_link(card, rtd->dai_link);
949da704f26SKuninori Morimoto 
950bc7a9091SKuninori Morimoto 	soc_free_pcm_runtime(rtd);
951bc7a9091SKuninori Morimoto }
95250cd9b53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_remove_pcm_runtime);
953bc7a9091SKuninori Morimoto 
95463dc47daSKuninori Morimoto /**
9550c048004SKuninori Morimoto  * snd_soc_add_pcm_runtime - Add a pcm_runtime dynamically via dai_link
9560c048004SKuninori Morimoto  * @card: The ASoC card to which the pcm_runtime is added
9570c048004SKuninori Morimoto  * @dai_link: The DAI link to find pcm_runtime
95863dc47daSKuninori Morimoto  *
9590c048004SKuninori Morimoto  * This function adds a pcm_runtime ASoC card by using dai_link.
96063dc47daSKuninori Morimoto  *
9610c048004SKuninori Morimoto  * Note: Topology can use this API to add pcm_runtime when probing the
96263dc47daSKuninori Morimoto  * topology component. And machine drivers can still define static
96363dc47daSKuninori Morimoto  * DAI links in dai_link array.
96463dc47daSKuninori Morimoto  */
9650c048004SKuninori Morimoto int snd_soc_add_pcm_runtime(struct snd_soc_card *card,
9666f2f1ff0SMengdong Lin 			    struct snd_soc_dai_link *dai_link)
967db2a4165SFrank Mandarino {
9681a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
96976afa643SShreyas NC 	struct snd_soc_dai_link_component *codec, *platform, *cpu;
97090be711eSKuninori Morimoto 	struct snd_soc_component *component;
971bfce78a5SKuninori Morimoto 	int i, ret;
972db2a4165SFrank Mandarino 
97363dc47daSKuninori Morimoto 	lockdep_assert_held(&client_mutex);
97463dc47daSKuninori Morimoto 
97563dc47daSKuninori Morimoto 	/*
97663dc47daSKuninori Morimoto 	 * Notify the machine driver for extra initialization
97763dc47daSKuninori Morimoto 	 */
978cbc7a6b5SKuninori Morimoto 	ret = snd_soc_card_add_dai_link(card, dai_link);
979cbc7a6b5SKuninori Morimoto 	if (ret < 0)
980cbc7a6b5SKuninori Morimoto 		return ret;
98163dc47daSKuninori Morimoto 
982a655de80SLiam Girdwood 	if (dai_link->ignore)
983a655de80SLiam Girdwood 		return 0;
984a655de80SLiam Girdwood 
9856f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
9866308419aSMark Brown 
987bfce78a5SKuninori Morimoto 	ret = soc_dai_link_sanity_check(card, dai_link);
988bfce78a5SKuninori Morimoto 	if (ret < 0)
989bfce78a5SKuninori Morimoto 		return ret;
990bfce78a5SKuninori Morimoto 
991513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
992513cb311SSudip Mukherjee 	if (!rtd)
993513cb311SSudip Mukherjee 		return -ENOMEM;
994513cb311SSudip Mukherjee 
99576afa643SShreyas NC 	for_each_link_cpus(dai_link, i, cpu) {
996c2233a26SKuninori Morimoto 		asoc_rtd_to_cpu(rtd, i) = snd_soc_find_dai(cpu);
997c2233a26SKuninori Morimoto 		if (!asoc_rtd_to_cpu(rtd, i)) {
9986b490879SMartin Hundebøll 			dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
99976afa643SShreyas NC 				 cpu->dai_name);
10001a497983SMengdong Lin 			goto _err_defer;
1001c5af3a2eSMark Brown 		}
1002c2233a26SKuninori Morimoto 		snd_soc_rtd_add_component(rtd, asoc_rtd_to_cpu(rtd, i)->component);
100376afa643SShreyas NC 	}
100476afa643SShreyas NC 
100588bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
100634614739SJerome Brunet 	for_each_link_codecs(dai_link, i, codec) {
1007c2233a26SKuninori Morimoto 		asoc_rtd_to_codec(rtd, i) = snd_soc_find_dai(codec);
1008c2233a26SKuninori Morimoto 		if (!asoc_rtd_to_codec(rtd, i)) {
10097c7e2d6aSStefan Agner 			dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
101034614739SJerome Brunet 				 codec->dai_name);
10111a497983SMengdong Lin 			goto _err_defer;
101212023a9aSMisael Lopez Cruz 		}
101334614739SJerome Brunet 
1014c2233a26SKuninori Morimoto 		snd_soc_rtd_add_component(rtd, asoc_rtd_to_codec(rtd, i)->component);
101588bd870fSBenoit Cousson 	}
101688bd870fSBenoit Cousson 
1017e2b30edfSKuninori Morimoto 	/* Find PLATFORM from registered PLATFORMs */
101834614739SJerome Brunet 	for_each_link_platforms(dai_link, i, platform) {
1019368dee94SKuninori Morimoto 		for_each_component(component) {
102034614739SJerome Brunet 			if (!snd_soc_is_matching_component(platform, component))
102190be711eSKuninori Morimoto 				continue;
102290be711eSKuninori Morimoto 
102312b05232SKuninori Morimoto 			snd_soc_rtd_add_component(rtd, component);
102490be711eSKuninori Morimoto 		}
102534614739SJerome Brunet 	}
102690be711eSKuninori Morimoto 
1027b19e6e7bSMark Brown 	return 0;
10281a497983SMengdong Lin 
10291a497983SMengdong Lin _err_defer:
103050cd9b53SKuninori Morimoto 	snd_soc_remove_pcm_runtime(card, rtd);
10311a497983SMengdong Lin 	return -EPROBE_DEFER;
10326b05eda6SMark Brown }
10330c048004SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_pcm_runtime);
10346b05eda6SMark Brown 
1035ba9e82a1SKuninori Morimoto static void snd_soc_runtime_get_dai_fmt(struct snd_soc_pcm_runtime *rtd)
1036ba9e82a1SKuninori Morimoto {
1037ba9e82a1SKuninori Morimoto 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1038ba9e82a1SKuninori Morimoto 	struct snd_soc_dai *dai, *not_used;
1039ba9e82a1SKuninori Morimoto 	struct device *dev = rtd->dev;
1040ba9e82a1SKuninori Morimoto 	u64 pos, possible_fmt;
1041ba9e82a1SKuninori Morimoto 	unsigned int mask = 0, dai_fmt = 0;
1042ba9e82a1SKuninori Morimoto 	int i, j, priority, pri, until;
1043ba9e82a1SKuninori Morimoto 
1044ba9e82a1SKuninori Morimoto 	/*
1045ba9e82a1SKuninori Morimoto 	 * Get selectable format from each DAIs.
1046ba9e82a1SKuninori Morimoto 	 *
1047ba9e82a1SKuninori Morimoto 	 ****************************
1048ba9e82a1SKuninori Morimoto 	 *            NOTE
1049ba9e82a1SKuninori Morimoto 	 * Using .auto_selectable_formats is not mandatory,
1050ba9e82a1SKuninori Morimoto 	 * we can select format manually from Sound Card.
1051ba9e82a1SKuninori Morimoto 	 * When use it, driver should list well tested format only.
1052ba9e82a1SKuninori Morimoto 	 ****************************
1053ba9e82a1SKuninori Morimoto 	 *
1054ba9e82a1SKuninori Morimoto 	 * ex)
1055ba9e82a1SKuninori Morimoto 	 *	auto_selectable_formats (= SND_SOC_POSSIBLE_xxx)
1056ba9e82a1SKuninori Morimoto 	 *		 (A)	 (B)	 (C)
1057ba9e82a1SKuninori Morimoto 	 *	DAI0_: { 0x000F, 0x00F0, 0x0F00 };
1058ba9e82a1SKuninori Morimoto 	 *	DAI1 : { 0xF000, 0x0F00 };
1059ba9e82a1SKuninori Morimoto 	 *		 (X)	 (Y)
1060ba9e82a1SKuninori Morimoto 	 *
1061ba9e82a1SKuninori Morimoto 	 * "until" will be 3 in this case (MAX array size from DAI0 and DAI1)
1062ba9e82a1SKuninori Morimoto 	 * Here is dev_dbg() message and comments
1063ba9e82a1SKuninori Morimoto 	 *
1064ba9e82a1SKuninori Morimoto 	 * priority = 1
1065ba9e82a1SKuninori Morimoto 	 * DAI0: (pri, fmt) = (1, 000000000000000F) // 1st check (A) DAI1 is not selected
1066ba9e82a1SKuninori Morimoto 	 * DAI1: (pri, fmt) = (0, 0000000000000000) //               Necessary Waste
1067ba9e82a1SKuninori Morimoto 	 * DAI0: (pri, fmt) = (1, 000000000000000F) // 2nd check (A)
1068ba9e82a1SKuninori Morimoto 	 * DAI1: (pri, fmt) = (1, 000000000000F000) //           (X)
1069ba9e82a1SKuninori Morimoto 	 * priority = 2
1070ba9e82a1SKuninori Morimoto 	 * DAI0: (pri, fmt) = (2, 00000000000000FF) // 3rd check (A) + (B)
1071ba9e82a1SKuninori Morimoto 	 * DAI1: (pri, fmt) = (1, 000000000000F000) //           (X)
1072ba9e82a1SKuninori Morimoto 	 * DAI0: (pri, fmt) = (2, 00000000000000FF) // 4th check (A) + (B)
1073ba9e82a1SKuninori Morimoto 	 * DAI1: (pri, fmt) = (2, 000000000000FF00) //           (X) + (Y)
1074ba9e82a1SKuninori Morimoto 	 * priority = 3
1075ba9e82a1SKuninori Morimoto 	 * DAI0: (pri, fmt) = (3, 0000000000000FFF) // 5th check (A) + (B) + (C)
1076ba9e82a1SKuninori Morimoto 	 * DAI1: (pri, fmt) = (2, 000000000000FF00) //           (X) + (Y)
1077ba9e82a1SKuninori Morimoto 	 * found auto selected format: 0000000000000F00
1078ba9e82a1SKuninori Morimoto 	 */
1079ba9e82a1SKuninori Morimoto 	until = snd_soc_dai_get_fmt_max_priority(rtd);
1080ba9e82a1SKuninori Morimoto 	for (priority = 1; priority <= until; priority++) {
1081ba9e82a1SKuninori Morimoto 
1082ba9e82a1SKuninori Morimoto 		dev_dbg(dev, "priority = %d\n", priority);
1083ba9e82a1SKuninori Morimoto 		for_each_rtd_dais(rtd, j, not_used) {
1084ba9e82a1SKuninori Morimoto 
1085ba9e82a1SKuninori Morimoto 			possible_fmt = ULLONG_MAX;
1086ba9e82a1SKuninori Morimoto 			for_each_rtd_dais(rtd, i, dai) {
1087ba9e82a1SKuninori Morimoto 				u64 fmt = 0;
1088ba9e82a1SKuninori Morimoto 
1089ba9e82a1SKuninori Morimoto 				pri = (j >= i) ? priority : priority - 1;
1090ba9e82a1SKuninori Morimoto 				fmt = snd_soc_dai_get_fmt(dai, pri);
1091ba9e82a1SKuninori Morimoto 				dev_dbg(dev, "%s: (pri, fmt) = (%d, %016llX)\n", dai->name, pri, fmt);
1092ba9e82a1SKuninori Morimoto 				possible_fmt &= fmt;
1093ba9e82a1SKuninori Morimoto 			}
1094ba9e82a1SKuninori Morimoto 			if (possible_fmt)
1095ba9e82a1SKuninori Morimoto 				goto found;
1096ba9e82a1SKuninori Morimoto 		}
1097ba9e82a1SKuninori Morimoto 	}
1098ba9e82a1SKuninori Morimoto 	/* Not Found */
1099ba9e82a1SKuninori Morimoto 	return;
1100ba9e82a1SKuninori Morimoto found:
1101ba9e82a1SKuninori Morimoto 	dev_dbg(dev, "found auto selected format: %016llX\n", possible_fmt);
1102ba9e82a1SKuninori Morimoto 
1103ba9e82a1SKuninori Morimoto 	/*
1104ba9e82a1SKuninori Morimoto 	 * convert POSSIBLE_DAIFMT to DAIFMT
1105ba9e82a1SKuninori Morimoto 	 *
1106ba9e82a1SKuninori Morimoto 	 * Some basic/default settings on each is defined as 0.
1107ba9e82a1SKuninori Morimoto 	 * see
1108ba9e82a1SKuninori Morimoto 	 *	SND_SOC_DAIFMT_NB_NF
1109ba9e82a1SKuninori Morimoto 	 *	SND_SOC_DAIFMT_GATED
1110ba9e82a1SKuninori Morimoto 	 *
1111ba9e82a1SKuninori Morimoto 	 * SND_SOC_DAIFMT_xxx_MASK can't notice it if Sound Card specify
1112ba9e82a1SKuninori Morimoto 	 * these value, and will be overwrite to auto selected value.
1113ba9e82a1SKuninori Morimoto 	 *
1114ba9e82a1SKuninori Morimoto 	 * To avoid such issue, loop from 63 to 0 here.
1115ba9e82a1SKuninori Morimoto 	 * Small number of SND_SOC_POSSIBLE_xxx will be Hi priority.
1116ba9e82a1SKuninori Morimoto 	 * Basic/Default settings of each part and aboves are defined
1117ba9e82a1SKuninori Morimoto 	 * as Hi priority (= small number) of SND_SOC_POSSIBLE_xxx.
1118ba9e82a1SKuninori Morimoto 	 */
1119ba9e82a1SKuninori Morimoto 	for (i = 63; i >= 0; i--) {
1120ba9e82a1SKuninori Morimoto 		pos = 1ULL << i;
1121ba9e82a1SKuninori Morimoto 		switch (possible_fmt & pos) {
1122ba9e82a1SKuninori Morimoto 		/*
1123ba9e82a1SKuninori Morimoto 		 * for format
1124ba9e82a1SKuninori Morimoto 		 */
1125ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_I2S:
1126ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_RIGHT_J:
1127ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_LEFT_J:
1128ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_DSP_A:
1129ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_DSP_B:
1130ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_AC97:
1131ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_PDM:
1132ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_FORMAT_MASK) | i;
1133ba9e82a1SKuninori Morimoto 			break;
1134ba9e82a1SKuninori Morimoto 		/*
1135ba9e82a1SKuninori Morimoto 		 * for clock
1136ba9e82a1SKuninori Morimoto 		 */
1137ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_CONT:
1138ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_MASK) | SND_SOC_DAIFMT_CONT;
1139ba9e82a1SKuninori Morimoto 			break;
1140ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_GATED:
1141ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_MASK) | SND_SOC_DAIFMT_GATED;
1142ba9e82a1SKuninori Morimoto 			break;
1143ba9e82a1SKuninori Morimoto 		/*
1144ba9e82a1SKuninori Morimoto 		 * for clock invert
1145ba9e82a1SKuninori Morimoto 		 */
1146ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_NB_NF:
1147ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_NB_NF;
1148ba9e82a1SKuninori Morimoto 			break;
1149ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_NB_IF:
1150ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_NB_IF;
1151ba9e82a1SKuninori Morimoto 			break;
1152ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_IB_NF:
1153ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_IB_NF;
1154ba9e82a1SKuninori Morimoto 			break;
1155ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_IB_IF:
1156ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_IB_IF;
1157ba9e82a1SKuninori Morimoto 			break;
1158ba9e82a1SKuninori Morimoto 		/*
1159ba9e82a1SKuninori Morimoto 		 * for clock provider / consumer
1160ba9e82a1SKuninori Morimoto 		 */
1161ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_CBP_CFP:
1162ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBP_CFP;
1163ba9e82a1SKuninori Morimoto 			break;
1164ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_CBC_CFP:
1165ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBC_CFP;
1166ba9e82a1SKuninori Morimoto 			break;
1167ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_CBP_CFC:
1168ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBP_CFC;
1169ba9e82a1SKuninori Morimoto 			break;
1170ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_CBC_CFC:
1171ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBC_CFC;
1172ba9e82a1SKuninori Morimoto 			break;
1173ba9e82a1SKuninori Morimoto 		}
1174ba9e82a1SKuninori Morimoto 	}
1175ba9e82a1SKuninori Morimoto 
1176ba9e82a1SKuninori Morimoto 	/*
1177ba9e82a1SKuninori Morimoto 	 * Some driver might have very complex limitation.
1178ba9e82a1SKuninori Morimoto 	 * In such case, user want to auto-select non-limitation part,
1179ba9e82a1SKuninori Morimoto 	 * and want to manually specify complex part.
1180ba9e82a1SKuninori Morimoto 	 *
1181ba9e82a1SKuninori Morimoto 	 * Or for example, if both CPU and Codec can be clock provider,
1182ba9e82a1SKuninori Morimoto 	 * but because of its quality, user want to specify it manually.
1183ba9e82a1SKuninori Morimoto 	 *
1184ba9e82a1SKuninori Morimoto 	 * Use manually specified settings if sound card did.
1185ba9e82a1SKuninori Morimoto 	 */
1186ba9e82a1SKuninori Morimoto 	if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK))
1187ba9e82a1SKuninori Morimoto 		mask |= SND_SOC_DAIFMT_FORMAT_MASK;
1188ba9e82a1SKuninori Morimoto 	if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_CLOCK_MASK))
1189ba9e82a1SKuninori Morimoto 		mask |= SND_SOC_DAIFMT_CLOCK_MASK;
1190ba9e82a1SKuninori Morimoto 	if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_INV_MASK))
1191ba9e82a1SKuninori Morimoto 		mask |= SND_SOC_DAIFMT_INV_MASK;
1192640eac4cSKuninori Morimoto 	if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK))
1193640eac4cSKuninori Morimoto 		mask |= SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK;
1194ba9e82a1SKuninori Morimoto 
1195ba9e82a1SKuninori Morimoto 	dai_link->dai_fmt |= (dai_fmt & mask);
1196ba9e82a1SKuninori Morimoto }
1197ba9e82a1SKuninori Morimoto 
11984d1a98b5SKuninori Morimoto /**
11994d1a98b5SKuninori Morimoto  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
12004d1a98b5SKuninori Morimoto  * @rtd: The runtime for which the DAI link format should be changed
12014d1a98b5SKuninori Morimoto  * @dai_fmt: The new DAI link format
12024d1a98b5SKuninori Morimoto  *
12034d1a98b5SKuninori Morimoto  * This function updates the DAI link format for all DAIs connected to the DAI
12044d1a98b5SKuninori Morimoto  * link for the specified runtime.
12054d1a98b5SKuninori Morimoto  *
12064d1a98b5SKuninori Morimoto  * Note: For setups with a static format set the dai_fmt field in the
12074d1a98b5SKuninori Morimoto  * corresponding snd_dai_link struct instead of using this function.
12084d1a98b5SKuninori Morimoto  *
12094d1a98b5SKuninori Morimoto  * Returns 0 on success, otherwise a negative error code.
12104d1a98b5SKuninori Morimoto  */
12114d1a98b5SKuninori Morimoto int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
12124d1a98b5SKuninori Morimoto 				unsigned int dai_fmt)
12134d1a98b5SKuninori Morimoto {
12144d1a98b5SKuninori Morimoto 	struct snd_soc_dai *cpu_dai;
12154d1a98b5SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
12164d1a98b5SKuninori Morimoto 	unsigned int i;
12174d1a98b5SKuninori Morimoto 	int ret;
12184d1a98b5SKuninori Morimoto 
12197db07e37SKuninori Morimoto 	if (!dai_fmt)
12207db07e37SKuninori Morimoto 		return 0;
12217db07e37SKuninori Morimoto 
12224d1a98b5SKuninori Morimoto 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
12234d1a98b5SKuninori Morimoto 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
12244d1a98b5SKuninori Morimoto 		if (ret != 0 && ret != -ENOTSUPP)
12254d1a98b5SKuninori Morimoto 			return ret;
12264d1a98b5SKuninori Morimoto 	}
12274d1a98b5SKuninori Morimoto 
12286c076273SCharles Keepax 	/* Flip the polarity for the "CPU" end of link */
12296c076273SCharles Keepax 	dai_fmt = snd_soc_daifmt_clock_provider_flipped(dai_fmt);
1230b44a67f8SKuninori Morimoto 
12314d1a98b5SKuninori Morimoto 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
12326c076273SCharles Keepax 		ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
12334d1a98b5SKuninori Morimoto 		if (ret != 0 && ret != -ENOTSUPP)
12344d1a98b5SKuninori Morimoto 			return ret;
12354d1a98b5SKuninori Morimoto 	}
12364d1a98b5SKuninori Morimoto 
12374d1a98b5SKuninori Morimoto 	return 0;
12384d1a98b5SKuninori Morimoto }
12394d1a98b5SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
12404d1a98b5SKuninori Morimoto 
1241eaffeefbSKuninori Morimoto static int soc_init_pcm_runtime(struct snd_soc_card *card,
124246496acbSKuninori Morimoto 				struct snd_soc_pcm_runtime *rtd)
124346496acbSKuninori Morimoto {
124446496acbSKuninori Morimoto 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1245c2233a26SKuninori Morimoto 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
124646496acbSKuninori Morimoto 	struct snd_soc_component *component;
1247613fb500SKuninori Morimoto 	int ret, num, i;
124846496acbSKuninori Morimoto 
124946496acbSKuninori Morimoto 	/* do machine specific initialization */
125002e75636SKuninori Morimoto 	ret = snd_soc_link_init(rtd);
125102e75636SKuninori Morimoto 	if (ret < 0)
125246496acbSKuninori Morimoto 		return ret;
125346496acbSKuninori Morimoto 
1254ba9e82a1SKuninori Morimoto 	snd_soc_runtime_get_dai_fmt(rtd);
125546496acbSKuninori Morimoto 	ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
125646496acbSKuninori Morimoto 	if (ret)
125746496acbSKuninori Morimoto 		return ret;
125846496acbSKuninori Morimoto 
125946496acbSKuninori Morimoto 	/* add DPCM sysfs entries */
126046496acbSKuninori Morimoto 	soc_dpcm_debugfs_add(rtd);
126146496acbSKuninori Morimoto 
126246496acbSKuninori Morimoto 	num = rtd->num;
126346496acbSKuninori Morimoto 
126446496acbSKuninori Morimoto 	/*
126546496acbSKuninori Morimoto 	 * most drivers will register their PCMs using DAI link ordering but
126646496acbSKuninori Morimoto 	 * topology based drivers can use the DAI link id field to set PCM
126746496acbSKuninori Morimoto 	 * device number and then use rtd + a base offset of the BEs.
126846496acbSKuninori Morimoto 	 */
1269613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
127046496acbSKuninori Morimoto 		if (!component->driver->use_dai_pcm_id)
127146496acbSKuninori Morimoto 			continue;
127246496acbSKuninori Morimoto 
127346496acbSKuninori Morimoto 		if (rtd->dai_link->no_pcm)
127446496acbSKuninori Morimoto 			num += component->driver->be_pcm_base;
127546496acbSKuninori Morimoto 		else
127646496acbSKuninori Morimoto 			num = rtd->dai_link->id;
127746496acbSKuninori Morimoto 	}
127846496acbSKuninori Morimoto 
127946496acbSKuninori Morimoto 	/* create compress_device if possible */
128046496acbSKuninori Morimoto 	ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
128162462e01SKuninori Morimoto 	if (ret != -ENOTSUPP)
128246496acbSKuninori Morimoto 		return ret;
128346496acbSKuninori Morimoto 
128446496acbSKuninori Morimoto 	/* create the pcm */
128546496acbSKuninori Morimoto 	ret = soc_new_pcm(rtd, num);
128646496acbSKuninori Morimoto 	if (ret < 0) {
128746496acbSKuninori Morimoto 		dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
128846496acbSKuninori Morimoto 			dai_link->stream_name, ret);
128946496acbSKuninori Morimoto 		return ret;
129046496acbSKuninori Morimoto 	}
1291d1eb6d11SKuninori Morimoto 
12920b73ba55SKuninori Morimoto 	return snd_soc_pcm_dai_new(rtd);
129346496acbSKuninori Morimoto }
129446496acbSKuninori Morimoto 
1295ffd60fbaSKuninori Morimoto static void soc_set_name_prefix(struct snd_soc_card *card,
1296ffd60fbaSKuninori Morimoto 				struct snd_soc_component *component)
1297ffd60fbaSKuninori Morimoto {
1298aec3ff99SKuninori Morimoto 	struct device_node *of_node = soc_component_to_node(component);
129929d9fc7aSKuninori Morimoto 	const char *str;
130029d9fc7aSKuninori Morimoto 	int ret, i;
1301ffd60fbaSKuninori Morimoto 
13024702f991SKuninori Morimoto 	for (i = 0; i < card->num_configs; i++) {
1303ffd60fbaSKuninori Morimoto 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1304ffd60fbaSKuninori Morimoto 
13053256ef98SSameer Pujar 		if (snd_soc_is_matching_component(&map->dlc, component) &&
13063256ef98SSameer Pujar 		    map->name_prefix) {
1307ffd60fbaSKuninori Morimoto 			component->name_prefix = map->name_prefix;
1308ffd60fbaSKuninori Morimoto 			return;
1309ffd60fbaSKuninori Morimoto 		}
1310c13493a2SKuninori Morimoto 	}
1311ffd60fbaSKuninori Morimoto 
1312ffd60fbaSKuninori Morimoto 	/*
1313ffd60fbaSKuninori Morimoto 	 * If there is no configuration table or no match in the table,
1314ffd60fbaSKuninori Morimoto 	 * check if a prefix is provided in the node
1315ffd60fbaSKuninori Morimoto 	 */
131629d9fc7aSKuninori Morimoto 	ret = of_property_read_string(of_node, "sound-name-prefix", &str);
131729d9fc7aSKuninori Morimoto 	if (ret < 0)
131829d9fc7aSKuninori Morimoto 		return;
131929d9fc7aSKuninori Morimoto 
132029d9fc7aSKuninori Morimoto 	component->name_prefix = str;
1321ffd60fbaSKuninori Morimoto }
1322ffd60fbaSKuninori Morimoto 
1323c6619b72SKuninori Morimoto static void soc_remove_component(struct snd_soc_component *component,
1324c6619b72SKuninori Morimoto 				 int probed)
132522d14231SKuninori Morimoto {
1326c6619b72SKuninori Morimoto 
1327c6619b72SKuninori Morimoto 	if (!component->card)
1328c6619b72SKuninori Morimoto 		return;
1329c6619b72SKuninori Morimoto 
1330c6619b72SKuninori Morimoto 	if (probed)
1331c6619b72SKuninori Morimoto 		snd_soc_component_remove(component);
1332c6619b72SKuninori Morimoto 
13337a5d9815SBard liao 	list_del_init(&component->card_list);
133422d14231SKuninori Morimoto 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
133522d14231SKuninori Morimoto 	soc_cleanup_component_debugfs(component);
133622d14231SKuninori Morimoto 	component->card = NULL;
13370e36f36bSPierre-Louis Bossart 	snd_soc_component_module_put_when_remove(component);
133822d14231SKuninori Morimoto }
133922d14231SKuninori Morimoto 
1340ffd60fbaSKuninori Morimoto static int soc_probe_component(struct snd_soc_card *card,
1341ffd60fbaSKuninori Morimoto 			       struct snd_soc_component *component)
1342ffd60fbaSKuninori Morimoto {
1343ffd60fbaSKuninori Morimoto 	struct snd_soc_dapm_context *dapm =
1344ffd60fbaSKuninori Morimoto 		snd_soc_component_get_dapm(component);
1345ffd60fbaSKuninori Morimoto 	struct snd_soc_dai *dai;
1346c6619b72SKuninori Morimoto 	int probed = 0;
1347ffd60fbaSKuninori Morimoto 	int ret;
1348ffd60fbaSKuninori Morimoto 
13498f1a1681SKuninori Morimoto 	if (snd_soc_component_is_dummy(component))
1350ffd60fbaSKuninori Morimoto 		return 0;
1351ffd60fbaSKuninori Morimoto 
1352ffd60fbaSKuninori Morimoto 	if (component->card) {
1353ffd60fbaSKuninori Morimoto 		if (component->card != card) {
1354ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1355ffd60fbaSKuninori Morimoto 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1356ffd60fbaSKuninori Morimoto 				card->name, component->card->name);
1357ffd60fbaSKuninori Morimoto 			return -ENODEV;
1358ffd60fbaSKuninori Morimoto 		}
1359ffd60fbaSKuninori Morimoto 		return 0;
1360ffd60fbaSKuninori Morimoto 	}
1361ffd60fbaSKuninori Morimoto 
1362ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_module_get_when_probe(component);
1363ffd60fbaSKuninori Morimoto 	if (ret < 0)
1364ffd60fbaSKuninori Morimoto 		return ret;
1365ffd60fbaSKuninori Morimoto 
1366ffd60fbaSKuninori Morimoto 	component->card = card;
1367ffd60fbaSKuninori Morimoto 	soc_set_name_prefix(card, component);
1368ffd60fbaSKuninori Morimoto 
1369ffd60fbaSKuninori Morimoto 	soc_init_component_debugfs(component);
1370ffd60fbaSKuninori Morimoto 
137195c267ddSKuninori Morimoto 	snd_soc_dapm_init(dapm, card, component);
1372b614beafSKuninori Morimoto 
1373ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_new_controls(dapm,
1374ffd60fbaSKuninori Morimoto 					component->driver->dapm_widgets,
1375ffd60fbaSKuninori Morimoto 					component->driver->num_dapm_widgets);
1376ffd60fbaSKuninori Morimoto 
1377ffd60fbaSKuninori Morimoto 	if (ret != 0) {
1378ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1379ffd60fbaSKuninori Morimoto 			"Failed to create new controls %d\n", ret);
1380ffd60fbaSKuninori Morimoto 		goto err_probe;
1381ffd60fbaSKuninori Morimoto 	}
1382ffd60fbaSKuninori Morimoto 
1383ffd60fbaSKuninori Morimoto 	for_each_component_dais(component, dai) {
1384ffd60fbaSKuninori Morimoto 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1385ffd60fbaSKuninori Morimoto 		if (ret != 0) {
1386ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1387ffd60fbaSKuninori Morimoto 				"Failed to create DAI widgets %d\n", ret);
1388ffd60fbaSKuninori Morimoto 			goto err_probe;
1389ffd60fbaSKuninori Morimoto 		}
1390ffd60fbaSKuninori Morimoto 	}
1391ffd60fbaSKuninori Morimoto 
1392ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_probe(component);
139360adbd8fSKuninori Morimoto 	if (ret < 0)
1394ffd60fbaSKuninori Morimoto 		goto err_probe;
139560adbd8fSKuninori Morimoto 
1396ffd60fbaSKuninori Morimoto 	WARN(dapm->idle_bias_off &&
1397ffd60fbaSKuninori Morimoto 	     dapm->bias_level != SND_SOC_BIAS_OFF,
1398ffd60fbaSKuninori Morimoto 	     "codec %s can not start from non-off bias with idle_bias_off==1\n",
1399ffd60fbaSKuninori Morimoto 	     component->name);
1400c6619b72SKuninori Morimoto 	probed = 1;
1401ffd60fbaSKuninori Morimoto 
1402257c4dacSKuninori Morimoto 	/*
1403257c4dacSKuninori Morimoto 	 * machine specific init
1404257c4dacSKuninori Morimoto 	 * see
1405257c4dacSKuninori Morimoto 	 *	snd_soc_component_set_aux()
1406257c4dacSKuninori Morimoto 	 */
1407257c4dacSKuninori Morimoto 	ret = snd_soc_component_init(component);
1408257c4dacSKuninori Morimoto 	if (ret < 0)
1409ffd60fbaSKuninori Morimoto 		goto err_probe;
1410ffd60fbaSKuninori Morimoto 
1411ffd60fbaSKuninori Morimoto 	ret = snd_soc_add_component_controls(component,
1412ffd60fbaSKuninori Morimoto 					     component->driver->controls,
1413ffd60fbaSKuninori Morimoto 					     component->driver->num_controls);
1414ffd60fbaSKuninori Morimoto 	if (ret < 0)
1415ffd60fbaSKuninori Morimoto 		goto err_probe;
1416ffd60fbaSKuninori Morimoto 
1417ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(dapm,
1418ffd60fbaSKuninori Morimoto 				      component->driver->dapm_routes,
1419ffd60fbaSKuninori Morimoto 				      component->driver->num_dapm_routes);
1420a22ae72bSPierre-Louis Bossart 	if (ret < 0) {
1421a22ae72bSPierre-Louis Bossart 		if (card->disable_route_checks) {
1422a22ae72bSPierre-Louis Bossart 			dev_info(card->dev,
1423a22ae72bSPierre-Louis Bossart 				 "%s: disable_route_checks set, ignoring errors on add_routes\n",
1424a22ae72bSPierre-Louis Bossart 				 __func__);
1425a22ae72bSPierre-Louis Bossart 		} else {
1426a22ae72bSPierre-Louis Bossart 			dev_err(card->dev,
1427a22ae72bSPierre-Louis Bossart 				"%s: snd_soc_dapm_add_routes failed: %d\n",
1428a22ae72bSPierre-Louis Bossart 				__func__, ret);
1429ffd60fbaSKuninori Morimoto 			goto err_probe;
1430a22ae72bSPierre-Louis Bossart 		}
1431a22ae72bSPierre-Louis Bossart 	}
1432ffd60fbaSKuninori Morimoto 
1433ffd60fbaSKuninori Morimoto 	/* see for_each_card_components */
1434ffd60fbaSKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1435ffd60fbaSKuninori Morimoto 
1436ffd60fbaSKuninori Morimoto err_probe:
1437ffd60fbaSKuninori Morimoto 	if (ret < 0)
1438c6619b72SKuninori Morimoto 		soc_remove_component(component, probed);
1439ffd60fbaSKuninori Morimoto 
1440ffd60fbaSKuninori Morimoto 	return ret;
1441ffd60fbaSKuninori Morimoto }
1442ffd60fbaSKuninori Morimoto 
14434ca47d21SKuninori Morimoto static void soc_remove_link_dais(struct snd_soc_card *card)
1444f0fba2adSLiam Girdwood {
14454ca47d21SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
14464ca47d21SKuninori Morimoto 	int order;
14474ca47d21SKuninori Morimoto 
14484ca47d21SKuninori Morimoto 	for_each_comp_order(order) {
14494ca47d21SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
14507eaa313bSKuninori Morimoto 			/* remove all rtd connected DAIs in good order */
14517eaa313bSKuninori Morimoto 			snd_soc_pcm_dai_remove(rtd, order);
1452f0fba2adSLiam Girdwood 		}
14534ca47d21SKuninori Morimoto 	}
14544ca47d21SKuninori Morimoto }
1455f0fba2adSLiam Girdwood 
1456bc7c16c2SKuninori Morimoto static int soc_probe_link_dais(struct snd_soc_card *card)
1457bc7c16c2SKuninori Morimoto {
1458bc7c16c2SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
145951801aeaSKuninori Morimoto 	int order, ret;
1460bc7c16c2SKuninori Morimoto 
1461bc7c16c2SKuninori Morimoto 	for_each_comp_order(order) {
1462bc7c16c2SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1463bc7c16c2SKuninori Morimoto 
1464bc7c16c2SKuninori Morimoto 			dev_dbg(card->dev,
1465bc7c16c2SKuninori Morimoto 				"ASoC: probe %s dai link %d late %d\n",
1466bc7c16c2SKuninori Morimoto 				card->name, rtd->num, order);
1467bc7c16c2SKuninori Morimoto 
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 
189353e947a0SKuninori Morimoto 	/* remove and free each DAI */
18947ce6088fSKuninori Morimoto 	soc_remove_link_dais(card);
18950ced7b05SKuninori Morimoto 	soc_remove_link_components(card);
18967ce6088fSKuninori Morimoto 
1897cc733900SKuninori Morimoto 	for_each_card_rtds_safe(card, rtd, n)
189850cd9b53SKuninori Morimoto 		snd_soc_remove_pcm_runtime(card, rtd);
18997ce6088fSKuninori Morimoto 
190053e947a0SKuninori Morimoto 	/* remove auxiliary devices */
190153e947a0SKuninori Morimoto 	soc_remove_aux_devices(card);
1902e8fbd250SKuninori Morimoto 	soc_unbind_aux_dev(card);
190353e947a0SKuninori Morimoto 
190453e947a0SKuninori Morimoto 	snd_soc_dapm_free(&card->dapm);
190553e947a0SKuninori Morimoto 	soc_cleanup_card_debugfs(card);
190653e947a0SKuninori Morimoto 
190753e947a0SKuninori Morimoto 	/* remove the card */
1908b0275d95SKuninori Morimoto 	snd_soc_card_remove(card);
19090ced7b05SKuninori Morimoto 
19100ced7b05SKuninori Morimoto 	if (card->snd_card) {
19110ced7b05SKuninori Morimoto 		snd_card_free(card->snd_card);
19120ced7b05SKuninori Morimoto 		card->snd_card = NULL;
19130ced7b05SKuninori Morimoto 	}
191453e947a0SKuninori Morimoto }
191553e947a0SKuninori Morimoto 
19162cc1afcfSKuninori Morimoto static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
19172cc1afcfSKuninori Morimoto {
19182cc1afcfSKuninori Morimoto 	if (card->instantiated) {
19192cc1afcfSKuninori Morimoto 		card->instantiated = false;
19202cc1afcfSKuninori Morimoto 		snd_soc_flush_all_delayed_work(card);
19212cc1afcfSKuninori Morimoto 
192227f07cacSKuninori Morimoto 		soc_cleanup_card_resources(card);
19232cc1afcfSKuninori Morimoto 		if (!unregister)
19242cc1afcfSKuninori Morimoto 			list_add(&card->list, &unbind_card_list);
19252cc1afcfSKuninori Morimoto 	} else {
19262cc1afcfSKuninori Morimoto 		if (unregister)
19272cc1afcfSKuninori Morimoto 			list_del(&card->list);
19282cc1afcfSKuninori Morimoto 	}
19292cc1afcfSKuninori Morimoto }
19302cc1afcfSKuninori Morimoto 
1931ed90c013SKuninori Morimoto static int snd_soc_bind_card(struct snd_soc_card *card)
1932f0fba2adSLiam Girdwood {
19331a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
193476c39e86SKuninori Morimoto 	struct snd_soc_component *component;
193561b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
193627f07cacSKuninori Morimoto 	int ret, i;
193796dd3622SMark Brown 
193834e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
193901b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1940f0fba2adSLiam Girdwood 
194195c267ddSKuninori Morimoto 	snd_soc_dapm_init(&card->dapm, card, NULL);
194253e947a0SKuninori Morimoto 
1943a655de80SLiam Girdwood 	/* check whether any platform is ignore machine FE and using topology */
1944a655de80SLiam Girdwood 	soc_check_tplg_fes(card);
1945a655de80SLiam Girdwood 
194644c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
1947bee886f1SKuninori Morimoto 	ret = soc_bind_aux_dev(card);
1948bee886f1SKuninori Morimoto 	if (ret < 0)
194953e947a0SKuninori Morimoto 		goto probe_end;
1950db2a4165SFrank Mandarino 
1951f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
1952d8145989SKuninori Morimoto 	card->num_rtd = 0;
19535b99a0aaSKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link) {
19540c048004SKuninori Morimoto 		ret = snd_soc_add_pcm_runtime(card, dai_link);
19555b99a0aaSKuninori Morimoto 		if (ret < 0)
19565b99a0aaSKuninori Morimoto 			goto probe_end;
19575b99a0aaSKuninori Morimoto 	}
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) {
1991f110bfc7SLiam Girdwood 		dev_err(card->dev,
199262f07a6bSKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
199353e947a0SKuninori Morimoto 		goto probe_end;
199462ae68faSStephen Warren 	}
199562ae68faSStephen Warren 
1996f2ed6b07SMengdong Lin 	/* probe auxiliary components */
1997f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
199874bd3f92SKuninori Morimoto 	if (ret < 0) {
199974bd3f92SKuninori Morimoto 		dev_err(card->dev,
200074bd3f92SKuninori Morimoto 			"ASoC: failed to probe aux component %d\n", ret);
200153e947a0SKuninori Morimoto 		goto probe_end;
200274bd3f92SKuninori Morimoto 	}
2003f2ed6b07SMengdong Lin 
200462ae68faSStephen Warren 	/* probe all DAI links on this card */
2005c7e73774SKuninori Morimoto 	ret = soc_probe_link_dais(card);
2006fe3e78e0SMark Brown 	if (ret < 0) {
2007f110bfc7SLiam Girdwood 		dev_err(card->dev,
2008c7e73774SKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
200953e947a0SKuninori Morimoto 		goto probe_end;
2010fe3e78e0SMark Brown 	}
2011fe3e78e0SMark Brown 
2012626c2e57SKuninori Morimoto 	for_each_card_rtds(card, rtd) {
2013eaffeefbSKuninori Morimoto 		ret = soc_init_pcm_runtime(card, rtd);
2014626c2e57SKuninori Morimoto 		if (ret < 0)
2015626c2e57SKuninori Morimoto 			goto probe_end;
2016626c2e57SKuninori Morimoto 	}
2017c4b46982SKuninori Morimoto 
2018888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
2019b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
2020888df395SMark Brown 
20219b98c7c2SKuninori Morimoto 	ret = snd_soc_add_card_controls(card, card->controls,
20222c7b696aSMarcel Ziswiler 					card->num_controls);
20239b98c7c2SKuninori Morimoto 	if (ret < 0)
20249b98c7c2SKuninori Morimoto 		goto probe_end;
2025b7af1dafSMark Brown 
2026daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2027b8ad29deSMark Brown 				      card->num_dapm_routes);
2028a22ae72bSPierre-Louis Bossart 	if (ret < 0) {
2029a22ae72bSPierre-Louis Bossart 		if (card->disable_route_checks) {
2030a22ae72bSPierre-Louis Bossart 			dev_info(card->dev,
2031a22ae72bSPierre-Louis Bossart 				 "%s: disable_route_checks set, ignoring errors on add_routes\n",
2032a22ae72bSPierre-Louis Bossart 				 __func__);
2033a22ae72bSPierre-Louis Bossart 		} else {
2034a22ae72bSPierre-Louis Bossart 			dev_err(card->dev,
2035a22ae72bSPierre-Louis Bossart 				 "%s: snd_soc_dapm_add_routes failed: %d\n",
2036a22ae72bSPierre-Louis Bossart 				 __func__, ret);
2037daa480bdSKuninori Morimoto 			goto probe_end;
2038a22ae72bSPierre-Louis Bossart 		}
2039a22ae72bSPierre-Louis Bossart 	}
2040b8ad29deSMark Brown 
2041daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2042f23e860eSNicolin Chen 				      card->num_of_dapm_routes);
2043daa480bdSKuninori Morimoto 	if (ret < 0)
2044daa480bdSKuninori Morimoto 		goto probe_end;
204575d9ac46SMark Brown 
2046861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
2047861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
2048861886d3STakashi Iwai 
2049c8d18e44SJaroslav Kysela 	soc_setup_card_name(card, card->snd_card->shortname,
2050c8d18e44SJaroslav Kysela 			    card->name, NULL);
2051c8d18e44SJaroslav Kysela 	soc_setup_card_name(card, card->snd_card->longname,
2052c8d18e44SJaroslav Kysela 			    card->long_name, card->name);
2053c8d18e44SJaroslav Kysela 	soc_setup_card_name(card, card->snd_card->driver,
2054c8d18e44SJaroslav Kysela 			    card->driver_name, card->name);
2055fe3e78e0SMark Brown 
2056dc73d73aSJaroslav Kysela 	if (card->components) {
2057dc73d73aSJaroslav Kysela 		/* the current implementation of snd_component_add() accepts */
2058dc73d73aSJaroslav Kysela 		/* multiple components in the string separated by space, */
2059dc73d73aSJaroslav Kysela 		/* but the string collision (identical string) check might */
2060dc73d73aSJaroslav Kysela 		/* not work correctly */
2061dc73d73aSJaroslav Kysela 		ret = snd_component_add(card->snd_card, card->components);
2062dc73d73aSJaroslav Kysela 		if (ret < 0) {
2063dc73d73aSJaroslav Kysela 			dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n",
2064dc73d73aSJaroslav Kysela 				card->name, ret);
2065dc73d73aSJaroslav Kysela 			goto probe_end;
2066dc73d73aSJaroslav Kysela 		}
2067dc73d73aSJaroslav Kysela 	}
2068dc73d73aSJaroslav Kysela 
20695c0eac03SKuninori Morimoto 	ret = snd_soc_card_late_probe(card);
20705c0eac03SKuninori Morimoto 	if (ret < 0)
207153e947a0SKuninori Morimoto 		goto probe_end;
207228e9ad92SMark Brown 
2073824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
2074df4d27b1SMartin Povišer 	snd_soc_card_fixup_controls(card);
20758c193b8dSLars-Peter Clausen 
2076f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2077fe3e78e0SMark Brown 	if (ret < 0) {
2078f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2079f110bfc7SLiam Girdwood 				ret);
208053e947a0SKuninori Morimoto 		goto probe_end;
2081fe3e78e0SMark Brown 	}
2082fe3e78e0SMark Brown 
2083435c5e25SMark Brown 	card->instantiated = 1;
2084882eab6cSTzung-Bi Shih 	dapm_mark_endpoints_dirty(card);
20854f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2086b19e6e7bSMark Brown 
2087ed90c013SKuninori Morimoto 	/* deactivate pins to sleep state */
208876c39e86SKuninori Morimoto 	for_each_card_components(card, component)
2089b3dea624SKuninori Morimoto 		if (!snd_soc_component_active(component))
209076c39e86SKuninori Morimoto 			pinctrl_pm_select_sleep_state(component->dev);
2091ed90c013SKuninori Morimoto 
209253e947a0SKuninori Morimoto probe_end:
209353e947a0SKuninori Morimoto 	if (ret < 0)
209427f07cacSKuninori Morimoto 		soc_cleanup_card_resources(card);
2095db2a4165SFrank Mandarino 
2096f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
209734e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2098db2a4165SFrank Mandarino 
2099b19e6e7bSMark Brown 	return ret;
2100435c5e25SMark Brown }
2101435c5e25SMark Brown 
2102435c5e25SMark Brown /* probes a new socdev */
2103435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2104435c5e25SMark Brown {
2105f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2106435c5e25SMark Brown 
210770a7ca34SVinod Koul 	/*
210870a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
210970a7ca34SVinod Koul 	 * we should not be here in that case so ret error
211070a7ca34SVinod Koul 	 */
211170a7ca34SVinod Koul 	if (!card)
211270a7ca34SVinod Koul 		return -EINVAL;
211370a7ca34SVinod Koul 
2114fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2115f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2116fe4085e8SMark Brown 		 card->name);
2117fe4085e8SMark Brown 
2118435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2119435c5e25SMark Brown 	card->dev = &pdev->dev;
2120f0fba2adSLiam Girdwood 
2121ad61b78eSQinglang Miao 	return devm_snd_soc_register_card(&pdev->dev, card);
2122db2a4165SFrank Mandarino }
2123db2a4165SFrank Mandarino 
21246f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
212551737470SMark Brown {
21266f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
212776c39e86SKuninori Morimoto 	struct snd_soc_component *component;
212851737470SMark Brown 
212951737470SMark Brown 	if (!card->instantiated)
2130416356fcSMark Brown 		return 0;
213151737470SMark Brown 
21322c7b696aSMarcel Ziswiler 	/*
21332c7b696aSMarcel Ziswiler 	 * Flush out pmdown_time work - we actually do want to run it
21342c7b696aSMarcel Ziswiler 	 * now, we're shutting down so no imminent restart.
21352c7b696aSMarcel Ziswiler 	 */
213665462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
213751737470SMark Brown 
2138f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2139416356fcSMark Brown 
2140988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
214176c39e86SKuninori Morimoto 	for_each_card_components(card, component)
214276c39e86SKuninori Morimoto 		pinctrl_pm_select_sleep_state(component->dev);
2143988e8cc4SNicolin Chen 
2144416356fcSMark Brown 	return 0;
214551737470SMark Brown }
21466f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
214751737470SMark Brown 
21486f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2149b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2150b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2151b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2152b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
21536f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2154b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2155416356fcSMark Brown };
2156deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2157416356fcSMark Brown 
2158db2a4165SFrank Mandarino /* ASoC platform driver */
2159db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2160db2a4165SFrank Mandarino 	.driver		= {
2161db2a4165SFrank Mandarino 		.name		= "soc-audio",
21626f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2163db2a4165SFrank Mandarino 	},
2164db2a4165SFrank Mandarino 	.probe		= soc_probe,
2165db2a4165SFrank Mandarino };
2166db2a4165SFrank Mandarino 
2167096e49d5SMark Brown /**
2168db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2169db2a4165SFrank Mandarino  * @_template: control template
2170db2a4165SFrank Mandarino  * @data: control private data
2171ac11a2b3SMark Brown  * @long_name: control long name
2172efb7ac3fSMark Brown  * @prefix: control name prefix
2173db2a4165SFrank Mandarino  *
2174db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2175db2a4165SFrank Mandarino  *
2176db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2177db2a4165SFrank Mandarino  */
2178db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
21793056557fSMark Brown 				  void *data, const char *long_name,
2180efb7ac3fSMark Brown 				  const char *prefix)
2181db2a4165SFrank Mandarino {
2182db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2183efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2184efb7ac3fSMark Brown 	char *name = NULL;
2185db2a4165SFrank Mandarino 
2186db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2187db2a4165SFrank Mandarino 	template.index = 0;
2188db2a4165SFrank Mandarino 
2189efb7ac3fSMark Brown 	if (!long_name)
2190efb7ac3fSMark Brown 		long_name = template.name;
2191efb7ac3fSMark Brown 
2192efb7ac3fSMark Brown 	if (prefix) {
21932b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2194efb7ac3fSMark Brown 		if (!name)
2195efb7ac3fSMark Brown 			return NULL;
2196efb7ac3fSMark Brown 
2197efb7ac3fSMark Brown 		template.name = name;
2198efb7ac3fSMark Brown 	} else {
2199efb7ac3fSMark Brown 		template.name = long_name;
2200efb7ac3fSMark Brown 	}
2201efb7ac3fSMark Brown 
2202efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2203efb7ac3fSMark Brown 
2204efb7ac3fSMark Brown 	kfree(name);
2205efb7ac3fSMark Brown 
2206efb7ac3fSMark Brown 	return kcontrol;
2207db2a4165SFrank Mandarino }
2208db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2209db2a4165SFrank Mandarino 
2210022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2211022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2212022658beSLiam Girdwood 	const char *prefix, void *data)
2213022658beSLiam Girdwood {
2214eaf2469cSKuninori Morimoto 	int i;
2215022658beSLiam Girdwood 
2216022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2217022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2218eaf2469cSKuninori Morimoto 		int err = snd_ctl_add(card, snd_soc_cnew(control, data,
2219022658beSLiam Girdwood 							 control->name, prefix));
2220022658beSLiam Girdwood 		if (err < 0) {
2221f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2222f110bfc7SLiam Girdwood 				control->name, err);
2223022658beSLiam Girdwood 			return err;
2224022658beSLiam Girdwood 		}
2225022658beSLiam Girdwood 	}
2226022658beSLiam Girdwood 
2227022658beSLiam Girdwood 	return 0;
2228022658beSLiam Girdwood }
2229022658beSLiam Girdwood 
2230db2a4165SFrank Mandarino /**
22310f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
22320f2780adSLars-Peter Clausen  *
22330f2780adSLars-Peter Clausen  * @component: Component to add controls to
22340f2780adSLars-Peter Clausen  * @controls: Array of controls to add
22350f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
22360f2780adSLars-Peter Clausen  *
22370f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
22380f2780adSLars-Peter Clausen  */
22390f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
22400f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
22410f2780adSLars-Peter Clausen {
22420f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
22430f2780adSLars-Peter Clausen 
22440f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
22450f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
22460f2780adSLars-Peter Clausen }
22470f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
22480f2780adSLars-Peter Clausen 
22490f2780adSLars-Peter Clausen /**
2250022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2251022658beSLiam Girdwood  * Convenience function to add a list of controls.
2252022658beSLiam Girdwood  *
2253022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2254022658beSLiam Girdwood  * @controls: array of controls to add
2255022658beSLiam Girdwood  * @num_controls: number of elements in the array
2256022658beSLiam Girdwood  *
2257022658beSLiam Girdwood  * Return 0 for success, else error.
2258022658beSLiam Girdwood  */
2259022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2260022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2261022658beSLiam Girdwood {
2262022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2263022658beSLiam Girdwood 
2264022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2265022658beSLiam Girdwood 			NULL, soc_card);
2266022658beSLiam Girdwood }
2267022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2268022658beSLiam Girdwood 
2269022658beSLiam Girdwood /**
2270022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2271022658beSLiam Girdwood  * Convienience function to add a list of controls.
2272022658beSLiam Girdwood  *
2273022658beSLiam Girdwood  * @dai: DAI to add controls to
2274022658beSLiam Girdwood  * @controls: array of controls to add
2275022658beSLiam Girdwood  * @num_controls: number of elements in the array
2276022658beSLiam Girdwood  *
2277022658beSLiam Girdwood  * Return 0 for success, else error.
2278022658beSLiam Girdwood  */
2279022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2280022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2281022658beSLiam Girdwood {
2282313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2283022658beSLiam Girdwood 
2284022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2285022658beSLiam Girdwood 			NULL, dai);
2286022658beSLiam Girdwood }
2287022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2288022658beSLiam Girdwood 
2289c5af3a2eSMark Brown /**
2290c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2291c5af3a2eSMark Brown  *
2292ac11a2b3SMark Brown  * @card: Card to register
2293c5af3a2eSMark Brown  *
2294c5af3a2eSMark Brown  */
229570a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2296c5af3a2eSMark Brown {
2297c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2298c5af3a2eSMark Brown 		return -EINVAL;
2299c5af3a2eSMark Brown 
2300ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2301ed77cc12SMark Brown 
2302b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->widgets);
2303b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->paths);
2304b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->dapm_list);
2305b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->aux_comp_list);
2306b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->component_dev_list);
2307b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->list);
23081a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
2309db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
23108a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2311b03bfaecSKuninori Morimoto 
2312c5af3a2eSMark Brown 	card->instantiated = 0;
2313f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
2314a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
231572b745e3SPeter Ujfalusi 	mutex_init(&card->pcm_mutex);
2316c5af3a2eSMark Brown 
2317e894efefSSrinivas Kandagatla 	return snd_soc_bind_card(card);
2318c5af3a2eSMark Brown }
231970a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2320c5af3a2eSMark Brown 
2321c5af3a2eSMark Brown /**
2322c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2323c5af3a2eSMark Brown  *
2324ac11a2b3SMark Brown  * @card: Card to unregister
2325c5af3a2eSMark Brown  *
2326c5af3a2eSMark Brown  */
23271892a991SUwe Kleine-König void snd_soc_unregister_card(struct snd_soc_card *card)
2328c5af3a2eSMark Brown {
2329b545542aSKuninori Morimoto 	mutex_lock(&client_mutex);
2330e894efefSSrinivas Kandagatla 	snd_soc_unbind_card(card, true);
2331b545542aSKuninori Morimoto 	mutex_unlock(&client_mutex);
2332f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2333c5af3a2eSMark Brown }
233470a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2335c5af3a2eSMark Brown 
2336f0fba2adSLiam Girdwood /*
2337f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
2338f0fba2adSLiam Girdwood  * and sanitizing names.
2339f0fba2adSLiam Girdwood  */
23400b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2341f0fba2adSLiam Girdwood {
234245dd9943SDmitry Baryshkov 	const char *devname = dev_name(dev);
234345dd9943SDmitry Baryshkov 	char *found, *name;
2344d3b47a88SPierre-Louis Bossart 	unsigned int id1, id2;
2345f0fba2adSLiam Girdwood 
234645dd9943SDmitry Baryshkov 	if (devname == NULL)
2347f0fba2adSLiam Girdwood 		return NULL;
2348f0fba2adSLiam Girdwood 
234945dd9943SDmitry Baryshkov 	name = devm_kstrdup(dev, devname, GFP_KERNEL);
235041daf6baSKefeng Wang 	if (!name)
235141daf6baSKefeng Wang 		return NULL;
2352f0fba2adSLiam Girdwood 
2353f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
2354f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
2355f0fba2adSLiam Girdwood 	if (found) {
2356f0fba2adSLiam Girdwood 		/* get ID */
2357f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2358f0fba2adSLiam Girdwood 
2359f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
2360f0fba2adSLiam Girdwood 			if (*id == -1)
2361f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
2362f0fba2adSLiam Girdwood 		}
2363f0fba2adSLiam Girdwood 
2364f0fba2adSLiam Girdwood 	/* I2C component devices are named "bus-addr" */
236545dd9943SDmitry Baryshkov 	} else if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2366f0fba2adSLiam Girdwood 
2367f0fba2adSLiam Girdwood 		/* create unique ID number from I2C addr and bus */
236805899446SJarkko Nikula 		*id = ((id1 & 0xffff) << 16) + id2;
2369f0fba2adSLiam Girdwood 
237045dd9943SDmitry Baryshkov 		devm_kfree(dev, name);
237145dd9943SDmitry Baryshkov 
2372f0fba2adSLiam Girdwood 		/* sanitize component name for DAI link creation */
237345dd9943SDmitry Baryshkov 		name = devm_kasprintf(dev, GFP_KERNEL, "%s.%s", dev->driver->name, devname);
237445dd9943SDmitry Baryshkov 	} else {
2375f0fba2adSLiam Girdwood 		*id = 0;
2376f0fba2adSLiam Girdwood 	}
2377f0fba2adSLiam Girdwood 
237845dd9943SDmitry Baryshkov 	return name;
2379f0fba2adSLiam Girdwood }
2380f0fba2adSLiam Girdwood 
2381f0fba2adSLiam Girdwood /*
2382f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
2383f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
2384f0fba2adSLiam Girdwood  */
2385f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
2386f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
2387f0fba2adSLiam Girdwood {
2388f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
238910e8aa9aSMichał Mirosław 		dev_err(dev,
239010e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
239110e8aa9aSMichał Mirosław 			dev_name(dev));
2392f0fba2adSLiam Girdwood 		return NULL;
2393f0fba2adSLiam Girdwood 	}
2394f0fba2adSLiam Girdwood 
239550014499SKuninori Morimoto 	return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2396f0fba2adSLiam Girdwood }
2397f0fba2adSLiam Girdwood 
2398ffdbca0bSKuninori Morimoto void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2399e11381f3SKuninori Morimoto {
2400e11381f3SKuninori Morimoto 	dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2401e11381f3SKuninori Morimoto 	list_del(&dai->list);
2402e11381f3SKuninori Morimoto }
2403ffdbca0bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2404e11381f3SKuninori Morimoto 
24057ca24386SKuninori Morimoto /**
24067ca24386SKuninori Morimoto  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
24077ca24386SKuninori Morimoto  *
24087ca24386SKuninori Morimoto  * @component: The component the DAIs are registered for
24097ca24386SKuninori Morimoto  * @dai_drv: DAI driver to use for the DAI
2410bc9a6655SRandy Dunlap  * @legacy_dai_naming: if %true, use legacy single-name format;
2411bc9a6655SRandy Dunlap  * 	if %false, use multiple-name format;
24127ca24386SKuninori Morimoto  *
24137ca24386SKuninori Morimoto  * Topology can use this API to register DAIs when probing a component.
24147ca24386SKuninori Morimoto  * These DAIs's widgets will be freed in the card cleanup and the DAIs
24157ca24386SKuninori Morimoto  * will be freed in the component cleanup.
24167ca24386SKuninori Morimoto  */
24177ca24386SKuninori Morimoto struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
24185e4fb372SMengdong Lin 					 struct snd_soc_dai_driver *dai_drv,
24195e4fb372SMengdong Lin 					 bool legacy_dai_naming)
24205e4fb372SMengdong Lin {
24215e4fb372SMengdong Lin 	struct device *dev = component->dev;
24225e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
24235e4fb372SMengdong Lin 
24245e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
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 
3201a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
3202a180e8b9SKuninori Morimoto {
320309d4cc03SKuninori Morimoto 	struct snd_soc_component *component;
3204c1e230f0SKuninori Morimoto 	struct snd_soc_dai_link_component dlc;
3205a180e8b9SKuninori Morimoto 	int ret;
3206a180e8b9SKuninori Morimoto 
3207c1e230f0SKuninori Morimoto 	dlc.of_node	= of_graph_get_port_parent(ep);
3208c1e230f0SKuninori Morimoto 	dlc.name	= NULL;
3209a180e8b9SKuninori Morimoto 	/*
3210a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
3211a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
3212a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
3213a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
3214a180e8b9SKuninori Morimoto 	 */
3215a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
3216a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
3217c1e230f0SKuninori Morimoto 	component = soc_find_component(&dlc);
32182c7b1704SKuninori Morimoto 	if (component)
32192c7b1704SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_id(component, ep);
3220a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
3221a180e8b9SKuninori Morimoto 
3222c1e230f0SKuninori Morimoto 	of_node_put(dlc.of_node);
3223c0a480d1STony Lindgren 
3224a180e8b9SKuninori Morimoto 	return ret;
3225a180e8b9SKuninori Morimoto }
3226a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3227a180e8b9SKuninori Morimoto 
3228933f98beSKrzysztof Kozlowski int snd_soc_get_dai_name(const struct of_phandle_args *args,
3229cb470087SKuninori Morimoto 				const char **dai_name)
3230cb470087SKuninori Morimoto {
3231cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
323293b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3233cb470087SKuninori Morimoto 
3234cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3235368dee94SKuninori Morimoto 	for_each_component(pos) {
3236bce00560SKuninori Morimoto 		struct device_node *component_of_node = soc_component_to_node(pos);
32373e0aa8d8SJyri Sarha 
3238f7d344a2SShengjiu Wang 		if (component_of_node != args->np || !pos->num_dai)
3239cb470087SKuninori Morimoto 			continue;
3240cb470087SKuninori Morimoto 
3241a2a34175SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3242a2a34175SKuninori Morimoto 		if (ret == -ENOTSUPP) {
324358bf4179SKuninori Morimoto 			struct snd_soc_dai *dai;
32446833c452SKuninori Morimoto 			int id = -1;
32456833c452SKuninori Morimoto 
324693b0f3eeSJean-Francois Moine 			switch (args->args_count) {
32476833c452SKuninori Morimoto 			case 0:
32486833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
32496833c452SKuninori Morimoto 				break;
32506833c452SKuninori Morimoto 			case 1:
325193b0f3eeSJean-Francois Moine 				id = args->args[0];
32526833c452SKuninori Morimoto 				break;
32536833c452SKuninori Morimoto 			default:
32546833c452SKuninori Morimoto 				/* not supported */
3255cb470087SKuninori Morimoto 				break;
3256cb470087SKuninori Morimoto 			}
3257cb470087SKuninori Morimoto 
32586833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
32596833c452SKuninori Morimoto 				ret = -EINVAL;
32603dcba280SNicolin Chen 				continue;
32616833c452SKuninori Morimoto 			}
3262e41975edSXiubo Li 
3263e41975edSXiubo Li 			ret = 0;
3264e41975edSXiubo Li 
326558bf4179SKuninori Morimoto 			/* find target DAI */
326615a0c645SKuninori Morimoto 			for_each_component_dais(pos, dai) {
326758bf4179SKuninori Morimoto 				if (id == 0)
326858bf4179SKuninori Morimoto 					break;
326958bf4179SKuninori Morimoto 				id--;
327058bf4179SKuninori Morimoto 			}
327158bf4179SKuninori Morimoto 
327258bf4179SKuninori Morimoto 			*dai_name = dai->driver->name;
3273e41975edSXiubo Li 			if (!*dai_name)
3274e41975edSXiubo Li 				*dai_name = pos->name;
32751dfa5a5aSJerome Brunet 		} else if (ret) {
32761dfa5a5aSJerome Brunet 			/*
32771dfa5a5aSJerome Brunet 			 * if another error than ENOTSUPP is returned go on and
32781dfa5a5aSJerome Brunet 			 * check if another component is provided with the same
32791dfa5a5aSJerome Brunet 			 * node. This may happen if a device provides several
32801dfa5a5aSJerome Brunet 			 * components
32811dfa5a5aSJerome Brunet 			 */
32821dfa5a5aSJerome Brunet 			continue;
32836833c452SKuninori Morimoto 		}
32846833c452SKuninori Morimoto 
3285cb470087SKuninori Morimoto 		break;
3286cb470087SKuninori Morimoto 	}
3287cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
328893b0f3eeSJean-Francois Moine 	return ret;
328993b0f3eeSJean-Francois Moine }
32901ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
329193b0f3eeSJean-Francois Moine 
329293b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
329393b0f3eeSJean-Francois Moine 			    const char **dai_name)
329493b0f3eeSJean-Francois Moine {
329593b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
329693b0f3eeSJean-Francois Moine 	int ret;
329793b0f3eeSJean-Francois Moine 
329893b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
329993b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
330093b0f3eeSJean-Francois Moine 	if (ret)
330193b0f3eeSJean-Francois Moine 		return ret;
330293b0f3eeSJean-Francois Moine 
330393b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3304cb470087SKuninori Morimoto 
3305cb470087SKuninori Morimoto 	of_node_put(args.np);
3306cb470087SKuninori Morimoto 
3307cb470087SKuninori Morimoto 	return ret;
3308cb470087SKuninori Morimoto }
3309cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3310cb470087SKuninori Morimoto 
33119cc69528SKuninori Morimoto static void __snd_soc_of_put_component(struct snd_soc_dai_link_component *component)
33129cc69528SKuninori Morimoto {
33139cc69528SKuninori Morimoto 	if (component->of_node) {
33149cc69528SKuninori Morimoto 		of_node_put(component->of_node);
33159cc69528SKuninori Morimoto 		component->of_node = NULL;
33169cc69528SKuninori Morimoto 	}
33179cc69528SKuninori Morimoto }
33189cc69528SKuninori Morimoto 
33199cc69528SKuninori Morimoto static int __snd_soc_of_get_dai_link_component_alloc(
33209cc69528SKuninori Morimoto 	struct device *dev, struct device_node *of_node,
33219cc69528SKuninori Morimoto 	struct snd_soc_dai_link_component **ret_component,
33229cc69528SKuninori Morimoto 	int *ret_num)
33239cc69528SKuninori Morimoto {
33249cc69528SKuninori Morimoto 	struct snd_soc_dai_link_component *component;
33259cc69528SKuninori Morimoto 	int num;
33269cc69528SKuninori Morimoto 
33279cc69528SKuninori Morimoto 	/* Count the number of CPUs/CODECs */
33289cc69528SKuninori Morimoto 	num = of_count_phandle_with_args(of_node, "sound-dai", "#sound-dai-cells");
33299cc69528SKuninori Morimoto 	if (num <= 0) {
33309cc69528SKuninori Morimoto 		if (num == -ENOENT)
33319cc69528SKuninori Morimoto 			dev_err(dev, "No 'sound-dai' property\n");
33329cc69528SKuninori Morimoto 		else
33339cc69528SKuninori Morimoto 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
33349cc69528SKuninori Morimoto 		return num;
33359cc69528SKuninori Morimoto 	}
33369cc69528SKuninori Morimoto 	component = devm_kcalloc(dev, num, sizeof(*component), GFP_KERNEL);
33379cc69528SKuninori Morimoto 	if (!component)
33389cc69528SKuninori Morimoto 		return -ENOMEM;
33399cc69528SKuninori Morimoto 
33409cc69528SKuninori Morimoto 	*ret_component	= component;
33419cc69528SKuninori Morimoto 	*ret_num	= num;
33429cc69528SKuninori Morimoto 
33439cc69528SKuninori Morimoto 	return 0;
33449cc69528SKuninori Morimoto }
33459cc69528SKuninori Morimoto 
33469cc69528SKuninori Morimoto static int __snd_soc_of_get_dai_link_component_parse(
33479cc69528SKuninori Morimoto 	struct device_node *of_node,
33489cc69528SKuninori Morimoto 	struct snd_soc_dai_link_component *component, int index)
33499cc69528SKuninori Morimoto {
33509cc69528SKuninori Morimoto 	struct of_phandle_args args;
33519cc69528SKuninori Morimoto 	int ret;
33529cc69528SKuninori Morimoto 
33539cc69528SKuninori Morimoto 	ret = of_parse_phandle_with_args(of_node, "sound-dai", "#sound-dai-cells",
33549cc69528SKuninori Morimoto 					 index, &args);
33559cc69528SKuninori Morimoto 	if (ret)
33569cc69528SKuninori Morimoto 		return ret;
33579cc69528SKuninori Morimoto 
33589cc69528SKuninori Morimoto 	ret = snd_soc_get_dai_name(&args, &component->dai_name);
33599cc69528SKuninori Morimoto 	if (ret < 0)
33609cc69528SKuninori Morimoto 		return ret;
33619cc69528SKuninori Morimoto 
33629cc69528SKuninori Morimoto 	component->of_node = args.np;
33639cc69528SKuninori Morimoto 	return 0;
33649cc69528SKuninori Morimoto }
33659cc69528SKuninori Morimoto 
336693b0f3eeSJean-Francois Moine /*
336794685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
336894685763SSylwester Nawrocki  * @dai_link: DAI link
336994685763SSylwester Nawrocki  *
337094685763SSylwester Nawrocki  * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
337194685763SSylwester Nawrocki  */
337294685763SSylwester Nawrocki void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
337394685763SSylwester Nawrocki {
33743db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *component;
337594685763SSylwester Nawrocki 	int index;
337694685763SSylwester Nawrocki 
33779cc69528SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component)
33789cc69528SKuninori Morimoto 		__snd_soc_of_put_component(component);
337994685763SSylwester Nawrocki }
338094685763SSylwester Nawrocki EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
338194685763SSylwester Nawrocki 
338294685763SSylwester Nawrocki /*
338393b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
338493b0f3eeSJean-Francois Moine  * @dev: Card device
338593b0f3eeSJean-Francois Moine  * @of_node: Device node
338693b0f3eeSJean-Francois Moine  * @dai_link: DAI link
338793b0f3eeSJean-Francois Moine  *
338893b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
338993b0f3eeSJean-Francois Moine  * 'sound-dai'.
339093b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
339194685763SSylwester Nawrocki  * The device nodes in the array (of_node) must be dereferenced by calling
339294685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs() on @dai_link.
339393b0f3eeSJean-Francois Moine  *
339493b0f3eeSJean-Francois Moine  * Returns 0 for success
339593b0f3eeSJean-Francois Moine  */
339693b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
339793b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
339893b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
339993b0f3eeSJean-Francois Moine {
340093b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
34019cc69528SKuninori Morimoto 	int index, ret;
340293b0f3eeSJean-Francois Moine 
34039cc69528SKuninori Morimoto 	ret = __snd_soc_of_get_dai_link_component_alloc(dev, of_node,
34049cc69528SKuninori Morimoto 					 &dai_link->codecs, &dai_link->num_codecs);
34059cc69528SKuninori Morimoto 	if (ret < 0)
34069cc69528SKuninori Morimoto 		return ret;
340793b0f3eeSJean-Francois Moine 
340893b0f3eeSJean-Francois Moine 	/* Parse the list */
34093db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
34109cc69528SKuninori Morimoto 		ret = __snd_soc_of_get_dai_link_component_parse(of_node, component, index);
341193b0f3eeSJean-Francois Moine 		if (ret)
341293b0f3eeSJean-Francois Moine 			goto err;
341393b0f3eeSJean-Francois Moine 	}
341493b0f3eeSJean-Francois Moine 	return 0;
341593b0f3eeSJean-Francois Moine err:
341694685763SSylwester Nawrocki 	snd_soc_of_put_dai_link_codecs(dai_link);
341793b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
341893b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
341993b0f3eeSJean-Francois Moine 	return ret;
342093b0f3eeSJean-Francois Moine }
342193b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
342293b0f3eeSJean-Francois Moine 
3423900dedd7SMartin Povišer /*
3424900dedd7SMartin Povišer  * snd_soc_of_put_dai_link_cpus - Dereference device nodes in the codecs array
3425900dedd7SMartin Povišer  * @dai_link: DAI link
3426900dedd7SMartin Povišer  *
3427900dedd7SMartin Povišer  * Dereference device nodes acquired by snd_soc_of_get_dai_link_cpus().
3428900dedd7SMartin Povišer  */
3429900dedd7SMartin Povišer void snd_soc_of_put_dai_link_cpus(struct snd_soc_dai_link *dai_link)
3430900dedd7SMartin Povišer {
3431900dedd7SMartin Povišer 	struct snd_soc_dai_link_component *component;
3432900dedd7SMartin Povišer 	int index;
3433900dedd7SMartin Povišer 
34349cc69528SKuninori Morimoto 	for_each_link_cpus(dai_link, index, component)
34359cc69528SKuninori Morimoto 		__snd_soc_of_put_component(component);
3436900dedd7SMartin Povišer }
3437900dedd7SMartin Povišer EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_cpus);
3438900dedd7SMartin Povišer 
3439900dedd7SMartin Povišer /*
3440900dedd7SMartin Povišer  * snd_soc_of_get_dai_link_cpus - Parse a list of CPU DAIs in the devicetree
3441900dedd7SMartin Povišer  * @dev: Card device
3442900dedd7SMartin Povišer  * @of_node: Device node
3443900dedd7SMartin Povišer  * @dai_link: DAI link
3444900dedd7SMartin Povišer  *
3445900dedd7SMartin Povišer  * Is analogous to snd_soc_of_get_dai_link_codecs but parses a list of CPU DAIs
3446900dedd7SMartin Povišer  * instead.
3447900dedd7SMartin Povišer  *
3448900dedd7SMartin Povišer  * Returns 0 for success
3449900dedd7SMartin Povišer  */
3450900dedd7SMartin Povišer int snd_soc_of_get_dai_link_cpus(struct device *dev,
3451900dedd7SMartin Povišer 				 struct device_node *of_node,
3452900dedd7SMartin Povišer 				 struct snd_soc_dai_link *dai_link)
3453900dedd7SMartin Povišer {
3454900dedd7SMartin Povišer 	struct snd_soc_dai_link_component *component;
34559cc69528SKuninori Morimoto 	int index, ret;
3456900dedd7SMartin Povišer 
3457586fb264SKuninori Morimoto 	/* Count the number of CPUs */
34589cc69528SKuninori Morimoto 	ret = __snd_soc_of_get_dai_link_component_alloc(dev, of_node,
34599cc69528SKuninori Morimoto 					 &dai_link->cpus, &dai_link->num_cpus);
34609cc69528SKuninori Morimoto 	if (ret < 0)
34619cc69528SKuninori Morimoto 		return ret;
3462900dedd7SMartin Povišer 
3463900dedd7SMartin Povišer 	/* Parse the list */
3464900dedd7SMartin Povišer 	for_each_link_cpus(dai_link, index, component) {
34659cc69528SKuninori Morimoto 		ret = __snd_soc_of_get_dai_link_component_parse(of_node, component, index);
3466900dedd7SMartin Povišer 		if (ret)
3467900dedd7SMartin Povišer 			goto err;
3468900dedd7SMartin Povišer 	}
3469900dedd7SMartin Povišer 	return 0;
3470900dedd7SMartin Povišer err:
3471586fb264SKuninori Morimoto 	snd_soc_of_put_dai_link_cpus(dai_link);
3472900dedd7SMartin Povišer 	dai_link->cpus = NULL;
3473900dedd7SMartin Povišer 	dai_link->num_cpus = 0;
3474900dedd7SMartin Povišer 	return ret;
3475900dedd7SMartin Povišer }
3476900dedd7SMartin Povišer EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_cpus);
3477900dedd7SMartin Povišer 
3478c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3479db2a4165SFrank Mandarino {
3480*6ec27c53SChen Zhongjin 	int ret;
3481fb257897SMark Brown 
3482*6ec27c53SChen Zhongjin 	snd_soc_debugfs_init();
3483*6ec27c53SChen Zhongjin 	ret = snd_soc_util_init();
3484*6ec27c53SChen Zhongjin 	if (ret)
3485*6ec27c53SChen Zhongjin 		goto err_util_init;
3486*6ec27c53SChen Zhongjin 
3487*6ec27c53SChen Zhongjin 	ret = platform_driver_register(&soc_driver);
3488*6ec27c53SChen Zhongjin 	if (ret)
3489*6ec27c53SChen Zhongjin 		goto err_register;
3490*6ec27c53SChen Zhongjin 	return 0;
3491*6ec27c53SChen Zhongjin 
3492*6ec27c53SChen Zhongjin err_register:
3493*6ec27c53SChen Zhongjin 	snd_soc_util_exit();
3494*6ec27c53SChen Zhongjin err_util_init:
3495*6ec27c53SChen Zhongjin 	snd_soc_debugfs_exit();
3496*6ec27c53SChen Zhongjin 	return ret;
3497db2a4165SFrank Mandarino }
34984abe8e16SMark Brown module_init(snd_soc_init);
3499db2a4165SFrank Mandarino 
35007d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3501db2a4165SFrank Mandarino {
3502fb257897SMark Brown 	snd_soc_util_exit();
35036553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3504fb257897SMark Brown 
3505db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3506db2a4165SFrank Mandarino }
3507db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3508db2a4165SFrank Mandarino 
3509db2a4165SFrank Mandarino /* Module information */
3510d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3511db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3512db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
35138b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3514