xref: /openbmc/linux/sound/soc/soc-core.c (revision b2acc24c805a42b6ebfe8b8ae09dbf9c994c5604)
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;
3680f6011fdSKuninori Morimoto 	int playback = SNDRV_PCM_STREAM_PLAYBACK;
36983f94a2eSKuninori Morimoto 
37083f94a2eSKuninori Morimoto 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
37183f94a2eSKuninori Morimoto 
37283f94a2eSKuninori Morimoto 	dev_dbg(rtd->dev,
37383f94a2eSKuninori Morimoto 		"ASoC: pop wq checking: %s status: %s waiting: %s\n",
37483f94a2eSKuninori Morimoto 		codec_dai->driver->playback.stream_name,
3750f6011fdSKuninori Morimoto 		codec_dai->stream_active[playback] ? "active" : "inactive",
37683f94a2eSKuninori Morimoto 		rtd->pop_wait ? "yes" : "no");
37783f94a2eSKuninori Morimoto 
37883f94a2eSKuninori Morimoto 	/* are we waiting on this codec DAI stream */
37983f94a2eSKuninori Morimoto 	if (rtd->pop_wait == 1) {
38083f94a2eSKuninori Morimoto 		rtd->pop_wait = 0;
3810f6011fdSKuninori Morimoto 		snd_soc_dapm_stream_event(rtd, playback,
38283f94a2eSKuninori Morimoto 					  SND_SOC_DAPM_STREAM_STOP);
38383f94a2eSKuninori Morimoto 	}
38483f94a2eSKuninori Morimoto 
38583f94a2eSKuninori Morimoto 	mutex_unlock(&rtd->card->pcm_mutex);
38683f94a2eSKuninori Morimoto }
38783f94a2eSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_close_delayed_work);
38883f94a2eSKuninori Morimoto 
3896e864344SKuninori Morimoto static void soc_release_rtd_dev(struct device *dev)
3906e864344SKuninori Morimoto {
3916e864344SKuninori Morimoto 	/* "dev" means "rtd->dev" */
3926e864344SKuninori Morimoto 	kfree(dev);
3936e864344SKuninori Morimoto }
3946e864344SKuninori Morimoto 
3951c93a9e0SKuninori Morimoto static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
3961c93a9e0SKuninori Morimoto {
3976e864344SKuninori Morimoto 	if (!rtd)
3986e864344SKuninori Morimoto 		return;
3996e864344SKuninori Morimoto 
400753ace0aSKuninori Morimoto 	list_del(&rtd->list);
401b7c5bc45SKuninori Morimoto 
4029c9b6520SCurtis Malainey 	if (delayed_work_pending(&rtd->delayed_work))
4030ced7b05SKuninori Morimoto 		flush_delayed_work(&rtd->delayed_work);
4040ced7b05SKuninori Morimoto 	snd_soc_pcm_component_free(rtd);
4050ced7b05SKuninori Morimoto 
406b7c5bc45SKuninori Morimoto 	/*
407b7c5bc45SKuninori Morimoto 	 * we don't need to call kfree() for rtd->dev
408b7c5bc45SKuninori Morimoto 	 * see
409b7c5bc45SKuninori Morimoto 	 *	soc_release_rtd_dev()
410d918a376SKuninori Morimoto 	 *
411d918a376SKuninori Morimoto 	 * We don't need rtd->dev NULL check, because
412d918a376SKuninori Morimoto 	 * it is alloced *before* rtd.
413d918a376SKuninori Morimoto 	 * see
414d918a376SKuninori Morimoto 	 *	soc_new_pcm_runtime()
415b7c5bc45SKuninori Morimoto 	 */
416b7c5bc45SKuninori Morimoto 	device_unregister(rtd->dev);
4171c93a9e0SKuninori Morimoto }
4181c93a9e0SKuninori Morimoto 
4194bf2e385SCurtis Malainey static void close_delayed_work(struct work_struct *work) {
4204bf2e385SCurtis Malainey 	struct snd_soc_pcm_runtime *rtd =
4214bf2e385SCurtis Malainey 			container_of(work, struct snd_soc_pcm_runtime,
4224bf2e385SCurtis Malainey 				     delayed_work.work);
4234bf2e385SCurtis Malainey 
4244bf2e385SCurtis Malainey 	if (rtd->close_delayed_work_func)
4254bf2e385SCurtis Malainey 		rtd->close_delayed_work_func(rtd);
4264bf2e385SCurtis Malainey }
4274bf2e385SCurtis Malainey 
4281a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
4291a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
4301a497983SMengdong Lin {
4311a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
432613fb500SKuninori Morimoto 	struct snd_soc_component *component;
433d918a376SKuninori Morimoto 	struct device *dev;
4346e864344SKuninori Morimoto 	int ret;
435d74c2a15SKuninori Morimoto 	int stream;
4361a497983SMengdong Lin 
4376e864344SKuninori Morimoto 	/*
438d918a376SKuninori Morimoto 	 * for rtd->dev
439d918a376SKuninori Morimoto 	 */
440d918a376SKuninori Morimoto 	dev = kzalloc(sizeof(struct device), GFP_KERNEL);
441d918a376SKuninori Morimoto 	if (!dev)
442d918a376SKuninori Morimoto 		return NULL;
443d918a376SKuninori Morimoto 
444d918a376SKuninori Morimoto 	dev->parent	= card->dev;
445d918a376SKuninori Morimoto 	dev->release	= soc_release_rtd_dev;
446d918a376SKuninori Morimoto 	dev->groups	= soc_dev_attr_groups;
447d918a376SKuninori Morimoto 
448d918a376SKuninori Morimoto 	dev_set_name(dev, "%s", dai_link->name);
449d918a376SKuninori Morimoto 
450d918a376SKuninori Morimoto 	ret = device_register(dev);
451d918a376SKuninori Morimoto 	if (ret < 0) {
452d918a376SKuninori Morimoto 		put_device(dev); /* soc_release_rtd_dev */
453d918a376SKuninori Morimoto 		return NULL;
454d918a376SKuninori Morimoto 	}
455d918a376SKuninori Morimoto 
456d918a376SKuninori Morimoto 	/*
4576e864344SKuninori Morimoto 	 * for rtd
4586e864344SKuninori Morimoto 	 */
459613fb500SKuninori Morimoto 	rtd = devm_kzalloc(dev,
460613fb500SKuninori Morimoto 			   sizeof(*rtd) +
461613fb500SKuninori Morimoto 			   sizeof(*component) * (dai_link->num_cpus +
462613fb500SKuninori Morimoto 						 dai_link->num_codecs +
463613fb500SKuninori Morimoto 						 dai_link->num_platforms),
464613fb500SKuninori Morimoto 			   GFP_KERNEL);
4651a497983SMengdong Lin 	if (!rtd)
4666e864344SKuninori Morimoto 		goto free_rtd;
4671a497983SMengdong Lin 
468d918a376SKuninori Morimoto 	rtd->dev = dev;
46907d22a9bSTakashi Iwai 	INIT_LIST_HEAD(&rtd->list);
470d74c2a15SKuninori Morimoto 	for_each_pcm_streams(stream) {
471d74c2a15SKuninori Morimoto 		INIT_LIST_HEAD(&rtd->dpcm[stream].be_clients);
472d74c2a15SKuninori Morimoto 		INIT_LIST_HEAD(&rtd->dpcm[stream].fe_clients);
473d74c2a15SKuninori Morimoto 	}
474d918a376SKuninori Morimoto 	dev_set_drvdata(dev, rtd);
4754bf2e385SCurtis Malainey 	INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
476d918a376SKuninori Morimoto 
4776e864344SKuninori Morimoto 	/*
47822a2fc81SKuninori Morimoto 	 * for rtd->dais
4796e864344SKuninori Morimoto 	 */
48022a2fc81SKuninori Morimoto 	rtd->dais = devm_kcalloc(dev, dai_link->num_cpus + dai_link->num_codecs,
4816396bb22SKees Cook 					sizeof(struct snd_soc_dai *),
4821a497983SMengdong Lin 					GFP_KERNEL);
48322a2fc81SKuninori Morimoto 	if (!rtd->dais)
4846e864344SKuninori Morimoto 		goto free_rtd;
4856e864344SKuninori Morimoto 
4866e864344SKuninori Morimoto 	/*
48722a2fc81SKuninori Morimoto 	 * dais = [][][][][][][][][][][][][][][][][][]
48822a2fc81SKuninori Morimoto 	 *	  ^cpu_dais         ^codec_dais
48922a2fc81SKuninori Morimoto 	 *	  |--- num_cpus ---|--- num_codecs --|
49076afa643SShreyas NC 	 */
49122a2fc81SKuninori Morimoto 	rtd->cpu_dais	= &rtd->dais[0];
49222a2fc81SKuninori Morimoto 	rtd->codec_dais	= &rtd->dais[dai_link->num_cpus];
49322a2fc81SKuninori Morimoto 
49476afa643SShreyas NC 	/*
4956e864344SKuninori Morimoto 	 * rtd remaining settings
4966e864344SKuninori Morimoto 	 */
497929deb84SKuninori Morimoto 	rtd->card = card;
498929deb84SKuninori Morimoto 	rtd->dai_link = dai_link;
499929deb84SKuninori Morimoto 
5006634e3d6SKuninori Morimoto 	/* see for_each_card_rtds */
5011a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
5021a497983SMengdong Lin 	rtd->num = card->num_rtd;
5031a497983SMengdong Lin 	card->num_rtd++;
504a848125eSKuninori Morimoto 
505a848125eSKuninori Morimoto 	return rtd;
5066e864344SKuninori Morimoto 
5076e864344SKuninori Morimoto free_rtd:
5086e864344SKuninori Morimoto 	soc_free_pcm_runtime(rtd);
5096e864344SKuninori Morimoto 	return NULL;
5101a497983SMengdong Lin }
5111a497983SMengdong Lin 
51265462e44SKuninori Morimoto static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
51365462e44SKuninori Morimoto {
51465462e44SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
51565462e44SKuninori Morimoto 
51665462e44SKuninori Morimoto 	for_each_card_rtds(card, rtd)
51765462e44SKuninori Morimoto 		flush_delayed_work(&rtd->delayed_work);
51865462e44SKuninori Morimoto }
51965462e44SKuninori Morimoto 
5206f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
521db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5226f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
523db2a4165SFrank Mandarino {
5246f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
525d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
5261a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
5270f6011fdSKuninori Morimoto 	int playback = SNDRV_PCM_STREAM_PLAYBACK;
5281a497983SMengdong Lin 	int i;
529db2a4165SFrank Mandarino 
530c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
531c5599b87SLars-Peter Clausen 	if (!card->instantiated)
532e3509ff0SDaniel Mack 		return 0;
533e3509ff0SDaniel Mack 
5342c7b696aSMarcel Ziswiler 	/*
5352c7b696aSMarcel Ziswiler 	 * Due to the resume being scheduled into a workqueue we could
5366ed25978SAndy Green 	 * suspend before that's finished - wait for it to complete.
5376ed25978SAndy Green 	 */
538f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
5396ed25978SAndy Green 
5406ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
541f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5426ed25978SAndy Green 
543a00f90f9SMark Brown 	/* mute any active DACs */
544bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5450b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
5463efab7dcSMark Brown 
5471a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5483efab7dcSMark Brown 			continue;
5493efab7dcSMark Brown 
550a4be4187SKuninori Morimoto 		for_each_rtd_codec_dais(rtd, i, dai) {
5510f6011fdSKuninori Morimoto 			if (dai->stream_active[playback])
5520f6011fdSKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 1, playback);
553db2a4165SFrank Mandarino 		}
55488bd870fSBenoit Cousson 	}
555db2a4165SFrank Mandarino 
5564ccab3e7SLiam Girdwood 	/* suspend all pcms */
557bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5581a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5593efab7dcSMark Brown 			continue;
5603efab7dcSMark Brown 
5611a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
5623efab7dcSMark Brown 	}
5634ccab3e7SLiam Girdwood 
56487506549SMark Brown 	if (card->suspend_pre)
56570b2ac12SMark Brown 		card->suspend_pre(card);
566db2a4165SFrank Mandarino 
56737660b6dSLars-Peter Clausen 	/* close any waiting streams */
56865462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
569db2a4165SFrank Mandarino 
570bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
571d74c2a15SKuninori Morimoto 		int stream;
5723efab7dcSMark Brown 
5731a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5743efab7dcSMark Brown 			continue;
5753efab7dcSMark Brown 
576d74c2a15SKuninori Morimoto 		for_each_pcm_streams(stream)
577d74c2a15SKuninori Morimoto 			snd_soc_dapm_stream_event(rtd, stream,
578db2a4165SFrank Mandarino 						  SND_SOC_DAPM_STREAM_SUSPEND);
579db2a4165SFrank Mandarino 	}
580db2a4165SFrank Mandarino 
5818be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
5828be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
583e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
584e2d32ff6SMark Brown 
5859178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
5861272063aSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5871272063aSKuninori Morimoto 
5881272063aSKuninori Morimoto 		if (rtd->dai_link->ignore_suspend)
5891272063aSKuninori Morimoto 			continue;
5901272063aSKuninori Morimoto 
5911272063aSKuninori Morimoto 		for_each_rtd_components(rtd, i, component) {
5922c7b696aSMarcel Ziswiler 			struct snd_soc_dapm_context *dapm =
5932c7b696aSMarcel Ziswiler 				snd_soc_component_get_dapm(component);
594d9fc4063SKuninori Morimoto 
5952c7b696aSMarcel Ziswiler 			/*
5961272063aSKuninori Morimoto 			 * ignore if component was already suspended
5972c7b696aSMarcel Ziswiler 			 */
5981272063aSKuninori Morimoto 			if (snd_soc_component_is_suspended(component))
5991272063aSKuninori Morimoto 				continue;
6001272063aSKuninori Morimoto 
6011272063aSKuninori Morimoto 			/*
6021272063aSKuninori Morimoto 			 * If there are paths active then the COMPONENT will be
6031272063aSKuninori Morimoto 			 * held with bias _ON and should not be suspended.
6041272063aSKuninori Morimoto 			 */
6054890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
6061547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
607125a25daSMark Brown 				/*
6089178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
609125a25daSMark Brown 				 * bias off then being in STANDBY
610125a25daSMark Brown 				 * means it's doing something,
611125a25daSMark Brown 				 * otherwise fall through.
612125a25daSMark Brown 				 */
6134890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
6149178feb4SKuninori Morimoto 					dev_dbg(component->dev,
61510e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
616125a25daSMark Brown 					break;
617125a25daSMark Brown 				}
6181a12d5dcSGustavo A. R. Silva 				/* fall through */
619a8093297SLars-Peter Clausen 
6201547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
62166c51573SKuninori Morimoto 				snd_soc_component_suspend(component);
6229178feb4SKuninori Morimoto 				if (component->regmap)
6239178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
624988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
6259178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
6261547aba9SMark Brown 				break;
6271547aba9SMark Brown 			default:
6289178feb4SKuninori Morimoto 				dev_dbg(component->dev,
6299178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
6301547aba9SMark Brown 				break;
6311547aba9SMark Brown 			}
6321547aba9SMark Brown 		}
633f0fba2adSLiam Girdwood 	}
634db2a4165SFrank Mandarino 
63587506549SMark Brown 	if (card->suspend_post)
63670b2ac12SMark Brown 		card->suspend_post(card);
637db2a4165SFrank Mandarino 
638db2a4165SFrank Mandarino 	return 0;
639db2a4165SFrank Mandarino }
6406f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
641db2a4165SFrank Mandarino 
6422c7b696aSMarcel Ziswiler /*
6432c7b696aSMarcel Ziswiler  * deferred resume work, so resume can complete before we finished
6446ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6456ed25978SAndy Green  */
6466ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
647db2a4165SFrank Mandarino {
648f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
6492c7b696aSMarcel Ziswiler 			container_of(work, struct snd_soc_card,
6502c7b696aSMarcel Ziswiler 				     deferred_resume_work);
6511a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
652d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
6531a497983SMengdong Lin 	int i;
654db2a4165SFrank Mandarino 
6552c7b696aSMarcel Ziswiler 	/*
6562c7b696aSMarcel Ziswiler 	 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6576ed25978SAndy Green 	 * so userspace apps are blocked from touching us
6586ed25978SAndy Green 	 */
6596ed25978SAndy Green 
660f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
6616ed25978SAndy Green 
6629949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
663f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
6649949788bSMark Brown 
66587506549SMark Brown 	if (card->resume_pre)
66670b2ac12SMark Brown 		card->resume_pre(card);
667db2a4165SFrank Mandarino 
668f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
669e40fadbcSKuninori Morimoto 		if (snd_soc_component_is_suspended(component))
6709a840cbaSKuninori Morimoto 			snd_soc_component_resume(component);
6711547aba9SMark Brown 	}
672db2a4165SFrank Mandarino 
673bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
674d74c2a15SKuninori Morimoto 		int stream;
6753efab7dcSMark Brown 
6761a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6773efab7dcSMark Brown 			continue;
6783efab7dcSMark Brown 
679d74c2a15SKuninori Morimoto 		for_each_pcm_streams(stream)
680d74c2a15SKuninori Morimoto 			snd_soc_dapm_stream_event(rtd, stream,
681db2a4165SFrank Mandarino 						  SND_SOC_DAPM_STREAM_RESUME);
682db2a4165SFrank Mandarino 	}
683db2a4165SFrank Mandarino 
6843ff3f64bSMark Brown 	/* unmute any active DACs */
685bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6860b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
6870f6011fdSKuninori Morimoto 		int playback = SNDRV_PCM_STREAM_PLAYBACK;
6883efab7dcSMark Brown 
6891a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6903efab7dcSMark Brown 			continue;
6913efab7dcSMark Brown 
692a4be4187SKuninori Morimoto 		for_each_rtd_codec_dais(rtd, i, dai) {
6930f6011fdSKuninori Morimoto 			if (dai->stream_active[playback])
6940f6011fdSKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 0, playback);
695db2a4165SFrank Mandarino 		}
69688bd870fSBenoit Cousson 	}
697db2a4165SFrank Mandarino 
69887506549SMark Brown 	if (card->resume_post)
69970b2ac12SMark Brown 		card->resume_post(card);
700db2a4165SFrank Mandarino 
701f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
7026ed25978SAndy Green 
7038be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
7048be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
705e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
7061a7aaa58SJeeja KP 
7071a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
7081a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
7096ed25978SAndy Green }
7106ed25978SAndy Green 
7116ed25978SAndy Green /* powers up audio subsystem after a suspend */
7126f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
7136ed25978SAndy Green {
7146f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
71576c39e86SKuninori Morimoto 	struct snd_soc_component *component;
716b9dd94a8SPeter Ujfalusi 
717c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
718c5599b87SLars-Peter Clausen 	if (!card->instantiated)
7195ff1ddf2SEric Miao 		return 0;
7205ff1ddf2SEric Miao 
721988e8cc4SNicolin Chen 	/* activate pins from sleep state */
72276c39e86SKuninori Morimoto 	for_each_card_components(card, component)
72376c39e86SKuninori Morimoto 		if (component->active)
72476c39e86SKuninori Morimoto 			pinctrl_pm_select_default_state(component->dev);
725988e8cc4SNicolin Chen 
726f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: Scheduling resume work\n");
7276308419aSMark Brown 	if (!schedule_work(&card->deferred_resume_work))
728f110bfc7SLiam Girdwood 		dev_err(dev, "ASoC: resume work item may be lost\n");
7296ed25978SAndy Green 
730db2a4165SFrank Mandarino 	return 0;
731db2a4165SFrank Mandarino }
7326f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
733b3da4251SKuninori Morimoto 
734b3da4251SKuninori Morimoto static void soc_resume_init(struct snd_soc_card *card)
735b3da4251SKuninori Morimoto {
736b3da4251SKuninori Morimoto 	/* deferred resume work */
737b3da4251SKuninori Morimoto 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
738b3da4251SKuninori Morimoto }
739db2a4165SFrank Mandarino #else
7406f8ab4acSMark Brown #define snd_soc_suspend NULL
7416f8ab4acSMark Brown #define snd_soc_resume NULL
742b3da4251SKuninori Morimoto static inline void soc_resume_init(struct snd_soc_card *card)
743b3da4251SKuninori Morimoto {
744b3da4251SKuninori Morimoto }
745db2a4165SFrank Mandarino #endif
746db2a4165SFrank Mandarino 
74785e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
74802a06d30SBarry Song };
74902a06d30SBarry Song 
750c0834440SKuninori Morimoto static struct device_node
751c0834440SKuninori Morimoto *soc_component_to_node(struct snd_soc_component *component)
75212023a9aSMisael Lopez Cruz {
753c0834440SKuninori Morimoto 	struct device_node *of_node;
75412023a9aSMisael Lopez Cruz 
755c0834440SKuninori Morimoto 	of_node = component->dev->of_node;
756c0834440SKuninori Morimoto 	if (!of_node && component->dev->parent)
757c0834440SKuninori Morimoto 		of_node = component->dev->parent->of_node;
75834e81ab4SLars-Peter Clausen 
759c0834440SKuninori Morimoto 	return of_node;
76012023a9aSMisael Lopez Cruz }
76112023a9aSMisael Lopez Cruz 
762be6ac0a9SKuninori Morimoto static int snd_soc_is_matching_component(
763be6ac0a9SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc,
764be6ac0a9SKuninori Morimoto 	struct snd_soc_component *component)
765be6ac0a9SKuninori Morimoto {
766be6ac0a9SKuninori Morimoto 	struct device_node *component_of_node;
767be6ac0a9SKuninori Morimoto 
7687d7db5d3SKuninori Morimoto 	if (!dlc)
7697d7db5d3SKuninori Morimoto 		return 0;
7707d7db5d3SKuninori Morimoto 
7717d7db5d3SKuninori Morimoto 	component_of_node = soc_component_to_node(component);
772be6ac0a9SKuninori Morimoto 
773be6ac0a9SKuninori Morimoto 	if (dlc->of_node && component_of_node != dlc->of_node)
774be6ac0a9SKuninori Morimoto 		return 0;
775be6ac0a9SKuninori Morimoto 	if (dlc->name && strcmp(component->name, dlc->name))
776be6ac0a9SKuninori Morimoto 		return 0;
777be6ac0a9SKuninori Morimoto 
778be6ac0a9SKuninori Morimoto 	return 1;
779be6ac0a9SKuninori Morimoto }
780be6ac0a9SKuninori Morimoto 
781db2a4165SFrank Mandarino static struct snd_soc_component *soc_find_component(
782c1e230f0SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc)
783db2a4165SFrank Mandarino {
784db2a4165SFrank Mandarino 	struct snd_soc_component *component;
785db2a4165SFrank Mandarino 
786db2a4165SFrank Mandarino 	lockdep_assert_held(&client_mutex);
787db2a4165SFrank Mandarino 
788e3303268SKuninori Morimoto 	/*
789e3303268SKuninori Morimoto 	 * NOTE
790e3303268SKuninori Morimoto 	 *
791e3303268SKuninori Morimoto 	 * It returns *1st* found component, but some driver
792e3303268SKuninori Morimoto 	 * has few components by same of_node/name
793e3303268SKuninori Morimoto 	 * ex)
794e3303268SKuninori Morimoto 	 *	CPU component and generic DMAEngine component
795e3303268SKuninori Morimoto 	 */
796c1e230f0SKuninori Morimoto 	for_each_component(component)
797c1e230f0SKuninori Morimoto 		if (snd_soc_is_matching_component(dlc, component))
798db2a4165SFrank Mandarino 			return component;
799db2a4165SFrank Mandarino 
800db2a4165SFrank Mandarino 	return NULL;
80112023a9aSMisael Lopez Cruz }
80212023a9aSMisael Lopez Cruz 
803fbb88b5cSMengdong Lin /**
804fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
805fbb88b5cSMengdong Lin  *
8064958471bSJeffy Chen  * @dlc: name of the DAI or the DAI driver and optional component info to match
807fbb88b5cSMengdong Lin  *
808ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
809fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
810fbb88b5cSMengdong Lin  * should also match if being specified.
811fbb88b5cSMengdong Lin  *
812fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
813fbb88b5cSMengdong Lin  */
814305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
81514621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
81612023a9aSMisael Lopez Cruz {
81714621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
81814621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
81912023a9aSMisael Lopez Cruz 
82034e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
82134e81ab4SLars-Peter Clausen 
82214621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs */
823368dee94SKuninori Morimoto 	for_each_component(component) {
824be6ac0a9SKuninori Morimoto 		if (!snd_soc_is_matching_component(dlc, component))
82512023a9aSMisael Lopez Cruz 			continue;
82615a0c645SKuninori Morimoto 		for_each_component_dais(component, dai) {
8274958471bSJeffy Chen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
8286a6dafdaSJeffy Chen 			    && (!dai->driver->name
8296a6dafdaSJeffy Chen 				|| strcmp(dai->driver->name, dlc->dai_name)))
83014621c7eSLars-Peter Clausen 				continue;
83112023a9aSMisael Lopez Cruz 
83214621c7eSLars-Peter Clausen 			return dai;
83312023a9aSMisael Lopez Cruz 		}
83412023a9aSMisael Lopez Cruz 	}
83512023a9aSMisael Lopez Cruz 
83612023a9aSMisael Lopez Cruz 	return NULL;
83712023a9aSMisael Lopez Cruz }
838305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
83912023a9aSMisael Lopez Cruz 
840bfce78a5SKuninori Morimoto static int soc_dai_link_sanity_check(struct snd_soc_card *card,
84136794902SKuninori Morimoto 				     struct snd_soc_dai_link *link)
84236794902SKuninori Morimoto {
84336794902SKuninori Morimoto 	int i;
84476afa643SShreyas NC 	struct snd_soc_dai_link_component *cpu, *codec, *platform;
84536794902SKuninori Morimoto 
84636794902SKuninori Morimoto 	for_each_link_codecs(link, i, codec) {
84736794902SKuninori Morimoto 		/*
84836794902SKuninori Morimoto 		 * Codec must be specified by 1 of name or OF node,
84936794902SKuninori Morimoto 		 * not both or neither.
85036794902SKuninori Morimoto 		 */
85136794902SKuninori Morimoto 		if (!!codec->name == !!codec->of_node) {
85236794902SKuninori Morimoto 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
85336794902SKuninori Morimoto 				link->name);
85436794902SKuninori Morimoto 			return -EINVAL;
85536794902SKuninori Morimoto 		}
85636794902SKuninori Morimoto 
85736794902SKuninori Morimoto 		/* Codec DAI name must be specified */
85836794902SKuninori Morimoto 		if (!codec->dai_name) {
85936794902SKuninori Morimoto 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
86036794902SKuninori Morimoto 				link->name);
86136794902SKuninori Morimoto 			return -EINVAL;
86236794902SKuninori Morimoto 		}
86336794902SKuninori Morimoto 
86436794902SKuninori Morimoto 		/*
86536794902SKuninori Morimoto 		 * Defer card registration if codec component is not added to
86636794902SKuninori Morimoto 		 * component list.
86736794902SKuninori Morimoto 		 */
868*b2acc24cSRanjani Sridharan 		if (!soc_find_component(codec)) {
869*b2acc24cSRanjani Sridharan 			dev_dbg(card->dev,
870*b2acc24cSRanjani Sridharan 				"ASoC: codec component %s not found for link %s\n",
871*b2acc24cSRanjani Sridharan 				codec->name, link->name);
87236794902SKuninori Morimoto 			return -EPROBE_DEFER;
87336794902SKuninori Morimoto 		}
874*b2acc24cSRanjani Sridharan 	}
87536794902SKuninori Morimoto 
87636794902SKuninori Morimoto 	for_each_link_platforms(link, i, platform) {
87736794902SKuninori Morimoto 		/*
87836794902SKuninori Morimoto 		 * Platform may be specified by either name or OF node, but it
87936794902SKuninori Morimoto 		 * can be left unspecified, then no components will be inserted
88036794902SKuninori Morimoto 		 * in the rtdcom list
88136794902SKuninori Morimoto 		 */
88236794902SKuninori Morimoto 		if (!!platform->name == !!platform->of_node) {
88336794902SKuninori Morimoto 			dev_err(card->dev,
88436794902SKuninori Morimoto 				"ASoC: Neither/both platform name/of_node are set for %s\n",
88536794902SKuninori Morimoto 				link->name);
88636794902SKuninori Morimoto 			return -EINVAL;
88736794902SKuninori Morimoto 		}
88836794902SKuninori Morimoto 
88936794902SKuninori Morimoto 		/*
89036794902SKuninori Morimoto 		 * Defer card registration if platform component is not added to
89136794902SKuninori Morimoto 		 * component list.
89236794902SKuninori Morimoto 		 */
893*b2acc24cSRanjani Sridharan 		if (!soc_find_component(platform)) {
894*b2acc24cSRanjani Sridharan 			dev_dbg(card->dev,
895*b2acc24cSRanjani Sridharan 				"ASoC: platform component %s not found for link %s\n",
896*b2acc24cSRanjani Sridharan 				platform->name, link->name);
89736794902SKuninori Morimoto 			return -EPROBE_DEFER;
89836794902SKuninori Morimoto 		}
899*b2acc24cSRanjani Sridharan 	}
90036794902SKuninori Morimoto 
90176afa643SShreyas NC 	for_each_link_cpus(link, i, cpu) {
90236794902SKuninori Morimoto 		/*
90336794902SKuninori Morimoto 		 * CPU device may be specified by either name or OF node, but
90436794902SKuninori Morimoto 		 * can be left unspecified, and will be matched based on DAI
90536794902SKuninori Morimoto 		 * name alone..
90636794902SKuninori Morimoto 		 */
90776afa643SShreyas NC 		if (cpu->name && cpu->of_node) {
90836794902SKuninori Morimoto 			dev_err(card->dev,
90936794902SKuninori Morimoto 				"ASoC: Neither/both cpu name/of_node are set for %s\n",
91036794902SKuninori Morimoto 				link->name);
91136794902SKuninori Morimoto 			return -EINVAL;
91236794902SKuninori Morimoto 		}
91336794902SKuninori Morimoto 
91436794902SKuninori Morimoto 		/*
915cd3c5ad7SKuninori Morimoto 		 * Defer card registration if cpu dai component is not added to
91636794902SKuninori Morimoto 		 * component list.
91736794902SKuninori Morimoto 		 */
91876afa643SShreyas NC 		if ((cpu->of_node || cpu->name) &&
919*b2acc24cSRanjani Sridharan 		    !soc_find_component(cpu)) {
920*b2acc24cSRanjani Sridharan 			dev_dbg(card->dev,
921*b2acc24cSRanjani Sridharan 				"ASoC: cpu component %s not found for link %s\n",
922*b2acc24cSRanjani Sridharan 				cpu->name, link->name);
92336794902SKuninori Morimoto 			return -EPROBE_DEFER;
924*b2acc24cSRanjani Sridharan 		}
92536794902SKuninori Morimoto 
92636794902SKuninori Morimoto 		/*
92736794902SKuninori Morimoto 		 * At least one of CPU DAI name or CPU device name/node must be
92836794902SKuninori Morimoto 		 * specified
92936794902SKuninori Morimoto 		 */
93076afa643SShreyas NC 		if (!cpu->dai_name &&
93176afa643SShreyas NC 		    !(cpu->name || cpu->of_node)) {
93236794902SKuninori Morimoto 			dev_err(card->dev,
93336794902SKuninori Morimoto 				"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
93436794902SKuninori Morimoto 				link->name);
93536794902SKuninori Morimoto 			return -EINVAL;
93636794902SKuninori Morimoto 		}
93776afa643SShreyas NC 	}
93836794902SKuninori Morimoto 
93936794902SKuninori Morimoto 	return 0;
94036794902SKuninori Morimoto }
94136794902SKuninori Morimoto 
942da704f26SKuninori Morimoto /**
94350cd9b53SKuninori Morimoto  * snd_soc_remove_pcm_runtime - Remove a pcm_runtime from card
94450cd9b53SKuninori Morimoto  * @card: The ASoC card to which the pcm_runtime has
94550cd9b53SKuninori Morimoto  * @rtd: The pcm_runtime to remove
946da704f26SKuninori Morimoto  *
94750cd9b53SKuninori Morimoto  * This function removes a pcm_runtime from the ASoC card.
948da704f26SKuninori Morimoto  */
94950cd9b53SKuninori Morimoto void snd_soc_remove_pcm_runtime(struct snd_soc_card *card,
95050cd9b53SKuninori Morimoto 				struct snd_soc_pcm_runtime *rtd)
951bc7a9091SKuninori Morimoto {
952da704f26SKuninori Morimoto 	lockdep_assert_held(&client_mutex);
953da704f26SKuninori Morimoto 
954da704f26SKuninori Morimoto 	/*
955da704f26SKuninori Morimoto 	 * Notify the machine driver for extra destruction
956da704f26SKuninori Morimoto 	 */
957da704f26SKuninori Morimoto 	if (card->remove_dai_link)
95850cd9b53SKuninori Morimoto 		card->remove_dai_link(card, rtd->dai_link);
959da704f26SKuninori Morimoto 
960bc7a9091SKuninori Morimoto 	soc_free_pcm_runtime(rtd);
961bc7a9091SKuninori Morimoto }
96250cd9b53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_remove_pcm_runtime);
963bc7a9091SKuninori Morimoto 
96463dc47daSKuninori Morimoto /**
9650c048004SKuninori Morimoto  * snd_soc_add_pcm_runtime - Add a pcm_runtime dynamically via dai_link
9660c048004SKuninori Morimoto  * @card: The ASoC card to which the pcm_runtime is added
9670c048004SKuninori Morimoto  * @dai_link: The DAI link to find pcm_runtime
96863dc47daSKuninori Morimoto  *
9690c048004SKuninori Morimoto  * This function adds a pcm_runtime ASoC card by using dai_link.
97063dc47daSKuninori Morimoto  *
9710c048004SKuninori Morimoto  * Note: Topology can use this API to add pcm_runtime when probing the
97263dc47daSKuninori Morimoto  * topology component. And machine drivers can still define static
97363dc47daSKuninori Morimoto  * DAI links in dai_link array.
97463dc47daSKuninori Morimoto  */
9750c048004SKuninori Morimoto int snd_soc_add_pcm_runtime(struct snd_soc_card *card,
9766f2f1ff0SMengdong Lin 			    struct snd_soc_dai_link *dai_link)
977db2a4165SFrank Mandarino {
9781a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
97976afa643SShreyas NC 	struct snd_soc_dai_link_component *codec, *platform, *cpu;
98090be711eSKuninori Morimoto 	struct snd_soc_component *component;
981bfce78a5SKuninori Morimoto 	int i, ret;
982db2a4165SFrank Mandarino 
98363dc47daSKuninori Morimoto 	lockdep_assert_held(&client_mutex);
98463dc47daSKuninori Morimoto 
98563dc47daSKuninori Morimoto 	/*
98663dc47daSKuninori Morimoto 	 * Notify the machine driver for extra initialization
98763dc47daSKuninori Morimoto 	 */
98863dc47daSKuninori Morimoto 	if (card->add_dai_link)
98963dc47daSKuninori Morimoto 		card->add_dai_link(card, dai_link);
99063dc47daSKuninori Morimoto 
991a655de80SLiam Girdwood 	if (dai_link->ignore)
992a655de80SLiam Girdwood 		return 0;
993a655de80SLiam Girdwood 
9946f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
9956308419aSMark Brown 
996bfce78a5SKuninori Morimoto 	ret = soc_dai_link_sanity_check(card, dai_link);
997bfce78a5SKuninori Morimoto 	if (ret < 0)
998bfce78a5SKuninori Morimoto 		return ret;
999bfce78a5SKuninori Morimoto 
1000513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
1001513cb311SSudip Mukherjee 	if (!rtd)
1002513cb311SSudip Mukherjee 		return -ENOMEM;
1003513cb311SSudip Mukherjee 
100476afa643SShreyas NC 	rtd->num_cpus = dai_link->num_cpus;
100576afa643SShreyas NC 	for_each_link_cpus(dai_link, i, cpu) {
100676afa643SShreyas NC 		rtd->cpu_dais[i] = snd_soc_find_dai(cpu);
100776afa643SShreyas NC 		if (!rtd->cpu_dais[i]) {
10086b490879SMartin Hundebøll 			dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
100976afa643SShreyas NC 				 cpu->dai_name);
10101a497983SMengdong Lin 			goto _err_defer;
1011c5af3a2eSMark Brown 		}
101276afa643SShreyas NC 		snd_soc_rtd_add_component(rtd, rtd->cpu_dais[i]->component);
101376afa643SShreyas NC 	}
101476afa643SShreyas NC 
101576afa643SShreyas NC 	/* Single cpu links expect cpu and cpu_dai in runtime data */
101676afa643SShreyas NC 	rtd->cpu_dai = rtd->cpu_dais[0];
1017c5af3a2eSMark Brown 
101888bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
1019e2b30edfSKuninori Morimoto 	rtd->num_codecs = dai_link->num_codecs;
102034614739SJerome Brunet 	for_each_link_codecs(dai_link, i, codec) {
102134614739SJerome Brunet 		rtd->codec_dais[i] = snd_soc_find_dai(codec);
10220a2cfcd9SKuninori Morimoto 		if (!rtd->codec_dais[i]) {
10237c7e2d6aSStefan Agner 			dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
102434614739SJerome Brunet 				 codec->dai_name);
10251a497983SMengdong Lin 			goto _err_defer;
102612023a9aSMisael Lopez Cruz 		}
102734614739SJerome Brunet 
102812b05232SKuninori Morimoto 		snd_soc_rtd_add_component(rtd, rtd->codec_dais[i]->component);
102988bd870fSBenoit Cousson 	}
103088bd870fSBenoit Cousson 
103188bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
10320a2cfcd9SKuninori Morimoto 	rtd->codec_dai = rtd->codec_dais[0];
103312023a9aSMisael Lopez Cruz 
1034e2b30edfSKuninori Morimoto 	/* Find PLATFORM from registered PLATFORMs */
103534614739SJerome Brunet 	for_each_link_platforms(dai_link, i, platform) {
1036368dee94SKuninori Morimoto 		for_each_component(component) {
103734614739SJerome Brunet 			if (!snd_soc_is_matching_component(platform, component))
103890be711eSKuninori Morimoto 				continue;
103990be711eSKuninori Morimoto 
104012b05232SKuninori Morimoto 			snd_soc_rtd_add_component(rtd, component);
104190be711eSKuninori Morimoto 		}
104234614739SJerome Brunet 	}
104390be711eSKuninori Morimoto 
1044b19e6e7bSMark Brown 	return 0;
10451a497983SMengdong Lin 
10461a497983SMengdong Lin _err_defer:
104750cd9b53SKuninori Morimoto 	snd_soc_remove_pcm_runtime(card, rtd);
10481a497983SMengdong Lin 	return -EPROBE_DEFER;
10496b05eda6SMark Brown }
10500c048004SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_pcm_runtime);
10516b05eda6SMark Brown 
1052d1eb6d11SKuninori Morimoto static int soc_dai_pcm_new(struct snd_soc_pcm_runtime *rtd)
10539e9c70a5SKuninori Morimoto {
1054d1eb6d11SKuninori Morimoto 	struct snd_soc_dai *dai;
10559e9c70a5SKuninori Morimoto 	int i, ret = 0;
10569e9c70a5SKuninori Morimoto 
1057d1eb6d11SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai) {
1058d1eb6d11SKuninori Morimoto 		struct snd_soc_dai_driver *drv = dai->driver;
10599e9c70a5SKuninori Morimoto 
10609e9c70a5SKuninori Morimoto 		if (drv->pcm_new)
1061d1eb6d11SKuninori Morimoto 			ret = drv->pcm_new(rtd, dai);
10629e9c70a5SKuninori Morimoto 		if (ret < 0) {
1063d1eb6d11SKuninori Morimoto 			dev_err(dai->dev,
10649e9c70a5SKuninori Morimoto 				"ASoC: Failed to bind %s with pcm device\n",
1065d1eb6d11SKuninori Morimoto 				dai->name);
10669e9c70a5SKuninori Morimoto 			return ret;
10679e9c70a5SKuninori Morimoto 		}
10689e9c70a5SKuninori Morimoto 	}
10699e9c70a5SKuninori Morimoto 
10709e9c70a5SKuninori Morimoto 	return 0;
10719e9c70a5SKuninori Morimoto }
10729e9c70a5SKuninori Morimoto 
1073eaffeefbSKuninori Morimoto static int soc_init_pcm_runtime(struct snd_soc_card *card,
107446496acbSKuninori Morimoto 				struct snd_soc_pcm_runtime *rtd)
107546496acbSKuninori Morimoto {
107646496acbSKuninori Morimoto 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
107746496acbSKuninori Morimoto 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
107846496acbSKuninori Morimoto 	struct snd_soc_component *component;
1079613fb500SKuninori Morimoto 	int ret, num, i;
108046496acbSKuninori Morimoto 
108146496acbSKuninori Morimoto 	/* set default power off timeout */
108246496acbSKuninori Morimoto 	rtd->pmdown_time = pmdown_time;
108346496acbSKuninori Morimoto 
108446496acbSKuninori Morimoto 	/* do machine specific initialization */
108546496acbSKuninori Morimoto 	if (dai_link->init) {
108646496acbSKuninori Morimoto 		ret = dai_link->init(rtd);
108746496acbSKuninori Morimoto 		if (ret < 0) {
108846496acbSKuninori Morimoto 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
108946496acbSKuninori Morimoto 				dai_link->name, ret);
109046496acbSKuninori Morimoto 			return ret;
109146496acbSKuninori Morimoto 		}
109246496acbSKuninori Morimoto 	}
109346496acbSKuninori Morimoto 
109446496acbSKuninori Morimoto 	if (dai_link->dai_fmt) {
109546496acbSKuninori Morimoto 		ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
109646496acbSKuninori Morimoto 		if (ret)
109746496acbSKuninori Morimoto 			return ret;
109846496acbSKuninori Morimoto 	}
109946496acbSKuninori Morimoto 
110046496acbSKuninori Morimoto 	/* add DPCM sysfs entries */
110146496acbSKuninori Morimoto 	soc_dpcm_debugfs_add(rtd);
110246496acbSKuninori Morimoto 
110346496acbSKuninori Morimoto 	num = rtd->num;
110446496acbSKuninori Morimoto 
110546496acbSKuninori Morimoto 	/*
110646496acbSKuninori Morimoto 	 * most drivers will register their PCMs using DAI link ordering but
110746496acbSKuninori Morimoto 	 * topology based drivers can use the DAI link id field to set PCM
110846496acbSKuninori Morimoto 	 * device number and then use rtd + a base offset of the BEs.
110946496acbSKuninori Morimoto 	 */
1110613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
111146496acbSKuninori Morimoto 		if (!component->driver->use_dai_pcm_id)
111246496acbSKuninori Morimoto 			continue;
111346496acbSKuninori Morimoto 
111446496acbSKuninori Morimoto 		if (rtd->dai_link->no_pcm)
111546496acbSKuninori Morimoto 			num += component->driver->be_pcm_base;
111646496acbSKuninori Morimoto 		else
111746496acbSKuninori Morimoto 			num = rtd->dai_link->id;
111846496acbSKuninori Morimoto 	}
111946496acbSKuninori Morimoto 
112046496acbSKuninori Morimoto 	/* create compress_device if possible */
112146496acbSKuninori Morimoto 	ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
112246496acbSKuninori Morimoto 	if (ret != -ENOTSUPP) {
112346496acbSKuninori Morimoto 		if (ret < 0)
112446496acbSKuninori Morimoto 			dev_err(card->dev, "ASoC: can't create compress %s\n",
112546496acbSKuninori Morimoto 				dai_link->stream_name);
112646496acbSKuninori Morimoto 		return ret;
112746496acbSKuninori Morimoto 	}
112846496acbSKuninori Morimoto 
112946496acbSKuninori Morimoto 	/* create the pcm */
113046496acbSKuninori Morimoto 	ret = soc_new_pcm(rtd, num);
113146496acbSKuninori Morimoto 	if (ret < 0) {
113246496acbSKuninori Morimoto 		dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
113346496acbSKuninori Morimoto 			dai_link->stream_name, ret);
113446496acbSKuninori Morimoto 		return ret;
113546496acbSKuninori Morimoto 	}
1136d1eb6d11SKuninori Morimoto 
1137d1eb6d11SKuninori Morimoto 	return soc_dai_pcm_new(rtd);
113846496acbSKuninori Morimoto }
113946496acbSKuninori Morimoto 
1140ffd60fbaSKuninori Morimoto static void soc_set_name_prefix(struct snd_soc_card *card,
1141ffd60fbaSKuninori Morimoto 				struct snd_soc_component *component)
1142ffd60fbaSKuninori Morimoto {
1143aec3ff99SKuninori Morimoto 	struct device_node *of_node = soc_component_to_node(component);
114429d9fc7aSKuninori Morimoto 	const char *str;
114529d9fc7aSKuninori Morimoto 	int ret, i;
1146ffd60fbaSKuninori Morimoto 
11474702f991SKuninori Morimoto 	for (i = 0; i < card->num_configs; i++) {
1148ffd60fbaSKuninori Morimoto 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1149ffd60fbaSKuninori Morimoto 
1150c13493a2SKuninori Morimoto 		if (snd_soc_is_matching_component(&map->dlc, component)) {
1151ffd60fbaSKuninori Morimoto 			component->name_prefix = map->name_prefix;
1152ffd60fbaSKuninori Morimoto 			return;
1153ffd60fbaSKuninori Morimoto 		}
1154c13493a2SKuninori Morimoto 	}
1155ffd60fbaSKuninori Morimoto 
1156ffd60fbaSKuninori Morimoto 	/*
1157ffd60fbaSKuninori Morimoto 	 * If there is no configuration table or no match in the table,
1158ffd60fbaSKuninori Morimoto 	 * check if a prefix is provided in the node
1159ffd60fbaSKuninori Morimoto 	 */
116029d9fc7aSKuninori Morimoto 	ret = of_property_read_string(of_node, "sound-name-prefix", &str);
116129d9fc7aSKuninori Morimoto 	if (ret < 0)
116229d9fc7aSKuninori Morimoto 		return;
116329d9fc7aSKuninori Morimoto 
116429d9fc7aSKuninori Morimoto 	component->name_prefix = str;
1165ffd60fbaSKuninori Morimoto }
1166ffd60fbaSKuninori Morimoto 
1167c6619b72SKuninori Morimoto static void soc_remove_component(struct snd_soc_component *component,
1168c6619b72SKuninori Morimoto 				 int probed)
116922d14231SKuninori Morimoto {
1170c6619b72SKuninori Morimoto 
1171c6619b72SKuninori Morimoto 	if (!component->card)
1172c6619b72SKuninori Morimoto 		return;
1173c6619b72SKuninori Morimoto 
1174c6619b72SKuninori Morimoto 	if (probed)
1175c6619b72SKuninori Morimoto 		snd_soc_component_remove(component);
1176c6619b72SKuninori Morimoto 
117704f770d9SKuninori Morimoto 	/* For framework level robustness */
11783bb936f5SAmadeusz Sławiński 	snd_soc_component_set_jack(component, NULL, NULL);
117904f770d9SKuninori Morimoto 
11807a5d9815SBard liao 	list_del_init(&component->card_list);
118122d14231SKuninori Morimoto 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
118222d14231SKuninori Morimoto 	soc_cleanup_component_debugfs(component);
118322d14231SKuninori Morimoto 	component->card = NULL;
11840e36f36bSPierre-Louis Bossart 	snd_soc_component_module_put_when_remove(component);
118522d14231SKuninori Morimoto }
118622d14231SKuninori Morimoto 
1187ffd60fbaSKuninori Morimoto static int soc_probe_component(struct snd_soc_card *card,
1188ffd60fbaSKuninori Morimoto 			       struct snd_soc_component *component)
1189ffd60fbaSKuninori Morimoto {
1190ffd60fbaSKuninori Morimoto 	struct snd_soc_dapm_context *dapm =
1191ffd60fbaSKuninori Morimoto 		snd_soc_component_get_dapm(component);
1192ffd60fbaSKuninori Morimoto 	struct snd_soc_dai *dai;
1193c6619b72SKuninori Morimoto 	int probed = 0;
1194ffd60fbaSKuninori Morimoto 	int ret;
1195ffd60fbaSKuninori Morimoto 
1196ffd60fbaSKuninori Morimoto 	if (!strcmp(component->name, "snd-soc-dummy"))
1197ffd60fbaSKuninori Morimoto 		return 0;
1198ffd60fbaSKuninori Morimoto 
1199ffd60fbaSKuninori Morimoto 	if (component->card) {
1200ffd60fbaSKuninori Morimoto 		if (component->card != card) {
1201ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1202ffd60fbaSKuninori Morimoto 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1203ffd60fbaSKuninori Morimoto 				card->name, component->card->name);
1204ffd60fbaSKuninori Morimoto 			return -ENODEV;
1205ffd60fbaSKuninori Morimoto 		}
1206ffd60fbaSKuninori Morimoto 		return 0;
1207ffd60fbaSKuninori Morimoto 	}
1208ffd60fbaSKuninori Morimoto 
1209ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_module_get_when_probe(component);
1210ffd60fbaSKuninori Morimoto 	if (ret < 0)
1211ffd60fbaSKuninori Morimoto 		return ret;
1212ffd60fbaSKuninori Morimoto 
1213ffd60fbaSKuninori Morimoto 	component->card = card;
1214ffd60fbaSKuninori Morimoto 	soc_set_name_prefix(card, component);
1215ffd60fbaSKuninori Morimoto 
1216ffd60fbaSKuninori Morimoto 	soc_init_component_debugfs(component);
1217ffd60fbaSKuninori Morimoto 
121895c267ddSKuninori Morimoto 	snd_soc_dapm_init(dapm, card, component);
1219b614beafSKuninori Morimoto 
1220ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_new_controls(dapm,
1221ffd60fbaSKuninori Morimoto 					component->driver->dapm_widgets,
1222ffd60fbaSKuninori Morimoto 					component->driver->num_dapm_widgets);
1223ffd60fbaSKuninori Morimoto 
1224ffd60fbaSKuninori Morimoto 	if (ret != 0) {
1225ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1226ffd60fbaSKuninori Morimoto 			"Failed to create new controls %d\n", ret);
1227ffd60fbaSKuninori Morimoto 		goto err_probe;
1228ffd60fbaSKuninori Morimoto 	}
1229ffd60fbaSKuninori Morimoto 
1230ffd60fbaSKuninori Morimoto 	for_each_component_dais(component, dai) {
1231ffd60fbaSKuninori Morimoto 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1232ffd60fbaSKuninori Morimoto 		if (ret != 0) {
1233ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1234ffd60fbaSKuninori Morimoto 				"Failed to create DAI widgets %d\n", ret);
1235ffd60fbaSKuninori Morimoto 			goto err_probe;
1236ffd60fbaSKuninori Morimoto 		}
1237ffd60fbaSKuninori Morimoto 	}
1238ffd60fbaSKuninori Morimoto 
1239ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_probe(component);
1240ffd60fbaSKuninori Morimoto 	if (ret < 0) {
1241ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1242ffd60fbaSKuninori Morimoto 			"ASoC: failed to probe component %d\n", ret);
1243ffd60fbaSKuninori Morimoto 		goto err_probe;
1244ffd60fbaSKuninori Morimoto 	}
1245ffd60fbaSKuninori Morimoto 	WARN(dapm->idle_bias_off &&
1246ffd60fbaSKuninori Morimoto 	     dapm->bias_level != SND_SOC_BIAS_OFF,
1247ffd60fbaSKuninori Morimoto 	     "codec %s can not start from non-off bias with idle_bias_off==1\n",
1248ffd60fbaSKuninori Morimoto 	     component->name);
1249c6619b72SKuninori Morimoto 	probed = 1;
1250ffd60fbaSKuninori Morimoto 
1251ffd60fbaSKuninori Morimoto 	/* machine specific init */
1252ffd60fbaSKuninori Morimoto 	if (component->init) {
1253ffd60fbaSKuninori Morimoto 		ret = component->init(component);
1254ffd60fbaSKuninori Morimoto 		if (ret < 0) {
1255ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1256ffd60fbaSKuninori Morimoto 				"Failed to do machine specific init %d\n", ret);
1257ffd60fbaSKuninori Morimoto 			goto err_probe;
1258ffd60fbaSKuninori Morimoto 		}
1259ffd60fbaSKuninori Morimoto 	}
1260ffd60fbaSKuninori Morimoto 
1261ffd60fbaSKuninori Morimoto 	ret = snd_soc_add_component_controls(component,
1262ffd60fbaSKuninori Morimoto 					     component->driver->controls,
1263ffd60fbaSKuninori Morimoto 					     component->driver->num_controls);
1264ffd60fbaSKuninori Morimoto 	if (ret < 0)
1265ffd60fbaSKuninori Morimoto 		goto err_probe;
1266ffd60fbaSKuninori Morimoto 
1267ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(dapm,
1268ffd60fbaSKuninori Morimoto 				      component->driver->dapm_routes,
1269ffd60fbaSKuninori Morimoto 				      component->driver->num_dapm_routes);
1270a22ae72bSPierre-Louis Bossart 	if (ret < 0) {
1271a22ae72bSPierre-Louis Bossart 		if (card->disable_route_checks) {
1272a22ae72bSPierre-Louis Bossart 			dev_info(card->dev,
1273a22ae72bSPierre-Louis Bossart 				 "%s: disable_route_checks set, ignoring errors on add_routes\n",
1274a22ae72bSPierre-Louis Bossart 				 __func__);
1275a22ae72bSPierre-Louis Bossart 		} else {
1276a22ae72bSPierre-Louis Bossart 			dev_err(card->dev,
1277a22ae72bSPierre-Louis Bossart 				"%s: snd_soc_dapm_add_routes failed: %d\n",
1278a22ae72bSPierre-Louis Bossart 				__func__, ret);
1279ffd60fbaSKuninori Morimoto 			goto err_probe;
1280a22ae72bSPierre-Louis Bossart 		}
1281a22ae72bSPierre-Louis Bossart 	}
1282ffd60fbaSKuninori Morimoto 
1283ffd60fbaSKuninori Morimoto 	/* see for_each_card_components */
1284ffd60fbaSKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1285ffd60fbaSKuninori Morimoto 
1286ffd60fbaSKuninori Morimoto err_probe:
1287ffd60fbaSKuninori Morimoto 	if (ret < 0)
1288c6619b72SKuninori Morimoto 		soc_remove_component(component, probed);
1289ffd60fbaSKuninori Morimoto 
1290ffd60fbaSKuninori Morimoto 	return ret;
1291ffd60fbaSKuninori Morimoto }
1292ffd60fbaSKuninori Morimoto 
1293e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1294589c3563SJarkko Nikula {
1295589c3563SJarkko Nikula 	int err;
1296589c3563SJarkko Nikula 
129752abe6ccSGuennadi Liakhovetski 	if (!dai || !dai->probed || !dai->driver ||
12982eda3cb1SKuninori Morimoto 	    dai->driver->remove_order != order)
12992eda3cb1SKuninori Morimoto 		return;
13002eda3cb1SKuninori Morimoto 
1301dcdab582SKuninori Morimoto 	err = snd_soc_dai_remove(dai);
1302589c3563SJarkko Nikula 	if (err < 0)
1303e60cd14fSLars-Peter Clausen 		dev_err(dai->dev,
1304b0aa88afSMisael Lopez Cruz 			"ASoC: failed to remove %s: %d\n",
1305e60cd14fSLars-Peter Clausen 			dai->name, err);
1306dcdab582SKuninori Morimoto 
1307e60cd14fSLars-Peter Clausen 	dai->probed = 0;
1308b0aa88afSMisael Lopez Cruz }
1309b0aa88afSMisael Lopez Cruz 
1310a7d44f78SKuninori Morimoto static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1311a7d44f78SKuninori Morimoto {
1312a7d44f78SKuninori Morimoto 	int ret;
1313a7d44f78SKuninori Morimoto 
1314a7d44f78SKuninori Morimoto 	if (dai->probed ||
1315a7d44f78SKuninori Morimoto 	    dai->driver->probe_order != order)
1316a7d44f78SKuninori Morimoto 		return 0;
1317a7d44f78SKuninori Morimoto 
1318a7d44f78SKuninori Morimoto 	ret = snd_soc_dai_probe(dai);
1319a7d44f78SKuninori Morimoto 	if (ret < 0) {
1320a7d44f78SKuninori Morimoto 		dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1321a7d44f78SKuninori Morimoto 			dai->name, ret);
1322a7d44f78SKuninori Morimoto 		return ret;
1323a7d44f78SKuninori Morimoto 	}
1324a7d44f78SKuninori Morimoto 
1325a7d44f78SKuninori Morimoto 	dai->probed = 1;
1326a7d44f78SKuninori Morimoto 
1327a7d44f78SKuninori Morimoto 	return 0;
1328a7d44f78SKuninori Morimoto }
1329a7d44f78SKuninori Morimoto 
13304ca47d21SKuninori Morimoto static void soc_remove_link_dais(struct snd_soc_card *card)
1331f0fba2adSLiam Girdwood {
1332e60cd14fSLars-Peter Clausen 	int i;
13333af6ff50SKuninori Morimoto 	struct snd_soc_dai *dai;
13344ca47d21SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
13354ca47d21SKuninori Morimoto 	int order;
13364ca47d21SKuninori Morimoto 
13374ca47d21SKuninori Morimoto 	for_each_comp_order(order) {
13384ca47d21SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
13393af6ff50SKuninori Morimoto 			/* remove DAIs */
13403af6ff50SKuninori Morimoto 			for_each_rtd_dais(rtd, i, dai)
13413af6ff50SKuninori Morimoto 				soc_remove_dai(dai, order);
1342f0fba2adSLiam Girdwood 		}
13434ca47d21SKuninori Morimoto 	}
13444ca47d21SKuninori Morimoto }
1345f0fba2adSLiam Girdwood 
1346bc7c16c2SKuninori Morimoto static int soc_probe_link_dais(struct snd_soc_card *card)
1347bc7c16c2SKuninori Morimoto {
13483af6ff50SKuninori Morimoto 	struct snd_soc_dai *dai;
1349bc7c16c2SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
1350bc7c16c2SKuninori Morimoto 	int i, order, ret;
1351bc7c16c2SKuninori Morimoto 
1352bc7c16c2SKuninori Morimoto 	for_each_comp_order(order) {
1353bc7c16c2SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1354bc7c16c2SKuninori Morimoto 
1355bc7c16c2SKuninori Morimoto 			dev_dbg(card->dev,
1356bc7c16c2SKuninori Morimoto 				"ASoC: probe %s dai link %d late %d\n",
1357bc7c16c2SKuninori Morimoto 				card->name, rtd->num, order);
1358bc7c16c2SKuninori Morimoto 
135976afa643SShreyas NC 			/* probe the CPU DAI */
13603af6ff50SKuninori Morimoto 			for_each_rtd_dais(rtd, i, dai) {
13613af6ff50SKuninori Morimoto 				ret = soc_probe_dai(dai, order);
1362bc7c16c2SKuninori Morimoto 				if (ret)
1363bc7c16c2SKuninori Morimoto 					return ret;
1364bc7c16c2SKuninori Morimoto 			}
1365bc7c16c2SKuninori Morimoto 		}
1366bc7c16c2SKuninori Morimoto 	}
1367bc7c16c2SKuninori Morimoto 
1368bc7c16c2SKuninori Morimoto 	return 0;
1369bc7c16c2SKuninori Morimoto }
1370bc7c16c2SKuninori Morimoto 
1371b006c0c6SKuninori Morimoto static void soc_remove_link_components(struct snd_soc_card *card)
137262ae68faSStephen Warren {
137361aca564SLars-Peter Clausen 	struct snd_soc_component *component;
1374b006c0c6SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
1375613fb500SKuninori Morimoto 	int i, order;
137662ae68faSStephen Warren 
1377b006c0c6SKuninori Morimoto 	for_each_comp_order(order) {
1378b006c0c6SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1379613fb500SKuninori Morimoto 			for_each_rtd_components(rtd, i, component) {
1380b006c0c6SKuninori Morimoto 				if (component->driver->remove_order != order)
1381b006c0c6SKuninori Morimoto 					continue;
1382b006c0c6SKuninori Morimoto 
1383c6619b72SKuninori Morimoto 				soc_remove_component(component, 1);
138462ae68faSStephen Warren 			}
138562ae68faSStephen Warren 		}
1386b006c0c6SKuninori Morimoto 	}
1387b006c0c6SKuninori Morimoto }
138862ae68faSStephen Warren 
138962f07a6bSKuninori Morimoto static int soc_probe_link_components(struct snd_soc_card *card)
13906fb03550SKuninori Morimoto {
13916fb03550SKuninori Morimoto 	struct snd_soc_component *component;
139262f07a6bSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
1393613fb500SKuninori Morimoto 	int i, ret, order;
13946fb03550SKuninori Morimoto 
139562f07a6bSKuninori Morimoto 	for_each_comp_order(order) {
139662f07a6bSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1397613fb500SKuninori Morimoto 			for_each_rtd_components(rtd, i, component) {
139862f07a6bSKuninori Morimoto 				if (component->driver->probe_order != order)
139962f07a6bSKuninori Morimoto 					continue;
140062f07a6bSKuninori Morimoto 
14016fb03550SKuninori Morimoto 				ret = soc_probe_component(card, component);
14026fb03550SKuninori Morimoto 				if (ret < 0)
14036fb03550SKuninori Morimoto 					return ret;
14046fb03550SKuninori Morimoto 			}
14056fb03550SKuninori Morimoto 		}
140662f07a6bSKuninori Morimoto 	}
14076fb03550SKuninori Morimoto 
14086fb03550SKuninori Morimoto 	return 0;
14096fb03550SKuninori Morimoto }
14106fb03550SKuninori Morimoto 
1411e8fbd250SKuninori Morimoto static void soc_unbind_aux_dev(struct snd_soc_card *card)
14124893a2ebSKuninori Morimoto {
1413e8fbd250SKuninori Morimoto 	struct snd_soc_component *component, *_component;
1414e8fbd250SKuninori Morimoto 
1415e8fbd250SKuninori Morimoto 	for_each_card_auxs_safe(card, component, _component) {
14164893a2ebSKuninori Morimoto 		component->init = NULL;
14174893a2ebSKuninori Morimoto 		list_del(&component->card_aux_list);
14184893a2ebSKuninori Morimoto 	}
1419e8fbd250SKuninori Morimoto }
14204893a2ebSKuninori Morimoto 
1421bee886f1SKuninori Morimoto static int soc_bind_aux_dev(struct snd_soc_card *card)
1422b19e6e7bSMark Brown {
1423f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1424bee886f1SKuninori Morimoto 	struct snd_soc_aux_dev *aux;
1425bee886f1SKuninori Morimoto 	int i;
14263ca041edSSebastian Reichel 
1427bee886f1SKuninori Morimoto 	for_each_card_pre_auxs(card, i, aux) {
1428f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1429bee886f1SKuninori Morimoto 		component = soc_find_component(&aux->dlc);
1430f2ed6b07SMengdong Lin 		if (!component)
14313dc29b8bSKuninori Morimoto 			return -EPROBE_DEFER;
14323ca041edSSebastian Reichel 
1433bee886f1SKuninori Morimoto 		component->init = aux->init;
1434c2b71c71SKuninori Morimoto 		/* see for_each_card_auxs */
1435d2e3a135SSylwester Nawrocki 		list_add(&component->card_aux_list, &card->aux_comp_list);
1436bee886f1SKuninori Morimoto 	}
1437f2ed6b07SMengdong Lin 	return 0;
1438b19e6e7bSMark Brown }
1439b19e6e7bSMark Brown 
1440f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1441f2ed6b07SMengdong Lin {
144274bd3f92SKuninori Morimoto 	struct snd_soc_component *component;
1443f2ed6b07SMengdong Lin 	int order;
1444f2ed6b07SMengdong Lin 	int ret;
1445f2ed6b07SMengdong Lin 
14461a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
144774bd3f92SKuninori Morimoto 		for_each_card_auxs(card, component) {
144874bd3f92SKuninori Morimoto 			if (component->driver->probe_order != order)
144974bd3f92SKuninori Morimoto 				continue;
145074bd3f92SKuninori Morimoto 
145174bd3f92SKuninori Morimoto 			ret = soc_probe_component(card,	component);
145274bd3f92SKuninori Morimoto 			if (ret < 0)
1453f2ed6b07SMengdong Lin 				return ret;
1454f2ed6b07SMengdong Lin 		}
1455f2ed6b07SMengdong Lin 	}
145665d9361fSLars-Peter Clausen 
145744c69bb1SLars-Peter Clausen 	return 0;
14583ca041edSSebastian Reichel }
14592eea392dSJarkko Nikula 
1460f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
146144c69bb1SLars-Peter Clausen {
1462f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1463f2ed6b07SMengdong Lin 	int order;
146444c69bb1SLars-Peter Clausen 
14651a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1466c2b71c71SKuninori Morimoto 		for_each_card_auxs_safe(card, comp, _comp) {
1467e8fbd250SKuninori Morimoto 			if (comp->driver->remove_order == order)
1468c6619b72SKuninori Morimoto 				soc_remove_component(comp, 1);
14695f3484acSLars-Peter Clausen 		}
14702eea392dSJarkko Nikula 	}
14712eea392dSJarkko Nikula }
14722eea392dSJarkko Nikula 
1473ce64c8b9SLars-Peter Clausen /**
1474ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1475ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1476ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1477ce64c8b9SLars-Peter Clausen  *
1478ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1479ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1480ce64c8b9SLars-Peter Clausen  *
1481ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1482ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1483ce64c8b9SLars-Peter Clausen  *
1484ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1485ce64c8b9SLars-Peter Clausen  */
1486ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1487ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1488ce64c8b9SLars-Peter Clausen {
148976afa643SShreyas NC 	struct snd_soc_dai *cpu_dai;
14900b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
149176afa643SShreyas NC 	unsigned int inv_dai_fmt;
1492ce64c8b9SLars-Peter Clausen 	unsigned int i;
1493ce64c8b9SLars-Peter Clausen 	int ret;
1494ce64c8b9SLars-Peter Clausen 
1495a4be4187SKuninori Morimoto 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
1496ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1497ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1498ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1499ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1500ce64c8b9SLars-Peter Clausen 			return ret;
1501ce64c8b9SLars-Peter Clausen 		}
1502ce64c8b9SLars-Peter Clausen 	}
1503ce64c8b9SLars-Peter Clausen 
15042c7b696aSMarcel Ziswiler 	/*
15052c7b696aSMarcel Ziswiler 	 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
15062c7b696aSMarcel Ziswiler 	 * the component which has non_legacy_dai_naming is Codec
15072c7b696aSMarcel Ziswiler 	 */
1508ce64c8b9SLars-Peter Clausen 	inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1509ce64c8b9SLars-Peter Clausen 	switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1510ce64c8b9SLars-Peter Clausen 	case SND_SOC_DAIFMT_CBM_CFM:
1511ce64c8b9SLars-Peter Clausen 		inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1512ce64c8b9SLars-Peter Clausen 		break;
1513ce64c8b9SLars-Peter Clausen 	case SND_SOC_DAIFMT_CBM_CFS:
1514ce64c8b9SLars-Peter Clausen 		inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1515ce64c8b9SLars-Peter Clausen 		break;
1516ce64c8b9SLars-Peter Clausen 	case SND_SOC_DAIFMT_CBS_CFM:
1517ce64c8b9SLars-Peter Clausen 		inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1518ce64c8b9SLars-Peter Clausen 		break;
1519ce64c8b9SLars-Peter Clausen 	case SND_SOC_DAIFMT_CBS_CFS:
1520ce64c8b9SLars-Peter Clausen 		inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1521ce64c8b9SLars-Peter Clausen 		break;
1522ce64c8b9SLars-Peter Clausen 	}
1523a4be4187SKuninori Morimoto 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
152476afa643SShreyas NC 		unsigned int fmt = dai_fmt;
1525ce64c8b9SLars-Peter Clausen 
152676afa643SShreyas NC 		if (cpu_dai->component->driver->non_legacy_dai_naming)
152776afa643SShreyas NC 			fmt = inv_dai_fmt;
1528ce64c8b9SLars-Peter Clausen 
152976afa643SShreyas NC 		ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
1530ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1531ce64c8b9SLars-Peter Clausen 			dev_warn(cpu_dai->dev,
1532ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1533ce64c8b9SLars-Peter Clausen 			return ret;
1534ce64c8b9SLars-Peter Clausen 		}
153576afa643SShreyas NC 	}
1536ce64c8b9SLars-Peter Clausen 
1537ce64c8b9SLars-Peter Clausen 	return 0;
1538ce64c8b9SLars-Peter Clausen }
1539ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1540ce64c8b9SLars-Peter Clausen 
15411f5a4535STakashi Iwai #ifdef CONFIG_DMI
15422c7b696aSMarcel Ziswiler /*
15438a6a6a38SKuninori Morimoto  * If a DMI filed contain strings in this blacklist (e.g.
15448a6a6a38SKuninori Morimoto  * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
15458a6a6a38SKuninori Morimoto  * as invalid and dropped when setting the card long name from DMI info.
15468a6a6a38SKuninori Morimoto  */
15478a6a6a38SKuninori Morimoto static const char * const dmi_blacklist[] = {
15488a6a6a38SKuninori Morimoto 	"To be filled by OEM",
15498a6a6a38SKuninori Morimoto 	"TBD by OEM",
15508a6a6a38SKuninori Morimoto 	"Default String",
15518a6a6a38SKuninori Morimoto 	"Board Manufacturer",
15528a6a6a38SKuninori Morimoto 	"Board Vendor Name",
15538a6a6a38SKuninori Morimoto 	"Board Product Name",
15548a6a6a38SKuninori Morimoto 	NULL,	/* terminator */
15558a6a6a38SKuninori Morimoto };
15568a6a6a38SKuninori Morimoto 
15578a6a6a38SKuninori Morimoto /*
15582c7b696aSMarcel Ziswiler  * Trim special characters, and replace '-' with '_' since '-' is used to
1559345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1560345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1561345233d7SLiam Girdwood  */
1562345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1563345233d7SLiam Girdwood {
1564345233d7SLiam Girdwood 	int i, j = 0;
1565345233d7SLiam Girdwood 
1566345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1567345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1568345233d7SLiam Girdwood 		    || (name[i] == '_'))
1569345233d7SLiam Girdwood 			name[j++] = name[i];
1570345233d7SLiam Girdwood 		else if (name[i] == '-')
1571345233d7SLiam Girdwood 			name[j++] = '_';
1572345233d7SLiam Girdwood 	}
1573345233d7SLiam Girdwood 
1574345233d7SLiam Girdwood 	name[j] = '\0';
1575345233d7SLiam Girdwood }
1576345233d7SLiam Girdwood 
15772c7b696aSMarcel Ziswiler /*
15782c7b696aSMarcel Ziswiler  * Check if a DMI field is valid, i.e. not containing any string
157998faf436SMengdong Lin  * in the black list.
158098faf436SMengdong Lin  */
158198faf436SMengdong Lin static int is_dmi_valid(const char *field)
158298faf436SMengdong Lin {
158398faf436SMengdong Lin 	int i = 0;
158498faf436SMengdong Lin 
158598faf436SMengdong Lin 	while (dmi_blacklist[i]) {
158698faf436SMengdong Lin 		if (strstr(field, dmi_blacklist[i]))
158798faf436SMengdong Lin 			return 0;
158898faf436SMengdong Lin 		i++;
158946b5a4d2SWu Fengguang 	}
159098faf436SMengdong Lin 
159198faf436SMengdong Lin 	return 1;
159298faf436SMengdong Lin }
159398faf436SMengdong Lin 
15944e01e5dbSJaroslav Kysela /*
15954e01e5dbSJaroslav Kysela  * Append a string to card->dmi_longname with character cleanups.
15964e01e5dbSJaroslav Kysela  */
15974e01e5dbSJaroslav Kysela static void append_dmi_string(struct snd_soc_card *card, const char *str)
15984e01e5dbSJaroslav Kysela {
15994e01e5dbSJaroslav Kysela 	char *dst = card->dmi_longname;
16004e01e5dbSJaroslav Kysela 	size_t dst_len = sizeof(card->dmi_longname);
16014e01e5dbSJaroslav Kysela 	size_t len;
16024e01e5dbSJaroslav Kysela 
16034e01e5dbSJaroslav Kysela 	len = strlen(dst);
16044e01e5dbSJaroslav Kysela 	snprintf(dst + len, dst_len - len, "-%s", str);
16054e01e5dbSJaroslav Kysela 
16064e01e5dbSJaroslav Kysela 	len++;	/* skip the separator "-" */
16074e01e5dbSJaroslav Kysela 	if (len < dst_len)
16084e01e5dbSJaroslav Kysela 		cleanup_dmi_name(dst + len);
16094e01e5dbSJaroslav Kysela }
16104e01e5dbSJaroslav Kysela 
1611345233d7SLiam Girdwood /**
1612345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
1613345233d7SLiam Girdwood  * @card: The card to register DMI names
1614345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
1615345233d7SLiam Girdwood  *
1616345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
1617345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
1618345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
1619345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
1620345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
1621345233d7SLiam Girdwood  * as the sound card long name, in the format of
1622345233d7SLiam Girdwood  * "vendor-product-version-board"
1623345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
1624345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
1625345233d7SLiam Girdwood  * (UCM) configurations for the card.
1626345233d7SLiam Girdwood  *
1627345233d7SLiam Girdwood  * Possible card long names may be:
1628345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
1629345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1630345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1631345233d7SLiam Girdwood  *
1632345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
1633345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
1634345233d7SLiam Girdwood  *
1635345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
1636345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
1637345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
1638345233d7SLiam Girdwood  * charactors like SPACE.
1639345233d7SLiam Girdwood  *
1640345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
1641345233d7SLiam Girdwood  */
1642345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1643345233d7SLiam Girdwood {
1644345233d7SLiam Girdwood 	const char *vendor, *product, *product_version, *board;
1645345233d7SLiam Girdwood 
1646345233d7SLiam Girdwood 	if (card->long_name)
1647345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
1648345233d7SLiam Girdwood 
16494e01e5dbSJaroslav Kysela 	/* make up dmi long name as: vendor-product-version-board */
1650345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
165198faf436SMengdong Lin 	if (!vendor || !is_dmi_valid(vendor)) {
1652345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1653345233d7SLiam Girdwood 		return 0;
1654345233d7SLiam Girdwood 	}
1655345233d7SLiam Girdwood 
16564e01e5dbSJaroslav Kysela 	snprintf(card->dmi_longname, sizeof(card->dmi_longname), "%s", vendor);
1657345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
1658345233d7SLiam Girdwood 
1659345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
166098faf436SMengdong Lin 	if (product && is_dmi_valid(product)) {
16614e01e5dbSJaroslav Kysela 		append_dmi_string(card, product);
1662345233d7SLiam Girdwood 
16632c7b696aSMarcel Ziswiler 		/*
16642c7b696aSMarcel Ziswiler 		 * some vendors like Lenovo may only put a self-explanatory
1665345233d7SLiam Girdwood 		 * name in the product version field
1666345233d7SLiam Girdwood 		 */
1667345233d7SLiam Girdwood 		product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
16684e01e5dbSJaroslav Kysela 		if (product_version && is_dmi_valid(product_version))
16694e01e5dbSJaroslav Kysela 			append_dmi_string(card, product_version);
1670345233d7SLiam Girdwood 	}
1671345233d7SLiam Girdwood 
1672345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
167398faf436SMengdong Lin 	if (board && is_dmi_valid(board)) {
167439870b0dSJaroslav Kysela 		if (!product || strcasecmp(board, product))
16754e01e5dbSJaroslav Kysela 			append_dmi_string(card, board);
1676345233d7SLiam Girdwood 	} else if (!product) {
1677345233d7SLiam Girdwood 		/* fall back to using legacy name */
1678345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1679345233d7SLiam Girdwood 		return 0;
1680345233d7SLiam Girdwood 	}
1681345233d7SLiam Girdwood 
1682345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
16834e01e5dbSJaroslav Kysela 	if (flavour)
16844e01e5dbSJaroslav Kysela 		append_dmi_string(card, flavour);
1685345233d7SLiam Girdwood 
1686345233d7SLiam Girdwood 	/* set the card long name */
1687345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
1688345233d7SLiam Girdwood 
1689345233d7SLiam Girdwood 	return 0;
1690345233d7SLiam Girdwood }
1691345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
16921f5a4535STakashi Iwai #endif /* CONFIG_DMI */
1693345233d7SLiam Girdwood 
1694a655de80SLiam Girdwood static void soc_check_tplg_fes(struct snd_soc_card *card)
1695a655de80SLiam Girdwood {
1696a655de80SLiam Girdwood 	struct snd_soc_component *component;
1697a655de80SLiam Girdwood 	const struct snd_soc_component_driver *comp_drv;
1698a655de80SLiam Girdwood 	struct snd_soc_dai_link *dai_link;
1699a655de80SLiam Girdwood 	int i;
1700a655de80SLiam Girdwood 
1701368dee94SKuninori Morimoto 	for_each_component(component) {
1702a655de80SLiam Girdwood 
170349f9c4f2SDaniel Baluta 		/* does this component override BEs ? */
1704a655de80SLiam Girdwood 		if (!component->driver->ignore_machine)
1705a655de80SLiam Girdwood 			continue;
1706a655de80SLiam Girdwood 
1707a655de80SLiam Girdwood 		/* for this machine ? */
1708e194098bSPierre-Louis Bossart 		if (!strcmp(component->driver->ignore_machine,
1709a655de80SLiam Girdwood 			    card->dev->driver->name))
1710e194098bSPierre-Louis Bossart 			goto match;
1711e194098bSPierre-Louis Bossart 		if (strcmp(component->driver->ignore_machine,
1712e194098bSPierre-Louis Bossart 			   dev_name(card->dev)))
1713a655de80SLiam Girdwood 			continue;
1714e194098bSPierre-Louis Bossart match:
1715a655de80SLiam Girdwood 		/* machine matches, so override the rtd data */
17167fe072b4SKuninori Morimoto 		for_each_card_prelinks(card, i, dai_link) {
1717a655de80SLiam Girdwood 
1718a655de80SLiam Girdwood 			/* ignore this FE */
1719a655de80SLiam Girdwood 			if (dai_link->dynamic) {
1720a655de80SLiam Girdwood 				dai_link->ignore = true;
1721a655de80SLiam Girdwood 				continue;
1722a655de80SLiam Girdwood 			}
1723a655de80SLiam Girdwood 
172449f9c4f2SDaniel Baluta 			dev_info(card->dev, "info: override BE DAI link %s\n",
1725a655de80SLiam Girdwood 				 card->dai_link[i].name);
1726a655de80SLiam Girdwood 
1727a655de80SLiam Girdwood 			/* override platform component */
1728adb76b5bSKuninori Morimoto 			if (!dai_link->platforms) {
1729daecf46eSKuninori Morimoto 				dev_err(card->dev, "init platform error");
1730daecf46eSKuninori Morimoto 				continue;
1731daecf46eSKuninori Morimoto 			}
1732910fdcabSKuninori Morimoto 			dai_link->platforms->name = component->name;
1733a655de80SLiam Girdwood 
1734a655de80SLiam Girdwood 			/* convert non BE into BE */
1735a655de80SLiam Girdwood 			dai_link->no_pcm = 1;
1736218fe9b7SDaniel Baluta 			dai_link->dpcm_playback = 1;
1737218fe9b7SDaniel Baluta 			dai_link->dpcm_capture = 1;
1738a655de80SLiam Girdwood 
1739a655de80SLiam Girdwood 			/* override any BE fixups */
1740a655de80SLiam Girdwood 			dai_link->be_hw_params_fixup =
1741a655de80SLiam Girdwood 				component->driver->be_hw_params_fixup;
1742a655de80SLiam Girdwood 
17432c7b696aSMarcel Ziswiler 			/*
17442c7b696aSMarcel Ziswiler 			 * most BE links don't set stream name, so set it to
1745a655de80SLiam Girdwood 			 * dai link name if it's NULL to help bind widgets.
1746a655de80SLiam Girdwood 			 */
1747a655de80SLiam Girdwood 			if (!dai_link->stream_name)
1748a655de80SLiam Girdwood 				dai_link->stream_name = dai_link->name;
1749a655de80SLiam Girdwood 		}
1750a655de80SLiam Girdwood 
1751a655de80SLiam Girdwood 		/* Inform userspace we are using alternate topology */
1752a655de80SLiam Girdwood 		if (component->driver->topology_name_prefix) {
1753a655de80SLiam Girdwood 
1754a655de80SLiam Girdwood 			/* topology shortname created? */
1755a655de80SLiam Girdwood 			if (!card->topology_shortname_created) {
1756a655de80SLiam Girdwood 				comp_drv = component->driver;
1757a655de80SLiam Girdwood 
1758a655de80SLiam Girdwood 				snprintf(card->topology_shortname, 32, "%s-%s",
1759a655de80SLiam Girdwood 					 comp_drv->topology_name_prefix,
1760a655de80SLiam Girdwood 					 card->name);
1761a655de80SLiam Girdwood 				card->topology_shortname_created = true;
1762a655de80SLiam Girdwood 			}
1763a655de80SLiam Girdwood 
1764a655de80SLiam Girdwood 			/* use topology shortname */
1765a655de80SLiam Girdwood 			card->name = card->topology_shortname;
1766a655de80SLiam Girdwood 		}
1767a655de80SLiam Girdwood 	}
1768a655de80SLiam Girdwood }
1769a655de80SLiam Girdwood 
17700f23f718SKuninori Morimoto #define soc_setup_card_name(name, name1, name2, norm)		\
17710f23f718SKuninori Morimoto 	__soc_setup_card_name(name, sizeof(name), name1, name2, norm)
17720f23f718SKuninori Morimoto static void __soc_setup_card_name(char *name, int len,
17730f23f718SKuninori Morimoto 				  const char *name1, const char *name2,
17740f23f718SKuninori Morimoto 				  int normalization)
17750f23f718SKuninori Morimoto {
17760f23f718SKuninori Morimoto 	int i;
17770f23f718SKuninori Morimoto 
17780f23f718SKuninori Morimoto 	snprintf(name, len, "%s", name1 ? name1 : name2);
17790f23f718SKuninori Morimoto 
17800f23f718SKuninori Morimoto 	if (!normalization)
17810f23f718SKuninori Morimoto 		return;
17820f23f718SKuninori Morimoto 
17830f23f718SKuninori Morimoto 	/*
17840f23f718SKuninori Morimoto 	 * Name normalization
17850f23f718SKuninori Morimoto 	 *
17860f23f718SKuninori Morimoto 	 * The driver name is somewhat special, as it's used as a key for
17870f23f718SKuninori Morimoto 	 * searches in the user-space.
17880f23f718SKuninori Morimoto 	 *
17890f23f718SKuninori Morimoto 	 * ex)
17900f23f718SKuninori Morimoto 	 *	"abcd??efg" -> "abcd__efg"
17910f23f718SKuninori Morimoto 	 */
17920f23f718SKuninori Morimoto 	for (i = 0; i < len; i++) {
17930f23f718SKuninori Morimoto 		switch (name[i]) {
17940f23f718SKuninori Morimoto 		case '_':
17950f23f718SKuninori Morimoto 		case '-':
17960f23f718SKuninori Morimoto 		case '\0':
17970f23f718SKuninori Morimoto 			break;
17980f23f718SKuninori Morimoto 		default:
17990f23f718SKuninori Morimoto 			if (!isalnum(name[i]))
18000f23f718SKuninori Morimoto 				name[i] = '_';
18010f23f718SKuninori Morimoto 			break;
18020f23f718SKuninori Morimoto 		}
18030f23f718SKuninori Morimoto 	}
18040f23f718SKuninori Morimoto }
18050f23f718SKuninori Morimoto 
1806ce21401cSKuninori Morimoto static void soc_cleanup_card_resources(struct snd_soc_card *card,
1807ce21401cSKuninori Morimoto 				       int card_probed)
180853e947a0SKuninori Morimoto {
1809cc733900SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd, *n;
18107ce6088fSKuninori Morimoto 
18110ced7b05SKuninori Morimoto 	if (card->snd_card)
18120ced7b05SKuninori Morimoto 		snd_card_disconnect_sync(card->snd_card);
181353e947a0SKuninori Morimoto 
18142a6f0892SKuninori Morimoto 	snd_soc_dapm_shutdown(card);
18152a6f0892SKuninori Morimoto 
181653e947a0SKuninori Morimoto 	/* remove and free each DAI */
18177ce6088fSKuninori Morimoto 	soc_remove_link_dais(card);
18180ced7b05SKuninori Morimoto 	soc_remove_link_components(card);
18197ce6088fSKuninori Morimoto 
1820cc733900SKuninori Morimoto 	for_each_card_rtds_safe(card, rtd, n)
182150cd9b53SKuninori Morimoto 		snd_soc_remove_pcm_runtime(card, rtd);
18227ce6088fSKuninori Morimoto 
182353e947a0SKuninori Morimoto 	/* remove auxiliary devices */
182453e947a0SKuninori Morimoto 	soc_remove_aux_devices(card);
1825e8fbd250SKuninori Morimoto 	soc_unbind_aux_dev(card);
182653e947a0SKuninori Morimoto 
182753e947a0SKuninori Morimoto 	snd_soc_dapm_free(&card->dapm);
182853e947a0SKuninori Morimoto 	soc_cleanup_card_debugfs(card);
182953e947a0SKuninori Morimoto 
183053e947a0SKuninori Morimoto 	/* remove the card */
1831ce21401cSKuninori Morimoto 	if (card_probed && card->remove)
183253e947a0SKuninori Morimoto 		card->remove(card);
18330ced7b05SKuninori Morimoto 
18340ced7b05SKuninori Morimoto 	if (card->snd_card) {
18350ced7b05SKuninori Morimoto 		snd_card_free(card->snd_card);
18360ced7b05SKuninori Morimoto 		card->snd_card = NULL;
18370ced7b05SKuninori Morimoto 	}
183853e947a0SKuninori Morimoto }
183953e947a0SKuninori Morimoto 
18402cc1afcfSKuninori Morimoto static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
18412cc1afcfSKuninori Morimoto {
18422cc1afcfSKuninori Morimoto 	if (card->instantiated) {
1843ce21401cSKuninori Morimoto 		int card_probed = 1;
1844ce21401cSKuninori Morimoto 
18452cc1afcfSKuninori Morimoto 		card->instantiated = false;
18462cc1afcfSKuninori Morimoto 		snd_soc_flush_all_delayed_work(card);
18472cc1afcfSKuninori Morimoto 
1848ce21401cSKuninori Morimoto 		soc_cleanup_card_resources(card, card_probed);
18492cc1afcfSKuninori Morimoto 		if (!unregister)
18502cc1afcfSKuninori Morimoto 			list_add(&card->list, &unbind_card_list);
18512cc1afcfSKuninori Morimoto 	} else {
18522cc1afcfSKuninori Morimoto 		if (unregister)
18532cc1afcfSKuninori Morimoto 			list_del(&card->list);
18542cc1afcfSKuninori Morimoto 	}
18552cc1afcfSKuninori Morimoto }
18562cc1afcfSKuninori Morimoto 
1857ed90c013SKuninori Morimoto static int snd_soc_bind_card(struct snd_soc_card *card)
1858f0fba2adSLiam Girdwood {
18591a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
186076c39e86SKuninori Morimoto 	struct snd_soc_component *component;
186161b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
1862ce21401cSKuninori Morimoto 	int ret, i, card_probed = 0;
186396dd3622SMark Brown 
186434e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
186501b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1866f0fba2adSLiam Girdwood 
186795c267ddSKuninori Morimoto 	snd_soc_dapm_init(&card->dapm, card, NULL);
186853e947a0SKuninori Morimoto 
1869a655de80SLiam Girdwood 	/* check whether any platform is ignore machine FE and using topology */
1870a655de80SLiam Girdwood 	soc_check_tplg_fes(card);
1871a655de80SLiam Girdwood 
187244c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
1873bee886f1SKuninori Morimoto 	ret = soc_bind_aux_dev(card);
1874bee886f1SKuninori Morimoto 	if (ret < 0)
187553e947a0SKuninori Morimoto 		goto probe_end;
1876db2a4165SFrank Mandarino 
1877f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
1878d8145989SKuninori Morimoto 	card->num_rtd = 0;
18795b99a0aaSKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link) {
18800c048004SKuninori Morimoto 		ret = snd_soc_add_pcm_runtime(card, dai_link);
18815b99a0aaSKuninori Morimoto 		if (ret < 0)
18825b99a0aaSKuninori Morimoto 			goto probe_end;
18835b99a0aaSKuninori Morimoto 	}
1884f8f80361SMengdong Lin 
1885f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1886102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1887f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1888f0fba2adSLiam Girdwood 	if (ret < 0) {
188910e8aa9aSMichał Mirosław 		dev_err(card->dev,
189010e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
189110e8aa9aSMichał Mirosław 			card->name, ret);
189253e947a0SKuninori Morimoto 		goto probe_end;
1893db2a4165SFrank Mandarino 	}
1894db2a4165SFrank Mandarino 
18950757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
18960757d834SLars-Peter Clausen 
1897b3da4251SKuninori Morimoto 	soc_resume_init(card);
18986ed25978SAndy Green 
1899b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
19009a841ebbSMark Brown 					card->num_dapm_widgets);
1901b8ba3b57SKuninori Morimoto 	if (ret < 0)
1902b8ba3b57SKuninori Morimoto 		goto probe_end;
19039a841ebbSMark Brown 
1904b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1905f23e860eSNicolin Chen 					card->num_of_dapm_widgets);
1906b8ba3b57SKuninori Morimoto 	if (ret < 0)
1907b8ba3b57SKuninori Morimoto 		goto probe_end;
1908f23e860eSNicolin Chen 
1909f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1910f0fba2adSLiam Girdwood 	if (card->probe) {
1911e7361ec4SMark Brown 		ret = card->probe(card);
1912f0fba2adSLiam Girdwood 		if (ret < 0)
191353e947a0SKuninori Morimoto 			goto probe_end;
1914ce21401cSKuninori Morimoto 		card_probed = 1;
1915f0fba2adSLiam Girdwood 	}
1916f0fba2adSLiam Girdwood 
191762ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
191862f07a6bSKuninori Morimoto 	ret = soc_probe_link_components(card);
191962ae68faSStephen Warren 	if (ret < 0) {
1920f110bfc7SLiam Girdwood 		dev_err(card->dev,
192162f07a6bSKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
192253e947a0SKuninori Morimoto 		goto probe_end;
192362ae68faSStephen Warren 	}
192462ae68faSStephen Warren 
1925f2ed6b07SMengdong Lin 	/* probe auxiliary components */
1926f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
192774bd3f92SKuninori Morimoto 	if (ret < 0) {
192874bd3f92SKuninori Morimoto 		dev_err(card->dev,
192974bd3f92SKuninori Morimoto 			"ASoC: failed to probe aux component %d\n", ret);
193053e947a0SKuninori Morimoto 		goto probe_end;
193174bd3f92SKuninori Morimoto 	}
1932f2ed6b07SMengdong Lin 
193362ae68faSStephen Warren 	/* probe all DAI links on this card */
1934c7e73774SKuninori Morimoto 	ret = soc_probe_link_dais(card);
1935fe3e78e0SMark Brown 	if (ret < 0) {
1936f110bfc7SLiam Girdwood 		dev_err(card->dev,
1937c7e73774SKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
193853e947a0SKuninori Morimoto 		goto probe_end;
1939fe3e78e0SMark Brown 	}
1940fe3e78e0SMark Brown 
1941626c2e57SKuninori Morimoto 	for_each_card_rtds(card, rtd) {
1942eaffeefbSKuninori Morimoto 		ret = soc_init_pcm_runtime(card, rtd);
1943626c2e57SKuninori Morimoto 		if (ret < 0)
1944626c2e57SKuninori Morimoto 			goto probe_end;
1945626c2e57SKuninori Morimoto 	}
1946c4b46982SKuninori Morimoto 
1947888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
1948b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
1949888df395SMark Brown 
19509b98c7c2SKuninori Morimoto 	ret = snd_soc_add_card_controls(card, card->controls,
19512c7b696aSMarcel Ziswiler 					card->num_controls);
19529b98c7c2SKuninori Morimoto 	if (ret < 0)
19539b98c7c2SKuninori Morimoto 		goto probe_end;
1954b7af1dafSMark Brown 
1955daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1956b8ad29deSMark Brown 				      card->num_dapm_routes);
1957a22ae72bSPierre-Louis Bossart 	if (ret < 0) {
1958a22ae72bSPierre-Louis Bossart 		if (card->disable_route_checks) {
1959a22ae72bSPierre-Louis Bossart 			dev_info(card->dev,
1960a22ae72bSPierre-Louis Bossart 				 "%s: disable_route_checks set, ignoring errors on add_routes\n",
1961a22ae72bSPierre-Louis Bossart 				 __func__);
1962a22ae72bSPierre-Louis Bossart 		} else {
1963a22ae72bSPierre-Louis Bossart 			dev_err(card->dev,
1964a22ae72bSPierre-Louis Bossart 				 "%s: snd_soc_dapm_add_routes failed: %d\n",
1965a22ae72bSPierre-Louis Bossart 				 __func__, ret);
1966daa480bdSKuninori Morimoto 			goto probe_end;
1967a22ae72bSPierre-Louis Bossart 		}
1968a22ae72bSPierre-Louis Bossart 	}
1969b8ad29deSMark Brown 
1970daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
1971f23e860eSNicolin Chen 				      card->num_of_dapm_routes);
1972daa480bdSKuninori Morimoto 	if (ret < 0)
1973daa480bdSKuninori Morimoto 		goto probe_end;
197475d9ac46SMark Brown 
1975861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
1976861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
1977861886d3STakashi Iwai 
19780f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->shortname,
19790f23f718SKuninori Morimoto 			    card->name, NULL, 0);
19800f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->longname,
19810f23f718SKuninori Morimoto 			    card->long_name, card->name, 0);
19820f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->driver,
19830f23f718SKuninori Morimoto 			    card->driver_name, card->name, 1);
1984fe3e78e0SMark Brown 
1985dc73d73aSJaroslav Kysela 	if (card->components) {
1986dc73d73aSJaroslav Kysela 		/* the current implementation of snd_component_add() accepts */
1987dc73d73aSJaroslav Kysela 		/* multiple components in the string separated by space, */
1988dc73d73aSJaroslav Kysela 		/* but the string collision (identical string) check might */
1989dc73d73aSJaroslav Kysela 		/* not work correctly */
1990dc73d73aSJaroslav Kysela 		ret = snd_component_add(card->snd_card, card->components);
1991dc73d73aSJaroslav Kysela 		if (ret < 0) {
1992dc73d73aSJaroslav Kysela 			dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n",
1993dc73d73aSJaroslav Kysela 				card->name, ret);
1994dc73d73aSJaroslav Kysela 			goto probe_end;
1995dc73d73aSJaroslav Kysela 		}
1996dc73d73aSJaroslav Kysela 	}
1997dc73d73aSJaroslav Kysela 
199828e9ad92SMark Brown 	if (card->late_probe) {
199928e9ad92SMark Brown 		ret = card->late_probe(card);
200028e9ad92SMark Brown 		if (ret < 0) {
2001f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
200228e9ad92SMark Brown 				card->name, ret);
200353e947a0SKuninori Morimoto 			goto probe_end;
200428e9ad92SMark Brown 		}
200528e9ad92SMark Brown 	}
2006ce21401cSKuninori Morimoto 	card_probed = 1;
200728e9ad92SMark Brown 
2008824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
20098c193b8dSLars-Peter Clausen 
2010f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2011fe3e78e0SMark Brown 	if (ret < 0) {
2012f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2013f110bfc7SLiam Girdwood 				ret);
201453e947a0SKuninori Morimoto 		goto probe_end;
2015fe3e78e0SMark Brown 	}
2016fe3e78e0SMark Brown 
2017435c5e25SMark Brown 	card->instantiated = 1;
2018882eab6cSTzung-Bi Shih 	dapm_mark_endpoints_dirty(card);
20194f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2020b19e6e7bSMark Brown 
2021ed90c013SKuninori Morimoto 	/* deactivate pins to sleep state */
202276c39e86SKuninori Morimoto 	for_each_card_components(card, component)
202376c39e86SKuninori Morimoto 		if (!component->active)
202476c39e86SKuninori Morimoto 			pinctrl_pm_select_sleep_state(component->dev);
2025ed90c013SKuninori Morimoto 
202653e947a0SKuninori Morimoto probe_end:
202753e947a0SKuninori Morimoto 	if (ret < 0)
2028ce21401cSKuninori Morimoto 		soc_cleanup_card_resources(card, card_probed);
2029db2a4165SFrank Mandarino 
2030f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
203134e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2032db2a4165SFrank Mandarino 
2033b19e6e7bSMark Brown 	return ret;
2034435c5e25SMark Brown }
2035435c5e25SMark Brown 
2036435c5e25SMark Brown /* probes a new socdev */
2037435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2038435c5e25SMark Brown {
2039f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2040435c5e25SMark Brown 
204170a7ca34SVinod Koul 	/*
204270a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
204370a7ca34SVinod Koul 	 * we should not be here in that case so ret error
204470a7ca34SVinod Koul 	 */
204570a7ca34SVinod Koul 	if (!card)
204670a7ca34SVinod Koul 		return -EINVAL;
204770a7ca34SVinod Koul 
2048fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2049f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2050fe4085e8SMark Brown 		 card->name);
2051fe4085e8SMark Brown 
2052435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2053435c5e25SMark Brown 	card->dev = &pdev->dev;
2054f0fba2adSLiam Girdwood 
205528d528c8SMark Brown 	return snd_soc_register_card(card);
2056435c5e25SMark Brown }
2057435c5e25SMark Brown 
2058b0e26485SVinod Koul /* removes a socdev */
2059b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2060b0e26485SVinod Koul {
2061b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2062b0e26485SVinod Koul 
2063c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2064db2a4165SFrank Mandarino 	return 0;
2065db2a4165SFrank Mandarino }
2066db2a4165SFrank Mandarino 
20676f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
206851737470SMark Brown {
20696f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
207076c39e86SKuninori Morimoto 	struct snd_soc_component *component;
207151737470SMark Brown 
207251737470SMark Brown 	if (!card->instantiated)
2073416356fcSMark Brown 		return 0;
207451737470SMark Brown 
20752c7b696aSMarcel Ziswiler 	/*
20762c7b696aSMarcel Ziswiler 	 * Flush out pmdown_time work - we actually do want to run it
20772c7b696aSMarcel Ziswiler 	 * now, we're shutting down so no imminent restart.
20782c7b696aSMarcel Ziswiler 	 */
207965462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
208051737470SMark Brown 
2081f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2082416356fcSMark Brown 
2083988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
208476c39e86SKuninori Morimoto 	for_each_card_components(card, component)
208576c39e86SKuninori Morimoto 		pinctrl_pm_select_sleep_state(component->dev);
2086988e8cc4SNicolin Chen 
2087416356fcSMark Brown 	return 0;
208851737470SMark Brown }
20896f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
209051737470SMark Brown 
20916f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2092b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2093b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2094b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2095b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
20966f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2097b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2098416356fcSMark Brown };
2099deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2100416356fcSMark Brown 
2101db2a4165SFrank Mandarino /* ASoC platform driver */
2102db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2103db2a4165SFrank Mandarino 	.driver		= {
2104db2a4165SFrank Mandarino 		.name		= "soc-audio",
21056f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2106db2a4165SFrank Mandarino 	},
2107db2a4165SFrank Mandarino 	.probe		= soc_probe,
2108db2a4165SFrank Mandarino 	.remove		= soc_remove,
2109db2a4165SFrank Mandarino };
2110db2a4165SFrank Mandarino 
2111096e49d5SMark Brown /**
2112db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2113db2a4165SFrank Mandarino  * @_template: control template
2114db2a4165SFrank Mandarino  * @data: control private data
2115ac11a2b3SMark Brown  * @long_name: control long name
2116efb7ac3fSMark Brown  * @prefix: control name prefix
2117db2a4165SFrank Mandarino  *
2118db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2119db2a4165SFrank Mandarino  *
2120db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2121db2a4165SFrank Mandarino  */
2122db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
21233056557fSMark Brown 				  void *data, const char *long_name,
2124efb7ac3fSMark Brown 				  const char *prefix)
2125db2a4165SFrank Mandarino {
2126db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2127efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2128efb7ac3fSMark Brown 	char *name = NULL;
2129db2a4165SFrank Mandarino 
2130db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2131db2a4165SFrank Mandarino 	template.index = 0;
2132db2a4165SFrank Mandarino 
2133efb7ac3fSMark Brown 	if (!long_name)
2134efb7ac3fSMark Brown 		long_name = template.name;
2135efb7ac3fSMark Brown 
2136efb7ac3fSMark Brown 	if (prefix) {
21372b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2138efb7ac3fSMark Brown 		if (!name)
2139efb7ac3fSMark Brown 			return NULL;
2140efb7ac3fSMark Brown 
2141efb7ac3fSMark Brown 		template.name = name;
2142efb7ac3fSMark Brown 	} else {
2143efb7ac3fSMark Brown 		template.name = long_name;
2144efb7ac3fSMark Brown 	}
2145efb7ac3fSMark Brown 
2146efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2147efb7ac3fSMark Brown 
2148efb7ac3fSMark Brown 	kfree(name);
2149efb7ac3fSMark Brown 
2150efb7ac3fSMark Brown 	return kcontrol;
2151db2a4165SFrank Mandarino }
2152db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2153db2a4165SFrank Mandarino 
2154022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2155022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2156022658beSLiam Girdwood 	const char *prefix, void *data)
2157022658beSLiam Girdwood {
2158022658beSLiam Girdwood 	int err, i;
2159022658beSLiam Girdwood 
2160022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2161022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
21622c7b696aSMarcel Ziswiler 
2163022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2164022658beSLiam Girdwood 						     control->name, prefix));
2165022658beSLiam Girdwood 		if (err < 0) {
2166f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2167f110bfc7SLiam Girdwood 				control->name, err);
2168022658beSLiam Girdwood 			return err;
2169022658beSLiam Girdwood 		}
2170022658beSLiam Girdwood 	}
2171022658beSLiam Girdwood 
2172022658beSLiam Girdwood 	return 0;
2173022658beSLiam Girdwood }
2174022658beSLiam Girdwood 
21754fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
21764fefd698SDimitris Papastamos 					       const char *name)
21774fefd698SDimitris Papastamos {
21784fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
21794fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
21804fefd698SDimitris Papastamos 
21814fefd698SDimitris Papastamos 	if (unlikely(!name))
21824fefd698SDimitris Papastamos 		return NULL;
21834fefd698SDimitris Papastamos 
21844fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
21854fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
21864fefd698SDimitris Papastamos 			return kctl;
21874fefd698SDimitris Papastamos 	return NULL;
21884fefd698SDimitris Papastamos }
21894fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
21904fefd698SDimitris Papastamos 
2191db2a4165SFrank Mandarino /**
21920f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
21930f2780adSLars-Peter Clausen  *
21940f2780adSLars-Peter Clausen  * @component: Component to add controls to
21950f2780adSLars-Peter Clausen  * @controls: Array of controls to add
21960f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
21970f2780adSLars-Peter Clausen  *
21980f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
21990f2780adSLars-Peter Clausen  */
22000f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
22010f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
22020f2780adSLars-Peter Clausen {
22030f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
22040f2780adSLars-Peter Clausen 
22050f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
22060f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
22070f2780adSLars-Peter Clausen }
22080f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
22090f2780adSLars-Peter Clausen 
22100f2780adSLars-Peter Clausen /**
2211022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2212022658beSLiam Girdwood  * Convenience function to add a list of controls.
2213022658beSLiam Girdwood  *
2214022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2215022658beSLiam Girdwood  * @controls: array of controls to add
2216022658beSLiam Girdwood  * @num_controls: number of elements in the array
2217022658beSLiam Girdwood  *
2218022658beSLiam Girdwood  * Return 0 for success, else error.
2219022658beSLiam Girdwood  */
2220022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2221022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2222022658beSLiam Girdwood {
2223022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2224022658beSLiam Girdwood 
2225022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2226022658beSLiam Girdwood 			NULL, soc_card);
2227022658beSLiam Girdwood }
2228022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2229022658beSLiam Girdwood 
2230022658beSLiam Girdwood /**
2231022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2232022658beSLiam Girdwood  * Convienience function to add a list of controls.
2233022658beSLiam Girdwood  *
2234022658beSLiam Girdwood  * @dai: DAI to add controls to
2235022658beSLiam Girdwood  * @controls: array of controls to add
2236022658beSLiam Girdwood  * @num_controls: number of elements in the array
2237022658beSLiam Girdwood  *
2238022658beSLiam Girdwood  * Return 0 for success, else error.
2239022658beSLiam Girdwood  */
2240022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2241022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2242022658beSLiam Girdwood {
2243313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2244022658beSLiam Girdwood 
2245022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2246022658beSLiam Girdwood 			NULL, dai);
2247022658beSLiam Girdwood }
2248022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2249022658beSLiam Girdwood 
2250c5af3a2eSMark Brown /**
2251c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2252c5af3a2eSMark Brown  *
2253ac11a2b3SMark Brown  * @card: Card to register
2254c5af3a2eSMark Brown  *
2255c5af3a2eSMark Brown  */
225670a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2257c5af3a2eSMark Brown {
2258c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2259c5af3a2eSMark Brown 		return -EINVAL;
2260c5af3a2eSMark Brown 
2261ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2262ed77cc12SMark Brown 
2263b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->widgets);
2264b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->paths);
2265b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->dapm_list);
2266b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->aux_comp_list);
2267b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->component_dev_list);
2268b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->list);
22691a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
2270db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
22718a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2272b03bfaecSKuninori Morimoto 
2273c5af3a2eSMark Brown 	card->instantiated = 0;
2274f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
2275a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
227672b745e3SPeter Ujfalusi 	mutex_init(&card->pcm_mutex);
2277a9764869SKaiChieh Chuang 	spin_lock_init(&card->dpcm_lock);
2278c5af3a2eSMark Brown 
2279e894efefSSrinivas Kandagatla 	return snd_soc_bind_card(card);
2280c5af3a2eSMark Brown }
228170a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2282c5af3a2eSMark Brown 
2283c5af3a2eSMark Brown /**
2284c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2285c5af3a2eSMark Brown  *
2286ac11a2b3SMark Brown  * @card: Card to unregister
2287c5af3a2eSMark Brown  *
2288c5af3a2eSMark Brown  */
228970a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2290c5af3a2eSMark Brown {
2291b545542aSKuninori Morimoto 	mutex_lock(&client_mutex);
2292e894efefSSrinivas Kandagatla 	snd_soc_unbind_card(card, true);
2293b545542aSKuninori Morimoto 	mutex_unlock(&client_mutex);
2294f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2295c5af3a2eSMark Brown 
2296c5af3a2eSMark Brown 	return 0;
2297c5af3a2eSMark Brown }
229870a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2299c5af3a2eSMark Brown 
2300f0fba2adSLiam Girdwood /*
2301f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
2302f0fba2adSLiam Girdwood  * and sanitizing names.
2303f0fba2adSLiam Girdwood  */
23040b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2305f0fba2adSLiam Girdwood {
2306f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
2307f0fba2adSLiam Girdwood 	int id1, id2;
2308f0fba2adSLiam Girdwood 
2309f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
2310f0fba2adSLiam Girdwood 		return NULL;
2311f0fba2adSLiam Girdwood 
231258818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2313f0fba2adSLiam Girdwood 
2314f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
2315f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
2316f0fba2adSLiam Girdwood 	if (found) {
2317f0fba2adSLiam Girdwood 		/* get ID */
2318f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2319f0fba2adSLiam Girdwood 
2320f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
2321f0fba2adSLiam Girdwood 			if (*id == -1)
2322f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
2323f0fba2adSLiam Girdwood 		}
2324f0fba2adSLiam Girdwood 
2325f0fba2adSLiam Girdwood 	} else {
2326f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr" */
2327f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2328f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
2329f0fba2adSLiam Girdwood 
2330f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
233105899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
2332f0fba2adSLiam Girdwood 
2333f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
23342c7b696aSMarcel Ziswiler 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
23352c7b696aSMarcel Ziswiler 				 name);
233658818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2337f0fba2adSLiam Girdwood 		} else
2338f0fba2adSLiam Girdwood 			*id = 0;
2339f0fba2adSLiam Girdwood 	}
2340f0fba2adSLiam Girdwood 
234150014499SKuninori Morimoto 	return devm_kstrdup(dev, name, GFP_KERNEL);
2342f0fba2adSLiam Girdwood }
2343f0fba2adSLiam Girdwood 
2344f0fba2adSLiam Girdwood /*
2345f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
2346f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
2347f0fba2adSLiam Girdwood  */
2348f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
2349f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
2350f0fba2adSLiam Girdwood {
2351f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
235210e8aa9aSMichał Mirosław 		dev_err(dev,
235310e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
235410e8aa9aSMichał Mirosław 			dev_name(dev));
2355f0fba2adSLiam Girdwood 		return NULL;
2356f0fba2adSLiam Girdwood 	}
2357f0fba2adSLiam Girdwood 
235850014499SKuninori Morimoto 	return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2359f0fba2adSLiam Girdwood }
2360f0fba2adSLiam Girdwood 
2361ffdbca0bSKuninori Morimoto void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2362e11381f3SKuninori Morimoto {
2363e11381f3SKuninori Morimoto 	dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2364e11381f3SKuninori Morimoto 	list_del(&dai->list);
2365e11381f3SKuninori Morimoto }
2366ffdbca0bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2367e11381f3SKuninori Morimoto 
23687ca24386SKuninori Morimoto /**
23697ca24386SKuninori Morimoto  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
23707ca24386SKuninori Morimoto  *
23717ca24386SKuninori Morimoto  * @component: The component the DAIs are registered for
23727ca24386SKuninori Morimoto  * @dai_drv: DAI driver to use for the DAI
2373bc9a6655SRandy Dunlap  * @legacy_dai_naming: if %true, use legacy single-name format;
2374bc9a6655SRandy Dunlap  * 	if %false, use multiple-name format;
23757ca24386SKuninori Morimoto  *
23767ca24386SKuninori Morimoto  * Topology can use this API to register DAIs when probing a component.
23777ca24386SKuninori Morimoto  * These DAIs's widgets will be freed in the card cleanup and the DAIs
23787ca24386SKuninori Morimoto  * will be freed in the component cleanup.
23797ca24386SKuninori Morimoto  */
23807ca24386SKuninori Morimoto struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
23815e4fb372SMengdong Lin 					 struct snd_soc_dai_driver *dai_drv,
23825e4fb372SMengdong Lin 					 bool legacy_dai_naming)
23835e4fb372SMengdong Lin {
23845e4fb372SMengdong Lin 	struct device *dev = component->dev;
23855e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
23865e4fb372SMengdong Lin 
23875e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
23885e4fb372SMengdong Lin 
23897ca24386SKuninori Morimoto 	lockdep_assert_held(&client_mutex);
23907ca24386SKuninori Morimoto 
239150014499SKuninori Morimoto 	dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
23925e4fb372SMengdong Lin 	if (dai == NULL)
23935e4fb372SMengdong Lin 		return NULL;
23945e4fb372SMengdong Lin 
23955e4fb372SMengdong Lin 	/*
23965e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
23975e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
23985e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
23995e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
24005e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
24015e4fb372SMengdong Lin 	 * component-less anymore.
24025e4fb372SMengdong Lin 	 */
24035e4fb372SMengdong Lin 	if (legacy_dai_naming &&
24045e4fb372SMengdong Lin 	    (dai_drv->id == 0 || dai_drv->name == NULL)) {
24055e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
24065e4fb372SMengdong Lin 	} else {
24075e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
24085e4fb372SMengdong Lin 		if (dai_drv->id)
24095e4fb372SMengdong Lin 			dai->id = dai_drv->id;
24105e4fb372SMengdong Lin 		else
24115e4fb372SMengdong Lin 			dai->id = component->num_dai;
24125e4fb372SMengdong Lin 	}
241350014499SKuninori Morimoto 	if (!dai->name)
24145e4fb372SMengdong Lin 		return NULL;
24155e4fb372SMengdong Lin 
24165e4fb372SMengdong Lin 	dai->component = component;
24175e4fb372SMengdong Lin 	dai->dev = dev;
24185e4fb372SMengdong Lin 	dai->driver = dai_drv;
24195e4fb372SMengdong Lin 	if (!dai->driver->ops)
24205e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
24215e4fb372SMengdong Lin 
242215a0c645SKuninori Morimoto 	/* see for_each_component_dais */
242358bf4179SKuninori Morimoto 	list_add_tail(&dai->list, &component->dai_list);
24245e4fb372SMengdong Lin 	component->num_dai++;
24255e4fb372SMengdong Lin 
24265e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
24275e4fb372SMengdong Lin 	return dai;
24285e4fb372SMengdong Lin }
2429e11381f3SKuninori Morimoto 
24309115171aSMark Brown /**
24313f6674aeSKuninori Morimoto  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
24323f6674aeSKuninori Morimoto  *
24333f6674aeSKuninori Morimoto  * @component: The component for which the DAIs should be unregistered
24343f6674aeSKuninori Morimoto  */
24353f6674aeSKuninori Morimoto static void snd_soc_unregister_dais(struct snd_soc_component *component)
24363f6674aeSKuninori Morimoto {
24373f6674aeSKuninori Morimoto 	struct snd_soc_dai *dai, *_dai;
24383f6674aeSKuninori Morimoto 
2439e11381f3SKuninori Morimoto 	for_each_component_dais_safe(component, dai, _dai)
2440e11381f3SKuninori Morimoto 		snd_soc_unregister_dai(dai);
24413f6674aeSKuninori Morimoto }
24423f6674aeSKuninori Morimoto 
24433f6674aeSKuninori Morimoto /**
2444daf77373SKuninori Morimoto  * snd_soc_register_dais - Register a DAI with the ASoC core
2445daf77373SKuninori Morimoto  *
2446daf77373SKuninori Morimoto  * @component: The component the DAIs are registered for
2447daf77373SKuninori Morimoto  * @dai_drv: DAI driver to use for the DAIs
2448daf77373SKuninori Morimoto  * @count: Number of DAIs
2449daf77373SKuninori Morimoto  */
2450daf77373SKuninori Morimoto static int snd_soc_register_dais(struct snd_soc_component *component,
2451daf77373SKuninori Morimoto 				 struct snd_soc_dai_driver *dai_drv,
2452daf77373SKuninori Morimoto 				 size_t count)
2453daf77373SKuninori Morimoto {
2454daf77373SKuninori Morimoto 	struct snd_soc_dai *dai;
2455daf77373SKuninori Morimoto 	unsigned int i;
2456daf77373SKuninori Morimoto 	int ret;
2457daf77373SKuninori Morimoto 
2458daf77373SKuninori Morimoto 	for (i = 0; i < count; i++) {
245971cb85f5SKuninori Morimoto 		dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
2460daf77373SKuninori Morimoto 				  !component->driver->non_legacy_dai_naming);
2461daf77373SKuninori Morimoto 		if (dai == NULL) {
2462daf77373SKuninori Morimoto 			ret = -ENOMEM;
2463daf77373SKuninori Morimoto 			goto err;
2464daf77373SKuninori Morimoto 		}
2465daf77373SKuninori Morimoto 	}
2466daf77373SKuninori Morimoto 
2467daf77373SKuninori Morimoto 	return 0;
2468daf77373SKuninori Morimoto 
2469daf77373SKuninori Morimoto err:
2470daf77373SKuninori Morimoto 	snd_soc_unregister_dais(component);
2471daf77373SKuninori Morimoto 
2472daf77373SKuninori Morimoto 	return ret;
2473daf77373SKuninori Morimoto }
2474daf77373SKuninori Morimoto 
2475bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
2476bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
2477d191bd8dSKuninori Morimoto {
24788d92bb51SKuninori Morimoto 	INIT_LIST_HEAD(&component->dai_list);
2479495efdb0SKuninori Morimoto 	INIT_LIST_HEAD(&component->dobj_list);
2480495efdb0SKuninori Morimoto 	INIT_LIST_HEAD(&component->card_list);
24818d92bb51SKuninori Morimoto 	mutex_init(&component->io_mutex);
24828d92bb51SKuninori Morimoto 
24836b62fa95SJerome Brunet 	component->name = fmt_single_name(dev, &component->id);
2484bb13109dSLars-Peter Clausen 	if (!component->name) {
2485bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
2486d191bd8dSKuninori Morimoto 		return -ENOMEM;
2487d191bd8dSKuninori Morimoto 	}
2488d191bd8dSKuninori Morimoto 
2489bb13109dSLars-Peter Clausen 	component->dev = dev;
2490bb13109dSLars-Peter Clausen 	component->driver = driver;
2491e2c330b9SLars-Peter Clausen 
2492bb13109dSLars-Peter Clausen 	return 0;
2493d191bd8dSKuninori Morimoto }
2494d191bd8dSKuninori Morimoto 
249520feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2496886f5692SLars-Peter Clausen {
2497886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
249820feb881SLars-Peter Clausen 
2499886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
2500886f5692SLars-Peter Clausen 	if (val_bytes > 0)
2501886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
2502886f5692SLars-Peter Clausen }
250320feb881SLars-Peter Clausen 
2504e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
2505e874bf5fSLars-Peter Clausen 
250620feb881SLars-Peter Clausen /**
25072c7b696aSMarcel Ziswiler  * snd_soc_component_init_regmap() - Initialize regmap instance for the
25082c7b696aSMarcel Ziswiler  *                                   component
250920feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
251020feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
251120feb881SLars-Peter Clausen  *
251220feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
251320feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
251420feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
251520feb881SLars-Peter Clausen  * before the first IO attempt of the component.
251620feb881SLars-Peter Clausen  */
251720feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
251820feb881SLars-Peter Clausen 	struct regmap *regmap)
251920feb881SLars-Peter Clausen {
252020feb881SLars-Peter Clausen 	component->regmap = regmap;
252120feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
2522886f5692SLars-Peter Clausen }
252320feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
252420feb881SLars-Peter Clausen 
252520feb881SLars-Peter Clausen /**
25262c7b696aSMarcel Ziswiler  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
25272c7b696aSMarcel Ziswiler  *                                   component
252820feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
252920feb881SLars-Peter Clausen  *
253020feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
253120feb881SLars-Peter Clausen  * removes the regmap instance from the component.
253220feb881SLars-Peter Clausen  *
253320feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
253420feb881SLars-Peter Clausen  * to initialize the regmap instance.
253520feb881SLars-Peter Clausen  */
253620feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
253720feb881SLars-Peter Clausen {
253820feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
253920feb881SLars-Peter Clausen 	component->regmap = NULL;
254020feb881SLars-Peter Clausen }
254120feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2542886f5692SLars-Peter Clausen 
2543e874bf5fSLars-Peter Clausen #endif
2544d191bd8dSKuninori Morimoto 
2545273d778eSKuninori Morimoto #define ENDIANNESS_MAP(name) \
2546273d778eSKuninori Morimoto 	(SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2547273d778eSKuninori Morimoto static u64 endianness_format_map[] = {
2548273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S16_),
2549273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U16_),
2550273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_),
2551273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_),
2552273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S32_),
2553273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U32_),
2554273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_3),
2555273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_3),
2556273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S20_3),
2557273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U20_3),
2558273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S18_3),
2559273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U18_3),
2560273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT_),
2561273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT64_),
2562273d778eSKuninori Morimoto 	ENDIANNESS_MAP(IEC958_SUBFRAME_),
2563273d778eSKuninori Morimoto };
2564273d778eSKuninori Morimoto 
2565273d778eSKuninori Morimoto /*
2566273d778eSKuninori Morimoto  * Fix up the DAI formats for endianness: codecs don't actually see
2567273d778eSKuninori Morimoto  * the endianness of the data but we're using the CPU format
2568273d778eSKuninori Morimoto  * definitions which do need to include endianness so we ensure that
2569273d778eSKuninori Morimoto  * codec DAIs always have both big and little endian variants set.
2570273d778eSKuninori Morimoto  */
2571273d778eSKuninori Morimoto static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2572273d778eSKuninori Morimoto {
2573273d778eSKuninori Morimoto 	int i;
2574273d778eSKuninori Morimoto 
2575273d778eSKuninori Morimoto 	for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2576273d778eSKuninori Morimoto 		if (stream->formats & endianness_format_map[i])
2577273d778eSKuninori Morimoto 			stream->formats |= endianness_format_map[i];
2578273d778eSKuninori Morimoto }
2579273d778eSKuninori Morimoto 
2580e894efefSSrinivas Kandagatla static void snd_soc_try_rebind_card(void)
2581e894efefSSrinivas Kandagatla {
2582e894efefSSrinivas Kandagatla 	struct snd_soc_card *card, *c;
2583e894efefSSrinivas Kandagatla 
2584b245d273SKuninori Morimoto 	list_for_each_entry_safe(card, c, &unbind_card_list, list)
2585e894efefSSrinivas Kandagatla 		if (!snd_soc_bind_card(card))
2586e894efefSSrinivas Kandagatla 			list_del(&card->list);
2587e894efefSSrinivas Kandagatla }
2588e894efefSSrinivas Kandagatla 
2589486c7978SKuninori Morimoto static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2590486c7978SKuninori Morimoto {
2591b18768f5SKuninori Morimoto 	struct snd_soc_card *card = component->card;
2592b18768f5SKuninori Morimoto 
2593486c7978SKuninori Morimoto 	snd_soc_unregister_dais(component);
2594b18768f5SKuninori Morimoto 
2595b18768f5SKuninori Morimoto 	if (card)
2596b18768f5SKuninori Morimoto 		snd_soc_unbind_card(card, false);
2597b18768f5SKuninori Morimoto 
2598b18768f5SKuninori Morimoto 	list_del(&component->list);
2599486c7978SKuninori Morimoto }
2600486c7978SKuninori Morimoto 
2601e0dac41bSKuninori Morimoto int snd_soc_add_component(struct device *dev,
2602e0dac41bSKuninori Morimoto 			struct snd_soc_component *component,
2603cf9e829eSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2604d191bd8dSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2605d191bd8dSKuninori Morimoto 			int num_dai)
2606d191bd8dSKuninori Morimoto {
2607bb13109dSLars-Peter Clausen 	int ret;
2608273d778eSKuninori Morimoto 	int i;
2609d191bd8dSKuninori Morimoto 
2610b18768f5SKuninori Morimoto 	mutex_lock(&client_mutex);
2611b18768f5SKuninori Morimoto 
2612cf9e829eSKuninori Morimoto 	ret = snd_soc_component_initialize(component, component_driver, dev);
2613bb13109dSLars-Peter Clausen 	if (ret)
2614bb13109dSLars-Peter Clausen 		goto err_free;
2615bb13109dSLars-Peter Clausen 
2616273d778eSKuninori Morimoto 	if (component_driver->endianness) {
2617273d778eSKuninori Morimoto 		for (i = 0; i < num_dai; i++) {
2618273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].playback);
2619273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].capture);
2620273d778eSKuninori Morimoto 		}
2621273d778eSKuninori Morimoto 	}
2622273d778eSKuninori Morimoto 
26230e7b25c6SKuninori Morimoto 	ret = snd_soc_register_dais(component, dai_drv, num_dai);
2624bb13109dSLars-Peter Clausen 	if (ret < 0) {
2625f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
2626bb13109dSLars-Peter Clausen 		goto err_cleanup;
2627bb13109dSLars-Peter Clausen 	}
2628bb13109dSLars-Peter Clausen 
2629b18768f5SKuninori Morimoto 	if (!component->driver->write && !component->driver->read) {
2630b18768f5SKuninori Morimoto 		if (!component->regmap)
2631b18768f5SKuninori Morimoto 			component->regmap = dev_get_regmap(component->dev,
2632b18768f5SKuninori Morimoto 							   NULL);
2633b18768f5SKuninori Morimoto 		if (component->regmap)
2634b18768f5SKuninori Morimoto 			snd_soc_component_setup_regmap(component);
2635b18768f5SKuninori Morimoto 	}
2636bb13109dSLars-Peter Clausen 
2637b18768f5SKuninori Morimoto 	/* see for_each_component */
2638b18768f5SKuninori Morimoto 	list_add(&component->list, &component_list);
2639bb13109dSLars-Peter Clausen 
2640bb13109dSLars-Peter Clausen err_cleanup:
2641b18768f5SKuninori Morimoto 	if (ret < 0)
2642486c7978SKuninori Morimoto 		snd_soc_del_component_unlocked(component);
2643bb13109dSLars-Peter Clausen err_free:
2644b18768f5SKuninori Morimoto 	mutex_unlock(&client_mutex);
2645b18768f5SKuninori Morimoto 
2646b18768f5SKuninori Morimoto 	if (ret == 0)
2647b18768f5SKuninori Morimoto 		snd_soc_try_rebind_card();
2648b18768f5SKuninori Morimoto 
2649bb13109dSLars-Peter Clausen 	return ret;
2650d191bd8dSKuninori Morimoto }
2651e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component);
2652e0dac41bSKuninori Morimoto 
2653e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev,
2654e0dac41bSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2655e0dac41bSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2656e0dac41bSKuninori Morimoto 			int num_dai)
2657e0dac41bSKuninori Morimoto {
2658e0dac41bSKuninori Morimoto 	struct snd_soc_component *component;
2659e0dac41bSKuninori Morimoto 
26607ecbd6a9SKuninori Morimoto 	component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
266108e61d03SKuninori Morimoto 	if (!component)
2662e0dac41bSKuninori Morimoto 		return -ENOMEM;
2663e0dac41bSKuninori Morimoto 
2664e0dac41bSKuninori Morimoto 	return snd_soc_add_component(dev, component, component_driver,
2665e0dac41bSKuninori Morimoto 				     dai_drv, num_dai);
2666e0dac41bSKuninori Morimoto }
2667d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
2668d191bd8dSKuninori Morimoto 
2669d191bd8dSKuninori Morimoto /**
26702eccea8cSKuninori Morimoto  * snd_soc_unregister_component - Unregister all related component
26712eccea8cSKuninori Morimoto  * from the ASoC core
2672d191bd8dSKuninori Morimoto  *
2673628536eaSJonathan Corbet  * @dev: The device to unregister
2674d191bd8dSKuninori Morimoto  */
26752eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
26762eccea8cSKuninori Morimoto {
2677ac6a4dd3SKuninori Morimoto 	struct snd_soc_component *component;
2678ac6a4dd3SKuninori Morimoto 
2679ac6a4dd3SKuninori Morimoto 	mutex_lock(&client_mutex);
2680ac6a4dd3SKuninori Morimoto 	while (1) {
268118dd66eaSKuninori Morimoto 		component = snd_soc_lookup_component_nolocked(dev, NULL);
2682ac6a4dd3SKuninori Morimoto 		if (!component)
2683ac6a4dd3SKuninori Morimoto 			break;
2684ac6a4dd3SKuninori Morimoto 
2685ac6a4dd3SKuninori Morimoto 		snd_soc_del_component_unlocked(component);
2686ac6a4dd3SKuninori Morimoto 	}
2687ac6a4dd3SKuninori Morimoto 	mutex_unlock(&client_mutex);
2688d191bd8dSKuninori Morimoto }
2689d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2690d191bd8dSKuninori Morimoto 
2691bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
2692b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2693bec4fa05SStephen Warren 			       const char *propname)
2694bec4fa05SStephen Warren {
2695b07609ceSKuninori Morimoto 	struct device_node *np;
2696bec4fa05SStephen Warren 	int ret;
2697bec4fa05SStephen Warren 
26987e07e7c0STushar Behera 	if (!card->dev) {
26997e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
27007e07e7c0STushar Behera 		return -EINVAL;
27017e07e7c0STushar Behera 	}
27027e07e7c0STushar Behera 
27037e07e7c0STushar Behera 	np = card->dev->of_node;
27047e07e7c0STushar Behera 
2705bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
2706bec4fa05SStephen Warren 	/*
2707bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
2708bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
2709bec4fa05SStephen Warren 	 * snd_soc_register_card.
2710bec4fa05SStephen Warren 	 */
2711bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
2712bec4fa05SStephen Warren 		dev_err(card->dev,
2713f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
2714bec4fa05SStephen Warren 			propname, ret);
2715bec4fa05SStephen Warren 		return ret;
2716bec4fa05SStephen Warren 	}
2717bec4fa05SStephen Warren 
2718bec4fa05SStephen Warren 	return 0;
2719bec4fa05SStephen Warren }
2720b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2721bec4fa05SStephen Warren 
27229a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
27239a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
27249a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
27259a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
27269a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
27279a6d4860SXiubo Li };
27289a6d4860SXiubo Li 
272921efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
27309a6d4860SXiubo Li 					  const char *propname)
27319a6d4860SXiubo Li {
273221efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
27339a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
27349a6d4860SXiubo Li 	const char *template, *wname;
27359a6d4860SXiubo Li 	int i, j, num_widgets, ret;
27369a6d4860SXiubo Li 
27379a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
27389a6d4860SXiubo Li 	if (num_widgets < 0) {
27399a6d4860SXiubo Li 		dev_err(card->dev,
27409a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
27419a6d4860SXiubo Li 		return -EINVAL;
27429a6d4860SXiubo Li 	}
27439a6d4860SXiubo Li 	if (num_widgets & 1) {
27449a6d4860SXiubo Li 		dev_err(card->dev,
27459a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
27469a6d4860SXiubo Li 		return -EINVAL;
27479a6d4860SXiubo Li 	}
27489a6d4860SXiubo Li 
27499a6d4860SXiubo Li 	num_widgets /= 2;
27509a6d4860SXiubo Li 	if (!num_widgets) {
27519a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
27529a6d4860SXiubo Li 			propname);
27539a6d4860SXiubo Li 		return -EINVAL;
27549a6d4860SXiubo Li 	}
27559a6d4860SXiubo Li 
27569a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
27579a6d4860SXiubo Li 			       GFP_KERNEL);
27589a6d4860SXiubo Li 	if (!widgets) {
27599a6d4860SXiubo Li 		dev_err(card->dev,
27609a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
27619a6d4860SXiubo Li 		return -ENOMEM;
27629a6d4860SXiubo Li 	}
27639a6d4860SXiubo Li 
27649a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
27659a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
27669a6d4860SXiubo Li 			2 * i, &template);
27679a6d4860SXiubo Li 		if (ret) {
27689a6d4860SXiubo Li 			dev_err(card->dev,
27699a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
27709a6d4860SXiubo Li 				propname, 2 * i, ret);
27719a6d4860SXiubo Li 			return -EINVAL;
27729a6d4860SXiubo Li 		}
27739a6d4860SXiubo Li 
27749a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
27759a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
27769a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
27779a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
27789a6d4860SXiubo Li 				break;
27799a6d4860SXiubo Li 			}
27809a6d4860SXiubo Li 		}
27819a6d4860SXiubo Li 
27829a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
27839a6d4860SXiubo Li 			dev_err(card->dev,
27849a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
27859a6d4860SXiubo Li 				template);
27869a6d4860SXiubo Li 			return -EINVAL;
27879a6d4860SXiubo Li 		}
27889a6d4860SXiubo Li 
27899a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
27909a6d4860SXiubo Li 						    (2 * i) + 1,
27919a6d4860SXiubo Li 						    &wname);
27929a6d4860SXiubo Li 		if (ret) {
27939a6d4860SXiubo Li 			dev_err(card->dev,
27949a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
27959a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
27969a6d4860SXiubo Li 			return -EINVAL;
27979a6d4860SXiubo Li 		}
27989a6d4860SXiubo Li 
27999a6d4860SXiubo Li 		widgets[i].name = wname;
28009a6d4860SXiubo Li 	}
28019a6d4860SXiubo Li 
2802f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
2803f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
28049a6d4860SXiubo Li 
28059a6d4860SXiubo Li 	return 0;
28069a6d4860SXiubo Li }
280721efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
28089a6d4860SXiubo Li 
2809cbdfab3bSJerome Brunet int snd_soc_of_get_slot_mask(struct device_node *np,
28106131084aSJyri Sarha 			     const char *prop_name,
28116131084aSJyri Sarha 			     unsigned int *mask)
28126131084aSJyri Sarha {
28136131084aSJyri Sarha 	u32 val;
28146c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
28156131084aSJyri Sarha 	int i;
28166131084aSJyri Sarha 
28176131084aSJyri Sarha 	if (!of_slot_mask)
28186131084aSJyri Sarha 		return 0;
28196131084aSJyri Sarha 	val /= sizeof(u32);
28206131084aSJyri Sarha 	for (i = 0; i < val; i++)
28216131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
28226131084aSJyri Sarha 			*mask |= (1 << i);
28236131084aSJyri Sarha 
28246131084aSJyri Sarha 	return val;
28256131084aSJyri Sarha }
2826cbdfab3bSJerome Brunet EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
28276131084aSJyri Sarha 
282889c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
28296131084aSJyri Sarha 			      unsigned int *tx_mask,
28306131084aSJyri Sarha 			      unsigned int *rx_mask,
283189c67857SXiubo Li 			      unsigned int *slots,
283289c67857SXiubo Li 			      unsigned int *slot_width)
283389c67857SXiubo Li {
283489c67857SXiubo Li 	u32 val;
283589c67857SXiubo Li 	int ret;
283689c67857SXiubo Li 
28376131084aSJyri Sarha 	if (tx_mask)
28386131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
28396131084aSJyri Sarha 	if (rx_mask)
28406131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
28416131084aSJyri Sarha 
284289c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
284389c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
284489c67857SXiubo Li 		if (ret)
284589c67857SXiubo Li 			return ret;
284689c67857SXiubo Li 
284789c67857SXiubo Li 		if (slots)
284889c67857SXiubo Li 			*slots = val;
284989c67857SXiubo Li 	}
285089c67857SXiubo Li 
285189c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
285289c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
285389c67857SXiubo Li 		if (ret)
285489c67857SXiubo Li 			return ret;
285589c67857SXiubo Li 
285689c67857SXiubo Li 		if (slot_width)
285789c67857SXiubo Li 			*slot_width = val;
285889c67857SXiubo Li 	}
285989c67857SXiubo Li 
286089c67857SXiubo Li 	return 0;
286189c67857SXiubo Li }
286289c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
286389c67857SXiubo Li 
28643b710356SKuninori Morimoto void snd_soc_of_parse_node_prefix(struct device_node *np,
28655e3cdaa2SKuninori Morimoto 				  struct snd_soc_codec_conf *codec_conf,
28665e3cdaa2SKuninori Morimoto 				  struct device_node *of_node,
28675e3cdaa2SKuninori Morimoto 				  const char *propname)
28685e3cdaa2SKuninori Morimoto {
28695e3cdaa2SKuninori Morimoto 	const char *str;
28705e3cdaa2SKuninori Morimoto 	int ret;
28715e3cdaa2SKuninori Morimoto 
28725e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
28735e3cdaa2SKuninori Morimoto 	if (ret < 0) {
28745e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
28755e3cdaa2SKuninori Morimoto 		return;
28765e3cdaa2SKuninori Morimoto 	}
28775e3cdaa2SKuninori Morimoto 
2878c13493a2SKuninori Morimoto 	codec_conf->dlc.of_node	= of_node;
28795e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
28805e3cdaa2SKuninori Morimoto }
28813b710356SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
28825e3cdaa2SKuninori Morimoto 
28832bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
2884a4a54dd5SStephen Warren 				   const char *propname)
2885a4a54dd5SStephen Warren {
28862bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
2887e3b1e6a1SMark Brown 	int num_routes;
2888a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
2889a4a54dd5SStephen Warren 	int i, ret;
2890a4a54dd5SStephen Warren 
2891a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
2892c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
289310e8aa9aSMichał Mirosław 		dev_err(card->dev,
289410e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
289510e8aa9aSMichał Mirosław 			propname);
2896a4a54dd5SStephen Warren 		return -EINVAL;
2897a4a54dd5SStephen Warren 	}
2898a4a54dd5SStephen Warren 	num_routes /= 2;
2899a4a54dd5SStephen Warren 	if (!num_routes) {
2900f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
2901a4a54dd5SStephen Warren 			propname);
2902a4a54dd5SStephen Warren 		return -EINVAL;
2903a4a54dd5SStephen Warren 	}
2904a4a54dd5SStephen Warren 
2905a86854d0SKees Cook 	routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
2906a4a54dd5SStephen Warren 			      GFP_KERNEL);
2907a4a54dd5SStephen Warren 	if (!routes) {
2908a4a54dd5SStephen Warren 		dev_err(card->dev,
2909f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
2910a4a54dd5SStephen Warren 		return -EINVAL;
2911a4a54dd5SStephen Warren 	}
2912a4a54dd5SStephen Warren 
2913a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
2914a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
2915e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
2916a4a54dd5SStephen Warren 		if (ret) {
2917c871bd0bSMark Brown 			dev_err(card->dev,
2918c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
2919c871bd0bSMark Brown 				propname, 2 * i, ret);
2920a4a54dd5SStephen Warren 			return -EINVAL;
2921a4a54dd5SStephen Warren 		}
2922a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
2923e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
2924a4a54dd5SStephen Warren 		if (ret) {
2925a4a54dd5SStephen Warren 			dev_err(card->dev,
2926c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
2927c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
2928a4a54dd5SStephen Warren 			return -EINVAL;
2929a4a54dd5SStephen Warren 		}
2930a4a54dd5SStephen Warren 	}
2931a4a54dd5SStephen Warren 
2932f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
2933f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
2934a4a54dd5SStephen Warren 
2935a4a54dd5SStephen Warren 	return 0;
2936a4a54dd5SStephen Warren }
29372bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
2938a4a54dd5SStephen Warren 
2939a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
2940389cb834SJyri Sarha 				     const char *prefix,
2941389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
2942389cb834SJyri Sarha 				     struct device_node **framemaster)
2943a7930ed4SKuninori Morimoto {
2944a7930ed4SKuninori Morimoto 	int ret, i;
2945a7930ed4SKuninori Morimoto 	char prop[128];
2946a7930ed4SKuninori Morimoto 	unsigned int format = 0;
2947a7930ed4SKuninori Morimoto 	int bit, frame;
2948a7930ed4SKuninori Morimoto 	const char *str;
2949a7930ed4SKuninori Morimoto 	struct {
2950a7930ed4SKuninori Morimoto 		char *name;
2951a7930ed4SKuninori Morimoto 		unsigned int val;
2952a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
2953a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
2954a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
2955a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
2956a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
2957a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
2958a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
2959a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
2960a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
2961a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
2962a7930ed4SKuninori Morimoto 	};
2963a7930ed4SKuninori Morimoto 
2964a7930ed4SKuninori Morimoto 	if (!prefix)
2965a7930ed4SKuninori Morimoto 		prefix = "";
2966a7930ed4SKuninori Morimoto 
2967a7930ed4SKuninori Morimoto 	/*
29685711c979SKuninori Morimoto 	 * check "dai-format = xxx"
29695711c979SKuninori Morimoto 	 * or    "[prefix]format = xxx"
2970a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
2971a7930ed4SKuninori Morimoto 	 */
29725711c979SKuninori Morimoto 	ret = of_property_read_string(np, "dai-format", &str);
29735711c979SKuninori Morimoto 	if (ret < 0) {
2974a7930ed4SKuninori Morimoto 		snprintf(prop, sizeof(prop), "%sformat", prefix);
2975a7930ed4SKuninori Morimoto 		ret = of_property_read_string(np, prop, &str);
29765711c979SKuninori Morimoto 	}
2977a7930ed4SKuninori Morimoto 	if (ret == 0) {
2978a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
2979a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
2980a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
2981a7930ed4SKuninori Morimoto 				break;
2982a7930ed4SKuninori Morimoto 			}
2983a7930ed4SKuninori Morimoto 		}
2984a7930ed4SKuninori Morimoto 	}
2985a7930ed4SKuninori Morimoto 
2986a7930ed4SKuninori Morimoto 	/*
29878c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
2988a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
2989a7930ed4SKuninori Morimoto 	 */
29908c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
299151930295SJulia Lawall 	if (of_property_read_bool(np, prop))
29928c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
29938c2d6a9fSKuninori Morimoto 	else
29948c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
2995a7930ed4SKuninori Morimoto 
2996a7930ed4SKuninori Morimoto 	/*
2997a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
2998a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
2999a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3000a7930ed4SKuninori Morimoto 	 */
3001a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3002a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3003a7930ed4SKuninori Morimoto 
3004a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3005a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3006a7930ed4SKuninori Morimoto 
3007a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3008a7930ed4SKuninori Morimoto 	case 0x11:
3009a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3010a7930ed4SKuninori Morimoto 		break;
3011a7930ed4SKuninori Morimoto 	case 0x10:
3012a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3013a7930ed4SKuninori Morimoto 		break;
3014a7930ed4SKuninori Morimoto 	case 0x01:
3015a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3016a7930ed4SKuninori Morimoto 		break;
3017a7930ed4SKuninori Morimoto 	default:
3018a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3019a7930ed4SKuninori Morimoto 		break;
3020a7930ed4SKuninori Morimoto 	}
3021a7930ed4SKuninori Morimoto 
3022a7930ed4SKuninori Morimoto 	/*
3023a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3024a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3025a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3026a7930ed4SKuninori Morimoto 	 */
3027a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3028a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3029389cb834SJyri Sarha 	if (bit && bitclkmaster)
3030389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3031a7930ed4SKuninori Morimoto 
3032a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3033a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3034389cb834SJyri Sarha 	if (frame && framemaster)
3035389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3036a7930ed4SKuninori Morimoto 
3037a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3038a7930ed4SKuninori Morimoto 	case 0x11:
3039a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3040a7930ed4SKuninori Morimoto 		break;
3041a7930ed4SKuninori Morimoto 	case 0x10:
3042a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3043a7930ed4SKuninori Morimoto 		break;
3044a7930ed4SKuninori Morimoto 	case 0x01:
3045a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3046a7930ed4SKuninori Morimoto 		break;
3047a7930ed4SKuninori Morimoto 	default:
3048a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3049a7930ed4SKuninori Morimoto 		break;
3050a7930ed4SKuninori Morimoto 	}
3051a7930ed4SKuninori Morimoto 
3052a7930ed4SKuninori Morimoto 	return format;
3053a7930ed4SKuninori Morimoto }
3054a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3055a7930ed4SKuninori Morimoto 
3056a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
3057a180e8b9SKuninori Morimoto {
305809d4cc03SKuninori Morimoto 	struct snd_soc_component *component;
3059c1e230f0SKuninori Morimoto 	struct snd_soc_dai_link_component dlc;
3060a180e8b9SKuninori Morimoto 	int ret;
3061a180e8b9SKuninori Morimoto 
3062c1e230f0SKuninori Morimoto 	dlc.of_node	= of_graph_get_port_parent(ep);
3063c1e230f0SKuninori Morimoto 	dlc.name	= NULL;
3064a180e8b9SKuninori Morimoto 	/*
3065a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
3066a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
3067a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
3068a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
3069a180e8b9SKuninori Morimoto 	 */
3070a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
3071a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
3072c1e230f0SKuninori Morimoto 	component = soc_find_component(&dlc);
30732c7b1704SKuninori Morimoto 	if (component)
30742c7b1704SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_id(component, ep);
3075a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
3076a180e8b9SKuninori Morimoto 
3077c1e230f0SKuninori Morimoto 	of_node_put(dlc.of_node);
3078c0a480d1STony Lindgren 
3079a180e8b9SKuninori Morimoto 	return ret;
3080a180e8b9SKuninori Morimoto }
3081a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3082a180e8b9SKuninori Morimoto 
30831ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args,
3084cb470087SKuninori Morimoto 				const char **dai_name)
3085cb470087SKuninori Morimoto {
3086cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
30873e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
308893b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3089cb470087SKuninori Morimoto 
3090cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3091368dee94SKuninori Morimoto 	for_each_component(pos) {
3092c0834440SKuninori Morimoto 		component_of_node = soc_component_to_node(pos);
30933e0aa8d8SJyri Sarha 
30943e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
3095cb470087SKuninori Morimoto 			continue;
3096cb470087SKuninori Morimoto 
3097a2a34175SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3098a2a34175SKuninori Morimoto 		if (ret == -ENOTSUPP) {
309958bf4179SKuninori Morimoto 			struct snd_soc_dai *dai;
31006833c452SKuninori Morimoto 			int id = -1;
31016833c452SKuninori Morimoto 
310293b0f3eeSJean-Francois Moine 			switch (args->args_count) {
31036833c452SKuninori Morimoto 			case 0:
31046833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
31056833c452SKuninori Morimoto 				break;
31066833c452SKuninori Morimoto 			case 1:
310793b0f3eeSJean-Francois Moine 				id = args->args[0];
31086833c452SKuninori Morimoto 				break;
31096833c452SKuninori Morimoto 			default:
31106833c452SKuninori Morimoto 				/* not supported */
3111cb470087SKuninori Morimoto 				break;
3112cb470087SKuninori Morimoto 			}
3113cb470087SKuninori Morimoto 
31146833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
31156833c452SKuninori Morimoto 				ret = -EINVAL;
31163dcba280SNicolin Chen 				continue;
31176833c452SKuninori Morimoto 			}
3118e41975edSXiubo Li 
3119e41975edSXiubo Li 			ret = 0;
3120e41975edSXiubo Li 
312158bf4179SKuninori Morimoto 			/* find target DAI */
312215a0c645SKuninori Morimoto 			for_each_component_dais(pos, dai) {
312358bf4179SKuninori Morimoto 				if (id == 0)
312458bf4179SKuninori Morimoto 					break;
312558bf4179SKuninori Morimoto 				id--;
312658bf4179SKuninori Morimoto 			}
312758bf4179SKuninori Morimoto 
312858bf4179SKuninori Morimoto 			*dai_name = dai->driver->name;
3129e41975edSXiubo Li 			if (!*dai_name)
3130e41975edSXiubo Li 				*dai_name = pos->name;
31311dfa5a5aSJerome Brunet 		} else if (ret) {
31321dfa5a5aSJerome Brunet 			/*
31331dfa5a5aSJerome Brunet 			 * if another error than ENOTSUPP is returned go on and
31341dfa5a5aSJerome Brunet 			 * check if another component is provided with the same
31351dfa5a5aSJerome Brunet 			 * node. This may happen if a device provides several
31361dfa5a5aSJerome Brunet 			 * components
31371dfa5a5aSJerome Brunet 			 */
31381dfa5a5aSJerome Brunet 			continue;
31396833c452SKuninori Morimoto 		}
31406833c452SKuninori Morimoto 
3141cb470087SKuninori Morimoto 		break;
3142cb470087SKuninori Morimoto 	}
3143cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
314493b0f3eeSJean-Francois Moine 	return ret;
314593b0f3eeSJean-Francois Moine }
31461ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
314793b0f3eeSJean-Francois Moine 
314893b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
314993b0f3eeSJean-Francois Moine 			    const char **dai_name)
315093b0f3eeSJean-Francois Moine {
315193b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
315293b0f3eeSJean-Francois Moine 	int ret;
315393b0f3eeSJean-Francois Moine 
315493b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
315593b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
315693b0f3eeSJean-Francois Moine 	if (ret)
315793b0f3eeSJean-Francois Moine 		return ret;
315893b0f3eeSJean-Francois Moine 
315993b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3160cb470087SKuninori Morimoto 
3161cb470087SKuninori Morimoto 	of_node_put(args.np);
3162cb470087SKuninori Morimoto 
3163cb470087SKuninori Morimoto 	return ret;
3164cb470087SKuninori Morimoto }
3165cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3166cb470087SKuninori Morimoto 
316793b0f3eeSJean-Francois Moine /*
316894685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
316994685763SSylwester Nawrocki  * @dai_link: DAI link
317094685763SSylwester Nawrocki  *
317194685763SSylwester Nawrocki  * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
317294685763SSylwester Nawrocki  */
317394685763SSylwester Nawrocki void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
317494685763SSylwester Nawrocki {
31753db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *component;
317694685763SSylwester Nawrocki 	int index;
317794685763SSylwester Nawrocki 
31783db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
317994685763SSylwester Nawrocki 		if (!component->of_node)
318094685763SSylwester Nawrocki 			break;
318194685763SSylwester Nawrocki 		of_node_put(component->of_node);
318294685763SSylwester Nawrocki 		component->of_node = NULL;
318394685763SSylwester Nawrocki 	}
318494685763SSylwester Nawrocki }
318594685763SSylwester Nawrocki EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
318694685763SSylwester Nawrocki 
318794685763SSylwester Nawrocki /*
318893b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
318993b0f3eeSJean-Francois Moine  * @dev: Card device
319093b0f3eeSJean-Francois Moine  * @of_node: Device node
319193b0f3eeSJean-Francois Moine  * @dai_link: DAI link
319293b0f3eeSJean-Francois Moine  *
319393b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
319493b0f3eeSJean-Francois Moine  * 'sound-dai'.
319593b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
319694685763SSylwester Nawrocki  * The device nodes in the array (of_node) must be dereferenced by calling
319794685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs() on @dai_link.
319893b0f3eeSJean-Francois Moine  *
319993b0f3eeSJean-Francois Moine  * Returns 0 for success
320093b0f3eeSJean-Francois Moine  */
320193b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
320293b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
320393b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
320493b0f3eeSJean-Francois Moine {
320593b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
320693b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
320793b0f3eeSJean-Francois Moine 	char *name;
320893b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
320993b0f3eeSJean-Francois Moine 
321093b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
321193b0f3eeSJean-Francois Moine 	name = "sound-dai";
321293b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
321393b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
321493b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
321593b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
321693b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
321793b0f3eeSJean-Francois Moine 		else
321893b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
321993b0f3eeSJean-Francois Moine 		return num_codecs;
322093b0f3eeSJean-Francois Moine 	}
3221a86854d0SKees Cook 	component = devm_kcalloc(dev,
3222a86854d0SKees Cook 				 num_codecs, sizeof(*component),
322393b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
322493b0f3eeSJean-Francois Moine 	if (!component)
322593b0f3eeSJean-Francois Moine 		return -ENOMEM;
322693b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
322793b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
322893b0f3eeSJean-Francois Moine 
322993b0f3eeSJean-Francois Moine 	/* Parse the list */
32303db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
323193b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
323293b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
323393b0f3eeSJean-Francois Moine 						 index, &args);
323493b0f3eeSJean-Francois Moine 		if (ret)
323593b0f3eeSJean-Francois Moine 			goto err;
323693b0f3eeSJean-Francois Moine 		component->of_node = args.np;
323793b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
323893b0f3eeSJean-Francois Moine 		if (ret < 0)
323993b0f3eeSJean-Francois Moine 			goto err;
324093b0f3eeSJean-Francois Moine 	}
324193b0f3eeSJean-Francois Moine 	return 0;
324293b0f3eeSJean-Francois Moine err:
324394685763SSylwester Nawrocki 	snd_soc_of_put_dai_link_codecs(dai_link);
324493b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
324593b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
324693b0f3eeSJean-Francois Moine 	return ret;
324793b0f3eeSJean-Francois Moine }
324893b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
324993b0f3eeSJean-Francois Moine 
3250c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3251db2a4165SFrank Mandarino {
32526553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
3253fb257897SMark Brown 	snd_soc_util_init();
3254fb257897SMark Brown 
3255db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3256db2a4165SFrank Mandarino }
32574abe8e16SMark Brown module_init(snd_soc_init);
3258db2a4165SFrank Mandarino 
32597d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3260db2a4165SFrank Mandarino {
3261fb257897SMark Brown 	snd_soc_util_exit();
32626553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3263fb257897SMark Brown 
3264db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3265db2a4165SFrank Mandarino }
3266db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3267db2a4165SFrank Mandarino 
3268db2a4165SFrank Mandarino /* Module information */
3269d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3270db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3271db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
32728b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3273