xref: /openbmc/linux/sound/soc/soc-core.c (revision a22ae72b86a4f754e8d25fbf9ea5a8f77365e531)
1873486edSKuninori Morimoto // SPDX-License-Identifier: GPL-2.0+
2873486edSKuninori Morimoto //
3873486edSKuninori Morimoto // soc-core.c  --  ALSA SoC Audio Layer
4873486edSKuninori Morimoto //
5873486edSKuninori Morimoto // Copyright 2005 Wolfson Microelectronics PLC.
6873486edSKuninori Morimoto // Copyright 2005 Openedhand Ltd.
7873486edSKuninori Morimoto // Copyright (C) 2010 Slimlogic Ltd.
8873486edSKuninori Morimoto // Copyright (C) 2010 Texas Instruments Inc.
9873486edSKuninori Morimoto //
10873486edSKuninori Morimoto // Author: Liam Girdwood <lrg@slimlogic.co.uk>
11873486edSKuninori Morimoto //         with code, comments and ideas from :-
12873486edSKuninori Morimoto //         Richard Purdie <richard@openedhand.com>
13873486edSKuninori Morimoto //
14873486edSKuninori Morimoto //  TODO:
15873486edSKuninori Morimoto //   o Add hw rules to enforce rates, etc.
16873486edSKuninori Morimoto //   o More testing with other codecs/machines.
17873486edSKuninori Morimoto //   o Add more codecs and platforms to ensure good API coverage.
18873486edSKuninori Morimoto //   o Support TDM on PCM and I2S
19db2a4165SFrank Mandarino 
20db2a4165SFrank Mandarino #include <linux/module.h>
21db2a4165SFrank Mandarino #include <linux/moduleparam.h>
22db2a4165SFrank Mandarino #include <linux/init.h>
23db2a4165SFrank Mandarino #include <linux/delay.h>
24db2a4165SFrank Mandarino #include <linux/pm.h>
25db2a4165SFrank Mandarino #include <linux/bitops.h>
2612ef193dSTroy Kisky #include <linux/debugfs.h>
27db2a4165SFrank Mandarino #include <linux/platform_device.h>
28741a509fSMarkus Pargmann #include <linux/pinctrl/consumer.h>
29f0e8ed85SMark Brown #include <linux/ctype.h>
305a0e3ad6STejun Heo #include <linux/slab.h>
31bec4fa05SStephen Warren #include <linux/of.h>
32a180e8b9SKuninori Morimoto #include <linux/of_graph.h>
33345233d7SLiam Girdwood #include <linux/dmi.h>
34db2a4165SFrank Mandarino #include <sound/core.h>
353028eb8cSMark Brown #include <sound/jack.h>
36db2a4165SFrank Mandarino #include <sound/pcm.h>
37db2a4165SFrank Mandarino #include <sound/pcm_params.h>
38db2a4165SFrank Mandarino #include <sound/soc.h>
3901d7584cSLiam Girdwood #include <sound/soc-dpcm.h>
408a978234SLiam Girdwood #include <sound/soc-topology.h>
41db2a4165SFrank Mandarino #include <sound/initval.h>
42db2a4165SFrank Mandarino 
43a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
44a8b1d34fSMark Brown #include <trace/events/asoc.h>
45a8b1d34fSMark Brown 
46f0fba2adSLiam Girdwood #define NAME_SIZE	32
47f0fba2adSLiam Girdwood 
48c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
49030e79f6SKuninori Morimoto static LIST_HEAD(component_list);
50e894efefSSrinivas Kandagatla static LIST_HEAD(unbind_card_list);
51c5af3a2eSMark Brown 
52368dee94SKuninori Morimoto #define for_each_component(component)			\
53368dee94SKuninori Morimoto 	list_for_each_entry(component, &component_list, list)
54368dee94SKuninori Morimoto 
55db2a4165SFrank Mandarino /*
56587c9844SKuninori Morimoto  * This is used if driver don't need to have CPU/Codec/Platform
57587c9844SKuninori Morimoto  * dai_link. see soc.h
58587c9844SKuninori Morimoto  */
59587c9844SKuninori Morimoto struct snd_soc_dai_link_component null_dailink_component[0];
60587c9844SKuninori Morimoto EXPORT_SYMBOL_GPL(null_dailink_component);
61587c9844SKuninori Morimoto 
62587c9844SKuninori Morimoto /*
63db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
64db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
65db2a4165SFrank Mandarino  * between two audio tracks.
66db2a4165SFrank Mandarino  */
67db2a4165SFrank Mandarino static int pmdown_time = 5000;
68db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
69db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
70db2a4165SFrank Mandarino 
71dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
72dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
73dbe21408SMark Brown {
7436ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
75dbe21408SMark Brown 
76f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
77dbe21408SMark Brown }
78dbe21408SMark Brown 
79dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
80dbe21408SMark Brown 			       struct device_attribute *attr,
81dbe21408SMark Brown 			       const char *buf, size_t count)
82dbe21408SMark Brown {
8336ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
84c593b520SMark Brown 	int ret;
85dbe21408SMark Brown 
86b785a492SJingoo Han 	ret = kstrtol(buf, 10, &rtd->pmdown_time);
87c593b520SMark Brown 	if (ret)
88c593b520SMark Brown 		return ret;
89dbe21408SMark Brown 
90dbe21408SMark Brown 	return count;
91dbe21408SMark Brown }
92dbe21408SMark Brown 
93dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
94dbe21408SMark Brown 
95d29697dcSTakashi Iwai static struct attribute *soc_dev_attrs[] = {
96d29697dcSTakashi Iwai 	&dev_attr_pmdown_time.attr,
97d29697dcSTakashi Iwai 	NULL
98d29697dcSTakashi Iwai };
99d29697dcSTakashi Iwai 
100d29697dcSTakashi Iwai static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
101d29697dcSTakashi Iwai 				       struct attribute *attr, int idx)
102d29697dcSTakashi Iwai {
103d29697dcSTakashi Iwai 	struct device *dev = kobj_to_dev(kobj);
104d29697dcSTakashi Iwai 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
105d29697dcSTakashi Iwai 
106d918a376SKuninori Morimoto 	if (!rtd)
107d918a376SKuninori Morimoto 		return 0;
108d918a376SKuninori Morimoto 
109d29697dcSTakashi Iwai 	if (attr == &dev_attr_pmdown_time.attr)
110d29697dcSTakashi Iwai 		return attr->mode; /* always visible */
1113b6eed8dSKuninori Morimoto 	return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
112d29697dcSTakashi Iwai }
113d29697dcSTakashi Iwai 
114d29697dcSTakashi Iwai static const struct attribute_group soc_dapm_dev_group = {
115d29697dcSTakashi Iwai 	.attrs = soc_dapm_dev_attrs,
116d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
117d29697dcSTakashi Iwai };
118d29697dcSTakashi Iwai 
119f7e73b26SMark Brown static const struct attribute_group soc_dev_group = {
120d29697dcSTakashi Iwai 	.attrs = soc_dev_attrs,
121d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
122d29697dcSTakashi Iwai };
123d29697dcSTakashi Iwai 
124d29697dcSTakashi Iwai static const struct attribute_group *soc_dev_attr_groups[] = {
125d29697dcSTakashi Iwai 	&soc_dapm_dev_group,
126f7e73b26SMark Brown 	&soc_dev_group,
127d29697dcSTakashi Iwai 	NULL
128d29697dcSTakashi Iwai };
129d29697dcSTakashi Iwai 
1302624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
131a4072cdfSKuninori Morimoto struct dentry *snd_soc_debugfs_root;
132a4072cdfSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
133a4072cdfSKuninori Morimoto 
13481c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component)
135e73f3de5SRussell King {
1366553bf06SLars-Peter Clausen 	if (!component->card->debugfs_card_root)
1376553bf06SLars-Peter Clausen 		return;
1386553bf06SLars-Peter Clausen 
13981c7cfd1SLars-Peter Clausen 	if (component->debugfs_prefix) {
14081c7cfd1SLars-Peter Clausen 		char *name;
141e73f3de5SRussell King 
14281c7cfd1SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s:%s",
14381c7cfd1SLars-Peter Clausen 			component->debugfs_prefix, component->name);
14481c7cfd1SLars-Peter Clausen 		if (name) {
14581c7cfd1SLars-Peter Clausen 			component->debugfs_root = debugfs_create_dir(name,
14681c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
14781c7cfd1SLars-Peter Clausen 			kfree(name);
14881c7cfd1SLars-Peter Clausen 		}
14981c7cfd1SLars-Peter Clausen 	} else {
15081c7cfd1SLars-Peter Clausen 		component->debugfs_root = debugfs_create_dir(component->name,
15181c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
152e73f3de5SRussell King 	}
153e73f3de5SRussell King 
15481c7cfd1SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
15581c7cfd1SLars-Peter Clausen 		component->debugfs_root);
15681c7cfd1SLars-Peter Clausen }
15781c7cfd1SLars-Peter Clausen 
15881c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
15981c7cfd1SLars-Peter Clausen {
160ad64bfbdSKuninori Morimoto 	if (!component->debugfs_root)
161ad64bfbdSKuninori Morimoto 		return;
16281c7cfd1SLars-Peter Clausen 	debugfs_remove_recursive(component->debugfs_root);
163ad64bfbdSKuninori Morimoto 	component->debugfs_root = NULL;
16481c7cfd1SLars-Peter Clausen }
16581c7cfd1SLars-Peter Clausen 
166c15b2a1dSPeng Donglin static int dai_list_show(struct seq_file *m, void *v)
167f3208780SMark Brown {
1681438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
169f3208780SMark Brown 	struct snd_soc_dai *dai;
170f3208780SMark Brown 
17134e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
17234e81ab4SLars-Peter Clausen 
173368dee94SKuninori Morimoto 	for_each_component(component)
17415a0c645SKuninori Morimoto 		for_each_component_dais(component, dai)
175700c17caSDonglin Peng 			seq_printf(m, "%s\n", dai->name);
176f3208780SMark Brown 
17734e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
17834e81ab4SLars-Peter Clausen 
179700c17caSDonglin Peng 	return 0;
180700c17caSDonglin Peng }
181c15b2a1dSPeng Donglin DEFINE_SHOW_ATTRIBUTE(dai_list);
182f3208780SMark Brown 
183db795f9bSKuninori Morimoto static int component_list_show(struct seq_file *m, void *v)
184db795f9bSKuninori Morimoto {
185db795f9bSKuninori Morimoto 	struct snd_soc_component *component;
186db795f9bSKuninori Morimoto 
187db795f9bSKuninori Morimoto 	mutex_lock(&client_mutex);
188db795f9bSKuninori Morimoto 
189368dee94SKuninori Morimoto 	for_each_component(component)
190db795f9bSKuninori Morimoto 		seq_printf(m, "%s\n", component->name);
191db795f9bSKuninori Morimoto 
192db795f9bSKuninori Morimoto 	mutex_unlock(&client_mutex);
193db795f9bSKuninori Morimoto 
194db795f9bSKuninori Morimoto 	return 0;
195db795f9bSKuninori Morimoto }
196db795f9bSKuninori Morimoto DEFINE_SHOW_ATTRIBUTE(component_list);
197db795f9bSKuninori Morimoto 
198a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
199a6052154SJarkko Nikula {
200a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
2018a9dab1aSMark Brown 						     snd_soc_debugfs_root);
2023a45b867SJarkko Nikula 
203fee531d6SGreg Kroah-Hartman 	debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root,
2043a45b867SJarkko Nikula 			   &card->pop_time);
205d8ca7a0aSKuninori Morimoto 
206d8ca7a0aSKuninori Morimoto 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
207a6052154SJarkko Nikula }
208a6052154SJarkko Nikula 
209a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
210a6052154SJarkko Nikula {
211a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
21229040d1aSKuninori Morimoto 	card->debugfs_card_root = NULL;
213a6052154SJarkko Nikula }
214a6052154SJarkko Nikula 
2156553bf06SLars-Peter Clausen static void snd_soc_debugfs_init(void)
2166553bf06SLars-Peter Clausen {
2176553bf06SLars-Peter Clausen 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
2186553bf06SLars-Peter Clausen 
219fee531d6SGreg Kroah-Hartman 	debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
220fee531d6SGreg Kroah-Hartman 			    &dai_list_fops);
221db795f9bSKuninori Morimoto 
222fee531d6SGreg Kroah-Hartman 	debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
223fee531d6SGreg Kroah-Hartman 			    &component_list_fops);
2246553bf06SLars-Peter Clausen }
2256553bf06SLars-Peter Clausen 
2266553bf06SLars-Peter Clausen static void snd_soc_debugfs_exit(void)
2276553bf06SLars-Peter Clausen {
2286553bf06SLars-Peter Clausen 	debugfs_remove_recursive(snd_soc_debugfs_root);
2296553bf06SLars-Peter Clausen }
2306553bf06SLars-Peter Clausen 
2312624d5faSMark Brown #else
2322624d5faSMark Brown 
23381c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs(
23481c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
2352624d5faSMark Brown {
2362624d5faSMark Brown }
2372624d5faSMark Brown 
23881c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs(
23981c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
240731f1ab2SSebastien Guiriec {
241731f1ab2SSebastien Guiriec }
242731f1ab2SSebastien Guiriec 
243b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
244b95fccbcSAxel Lin {
245b95fccbcSAxel Lin }
246b95fccbcSAxel Lin 
247b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
248b95fccbcSAxel Lin {
249b95fccbcSAxel Lin }
2506553bf06SLars-Peter Clausen 
2516553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_init(void)
2526553bf06SLars-Peter Clausen {
2536553bf06SLars-Peter Clausen }
2546553bf06SLars-Peter Clausen 
2556553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_exit(void)
2566553bf06SLars-Peter Clausen {
2576553bf06SLars-Peter Clausen }
2586553bf06SLars-Peter Clausen 
2592624d5faSMark Brown #endif
2602624d5faSMark Brown 
26112b05232SKuninori Morimoto static int snd_soc_rtd_add_component(struct snd_soc_pcm_runtime *rtd,
262a0ac4411SKuninori Morimoto 				     struct snd_soc_component *component)
263a0ac4411SKuninori Morimoto {
2642b544dd7SKuninori Morimoto 	struct snd_soc_component *comp;
265613fb500SKuninori Morimoto 	int i;
266a0ac4411SKuninori Morimoto 
267613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, comp) {
268a0ac4411SKuninori Morimoto 		/* already connected */
2692b544dd7SKuninori Morimoto 		if (comp == component)
270a0ac4411SKuninori Morimoto 			return 0;
271a0ac4411SKuninori Morimoto 	}
272a0ac4411SKuninori Morimoto 
273613fb500SKuninori Morimoto 	/* see for_each_rtd_components */
274613fb500SKuninori Morimoto 	rtd->components[rtd->num_components] = component;
275613fb500SKuninori Morimoto 	rtd->num_components++;
276a0ac4411SKuninori Morimoto 
277a0ac4411SKuninori Morimoto 	return 0;
278a0ac4411SKuninori Morimoto }
279a0ac4411SKuninori Morimoto 
280a0ac4411SKuninori Morimoto struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
281a0ac4411SKuninori Morimoto 						const char *driver_name)
282a0ac4411SKuninori Morimoto {
2832b544dd7SKuninori Morimoto 	struct snd_soc_component *component;
284613fb500SKuninori Morimoto 	int i;
285a0ac4411SKuninori Morimoto 
286971da24cSKuninori Morimoto 	if (!driver_name)
287971da24cSKuninori Morimoto 		return NULL;
288971da24cSKuninori Morimoto 
289a33c0d16SKuninori Morimoto 	/*
290a33c0d16SKuninori Morimoto 	 * NOTE
291a33c0d16SKuninori Morimoto 	 *
292a33c0d16SKuninori Morimoto 	 * snd_soc_rtdcom_lookup() will find component from rtd by using
293a33c0d16SKuninori Morimoto 	 * specified driver name.
294a33c0d16SKuninori Morimoto 	 * But, if many components which have same driver name are connected
295a33c0d16SKuninori Morimoto 	 * to 1 rtd, this function will return 1st found component.
296a33c0d16SKuninori Morimoto 	 */
297613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
2982b544dd7SKuninori Morimoto 		const char *component_name = component->driver->name;
299971da24cSKuninori Morimoto 
300971da24cSKuninori Morimoto 		if (!component_name)
301971da24cSKuninori Morimoto 			continue;
302971da24cSKuninori Morimoto 
303971da24cSKuninori Morimoto 		if ((component_name == driver_name) ||
304971da24cSKuninori Morimoto 		    strcmp(component_name, driver_name) == 0)
305613fb500SKuninori Morimoto 			return component;
306a0ac4411SKuninori Morimoto 	}
307a0ac4411SKuninori Morimoto 
308a0ac4411SKuninori Morimoto 	return NULL;
309a0ac4411SKuninori Morimoto }
310031734b7SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
311a0ac4411SKuninori Morimoto 
31218dd66eaSKuninori Morimoto static struct snd_soc_component
31318dd66eaSKuninori Morimoto *snd_soc_lookup_component_nolocked(struct device *dev, const char *driver_name)
314b8132657SKuninori Morimoto {
315b8132657SKuninori Morimoto 	struct snd_soc_component *component;
3165bd7e08bSKuninori Morimoto 	struct snd_soc_component *found_component;
317b8132657SKuninori Morimoto 
3185bd7e08bSKuninori Morimoto 	found_component = NULL;
319b8132657SKuninori Morimoto 	for_each_component(component) {
3205bd7e08bSKuninori Morimoto 		if ((dev == component->dev) &&
3215bd7e08bSKuninori Morimoto 		    (!driver_name ||
3225bd7e08bSKuninori Morimoto 		     (driver_name == component->driver->name) ||
3235bd7e08bSKuninori Morimoto 		     (strcmp(component->driver->name, driver_name) == 0))) {
3245bd7e08bSKuninori Morimoto 			found_component = component;
325b8132657SKuninori Morimoto 			break;
326b8132657SKuninori Morimoto 		}
3275bd7e08bSKuninori Morimoto 	}
328b8132657SKuninori Morimoto 
3295bd7e08bSKuninori Morimoto 	return found_component;
330b8132657SKuninori Morimoto }
33118dd66eaSKuninori Morimoto 
33218dd66eaSKuninori Morimoto struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
33318dd66eaSKuninori Morimoto 						   const char *driver_name)
33418dd66eaSKuninori Morimoto {
33518dd66eaSKuninori Morimoto 	struct snd_soc_component *component;
33618dd66eaSKuninori Morimoto 
33718dd66eaSKuninori Morimoto 	mutex_lock(&client_mutex);
33818dd66eaSKuninori Morimoto 	component = snd_soc_lookup_component_nolocked(dev, driver_name);
33918dd66eaSKuninori Morimoto 	mutex_unlock(&client_mutex);
34018dd66eaSKuninori Morimoto 
34118dd66eaSKuninori Morimoto 	return component;
34218dd66eaSKuninori Morimoto }
343b8132657SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
344b8132657SKuninori Morimoto 
34594def8eaSKuninori Morimoto struct snd_soc_pcm_runtime
34694def8eaSKuninori Morimoto *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
3474468189fSKuninori Morimoto 			 struct snd_soc_dai_link *dai_link)
34894def8eaSKuninori Morimoto {
34994def8eaSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
35094def8eaSKuninori Morimoto 
35194def8eaSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
3524468189fSKuninori Morimoto 		if (rtd->dai_link == dai_link)
35394def8eaSKuninori Morimoto 			return rtd;
35494def8eaSKuninori Morimoto 	}
3554468189fSKuninori Morimoto 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link->name);
35694def8eaSKuninori Morimoto 	return NULL;
35794def8eaSKuninori Morimoto }
35894def8eaSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
35994def8eaSKuninori Morimoto 
36083f94a2eSKuninori Morimoto /*
36183f94a2eSKuninori Morimoto  * Power down the audio subsystem pmdown_time msecs after close is called.
36283f94a2eSKuninori Morimoto  * This is to ensure there are no pops or clicks in between any music tracks
36383f94a2eSKuninori Morimoto  * due to DAPM power cycling.
36483f94a2eSKuninori Morimoto  */
36583f94a2eSKuninori Morimoto void snd_soc_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
36683f94a2eSKuninori Morimoto {
36783f94a2eSKuninori Morimoto 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
36883f94a2eSKuninori Morimoto 
36983f94a2eSKuninori Morimoto 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
37083f94a2eSKuninori Morimoto 
37183f94a2eSKuninori Morimoto 	dev_dbg(rtd->dev,
37283f94a2eSKuninori Morimoto 		"ASoC: pop wq checking: %s status: %s waiting: %s\n",
37383f94a2eSKuninori Morimoto 		codec_dai->driver->playback.stream_name,
37483f94a2eSKuninori Morimoto 		codec_dai->playback_active ? "active" : "inactive",
37583f94a2eSKuninori Morimoto 		rtd->pop_wait ? "yes" : "no");
37683f94a2eSKuninori Morimoto 
37783f94a2eSKuninori Morimoto 	/* are we waiting on this codec DAI stream */
37883f94a2eSKuninori Morimoto 	if (rtd->pop_wait == 1) {
37983f94a2eSKuninori Morimoto 		rtd->pop_wait = 0;
38083f94a2eSKuninori Morimoto 		snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
38183f94a2eSKuninori Morimoto 					  SND_SOC_DAPM_STREAM_STOP);
38283f94a2eSKuninori Morimoto 	}
38383f94a2eSKuninori Morimoto 
38483f94a2eSKuninori Morimoto 	mutex_unlock(&rtd->card->pcm_mutex);
38583f94a2eSKuninori Morimoto }
38683f94a2eSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_close_delayed_work);
38783f94a2eSKuninori Morimoto 
3886e864344SKuninori Morimoto static void soc_release_rtd_dev(struct device *dev)
3896e864344SKuninori Morimoto {
3906e864344SKuninori Morimoto 	/* "dev" means "rtd->dev" */
3916e864344SKuninori Morimoto 	kfree(dev);
3926e864344SKuninori Morimoto }
3936e864344SKuninori Morimoto 
3941c93a9e0SKuninori Morimoto static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
3951c93a9e0SKuninori Morimoto {
3966e864344SKuninori Morimoto 	if (!rtd)
3976e864344SKuninori Morimoto 		return;
3986e864344SKuninori Morimoto 
399753ace0aSKuninori Morimoto 	list_del(&rtd->list);
400b7c5bc45SKuninori Morimoto 
4019c9b6520SCurtis Malainey 	if (delayed_work_pending(&rtd->delayed_work))
4020ced7b05SKuninori Morimoto 		flush_delayed_work(&rtd->delayed_work);
4030ced7b05SKuninori Morimoto 	snd_soc_pcm_component_free(rtd);
4040ced7b05SKuninori Morimoto 
405b7c5bc45SKuninori Morimoto 	/*
406b7c5bc45SKuninori Morimoto 	 * we don't need to call kfree() for rtd->dev
407b7c5bc45SKuninori Morimoto 	 * see
408b7c5bc45SKuninori Morimoto 	 *	soc_release_rtd_dev()
409d918a376SKuninori Morimoto 	 *
410d918a376SKuninori Morimoto 	 * We don't need rtd->dev NULL check, because
411d918a376SKuninori Morimoto 	 * it is alloced *before* rtd.
412d918a376SKuninori Morimoto 	 * see
413d918a376SKuninori Morimoto 	 *	soc_new_pcm_runtime()
414b7c5bc45SKuninori Morimoto 	 */
415b7c5bc45SKuninori Morimoto 	device_unregister(rtd->dev);
4161c93a9e0SKuninori Morimoto }
4171c93a9e0SKuninori Morimoto 
4184bf2e385SCurtis Malainey static void close_delayed_work(struct work_struct *work) {
4194bf2e385SCurtis Malainey 	struct snd_soc_pcm_runtime *rtd =
4204bf2e385SCurtis Malainey 			container_of(work, struct snd_soc_pcm_runtime,
4214bf2e385SCurtis Malainey 				     delayed_work.work);
4224bf2e385SCurtis Malainey 
4234bf2e385SCurtis Malainey 	if (rtd->close_delayed_work_func)
4244bf2e385SCurtis Malainey 		rtd->close_delayed_work_func(rtd);
4254bf2e385SCurtis Malainey }
4264bf2e385SCurtis Malainey 
4271a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
4281a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
4291a497983SMengdong Lin {
4301a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
431613fb500SKuninori Morimoto 	struct snd_soc_component *component;
432d918a376SKuninori Morimoto 	struct device *dev;
4336e864344SKuninori Morimoto 	int ret;
4341a497983SMengdong Lin 
4356e864344SKuninori Morimoto 	/*
436d918a376SKuninori Morimoto 	 * for rtd->dev
437d918a376SKuninori Morimoto 	 */
438d918a376SKuninori Morimoto 	dev = kzalloc(sizeof(struct device), GFP_KERNEL);
439d918a376SKuninori Morimoto 	if (!dev)
440d918a376SKuninori Morimoto 		return NULL;
441d918a376SKuninori Morimoto 
442d918a376SKuninori Morimoto 	dev->parent	= card->dev;
443d918a376SKuninori Morimoto 	dev->release	= soc_release_rtd_dev;
444d918a376SKuninori Morimoto 	dev->groups	= soc_dev_attr_groups;
445d918a376SKuninori Morimoto 
446d918a376SKuninori Morimoto 	dev_set_name(dev, "%s", dai_link->name);
447d918a376SKuninori Morimoto 
448d918a376SKuninori Morimoto 	ret = device_register(dev);
449d918a376SKuninori Morimoto 	if (ret < 0) {
450d918a376SKuninori Morimoto 		put_device(dev); /* soc_release_rtd_dev */
451d918a376SKuninori Morimoto 		return NULL;
452d918a376SKuninori Morimoto 	}
453d918a376SKuninori Morimoto 
454d918a376SKuninori Morimoto 	/*
4556e864344SKuninori Morimoto 	 * for rtd
4566e864344SKuninori Morimoto 	 */
457613fb500SKuninori Morimoto 	rtd = devm_kzalloc(dev,
458613fb500SKuninori Morimoto 			   sizeof(*rtd) +
459613fb500SKuninori Morimoto 			   sizeof(*component) * (dai_link->num_cpus +
460613fb500SKuninori Morimoto 						 dai_link->num_codecs +
461613fb500SKuninori Morimoto 						 dai_link->num_platforms),
462613fb500SKuninori Morimoto 			   GFP_KERNEL);
4631a497983SMengdong Lin 	if (!rtd)
4646e864344SKuninori Morimoto 		goto free_rtd;
4651a497983SMengdong Lin 
466d918a376SKuninori Morimoto 	rtd->dev = dev;
46707d22a9bSTakashi Iwai 	INIT_LIST_HEAD(&rtd->list);
46807d22a9bSTakashi Iwai 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
46907d22a9bSTakashi Iwai 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
47007d22a9bSTakashi Iwai 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
47107d22a9bSTakashi Iwai 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
472d918a376SKuninori Morimoto 	dev_set_drvdata(dev, rtd);
4734bf2e385SCurtis Malainey 	INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
474d918a376SKuninori Morimoto 
4756e864344SKuninori Morimoto 	/*
4766e864344SKuninori Morimoto 	 * for rtd->codec_dais
4776e864344SKuninori Morimoto 	 */
4784dc0e7dfSKuninori Morimoto 	rtd->codec_dais = devm_kcalloc(dev, dai_link->num_codecs,
4796396bb22SKees Cook 					sizeof(struct snd_soc_dai *),
4801a497983SMengdong Lin 					GFP_KERNEL);
4816e864344SKuninori Morimoto 	if (!rtd->codec_dais)
4826e864344SKuninori Morimoto 		goto free_rtd;
4836e864344SKuninori Morimoto 
4846e864344SKuninori Morimoto 	/*
4856e864344SKuninori Morimoto 	 * rtd remaining settings
4866e864344SKuninori Morimoto 	 */
487929deb84SKuninori Morimoto 	rtd->card = card;
488929deb84SKuninori Morimoto 	rtd->dai_link = dai_link;
489929deb84SKuninori Morimoto 
4906634e3d6SKuninori Morimoto 	/* see for_each_card_rtds */
4911a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
4921a497983SMengdong Lin 	rtd->num = card->num_rtd;
4931a497983SMengdong Lin 	card->num_rtd++;
494a848125eSKuninori Morimoto 
495a848125eSKuninori Morimoto 	return rtd;
4966e864344SKuninori Morimoto 
4976e864344SKuninori Morimoto free_rtd:
4986e864344SKuninori Morimoto 	soc_free_pcm_runtime(rtd);
4996e864344SKuninori Morimoto 	return NULL;
5001a497983SMengdong Lin }
5011a497983SMengdong Lin 
50265462e44SKuninori Morimoto static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
50365462e44SKuninori Morimoto {
50465462e44SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
50565462e44SKuninori Morimoto 
50665462e44SKuninori Morimoto 	for_each_card_rtds(card, rtd)
50765462e44SKuninori Morimoto 		flush_delayed_work(&rtd->delayed_work);
50865462e44SKuninori Morimoto }
50965462e44SKuninori Morimoto 
5106f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
511db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5126f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
513db2a4165SFrank Mandarino {
5146f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
515d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
5161a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
5171a497983SMengdong Lin 	int i;
518db2a4165SFrank Mandarino 
519c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
520c5599b87SLars-Peter Clausen 	if (!card->instantiated)
521e3509ff0SDaniel Mack 		return 0;
522e3509ff0SDaniel Mack 
5232c7b696aSMarcel Ziswiler 	/*
5242c7b696aSMarcel Ziswiler 	 * Due to the resume being scheduled into a workqueue we could
5256ed25978SAndy Green 	 * suspend before that's finished - wait for it to complete.
5266ed25978SAndy Green 	 */
527f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
5286ed25978SAndy Green 
5296ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
530f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5316ed25978SAndy Green 
532a00f90f9SMark Brown 	/* mute any active DACs */
533bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5340b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
5353efab7dcSMark Brown 
5361a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5373efab7dcSMark Brown 			continue;
5383efab7dcSMark Brown 
5390b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
54088fdffa2SKuninori Morimoto 			if (dai->playback_active)
54188fdffa2SKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 1,
54288fdffa2SKuninori Morimoto 						SNDRV_PCM_STREAM_PLAYBACK);
543db2a4165SFrank Mandarino 		}
54488bd870fSBenoit Cousson 	}
545db2a4165SFrank Mandarino 
5464ccab3e7SLiam Girdwood 	/* suspend all pcms */
547bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5481a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5493efab7dcSMark Brown 			continue;
5503efab7dcSMark Brown 
5511a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
5523efab7dcSMark Brown 	}
5534ccab3e7SLiam Girdwood 
55487506549SMark Brown 	if (card->suspend_pre)
55570b2ac12SMark Brown 		card->suspend_pre(card);
556db2a4165SFrank Mandarino 
55737660b6dSLars-Peter Clausen 	/* close any waiting streams */
55865462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
559db2a4165SFrank Mandarino 
560bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5613efab7dcSMark Brown 
5621a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5633efab7dcSMark Brown 			continue;
5643efab7dcSMark Brown 
5651a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
5667bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
567db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
568f0fba2adSLiam Girdwood 
5691a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
5707bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
571db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
572db2a4165SFrank Mandarino 	}
573db2a4165SFrank Mandarino 
5748be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
5758be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
576e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
577e2d32ff6SMark Brown 
5789178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
5791272063aSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5801272063aSKuninori Morimoto 
5811272063aSKuninori Morimoto 		if (rtd->dai_link->ignore_suspend)
5821272063aSKuninori Morimoto 			continue;
5831272063aSKuninori Morimoto 
5841272063aSKuninori Morimoto 		for_each_rtd_components(rtd, i, component) {
5852c7b696aSMarcel Ziswiler 			struct snd_soc_dapm_context *dapm =
5862c7b696aSMarcel Ziswiler 				snd_soc_component_get_dapm(component);
587d9fc4063SKuninori Morimoto 
5882c7b696aSMarcel Ziswiler 			/*
5891272063aSKuninori Morimoto 			 * ignore if component was already suspended
5902c7b696aSMarcel Ziswiler 			 */
5911272063aSKuninori Morimoto 			if (snd_soc_component_is_suspended(component))
5921272063aSKuninori Morimoto 				continue;
5931272063aSKuninori Morimoto 
5941272063aSKuninori Morimoto 			/*
5951272063aSKuninori Morimoto 			 * If there are paths active then the COMPONENT will be
5961272063aSKuninori Morimoto 			 * held with bias _ON and should not be suspended.
5971272063aSKuninori Morimoto 			 */
5984890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
5991547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
600125a25daSMark Brown 				/*
6019178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
602125a25daSMark Brown 				 * bias off then being in STANDBY
603125a25daSMark Brown 				 * means it's doing something,
604125a25daSMark Brown 				 * otherwise fall through.
605125a25daSMark Brown 				 */
6064890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
6079178feb4SKuninori Morimoto 					dev_dbg(component->dev,
60810e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
609125a25daSMark Brown 					break;
610125a25daSMark Brown 				}
6111a12d5dcSGustavo A. R. Silva 				/* fall through */
612a8093297SLars-Peter Clausen 
6131547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
61466c51573SKuninori Morimoto 				snd_soc_component_suspend(component);
6159178feb4SKuninori Morimoto 				if (component->regmap)
6169178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
617988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
6189178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
6191547aba9SMark Brown 				break;
6201547aba9SMark Brown 			default:
6219178feb4SKuninori Morimoto 				dev_dbg(component->dev,
6229178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
6231547aba9SMark Brown 				break;
6241547aba9SMark Brown 			}
6251547aba9SMark Brown 		}
626f0fba2adSLiam Girdwood 	}
627db2a4165SFrank Mandarino 
62887506549SMark Brown 	if (card->suspend_post)
62970b2ac12SMark Brown 		card->suspend_post(card);
630db2a4165SFrank Mandarino 
631db2a4165SFrank Mandarino 	return 0;
632db2a4165SFrank Mandarino }
6336f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
634db2a4165SFrank Mandarino 
6352c7b696aSMarcel Ziswiler /*
6362c7b696aSMarcel Ziswiler  * deferred resume work, so resume can complete before we finished
6376ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6386ed25978SAndy Green  */
6396ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
640db2a4165SFrank Mandarino {
641f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
6422c7b696aSMarcel Ziswiler 			container_of(work, struct snd_soc_card,
6432c7b696aSMarcel Ziswiler 				     deferred_resume_work);
6441a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
645d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
6461a497983SMengdong Lin 	int i;
647db2a4165SFrank Mandarino 
6482c7b696aSMarcel Ziswiler 	/*
6492c7b696aSMarcel Ziswiler 	 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6506ed25978SAndy Green 	 * so userspace apps are blocked from touching us
6516ed25978SAndy Green 	 */
6526ed25978SAndy Green 
653f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
6546ed25978SAndy Green 
6559949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
656f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
6579949788bSMark Brown 
65887506549SMark Brown 	if (card->resume_pre)
65970b2ac12SMark Brown 		card->resume_pre(card);
660db2a4165SFrank Mandarino 
661f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
662e40fadbcSKuninori Morimoto 		if (snd_soc_component_is_suspended(component))
6639a840cbaSKuninori Morimoto 			snd_soc_component_resume(component);
6641547aba9SMark Brown 	}
665db2a4165SFrank Mandarino 
666bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6673efab7dcSMark Brown 
6681a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6693efab7dcSMark Brown 			continue;
6703efab7dcSMark Brown 
6711a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
672d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
673db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
674f0fba2adSLiam Girdwood 
6751a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
676d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
677db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
678db2a4165SFrank Mandarino 	}
679db2a4165SFrank Mandarino 
6803ff3f64bSMark Brown 	/* unmute any active DACs */
681bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6820b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
6833efab7dcSMark Brown 
6841a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6853efab7dcSMark Brown 			continue;
6863efab7dcSMark Brown 
6870b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
68888fdffa2SKuninori Morimoto 			if (dai->playback_active)
68988fdffa2SKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 0,
69088fdffa2SKuninori Morimoto 						SNDRV_PCM_STREAM_PLAYBACK);
691db2a4165SFrank Mandarino 		}
69288bd870fSBenoit Cousson 	}
693db2a4165SFrank Mandarino 
69487506549SMark Brown 	if (card->resume_post)
69570b2ac12SMark Brown 		card->resume_post(card);
696db2a4165SFrank Mandarino 
697f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
6986ed25978SAndy Green 
6998be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
7008be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
701e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
7021a7aaa58SJeeja KP 
7031a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
7041a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
7056ed25978SAndy Green }
7066ed25978SAndy Green 
7076ed25978SAndy Green /* powers up audio subsystem after a suspend */
7086f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
7096ed25978SAndy Green {
7106f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
71176c39e86SKuninori Morimoto 	struct snd_soc_component *component;
712b9dd94a8SPeter Ujfalusi 
713c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
714c5599b87SLars-Peter Clausen 	if (!card->instantiated)
7155ff1ddf2SEric Miao 		return 0;
7165ff1ddf2SEric Miao 
717988e8cc4SNicolin Chen 	/* activate pins from sleep state */
71876c39e86SKuninori Morimoto 	for_each_card_components(card, component)
71976c39e86SKuninori Morimoto 		if (component->active)
72076c39e86SKuninori Morimoto 			pinctrl_pm_select_default_state(component->dev);
721988e8cc4SNicolin Chen 
722f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: Scheduling resume work\n");
7236308419aSMark Brown 	if (!schedule_work(&card->deferred_resume_work))
724f110bfc7SLiam Girdwood 		dev_err(dev, "ASoC: resume work item may be lost\n");
7256ed25978SAndy Green 
726db2a4165SFrank Mandarino 	return 0;
727db2a4165SFrank Mandarino }
7286f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
729b3da4251SKuninori Morimoto 
730b3da4251SKuninori Morimoto static void soc_resume_init(struct snd_soc_card *card)
731b3da4251SKuninori Morimoto {
732b3da4251SKuninori Morimoto 	/* deferred resume work */
733b3da4251SKuninori Morimoto 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
734b3da4251SKuninori Morimoto }
735db2a4165SFrank Mandarino #else
7366f8ab4acSMark Brown #define snd_soc_suspend NULL
7376f8ab4acSMark Brown #define snd_soc_resume NULL
738b3da4251SKuninori Morimoto static inline void soc_resume_init(struct snd_soc_card *card)
739b3da4251SKuninori Morimoto {
740b3da4251SKuninori Morimoto }
741db2a4165SFrank Mandarino #endif
742db2a4165SFrank Mandarino 
74385e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
74402a06d30SBarry Song };
74502a06d30SBarry Song 
746c0834440SKuninori Morimoto static struct device_node
747c0834440SKuninori Morimoto *soc_component_to_node(struct snd_soc_component *component)
74812023a9aSMisael Lopez Cruz {
749c0834440SKuninori Morimoto 	struct device_node *of_node;
75012023a9aSMisael Lopez Cruz 
751c0834440SKuninori Morimoto 	of_node = component->dev->of_node;
752c0834440SKuninori Morimoto 	if (!of_node && component->dev->parent)
753c0834440SKuninori Morimoto 		of_node = component->dev->parent->of_node;
75434e81ab4SLars-Peter Clausen 
755c0834440SKuninori Morimoto 	return of_node;
75612023a9aSMisael Lopez Cruz }
75712023a9aSMisael Lopez Cruz 
758be6ac0a9SKuninori Morimoto static int snd_soc_is_matching_component(
759be6ac0a9SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc,
760be6ac0a9SKuninori Morimoto 	struct snd_soc_component *component)
761be6ac0a9SKuninori Morimoto {
762be6ac0a9SKuninori Morimoto 	struct device_node *component_of_node;
763be6ac0a9SKuninori Morimoto 
7647d7db5d3SKuninori Morimoto 	if (!dlc)
7657d7db5d3SKuninori Morimoto 		return 0;
7667d7db5d3SKuninori Morimoto 
7677d7db5d3SKuninori Morimoto 	component_of_node = soc_component_to_node(component);
768be6ac0a9SKuninori Morimoto 
769be6ac0a9SKuninori Morimoto 	if (dlc->of_node && component_of_node != dlc->of_node)
770be6ac0a9SKuninori Morimoto 		return 0;
771be6ac0a9SKuninori Morimoto 	if (dlc->name && strcmp(component->name, dlc->name))
772be6ac0a9SKuninori Morimoto 		return 0;
773be6ac0a9SKuninori Morimoto 
774be6ac0a9SKuninori Morimoto 	return 1;
775be6ac0a9SKuninori Morimoto }
776be6ac0a9SKuninori Morimoto 
777db2a4165SFrank Mandarino static struct snd_soc_component *soc_find_component(
778c1e230f0SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc)
779db2a4165SFrank Mandarino {
780db2a4165SFrank Mandarino 	struct snd_soc_component *component;
781db2a4165SFrank Mandarino 
782db2a4165SFrank Mandarino 	lockdep_assert_held(&client_mutex);
783db2a4165SFrank Mandarino 
784e3303268SKuninori Morimoto 	/*
785e3303268SKuninori Morimoto 	 * NOTE
786e3303268SKuninori Morimoto 	 *
787e3303268SKuninori Morimoto 	 * It returns *1st* found component, but some driver
788e3303268SKuninori Morimoto 	 * has few components by same of_node/name
789e3303268SKuninori Morimoto 	 * ex)
790e3303268SKuninori Morimoto 	 *	CPU component and generic DMAEngine component
791e3303268SKuninori Morimoto 	 */
792c1e230f0SKuninori Morimoto 	for_each_component(component)
793c1e230f0SKuninori Morimoto 		if (snd_soc_is_matching_component(dlc, component))
794db2a4165SFrank Mandarino 			return component;
795db2a4165SFrank Mandarino 
796db2a4165SFrank Mandarino 	return NULL;
79712023a9aSMisael Lopez Cruz }
79812023a9aSMisael Lopez Cruz 
799fbb88b5cSMengdong Lin /**
800fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
801fbb88b5cSMengdong Lin  *
8024958471bSJeffy Chen  * @dlc: name of the DAI or the DAI driver and optional component info to match
803fbb88b5cSMengdong Lin  *
804ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
805fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
806fbb88b5cSMengdong Lin  * should also match if being specified.
807fbb88b5cSMengdong Lin  *
808fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
809fbb88b5cSMengdong Lin  */
810305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
81114621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
81212023a9aSMisael Lopez Cruz {
81314621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
81414621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
81512023a9aSMisael Lopez Cruz 
81634e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
81734e81ab4SLars-Peter Clausen 
81814621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs */
819368dee94SKuninori Morimoto 	for_each_component(component) {
820be6ac0a9SKuninori Morimoto 		if (!snd_soc_is_matching_component(dlc, component))
82112023a9aSMisael Lopez Cruz 			continue;
82215a0c645SKuninori Morimoto 		for_each_component_dais(component, dai) {
8234958471bSJeffy Chen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
8246a6dafdaSJeffy Chen 			    && (!dai->driver->name
8256a6dafdaSJeffy Chen 				|| strcmp(dai->driver->name, dlc->dai_name)))
82614621c7eSLars-Peter Clausen 				continue;
82712023a9aSMisael Lopez Cruz 
82814621c7eSLars-Peter Clausen 			return dai;
82912023a9aSMisael Lopez Cruz 		}
83012023a9aSMisael Lopez Cruz 	}
83112023a9aSMisael Lopez Cruz 
83212023a9aSMisael Lopez Cruz 	return NULL;
83312023a9aSMisael Lopez Cruz }
834305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
83512023a9aSMisael Lopez Cruz 
836bfce78a5SKuninori Morimoto static int soc_dai_link_sanity_check(struct snd_soc_card *card,
83736794902SKuninori Morimoto 				     struct snd_soc_dai_link *link)
83836794902SKuninori Morimoto {
83936794902SKuninori Morimoto 	int i;
84036794902SKuninori Morimoto 	struct snd_soc_dai_link_component *codec, *platform;
84136794902SKuninori Morimoto 
84236794902SKuninori Morimoto 	for_each_link_codecs(link, i, codec) {
84336794902SKuninori Morimoto 		/*
84436794902SKuninori Morimoto 		 * Codec must be specified by 1 of name or OF node,
84536794902SKuninori Morimoto 		 * not both or neither.
84636794902SKuninori Morimoto 		 */
84736794902SKuninori Morimoto 		if (!!codec->name == !!codec->of_node) {
84836794902SKuninori Morimoto 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
84936794902SKuninori Morimoto 				link->name);
85036794902SKuninori Morimoto 			return -EINVAL;
85136794902SKuninori Morimoto 		}
85236794902SKuninori Morimoto 
85336794902SKuninori Morimoto 		/* Codec DAI name must be specified */
85436794902SKuninori Morimoto 		if (!codec->dai_name) {
85536794902SKuninori Morimoto 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
85636794902SKuninori Morimoto 				link->name);
85736794902SKuninori Morimoto 			return -EINVAL;
85836794902SKuninori Morimoto 		}
85936794902SKuninori Morimoto 
86036794902SKuninori Morimoto 		/*
86136794902SKuninori Morimoto 		 * Defer card registration if codec component is not added to
86236794902SKuninori Morimoto 		 * component list.
86336794902SKuninori Morimoto 		 */
86436794902SKuninori Morimoto 		if (!soc_find_component(codec))
86536794902SKuninori Morimoto 			return -EPROBE_DEFER;
86636794902SKuninori Morimoto 	}
86736794902SKuninori Morimoto 
86836794902SKuninori Morimoto 	for_each_link_platforms(link, i, platform) {
86936794902SKuninori Morimoto 		/*
87036794902SKuninori Morimoto 		 * Platform may be specified by either name or OF node, but it
87136794902SKuninori Morimoto 		 * can be left unspecified, then no components will be inserted
87236794902SKuninori Morimoto 		 * in the rtdcom list
87336794902SKuninori Morimoto 		 */
87436794902SKuninori Morimoto 		if (!!platform->name == !!platform->of_node) {
87536794902SKuninori Morimoto 			dev_err(card->dev,
87636794902SKuninori Morimoto 				"ASoC: Neither/both platform name/of_node are set for %s\n",
87736794902SKuninori Morimoto 				link->name);
87836794902SKuninori Morimoto 			return -EINVAL;
87936794902SKuninori Morimoto 		}
88036794902SKuninori Morimoto 
88136794902SKuninori Morimoto 		/*
88236794902SKuninori Morimoto 		 * Defer card registration if platform component is not added to
88336794902SKuninori Morimoto 		 * component list.
88436794902SKuninori Morimoto 		 */
88536794902SKuninori Morimoto 		if (!soc_find_component(platform))
88636794902SKuninori Morimoto 			return -EPROBE_DEFER;
88736794902SKuninori Morimoto 	}
88836794902SKuninori Morimoto 
88936794902SKuninori Morimoto 	/* FIXME */
89036794902SKuninori Morimoto 	if (link->num_cpus > 1) {
89136794902SKuninori Morimoto 		dev_err(card->dev,
89236794902SKuninori Morimoto 			"ASoC: multi cpu is not yet supported %s\n",
89336794902SKuninori Morimoto 			link->name);
89436794902SKuninori Morimoto 		return -EINVAL;
89536794902SKuninori Morimoto 	}
89636794902SKuninori Morimoto 
89736794902SKuninori Morimoto 	/*
89836794902SKuninori Morimoto 	 * CPU device may be specified by either name or OF node, but
89936794902SKuninori Morimoto 	 * can be left unspecified, and will be matched based on DAI
90036794902SKuninori Morimoto 	 * name alone..
90136794902SKuninori Morimoto 	 */
90236794902SKuninori Morimoto 	if (link->cpus->name && link->cpus->of_node) {
90336794902SKuninori Morimoto 		dev_err(card->dev,
90436794902SKuninori Morimoto 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
90536794902SKuninori Morimoto 			link->name);
90636794902SKuninori Morimoto 		return -EINVAL;
90736794902SKuninori Morimoto 	}
90836794902SKuninori Morimoto 
90936794902SKuninori Morimoto 	/*
910cd3c5ad7SKuninori Morimoto 	 * Defer card registration if cpu dai component is not added to
91136794902SKuninori Morimoto 	 * component list.
91236794902SKuninori Morimoto 	 */
91336794902SKuninori Morimoto 	if ((link->cpus->of_node || link->cpus->name) &&
91436794902SKuninori Morimoto 	    !soc_find_component(link->cpus))
91536794902SKuninori Morimoto 		return -EPROBE_DEFER;
91636794902SKuninori Morimoto 
91736794902SKuninori Morimoto 	/*
91836794902SKuninori Morimoto 	 * At least one of CPU DAI name or CPU device name/node must be
91936794902SKuninori Morimoto 	 * specified
92036794902SKuninori Morimoto 	 */
92136794902SKuninori Morimoto 	if (!link->cpus->dai_name &&
92236794902SKuninori Morimoto 	    !(link->cpus->name || link->cpus->of_node)) {
92336794902SKuninori Morimoto 		dev_err(card->dev,
92436794902SKuninori Morimoto 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
92536794902SKuninori Morimoto 			link->name);
92636794902SKuninori Morimoto 		return -EINVAL;
92736794902SKuninori Morimoto 	}
92836794902SKuninori Morimoto 
92936794902SKuninori Morimoto 	return 0;
93036794902SKuninori Morimoto }
93136794902SKuninori Morimoto 
932da704f26SKuninori Morimoto /**
93350cd9b53SKuninori Morimoto  * snd_soc_remove_pcm_runtime - Remove a pcm_runtime from card
93450cd9b53SKuninori Morimoto  * @card: The ASoC card to which the pcm_runtime has
93550cd9b53SKuninori Morimoto  * @rtd: The pcm_runtime to remove
936da704f26SKuninori Morimoto  *
93750cd9b53SKuninori Morimoto  * This function removes a pcm_runtime from the ASoC card.
938da704f26SKuninori Morimoto  */
93950cd9b53SKuninori Morimoto void snd_soc_remove_pcm_runtime(struct snd_soc_card *card,
94050cd9b53SKuninori Morimoto 				struct snd_soc_pcm_runtime *rtd)
941bc7a9091SKuninori Morimoto {
942da704f26SKuninori Morimoto 	lockdep_assert_held(&client_mutex);
943da704f26SKuninori Morimoto 
944da704f26SKuninori Morimoto 	/*
945da704f26SKuninori Morimoto 	 * Notify the machine driver for extra destruction
946da704f26SKuninori Morimoto 	 */
947da704f26SKuninori Morimoto 	if (card->remove_dai_link)
94850cd9b53SKuninori Morimoto 		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;
96934614739SJerome Brunet 	struct snd_soc_dai_link_component *codec, *platform;
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 	 */
97863dc47daSKuninori Morimoto 	if (card->add_dai_link)
97963dc47daSKuninori Morimoto 		card->add_dai_link(card, dai_link);
98063dc47daSKuninori Morimoto 
981a655de80SLiam Girdwood 	if (dai_link->ignore)
982a655de80SLiam Girdwood 		return 0;
983a655de80SLiam Girdwood 
9846f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
9856308419aSMark Brown 
986bfce78a5SKuninori Morimoto 	ret = soc_dai_link_sanity_check(card, dai_link);
987bfce78a5SKuninori Morimoto 	if (ret < 0)
988bfce78a5SKuninori Morimoto 		return ret;
989bfce78a5SKuninori Morimoto 
990513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
991513cb311SSudip Mukherjee 	if (!rtd)
992513cb311SSudip Mukherjee 		return -ENOMEM;
993513cb311SSudip Mukherjee 
99408a5841eSKuninori Morimoto 	/* FIXME: we need multi CPU support in the future */
99508a5841eSKuninori Morimoto 	rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
996b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
9976b490879SMartin Hundebøll 		dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
99808a5841eSKuninori Morimoto 			 dai_link->cpus->dai_name);
9991a497983SMengdong Lin 		goto _err_defer;
1000c5af3a2eSMark Brown 	}
100112b05232SKuninori Morimoto 	snd_soc_rtd_add_component(rtd, rtd->cpu_dai->component);
1002c5af3a2eSMark Brown 
100388bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
1004e2b30edfSKuninori Morimoto 	rtd->num_codecs = dai_link->num_codecs;
100534614739SJerome Brunet 	for_each_link_codecs(dai_link, i, codec) {
100634614739SJerome Brunet 		rtd->codec_dais[i] = snd_soc_find_dai(codec);
10070a2cfcd9SKuninori Morimoto 		if (!rtd->codec_dais[i]) {
10087c7e2d6aSStefan Agner 			dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
100934614739SJerome Brunet 				 codec->dai_name);
10101a497983SMengdong Lin 			goto _err_defer;
101112023a9aSMisael Lopez Cruz 		}
101234614739SJerome Brunet 
101312b05232SKuninori Morimoto 		snd_soc_rtd_add_component(rtd, rtd->codec_dais[i]->component);
101488bd870fSBenoit Cousson 	}
101588bd870fSBenoit Cousson 
101688bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
10170a2cfcd9SKuninori Morimoto 	rtd->codec_dai = rtd->codec_dais[0];
101812023a9aSMisael Lopez Cruz 
1019e2b30edfSKuninori Morimoto 	/* Find PLATFORM from registered PLATFORMs */
102034614739SJerome Brunet 	for_each_link_platforms(dai_link, i, platform) {
1021368dee94SKuninori Morimoto 		for_each_component(component) {
102234614739SJerome Brunet 			if (!snd_soc_is_matching_component(platform, component))
102390be711eSKuninori Morimoto 				continue;
102490be711eSKuninori Morimoto 
102512b05232SKuninori Morimoto 			snd_soc_rtd_add_component(rtd, component);
102690be711eSKuninori Morimoto 		}
102734614739SJerome Brunet 	}
102890be711eSKuninori Morimoto 
1029b19e6e7bSMark Brown 	return 0;
10301a497983SMengdong Lin 
10311a497983SMengdong Lin _err_defer:
103250cd9b53SKuninori Morimoto 	snd_soc_remove_pcm_runtime(card, rtd);
10331a497983SMengdong Lin 	return -EPROBE_DEFER;
10346b05eda6SMark Brown }
10350c048004SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_pcm_runtime);
10366b05eda6SMark Brown 
1037bfa0dd89SKuninori Morimoto static int soc_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
10389e9c70a5SKuninori Morimoto 			   struct snd_soc_pcm_runtime *rtd)
10399e9c70a5SKuninori Morimoto {
10409e9c70a5SKuninori Morimoto 	int i, ret = 0;
10419e9c70a5SKuninori Morimoto 
10429e9c70a5SKuninori Morimoto 	for (i = 0; i < num_dais; ++i) {
10439e9c70a5SKuninori Morimoto 		struct snd_soc_dai_driver *drv = dais[i]->driver;
10449e9c70a5SKuninori Morimoto 
10459e9c70a5SKuninori Morimoto 		if (drv->pcm_new)
10469e9c70a5SKuninori Morimoto 			ret = drv->pcm_new(rtd, dais[i]);
10479e9c70a5SKuninori Morimoto 		if (ret < 0) {
10489e9c70a5SKuninori Morimoto 			dev_err(dais[i]->dev,
10499e9c70a5SKuninori Morimoto 				"ASoC: Failed to bind %s with pcm device\n",
10509e9c70a5SKuninori Morimoto 				dais[i]->name);
10519e9c70a5SKuninori Morimoto 			return ret;
10529e9c70a5SKuninori Morimoto 		}
10539e9c70a5SKuninori Morimoto 	}
10549e9c70a5SKuninori Morimoto 
10559e9c70a5SKuninori Morimoto 	return 0;
10569e9c70a5SKuninori Morimoto }
10579e9c70a5SKuninori Morimoto 
1058eaffeefbSKuninori Morimoto static int soc_init_pcm_runtime(struct snd_soc_card *card,
105946496acbSKuninori Morimoto 				struct snd_soc_pcm_runtime *rtd)
106046496acbSKuninori Morimoto {
106146496acbSKuninori Morimoto 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
106246496acbSKuninori Morimoto 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
106346496acbSKuninori Morimoto 	struct snd_soc_component *component;
1064613fb500SKuninori Morimoto 	int ret, num, i;
106546496acbSKuninori Morimoto 
106646496acbSKuninori Morimoto 	/* set default power off timeout */
106746496acbSKuninori Morimoto 	rtd->pmdown_time = pmdown_time;
106846496acbSKuninori Morimoto 
106946496acbSKuninori Morimoto 	/* do machine specific initialization */
107046496acbSKuninori Morimoto 	if (dai_link->init) {
107146496acbSKuninori Morimoto 		ret = dai_link->init(rtd);
107246496acbSKuninori Morimoto 		if (ret < 0) {
107346496acbSKuninori Morimoto 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
107446496acbSKuninori Morimoto 				dai_link->name, ret);
107546496acbSKuninori Morimoto 			return ret;
107646496acbSKuninori Morimoto 		}
107746496acbSKuninori Morimoto 	}
107846496acbSKuninori Morimoto 
107946496acbSKuninori Morimoto 	if (dai_link->dai_fmt) {
108046496acbSKuninori Morimoto 		ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
108146496acbSKuninori Morimoto 		if (ret)
108246496acbSKuninori Morimoto 			return ret;
108346496acbSKuninori Morimoto 	}
108446496acbSKuninori Morimoto 
108546496acbSKuninori Morimoto 	/* add DPCM sysfs entries */
108646496acbSKuninori Morimoto 	soc_dpcm_debugfs_add(rtd);
108746496acbSKuninori Morimoto 
108846496acbSKuninori Morimoto 	num = rtd->num;
108946496acbSKuninori Morimoto 
109046496acbSKuninori Morimoto 	/*
109146496acbSKuninori Morimoto 	 * most drivers will register their PCMs using DAI link ordering but
109246496acbSKuninori Morimoto 	 * topology based drivers can use the DAI link id field to set PCM
109346496acbSKuninori Morimoto 	 * device number and then use rtd + a base offset of the BEs.
109446496acbSKuninori Morimoto 	 */
1095613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
109646496acbSKuninori Morimoto 		if (!component->driver->use_dai_pcm_id)
109746496acbSKuninori Morimoto 			continue;
109846496acbSKuninori Morimoto 
109946496acbSKuninori Morimoto 		if (rtd->dai_link->no_pcm)
110046496acbSKuninori Morimoto 			num += component->driver->be_pcm_base;
110146496acbSKuninori Morimoto 		else
110246496acbSKuninori Morimoto 			num = rtd->dai_link->id;
110346496acbSKuninori Morimoto 	}
110446496acbSKuninori Morimoto 
110546496acbSKuninori Morimoto 	/* create compress_device if possible */
110646496acbSKuninori Morimoto 	ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
110746496acbSKuninori Morimoto 	if (ret != -ENOTSUPP) {
110846496acbSKuninori Morimoto 		if (ret < 0)
110946496acbSKuninori Morimoto 			dev_err(card->dev, "ASoC: can't create compress %s\n",
111046496acbSKuninori Morimoto 				dai_link->stream_name);
111146496acbSKuninori Morimoto 		return ret;
111246496acbSKuninori Morimoto 	}
111346496acbSKuninori Morimoto 
111446496acbSKuninori Morimoto 	/* create the pcm */
111546496acbSKuninori Morimoto 	ret = soc_new_pcm(rtd, num);
111646496acbSKuninori Morimoto 	if (ret < 0) {
111746496acbSKuninori Morimoto 		dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
111846496acbSKuninori Morimoto 			dai_link->stream_name, ret);
111946496acbSKuninori Morimoto 		return ret;
112046496acbSKuninori Morimoto 	}
112146496acbSKuninori Morimoto 	ret = soc_dai_pcm_new(&cpu_dai, 1, rtd);
112246496acbSKuninori Morimoto 	if (ret < 0)
112346496acbSKuninori Morimoto 		return ret;
112446496acbSKuninori Morimoto 	ret = soc_dai_pcm_new(rtd->codec_dais,
112546496acbSKuninori Morimoto 			      rtd->num_codecs, rtd);
112646496acbSKuninori Morimoto 	return ret;
112746496acbSKuninori Morimoto }
112846496acbSKuninori Morimoto 
1129ffd60fbaSKuninori Morimoto static void soc_set_name_prefix(struct snd_soc_card *card,
1130ffd60fbaSKuninori Morimoto 				struct snd_soc_component *component)
1131ffd60fbaSKuninori Morimoto {
1132aec3ff99SKuninori Morimoto 	struct device_node *of_node = soc_component_to_node(component);
113329d9fc7aSKuninori Morimoto 	const char *str;
113429d9fc7aSKuninori Morimoto 	int ret, i;
1135ffd60fbaSKuninori Morimoto 
11364702f991SKuninori Morimoto 	for (i = 0; i < card->num_configs; i++) {
1137ffd60fbaSKuninori Morimoto 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1138ffd60fbaSKuninori Morimoto 
1139c13493a2SKuninori Morimoto 		if (snd_soc_is_matching_component(&map->dlc, component)) {
1140ffd60fbaSKuninori Morimoto 			component->name_prefix = map->name_prefix;
1141ffd60fbaSKuninori Morimoto 			return;
1142ffd60fbaSKuninori Morimoto 		}
1143c13493a2SKuninori Morimoto 	}
1144ffd60fbaSKuninori Morimoto 
1145ffd60fbaSKuninori Morimoto 	/*
1146ffd60fbaSKuninori Morimoto 	 * If there is no configuration table or no match in the table,
1147ffd60fbaSKuninori Morimoto 	 * check if a prefix is provided in the node
1148ffd60fbaSKuninori Morimoto 	 */
114929d9fc7aSKuninori Morimoto 	ret = of_property_read_string(of_node, "sound-name-prefix", &str);
115029d9fc7aSKuninori Morimoto 	if (ret < 0)
115129d9fc7aSKuninori Morimoto 		return;
115229d9fc7aSKuninori Morimoto 
115329d9fc7aSKuninori Morimoto 	component->name_prefix = str;
1154ffd60fbaSKuninori Morimoto }
1155ffd60fbaSKuninori Morimoto 
1156c6619b72SKuninori Morimoto static void soc_remove_component(struct snd_soc_component *component,
1157c6619b72SKuninori Morimoto 				 int probed)
115822d14231SKuninori Morimoto {
1159c6619b72SKuninori Morimoto 
1160c6619b72SKuninori Morimoto 	if (!component->card)
1161c6619b72SKuninori Morimoto 		return;
1162c6619b72SKuninori Morimoto 
1163c6619b72SKuninori Morimoto 	if (probed)
1164c6619b72SKuninori Morimoto 		snd_soc_component_remove(component);
1165c6619b72SKuninori Morimoto 
116604f770d9SKuninori Morimoto 	/* For framework level robustness */
11673bb936f5SAmadeusz Sławiński 	snd_soc_component_set_jack(component, NULL, NULL);
116804f770d9SKuninori Morimoto 
11697a5d9815SBard liao 	list_del_init(&component->card_list);
117022d14231SKuninori Morimoto 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
117122d14231SKuninori Morimoto 	soc_cleanup_component_debugfs(component);
117222d14231SKuninori Morimoto 	component->card = NULL;
11730e36f36bSPierre-Louis Bossart 	snd_soc_component_module_put_when_remove(component);
117422d14231SKuninori Morimoto }
117522d14231SKuninori Morimoto 
1176ffd60fbaSKuninori Morimoto static int soc_probe_component(struct snd_soc_card *card,
1177ffd60fbaSKuninori Morimoto 			       struct snd_soc_component *component)
1178ffd60fbaSKuninori Morimoto {
1179ffd60fbaSKuninori Morimoto 	struct snd_soc_dapm_context *dapm =
1180ffd60fbaSKuninori Morimoto 		snd_soc_component_get_dapm(component);
1181ffd60fbaSKuninori Morimoto 	struct snd_soc_dai *dai;
1182c6619b72SKuninori Morimoto 	int probed = 0;
1183ffd60fbaSKuninori Morimoto 	int ret;
1184ffd60fbaSKuninori Morimoto 
1185ffd60fbaSKuninori Morimoto 	if (!strcmp(component->name, "snd-soc-dummy"))
1186ffd60fbaSKuninori Morimoto 		return 0;
1187ffd60fbaSKuninori Morimoto 
1188ffd60fbaSKuninori Morimoto 	if (component->card) {
1189ffd60fbaSKuninori Morimoto 		if (component->card != card) {
1190ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1191ffd60fbaSKuninori Morimoto 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1192ffd60fbaSKuninori Morimoto 				card->name, component->card->name);
1193ffd60fbaSKuninori Morimoto 			return -ENODEV;
1194ffd60fbaSKuninori Morimoto 		}
1195ffd60fbaSKuninori Morimoto 		return 0;
1196ffd60fbaSKuninori Morimoto 	}
1197ffd60fbaSKuninori Morimoto 
1198ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_module_get_when_probe(component);
1199ffd60fbaSKuninori Morimoto 	if (ret < 0)
1200ffd60fbaSKuninori Morimoto 		return ret;
1201ffd60fbaSKuninori Morimoto 
1202ffd60fbaSKuninori Morimoto 	component->card = card;
1203ffd60fbaSKuninori Morimoto 	soc_set_name_prefix(card, component);
1204ffd60fbaSKuninori Morimoto 
1205ffd60fbaSKuninori Morimoto 	soc_init_component_debugfs(component);
1206ffd60fbaSKuninori Morimoto 
120795c267ddSKuninori Morimoto 	snd_soc_dapm_init(dapm, card, component);
1208b614beafSKuninori Morimoto 
1209ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_new_controls(dapm,
1210ffd60fbaSKuninori Morimoto 					component->driver->dapm_widgets,
1211ffd60fbaSKuninori Morimoto 					component->driver->num_dapm_widgets);
1212ffd60fbaSKuninori Morimoto 
1213ffd60fbaSKuninori Morimoto 	if (ret != 0) {
1214ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1215ffd60fbaSKuninori Morimoto 			"Failed to create new controls %d\n", ret);
1216ffd60fbaSKuninori Morimoto 		goto err_probe;
1217ffd60fbaSKuninori Morimoto 	}
1218ffd60fbaSKuninori Morimoto 
1219ffd60fbaSKuninori Morimoto 	for_each_component_dais(component, dai) {
1220ffd60fbaSKuninori Morimoto 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1221ffd60fbaSKuninori Morimoto 		if (ret != 0) {
1222ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1223ffd60fbaSKuninori Morimoto 				"Failed to create DAI widgets %d\n", ret);
1224ffd60fbaSKuninori Morimoto 			goto err_probe;
1225ffd60fbaSKuninori Morimoto 		}
1226ffd60fbaSKuninori Morimoto 	}
1227ffd60fbaSKuninori Morimoto 
1228ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_probe(component);
1229ffd60fbaSKuninori Morimoto 	if (ret < 0) {
1230ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1231ffd60fbaSKuninori Morimoto 			"ASoC: failed to probe component %d\n", ret);
1232ffd60fbaSKuninori Morimoto 		goto err_probe;
1233ffd60fbaSKuninori Morimoto 	}
1234ffd60fbaSKuninori Morimoto 	WARN(dapm->idle_bias_off &&
1235ffd60fbaSKuninori Morimoto 	     dapm->bias_level != SND_SOC_BIAS_OFF,
1236ffd60fbaSKuninori Morimoto 	     "codec %s can not start from non-off bias with idle_bias_off==1\n",
1237ffd60fbaSKuninori Morimoto 	     component->name);
1238c6619b72SKuninori Morimoto 	probed = 1;
1239ffd60fbaSKuninori Morimoto 
1240ffd60fbaSKuninori Morimoto 	/* machine specific init */
1241ffd60fbaSKuninori Morimoto 	if (component->init) {
1242ffd60fbaSKuninori Morimoto 		ret = component->init(component);
1243ffd60fbaSKuninori Morimoto 		if (ret < 0) {
1244ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1245ffd60fbaSKuninori Morimoto 				"Failed to do machine specific init %d\n", ret);
1246ffd60fbaSKuninori Morimoto 			goto err_probe;
1247ffd60fbaSKuninori Morimoto 		}
1248ffd60fbaSKuninori Morimoto 	}
1249ffd60fbaSKuninori Morimoto 
1250ffd60fbaSKuninori Morimoto 	ret = snd_soc_add_component_controls(component,
1251ffd60fbaSKuninori Morimoto 					     component->driver->controls,
1252ffd60fbaSKuninori Morimoto 					     component->driver->num_controls);
1253ffd60fbaSKuninori Morimoto 	if (ret < 0)
1254ffd60fbaSKuninori Morimoto 		goto err_probe;
1255ffd60fbaSKuninori Morimoto 
1256ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(dapm,
1257ffd60fbaSKuninori Morimoto 				      component->driver->dapm_routes,
1258ffd60fbaSKuninori Morimoto 				      component->driver->num_dapm_routes);
1259*a22ae72bSPierre-Louis Bossart 	if (ret < 0) {
1260*a22ae72bSPierre-Louis Bossart 		if (card->disable_route_checks) {
1261*a22ae72bSPierre-Louis Bossart 			dev_info(card->dev,
1262*a22ae72bSPierre-Louis Bossart 				 "%s: disable_route_checks set, ignoring errors on add_routes\n",
1263*a22ae72bSPierre-Louis Bossart 				 __func__);
1264*a22ae72bSPierre-Louis Bossart 		} else {
1265*a22ae72bSPierre-Louis Bossart 			dev_err(card->dev,
1266*a22ae72bSPierre-Louis Bossart 				"%s: snd_soc_dapm_add_routes failed: %d\n",
1267*a22ae72bSPierre-Louis Bossart 				__func__, ret);
1268ffd60fbaSKuninori Morimoto 			goto err_probe;
1269*a22ae72bSPierre-Louis Bossart 		}
1270*a22ae72bSPierre-Louis Bossart 	}
1271ffd60fbaSKuninori Morimoto 
1272ffd60fbaSKuninori Morimoto 	/* see for_each_card_components */
1273ffd60fbaSKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1274ffd60fbaSKuninori Morimoto 
1275ffd60fbaSKuninori Morimoto err_probe:
1276ffd60fbaSKuninori Morimoto 	if (ret < 0)
1277c6619b72SKuninori Morimoto 		soc_remove_component(component, probed);
1278ffd60fbaSKuninori Morimoto 
1279ffd60fbaSKuninori Morimoto 	return ret;
1280ffd60fbaSKuninori Morimoto }
1281ffd60fbaSKuninori Morimoto 
1282e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1283589c3563SJarkko Nikula {
1284589c3563SJarkko Nikula 	int err;
1285589c3563SJarkko Nikula 
128652abe6ccSGuennadi Liakhovetski 	if (!dai || !dai->probed || !dai->driver ||
12872eda3cb1SKuninori Morimoto 	    dai->driver->remove_order != order)
12882eda3cb1SKuninori Morimoto 		return;
12892eda3cb1SKuninori Morimoto 
1290dcdab582SKuninori Morimoto 	err = snd_soc_dai_remove(dai);
1291589c3563SJarkko Nikula 	if (err < 0)
1292e60cd14fSLars-Peter Clausen 		dev_err(dai->dev,
1293b0aa88afSMisael Lopez Cruz 			"ASoC: failed to remove %s: %d\n",
1294e60cd14fSLars-Peter Clausen 			dai->name, err);
1295dcdab582SKuninori Morimoto 
1296e60cd14fSLars-Peter Clausen 	dai->probed = 0;
1297b0aa88afSMisael Lopez Cruz }
1298b0aa88afSMisael Lopez Cruz 
1299a7d44f78SKuninori Morimoto static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1300a7d44f78SKuninori Morimoto {
1301a7d44f78SKuninori Morimoto 	int ret;
1302a7d44f78SKuninori Morimoto 
1303a7d44f78SKuninori Morimoto 	if (dai->probed ||
1304a7d44f78SKuninori Morimoto 	    dai->driver->probe_order != order)
1305a7d44f78SKuninori Morimoto 		return 0;
1306a7d44f78SKuninori Morimoto 
1307a7d44f78SKuninori Morimoto 	ret = snd_soc_dai_probe(dai);
1308a7d44f78SKuninori Morimoto 	if (ret < 0) {
1309a7d44f78SKuninori Morimoto 		dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1310a7d44f78SKuninori Morimoto 			dai->name, ret);
1311a7d44f78SKuninori Morimoto 		return ret;
1312a7d44f78SKuninori Morimoto 	}
1313a7d44f78SKuninori Morimoto 
1314a7d44f78SKuninori Morimoto 	dai->probed = 1;
1315a7d44f78SKuninori Morimoto 
1316a7d44f78SKuninori Morimoto 	return 0;
1317a7d44f78SKuninori Morimoto }
1318a7d44f78SKuninori Morimoto 
13194ca47d21SKuninori Morimoto static void soc_remove_link_dais(struct snd_soc_card *card)
1320f0fba2adSLiam Girdwood {
1321e60cd14fSLars-Peter Clausen 	int i;
13220b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
13234ca47d21SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
13244ca47d21SKuninori Morimoto 	int order;
13254ca47d21SKuninori Morimoto 
13264ca47d21SKuninori Morimoto 	for_each_comp_order(order) {
13274ca47d21SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1328f0fba2adSLiam Girdwood 			/* remove the CODEC DAI */
13290b7990e3SKuninori Morimoto 			for_each_rtd_codec_dai(rtd, i, codec_dai)
13300b7990e3SKuninori Morimoto 				soc_remove_dai(codec_dai, order);
1331f0fba2adSLiam Girdwood 
1332e60cd14fSLars-Peter Clausen 			soc_remove_dai(rtd->cpu_dai, order);
1333f0fba2adSLiam Girdwood 		}
13344ca47d21SKuninori Morimoto 	}
13354ca47d21SKuninori Morimoto }
1336f0fba2adSLiam Girdwood 
1337bc7c16c2SKuninori Morimoto static int soc_probe_link_dais(struct snd_soc_card *card)
1338bc7c16c2SKuninori Morimoto {
1339bc7c16c2SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1340bc7c16c2SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
1341bc7c16c2SKuninori Morimoto 	int i, order, ret;
1342bc7c16c2SKuninori Morimoto 
1343bc7c16c2SKuninori Morimoto 	for_each_comp_order(order) {
1344bc7c16c2SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1345bc7c16c2SKuninori Morimoto 
1346bc7c16c2SKuninori Morimoto 			dev_dbg(card->dev,
1347bc7c16c2SKuninori Morimoto 				"ASoC: probe %s dai link %d late %d\n",
1348bc7c16c2SKuninori Morimoto 				card->name, rtd->num, order);
1349bc7c16c2SKuninori Morimoto 
1350bc7c16c2SKuninori Morimoto 			ret = soc_probe_dai(rtd->cpu_dai, order);
1351bc7c16c2SKuninori Morimoto 			if (ret)
1352bc7c16c2SKuninori Morimoto 				return ret;
1353bc7c16c2SKuninori Morimoto 
1354bc7c16c2SKuninori Morimoto 			/* probe the CODEC DAI */
1355bc7c16c2SKuninori Morimoto 			for_each_rtd_codec_dai(rtd, i, codec_dai) {
1356bc7c16c2SKuninori Morimoto 				ret = soc_probe_dai(codec_dai, order);
1357bc7c16c2SKuninori Morimoto 				if (ret)
1358bc7c16c2SKuninori Morimoto 					return ret;
1359bc7c16c2SKuninori Morimoto 			}
1360bc7c16c2SKuninori Morimoto 		}
1361bc7c16c2SKuninori Morimoto 	}
1362bc7c16c2SKuninori Morimoto 
1363bc7c16c2SKuninori Morimoto 	return 0;
1364bc7c16c2SKuninori Morimoto }
1365bc7c16c2SKuninori Morimoto 
1366b006c0c6SKuninori Morimoto static void soc_remove_link_components(struct snd_soc_card *card)
136762ae68faSStephen Warren {
136861aca564SLars-Peter Clausen 	struct snd_soc_component *component;
1369b006c0c6SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
1370613fb500SKuninori Morimoto 	int i, order;
137162ae68faSStephen Warren 
1372b006c0c6SKuninori Morimoto 	for_each_comp_order(order) {
1373b006c0c6SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1374613fb500SKuninori Morimoto 			for_each_rtd_components(rtd, i, component) {
1375b006c0c6SKuninori Morimoto 				if (component->driver->remove_order != order)
1376b006c0c6SKuninori Morimoto 					continue;
1377b006c0c6SKuninori Morimoto 
1378c6619b72SKuninori Morimoto 				soc_remove_component(component, 1);
137962ae68faSStephen Warren 			}
138062ae68faSStephen Warren 		}
1381b006c0c6SKuninori Morimoto 	}
1382b006c0c6SKuninori Morimoto }
138362ae68faSStephen Warren 
138462f07a6bSKuninori Morimoto static int soc_probe_link_components(struct snd_soc_card *card)
13856fb03550SKuninori Morimoto {
13866fb03550SKuninori Morimoto 	struct snd_soc_component *component;
138762f07a6bSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
1388613fb500SKuninori Morimoto 	int i, ret, order;
13896fb03550SKuninori Morimoto 
139062f07a6bSKuninori Morimoto 	for_each_comp_order(order) {
139162f07a6bSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1392613fb500SKuninori Morimoto 			for_each_rtd_components(rtd, i, component) {
139362f07a6bSKuninori Morimoto 				if (component->driver->probe_order != order)
139462f07a6bSKuninori Morimoto 					continue;
139562f07a6bSKuninori Morimoto 
13966fb03550SKuninori Morimoto 				ret = soc_probe_component(card, component);
13976fb03550SKuninori Morimoto 				if (ret < 0)
13986fb03550SKuninori Morimoto 					return ret;
13996fb03550SKuninori Morimoto 			}
14006fb03550SKuninori Morimoto 		}
140162f07a6bSKuninori Morimoto 	}
14026fb03550SKuninori Morimoto 
14036fb03550SKuninori Morimoto 	return 0;
14046fb03550SKuninori Morimoto }
14056fb03550SKuninori Morimoto 
1406e8fbd250SKuninori Morimoto static void soc_unbind_aux_dev(struct snd_soc_card *card)
14074893a2ebSKuninori Morimoto {
1408e8fbd250SKuninori Morimoto 	struct snd_soc_component *component, *_component;
1409e8fbd250SKuninori Morimoto 
1410e8fbd250SKuninori Morimoto 	for_each_card_auxs_safe(card, component, _component) {
14114893a2ebSKuninori Morimoto 		component->init = NULL;
14124893a2ebSKuninori Morimoto 		list_del(&component->card_aux_list);
14134893a2ebSKuninori Morimoto 	}
1414e8fbd250SKuninori Morimoto }
14154893a2ebSKuninori Morimoto 
1416bee886f1SKuninori Morimoto static int soc_bind_aux_dev(struct snd_soc_card *card)
1417b19e6e7bSMark Brown {
1418f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1419bee886f1SKuninori Morimoto 	struct snd_soc_aux_dev *aux;
1420bee886f1SKuninori Morimoto 	int i;
14213ca041edSSebastian Reichel 
1422bee886f1SKuninori Morimoto 	for_each_card_pre_auxs(card, i, aux) {
1423f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1424bee886f1SKuninori Morimoto 		component = soc_find_component(&aux->dlc);
1425f2ed6b07SMengdong Lin 		if (!component)
14263dc29b8bSKuninori Morimoto 			return -EPROBE_DEFER;
14273ca041edSSebastian Reichel 
1428bee886f1SKuninori Morimoto 		component->init = aux->init;
1429c2b71c71SKuninori Morimoto 		/* see for_each_card_auxs */
1430d2e3a135SSylwester Nawrocki 		list_add(&component->card_aux_list, &card->aux_comp_list);
1431bee886f1SKuninori Morimoto 	}
1432f2ed6b07SMengdong Lin 	return 0;
1433b19e6e7bSMark Brown }
1434b19e6e7bSMark Brown 
1435f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1436f2ed6b07SMengdong Lin {
143774bd3f92SKuninori Morimoto 	struct snd_soc_component *component;
1438f2ed6b07SMengdong Lin 	int order;
1439f2ed6b07SMengdong Lin 	int ret;
1440f2ed6b07SMengdong Lin 
14411a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
144274bd3f92SKuninori Morimoto 		for_each_card_auxs(card, component) {
144374bd3f92SKuninori Morimoto 			if (component->driver->probe_order != order)
144474bd3f92SKuninori Morimoto 				continue;
144574bd3f92SKuninori Morimoto 
144674bd3f92SKuninori Morimoto 			ret = soc_probe_component(card,	component);
144774bd3f92SKuninori Morimoto 			if (ret < 0)
1448f2ed6b07SMengdong Lin 				return ret;
1449f2ed6b07SMengdong Lin 		}
1450f2ed6b07SMengdong Lin 	}
145165d9361fSLars-Peter Clausen 
145244c69bb1SLars-Peter Clausen 	return 0;
14533ca041edSSebastian Reichel }
14542eea392dSJarkko Nikula 
1455f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
145644c69bb1SLars-Peter Clausen {
1457f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1458f2ed6b07SMengdong Lin 	int order;
145944c69bb1SLars-Peter Clausen 
14601a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1461c2b71c71SKuninori Morimoto 		for_each_card_auxs_safe(card, comp, _comp) {
1462e8fbd250SKuninori Morimoto 			if (comp->driver->remove_order == order)
1463c6619b72SKuninori Morimoto 				soc_remove_component(comp, 1);
14645f3484acSLars-Peter Clausen 		}
14652eea392dSJarkko Nikula 	}
14662eea392dSJarkko Nikula }
14672eea392dSJarkko Nikula 
1468ce64c8b9SLars-Peter Clausen /**
1469ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1470ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1471ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1472ce64c8b9SLars-Peter Clausen  *
1473ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1474ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1475ce64c8b9SLars-Peter Clausen  *
1476ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1477ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1478ce64c8b9SLars-Peter Clausen  *
1479ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1480ce64c8b9SLars-Peter Clausen  */
1481ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1482ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1483ce64c8b9SLars-Peter Clausen {
1484ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
14850b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1486ce64c8b9SLars-Peter Clausen 	unsigned int i;
1487ce64c8b9SLars-Peter Clausen 	int ret;
1488ce64c8b9SLars-Peter Clausen 
14890b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1490ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1491ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1492ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1493ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1494ce64c8b9SLars-Peter Clausen 			return ret;
1495ce64c8b9SLars-Peter Clausen 		}
1496ce64c8b9SLars-Peter Clausen 	}
1497ce64c8b9SLars-Peter Clausen 
14982c7b696aSMarcel Ziswiler 	/*
14992c7b696aSMarcel Ziswiler 	 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
15002c7b696aSMarcel Ziswiler 	 * the component which has non_legacy_dai_naming is Codec
15012c7b696aSMarcel Ziswiler 	 */
1502999f7f5aSKuninori Morimoto 	if (cpu_dai->component->driver->non_legacy_dai_naming) {
1503ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1504ce64c8b9SLars-Peter Clausen 
1505ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1506ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1507ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1508ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1509ce64c8b9SLars-Peter Clausen 			break;
1510ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1511ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1512ce64c8b9SLars-Peter Clausen 			break;
1513ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1514ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1515ce64c8b9SLars-Peter Clausen 			break;
1516ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1517ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1518ce64c8b9SLars-Peter Clausen 			break;
1519ce64c8b9SLars-Peter Clausen 		}
1520ce64c8b9SLars-Peter Clausen 
1521ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1522ce64c8b9SLars-Peter Clausen 	}
1523ce64c8b9SLars-Peter Clausen 
1524ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1525ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1526ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1527ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1528ce64c8b9SLars-Peter Clausen 		return ret;
1529ce64c8b9SLars-Peter Clausen 	}
1530ce64c8b9SLars-Peter Clausen 
1531ce64c8b9SLars-Peter Clausen 	return 0;
1532ce64c8b9SLars-Peter Clausen }
1533ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1534ce64c8b9SLars-Peter Clausen 
15351f5a4535STakashi Iwai #ifdef CONFIG_DMI
15362c7b696aSMarcel Ziswiler /*
15378a6a6a38SKuninori Morimoto  * If a DMI filed contain strings in this blacklist (e.g.
15388a6a6a38SKuninori Morimoto  * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
15398a6a6a38SKuninori Morimoto  * as invalid and dropped when setting the card long name from DMI info.
15408a6a6a38SKuninori Morimoto  */
15418a6a6a38SKuninori Morimoto static const char * const dmi_blacklist[] = {
15428a6a6a38SKuninori Morimoto 	"To be filled by OEM",
15438a6a6a38SKuninori Morimoto 	"TBD by OEM",
15448a6a6a38SKuninori Morimoto 	"Default String",
15458a6a6a38SKuninori Morimoto 	"Board Manufacturer",
15468a6a6a38SKuninori Morimoto 	"Board Vendor Name",
15478a6a6a38SKuninori Morimoto 	"Board Product Name",
15488a6a6a38SKuninori Morimoto 	NULL,	/* terminator */
15498a6a6a38SKuninori Morimoto };
15508a6a6a38SKuninori Morimoto 
15518a6a6a38SKuninori Morimoto /*
15522c7b696aSMarcel Ziswiler  * Trim special characters, and replace '-' with '_' since '-' is used to
1553345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1554345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1555345233d7SLiam Girdwood  */
1556345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1557345233d7SLiam Girdwood {
1558345233d7SLiam Girdwood 	int i, j = 0;
1559345233d7SLiam Girdwood 
1560345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1561345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1562345233d7SLiam Girdwood 		    || (name[i] == '_'))
1563345233d7SLiam Girdwood 			name[j++] = name[i];
1564345233d7SLiam Girdwood 		else if (name[i] == '-')
1565345233d7SLiam Girdwood 			name[j++] = '_';
1566345233d7SLiam Girdwood 	}
1567345233d7SLiam Girdwood 
1568345233d7SLiam Girdwood 	name[j] = '\0';
1569345233d7SLiam Girdwood }
1570345233d7SLiam Girdwood 
15712c7b696aSMarcel Ziswiler /*
15722c7b696aSMarcel Ziswiler  * Check if a DMI field is valid, i.e. not containing any string
157398faf436SMengdong Lin  * in the black list.
157498faf436SMengdong Lin  */
157598faf436SMengdong Lin static int is_dmi_valid(const char *field)
157698faf436SMengdong Lin {
157798faf436SMengdong Lin 	int i = 0;
157898faf436SMengdong Lin 
157998faf436SMengdong Lin 	while (dmi_blacklist[i]) {
158098faf436SMengdong Lin 		if (strstr(field, dmi_blacklist[i]))
158198faf436SMengdong Lin 			return 0;
158298faf436SMengdong Lin 		i++;
158346b5a4d2SWu Fengguang 	}
158498faf436SMengdong Lin 
158598faf436SMengdong Lin 	return 1;
158698faf436SMengdong Lin }
158798faf436SMengdong Lin 
15884e01e5dbSJaroslav Kysela /*
15894e01e5dbSJaroslav Kysela  * Append a string to card->dmi_longname with character cleanups.
15904e01e5dbSJaroslav Kysela  */
15914e01e5dbSJaroslav Kysela static void append_dmi_string(struct snd_soc_card *card, const char *str)
15924e01e5dbSJaroslav Kysela {
15934e01e5dbSJaroslav Kysela 	char *dst = card->dmi_longname;
15944e01e5dbSJaroslav Kysela 	size_t dst_len = sizeof(card->dmi_longname);
15954e01e5dbSJaroslav Kysela 	size_t len;
15964e01e5dbSJaroslav Kysela 
15974e01e5dbSJaroslav Kysela 	len = strlen(dst);
15984e01e5dbSJaroslav Kysela 	snprintf(dst + len, dst_len - len, "-%s", str);
15994e01e5dbSJaroslav Kysela 
16004e01e5dbSJaroslav Kysela 	len++;	/* skip the separator "-" */
16014e01e5dbSJaroslav Kysela 	if (len < dst_len)
16024e01e5dbSJaroslav Kysela 		cleanup_dmi_name(dst + len);
16034e01e5dbSJaroslav Kysela }
16044e01e5dbSJaroslav Kysela 
1605345233d7SLiam Girdwood /**
1606345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
1607345233d7SLiam Girdwood  * @card: The card to register DMI names
1608345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
1609345233d7SLiam Girdwood  *
1610345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
1611345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
1612345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
1613345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
1614345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
1615345233d7SLiam Girdwood  * as the sound card long name, in the format of
1616345233d7SLiam Girdwood  * "vendor-product-version-board"
1617345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
1618345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
1619345233d7SLiam Girdwood  * (UCM) configurations for the card.
1620345233d7SLiam Girdwood  *
1621345233d7SLiam Girdwood  * Possible card long names may be:
1622345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
1623345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1624345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1625345233d7SLiam Girdwood  *
1626345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
1627345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
1628345233d7SLiam Girdwood  *
1629345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
1630345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
1631345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
1632345233d7SLiam Girdwood  * charactors like SPACE.
1633345233d7SLiam Girdwood  *
1634345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
1635345233d7SLiam Girdwood  */
1636345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1637345233d7SLiam Girdwood {
1638345233d7SLiam Girdwood 	const char *vendor, *product, *product_version, *board;
1639345233d7SLiam Girdwood 
1640345233d7SLiam Girdwood 	if (card->long_name)
1641345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
1642345233d7SLiam Girdwood 
16434e01e5dbSJaroslav Kysela 	/* make up dmi long name as: vendor-product-version-board */
1644345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
164598faf436SMengdong Lin 	if (!vendor || !is_dmi_valid(vendor)) {
1646345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1647345233d7SLiam Girdwood 		return 0;
1648345233d7SLiam Girdwood 	}
1649345233d7SLiam Girdwood 
16504e01e5dbSJaroslav Kysela 	snprintf(card->dmi_longname, sizeof(card->dmi_longname), "%s", vendor);
1651345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
1652345233d7SLiam Girdwood 
1653345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
165498faf436SMengdong Lin 	if (product && is_dmi_valid(product)) {
16554e01e5dbSJaroslav Kysela 		append_dmi_string(card, product);
1656345233d7SLiam Girdwood 
16572c7b696aSMarcel Ziswiler 		/*
16582c7b696aSMarcel Ziswiler 		 * some vendors like Lenovo may only put a self-explanatory
1659345233d7SLiam Girdwood 		 * name in the product version field
1660345233d7SLiam Girdwood 		 */
1661345233d7SLiam Girdwood 		product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
16624e01e5dbSJaroslav Kysela 		if (product_version && is_dmi_valid(product_version))
16634e01e5dbSJaroslav Kysela 			append_dmi_string(card, product_version);
1664345233d7SLiam Girdwood 	}
1665345233d7SLiam Girdwood 
1666345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
166798faf436SMengdong Lin 	if (board && is_dmi_valid(board)) {
166839870b0dSJaroslav Kysela 		if (!product || strcasecmp(board, product))
16694e01e5dbSJaroslav Kysela 			append_dmi_string(card, board);
1670345233d7SLiam Girdwood 	} else if (!product) {
1671345233d7SLiam Girdwood 		/* fall back to using legacy name */
1672345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1673345233d7SLiam Girdwood 		return 0;
1674345233d7SLiam Girdwood 	}
1675345233d7SLiam Girdwood 
1676345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
16774e01e5dbSJaroslav Kysela 	if (flavour)
16784e01e5dbSJaroslav Kysela 		append_dmi_string(card, flavour);
1679345233d7SLiam Girdwood 
1680345233d7SLiam Girdwood 	/* set the card long name */
1681345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
1682345233d7SLiam Girdwood 
1683345233d7SLiam Girdwood 	return 0;
1684345233d7SLiam Girdwood }
1685345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
16861f5a4535STakashi Iwai #endif /* CONFIG_DMI */
1687345233d7SLiam Girdwood 
1688a655de80SLiam Girdwood static void soc_check_tplg_fes(struct snd_soc_card *card)
1689a655de80SLiam Girdwood {
1690a655de80SLiam Girdwood 	struct snd_soc_component *component;
1691a655de80SLiam Girdwood 	const struct snd_soc_component_driver *comp_drv;
1692a655de80SLiam Girdwood 	struct snd_soc_dai_link *dai_link;
1693a655de80SLiam Girdwood 	int i;
1694a655de80SLiam Girdwood 
1695368dee94SKuninori Morimoto 	for_each_component(component) {
1696a655de80SLiam Girdwood 
169749f9c4f2SDaniel Baluta 		/* does this component override BEs ? */
1698a655de80SLiam Girdwood 		if (!component->driver->ignore_machine)
1699a655de80SLiam Girdwood 			continue;
1700a655de80SLiam Girdwood 
1701a655de80SLiam Girdwood 		/* for this machine ? */
1702e194098bSPierre-Louis Bossart 		if (!strcmp(component->driver->ignore_machine,
1703a655de80SLiam Girdwood 			    card->dev->driver->name))
1704e194098bSPierre-Louis Bossart 			goto match;
1705e194098bSPierre-Louis Bossart 		if (strcmp(component->driver->ignore_machine,
1706e194098bSPierre-Louis Bossart 			   dev_name(card->dev)))
1707a655de80SLiam Girdwood 			continue;
1708e194098bSPierre-Louis Bossart match:
1709a655de80SLiam Girdwood 		/* machine matches, so override the rtd data */
17107fe072b4SKuninori Morimoto 		for_each_card_prelinks(card, i, dai_link) {
1711a655de80SLiam Girdwood 
1712a655de80SLiam Girdwood 			/* ignore this FE */
1713a655de80SLiam Girdwood 			if (dai_link->dynamic) {
1714a655de80SLiam Girdwood 				dai_link->ignore = true;
1715a655de80SLiam Girdwood 				continue;
1716a655de80SLiam Girdwood 			}
1717a655de80SLiam Girdwood 
171849f9c4f2SDaniel Baluta 			dev_info(card->dev, "info: override BE DAI link %s\n",
1719a655de80SLiam Girdwood 				 card->dai_link[i].name);
1720a655de80SLiam Girdwood 
1721a655de80SLiam Girdwood 			/* override platform component */
1722adb76b5bSKuninori Morimoto 			if (!dai_link->platforms) {
1723daecf46eSKuninori Morimoto 				dev_err(card->dev, "init platform error");
1724daecf46eSKuninori Morimoto 				continue;
1725daecf46eSKuninori Morimoto 			}
1726910fdcabSKuninori Morimoto 			dai_link->platforms->name = component->name;
1727a655de80SLiam Girdwood 
1728a655de80SLiam Girdwood 			/* convert non BE into BE */
1729a655de80SLiam Girdwood 			dai_link->no_pcm = 1;
1730218fe9b7SDaniel Baluta 			dai_link->dpcm_playback = 1;
1731218fe9b7SDaniel Baluta 			dai_link->dpcm_capture = 1;
1732a655de80SLiam Girdwood 
1733a655de80SLiam Girdwood 			/* override any BE fixups */
1734a655de80SLiam Girdwood 			dai_link->be_hw_params_fixup =
1735a655de80SLiam Girdwood 				component->driver->be_hw_params_fixup;
1736a655de80SLiam Girdwood 
17372c7b696aSMarcel Ziswiler 			/*
17382c7b696aSMarcel Ziswiler 			 * most BE links don't set stream name, so set it to
1739a655de80SLiam Girdwood 			 * dai link name if it's NULL to help bind widgets.
1740a655de80SLiam Girdwood 			 */
1741a655de80SLiam Girdwood 			if (!dai_link->stream_name)
1742a655de80SLiam Girdwood 				dai_link->stream_name = dai_link->name;
1743a655de80SLiam Girdwood 		}
1744a655de80SLiam Girdwood 
1745a655de80SLiam Girdwood 		/* Inform userspace we are using alternate topology */
1746a655de80SLiam Girdwood 		if (component->driver->topology_name_prefix) {
1747a655de80SLiam Girdwood 
1748a655de80SLiam Girdwood 			/* topology shortname created? */
1749a655de80SLiam Girdwood 			if (!card->topology_shortname_created) {
1750a655de80SLiam Girdwood 				comp_drv = component->driver;
1751a655de80SLiam Girdwood 
1752a655de80SLiam Girdwood 				snprintf(card->topology_shortname, 32, "%s-%s",
1753a655de80SLiam Girdwood 					 comp_drv->topology_name_prefix,
1754a655de80SLiam Girdwood 					 card->name);
1755a655de80SLiam Girdwood 				card->topology_shortname_created = true;
1756a655de80SLiam Girdwood 			}
1757a655de80SLiam Girdwood 
1758a655de80SLiam Girdwood 			/* use topology shortname */
1759a655de80SLiam Girdwood 			card->name = card->topology_shortname;
1760a655de80SLiam Girdwood 		}
1761a655de80SLiam Girdwood 	}
1762a655de80SLiam Girdwood }
1763a655de80SLiam Girdwood 
17640f23f718SKuninori Morimoto #define soc_setup_card_name(name, name1, name2, norm)		\
17650f23f718SKuninori Morimoto 	__soc_setup_card_name(name, sizeof(name), name1, name2, norm)
17660f23f718SKuninori Morimoto static void __soc_setup_card_name(char *name, int len,
17670f23f718SKuninori Morimoto 				  const char *name1, const char *name2,
17680f23f718SKuninori Morimoto 				  int normalization)
17690f23f718SKuninori Morimoto {
17700f23f718SKuninori Morimoto 	int i;
17710f23f718SKuninori Morimoto 
17720f23f718SKuninori Morimoto 	snprintf(name, len, "%s", name1 ? name1 : name2);
17730f23f718SKuninori Morimoto 
17740f23f718SKuninori Morimoto 	if (!normalization)
17750f23f718SKuninori Morimoto 		return;
17760f23f718SKuninori Morimoto 
17770f23f718SKuninori Morimoto 	/*
17780f23f718SKuninori Morimoto 	 * Name normalization
17790f23f718SKuninori Morimoto 	 *
17800f23f718SKuninori Morimoto 	 * The driver name is somewhat special, as it's used as a key for
17810f23f718SKuninori Morimoto 	 * searches in the user-space.
17820f23f718SKuninori Morimoto 	 *
17830f23f718SKuninori Morimoto 	 * ex)
17840f23f718SKuninori Morimoto 	 *	"abcd??efg" -> "abcd__efg"
17850f23f718SKuninori Morimoto 	 */
17860f23f718SKuninori Morimoto 	for (i = 0; i < len; i++) {
17870f23f718SKuninori Morimoto 		switch (name[i]) {
17880f23f718SKuninori Morimoto 		case '_':
17890f23f718SKuninori Morimoto 		case '-':
17900f23f718SKuninori Morimoto 		case '\0':
17910f23f718SKuninori Morimoto 			break;
17920f23f718SKuninori Morimoto 		default:
17930f23f718SKuninori Morimoto 			if (!isalnum(name[i]))
17940f23f718SKuninori Morimoto 				name[i] = '_';
17950f23f718SKuninori Morimoto 			break;
17960f23f718SKuninori Morimoto 		}
17970f23f718SKuninori Morimoto 	}
17980f23f718SKuninori Morimoto }
17990f23f718SKuninori Morimoto 
1800ce21401cSKuninori Morimoto static void soc_cleanup_card_resources(struct snd_soc_card *card,
1801ce21401cSKuninori Morimoto 				       int card_probed)
180253e947a0SKuninori Morimoto {
1803cc733900SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd, *n;
18047ce6088fSKuninori Morimoto 
18050ced7b05SKuninori Morimoto 	if (card->snd_card)
18060ced7b05SKuninori Morimoto 		snd_card_disconnect_sync(card->snd_card);
180753e947a0SKuninori Morimoto 
18082a6f0892SKuninori Morimoto 	snd_soc_dapm_shutdown(card);
18092a6f0892SKuninori Morimoto 
181053e947a0SKuninori Morimoto 	/* remove and free each DAI */
18117ce6088fSKuninori Morimoto 	soc_remove_link_dais(card);
18120ced7b05SKuninori Morimoto 	soc_remove_link_components(card);
18137ce6088fSKuninori Morimoto 
1814cc733900SKuninori Morimoto 	for_each_card_rtds_safe(card, rtd, n)
181550cd9b53SKuninori Morimoto 		snd_soc_remove_pcm_runtime(card, rtd);
18167ce6088fSKuninori Morimoto 
181753e947a0SKuninori Morimoto 	/* remove auxiliary devices */
181853e947a0SKuninori Morimoto 	soc_remove_aux_devices(card);
1819e8fbd250SKuninori Morimoto 	soc_unbind_aux_dev(card);
182053e947a0SKuninori Morimoto 
182153e947a0SKuninori Morimoto 	snd_soc_dapm_free(&card->dapm);
182253e947a0SKuninori Morimoto 	soc_cleanup_card_debugfs(card);
182353e947a0SKuninori Morimoto 
182453e947a0SKuninori Morimoto 	/* remove the card */
1825ce21401cSKuninori Morimoto 	if (card_probed && card->remove)
182653e947a0SKuninori Morimoto 		card->remove(card);
18270ced7b05SKuninori Morimoto 
18280ced7b05SKuninori Morimoto 	if (card->snd_card) {
18290ced7b05SKuninori Morimoto 		snd_card_free(card->snd_card);
18300ced7b05SKuninori Morimoto 		card->snd_card = NULL;
18310ced7b05SKuninori Morimoto 	}
183253e947a0SKuninori Morimoto }
183353e947a0SKuninori Morimoto 
18342cc1afcfSKuninori Morimoto static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
18352cc1afcfSKuninori Morimoto {
18362cc1afcfSKuninori Morimoto 	if (card->instantiated) {
1837ce21401cSKuninori Morimoto 		int card_probed = 1;
1838ce21401cSKuninori Morimoto 
18392cc1afcfSKuninori Morimoto 		card->instantiated = false;
18402cc1afcfSKuninori Morimoto 		snd_soc_flush_all_delayed_work(card);
18412cc1afcfSKuninori Morimoto 
1842ce21401cSKuninori Morimoto 		soc_cleanup_card_resources(card, card_probed);
18432cc1afcfSKuninori Morimoto 		if (!unregister)
18442cc1afcfSKuninori Morimoto 			list_add(&card->list, &unbind_card_list);
18452cc1afcfSKuninori Morimoto 	} else {
18462cc1afcfSKuninori Morimoto 		if (unregister)
18472cc1afcfSKuninori Morimoto 			list_del(&card->list);
18482cc1afcfSKuninori Morimoto 	}
18492cc1afcfSKuninori Morimoto }
18502cc1afcfSKuninori Morimoto 
1851ed90c013SKuninori Morimoto static int snd_soc_bind_card(struct snd_soc_card *card)
1852f0fba2adSLiam Girdwood {
18531a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
185476c39e86SKuninori Morimoto 	struct snd_soc_component *component;
185561b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
1856ce21401cSKuninori Morimoto 	int ret, i, card_probed = 0;
185796dd3622SMark Brown 
185834e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
185901b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1860f0fba2adSLiam Girdwood 
186195c267ddSKuninori Morimoto 	snd_soc_dapm_init(&card->dapm, card, NULL);
186253e947a0SKuninori Morimoto 
1863a655de80SLiam Girdwood 	/* check whether any platform is ignore machine FE and using topology */
1864a655de80SLiam Girdwood 	soc_check_tplg_fes(card);
1865a655de80SLiam Girdwood 
186644c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
1867bee886f1SKuninori Morimoto 	ret = soc_bind_aux_dev(card);
1868bee886f1SKuninori Morimoto 	if (ret < 0)
186953e947a0SKuninori Morimoto 		goto probe_end;
1870db2a4165SFrank Mandarino 
1871f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
1872d8145989SKuninori Morimoto 	card->num_rtd = 0;
18735b99a0aaSKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link) {
18740c048004SKuninori Morimoto 		ret = snd_soc_add_pcm_runtime(card, dai_link);
18755b99a0aaSKuninori Morimoto 		if (ret < 0)
18765b99a0aaSKuninori Morimoto 			goto probe_end;
18775b99a0aaSKuninori Morimoto 	}
1878f8f80361SMengdong Lin 
1879f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1880102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1881f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1882f0fba2adSLiam Girdwood 	if (ret < 0) {
188310e8aa9aSMichał Mirosław 		dev_err(card->dev,
188410e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
188510e8aa9aSMichał Mirosław 			card->name, ret);
188653e947a0SKuninori Morimoto 		goto probe_end;
1887db2a4165SFrank Mandarino 	}
1888db2a4165SFrank Mandarino 
18890757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
18900757d834SLars-Peter Clausen 
1891b3da4251SKuninori Morimoto 	soc_resume_init(card);
18926ed25978SAndy Green 
1893b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
18949a841ebbSMark Brown 					card->num_dapm_widgets);
1895b8ba3b57SKuninori Morimoto 	if (ret < 0)
1896b8ba3b57SKuninori Morimoto 		goto probe_end;
18979a841ebbSMark Brown 
1898b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1899f23e860eSNicolin Chen 					card->num_of_dapm_widgets);
1900b8ba3b57SKuninori Morimoto 	if (ret < 0)
1901b8ba3b57SKuninori Morimoto 		goto probe_end;
1902f23e860eSNicolin Chen 
1903f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1904f0fba2adSLiam Girdwood 	if (card->probe) {
1905e7361ec4SMark Brown 		ret = card->probe(card);
1906f0fba2adSLiam Girdwood 		if (ret < 0)
190753e947a0SKuninori Morimoto 			goto probe_end;
1908ce21401cSKuninori Morimoto 		card_probed = 1;
1909f0fba2adSLiam Girdwood 	}
1910f0fba2adSLiam Girdwood 
191162ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
191262f07a6bSKuninori Morimoto 	ret = soc_probe_link_components(card);
191362ae68faSStephen Warren 	if (ret < 0) {
1914f110bfc7SLiam Girdwood 		dev_err(card->dev,
191562f07a6bSKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
191653e947a0SKuninori Morimoto 		goto probe_end;
191762ae68faSStephen Warren 	}
191862ae68faSStephen Warren 
1919f2ed6b07SMengdong Lin 	/* probe auxiliary components */
1920f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
192174bd3f92SKuninori Morimoto 	if (ret < 0) {
192274bd3f92SKuninori Morimoto 		dev_err(card->dev,
192374bd3f92SKuninori Morimoto 			"ASoC: failed to probe aux component %d\n", ret);
192453e947a0SKuninori Morimoto 		goto probe_end;
192574bd3f92SKuninori Morimoto 	}
1926f2ed6b07SMengdong Lin 
192762ae68faSStephen Warren 	/* probe all DAI links on this card */
1928c7e73774SKuninori Morimoto 	ret = soc_probe_link_dais(card);
1929fe3e78e0SMark Brown 	if (ret < 0) {
1930f110bfc7SLiam Girdwood 		dev_err(card->dev,
1931c7e73774SKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
193253e947a0SKuninori Morimoto 		goto probe_end;
1933fe3e78e0SMark Brown 	}
1934fe3e78e0SMark Brown 
1935626c2e57SKuninori Morimoto 	for_each_card_rtds(card, rtd) {
1936eaffeefbSKuninori Morimoto 		ret = soc_init_pcm_runtime(card, rtd);
1937626c2e57SKuninori Morimoto 		if (ret < 0)
1938626c2e57SKuninori Morimoto 			goto probe_end;
1939626c2e57SKuninori Morimoto 	}
1940c4b46982SKuninori Morimoto 
1941888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
1942b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
1943888df395SMark Brown 
19449b98c7c2SKuninori Morimoto 	ret = snd_soc_add_card_controls(card, card->controls,
19452c7b696aSMarcel Ziswiler 					card->num_controls);
19469b98c7c2SKuninori Morimoto 	if (ret < 0)
19479b98c7c2SKuninori Morimoto 		goto probe_end;
1948b7af1dafSMark Brown 
1949daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1950b8ad29deSMark Brown 				      card->num_dapm_routes);
1951*a22ae72bSPierre-Louis Bossart 	if (ret < 0) {
1952*a22ae72bSPierre-Louis Bossart 		if (card->disable_route_checks) {
1953*a22ae72bSPierre-Louis Bossart 			dev_info(card->dev,
1954*a22ae72bSPierre-Louis Bossart 				 "%s: disable_route_checks set, ignoring errors on add_routes\n",
1955*a22ae72bSPierre-Louis Bossart 				 __func__);
1956*a22ae72bSPierre-Louis Bossart 		} else {
1957*a22ae72bSPierre-Louis Bossart 			dev_err(card->dev,
1958*a22ae72bSPierre-Louis Bossart 				 "%s: snd_soc_dapm_add_routes failed: %d\n",
1959*a22ae72bSPierre-Louis Bossart 				 __func__, ret);
1960daa480bdSKuninori Morimoto 			goto probe_end;
1961*a22ae72bSPierre-Louis Bossart 		}
1962*a22ae72bSPierre-Louis Bossart 	}
1963b8ad29deSMark Brown 
1964daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
1965f23e860eSNicolin Chen 				      card->num_of_dapm_routes);
1966daa480bdSKuninori Morimoto 	if (ret < 0)
1967daa480bdSKuninori Morimoto 		goto probe_end;
196875d9ac46SMark Brown 
1969861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
1970861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
1971861886d3STakashi Iwai 
19720f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->shortname,
19730f23f718SKuninori Morimoto 			    card->name, NULL, 0);
19740f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->longname,
19750f23f718SKuninori Morimoto 			    card->long_name, card->name, 0);
19760f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->driver,
19770f23f718SKuninori Morimoto 			    card->driver_name, card->name, 1);
1978fe3e78e0SMark Brown 
1979dc73d73aSJaroslav Kysela 	if (card->components) {
1980dc73d73aSJaroslav Kysela 		/* the current implementation of snd_component_add() accepts */
1981dc73d73aSJaroslav Kysela 		/* multiple components in the string separated by space, */
1982dc73d73aSJaroslav Kysela 		/* but the string collision (identical string) check might */
1983dc73d73aSJaroslav Kysela 		/* not work correctly */
1984dc73d73aSJaroslav Kysela 		ret = snd_component_add(card->snd_card, card->components);
1985dc73d73aSJaroslav Kysela 		if (ret < 0) {
1986dc73d73aSJaroslav Kysela 			dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n",
1987dc73d73aSJaroslav Kysela 				card->name, ret);
1988dc73d73aSJaroslav Kysela 			goto probe_end;
1989dc73d73aSJaroslav Kysela 		}
1990dc73d73aSJaroslav Kysela 	}
1991dc73d73aSJaroslav Kysela 
199228e9ad92SMark Brown 	if (card->late_probe) {
199328e9ad92SMark Brown 		ret = card->late_probe(card);
199428e9ad92SMark Brown 		if (ret < 0) {
1995f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
199628e9ad92SMark Brown 				card->name, ret);
199753e947a0SKuninori Morimoto 			goto probe_end;
199828e9ad92SMark Brown 		}
199928e9ad92SMark Brown 	}
2000ce21401cSKuninori Morimoto 	card_probed = 1;
200128e9ad92SMark Brown 
2002824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
20038c193b8dSLars-Peter Clausen 
2004f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2005fe3e78e0SMark Brown 	if (ret < 0) {
2006f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2007f110bfc7SLiam Girdwood 				ret);
200853e947a0SKuninori Morimoto 		goto probe_end;
2009fe3e78e0SMark Brown 	}
2010fe3e78e0SMark Brown 
2011435c5e25SMark Brown 	card->instantiated = 1;
2012882eab6cSTzung-Bi Shih 	dapm_mark_endpoints_dirty(card);
20134f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2014b19e6e7bSMark Brown 
2015ed90c013SKuninori Morimoto 	/* deactivate pins to sleep state */
201676c39e86SKuninori Morimoto 	for_each_card_components(card, component)
201776c39e86SKuninori Morimoto 		if (!component->active)
201876c39e86SKuninori Morimoto 			pinctrl_pm_select_sleep_state(component->dev);
2019ed90c013SKuninori Morimoto 
202053e947a0SKuninori Morimoto probe_end:
202153e947a0SKuninori Morimoto 	if (ret < 0)
2022ce21401cSKuninori Morimoto 		soc_cleanup_card_resources(card, card_probed);
2023db2a4165SFrank Mandarino 
2024f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
202534e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2026db2a4165SFrank Mandarino 
2027b19e6e7bSMark Brown 	return ret;
2028435c5e25SMark Brown }
2029435c5e25SMark Brown 
2030435c5e25SMark Brown /* probes a new socdev */
2031435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2032435c5e25SMark Brown {
2033f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2034435c5e25SMark Brown 
203570a7ca34SVinod Koul 	/*
203670a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
203770a7ca34SVinod Koul 	 * we should not be here in that case so ret error
203870a7ca34SVinod Koul 	 */
203970a7ca34SVinod Koul 	if (!card)
204070a7ca34SVinod Koul 		return -EINVAL;
204170a7ca34SVinod Koul 
2042fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2043f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2044fe4085e8SMark Brown 		 card->name);
2045fe4085e8SMark Brown 
2046435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2047435c5e25SMark Brown 	card->dev = &pdev->dev;
2048f0fba2adSLiam Girdwood 
204928d528c8SMark Brown 	return snd_soc_register_card(card);
2050435c5e25SMark Brown }
2051435c5e25SMark Brown 
2052b0e26485SVinod Koul /* removes a socdev */
2053b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2054b0e26485SVinod Koul {
2055b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2056b0e26485SVinod Koul 
2057c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2058db2a4165SFrank Mandarino 	return 0;
2059db2a4165SFrank Mandarino }
2060db2a4165SFrank Mandarino 
20616f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
206251737470SMark Brown {
20636f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
206476c39e86SKuninori Morimoto 	struct snd_soc_component *component;
206551737470SMark Brown 
206651737470SMark Brown 	if (!card->instantiated)
2067416356fcSMark Brown 		return 0;
206851737470SMark Brown 
20692c7b696aSMarcel Ziswiler 	/*
20702c7b696aSMarcel Ziswiler 	 * Flush out pmdown_time work - we actually do want to run it
20712c7b696aSMarcel Ziswiler 	 * now, we're shutting down so no imminent restart.
20722c7b696aSMarcel Ziswiler 	 */
207365462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
207451737470SMark Brown 
2075f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2076416356fcSMark Brown 
2077988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
207876c39e86SKuninori Morimoto 	for_each_card_components(card, component)
207976c39e86SKuninori Morimoto 		pinctrl_pm_select_sleep_state(component->dev);
2080988e8cc4SNicolin Chen 
2081416356fcSMark Brown 	return 0;
208251737470SMark Brown }
20836f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
208451737470SMark Brown 
20856f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2086b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2087b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2088b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2089b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
20906f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2091b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2092416356fcSMark Brown };
2093deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2094416356fcSMark Brown 
2095db2a4165SFrank Mandarino /* ASoC platform driver */
2096db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2097db2a4165SFrank Mandarino 	.driver		= {
2098db2a4165SFrank Mandarino 		.name		= "soc-audio",
20996f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2100db2a4165SFrank Mandarino 	},
2101db2a4165SFrank Mandarino 	.probe		= soc_probe,
2102db2a4165SFrank Mandarino 	.remove		= soc_remove,
2103db2a4165SFrank Mandarino };
2104db2a4165SFrank Mandarino 
2105096e49d5SMark Brown /**
2106db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2107db2a4165SFrank Mandarino  * @_template: control template
2108db2a4165SFrank Mandarino  * @data: control private data
2109ac11a2b3SMark Brown  * @long_name: control long name
2110efb7ac3fSMark Brown  * @prefix: control name prefix
2111db2a4165SFrank Mandarino  *
2112db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2113db2a4165SFrank Mandarino  *
2114db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2115db2a4165SFrank Mandarino  */
2116db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
21173056557fSMark Brown 				  void *data, const char *long_name,
2118efb7ac3fSMark Brown 				  const char *prefix)
2119db2a4165SFrank Mandarino {
2120db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2121efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2122efb7ac3fSMark Brown 	char *name = NULL;
2123db2a4165SFrank Mandarino 
2124db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2125db2a4165SFrank Mandarino 	template.index = 0;
2126db2a4165SFrank Mandarino 
2127efb7ac3fSMark Brown 	if (!long_name)
2128efb7ac3fSMark Brown 		long_name = template.name;
2129efb7ac3fSMark Brown 
2130efb7ac3fSMark Brown 	if (prefix) {
21312b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2132efb7ac3fSMark Brown 		if (!name)
2133efb7ac3fSMark Brown 			return NULL;
2134efb7ac3fSMark Brown 
2135efb7ac3fSMark Brown 		template.name = name;
2136efb7ac3fSMark Brown 	} else {
2137efb7ac3fSMark Brown 		template.name = long_name;
2138efb7ac3fSMark Brown 	}
2139efb7ac3fSMark Brown 
2140efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2141efb7ac3fSMark Brown 
2142efb7ac3fSMark Brown 	kfree(name);
2143efb7ac3fSMark Brown 
2144efb7ac3fSMark Brown 	return kcontrol;
2145db2a4165SFrank Mandarino }
2146db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2147db2a4165SFrank Mandarino 
2148022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2149022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2150022658beSLiam Girdwood 	const char *prefix, void *data)
2151022658beSLiam Girdwood {
2152022658beSLiam Girdwood 	int err, i;
2153022658beSLiam Girdwood 
2154022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2155022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
21562c7b696aSMarcel Ziswiler 
2157022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2158022658beSLiam Girdwood 						     control->name, prefix));
2159022658beSLiam Girdwood 		if (err < 0) {
2160f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2161f110bfc7SLiam Girdwood 				control->name, err);
2162022658beSLiam Girdwood 			return err;
2163022658beSLiam Girdwood 		}
2164022658beSLiam Girdwood 	}
2165022658beSLiam Girdwood 
2166022658beSLiam Girdwood 	return 0;
2167022658beSLiam Girdwood }
2168022658beSLiam Girdwood 
21694fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
21704fefd698SDimitris Papastamos 					       const char *name)
21714fefd698SDimitris Papastamos {
21724fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
21734fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
21744fefd698SDimitris Papastamos 
21754fefd698SDimitris Papastamos 	if (unlikely(!name))
21764fefd698SDimitris Papastamos 		return NULL;
21774fefd698SDimitris Papastamos 
21784fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
21794fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
21804fefd698SDimitris Papastamos 			return kctl;
21814fefd698SDimitris Papastamos 	return NULL;
21824fefd698SDimitris Papastamos }
21834fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
21844fefd698SDimitris Papastamos 
2185db2a4165SFrank Mandarino /**
21860f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
21870f2780adSLars-Peter Clausen  *
21880f2780adSLars-Peter Clausen  * @component: Component to add controls to
21890f2780adSLars-Peter Clausen  * @controls: Array of controls to add
21900f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
21910f2780adSLars-Peter Clausen  *
21920f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
21930f2780adSLars-Peter Clausen  */
21940f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
21950f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
21960f2780adSLars-Peter Clausen {
21970f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
21980f2780adSLars-Peter Clausen 
21990f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
22000f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
22010f2780adSLars-Peter Clausen }
22020f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
22030f2780adSLars-Peter Clausen 
22040f2780adSLars-Peter Clausen /**
2205022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2206022658beSLiam Girdwood  * Convenience function to add a list of controls.
2207022658beSLiam Girdwood  *
2208022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2209022658beSLiam Girdwood  * @controls: array of controls to add
2210022658beSLiam Girdwood  * @num_controls: number of elements in the array
2211022658beSLiam Girdwood  *
2212022658beSLiam Girdwood  * Return 0 for success, else error.
2213022658beSLiam Girdwood  */
2214022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2215022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2216022658beSLiam Girdwood {
2217022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2218022658beSLiam Girdwood 
2219022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2220022658beSLiam Girdwood 			NULL, soc_card);
2221022658beSLiam Girdwood }
2222022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2223022658beSLiam Girdwood 
2224022658beSLiam Girdwood /**
2225022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2226022658beSLiam Girdwood  * Convienience function to add a list of controls.
2227022658beSLiam Girdwood  *
2228022658beSLiam Girdwood  * @dai: DAI to add controls to
2229022658beSLiam Girdwood  * @controls: array of controls to add
2230022658beSLiam Girdwood  * @num_controls: number of elements in the array
2231022658beSLiam Girdwood  *
2232022658beSLiam Girdwood  * Return 0 for success, else error.
2233022658beSLiam Girdwood  */
2234022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2235022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2236022658beSLiam Girdwood {
2237313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2238022658beSLiam Girdwood 
2239022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2240022658beSLiam Girdwood 			NULL, dai);
2241022658beSLiam Girdwood }
2242022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2243022658beSLiam Girdwood 
2244c5af3a2eSMark Brown /**
2245c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2246c5af3a2eSMark Brown  *
2247ac11a2b3SMark Brown  * @card: Card to register
2248c5af3a2eSMark Brown  *
2249c5af3a2eSMark Brown  */
225070a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2251c5af3a2eSMark Brown {
2252c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2253c5af3a2eSMark Brown 		return -EINVAL;
2254c5af3a2eSMark Brown 
2255ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2256ed77cc12SMark Brown 
2257b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->widgets);
2258b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->paths);
2259b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->dapm_list);
2260b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->aux_comp_list);
2261b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->component_dev_list);
2262b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->list);
22631a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
2264db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
22658a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2266b03bfaecSKuninori Morimoto 
2267c5af3a2eSMark Brown 	card->instantiated = 0;
2268f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
2269a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
227072b745e3SPeter Ujfalusi 	mutex_init(&card->pcm_mutex);
2271a9764869SKaiChieh Chuang 	spin_lock_init(&card->dpcm_lock);
2272c5af3a2eSMark Brown 
2273e894efefSSrinivas Kandagatla 	return snd_soc_bind_card(card);
2274c5af3a2eSMark Brown }
227570a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2276c5af3a2eSMark Brown 
2277c5af3a2eSMark Brown /**
2278c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2279c5af3a2eSMark Brown  *
2280ac11a2b3SMark Brown  * @card: Card to unregister
2281c5af3a2eSMark Brown  *
2282c5af3a2eSMark Brown  */
228370a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2284c5af3a2eSMark Brown {
2285b545542aSKuninori Morimoto 	mutex_lock(&client_mutex);
2286e894efefSSrinivas Kandagatla 	snd_soc_unbind_card(card, true);
2287b545542aSKuninori Morimoto 	mutex_unlock(&client_mutex);
2288f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2289c5af3a2eSMark Brown 
2290c5af3a2eSMark Brown 	return 0;
2291c5af3a2eSMark Brown }
229270a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2293c5af3a2eSMark Brown 
2294f0fba2adSLiam Girdwood /*
2295f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
2296f0fba2adSLiam Girdwood  * and sanitizing names.
2297f0fba2adSLiam Girdwood  */
22980b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2299f0fba2adSLiam Girdwood {
2300f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
2301f0fba2adSLiam Girdwood 	int id1, id2;
2302f0fba2adSLiam Girdwood 
2303f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
2304f0fba2adSLiam Girdwood 		return NULL;
2305f0fba2adSLiam Girdwood 
230658818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2307f0fba2adSLiam Girdwood 
2308f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
2309f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
2310f0fba2adSLiam Girdwood 	if (found) {
2311f0fba2adSLiam Girdwood 		/* get ID */
2312f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2313f0fba2adSLiam Girdwood 
2314f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
2315f0fba2adSLiam Girdwood 			if (*id == -1)
2316f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
2317f0fba2adSLiam Girdwood 		}
2318f0fba2adSLiam Girdwood 
2319f0fba2adSLiam Girdwood 	} else {
2320f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr" */
2321f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2322f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
2323f0fba2adSLiam Girdwood 
2324f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
232505899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
2326f0fba2adSLiam Girdwood 
2327f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
23282c7b696aSMarcel Ziswiler 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
23292c7b696aSMarcel Ziswiler 				 name);
233058818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2331f0fba2adSLiam Girdwood 		} else
2332f0fba2adSLiam Girdwood 			*id = 0;
2333f0fba2adSLiam Girdwood 	}
2334f0fba2adSLiam Girdwood 
233550014499SKuninori Morimoto 	return devm_kstrdup(dev, name, GFP_KERNEL);
2336f0fba2adSLiam Girdwood }
2337f0fba2adSLiam Girdwood 
2338f0fba2adSLiam Girdwood /*
2339f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
2340f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
2341f0fba2adSLiam Girdwood  */
2342f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
2343f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
2344f0fba2adSLiam Girdwood {
2345f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
234610e8aa9aSMichał Mirosław 		dev_err(dev,
234710e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
234810e8aa9aSMichał Mirosław 			dev_name(dev));
2349f0fba2adSLiam Girdwood 		return NULL;
2350f0fba2adSLiam Girdwood 	}
2351f0fba2adSLiam Girdwood 
235250014499SKuninori Morimoto 	return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2353f0fba2adSLiam Girdwood }
2354f0fba2adSLiam Girdwood 
2355ffdbca0bSKuninori Morimoto void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2356e11381f3SKuninori Morimoto {
2357e11381f3SKuninori Morimoto 	dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2358e11381f3SKuninori Morimoto 	list_del(&dai->list);
2359e11381f3SKuninori Morimoto }
2360ffdbca0bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2361e11381f3SKuninori Morimoto 
23627ca24386SKuninori Morimoto /**
23637ca24386SKuninori Morimoto  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
23647ca24386SKuninori Morimoto  *
23657ca24386SKuninori Morimoto  * @component: The component the DAIs are registered for
23667ca24386SKuninori Morimoto  * @dai_drv: DAI driver to use for the DAI
2367bc9a6655SRandy Dunlap  * @legacy_dai_naming: if %true, use legacy single-name format;
2368bc9a6655SRandy Dunlap  * 	if %false, use multiple-name format;
23697ca24386SKuninori Morimoto  *
23707ca24386SKuninori Morimoto  * Topology can use this API to register DAIs when probing a component.
23717ca24386SKuninori Morimoto  * These DAIs's widgets will be freed in the card cleanup and the DAIs
23727ca24386SKuninori Morimoto  * will be freed in the component cleanup.
23737ca24386SKuninori Morimoto  */
23747ca24386SKuninori Morimoto struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
23755e4fb372SMengdong Lin 					 struct snd_soc_dai_driver *dai_drv,
23765e4fb372SMengdong Lin 					 bool legacy_dai_naming)
23775e4fb372SMengdong Lin {
23785e4fb372SMengdong Lin 	struct device *dev = component->dev;
23795e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
23805e4fb372SMengdong Lin 
23815e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
23825e4fb372SMengdong Lin 
23837ca24386SKuninori Morimoto 	lockdep_assert_held(&client_mutex);
23847ca24386SKuninori Morimoto 
238550014499SKuninori Morimoto 	dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
23865e4fb372SMengdong Lin 	if (dai == NULL)
23875e4fb372SMengdong Lin 		return NULL;
23885e4fb372SMengdong Lin 
23895e4fb372SMengdong Lin 	/*
23905e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
23915e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
23925e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
23935e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
23945e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
23955e4fb372SMengdong Lin 	 * component-less anymore.
23965e4fb372SMengdong Lin 	 */
23975e4fb372SMengdong Lin 	if (legacy_dai_naming &&
23985e4fb372SMengdong Lin 	    (dai_drv->id == 0 || dai_drv->name == NULL)) {
23995e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
24005e4fb372SMengdong Lin 	} else {
24015e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
24025e4fb372SMengdong Lin 		if (dai_drv->id)
24035e4fb372SMengdong Lin 			dai->id = dai_drv->id;
24045e4fb372SMengdong Lin 		else
24055e4fb372SMengdong Lin 			dai->id = component->num_dai;
24065e4fb372SMengdong Lin 	}
240750014499SKuninori Morimoto 	if (!dai->name)
24085e4fb372SMengdong Lin 		return NULL;
24095e4fb372SMengdong Lin 
24105e4fb372SMengdong Lin 	dai->component = component;
24115e4fb372SMengdong Lin 	dai->dev = dev;
24125e4fb372SMengdong Lin 	dai->driver = dai_drv;
24135e4fb372SMengdong Lin 	if (!dai->driver->ops)
24145e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
24155e4fb372SMengdong Lin 
241615a0c645SKuninori Morimoto 	/* see for_each_component_dais */
241758bf4179SKuninori Morimoto 	list_add_tail(&dai->list, &component->dai_list);
24185e4fb372SMengdong Lin 	component->num_dai++;
24195e4fb372SMengdong Lin 
24205e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
24215e4fb372SMengdong Lin 	return dai;
24225e4fb372SMengdong Lin }
2423e11381f3SKuninori Morimoto 
24249115171aSMark Brown /**
24253f6674aeSKuninori Morimoto  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
24263f6674aeSKuninori Morimoto  *
24273f6674aeSKuninori Morimoto  * @component: The component for which the DAIs should be unregistered
24283f6674aeSKuninori Morimoto  */
24293f6674aeSKuninori Morimoto static void snd_soc_unregister_dais(struct snd_soc_component *component)
24303f6674aeSKuninori Morimoto {
24313f6674aeSKuninori Morimoto 	struct snd_soc_dai *dai, *_dai;
24323f6674aeSKuninori Morimoto 
2433e11381f3SKuninori Morimoto 	for_each_component_dais_safe(component, dai, _dai)
2434e11381f3SKuninori Morimoto 		snd_soc_unregister_dai(dai);
24353f6674aeSKuninori Morimoto }
24363f6674aeSKuninori Morimoto 
24373f6674aeSKuninori Morimoto /**
2438daf77373SKuninori Morimoto  * snd_soc_register_dais - Register a DAI with the ASoC core
2439daf77373SKuninori Morimoto  *
2440daf77373SKuninori Morimoto  * @component: The component the DAIs are registered for
2441daf77373SKuninori Morimoto  * @dai_drv: DAI driver to use for the DAIs
2442daf77373SKuninori Morimoto  * @count: Number of DAIs
2443daf77373SKuninori Morimoto  */
2444daf77373SKuninori Morimoto static int snd_soc_register_dais(struct snd_soc_component *component,
2445daf77373SKuninori Morimoto 				 struct snd_soc_dai_driver *dai_drv,
2446daf77373SKuninori Morimoto 				 size_t count)
2447daf77373SKuninori Morimoto {
2448daf77373SKuninori Morimoto 	struct snd_soc_dai *dai;
2449daf77373SKuninori Morimoto 	unsigned int i;
2450daf77373SKuninori Morimoto 	int ret;
2451daf77373SKuninori Morimoto 
2452daf77373SKuninori Morimoto 	for (i = 0; i < count; i++) {
245371cb85f5SKuninori Morimoto 		dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
2454daf77373SKuninori Morimoto 				  !component->driver->non_legacy_dai_naming);
2455daf77373SKuninori Morimoto 		if (dai == NULL) {
2456daf77373SKuninori Morimoto 			ret = -ENOMEM;
2457daf77373SKuninori Morimoto 			goto err;
2458daf77373SKuninori Morimoto 		}
2459daf77373SKuninori Morimoto 	}
2460daf77373SKuninori Morimoto 
2461daf77373SKuninori Morimoto 	return 0;
2462daf77373SKuninori Morimoto 
2463daf77373SKuninori Morimoto err:
2464daf77373SKuninori Morimoto 	snd_soc_unregister_dais(component);
2465daf77373SKuninori Morimoto 
2466daf77373SKuninori Morimoto 	return ret;
2467daf77373SKuninori Morimoto }
2468daf77373SKuninori Morimoto 
2469bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
2470bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
2471d191bd8dSKuninori Morimoto {
24728d92bb51SKuninori Morimoto 	INIT_LIST_HEAD(&component->dai_list);
2473495efdb0SKuninori Morimoto 	INIT_LIST_HEAD(&component->dobj_list);
2474495efdb0SKuninori Morimoto 	INIT_LIST_HEAD(&component->card_list);
24758d92bb51SKuninori Morimoto 	mutex_init(&component->io_mutex);
24768d92bb51SKuninori Morimoto 
2477bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
2478bb13109dSLars-Peter Clausen 	if (!component->name) {
2479bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
2480d191bd8dSKuninori Morimoto 		return -ENOMEM;
2481d191bd8dSKuninori Morimoto 	}
2482d191bd8dSKuninori Morimoto 
2483bb13109dSLars-Peter Clausen 	component->dev = dev;
2484bb13109dSLars-Peter Clausen 	component->driver = driver;
2485e2c330b9SLars-Peter Clausen 
2486bb13109dSLars-Peter Clausen 	return 0;
2487d191bd8dSKuninori Morimoto }
2488d191bd8dSKuninori Morimoto 
248920feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2490886f5692SLars-Peter Clausen {
2491886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
249220feb881SLars-Peter Clausen 
2493886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
2494886f5692SLars-Peter Clausen 	if (val_bytes > 0)
2495886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
2496886f5692SLars-Peter Clausen }
249720feb881SLars-Peter Clausen 
2498e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
2499e874bf5fSLars-Peter Clausen 
250020feb881SLars-Peter Clausen /**
25012c7b696aSMarcel Ziswiler  * snd_soc_component_init_regmap() - Initialize regmap instance for the
25022c7b696aSMarcel Ziswiler  *                                   component
250320feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
250420feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
250520feb881SLars-Peter Clausen  *
250620feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
250720feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
250820feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
250920feb881SLars-Peter Clausen  * before the first IO attempt of the component.
251020feb881SLars-Peter Clausen  */
251120feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
251220feb881SLars-Peter Clausen 	struct regmap *regmap)
251320feb881SLars-Peter Clausen {
251420feb881SLars-Peter Clausen 	component->regmap = regmap;
251520feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
2516886f5692SLars-Peter Clausen }
251720feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
251820feb881SLars-Peter Clausen 
251920feb881SLars-Peter Clausen /**
25202c7b696aSMarcel Ziswiler  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
25212c7b696aSMarcel Ziswiler  *                                   component
252220feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
252320feb881SLars-Peter Clausen  *
252420feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
252520feb881SLars-Peter Clausen  * removes the regmap instance from the component.
252620feb881SLars-Peter Clausen  *
252720feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
252820feb881SLars-Peter Clausen  * to initialize the regmap instance.
252920feb881SLars-Peter Clausen  */
253020feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
253120feb881SLars-Peter Clausen {
253220feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
253320feb881SLars-Peter Clausen 	component->regmap = NULL;
253420feb881SLars-Peter Clausen }
253520feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2536886f5692SLars-Peter Clausen 
2537e874bf5fSLars-Peter Clausen #endif
2538d191bd8dSKuninori Morimoto 
2539273d778eSKuninori Morimoto #define ENDIANNESS_MAP(name) \
2540273d778eSKuninori Morimoto 	(SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2541273d778eSKuninori Morimoto static u64 endianness_format_map[] = {
2542273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S16_),
2543273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U16_),
2544273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_),
2545273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_),
2546273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S32_),
2547273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U32_),
2548273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_3),
2549273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_3),
2550273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S20_3),
2551273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U20_3),
2552273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S18_3),
2553273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U18_3),
2554273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT_),
2555273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT64_),
2556273d778eSKuninori Morimoto 	ENDIANNESS_MAP(IEC958_SUBFRAME_),
2557273d778eSKuninori Morimoto };
2558273d778eSKuninori Morimoto 
2559273d778eSKuninori Morimoto /*
2560273d778eSKuninori Morimoto  * Fix up the DAI formats for endianness: codecs don't actually see
2561273d778eSKuninori Morimoto  * the endianness of the data but we're using the CPU format
2562273d778eSKuninori Morimoto  * definitions which do need to include endianness so we ensure that
2563273d778eSKuninori Morimoto  * codec DAIs always have both big and little endian variants set.
2564273d778eSKuninori Morimoto  */
2565273d778eSKuninori Morimoto static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2566273d778eSKuninori Morimoto {
2567273d778eSKuninori Morimoto 	int i;
2568273d778eSKuninori Morimoto 
2569273d778eSKuninori Morimoto 	for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2570273d778eSKuninori Morimoto 		if (stream->formats & endianness_format_map[i])
2571273d778eSKuninori Morimoto 			stream->formats |= endianness_format_map[i];
2572273d778eSKuninori Morimoto }
2573273d778eSKuninori Morimoto 
2574e894efefSSrinivas Kandagatla static void snd_soc_try_rebind_card(void)
2575e894efefSSrinivas Kandagatla {
2576e894efefSSrinivas Kandagatla 	struct snd_soc_card *card, *c;
2577e894efefSSrinivas Kandagatla 
2578b245d273SKuninori Morimoto 	list_for_each_entry_safe(card, c, &unbind_card_list, list)
2579e894efefSSrinivas Kandagatla 		if (!snd_soc_bind_card(card))
2580e894efefSSrinivas Kandagatla 			list_del(&card->list);
2581e894efefSSrinivas Kandagatla }
2582e894efefSSrinivas Kandagatla 
2583486c7978SKuninori Morimoto static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2584486c7978SKuninori Morimoto {
2585b18768f5SKuninori Morimoto 	struct snd_soc_card *card = component->card;
2586b18768f5SKuninori Morimoto 
2587486c7978SKuninori Morimoto 	snd_soc_unregister_dais(component);
2588b18768f5SKuninori Morimoto 
2589b18768f5SKuninori Morimoto 	if (card)
2590b18768f5SKuninori Morimoto 		snd_soc_unbind_card(card, false);
2591b18768f5SKuninori Morimoto 
2592b18768f5SKuninori Morimoto 	list_del(&component->list);
2593486c7978SKuninori Morimoto }
2594486c7978SKuninori Morimoto 
2595e0dac41bSKuninori Morimoto int snd_soc_add_component(struct device *dev,
2596e0dac41bSKuninori Morimoto 			struct snd_soc_component *component,
2597cf9e829eSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2598d191bd8dSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2599d191bd8dSKuninori Morimoto 			int num_dai)
2600d191bd8dSKuninori Morimoto {
2601bb13109dSLars-Peter Clausen 	int ret;
2602273d778eSKuninori Morimoto 	int i;
2603d191bd8dSKuninori Morimoto 
2604b18768f5SKuninori Morimoto 	mutex_lock(&client_mutex);
2605b18768f5SKuninori Morimoto 
2606cf9e829eSKuninori Morimoto 	ret = snd_soc_component_initialize(component, component_driver, dev);
2607bb13109dSLars-Peter Clausen 	if (ret)
2608bb13109dSLars-Peter Clausen 		goto err_free;
2609bb13109dSLars-Peter Clausen 
2610273d778eSKuninori Morimoto 	if (component_driver->endianness) {
2611273d778eSKuninori Morimoto 		for (i = 0; i < num_dai; i++) {
2612273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].playback);
2613273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].capture);
2614273d778eSKuninori Morimoto 		}
2615273d778eSKuninori Morimoto 	}
2616273d778eSKuninori Morimoto 
26170e7b25c6SKuninori Morimoto 	ret = snd_soc_register_dais(component, dai_drv, num_dai);
2618bb13109dSLars-Peter Clausen 	if (ret < 0) {
2619f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
2620bb13109dSLars-Peter Clausen 		goto err_cleanup;
2621bb13109dSLars-Peter Clausen 	}
2622bb13109dSLars-Peter Clausen 
2623b18768f5SKuninori Morimoto 	if (!component->driver->write && !component->driver->read) {
2624b18768f5SKuninori Morimoto 		if (!component->regmap)
2625b18768f5SKuninori Morimoto 			component->regmap = dev_get_regmap(component->dev,
2626b18768f5SKuninori Morimoto 							   NULL);
2627b18768f5SKuninori Morimoto 		if (component->regmap)
2628b18768f5SKuninori Morimoto 			snd_soc_component_setup_regmap(component);
2629b18768f5SKuninori Morimoto 	}
2630bb13109dSLars-Peter Clausen 
2631b18768f5SKuninori Morimoto 	/* see for_each_component */
2632b18768f5SKuninori Morimoto 	list_add(&component->list, &component_list);
2633bb13109dSLars-Peter Clausen 
2634bb13109dSLars-Peter Clausen err_cleanup:
2635b18768f5SKuninori Morimoto 	if (ret < 0)
2636486c7978SKuninori Morimoto 		snd_soc_del_component_unlocked(component);
2637bb13109dSLars-Peter Clausen err_free:
2638b18768f5SKuninori Morimoto 	mutex_unlock(&client_mutex);
2639b18768f5SKuninori Morimoto 
2640b18768f5SKuninori Morimoto 	if (ret == 0)
2641b18768f5SKuninori Morimoto 		snd_soc_try_rebind_card();
2642b18768f5SKuninori Morimoto 
2643bb13109dSLars-Peter Clausen 	return ret;
2644d191bd8dSKuninori Morimoto }
2645e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component);
2646e0dac41bSKuninori Morimoto 
2647e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev,
2648e0dac41bSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2649e0dac41bSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2650e0dac41bSKuninori Morimoto 			int num_dai)
2651e0dac41bSKuninori Morimoto {
2652e0dac41bSKuninori Morimoto 	struct snd_soc_component *component;
2653e0dac41bSKuninori Morimoto 
26547ecbd6a9SKuninori Morimoto 	component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
265508e61d03SKuninori Morimoto 	if (!component)
2656e0dac41bSKuninori Morimoto 		return -ENOMEM;
2657e0dac41bSKuninori Morimoto 
2658e0dac41bSKuninori Morimoto 	return snd_soc_add_component(dev, component, component_driver,
2659e0dac41bSKuninori Morimoto 				     dai_drv, num_dai);
2660e0dac41bSKuninori Morimoto }
2661d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
2662d191bd8dSKuninori Morimoto 
2663d191bd8dSKuninori Morimoto /**
26642eccea8cSKuninori Morimoto  * snd_soc_unregister_component - Unregister all related component
26652eccea8cSKuninori Morimoto  * from the ASoC core
2666d191bd8dSKuninori Morimoto  *
2667628536eaSJonathan Corbet  * @dev: The device to unregister
2668d191bd8dSKuninori Morimoto  */
26692eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
26702eccea8cSKuninori Morimoto {
2671ac6a4dd3SKuninori Morimoto 	struct snd_soc_component *component;
2672ac6a4dd3SKuninori Morimoto 
2673ac6a4dd3SKuninori Morimoto 	mutex_lock(&client_mutex);
2674ac6a4dd3SKuninori Morimoto 	while (1) {
267518dd66eaSKuninori Morimoto 		component = snd_soc_lookup_component_nolocked(dev, NULL);
2676ac6a4dd3SKuninori Morimoto 		if (!component)
2677ac6a4dd3SKuninori Morimoto 			break;
2678ac6a4dd3SKuninori Morimoto 
2679ac6a4dd3SKuninori Morimoto 		snd_soc_del_component_unlocked(component);
2680ac6a4dd3SKuninori Morimoto 	}
2681ac6a4dd3SKuninori Morimoto 	mutex_unlock(&client_mutex);
2682d191bd8dSKuninori Morimoto }
2683d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2684d191bd8dSKuninori Morimoto 
2685bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
2686b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2687bec4fa05SStephen Warren 			       const char *propname)
2688bec4fa05SStephen Warren {
2689b07609ceSKuninori Morimoto 	struct device_node *np;
2690bec4fa05SStephen Warren 	int ret;
2691bec4fa05SStephen Warren 
26927e07e7c0STushar Behera 	if (!card->dev) {
26937e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
26947e07e7c0STushar Behera 		return -EINVAL;
26957e07e7c0STushar Behera 	}
26967e07e7c0STushar Behera 
26977e07e7c0STushar Behera 	np = card->dev->of_node;
26987e07e7c0STushar Behera 
2699bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
2700bec4fa05SStephen Warren 	/*
2701bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
2702bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
2703bec4fa05SStephen Warren 	 * snd_soc_register_card.
2704bec4fa05SStephen Warren 	 */
2705bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
2706bec4fa05SStephen Warren 		dev_err(card->dev,
2707f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
2708bec4fa05SStephen Warren 			propname, ret);
2709bec4fa05SStephen Warren 		return ret;
2710bec4fa05SStephen Warren 	}
2711bec4fa05SStephen Warren 
2712bec4fa05SStephen Warren 	return 0;
2713bec4fa05SStephen Warren }
2714b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2715bec4fa05SStephen Warren 
27169a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
27179a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
27189a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
27199a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
27209a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
27219a6d4860SXiubo Li };
27229a6d4860SXiubo Li 
272321efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
27249a6d4860SXiubo Li 					  const char *propname)
27259a6d4860SXiubo Li {
272621efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
27279a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
27289a6d4860SXiubo Li 	const char *template, *wname;
27299a6d4860SXiubo Li 	int i, j, num_widgets, ret;
27309a6d4860SXiubo Li 
27319a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
27329a6d4860SXiubo Li 	if (num_widgets < 0) {
27339a6d4860SXiubo Li 		dev_err(card->dev,
27349a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
27359a6d4860SXiubo Li 		return -EINVAL;
27369a6d4860SXiubo Li 	}
27379a6d4860SXiubo Li 	if (num_widgets & 1) {
27389a6d4860SXiubo Li 		dev_err(card->dev,
27399a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
27409a6d4860SXiubo Li 		return -EINVAL;
27419a6d4860SXiubo Li 	}
27429a6d4860SXiubo Li 
27439a6d4860SXiubo Li 	num_widgets /= 2;
27449a6d4860SXiubo Li 	if (!num_widgets) {
27459a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
27469a6d4860SXiubo Li 			propname);
27479a6d4860SXiubo Li 		return -EINVAL;
27489a6d4860SXiubo Li 	}
27499a6d4860SXiubo Li 
27509a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
27519a6d4860SXiubo Li 			       GFP_KERNEL);
27529a6d4860SXiubo Li 	if (!widgets) {
27539a6d4860SXiubo Li 		dev_err(card->dev,
27549a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
27559a6d4860SXiubo Li 		return -ENOMEM;
27569a6d4860SXiubo Li 	}
27579a6d4860SXiubo Li 
27589a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
27599a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
27609a6d4860SXiubo Li 			2 * i, &template);
27619a6d4860SXiubo Li 		if (ret) {
27629a6d4860SXiubo Li 			dev_err(card->dev,
27639a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
27649a6d4860SXiubo Li 				propname, 2 * i, ret);
27659a6d4860SXiubo Li 			return -EINVAL;
27669a6d4860SXiubo Li 		}
27679a6d4860SXiubo Li 
27689a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
27699a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
27709a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
27719a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
27729a6d4860SXiubo Li 				break;
27739a6d4860SXiubo Li 			}
27749a6d4860SXiubo Li 		}
27759a6d4860SXiubo Li 
27769a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
27779a6d4860SXiubo Li 			dev_err(card->dev,
27789a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
27799a6d4860SXiubo Li 				template);
27809a6d4860SXiubo Li 			return -EINVAL;
27819a6d4860SXiubo Li 		}
27829a6d4860SXiubo Li 
27839a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
27849a6d4860SXiubo Li 						    (2 * i) + 1,
27859a6d4860SXiubo Li 						    &wname);
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) + 1, ret);
27909a6d4860SXiubo Li 			return -EINVAL;
27919a6d4860SXiubo Li 		}
27929a6d4860SXiubo Li 
27939a6d4860SXiubo Li 		widgets[i].name = wname;
27949a6d4860SXiubo Li 	}
27959a6d4860SXiubo Li 
2796f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
2797f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
27989a6d4860SXiubo Li 
27999a6d4860SXiubo Li 	return 0;
28009a6d4860SXiubo Li }
280121efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
28029a6d4860SXiubo Li 
2803cbdfab3bSJerome Brunet int snd_soc_of_get_slot_mask(struct device_node *np,
28046131084aSJyri Sarha 			     const char *prop_name,
28056131084aSJyri Sarha 			     unsigned int *mask)
28066131084aSJyri Sarha {
28076131084aSJyri Sarha 	u32 val;
28086c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
28096131084aSJyri Sarha 	int i;
28106131084aSJyri Sarha 
28116131084aSJyri Sarha 	if (!of_slot_mask)
28126131084aSJyri Sarha 		return 0;
28136131084aSJyri Sarha 	val /= sizeof(u32);
28146131084aSJyri Sarha 	for (i = 0; i < val; i++)
28156131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
28166131084aSJyri Sarha 			*mask |= (1 << i);
28176131084aSJyri Sarha 
28186131084aSJyri Sarha 	return val;
28196131084aSJyri Sarha }
2820cbdfab3bSJerome Brunet EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
28216131084aSJyri Sarha 
282289c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
28236131084aSJyri Sarha 			      unsigned int *tx_mask,
28246131084aSJyri Sarha 			      unsigned int *rx_mask,
282589c67857SXiubo Li 			      unsigned int *slots,
282689c67857SXiubo Li 			      unsigned int *slot_width)
282789c67857SXiubo Li {
282889c67857SXiubo Li 	u32 val;
282989c67857SXiubo Li 	int ret;
283089c67857SXiubo Li 
28316131084aSJyri Sarha 	if (tx_mask)
28326131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
28336131084aSJyri Sarha 	if (rx_mask)
28346131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
28356131084aSJyri Sarha 
283689c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
283789c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
283889c67857SXiubo Li 		if (ret)
283989c67857SXiubo Li 			return ret;
284089c67857SXiubo Li 
284189c67857SXiubo Li 		if (slots)
284289c67857SXiubo Li 			*slots = val;
284389c67857SXiubo Li 	}
284489c67857SXiubo Li 
284589c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
284689c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
284789c67857SXiubo Li 		if (ret)
284889c67857SXiubo Li 			return ret;
284989c67857SXiubo Li 
285089c67857SXiubo Li 		if (slot_width)
285189c67857SXiubo Li 			*slot_width = val;
285289c67857SXiubo Li 	}
285389c67857SXiubo Li 
285489c67857SXiubo Li 	return 0;
285589c67857SXiubo Li }
285689c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
285789c67857SXiubo Li 
28583b710356SKuninori Morimoto void snd_soc_of_parse_node_prefix(struct device_node *np,
28595e3cdaa2SKuninori Morimoto 				  struct snd_soc_codec_conf *codec_conf,
28605e3cdaa2SKuninori Morimoto 				  struct device_node *of_node,
28615e3cdaa2SKuninori Morimoto 				  const char *propname)
28625e3cdaa2SKuninori Morimoto {
28635e3cdaa2SKuninori Morimoto 	const char *str;
28645e3cdaa2SKuninori Morimoto 	int ret;
28655e3cdaa2SKuninori Morimoto 
28665e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
28675e3cdaa2SKuninori Morimoto 	if (ret < 0) {
28685e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
28695e3cdaa2SKuninori Morimoto 		return;
28705e3cdaa2SKuninori Morimoto 	}
28715e3cdaa2SKuninori Morimoto 
2872c13493a2SKuninori Morimoto 	codec_conf->dlc.of_node	= of_node;
28735e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
28745e3cdaa2SKuninori Morimoto }
28753b710356SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
28765e3cdaa2SKuninori Morimoto 
28772bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
2878a4a54dd5SStephen Warren 				   const char *propname)
2879a4a54dd5SStephen Warren {
28802bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
2881e3b1e6a1SMark Brown 	int num_routes;
2882a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
2883a4a54dd5SStephen Warren 	int i, ret;
2884a4a54dd5SStephen Warren 
2885a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
2886c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
288710e8aa9aSMichał Mirosław 		dev_err(card->dev,
288810e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
288910e8aa9aSMichał Mirosław 			propname);
2890a4a54dd5SStephen Warren 		return -EINVAL;
2891a4a54dd5SStephen Warren 	}
2892a4a54dd5SStephen Warren 	num_routes /= 2;
2893a4a54dd5SStephen Warren 	if (!num_routes) {
2894f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
2895a4a54dd5SStephen Warren 			propname);
2896a4a54dd5SStephen Warren 		return -EINVAL;
2897a4a54dd5SStephen Warren 	}
2898a4a54dd5SStephen Warren 
2899a86854d0SKees Cook 	routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
2900a4a54dd5SStephen Warren 			      GFP_KERNEL);
2901a4a54dd5SStephen Warren 	if (!routes) {
2902a4a54dd5SStephen Warren 		dev_err(card->dev,
2903f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
2904a4a54dd5SStephen Warren 		return -EINVAL;
2905a4a54dd5SStephen Warren 	}
2906a4a54dd5SStephen Warren 
2907a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
2908a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
2909e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
2910a4a54dd5SStephen Warren 		if (ret) {
2911c871bd0bSMark Brown 			dev_err(card->dev,
2912c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
2913c871bd0bSMark Brown 				propname, 2 * i, ret);
2914a4a54dd5SStephen Warren 			return -EINVAL;
2915a4a54dd5SStephen Warren 		}
2916a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
2917e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
2918a4a54dd5SStephen Warren 		if (ret) {
2919a4a54dd5SStephen Warren 			dev_err(card->dev,
2920c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
2921c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
2922a4a54dd5SStephen Warren 			return -EINVAL;
2923a4a54dd5SStephen Warren 		}
2924a4a54dd5SStephen Warren 	}
2925a4a54dd5SStephen Warren 
2926f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
2927f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
2928a4a54dd5SStephen Warren 
2929a4a54dd5SStephen Warren 	return 0;
2930a4a54dd5SStephen Warren }
29312bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
2932a4a54dd5SStephen Warren 
2933a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
2934389cb834SJyri Sarha 				     const char *prefix,
2935389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
2936389cb834SJyri Sarha 				     struct device_node **framemaster)
2937a7930ed4SKuninori Morimoto {
2938a7930ed4SKuninori Morimoto 	int ret, i;
2939a7930ed4SKuninori Morimoto 	char prop[128];
2940a7930ed4SKuninori Morimoto 	unsigned int format = 0;
2941a7930ed4SKuninori Morimoto 	int bit, frame;
2942a7930ed4SKuninori Morimoto 	const char *str;
2943a7930ed4SKuninori Morimoto 	struct {
2944a7930ed4SKuninori Morimoto 		char *name;
2945a7930ed4SKuninori Morimoto 		unsigned int val;
2946a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
2947a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
2948a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
2949a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
2950a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
2951a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
2952a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
2953a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
2954a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
2955a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
2956a7930ed4SKuninori Morimoto 	};
2957a7930ed4SKuninori Morimoto 
2958a7930ed4SKuninori Morimoto 	if (!prefix)
2959a7930ed4SKuninori Morimoto 		prefix = "";
2960a7930ed4SKuninori Morimoto 
2961a7930ed4SKuninori Morimoto 	/*
29625711c979SKuninori Morimoto 	 * check "dai-format = xxx"
29635711c979SKuninori Morimoto 	 * or    "[prefix]format = xxx"
2964a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
2965a7930ed4SKuninori Morimoto 	 */
29665711c979SKuninori Morimoto 	ret = of_property_read_string(np, "dai-format", &str);
29675711c979SKuninori Morimoto 	if (ret < 0) {
2968a7930ed4SKuninori Morimoto 		snprintf(prop, sizeof(prop), "%sformat", prefix);
2969a7930ed4SKuninori Morimoto 		ret = of_property_read_string(np, prop, &str);
29705711c979SKuninori Morimoto 	}
2971a7930ed4SKuninori Morimoto 	if (ret == 0) {
2972a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
2973a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
2974a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
2975a7930ed4SKuninori Morimoto 				break;
2976a7930ed4SKuninori Morimoto 			}
2977a7930ed4SKuninori Morimoto 		}
2978a7930ed4SKuninori Morimoto 	}
2979a7930ed4SKuninori Morimoto 
2980a7930ed4SKuninori Morimoto 	/*
29818c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
2982a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
2983a7930ed4SKuninori Morimoto 	 */
29848c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
298551930295SJulia Lawall 	if (of_property_read_bool(np, prop))
29868c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
29878c2d6a9fSKuninori Morimoto 	else
29888c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
2989a7930ed4SKuninori Morimoto 
2990a7930ed4SKuninori Morimoto 	/*
2991a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
2992a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
2993a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
2994a7930ed4SKuninori Morimoto 	 */
2995a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
2996a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
2997a7930ed4SKuninori Morimoto 
2998a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
2999a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3000a7930ed4SKuninori Morimoto 
3001a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3002a7930ed4SKuninori Morimoto 	case 0x11:
3003a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3004a7930ed4SKuninori Morimoto 		break;
3005a7930ed4SKuninori Morimoto 	case 0x10:
3006a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3007a7930ed4SKuninori Morimoto 		break;
3008a7930ed4SKuninori Morimoto 	case 0x01:
3009a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3010a7930ed4SKuninori Morimoto 		break;
3011a7930ed4SKuninori Morimoto 	default:
3012a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3013a7930ed4SKuninori Morimoto 		break;
3014a7930ed4SKuninori Morimoto 	}
3015a7930ed4SKuninori Morimoto 
3016a7930ed4SKuninori Morimoto 	/*
3017a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3018a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3019a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3020a7930ed4SKuninori Morimoto 	 */
3021a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3022a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3023389cb834SJyri Sarha 	if (bit && bitclkmaster)
3024389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3025a7930ed4SKuninori Morimoto 
3026a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3027a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3028389cb834SJyri Sarha 	if (frame && framemaster)
3029389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3030a7930ed4SKuninori Morimoto 
3031a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3032a7930ed4SKuninori Morimoto 	case 0x11:
3033a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3034a7930ed4SKuninori Morimoto 		break;
3035a7930ed4SKuninori Morimoto 	case 0x10:
3036a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3037a7930ed4SKuninori Morimoto 		break;
3038a7930ed4SKuninori Morimoto 	case 0x01:
3039a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3040a7930ed4SKuninori Morimoto 		break;
3041a7930ed4SKuninori Morimoto 	default:
3042a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3043a7930ed4SKuninori Morimoto 		break;
3044a7930ed4SKuninori Morimoto 	}
3045a7930ed4SKuninori Morimoto 
3046a7930ed4SKuninori Morimoto 	return format;
3047a7930ed4SKuninori Morimoto }
3048a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3049a7930ed4SKuninori Morimoto 
3050a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
3051a180e8b9SKuninori Morimoto {
305209d4cc03SKuninori Morimoto 	struct snd_soc_component *component;
3053c1e230f0SKuninori Morimoto 	struct snd_soc_dai_link_component dlc;
3054a180e8b9SKuninori Morimoto 	int ret;
3055a180e8b9SKuninori Morimoto 
3056c1e230f0SKuninori Morimoto 	dlc.of_node	= of_graph_get_port_parent(ep);
3057c1e230f0SKuninori Morimoto 	dlc.name	= NULL;
3058a180e8b9SKuninori Morimoto 	/*
3059a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
3060a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
3061a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
3062a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
3063a180e8b9SKuninori Morimoto 	 */
3064a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
3065a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
3066c1e230f0SKuninori Morimoto 	component = soc_find_component(&dlc);
30672c7b1704SKuninori Morimoto 	if (component)
30682c7b1704SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_id(component, ep);
3069a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
3070a180e8b9SKuninori Morimoto 
3071c1e230f0SKuninori Morimoto 	of_node_put(dlc.of_node);
3072c0a480d1STony Lindgren 
3073a180e8b9SKuninori Morimoto 	return ret;
3074a180e8b9SKuninori Morimoto }
3075a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3076a180e8b9SKuninori Morimoto 
30771ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args,
3078cb470087SKuninori Morimoto 				const char **dai_name)
3079cb470087SKuninori Morimoto {
3080cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
30813e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
308293b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3083cb470087SKuninori Morimoto 
3084cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3085368dee94SKuninori Morimoto 	for_each_component(pos) {
3086c0834440SKuninori Morimoto 		component_of_node = soc_component_to_node(pos);
30873e0aa8d8SJyri Sarha 
30883e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
3089cb470087SKuninori Morimoto 			continue;
3090cb470087SKuninori Morimoto 
3091a2a34175SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3092a2a34175SKuninori Morimoto 		if (ret == -ENOTSUPP) {
309358bf4179SKuninori Morimoto 			struct snd_soc_dai *dai;
30946833c452SKuninori Morimoto 			int id = -1;
30956833c452SKuninori Morimoto 
309693b0f3eeSJean-Francois Moine 			switch (args->args_count) {
30976833c452SKuninori Morimoto 			case 0:
30986833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
30996833c452SKuninori Morimoto 				break;
31006833c452SKuninori Morimoto 			case 1:
310193b0f3eeSJean-Francois Moine 				id = args->args[0];
31026833c452SKuninori Morimoto 				break;
31036833c452SKuninori Morimoto 			default:
31046833c452SKuninori Morimoto 				/* not supported */
3105cb470087SKuninori Morimoto 				break;
3106cb470087SKuninori Morimoto 			}
3107cb470087SKuninori Morimoto 
31086833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
31096833c452SKuninori Morimoto 				ret = -EINVAL;
31103dcba280SNicolin Chen 				continue;
31116833c452SKuninori Morimoto 			}
3112e41975edSXiubo Li 
3113e41975edSXiubo Li 			ret = 0;
3114e41975edSXiubo Li 
311558bf4179SKuninori Morimoto 			/* find target DAI */
311615a0c645SKuninori Morimoto 			for_each_component_dais(pos, dai) {
311758bf4179SKuninori Morimoto 				if (id == 0)
311858bf4179SKuninori Morimoto 					break;
311958bf4179SKuninori Morimoto 				id--;
312058bf4179SKuninori Morimoto 			}
312158bf4179SKuninori Morimoto 
312258bf4179SKuninori Morimoto 			*dai_name = dai->driver->name;
3123e41975edSXiubo Li 			if (!*dai_name)
3124e41975edSXiubo Li 				*dai_name = pos->name;
31256833c452SKuninori Morimoto 		}
31266833c452SKuninori Morimoto 
3127cb470087SKuninori Morimoto 		break;
3128cb470087SKuninori Morimoto 	}
3129cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
313093b0f3eeSJean-Francois Moine 	return ret;
313193b0f3eeSJean-Francois Moine }
31321ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
313393b0f3eeSJean-Francois Moine 
313493b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
313593b0f3eeSJean-Francois Moine 			    const char **dai_name)
313693b0f3eeSJean-Francois Moine {
313793b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
313893b0f3eeSJean-Francois Moine 	int ret;
313993b0f3eeSJean-Francois Moine 
314093b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
314193b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
314293b0f3eeSJean-Francois Moine 	if (ret)
314393b0f3eeSJean-Francois Moine 		return ret;
314493b0f3eeSJean-Francois Moine 
314593b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3146cb470087SKuninori Morimoto 
3147cb470087SKuninori Morimoto 	of_node_put(args.np);
3148cb470087SKuninori Morimoto 
3149cb470087SKuninori Morimoto 	return ret;
3150cb470087SKuninori Morimoto }
3151cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3152cb470087SKuninori Morimoto 
315393b0f3eeSJean-Francois Moine /*
315494685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
315594685763SSylwester Nawrocki  * @dai_link: DAI link
315694685763SSylwester Nawrocki  *
315794685763SSylwester Nawrocki  * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
315894685763SSylwester Nawrocki  */
315994685763SSylwester Nawrocki void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
316094685763SSylwester Nawrocki {
31613db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *component;
316294685763SSylwester Nawrocki 	int index;
316394685763SSylwester Nawrocki 
31643db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
316594685763SSylwester Nawrocki 		if (!component->of_node)
316694685763SSylwester Nawrocki 			break;
316794685763SSylwester Nawrocki 		of_node_put(component->of_node);
316894685763SSylwester Nawrocki 		component->of_node = NULL;
316994685763SSylwester Nawrocki 	}
317094685763SSylwester Nawrocki }
317194685763SSylwester Nawrocki EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
317294685763SSylwester Nawrocki 
317394685763SSylwester Nawrocki /*
317493b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
317593b0f3eeSJean-Francois Moine  * @dev: Card device
317693b0f3eeSJean-Francois Moine  * @of_node: Device node
317793b0f3eeSJean-Francois Moine  * @dai_link: DAI link
317893b0f3eeSJean-Francois Moine  *
317993b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
318093b0f3eeSJean-Francois Moine  * 'sound-dai'.
318193b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
318294685763SSylwester Nawrocki  * The device nodes in the array (of_node) must be dereferenced by calling
318394685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs() on @dai_link.
318493b0f3eeSJean-Francois Moine  *
318593b0f3eeSJean-Francois Moine  * Returns 0 for success
318693b0f3eeSJean-Francois Moine  */
318793b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
318893b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
318993b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
319093b0f3eeSJean-Francois Moine {
319193b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
319293b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
319393b0f3eeSJean-Francois Moine 	char *name;
319493b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
319593b0f3eeSJean-Francois Moine 
319693b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
319793b0f3eeSJean-Francois Moine 	name = "sound-dai";
319893b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
319993b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
320093b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
320193b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
320293b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
320393b0f3eeSJean-Francois Moine 		else
320493b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
320593b0f3eeSJean-Francois Moine 		return num_codecs;
320693b0f3eeSJean-Francois Moine 	}
3207a86854d0SKees Cook 	component = devm_kcalloc(dev,
3208a86854d0SKees Cook 				 num_codecs, sizeof(*component),
320993b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
321093b0f3eeSJean-Francois Moine 	if (!component)
321193b0f3eeSJean-Francois Moine 		return -ENOMEM;
321293b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
321393b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
321493b0f3eeSJean-Francois Moine 
321593b0f3eeSJean-Francois Moine 	/* Parse the list */
32163db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
321793b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
321893b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
321993b0f3eeSJean-Francois Moine 						 index, &args);
322093b0f3eeSJean-Francois Moine 		if (ret)
322193b0f3eeSJean-Francois Moine 			goto err;
322293b0f3eeSJean-Francois Moine 		component->of_node = args.np;
322393b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
322493b0f3eeSJean-Francois Moine 		if (ret < 0)
322593b0f3eeSJean-Francois Moine 			goto err;
322693b0f3eeSJean-Francois Moine 	}
322793b0f3eeSJean-Francois Moine 	return 0;
322893b0f3eeSJean-Francois Moine err:
322994685763SSylwester Nawrocki 	snd_soc_of_put_dai_link_codecs(dai_link);
323093b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
323193b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
323293b0f3eeSJean-Francois Moine 	return ret;
323393b0f3eeSJean-Francois Moine }
323493b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
323593b0f3eeSJean-Francois Moine 
3236c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3237db2a4165SFrank Mandarino {
32386553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
3239fb257897SMark Brown 	snd_soc_util_init();
3240fb257897SMark Brown 
3241db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3242db2a4165SFrank Mandarino }
32434abe8e16SMark Brown module_init(snd_soc_init);
3244db2a4165SFrank Mandarino 
32457d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3246db2a4165SFrank Mandarino {
3247fb257897SMark Brown 	snd_soc_util_exit();
32486553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3249fb257897SMark Brown 
3250db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3251db2a4165SFrank Mandarino }
3252db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3253db2a4165SFrank Mandarino 
3254db2a4165SFrank Mandarino /* Module information */
3255d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3256db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3257db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
32588b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3259