xref: /openbmc/linux/sound/soc/soc-core.c (revision c2b71c71037bea7765aa6ff37824520d19108769)
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 
762c7b696aSMarcel Ziswiler /*
772c7b696aSMarcel Ziswiler  * If a DMI filed contain strings in this blacklist (e.g.
7898faf436SMengdong Lin  * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
7998faf436SMengdong Lin  * as invalid and dropped when setting the card long name from DMI info.
8098faf436SMengdong Lin  */
8198faf436SMengdong Lin static const char * const dmi_blacklist[] = {
8298faf436SMengdong Lin 	"To be filled by OEM",
8398faf436SMengdong Lin 	"TBD by OEM",
8498faf436SMengdong Lin 	"Default String",
8598faf436SMengdong Lin 	"Board Manufacturer",
8698faf436SMengdong Lin 	"Board Vendor Name",
8798faf436SMengdong Lin 	"Board Product Name",
8898faf436SMengdong Lin 	NULL,	/* terminator */
8998faf436SMengdong Lin };
9098faf436SMengdong Lin 
91dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
92dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
93dbe21408SMark Brown {
9436ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
95dbe21408SMark Brown 
96f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
97dbe21408SMark Brown }
98dbe21408SMark Brown 
99dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
100dbe21408SMark Brown 			       struct device_attribute *attr,
101dbe21408SMark Brown 			       const char *buf, size_t count)
102dbe21408SMark Brown {
10336ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
104c593b520SMark Brown 	int ret;
105dbe21408SMark Brown 
106b785a492SJingoo Han 	ret = kstrtol(buf, 10, &rtd->pmdown_time);
107c593b520SMark Brown 	if (ret)
108c593b520SMark Brown 		return ret;
109dbe21408SMark Brown 
110dbe21408SMark Brown 	return count;
111dbe21408SMark Brown }
112dbe21408SMark Brown 
113dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
114dbe21408SMark Brown 
115d29697dcSTakashi Iwai static struct attribute *soc_dev_attrs[] = {
116d29697dcSTakashi Iwai 	&dev_attr_pmdown_time.attr,
117d29697dcSTakashi Iwai 	NULL
118d29697dcSTakashi Iwai };
119d29697dcSTakashi Iwai 
120d29697dcSTakashi Iwai static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
121d29697dcSTakashi Iwai 				       struct attribute *attr, int idx)
122d29697dcSTakashi Iwai {
123d29697dcSTakashi Iwai 	struct device *dev = kobj_to_dev(kobj);
124d29697dcSTakashi Iwai 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
125d29697dcSTakashi Iwai 
126d29697dcSTakashi Iwai 	if (attr == &dev_attr_pmdown_time.attr)
127d29697dcSTakashi Iwai 		return attr->mode; /* always visible */
1283b6eed8dSKuninori Morimoto 	return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
129d29697dcSTakashi Iwai }
130d29697dcSTakashi Iwai 
131d29697dcSTakashi Iwai static const struct attribute_group soc_dapm_dev_group = {
132d29697dcSTakashi Iwai 	.attrs = soc_dapm_dev_attrs,
133d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
134d29697dcSTakashi Iwai };
135d29697dcSTakashi Iwai 
136f7e73b26SMark Brown static const struct attribute_group soc_dev_group = {
137d29697dcSTakashi Iwai 	.attrs = soc_dev_attrs,
138d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
139d29697dcSTakashi Iwai };
140d29697dcSTakashi Iwai 
141d29697dcSTakashi Iwai static const struct attribute_group *soc_dev_attr_groups[] = {
142d29697dcSTakashi Iwai 	&soc_dapm_dev_group,
143f7e73b26SMark Brown 	&soc_dev_group,
144d29697dcSTakashi Iwai 	NULL
145d29697dcSTakashi Iwai };
146d29697dcSTakashi Iwai 
1472624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
14881c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component)
149e73f3de5SRussell King {
1506553bf06SLars-Peter Clausen 	if (!component->card->debugfs_card_root)
1516553bf06SLars-Peter Clausen 		return;
1526553bf06SLars-Peter Clausen 
15381c7cfd1SLars-Peter Clausen 	if (component->debugfs_prefix) {
15481c7cfd1SLars-Peter Clausen 		char *name;
155e73f3de5SRussell King 
15681c7cfd1SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s:%s",
15781c7cfd1SLars-Peter Clausen 			component->debugfs_prefix, component->name);
15881c7cfd1SLars-Peter Clausen 		if (name) {
15981c7cfd1SLars-Peter Clausen 			component->debugfs_root = debugfs_create_dir(name,
16081c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
16181c7cfd1SLars-Peter Clausen 			kfree(name);
16281c7cfd1SLars-Peter Clausen 		}
16381c7cfd1SLars-Peter Clausen 	} else {
16481c7cfd1SLars-Peter Clausen 		component->debugfs_root = debugfs_create_dir(component->name,
16581c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
166e73f3de5SRussell King 	}
167e73f3de5SRussell King 
16881c7cfd1SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
16981c7cfd1SLars-Peter Clausen 		component->debugfs_root);
17081c7cfd1SLars-Peter Clausen }
17181c7cfd1SLars-Peter Clausen 
17281c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
17381c7cfd1SLars-Peter Clausen {
174ad64bfbdSKuninori Morimoto 	if (!component->debugfs_root)
175ad64bfbdSKuninori Morimoto 		return;
17681c7cfd1SLars-Peter Clausen 	debugfs_remove_recursive(component->debugfs_root);
177ad64bfbdSKuninori Morimoto 	component->debugfs_root = NULL;
17881c7cfd1SLars-Peter Clausen }
17981c7cfd1SLars-Peter Clausen 
180c15b2a1dSPeng Donglin static int dai_list_show(struct seq_file *m, void *v)
181f3208780SMark Brown {
1821438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
183f3208780SMark Brown 	struct snd_soc_dai *dai;
184f3208780SMark Brown 
18534e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
18634e81ab4SLars-Peter Clausen 
187368dee94SKuninori Morimoto 	for_each_component(component)
18815a0c645SKuninori Morimoto 		for_each_component_dais(component, dai)
189700c17caSDonglin Peng 			seq_printf(m, "%s\n", dai->name);
190f3208780SMark Brown 
19134e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
19234e81ab4SLars-Peter Clausen 
193700c17caSDonglin Peng 	return 0;
194700c17caSDonglin Peng }
195c15b2a1dSPeng Donglin DEFINE_SHOW_ATTRIBUTE(dai_list);
196f3208780SMark Brown 
197db795f9bSKuninori Morimoto static int component_list_show(struct seq_file *m, void *v)
198db795f9bSKuninori Morimoto {
199db795f9bSKuninori Morimoto 	struct snd_soc_component *component;
200db795f9bSKuninori Morimoto 
201db795f9bSKuninori Morimoto 	mutex_lock(&client_mutex);
202db795f9bSKuninori Morimoto 
203368dee94SKuninori Morimoto 	for_each_component(component)
204db795f9bSKuninori Morimoto 		seq_printf(m, "%s\n", component->name);
205db795f9bSKuninori Morimoto 
206db795f9bSKuninori Morimoto 	mutex_unlock(&client_mutex);
207db795f9bSKuninori Morimoto 
208db795f9bSKuninori Morimoto 	return 0;
209db795f9bSKuninori Morimoto }
210db795f9bSKuninori Morimoto DEFINE_SHOW_ATTRIBUTE(component_list);
211db795f9bSKuninori Morimoto 
212a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
213a6052154SJarkko Nikula {
214a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
2158a9dab1aSMark Brown 						     snd_soc_debugfs_root);
2163a45b867SJarkko Nikula 
217fee531d6SGreg Kroah-Hartman 	debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root,
2183a45b867SJarkko Nikula 			   &card->pop_time);
219d8ca7a0aSKuninori Morimoto 
220d8ca7a0aSKuninori Morimoto 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
221a6052154SJarkko Nikula }
222a6052154SJarkko Nikula 
223a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
224a6052154SJarkko Nikula {
225a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
22629040d1aSKuninori Morimoto 	card->debugfs_card_root = NULL;
227a6052154SJarkko Nikula }
228a6052154SJarkko Nikula 
2296553bf06SLars-Peter Clausen static void snd_soc_debugfs_init(void)
2306553bf06SLars-Peter Clausen {
2316553bf06SLars-Peter Clausen 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
2326553bf06SLars-Peter Clausen 
233fee531d6SGreg Kroah-Hartman 	debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
234fee531d6SGreg Kroah-Hartman 			    &dai_list_fops);
235db795f9bSKuninori Morimoto 
236fee531d6SGreg Kroah-Hartman 	debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
237fee531d6SGreg Kroah-Hartman 			    &component_list_fops);
2386553bf06SLars-Peter Clausen }
2396553bf06SLars-Peter Clausen 
2406553bf06SLars-Peter Clausen static void snd_soc_debugfs_exit(void)
2416553bf06SLars-Peter Clausen {
2426553bf06SLars-Peter Clausen 	debugfs_remove_recursive(snd_soc_debugfs_root);
2436553bf06SLars-Peter Clausen }
2446553bf06SLars-Peter Clausen 
2452624d5faSMark Brown #else
2462624d5faSMark Brown 
24781c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs(
24881c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
2492624d5faSMark Brown {
2502624d5faSMark Brown }
2512624d5faSMark Brown 
25281c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs(
25381c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
254731f1ab2SSebastien Guiriec {
255731f1ab2SSebastien Guiriec }
256731f1ab2SSebastien Guiriec 
257b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
258b95fccbcSAxel Lin {
259b95fccbcSAxel Lin }
260b95fccbcSAxel Lin 
261b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
262b95fccbcSAxel Lin {
263b95fccbcSAxel Lin }
2646553bf06SLars-Peter Clausen 
2656553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_init(void)
2666553bf06SLars-Peter Clausen {
2676553bf06SLars-Peter Clausen }
2686553bf06SLars-Peter Clausen 
2696553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_exit(void)
2706553bf06SLars-Peter Clausen {
2716553bf06SLars-Peter Clausen }
2726553bf06SLars-Peter Clausen 
2732624d5faSMark Brown #endif
2742624d5faSMark Brown 
275a0ac4411SKuninori Morimoto static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
276a0ac4411SKuninori Morimoto 			      struct snd_soc_component *component)
277a0ac4411SKuninori Morimoto {
278a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
279a0ac4411SKuninori Morimoto 
280a0ac4411SKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
281a0ac4411SKuninori Morimoto 		/* already connected */
282a0ac4411SKuninori Morimoto 		if (rtdcom->component == component)
283a0ac4411SKuninori Morimoto 			return 0;
284a0ac4411SKuninori Morimoto 	}
285a0ac4411SKuninori Morimoto 
28632d2c172SKuninori Morimoto 	rtdcom = kmalloc(sizeof(*rtdcom), GFP_KERNEL);
28732d2c172SKuninori Morimoto 	if (!rtdcom)
288a0ac4411SKuninori Morimoto 		return -ENOMEM;
289a0ac4411SKuninori Morimoto 
29032d2c172SKuninori Morimoto 	rtdcom->component = component;
29132d2c172SKuninori Morimoto 	INIT_LIST_HEAD(&rtdcom->list);
292a0ac4411SKuninori Morimoto 
29332d2c172SKuninori Morimoto 	list_add_tail(&rtdcom->list, &rtd->component_list);
294a0ac4411SKuninori Morimoto 
295a0ac4411SKuninori Morimoto 	return 0;
296a0ac4411SKuninori Morimoto }
297a0ac4411SKuninori Morimoto 
298a0ac4411SKuninori Morimoto static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
299a0ac4411SKuninori Morimoto {
300a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
301a0ac4411SKuninori Morimoto 
302a0ac4411SKuninori Morimoto 	for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
303a0ac4411SKuninori Morimoto 		kfree(rtdcom1);
304a0ac4411SKuninori Morimoto 
305a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
306a0ac4411SKuninori Morimoto }
307a0ac4411SKuninori Morimoto 
308a0ac4411SKuninori Morimoto struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
309a0ac4411SKuninori Morimoto 						const char *driver_name)
310a0ac4411SKuninori Morimoto {
311a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
312a0ac4411SKuninori Morimoto 
313971da24cSKuninori Morimoto 	if (!driver_name)
314971da24cSKuninori Morimoto 		return NULL;
315971da24cSKuninori Morimoto 
316a0ac4411SKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
317971da24cSKuninori Morimoto 		const char *component_name = rtdcom->component->driver->name;
318971da24cSKuninori Morimoto 
319971da24cSKuninori Morimoto 		if (!component_name)
320971da24cSKuninori Morimoto 			continue;
321971da24cSKuninori Morimoto 
322971da24cSKuninori Morimoto 		if ((component_name == driver_name) ||
323971da24cSKuninori Morimoto 		    strcmp(component_name, driver_name) == 0)
324a0ac4411SKuninori Morimoto 			return rtdcom->component;
325a0ac4411SKuninori Morimoto 	}
326a0ac4411SKuninori Morimoto 
327a0ac4411SKuninori Morimoto 	return NULL;
328a0ac4411SKuninori Morimoto }
329031734b7SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
330a0ac4411SKuninori Morimoto 
33147c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
33247c88fffSLiam Girdwood 		const char *dai_link, int stream)
33347c88fffSLiam Girdwood {
3341a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
33547c88fffSLiam Girdwood 
336bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
3371a497983SMengdong Lin 		if (rtd->dai_link->no_pcm &&
3381a497983SMengdong Lin 			!strcmp(rtd->dai_link->name, dai_link))
3391a497983SMengdong Lin 			return rtd->pcm->streams[stream].substream;
34047c88fffSLiam Girdwood 	}
341f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
34247c88fffSLiam Girdwood 	return NULL;
34347c88fffSLiam Girdwood }
34447c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
34547c88fffSLiam Girdwood 
34675ab9eb6SKuninori Morimoto static const struct snd_soc_ops null_snd_soc_ops;
34775ab9eb6SKuninori Morimoto 
3481a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
3491a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
3501a497983SMengdong Lin {
3511a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
3521a497983SMengdong Lin 
3531a497983SMengdong Lin 	rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
3541a497983SMengdong Lin 	if (!rtd)
3551a497983SMengdong Lin 		return NULL;
3561a497983SMengdong Lin 
357a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
3581a497983SMengdong Lin 	rtd->card = card;
3591a497983SMengdong Lin 	rtd->dai_link = dai_link;
36075ab9eb6SKuninori Morimoto 	if (!rtd->dai_link->ops)
36175ab9eb6SKuninori Morimoto 		rtd->dai_link->ops = &null_snd_soc_ops;
36275ab9eb6SKuninori Morimoto 
3636396bb22SKees Cook 	rtd->codec_dais = kcalloc(dai_link->num_codecs,
3646396bb22SKees Cook 					sizeof(struct snd_soc_dai *),
3651a497983SMengdong Lin 					GFP_KERNEL);
3661a497983SMengdong Lin 	if (!rtd->codec_dais) {
3671a497983SMengdong Lin 		kfree(rtd);
3681a497983SMengdong Lin 		return NULL;
3691a497983SMengdong Lin 	}
3701a497983SMengdong Lin 
3711a497983SMengdong Lin 	return rtd;
3721a497983SMengdong Lin }
3731a497983SMengdong Lin 
3741a497983SMengdong Lin static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
3751a497983SMengdong Lin {
3761a497983SMengdong Lin 	kfree(rtd->codec_dais);
377a0ac4411SKuninori Morimoto 	snd_soc_rtdcom_del_all(rtd);
3781a497983SMengdong Lin 	kfree(rtd);
3791a497983SMengdong Lin }
3801a497983SMengdong Lin 
3811a497983SMengdong Lin static void soc_add_pcm_runtime(struct snd_soc_card *card,
3821a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd)
3831a497983SMengdong Lin {
3846634e3d6SKuninori Morimoto 	/* see for_each_card_rtds */
3851a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
3861a497983SMengdong Lin 	rtd->num = card->num_rtd;
3871a497983SMengdong Lin 	card->num_rtd++;
3881a497983SMengdong Lin }
3891a497983SMengdong Lin 
3901a497983SMengdong Lin static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
3911a497983SMengdong Lin {
3921a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, *_rtd;
3931a497983SMengdong Lin 
394bcb1fd1fSKuninori Morimoto 	for_each_card_rtds_safe(card, rtd, _rtd) {
3951a497983SMengdong Lin 		list_del(&rtd->list);
3961a497983SMengdong Lin 		soc_free_pcm_runtime(rtd);
3971a497983SMengdong Lin 	}
3981a497983SMengdong Lin 
3991a497983SMengdong Lin 	card->num_rtd = 0;
4001a497983SMengdong Lin }
4011a497983SMengdong Lin 
40247c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
40347c88fffSLiam Girdwood 		const char *dai_link)
40447c88fffSLiam Girdwood {
4051a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
40647c88fffSLiam Girdwood 
407bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4081a497983SMengdong Lin 		if (!strcmp(rtd->dai_link->name, dai_link))
4091a497983SMengdong Lin 			return rtd;
41047c88fffSLiam Girdwood 	}
411f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
41247c88fffSLiam Girdwood 	return NULL;
41347c88fffSLiam Girdwood }
41447c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
41547c88fffSLiam Girdwood 
41665462e44SKuninori Morimoto static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
41765462e44SKuninori Morimoto {
41865462e44SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
41965462e44SKuninori Morimoto 
42065462e44SKuninori Morimoto 	for_each_card_rtds(card, rtd)
42165462e44SKuninori Morimoto 		flush_delayed_work(&rtd->delayed_work);
42265462e44SKuninori Morimoto }
42365462e44SKuninori Morimoto 
4246f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
425db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
4266f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
427db2a4165SFrank Mandarino {
4286f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
429d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
4301a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
4311a497983SMengdong Lin 	int i;
432db2a4165SFrank Mandarino 
433c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
434c5599b87SLars-Peter Clausen 	if (!card->instantiated)
435e3509ff0SDaniel Mack 		return 0;
436e3509ff0SDaniel Mack 
4372c7b696aSMarcel Ziswiler 	/*
4382c7b696aSMarcel Ziswiler 	 * Due to the resume being scheduled into a workqueue we could
4396ed25978SAndy Green 	 * suspend before that's finished - wait for it to complete.
4406ed25978SAndy Green 	 */
441f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
4426ed25978SAndy Green 
4436ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
444f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
4456ed25978SAndy Green 
446a00f90f9SMark Brown 	/* mute any active DACs */
447bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4480b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
4493efab7dcSMark Brown 
4501a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4513efab7dcSMark Brown 			continue;
4523efab7dcSMark Brown 
4530b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
45488fdffa2SKuninori Morimoto 			if (dai->playback_active)
45588fdffa2SKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 1,
45688fdffa2SKuninori Morimoto 						SNDRV_PCM_STREAM_PLAYBACK);
457db2a4165SFrank Mandarino 		}
45888bd870fSBenoit Cousson 	}
459db2a4165SFrank Mandarino 
4604ccab3e7SLiam Girdwood 	/* suspend all pcms */
461bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4621a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4633efab7dcSMark Brown 			continue;
4643efab7dcSMark Brown 
4651a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
4663efab7dcSMark Brown 	}
4674ccab3e7SLiam Girdwood 
46887506549SMark Brown 	if (card->suspend_pre)
46970b2ac12SMark Brown 		card->suspend_pre(card);
470db2a4165SFrank Mandarino 
471bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4721a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
4733efab7dcSMark Brown 
4741a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4753efab7dcSMark Brown 			continue;
4763efab7dcSMark Brown 
477e0f22622SKuninori Morimoto 		if (!cpu_dai->driver->bus_control)
478e0f22622SKuninori Morimoto 			snd_soc_dai_suspend(cpu_dai);
479db2a4165SFrank Mandarino 	}
480db2a4165SFrank Mandarino 
48137660b6dSLars-Peter Clausen 	/* close any waiting streams */
48265462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
483db2a4165SFrank Mandarino 
484bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4853efab7dcSMark Brown 
4861a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4873efab7dcSMark Brown 			continue;
4883efab7dcSMark Brown 
4891a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
4907bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
491db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
492f0fba2adSLiam Girdwood 
4931a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
4947bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
495db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
496db2a4165SFrank Mandarino 	}
497db2a4165SFrank Mandarino 
4988be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
4998be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
500e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
501e2d32ff6SMark Brown 
5029178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
503f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
5042c7b696aSMarcel Ziswiler 		struct snd_soc_dapm_context *dapm =
5052c7b696aSMarcel Ziswiler 				snd_soc_component_get_dapm(component);
506d9fc4063SKuninori Morimoto 
5072c7b696aSMarcel Ziswiler 		/*
5082c7b696aSMarcel Ziswiler 		 * If there are paths active then the COMPONENT will be held
5092c7b696aSMarcel Ziswiler 		 * with bias _ON and should not be suspended.
5102c7b696aSMarcel Ziswiler 		 */
511e40fadbcSKuninori Morimoto 		if (!snd_soc_component_is_suspended(component)) {
5124890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
5131547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
514125a25daSMark Brown 				/*
5159178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
516125a25daSMark Brown 				 * bias off then being in STANDBY
517125a25daSMark Brown 				 * means it's doing something,
518125a25daSMark Brown 				 * otherwise fall through.
519125a25daSMark Brown 				 */
5204890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
5219178feb4SKuninori Morimoto 					dev_dbg(component->dev,
52210e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
523125a25daSMark Brown 					break;
524125a25daSMark Brown 				}
5251a12d5dcSGustavo A. R. Silva 				/* fall through */
526a8093297SLars-Peter Clausen 
5271547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
52866c51573SKuninori Morimoto 				snd_soc_component_suspend(component);
5299178feb4SKuninori Morimoto 				if (component->regmap)
5309178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
531988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
5329178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
5331547aba9SMark Brown 				break;
5341547aba9SMark Brown 			default:
5359178feb4SKuninori Morimoto 				dev_dbg(component->dev,
5369178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
5371547aba9SMark Brown 				break;
5381547aba9SMark Brown 			}
5391547aba9SMark Brown 		}
540f0fba2adSLiam Girdwood 	}
541db2a4165SFrank Mandarino 
542bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5431a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
5443efab7dcSMark Brown 
5451a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5463efab7dcSMark Brown 			continue;
5473efab7dcSMark Brown 
548e0f22622SKuninori Morimoto 		if (cpu_dai->driver->bus_control)
549e0f22622SKuninori Morimoto 			snd_soc_dai_suspend(cpu_dai);
550988e8cc4SNicolin Chen 
551988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
552988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
553db2a4165SFrank Mandarino 	}
554db2a4165SFrank Mandarino 
55587506549SMark Brown 	if (card->suspend_post)
55670b2ac12SMark Brown 		card->suspend_post(card);
557db2a4165SFrank Mandarino 
558db2a4165SFrank Mandarino 	return 0;
559db2a4165SFrank Mandarino }
5606f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
561db2a4165SFrank Mandarino 
5622c7b696aSMarcel Ziswiler /*
5632c7b696aSMarcel Ziswiler  * deferred resume work, so resume can complete before we finished
5646ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
5656ed25978SAndy Green  */
5666ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
567db2a4165SFrank Mandarino {
568f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
5692c7b696aSMarcel Ziswiler 			container_of(work, struct snd_soc_card,
5702c7b696aSMarcel Ziswiler 				     deferred_resume_work);
5711a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
572d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
5731a497983SMengdong Lin 	int i;
574db2a4165SFrank Mandarino 
5752c7b696aSMarcel Ziswiler 	/*
5762c7b696aSMarcel Ziswiler 	 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
5776ed25978SAndy Green 	 * so userspace apps are blocked from touching us
5786ed25978SAndy Green 	 */
5796ed25978SAndy Green 
580f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
5816ed25978SAndy Green 
5829949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
583f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
5849949788bSMark Brown 
58587506549SMark Brown 	if (card->resume_pre)
58670b2ac12SMark Brown 		card->resume_pre(card);
587db2a4165SFrank Mandarino 
588bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
589bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5901a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
5913efab7dcSMark Brown 
5921a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5933efab7dcSMark Brown 			continue;
5943efab7dcSMark Brown 
59524b09d05SKuninori Morimoto 		if (cpu_dai->driver->bus_control)
59624b09d05SKuninori Morimoto 			snd_soc_dai_resume(cpu_dai);
597db2a4165SFrank Mandarino 	}
598db2a4165SFrank Mandarino 
599f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
600e40fadbcSKuninori Morimoto 		if (snd_soc_component_is_suspended(component))
6019a840cbaSKuninori Morimoto 			snd_soc_component_resume(component);
6021547aba9SMark Brown 	}
603db2a4165SFrank Mandarino 
604bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6053efab7dcSMark Brown 
6061a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6073efab7dcSMark Brown 			continue;
6083efab7dcSMark Brown 
6091a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
610d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
611db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
612f0fba2adSLiam Girdwood 
6131a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
614d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
615db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
616db2a4165SFrank Mandarino 	}
617db2a4165SFrank Mandarino 
6183ff3f64bSMark Brown 	/* unmute any active DACs */
619bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6200b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
6213efab7dcSMark Brown 
6221a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6233efab7dcSMark Brown 			continue;
6243efab7dcSMark Brown 
6250b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
62688fdffa2SKuninori Morimoto 			if (dai->playback_active)
62788fdffa2SKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 0,
62888fdffa2SKuninori Morimoto 						SNDRV_PCM_STREAM_PLAYBACK);
629db2a4165SFrank Mandarino 		}
63088bd870fSBenoit Cousson 	}
631db2a4165SFrank Mandarino 
632bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6331a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6343efab7dcSMark Brown 
6351a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6363efab7dcSMark Brown 			continue;
6373efab7dcSMark Brown 
63824b09d05SKuninori Morimoto 		if (!cpu_dai->driver->bus_control)
63924b09d05SKuninori Morimoto 			snd_soc_dai_resume(cpu_dai);
640db2a4165SFrank Mandarino 	}
641db2a4165SFrank Mandarino 
64287506549SMark Brown 	if (card->resume_post)
64370b2ac12SMark Brown 		card->resume_post(card);
644db2a4165SFrank Mandarino 
645f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
6466ed25978SAndy Green 
6478be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
6488be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
649e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
6501a7aaa58SJeeja KP 
6511a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
6521a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
6536ed25978SAndy Green }
6546ed25978SAndy Green 
6556ed25978SAndy Green /* powers up audio subsystem after a suspend */
6566f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
6576ed25978SAndy Green {
6586f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
659bc263214SLars-Peter Clausen 	bool bus_control = false;
6601a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
66122d251a5SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
66222d251a5SKuninori Morimoto 	int i;
663b9dd94a8SPeter Ujfalusi 
664c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
665c5599b87SLars-Peter Clausen 	if (!card->instantiated)
6665ff1ddf2SEric Miao 		return 0;
6675ff1ddf2SEric Miao 
668988e8cc4SNicolin Chen 	/* activate pins from sleep state */
669bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
67088bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
67188bd870fSBenoit Cousson 
672988e8cc4SNicolin Chen 		if (cpu_dai->active)
673988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
67488bd870fSBenoit Cousson 
67522d251a5SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
676988e8cc4SNicolin Chen 			if (codec_dai->active)
677988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
678988e8cc4SNicolin Chen 		}
67988bd870fSBenoit Cousson 	}
680988e8cc4SNicolin Chen 
681bc263214SLars-Peter Clausen 	/*
682bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
683bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
684bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
68564ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
68664ab9baaSMark Brown 	 */
687bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6881a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6892c7b696aSMarcel Ziswiler 
690bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
69182e14e8bSStephen Warren 	}
692bc263214SLars-Peter Clausen 	if (bus_control) {
693bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
69464ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
69564ab9baaSMark Brown 	} else {
696f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
6976308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
698f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
699f0fba2adSLiam Girdwood 	}
7006ed25978SAndy Green 
701db2a4165SFrank Mandarino 	return 0;
702db2a4165SFrank Mandarino }
7036f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
704b3da4251SKuninori Morimoto 
705b3da4251SKuninori Morimoto static void soc_resume_init(struct snd_soc_card *card)
706b3da4251SKuninori Morimoto {
707b3da4251SKuninori Morimoto 	/* deferred resume work */
708b3da4251SKuninori Morimoto 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
709b3da4251SKuninori Morimoto }
710db2a4165SFrank Mandarino #else
7116f8ab4acSMark Brown #define snd_soc_suspend NULL
7126f8ab4acSMark Brown #define snd_soc_resume NULL
713b3da4251SKuninori Morimoto static inline void soc_resume_init(struct snd_soc_card *card)
714b3da4251SKuninori Morimoto {
715b3da4251SKuninori Morimoto }
716db2a4165SFrank Mandarino #endif
717db2a4165SFrank Mandarino 
71885e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
71902a06d30SBarry Song };
72002a06d30SBarry Song 
721c0834440SKuninori Morimoto static struct device_node
722c0834440SKuninori Morimoto *soc_component_to_node(struct snd_soc_component *component)
72312023a9aSMisael Lopez Cruz {
724c0834440SKuninori Morimoto 	struct device_node *of_node;
72512023a9aSMisael Lopez Cruz 
726c0834440SKuninori Morimoto 	of_node = component->dev->of_node;
727c0834440SKuninori Morimoto 	if (!of_node && component->dev->parent)
728c0834440SKuninori Morimoto 		of_node = component->dev->parent->of_node;
72934e81ab4SLars-Peter Clausen 
730c0834440SKuninori Morimoto 	return of_node;
73112023a9aSMisael Lopez Cruz }
73212023a9aSMisael Lopez Cruz 
733be6ac0a9SKuninori Morimoto static int snd_soc_is_matching_component(
734be6ac0a9SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc,
735be6ac0a9SKuninori Morimoto 	struct snd_soc_component *component)
736be6ac0a9SKuninori Morimoto {
737be6ac0a9SKuninori Morimoto 	struct device_node *component_of_node;
738be6ac0a9SKuninori Morimoto 
7397d7db5d3SKuninori Morimoto 	if (!dlc)
7407d7db5d3SKuninori Morimoto 		return 0;
7417d7db5d3SKuninori Morimoto 
7427d7db5d3SKuninori Morimoto 	component_of_node = soc_component_to_node(component);
743be6ac0a9SKuninori Morimoto 
744be6ac0a9SKuninori Morimoto 	if (dlc->of_node && component_of_node != dlc->of_node)
745be6ac0a9SKuninori Morimoto 		return 0;
746be6ac0a9SKuninori Morimoto 	if (dlc->name && strcmp(component->name, dlc->name))
747be6ac0a9SKuninori Morimoto 		return 0;
748be6ac0a9SKuninori Morimoto 
749be6ac0a9SKuninori Morimoto 	return 1;
750be6ac0a9SKuninori Morimoto }
751be6ac0a9SKuninori Morimoto 
752db2a4165SFrank Mandarino static struct snd_soc_component *soc_find_component(
753c1e230f0SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc)
754db2a4165SFrank Mandarino {
755db2a4165SFrank Mandarino 	struct snd_soc_component *component;
756db2a4165SFrank Mandarino 
757db2a4165SFrank Mandarino 	lockdep_assert_held(&client_mutex);
758db2a4165SFrank Mandarino 
759e3303268SKuninori Morimoto 	/*
760e3303268SKuninori Morimoto 	 * NOTE
761e3303268SKuninori Morimoto 	 *
762e3303268SKuninori Morimoto 	 * It returns *1st* found component, but some driver
763e3303268SKuninori Morimoto 	 * has few components by same of_node/name
764e3303268SKuninori Morimoto 	 * ex)
765e3303268SKuninori Morimoto 	 *	CPU component and generic DMAEngine component
766e3303268SKuninori Morimoto 	 */
767c1e230f0SKuninori Morimoto 	for_each_component(component)
768c1e230f0SKuninori Morimoto 		if (snd_soc_is_matching_component(dlc, component))
769db2a4165SFrank Mandarino 			return component;
770db2a4165SFrank Mandarino 
771db2a4165SFrank Mandarino 	return NULL;
77212023a9aSMisael Lopez Cruz }
77312023a9aSMisael Lopez Cruz 
774fbb88b5cSMengdong Lin /**
775fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
776fbb88b5cSMengdong Lin  *
7774958471bSJeffy Chen  * @dlc: name of the DAI or the DAI driver and optional component info to match
778fbb88b5cSMengdong Lin  *
779ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
780fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
781fbb88b5cSMengdong Lin  * should also match if being specified.
782fbb88b5cSMengdong Lin  *
783fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
784fbb88b5cSMengdong Lin  */
785305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
78614621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
78712023a9aSMisael Lopez Cruz {
78814621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
78914621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
79012023a9aSMisael Lopez Cruz 
79134e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
79234e81ab4SLars-Peter Clausen 
79314621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs */
794368dee94SKuninori Morimoto 	for_each_component(component) {
795be6ac0a9SKuninori Morimoto 		if (!snd_soc_is_matching_component(dlc, component))
79612023a9aSMisael Lopez Cruz 			continue;
79715a0c645SKuninori Morimoto 		for_each_component_dais(component, dai) {
7984958471bSJeffy Chen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
7996a6dafdaSJeffy Chen 			    && (!dai->driver->name
8006a6dafdaSJeffy Chen 				|| strcmp(dai->driver->name, dlc->dai_name)))
80114621c7eSLars-Peter Clausen 				continue;
80212023a9aSMisael Lopez Cruz 
80314621c7eSLars-Peter Clausen 			return dai;
80412023a9aSMisael Lopez Cruz 		}
80512023a9aSMisael Lopez Cruz 	}
80612023a9aSMisael Lopez Cruz 
80712023a9aSMisael Lopez Cruz 	return NULL;
80812023a9aSMisael Lopez Cruz }
809305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
81012023a9aSMisael Lopez Cruz 
81117fb1755SMengdong Lin /**
81217fb1755SMengdong Lin  * snd_soc_find_dai_link - Find a DAI link
81317fb1755SMengdong Lin  *
81417fb1755SMengdong Lin  * @card: soc card
81517fb1755SMengdong Lin  * @id: DAI link ID to match
81617fb1755SMengdong Lin  * @name: DAI link name to match, optional
8178abab35fSCharles Keepax  * @stream_name: DAI link stream name to match, optional
81817fb1755SMengdong Lin  *
81917fb1755SMengdong Lin  * This function will search all existing DAI links of the soc card to
82017fb1755SMengdong Lin  * find the link of the same ID. Since DAI links may not have their
82117fb1755SMengdong Lin  * unique ID, so name and stream name should also match if being
82217fb1755SMengdong Lin  * specified.
82317fb1755SMengdong Lin  *
82417fb1755SMengdong Lin  * Return: pointer of DAI link, or NULL if not found.
82517fb1755SMengdong Lin  */
82617fb1755SMengdong Lin struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
82717fb1755SMengdong Lin 					       int id, const char *name,
82817fb1755SMengdong Lin 					       const char *stream_name)
82917fb1755SMengdong Lin {
83042849064SKuninori Morimoto 	struct snd_soc_dai_link *link;
83117fb1755SMengdong Lin 
83217fb1755SMengdong Lin 	lockdep_assert_held(&client_mutex);
83317fb1755SMengdong Lin 
83442849064SKuninori Morimoto 	for_each_card_links(card, link) {
83517fb1755SMengdong Lin 		if (link->id != id)
83617fb1755SMengdong Lin 			continue;
83717fb1755SMengdong Lin 
83817fb1755SMengdong Lin 		if (name && (!link->name || strcmp(name, link->name)))
83917fb1755SMengdong Lin 			continue;
84017fb1755SMengdong Lin 
84117fb1755SMengdong Lin 		if (stream_name && (!link->stream_name
84217fb1755SMengdong Lin 			|| strcmp(stream_name, link->stream_name)))
84317fb1755SMengdong Lin 			continue;
84417fb1755SMengdong Lin 
84517fb1755SMengdong Lin 		return link;
84617fb1755SMengdong Lin 	}
84717fb1755SMengdong Lin 
84817fb1755SMengdong Lin 	return NULL;
84917fb1755SMengdong Lin }
85017fb1755SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
85117fb1755SMengdong Lin 
85249a5ba1cSMengdong Lin static bool soc_is_dai_link_bound(struct snd_soc_card *card,
85349a5ba1cSMengdong Lin 		struct snd_soc_dai_link *dai_link)
854db2a4165SFrank Mandarino {
85549a5ba1cSMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
85649a5ba1cSMengdong Lin 
857bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
85849a5ba1cSMengdong Lin 		if (rtd->dai_link == dai_link)
85949a5ba1cSMengdong Lin 			return true;
86049a5ba1cSMengdong Lin 	}
86149a5ba1cSMengdong Lin 
86249a5ba1cSMengdong Lin 	return false;
86349a5ba1cSMengdong Lin }
86449a5ba1cSMengdong Lin 
8656f2f1ff0SMengdong Lin static int soc_bind_dai_link(struct snd_soc_card *card,
8666f2f1ff0SMengdong Lin 	struct snd_soc_dai_link *dai_link)
867db2a4165SFrank Mandarino {
8681a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
86934614739SJerome Brunet 	struct snd_soc_dai_link_component *codec, *platform;
87090be711eSKuninori Morimoto 	struct snd_soc_component *component;
87188bd870fSBenoit Cousson 	int i;
872db2a4165SFrank Mandarino 
873a655de80SLiam Girdwood 	if (dai_link->ignore)
874a655de80SLiam Girdwood 		return 0;
875a655de80SLiam Girdwood 
8766f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
8776308419aSMark Brown 
87849a5ba1cSMengdong Lin 	if (soc_is_dai_link_bound(card, dai_link)) {
87949a5ba1cSMengdong Lin 		dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
88049a5ba1cSMengdong Lin 			dai_link->name);
88149a5ba1cSMengdong Lin 		return 0;
88249a5ba1cSMengdong Lin 	}
883db2a4165SFrank Mandarino 
884513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
885513cb311SSudip Mukherjee 	if (!rtd)
886513cb311SSudip Mukherjee 		return -ENOMEM;
887513cb311SSudip Mukherjee 
88808a5841eSKuninori Morimoto 	/* FIXME: we need multi CPU support in the future */
88908a5841eSKuninori Morimoto 	rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
890b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
8916b490879SMartin Hundebøll 		dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
89208a5841eSKuninori Morimoto 			 dai_link->cpus->dai_name);
8931a497983SMengdong Lin 		goto _err_defer;
894c5af3a2eSMark Brown 	}
89590be711eSKuninori Morimoto 	snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
896c5af3a2eSMark Brown 
89788bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
898e2b30edfSKuninori Morimoto 	rtd->num_codecs = dai_link->num_codecs;
89934614739SJerome Brunet 	for_each_link_codecs(dai_link, i, codec) {
90034614739SJerome Brunet 		rtd->codec_dais[i] = snd_soc_find_dai(codec);
9010a2cfcd9SKuninori Morimoto 		if (!rtd->codec_dais[i]) {
9027c7e2d6aSStefan Agner 			dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
90334614739SJerome Brunet 				 codec->dai_name);
9041a497983SMengdong Lin 			goto _err_defer;
90512023a9aSMisael Lopez Cruz 		}
90634614739SJerome Brunet 
9070a2cfcd9SKuninori Morimoto 		snd_soc_rtdcom_add(rtd, rtd->codec_dais[i]->component);
90888bd870fSBenoit Cousson 	}
90988bd870fSBenoit Cousson 
91088bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
9110a2cfcd9SKuninori Morimoto 	rtd->codec_dai = rtd->codec_dais[0];
91212023a9aSMisael Lopez Cruz 
913e2b30edfSKuninori Morimoto 	/* Find PLATFORM from registered PLATFORMs */
91434614739SJerome Brunet 	for_each_link_platforms(dai_link, i, platform) {
915368dee94SKuninori Morimoto 		for_each_component(component) {
91634614739SJerome Brunet 			if (!snd_soc_is_matching_component(platform, component))
91790be711eSKuninori Morimoto 				continue;
91890be711eSKuninori Morimoto 
91990be711eSKuninori Morimoto 			snd_soc_rtdcom_add(rtd, component);
92090be711eSKuninori Morimoto 		}
92134614739SJerome Brunet 	}
92290be711eSKuninori Morimoto 
9231a497983SMengdong Lin 	soc_add_pcm_runtime(card, rtd);
924b19e6e7bSMark Brown 	return 0;
9251a497983SMengdong Lin 
9261a497983SMengdong Lin _err_defer:
9271a497983SMengdong Lin 	soc_free_pcm_runtime(rtd);
9281a497983SMengdong Lin 	return -EPROBE_DEFER;
9296b05eda6SMark Brown }
9306b05eda6SMark Brown 
93122d14231SKuninori Morimoto static void soc_cleanup_component(struct snd_soc_component *component)
93222d14231SKuninori Morimoto {
9333bb936f5SAmadeusz Sławiński 	snd_soc_component_set_jack(component, NULL, NULL);
93422d14231SKuninori Morimoto 	list_del(&component->card_list);
93522d14231SKuninori Morimoto 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
93622d14231SKuninori Morimoto 	soc_cleanup_component_debugfs(component);
93722d14231SKuninori Morimoto 	component->card = NULL;
9380e36f36bSPierre-Louis Bossart 	snd_soc_component_module_put_when_remove(component);
93922d14231SKuninori Morimoto }
94022d14231SKuninori Morimoto 
941f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
942d12cd198SStephen Warren {
943abd31b32SLars-Peter Clausen 	if (!component->card)
94470090bbbSLars-Peter Clausen 		return;
945d12cd198SStephen Warren 
94603b34dd7SKuninori Morimoto 	snd_soc_component_remove(component);
947d12cd198SStephen Warren 
94822d14231SKuninori Morimoto 	soc_cleanup_component(component);
949d12cd198SStephen Warren }
950d12cd198SStephen Warren 
951e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
952589c3563SJarkko Nikula {
953589c3563SJarkko Nikula 	int err;
954589c3563SJarkko Nikula 
95552abe6ccSGuennadi Liakhovetski 	if (!dai || !dai->probed || !dai->driver ||
9562eda3cb1SKuninori Morimoto 	    dai->driver->remove_order != order)
9572eda3cb1SKuninori Morimoto 		return;
9582eda3cb1SKuninori Morimoto 
959dcdab582SKuninori Morimoto 	err = snd_soc_dai_remove(dai);
960589c3563SJarkko Nikula 	if (err < 0)
961e60cd14fSLars-Peter Clausen 		dev_err(dai->dev,
962b0aa88afSMisael Lopez Cruz 			"ASoC: failed to remove %s: %d\n",
963e60cd14fSLars-Peter Clausen 			dai->name, err);
964dcdab582SKuninori Morimoto 
965e60cd14fSLars-Peter Clausen 	dai->probed = 0;
966b0aa88afSMisael Lopez Cruz }
967b0aa88afSMisael Lopez Cruz 
9681a497983SMengdong Lin static void soc_remove_link_dais(struct snd_soc_card *card,
9691a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
970f0fba2adSLiam Girdwood {
971e60cd14fSLars-Peter Clausen 	int i;
9720b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
973f0fba2adSLiam Girdwood 
974f0fba2adSLiam Girdwood 	/* unregister the rtd device */
975f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
97636ae1a96SMark Brown 		device_unregister(rtd->dev);
977f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
97802a06d30SBarry Song 	}
97902a06d30SBarry Song 
980f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
9810b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
9820b7990e3SKuninori Morimoto 		soc_remove_dai(codec_dai, order);
983f0fba2adSLiam Girdwood 
984e60cd14fSLars-Peter Clausen 	soc_remove_dai(rtd->cpu_dai, order);
985f0fba2adSLiam Girdwood }
986f0fba2adSLiam Girdwood 
9871a497983SMengdong Lin static void soc_remove_link_components(struct snd_soc_card *card,
9881a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, int order)
98962ae68faSStephen Warren {
99061aca564SLars-Peter Clausen 	struct snd_soc_component *component;
99190be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
99262ae68faSStephen Warren 
99390be711eSKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
99490be711eSKuninori Morimoto 		component = rtdcom->component;
99562ae68faSStephen Warren 
99670090bbbSLars-Peter Clausen 		if (component->driver->remove_order == order)
99761aca564SLars-Peter Clausen 			soc_remove_component(component);
99862ae68faSStephen Warren 	}
99962ae68faSStephen Warren }
100062ae68faSStephen Warren 
10010671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
10020671fd8eSKuninori Morimoto {
10031a497983SMengdong Lin 	int order;
10041a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
1005f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
10060671fd8eSKuninori Morimoto 
10071a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1008bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd)
10091a497983SMengdong Lin 			soc_remove_link_dais(card, rtd, order);
10100168bf0dSLiam Girdwood 	}
101162ae68faSStephen Warren 
10121a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1013bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd)
10141a497983SMengdong Lin 			soc_remove_link_components(card, rtd, order);
101562ae68faSStephen Warren 	}
101662ae68faSStephen Warren 
101798061fdbSKuninori Morimoto 	for_each_card_links_safe(card, link, _link) {
1018f8f80361SMengdong Lin 		if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1019f8f80361SMengdong Lin 			dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1020f8f80361SMengdong Lin 				link->name);
1021f8f80361SMengdong Lin 
1022f8f80361SMengdong Lin 		list_del(&link->list);
10230671fd8eSKuninori Morimoto 	}
10240671fd8eSKuninori Morimoto }
10250671fd8eSKuninori Morimoto 
1026923c5e61SMengdong Lin static int soc_init_dai_link(struct snd_soc_card *card,
1027923c5e61SMengdong Lin 			     struct snd_soc_dai_link *link)
1028923c5e61SMengdong Lin {
1029adb76b5bSKuninori Morimoto 	int i;
103034614739SJerome Brunet 	struct snd_soc_dai_link_component *codec, *platform;
1031923c5e61SMengdong Lin 
10323db769f1SKuninori Morimoto 	for_each_link_codecs(link, i, codec) {
1033923c5e61SMengdong Lin 		/*
1034923c5e61SMengdong Lin 		 * Codec must be specified by 1 of name or OF node,
1035923c5e61SMengdong Lin 		 * not both or neither.
1036923c5e61SMengdong Lin 		 */
103734614739SJerome Brunet 		if (!!codec->name == !!codec->of_node) {
1038923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1039923c5e61SMengdong Lin 				link->name);
1040923c5e61SMengdong Lin 			return -EINVAL;
1041923c5e61SMengdong Lin 		}
1042af18b13fSJerome Brunet 
1043923c5e61SMengdong Lin 		/* Codec DAI name must be specified */
10443db769f1SKuninori Morimoto 		if (!codec->dai_name) {
1045923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1046923c5e61SMengdong Lin 				link->name);
1047923c5e61SMengdong Lin 			return -EINVAL;
1048923c5e61SMengdong Lin 		}
1049af18b13fSJerome Brunet 
1050af18b13fSJerome Brunet 		/*
1051af18b13fSJerome Brunet 		 * Defer card registration if codec component is not added to
1052af18b13fSJerome Brunet 		 * component list.
1053af18b13fSJerome Brunet 		 */
1054af18b13fSJerome Brunet 		if (!soc_find_component(codec))
1055af18b13fSJerome Brunet 			return -EPROBE_DEFER;
1056923c5e61SMengdong Lin 	}
1057923c5e61SMengdong Lin 
105834614739SJerome Brunet 	for_each_link_platforms(link, i, platform) {
10591d768989SKuninori Morimoto 		/*
106034614739SJerome Brunet 		 * Platform may be specified by either name or OF node, but it
106134614739SJerome Brunet 		 * can be left unspecified, then no components will be inserted
106234614739SJerome Brunet 		 * in the rtdcom list
10631d768989SKuninori Morimoto 		 */
106434614739SJerome Brunet 		if (!!platform->name == !!platform->of_node) {
1065910fdcabSKuninori Morimoto 			dev_err(card->dev,
106634614739SJerome Brunet 				"ASoC: Neither/both platform name/of_node are set for %s\n",
1067923c5e61SMengdong Lin 				link->name);
1068923c5e61SMengdong Lin 			return -EINVAL;
1069923c5e61SMengdong Lin 		}
10708780cf11SAjit Pandey 
10718780cf11SAjit Pandey 		/*
107234614739SJerome Brunet 		 * Defer card registration if platform component is not added to
107334614739SJerome Brunet 		 * component list.
10748780cf11SAjit Pandey 		 */
107534614739SJerome Brunet 		if (!soc_find_component(platform))
10768780cf11SAjit Pandey 			return -EPROBE_DEFER;
1077923c5e61SMengdong Lin 	}
1078923c5e61SMengdong Lin 
1079923c5e61SMengdong Lin 	/* FIXME */
108008a5841eSKuninori Morimoto 	if (link->num_cpus > 1) {
1081923c5e61SMengdong Lin 		dev_err(card->dev,
108208a5841eSKuninori Morimoto 			"ASoC: multi cpu is not yet supported %s\n",
1083923c5e61SMengdong Lin 			link->name);
1084923c5e61SMengdong Lin 		return -EINVAL;
1085923c5e61SMengdong Lin 	}
1086923c5e61SMengdong Lin 
1087923c5e61SMengdong Lin 	/*
1088923c5e61SMengdong Lin 	 * CPU device may be specified by either name or OF node, but
1089923c5e61SMengdong Lin 	 * can be left unspecified, and will be matched based on DAI
1090923c5e61SMengdong Lin 	 * name alone..
1091923c5e61SMengdong Lin 	 */
109208a5841eSKuninori Morimoto 	if (link->cpus->name && link->cpus->of_node) {
1093923c5e61SMengdong Lin 		dev_err(card->dev,
1094923c5e61SMengdong Lin 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
1095923c5e61SMengdong Lin 			link->name);
1096923c5e61SMengdong Lin 		return -EINVAL;
1097923c5e61SMengdong Lin 	}
10988780cf11SAjit Pandey 
10998780cf11SAjit Pandey 	/*
11008780cf11SAjit Pandey 	 * Defer card registartion if cpu dai component is not added to
11018780cf11SAjit Pandey 	 * component list.
11028780cf11SAjit Pandey 	 */
110308a5841eSKuninori Morimoto 	if ((link->cpus->of_node || link->cpus->name) &&
1104c1e230f0SKuninori Morimoto 	    !soc_find_component(link->cpus))
11058780cf11SAjit Pandey 		return -EPROBE_DEFER;
11068780cf11SAjit Pandey 
1107923c5e61SMengdong Lin 	/*
1108923c5e61SMengdong Lin 	 * At least one of CPU DAI name or CPU device name/node must be
1109923c5e61SMengdong Lin 	 * specified
1110923c5e61SMengdong Lin 	 */
111108a5841eSKuninori Morimoto 	if (!link->cpus->dai_name &&
111208a5841eSKuninori Morimoto 	    !(link->cpus->name || link->cpus->of_node)) {
1113923c5e61SMengdong Lin 		dev_err(card->dev,
1114923c5e61SMengdong Lin 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1115923c5e61SMengdong Lin 			link->name);
1116923c5e61SMengdong Lin 		return -EINVAL;
1117923c5e61SMengdong Lin 	}
1118923c5e61SMengdong Lin 
1119923c5e61SMengdong Lin 	return 0;
1120923c5e61SMengdong Lin }
1121923c5e61SMengdong Lin 
1122ef2e8175SKuninori Morimoto void snd_soc_disconnect_sync(struct device *dev)
1123ef2e8175SKuninori Morimoto {
11242c7b696aSMarcel Ziswiler 	struct snd_soc_component *component =
11252c7b696aSMarcel Ziswiler 			snd_soc_lookup_component(dev, NULL);
1126ef2e8175SKuninori Morimoto 
1127ef2e8175SKuninori Morimoto 	if (!component || !component->card)
1128ef2e8175SKuninori Morimoto 		return;
1129ef2e8175SKuninori Morimoto 
1130ef2e8175SKuninori Morimoto 	snd_card_disconnect_sync(component->card->snd_card);
1131ef2e8175SKuninori Morimoto }
1132df532185SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
1133ef2e8175SKuninori Morimoto 
1134f8f80361SMengdong Lin /**
1135f8f80361SMengdong Lin  * snd_soc_add_dai_link - Add a DAI link dynamically
1136f8f80361SMengdong Lin  * @card: The ASoC card to which the DAI link is added
1137f8f80361SMengdong Lin  * @dai_link: The new DAI link to add
1138f8f80361SMengdong Lin  *
1139f8f80361SMengdong Lin  * This function adds a DAI link to the ASoC card's link list.
1140f8f80361SMengdong Lin  *
1141f8f80361SMengdong Lin  * Note: Topology can use this API to add DAI links when probing the
1142f8f80361SMengdong Lin  * topology component. And machine drivers can still define static
1143f8f80361SMengdong Lin  * DAI links in dai_link array.
1144f8f80361SMengdong Lin  */
1145f8f80361SMengdong Lin int snd_soc_add_dai_link(struct snd_soc_card *card,
1146f8f80361SMengdong Lin 		struct snd_soc_dai_link *dai_link)
1147f8f80361SMengdong Lin {
1148f8f80361SMengdong Lin 	if (dai_link->dobj.type
1149f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1150f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1151f8f80361SMengdong Lin 			dai_link->dobj.type);
1152f8f80361SMengdong Lin 		return -EINVAL;
1153f8f80361SMengdong Lin 	}
1154f8f80361SMengdong Lin 
1155f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
11562c7b696aSMarcel Ziswiler 	/*
11572c7b696aSMarcel Ziswiler 	 * Notify the machine driver for extra initialization
1158d6f220eaSMengdong Lin 	 * on the link created by topology.
1159d6f220eaSMengdong Lin 	 */
1160d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->add_dai_link)
1161d6f220eaSMengdong Lin 		card->add_dai_link(card, dai_link);
1162d6f220eaSMengdong Lin 
11636634e3d6SKuninori Morimoto 	/* see for_each_card_links */
1164f8f80361SMengdong Lin 	list_add_tail(&dai_link->list, &card->dai_link_list);
1165f8f80361SMengdong Lin 
1166f8f80361SMengdong Lin 	return 0;
1167f8f80361SMengdong Lin }
1168f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1169f8f80361SMengdong Lin 
1170f8f80361SMengdong Lin /**
1171f8f80361SMengdong Lin  * snd_soc_remove_dai_link - Remove a DAI link from the list
1172f8f80361SMengdong Lin  * @card: The ASoC card that owns the link
1173f8f80361SMengdong Lin  * @dai_link: The DAI link to remove
1174f8f80361SMengdong Lin  *
1175f8f80361SMengdong Lin  * This function removes a DAI link from the ASoC card's link list.
1176f8f80361SMengdong Lin  *
1177f8f80361SMengdong Lin  * For DAI links previously added by topology, topology should
1178f8f80361SMengdong Lin  * remove them by using the dobj embedded in the link.
1179f8f80361SMengdong Lin  */
1180f8f80361SMengdong Lin void snd_soc_remove_dai_link(struct snd_soc_card *card,
1181f8f80361SMengdong Lin 			     struct snd_soc_dai_link *dai_link)
1182f8f80361SMengdong Lin {
1183f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
1184f8f80361SMengdong Lin 
1185f8f80361SMengdong Lin 	if (dai_link->dobj.type
1186f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1187f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1188f8f80361SMengdong Lin 			dai_link->dobj.type);
1189f8f80361SMengdong Lin 		return;
1190f8f80361SMengdong Lin 	}
1191f8f80361SMengdong Lin 
1192f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
11932c7b696aSMarcel Ziswiler 	/*
11942c7b696aSMarcel Ziswiler 	 * Notify the machine driver for extra destruction
1195d6f220eaSMengdong Lin 	 * on the link created by topology.
1196d6f220eaSMengdong Lin 	 */
1197d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->remove_dai_link)
1198d6f220eaSMengdong Lin 		card->remove_dai_link(card, dai_link);
1199d6f220eaSMengdong Lin 
120098061fdbSKuninori Morimoto 	for_each_card_links_safe(card, link, _link) {
1201f8f80361SMengdong Lin 		if (link == dai_link) {
1202f8f80361SMengdong Lin 			list_del(&link->list);
1203f8f80361SMengdong Lin 			return;
1204f8f80361SMengdong Lin 		}
1205f8f80361SMengdong Lin 	}
1206f8f80361SMengdong Lin }
1207f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1208f0fba2adSLiam Girdwood 
1209aefba455SJerome Brunet static void soc_set_of_name_prefix(struct snd_soc_component *component)
1210aefba455SJerome Brunet {
1211c0834440SKuninori Morimoto 	struct device_node *component_of_node = soc_component_to_node(component);
1212aefba455SJerome Brunet 	const char *str;
1213aefba455SJerome Brunet 	int ret;
1214aefba455SJerome Brunet 
1215aefba455SJerome Brunet 	ret = of_property_read_string(component_of_node, "sound-name-prefix",
1216aefba455SJerome Brunet 				      &str);
1217aefba455SJerome Brunet 	if (!ret)
1218aefba455SJerome Brunet 		component->name_prefix = str;
1219aefba455SJerome Brunet }
1220aefba455SJerome Brunet 
1221ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
122294f99c87SLars-Peter Clausen 				struct snd_soc_component *component)
1223ead9b919SJarkko Nikula {
1224ead9b919SJarkko Nikula 	int i;
1225ead9b919SJarkko Nikula 
1226aefba455SJerome Brunet 	for (i = 0; i < card->num_configs && card->codec_conf; i++) {
1227ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1228c0834440SKuninori Morimoto 		struct device_node *component_of_node = soc_component_to_node(component);
1229b24c539bSCharles Keepax 
1230b24c539bSCharles Keepax 		if (map->of_node && component_of_node != map->of_node)
12313ca041edSSebastian Reichel 			continue;
123294f99c87SLars-Peter Clausen 		if (map->dev_name && strcmp(component->name, map->dev_name))
12333ca041edSSebastian Reichel 			continue;
123494f99c87SLars-Peter Clausen 		component->name_prefix = map->name_prefix;
1235aefba455SJerome Brunet 		return;
1236ead9b919SJarkko Nikula 	}
1237aefba455SJerome Brunet 
1238aefba455SJerome Brunet 	/*
1239aefba455SJerome Brunet 	 * If there is no configuration table or no match in the table,
1240aefba455SJerome Brunet 	 * check if a prefix is provided in the node
1241aefba455SJerome Brunet 	 */
1242aefba455SJerome Brunet 	soc_set_of_name_prefix(component);
1243ead9b919SJarkko Nikula }
1244ead9b919SJarkko Nikula 
1245f1d45cc3SLars-Peter Clausen static int soc_probe_component(struct snd_soc_card *card,
1246f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component)
1247589c3563SJarkko Nikula {
12482c7b696aSMarcel Ziswiler 	struct snd_soc_dapm_context *dapm =
12492c7b696aSMarcel Ziswiler 			snd_soc_component_get_dapm(component);
1250888df395SMark Brown 	struct snd_soc_dai *dai;
1251f1d45cc3SLars-Peter Clausen 	int ret;
1252589c3563SJarkko Nikula 
12531b7c1231SLars-Peter Clausen 	if (!strcmp(component->name, "snd-soc-dummy"))
125470090bbbSLars-Peter Clausen 		return 0;
1255589c3563SJarkko Nikula 
1256abd31b32SLars-Peter Clausen 	if (component->card) {
12571b7c1231SLars-Peter Clausen 		if (component->card != card) {
12581b7c1231SLars-Peter Clausen 			dev_err(component->dev,
12591b7c1231SLars-Peter Clausen 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
12601b7c1231SLars-Peter Clausen 				card->name, component->card->name);
12611b7c1231SLars-Peter Clausen 			return -ENODEV;
12621b7c1231SLars-Peter Clausen 		}
12631b7c1231SLars-Peter Clausen 		return 0;
12641b7c1231SLars-Peter Clausen 	}
12651b7c1231SLars-Peter Clausen 
12664a81e8f3SKuninori Morimoto 	ret = snd_soc_component_module_get_when_probe(component);
12674a81e8f3SKuninori Morimoto 	if (ret < 0)
12684a81e8f3SKuninori Morimoto 		return ret;
1269abd31b32SLars-Peter Clausen 
1270f1d45cc3SLars-Peter Clausen 	component->card = card;
1271f1d45cc3SLars-Peter Clausen 	dapm->card = card;
127222d14231SKuninori Morimoto 	INIT_LIST_HEAD(&component->card_list);
127322d14231SKuninori Morimoto 	INIT_LIST_HEAD(&dapm->list);
1274f1d45cc3SLars-Peter Clausen 	soc_set_name_prefix(card, component);
1275589c3563SJarkko Nikula 
1276f1d45cc3SLars-Peter Clausen 	soc_init_component_debugfs(component);
1277d5d1e0beSLars-Peter Clausen 
1278688d0ebfSKuninori Morimoto 	ret = snd_soc_dapm_new_controls(dapm,
1279688d0ebfSKuninori Morimoto 					component->driver->dapm_widgets,
1280688d0ebfSKuninori Morimoto 					component->driver->num_dapm_widgets);
128177530150SLars-Peter Clausen 
1282b318ad50SNariman Poushin 	if (ret != 0) {
1283f1d45cc3SLars-Peter Clausen 		dev_err(component->dev,
1284b318ad50SNariman Poushin 			"Failed to create new controls %d\n", ret);
1285b318ad50SNariman Poushin 		goto err_probe;
1286b318ad50SNariman Poushin 	}
1287b318ad50SNariman Poushin 
128815a0c645SKuninori Morimoto 	for_each_component_dais(component, dai) {
12890634814fSLars-Peter Clausen 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1290261edc70SNariman Poushin 		if (ret != 0) {
1291f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1292261edc70SNariman Poushin 				"Failed to create DAI widgets %d\n", ret);
1293261edc70SNariman Poushin 			goto err_probe;
1294261edc70SNariman Poushin 		}
1295261edc70SNariman Poushin 	}
1296888df395SMark Brown 
129708e837ddSKuninori Morimoto 	ret = snd_soc_component_probe(component);
1298589c3563SJarkko Nikula 	if (ret < 0) {
1299f1d45cc3SLars-Peter Clausen 		dev_err(component->dev,
1300f1d45cc3SLars-Peter Clausen 			"ASoC: failed to probe component %d\n", ret);
130170d29331SJarkko Nikula 		goto err_probe;
1302589c3563SJarkko Nikula 	}
1303f1d45cc3SLars-Peter Clausen 	WARN(dapm->idle_bias_off &&
1304f1d45cc3SLars-Peter Clausen 	     dapm->bias_level != SND_SOC_BIAS_OFF,
130510e8aa9aSMichał Mirosław 	     "codec %s can not start from non-off bias with idle_bias_off==1\n",
1306f1d45cc3SLars-Peter Clausen 	     component->name);
1307589c3563SJarkko Nikula 
1308f2ed6b07SMengdong Lin 	/* machine specific init */
1309f2ed6b07SMengdong Lin 	if (component->init) {
1310f2ed6b07SMengdong Lin 		ret = component->init(component);
1311f2ed6b07SMengdong Lin 		if (ret < 0) {
1312f2ed6b07SMengdong Lin 			dev_err(component->dev,
1313f2ed6b07SMengdong Lin 				"Failed to do machine specific init %d\n", ret);
1314f2ed6b07SMengdong Lin 			goto err_probe;
1315f2ed6b07SMengdong Lin 		}
1316f2ed6b07SMengdong Lin 	}
1317f2ed6b07SMengdong Lin 
1318e6d7020cSKuninori Morimoto 	ret = snd_soc_add_component_controls(component,
1319b8972bf0SKuninori Morimoto 					     component->driver->controls,
1320b8972bf0SKuninori Morimoto 					     component->driver->num_controls);
1321e6d7020cSKuninori Morimoto 	if (ret < 0)
1322e6d7020cSKuninori Morimoto 		goto err_probe;
1323e6d7020cSKuninori Morimoto 
1324daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(dapm,
13256969b2baSKuninori Morimoto 				      component->driver->dapm_routes,
13266969b2baSKuninori Morimoto 				      component->driver->num_dapm_routes);
1327daa480bdSKuninori Morimoto 	if (ret < 0)
1328daa480bdSKuninori Morimoto 		goto err_probe;
132989b95ac0SMark Brown 
1330f1d45cc3SLars-Peter Clausen 	list_add(&dapm->list, &card->dapm_list);
1331f70f18f7SKuninori Morimoto 	/* see for_each_card_components */
1332d9fc4063SKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1333589c3563SJarkko Nikula 
133470d29331SJarkko Nikula err_probe:
133522d14231SKuninori Morimoto 	if (ret < 0)
133622d14231SKuninori Morimoto 		soc_cleanup_component(component);
1337956245e9SLiam Girdwood 
1338956245e9SLiam Girdwood 	return ret;
1339956245e9SLiam Girdwood }
1340956245e9SLiam Girdwood 
134136ae1a96SMark Brown static void rtd_release(struct device *dev)
134236ae1a96SMark Brown {
134336ae1a96SMark Brown 	kfree(dev);
134436ae1a96SMark Brown }
1345f0fba2adSLiam Girdwood 
13465f3484acSLars-Peter Clausen static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
13475f3484acSLars-Peter Clausen 	const char *name)
1348503ae5e0SMisael Lopez Cruz {
1349589c3563SJarkko Nikula 	int ret = 0;
1350589c3563SJarkko Nikula 
1351589c3563SJarkko Nikula 	/* register the rtd device */
135236ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
135336ae1a96SMark Brown 	if (!rtd->dev)
135436ae1a96SMark Brown 		return -ENOMEM;
135536ae1a96SMark Brown 	device_initialize(rtd->dev);
13565f3484acSLars-Peter Clausen 	rtd->dev->parent = rtd->card->dev;
135736ae1a96SMark Brown 	rtd->dev->release = rtd_release;
1358d29697dcSTakashi Iwai 	rtd->dev->groups = soc_dev_attr_groups;
1359f294afedSLars-Peter Clausen 	dev_set_name(rtd->dev, "%s", name);
136036ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1361b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
136201d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
136301d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
136401d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
136501d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
136636ae1a96SMark Brown 	ret = device_add(rtd->dev);
1367589c3563SJarkko Nikula 	if (ret < 0) {
1368865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1369865df9cbSChuansheng Liu 		put_device(rtd->dev);
13705f3484acSLars-Peter Clausen 		dev_err(rtd->card->dev,
1371f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1372589c3563SJarkko Nikula 		return ret;
1373589c3563SJarkko Nikula 	}
1374589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1375589c3563SJarkko Nikula 	return 0;
1376589c3563SJarkko Nikula }
1377589c3563SJarkko Nikula 
13781a497983SMengdong Lin static int soc_probe_link_components(struct snd_soc_card *card,
13792c7b696aSMarcel Ziswiler 				     struct snd_soc_pcm_runtime *rtd, int order)
138062ae68faSStephen Warren {
1381f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component;
138290be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
138390be711eSKuninori Morimoto 	int ret;
138462ae68faSStephen Warren 
138590be711eSKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
138690be711eSKuninori Morimoto 		component = rtdcom->component;
138790be711eSKuninori Morimoto 
138870090bbbSLars-Peter Clausen 		if (component->driver->probe_order == order) {
1389f1d45cc3SLars-Peter Clausen 			ret = soc_probe_component(card, component);
139062ae68faSStephen Warren 			if (ret < 0)
139162ae68faSStephen Warren 				return ret;
139262ae68faSStephen Warren 		}
139362ae68faSStephen Warren 	}
139462ae68faSStephen Warren 
139562ae68faSStephen Warren 	return 0;
139662ae68faSStephen Warren }
139762ae68faSStephen Warren 
13988e2be562SLars-Peter Clausen static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1399b0aa88afSMisael Lopez Cruz {
1400cfd9b5fbSKuninori Morimoto 	int ret;
1401cfd9b5fbSKuninori Morimoto 
14027a2ccad5SKuninori Morimoto 	if (dai->probed ||
14037a2ccad5SKuninori Morimoto 	    dai->driver->probe_order != order)
14047a2ccad5SKuninori Morimoto 		return 0;
1405b0aa88afSMisael Lopez Cruz 
1406cfd9b5fbSKuninori Morimoto 	ret = snd_soc_dai_probe(dai);
1407b0aa88afSMisael Lopez Cruz 	if (ret < 0) {
14087a2ccad5SKuninori Morimoto 		dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
14098e2be562SLars-Peter Clausen 			dai->name, ret);
1410b0aa88afSMisael Lopez Cruz 		return ret;
1411b0aa88afSMisael Lopez Cruz 	}
1412b0aa88afSMisael Lopez Cruz 
14138e2be562SLars-Peter Clausen 	dai->probed = 1;
1414b0aa88afSMisael Lopez Cruz 
1415b0aa88afSMisael Lopez Cruz 	return 0;
1416b0aa88afSMisael Lopez Cruz }
1417b0aa88afSMisael Lopez Cruz 
141825f7b701SArnaud Pouliquen static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
141925f7b701SArnaud Pouliquen 				struct snd_soc_pcm_runtime *rtd)
142025f7b701SArnaud Pouliquen {
142125f7b701SArnaud Pouliquen 	int i, ret = 0;
142225f7b701SArnaud Pouliquen 
142325f7b701SArnaud Pouliquen 	for (i = 0; i < num_dais; ++i) {
142425f7b701SArnaud Pouliquen 		struct snd_soc_dai_driver *drv = dais[i]->driver;
142525f7b701SArnaud Pouliquen 
1426de17f14eSRohit kumar 		if (drv->pcm_new)
142725f7b701SArnaud Pouliquen 			ret = drv->pcm_new(rtd, dais[i]);
142825f7b701SArnaud Pouliquen 		if (ret < 0) {
142925f7b701SArnaud Pouliquen 			dev_err(dais[i]->dev,
143025f7b701SArnaud Pouliquen 				"ASoC: Failed to bind %s with pcm device\n",
143125f7b701SArnaud Pouliquen 				dais[i]->name);
143225f7b701SArnaud Pouliquen 			return ret;
143325f7b701SArnaud Pouliquen 		}
143425f7b701SArnaud Pouliquen 	}
143525f7b701SArnaud Pouliquen 
143625f7b701SArnaud Pouliquen 	return 0;
143725f7b701SArnaud Pouliquen }
143825f7b701SArnaud Pouliquen 
14391a497983SMengdong Lin static int soc_probe_link_dais(struct snd_soc_card *card,
14401a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1441f0fba2adSLiam Girdwood {
14421a497983SMengdong Lin 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1443c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1444a655de80SLiam Girdwood 	struct snd_soc_rtdcom_list *rtdcom;
1445a655de80SLiam Girdwood 	struct snd_soc_component *component;
14460b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1447a655de80SLiam Girdwood 	int i, ret, num;
1448f0fba2adSLiam Girdwood 
1449f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
14501a497983SMengdong Lin 			card->name, rtd->num, order);
1451f0fba2adSLiam Girdwood 
1452f0fba2adSLiam Girdwood 	/* set default power off timeout */
1453f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1454f0fba2adSLiam Girdwood 
14558e2be562SLars-Peter Clausen 	ret = soc_probe_dai(cpu_dai, order);
14568e2be562SLars-Peter Clausen 	if (ret)
1457f0fba2adSLiam Girdwood 		return ret;
1458f0fba2adSLiam Girdwood 
1459f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
14600b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
14610b7990e3SKuninori Morimoto 		ret = soc_probe_dai(codec_dai, order);
1462b0aa88afSMisael Lopez Cruz 		if (ret)
1463f0fba2adSLiam Girdwood 			return ret;
146488bd870fSBenoit Cousson 	}
1465f0fba2adSLiam Girdwood 
14660168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
14670168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
14680168bf0dSLiam Girdwood 		return 0;
14690168bf0dSLiam Girdwood 
14705f3484acSLars-Peter Clausen 	/* do machine specific initialization */
14715f3484acSLars-Peter Clausen 	if (dai_link->init) {
14725f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
14735f3484acSLars-Peter Clausen 		if (ret < 0) {
14745f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
14755f3484acSLars-Peter Clausen 				dai_link->name, ret);
14765f3484acSLars-Peter Clausen 			return ret;
14775f3484acSLars-Peter Clausen 		}
14785f3484acSLars-Peter Clausen 	}
14795f3484acSLars-Peter Clausen 
148040aa5383SRicard Wanderlof 	if (dai_link->dai_fmt) {
148140aa5383SRicard Wanderlof 		ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
148240aa5383SRicard Wanderlof 		if (ret)
148340aa5383SRicard Wanderlof 			return ret;
148440aa5383SRicard Wanderlof 	}
1485a5053a8eSKuninori Morimoto 
14865f3484acSLars-Peter Clausen 	ret = soc_post_component_init(rtd, dai_link->name);
1487589c3563SJarkko Nikula 	if (ret)
1488f0fba2adSLiam Girdwood 		return ret;
1489f0fba2adSLiam Girdwood 
14905f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
14912e55b90aSLars-Peter Clausen 	soc_dpcm_debugfs_add(rtd);
14925f3484acSLars-Peter Clausen 
1493a655de80SLiam Girdwood 	num = rtd->num;
1494a655de80SLiam Girdwood 
1495a655de80SLiam Girdwood 	/*
1496a655de80SLiam Girdwood 	 * most drivers will register their PCMs using DAI link ordering but
1497a655de80SLiam Girdwood 	 * topology based drivers can use the DAI link id field to set PCM
1498a655de80SLiam Girdwood 	 * device number and then use rtd + a base offset of the BEs.
1499a655de80SLiam Girdwood 	 */
1500a655de80SLiam Girdwood 	for_each_rtdcom(rtd, rtdcom) {
1501a655de80SLiam Girdwood 		component = rtdcom->component;
1502a655de80SLiam Girdwood 
1503a655de80SLiam Girdwood 		if (!component->driver->use_dai_pcm_id)
1504a655de80SLiam Girdwood 			continue;
1505a655de80SLiam Girdwood 
1506a655de80SLiam Girdwood 		if (rtd->dai_link->no_pcm)
1507a655de80SLiam Girdwood 			num += component->driver->be_pcm_base;
1508a655de80SLiam Girdwood 		else
1509a655de80SLiam Girdwood 			num = rtd->dai_link->id;
1510a655de80SLiam Girdwood 	}
1511a655de80SLiam Girdwood 
1512b423c420SKuninori Morimoto 	/* create compress_device if possible */
1513b423c420SKuninori Morimoto 	ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1514b423c420SKuninori Morimoto 	if (ret != -ENOTSUPP) {
1515b423c420SKuninori Morimoto 		if (ret < 0)
1516f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
15171245b700SNamarta Kohli 					 dai_link->stream_name);
15181245b700SNamarta Kohli 		return ret;
15191245b700SNamarta Kohli 	}
1520b423c420SKuninori Morimoto 
1521f0fba2adSLiam Girdwood 	/* create the pcm */
1522a655de80SLiam Girdwood 	ret = soc_new_pcm(rtd, num);
1523f0fba2adSLiam Girdwood 	if (ret < 0) {
1524f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
15250837fc62SFabio Estevam 			dai_link->stream_name, ret);
1526f0fba2adSLiam Girdwood 		return ret;
1527f0fba2adSLiam Girdwood 	}
152825f7b701SArnaud Pouliquen 	ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
152925f7b701SArnaud Pouliquen 	if (ret < 0)
153025f7b701SArnaud Pouliquen 		return ret;
153125f7b701SArnaud Pouliquen 	ret = soc_link_dai_pcm_new(rtd->codec_dais,
153225f7b701SArnaud Pouliquen 				   rtd->num_codecs, rtd);
153325f7b701SArnaud Pouliquen 	return ret;
1534f0fba2adSLiam Girdwood }
1535f0fba2adSLiam Girdwood 
1536*c2b71c71SKuninori Morimoto static int soc_bind_aux_dev(struct snd_soc_card *card,
1537*c2b71c71SKuninori Morimoto 			    struct snd_soc_aux_dev *aux_dev)
1538b19e6e7bSMark Brown {
1539f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
15403ca041edSSebastian Reichel 
1541f2ed6b07SMengdong Lin 	/* codecs, usually analog devices */
15423dc29b8bSKuninori Morimoto 	component = soc_find_component(&aux_dev->dlc);
1543f2ed6b07SMengdong Lin 	if (!component)
15443dc29b8bSKuninori Morimoto 		return -EPROBE_DEFER;
15453ca041edSSebastian Reichel 
1546f2ed6b07SMengdong Lin 	component->init = aux_dev->init;
1547*c2b71c71SKuninori Morimoto 	/* see for_each_card_auxs */
1548d2e3a135SSylwester Nawrocki 	list_add(&component->card_aux_list, &card->aux_comp_list);
15491a653aa4SKuninori Morimoto 
1550f2ed6b07SMengdong Lin 	return 0;
1551b19e6e7bSMark Brown }
1552b19e6e7bSMark Brown 
1553f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1554f2ed6b07SMengdong Lin {
1555991454e1SKuninori Morimoto 	struct snd_soc_component *comp;
1556f2ed6b07SMengdong Lin 	int order;
1557f2ed6b07SMengdong Lin 	int ret;
1558f2ed6b07SMengdong Lin 
15591a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1560*c2b71c71SKuninori Morimoto 		for_each_card_auxs(card, comp) {
1561f2ed6b07SMengdong Lin 			if (comp->driver->probe_order == order) {
1562f2ed6b07SMengdong Lin 				ret = soc_probe_component(card,	comp);
1563f2ed6b07SMengdong Lin 				if (ret < 0) {
1564f2ed6b07SMengdong Lin 					dev_err(card->dev,
1565f2ed6b07SMengdong Lin 						"ASoC: failed to probe aux component %s %d\n",
1566f2ed6b07SMengdong Lin 						comp->name, ret);
1567f2ed6b07SMengdong Lin 					return ret;
1568f2ed6b07SMengdong Lin 				}
1569f2ed6b07SMengdong Lin 			}
1570f2ed6b07SMengdong Lin 		}
1571f2ed6b07SMengdong Lin 	}
157265d9361fSLars-Peter Clausen 
157344c69bb1SLars-Peter Clausen 	return 0;
15743ca041edSSebastian Reichel }
15752eea392dSJarkko Nikula 
1576f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
157744c69bb1SLars-Peter Clausen {
1578f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1579f2ed6b07SMengdong Lin 	int order;
158044c69bb1SLars-Peter Clausen 
15811a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1582*c2b71c71SKuninori Morimoto 		for_each_card_auxs_safe(card, comp, _comp) {
15831a653aa4SKuninori Morimoto 
1584f2ed6b07SMengdong Lin 			if (comp->driver->remove_order == order) {
1585f2ed6b07SMengdong Lin 				soc_remove_component(comp);
1586991454e1SKuninori Morimoto 				/* remove it from the card's aux_comp_list */
1587991454e1SKuninori Morimoto 				list_del(&comp->card_aux_list);
15882eea392dSJarkko Nikula 			}
15895f3484acSLars-Peter Clausen 		}
15902eea392dSJarkko Nikula 	}
15912eea392dSJarkko Nikula }
15922eea392dSJarkko Nikula 
1593ce64c8b9SLars-Peter Clausen /**
1594ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1595ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1596ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1597ce64c8b9SLars-Peter Clausen  *
1598ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1599ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1600ce64c8b9SLars-Peter Clausen  *
1601ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1602ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1603ce64c8b9SLars-Peter Clausen  *
1604ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1605ce64c8b9SLars-Peter Clausen  */
1606ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1607ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1608ce64c8b9SLars-Peter Clausen {
1609ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
16100b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1611ce64c8b9SLars-Peter Clausen 	unsigned int i;
1612ce64c8b9SLars-Peter Clausen 	int ret;
1613ce64c8b9SLars-Peter Clausen 
16140b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1615ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1616ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1617ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1618ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1619ce64c8b9SLars-Peter Clausen 			return ret;
1620ce64c8b9SLars-Peter Clausen 		}
1621ce64c8b9SLars-Peter Clausen 	}
1622ce64c8b9SLars-Peter Clausen 
16232c7b696aSMarcel Ziswiler 	/*
16242c7b696aSMarcel Ziswiler 	 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
16252c7b696aSMarcel Ziswiler 	 * the component which has non_legacy_dai_naming is Codec
16262c7b696aSMarcel Ziswiler 	 */
1627999f7f5aSKuninori Morimoto 	if (cpu_dai->component->driver->non_legacy_dai_naming) {
1628ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1629ce64c8b9SLars-Peter Clausen 
1630ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1631ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1632ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1633ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1634ce64c8b9SLars-Peter Clausen 			break;
1635ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1636ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1637ce64c8b9SLars-Peter Clausen 			break;
1638ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1639ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1640ce64c8b9SLars-Peter Clausen 			break;
1641ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1642ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1643ce64c8b9SLars-Peter Clausen 			break;
1644ce64c8b9SLars-Peter Clausen 		}
1645ce64c8b9SLars-Peter Clausen 
1646ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1647ce64c8b9SLars-Peter Clausen 	}
1648ce64c8b9SLars-Peter Clausen 
1649ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1650ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1651ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1652ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1653ce64c8b9SLars-Peter Clausen 		return ret;
1654ce64c8b9SLars-Peter Clausen 	}
1655ce64c8b9SLars-Peter Clausen 
1656ce64c8b9SLars-Peter Clausen 	return 0;
1657ce64c8b9SLars-Peter Clausen }
1658ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1659ce64c8b9SLars-Peter Clausen 
16601f5a4535STakashi Iwai #ifdef CONFIG_DMI
16612c7b696aSMarcel Ziswiler /*
16622c7b696aSMarcel Ziswiler  * Trim special characters, and replace '-' with '_' since '-' is used to
1663345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1664345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1665345233d7SLiam Girdwood  */
1666345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1667345233d7SLiam Girdwood {
1668345233d7SLiam Girdwood 	int i, j = 0;
1669345233d7SLiam Girdwood 
1670345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1671345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1672345233d7SLiam Girdwood 		    || (name[i] == '_'))
1673345233d7SLiam Girdwood 			name[j++] = name[i];
1674345233d7SLiam Girdwood 		else if (name[i] == '-')
1675345233d7SLiam Girdwood 			name[j++] = '_';
1676345233d7SLiam Girdwood 	}
1677345233d7SLiam Girdwood 
1678345233d7SLiam Girdwood 	name[j] = '\0';
1679345233d7SLiam Girdwood }
1680345233d7SLiam Girdwood 
16812c7b696aSMarcel Ziswiler /*
16822c7b696aSMarcel Ziswiler  * Check if a DMI field is valid, i.e. not containing any string
168398faf436SMengdong Lin  * in the black list.
168498faf436SMengdong Lin  */
168598faf436SMengdong Lin static int is_dmi_valid(const char *field)
168698faf436SMengdong Lin {
168798faf436SMengdong Lin 	int i = 0;
168898faf436SMengdong Lin 
168998faf436SMengdong Lin 	while (dmi_blacklist[i]) {
169098faf436SMengdong Lin 		if (strstr(field, dmi_blacklist[i]))
169198faf436SMengdong Lin 			return 0;
169298faf436SMengdong Lin 		i++;
169346b5a4d2SWu Fengguang 	}
169498faf436SMengdong Lin 
169598faf436SMengdong Lin 	return 1;
169698faf436SMengdong Lin }
169798faf436SMengdong Lin 
1698345233d7SLiam Girdwood /**
1699345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
1700345233d7SLiam Girdwood  * @card: The card to register DMI names
1701345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
1702345233d7SLiam Girdwood  *
1703345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
1704345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
1705345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
1706345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
1707345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
1708345233d7SLiam Girdwood  * as the sound card long name, in the format of
1709345233d7SLiam Girdwood  * "vendor-product-version-board"
1710345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
1711345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
1712345233d7SLiam Girdwood  * (UCM) configurations for the card.
1713345233d7SLiam Girdwood  *
1714345233d7SLiam Girdwood  * Possible card long names may be:
1715345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
1716345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1717345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1718345233d7SLiam Girdwood  *
1719345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
1720345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
1721345233d7SLiam Girdwood  *
1722345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
1723345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
1724345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
1725345233d7SLiam Girdwood  * charactors like SPACE.
1726345233d7SLiam Girdwood  *
1727345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
1728345233d7SLiam Girdwood  */
1729345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1730345233d7SLiam Girdwood {
1731345233d7SLiam Girdwood 	const char *vendor, *product, *product_version, *board;
1732345233d7SLiam Girdwood 	size_t longname_buf_size = sizeof(card->snd_card->longname);
1733345233d7SLiam Girdwood 	size_t len;
1734345233d7SLiam Girdwood 
1735345233d7SLiam Girdwood 	if (card->long_name)
1736345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
1737345233d7SLiam Girdwood 
1738345233d7SLiam Girdwood 	/* make up dmi long name as: vendor.product.version.board */
1739345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
174098faf436SMengdong Lin 	if (!vendor || !is_dmi_valid(vendor)) {
1741345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1742345233d7SLiam Girdwood 		return 0;
1743345233d7SLiam Girdwood 	}
1744345233d7SLiam Girdwood 
1745345233d7SLiam Girdwood 	snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1746345233d7SLiam Girdwood 			 "%s", vendor);
1747345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
1748345233d7SLiam Girdwood 
1749345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
175098faf436SMengdong Lin 	if (product && is_dmi_valid(product)) {
1751345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1752345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1753345233d7SLiam Girdwood 			 longname_buf_size - len,
1754345233d7SLiam Girdwood 			 "-%s", product);
1755345233d7SLiam Girdwood 
1756345233d7SLiam Girdwood 		len++;	/* skip the separator "-" */
1757345233d7SLiam Girdwood 		if (len < longname_buf_size)
1758345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1759345233d7SLiam Girdwood 
17602c7b696aSMarcel Ziswiler 		/*
17612c7b696aSMarcel Ziswiler 		 * some vendors like Lenovo may only put a self-explanatory
1762345233d7SLiam Girdwood 		 * name in the product version field
1763345233d7SLiam Girdwood 		 */
1764345233d7SLiam Girdwood 		product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
176598faf436SMengdong Lin 		if (product_version && is_dmi_valid(product_version)) {
1766345233d7SLiam Girdwood 			len = strlen(card->dmi_longname);
1767345233d7SLiam Girdwood 			snprintf(card->dmi_longname + len,
1768345233d7SLiam Girdwood 				 longname_buf_size - len,
1769345233d7SLiam Girdwood 				 "-%s", product_version);
1770345233d7SLiam Girdwood 
1771345233d7SLiam Girdwood 			len++;
1772345233d7SLiam Girdwood 			if (len < longname_buf_size)
1773345233d7SLiam Girdwood 				cleanup_dmi_name(card->dmi_longname + len);
1774345233d7SLiam Girdwood 		}
1775345233d7SLiam Girdwood 	}
1776345233d7SLiam Girdwood 
1777345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
177898faf436SMengdong Lin 	if (board && is_dmi_valid(board)) {
1779345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1780345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1781345233d7SLiam Girdwood 			 longname_buf_size - len,
1782345233d7SLiam Girdwood 			 "-%s", board);
1783345233d7SLiam Girdwood 
1784345233d7SLiam Girdwood 		len++;
1785345233d7SLiam Girdwood 		if (len < longname_buf_size)
1786345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1787345233d7SLiam Girdwood 	} else if (!product) {
1788345233d7SLiam Girdwood 		/* fall back to using legacy name */
1789345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1790345233d7SLiam Girdwood 		return 0;
1791345233d7SLiam Girdwood 	}
1792345233d7SLiam Girdwood 
1793345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
1794345233d7SLiam Girdwood 	if (flavour) {
1795345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1796345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1797345233d7SLiam Girdwood 			 longname_buf_size - len,
1798345233d7SLiam Girdwood 			 "-%s", flavour);
1799345233d7SLiam Girdwood 
1800345233d7SLiam Girdwood 		len++;
1801345233d7SLiam Girdwood 		if (len < longname_buf_size)
1802345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1803345233d7SLiam Girdwood 	}
1804345233d7SLiam Girdwood 
1805345233d7SLiam Girdwood 	/* set the card long name */
1806345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
1807345233d7SLiam Girdwood 
1808345233d7SLiam Girdwood 	return 0;
1809345233d7SLiam Girdwood }
1810345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
18111f5a4535STakashi Iwai #endif /* CONFIG_DMI */
1812345233d7SLiam Girdwood 
1813a655de80SLiam Girdwood static void soc_check_tplg_fes(struct snd_soc_card *card)
1814a655de80SLiam Girdwood {
1815a655de80SLiam Girdwood 	struct snd_soc_component *component;
1816a655de80SLiam Girdwood 	const struct snd_soc_component_driver *comp_drv;
1817a655de80SLiam Girdwood 	struct snd_soc_dai_link *dai_link;
1818a655de80SLiam Girdwood 	int i;
1819a655de80SLiam Girdwood 
1820368dee94SKuninori Morimoto 	for_each_component(component) {
1821a655de80SLiam Girdwood 
1822a655de80SLiam Girdwood 		/* does this component override FEs ? */
1823a655de80SLiam Girdwood 		if (!component->driver->ignore_machine)
1824a655de80SLiam Girdwood 			continue;
1825a655de80SLiam Girdwood 
1826a655de80SLiam Girdwood 		/* for this machine ? */
1827e194098bSPierre-Louis Bossart 		if (!strcmp(component->driver->ignore_machine,
1828a655de80SLiam Girdwood 			    card->dev->driver->name))
1829e194098bSPierre-Louis Bossart 			goto match;
1830e194098bSPierre-Louis Bossart 		if (strcmp(component->driver->ignore_machine,
1831e194098bSPierre-Louis Bossart 			   dev_name(card->dev)))
1832a655de80SLiam Girdwood 			continue;
1833e194098bSPierre-Louis Bossart match:
1834a655de80SLiam Girdwood 		/* machine matches, so override the rtd data */
18357fe072b4SKuninori Morimoto 		for_each_card_prelinks(card, i, dai_link) {
1836a655de80SLiam Girdwood 
1837a655de80SLiam Girdwood 			/* ignore this FE */
1838a655de80SLiam Girdwood 			if (dai_link->dynamic) {
1839a655de80SLiam Girdwood 				dai_link->ignore = true;
1840a655de80SLiam Girdwood 				continue;
1841a655de80SLiam Girdwood 			}
1842a655de80SLiam Girdwood 
1843a655de80SLiam Girdwood 			dev_info(card->dev, "info: override FE DAI link %s\n",
1844a655de80SLiam Girdwood 				 card->dai_link[i].name);
1845a655de80SLiam Girdwood 
1846a655de80SLiam Girdwood 			/* override platform component */
1847adb76b5bSKuninori Morimoto 			if (!dai_link->platforms) {
1848daecf46eSKuninori Morimoto 				dev_err(card->dev, "init platform error");
1849daecf46eSKuninori Morimoto 				continue;
1850daecf46eSKuninori Morimoto 			}
1851910fdcabSKuninori Morimoto 			dai_link->platforms->name = component->name;
1852a655de80SLiam Girdwood 
1853a655de80SLiam Girdwood 			/* convert non BE into BE */
1854a655de80SLiam Girdwood 			dai_link->no_pcm = 1;
1855a655de80SLiam Girdwood 
1856a655de80SLiam Girdwood 			/* override any BE fixups */
1857a655de80SLiam Girdwood 			dai_link->be_hw_params_fixup =
1858a655de80SLiam Girdwood 				component->driver->be_hw_params_fixup;
1859a655de80SLiam Girdwood 
18602c7b696aSMarcel Ziswiler 			/*
18612c7b696aSMarcel Ziswiler 			 * most BE links don't set stream name, so set it to
1862a655de80SLiam Girdwood 			 * dai link name if it's NULL to help bind widgets.
1863a655de80SLiam Girdwood 			 */
1864a655de80SLiam Girdwood 			if (!dai_link->stream_name)
1865a655de80SLiam Girdwood 				dai_link->stream_name = dai_link->name;
1866a655de80SLiam Girdwood 		}
1867a655de80SLiam Girdwood 
1868a655de80SLiam Girdwood 		/* Inform userspace we are using alternate topology */
1869a655de80SLiam Girdwood 		if (component->driver->topology_name_prefix) {
1870a655de80SLiam Girdwood 
1871a655de80SLiam Girdwood 			/* topology shortname created? */
1872a655de80SLiam Girdwood 			if (!card->topology_shortname_created) {
1873a655de80SLiam Girdwood 				comp_drv = component->driver;
1874a655de80SLiam Girdwood 
1875a655de80SLiam Girdwood 				snprintf(card->topology_shortname, 32, "%s-%s",
1876a655de80SLiam Girdwood 					 comp_drv->topology_name_prefix,
1877a655de80SLiam Girdwood 					 card->name);
1878a655de80SLiam Girdwood 				card->topology_shortname_created = true;
1879a655de80SLiam Girdwood 			}
1880a655de80SLiam Girdwood 
1881a655de80SLiam Girdwood 			/* use topology shortname */
1882a655de80SLiam Girdwood 			card->name = card->topology_shortname;
1883a655de80SLiam Girdwood 		}
1884a655de80SLiam Girdwood 	}
1885a655de80SLiam Girdwood }
1886a655de80SLiam Girdwood 
188753e947a0SKuninori Morimoto static int soc_cleanup_card_resources(struct snd_soc_card *card)
188853e947a0SKuninori Morimoto {
188953e947a0SKuninori Morimoto 	/* free the ALSA card at first; this syncs with pending operations */
189029040d1aSKuninori Morimoto 	if (card->snd_card) {
189153e947a0SKuninori Morimoto 		snd_card_free(card->snd_card);
189229040d1aSKuninori Morimoto 		card->snd_card = NULL;
189329040d1aSKuninori Morimoto 	}
189453e947a0SKuninori Morimoto 
189553e947a0SKuninori Morimoto 	/* remove and free each DAI */
189653e947a0SKuninori Morimoto 	soc_remove_dai_links(card);
189753e947a0SKuninori Morimoto 	soc_remove_pcm_runtimes(card);
189853e947a0SKuninori Morimoto 
189953e947a0SKuninori Morimoto 	/* remove auxiliary devices */
190053e947a0SKuninori Morimoto 	soc_remove_aux_devices(card);
190153e947a0SKuninori Morimoto 
190253e947a0SKuninori Morimoto 	snd_soc_dapm_free(&card->dapm);
190353e947a0SKuninori Morimoto 	soc_cleanup_card_debugfs(card);
190453e947a0SKuninori Morimoto 
190553e947a0SKuninori Morimoto 	/* remove the card */
190653e947a0SKuninori Morimoto 	if (card->remove)
190753e947a0SKuninori Morimoto 		card->remove(card);
190853e947a0SKuninori Morimoto 
190953e947a0SKuninori Morimoto 	return 0;
191053e947a0SKuninori Morimoto }
191153e947a0SKuninori Morimoto 
1912b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1913f0fba2adSLiam Girdwood {
19141a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
191561b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
1916*c2b71c71SKuninori Morimoto 	struct snd_soc_aux_dev *aux;
1917ce64c8b9SLars-Peter Clausen 	int ret, i, order;
191896dd3622SMark Brown 
191934e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
192070fc5373STzung-Bi Shih 	for_each_card_prelinks(card, i, dai_link) {
192170fc5373STzung-Bi Shih 		ret = soc_init_dai_link(card, dai_link);
192270fc5373STzung-Bi Shih 		if (ret) {
192370fc5373STzung-Bi Shih 			dev_err(card->dev, "ASoC: failed to init link %s: %d\n",
192470fc5373STzung-Bi Shih 				dai_link->name, ret);
192570fc5373STzung-Bi Shih 			mutex_unlock(&client_mutex);
192670fc5373STzung-Bi Shih 			return ret;
192770fc5373STzung-Bi Shih 		}
192870fc5373STzung-Bi Shih 	}
192901b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1930f0fba2adSLiam Girdwood 
193153e947a0SKuninori Morimoto 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
193253e947a0SKuninori Morimoto 	card->dapm.dev = card->dev;
193353e947a0SKuninori Morimoto 	card->dapm.card = card;
193453e947a0SKuninori Morimoto 	list_add(&card->dapm.list, &card->dapm_list);
193553e947a0SKuninori Morimoto 
1936a655de80SLiam Girdwood 	/* check whether any platform is ignore machine FE and using topology */
1937a655de80SLiam Girdwood 	soc_check_tplg_fes(card);
1938a655de80SLiam Girdwood 
1939b19e6e7bSMark Brown 	/* bind DAIs */
19407fe072b4SKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link) {
19417fe072b4SKuninori Morimoto 		ret = soc_bind_dai_link(card, dai_link);
1942b19e6e7bSMark Brown 		if (ret != 0)
194353e947a0SKuninori Morimoto 			goto probe_end;
1944db2a4165SFrank Mandarino 	}
1945db2a4165SFrank Mandarino 
194644c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
1947*c2b71c71SKuninori Morimoto 	for_each_card_pre_auxs(card, i, aux) {
1948*c2b71c71SKuninori Morimoto 		ret = soc_bind_aux_dev(card, aux);
1949b19e6e7bSMark Brown 		if (ret != 0)
195053e947a0SKuninori Morimoto 			goto probe_end;
1951db2a4165SFrank Mandarino 	}
1952db2a4165SFrank Mandarino 
1953f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
19545b99a0aaSKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link) {
19555b99a0aaSKuninori Morimoto 		ret = snd_soc_add_dai_link(card, dai_link);
19565b99a0aaSKuninori Morimoto 		if (ret < 0)
19575b99a0aaSKuninori Morimoto 			goto probe_end;
19585b99a0aaSKuninori Morimoto 	}
1959f8f80361SMengdong Lin 
1960f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1961102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1962f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1963f0fba2adSLiam Girdwood 	if (ret < 0) {
196410e8aa9aSMichał Mirosław 		dev_err(card->dev,
196510e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
196610e8aa9aSMichał Mirosław 			card->name, ret);
196753e947a0SKuninori Morimoto 		goto probe_end;
1968db2a4165SFrank Mandarino 	}
1969db2a4165SFrank Mandarino 
19700757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
19710757d834SLars-Peter Clausen 
1972b3da4251SKuninori Morimoto 	soc_resume_init(card);
19736ed25978SAndy Green 
1974b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
19759a841ebbSMark Brown 					card->num_dapm_widgets);
1976b8ba3b57SKuninori Morimoto 	if (ret < 0)
1977b8ba3b57SKuninori Morimoto 		goto probe_end;
19789a841ebbSMark Brown 
1979b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1980f23e860eSNicolin Chen 					card->num_of_dapm_widgets);
1981b8ba3b57SKuninori Morimoto 	if (ret < 0)
1982b8ba3b57SKuninori Morimoto 		goto probe_end;
1983f23e860eSNicolin Chen 
1984f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1985f0fba2adSLiam Girdwood 	if (card->probe) {
1986e7361ec4SMark Brown 		ret = card->probe(card);
1987f0fba2adSLiam Girdwood 		if (ret < 0)
198853e947a0SKuninori Morimoto 			goto probe_end;
1989f0fba2adSLiam Girdwood 	}
1990f0fba2adSLiam Girdwood 
199162ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
19921a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1993bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
19941a497983SMengdong Lin 			ret = soc_probe_link_components(card, rtd, order);
199562ae68faSStephen Warren 			if (ret < 0) {
1996f110bfc7SLiam Girdwood 				dev_err(card->dev,
1997f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
1998f110bfc7SLiam Girdwood 					ret);
199953e947a0SKuninori Morimoto 				goto probe_end;
200062ae68faSStephen Warren 			}
200162ae68faSStephen Warren 		}
200262ae68faSStephen Warren 	}
200362ae68faSStephen Warren 
2004f2ed6b07SMengdong Lin 	/* probe auxiliary components */
2005f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
2006f2ed6b07SMengdong Lin 	if (ret < 0)
200753e947a0SKuninori Morimoto 		goto probe_end;
2008f2ed6b07SMengdong Lin 
20092c7b696aSMarcel Ziswiler 	/*
20102c7b696aSMarcel Ziswiler 	 * Find new DAI links added during probing components and bind them.
201161b0088bSMengdong Lin 	 * Components with topology may bring new DAIs and DAI links.
201261b0088bSMengdong Lin 	 */
201398061fdbSKuninori Morimoto 	for_each_card_links(card, dai_link) {
201461b0088bSMengdong Lin 		if (soc_is_dai_link_bound(card, dai_link))
201561b0088bSMengdong Lin 			continue;
201661b0088bSMengdong Lin 
201761b0088bSMengdong Lin 		ret = soc_init_dai_link(card, dai_link);
201861b0088bSMengdong Lin 		if (ret)
201953e947a0SKuninori Morimoto 			goto probe_end;
202061b0088bSMengdong Lin 		ret = soc_bind_dai_link(card, dai_link);
202161b0088bSMengdong Lin 		if (ret)
202253e947a0SKuninori Morimoto 			goto probe_end;
202361b0088bSMengdong Lin 	}
202461b0088bSMengdong Lin 
202562ae68faSStephen Warren 	/* probe all DAI links on this card */
20261a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
2027bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
20281a497983SMengdong Lin 			ret = soc_probe_link_dais(card, rtd, order);
2029fe3e78e0SMark Brown 			if (ret < 0) {
2030f110bfc7SLiam Girdwood 				dev_err(card->dev,
2031f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
2032f110bfc7SLiam Girdwood 					ret);
203353e947a0SKuninori Morimoto 				goto probe_end;
2034fe3e78e0SMark Brown 			}
2035fe3e78e0SMark Brown 		}
20360168bf0dSLiam Girdwood 	}
2037fe3e78e0SMark Brown 
2038888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
2039b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
2040888df395SMark Brown 
20419b98c7c2SKuninori Morimoto 	ret = snd_soc_add_card_controls(card, card->controls,
20422c7b696aSMarcel Ziswiler 					card->num_controls);
20439b98c7c2SKuninori Morimoto 	if (ret < 0)
20449b98c7c2SKuninori Morimoto 		goto probe_end;
2045b7af1dafSMark Brown 
2046daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2047b8ad29deSMark Brown 				      card->num_dapm_routes);
2048daa480bdSKuninori Morimoto 	if (ret < 0)
2049daa480bdSKuninori Morimoto 		goto probe_end;
2050b8ad29deSMark Brown 
2051daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2052f23e860eSNicolin Chen 				      card->num_of_dapm_routes);
2053daa480bdSKuninori Morimoto 	if (ret < 0)
2054daa480bdSKuninori Morimoto 		goto probe_end;
205575d9ac46SMark Brown 
2056861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
2057861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
2058861886d3STakashi Iwai 
2059f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
2060fe3e78e0SMark Brown 		 "%s", card->name);
2061f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
206222de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
2063f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2064f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
2065f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2066f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
2067f0e8ed85SMark Brown 		case '_':
2068f0e8ed85SMark Brown 		case '-':
2069f0e8ed85SMark Brown 		case '\0':
2070f0e8ed85SMark Brown 			break;
2071f0e8ed85SMark Brown 		default:
2072f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
2073f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
2074f0e8ed85SMark Brown 			break;
2075f0e8ed85SMark Brown 		}
2076f0e8ed85SMark Brown 	}
2077fe3e78e0SMark Brown 
207828e9ad92SMark Brown 	if (card->late_probe) {
207928e9ad92SMark Brown 		ret = card->late_probe(card);
208028e9ad92SMark Brown 		if (ret < 0) {
2081f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
208228e9ad92SMark Brown 				card->name, ret);
208353e947a0SKuninori Morimoto 			goto probe_end;
208428e9ad92SMark Brown 		}
208528e9ad92SMark Brown 	}
208628e9ad92SMark Brown 
2087824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
20888c193b8dSLars-Peter Clausen 
2089f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2090fe3e78e0SMark Brown 	if (ret < 0) {
2091f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2092f110bfc7SLiam Girdwood 				ret);
209353e947a0SKuninori Morimoto 		goto probe_end;
2094fe3e78e0SMark Brown 	}
2095fe3e78e0SMark Brown 
2096435c5e25SMark Brown 	card->instantiated = 1;
2097882eab6cSTzung-Bi Shih 	dapm_mark_endpoints_dirty(card);
20984f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2099b19e6e7bSMark Brown 
210053e947a0SKuninori Morimoto probe_end:
210153e947a0SKuninori Morimoto 	if (ret < 0)
210253e947a0SKuninori Morimoto 		soc_cleanup_card_resources(card);
2103db2a4165SFrank Mandarino 
2104f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
210534e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2106db2a4165SFrank Mandarino 
2107b19e6e7bSMark Brown 	return ret;
2108435c5e25SMark Brown }
2109435c5e25SMark Brown 
2110435c5e25SMark Brown /* probes a new socdev */
2111435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2112435c5e25SMark Brown {
2113f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2114435c5e25SMark Brown 
211570a7ca34SVinod Koul 	/*
211670a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
211770a7ca34SVinod Koul 	 * we should not be here in that case so ret error
211870a7ca34SVinod Koul 	 */
211970a7ca34SVinod Koul 	if (!card)
212070a7ca34SVinod Koul 		return -EINVAL;
212170a7ca34SVinod Koul 
2122fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2123f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2124fe4085e8SMark Brown 		 card->name);
2125fe4085e8SMark Brown 
2126435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2127435c5e25SMark Brown 	card->dev = &pdev->dev;
2128f0fba2adSLiam Girdwood 
212928d528c8SMark Brown 	return snd_soc_register_card(card);
2130435c5e25SMark Brown }
2131435c5e25SMark Brown 
2132b0e26485SVinod Koul /* removes a socdev */
2133b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2134b0e26485SVinod Koul {
2135b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2136b0e26485SVinod Koul 
2137c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2138db2a4165SFrank Mandarino 	return 0;
2139db2a4165SFrank Mandarino }
2140db2a4165SFrank Mandarino 
21416f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
214251737470SMark Brown {
21436f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
21441a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
214551737470SMark Brown 
214651737470SMark Brown 	if (!card->instantiated)
2147416356fcSMark Brown 		return 0;
214851737470SMark Brown 
21492c7b696aSMarcel Ziswiler 	/*
21502c7b696aSMarcel Ziswiler 	 * Flush out pmdown_time work - we actually do want to run it
21512c7b696aSMarcel Ziswiler 	 * now, we're shutting down so no imminent restart.
21522c7b696aSMarcel Ziswiler 	 */
215365462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
215451737470SMark Brown 
2155f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2156416356fcSMark Brown 
2157988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
2158bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
215988bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
21600b7990e3SKuninori Morimoto 		struct snd_soc_dai *codec_dai;
21611a497983SMengdong Lin 		int i;
216288bd870fSBenoit Cousson 
2163988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
21640b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
216588bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
216688bd870fSBenoit Cousson 		}
2167988e8cc4SNicolin Chen 	}
2168988e8cc4SNicolin Chen 
2169416356fcSMark Brown 	return 0;
217051737470SMark Brown }
21716f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
217251737470SMark Brown 
21736f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2174b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2175b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2176b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2177b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
21786f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2179b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2180416356fcSMark Brown };
2181deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2182416356fcSMark Brown 
2183db2a4165SFrank Mandarino /* ASoC platform driver */
2184db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2185db2a4165SFrank Mandarino 	.driver		= {
2186db2a4165SFrank Mandarino 		.name		= "soc-audio",
21876f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2188db2a4165SFrank Mandarino 	},
2189db2a4165SFrank Mandarino 	.probe		= soc_probe,
2190db2a4165SFrank Mandarino 	.remove		= soc_remove,
2191db2a4165SFrank Mandarino };
2192db2a4165SFrank Mandarino 
2193096e49d5SMark Brown /**
2194db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2195db2a4165SFrank Mandarino  * @_template: control template
2196db2a4165SFrank Mandarino  * @data: control private data
2197ac11a2b3SMark Brown  * @long_name: control long name
2198efb7ac3fSMark Brown  * @prefix: control name prefix
2199db2a4165SFrank Mandarino  *
2200db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2201db2a4165SFrank Mandarino  *
2202db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2203db2a4165SFrank Mandarino  */
2204db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
22053056557fSMark Brown 				  void *data, const char *long_name,
2206efb7ac3fSMark Brown 				  const char *prefix)
2207db2a4165SFrank Mandarino {
2208db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2209efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2210efb7ac3fSMark Brown 	char *name = NULL;
2211db2a4165SFrank Mandarino 
2212db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2213db2a4165SFrank Mandarino 	template.index = 0;
2214db2a4165SFrank Mandarino 
2215efb7ac3fSMark Brown 	if (!long_name)
2216efb7ac3fSMark Brown 		long_name = template.name;
2217efb7ac3fSMark Brown 
2218efb7ac3fSMark Brown 	if (prefix) {
22192b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2220efb7ac3fSMark Brown 		if (!name)
2221efb7ac3fSMark Brown 			return NULL;
2222efb7ac3fSMark Brown 
2223efb7ac3fSMark Brown 		template.name = name;
2224efb7ac3fSMark Brown 	} else {
2225efb7ac3fSMark Brown 		template.name = long_name;
2226efb7ac3fSMark Brown 	}
2227efb7ac3fSMark Brown 
2228efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2229efb7ac3fSMark Brown 
2230efb7ac3fSMark Brown 	kfree(name);
2231efb7ac3fSMark Brown 
2232efb7ac3fSMark Brown 	return kcontrol;
2233db2a4165SFrank Mandarino }
2234db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2235db2a4165SFrank Mandarino 
2236022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2237022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2238022658beSLiam Girdwood 	const char *prefix, void *data)
2239022658beSLiam Girdwood {
2240022658beSLiam Girdwood 	int err, i;
2241022658beSLiam Girdwood 
2242022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2243022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
22442c7b696aSMarcel Ziswiler 
2245022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2246022658beSLiam Girdwood 						     control->name, prefix));
2247022658beSLiam Girdwood 		if (err < 0) {
2248f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2249f110bfc7SLiam Girdwood 				control->name, err);
2250022658beSLiam Girdwood 			return err;
2251022658beSLiam Girdwood 		}
2252022658beSLiam Girdwood 	}
2253022658beSLiam Girdwood 
2254022658beSLiam Girdwood 	return 0;
2255022658beSLiam Girdwood }
2256022658beSLiam Girdwood 
22574fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
22584fefd698SDimitris Papastamos 					       const char *name)
22594fefd698SDimitris Papastamos {
22604fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
22614fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
22624fefd698SDimitris Papastamos 
22634fefd698SDimitris Papastamos 	if (unlikely(!name))
22644fefd698SDimitris Papastamos 		return NULL;
22654fefd698SDimitris Papastamos 
22664fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
22674fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
22684fefd698SDimitris Papastamos 			return kctl;
22694fefd698SDimitris Papastamos 	return NULL;
22704fefd698SDimitris Papastamos }
22714fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
22724fefd698SDimitris Papastamos 
2273db2a4165SFrank Mandarino /**
22740f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
22750f2780adSLars-Peter Clausen  *
22760f2780adSLars-Peter Clausen  * @component: Component to add controls to
22770f2780adSLars-Peter Clausen  * @controls: Array of controls to add
22780f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
22790f2780adSLars-Peter Clausen  *
22800f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
22810f2780adSLars-Peter Clausen  */
22820f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
22830f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
22840f2780adSLars-Peter Clausen {
22850f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
22860f2780adSLars-Peter Clausen 
22870f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
22880f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
22890f2780adSLars-Peter Clausen }
22900f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
22910f2780adSLars-Peter Clausen 
22920f2780adSLars-Peter Clausen /**
2293022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2294022658beSLiam Girdwood  * Convenience function to add a list of controls.
2295022658beSLiam Girdwood  *
2296022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2297022658beSLiam Girdwood  * @controls: array of controls to add
2298022658beSLiam Girdwood  * @num_controls: number of elements in the array
2299022658beSLiam Girdwood  *
2300022658beSLiam Girdwood  * Return 0 for success, else error.
2301022658beSLiam Girdwood  */
2302022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2303022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2304022658beSLiam Girdwood {
2305022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2306022658beSLiam Girdwood 
2307022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2308022658beSLiam Girdwood 			NULL, soc_card);
2309022658beSLiam Girdwood }
2310022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2311022658beSLiam Girdwood 
2312022658beSLiam Girdwood /**
2313022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2314022658beSLiam Girdwood  * Convienience function to add a list of controls.
2315022658beSLiam Girdwood  *
2316022658beSLiam Girdwood  * @dai: DAI to add controls to
2317022658beSLiam Girdwood  * @controls: array of controls to add
2318022658beSLiam Girdwood  * @num_controls: number of elements in the array
2319022658beSLiam Girdwood  *
2320022658beSLiam Girdwood  * Return 0 for success, else error.
2321022658beSLiam Girdwood  */
2322022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2323022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2324022658beSLiam Girdwood {
2325313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2326022658beSLiam Girdwood 
2327022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2328022658beSLiam Girdwood 			NULL, dai);
2329022658beSLiam Girdwood }
2330022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2331022658beSLiam Girdwood 
2332e894efefSSrinivas Kandagatla static int snd_soc_bind_card(struct snd_soc_card *card)
2333e894efefSSrinivas Kandagatla {
2334e894efefSSrinivas Kandagatla 	struct snd_soc_pcm_runtime *rtd;
2335e894efefSSrinivas Kandagatla 	int ret;
2336e894efefSSrinivas Kandagatla 
2337e894efefSSrinivas Kandagatla 	ret = snd_soc_instantiate_card(card);
2338e894efefSSrinivas Kandagatla 	if (ret != 0)
2339e894efefSSrinivas Kandagatla 		return ret;
2340e894efefSSrinivas Kandagatla 
2341e894efefSSrinivas Kandagatla 	/* deactivate pins to sleep state */
2342bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
2343e894efefSSrinivas Kandagatla 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2344e894efefSSrinivas Kandagatla 		struct snd_soc_dai *codec_dai;
2345e894efefSSrinivas Kandagatla 		int j;
2346e894efefSSrinivas Kandagatla 
2347e894efefSSrinivas Kandagatla 		for_each_rtd_codec_dai(rtd, j, codec_dai) {
2348e894efefSSrinivas Kandagatla 			if (!codec_dai->active)
2349e894efefSSrinivas Kandagatla 				pinctrl_pm_select_sleep_state(codec_dai->dev);
2350e894efefSSrinivas Kandagatla 		}
2351e894efefSSrinivas Kandagatla 
2352e894efefSSrinivas Kandagatla 		if (!cpu_dai->active)
2353e894efefSSrinivas Kandagatla 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
2354e894efefSSrinivas Kandagatla 	}
2355e894efefSSrinivas Kandagatla 
2356e894efefSSrinivas Kandagatla 	return ret;
2357e894efefSSrinivas Kandagatla }
2358e894efefSSrinivas Kandagatla 
2359c5af3a2eSMark Brown /**
2360c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2361c5af3a2eSMark Brown  *
2362ac11a2b3SMark Brown  * @card: Card to register
2363c5af3a2eSMark Brown  *
2364c5af3a2eSMark Brown  */
236570a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2366c5af3a2eSMark Brown {
2367c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2368c5af3a2eSMark Brown 		return -EINVAL;
2369c5af3a2eSMark Brown 
2370ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2371ed77cc12SMark Brown 
2372111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
2373111c6419SStephen Warren 
2374f8f80361SMengdong Lin 	INIT_LIST_HEAD(&card->dai_link_list);
2375f0fba2adSLiam Girdwood 
23761a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
23779115171aSMark Brown 	card->num_rtd = 0;
23788c6529dbSLiam Girdwood 
2379db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
23808a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2381dee89c4dSMark Brown 	card->instantiated = 0;
2382dee89c4dSMark Brown 	mutex_init(&card->mutex);
2383a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
2384a9764869SKaiChieh Chuang 	spin_lock_init(&card->dpcm_lock);
23858c6529dbSLiam Girdwood 
2386e894efefSSrinivas Kandagatla 	return snd_soc_bind_card(card);
23878c6529dbSLiam Girdwood }
238870a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
23898c6529dbSLiam Girdwood 
2390e894efefSSrinivas Kandagatla static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2391e894efefSSrinivas Kandagatla {
2392f96fb7d1SRanjani Sridharan 	struct snd_soc_pcm_runtime *rtd;
2393f96fb7d1SRanjani Sridharan 	int order;
2394f96fb7d1SRanjani Sridharan 
2395e894efefSSrinivas Kandagatla 	if (card->instantiated) {
2396e894efefSSrinivas Kandagatla 		card->instantiated = false;
2397e894efefSSrinivas Kandagatla 		snd_soc_dapm_shutdown(card);
239853e947a0SKuninori Morimoto 		snd_soc_flush_all_delayed_work(card);
2399f96fb7d1SRanjani Sridharan 
2400f96fb7d1SRanjani Sridharan 		/* remove all components used by DAI links on this card */
2401f96fb7d1SRanjani Sridharan 		for_each_comp_order(order) {
2402f96fb7d1SRanjani Sridharan 			for_each_card_rtds(card, rtd) {
2403f96fb7d1SRanjani Sridharan 				soc_remove_link_components(card, rtd, order);
2404f96fb7d1SRanjani Sridharan 			}
2405f96fb7d1SRanjani Sridharan 		}
2406f96fb7d1SRanjani Sridharan 
2407e894efefSSrinivas Kandagatla 		soc_cleanup_card_resources(card);
2408e894efefSSrinivas Kandagatla 		if (!unregister)
2409e894efefSSrinivas Kandagatla 			list_add(&card->list, &unbind_card_list);
2410e894efefSSrinivas Kandagatla 	} else {
2411e894efefSSrinivas Kandagatla 		if (unregister)
2412e894efefSSrinivas Kandagatla 			list_del(&card->list);
2413e894efefSSrinivas Kandagatla 	}
2414e894efefSSrinivas Kandagatla }
2415e894efefSSrinivas Kandagatla 
24168c6529dbSLiam Girdwood /**
24178c6529dbSLiam Girdwood  * snd_soc_unregister_card - Unregister a card with the ASoC core
24188c6529dbSLiam Girdwood  *
2419dee89c4dSMark Brown  * @card: Card to unregister
2420dee89c4dSMark Brown  *
24218c6529dbSLiam Girdwood  */
242270a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
24238c6529dbSLiam Girdwood {
2424b545542aSKuninori Morimoto 	mutex_lock(&client_mutex);
2425e894efefSSrinivas Kandagatla 	snd_soc_unbind_card(card, true);
2426b545542aSKuninori Morimoto 	mutex_unlock(&client_mutex);
2427f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
24288c6529dbSLiam Girdwood 
24298c6529dbSLiam Girdwood 	return 0;
24308c6529dbSLiam Girdwood }
243170a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
24328c6529dbSLiam Girdwood 
24338c6529dbSLiam Girdwood /*
24348c6529dbSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
24358c6529dbSLiam Girdwood  * and sanitizing names.
24368c6529dbSLiam Girdwood  */
24370b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
24388c6529dbSLiam Girdwood {
24398c6529dbSLiam Girdwood 	char *found, name[NAME_SIZE];
24408c6529dbSLiam Girdwood 	int id1, id2;
24418c6529dbSLiam Girdwood 
24428c6529dbSLiam Girdwood 	if (dev_name(dev) == NULL)
24438c6529dbSLiam Girdwood 		return NULL;
24448c6529dbSLiam Girdwood 
244558818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
24468c6529dbSLiam Girdwood 
24478c6529dbSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
2448c5af3a2eSMark Brown 	found = strstr(name, dev->driver->name);
2449c5af3a2eSMark Brown 	if (found) {
2450c5af3a2eSMark Brown 		/* get ID */
2451c5af3a2eSMark Brown 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2452c5af3a2eSMark Brown 
2453c5af3a2eSMark Brown 			/* discard ID from name if ID == -1 */
2454c5af3a2eSMark Brown 			if (*id == -1)
2455c5af3a2eSMark Brown 				found[strlen(dev->driver->name)] = '\0';
2456c5af3a2eSMark Brown 		}
2457c5af3a2eSMark Brown 
2458c5af3a2eSMark Brown 	} else {
2459c5af3a2eSMark Brown 		/* I2C component devices are named "bus-addr" */
2460c5af3a2eSMark Brown 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2461c5af3a2eSMark Brown 			char tmp[NAME_SIZE];
2462c5af3a2eSMark Brown 
2463c5af3a2eSMark Brown 			/* create unique ID number from I2C addr and bus */
2464c5af3a2eSMark Brown 			*id = ((id1 & 0xffff) << 16) + id2;
2465c5af3a2eSMark Brown 
2466c5af3a2eSMark Brown 			/* sanitize component name for DAI link creation */
24672c7b696aSMarcel Ziswiler 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
24682c7b696aSMarcel Ziswiler 				 name);
246958818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2470c5af3a2eSMark Brown 		} else
2471c5af3a2eSMark Brown 			*id = 0;
2472c5af3a2eSMark Brown 	}
2473c5af3a2eSMark Brown 
2474c5af3a2eSMark Brown 	return kstrdup(name, GFP_KERNEL);
2475c5af3a2eSMark Brown }
2476c5af3a2eSMark Brown 
2477c5af3a2eSMark Brown /*
2478c5af3a2eSMark Brown  * Simplify DAI link naming for single devices with multiple DAIs by removing
2479c5af3a2eSMark Brown  * any ".-1" and using the DAI name (instead of device name).
2480c5af3a2eSMark Brown  */
2481c5af3a2eSMark Brown static inline char *fmt_multiple_name(struct device *dev,
2482c5af3a2eSMark Brown 		struct snd_soc_dai_driver *dai_drv)
2483c5af3a2eSMark Brown {
2484c5af3a2eSMark Brown 	if (dai_drv->name == NULL) {
248510e8aa9aSMichał Mirosław 		dev_err(dev,
248610e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
248710e8aa9aSMichał Mirosław 			dev_name(dev));
2488c5af3a2eSMark Brown 		return NULL;
2489c5af3a2eSMark Brown 	}
2490c5af3a2eSMark Brown 
2491c5af3a2eSMark Brown 	return kstrdup(dai_drv->name, GFP_KERNEL);
2492c5af3a2eSMark Brown }
2493c5af3a2eSMark Brown 
2494c5af3a2eSMark Brown /**
249532c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
24969115171aSMark Brown  *
249732c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
24989115171aSMark Brown  */
249932c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
25009115171aSMark Brown {
25015c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
25029115171aSMark Brown 
250315a0c645SKuninori Morimoto 	for_each_component_dais_safe(component, dai, _dai) {
250432c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
250532c9ba54SLars-Peter Clausen 			dai->name);
25069115171aSMark Brown 		list_del(&dai->list);
2507f0fba2adSLiam Girdwood 		kfree(dai->name);
2508f0fba2adSLiam Girdwood 		kfree(dai);
25099115171aSMark Brown 	}
251032c9ba54SLars-Peter Clausen }
25119115171aSMark Brown 
25125e4fb372SMengdong Lin /* Create a DAI and add it to the component's DAI list */
25135e4fb372SMengdong Lin static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
25145e4fb372SMengdong Lin 	struct snd_soc_dai_driver *dai_drv,
25155e4fb372SMengdong Lin 	bool legacy_dai_naming)
25165e4fb372SMengdong Lin {
25175e4fb372SMengdong Lin 	struct device *dev = component->dev;
25185e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
25195e4fb372SMengdong Lin 
25205e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
25215e4fb372SMengdong Lin 
25225e4fb372SMengdong Lin 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
25235e4fb372SMengdong Lin 	if (dai == NULL)
25245e4fb372SMengdong Lin 		return NULL;
25255e4fb372SMengdong Lin 
25265e4fb372SMengdong Lin 	/*
25275e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
25285e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
25295e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
25305e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
25315e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
25325e4fb372SMengdong Lin 	 * component-less anymore.
25335e4fb372SMengdong Lin 	 */
25345e4fb372SMengdong Lin 	if (legacy_dai_naming &&
25355e4fb372SMengdong Lin 	    (dai_drv->id == 0 || dai_drv->name == NULL)) {
25365e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
25375e4fb372SMengdong Lin 	} else {
25385e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
25395e4fb372SMengdong Lin 		if (dai_drv->id)
25405e4fb372SMengdong Lin 			dai->id = dai_drv->id;
25415e4fb372SMengdong Lin 		else
25425e4fb372SMengdong Lin 			dai->id = component->num_dai;
25435e4fb372SMengdong Lin 	}
25445e4fb372SMengdong Lin 	if (dai->name == NULL) {
25455e4fb372SMengdong Lin 		kfree(dai);
25465e4fb372SMengdong Lin 		return NULL;
25475e4fb372SMengdong Lin 	}
25485e4fb372SMengdong Lin 
25495e4fb372SMengdong Lin 	dai->component = component;
25505e4fb372SMengdong Lin 	dai->dev = dev;
25515e4fb372SMengdong Lin 	dai->driver = dai_drv;
25525e4fb372SMengdong Lin 	if (!dai->driver->ops)
25535e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
25545e4fb372SMengdong Lin 
255515a0c645SKuninori Morimoto 	/* see for_each_component_dais */
255658bf4179SKuninori Morimoto 	list_add_tail(&dai->list, &component->dai_list);
25575e4fb372SMengdong Lin 	component->num_dai++;
25585e4fb372SMengdong Lin 
25595e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
25605e4fb372SMengdong Lin 	return dai;
25615e4fb372SMengdong Lin }
25625e4fb372SMengdong Lin 
25639115171aSMark Brown /**
256432c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
25659115171aSMark Brown  *
25666106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
25676106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
2568ac11a2b3SMark Brown  * @count: Number of DAIs
25699115171aSMark Brown  */
257032c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
25712c7b696aSMarcel Ziswiler 				 struct snd_soc_dai_driver *dai_drv,
25722c7b696aSMarcel Ziswiler 				 size_t count)
25739115171aSMark Brown {
25746106d129SLars-Peter Clausen 	struct device *dev = component->dev;
2575f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
257632c9ba54SLars-Peter Clausen 	unsigned int i;
257732c9ba54SLars-Peter Clausen 	int ret;
2578f0fba2adSLiam Girdwood 
25795b5e0928SAlexey Dobriyan 	dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
25809115171aSMark Brown 
25819115171aSMark Brown 	for (i = 0; i < count; i++) {
2582f0fba2adSLiam Girdwood 
25832c7b696aSMarcel Ziswiler 		dai = soc_add_dai(component, dai_drv + i, count == 1 &&
25842c7b696aSMarcel Ziswiler 				  !component->driver->non_legacy_dai_naming);
2585c46e0079SAxel Lin 		if (dai == NULL) {
2586c46e0079SAxel Lin 			ret = -ENOMEM;
2587c46e0079SAxel Lin 			goto err;
2588c46e0079SAxel Lin 		}
2589f0fba2adSLiam Girdwood 	}
2590f0fba2adSLiam Girdwood 
25919115171aSMark Brown 	return 0;
25929115171aSMark Brown 
25939115171aSMark Brown err:
259432c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
25959115171aSMark Brown 
25969115171aSMark Brown 	return ret;
25979115171aSMark Brown }
25989115171aSMark Brown 
259968003e6cSMengdong Lin /**
260068003e6cSMengdong Lin  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
260168003e6cSMengdong Lin  *
260268003e6cSMengdong Lin  * @component: The component the DAIs are registered for
260368003e6cSMengdong Lin  * @dai_drv: DAI driver to use for the DAI
260468003e6cSMengdong Lin  *
260568003e6cSMengdong Lin  * Topology can use this API to register DAIs when probing a component.
260668003e6cSMengdong Lin  * These DAIs's widgets will be freed in the card cleanup and the DAIs
260768003e6cSMengdong Lin  * will be freed in the component cleanup.
260868003e6cSMengdong Lin  */
260968003e6cSMengdong Lin int snd_soc_register_dai(struct snd_soc_component *component,
261068003e6cSMengdong Lin 	struct snd_soc_dai_driver *dai_drv)
261168003e6cSMengdong Lin {
261268003e6cSMengdong Lin 	struct snd_soc_dapm_context *dapm =
261368003e6cSMengdong Lin 		snd_soc_component_get_dapm(component);
261468003e6cSMengdong Lin 	struct snd_soc_dai *dai;
261568003e6cSMengdong Lin 	int ret;
261668003e6cSMengdong Lin 
261768003e6cSMengdong Lin 	if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
261868003e6cSMengdong Lin 		dev_err(component->dev, "Invalid dai type %d\n",
261968003e6cSMengdong Lin 			dai_drv->dobj.type);
262068003e6cSMengdong Lin 		return -EINVAL;
262168003e6cSMengdong Lin 	}
262268003e6cSMengdong Lin 
262368003e6cSMengdong Lin 	lockdep_assert_held(&client_mutex);
262468003e6cSMengdong Lin 	dai = soc_add_dai(component, dai_drv, false);
262568003e6cSMengdong Lin 	if (!dai)
262668003e6cSMengdong Lin 		return -ENOMEM;
262768003e6cSMengdong Lin 
26282c7b696aSMarcel Ziswiler 	/*
26292c7b696aSMarcel Ziswiler 	 * Create the DAI widgets here. After adding DAIs, topology may
263068003e6cSMengdong Lin 	 * also add routes that need these widgets as source or sink.
263168003e6cSMengdong Lin 	 */
263268003e6cSMengdong Lin 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
263368003e6cSMengdong Lin 	if (ret != 0) {
263468003e6cSMengdong Lin 		dev_err(component->dev,
263568003e6cSMengdong Lin 			"Failed to create DAI widgets %d\n", ret);
263668003e6cSMengdong Lin 	}
263768003e6cSMengdong Lin 
263868003e6cSMengdong Lin 	return ret;
263968003e6cSMengdong Lin }
264068003e6cSMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_register_dai);
264168003e6cSMengdong Lin 
2642bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
2643bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
2644d191bd8dSKuninori Morimoto {
2645ce0fc93aSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
2646ce0fc93aSLars-Peter Clausen 
2647bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
2648bb13109dSLars-Peter Clausen 	if (!component->name) {
2649bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
2650d191bd8dSKuninori Morimoto 		return -ENOMEM;
2651d191bd8dSKuninori Morimoto 	}
2652d191bd8dSKuninori Morimoto 
2653bb13109dSLars-Peter Clausen 	component->dev = dev;
2654bb13109dSLars-Peter Clausen 	component->driver = driver;
2655e2c330b9SLars-Peter Clausen 
265688c27465SKuninori Morimoto 	dapm = snd_soc_component_get_dapm(component);
2657ce0fc93aSLars-Peter Clausen 	dapm->dev = dev;
2658ce0fc93aSLars-Peter Clausen 	dapm->component = component;
2659ce0fc93aSLars-Peter Clausen 	dapm->bias_level = SND_SOC_BIAS_OFF;
26607ba236ceSKuninori Morimoto 	dapm->idle_bias_off = !driver->idle_bias_on;
26617ba236ceSKuninori Morimoto 	dapm->suspend_bias_off = driver->suspend_bias_off;
2662ce0fc93aSLars-Peter Clausen 
2663bb13109dSLars-Peter Clausen 	INIT_LIST_HEAD(&component->dai_list);
2664bb13109dSLars-Peter Clausen 	mutex_init(&component->io_mutex);
2665bb13109dSLars-Peter Clausen 
2666bb13109dSLars-Peter Clausen 	return 0;
2667d191bd8dSKuninori Morimoto }
2668d191bd8dSKuninori Morimoto 
266920feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2670886f5692SLars-Peter Clausen {
2671886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
267220feb881SLars-Peter Clausen 
2673886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
2674886f5692SLars-Peter Clausen 	if (val_bytes > 0)
2675886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
2676886f5692SLars-Peter Clausen }
267720feb881SLars-Peter Clausen 
2678e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
2679e874bf5fSLars-Peter Clausen 
268020feb881SLars-Peter Clausen /**
26812c7b696aSMarcel Ziswiler  * snd_soc_component_init_regmap() - Initialize regmap instance for the
26822c7b696aSMarcel Ziswiler  *                                   component
268320feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
268420feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
268520feb881SLars-Peter Clausen  *
268620feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
268720feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
268820feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
268920feb881SLars-Peter Clausen  * before the first IO attempt of the component.
269020feb881SLars-Peter Clausen  */
269120feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
269220feb881SLars-Peter Clausen 	struct regmap *regmap)
269320feb881SLars-Peter Clausen {
269420feb881SLars-Peter Clausen 	component->regmap = regmap;
269520feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
2696886f5692SLars-Peter Clausen }
269720feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
269820feb881SLars-Peter Clausen 
269920feb881SLars-Peter Clausen /**
27002c7b696aSMarcel Ziswiler  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
27012c7b696aSMarcel Ziswiler  *                                   component
270220feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
270320feb881SLars-Peter Clausen  *
270420feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
270520feb881SLars-Peter Clausen  * removes the regmap instance from the component.
270620feb881SLars-Peter Clausen  *
270720feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
270820feb881SLars-Peter Clausen  * to initialize the regmap instance.
270920feb881SLars-Peter Clausen  */
271020feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
271120feb881SLars-Peter Clausen {
271220feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
271320feb881SLars-Peter Clausen 	component->regmap = NULL;
271420feb881SLars-Peter Clausen }
271520feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2716886f5692SLars-Peter Clausen 
2717e874bf5fSLars-Peter Clausen #endif
2718d191bd8dSKuninori Morimoto 
2719359c71eeSKuninori Morimoto static void snd_soc_component_add(struct snd_soc_component *component)
2720bb13109dSLars-Peter Clausen {
2721359c71eeSKuninori Morimoto 	mutex_lock(&client_mutex);
2722359c71eeSKuninori Morimoto 
2723999f7f5aSKuninori Morimoto 	if (!component->driver->write && !component->driver->read) {
272420feb881SLars-Peter Clausen 		if (!component->regmap)
27252c7b696aSMarcel Ziswiler 			component->regmap = dev_get_regmap(component->dev,
27262c7b696aSMarcel Ziswiler 							   NULL);
272720feb881SLars-Peter Clausen 		if (component->regmap)
272820feb881SLars-Peter Clausen 			snd_soc_component_setup_regmap(component);
272920feb881SLars-Peter Clausen 	}
2730886f5692SLars-Peter Clausen 
2731368dee94SKuninori Morimoto 	/* see for_each_component */
2732bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
27338a978234SLiam Girdwood 	INIT_LIST_HEAD(&component->dobj_list);
2734d191bd8dSKuninori Morimoto 
2735d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
2736bb13109dSLars-Peter Clausen }
2737d191bd8dSKuninori Morimoto 
2738bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
2739bb13109dSLars-Peter Clausen {
2740bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
2741bb13109dSLars-Peter Clausen 	kfree(component->name);
2742bb13109dSLars-Peter Clausen }
2743d191bd8dSKuninori Morimoto 
2744bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2745bb13109dSLars-Peter Clausen {
2746c12c1aadSKuninori Morimoto 	struct snd_soc_card *card = component->card;
2747c12c1aadSKuninori Morimoto 
2748c12c1aadSKuninori Morimoto 	if (card)
2749e894efefSSrinivas Kandagatla 		snd_soc_unbind_card(card, false);
2750c12c1aadSKuninori Morimoto 
2751bb13109dSLars-Peter Clausen 	list_del(&component->list);
2752bb13109dSLars-Peter Clausen }
2753d191bd8dSKuninori Morimoto 
2754273d778eSKuninori Morimoto #define ENDIANNESS_MAP(name) \
2755273d778eSKuninori Morimoto 	(SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2756273d778eSKuninori Morimoto static u64 endianness_format_map[] = {
2757273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S16_),
2758273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U16_),
2759273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_),
2760273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_),
2761273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S32_),
2762273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U32_),
2763273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_3),
2764273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_3),
2765273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S20_3),
2766273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U20_3),
2767273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S18_3),
2768273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U18_3),
2769273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT_),
2770273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT64_),
2771273d778eSKuninori Morimoto 	ENDIANNESS_MAP(IEC958_SUBFRAME_),
2772273d778eSKuninori Morimoto };
2773273d778eSKuninori Morimoto 
2774273d778eSKuninori Morimoto /*
2775273d778eSKuninori Morimoto  * Fix up the DAI formats for endianness: codecs don't actually see
2776273d778eSKuninori Morimoto  * the endianness of the data but we're using the CPU format
2777273d778eSKuninori Morimoto  * definitions which do need to include endianness so we ensure that
2778273d778eSKuninori Morimoto  * codec DAIs always have both big and little endian variants set.
2779273d778eSKuninori Morimoto  */
2780273d778eSKuninori Morimoto static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2781273d778eSKuninori Morimoto {
2782273d778eSKuninori Morimoto 	int i;
2783273d778eSKuninori Morimoto 
2784273d778eSKuninori Morimoto 	for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2785273d778eSKuninori Morimoto 		if (stream->formats & endianness_format_map[i])
2786273d778eSKuninori Morimoto 			stream->formats |= endianness_format_map[i];
2787273d778eSKuninori Morimoto }
2788273d778eSKuninori Morimoto 
2789e894efefSSrinivas Kandagatla static void snd_soc_try_rebind_card(void)
2790e894efefSSrinivas Kandagatla {
2791e894efefSSrinivas Kandagatla 	struct snd_soc_card *card, *c;
2792e894efefSSrinivas Kandagatla 
2793b245d273SKuninori Morimoto 	list_for_each_entry_safe(card, c, &unbind_card_list, list)
2794e894efefSSrinivas Kandagatla 		if (!snd_soc_bind_card(card))
2795e894efefSSrinivas Kandagatla 			list_del(&card->list);
2796e894efefSSrinivas Kandagatla }
2797e894efefSSrinivas Kandagatla 
2798e0dac41bSKuninori Morimoto int snd_soc_add_component(struct device *dev,
2799e0dac41bSKuninori Morimoto 			struct snd_soc_component *component,
2800cf9e829eSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2801d191bd8dSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2802d191bd8dSKuninori Morimoto 			int num_dai)
2803d191bd8dSKuninori Morimoto {
2804bb13109dSLars-Peter Clausen 	int ret;
2805273d778eSKuninori Morimoto 	int i;
2806d191bd8dSKuninori Morimoto 
2807cf9e829eSKuninori Morimoto 	ret = snd_soc_component_initialize(component, component_driver, dev);
2808bb13109dSLars-Peter Clausen 	if (ret)
2809bb13109dSLars-Peter Clausen 		goto err_free;
2810bb13109dSLars-Peter Clausen 
2811273d778eSKuninori Morimoto 	if (component_driver->endianness) {
2812273d778eSKuninori Morimoto 		for (i = 0; i < num_dai; i++) {
2813273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].playback);
2814273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].capture);
2815273d778eSKuninori Morimoto 		}
2816273d778eSKuninori Morimoto 	}
2817273d778eSKuninori Morimoto 
28180e7b25c6SKuninori Morimoto 	ret = snd_soc_register_dais(component, dai_drv, num_dai);
2819bb13109dSLars-Peter Clausen 	if (ret < 0) {
2820f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
2821bb13109dSLars-Peter Clausen 		goto err_cleanup;
2822bb13109dSLars-Peter Clausen 	}
2823bb13109dSLars-Peter Clausen 
2824cf9e829eSKuninori Morimoto 	snd_soc_component_add(component);
2825e894efefSSrinivas Kandagatla 	snd_soc_try_rebind_card();
2826bb13109dSLars-Peter Clausen 
2827bb13109dSLars-Peter Clausen 	return 0;
2828bb13109dSLars-Peter Clausen 
2829bb13109dSLars-Peter Clausen err_cleanup:
2830cf9e829eSKuninori Morimoto 	snd_soc_component_cleanup(component);
2831bb13109dSLars-Peter Clausen err_free:
2832bb13109dSLars-Peter Clausen 	return ret;
2833d191bd8dSKuninori Morimoto }
2834e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component);
2835e0dac41bSKuninori Morimoto 
2836e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev,
2837e0dac41bSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2838e0dac41bSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2839e0dac41bSKuninori Morimoto 			int num_dai)
2840e0dac41bSKuninori Morimoto {
2841e0dac41bSKuninori Morimoto 	struct snd_soc_component *component;
2842e0dac41bSKuninori Morimoto 
28437ecbd6a9SKuninori Morimoto 	component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
284408e61d03SKuninori Morimoto 	if (!component)
2845e0dac41bSKuninori Morimoto 		return -ENOMEM;
2846e0dac41bSKuninori Morimoto 
2847e0dac41bSKuninori Morimoto 	return snd_soc_add_component(dev, component, component_driver,
2848e0dac41bSKuninori Morimoto 				     dai_drv, num_dai);
2849e0dac41bSKuninori Morimoto }
2850d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
2851d191bd8dSKuninori Morimoto 
2852d191bd8dSKuninori Morimoto /**
28532eccea8cSKuninori Morimoto  * snd_soc_unregister_component - Unregister all related component
28542eccea8cSKuninori Morimoto  * from the ASoC core
2855d191bd8dSKuninori Morimoto  *
2856628536eaSJonathan Corbet  * @dev: The device to unregister
2857d191bd8dSKuninori Morimoto  */
28582eccea8cSKuninori Morimoto static int __snd_soc_unregister_component(struct device *dev)
2859d191bd8dSKuninori Morimoto {
2860cf9e829eSKuninori Morimoto 	struct snd_soc_component *component;
286121a03528SKuninori Morimoto 	int found = 0;
2862d191bd8dSKuninori Morimoto 
286334e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
2864368dee94SKuninori Morimoto 	for_each_component(component) {
2865999f7f5aSKuninori Morimoto 		if (dev != component->dev)
286621a03528SKuninori Morimoto 			continue;
2867d191bd8dSKuninori Morimoto 
28682c7b696aSMarcel Ziswiler 		snd_soc_tplg_component_remove(component,
28692c7b696aSMarcel Ziswiler 					      SND_SOC_TPLG_INDEX_ALL);
2870cf9e829eSKuninori Morimoto 		snd_soc_component_del_unlocked(component);
287121a03528SKuninori Morimoto 		found = 1;
287221a03528SKuninori Morimoto 		break;
2873d191bd8dSKuninori Morimoto 	}
2874d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
2875d191bd8dSKuninori Morimoto 
28762c7b696aSMarcel Ziswiler 	if (found)
2877cf9e829eSKuninori Morimoto 		snd_soc_component_cleanup(component);
28782eccea8cSKuninori Morimoto 
28792eccea8cSKuninori Morimoto 	return found;
28802eccea8cSKuninori Morimoto }
28812eccea8cSKuninori Morimoto 
28822eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
28832eccea8cSKuninori Morimoto {
28842c7b696aSMarcel Ziswiler 	while (__snd_soc_unregister_component(dev))
28852c7b696aSMarcel Ziswiler 		;
2886d191bd8dSKuninori Morimoto }
2887d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2888d191bd8dSKuninori Morimoto 
28897dd5d0d9SKuninori Morimoto struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
28907dd5d0d9SKuninori Morimoto 						   const char *driver_name)
28917dd5d0d9SKuninori Morimoto {
28927dd5d0d9SKuninori Morimoto 	struct snd_soc_component *component;
28937dd5d0d9SKuninori Morimoto 	struct snd_soc_component *ret;
28947dd5d0d9SKuninori Morimoto 
28957dd5d0d9SKuninori Morimoto 	ret = NULL;
28967dd5d0d9SKuninori Morimoto 	mutex_lock(&client_mutex);
2897368dee94SKuninori Morimoto 	for_each_component(component) {
28987dd5d0d9SKuninori Morimoto 		if (dev != component->dev)
28997dd5d0d9SKuninori Morimoto 			continue;
29007dd5d0d9SKuninori Morimoto 
29017dd5d0d9SKuninori Morimoto 		if (driver_name &&
29027dd5d0d9SKuninori Morimoto 		    (driver_name != component->driver->name) &&
29037dd5d0d9SKuninori Morimoto 		    (strcmp(component->driver->name, driver_name) != 0))
29047dd5d0d9SKuninori Morimoto 			continue;
29057dd5d0d9SKuninori Morimoto 
29067dd5d0d9SKuninori Morimoto 		ret = component;
29077dd5d0d9SKuninori Morimoto 		break;
29087dd5d0d9SKuninori Morimoto 	}
29097dd5d0d9SKuninori Morimoto 	mutex_unlock(&client_mutex);
29107dd5d0d9SKuninori Morimoto 
29117dd5d0d9SKuninori Morimoto 	return ret;
29127dd5d0d9SKuninori Morimoto }
29137dd5d0d9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
29147dd5d0d9SKuninori Morimoto 
2915bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
2916b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2917bec4fa05SStephen Warren 			       const char *propname)
2918bec4fa05SStephen Warren {
2919b07609ceSKuninori Morimoto 	struct device_node *np;
2920bec4fa05SStephen Warren 	int ret;
2921bec4fa05SStephen Warren 
29227e07e7c0STushar Behera 	if (!card->dev) {
29237e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
29247e07e7c0STushar Behera 		return -EINVAL;
29257e07e7c0STushar Behera 	}
29267e07e7c0STushar Behera 
29277e07e7c0STushar Behera 	np = card->dev->of_node;
29287e07e7c0STushar Behera 
2929bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
2930bec4fa05SStephen Warren 	/*
2931bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
2932bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
2933bec4fa05SStephen Warren 	 * snd_soc_register_card.
2934bec4fa05SStephen Warren 	 */
2935bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
2936bec4fa05SStephen Warren 		dev_err(card->dev,
2937f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
2938bec4fa05SStephen Warren 			propname, ret);
2939bec4fa05SStephen Warren 		return ret;
2940bec4fa05SStephen Warren 	}
2941bec4fa05SStephen Warren 
2942bec4fa05SStephen Warren 	return 0;
2943bec4fa05SStephen Warren }
2944b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2945bec4fa05SStephen Warren 
29469a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
29479a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
29489a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
29499a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
29509a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
29519a6d4860SXiubo Li };
29529a6d4860SXiubo Li 
295321efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
29549a6d4860SXiubo Li 					  const char *propname)
29559a6d4860SXiubo Li {
295621efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
29579a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
29589a6d4860SXiubo Li 	const char *template, *wname;
29599a6d4860SXiubo Li 	int i, j, num_widgets, ret;
29609a6d4860SXiubo Li 
29619a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
29629a6d4860SXiubo Li 	if (num_widgets < 0) {
29639a6d4860SXiubo Li 		dev_err(card->dev,
29649a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
29659a6d4860SXiubo Li 		return -EINVAL;
29669a6d4860SXiubo Li 	}
29679a6d4860SXiubo Li 	if (num_widgets & 1) {
29689a6d4860SXiubo Li 		dev_err(card->dev,
29699a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
29709a6d4860SXiubo Li 		return -EINVAL;
29719a6d4860SXiubo Li 	}
29729a6d4860SXiubo Li 
29739a6d4860SXiubo Li 	num_widgets /= 2;
29749a6d4860SXiubo Li 	if (!num_widgets) {
29759a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
29769a6d4860SXiubo Li 			propname);
29779a6d4860SXiubo Li 		return -EINVAL;
29789a6d4860SXiubo Li 	}
29799a6d4860SXiubo Li 
29809a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
29819a6d4860SXiubo Li 			       GFP_KERNEL);
29829a6d4860SXiubo Li 	if (!widgets) {
29839a6d4860SXiubo Li 		dev_err(card->dev,
29849a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
29859a6d4860SXiubo Li 		return -ENOMEM;
29869a6d4860SXiubo Li 	}
29879a6d4860SXiubo Li 
29889a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
29899a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
29909a6d4860SXiubo Li 			2 * i, &template);
29919a6d4860SXiubo Li 		if (ret) {
29929a6d4860SXiubo Li 			dev_err(card->dev,
29939a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
29949a6d4860SXiubo Li 				propname, 2 * i, ret);
29959a6d4860SXiubo Li 			return -EINVAL;
29969a6d4860SXiubo Li 		}
29979a6d4860SXiubo Li 
29989a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
29999a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
30009a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
30019a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
30029a6d4860SXiubo Li 				break;
30039a6d4860SXiubo Li 			}
30049a6d4860SXiubo Li 		}
30059a6d4860SXiubo Li 
30069a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
30079a6d4860SXiubo Li 			dev_err(card->dev,
30089a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
30099a6d4860SXiubo Li 				template);
30109a6d4860SXiubo Li 			return -EINVAL;
30119a6d4860SXiubo Li 		}
30129a6d4860SXiubo Li 
30139a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
30149a6d4860SXiubo Li 						    (2 * i) + 1,
30159a6d4860SXiubo Li 						    &wname);
30169a6d4860SXiubo Li 		if (ret) {
30179a6d4860SXiubo Li 			dev_err(card->dev,
30189a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
30199a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
30209a6d4860SXiubo Li 			return -EINVAL;
30219a6d4860SXiubo Li 		}
30229a6d4860SXiubo Li 
30239a6d4860SXiubo Li 		widgets[i].name = wname;
30249a6d4860SXiubo Li 	}
30259a6d4860SXiubo Li 
3026f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
3027f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
30289a6d4860SXiubo Li 
30299a6d4860SXiubo Li 	return 0;
30309a6d4860SXiubo Li }
303121efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
30329a6d4860SXiubo Li 
3033cbdfab3bSJerome Brunet int snd_soc_of_get_slot_mask(struct device_node *np,
30346131084aSJyri Sarha 			     const char *prop_name,
30356131084aSJyri Sarha 			     unsigned int *mask)
30366131084aSJyri Sarha {
30376131084aSJyri Sarha 	u32 val;
30386c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
30396131084aSJyri Sarha 	int i;
30406131084aSJyri Sarha 
30416131084aSJyri Sarha 	if (!of_slot_mask)
30426131084aSJyri Sarha 		return 0;
30436131084aSJyri Sarha 	val /= sizeof(u32);
30446131084aSJyri Sarha 	for (i = 0; i < val; i++)
30456131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
30466131084aSJyri Sarha 			*mask |= (1 << i);
30476131084aSJyri Sarha 
30486131084aSJyri Sarha 	return val;
30496131084aSJyri Sarha }
3050cbdfab3bSJerome Brunet EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
30516131084aSJyri Sarha 
305289c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
30536131084aSJyri Sarha 			      unsigned int *tx_mask,
30546131084aSJyri Sarha 			      unsigned int *rx_mask,
305589c67857SXiubo Li 			      unsigned int *slots,
305689c67857SXiubo Li 			      unsigned int *slot_width)
305789c67857SXiubo Li {
305889c67857SXiubo Li 	u32 val;
305989c67857SXiubo Li 	int ret;
306089c67857SXiubo Li 
30616131084aSJyri Sarha 	if (tx_mask)
30626131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
30636131084aSJyri Sarha 	if (rx_mask)
30646131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
30656131084aSJyri Sarha 
306689c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
306789c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
306889c67857SXiubo Li 		if (ret)
306989c67857SXiubo Li 			return ret;
307089c67857SXiubo Li 
307189c67857SXiubo Li 		if (slots)
307289c67857SXiubo Li 			*slots = val;
307389c67857SXiubo Li 	}
307489c67857SXiubo Li 
307589c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
307689c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
307789c67857SXiubo Li 		if (ret)
307889c67857SXiubo Li 			return ret;
307989c67857SXiubo Li 
308089c67857SXiubo Li 		if (slot_width)
308189c67857SXiubo Li 			*slot_width = val;
308289c67857SXiubo Li 	}
308389c67857SXiubo Li 
308489c67857SXiubo Li 	return 0;
308589c67857SXiubo Li }
308689c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
308789c67857SXiubo Li 
30883b710356SKuninori Morimoto void snd_soc_of_parse_node_prefix(struct device_node *np,
30895e3cdaa2SKuninori Morimoto 				  struct snd_soc_codec_conf *codec_conf,
30905e3cdaa2SKuninori Morimoto 				  struct device_node *of_node,
30915e3cdaa2SKuninori Morimoto 				  const char *propname)
30925e3cdaa2SKuninori Morimoto {
30935e3cdaa2SKuninori Morimoto 	const char *str;
30945e3cdaa2SKuninori Morimoto 	int ret;
30955e3cdaa2SKuninori Morimoto 
30965e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
30975e3cdaa2SKuninori Morimoto 	if (ret < 0) {
30985e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
30995e3cdaa2SKuninori Morimoto 		return;
31005e3cdaa2SKuninori Morimoto 	}
31015e3cdaa2SKuninori Morimoto 
31025e3cdaa2SKuninori Morimoto 	codec_conf->of_node	= of_node;
31035e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
31045e3cdaa2SKuninori Morimoto }
31053b710356SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
31065e3cdaa2SKuninori Morimoto 
31072bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3108a4a54dd5SStephen Warren 				   const char *propname)
3109a4a54dd5SStephen Warren {
31102bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
3111e3b1e6a1SMark Brown 	int num_routes;
3112a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3113a4a54dd5SStephen Warren 	int i, ret;
3114a4a54dd5SStephen Warren 
3115a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3116c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
311710e8aa9aSMichał Mirosław 		dev_err(card->dev,
311810e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
311910e8aa9aSMichał Mirosław 			propname);
3120a4a54dd5SStephen Warren 		return -EINVAL;
3121a4a54dd5SStephen Warren 	}
3122a4a54dd5SStephen Warren 	num_routes /= 2;
3123a4a54dd5SStephen Warren 	if (!num_routes) {
3124f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3125a4a54dd5SStephen Warren 			propname);
3126a4a54dd5SStephen Warren 		return -EINVAL;
3127a4a54dd5SStephen Warren 	}
3128a4a54dd5SStephen Warren 
3129a86854d0SKees Cook 	routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
3130a4a54dd5SStephen Warren 			      GFP_KERNEL);
3131a4a54dd5SStephen Warren 	if (!routes) {
3132a4a54dd5SStephen Warren 		dev_err(card->dev,
3133f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3134a4a54dd5SStephen Warren 		return -EINVAL;
3135a4a54dd5SStephen Warren 	}
3136a4a54dd5SStephen Warren 
3137a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3138a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3139e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
3140a4a54dd5SStephen Warren 		if (ret) {
3141c871bd0bSMark Brown 			dev_err(card->dev,
3142c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3143c871bd0bSMark Brown 				propname, 2 * i, ret);
3144a4a54dd5SStephen Warren 			return -EINVAL;
3145a4a54dd5SStephen Warren 		}
3146a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3147e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3148a4a54dd5SStephen Warren 		if (ret) {
3149a4a54dd5SStephen Warren 			dev_err(card->dev,
3150c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3151c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3152a4a54dd5SStephen Warren 			return -EINVAL;
3153a4a54dd5SStephen Warren 		}
3154a4a54dd5SStephen Warren 	}
3155a4a54dd5SStephen Warren 
3156f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
3157f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
3158a4a54dd5SStephen Warren 
3159a4a54dd5SStephen Warren 	return 0;
3160a4a54dd5SStephen Warren }
31612bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3162a4a54dd5SStephen Warren 
3163a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3164389cb834SJyri Sarha 				     const char *prefix,
3165389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3166389cb834SJyri Sarha 				     struct device_node **framemaster)
3167a7930ed4SKuninori Morimoto {
3168a7930ed4SKuninori Morimoto 	int ret, i;
3169a7930ed4SKuninori Morimoto 	char prop[128];
3170a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3171a7930ed4SKuninori Morimoto 	int bit, frame;
3172a7930ed4SKuninori Morimoto 	const char *str;
3173a7930ed4SKuninori Morimoto 	struct {
3174a7930ed4SKuninori Morimoto 		char *name;
3175a7930ed4SKuninori Morimoto 		unsigned int val;
3176a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3177a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3178a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3179a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3180a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3181a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3182a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3183a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3184a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3185a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3186a7930ed4SKuninori Morimoto 	};
3187a7930ed4SKuninori Morimoto 
3188a7930ed4SKuninori Morimoto 	if (!prefix)
3189a7930ed4SKuninori Morimoto 		prefix = "";
3190a7930ed4SKuninori Morimoto 
3191a7930ed4SKuninori Morimoto 	/*
31925711c979SKuninori Morimoto 	 * check "dai-format = xxx"
31935711c979SKuninori Morimoto 	 * or    "[prefix]format = xxx"
3194a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3195a7930ed4SKuninori Morimoto 	 */
31965711c979SKuninori Morimoto 	ret = of_property_read_string(np, "dai-format", &str);
31975711c979SKuninori Morimoto 	if (ret < 0) {
3198a7930ed4SKuninori Morimoto 		snprintf(prop, sizeof(prop), "%sformat", prefix);
3199a7930ed4SKuninori Morimoto 		ret = of_property_read_string(np, prop, &str);
32005711c979SKuninori Morimoto 	}
3201a7930ed4SKuninori Morimoto 	if (ret == 0) {
3202a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3203a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3204a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3205a7930ed4SKuninori Morimoto 				break;
3206a7930ed4SKuninori Morimoto 			}
3207a7930ed4SKuninori Morimoto 		}
3208a7930ed4SKuninori Morimoto 	}
3209a7930ed4SKuninori Morimoto 
3210a7930ed4SKuninori Morimoto 	/*
32118c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3212a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3213a7930ed4SKuninori Morimoto 	 */
32148c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
321551930295SJulia Lawall 	if (of_property_read_bool(np, prop))
32168c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
32178c2d6a9fSKuninori Morimoto 	else
32188c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3219a7930ed4SKuninori Morimoto 
3220a7930ed4SKuninori Morimoto 	/*
3221a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3222a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3223a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3224a7930ed4SKuninori Morimoto 	 */
3225a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3226a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3227a7930ed4SKuninori Morimoto 
3228a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3229a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3230a7930ed4SKuninori Morimoto 
3231a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3232a7930ed4SKuninori Morimoto 	case 0x11:
3233a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3234a7930ed4SKuninori Morimoto 		break;
3235a7930ed4SKuninori Morimoto 	case 0x10:
3236a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3237a7930ed4SKuninori Morimoto 		break;
3238a7930ed4SKuninori Morimoto 	case 0x01:
3239a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3240a7930ed4SKuninori Morimoto 		break;
3241a7930ed4SKuninori Morimoto 	default:
3242a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3243a7930ed4SKuninori Morimoto 		break;
3244a7930ed4SKuninori Morimoto 	}
3245a7930ed4SKuninori Morimoto 
3246a7930ed4SKuninori Morimoto 	/*
3247a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3248a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3249a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3250a7930ed4SKuninori Morimoto 	 */
3251a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3252a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3253389cb834SJyri Sarha 	if (bit && bitclkmaster)
3254389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3255a7930ed4SKuninori Morimoto 
3256a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3257a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3258389cb834SJyri Sarha 	if (frame && framemaster)
3259389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3260a7930ed4SKuninori Morimoto 
3261a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3262a7930ed4SKuninori Morimoto 	case 0x11:
3263a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3264a7930ed4SKuninori Morimoto 		break;
3265a7930ed4SKuninori Morimoto 	case 0x10:
3266a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3267a7930ed4SKuninori Morimoto 		break;
3268a7930ed4SKuninori Morimoto 	case 0x01:
3269a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3270a7930ed4SKuninori Morimoto 		break;
3271a7930ed4SKuninori Morimoto 	default:
3272a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3273a7930ed4SKuninori Morimoto 		break;
3274a7930ed4SKuninori Morimoto 	}
3275a7930ed4SKuninori Morimoto 
3276a7930ed4SKuninori Morimoto 	return format;
3277a7930ed4SKuninori Morimoto }
3278a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3279a7930ed4SKuninori Morimoto 
3280a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
3281a180e8b9SKuninori Morimoto {
328209d4cc03SKuninori Morimoto 	struct snd_soc_component *component;
3283c1e230f0SKuninori Morimoto 	struct snd_soc_dai_link_component dlc;
3284a180e8b9SKuninori Morimoto 	int ret;
3285a180e8b9SKuninori Morimoto 
3286c1e230f0SKuninori Morimoto 	dlc.of_node	= of_graph_get_port_parent(ep);
3287c1e230f0SKuninori Morimoto 	dlc.name	= NULL;
3288a180e8b9SKuninori Morimoto 	/*
3289a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
3290a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
3291a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
3292a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
3293a180e8b9SKuninori Morimoto 	 */
3294a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
3295a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
3296c1e230f0SKuninori Morimoto 	component = soc_find_component(&dlc);
32972c7b1704SKuninori Morimoto 	if (component)
32982c7b1704SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_id(component, ep);
3299a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
3300a180e8b9SKuninori Morimoto 
3301c1e230f0SKuninori Morimoto 	of_node_put(dlc.of_node);
3302c0a480d1STony Lindgren 
3303a180e8b9SKuninori Morimoto 	return ret;
3304a180e8b9SKuninori Morimoto }
3305a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3306a180e8b9SKuninori Morimoto 
33071ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args,
3308cb470087SKuninori Morimoto 				const char **dai_name)
3309cb470087SKuninori Morimoto {
3310cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
33113e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
331293b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3313cb470087SKuninori Morimoto 
3314cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3315368dee94SKuninori Morimoto 	for_each_component(pos) {
3316c0834440SKuninori Morimoto 		component_of_node = soc_component_to_node(pos);
33173e0aa8d8SJyri Sarha 
33183e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
3319cb470087SKuninori Morimoto 			continue;
3320cb470087SKuninori Morimoto 
3321a2a34175SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3322a2a34175SKuninori Morimoto 		if (ret == -ENOTSUPP) {
332358bf4179SKuninori Morimoto 			struct snd_soc_dai *dai;
33246833c452SKuninori Morimoto 			int id = -1;
33256833c452SKuninori Morimoto 
332693b0f3eeSJean-Francois Moine 			switch (args->args_count) {
33276833c452SKuninori Morimoto 			case 0:
33286833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
33296833c452SKuninori Morimoto 				break;
33306833c452SKuninori Morimoto 			case 1:
333193b0f3eeSJean-Francois Moine 				id = args->args[0];
33326833c452SKuninori Morimoto 				break;
33336833c452SKuninori Morimoto 			default:
33346833c452SKuninori Morimoto 				/* not supported */
3335cb470087SKuninori Morimoto 				break;
3336cb470087SKuninori Morimoto 			}
3337cb470087SKuninori Morimoto 
33386833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
33396833c452SKuninori Morimoto 				ret = -EINVAL;
33403dcba280SNicolin Chen 				continue;
33416833c452SKuninori Morimoto 			}
3342e41975edSXiubo Li 
3343e41975edSXiubo Li 			ret = 0;
3344e41975edSXiubo Li 
334558bf4179SKuninori Morimoto 			/* find target DAI */
334615a0c645SKuninori Morimoto 			for_each_component_dais(pos, dai) {
334758bf4179SKuninori Morimoto 				if (id == 0)
334858bf4179SKuninori Morimoto 					break;
334958bf4179SKuninori Morimoto 				id--;
335058bf4179SKuninori Morimoto 			}
335158bf4179SKuninori Morimoto 
335258bf4179SKuninori Morimoto 			*dai_name = dai->driver->name;
3353e41975edSXiubo Li 			if (!*dai_name)
3354e41975edSXiubo Li 				*dai_name = pos->name;
33556833c452SKuninori Morimoto 		}
33566833c452SKuninori Morimoto 
3357cb470087SKuninori Morimoto 		break;
3358cb470087SKuninori Morimoto 	}
3359cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
336093b0f3eeSJean-Francois Moine 	return ret;
336193b0f3eeSJean-Francois Moine }
33621ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
336393b0f3eeSJean-Francois Moine 
336493b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
336593b0f3eeSJean-Francois Moine 			    const char **dai_name)
336693b0f3eeSJean-Francois Moine {
336793b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
336893b0f3eeSJean-Francois Moine 	int ret;
336993b0f3eeSJean-Francois Moine 
337093b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
337193b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
337293b0f3eeSJean-Francois Moine 	if (ret)
337393b0f3eeSJean-Francois Moine 		return ret;
337493b0f3eeSJean-Francois Moine 
337593b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3376cb470087SKuninori Morimoto 
3377cb470087SKuninori Morimoto 	of_node_put(args.np);
3378cb470087SKuninori Morimoto 
3379cb470087SKuninori Morimoto 	return ret;
3380cb470087SKuninori Morimoto }
3381cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3382cb470087SKuninori Morimoto 
338393b0f3eeSJean-Francois Moine /*
338494685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
338594685763SSylwester Nawrocki  * @dai_link: DAI link
338694685763SSylwester Nawrocki  *
338794685763SSylwester Nawrocki  * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
338894685763SSylwester Nawrocki  */
338994685763SSylwester Nawrocki void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
339094685763SSylwester Nawrocki {
33913db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *component;
339294685763SSylwester Nawrocki 	int index;
339394685763SSylwester Nawrocki 
33943db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
339594685763SSylwester Nawrocki 		if (!component->of_node)
339694685763SSylwester Nawrocki 			break;
339794685763SSylwester Nawrocki 		of_node_put(component->of_node);
339894685763SSylwester Nawrocki 		component->of_node = NULL;
339994685763SSylwester Nawrocki 	}
340094685763SSylwester Nawrocki }
340194685763SSylwester Nawrocki EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
340294685763SSylwester Nawrocki 
340394685763SSylwester Nawrocki /*
340493b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
340593b0f3eeSJean-Francois Moine  * @dev: Card device
340693b0f3eeSJean-Francois Moine  * @of_node: Device node
340793b0f3eeSJean-Francois Moine  * @dai_link: DAI link
340893b0f3eeSJean-Francois Moine  *
340993b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
341093b0f3eeSJean-Francois Moine  * 'sound-dai'.
341193b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
341294685763SSylwester Nawrocki  * The device nodes in the array (of_node) must be dereferenced by calling
341394685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs() on @dai_link.
341493b0f3eeSJean-Francois Moine  *
341593b0f3eeSJean-Francois Moine  * Returns 0 for success
341693b0f3eeSJean-Francois Moine  */
341793b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
341893b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
341993b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
342093b0f3eeSJean-Francois Moine {
342193b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
342293b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
342393b0f3eeSJean-Francois Moine 	char *name;
342493b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
342593b0f3eeSJean-Francois Moine 
342693b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
342793b0f3eeSJean-Francois Moine 	name = "sound-dai";
342893b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
342993b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
343093b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
343193b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
343293b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
343393b0f3eeSJean-Francois Moine 		else
343493b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
343593b0f3eeSJean-Francois Moine 		return num_codecs;
343693b0f3eeSJean-Francois Moine 	}
3437a86854d0SKees Cook 	component = devm_kcalloc(dev,
3438a86854d0SKees Cook 				 num_codecs, sizeof(*component),
343993b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
344093b0f3eeSJean-Francois Moine 	if (!component)
344193b0f3eeSJean-Francois Moine 		return -ENOMEM;
344293b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
344393b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
344493b0f3eeSJean-Francois Moine 
344593b0f3eeSJean-Francois Moine 	/* Parse the list */
34463db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
344793b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
344893b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
344993b0f3eeSJean-Francois Moine 						 index, &args);
345093b0f3eeSJean-Francois Moine 		if (ret)
345193b0f3eeSJean-Francois Moine 			goto err;
345293b0f3eeSJean-Francois Moine 		component->of_node = args.np;
345393b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
345493b0f3eeSJean-Francois Moine 		if (ret < 0)
345593b0f3eeSJean-Francois Moine 			goto err;
345693b0f3eeSJean-Francois Moine 	}
345793b0f3eeSJean-Francois Moine 	return 0;
345893b0f3eeSJean-Francois Moine err:
345994685763SSylwester Nawrocki 	snd_soc_of_put_dai_link_codecs(dai_link);
346093b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
346193b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
346293b0f3eeSJean-Francois Moine 	return ret;
346393b0f3eeSJean-Francois Moine }
346493b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
346593b0f3eeSJean-Francois Moine 
3466c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3467db2a4165SFrank Mandarino {
34686553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
3469fb257897SMark Brown 	snd_soc_util_init();
3470fb257897SMark Brown 
3471db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3472db2a4165SFrank Mandarino }
34734abe8e16SMark Brown module_init(snd_soc_init);
3474db2a4165SFrank Mandarino 
34757d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3476db2a4165SFrank Mandarino {
3477fb257897SMark Brown 	snd_soc_util_exit();
34786553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3479fb257897SMark Brown 
3480db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3481db2a4165SFrank Mandarino }
3482db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3483db2a4165SFrank Mandarino 
3484db2a4165SFrank Mandarino /* Module information */
3485d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3486db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3487db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
34888b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3489