xref: /openbmc/linux/sound/soc/soc-core.c (revision ad64bfbd09d7b7997a1b1510983350836bcd6ed9)
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 {
174*ad64bfbdSKuninori Morimoto 	if (!component->debugfs_root)
175*ad64bfbdSKuninori Morimoto 		return;
17681c7cfd1SLars-Peter Clausen 	debugfs_remove_recursive(component->debugfs_root);
177*ad64bfbdSKuninori 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);
219a6052154SJarkko Nikula }
220a6052154SJarkko Nikula 
221a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
222a6052154SJarkko Nikula {
223a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
22429040d1aSKuninori Morimoto 	card->debugfs_card_root = NULL;
225a6052154SJarkko Nikula }
226a6052154SJarkko Nikula 
2276553bf06SLars-Peter Clausen static void snd_soc_debugfs_init(void)
2286553bf06SLars-Peter Clausen {
2296553bf06SLars-Peter Clausen 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
2306553bf06SLars-Peter Clausen 
231fee531d6SGreg Kroah-Hartman 	debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
232fee531d6SGreg Kroah-Hartman 			    &dai_list_fops);
233db795f9bSKuninori Morimoto 
234fee531d6SGreg Kroah-Hartman 	debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
235fee531d6SGreg Kroah-Hartman 			    &component_list_fops);
2366553bf06SLars-Peter Clausen }
2376553bf06SLars-Peter Clausen 
2386553bf06SLars-Peter Clausen static void snd_soc_debugfs_exit(void)
2396553bf06SLars-Peter Clausen {
2406553bf06SLars-Peter Clausen 	debugfs_remove_recursive(snd_soc_debugfs_root);
2416553bf06SLars-Peter Clausen }
2426553bf06SLars-Peter Clausen 
2432624d5faSMark Brown #else
2442624d5faSMark Brown 
24581c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs(
24681c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
2472624d5faSMark Brown {
2482624d5faSMark Brown }
2492624d5faSMark Brown 
25081c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs(
25181c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
252731f1ab2SSebastien Guiriec {
253731f1ab2SSebastien Guiriec }
254731f1ab2SSebastien Guiriec 
255b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
256b95fccbcSAxel Lin {
257b95fccbcSAxel Lin }
258b95fccbcSAxel Lin 
259b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
260b95fccbcSAxel Lin {
261b95fccbcSAxel Lin }
2626553bf06SLars-Peter Clausen 
2636553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_init(void)
2646553bf06SLars-Peter Clausen {
2656553bf06SLars-Peter Clausen }
2666553bf06SLars-Peter Clausen 
2676553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_exit(void)
2686553bf06SLars-Peter Clausen {
2696553bf06SLars-Peter Clausen }
2706553bf06SLars-Peter Clausen 
2712624d5faSMark Brown #endif
2722624d5faSMark Brown 
273a0ac4411SKuninori Morimoto static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
274a0ac4411SKuninori Morimoto 			      struct snd_soc_component *component)
275a0ac4411SKuninori Morimoto {
276a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
277a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *new_rtdcom;
278a0ac4411SKuninori Morimoto 
279a0ac4411SKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
280a0ac4411SKuninori Morimoto 		/* already connected */
281a0ac4411SKuninori Morimoto 		if (rtdcom->component == component)
282a0ac4411SKuninori Morimoto 			return 0;
283a0ac4411SKuninori Morimoto 	}
284a0ac4411SKuninori Morimoto 
285a0ac4411SKuninori Morimoto 	new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
286a0ac4411SKuninori Morimoto 	if (!new_rtdcom)
287a0ac4411SKuninori Morimoto 		return -ENOMEM;
288a0ac4411SKuninori Morimoto 
289a0ac4411SKuninori Morimoto 	new_rtdcom->component = component;
290a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&new_rtdcom->list);
291a0ac4411SKuninori Morimoto 
292a0ac4411SKuninori Morimoto 	list_add_tail(&new_rtdcom->list, &rtd->component_list);
293a0ac4411SKuninori Morimoto 
294a0ac4411SKuninori Morimoto 	return 0;
295a0ac4411SKuninori Morimoto }
296a0ac4411SKuninori Morimoto 
297a0ac4411SKuninori Morimoto static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
298a0ac4411SKuninori Morimoto {
299a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
300a0ac4411SKuninori Morimoto 
301a0ac4411SKuninori Morimoto 	for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
302a0ac4411SKuninori Morimoto 		kfree(rtdcom1);
303a0ac4411SKuninori Morimoto 
304a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
305a0ac4411SKuninori Morimoto }
306a0ac4411SKuninori Morimoto 
307a0ac4411SKuninori Morimoto struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
308a0ac4411SKuninori Morimoto 						const char *driver_name)
309a0ac4411SKuninori Morimoto {
310a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
311a0ac4411SKuninori Morimoto 
312971da24cSKuninori Morimoto 	if (!driver_name)
313971da24cSKuninori Morimoto 		return NULL;
314971da24cSKuninori Morimoto 
315a0ac4411SKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
316971da24cSKuninori Morimoto 		const char *component_name = rtdcom->component->driver->name;
317971da24cSKuninori Morimoto 
318971da24cSKuninori Morimoto 		if (!component_name)
319971da24cSKuninori Morimoto 			continue;
320971da24cSKuninori Morimoto 
321971da24cSKuninori Morimoto 		if ((component_name == driver_name) ||
322971da24cSKuninori Morimoto 		    strcmp(component_name, driver_name) == 0)
323a0ac4411SKuninori Morimoto 			return rtdcom->component;
324a0ac4411SKuninori Morimoto 	}
325a0ac4411SKuninori Morimoto 
326a0ac4411SKuninori Morimoto 	return NULL;
327a0ac4411SKuninori Morimoto }
328031734b7SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
329a0ac4411SKuninori Morimoto 
33047c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
33147c88fffSLiam Girdwood 		const char *dai_link, int stream)
33247c88fffSLiam Girdwood {
3331a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
33447c88fffSLiam Girdwood 
335bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
3361a497983SMengdong Lin 		if (rtd->dai_link->no_pcm &&
3371a497983SMengdong Lin 			!strcmp(rtd->dai_link->name, dai_link))
3381a497983SMengdong Lin 			return rtd->pcm->streams[stream].substream;
33947c88fffSLiam Girdwood 	}
340f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
34147c88fffSLiam Girdwood 	return NULL;
34247c88fffSLiam Girdwood }
34347c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
34447c88fffSLiam Girdwood 
34575ab9eb6SKuninori Morimoto static const struct snd_soc_ops null_snd_soc_ops;
34675ab9eb6SKuninori Morimoto 
3471a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
3481a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
3491a497983SMengdong Lin {
3501a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
3511a497983SMengdong Lin 
3521a497983SMengdong Lin 	rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
3531a497983SMengdong Lin 	if (!rtd)
3541a497983SMengdong Lin 		return NULL;
3551a497983SMengdong Lin 
356a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
3571a497983SMengdong Lin 	rtd->card = card;
3581a497983SMengdong Lin 	rtd->dai_link = dai_link;
35975ab9eb6SKuninori Morimoto 	if (!rtd->dai_link->ops)
36075ab9eb6SKuninori Morimoto 		rtd->dai_link->ops = &null_snd_soc_ops;
36175ab9eb6SKuninori Morimoto 
3626396bb22SKees Cook 	rtd->codec_dais = kcalloc(dai_link->num_codecs,
3636396bb22SKees Cook 					sizeof(struct snd_soc_dai *),
3641a497983SMengdong Lin 					GFP_KERNEL);
3651a497983SMengdong Lin 	if (!rtd->codec_dais) {
3661a497983SMengdong Lin 		kfree(rtd);
3671a497983SMengdong Lin 		return NULL;
3681a497983SMengdong Lin 	}
3691a497983SMengdong Lin 
3701a497983SMengdong Lin 	return rtd;
3711a497983SMengdong Lin }
3721a497983SMengdong Lin 
3731a497983SMengdong Lin static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
3741a497983SMengdong Lin {
3751a497983SMengdong Lin 	kfree(rtd->codec_dais);
376a0ac4411SKuninori Morimoto 	snd_soc_rtdcom_del_all(rtd);
3771a497983SMengdong Lin 	kfree(rtd);
3781a497983SMengdong Lin }
3791a497983SMengdong Lin 
3801a497983SMengdong Lin static void soc_add_pcm_runtime(struct snd_soc_card *card,
3811a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd)
3821a497983SMengdong Lin {
3831a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
3841a497983SMengdong Lin 	rtd->num = card->num_rtd;
3851a497983SMengdong Lin 	card->num_rtd++;
3861a497983SMengdong Lin }
3871a497983SMengdong Lin 
3881a497983SMengdong Lin static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
3891a497983SMengdong Lin {
3901a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, *_rtd;
3911a497983SMengdong Lin 
392bcb1fd1fSKuninori Morimoto 	for_each_card_rtds_safe(card, rtd, _rtd) {
3931a497983SMengdong Lin 		list_del(&rtd->list);
3941a497983SMengdong Lin 		soc_free_pcm_runtime(rtd);
3951a497983SMengdong Lin 	}
3961a497983SMengdong Lin 
3971a497983SMengdong Lin 	card->num_rtd = 0;
3981a497983SMengdong Lin }
3991a497983SMengdong Lin 
40047c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
40147c88fffSLiam Girdwood 		const char *dai_link)
40247c88fffSLiam Girdwood {
4031a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
40447c88fffSLiam Girdwood 
405bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4061a497983SMengdong Lin 		if (!strcmp(rtd->dai_link->name, dai_link))
4071a497983SMengdong Lin 			return rtd;
40847c88fffSLiam Girdwood 	}
409f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
41047c88fffSLiam Girdwood 	return NULL;
41147c88fffSLiam Girdwood }
41247c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
41347c88fffSLiam Girdwood 
41465462e44SKuninori Morimoto static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
41565462e44SKuninori Morimoto {
41665462e44SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
41765462e44SKuninori Morimoto 
41865462e44SKuninori Morimoto 	for_each_card_rtds(card, rtd)
41965462e44SKuninori Morimoto 		flush_delayed_work(&rtd->delayed_work);
42065462e44SKuninori Morimoto }
42165462e44SKuninori Morimoto 
4226f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
423db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
4246f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
425db2a4165SFrank Mandarino {
4266f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
427d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
4281a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
4291a497983SMengdong Lin 	int i;
430db2a4165SFrank Mandarino 
431c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
432c5599b87SLars-Peter Clausen 	if (!card->instantiated)
433e3509ff0SDaniel Mack 		return 0;
434e3509ff0SDaniel Mack 
4352c7b696aSMarcel Ziswiler 	/*
4362c7b696aSMarcel Ziswiler 	 * Due to the resume being scheduled into a workqueue we could
4376ed25978SAndy Green 	 * suspend before that's finished - wait for it to complete.
4386ed25978SAndy Green 	 */
439f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
4406ed25978SAndy Green 
4416ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
442f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
4436ed25978SAndy Green 
444a00f90f9SMark Brown 	/* mute any active DACs */
445bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4460b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
4473efab7dcSMark Brown 
4481a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4493efab7dcSMark Brown 			continue;
4503efab7dcSMark Brown 
4510b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
45288fdffa2SKuninori Morimoto 			if (dai->playback_active)
45388fdffa2SKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 1,
45488fdffa2SKuninori Morimoto 						SNDRV_PCM_STREAM_PLAYBACK);
455db2a4165SFrank Mandarino 		}
45688bd870fSBenoit Cousson 	}
457db2a4165SFrank Mandarino 
4584ccab3e7SLiam Girdwood 	/* suspend all pcms */
459bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4601a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4613efab7dcSMark Brown 			continue;
4623efab7dcSMark Brown 
4631a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
4643efab7dcSMark Brown 	}
4654ccab3e7SLiam Girdwood 
46687506549SMark Brown 	if (card->suspend_pre)
46770b2ac12SMark Brown 		card->suspend_pre(card);
468db2a4165SFrank Mandarino 
469bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4701a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
4713efab7dcSMark Brown 
4721a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4733efab7dcSMark Brown 			continue;
4743efab7dcSMark Brown 
475e0f22622SKuninori Morimoto 		if (!cpu_dai->driver->bus_control)
476e0f22622SKuninori Morimoto 			snd_soc_dai_suspend(cpu_dai);
477db2a4165SFrank Mandarino 	}
478db2a4165SFrank Mandarino 
47937660b6dSLars-Peter Clausen 	/* close any waiting streams */
48065462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
481db2a4165SFrank Mandarino 
482bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4833efab7dcSMark Brown 
4841a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4853efab7dcSMark Brown 			continue;
4863efab7dcSMark Brown 
4871a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
4887bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
489db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
490f0fba2adSLiam Girdwood 
4911a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
4927bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
493db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
494db2a4165SFrank Mandarino 	}
495db2a4165SFrank Mandarino 
4968be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
4978be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
498e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
499e2d32ff6SMark Brown 
5009178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
501f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
5022c7b696aSMarcel Ziswiler 		struct snd_soc_dapm_context *dapm =
5032c7b696aSMarcel Ziswiler 				snd_soc_component_get_dapm(component);
504d9fc4063SKuninori Morimoto 
5052c7b696aSMarcel Ziswiler 		/*
5062c7b696aSMarcel Ziswiler 		 * If there are paths active then the COMPONENT will be held
5072c7b696aSMarcel Ziswiler 		 * with bias _ON and should not be suspended.
5082c7b696aSMarcel Ziswiler 		 */
509e40fadbcSKuninori Morimoto 		if (!snd_soc_component_is_suspended(component)) {
5104890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
5111547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
512125a25daSMark Brown 				/*
5139178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
514125a25daSMark Brown 				 * bias off then being in STANDBY
515125a25daSMark Brown 				 * means it's doing something,
516125a25daSMark Brown 				 * otherwise fall through.
517125a25daSMark Brown 				 */
5184890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
5199178feb4SKuninori Morimoto 					dev_dbg(component->dev,
52010e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
521125a25daSMark Brown 					break;
522125a25daSMark Brown 				}
5231a12d5dcSGustavo A. R. Silva 				/* fall through */
524a8093297SLars-Peter Clausen 
5251547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
52666c51573SKuninori Morimoto 				snd_soc_component_suspend(component);
5279178feb4SKuninori Morimoto 				if (component->regmap)
5289178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
529988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
5309178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
5311547aba9SMark Brown 				break;
5321547aba9SMark Brown 			default:
5339178feb4SKuninori Morimoto 				dev_dbg(component->dev,
5349178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
5351547aba9SMark Brown 				break;
5361547aba9SMark Brown 			}
5371547aba9SMark Brown 		}
538f0fba2adSLiam Girdwood 	}
539db2a4165SFrank Mandarino 
540bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5411a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
5423efab7dcSMark Brown 
5431a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5443efab7dcSMark Brown 			continue;
5453efab7dcSMark Brown 
546e0f22622SKuninori Morimoto 		if (cpu_dai->driver->bus_control)
547e0f22622SKuninori Morimoto 			snd_soc_dai_suspend(cpu_dai);
548988e8cc4SNicolin Chen 
549988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
550988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
551db2a4165SFrank Mandarino 	}
552db2a4165SFrank Mandarino 
55387506549SMark Brown 	if (card->suspend_post)
55470b2ac12SMark Brown 		card->suspend_post(card);
555db2a4165SFrank Mandarino 
556db2a4165SFrank Mandarino 	return 0;
557db2a4165SFrank Mandarino }
5586f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
559db2a4165SFrank Mandarino 
5602c7b696aSMarcel Ziswiler /*
5612c7b696aSMarcel Ziswiler  * deferred resume work, so resume can complete before we finished
5626ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
5636ed25978SAndy Green  */
5646ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
565db2a4165SFrank Mandarino {
566f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
5672c7b696aSMarcel Ziswiler 			container_of(work, struct snd_soc_card,
5682c7b696aSMarcel Ziswiler 				     deferred_resume_work);
5691a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
570d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
5711a497983SMengdong Lin 	int i;
572db2a4165SFrank Mandarino 
5732c7b696aSMarcel Ziswiler 	/*
5742c7b696aSMarcel Ziswiler 	 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
5756ed25978SAndy Green 	 * so userspace apps are blocked from touching us
5766ed25978SAndy Green 	 */
5776ed25978SAndy Green 
578f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
5796ed25978SAndy Green 
5809949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
581f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
5829949788bSMark Brown 
58387506549SMark Brown 	if (card->resume_pre)
58470b2ac12SMark Brown 		card->resume_pre(card);
585db2a4165SFrank Mandarino 
586bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
587bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5881a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
5893efab7dcSMark Brown 
5901a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5913efab7dcSMark Brown 			continue;
5923efab7dcSMark Brown 
59324b09d05SKuninori Morimoto 		if (cpu_dai->driver->bus_control)
59424b09d05SKuninori Morimoto 			snd_soc_dai_resume(cpu_dai);
595db2a4165SFrank Mandarino 	}
596db2a4165SFrank Mandarino 
597f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
598e40fadbcSKuninori Morimoto 		if (snd_soc_component_is_suspended(component))
5999a840cbaSKuninori Morimoto 			snd_soc_component_resume(component);
6001547aba9SMark Brown 	}
601db2a4165SFrank Mandarino 
602bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6033efab7dcSMark Brown 
6041a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6053efab7dcSMark Brown 			continue;
6063efab7dcSMark Brown 
6071a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
608d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
609db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
610f0fba2adSLiam Girdwood 
6111a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
612d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
613db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
614db2a4165SFrank Mandarino 	}
615db2a4165SFrank Mandarino 
6163ff3f64bSMark Brown 	/* unmute any active DACs */
617bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6180b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
6193efab7dcSMark Brown 
6201a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6213efab7dcSMark Brown 			continue;
6223efab7dcSMark Brown 
6230b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
62488fdffa2SKuninori Morimoto 			if (dai->playback_active)
62588fdffa2SKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 0,
62688fdffa2SKuninori Morimoto 						SNDRV_PCM_STREAM_PLAYBACK);
627db2a4165SFrank Mandarino 		}
62888bd870fSBenoit Cousson 	}
629db2a4165SFrank Mandarino 
630bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6311a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6323efab7dcSMark Brown 
6331a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6343efab7dcSMark Brown 			continue;
6353efab7dcSMark Brown 
63624b09d05SKuninori Morimoto 		if (!cpu_dai->driver->bus_control)
63724b09d05SKuninori Morimoto 			snd_soc_dai_resume(cpu_dai);
638db2a4165SFrank Mandarino 	}
639db2a4165SFrank Mandarino 
64087506549SMark Brown 	if (card->resume_post)
64170b2ac12SMark Brown 		card->resume_post(card);
642db2a4165SFrank Mandarino 
643f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
6446ed25978SAndy Green 
6458be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
6468be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
647e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
6481a7aaa58SJeeja KP 
6491a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
6501a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
6516ed25978SAndy Green }
6526ed25978SAndy Green 
6536ed25978SAndy Green /* powers up audio subsystem after a suspend */
6546f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
6556ed25978SAndy Green {
6566f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
657bc263214SLars-Peter Clausen 	bool bus_control = false;
6581a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
65922d251a5SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
66022d251a5SKuninori Morimoto 	int i;
661b9dd94a8SPeter Ujfalusi 
662c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
663c5599b87SLars-Peter Clausen 	if (!card->instantiated)
6645ff1ddf2SEric Miao 		return 0;
6655ff1ddf2SEric Miao 
666988e8cc4SNicolin Chen 	/* activate pins from sleep state */
667bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
66888bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
66988bd870fSBenoit Cousson 
670988e8cc4SNicolin Chen 		if (cpu_dai->active)
671988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
67288bd870fSBenoit Cousson 
67322d251a5SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
674988e8cc4SNicolin Chen 			if (codec_dai->active)
675988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
676988e8cc4SNicolin Chen 		}
67788bd870fSBenoit Cousson 	}
678988e8cc4SNicolin Chen 
679bc263214SLars-Peter Clausen 	/*
680bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
681bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
682bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
68364ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
68464ab9baaSMark Brown 	 */
685bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6861a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6872c7b696aSMarcel Ziswiler 
688bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
68982e14e8bSStephen Warren 	}
690bc263214SLars-Peter Clausen 	if (bus_control) {
691bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
69264ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
69364ab9baaSMark Brown 	} else {
694f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
6956308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
696f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
697f0fba2adSLiam Girdwood 	}
6986ed25978SAndy Green 
699db2a4165SFrank Mandarino 	return 0;
700db2a4165SFrank Mandarino }
7016f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
702db2a4165SFrank Mandarino #else
7036f8ab4acSMark Brown #define snd_soc_suspend NULL
7046f8ab4acSMark Brown #define snd_soc_resume NULL
705db2a4165SFrank Mandarino #endif
706db2a4165SFrank Mandarino 
70785e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
70802a06d30SBarry Song };
70902a06d30SBarry Song 
710c0834440SKuninori Morimoto static struct device_node
711c0834440SKuninori Morimoto *soc_component_to_node(struct snd_soc_component *component)
71212023a9aSMisael Lopez Cruz {
713c0834440SKuninori Morimoto 	struct device_node *of_node;
71412023a9aSMisael Lopez Cruz 
715c0834440SKuninori Morimoto 	of_node = component->dev->of_node;
716c0834440SKuninori Morimoto 	if (!of_node && component->dev->parent)
717c0834440SKuninori Morimoto 		of_node = component->dev->parent->of_node;
71834e81ab4SLars-Peter Clausen 
719c0834440SKuninori Morimoto 	return of_node;
72012023a9aSMisael Lopez Cruz }
72112023a9aSMisael Lopez Cruz 
722be6ac0a9SKuninori Morimoto static int snd_soc_is_matching_component(
723be6ac0a9SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc,
724be6ac0a9SKuninori Morimoto 	struct snd_soc_component *component)
725be6ac0a9SKuninori Morimoto {
726be6ac0a9SKuninori Morimoto 	struct device_node *component_of_node;
727be6ac0a9SKuninori Morimoto 
7287d7db5d3SKuninori Morimoto 	if (!dlc)
7297d7db5d3SKuninori Morimoto 		return 0;
7307d7db5d3SKuninori Morimoto 
7317d7db5d3SKuninori Morimoto 	component_of_node = soc_component_to_node(component);
732be6ac0a9SKuninori Morimoto 
733be6ac0a9SKuninori Morimoto 	if (dlc->of_node && component_of_node != dlc->of_node)
734be6ac0a9SKuninori Morimoto 		return 0;
735be6ac0a9SKuninori Morimoto 	if (dlc->name && strcmp(component->name, dlc->name))
736be6ac0a9SKuninori Morimoto 		return 0;
737be6ac0a9SKuninori Morimoto 
738be6ac0a9SKuninori Morimoto 	return 1;
739be6ac0a9SKuninori Morimoto }
740be6ac0a9SKuninori Morimoto 
741db2a4165SFrank Mandarino static struct snd_soc_component *soc_find_component(
742c1e230f0SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc)
743db2a4165SFrank Mandarino {
744db2a4165SFrank Mandarino 	struct snd_soc_component *component;
745db2a4165SFrank Mandarino 
746db2a4165SFrank Mandarino 	lockdep_assert_held(&client_mutex);
747db2a4165SFrank Mandarino 
748e3303268SKuninori Morimoto 	/*
749e3303268SKuninori Morimoto 	 * NOTE
750e3303268SKuninori Morimoto 	 *
751e3303268SKuninori Morimoto 	 * It returns *1st* found component, but some driver
752e3303268SKuninori Morimoto 	 * has few components by same of_node/name
753e3303268SKuninori Morimoto 	 * ex)
754e3303268SKuninori Morimoto 	 *	CPU component and generic DMAEngine component
755e3303268SKuninori Morimoto 	 */
756c1e230f0SKuninori Morimoto 	for_each_component(component)
757c1e230f0SKuninori Morimoto 		if (snd_soc_is_matching_component(dlc, component))
758db2a4165SFrank Mandarino 			return component;
759db2a4165SFrank Mandarino 
760db2a4165SFrank Mandarino 	return NULL;
76112023a9aSMisael Lopez Cruz }
76212023a9aSMisael Lopez Cruz 
763fbb88b5cSMengdong Lin /**
764fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
765fbb88b5cSMengdong Lin  *
7664958471bSJeffy Chen  * @dlc: name of the DAI or the DAI driver and optional component info to match
767fbb88b5cSMengdong Lin  *
768ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
769fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
770fbb88b5cSMengdong Lin  * should also match if being specified.
771fbb88b5cSMengdong Lin  *
772fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
773fbb88b5cSMengdong Lin  */
774305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
77514621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
77612023a9aSMisael Lopez Cruz {
77714621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
77814621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
77912023a9aSMisael Lopez Cruz 
78034e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
78134e81ab4SLars-Peter Clausen 
78214621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs */
783368dee94SKuninori Morimoto 	for_each_component(component) {
784be6ac0a9SKuninori Morimoto 		if (!snd_soc_is_matching_component(dlc, component))
78512023a9aSMisael Lopez Cruz 			continue;
78615a0c645SKuninori Morimoto 		for_each_component_dais(component, dai) {
7874958471bSJeffy Chen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
7886a6dafdaSJeffy Chen 			    && (!dai->driver->name
7896a6dafdaSJeffy Chen 				|| strcmp(dai->driver->name, dlc->dai_name)))
79014621c7eSLars-Peter Clausen 				continue;
79112023a9aSMisael Lopez Cruz 
79214621c7eSLars-Peter Clausen 			return dai;
79312023a9aSMisael Lopez Cruz 		}
79412023a9aSMisael Lopez Cruz 	}
79512023a9aSMisael Lopez Cruz 
79612023a9aSMisael Lopez Cruz 	return NULL;
79712023a9aSMisael Lopez Cruz }
798305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
79912023a9aSMisael Lopez Cruz 
80017fb1755SMengdong Lin /**
80117fb1755SMengdong Lin  * snd_soc_find_dai_link - Find a DAI link
80217fb1755SMengdong Lin  *
80317fb1755SMengdong Lin  * @card: soc card
80417fb1755SMengdong Lin  * @id: DAI link ID to match
80517fb1755SMengdong Lin  * @name: DAI link name to match, optional
8068abab35fSCharles Keepax  * @stream_name: DAI link stream name to match, optional
80717fb1755SMengdong Lin  *
80817fb1755SMengdong Lin  * This function will search all existing DAI links of the soc card to
80917fb1755SMengdong Lin  * find the link of the same ID. Since DAI links may not have their
81017fb1755SMengdong Lin  * unique ID, so name and stream name should also match if being
81117fb1755SMengdong Lin  * specified.
81217fb1755SMengdong Lin  *
81317fb1755SMengdong Lin  * Return: pointer of DAI link, or NULL if not found.
81417fb1755SMengdong Lin  */
81517fb1755SMengdong Lin struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
81617fb1755SMengdong Lin 					       int id, const char *name,
81717fb1755SMengdong Lin 					       const char *stream_name)
81817fb1755SMengdong Lin {
81917fb1755SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
82017fb1755SMengdong Lin 
82117fb1755SMengdong Lin 	lockdep_assert_held(&client_mutex);
82217fb1755SMengdong Lin 
82398061fdbSKuninori Morimoto 	for_each_card_links_safe(card, link, _link) {
82417fb1755SMengdong Lin 		if (link->id != id)
82517fb1755SMengdong Lin 			continue;
82617fb1755SMengdong Lin 
82717fb1755SMengdong Lin 		if (name && (!link->name || strcmp(name, link->name)))
82817fb1755SMengdong Lin 			continue;
82917fb1755SMengdong Lin 
83017fb1755SMengdong Lin 		if (stream_name && (!link->stream_name
83117fb1755SMengdong Lin 			|| strcmp(stream_name, link->stream_name)))
83217fb1755SMengdong Lin 			continue;
83317fb1755SMengdong Lin 
83417fb1755SMengdong Lin 		return link;
83517fb1755SMengdong Lin 	}
83617fb1755SMengdong Lin 
83717fb1755SMengdong Lin 	return NULL;
83817fb1755SMengdong Lin }
83917fb1755SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
84017fb1755SMengdong Lin 
84149a5ba1cSMengdong Lin static bool soc_is_dai_link_bound(struct snd_soc_card *card,
84249a5ba1cSMengdong Lin 		struct snd_soc_dai_link *dai_link)
843db2a4165SFrank Mandarino {
84449a5ba1cSMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
84549a5ba1cSMengdong Lin 
846bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
84749a5ba1cSMengdong Lin 		if (rtd->dai_link == dai_link)
84849a5ba1cSMengdong Lin 			return true;
84949a5ba1cSMengdong Lin 	}
85049a5ba1cSMengdong Lin 
85149a5ba1cSMengdong Lin 	return false;
85249a5ba1cSMengdong Lin }
85349a5ba1cSMengdong Lin 
8546f2f1ff0SMengdong Lin static int soc_bind_dai_link(struct snd_soc_card *card,
8556f2f1ff0SMengdong Lin 	struct snd_soc_dai_link *dai_link)
856db2a4165SFrank Mandarino {
8571a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
85834614739SJerome Brunet 	struct snd_soc_dai_link_component *codec, *platform;
85990be711eSKuninori Morimoto 	struct snd_soc_component *component;
86088bd870fSBenoit Cousson 	int i;
861db2a4165SFrank Mandarino 
862a655de80SLiam Girdwood 	if (dai_link->ignore)
863a655de80SLiam Girdwood 		return 0;
864a655de80SLiam Girdwood 
8656f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
8666308419aSMark Brown 
86749a5ba1cSMengdong Lin 	if (soc_is_dai_link_bound(card, dai_link)) {
86849a5ba1cSMengdong Lin 		dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
86949a5ba1cSMengdong Lin 			dai_link->name);
87049a5ba1cSMengdong Lin 		return 0;
87149a5ba1cSMengdong Lin 	}
872db2a4165SFrank Mandarino 
873513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
874513cb311SSudip Mukherjee 	if (!rtd)
875513cb311SSudip Mukherjee 		return -ENOMEM;
876513cb311SSudip Mukherjee 
87708a5841eSKuninori Morimoto 	/* FIXME: we need multi CPU support in the future */
87808a5841eSKuninori Morimoto 	rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
879b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
8806b490879SMartin Hundebøll 		dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
88108a5841eSKuninori Morimoto 			 dai_link->cpus->dai_name);
8821a497983SMengdong Lin 		goto _err_defer;
883c5af3a2eSMark Brown 	}
88490be711eSKuninori Morimoto 	snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
885c5af3a2eSMark Brown 
88688bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
887e2b30edfSKuninori Morimoto 	rtd->num_codecs = dai_link->num_codecs;
88834614739SJerome Brunet 	for_each_link_codecs(dai_link, i, codec) {
88934614739SJerome Brunet 		rtd->codec_dais[i] = snd_soc_find_dai(codec);
8900a2cfcd9SKuninori Morimoto 		if (!rtd->codec_dais[i]) {
8917c7e2d6aSStefan Agner 			dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
89234614739SJerome Brunet 				 codec->dai_name);
8931a497983SMengdong Lin 			goto _err_defer;
89412023a9aSMisael Lopez Cruz 		}
89534614739SJerome Brunet 
8960a2cfcd9SKuninori Morimoto 		snd_soc_rtdcom_add(rtd, rtd->codec_dais[i]->component);
89788bd870fSBenoit Cousson 	}
89888bd870fSBenoit Cousson 
89988bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
9000a2cfcd9SKuninori Morimoto 	rtd->codec_dai = rtd->codec_dais[0];
90112023a9aSMisael Lopez Cruz 
902e2b30edfSKuninori Morimoto 	/* Find PLATFORM from registered PLATFORMs */
90334614739SJerome Brunet 	for_each_link_platforms(dai_link, i, platform) {
904368dee94SKuninori Morimoto 		for_each_component(component) {
90534614739SJerome Brunet 			if (!snd_soc_is_matching_component(platform, component))
90690be711eSKuninori Morimoto 				continue;
90790be711eSKuninori Morimoto 
90890be711eSKuninori Morimoto 			snd_soc_rtdcom_add(rtd, component);
90990be711eSKuninori Morimoto 		}
91034614739SJerome Brunet 	}
91190be711eSKuninori Morimoto 
9121a497983SMengdong Lin 	soc_add_pcm_runtime(card, rtd);
913b19e6e7bSMark Brown 	return 0;
9141a497983SMengdong Lin 
9151a497983SMengdong Lin _err_defer:
9161a497983SMengdong Lin 	soc_free_pcm_runtime(rtd);
9171a497983SMengdong Lin 	return -EPROBE_DEFER;
9186b05eda6SMark Brown }
9196b05eda6SMark Brown 
92022d14231SKuninori Morimoto static void soc_cleanup_component(struct snd_soc_component *component)
92122d14231SKuninori Morimoto {
9223bb936f5SAmadeusz Sławiński 	snd_soc_component_set_jack(component, NULL, NULL);
92322d14231SKuninori Morimoto 	list_del(&component->card_list);
92422d14231SKuninori Morimoto 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
92522d14231SKuninori Morimoto 	soc_cleanup_component_debugfs(component);
92622d14231SKuninori Morimoto 	component->card = NULL;
9274a81e8f3SKuninori Morimoto 	snd_soc_component_module_put_when_close(component);
92822d14231SKuninori Morimoto }
92922d14231SKuninori Morimoto 
930f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
931d12cd198SStephen Warren {
932abd31b32SLars-Peter Clausen 	if (!component->card)
93370090bbbSLars-Peter Clausen 		return;
934d12cd198SStephen Warren 
93503b34dd7SKuninori Morimoto 	snd_soc_component_remove(component);
936d12cd198SStephen Warren 
93722d14231SKuninori Morimoto 	soc_cleanup_component(component);
938d12cd198SStephen Warren }
939d12cd198SStephen Warren 
940e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
941589c3563SJarkko Nikula {
942589c3563SJarkko Nikula 	int err;
943589c3563SJarkko Nikula 
94452abe6ccSGuennadi Liakhovetski 	if (!dai || !dai->probed || !dai->driver ||
9452eda3cb1SKuninori Morimoto 	    dai->driver->remove_order != order)
9462eda3cb1SKuninori Morimoto 		return;
9472eda3cb1SKuninori Morimoto 
948dcdab582SKuninori Morimoto 	err = snd_soc_dai_remove(dai);
949589c3563SJarkko Nikula 	if (err < 0)
950e60cd14fSLars-Peter Clausen 		dev_err(dai->dev,
951b0aa88afSMisael Lopez Cruz 			"ASoC: failed to remove %s: %d\n",
952e60cd14fSLars-Peter Clausen 			dai->name, err);
953dcdab582SKuninori Morimoto 
954e60cd14fSLars-Peter Clausen 	dai->probed = 0;
955b0aa88afSMisael Lopez Cruz }
956b0aa88afSMisael Lopez Cruz 
9571a497983SMengdong Lin static void soc_remove_link_dais(struct snd_soc_card *card,
9581a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
959f0fba2adSLiam Girdwood {
960e60cd14fSLars-Peter Clausen 	int i;
9610b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
962f0fba2adSLiam Girdwood 
963f0fba2adSLiam Girdwood 	/* unregister the rtd device */
964f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
96536ae1a96SMark Brown 		device_unregister(rtd->dev);
966f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
96702a06d30SBarry Song 	}
96802a06d30SBarry Song 
969f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
9700b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
9710b7990e3SKuninori Morimoto 		soc_remove_dai(codec_dai, order);
972f0fba2adSLiam Girdwood 
973e60cd14fSLars-Peter Clausen 	soc_remove_dai(rtd->cpu_dai, order);
974f0fba2adSLiam Girdwood }
975f0fba2adSLiam Girdwood 
9761a497983SMengdong Lin static void soc_remove_link_components(struct snd_soc_card *card,
9771a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, int order)
97862ae68faSStephen Warren {
97961aca564SLars-Peter Clausen 	struct snd_soc_component *component;
98090be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
98162ae68faSStephen Warren 
98290be711eSKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
98390be711eSKuninori Morimoto 		component = rtdcom->component;
98462ae68faSStephen Warren 
98570090bbbSLars-Peter Clausen 		if (component->driver->remove_order == order)
98661aca564SLars-Peter Clausen 			soc_remove_component(component);
98762ae68faSStephen Warren 	}
98862ae68faSStephen Warren }
98962ae68faSStephen Warren 
9900671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
9910671fd8eSKuninori Morimoto {
9921a497983SMengdong Lin 	int order;
9931a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
994f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
9950671fd8eSKuninori Morimoto 
9961a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
997bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd)
9981a497983SMengdong Lin 			soc_remove_link_dais(card, rtd, order);
9990168bf0dSLiam Girdwood 	}
100062ae68faSStephen Warren 
10011a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1002bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd)
10031a497983SMengdong Lin 			soc_remove_link_components(card, rtd, order);
100462ae68faSStephen Warren 	}
100562ae68faSStephen Warren 
100698061fdbSKuninori Morimoto 	for_each_card_links_safe(card, link, _link) {
1007f8f80361SMengdong Lin 		if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1008f8f80361SMengdong Lin 			dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1009f8f80361SMengdong Lin 				link->name);
1010f8f80361SMengdong Lin 
1011f8f80361SMengdong Lin 		list_del(&link->list);
10120671fd8eSKuninori Morimoto 	}
10130671fd8eSKuninori Morimoto }
10140671fd8eSKuninori Morimoto 
1015923c5e61SMengdong Lin static int soc_init_dai_link(struct snd_soc_card *card,
1016923c5e61SMengdong Lin 			     struct snd_soc_dai_link *link)
1017923c5e61SMengdong Lin {
1018adb76b5bSKuninori Morimoto 	int i;
101934614739SJerome Brunet 	struct snd_soc_dai_link_component *codec, *platform;
1020923c5e61SMengdong Lin 
10213db769f1SKuninori Morimoto 	for_each_link_codecs(link, i, codec) {
1022923c5e61SMengdong Lin 		/*
1023923c5e61SMengdong Lin 		 * Codec must be specified by 1 of name or OF node,
1024923c5e61SMengdong Lin 		 * not both or neither.
1025923c5e61SMengdong Lin 		 */
102634614739SJerome Brunet 		if (!!codec->name == !!codec->of_node) {
1027923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1028923c5e61SMengdong Lin 				link->name);
1029923c5e61SMengdong Lin 			return -EINVAL;
1030923c5e61SMengdong Lin 		}
1031af18b13fSJerome Brunet 
1032923c5e61SMengdong Lin 		/* Codec DAI name must be specified */
10333db769f1SKuninori Morimoto 		if (!codec->dai_name) {
1034923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1035923c5e61SMengdong Lin 				link->name);
1036923c5e61SMengdong Lin 			return -EINVAL;
1037923c5e61SMengdong Lin 		}
1038af18b13fSJerome Brunet 
1039af18b13fSJerome Brunet 		/*
1040af18b13fSJerome Brunet 		 * Defer card registration if codec component is not added to
1041af18b13fSJerome Brunet 		 * component list.
1042af18b13fSJerome Brunet 		 */
1043af18b13fSJerome Brunet 		if (!soc_find_component(codec))
1044af18b13fSJerome Brunet 			return -EPROBE_DEFER;
1045923c5e61SMengdong Lin 	}
1046923c5e61SMengdong Lin 
104734614739SJerome Brunet 	for_each_link_platforms(link, i, platform) {
10481d768989SKuninori Morimoto 		/*
104934614739SJerome Brunet 		 * Platform may be specified by either name or OF node, but it
105034614739SJerome Brunet 		 * can be left unspecified, then no components will be inserted
105134614739SJerome Brunet 		 * in the rtdcom list
10521d768989SKuninori Morimoto 		 */
105334614739SJerome Brunet 		if (!!platform->name == !!platform->of_node) {
1054910fdcabSKuninori Morimoto 			dev_err(card->dev,
105534614739SJerome Brunet 				"ASoC: Neither/both platform name/of_node are set for %s\n",
1056923c5e61SMengdong Lin 				link->name);
1057923c5e61SMengdong Lin 			return -EINVAL;
1058923c5e61SMengdong Lin 		}
10598780cf11SAjit Pandey 
10608780cf11SAjit Pandey 		/*
106134614739SJerome Brunet 		 * Defer card registration if platform component is not added to
106234614739SJerome Brunet 		 * component list.
10638780cf11SAjit Pandey 		 */
106434614739SJerome Brunet 		if (!soc_find_component(platform))
10658780cf11SAjit Pandey 			return -EPROBE_DEFER;
1066923c5e61SMengdong Lin 	}
1067923c5e61SMengdong Lin 
1068923c5e61SMengdong Lin 	/* FIXME */
106908a5841eSKuninori Morimoto 	if (link->num_cpus > 1) {
1070923c5e61SMengdong Lin 		dev_err(card->dev,
107108a5841eSKuninori Morimoto 			"ASoC: multi cpu is not yet supported %s\n",
1072923c5e61SMengdong Lin 			link->name);
1073923c5e61SMengdong Lin 		return -EINVAL;
1074923c5e61SMengdong Lin 	}
1075923c5e61SMengdong Lin 
1076923c5e61SMengdong Lin 	/*
1077923c5e61SMengdong Lin 	 * CPU device may be specified by either name or OF node, but
1078923c5e61SMengdong Lin 	 * can be left unspecified, and will be matched based on DAI
1079923c5e61SMengdong Lin 	 * name alone..
1080923c5e61SMengdong Lin 	 */
108108a5841eSKuninori Morimoto 	if (link->cpus->name && link->cpus->of_node) {
1082923c5e61SMengdong Lin 		dev_err(card->dev,
1083923c5e61SMengdong Lin 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
1084923c5e61SMengdong Lin 			link->name);
1085923c5e61SMengdong Lin 		return -EINVAL;
1086923c5e61SMengdong Lin 	}
10878780cf11SAjit Pandey 
10888780cf11SAjit Pandey 	/*
10898780cf11SAjit Pandey 	 * Defer card registartion if cpu dai component is not added to
10908780cf11SAjit Pandey 	 * component list.
10918780cf11SAjit Pandey 	 */
109208a5841eSKuninori Morimoto 	if ((link->cpus->of_node || link->cpus->name) &&
1093c1e230f0SKuninori Morimoto 	    !soc_find_component(link->cpus))
10948780cf11SAjit Pandey 		return -EPROBE_DEFER;
10958780cf11SAjit Pandey 
1096923c5e61SMengdong Lin 	/*
1097923c5e61SMengdong Lin 	 * At least one of CPU DAI name or CPU device name/node must be
1098923c5e61SMengdong Lin 	 * specified
1099923c5e61SMengdong Lin 	 */
110008a5841eSKuninori Morimoto 	if (!link->cpus->dai_name &&
110108a5841eSKuninori Morimoto 	    !(link->cpus->name || link->cpus->of_node)) {
1102923c5e61SMengdong Lin 		dev_err(card->dev,
1103923c5e61SMengdong Lin 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1104923c5e61SMengdong Lin 			link->name);
1105923c5e61SMengdong Lin 		return -EINVAL;
1106923c5e61SMengdong Lin 	}
1107923c5e61SMengdong Lin 
1108923c5e61SMengdong Lin 	return 0;
1109923c5e61SMengdong Lin }
1110923c5e61SMengdong Lin 
1111ef2e8175SKuninori Morimoto void snd_soc_disconnect_sync(struct device *dev)
1112ef2e8175SKuninori Morimoto {
11132c7b696aSMarcel Ziswiler 	struct snd_soc_component *component =
11142c7b696aSMarcel Ziswiler 			snd_soc_lookup_component(dev, NULL);
1115ef2e8175SKuninori Morimoto 
1116ef2e8175SKuninori Morimoto 	if (!component || !component->card)
1117ef2e8175SKuninori Morimoto 		return;
1118ef2e8175SKuninori Morimoto 
1119ef2e8175SKuninori Morimoto 	snd_card_disconnect_sync(component->card->snd_card);
1120ef2e8175SKuninori Morimoto }
1121df532185SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
1122ef2e8175SKuninori Morimoto 
1123f8f80361SMengdong Lin /**
1124f8f80361SMengdong Lin  * snd_soc_add_dai_link - Add a DAI link dynamically
1125f8f80361SMengdong Lin  * @card: The ASoC card to which the DAI link is added
1126f8f80361SMengdong Lin  * @dai_link: The new DAI link to add
1127f8f80361SMengdong Lin  *
1128f8f80361SMengdong Lin  * This function adds a DAI link to the ASoC card's link list.
1129f8f80361SMengdong Lin  *
1130f8f80361SMengdong Lin  * Note: Topology can use this API to add DAI links when probing the
1131f8f80361SMengdong Lin  * topology component. And machine drivers can still define static
1132f8f80361SMengdong Lin  * DAI links in dai_link array.
1133f8f80361SMengdong Lin  */
1134f8f80361SMengdong Lin int snd_soc_add_dai_link(struct snd_soc_card *card,
1135f8f80361SMengdong Lin 		struct snd_soc_dai_link *dai_link)
1136f8f80361SMengdong Lin {
1137f8f80361SMengdong Lin 	if (dai_link->dobj.type
1138f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1139f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1140f8f80361SMengdong Lin 			dai_link->dobj.type);
1141f8f80361SMengdong Lin 		return -EINVAL;
1142f8f80361SMengdong Lin 	}
1143f8f80361SMengdong Lin 
1144f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
11452c7b696aSMarcel Ziswiler 	/*
11462c7b696aSMarcel Ziswiler 	 * Notify the machine driver for extra initialization
1147d6f220eaSMengdong Lin 	 * on the link created by topology.
1148d6f220eaSMengdong Lin 	 */
1149d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->add_dai_link)
1150d6f220eaSMengdong Lin 		card->add_dai_link(card, dai_link);
1151d6f220eaSMengdong Lin 
1152f8f80361SMengdong Lin 	list_add_tail(&dai_link->list, &card->dai_link_list);
1153f8f80361SMengdong Lin 
1154f8f80361SMengdong Lin 	return 0;
1155f8f80361SMengdong Lin }
1156f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1157f8f80361SMengdong Lin 
1158f8f80361SMengdong Lin /**
1159f8f80361SMengdong Lin  * snd_soc_remove_dai_link - Remove a DAI link from the list
1160f8f80361SMengdong Lin  * @card: The ASoC card that owns the link
1161f8f80361SMengdong Lin  * @dai_link: The DAI link to remove
1162f8f80361SMengdong Lin  *
1163f8f80361SMengdong Lin  * This function removes a DAI link from the ASoC card's link list.
1164f8f80361SMengdong Lin  *
1165f8f80361SMengdong Lin  * For DAI links previously added by topology, topology should
1166f8f80361SMengdong Lin  * remove them by using the dobj embedded in the link.
1167f8f80361SMengdong Lin  */
1168f8f80361SMengdong Lin void snd_soc_remove_dai_link(struct snd_soc_card *card,
1169f8f80361SMengdong Lin 			     struct snd_soc_dai_link *dai_link)
1170f8f80361SMengdong Lin {
1171f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
1172f8f80361SMengdong Lin 
1173f8f80361SMengdong Lin 	if (dai_link->dobj.type
1174f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1175f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1176f8f80361SMengdong Lin 			dai_link->dobj.type);
1177f8f80361SMengdong Lin 		return;
1178f8f80361SMengdong Lin 	}
1179f8f80361SMengdong Lin 
1180f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
11812c7b696aSMarcel Ziswiler 	/*
11822c7b696aSMarcel Ziswiler 	 * Notify the machine driver for extra destruction
1183d6f220eaSMengdong Lin 	 * on the link created by topology.
1184d6f220eaSMengdong Lin 	 */
1185d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->remove_dai_link)
1186d6f220eaSMengdong Lin 		card->remove_dai_link(card, dai_link);
1187d6f220eaSMengdong Lin 
118898061fdbSKuninori Morimoto 	for_each_card_links_safe(card, link, _link) {
1189f8f80361SMengdong Lin 		if (link == dai_link) {
1190f8f80361SMengdong Lin 			list_del(&link->list);
1191f8f80361SMengdong Lin 			return;
1192f8f80361SMengdong Lin 		}
1193f8f80361SMengdong Lin 	}
1194f8f80361SMengdong Lin }
1195f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1196f0fba2adSLiam Girdwood 
1197aefba455SJerome Brunet static void soc_set_of_name_prefix(struct snd_soc_component *component)
1198aefba455SJerome Brunet {
1199c0834440SKuninori Morimoto 	struct device_node *component_of_node = soc_component_to_node(component);
1200aefba455SJerome Brunet 	const char *str;
1201aefba455SJerome Brunet 	int ret;
1202aefba455SJerome Brunet 
1203aefba455SJerome Brunet 	ret = of_property_read_string(component_of_node, "sound-name-prefix",
1204aefba455SJerome Brunet 				      &str);
1205aefba455SJerome Brunet 	if (!ret)
1206aefba455SJerome Brunet 		component->name_prefix = str;
1207aefba455SJerome Brunet }
1208aefba455SJerome Brunet 
1209ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
121094f99c87SLars-Peter Clausen 				struct snd_soc_component *component)
1211ead9b919SJarkko Nikula {
1212ead9b919SJarkko Nikula 	int i;
1213ead9b919SJarkko Nikula 
1214aefba455SJerome Brunet 	for (i = 0; i < card->num_configs && card->codec_conf; i++) {
1215ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1216c0834440SKuninori Morimoto 		struct device_node *component_of_node = soc_component_to_node(component);
1217b24c539bSCharles Keepax 
1218b24c539bSCharles Keepax 		if (map->of_node && component_of_node != map->of_node)
12193ca041edSSebastian Reichel 			continue;
122094f99c87SLars-Peter Clausen 		if (map->dev_name && strcmp(component->name, map->dev_name))
12213ca041edSSebastian Reichel 			continue;
122294f99c87SLars-Peter Clausen 		component->name_prefix = map->name_prefix;
1223aefba455SJerome Brunet 		return;
1224ead9b919SJarkko Nikula 	}
1225aefba455SJerome Brunet 
1226aefba455SJerome Brunet 	/*
1227aefba455SJerome Brunet 	 * If there is no configuration table or no match in the table,
1228aefba455SJerome Brunet 	 * check if a prefix is provided in the node
1229aefba455SJerome Brunet 	 */
1230aefba455SJerome Brunet 	soc_set_of_name_prefix(component);
1231ead9b919SJarkko Nikula }
1232ead9b919SJarkko Nikula 
1233f1d45cc3SLars-Peter Clausen static int soc_probe_component(struct snd_soc_card *card,
1234f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component)
1235589c3563SJarkko Nikula {
12362c7b696aSMarcel Ziswiler 	struct snd_soc_dapm_context *dapm =
12372c7b696aSMarcel Ziswiler 			snd_soc_component_get_dapm(component);
1238888df395SMark Brown 	struct snd_soc_dai *dai;
1239f1d45cc3SLars-Peter Clausen 	int ret;
1240589c3563SJarkko Nikula 
12411b7c1231SLars-Peter Clausen 	if (!strcmp(component->name, "snd-soc-dummy"))
124270090bbbSLars-Peter Clausen 		return 0;
1243589c3563SJarkko Nikula 
1244abd31b32SLars-Peter Clausen 	if (component->card) {
12451b7c1231SLars-Peter Clausen 		if (component->card != card) {
12461b7c1231SLars-Peter Clausen 			dev_err(component->dev,
12471b7c1231SLars-Peter Clausen 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
12481b7c1231SLars-Peter Clausen 				card->name, component->card->name);
12491b7c1231SLars-Peter Clausen 			return -ENODEV;
12501b7c1231SLars-Peter Clausen 		}
12511b7c1231SLars-Peter Clausen 		return 0;
12521b7c1231SLars-Peter Clausen 	}
12531b7c1231SLars-Peter Clausen 
12544a81e8f3SKuninori Morimoto 	ret = snd_soc_component_module_get_when_probe(component);
12554a81e8f3SKuninori Morimoto 	if (ret < 0)
12564a81e8f3SKuninori Morimoto 		return ret;
1257abd31b32SLars-Peter Clausen 
1258f1d45cc3SLars-Peter Clausen 	component->card = card;
1259f1d45cc3SLars-Peter Clausen 	dapm->card = card;
126022d14231SKuninori Morimoto 	INIT_LIST_HEAD(&component->card_list);
126122d14231SKuninori Morimoto 	INIT_LIST_HEAD(&dapm->list);
1262f1d45cc3SLars-Peter Clausen 	soc_set_name_prefix(card, component);
1263589c3563SJarkko Nikula 
1264f1d45cc3SLars-Peter Clausen 	soc_init_component_debugfs(component);
1265d5d1e0beSLars-Peter Clausen 
1266688d0ebfSKuninori Morimoto 	if (component->driver->dapm_widgets) {
1267688d0ebfSKuninori Morimoto 		ret = snd_soc_dapm_new_controls(dapm,
1268688d0ebfSKuninori Morimoto 					component->driver->dapm_widgets,
1269688d0ebfSKuninori Morimoto 					component->driver->num_dapm_widgets);
127077530150SLars-Peter Clausen 
1271b318ad50SNariman Poushin 		if (ret != 0) {
1272f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1273b318ad50SNariman Poushin 				"Failed to create new controls %d\n", ret);
1274b318ad50SNariman Poushin 			goto err_probe;
1275b318ad50SNariman Poushin 		}
1276b318ad50SNariman Poushin 	}
1277b318ad50SNariman Poushin 
127815a0c645SKuninori Morimoto 	for_each_component_dais(component, dai) {
12790634814fSLars-Peter Clausen 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1280261edc70SNariman Poushin 		if (ret != 0) {
1281f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1282261edc70SNariman Poushin 				"Failed to create DAI widgets %d\n", ret);
1283261edc70SNariman Poushin 			goto err_probe;
1284261edc70SNariman Poushin 		}
1285261edc70SNariman Poushin 	}
1286888df395SMark Brown 
128708e837ddSKuninori Morimoto 	ret = snd_soc_component_probe(component);
1288589c3563SJarkko Nikula 	if (ret < 0) {
1289f1d45cc3SLars-Peter Clausen 		dev_err(component->dev,
1290f1d45cc3SLars-Peter Clausen 			"ASoC: failed to probe component %d\n", ret);
129170d29331SJarkko Nikula 		goto err_probe;
1292589c3563SJarkko Nikula 	}
1293f1d45cc3SLars-Peter Clausen 	WARN(dapm->idle_bias_off &&
1294f1d45cc3SLars-Peter Clausen 	     dapm->bias_level != SND_SOC_BIAS_OFF,
129510e8aa9aSMichał Mirosław 	     "codec %s can not start from non-off bias with idle_bias_off==1\n",
1296f1d45cc3SLars-Peter Clausen 	     component->name);
1297589c3563SJarkko Nikula 
1298f2ed6b07SMengdong Lin 	/* machine specific init */
1299f2ed6b07SMengdong Lin 	if (component->init) {
1300f2ed6b07SMengdong Lin 		ret = component->init(component);
1301f2ed6b07SMengdong Lin 		if (ret < 0) {
1302f2ed6b07SMengdong Lin 			dev_err(component->dev,
1303f2ed6b07SMengdong Lin 				"Failed to do machine specific init %d\n", ret);
1304f2ed6b07SMengdong Lin 			goto err_probe;
1305f2ed6b07SMengdong Lin 		}
1306f2ed6b07SMengdong Lin 	}
1307f2ed6b07SMengdong Lin 
1308b8972bf0SKuninori Morimoto 	if (component->driver->controls)
1309b8972bf0SKuninori Morimoto 		snd_soc_add_component_controls(component,
1310b8972bf0SKuninori Morimoto 					       component->driver->controls,
1311b8972bf0SKuninori Morimoto 					       component->driver->num_controls);
13126969b2baSKuninori Morimoto 	if (component->driver->dapm_routes)
13136969b2baSKuninori Morimoto 		snd_soc_dapm_add_routes(dapm,
13146969b2baSKuninori Morimoto 					component->driver->dapm_routes,
13156969b2baSKuninori Morimoto 					component->driver->num_dapm_routes);
131689b95ac0SMark Brown 
1317f1d45cc3SLars-Peter Clausen 	list_add(&dapm->list, &card->dapm_list);
1318f70f18f7SKuninori Morimoto 	/* see for_each_card_components */
1319d9fc4063SKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1320589c3563SJarkko Nikula 
132170d29331SJarkko Nikula err_probe:
132222d14231SKuninori Morimoto 	if (ret < 0)
132322d14231SKuninori Morimoto 		soc_cleanup_component(component);
1324956245e9SLiam Girdwood 
1325956245e9SLiam Girdwood 	return ret;
1326956245e9SLiam Girdwood }
1327956245e9SLiam Girdwood 
132836ae1a96SMark Brown static void rtd_release(struct device *dev)
132936ae1a96SMark Brown {
133036ae1a96SMark Brown 	kfree(dev);
133136ae1a96SMark Brown }
1332f0fba2adSLiam Girdwood 
13335f3484acSLars-Peter Clausen static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
13345f3484acSLars-Peter Clausen 	const char *name)
1335503ae5e0SMisael Lopez Cruz {
1336589c3563SJarkko Nikula 	int ret = 0;
1337589c3563SJarkko Nikula 
1338589c3563SJarkko Nikula 	/* register the rtd device */
133936ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
134036ae1a96SMark Brown 	if (!rtd->dev)
134136ae1a96SMark Brown 		return -ENOMEM;
134236ae1a96SMark Brown 	device_initialize(rtd->dev);
13435f3484acSLars-Peter Clausen 	rtd->dev->parent = rtd->card->dev;
134436ae1a96SMark Brown 	rtd->dev->release = rtd_release;
1345d29697dcSTakashi Iwai 	rtd->dev->groups = soc_dev_attr_groups;
1346f294afedSLars-Peter Clausen 	dev_set_name(rtd->dev, "%s", name);
134736ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1348b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
134901d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
135001d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
135101d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
135201d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
135336ae1a96SMark Brown 	ret = device_add(rtd->dev);
1354589c3563SJarkko Nikula 	if (ret < 0) {
1355865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1356865df9cbSChuansheng Liu 		put_device(rtd->dev);
13575f3484acSLars-Peter Clausen 		dev_err(rtd->card->dev,
1358f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1359589c3563SJarkko Nikula 		return ret;
1360589c3563SJarkko Nikula 	}
1361589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1362589c3563SJarkko Nikula 	return 0;
1363589c3563SJarkko Nikula }
1364589c3563SJarkko Nikula 
13651a497983SMengdong Lin static int soc_probe_link_components(struct snd_soc_card *card,
13662c7b696aSMarcel Ziswiler 				     struct snd_soc_pcm_runtime *rtd, int order)
136762ae68faSStephen Warren {
1368f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component;
136990be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
137090be711eSKuninori Morimoto 	int ret;
137162ae68faSStephen Warren 
137290be711eSKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
137390be711eSKuninori Morimoto 		component = rtdcom->component;
137490be711eSKuninori Morimoto 
137570090bbbSLars-Peter Clausen 		if (component->driver->probe_order == order) {
1376f1d45cc3SLars-Peter Clausen 			ret = soc_probe_component(card, component);
137762ae68faSStephen Warren 			if (ret < 0)
137862ae68faSStephen Warren 				return ret;
137962ae68faSStephen Warren 		}
138062ae68faSStephen Warren 	}
138162ae68faSStephen Warren 
138262ae68faSStephen Warren 	return 0;
138362ae68faSStephen Warren }
138462ae68faSStephen Warren 
13858e2be562SLars-Peter Clausen static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1386b0aa88afSMisael Lopez Cruz {
1387cfd9b5fbSKuninori Morimoto 	int ret;
1388cfd9b5fbSKuninori Morimoto 
13897a2ccad5SKuninori Morimoto 	if (dai->probed ||
13907a2ccad5SKuninori Morimoto 	    dai->driver->probe_order != order)
13917a2ccad5SKuninori Morimoto 		return 0;
1392b0aa88afSMisael Lopez Cruz 
1393cfd9b5fbSKuninori Morimoto 	ret = snd_soc_dai_probe(dai);
1394b0aa88afSMisael Lopez Cruz 	if (ret < 0) {
13957a2ccad5SKuninori Morimoto 		dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
13968e2be562SLars-Peter Clausen 			dai->name, ret);
1397b0aa88afSMisael Lopez Cruz 		return ret;
1398b0aa88afSMisael Lopez Cruz 	}
1399b0aa88afSMisael Lopez Cruz 
14008e2be562SLars-Peter Clausen 	dai->probed = 1;
1401b0aa88afSMisael Lopez Cruz 
1402b0aa88afSMisael Lopez Cruz 	return 0;
1403b0aa88afSMisael Lopez Cruz }
1404b0aa88afSMisael Lopez Cruz 
140525f7b701SArnaud Pouliquen static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
140625f7b701SArnaud Pouliquen 				struct snd_soc_pcm_runtime *rtd)
140725f7b701SArnaud Pouliquen {
140825f7b701SArnaud Pouliquen 	int i, ret = 0;
140925f7b701SArnaud Pouliquen 
141025f7b701SArnaud Pouliquen 	for (i = 0; i < num_dais; ++i) {
141125f7b701SArnaud Pouliquen 		struct snd_soc_dai_driver *drv = dais[i]->driver;
141225f7b701SArnaud Pouliquen 
1413de17f14eSRohit kumar 		if (drv->pcm_new)
141425f7b701SArnaud Pouliquen 			ret = drv->pcm_new(rtd, dais[i]);
141525f7b701SArnaud Pouliquen 		if (ret < 0) {
141625f7b701SArnaud Pouliquen 			dev_err(dais[i]->dev,
141725f7b701SArnaud Pouliquen 				"ASoC: Failed to bind %s with pcm device\n",
141825f7b701SArnaud Pouliquen 				dais[i]->name);
141925f7b701SArnaud Pouliquen 			return ret;
142025f7b701SArnaud Pouliquen 		}
142125f7b701SArnaud Pouliquen 	}
142225f7b701SArnaud Pouliquen 
142325f7b701SArnaud Pouliquen 	return 0;
142425f7b701SArnaud Pouliquen }
142525f7b701SArnaud Pouliquen 
14261a497983SMengdong Lin static int soc_probe_link_dais(struct snd_soc_card *card,
14271a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1428f0fba2adSLiam Girdwood {
14291a497983SMengdong Lin 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1430c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1431a655de80SLiam Girdwood 	struct snd_soc_rtdcom_list *rtdcom;
1432a655de80SLiam Girdwood 	struct snd_soc_component *component;
14330b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1434a655de80SLiam Girdwood 	int i, ret, num;
1435f0fba2adSLiam Girdwood 
1436f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
14371a497983SMengdong Lin 			card->name, rtd->num, order);
1438f0fba2adSLiam Girdwood 
1439f0fba2adSLiam Girdwood 	/* set default power off timeout */
1440f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1441f0fba2adSLiam Girdwood 
14428e2be562SLars-Peter Clausen 	ret = soc_probe_dai(cpu_dai, order);
14438e2be562SLars-Peter Clausen 	if (ret)
1444f0fba2adSLiam Girdwood 		return ret;
1445f0fba2adSLiam Girdwood 
1446f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
14470b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
14480b7990e3SKuninori Morimoto 		ret = soc_probe_dai(codec_dai, order);
1449b0aa88afSMisael Lopez Cruz 		if (ret)
1450f0fba2adSLiam Girdwood 			return ret;
145188bd870fSBenoit Cousson 	}
1452f0fba2adSLiam Girdwood 
14530168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
14540168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
14550168bf0dSLiam Girdwood 		return 0;
14560168bf0dSLiam Girdwood 
14575f3484acSLars-Peter Clausen 	/* do machine specific initialization */
14585f3484acSLars-Peter Clausen 	if (dai_link->init) {
14595f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
14605f3484acSLars-Peter Clausen 		if (ret < 0) {
14615f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
14625f3484acSLars-Peter Clausen 				dai_link->name, ret);
14635f3484acSLars-Peter Clausen 			return ret;
14645f3484acSLars-Peter Clausen 		}
14655f3484acSLars-Peter Clausen 	}
14665f3484acSLars-Peter Clausen 
146740aa5383SRicard Wanderlof 	if (dai_link->dai_fmt) {
146840aa5383SRicard Wanderlof 		ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
146940aa5383SRicard Wanderlof 		if (ret)
147040aa5383SRicard Wanderlof 			return ret;
147140aa5383SRicard Wanderlof 	}
1472a5053a8eSKuninori Morimoto 
14735f3484acSLars-Peter Clausen 	ret = soc_post_component_init(rtd, dai_link->name);
1474589c3563SJarkko Nikula 	if (ret)
1475f0fba2adSLiam Girdwood 		return ret;
1476f0fba2adSLiam Girdwood 
14775f3484acSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
14785f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
14792e55b90aSLars-Peter Clausen 	if (dai_link->dynamic)
14802e55b90aSLars-Peter Clausen 		soc_dpcm_debugfs_add(rtd);
14815f3484acSLars-Peter Clausen #endif
14825f3484acSLars-Peter Clausen 
1483a655de80SLiam Girdwood 	num = rtd->num;
1484a655de80SLiam Girdwood 
1485a655de80SLiam Girdwood 	/*
1486a655de80SLiam Girdwood 	 * most drivers will register their PCMs using DAI link ordering but
1487a655de80SLiam Girdwood 	 * topology based drivers can use the DAI link id field to set PCM
1488a655de80SLiam Girdwood 	 * device number and then use rtd + a base offset of the BEs.
1489a655de80SLiam Girdwood 	 */
1490a655de80SLiam Girdwood 	for_each_rtdcom(rtd, rtdcom) {
1491a655de80SLiam Girdwood 		component = rtdcom->component;
1492a655de80SLiam Girdwood 
1493a655de80SLiam Girdwood 		if (!component->driver->use_dai_pcm_id)
1494a655de80SLiam Girdwood 			continue;
1495a655de80SLiam Girdwood 
1496a655de80SLiam Girdwood 		if (rtd->dai_link->no_pcm)
1497a655de80SLiam Girdwood 			num += component->driver->be_pcm_base;
1498a655de80SLiam Girdwood 		else
1499a655de80SLiam Girdwood 			num = rtd->dai_link->id;
1500a655de80SLiam Girdwood 	}
1501a655de80SLiam Girdwood 
1502b423c420SKuninori Morimoto 	/* create compress_device if possible */
1503b423c420SKuninori Morimoto 	ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1504b423c420SKuninori Morimoto 	if (ret != -ENOTSUPP) {
1505b423c420SKuninori Morimoto 		if (ret < 0)
1506f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
15071245b700SNamarta Kohli 					 dai_link->stream_name);
15081245b700SNamarta Kohli 		return ret;
15091245b700SNamarta Kohli 	}
1510b423c420SKuninori Morimoto 
1511f0fba2adSLiam Girdwood 	/* create the pcm */
1512a655de80SLiam Girdwood 	ret = soc_new_pcm(rtd, num);
1513f0fba2adSLiam Girdwood 	if (ret < 0) {
1514f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
15150837fc62SFabio Estevam 			dai_link->stream_name, ret);
1516f0fba2adSLiam Girdwood 		return ret;
1517f0fba2adSLiam Girdwood 	}
151825f7b701SArnaud Pouliquen 	ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
151925f7b701SArnaud Pouliquen 	if (ret < 0)
152025f7b701SArnaud Pouliquen 		return ret;
152125f7b701SArnaud Pouliquen 	ret = soc_link_dai_pcm_new(rtd->codec_dais,
152225f7b701SArnaud Pouliquen 				   rtd->num_codecs, rtd);
152325f7b701SArnaud Pouliquen 	return ret;
1524f0fba2adSLiam Girdwood }
1525f0fba2adSLiam Girdwood 
152644c69bb1SLars-Peter Clausen static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1527b19e6e7bSMark Brown {
15283ca041edSSebastian Reichel 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1529f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1530c1e230f0SKuninori Morimoto 	struct snd_soc_dai_link_component dlc;
15313ca041edSSebastian Reichel 
1532f2ed6b07SMengdong Lin 	if (aux_dev->codec_of_node || aux_dev->codec_name) {
1533f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1534c1e230f0SKuninori Morimoto 		dlc.name = aux_dev->codec_name;
1535c1e230f0SKuninori Morimoto 		dlc.of_node = aux_dev->codec_of_node;
1536c1e230f0SKuninori Morimoto 		component = soc_find_component(&dlc);
1537f2ed6b07SMengdong Lin 		if (!component) {
1538c1e230f0SKuninori Morimoto 			if (dlc.of_node)
1539c1e230f0SKuninori Morimoto 				dlc.name = of_node_full_name(dlc.of_node);
1540f2ed6b07SMengdong Lin 			goto err_defer;
1541f2ed6b07SMengdong Lin 		}
1542f2ed6b07SMengdong Lin 	} else if (aux_dev->name) {
1543f2ed6b07SMengdong Lin 		/* generic components */
1544c1e230f0SKuninori Morimoto 		dlc.name = aux_dev->name;
1545c1e230f0SKuninori Morimoto 		dlc.of_node = NULL;
1546c1e230f0SKuninori Morimoto 		component = soc_find_component(&dlc);
1547f2ed6b07SMengdong Lin 		if (!component)
1548f2ed6b07SMengdong Lin 			goto err_defer;
1549f2ed6b07SMengdong Lin 	} else {
1550f2ed6b07SMengdong Lin 		dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1551f2ed6b07SMengdong Lin 		return -EINVAL;
1552f2ed6b07SMengdong Lin 	}
15533ca041edSSebastian Reichel 
1554f2ed6b07SMengdong Lin 	component->init = aux_dev->init;
1555d2e3a135SSylwester Nawrocki 	list_add(&component->card_aux_list, &card->aux_comp_list);
15561a653aa4SKuninori Morimoto 
1557f2ed6b07SMengdong Lin 	return 0;
1558f2ed6b07SMengdong Lin 
1559f2ed6b07SMengdong Lin err_defer:
1560c1e230f0SKuninori Morimoto 	dev_err(card->dev, "ASoC: %s not registered\n", dlc.name);
1561b19e6e7bSMark Brown 	return -EPROBE_DEFER;
1562b19e6e7bSMark Brown }
1563b19e6e7bSMark Brown 
1564f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1565f2ed6b07SMengdong Lin {
1566991454e1SKuninori Morimoto 	struct snd_soc_component *comp;
1567f2ed6b07SMengdong Lin 	int order;
1568f2ed6b07SMengdong Lin 	int ret;
1569f2ed6b07SMengdong Lin 
15701a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1571991454e1SKuninori Morimoto 		list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
1572f2ed6b07SMengdong Lin 			if (comp->driver->probe_order == order) {
1573f2ed6b07SMengdong Lin 				ret = soc_probe_component(card,	comp);
1574f2ed6b07SMengdong Lin 				if (ret < 0) {
1575f2ed6b07SMengdong Lin 					dev_err(card->dev,
1576f2ed6b07SMengdong Lin 						"ASoC: failed to probe aux component %s %d\n",
1577f2ed6b07SMengdong Lin 						comp->name, ret);
1578f2ed6b07SMengdong Lin 					return ret;
1579f2ed6b07SMengdong Lin 				}
1580f2ed6b07SMengdong Lin 			}
1581f2ed6b07SMengdong Lin 		}
1582f2ed6b07SMengdong Lin 	}
158365d9361fSLars-Peter Clausen 
158444c69bb1SLars-Peter Clausen 	return 0;
15853ca041edSSebastian Reichel }
15862eea392dSJarkko Nikula 
1587f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
158844c69bb1SLars-Peter Clausen {
1589f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1590f2ed6b07SMengdong Lin 	int order;
159144c69bb1SLars-Peter Clausen 
15921a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1593f2ed6b07SMengdong Lin 		list_for_each_entry_safe(comp, _comp,
1594991454e1SKuninori Morimoto 			&card->aux_comp_list, card_aux_list) {
15951a653aa4SKuninori Morimoto 
1596f2ed6b07SMengdong Lin 			if (comp->driver->remove_order == order) {
1597f2ed6b07SMengdong Lin 				soc_remove_component(comp);
1598991454e1SKuninori Morimoto 				/* remove it from the card's aux_comp_list */
1599991454e1SKuninori Morimoto 				list_del(&comp->card_aux_list);
16002eea392dSJarkko Nikula 			}
16015f3484acSLars-Peter Clausen 		}
16022eea392dSJarkko Nikula 	}
16032eea392dSJarkko Nikula }
16042eea392dSJarkko Nikula 
1605ce64c8b9SLars-Peter Clausen /**
1606ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1607ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1608ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1609ce64c8b9SLars-Peter Clausen  *
1610ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1611ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1612ce64c8b9SLars-Peter Clausen  *
1613ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1614ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1615ce64c8b9SLars-Peter Clausen  *
1616ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1617ce64c8b9SLars-Peter Clausen  */
1618ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1619ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1620ce64c8b9SLars-Peter Clausen {
1621ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
16220b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1623ce64c8b9SLars-Peter Clausen 	unsigned int i;
1624ce64c8b9SLars-Peter Clausen 	int ret;
1625ce64c8b9SLars-Peter Clausen 
16260b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1627ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1628ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1629ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1630ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1631ce64c8b9SLars-Peter Clausen 			return ret;
1632ce64c8b9SLars-Peter Clausen 		}
1633ce64c8b9SLars-Peter Clausen 	}
1634ce64c8b9SLars-Peter Clausen 
16352c7b696aSMarcel Ziswiler 	/*
16362c7b696aSMarcel Ziswiler 	 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
16372c7b696aSMarcel Ziswiler 	 * the component which has non_legacy_dai_naming is Codec
16382c7b696aSMarcel Ziswiler 	 */
1639999f7f5aSKuninori Morimoto 	if (cpu_dai->component->driver->non_legacy_dai_naming) {
1640ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1641ce64c8b9SLars-Peter Clausen 
1642ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1643ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1644ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1645ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1646ce64c8b9SLars-Peter Clausen 			break;
1647ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1648ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1649ce64c8b9SLars-Peter Clausen 			break;
1650ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1651ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1652ce64c8b9SLars-Peter Clausen 			break;
1653ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1654ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1655ce64c8b9SLars-Peter Clausen 			break;
1656ce64c8b9SLars-Peter Clausen 		}
1657ce64c8b9SLars-Peter Clausen 
1658ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1659ce64c8b9SLars-Peter Clausen 	}
1660ce64c8b9SLars-Peter Clausen 
1661ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1662ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1663ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1664ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1665ce64c8b9SLars-Peter Clausen 		return ret;
1666ce64c8b9SLars-Peter Clausen 	}
1667ce64c8b9SLars-Peter Clausen 
1668ce64c8b9SLars-Peter Clausen 	return 0;
1669ce64c8b9SLars-Peter Clausen }
1670ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1671ce64c8b9SLars-Peter Clausen 
16721f5a4535STakashi Iwai #ifdef CONFIG_DMI
16732c7b696aSMarcel Ziswiler /*
16742c7b696aSMarcel Ziswiler  * Trim special characters, and replace '-' with '_' since '-' is used to
1675345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1676345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1677345233d7SLiam Girdwood  */
1678345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1679345233d7SLiam Girdwood {
1680345233d7SLiam Girdwood 	int i, j = 0;
1681345233d7SLiam Girdwood 
1682345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1683345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1684345233d7SLiam Girdwood 		    || (name[i] == '_'))
1685345233d7SLiam Girdwood 			name[j++] = name[i];
1686345233d7SLiam Girdwood 		else if (name[i] == '-')
1687345233d7SLiam Girdwood 			name[j++] = '_';
1688345233d7SLiam Girdwood 	}
1689345233d7SLiam Girdwood 
1690345233d7SLiam Girdwood 	name[j] = '\0';
1691345233d7SLiam Girdwood }
1692345233d7SLiam Girdwood 
16932c7b696aSMarcel Ziswiler /*
16942c7b696aSMarcel Ziswiler  * Check if a DMI field is valid, i.e. not containing any string
169598faf436SMengdong Lin  * in the black list.
169698faf436SMengdong Lin  */
169798faf436SMengdong Lin static int is_dmi_valid(const char *field)
169898faf436SMengdong Lin {
169998faf436SMengdong Lin 	int i = 0;
170098faf436SMengdong Lin 
170198faf436SMengdong Lin 	while (dmi_blacklist[i]) {
170298faf436SMengdong Lin 		if (strstr(field, dmi_blacklist[i]))
170398faf436SMengdong Lin 			return 0;
170498faf436SMengdong Lin 		i++;
170546b5a4d2SWu Fengguang 	}
170698faf436SMengdong Lin 
170798faf436SMengdong Lin 	return 1;
170898faf436SMengdong Lin }
170998faf436SMengdong Lin 
1710345233d7SLiam Girdwood /**
1711345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
1712345233d7SLiam Girdwood  * @card: The card to register DMI names
1713345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
1714345233d7SLiam Girdwood  *
1715345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
1716345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
1717345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
1718345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
1719345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
1720345233d7SLiam Girdwood  * as the sound card long name, in the format of
1721345233d7SLiam Girdwood  * "vendor-product-version-board"
1722345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
1723345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
1724345233d7SLiam Girdwood  * (UCM) configurations for the card.
1725345233d7SLiam Girdwood  *
1726345233d7SLiam Girdwood  * Possible card long names may be:
1727345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
1728345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1729345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1730345233d7SLiam Girdwood  *
1731345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
1732345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
1733345233d7SLiam Girdwood  *
1734345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
1735345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
1736345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
1737345233d7SLiam Girdwood  * charactors like SPACE.
1738345233d7SLiam Girdwood  *
1739345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
1740345233d7SLiam Girdwood  */
1741345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1742345233d7SLiam Girdwood {
1743345233d7SLiam Girdwood 	const char *vendor, *product, *product_version, *board;
1744345233d7SLiam Girdwood 	size_t longname_buf_size = sizeof(card->snd_card->longname);
1745345233d7SLiam Girdwood 	size_t len;
1746345233d7SLiam Girdwood 
1747345233d7SLiam Girdwood 	if (card->long_name)
1748345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
1749345233d7SLiam Girdwood 
1750345233d7SLiam Girdwood 	/* make up dmi long name as: vendor.product.version.board */
1751345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
175298faf436SMengdong Lin 	if (!vendor || !is_dmi_valid(vendor)) {
1753345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1754345233d7SLiam Girdwood 		return 0;
1755345233d7SLiam Girdwood 	}
1756345233d7SLiam Girdwood 
1757345233d7SLiam Girdwood 	snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1758345233d7SLiam Girdwood 			 "%s", vendor);
1759345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
1760345233d7SLiam Girdwood 
1761345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
176298faf436SMengdong Lin 	if (product && is_dmi_valid(product)) {
1763345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1764345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1765345233d7SLiam Girdwood 			 longname_buf_size - len,
1766345233d7SLiam Girdwood 			 "-%s", product);
1767345233d7SLiam Girdwood 
1768345233d7SLiam Girdwood 		len++;	/* skip the separator "-" */
1769345233d7SLiam Girdwood 		if (len < longname_buf_size)
1770345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1771345233d7SLiam Girdwood 
17722c7b696aSMarcel Ziswiler 		/*
17732c7b696aSMarcel Ziswiler 		 * some vendors like Lenovo may only put a self-explanatory
1774345233d7SLiam Girdwood 		 * name in the product version field
1775345233d7SLiam Girdwood 		 */
1776345233d7SLiam Girdwood 		product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
177798faf436SMengdong Lin 		if (product_version && is_dmi_valid(product_version)) {
1778345233d7SLiam Girdwood 			len = strlen(card->dmi_longname);
1779345233d7SLiam Girdwood 			snprintf(card->dmi_longname + len,
1780345233d7SLiam Girdwood 				 longname_buf_size - len,
1781345233d7SLiam Girdwood 				 "-%s", product_version);
1782345233d7SLiam Girdwood 
1783345233d7SLiam Girdwood 			len++;
1784345233d7SLiam Girdwood 			if (len < longname_buf_size)
1785345233d7SLiam Girdwood 				cleanup_dmi_name(card->dmi_longname + len);
1786345233d7SLiam Girdwood 		}
1787345233d7SLiam Girdwood 	}
1788345233d7SLiam Girdwood 
1789345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
179098faf436SMengdong Lin 	if (board && is_dmi_valid(board)) {
1791345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1792345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1793345233d7SLiam Girdwood 			 longname_buf_size - len,
1794345233d7SLiam Girdwood 			 "-%s", board);
1795345233d7SLiam Girdwood 
1796345233d7SLiam Girdwood 		len++;
1797345233d7SLiam Girdwood 		if (len < longname_buf_size)
1798345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1799345233d7SLiam Girdwood 	} else if (!product) {
1800345233d7SLiam Girdwood 		/* fall back to using legacy name */
1801345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1802345233d7SLiam Girdwood 		return 0;
1803345233d7SLiam Girdwood 	}
1804345233d7SLiam Girdwood 
1805345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
1806345233d7SLiam Girdwood 	if (flavour) {
1807345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1808345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1809345233d7SLiam Girdwood 			 longname_buf_size - len,
1810345233d7SLiam Girdwood 			 "-%s", flavour);
1811345233d7SLiam Girdwood 
1812345233d7SLiam Girdwood 		len++;
1813345233d7SLiam Girdwood 		if (len < longname_buf_size)
1814345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1815345233d7SLiam Girdwood 	}
1816345233d7SLiam Girdwood 
1817345233d7SLiam Girdwood 	/* set the card long name */
1818345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
1819345233d7SLiam Girdwood 
1820345233d7SLiam Girdwood 	return 0;
1821345233d7SLiam Girdwood }
1822345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
18231f5a4535STakashi Iwai #endif /* CONFIG_DMI */
1824345233d7SLiam Girdwood 
1825a655de80SLiam Girdwood static void soc_check_tplg_fes(struct snd_soc_card *card)
1826a655de80SLiam Girdwood {
1827a655de80SLiam Girdwood 	struct snd_soc_component *component;
1828a655de80SLiam Girdwood 	const struct snd_soc_component_driver *comp_drv;
1829a655de80SLiam Girdwood 	struct snd_soc_dai_link *dai_link;
1830a655de80SLiam Girdwood 	int i;
1831a655de80SLiam Girdwood 
1832368dee94SKuninori Morimoto 	for_each_component(component) {
1833a655de80SLiam Girdwood 
1834a655de80SLiam Girdwood 		/* does this component override FEs ? */
1835a655de80SLiam Girdwood 		if (!component->driver->ignore_machine)
1836a655de80SLiam Girdwood 			continue;
1837a655de80SLiam Girdwood 
1838a655de80SLiam Girdwood 		/* for this machine ? */
1839e194098bSPierre-Louis Bossart 		if (!strcmp(component->driver->ignore_machine,
1840a655de80SLiam Girdwood 			    card->dev->driver->name))
1841e194098bSPierre-Louis Bossart 			goto match;
1842e194098bSPierre-Louis Bossart 		if (strcmp(component->driver->ignore_machine,
1843e194098bSPierre-Louis Bossart 			   dev_name(card->dev)))
1844a655de80SLiam Girdwood 			continue;
1845e194098bSPierre-Louis Bossart match:
1846a655de80SLiam Girdwood 		/* machine matches, so override the rtd data */
18477fe072b4SKuninori Morimoto 		for_each_card_prelinks(card, i, dai_link) {
1848a655de80SLiam Girdwood 
1849a655de80SLiam Girdwood 			/* ignore this FE */
1850a655de80SLiam Girdwood 			if (dai_link->dynamic) {
1851a655de80SLiam Girdwood 				dai_link->ignore = true;
1852a655de80SLiam Girdwood 				continue;
1853a655de80SLiam Girdwood 			}
1854a655de80SLiam Girdwood 
1855a655de80SLiam Girdwood 			dev_info(card->dev, "info: override FE DAI link %s\n",
1856a655de80SLiam Girdwood 				 card->dai_link[i].name);
1857a655de80SLiam Girdwood 
1858a655de80SLiam Girdwood 			/* override platform component */
1859adb76b5bSKuninori Morimoto 			if (!dai_link->platforms) {
1860daecf46eSKuninori Morimoto 				dev_err(card->dev, "init platform error");
1861daecf46eSKuninori Morimoto 				continue;
1862daecf46eSKuninori Morimoto 			}
1863910fdcabSKuninori Morimoto 			dai_link->platforms->name = component->name;
1864a655de80SLiam Girdwood 
1865a655de80SLiam Girdwood 			/* convert non BE into BE */
1866a655de80SLiam Girdwood 			dai_link->no_pcm = 1;
1867a655de80SLiam Girdwood 
1868a655de80SLiam Girdwood 			/* override any BE fixups */
1869a655de80SLiam Girdwood 			dai_link->be_hw_params_fixup =
1870a655de80SLiam Girdwood 				component->driver->be_hw_params_fixup;
1871a655de80SLiam Girdwood 
18722c7b696aSMarcel Ziswiler 			/*
18732c7b696aSMarcel Ziswiler 			 * most BE links don't set stream name, so set it to
1874a655de80SLiam Girdwood 			 * dai link name if it's NULL to help bind widgets.
1875a655de80SLiam Girdwood 			 */
1876a655de80SLiam Girdwood 			if (!dai_link->stream_name)
1877a655de80SLiam Girdwood 				dai_link->stream_name = dai_link->name;
1878a655de80SLiam Girdwood 		}
1879a655de80SLiam Girdwood 
1880a655de80SLiam Girdwood 		/* Inform userspace we are using alternate topology */
1881a655de80SLiam Girdwood 		if (component->driver->topology_name_prefix) {
1882a655de80SLiam Girdwood 
1883a655de80SLiam Girdwood 			/* topology shortname created? */
1884a655de80SLiam Girdwood 			if (!card->topology_shortname_created) {
1885a655de80SLiam Girdwood 				comp_drv = component->driver;
1886a655de80SLiam Girdwood 
1887a655de80SLiam Girdwood 				snprintf(card->topology_shortname, 32, "%s-%s",
1888a655de80SLiam Girdwood 					 comp_drv->topology_name_prefix,
1889a655de80SLiam Girdwood 					 card->name);
1890a655de80SLiam Girdwood 				card->topology_shortname_created = true;
1891a655de80SLiam Girdwood 			}
1892a655de80SLiam Girdwood 
1893a655de80SLiam Girdwood 			/* use topology shortname */
1894a655de80SLiam Girdwood 			card->name = card->topology_shortname;
1895a655de80SLiam Girdwood 		}
1896a655de80SLiam Girdwood 	}
1897a655de80SLiam Girdwood }
1898a655de80SLiam Girdwood 
189953e947a0SKuninori Morimoto static int soc_cleanup_card_resources(struct snd_soc_card *card)
190053e947a0SKuninori Morimoto {
190153e947a0SKuninori Morimoto 	/* free the ALSA card at first; this syncs with pending operations */
190229040d1aSKuninori Morimoto 	if (card->snd_card) {
190353e947a0SKuninori Morimoto 		snd_card_free(card->snd_card);
190429040d1aSKuninori Morimoto 		card->snd_card = NULL;
190529040d1aSKuninori Morimoto 	}
190653e947a0SKuninori Morimoto 
190753e947a0SKuninori Morimoto 	/* remove and free each DAI */
190853e947a0SKuninori Morimoto 	soc_remove_dai_links(card);
190953e947a0SKuninori Morimoto 	soc_remove_pcm_runtimes(card);
191053e947a0SKuninori Morimoto 
191153e947a0SKuninori Morimoto 	/* remove auxiliary devices */
191253e947a0SKuninori Morimoto 	soc_remove_aux_devices(card);
191353e947a0SKuninori Morimoto 
191453e947a0SKuninori Morimoto 	snd_soc_dapm_free(&card->dapm);
191553e947a0SKuninori Morimoto 	soc_cleanup_card_debugfs(card);
191653e947a0SKuninori Morimoto 
191753e947a0SKuninori Morimoto 	/* remove the card */
191853e947a0SKuninori Morimoto 	if (card->remove)
191953e947a0SKuninori Morimoto 		card->remove(card);
192053e947a0SKuninori Morimoto 
192153e947a0SKuninori Morimoto 	return 0;
192253e947a0SKuninori Morimoto }
192353e947a0SKuninori Morimoto 
1924b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1925f0fba2adSLiam Girdwood {
19261a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
192761b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
1928ce64c8b9SLars-Peter Clausen 	int ret, i, order;
192996dd3622SMark Brown 
193034e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
193170fc5373STzung-Bi Shih 	for_each_card_prelinks(card, i, dai_link) {
193270fc5373STzung-Bi Shih 		ret = soc_init_dai_link(card, dai_link);
193370fc5373STzung-Bi Shih 		if (ret) {
193470fc5373STzung-Bi Shih 			dev_err(card->dev, "ASoC: failed to init link %s: %d\n",
193570fc5373STzung-Bi Shih 				dai_link->name, ret);
193670fc5373STzung-Bi Shih 			mutex_unlock(&client_mutex);
193770fc5373STzung-Bi Shih 			return ret;
193870fc5373STzung-Bi Shih 		}
193970fc5373STzung-Bi Shih 	}
194001b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1941f0fba2adSLiam Girdwood 
194253e947a0SKuninori Morimoto 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
194353e947a0SKuninori Morimoto 	card->dapm.dev = card->dev;
194453e947a0SKuninori Morimoto 	card->dapm.card = card;
194553e947a0SKuninori Morimoto 	list_add(&card->dapm.list, &card->dapm_list);
194653e947a0SKuninori Morimoto 
1947a655de80SLiam Girdwood 	/* check whether any platform is ignore machine FE and using topology */
1948a655de80SLiam Girdwood 	soc_check_tplg_fes(card);
1949a655de80SLiam Girdwood 
1950b19e6e7bSMark Brown 	/* bind DAIs */
19517fe072b4SKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link) {
19527fe072b4SKuninori Morimoto 		ret = soc_bind_dai_link(card, dai_link);
1953b19e6e7bSMark Brown 		if (ret != 0)
195453e947a0SKuninori Morimoto 			goto probe_end;
1955db2a4165SFrank Mandarino 	}
1956db2a4165SFrank Mandarino 
195744c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
1958b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
195944c69bb1SLars-Peter Clausen 		ret = soc_bind_aux_dev(card, i);
1960b19e6e7bSMark Brown 		if (ret != 0)
196153e947a0SKuninori Morimoto 			goto probe_end;
1962db2a4165SFrank Mandarino 	}
1963db2a4165SFrank Mandarino 
1964f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
19657fe072b4SKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link)
19667fe072b4SKuninori Morimoto 		snd_soc_add_dai_link(card, dai_link);
1967f8f80361SMengdong Lin 
1968f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1969102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1970f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1971f0fba2adSLiam Girdwood 	if (ret < 0) {
197210e8aa9aSMichał Mirosław 		dev_err(card->dev,
197310e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
197410e8aa9aSMichał Mirosław 			card->name, ret);
197553e947a0SKuninori Morimoto 		goto probe_end;
1976db2a4165SFrank Mandarino 	}
1977db2a4165SFrank Mandarino 
19780757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
19790757d834SLars-Peter Clausen 
1980d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
1981d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1982d5d1e0beSLars-Peter Clausen #endif
1983d5d1e0beSLars-Peter Clausen 
198488ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
19856ed25978SAndy Green 	/* deferred resume work */
19866308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
19871301a964SRandy Dunlap #endif
19886ed25978SAndy Green 
19899a841ebbSMark Brown 	if (card->dapm_widgets)
19909a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
19919a841ebbSMark Brown 					  card->num_dapm_widgets);
19929a841ebbSMark Brown 
1993f23e860eSNicolin Chen 	if (card->of_dapm_widgets)
1994f23e860eSNicolin Chen 		snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1995f23e860eSNicolin Chen 					  card->num_of_dapm_widgets);
1996f23e860eSNicolin Chen 
1997f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
1998f0fba2adSLiam Girdwood 	if (card->probe) {
1999e7361ec4SMark Brown 		ret = card->probe(card);
2000f0fba2adSLiam Girdwood 		if (ret < 0)
200153e947a0SKuninori Morimoto 			goto probe_end;
2002f0fba2adSLiam Girdwood 	}
2003f0fba2adSLiam Girdwood 
200462ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
20051a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
2006bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
20071a497983SMengdong Lin 			ret = soc_probe_link_components(card, rtd, order);
200862ae68faSStephen Warren 			if (ret < 0) {
2009f110bfc7SLiam Girdwood 				dev_err(card->dev,
2010f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
2011f110bfc7SLiam Girdwood 					ret);
201253e947a0SKuninori Morimoto 				goto probe_end;
201362ae68faSStephen Warren 			}
201462ae68faSStephen Warren 		}
201562ae68faSStephen Warren 	}
201662ae68faSStephen Warren 
2017f2ed6b07SMengdong Lin 	/* probe auxiliary components */
2018f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
2019f2ed6b07SMengdong Lin 	if (ret < 0)
202053e947a0SKuninori Morimoto 		goto probe_end;
2021f2ed6b07SMengdong Lin 
20222c7b696aSMarcel Ziswiler 	/*
20232c7b696aSMarcel Ziswiler 	 * Find new DAI links added during probing components and bind them.
202461b0088bSMengdong Lin 	 * Components with topology may bring new DAIs and DAI links.
202561b0088bSMengdong Lin 	 */
202698061fdbSKuninori Morimoto 	for_each_card_links(card, dai_link) {
202761b0088bSMengdong Lin 		if (soc_is_dai_link_bound(card, dai_link))
202861b0088bSMengdong Lin 			continue;
202961b0088bSMengdong Lin 
203061b0088bSMengdong Lin 		ret = soc_init_dai_link(card, dai_link);
203161b0088bSMengdong Lin 		if (ret)
203253e947a0SKuninori Morimoto 			goto probe_end;
203361b0088bSMengdong Lin 		ret = soc_bind_dai_link(card, dai_link);
203461b0088bSMengdong Lin 		if (ret)
203553e947a0SKuninori Morimoto 			goto probe_end;
203661b0088bSMengdong Lin 	}
203761b0088bSMengdong Lin 
203862ae68faSStephen Warren 	/* probe all DAI links on this card */
20391a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
2040bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
20411a497983SMengdong Lin 			ret = soc_probe_link_dais(card, rtd, order);
2042fe3e78e0SMark Brown 			if (ret < 0) {
2043f110bfc7SLiam Girdwood 				dev_err(card->dev,
2044f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
2045f110bfc7SLiam Girdwood 					ret);
204653e947a0SKuninori Morimoto 				goto probe_end;
2047fe3e78e0SMark Brown 			}
2048fe3e78e0SMark Brown 		}
20490168bf0dSLiam Girdwood 	}
2050fe3e78e0SMark Brown 
2051888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
2052b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
2053888df395SMark Brown 
2054b7af1dafSMark Brown 	if (card->controls)
20552c7b696aSMarcel Ziswiler 		snd_soc_add_card_controls(card, card->controls,
20562c7b696aSMarcel Ziswiler 					  card->num_controls);
2057b7af1dafSMark Brown 
2058b8ad29deSMark Brown 	if (card->dapm_routes)
2059b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2060b8ad29deSMark Brown 					card->num_dapm_routes);
2061b8ad29deSMark Brown 
2062f23e860eSNicolin Chen 	if (card->of_dapm_routes)
2063f23e860eSNicolin Chen 		snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2064f23e860eSNicolin Chen 					card->num_of_dapm_routes);
206575d9ac46SMark Brown 
2066861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
2067861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
2068861886d3STakashi Iwai 
2069f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
2070fe3e78e0SMark Brown 		 "%s", card->name);
2071f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
207222de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
2073f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2074f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
2075f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2076f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
2077f0e8ed85SMark Brown 		case '_':
2078f0e8ed85SMark Brown 		case '-':
2079f0e8ed85SMark Brown 		case '\0':
2080f0e8ed85SMark Brown 			break;
2081f0e8ed85SMark Brown 		default:
2082f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
2083f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
2084f0e8ed85SMark Brown 			break;
2085f0e8ed85SMark Brown 		}
2086f0e8ed85SMark Brown 	}
2087fe3e78e0SMark Brown 
208828e9ad92SMark Brown 	if (card->late_probe) {
208928e9ad92SMark Brown 		ret = card->late_probe(card);
209028e9ad92SMark Brown 		if (ret < 0) {
2091f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
209228e9ad92SMark Brown 				card->name, ret);
209353e947a0SKuninori Morimoto 			goto probe_end;
209428e9ad92SMark Brown 		}
209528e9ad92SMark Brown 	}
209628e9ad92SMark Brown 
2097824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
20988c193b8dSLars-Peter Clausen 
2099f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2100fe3e78e0SMark Brown 	if (ret < 0) {
2101f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2102f110bfc7SLiam Girdwood 				ret);
210353e947a0SKuninori Morimoto 		goto probe_end;
2104fe3e78e0SMark Brown 	}
2105fe3e78e0SMark Brown 
2106435c5e25SMark Brown 	card->instantiated = 1;
2107882eab6cSTzung-Bi Shih 	dapm_mark_endpoints_dirty(card);
21084f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2109b19e6e7bSMark Brown 
211053e947a0SKuninori Morimoto probe_end:
211153e947a0SKuninori Morimoto 	if (ret < 0)
211253e947a0SKuninori Morimoto 		soc_cleanup_card_resources(card);
2113db2a4165SFrank Mandarino 
2114f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
211534e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2116db2a4165SFrank Mandarino 
2117b19e6e7bSMark Brown 	return ret;
2118435c5e25SMark Brown }
2119435c5e25SMark Brown 
2120435c5e25SMark Brown /* probes a new socdev */
2121435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2122435c5e25SMark Brown {
2123f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2124435c5e25SMark Brown 
212570a7ca34SVinod Koul 	/*
212670a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
212770a7ca34SVinod Koul 	 * we should not be here in that case so ret error
212870a7ca34SVinod Koul 	 */
212970a7ca34SVinod Koul 	if (!card)
213070a7ca34SVinod Koul 		return -EINVAL;
213170a7ca34SVinod Koul 
2132fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2133f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2134fe4085e8SMark Brown 		 card->name);
2135fe4085e8SMark Brown 
2136435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2137435c5e25SMark Brown 	card->dev = &pdev->dev;
2138f0fba2adSLiam Girdwood 
213928d528c8SMark Brown 	return snd_soc_register_card(card);
2140435c5e25SMark Brown }
2141435c5e25SMark Brown 
2142b0e26485SVinod Koul /* removes a socdev */
2143b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2144b0e26485SVinod Koul {
2145b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2146b0e26485SVinod Koul 
2147c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2148db2a4165SFrank Mandarino 	return 0;
2149db2a4165SFrank Mandarino }
2150db2a4165SFrank Mandarino 
21516f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
215251737470SMark Brown {
21536f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
21541a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
215551737470SMark Brown 
215651737470SMark Brown 	if (!card->instantiated)
2157416356fcSMark Brown 		return 0;
215851737470SMark Brown 
21592c7b696aSMarcel Ziswiler 	/*
21602c7b696aSMarcel Ziswiler 	 * Flush out pmdown_time work - we actually do want to run it
21612c7b696aSMarcel Ziswiler 	 * now, we're shutting down so no imminent restart.
21622c7b696aSMarcel Ziswiler 	 */
216365462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
216451737470SMark Brown 
2165f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2166416356fcSMark Brown 
2167988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
2168bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
216988bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
21700b7990e3SKuninori Morimoto 		struct snd_soc_dai *codec_dai;
21711a497983SMengdong Lin 		int i;
217288bd870fSBenoit Cousson 
2173988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
21740b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
217588bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
217688bd870fSBenoit Cousson 		}
2177988e8cc4SNicolin Chen 	}
2178988e8cc4SNicolin Chen 
2179416356fcSMark Brown 	return 0;
218051737470SMark Brown }
21816f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
218251737470SMark Brown 
21836f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2184b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2185b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2186b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2187b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
21886f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2189b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2190416356fcSMark Brown };
2191deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2192416356fcSMark Brown 
2193db2a4165SFrank Mandarino /* ASoC platform driver */
2194db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2195db2a4165SFrank Mandarino 	.driver		= {
2196db2a4165SFrank Mandarino 		.name		= "soc-audio",
21976f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2198db2a4165SFrank Mandarino 	},
2199db2a4165SFrank Mandarino 	.probe		= soc_probe,
2200db2a4165SFrank Mandarino 	.remove		= soc_remove,
2201db2a4165SFrank Mandarino };
2202db2a4165SFrank Mandarino 
2203096e49d5SMark Brown /**
2204db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2205db2a4165SFrank Mandarino  * @_template: control template
2206db2a4165SFrank Mandarino  * @data: control private data
2207ac11a2b3SMark Brown  * @long_name: control long name
2208efb7ac3fSMark Brown  * @prefix: control name prefix
2209db2a4165SFrank Mandarino  *
2210db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2211db2a4165SFrank Mandarino  *
2212db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2213db2a4165SFrank Mandarino  */
2214db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
22153056557fSMark Brown 				  void *data, const char *long_name,
2216efb7ac3fSMark Brown 				  const char *prefix)
2217db2a4165SFrank Mandarino {
2218db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2219efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2220efb7ac3fSMark Brown 	char *name = NULL;
2221db2a4165SFrank Mandarino 
2222db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2223db2a4165SFrank Mandarino 	template.index = 0;
2224db2a4165SFrank Mandarino 
2225efb7ac3fSMark Brown 	if (!long_name)
2226efb7ac3fSMark Brown 		long_name = template.name;
2227efb7ac3fSMark Brown 
2228efb7ac3fSMark Brown 	if (prefix) {
22292b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2230efb7ac3fSMark Brown 		if (!name)
2231efb7ac3fSMark Brown 			return NULL;
2232efb7ac3fSMark Brown 
2233efb7ac3fSMark Brown 		template.name = name;
2234efb7ac3fSMark Brown 	} else {
2235efb7ac3fSMark Brown 		template.name = long_name;
2236efb7ac3fSMark Brown 	}
2237efb7ac3fSMark Brown 
2238efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2239efb7ac3fSMark Brown 
2240efb7ac3fSMark Brown 	kfree(name);
2241efb7ac3fSMark Brown 
2242efb7ac3fSMark Brown 	return kcontrol;
2243db2a4165SFrank Mandarino }
2244db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2245db2a4165SFrank Mandarino 
2246022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2247022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2248022658beSLiam Girdwood 	const char *prefix, void *data)
2249022658beSLiam Girdwood {
2250022658beSLiam Girdwood 	int err, i;
2251022658beSLiam Girdwood 
2252022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2253022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
22542c7b696aSMarcel Ziswiler 
2255022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2256022658beSLiam Girdwood 						     control->name, prefix));
2257022658beSLiam Girdwood 		if (err < 0) {
2258f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2259f110bfc7SLiam Girdwood 				control->name, err);
2260022658beSLiam Girdwood 			return err;
2261022658beSLiam Girdwood 		}
2262022658beSLiam Girdwood 	}
2263022658beSLiam Girdwood 
2264022658beSLiam Girdwood 	return 0;
2265022658beSLiam Girdwood }
2266022658beSLiam Girdwood 
22674fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
22684fefd698SDimitris Papastamos 					       const char *name)
22694fefd698SDimitris Papastamos {
22704fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
22714fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
22724fefd698SDimitris Papastamos 
22734fefd698SDimitris Papastamos 	if (unlikely(!name))
22744fefd698SDimitris Papastamos 		return NULL;
22754fefd698SDimitris Papastamos 
22764fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
22774fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
22784fefd698SDimitris Papastamos 			return kctl;
22794fefd698SDimitris Papastamos 	return NULL;
22804fefd698SDimitris Papastamos }
22814fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
22824fefd698SDimitris Papastamos 
2283db2a4165SFrank Mandarino /**
22840f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
22850f2780adSLars-Peter Clausen  *
22860f2780adSLars-Peter Clausen  * @component: Component to add controls to
22870f2780adSLars-Peter Clausen  * @controls: Array of controls to add
22880f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
22890f2780adSLars-Peter Clausen  *
22900f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
22910f2780adSLars-Peter Clausen  */
22920f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
22930f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
22940f2780adSLars-Peter Clausen {
22950f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
22960f2780adSLars-Peter Clausen 
22970f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
22980f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
22990f2780adSLars-Peter Clausen }
23000f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
23010f2780adSLars-Peter Clausen 
23020f2780adSLars-Peter Clausen /**
2303022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2304022658beSLiam Girdwood  * Convenience function to add a list of controls.
2305022658beSLiam Girdwood  *
2306022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2307022658beSLiam Girdwood  * @controls: array of controls to add
2308022658beSLiam Girdwood  * @num_controls: number of elements in the array
2309022658beSLiam Girdwood  *
2310022658beSLiam Girdwood  * Return 0 for success, else error.
2311022658beSLiam Girdwood  */
2312022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2313022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2314022658beSLiam Girdwood {
2315022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2316022658beSLiam Girdwood 
2317022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2318022658beSLiam Girdwood 			NULL, soc_card);
2319022658beSLiam Girdwood }
2320022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2321022658beSLiam Girdwood 
2322022658beSLiam Girdwood /**
2323022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2324022658beSLiam Girdwood  * Convienience function to add a list of controls.
2325022658beSLiam Girdwood  *
2326022658beSLiam Girdwood  * @dai: DAI to add controls to
2327022658beSLiam Girdwood  * @controls: array of controls to add
2328022658beSLiam Girdwood  * @num_controls: number of elements in the array
2329022658beSLiam Girdwood  *
2330022658beSLiam Girdwood  * Return 0 for success, else error.
2331022658beSLiam Girdwood  */
2332022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2333022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2334022658beSLiam Girdwood {
2335313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2336022658beSLiam Girdwood 
2337022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2338022658beSLiam Girdwood 			NULL, dai);
2339022658beSLiam Girdwood }
2340022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2341022658beSLiam Girdwood 
2342e894efefSSrinivas Kandagatla static int snd_soc_bind_card(struct snd_soc_card *card)
2343e894efefSSrinivas Kandagatla {
2344e894efefSSrinivas Kandagatla 	struct snd_soc_pcm_runtime *rtd;
2345e894efefSSrinivas Kandagatla 	int ret;
2346e894efefSSrinivas Kandagatla 
2347e894efefSSrinivas Kandagatla 	ret = snd_soc_instantiate_card(card);
2348e894efefSSrinivas Kandagatla 	if (ret != 0)
2349e894efefSSrinivas Kandagatla 		return ret;
2350e894efefSSrinivas Kandagatla 
2351e894efefSSrinivas Kandagatla 	/* deactivate pins to sleep state */
2352bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
2353e894efefSSrinivas Kandagatla 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2354e894efefSSrinivas Kandagatla 		struct snd_soc_dai *codec_dai;
2355e894efefSSrinivas Kandagatla 		int j;
2356e894efefSSrinivas Kandagatla 
2357e894efefSSrinivas Kandagatla 		for_each_rtd_codec_dai(rtd, j, codec_dai) {
2358e894efefSSrinivas Kandagatla 			if (!codec_dai->active)
2359e894efefSSrinivas Kandagatla 				pinctrl_pm_select_sleep_state(codec_dai->dev);
2360e894efefSSrinivas Kandagatla 		}
2361e894efefSSrinivas Kandagatla 
2362e894efefSSrinivas Kandagatla 		if (!cpu_dai->active)
2363e894efefSSrinivas Kandagatla 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
2364e894efefSSrinivas Kandagatla 	}
2365e894efefSSrinivas Kandagatla 
2366e894efefSSrinivas Kandagatla 	return ret;
2367e894efefSSrinivas Kandagatla }
2368e894efefSSrinivas Kandagatla 
2369c5af3a2eSMark Brown /**
2370c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2371c5af3a2eSMark Brown  *
2372ac11a2b3SMark Brown  * @card: Card to register
2373c5af3a2eSMark Brown  *
2374c5af3a2eSMark Brown  */
237570a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2376c5af3a2eSMark Brown {
2377c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2378c5af3a2eSMark Brown 		return -EINVAL;
2379c5af3a2eSMark Brown 
2380ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2381ed77cc12SMark Brown 
2382111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
2383111c6419SStephen Warren 
2384f8f80361SMengdong Lin 	INIT_LIST_HEAD(&card->dai_link_list);
2385f0fba2adSLiam Girdwood 
23861a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
23879115171aSMark Brown 	card->num_rtd = 0;
23888c6529dbSLiam Girdwood 
2389db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
23908a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2391dee89c4dSMark Brown 	card->instantiated = 0;
2392dee89c4dSMark Brown 	mutex_init(&card->mutex);
2393a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
2394a9764869SKaiChieh Chuang 	spin_lock_init(&card->dpcm_lock);
23958c6529dbSLiam Girdwood 
2396e894efefSSrinivas Kandagatla 	return snd_soc_bind_card(card);
23978c6529dbSLiam Girdwood }
239870a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
23998c6529dbSLiam Girdwood 
2400e894efefSSrinivas Kandagatla static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2401e894efefSSrinivas Kandagatla {
2402f96fb7d1SRanjani Sridharan 	struct snd_soc_pcm_runtime *rtd;
2403f96fb7d1SRanjani Sridharan 	int order;
2404f96fb7d1SRanjani Sridharan 
2405e894efefSSrinivas Kandagatla 	if (card->instantiated) {
2406e894efefSSrinivas Kandagatla 		card->instantiated = false;
2407e894efefSSrinivas Kandagatla 		snd_soc_dapm_shutdown(card);
240853e947a0SKuninori Morimoto 		snd_soc_flush_all_delayed_work(card);
2409f96fb7d1SRanjani Sridharan 
2410f96fb7d1SRanjani Sridharan 		/* remove all components used by DAI links on this card */
2411f96fb7d1SRanjani Sridharan 		for_each_comp_order(order) {
2412f96fb7d1SRanjani Sridharan 			for_each_card_rtds(card, rtd) {
2413f96fb7d1SRanjani Sridharan 				soc_remove_link_components(card, rtd, order);
2414f96fb7d1SRanjani Sridharan 			}
2415f96fb7d1SRanjani Sridharan 		}
2416f96fb7d1SRanjani Sridharan 
2417e894efefSSrinivas Kandagatla 		soc_cleanup_card_resources(card);
2418e894efefSSrinivas Kandagatla 		if (!unregister)
2419e894efefSSrinivas Kandagatla 			list_add(&card->list, &unbind_card_list);
2420e894efefSSrinivas Kandagatla 	} else {
2421e894efefSSrinivas Kandagatla 		if (unregister)
2422e894efefSSrinivas Kandagatla 			list_del(&card->list);
2423e894efefSSrinivas Kandagatla 	}
2424e894efefSSrinivas Kandagatla }
2425e894efefSSrinivas Kandagatla 
24268c6529dbSLiam Girdwood /**
24278c6529dbSLiam Girdwood  * snd_soc_unregister_card - Unregister a card with the ASoC core
24288c6529dbSLiam Girdwood  *
2429dee89c4dSMark Brown  * @card: Card to unregister
2430dee89c4dSMark Brown  *
24318c6529dbSLiam Girdwood  */
243270a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
24338c6529dbSLiam Girdwood {
2434b545542aSKuninori Morimoto 	mutex_lock(&client_mutex);
2435e894efefSSrinivas Kandagatla 	snd_soc_unbind_card(card, true);
2436b545542aSKuninori Morimoto 	mutex_unlock(&client_mutex);
2437f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
24388c6529dbSLiam Girdwood 
24398c6529dbSLiam Girdwood 	return 0;
24408c6529dbSLiam Girdwood }
244170a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
24428c6529dbSLiam Girdwood 
24438c6529dbSLiam Girdwood /*
24448c6529dbSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
24458c6529dbSLiam Girdwood  * and sanitizing names.
24468c6529dbSLiam Girdwood  */
24470b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
24488c6529dbSLiam Girdwood {
24498c6529dbSLiam Girdwood 	char *found, name[NAME_SIZE];
24508c6529dbSLiam Girdwood 	int id1, id2;
24518c6529dbSLiam Girdwood 
24528c6529dbSLiam Girdwood 	if (dev_name(dev) == NULL)
24538c6529dbSLiam Girdwood 		return NULL;
24548c6529dbSLiam Girdwood 
245558818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
24568c6529dbSLiam Girdwood 
24578c6529dbSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
2458c5af3a2eSMark Brown 	found = strstr(name, dev->driver->name);
2459c5af3a2eSMark Brown 	if (found) {
2460c5af3a2eSMark Brown 		/* get ID */
2461c5af3a2eSMark Brown 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2462c5af3a2eSMark Brown 
2463c5af3a2eSMark Brown 			/* discard ID from name if ID == -1 */
2464c5af3a2eSMark Brown 			if (*id == -1)
2465c5af3a2eSMark Brown 				found[strlen(dev->driver->name)] = '\0';
2466c5af3a2eSMark Brown 		}
2467c5af3a2eSMark Brown 
2468c5af3a2eSMark Brown 	} else {
2469c5af3a2eSMark Brown 		/* I2C component devices are named "bus-addr" */
2470c5af3a2eSMark Brown 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2471c5af3a2eSMark Brown 			char tmp[NAME_SIZE];
2472c5af3a2eSMark Brown 
2473c5af3a2eSMark Brown 			/* create unique ID number from I2C addr and bus */
2474c5af3a2eSMark Brown 			*id = ((id1 & 0xffff) << 16) + id2;
2475c5af3a2eSMark Brown 
2476c5af3a2eSMark Brown 			/* sanitize component name for DAI link creation */
24772c7b696aSMarcel Ziswiler 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
24782c7b696aSMarcel Ziswiler 				 name);
247958818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2480c5af3a2eSMark Brown 		} else
2481c5af3a2eSMark Brown 			*id = 0;
2482c5af3a2eSMark Brown 	}
2483c5af3a2eSMark Brown 
2484c5af3a2eSMark Brown 	return kstrdup(name, GFP_KERNEL);
2485c5af3a2eSMark Brown }
2486c5af3a2eSMark Brown 
2487c5af3a2eSMark Brown /*
2488c5af3a2eSMark Brown  * Simplify DAI link naming for single devices with multiple DAIs by removing
2489c5af3a2eSMark Brown  * any ".-1" and using the DAI name (instead of device name).
2490c5af3a2eSMark Brown  */
2491c5af3a2eSMark Brown static inline char *fmt_multiple_name(struct device *dev,
2492c5af3a2eSMark Brown 		struct snd_soc_dai_driver *dai_drv)
2493c5af3a2eSMark Brown {
2494c5af3a2eSMark Brown 	if (dai_drv->name == NULL) {
249510e8aa9aSMichał Mirosław 		dev_err(dev,
249610e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
249710e8aa9aSMichał Mirosław 			dev_name(dev));
2498c5af3a2eSMark Brown 		return NULL;
2499c5af3a2eSMark Brown 	}
2500c5af3a2eSMark Brown 
2501c5af3a2eSMark Brown 	return kstrdup(dai_drv->name, GFP_KERNEL);
2502c5af3a2eSMark Brown }
2503c5af3a2eSMark Brown 
2504c5af3a2eSMark Brown /**
250532c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
25069115171aSMark Brown  *
250732c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
25089115171aSMark Brown  */
250932c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
25109115171aSMark Brown {
25115c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
25129115171aSMark Brown 
251315a0c645SKuninori Morimoto 	for_each_component_dais_safe(component, dai, _dai) {
251432c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
251532c9ba54SLars-Peter Clausen 			dai->name);
25169115171aSMark Brown 		list_del(&dai->list);
2517f0fba2adSLiam Girdwood 		kfree(dai->name);
2518f0fba2adSLiam Girdwood 		kfree(dai);
25199115171aSMark Brown 	}
252032c9ba54SLars-Peter Clausen }
25219115171aSMark Brown 
25225e4fb372SMengdong Lin /* Create a DAI and add it to the component's DAI list */
25235e4fb372SMengdong Lin static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
25245e4fb372SMengdong Lin 	struct snd_soc_dai_driver *dai_drv,
25255e4fb372SMengdong Lin 	bool legacy_dai_naming)
25265e4fb372SMengdong Lin {
25275e4fb372SMengdong Lin 	struct device *dev = component->dev;
25285e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
25295e4fb372SMengdong Lin 
25305e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
25315e4fb372SMengdong Lin 
25325e4fb372SMengdong Lin 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
25335e4fb372SMengdong Lin 	if (dai == NULL)
25345e4fb372SMengdong Lin 		return NULL;
25355e4fb372SMengdong Lin 
25365e4fb372SMengdong Lin 	/*
25375e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
25385e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
25395e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
25405e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
25415e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
25425e4fb372SMengdong Lin 	 * component-less anymore.
25435e4fb372SMengdong Lin 	 */
25445e4fb372SMengdong Lin 	if (legacy_dai_naming &&
25455e4fb372SMengdong Lin 	    (dai_drv->id == 0 || dai_drv->name == NULL)) {
25465e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
25475e4fb372SMengdong Lin 	} else {
25485e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
25495e4fb372SMengdong Lin 		if (dai_drv->id)
25505e4fb372SMengdong Lin 			dai->id = dai_drv->id;
25515e4fb372SMengdong Lin 		else
25525e4fb372SMengdong Lin 			dai->id = component->num_dai;
25535e4fb372SMengdong Lin 	}
25545e4fb372SMengdong Lin 	if (dai->name == NULL) {
25555e4fb372SMengdong Lin 		kfree(dai);
25565e4fb372SMengdong Lin 		return NULL;
25575e4fb372SMengdong Lin 	}
25585e4fb372SMengdong Lin 
25595e4fb372SMengdong Lin 	dai->component = component;
25605e4fb372SMengdong Lin 	dai->dev = dev;
25615e4fb372SMengdong Lin 	dai->driver = dai_drv;
25625e4fb372SMengdong Lin 	if (!dai->driver->ops)
25635e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
25645e4fb372SMengdong Lin 
256515a0c645SKuninori Morimoto 	/* see for_each_component_dais */
256658bf4179SKuninori Morimoto 	list_add_tail(&dai->list, &component->dai_list);
25675e4fb372SMengdong Lin 	component->num_dai++;
25685e4fb372SMengdong Lin 
25695e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
25705e4fb372SMengdong Lin 	return dai;
25715e4fb372SMengdong Lin }
25725e4fb372SMengdong Lin 
25739115171aSMark Brown /**
257432c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
25759115171aSMark Brown  *
25766106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
25776106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
2578ac11a2b3SMark Brown  * @count: Number of DAIs
25799115171aSMark Brown  */
258032c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
25812c7b696aSMarcel Ziswiler 				 struct snd_soc_dai_driver *dai_drv,
25822c7b696aSMarcel Ziswiler 				 size_t count)
25839115171aSMark Brown {
25846106d129SLars-Peter Clausen 	struct device *dev = component->dev;
2585f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
258632c9ba54SLars-Peter Clausen 	unsigned int i;
258732c9ba54SLars-Peter Clausen 	int ret;
2588f0fba2adSLiam Girdwood 
25895b5e0928SAlexey Dobriyan 	dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
25909115171aSMark Brown 
25919115171aSMark Brown 	for (i = 0; i < count; i++) {
2592f0fba2adSLiam Girdwood 
25932c7b696aSMarcel Ziswiler 		dai = soc_add_dai(component, dai_drv + i, count == 1 &&
25942c7b696aSMarcel Ziswiler 				  !component->driver->non_legacy_dai_naming);
2595c46e0079SAxel Lin 		if (dai == NULL) {
2596c46e0079SAxel Lin 			ret = -ENOMEM;
2597c46e0079SAxel Lin 			goto err;
2598c46e0079SAxel Lin 		}
2599f0fba2adSLiam Girdwood 	}
2600f0fba2adSLiam Girdwood 
26019115171aSMark Brown 	return 0;
26029115171aSMark Brown 
26039115171aSMark Brown err:
260432c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
26059115171aSMark Brown 
26069115171aSMark Brown 	return ret;
26079115171aSMark Brown }
26089115171aSMark Brown 
260968003e6cSMengdong Lin /**
261068003e6cSMengdong Lin  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
261168003e6cSMengdong Lin  *
261268003e6cSMengdong Lin  * @component: The component the DAIs are registered for
261368003e6cSMengdong Lin  * @dai_drv: DAI driver to use for the DAI
261468003e6cSMengdong Lin  *
261568003e6cSMengdong Lin  * Topology can use this API to register DAIs when probing a component.
261668003e6cSMengdong Lin  * These DAIs's widgets will be freed in the card cleanup and the DAIs
261768003e6cSMengdong Lin  * will be freed in the component cleanup.
261868003e6cSMengdong Lin  */
261968003e6cSMengdong Lin int snd_soc_register_dai(struct snd_soc_component *component,
262068003e6cSMengdong Lin 	struct snd_soc_dai_driver *dai_drv)
262168003e6cSMengdong Lin {
262268003e6cSMengdong Lin 	struct snd_soc_dapm_context *dapm =
262368003e6cSMengdong Lin 		snd_soc_component_get_dapm(component);
262468003e6cSMengdong Lin 	struct snd_soc_dai *dai;
262568003e6cSMengdong Lin 	int ret;
262668003e6cSMengdong Lin 
262768003e6cSMengdong Lin 	if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
262868003e6cSMengdong Lin 		dev_err(component->dev, "Invalid dai type %d\n",
262968003e6cSMengdong Lin 			dai_drv->dobj.type);
263068003e6cSMengdong Lin 		return -EINVAL;
263168003e6cSMengdong Lin 	}
263268003e6cSMengdong Lin 
263368003e6cSMengdong Lin 	lockdep_assert_held(&client_mutex);
263468003e6cSMengdong Lin 	dai = soc_add_dai(component, dai_drv, false);
263568003e6cSMengdong Lin 	if (!dai)
263668003e6cSMengdong Lin 		return -ENOMEM;
263768003e6cSMengdong Lin 
26382c7b696aSMarcel Ziswiler 	/*
26392c7b696aSMarcel Ziswiler 	 * Create the DAI widgets here. After adding DAIs, topology may
264068003e6cSMengdong Lin 	 * also add routes that need these widgets as source or sink.
264168003e6cSMengdong Lin 	 */
264268003e6cSMengdong Lin 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
264368003e6cSMengdong Lin 	if (ret != 0) {
264468003e6cSMengdong Lin 		dev_err(component->dev,
264568003e6cSMengdong Lin 			"Failed to create DAI widgets %d\n", ret);
264668003e6cSMengdong Lin 	}
264768003e6cSMengdong Lin 
264868003e6cSMengdong Lin 	return ret;
264968003e6cSMengdong Lin }
265068003e6cSMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_register_dai);
265168003e6cSMengdong Lin 
2652bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
2653bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
2654d191bd8dSKuninori Morimoto {
2655ce0fc93aSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
2656ce0fc93aSLars-Peter Clausen 
2657bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
2658bb13109dSLars-Peter Clausen 	if (!component->name) {
2659bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
2660d191bd8dSKuninori Morimoto 		return -ENOMEM;
2661d191bd8dSKuninori Morimoto 	}
2662d191bd8dSKuninori Morimoto 
2663bb13109dSLars-Peter Clausen 	component->dev = dev;
2664bb13109dSLars-Peter Clausen 	component->driver = driver;
2665e2c330b9SLars-Peter Clausen 
266688c27465SKuninori Morimoto 	dapm = snd_soc_component_get_dapm(component);
2667ce0fc93aSLars-Peter Clausen 	dapm->dev = dev;
2668ce0fc93aSLars-Peter Clausen 	dapm->component = component;
2669ce0fc93aSLars-Peter Clausen 	dapm->bias_level = SND_SOC_BIAS_OFF;
26707ba236ceSKuninori Morimoto 	dapm->idle_bias_off = !driver->idle_bias_on;
26717ba236ceSKuninori Morimoto 	dapm->suspend_bias_off = driver->suspend_bias_off;
2672ce0fc93aSLars-Peter Clausen 
2673bb13109dSLars-Peter Clausen 	INIT_LIST_HEAD(&component->dai_list);
2674bb13109dSLars-Peter Clausen 	mutex_init(&component->io_mutex);
2675bb13109dSLars-Peter Clausen 
2676bb13109dSLars-Peter Clausen 	return 0;
2677d191bd8dSKuninori Morimoto }
2678d191bd8dSKuninori Morimoto 
267920feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2680886f5692SLars-Peter Clausen {
2681886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
268220feb881SLars-Peter Clausen 
2683886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
2684886f5692SLars-Peter Clausen 	if (val_bytes > 0)
2685886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
2686886f5692SLars-Peter Clausen }
268720feb881SLars-Peter Clausen 
2688e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
2689e874bf5fSLars-Peter Clausen 
269020feb881SLars-Peter Clausen /**
26912c7b696aSMarcel Ziswiler  * snd_soc_component_init_regmap() - Initialize regmap instance for the
26922c7b696aSMarcel Ziswiler  *                                   component
269320feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
269420feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
269520feb881SLars-Peter Clausen  *
269620feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
269720feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
269820feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
269920feb881SLars-Peter Clausen  * before the first IO attempt of the component.
270020feb881SLars-Peter Clausen  */
270120feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
270220feb881SLars-Peter Clausen 	struct regmap *regmap)
270320feb881SLars-Peter Clausen {
270420feb881SLars-Peter Clausen 	component->regmap = regmap;
270520feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
2706886f5692SLars-Peter Clausen }
270720feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
270820feb881SLars-Peter Clausen 
270920feb881SLars-Peter Clausen /**
27102c7b696aSMarcel Ziswiler  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
27112c7b696aSMarcel Ziswiler  *                                   component
271220feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
271320feb881SLars-Peter Clausen  *
271420feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
271520feb881SLars-Peter Clausen  * removes the regmap instance from the component.
271620feb881SLars-Peter Clausen  *
271720feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
271820feb881SLars-Peter Clausen  * to initialize the regmap instance.
271920feb881SLars-Peter Clausen  */
272020feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
272120feb881SLars-Peter Clausen {
272220feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
272320feb881SLars-Peter Clausen 	component->regmap = NULL;
272420feb881SLars-Peter Clausen }
272520feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2726886f5692SLars-Peter Clausen 
2727e874bf5fSLars-Peter Clausen #endif
2728d191bd8dSKuninori Morimoto 
2729359c71eeSKuninori Morimoto static void snd_soc_component_add(struct snd_soc_component *component)
2730bb13109dSLars-Peter Clausen {
2731359c71eeSKuninori Morimoto 	mutex_lock(&client_mutex);
2732359c71eeSKuninori Morimoto 
2733999f7f5aSKuninori Morimoto 	if (!component->driver->write && !component->driver->read) {
273420feb881SLars-Peter Clausen 		if (!component->regmap)
27352c7b696aSMarcel Ziswiler 			component->regmap = dev_get_regmap(component->dev,
27362c7b696aSMarcel Ziswiler 							   NULL);
273720feb881SLars-Peter Clausen 		if (component->regmap)
273820feb881SLars-Peter Clausen 			snd_soc_component_setup_regmap(component);
273920feb881SLars-Peter Clausen 	}
2740886f5692SLars-Peter Clausen 
2741368dee94SKuninori Morimoto 	/* see for_each_component */
2742bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
27438a978234SLiam Girdwood 	INIT_LIST_HEAD(&component->dobj_list);
2744d191bd8dSKuninori Morimoto 
2745d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
2746bb13109dSLars-Peter Clausen }
2747d191bd8dSKuninori Morimoto 
2748bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
2749bb13109dSLars-Peter Clausen {
2750bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
2751bb13109dSLars-Peter Clausen 	kfree(component->name);
2752bb13109dSLars-Peter Clausen }
2753d191bd8dSKuninori Morimoto 
2754bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2755bb13109dSLars-Peter Clausen {
2756c12c1aadSKuninori Morimoto 	struct snd_soc_card *card = component->card;
2757c12c1aadSKuninori Morimoto 
2758c12c1aadSKuninori Morimoto 	if (card)
2759e894efefSSrinivas Kandagatla 		snd_soc_unbind_card(card, false);
2760c12c1aadSKuninori Morimoto 
2761bb13109dSLars-Peter Clausen 	list_del(&component->list);
2762bb13109dSLars-Peter Clausen }
2763d191bd8dSKuninori Morimoto 
2764273d778eSKuninori Morimoto #define ENDIANNESS_MAP(name) \
2765273d778eSKuninori Morimoto 	(SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2766273d778eSKuninori Morimoto static u64 endianness_format_map[] = {
2767273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S16_),
2768273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U16_),
2769273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_),
2770273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_),
2771273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S32_),
2772273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U32_),
2773273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_3),
2774273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_3),
2775273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S20_3),
2776273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U20_3),
2777273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S18_3),
2778273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U18_3),
2779273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT_),
2780273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT64_),
2781273d778eSKuninori Morimoto 	ENDIANNESS_MAP(IEC958_SUBFRAME_),
2782273d778eSKuninori Morimoto };
2783273d778eSKuninori Morimoto 
2784273d778eSKuninori Morimoto /*
2785273d778eSKuninori Morimoto  * Fix up the DAI formats for endianness: codecs don't actually see
2786273d778eSKuninori Morimoto  * the endianness of the data but we're using the CPU format
2787273d778eSKuninori Morimoto  * definitions which do need to include endianness so we ensure that
2788273d778eSKuninori Morimoto  * codec DAIs always have both big and little endian variants set.
2789273d778eSKuninori Morimoto  */
2790273d778eSKuninori Morimoto static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2791273d778eSKuninori Morimoto {
2792273d778eSKuninori Morimoto 	int i;
2793273d778eSKuninori Morimoto 
2794273d778eSKuninori Morimoto 	for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2795273d778eSKuninori Morimoto 		if (stream->formats & endianness_format_map[i])
2796273d778eSKuninori Morimoto 			stream->formats |= endianness_format_map[i];
2797273d778eSKuninori Morimoto }
2798273d778eSKuninori Morimoto 
2799e894efefSSrinivas Kandagatla static void snd_soc_try_rebind_card(void)
2800e894efefSSrinivas Kandagatla {
2801e894efefSSrinivas Kandagatla 	struct snd_soc_card *card, *c;
2802e894efefSSrinivas Kandagatla 
2803e894efefSSrinivas Kandagatla 	if (!list_empty(&unbind_card_list)) {
2804e894efefSSrinivas Kandagatla 		list_for_each_entry_safe(card, c, &unbind_card_list, list) {
2805e894efefSSrinivas Kandagatla 			if (!snd_soc_bind_card(card))
2806e894efefSSrinivas Kandagatla 				list_del(&card->list);
2807e894efefSSrinivas Kandagatla 		}
2808e894efefSSrinivas Kandagatla 	}
2809e894efefSSrinivas Kandagatla }
2810e894efefSSrinivas Kandagatla 
2811e0dac41bSKuninori Morimoto int snd_soc_add_component(struct device *dev,
2812e0dac41bSKuninori Morimoto 			struct snd_soc_component *component,
2813cf9e829eSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2814d191bd8dSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2815d191bd8dSKuninori Morimoto 			int num_dai)
2816d191bd8dSKuninori Morimoto {
2817bb13109dSLars-Peter Clausen 	int ret;
2818273d778eSKuninori Morimoto 	int i;
2819d191bd8dSKuninori Morimoto 
2820cf9e829eSKuninori Morimoto 	ret = snd_soc_component_initialize(component, component_driver, dev);
2821bb13109dSLars-Peter Clausen 	if (ret)
2822bb13109dSLars-Peter Clausen 		goto err_free;
2823bb13109dSLars-Peter Clausen 
2824273d778eSKuninori Morimoto 	if (component_driver->endianness) {
2825273d778eSKuninori Morimoto 		for (i = 0; i < num_dai; i++) {
2826273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].playback);
2827273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].capture);
2828273d778eSKuninori Morimoto 		}
2829273d778eSKuninori Morimoto 	}
2830273d778eSKuninori Morimoto 
28310e7b25c6SKuninori Morimoto 	ret = snd_soc_register_dais(component, dai_drv, num_dai);
2832bb13109dSLars-Peter Clausen 	if (ret < 0) {
2833f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
2834bb13109dSLars-Peter Clausen 		goto err_cleanup;
2835bb13109dSLars-Peter Clausen 	}
2836bb13109dSLars-Peter Clausen 
2837cf9e829eSKuninori Morimoto 	snd_soc_component_add(component);
2838e894efefSSrinivas Kandagatla 	snd_soc_try_rebind_card();
2839bb13109dSLars-Peter Clausen 
2840bb13109dSLars-Peter Clausen 	return 0;
2841bb13109dSLars-Peter Clausen 
2842bb13109dSLars-Peter Clausen err_cleanup:
2843cf9e829eSKuninori Morimoto 	snd_soc_component_cleanup(component);
2844bb13109dSLars-Peter Clausen err_free:
2845bb13109dSLars-Peter Clausen 	return ret;
2846d191bd8dSKuninori Morimoto }
2847e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component);
2848e0dac41bSKuninori Morimoto 
2849e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev,
2850e0dac41bSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2851e0dac41bSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2852e0dac41bSKuninori Morimoto 			int num_dai)
2853e0dac41bSKuninori Morimoto {
2854e0dac41bSKuninori Morimoto 	struct snd_soc_component *component;
2855e0dac41bSKuninori Morimoto 
28567ecbd6a9SKuninori Morimoto 	component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
285708e61d03SKuninori Morimoto 	if (!component)
2858e0dac41bSKuninori Morimoto 		return -ENOMEM;
2859e0dac41bSKuninori Morimoto 
2860e0dac41bSKuninori Morimoto 	return snd_soc_add_component(dev, component, component_driver,
2861e0dac41bSKuninori Morimoto 				     dai_drv, num_dai);
2862e0dac41bSKuninori Morimoto }
2863d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
2864d191bd8dSKuninori Morimoto 
2865d191bd8dSKuninori Morimoto /**
28662eccea8cSKuninori Morimoto  * snd_soc_unregister_component - Unregister all related component
28672eccea8cSKuninori Morimoto  * from the ASoC core
2868d191bd8dSKuninori Morimoto  *
2869628536eaSJonathan Corbet  * @dev: The device to unregister
2870d191bd8dSKuninori Morimoto  */
28712eccea8cSKuninori Morimoto static int __snd_soc_unregister_component(struct device *dev)
2872d191bd8dSKuninori Morimoto {
2873cf9e829eSKuninori Morimoto 	struct snd_soc_component *component;
287421a03528SKuninori Morimoto 	int found = 0;
2875d191bd8dSKuninori Morimoto 
287634e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
2877368dee94SKuninori Morimoto 	for_each_component(component) {
2878999f7f5aSKuninori Morimoto 		if (dev != component->dev)
287921a03528SKuninori Morimoto 			continue;
2880d191bd8dSKuninori Morimoto 
28812c7b696aSMarcel Ziswiler 		snd_soc_tplg_component_remove(component,
28822c7b696aSMarcel Ziswiler 					      SND_SOC_TPLG_INDEX_ALL);
2883cf9e829eSKuninori Morimoto 		snd_soc_component_del_unlocked(component);
288421a03528SKuninori Morimoto 		found = 1;
288521a03528SKuninori Morimoto 		break;
2886d191bd8dSKuninori Morimoto 	}
2887d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
2888d191bd8dSKuninori Morimoto 
28892c7b696aSMarcel Ziswiler 	if (found)
2890cf9e829eSKuninori Morimoto 		snd_soc_component_cleanup(component);
28912eccea8cSKuninori Morimoto 
28922eccea8cSKuninori Morimoto 	return found;
28932eccea8cSKuninori Morimoto }
28942eccea8cSKuninori Morimoto 
28952eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
28962eccea8cSKuninori Morimoto {
28972c7b696aSMarcel Ziswiler 	while (__snd_soc_unregister_component(dev))
28982c7b696aSMarcel Ziswiler 		;
2899d191bd8dSKuninori Morimoto }
2900d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2901d191bd8dSKuninori Morimoto 
29027dd5d0d9SKuninori Morimoto struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
29037dd5d0d9SKuninori Morimoto 						   const char *driver_name)
29047dd5d0d9SKuninori Morimoto {
29057dd5d0d9SKuninori Morimoto 	struct snd_soc_component *component;
29067dd5d0d9SKuninori Morimoto 	struct snd_soc_component *ret;
29077dd5d0d9SKuninori Morimoto 
29087dd5d0d9SKuninori Morimoto 	ret = NULL;
29097dd5d0d9SKuninori Morimoto 	mutex_lock(&client_mutex);
2910368dee94SKuninori Morimoto 	for_each_component(component) {
29117dd5d0d9SKuninori Morimoto 		if (dev != component->dev)
29127dd5d0d9SKuninori Morimoto 			continue;
29137dd5d0d9SKuninori Morimoto 
29147dd5d0d9SKuninori Morimoto 		if (driver_name &&
29157dd5d0d9SKuninori Morimoto 		    (driver_name != component->driver->name) &&
29167dd5d0d9SKuninori Morimoto 		    (strcmp(component->driver->name, driver_name) != 0))
29177dd5d0d9SKuninori Morimoto 			continue;
29187dd5d0d9SKuninori Morimoto 
29197dd5d0d9SKuninori Morimoto 		ret = component;
29207dd5d0d9SKuninori Morimoto 		break;
29217dd5d0d9SKuninori Morimoto 	}
29227dd5d0d9SKuninori Morimoto 	mutex_unlock(&client_mutex);
29237dd5d0d9SKuninori Morimoto 
29247dd5d0d9SKuninori Morimoto 	return ret;
29257dd5d0d9SKuninori Morimoto }
29267dd5d0d9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
29277dd5d0d9SKuninori Morimoto 
2928bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
2929b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2930bec4fa05SStephen Warren 			       const char *propname)
2931bec4fa05SStephen Warren {
2932b07609ceSKuninori Morimoto 	struct device_node *np;
2933bec4fa05SStephen Warren 	int ret;
2934bec4fa05SStephen Warren 
29357e07e7c0STushar Behera 	if (!card->dev) {
29367e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
29377e07e7c0STushar Behera 		return -EINVAL;
29387e07e7c0STushar Behera 	}
29397e07e7c0STushar Behera 
29407e07e7c0STushar Behera 	np = card->dev->of_node;
29417e07e7c0STushar Behera 
2942bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
2943bec4fa05SStephen Warren 	/*
2944bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
2945bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
2946bec4fa05SStephen Warren 	 * snd_soc_register_card.
2947bec4fa05SStephen Warren 	 */
2948bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
2949bec4fa05SStephen Warren 		dev_err(card->dev,
2950f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
2951bec4fa05SStephen Warren 			propname, ret);
2952bec4fa05SStephen Warren 		return ret;
2953bec4fa05SStephen Warren 	}
2954bec4fa05SStephen Warren 
2955bec4fa05SStephen Warren 	return 0;
2956bec4fa05SStephen Warren }
2957b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2958bec4fa05SStephen Warren 
29599a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
29609a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
29619a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
29629a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
29639a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
29649a6d4860SXiubo Li };
29659a6d4860SXiubo Li 
296621efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
29679a6d4860SXiubo Li 					  const char *propname)
29689a6d4860SXiubo Li {
296921efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
29709a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
29719a6d4860SXiubo Li 	const char *template, *wname;
29729a6d4860SXiubo Li 	int i, j, num_widgets, ret;
29739a6d4860SXiubo Li 
29749a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
29759a6d4860SXiubo Li 	if (num_widgets < 0) {
29769a6d4860SXiubo Li 		dev_err(card->dev,
29779a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
29789a6d4860SXiubo Li 		return -EINVAL;
29799a6d4860SXiubo Li 	}
29809a6d4860SXiubo Li 	if (num_widgets & 1) {
29819a6d4860SXiubo Li 		dev_err(card->dev,
29829a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
29839a6d4860SXiubo Li 		return -EINVAL;
29849a6d4860SXiubo Li 	}
29859a6d4860SXiubo Li 
29869a6d4860SXiubo Li 	num_widgets /= 2;
29879a6d4860SXiubo Li 	if (!num_widgets) {
29889a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
29899a6d4860SXiubo Li 			propname);
29909a6d4860SXiubo Li 		return -EINVAL;
29919a6d4860SXiubo Li 	}
29929a6d4860SXiubo Li 
29939a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
29949a6d4860SXiubo Li 			       GFP_KERNEL);
29959a6d4860SXiubo Li 	if (!widgets) {
29969a6d4860SXiubo Li 		dev_err(card->dev,
29979a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
29989a6d4860SXiubo Li 		return -ENOMEM;
29999a6d4860SXiubo Li 	}
30009a6d4860SXiubo Li 
30019a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
30029a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
30039a6d4860SXiubo Li 			2 * i, &template);
30049a6d4860SXiubo Li 		if (ret) {
30059a6d4860SXiubo Li 			dev_err(card->dev,
30069a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
30079a6d4860SXiubo Li 				propname, 2 * i, ret);
30089a6d4860SXiubo Li 			return -EINVAL;
30099a6d4860SXiubo Li 		}
30109a6d4860SXiubo Li 
30119a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
30129a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
30139a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
30149a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
30159a6d4860SXiubo Li 				break;
30169a6d4860SXiubo Li 			}
30179a6d4860SXiubo Li 		}
30189a6d4860SXiubo Li 
30199a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
30209a6d4860SXiubo Li 			dev_err(card->dev,
30219a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
30229a6d4860SXiubo Li 				template);
30239a6d4860SXiubo Li 			return -EINVAL;
30249a6d4860SXiubo Li 		}
30259a6d4860SXiubo Li 
30269a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
30279a6d4860SXiubo Li 						    (2 * i) + 1,
30289a6d4860SXiubo Li 						    &wname);
30299a6d4860SXiubo Li 		if (ret) {
30309a6d4860SXiubo Li 			dev_err(card->dev,
30319a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
30329a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
30339a6d4860SXiubo Li 			return -EINVAL;
30349a6d4860SXiubo Li 		}
30359a6d4860SXiubo Li 
30369a6d4860SXiubo Li 		widgets[i].name = wname;
30379a6d4860SXiubo Li 	}
30389a6d4860SXiubo Li 
3039f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
3040f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
30419a6d4860SXiubo Li 
30429a6d4860SXiubo Li 	return 0;
30439a6d4860SXiubo Li }
304421efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
30459a6d4860SXiubo Li 
3046cbdfab3bSJerome Brunet int snd_soc_of_get_slot_mask(struct device_node *np,
30476131084aSJyri Sarha 			     const char *prop_name,
30486131084aSJyri Sarha 			     unsigned int *mask)
30496131084aSJyri Sarha {
30506131084aSJyri Sarha 	u32 val;
30516c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
30526131084aSJyri Sarha 	int i;
30536131084aSJyri Sarha 
30546131084aSJyri Sarha 	if (!of_slot_mask)
30556131084aSJyri Sarha 		return 0;
30566131084aSJyri Sarha 	val /= sizeof(u32);
30576131084aSJyri Sarha 	for (i = 0; i < val; i++)
30586131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
30596131084aSJyri Sarha 			*mask |= (1 << i);
30606131084aSJyri Sarha 
30616131084aSJyri Sarha 	return val;
30626131084aSJyri Sarha }
3063cbdfab3bSJerome Brunet EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
30646131084aSJyri Sarha 
306589c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
30666131084aSJyri Sarha 			      unsigned int *tx_mask,
30676131084aSJyri Sarha 			      unsigned int *rx_mask,
306889c67857SXiubo Li 			      unsigned int *slots,
306989c67857SXiubo Li 			      unsigned int *slot_width)
307089c67857SXiubo Li {
307189c67857SXiubo Li 	u32 val;
307289c67857SXiubo Li 	int ret;
307389c67857SXiubo Li 
30746131084aSJyri Sarha 	if (tx_mask)
30756131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
30766131084aSJyri Sarha 	if (rx_mask)
30776131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
30786131084aSJyri Sarha 
307989c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
308089c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
308189c67857SXiubo Li 		if (ret)
308289c67857SXiubo Li 			return ret;
308389c67857SXiubo Li 
308489c67857SXiubo Li 		if (slots)
308589c67857SXiubo Li 			*slots = val;
308689c67857SXiubo Li 	}
308789c67857SXiubo Li 
308889c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
308989c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
309089c67857SXiubo Li 		if (ret)
309189c67857SXiubo Li 			return ret;
309289c67857SXiubo Li 
309389c67857SXiubo Li 		if (slot_width)
309489c67857SXiubo Li 			*slot_width = val;
309589c67857SXiubo Li 	}
309689c67857SXiubo Li 
309789c67857SXiubo Li 	return 0;
309889c67857SXiubo Li }
309989c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
310089c67857SXiubo Li 
31013b710356SKuninori Morimoto void snd_soc_of_parse_node_prefix(struct device_node *np,
31025e3cdaa2SKuninori Morimoto 				  struct snd_soc_codec_conf *codec_conf,
31035e3cdaa2SKuninori Morimoto 				  struct device_node *of_node,
31045e3cdaa2SKuninori Morimoto 				  const char *propname)
31055e3cdaa2SKuninori Morimoto {
31065e3cdaa2SKuninori Morimoto 	const char *str;
31075e3cdaa2SKuninori Morimoto 	int ret;
31085e3cdaa2SKuninori Morimoto 
31095e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
31105e3cdaa2SKuninori Morimoto 	if (ret < 0) {
31115e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
31125e3cdaa2SKuninori Morimoto 		return;
31135e3cdaa2SKuninori Morimoto 	}
31145e3cdaa2SKuninori Morimoto 
31155e3cdaa2SKuninori Morimoto 	codec_conf->of_node	= of_node;
31165e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
31175e3cdaa2SKuninori Morimoto }
31183b710356SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
31195e3cdaa2SKuninori Morimoto 
31202bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3121a4a54dd5SStephen Warren 				   const char *propname)
3122a4a54dd5SStephen Warren {
31232bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
3124e3b1e6a1SMark Brown 	int num_routes;
3125a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3126a4a54dd5SStephen Warren 	int i, ret;
3127a4a54dd5SStephen Warren 
3128a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3129c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
313010e8aa9aSMichał Mirosław 		dev_err(card->dev,
313110e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
313210e8aa9aSMichał Mirosław 			propname);
3133a4a54dd5SStephen Warren 		return -EINVAL;
3134a4a54dd5SStephen Warren 	}
3135a4a54dd5SStephen Warren 	num_routes /= 2;
3136a4a54dd5SStephen Warren 	if (!num_routes) {
3137f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3138a4a54dd5SStephen Warren 			propname);
3139a4a54dd5SStephen Warren 		return -EINVAL;
3140a4a54dd5SStephen Warren 	}
3141a4a54dd5SStephen Warren 
3142a86854d0SKees Cook 	routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
3143a4a54dd5SStephen Warren 			      GFP_KERNEL);
3144a4a54dd5SStephen Warren 	if (!routes) {
3145a4a54dd5SStephen Warren 		dev_err(card->dev,
3146f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3147a4a54dd5SStephen Warren 		return -EINVAL;
3148a4a54dd5SStephen Warren 	}
3149a4a54dd5SStephen Warren 
3150a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3151a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3152e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
3153a4a54dd5SStephen Warren 		if (ret) {
3154c871bd0bSMark Brown 			dev_err(card->dev,
3155c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3156c871bd0bSMark Brown 				propname, 2 * i, ret);
3157a4a54dd5SStephen Warren 			return -EINVAL;
3158a4a54dd5SStephen Warren 		}
3159a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3160e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3161a4a54dd5SStephen Warren 		if (ret) {
3162a4a54dd5SStephen Warren 			dev_err(card->dev,
3163c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3164c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3165a4a54dd5SStephen Warren 			return -EINVAL;
3166a4a54dd5SStephen Warren 		}
3167a4a54dd5SStephen Warren 	}
3168a4a54dd5SStephen Warren 
3169f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
3170f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
3171a4a54dd5SStephen Warren 
3172a4a54dd5SStephen Warren 	return 0;
3173a4a54dd5SStephen Warren }
31742bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3175a4a54dd5SStephen Warren 
3176a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3177389cb834SJyri Sarha 				     const char *prefix,
3178389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3179389cb834SJyri Sarha 				     struct device_node **framemaster)
3180a7930ed4SKuninori Morimoto {
3181a7930ed4SKuninori Morimoto 	int ret, i;
3182a7930ed4SKuninori Morimoto 	char prop[128];
3183a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3184a7930ed4SKuninori Morimoto 	int bit, frame;
3185a7930ed4SKuninori Morimoto 	const char *str;
3186a7930ed4SKuninori Morimoto 	struct {
3187a7930ed4SKuninori Morimoto 		char *name;
3188a7930ed4SKuninori Morimoto 		unsigned int val;
3189a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3190a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3191a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3192a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3193a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3194a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3195a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3196a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3197a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3198a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3199a7930ed4SKuninori Morimoto 	};
3200a7930ed4SKuninori Morimoto 
3201a7930ed4SKuninori Morimoto 	if (!prefix)
3202a7930ed4SKuninori Morimoto 		prefix = "";
3203a7930ed4SKuninori Morimoto 
3204a7930ed4SKuninori Morimoto 	/*
32055711c979SKuninori Morimoto 	 * check "dai-format = xxx"
32065711c979SKuninori Morimoto 	 * or    "[prefix]format = xxx"
3207a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3208a7930ed4SKuninori Morimoto 	 */
32095711c979SKuninori Morimoto 	ret = of_property_read_string(np, "dai-format", &str);
32105711c979SKuninori Morimoto 	if (ret < 0) {
3211a7930ed4SKuninori Morimoto 		snprintf(prop, sizeof(prop), "%sformat", prefix);
3212a7930ed4SKuninori Morimoto 		ret = of_property_read_string(np, prop, &str);
32135711c979SKuninori Morimoto 	}
3214a7930ed4SKuninori Morimoto 	if (ret == 0) {
3215a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3216a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3217a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3218a7930ed4SKuninori Morimoto 				break;
3219a7930ed4SKuninori Morimoto 			}
3220a7930ed4SKuninori Morimoto 		}
3221a7930ed4SKuninori Morimoto 	}
3222a7930ed4SKuninori Morimoto 
3223a7930ed4SKuninori Morimoto 	/*
32248c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3225a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3226a7930ed4SKuninori Morimoto 	 */
32278c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
322851930295SJulia Lawall 	if (of_property_read_bool(np, prop))
32298c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
32308c2d6a9fSKuninori Morimoto 	else
32318c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3232a7930ed4SKuninori Morimoto 
3233a7930ed4SKuninori Morimoto 	/*
3234a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3235a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3236a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3237a7930ed4SKuninori Morimoto 	 */
3238a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3239a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3240a7930ed4SKuninori Morimoto 
3241a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3242a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3243a7930ed4SKuninori Morimoto 
3244a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3245a7930ed4SKuninori Morimoto 	case 0x11:
3246a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3247a7930ed4SKuninori Morimoto 		break;
3248a7930ed4SKuninori Morimoto 	case 0x10:
3249a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3250a7930ed4SKuninori Morimoto 		break;
3251a7930ed4SKuninori Morimoto 	case 0x01:
3252a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3253a7930ed4SKuninori Morimoto 		break;
3254a7930ed4SKuninori Morimoto 	default:
3255a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3256a7930ed4SKuninori Morimoto 		break;
3257a7930ed4SKuninori Morimoto 	}
3258a7930ed4SKuninori Morimoto 
3259a7930ed4SKuninori Morimoto 	/*
3260a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3261a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3262a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3263a7930ed4SKuninori Morimoto 	 */
3264a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3265a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3266389cb834SJyri Sarha 	if (bit && bitclkmaster)
3267389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3268a7930ed4SKuninori Morimoto 
3269a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3270a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3271389cb834SJyri Sarha 	if (frame && framemaster)
3272389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3273a7930ed4SKuninori Morimoto 
3274a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3275a7930ed4SKuninori Morimoto 	case 0x11:
3276a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3277a7930ed4SKuninori Morimoto 		break;
3278a7930ed4SKuninori Morimoto 	case 0x10:
3279a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3280a7930ed4SKuninori Morimoto 		break;
3281a7930ed4SKuninori Morimoto 	case 0x01:
3282a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3283a7930ed4SKuninori Morimoto 		break;
3284a7930ed4SKuninori Morimoto 	default:
3285a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3286a7930ed4SKuninori Morimoto 		break;
3287a7930ed4SKuninori Morimoto 	}
3288a7930ed4SKuninori Morimoto 
3289a7930ed4SKuninori Morimoto 	return format;
3290a7930ed4SKuninori Morimoto }
3291a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3292a7930ed4SKuninori Morimoto 
3293a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
3294a180e8b9SKuninori Morimoto {
329509d4cc03SKuninori Morimoto 	struct snd_soc_component *component;
3296c1e230f0SKuninori Morimoto 	struct snd_soc_dai_link_component dlc;
3297a180e8b9SKuninori Morimoto 	int ret;
3298a180e8b9SKuninori Morimoto 
3299c1e230f0SKuninori Morimoto 	dlc.of_node	= of_graph_get_port_parent(ep);
3300c1e230f0SKuninori Morimoto 	dlc.name	= NULL;
3301a180e8b9SKuninori Morimoto 	/*
3302a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
3303a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
3304a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
3305a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
3306a180e8b9SKuninori Morimoto 	 */
3307a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
3308a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
3309c1e230f0SKuninori Morimoto 	component = soc_find_component(&dlc);
33102c7b1704SKuninori Morimoto 	if (component)
33112c7b1704SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_id(component, ep);
3312a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
3313a180e8b9SKuninori Morimoto 
3314c1e230f0SKuninori Morimoto 	of_node_put(dlc.of_node);
3315c0a480d1STony Lindgren 
3316a180e8b9SKuninori Morimoto 	return ret;
3317a180e8b9SKuninori Morimoto }
3318a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3319a180e8b9SKuninori Morimoto 
33201ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args,
3321cb470087SKuninori Morimoto 				const char **dai_name)
3322cb470087SKuninori Morimoto {
3323cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
33243e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
332593b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3326cb470087SKuninori Morimoto 
3327cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3328368dee94SKuninori Morimoto 	for_each_component(pos) {
3329c0834440SKuninori Morimoto 		component_of_node = soc_component_to_node(pos);
33303e0aa8d8SJyri Sarha 
33313e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
3332cb470087SKuninori Morimoto 			continue;
3333cb470087SKuninori Morimoto 
3334a2a34175SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3335a2a34175SKuninori Morimoto 		if (ret == -ENOTSUPP) {
333658bf4179SKuninori Morimoto 			struct snd_soc_dai *dai;
33376833c452SKuninori Morimoto 			int id = -1;
33386833c452SKuninori Morimoto 
333993b0f3eeSJean-Francois Moine 			switch (args->args_count) {
33406833c452SKuninori Morimoto 			case 0:
33416833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
33426833c452SKuninori Morimoto 				break;
33436833c452SKuninori Morimoto 			case 1:
334493b0f3eeSJean-Francois Moine 				id = args->args[0];
33456833c452SKuninori Morimoto 				break;
33466833c452SKuninori Morimoto 			default:
33476833c452SKuninori Morimoto 				/* not supported */
3348cb470087SKuninori Morimoto 				break;
3349cb470087SKuninori Morimoto 			}
3350cb470087SKuninori Morimoto 
33516833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
33526833c452SKuninori Morimoto 				ret = -EINVAL;
33533dcba280SNicolin Chen 				continue;
33546833c452SKuninori Morimoto 			}
3355e41975edSXiubo Li 
3356e41975edSXiubo Li 			ret = 0;
3357e41975edSXiubo Li 
335858bf4179SKuninori Morimoto 			/* find target DAI */
335915a0c645SKuninori Morimoto 			for_each_component_dais(pos, dai) {
336058bf4179SKuninori Morimoto 				if (id == 0)
336158bf4179SKuninori Morimoto 					break;
336258bf4179SKuninori Morimoto 				id--;
336358bf4179SKuninori Morimoto 			}
336458bf4179SKuninori Morimoto 
336558bf4179SKuninori Morimoto 			*dai_name = dai->driver->name;
3366e41975edSXiubo Li 			if (!*dai_name)
3367e41975edSXiubo Li 				*dai_name = pos->name;
33686833c452SKuninori Morimoto 		}
33696833c452SKuninori Morimoto 
3370cb470087SKuninori Morimoto 		break;
3371cb470087SKuninori Morimoto 	}
3372cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
337393b0f3eeSJean-Francois Moine 	return ret;
337493b0f3eeSJean-Francois Moine }
33751ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
337693b0f3eeSJean-Francois Moine 
337793b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
337893b0f3eeSJean-Francois Moine 			    const char **dai_name)
337993b0f3eeSJean-Francois Moine {
338093b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
338193b0f3eeSJean-Francois Moine 	int ret;
338293b0f3eeSJean-Francois Moine 
338393b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
338493b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
338593b0f3eeSJean-Francois Moine 	if (ret)
338693b0f3eeSJean-Francois Moine 		return ret;
338793b0f3eeSJean-Francois Moine 
338893b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3389cb470087SKuninori Morimoto 
3390cb470087SKuninori Morimoto 	of_node_put(args.np);
3391cb470087SKuninori Morimoto 
3392cb470087SKuninori Morimoto 	return ret;
3393cb470087SKuninori Morimoto }
3394cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3395cb470087SKuninori Morimoto 
339693b0f3eeSJean-Francois Moine /*
339794685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
339894685763SSylwester Nawrocki  * @dai_link: DAI link
339994685763SSylwester Nawrocki  *
340094685763SSylwester Nawrocki  * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
340194685763SSylwester Nawrocki  */
340294685763SSylwester Nawrocki void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
340394685763SSylwester Nawrocki {
34043db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *component;
340594685763SSylwester Nawrocki 	int index;
340694685763SSylwester Nawrocki 
34073db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
340894685763SSylwester Nawrocki 		if (!component->of_node)
340994685763SSylwester Nawrocki 			break;
341094685763SSylwester Nawrocki 		of_node_put(component->of_node);
341194685763SSylwester Nawrocki 		component->of_node = NULL;
341294685763SSylwester Nawrocki 	}
341394685763SSylwester Nawrocki }
341494685763SSylwester Nawrocki EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
341594685763SSylwester Nawrocki 
341694685763SSylwester Nawrocki /*
341793b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
341893b0f3eeSJean-Francois Moine  * @dev: Card device
341993b0f3eeSJean-Francois Moine  * @of_node: Device node
342093b0f3eeSJean-Francois Moine  * @dai_link: DAI link
342193b0f3eeSJean-Francois Moine  *
342293b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
342393b0f3eeSJean-Francois Moine  * 'sound-dai'.
342493b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
342594685763SSylwester Nawrocki  * The device nodes in the array (of_node) must be dereferenced by calling
342694685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs() on @dai_link.
342793b0f3eeSJean-Francois Moine  *
342893b0f3eeSJean-Francois Moine  * Returns 0 for success
342993b0f3eeSJean-Francois Moine  */
343093b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
343193b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
343293b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
343393b0f3eeSJean-Francois Moine {
343493b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
343593b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
343693b0f3eeSJean-Francois Moine 	char *name;
343793b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
343893b0f3eeSJean-Francois Moine 
343993b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
344093b0f3eeSJean-Francois Moine 	name = "sound-dai";
344193b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
344293b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
344393b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
344493b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
344593b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
344693b0f3eeSJean-Francois Moine 		else
344793b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
344893b0f3eeSJean-Francois Moine 		return num_codecs;
344993b0f3eeSJean-Francois Moine 	}
3450a86854d0SKees Cook 	component = devm_kcalloc(dev,
3451a86854d0SKees Cook 				 num_codecs, sizeof(*component),
345293b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
345393b0f3eeSJean-Francois Moine 	if (!component)
345493b0f3eeSJean-Francois Moine 		return -ENOMEM;
345593b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
345693b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
345793b0f3eeSJean-Francois Moine 
345893b0f3eeSJean-Francois Moine 	/* Parse the list */
34593db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
346093b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
346193b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
346293b0f3eeSJean-Francois Moine 						 index, &args);
346393b0f3eeSJean-Francois Moine 		if (ret)
346493b0f3eeSJean-Francois Moine 			goto err;
346593b0f3eeSJean-Francois Moine 		component->of_node = args.np;
346693b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
346793b0f3eeSJean-Francois Moine 		if (ret < 0)
346893b0f3eeSJean-Francois Moine 			goto err;
346993b0f3eeSJean-Francois Moine 	}
347093b0f3eeSJean-Francois Moine 	return 0;
347193b0f3eeSJean-Francois Moine err:
347294685763SSylwester Nawrocki 	snd_soc_of_put_dai_link_codecs(dai_link);
347393b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
347493b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
347593b0f3eeSJean-Francois Moine 	return ret;
347693b0f3eeSJean-Francois Moine }
347793b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
347893b0f3eeSJean-Francois Moine 
3479c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3480db2a4165SFrank Mandarino {
34816553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
3482fb257897SMark Brown 	snd_soc_util_init();
3483fb257897SMark Brown 
3484db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3485db2a4165SFrank Mandarino }
34864abe8e16SMark Brown module_init(snd_soc_init);
3487db2a4165SFrank Mandarino 
34887d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3489db2a4165SFrank Mandarino {
3490fb257897SMark Brown 	snd_soc_util_exit();
34916553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3492fb257897SMark Brown 
3493db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3494db2a4165SFrank Mandarino }
3495db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3496db2a4165SFrank Mandarino 
3497db2a4165SFrank Mandarino /* Module information */
3498d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3499db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3500db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
35018b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3502