xref: /openbmc/linux/sound/soc/soc-core.c (revision ffd60fba19d9752f553aac367cd40362011ab6c9)
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 
57368dee94SKuninori Morimoto #define for_each_component(component)			\
58368dee94SKuninori Morimoto 	list_for_each_entry(component, &component_list, list)
59368dee94SKuninori Morimoto 
60db2a4165SFrank Mandarino /*
61587c9844SKuninori Morimoto  * This is used if driver don't need to have CPU/Codec/Platform
62587c9844SKuninori Morimoto  * dai_link. see soc.h
63587c9844SKuninori Morimoto  */
64587c9844SKuninori Morimoto struct snd_soc_dai_link_component null_dailink_component[0];
65587c9844SKuninori Morimoto EXPORT_SYMBOL_GPL(null_dailink_component);
66587c9844SKuninori Morimoto 
67587c9844SKuninori Morimoto /*
68db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
69db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
70db2a4165SFrank Mandarino  * between two audio tracks.
71db2a4165SFrank Mandarino  */
72db2a4165SFrank Mandarino static int pmdown_time = 5000;
73db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
74db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
75db2a4165SFrank Mandarino 
760faf1237SYueHaibing #ifdef CONFIG_DMI
772c7b696aSMarcel Ziswiler /*
782c7b696aSMarcel Ziswiler  * If a DMI filed contain strings in this blacklist (e.g.
7998faf436SMengdong Lin  * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
8098faf436SMengdong Lin  * as invalid and dropped when setting the card long name from DMI info.
8198faf436SMengdong Lin  */
8298faf436SMengdong Lin static const char * const dmi_blacklist[] = {
8398faf436SMengdong Lin 	"To be filled by OEM",
8498faf436SMengdong Lin 	"TBD by OEM",
8598faf436SMengdong Lin 	"Default String",
8698faf436SMengdong Lin 	"Board Manufacturer",
8798faf436SMengdong Lin 	"Board Vendor Name",
8898faf436SMengdong Lin 	"Board Product Name",
8998faf436SMengdong Lin 	NULL,	/* terminator */
9098faf436SMengdong Lin };
910faf1237SYueHaibing #endif
9298faf436SMengdong Lin 
93dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
94dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
95dbe21408SMark Brown {
9636ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
97dbe21408SMark Brown 
98f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
99dbe21408SMark Brown }
100dbe21408SMark Brown 
101dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
102dbe21408SMark Brown 			       struct device_attribute *attr,
103dbe21408SMark Brown 			       const char *buf, size_t count)
104dbe21408SMark Brown {
10536ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
106c593b520SMark Brown 	int ret;
107dbe21408SMark Brown 
108b785a492SJingoo Han 	ret = kstrtol(buf, 10, &rtd->pmdown_time);
109c593b520SMark Brown 	if (ret)
110c593b520SMark Brown 		return ret;
111dbe21408SMark Brown 
112dbe21408SMark Brown 	return count;
113dbe21408SMark Brown }
114dbe21408SMark Brown 
115dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
116dbe21408SMark Brown 
117d29697dcSTakashi Iwai static struct attribute *soc_dev_attrs[] = {
118d29697dcSTakashi Iwai 	&dev_attr_pmdown_time.attr,
119d29697dcSTakashi Iwai 	NULL
120d29697dcSTakashi Iwai };
121d29697dcSTakashi Iwai 
122d29697dcSTakashi Iwai static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
123d29697dcSTakashi Iwai 				       struct attribute *attr, int idx)
124d29697dcSTakashi Iwai {
125d29697dcSTakashi Iwai 	struct device *dev = kobj_to_dev(kobj);
126d29697dcSTakashi Iwai 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
127d29697dcSTakashi Iwai 
128d29697dcSTakashi Iwai 	if (attr == &dev_attr_pmdown_time.attr)
129d29697dcSTakashi Iwai 		return attr->mode; /* always visible */
1303b6eed8dSKuninori Morimoto 	return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
131d29697dcSTakashi Iwai }
132d29697dcSTakashi Iwai 
133d29697dcSTakashi Iwai static const struct attribute_group soc_dapm_dev_group = {
134d29697dcSTakashi Iwai 	.attrs = soc_dapm_dev_attrs,
135d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
136d29697dcSTakashi Iwai };
137d29697dcSTakashi Iwai 
138f7e73b26SMark Brown static const struct attribute_group soc_dev_group = {
139d29697dcSTakashi Iwai 	.attrs = soc_dev_attrs,
140d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
141d29697dcSTakashi Iwai };
142d29697dcSTakashi Iwai 
143d29697dcSTakashi Iwai static const struct attribute_group *soc_dev_attr_groups[] = {
144d29697dcSTakashi Iwai 	&soc_dapm_dev_group,
145f7e73b26SMark Brown 	&soc_dev_group,
146d29697dcSTakashi Iwai 	NULL
147d29697dcSTakashi Iwai };
148d29697dcSTakashi Iwai 
1492624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
15081c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component)
151e73f3de5SRussell King {
1526553bf06SLars-Peter Clausen 	if (!component->card->debugfs_card_root)
1536553bf06SLars-Peter Clausen 		return;
1546553bf06SLars-Peter Clausen 
15581c7cfd1SLars-Peter Clausen 	if (component->debugfs_prefix) {
15681c7cfd1SLars-Peter Clausen 		char *name;
157e73f3de5SRussell King 
15881c7cfd1SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s:%s",
15981c7cfd1SLars-Peter Clausen 			component->debugfs_prefix, component->name);
16081c7cfd1SLars-Peter Clausen 		if (name) {
16181c7cfd1SLars-Peter Clausen 			component->debugfs_root = debugfs_create_dir(name,
16281c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
16381c7cfd1SLars-Peter Clausen 			kfree(name);
16481c7cfd1SLars-Peter Clausen 		}
16581c7cfd1SLars-Peter Clausen 	} else {
16681c7cfd1SLars-Peter Clausen 		component->debugfs_root = debugfs_create_dir(component->name,
16781c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
168e73f3de5SRussell King 	}
169e73f3de5SRussell King 
17081c7cfd1SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
17181c7cfd1SLars-Peter Clausen 		component->debugfs_root);
17281c7cfd1SLars-Peter Clausen }
17381c7cfd1SLars-Peter Clausen 
17481c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
17581c7cfd1SLars-Peter Clausen {
176ad64bfbdSKuninori Morimoto 	if (!component->debugfs_root)
177ad64bfbdSKuninori Morimoto 		return;
17881c7cfd1SLars-Peter Clausen 	debugfs_remove_recursive(component->debugfs_root);
179ad64bfbdSKuninori Morimoto 	component->debugfs_root = NULL;
18081c7cfd1SLars-Peter Clausen }
18181c7cfd1SLars-Peter Clausen 
182c15b2a1dSPeng Donglin static int dai_list_show(struct seq_file *m, void *v)
183f3208780SMark Brown {
1841438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
185f3208780SMark Brown 	struct snd_soc_dai *dai;
186f3208780SMark Brown 
18734e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
18834e81ab4SLars-Peter Clausen 
189368dee94SKuninori Morimoto 	for_each_component(component)
19015a0c645SKuninori Morimoto 		for_each_component_dais(component, dai)
191700c17caSDonglin Peng 			seq_printf(m, "%s\n", dai->name);
192f3208780SMark Brown 
19334e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
19434e81ab4SLars-Peter Clausen 
195700c17caSDonglin Peng 	return 0;
196700c17caSDonglin Peng }
197c15b2a1dSPeng Donglin DEFINE_SHOW_ATTRIBUTE(dai_list);
198f3208780SMark Brown 
199db795f9bSKuninori Morimoto static int component_list_show(struct seq_file *m, void *v)
200db795f9bSKuninori Morimoto {
201db795f9bSKuninori Morimoto 	struct snd_soc_component *component;
202db795f9bSKuninori Morimoto 
203db795f9bSKuninori Morimoto 	mutex_lock(&client_mutex);
204db795f9bSKuninori Morimoto 
205368dee94SKuninori Morimoto 	for_each_component(component)
206db795f9bSKuninori Morimoto 		seq_printf(m, "%s\n", component->name);
207db795f9bSKuninori Morimoto 
208db795f9bSKuninori Morimoto 	mutex_unlock(&client_mutex);
209db795f9bSKuninori Morimoto 
210db795f9bSKuninori Morimoto 	return 0;
211db795f9bSKuninori Morimoto }
212db795f9bSKuninori Morimoto DEFINE_SHOW_ATTRIBUTE(component_list);
213db795f9bSKuninori Morimoto 
214a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
215a6052154SJarkko Nikula {
216a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
2178a9dab1aSMark Brown 						     snd_soc_debugfs_root);
2183a45b867SJarkko Nikula 
219fee531d6SGreg Kroah-Hartman 	debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root,
2203a45b867SJarkko Nikula 			   &card->pop_time);
221d8ca7a0aSKuninori Morimoto 
222d8ca7a0aSKuninori Morimoto 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
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);
22829040d1aSKuninori Morimoto 	card->debugfs_card_root = NULL;
229a6052154SJarkko Nikula }
230a6052154SJarkko Nikula 
2316553bf06SLars-Peter Clausen static void snd_soc_debugfs_init(void)
2326553bf06SLars-Peter Clausen {
2336553bf06SLars-Peter Clausen 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
2346553bf06SLars-Peter Clausen 
235fee531d6SGreg Kroah-Hartman 	debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
236fee531d6SGreg Kroah-Hartman 			    &dai_list_fops);
237db795f9bSKuninori Morimoto 
238fee531d6SGreg Kroah-Hartman 	debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
239fee531d6SGreg Kroah-Hartman 			    &component_list_fops);
2406553bf06SLars-Peter Clausen }
2416553bf06SLars-Peter Clausen 
2426553bf06SLars-Peter Clausen static void snd_soc_debugfs_exit(void)
2436553bf06SLars-Peter Clausen {
2446553bf06SLars-Peter Clausen 	debugfs_remove_recursive(snd_soc_debugfs_root);
2456553bf06SLars-Peter Clausen }
2466553bf06SLars-Peter Clausen 
2472624d5faSMark Brown #else
2482624d5faSMark Brown 
24981c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs(
25081c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
2512624d5faSMark Brown {
2522624d5faSMark Brown }
2532624d5faSMark Brown 
25481c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs(
25581c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
256731f1ab2SSebastien Guiriec {
257731f1ab2SSebastien Guiriec }
258731f1ab2SSebastien Guiriec 
259b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
260b95fccbcSAxel Lin {
261b95fccbcSAxel Lin }
262b95fccbcSAxel Lin 
263b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
264b95fccbcSAxel Lin {
265b95fccbcSAxel Lin }
2666553bf06SLars-Peter Clausen 
2676553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_init(void)
2686553bf06SLars-Peter Clausen {
2696553bf06SLars-Peter Clausen }
2706553bf06SLars-Peter Clausen 
2716553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_exit(void)
2726553bf06SLars-Peter Clausen {
2736553bf06SLars-Peter Clausen }
2746553bf06SLars-Peter Clausen 
2752624d5faSMark Brown #endif
2762624d5faSMark Brown 
277a0ac4411SKuninori Morimoto static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
278a0ac4411SKuninori Morimoto 			      struct snd_soc_component *component)
279a0ac4411SKuninori Morimoto {
280a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
281a0ac4411SKuninori Morimoto 
282a0ac4411SKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
283a0ac4411SKuninori Morimoto 		/* already connected */
284a0ac4411SKuninori Morimoto 		if (rtdcom->component == component)
285a0ac4411SKuninori Morimoto 			return 0;
286a0ac4411SKuninori Morimoto 	}
287a0ac4411SKuninori Morimoto 
28832d2c172SKuninori Morimoto 	rtdcom = kmalloc(sizeof(*rtdcom), GFP_KERNEL);
28932d2c172SKuninori Morimoto 	if (!rtdcom)
290a0ac4411SKuninori Morimoto 		return -ENOMEM;
291a0ac4411SKuninori Morimoto 
29232d2c172SKuninori Morimoto 	rtdcom->component = component;
29332d2c172SKuninori Morimoto 	INIT_LIST_HEAD(&rtdcom->list);
294a0ac4411SKuninori Morimoto 
29532d2c172SKuninori Morimoto 	list_add_tail(&rtdcom->list, &rtd->component_list);
296a0ac4411SKuninori Morimoto 
297a0ac4411SKuninori Morimoto 	return 0;
298a0ac4411SKuninori Morimoto }
299a0ac4411SKuninori Morimoto 
300a0ac4411SKuninori Morimoto static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
301a0ac4411SKuninori Morimoto {
302a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
303a0ac4411SKuninori Morimoto 
304a0ac4411SKuninori Morimoto 	for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
305a0ac4411SKuninori Morimoto 		kfree(rtdcom1);
306a0ac4411SKuninori Morimoto 
307a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
308a0ac4411SKuninori Morimoto }
309a0ac4411SKuninori Morimoto 
310a0ac4411SKuninori Morimoto struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
311a0ac4411SKuninori Morimoto 						const char *driver_name)
312a0ac4411SKuninori Morimoto {
313a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
314a0ac4411SKuninori Morimoto 
315971da24cSKuninori Morimoto 	if (!driver_name)
316971da24cSKuninori Morimoto 		return NULL;
317971da24cSKuninori Morimoto 
318a33c0d16SKuninori Morimoto 	/*
319a33c0d16SKuninori Morimoto 	 * NOTE
320a33c0d16SKuninori Morimoto 	 *
321a33c0d16SKuninori Morimoto 	 * snd_soc_rtdcom_lookup() will find component from rtd by using
322a33c0d16SKuninori Morimoto 	 * specified driver name.
323a33c0d16SKuninori Morimoto 	 * But, if many components which have same driver name are connected
324a33c0d16SKuninori Morimoto 	 * to 1 rtd, this function will return 1st found component.
325a33c0d16SKuninori Morimoto 	 */
326a0ac4411SKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
327971da24cSKuninori Morimoto 		const char *component_name = rtdcom->component->driver->name;
328971da24cSKuninori Morimoto 
329971da24cSKuninori Morimoto 		if (!component_name)
330971da24cSKuninori Morimoto 			continue;
331971da24cSKuninori Morimoto 
332971da24cSKuninori Morimoto 		if ((component_name == driver_name) ||
333971da24cSKuninori Morimoto 		    strcmp(component_name, driver_name) == 0)
334a0ac4411SKuninori Morimoto 			return rtdcom->component;
335a0ac4411SKuninori Morimoto 	}
336a0ac4411SKuninori Morimoto 
337a0ac4411SKuninori Morimoto 	return NULL;
338a0ac4411SKuninori Morimoto }
339031734b7SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
340a0ac4411SKuninori Morimoto 
34147c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
34247c88fffSLiam Girdwood 		const char *dai_link, int stream)
34347c88fffSLiam Girdwood {
3441a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
34547c88fffSLiam Girdwood 
346bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
3471a497983SMengdong Lin 		if (rtd->dai_link->no_pcm &&
3481a497983SMengdong Lin 			!strcmp(rtd->dai_link->name, dai_link))
3491a497983SMengdong Lin 			return rtd->pcm->streams[stream].substream;
35047c88fffSLiam Girdwood 	}
351f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
35247c88fffSLiam Girdwood 	return NULL;
35347c88fffSLiam Girdwood }
35447c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
35547c88fffSLiam Girdwood 
35675ab9eb6SKuninori Morimoto static const struct snd_soc_ops null_snd_soc_ops;
35775ab9eb6SKuninori Morimoto 
3581a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
3591a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
3601a497983SMengdong Lin {
3611a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
3621a497983SMengdong Lin 
3631a497983SMengdong Lin 	rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
3641a497983SMengdong Lin 	if (!rtd)
3651a497983SMengdong Lin 		return NULL;
3661a497983SMengdong Lin 
367a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
3681a497983SMengdong Lin 	rtd->card = card;
3691a497983SMengdong Lin 	rtd->dai_link = dai_link;
37075ab9eb6SKuninori Morimoto 	if (!rtd->dai_link->ops)
37175ab9eb6SKuninori Morimoto 		rtd->dai_link->ops = &null_snd_soc_ops;
37275ab9eb6SKuninori Morimoto 
3736396bb22SKees Cook 	rtd->codec_dais = kcalloc(dai_link->num_codecs,
3746396bb22SKees Cook 					sizeof(struct snd_soc_dai *),
3751a497983SMengdong Lin 					GFP_KERNEL);
3761a497983SMengdong Lin 	if (!rtd->codec_dais) {
3771a497983SMengdong Lin 		kfree(rtd);
3781a497983SMengdong Lin 		return NULL;
3791a497983SMengdong Lin 	}
3801a497983SMengdong Lin 
3811a497983SMengdong Lin 	return rtd;
3821a497983SMengdong Lin }
3831a497983SMengdong Lin 
3841a497983SMengdong Lin static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
3851a497983SMengdong Lin {
3861a497983SMengdong Lin 	kfree(rtd->codec_dais);
387a0ac4411SKuninori Morimoto 	snd_soc_rtdcom_del_all(rtd);
3881a497983SMengdong Lin 	kfree(rtd);
3891a497983SMengdong Lin }
3901a497983SMengdong Lin 
3911a497983SMengdong Lin static void soc_add_pcm_runtime(struct snd_soc_card *card,
3921a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd)
3931a497983SMengdong Lin {
3946634e3d6SKuninori Morimoto 	/* see for_each_card_rtds */
3951a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
3961a497983SMengdong Lin 	rtd->num = card->num_rtd;
3971a497983SMengdong Lin 	card->num_rtd++;
3981a497983SMengdong Lin }
3991a497983SMengdong Lin 
4001a497983SMengdong Lin static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
4011a497983SMengdong Lin {
4021a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, *_rtd;
4031a497983SMengdong Lin 
404bcb1fd1fSKuninori Morimoto 	for_each_card_rtds_safe(card, rtd, _rtd) {
4051a497983SMengdong Lin 		list_del(&rtd->list);
4061a497983SMengdong Lin 		soc_free_pcm_runtime(rtd);
4071a497983SMengdong Lin 	}
4081a497983SMengdong Lin 
4091a497983SMengdong Lin 	card->num_rtd = 0;
4101a497983SMengdong Lin }
4111a497983SMengdong Lin 
41247c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
41347c88fffSLiam Girdwood 		const char *dai_link)
41447c88fffSLiam Girdwood {
4151a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
41647c88fffSLiam Girdwood 
417bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4181a497983SMengdong Lin 		if (!strcmp(rtd->dai_link->name, dai_link))
4191a497983SMengdong Lin 			return rtd;
42047c88fffSLiam Girdwood 	}
421f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
42247c88fffSLiam Girdwood 	return NULL;
42347c88fffSLiam Girdwood }
42447c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
42547c88fffSLiam Girdwood 
42665462e44SKuninori Morimoto static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
42765462e44SKuninori Morimoto {
42865462e44SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
42965462e44SKuninori Morimoto 
43065462e44SKuninori Morimoto 	for_each_card_rtds(card, rtd)
43165462e44SKuninori Morimoto 		flush_delayed_work(&rtd->delayed_work);
43265462e44SKuninori Morimoto }
43365462e44SKuninori Morimoto 
4346f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
435db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
4366f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
437db2a4165SFrank Mandarino {
4386f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
439d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
4401a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
4411a497983SMengdong Lin 	int i;
442db2a4165SFrank Mandarino 
443c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
444c5599b87SLars-Peter Clausen 	if (!card->instantiated)
445e3509ff0SDaniel Mack 		return 0;
446e3509ff0SDaniel Mack 
4472c7b696aSMarcel Ziswiler 	/*
4482c7b696aSMarcel Ziswiler 	 * Due to the resume being scheduled into a workqueue we could
4496ed25978SAndy Green 	 * suspend before that's finished - wait for it to complete.
4506ed25978SAndy Green 	 */
451f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
4526ed25978SAndy Green 
4536ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
454f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
4556ed25978SAndy Green 
456a00f90f9SMark Brown 	/* mute any active DACs */
457bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4580b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
4593efab7dcSMark Brown 
4601a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4613efab7dcSMark Brown 			continue;
4623efab7dcSMark Brown 
4630b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
46488fdffa2SKuninori Morimoto 			if (dai->playback_active)
46588fdffa2SKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 1,
46688fdffa2SKuninori Morimoto 						SNDRV_PCM_STREAM_PLAYBACK);
467db2a4165SFrank Mandarino 		}
46888bd870fSBenoit Cousson 	}
469db2a4165SFrank Mandarino 
4704ccab3e7SLiam Girdwood 	/* suspend all pcms */
471bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4721a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4733efab7dcSMark Brown 			continue;
4743efab7dcSMark Brown 
4751a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
4763efab7dcSMark Brown 	}
4774ccab3e7SLiam Girdwood 
47887506549SMark Brown 	if (card->suspend_pre)
47970b2ac12SMark Brown 		card->suspend_pre(card);
480db2a4165SFrank Mandarino 
481bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4821a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
4833efab7dcSMark Brown 
4841a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4853efab7dcSMark Brown 			continue;
4863efab7dcSMark Brown 
487e0f22622SKuninori Morimoto 		if (!cpu_dai->driver->bus_control)
488e0f22622SKuninori Morimoto 			snd_soc_dai_suspend(cpu_dai);
489db2a4165SFrank Mandarino 	}
490db2a4165SFrank Mandarino 
49137660b6dSLars-Peter Clausen 	/* close any waiting streams */
49265462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
493db2a4165SFrank Mandarino 
494bcb1fd1fSKuninori 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 */
513f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
5142c7b696aSMarcel Ziswiler 		struct snd_soc_dapm_context *dapm =
5152c7b696aSMarcel Ziswiler 				snd_soc_component_get_dapm(component);
516d9fc4063SKuninori Morimoto 
5172c7b696aSMarcel Ziswiler 		/*
5182c7b696aSMarcel Ziswiler 		 * If there are paths active then the COMPONENT will be held
5192c7b696aSMarcel Ziswiler 		 * with bias _ON and should not be suspended.
5202c7b696aSMarcel Ziswiler 		 */
521e40fadbcSKuninori Morimoto 		if (!snd_soc_component_is_suspended(component)) {
5224890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
5231547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
524125a25daSMark Brown 				/*
5259178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
526125a25daSMark Brown 				 * bias off then being in STANDBY
527125a25daSMark Brown 				 * means it's doing something,
528125a25daSMark Brown 				 * otherwise fall through.
529125a25daSMark Brown 				 */
5304890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
5319178feb4SKuninori Morimoto 					dev_dbg(component->dev,
53210e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
533125a25daSMark Brown 					break;
534125a25daSMark Brown 				}
5351a12d5dcSGustavo A. R. Silva 				/* fall through */
536a8093297SLars-Peter Clausen 
5371547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
53866c51573SKuninori Morimoto 				snd_soc_component_suspend(component);
5399178feb4SKuninori Morimoto 				if (component->regmap)
5409178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
541988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
5429178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
5431547aba9SMark Brown 				break;
5441547aba9SMark Brown 			default:
5459178feb4SKuninori Morimoto 				dev_dbg(component->dev,
5469178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
5471547aba9SMark Brown 				break;
5481547aba9SMark Brown 			}
5491547aba9SMark Brown 		}
550f0fba2adSLiam Girdwood 	}
551db2a4165SFrank Mandarino 
552bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5531a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
5543efab7dcSMark Brown 
5551a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5563efab7dcSMark Brown 			continue;
5573efab7dcSMark Brown 
558e0f22622SKuninori Morimoto 		if (cpu_dai->driver->bus_control)
559e0f22622SKuninori Morimoto 			snd_soc_dai_suspend(cpu_dai);
560988e8cc4SNicolin Chen 
561988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
562988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
563db2a4165SFrank Mandarino 	}
564db2a4165SFrank Mandarino 
56587506549SMark Brown 	if (card->suspend_post)
56670b2ac12SMark Brown 		card->suspend_post(card);
567db2a4165SFrank Mandarino 
568db2a4165SFrank Mandarino 	return 0;
569db2a4165SFrank Mandarino }
5706f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
571db2a4165SFrank Mandarino 
5722c7b696aSMarcel Ziswiler /*
5732c7b696aSMarcel Ziswiler  * deferred resume work, so resume can complete before we finished
5746ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
5756ed25978SAndy Green  */
5766ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
577db2a4165SFrank Mandarino {
578f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
5792c7b696aSMarcel Ziswiler 			container_of(work, struct snd_soc_card,
5802c7b696aSMarcel Ziswiler 				     deferred_resume_work);
5811a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
582d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
5831a497983SMengdong Lin 	int i;
584db2a4165SFrank Mandarino 
5852c7b696aSMarcel Ziswiler 	/*
5862c7b696aSMarcel Ziswiler 	 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
5876ed25978SAndy Green 	 * so userspace apps are blocked from touching us
5886ed25978SAndy Green 	 */
5896ed25978SAndy Green 
590f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
5916ed25978SAndy Green 
5929949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
593f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
5949949788bSMark Brown 
59587506549SMark Brown 	if (card->resume_pre)
59670b2ac12SMark Brown 		card->resume_pre(card);
597db2a4165SFrank Mandarino 
598bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
599bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6001a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6013efab7dcSMark Brown 
6021a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6033efab7dcSMark Brown 			continue;
6043efab7dcSMark Brown 
60524b09d05SKuninori Morimoto 		if (cpu_dai->driver->bus_control)
60624b09d05SKuninori Morimoto 			snd_soc_dai_resume(cpu_dai);
607db2a4165SFrank Mandarino 	}
608db2a4165SFrank Mandarino 
609f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
610e40fadbcSKuninori Morimoto 		if (snd_soc_component_is_suspended(component))
6119a840cbaSKuninori Morimoto 			snd_soc_component_resume(component);
6121547aba9SMark Brown 	}
613db2a4165SFrank Mandarino 
614bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6153efab7dcSMark Brown 
6161a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6173efab7dcSMark Brown 			continue;
6183efab7dcSMark Brown 
6191a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
620d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
621db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
622f0fba2adSLiam Girdwood 
6231a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
624d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
625db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
626db2a4165SFrank Mandarino 	}
627db2a4165SFrank Mandarino 
6283ff3f64bSMark Brown 	/* unmute any active DACs */
629bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6300b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
6313efab7dcSMark Brown 
6321a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6333efab7dcSMark Brown 			continue;
6343efab7dcSMark Brown 
6350b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
63688fdffa2SKuninori Morimoto 			if (dai->playback_active)
63788fdffa2SKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 0,
63888fdffa2SKuninori Morimoto 						SNDRV_PCM_STREAM_PLAYBACK);
639db2a4165SFrank Mandarino 		}
64088bd870fSBenoit Cousson 	}
641db2a4165SFrank Mandarino 
642bcb1fd1fSKuninori 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 
64824b09d05SKuninori Morimoto 		if (!cpu_dai->driver->bus_control)
64924b09d05SKuninori Morimoto 			snd_soc_dai_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;
67122d251a5SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
67222d251a5SKuninori Morimoto 	int i;
673b9dd94a8SPeter Ujfalusi 
674c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
675c5599b87SLars-Peter Clausen 	if (!card->instantiated)
6765ff1ddf2SEric Miao 		return 0;
6775ff1ddf2SEric Miao 
678988e8cc4SNicolin Chen 	/* activate pins from sleep state */
679bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
68088bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
68188bd870fSBenoit Cousson 
682988e8cc4SNicolin Chen 		if (cpu_dai->active)
683988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
68488bd870fSBenoit Cousson 
68522d251a5SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, 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 	 */
697bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6981a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6992c7b696aSMarcel Ziswiler 
700bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
70182e14e8bSStephen Warren 	}
702bc263214SLars-Peter Clausen 	if (bus_control) {
703bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
70464ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
70564ab9baaSMark Brown 	} else {
706f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
7076308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
708f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
709f0fba2adSLiam Girdwood 	}
7106ed25978SAndy Green 
711db2a4165SFrank Mandarino 	return 0;
712db2a4165SFrank Mandarino }
7136f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
714b3da4251SKuninori Morimoto 
715b3da4251SKuninori Morimoto static void soc_resume_init(struct snd_soc_card *card)
716b3da4251SKuninori Morimoto {
717b3da4251SKuninori Morimoto 	/* deferred resume work */
718b3da4251SKuninori Morimoto 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
719b3da4251SKuninori Morimoto }
720db2a4165SFrank Mandarino #else
7216f8ab4acSMark Brown #define snd_soc_suspend NULL
7226f8ab4acSMark Brown #define snd_soc_resume NULL
723b3da4251SKuninori Morimoto static inline void soc_resume_init(struct snd_soc_card *card)
724b3da4251SKuninori Morimoto {
725b3da4251SKuninori Morimoto }
726db2a4165SFrank Mandarino #endif
727db2a4165SFrank Mandarino 
72885e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
72902a06d30SBarry Song };
73002a06d30SBarry Song 
731c0834440SKuninori Morimoto static struct device_node
732c0834440SKuninori Morimoto *soc_component_to_node(struct snd_soc_component *component)
73312023a9aSMisael Lopez Cruz {
734c0834440SKuninori Morimoto 	struct device_node *of_node;
73512023a9aSMisael Lopez Cruz 
736c0834440SKuninori Morimoto 	of_node = component->dev->of_node;
737c0834440SKuninori Morimoto 	if (!of_node && component->dev->parent)
738c0834440SKuninori Morimoto 		of_node = component->dev->parent->of_node;
73934e81ab4SLars-Peter Clausen 
740c0834440SKuninori Morimoto 	return of_node;
74112023a9aSMisael Lopez Cruz }
74212023a9aSMisael Lopez Cruz 
743be6ac0a9SKuninori Morimoto static int snd_soc_is_matching_component(
744be6ac0a9SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc,
745be6ac0a9SKuninori Morimoto 	struct snd_soc_component *component)
746be6ac0a9SKuninori Morimoto {
747be6ac0a9SKuninori Morimoto 	struct device_node *component_of_node;
748be6ac0a9SKuninori Morimoto 
7497d7db5d3SKuninori Morimoto 	if (!dlc)
7507d7db5d3SKuninori Morimoto 		return 0;
7517d7db5d3SKuninori Morimoto 
7527d7db5d3SKuninori Morimoto 	component_of_node = soc_component_to_node(component);
753be6ac0a9SKuninori Morimoto 
754be6ac0a9SKuninori Morimoto 	if (dlc->of_node && component_of_node != dlc->of_node)
755be6ac0a9SKuninori Morimoto 		return 0;
756be6ac0a9SKuninori Morimoto 	if (dlc->name && strcmp(component->name, dlc->name))
757be6ac0a9SKuninori Morimoto 		return 0;
758be6ac0a9SKuninori Morimoto 
759be6ac0a9SKuninori Morimoto 	return 1;
760be6ac0a9SKuninori Morimoto }
761be6ac0a9SKuninori Morimoto 
762db2a4165SFrank Mandarino static struct snd_soc_component *soc_find_component(
763c1e230f0SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc)
764db2a4165SFrank Mandarino {
765db2a4165SFrank Mandarino 	struct snd_soc_component *component;
766db2a4165SFrank Mandarino 
767db2a4165SFrank Mandarino 	lockdep_assert_held(&client_mutex);
768db2a4165SFrank Mandarino 
769e3303268SKuninori Morimoto 	/*
770e3303268SKuninori Morimoto 	 * NOTE
771e3303268SKuninori Morimoto 	 *
772e3303268SKuninori Morimoto 	 * It returns *1st* found component, but some driver
773e3303268SKuninori Morimoto 	 * has few components by same of_node/name
774e3303268SKuninori Morimoto 	 * ex)
775e3303268SKuninori Morimoto 	 *	CPU component and generic DMAEngine component
776e3303268SKuninori Morimoto 	 */
777c1e230f0SKuninori Morimoto 	for_each_component(component)
778c1e230f0SKuninori Morimoto 		if (snd_soc_is_matching_component(dlc, component))
779db2a4165SFrank Mandarino 			return component;
780db2a4165SFrank Mandarino 
781db2a4165SFrank Mandarino 	return NULL;
78212023a9aSMisael Lopez Cruz }
78312023a9aSMisael Lopez Cruz 
784fbb88b5cSMengdong Lin /**
785fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
786fbb88b5cSMengdong Lin  *
7874958471bSJeffy Chen  * @dlc: name of the DAI or the DAI driver and optional component info to match
788fbb88b5cSMengdong Lin  *
789ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
790fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
791fbb88b5cSMengdong Lin  * should also match if being specified.
792fbb88b5cSMengdong Lin  *
793fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
794fbb88b5cSMengdong Lin  */
795305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
79614621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
79712023a9aSMisael Lopez Cruz {
79814621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
79914621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
80012023a9aSMisael Lopez Cruz 
80134e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
80234e81ab4SLars-Peter Clausen 
80314621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs */
804368dee94SKuninori Morimoto 	for_each_component(component) {
805be6ac0a9SKuninori Morimoto 		if (!snd_soc_is_matching_component(dlc, component))
80612023a9aSMisael Lopez Cruz 			continue;
80715a0c645SKuninori Morimoto 		for_each_component_dais(component, dai) {
8084958471bSJeffy Chen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
8096a6dafdaSJeffy Chen 			    && (!dai->driver->name
8106a6dafdaSJeffy Chen 				|| strcmp(dai->driver->name, dlc->dai_name)))
81114621c7eSLars-Peter Clausen 				continue;
81212023a9aSMisael Lopez Cruz 
81314621c7eSLars-Peter Clausen 			return dai;
81412023a9aSMisael Lopez Cruz 		}
81512023a9aSMisael Lopez Cruz 	}
81612023a9aSMisael Lopez Cruz 
81712023a9aSMisael Lopez Cruz 	return NULL;
81812023a9aSMisael Lopez Cruz }
819305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
82012023a9aSMisael Lopez Cruz 
82117fb1755SMengdong Lin /**
82217fb1755SMengdong Lin  * snd_soc_find_dai_link - Find a DAI link
82317fb1755SMengdong Lin  *
82417fb1755SMengdong Lin  * @card: soc card
82517fb1755SMengdong Lin  * @id: DAI link ID to match
82617fb1755SMengdong Lin  * @name: DAI link name to match, optional
8278abab35fSCharles Keepax  * @stream_name: DAI link stream name to match, optional
82817fb1755SMengdong Lin  *
82917fb1755SMengdong Lin  * This function will search all existing DAI links of the soc card to
83017fb1755SMengdong Lin  * find the link of the same ID. Since DAI links may not have their
83117fb1755SMengdong Lin  * unique ID, so name and stream name should also match if being
83217fb1755SMengdong Lin  * specified.
83317fb1755SMengdong Lin  *
83417fb1755SMengdong Lin  * Return: pointer of DAI link, or NULL if not found.
83517fb1755SMengdong Lin  */
83617fb1755SMengdong Lin struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
83717fb1755SMengdong Lin 					       int id, const char *name,
83817fb1755SMengdong Lin 					       const char *stream_name)
83917fb1755SMengdong Lin {
84042849064SKuninori Morimoto 	struct snd_soc_dai_link *link;
84117fb1755SMengdong Lin 
84217fb1755SMengdong Lin 	lockdep_assert_held(&client_mutex);
84317fb1755SMengdong Lin 
84442849064SKuninori Morimoto 	for_each_card_links(card, link) {
84517fb1755SMengdong Lin 		if (link->id != id)
84617fb1755SMengdong Lin 			continue;
84717fb1755SMengdong Lin 
84817fb1755SMengdong Lin 		if (name && (!link->name || strcmp(name, link->name)))
84917fb1755SMengdong Lin 			continue;
85017fb1755SMengdong Lin 
85117fb1755SMengdong Lin 		if (stream_name && (!link->stream_name
85217fb1755SMengdong Lin 			|| strcmp(stream_name, link->stream_name)))
85317fb1755SMengdong Lin 			continue;
85417fb1755SMengdong Lin 
85517fb1755SMengdong Lin 		return link;
85617fb1755SMengdong Lin 	}
85717fb1755SMengdong Lin 
85817fb1755SMengdong Lin 	return NULL;
85917fb1755SMengdong Lin }
86017fb1755SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
86117fb1755SMengdong Lin 
86249a5ba1cSMengdong Lin static bool soc_is_dai_link_bound(struct snd_soc_card *card,
86349a5ba1cSMengdong Lin 		struct snd_soc_dai_link *dai_link)
864db2a4165SFrank Mandarino {
86549a5ba1cSMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
86649a5ba1cSMengdong Lin 
867bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
86849a5ba1cSMengdong Lin 		if (rtd->dai_link == dai_link)
86949a5ba1cSMengdong Lin 			return true;
87049a5ba1cSMengdong Lin 	}
87149a5ba1cSMengdong Lin 
87249a5ba1cSMengdong Lin 	return false;
87349a5ba1cSMengdong Lin }
87449a5ba1cSMengdong Lin 
8756f2f1ff0SMengdong Lin static int soc_bind_dai_link(struct snd_soc_card *card,
8766f2f1ff0SMengdong Lin 	struct snd_soc_dai_link *dai_link)
877db2a4165SFrank Mandarino {
8781a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
87934614739SJerome Brunet 	struct snd_soc_dai_link_component *codec, *platform;
88090be711eSKuninori Morimoto 	struct snd_soc_component *component;
88188bd870fSBenoit Cousson 	int i;
882db2a4165SFrank Mandarino 
883a655de80SLiam Girdwood 	if (dai_link->ignore)
884a655de80SLiam Girdwood 		return 0;
885a655de80SLiam Girdwood 
8866f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
8876308419aSMark Brown 
88849a5ba1cSMengdong Lin 	if (soc_is_dai_link_bound(card, dai_link)) {
88949a5ba1cSMengdong Lin 		dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
89049a5ba1cSMengdong Lin 			dai_link->name);
89149a5ba1cSMengdong Lin 		return 0;
89249a5ba1cSMengdong Lin 	}
893db2a4165SFrank Mandarino 
894513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
895513cb311SSudip Mukherjee 	if (!rtd)
896513cb311SSudip Mukherjee 		return -ENOMEM;
897513cb311SSudip Mukherjee 
89808a5841eSKuninori Morimoto 	/* FIXME: we need multi CPU support in the future */
89908a5841eSKuninori Morimoto 	rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
900b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
9016b490879SMartin Hundebøll 		dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
90208a5841eSKuninori Morimoto 			 dai_link->cpus->dai_name);
9031a497983SMengdong Lin 		goto _err_defer;
904c5af3a2eSMark Brown 	}
90590be711eSKuninori Morimoto 	snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
906c5af3a2eSMark Brown 
90788bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
908e2b30edfSKuninori Morimoto 	rtd->num_codecs = dai_link->num_codecs;
90934614739SJerome Brunet 	for_each_link_codecs(dai_link, i, codec) {
91034614739SJerome Brunet 		rtd->codec_dais[i] = snd_soc_find_dai(codec);
9110a2cfcd9SKuninori Morimoto 		if (!rtd->codec_dais[i]) {
9127c7e2d6aSStefan Agner 			dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
91334614739SJerome Brunet 				 codec->dai_name);
9141a497983SMengdong Lin 			goto _err_defer;
91512023a9aSMisael Lopez Cruz 		}
91634614739SJerome Brunet 
9170a2cfcd9SKuninori Morimoto 		snd_soc_rtdcom_add(rtd, rtd->codec_dais[i]->component);
91888bd870fSBenoit Cousson 	}
91988bd870fSBenoit Cousson 
92088bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
9210a2cfcd9SKuninori Morimoto 	rtd->codec_dai = rtd->codec_dais[0];
92212023a9aSMisael Lopez Cruz 
923e2b30edfSKuninori Morimoto 	/* Find PLATFORM from registered PLATFORMs */
92434614739SJerome Brunet 	for_each_link_platforms(dai_link, i, platform) {
925368dee94SKuninori Morimoto 		for_each_component(component) {
92634614739SJerome Brunet 			if (!snd_soc_is_matching_component(platform, component))
92790be711eSKuninori Morimoto 				continue;
92890be711eSKuninori Morimoto 
92990be711eSKuninori Morimoto 			snd_soc_rtdcom_add(rtd, component);
93090be711eSKuninori Morimoto 		}
93134614739SJerome Brunet 	}
93290be711eSKuninori Morimoto 
9331a497983SMengdong Lin 	soc_add_pcm_runtime(card, rtd);
934b19e6e7bSMark Brown 	return 0;
9351a497983SMengdong Lin 
9361a497983SMengdong Lin _err_defer:
9371a497983SMengdong Lin 	soc_free_pcm_runtime(rtd);
9381a497983SMengdong Lin 	return -EPROBE_DEFER;
9396b05eda6SMark Brown }
9406b05eda6SMark Brown 
941*ffd60fbaSKuninori Morimoto static void soc_set_of_name_prefix(struct snd_soc_component *component)
942*ffd60fbaSKuninori Morimoto {
943*ffd60fbaSKuninori Morimoto 	struct device_node *of_node = soc_component_to_node(component);
944*ffd60fbaSKuninori Morimoto 	const char *str;
945*ffd60fbaSKuninori Morimoto 	int ret;
946*ffd60fbaSKuninori Morimoto 
947*ffd60fbaSKuninori Morimoto 	ret = of_property_read_string(of_node, "sound-name-prefix", &str);
948*ffd60fbaSKuninori Morimoto 	if (!ret)
949*ffd60fbaSKuninori Morimoto 		component->name_prefix = str;
950*ffd60fbaSKuninori Morimoto }
951*ffd60fbaSKuninori Morimoto 
952*ffd60fbaSKuninori Morimoto static void soc_set_name_prefix(struct snd_soc_card *card,
953*ffd60fbaSKuninori Morimoto 				struct snd_soc_component *component)
954*ffd60fbaSKuninori Morimoto {
955*ffd60fbaSKuninori Morimoto 	int i;
956*ffd60fbaSKuninori Morimoto 
957*ffd60fbaSKuninori Morimoto 	for (i = 0; i < card->num_configs && card->codec_conf; i++) {
958*ffd60fbaSKuninori Morimoto 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
959*ffd60fbaSKuninori Morimoto 		struct device_node *of_node = soc_component_to_node(component);
960*ffd60fbaSKuninori Morimoto 
961*ffd60fbaSKuninori Morimoto 		if (map->of_node && of_node != map->of_node)
962*ffd60fbaSKuninori Morimoto 			continue;
963*ffd60fbaSKuninori Morimoto 		if (map->dev_name && strcmp(component->name, map->dev_name))
964*ffd60fbaSKuninori Morimoto 			continue;
965*ffd60fbaSKuninori Morimoto 		component->name_prefix = map->name_prefix;
966*ffd60fbaSKuninori Morimoto 		return;
967*ffd60fbaSKuninori Morimoto 	}
968*ffd60fbaSKuninori Morimoto 
969*ffd60fbaSKuninori Morimoto 	/*
970*ffd60fbaSKuninori Morimoto 	 * If there is no configuration table or no match in the table,
971*ffd60fbaSKuninori Morimoto 	 * check if a prefix is provided in the node
972*ffd60fbaSKuninori Morimoto 	 */
973*ffd60fbaSKuninori Morimoto 	soc_set_of_name_prefix(component);
974*ffd60fbaSKuninori Morimoto }
975*ffd60fbaSKuninori Morimoto 
97622d14231SKuninori Morimoto static void soc_cleanup_component(struct snd_soc_component *component)
97722d14231SKuninori Morimoto {
9783bb936f5SAmadeusz Sławiński 	snd_soc_component_set_jack(component, NULL, NULL);
97922d14231SKuninori Morimoto 	list_del(&component->card_list);
98022d14231SKuninori Morimoto 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
98122d14231SKuninori Morimoto 	soc_cleanup_component_debugfs(component);
98222d14231SKuninori Morimoto 	component->card = NULL;
9830e36f36bSPierre-Louis Bossart 	snd_soc_component_module_put_when_remove(component);
98422d14231SKuninori Morimoto }
98522d14231SKuninori Morimoto 
986f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
987d12cd198SStephen Warren {
988abd31b32SLars-Peter Clausen 	if (!component->card)
98970090bbbSLars-Peter Clausen 		return;
990d12cd198SStephen Warren 
99103b34dd7SKuninori Morimoto 	snd_soc_component_remove(component);
992d12cd198SStephen Warren 
99322d14231SKuninori Morimoto 	soc_cleanup_component(component);
994d12cd198SStephen Warren }
995d12cd198SStephen Warren 
996*ffd60fbaSKuninori Morimoto static int soc_probe_component(struct snd_soc_card *card,
997*ffd60fbaSKuninori Morimoto 			       struct snd_soc_component *component)
998*ffd60fbaSKuninori Morimoto {
999*ffd60fbaSKuninori Morimoto 	struct snd_soc_dapm_context *dapm =
1000*ffd60fbaSKuninori Morimoto 		snd_soc_component_get_dapm(component);
1001*ffd60fbaSKuninori Morimoto 	struct snd_soc_dai *dai;
1002*ffd60fbaSKuninori Morimoto 	int ret;
1003*ffd60fbaSKuninori Morimoto 
1004*ffd60fbaSKuninori Morimoto 	if (!strcmp(component->name, "snd-soc-dummy"))
1005*ffd60fbaSKuninori Morimoto 		return 0;
1006*ffd60fbaSKuninori Morimoto 
1007*ffd60fbaSKuninori Morimoto 	if (component->card) {
1008*ffd60fbaSKuninori Morimoto 		if (component->card != card) {
1009*ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1010*ffd60fbaSKuninori Morimoto 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1011*ffd60fbaSKuninori Morimoto 				card->name, component->card->name);
1012*ffd60fbaSKuninori Morimoto 			return -ENODEV;
1013*ffd60fbaSKuninori Morimoto 		}
1014*ffd60fbaSKuninori Morimoto 		return 0;
1015*ffd60fbaSKuninori Morimoto 	}
1016*ffd60fbaSKuninori Morimoto 
1017*ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_module_get_when_probe(component);
1018*ffd60fbaSKuninori Morimoto 	if (ret < 0)
1019*ffd60fbaSKuninori Morimoto 		return ret;
1020*ffd60fbaSKuninori Morimoto 
1021*ffd60fbaSKuninori Morimoto 	component->card = card;
1022*ffd60fbaSKuninori Morimoto 	dapm->card = card;
1023*ffd60fbaSKuninori Morimoto 	INIT_LIST_HEAD(&dapm->list);
1024*ffd60fbaSKuninori Morimoto 	soc_set_name_prefix(card, component);
1025*ffd60fbaSKuninori Morimoto 
1026*ffd60fbaSKuninori Morimoto 	soc_init_component_debugfs(component);
1027*ffd60fbaSKuninori Morimoto 
1028*ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_new_controls(dapm,
1029*ffd60fbaSKuninori Morimoto 					component->driver->dapm_widgets,
1030*ffd60fbaSKuninori Morimoto 					component->driver->num_dapm_widgets);
1031*ffd60fbaSKuninori Morimoto 
1032*ffd60fbaSKuninori Morimoto 	if (ret != 0) {
1033*ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1034*ffd60fbaSKuninori Morimoto 			"Failed to create new controls %d\n", ret);
1035*ffd60fbaSKuninori Morimoto 		goto err_probe;
1036*ffd60fbaSKuninori Morimoto 	}
1037*ffd60fbaSKuninori Morimoto 
1038*ffd60fbaSKuninori Morimoto 	for_each_component_dais(component, dai) {
1039*ffd60fbaSKuninori Morimoto 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1040*ffd60fbaSKuninori Morimoto 		if (ret != 0) {
1041*ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1042*ffd60fbaSKuninori Morimoto 				"Failed to create DAI widgets %d\n", ret);
1043*ffd60fbaSKuninori Morimoto 			goto err_probe;
1044*ffd60fbaSKuninori Morimoto 		}
1045*ffd60fbaSKuninori Morimoto 	}
1046*ffd60fbaSKuninori Morimoto 
1047*ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_probe(component);
1048*ffd60fbaSKuninori Morimoto 	if (ret < 0) {
1049*ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1050*ffd60fbaSKuninori Morimoto 			"ASoC: failed to probe component %d\n", ret);
1051*ffd60fbaSKuninori Morimoto 		goto err_probe;
1052*ffd60fbaSKuninori Morimoto 	}
1053*ffd60fbaSKuninori Morimoto 	WARN(dapm->idle_bias_off &&
1054*ffd60fbaSKuninori Morimoto 	     dapm->bias_level != SND_SOC_BIAS_OFF,
1055*ffd60fbaSKuninori Morimoto 	     "codec %s can not start from non-off bias with idle_bias_off==1\n",
1056*ffd60fbaSKuninori Morimoto 	     component->name);
1057*ffd60fbaSKuninori Morimoto 
1058*ffd60fbaSKuninori Morimoto 	/* machine specific init */
1059*ffd60fbaSKuninori Morimoto 	if (component->init) {
1060*ffd60fbaSKuninori Morimoto 		ret = component->init(component);
1061*ffd60fbaSKuninori Morimoto 		if (ret < 0) {
1062*ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1063*ffd60fbaSKuninori Morimoto 				"Failed to do machine specific init %d\n", ret);
1064*ffd60fbaSKuninori Morimoto 			goto err_probe;
1065*ffd60fbaSKuninori Morimoto 		}
1066*ffd60fbaSKuninori Morimoto 	}
1067*ffd60fbaSKuninori Morimoto 
1068*ffd60fbaSKuninori Morimoto 	ret = snd_soc_add_component_controls(component,
1069*ffd60fbaSKuninori Morimoto 					     component->driver->controls,
1070*ffd60fbaSKuninori Morimoto 					     component->driver->num_controls);
1071*ffd60fbaSKuninori Morimoto 	if (ret < 0)
1072*ffd60fbaSKuninori Morimoto 		goto err_probe;
1073*ffd60fbaSKuninori Morimoto 
1074*ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(dapm,
1075*ffd60fbaSKuninori Morimoto 				      component->driver->dapm_routes,
1076*ffd60fbaSKuninori Morimoto 				      component->driver->num_dapm_routes);
1077*ffd60fbaSKuninori Morimoto 	if (ret < 0)
1078*ffd60fbaSKuninori Morimoto 		goto err_probe;
1079*ffd60fbaSKuninori Morimoto 
1080*ffd60fbaSKuninori Morimoto 	list_add(&dapm->list, &card->dapm_list);
1081*ffd60fbaSKuninori Morimoto 	/* see for_each_card_components */
1082*ffd60fbaSKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1083*ffd60fbaSKuninori Morimoto 
1084*ffd60fbaSKuninori Morimoto err_probe:
1085*ffd60fbaSKuninori Morimoto 	if (ret < 0)
1086*ffd60fbaSKuninori Morimoto 		soc_cleanup_component(component);
1087*ffd60fbaSKuninori Morimoto 
1088*ffd60fbaSKuninori Morimoto 	return ret;
1089*ffd60fbaSKuninori Morimoto }
1090*ffd60fbaSKuninori Morimoto 
1091e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1092589c3563SJarkko Nikula {
1093589c3563SJarkko Nikula 	int err;
1094589c3563SJarkko Nikula 
109552abe6ccSGuennadi Liakhovetski 	if (!dai || !dai->probed || !dai->driver ||
10962eda3cb1SKuninori Morimoto 	    dai->driver->remove_order != order)
10972eda3cb1SKuninori Morimoto 		return;
10982eda3cb1SKuninori Morimoto 
1099dcdab582SKuninori Morimoto 	err = snd_soc_dai_remove(dai);
1100589c3563SJarkko Nikula 	if (err < 0)
1101e60cd14fSLars-Peter Clausen 		dev_err(dai->dev,
1102b0aa88afSMisael Lopez Cruz 			"ASoC: failed to remove %s: %d\n",
1103e60cd14fSLars-Peter Clausen 			dai->name, err);
1104dcdab582SKuninori Morimoto 
1105e60cd14fSLars-Peter Clausen 	dai->probed = 0;
1106b0aa88afSMisael Lopez Cruz }
1107b0aa88afSMisael Lopez Cruz 
11089a7c9fe1SKuninori Morimoto static void soc_rtd_free(struct snd_soc_pcm_runtime *rtd); /* remove me */
11091a497983SMengdong Lin static void soc_remove_link_dais(struct snd_soc_card *card,
11101a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1111f0fba2adSLiam Girdwood {
1112e60cd14fSLars-Peter Clausen 	int i;
11130b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1114f0fba2adSLiam Girdwood 
11159a7c9fe1SKuninori Morimoto 	/* finalize rtd device */
11169a7c9fe1SKuninori Morimoto 	soc_rtd_free(rtd);
111702a06d30SBarry Song 
1118f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
11190b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
11200b7990e3SKuninori Morimoto 		soc_remove_dai(codec_dai, order);
1121f0fba2adSLiam Girdwood 
1122e60cd14fSLars-Peter Clausen 	soc_remove_dai(rtd->cpu_dai, order);
1123f0fba2adSLiam Girdwood }
1124f0fba2adSLiam Girdwood 
11251a497983SMengdong Lin static void soc_remove_link_components(struct snd_soc_card *card,
11261a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, int order)
112762ae68faSStephen Warren {
112861aca564SLars-Peter Clausen 	struct snd_soc_component *component;
112990be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
113062ae68faSStephen Warren 
113190be711eSKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
113290be711eSKuninori Morimoto 		component = rtdcom->component;
113362ae68faSStephen Warren 
113470090bbbSLars-Peter Clausen 		if (component->driver->remove_order == order)
113561aca564SLars-Peter Clausen 			soc_remove_component(component);
113662ae68faSStephen Warren 	}
113762ae68faSStephen Warren }
113862ae68faSStephen Warren 
11390671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
11400671fd8eSKuninori Morimoto {
11411a497983SMengdong Lin 	int order;
11421a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
1143f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
11440671fd8eSKuninori Morimoto 
11451a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1146bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd)
11471a497983SMengdong Lin 			soc_remove_link_dais(card, rtd, order);
11480168bf0dSLiam Girdwood 	}
114962ae68faSStephen Warren 
11501a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1151bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd)
11521a497983SMengdong Lin 			soc_remove_link_components(card, rtd, order);
115362ae68faSStephen Warren 	}
115462ae68faSStephen Warren 
115598061fdbSKuninori Morimoto 	for_each_card_links_safe(card, link, _link) {
1156f8f80361SMengdong Lin 		if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1157f8f80361SMengdong Lin 			dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1158f8f80361SMengdong Lin 				link->name);
1159f8f80361SMengdong Lin 
1160f8f80361SMengdong Lin 		list_del(&link->list);
11610671fd8eSKuninori Morimoto 	}
11620671fd8eSKuninori Morimoto }
11630671fd8eSKuninori Morimoto 
1164923c5e61SMengdong Lin static int soc_init_dai_link(struct snd_soc_card *card,
1165923c5e61SMengdong Lin 			     struct snd_soc_dai_link *link)
1166923c5e61SMengdong Lin {
1167adb76b5bSKuninori Morimoto 	int i;
116834614739SJerome Brunet 	struct snd_soc_dai_link_component *codec, *platform;
1169923c5e61SMengdong Lin 
11703db769f1SKuninori Morimoto 	for_each_link_codecs(link, i, codec) {
1171923c5e61SMengdong Lin 		/*
1172923c5e61SMengdong Lin 		 * Codec must be specified by 1 of name or OF node,
1173923c5e61SMengdong Lin 		 * not both or neither.
1174923c5e61SMengdong Lin 		 */
117534614739SJerome Brunet 		if (!!codec->name == !!codec->of_node) {
1176923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1177923c5e61SMengdong Lin 				link->name);
1178923c5e61SMengdong Lin 			return -EINVAL;
1179923c5e61SMengdong Lin 		}
1180af18b13fSJerome Brunet 
1181923c5e61SMengdong Lin 		/* Codec DAI name must be specified */
11823db769f1SKuninori Morimoto 		if (!codec->dai_name) {
1183923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1184923c5e61SMengdong Lin 				link->name);
1185923c5e61SMengdong Lin 			return -EINVAL;
1186923c5e61SMengdong Lin 		}
1187af18b13fSJerome Brunet 
1188af18b13fSJerome Brunet 		/*
1189af18b13fSJerome Brunet 		 * Defer card registration if codec component is not added to
1190af18b13fSJerome Brunet 		 * component list.
1191af18b13fSJerome Brunet 		 */
1192af18b13fSJerome Brunet 		if (!soc_find_component(codec))
1193af18b13fSJerome Brunet 			return -EPROBE_DEFER;
1194923c5e61SMengdong Lin 	}
1195923c5e61SMengdong Lin 
119634614739SJerome Brunet 	for_each_link_platforms(link, i, platform) {
11971d768989SKuninori Morimoto 		/*
119834614739SJerome Brunet 		 * Platform may be specified by either name or OF node, but it
119934614739SJerome Brunet 		 * can be left unspecified, then no components will be inserted
120034614739SJerome Brunet 		 * in the rtdcom list
12011d768989SKuninori Morimoto 		 */
120234614739SJerome Brunet 		if (!!platform->name == !!platform->of_node) {
1203910fdcabSKuninori Morimoto 			dev_err(card->dev,
120434614739SJerome Brunet 				"ASoC: Neither/both platform name/of_node are set for %s\n",
1205923c5e61SMengdong Lin 				link->name);
1206923c5e61SMengdong Lin 			return -EINVAL;
1207923c5e61SMengdong Lin 		}
12088780cf11SAjit Pandey 
12098780cf11SAjit Pandey 		/*
121034614739SJerome Brunet 		 * Defer card registration if platform component is not added to
121134614739SJerome Brunet 		 * component list.
12128780cf11SAjit Pandey 		 */
121334614739SJerome Brunet 		if (!soc_find_component(platform))
12148780cf11SAjit Pandey 			return -EPROBE_DEFER;
1215923c5e61SMengdong Lin 	}
1216923c5e61SMengdong Lin 
1217923c5e61SMengdong Lin 	/* FIXME */
121808a5841eSKuninori Morimoto 	if (link->num_cpus > 1) {
1219923c5e61SMengdong Lin 		dev_err(card->dev,
122008a5841eSKuninori Morimoto 			"ASoC: multi cpu is not yet supported %s\n",
1221923c5e61SMengdong Lin 			link->name);
1222923c5e61SMengdong Lin 		return -EINVAL;
1223923c5e61SMengdong Lin 	}
1224923c5e61SMengdong Lin 
1225923c5e61SMengdong Lin 	/*
1226923c5e61SMengdong Lin 	 * CPU device may be specified by either name or OF node, but
1227923c5e61SMengdong Lin 	 * can be left unspecified, and will be matched based on DAI
1228923c5e61SMengdong Lin 	 * name alone..
1229923c5e61SMengdong Lin 	 */
123008a5841eSKuninori Morimoto 	if (link->cpus->name && link->cpus->of_node) {
1231923c5e61SMengdong Lin 		dev_err(card->dev,
1232923c5e61SMengdong Lin 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
1233923c5e61SMengdong Lin 			link->name);
1234923c5e61SMengdong Lin 		return -EINVAL;
1235923c5e61SMengdong Lin 	}
12368780cf11SAjit Pandey 
12378780cf11SAjit Pandey 	/*
12388780cf11SAjit Pandey 	 * Defer card registartion if cpu dai component is not added to
12398780cf11SAjit Pandey 	 * component list.
12408780cf11SAjit Pandey 	 */
124108a5841eSKuninori Morimoto 	if ((link->cpus->of_node || link->cpus->name) &&
1242c1e230f0SKuninori Morimoto 	    !soc_find_component(link->cpus))
12438780cf11SAjit Pandey 		return -EPROBE_DEFER;
12448780cf11SAjit Pandey 
1245923c5e61SMengdong Lin 	/*
1246923c5e61SMengdong Lin 	 * At least one of CPU DAI name or CPU device name/node must be
1247923c5e61SMengdong Lin 	 * specified
1248923c5e61SMengdong Lin 	 */
124908a5841eSKuninori Morimoto 	if (!link->cpus->dai_name &&
125008a5841eSKuninori Morimoto 	    !(link->cpus->name || link->cpus->of_node)) {
1251923c5e61SMengdong Lin 		dev_err(card->dev,
1252923c5e61SMengdong Lin 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1253923c5e61SMengdong Lin 			link->name);
1254923c5e61SMengdong Lin 		return -EINVAL;
1255923c5e61SMengdong Lin 	}
1256923c5e61SMengdong Lin 
1257923c5e61SMengdong Lin 	return 0;
1258923c5e61SMengdong Lin }
1259923c5e61SMengdong Lin 
1260ef2e8175SKuninori Morimoto void snd_soc_disconnect_sync(struct device *dev)
1261ef2e8175SKuninori Morimoto {
12622c7b696aSMarcel Ziswiler 	struct snd_soc_component *component =
12632c7b696aSMarcel Ziswiler 			snd_soc_lookup_component(dev, NULL);
1264ef2e8175SKuninori Morimoto 
1265ef2e8175SKuninori Morimoto 	if (!component || !component->card)
1266ef2e8175SKuninori Morimoto 		return;
1267ef2e8175SKuninori Morimoto 
1268ef2e8175SKuninori Morimoto 	snd_card_disconnect_sync(component->card->snd_card);
1269ef2e8175SKuninori Morimoto }
1270df532185SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
1271ef2e8175SKuninori Morimoto 
1272f8f80361SMengdong Lin /**
1273f8f80361SMengdong Lin  * snd_soc_add_dai_link - Add a DAI link dynamically
1274f8f80361SMengdong Lin  * @card: The ASoC card to which the DAI link is added
1275f8f80361SMengdong Lin  * @dai_link: The new DAI link to add
1276f8f80361SMengdong Lin  *
1277f8f80361SMengdong Lin  * This function adds a DAI link to the ASoC card's link list.
1278f8f80361SMengdong Lin  *
1279f8f80361SMengdong Lin  * Note: Topology can use this API to add DAI links when probing the
1280f8f80361SMengdong Lin  * topology component. And machine drivers can still define static
1281f8f80361SMengdong Lin  * DAI links in dai_link array.
1282f8f80361SMengdong Lin  */
1283f8f80361SMengdong Lin int snd_soc_add_dai_link(struct snd_soc_card *card,
1284f8f80361SMengdong Lin 		struct snd_soc_dai_link *dai_link)
1285f8f80361SMengdong Lin {
1286f8f80361SMengdong Lin 	if (dai_link->dobj.type
1287f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1288f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1289f8f80361SMengdong Lin 			dai_link->dobj.type);
1290f8f80361SMengdong Lin 		return -EINVAL;
1291f8f80361SMengdong Lin 	}
1292f8f80361SMengdong Lin 
1293f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
12942c7b696aSMarcel Ziswiler 	/*
12952c7b696aSMarcel Ziswiler 	 * Notify the machine driver for extra initialization
1296d6f220eaSMengdong Lin 	 * on the link created by topology.
1297d6f220eaSMengdong Lin 	 */
1298d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->add_dai_link)
1299d6f220eaSMengdong Lin 		card->add_dai_link(card, dai_link);
1300d6f220eaSMengdong Lin 
13016634e3d6SKuninori Morimoto 	/* see for_each_card_links */
1302f8f80361SMengdong Lin 	list_add_tail(&dai_link->list, &card->dai_link_list);
1303f8f80361SMengdong Lin 
1304f8f80361SMengdong Lin 	return 0;
1305f8f80361SMengdong Lin }
1306f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1307f8f80361SMengdong Lin 
1308f8f80361SMengdong Lin /**
1309f8f80361SMengdong Lin  * snd_soc_remove_dai_link - Remove a DAI link from the list
1310f8f80361SMengdong Lin  * @card: The ASoC card that owns the link
1311f8f80361SMengdong Lin  * @dai_link: The DAI link to remove
1312f8f80361SMengdong Lin  *
1313f8f80361SMengdong Lin  * This function removes a DAI link from the ASoC card's link list.
1314f8f80361SMengdong Lin  *
1315f8f80361SMengdong Lin  * For DAI links previously added by topology, topology should
1316f8f80361SMengdong Lin  * remove them by using the dobj embedded in the link.
1317f8f80361SMengdong Lin  */
1318f8f80361SMengdong Lin void snd_soc_remove_dai_link(struct snd_soc_card *card,
1319f8f80361SMengdong Lin 			     struct snd_soc_dai_link *dai_link)
1320f8f80361SMengdong Lin {
1321f8f80361SMengdong Lin 	if (dai_link->dobj.type
1322f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1323f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1324f8f80361SMengdong Lin 			dai_link->dobj.type);
1325f8f80361SMengdong Lin 		return;
1326f8f80361SMengdong Lin 	}
1327f8f80361SMengdong Lin 
1328f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
13292c7b696aSMarcel Ziswiler 	/*
13302c7b696aSMarcel Ziswiler 	 * Notify the machine driver for extra destruction
1331d6f220eaSMengdong Lin 	 * on the link created by topology.
1332d6f220eaSMengdong Lin 	 */
1333d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->remove_dai_link)
1334d6f220eaSMengdong Lin 		card->remove_dai_link(card, dai_link);
1335d6f220eaSMengdong Lin 
1336c26a8841SKuninori Morimoto 	list_del(&dai_link->list);
1337f8f80361SMengdong Lin }
1338f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1339f0fba2adSLiam Girdwood 
13409a7c9fe1SKuninori Morimoto static void soc_rtd_free(struct snd_soc_pcm_runtime *rtd)
13419a7c9fe1SKuninori Morimoto {
13429a7c9fe1SKuninori Morimoto 	if (rtd->dev_registered) {
13439a7c9fe1SKuninori Morimoto 		/* we don't need to call kfree() for rtd->dev */
13449a7c9fe1SKuninori Morimoto 		device_unregister(rtd->dev);
13459a7c9fe1SKuninori Morimoto 		rtd->dev_registered = 0;
13469a7c9fe1SKuninori Morimoto 	}
13479a7c9fe1SKuninori Morimoto }
13489a7c9fe1SKuninori Morimoto 
1349542694dfSKuninori Morimoto static void soc_rtd_release(struct device *dev)
135036ae1a96SMark Brown {
135136ae1a96SMark Brown 	kfree(dev);
135236ae1a96SMark Brown }
1353f0fba2adSLiam Girdwood 
1354542694dfSKuninori Morimoto static int soc_rtd_init(struct snd_soc_pcm_runtime *rtd, const char *name)
1355503ae5e0SMisael Lopez Cruz {
1356589c3563SJarkko Nikula 	int ret = 0;
1357589c3563SJarkko Nikula 
1358589c3563SJarkko Nikula 	/* register the rtd device */
135936ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
136036ae1a96SMark Brown 	if (!rtd->dev)
136136ae1a96SMark Brown 		return -ENOMEM;
13625f3484acSLars-Peter Clausen 	rtd->dev->parent = rtd->card->dev;
1363542694dfSKuninori Morimoto 	rtd->dev->release = soc_rtd_release;
1364d29697dcSTakashi Iwai 	rtd->dev->groups = soc_dev_attr_groups;
1365f294afedSLars-Peter Clausen 	dev_set_name(rtd->dev, "%s", name);
136636ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
136701d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
136801d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
136901d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
137001d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
13714168ddabSKuninori Morimoto 	ret = device_register(rtd->dev);
1372589c3563SJarkko Nikula 	if (ret < 0) {
1373865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1374865df9cbSChuansheng Liu 		put_device(rtd->dev);
13755f3484acSLars-Peter Clausen 		dev_err(rtd->card->dev,
1376f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1377589c3563SJarkko Nikula 		return ret;
1378589c3563SJarkko Nikula 	}
1379589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1380589c3563SJarkko Nikula 	return 0;
1381589c3563SJarkko Nikula }
1382589c3563SJarkko Nikula 
13831a497983SMengdong Lin static int soc_probe_link_components(struct snd_soc_card *card,
13842c7b696aSMarcel Ziswiler 				     struct snd_soc_pcm_runtime *rtd, int order)
138562ae68faSStephen Warren {
1386f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component;
138790be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
138890be711eSKuninori Morimoto 	int ret;
138962ae68faSStephen Warren 
139090be711eSKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
139190be711eSKuninori Morimoto 		component = rtdcom->component;
139290be711eSKuninori Morimoto 
139370090bbbSLars-Peter Clausen 		if (component->driver->probe_order == order) {
1394f1d45cc3SLars-Peter Clausen 			ret = soc_probe_component(card, component);
139562ae68faSStephen Warren 			if (ret < 0)
139662ae68faSStephen Warren 				return ret;
139762ae68faSStephen Warren 		}
139862ae68faSStephen Warren 	}
139962ae68faSStephen Warren 
140062ae68faSStephen Warren 	return 0;
140162ae68faSStephen Warren }
140262ae68faSStephen Warren 
14038e2be562SLars-Peter Clausen static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1404b0aa88afSMisael Lopez Cruz {
1405cfd9b5fbSKuninori Morimoto 	int ret;
1406cfd9b5fbSKuninori Morimoto 
14077a2ccad5SKuninori Morimoto 	if (dai->probed ||
14087a2ccad5SKuninori Morimoto 	    dai->driver->probe_order != order)
14097a2ccad5SKuninori Morimoto 		return 0;
1410b0aa88afSMisael Lopez Cruz 
1411cfd9b5fbSKuninori Morimoto 	ret = snd_soc_dai_probe(dai);
1412b0aa88afSMisael Lopez Cruz 	if (ret < 0) {
14137a2ccad5SKuninori Morimoto 		dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
14148e2be562SLars-Peter Clausen 			dai->name, ret);
1415b0aa88afSMisael Lopez Cruz 		return ret;
1416b0aa88afSMisael Lopez Cruz 	}
1417b0aa88afSMisael Lopez Cruz 
14188e2be562SLars-Peter Clausen 	dai->probed = 1;
1419b0aa88afSMisael Lopez Cruz 
1420b0aa88afSMisael Lopez Cruz 	return 0;
1421b0aa88afSMisael Lopez Cruz }
1422b0aa88afSMisael Lopez Cruz 
142325f7b701SArnaud Pouliquen static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
142425f7b701SArnaud Pouliquen 				struct snd_soc_pcm_runtime *rtd)
142525f7b701SArnaud Pouliquen {
142625f7b701SArnaud Pouliquen 	int i, ret = 0;
142725f7b701SArnaud Pouliquen 
142825f7b701SArnaud Pouliquen 	for (i = 0; i < num_dais; ++i) {
142925f7b701SArnaud Pouliquen 		struct snd_soc_dai_driver *drv = dais[i]->driver;
143025f7b701SArnaud Pouliquen 
1431de17f14eSRohit kumar 		if (drv->pcm_new)
143225f7b701SArnaud Pouliquen 			ret = drv->pcm_new(rtd, dais[i]);
143325f7b701SArnaud Pouliquen 		if (ret < 0) {
143425f7b701SArnaud Pouliquen 			dev_err(dais[i]->dev,
143525f7b701SArnaud Pouliquen 				"ASoC: Failed to bind %s with pcm device\n",
143625f7b701SArnaud Pouliquen 				dais[i]->name);
143725f7b701SArnaud Pouliquen 			return ret;
143825f7b701SArnaud Pouliquen 		}
143925f7b701SArnaud Pouliquen 	}
144025f7b701SArnaud Pouliquen 
144125f7b701SArnaud Pouliquen 	return 0;
144225f7b701SArnaud Pouliquen }
144325f7b701SArnaud Pouliquen 
14441a497983SMengdong Lin static int soc_probe_link_dais(struct snd_soc_card *card,
14451a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1446f0fba2adSLiam Girdwood {
14471a497983SMengdong Lin 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1448c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1449a655de80SLiam Girdwood 	struct snd_soc_rtdcom_list *rtdcom;
1450a655de80SLiam Girdwood 	struct snd_soc_component *component;
14510b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1452a655de80SLiam Girdwood 	int i, ret, num;
1453f0fba2adSLiam Girdwood 
1454f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
14551a497983SMengdong Lin 			card->name, rtd->num, order);
1456f0fba2adSLiam Girdwood 
1457f0fba2adSLiam Girdwood 	/* set default power off timeout */
1458f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1459f0fba2adSLiam Girdwood 
14608e2be562SLars-Peter Clausen 	ret = soc_probe_dai(cpu_dai, order);
14618e2be562SLars-Peter Clausen 	if (ret)
1462f0fba2adSLiam Girdwood 		return ret;
1463f0fba2adSLiam Girdwood 
1464f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
14650b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
14660b7990e3SKuninori Morimoto 		ret = soc_probe_dai(codec_dai, order);
1467b0aa88afSMisael Lopez Cruz 		if (ret)
1468f0fba2adSLiam Girdwood 			return ret;
146988bd870fSBenoit Cousson 	}
1470f0fba2adSLiam Girdwood 
14710168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
14720168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
14730168bf0dSLiam Girdwood 		return 0;
14740168bf0dSLiam Girdwood 
14755f3484acSLars-Peter Clausen 	/* do machine specific initialization */
14765f3484acSLars-Peter Clausen 	if (dai_link->init) {
14775f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
14785f3484acSLars-Peter Clausen 		if (ret < 0) {
14795f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
14805f3484acSLars-Peter Clausen 				dai_link->name, ret);
14815f3484acSLars-Peter Clausen 			return ret;
14825f3484acSLars-Peter Clausen 		}
14835f3484acSLars-Peter Clausen 	}
14845f3484acSLars-Peter Clausen 
148540aa5383SRicard Wanderlof 	if (dai_link->dai_fmt) {
148640aa5383SRicard Wanderlof 		ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
148740aa5383SRicard Wanderlof 		if (ret)
148840aa5383SRicard Wanderlof 			return ret;
148940aa5383SRicard Wanderlof 	}
1490a5053a8eSKuninori Morimoto 
1491542694dfSKuninori Morimoto 	ret = soc_rtd_init(rtd, dai_link->name);
1492589c3563SJarkko Nikula 	if (ret)
1493f0fba2adSLiam Girdwood 		return ret;
1494f0fba2adSLiam Girdwood 
14955f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
14962e55b90aSLars-Peter Clausen 	soc_dpcm_debugfs_add(rtd);
14975f3484acSLars-Peter Clausen 
1498a655de80SLiam Girdwood 	num = rtd->num;
1499a655de80SLiam Girdwood 
1500a655de80SLiam Girdwood 	/*
1501a655de80SLiam Girdwood 	 * most drivers will register their PCMs using DAI link ordering but
1502a655de80SLiam Girdwood 	 * topology based drivers can use the DAI link id field to set PCM
1503a655de80SLiam Girdwood 	 * device number and then use rtd + a base offset of the BEs.
1504a655de80SLiam Girdwood 	 */
1505a655de80SLiam Girdwood 	for_each_rtdcom(rtd, rtdcom) {
1506a655de80SLiam Girdwood 		component = rtdcom->component;
1507a655de80SLiam Girdwood 
1508a655de80SLiam Girdwood 		if (!component->driver->use_dai_pcm_id)
1509a655de80SLiam Girdwood 			continue;
1510a655de80SLiam Girdwood 
1511a655de80SLiam Girdwood 		if (rtd->dai_link->no_pcm)
1512a655de80SLiam Girdwood 			num += component->driver->be_pcm_base;
1513a655de80SLiam Girdwood 		else
1514a655de80SLiam Girdwood 			num = rtd->dai_link->id;
1515a655de80SLiam Girdwood 	}
1516a655de80SLiam Girdwood 
1517b423c420SKuninori Morimoto 	/* create compress_device if possible */
1518b423c420SKuninori Morimoto 	ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1519b423c420SKuninori Morimoto 	if (ret != -ENOTSUPP) {
1520b423c420SKuninori Morimoto 		if (ret < 0)
1521f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
15221245b700SNamarta Kohli 					 dai_link->stream_name);
15231245b700SNamarta Kohli 		return ret;
15241245b700SNamarta Kohli 	}
1525b423c420SKuninori Morimoto 
1526f0fba2adSLiam Girdwood 	/* create the pcm */
1527a655de80SLiam Girdwood 	ret = soc_new_pcm(rtd, num);
1528f0fba2adSLiam Girdwood 	if (ret < 0) {
1529f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
15300837fc62SFabio Estevam 			dai_link->stream_name, ret);
1531f0fba2adSLiam Girdwood 		return ret;
1532f0fba2adSLiam Girdwood 	}
153325f7b701SArnaud Pouliquen 	ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
153425f7b701SArnaud Pouliquen 	if (ret < 0)
153525f7b701SArnaud Pouliquen 		return ret;
153625f7b701SArnaud Pouliquen 	ret = soc_link_dai_pcm_new(rtd->codec_dais,
153725f7b701SArnaud Pouliquen 				   rtd->num_codecs, rtd);
153825f7b701SArnaud Pouliquen 	return ret;
1539f0fba2adSLiam Girdwood }
1540f0fba2adSLiam Girdwood 
1541c2b71c71SKuninori Morimoto static int soc_bind_aux_dev(struct snd_soc_card *card,
1542c2b71c71SKuninori Morimoto 			    struct snd_soc_aux_dev *aux_dev)
1543b19e6e7bSMark Brown {
1544f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
15453ca041edSSebastian Reichel 
1546f2ed6b07SMengdong Lin 	/* codecs, usually analog devices */
15473dc29b8bSKuninori Morimoto 	component = soc_find_component(&aux_dev->dlc);
1548f2ed6b07SMengdong Lin 	if (!component)
15493dc29b8bSKuninori Morimoto 		return -EPROBE_DEFER;
15503ca041edSSebastian Reichel 
1551f2ed6b07SMengdong Lin 	component->init = aux_dev->init;
1552c2b71c71SKuninori Morimoto 	/* see for_each_card_auxs */
1553d2e3a135SSylwester Nawrocki 	list_add(&component->card_aux_list, &card->aux_comp_list);
15541a653aa4SKuninori Morimoto 
1555f2ed6b07SMengdong Lin 	return 0;
1556b19e6e7bSMark Brown }
1557b19e6e7bSMark Brown 
1558f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1559f2ed6b07SMengdong Lin {
1560991454e1SKuninori Morimoto 	struct snd_soc_component *comp;
1561f2ed6b07SMengdong Lin 	int order;
1562f2ed6b07SMengdong Lin 	int ret;
1563f2ed6b07SMengdong Lin 
15641a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1565c2b71c71SKuninori Morimoto 		for_each_card_auxs(card, comp) {
1566f2ed6b07SMengdong Lin 			if (comp->driver->probe_order == order) {
1567f2ed6b07SMengdong Lin 				ret = soc_probe_component(card,	comp);
1568f2ed6b07SMengdong Lin 				if (ret < 0) {
1569f2ed6b07SMengdong Lin 					dev_err(card->dev,
1570f2ed6b07SMengdong Lin 						"ASoC: failed to probe aux component %s %d\n",
1571f2ed6b07SMengdong Lin 						comp->name, ret);
1572f2ed6b07SMengdong Lin 					return ret;
1573f2ed6b07SMengdong Lin 				}
1574f2ed6b07SMengdong Lin 			}
1575f2ed6b07SMengdong Lin 		}
1576f2ed6b07SMengdong Lin 	}
157765d9361fSLars-Peter Clausen 
157844c69bb1SLars-Peter Clausen 	return 0;
15793ca041edSSebastian Reichel }
15802eea392dSJarkko Nikula 
1581f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
158244c69bb1SLars-Peter Clausen {
1583f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1584f2ed6b07SMengdong Lin 	int order;
158544c69bb1SLars-Peter Clausen 
15861a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1587c2b71c71SKuninori Morimoto 		for_each_card_auxs_safe(card, comp, _comp) {
15881a653aa4SKuninori Morimoto 
1589f2ed6b07SMengdong Lin 			if (comp->driver->remove_order == order) {
1590f2ed6b07SMengdong Lin 				soc_remove_component(comp);
1591991454e1SKuninori Morimoto 				/* remove it from the card's aux_comp_list */
1592991454e1SKuninori Morimoto 				list_del(&comp->card_aux_list);
15932eea392dSJarkko Nikula 			}
15945f3484acSLars-Peter Clausen 		}
15952eea392dSJarkko Nikula 	}
15962eea392dSJarkko Nikula }
15972eea392dSJarkko Nikula 
1598ce64c8b9SLars-Peter Clausen /**
1599ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1600ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1601ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1602ce64c8b9SLars-Peter Clausen  *
1603ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1604ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1605ce64c8b9SLars-Peter Clausen  *
1606ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1607ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1608ce64c8b9SLars-Peter Clausen  *
1609ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1610ce64c8b9SLars-Peter Clausen  */
1611ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1612ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1613ce64c8b9SLars-Peter Clausen {
1614ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
16150b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1616ce64c8b9SLars-Peter Clausen 	unsigned int i;
1617ce64c8b9SLars-Peter Clausen 	int ret;
1618ce64c8b9SLars-Peter Clausen 
16190b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1620ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1621ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1622ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1623ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1624ce64c8b9SLars-Peter Clausen 			return ret;
1625ce64c8b9SLars-Peter Clausen 		}
1626ce64c8b9SLars-Peter Clausen 	}
1627ce64c8b9SLars-Peter Clausen 
16282c7b696aSMarcel Ziswiler 	/*
16292c7b696aSMarcel Ziswiler 	 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
16302c7b696aSMarcel Ziswiler 	 * the component which has non_legacy_dai_naming is Codec
16312c7b696aSMarcel Ziswiler 	 */
1632999f7f5aSKuninori Morimoto 	if (cpu_dai->component->driver->non_legacy_dai_naming) {
1633ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1634ce64c8b9SLars-Peter Clausen 
1635ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1636ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1637ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1638ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1639ce64c8b9SLars-Peter Clausen 			break;
1640ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1641ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1642ce64c8b9SLars-Peter Clausen 			break;
1643ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1644ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1645ce64c8b9SLars-Peter Clausen 			break;
1646ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1647ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1648ce64c8b9SLars-Peter Clausen 			break;
1649ce64c8b9SLars-Peter Clausen 		}
1650ce64c8b9SLars-Peter Clausen 
1651ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1652ce64c8b9SLars-Peter Clausen 	}
1653ce64c8b9SLars-Peter Clausen 
1654ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1655ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1656ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1657ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1658ce64c8b9SLars-Peter Clausen 		return ret;
1659ce64c8b9SLars-Peter Clausen 	}
1660ce64c8b9SLars-Peter Clausen 
1661ce64c8b9SLars-Peter Clausen 	return 0;
1662ce64c8b9SLars-Peter Clausen }
1663ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1664ce64c8b9SLars-Peter Clausen 
16651f5a4535STakashi Iwai #ifdef CONFIG_DMI
16662c7b696aSMarcel Ziswiler /*
16672c7b696aSMarcel Ziswiler  * Trim special characters, and replace '-' with '_' since '-' is used to
1668345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1669345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1670345233d7SLiam Girdwood  */
1671345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1672345233d7SLiam Girdwood {
1673345233d7SLiam Girdwood 	int i, j = 0;
1674345233d7SLiam Girdwood 
1675345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1676345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1677345233d7SLiam Girdwood 		    || (name[i] == '_'))
1678345233d7SLiam Girdwood 			name[j++] = name[i];
1679345233d7SLiam Girdwood 		else if (name[i] == '-')
1680345233d7SLiam Girdwood 			name[j++] = '_';
1681345233d7SLiam Girdwood 	}
1682345233d7SLiam Girdwood 
1683345233d7SLiam Girdwood 	name[j] = '\0';
1684345233d7SLiam Girdwood }
1685345233d7SLiam Girdwood 
16862c7b696aSMarcel Ziswiler /*
16872c7b696aSMarcel Ziswiler  * Check if a DMI field is valid, i.e. not containing any string
168898faf436SMengdong Lin  * in the black list.
168998faf436SMengdong Lin  */
169098faf436SMengdong Lin static int is_dmi_valid(const char *field)
169198faf436SMengdong Lin {
169298faf436SMengdong Lin 	int i = 0;
169398faf436SMengdong Lin 
169498faf436SMengdong Lin 	while (dmi_blacklist[i]) {
169598faf436SMengdong Lin 		if (strstr(field, dmi_blacklist[i]))
169698faf436SMengdong Lin 			return 0;
169798faf436SMengdong Lin 		i++;
169846b5a4d2SWu Fengguang 	}
169998faf436SMengdong Lin 
170098faf436SMengdong Lin 	return 1;
170198faf436SMengdong Lin }
170298faf436SMengdong Lin 
1703345233d7SLiam Girdwood /**
1704345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
1705345233d7SLiam Girdwood  * @card: The card to register DMI names
1706345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
1707345233d7SLiam Girdwood  *
1708345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
1709345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
1710345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
1711345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
1712345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
1713345233d7SLiam Girdwood  * as the sound card long name, in the format of
1714345233d7SLiam Girdwood  * "vendor-product-version-board"
1715345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
1716345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
1717345233d7SLiam Girdwood  * (UCM) configurations for the card.
1718345233d7SLiam Girdwood  *
1719345233d7SLiam Girdwood  * Possible card long names may be:
1720345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
1721345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1722345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1723345233d7SLiam Girdwood  *
1724345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
1725345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
1726345233d7SLiam Girdwood  *
1727345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
1728345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
1729345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
1730345233d7SLiam Girdwood  * charactors like SPACE.
1731345233d7SLiam Girdwood  *
1732345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
1733345233d7SLiam Girdwood  */
1734345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1735345233d7SLiam Girdwood {
1736345233d7SLiam Girdwood 	const char *vendor, *product, *product_version, *board;
1737345233d7SLiam Girdwood 	size_t longname_buf_size = sizeof(card->snd_card->longname);
1738345233d7SLiam Girdwood 	size_t len;
1739345233d7SLiam Girdwood 
1740345233d7SLiam Girdwood 	if (card->long_name)
1741345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
1742345233d7SLiam Girdwood 
1743345233d7SLiam Girdwood 	/* make up dmi long name as: vendor.product.version.board */
1744345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
174598faf436SMengdong Lin 	if (!vendor || !is_dmi_valid(vendor)) {
1746345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1747345233d7SLiam Girdwood 		return 0;
1748345233d7SLiam Girdwood 	}
1749345233d7SLiam Girdwood 
1750345233d7SLiam Girdwood 	snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1751345233d7SLiam Girdwood 			 "%s", vendor);
1752345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
1753345233d7SLiam Girdwood 
1754345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
175598faf436SMengdong Lin 	if (product && is_dmi_valid(product)) {
1756345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1757345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1758345233d7SLiam Girdwood 			 longname_buf_size - len,
1759345233d7SLiam Girdwood 			 "-%s", product);
1760345233d7SLiam Girdwood 
1761345233d7SLiam Girdwood 		len++;	/* skip the separator "-" */
1762345233d7SLiam Girdwood 		if (len < longname_buf_size)
1763345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1764345233d7SLiam Girdwood 
17652c7b696aSMarcel Ziswiler 		/*
17662c7b696aSMarcel Ziswiler 		 * some vendors like Lenovo may only put a self-explanatory
1767345233d7SLiam Girdwood 		 * name in the product version field
1768345233d7SLiam Girdwood 		 */
1769345233d7SLiam Girdwood 		product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
177098faf436SMengdong Lin 		if (product_version && is_dmi_valid(product_version)) {
1771345233d7SLiam Girdwood 			len = strlen(card->dmi_longname);
1772345233d7SLiam Girdwood 			snprintf(card->dmi_longname + len,
1773345233d7SLiam Girdwood 				 longname_buf_size - len,
1774345233d7SLiam Girdwood 				 "-%s", product_version);
1775345233d7SLiam Girdwood 
1776345233d7SLiam Girdwood 			len++;
1777345233d7SLiam Girdwood 			if (len < longname_buf_size)
1778345233d7SLiam Girdwood 				cleanup_dmi_name(card->dmi_longname + len);
1779345233d7SLiam Girdwood 		}
1780345233d7SLiam Girdwood 	}
1781345233d7SLiam Girdwood 
1782345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
178398faf436SMengdong Lin 	if (board && is_dmi_valid(board)) {
1784345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1785345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1786345233d7SLiam Girdwood 			 longname_buf_size - len,
1787345233d7SLiam Girdwood 			 "-%s", board);
1788345233d7SLiam Girdwood 
1789345233d7SLiam Girdwood 		len++;
1790345233d7SLiam Girdwood 		if (len < longname_buf_size)
1791345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1792345233d7SLiam Girdwood 	} else if (!product) {
1793345233d7SLiam Girdwood 		/* fall back to using legacy name */
1794345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1795345233d7SLiam Girdwood 		return 0;
1796345233d7SLiam Girdwood 	}
1797345233d7SLiam Girdwood 
1798345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
1799345233d7SLiam Girdwood 	if (flavour) {
1800345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1801345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1802345233d7SLiam Girdwood 			 longname_buf_size - len,
1803345233d7SLiam Girdwood 			 "-%s", flavour);
1804345233d7SLiam Girdwood 
1805345233d7SLiam Girdwood 		len++;
1806345233d7SLiam Girdwood 		if (len < longname_buf_size)
1807345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1808345233d7SLiam Girdwood 	}
1809345233d7SLiam Girdwood 
1810345233d7SLiam Girdwood 	/* set the card long name */
1811345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
1812345233d7SLiam Girdwood 
1813345233d7SLiam Girdwood 	return 0;
1814345233d7SLiam Girdwood }
1815345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
18161f5a4535STakashi Iwai #endif /* CONFIG_DMI */
1817345233d7SLiam Girdwood 
1818a655de80SLiam Girdwood static void soc_check_tplg_fes(struct snd_soc_card *card)
1819a655de80SLiam Girdwood {
1820a655de80SLiam Girdwood 	struct snd_soc_component *component;
1821a655de80SLiam Girdwood 	const struct snd_soc_component_driver *comp_drv;
1822a655de80SLiam Girdwood 	struct snd_soc_dai_link *dai_link;
1823a655de80SLiam Girdwood 	int i;
1824a655de80SLiam Girdwood 
1825368dee94SKuninori Morimoto 	for_each_component(component) {
1826a655de80SLiam Girdwood 
1827a655de80SLiam Girdwood 		/* does this component override FEs ? */
1828a655de80SLiam Girdwood 		if (!component->driver->ignore_machine)
1829a655de80SLiam Girdwood 			continue;
1830a655de80SLiam Girdwood 
1831a655de80SLiam Girdwood 		/* for this machine ? */
1832e194098bSPierre-Louis Bossart 		if (!strcmp(component->driver->ignore_machine,
1833a655de80SLiam Girdwood 			    card->dev->driver->name))
1834e194098bSPierre-Louis Bossart 			goto match;
1835e194098bSPierre-Louis Bossart 		if (strcmp(component->driver->ignore_machine,
1836e194098bSPierre-Louis Bossart 			   dev_name(card->dev)))
1837a655de80SLiam Girdwood 			continue;
1838e194098bSPierre-Louis Bossart match:
1839a655de80SLiam Girdwood 		/* machine matches, so override the rtd data */
18407fe072b4SKuninori Morimoto 		for_each_card_prelinks(card, i, dai_link) {
1841a655de80SLiam Girdwood 
1842a655de80SLiam Girdwood 			/* ignore this FE */
1843a655de80SLiam Girdwood 			if (dai_link->dynamic) {
1844a655de80SLiam Girdwood 				dai_link->ignore = true;
1845a655de80SLiam Girdwood 				continue;
1846a655de80SLiam Girdwood 			}
1847a655de80SLiam Girdwood 
1848a655de80SLiam Girdwood 			dev_info(card->dev, "info: override FE DAI link %s\n",
1849a655de80SLiam Girdwood 				 card->dai_link[i].name);
1850a655de80SLiam Girdwood 
1851a655de80SLiam Girdwood 			/* override platform component */
1852adb76b5bSKuninori Morimoto 			if (!dai_link->platforms) {
1853daecf46eSKuninori Morimoto 				dev_err(card->dev, "init platform error");
1854daecf46eSKuninori Morimoto 				continue;
1855daecf46eSKuninori Morimoto 			}
1856910fdcabSKuninori Morimoto 			dai_link->platforms->name = component->name;
1857a655de80SLiam Girdwood 
1858a655de80SLiam Girdwood 			/* convert non BE into BE */
1859a655de80SLiam Girdwood 			dai_link->no_pcm = 1;
1860a655de80SLiam Girdwood 
1861a655de80SLiam Girdwood 			/* override any BE fixups */
1862a655de80SLiam Girdwood 			dai_link->be_hw_params_fixup =
1863a655de80SLiam Girdwood 				component->driver->be_hw_params_fixup;
1864a655de80SLiam Girdwood 
18652c7b696aSMarcel Ziswiler 			/*
18662c7b696aSMarcel Ziswiler 			 * most BE links don't set stream name, so set it to
1867a655de80SLiam Girdwood 			 * dai link name if it's NULL to help bind widgets.
1868a655de80SLiam Girdwood 			 */
1869a655de80SLiam Girdwood 			if (!dai_link->stream_name)
1870a655de80SLiam Girdwood 				dai_link->stream_name = dai_link->name;
1871a655de80SLiam Girdwood 		}
1872a655de80SLiam Girdwood 
1873a655de80SLiam Girdwood 		/* Inform userspace we are using alternate topology */
1874a655de80SLiam Girdwood 		if (component->driver->topology_name_prefix) {
1875a655de80SLiam Girdwood 
1876a655de80SLiam Girdwood 			/* topology shortname created? */
1877a655de80SLiam Girdwood 			if (!card->topology_shortname_created) {
1878a655de80SLiam Girdwood 				comp_drv = component->driver;
1879a655de80SLiam Girdwood 
1880a655de80SLiam Girdwood 				snprintf(card->topology_shortname, 32, "%s-%s",
1881a655de80SLiam Girdwood 					 comp_drv->topology_name_prefix,
1882a655de80SLiam Girdwood 					 card->name);
1883a655de80SLiam Girdwood 				card->topology_shortname_created = true;
1884a655de80SLiam Girdwood 			}
1885a655de80SLiam Girdwood 
1886a655de80SLiam Girdwood 			/* use topology shortname */
1887a655de80SLiam Girdwood 			card->name = card->topology_shortname;
1888a655de80SLiam Girdwood 		}
1889a655de80SLiam Girdwood 	}
1890a655de80SLiam Girdwood }
1891a655de80SLiam Girdwood 
1892a4de83a3SKuninori Morimoto static void soc_cleanup_card_resources(struct snd_soc_card *card)
189353e947a0SKuninori Morimoto {
189453e947a0SKuninori Morimoto 	/* free the ALSA card at first; this syncs with pending operations */
189529040d1aSKuninori Morimoto 	if (card->snd_card) {
189653e947a0SKuninori Morimoto 		snd_card_free(card->snd_card);
189729040d1aSKuninori Morimoto 		card->snd_card = NULL;
189829040d1aSKuninori Morimoto 	}
189953e947a0SKuninori Morimoto 
190053e947a0SKuninori Morimoto 	/* remove and free each DAI */
190153e947a0SKuninori Morimoto 	soc_remove_dai_links(card);
190253e947a0SKuninori Morimoto 	soc_remove_pcm_runtimes(card);
190353e947a0SKuninori Morimoto 
190453e947a0SKuninori Morimoto 	/* remove auxiliary devices */
190553e947a0SKuninori Morimoto 	soc_remove_aux_devices(card);
190653e947a0SKuninori Morimoto 
190753e947a0SKuninori Morimoto 	snd_soc_dapm_free(&card->dapm);
190853e947a0SKuninori Morimoto 	soc_cleanup_card_debugfs(card);
190953e947a0SKuninori Morimoto 
191053e947a0SKuninori Morimoto 	/* remove the card */
191153e947a0SKuninori Morimoto 	if (card->remove)
191253e947a0SKuninori Morimoto 		card->remove(card);
191353e947a0SKuninori Morimoto }
191453e947a0SKuninori Morimoto 
1915b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1916f0fba2adSLiam Girdwood {
19171a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
191861b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
1919c2b71c71SKuninori Morimoto 	struct snd_soc_aux_dev *aux;
1920ce64c8b9SLars-Peter Clausen 	int ret, i, order;
192196dd3622SMark Brown 
192234e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
192370fc5373STzung-Bi Shih 	for_each_card_prelinks(card, i, dai_link) {
192470fc5373STzung-Bi Shih 		ret = soc_init_dai_link(card, dai_link);
192570fc5373STzung-Bi Shih 		if (ret) {
192670fc5373STzung-Bi Shih 			dev_err(card->dev, "ASoC: failed to init link %s: %d\n",
192770fc5373STzung-Bi Shih 				dai_link->name, ret);
192870fc5373STzung-Bi Shih 			mutex_unlock(&client_mutex);
192970fc5373STzung-Bi Shih 			return ret;
193070fc5373STzung-Bi Shih 		}
193170fc5373STzung-Bi Shih 	}
193201b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1933f0fba2adSLiam Girdwood 
193453e947a0SKuninori Morimoto 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
193553e947a0SKuninori Morimoto 	card->dapm.dev = card->dev;
193653e947a0SKuninori Morimoto 	card->dapm.card = card;
193753e947a0SKuninori Morimoto 	list_add(&card->dapm.list, &card->dapm_list);
193853e947a0SKuninori Morimoto 
1939a655de80SLiam Girdwood 	/* check whether any platform is ignore machine FE and using topology */
1940a655de80SLiam Girdwood 	soc_check_tplg_fes(card);
1941a655de80SLiam Girdwood 
1942b19e6e7bSMark Brown 	/* bind DAIs */
19437fe072b4SKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link) {
19447fe072b4SKuninori Morimoto 		ret = soc_bind_dai_link(card, dai_link);
1945b19e6e7bSMark Brown 		if (ret != 0)
194653e947a0SKuninori Morimoto 			goto probe_end;
1947db2a4165SFrank Mandarino 	}
1948db2a4165SFrank Mandarino 
194944c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
1950c2b71c71SKuninori Morimoto 	for_each_card_pre_auxs(card, i, aux) {
1951c2b71c71SKuninori Morimoto 		ret = soc_bind_aux_dev(card, aux);
1952b19e6e7bSMark Brown 		if (ret != 0)
195353e947a0SKuninori Morimoto 			goto probe_end;
1954db2a4165SFrank Mandarino 	}
1955db2a4165SFrank Mandarino 
1956f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
19575b99a0aaSKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link) {
19585b99a0aaSKuninori Morimoto 		ret = snd_soc_add_dai_link(card, dai_link);
19595b99a0aaSKuninori Morimoto 		if (ret < 0)
19605b99a0aaSKuninori Morimoto 			goto probe_end;
19615b99a0aaSKuninori Morimoto 	}
1962f8f80361SMengdong Lin 
1963f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1964102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1965f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1966f0fba2adSLiam Girdwood 	if (ret < 0) {
196710e8aa9aSMichał Mirosław 		dev_err(card->dev,
196810e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
196910e8aa9aSMichał Mirosław 			card->name, ret);
197053e947a0SKuninori Morimoto 		goto probe_end;
1971db2a4165SFrank Mandarino 	}
1972db2a4165SFrank Mandarino 
19730757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
19740757d834SLars-Peter Clausen 
1975b3da4251SKuninori Morimoto 	soc_resume_init(card);
19766ed25978SAndy Green 
1977b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
19789a841ebbSMark Brown 					card->num_dapm_widgets);
1979b8ba3b57SKuninori Morimoto 	if (ret < 0)
1980b8ba3b57SKuninori Morimoto 		goto probe_end;
19819a841ebbSMark Brown 
1982b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1983f23e860eSNicolin Chen 					card->num_of_dapm_widgets);
1984b8ba3b57SKuninori Morimoto 	if (ret < 0)
1985b8ba3b57SKuninori Morimoto 		goto probe_end;
1986f23e860eSNicolin Chen 
1987f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1988f0fba2adSLiam Girdwood 	if (card->probe) {
1989e7361ec4SMark Brown 		ret = card->probe(card);
1990f0fba2adSLiam Girdwood 		if (ret < 0)
199153e947a0SKuninori Morimoto 			goto probe_end;
1992f0fba2adSLiam Girdwood 	}
1993f0fba2adSLiam Girdwood 
199462ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
19951a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1996bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
19971a497983SMengdong Lin 			ret = soc_probe_link_components(card, rtd, order);
199862ae68faSStephen Warren 			if (ret < 0) {
1999f110bfc7SLiam Girdwood 				dev_err(card->dev,
2000f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
2001f110bfc7SLiam Girdwood 					ret);
200253e947a0SKuninori Morimoto 				goto probe_end;
200362ae68faSStephen Warren 			}
200462ae68faSStephen Warren 		}
200562ae68faSStephen Warren 	}
200662ae68faSStephen Warren 
2007f2ed6b07SMengdong Lin 	/* probe auxiliary components */
2008f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
2009f2ed6b07SMengdong Lin 	if (ret < 0)
201053e947a0SKuninori Morimoto 		goto probe_end;
2011f2ed6b07SMengdong Lin 
20122c7b696aSMarcel Ziswiler 	/*
20132c7b696aSMarcel Ziswiler 	 * Find new DAI links added during probing components and bind them.
201461b0088bSMengdong Lin 	 * Components with topology may bring new DAIs and DAI links.
201561b0088bSMengdong Lin 	 */
201698061fdbSKuninori Morimoto 	for_each_card_links(card, dai_link) {
201761b0088bSMengdong Lin 		if (soc_is_dai_link_bound(card, dai_link))
201861b0088bSMengdong Lin 			continue;
201961b0088bSMengdong Lin 
202061b0088bSMengdong Lin 		ret = soc_init_dai_link(card, dai_link);
202161b0088bSMengdong Lin 		if (ret)
202253e947a0SKuninori Morimoto 			goto probe_end;
202361b0088bSMengdong Lin 		ret = soc_bind_dai_link(card, dai_link);
202461b0088bSMengdong Lin 		if (ret)
202553e947a0SKuninori Morimoto 			goto probe_end;
202661b0088bSMengdong Lin 	}
202761b0088bSMengdong Lin 
202862ae68faSStephen Warren 	/* probe all DAI links on this card */
20291a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
2030bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
20311a497983SMengdong Lin 			ret = soc_probe_link_dais(card, rtd, order);
2032fe3e78e0SMark Brown 			if (ret < 0) {
2033f110bfc7SLiam Girdwood 				dev_err(card->dev,
2034f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
2035f110bfc7SLiam Girdwood 					ret);
203653e947a0SKuninori Morimoto 				goto probe_end;
2037fe3e78e0SMark Brown 			}
2038fe3e78e0SMark Brown 		}
20390168bf0dSLiam Girdwood 	}
2040fe3e78e0SMark Brown 
2041888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
2042b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
2043888df395SMark Brown 
20449b98c7c2SKuninori Morimoto 	ret = snd_soc_add_card_controls(card, card->controls,
20452c7b696aSMarcel Ziswiler 					card->num_controls);
20469b98c7c2SKuninori Morimoto 	if (ret < 0)
20479b98c7c2SKuninori Morimoto 		goto probe_end;
2048b7af1dafSMark Brown 
2049daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2050b8ad29deSMark Brown 				      card->num_dapm_routes);
2051daa480bdSKuninori Morimoto 	if (ret < 0)
2052daa480bdSKuninori Morimoto 		goto probe_end;
2053b8ad29deSMark Brown 
2054daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2055f23e860eSNicolin Chen 				      card->num_of_dapm_routes);
2056daa480bdSKuninori Morimoto 	if (ret < 0)
2057daa480bdSKuninori Morimoto 		goto probe_end;
205875d9ac46SMark Brown 
2059861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
2060861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
2061861886d3STakashi Iwai 
2062f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
2063fe3e78e0SMark Brown 		 "%s", card->name);
2064f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
206522de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
2066f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2067f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
2068f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2069f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
2070f0e8ed85SMark Brown 		case '_':
2071f0e8ed85SMark Brown 		case '-':
2072f0e8ed85SMark Brown 		case '\0':
2073f0e8ed85SMark Brown 			break;
2074f0e8ed85SMark Brown 		default:
2075f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
2076f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
2077f0e8ed85SMark Brown 			break;
2078f0e8ed85SMark Brown 		}
2079f0e8ed85SMark Brown 	}
2080fe3e78e0SMark Brown 
208128e9ad92SMark Brown 	if (card->late_probe) {
208228e9ad92SMark Brown 		ret = card->late_probe(card);
208328e9ad92SMark Brown 		if (ret < 0) {
2084f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
208528e9ad92SMark Brown 				card->name, ret);
208653e947a0SKuninori Morimoto 			goto probe_end;
208728e9ad92SMark Brown 		}
208828e9ad92SMark Brown 	}
208928e9ad92SMark Brown 
2090824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
20918c193b8dSLars-Peter Clausen 
2092f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2093fe3e78e0SMark Brown 	if (ret < 0) {
2094f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2095f110bfc7SLiam Girdwood 				ret);
209653e947a0SKuninori Morimoto 		goto probe_end;
2097fe3e78e0SMark Brown 	}
2098fe3e78e0SMark Brown 
2099435c5e25SMark Brown 	card->instantiated = 1;
2100882eab6cSTzung-Bi Shih 	dapm_mark_endpoints_dirty(card);
21014f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2102b19e6e7bSMark Brown 
210353e947a0SKuninori Morimoto probe_end:
210453e947a0SKuninori Morimoto 	if (ret < 0)
210553e947a0SKuninori Morimoto 		soc_cleanup_card_resources(card);
2106db2a4165SFrank Mandarino 
2107f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
210834e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2109db2a4165SFrank Mandarino 
2110b19e6e7bSMark Brown 	return ret;
2111435c5e25SMark Brown }
2112435c5e25SMark Brown 
2113435c5e25SMark Brown /* probes a new socdev */
2114435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2115435c5e25SMark Brown {
2116f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2117435c5e25SMark Brown 
211870a7ca34SVinod Koul 	/*
211970a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
212070a7ca34SVinod Koul 	 * we should not be here in that case so ret error
212170a7ca34SVinod Koul 	 */
212270a7ca34SVinod Koul 	if (!card)
212370a7ca34SVinod Koul 		return -EINVAL;
212470a7ca34SVinod Koul 
2125fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2126f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2127fe4085e8SMark Brown 		 card->name);
2128fe4085e8SMark Brown 
2129435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2130435c5e25SMark Brown 	card->dev = &pdev->dev;
2131f0fba2adSLiam Girdwood 
213228d528c8SMark Brown 	return snd_soc_register_card(card);
2133435c5e25SMark Brown }
2134435c5e25SMark Brown 
2135b0e26485SVinod Koul /* removes a socdev */
2136b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2137b0e26485SVinod Koul {
2138b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2139b0e26485SVinod Koul 
2140c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2141db2a4165SFrank Mandarino 	return 0;
2142db2a4165SFrank Mandarino }
2143db2a4165SFrank Mandarino 
21446f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
214551737470SMark Brown {
21466f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
21471a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
214851737470SMark Brown 
214951737470SMark Brown 	if (!card->instantiated)
2150416356fcSMark Brown 		return 0;
215151737470SMark Brown 
21522c7b696aSMarcel Ziswiler 	/*
21532c7b696aSMarcel Ziswiler 	 * Flush out pmdown_time work - we actually do want to run it
21542c7b696aSMarcel Ziswiler 	 * now, we're shutting down so no imminent restart.
21552c7b696aSMarcel Ziswiler 	 */
215665462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
215751737470SMark Brown 
2158f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2159416356fcSMark Brown 
2160988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
2161bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
216288bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
21630b7990e3SKuninori Morimoto 		struct snd_soc_dai *codec_dai;
21641a497983SMengdong Lin 		int i;
216588bd870fSBenoit Cousson 
2166988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
21670b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
216888bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
216988bd870fSBenoit Cousson 		}
2170988e8cc4SNicolin Chen 	}
2171988e8cc4SNicolin Chen 
2172416356fcSMark Brown 	return 0;
217351737470SMark Brown }
21746f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
217551737470SMark Brown 
21766f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2177b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2178b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2179b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2180b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
21816f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2182b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2183416356fcSMark Brown };
2184deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2185416356fcSMark Brown 
2186db2a4165SFrank Mandarino /* ASoC platform driver */
2187db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2188db2a4165SFrank Mandarino 	.driver		= {
2189db2a4165SFrank Mandarino 		.name		= "soc-audio",
21906f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2191db2a4165SFrank Mandarino 	},
2192db2a4165SFrank Mandarino 	.probe		= soc_probe,
2193db2a4165SFrank Mandarino 	.remove		= soc_remove,
2194db2a4165SFrank Mandarino };
2195db2a4165SFrank Mandarino 
2196096e49d5SMark Brown /**
2197db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2198db2a4165SFrank Mandarino  * @_template: control template
2199db2a4165SFrank Mandarino  * @data: control private data
2200ac11a2b3SMark Brown  * @long_name: control long name
2201efb7ac3fSMark Brown  * @prefix: control name prefix
2202db2a4165SFrank Mandarino  *
2203db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2204db2a4165SFrank Mandarino  *
2205db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2206db2a4165SFrank Mandarino  */
2207db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
22083056557fSMark Brown 				  void *data, const char *long_name,
2209efb7ac3fSMark Brown 				  const char *prefix)
2210db2a4165SFrank Mandarino {
2211db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2212efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2213efb7ac3fSMark Brown 	char *name = NULL;
2214db2a4165SFrank Mandarino 
2215db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2216db2a4165SFrank Mandarino 	template.index = 0;
2217db2a4165SFrank Mandarino 
2218efb7ac3fSMark Brown 	if (!long_name)
2219efb7ac3fSMark Brown 		long_name = template.name;
2220efb7ac3fSMark Brown 
2221efb7ac3fSMark Brown 	if (prefix) {
22222b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2223efb7ac3fSMark Brown 		if (!name)
2224efb7ac3fSMark Brown 			return NULL;
2225efb7ac3fSMark Brown 
2226efb7ac3fSMark Brown 		template.name = name;
2227efb7ac3fSMark Brown 	} else {
2228efb7ac3fSMark Brown 		template.name = long_name;
2229efb7ac3fSMark Brown 	}
2230efb7ac3fSMark Brown 
2231efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2232efb7ac3fSMark Brown 
2233efb7ac3fSMark Brown 	kfree(name);
2234efb7ac3fSMark Brown 
2235efb7ac3fSMark Brown 	return kcontrol;
2236db2a4165SFrank Mandarino }
2237db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2238db2a4165SFrank Mandarino 
2239022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2240022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2241022658beSLiam Girdwood 	const char *prefix, void *data)
2242022658beSLiam Girdwood {
2243022658beSLiam Girdwood 	int err, i;
2244022658beSLiam Girdwood 
2245022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2246022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
22472c7b696aSMarcel Ziswiler 
2248022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2249022658beSLiam Girdwood 						     control->name, prefix));
2250022658beSLiam Girdwood 		if (err < 0) {
2251f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2252f110bfc7SLiam Girdwood 				control->name, err);
2253022658beSLiam Girdwood 			return err;
2254022658beSLiam Girdwood 		}
2255022658beSLiam Girdwood 	}
2256022658beSLiam Girdwood 
2257022658beSLiam Girdwood 	return 0;
2258022658beSLiam Girdwood }
2259022658beSLiam Girdwood 
22604fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
22614fefd698SDimitris Papastamos 					       const char *name)
22624fefd698SDimitris Papastamos {
22634fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
22644fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
22654fefd698SDimitris Papastamos 
22664fefd698SDimitris Papastamos 	if (unlikely(!name))
22674fefd698SDimitris Papastamos 		return NULL;
22684fefd698SDimitris Papastamos 
22694fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
22704fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
22714fefd698SDimitris Papastamos 			return kctl;
22724fefd698SDimitris Papastamos 	return NULL;
22734fefd698SDimitris Papastamos }
22744fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
22754fefd698SDimitris Papastamos 
2276db2a4165SFrank Mandarino /**
22770f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
22780f2780adSLars-Peter Clausen  *
22790f2780adSLars-Peter Clausen  * @component: Component to add controls to
22800f2780adSLars-Peter Clausen  * @controls: Array of controls to add
22810f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
22820f2780adSLars-Peter Clausen  *
22830f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
22840f2780adSLars-Peter Clausen  */
22850f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
22860f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
22870f2780adSLars-Peter Clausen {
22880f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
22890f2780adSLars-Peter Clausen 
22900f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
22910f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
22920f2780adSLars-Peter Clausen }
22930f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
22940f2780adSLars-Peter Clausen 
22950f2780adSLars-Peter Clausen /**
2296022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2297022658beSLiam Girdwood  * Convenience function to add a list of controls.
2298022658beSLiam Girdwood  *
2299022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2300022658beSLiam Girdwood  * @controls: array of controls to add
2301022658beSLiam Girdwood  * @num_controls: number of elements in the array
2302022658beSLiam Girdwood  *
2303022658beSLiam Girdwood  * Return 0 for success, else error.
2304022658beSLiam Girdwood  */
2305022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2306022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2307022658beSLiam Girdwood {
2308022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2309022658beSLiam Girdwood 
2310022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2311022658beSLiam Girdwood 			NULL, soc_card);
2312022658beSLiam Girdwood }
2313022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2314022658beSLiam Girdwood 
2315022658beSLiam Girdwood /**
2316022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2317022658beSLiam Girdwood  * Convienience function to add a list of controls.
2318022658beSLiam Girdwood  *
2319022658beSLiam Girdwood  * @dai: DAI to add controls to
2320022658beSLiam Girdwood  * @controls: array of controls to add
2321022658beSLiam Girdwood  * @num_controls: number of elements in the array
2322022658beSLiam Girdwood  *
2323022658beSLiam Girdwood  * Return 0 for success, else error.
2324022658beSLiam Girdwood  */
2325022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2326022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2327022658beSLiam Girdwood {
2328313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2329022658beSLiam Girdwood 
2330022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2331022658beSLiam Girdwood 			NULL, dai);
2332022658beSLiam Girdwood }
2333022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2334022658beSLiam Girdwood 
2335e894efefSSrinivas Kandagatla static int snd_soc_bind_card(struct snd_soc_card *card)
2336e894efefSSrinivas Kandagatla {
2337e894efefSSrinivas Kandagatla 	struct snd_soc_pcm_runtime *rtd;
2338e894efefSSrinivas Kandagatla 	int ret;
2339e894efefSSrinivas Kandagatla 
2340e894efefSSrinivas Kandagatla 	ret = snd_soc_instantiate_card(card);
2341e894efefSSrinivas Kandagatla 	if (ret != 0)
2342e894efefSSrinivas Kandagatla 		return ret;
2343e894efefSSrinivas Kandagatla 
2344e894efefSSrinivas Kandagatla 	/* deactivate pins to sleep state */
2345bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
2346e894efefSSrinivas Kandagatla 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2347e894efefSSrinivas Kandagatla 		struct snd_soc_dai *codec_dai;
2348e894efefSSrinivas Kandagatla 		int j;
2349e894efefSSrinivas Kandagatla 
2350e894efefSSrinivas Kandagatla 		for_each_rtd_codec_dai(rtd, j, codec_dai) {
2351e894efefSSrinivas Kandagatla 			if (!codec_dai->active)
2352e894efefSSrinivas Kandagatla 				pinctrl_pm_select_sleep_state(codec_dai->dev);
2353e894efefSSrinivas Kandagatla 		}
2354e894efefSSrinivas Kandagatla 
2355e894efefSSrinivas Kandagatla 		if (!cpu_dai->active)
2356e894efefSSrinivas Kandagatla 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
2357e894efefSSrinivas Kandagatla 	}
2358e894efefSSrinivas Kandagatla 
2359e894efefSSrinivas Kandagatla 	return ret;
2360e894efefSSrinivas Kandagatla }
2361e894efefSSrinivas Kandagatla 
2362c5af3a2eSMark Brown /**
2363c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2364c5af3a2eSMark Brown  *
2365ac11a2b3SMark Brown  * @card: Card to register
2366c5af3a2eSMark Brown  *
2367c5af3a2eSMark Brown  */
236870a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2369c5af3a2eSMark Brown {
2370c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2371c5af3a2eSMark Brown 		return -EINVAL;
2372c5af3a2eSMark Brown 
2373ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2374ed77cc12SMark Brown 
2375b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->widgets);
2376b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->paths);
2377b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->dapm_list);
2378b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->aux_comp_list);
2379b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->component_dev_list);
2380b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->list);
2381f8f80361SMengdong Lin 	INIT_LIST_HEAD(&card->dai_link_list);
23821a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
2383db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
23848a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2385b03bfaecSKuninori Morimoto 
2386b03bfaecSKuninori Morimoto 	card->num_rtd = 0;
2387c5af3a2eSMark Brown 	card->instantiated = 0;
2388f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
2389a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
239072b745e3SPeter Ujfalusi 	mutex_init(&card->pcm_mutex);
2391a9764869SKaiChieh Chuang 	spin_lock_init(&card->dpcm_lock);
2392c5af3a2eSMark Brown 
2393e894efefSSrinivas Kandagatla 	return snd_soc_bind_card(card);
2394c5af3a2eSMark Brown }
239570a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2396c5af3a2eSMark Brown 
2397e894efefSSrinivas Kandagatla static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2398e894efefSSrinivas Kandagatla {
2399f96fb7d1SRanjani Sridharan 	struct snd_soc_pcm_runtime *rtd;
2400f96fb7d1SRanjani Sridharan 	int order;
2401f96fb7d1SRanjani Sridharan 
2402e894efefSSrinivas Kandagatla 	if (card->instantiated) {
2403e894efefSSrinivas Kandagatla 		card->instantiated = false;
2404e894efefSSrinivas Kandagatla 		snd_soc_dapm_shutdown(card);
240553e947a0SKuninori Morimoto 		snd_soc_flush_all_delayed_work(card);
2406f96fb7d1SRanjani Sridharan 
2407f96fb7d1SRanjani Sridharan 		/* remove all components used by DAI links on this card */
2408f96fb7d1SRanjani Sridharan 		for_each_comp_order(order) {
2409f96fb7d1SRanjani Sridharan 			for_each_card_rtds(card, rtd) {
2410f96fb7d1SRanjani Sridharan 				soc_remove_link_components(card, rtd, order);
2411f96fb7d1SRanjani Sridharan 			}
2412f96fb7d1SRanjani Sridharan 		}
2413f96fb7d1SRanjani Sridharan 
2414e894efefSSrinivas Kandagatla 		soc_cleanup_card_resources(card);
2415e894efefSSrinivas Kandagatla 		if (!unregister)
2416e894efefSSrinivas Kandagatla 			list_add(&card->list, &unbind_card_list);
2417e894efefSSrinivas Kandagatla 	} else {
2418e894efefSSrinivas Kandagatla 		if (unregister)
2419e894efefSSrinivas Kandagatla 			list_del(&card->list);
2420e894efefSSrinivas Kandagatla 	}
2421e894efefSSrinivas Kandagatla }
2422e894efefSSrinivas Kandagatla 
2423c5af3a2eSMark Brown /**
2424c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2425c5af3a2eSMark Brown  *
2426ac11a2b3SMark Brown  * @card: Card to unregister
2427c5af3a2eSMark Brown  *
2428c5af3a2eSMark Brown  */
242970a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2430c5af3a2eSMark Brown {
2431b545542aSKuninori Morimoto 	mutex_lock(&client_mutex);
2432e894efefSSrinivas Kandagatla 	snd_soc_unbind_card(card, true);
2433b545542aSKuninori Morimoto 	mutex_unlock(&client_mutex);
2434f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2435c5af3a2eSMark Brown 
2436c5af3a2eSMark Brown 	return 0;
2437c5af3a2eSMark Brown }
243870a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2439c5af3a2eSMark Brown 
2440f0fba2adSLiam Girdwood /*
2441f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
2442f0fba2adSLiam Girdwood  * and sanitizing names.
2443f0fba2adSLiam Girdwood  */
24440b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2445f0fba2adSLiam Girdwood {
2446f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
2447f0fba2adSLiam Girdwood 	int id1, id2;
2448f0fba2adSLiam Girdwood 
2449f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
2450f0fba2adSLiam Girdwood 		return NULL;
2451f0fba2adSLiam Girdwood 
245258818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2453f0fba2adSLiam Girdwood 
2454f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
2455f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
2456f0fba2adSLiam Girdwood 	if (found) {
2457f0fba2adSLiam Girdwood 		/* get ID */
2458f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2459f0fba2adSLiam Girdwood 
2460f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
2461f0fba2adSLiam Girdwood 			if (*id == -1)
2462f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
2463f0fba2adSLiam Girdwood 		}
2464f0fba2adSLiam Girdwood 
2465f0fba2adSLiam Girdwood 	} else {
2466f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr" */
2467f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2468f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
2469f0fba2adSLiam Girdwood 
2470f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
247105899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
2472f0fba2adSLiam Girdwood 
2473f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
24742c7b696aSMarcel Ziswiler 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
24752c7b696aSMarcel Ziswiler 				 name);
247658818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2477f0fba2adSLiam Girdwood 		} else
2478f0fba2adSLiam Girdwood 			*id = 0;
2479f0fba2adSLiam Girdwood 	}
2480f0fba2adSLiam Girdwood 
2481f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
2482f0fba2adSLiam Girdwood }
2483f0fba2adSLiam Girdwood 
2484f0fba2adSLiam Girdwood /*
2485f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
2486f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
2487f0fba2adSLiam Girdwood  */
2488f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
2489f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
2490f0fba2adSLiam Girdwood {
2491f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
249210e8aa9aSMichał Mirosław 		dev_err(dev,
249310e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
249410e8aa9aSMichał Mirosław 			dev_name(dev));
2495f0fba2adSLiam Girdwood 		return NULL;
2496f0fba2adSLiam Girdwood 	}
2497f0fba2adSLiam Girdwood 
2498f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
2499f0fba2adSLiam Girdwood }
2500f0fba2adSLiam Girdwood 
25019115171aSMark Brown /**
250232c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
25039115171aSMark Brown  *
250432c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
25059115171aSMark Brown  */
250632c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
25079115171aSMark Brown {
25085c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
25099115171aSMark Brown 
251015a0c645SKuninori Morimoto 	for_each_component_dais_safe(component, dai, _dai) {
251132c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
251232c9ba54SLars-Peter Clausen 			dai->name);
25139115171aSMark Brown 		list_del(&dai->list);
2514f0fba2adSLiam Girdwood 		kfree(dai->name);
2515f0fba2adSLiam Girdwood 		kfree(dai);
25169115171aSMark Brown 	}
251732c9ba54SLars-Peter Clausen }
25189115171aSMark Brown 
25195e4fb372SMengdong Lin /* Create a DAI and add it to the component's DAI list */
25205e4fb372SMengdong Lin static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
25215e4fb372SMengdong Lin 	struct snd_soc_dai_driver *dai_drv,
25225e4fb372SMengdong Lin 	bool legacy_dai_naming)
25235e4fb372SMengdong Lin {
25245e4fb372SMengdong Lin 	struct device *dev = component->dev;
25255e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
25265e4fb372SMengdong Lin 
25275e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
25285e4fb372SMengdong Lin 
25295e4fb372SMengdong Lin 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
25305e4fb372SMengdong Lin 	if (dai == NULL)
25315e4fb372SMengdong Lin 		return NULL;
25325e4fb372SMengdong Lin 
25335e4fb372SMengdong Lin 	/*
25345e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
25355e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
25365e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
25375e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
25385e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
25395e4fb372SMengdong Lin 	 * component-less anymore.
25405e4fb372SMengdong Lin 	 */
25415e4fb372SMengdong Lin 	if (legacy_dai_naming &&
25425e4fb372SMengdong Lin 	    (dai_drv->id == 0 || dai_drv->name == NULL)) {
25435e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
25445e4fb372SMengdong Lin 	} else {
25455e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
25465e4fb372SMengdong Lin 		if (dai_drv->id)
25475e4fb372SMengdong Lin 			dai->id = dai_drv->id;
25485e4fb372SMengdong Lin 		else
25495e4fb372SMengdong Lin 			dai->id = component->num_dai;
25505e4fb372SMengdong Lin 	}
25515e4fb372SMengdong Lin 	if (dai->name == NULL) {
25525e4fb372SMengdong Lin 		kfree(dai);
25535e4fb372SMengdong Lin 		return NULL;
25545e4fb372SMengdong Lin 	}
25555e4fb372SMengdong Lin 
25565e4fb372SMengdong Lin 	dai->component = component;
25575e4fb372SMengdong Lin 	dai->dev = dev;
25585e4fb372SMengdong Lin 	dai->driver = dai_drv;
25595e4fb372SMengdong Lin 	if (!dai->driver->ops)
25605e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
25615e4fb372SMengdong Lin 
256215a0c645SKuninori Morimoto 	/* see for_each_component_dais */
256358bf4179SKuninori Morimoto 	list_add_tail(&dai->list, &component->dai_list);
25645e4fb372SMengdong Lin 	component->num_dai++;
25655e4fb372SMengdong Lin 
25665e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
25675e4fb372SMengdong Lin 	return dai;
25685e4fb372SMengdong Lin }
25695e4fb372SMengdong Lin 
25709115171aSMark Brown /**
257132c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
25729115171aSMark Brown  *
25736106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
25746106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
2575ac11a2b3SMark Brown  * @count: Number of DAIs
25769115171aSMark Brown  */
257732c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
25782c7b696aSMarcel Ziswiler 				 struct snd_soc_dai_driver *dai_drv,
25792c7b696aSMarcel Ziswiler 				 size_t count)
25809115171aSMark Brown {
25816106d129SLars-Peter Clausen 	struct device *dev = component->dev;
2582f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
258332c9ba54SLars-Peter Clausen 	unsigned int i;
258432c9ba54SLars-Peter Clausen 	int ret;
2585f0fba2adSLiam Girdwood 
25865b5e0928SAlexey Dobriyan 	dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
25879115171aSMark Brown 
25889115171aSMark Brown 	for (i = 0; i < count; i++) {
2589f0fba2adSLiam Girdwood 
25902c7b696aSMarcel Ziswiler 		dai = soc_add_dai(component, dai_drv + i, count == 1 &&
25912c7b696aSMarcel Ziswiler 				  !component->driver->non_legacy_dai_naming);
2592c46e0079SAxel Lin 		if (dai == NULL) {
2593c46e0079SAxel Lin 			ret = -ENOMEM;
2594c46e0079SAxel Lin 			goto err;
2595c46e0079SAxel Lin 		}
2596f0fba2adSLiam Girdwood 	}
2597f0fba2adSLiam Girdwood 
25989115171aSMark Brown 	return 0;
25999115171aSMark Brown 
26009115171aSMark Brown err:
260132c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
26029115171aSMark Brown 
26039115171aSMark Brown 	return ret;
26049115171aSMark Brown }
26059115171aSMark Brown 
260668003e6cSMengdong Lin /**
260768003e6cSMengdong Lin  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
260868003e6cSMengdong Lin  *
260968003e6cSMengdong Lin  * @component: The component the DAIs are registered for
261068003e6cSMengdong Lin  * @dai_drv: DAI driver to use for the DAI
261168003e6cSMengdong Lin  *
261268003e6cSMengdong Lin  * Topology can use this API to register DAIs when probing a component.
261368003e6cSMengdong Lin  * These DAIs's widgets will be freed in the card cleanup and the DAIs
261468003e6cSMengdong Lin  * will be freed in the component cleanup.
261568003e6cSMengdong Lin  */
261668003e6cSMengdong Lin int snd_soc_register_dai(struct snd_soc_component *component,
261768003e6cSMengdong Lin 	struct snd_soc_dai_driver *dai_drv)
261868003e6cSMengdong Lin {
261968003e6cSMengdong Lin 	struct snd_soc_dapm_context *dapm =
262068003e6cSMengdong Lin 		snd_soc_component_get_dapm(component);
262168003e6cSMengdong Lin 	struct snd_soc_dai *dai;
262268003e6cSMengdong Lin 	int ret;
262368003e6cSMengdong Lin 
262468003e6cSMengdong Lin 	if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
262568003e6cSMengdong Lin 		dev_err(component->dev, "Invalid dai type %d\n",
262668003e6cSMengdong Lin 			dai_drv->dobj.type);
262768003e6cSMengdong Lin 		return -EINVAL;
262868003e6cSMengdong Lin 	}
262968003e6cSMengdong Lin 
263068003e6cSMengdong Lin 	lockdep_assert_held(&client_mutex);
263168003e6cSMengdong Lin 	dai = soc_add_dai(component, dai_drv, false);
263268003e6cSMengdong Lin 	if (!dai)
263368003e6cSMengdong Lin 		return -ENOMEM;
263468003e6cSMengdong Lin 
26352c7b696aSMarcel Ziswiler 	/*
26362c7b696aSMarcel Ziswiler 	 * Create the DAI widgets here. After adding DAIs, topology may
263768003e6cSMengdong Lin 	 * also add routes that need these widgets as source or sink.
263868003e6cSMengdong Lin 	 */
263968003e6cSMengdong Lin 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
264068003e6cSMengdong Lin 	if (ret != 0) {
264168003e6cSMengdong Lin 		dev_err(component->dev,
264268003e6cSMengdong Lin 			"Failed to create DAI widgets %d\n", ret);
264368003e6cSMengdong Lin 	}
264468003e6cSMengdong Lin 
264568003e6cSMengdong Lin 	return ret;
264668003e6cSMengdong Lin }
264768003e6cSMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_register_dai);
264868003e6cSMengdong Lin 
2649bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
2650bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
2651d191bd8dSKuninori Morimoto {
2652ce0fc93aSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
2653ce0fc93aSLars-Peter Clausen 
26548d92bb51SKuninori Morimoto 	INIT_LIST_HEAD(&component->dai_list);
2655495efdb0SKuninori Morimoto 	INIT_LIST_HEAD(&component->dobj_list);
2656495efdb0SKuninori Morimoto 	INIT_LIST_HEAD(&component->card_list);
26578d92bb51SKuninori Morimoto 	mutex_init(&component->io_mutex);
26588d92bb51SKuninori Morimoto 
2659bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
2660bb13109dSLars-Peter Clausen 	if (!component->name) {
2661bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
2662d191bd8dSKuninori Morimoto 		return -ENOMEM;
2663d191bd8dSKuninori Morimoto 	}
2664d191bd8dSKuninori Morimoto 
2665bb13109dSLars-Peter Clausen 	component->dev = dev;
2666bb13109dSLars-Peter Clausen 	component->driver = driver;
2667e2c330b9SLars-Peter Clausen 
266888c27465SKuninori Morimoto 	dapm = snd_soc_component_get_dapm(component);
2669ce0fc93aSLars-Peter Clausen 	dapm->dev = dev;
2670ce0fc93aSLars-Peter Clausen 	dapm->component = component;
2671ce0fc93aSLars-Peter Clausen 	dapm->bias_level = SND_SOC_BIAS_OFF;
26727ba236ceSKuninori Morimoto 	dapm->idle_bias_off = !driver->idle_bias_on;
26737ba236ceSKuninori Morimoto 	dapm->suspend_bias_off = driver->suspend_bias_off;
2674ce0fc93aSLars-Peter Clausen 
2675bb13109dSLars-Peter Clausen 	return 0;
2676d191bd8dSKuninori Morimoto }
2677d191bd8dSKuninori Morimoto 
267820feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2679886f5692SLars-Peter Clausen {
2680886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
268120feb881SLars-Peter Clausen 
2682886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
2683886f5692SLars-Peter Clausen 	if (val_bytes > 0)
2684886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
2685886f5692SLars-Peter Clausen }
268620feb881SLars-Peter Clausen 
2687e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
2688e874bf5fSLars-Peter Clausen 
268920feb881SLars-Peter Clausen /**
26902c7b696aSMarcel Ziswiler  * snd_soc_component_init_regmap() - Initialize regmap instance for the
26912c7b696aSMarcel Ziswiler  *                                   component
269220feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
269320feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
269420feb881SLars-Peter Clausen  *
269520feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
269620feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
269720feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
269820feb881SLars-Peter Clausen  * before the first IO attempt of the component.
269920feb881SLars-Peter Clausen  */
270020feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
270120feb881SLars-Peter Clausen 	struct regmap *regmap)
270220feb881SLars-Peter Clausen {
270320feb881SLars-Peter Clausen 	component->regmap = regmap;
270420feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
2705886f5692SLars-Peter Clausen }
270620feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
270720feb881SLars-Peter Clausen 
270820feb881SLars-Peter Clausen /**
27092c7b696aSMarcel Ziswiler  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
27102c7b696aSMarcel Ziswiler  *                                   component
271120feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
271220feb881SLars-Peter Clausen  *
271320feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
271420feb881SLars-Peter Clausen  * removes the regmap instance from the component.
271520feb881SLars-Peter Clausen  *
271620feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
271720feb881SLars-Peter Clausen  * to initialize the regmap instance.
271820feb881SLars-Peter Clausen  */
271920feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
272020feb881SLars-Peter Clausen {
272120feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
272220feb881SLars-Peter Clausen 	component->regmap = NULL;
272320feb881SLars-Peter Clausen }
272420feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2725886f5692SLars-Peter Clausen 
2726e874bf5fSLars-Peter Clausen #endif
2727d191bd8dSKuninori Morimoto 
2728359c71eeSKuninori Morimoto static void snd_soc_component_add(struct snd_soc_component *component)
2729bb13109dSLars-Peter Clausen {
2730359c71eeSKuninori Morimoto 	mutex_lock(&client_mutex);
2731359c71eeSKuninori Morimoto 
2732999f7f5aSKuninori Morimoto 	if (!component->driver->write && !component->driver->read) {
273320feb881SLars-Peter Clausen 		if (!component->regmap)
27342c7b696aSMarcel Ziswiler 			component->regmap = dev_get_regmap(component->dev,
27352c7b696aSMarcel Ziswiler 							   NULL);
273620feb881SLars-Peter Clausen 		if (component->regmap)
273720feb881SLars-Peter Clausen 			snd_soc_component_setup_regmap(component);
273820feb881SLars-Peter Clausen 	}
2739886f5692SLars-Peter Clausen 
2740368dee94SKuninori Morimoto 	/* see for_each_component */
2741bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
2742d191bd8dSKuninori Morimoto 
2743d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
2744bb13109dSLars-Peter Clausen }
2745d191bd8dSKuninori Morimoto 
2746bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
2747bb13109dSLars-Peter Clausen {
2748bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
2749bb13109dSLars-Peter Clausen 	kfree(component->name);
2750bb13109dSLars-Peter Clausen }
2751d191bd8dSKuninori Morimoto 
2752bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2753bb13109dSLars-Peter Clausen {
2754c12c1aadSKuninori Morimoto 	struct snd_soc_card *card = component->card;
2755c12c1aadSKuninori Morimoto 
2756c12c1aadSKuninori Morimoto 	if (card)
2757e894efefSSrinivas Kandagatla 		snd_soc_unbind_card(card, false);
2758c12c1aadSKuninori Morimoto 
2759bb13109dSLars-Peter Clausen 	list_del(&component->list);
2760bb13109dSLars-Peter Clausen }
2761d191bd8dSKuninori Morimoto 
2762273d778eSKuninori Morimoto #define ENDIANNESS_MAP(name) \
2763273d778eSKuninori Morimoto 	(SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2764273d778eSKuninori Morimoto static u64 endianness_format_map[] = {
2765273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S16_),
2766273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U16_),
2767273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_),
2768273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_),
2769273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S32_),
2770273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U32_),
2771273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_3),
2772273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_3),
2773273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S20_3),
2774273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U20_3),
2775273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S18_3),
2776273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U18_3),
2777273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT_),
2778273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT64_),
2779273d778eSKuninori Morimoto 	ENDIANNESS_MAP(IEC958_SUBFRAME_),
2780273d778eSKuninori Morimoto };
2781273d778eSKuninori Morimoto 
2782273d778eSKuninori Morimoto /*
2783273d778eSKuninori Morimoto  * Fix up the DAI formats for endianness: codecs don't actually see
2784273d778eSKuninori Morimoto  * the endianness of the data but we're using the CPU format
2785273d778eSKuninori Morimoto  * definitions which do need to include endianness so we ensure that
2786273d778eSKuninori Morimoto  * codec DAIs always have both big and little endian variants set.
2787273d778eSKuninori Morimoto  */
2788273d778eSKuninori Morimoto static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2789273d778eSKuninori Morimoto {
2790273d778eSKuninori Morimoto 	int i;
2791273d778eSKuninori Morimoto 
2792273d778eSKuninori Morimoto 	for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2793273d778eSKuninori Morimoto 		if (stream->formats & endianness_format_map[i])
2794273d778eSKuninori Morimoto 			stream->formats |= endianness_format_map[i];
2795273d778eSKuninori Morimoto }
2796273d778eSKuninori Morimoto 
2797e894efefSSrinivas Kandagatla static void snd_soc_try_rebind_card(void)
2798e894efefSSrinivas Kandagatla {
2799e894efefSSrinivas Kandagatla 	struct snd_soc_card *card, *c;
2800e894efefSSrinivas Kandagatla 
2801b245d273SKuninori Morimoto 	list_for_each_entry_safe(card, c, &unbind_card_list, list)
2802e894efefSSrinivas Kandagatla 		if (!snd_soc_bind_card(card))
2803e894efefSSrinivas Kandagatla 			list_del(&card->list);
2804e894efefSSrinivas Kandagatla }
2805e894efefSSrinivas Kandagatla 
2806e0dac41bSKuninori Morimoto int snd_soc_add_component(struct device *dev,
2807e0dac41bSKuninori Morimoto 			struct snd_soc_component *component,
2808cf9e829eSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2809d191bd8dSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2810d191bd8dSKuninori Morimoto 			int num_dai)
2811d191bd8dSKuninori Morimoto {
2812bb13109dSLars-Peter Clausen 	int ret;
2813273d778eSKuninori Morimoto 	int i;
2814d191bd8dSKuninori Morimoto 
2815cf9e829eSKuninori Morimoto 	ret = snd_soc_component_initialize(component, component_driver, dev);
2816bb13109dSLars-Peter Clausen 	if (ret)
2817bb13109dSLars-Peter Clausen 		goto err_free;
2818bb13109dSLars-Peter Clausen 
2819273d778eSKuninori Morimoto 	if (component_driver->endianness) {
2820273d778eSKuninori Morimoto 		for (i = 0; i < num_dai; i++) {
2821273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].playback);
2822273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].capture);
2823273d778eSKuninori Morimoto 		}
2824273d778eSKuninori Morimoto 	}
2825273d778eSKuninori Morimoto 
28260e7b25c6SKuninori Morimoto 	ret = snd_soc_register_dais(component, dai_drv, num_dai);
2827bb13109dSLars-Peter Clausen 	if (ret < 0) {
2828f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
2829bb13109dSLars-Peter Clausen 		goto err_cleanup;
2830bb13109dSLars-Peter Clausen 	}
2831bb13109dSLars-Peter Clausen 
2832cf9e829eSKuninori Morimoto 	snd_soc_component_add(component);
2833e894efefSSrinivas Kandagatla 	snd_soc_try_rebind_card();
2834bb13109dSLars-Peter Clausen 
2835bb13109dSLars-Peter Clausen 	return 0;
2836bb13109dSLars-Peter Clausen 
2837bb13109dSLars-Peter Clausen err_cleanup:
2838cf9e829eSKuninori Morimoto 	snd_soc_component_cleanup(component);
2839bb13109dSLars-Peter Clausen err_free:
2840bb13109dSLars-Peter Clausen 	return ret;
2841d191bd8dSKuninori Morimoto }
2842e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component);
2843e0dac41bSKuninori Morimoto 
2844e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev,
2845e0dac41bSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2846e0dac41bSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2847e0dac41bSKuninori Morimoto 			int num_dai)
2848e0dac41bSKuninori Morimoto {
2849e0dac41bSKuninori Morimoto 	struct snd_soc_component *component;
2850e0dac41bSKuninori Morimoto 
28517ecbd6a9SKuninori Morimoto 	component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
285208e61d03SKuninori Morimoto 	if (!component)
2853e0dac41bSKuninori Morimoto 		return -ENOMEM;
2854e0dac41bSKuninori Morimoto 
2855e0dac41bSKuninori Morimoto 	return snd_soc_add_component(dev, component, component_driver,
2856e0dac41bSKuninori Morimoto 				     dai_drv, num_dai);
2857e0dac41bSKuninori Morimoto }
2858d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
2859d191bd8dSKuninori Morimoto 
2860d191bd8dSKuninori Morimoto /**
28612eccea8cSKuninori Morimoto  * snd_soc_unregister_component - Unregister all related component
28622eccea8cSKuninori Morimoto  * from the ASoC core
2863d191bd8dSKuninori Morimoto  *
2864628536eaSJonathan Corbet  * @dev: The device to unregister
2865d191bd8dSKuninori Morimoto  */
28662eccea8cSKuninori Morimoto static int __snd_soc_unregister_component(struct device *dev)
2867d191bd8dSKuninori Morimoto {
2868cf9e829eSKuninori Morimoto 	struct snd_soc_component *component;
286921a03528SKuninori Morimoto 	int found = 0;
2870d191bd8dSKuninori Morimoto 
287134e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
2872368dee94SKuninori Morimoto 	for_each_component(component) {
2873999f7f5aSKuninori Morimoto 		if (dev != component->dev)
287421a03528SKuninori Morimoto 			continue;
2875d191bd8dSKuninori Morimoto 
28762c7b696aSMarcel Ziswiler 		snd_soc_tplg_component_remove(component,
28772c7b696aSMarcel Ziswiler 					      SND_SOC_TPLG_INDEX_ALL);
2878cf9e829eSKuninori Morimoto 		snd_soc_component_del_unlocked(component);
287921a03528SKuninori Morimoto 		found = 1;
288021a03528SKuninori Morimoto 		break;
2881d191bd8dSKuninori Morimoto 	}
2882d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
2883d191bd8dSKuninori Morimoto 
28842c7b696aSMarcel Ziswiler 	if (found)
2885cf9e829eSKuninori Morimoto 		snd_soc_component_cleanup(component);
28862eccea8cSKuninori Morimoto 
28872eccea8cSKuninori Morimoto 	return found;
28882eccea8cSKuninori Morimoto }
28892eccea8cSKuninori Morimoto 
28902eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
28912eccea8cSKuninori Morimoto {
28922c7b696aSMarcel Ziswiler 	while (__snd_soc_unregister_component(dev))
28932c7b696aSMarcel Ziswiler 		;
2894d191bd8dSKuninori Morimoto }
2895d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2896d191bd8dSKuninori Morimoto 
28977dd5d0d9SKuninori Morimoto struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
28987dd5d0d9SKuninori Morimoto 						   const char *driver_name)
28997dd5d0d9SKuninori Morimoto {
29007dd5d0d9SKuninori Morimoto 	struct snd_soc_component *component;
29017dd5d0d9SKuninori Morimoto 	struct snd_soc_component *ret;
29027dd5d0d9SKuninori Morimoto 
29037dd5d0d9SKuninori Morimoto 	ret = NULL;
29047dd5d0d9SKuninori Morimoto 	mutex_lock(&client_mutex);
2905368dee94SKuninori Morimoto 	for_each_component(component) {
29067dd5d0d9SKuninori Morimoto 		if (dev != component->dev)
29077dd5d0d9SKuninori Morimoto 			continue;
29087dd5d0d9SKuninori Morimoto 
29097dd5d0d9SKuninori Morimoto 		if (driver_name &&
29107dd5d0d9SKuninori Morimoto 		    (driver_name != component->driver->name) &&
29117dd5d0d9SKuninori Morimoto 		    (strcmp(component->driver->name, driver_name) != 0))
29127dd5d0d9SKuninori Morimoto 			continue;
29137dd5d0d9SKuninori Morimoto 
29147dd5d0d9SKuninori Morimoto 		ret = component;
29157dd5d0d9SKuninori Morimoto 		break;
29167dd5d0d9SKuninori Morimoto 	}
29177dd5d0d9SKuninori Morimoto 	mutex_unlock(&client_mutex);
29187dd5d0d9SKuninori Morimoto 
29197dd5d0d9SKuninori Morimoto 	return ret;
29207dd5d0d9SKuninori Morimoto }
29217dd5d0d9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
29227dd5d0d9SKuninori Morimoto 
2923bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
2924b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2925bec4fa05SStephen Warren 			       const char *propname)
2926bec4fa05SStephen Warren {
2927b07609ceSKuninori Morimoto 	struct device_node *np;
2928bec4fa05SStephen Warren 	int ret;
2929bec4fa05SStephen Warren 
29307e07e7c0STushar Behera 	if (!card->dev) {
29317e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
29327e07e7c0STushar Behera 		return -EINVAL;
29337e07e7c0STushar Behera 	}
29347e07e7c0STushar Behera 
29357e07e7c0STushar Behera 	np = card->dev->of_node;
29367e07e7c0STushar Behera 
2937bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
2938bec4fa05SStephen Warren 	/*
2939bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
2940bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
2941bec4fa05SStephen Warren 	 * snd_soc_register_card.
2942bec4fa05SStephen Warren 	 */
2943bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
2944bec4fa05SStephen Warren 		dev_err(card->dev,
2945f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
2946bec4fa05SStephen Warren 			propname, ret);
2947bec4fa05SStephen Warren 		return ret;
2948bec4fa05SStephen Warren 	}
2949bec4fa05SStephen Warren 
2950bec4fa05SStephen Warren 	return 0;
2951bec4fa05SStephen Warren }
2952b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2953bec4fa05SStephen Warren 
29549a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
29559a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
29569a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
29579a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
29589a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
29599a6d4860SXiubo Li };
29609a6d4860SXiubo Li 
296121efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
29629a6d4860SXiubo Li 					  const char *propname)
29639a6d4860SXiubo Li {
296421efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
29659a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
29669a6d4860SXiubo Li 	const char *template, *wname;
29679a6d4860SXiubo Li 	int i, j, num_widgets, ret;
29689a6d4860SXiubo Li 
29699a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
29709a6d4860SXiubo Li 	if (num_widgets < 0) {
29719a6d4860SXiubo Li 		dev_err(card->dev,
29729a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
29739a6d4860SXiubo Li 		return -EINVAL;
29749a6d4860SXiubo Li 	}
29759a6d4860SXiubo Li 	if (num_widgets & 1) {
29769a6d4860SXiubo Li 		dev_err(card->dev,
29779a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
29789a6d4860SXiubo Li 		return -EINVAL;
29799a6d4860SXiubo Li 	}
29809a6d4860SXiubo Li 
29819a6d4860SXiubo Li 	num_widgets /= 2;
29829a6d4860SXiubo Li 	if (!num_widgets) {
29839a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
29849a6d4860SXiubo Li 			propname);
29859a6d4860SXiubo Li 		return -EINVAL;
29869a6d4860SXiubo Li 	}
29879a6d4860SXiubo Li 
29889a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
29899a6d4860SXiubo Li 			       GFP_KERNEL);
29909a6d4860SXiubo Li 	if (!widgets) {
29919a6d4860SXiubo Li 		dev_err(card->dev,
29929a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
29939a6d4860SXiubo Li 		return -ENOMEM;
29949a6d4860SXiubo Li 	}
29959a6d4860SXiubo Li 
29969a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
29979a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
29989a6d4860SXiubo Li 			2 * i, &template);
29999a6d4860SXiubo Li 		if (ret) {
30009a6d4860SXiubo Li 			dev_err(card->dev,
30019a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
30029a6d4860SXiubo Li 				propname, 2 * i, ret);
30039a6d4860SXiubo Li 			return -EINVAL;
30049a6d4860SXiubo Li 		}
30059a6d4860SXiubo Li 
30069a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
30079a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
30089a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
30099a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
30109a6d4860SXiubo Li 				break;
30119a6d4860SXiubo Li 			}
30129a6d4860SXiubo Li 		}
30139a6d4860SXiubo Li 
30149a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
30159a6d4860SXiubo Li 			dev_err(card->dev,
30169a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
30179a6d4860SXiubo Li 				template);
30189a6d4860SXiubo Li 			return -EINVAL;
30199a6d4860SXiubo Li 		}
30209a6d4860SXiubo Li 
30219a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
30229a6d4860SXiubo Li 						    (2 * i) + 1,
30239a6d4860SXiubo Li 						    &wname);
30249a6d4860SXiubo Li 		if (ret) {
30259a6d4860SXiubo Li 			dev_err(card->dev,
30269a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
30279a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
30289a6d4860SXiubo Li 			return -EINVAL;
30299a6d4860SXiubo Li 		}
30309a6d4860SXiubo Li 
30319a6d4860SXiubo Li 		widgets[i].name = wname;
30329a6d4860SXiubo Li 	}
30339a6d4860SXiubo Li 
3034f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
3035f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
30369a6d4860SXiubo Li 
30379a6d4860SXiubo Li 	return 0;
30389a6d4860SXiubo Li }
303921efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
30409a6d4860SXiubo Li 
3041cbdfab3bSJerome Brunet int snd_soc_of_get_slot_mask(struct device_node *np,
30426131084aSJyri Sarha 			     const char *prop_name,
30436131084aSJyri Sarha 			     unsigned int *mask)
30446131084aSJyri Sarha {
30456131084aSJyri Sarha 	u32 val;
30466c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
30476131084aSJyri Sarha 	int i;
30486131084aSJyri Sarha 
30496131084aSJyri Sarha 	if (!of_slot_mask)
30506131084aSJyri Sarha 		return 0;
30516131084aSJyri Sarha 	val /= sizeof(u32);
30526131084aSJyri Sarha 	for (i = 0; i < val; i++)
30536131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
30546131084aSJyri Sarha 			*mask |= (1 << i);
30556131084aSJyri Sarha 
30566131084aSJyri Sarha 	return val;
30576131084aSJyri Sarha }
3058cbdfab3bSJerome Brunet EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
30596131084aSJyri Sarha 
306089c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
30616131084aSJyri Sarha 			      unsigned int *tx_mask,
30626131084aSJyri Sarha 			      unsigned int *rx_mask,
306389c67857SXiubo Li 			      unsigned int *slots,
306489c67857SXiubo Li 			      unsigned int *slot_width)
306589c67857SXiubo Li {
306689c67857SXiubo Li 	u32 val;
306789c67857SXiubo Li 	int ret;
306889c67857SXiubo Li 
30696131084aSJyri Sarha 	if (tx_mask)
30706131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
30716131084aSJyri Sarha 	if (rx_mask)
30726131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
30736131084aSJyri Sarha 
307489c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
307589c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
307689c67857SXiubo Li 		if (ret)
307789c67857SXiubo Li 			return ret;
307889c67857SXiubo Li 
307989c67857SXiubo Li 		if (slots)
308089c67857SXiubo Li 			*slots = val;
308189c67857SXiubo Li 	}
308289c67857SXiubo Li 
308389c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
308489c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
308589c67857SXiubo Li 		if (ret)
308689c67857SXiubo Li 			return ret;
308789c67857SXiubo Li 
308889c67857SXiubo Li 		if (slot_width)
308989c67857SXiubo Li 			*slot_width = val;
309089c67857SXiubo Li 	}
309189c67857SXiubo Li 
309289c67857SXiubo Li 	return 0;
309389c67857SXiubo Li }
309489c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
309589c67857SXiubo Li 
30963b710356SKuninori Morimoto void snd_soc_of_parse_node_prefix(struct device_node *np,
30975e3cdaa2SKuninori Morimoto 				  struct snd_soc_codec_conf *codec_conf,
30985e3cdaa2SKuninori Morimoto 				  struct device_node *of_node,
30995e3cdaa2SKuninori Morimoto 				  const char *propname)
31005e3cdaa2SKuninori Morimoto {
31015e3cdaa2SKuninori Morimoto 	const char *str;
31025e3cdaa2SKuninori Morimoto 	int ret;
31035e3cdaa2SKuninori Morimoto 
31045e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
31055e3cdaa2SKuninori Morimoto 	if (ret < 0) {
31065e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
31075e3cdaa2SKuninori Morimoto 		return;
31085e3cdaa2SKuninori Morimoto 	}
31095e3cdaa2SKuninori Morimoto 
31105e3cdaa2SKuninori Morimoto 	codec_conf->of_node	= of_node;
31115e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
31125e3cdaa2SKuninori Morimoto }
31133b710356SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
31145e3cdaa2SKuninori Morimoto 
31152bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3116a4a54dd5SStephen Warren 				   const char *propname)
3117a4a54dd5SStephen Warren {
31182bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
3119e3b1e6a1SMark Brown 	int num_routes;
3120a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3121a4a54dd5SStephen Warren 	int i, ret;
3122a4a54dd5SStephen Warren 
3123a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3124c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
312510e8aa9aSMichał Mirosław 		dev_err(card->dev,
312610e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
312710e8aa9aSMichał Mirosław 			propname);
3128a4a54dd5SStephen Warren 		return -EINVAL;
3129a4a54dd5SStephen Warren 	}
3130a4a54dd5SStephen Warren 	num_routes /= 2;
3131a4a54dd5SStephen Warren 	if (!num_routes) {
3132f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3133a4a54dd5SStephen Warren 			propname);
3134a4a54dd5SStephen Warren 		return -EINVAL;
3135a4a54dd5SStephen Warren 	}
3136a4a54dd5SStephen Warren 
3137a86854d0SKees Cook 	routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
3138a4a54dd5SStephen Warren 			      GFP_KERNEL);
3139a4a54dd5SStephen Warren 	if (!routes) {
3140a4a54dd5SStephen Warren 		dev_err(card->dev,
3141f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3142a4a54dd5SStephen Warren 		return -EINVAL;
3143a4a54dd5SStephen Warren 	}
3144a4a54dd5SStephen Warren 
3145a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3146a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3147e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
3148a4a54dd5SStephen Warren 		if (ret) {
3149c871bd0bSMark Brown 			dev_err(card->dev,
3150c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3151c871bd0bSMark Brown 				propname, 2 * i, ret);
3152a4a54dd5SStephen Warren 			return -EINVAL;
3153a4a54dd5SStephen Warren 		}
3154a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3155e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3156a4a54dd5SStephen Warren 		if (ret) {
3157a4a54dd5SStephen Warren 			dev_err(card->dev,
3158c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3159c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3160a4a54dd5SStephen Warren 			return -EINVAL;
3161a4a54dd5SStephen Warren 		}
3162a4a54dd5SStephen Warren 	}
3163a4a54dd5SStephen Warren 
3164f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
3165f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
3166a4a54dd5SStephen Warren 
3167a4a54dd5SStephen Warren 	return 0;
3168a4a54dd5SStephen Warren }
31692bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3170a4a54dd5SStephen Warren 
3171a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3172389cb834SJyri Sarha 				     const char *prefix,
3173389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3174389cb834SJyri Sarha 				     struct device_node **framemaster)
3175a7930ed4SKuninori Morimoto {
3176a7930ed4SKuninori Morimoto 	int ret, i;
3177a7930ed4SKuninori Morimoto 	char prop[128];
3178a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3179a7930ed4SKuninori Morimoto 	int bit, frame;
3180a7930ed4SKuninori Morimoto 	const char *str;
3181a7930ed4SKuninori Morimoto 	struct {
3182a7930ed4SKuninori Morimoto 		char *name;
3183a7930ed4SKuninori Morimoto 		unsigned int val;
3184a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3185a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3186a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3187a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3188a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3189a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3190a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3191a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3192a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3193a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3194a7930ed4SKuninori Morimoto 	};
3195a7930ed4SKuninori Morimoto 
3196a7930ed4SKuninori Morimoto 	if (!prefix)
3197a7930ed4SKuninori Morimoto 		prefix = "";
3198a7930ed4SKuninori Morimoto 
3199a7930ed4SKuninori Morimoto 	/*
32005711c979SKuninori Morimoto 	 * check "dai-format = xxx"
32015711c979SKuninori Morimoto 	 * or    "[prefix]format = xxx"
3202a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3203a7930ed4SKuninori Morimoto 	 */
32045711c979SKuninori Morimoto 	ret = of_property_read_string(np, "dai-format", &str);
32055711c979SKuninori Morimoto 	if (ret < 0) {
3206a7930ed4SKuninori Morimoto 		snprintf(prop, sizeof(prop), "%sformat", prefix);
3207a7930ed4SKuninori Morimoto 		ret = of_property_read_string(np, prop, &str);
32085711c979SKuninori Morimoto 	}
3209a7930ed4SKuninori Morimoto 	if (ret == 0) {
3210a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3211a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3212a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3213a7930ed4SKuninori Morimoto 				break;
3214a7930ed4SKuninori Morimoto 			}
3215a7930ed4SKuninori Morimoto 		}
3216a7930ed4SKuninori Morimoto 	}
3217a7930ed4SKuninori Morimoto 
3218a7930ed4SKuninori Morimoto 	/*
32198c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3220a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3221a7930ed4SKuninori Morimoto 	 */
32228c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
322351930295SJulia Lawall 	if (of_property_read_bool(np, prop))
32248c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
32258c2d6a9fSKuninori Morimoto 	else
32268c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3227a7930ed4SKuninori Morimoto 
3228a7930ed4SKuninori Morimoto 	/*
3229a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3230a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3231a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3232a7930ed4SKuninori Morimoto 	 */
3233a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3234a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3235a7930ed4SKuninori Morimoto 
3236a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3237a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3238a7930ed4SKuninori Morimoto 
3239a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3240a7930ed4SKuninori Morimoto 	case 0x11:
3241a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3242a7930ed4SKuninori Morimoto 		break;
3243a7930ed4SKuninori Morimoto 	case 0x10:
3244a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3245a7930ed4SKuninori Morimoto 		break;
3246a7930ed4SKuninori Morimoto 	case 0x01:
3247a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3248a7930ed4SKuninori Morimoto 		break;
3249a7930ed4SKuninori Morimoto 	default:
3250a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3251a7930ed4SKuninori Morimoto 		break;
3252a7930ed4SKuninori Morimoto 	}
3253a7930ed4SKuninori Morimoto 
3254a7930ed4SKuninori Morimoto 	/*
3255a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3256a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3257a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3258a7930ed4SKuninori Morimoto 	 */
3259a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3260a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3261389cb834SJyri Sarha 	if (bit && bitclkmaster)
3262389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3263a7930ed4SKuninori Morimoto 
3264a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3265a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3266389cb834SJyri Sarha 	if (frame && framemaster)
3267389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3268a7930ed4SKuninori Morimoto 
3269a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3270a7930ed4SKuninori Morimoto 	case 0x11:
3271a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3272a7930ed4SKuninori Morimoto 		break;
3273a7930ed4SKuninori Morimoto 	case 0x10:
3274a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3275a7930ed4SKuninori Morimoto 		break;
3276a7930ed4SKuninori Morimoto 	case 0x01:
3277a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3278a7930ed4SKuninori Morimoto 		break;
3279a7930ed4SKuninori Morimoto 	default:
3280a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3281a7930ed4SKuninori Morimoto 		break;
3282a7930ed4SKuninori Morimoto 	}
3283a7930ed4SKuninori Morimoto 
3284a7930ed4SKuninori Morimoto 	return format;
3285a7930ed4SKuninori Morimoto }
3286a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3287a7930ed4SKuninori Morimoto 
3288a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
3289a180e8b9SKuninori Morimoto {
329009d4cc03SKuninori Morimoto 	struct snd_soc_component *component;
3291c1e230f0SKuninori Morimoto 	struct snd_soc_dai_link_component dlc;
3292a180e8b9SKuninori Morimoto 	int ret;
3293a180e8b9SKuninori Morimoto 
3294c1e230f0SKuninori Morimoto 	dlc.of_node	= of_graph_get_port_parent(ep);
3295c1e230f0SKuninori Morimoto 	dlc.name	= NULL;
3296a180e8b9SKuninori Morimoto 	/*
3297a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
3298a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
3299a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
3300a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
3301a180e8b9SKuninori Morimoto 	 */
3302a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
3303a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
3304c1e230f0SKuninori Morimoto 	component = soc_find_component(&dlc);
33052c7b1704SKuninori Morimoto 	if (component)
33062c7b1704SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_id(component, ep);
3307a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
3308a180e8b9SKuninori Morimoto 
3309c1e230f0SKuninori Morimoto 	of_node_put(dlc.of_node);
3310c0a480d1STony Lindgren 
3311a180e8b9SKuninori Morimoto 	return ret;
3312a180e8b9SKuninori Morimoto }
3313a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3314a180e8b9SKuninori Morimoto 
33151ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args,
3316cb470087SKuninori Morimoto 				const char **dai_name)
3317cb470087SKuninori Morimoto {
3318cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
33193e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
332093b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3321cb470087SKuninori Morimoto 
3322cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3323368dee94SKuninori Morimoto 	for_each_component(pos) {
3324c0834440SKuninori Morimoto 		component_of_node = soc_component_to_node(pos);
33253e0aa8d8SJyri Sarha 
33263e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
3327cb470087SKuninori Morimoto 			continue;
3328cb470087SKuninori Morimoto 
3329a2a34175SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3330a2a34175SKuninori Morimoto 		if (ret == -ENOTSUPP) {
333158bf4179SKuninori Morimoto 			struct snd_soc_dai *dai;
33326833c452SKuninori Morimoto 			int id = -1;
33336833c452SKuninori Morimoto 
333493b0f3eeSJean-Francois Moine 			switch (args->args_count) {
33356833c452SKuninori Morimoto 			case 0:
33366833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
33376833c452SKuninori Morimoto 				break;
33386833c452SKuninori Morimoto 			case 1:
333993b0f3eeSJean-Francois Moine 				id = args->args[0];
33406833c452SKuninori Morimoto 				break;
33416833c452SKuninori Morimoto 			default:
33426833c452SKuninori Morimoto 				/* not supported */
3343cb470087SKuninori Morimoto 				break;
3344cb470087SKuninori Morimoto 			}
3345cb470087SKuninori Morimoto 
33466833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
33476833c452SKuninori Morimoto 				ret = -EINVAL;
33483dcba280SNicolin Chen 				continue;
33496833c452SKuninori Morimoto 			}
3350e41975edSXiubo Li 
3351e41975edSXiubo Li 			ret = 0;
3352e41975edSXiubo Li 
335358bf4179SKuninori Morimoto 			/* find target DAI */
335415a0c645SKuninori Morimoto 			for_each_component_dais(pos, dai) {
335558bf4179SKuninori Morimoto 				if (id == 0)
335658bf4179SKuninori Morimoto 					break;
335758bf4179SKuninori Morimoto 				id--;
335858bf4179SKuninori Morimoto 			}
335958bf4179SKuninori Morimoto 
336058bf4179SKuninori Morimoto 			*dai_name = dai->driver->name;
3361e41975edSXiubo Li 			if (!*dai_name)
3362e41975edSXiubo Li 				*dai_name = pos->name;
33636833c452SKuninori Morimoto 		}
33646833c452SKuninori Morimoto 
3365cb470087SKuninori Morimoto 		break;
3366cb470087SKuninori Morimoto 	}
3367cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
336893b0f3eeSJean-Francois Moine 	return ret;
336993b0f3eeSJean-Francois Moine }
33701ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
337193b0f3eeSJean-Francois Moine 
337293b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
337393b0f3eeSJean-Francois Moine 			    const char **dai_name)
337493b0f3eeSJean-Francois Moine {
337593b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
337693b0f3eeSJean-Francois Moine 	int ret;
337793b0f3eeSJean-Francois Moine 
337893b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
337993b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
338093b0f3eeSJean-Francois Moine 	if (ret)
338193b0f3eeSJean-Francois Moine 		return ret;
338293b0f3eeSJean-Francois Moine 
338393b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3384cb470087SKuninori Morimoto 
3385cb470087SKuninori Morimoto 	of_node_put(args.np);
3386cb470087SKuninori Morimoto 
3387cb470087SKuninori Morimoto 	return ret;
3388cb470087SKuninori Morimoto }
3389cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3390cb470087SKuninori Morimoto 
339193b0f3eeSJean-Francois Moine /*
339294685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
339394685763SSylwester Nawrocki  * @dai_link: DAI link
339494685763SSylwester Nawrocki  *
339594685763SSylwester Nawrocki  * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
339694685763SSylwester Nawrocki  */
339794685763SSylwester Nawrocki void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
339894685763SSylwester Nawrocki {
33993db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *component;
340094685763SSylwester Nawrocki 	int index;
340194685763SSylwester Nawrocki 
34023db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
340394685763SSylwester Nawrocki 		if (!component->of_node)
340494685763SSylwester Nawrocki 			break;
340594685763SSylwester Nawrocki 		of_node_put(component->of_node);
340694685763SSylwester Nawrocki 		component->of_node = NULL;
340794685763SSylwester Nawrocki 	}
340894685763SSylwester Nawrocki }
340994685763SSylwester Nawrocki EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
341094685763SSylwester Nawrocki 
341194685763SSylwester Nawrocki /*
341293b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
341393b0f3eeSJean-Francois Moine  * @dev: Card device
341493b0f3eeSJean-Francois Moine  * @of_node: Device node
341593b0f3eeSJean-Francois Moine  * @dai_link: DAI link
341693b0f3eeSJean-Francois Moine  *
341793b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
341893b0f3eeSJean-Francois Moine  * 'sound-dai'.
341993b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
342094685763SSylwester Nawrocki  * The device nodes in the array (of_node) must be dereferenced by calling
342194685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs() on @dai_link.
342293b0f3eeSJean-Francois Moine  *
342393b0f3eeSJean-Francois Moine  * Returns 0 for success
342493b0f3eeSJean-Francois Moine  */
342593b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
342693b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
342793b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
342893b0f3eeSJean-Francois Moine {
342993b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
343093b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
343193b0f3eeSJean-Francois Moine 	char *name;
343293b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
343393b0f3eeSJean-Francois Moine 
343493b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
343593b0f3eeSJean-Francois Moine 	name = "sound-dai";
343693b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
343793b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
343893b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
343993b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
344093b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
344193b0f3eeSJean-Francois Moine 		else
344293b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
344393b0f3eeSJean-Francois Moine 		return num_codecs;
344493b0f3eeSJean-Francois Moine 	}
3445a86854d0SKees Cook 	component = devm_kcalloc(dev,
3446a86854d0SKees Cook 				 num_codecs, sizeof(*component),
344793b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
344893b0f3eeSJean-Francois Moine 	if (!component)
344993b0f3eeSJean-Francois Moine 		return -ENOMEM;
345093b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
345193b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
345293b0f3eeSJean-Francois Moine 
345393b0f3eeSJean-Francois Moine 	/* Parse the list */
34543db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
345593b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
345693b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
345793b0f3eeSJean-Francois Moine 						 index, &args);
345893b0f3eeSJean-Francois Moine 		if (ret)
345993b0f3eeSJean-Francois Moine 			goto err;
346093b0f3eeSJean-Francois Moine 		component->of_node = args.np;
346193b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
346293b0f3eeSJean-Francois Moine 		if (ret < 0)
346393b0f3eeSJean-Francois Moine 			goto err;
346493b0f3eeSJean-Francois Moine 	}
346593b0f3eeSJean-Francois Moine 	return 0;
346693b0f3eeSJean-Francois Moine err:
346794685763SSylwester Nawrocki 	snd_soc_of_put_dai_link_codecs(dai_link);
346893b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
346993b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
347093b0f3eeSJean-Francois Moine 	return ret;
347193b0f3eeSJean-Francois Moine }
347293b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
347393b0f3eeSJean-Francois Moine 
3474c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3475db2a4165SFrank Mandarino {
34766553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
3477fb257897SMark Brown 	snd_soc_util_init();
3478fb257897SMark Brown 
3479db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3480db2a4165SFrank Mandarino }
34814abe8e16SMark Brown module_init(snd_soc_init);
3482db2a4165SFrank Mandarino 
34837d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3484db2a4165SFrank Mandarino {
3485fb257897SMark Brown 	snd_soc_util_exit();
34866553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3487fb257897SMark Brown 
3488db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3489db2a4165SFrank Mandarino }
3490db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3491db2a4165SFrank Mandarino 
3492db2a4165SFrank Mandarino /* Module information */
3493d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3494db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3495db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
34968b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3497