xref: /openbmc/linux/sound/soc/soc-core.c (revision a848125e320a154dfc17b9a377361b4b0df87363)
1873486edSKuninori Morimoto // SPDX-License-Identifier: GPL-2.0+
2873486edSKuninori Morimoto //
3873486edSKuninori Morimoto // soc-core.c  --  ALSA SoC Audio Layer
4873486edSKuninori Morimoto //
5873486edSKuninori Morimoto // Copyright 2005 Wolfson Microelectronics PLC.
6873486edSKuninori Morimoto // Copyright 2005 Openedhand Ltd.
7873486edSKuninori Morimoto // Copyright (C) 2010 Slimlogic Ltd.
8873486edSKuninori Morimoto // Copyright (C) 2010 Texas Instruments Inc.
9873486edSKuninori Morimoto //
10873486edSKuninori Morimoto // Author: Liam Girdwood <lrg@slimlogic.co.uk>
11873486edSKuninori Morimoto //         with code, comments and ideas from :-
12873486edSKuninori Morimoto //         Richard Purdie <richard@openedhand.com>
13873486edSKuninori Morimoto //
14873486edSKuninori Morimoto //  TODO:
15873486edSKuninori Morimoto //   o Add hw rules to enforce rates, etc.
16873486edSKuninori Morimoto //   o More testing with other codecs/machines.
17873486edSKuninori Morimoto //   o Add more codecs and platforms to ensure good API coverage.
18873486edSKuninori Morimoto //   o Support TDM on PCM and I2S
19db2a4165SFrank Mandarino 
20db2a4165SFrank Mandarino #include <linux/module.h>
21db2a4165SFrank Mandarino #include <linux/moduleparam.h>
22db2a4165SFrank Mandarino #include <linux/init.h>
23db2a4165SFrank Mandarino #include <linux/delay.h>
24db2a4165SFrank Mandarino #include <linux/pm.h>
25db2a4165SFrank Mandarino #include <linux/bitops.h>
2612ef193dSTroy Kisky #include <linux/debugfs.h>
27db2a4165SFrank Mandarino #include <linux/platform_device.h>
28741a509fSMarkus Pargmann #include <linux/pinctrl/consumer.h>
29f0e8ed85SMark Brown #include <linux/ctype.h>
305a0e3ad6STejun Heo #include <linux/slab.h>
31bec4fa05SStephen Warren #include <linux/of.h>
32a180e8b9SKuninori Morimoto #include <linux/of_graph.h>
33345233d7SLiam Girdwood #include <linux/dmi.h>
34db2a4165SFrank Mandarino #include <sound/core.h>
353028eb8cSMark Brown #include <sound/jack.h>
36db2a4165SFrank Mandarino #include <sound/pcm.h>
37db2a4165SFrank Mandarino #include <sound/pcm_params.h>
38db2a4165SFrank Mandarino #include <sound/soc.h>
3901d7584cSLiam Girdwood #include <sound/soc-dpcm.h>
408a978234SLiam Girdwood #include <sound/soc-topology.h>
41db2a4165SFrank Mandarino #include <sound/initval.h>
42db2a4165SFrank Mandarino 
43a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
44a8b1d34fSMark Brown #include <trace/events/asoc.h>
45a8b1d34fSMark Brown 
46f0fba2adSLiam Girdwood #define NAME_SIZE	32
47f0fba2adSLiam Girdwood 
48384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
498a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root;
508a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
51384c89e2SMark Brown #endif
52384c89e2SMark Brown 
53c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
54030e79f6SKuninori Morimoto static LIST_HEAD(component_list);
55e894efefSSrinivas Kandagatla static LIST_HEAD(unbind_card_list);
56c5af3a2eSMark Brown 
57368dee94SKuninori Morimoto #define for_each_component(component)			\
58368dee94SKuninori Morimoto 	list_for_each_entry(component, &component_list, list)
59368dee94SKuninori Morimoto 
60db2a4165SFrank Mandarino /*
61587c9844SKuninori Morimoto  * This is used if driver don't need to have CPU/Codec/Platform
62587c9844SKuninori Morimoto  * dai_link. see soc.h
63587c9844SKuninori Morimoto  */
64587c9844SKuninori Morimoto struct snd_soc_dai_link_component null_dailink_component[0];
65587c9844SKuninori Morimoto EXPORT_SYMBOL_GPL(null_dailink_component);
66587c9844SKuninori Morimoto 
67587c9844SKuninori Morimoto /*
68db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
69db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
70db2a4165SFrank Mandarino  * between two audio tracks.
71db2a4165SFrank Mandarino  */
72db2a4165SFrank Mandarino static int pmdown_time = 5000;
73db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
74db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
75db2a4165SFrank Mandarino 
760faf1237SYueHaibing #ifdef CONFIG_DMI
772c7b696aSMarcel Ziswiler /*
782c7b696aSMarcel Ziswiler  * If a DMI filed contain strings in this blacklist (e.g.
7998faf436SMengdong Lin  * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
8098faf436SMengdong Lin  * as invalid and dropped when setting the card long name from DMI info.
8198faf436SMengdong Lin  */
8298faf436SMengdong Lin static const char * const dmi_blacklist[] = {
8398faf436SMengdong Lin 	"To be filled by OEM",
8498faf436SMengdong Lin 	"TBD by OEM",
8598faf436SMengdong Lin 	"Default String",
8698faf436SMengdong Lin 	"Board Manufacturer",
8798faf436SMengdong Lin 	"Board Vendor Name",
8898faf436SMengdong Lin 	"Board Product Name",
8998faf436SMengdong Lin 	NULL,	/* terminator */
9098faf436SMengdong Lin };
910faf1237SYueHaibing #endif
9298faf436SMengdong Lin 
93dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
94dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
95dbe21408SMark Brown {
9636ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
97dbe21408SMark Brown 
98f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
99dbe21408SMark Brown }
100dbe21408SMark Brown 
101dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
102dbe21408SMark Brown 			       struct device_attribute *attr,
103dbe21408SMark Brown 			       const char *buf, size_t count)
104dbe21408SMark Brown {
10536ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
106c593b520SMark Brown 	int ret;
107dbe21408SMark Brown 
108b785a492SJingoo Han 	ret = kstrtol(buf, 10, &rtd->pmdown_time);
109c593b520SMark Brown 	if (ret)
110c593b520SMark Brown 		return ret;
111dbe21408SMark Brown 
112dbe21408SMark Brown 	return count;
113dbe21408SMark Brown }
114dbe21408SMark Brown 
115dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
116dbe21408SMark Brown 
117d29697dcSTakashi Iwai static struct attribute *soc_dev_attrs[] = {
118d29697dcSTakashi Iwai 	&dev_attr_pmdown_time.attr,
119d29697dcSTakashi Iwai 	NULL
120d29697dcSTakashi Iwai };
121d29697dcSTakashi Iwai 
122d29697dcSTakashi Iwai static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
123d29697dcSTakashi Iwai 				       struct attribute *attr, int idx)
124d29697dcSTakashi Iwai {
125d29697dcSTakashi Iwai 	struct device *dev = kobj_to_dev(kobj);
126d29697dcSTakashi Iwai 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
127d29697dcSTakashi Iwai 
128d29697dcSTakashi Iwai 	if (attr == &dev_attr_pmdown_time.attr)
129d29697dcSTakashi Iwai 		return attr->mode; /* always visible */
1303b6eed8dSKuninori Morimoto 	return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
131d29697dcSTakashi Iwai }
132d29697dcSTakashi Iwai 
133d29697dcSTakashi Iwai static const struct attribute_group soc_dapm_dev_group = {
134d29697dcSTakashi Iwai 	.attrs = soc_dapm_dev_attrs,
135d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
136d29697dcSTakashi Iwai };
137d29697dcSTakashi Iwai 
138f7e73b26SMark Brown static const struct attribute_group soc_dev_group = {
139d29697dcSTakashi Iwai 	.attrs = soc_dev_attrs,
140d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
141d29697dcSTakashi Iwai };
142d29697dcSTakashi Iwai 
143d29697dcSTakashi Iwai static const struct attribute_group *soc_dev_attr_groups[] = {
144d29697dcSTakashi Iwai 	&soc_dapm_dev_group,
145f7e73b26SMark Brown 	&soc_dev_group,
146d29697dcSTakashi Iwai 	NULL
147d29697dcSTakashi Iwai };
148d29697dcSTakashi Iwai 
1492624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
15081c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component)
151e73f3de5SRussell King {
1526553bf06SLars-Peter Clausen 	if (!component->card->debugfs_card_root)
1536553bf06SLars-Peter Clausen 		return;
1546553bf06SLars-Peter Clausen 
15581c7cfd1SLars-Peter Clausen 	if (component->debugfs_prefix) {
15681c7cfd1SLars-Peter Clausen 		char *name;
157e73f3de5SRussell King 
15881c7cfd1SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s:%s",
15981c7cfd1SLars-Peter Clausen 			component->debugfs_prefix, component->name);
16081c7cfd1SLars-Peter Clausen 		if (name) {
16181c7cfd1SLars-Peter Clausen 			component->debugfs_root = debugfs_create_dir(name,
16281c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
16381c7cfd1SLars-Peter Clausen 			kfree(name);
16481c7cfd1SLars-Peter Clausen 		}
16581c7cfd1SLars-Peter Clausen 	} else {
16681c7cfd1SLars-Peter Clausen 		component->debugfs_root = debugfs_create_dir(component->name,
16781c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
168e73f3de5SRussell King 	}
169e73f3de5SRussell King 
17081c7cfd1SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
17181c7cfd1SLars-Peter Clausen 		component->debugfs_root);
17281c7cfd1SLars-Peter Clausen }
17381c7cfd1SLars-Peter Clausen 
17481c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
17581c7cfd1SLars-Peter Clausen {
176ad64bfbdSKuninori Morimoto 	if (!component->debugfs_root)
177ad64bfbdSKuninori Morimoto 		return;
17881c7cfd1SLars-Peter Clausen 	debugfs_remove_recursive(component->debugfs_root);
179ad64bfbdSKuninori Morimoto 	component->debugfs_root = NULL;
18081c7cfd1SLars-Peter Clausen }
18181c7cfd1SLars-Peter Clausen 
182c15b2a1dSPeng Donglin static int dai_list_show(struct seq_file *m, void *v)
183f3208780SMark Brown {
1841438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
185f3208780SMark Brown 	struct snd_soc_dai *dai;
186f3208780SMark Brown 
18734e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
18834e81ab4SLars-Peter Clausen 
189368dee94SKuninori Morimoto 	for_each_component(component)
19015a0c645SKuninori Morimoto 		for_each_component_dais(component, dai)
191700c17caSDonglin Peng 			seq_printf(m, "%s\n", dai->name);
192f3208780SMark Brown 
19334e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
19434e81ab4SLars-Peter Clausen 
195700c17caSDonglin Peng 	return 0;
196700c17caSDonglin Peng }
197c15b2a1dSPeng Donglin DEFINE_SHOW_ATTRIBUTE(dai_list);
198f3208780SMark Brown 
199db795f9bSKuninori Morimoto static int component_list_show(struct seq_file *m, void *v)
200db795f9bSKuninori Morimoto {
201db795f9bSKuninori Morimoto 	struct snd_soc_component *component;
202db795f9bSKuninori Morimoto 
203db795f9bSKuninori Morimoto 	mutex_lock(&client_mutex);
204db795f9bSKuninori Morimoto 
205368dee94SKuninori Morimoto 	for_each_component(component)
206db795f9bSKuninori Morimoto 		seq_printf(m, "%s\n", component->name);
207db795f9bSKuninori Morimoto 
208db795f9bSKuninori Morimoto 	mutex_unlock(&client_mutex);
209db795f9bSKuninori Morimoto 
210db795f9bSKuninori Morimoto 	return 0;
211db795f9bSKuninori Morimoto }
212db795f9bSKuninori Morimoto DEFINE_SHOW_ATTRIBUTE(component_list);
213db795f9bSKuninori Morimoto 
214a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
215a6052154SJarkko Nikula {
216a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
2178a9dab1aSMark Brown 						     snd_soc_debugfs_root);
2183a45b867SJarkko Nikula 
219fee531d6SGreg Kroah-Hartman 	debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root,
2203a45b867SJarkko Nikula 			   &card->pop_time);
221d8ca7a0aSKuninori Morimoto 
222d8ca7a0aSKuninori Morimoto 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
223a6052154SJarkko Nikula }
224a6052154SJarkko Nikula 
225a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
226a6052154SJarkko Nikula {
227a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
22829040d1aSKuninori Morimoto 	card->debugfs_card_root = NULL;
229a6052154SJarkko Nikula }
230a6052154SJarkko Nikula 
2316553bf06SLars-Peter Clausen static void snd_soc_debugfs_init(void)
2326553bf06SLars-Peter Clausen {
2336553bf06SLars-Peter Clausen 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
2346553bf06SLars-Peter Clausen 
235fee531d6SGreg Kroah-Hartman 	debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
236fee531d6SGreg Kroah-Hartman 			    &dai_list_fops);
237db795f9bSKuninori Morimoto 
238fee531d6SGreg Kroah-Hartman 	debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
239fee531d6SGreg Kroah-Hartman 			    &component_list_fops);
2406553bf06SLars-Peter Clausen }
2416553bf06SLars-Peter Clausen 
2426553bf06SLars-Peter Clausen static void snd_soc_debugfs_exit(void)
2436553bf06SLars-Peter Clausen {
2446553bf06SLars-Peter Clausen 	debugfs_remove_recursive(snd_soc_debugfs_root);
2456553bf06SLars-Peter Clausen }
2466553bf06SLars-Peter Clausen 
2472624d5faSMark Brown #else
2482624d5faSMark Brown 
24981c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs(
25081c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
2512624d5faSMark Brown {
2522624d5faSMark Brown }
2532624d5faSMark Brown 
25481c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs(
25581c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
256731f1ab2SSebastien Guiriec {
257731f1ab2SSebastien Guiriec }
258731f1ab2SSebastien Guiriec 
259b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
260b95fccbcSAxel Lin {
261b95fccbcSAxel Lin }
262b95fccbcSAxel Lin 
263b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
264b95fccbcSAxel Lin {
265b95fccbcSAxel Lin }
2666553bf06SLars-Peter Clausen 
2676553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_init(void)
2686553bf06SLars-Peter Clausen {
2696553bf06SLars-Peter Clausen }
2706553bf06SLars-Peter Clausen 
2716553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_exit(void)
2726553bf06SLars-Peter Clausen {
2736553bf06SLars-Peter Clausen }
2746553bf06SLars-Peter Clausen 
2752624d5faSMark Brown #endif
2762624d5faSMark Brown 
277a0ac4411SKuninori Morimoto static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
278a0ac4411SKuninori Morimoto 			      struct snd_soc_component *component)
279a0ac4411SKuninori Morimoto {
280a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
281a0ac4411SKuninori Morimoto 
282a0ac4411SKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
283a0ac4411SKuninori Morimoto 		/* already connected */
284a0ac4411SKuninori Morimoto 		if (rtdcom->component == component)
285a0ac4411SKuninori Morimoto 			return 0;
286a0ac4411SKuninori Morimoto 	}
287a0ac4411SKuninori Morimoto 
28832d2c172SKuninori Morimoto 	rtdcom = kmalloc(sizeof(*rtdcom), GFP_KERNEL);
28932d2c172SKuninori Morimoto 	if (!rtdcom)
290a0ac4411SKuninori Morimoto 		return -ENOMEM;
291a0ac4411SKuninori Morimoto 
29232d2c172SKuninori Morimoto 	rtdcom->component = component;
29332d2c172SKuninori Morimoto 	INIT_LIST_HEAD(&rtdcom->list);
294a0ac4411SKuninori Morimoto 
29532d2c172SKuninori Morimoto 	list_add_tail(&rtdcom->list, &rtd->component_list);
296a0ac4411SKuninori Morimoto 
297a0ac4411SKuninori Morimoto 	return 0;
298a0ac4411SKuninori Morimoto }
299a0ac4411SKuninori Morimoto 
300a0ac4411SKuninori Morimoto static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
301a0ac4411SKuninori Morimoto {
302a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
303a0ac4411SKuninori Morimoto 
304a0ac4411SKuninori Morimoto 	for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
305a0ac4411SKuninori Morimoto 		kfree(rtdcom1);
306a0ac4411SKuninori Morimoto 
307a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
308a0ac4411SKuninori Morimoto }
309a0ac4411SKuninori Morimoto 
310a0ac4411SKuninori Morimoto struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
311a0ac4411SKuninori Morimoto 						const char *driver_name)
312a0ac4411SKuninori Morimoto {
313a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
314a0ac4411SKuninori Morimoto 
315971da24cSKuninori Morimoto 	if (!driver_name)
316971da24cSKuninori Morimoto 		return NULL;
317971da24cSKuninori Morimoto 
318a33c0d16SKuninori Morimoto 	/*
319a33c0d16SKuninori Morimoto 	 * NOTE
320a33c0d16SKuninori Morimoto 	 *
321a33c0d16SKuninori Morimoto 	 * snd_soc_rtdcom_lookup() will find component from rtd by using
322a33c0d16SKuninori Morimoto 	 * specified driver name.
323a33c0d16SKuninori Morimoto 	 * But, if many components which have same driver name are connected
324a33c0d16SKuninori Morimoto 	 * to 1 rtd, this function will return 1st found component.
325a33c0d16SKuninori Morimoto 	 */
326a0ac4411SKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
327971da24cSKuninori Morimoto 		const char *component_name = rtdcom->component->driver->name;
328971da24cSKuninori Morimoto 
329971da24cSKuninori Morimoto 		if (!component_name)
330971da24cSKuninori Morimoto 			continue;
331971da24cSKuninori Morimoto 
332971da24cSKuninori Morimoto 		if ((component_name == driver_name) ||
333971da24cSKuninori Morimoto 		    strcmp(component_name, driver_name) == 0)
334a0ac4411SKuninori Morimoto 			return rtdcom->component;
335a0ac4411SKuninori Morimoto 	}
336a0ac4411SKuninori Morimoto 
337a0ac4411SKuninori Morimoto 	return NULL;
338a0ac4411SKuninori Morimoto }
339031734b7SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
340a0ac4411SKuninori Morimoto 
34147c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
34247c88fffSLiam Girdwood 		const char *dai_link, int stream)
34347c88fffSLiam Girdwood {
3441a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
34547c88fffSLiam Girdwood 
346bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
3471a497983SMengdong Lin 		if (rtd->dai_link->no_pcm &&
3481a497983SMengdong Lin 			!strcmp(rtd->dai_link->name, dai_link))
3491a497983SMengdong Lin 			return rtd->pcm->streams[stream].substream;
35047c88fffSLiam Girdwood 	}
351f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
35247c88fffSLiam Girdwood 	return NULL;
35347c88fffSLiam Girdwood }
35447c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
35547c88fffSLiam Girdwood 
35675ab9eb6SKuninori Morimoto static const struct snd_soc_ops null_snd_soc_ops;
35775ab9eb6SKuninori Morimoto 
3581c93a9e0SKuninori Morimoto static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
3591c93a9e0SKuninori Morimoto {
3601c93a9e0SKuninori Morimoto 	kfree(rtd->codec_dais);
3611c93a9e0SKuninori Morimoto 	snd_soc_rtdcom_del_all(rtd);
3621c93a9e0SKuninori Morimoto 	kfree(rtd);
3631c93a9e0SKuninori Morimoto }
3641c93a9e0SKuninori Morimoto 
3651a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
3661a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
3671a497983SMengdong Lin {
3681a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
3691a497983SMengdong Lin 
3701a497983SMengdong Lin 	rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
3711a497983SMengdong Lin 	if (!rtd)
3721a497983SMengdong Lin 		return NULL;
3731a497983SMengdong Lin 
374a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
3751a497983SMengdong Lin 	rtd->card = card;
3761a497983SMengdong Lin 	rtd->dai_link = dai_link;
37775ab9eb6SKuninori Morimoto 	if (!rtd->dai_link->ops)
37875ab9eb6SKuninori Morimoto 		rtd->dai_link->ops = &null_snd_soc_ops;
37975ab9eb6SKuninori Morimoto 
3806396bb22SKees Cook 	rtd->codec_dais = kcalloc(dai_link->num_codecs,
3816396bb22SKees Cook 					sizeof(struct snd_soc_dai *),
3821a497983SMengdong Lin 					GFP_KERNEL);
3831a497983SMengdong Lin 	if (!rtd->codec_dais) {
3841a497983SMengdong Lin 		kfree(rtd);
3851a497983SMengdong Lin 		return NULL;
3861a497983SMengdong Lin 	}
3871a497983SMengdong Lin 
3886634e3d6SKuninori Morimoto 	/* see for_each_card_rtds */
3891a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
3901a497983SMengdong Lin 	rtd->num = card->num_rtd;
3911a497983SMengdong Lin 	card->num_rtd++;
392*a848125eSKuninori Morimoto 
393*a848125eSKuninori Morimoto 	return rtd;
3941a497983SMengdong Lin }
3951a497983SMengdong Lin 
3961a497983SMengdong Lin static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
3971a497983SMengdong Lin {
3981a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, *_rtd;
3991a497983SMengdong Lin 
400bcb1fd1fSKuninori Morimoto 	for_each_card_rtds_safe(card, rtd, _rtd) {
4011a497983SMengdong Lin 		list_del(&rtd->list);
4021a497983SMengdong Lin 		soc_free_pcm_runtime(rtd);
4031a497983SMengdong Lin 	}
4041a497983SMengdong Lin 
4051a497983SMengdong Lin 	card->num_rtd = 0;
4061a497983SMengdong Lin }
4071a497983SMengdong Lin 
40847c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
40947c88fffSLiam Girdwood 		const char *dai_link)
41047c88fffSLiam Girdwood {
4111a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
41247c88fffSLiam Girdwood 
413bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4141a497983SMengdong Lin 		if (!strcmp(rtd->dai_link->name, dai_link))
4151a497983SMengdong Lin 			return rtd;
41647c88fffSLiam Girdwood 	}
417f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
41847c88fffSLiam Girdwood 	return NULL;
41947c88fffSLiam Girdwood }
42047c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
42147c88fffSLiam Girdwood 
42265462e44SKuninori Morimoto static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
42365462e44SKuninori Morimoto {
42465462e44SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
42565462e44SKuninori Morimoto 
42665462e44SKuninori Morimoto 	for_each_card_rtds(card, rtd)
42765462e44SKuninori Morimoto 		flush_delayed_work(&rtd->delayed_work);
42865462e44SKuninori Morimoto }
42965462e44SKuninori Morimoto 
4306f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
431db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
4326f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
433db2a4165SFrank Mandarino {
4346f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
435d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
4361a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
4371a497983SMengdong Lin 	int i;
438db2a4165SFrank Mandarino 
439c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
440c5599b87SLars-Peter Clausen 	if (!card->instantiated)
441e3509ff0SDaniel Mack 		return 0;
442e3509ff0SDaniel Mack 
4432c7b696aSMarcel Ziswiler 	/*
4442c7b696aSMarcel Ziswiler 	 * Due to the resume being scheduled into a workqueue we could
4456ed25978SAndy Green 	 * suspend before that's finished - wait for it to complete.
4466ed25978SAndy Green 	 */
447f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
4486ed25978SAndy Green 
4496ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
450f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
4516ed25978SAndy Green 
452a00f90f9SMark Brown 	/* mute any active DACs */
453bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4540b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
4553efab7dcSMark Brown 
4561a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4573efab7dcSMark Brown 			continue;
4583efab7dcSMark Brown 
4590b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
46088fdffa2SKuninori Morimoto 			if (dai->playback_active)
46188fdffa2SKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 1,
46288fdffa2SKuninori Morimoto 						SNDRV_PCM_STREAM_PLAYBACK);
463db2a4165SFrank Mandarino 		}
46488bd870fSBenoit Cousson 	}
465db2a4165SFrank Mandarino 
4664ccab3e7SLiam Girdwood 	/* suspend all pcms */
467bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4681a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4693efab7dcSMark Brown 			continue;
4703efab7dcSMark Brown 
4711a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
4723efab7dcSMark Brown 	}
4734ccab3e7SLiam Girdwood 
47487506549SMark Brown 	if (card->suspend_pre)
47570b2ac12SMark Brown 		card->suspend_pre(card);
476db2a4165SFrank Mandarino 
477bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4781a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
4793efab7dcSMark Brown 
4801a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4813efab7dcSMark Brown 			continue;
4823efab7dcSMark Brown 
483e0f22622SKuninori Morimoto 		if (!cpu_dai->driver->bus_control)
484e0f22622SKuninori Morimoto 			snd_soc_dai_suspend(cpu_dai);
485db2a4165SFrank Mandarino 	}
486db2a4165SFrank Mandarino 
48737660b6dSLars-Peter Clausen 	/* close any waiting streams */
48865462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
489db2a4165SFrank Mandarino 
490bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4913efab7dcSMark Brown 
4921a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4933efab7dcSMark Brown 			continue;
4943efab7dcSMark Brown 
4951a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
4967bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
497db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
498f0fba2adSLiam Girdwood 
4991a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
5007bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
501db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
502db2a4165SFrank Mandarino 	}
503db2a4165SFrank Mandarino 
5048be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
5058be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
506e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
507e2d32ff6SMark Brown 
5089178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
509f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
5102c7b696aSMarcel Ziswiler 		struct snd_soc_dapm_context *dapm =
5112c7b696aSMarcel Ziswiler 				snd_soc_component_get_dapm(component);
512d9fc4063SKuninori Morimoto 
5132c7b696aSMarcel Ziswiler 		/*
5142c7b696aSMarcel Ziswiler 		 * If there are paths active then the COMPONENT will be held
5152c7b696aSMarcel Ziswiler 		 * with bias _ON and should not be suspended.
5162c7b696aSMarcel Ziswiler 		 */
517e40fadbcSKuninori Morimoto 		if (!snd_soc_component_is_suspended(component)) {
5184890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
5191547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
520125a25daSMark Brown 				/*
5219178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
522125a25daSMark Brown 				 * bias off then being in STANDBY
523125a25daSMark Brown 				 * means it's doing something,
524125a25daSMark Brown 				 * otherwise fall through.
525125a25daSMark Brown 				 */
5264890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
5279178feb4SKuninori Morimoto 					dev_dbg(component->dev,
52810e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
529125a25daSMark Brown 					break;
530125a25daSMark Brown 				}
5311a12d5dcSGustavo A. R. Silva 				/* fall through */
532a8093297SLars-Peter Clausen 
5331547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
53466c51573SKuninori Morimoto 				snd_soc_component_suspend(component);
5359178feb4SKuninori Morimoto 				if (component->regmap)
5369178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
537988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
5389178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
5391547aba9SMark Brown 				break;
5401547aba9SMark Brown 			default:
5419178feb4SKuninori Morimoto 				dev_dbg(component->dev,
5429178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
5431547aba9SMark Brown 				break;
5441547aba9SMark Brown 			}
5451547aba9SMark Brown 		}
546f0fba2adSLiam Girdwood 	}
547db2a4165SFrank Mandarino 
548bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5491a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
5503efab7dcSMark Brown 
5511a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5523efab7dcSMark Brown 			continue;
5533efab7dcSMark Brown 
554e0f22622SKuninori Morimoto 		if (cpu_dai->driver->bus_control)
555e0f22622SKuninori Morimoto 			snd_soc_dai_suspend(cpu_dai);
556988e8cc4SNicolin Chen 
557988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
558988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
559db2a4165SFrank Mandarino 	}
560db2a4165SFrank Mandarino 
56187506549SMark Brown 	if (card->suspend_post)
56270b2ac12SMark Brown 		card->suspend_post(card);
563db2a4165SFrank Mandarino 
564db2a4165SFrank Mandarino 	return 0;
565db2a4165SFrank Mandarino }
5666f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
567db2a4165SFrank Mandarino 
5682c7b696aSMarcel Ziswiler /*
5692c7b696aSMarcel Ziswiler  * deferred resume work, so resume can complete before we finished
5706ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
5716ed25978SAndy Green  */
5726ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
573db2a4165SFrank Mandarino {
574f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
5752c7b696aSMarcel Ziswiler 			container_of(work, struct snd_soc_card,
5762c7b696aSMarcel Ziswiler 				     deferred_resume_work);
5771a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
578d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
5791a497983SMengdong Lin 	int i;
580db2a4165SFrank Mandarino 
5812c7b696aSMarcel Ziswiler 	/*
5822c7b696aSMarcel Ziswiler 	 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
5836ed25978SAndy Green 	 * so userspace apps are blocked from touching us
5846ed25978SAndy Green 	 */
5856ed25978SAndy Green 
586f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
5876ed25978SAndy Green 
5889949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
589f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
5909949788bSMark Brown 
59187506549SMark Brown 	if (card->resume_pre)
59270b2ac12SMark Brown 		card->resume_pre(card);
593db2a4165SFrank Mandarino 
594bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
595bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5961a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
5973efab7dcSMark Brown 
5981a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5993efab7dcSMark Brown 			continue;
6003efab7dcSMark Brown 
60124b09d05SKuninori Morimoto 		if (cpu_dai->driver->bus_control)
60224b09d05SKuninori Morimoto 			snd_soc_dai_resume(cpu_dai);
603db2a4165SFrank Mandarino 	}
604db2a4165SFrank Mandarino 
605f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
606e40fadbcSKuninori Morimoto 		if (snd_soc_component_is_suspended(component))
6079a840cbaSKuninori Morimoto 			snd_soc_component_resume(component);
6081547aba9SMark Brown 	}
609db2a4165SFrank Mandarino 
610bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6113efab7dcSMark Brown 
6121a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6133efab7dcSMark Brown 			continue;
6143efab7dcSMark Brown 
6151a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
616d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
617db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
618f0fba2adSLiam Girdwood 
6191a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
620d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
621db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
622db2a4165SFrank Mandarino 	}
623db2a4165SFrank Mandarino 
6243ff3f64bSMark Brown 	/* unmute any active DACs */
625bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6260b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
6273efab7dcSMark Brown 
6281a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6293efab7dcSMark Brown 			continue;
6303efab7dcSMark Brown 
6310b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
63288fdffa2SKuninori Morimoto 			if (dai->playback_active)
63388fdffa2SKuninori Morimoto 				snd_soc_dai_digital_mute(dai, 0,
63488fdffa2SKuninori Morimoto 						SNDRV_PCM_STREAM_PLAYBACK);
635db2a4165SFrank Mandarino 		}
63688bd870fSBenoit Cousson 	}
637db2a4165SFrank Mandarino 
638bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6391a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6403efab7dcSMark Brown 
6411a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6423efab7dcSMark Brown 			continue;
6433efab7dcSMark Brown 
64424b09d05SKuninori Morimoto 		if (!cpu_dai->driver->bus_control)
64524b09d05SKuninori Morimoto 			snd_soc_dai_resume(cpu_dai);
646db2a4165SFrank Mandarino 	}
647db2a4165SFrank Mandarino 
64887506549SMark Brown 	if (card->resume_post)
64970b2ac12SMark Brown 		card->resume_post(card);
650db2a4165SFrank Mandarino 
651f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
6526ed25978SAndy Green 
6538be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
6548be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
655e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
6561a7aaa58SJeeja KP 
6571a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
6581a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
6596ed25978SAndy Green }
6606ed25978SAndy Green 
6616ed25978SAndy Green /* powers up audio subsystem after a suspend */
6626f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
6636ed25978SAndy Green {
6646f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
665bc263214SLars-Peter Clausen 	bool bus_control = false;
6661a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
66722d251a5SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
66822d251a5SKuninori Morimoto 	int i;
669b9dd94a8SPeter Ujfalusi 
670c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
671c5599b87SLars-Peter Clausen 	if (!card->instantiated)
6725ff1ddf2SEric Miao 		return 0;
6735ff1ddf2SEric Miao 
674988e8cc4SNicolin Chen 	/* activate pins from sleep state */
675bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
67688bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
67788bd870fSBenoit Cousson 
678988e8cc4SNicolin Chen 		if (cpu_dai->active)
679988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
68088bd870fSBenoit Cousson 
68122d251a5SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
682988e8cc4SNicolin Chen 			if (codec_dai->active)
683988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
684988e8cc4SNicolin Chen 		}
68588bd870fSBenoit Cousson 	}
686988e8cc4SNicolin Chen 
687bc263214SLars-Peter Clausen 	/*
688bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
689bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
690bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
69164ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
69264ab9baaSMark Brown 	 */
693bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6941a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6952c7b696aSMarcel Ziswiler 
696bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
69782e14e8bSStephen Warren 	}
698bc263214SLars-Peter Clausen 	if (bus_control) {
699bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
70064ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
70164ab9baaSMark Brown 	} else {
702f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
7036308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
704f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
705f0fba2adSLiam Girdwood 	}
7066ed25978SAndy Green 
707db2a4165SFrank Mandarino 	return 0;
708db2a4165SFrank Mandarino }
7096f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
710b3da4251SKuninori Morimoto 
711b3da4251SKuninori Morimoto static void soc_resume_init(struct snd_soc_card *card)
712b3da4251SKuninori Morimoto {
713b3da4251SKuninori Morimoto 	/* deferred resume work */
714b3da4251SKuninori Morimoto 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
715b3da4251SKuninori Morimoto }
716db2a4165SFrank Mandarino #else
7176f8ab4acSMark Brown #define snd_soc_suspend NULL
7186f8ab4acSMark Brown #define snd_soc_resume NULL
719b3da4251SKuninori Morimoto static inline void soc_resume_init(struct snd_soc_card *card)
720b3da4251SKuninori Morimoto {
721b3da4251SKuninori Morimoto }
722db2a4165SFrank Mandarino #endif
723db2a4165SFrank Mandarino 
72485e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
72502a06d30SBarry Song };
72602a06d30SBarry Song 
727c0834440SKuninori Morimoto static struct device_node
728c0834440SKuninori Morimoto *soc_component_to_node(struct snd_soc_component *component)
72912023a9aSMisael Lopez Cruz {
730c0834440SKuninori Morimoto 	struct device_node *of_node;
73112023a9aSMisael Lopez Cruz 
732c0834440SKuninori Morimoto 	of_node = component->dev->of_node;
733c0834440SKuninori Morimoto 	if (!of_node && component->dev->parent)
734c0834440SKuninori Morimoto 		of_node = component->dev->parent->of_node;
73534e81ab4SLars-Peter Clausen 
736c0834440SKuninori Morimoto 	return of_node;
73712023a9aSMisael Lopez Cruz }
73812023a9aSMisael Lopez Cruz 
739be6ac0a9SKuninori Morimoto static int snd_soc_is_matching_component(
740be6ac0a9SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc,
741be6ac0a9SKuninori Morimoto 	struct snd_soc_component *component)
742be6ac0a9SKuninori Morimoto {
743be6ac0a9SKuninori Morimoto 	struct device_node *component_of_node;
744be6ac0a9SKuninori Morimoto 
7457d7db5d3SKuninori Morimoto 	if (!dlc)
7467d7db5d3SKuninori Morimoto 		return 0;
7477d7db5d3SKuninori Morimoto 
7487d7db5d3SKuninori Morimoto 	component_of_node = soc_component_to_node(component);
749be6ac0a9SKuninori Morimoto 
750be6ac0a9SKuninori Morimoto 	if (dlc->of_node && component_of_node != dlc->of_node)
751be6ac0a9SKuninori Morimoto 		return 0;
752be6ac0a9SKuninori Morimoto 	if (dlc->name && strcmp(component->name, dlc->name))
753be6ac0a9SKuninori Morimoto 		return 0;
754be6ac0a9SKuninori Morimoto 
755be6ac0a9SKuninori Morimoto 	return 1;
756be6ac0a9SKuninori Morimoto }
757be6ac0a9SKuninori Morimoto 
758db2a4165SFrank Mandarino static struct snd_soc_component *soc_find_component(
759c1e230f0SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc)
760db2a4165SFrank Mandarino {
761db2a4165SFrank Mandarino 	struct snd_soc_component *component;
762db2a4165SFrank Mandarino 
763db2a4165SFrank Mandarino 	lockdep_assert_held(&client_mutex);
764db2a4165SFrank Mandarino 
765e3303268SKuninori Morimoto 	/*
766e3303268SKuninori Morimoto 	 * NOTE
767e3303268SKuninori Morimoto 	 *
768e3303268SKuninori Morimoto 	 * It returns *1st* found component, but some driver
769e3303268SKuninori Morimoto 	 * has few components by same of_node/name
770e3303268SKuninori Morimoto 	 * ex)
771e3303268SKuninori Morimoto 	 *	CPU component and generic DMAEngine component
772e3303268SKuninori Morimoto 	 */
773c1e230f0SKuninori Morimoto 	for_each_component(component)
774c1e230f0SKuninori Morimoto 		if (snd_soc_is_matching_component(dlc, component))
775db2a4165SFrank Mandarino 			return component;
776db2a4165SFrank Mandarino 
777db2a4165SFrank Mandarino 	return NULL;
77812023a9aSMisael Lopez Cruz }
77912023a9aSMisael Lopez Cruz 
780fbb88b5cSMengdong Lin /**
781fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
782fbb88b5cSMengdong Lin  *
7834958471bSJeffy Chen  * @dlc: name of the DAI or the DAI driver and optional component info to match
784fbb88b5cSMengdong Lin  *
785ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
786fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
787fbb88b5cSMengdong Lin  * should also match if being specified.
788fbb88b5cSMengdong Lin  *
789fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
790fbb88b5cSMengdong Lin  */
791305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
79214621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
79312023a9aSMisael Lopez Cruz {
79414621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
79514621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
79612023a9aSMisael Lopez Cruz 
79734e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
79834e81ab4SLars-Peter Clausen 
79914621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs */
800368dee94SKuninori Morimoto 	for_each_component(component) {
801be6ac0a9SKuninori Morimoto 		if (!snd_soc_is_matching_component(dlc, component))
80212023a9aSMisael Lopez Cruz 			continue;
80315a0c645SKuninori Morimoto 		for_each_component_dais(component, dai) {
8044958471bSJeffy Chen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
8056a6dafdaSJeffy Chen 			    && (!dai->driver->name
8066a6dafdaSJeffy Chen 				|| strcmp(dai->driver->name, dlc->dai_name)))
80714621c7eSLars-Peter Clausen 				continue;
80812023a9aSMisael Lopez Cruz 
80914621c7eSLars-Peter Clausen 			return dai;
81012023a9aSMisael Lopez Cruz 		}
81112023a9aSMisael Lopez Cruz 	}
81212023a9aSMisael Lopez Cruz 
81312023a9aSMisael Lopez Cruz 	return NULL;
81412023a9aSMisael Lopez Cruz }
815305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
81612023a9aSMisael Lopez Cruz 
81717fb1755SMengdong Lin /**
81817fb1755SMengdong Lin  * snd_soc_find_dai_link - Find a DAI link
81917fb1755SMengdong Lin  *
82017fb1755SMengdong Lin  * @card: soc card
82117fb1755SMengdong Lin  * @id: DAI link ID to match
82217fb1755SMengdong Lin  * @name: DAI link name to match, optional
8238abab35fSCharles Keepax  * @stream_name: DAI link stream name to match, optional
82417fb1755SMengdong Lin  *
82517fb1755SMengdong Lin  * This function will search all existing DAI links of the soc card to
82617fb1755SMengdong Lin  * find the link of the same ID. Since DAI links may not have their
82717fb1755SMengdong Lin  * unique ID, so name and stream name should also match if being
82817fb1755SMengdong Lin  * specified.
82917fb1755SMengdong Lin  *
83017fb1755SMengdong Lin  * Return: pointer of DAI link, or NULL if not found.
83117fb1755SMengdong Lin  */
83217fb1755SMengdong Lin struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
83317fb1755SMengdong Lin 					       int id, const char *name,
83417fb1755SMengdong Lin 					       const char *stream_name)
83517fb1755SMengdong Lin {
83642849064SKuninori Morimoto 	struct snd_soc_dai_link *link;
83717fb1755SMengdong Lin 
83817fb1755SMengdong Lin 	lockdep_assert_held(&client_mutex);
83917fb1755SMengdong Lin 
84042849064SKuninori Morimoto 	for_each_card_links(card, link) {
84117fb1755SMengdong Lin 		if (link->id != id)
84217fb1755SMengdong Lin 			continue;
84317fb1755SMengdong Lin 
84417fb1755SMengdong Lin 		if (name && (!link->name || strcmp(name, link->name)))
84517fb1755SMengdong Lin 			continue;
84617fb1755SMengdong Lin 
84717fb1755SMengdong Lin 		if (stream_name && (!link->stream_name
84817fb1755SMengdong Lin 			|| strcmp(stream_name, link->stream_name)))
84917fb1755SMengdong Lin 			continue;
85017fb1755SMengdong Lin 
85117fb1755SMengdong Lin 		return link;
85217fb1755SMengdong Lin 	}
85317fb1755SMengdong Lin 
85417fb1755SMengdong Lin 	return NULL;
85517fb1755SMengdong Lin }
85617fb1755SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
85717fb1755SMengdong Lin 
85849a5ba1cSMengdong Lin static bool soc_is_dai_link_bound(struct snd_soc_card *card,
85949a5ba1cSMengdong Lin 		struct snd_soc_dai_link *dai_link)
860db2a4165SFrank Mandarino {
86149a5ba1cSMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
86249a5ba1cSMengdong Lin 
863bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
86449a5ba1cSMengdong Lin 		if (rtd->dai_link == dai_link)
86549a5ba1cSMengdong Lin 			return true;
86649a5ba1cSMengdong Lin 	}
86749a5ba1cSMengdong Lin 
86849a5ba1cSMengdong Lin 	return false;
86949a5ba1cSMengdong Lin }
87049a5ba1cSMengdong Lin 
8716f2f1ff0SMengdong Lin static int soc_bind_dai_link(struct snd_soc_card *card,
8726f2f1ff0SMengdong Lin 	struct snd_soc_dai_link *dai_link)
873db2a4165SFrank Mandarino {
8741a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
87534614739SJerome Brunet 	struct snd_soc_dai_link_component *codec, *platform;
87690be711eSKuninori Morimoto 	struct snd_soc_component *component;
87788bd870fSBenoit Cousson 	int i;
878db2a4165SFrank Mandarino 
879a655de80SLiam Girdwood 	if (dai_link->ignore)
880a655de80SLiam Girdwood 		return 0;
881a655de80SLiam Girdwood 
8826f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
8836308419aSMark Brown 
88449a5ba1cSMengdong Lin 	if (soc_is_dai_link_bound(card, dai_link)) {
88549a5ba1cSMengdong Lin 		dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
88649a5ba1cSMengdong Lin 			dai_link->name);
88749a5ba1cSMengdong Lin 		return 0;
88849a5ba1cSMengdong Lin 	}
889db2a4165SFrank Mandarino 
890513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
891513cb311SSudip Mukherjee 	if (!rtd)
892513cb311SSudip Mukherjee 		return -ENOMEM;
893513cb311SSudip Mukherjee 
89408a5841eSKuninori Morimoto 	/* FIXME: we need multi CPU support in the future */
89508a5841eSKuninori Morimoto 	rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
896b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
8976b490879SMartin Hundebøll 		dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
89808a5841eSKuninori Morimoto 			 dai_link->cpus->dai_name);
8991a497983SMengdong Lin 		goto _err_defer;
900c5af3a2eSMark Brown 	}
90190be711eSKuninori Morimoto 	snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
902c5af3a2eSMark Brown 
90388bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
904e2b30edfSKuninori Morimoto 	rtd->num_codecs = dai_link->num_codecs;
90534614739SJerome Brunet 	for_each_link_codecs(dai_link, i, codec) {
90634614739SJerome Brunet 		rtd->codec_dais[i] = snd_soc_find_dai(codec);
9070a2cfcd9SKuninori Morimoto 		if (!rtd->codec_dais[i]) {
9087c7e2d6aSStefan Agner 			dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
90934614739SJerome Brunet 				 codec->dai_name);
9101a497983SMengdong Lin 			goto _err_defer;
91112023a9aSMisael Lopez Cruz 		}
91234614739SJerome Brunet 
9130a2cfcd9SKuninori Morimoto 		snd_soc_rtdcom_add(rtd, rtd->codec_dais[i]->component);
91488bd870fSBenoit Cousson 	}
91588bd870fSBenoit Cousson 
91688bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
9170a2cfcd9SKuninori Morimoto 	rtd->codec_dai = rtd->codec_dais[0];
91812023a9aSMisael Lopez Cruz 
919e2b30edfSKuninori Morimoto 	/* Find PLATFORM from registered PLATFORMs */
92034614739SJerome Brunet 	for_each_link_platforms(dai_link, i, platform) {
921368dee94SKuninori Morimoto 		for_each_component(component) {
92234614739SJerome Brunet 			if (!snd_soc_is_matching_component(platform, component))
92390be711eSKuninori Morimoto 				continue;
92490be711eSKuninori Morimoto 
92590be711eSKuninori Morimoto 			snd_soc_rtdcom_add(rtd, component);
92690be711eSKuninori Morimoto 		}
92734614739SJerome Brunet 	}
92890be711eSKuninori Morimoto 
929b19e6e7bSMark Brown 	return 0;
9301a497983SMengdong Lin 
9311a497983SMengdong Lin _err_defer:
9321a497983SMengdong Lin 	soc_free_pcm_runtime(rtd);
9331a497983SMengdong Lin 	return -EPROBE_DEFER;
9346b05eda6SMark Brown }
9356b05eda6SMark Brown 
936ffd60fbaSKuninori Morimoto static void soc_set_of_name_prefix(struct snd_soc_component *component)
937ffd60fbaSKuninori Morimoto {
938ffd60fbaSKuninori Morimoto 	struct device_node *of_node = soc_component_to_node(component);
939ffd60fbaSKuninori Morimoto 	const char *str;
940ffd60fbaSKuninori Morimoto 	int ret;
941ffd60fbaSKuninori Morimoto 
942ffd60fbaSKuninori Morimoto 	ret = of_property_read_string(of_node, "sound-name-prefix", &str);
943ffd60fbaSKuninori Morimoto 	if (!ret)
944ffd60fbaSKuninori Morimoto 		component->name_prefix = str;
945ffd60fbaSKuninori Morimoto }
946ffd60fbaSKuninori Morimoto 
947ffd60fbaSKuninori Morimoto static void soc_set_name_prefix(struct snd_soc_card *card,
948ffd60fbaSKuninori Morimoto 				struct snd_soc_component *component)
949ffd60fbaSKuninori Morimoto {
950ffd60fbaSKuninori Morimoto 	int i;
951ffd60fbaSKuninori Morimoto 
952ffd60fbaSKuninori Morimoto 	for (i = 0; i < card->num_configs && card->codec_conf; i++) {
953ffd60fbaSKuninori Morimoto 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
954ffd60fbaSKuninori Morimoto 		struct device_node *of_node = soc_component_to_node(component);
955ffd60fbaSKuninori Morimoto 
956ffd60fbaSKuninori Morimoto 		if (map->of_node && of_node != map->of_node)
957ffd60fbaSKuninori Morimoto 			continue;
958ffd60fbaSKuninori Morimoto 		if (map->dev_name && strcmp(component->name, map->dev_name))
959ffd60fbaSKuninori Morimoto 			continue;
960ffd60fbaSKuninori Morimoto 		component->name_prefix = map->name_prefix;
961ffd60fbaSKuninori Morimoto 		return;
962ffd60fbaSKuninori Morimoto 	}
963ffd60fbaSKuninori Morimoto 
964ffd60fbaSKuninori Morimoto 	/*
965ffd60fbaSKuninori Morimoto 	 * If there is no configuration table or no match in the table,
966ffd60fbaSKuninori Morimoto 	 * check if a prefix is provided in the node
967ffd60fbaSKuninori Morimoto 	 */
968ffd60fbaSKuninori Morimoto 	soc_set_of_name_prefix(component);
969ffd60fbaSKuninori Morimoto }
970ffd60fbaSKuninori Morimoto 
97122d14231SKuninori Morimoto static void soc_cleanup_component(struct snd_soc_component *component)
97222d14231SKuninori Morimoto {
97304f770d9SKuninori Morimoto 	/* For framework level robustness */
9743bb936f5SAmadeusz Sławiński 	snd_soc_component_set_jack(component, NULL, NULL);
97504f770d9SKuninori Morimoto 
9767a5d9815SBard liao 	list_del_init(&component->card_list);
97722d14231SKuninori Morimoto 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
97822d14231SKuninori Morimoto 	soc_cleanup_component_debugfs(component);
97922d14231SKuninori Morimoto 	component->card = NULL;
9800e36f36bSPierre-Louis Bossart 	snd_soc_component_module_put_when_remove(component);
98122d14231SKuninori Morimoto }
98222d14231SKuninori Morimoto 
983f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
984d12cd198SStephen Warren {
985abd31b32SLars-Peter Clausen 	if (!component->card)
98670090bbbSLars-Peter Clausen 		return;
987d12cd198SStephen Warren 
98803b34dd7SKuninori Morimoto 	snd_soc_component_remove(component);
9897a5d9815SBard liao 
99022d14231SKuninori Morimoto 	soc_cleanup_component(component);
991d12cd198SStephen Warren }
992d12cd198SStephen Warren 
993ffd60fbaSKuninori Morimoto static int soc_probe_component(struct snd_soc_card *card,
994ffd60fbaSKuninori Morimoto 			       struct snd_soc_component *component)
995ffd60fbaSKuninori Morimoto {
996ffd60fbaSKuninori Morimoto 	struct snd_soc_dapm_context *dapm =
997ffd60fbaSKuninori Morimoto 		snd_soc_component_get_dapm(component);
998ffd60fbaSKuninori Morimoto 	struct snd_soc_dai *dai;
999ffd60fbaSKuninori Morimoto 	int ret;
1000ffd60fbaSKuninori Morimoto 
1001ffd60fbaSKuninori Morimoto 	if (!strcmp(component->name, "snd-soc-dummy"))
1002ffd60fbaSKuninori Morimoto 		return 0;
1003ffd60fbaSKuninori Morimoto 
1004ffd60fbaSKuninori Morimoto 	if (component->card) {
1005ffd60fbaSKuninori Morimoto 		if (component->card != card) {
1006ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1007ffd60fbaSKuninori Morimoto 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1008ffd60fbaSKuninori Morimoto 				card->name, component->card->name);
1009ffd60fbaSKuninori Morimoto 			return -ENODEV;
1010ffd60fbaSKuninori Morimoto 		}
1011ffd60fbaSKuninori Morimoto 		return 0;
1012ffd60fbaSKuninori Morimoto 	}
1013ffd60fbaSKuninori Morimoto 
1014ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_module_get_when_probe(component);
1015ffd60fbaSKuninori Morimoto 	if (ret < 0)
1016ffd60fbaSKuninori Morimoto 		return ret;
1017ffd60fbaSKuninori Morimoto 
1018ffd60fbaSKuninori Morimoto 	component->card = card;
1019ffd60fbaSKuninori Morimoto 	soc_set_name_prefix(card, component);
1020ffd60fbaSKuninori Morimoto 
1021ffd60fbaSKuninori Morimoto 	soc_init_component_debugfs(component);
1022ffd60fbaSKuninori Morimoto 
102395c267ddSKuninori Morimoto 	snd_soc_dapm_init(dapm, card, component);
1024b614beafSKuninori Morimoto 
1025ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_new_controls(dapm,
1026ffd60fbaSKuninori Morimoto 					component->driver->dapm_widgets,
1027ffd60fbaSKuninori Morimoto 					component->driver->num_dapm_widgets);
1028ffd60fbaSKuninori Morimoto 
1029ffd60fbaSKuninori Morimoto 	if (ret != 0) {
1030ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1031ffd60fbaSKuninori Morimoto 			"Failed to create new controls %d\n", ret);
1032ffd60fbaSKuninori Morimoto 		goto err_probe;
1033ffd60fbaSKuninori Morimoto 	}
1034ffd60fbaSKuninori Morimoto 
1035ffd60fbaSKuninori Morimoto 	for_each_component_dais(component, dai) {
1036ffd60fbaSKuninori Morimoto 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1037ffd60fbaSKuninori Morimoto 		if (ret != 0) {
1038ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1039ffd60fbaSKuninori Morimoto 				"Failed to create DAI widgets %d\n", ret);
1040ffd60fbaSKuninori Morimoto 			goto err_probe;
1041ffd60fbaSKuninori Morimoto 		}
1042ffd60fbaSKuninori Morimoto 	}
1043ffd60fbaSKuninori Morimoto 
1044ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_probe(component);
1045ffd60fbaSKuninori Morimoto 	if (ret < 0) {
1046ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1047ffd60fbaSKuninori Morimoto 			"ASoC: failed to probe component %d\n", ret);
1048ffd60fbaSKuninori Morimoto 		goto err_probe;
1049ffd60fbaSKuninori Morimoto 	}
1050ffd60fbaSKuninori Morimoto 	WARN(dapm->idle_bias_off &&
1051ffd60fbaSKuninori Morimoto 	     dapm->bias_level != SND_SOC_BIAS_OFF,
1052ffd60fbaSKuninori Morimoto 	     "codec %s can not start from non-off bias with idle_bias_off==1\n",
1053ffd60fbaSKuninori Morimoto 	     component->name);
1054ffd60fbaSKuninori Morimoto 
1055ffd60fbaSKuninori Morimoto 	/* machine specific init */
1056ffd60fbaSKuninori Morimoto 	if (component->init) {
1057ffd60fbaSKuninori Morimoto 		ret = component->init(component);
1058ffd60fbaSKuninori Morimoto 		if (ret < 0) {
1059ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1060ffd60fbaSKuninori Morimoto 				"Failed to do machine specific init %d\n", ret);
1061ffd60fbaSKuninori Morimoto 			goto err_probe;
1062ffd60fbaSKuninori Morimoto 		}
1063ffd60fbaSKuninori Morimoto 	}
1064ffd60fbaSKuninori Morimoto 
1065ffd60fbaSKuninori Morimoto 	ret = snd_soc_add_component_controls(component,
1066ffd60fbaSKuninori Morimoto 					     component->driver->controls,
1067ffd60fbaSKuninori Morimoto 					     component->driver->num_controls);
1068ffd60fbaSKuninori Morimoto 	if (ret < 0)
1069ffd60fbaSKuninori Morimoto 		goto err_probe;
1070ffd60fbaSKuninori Morimoto 
1071ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(dapm,
1072ffd60fbaSKuninori Morimoto 				      component->driver->dapm_routes,
1073ffd60fbaSKuninori Morimoto 				      component->driver->num_dapm_routes);
1074ffd60fbaSKuninori Morimoto 	if (ret < 0)
1075ffd60fbaSKuninori Morimoto 		goto err_probe;
1076ffd60fbaSKuninori Morimoto 
1077ffd60fbaSKuninori Morimoto 	/* see for_each_card_components */
1078ffd60fbaSKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1079ffd60fbaSKuninori Morimoto 
1080ffd60fbaSKuninori Morimoto err_probe:
1081ffd60fbaSKuninori Morimoto 	if (ret < 0)
1082ffd60fbaSKuninori Morimoto 		soc_cleanup_component(component);
1083ffd60fbaSKuninori Morimoto 
1084ffd60fbaSKuninori Morimoto 	return ret;
1085ffd60fbaSKuninori Morimoto }
1086ffd60fbaSKuninori Morimoto 
1087e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
1088589c3563SJarkko Nikula {
1089589c3563SJarkko Nikula 	int err;
1090589c3563SJarkko Nikula 
109152abe6ccSGuennadi Liakhovetski 	if (!dai || !dai->probed || !dai->driver ||
10922eda3cb1SKuninori Morimoto 	    dai->driver->remove_order != order)
10932eda3cb1SKuninori Morimoto 		return;
10942eda3cb1SKuninori Morimoto 
1095dcdab582SKuninori Morimoto 	err = snd_soc_dai_remove(dai);
1096589c3563SJarkko Nikula 	if (err < 0)
1097e60cd14fSLars-Peter Clausen 		dev_err(dai->dev,
1098b0aa88afSMisael Lopez Cruz 			"ASoC: failed to remove %s: %d\n",
1099e60cd14fSLars-Peter Clausen 			dai->name, err);
1100dcdab582SKuninori Morimoto 
1101e60cd14fSLars-Peter Clausen 	dai->probed = 0;
1102b0aa88afSMisael Lopez Cruz }
1103b0aa88afSMisael Lopez Cruz 
1104a7d44f78SKuninori Morimoto static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1105a7d44f78SKuninori Morimoto {
1106a7d44f78SKuninori Morimoto 	int ret;
1107a7d44f78SKuninori Morimoto 
1108a7d44f78SKuninori Morimoto 	if (dai->probed ||
1109a7d44f78SKuninori Morimoto 	    dai->driver->probe_order != order)
1110a7d44f78SKuninori Morimoto 		return 0;
1111a7d44f78SKuninori Morimoto 
1112a7d44f78SKuninori Morimoto 	ret = snd_soc_dai_probe(dai);
1113a7d44f78SKuninori Morimoto 	if (ret < 0) {
1114a7d44f78SKuninori Morimoto 		dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1115a7d44f78SKuninori Morimoto 			dai->name, ret);
1116a7d44f78SKuninori Morimoto 		return ret;
1117a7d44f78SKuninori Morimoto 	}
1118a7d44f78SKuninori Morimoto 
1119a7d44f78SKuninori Morimoto 	dai->probed = 1;
1120a7d44f78SKuninori Morimoto 
1121a7d44f78SKuninori Morimoto 	return 0;
1122a7d44f78SKuninori Morimoto }
1123a7d44f78SKuninori Morimoto 
11249a7c9fe1SKuninori Morimoto static void soc_rtd_free(struct snd_soc_pcm_runtime *rtd); /* remove me */
11254ca47d21SKuninori Morimoto static void soc_remove_link_dais(struct snd_soc_card *card)
1126f0fba2adSLiam Girdwood {
1127e60cd14fSLars-Peter Clausen 	int i;
11280b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
11294ca47d21SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
11304ca47d21SKuninori Morimoto 	int order;
11314ca47d21SKuninori Morimoto 
11324ca47d21SKuninori Morimoto 	for_each_comp_order(order) {
11334ca47d21SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1134f0fba2adSLiam Girdwood 
11359a7c9fe1SKuninori Morimoto 			/* finalize rtd device */
11369a7c9fe1SKuninori Morimoto 			soc_rtd_free(rtd);
113702a06d30SBarry Song 
1138f0fba2adSLiam Girdwood 			/* remove the CODEC DAI */
11390b7990e3SKuninori Morimoto 			for_each_rtd_codec_dai(rtd, i, codec_dai)
11400b7990e3SKuninori Morimoto 				soc_remove_dai(codec_dai, order);
1141f0fba2adSLiam Girdwood 
1142e60cd14fSLars-Peter Clausen 			soc_remove_dai(rtd->cpu_dai, order);
1143f0fba2adSLiam Girdwood 		}
11444ca47d21SKuninori Morimoto 	}
11454ca47d21SKuninori Morimoto }
1146f0fba2adSLiam Girdwood 
1147bc7c16c2SKuninori Morimoto static int soc_probe_link_dais(struct snd_soc_card *card)
1148bc7c16c2SKuninori Morimoto {
1149bc7c16c2SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1150bc7c16c2SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
1151bc7c16c2SKuninori Morimoto 	int i, order, ret;
1152bc7c16c2SKuninori Morimoto 
1153bc7c16c2SKuninori Morimoto 	for_each_comp_order(order) {
1154bc7c16c2SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1155bc7c16c2SKuninori Morimoto 
1156bc7c16c2SKuninori Morimoto 			dev_dbg(card->dev,
1157bc7c16c2SKuninori Morimoto 				"ASoC: probe %s dai link %d late %d\n",
1158bc7c16c2SKuninori Morimoto 				card->name, rtd->num, order);
1159bc7c16c2SKuninori Morimoto 
1160bc7c16c2SKuninori Morimoto 			ret = soc_probe_dai(rtd->cpu_dai, order);
1161bc7c16c2SKuninori Morimoto 			if (ret)
1162bc7c16c2SKuninori Morimoto 				return ret;
1163bc7c16c2SKuninori Morimoto 
1164bc7c16c2SKuninori Morimoto 			/* probe the CODEC DAI */
1165bc7c16c2SKuninori Morimoto 			for_each_rtd_codec_dai(rtd, i, codec_dai) {
1166bc7c16c2SKuninori Morimoto 				ret = soc_probe_dai(codec_dai, order);
1167bc7c16c2SKuninori Morimoto 				if (ret)
1168bc7c16c2SKuninori Morimoto 					return ret;
1169bc7c16c2SKuninori Morimoto 			}
1170bc7c16c2SKuninori Morimoto 		}
1171bc7c16c2SKuninori Morimoto 	}
1172bc7c16c2SKuninori Morimoto 
1173bc7c16c2SKuninori Morimoto 	return 0;
1174bc7c16c2SKuninori Morimoto }
1175bc7c16c2SKuninori Morimoto 
1176b006c0c6SKuninori Morimoto static void soc_remove_link_components(struct snd_soc_card *card)
117762ae68faSStephen Warren {
117861aca564SLars-Peter Clausen 	struct snd_soc_component *component;
1179b006c0c6SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
118090be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
1181b006c0c6SKuninori Morimoto 	int order;
118262ae68faSStephen Warren 
1183b006c0c6SKuninori Morimoto 	for_each_comp_order(order) {
1184b006c0c6SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
118590be711eSKuninori Morimoto 			for_each_rtdcom(rtd, rtdcom) {
118690be711eSKuninori Morimoto 				component = rtdcom->component;
118762ae68faSStephen Warren 
1188b006c0c6SKuninori Morimoto 				if (component->driver->remove_order != order)
1189b006c0c6SKuninori Morimoto 					continue;
1190b006c0c6SKuninori Morimoto 
119161aca564SLars-Peter Clausen 				soc_remove_component(component);
119262ae68faSStephen Warren 			}
119362ae68faSStephen Warren 		}
1194b006c0c6SKuninori Morimoto 	}
1195b006c0c6SKuninori Morimoto }
119662ae68faSStephen Warren 
119762f07a6bSKuninori Morimoto static int soc_probe_link_components(struct snd_soc_card *card)
11986fb03550SKuninori Morimoto {
11996fb03550SKuninori Morimoto 	struct snd_soc_component *component;
120062f07a6bSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
12016fb03550SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
120262f07a6bSKuninori Morimoto 	int ret, order;
12036fb03550SKuninori Morimoto 
120462f07a6bSKuninori Morimoto 	for_each_comp_order(order) {
120562f07a6bSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
12066fb03550SKuninori Morimoto 			for_each_rtdcom(rtd, rtdcom) {
12076fb03550SKuninori Morimoto 				component = rtdcom->component;
12086fb03550SKuninori Morimoto 
120962f07a6bSKuninori Morimoto 				if (component->driver->probe_order != order)
121062f07a6bSKuninori Morimoto 					continue;
121162f07a6bSKuninori Morimoto 
12126fb03550SKuninori Morimoto 				ret = soc_probe_component(card, component);
12136fb03550SKuninori Morimoto 				if (ret < 0)
12146fb03550SKuninori Morimoto 					return ret;
12156fb03550SKuninori Morimoto 			}
12166fb03550SKuninori Morimoto 		}
121762f07a6bSKuninori Morimoto 	}
12186fb03550SKuninori Morimoto 
12196fb03550SKuninori Morimoto 	return 0;
12206fb03550SKuninori Morimoto }
12216fb03550SKuninori Morimoto 
12220671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
12230671fd8eSKuninori Morimoto {
1224f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
12250671fd8eSKuninori Morimoto 
12264ca47d21SKuninori Morimoto 	soc_remove_link_dais(card);
122762ae68faSStephen Warren 
1228b006c0c6SKuninori Morimoto 	soc_remove_link_components(card);
122962ae68faSStephen Warren 
123098061fdbSKuninori Morimoto 	for_each_card_links_safe(card, link, _link) {
1231f8f80361SMengdong Lin 		if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1232f8f80361SMengdong Lin 			dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1233f8f80361SMengdong Lin 				link->name);
1234f8f80361SMengdong Lin 
1235f8f80361SMengdong Lin 		list_del(&link->list);
12360671fd8eSKuninori Morimoto 	}
12370671fd8eSKuninori Morimoto }
12380671fd8eSKuninori Morimoto 
1239923c5e61SMengdong Lin static int soc_init_dai_link(struct snd_soc_card *card,
1240923c5e61SMengdong Lin 			     struct snd_soc_dai_link *link)
1241923c5e61SMengdong Lin {
1242adb76b5bSKuninori Morimoto 	int i;
124334614739SJerome Brunet 	struct snd_soc_dai_link_component *codec, *platform;
1244923c5e61SMengdong Lin 
12453db769f1SKuninori Morimoto 	for_each_link_codecs(link, i, codec) {
1246923c5e61SMengdong Lin 		/*
1247923c5e61SMengdong Lin 		 * Codec must be specified by 1 of name or OF node,
1248923c5e61SMengdong Lin 		 * not both or neither.
1249923c5e61SMengdong Lin 		 */
125034614739SJerome Brunet 		if (!!codec->name == !!codec->of_node) {
1251923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1252923c5e61SMengdong Lin 				link->name);
1253923c5e61SMengdong Lin 			return -EINVAL;
1254923c5e61SMengdong Lin 		}
1255af18b13fSJerome Brunet 
1256923c5e61SMengdong Lin 		/* Codec DAI name must be specified */
12573db769f1SKuninori Morimoto 		if (!codec->dai_name) {
1258923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1259923c5e61SMengdong Lin 				link->name);
1260923c5e61SMengdong Lin 			return -EINVAL;
1261923c5e61SMengdong Lin 		}
1262af18b13fSJerome Brunet 
1263af18b13fSJerome Brunet 		/*
1264af18b13fSJerome Brunet 		 * Defer card registration if codec component is not added to
1265af18b13fSJerome Brunet 		 * component list.
1266af18b13fSJerome Brunet 		 */
1267af18b13fSJerome Brunet 		if (!soc_find_component(codec))
1268af18b13fSJerome Brunet 			return -EPROBE_DEFER;
1269923c5e61SMengdong Lin 	}
1270923c5e61SMengdong Lin 
127134614739SJerome Brunet 	for_each_link_platforms(link, i, platform) {
12721d768989SKuninori Morimoto 		/*
127334614739SJerome Brunet 		 * Platform may be specified by either name or OF node, but it
127434614739SJerome Brunet 		 * can be left unspecified, then no components will be inserted
127534614739SJerome Brunet 		 * in the rtdcom list
12761d768989SKuninori Morimoto 		 */
127734614739SJerome Brunet 		if (!!platform->name == !!platform->of_node) {
1278910fdcabSKuninori Morimoto 			dev_err(card->dev,
127934614739SJerome Brunet 				"ASoC: Neither/both platform name/of_node are set for %s\n",
1280923c5e61SMengdong Lin 				link->name);
1281923c5e61SMengdong Lin 			return -EINVAL;
1282923c5e61SMengdong Lin 		}
12838780cf11SAjit Pandey 
12848780cf11SAjit Pandey 		/*
128534614739SJerome Brunet 		 * Defer card registration if platform component is not added to
128634614739SJerome Brunet 		 * component list.
12878780cf11SAjit Pandey 		 */
128834614739SJerome Brunet 		if (!soc_find_component(platform))
12898780cf11SAjit Pandey 			return -EPROBE_DEFER;
1290923c5e61SMengdong Lin 	}
1291923c5e61SMengdong Lin 
1292923c5e61SMengdong Lin 	/* FIXME */
129308a5841eSKuninori Morimoto 	if (link->num_cpus > 1) {
1294923c5e61SMengdong Lin 		dev_err(card->dev,
129508a5841eSKuninori Morimoto 			"ASoC: multi cpu is not yet supported %s\n",
1296923c5e61SMengdong Lin 			link->name);
1297923c5e61SMengdong Lin 		return -EINVAL;
1298923c5e61SMengdong Lin 	}
1299923c5e61SMengdong Lin 
1300923c5e61SMengdong Lin 	/*
1301923c5e61SMengdong Lin 	 * CPU device may be specified by either name or OF node, but
1302923c5e61SMengdong Lin 	 * can be left unspecified, and will be matched based on DAI
1303923c5e61SMengdong Lin 	 * name alone..
1304923c5e61SMengdong Lin 	 */
130508a5841eSKuninori Morimoto 	if (link->cpus->name && link->cpus->of_node) {
1306923c5e61SMengdong Lin 		dev_err(card->dev,
1307923c5e61SMengdong Lin 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
1308923c5e61SMengdong Lin 			link->name);
1309923c5e61SMengdong Lin 		return -EINVAL;
1310923c5e61SMengdong Lin 	}
13118780cf11SAjit Pandey 
13128780cf11SAjit Pandey 	/*
13138780cf11SAjit Pandey 	 * Defer card registartion if cpu dai component is not added to
13148780cf11SAjit Pandey 	 * component list.
13158780cf11SAjit Pandey 	 */
131608a5841eSKuninori Morimoto 	if ((link->cpus->of_node || link->cpus->name) &&
1317c1e230f0SKuninori Morimoto 	    !soc_find_component(link->cpus))
13188780cf11SAjit Pandey 		return -EPROBE_DEFER;
13198780cf11SAjit Pandey 
1320923c5e61SMengdong Lin 	/*
1321923c5e61SMengdong Lin 	 * At least one of CPU DAI name or CPU device name/node must be
1322923c5e61SMengdong Lin 	 * specified
1323923c5e61SMengdong Lin 	 */
132408a5841eSKuninori Morimoto 	if (!link->cpus->dai_name &&
132508a5841eSKuninori Morimoto 	    !(link->cpus->name || link->cpus->of_node)) {
1326923c5e61SMengdong Lin 		dev_err(card->dev,
1327923c5e61SMengdong Lin 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1328923c5e61SMengdong Lin 			link->name);
1329923c5e61SMengdong Lin 		return -EINVAL;
1330923c5e61SMengdong Lin 	}
1331923c5e61SMengdong Lin 
1332923c5e61SMengdong Lin 	return 0;
1333923c5e61SMengdong Lin }
1334923c5e61SMengdong Lin 
1335ef2e8175SKuninori Morimoto void snd_soc_disconnect_sync(struct device *dev)
1336ef2e8175SKuninori Morimoto {
13372c7b696aSMarcel Ziswiler 	struct snd_soc_component *component =
13382c7b696aSMarcel Ziswiler 			snd_soc_lookup_component(dev, NULL);
1339ef2e8175SKuninori Morimoto 
1340ef2e8175SKuninori Morimoto 	if (!component || !component->card)
1341ef2e8175SKuninori Morimoto 		return;
1342ef2e8175SKuninori Morimoto 
1343ef2e8175SKuninori Morimoto 	snd_card_disconnect_sync(component->card->snd_card);
1344ef2e8175SKuninori Morimoto }
1345df532185SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
1346ef2e8175SKuninori Morimoto 
1347f8f80361SMengdong Lin /**
1348f8f80361SMengdong Lin  * snd_soc_add_dai_link - Add a DAI link dynamically
1349f8f80361SMengdong Lin  * @card: The ASoC card to which the DAI link is added
1350f8f80361SMengdong Lin  * @dai_link: The new DAI link to add
1351f8f80361SMengdong Lin  *
1352f8f80361SMengdong Lin  * This function adds a DAI link to the ASoC card's link list.
1353f8f80361SMengdong Lin  *
1354f8f80361SMengdong Lin  * Note: Topology can use this API to add DAI links when probing the
1355f8f80361SMengdong Lin  * topology component. And machine drivers can still define static
1356f8f80361SMengdong Lin  * DAI links in dai_link array.
1357f8f80361SMengdong Lin  */
1358f8f80361SMengdong Lin int snd_soc_add_dai_link(struct snd_soc_card *card,
1359f8f80361SMengdong Lin 		struct snd_soc_dai_link *dai_link)
1360f8f80361SMengdong Lin {
1361f8f80361SMengdong Lin 	if (dai_link->dobj.type
1362f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1363f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1364f8f80361SMengdong Lin 			dai_link->dobj.type);
1365f8f80361SMengdong Lin 		return -EINVAL;
1366f8f80361SMengdong Lin 	}
1367f8f80361SMengdong Lin 
1368f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
13692c7b696aSMarcel Ziswiler 	/*
13702c7b696aSMarcel Ziswiler 	 * Notify the machine driver for extra initialization
1371d6f220eaSMengdong Lin 	 * on the link created by topology.
1372d6f220eaSMengdong Lin 	 */
1373d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->add_dai_link)
1374d6f220eaSMengdong Lin 		card->add_dai_link(card, dai_link);
1375d6f220eaSMengdong Lin 
13766634e3d6SKuninori Morimoto 	/* see for_each_card_links */
1377f8f80361SMengdong Lin 	list_add_tail(&dai_link->list, &card->dai_link_list);
1378f8f80361SMengdong Lin 
1379f8f80361SMengdong Lin 	return 0;
1380f8f80361SMengdong Lin }
1381f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1382f8f80361SMengdong Lin 
1383f8f80361SMengdong Lin /**
1384f8f80361SMengdong Lin  * snd_soc_remove_dai_link - Remove a DAI link from the list
1385f8f80361SMengdong Lin  * @card: The ASoC card that owns the link
1386f8f80361SMengdong Lin  * @dai_link: The DAI link to remove
1387f8f80361SMengdong Lin  *
1388f8f80361SMengdong Lin  * This function removes a DAI link from the ASoC card's link list.
1389f8f80361SMengdong Lin  *
1390f8f80361SMengdong Lin  * For DAI links previously added by topology, topology should
1391f8f80361SMengdong Lin  * remove them by using the dobj embedded in the link.
1392f8f80361SMengdong Lin  */
1393f8f80361SMengdong Lin void snd_soc_remove_dai_link(struct snd_soc_card *card,
1394f8f80361SMengdong Lin 			     struct snd_soc_dai_link *dai_link)
1395f8f80361SMengdong Lin {
1396f8f80361SMengdong Lin 	if (dai_link->dobj.type
1397f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1398f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1399f8f80361SMengdong Lin 			dai_link->dobj.type);
1400f8f80361SMengdong Lin 		return;
1401f8f80361SMengdong Lin 	}
1402f8f80361SMengdong Lin 
1403f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
14042c7b696aSMarcel Ziswiler 	/*
14052c7b696aSMarcel Ziswiler 	 * Notify the machine driver for extra destruction
1406d6f220eaSMengdong Lin 	 * on the link created by topology.
1407d6f220eaSMengdong Lin 	 */
1408d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->remove_dai_link)
1409d6f220eaSMengdong Lin 		card->remove_dai_link(card, dai_link);
1410d6f220eaSMengdong Lin 
1411c26a8841SKuninori Morimoto 	list_del(&dai_link->list);
1412f8f80361SMengdong Lin }
1413f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1414f0fba2adSLiam Girdwood 
14159a7c9fe1SKuninori Morimoto static void soc_rtd_free(struct snd_soc_pcm_runtime *rtd)
14169a7c9fe1SKuninori Morimoto {
14179a7c9fe1SKuninori Morimoto 	if (rtd->dev_registered) {
14189a7c9fe1SKuninori Morimoto 		/* we don't need to call kfree() for rtd->dev */
14199a7c9fe1SKuninori Morimoto 		device_unregister(rtd->dev);
14209a7c9fe1SKuninori Morimoto 		rtd->dev_registered = 0;
14219a7c9fe1SKuninori Morimoto 	}
14229a7c9fe1SKuninori Morimoto }
14239a7c9fe1SKuninori Morimoto 
1424542694dfSKuninori Morimoto static void soc_rtd_release(struct device *dev)
142536ae1a96SMark Brown {
142636ae1a96SMark Brown 	kfree(dev);
142736ae1a96SMark Brown }
1428f0fba2adSLiam Girdwood 
1429542694dfSKuninori Morimoto static int soc_rtd_init(struct snd_soc_pcm_runtime *rtd, const char *name)
1430503ae5e0SMisael Lopez Cruz {
1431589c3563SJarkko Nikula 	int ret = 0;
1432589c3563SJarkko Nikula 
1433589c3563SJarkko Nikula 	/* register the rtd device */
143436ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
143536ae1a96SMark Brown 	if (!rtd->dev)
143636ae1a96SMark Brown 		return -ENOMEM;
14375f3484acSLars-Peter Clausen 	rtd->dev->parent = rtd->card->dev;
1438542694dfSKuninori Morimoto 	rtd->dev->release = soc_rtd_release;
1439d29697dcSTakashi Iwai 	rtd->dev->groups = soc_dev_attr_groups;
1440f294afedSLars-Peter Clausen 	dev_set_name(rtd->dev, "%s", name);
144136ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
144201d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
144301d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
144401d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
144501d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
14464168ddabSKuninori Morimoto 	ret = device_register(rtd->dev);
1447589c3563SJarkko Nikula 	if (ret < 0) {
1448865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1449865df9cbSChuansheng Liu 		put_device(rtd->dev);
14505f3484acSLars-Peter Clausen 		dev_err(rtd->card->dev,
1451f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1452589c3563SJarkko Nikula 		return ret;
1453589c3563SJarkko Nikula 	}
1454589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1455589c3563SJarkko Nikula 	return 0;
1456589c3563SJarkko Nikula }
1457589c3563SJarkko Nikula 
145825f7b701SArnaud Pouliquen static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
145925f7b701SArnaud Pouliquen 				struct snd_soc_pcm_runtime *rtd)
146025f7b701SArnaud Pouliquen {
146125f7b701SArnaud Pouliquen 	int i, ret = 0;
146225f7b701SArnaud Pouliquen 
146325f7b701SArnaud Pouliquen 	for (i = 0; i < num_dais; ++i) {
146425f7b701SArnaud Pouliquen 		struct snd_soc_dai_driver *drv = dais[i]->driver;
146525f7b701SArnaud Pouliquen 
1466de17f14eSRohit kumar 		if (drv->pcm_new)
146725f7b701SArnaud Pouliquen 			ret = drv->pcm_new(rtd, dais[i]);
146825f7b701SArnaud Pouliquen 		if (ret < 0) {
146925f7b701SArnaud Pouliquen 			dev_err(dais[i]->dev,
147025f7b701SArnaud Pouliquen 				"ASoC: Failed to bind %s with pcm device\n",
147125f7b701SArnaud Pouliquen 				dais[i]->name);
147225f7b701SArnaud Pouliquen 			return ret;
147325f7b701SArnaud Pouliquen 		}
147425f7b701SArnaud Pouliquen 	}
147525f7b701SArnaud Pouliquen 
147625f7b701SArnaud Pouliquen 	return 0;
147725f7b701SArnaud Pouliquen }
147825f7b701SArnaud Pouliquen 
1479c4b46982SKuninori Morimoto static int soc_link_init(struct snd_soc_card *card,
1480c4b46982SKuninori Morimoto 			 struct snd_soc_pcm_runtime *rtd)
1481c4b46982SKuninori Morimoto {
1482c4b46982SKuninori Morimoto 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1483c4b46982SKuninori Morimoto 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1484c4b46982SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
1485c4b46982SKuninori Morimoto 	struct snd_soc_component *component;
1486c4b46982SKuninori Morimoto 	int ret, num;
1487c4b46982SKuninori Morimoto 
1488c4b46982SKuninori Morimoto 	/* set default power off timeout */
1489c4b46982SKuninori Morimoto 	rtd->pmdown_time = pmdown_time;
14900168bf0dSLiam Girdwood 
14915f3484acSLars-Peter Clausen 	/* do machine specific initialization */
14925f3484acSLars-Peter Clausen 	if (dai_link->init) {
14935f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
14945f3484acSLars-Peter Clausen 		if (ret < 0) {
14955f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
14965f3484acSLars-Peter Clausen 				dai_link->name, ret);
14975f3484acSLars-Peter Clausen 			return ret;
14985f3484acSLars-Peter Clausen 		}
14995f3484acSLars-Peter Clausen 	}
15005f3484acSLars-Peter Clausen 
150140aa5383SRicard Wanderlof 	if (dai_link->dai_fmt) {
150240aa5383SRicard Wanderlof 		ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
150340aa5383SRicard Wanderlof 		if (ret)
150440aa5383SRicard Wanderlof 			return ret;
150540aa5383SRicard Wanderlof 	}
1506a5053a8eSKuninori Morimoto 
1507542694dfSKuninori Morimoto 	ret = soc_rtd_init(rtd, dai_link->name);
1508589c3563SJarkko Nikula 	if (ret)
1509f0fba2adSLiam Girdwood 		return ret;
1510f0fba2adSLiam Girdwood 
15115f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
15122e55b90aSLars-Peter Clausen 	soc_dpcm_debugfs_add(rtd);
15135f3484acSLars-Peter Clausen 
1514a655de80SLiam Girdwood 	num = rtd->num;
1515a655de80SLiam Girdwood 
1516a655de80SLiam Girdwood 	/*
1517a655de80SLiam Girdwood 	 * most drivers will register their PCMs using DAI link ordering but
1518a655de80SLiam Girdwood 	 * topology based drivers can use the DAI link id field to set PCM
1519a655de80SLiam Girdwood 	 * device number and then use rtd + a base offset of the BEs.
1520a655de80SLiam Girdwood 	 */
1521a655de80SLiam Girdwood 	for_each_rtdcom(rtd, rtdcom) {
1522a655de80SLiam Girdwood 		component = rtdcom->component;
1523a655de80SLiam Girdwood 
1524a655de80SLiam Girdwood 		if (!component->driver->use_dai_pcm_id)
1525a655de80SLiam Girdwood 			continue;
1526a655de80SLiam Girdwood 
1527a655de80SLiam Girdwood 		if (rtd->dai_link->no_pcm)
1528a655de80SLiam Girdwood 			num += component->driver->be_pcm_base;
1529a655de80SLiam Girdwood 		else
1530a655de80SLiam Girdwood 			num = rtd->dai_link->id;
1531a655de80SLiam Girdwood 	}
1532a655de80SLiam Girdwood 
1533b423c420SKuninori Morimoto 	/* create compress_device if possible */
1534b423c420SKuninori Morimoto 	ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1535b423c420SKuninori Morimoto 	if (ret != -ENOTSUPP) {
1536b423c420SKuninori Morimoto 		if (ret < 0)
1537f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
15381245b700SNamarta Kohli 					 dai_link->stream_name);
15391245b700SNamarta Kohli 		return ret;
15401245b700SNamarta Kohli 	}
1541b423c420SKuninori Morimoto 
1542f0fba2adSLiam Girdwood 	/* create the pcm */
1543a655de80SLiam Girdwood 	ret = soc_new_pcm(rtd, num);
1544f0fba2adSLiam Girdwood 	if (ret < 0) {
1545f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
15460837fc62SFabio Estevam 			dai_link->stream_name, ret);
1547f0fba2adSLiam Girdwood 		return ret;
1548f0fba2adSLiam Girdwood 	}
154925f7b701SArnaud Pouliquen 	ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
155025f7b701SArnaud Pouliquen 	if (ret < 0)
155125f7b701SArnaud Pouliquen 		return ret;
155225f7b701SArnaud Pouliquen 	ret = soc_link_dai_pcm_new(rtd->codec_dais,
155325f7b701SArnaud Pouliquen 				   rtd->num_codecs, rtd);
155425f7b701SArnaud Pouliquen 	return ret;
1555f0fba2adSLiam Girdwood }
1556f0fba2adSLiam Girdwood 
1557e8fbd250SKuninori Morimoto static void soc_unbind_aux_dev(struct snd_soc_card *card)
15584893a2ebSKuninori Morimoto {
1559e8fbd250SKuninori Morimoto 	struct snd_soc_component *component, *_component;
1560e8fbd250SKuninori Morimoto 
1561e8fbd250SKuninori Morimoto 	for_each_card_auxs_safe(card, component, _component) {
15624893a2ebSKuninori Morimoto 		component->init = NULL;
15634893a2ebSKuninori Morimoto 		list_del(&component->card_aux_list);
15644893a2ebSKuninori Morimoto 	}
1565e8fbd250SKuninori Morimoto }
15664893a2ebSKuninori Morimoto 
1567bee886f1SKuninori Morimoto static int soc_bind_aux_dev(struct snd_soc_card *card)
1568b19e6e7bSMark Brown {
1569f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1570bee886f1SKuninori Morimoto 	struct snd_soc_aux_dev *aux;
1571bee886f1SKuninori Morimoto 	int i;
15723ca041edSSebastian Reichel 
1573bee886f1SKuninori Morimoto 	for_each_card_pre_auxs(card, i, aux) {
1574f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1575bee886f1SKuninori Morimoto 		component = soc_find_component(&aux->dlc);
1576f2ed6b07SMengdong Lin 		if (!component)
15773dc29b8bSKuninori Morimoto 			return -EPROBE_DEFER;
15783ca041edSSebastian Reichel 
1579bee886f1SKuninori Morimoto 		component->init = aux->init;
1580c2b71c71SKuninori Morimoto 		/* see for_each_card_auxs */
1581d2e3a135SSylwester Nawrocki 		list_add(&component->card_aux_list, &card->aux_comp_list);
1582bee886f1SKuninori Morimoto 	}
1583f2ed6b07SMengdong Lin 	return 0;
1584b19e6e7bSMark Brown }
1585b19e6e7bSMark Brown 
1586f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1587f2ed6b07SMengdong Lin {
1588991454e1SKuninori Morimoto 	struct snd_soc_component *comp;
1589f2ed6b07SMengdong Lin 	int order;
1590f2ed6b07SMengdong Lin 	int ret;
1591f2ed6b07SMengdong Lin 
15921a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1593c2b71c71SKuninori Morimoto 		for_each_card_auxs(card, comp) {
1594f2ed6b07SMengdong Lin 			if (comp->driver->probe_order == order) {
1595f2ed6b07SMengdong Lin 				ret = soc_probe_component(card,	comp);
1596f2ed6b07SMengdong Lin 				if (ret < 0) {
1597f2ed6b07SMengdong Lin 					dev_err(card->dev,
1598f2ed6b07SMengdong Lin 						"ASoC: failed to probe aux component %s %d\n",
1599f2ed6b07SMengdong Lin 						comp->name, ret);
1600f2ed6b07SMengdong Lin 					return ret;
1601f2ed6b07SMengdong Lin 				}
1602f2ed6b07SMengdong Lin 			}
1603f2ed6b07SMengdong Lin 		}
1604f2ed6b07SMengdong Lin 	}
160565d9361fSLars-Peter Clausen 
160644c69bb1SLars-Peter Clausen 	return 0;
16073ca041edSSebastian Reichel }
16082eea392dSJarkko Nikula 
1609f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
161044c69bb1SLars-Peter Clausen {
1611f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1612f2ed6b07SMengdong Lin 	int order;
161344c69bb1SLars-Peter Clausen 
16141a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1615c2b71c71SKuninori Morimoto 		for_each_card_auxs_safe(card, comp, _comp) {
1616e8fbd250SKuninori Morimoto 			if (comp->driver->remove_order == order)
1617f2ed6b07SMengdong Lin 				soc_remove_component(comp);
16185f3484acSLars-Peter Clausen 		}
16192eea392dSJarkko Nikula 	}
16202eea392dSJarkko Nikula }
16212eea392dSJarkko Nikula 
1622ce64c8b9SLars-Peter Clausen /**
1623ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1624ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1625ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1626ce64c8b9SLars-Peter Clausen  *
1627ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1628ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1629ce64c8b9SLars-Peter Clausen  *
1630ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1631ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1632ce64c8b9SLars-Peter Clausen  *
1633ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1634ce64c8b9SLars-Peter Clausen  */
1635ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1636ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1637ce64c8b9SLars-Peter Clausen {
1638ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
16390b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1640ce64c8b9SLars-Peter Clausen 	unsigned int i;
1641ce64c8b9SLars-Peter Clausen 	int ret;
1642ce64c8b9SLars-Peter Clausen 
16430b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1644ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1645ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1646ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1647ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1648ce64c8b9SLars-Peter Clausen 			return ret;
1649ce64c8b9SLars-Peter Clausen 		}
1650ce64c8b9SLars-Peter Clausen 	}
1651ce64c8b9SLars-Peter Clausen 
16522c7b696aSMarcel Ziswiler 	/*
16532c7b696aSMarcel Ziswiler 	 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
16542c7b696aSMarcel Ziswiler 	 * the component which has non_legacy_dai_naming is Codec
16552c7b696aSMarcel Ziswiler 	 */
1656999f7f5aSKuninori Morimoto 	if (cpu_dai->component->driver->non_legacy_dai_naming) {
1657ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1658ce64c8b9SLars-Peter Clausen 
1659ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1660ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1661ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1662ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1663ce64c8b9SLars-Peter Clausen 			break;
1664ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1665ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1666ce64c8b9SLars-Peter Clausen 			break;
1667ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1668ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1669ce64c8b9SLars-Peter Clausen 			break;
1670ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1671ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1672ce64c8b9SLars-Peter Clausen 			break;
1673ce64c8b9SLars-Peter Clausen 		}
1674ce64c8b9SLars-Peter Clausen 
1675ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1676ce64c8b9SLars-Peter Clausen 	}
1677ce64c8b9SLars-Peter Clausen 
1678ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1679ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1680ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1681ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1682ce64c8b9SLars-Peter Clausen 		return ret;
1683ce64c8b9SLars-Peter Clausen 	}
1684ce64c8b9SLars-Peter Clausen 
1685ce64c8b9SLars-Peter Clausen 	return 0;
1686ce64c8b9SLars-Peter Clausen }
1687ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1688ce64c8b9SLars-Peter Clausen 
16891f5a4535STakashi Iwai #ifdef CONFIG_DMI
16902c7b696aSMarcel Ziswiler /*
16912c7b696aSMarcel Ziswiler  * Trim special characters, and replace '-' with '_' since '-' is used to
1692345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1693345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1694345233d7SLiam Girdwood  */
1695345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1696345233d7SLiam Girdwood {
1697345233d7SLiam Girdwood 	int i, j = 0;
1698345233d7SLiam Girdwood 
1699345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1700345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1701345233d7SLiam Girdwood 		    || (name[i] == '_'))
1702345233d7SLiam Girdwood 			name[j++] = name[i];
1703345233d7SLiam Girdwood 		else if (name[i] == '-')
1704345233d7SLiam Girdwood 			name[j++] = '_';
1705345233d7SLiam Girdwood 	}
1706345233d7SLiam Girdwood 
1707345233d7SLiam Girdwood 	name[j] = '\0';
1708345233d7SLiam Girdwood }
1709345233d7SLiam Girdwood 
17102c7b696aSMarcel Ziswiler /*
17112c7b696aSMarcel Ziswiler  * Check if a DMI field is valid, i.e. not containing any string
171298faf436SMengdong Lin  * in the black list.
171398faf436SMengdong Lin  */
171498faf436SMengdong Lin static int is_dmi_valid(const char *field)
171598faf436SMengdong Lin {
171698faf436SMengdong Lin 	int i = 0;
171798faf436SMengdong Lin 
171898faf436SMengdong Lin 	while (dmi_blacklist[i]) {
171998faf436SMengdong Lin 		if (strstr(field, dmi_blacklist[i]))
172098faf436SMengdong Lin 			return 0;
172198faf436SMengdong Lin 		i++;
172246b5a4d2SWu Fengguang 	}
172398faf436SMengdong Lin 
172498faf436SMengdong Lin 	return 1;
172598faf436SMengdong Lin }
172698faf436SMengdong Lin 
1727345233d7SLiam Girdwood /**
1728345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
1729345233d7SLiam Girdwood  * @card: The card to register DMI names
1730345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
1731345233d7SLiam Girdwood  *
1732345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
1733345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
1734345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
1735345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
1736345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
1737345233d7SLiam Girdwood  * as the sound card long name, in the format of
1738345233d7SLiam Girdwood  * "vendor-product-version-board"
1739345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
1740345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
1741345233d7SLiam Girdwood  * (UCM) configurations for the card.
1742345233d7SLiam Girdwood  *
1743345233d7SLiam Girdwood  * Possible card long names may be:
1744345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
1745345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1746345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1747345233d7SLiam Girdwood  *
1748345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
1749345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
1750345233d7SLiam Girdwood  *
1751345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
1752345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
1753345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
1754345233d7SLiam Girdwood  * charactors like SPACE.
1755345233d7SLiam Girdwood  *
1756345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
1757345233d7SLiam Girdwood  */
1758345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1759345233d7SLiam Girdwood {
1760345233d7SLiam Girdwood 	const char *vendor, *product, *product_version, *board;
1761345233d7SLiam Girdwood 	size_t longname_buf_size = sizeof(card->snd_card->longname);
1762345233d7SLiam Girdwood 	size_t len;
1763345233d7SLiam Girdwood 
1764345233d7SLiam Girdwood 	if (card->long_name)
1765345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
1766345233d7SLiam Girdwood 
1767345233d7SLiam Girdwood 	/* make up dmi long name as: vendor.product.version.board */
1768345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
176998faf436SMengdong Lin 	if (!vendor || !is_dmi_valid(vendor)) {
1770345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1771345233d7SLiam Girdwood 		return 0;
1772345233d7SLiam Girdwood 	}
1773345233d7SLiam Girdwood 
1774345233d7SLiam Girdwood 	snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1775345233d7SLiam Girdwood 			 "%s", vendor);
1776345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
1777345233d7SLiam Girdwood 
1778345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
177998faf436SMengdong Lin 	if (product && is_dmi_valid(product)) {
1780345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1781345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1782345233d7SLiam Girdwood 			 longname_buf_size - len,
1783345233d7SLiam Girdwood 			 "-%s", product);
1784345233d7SLiam Girdwood 
1785345233d7SLiam Girdwood 		len++;	/* skip the separator "-" */
1786345233d7SLiam Girdwood 		if (len < longname_buf_size)
1787345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1788345233d7SLiam Girdwood 
17892c7b696aSMarcel Ziswiler 		/*
17902c7b696aSMarcel Ziswiler 		 * some vendors like Lenovo may only put a self-explanatory
1791345233d7SLiam Girdwood 		 * name in the product version field
1792345233d7SLiam Girdwood 		 */
1793345233d7SLiam Girdwood 		product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
179498faf436SMengdong Lin 		if (product_version && is_dmi_valid(product_version)) {
1795345233d7SLiam Girdwood 			len = strlen(card->dmi_longname);
1796345233d7SLiam Girdwood 			snprintf(card->dmi_longname + len,
1797345233d7SLiam Girdwood 				 longname_buf_size - len,
1798345233d7SLiam Girdwood 				 "-%s", product_version);
1799345233d7SLiam Girdwood 
1800345233d7SLiam Girdwood 			len++;
1801345233d7SLiam Girdwood 			if (len < longname_buf_size)
1802345233d7SLiam Girdwood 				cleanup_dmi_name(card->dmi_longname + len);
1803345233d7SLiam Girdwood 		}
1804345233d7SLiam Girdwood 	}
1805345233d7SLiam Girdwood 
1806345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
180798faf436SMengdong Lin 	if (board && is_dmi_valid(board)) {
1808345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1809345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1810345233d7SLiam Girdwood 			 longname_buf_size - len,
1811345233d7SLiam Girdwood 			 "-%s", board);
1812345233d7SLiam Girdwood 
1813345233d7SLiam Girdwood 		len++;
1814345233d7SLiam Girdwood 		if (len < longname_buf_size)
1815345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1816345233d7SLiam Girdwood 	} else if (!product) {
1817345233d7SLiam Girdwood 		/* fall back to using legacy name */
1818345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1819345233d7SLiam Girdwood 		return 0;
1820345233d7SLiam Girdwood 	}
1821345233d7SLiam Girdwood 
1822345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
1823345233d7SLiam Girdwood 	if (flavour) {
1824345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1825345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1826345233d7SLiam Girdwood 			 longname_buf_size - len,
1827345233d7SLiam Girdwood 			 "-%s", flavour);
1828345233d7SLiam Girdwood 
1829345233d7SLiam Girdwood 		len++;
1830345233d7SLiam Girdwood 		if (len < longname_buf_size)
1831345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1832345233d7SLiam Girdwood 	}
1833345233d7SLiam Girdwood 
1834345233d7SLiam Girdwood 	/* set the card long name */
1835345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
1836345233d7SLiam Girdwood 
1837345233d7SLiam Girdwood 	return 0;
1838345233d7SLiam Girdwood }
1839345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
18401f5a4535STakashi Iwai #endif /* CONFIG_DMI */
1841345233d7SLiam Girdwood 
1842a655de80SLiam Girdwood static void soc_check_tplg_fes(struct snd_soc_card *card)
1843a655de80SLiam Girdwood {
1844a655de80SLiam Girdwood 	struct snd_soc_component *component;
1845a655de80SLiam Girdwood 	const struct snd_soc_component_driver *comp_drv;
1846a655de80SLiam Girdwood 	struct snd_soc_dai_link *dai_link;
1847a655de80SLiam Girdwood 	int i;
1848a655de80SLiam Girdwood 
1849368dee94SKuninori Morimoto 	for_each_component(component) {
1850a655de80SLiam Girdwood 
1851a655de80SLiam Girdwood 		/* does this component override FEs ? */
1852a655de80SLiam Girdwood 		if (!component->driver->ignore_machine)
1853a655de80SLiam Girdwood 			continue;
1854a655de80SLiam Girdwood 
1855a655de80SLiam Girdwood 		/* for this machine ? */
1856e194098bSPierre-Louis Bossart 		if (!strcmp(component->driver->ignore_machine,
1857a655de80SLiam Girdwood 			    card->dev->driver->name))
1858e194098bSPierre-Louis Bossart 			goto match;
1859e194098bSPierre-Louis Bossart 		if (strcmp(component->driver->ignore_machine,
1860e194098bSPierre-Louis Bossart 			   dev_name(card->dev)))
1861a655de80SLiam Girdwood 			continue;
1862e194098bSPierre-Louis Bossart match:
1863a655de80SLiam Girdwood 		/* machine matches, so override the rtd data */
18647fe072b4SKuninori Morimoto 		for_each_card_prelinks(card, i, dai_link) {
1865a655de80SLiam Girdwood 
1866a655de80SLiam Girdwood 			/* ignore this FE */
1867a655de80SLiam Girdwood 			if (dai_link->dynamic) {
1868a655de80SLiam Girdwood 				dai_link->ignore = true;
1869a655de80SLiam Girdwood 				continue;
1870a655de80SLiam Girdwood 			}
1871a655de80SLiam Girdwood 
1872a655de80SLiam Girdwood 			dev_info(card->dev, "info: override FE DAI link %s\n",
1873a655de80SLiam Girdwood 				 card->dai_link[i].name);
1874a655de80SLiam Girdwood 
1875a655de80SLiam Girdwood 			/* override platform component */
1876adb76b5bSKuninori Morimoto 			if (!dai_link->platforms) {
1877daecf46eSKuninori Morimoto 				dev_err(card->dev, "init platform error");
1878daecf46eSKuninori Morimoto 				continue;
1879daecf46eSKuninori Morimoto 			}
1880910fdcabSKuninori Morimoto 			dai_link->platforms->name = component->name;
1881a655de80SLiam Girdwood 
1882a655de80SLiam Girdwood 			/* convert non BE into BE */
1883a655de80SLiam Girdwood 			dai_link->no_pcm = 1;
1884a655de80SLiam Girdwood 
1885a655de80SLiam Girdwood 			/* override any BE fixups */
1886a655de80SLiam Girdwood 			dai_link->be_hw_params_fixup =
1887a655de80SLiam Girdwood 				component->driver->be_hw_params_fixup;
1888a655de80SLiam Girdwood 
18892c7b696aSMarcel Ziswiler 			/*
18902c7b696aSMarcel Ziswiler 			 * most BE links don't set stream name, so set it to
1891a655de80SLiam Girdwood 			 * dai link name if it's NULL to help bind widgets.
1892a655de80SLiam Girdwood 			 */
1893a655de80SLiam Girdwood 			if (!dai_link->stream_name)
1894a655de80SLiam Girdwood 				dai_link->stream_name = dai_link->name;
1895a655de80SLiam Girdwood 		}
1896a655de80SLiam Girdwood 
1897a655de80SLiam Girdwood 		/* Inform userspace we are using alternate topology */
1898a655de80SLiam Girdwood 		if (component->driver->topology_name_prefix) {
1899a655de80SLiam Girdwood 
1900a655de80SLiam Girdwood 			/* topology shortname created? */
1901a655de80SLiam Girdwood 			if (!card->topology_shortname_created) {
1902a655de80SLiam Girdwood 				comp_drv = component->driver;
1903a655de80SLiam Girdwood 
1904a655de80SLiam Girdwood 				snprintf(card->topology_shortname, 32, "%s-%s",
1905a655de80SLiam Girdwood 					 comp_drv->topology_name_prefix,
1906a655de80SLiam Girdwood 					 card->name);
1907a655de80SLiam Girdwood 				card->topology_shortname_created = true;
1908a655de80SLiam Girdwood 			}
1909a655de80SLiam Girdwood 
1910a655de80SLiam Girdwood 			/* use topology shortname */
1911a655de80SLiam Girdwood 			card->name = card->topology_shortname;
1912a655de80SLiam Girdwood 		}
1913a655de80SLiam Girdwood 	}
1914a655de80SLiam Girdwood }
1915a655de80SLiam Girdwood 
1916a4de83a3SKuninori Morimoto static void soc_cleanup_card_resources(struct snd_soc_card *card)
191753e947a0SKuninori Morimoto {
191853e947a0SKuninori Morimoto 	/* free the ALSA card at first; this syncs with pending operations */
191929040d1aSKuninori Morimoto 	if (card->snd_card) {
192053e947a0SKuninori Morimoto 		snd_card_free(card->snd_card);
192129040d1aSKuninori Morimoto 		card->snd_card = NULL;
192229040d1aSKuninori Morimoto 	}
192353e947a0SKuninori Morimoto 
192453e947a0SKuninori Morimoto 	/* remove and free each DAI */
192553e947a0SKuninori Morimoto 	soc_remove_dai_links(card);
192653e947a0SKuninori Morimoto 	soc_remove_pcm_runtimes(card);
192753e947a0SKuninori Morimoto 
192853e947a0SKuninori Morimoto 	/* remove auxiliary devices */
192953e947a0SKuninori Morimoto 	soc_remove_aux_devices(card);
1930e8fbd250SKuninori Morimoto 	soc_unbind_aux_dev(card);
193153e947a0SKuninori Morimoto 
193253e947a0SKuninori Morimoto 	snd_soc_dapm_free(&card->dapm);
193353e947a0SKuninori Morimoto 	soc_cleanup_card_debugfs(card);
193453e947a0SKuninori Morimoto 
193553e947a0SKuninori Morimoto 	/* remove the card */
193653e947a0SKuninori Morimoto 	if (card->remove)
193753e947a0SKuninori Morimoto 		card->remove(card);
193853e947a0SKuninori Morimoto }
193953e947a0SKuninori Morimoto 
1940b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1941f0fba2adSLiam Girdwood {
19421a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
194361b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
1944c7e73774SKuninori Morimoto 	int ret, i;
194596dd3622SMark Brown 
194634e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
194770fc5373STzung-Bi Shih 	for_each_card_prelinks(card, i, dai_link) {
194870fc5373STzung-Bi Shih 		ret = soc_init_dai_link(card, dai_link);
194970fc5373STzung-Bi Shih 		if (ret) {
195070fc5373STzung-Bi Shih 			dev_err(card->dev, "ASoC: failed to init link %s: %d\n",
195170fc5373STzung-Bi Shih 				dai_link->name, ret);
195270fc5373STzung-Bi Shih 			mutex_unlock(&client_mutex);
195370fc5373STzung-Bi Shih 			return ret;
195470fc5373STzung-Bi Shih 		}
195570fc5373STzung-Bi Shih 	}
195601b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1957f0fba2adSLiam Girdwood 
195895c267ddSKuninori Morimoto 	snd_soc_dapm_init(&card->dapm, card, NULL);
195953e947a0SKuninori Morimoto 
1960a655de80SLiam Girdwood 	/* check whether any platform is ignore machine FE and using topology */
1961a655de80SLiam Girdwood 	soc_check_tplg_fes(card);
1962a655de80SLiam Girdwood 
1963b19e6e7bSMark Brown 	/* bind DAIs */
19647fe072b4SKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link) {
19657fe072b4SKuninori Morimoto 		ret = soc_bind_dai_link(card, dai_link);
1966b19e6e7bSMark Brown 		if (ret != 0)
196753e947a0SKuninori Morimoto 			goto probe_end;
1968db2a4165SFrank Mandarino 	}
1969db2a4165SFrank Mandarino 
197044c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
1971bee886f1SKuninori Morimoto 	ret = soc_bind_aux_dev(card);
1972bee886f1SKuninori Morimoto 	if (ret < 0)
197353e947a0SKuninori Morimoto 		goto probe_end;
1974db2a4165SFrank Mandarino 
1975f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
19765b99a0aaSKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link) {
19775b99a0aaSKuninori Morimoto 		ret = snd_soc_add_dai_link(card, dai_link);
19785b99a0aaSKuninori Morimoto 		if (ret < 0)
19795b99a0aaSKuninori Morimoto 			goto probe_end;
19805b99a0aaSKuninori Morimoto 	}
1981f8f80361SMengdong Lin 
1982f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1983102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1984f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1985f0fba2adSLiam Girdwood 	if (ret < 0) {
198610e8aa9aSMichał Mirosław 		dev_err(card->dev,
198710e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
198810e8aa9aSMichał Mirosław 			card->name, ret);
198953e947a0SKuninori Morimoto 		goto probe_end;
1990db2a4165SFrank Mandarino 	}
1991db2a4165SFrank Mandarino 
19920757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
19930757d834SLars-Peter Clausen 
1994b3da4251SKuninori Morimoto 	soc_resume_init(card);
19956ed25978SAndy Green 
1996b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
19979a841ebbSMark Brown 					card->num_dapm_widgets);
1998b8ba3b57SKuninori Morimoto 	if (ret < 0)
1999b8ba3b57SKuninori Morimoto 		goto probe_end;
20009a841ebbSMark Brown 
2001b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2002f23e860eSNicolin Chen 					card->num_of_dapm_widgets);
2003b8ba3b57SKuninori Morimoto 	if (ret < 0)
2004b8ba3b57SKuninori Morimoto 		goto probe_end;
2005f23e860eSNicolin Chen 
2006f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
2007f0fba2adSLiam Girdwood 	if (card->probe) {
2008e7361ec4SMark Brown 		ret = card->probe(card);
2009f0fba2adSLiam Girdwood 		if (ret < 0)
201053e947a0SKuninori Morimoto 			goto probe_end;
2011f0fba2adSLiam Girdwood 	}
2012f0fba2adSLiam Girdwood 
201362ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
201462f07a6bSKuninori Morimoto 	ret = soc_probe_link_components(card);
201562ae68faSStephen Warren 	if (ret < 0) {
2016f110bfc7SLiam Girdwood 		dev_err(card->dev,
201762f07a6bSKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
201853e947a0SKuninori Morimoto 		goto probe_end;
201962ae68faSStephen Warren 	}
202062ae68faSStephen Warren 
2021f2ed6b07SMengdong Lin 	/* probe auxiliary components */
2022f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
2023f2ed6b07SMengdong Lin 	if (ret < 0)
202453e947a0SKuninori Morimoto 		goto probe_end;
2025f2ed6b07SMengdong Lin 
20262c7b696aSMarcel Ziswiler 	/*
20272c7b696aSMarcel Ziswiler 	 * Find new DAI links added during probing components and bind them.
202861b0088bSMengdong Lin 	 * Components with topology may bring new DAIs and DAI links.
202961b0088bSMengdong Lin 	 */
203098061fdbSKuninori Morimoto 	for_each_card_links(card, dai_link) {
203161b0088bSMengdong Lin 		if (soc_is_dai_link_bound(card, dai_link))
203261b0088bSMengdong Lin 			continue;
203361b0088bSMengdong Lin 
203461b0088bSMengdong Lin 		ret = soc_init_dai_link(card, dai_link);
203561b0088bSMengdong Lin 		if (ret)
203653e947a0SKuninori Morimoto 			goto probe_end;
203761b0088bSMengdong Lin 		ret = soc_bind_dai_link(card, dai_link);
203861b0088bSMengdong Lin 		if (ret)
203953e947a0SKuninori Morimoto 			goto probe_end;
204061b0088bSMengdong Lin 	}
204161b0088bSMengdong Lin 
204262ae68faSStephen Warren 	/* probe all DAI links on this card */
2043c7e73774SKuninori Morimoto 	ret = soc_probe_link_dais(card);
2044fe3e78e0SMark Brown 	if (ret < 0) {
2045f110bfc7SLiam Girdwood 		dev_err(card->dev,
2046c7e73774SKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
204753e947a0SKuninori Morimoto 		goto probe_end;
2048fe3e78e0SMark Brown 	}
2049fe3e78e0SMark Brown 
2050c4b46982SKuninori Morimoto 	for_each_card_rtds(card, rtd)
2051c4b46982SKuninori Morimoto 		soc_link_init(card, rtd);
2052c4b46982SKuninori Morimoto 
2053888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
2054b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
2055888df395SMark Brown 
20569b98c7c2SKuninori Morimoto 	ret = snd_soc_add_card_controls(card, card->controls,
20572c7b696aSMarcel Ziswiler 					card->num_controls);
20589b98c7c2SKuninori Morimoto 	if (ret < 0)
20599b98c7c2SKuninori Morimoto 		goto probe_end;
2060b7af1dafSMark Brown 
2061daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2062b8ad29deSMark Brown 				      card->num_dapm_routes);
2063daa480bdSKuninori Morimoto 	if (ret < 0)
2064daa480bdSKuninori Morimoto 		goto probe_end;
2065b8ad29deSMark Brown 
2066daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2067f23e860eSNicolin Chen 				      card->num_of_dapm_routes);
2068daa480bdSKuninori Morimoto 	if (ret < 0)
2069daa480bdSKuninori Morimoto 		goto probe_end;
207075d9ac46SMark Brown 
2071861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
2072861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
2073861886d3STakashi Iwai 
2074f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
2075fe3e78e0SMark Brown 		 "%s", card->name);
2076f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
207722de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
2078f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2079f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
2080f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2081f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
2082f0e8ed85SMark Brown 		case '_':
2083f0e8ed85SMark Brown 		case '-':
2084f0e8ed85SMark Brown 		case '\0':
2085f0e8ed85SMark Brown 			break;
2086f0e8ed85SMark Brown 		default:
2087f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
2088f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
2089f0e8ed85SMark Brown 			break;
2090f0e8ed85SMark Brown 		}
2091f0e8ed85SMark Brown 	}
2092fe3e78e0SMark Brown 
209328e9ad92SMark Brown 	if (card->late_probe) {
209428e9ad92SMark Brown 		ret = card->late_probe(card);
209528e9ad92SMark Brown 		if (ret < 0) {
2096f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
209728e9ad92SMark Brown 				card->name, ret);
209853e947a0SKuninori Morimoto 			goto probe_end;
209928e9ad92SMark Brown 		}
210028e9ad92SMark Brown 	}
210128e9ad92SMark Brown 
2102824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
21038c193b8dSLars-Peter Clausen 
2104f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2105fe3e78e0SMark Brown 	if (ret < 0) {
2106f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2107f110bfc7SLiam Girdwood 				ret);
210853e947a0SKuninori Morimoto 		goto probe_end;
2109fe3e78e0SMark Brown 	}
2110fe3e78e0SMark Brown 
2111435c5e25SMark Brown 	card->instantiated = 1;
2112882eab6cSTzung-Bi Shih 	dapm_mark_endpoints_dirty(card);
21134f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2114b19e6e7bSMark Brown 
211553e947a0SKuninori Morimoto probe_end:
211653e947a0SKuninori Morimoto 	if (ret < 0)
211753e947a0SKuninori Morimoto 		soc_cleanup_card_resources(card);
2118db2a4165SFrank Mandarino 
2119f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
212034e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2121db2a4165SFrank Mandarino 
2122b19e6e7bSMark Brown 	return ret;
2123435c5e25SMark Brown }
2124435c5e25SMark Brown 
2125435c5e25SMark Brown /* probes a new socdev */
2126435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2127435c5e25SMark Brown {
2128f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2129435c5e25SMark Brown 
213070a7ca34SVinod Koul 	/*
213170a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
213270a7ca34SVinod Koul 	 * we should not be here in that case so ret error
213370a7ca34SVinod Koul 	 */
213470a7ca34SVinod Koul 	if (!card)
213570a7ca34SVinod Koul 		return -EINVAL;
213670a7ca34SVinod Koul 
2137fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2138f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2139fe4085e8SMark Brown 		 card->name);
2140fe4085e8SMark Brown 
2141435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2142435c5e25SMark Brown 	card->dev = &pdev->dev;
2143f0fba2adSLiam Girdwood 
214428d528c8SMark Brown 	return snd_soc_register_card(card);
2145435c5e25SMark Brown }
2146435c5e25SMark Brown 
2147b0e26485SVinod Koul /* removes a socdev */
2148b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2149b0e26485SVinod Koul {
2150b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2151b0e26485SVinod Koul 
2152c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2153db2a4165SFrank Mandarino 	return 0;
2154db2a4165SFrank Mandarino }
2155db2a4165SFrank Mandarino 
21566f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
215751737470SMark Brown {
21586f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
21591a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
216051737470SMark Brown 
216151737470SMark Brown 	if (!card->instantiated)
2162416356fcSMark Brown 		return 0;
216351737470SMark Brown 
21642c7b696aSMarcel Ziswiler 	/*
21652c7b696aSMarcel Ziswiler 	 * Flush out pmdown_time work - we actually do want to run it
21662c7b696aSMarcel Ziswiler 	 * now, we're shutting down so no imminent restart.
21672c7b696aSMarcel Ziswiler 	 */
216865462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
216951737470SMark Brown 
2170f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2171416356fcSMark Brown 
2172988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
2173bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
217488bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
21750b7990e3SKuninori Morimoto 		struct snd_soc_dai *codec_dai;
21761a497983SMengdong Lin 		int i;
217788bd870fSBenoit Cousson 
2178988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
21790b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
218088bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
218188bd870fSBenoit Cousson 		}
2182988e8cc4SNicolin Chen 	}
2183988e8cc4SNicolin Chen 
2184416356fcSMark Brown 	return 0;
218551737470SMark Brown }
21866f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
218751737470SMark Brown 
21886f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2189b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2190b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2191b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2192b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
21936f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2194b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2195416356fcSMark Brown };
2196deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2197416356fcSMark Brown 
2198db2a4165SFrank Mandarino /* ASoC platform driver */
2199db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2200db2a4165SFrank Mandarino 	.driver		= {
2201db2a4165SFrank Mandarino 		.name		= "soc-audio",
22026f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2203db2a4165SFrank Mandarino 	},
2204db2a4165SFrank Mandarino 	.probe		= soc_probe,
2205db2a4165SFrank Mandarino 	.remove		= soc_remove,
2206db2a4165SFrank Mandarino };
2207db2a4165SFrank Mandarino 
2208096e49d5SMark Brown /**
2209db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2210db2a4165SFrank Mandarino  * @_template: control template
2211db2a4165SFrank Mandarino  * @data: control private data
2212ac11a2b3SMark Brown  * @long_name: control long name
2213efb7ac3fSMark Brown  * @prefix: control name prefix
2214db2a4165SFrank Mandarino  *
2215db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2216db2a4165SFrank Mandarino  *
2217db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2218db2a4165SFrank Mandarino  */
2219db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
22203056557fSMark Brown 				  void *data, const char *long_name,
2221efb7ac3fSMark Brown 				  const char *prefix)
2222db2a4165SFrank Mandarino {
2223db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2224efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2225efb7ac3fSMark Brown 	char *name = NULL;
2226db2a4165SFrank Mandarino 
2227db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2228db2a4165SFrank Mandarino 	template.index = 0;
2229db2a4165SFrank Mandarino 
2230efb7ac3fSMark Brown 	if (!long_name)
2231efb7ac3fSMark Brown 		long_name = template.name;
2232efb7ac3fSMark Brown 
2233efb7ac3fSMark Brown 	if (prefix) {
22342b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2235efb7ac3fSMark Brown 		if (!name)
2236efb7ac3fSMark Brown 			return NULL;
2237efb7ac3fSMark Brown 
2238efb7ac3fSMark Brown 		template.name = name;
2239efb7ac3fSMark Brown 	} else {
2240efb7ac3fSMark Brown 		template.name = long_name;
2241efb7ac3fSMark Brown 	}
2242efb7ac3fSMark Brown 
2243efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2244efb7ac3fSMark Brown 
2245efb7ac3fSMark Brown 	kfree(name);
2246efb7ac3fSMark Brown 
2247efb7ac3fSMark Brown 	return kcontrol;
2248db2a4165SFrank Mandarino }
2249db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2250db2a4165SFrank Mandarino 
2251022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2252022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2253022658beSLiam Girdwood 	const char *prefix, void *data)
2254022658beSLiam Girdwood {
2255022658beSLiam Girdwood 	int err, i;
2256022658beSLiam Girdwood 
2257022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2258022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
22592c7b696aSMarcel Ziswiler 
2260022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2261022658beSLiam Girdwood 						     control->name, prefix));
2262022658beSLiam Girdwood 		if (err < 0) {
2263f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2264f110bfc7SLiam Girdwood 				control->name, err);
2265022658beSLiam Girdwood 			return err;
2266022658beSLiam Girdwood 		}
2267022658beSLiam Girdwood 	}
2268022658beSLiam Girdwood 
2269022658beSLiam Girdwood 	return 0;
2270022658beSLiam Girdwood }
2271022658beSLiam Girdwood 
22724fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
22734fefd698SDimitris Papastamos 					       const char *name)
22744fefd698SDimitris Papastamos {
22754fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
22764fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
22774fefd698SDimitris Papastamos 
22784fefd698SDimitris Papastamos 	if (unlikely(!name))
22794fefd698SDimitris Papastamos 		return NULL;
22804fefd698SDimitris Papastamos 
22814fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
22824fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
22834fefd698SDimitris Papastamos 			return kctl;
22844fefd698SDimitris Papastamos 	return NULL;
22854fefd698SDimitris Papastamos }
22864fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
22874fefd698SDimitris Papastamos 
2288db2a4165SFrank Mandarino /**
22890f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
22900f2780adSLars-Peter Clausen  *
22910f2780adSLars-Peter Clausen  * @component: Component to add controls to
22920f2780adSLars-Peter Clausen  * @controls: Array of controls to add
22930f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
22940f2780adSLars-Peter Clausen  *
22950f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
22960f2780adSLars-Peter Clausen  */
22970f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
22980f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
22990f2780adSLars-Peter Clausen {
23000f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
23010f2780adSLars-Peter Clausen 
23020f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
23030f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
23040f2780adSLars-Peter Clausen }
23050f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
23060f2780adSLars-Peter Clausen 
23070f2780adSLars-Peter Clausen /**
2308022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2309022658beSLiam Girdwood  * Convenience function to add a list of controls.
2310022658beSLiam Girdwood  *
2311022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2312022658beSLiam Girdwood  * @controls: array of controls to add
2313022658beSLiam Girdwood  * @num_controls: number of elements in the array
2314022658beSLiam Girdwood  *
2315022658beSLiam Girdwood  * Return 0 for success, else error.
2316022658beSLiam Girdwood  */
2317022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2318022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2319022658beSLiam Girdwood {
2320022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2321022658beSLiam Girdwood 
2322022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2323022658beSLiam Girdwood 			NULL, soc_card);
2324022658beSLiam Girdwood }
2325022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2326022658beSLiam Girdwood 
2327022658beSLiam Girdwood /**
2328022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2329022658beSLiam Girdwood  * Convienience function to add a list of controls.
2330022658beSLiam Girdwood  *
2331022658beSLiam Girdwood  * @dai: DAI to add controls to
2332022658beSLiam Girdwood  * @controls: array of controls to add
2333022658beSLiam Girdwood  * @num_controls: number of elements in the array
2334022658beSLiam Girdwood  *
2335022658beSLiam Girdwood  * Return 0 for success, else error.
2336022658beSLiam Girdwood  */
2337022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2338022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2339022658beSLiam Girdwood {
2340313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2341022658beSLiam Girdwood 
2342022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2343022658beSLiam Girdwood 			NULL, dai);
2344022658beSLiam Girdwood }
2345022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2346022658beSLiam Girdwood 
2347e894efefSSrinivas Kandagatla static int snd_soc_bind_card(struct snd_soc_card *card)
2348e894efefSSrinivas Kandagatla {
2349e894efefSSrinivas Kandagatla 	struct snd_soc_pcm_runtime *rtd;
2350e894efefSSrinivas Kandagatla 	int ret;
2351e894efefSSrinivas Kandagatla 
2352e894efefSSrinivas Kandagatla 	ret = snd_soc_instantiate_card(card);
2353e894efefSSrinivas Kandagatla 	if (ret != 0)
2354e894efefSSrinivas Kandagatla 		return ret;
2355e894efefSSrinivas Kandagatla 
2356e894efefSSrinivas Kandagatla 	/* deactivate pins to sleep state */
2357bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
2358e894efefSSrinivas Kandagatla 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2359e894efefSSrinivas Kandagatla 		struct snd_soc_dai *codec_dai;
2360e894efefSSrinivas Kandagatla 		int j;
2361e894efefSSrinivas Kandagatla 
2362e894efefSSrinivas Kandagatla 		for_each_rtd_codec_dai(rtd, j, codec_dai) {
2363e894efefSSrinivas Kandagatla 			if (!codec_dai->active)
2364e894efefSSrinivas Kandagatla 				pinctrl_pm_select_sleep_state(codec_dai->dev);
2365e894efefSSrinivas Kandagatla 		}
2366e894efefSSrinivas Kandagatla 
2367e894efefSSrinivas Kandagatla 		if (!cpu_dai->active)
2368e894efefSSrinivas Kandagatla 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
2369e894efefSSrinivas Kandagatla 	}
2370e894efefSSrinivas Kandagatla 
2371e894efefSSrinivas Kandagatla 	return ret;
2372e894efefSSrinivas Kandagatla }
2373e894efefSSrinivas Kandagatla 
2374c5af3a2eSMark Brown /**
2375c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2376c5af3a2eSMark Brown  *
2377ac11a2b3SMark Brown  * @card: Card to register
2378c5af3a2eSMark Brown  *
2379c5af3a2eSMark Brown  */
238070a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2381c5af3a2eSMark Brown {
2382c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2383c5af3a2eSMark Brown 		return -EINVAL;
2384c5af3a2eSMark Brown 
2385ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2386ed77cc12SMark Brown 
2387b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->widgets);
2388b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->paths);
2389b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->dapm_list);
2390b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->aux_comp_list);
2391b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->component_dev_list);
2392b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->list);
2393f8f80361SMengdong Lin 	INIT_LIST_HEAD(&card->dai_link_list);
23941a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
2395db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
23968a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2397b03bfaecSKuninori Morimoto 
2398b03bfaecSKuninori Morimoto 	card->num_rtd = 0;
2399c5af3a2eSMark Brown 	card->instantiated = 0;
2400f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
2401a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
240272b745e3SPeter Ujfalusi 	mutex_init(&card->pcm_mutex);
2403a9764869SKaiChieh Chuang 	spin_lock_init(&card->dpcm_lock);
2404c5af3a2eSMark Brown 
2405e894efefSSrinivas Kandagatla 	return snd_soc_bind_card(card);
2406c5af3a2eSMark Brown }
240770a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2408c5af3a2eSMark Brown 
2409e894efefSSrinivas Kandagatla static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2410e894efefSSrinivas Kandagatla {
2411e894efefSSrinivas Kandagatla 	if (card->instantiated) {
2412e894efefSSrinivas Kandagatla 		card->instantiated = false;
2413e894efefSSrinivas Kandagatla 		snd_soc_dapm_shutdown(card);
241453e947a0SKuninori Morimoto 		snd_soc_flush_all_delayed_work(card);
2415f96fb7d1SRanjani Sridharan 
2416f96fb7d1SRanjani Sridharan 		/* remove all components used by DAI links on this card */
2417b006c0c6SKuninori Morimoto 		soc_remove_link_components(card);
2418f96fb7d1SRanjani Sridharan 
2419e894efefSSrinivas Kandagatla 		soc_cleanup_card_resources(card);
2420e894efefSSrinivas Kandagatla 		if (!unregister)
2421e894efefSSrinivas Kandagatla 			list_add(&card->list, &unbind_card_list);
2422e894efefSSrinivas Kandagatla 	} else {
2423e894efefSSrinivas Kandagatla 		if (unregister)
2424e894efefSSrinivas Kandagatla 			list_del(&card->list);
2425e894efefSSrinivas Kandagatla 	}
2426e894efefSSrinivas Kandagatla }
2427e894efefSSrinivas Kandagatla 
2428c5af3a2eSMark Brown /**
2429c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2430c5af3a2eSMark Brown  *
2431ac11a2b3SMark Brown  * @card: Card to unregister
2432c5af3a2eSMark Brown  *
2433c5af3a2eSMark Brown  */
243470a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2435c5af3a2eSMark Brown {
2436b545542aSKuninori Morimoto 	mutex_lock(&client_mutex);
2437e894efefSSrinivas Kandagatla 	snd_soc_unbind_card(card, true);
2438b545542aSKuninori Morimoto 	mutex_unlock(&client_mutex);
2439f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2440c5af3a2eSMark Brown 
2441c5af3a2eSMark Brown 	return 0;
2442c5af3a2eSMark Brown }
244370a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2444c5af3a2eSMark Brown 
2445f0fba2adSLiam Girdwood /*
2446f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
2447f0fba2adSLiam Girdwood  * and sanitizing names.
2448f0fba2adSLiam Girdwood  */
24490b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2450f0fba2adSLiam Girdwood {
2451f0fba2adSLiam Girdwood 	char *found, name[NAME_SIZE];
2452f0fba2adSLiam Girdwood 	int id1, id2;
2453f0fba2adSLiam Girdwood 
2454f0fba2adSLiam Girdwood 	if (dev_name(dev) == NULL)
2455f0fba2adSLiam Girdwood 		return NULL;
2456f0fba2adSLiam Girdwood 
245758818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2458f0fba2adSLiam Girdwood 
2459f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
2460f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
2461f0fba2adSLiam Girdwood 	if (found) {
2462f0fba2adSLiam Girdwood 		/* get ID */
2463f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2464f0fba2adSLiam Girdwood 
2465f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
2466f0fba2adSLiam Girdwood 			if (*id == -1)
2467f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
2468f0fba2adSLiam Girdwood 		}
2469f0fba2adSLiam Girdwood 
2470f0fba2adSLiam Girdwood 	} else {
2471f0fba2adSLiam Girdwood 		/* I2C component devices are named "bus-addr" */
2472f0fba2adSLiam Girdwood 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2473f0fba2adSLiam Girdwood 			char tmp[NAME_SIZE];
2474f0fba2adSLiam Girdwood 
2475f0fba2adSLiam Girdwood 			/* create unique ID number from I2C addr and bus */
247605899446SJarkko Nikula 			*id = ((id1 & 0xffff) << 16) + id2;
2477f0fba2adSLiam Girdwood 
2478f0fba2adSLiam Girdwood 			/* sanitize component name for DAI link creation */
24792c7b696aSMarcel Ziswiler 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
24802c7b696aSMarcel Ziswiler 				 name);
248158818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2482f0fba2adSLiam Girdwood 		} else
2483f0fba2adSLiam Girdwood 			*id = 0;
2484f0fba2adSLiam Girdwood 	}
2485f0fba2adSLiam Girdwood 
2486f0fba2adSLiam Girdwood 	return kstrdup(name, GFP_KERNEL);
2487f0fba2adSLiam Girdwood }
2488f0fba2adSLiam Girdwood 
2489f0fba2adSLiam Girdwood /*
2490f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
2491f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
2492f0fba2adSLiam Girdwood  */
2493f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
2494f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
2495f0fba2adSLiam Girdwood {
2496f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
249710e8aa9aSMichał Mirosław 		dev_err(dev,
249810e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
249910e8aa9aSMichał Mirosław 			dev_name(dev));
2500f0fba2adSLiam Girdwood 		return NULL;
2501f0fba2adSLiam Girdwood 	}
2502f0fba2adSLiam Girdwood 
2503f0fba2adSLiam Girdwood 	return kstrdup(dai_drv->name, GFP_KERNEL);
2504f0fba2adSLiam Girdwood }
2505f0fba2adSLiam Girdwood 
25069115171aSMark Brown /**
250732c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
25089115171aSMark Brown  *
250932c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
25109115171aSMark Brown  */
251132c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
25129115171aSMark Brown {
25135c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
25149115171aSMark Brown 
251515a0c645SKuninori Morimoto 	for_each_component_dais_safe(component, dai, _dai) {
251632c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
251732c9ba54SLars-Peter Clausen 			dai->name);
25189115171aSMark Brown 		list_del(&dai->list);
2519f0fba2adSLiam Girdwood 		kfree(dai->name);
2520f0fba2adSLiam Girdwood 		kfree(dai);
25219115171aSMark Brown 	}
252232c9ba54SLars-Peter Clausen }
25239115171aSMark Brown 
25245e4fb372SMengdong Lin /* Create a DAI and add it to the component's DAI list */
25255e4fb372SMengdong Lin static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
25265e4fb372SMengdong Lin 	struct snd_soc_dai_driver *dai_drv,
25275e4fb372SMengdong Lin 	bool legacy_dai_naming)
25285e4fb372SMengdong Lin {
25295e4fb372SMengdong Lin 	struct device *dev = component->dev;
25305e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
25315e4fb372SMengdong Lin 
25325e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
25335e4fb372SMengdong Lin 
25345e4fb372SMengdong Lin 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
25355e4fb372SMengdong Lin 	if (dai == NULL)
25365e4fb372SMengdong Lin 		return NULL;
25375e4fb372SMengdong Lin 
25385e4fb372SMengdong Lin 	/*
25395e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
25405e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
25415e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
25425e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
25435e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
25445e4fb372SMengdong Lin 	 * component-less anymore.
25455e4fb372SMengdong Lin 	 */
25465e4fb372SMengdong Lin 	if (legacy_dai_naming &&
25475e4fb372SMengdong Lin 	    (dai_drv->id == 0 || dai_drv->name == NULL)) {
25485e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
25495e4fb372SMengdong Lin 	} else {
25505e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
25515e4fb372SMengdong Lin 		if (dai_drv->id)
25525e4fb372SMengdong Lin 			dai->id = dai_drv->id;
25535e4fb372SMengdong Lin 		else
25545e4fb372SMengdong Lin 			dai->id = component->num_dai;
25555e4fb372SMengdong Lin 	}
25565e4fb372SMengdong Lin 	if (dai->name == NULL) {
25575e4fb372SMengdong Lin 		kfree(dai);
25585e4fb372SMengdong Lin 		return NULL;
25595e4fb372SMengdong Lin 	}
25605e4fb372SMengdong Lin 
25615e4fb372SMengdong Lin 	dai->component = component;
25625e4fb372SMengdong Lin 	dai->dev = dev;
25635e4fb372SMengdong Lin 	dai->driver = dai_drv;
25645e4fb372SMengdong Lin 	if (!dai->driver->ops)
25655e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
25665e4fb372SMengdong Lin 
256715a0c645SKuninori Morimoto 	/* see for_each_component_dais */
256858bf4179SKuninori Morimoto 	list_add_tail(&dai->list, &component->dai_list);
25695e4fb372SMengdong Lin 	component->num_dai++;
25705e4fb372SMengdong Lin 
25715e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
25725e4fb372SMengdong Lin 	return dai;
25735e4fb372SMengdong Lin }
25745e4fb372SMengdong Lin 
25759115171aSMark Brown /**
257632c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
25779115171aSMark Brown  *
25786106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
25796106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
2580ac11a2b3SMark Brown  * @count: Number of DAIs
25819115171aSMark Brown  */
258232c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
25832c7b696aSMarcel Ziswiler 				 struct snd_soc_dai_driver *dai_drv,
25842c7b696aSMarcel Ziswiler 				 size_t count)
25859115171aSMark Brown {
25866106d129SLars-Peter Clausen 	struct device *dev = component->dev;
2587f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
258832c9ba54SLars-Peter Clausen 	unsigned int i;
258932c9ba54SLars-Peter Clausen 	int ret;
2590f0fba2adSLiam Girdwood 
25915b5e0928SAlexey Dobriyan 	dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
25929115171aSMark Brown 
25939115171aSMark Brown 	for (i = 0; i < count; i++) {
2594f0fba2adSLiam Girdwood 
25952c7b696aSMarcel Ziswiler 		dai = soc_add_dai(component, dai_drv + i, count == 1 &&
25962c7b696aSMarcel Ziswiler 				  !component->driver->non_legacy_dai_naming);
2597c46e0079SAxel Lin 		if (dai == NULL) {
2598c46e0079SAxel Lin 			ret = -ENOMEM;
2599c46e0079SAxel Lin 			goto err;
2600c46e0079SAxel Lin 		}
2601f0fba2adSLiam Girdwood 	}
2602f0fba2adSLiam Girdwood 
26039115171aSMark Brown 	return 0;
26049115171aSMark Brown 
26059115171aSMark Brown err:
260632c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
26079115171aSMark Brown 
26089115171aSMark Brown 	return ret;
26099115171aSMark Brown }
26109115171aSMark Brown 
261168003e6cSMengdong Lin /**
261268003e6cSMengdong Lin  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
261368003e6cSMengdong Lin  *
261468003e6cSMengdong Lin  * @component: The component the DAIs are registered for
261568003e6cSMengdong Lin  * @dai_drv: DAI driver to use for the DAI
261668003e6cSMengdong Lin  *
261768003e6cSMengdong Lin  * Topology can use this API to register DAIs when probing a component.
261868003e6cSMengdong Lin  * These DAIs's widgets will be freed in the card cleanup and the DAIs
261968003e6cSMengdong Lin  * will be freed in the component cleanup.
262068003e6cSMengdong Lin  */
262168003e6cSMengdong Lin int snd_soc_register_dai(struct snd_soc_component *component,
262268003e6cSMengdong Lin 	struct snd_soc_dai_driver *dai_drv)
262368003e6cSMengdong Lin {
262468003e6cSMengdong Lin 	struct snd_soc_dapm_context *dapm =
262568003e6cSMengdong Lin 		snd_soc_component_get_dapm(component);
262668003e6cSMengdong Lin 	struct snd_soc_dai *dai;
262768003e6cSMengdong Lin 	int ret;
262868003e6cSMengdong Lin 
262968003e6cSMengdong Lin 	if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
263068003e6cSMengdong Lin 		dev_err(component->dev, "Invalid dai type %d\n",
263168003e6cSMengdong Lin 			dai_drv->dobj.type);
263268003e6cSMengdong Lin 		return -EINVAL;
263368003e6cSMengdong Lin 	}
263468003e6cSMengdong Lin 
263568003e6cSMengdong Lin 	lockdep_assert_held(&client_mutex);
263668003e6cSMengdong Lin 	dai = soc_add_dai(component, dai_drv, false);
263768003e6cSMengdong Lin 	if (!dai)
263868003e6cSMengdong Lin 		return -ENOMEM;
263968003e6cSMengdong Lin 
26402c7b696aSMarcel Ziswiler 	/*
26412c7b696aSMarcel Ziswiler 	 * Create the DAI widgets here. After adding DAIs, topology may
264268003e6cSMengdong Lin 	 * also add routes that need these widgets as source or sink.
264368003e6cSMengdong Lin 	 */
264468003e6cSMengdong Lin 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
264568003e6cSMengdong Lin 	if (ret != 0) {
264668003e6cSMengdong Lin 		dev_err(component->dev,
264768003e6cSMengdong Lin 			"Failed to create DAI widgets %d\n", ret);
264868003e6cSMengdong Lin 	}
264968003e6cSMengdong Lin 
265068003e6cSMengdong Lin 	return ret;
265168003e6cSMengdong Lin }
265268003e6cSMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_register_dai);
265368003e6cSMengdong Lin 
2654bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
2655bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
2656d191bd8dSKuninori Morimoto {
26578d92bb51SKuninori Morimoto 	INIT_LIST_HEAD(&component->dai_list);
2658495efdb0SKuninori Morimoto 	INIT_LIST_HEAD(&component->dobj_list);
2659495efdb0SKuninori Morimoto 	INIT_LIST_HEAD(&component->card_list);
26608d92bb51SKuninori Morimoto 	mutex_init(&component->io_mutex);
26618d92bb51SKuninori Morimoto 
2662bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
2663bb13109dSLars-Peter Clausen 	if (!component->name) {
2664bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
2665d191bd8dSKuninori Morimoto 		return -ENOMEM;
2666d191bd8dSKuninori Morimoto 	}
2667d191bd8dSKuninori Morimoto 
2668bb13109dSLars-Peter Clausen 	component->dev = dev;
2669bb13109dSLars-Peter Clausen 	component->driver = driver;
2670e2c330b9SLars-Peter Clausen 
2671bb13109dSLars-Peter Clausen 	return 0;
2672d191bd8dSKuninori Morimoto }
2673d191bd8dSKuninori Morimoto 
267420feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
2675886f5692SLars-Peter Clausen {
2676886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
267720feb881SLars-Peter Clausen 
2678886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
2679886f5692SLars-Peter Clausen 	if (val_bytes > 0)
2680886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
2681886f5692SLars-Peter Clausen }
268220feb881SLars-Peter Clausen 
2683e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
2684e874bf5fSLars-Peter Clausen 
268520feb881SLars-Peter Clausen /**
26862c7b696aSMarcel Ziswiler  * snd_soc_component_init_regmap() - Initialize regmap instance for the
26872c7b696aSMarcel Ziswiler  *                                   component
268820feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
268920feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
269020feb881SLars-Peter Clausen  *
269120feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
269220feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
269320feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
269420feb881SLars-Peter Clausen  * before the first IO attempt of the component.
269520feb881SLars-Peter Clausen  */
269620feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
269720feb881SLars-Peter Clausen 	struct regmap *regmap)
269820feb881SLars-Peter Clausen {
269920feb881SLars-Peter Clausen 	component->regmap = regmap;
270020feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
2701886f5692SLars-Peter Clausen }
270220feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
270320feb881SLars-Peter Clausen 
270420feb881SLars-Peter Clausen /**
27052c7b696aSMarcel Ziswiler  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
27062c7b696aSMarcel Ziswiler  *                                   component
270720feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
270820feb881SLars-Peter Clausen  *
270920feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
271020feb881SLars-Peter Clausen  * removes the regmap instance from the component.
271120feb881SLars-Peter Clausen  *
271220feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
271320feb881SLars-Peter Clausen  * to initialize the regmap instance.
271420feb881SLars-Peter Clausen  */
271520feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
271620feb881SLars-Peter Clausen {
271720feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
271820feb881SLars-Peter Clausen 	component->regmap = NULL;
271920feb881SLars-Peter Clausen }
272020feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2721886f5692SLars-Peter Clausen 
2722e874bf5fSLars-Peter Clausen #endif
2723d191bd8dSKuninori Morimoto 
2724359c71eeSKuninori Morimoto static void snd_soc_component_add(struct snd_soc_component *component)
2725bb13109dSLars-Peter Clausen {
2726359c71eeSKuninori Morimoto 	mutex_lock(&client_mutex);
2727359c71eeSKuninori Morimoto 
2728999f7f5aSKuninori Morimoto 	if (!component->driver->write && !component->driver->read) {
272920feb881SLars-Peter Clausen 		if (!component->regmap)
27302c7b696aSMarcel Ziswiler 			component->regmap = dev_get_regmap(component->dev,
27312c7b696aSMarcel Ziswiler 							   NULL);
273220feb881SLars-Peter Clausen 		if (component->regmap)
273320feb881SLars-Peter Clausen 			snd_soc_component_setup_regmap(component);
273420feb881SLars-Peter Clausen 	}
2735886f5692SLars-Peter Clausen 
2736368dee94SKuninori Morimoto 	/* see for_each_component */
2737bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
2738d191bd8dSKuninori Morimoto 
2739d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
2740bb13109dSLars-Peter Clausen }
2741d191bd8dSKuninori Morimoto 
2742bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
2743bb13109dSLars-Peter Clausen {
2744bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
2745bb13109dSLars-Peter Clausen 	kfree(component->name);
2746bb13109dSLars-Peter Clausen }
2747d191bd8dSKuninori Morimoto 
2748bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2749bb13109dSLars-Peter Clausen {
2750c12c1aadSKuninori Morimoto 	struct snd_soc_card *card = component->card;
2751c12c1aadSKuninori Morimoto 
2752c12c1aadSKuninori Morimoto 	if (card)
2753e894efefSSrinivas Kandagatla 		snd_soc_unbind_card(card, false);
2754c12c1aadSKuninori Morimoto 
2755bb13109dSLars-Peter Clausen 	list_del(&component->list);
2756bb13109dSLars-Peter Clausen }
2757d191bd8dSKuninori Morimoto 
2758273d778eSKuninori Morimoto #define ENDIANNESS_MAP(name) \
2759273d778eSKuninori Morimoto 	(SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2760273d778eSKuninori Morimoto static u64 endianness_format_map[] = {
2761273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S16_),
2762273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U16_),
2763273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_),
2764273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_),
2765273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S32_),
2766273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U32_),
2767273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_3),
2768273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_3),
2769273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S20_3),
2770273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U20_3),
2771273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S18_3),
2772273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U18_3),
2773273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT_),
2774273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT64_),
2775273d778eSKuninori Morimoto 	ENDIANNESS_MAP(IEC958_SUBFRAME_),
2776273d778eSKuninori Morimoto };
2777273d778eSKuninori Morimoto 
2778273d778eSKuninori Morimoto /*
2779273d778eSKuninori Morimoto  * Fix up the DAI formats for endianness: codecs don't actually see
2780273d778eSKuninori Morimoto  * the endianness of the data but we're using the CPU format
2781273d778eSKuninori Morimoto  * definitions which do need to include endianness so we ensure that
2782273d778eSKuninori Morimoto  * codec DAIs always have both big and little endian variants set.
2783273d778eSKuninori Morimoto  */
2784273d778eSKuninori Morimoto static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2785273d778eSKuninori Morimoto {
2786273d778eSKuninori Morimoto 	int i;
2787273d778eSKuninori Morimoto 
2788273d778eSKuninori Morimoto 	for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2789273d778eSKuninori Morimoto 		if (stream->formats & endianness_format_map[i])
2790273d778eSKuninori Morimoto 			stream->formats |= endianness_format_map[i];
2791273d778eSKuninori Morimoto }
2792273d778eSKuninori Morimoto 
2793e894efefSSrinivas Kandagatla static void snd_soc_try_rebind_card(void)
2794e894efefSSrinivas Kandagatla {
2795e894efefSSrinivas Kandagatla 	struct snd_soc_card *card, *c;
2796e894efefSSrinivas Kandagatla 
2797b245d273SKuninori Morimoto 	list_for_each_entry_safe(card, c, &unbind_card_list, list)
2798e894efefSSrinivas Kandagatla 		if (!snd_soc_bind_card(card))
2799e894efefSSrinivas Kandagatla 			list_del(&card->list);
2800e894efefSSrinivas Kandagatla }
2801e894efefSSrinivas Kandagatla 
2802e0dac41bSKuninori Morimoto int snd_soc_add_component(struct device *dev,
2803e0dac41bSKuninori Morimoto 			struct snd_soc_component *component,
2804cf9e829eSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2805d191bd8dSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2806d191bd8dSKuninori Morimoto 			int num_dai)
2807d191bd8dSKuninori Morimoto {
2808bb13109dSLars-Peter Clausen 	int ret;
2809273d778eSKuninori Morimoto 	int i;
2810d191bd8dSKuninori Morimoto 
2811cf9e829eSKuninori Morimoto 	ret = snd_soc_component_initialize(component, component_driver, dev);
2812bb13109dSLars-Peter Clausen 	if (ret)
2813bb13109dSLars-Peter Clausen 		goto err_free;
2814bb13109dSLars-Peter Clausen 
2815273d778eSKuninori Morimoto 	if (component_driver->endianness) {
2816273d778eSKuninori Morimoto 		for (i = 0; i < num_dai; i++) {
2817273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].playback);
2818273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].capture);
2819273d778eSKuninori Morimoto 		}
2820273d778eSKuninori Morimoto 	}
2821273d778eSKuninori Morimoto 
28220e7b25c6SKuninori Morimoto 	ret = snd_soc_register_dais(component, dai_drv, num_dai);
2823bb13109dSLars-Peter Clausen 	if (ret < 0) {
2824f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
2825bb13109dSLars-Peter Clausen 		goto err_cleanup;
2826bb13109dSLars-Peter Clausen 	}
2827bb13109dSLars-Peter Clausen 
2828cf9e829eSKuninori Morimoto 	snd_soc_component_add(component);
2829e894efefSSrinivas Kandagatla 	snd_soc_try_rebind_card();
2830bb13109dSLars-Peter Clausen 
2831bb13109dSLars-Peter Clausen 	return 0;
2832bb13109dSLars-Peter Clausen 
2833bb13109dSLars-Peter Clausen err_cleanup:
2834cf9e829eSKuninori Morimoto 	snd_soc_component_cleanup(component);
2835bb13109dSLars-Peter Clausen err_free:
2836bb13109dSLars-Peter Clausen 	return ret;
2837d191bd8dSKuninori Morimoto }
2838e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component);
2839e0dac41bSKuninori Morimoto 
2840e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev,
2841e0dac41bSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2842e0dac41bSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2843e0dac41bSKuninori Morimoto 			int num_dai)
2844e0dac41bSKuninori Morimoto {
2845e0dac41bSKuninori Morimoto 	struct snd_soc_component *component;
2846e0dac41bSKuninori Morimoto 
28477ecbd6a9SKuninori Morimoto 	component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
284808e61d03SKuninori Morimoto 	if (!component)
2849e0dac41bSKuninori Morimoto 		return -ENOMEM;
2850e0dac41bSKuninori Morimoto 
2851e0dac41bSKuninori Morimoto 	return snd_soc_add_component(dev, component, component_driver,
2852e0dac41bSKuninori Morimoto 				     dai_drv, num_dai);
2853e0dac41bSKuninori Morimoto }
2854d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
2855d191bd8dSKuninori Morimoto 
2856d191bd8dSKuninori Morimoto /**
28572eccea8cSKuninori Morimoto  * snd_soc_unregister_component - Unregister all related component
28582eccea8cSKuninori Morimoto  * from the ASoC core
2859d191bd8dSKuninori Morimoto  *
2860628536eaSJonathan Corbet  * @dev: The device to unregister
2861d191bd8dSKuninori Morimoto  */
28622eccea8cSKuninori Morimoto static int __snd_soc_unregister_component(struct device *dev)
2863d191bd8dSKuninori Morimoto {
2864cf9e829eSKuninori Morimoto 	struct snd_soc_component *component;
286521a03528SKuninori Morimoto 	int found = 0;
2866d191bd8dSKuninori Morimoto 
286734e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
2868368dee94SKuninori Morimoto 	for_each_component(component) {
2869999f7f5aSKuninori Morimoto 		if (dev != component->dev)
287021a03528SKuninori Morimoto 			continue;
2871d191bd8dSKuninori Morimoto 
28722c7b696aSMarcel Ziswiler 		snd_soc_tplg_component_remove(component,
28732c7b696aSMarcel Ziswiler 					      SND_SOC_TPLG_INDEX_ALL);
2874cf9e829eSKuninori Morimoto 		snd_soc_component_del_unlocked(component);
287521a03528SKuninori Morimoto 		found = 1;
287621a03528SKuninori Morimoto 		break;
2877d191bd8dSKuninori Morimoto 	}
2878d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
2879d191bd8dSKuninori Morimoto 
28802c7b696aSMarcel Ziswiler 	if (found)
2881cf9e829eSKuninori Morimoto 		snd_soc_component_cleanup(component);
28822eccea8cSKuninori Morimoto 
28832eccea8cSKuninori Morimoto 	return found;
28842eccea8cSKuninori Morimoto }
28852eccea8cSKuninori Morimoto 
28862eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
28872eccea8cSKuninori Morimoto {
28882c7b696aSMarcel Ziswiler 	while (__snd_soc_unregister_component(dev))
28892c7b696aSMarcel Ziswiler 		;
2890d191bd8dSKuninori Morimoto }
2891d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2892d191bd8dSKuninori Morimoto 
28937dd5d0d9SKuninori Morimoto struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
28947dd5d0d9SKuninori Morimoto 						   const char *driver_name)
28957dd5d0d9SKuninori Morimoto {
28967dd5d0d9SKuninori Morimoto 	struct snd_soc_component *component;
28977dd5d0d9SKuninori Morimoto 	struct snd_soc_component *ret;
28987dd5d0d9SKuninori Morimoto 
28997dd5d0d9SKuninori Morimoto 	ret = NULL;
29007dd5d0d9SKuninori Morimoto 	mutex_lock(&client_mutex);
2901368dee94SKuninori Morimoto 	for_each_component(component) {
29027dd5d0d9SKuninori Morimoto 		if (dev != component->dev)
29037dd5d0d9SKuninori Morimoto 			continue;
29047dd5d0d9SKuninori Morimoto 
29057dd5d0d9SKuninori Morimoto 		if (driver_name &&
29067dd5d0d9SKuninori Morimoto 		    (driver_name != component->driver->name) &&
29077dd5d0d9SKuninori Morimoto 		    (strcmp(component->driver->name, driver_name) != 0))
29087dd5d0d9SKuninori Morimoto 			continue;
29097dd5d0d9SKuninori Morimoto 
29107dd5d0d9SKuninori Morimoto 		ret = component;
29117dd5d0d9SKuninori Morimoto 		break;
29127dd5d0d9SKuninori Morimoto 	}
29137dd5d0d9SKuninori Morimoto 	mutex_unlock(&client_mutex);
29147dd5d0d9SKuninori Morimoto 
29157dd5d0d9SKuninori Morimoto 	return ret;
29167dd5d0d9SKuninori Morimoto }
29177dd5d0d9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
29187dd5d0d9SKuninori Morimoto 
2919bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
2920b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2921bec4fa05SStephen Warren 			       const char *propname)
2922bec4fa05SStephen Warren {
2923b07609ceSKuninori Morimoto 	struct device_node *np;
2924bec4fa05SStephen Warren 	int ret;
2925bec4fa05SStephen Warren 
29267e07e7c0STushar Behera 	if (!card->dev) {
29277e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
29287e07e7c0STushar Behera 		return -EINVAL;
29297e07e7c0STushar Behera 	}
29307e07e7c0STushar Behera 
29317e07e7c0STushar Behera 	np = card->dev->of_node;
29327e07e7c0STushar Behera 
2933bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
2934bec4fa05SStephen Warren 	/*
2935bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
2936bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
2937bec4fa05SStephen Warren 	 * snd_soc_register_card.
2938bec4fa05SStephen Warren 	 */
2939bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
2940bec4fa05SStephen Warren 		dev_err(card->dev,
2941f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
2942bec4fa05SStephen Warren 			propname, ret);
2943bec4fa05SStephen Warren 		return ret;
2944bec4fa05SStephen Warren 	}
2945bec4fa05SStephen Warren 
2946bec4fa05SStephen Warren 	return 0;
2947bec4fa05SStephen Warren }
2948b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2949bec4fa05SStephen Warren 
29509a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
29519a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
29529a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
29539a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
29549a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
29559a6d4860SXiubo Li };
29569a6d4860SXiubo Li 
295721efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
29589a6d4860SXiubo Li 					  const char *propname)
29599a6d4860SXiubo Li {
296021efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
29619a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
29629a6d4860SXiubo Li 	const char *template, *wname;
29639a6d4860SXiubo Li 	int i, j, num_widgets, ret;
29649a6d4860SXiubo Li 
29659a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
29669a6d4860SXiubo Li 	if (num_widgets < 0) {
29679a6d4860SXiubo Li 		dev_err(card->dev,
29689a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
29699a6d4860SXiubo Li 		return -EINVAL;
29709a6d4860SXiubo Li 	}
29719a6d4860SXiubo Li 	if (num_widgets & 1) {
29729a6d4860SXiubo Li 		dev_err(card->dev,
29739a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
29749a6d4860SXiubo Li 		return -EINVAL;
29759a6d4860SXiubo Li 	}
29769a6d4860SXiubo Li 
29779a6d4860SXiubo Li 	num_widgets /= 2;
29789a6d4860SXiubo Li 	if (!num_widgets) {
29799a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
29809a6d4860SXiubo Li 			propname);
29819a6d4860SXiubo Li 		return -EINVAL;
29829a6d4860SXiubo Li 	}
29839a6d4860SXiubo Li 
29849a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
29859a6d4860SXiubo Li 			       GFP_KERNEL);
29869a6d4860SXiubo Li 	if (!widgets) {
29879a6d4860SXiubo Li 		dev_err(card->dev,
29889a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
29899a6d4860SXiubo Li 		return -ENOMEM;
29909a6d4860SXiubo Li 	}
29919a6d4860SXiubo Li 
29929a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
29939a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
29949a6d4860SXiubo Li 			2 * i, &template);
29959a6d4860SXiubo Li 		if (ret) {
29969a6d4860SXiubo Li 			dev_err(card->dev,
29979a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
29989a6d4860SXiubo Li 				propname, 2 * i, ret);
29999a6d4860SXiubo Li 			return -EINVAL;
30009a6d4860SXiubo Li 		}
30019a6d4860SXiubo Li 
30029a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
30039a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
30049a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
30059a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
30069a6d4860SXiubo Li 				break;
30079a6d4860SXiubo Li 			}
30089a6d4860SXiubo Li 		}
30099a6d4860SXiubo Li 
30109a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
30119a6d4860SXiubo Li 			dev_err(card->dev,
30129a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
30139a6d4860SXiubo Li 				template);
30149a6d4860SXiubo Li 			return -EINVAL;
30159a6d4860SXiubo Li 		}
30169a6d4860SXiubo Li 
30179a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
30189a6d4860SXiubo Li 						    (2 * i) + 1,
30199a6d4860SXiubo Li 						    &wname);
30209a6d4860SXiubo Li 		if (ret) {
30219a6d4860SXiubo Li 			dev_err(card->dev,
30229a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
30239a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
30249a6d4860SXiubo Li 			return -EINVAL;
30259a6d4860SXiubo Li 		}
30269a6d4860SXiubo Li 
30279a6d4860SXiubo Li 		widgets[i].name = wname;
30289a6d4860SXiubo Li 	}
30299a6d4860SXiubo Li 
3030f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
3031f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
30329a6d4860SXiubo Li 
30339a6d4860SXiubo Li 	return 0;
30349a6d4860SXiubo Li }
303521efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
30369a6d4860SXiubo Li 
3037cbdfab3bSJerome Brunet int snd_soc_of_get_slot_mask(struct device_node *np,
30386131084aSJyri Sarha 			     const char *prop_name,
30396131084aSJyri Sarha 			     unsigned int *mask)
30406131084aSJyri Sarha {
30416131084aSJyri Sarha 	u32 val;
30426c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
30436131084aSJyri Sarha 	int i;
30446131084aSJyri Sarha 
30456131084aSJyri Sarha 	if (!of_slot_mask)
30466131084aSJyri Sarha 		return 0;
30476131084aSJyri Sarha 	val /= sizeof(u32);
30486131084aSJyri Sarha 	for (i = 0; i < val; i++)
30496131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
30506131084aSJyri Sarha 			*mask |= (1 << i);
30516131084aSJyri Sarha 
30526131084aSJyri Sarha 	return val;
30536131084aSJyri Sarha }
3054cbdfab3bSJerome Brunet EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
30556131084aSJyri Sarha 
305689c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
30576131084aSJyri Sarha 			      unsigned int *tx_mask,
30586131084aSJyri Sarha 			      unsigned int *rx_mask,
305989c67857SXiubo Li 			      unsigned int *slots,
306089c67857SXiubo Li 			      unsigned int *slot_width)
306189c67857SXiubo Li {
306289c67857SXiubo Li 	u32 val;
306389c67857SXiubo Li 	int ret;
306489c67857SXiubo Li 
30656131084aSJyri Sarha 	if (tx_mask)
30666131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
30676131084aSJyri Sarha 	if (rx_mask)
30686131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
30696131084aSJyri Sarha 
307089c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
307189c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
307289c67857SXiubo Li 		if (ret)
307389c67857SXiubo Li 			return ret;
307489c67857SXiubo Li 
307589c67857SXiubo Li 		if (slots)
307689c67857SXiubo Li 			*slots = val;
307789c67857SXiubo Li 	}
307889c67857SXiubo Li 
307989c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
308089c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
308189c67857SXiubo Li 		if (ret)
308289c67857SXiubo Li 			return ret;
308389c67857SXiubo Li 
308489c67857SXiubo Li 		if (slot_width)
308589c67857SXiubo Li 			*slot_width = val;
308689c67857SXiubo Li 	}
308789c67857SXiubo Li 
308889c67857SXiubo Li 	return 0;
308989c67857SXiubo Li }
309089c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
309189c67857SXiubo Li 
30923b710356SKuninori Morimoto void snd_soc_of_parse_node_prefix(struct device_node *np,
30935e3cdaa2SKuninori Morimoto 				  struct snd_soc_codec_conf *codec_conf,
30945e3cdaa2SKuninori Morimoto 				  struct device_node *of_node,
30955e3cdaa2SKuninori Morimoto 				  const char *propname)
30965e3cdaa2SKuninori Morimoto {
30975e3cdaa2SKuninori Morimoto 	const char *str;
30985e3cdaa2SKuninori Morimoto 	int ret;
30995e3cdaa2SKuninori Morimoto 
31005e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
31015e3cdaa2SKuninori Morimoto 	if (ret < 0) {
31025e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
31035e3cdaa2SKuninori Morimoto 		return;
31045e3cdaa2SKuninori Morimoto 	}
31055e3cdaa2SKuninori Morimoto 
31065e3cdaa2SKuninori Morimoto 	codec_conf->of_node	= of_node;
31075e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
31085e3cdaa2SKuninori Morimoto }
31093b710356SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
31105e3cdaa2SKuninori Morimoto 
31112bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3112a4a54dd5SStephen Warren 				   const char *propname)
3113a4a54dd5SStephen Warren {
31142bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
3115e3b1e6a1SMark Brown 	int num_routes;
3116a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3117a4a54dd5SStephen Warren 	int i, ret;
3118a4a54dd5SStephen Warren 
3119a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3120c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
312110e8aa9aSMichał Mirosław 		dev_err(card->dev,
312210e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
312310e8aa9aSMichał Mirosław 			propname);
3124a4a54dd5SStephen Warren 		return -EINVAL;
3125a4a54dd5SStephen Warren 	}
3126a4a54dd5SStephen Warren 	num_routes /= 2;
3127a4a54dd5SStephen Warren 	if (!num_routes) {
3128f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3129a4a54dd5SStephen Warren 			propname);
3130a4a54dd5SStephen Warren 		return -EINVAL;
3131a4a54dd5SStephen Warren 	}
3132a4a54dd5SStephen Warren 
3133a86854d0SKees Cook 	routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
3134a4a54dd5SStephen Warren 			      GFP_KERNEL);
3135a4a54dd5SStephen Warren 	if (!routes) {
3136a4a54dd5SStephen Warren 		dev_err(card->dev,
3137f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3138a4a54dd5SStephen Warren 		return -EINVAL;
3139a4a54dd5SStephen Warren 	}
3140a4a54dd5SStephen Warren 
3141a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3142a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3143e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
3144a4a54dd5SStephen Warren 		if (ret) {
3145c871bd0bSMark Brown 			dev_err(card->dev,
3146c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3147c871bd0bSMark Brown 				propname, 2 * i, ret);
3148a4a54dd5SStephen Warren 			return -EINVAL;
3149a4a54dd5SStephen Warren 		}
3150a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3151e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3152a4a54dd5SStephen Warren 		if (ret) {
3153a4a54dd5SStephen Warren 			dev_err(card->dev,
3154c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3155c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3156a4a54dd5SStephen Warren 			return -EINVAL;
3157a4a54dd5SStephen Warren 		}
3158a4a54dd5SStephen Warren 	}
3159a4a54dd5SStephen Warren 
3160f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
3161f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
3162a4a54dd5SStephen Warren 
3163a4a54dd5SStephen Warren 	return 0;
3164a4a54dd5SStephen Warren }
31652bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3166a4a54dd5SStephen Warren 
3167a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3168389cb834SJyri Sarha 				     const char *prefix,
3169389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3170389cb834SJyri Sarha 				     struct device_node **framemaster)
3171a7930ed4SKuninori Morimoto {
3172a7930ed4SKuninori Morimoto 	int ret, i;
3173a7930ed4SKuninori Morimoto 	char prop[128];
3174a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3175a7930ed4SKuninori Morimoto 	int bit, frame;
3176a7930ed4SKuninori Morimoto 	const char *str;
3177a7930ed4SKuninori Morimoto 	struct {
3178a7930ed4SKuninori Morimoto 		char *name;
3179a7930ed4SKuninori Morimoto 		unsigned int val;
3180a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3181a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3182a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3183a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3184a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3185a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3186a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3187a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3188a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3189a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3190a7930ed4SKuninori Morimoto 	};
3191a7930ed4SKuninori Morimoto 
3192a7930ed4SKuninori Morimoto 	if (!prefix)
3193a7930ed4SKuninori Morimoto 		prefix = "";
3194a7930ed4SKuninori Morimoto 
3195a7930ed4SKuninori Morimoto 	/*
31965711c979SKuninori Morimoto 	 * check "dai-format = xxx"
31975711c979SKuninori Morimoto 	 * or    "[prefix]format = xxx"
3198a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3199a7930ed4SKuninori Morimoto 	 */
32005711c979SKuninori Morimoto 	ret = of_property_read_string(np, "dai-format", &str);
32015711c979SKuninori Morimoto 	if (ret < 0) {
3202a7930ed4SKuninori Morimoto 		snprintf(prop, sizeof(prop), "%sformat", prefix);
3203a7930ed4SKuninori Morimoto 		ret = of_property_read_string(np, prop, &str);
32045711c979SKuninori Morimoto 	}
3205a7930ed4SKuninori Morimoto 	if (ret == 0) {
3206a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3207a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3208a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3209a7930ed4SKuninori Morimoto 				break;
3210a7930ed4SKuninori Morimoto 			}
3211a7930ed4SKuninori Morimoto 		}
3212a7930ed4SKuninori Morimoto 	}
3213a7930ed4SKuninori Morimoto 
3214a7930ed4SKuninori Morimoto 	/*
32158c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3216a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3217a7930ed4SKuninori Morimoto 	 */
32188c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
321951930295SJulia Lawall 	if (of_property_read_bool(np, prop))
32208c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
32218c2d6a9fSKuninori Morimoto 	else
32228c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3223a7930ed4SKuninori Morimoto 
3224a7930ed4SKuninori Morimoto 	/*
3225a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3226a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3227a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3228a7930ed4SKuninori Morimoto 	 */
3229a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3230a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3231a7930ed4SKuninori Morimoto 
3232a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3233a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3234a7930ed4SKuninori Morimoto 
3235a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3236a7930ed4SKuninori Morimoto 	case 0x11:
3237a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3238a7930ed4SKuninori Morimoto 		break;
3239a7930ed4SKuninori Morimoto 	case 0x10:
3240a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3241a7930ed4SKuninori Morimoto 		break;
3242a7930ed4SKuninori Morimoto 	case 0x01:
3243a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3244a7930ed4SKuninori Morimoto 		break;
3245a7930ed4SKuninori Morimoto 	default:
3246a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3247a7930ed4SKuninori Morimoto 		break;
3248a7930ed4SKuninori Morimoto 	}
3249a7930ed4SKuninori Morimoto 
3250a7930ed4SKuninori Morimoto 	/*
3251a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3252a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3253a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3254a7930ed4SKuninori Morimoto 	 */
3255a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3256a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3257389cb834SJyri Sarha 	if (bit && bitclkmaster)
3258389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3259a7930ed4SKuninori Morimoto 
3260a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3261a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3262389cb834SJyri Sarha 	if (frame && framemaster)
3263389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3264a7930ed4SKuninori Morimoto 
3265a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3266a7930ed4SKuninori Morimoto 	case 0x11:
3267a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3268a7930ed4SKuninori Morimoto 		break;
3269a7930ed4SKuninori Morimoto 	case 0x10:
3270a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3271a7930ed4SKuninori Morimoto 		break;
3272a7930ed4SKuninori Morimoto 	case 0x01:
3273a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3274a7930ed4SKuninori Morimoto 		break;
3275a7930ed4SKuninori Morimoto 	default:
3276a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3277a7930ed4SKuninori Morimoto 		break;
3278a7930ed4SKuninori Morimoto 	}
3279a7930ed4SKuninori Morimoto 
3280a7930ed4SKuninori Morimoto 	return format;
3281a7930ed4SKuninori Morimoto }
3282a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3283a7930ed4SKuninori Morimoto 
3284a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
3285a180e8b9SKuninori Morimoto {
328609d4cc03SKuninori Morimoto 	struct snd_soc_component *component;
3287c1e230f0SKuninori Morimoto 	struct snd_soc_dai_link_component dlc;
3288a180e8b9SKuninori Morimoto 	int ret;
3289a180e8b9SKuninori Morimoto 
3290c1e230f0SKuninori Morimoto 	dlc.of_node	= of_graph_get_port_parent(ep);
3291c1e230f0SKuninori Morimoto 	dlc.name	= NULL;
3292a180e8b9SKuninori Morimoto 	/*
3293a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
3294a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
3295a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
3296a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
3297a180e8b9SKuninori Morimoto 	 */
3298a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
3299a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
3300c1e230f0SKuninori Morimoto 	component = soc_find_component(&dlc);
33012c7b1704SKuninori Morimoto 	if (component)
33022c7b1704SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_id(component, ep);
3303a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
3304a180e8b9SKuninori Morimoto 
3305c1e230f0SKuninori Morimoto 	of_node_put(dlc.of_node);
3306c0a480d1STony Lindgren 
3307a180e8b9SKuninori Morimoto 	return ret;
3308a180e8b9SKuninori Morimoto }
3309a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3310a180e8b9SKuninori Morimoto 
33111ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args,
3312cb470087SKuninori Morimoto 				const char **dai_name)
3313cb470087SKuninori Morimoto {
3314cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
33153e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
331693b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3317cb470087SKuninori Morimoto 
3318cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3319368dee94SKuninori Morimoto 	for_each_component(pos) {
3320c0834440SKuninori Morimoto 		component_of_node = soc_component_to_node(pos);
33213e0aa8d8SJyri Sarha 
33223e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
3323cb470087SKuninori Morimoto 			continue;
3324cb470087SKuninori Morimoto 
3325a2a34175SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3326a2a34175SKuninori Morimoto 		if (ret == -ENOTSUPP) {
332758bf4179SKuninori Morimoto 			struct snd_soc_dai *dai;
33286833c452SKuninori Morimoto 			int id = -1;
33296833c452SKuninori Morimoto 
333093b0f3eeSJean-Francois Moine 			switch (args->args_count) {
33316833c452SKuninori Morimoto 			case 0:
33326833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
33336833c452SKuninori Morimoto 				break;
33346833c452SKuninori Morimoto 			case 1:
333593b0f3eeSJean-Francois Moine 				id = args->args[0];
33366833c452SKuninori Morimoto 				break;
33376833c452SKuninori Morimoto 			default:
33386833c452SKuninori Morimoto 				/* not supported */
3339cb470087SKuninori Morimoto 				break;
3340cb470087SKuninori Morimoto 			}
3341cb470087SKuninori Morimoto 
33426833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
33436833c452SKuninori Morimoto 				ret = -EINVAL;
33443dcba280SNicolin Chen 				continue;
33456833c452SKuninori Morimoto 			}
3346e41975edSXiubo Li 
3347e41975edSXiubo Li 			ret = 0;
3348e41975edSXiubo Li 
334958bf4179SKuninori Morimoto 			/* find target DAI */
335015a0c645SKuninori Morimoto 			for_each_component_dais(pos, dai) {
335158bf4179SKuninori Morimoto 				if (id == 0)
335258bf4179SKuninori Morimoto 					break;
335358bf4179SKuninori Morimoto 				id--;
335458bf4179SKuninori Morimoto 			}
335558bf4179SKuninori Morimoto 
335658bf4179SKuninori Morimoto 			*dai_name = dai->driver->name;
3357e41975edSXiubo Li 			if (!*dai_name)
3358e41975edSXiubo Li 				*dai_name = pos->name;
33596833c452SKuninori Morimoto 		}
33606833c452SKuninori Morimoto 
3361cb470087SKuninori Morimoto 		break;
3362cb470087SKuninori Morimoto 	}
3363cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
336493b0f3eeSJean-Francois Moine 	return ret;
336593b0f3eeSJean-Francois Moine }
33661ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
336793b0f3eeSJean-Francois Moine 
336893b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
336993b0f3eeSJean-Francois Moine 			    const char **dai_name)
337093b0f3eeSJean-Francois Moine {
337193b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
337293b0f3eeSJean-Francois Moine 	int ret;
337393b0f3eeSJean-Francois Moine 
337493b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
337593b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
337693b0f3eeSJean-Francois Moine 	if (ret)
337793b0f3eeSJean-Francois Moine 		return ret;
337893b0f3eeSJean-Francois Moine 
337993b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3380cb470087SKuninori Morimoto 
3381cb470087SKuninori Morimoto 	of_node_put(args.np);
3382cb470087SKuninori Morimoto 
3383cb470087SKuninori Morimoto 	return ret;
3384cb470087SKuninori Morimoto }
3385cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3386cb470087SKuninori Morimoto 
338793b0f3eeSJean-Francois Moine /*
338894685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
338994685763SSylwester Nawrocki  * @dai_link: DAI link
339094685763SSylwester Nawrocki  *
339194685763SSylwester Nawrocki  * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
339294685763SSylwester Nawrocki  */
339394685763SSylwester Nawrocki void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
339494685763SSylwester Nawrocki {
33953db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *component;
339694685763SSylwester Nawrocki 	int index;
339794685763SSylwester Nawrocki 
33983db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
339994685763SSylwester Nawrocki 		if (!component->of_node)
340094685763SSylwester Nawrocki 			break;
340194685763SSylwester Nawrocki 		of_node_put(component->of_node);
340294685763SSylwester Nawrocki 		component->of_node = NULL;
340394685763SSylwester Nawrocki 	}
340494685763SSylwester Nawrocki }
340594685763SSylwester Nawrocki EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
340694685763SSylwester Nawrocki 
340794685763SSylwester Nawrocki /*
340893b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
340993b0f3eeSJean-Francois Moine  * @dev: Card device
341093b0f3eeSJean-Francois Moine  * @of_node: Device node
341193b0f3eeSJean-Francois Moine  * @dai_link: DAI link
341293b0f3eeSJean-Francois Moine  *
341393b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
341493b0f3eeSJean-Francois Moine  * 'sound-dai'.
341593b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
341694685763SSylwester Nawrocki  * The device nodes in the array (of_node) must be dereferenced by calling
341794685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs() on @dai_link.
341893b0f3eeSJean-Francois Moine  *
341993b0f3eeSJean-Francois Moine  * Returns 0 for success
342093b0f3eeSJean-Francois Moine  */
342193b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
342293b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
342393b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
342493b0f3eeSJean-Francois Moine {
342593b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
342693b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
342793b0f3eeSJean-Francois Moine 	char *name;
342893b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
342993b0f3eeSJean-Francois Moine 
343093b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
343193b0f3eeSJean-Francois Moine 	name = "sound-dai";
343293b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
343393b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
343493b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
343593b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
343693b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
343793b0f3eeSJean-Francois Moine 		else
343893b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
343993b0f3eeSJean-Francois Moine 		return num_codecs;
344093b0f3eeSJean-Francois Moine 	}
3441a86854d0SKees Cook 	component = devm_kcalloc(dev,
3442a86854d0SKees Cook 				 num_codecs, sizeof(*component),
344393b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
344493b0f3eeSJean-Francois Moine 	if (!component)
344593b0f3eeSJean-Francois Moine 		return -ENOMEM;
344693b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
344793b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
344893b0f3eeSJean-Francois Moine 
344993b0f3eeSJean-Francois Moine 	/* Parse the list */
34503db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
345193b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
345293b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
345393b0f3eeSJean-Francois Moine 						 index, &args);
345493b0f3eeSJean-Francois Moine 		if (ret)
345593b0f3eeSJean-Francois Moine 			goto err;
345693b0f3eeSJean-Francois Moine 		component->of_node = args.np;
345793b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
345893b0f3eeSJean-Francois Moine 		if (ret < 0)
345993b0f3eeSJean-Francois Moine 			goto err;
346093b0f3eeSJean-Francois Moine 	}
346193b0f3eeSJean-Francois Moine 	return 0;
346293b0f3eeSJean-Francois Moine err:
346394685763SSylwester Nawrocki 	snd_soc_of_put_dai_link_codecs(dai_link);
346493b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
346593b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
346693b0f3eeSJean-Francois Moine 	return ret;
346793b0f3eeSJean-Francois Moine }
346893b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
346993b0f3eeSJean-Francois Moine 
3470c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3471db2a4165SFrank Mandarino {
34726553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
3473fb257897SMark Brown 	snd_soc_util_init();
3474fb257897SMark Brown 
3475db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3476db2a4165SFrank Mandarino }
34774abe8e16SMark Brown module_init(snd_soc_init);
3478db2a4165SFrank Mandarino 
34797d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3480db2a4165SFrank Mandarino {
3481fb257897SMark Brown 	snd_soc_util_exit();
34826553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3483fb257897SMark Brown 
3484db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3485db2a4165SFrank Mandarino }
3486db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3487db2a4165SFrank Mandarino 
3488db2a4165SFrank Mandarino /* Module information */
3489d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3490db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3491db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
34928b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3493