xref: /openbmc/linux/sound/soc/soc-core.c (revision bcb1fd1fcd6507ba5a1f8610550135dc367aedb7)
1873486edSKuninori Morimoto // SPDX-License-Identifier: GPL-2.0+
2873486edSKuninori Morimoto //
3873486edSKuninori Morimoto // soc-core.c  --  ALSA SoC Audio Layer
4873486edSKuninori Morimoto //
5873486edSKuninori Morimoto // Copyright 2005 Wolfson Microelectronics PLC.
6873486edSKuninori Morimoto // Copyright 2005 Openedhand Ltd.
7873486edSKuninori Morimoto // Copyright (C) 2010 Slimlogic Ltd.
8873486edSKuninori Morimoto // Copyright (C) 2010 Texas Instruments Inc.
9873486edSKuninori Morimoto //
10873486edSKuninori Morimoto // Author: Liam Girdwood <lrg@slimlogic.co.uk>
11873486edSKuninori Morimoto //         with code, comments and ideas from :-
12873486edSKuninori Morimoto //         Richard Purdie <richard@openedhand.com>
13873486edSKuninori Morimoto //
14873486edSKuninori Morimoto //  TODO:
15873486edSKuninori Morimoto //   o Add hw rules to enforce rates, etc.
16873486edSKuninori Morimoto //   o More testing with other codecs/machines.
17873486edSKuninori Morimoto //   o Add more codecs and platforms to ensure good API coverage.
18873486edSKuninori Morimoto //   o Support TDM on PCM and I2S
19db2a4165SFrank Mandarino 
20db2a4165SFrank Mandarino #include <linux/module.h>
21db2a4165SFrank Mandarino #include <linux/moduleparam.h>
22db2a4165SFrank Mandarino #include <linux/init.h>
23db2a4165SFrank Mandarino #include <linux/delay.h>
24db2a4165SFrank Mandarino #include <linux/pm.h>
25db2a4165SFrank Mandarino #include <linux/bitops.h>
2612ef193dSTroy Kisky #include <linux/debugfs.h>
27db2a4165SFrank Mandarino #include <linux/platform_device.h>
28741a509fSMarkus Pargmann #include <linux/pinctrl/consumer.h>
29f0e8ed85SMark Brown #include <linux/ctype.h>
305a0e3ad6STejun Heo #include <linux/slab.h>
31bec4fa05SStephen Warren #include <linux/of.h>
32a180e8b9SKuninori Morimoto #include <linux/of_graph.h>
33345233d7SLiam Girdwood #include <linux/dmi.h>
34db2a4165SFrank Mandarino #include <sound/core.h>
353028eb8cSMark Brown #include <sound/jack.h>
36db2a4165SFrank Mandarino #include <sound/pcm.h>
37db2a4165SFrank Mandarino #include <sound/pcm_params.h>
38db2a4165SFrank Mandarino #include <sound/soc.h>
3901d7584cSLiam Girdwood #include <sound/soc-dpcm.h>
408a978234SLiam Girdwood #include <sound/soc-topology.h>
41db2a4165SFrank Mandarino #include <sound/initval.h>
42db2a4165SFrank Mandarino 
43a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
44a8b1d34fSMark Brown #include <trace/events/asoc.h>
45a8b1d34fSMark Brown 
46f0fba2adSLiam Girdwood #define NAME_SIZE	32
47f0fba2adSLiam Girdwood 
48384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
498a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root;
508a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
51384c89e2SMark Brown #endif
52384c89e2SMark Brown 
53c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
54030e79f6SKuninori Morimoto static LIST_HEAD(component_list);
55e894efefSSrinivas Kandagatla static LIST_HEAD(unbind_card_list);
56c5af3a2eSMark Brown 
57db2a4165SFrank Mandarino /*
58db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
59db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
60db2a4165SFrank Mandarino  * between two audio tracks.
61db2a4165SFrank Mandarino  */
62db2a4165SFrank Mandarino static int pmdown_time = 5000;
63db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
64db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
65db2a4165SFrank Mandarino 
6698faf436SMengdong Lin /* If a DMI filed contain strings in this blacklist (e.g.
6798faf436SMengdong Lin  * "Type2 - Board Manufacturer" or  "Type1 - TBD by OEM"), it will be taken
6898faf436SMengdong Lin  * as invalid and dropped when setting the card long name from DMI info.
6998faf436SMengdong Lin  */
7098faf436SMengdong Lin static const char * const dmi_blacklist[] = {
7198faf436SMengdong Lin 	"To be filled by OEM",
7298faf436SMengdong Lin 	"TBD by OEM",
7398faf436SMengdong Lin 	"Default String",
7498faf436SMengdong Lin 	"Board Manufacturer",
7598faf436SMengdong Lin 	"Board Vendor Name",
7698faf436SMengdong Lin 	"Board Product Name",
7798faf436SMengdong Lin 	NULL,	/* terminator */
7898faf436SMengdong Lin };
7998faf436SMengdong Lin 
80dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
81dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
82dbe21408SMark Brown {
8336ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
84dbe21408SMark Brown 
85f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
86dbe21408SMark Brown }
87dbe21408SMark Brown 
88dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
89dbe21408SMark Brown 			       struct device_attribute *attr,
90dbe21408SMark Brown 			       const char *buf, size_t count)
91dbe21408SMark Brown {
9236ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
93c593b520SMark Brown 	int ret;
94dbe21408SMark Brown 
95b785a492SJingoo Han 	ret = kstrtol(buf, 10, &rtd->pmdown_time);
96c593b520SMark Brown 	if (ret)
97c593b520SMark Brown 		return ret;
98dbe21408SMark Brown 
99dbe21408SMark Brown 	return count;
100dbe21408SMark Brown }
101dbe21408SMark Brown 
102dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
103dbe21408SMark Brown 
104d29697dcSTakashi Iwai static struct attribute *soc_dev_attrs[] = {
105d29697dcSTakashi Iwai 	&dev_attr_pmdown_time.attr,
106d29697dcSTakashi Iwai 	NULL
107d29697dcSTakashi Iwai };
108d29697dcSTakashi Iwai 
109d29697dcSTakashi Iwai static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
110d29697dcSTakashi Iwai 				       struct attribute *attr, int idx)
111d29697dcSTakashi Iwai {
112d29697dcSTakashi Iwai 	struct device *dev = kobj_to_dev(kobj);
113d29697dcSTakashi Iwai 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
114d29697dcSTakashi Iwai 
115d29697dcSTakashi Iwai 	if (attr == &dev_attr_pmdown_time.attr)
116d29697dcSTakashi Iwai 		return attr->mode; /* always visible */
1173b6eed8dSKuninori Morimoto 	return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
118d29697dcSTakashi Iwai }
119d29697dcSTakashi Iwai 
120d29697dcSTakashi Iwai static const struct attribute_group soc_dapm_dev_group = {
121d29697dcSTakashi Iwai 	.attrs = soc_dapm_dev_attrs,
122d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
123d29697dcSTakashi Iwai };
124d29697dcSTakashi Iwai 
125f7e73b26SMark Brown static const struct attribute_group soc_dev_group = {
126d29697dcSTakashi Iwai 	.attrs = soc_dev_attrs,
127d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
128d29697dcSTakashi Iwai };
129d29697dcSTakashi Iwai 
130d29697dcSTakashi Iwai static const struct attribute_group *soc_dev_attr_groups[] = {
131d29697dcSTakashi Iwai 	&soc_dapm_dev_group,
132f7e73b26SMark Brown 	&soc_dev_group,
133d29697dcSTakashi Iwai 	NULL
134d29697dcSTakashi Iwai };
135d29697dcSTakashi Iwai 
1362624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
13781c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component)
138e73f3de5SRussell King {
1396553bf06SLars-Peter Clausen 	if (!component->card->debugfs_card_root)
1406553bf06SLars-Peter Clausen 		return;
1416553bf06SLars-Peter Clausen 
14281c7cfd1SLars-Peter Clausen 	if (component->debugfs_prefix) {
14381c7cfd1SLars-Peter Clausen 		char *name;
144e73f3de5SRussell King 
14581c7cfd1SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s:%s",
14681c7cfd1SLars-Peter Clausen 			component->debugfs_prefix, component->name);
14781c7cfd1SLars-Peter Clausen 		if (name) {
14881c7cfd1SLars-Peter Clausen 			component->debugfs_root = debugfs_create_dir(name,
14981c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
15081c7cfd1SLars-Peter Clausen 			kfree(name);
15181c7cfd1SLars-Peter Clausen 		}
15281c7cfd1SLars-Peter Clausen 	} else {
15381c7cfd1SLars-Peter Clausen 		component->debugfs_root = debugfs_create_dir(component->name,
15481c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
155e73f3de5SRussell King 	}
156e73f3de5SRussell King 
15781c7cfd1SLars-Peter Clausen 	if (!component->debugfs_root) {
15881c7cfd1SLars-Peter Clausen 		dev_warn(component->dev,
15981c7cfd1SLars-Peter Clausen 			"ASoC: Failed to create component debugfs directory\n");
1602624d5faSMark Brown 		return;
1612624d5faSMark Brown 	}
1622624d5faSMark Brown 
16381c7cfd1SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
16481c7cfd1SLars-Peter Clausen 		component->debugfs_root);
16581c7cfd1SLars-Peter Clausen }
16681c7cfd1SLars-Peter Clausen 
16781c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
16881c7cfd1SLars-Peter Clausen {
16981c7cfd1SLars-Peter Clausen 	debugfs_remove_recursive(component->debugfs_root);
17081c7cfd1SLars-Peter Clausen }
17181c7cfd1SLars-Peter Clausen 
172c15b2a1dSPeng Donglin static int dai_list_show(struct seq_file *m, void *v)
173f3208780SMark Brown {
1741438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
175f3208780SMark Brown 	struct snd_soc_dai *dai;
176f3208780SMark Brown 
17734e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
17834e81ab4SLars-Peter Clausen 
179700c17caSDonglin Peng 	list_for_each_entry(component, &component_list, list)
180700c17caSDonglin Peng 		list_for_each_entry(dai, &component->dai_list, list)
181700c17caSDonglin Peng 			seq_printf(m, "%s\n", dai->name);
182f3208780SMark Brown 
18334e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
18434e81ab4SLars-Peter Clausen 
185700c17caSDonglin Peng 	return 0;
186700c17caSDonglin Peng }
187c15b2a1dSPeng Donglin DEFINE_SHOW_ATTRIBUTE(dai_list);
188f3208780SMark Brown 
189db795f9bSKuninori Morimoto static int component_list_show(struct seq_file *m, void *v)
190db795f9bSKuninori Morimoto {
191db795f9bSKuninori Morimoto 	struct snd_soc_component *component;
192db795f9bSKuninori Morimoto 
193db795f9bSKuninori Morimoto 	mutex_lock(&client_mutex);
194db795f9bSKuninori Morimoto 
195db795f9bSKuninori Morimoto 	list_for_each_entry(component, &component_list, list)
196db795f9bSKuninori Morimoto 		seq_printf(m, "%s\n", component->name);
197db795f9bSKuninori Morimoto 
198db795f9bSKuninori Morimoto 	mutex_unlock(&client_mutex);
199db795f9bSKuninori Morimoto 
200db795f9bSKuninori Morimoto 	return 0;
201db795f9bSKuninori Morimoto }
202db795f9bSKuninori Morimoto DEFINE_SHOW_ATTRIBUTE(component_list);
203db795f9bSKuninori Morimoto 
204a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
205a6052154SJarkko Nikula {
2066553bf06SLars-Peter Clausen 	if (!snd_soc_debugfs_root)
2076553bf06SLars-Peter Clausen 		return;
2086553bf06SLars-Peter Clausen 
209a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
2108a9dab1aSMark Brown 						     snd_soc_debugfs_root);
2113a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
212a6052154SJarkko Nikula 		dev_warn(card->dev,
2137c08be84SLothar Waßmann 			 "ASoC: Failed to create card debugfs directory\n");
2143a45b867SJarkko Nikula 		return;
2153a45b867SJarkko Nikula 	}
2163a45b867SJarkko Nikula 
2173a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
2183a45b867SJarkko Nikula 						    card->debugfs_card_root,
2193a45b867SJarkko Nikula 						    &card->pop_time);
2203a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
2213a45b867SJarkko Nikula 		dev_warn(card->dev,
222f110bfc7SLiam Girdwood 		       "ASoC: Failed to create pop time debugfs file\n");
223a6052154SJarkko Nikula }
224a6052154SJarkko Nikula 
225a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
226a6052154SJarkko Nikula {
227a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
228a6052154SJarkko Nikula }
229a6052154SJarkko Nikula 
2306553bf06SLars-Peter Clausen static void snd_soc_debugfs_init(void)
2316553bf06SLars-Peter Clausen {
2326553bf06SLars-Peter Clausen 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
233d9a02c55SFabio Estevam 	if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) {
2346553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create debugfs directory\n");
2356553bf06SLars-Peter Clausen 		snd_soc_debugfs_root = NULL;
2366553bf06SLars-Peter Clausen 		return;
2376553bf06SLars-Peter Clausen 	}
2386553bf06SLars-Peter Clausen 
2396553bf06SLars-Peter Clausen 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
2406553bf06SLars-Peter Clausen 				 &dai_list_fops))
2416553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
242db795f9bSKuninori Morimoto 
243db795f9bSKuninori Morimoto 	if (!debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
244db795f9bSKuninori Morimoto 				 &component_list_fops))
245db795f9bSKuninori Morimoto 		pr_warn("ASoC: Failed to create component list debugfs file\n");
2466553bf06SLars-Peter Clausen }
2476553bf06SLars-Peter Clausen 
2486553bf06SLars-Peter Clausen static void snd_soc_debugfs_exit(void)
2496553bf06SLars-Peter Clausen {
2506553bf06SLars-Peter Clausen 	debugfs_remove_recursive(snd_soc_debugfs_root);
2516553bf06SLars-Peter Clausen }
2526553bf06SLars-Peter Clausen 
2532624d5faSMark Brown #else
2542624d5faSMark Brown 
25581c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs(
25681c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
2572624d5faSMark Brown {
2582624d5faSMark Brown }
2592624d5faSMark Brown 
26081c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs(
26181c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
262731f1ab2SSebastien Guiriec {
263731f1ab2SSebastien Guiriec }
264731f1ab2SSebastien Guiriec 
265b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
266b95fccbcSAxel Lin {
267b95fccbcSAxel Lin }
268b95fccbcSAxel Lin 
269b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
270b95fccbcSAxel Lin {
271b95fccbcSAxel Lin }
2726553bf06SLars-Peter Clausen 
2736553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_init(void)
2746553bf06SLars-Peter Clausen {
2756553bf06SLars-Peter Clausen }
2766553bf06SLars-Peter Clausen 
2776553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_exit(void)
2786553bf06SLars-Peter Clausen {
2796553bf06SLars-Peter Clausen }
2806553bf06SLars-Peter Clausen 
2812624d5faSMark Brown #endif
2822624d5faSMark Brown 
283a0ac4411SKuninori Morimoto static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
284a0ac4411SKuninori Morimoto 			      struct snd_soc_component *component)
285a0ac4411SKuninori Morimoto {
286a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
287a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *new_rtdcom;
288a0ac4411SKuninori Morimoto 
289a0ac4411SKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
290a0ac4411SKuninori Morimoto 		/* already connected */
291a0ac4411SKuninori Morimoto 		if (rtdcom->component == component)
292a0ac4411SKuninori Morimoto 			return 0;
293a0ac4411SKuninori Morimoto 	}
294a0ac4411SKuninori Morimoto 
295a0ac4411SKuninori Morimoto 	new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
296a0ac4411SKuninori Morimoto 	if (!new_rtdcom)
297a0ac4411SKuninori Morimoto 		return -ENOMEM;
298a0ac4411SKuninori Morimoto 
299a0ac4411SKuninori Morimoto 	new_rtdcom->component = component;
300a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&new_rtdcom->list);
301a0ac4411SKuninori Morimoto 
302a0ac4411SKuninori Morimoto 	list_add_tail(&new_rtdcom->list, &rtd->component_list);
303a0ac4411SKuninori Morimoto 
304a0ac4411SKuninori Morimoto 	return 0;
305a0ac4411SKuninori Morimoto }
306a0ac4411SKuninori Morimoto 
307a0ac4411SKuninori Morimoto static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
308a0ac4411SKuninori Morimoto {
309a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
310a0ac4411SKuninori Morimoto 
311a0ac4411SKuninori Morimoto 	for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
312a0ac4411SKuninori Morimoto 		kfree(rtdcom1);
313a0ac4411SKuninori Morimoto 
314a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
315a0ac4411SKuninori Morimoto }
316a0ac4411SKuninori Morimoto 
317a0ac4411SKuninori Morimoto struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
318a0ac4411SKuninori Morimoto 						const char *driver_name)
319a0ac4411SKuninori Morimoto {
320a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
321a0ac4411SKuninori Morimoto 
322971da24cSKuninori Morimoto 	if (!driver_name)
323971da24cSKuninori Morimoto 		return NULL;
324971da24cSKuninori Morimoto 
325a0ac4411SKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
326971da24cSKuninori Morimoto 		const char *component_name = rtdcom->component->driver->name;
327971da24cSKuninori Morimoto 
328971da24cSKuninori Morimoto 		if (!component_name)
329971da24cSKuninori Morimoto 			continue;
330971da24cSKuninori Morimoto 
331971da24cSKuninori Morimoto 		if ((component_name == driver_name) ||
332971da24cSKuninori Morimoto 		    strcmp(component_name, driver_name) == 0)
333a0ac4411SKuninori Morimoto 			return rtdcom->component;
334a0ac4411SKuninori Morimoto 	}
335a0ac4411SKuninori Morimoto 
336a0ac4411SKuninori Morimoto 	return NULL;
337a0ac4411SKuninori Morimoto }
338031734b7SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
339a0ac4411SKuninori Morimoto 
34047c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
34147c88fffSLiam Girdwood 		const char *dai_link, int stream)
34247c88fffSLiam Girdwood {
3431a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
34447c88fffSLiam Girdwood 
345*bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
3461a497983SMengdong Lin 		if (rtd->dai_link->no_pcm &&
3471a497983SMengdong Lin 			!strcmp(rtd->dai_link->name, dai_link))
3481a497983SMengdong Lin 			return rtd->pcm->streams[stream].substream;
34947c88fffSLiam Girdwood 	}
350f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
35147c88fffSLiam Girdwood 	return NULL;
35247c88fffSLiam Girdwood }
35347c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
35447c88fffSLiam Girdwood 
35575ab9eb6SKuninori Morimoto static const struct snd_soc_ops null_snd_soc_ops;
35675ab9eb6SKuninori Morimoto 
3571a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
3581a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
3591a497983SMengdong Lin {
3601a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
3611a497983SMengdong Lin 
3621a497983SMengdong Lin 	rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
3631a497983SMengdong Lin 	if (!rtd)
3641a497983SMengdong Lin 		return NULL;
3651a497983SMengdong Lin 
366a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
3671a497983SMengdong Lin 	rtd->card = card;
3681a497983SMengdong Lin 	rtd->dai_link = dai_link;
36975ab9eb6SKuninori Morimoto 	if (!rtd->dai_link->ops)
37075ab9eb6SKuninori Morimoto 		rtd->dai_link->ops = &null_snd_soc_ops;
37175ab9eb6SKuninori Morimoto 
3726396bb22SKees Cook 	rtd->codec_dais = kcalloc(dai_link->num_codecs,
3736396bb22SKees Cook 					sizeof(struct snd_soc_dai *),
3741a497983SMengdong Lin 					GFP_KERNEL);
3751a497983SMengdong Lin 	if (!rtd->codec_dais) {
3761a497983SMengdong Lin 		kfree(rtd);
3771a497983SMengdong Lin 		return NULL;
3781a497983SMengdong Lin 	}
3791a497983SMengdong Lin 
3801a497983SMengdong Lin 	return rtd;
3811a497983SMengdong Lin }
3821a497983SMengdong Lin 
3831a497983SMengdong Lin static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
3841a497983SMengdong Lin {
3851a497983SMengdong Lin 	kfree(rtd->codec_dais);
386a0ac4411SKuninori Morimoto 	snd_soc_rtdcom_del_all(rtd);
3871a497983SMengdong Lin 	kfree(rtd);
3881a497983SMengdong Lin }
3891a497983SMengdong Lin 
3901a497983SMengdong Lin static void soc_add_pcm_runtime(struct snd_soc_card *card,
3911a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd)
3921a497983SMengdong Lin {
3931a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
3941a497983SMengdong Lin 	rtd->num = card->num_rtd;
3951a497983SMengdong Lin 	card->num_rtd++;
3961a497983SMengdong Lin }
3971a497983SMengdong Lin 
3981a497983SMengdong Lin static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
3991a497983SMengdong Lin {
4001a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, *_rtd;
4011a497983SMengdong Lin 
402*bcb1fd1fSKuninori Morimoto 	for_each_card_rtds_safe(card, rtd, _rtd) {
4031a497983SMengdong Lin 		list_del(&rtd->list);
4041a497983SMengdong Lin 		soc_free_pcm_runtime(rtd);
4051a497983SMengdong Lin 	}
4061a497983SMengdong Lin 
4071a497983SMengdong Lin 	card->num_rtd = 0;
4081a497983SMengdong Lin }
4091a497983SMengdong Lin 
41047c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
41147c88fffSLiam Girdwood 		const char *dai_link)
41247c88fffSLiam Girdwood {
4131a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
41447c88fffSLiam Girdwood 
415*bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4161a497983SMengdong Lin 		if (!strcmp(rtd->dai_link->name, dai_link))
4171a497983SMengdong Lin 			return rtd;
41847c88fffSLiam Girdwood 	}
419f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
42047c88fffSLiam Girdwood 	return NULL;
42147c88fffSLiam Girdwood }
42247c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
42347c88fffSLiam Girdwood 
4249d58a077SRichard Fitzgerald static void codec2codec_close_delayed_work(struct work_struct *work)
4259d58a077SRichard Fitzgerald {
4269d58a077SRichard Fitzgerald 	/* Currently nothing to do for c2c links
4279d58a077SRichard Fitzgerald 	 * Since c2c links are internal nodes in the DAPM graph and
4289d58a077SRichard Fitzgerald 	 * don't interface with the outside world or application layer
4299d58a077SRichard Fitzgerald 	 * we don't have to do any special handling on close.
4309d58a077SRichard Fitzgerald 	 */
4319d58a077SRichard Fitzgerald }
4329d58a077SRichard Fitzgerald 
4336f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
434db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
4356f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
436db2a4165SFrank Mandarino {
4376f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
438d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
4391a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
4401a497983SMengdong Lin 	int i;
441db2a4165SFrank Mandarino 
442c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
443c5599b87SLars-Peter Clausen 	if (!card->instantiated)
444e3509ff0SDaniel Mack 		return 0;
445e3509ff0SDaniel Mack 
4466ed25978SAndy Green 	/* Due to the resume being scheduled into a workqueue we could
4476ed25978SAndy Green 	* suspend before that's finished - wait for it to complete.
4486ed25978SAndy Green 	 */
449f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
4506ed25978SAndy Green 
4516ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
452f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
4536ed25978SAndy Green 
454a00f90f9SMark Brown 	/* mute any active DACs */
455*bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4560b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
4573efab7dcSMark Brown 
4581a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4593efab7dcSMark Brown 			continue;
4603efab7dcSMark Brown 
4610b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
46288bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
46388bd870fSBenoit Cousson 
464f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
465f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 1);
466db2a4165SFrank Mandarino 		}
46788bd870fSBenoit Cousson 	}
468db2a4165SFrank Mandarino 
4694ccab3e7SLiam Girdwood 	/* suspend all pcms */
470*bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4711a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4723efab7dcSMark Brown 			continue;
4733efab7dcSMark Brown 
4741a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
4753efab7dcSMark Brown 	}
4764ccab3e7SLiam Girdwood 
47787506549SMark Brown 	if (card->suspend_pre)
47870b2ac12SMark Brown 		card->suspend_pre(card);
479db2a4165SFrank Mandarino 
480*bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4811a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
4823efab7dcSMark Brown 
4831a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4843efab7dcSMark Brown 			continue;
4853efab7dcSMark Brown 
486bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
487f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
488db2a4165SFrank Mandarino 	}
489db2a4165SFrank Mandarino 
49037660b6dSLars-Peter Clausen 	/* close any waiting streams */
491*bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd)
4921a497983SMengdong Lin 		flush_delayed_work(&rtd->delayed_work);
493db2a4165SFrank Mandarino 
494*bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4953efab7dcSMark Brown 
4961a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4973efab7dcSMark Brown 			continue;
4983efab7dcSMark Brown 
4991a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
5007bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
501db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
502f0fba2adSLiam Girdwood 
5031a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
5047bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
505db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
506db2a4165SFrank Mandarino 	}
507db2a4165SFrank Mandarino 
5088be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
5098be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
510e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
511e2d32ff6SMark Brown 
5129178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
513d9fc4063SKuninori Morimoto 	list_for_each_entry(component, &card->component_dev_list, card_list) {
514d9fc4063SKuninori Morimoto 		struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
515d9fc4063SKuninori Morimoto 
5169178feb4SKuninori Morimoto 		/* If there are paths active then the COMPONENT will be held with
5171547aba9SMark Brown 		 * bias _ON and should not be suspended. */
5189178feb4SKuninori Morimoto 		if (!component->suspended) {
5194890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
5201547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
521125a25daSMark Brown 				/*
5229178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
523125a25daSMark Brown 				 * bias off then being in STANDBY
524125a25daSMark Brown 				 * means it's doing something,
525125a25daSMark Brown 				 * otherwise fall through.
526125a25daSMark Brown 				 */
5274890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
5289178feb4SKuninori Morimoto 					dev_dbg(component->dev,
52910e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
530125a25daSMark Brown 					break;
531125a25daSMark Brown 				}
5321a12d5dcSGustavo A. R. Silva 				/* fall through */
533a8093297SLars-Peter Clausen 
5341547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
535999f7f5aSKuninori Morimoto 				if (component->driver->suspend)
536999f7f5aSKuninori Morimoto 					component->driver->suspend(component);
5379178feb4SKuninori Morimoto 				component->suspended = 1;
5389178feb4SKuninori Morimoto 				if (component->regmap)
5399178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
540988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
5419178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
5421547aba9SMark Brown 				break;
5431547aba9SMark Brown 			default:
5449178feb4SKuninori Morimoto 				dev_dbg(component->dev,
5459178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
5461547aba9SMark Brown 				break;
5471547aba9SMark Brown 			}
5481547aba9SMark Brown 		}
549f0fba2adSLiam Girdwood 	}
550db2a4165SFrank Mandarino 
551*bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5521a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
5533efab7dcSMark Brown 
5541a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5553efab7dcSMark Brown 			continue;
5563efab7dcSMark Brown 
557bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
558f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
559988e8cc4SNicolin Chen 
560988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
561988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
562db2a4165SFrank Mandarino 	}
563db2a4165SFrank Mandarino 
56487506549SMark Brown 	if (card->suspend_post)
56570b2ac12SMark Brown 		card->suspend_post(card);
566db2a4165SFrank Mandarino 
567db2a4165SFrank Mandarino 	return 0;
568db2a4165SFrank Mandarino }
5696f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
570db2a4165SFrank Mandarino 
5716ed25978SAndy Green /* deferred resume work, so resume can complete before we finished
5726ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
5736ed25978SAndy Green  */
5746ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
575db2a4165SFrank Mandarino {
576f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
577f0fba2adSLiam Girdwood 			container_of(work, struct snd_soc_card, deferred_resume_work);
5781a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
579d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
5801a497983SMengdong Lin 	int i;
581db2a4165SFrank Mandarino 
5826ed25978SAndy Green 	/* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
5836ed25978SAndy Green 	 * so userspace apps are blocked from touching us
5846ed25978SAndy Green 	 */
5856ed25978SAndy Green 
586f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
5876ed25978SAndy Green 
5889949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
589f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
5909949788bSMark Brown 
59187506549SMark Brown 	if (card->resume_pre)
59270b2ac12SMark Brown 		card->resume_pre(card);
593db2a4165SFrank Mandarino 
594bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
595*bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5961a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
5973efab7dcSMark Brown 
5981a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5993efab7dcSMark Brown 			continue;
6003efab7dcSMark Brown 
601bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
602f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
603db2a4165SFrank Mandarino 	}
604db2a4165SFrank Mandarino 
605d9fc4063SKuninori Morimoto 	list_for_each_entry(component, &card->component_dev_list, card_list) {
6069178feb4SKuninori Morimoto 		if (component->suspended) {
607999f7f5aSKuninori Morimoto 			if (component->driver->resume)
608999f7f5aSKuninori Morimoto 				component->driver->resume(component);
6099178feb4SKuninori Morimoto 			component->suspended = 0;
6101547aba9SMark Brown 		}
611f0fba2adSLiam Girdwood 	}
612db2a4165SFrank Mandarino 
613*bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6143efab7dcSMark Brown 
6151a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6163efab7dcSMark Brown 			continue;
6173efab7dcSMark Brown 
6181a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
619d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
620db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
621f0fba2adSLiam Girdwood 
6221a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
623d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
624db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
625db2a4165SFrank Mandarino 	}
626db2a4165SFrank Mandarino 
6273ff3f64bSMark Brown 	/* unmute any active DACs */
628*bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6290b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
6303efab7dcSMark Brown 
6311a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6323efab7dcSMark Brown 			continue;
6333efab7dcSMark Brown 
6340b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
63588bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
63688bd870fSBenoit Cousson 
637f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
638f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 0);
639db2a4165SFrank Mandarino 		}
64088bd870fSBenoit Cousson 	}
641db2a4165SFrank Mandarino 
642*bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6431a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6443efab7dcSMark Brown 
6451a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6463efab7dcSMark Brown 			continue;
6473efab7dcSMark Brown 
648bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
649f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
650db2a4165SFrank Mandarino 	}
651db2a4165SFrank Mandarino 
65287506549SMark Brown 	if (card->resume_post)
65370b2ac12SMark Brown 		card->resume_post(card);
654db2a4165SFrank Mandarino 
655f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
6566ed25978SAndy Green 
6578be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
6588be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
659e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
6601a7aaa58SJeeja KP 
6611a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
6621a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
6636ed25978SAndy Green }
6646ed25978SAndy Green 
6656ed25978SAndy Green /* powers up audio subsystem after a suspend */
6666f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
6676ed25978SAndy Green {
6686f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
669bc263214SLars-Peter Clausen 	bool bus_control = false;
6701a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
671b9dd94a8SPeter Ujfalusi 
672c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
673c5599b87SLars-Peter Clausen 	if (!card->instantiated)
6745ff1ddf2SEric Miao 		return 0;
6755ff1ddf2SEric Miao 
676988e8cc4SNicolin Chen 	/* activate pins from sleep state */
677*bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6780b7990e3SKuninori Morimoto 		struct snd_soc_dai *codec_dai;
67988bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
68088bd870fSBenoit Cousson 		int j;
68188bd870fSBenoit Cousson 
682988e8cc4SNicolin Chen 		if (cpu_dai->active)
683988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
68488bd870fSBenoit Cousson 
6850b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, j, codec_dai) {
686988e8cc4SNicolin Chen 			if (codec_dai->active)
687988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
688988e8cc4SNicolin Chen 		}
68988bd870fSBenoit Cousson 	}
690988e8cc4SNicolin Chen 
691bc263214SLars-Peter Clausen 	/*
692bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
693bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
694bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
69564ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
69664ab9baaSMark Brown 	 */
697*bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6981a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
699bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
70082e14e8bSStephen Warren 	}
701bc263214SLars-Peter Clausen 	if (bus_control) {
702bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
70364ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
70464ab9baaSMark Brown 	} else {
705f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
7066308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
707f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
708f0fba2adSLiam Girdwood 	}
7096ed25978SAndy Green 
710db2a4165SFrank Mandarino 	return 0;
711db2a4165SFrank Mandarino }
7126f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
713db2a4165SFrank Mandarino #else
7146f8ab4acSMark Brown #define snd_soc_suspend NULL
7156f8ab4acSMark Brown #define snd_soc_resume NULL
716db2a4165SFrank Mandarino #endif
717db2a4165SFrank Mandarino 
71885e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
71902a06d30SBarry Song };
72002a06d30SBarry Song 
72165d9361fSLars-Peter Clausen static struct snd_soc_component *soc_find_component(
72265d9361fSLars-Peter Clausen 	const struct device_node *of_node, const char *name)
72312023a9aSMisael Lopez Cruz {
72465d9361fSLars-Peter Clausen 	struct snd_soc_component *component;
72512023a9aSMisael Lopez Cruz 
72634e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
72734e81ab4SLars-Peter Clausen 
72865d9361fSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
72965d9361fSLars-Peter Clausen 		if (of_node) {
73065d9361fSLars-Peter Clausen 			if (component->dev->of_node == of_node)
73165d9361fSLars-Peter Clausen 				return component;
73265d9361fSLars-Peter Clausen 		} else if (strcmp(component->name, name) == 0) {
73365d9361fSLars-Peter Clausen 			return component;
73412023a9aSMisael Lopez Cruz 		}
73512023a9aSMisael Lopez Cruz 	}
73612023a9aSMisael Lopez Cruz 
73712023a9aSMisael Lopez Cruz 	return NULL;
73812023a9aSMisael Lopez Cruz }
73912023a9aSMisael Lopez Cruz 
740be6ac0a9SKuninori Morimoto static int snd_soc_is_matching_component(
741be6ac0a9SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc,
742be6ac0a9SKuninori Morimoto 	struct snd_soc_component *component)
743be6ac0a9SKuninori Morimoto {
744be6ac0a9SKuninori Morimoto 	struct device_node *component_of_node;
745be6ac0a9SKuninori Morimoto 
746be6ac0a9SKuninori Morimoto 	component_of_node = component->dev->of_node;
747be6ac0a9SKuninori Morimoto 	if (!component_of_node && component->dev->parent)
748be6ac0a9SKuninori Morimoto 		component_of_node = component->dev->parent->of_node;
749be6ac0a9SKuninori Morimoto 
750be6ac0a9SKuninori Morimoto 	if (dlc->of_node && component_of_node != dlc->of_node)
751be6ac0a9SKuninori Morimoto 		return 0;
752be6ac0a9SKuninori Morimoto 	if (dlc->name && strcmp(component->name, dlc->name))
753be6ac0a9SKuninori Morimoto 		return 0;
754be6ac0a9SKuninori Morimoto 
755be6ac0a9SKuninori Morimoto 	return 1;
756be6ac0a9SKuninori Morimoto }
757be6ac0a9SKuninori Morimoto 
758fbb88b5cSMengdong Lin /**
759fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
760fbb88b5cSMengdong Lin  *
7614958471bSJeffy Chen  * @dlc: name of the DAI or the DAI driver and optional component info to match
762fbb88b5cSMengdong Lin  *
763ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
764fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
765fbb88b5cSMengdong Lin  * should also match if being specified.
766fbb88b5cSMengdong Lin  *
767fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
768fbb88b5cSMengdong Lin  */
769305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
77014621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
77112023a9aSMisael Lopez Cruz {
77214621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
77314621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
77412023a9aSMisael Lopez Cruz 
77534e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
77634e81ab4SLars-Peter Clausen 
77714621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs*/
77814621c7eSLars-Peter Clausen 	list_for_each_entry(component, &component_list, list) {
779be6ac0a9SKuninori Morimoto 		if (!snd_soc_is_matching_component(dlc, component))
78012023a9aSMisael Lopez Cruz 			continue;
78114621c7eSLars-Peter Clausen 		list_for_each_entry(dai, &component->dai_list, list) {
7824958471bSJeffy Chen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
7836a6dafdaSJeffy Chen 			    && (!dai->driver->name
7846a6dafdaSJeffy Chen 				|| strcmp(dai->driver->name, dlc->dai_name)))
78514621c7eSLars-Peter Clausen 				continue;
78612023a9aSMisael Lopez Cruz 
78714621c7eSLars-Peter Clausen 			return dai;
78812023a9aSMisael Lopez Cruz 		}
78912023a9aSMisael Lopez Cruz 	}
79012023a9aSMisael Lopez Cruz 
79112023a9aSMisael Lopez Cruz 	return NULL;
79212023a9aSMisael Lopez Cruz }
793305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
79412023a9aSMisael Lopez Cruz 
79517fb1755SMengdong Lin 
79617fb1755SMengdong Lin /**
79717fb1755SMengdong Lin  * snd_soc_find_dai_link - Find a DAI link
79817fb1755SMengdong Lin  *
79917fb1755SMengdong Lin  * @card: soc card
80017fb1755SMengdong Lin  * @id: DAI link ID to match
80117fb1755SMengdong Lin  * @name: DAI link name to match, optional
8028abab35fSCharles Keepax  * @stream_name: DAI link stream name to match, optional
80317fb1755SMengdong Lin  *
80417fb1755SMengdong Lin  * This function will search all existing DAI links of the soc card to
80517fb1755SMengdong Lin  * find the link of the same ID. Since DAI links may not have their
80617fb1755SMengdong Lin  * unique ID, so name and stream name should also match if being
80717fb1755SMengdong Lin  * specified.
80817fb1755SMengdong Lin  *
80917fb1755SMengdong Lin  * Return: pointer of DAI link, or NULL if not found.
81017fb1755SMengdong Lin  */
81117fb1755SMengdong Lin struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
81217fb1755SMengdong Lin 					       int id, const char *name,
81317fb1755SMengdong Lin 					       const char *stream_name)
81417fb1755SMengdong Lin {
81517fb1755SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
81617fb1755SMengdong Lin 
81717fb1755SMengdong Lin 	lockdep_assert_held(&client_mutex);
81817fb1755SMengdong Lin 
81998061fdbSKuninori Morimoto 	for_each_card_links_safe(card, link, _link) {
82017fb1755SMengdong Lin 		if (link->id != id)
82117fb1755SMengdong Lin 			continue;
82217fb1755SMengdong Lin 
82317fb1755SMengdong Lin 		if (name && (!link->name || strcmp(name, link->name)))
82417fb1755SMengdong Lin 			continue;
82517fb1755SMengdong Lin 
82617fb1755SMengdong Lin 		if (stream_name && (!link->stream_name
82717fb1755SMengdong Lin 			|| strcmp(stream_name, link->stream_name)))
82817fb1755SMengdong Lin 			continue;
82917fb1755SMengdong Lin 
83017fb1755SMengdong Lin 		return link;
83117fb1755SMengdong Lin 	}
83217fb1755SMengdong Lin 
83317fb1755SMengdong Lin 	return NULL;
83417fb1755SMengdong Lin }
83517fb1755SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
83617fb1755SMengdong Lin 
83749a5ba1cSMengdong Lin static bool soc_is_dai_link_bound(struct snd_soc_card *card,
83849a5ba1cSMengdong Lin 		struct snd_soc_dai_link *dai_link)
839db2a4165SFrank Mandarino {
84049a5ba1cSMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
84149a5ba1cSMengdong Lin 
842*bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
84349a5ba1cSMengdong Lin 		if (rtd->dai_link == dai_link)
84449a5ba1cSMengdong Lin 			return true;
84549a5ba1cSMengdong Lin 	}
84649a5ba1cSMengdong Lin 
84749a5ba1cSMengdong Lin 	return false;
84849a5ba1cSMengdong Lin }
84949a5ba1cSMengdong Lin 
8506f2f1ff0SMengdong Lin static int soc_bind_dai_link(struct snd_soc_card *card,
8516f2f1ff0SMengdong Lin 	struct snd_soc_dai_link *dai_link)
852db2a4165SFrank Mandarino {
8531a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
85488bd870fSBenoit Cousson 	struct snd_soc_dai_link_component *codecs = dai_link->codecs;
85514621c7eSLars-Peter Clausen 	struct snd_soc_dai_link_component cpu_dai_component;
85690be711eSKuninori Morimoto 	struct snd_soc_component *component;
8571a497983SMengdong Lin 	struct snd_soc_dai **codec_dais;
85888bd870fSBenoit Cousson 	int i;
859db2a4165SFrank Mandarino 
860a655de80SLiam Girdwood 	if (dai_link->ignore)
861a655de80SLiam Girdwood 		return 0;
862a655de80SLiam Girdwood 
8636f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
8646308419aSMark Brown 
86549a5ba1cSMengdong Lin 	if (soc_is_dai_link_bound(card, dai_link)) {
86649a5ba1cSMengdong Lin 		dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
86749a5ba1cSMengdong Lin 			dai_link->name);
86849a5ba1cSMengdong Lin 		return 0;
86949a5ba1cSMengdong Lin 	}
870db2a4165SFrank Mandarino 
871513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
872513cb311SSudip Mukherjee 	if (!rtd)
873513cb311SSudip Mukherjee 		return -ENOMEM;
874513cb311SSudip Mukherjee 
87514621c7eSLars-Peter Clausen 	cpu_dai_component.name = dai_link->cpu_name;
87614621c7eSLars-Peter Clausen 	cpu_dai_component.of_node = dai_link->cpu_of_node;
87714621c7eSLars-Peter Clausen 	cpu_dai_component.dai_name = dai_link->cpu_dai_name;
87814621c7eSLars-Peter Clausen 	rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
879b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
8806b490879SMartin Hundebøll 		dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
881f0fba2adSLiam Girdwood 			 dai_link->cpu_dai_name);
8821a497983SMengdong Lin 		goto _err_defer;
883c5af3a2eSMark Brown 	}
88490be711eSKuninori Morimoto 	snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
885c5af3a2eSMark Brown 
88688bd870fSBenoit Cousson 	rtd->num_codecs = dai_link->num_codecs;
88788bd870fSBenoit Cousson 
88888bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
8890b7990e3SKuninori Morimoto 	/* we can use for_each_rtd_codec_dai() after this */
8901a497983SMengdong Lin 	codec_dais = rtd->codec_dais;
89188bd870fSBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
89214621c7eSLars-Peter Clausen 		codec_dais[i] = snd_soc_find_dai(&codecs[i]);
89388bd870fSBenoit Cousson 		if (!codec_dais[i]) {
89412023a9aSMisael Lopez Cruz 			dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
89588bd870fSBenoit Cousson 				codecs[i].dai_name);
8961a497983SMengdong Lin 			goto _err_defer;
89712023a9aSMisael Lopez Cruz 		}
89890be711eSKuninori Morimoto 		snd_soc_rtdcom_add(rtd, codec_dais[i]->component);
89988bd870fSBenoit Cousson 	}
90088bd870fSBenoit Cousson 
90188bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
90288bd870fSBenoit Cousson 	rtd->codec_dai = codec_dais[0];
90312023a9aSMisael Lopez Cruz 
904b19e6e7bSMark Brown 	/* find one from the set of registered platforms */
90590be711eSKuninori Morimoto 	list_for_each_entry(component, &component_list, list) {
906be6ac0a9SKuninori Morimoto 		if (!snd_soc_is_matching_component(dai_link->platform,
907be6ac0a9SKuninori Morimoto 						   component))
90890be711eSKuninori Morimoto 			continue;
90990be711eSKuninori Morimoto 
91090be711eSKuninori Morimoto 		snd_soc_rtdcom_add(rtd, component);
91190be711eSKuninori Morimoto 	}
91290be711eSKuninori Morimoto 
9131a497983SMengdong Lin 	soc_add_pcm_runtime(card, rtd);
914b19e6e7bSMark Brown 	return 0;
9151a497983SMengdong Lin 
9161a497983SMengdong Lin _err_defer:
9171a497983SMengdong Lin 	soc_free_pcm_runtime(rtd);
9181a497983SMengdong Lin 	return  -EPROBE_DEFER;
9196b05eda6SMark Brown }
9206b05eda6SMark Brown 
921f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
922d12cd198SStephen Warren {
923abd31b32SLars-Peter Clausen 	if (!component->card)
92470090bbbSLars-Peter Clausen 		return;
925d12cd198SStephen Warren 
926d9fc4063SKuninori Morimoto 	list_del(&component->card_list);
927d12cd198SStephen Warren 
928999f7f5aSKuninori Morimoto 	if (component->driver->remove)
929999f7f5aSKuninori Morimoto 		component->driver->remove(component);
930d12cd198SStephen Warren 
931f1d45cc3SLars-Peter Clausen 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
932d12cd198SStephen Warren 
933f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
934abd31b32SLars-Peter Clausen 	component->card = NULL;
935f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
936d12cd198SStephen Warren }
937d12cd198SStephen Warren 
938e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
939589c3563SJarkko Nikula {
940589c3563SJarkko Nikula 	int err;
941589c3563SJarkko Nikula 
9422eda3cb1SKuninori Morimoto 	if (!dai || !dai->probed ||
9432eda3cb1SKuninori Morimoto 	    dai->driver->remove_order != order)
9442eda3cb1SKuninori Morimoto 		return;
9452eda3cb1SKuninori Morimoto 
946e60cd14fSLars-Peter Clausen 	if (dai->driver->remove) {
947e60cd14fSLars-Peter Clausen 		err = dai->driver->remove(dai);
948589c3563SJarkko Nikula 		if (err < 0)
949e60cd14fSLars-Peter Clausen 			dev_err(dai->dev,
950b0aa88afSMisael Lopez Cruz 				"ASoC: failed to remove %s: %d\n",
951e60cd14fSLars-Peter Clausen 				dai->name, err);
952b0aa88afSMisael Lopez Cruz 	}
953e60cd14fSLars-Peter Clausen 	dai->probed = 0;
954b0aa88afSMisael Lopez Cruz }
955b0aa88afSMisael Lopez Cruz 
9561a497983SMengdong Lin static void soc_remove_link_dais(struct snd_soc_card *card,
9571a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
958f0fba2adSLiam Girdwood {
959e60cd14fSLars-Peter Clausen 	int i;
9600b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
961f0fba2adSLiam Girdwood 
962f0fba2adSLiam Girdwood 	/* unregister the rtd device */
963f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
96436ae1a96SMark Brown 		device_unregister(rtd->dev);
965f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
96602a06d30SBarry Song 	}
96702a06d30SBarry Song 
968f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
9690b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
9700b7990e3SKuninori Morimoto 		soc_remove_dai(codec_dai, order);
971f0fba2adSLiam Girdwood 
972e60cd14fSLars-Peter Clausen 	soc_remove_dai(rtd->cpu_dai, order);
973f0fba2adSLiam Girdwood }
974f0fba2adSLiam Girdwood 
9751a497983SMengdong Lin static void soc_remove_link_components(struct snd_soc_card *card,
9761a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, int order)
97762ae68faSStephen Warren {
97861aca564SLars-Peter Clausen 	struct snd_soc_component *component;
97990be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
98062ae68faSStephen Warren 
98190be711eSKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
98290be711eSKuninori Morimoto 		component = rtdcom->component;
98362ae68faSStephen Warren 
98470090bbbSLars-Peter Clausen 		if (component->driver->remove_order == order)
98561aca564SLars-Peter Clausen 			soc_remove_component(component);
98662ae68faSStephen Warren 	}
98762ae68faSStephen Warren }
98862ae68faSStephen Warren 
9890671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
9900671fd8eSKuninori Morimoto {
9911a497983SMengdong Lin 	int order;
9921a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
993f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
9940671fd8eSKuninori Morimoto 
9950168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
9960168bf0dSLiam Girdwood 			order++) {
997*bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd)
9981a497983SMengdong Lin 			soc_remove_link_dais(card, rtd, order);
9990168bf0dSLiam Girdwood 	}
100062ae68faSStephen Warren 
100162ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
100262ae68faSStephen Warren 			order++) {
1003*bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd)
10041a497983SMengdong Lin 			soc_remove_link_components(card, rtd, order);
100562ae68faSStephen Warren 	}
100662ae68faSStephen Warren 
100798061fdbSKuninori Morimoto 	for_each_card_links_safe(card, link, _link) {
1008f8f80361SMengdong Lin 		if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1009f8f80361SMengdong Lin 			dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1010f8f80361SMengdong Lin 				link->name);
1011f8f80361SMengdong Lin 
1012f8f80361SMengdong Lin 		list_del(&link->list);
10130671fd8eSKuninori Morimoto 	}
10140671fd8eSKuninori Morimoto }
10150671fd8eSKuninori Morimoto 
1016daecf46eSKuninori Morimoto static int snd_soc_init_platform(struct snd_soc_card *card,
1017daecf46eSKuninori Morimoto 				 struct snd_soc_dai_link *dai_link)
1018daecf46eSKuninori Morimoto {
10194a9ed394SKuninori Morimoto 	struct snd_soc_dai_link_component *platform = dai_link->platform;
10204a9ed394SKuninori Morimoto 
1021daecf46eSKuninori Morimoto 	/*
1022daecf46eSKuninori Morimoto 	 * FIXME
1023daecf46eSKuninori Morimoto 	 *
1024daecf46eSKuninori Morimoto 	 * this function should be removed in the future
1025daecf46eSKuninori Morimoto 	 */
1026daecf46eSKuninori Morimoto 	/* convert Legacy platform link */
10274a9ed394SKuninori Morimoto 	if (!platform) {
10284a9ed394SKuninori Morimoto 		platform = devm_kzalloc(card->dev,
1029daecf46eSKuninori Morimoto 				sizeof(struct snd_soc_dai_link_component),
1030daecf46eSKuninori Morimoto 				GFP_KERNEL);
10314a9ed394SKuninori Morimoto 		if (!platform)
1032daecf46eSKuninori Morimoto 			return -ENOMEM;
1033daecf46eSKuninori Morimoto 
10344a9ed394SKuninori Morimoto 		dai_link->platform	= platform;
10354a9ed394SKuninori Morimoto 		platform->name		= dai_link->platform_name;
10364a9ed394SKuninori Morimoto 		platform->of_node	= dai_link->platform_of_node;
10374a9ed394SKuninori Morimoto 		platform->dai_name	= NULL;
10384a9ed394SKuninori Morimoto 	}
10394a9ed394SKuninori Morimoto 
10404a9ed394SKuninori Morimoto 	/* if there's no platform we match on the empty platform */
10414a9ed394SKuninori Morimoto 	if (!platform->name &&
10424a9ed394SKuninori Morimoto 	    !platform->of_node)
10434a9ed394SKuninori Morimoto 		platform->name = "snd-soc-dummy";
1044daecf46eSKuninori Morimoto 
1045daecf46eSKuninori Morimoto 	return 0;
1046daecf46eSKuninori Morimoto }
1047daecf46eSKuninori Morimoto 
1048923c5e61SMengdong Lin static int snd_soc_init_multicodec(struct snd_soc_card *card,
1049923c5e61SMengdong Lin 				   struct snd_soc_dai_link *dai_link)
1050923c5e61SMengdong Lin {
1051923c5e61SMengdong Lin 	/* Legacy codec/codec_dai link is a single entry in multicodec */
1052923c5e61SMengdong Lin 	if (dai_link->codec_name || dai_link->codec_of_node ||
1053923c5e61SMengdong Lin 	    dai_link->codec_dai_name) {
1054923c5e61SMengdong Lin 		dai_link->num_codecs = 1;
1055923c5e61SMengdong Lin 
1056923c5e61SMengdong Lin 		dai_link->codecs = devm_kzalloc(card->dev,
1057923c5e61SMengdong Lin 				sizeof(struct snd_soc_dai_link_component),
1058923c5e61SMengdong Lin 				GFP_KERNEL);
1059923c5e61SMengdong Lin 		if (!dai_link->codecs)
1060923c5e61SMengdong Lin 			return -ENOMEM;
1061923c5e61SMengdong Lin 
1062923c5e61SMengdong Lin 		dai_link->codecs[0].name = dai_link->codec_name;
1063923c5e61SMengdong Lin 		dai_link->codecs[0].of_node = dai_link->codec_of_node;
1064923c5e61SMengdong Lin 		dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1065923c5e61SMengdong Lin 	}
1066923c5e61SMengdong Lin 
1067923c5e61SMengdong Lin 	if (!dai_link->codecs) {
1068923c5e61SMengdong Lin 		dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1069923c5e61SMengdong Lin 		return -EINVAL;
1070923c5e61SMengdong Lin 	}
1071923c5e61SMengdong Lin 
1072923c5e61SMengdong Lin 	return 0;
1073923c5e61SMengdong Lin }
1074923c5e61SMengdong Lin 
1075923c5e61SMengdong Lin static int soc_init_dai_link(struct snd_soc_card *card,
1076923c5e61SMengdong Lin 				   struct snd_soc_dai_link *link)
1077923c5e61SMengdong Lin {
1078923c5e61SMengdong Lin 	int i, ret;
10793db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *codec;
1080923c5e61SMengdong Lin 
1081daecf46eSKuninori Morimoto 	ret = snd_soc_init_platform(card, link);
1082daecf46eSKuninori Morimoto 	if (ret) {
1083daecf46eSKuninori Morimoto 		dev_err(card->dev, "ASoC: failed to init multiplatform\n");
1084daecf46eSKuninori Morimoto 		return ret;
1085daecf46eSKuninori Morimoto 	}
1086daecf46eSKuninori Morimoto 
1087923c5e61SMengdong Lin 	ret = snd_soc_init_multicodec(card, link);
1088923c5e61SMengdong Lin 	if (ret) {
1089923c5e61SMengdong Lin 		dev_err(card->dev, "ASoC: failed to init multicodec\n");
1090923c5e61SMengdong Lin 		return ret;
1091923c5e61SMengdong Lin 	}
1092923c5e61SMengdong Lin 
10933db769f1SKuninori Morimoto 	for_each_link_codecs(link, i, codec) {
1094923c5e61SMengdong Lin 		/*
1095923c5e61SMengdong Lin 		 * Codec must be specified by 1 of name or OF node,
1096923c5e61SMengdong Lin 		 * not both or neither.
1097923c5e61SMengdong Lin 		 */
10983db769f1SKuninori Morimoto 		if (!!codec->name ==
10993db769f1SKuninori Morimoto 		    !!codec->of_node) {
1100923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1101923c5e61SMengdong Lin 				link->name);
1102923c5e61SMengdong Lin 			return -EINVAL;
1103923c5e61SMengdong Lin 		}
1104923c5e61SMengdong Lin 		/* Codec DAI name must be specified */
11053db769f1SKuninori Morimoto 		if (!codec->dai_name) {
1106923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1107923c5e61SMengdong Lin 				link->name);
1108923c5e61SMengdong Lin 			return -EINVAL;
1109923c5e61SMengdong Lin 		}
1110923c5e61SMengdong Lin 	}
1111923c5e61SMengdong Lin 
1112923c5e61SMengdong Lin 	/*
1113923c5e61SMengdong Lin 	 * Platform may be specified by either name or OF node, but
1114923c5e61SMengdong Lin 	 * can be left unspecified, and a dummy platform will be used.
1115923c5e61SMengdong Lin 	 */
1116daecf46eSKuninori Morimoto 	if (link->platform->name && link->platform->of_node) {
1117923c5e61SMengdong Lin 		dev_err(card->dev,
1118923c5e61SMengdong Lin 			"ASoC: Both platform name/of_node are set for %s\n",
1119923c5e61SMengdong Lin 			link->name);
1120923c5e61SMengdong Lin 		return -EINVAL;
1121923c5e61SMengdong Lin 	}
1122923c5e61SMengdong Lin 	/*
1123923c5e61SMengdong Lin 	 * CPU device may be specified by either name or OF node, but
1124923c5e61SMengdong Lin 	 * can be left unspecified, and will be matched based on DAI
1125923c5e61SMengdong Lin 	 * name alone..
1126923c5e61SMengdong Lin 	 */
1127923c5e61SMengdong Lin 	if (link->cpu_name && link->cpu_of_node) {
1128923c5e61SMengdong Lin 		dev_err(card->dev,
1129923c5e61SMengdong Lin 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
1130923c5e61SMengdong Lin 			link->name);
1131923c5e61SMengdong Lin 		return -EINVAL;
1132923c5e61SMengdong Lin 	}
1133923c5e61SMengdong Lin 	/*
1134923c5e61SMengdong Lin 	 * At least one of CPU DAI name or CPU device name/node must be
1135923c5e61SMengdong Lin 	 * specified
1136923c5e61SMengdong Lin 	 */
1137923c5e61SMengdong Lin 	if (!link->cpu_dai_name &&
1138923c5e61SMengdong Lin 	    !(link->cpu_name || link->cpu_of_node)) {
1139923c5e61SMengdong Lin 		dev_err(card->dev,
1140923c5e61SMengdong Lin 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1141923c5e61SMengdong Lin 			link->name);
1142923c5e61SMengdong Lin 		return -EINVAL;
1143923c5e61SMengdong Lin 	}
1144923c5e61SMengdong Lin 
1145923c5e61SMengdong Lin 	return 0;
1146923c5e61SMengdong Lin }
1147923c5e61SMengdong Lin 
1148ef2e8175SKuninori Morimoto void snd_soc_disconnect_sync(struct device *dev)
1149ef2e8175SKuninori Morimoto {
1150ef2e8175SKuninori Morimoto 	struct snd_soc_component *component = snd_soc_lookup_component(dev, NULL);
1151ef2e8175SKuninori Morimoto 
1152ef2e8175SKuninori Morimoto 	if (!component || !component->card)
1153ef2e8175SKuninori Morimoto 		return;
1154ef2e8175SKuninori Morimoto 
1155ef2e8175SKuninori Morimoto 	snd_card_disconnect_sync(component->card->snd_card);
1156ef2e8175SKuninori Morimoto }
1157df532185SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
1158ef2e8175SKuninori Morimoto 
1159f8f80361SMengdong Lin /**
1160f8f80361SMengdong Lin  * snd_soc_add_dai_link - Add a DAI link dynamically
1161f8f80361SMengdong Lin  * @card: The ASoC card to which the DAI link is added
1162f8f80361SMengdong Lin  * @dai_link: The new DAI link to add
1163f8f80361SMengdong Lin  *
1164f8f80361SMengdong Lin  * This function adds a DAI link to the ASoC card's link list.
1165f8f80361SMengdong Lin  *
1166f8f80361SMengdong Lin  * Note: Topology can use this API to add DAI links when probing the
1167f8f80361SMengdong Lin  * topology component. And machine drivers can still define static
1168f8f80361SMengdong Lin  * DAI links in dai_link array.
1169f8f80361SMengdong Lin  */
1170f8f80361SMengdong Lin int snd_soc_add_dai_link(struct snd_soc_card *card,
1171f8f80361SMengdong Lin 		struct snd_soc_dai_link *dai_link)
1172f8f80361SMengdong Lin {
1173f8f80361SMengdong Lin 	if (dai_link->dobj.type
1174f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1175f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1176f8f80361SMengdong Lin 			dai_link->dobj.type);
1177f8f80361SMengdong Lin 		return -EINVAL;
1178f8f80361SMengdong Lin 	}
1179f8f80361SMengdong Lin 
1180f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
1181d6f220eaSMengdong Lin 	/* Notify the machine driver for extra initialization
1182d6f220eaSMengdong Lin 	 * on the link created by topology.
1183d6f220eaSMengdong Lin 	 */
1184d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->add_dai_link)
1185d6f220eaSMengdong Lin 		card->add_dai_link(card, dai_link);
1186d6f220eaSMengdong Lin 
1187f8f80361SMengdong Lin 	list_add_tail(&dai_link->list, &card->dai_link_list);
1188f8f80361SMengdong Lin 
1189f8f80361SMengdong Lin 	return 0;
1190f8f80361SMengdong Lin }
1191f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1192f8f80361SMengdong Lin 
1193f8f80361SMengdong Lin /**
1194f8f80361SMengdong Lin  * snd_soc_remove_dai_link - Remove a DAI link from the list
1195f8f80361SMengdong Lin  * @card: The ASoC card that owns the link
1196f8f80361SMengdong Lin  * @dai_link: The DAI link to remove
1197f8f80361SMengdong Lin  *
1198f8f80361SMengdong Lin  * This function removes a DAI link from the ASoC card's link list.
1199f8f80361SMengdong Lin  *
1200f8f80361SMengdong Lin  * For DAI links previously added by topology, topology should
1201f8f80361SMengdong Lin  * remove them by using the dobj embedded in the link.
1202f8f80361SMengdong Lin  */
1203f8f80361SMengdong Lin void snd_soc_remove_dai_link(struct snd_soc_card *card,
1204f8f80361SMengdong Lin 			     struct snd_soc_dai_link *dai_link)
1205f8f80361SMengdong Lin {
1206f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
1207f8f80361SMengdong Lin 
1208f8f80361SMengdong Lin 	if (dai_link->dobj.type
1209f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1210f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1211f8f80361SMengdong Lin 			dai_link->dobj.type);
1212f8f80361SMengdong Lin 		return;
1213f8f80361SMengdong Lin 	}
1214f8f80361SMengdong Lin 
1215f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
1216d6f220eaSMengdong Lin 	/* Notify the machine driver for extra destruction
1217d6f220eaSMengdong Lin 	 * on the link created by topology.
1218d6f220eaSMengdong Lin 	 */
1219d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->remove_dai_link)
1220d6f220eaSMengdong Lin 		card->remove_dai_link(card, dai_link);
1221d6f220eaSMengdong Lin 
122298061fdbSKuninori Morimoto 	for_each_card_links_safe(card, link, _link) {
1223f8f80361SMengdong Lin 		if (link == dai_link) {
1224f8f80361SMengdong Lin 			list_del(&link->list);
1225f8f80361SMengdong Lin 			return;
1226f8f80361SMengdong Lin 		}
1227f8f80361SMengdong Lin 	}
1228f8f80361SMengdong Lin }
1229f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1230f0fba2adSLiam Girdwood 
1231aefba455SJerome Brunet static void soc_set_of_name_prefix(struct snd_soc_component *component)
1232aefba455SJerome Brunet {
1233aefba455SJerome Brunet 	struct device_node *component_of_node = component->dev->of_node;
1234aefba455SJerome Brunet 	const char *str;
1235aefba455SJerome Brunet 	int ret;
1236aefba455SJerome Brunet 
1237aefba455SJerome Brunet 	if (!component_of_node && component->dev->parent)
1238aefba455SJerome Brunet 		component_of_node = component->dev->parent->of_node;
1239aefba455SJerome Brunet 
1240aefba455SJerome Brunet 	ret = of_property_read_string(component_of_node, "sound-name-prefix",
1241aefba455SJerome Brunet 				      &str);
1242aefba455SJerome Brunet 	if (!ret)
1243aefba455SJerome Brunet 		component->name_prefix = str;
1244aefba455SJerome Brunet }
1245aefba455SJerome Brunet 
1246ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
124794f99c87SLars-Peter Clausen 				struct snd_soc_component *component)
1248ead9b919SJarkko Nikula {
1249ead9b919SJarkko Nikula 	int i;
1250ead9b919SJarkko Nikula 
1251aefba455SJerome Brunet 	for (i = 0; i < card->num_configs && card->codec_conf; i++) {
1252ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1253b24c539bSCharles Keepax 		struct device_node *component_of_node = component->dev->of_node;
1254b24c539bSCharles Keepax 
1255b24c539bSCharles Keepax 		if (!component_of_node && component->dev->parent)
1256b24c539bSCharles Keepax 			component_of_node = component->dev->parent->of_node;
1257b24c539bSCharles Keepax 
1258b24c539bSCharles Keepax 		if (map->of_node && component_of_node != map->of_node)
12593ca041edSSebastian Reichel 			continue;
126094f99c87SLars-Peter Clausen 		if (map->dev_name && strcmp(component->name, map->dev_name))
12613ca041edSSebastian Reichel 			continue;
126294f99c87SLars-Peter Clausen 		component->name_prefix = map->name_prefix;
1263aefba455SJerome Brunet 		return;
1264ead9b919SJarkko Nikula 	}
1265aefba455SJerome Brunet 
1266aefba455SJerome Brunet 	/*
1267aefba455SJerome Brunet 	 * If there is no configuration table or no match in the table,
1268aefba455SJerome Brunet 	 * check if a prefix is provided in the node
1269aefba455SJerome Brunet 	 */
1270aefba455SJerome Brunet 	soc_set_of_name_prefix(component);
1271ead9b919SJarkko Nikula }
1272ead9b919SJarkko Nikula 
1273f1d45cc3SLars-Peter Clausen static int soc_probe_component(struct snd_soc_card *card,
1274f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component)
1275589c3563SJarkko Nikula {
1276f1d45cc3SLars-Peter Clausen 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1277888df395SMark Brown 	struct snd_soc_dai *dai;
1278f1d45cc3SLars-Peter Clausen 	int ret;
1279589c3563SJarkko Nikula 
12801b7c1231SLars-Peter Clausen 	if (!strcmp(component->name, "snd-soc-dummy"))
128170090bbbSLars-Peter Clausen 		return 0;
1282589c3563SJarkko Nikula 
1283abd31b32SLars-Peter Clausen 	if (component->card) {
12841b7c1231SLars-Peter Clausen 		if (component->card != card) {
12851b7c1231SLars-Peter Clausen 			dev_err(component->dev,
12861b7c1231SLars-Peter Clausen 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
12871b7c1231SLars-Peter Clausen 				card->name, component->card->name);
12881b7c1231SLars-Peter Clausen 			return -ENODEV;
12891b7c1231SLars-Peter Clausen 		}
12901b7c1231SLars-Peter Clausen 		return 0;
12911b7c1231SLars-Peter Clausen 	}
12921b7c1231SLars-Peter Clausen 
1293abd31b32SLars-Peter Clausen 	if (!try_module_get(component->dev->driver->owner))
1294abd31b32SLars-Peter Clausen 		return -ENODEV;
1295abd31b32SLars-Peter Clausen 
1296f1d45cc3SLars-Peter Clausen 	component->card = card;
1297f1d45cc3SLars-Peter Clausen 	dapm->card = card;
1298f1d45cc3SLars-Peter Clausen 	soc_set_name_prefix(card, component);
1299589c3563SJarkko Nikula 
1300f1d45cc3SLars-Peter Clausen 	soc_init_component_debugfs(component);
1301d5d1e0beSLars-Peter Clausen 
1302688d0ebfSKuninori Morimoto 	if (component->driver->dapm_widgets) {
1303688d0ebfSKuninori Morimoto 		ret = snd_soc_dapm_new_controls(dapm,
1304688d0ebfSKuninori Morimoto 					component->driver->dapm_widgets,
1305688d0ebfSKuninori Morimoto 					component->driver->num_dapm_widgets);
130677530150SLars-Peter Clausen 
1307b318ad50SNariman Poushin 		if (ret != 0) {
1308f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1309b318ad50SNariman Poushin 				"Failed to create new controls %d\n", ret);
1310b318ad50SNariman Poushin 			goto err_probe;
1311b318ad50SNariman Poushin 		}
1312b318ad50SNariman Poushin 	}
1313b318ad50SNariman Poushin 
13140634814fSLars-Peter Clausen 	list_for_each_entry(dai, &component->dai_list, list) {
13150634814fSLars-Peter Clausen 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1316261edc70SNariman Poushin 		if (ret != 0) {
1317f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1318261edc70SNariman Poushin 				"Failed to create DAI widgets %d\n", ret);
1319261edc70SNariman Poushin 			goto err_probe;
1320261edc70SNariman Poushin 		}
1321261edc70SNariman Poushin 	}
1322888df395SMark Brown 
1323999f7f5aSKuninori Morimoto 	if (component->driver->probe) {
1324999f7f5aSKuninori Morimoto 		ret = component->driver->probe(component);
1325589c3563SJarkko Nikula 		if (ret < 0) {
1326f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1327f1d45cc3SLars-Peter Clausen 				"ASoC: failed to probe component %d\n", ret);
132870d29331SJarkko Nikula 			goto err_probe;
1329589c3563SJarkko Nikula 		}
1330cb2cf612SLiam Girdwood 
1331f1d45cc3SLars-Peter Clausen 		WARN(dapm->idle_bias_off &&
1332f1d45cc3SLars-Peter Clausen 			dapm->bias_level != SND_SOC_BIAS_OFF,
1333956245e9SLiam Girdwood 			"codec %s can not start from non-off bias with idle_bias_off==1\n",
1334f1d45cc3SLars-Peter Clausen 			component->name);
1335956245e9SLiam Girdwood 	}
1336956245e9SLiam Girdwood 
1337f2ed6b07SMengdong Lin 	/* machine specific init */
1338f2ed6b07SMengdong Lin 	if (component->init) {
1339f2ed6b07SMengdong Lin 		ret = component->init(component);
1340f2ed6b07SMengdong Lin 		if (ret < 0) {
1341f2ed6b07SMengdong Lin 			dev_err(component->dev,
1342f2ed6b07SMengdong Lin 				"Failed to do machine specific init %d\n", ret);
1343f2ed6b07SMengdong Lin 			goto err_probe;
1344f2ed6b07SMengdong Lin 		}
1345f2ed6b07SMengdong Lin 	}
1346f2ed6b07SMengdong Lin 
1347b8972bf0SKuninori Morimoto 	if (component->driver->controls)
1348b8972bf0SKuninori Morimoto 		snd_soc_add_component_controls(component,
1349b8972bf0SKuninori Morimoto 					       component->driver->controls,
1350b8972bf0SKuninori Morimoto 					       component->driver->num_controls);
13516969b2baSKuninori Morimoto 	if (component->driver->dapm_routes)
13526969b2baSKuninori Morimoto 		snd_soc_dapm_add_routes(dapm,
13536969b2baSKuninori Morimoto 					component->driver->dapm_routes,
13546969b2baSKuninori Morimoto 					component->driver->num_dapm_routes);
1355956245e9SLiam Girdwood 
1356f1d45cc3SLars-Peter Clausen 	list_add(&dapm->list, &card->dapm_list);
1357d9fc4063SKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1358956245e9SLiam Girdwood 
1359956245e9SLiam Girdwood 	return 0;
1360956245e9SLiam Girdwood 
1361956245e9SLiam Girdwood err_probe:
1362f1d45cc3SLars-Peter Clausen 	soc_cleanup_component_debugfs(component);
1363abd31b32SLars-Peter Clausen 	component->card = NULL;
1364f1d45cc3SLars-Peter Clausen 	module_put(component->dev->driver->owner);
1365956245e9SLiam Girdwood 
1366956245e9SLiam Girdwood 	return ret;
1367956245e9SLiam Girdwood }
1368956245e9SLiam Girdwood 
136936ae1a96SMark Brown static void rtd_release(struct device *dev)
137036ae1a96SMark Brown {
137136ae1a96SMark Brown 	kfree(dev);
137236ae1a96SMark Brown }
1373f0fba2adSLiam Girdwood 
13745f3484acSLars-Peter Clausen static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
13755f3484acSLars-Peter Clausen 	const char *name)
1376503ae5e0SMisael Lopez Cruz {
1377589c3563SJarkko Nikula 	int ret = 0;
1378589c3563SJarkko Nikula 
1379589c3563SJarkko Nikula 	/* register the rtd device */
138036ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
138136ae1a96SMark Brown 	if (!rtd->dev)
138236ae1a96SMark Brown 		return -ENOMEM;
138336ae1a96SMark Brown 	device_initialize(rtd->dev);
13845f3484acSLars-Peter Clausen 	rtd->dev->parent = rtd->card->dev;
138536ae1a96SMark Brown 	rtd->dev->release = rtd_release;
1386d29697dcSTakashi Iwai 	rtd->dev->groups = soc_dev_attr_groups;
1387f294afedSLars-Peter Clausen 	dev_set_name(rtd->dev, "%s", name);
138836ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1389b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
139001d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
139101d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
139201d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
139301d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
139436ae1a96SMark Brown 	ret = device_add(rtd->dev);
1395589c3563SJarkko Nikula 	if (ret < 0) {
1396865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1397865df9cbSChuansheng Liu 		put_device(rtd->dev);
13985f3484acSLars-Peter Clausen 		dev_err(rtd->card->dev,
1399f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1400589c3563SJarkko Nikula 		return ret;
1401589c3563SJarkko Nikula 	}
1402589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1403589c3563SJarkko Nikula 	return 0;
1404589c3563SJarkko Nikula }
1405589c3563SJarkko Nikula 
14061a497983SMengdong Lin static int soc_probe_link_components(struct snd_soc_card *card,
14071a497983SMengdong Lin 			struct snd_soc_pcm_runtime *rtd,
140862ae68faSStephen Warren 				     int order)
140962ae68faSStephen Warren {
1410f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component;
141190be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
141290be711eSKuninori Morimoto 	int ret;
141362ae68faSStephen Warren 
141490be711eSKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
141590be711eSKuninori Morimoto 		component = rtdcom->component;
141690be711eSKuninori Morimoto 
141770090bbbSLars-Peter Clausen 		if (component->driver->probe_order == order) {
1418f1d45cc3SLars-Peter Clausen 			ret = soc_probe_component(card, component);
141962ae68faSStephen Warren 			if (ret < 0)
142062ae68faSStephen Warren 				return ret;
142162ae68faSStephen Warren 		}
142262ae68faSStephen Warren 	}
142362ae68faSStephen Warren 
142462ae68faSStephen Warren 	return 0;
142562ae68faSStephen Warren }
142662ae68faSStephen Warren 
14278e2be562SLars-Peter Clausen static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1428b0aa88afSMisael Lopez Cruz {
14297a2ccad5SKuninori Morimoto 	if (dai->probed ||
14307a2ccad5SKuninori Morimoto 	    dai->driver->probe_order != order)
14317a2ccad5SKuninori Morimoto 		return 0;
1432b0aa88afSMisael Lopez Cruz 
14338e2be562SLars-Peter Clausen 	if (dai->driver->probe) {
14347a2ccad5SKuninori Morimoto 		int ret = dai->driver->probe(dai);
1435b0aa88afSMisael Lopez Cruz 		if (ret < 0) {
14367a2ccad5SKuninori Morimoto 			dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
14378e2be562SLars-Peter Clausen 				dai->name, ret);
1438b0aa88afSMisael Lopez Cruz 			return ret;
1439b0aa88afSMisael Lopez Cruz 		}
1440b0aa88afSMisael Lopez Cruz 	}
1441b0aa88afSMisael Lopez Cruz 
14428e2be562SLars-Peter Clausen 	dai->probed = 1;
1443b0aa88afSMisael Lopez Cruz 
1444b0aa88afSMisael Lopez Cruz 	return 0;
1445b0aa88afSMisael Lopez Cruz }
1446b0aa88afSMisael Lopez Cruz 
144725f7b701SArnaud Pouliquen static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
144825f7b701SArnaud Pouliquen 				struct snd_soc_pcm_runtime *rtd)
144925f7b701SArnaud Pouliquen {
145025f7b701SArnaud Pouliquen 	int i, ret = 0;
145125f7b701SArnaud Pouliquen 
145225f7b701SArnaud Pouliquen 	for (i = 0; i < num_dais; ++i) {
145325f7b701SArnaud Pouliquen 		struct snd_soc_dai_driver *drv = dais[i]->driver;
145425f7b701SArnaud Pouliquen 
145525f7b701SArnaud Pouliquen 		if (!rtd->dai_link->no_pcm && drv->pcm_new)
145625f7b701SArnaud Pouliquen 			ret = drv->pcm_new(rtd, dais[i]);
145725f7b701SArnaud Pouliquen 		if (ret < 0) {
145825f7b701SArnaud Pouliquen 			dev_err(dais[i]->dev,
145925f7b701SArnaud Pouliquen 				"ASoC: Failed to bind %s with pcm device\n",
146025f7b701SArnaud Pouliquen 				dais[i]->name);
146125f7b701SArnaud Pouliquen 			return ret;
146225f7b701SArnaud Pouliquen 		}
146325f7b701SArnaud Pouliquen 	}
146425f7b701SArnaud Pouliquen 
146525f7b701SArnaud Pouliquen 	return 0;
146625f7b701SArnaud Pouliquen }
146725f7b701SArnaud Pouliquen 
14681a497983SMengdong Lin static int soc_probe_link_dais(struct snd_soc_card *card,
14691a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1470f0fba2adSLiam Girdwood {
14711a497983SMengdong Lin 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1472c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1473a655de80SLiam Girdwood 	struct snd_soc_rtdcom_list *rtdcom;
1474a655de80SLiam Girdwood 	struct snd_soc_component *component;
14750b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1476a655de80SLiam Girdwood 	int i, ret, num;
1477f0fba2adSLiam Girdwood 
1478f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
14791a497983SMengdong Lin 			card->name, rtd->num, order);
1480f0fba2adSLiam Girdwood 
1481f0fba2adSLiam Girdwood 	/* set default power off timeout */
1482f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1483f0fba2adSLiam Girdwood 
14848e2be562SLars-Peter Clausen 	ret = soc_probe_dai(cpu_dai, order);
14858e2be562SLars-Peter Clausen 	if (ret)
1486f0fba2adSLiam Girdwood 		return ret;
1487f0fba2adSLiam Girdwood 
1488f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
14890b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
14900b7990e3SKuninori Morimoto 		ret = soc_probe_dai(codec_dai, order);
1491b0aa88afSMisael Lopez Cruz 		if (ret)
1492f0fba2adSLiam Girdwood 			return ret;
149388bd870fSBenoit Cousson 	}
1494f0fba2adSLiam Girdwood 
14950168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
14960168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
14970168bf0dSLiam Girdwood 		return 0;
14980168bf0dSLiam Girdwood 
14995f3484acSLars-Peter Clausen 	/* do machine specific initialization */
15005f3484acSLars-Peter Clausen 	if (dai_link->init) {
15015f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
15025f3484acSLars-Peter Clausen 		if (ret < 0) {
15035f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
15045f3484acSLars-Peter Clausen 				dai_link->name, ret);
15055f3484acSLars-Peter Clausen 			return ret;
15065f3484acSLars-Peter Clausen 		}
15075f3484acSLars-Peter Clausen 	}
15085f3484acSLars-Peter Clausen 
1509a5053a8eSKuninori Morimoto 	if (dai_link->dai_fmt)
1510a5053a8eSKuninori Morimoto 		snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1511a5053a8eSKuninori Morimoto 
15125f3484acSLars-Peter Clausen 	ret = soc_post_component_init(rtd, dai_link->name);
1513589c3563SJarkko Nikula 	if (ret)
1514f0fba2adSLiam Girdwood 		return ret;
1515f0fba2adSLiam Girdwood 
15165f3484acSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
15175f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
15182e55b90aSLars-Peter Clausen 	if (dai_link->dynamic)
15192e55b90aSLars-Peter Clausen 		soc_dpcm_debugfs_add(rtd);
15205f3484acSLars-Peter Clausen #endif
15215f3484acSLars-Peter Clausen 
1522a655de80SLiam Girdwood 	num = rtd->num;
1523a655de80SLiam Girdwood 
1524a655de80SLiam Girdwood 	/*
1525a655de80SLiam Girdwood 	 * most drivers will register their PCMs using DAI link ordering but
1526a655de80SLiam Girdwood 	 * topology based drivers can use the DAI link id field to set PCM
1527a655de80SLiam Girdwood 	 * device number and then use rtd + a base offset of the BEs.
1528a655de80SLiam Girdwood 	 */
1529a655de80SLiam Girdwood 	for_each_rtdcom(rtd, rtdcom) {
1530a655de80SLiam Girdwood 		component = rtdcom->component;
1531a655de80SLiam Girdwood 
1532a655de80SLiam Girdwood 		if (!component->driver->use_dai_pcm_id)
1533a655de80SLiam Girdwood 			continue;
1534a655de80SLiam Girdwood 
1535a655de80SLiam Girdwood 		if (rtd->dai_link->no_pcm)
1536a655de80SLiam Girdwood 			num += component->driver->be_pcm_base;
1537a655de80SLiam Girdwood 		else
1538a655de80SLiam Girdwood 			num = rtd->dai_link->id;
1539a655de80SLiam Girdwood 	}
1540a655de80SLiam Girdwood 
15416f0c4226SJie Yang 	if (cpu_dai->driver->compress_new) {
15421245b700SNamarta Kohli 		/*create compress_device"*/
1543a655de80SLiam Girdwood 		ret = cpu_dai->driver->compress_new(rtd, num);
15441245b700SNamarta Kohli 		if (ret < 0) {
1545f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
15461245b700SNamarta Kohli 					 dai_link->stream_name);
15471245b700SNamarta Kohli 			return ret;
15481245b700SNamarta Kohli 		}
15491245b700SNamarta Kohli 	} else {
15501245b700SNamarta Kohli 
1551c74184edSMark Brown 		if (!dai_link->params) {
1552f0fba2adSLiam Girdwood 			/* create the pcm */
1553a655de80SLiam Girdwood 			ret = soc_new_pcm(rtd, num);
1554f0fba2adSLiam Girdwood 			if (ret < 0) {
1555f110bfc7SLiam Girdwood 				dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
15560837fc62SFabio Estevam 				       dai_link->stream_name, ret);
1557f0fba2adSLiam Girdwood 				return ret;
1558f0fba2adSLiam Girdwood 			}
155925f7b701SArnaud Pouliquen 			ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
156025f7b701SArnaud Pouliquen 			if (ret < 0)
156125f7b701SArnaud Pouliquen 				return ret;
156225f7b701SArnaud Pouliquen 			ret = soc_link_dai_pcm_new(rtd->codec_dais,
156325f7b701SArnaud Pouliquen 						   rtd->num_codecs, rtd);
156425f7b701SArnaud Pouliquen 			if (ret < 0)
156525f7b701SArnaud Pouliquen 				return ret;
1566c74184edSMark Brown 		} else {
15679d58a077SRichard Fitzgerald 			INIT_DELAYED_WORK(&rtd->delayed_work,
15689d58a077SRichard Fitzgerald 						codec2codec_close_delayed_work);
1569c74184edSMark Brown 		}
1570c74184edSMark Brown 	}
1571c74184edSMark Brown 
1572f0fba2adSLiam Girdwood 	return 0;
1573f0fba2adSLiam Girdwood }
1574f0fba2adSLiam Girdwood 
157544c69bb1SLars-Peter Clausen static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1576b19e6e7bSMark Brown {
15773ca041edSSebastian Reichel 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1578f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1579f2ed6b07SMengdong Lin 	const char *name;
1580f2ed6b07SMengdong Lin 	struct device_node *codec_of_node;
15813ca041edSSebastian Reichel 
1582f2ed6b07SMengdong Lin 	if (aux_dev->codec_of_node || aux_dev->codec_name) {
1583f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1584f2ed6b07SMengdong Lin 		name = aux_dev->codec_name;
1585f2ed6b07SMengdong Lin 		codec_of_node = aux_dev->codec_of_node;
1586f2ed6b07SMengdong Lin 		component = soc_find_component(codec_of_node, name);
1587f2ed6b07SMengdong Lin 		if (!component) {
1588f2ed6b07SMengdong Lin 			if (codec_of_node)
1589f2ed6b07SMengdong Lin 				name = of_node_full_name(codec_of_node);
1590f2ed6b07SMengdong Lin 			goto err_defer;
1591f2ed6b07SMengdong Lin 		}
1592f2ed6b07SMengdong Lin 	} else if (aux_dev->name) {
1593f2ed6b07SMengdong Lin 		/* generic components */
1594f2ed6b07SMengdong Lin 		name = aux_dev->name;
1595f2ed6b07SMengdong Lin 		component = soc_find_component(NULL, name);
1596f2ed6b07SMengdong Lin 		if (!component)
1597f2ed6b07SMengdong Lin 			goto err_defer;
1598f2ed6b07SMengdong Lin 	} else {
1599f2ed6b07SMengdong Lin 		dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1600f2ed6b07SMengdong Lin 		return -EINVAL;
1601f2ed6b07SMengdong Lin 	}
16023ca041edSSebastian Reichel 
1603f2ed6b07SMengdong Lin 	component->init = aux_dev->init;
1604d2e3a135SSylwester Nawrocki 	list_add(&component->card_aux_list, &card->aux_comp_list);
16051a653aa4SKuninori Morimoto 
1606f2ed6b07SMengdong Lin 	return 0;
1607f2ed6b07SMengdong Lin 
1608f2ed6b07SMengdong Lin err_defer:
160965d9361fSLars-Peter Clausen 	dev_err(card->dev, "ASoC: %s not registered\n", name);
1610b19e6e7bSMark Brown 	return -EPROBE_DEFER;
1611b19e6e7bSMark Brown }
1612b19e6e7bSMark Brown 
1613f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1614f2ed6b07SMengdong Lin {
1615991454e1SKuninori Morimoto 	struct snd_soc_component *comp;
1616f2ed6b07SMengdong Lin 	int order;
1617f2ed6b07SMengdong Lin 	int ret;
1618f2ed6b07SMengdong Lin 
1619f2ed6b07SMengdong Lin 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1620f2ed6b07SMengdong Lin 		order++) {
1621991454e1SKuninori Morimoto 		list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
1622f2ed6b07SMengdong Lin 			if (comp->driver->probe_order == order) {
1623f2ed6b07SMengdong Lin 				ret = soc_probe_component(card,	comp);
1624f2ed6b07SMengdong Lin 				if (ret < 0) {
1625f2ed6b07SMengdong Lin 					dev_err(card->dev,
1626f2ed6b07SMengdong Lin 						"ASoC: failed to probe aux component %s %d\n",
1627f2ed6b07SMengdong Lin 						comp->name, ret);
1628f2ed6b07SMengdong Lin 					return ret;
1629f2ed6b07SMengdong Lin 				}
1630f2ed6b07SMengdong Lin 			}
1631f2ed6b07SMengdong Lin 		}
1632f2ed6b07SMengdong Lin 	}
163365d9361fSLars-Peter Clausen 
163444c69bb1SLars-Peter Clausen 	return 0;
16353ca041edSSebastian Reichel }
16362eea392dSJarkko Nikula 
1637f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
163844c69bb1SLars-Peter Clausen {
1639f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1640f2ed6b07SMengdong Lin 	int order;
164144c69bb1SLars-Peter Clausen 
1642f2ed6b07SMengdong Lin 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1643f2ed6b07SMengdong Lin 		order++) {
1644f2ed6b07SMengdong Lin 		list_for_each_entry_safe(comp, _comp,
1645991454e1SKuninori Morimoto 			&card->aux_comp_list, card_aux_list) {
16461a653aa4SKuninori Morimoto 
1647f2ed6b07SMengdong Lin 			if (comp->driver->remove_order == order) {
1648f2ed6b07SMengdong Lin 				soc_remove_component(comp);
1649991454e1SKuninori Morimoto 				/* remove it from the card's aux_comp_list */
1650991454e1SKuninori Morimoto 				list_del(&comp->card_aux_list);
16512eea392dSJarkko Nikula 			}
16525f3484acSLars-Peter Clausen 		}
16532eea392dSJarkko Nikula 	}
16542eea392dSJarkko Nikula }
16552eea392dSJarkko Nikula 
1656ce64c8b9SLars-Peter Clausen /**
1657ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1658ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1659ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1660ce64c8b9SLars-Peter Clausen  *
1661ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1662ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1663ce64c8b9SLars-Peter Clausen  *
1664ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1665ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1666ce64c8b9SLars-Peter Clausen  *
1667ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1668ce64c8b9SLars-Peter Clausen  */
1669ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1670ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1671ce64c8b9SLars-Peter Clausen {
1672ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
16730b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1674ce64c8b9SLars-Peter Clausen 	unsigned int i;
1675ce64c8b9SLars-Peter Clausen 	int ret;
1676ce64c8b9SLars-Peter Clausen 
16770b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1678ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1679ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1680ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1681ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1682ce64c8b9SLars-Peter Clausen 			return ret;
1683ce64c8b9SLars-Peter Clausen 		}
1684ce64c8b9SLars-Peter Clausen 	}
1685ce64c8b9SLars-Peter Clausen 
1686ce64c8b9SLars-Peter Clausen 	/* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1687cb2cf0deSKuninori Morimoto 	/* the component which has non_legacy_dai_naming is Codec */
1688999f7f5aSKuninori Morimoto 	if (cpu_dai->component->driver->non_legacy_dai_naming) {
1689ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1690ce64c8b9SLars-Peter Clausen 
1691ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1692ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1693ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1694ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1695ce64c8b9SLars-Peter Clausen 			break;
1696ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1697ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1698ce64c8b9SLars-Peter Clausen 			break;
1699ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1700ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1701ce64c8b9SLars-Peter Clausen 			break;
1702ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1703ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1704ce64c8b9SLars-Peter Clausen 			break;
1705ce64c8b9SLars-Peter Clausen 		}
1706ce64c8b9SLars-Peter Clausen 
1707ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1708ce64c8b9SLars-Peter Clausen 	}
1709ce64c8b9SLars-Peter Clausen 
1710ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1711ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1712ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1713ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1714ce64c8b9SLars-Peter Clausen 		return ret;
1715ce64c8b9SLars-Peter Clausen 	}
1716ce64c8b9SLars-Peter Clausen 
1717ce64c8b9SLars-Peter Clausen 	return 0;
1718ce64c8b9SLars-Peter Clausen }
1719ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1720ce64c8b9SLars-Peter Clausen 
1721345233d7SLiam Girdwood 
17221f5a4535STakashi Iwai #ifdef CONFIG_DMI
1723345233d7SLiam Girdwood /* Trim special characters, and replace '-' with '_' since '-' is used to
1724345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1725345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1726345233d7SLiam Girdwood  */
1727345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1728345233d7SLiam Girdwood {
1729345233d7SLiam Girdwood 	int i, j = 0;
1730345233d7SLiam Girdwood 
1731345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1732345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1733345233d7SLiam Girdwood 		    || (name[i] == '_'))
1734345233d7SLiam Girdwood 			name[j++] = name[i];
1735345233d7SLiam Girdwood 		else if (name[i] == '-')
1736345233d7SLiam Girdwood 			name[j++] = '_';
1737345233d7SLiam Girdwood 	}
1738345233d7SLiam Girdwood 
1739345233d7SLiam Girdwood 	name[j] = '\0';
1740345233d7SLiam Girdwood }
1741345233d7SLiam Girdwood 
174298faf436SMengdong Lin /* Check if a DMI field is valid, i.e. not containing any string
174398faf436SMengdong Lin  * in the black list.
174498faf436SMengdong Lin  */
174598faf436SMengdong Lin static int is_dmi_valid(const char *field)
174698faf436SMengdong Lin {
174798faf436SMengdong Lin 	int i = 0;
174898faf436SMengdong Lin 
174998faf436SMengdong Lin 	while (dmi_blacklist[i]) {
175098faf436SMengdong Lin 		if (strstr(field, dmi_blacklist[i]))
175198faf436SMengdong Lin 			return 0;
175298faf436SMengdong Lin 		i++;
175346b5a4d2SWu Fengguang 	}
175498faf436SMengdong Lin 
175598faf436SMengdong Lin 	return 1;
175698faf436SMengdong Lin }
175798faf436SMengdong Lin 
1758345233d7SLiam Girdwood /**
1759345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
1760345233d7SLiam Girdwood  * @card: The card to register DMI names
1761345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
1762345233d7SLiam Girdwood  *
1763345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
1764345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
1765345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
1766345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
1767345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
1768345233d7SLiam Girdwood  * as the sound card long name, in the format of
1769345233d7SLiam Girdwood  * "vendor-product-version-board"
1770345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
1771345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
1772345233d7SLiam Girdwood  * (UCM) configurations for the card.
1773345233d7SLiam Girdwood  *
1774345233d7SLiam Girdwood  * Possible card long names may be:
1775345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
1776345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1777345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1778345233d7SLiam Girdwood  *
1779345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
1780345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
1781345233d7SLiam Girdwood  *
1782345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
1783345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
1784345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
1785345233d7SLiam Girdwood  * charactors like SPACE.
1786345233d7SLiam Girdwood  *
1787345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
1788345233d7SLiam Girdwood  */
1789345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1790345233d7SLiam Girdwood {
1791345233d7SLiam Girdwood 	const char *vendor, *product, *product_version, *board;
1792345233d7SLiam Girdwood 	size_t longname_buf_size = sizeof(card->snd_card->longname);
1793345233d7SLiam Girdwood 	size_t len;
1794345233d7SLiam Girdwood 
1795345233d7SLiam Girdwood 	if (card->long_name)
1796345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
1797345233d7SLiam Girdwood 
1798345233d7SLiam Girdwood 	/* make up dmi long name as: vendor.product.version.board */
1799345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
180098faf436SMengdong Lin 	if (!vendor || !is_dmi_valid(vendor)) {
1801345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1802345233d7SLiam Girdwood 		return 0;
1803345233d7SLiam Girdwood 	}
1804345233d7SLiam Girdwood 
180598faf436SMengdong Lin 
1806345233d7SLiam Girdwood 	snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1807345233d7SLiam Girdwood 			 "%s", vendor);
1808345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
1809345233d7SLiam Girdwood 
1810345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
181198faf436SMengdong Lin 	if (product && is_dmi_valid(product)) {
1812345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1813345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1814345233d7SLiam Girdwood 			 longname_buf_size - len,
1815345233d7SLiam Girdwood 			 "-%s", product);
1816345233d7SLiam Girdwood 
1817345233d7SLiam Girdwood 		len++;	/* skip the separator "-" */
1818345233d7SLiam Girdwood 		if (len < longname_buf_size)
1819345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1820345233d7SLiam Girdwood 
1821345233d7SLiam Girdwood 		/* some vendors like Lenovo may only put a self-explanatory
1822345233d7SLiam Girdwood 		 * name in the product version field
1823345233d7SLiam Girdwood 		 */
1824345233d7SLiam Girdwood 		product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
182598faf436SMengdong Lin 		if (product_version && is_dmi_valid(product_version)) {
1826345233d7SLiam Girdwood 			len = strlen(card->dmi_longname);
1827345233d7SLiam Girdwood 			snprintf(card->dmi_longname + len,
1828345233d7SLiam Girdwood 				 longname_buf_size - len,
1829345233d7SLiam Girdwood 				 "-%s", product_version);
1830345233d7SLiam Girdwood 
1831345233d7SLiam Girdwood 			len++;
1832345233d7SLiam Girdwood 			if (len < longname_buf_size)
1833345233d7SLiam Girdwood 				cleanup_dmi_name(card->dmi_longname + len);
1834345233d7SLiam Girdwood 		}
1835345233d7SLiam Girdwood 	}
1836345233d7SLiam Girdwood 
1837345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
183898faf436SMengdong Lin 	if (board && is_dmi_valid(board)) {
1839345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1840345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1841345233d7SLiam Girdwood 			 longname_buf_size - len,
1842345233d7SLiam Girdwood 			 "-%s", board);
1843345233d7SLiam Girdwood 
1844345233d7SLiam Girdwood 		len++;
1845345233d7SLiam Girdwood 		if (len < longname_buf_size)
1846345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1847345233d7SLiam Girdwood 	} else if (!product) {
1848345233d7SLiam Girdwood 		/* fall back to using legacy name */
1849345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1850345233d7SLiam Girdwood 		return 0;
1851345233d7SLiam Girdwood 	}
1852345233d7SLiam Girdwood 
1853345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
1854345233d7SLiam Girdwood 	if (flavour) {
1855345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1856345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1857345233d7SLiam Girdwood 			 longname_buf_size - len,
1858345233d7SLiam Girdwood 			 "-%s", flavour);
1859345233d7SLiam Girdwood 
1860345233d7SLiam Girdwood 		len++;
1861345233d7SLiam Girdwood 		if (len < longname_buf_size)
1862345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1863345233d7SLiam Girdwood 	}
1864345233d7SLiam Girdwood 
1865345233d7SLiam Girdwood 	/* set the card long name */
1866345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
1867345233d7SLiam Girdwood 
1868345233d7SLiam Girdwood 	return 0;
1869345233d7SLiam Girdwood }
1870345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
18711f5a4535STakashi Iwai #endif /* CONFIG_DMI */
1872345233d7SLiam Girdwood 
1873a655de80SLiam Girdwood static void soc_check_tplg_fes(struct snd_soc_card *card)
1874a655de80SLiam Girdwood {
1875a655de80SLiam Girdwood 	struct snd_soc_component *component;
1876a655de80SLiam Girdwood 	const struct snd_soc_component_driver *comp_drv;
1877a655de80SLiam Girdwood 	struct snd_soc_dai_link *dai_link;
1878a655de80SLiam Girdwood 	int i;
1879a655de80SLiam Girdwood 
1880a655de80SLiam Girdwood 	list_for_each_entry(component, &component_list, list) {
1881a655de80SLiam Girdwood 
1882a655de80SLiam Girdwood 		/* does this component override FEs ? */
1883a655de80SLiam Girdwood 		if (!component->driver->ignore_machine)
1884a655de80SLiam Girdwood 			continue;
1885a655de80SLiam Girdwood 
1886a655de80SLiam Girdwood 		/* for this machine ? */
1887a655de80SLiam Girdwood 		if (strcmp(component->driver->ignore_machine,
1888a655de80SLiam Girdwood 			   card->dev->driver->name))
1889a655de80SLiam Girdwood 			continue;
1890a655de80SLiam Girdwood 
1891a655de80SLiam Girdwood 		/* machine matches, so override the rtd data */
18927fe072b4SKuninori Morimoto 		for_each_card_prelinks(card, i, dai_link) {
1893a655de80SLiam Girdwood 
1894a655de80SLiam Girdwood 			/* ignore this FE */
1895a655de80SLiam Girdwood 			if (dai_link->dynamic) {
1896a655de80SLiam Girdwood 				dai_link->ignore = true;
1897a655de80SLiam Girdwood 				continue;
1898a655de80SLiam Girdwood 			}
1899a655de80SLiam Girdwood 
1900a655de80SLiam Girdwood 			dev_info(card->dev, "info: override FE DAI link %s\n",
1901a655de80SLiam Girdwood 				 card->dai_link[i].name);
1902a655de80SLiam Girdwood 
1903a655de80SLiam Girdwood 			/* override platform component */
1904daecf46eSKuninori Morimoto 			if (snd_soc_init_platform(card, dai_link) < 0) {
1905daecf46eSKuninori Morimoto 				dev_err(card->dev, "init platform error");
1906daecf46eSKuninori Morimoto 				continue;
1907daecf46eSKuninori Morimoto 			}
1908daecf46eSKuninori Morimoto 			dai_link->platform->name = component->name;
1909a655de80SLiam Girdwood 
1910a655de80SLiam Girdwood 			/* convert non BE into BE */
1911a655de80SLiam Girdwood 			dai_link->no_pcm = 1;
1912a655de80SLiam Girdwood 
1913a655de80SLiam Girdwood 			/* override any BE fixups */
1914a655de80SLiam Girdwood 			dai_link->be_hw_params_fixup =
1915a655de80SLiam Girdwood 				component->driver->be_hw_params_fixup;
1916a655de80SLiam Girdwood 
1917a655de80SLiam Girdwood 			/* most BE links don't set stream name, so set it to
1918a655de80SLiam Girdwood 			 * dai link name if it's NULL to help bind widgets.
1919a655de80SLiam Girdwood 			 */
1920a655de80SLiam Girdwood 			if (!dai_link->stream_name)
1921a655de80SLiam Girdwood 				dai_link->stream_name = dai_link->name;
1922a655de80SLiam Girdwood 		}
1923a655de80SLiam Girdwood 
1924a655de80SLiam Girdwood 		/* Inform userspace we are using alternate topology */
1925a655de80SLiam Girdwood 		if (component->driver->topology_name_prefix) {
1926a655de80SLiam Girdwood 
1927a655de80SLiam Girdwood 			/* topology shortname created ? */
1928a655de80SLiam Girdwood 			if (!card->topology_shortname_created) {
1929a655de80SLiam Girdwood 				comp_drv = component->driver;
1930a655de80SLiam Girdwood 
1931a655de80SLiam Girdwood 				snprintf(card->topology_shortname, 32, "%s-%s",
1932a655de80SLiam Girdwood 					 comp_drv->topology_name_prefix,
1933a655de80SLiam Girdwood 					 card->name);
1934a655de80SLiam Girdwood 				card->topology_shortname_created = true;
1935a655de80SLiam Girdwood 			}
1936a655de80SLiam Girdwood 
1937a655de80SLiam Girdwood 			/* use topology shortname */
1938a655de80SLiam Girdwood 			card->name = card->topology_shortname;
1939a655de80SLiam Girdwood 		}
1940a655de80SLiam Girdwood 	}
1941a655de80SLiam Girdwood }
1942a655de80SLiam Girdwood 
1943b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1944f0fba2adSLiam Girdwood {
19451a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
194661b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
1947ce64c8b9SLars-Peter Clausen 	int ret, i, order;
194896dd3622SMark Brown 
194934e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
195001b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1951f0fba2adSLiam Girdwood 
1952a655de80SLiam Girdwood 	/* check whether any platform is ignore machine FE and using topology */
1953a655de80SLiam Girdwood 	soc_check_tplg_fes(card);
1954a655de80SLiam Girdwood 
1955b19e6e7bSMark Brown 	/* bind DAIs */
19567fe072b4SKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link) {
19577fe072b4SKuninori Morimoto 		ret = soc_bind_dai_link(card, dai_link);
1958b19e6e7bSMark Brown 		if (ret != 0)
1959b19e6e7bSMark Brown 			goto base_error;
1960db2a4165SFrank Mandarino 	}
1961db2a4165SFrank Mandarino 
196244c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
1963b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
196444c69bb1SLars-Peter Clausen 		ret = soc_bind_aux_dev(card, i);
1965b19e6e7bSMark Brown 		if (ret != 0)
1966b19e6e7bSMark Brown 			goto base_error;
1967db2a4165SFrank Mandarino 	}
1968db2a4165SFrank Mandarino 
1969f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
19707fe072b4SKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link)
19717fe072b4SKuninori Morimoto 		snd_soc_add_dai_link(card, dai_link);
1972f8f80361SMengdong Lin 
1973f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1974102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1975f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1976f0fba2adSLiam Girdwood 	if (ret < 0) {
197710e8aa9aSMichał Mirosław 		dev_err(card->dev,
197810e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
197910e8aa9aSMichał Mirosław 			card->name, ret);
1980b19e6e7bSMark Brown 		goto base_error;
1981db2a4165SFrank Mandarino 	}
1982db2a4165SFrank Mandarino 
19830757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
19840757d834SLars-Peter Clausen 
1985e37a4970SMark Brown 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
1986e37a4970SMark Brown 	card->dapm.dev = card->dev;
1987e37a4970SMark Brown 	card->dapm.card = card;
1988e37a4970SMark Brown 	list_add(&card->dapm.list, &card->dapm_list);
1989e37a4970SMark Brown 
1990d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
1991d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1992d5d1e0beSLars-Peter Clausen #endif
1993d5d1e0beSLars-Peter Clausen 
199488ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
19956ed25978SAndy Green 	/* deferred resume work */
19966308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
19971301a964SRandy Dunlap #endif
19986ed25978SAndy Green 
19999a841ebbSMark Brown 	if (card->dapm_widgets)
20009a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
20019a841ebbSMark Brown 					  card->num_dapm_widgets);
20029a841ebbSMark Brown 
2003f23e860eSNicolin Chen 	if (card->of_dapm_widgets)
2004f23e860eSNicolin Chen 		snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2005f23e860eSNicolin Chen 					  card->num_of_dapm_widgets);
2006f23e860eSNicolin Chen 
2007f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
2008f0fba2adSLiam Girdwood 	if (card->probe) {
2009e7361ec4SMark Brown 		ret = card->probe(card);
2010f0fba2adSLiam Girdwood 		if (ret < 0)
2011f0fba2adSLiam Girdwood 			goto card_probe_error;
2012f0fba2adSLiam Girdwood 	}
2013f0fba2adSLiam Girdwood 
201462ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
20150168bf0dSLiam Girdwood 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
20160168bf0dSLiam Girdwood 			order++) {
2017*bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
20181a497983SMengdong Lin 			ret = soc_probe_link_components(card, rtd, order);
201962ae68faSStephen Warren 			if (ret < 0) {
2020f110bfc7SLiam Girdwood 				dev_err(card->dev,
2021f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
2022f110bfc7SLiam Girdwood 					ret);
202362ae68faSStephen Warren 				goto probe_dai_err;
202462ae68faSStephen Warren 			}
202562ae68faSStephen Warren 		}
202662ae68faSStephen Warren 	}
202762ae68faSStephen Warren 
2028f2ed6b07SMengdong Lin 	/* probe auxiliary components */
2029f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
2030f2ed6b07SMengdong Lin 	if (ret < 0)
2031f2ed6b07SMengdong Lin 		goto probe_dai_err;
2032f2ed6b07SMengdong Lin 
203361b0088bSMengdong Lin 	/* Find new DAI links added during probing components and bind them.
203461b0088bSMengdong Lin 	 * Components with topology may bring new DAIs and DAI links.
203561b0088bSMengdong Lin 	 */
203698061fdbSKuninori Morimoto 	for_each_card_links(card, dai_link) {
203761b0088bSMengdong Lin 		if (soc_is_dai_link_bound(card, dai_link))
203861b0088bSMengdong Lin 			continue;
203961b0088bSMengdong Lin 
204061b0088bSMengdong Lin 		ret = soc_init_dai_link(card, dai_link);
204161b0088bSMengdong Lin 		if (ret)
204261b0088bSMengdong Lin 			goto probe_dai_err;
204361b0088bSMengdong Lin 		ret = soc_bind_dai_link(card, dai_link);
204461b0088bSMengdong Lin 		if (ret)
204561b0088bSMengdong Lin 			goto probe_dai_err;
204661b0088bSMengdong Lin 	}
204761b0088bSMengdong Lin 
204862ae68faSStephen Warren 	/* probe all DAI links on this card */
204962ae68faSStephen Warren 	for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
205062ae68faSStephen Warren 			order++) {
2051*bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
20521a497983SMengdong Lin 			ret = soc_probe_link_dais(card, rtd, order);
2053fe3e78e0SMark Brown 			if (ret < 0) {
2054f110bfc7SLiam Girdwood 				dev_err(card->dev,
2055f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
2056f110bfc7SLiam Girdwood 					ret);
2057f0fba2adSLiam Girdwood 				goto probe_dai_err;
2058fe3e78e0SMark Brown 			}
2059fe3e78e0SMark Brown 		}
20600168bf0dSLiam Girdwood 	}
2061fe3e78e0SMark Brown 
2062888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
2063b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
2064888df395SMark Brown 
2065b7af1dafSMark Brown 	if (card->controls)
2066022658beSLiam Girdwood 		snd_soc_add_card_controls(card, card->controls, card->num_controls);
2067b7af1dafSMark Brown 
2068b8ad29deSMark Brown 	if (card->dapm_routes)
2069b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2070b8ad29deSMark Brown 					card->num_dapm_routes);
2071b8ad29deSMark Brown 
2072f23e860eSNicolin Chen 	if (card->of_dapm_routes)
2073f23e860eSNicolin Chen 		snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2074f23e860eSNicolin Chen 					card->num_of_dapm_routes);
207575d9ac46SMark Brown 
2076861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
2077861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
2078861886d3STakashi Iwai 
2079f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
2080fe3e78e0SMark Brown 		 "%s", card->name);
2081f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
208222de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
2083f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2084f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
2085f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2086f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
2087f0e8ed85SMark Brown 		case '_':
2088f0e8ed85SMark Brown 		case '-':
2089f0e8ed85SMark Brown 		case '\0':
2090f0e8ed85SMark Brown 			break;
2091f0e8ed85SMark Brown 		default:
2092f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
2093f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
2094f0e8ed85SMark Brown 			break;
2095f0e8ed85SMark Brown 		}
2096f0e8ed85SMark Brown 	}
2097fe3e78e0SMark Brown 
209828e9ad92SMark Brown 	if (card->late_probe) {
209928e9ad92SMark Brown 		ret = card->late_probe(card);
210028e9ad92SMark Brown 		if (ret < 0) {
2101f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
210228e9ad92SMark Brown 				card->name, ret);
210328e9ad92SMark Brown 			goto probe_aux_dev_err;
210428e9ad92SMark Brown 		}
210528e9ad92SMark Brown 	}
210628e9ad92SMark Brown 
2107824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
21088c193b8dSLars-Peter Clausen 
2109f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2110fe3e78e0SMark Brown 	if (ret < 0) {
2111f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2112f110bfc7SLiam Girdwood 				ret);
21136b3ed785SAxel Lin 		goto probe_aux_dev_err;
2114fe3e78e0SMark Brown 	}
2115fe3e78e0SMark Brown 
2116435c5e25SMark Brown 	card->instantiated = 1;
21174f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2118f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
211934e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2120b19e6e7bSMark Brown 
2121b19e6e7bSMark Brown 	return 0;
2122db2a4165SFrank Mandarino 
21232eea392dSJarkko Nikula probe_aux_dev_err:
2124f2ed6b07SMengdong Lin 	soc_remove_aux_devices(card);
21252eea392dSJarkko Nikula 
2126f0fba2adSLiam Girdwood probe_dai_err:
21270671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
2128fe3e78e0SMark Brown 
2129f0fba2adSLiam Girdwood card_probe_error:
213087506549SMark Brown 	if (card->remove)
2131e7361ec4SMark Brown 		card->remove(card);
2132f0fba2adSLiam Girdwood 
21332210438bSLars-Peter Clausen 	snd_soc_dapm_free(&card->dapm);
21340757d834SLars-Peter Clausen 	soc_cleanup_card_debugfs(card);
2135f0fba2adSLiam Girdwood 	snd_card_free(card->snd_card);
2136f0fba2adSLiam Girdwood 
2137b19e6e7bSMark Brown base_error:
21381a497983SMengdong Lin 	soc_remove_pcm_runtimes(card);
2139f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
214034e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2141db2a4165SFrank Mandarino 
2142b19e6e7bSMark Brown 	return ret;
2143435c5e25SMark Brown }
2144435c5e25SMark Brown 
2145435c5e25SMark Brown /* probes a new socdev */
2146435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2147435c5e25SMark Brown {
2148f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2149435c5e25SMark Brown 
215070a7ca34SVinod Koul 	/*
215170a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
215270a7ca34SVinod Koul 	 * we should not be here in that case so ret error
215370a7ca34SVinod Koul 	 */
215470a7ca34SVinod Koul 	if (!card)
215570a7ca34SVinod Koul 		return -EINVAL;
215670a7ca34SVinod Koul 
2157fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2158f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2159fe4085e8SMark Brown 		 card->name);
2160fe4085e8SMark Brown 
2161435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2162435c5e25SMark Brown 	card->dev = &pdev->dev;
2163f0fba2adSLiam Girdwood 
216428d528c8SMark Brown 	return snd_soc_register_card(card);
2165435c5e25SMark Brown }
2166435c5e25SMark Brown 
2167b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card)
2168db2a4165SFrank Mandarino {
21691a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
2170db2a4165SFrank Mandarino 
2171f0fba2adSLiam Girdwood 	/* make sure any delayed work runs */
2172*bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd)
217343829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
2174db2a4165SFrank Mandarino 
21754efda5f2STakashi Iwai 	/* free the ALSA card at first; this syncs with pending operations */
21764efda5f2STakashi Iwai 	snd_card_free(card->snd_card);
21774efda5f2STakashi Iwai 
2178f0fba2adSLiam Girdwood 	/* remove and free each DAI */
21790671fd8eSKuninori Morimoto 	soc_remove_dai_links(card);
21801a497983SMengdong Lin 	soc_remove_pcm_runtimes(card);
2181f0fba2adSLiam Girdwood 
2182f2ed6b07SMengdong Lin 	/* remove auxiliary devices */
2183f2ed6b07SMengdong Lin 	soc_remove_aux_devices(card);
2184f2ed6b07SMengdong Lin 
2185d1e81428SMark Brown 	snd_soc_dapm_free(&card->dapm);
2186a6052154SJarkko Nikula 	soc_cleanup_card_debugfs(card);
2187a6052154SJarkko Nikula 
2188f0fba2adSLiam Girdwood 	/* remove the card */
218987506549SMark Brown 	if (card->remove)
2190e7361ec4SMark Brown 		card->remove(card);
2191f0fba2adSLiam Girdwood 
2192b0e26485SVinod Koul 	return 0;
2193b2dfa62cSGuennadi Liakhovetski }
2194b0e26485SVinod Koul 
2195b0e26485SVinod Koul /* removes a socdev */
2196b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2197b0e26485SVinod Koul {
2198b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2199b0e26485SVinod Koul 
2200c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2201db2a4165SFrank Mandarino 	return 0;
2202db2a4165SFrank Mandarino }
2203db2a4165SFrank Mandarino 
22046f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
220551737470SMark Brown {
22066f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
22071a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
220851737470SMark Brown 
220951737470SMark Brown 	if (!card->instantiated)
2210416356fcSMark Brown 		return 0;
221151737470SMark Brown 
221251737470SMark Brown 	/* Flush out pmdown_time work - we actually do want to run it
221351737470SMark Brown 	 * now, we're shutting down so no imminent restart. */
2214*bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd)
221543829731STejun Heo 		flush_delayed_work(&rtd->delayed_work);
221651737470SMark Brown 
2217f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2218416356fcSMark Brown 
2219988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
2220*bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
222188bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
22220b7990e3SKuninori Morimoto 		struct snd_soc_dai *codec_dai;
22231a497983SMengdong Lin 		int i;
222488bd870fSBenoit Cousson 
2225988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
22260b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
222788bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
222888bd870fSBenoit Cousson 		}
2229988e8cc4SNicolin Chen 	}
2230988e8cc4SNicolin Chen 
2231416356fcSMark Brown 	return 0;
223251737470SMark Brown }
22336f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
223451737470SMark Brown 
22356f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2236b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2237b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2238b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2239b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
22406f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2241b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2242416356fcSMark Brown };
2243deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2244416356fcSMark Brown 
2245db2a4165SFrank Mandarino /* ASoC platform driver */
2246db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2247db2a4165SFrank Mandarino 	.driver		= {
2248db2a4165SFrank Mandarino 		.name		= "soc-audio",
22496f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2250db2a4165SFrank Mandarino 	},
2251db2a4165SFrank Mandarino 	.probe		= soc_probe,
2252db2a4165SFrank Mandarino 	.remove		= soc_remove,
2253db2a4165SFrank Mandarino };
2254db2a4165SFrank Mandarino 
2255096e49d5SMark Brown /**
2256db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2257db2a4165SFrank Mandarino  * @_template: control template
2258db2a4165SFrank Mandarino  * @data: control private data
2259ac11a2b3SMark Brown  * @long_name: control long name
2260efb7ac3fSMark Brown  * @prefix: control name prefix
2261db2a4165SFrank Mandarino  *
2262db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2263db2a4165SFrank Mandarino  *
2264db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2265db2a4165SFrank Mandarino  */
2266db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
22673056557fSMark Brown 				  void *data, const char *long_name,
2268efb7ac3fSMark Brown 				  const char *prefix)
2269db2a4165SFrank Mandarino {
2270db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2271efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2272efb7ac3fSMark Brown 	char *name = NULL;
2273db2a4165SFrank Mandarino 
2274db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2275db2a4165SFrank Mandarino 	template.index = 0;
2276db2a4165SFrank Mandarino 
2277efb7ac3fSMark Brown 	if (!long_name)
2278efb7ac3fSMark Brown 		long_name = template.name;
2279efb7ac3fSMark Brown 
2280efb7ac3fSMark Brown 	if (prefix) {
22812b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2282efb7ac3fSMark Brown 		if (!name)
2283efb7ac3fSMark Brown 			return NULL;
2284efb7ac3fSMark Brown 
2285efb7ac3fSMark Brown 		template.name = name;
2286efb7ac3fSMark Brown 	} else {
2287efb7ac3fSMark Brown 		template.name = long_name;
2288efb7ac3fSMark Brown 	}
2289efb7ac3fSMark Brown 
2290efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2291efb7ac3fSMark Brown 
2292efb7ac3fSMark Brown 	kfree(name);
2293efb7ac3fSMark Brown 
2294efb7ac3fSMark Brown 	return kcontrol;
2295db2a4165SFrank Mandarino }
2296db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2297db2a4165SFrank Mandarino 
2298022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2299022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2300022658beSLiam Girdwood 	const char *prefix, void *data)
2301022658beSLiam Girdwood {
2302022658beSLiam Girdwood 	int err, i;
2303022658beSLiam Girdwood 
2304022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2305022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2306022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2307022658beSLiam Girdwood 						     control->name, prefix));
2308022658beSLiam Girdwood 		if (err < 0) {
2309f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2310f110bfc7SLiam Girdwood 				control->name, err);
2311022658beSLiam Girdwood 			return err;
2312022658beSLiam Girdwood 		}
2313022658beSLiam Girdwood 	}
2314022658beSLiam Girdwood 
2315022658beSLiam Girdwood 	return 0;
2316022658beSLiam Girdwood }
2317022658beSLiam Girdwood 
23184fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
23194fefd698SDimitris Papastamos 					       const char *name)
23204fefd698SDimitris Papastamos {
23214fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
23224fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
23234fefd698SDimitris Papastamos 
23244fefd698SDimitris Papastamos 	if (unlikely(!name))
23254fefd698SDimitris Papastamos 		return NULL;
23264fefd698SDimitris Papastamos 
23274fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
23284fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
23294fefd698SDimitris Papastamos 			return kctl;
23304fefd698SDimitris Papastamos 	return NULL;
23314fefd698SDimitris Papastamos }
23324fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
23334fefd698SDimitris Papastamos 
2334db2a4165SFrank Mandarino /**
23350f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
23360f2780adSLars-Peter Clausen  *
23370f2780adSLars-Peter Clausen  * @component: Component to add controls to
23380f2780adSLars-Peter Clausen  * @controls: Array of controls to add
23390f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
23400f2780adSLars-Peter Clausen  *
23410f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
23420f2780adSLars-Peter Clausen  */
23430f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
23440f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
23450f2780adSLars-Peter Clausen {
23460f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
23470f2780adSLars-Peter Clausen 
23480f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
23490f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
23500f2780adSLars-Peter Clausen }
23510f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
23520f2780adSLars-Peter Clausen 
23530f2780adSLars-Peter Clausen /**
2354022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2355022658beSLiam Girdwood  * Convenience function to add a list of controls.
2356022658beSLiam Girdwood  *
2357022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2358022658beSLiam Girdwood  * @controls: array of controls to add
2359022658beSLiam Girdwood  * @num_controls: number of elements in the array
2360022658beSLiam Girdwood  *
2361022658beSLiam Girdwood  * Return 0 for success, else error.
2362022658beSLiam Girdwood  */
2363022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2364022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2365022658beSLiam Girdwood {
2366022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2367022658beSLiam Girdwood 
2368022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2369022658beSLiam Girdwood 			NULL, soc_card);
2370022658beSLiam Girdwood }
2371022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2372022658beSLiam Girdwood 
2373022658beSLiam Girdwood /**
2374022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2375022658beSLiam Girdwood  * Convienience function to add a list of controls.
2376022658beSLiam Girdwood  *
2377022658beSLiam Girdwood  * @dai: DAI to add controls to
2378022658beSLiam Girdwood  * @controls: array of controls to add
2379022658beSLiam Girdwood  * @num_controls: number of elements in the array
2380022658beSLiam Girdwood  *
2381022658beSLiam Girdwood  * Return 0 for success, else error.
2382022658beSLiam Girdwood  */
2383022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2384022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2385022658beSLiam Girdwood {
2386313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2387022658beSLiam Girdwood 
2388022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2389022658beSLiam Girdwood 			NULL, dai);
2390022658beSLiam Girdwood }
2391022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2392022658beSLiam Girdwood 
2393022658beSLiam Girdwood /**
23948c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
23958c6529dbSLiam Girdwood  * @dai: DAI
23968c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
23978c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
23988c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
23998c6529dbSLiam Girdwood  *
24008c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
24018c6529dbSLiam Girdwood  */
24028c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
24038c6529dbSLiam Girdwood 	unsigned int freq, int dir)
24048c6529dbSLiam Girdwood {
240546471925SKuninori Morimoto 	if (dai->driver->ops->set_sysclk)
2406f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
240771ccef0dSKuninori Morimoto 
240871ccef0dSKuninori Morimoto 	return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2409ec4ee52aSMark Brown 					    freq, dir);
24108c6529dbSLiam Girdwood }
24118c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
24128c6529dbSLiam Girdwood 
24138c6529dbSLiam Girdwood /**
241471ccef0dSKuninori Morimoto  * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
241571ccef0dSKuninori Morimoto  * @component: COMPONENT
241671ccef0dSKuninori Morimoto  * @clk_id: DAI specific clock ID
241771ccef0dSKuninori Morimoto  * @source: Source for the clock
241871ccef0dSKuninori Morimoto  * @freq: new clock frequency in Hz
241971ccef0dSKuninori Morimoto  * @dir: new clock direction - input/output.
242071ccef0dSKuninori Morimoto  *
242171ccef0dSKuninori Morimoto  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
242271ccef0dSKuninori Morimoto  */
242371ccef0dSKuninori Morimoto int snd_soc_component_set_sysclk(struct snd_soc_component *component, int clk_id,
242471ccef0dSKuninori Morimoto 			     int source, unsigned int freq, int dir)
242571ccef0dSKuninori Morimoto {
242671ccef0dSKuninori Morimoto 	if (component->driver->set_sysclk)
242771ccef0dSKuninori Morimoto 		return component->driver->set_sysclk(component, clk_id, source,
242871ccef0dSKuninori Morimoto 						 freq, dir);
242971ccef0dSKuninori Morimoto 
243071ccef0dSKuninori Morimoto 	return -ENOTSUPP;
243171ccef0dSKuninori Morimoto }
243271ccef0dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
243371ccef0dSKuninori Morimoto 
243471ccef0dSKuninori Morimoto /**
24358c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
24368c6529dbSLiam Girdwood  * @dai: DAI
2437ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
24388c6529dbSLiam Girdwood  * @div: new clock divisor.
24398c6529dbSLiam Girdwood  *
24408c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
24418c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
24428c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
24438c6529dbSLiam Girdwood  */
24448c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
24458c6529dbSLiam Girdwood 	int div_id, int div)
24468c6529dbSLiam Girdwood {
244746471925SKuninori Morimoto 	if (dai->driver->ops->set_clkdiv)
2448f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
24498c6529dbSLiam Girdwood 	else
24508c6529dbSLiam Girdwood 		return -EINVAL;
24518c6529dbSLiam Girdwood }
24528c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
24538c6529dbSLiam Girdwood 
24548c6529dbSLiam Girdwood /**
24558c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
24568c6529dbSLiam Girdwood  * @dai: DAI
24578c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
245885488037SMark Brown  * @source: DAI specific source for the PLL
24598c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
24608c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
24618c6529dbSLiam Girdwood  *
24628c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
24638c6529dbSLiam Girdwood  */
246485488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
246585488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
24668c6529dbSLiam Girdwood {
246746471925SKuninori Morimoto 	if (dai->driver->ops->set_pll)
2468f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
246985488037SMark Brown 					 freq_in, freq_out);
2470ef641e5dSKuninori Morimoto 
2471ef641e5dSKuninori Morimoto 	return snd_soc_component_set_pll(dai->component, pll_id, source,
2472ec4ee52aSMark Brown 					 freq_in, freq_out);
24738c6529dbSLiam Girdwood }
24748c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
24758c6529dbSLiam Girdwood 
2476ec4ee52aSMark Brown /*
2477ef641e5dSKuninori Morimoto  * snd_soc_component_set_pll - configure component PLL.
2478ef641e5dSKuninori Morimoto  * @component: COMPONENT
2479ef641e5dSKuninori Morimoto  * @pll_id: DAI specific PLL ID
2480ef641e5dSKuninori Morimoto  * @source: DAI specific source for the PLL
2481ef641e5dSKuninori Morimoto  * @freq_in: PLL input clock frequency in Hz
2482ef641e5dSKuninori Morimoto  * @freq_out: requested PLL output clock frequency in Hz
2483ef641e5dSKuninori Morimoto  *
2484ef641e5dSKuninori Morimoto  * Configures and enables PLL to generate output clock based on input clock.
2485ef641e5dSKuninori Morimoto  */
2486ef641e5dSKuninori Morimoto int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2487ef641e5dSKuninori Morimoto 			      int source, unsigned int freq_in,
2488ef641e5dSKuninori Morimoto 			      unsigned int freq_out)
2489ef641e5dSKuninori Morimoto {
2490ef641e5dSKuninori Morimoto 	if (component->driver->set_pll)
2491ef641e5dSKuninori Morimoto 		return component->driver->set_pll(component, pll_id, source,
2492ef641e5dSKuninori Morimoto 					      freq_in, freq_out);
2493ef641e5dSKuninori Morimoto 
2494ef641e5dSKuninori Morimoto 	return -EINVAL;
2495ef641e5dSKuninori Morimoto }
2496ef641e5dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2497ef641e5dSKuninori Morimoto 
24988c6529dbSLiam Girdwood /**
2499e54cf76bSLiam Girdwood  * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2500e54cf76bSLiam Girdwood  * @dai: DAI
2501231b86b1SMasanari Iida  * @ratio: Ratio of BCLK to Sample rate.
2502e54cf76bSLiam Girdwood  *
2503e54cf76bSLiam Girdwood  * Configures the DAI for a preset BCLK to sample rate ratio.
2504e54cf76bSLiam Girdwood  */
2505e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2506e54cf76bSLiam Girdwood {
250746471925SKuninori Morimoto 	if (dai->driver->ops->set_bclk_ratio)
2508e54cf76bSLiam Girdwood 		return dai->driver->ops->set_bclk_ratio(dai, ratio);
2509e54cf76bSLiam Girdwood 	else
2510e54cf76bSLiam Girdwood 		return -EINVAL;
2511e54cf76bSLiam Girdwood }
2512e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2513e54cf76bSLiam Girdwood 
2514e54cf76bSLiam Girdwood /**
25158c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
25168c6529dbSLiam Girdwood  * @dai: DAI
2517bb19ba2aSRandy Dunlap  * @fmt: SND_SOC_DAIFMT_* format value.
25188c6529dbSLiam Girdwood  *
25198c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
25208c6529dbSLiam Girdwood  */
25218c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
25228c6529dbSLiam Girdwood {
25235e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
25245e4ba569SShawn Guo 		return -ENOTSUPP;
25255e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
25268c6529dbSLiam Girdwood }
25278c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
25288c6529dbSLiam Girdwood 
25298c6529dbSLiam Girdwood /**
2530e5c21514SXiubo Li  * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
253189c67857SXiubo Li  * @slots: Number of slots in use.
253289c67857SXiubo Li  * @tx_mask: bitmask representing active TX slots.
253389c67857SXiubo Li  * @rx_mask: bitmask representing active RX slots.
253489c67857SXiubo Li  *
253589c67857SXiubo Li  * Generates the TDM tx and rx slot default masks for DAI.
253689c67857SXiubo Li  */
2537e5c21514SXiubo Li static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
253889c67857SXiubo Li 					  unsigned int *tx_mask,
253989c67857SXiubo Li 					  unsigned int *rx_mask)
254089c67857SXiubo Li {
254189c67857SXiubo Li 	if (*tx_mask || *rx_mask)
254289c67857SXiubo Li 		return 0;
254389c67857SXiubo Li 
254489c67857SXiubo Li 	if (!slots)
254589c67857SXiubo Li 		return -EINVAL;
254689c67857SXiubo Li 
254789c67857SXiubo Li 	*tx_mask = (1 << slots) - 1;
254889c67857SXiubo Li 	*rx_mask = (1 << slots) - 1;
254989c67857SXiubo Li 
255089c67857SXiubo Li 	return 0;
255189c67857SXiubo Li }
255289c67857SXiubo Li 
255389c67857SXiubo Li /**
2554e46c9366SLars-Peter Clausen  * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2555e46c9366SLars-Peter Clausen  * @dai: The DAI to configure
2556a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2557a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
25588c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2559a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
25608c6529dbSLiam Girdwood  *
2561e46c9366SLars-Peter Clausen  * This function configures the specified DAI for TDM operation. @slot contains
2562e46c9366SLars-Peter Clausen  * the total number of slots of the TDM stream and @slot_with the width of each
2563e46c9366SLars-Peter Clausen  * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2564e46c9366SLars-Peter Clausen  * active slots of the TDM stream for the specified DAI, i.e. which slots the
2565e46c9366SLars-Peter Clausen  * DAI should write to or read from. If a bit is set the corresponding slot is
2566e46c9366SLars-Peter Clausen  * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2567e46c9366SLars-Peter Clausen  * the first slot, bit 1 to the second slot and so on. The first active slot
2568e46c9366SLars-Peter Clausen  * maps to the first channel of the DAI, the second active slot to the second
2569e46c9366SLars-Peter Clausen  * channel and so on.
2570e46c9366SLars-Peter Clausen  *
2571e46c9366SLars-Peter Clausen  * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2572e46c9366SLars-Peter Clausen  * @rx_mask and @slot_width will be ignored.
2573e46c9366SLars-Peter Clausen  *
2574e46c9366SLars-Peter Clausen  * Returns 0 on success, a negative error code otherwise.
25758c6529dbSLiam Girdwood  */
25768c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2577a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
25788c6529dbSLiam Girdwood {
257946471925SKuninori Morimoto 	if (dai->driver->ops->xlate_tdm_slot_mask)
2580e5c21514SXiubo Li 		dai->driver->ops->xlate_tdm_slot_mask(slots,
258189c67857SXiubo Li 						&tx_mask, &rx_mask);
258289c67857SXiubo Li 	else
2583e5c21514SXiubo Li 		snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
258489c67857SXiubo Li 
258588bd870fSBenoit Cousson 	dai->tx_mask = tx_mask;
258688bd870fSBenoit Cousson 	dai->rx_mask = rx_mask;
258788bd870fSBenoit Cousson 
258846471925SKuninori Morimoto 	if (dai->driver->ops->set_tdm_slot)
2589f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2590a5479e38SDaniel Ribeiro 				slots, slot_width);
25918c6529dbSLiam Girdwood 	else
2592b2cbb6e1SXiubo Li 		return -ENOTSUPP;
25938c6529dbSLiam Girdwood }
25948c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
25958c6529dbSLiam Girdwood 
25968c6529dbSLiam Girdwood /**
2597472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2598472df3cbSBarry Song  * @dai: DAI
2599472df3cbSBarry Song  * @tx_num: how many TX channels
2600472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2601472df3cbSBarry Song  *           0~num-1 uses
2602472df3cbSBarry Song  * @rx_num: how many RX channels
2603472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2604472df3cbSBarry Song  *           0~num-1 uses
2605472df3cbSBarry Song  *
2606472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2607472df3cbSBarry Song  */
2608472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2609472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
2610472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
2611472df3cbSBarry Song {
261246471925SKuninori Morimoto 	if (dai->driver->ops->set_channel_map)
2613f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2614472df3cbSBarry Song 			rx_num, rx_slot);
2615472df3cbSBarry Song 	else
2616472df3cbSBarry Song 		return -EINVAL;
2617472df3cbSBarry Song }
2618472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2619472df3cbSBarry Song 
2620472df3cbSBarry Song /**
2621467b061fSSrinivas Kandagatla  * snd_soc_dai_get_channel_map - Get DAI audio channel map
2622467b061fSSrinivas Kandagatla  * @dai: DAI
2623467b061fSSrinivas Kandagatla  * @tx_num: how many TX channels
2624467b061fSSrinivas Kandagatla  * @tx_slot: pointer to an array which imply the TX slot number channel
2625467b061fSSrinivas Kandagatla  *           0~num-1 uses
2626467b061fSSrinivas Kandagatla  * @rx_num: how many RX channels
2627467b061fSSrinivas Kandagatla  * @rx_slot: pointer to an array which imply the RX slot number channel
2628467b061fSSrinivas Kandagatla  *           0~num-1 uses
2629467b061fSSrinivas Kandagatla  */
2630467b061fSSrinivas Kandagatla int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
2631467b061fSSrinivas Kandagatla 	unsigned int *tx_num, unsigned int *tx_slot,
2632467b061fSSrinivas Kandagatla 	unsigned int *rx_num, unsigned int *rx_slot)
2633467b061fSSrinivas Kandagatla {
2634467b061fSSrinivas Kandagatla 	if (dai->driver->ops->get_channel_map)
2635467b061fSSrinivas Kandagatla 		return dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
2636467b061fSSrinivas Kandagatla 			rx_num, rx_slot);
2637467b061fSSrinivas Kandagatla 	else
2638467b061fSSrinivas Kandagatla 		return -ENOTSUPP;
2639467b061fSSrinivas Kandagatla }
2640467b061fSSrinivas Kandagatla EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
2641467b061fSSrinivas Kandagatla 
2642467b061fSSrinivas Kandagatla /**
26438c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
26448c6529dbSLiam Girdwood  * @dai: DAI
26458c6529dbSLiam Girdwood  * @tristate: tristate enable
26468c6529dbSLiam Girdwood  *
26478c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
26488c6529dbSLiam Girdwood  */
26498c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
26508c6529dbSLiam Girdwood {
265146471925SKuninori Morimoto 	if (dai->driver->ops->set_tristate)
2652f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
26538c6529dbSLiam Girdwood 	else
26548c6529dbSLiam Girdwood 		return -EINVAL;
26558c6529dbSLiam Girdwood }
26568c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
26578c6529dbSLiam Girdwood 
26588c6529dbSLiam Girdwood /**
26598c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
26608c6529dbSLiam Girdwood  * @dai: DAI
26618c6529dbSLiam Girdwood  * @mute: mute enable
2662da18396fSMark Brown  * @direction: stream to mute
26638c6529dbSLiam Girdwood  *
26648c6529dbSLiam Girdwood  * Mutes the DAI DAC.
26658c6529dbSLiam Girdwood  */
2666da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2667da18396fSMark Brown 			     int direction)
26688c6529dbSLiam Girdwood {
2669da18396fSMark Brown 	if (dai->driver->ops->mute_stream)
2670da18396fSMark Brown 		return dai->driver->ops->mute_stream(dai, mute, direction);
2671da18396fSMark Brown 	else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2672da18396fSMark Brown 		 dai->driver->ops->digital_mute)
2673f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
26748c6529dbSLiam Girdwood 	else
267504570c62SMark Brown 		return -ENOTSUPP;
26768c6529dbSLiam Girdwood }
26778c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
26788c6529dbSLiam Girdwood 
2679e894efefSSrinivas Kandagatla static int snd_soc_bind_card(struct snd_soc_card *card)
2680e894efefSSrinivas Kandagatla {
2681e894efefSSrinivas Kandagatla 	struct snd_soc_pcm_runtime *rtd;
2682e894efefSSrinivas Kandagatla 	int ret;
2683e894efefSSrinivas Kandagatla 
2684e894efefSSrinivas Kandagatla 	ret = snd_soc_instantiate_card(card);
2685e894efefSSrinivas Kandagatla 	if (ret != 0)
2686e894efefSSrinivas Kandagatla 		return ret;
2687e894efefSSrinivas Kandagatla 
2688e894efefSSrinivas Kandagatla 	/* deactivate pins to sleep state */
2689*bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd)  {
2690e894efefSSrinivas Kandagatla 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2691e894efefSSrinivas Kandagatla 		struct snd_soc_dai *codec_dai;
2692e894efefSSrinivas Kandagatla 		int j;
2693e894efefSSrinivas Kandagatla 
2694e894efefSSrinivas Kandagatla 		for_each_rtd_codec_dai(rtd, j, codec_dai) {
2695e894efefSSrinivas Kandagatla 			if (!codec_dai->active)
2696e894efefSSrinivas Kandagatla 				pinctrl_pm_select_sleep_state(codec_dai->dev);
2697e894efefSSrinivas Kandagatla 		}
2698e894efefSSrinivas Kandagatla 
2699e894efefSSrinivas Kandagatla 		if (!cpu_dai->active)
2700e894efefSSrinivas Kandagatla 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
2701e894efefSSrinivas Kandagatla 	}
2702e894efefSSrinivas Kandagatla 
2703e894efefSSrinivas Kandagatla 	return ret;
2704e894efefSSrinivas Kandagatla }
2705e894efefSSrinivas Kandagatla 
2706c5af3a2eSMark Brown /**
2707c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2708c5af3a2eSMark Brown  *
2709ac11a2b3SMark Brown  * @card: Card to register
2710c5af3a2eSMark Brown  *
2711c5af3a2eSMark Brown  */
271270a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2713c5af3a2eSMark Brown {
2714923c5e61SMengdong Lin 	int i, ret;
27157fe072b4SKuninori Morimoto 	struct snd_soc_dai_link *link;
2716f0fba2adSLiam Girdwood 
2717c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2718c5af3a2eSMark Brown 		return -EINVAL;
2719c5af3a2eSMark Brown 
27207fe072b4SKuninori Morimoto 	for_each_card_prelinks(card, i, link) {
27215a504963SStephen Warren 
2722923c5e61SMengdong Lin 		ret = soc_init_dai_link(card, link);
272388bd870fSBenoit Cousson 		if (ret) {
2724923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: failed to init link %s\n",
2725923c5e61SMengdong Lin 				link->name);
272688bd870fSBenoit Cousson 			return ret;
272788bd870fSBenoit Cousson 		}
27285a504963SStephen Warren 	}
27295a504963SStephen Warren 
2730ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2731ed77cc12SMark Brown 
2732111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
2733111c6419SStephen Warren 
2734f8f80361SMengdong Lin 	INIT_LIST_HEAD(&card->dai_link_list);
2735f0fba2adSLiam Girdwood 
27361a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
27379115171aSMark Brown 	card->num_rtd = 0;
2738db2a4165SFrank Mandarino 
2739db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
27408a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2741db2a4165SFrank Mandarino 	card->instantiated = 0;
2742db2a4165SFrank Mandarino 	mutex_init(&card->mutex);
2743a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
2744db2a4165SFrank Mandarino 
2745e894efefSSrinivas Kandagatla 	return snd_soc_bind_card(card);
2746db2a4165SFrank Mandarino }
274770a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2748db2a4165SFrank Mandarino 
2749e894efefSSrinivas Kandagatla static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2750e894efefSSrinivas Kandagatla {
2751e894efefSSrinivas Kandagatla 	if (card->instantiated) {
2752e894efefSSrinivas Kandagatla 		card->instantiated = false;
2753e894efefSSrinivas Kandagatla 		snd_soc_dapm_shutdown(card);
2754e894efefSSrinivas Kandagatla 		soc_cleanup_card_resources(card);
2755e894efefSSrinivas Kandagatla 		if (!unregister)
2756e894efefSSrinivas Kandagatla 			list_add(&card->list, &unbind_card_list);
2757e894efefSSrinivas Kandagatla 	} else {
2758e894efefSSrinivas Kandagatla 		if (unregister)
2759e894efefSSrinivas Kandagatla 			list_del(&card->list);
2760e894efefSSrinivas Kandagatla 	}
2761e894efefSSrinivas Kandagatla }
2762e894efefSSrinivas Kandagatla 
2763db2a4165SFrank Mandarino /**
2764db2a4165SFrank Mandarino  * snd_soc_unregister_card - Unregister a card with the ASoC core
2765db2a4165SFrank Mandarino  *
2766db2a4165SFrank Mandarino  * @card: Card to unregister
2767db2a4165SFrank Mandarino  *
2768db2a4165SFrank Mandarino  */
276970a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2770db2a4165SFrank Mandarino {
2771e894efefSSrinivas Kandagatla 	snd_soc_unbind_card(card, true);
2772f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2773db2a4165SFrank Mandarino 
2774db2a4165SFrank Mandarino 	return 0;
2775db2a4165SFrank Mandarino }
277670a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2777db2a4165SFrank Mandarino 
2778db2a4165SFrank Mandarino /*
2779db2a4165SFrank Mandarino  * Simplify DAI link configuration by removing ".-1" from device names
2780db2a4165SFrank Mandarino  * and sanitizing names.
2781db2a4165SFrank Mandarino  */
27820b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2783db2a4165SFrank Mandarino {
2784db2a4165SFrank Mandarino 	char *found, name[NAME_SIZE];
2785db2a4165SFrank Mandarino 	int id1, id2;
2786db2a4165SFrank Mandarino 
2787db2a4165SFrank Mandarino 	if (dev_name(dev) == NULL)
2788db2a4165SFrank Mandarino 		return NULL;
2789db2a4165SFrank Mandarino 
279058818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2791db2a4165SFrank Mandarino 
2792db2a4165SFrank Mandarino 	/* are we a "%s.%d" name (platform and SPI components) */
2793c5af3a2eSMark Brown 	found = strstr(name, dev->driver->name);
2794c5af3a2eSMark Brown 	if (found) {
2795c5af3a2eSMark Brown 		/* get ID */
2796c5af3a2eSMark Brown 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2797c5af3a2eSMark Brown 
2798c5af3a2eSMark Brown 			/* discard ID from name if ID == -1 */
2799c5af3a2eSMark Brown 			if (*id == -1)
2800c5af3a2eSMark Brown 				found[strlen(dev->driver->name)] = '\0';
2801c5af3a2eSMark Brown 		}
2802c5af3a2eSMark Brown 
2803c5af3a2eSMark Brown 	} else {
2804c5af3a2eSMark Brown 		/* I2C component devices are named "bus-addr"  */
2805c5af3a2eSMark Brown 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2806c5af3a2eSMark Brown 			char tmp[NAME_SIZE];
2807c5af3a2eSMark Brown 
2808c5af3a2eSMark Brown 			/* create unique ID number from I2C addr and bus */
2809c5af3a2eSMark Brown 			*id = ((id1 & 0xffff) << 16) + id2;
2810c5af3a2eSMark Brown 
2811c5af3a2eSMark Brown 			/* sanitize component name for DAI link creation */
2812c5af3a2eSMark Brown 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
281358818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2814c5af3a2eSMark Brown 		} else
2815c5af3a2eSMark Brown 			*id = 0;
2816c5af3a2eSMark Brown 	}
2817c5af3a2eSMark Brown 
2818c5af3a2eSMark Brown 	return kstrdup(name, GFP_KERNEL);
2819c5af3a2eSMark Brown }
2820c5af3a2eSMark Brown 
2821c5af3a2eSMark Brown /*
2822c5af3a2eSMark Brown  * Simplify DAI link naming for single devices with multiple DAIs by removing
2823c5af3a2eSMark Brown  * any ".-1" and using the DAI name (instead of device name).
2824c5af3a2eSMark Brown  */
2825c5af3a2eSMark Brown static inline char *fmt_multiple_name(struct device *dev,
2826c5af3a2eSMark Brown 		struct snd_soc_dai_driver *dai_drv)
2827c5af3a2eSMark Brown {
2828c5af3a2eSMark Brown 	if (dai_drv->name == NULL) {
282910e8aa9aSMichał Mirosław 		dev_err(dev,
283010e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
283110e8aa9aSMichał Mirosław 			dev_name(dev));
2832c5af3a2eSMark Brown 		return NULL;
2833c5af3a2eSMark Brown 	}
2834c5af3a2eSMark Brown 
2835c5af3a2eSMark Brown 	return kstrdup(dai_drv->name, GFP_KERNEL);
2836c5af3a2eSMark Brown }
2837c5af3a2eSMark Brown 
2838c5af3a2eSMark Brown /**
283932c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
28409115171aSMark Brown  *
284132c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
28429115171aSMark Brown  */
284332c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
28449115171aSMark Brown {
28455c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
28469115171aSMark Brown 
28475c1d5f09SLars-Peter Clausen 	list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
284832c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
284932c9ba54SLars-Peter Clausen 			dai->name);
28509115171aSMark Brown 		list_del(&dai->list);
2851f0fba2adSLiam Girdwood 		kfree(dai->name);
2852f0fba2adSLiam Girdwood 		kfree(dai);
28539115171aSMark Brown 	}
285432c9ba54SLars-Peter Clausen }
28559115171aSMark Brown 
28565e4fb372SMengdong Lin /* Create a DAI and add it to the component's DAI list */
28575e4fb372SMengdong Lin static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
28585e4fb372SMengdong Lin 	struct snd_soc_dai_driver *dai_drv,
28595e4fb372SMengdong Lin 	bool legacy_dai_naming)
28605e4fb372SMengdong Lin {
28615e4fb372SMengdong Lin 	struct device *dev = component->dev;
28625e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
28635e4fb372SMengdong Lin 
28645e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
28655e4fb372SMengdong Lin 
28665e4fb372SMengdong Lin 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
28675e4fb372SMengdong Lin 	if (dai == NULL)
28685e4fb372SMengdong Lin 		return NULL;
28695e4fb372SMengdong Lin 
28705e4fb372SMengdong Lin 	/*
28715e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
28725e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
28735e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
28745e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
28755e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
28765e4fb372SMengdong Lin 	 * component-less anymore.
28775e4fb372SMengdong Lin 	 */
28785e4fb372SMengdong Lin 	if (legacy_dai_naming &&
28795e4fb372SMengdong Lin 	   (dai_drv->id == 0 || dai_drv->name == NULL)) {
28805e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
28815e4fb372SMengdong Lin 	} else {
28825e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
28835e4fb372SMengdong Lin 		if (dai_drv->id)
28845e4fb372SMengdong Lin 			dai->id = dai_drv->id;
28855e4fb372SMengdong Lin 		else
28865e4fb372SMengdong Lin 			dai->id = component->num_dai;
28875e4fb372SMengdong Lin 	}
28885e4fb372SMengdong Lin 	if (dai->name == NULL) {
28895e4fb372SMengdong Lin 		kfree(dai);
28905e4fb372SMengdong Lin 		return NULL;
28915e4fb372SMengdong Lin 	}
28925e4fb372SMengdong Lin 
28935e4fb372SMengdong Lin 	dai->component = component;
28945e4fb372SMengdong Lin 	dai->dev = dev;
28955e4fb372SMengdong Lin 	dai->driver = dai_drv;
28965e4fb372SMengdong Lin 	if (!dai->driver->ops)
28975e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
28985e4fb372SMengdong Lin 
289958bf4179SKuninori Morimoto 	list_add_tail(&dai->list, &component->dai_list);
29005e4fb372SMengdong Lin 	component->num_dai++;
29015e4fb372SMengdong Lin 
29025e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
29035e4fb372SMengdong Lin 	return dai;
29045e4fb372SMengdong Lin }
29055e4fb372SMengdong Lin 
29069115171aSMark Brown /**
290732c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
29089115171aSMark Brown  *
29096106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
29106106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
2911ac11a2b3SMark Brown  * @count: Number of DAIs
29129115171aSMark Brown  */
291332c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
29140e7b25c6SKuninori Morimoto 				 struct snd_soc_dai_driver *dai_drv, size_t count)
29159115171aSMark Brown {
29166106d129SLars-Peter Clausen 	struct device *dev = component->dev;
2917f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
291832c9ba54SLars-Peter Clausen 	unsigned int i;
291932c9ba54SLars-Peter Clausen 	int ret;
2920f0fba2adSLiam Girdwood 
29215b5e0928SAlexey Dobriyan 	dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
29229115171aSMark Brown 
29239115171aSMark Brown 	for (i = 0; i < count; i++) {
2924f0fba2adSLiam Girdwood 
29255e4fb372SMengdong Lin 		dai = soc_add_dai(component, dai_drv + i,
29260e7b25c6SKuninori Morimoto 				  count == 1 && !component->driver->non_legacy_dai_naming);
2927c46e0079SAxel Lin 		if (dai == NULL) {
2928c46e0079SAxel Lin 			ret = -ENOMEM;
2929c46e0079SAxel Lin 			goto err;
2930c46e0079SAxel Lin 		}
2931f0fba2adSLiam Girdwood 	}
2932f0fba2adSLiam Girdwood 
29339115171aSMark Brown 	return 0;
29349115171aSMark Brown 
29359115171aSMark Brown err:
293632c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
29379115171aSMark Brown 
29389115171aSMark Brown 	return ret;
29399115171aSMark Brown }
29409115171aSMark Brown 
294168003e6cSMengdong Lin /**
294268003e6cSMengdong Lin  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
294368003e6cSMengdong Lin  *
294468003e6cSMengdong Lin  * @component: The component the DAIs are registered for
294568003e6cSMengdong Lin  * @dai_drv: DAI driver to use for the DAI
294668003e6cSMengdong Lin  *
294768003e6cSMengdong Lin  * Topology can use this API to register DAIs when probing a component.
294868003e6cSMengdong Lin  * These DAIs's widgets will be freed in the card cleanup and the DAIs
294968003e6cSMengdong Lin  * will be freed in the component cleanup.
295068003e6cSMengdong Lin  */
295168003e6cSMengdong Lin int snd_soc_register_dai(struct snd_soc_component *component,
295268003e6cSMengdong Lin 	struct snd_soc_dai_driver *dai_drv)
295368003e6cSMengdong Lin {
295468003e6cSMengdong Lin 	struct snd_soc_dapm_context *dapm =
295568003e6cSMengdong Lin 		snd_soc_component_get_dapm(component);
295668003e6cSMengdong Lin 	struct snd_soc_dai *dai;
295768003e6cSMengdong Lin 	int ret;
295868003e6cSMengdong Lin 
295968003e6cSMengdong Lin 	if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
296068003e6cSMengdong Lin 		dev_err(component->dev, "Invalid dai type %d\n",
296168003e6cSMengdong Lin 			dai_drv->dobj.type);
296268003e6cSMengdong Lin 		return -EINVAL;
296368003e6cSMengdong Lin 	}
296468003e6cSMengdong Lin 
296568003e6cSMengdong Lin 	lockdep_assert_held(&client_mutex);
296668003e6cSMengdong Lin 	dai = soc_add_dai(component, dai_drv, false);
296768003e6cSMengdong Lin 	if (!dai)
296868003e6cSMengdong Lin 		return -ENOMEM;
296968003e6cSMengdong Lin 
297068003e6cSMengdong Lin 	/* Create the DAI widgets here. After adding DAIs, topology may
297168003e6cSMengdong Lin 	 * also add routes that need these widgets as source or sink.
297268003e6cSMengdong Lin 	 */
297368003e6cSMengdong Lin 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
297468003e6cSMengdong Lin 	if (ret != 0) {
297568003e6cSMengdong Lin 		dev_err(component->dev,
297668003e6cSMengdong Lin 			"Failed to create DAI widgets %d\n", ret);
297768003e6cSMengdong Lin 	}
297868003e6cSMengdong Lin 
297968003e6cSMengdong Lin 	return ret;
298068003e6cSMengdong Lin }
298168003e6cSMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_register_dai);
298268003e6cSMengdong Lin 
298314e8bdebSLars-Peter Clausen static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
298414e8bdebSLars-Peter Clausen 	enum snd_soc_dapm_type type, int subseq)
2985d191bd8dSKuninori Morimoto {
298614e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
2987d191bd8dSKuninori Morimoto 
298814e8bdebSLars-Peter Clausen 	component->driver->seq_notifier(component, type, subseq);
298914e8bdebSLars-Peter Clausen }
2990d191bd8dSKuninori Morimoto 
299114e8bdebSLars-Peter Clausen static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
299214e8bdebSLars-Peter Clausen 	int event)
299314e8bdebSLars-Peter Clausen {
299414e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
299514e8bdebSLars-Peter Clausen 
299614e8bdebSLars-Peter Clausen 	return component->driver->stream_event(component, event);
299714e8bdebSLars-Peter Clausen }
299814e8bdebSLars-Peter Clausen 
29997ba236ceSKuninori Morimoto static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
30007ba236ceSKuninori Morimoto 					enum snd_soc_bias_level level)
30017ba236ceSKuninori Morimoto {
30027ba236ceSKuninori Morimoto 	struct snd_soc_component *component = dapm->component;
30037ba236ceSKuninori Morimoto 
30047ba236ceSKuninori Morimoto 	return component->driver->set_bias_level(component, level);
30057ba236ceSKuninori Morimoto }
30067ba236ceSKuninori Morimoto 
3007bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
3008bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
3009d191bd8dSKuninori Morimoto {
3010ce0fc93aSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
3011ce0fc93aSLars-Peter Clausen 
3012bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
3013bb13109dSLars-Peter Clausen 	if (!component->name) {
3014bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
3015d191bd8dSKuninori Morimoto 		return -ENOMEM;
3016d191bd8dSKuninori Morimoto 	}
3017d191bd8dSKuninori Morimoto 
3018bb13109dSLars-Peter Clausen 	component->dev = dev;
3019bb13109dSLars-Peter Clausen 	component->driver = driver;
3020e2c330b9SLars-Peter Clausen 
302188c27465SKuninori Morimoto 	dapm = snd_soc_component_get_dapm(component);
3022ce0fc93aSLars-Peter Clausen 	dapm->dev = dev;
3023ce0fc93aSLars-Peter Clausen 	dapm->component = component;
3024ce0fc93aSLars-Peter Clausen 	dapm->bias_level = SND_SOC_BIAS_OFF;
30257ba236ceSKuninori Morimoto 	dapm->idle_bias_off = !driver->idle_bias_on;
30267ba236ceSKuninori Morimoto 	dapm->suspend_bias_off = driver->suspend_bias_off;
302714e8bdebSLars-Peter Clausen 	if (driver->seq_notifier)
302814e8bdebSLars-Peter Clausen 		dapm->seq_notifier = snd_soc_component_seq_notifier;
302914e8bdebSLars-Peter Clausen 	if (driver->stream_event)
303014e8bdebSLars-Peter Clausen 		dapm->stream_event = snd_soc_component_stream_event;
30317ba236ceSKuninori Morimoto 	if (driver->set_bias_level)
30327ba236ceSKuninori Morimoto 		dapm->set_bias_level = snd_soc_component_set_bias_level;
3033ce0fc93aSLars-Peter Clausen 
3034bb13109dSLars-Peter Clausen 	INIT_LIST_HEAD(&component->dai_list);
3035bb13109dSLars-Peter Clausen 	mutex_init(&component->io_mutex);
3036bb13109dSLars-Peter Clausen 
3037bb13109dSLars-Peter Clausen 	return 0;
3038d191bd8dSKuninori Morimoto }
3039d191bd8dSKuninori Morimoto 
304020feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
3041886f5692SLars-Peter Clausen {
3042886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
304320feb881SLars-Peter Clausen 
3044886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
3045886f5692SLars-Peter Clausen 	if (val_bytes > 0)
3046886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
3047886f5692SLars-Peter Clausen }
304820feb881SLars-Peter Clausen 
3049e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
3050e874bf5fSLars-Peter Clausen 
305120feb881SLars-Peter Clausen /**
305220feb881SLars-Peter Clausen  * snd_soc_component_init_regmap() - Initialize regmap instance for the component
305320feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
305420feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
305520feb881SLars-Peter Clausen  *
305620feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
305720feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
305820feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
305920feb881SLars-Peter Clausen  * before the first IO attempt of the component.
306020feb881SLars-Peter Clausen  */
306120feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
306220feb881SLars-Peter Clausen 	struct regmap *regmap)
306320feb881SLars-Peter Clausen {
306420feb881SLars-Peter Clausen 	component->regmap = regmap;
306520feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
3066886f5692SLars-Peter Clausen }
306720feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
306820feb881SLars-Peter Clausen 
306920feb881SLars-Peter Clausen /**
307020feb881SLars-Peter Clausen  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
307120feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
307220feb881SLars-Peter Clausen  *
307320feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
307420feb881SLars-Peter Clausen  * removes the regmap instance from the component.
307520feb881SLars-Peter Clausen  *
307620feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
307720feb881SLars-Peter Clausen  * to initialize the regmap instance.
307820feb881SLars-Peter Clausen  */
307920feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
308020feb881SLars-Peter Clausen {
308120feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
308220feb881SLars-Peter Clausen 	component->regmap = NULL;
308320feb881SLars-Peter Clausen }
308420feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3085886f5692SLars-Peter Clausen 
3086e874bf5fSLars-Peter Clausen #endif
3087d191bd8dSKuninori Morimoto 
3088359c71eeSKuninori Morimoto static void snd_soc_component_add(struct snd_soc_component *component)
3089bb13109dSLars-Peter Clausen {
3090359c71eeSKuninori Morimoto 	mutex_lock(&client_mutex);
3091359c71eeSKuninori Morimoto 
3092999f7f5aSKuninori Morimoto 	if (!component->driver->write && !component->driver->read) {
309320feb881SLars-Peter Clausen 		if (!component->regmap)
309420feb881SLars-Peter Clausen 			component->regmap = dev_get_regmap(component->dev, NULL);
309520feb881SLars-Peter Clausen 		if (component->regmap)
309620feb881SLars-Peter Clausen 			snd_soc_component_setup_regmap(component);
309720feb881SLars-Peter Clausen 	}
3098886f5692SLars-Peter Clausen 
3099bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
31008a978234SLiam Girdwood 	INIT_LIST_HEAD(&component->dobj_list);
3101d191bd8dSKuninori Morimoto 
3102d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
3103bb13109dSLars-Peter Clausen }
3104d191bd8dSKuninori Morimoto 
3105bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
3106bb13109dSLars-Peter Clausen {
3107bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
3108bb13109dSLars-Peter Clausen 	kfree(component->name);
3109bb13109dSLars-Peter Clausen }
3110d191bd8dSKuninori Morimoto 
3111bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3112bb13109dSLars-Peter Clausen {
3113c12c1aadSKuninori Morimoto 	struct snd_soc_card *card = component->card;
3114c12c1aadSKuninori Morimoto 
3115c12c1aadSKuninori Morimoto 	if (card)
3116e894efefSSrinivas Kandagatla 		snd_soc_unbind_card(card, false);
3117c12c1aadSKuninori Morimoto 
3118bb13109dSLars-Peter Clausen 	list_del(&component->list);
3119bb13109dSLars-Peter Clausen }
3120d191bd8dSKuninori Morimoto 
3121273d778eSKuninori Morimoto #define ENDIANNESS_MAP(name) \
3122273d778eSKuninori Morimoto 	(SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3123273d778eSKuninori Morimoto static u64 endianness_format_map[] = {
3124273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S16_),
3125273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U16_),
3126273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_),
3127273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_),
3128273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S32_),
3129273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U32_),
3130273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_3),
3131273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_3),
3132273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S20_3),
3133273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U20_3),
3134273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S18_3),
3135273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U18_3),
3136273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT_),
3137273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT64_),
3138273d778eSKuninori Morimoto 	ENDIANNESS_MAP(IEC958_SUBFRAME_),
3139273d778eSKuninori Morimoto };
3140273d778eSKuninori Morimoto 
3141273d778eSKuninori Morimoto /*
3142273d778eSKuninori Morimoto  * Fix up the DAI formats for endianness: codecs don't actually see
3143273d778eSKuninori Morimoto  * the endianness of the data but we're using the CPU format
3144273d778eSKuninori Morimoto  * definitions which do need to include endianness so we ensure that
3145273d778eSKuninori Morimoto  * codec DAIs always have both big and little endian variants set.
3146273d778eSKuninori Morimoto  */
3147273d778eSKuninori Morimoto static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3148273d778eSKuninori Morimoto {
3149273d778eSKuninori Morimoto 	int i;
3150273d778eSKuninori Morimoto 
3151273d778eSKuninori Morimoto 	for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3152273d778eSKuninori Morimoto 		if (stream->formats & endianness_format_map[i])
3153273d778eSKuninori Morimoto 			stream->formats |= endianness_format_map[i];
3154273d778eSKuninori Morimoto }
3155273d778eSKuninori Morimoto 
3156e894efefSSrinivas Kandagatla static void snd_soc_try_rebind_card(void)
3157e894efefSSrinivas Kandagatla {
3158e894efefSSrinivas Kandagatla 	struct snd_soc_card *card, *c;
3159e894efefSSrinivas Kandagatla 
3160e894efefSSrinivas Kandagatla 	if (!list_empty(&unbind_card_list)) {
3161e894efefSSrinivas Kandagatla 		list_for_each_entry_safe(card, c, &unbind_card_list, list) {
3162e894efefSSrinivas Kandagatla 			if (!snd_soc_bind_card(card))
3163e894efefSSrinivas Kandagatla 				list_del(&card->list);
3164e894efefSSrinivas Kandagatla 		}
3165e894efefSSrinivas Kandagatla 	}
3166e894efefSSrinivas Kandagatla }
3167e894efefSSrinivas Kandagatla 
3168e0dac41bSKuninori Morimoto int snd_soc_add_component(struct device *dev,
3169e0dac41bSKuninori Morimoto 			struct snd_soc_component *component,
3170cf9e829eSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
3171d191bd8dSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
3172d191bd8dSKuninori Morimoto 			int num_dai)
3173d191bd8dSKuninori Morimoto {
3174bb13109dSLars-Peter Clausen 	int ret;
3175273d778eSKuninori Morimoto 	int i;
3176d191bd8dSKuninori Morimoto 
3177cf9e829eSKuninori Morimoto 	ret = snd_soc_component_initialize(component, component_driver, dev);
3178bb13109dSLars-Peter Clausen 	if (ret)
3179bb13109dSLars-Peter Clausen 		goto err_free;
3180bb13109dSLars-Peter Clausen 
3181273d778eSKuninori Morimoto 	if (component_driver->endianness) {
3182273d778eSKuninori Morimoto 		for (i = 0; i < num_dai; i++) {
3183273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].playback);
3184273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].capture);
3185273d778eSKuninori Morimoto 		}
3186273d778eSKuninori Morimoto 	}
3187273d778eSKuninori Morimoto 
31880e7b25c6SKuninori Morimoto 	ret = snd_soc_register_dais(component, dai_drv, num_dai);
3189bb13109dSLars-Peter Clausen 	if (ret < 0) {
3190f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3191bb13109dSLars-Peter Clausen 		goto err_cleanup;
3192bb13109dSLars-Peter Clausen 	}
3193bb13109dSLars-Peter Clausen 
3194cf9e829eSKuninori Morimoto 	snd_soc_component_add(component);
3195e894efefSSrinivas Kandagatla 	snd_soc_try_rebind_card();
3196bb13109dSLars-Peter Clausen 
3197bb13109dSLars-Peter Clausen 	return 0;
3198bb13109dSLars-Peter Clausen 
3199bb13109dSLars-Peter Clausen err_cleanup:
3200cf9e829eSKuninori Morimoto 	snd_soc_component_cleanup(component);
3201bb13109dSLars-Peter Clausen err_free:
3202bb13109dSLars-Peter Clausen 	return ret;
3203d191bd8dSKuninori Morimoto }
3204e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component);
3205e0dac41bSKuninori Morimoto 
3206e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev,
3207e0dac41bSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
3208e0dac41bSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
3209e0dac41bSKuninori Morimoto 			int num_dai)
3210e0dac41bSKuninori Morimoto {
3211e0dac41bSKuninori Morimoto 	struct snd_soc_component *component;
3212e0dac41bSKuninori Morimoto 
32137ecbd6a9SKuninori Morimoto 	component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
321408e61d03SKuninori Morimoto 	if (!component)
3215e0dac41bSKuninori Morimoto 		return -ENOMEM;
3216e0dac41bSKuninori Morimoto 
3217e0dac41bSKuninori Morimoto 	return snd_soc_add_component(dev, component, component_driver,
3218e0dac41bSKuninori Morimoto 				     dai_drv, num_dai);
3219e0dac41bSKuninori Morimoto }
3220d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
3221d191bd8dSKuninori Morimoto 
3222d191bd8dSKuninori Morimoto /**
32232eccea8cSKuninori Morimoto  * snd_soc_unregister_component - Unregister all related component
32242eccea8cSKuninori Morimoto  * from the ASoC core
3225d191bd8dSKuninori Morimoto  *
3226628536eaSJonathan Corbet  * @dev: The device to unregister
3227d191bd8dSKuninori Morimoto  */
32282eccea8cSKuninori Morimoto static int __snd_soc_unregister_component(struct device *dev)
3229d191bd8dSKuninori Morimoto {
3230cf9e829eSKuninori Morimoto 	struct snd_soc_component *component;
323121a03528SKuninori Morimoto 	int found = 0;
3232d191bd8dSKuninori Morimoto 
323334e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
3234cf9e829eSKuninori Morimoto 	list_for_each_entry(component, &component_list, list) {
3235999f7f5aSKuninori Morimoto 		if (dev != component->dev)
323621a03528SKuninori Morimoto 			continue;
3237d191bd8dSKuninori Morimoto 
3238cf9e829eSKuninori Morimoto 		snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);
3239cf9e829eSKuninori Morimoto 		snd_soc_component_del_unlocked(component);
324021a03528SKuninori Morimoto 		found = 1;
324121a03528SKuninori Morimoto 		break;
3242d191bd8dSKuninori Morimoto 	}
3243d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
3244d191bd8dSKuninori Morimoto 
324521a03528SKuninori Morimoto 	if (found) {
3246cf9e829eSKuninori Morimoto 		snd_soc_component_cleanup(component);
3247d191bd8dSKuninori Morimoto 	}
32482eccea8cSKuninori Morimoto 
32492eccea8cSKuninori Morimoto 	return found;
32502eccea8cSKuninori Morimoto }
32512eccea8cSKuninori Morimoto 
32522eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
32532eccea8cSKuninori Morimoto {
32542eccea8cSKuninori Morimoto 	while (__snd_soc_unregister_component(dev));
3255d191bd8dSKuninori Morimoto }
3256d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3257d191bd8dSKuninori Morimoto 
32587dd5d0d9SKuninori Morimoto struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
32597dd5d0d9SKuninori Morimoto 						   const char *driver_name)
32607dd5d0d9SKuninori Morimoto {
32617dd5d0d9SKuninori Morimoto 	struct snd_soc_component *component;
32627dd5d0d9SKuninori Morimoto 	struct snd_soc_component *ret;
32637dd5d0d9SKuninori Morimoto 
32647dd5d0d9SKuninori Morimoto 	ret = NULL;
32657dd5d0d9SKuninori Morimoto 	mutex_lock(&client_mutex);
32667dd5d0d9SKuninori Morimoto 	list_for_each_entry(component, &component_list, list) {
32677dd5d0d9SKuninori Morimoto 		if (dev != component->dev)
32687dd5d0d9SKuninori Morimoto 			continue;
32697dd5d0d9SKuninori Morimoto 
32707dd5d0d9SKuninori Morimoto 		if (driver_name &&
32717dd5d0d9SKuninori Morimoto 		    (driver_name != component->driver->name) &&
32727dd5d0d9SKuninori Morimoto 		    (strcmp(component->driver->name, driver_name) != 0))
32737dd5d0d9SKuninori Morimoto 			continue;
32747dd5d0d9SKuninori Morimoto 
32757dd5d0d9SKuninori Morimoto 		ret = component;
32767dd5d0d9SKuninori Morimoto 		break;
32777dd5d0d9SKuninori Morimoto 	}
32787dd5d0d9SKuninori Morimoto 	mutex_unlock(&client_mutex);
32797dd5d0d9SKuninori Morimoto 
32807dd5d0d9SKuninori Morimoto 	return ret;
32817dd5d0d9SKuninori Morimoto }
32827dd5d0d9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
32837dd5d0d9SKuninori Morimoto 
3284bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
3285b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3286bec4fa05SStephen Warren 			       const char *propname)
3287bec4fa05SStephen Warren {
3288b07609ceSKuninori Morimoto 	struct device_node *np;
3289bec4fa05SStephen Warren 	int ret;
3290bec4fa05SStephen Warren 
32917e07e7c0STushar Behera 	if (!card->dev) {
32927e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
32937e07e7c0STushar Behera 		return -EINVAL;
32947e07e7c0STushar Behera 	}
32957e07e7c0STushar Behera 
32967e07e7c0STushar Behera 	np = card->dev->of_node;
32977e07e7c0STushar Behera 
3298bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
3299bec4fa05SStephen Warren 	/*
3300bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
3301bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
3302bec4fa05SStephen Warren 	 * snd_soc_register_card.
3303bec4fa05SStephen Warren 	 */
3304bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
3305bec4fa05SStephen Warren 		dev_err(card->dev,
3306f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
3307bec4fa05SStephen Warren 			propname, ret);
3308bec4fa05SStephen Warren 		return ret;
3309bec4fa05SStephen Warren 	}
3310bec4fa05SStephen Warren 
3311bec4fa05SStephen Warren 	return 0;
3312bec4fa05SStephen Warren }
3313b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3314bec4fa05SStephen Warren 
33159a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
33169a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
33179a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
33189a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
33199a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
33209a6d4860SXiubo Li };
33219a6d4860SXiubo Li 
332221efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
33239a6d4860SXiubo Li 					  const char *propname)
33249a6d4860SXiubo Li {
332521efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
33269a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
33279a6d4860SXiubo Li 	const char *template, *wname;
33289a6d4860SXiubo Li 	int i, j, num_widgets, ret;
33299a6d4860SXiubo Li 
33309a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
33319a6d4860SXiubo Li 	if (num_widgets < 0) {
33329a6d4860SXiubo Li 		dev_err(card->dev,
33339a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
33349a6d4860SXiubo Li 		return -EINVAL;
33359a6d4860SXiubo Li 	}
33369a6d4860SXiubo Li 	if (num_widgets & 1) {
33379a6d4860SXiubo Li 		dev_err(card->dev,
33389a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
33399a6d4860SXiubo Li 		return -EINVAL;
33409a6d4860SXiubo Li 	}
33419a6d4860SXiubo Li 
33429a6d4860SXiubo Li 	num_widgets /= 2;
33439a6d4860SXiubo Li 	if (!num_widgets) {
33449a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
33459a6d4860SXiubo Li 			propname);
33469a6d4860SXiubo Li 		return -EINVAL;
33479a6d4860SXiubo Li 	}
33489a6d4860SXiubo Li 
33499a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
33509a6d4860SXiubo Li 			       GFP_KERNEL);
33519a6d4860SXiubo Li 	if (!widgets) {
33529a6d4860SXiubo Li 		dev_err(card->dev,
33539a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
33549a6d4860SXiubo Li 		return -ENOMEM;
33559a6d4860SXiubo Li 	}
33569a6d4860SXiubo Li 
33579a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
33589a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
33599a6d4860SXiubo Li 			2 * i, &template);
33609a6d4860SXiubo Li 		if (ret) {
33619a6d4860SXiubo Li 			dev_err(card->dev,
33629a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
33639a6d4860SXiubo Li 				propname, 2 * i, ret);
33649a6d4860SXiubo Li 			return -EINVAL;
33659a6d4860SXiubo Li 		}
33669a6d4860SXiubo Li 
33679a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
33689a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
33699a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
33709a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
33719a6d4860SXiubo Li 				break;
33729a6d4860SXiubo Li 			}
33739a6d4860SXiubo Li 		}
33749a6d4860SXiubo Li 
33759a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
33769a6d4860SXiubo Li 			dev_err(card->dev,
33779a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
33789a6d4860SXiubo Li 				template);
33799a6d4860SXiubo Li 			return -EINVAL;
33809a6d4860SXiubo Li 		}
33819a6d4860SXiubo Li 
33829a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
33839a6d4860SXiubo Li 						    (2 * i) + 1,
33849a6d4860SXiubo Li 						    &wname);
33859a6d4860SXiubo Li 		if (ret) {
33869a6d4860SXiubo Li 			dev_err(card->dev,
33879a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
33889a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
33899a6d4860SXiubo Li 			return -EINVAL;
33909a6d4860SXiubo Li 		}
33919a6d4860SXiubo Li 
33929a6d4860SXiubo Li 		widgets[i].name = wname;
33939a6d4860SXiubo Li 	}
33949a6d4860SXiubo Li 
3395f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
3396f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
33979a6d4860SXiubo Li 
33989a6d4860SXiubo Li 	return 0;
33999a6d4860SXiubo Li }
340021efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
34019a6d4860SXiubo Li 
3402cbdfab3bSJerome Brunet int snd_soc_of_get_slot_mask(struct device_node *np,
34036131084aSJyri Sarha 			     const char *prop_name,
34046131084aSJyri Sarha 			     unsigned int *mask)
34056131084aSJyri Sarha {
34066131084aSJyri Sarha 	u32 val;
34076c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
34086131084aSJyri Sarha 	int i;
34096131084aSJyri Sarha 
34106131084aSJyri Sarha 	if (!of_slot_mask)
34116131084aSJyri Sarha 		return 0;
34126131084aSJyri Sarha 	val /= sizeof(u32);
34136131084aSJyri Sarha 	for (i = 0; i < val; i++)
34146131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
34156131084aSJyri Sarha 			*mask |= (1 << i);
34166131084aSJyri Sarha 
34176131084aSJyri Sarha 	return val;
34186131084aSJyri Sarha }
3419cbdfab3bSJerome Brunet EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
34206131084aSJyri Sarha 
342189c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
34226131084aSJyri Sarha 			      unsigned int *tx_mask,
34236131084aSJyri Sarha 			      unsigned int *rx_mask,
342489c67857SXiubo Li 			      unsigned int *slots,
342589c67857SXiubo Li 			      unsigned int *slot_width)
342689c67857SXiubo Li {
342789c67857SXiubo Li 	u32 val;
342889c67857SXiubo Li 	int ret;
342989c67857SXiubo Li 
34306131084aSJyri Sarha 	if (tx_mask)
34316131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
34326131084aSJyri Sarha 	if (rx_mask)
34336131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
34346131084aSJyri Sarha 
343589c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
343689c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
343789c67857SXiubo Li 		if (ret)
343889c67857SXiubo Li 			return ret;
343989c67857SXiubo Li 
344089c67857SXiubo Li 		if (slots)
344189c67857SXiubo Li 			*slots = val;
344289c67857SXiubo Li 	}
344389c67857SXiubo Li 
344489c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
344589c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
344689c67857SXiubo Li 		if (ret)
344789c67857SXiubo Li 			return ret;
344889c67857SXiubo Li 
344989c67857SXiubo Li 		if (slot_width)
345089c67857SXiubo Li 			*slot_width = val;
345189c67857SXiubo Li 	}
345289c67857SXiubo Li 
345389c67857SXiubo Li 	return 0;
345489c67857SXiubo Li }
345589c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
345689c67857SXiubo Li 
3457440a3006SKuninori Morimoto void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
34585e3cdaa2SKuninori Morimoto 				   struct snd_soc_codec_conf *codec_conf,
34595e3cdaa2SKuninori Morimoto 				   struct device_node *of_node,
34605e3cdaa2SKuninori Morimoto 				   const char *propname)
34615e3cdaa2SKuninori Morimoto {
3462440a3006SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
34635e3cdaa2SKuninori Morimoto 	const char *str;
34645e3cdaa2SKuninori Morimoto 	int ret;
34655e3cdaa2SKuninori Morimoto 
34665e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
34675e3cdaa2SKuninori Morimoto 	if (ret < 0) {
34685e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
34695e3cdaa2SKuninori Morimoto 		return;
34705e3cdaa2SKuninori Morimoto 	}
34715e3cdaa2SKuninori Morimoto 
34725e3cdaa2SKuninori Morimoto 	codec_conf->of_node	= of_node;
34735e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
34745e3cdaa2SKuninori Morimoto }
3475440a3006SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
34765e3cdaa2SKuninori Morimoto 
34772bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3478a4a54dd5SStephen Warren 				   const char *propname)
3479a4a54dd5SStephen Warren {
34802bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
3481e3b1e6a1SMark Brown 	int num_routes;
3482a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3483a4a54dd5SStephen Warren 	int i, ret;
3484a4a54dd5SStephen Warren 
3485a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3486c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
348710e8aa9aSMichał Mirosław 		dev_err(card->dev,
348810e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
348910e8aa9aSMichał Mirosław 			propname);
3490a4a54dd5SStephen Warren 		return -EINVAL;
3491a4a54dd5SStephen Warren 	}
3492a4a54dd5SStephen Warren 	num_routes /= 2;
3493a4a54dd5SStephen Warren 	if (!num_routes) {
3494f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3495a4a54dd5SStephen Warren 			propname);
3496a4a54dd5SStephen Warren 		return -EINVAL;
3497a4a54dd5SStephen Warren 	}
3498a4a54dd5SStephen Warren 
3499a86854d0SKees Cook 	routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
3500a4a54dd5SStephen Warren 			      GFP_KERNEL);
3501a4a54dd5SStephen Warren 	if (!routes) {
3502a4a54dd5SStephen Warren 		dev_err(card->dev,
3503f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3504a4a54dd5SStephen Warren 		return -EINVAL;
3505a4a54dd5SStephen Warren 	}
3506a4a54dd5SStephen Warren 
3507a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3508a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3509e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
3510a4a54dd5SStephen Warren 		if (ret) {
3511c871bd0bSMark Brown 			dev_err(card->dev,
3512c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3513c871bd0bSMark Brown 				propname, 2 * i, ret);
3514a4a54dd5SStephen Warren 			return -EINVAL;
3515a4a54dd5SStephen Warren 		}
3516a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3517e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3518a4a54dd5SStephen Warren 		if (ret) {
3519a4a54dd5SStephen Warren 			dev_err(card->dev,
3520c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3521c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3522a4a54dd5SStephen Warren 			return -EINVAL;
3523a4a54dd5SStephen Warren 		}
3524a4a54dd5SStephen Warren 	}
3525a4a54dd5SStephen Warren 
3526f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
3527f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
3528a4a54dd5SStephen Warren 
3529a4a54dd5SStephen Warren 	return 0;
3530a4a54dd5SStephen Warren }
35312bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3532a4a54dd5SStephen Warren 
3533a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3534389cb834SJyri Sarha 				     const char *prefix,
3535389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3536389cb834SJyri Sarha 				     struct device_node **framemaster)
3537a7930ed4SKuninori Morimoto {
3538a7930ed4SKuninori Morimoto 	int ret, i;
3539a7930ed4SKuninori Morimoto 	char prop[128];
3540a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3541a7930ed4SKuninori Morimoto 	int bit, frame;
3542a7930ed4SKuninori Morimoto 	const char *str;
3543a7930ed4SKuninori Morimoto 	struct {
3544a7930ed4SKuninori Morimoto 		char *name;
3545a7930ed4SKuninori Morimoto 		unsigned int val;
3546a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3547a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3548a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3549a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3550a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3551a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3552a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3553a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3554a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3555a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3556a7930ed4SKuninori Morimoto 	};
3557a7930ed4SKuninori Morimoto 
3558a7930ed4SKuninori Morimoto 	if (!prefix)
3559a7930ed4SKuninori Morimoto 		prefix = "";
3560a7930ed4SKuninori Morimoto 
3561a7930ed4SKuninori Morimoto 	/*
35625711c979SKuninori Morimoto 	 * check "dai-format = xxx"
35635711c979SKuninori Morimoto 	 * or    "[prefix]format = xxx"
3564a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3565a7930ed4SKuninori Morimoto 	 */
35665711c979SKuninori Morimoto 	ret = of_property_read_string(np, "dai-format", &str);
35675711c979SKuninori Morimoto 	if (ret < 0) {
3568a7930ed4SKuninori Morimoto 		snprintf(prop, sizeof(prop), "%sformat", prefix);
3569a7930ed4SKuninori Morimoto 		ret = of_property_read_string(np, prop, &str);
35705711c979SKuninori Morimoto 	}
3571a7930ed4SKuninori Morimoto 	if (ret == 0) {
3572a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3573a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3574a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3575a7930ed4SKuninori Morimoto 				break;
3576a7930ed4SKuninori Morimoto 			}
3577a7930ed4SKuninori Morimoto 		}
3578a7930ed4SKuninori Morimoto 	}
3579a7930ed4SKuninori Morimoto 
3580a7930ed4SKuninori Morimoto 	/*
35818c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3582a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3583a7930ed4SKuninori Morimoto 	 */
35848c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
358551930295SJulia Lawall 	if (of_property_read_bool(np, prop))
35868c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
35878c2d6a9fSKuninori Morimoto 	else
35888c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3589a7930ed4SKuninori Morimoto 
3590a7930ed4SKuninori Morimoto 	/*
3591a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3592a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3593a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3594a7930ed4SKuninori Morimoto 	 */
3595a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3596a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3597a7930ed4SKuninori Morimoto 
3598a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3599a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3600a7930ed4SKuninori Morimoto 
3601a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3602a7930ed4SKuninori Morimoto 	case 0x11:
3603a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3604a7930ed4SKuninori Morimoto 		break;
3605a7930ed4SKuninori Morimoto 	case 0x10:
3606a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3607a7930ed4SKuninori Morimoto 		break;
3608a7930ed4SKuninori Morimoto 	case 0x01:
3609a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3610a7930ed4SKuninori Morimoto 		break;
3611a7930ed4SKuninori Morimoto 	default:
3612a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3613a7930ed4SKuninori Morimoto 		break;
3614a7930ed4SKuninori Morimoto 	}
3615a7930ed4SKuninori Morimoto 
3616a7930ed4SKuninori Morimoto 	/*
3617a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3618a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3619a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3620a7930ed4SKuninori Morimoto 	 */
3621a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3622a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3623389cb834SJyri Sarha 	if (bit && bitclkmaster)
3624389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3625a7930ed4SKuninori Morimoto 
3626a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3627a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3628389cb834SJyri Sarha 	if (frame && framemaster)
3629389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3630a7930ed4SKuninori Morimoto 
3631a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3632a7930ed4SKuninori Morimoto 	case 0x11:
3633a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3634a7930ed4SKuninori Morimoto 		break;
3635a7930ed4SKuninori Morimoto 	case 0x10:
3636a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3637a7930ed4SKuninori Morimoto 		break;
3638a7930ed4SKuninori Morimoto 	case 0x01:
3639a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3640a7930ed4SKuninori Morimoto 		break;
3641a7930ed4SKuninori Morimoto 	default:
3642a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3643a7930ed4SKuninori Morimoto 		break;
3644a7930ed4SKuninori Morimoto 	}
3645a7930ed4SKuninori Morimoto 
3646a7930ed4SKuninori Morimoto 	return format;
3647a7930ed4SKuninori Morimoto }
3648a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3649a7930ed4SKuninori Morimoto 
3650a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
3651a180e8b9SKuninori Morimoto {
3652a180e8b9SKuninori Morimoto 	struct snd_soc_component *pos;
3653a180e8b9SKuninori Morimoto 	struct device_node *node;
3654a180e8b9SKuninori Morimoto 	int ret;
3655a180e8b9SKuninori Morimoto 
3656a180e8b9SKuninori Morimoto 	node = of_graph_get_port_parent(ep);
3657a180e8b9SKuninori Morimoto 
3658a180e8b9SKuninori Morimoto 	/*
3659a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
3660a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
3661a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
3662a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
3663a180e8b9SKuninori Morimoto 	 */
3664a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
3665a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
3666a180e8b9SKuninori Morimoto 	list_for_each_entry(pos, &component_list, list) {
3667a180e8b9SKuninori Morimoto 		struct device_node *component_of_node = pos->dev->of_node;
3668a180e8b9SKuninori Morimoto 
3669a180e8b9SKuninori Morimoto 		if (!component_of_node && pos->dev->parent)
3670a180e8b9SKuninori Morimoto 			component_of_node = pos->dev->parent->of_node;
3671a180e8b9SKuninori Morimoto 
3672a180e8b9SKuninori Morimoto 		if (component_of_node != node)
3673a180e8b9SKuninori Morimoto 			continue;
3674a180e8b9SKuninori Morimoto 
3675a180e8b9SKuninori Morimoto 		if (pos->driver->of_xlate_dai_id)
3676a180e8b9SKuninori Morimoto 			ret = pos->driver->of_xlate_dai_id(pos, ep);
3677a180e8b9SKuninori Morimoto 
3678a180e8b9SKuninori Morimoto 		break;
3679a180e8b9SKuninori Morimoto 	}
3680a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
3681a180e8b9SKuninori Morimoto 
3682c0a480d1STony Lindgren 	of_node_put(node);
3683c0a480d1STony Lindgren 
3684a180e8b9SKuninori Morimoto 	return ret;
3685a180e8b9SKuninori Morimoto }
3686a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3687a180e8b9SKuninori Morimoto 
36881ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args,
3689cb470087SKuninori Morimoto 				const char **dai_name)
3690cb470087SKuninori Morimoto {
3691cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
36923e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
369393b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3694cb470087SKuninori Morimoto 
3695cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3696cb470087SKuninori Morimoto 	list_for_each_entry(pos, &component_list, list) {
36973e0aa8d8SJyri Sarha 		component_of_node = pos->dev->of_node;
36983e0aa8d8SJyri Sarha 		if (!component_of_node && pos->dev->parent)
36993e0aa8d8SJyri Sarha 			component_of_node = pos->dev->parent->of_node;
37003e0aa8d8SJyri Sarha 
37013e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
3702cb470087SKuninori Morimoto 			continue;
3703cb470087SKuninori Morimoto 
37046833c452SKuninori Morimoto 		if (pos->driver->of_xlate_dai_name) {
370593b0f3eeSJean-Francois Moine 			ret = pos->driver->of_xlate_dai_name(pos,
370693b0f3eeSJean-Francois Moine 							     args,
370793b0f3eeSJean-Francois Moine 							     dai_name);
37086833c452SKuninori Morimoto 		} else {
370958bf4179SKuninori Morimoto 			struct snd_soc_dai *dai;
37106833c452SKuninori Morimoto 			int id = -1;
37116833c452SKuninori Morimoto 
371293b0f3eeSJean-Francois Moine 			switch (args->args_count) {
37136833c452SKuninori Morimoto 			case 0:
37146833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
37156833c452SKuninori Morimoto 				break;
37166833c452SKuninori Morimoto 			case 1:
371793b0f3eeSJean-Francois Moine 				id = args->args[0];
37186833c452SKuninori Morimoto 				break;
37196833c452SKuninori Morimoto 			default:
37206833c452SKuninori Morimoto 				/* not supported */
3721cb470087SKuninori Morimoto 				break;
3722cb470087SKuninori Morimoto 			}
3723cb470087SKuninori Morimoto 
37246833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
37256833c452SKuninori Morimoto 				ret = -EINVAL;
37263dcba280SNicolin Chen 				continue;
37276833c452SKuninori Morimoto 			}
3728e41975edSXiubo Li 
3729e41975edSXiubo Li 			ret = 0;
3730e41975edSXiubo Li 
373158bf4179SKuninori Morimoto 			/* find target DAI */
373258bf4179SKuninori Morimoto 			list_for_each_entry(dai, &pos->dai_list, list) {
373358bf4179SKuninori Morimoto 				if (id == 0)
373458bf4179SKuninori Morimoto 					break;
373558bf4179SKuninori Morimoto 				id--;
373658bf4179SKuninori Morimoto 			}
373758bf4179SKuninori Morimoto 
373858bf4179SKuninori Morimoto 			*dai_name = dai->driver->name;
3739e41975edSXiubo Li 			if (!*dai_name)
3740e41975edSXiubo Li 				*dai_name = pos->name;
37416833c452SKuninori Morimoto 		}
37426833c452SKuninori Morimoto 
3743cb470087SKuninori Morimoto 		break;
3744cb470087SKuninori Morimoto 	}
3745cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
374693b0f3eeSJean-Francois Moine 	return ret;
374793b0f3eeSJean-Francois Moine }
37481ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
374993b0f3eeSJean-Francois Moine 
375093b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
375193b0f3eeSJean-Francois Moine 			    const char **dai_name)
375293b0f3eeSJean-Francois Moine {
375393b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
375493b0f3eeSJean-Francois Moine 	int ret;
375593b0f3eeSJean-Francois Moine 
375693b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
375793b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
375893b0f3eeSJean-Francois Moine 	if (ret)
375993b0f3eeSJean-Francois Moine 		return ret;
376093b0f3eeSJean-Francois Moine 
376193b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3762cb470087SKuninori Morimoto 
3763cb470087SKuninori Morimoto 	of_node_put(args.np);
3764cb470087SKuninori Morimoto 
3765cb470087SKuninori Morimoto 	return ret;
3766cb470087SKuninori Morimoto }
3767cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3768cb470087SKuninori Morimoto 
376993b0f3eeSJean-Francois Moine /*
377094685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
377194685763SSylwester Nawrocki  * @dai_link: DAI link
377294685763SSylwester Nawrocki  *
377394685763SSylwester Nawrocki  * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
377494685763SSylwester Nawrocki  */
377594685763SSylwester Nawrocki void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
377694685763SSylwester Nawrocki {
37773db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *component;
377894685763SSylwester Nawrocki 	int index;
377994685763SSylwester Nawrocki 
37803db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
378194685763SSylwester Nawrocki 		if (!component->of_node)
378294685763SSylwester Nawrocki 			break;
378394685763SSylwester Nawrocki 		of_node_put(component->of_node);
378494685763SSylwester Nawrocki 		component->of_node = NULL;
378594685763SSylwester Nawrocki 	}
378694685763SSylwester Nawrocki }
378794685763SSylwester Nawrocki EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
378894685763SSylwester Nawrocki 
378994685763SSylwester Nawrocki /*
379093b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
379193b0f3eeSJean-Francois Moine  * @dev: Card device
379293b0f3eeSJean-Francois Moine  * @of_node: Device node
379393b0f3eeSJean-Francois Moine  * @dai_link: DAI link
379493b0f3eeSJean-Francois Moine  *
379593b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
379693b0f3eeSJean-Francois Moine  * 'sound-dai'.
379793b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
379894685763SSylwester Nawrocki  * The device nodes in the array (of_node) must be dereferenced by calling
379994685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs() on @dai_link.
380093b0f3eeSJean-Francois Moine  *
380193b0f3eeSJean-Francois Moine  * Returns 0 for success
380293b0f3eeSJean-Francois Moine  */
380393b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
380493b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
380593b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
380693b0f3eeSJean-Francois Moine {
380793b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
380893b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
380993b0f3eeSJean-Francois Moine 	char *name;
381093b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
381193b0f3eeSJean-Francois Moine 
381293b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
381393b0f3eeSJean-Francois Moine 	name = "sound-dai";
381493b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
381593b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
381693b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
381793b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
381893b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
381993b0f3eeSJean-Francois Moine 		else
382093b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
382193b0f3eeSJean-Francois Moine 		return num_codecs;
382293b0f3eeSJean-Francois Moine 	}
3823a86854d0SKees Cook 	component = devm_kcalloc(dev,
3824a86854d0SKees Cook 				 num_codecs, sizeof(*component),
382593b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
382693b0f3eeSJean-Francois Moine 	if (!component)
382793b0f3eeSJean-Francois Moine 		return -ENOMEM;
382893b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
382993b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
383093b0f3eeSJean-Francois Moine 
383193b0f3eeSJean-Francois Moine 	/* Parse the list */
38323db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
383393b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
383493b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
383593b0f3eeSJean-Francois Moine 						  index, &args);
383693b0f3eeSJean-Francois Moine 		if (ret)
383793b0f3eeSJean-Francois Moine 			goto err;
383893b0f3eeSJean-Francois Moine 		component->of_node = args.np;
383993b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
384093b0f3eeSJean-Francois Moine 		if (ret < 0)
384193b0f3eeSJean-Francois Moine 			goto err;
384293b0f3eeSJean-Francois Moine 	}
384393b0f3eeSJean-Francois Moine 	return 0;
384493b0f3eeSJean-Francois Moine err:
384594685763SSylwester Nawrocki 	snd_soc_of_put_dai_link_codecs(dai_link);
384693b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
384793b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
384893b0f3eeSJean-Francois Moine 	return ret;
384993b0f3eeSJean-Francois Moine }
385093b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
385193b0f3eeSJean-Francois Moine 
3852c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3853db2a4165SFrank Mandarino {
38546553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
3855fb257897SMark Brown 	snd_soc_util_init();
3856fb257897SMark Brown 
3857db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3858db2a4165SFrank Mandarino }
38594abe8e16SMark Brown module_init(snd_soc_init);
3860db2a4165SFrank Mandarino 
38617d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3862db2a4165SFrank Mandarino {
3863fb257897SMark Brown 	snd_soc_util_exit();
38646553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3865fb257897SMark Brown 
3866db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3867db2a4165SFrank Mandarino }
3868db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3869db2a4165SFrank Mandarino 
3870db2a4165SFrank Mandarino /* Module information */
3871d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3872db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3873db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
3874db2a4165SFrank Mandarino MODULE_ALIAS("platform:soc-audio");
3875