xref: /openbmc/linux/sound/soc/soc-core.c (revision c1e230f0f7a3cc640317aa78389258a2fa54bcde)
1873486edSKuninori Morimoto // SPDX-License-Identifier: GPL-2.0+
2873486edSKuninori Morimoto //
3873486edSKuninori Morimoto // soc-core.c  --  ALSA SoC Audio Layer
4873486edSKuninori Morimoto //
5873486edSKuninori Morimoto // Copyright 2005 Wolfson Microelectronics PLC.
6873486edSKuninori Morimoto // Copyright 2005 Openedhand Ltd.
7873486edSKuninori Morimoto // Copyright (C) 2010 Slimlogic Ltd.
8873486edSKuninori Morimoto // Copyright (C) 2010 Texas Instruments Inc.
9873486edSKuninori Morimoto //
10873486edSKuninori Morimoto // Author: Liam Girdwood <lrg@slimlogic.co.uk>
11873486edSKuninori Morimoto //         with code, comments and ideas from :-
12873486edSKuninori Morimoto //         Richard Purdie <richard@openedhand.com>
13873486edSKuninori Morimoto //
14873486edSKuninori Morimoto //  TODO:
15873486edSKuninori Morimoto //   o Add hw rules to enforce rates, etc.
16873486edSKuninori Morimoto //   o More testing with other codecs/machines.
17873486edSKuninori Morimoto //   o Add more codecs and platforms to ensure good API coverage.
18873486edSKuninori Morimoto //   o Support TDM on PCM and I2S
19db2a4165SFrank Mandarino 
20db2a4165SFrank Mandarino #include <linux/module.h>
21db2a4165SFrank Mandarino #include <linux/moduleparam.h>
22db2a4165SFrank Mandarino #include <linux/init.h>
23db2a4165SFrank Mandarino #include <linux/delay.h>
24db2a4165SFrank Mandarino #include <linux/pm.h>
25db2a4165SFrank Mandarino #include <linux/bitops.h>
2612ef193dSTroy Kisky #include <linux/debugfs.h>
27db2a4165SFrank Mandarino #include <linux/platform_device.h>
28741a509fSMarkus Pargmann #include <linux/pinctrl/consumer.h>
29f0e8ed85SMark Brown #include <linux/ctype.h>
305a0e3ad6STejun Heo #include <linux/slab.h>
31bec4fa05SStephen Warren #include <linux/of.h>
32a180e8b9SKuninori Morimoto #include <linux/of_graph.h>
33345233d7SLiam Girdwood #include <linux/dmi.h>
34db2a4165SFrank Mandarino #include <sound/core.h>
353028eb8cSMark Brown #include <sound/jack.h>
36db2a4165SFrank Mandarino #include <sound/pcm.h>
37db2a4165SFrank Mandarino #include <sound/pcm_params.h>
38db2a4165SFrank Mandarino #include <sound/soc.h>
3901d7584cSLiam Girdwood #include <sound/soc-dpcm.h>
408a978234SLiam Girdwood #include <sound/soc-topology.h>
41db2a4165SFrank Mandarino #include <sound/initval.h>
42db2a4165SFrank Mandarino 
43a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
44a8b1d34fSMark Brown #include <trace/events/asoc.h>
45a8b1d34fSMark Brown 
46f0fba2adSLiam Girdwood #define NAME_SIZE	32
47f0fba2adSLiam Girdwood 
48384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS
498a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root;
508a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
51384c89e2SMark Brown #endif
52384c89e2SMark Brown 
53c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
54030e79f6SKuninori Morimoto static LIST_HEAD(component_list);
55e894efefSSrinivas Kandagatla static LIST_HEAD(unbind_card_list);
56c5af3a2eSMark Brown 
57368dee94SKuninori Morimoto #define for_each_component(component)			\
58368dee94SKuninori Morimoto 	list_for_each_entry(component, &component_list, list)
59368dee94SKuninori Morimoto 
60db2a4165SFrank Mandarino /*
61587c9844SKuninori Morimoto  * This is used if driver don't need to have CPU/Codec/Platform
62587c9844SKuninori Morimoto  * dai_link. see soc.h
63587c9844SKuninori Morimoto  */
64587c9844SKuninori Morimoto struct snd_soc_dai_link_component null_dailink_component[0];
65587c9844SKuninori Morimoto EXPORT_SYMBOL_GPL(null_dailink_component);
66587c9844SKuninori Morimoto 
67587c9844SKuninori Morimoto /*
68db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
69db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
70db2a4165SFrank Mandarino  * between two audio tracks.
71db2a4165SFrank Mandarino  */
72db2a4165SFrank Mandarino static int pmdown_time = 5000;
73db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
74db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
75db2a4165SFrank Mandarino 
762c7b696aSMarcel Ziswiler /*
772c7b696aSMarcel Ziswiler  * If a DMI filed contain strings in this blacklist (e.g.
7898faf436SMengdong Lin  * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
7998faf436SMengdong Lin  * as invalid and dropped when setting the card long name from DMI info.
8098faf436SMengdong Lin  */
8198faf436SMengdong Lin static const char * const dmi_blacklist[] = {
8298faf436SMengdong Lin 	"To be filled by OEM",
8398faf436SMengdong Lin 	"TBD by OEM",
8498faf436SMengdong Lin 	"Default String",
8598faf436SMengdong Lin 	"Board Manufacturer",
8698faf436SMengdong Lin 	"Board Vendor Name",
8798faf436SMengdong Lin 	"Board Product Name",
8898faf436SMengdong Lin 	NULL,	/* terminator */
8998faf436SMengdong Lin };
9098faf436SMengdong Lin 
91dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
92dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
93dbe21408SMark Brown {
9436ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
95dbe21408SMark Brown 
96f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
97dbe21408SMark Brown }
98dbe21408SMark Brown 
99dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev,
100dbe21408SMark Brown 			       struct device_attribute *attr,
101dbe21408SMark Brown 			       const char *buf, size_t count)
102dbe21408SMark Brown {
10336ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
104c593b520SMark Brown 	int ret;
105dbe21408SMark Brown 
106b785a492SJingoo Han 	ret = kstrtol(buf, 10, &rtd->pmdown_time);
107c593b520SMark Brown 	if (ret)
108c593b520SMark Brown 		return ret;
109dbe21408SMark Brown 
110dbe21408SMark Brown 	return count;
111dbe21408SMark Brown }
112dbe21408SMark Brown 
113dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
114dbe21408SMark Brown 
115d29697dcSTakashi Iwai static struct attribute *soc_dev_attrs[] = {
116d29697dcSTakashi Iwai 	&dev_attr_pmdown_time.attr,
117d29697dcSTakashi Iwai 	NULL
118d29697dcSTakashi Iwai };
119d29697dcSTakashi Iwai 
120d29697dcSTakashi Iwai static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
121d29697dcSTakashi Iwai 				       struct attribute *attr, int idx)
122d29697dcSTakashi Iwai {
123d29697dcSTakashi Iwai 	struct device *dev = kobj_to_dev(kobj);
124d29697dcSTakashi Iwai 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
125d29697dcSTakashi Iwai 
126d29697dcSTakashi Iwai 	if (attr == &dev_attr_pmdown_time.attr)
127d29697dcSTakashi Iwai 		return attr->mode; /* always visible */
1283b6eed8dSKuninori Morimoto 	return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
129d29697dcSTakashi Iwai }
130d29697dcSTakashi Iwai 
131d29697dcSTakashi Iwai static const struct attribute_group soc_dapm_dev_group = {
132d29697dcSTakashi Iwai 	.attrs = soc_dapm_dev_attrs,
133d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
134d29697dcSTakashi Iwai };
135d29697dcSTakashi Iwai 
136f7e73b26SMark Brown static const struct attribute_group soc_dev_group = {
137d29697dcSTakashi Iwai 	.attrs = soc_dev_attrs,
138d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
139d29697dcSTakashi Iwai };
140d29697dcSTakashi Iwai 
141d29697dcSTakashi Iwai static const struct attribute_group *soc_dev_attr_groups[] = {
142d29697dcSTakashi Iwai 	&soc_dapm_dev_group,
143f7e73b26SMark Brown 	&soc_dev_group,
144d29697dcSTakashi Iwai 	NULL
145d29697dcSTakashi Iwai };
146d29697dcSTakashi Iwai 
1472624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
14881c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component)
149e73f3de5SRussell King {
1506553bf06SLars-Peter Clausen 	if (!component->card->debugfs_card_root)
1516553bf06SLars-Peter Clausen 		return;
1526553bf06SLars-Peter Clausen 
15381c7cfd1SLars-Peter Clausen 	if (component->debugfs_prefix) {
15481c7cfd1SLars-Peter Clausen 		char *name;
155e73f3de5SRussell King 
15681c7cfd1SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s:%s",
15781c7cfd1SLars-Peter Clausen 			component->debugfs_prefix, component->name);
15881c7cfd1SLars-Peter Clausen 		if (name) {
15981c7cfd1SLars-Peter Clausen 			component->debugfs_root = debugfs_create_dir(name,
16081c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
16181c7cfd1SLars-Peter Clausen 			kfree(name);
16281c7cfd1SLars-Peter Clausen 		}
16381c7cfd1SLars-Peter Clausen 	} else {
16481c7cfd1SLars-Peter Clausen 		component->debugfs_root = debugfs_create_dir(component->name,
16581c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
166e73f3de5SRussell King 	}
167e73f3de5SRussell King 
16881c7cfd1SLars-Peter Clausen 	if (!component->debugfs_root) {
16981c7cfd1SLars-Peter Clausen 		dev_warn(component->dev,
17081c7cfd1SLars-Peter Clausen 			"ASoC: Failed to create component debugfs directory\n");
1712624d5faSMark Brown 		return;
1722624d5faSMark Brown 	}
1732624d5faSMark Brown 
17481c7cfd1SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
17581c7cfd1SLars-Peter Clausen 		component->debugfs_root);
17681c7cfd1SLars-Peter Clausen }
17781c7cfd1SLars-Peter Clausen 
17881c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
17981c7cfd1SLars-Peter Clausen {
18081c7cfd1SLars-Peter Clausen 	debugfs_remove_recursive(component->debugfs_root);
18181c7cfd1SLars-Peter Clausen }
18281c7cfd1SLars-Peter Clausen 
183c15b2a1dSPeng Donglin static int dai_list_show(struct seq_file *m, void *v)
184f3208780SMark Brown {
1851438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
186f3208780SMark Brown 	struct snd_soc_dai *dai;
187f3208780SMark Brown 
18834e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
18934e81ab4SLars-Peter Clausen 
190368dee94SKuninori Morimoto 	for_each_component(component)
19115a0c645SKuninori Morimoto 		for_each_component_dais(component, dai)
192700c17caSDonglin Peng 			seq_printf(m, "%s\n", dai->name);
193f3208780SMark Brown 
19434e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
19534e81ab4SLars-Peter Clausen 
196700c17caSDonglin Peng 	return 0;
197700c17caSDonglin Peng }
198c15b2a1dSPeng Donglin DEFINE_SHOW_ATTRIBUTE(dai_list);
199f3208780SMark Brown 
200db795f9bSKuninori Morimoto static int component_list_show(struct seq_file *m, void *v)
201db795f9bSKuninori Morimoto {
202db795f9bSKuninori Morimoto 	struct snd_soc_component *component;
203db795f9bSKuninori Morimoto 
204db795f9bSKuninori Morimoto 	mutex_lock(&client_mutex);
205db795f9bSKuninori Morimoto 
206368dee94SKuninori Morimoto 	for_each_component(component)
207db795f9bSKuninori Morimoto 		seq_printf(m, "%s\n", component->name);
208db795f9bSKuninori Morimoto 
209db795f9bSKuninori Morimoto 	mutex_unlock(&client_mutex);
210db795f9bSKuninori Morimoto 
211db795f9bSKuninori Morimoto 	return 0;
212db795f9bSKuninori Morimoto }
213db795f9bSKuninori Morimoto DEFINE_SHOW_ATTRIBUTE(component_list);
214db795f9bSKuninori Morimoto 
215a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
216a6052154SJarkko Nikula {
2176553bf06SLars-Peter Clausen 	if (!snd_soc_debugfs_root)
2186553bf06SLars-Peter Clausen 		return;
2196553bf06SLars-Peter Clausen 
220a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
2218a9dab1aSMark Brown 						     snd_soc_debugfs_root);
2223a45b867SJarkko Nikula 	if (!card->debugfs_card_root) {
223a6052154SJarkko Nikula 		dev_warn(card->dev,
2247c08be84SLothar Waßmann 			 "ASoC: Failed to create card debugfs directory\n");
2253a45b867SJarkko Nikula 		return;
2263a45b867SJarkko Nikula 	}
2273a45b867SJarkko Nikula 
2283a45b867SJarkko Nikula 	card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
2293a45b867SJarkko Nikula 						    card->debugfs_card_root,
2303a45b867SJarkko Nikula 						    &card->pop_time);
2313a45b867SJarkko Nikula 	if (!card->debugfs_pop_time)
2323a45b867SJarkko Nikula 		dev_warn(card->dev,
233f110bfc7SLiam Girdwood 			 "ASoC: Failed to create pop time debugfs file\n");
234a6052154SJarkko Nikula }
235a6052154SJarkko Nikula 
236a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
237a6052154SJarkko Nikula {
23829040d1aSKuninori Morimoto 	if (!card->debugfs_card_root)
23929040d1aSKuninori Morimoto 		return;
240a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
24129040d1aSKuninori Morimoto 	card->debugfs_card_root = NULL;
242a6052154SJarkko Nikula }
243a6052154SJarkko Nikula 
2446553bf06SLars-Peter Clausen static void snd_soc_debugfs_init(void)
2456553bf06SLars-Peter Clausen {
2466553bf06SLars-Peter Clausen 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
247d9a02c55SFabio Estevam 	if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) {
2486553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create debugfs directory\n");
2496553bf06SLars-Peter Clausen 		snd_soc_debugfs_root = NULL;
2506553bf06SLars-Peter Clausen 		return;
2516553bf06SLars-Peter Clausen 	}
2526553bf06SLars-Peter Clausen 
2536553bf06SLars-Peter Clausen 	if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
2546553bf06SLars-Peter Clausen 				 &dai_list_fops))
2556553bf06SLars-Peter Clausen 		pr_warn("ASoC: Failed to create DAI list debugfs file\n");
256db795f9bSKuninori Morimoto 
257db795f9bSKuninori Morimoto 	if (!debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
258db795f9bSKuninori Morimoto 				 &component_list_fops))
259db795f9bSKuninori Morimoto 		pr_warn("ASoC: Failed to create component list debugfs file\n");
2606553bf06SLars-Peter Clausen }
2616553bf06SLars-Peter Clausen 
2626553bf06SLars-Peter Clausen static void snd_soc_debugfs_exit(void)
2636553bf06SLars-Peter Clausen {
2646553bf06SLars-Peter Clausen 	debugfs_remove_recursive(snd_soc_debugfs_root);
2656553bf06SLars-Peter Clausen }
2666553bf06SLars-Peter Clausen 
2672624d5faSMark Brown #else
2682624d5faSMark Brown 
26981c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs(
27081c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
2712624d5faSMark Brown {
2722624d5faSMark Brown }
2732624d5faSMark Brown 
27481c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs(
27581c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
276731f1ab2SSebastien Guiriec {
277731f1ab2SSebastien Guiriec }
278731f1ab2SSebastien Guiriec 
279b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
280b95fccbcSAxel Lin {
281b95fccbcSAxel Lin }
282b95fccbcSAxel Lin 
283b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
284b95fccbcSAxel Lin {
285b95fccbcSAxel Lin }
2866553bf06SLars-Peter Clausen 
2876553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_init(void)
2886553bf06SLars-Peter Clausen {
2896553bf06SLars-Peter Clausen }
2906553bf06SLars-Peter Clausen 
2916553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_exit(void)
2926553bf06SLars-Peter Clausen {
2936553bf06SLars-Peter Clausen }
2946553bf06SLars-Peter Clausen 
2952624d5faSMark Brown #endif
2962624d5faSMark Brown 
297a0ac4411SKuninori Morimoto static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
298a0ac4411SKuninori Morimoto 			      struct snd_soc_component *component)
299a0ac4411SKuninori Morimoto {
300a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
301a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *new_rtdcom;
302a0ac4411SKuninori Morimoto 
303a0ac4411SKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
304a0ac4411SKuninori Morimoto 		/* already connected */
305a0ac4411SKuninori Morimoto 		if (rtdcom->component == component)
306a0ac4411SKuninori Morimoto 			return 0;
307a0ac4411SKuninori Morimoto 	}
308a0ac4411SKuninori Morimoto 
309a0ac4411SKuninori Morimoto 	new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
310a0ac4411SKuninori Morimoto 	if (!new_rtdcom)
311a0ac4411SKuninori Morimoto 		return -ENOMEM;
312a0ac4411SKuninori Morimoto 
313a0ac4411SKuninori Morimoto 	new_rtdcom->component = component;
314a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&new_rtdcom->list);
315a0ac4411SKuninori Morimoto 
316a0ac4411SKuninori Morimoto 	list_add_tail(&new_rtdcom->list, &rtd->component_list);
317a0ac4411SKuninori Morimoto 
318a0ac4411SKuninori Morimoto 	return 0;
319a0ac4411SKuninori Morimoto }
320a0ac4411SKuninori Morimoto 
321a0ac4411SKuninori Morimoto static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
322a0ac4411SKuninori Morimoto {
323a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
324a0ac4411SKuninori Morimoto 
325a0ac4411SKuninori Morimoto 	for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
326a0ac4411SKuninori Morimoto 		kfree(rtdcom1);
327a0ac4411SKuninori Morimoto 
328a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
329a0ac4411SKuninori Morimoto }
330a0ac4411SKuninori Morimoto 
331a0ac4411SKuninori Morimoto struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
332a0ac4411SKuninori Morimoto 						const char *driver_name)
333a0ac4411SKuninori Morimoto {
334a0ac4411SKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
335a0ac4411SKuninori Morimoto 
336971da24cSKuninori Morimoto 	if (!driver_name)
337971da24cSKuninori Morimoto 		return NULL;
338971da24cSKuninori Morimoto 
339a0ac4411SKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
340971da24cSKuninori Morimoto 		const char *component_name = rtdcom->component->driver->name;
341971da24cSKuninori Morimoto 
342971da24cSKuninori Morimoto 		if (!component_name)
343971da24cSKuninori Morimoto 			continue;
344971da24cSKuninori Morimoto 
345971da24cSKuninori Morimoto 		if ((component_name == driver_name) ||
346971da24cSKuninori Morimoto 		    strcmp(component_name, driver_name) == 0)
347a0ac4411SKuninori Morimoto 			return rtdcom->component;
348a0ac4411SKuninori Morimoto 	}
349a0ac4411SKuninori Morimoto 
350a0ac4411SKuninori Morimoto 	return NULL;
351a0ac4411SKuninori Morimoto }
352031734b7SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
353a0ac4411SKuninori Morimoto 
35447c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
35547c88fffSLiam Girdwood 		const char *dai_link, int stream)
35647c88fffSLiam Girdwood {
3571a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
35847c88fffSLiam Girdwood 
359bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
3601a497983SMengdong Lin 		if (rtd->dai_link->no_pcm &&
3611a497983SMengdong Lin 			!strcmp(rtd->dai_link->name, dai_link))
3621a497983SMengdong Lin 			return rtd->pcm->streams[stream].substream;
36347c88fffSLiam Girdwood 	}
364f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
36547c88fffSLiam Girdwood 	return NULL;
36647c88fffSLiam Girdwood }
36747c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
36847c88fffSLiam Girdwood 
36975ab9eb6SKuninori Morimoto static const struct snd_soc_ops null_snd_soc_ops;
37075ab9eb6SKuninori Morimoto 
3711a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
3721a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
3731a497983SMengdong Lin {
3741a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
3751a497983SMengdong Lin 
3761a497983SMengdong Lin 	rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
3771a497983SMengdong Lin 	if (!rtd)
3781a497983SMengdong Lin 		return NULL;
3791a497983SMengdong Lin 
380a0ac4411SKuninori Morimoto 	INIT_LIST_HEAD(&rtd->component_list);
3811a497983SMengdong Lin 	rtd->card = card;
3821a497983SMengdong Lin 	rtd->dai_link = dai_link;
38375ab9eb6SKuninori Morimoto 	if (!rtd->dai_link->ops)
38475ab9eb6SKuninori Morimoto 		rtd->dai_link->ops = &null_snd_soc_ops;
38575ab9eb6SKuninori Morimoto 
3866396bb22SKees Cook 	rtd->codec_dais = kcalloc(dai_link->num_codecs,
3876396bb22SKees Cook 					sizeof(struct snd_soc_dai *),
3881a497983SMengdong Lin 					GFP_KERNEL);
3891a497983SMengdong Lin 	if (!rtd->codec_dais) {
3901a497983SMengdong Lin 		kfree(rtd);
3911a497983SMengdong Lin 		return NULL;
3921a497983SMengdong Lin 	}
3931a497983SMengdong Lin 
3941a497983SMengdong Lin 	return rtd;
3951a497983SMengdong Lin }
3961a497983SMengdong Lin 
3971a497983SMengdong Lin static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
3981a497983SMengdong Lin {
3991a497983SMengdong Lin 	kfree(rtd->codec_dais);
400a0ac4411SKuninori Morimoto 	snd_soc_rtdcom_del_all(rtd);
4011a497983SMengdong Lin 	kfree(rtd);
4021a497983SMengdong Lin }
4031a497983SMengdong Lin 
4041a497983SMengdong Lin static void soc_add_pcm_runtime(struct snd_soc_card *card,
4051a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd)
4061a497983SMengdong Lin {
4071a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
4081a497983SMengdong Lin 	rtd->num = card->num_rtd;
4091a497983SMengdong Lin 	card->num_rtd++;
4101a497983SMengdong Lin }
4111a497983SMengdong Lin 
4121a497983SMengdong Lin static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
4131a497983SMengdong Lin {
4141a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, *_rtd;
4151a497983SMengdong Lin 
416bcb1fd1fSKuninori Morimoto 	for_each_card_rtds_safe(card, rtd, _rtd) {
4171a497983SMengdong Lin 		list_del(&rtd->list);
4181a497983SMengdong Lin 		soc_free_pcm_runtime(rtd);
4191a497983SMengdong Lin 	}
4201a497983SMengdong Lin 
4211a497983SMengdong Lin 	card->num_rtd = 0;
4221a497983SMengdong Lin }
4231a497983SMengdong Lin 
42447c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
42547c88fffSLiam Girdwood 		const char *dai_link)
42647c88fffSLiam Girdwood {
4271a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
42847c88fffSLiam Girdwood 
429bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4301a497983SMengdong Lin 		if (!strcmp(rtd->dai_link->name, dai_link))
4311a497983SMengdong Lin 			return rtd;
43247c88fffSLiam Girdwood 	}
433f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
43447c88fffSLiam Girdwood 	return NULL;
43547c88fffSLiam Girdwood }
43647c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
43747c88fffSLiam Girdwood 
43865462e44SKuninori Morimoto static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
43965462e44SKuninori Morimoto {
44065462e44SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
44165462e44SKuninori Morimoto 
44265462e44SKuninori Morimoto 	for_each_card_rtds(card, rtd)
44365462e44SKuninori Morimoto 		flush_delayed_work(&rtd->delayed_work);
44465462e44SKuninori Morimoto }
44565462e44SKuninori Morimoto 
4469d58a077SRichard Fitzgerald static void codec2codec_close_delayed_work(struct work_struct *work)
4479d58a077SRichard Fitzgerald {
4482c7b696aSMarcel Ziswiler 	/*
4492c7b696aSMarcel Ziswiler 	 * Currently nothing to do for c2c links
4509d58a077SRichard Fitzgerald 	 * Since c2c links are internal nodes in the DAPM graph and
4519d58a077SRichard Fitzgerald 	 * don't interface with the outside world or application layer
4529d58a077SRichard Fitzgerald 	 * we don't have to do any special handling on close.
4539d58a077SRichard Fitzgerald 	 */
4549d58a077SRichard Fitzgerald }
4559d58a077SRichard Fitzgerald 
4566f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
457db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
4586f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
459db2a4165SFrank Mandarino {
4606f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
461d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
4621a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
4631a497983SMengdong Lin 	int i;
464db2a4165SFrank Mandarino 
465c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
466c5599b87SLars-Peter Clausen 	if (!card->instantiated)
467e3509ff0SDaniel Mack 		return 0;
468e3509ff0SDaniel Mack 
4692c7b696aSMarcel Ziswiler 	/*
4702c7b696aSMarcel Ziswiler 	 * Due to the resume being scheduled into a workqueue we could
4716ed25978SAndy Green 	 * suspend before that's finished - wait for it to complete.
4726ed25978SAndy Green 	 */
473f0fba2adSLiam Girdwood 	snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
4746ed25978SAndy Green 
4756ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
476f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
4776ed25978SAndy Green 
478a00f90f9SMark Brown 	/* mute any active DACs */
479bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4800b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
4813efab7dcSMark Brown 
4821a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4833efab7dcSMark Brown 			continue;
4843efab7dcSMark Brown 
4850b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
48688bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
48788bd870fSBenoit Cousson 
488f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
489f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 1);
490db2a4165SFrank Mandarino 		}
49188bd870fSBenoit Cousson 	}
492db2a4165SFrank Mandarino 
4934ccab3e7SLiam Girdwood 	/* suspend all pcms */
494bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
4951a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
4963efab7dcSMark Brown 			continue;
4973efab7dcSMark Brown 
4981a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
4993efab7dcSMark Brown 	}
5004ccab3e7SLiam Girdwood 
50187506549SMark Brown 	if (card->suspend_pre)
50270b2ac12SMark Brown 		card->suspend_pre(card);
503db2a4165SFrank Mandarino 
504bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5051a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
5063efab7dcSMark Brown 
5071a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5083efab7dcSMark Brown 			continue;
5093efab7dcSMark Brown 
510bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
511f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
512db2a4165SFrank Mandarino 	}
513db2a4165SFrank Mandarino 
51437660b6dSLars-Peter Clausen 	/* close any waiting streams */
51565462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
516db2a4165SFrank Mandarino 
517bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5183efab7dcSMark Brown 
5191a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5203efab7dcSMark Brown 			continue;
5213efab7dcSMark Brown 
5221a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
5237bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_PLAYBACK,
524db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
525f0fba2adSLiam Girdwood 
5261a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
5277bd3a6f3SMark Brown 					  SNDRV_PCM_STREAM_CAPTURE,
528db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_SUSPEND);
529db2a4165SFrank Mandarino 	}
530db2a4165SFrank Mandarino 
5318be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
5328be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
533e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
534e2d32ff6SMark Brown 
5359178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
536f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
5372c7b696aSMarcel Ziswiler 		struct snd_soc_dapm_context *dapm =
5382c7b696aSMarcel Ziswiler 				snd_soc_component_get_dapm(component);
539d9fc4063SKuninori Morimoto 
5402c7b696aSMarcel Ziswiler 		/*
5412c7b696aSMarcel Ziswiler 		 * If there are paths active then the COMPONENT will be held
5422c7b696aSMarcel Ziswiler 		 * with bias _ON and should not be suspended.
5432c7b696aSMarcel Ziswiler 		 */
5449178feb4SKuninori Morimoto 		if (!component->suspended) {
5454890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
5461547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
547125a25daSMark Brown 				/*
5489178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
549125a25daSMark Brown 				 * bias off then being in STANDBY
550125a25daSMark Brown 				 * means it's doing something,
551125a25daSMark Brown 				 * otherwise fall through.
552125a25daSMark Brown 				 */
5534890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
5549178feb4SKuninori Morimoto 					dev_dbg(component->dev,
55510e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
556125a25daSMark Brown 					break;
557125a25daSMark Brown 				}
5581a12d5dcSGustavo A. R. Silva 				/* fall through */
559a8093297SLars-Peter Clausen 
5601547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
561999f7f5aSKuninori Morimoto 				if (component->driver->suspend)
562999f7f5aSKuninori Morimoto 					component->driver->suspend(component);
5639178feb4SKuninori Morimoto 				component->suspended = 1;
5649178feb4SKuninori Morimoto 				if (component->regmap)
5659178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
566988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
5679178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
5681547aba9SMark Brown 				break;
5691547aba9SMark Brown 			default:
5709178feb4SKuninori Morimoto 				dev_dbg(component->dev,
5719178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
5721547aba9SMark Brown 				break;
5731547aba9SMark Brown 			}
5741547aba9SMark Brown 		}
575f0fba2adSLiam Girdwood 	}
576db2a4165SFrank Mandarino 
577bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5781a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
5793efab7dcSMark Brown 
5801a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5813efab7dcSMark Brown 			continue;
5823efab7dcSMark Brown 
583bc263214SLars-Peter Clausen 		if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
584f0fba2adSLiam Girdwood 			cpu_dai->driver->suspend(cpu_dai);
585988e8cc4SNicolin Chen 
586988e8cc4SNicolin Chen 		/* deactivate pins to sleep state */
587988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
588db2a4165SFrank Mandarino 	}
589db2a4165SFrank Mandarino 
59087506549SMark Brown 	if (card->suspend_post)
59170b2ac12SMark Brown 		card->suspend_post(card);
592db2a4165SFrank Mandarino 
593db2a4165SFrank Mandarino 	return 0;
594db2a4165SFrank Mandarino }
5956f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
596db2a4165SFrank Mandarino 
5972c7b696aSMarcel Ziswiler /*
5982c7b696aSMarcel Ziswiler  * deferred resume work, so resume can complete before we finished
5996ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6006ed25978SAndy Green  */
6016ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
602db2a4165SFrank Mandarino {
603f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
6042c7b696aSMarcel Ziswiler 			container_of(work, struct snd_soc_card,
6052c7b696aSMarcel Ziswiler 				     deferred_resume_work);
6061a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
607d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
6081a497983SMengdong Lin 	int i;
609db2a4165SFrank Mandarino 
6102c7b696aSMarcel Ziswiler 	/*
6112c7b696aSMarcel Ziswiler 	 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6126ed25978SAndy Green 	 * so userspace apps are blocked from touching us
6136ed25978SAndy Green 	 */
6146ed25978SAndy Green 
615f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
6166ed25978SAndy Green 
6179949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
618f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
6199949788bSMark Brown 
62087506549SMark Brown 	if (card->resume_pre)
62170b2ac12SMark Brown 		card->resume_pre(card);
622db2a4165SFrank Mandarino 
623bc263214SLars-Peter Clausen 	/* resume control bus DAIs */
624bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6251a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6263efab7dcSMark Brown 
6271a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6283efab7dcSMark Brown 			continue;
6293efab7dcSMark Brown 
630bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
631f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
632db2a4165SFrank Mandarino 	}
633db2a4165SFrank Mandarino 
634f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
6359178feb4SKuninori Morimoto 		if (component->suspended) {
636999f7f5aSKuninori Morimoto 			if (component->driver->resume)
637999f7f5aSKuninori Morimoto 				component->driver->resume(component);
6389178feb4SKuninori Morimoto 			component->suspended = 0;
6391547aba9SMark Brown 		}
640f0fba2adSLiam Girdwood 	}
641db2a4165SFrank Mandarino 
642bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6433efab7dcSMark Brown 
6441a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6453efab7dcSMark Brown 			continue;
6463efab7dcSMark Brown 
6471a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
648d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_PLAYBACK,
649db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
650f0fba2adSLiam Girdwood 
6511a497983SMengdong Lin 		snd_soc_dapm_stream_event(rtd,
652d9b0951bSLiam Girdwood 					  SNDRV_PCM_STREAM_CAPTURE,
653db2a4165SFrank Mandarino 					  SND_SOC_DAPM_STREAM_RESUME);
654db2a4165SFrank Mandarino 	}
655db2a4165SFrank Mandarino 
6563ff3f64bSMark Brown 	/* unmute any active DACs */
657bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6580b7990e3SKuninori Morimoto 		struct snd_soc_dai *dai;
6593efab7dcSMark Brown 
6601a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6613efab7dcSMark Brown 			continue;
6623efab7dcSMark Brown 
6630b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, dai) {
66488bd870fSBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
66588bd870fSBenoit Cousson 
666f0fba2adSLiam Girdwood 			if (drv->ops->digital_mute && dai->playback_active)
667f0fba2adSLiam Girdwood 				drv->ops->digital_mute(dai, 0);
668db2a4165SFrank Mandarino 		}
66988bd870fSBenoit Cousson 	}
670db2a4165SFrank Mandarino 
671bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6721a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6733efab7dcSMark Brown 
6741a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
6753efab7dcSMark Brown 			continue;
6763efab7dcSMark Brown 
677bc263214SLars-Peter Clausen 		if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
678f0fba2adSLiam Girdwood 			cpu_dai->driver->resume(cpu_dai);
679db2a4165SFrank Mandarino 	}
680db2a4165SFrank Mandarino 
68187506549SMark Brown 	if (card->resume_post)
68270b2ac12SMark Brown 		card->resume_post(card);
683db2a4165SFrank Mandarino 
684f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
6856ed25978SAndy Green 
6868be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
6878be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
688e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
6891a7aaa58SJeeja KP 
6901a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
6911a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
6926ed25978SAndy Green }
6936ed25978SAndy Green 
6946ed25978SAndy Green /* powers up audio subsystem after a suspend */
6956f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
6966ed25978SAndy Green {
6976f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
698bc263214SLars-Peter Clausen 	bool bus_control = false;
6991a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
70022d251a5SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
70122d251a5SKuninori Morimoto 	int i;
702b9dd94a8SPeter Ujfalusi 
703c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
704c5599b87SLars-Peter Clausen 	if (!card->instantiated)
7055ff1ddf2SEric Miao 		return 0;
7065ff1ddf2SEric Miao 
707988e8cc4SNicolin Chen 	/* activate pins from sleep state */
708bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
70988bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
71088bd870fSBenoit Cousson 
711988e8cc4SNicolin Chen 		if (cpu_dai->active)
712988e8cc4SNicolin Chen 			pinctrl_pm_select_default_state(cpu_dai->dev);
71388bd870fSBenoit Cousson 
71422d251a5SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
715988e8cc4SNicolin Chen 			if (codec_dai->active)
716988e8cc4SNicolin Chen 				pinctrl_pm_select_default_state(codec_dai->dev);
717988e8cc4SNicolin Chen 		}
71888bd870fSBenoit Cousson 	}
719988e8cc4SNicolin Chen 
720bc263214SLars-Peter Clausen 	/*
721bc263214SLars-Peter Clausen 	 * DAIs that also act as the control bus master might have other drivers
722bc263214SLars-Peter Clausen 	 * hanging off them so need to resume immediately. Other drivers don't
723bc263214SLars-Peter Clausen 	 * have that problem and may take a substantial amount of time to resume
72464ab9baaSMark Brown 	 * due to I/O costs and anti-pop so handle them out of line.
72564ab9baaSMark Brown 	 */
726bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
7271a497983SMengdong Lin 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7282c7b696aSMarcel Ziswiler 
729bc263214SLars-Peter Clausen 		bus_control |= cpu_dai->driver->bus_control;
73082e14e8bSStephen Warren 	}
731bc263214SLars-Peter Clausen 	if (bus_control) {
732bc263214SLars-Peter Clausen 		dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
73364ab9baaSMark Brown 		soc_resume_deferred(&card->deferred_resume_work);
73464ab9baaSMark Brown 	} else {
735f110bfc7SLiam Girdwood 		dev_dbg(dev, "ASoC: Scheduling resume work\n");
7366308419aSMark Brown 		if (!schedule_work(&card->deferred_resume_work))
737f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: resume work item may be lost\n");
738f0fba2adSLiam Girdwood 	}
7396ed25978SAndy Green 
740db2a4165SFrank Mandarino 	return 0;
741db2a4165SFrank Mandarino }
7426f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
743db2a4165SFrank Mandarino #else
7446f8ab4acSMark Brown #define snd_soc_suspend NULL
7456f8ab4acSMark Brown #define snd_soc_resume NULL
746db2a4165SFrank Mandarino #endif
747db2a4165SFrank Mandarino 
74885e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = {
74902a06d30SBarry Song };
75002a06d30SBarry Song 
751c0834440SKuninori Morimoto static struct device_node
752c0834440SKuninori Morimoto *soc_component_to_node(struct snd_soc_component *component)
753c0834440SKuninori Morimoto {
754c0834440SKuninori Morimoto 	struct device_node *of_node;
755c0834440SKuninori Morimoto 
756c0834440SKuninori Morimoto 	of_node = component->dev->of_node;
757c0834440SKuninori Morimoto 	if (!of_node && component->dev->parent)
758c0834440SKuninori Morimoto 		of_node = component->dev->parent->of_node;
759c0834440SKuninori Morimoto 
760c0834440SKuninori Morimoto 	return of_node;
761c0834440SKuninori Morimoto }
762c0834440SKuninori Morimoto 
7637d7db5d3SKuninori Morimoto static int snd_soc_is_matching_component(
7647d7db5d3SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc,
7657d7db5d3SKuninori Morimoto 	struct snd_soc_component *component)
7667d7db5d3SKuninori Morimoto {
7677d7db5d3SKuninori Morimoto 	struct device_node *component_of_node;
7687d7db5d3SKuninori Morimoto 
7697d7db5d3SKuninori Morimoto 	if (!dlc)
7707d7db5d3SKuninori Morimoto 		return 0;
7717d7db5d3SKuninori Morimoto 
7727d7db5d3SKuninori Morimoto 	component_of_node = soc_component_to_node(component);
7737d7db5d3SKuninori Morimoto 
7747d7db5d3SKuninori Morimoto 	if (dlc->of_node && component_of_node != dlc->of_node)
7757d7db5d3SKuninori Morimoto 		return 0;
7767d7db5d3SKuninori Morimoto 	if (dlc->name && strcmp(component->name, dlc->name))
7777d7db5d3SKuninori Morimoto 		return 0;
7787d7db5d3SKuninori Morimoto 
7797d7db5d3SKuninori Morimoto 	return 1;
7807d7db5d3SKuninori Morimoto }
7817d7db5d3SKuninori Morimoto 
78265d9361fSLars-Peter Clausen static struct snd_soc_component *soc_find_component(
783*c1e230f0SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc)
78412023a9aSMisael Lopez Cruz {
78565d9361fSLars-Peter Clausen 	struct snd_soc_component *component;
78612023a9aSMisael Lopez Cruz 
78734e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
78834e81ab4SLars-Peter Clausen 
789*c1e230f0SKuninori Morimoto 	for_each_component(component)
790*c1e230f0SKuninori Morimoto 		if (snd_soc_is_matching_component(dlc, component))
79165d9361fSLars-Peter Clausen 			return component;
79212023a9aSMisael Lopez Cruz 
79312023a9aSMisael Lopez Cruz 	return NULL;
79412023a9aSMisael Lopez Cruz }
79512023a9aSMisael Lopez Cruz 
796fbb88b5cSMengdong Lin /**
797fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
798fbb88b5cSMengdong Lin  *
7994958471bSJeffy Chen  * @dlc: name of the DAI or the DAI driver and optional component info to match
800fbb88b5cSMengdong Lin  *
801ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
802fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
803fbb88b5cSMengdong Lin  * should also match if being specified.
804fbb88b5cSMengdong Lin  *
805fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
806fbb88b5cSMengdong Lin  */
807305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
80814621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
80912023a9aSMisael Lopez Cruz {
81014621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
81114621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
81212023a9aSMisael Lopez Cruz 
81334e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
81434e81ab4SLars-Peter Clausen 
81514621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs */
816368dee94SKuninori Morimoto 	for_each_component(component) {
817be6ac0a9SKuninori Morimoto 		if (!snd_soc_is_matching_component(dlc, component))
81812023a9aSMisael Lopez Cruz 			continue;
81915a0c645SKuninori Morimoto 		for_each_component_dais(component, dai) {
8204958471bSJeffy Chen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
8216a6dafdaSJeffy Chen 			    && (!dai->driver->name
8226a6dafdaSJeffy Chen 				|| strcmp(dai->driver->name, dlc->dai_name)))
82314621c7eSLars-Peter Clausen 				continue;
82412023a9aSMisael Lopez Cruz 
82514621c7eSLars-Peter Clausen 			return dai;
82612023a9aSMisael Lopez Cruz 		}
82712023a9aSMisael Lopez Cruz 	}
82812023a9aSMisael Lopez Cruz 
82912023a9aSMisael Lopez Cruz 	return NULL;
83012023a9aSMisael Lopez Cruz }
831305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
83212023a9aSMisael Lopez Cruz 
83317fb1755SMengdong Lin /**
83417fb1755SMengdong Lin  * snd_soc_find_dai_link - Find a DAI link
83517fb1755SMengdong Lin  *
83617fb1755SMengdong Lin  * @card: soc card
83717fb1755SMengdong Lin  * @id: DAI link ID to match
83817fb1755SMengdong Lin  * @name: DAI link name to match, optional
8398abab35fSCharles Keepax  * @stream_name: DAI link stream name to match, optional
84017fb1755SMengdong Lin  *
84117fb1755SMengdong Lin  * This function will search all existing DAI links of the soc card to
84217fb1755SMengdong Lin  * find the link of the same ID. Since DAI links may not have their
84317fb1755SMengdong Lin  * unique ID, so name and stream name should also match if being
84417fb1755SMengdong Lin  * specified.
84517fb1755SMengdong Lin  *
84617fb1755SMengdong Lin  * Return: pointer of DAI link, or NULL if not found.
84717fb1755SMengdong Lin  */
84817fb1755SMengdong Lin struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
84917fb1755SMengdong Lin 					       int id, const char *name,
85017fb1755SMengdong Lin 					       const char *stream_name)
85117fb1755SMengdong Lin {
85217fb1755SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
85317fb1755SMengdong Lin 
85417fb1755SMengdong Lin 	lockdep_assert_held(&client_mutex);
85517fb1755SMengdong Lin 
85698061fdbSKuninori Morimoto 	for_each_card_links_safe(card, link, _link) {
85717fb1755SMengdong Lin 		if (link->id != id)
85817fb1755SMengdong Lin 			continue;
85917fb1755SMengdong Lin 
86017fb1755SMengdong Lin 		if (name && (!link->name || strcmp(name, link->name)))
86117fb1755SMengdong Lin 			continue;
86217fb1755SMengdong Lin 
86317fb1755SMengdong Lin 		if (stream_name && (!link->stream_name
86417fb1755SMengdong Lin 			|| strcmp(stream_name, link->stream_name)))
86517fb1755SMengdong Lin 			continue;
86617fb1755SMengdong Lin 
86717fb1755SMengdong Lin 		return link;
86817fb1755SMengdong Lin 	}
86917fb1755SMengdong Lin 
87017fb1755SMengdong Lin 	return NULL;
87117fb1755SMengdong Lin }
87217fb1755SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
87317fb1755SMengdong Lin 
87449a5ba1cSMengdong Lin static bool soc_is_dai_link_bound(struct snd_soc_card *card,
87549a5ba1cSMengdong Lin 		struct snd_soc_dai_link *dai_link)
876db2a4165SFrank Mandarino {
87749a5ba1cSMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
87849a5ba1cSMengdong Lin 
879bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
88049a5ba1cSMengdong Lin 		if (rtd->dai_link == dai_link)
88149a5ba1cSMengdong Lin 			return true;
88249a5ba1cSMengdong Lin 	}
88349a5ba1cSMengdong Lin 
88449a5ba1cSMengdong Lin 	return false;
88549a5ba1cSMengdong Lin }
88649a5ba1cSMengdong Lin 
8876f2f1ff0SMengdong Lin static int soc_bind_dai_link(struct snd_soc_card *card,
8886f2f1ff0SMengdong Lin 	struct snd_soc_dai_link *dai_link)
889db2a4165SFrank Mandarino {
8901a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
891720734a0SKuninori Morimoto 	struct snd_soc_dai_link_component *codecs;
89290be711eSKuninori Morimoto 	struct snd_soc_component *component;
89388bd870fSBenoit Cousson 	int i;
894db2a4165SFrank Mandarino 
895a655de80SLiam Girdwood 	if (dai_link->ignore)
896a655de80SLiam Girdwood 		return 0;
897a655de80SLiam Girdwood 
8986f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
8996308419aSMark Brown 
90049a5ba1cSMengdong Lin 	if (soc_is_dai_link_bound(card, dai_link)) {
90149a5ba1cSMengdong Lin 		dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
90249a5ba1cSMengdong Lin 			dai_link->name);
90349a5ba1cSMengdong Lin 		return 0;
90449a5ba1cSMengdong Lin 	}
905db2a4165SFrank Mandarino 
906513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
907513cb311SSudip Mukherjee 	if (!rtd)
908513cb311SSudip Mukherjee 		return -ENOMEM;
909513cb311SSudip Mukherjee 
91008a5841eSKuninori Morimoto 	/* FIXME: we need multi CPU support in the future */
91108a5841eSKuninori Morimoto 	rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
912b19e6e7bSMark Brown 	if (!rtd->cpu_dai) {
9136b490879SMartin Hundebøll 		dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
91408a5841eSKuninori Morimoto 			 dai_link->cpus->dai_name);
9151a497983SMengdong Lin 		goto _err_defer;
916c5af3a2eSMark Brown 	}
91790be711eSKuninori Morimoto 	snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
918c5af3a2eSMark Brown 
91988bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
920e2b30edfSKuninori Morimoto 	rtd->num_codecs = dai_link->num_codecs;
921720734a0SKuninori Morimoto 	for_each_link_codecs(dai_link, i, codecs) {
9220a2cfcd9SKuninori Morimoto 		rtd->codec_dais[i] = snd_soc_find_dai(codecs);
9230a2cfcd9SKuninori Morimoto 		if (!rtd->codec_dais[i]) {
9247c7e2d6aSStefan Agner 			dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
925720734a0SKuninori Morimoto 				 codecs->dai_name);
9261a497983SMengdong Lin 			goto _err_defer;
92712023a9aSMisael Lopez Cruz 		}
9280a2cfcd9SKuninori Morimoto 		snd_soc_rtdcom_add(rtd, rtd->codec_dais[i]->component);
92988bd870fSBenoit Cousson 	}
93088bd870fSBenoit Cousson 
93188bd870fSBenoit Cousson 	/* Single codec links expect codec and codec_dai in runtime data */
9320a2cfcd9SKuninori Morimoto 	rtd->codec_dai = rtd->codec_dais[0];
93312023a9aSMisael Lopez Cruz 
934e2b30edfSKuninori Morimoto 	/* Find PLATFORM from registered PLATFORMs */
935368dee94SKuninori Morimoto 	for_each_component(component) {
936910fdcabSKuninori Morimoto 		if (!snd_soc_is_matching_component(dai_link->platforms,
937be6ac0a9SKuninori Morimoto 						   component))
93890be711eSKuninori Morimoto 			continue;
93990be711eSKuninori Morimoto 
94090be711eSKuninori Morimoto 		snd_soc_rtdcom_add(rtd, component);
94190be711eSKuninori Morimoto 	}
94290be711eSKuninori Morimoto 
9431a497983SMengdong Lin 	soc_add_pcm_runtime(card, rtd);
944b19e6e7bSMark Brown 	return 0;
9451a497983SMengdong Lin 
9461a497983SMengdong Lin _err_defer:
9471a497983SMengdong Lin 	soc_free_pcm_runtime(rtd);
9481a497983SMengdong Lin 	return -EPROBE_DEFER;
9496b05eda6SMark Brown }
9506b05eda6SMark Brown 
95122d14231SKuninori Morimoto static void soc_cleanup_component(struct snd_soc_component *component)
95222d14231SKuninori Morimoto {
9533bb936f5SAmadeusz Sławiński 	snd_soc_component_set_jack(component, NULL, NULL);
95422d14231SKuninori Morimoto 	list_del(&component->card_list);
95522d14231SKuninori Morimoto 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
95622d14231SKuninori Morimoto 	soc_cleanup_component_debugfs(component);
95722d14231SKuninori Morimoto 	component->card = NULL;
958b4ed6b51SRanjani Sridharan 	if (!component->driver->module_get_upon_open)
95922d14231SKuninori Morimoto 		module_put(component->dev->driver->owner);
96022d14231SKuninori Morimoto }
96122d14231SKuninori Morimoto 
962f1d45cc3SLars-Peter Clausen static void soc_remove_component(struct snd_soc_component *component)
963d12cd198SStephen Warren {
964abd31b32SLars-Peter Clausen 	if (!component->card)
96570090bbbSLars-Peter Clausen 		return;
966d12cd198SStephen Warren 
967999f7f5aSKuninori Morimoto 	if (component->driver->remove)
968999f7f5aSKuninori Morimoto 		component->driver->remove(component);
969d12cd198SStephen Warren 
97022d14231SKuninori Morimoto 	soc_cleanup_component(component);
971d12cd198SStephen Warren }
972d12cd198SStephen Warren 
973e60cd14fSLars-Peter Clausen static void soc_remove_dai(struct snd_soc_dai *dai, int order)
974589c3563SJarkko Nikula {
975589c3563SJarkko Nikula 	int err;
976589c3563SJarkko Nikula 
97752abe6ccSGuennadi Liakhovetski 	if (!dai || !dai->probed || !dai->driver ||
9782eda3cb1SKuninori Morimoto 	    dai->driver->remove_order != order)
9792eda3cb1SKuninori Morimoto 		return;
9802eda3cb1SKuninori Morimoto 
981e60cd14fSLars-Peter Clausen 	if (dai->driver->remove) {
982e60cd14fSLars-Peter Clausen 		err = dai->driver->remove(dai);
983589c3563SJarkko Nikula 		if (err < 0)
984e60cd14fSLars-Peter Clausen 			dev_err(dai->dev,
985b0aa88afSMisael Lopez Cruz 				"ASoC: failed to remove %s: %d\n",
986e60cd14fSLars-Peter Clausen 				dai->name, err);
987b0aa88afSMisael Lopez Cruz 	}
988e60cd14fSLars-Peter Clausen 	dai->probed = 0;
989b0aa88afSMisael Lopez Cruz }
990b0aa88afSMisael Lopez Cruz 
9911a497983SMengdong Lin static void soc_remove_link_dais(struct snd_soc_card *card,
9921a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
993f0fba2adSLiam Girdwood {
994e60cd14fSLars-Peter Clausen 	int i;
9950b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
996f0fba2adSLiam Girdwood 
997f0fba2adSLiam Girdwood 	/* unregister the rtd device */
998f0fba2adSLiam Girdwood 	if (rtd->dev_registered) {
99936ae1a96SMark Brown 		device_unregister(rtd->dev);
1000f0fba2adSLiam Girdwood 		rtd->dev_registered = 0;
100102a06d30SBarry Song 	}
100202a06d30SBarry Song 
1003f0fba2adSLiam Girdwood 	/* remove the CODEC DAI */
10040b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
10050b7990e3SKuninori Morimoto 		soc_remove_dai(codec_dai, order);
1006f0fba2adSLiam Girdwood 
1007e60cd14fSLars-Peter Clausen 	soc_remove_dai(rtd->cpu_dai, order);
1008f0fba2adSLiam Girdwood }
1009f0fba2adSLiam Girdwood 
10101a497983SMengdong Lin static void soc_remove_link_components(struct snd_soc_card *card,
10111a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd, int order)
101262ae68faSStephen Warren {
101361aca564SLars-Peter Clausen 	struct snd_soc_component *component;
101490be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
101562ae68faSStephen Warren 
101634ac3c3eSRanjani Sridharan 	mutex_lock(&client_mutex);
101790be711eSKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
101890be711eSKuninori Morimoto 		component = rtdcom->component;
101962ae68faSStephen Warren 
102070090bbbSLars-Peter Clausen 		if (component->driver->remove_order == order)
102161aca564SLars-Peter Clausen 			soc_remove_component(component);
102262ae68faSStephen Warren 	}
102334ac3c3eSRanjani Sridharan 	mutex_unlock(&client_mutex);
102462ae68faSStephen Warren }
102562ae68faSStephen Warren 
10260671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card)
10270671fd8eSKuninori Morimoto {
10281a497983SMengdong Lin 	int order;
10291a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
1030f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
10310671fd8eSKuninori Morimoto 
10321a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1033bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd)
10341a497983SMengdong Lin 			soc_remove_link_dais(card, rtd, order);
10350168bf0dSLiam Girdwood 	}
103662ae68faSStephen Warren 
10371a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1038bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd)
10391a497983SMengdong Lin 			soc_remove_link_components(card, rtd, order);
104062ae68faSStephen Warren 	}
104162ae68faSStephen Warren 
104298061fdbSKuninori Morimoto 	for_each_card_links_safe(card, link, _link) {
1043f8f80361SMengdong Lin 		if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1044f8f80361SMengdong Lin 			dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1045f8f80361SMengdong Lin 				link->name);
1046f8f80361SMengdong Lin 
1047f8f80361SMengdong Lin 		list_del(&link->list);
10480671fd8eSKuninori Morimoto 	}
10490671fd8eSKuninori Morimoto }
10500671fd8eSKuninori Morimoto 
1051923c5e61SMengdong Lin static int soc_init_dai_link(struct snd_soc_card *card,
1052923c5e61SMengdong Lin 			     struct snd_soc_dai_link *link)
1053923c5e61SMengdong Lin {
1054adb76b5bSKuninori Morimoto 	int i;
10553db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *codec;
1056923c5e61SMengdong Lin 
10573db769f1SKuninori Morimoto 	for_each_link_codecs(link, i, codec) {
1058923c5e61SMengdong Lin 		/*
1059923c5e61SMengdong Lin 		 * Codec must be specified by 1 of name or OF node,
1060923c5e61SMengdong Lin 		 * not both or neither.
1061923c5e61SMengdong Lin 		 */
10623db769f1SKuninori Morimoto 		if (!!codec->name ==
10633db769f1SKuninori Morimoto 		    !!codec->of_node) {
1064923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1065923c5e61SMengdong Lin 				link->name);
1066923c5e61SMengdong Lin 			return -EINVAL;
1067923c5e61SMengdong Lin 		}
1068923c5e61SMengdong Lin 		/* Codec DAI name must be specified */
10693db769f1SKuninori Morimoto 		if (!codec->dai_name) {
1070923c5e61SMengdong Lin 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1071923c5e61SMengdong Lin 				link->name);
1072923c5e61SMengdong Lin 			return -EINVAL;
1073923c5e61SMengdong Lin 		}
1074923c5e61SMengdong Lin 	}
1075923c5e61SMengdong Lin 
10761d768989SKuninori Morimoto 	/*
10771d768989SKuninori Morimoto 	 * Platform may be specified by either name or OF node,
10781d768989SKuninori Morimoto 	 * or no Platform.
10791d768989SKuninori Morimoto 	 *
10801d768989SKuninori Morimoto 	 * FIXME
10811d768989SKuninori Morimoto 	 *
10821d768989SKuninori Morimoto 	 * We need multi-platform support
10831d768989SKuninori Morimoto 	 */
10841d768989SKuninori Morimoto 	if (link->num_platforms > 0) {
10851d768989SKuninori Morimoto 
1086910fdcabSKuninori Morimoto 		if (link->num_platforms > 1) {
1087910fdcabSKuninori Morimoto 			dev_err(card->dev,
1088910fdcabSKuninori Morimoto 				"ASoC: multi platform is not yet supported %s\n",
1089910fdcabSKuninori Morimoto 				link->name);
1090910fdcabSKuninori Morimoto 			return -EINVAL;
1091910fdcabSKuninori Morimoto 		}
1092910fdcabSKuninori Morimoto 
1093910fdcabSKuninori Morimoto 		if (link->platforms->name && link->platforms->of_node) {
1094923c5e61SMengdong Lin 			dev_err(card->dev,
1095923c5e61SMengdong Lin 				"ASoC: Both platform name/of_node are set for %s\n",
1096923c5e61SMengdong Lin 				link->name);
1097923c5e61SMengdong Lin 			return -EINVAL;
1098923c5e61SMengdong Lin 		}
10998780cf11SAjit Pandey 
11008780cf11SAjit Pandey 		/*
11011d768989SKuninori Morimoto 		 * Defer card registartion if platform dai component is not
11021d768989SKuninori Morimoto 		 * added to component list.
11038780cf11SAjit Pandey 		 */
1104910fdcabSKuninori Morimoto 		if ((link->platforms->of_node || link->platforms->name) &&
1105*c1e230f0SKuninori Morimoto 		    !soc_find_component(link->platforms))
11068780cf11SAjit Pandey 			return -EPROBE_DEFER;
11071d768989SKuninori Morimoto 	}
11088780cf11SAjit Pandey 
110908a5841eSKuninori Morimoto 	/* FIXME */
111008a5841eSKuninori Morimoto 	if (link->num_cpus > 1) {
111108a5841eSKuninori Morimoto 		dev_err(card->dev,
111208a5841eSKuninori Morimoto 			"ASoC: multi cpu is not yet supported %s\n",
111308a5841eSKuninori Morimoto 			link->name);
111408a5841eSKuninori Morimoto 		return -EINVAL;
111508a5841eSKuninori Morimoto 	}
111608a5841eSKuninori Morimoto 
1117923c5e61SMengdong Lin 	/*
1118923c5e61SMengdong Lin 	 * CPU device may be specified by either name or OF node, but
1119923c5e61SMengdong Lin 	 * can be left unspecified, and will be matched based on DAI
1120923c5e61SMengdong Lin 	 * name alone..
1121923c5e61SMengdong Lin 	 */
112208a5841eSKuninori Morimoto 	if (link->cpus->name && link->cpus->of_node) {
1123923c5e61SMengdong Lin 		dev_err(card->dev,
1124923c5e61SMengdong Lin 			"ASoC: Neither/both cpu name/of_node are set for %s\n",
1125923c5e61SMengdong Lin 			link->name);
1126923c5e61SMengdong Lin 		return -EINVAL;
1127923c5e61SMengdong Lin 	}
11288780cf11SAjit Pandey 
11298780cf11SAjit Pandey 	/*
11308780cf11SAjit Pandey 	 * Defer card registartion if cpu dai component is not added to
11318780cf11SAjit Pandey 	 * component list.
11328780cf11SAjit Pandey 	 */
113308a5841eSKuninori Morimoto 	if ((link->cpus->of_node || link->cpus->name) &&
1134*c1e230f0SKuninori Morimoto 	    !soc_find_component(link->cpus))
11358780cf11SAjit Pandey 		return -EPROBE_DEFER;
11368780cf11SAjit Pandey 
1137923c5e61SMengdong Lin 	/*
1138923c5e61SMengdong Lin 	 * At least one of CPU DAI name or CPU device name/node must be
1139923c5e61SMengdong Lin 	 * specified
1140923c5e61SMengdong Lin 	 */
114108a5841eSKuninori Morimoto 	if (!link->cpus->dai_name &&
114208a5841eSKuninori Morimoto 	    !(link->cpus->name || link->cpus->of_node)) {
1143923c5e61SMengdong Lin 		dev_err(card->dev,
1144923c5e61SMengdong Lin 			"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1145923c5e61SMengdong Lin 			link->name);
1146923c5e61SMengdong Lin 		return -EINVAL;
1147923c5e61SMengdong Lin 	}
1148923c5e61SMengdong Lin 
1149923c5e61SMengdong Lin 	return 0;
1150923c5e61SMengdong Lin }
1151923c5e61SMengdong Lin 
1152ef2e8175SKuninori Morimoto void snd_soc_disconnect_sync(struct device *dev)
1153ef2e8175SKuninori Morimoto {
11542c7b696aSMarcel Ziswiler 	struct snd_soc_component *component =
11552c7b696aSMarcel Ziswiler 			snd_soc_lookup_component(dev, NULL);
1156ef2e8175SKuninori Morimoto 
1157ef2e8175SKuninori Morimoto 	if (!component || !component->card)
1158ef2e8175SKuninori Morimoto 		return;
1159ef2e8175SKuninori Morimoto 
1160ef2e8175SKuninori Morimoto 	snd_card_disconnect_sync(component->card->snd_card);
1161ef2e8175SKuninori Morimoto }
1162df532185SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
1163ef2e8175SKuninori Morimoto 
1164f8f80361SMengdong Lin /**
1165f8f80361SMengdong Lin  * snd_soc_add_dai_link - Add a DAI link dynamically
1166f8f80361SMengdong Lin  * @card: The ASoC card to which the DAI link is added
1167f8f80361SMengdong Lin  * @dai_link: The new DAI link to add
1168f8f80361SMengdong Lin  *
1169f8f80361SMengdong Lin  * This function adds a DAI link to the ASoC card's link list.
1170f8f80361SMengdong Lin  *
1171f8f80361SMengdong Lin  * Note: Topology can use this API to add DAI links when probing the
1172f8f80361SMengdong Lin  * topology component. And machine drivers can still define static
1173f8f80361SMengdong Lin  * DAI links in dai_link array.
1174f8f80361SMengdong Lin  */
1175f8f80361SMengdong Lin int snd_soc_add_dai_link(struct snd_soc_card *card,
1176f8f80361SMengdong Lin 		struct snd_soc_dai_link *dai_link)
1177f8f80361SMengdong Lin {
1178f8f80361SMengdong Lin 	if (dai_link->dobj.type
1179f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1180f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1181f8f80361SMengdong Lin 			dai_link->dobj.type);
1182f8f80361SMengdong Lin 		return -EINVAL;
1183f8f80361SMengdong Lin 	}
1184f8f80361SMengdong Lin 
1185f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
11862c7b696aSMarcel Ziswiler 	/*
11872c7b696aSMarcel Ziswiler 	 * Notify the machine driver for extra initialization
1188d6f220eaSMengdong Lin 	 * on the link created by topology.
1189d6f220eaSMengdong Lin 	 */
1190d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->add_dai_link)
1191d6f220eaSMengdong Lin 		card->add_dai_link(card, dai_link);
1192d6f220eaSMengdong Lin 
1193f8f80361SMengdong Lin 	list_add_tail(&dai_link->list, &card->dai_link_list);
1194f8f80361SMengdong Lin 
1195f8f80361SMengdong Lin 	return 0;
1196f8f80361SMengdong Lin }
1197f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1198f8f80361SMengdong Lin 
1199f8f80361SMengdong Lin /**
1200f8f80361SMengdong Lin  * snd_soc_remove_dai_link - Remove a DAI link from the list
1201f8f80361SMengdong Lin  * @card: The ASoC card that owns the link
1202f8f80361SMengdong Lin  * @dai_link: The DAI link to remove
1203f8f80361SMengdong Lin  *
1204f8f80361SMengdong Lin  * This function removes a DAI link from the ASoC card's link list.
1205f8f80361SMengdong Lin  *
1206f8f80361SMengdong Lin  * For DAI links previously added by topology, topology should
1207f8f80361SMengdong Lin  * remove them by using the dobj embedded in the link.
1208f8f80361SMengdong Lin  */
1209f8f80361SMengdong Lin void snd_soc_remove_dai_link(struct snd_soc_card *card,
1210f8f80361SMengdong Lin 			     struct snd_soc_dai_link *dai_link)
1211f8f80361SMengdong Lin {
1212f8f80361SMengdong Lin 	struct snd_soc_dai_link *link, *_link;
1213f8f80361SMengdong Lin 
1214f8f80361SMengdong Lin 	if (dai_link->dobj.type
1215f8f80361SMengdong Lin 	    && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1216f8f80361SMengdong Lin 		dev_err(card->dev, "Invalid dai link type %d\n",
1217f8f80361SMengdong Lin 			dai_link->dobj.type);
1218f8f80361SMengdong Lin 		return;
1219f8f80361SMengdong Lin 	}
1220f8f80361SMengdong Lin 
1221f8f80361SMengdong Lin 	lockdep_assert_held(&client_mutex);
12222c7b696aSMarcel Ziswiler 	/*
12232c7b696aSMarcel Ziswiler 	 * Notify the machine driver for extra destruction
1224d6f220eaSMengdong Lin 	 * on the link created by topology.
1225d6f220eaSMengdong Lin 	 */
1226d6f220eaSMengdong Lin 	if (dai_link->dobj.type && card->remove_dai_link)
1227d6f220eaSMengdong Lin 		card->remove_dai_link(card, dai_link);
1228d6f220eaSMengdong Lin 
122998061fdbSKuninori Morimoto 	for_each_card_links_safe(card, link, _link) {
1230f8f80361SMengdong Lin 		if (link == dai_link) {
1231f8f80361SMengdong Lin 			list_del(&link->list);
1232f8f80361SMengdong Lin 			return;
1233f8f80361SMengdong Lin 		}
1234f8f80361SMengdong Lin 	}
1235f8f80361SMengdong Lin }
1236f8f80361SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1237f0fba2adSLiam Girdwood 
1238aefba455SJerome Brunet static void soc_set_of_name_prefix(struct snd_soc_component *component)
1239aefba455SJerome Brunet {
1240c0834440SKuninori Morimoto 	struct device_node *component_of_node = soc_component_to_node(component);
1241aefba455SJerome Brunet 	const char *str;
1242aefba455SJerome Brunet 	int ret;
1243aefba455SJerome Brunet 
1244aefba455SJerome Brunet 	ret = of_property_read_string(component_of_node, "sound-name-prefix",
1245aefba455SJerome Brunet 				      &str);
1246aefba455SJerome Brunet 	if (!ret)
1247aefba455SJerome Brunet 		component->name_prefix = str;
1248aefba455SJerome Brunet }
1249aefba455SJerome Brunet 
1250ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card,
125194f99c87SLars-Peter Clausen 				struct snd_soc_component *component)
1252ead9b919SJarkko Nikula {
1253ead9b919SJarkko Nikula 	int i;
1254ead9b919SJarkko Nikula 
1255aefba455SJerome Brunet 	for (i = 0; i < card->num_configs && card->codec_conf; i++) {
1256ff819b83SDimitris Papastamos 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1257c0834440SKuninori Morimoto 		struct device_node *component_of_node = soc_component_to_node(component);
1258b24c539bSCharles Keepax 
1259b24c539bSCharles Keepax 		if (map->of_node && component_of_node != map->of_node)
12603ca041edSSebastian Reichel 			continue;
126194f99c87SLars-Peter Clausen 		if (map->dev_name && strcmp(component->name, map->dev_name))
12623ca041edSSebastian Reichel 			continue;
126394f99c87SLars-Peter Clausen 		component->name_prefix = map->name_prefix;
1264aefba455SJerome Brunet 		return;
1265ead9b919SJarkko Nikula 	}
1266aefba455SJerome Brunet 
1267aefba455SJerome Brunet 	/*
1268aefba455SJerome Brunet 	 * If there is no configuration table or no match in the table,
1269aefba455SJerome Brunet 	 * check if a prefix is provided in the node
1270aefba455SJerome Brunet 	 */
1271aefba455SJerome Brunet 	soc_set_of_name_prefix(component);
1272ead9b919SJarkko Nikula }
1273ead9b919SJarkko Nikula 
1274f1d45cc3SLars-Peter Clausen static int soc_probe_component(struct snd_soc_card *card,
1275f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component)
1276589c3563SJarkko Nikula {
12772c7b696aSMarcel Ziswiler 	struct snd_soc_dapm_context *dapm =
12782c7b696aSMarcel Ziswiler 			snd_soc_component_get_dapm(component);
1279888df395SMark Brown 	struct snd_soc_dai *dai;
1280f1d45cc3SLars-Peter Clausen 	int ret;
1281589c3563SJarkko Nikula 
12821b7c1231SLars-Peter Clausen 	if (!strcmp(component->name, "snd-soc-dummy"))
128370090bbbSLars-Peter Clausen 		return 0;
1284589c3563SJarkko Nikula 
1285abd31b32SLars-Peter Clausen 	if (component->card) {
12861b7c1231SLars-Peter Clausen 		if (component->card != card) {
12871b7c1231SLars-Peter Clausen 			dev_err(component->dev,
12881b7c1231SLars-Peter Clausen 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
12891b7c1231SLars-Peter Clausen 				card->name, component->card->name);
12901b7c1231SLars-Peter Clausen 			return -ENODEV;
12911b7c1231SLars-Peter Clausen 		}
12921b7c1231SLars-Peter Clausen 		return 0;
12931b7c1231SLars-Peter Clausen 	}
12941b7c1231SLars-Peter Clausen 
1295b4ed6b51SRanjani Sridharan 	if (!component->driver->module_get_upon_open &&
1296b450b878SPierre-Louis Bossart 	    !try_module_get(component->dev->driver->owner))
1297abd31b32SLars-Peter Clausen 		return -ENODEV;
1298abd31b32SLars-Peter Clausen 
1299f1d45cc3SLars-Peter Clausen 	component->card = card;
1300f1d45cc3SLars-Peter Clausen 	dapm->card = card;
130122d14231SKuninori Morimoto 	INIT_LIST_HEAD(&component->card_list);
130222d14231SKuninori Morimoto 	INIT_LIST_HEAD(&dapm->list);
1303f1d45cc3SLars-Peter Clausen 	soc_set_name_prefix(card, component);
1304589c3563SJarkko Nikula 
1305f1d45cc3SLars-Peter Clausen 	soc_init_component_debugfs(component);
1306d5d1e0beSLars-Peter Clausen 
1307688d0ebfSKuninori Morimoto 	if (component->driver->dapm_widgets) {
1308688d0ebfSKuninori Morimoto 		ret = snd_soc_dapm_new_controls(dapm,
1309688d0ebfSKuninori Morimoto 					component->driver->dapm_widgets,
1310688d0ebfSKuninori Morimoto 					component->driver->num_dapm_widgets);
131177530150SLars-Peter Clausen 
1312b318ad50SNariman Poushin 		if (ret != 0) {
1313f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1314b318ad50SNariman Poushin 				"Failed to create new controls %d\n", ret);
1315b318ad50SNariman Poushin 			goto err_probe;
1316b318ad50SNariman Poushin 		}
1317b318ad50SNariman Poushin 	}
1318b318ad50SNariman Poushin 
131915a0c645SKuninori Morimoto 	for_each_component_dais(component, dai) {
13200634814fSLars-Peter Clausen 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1321261edc70SNariman Poushin 		if (ret != 0) {
1322f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1323261edc70SNariman Poushin 				"Failed to create DAI widgets %d\n", ret);
1324261edc70SNariman Poushin 			goto err_probe;
1325261edc70SNariman Poushin 		}
1326261edc70SNariman Poushin 	}
1327888df395SMark Brown 
1328999f7f5aSKuninori Morimoto 	if (component->driver->probe) {
1329999f7f5aSKuninori Morimoto 		ret = component->driver->probe(component);
1330589c3563SJarkko Nikula 		if (ret < 0) {
1331f1d45cc3SLars-Peter Clausen 			dev_err(component->dev,
1332f1d45cc3SLars-Peter Clausen 				"ASoC: failed to probe component %d\n", ret);
133370d29331SJarkko Nikula 			goto err_probe;
1334589c3563SJarkko Nikula 		}
13352ffb0f58SKuninori Morimoto 	}
1336f1d45cc3SLars-Peter Clausen 	WARN(dapm->idle_bias_off &&
1337f1d45cc3SLars-Peter Clausen 	     dapm->bias_level != SND_SOC_BIAS_OFF,
133810e8aa9aSMichał Mirosław 	     "codec %s can not start from non-off bias with idle_bias_off==1\n",
1339f1d45cc3SLars-Peter Clausen 	     component->name);
1340589c3563SJarkko Nikula 
1341f2ed6b07SMengdong Lin 	/* machine specific init */
1342f2ed6b07SMengdong Lin 	if (component->init) {
1343f2ed6b07SMengdong Lin 		ret = component->init(component);
1344f2ed6b07SMengdong Lin 		if (ret < 0) {
1345f2ed6b07SMengdong Lin 			dev_err(component->dev,
1346f2ed6b07SMengdong Lin 				"Failed to do machine specific init %d\n", ret);
1347f2ed6b07SMengdong Lin 			goto err_probe;
1348f2ed6b07SMengdong Lin 		}
1349f2ed6b07SMengdong Lin 	}
1350f2ed6b07SMengdong Lin 
1351b8972bf0SKuninori Morimoto 	if (component->driver->controls)
1352b8972bf0SKuninori Morimoto 		snd_soc_add_component_controls(component,
1353b8972bf0SKuninori Morimoto 					       component->driver->controls,
1354b8972bf0SKuninori Morimoto 					       component->driver->num_controls);
13556969b2baSKuninori Morimoto 	if (component->driver->dapm_routes)
13566969b2baSKuninori Morimoto 		snd_soc_dapm_add_routes(dapm,
13576969b2baSKuninori Morimoto 					component->driver->dapm_routes,
13586969b2baSKuninori Morimoto 					component->driver->num_dapm_routes);
135989b95ac0SMark Brown 
1360f1d45cc3SLars-Peter Clausen 	list_add(&dapm->list, &card->dapm_list);
1361f70f18f7SKuninori Morimoto 	/* see for_each_card_components */
1362d9fc4063SKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1363589c3563SJarkko Nikula 
136470d29331SJarkko Nikula err_probe:
136522d14231SKuninori Morimoto 	if (ret < 0)
136622d14231SKuninori Morimoto 		soc_cleanup_component(component);
1367956245e9SLiam Girdwood 
1368956245e9SLiam Girdwood 	return ret;
1369956245e9SLiam Girdwood }
1370956245e9SLiam Girdwood 
137136ae1a96SMark Brown static void rtd_release(struct device *dev)
137236ae1a96SMark Brown {
137336ae1a96SMark Brown 	kfree(dev);
137436ae1a96SMark Brown }
1375f0fba2adSLiam Girdwood 
13765f3484acSLars-Peter Clausen static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
13775f3484acSLars-Peter Clausen 	const char *name)
1378503ae5e0SMisael Lopez Cruz {
1379589c3563SJarkko Nikula 	int ret = 0;
1380589c3563SJarkko Nikula 
1381589c3563SJarkko Nikula 	/* register the rtd device */
138236ae1a96SMark Brown 	rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
138336ae1a96SMark Brown 	if (!rtd->dev)
138436ae1a96SMark Brown 		return -ENOMEM;
138536ae1a96SMark Brown 	device_initialize(rtd->dev);
13865f3484acSLars-Peter Clausen 	rtd->dev->parent = rtd->card->dev;
138736ae1a96SMark Brown 	rtd->dev->release = rtd_release;
1388d29697dcSTakashi Iwai 	rtd->dev->groups = soc_dev_attr_groups;
1389f294afedSLars-Peter Clausen 	dev_set_name(rtd->dev, "%s", name);
139036ae1a96SMark Brown 	dev_set_drvdata(rtd->dev, rtd);
1391b8c0dab9SLiam Girdwood 	mutex_init(&rtd->pcm_mutex);
139201d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
139301d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
139401d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
139501d7584cSLiam Girdwood 	INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
139636ae1a96SMark Brown 	ret = device_add(rtd->dev);
1397589c3563SJarkko Nikula 	if (ret < 0) {
1398865df9cbSChuansheng Liu 		/* calling put_device() here to free the rtd->dev */
1399865df9cbSChuansheng Liu 		put_device(rtd->dev);
14005f3484acSLars-Peter Clausen 		dev_err(rtd->card->dev,
1401f110bfc7SLiam Girdwood 			"ASoC: failed to register runtime device: %d\n", ret);
1402589c3563SJarkko Nikula 		return ret;
1403589c3563SJarkko Nikula 	}
1404589c3563SJarkko Nikula 	rtd->dev_registered = 1;
1405589c3563SJarkko Nikula 	return 0;
1406589c3563SJarkko Nikula }
1407589c3563SJarkko Nikula 
14081a497983SMengdong Lin static int soc_probe_link_components(struct snd_soc_card *card,
14092c7b696aSMarcel Ziswiler 				     struct snd_soc_pcm_runtime *rtd, int order)
141062ae68faSStephen Warren {
1411f1d45cc3SLars-Peter Clausen 	struct snd_soc_component *component;
141290be711eSKuninori Morimoto 	struct snd_soc_rtdcom_list *rtdcom;
141390be711eSKuninori Morimoto 	int ret;
141462ae68faSStephen Warren 
141590be711eSKuninori Morimoto 	for_each_rtdcom(rtd, rtdcom) {
141690be711eSKuninori Morimoto 		component = rtdcom->component;
141790be711eSKuninori Morimoto 
141870090bbbSLars-Peter Clausen 		if (component->driver->probe_order == order) {
1419f1d45cc3SLars-Peter Clausen 			ret = soc_probe_component(card, component);
142062ae68faSStephen Warren 			if (ret < 0)
142162ae68faSStephen Warren 				return ret;
142262ae68faSStephen Warren 		}
142362ae68faSStephen Warren 	}
142462ae68faSStephen Warren 
142562ae68faSStephen Warren 	return 0;
142662ae68faSStephen Warren }
142762ae68faSStephen Warren 
14288e2be562SLars-Peter Clausen static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1429b0aa88afSMisael Lopez Cruz {
14307a2ccad5SKuninori Morimoto 	if (dai->probed ||
14317a2ccad5SKuninori Morimoto 	    dai->driver->probe_order != order)
14327a2ccad5SKuninori Morimoto 		return 0;
1433b0aa88afSMisael Lopez Cruz 
14348e2be562SLars-Peter Clausen 	if (dai->driver->probe) {
14357a2ccad5SKuninori Morimoto 		int ret = dai->driver->probe(dai);
14362c7b696aSMarcel Ziswiler 
1437b0aa88afSMisael Lopez Cruz 		if (ret < 0) {
14387a2ccad5SKuninori Morimoto 			dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
14398e2be562SLars-Peter Clausen 				dai->name, ret);
1440b0aa88afSMisael Lopez Cruz 			return ret;
1441b0aa88afSMisael Lopez Cruz 		}
1442b0aa88afSMisael Lopez Cruz 	}
1443b0aa88afSMisael Lopez Cruz 
14448e2be562SLars-Peter Clausen 	dai->probed = 1;
1445b0aa88afSMisael Lopez Cruz 
1446b0aa88afSMisael Lopez Cruz 	return 0;
1447b0aa88afSMisael Lopez Cruz }
1448b0aa88afSMisael Lopez Cruz 
144925f7b701SArnaud Pouliquen static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
145025f7b701SArnaud Pouliquen 				struct snd_soc_pcm_runtime *rtd)
145125f7b701SArnaud Pouliquen {
145225f7b701SArnaud Pouliquen 	int i, ret = 0;
145325f7b701SArnaud Pouliquen 
145425f7b701SArnaud Pouliquen 	for (i = 0; i < num_dais; ++i) {
145525f7b701SArnaud Pouliquen 		struct snd_soc_dai_driver *drv = dais[i]->driver;
145625f7b701SArnaud Pouliquen 
1457de17f14eSRohit kumar 		if (drv->pcm_new)
145825f7b701SArnaud Pouliquen 			ret = drv->pcm_new(rtd, dais[i]);
145925f7b701SArnaud Pouliquen 		if (ret < 0) {
146025f7b701SArnaud Pouliquen 			dev_err(dais[i]->dev,
146125f7b701SArnaud Pouliquen 				"ASoC: Failed to bind %s with pcm device\n",
146225f7b701SArnaud Pouliquen 				dais[i]->name);
146325f7b701SArnaud Pouliquen 			return ret;
146425f7b701SArnaud Pouliquen 		}
146525f7b701SArnaud Pouliquen 	}
146625f7b701SArnaud Pouliquen 
146725f7b701SArnaud Pouliquen 	return 0;
146825f7b701SArnaud Pouliquen }
146925f7b701SArnaud Pouliquen 
14701a497983SMengdong Lin static int soc_probe_link_dais(struct snd_soc_card *card,
14711a497983SMengdong Lin 		struct snd_soc_pcm_runtime *rtd, int order)
1472f0fba2adSLiam Girdwood {
14731a497983SMengdong Lin 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1474c74184edSMark Brown 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1475a655de80SLiam Girdwood 	struct snd_soc_rtdcom_list *rtdcom;
1476a655de80SLiam Girdwood 	struct snd_soc_component *component;
14770b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1478a655de80SLiam Girdwood 	int i, ret, num;
1479f0fba2adSLiam Girdwood 
1480f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
14811a497983SMengdong Lin 			card->name, rtd->num, order);
1482f0fba2adSLiam Girdwood 
1483f0fba2adSLiam Girdwood 	/* set default power off timeout */
1484f0fba2adSLiam Girdwood 	rtd->pmdown_time = pmdown_time;
1485f0fba2adSLiam Girdwood 
14868e2be562SLars-Peter Clausen 	ret = soc_probe_dai(cpu_dai, order);
14878e2be562SLars-Peter Clausen 	if (ret)
1488f0fba2adSLiam Girdwood 		return ret;
1489f0fba2adSLiam Girdwood 
1490f0fba2adSLiam Girdwood 	/* probe the CODEC DAI */
14910b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
14920b7990e3SKuninori Morimoto 		ret = soc_probe_dai(codec_dai, order);
1493b0aa88afSMisael Lopez Cruz 		if (ret)
1494f0fba2adSLiam Girdwood 			return ret;
149588bd870fSBenoit Cousson 	}
1496f0fba2adSLiam Girdwood 
14970168bf0dSLiam Girdwood 	/* complete DAI probe during last probe */
14980168bf0dSLiam Girdwood 	if (order != SND_SOC_COMP_ORDER_LAST)
14990168bf0dSLiam Girdwood 		return 0;
15000168bf0dSLiam Girdwood 
15015f3484acSLars-Peter Clausen 	/* do machine specific initialization */
15025f3484acSLars-Peter Clausen 	if (dai_link->init) {
15035f3484acSLars-Peter Clausen 		ret = dai_link->init(rtd);
15045f3484acSLars-Peter Clausen 		if (ret < 0) {
15055f3484acSLars-Peter Clausen 			dev_err(card->dev, "ASoC: failed to init %s: %d\n",
15065f3484acSLars-Peter Clausen 				dai_link->name, ret);
15075f3484acSLars-Peter Clausen 			return ret;
15085f3484acSLars-Peter Clausen 		}
15095f3484acSLars-Peter Clausen 	}
15105f3484acSLars-Peter Clausen 
1511a5053a8eSKuninori Morimoto 	if (dai_link->dai_fmt)
1512a5053a8eSKuninori Morimoto 		snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1513a5053a8eSKuninori Morimoto 
15145f3484acSLars-Peter Clausen 	ret = soc_post_component_init(rtd, dai_link->name);
1515589c3563SJarkko Nikula 	if (ret)
1516f0fba2adSLiam Girdwood 		return ret;
1517f0fba2adSLiam Girdwood 
15185f3484acSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
15195f3484acSLars-Peter Clausen 	/* add DPCM sysfs entries */
15202e55b90aSLars-Peter Clausen 	if (dai_link->dynamic)
15212e55b90aSLars-Peter Clausen 		soc_dpcm_debugfs_add(rtd);
15225f3484acSLars-Peter Clausen #endif
15235f3484acSLars-Peter Clausen 
1524a655de80SLiam Girdwood 	num = rtd->num;
1525a655de80SLiam Girdwood 
1526a655de80SLiam Girdwood 	/*
1527a655de80SLiam Girdwood 	 * most drivers will register their PCMs using DAI link ordering but
1528a655de80SLiam Girdwood 	 * topology based drivers can use the DAI link id field to set PCM
1529a655de80SLiam Girdwood 	 * device number and then use rtd + a base offset of the BEs.
1530a655de80SLiam Girdwood 	 */
1531a655de80SLiam Girdwood 	for_each_rtdcom(rtd, rtdcom) {
1532a655de80SLiam Girdwood 		component = rtdcom->component;
1533a655de80SLiam Girdwood 
1534a655de80SLiam Girdwood 		if (!component->driver->use_dai_pcm_id)
1535a655de80SLiam Girdwood 			continue;
1536a655de80SLiam Girdwood 
1537a655de80SLiam Girdwood 		if (rtd->dai_link->no_pcm)
1538a655de80SLiam Girdwood 			num += component->driver->be_pcm_base;
1539a655de80SLiam Girdwood 		else
1540a655de80SLiam Girdwood 			num = rtd->dai_link->id;
1541a655de80SLiam Girdwood 	}
1542a655de80SLiam Girdwood 
15436f0c4226SJie Yang 	if (cpu_dai->driver->compress_new) {
15441245b700SNamarta Kohli 		/* create compress_device" */
1545a655de80SLiam Girdwood 		ret = cpu_dai->driver->compress_new(rtd, num);
15461245b700SNamarta Kohli 		if (ret < 0) {
1547f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create compress %s\n",
15481245b700SNamarta Kohli 					 dai_link->stream_name);
15491245b700SNamarta Kohli 			return ret;
15501245b700SNamarta Kohli 		}
155152293596SKuninori Morimoto 	} else if (!dai_link->params) {
1552f0fba2adSLiam Girdwood 		/* create the pcm */
1553a655de80SLiam Girdwood 		ret = soc_new_pcm(rtd, num);
1554f0fba2adSLiam Girdwood 		if (ret < 0) {
1555f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
15560837fc62SFabio Estevam 				dai_link->stream_name, ret);
1557f0fba2adSLiam Girdwood 			return ret;
1558f0fba2adSLiam Girdwood 		}
155925f7b701SArnaud Pouliquen 		ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
156025f7b701SArnaud Pouliquen 		if (ret < 0)
156125f7b701SArnaud Pouliquen 			return ret;
156225f7b701SArnaud Pouliquen 		ret = soc_link_dai_pcm_new(rtd->codec_dais,
156325f7b701SArnaud Pouliquen 					   rtd->num_codecs, rtd);
156425f7b701SArnaud Pouliquen 		if (ret < 0)
156525f7b701SArnaud Pouliquen 			return ret;
1566c74184edSMark Brown 	} else {
15679d58a077SRichard Fitzgerald 		INIT_DELAYED_WORK(&rtd->delayed_work,
15689d58a077SRichard Fitzgerald 				  codec2codec_close_delayed_work);
1569c74184edSMark Brown 	}
1570c74184edSMark Brown 
1571f0fba2adSLiam Girdwood 	return 0;
1572f0fba2adSLiam Girdwood }
1573f0fba2adSLiam Girdwood 
157444c69bb1SLars-Peter Clausen static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1575b19e6e7bSMark Brown {
15763ca041edSSebastian Reichel 	struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1577f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1578*c1e230f0SKuninori Morimoto 	struct snd_soc_dai_link_component dlc;
15793ca041edSSebastian Reichel 
1580f2ed6b07SMengdong Lin 	if (aux_dev->codec_of_node || aux_dev->codec_name) {
1581f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1582*c1e230f0SKuninori Morimoto 		dlc.name = aux_dev->codec_name;
1583*c1e230f0SKuninori Morimoto 		dlc.of_node = aux_dev->codec_of_node;
1584*c1e230f0SKuninori Morimoto 		component = soc_find_component(&dlc);
1585f2ed6b07SMengdong Lin 		if (!component) {
1586*c1e230f0SKuninori Morimoto 			if (dlc.of_node)
1587*c1e230f0SKuninori Morimoto 				dlc.name = of_node_full_name(dlc.of_node);
1588f2ed6b07SMengdong Lin 			goto err_defer;
1589f2ed6b07SMengdong Lin 		}
1590f2ed6b07SMengdong Lin 	} else if (aux_dev->name) {
1591f2ed6b07SMengdong Lin 		/* generic components */
1592*c1e230f0SKuninori Morimoto 		dlc.name = aux_dev->name;
1593*c1e230f0SKuninori Morimoto 		dlc.of_node = NULL;
1594*c1e230f0SKuninori Morimoto 		component = soc_find_component(&dlc);
1595f2ed6b07SMengdong Lin 		if (!component)
1596f2ed6b07SMengdong Lin 			goto err_defer;
1597f2ed6b07SMengdong Lin 	} else {
1598f2ed6b07SMengdong Lin 		dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1599f2ed6b07SMengdong Lin 		return -EINVAL;
1600f2ed6b07SMengdong Lin 	}
16013ca041edSSebastian Reichel 
1602f2ed6b07SMengdong Lin 	component->init = aux_dev->init;
1603d2e3a135SSylwester Nawrocki 	list_add(&component->card_aux_list, &card->aux_comp_list);
16041a653aa4SKuninori Morimoto 
1605f2ed6b07SMengdong Lin 	return 0;
1606f2ed6b07SMengdong Lin 
1607f2ed6b07SMengdong Lin err_defer:
1608*c1e230f0SKuninori Morimoto 	dev_err(card->dev, "ASoC: %s not registered\n", dlc.name);
1609b19e6e7bSMark Brown 	return -EPROBE_DEFER;
1610b19e6e7bSMark Brown }
1611b19e6e7bSMark Brown 
1612f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1613f2ed6b07SMengdong Lin {
1614991454e1SKuninori Morimoto 	struct snd_soc_component *comp;
1615f2ed6b07SMengdong Lin 	int order;
1616f2ed6b07SMengdong Lin 	int ret;
1617f2ed6b07SMengdong Lin 
16181a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1619991454e1SKuninori Morimoto 		list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
1620f2ed6b07SMengdong Lin 			if (comp->driver->probe_order == order) {
1621f2ed6b07SMengdong Lin 				ret = soc_probe_component(card,	comp);
1622f2ed6b07SMengdong Lin 				if (ret < 0) {
1623f2ed6b07SMengdong Lin 					dev_err(card->dev,
1624f2ed6b07SMengdong Lin 						"ASoC: failed to probe aux component %s %d\n",
1625f2ed6b07SMengdong Lin 						comp->name, ret);
1626f2ed6b07SMengdong Lin 					return ret;
1627f2ed6b07SMengdong Lin 				}
1628f2ed6b07SMengdong Lin 			}
1629f2ed6b07SMengdong Lin 		}
1630f2ed6b07SMengdong Lin 	}
163165d9361fSLars-Peter Clausen 
163244c69bb1SLars-Peter Clausen 	return 0;
16333ca041edSSebastian Reichel }
16342eea392dSJarkko Nikula 
1635f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
163644c69bb1SLars-Peter Clausen {
1637f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1638f2ed6b07SMengdong Lin 	int order;
163944c69bb1SLars-Peter Clausen 
16401a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1641f2ed6b07SMengdong Lin 		list_for_each_entry_safe(comp, _comp,
1642991454e1SKuninori Morimoto 			&card->aux_comp_list, card_aux_list) {
16431a653aa4SKuninori Morimoto 
1644f2ed6b07SMengdong Lin 			if (comp->driver->remove_order == order) {
1645f2ed6b07SMengdong Lin 				soc_remove_component(comp);
1646991454e1SKuninori Morimoto 				/* remove it from the card's aux_comp_list */
1647991454e1SKuninori Morimoto 				list_del(&comp->card_aux_list);
16482eea392dSJarkko Nikula 			}
16495f3484acSLars-Peter Clausen 		}
16502eea392dSJarkko Nikula 	}
16512eea392dSJarkko Nikula }
16522eea392dSJarkko Nikula 
1653ce64c8b9SLars-Peter Clausen /**
1654ce64c8b9SLars-Peter Clausen  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1655ce64c8b9SLars-Peter Clausen  * @rtd: The runtime for which the DAI link format should be changed
1656ce64c8b9SLars-Peter Clausen  * @dai_fmt: The new DAI link format
1657ce64c8b9SLars-Peter Clausen  *
1658ce64c8b9SLars-Peter Clausen  * This function updates the DAI link format for all DAIs connected to the DAI
1659ce64c8b9SLars-Peter Clausen  * link for the specified runtime.
1660ce64c8b9SLars-Peter Clausen  *
1661ce64c8b9SLars-Peter Clausen  * Note: For setups with a static format set the dai_fmt field in the
1662ce64c8b9SLars-Peter Clausen  * corresponding snd_dai_link struct instead of using this function.
1663ce64c8b9SLars-Peter Clausen  *
1664ce64c8b9SLars-Peter Clausen  * Returns 0 on success, otherwise a negative error code.
1665ce64c8b9SLars-Peter Clausen  */
1666ce64c8b9SLars-Peter Clausen int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1667ce64c8b9SLars-Peter Clausen 	unsigned int dai_fmt)
1668ce64c8b9SLars-Peter Clausen {
1669ce64c8b9SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
16700b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1671ce64c8b9SLars-Peter Clausen 	unsigned int i;
1672ce64c8b9SLars-Peter Clausen 	int ret;
1673ce64c8b9SLars-Peter Clausen 
16740b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1675ce64c8b9SLars-Peter Clausen 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1676ce64c8b9SLars-Peter Clausen 		if (ret != 0 && ret != -ENOTSUPP) {
1677ce64c8b9SLars-Peter Clausen 			dev_warn(codec_dai->dev,
1678ce64c8b9SLars-Peter Clausen 				 "ASoC: Failed to set DAI format: %d\n", ret);
1679ce64c8b9SLars-Peter Clausen 			return ret;
1680ce64c8b9SLars-Peter Clausen 		}
1681ce64c8b9SLars-Peter Clausen 	}
1682ce64c8b9SLars-Peter Clausen 
16832c7b696aSMarcel Ziswiler 	/*
16842c7b696aSMarcel Ziswiler 	 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
16852c7b696aSMarcel Ziswiler 	 * the component which has non_legacy_dai_naming is Codec
16862c7b696aSMarcel Ziswiler 	 */
1687999f7f5aSKuninori Morimoto 	if (cpu_dai->component->driver->non_legacy_dai_naming) {
1688ce64c8b9SLars-Peter Clausen 		unsigned int inv_dai_fmt;
1689ce64c8b9SLars-Peter Clausen 
1690ce64c8b9SLars-Peter Clausen 		inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1691ce64c8b9SLars-Peter Clausen 		switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1692ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFM:
1693ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1694ce64c8b9SLars-Peter Clausen 			break;
1695ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBM_CFS:
1696ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1697ce64c8b9SLars-Peter Clausen 			break;
1698ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFM:
1699ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1700ce64c8b9SLars-Peter Clausen 			break;
1701ce64c8b9SLars-Peter Clausen 		case SND_SOC_DAIFMT_CBS_CFS:
1702ce64c8b9SLars-Peter Clausen 			inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1703ce64c8b9SLars-Peter Clausen 			break;
1704ce64c8b9SLars-Peter Clausen 		}
1705ce64c8b9SLars-Peter Clausen 
1706ce64c8b9SLars-Peter Clausen 		dai_fmt = inv_dai_fmt;
1707ce64c8b9SLars-Peter Clausen 	}
1708ce64c8b9SLars-Peter Clausen 
1709ce64c8b9SLars-Peter Clausen 	ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1710ce64c8b9SLars-Peter Clausen 	if (ret != 0 && ret != -ENOTSUPP) {
1711ce64c8b9SLars-Peter Clausen 		dev_warn(cpu_dai->dev,
1712ce64c8b9SLars-Peter Clausen 			 "ASoC: Failed to set DAI format: %d\n", ret);
1713ce64c8b9SLars-Peter Clausen 		return ret;
1714ce64c8b9SLars-Peter Clausen 	}
1715ce64c8b9SLars-Peter Clausen 
1716ce64c8b9SLars-Peter Clausen 	return 0;
1717ce64c8b9SLars-Peter Clausen }
1718ddaca25aSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1719ce64c8b9SLars-Peter Clausen 
17201f5a4535STakashi Iwai #ifdef CONFIG_DMI
17212c7b696aSMarcel Ziswiler /*
17222c7b696aSMarcel Ziswiler  * Trim special characters, and replace '-' with '_' since '-' is used to
1723345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1724345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1725345233d7SLiam Girdwood  */
1726345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1727345233d7SLiam Girdwood {
1728345233d7SLiam Girdwood 	int i, j = 0;
1729345233d7SLiam Girdwood 
1730345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1731345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1732345233d7SLiam Girdwood 		    || (name[i] == '_'))
1733345233d7SLiam Girdwood 			name[j++] = name[i];
1734345233d7SLiam Girdwood 		else if (name[i] == '-')
1735345233d7SLiam Girdwood 			name[j++] = '_';
1736345233d7SLiam Girdwood 	}
1737345233d7SLiam Girdwood 
1738345233d7SLiam Girdwood 	name[j] = '\0';
1739345233d7SLiam Girdwood }
1740345233d7SLiam Girdwood 
17412c7b696aSMarcel Ziswiler /*
17422c7b696aSMarcel Ziswiler  * Check if a DMI field is valid, i.e. not containing any string
174398faf436SMengdong Lin  * in the black list.
174498faf436SMengdong Lin  */
174598faf436SMengdong Lin static int is_dmi_valid(const char *field)
174698faf436SMengdong Lin {
174798faf436SMengdong Lin 	int i = 0;
174898faf436SMengdong Lin 
174998faf436SMengdong Lin 	while (dmi_blacklist[i]) {
175098faf436SMengdong Lin 		if (strstr(field, dmi_blacklist[i]))
175198faf436SMengdong Lin 			return 0;
175298faf436SMengdong Lin 		i++;
175346b5a4d2SWu Fengguang 	}
175498faf436SMengdong Lin 
175598faf436SMengdong Lin 	return 1;
175698faf436SMengdong Lin }
175798faf436SMengdong Lin 
1758345233d7SLiam Girdwood /**
1759345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
1760345233d7SLiam Girdwood  * @card: The card to register DMI names
1761345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
1762345233d7SLiam Girdwood  *
1763345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
1764345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
1765345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
1766345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
1767345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
1768345233d7SLiam Girdwood  * as the sound card long name, in the format of
1769345233d7SLiam Girdwood  * "vendor-product-version-board"
1770345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
1771345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
1772345233d7SLiam Girdwood  * (UCM) configurations for the card.
1773345233d7SLiam Girdwood  *
1774345233d7SLiam Girdwood  * Possible card long names may be:
1775345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
1776345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1777345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1778345233d7SLiam Girdwood  *
1779345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
1780345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
1781345233d7SLiam Girdwood  *
1782345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
1783345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
1784345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
1785345233d7SLiam Girdwood  * charactors like SPACE.
1786345233d7SLiam Girdwood  *
1787345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
1788345233d7SLiam Girdwood  */
1789345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1790345233d7SLiam Girdwood {
1791345233d7SLiam Girdwood 	const char *vendor, *product, *product_version, *board;
1792345233d7SLiam Girdwood 	size_t longname_buf_size = sizeof(card->snd_card->longname);
1793345233d7SLiam Girdwood 	size_t len;
1794345233d7SLiam Girdwood 
1795345233d7SLiam Girdwood 	if (card->long_name)
1796345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
1797345233d7SLiam Girdwood 
1798345233d7SLiam Girdwood 	/* make up dmi long name as: vendor.product.version.board */
1799345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
180098faf436SMengdong Lin 	if (!vendor || !is_dmi_valid(vendor)) {
1801345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1802345233d7SLiam Girdwood 		return 0;
1803345233d7SLiam Girdwood 	}
1804345233d7SLiam Girdwood 
1805345233d7SLiam Girdwood 	snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1806345233d7SLiam Girdwood 			 "%s", vendor);
1807345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
1808345233d7SLiam Girdwood 
1809345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
181098faf436SMengdong Lin 	if (product && is_dmi_valid(product)) {
1811345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1812345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1813345233d7SLiam Girdwood 			 longname_buf_size - len,
1814345233d7SLiam Girdwood 			 "-%s", product);
1815345233d7SLiam Girdwood 
1816345233d7SLiam Girdwood 		len++;	/* skip the separator "-" */
1817345233d7SLiam Girdwood 		if (len < longname_buf_size)
1818345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1819345233d7SLiam Girdwood 
18202c7b696aSMarcel Ziswiler 		/*
18212c7b696aSMarcel Ziswiler 		 * some vendors like Lenovo may only put a self-explanatory
1822345233d7SLiam Girdwood 		 * name in the product version field
1823345233d7SLiam Girdwood 		 */
1824345233d7SLiam Girdwood 		product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
182598faf436SMengdong Lin 		if (product_version && is_dmi_valid(product_version)) {
1826345233d7SLiam Girdwood 			len = strlen(card->dmi_longname);
1827345233d7SLiam Girdwood 			snprintf(card->dmi_longname + len,
1828345233d7SLiam Girdwood 				 longname_buf_size - len,
1829345233d7SLiam Girdwood 				 "-%s", product_version);
1830345233d7SLiam Girdwood 
1831345233d7SLiam Girdwood 			len++;
1832345233d7SLiam Girdwood 			if (len < longname_buf_size)
1833345233d7SLiam Girdwood 				cleanup_dmi_name(card->dmi_longname + len);
1834345233d7SLiam Girdwood 		}
1835345233d7SLiam Girdwood 	}
1836345233d7SLiam Girdwood 
1837345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
183898faf436SMengdong Lin 	if (board && is_dmi_valid(board)) {
1839345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1840345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1841345233d7SLiam Girdwood 			 longname_buf_size - len,
1842345233d7SLiam Girdwood 			 "-%s", board);
1843345233d7SLiam Girdwood 
1844345233d7SLiam Girdwood 		len++;
1845345233d7SLiam Girdwood 		if (len < longname_buf_size)
1846345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1847345233d7SLiam Girdwood 	} else if (!product) {
1848345233d7SLiam Girdwood 		/* fall back to using legacy name */
1849345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1850345233d7SLiam Girdwood 		return 0;
1851345233d7SLiam Girdwood 	}
1852345233d7SLiam Girdwood 
1853345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
1854345233d7SLiam Girdwood 	if (flavour) {
1855345233d7SLiam Girdwood 		len = strlen(card->dmi_longname);
1856345233d7SLiam Girdwood 		snprintf(card->dmi_longname + len,
1857345233d7SLiam Girdwood 			 longname_buf_size - len,
1858345233d7SLiam Girdwood 			 "-%s", flavour);
1859345233d7SLiam Girdwood 
1860345233d7SLiam Girdwood 		len++;
1861345233d7SLiam Girdwood 		if (len < longname_buf_size)
1862345233d7SLiam Girdwood 			cleanup_dmi_name(card->dmi_longname + len);
1863345233d7SLiam Girdwood 	}
1864345233d7SLiam Girdwood 
1865345233d7SLiam Girdwood 	/* set the card long name */
1866345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
1867345233d7SLiam Girdwood 
1868345233d7SLiam Girdwood 	return 0;
1869345233d7SLiam Girdwood }
1870345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
18711f5a4535STakashi Iwai #endif /* CONFIG_DMI */
1872345233d7SLiam Girdwood 
1873a655de80SLiam Girdwood static void soc_check_tplg_fes(struct snd_soc_card *card)
1874a655de80SLiam Girdwood {
1875a655de80SLiam Girdwood 	struct snd_soc_component *component;
1876a655de80SLiam Girdwood 	const struct snd_soc_component_driver *comp_drv;
1877a655de80SLiam Girdwood 	struct snd_soc_dai_link *dai_link;
1878a655de80SLiam Girdwood 	int i;
1879a655de80SLiam Girdwood 
1880368dee94SKuninori Morimoto 	for_each_component(component) {
1881a655de80SLiam Girdwood 
1882a655de80SLiam Girdwood 		/* does this component override FEs ? */
1883a655de80SLiam Girdwood 		if (!component->driver->ignore_machine)
1884a655de80SLiam Girdwood 			continue;
1885a655de80SLiam Girdwood 
1886a655de80SLiam Girdwood 		/* for this machine ? */
1887e194098bSPierre-Louis Bossart 		if (!strcmp(component->driver->ignore_machine,
1888a655de80SLiam Girdwood 			    card->dev->driver->name))
1889e194098bSPierre-Louis Bossart 			goto match;
1890e194098bSPierre-Louis Bossart 		if (strcmp(component->driver->ignore_machine,
1891e194098bSPierre-Louis Bossart 			   dev_name(card->dev)))
1892a655de80SLiam Girdwood 			continue;
1893e194098bSPierre-Louis Bossart match:
1894a655de80SLiam Girdwood 		/* machine matches, so override the rtd data */
18957fe072b4SKuninori Morimoto 		for_each_card_prelinks(card, i, dai_link) {
1896a655de80SLiam Girdwood 
1897a655de80SLiam Girdwood 			/* ignore this FE */
1898a655de80SLiam Girdwood 			if (dai_link->dynamic) {
1899a655de80SLiam Girdwood 				dai_link->ignore = true;
1900a655de80SLiam Girdwood 				continue;
1901a655de80SLiam Girdwood 			}
1902a655de80SLiam Girdwood 
1903a655de80SLiam Girdwood 			dev_info(card->dev, "info: override FE DAI link %s\n",
1904a655de80SLiam Girdwood 				 card->dai_link[i].name);
1905a655de80SLiam Girdwood 
1906a655de80SLiam Girdwood 			/* override platform component */
1907adb76b5bSKuninori Morimoto 			if (!dai_link->platforms) {
1908daecf46eSKuninori Morimoto 				dev_err(card->dev, "init platform error");
1909daecf46eSKuninori Morimoto 				continue;
1910daecf46eSKuninori Morimoto 			}
1911910fdcabSKuninori Morimoto 			dai_link->platforms->name = component->name;
1912a655de80SLiam Girdwood 
1913a655de80SLiam Girdwood 			/* convert non BE into BE */
1914a655de80SLiam Girdwood 			dai_link->no_pcm = 1;
1915a655de80SLiam Girdwood 
1916a655de80SLiam Girdwood 			/* override any BE fixups */
1917a655de80SLiam Girdwood 			dai_link->be_hw_params_fixup =
1918a655de80SLiam Girdwood 				component->driver->be_hw_params_fixup;
1919a655de80SLiam Girdwood 
19202c7b696aSMarcel Ziswiler 			/*
19212c7b696aSMarcel Ziswiler 			 * most BE links don't set stream name, so set it to
1922a655de80SLiam Girdwood 			 * dai link name if it's NULL to help bind widgets.
1923a655de80SLiam Girdwood 			 */
1924a655de80SLiam Girdwood 			if (!dai_link->stream_name)
1925a655de80SLiam Girdwood 				dai_link->stream_name = dai_link->name;
1926a655de80SLiam Girdwood 		}
1927a655de80SLiam Girdwood 
1928a655de80SLiam Girdwood 		/* Inform userspace we are using alternate topology */
1929a655de80SLiam Girdwood 		if (component->driver->topology_name_prefix) {
1930a655de80SLiam Girdwood 
1931a655de80SLiam Girdwood 			/* topology shortname created? */
1932a655de80SLiam Girdwood 			if (!card->topology_shortname_created) {
1933a655de80SLiam Girdwood 				comp_drv = component->driver;
1934a655de80SLiam Girdwood 
1935a655de80SLiam Girdwood 				snprintf(card->topology_shortname, 32, "%s-%s",
1936a655de80SLiam Girdwood 					 comp_drv->topology_name_prefix,
1937a655de80SLiam Girdwood 					 card->name);
1938a655de80SLiam Girdwood 				card->topology_shortname_created = true;
1939a655de80SLiam Girdwood 			}
1940a655de80SLiam Girdwood 
1941a655de80SLiam Girdwood 			/* use topology shortname */
1942a655de80SLiam Girdwood 			card->name = card->topology_shortname;
1943a655de80SLiam Girdwood 		}
1944a655de80SLiam Girdwood 	}
1945a655de80SLiam Girdwood }
1946a655de80SLiam Girdwood 
194753e947a0SKuninori Morimoto static int soc_cleanup_card_resources(struct snd_soc_card *card)
194853e947a0SKuninori Morimoto {
194953e947a0SKuninori Morimoto 	/* free the ALSA card at first; this syncs with pending operations */
195029040d1aSKuninori Morimoto 	if (card->snd_card) {
195153e947a0SKuninori Morimoto 		snd_card_free(card->snd_card);
195229040d1aSKuninori Morimoto 		card->snd_card = NULL;
195329040d1aSKuninori Morimoto 	}
195453e947a0SKuninori Morimoto 
195553e947a0SKuninori Morimoto 	/* remove and free each DAI */
195653e947a0SKuninori Morimoto 	soc_remove_dai_links(card);
195753e947a0SKuninori Morimoto 	soc_remove_pcm_runtimes(card);
195853e947a0SKuninori Morimoto 
195953e947a0SKuninori Morimoto 	/* remove auxiliary devices */
196053e947a0SKuninori Morimoto 	soc_remove_aux_devices(card);
196153e947a0SKuninori Morimoto 
196253e947a0SKuninori Morimoto 	snd_soc_dapm_free(&card->dapm);
196353e947a0SKuninori Morimoto 	soc_cleanup_card_debugfs(card);
196453e947a0SKuninori Morimoto 
196553e947a0SKuninori Morimoto 	/* remove the card */
196653e947a0SKuninori Morimoto 	if (card->remove)
196753e947a0SKuninori Morimoto 		card->remove(card);
196853e947a0SKuninori Morimoto 
196953e947a0SKuninori Morimoto 	return 0;
197053e947a0SKuninori Morimoto }
197153e947a0SKuninori Morimoto 
1972b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card)
1973f0fba2adSLiam Girdwood {
19741a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
197561b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
1976ce64c8b9SLars-Peter Clausen 	int ret, i, order;
197796dd3622SMark Brown 
197834e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
197970fc5373STzung-Bi Shih 	for_each_card_prelinks(card, i, dai_link) {
198070fc5373STzung-Bi Shih 		ret = soc_init_dai_link(card, dai_link);
198170fc5373STzung-Bi Shih 		if (ret) {
198270fc5373STzung-Bi Shih 			dev_err(card->dev, "ASoC: failed to init link %s: %d\n",
198370fc5373STzung-Bi Shih 				dai_link->name, ret);
198470fc5373STzung-Bi Shih 			mutex_unlock(&client_mutex);
198570fc5373STzung-Bi Shih 			return ret;
198670fc5373STzung-Bi Shih 		}
198770fc5373STzung-Bi Shih 	}
198801b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1989f0fba2adSLiam Girdwood 
199053e947a0SKuninori Morimoto 	card->dapm.bias_level = SND_SOC_BIAS_OFF;
199153e947a0SKuninori Morimoto 	card->dapm.dev = card->dev;
199253e947a0SKuninori Morimoto 	card->dapm.card = card;
199353e947a0SKuninori Morimoto 	list_add(&card->dapm.list, &card->dapm_list);
199453e947a0SKuninori Morimoto 
1995a655de80SLiam Girdwood 	/* check whether any platform is ignore machine FE and using topology */
1996a655de80SLiam Girdwood 	soc_check_tplg_fes(card);
1997a655de80SLiam Girdwood 
1998b19e6e7bSMark Brown 	/* bind DAIs */
19997fe072b4SKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link) {
20007fe072b4SKuninori Morimoto 		ret = soc_bind_dai_link(card, dai_link);
2001b19e6e7bSMark Brown 		if (ret != 0)
200253e947a0SKuninori Morimoto 			goto probe_end;
2003db2a4165SFrank Mandarino 	}
2004db2a4165SFrank Mandarino 
200544c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
2006b19e6e7bSMark Brown 	for (i = 0; i < card->num_aux_devs; i++) {
200744c69bb1SLars-Peter Clausen 		ret = soc_bind_aux_dev(card, i);
2008b19e6e7bSMark Brown 		if (ret != 0)
200953e947a0SKuninori Morimoto 			goto probe_end;
2010db2a4165SFrank Mandarino 	}
2011db2a4165SFrank Mandarino 
2012f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
20137fe072b4SKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link)
20147fe072b4SKuninori Morimoto 		snd_soc_add_dai_link(card, dai_link);
2015f8f80361SMengdong Lin 
2016f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
2017102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2018f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
2019f0fba2adSLiam Girdwood 	if (ret < 0) {
202010e8aa9aSMichał Mirosław 		dev_err(card->dev,
202110e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
202210e8aa9aSMichał Mirosław 			card->name, ret);
202353e947a0SKuninori Morimoto 		goto probe_end;
2024db2a4165SFrank Mandarino 	}
2025db2a4165SFrank Mandarino 
20260757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
20270757d834SLars-Peter Clausen 
2028d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS
2029d5d1e0beSLars-Peter Clausen 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2030d5d1e0beSLars-Peter Clausen #endif
2031d5d1e0beSLars-Peter Clausen 
203288ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP
20336ed25978SAndy Green 	/* deferred resume work */
20346308419aSMark Brown 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
20351301a964SRandy Dunlap #endif
20366ed25978SAndy Green 
20379a841ebbSMark Brown 	if (card->dapm_widgets)
20389a841ebbSMark Brown 		snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
20399a841ebbSMark Brown 					  card->num_dapm_widgets);
20409a841ebbSMark Brown 
2041f23e860eSNicolin Chen 	if (card->of_dapm_widgets)
2042f23e860eSNicolin Chen 		snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2043f23e860eSNicolin Chen 					  card->num_of_dapm_widgets);
2044f23e860eSNicolin Chen 
2045f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
2046f0fba2adSLiam Girdwood 	if (card->probe) {
2047e7361ec4SMark Brown 		ret = card->probe(card);
2048f0fba2adSLiam Girdwood 		if (ret < 0)
204953e947a0SKuninori Morimoto 			goto probe_end;
2050f0fba2adSLiam Girdwood 	}
2051f0fba2adSLiam Girdwood 
205262ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
20531a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
2054bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
20551a497983SMengdong Lin 			ret = soc_probe_link_components(card, rtd, order);
205662ae68faSStephen Warren 			if (ret < 0) {
2057f110bfc7SLiam Girdwood 				dev_err(card->dev,
2058f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
2059f110bfc7SLiam Girdwood 					ret);
206053e947a0SKuninori Morimoto 				goto probe_end;
206162ae68faSStephen Warren 			}
206262ae68faSStephen Warren 		}
206362ae68faSStephen Warren 	}
206462ae68faSStephen Warren 
2065f2ed6b07SMengdong Lin 	/* probe auxiliary components */
2066f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
2067f2ed6b07SMengdong Lin 	if (ret < 0)
206853e947a0SKuninori Morimoto 		goto probe_end;
2069f2ed6b07SMengdong Lin 
20702c7b696aSMarcel Ziswiler 	/*
20712c7b696aSMarcel Ziswiler 	 * Find new DAI links added during probing components and bind them.
207261b0088bSMengdong Lin 	 * Components with topology may bring new DAIs and DAI links.
207361b0088bSMengdong Lin 	 */
207498061fdbSKuninori Morimoto 	for_each_card_links(card, dai_link) {
207561b0088bSMengdong Lin 		if (soc_is_dai_link_bound(card, dai_link))
207661b0088bSMengdong Lin 			continue;
207761b0088bSMengdong Lin 
207861b0088bSMengdong Lin 		ret = soc_init_dai_link(card, dai_link);
207961b0088bSMengdong Lin 		if (ret)
208053e947a0SKuninori Morimoto 			goto probe_end;
208161b0088bSMengdong Lin 		ret = soc_bind_dai_link(card, dai_link);
208261b0088bSMengdong Lin 		if (ret)
208353e947a0SKuninori Morimoto 			goto probe_end;
208461b0088bSMengdong Lin 	}
208561b0088bSMengdong Lin 
208662ae68faSStephen Warren 	/* probe all DAI links on this card */
20871a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
2088bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
20891a497983SMengdong Lin 			ret = soc_probe_link_dais(card, rtd, order);
2090fe3e78e0SMark Brown 			if (ret < 0) {
2091f110bfc7SLiam Girdwood 				dev_err(card->dev,
2092f110bfc7SLiam Girdwood 					"ASoC: failed to instantiate card %d\n",
2093f110bfc7SLiam Girdwood 					ret);
209453e947a0SKuninori Morimoto 				goto probe_end;
2095fe3e78e0SMark Brown 			}
2096fe3e78e0SMark Brown 		}
20970168bf0dSLiam Girdwood 	}
2098fe3e78e0SMark Brown 
2099888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
2100b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
2101888df395SMark Brown 
2102b7af1dafSMark Brown 	if (card->controls)
21032c7b696aSMarcel Ziswiler 		snd_soc_add_card_controls(card, card->controls,
21042c7b696aSMarcel Ziswiler 					  card->num_controls);
2105b7af1dafSMark Brown 
2106b8ad29deSMark Brown 	if (card->dapm_routes)
2107b8ad29deSMark Brown 		snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2108b8ad29deSMark Brown 					card->num_dapm_routes);
2109b8ad29deSMark Brown 
2110f23e860eSNicolin Chen 	if (card->of_dapm_routes)
2111f23e860eSNicolin Chen 		snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2112f23e860eSNicolin Chen 					card->num_of_dapm_routes);
211375d9ac46SMark Brown 
2114861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
2115861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
2116861886d3STakashi Iwai 
2117f0fba2adSLiam Girdwood 	snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
2118fe3e78e0SMark Brown 		 "%s", card->name);
2119f0fba2adSLiam Girdwood 	snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
212022de71baSLiam Girdwood 		 "%s", card->long_name ? card->long_name : card->name);
2121f0e8ed85SMark Brown 	snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2122f0e8ed85SMark Brown 		 "%s", card->driver_name ? card->driver_name : card->name);
2123f0e8ed85SMark Brown 	for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2124f0e8ed85SMark Brown 		switch (card->snd_card->driver[i]) {
2125f0e8ed85SMark Brown 		case '_':
2126f0e8ed85SMark Brown 		case '-':
2127f0e8ed85SMark Brown 		case '\0':
2128f0e8ed85SMark Brown 			break;
2129f0e8ed85SMark Brown 		default:
2130f0e8ed85SMark Brown 			if (!isalnum(card->snd_card->driver[i]))
2131f0e8ed85SMark Brown 				card->snd_card->driver[i] = '_';
2132f0e8ed85SMark Brown 			break;
2133f0e8ed85SMark Brown 		}
2134f0e8ed85SMark Brown 	}
2135fe3e78e0SMark Brown 
213628e9ad92SMark Brown 	if (card->late_probe) {
213728e9ad92SMark Brown 		ret = card->late_probe(card);
213828e9ad92SMark Brown 		if (ret < 0) {
2139f110bfc7SLiam Girdwood 			dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
214028e9ad92SMark Brown 				card->name, ret);
214153e947a0SKuninori Morimoto 			goto probe_end;
214228e9ad92SMark Brown 		}
214328e9ad92SMark Brown 	}
214428e9ad92SMark Brown 
2145824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
21468c193b8dSLars-Peter Clausen 
2147f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2148fe3e78e0SMark Brown 	if (ret < 0) {
2149f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2150f110bfc7SLiam Girdwood 				ret);
215153e947a0SKuninori Morimoto 		goto probe_end;
2152fe3e78e0SMark Brown 	}
2153fe3e78e0SMark Brown 
2154435c5e25SMark Brown 	card->instantiated = 1;
2155882eab6cSTzung-Bi Shih 	dapm_mark_endpoints_dirty(card);
21564f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2157b19e6e7bSMark Brown 
215853e947a0SKuninori Morimoto probe_end:
215953e947a0SKuninori Morimoto 	if (ret < 0)
216053e947a0SKuninori Morimoto 		soc_cleanup_card_resources(card);
2161db2a4165SFrank Mandarino 
2162f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
216334e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2164db2a4165SFrank Mandarino 
2165b19e6e7bSMark Brown 	return ret;
2166435c5e25SMark Brown }
2167435c5e25SMark Brown 
2168435c5e25SMark Brown /* probes a new socdev */
2169435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2170435c5e25SMark Brown {
2171f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2172435c5e25SMark Brown 
217370a7ca34SVinod Koul 	/*
217470a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
217570a7ca34SVinod Koul 	 * we should not be here in that case so ret error
217670a7ca34SVinod Koul 	 */
217770a7ca34SVinod Koul 	if (!card)
217870a7ca34SVinod Koul 		return -EINVAL;
217970a7ca34SVinod Koul 
2180fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2181f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2182fe4085e8SMark Brown 		 card->name);
2183fe4085e8SMark Brown 
2184435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2185435c5e25SMark Brown 	card->dev = &pdev->dev;
2186f0fba2adSLiam Girdwood 
218728d528c8SMark Brown 	return snd_soc_register_card(card);
2188435c5e25SMark Brown }
2189435c5e25SMark Brown 
2190b0e26485SVinod Koul /* removes a socdev */
2191b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev)
2192b0e26485SVinod Koul {
2193b0e26485SVinod Koul 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2194b0e26485SVinod Koul 
2195c5af3a2eSMark Brown 	snd_soc_unregister_card(card);
2196db2a4165SFrank Mandarino 	return 0;
2197db2a4165SFrank Mandarino }
2198db2a4165SFrank Mandarino 
21996f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
220051737470SMark Brown {
22016f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
22021a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
220351737470SMark Brown 
220451737470SMark Brown 	if (!card->instantiated)
2205416356fcSMark Brown 		return 0;
220651737470SMark Brown 
22072c7b696aSMarcel Ziswiler 	/*
22082c7b696aSMarcel Ziswiler 	 * Flush out pmdown_time work - we actually do want to run it
22092c7b696aSMarcel Ziswiler 	 * now, we're shutting down so no imminent restart.
22102c7b696aSMarcel Ziswiler 	 */
221165462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
221251737470SMark Brown 
2213f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2214416356fcSMark Brown 
2215988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
2216bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
221788bd870fSBenoit Cousson 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
22180b7990e3SKuninori Morimoto 		struct snd_soc_dai *codec_dai;
22191a497983SMengdong Lin 		int i;
222088bd870fSBenoit Cousson 
2221988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
22220b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
222388bd870fSBenoit Cousson 			pinctrl_pm_select_sleep_state(codec_dai->dev);
222488bd870fSBenoit Cousson 		}
2225988e8cc4SNicolin Chen 	}
2226988e8cc4SNicolin Chen 
2227416356fcSMark Brown 	return 0;
222851737470SMark Brown }
22296f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
223051737470SMark Brown 
22316f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2232b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2233b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2234b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2235b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
22366f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2237b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2238416356fcSMark Brown };
2239deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2240416356fcSMark Brown 
2241db2a4165SFrank Mandarino /* ASoC platform driver */
2242db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2243db2a4165SFrank Mandarino 	.driver		= {
2244db2a4165SFrank Mandarino 		.name		= "soc-audio",
22456f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2246db2a4165SFrank Mandarino 	},
2247db2a4165SFrank Mandarino 	.probe		= soc_probe,
2248db2a4165SFrank Mandarino 	.remove		= soc_remove,
2249db2a4165SFrank Mandarino };
2250db2a4165SFrank Mandarino 
2251096e49d5SMark Brown /**
2252db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2253db2a4165SFrank Mandarino  * @_template: control template
2254db2a4165SFrank Mandarino  * @data: control private data
2255ac11a2b3SMark Brown  * @long_name: control long name
2256efb7ac3fSMark Brown  * @prefix: control name prefix
2257db2a4165SFrank Mandarino  *
2258db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2259db2a4165SFrank Mandarino  *
2260db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2261db2a4165SFrank Mandarino  */
2262db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
22633056557fSMark Brown 				  void *data, const char *long_name,
2264efb7ac3fSMark Brown 				  const char *prefix)
2265db2a4165SFrank Mandarino {
2266db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2267efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2268efb7ac3fSMark Brown 	char *name = NULL;
2269db2a4165SFrank Mandarino 
2270db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2271db2a4165SFrank Mandarino 	template.index = 0;
2272db2a4165SFrank Mandarino 
2273efb7ac3fSMark Brown 	if (!long_name)
2274efb7ac3fSMark Brown 		long_name = template.name;
2275efb7ac3fSMark Brown 
2276efb7ac3fSMark Brown 	if (prefix) {
22772b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2278efb7ac3fSMark Brown 		if (!name)
2279efb7ac3fSMark Brown 			return NULL;
2280efb7ac3fSMark Brown 
2281efb7ac3fSMark Brown 		template.name = name;
2282efb7ac3fSMark Brown 	} else {
2283efb7ac3fSMark Brown 		template.name = long_name;
2284efb7ac3fSMark Brown 	}
2285efb7ac3fSMark Brown 
2286efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2287efb7ac3fSMark Brown 
2288efb7ac3fSMark Brown 	kfree(name);
2289efb7ac3fSMark Brown 
2290efb7ac3fSMark Brown 	return kcontrol;
2291db2a4165SFrank Mandarino }
2292db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2293db2a4165SFrank Mandarino 
2294022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2295022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2296022658beSLiam Girdwood 	const char *prefix, void *data)
2297022658beSLiam Girdwood {
2298022658beSLiam Girdwood 	int err, i;
2299022658beSLiam Girdwood 
2300022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2301022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
23022c7b696aSMarcel Ziswiler 
2303022658beSLiam Girdwood 		err = snd_ctl_add(card, snd_soc_cnew(control, data,
2304022658beSLiam Girdwood 						     control->name, prefix));
2305022658beSLiam Girdwood 		if (err < 0) {
2306f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2307f110bfc7SLiam Girdwood 				control->name, err);
2308022658beSLiam Girdwood 			return err;
2309022658beSLiam Girdwood 		}
2310022658beSLiam Girdwood 	}
2311022658beSLiam Girdwood 
2312022658beSLiam Girdwood 	return 0;
2313022658beSLiam Girdwood }
2314022658beSLiam Girdwood 
23154fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
23164fefd698SDimitris Papastamos 					       const char *name)
23174fefd698SDimitris Papastamos {
23184fefd698SDimitris Papastamos 	struct snd_card *card = soc_card->snd_card;
23194fefd698SDimitris Papastamos 	struct snd_kcontrol *kctl;
23204fefd698SDimitris Papastamos 
23214fefd698SDimitris Papastamos 	if (unlikely(!name))
23224fefd698SDimitris Papastamos 		return NULL;
23234fefd698SDimitris Papastamos 
23244fefd698SDimitris Papastamos 	list_for_each_entry(kctl, &card->controls, list)
23254fefd698SDimitris Papastamos 		if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
23264fefd698SDimitris Papastamos 			return kctl;
23274fefd698SDimitris Papastamos 	return NULL;
23284fefd698SDimitris Papastamos }
23294fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
23304fefd698SDimitris Papastamos 
2331db2a4165SFrank Mandarino /**
23320f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
23330f2780adSLars-Peter Clausen  *
23340f2780adSLars-Peter Clausen  * @component: Component to add controls to
23350f2780adSLars-Peter Clausen  * @controls: Array of controls to add
23360f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
23370f2780adSLars-Peter Clausen  *
23380f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
23390f2780adSLars-Peter Clausen  */
23400f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
23410f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
23420f2780adSLars-Peter Clausen {
23430f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
23440f2780adSLars-Peter Clausen 
23450f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
23460f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
23470f2780adSLars-Peter Clausen }
23480f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
23490f2780adSLars-Peter Clausen 
23500f2780adSLars-Peter Clausen /**
2351022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2352022658beSLiam Girdwood  * Convenience function to add a list of controls.
2353022658beSLiam Girdwood  *
2354022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2355022658beSLiam Girdwood  * @controls: array of controls to add
2356022658beSLiam Girdwood  * @num_controls: number of elements in the array
2357022658beSLiam Girdwood  *
2358022658beSLiam Girdwood  * Return 0 for success, else error.
2359022658beSLiam Girdwood  */
2360022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2361022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2362022658beSLiam Girdwood {
2363022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2364022658beSLiam Girdwood 
2365022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2366022658beSLiam Girdwood 			NULL, soc_card);
2367022658beSLiam Girdwood }
2368022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2369022658beSLiam Girdwood 
2370022658beSLiam Girdwood /**
2371022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2372022658beSLiam Girdwood  * Convienience function to add a list of controls.
2373022658beSLiam Girdwood  *
2374022658beSLiam Girdwood  * @dai: DAI to add controls to
2375022658beSLiam Girdwood  * @controls: array of controls to add
2376022658beSLiam Girdwood  * @num_controls: number of elements in the array
2377022658beSLiam Girdwood  *
2378022658beSLiam Girdwood  * Return 0 for success, else error.
2379022658beSLiam Girdwood  */
2380022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2381022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2382022658beSLiam Girdwood {
2383313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2384022658beSLiam Girdwood 
2385022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2386022658beSLiam Girdwood 			NULL, dai);
2387022658beSLiam Girdwood }
2388022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2389022658beSLiam Girdwood 
2390022658beSLiam Girdwood /**
23918c6529dbSLiam Girdwood  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
23928c6529dbSLiam Girdwood  * @dai: DAI
23938c6529dbSLiam Girdwood  * @clk_id: DAI specific clock ID
23948c6529dbSLiam Girdwood  * @freq: new clock frequency in Hz
23958c6529dbSLiam Girdwood  * @dir: new clock direction - input/output.
23968c6529dbSLiam Girdwood  *
23978c6529dbSLiam Girdwood  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
23988c6529dbSLiam Girdwood  */
23998c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
24008c6529dbSLiam Girdwood 	unsigned int freq, int dir)
24018c6529dbSLiam Girdwood {
240246471925SKuninori Morimoto 	if (dai->driver->ops->set_sysclk)
2403f0fba2adSLiam Girdwood 		return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
240471ccef0dSKuninori Morimoto 
240571ccef0dSKuninori Morimoto 	return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2406ec4ee52aSMark Brown 					    freq, dir);
24078c6529dbSLiam Girdwood }
24088c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
24098c6529dbSLiam Girdwood 
24108c6529dbSLiam Girdwood /**
241171ccef0dSKuninori Morimoto  * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
241271ccef0dSKuninori Morimoto  * @component: COMPONENT
241371ccef0dSKuninori Morimoto  * @clk_id: DAI specific clock ID
241471ccef0dSKuninori Morimoto  * @source: Source for the clock
241571ccef0dSKuninori Morimoto  * @freq: new clock frequency in Hz
241671ccef0dSKuninori Morimoto  * @dir: new clock direction - input/output.
241771ccef0dSKuninori Morimoto  *
241871ccef0dSKuninori Morimoto  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
241971ccef0dSKuninori Morimoto  */
24202c7b696aSMarcel Ziswiler int snd_soc_component_set_sysclk(struct snd_soc_component *component,
24212c7b696aSMarcel Ziswiler 				 int clk_id, int source, unsigned int freq,
24222c7b696aSMarcel Ziswiler 				 int dir)
242371ccef0dSKuninori Morimoto {
242471ccef0dSKuninori Morimoto 	if (component->driver->set_sysclk)
242571ccef0dSKuninori Morimoto 		return component->driver->set_sysclk(component, clk_id, source,
242671ccef0dSKuninori Morimoto 						 freq, dir);
242771ccef0dSKuninori Morimoto 
242871ccef0dSKuninori Morimoto 	return -ENOTSUPP;
242971ccef0dSKuninori Morimoto }
243071ccef0dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
243171ccef0dSKuninori Morimoto 
243271ccef0dSKuninori Morimoto /**
24338c6529dbSLiam Girdwood  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
24348c6529dbSLiam Girdwood  * @dai: DAI
2435ac11a2b3SMark Brown  * @div_id: DAI specific clock divider ID
24368c6529dbSLiam Girdwood  * @div: new clock divisor.
24378c6529dbSLiam Girdwood  *
24388c6529dbSLiam Girdwood  * Configures the clock dividers. This is used to derive the best DAI bit and
24398c6529dbSLiam Girdwood  * frame clocks from the system or master clock. It's best to set the DAI bit
24408c6529dbSLiam Girdwood  * and frame clocks as low as possible to save system power.
24418c6529dbSLiam Girdwood  */
24428c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
24438c6529dbSLiam Girdwood 	int div_id, int div)
24448c6529dbSLiam Girdwood {
244546471925SKuninori Morimoto 	if (dai->driver->ops->set_clkdiv)
2446f0fba2adSLiam Girdwood 		return dai->driver->ops->set_clkdiv(dai, div_id, div);
24478c6529dbSLiam Girdwood 	else
24488c6529dbSLiam Girdwood 		return -EINVAL;
24498c6529dbSLiam Girdwood }
24508c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
24518c6529dbSLiam Girdwood 
24528c6529dbSLiam Girdwood /**
24538c6529dbSLiam Girdwood  * snd_soc_dai_set_pll - configure DAI PLL.
24548c6529dbSLiam Girdwood  * @dai: DAI
24558c6529dbSLiam Girdwood  * @pll_id: DAI specific PLL ID
245685488037SMark Brown  * @source: DAI specific source for the PLL
24578c6529dbSLiam Girdwood  * @freq_in: PLL input clock frequency in Hz
24588c6529dbSLiam Girdwood  * @freq_out: requested PLL output clock frequency in Hz
24598c6529dbSLiam Girdwood  *
24608c6529dbSLiam Girdwood  * Configures and enables PLL to generate output clock based on input clock.
24618c6529dbSLiam Girdwood  */
246285488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
246385488037SMark Brown 	unsigned int freq_in, unsigned int freq_out)
24648c6529dbSLiam Girdwood {
246546471925SKuninori Morimoto 	if (dai->driver->ops->set_pll)
2466f0fba2adSLiam Girdwood 		return dai->driver->ops->set_pll(dai, pll_id, source,
246785488037SMark Brown 					 freq_in, freq_out);
2468ef641e5dSKuninori Morimoto 
2469ef641e5dSKuninori Morimoto 	return snd_soc_component_set_pll(dai->component, pll_id, source,
2470ec4ee52aSMark Brown 					 freq_in, freq_out);
24718c6529dbSLiam Girdwood }
24728c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
24738c6529dbSLiam Girdwood 
2474ec4ee52aSMark Brown /*
2475ef641e5dSKuninori Morimoto  * snd_soc_component_set_pll - configure component PLL.
2476ef641e5dSKuninori Morimoto  * @component: COMPONENT
2477ef641e5dSKuninori Morimoto  * @pll_id: DAI specific PLL ID
2478ef641e5dSKuninori Morimoto  * @source: DAI specific source for the PLL
2479ef641e5dSKuninori Morimoto  * @freq_in: PLL input clock frequency in Hz
2480ef641e5dSKuninori Morimoto  * @freq_out: requested PLL output clock frequency in Hz
2481ef641e5dSKuninori Morimoto  *
2482ef641e5dSKuninori Morimoto  * Configures and enables PLL to generate output clock based on input clock.
2483ef641e5dSKuninori Morimoto  */
2484ef641e5dSKuninori Morimoto int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2485ef641e5dSKuninori Morimoto 			      int source, unsigned int freq_in,
2486ef641e5dSKuninori Morimoto 			      unsigned int freq_out)
2487ef641e5dSKuninori Morimoto {
2488ef641e5dSKuninori Morimoto 	if (component->driver->set_pll)
2489ef641e5dSKuninori Morimoto 		return component->driver->set_pll(component, pll_id, source,
2490ef641e5dSKuninori Morimoto 						  freq_in, freq_out);
2491ef641e5dSKuninori Morimoto 
2492ef641e5dSKuninori Morimoto 	return -EINVAL;
2493ef641e5dSKuninori Morimoto }
2494ef641e5dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2495ef641e5dSKuninori Morimoto 
24968c6529dbSLiam Girdwood /**
2497e54cf76bSLiam Girdwood  * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2498e54cf76bSLiam Girdwood  * @dai: DAI
2499231b86b1SMasanari Iida  * @ratio: Ratio of BCLK to Sample rate.
2500e54cf76bSLiam Girdwood  *
2501e54cf76bSLiam Girdwood  * Configures the DAI for a preset BCLK to sample rate ratio.
2502e54cf76bSLiam Girdwood  */
2503e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2504e54cf76bSLiam Girdwood {
250546471925SKuninori Morimoto 	if (dai->driver->ops->set_bclk_ratio)
2506e54cf76bSLiam Girdwood 		return dai->driver->ops->set_bclk_ratio(dai, ratio);
2507e54cf76bSLiam Girdwood 	else
2508e54cf76bSLiam Girdwood 		return -EINVAL;
2509e54cf76bSLiam Girdwood }
2510e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2511e54cf76bSLiam Girdwood 
2512e54cf76bSLiam Girdwood /**
25138c6529dbSLiam Girdwood  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
25148c6529dbSLiam Girdwood  * @dai: DAI
2515bb19ba2aSRandy Dunlap  * @fmt: SND_SOC_DAIFMT_* format value.
25168c6529dbSLiam Girdwood  *
25178c6529dbSLiam Girdwood  * Configures the DAI hardware format and clocking.
25188c6529dbSLiam Girdwood  */
25198c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
25208c6529dbSLiam Girdwood {
25215e4ba569SShawn Guo 	if (dai->driver->ops->set_fmt == NULL)
25225e4ba569SShawn Guo 		return -ENOTSUPP;
25235e4ba569SShawn Guo 	return dai->driver->ops->set_fmt(dai, fmt);
25248c6529dbSLiam Girdwood }
25258c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
25268c6529dbSLiam Girdwood 
25278c6529dbSLiam Girdwood /**
2528e5c21514SXiubo Li  * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
252989c67857SXiubo Li  * @slots: Number of slots in use.
253089c67857SXiubo Li  * @tx_mask: bitmask representing active TX slots.
253189c67857SXiubo Li  * @rx_mask: bitmask representing active RX slots.
253289c67857SXiubo Li  *
253389c67857SXiubo Li  * Generates the TDM tx and rx slot default masks for DAI.
253489c67857SXiubo Li  */
2535e5c21514SXiubo Li static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
253689c67857SXiubo Li 				       unsigned int *tx_mask,
253789c67857SXiubo Li 				       unsigned int *rx_mask)
253889c67857SXiubo Li {
253989c67857SXiubo Li 	if (*tx_mask || *rx_mask)
254089c67857SXiubo Li 		return 0;
254189c67857SXiubo Li 
254289c67857SXiubo Li 	if (!slots)
254389c67857SXiubo Li 		return -EINVAL;
254489c67857SXiubo Li 
254589c67857SXiubo Li 	*tx_mask = (1 << slots) - 1;
254689c67857SXiubo Li 	*rx_mask = (1 << slots) - 1;
254789c67857SXiubo Li 
254889c67857SXiubo Li 	return 0;
254989c67857SXiubo Li }
255089c67857SXiubo Li 
255189c67857SXiubo Li /**
2552e46c9366SLars-Peter Clausen  * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2553e46c9366SLars-Peter Clausen  * @dai: The DAI to configure
2554a5479e38SDaniel Ribeiro  * @tx_mask: bitmask representing active TX slots.
2555a5479e38SDaniel Ribeiro  * @rx_mask: bitmask representing active RX slots.
25568c6529dbSLiam Girdwood  * @slots: Number of slots in use.
2557a5479e38SDaniel Ribeiro  * @slot_width: Width in bits for each slot.
25588c6529dbSLiam Girdwood  *
2559e46c9366SLars-Peter Clausen  * This function configures the specified DAI for TDM operation. @slot contains
2560e46c9366SLars-Peter Clausen  * the total number of slots of the TDM stream and @slot_with the width of each
2561e46c9366SLars-Peter Clausen  * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2562e46c9366SLars-Peter Clausen  * active slots of the TDM stream for the specified DAI, i.e. which slots the
2563e46c9366SLars-Peter Clausen  * DAI should write to or read from. If a bit is set the corresponding slot is
2564e46c9366SLars-Peter Clausen  * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2565e46c9366SLars-Peter Clausen  * the first slot, bit 1 to the second slot and so on. The first active slot
2566e46c9366SLars-Peter Clausen  * maps to the first channel of the DAI, the second active slot to the second
2567e46c9366SLars-Peter Clausen  * channel and so on.
2568e46c9366SLars-Peter Clausen  *
2569e46c9366SLars-Peter Clausen  * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2570e46c9366SLars-Peter Clausen  * @rx_mask and @slot_width will be ignored.
2571e46c9366SLars-Peter Clausen  *
2572e46c9366SLars-Peter Clausen  * Returns 0 on success, a negative error code otherwise.
25738c6529dbSLiam Girdwood  */
25748c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2575a5479e38SDaniel Ribeiro 	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
25768c6529dbSLiam Girdwood {
257746471925SKuninori Morimoto 	if (dai->driver->ops->xlate_tdm_slot_mask)
2578e5c21514SXiubo Li 		dai->driver->ops->xlate_tdm_slot_mask(slots,
257989c67857SXiubo Li 						&tx_mask, &rx_mask);
258089c67857SXiubo Li 	else
2581e5c21514SXiubo Li 		snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
258289c67857SXiubo Li 
258388bd870fSBenoit Cousson 	dai->tx_mask = tx_mask;
258488bd870fSBenoit Cousson 	dai->rx_mask = rx_mask;
258588bd870fSBenoit Cousson 
258646471925SKuninori Morimoto 	if (dai->driver->ops->set_tdm_slot)
2587f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2588a5479e38SDaniel Ribeiro 				slots, slot_width);
25898c6529dbSLiam Girdwood 	else
2590b2cbb6e1SXiubo Li 		return -ENOTSUPP;
25918c6529dbSLiam Girdwood }
25928c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
25938c6529dbSLiam Girdwood 
25948c6529dbSLiam Girdwood /**
2595472df3cbSBarry Song  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2596472df3cbSBarry Song  * @dai: DAI
2597472df3cbSBarry Song  * @tx_num: how many TX channels
2598472df3cbSBarry Song  * @tx_slot: pointer to an array which imply the TX slot number channel
2599472df3cbSBarry Song  *           0~num-1 uses
2600472df3cbSBarry Song  * @rx_num: how many RX channels
2601472df3cbSBarry Song  * @rx_slot: pointer to an array which imply the RX slot number channel
2602472df3cbSBarry Song  *           0~num-1 uses
2603472df3cbSBarry Song  *
2604472df3cbSBarry Song  * configure the relationship between channel number and TDM slot number.
2605472df3cbSBarry Song  */
2606472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2607472df3cbSBarry Song 	unsigned int tx_num, unsigned int *tx_slot,
2608472df3cbSBarry Song 	unsigned int rx_num, unsigned int *rx_slot)
2609472df3cbSBarry Song {
261046471925SKuninori Morimoto 	if (dai->driver->ops->set_channel_map)
2611f0fba2adSLiam Girdwood 		return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2612472df3cbSBarry Song 			rx_num, rx_slot);
2613472df3cbSBarry Song 	else
2614472df3cbSBarry Song 		return -EINVAL;
2615472df3cbSBarry Song }
2616472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2617472df3cbSBarry Song 
2618472df3cbSBarry Song /**
2619467b061fSSrinivas Kandagatla  * snd_soc_dai_get_channel_map - Get DAI audio channel map
2620467b061fSSrinivas Kandagatla  * @dai: DAI
2621467b061fSSrinivas Kandagatla  * @tx_num: how many TX channels
2622467b061fSSrinivas Kandagatla  * @tx_slot: pointer to an array which imply the TX slot number channel
2623467b061fSSrinivas Kandagatla  *           0~num-1 uses
2624467b061fSSrinivas Kandagatla  * @rx_num: how many RX channels
2625467b061fSSrinivas Kandagatla  * @rx_slot: pointer to an array which imply the RX slot number channel
2626467b061fSSrinivas Kandagatla  *           0~num-1 uses
2627467b061fSSrinivas Kandagatla  */
2628467b061fSSrinivas Kandagatla int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
2629467b061fSSrinivas Kandagatla 	unsigned int *tx_num, unsigned int *tx_slot,
2630467b061fSSrinivas Kandagatla 	unsigned int *rx_num, unsigned int *rx_slot)
2631467b061fSSrinivas Kandagatla {
2632467b061fSSrinivas Kandagatla 	if (dai->driver->ops->get_channel_map)
2633467b061fSSrinivas Kandagatla 		return dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
2634467b061fSSrinivas Kandagatla 			rx_num, rx_slot);
2635467b061fSSrinivas Kandagatla 	else
2636467b061fSSrinivas Kandagatla 		return -ENOTSUPP;
2637467b061fSSrinivas Kandagatla }
2638467b061fSSrinivas Kandagatla EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
2639467b061fSSrinivas Kandagatla 
2640467b061fSSrinivas Kandagatla /**
26418c6529dbSLiam Girdwood  * snd_soc_dai_set_tristate - configure DAI system or master clock.
26428c6529dbSLiam Girdwood  * @dai: DAI
26438c6529dbSLiam Girdwood  * @tristate: tristate enable
26448c6529dbSLiam Girdwood  *
26458c6529dbSLiam Girdwood  * Tristates the DAI so that others can use it.
26468c6529dbSLiam Girdwood  */
26478c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
26488c6529dbSLiam Girdwood {
264946471925SKuninori Morimoto 	if (dai->driver->ops->set_tristate)
2650f0fba2adSLiam Girdwood 		return dai->driver->ops->set_tristate(dai, tristate);
26518c6529dbSLiam Girdwood 	else
26528c6529dbSLiam Girdwood 		return -EINVAL;
26538c6529dbSLiam Girdwood }
26548c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
26558c6529dbSLiam Girdwood 
26568c6529dbSLiam Girdwood /**
26578c6529dbSLiam Girdwood  * snd_soc_dai_digital_mute - configure DAI system or master clock.
26588c6529dbSLiam Girdwood  * @dai: DAI
26598c6529dbSLiam Girdwood  * @mute: mute enable
2660da18396fSMark Brown  * @direction: stream to mute
26618c6529dbSLiam Girdwood  *
26628c6529dbSLiam Girdwood  * Mutes the DAI DAC.
26638c6529dbSLiam Girdwood  */
2664da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2665da18396fSMark Brown 			     int direction)
26668c6529dbSLiam Girdwood {
2667da18396fSMark Brown 	if (dai->driver->ops->mute_stream)
2668da18396fSMark Brown 		return dai->driver->ops->mute_stream(dai, mute, direction);
2669da18396fSMark Brown 	else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2670da18396fSMark Brown 		 dai->driver->ops->digital_mute)
2671f0fba2adSLiam Girdwood 		return dai->driver->ops->digital_mute(dai, mute);
26728c6529dbSLiam Girdwood 	else
267304570c62SMark Brown 		return -ENOTSUPP;
26748c6529dbSLiam Girdwood }
26758c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
26768c6529dbSLiam Girdwood 
2677e894efefSSrinivas Kandagatla static int snd_soc_bind_card(struct snd_soc_card *card)
2678e894efefSSrinivas Kandagatla {
2679e894efefSSrinivas Kandagatla 	struct snd_soc_pcm_runtime *rtd;
2680e894efefSSrinivas Kandagatla 	int ret;
2681e894efefSSrinivas Kandagatla 
2682e894efefSSrinivas Kandagatla 	ret = snd_soc_instantiate_card(card);
2683e894efefSSrinivas Kandagatla 	if (ret != 0)
2684e894efefSSrinivas Kandagatla 		return ret;
2685e894efefSSrinivas Kandagatla 
2686e894efefSSrinivas Kandagatla 	/* deactivate pins to sleep state */
2687bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
2688e894efefSSrinivas Kandagatla 		struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2689e894efefSSrinivas Kandagatla 		struct snd_soc_dai *codec_dai;
2690e894efefSSrinivas Kandagatla 		int j;
2691e894efefSSrinivas Kandagatla 
2692e894efefSSrinivas Kandagatla 		for_each_rtd_codec_dai(rtd, j, codec_dai) {
2693e894efefSSrinivas Kandagatla 			if (!codec_dai->active)
2694e894efefSSrinivas Kandagatla 				pinctrl_pm_select_sleep_state(codec_dai->dev);
2695e894efefSSrinivas Kandagatla 		}
2696e894efefSSrinivas Kandagatla 
2697e894efefSSrinivas Kandagatla 		if (!cpu_dai->active)
2698e894efefSSrinivas Kandagatla 			pinctrl_pm_select_sleep_state(cpu_dai->dev);
2699e894efefSSrinivas Kandagatla 	}
2700e894efefSSrinivas Kandagatla 
2701e894efefSSrinivas Kandagatla 	return ret;
2702e894efefSSrinivas Kandagatla }
2703e894efefSSrinivas Kandagatla 
2704c5af3a2eSMark Brown /**
2705c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2706c5af3a2eSMark Brown  *
2707ac11a2b3SMark Brown  * @card: Card to register
2708c5af3a2eSMark Brown  *
2709c5af3a2eSMark Brown  */
271070a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2711c5af3a2eSMark Brown {
2712c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2713c5af3a2eSMark Brown 		return -EINVAL;
2714c5af3a2eSMark Brown 
2715ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2716ed77cc12SMark Brown 
2717111c6419SStephen Warren 	snd_soc_initialize_card_lists(card);
2718111c6419SStephen Warren 
2719f8f80361SMengdong Lin 	INIT_LIST_HEAD(&card->dai_link_list);
2720f0fba2adSLiam Girdwood 
27211a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
27229115171aSMark Brown 	card->num_rtd = 0;
2723db2a4165SFrank Mandarino 
2724db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
27258a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2726db2a4165SFrank Mandarino 	card->instantiated = 0;
2727db2a4165SFrank Mandarino 	mutex_init(&card->mutex);
2728a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
2729a9764869SKaiChieh Chuang 	spin_lock_init(&card->dpcm_lock);
2730db2a4165SFrank Mandarino 
2731e894efefSSrinivas Kandagatla 	return snd_soc_bind_card(card);
2732db2a4165SFrank Mandarino }
273370a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2734db2a4165SFrank Mandarino 
2735e894efefSSrinivas Kandagatla static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2736e894efefSSrinivas Kandagatla {
2737f96fb7d1SRanjani Sridharan 	struct snd_soc_pcm_runtime *rtd;
2738f96fb7d1SRanjani Sridharan 	int order;
2739f96fb7d1SRanjani Sridharan 
2740e894efefSSrinivas Kandagatla 	if (card->instantiated) {
2741e894efefSSrinivas Kandagatla 		card->instantiated = false;
2742e894efefSSrinivas Kandagatla 		snd_soc_dapm_shutdown(card);
274353e947a0SKuninori Morimoto 		snd_soc_flush_all_delayed_work(card);
2744f96fb7d1SRanjani Sridharan 
2745f96fb7d1SRanjani Sridharan 		/* remove all components used by DAI links on this card */
2746f96fb7d1SRanjani Sridharan 		for_each_comp_order(order) {
2747f96fb7d1SRanjani Sridharan 			for_each_card_rtds(card, rtd) {
2748f96fb7d1SRanjani Sridharan 				soc_remove_link_components(card, rtd, order);
2749f96fb7d1SRanjani Sridharan 			}
2750f96fb7d1SRanjani Sridharan 		}
2751f96fb7d1SRanjani Sridharan 
2752e894efefSSrinivas Kandagatla 		soc_cleanup_card_resources(card);
2753e894efefSSrinivas Kandagatla 		if (!unregister)
2754e894efefSSrinivas Kandagatla 			list_add(&card->list, &unbind_card_list);
2755e894efefSSrinivas Kandagatla 	} else {
2756e894efefSSrinivas Kandagatla 		if (unregister)
2757e894efefSSrinivas Kandagatla 			list_del(&card->list);
2758e894efefSSrinivas Kandagatla 	}
2759e894efefSSrinivas Kandagatla }
2760e894efefSSrinivas Kandagatla 
2761db2a4165SFrank Mandarino /**
2762db2a4165SFrank Mandarino  * snd_soc_unregister_card - Unregister a card with the ASoC core
2763db2a4165SFrank Mandarino  *
2764db2a4165SFrank Mandarino  * @card: Card to unregister
2765db2a4165SFrank Mandarino  *
2766db2a4165SFrank Mandarino  */
276770a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2768db2a4165SFrank Mandarino {
2769e894efefSSrinivas Kandagatla 	snd_soc_unbind_card(card, true);
2770f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2771db2a4165SFrank Mandarino 
2772db2a4165SFrank Mandarino 	return 0;
2773db2a4165SFrank Mandarino }
277470a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2775db2a4165SFrank Mandarino 
2776db2a4165SFrank Mandarino /*
2777db2a4165SFrank Mandarino  * Simplify DAI link configuration by removing ".-1" from device names
2778db2a4165SFrank Mandarino  * and sanitizing names.
2779db2a4165SFrank Mandarino  */
27800b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2781db2a4165SFrank Mandarino {
2782db2a4165SFrank Mandarino 	char *found, name[NAME_SIZE];
2783db2a4165SFrank Mandarino 	int id1, id2;
2784db2a4165SFrank Mandarino 
2785db2a4165SFrank Mandarino 	if (dev_name(dev) == NULL)
2786db2a4165SFrank Mandarino 		return NULL;
2787db2a4165SFrank Mandarino 
278858818a77SDimitris Papastamos 	strlcpy(name, dev_name(dev), NAME_SIZE);
2789db2a4165SFrank Mandarino 
2790db2a4165SFrank Mandarino 	/* are we a "%s.%d" name (platform and SPI components) */
2791c5af3a2eSMark Brown 	found = strstr(name, dev->driver->name);
2792c5af3a2eSMark Brown 	if (found) {
2793c5af3a2eSMark Brown 		/* get ID */
2794c5af3a2eSMark Brown 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2795c5af3a2eSMark Brown 
2796c5af3a2eSMark Brown 			/* discard ID from name if ID == -1 */
2797c5af3a2eSMark Brown 			if (*id == -1)
2798c5af3a2eSMark Brown 				found[strlen(dev->driver->name)] = '\0';
2799c5af3a2eSMark Brown 		}
2800c5af3a2eSMark Brown 
2801c5af3a2eSMark Brown 	} else {
2802c5af3a2eSMark Brown 		/* I2C component devices are named "bus-addr" */
2803c5af3a2eSMark Brown 		if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2804c5af3a2eSMark Brown 			char tmp[NAME_SIZE];
2805c5af3a2eSMark Brown 
2806c5af3a2eSMark Brown 			/* create unique ID number from I2C addr and bus */
2807c5af3a2eSMark Brown 			*id = ((id1 & 0xffff) << 16) + id2;
2808c5af3a2eSMark Brown 
2809c5af3a2eSMark Brown 			/* sanitize component name for DAI link creation */
28102c7b696aSMarcel Ziswiler 			snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
28112c7b696aSMarcel Ziswiler 				 name);
281258818a77SDimitris Papastamos 			strlcpy(name, tmp, NAME_SIZE);
2813c5af3a2eSMark Brown 		} else
2814c5af3a2eSMark Brown 			*id = 0;
2815c5af3a2eSMark Brown 	}
2816c5af3a2eSMark Brown 
2817c5af3a2eSMark Brown 	return kstrdup(name, GFP_KERNEL);
2818c5af3a2eSMark Brown }
2819c5af3a2eSMark Brown 
2820c5af3a2eSMark Brown /*
2821c5af3a2eSMark Brown  * Simplify DAI link naming for single devices with multiple DAIs by removing
2822c5af3a2eSMark Brown  * any ".-1" and using the DAI name (instead of device name).
2823c5af3a2eSMark Brown  */
2824c5af3a2eSMark Brown static inline char *fmt_multiple_name(struct device *dev,
2825c5af3a2eSMark Brown 		struct snd_soc_dai_driver *dai_drv)
2826c5af3a2eSMark Brown {
2827c5af3a2eSMark Brown 	if (dai_drv->name == NULL) {
282810e8aa9aSMichał Mirosław 		dev_err(dev,
282910e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
283010e8aa9aSMichał Mirosław 			dev_name(dev));
2831c5af3a2eSMark Brown 		return NULL;
2832c5af3a2eSMark Brown 	}
2833c5af3a2eSMark Brown 
2834c5af3a2eSMark Brown 	return kstrdup(dai_drv->name, GFP_KERNEL);
2835c5af3a2eSMark Brown }
2836c5af3a2eSMark Brown 
2837c5af3a2eSMark Brown /**
283832c9ba54SLars-Peter Clausen  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
28399115171aSMark Brown  *
284032c9ba54SLars-Peter Clausen  * @component: The component for which the DAIs should be unregistered
28419115171aSMark Brown  */
284232c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component)
28439115171aSMark Brown {
28445c1d5f09SLars-Peter Clausen 	struct snd_soc_dai *dai, *_dai;
28459115171aSMark Brown 
284615a0c645SKuninori Morimoto 	for_each_component_dais_safe(component, dai, _dai) {
284732c9ba54SLars-Peter Clausen 		dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
284832c9ba54SLars-Peter Clausen 			dai->name);
28499115171aSMark Brown 		list_del(&dai->list);
2850f0fba2adSLiam Girdwood 		kfree(dai->name);
2851f0fba2adSLiam Girdwood 		kfree(dai);
28529115171aSMark Brown 	}
285332c9ba54SLars-Peter Clausen }
28549115171aSMark Brown 
28555e4fb372SMengdong Lin /* Create a DAI and add it to the component's DAI list */
28565e4fb372SMengdong Lin static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
28575e4fb372SMengdong Lin 	struct snd_soc_dai_driver *dai_drv,
28585e4fb372SMengdong Lin 	bool legacy_dai_naming)
28595e4fb372SMengdong Lin {
28605e4fb372SMengdong Lin 	struct device *dev = component->dev;
28615e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
28625e4fb372SMengdong Lin 
28635e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
28645e4fb372SMengdong Lin 
28655e4fb372SMengdong Lin 	dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
28665e4fb372SMengdong Lin 	if (dai == NULL)
28675e4fb372SMengdong Lin 		return NULL;
28685e4fb372SMengdong Lin 
28695e4fb372SMengdong Lin 	/*
28705e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
28715e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
28725e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
28735e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
28745e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
28755e4fb372SMengdong Lin 	 * component-less anymore.
28765e4fb372SMengdong Lin 	 */
28775e4fb372SMengdong Lin 	if (legacy_dai_naming &&
28785e4fb372SMengdong Lin 	    (dai_drv->id == 0 || dai_drv->name == NULL)) {
28795e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
28805e4fb372SMengdong Lin 	} else {
28815e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
28825e4fb372SMengdong Lin 		if (dai_drv->id)
28835e4fb372SMengdong Lin 			dai->id = dai_drv->id;
28845e4fb372SMengdong Lin 		else
28855e4fb372SMengdong Lin 			dai->id = component->num_dai;
28865e4fb372SMengdong Lin 	}
28875e4fb372SMengdong Lin 	if (dai->name == NULL) {
28885e4fb372SMengdong Lin 		kfree(dai);
28895e4fb372SMengdong Lin 		return NULL;
28905e4fb372SMengdong Lin 	}
28915e4fb372SMengdong Lin 
28925e4fb372SMengdong Lin 	dai->component = component;
28935e4fb372SMengdong Lin 	dai->dev = dev;
28945e4fb372SMengdong Lin 	dai->driver = dai_drv;
28955e4fb372SMengdong Lin 	if (!dai->driver->ops)
28965e4fb372SMengdong Lin 		dai->driver->ops = &null_dai_ops;
28975e4fb372SMengdong Lin 
289815a0c645SKuninori Morimoto 	/* see for_each_component_dais */
289958bf4179SKuninori Morimoto 	list_add_tail(&dai->list, &component->dai_list);
29005e4fb372SMengdong Lin 	component->num_dai++;
29015e4fb372SMengdong Lin 
29025e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
29035e4fb372SMengdong Lin 	return dai;
29045e4fb372SMengdong Lin }
29055e4fb372SMengdong Lin 
29069115171aSMark Brown /**
290732c9ba54SLars-Peter Clausen  * snd_soc_register_dais - Register a DAI with the ASoC core
29089115171aSMark Brown  *
29096106d129SLars-Peter Clausen  * @component: The component the DAIs are registered for
29106106d129SLars-Peter Clausen  * @dai_drv: DAI driver to use for the DAIs
2911ac11a2b3SMark Brown  * @count: Number of DAIs
29129115171aSMark Brown  */
291332c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component,
29142c7b696aSMarcel Ziswiler 				 struct snd_soc_dai_driver *dai_drv,
29152c7b696aSMarcel Ziswiler 				 size_t count)
29169115171aSMark Brown {
29176106d129SLars-Peter Clausen 	struct device *dev = component->dev;
2918f0fba2adSLiam Girdwood 	struct snd_soc_dai *dai;
291932c9ba54SLars-Peter Clausen 	unsigned int i;
292032c9ba54SLars-Peter Clausen 	int ret;
2921f0fba2adSLiam Girdwood 
29225b5e0928SAlexey Dobriyan 	dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
29239115171aSMark Brown 
29249115171aSMark Brown 	for (i = 0; i < count; i++) {
2925f0fba2adSLiam Girdwood 
29262c7b696aSMarcel Ziswiler 		dai = soc_add_dai(component, dai_drv + i, count == 1 &&
29272c7b696aSMarcel Ziswiler 				  !component->driver->non_legacy_dai_naming);
2928c46e0079SAxel Lin 		if (dai == NULL) {
2929c46e0079SAxel Lin 			ret = -ENOMEM;
2930c46e0079SAxel Lin 			goto err;
2931c46e0079SAxel Lin 		}
2932f0fba2adSLiam Girdwood 	}
2933f0fba2adSLiam Girdwood 
29349115171aSMark Brown 	return 0;
29359115171aSMark Brown 
29369115171aSMark Brown err:
293732c9ba54SLars-Peter Clausen 	snd_soc_unregister_dais(component);
29389115171aSMark Brown 
29399115171aSMark Brown 	return ret;
29409115171aSMark Brown }
29419115171aSMark Brown 
294268003e6cSMengdong Lin /**
294368003e6cSMengdong Lin  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
294468003e6cSMengdong Lin  *
294568003e6cSMengdong Lin  * @component: The component the DAIs are registered for
294668003e6cSMengdong Lin  * @dai_drv: DAI driver to use for the DAI
294768003e6cSMengdong Lin  *
294868003e6cSMengdong Lin  * Topology can use this API to register DAIs when probing a component.
294968003e6cSMengdong Lin  * These DAIs's widgets will be freed in the card cleanup and the DAIs
295068003e6cSMengdong Lin  * will be freed in the component cleanup.
295168003e6cSMengdong Lin  */
295268003e6cSMengdong Lin int snd_soc_register_dai(struct snd_soc_component *component,
295368003e6cSMengdong Lin 	struct snd_soc_dai_driver *dai_drv)
295468003e6cSMengdong Lin {
295568003e6cSMengdong Lin 	struct snd_soc_dapm_context *dapm =
295668003e6cSMengdong Lin 		snd_soc_component_get_dapm(component);
295768003e6cSMengdong Lin 	struct snd_soc_dai *dai;
295868003e6cSMengdong Lin 	int ret;
295968003e6cSMengdong Lin 
296068003e6cSMengdong Lin 	if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
296168003e6cSMengdong Lin 		dev_err(component->dev, "Invalid dai type %d\n",
296268003e6cSMengdong Lin 			dai_drv->dobj.type);
296368003e6cSMengdong Lin 		return -EINVAL;
296468003e6cSMengdong Lin 	}
296568003e6cSMengdong Lin 
296668003e6cSMengdong Lin 	lockdep_assert_held(&client_mutex);
296768003e6cSMengdong Lin 	dai = soc_add_dai(component, dai_drv, false);
296868003e6cSMengdong Lin 	if (!dai)
296968003e6cSMengdong Lin 		return -ENOMEM;
297068003e6cSMengdong Lin 
29712c7b696aSMarcel Ziswiler 	/*
29722c7b696aSMarcel Ziswiler 	 * Create the DAI widgets here. After adding DAIs, topology may
297368003e6cSMengdong Lin 	 * also add routes that need these widgets as source or sink.
297468003e6cSMengdong Lin 	 */
297568003e6cSMengdong Lin 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
297668003e6cSMengdong Lin 	if (ret != 0) {
297768003e6cSMengdong Lin 		dev_err(component->dev,
297868003e6cSMengdong Lin 			"Failed to create DAI widgets %d\n", ret);
297968003e6cSMengdong Lin 	}
298068003e6cSMengdong Lin 
298168003e6cSMengdong Lin 	return ret;
298268003e6cSMengdong Lin }
298368003e6cSMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_register_dai);
298468003e6cSMengdong Lin 
298514e8bdebSLars-Peter Clausen static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
298614e8bdebSLars-Peter Clausen 	enum snd_soc_dapm_type type, int subseq)
2987d191bd8dSKuninori Morimoto {
298814e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
2989d191bd8dSKuninori Morimoto 
299014e8bdebSLars-Peter Clausen 	component->driver->seq_notifier(component, type, subseq);
299114e8bdebSLars-Peter Clausen }
2992d191bd8dSKuninori Morimoto 
299314e8bdebSLars-Peter Clausen static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
299414e8bdebSLars-Peter Clausen 	int event)
299514e8bdebSLars-Peter Clausen {
299614e8bdebSLars-Peter Clausen 	struct snd_soc_component *component = dapm->component;
299714e8bdebSLars-Peter Clausen 
299814e8bdebSLars-Peter Clausen 	return component->driver->stream_event(component, event);
299914e8bdebSLars-Peter Clausen }
300014e8bdebSLars-Peter Clausen 
30017ba236ceSKuninori Morimoto static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
30027ba236ceSKuninori Morimoto 					enum snd_soc_bias_level level)
30037ba236ceSKuninori Morimoto {
30047ba236ceSKuninori Morimoto 	struct snd_soc_component *component = dapm->component;
30057ba236ceSKuninori Morimoto 
30067ba236ceSKuninori Morimoto 	return component->driver->set_bias_level(component, level);
30077ba236ceSKuninori Morimoto }
30087ba236ceSKuninori Morimoto 
3009bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component,
3010bb13109dSLars-Peter Clausen 	const struct snd_soc_component_driver *driver, struct device *dev)
3011d191bd8dSKuninori Morimoto {
3012ce0fc93aSLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
3013ce0fc93aSLars-Peter Clausen 
3014bb13109dSLars-Peter Clausen 	component->name = fmt_single_name(dev, &component->id);
3015bb13109dSLars-Peter Clausen 	if (!component->name) {
3016bb13109dSLars-Peter Clausen 		dev_err(dev, "ASoC: Failed to allocate name\n");
3017d191bd8dSKuninori Morimoto 		return -ENOMEM;
3018d191bd8dSKuninori Morimoto 	}
3019d191bd8dSKuninori Morimoto 
3020bb13109dSLars-Peter Clausen 	component->dev = dev;
3021bb13109dSLars-Peter Clausen 	component->driver = driver;
3022e2c330b9SLars-Peter Clausen 
302388c27465SKuninori Morimoto 	dapm = snd_soc_component_get_dapm(component);
3024ce0fc93aSLars-Peter Clausen 	dapm->dev = dev;
3025ce0fc93aSLars-Peter Clausen 	dapm->component = component;
3026ce0fc93aSLars-Peter Clausen 	dapm->bias_level = SND_SOC_BIAS_OFF;
30277ba236ceSKuninori Morimoto 	dapm->idle_bias_off = !driver->idle_bias_on;
30287ba236ceSKuninori Morimoto 	dapm->suspend_bias_off = driver->suspend_bias_off;
302914e8bdebSLars-Peter Clausen 	if (driver->seq_notifier)
303014e8bdebSLars-Peter Clausen 		dapm->seq_notifier = snd_soc_component_seq_notifier;
303114e8bdebSLars-Peter Clausen 	if (driver->stream_event)
303214e8bdebSLars-Peter Clausen 		dapm->stream_event = snd_soc_component_stream_event;
30337ba236ceSKuninori Morimoto 	if (driver->set_bias_level)
30347ba236ceSKuninori Morimoto 		dapm->set_bias_level = snd_soc_component_set_bias_level;
3035ce0fc93aSLars-Peter Clausen 
3036bb13109dSLars-Peter Clausen 	INIT_LIST_HEAD(&component->dai_list);
3037bb13109dSLars-Peter Clausen 	mutex_init(&component->io_mutex);
3038bb13109dSLars-Peter Clausen 
3039bb13109dSLars-Peter Clausen 	return 0;
3040d191bd8dSKuninori Morimoto }
3041d191bd8dSKuninori Morimoto 
304220feb881SLars-Peter Clausen static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
3043886f5692SLars-Peter Clausen {
3044886f5692SLars-Peter Clausen 	int val_bytes = regmap_get_val_bytes(component->regmap);
304520feb881SLars-Peter Clausen 
3046886f5692SLars-Peter Clausen 	/* Errors are legitimate for non-integer byte multiples */
3047886f5692SLars-Peter Clausen 	if (val_bytes > 0)
3048886f5692SLars-Peter Clausen 		component->val_bytes = val_bytes;
3049886f5692SLars-Peter Clausen }
305020feb881SLars-Peter Clausen 
3051e874bf5fSLars-Peter Clausen #ifdef CONFIG_REGMAP
3052e874bf5fSLars-Peter Clausen 
305320feb881SLars-Peter Clausen /**
30542c7b696aSMarcel Ziswiler  * snd_soc_component_init_regmap() - Initialize regmap instance for the
30552c7b696aSMarcel Ziswiler  *                                   component
305620feb881SLars-Peter Clausen  * @component: The component for which to initialize the regmap instance
305720feb881SLars-Peter Clausen  * @regmap: The regmap instance that should be used by the component
305820feb881SLars-Peter Clausen  *
305920feb881SLars-Peter Clausen  * This function allows deferred assignment of the regmap instance that is
306020feb881SLars-Peter Clausen  * associated with the component. Only use this if the regmap instance is not
306120feb881SLars-Peter Clausen  * yet ready when the component is registered. The function must also be called
306220feb881SLars-Peter Clausen  * before the first IO attempt of the component.
306320feb881SLars-Peter Clausen  */
306420feb881SLars-Peter Clausen void snd_soc_component_init_regmap(struct snd_soc_component *component,
306520feb881SLars-Peter Clausen 	struct regmap *regmap)
306620feb881SLars-Peter Clausen {
306720feb881SLars-Peter Clausen 	component->regmap = regmap;
306820feb881SLars-Peter Clausen 	snd_soc_component_setup_regmap(component);
3069886f5692SLars-Peter Clausen }
307020feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
307120feb881SLars-Peter Clausen 
307220feb881SLars-Peter Clausen /**
30732c7b696aSMarcel Ziswiler  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
30742c7b696aSMarcel Ziswiler  *                                   component
307520feb881SLars-Peter Clausen  * @component: The component for which to de-initialize the regmap instance
307620feb881SLars-Peter Clausen  *
307720feb881SLars-Peter Clausen  * Calls regmap_exit() on the regmap instance associated to the component and
307820feb881SLars-Peter Clausen  * removes the regmap instance from the component.
307920feb881SLars-Peter Clausen  *
308020feb881SLars-Peter Clausen  * This function should only be used if snd_soc_component_init_regmap() was used
308120feb881SLars-Peter Clausen  * to initialize the regmap instance.
308220feb881SLars-Peter Clausen  */
308320feb881SLars-Peter Clausen void snd_soc_component_exit_regmap(struct snd_soc_component *component)
308420feb881SLars-Peter Clausen {
308520feb881SLars-Peter Clausen 	regmap_exit(component->regmap);
308620feb881SLars-Peter Clausen 	component->regmap = NULL;
308720feb881SLars-Peter Clausen }
308820feb881SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3089886f5692SLars-Peter Clausen 
3090e874bf5fSLars-Peter Clausen #endif
3091d191bd8dSKuninori Morimoto 
3092359c71eeSKuninori Morimoto static void snd_soc_component_add(struct snd_soc_component *component)
3093bb13109dSLars-Peter Clausen {
3094359c71eeSKuninori Morimoto 	mutex_lock(&client_mutex);
3095359c71eeSKuninori Morimoto 
3096999f7f5aSKuninori Morimoto 	if (!component->driver->write && !component->driver->read) {
309720feb881SLars-Peter Clausen 		if (!component->regmap)
30982c7b696aSMarcel Ziswiler 			component->regmap = dev_get_regmap(component->dev,
30992c7b696aSMarcel Ziswiler 							   NULL);
310020feb881SLars-Peter Clausen 		if (component->regmap)
310120feb881SLars-Peter Clausen 			snd_soc_component_setup_regmap(component);
310220feb881SLars-Peter Clausen 	}
3103886f5692SLars-Peter Clausen 
3104368dee94SKuninori Morimoto 	/* see for_each_component */
3105bb13109dSLars-Peter Clausen 	list_add(&component->list, &component_list);
31068a978234SLiam Girdwood 	INIT_LIST_HEAD(&component->dobj_list);
3107d191bd8dSKuninori Morimoto 
3108d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
3109bb13109dSLars-Peter Clausen }
3110d191bd8dSKuninori Morimoto 
3111bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component)
3112bb13109dSLars-Peter Clausen {
3113bb13109dSLars-Peter Clausen 	snd_soc_unregister_dais(component);
3114bb13109dSLars-Peter Clausen 	kfree(component->name);
3115bb13109dSLars-Peter Clausen }
3116d191bd8dSKuninori Morimoto 
3117bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3118bb13109dSLars-Peter Clausen {
3119c12c1aadSKuninori Morimoto 	struct snd_soc_card *card = component->card;
3120c12c1aadSKuninori Morimoto 
3121c12c1aadSKuninori Morimoto 	if (card)
3122e894efefSSrinivas Kandagatla 		snd_soc_unbind_card(card, false);
3123c12c1aadSKuninori Morimoto 
3124bb13109dSLars-Peter Clausen 	list_del(&component->list);
3125bb13109dSLars-Peter Clausen }
3126d191bd8dSKuninori Morimoto 
3127273d778eSKuninori Morimoto #define ENDIANNESS_MAP(name) \
3128273d778eSKuninori Morimoto 	(SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3129273d778eSKuninori Morimoto static u64 endianness_format_map[] = {
3130273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S16_),
3131273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U16_),
3132273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_),
3133273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_),
3134273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S32_),
3135273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U32_),
3136273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_3),
3137273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_3),
3138273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S20_3),
3139273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U20_3),
3140273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S18_3),
3141273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U18_3),
3142273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT_),
3143273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT64_),
3144273d778eSKuninori Morimoto 	ENDIANNESS_MAP(IEC958_SUBFRAME_),
3145273d778eSKuninori Morimoto };
3146273d778eSKuninori Morimoto 
3147273d778eSKuninori Morimoto /*
3148273d778eSKuninori Morimoto  * Fix up the DAI formats for endianness: codecs don't actually see
3149273d778eSKuninori Morimoto  * the endianness of the data but we're using the CPU format
3150273d778eSKuninori Morimoto  * definitions which do need to include endianness so we ensure that
3151273d778eSKuninori Morimoto  * codec DAIs always have both big and little endian variants set.
3152273d778eSKuninori Morimoto  */
3153273d778eSKuninori Morimoto static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3154273d778eSKuninori Morimoto {
3155273d778eSKuninori Morimoto 	int i;
3156273d778eSKuninori Morimoto 
3157273d778eSKuninori Morimoto 	for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3158273d778eSKuninori Morimoto 		if (stream->formats & endianness_format_map[i])
3159273d778eSKuninori Morimoto 			stream->formats |= endianness_format_map[i];
3160273d778eSKuninori Morimoto }
3161273d778eSKuninori Morimoto 
3162e894efefSSrinivas Kandagatla static void snd_soc_try_rebind_card(void)
3163e894efefSSrinivas Kandagatla {
3164e894efefSSrinivas Kandagatla 	struct snd_soc_card *card, *c;
3165e894efefSSrinivas Kandagatla 
3166e894efefSSrinivas Kandagatla 	if (!list_empty(&unbind_card_list)) {
3167e894efefSSrinivas Kandagatla 		list_for_each_entry_safe(card, c, &unbind_card_list, list) {
3168e894efefSSrinivas Kandagatla 			if (!snd_soc_bind_card(card))
3169e894efefSSrinivas Kandagatla 				list_del(&card->list);
3170e894efefSSrinivas Kandagatla 		}
3171e894efefSSrinivas Kandagatla 	}
3172e894efefSSrinivas Kandagatla }
3173e894efefSSrinivas Kandagatla 
3174e0dac41bSKuninori Morimoto int snd_soc_add_component(struct device *dev,
3175e0dac41bSKuninori Morimoto 			struct snd_soc_component *component,
3176cf9e829eSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
3177d191bd8dSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
3178d191bd8dSKuninori Morimoto 			int num_dai)
3179d191bd8dSKuninori Morimoto {
3180bb13109dSLars-Peter Clausen 	int ret;
3181273d778eSKuninori Morimoto 	int i;
3182d191bd8dSKuninori Morimoto 
3183cf9e829eSKuninori Morimoto 	ret = snd_soc_component_initialize(component, component_driver, dev);
3184bb13109dSLars-Peter Clausen 	if (ret)
3185bb13109dSLars-Peter Clausen 		goto err_free;
3186bb13109dSLars-Peter Clausen 
3187273d778eSKuninori Morimoto 	if (component_driver->endianness) {
3188273d778eSKuninori Morimoto 		for (i = 0; i < num_dai; i++) {
3189273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].playback);
3190273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].capture);
3191273d778eSKuninori Morimoto 		}
3192273d778eSKuninori Morimoto 	}
3193273d778eSKuninori Morimoto 
31940e7b25c6SKuninori Morimoto 	ret = snd_soc_register_dais(component, dai_drv, num_dai);
3195bb13109dSLars-Peter Clausen 	if (ret < 0) {
3196f42cf8d6SMasanari Iida 		dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
3197bb13109dSLars-Peter Clausen 		goto err_cleanup;
3198bb13109dSLars-Peter Clausen 	}
3199bb13109dSLars-Peter Clausen 
3200cf9e829eSKuninori Morimoto 	snd_soc_component_add(component);
3201e894efefSSrinivas Kandagatla 	snd_soc_try_rebind_card();
3202bb13109dSLars-Peter Clausen 
3203bb13109dSLars-Peter Clausen 	return 0;
3204bb13109dSLars-Peter Clausen 
3205bb13109dSLars-Peter Clausen err_cleanup:
3206cf9e829eSKuninori Morimoto 	snd_soc_component_cleanup(component);
3207bb13109dSLars-Peter Clausen err_free:
3208bb13109dSLars-Peter Clausen 	return ret;
3209d191bd8dSKuninori Morimoto }
3210e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component);
3211e0dac41bSKuninori Morimoto 
3212e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev,
3213e0dac41bSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
3214e0dac41bSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
3215e0dac41bSKuninori Morimoto 			int num_dai)
3216e0dac41bSKuninori Morimoto {
3217e0dac41bSKuninori Morimoto 	struct snd_soc_component *component;
3218e0dac41bSKuninori Morimoto 
32197ecbd6a9SKuninori Morimoto 	component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
322008e61d03SKuninori Morimoto 	if (!component)
3221e0dac41bSKuninori Morimoto 		return -ENOMEM;
3222e0dac41bSKuninori Morimoto 
3223e0dac41bSKuninori Morimoto 	return snd_soc_add_component(dev, component, component_driver,
3224e0dac41bSKuninori Morimoto 				     dai_drv, num_dai);
3225e0dac41bSKuninori Morimoto }
3226d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
3227d191bd8dSKuninori Morimoto 
3228d191bd8dSKuninori Morimoto /**
32292eccea8cSKuninori Morimoto  * snd_soc_unregister_component - Unregister all related component
32302eccea8cSKuninori Morimoto  * from the ASoC core
3231d191bd8dSKuninori Morimoto  *
3232628536eaSJonathan Corbet  * @dev: The device to unregister
3233d191bd8dSKuninori Morimoto  */
32342eccea8cSKuninori Morimoto static int __snd_soc_unregister_component(struct device *dev)
3235d191bd8dSKuninori Morimoto {
3236cf9e829eSKuninori Morimoto 	struct snd_soc_component *component;
323721a03528SKuninori Morimoto 	int found = 0;
3238d191bd8dSKuninori Morimoto 
323934e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
3240368dee94SKuninori Morimoto 	for_each_component(component) {
3241999f7f5aSKuninori Morimoto 		if (dev != component->dev)
324221a03528SKuninori Morimoto 			continue;
3243d191bd8dSKuninori Morimoto 
32442c7b696aSMarcel Ziswiler 		snd_soc_tplg_component_remove(component,
32452c7b696aSMarcel Ziswiler 					      SND_SOC_TPLG_INDEX_ALL);
3246cf9e829eSKuninori Morimoto 		snd_soc_component_del_unlocked(component);
324721a03528SKuninori Morimoto 		found = 1;
324821a03528SKuninori Morimoto 		break;
3249d191bd8dSKuninori Morimoto 	}
3250d191bd8dSKuninori Morimoto 	mutex_unlock(&client_mutex);
3251d191bd8dSKuninori Morimoto 
32522c7b696aSMarcel Ziswiler 	if (found)
3253cf9e829eSKuninori Morimoto 		snd_soc_component_cleanup(component);
32542eccea8cSKuninori Morimoto 
32552eccea8cSKuninori Morimoto 	return found;
32562eccea8cSKuninori Morimoto }
32572eccea8cSKuninori Morimoto 
32582eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
32592eccea8cSKuninori Morimoto {
32602c7b696aSMarcel Ziswiler 	while (__snd_soc_unregister_component(dev))
32612c7b696aSMarcel Ziswiler 		;
3262d191bd8dSKuninori Morimoto }
3263d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3264d191bd8dSKuninori Morimoto 
32657dd5d0d9SKuninori Morimoto struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
32667dd5d0d9SKuninori Morimoto 						   const char *driver_name)
32677dd5d0d9SKuninori Morimoto {
32687dd5d0d9SKuninori Morimoto 	struct snd_soc_component *component;
32697dd5d0d9SKuninori Morimoto 	struct snd_soc_component *ret;
32707dd5d0d9SKuninori Morimoto 
32717dd5d0d9SKuninori Morimoto 	ret = NULL;
32727dd5d0d9SKuninori Morimoto 	mutex_lock(&client_mutex);
3273368dee94SKuninori Morimoto 	for_each_component(component) {
32747dd5d0d9SKuninori Morimoto 		if (dev != component->dev)
32757dd5d0d9SKuninori Morimoto 			continue;
32767dd5d0d9SKuninori Morimoto 
32777dd5d0d9SKuninori Morimoto 		if (driver_name &&
32787dd5d0d9SKuninori Morimoto 		    (driver_name != component->driver->name) &&
32797dd5d0d9SKuninori Morimoto 		    (strcmp(component->driver->name, driver_name) != 0))
32807dd5d0d9SKuninori Morimoto 			continue;
32817dd5d0d9SKuninori Morimoto 
32827dd5d0d9SKuninori Morimoto 		ret = component;
32837dd5d0d9SKuninori Morimoto 		break;
32847dd5d0d9SKuninori Morimoto 	}
32857dd5d0d9SKuninori Morimoto 	mutex_unlock(&client_mutex);
32867dd5d0d9SKuninori Morimoto 
32877dd5d0d9SKuninori Morimoto 	return ret;
32887dd5d0d9SKuninori Morimoto }
32897dd5d0d9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
32907dd5d0d9SKuninori Morimoto 
3291bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
3292b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3293bec4fa05SStephen Warren 			       const char *propname)
3294bec4fa05SStephen Warren {
3295b07609ceSKuninori Morimoto 	struct device_node *np;
3296bec4fa05SStephen Warren 	int ret;
3297bec4fa05SStephen Warren 
32987e07e7c0STushar Behera 	if (!card->dev) {
32997e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
33007e07e7c0STushar Behera 		return -EINVAL;
33017e07e7c0STushar Behera 	}
33027e07e7c0STushar Behera 
33037e07e7c0STushar Behera 	np = card->dev->of_node;
33047e07e7c0STushar Behera 
3305bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
3306bec4fa05SStephen Warren 	/*
3307bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
3308bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
3309bec4fa05SStephen Warren 	 * snd_soc_register_card.
3310bec4fa05SStephen Warren 	 */
3311bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
3312bec4fa05SStephen Warren 		dev_err(card->dev,
3313f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
3314bec4fa05SStephen Warren 			propname, ret);
3315bec4fa05SStephen Warren 		return ret;
3316bec4fa05SStephen Warren 	}
3317bec4fa05SStephen Warren 
3318bec4fa05SStephen Warren 	return 0;
3319bec4fa05SStephen Warren }
3320b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3321bec4fa05SStephen Warren 
33229a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
33239a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
33249a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
33259a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
33269a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
33279a6d4860SXiubo Li };
33289a6d4860SXiubo Li 
332921efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
33309a6d4860SXiubo Li 					  const char *propname)
33319a6d4860SXiubo Li {
333221efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
33339a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
33349a6d4860SXiubo Li 	const char *template, *wname;
33359a6d4860SXiubo Li 	int i, j, num_widgets, ret;
33369a6d4860SXiubo Li 
33379a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
33389a6d4860SXiubo Li 	if (num_widgets < 0) {
33399a6d4860SXiubo Li 		dev_err(card->dev,
33409a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
33419a6d4860SXiubo Li 		return -EINVAL;
33429a6d4860SXiubo Li 	}
33439a6d4860SXiubo Li 	if (num_widgets & 1) {
33449a6d4860SXiubo Li 		dev_err(card->dev,
33459a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
33469a6d4860SXiubo Li 		return -EINVAL;
33479a6d4860SXiubo Li 	}
33489a6d4860SXiubo Li 
33499a6d4860SXiubo Li 	num_widgets /= 2;
33509a6d4860SXiubo Li 	if (!num_widgets) {
33519a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
33529a6d4860SXiubo Li 			propname);
33539a6d4860SXiubo Li 		return -EINVAL;
33549a6d4860SXiubo Li 	}
33559a6d4860SXiubo Li 
33569a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
33579a6d4860SXiubo Li 			       GFP_KERNEL);
33589a6d4860SXiubo Li 	if (!widgets) {
33599a6d4860SXiubo Li 		dev_err(card->dev,
33609a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
33619a6d4860SXiubo Li 		return -ENOMEM;
33629a6d4860SXiubo Li 	}
33639a6d4860SXiubo Li 
33649a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
33659a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
33669a6d4860SXiubo Li 			2 * i, &template);
33679a6d4860SXiubo Li 		if (ret) {
33689a6d4860SXiubo Li 			dev_err(card->dev,
33699a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
33709a6d4860SXiubo Li 				propname, 2 * i, ret);
33719a6d4860SXiubo Li 			return -EINVAL;
33729a6d4860SXiubo Li 		}
33739a6d4860SXiubo Li 
33749a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
33759a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
33769a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
33779a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
33789a6d4860SXiubo Li 				break;
33799a6d4860SXiubo Li 			}
33809a6d4860SXiubo Li 		}
33819a6d4860SXiubo Li 
33829a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
33839a6d4860SXiubo Li 			dev_err(card->dev,
33849a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
33859a6d4860SXiubo Li 				template);
33869a6d4860SXiubo Li 			return -EINVAL;
33879a6d4860SXiubo Li 		}
33889a6d4860SXiubo Li 
33899a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
33909a6d4860SXiubo Li 						    (2 * i) + 1,
33919a6d4860SXiubo Li 						    &wname);
33929a6d4860SXiubo Li 		if (ret) {
33939a6d4860SXiubo Li 			dev_err(card->dev,
33949a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
33959a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
33969a6d4860SXiubo Li 			return -EINVAL;
33979a6d4860SXiubo Li 		}
33989a6d4860SXiubo Li 
33999a6d4860SXiubo Li 		widgets[i].name = wname;
34009a6d4860SXiubo Li 	}
34019a6d4860SXiubo Li 
3402f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
3403f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
34049a6d4860SXiubo Li 
34059a6d4860SXiubo Li 	return 0;
34069a6d4860SXiubo Li }
340721efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
34089a6d4860SXiubo Li 
3409cbdfab3bSJerome Brunet int snd_soc_of_get_slot_mask(struct device_node *np,
34106131084aSJyri Sarha 			     const char *prop_name,
34116131084aSJyri Sarha 			     unsigned int *mask)
34126131084aSJyri Sarha {
34136131084aSJyri Sarha 	u32 val;
34146c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
34156131084aSJyri Sarha 	int i;
34166131084aSJyri Sarha 
34176131084aSJyri Sarha 	if (!of_slot_mask)
34186131084aSJyri Sarha 		return 0;
34196131084aSJyri Sarha 	val /= sizeof(u32);
34206131084aSJyri Sarha 	for (i = 0; i < val; i++)
34216131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
34226131084aSJyri Sarha 			*mask |= (1 << i);
34236131084aSJyri Sarha 
34246131084aSJyri Sarha 	return val;
34256131084aSJyri Sarha }
3426cbdfab3bSJerome Brunet EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
34276131084aSJyri Sarha 
342889c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
34296131084aSJyri Sarha 			      unsigned int *tx_mask,
34306131084aSJyri Sarha 			      unsigned int *rx_mask,
343189c67857SXiubo Li 			      unsigned int *slots,
343289c67857SXiubo Li 			      unsigned int *slot_width)
343389c67857SXiubo Li {
343489c67857SXiubo Li 	u32 val;
343589c67857SXiubo Li 	int ret;
343689c67857SXiubo Li 
34376131084aSJyri Sarha 	if (tx_mask)
34386131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
34396131084aSJyri Sarha 	if (rx_mask)
34406131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
34416131084aSJyri Sarha 
344289c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
344389c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
344489c67857SXiubo Li 		if (ret)
344589c67857SXiubo Li 			return ret;
344689c67857SXiubo Li 
344789c67857SXiubo Li 		if (slots)
344889c67857SXiubo Li 			*slots = val;
344989c67857SXiubo Li 	}
345089c67857SXiubo Li 
345189c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
345289c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
345389c67857SXiubo Li 		if (ret)
345489c67857SXiubo Li 			return ret;
345589c67857SXiubo Li 
345689c67857SXiubo Li 		if (slot_width)
345789c67857SXiubo Li 			*slot_width = val;
345889c67857SXiubo Li 	}
345989c67857SXiubo Li 
346089c67857SXiubo Li 	return 0;
346189c67857SXiubo Li }
346289c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
346389c67857SXiubo Li 
34643b710356SKuninori Morimoto void snd_soc_of_parse_node_prefix(struct device_node *np,
34655e3cdaa2SKuninori Morimoto 				  struct snd_soc_codec_conf *codec_conf,
34665e3cdaa2SKuninori Morimoto 				  struct device_node *of_node,
34675e3cdaa2SKuninori Morimoto 				  const char *propname)
34685e3cdaa2SKuninori Morimoto {
34695e3cdaa2SKuninori Morimoto 	const char *str;
34705e3cdaa2SKuninori Morimoto 	int ret;
34715e3cdaa2SKuninori Morimoto 
34725e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
34735e3cdaa2SKuninori Morimoto 	if (ret < 0) {
34745e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
34755e3cdaa2SKuninori Morimoto 		return;
34765e3cdaa2SKuninori Morimoto 	}
34775e3cdaa2SKuninori Morimoto 
34785e3cdaa2SKuninori Morimoto 	codec_conf->of_node	= of_node;
34795e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
34805e3cdaa2SKuninori Morimoto }
34813b710356SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
34825e3cdaa2SKuninori Morimoto 
34832bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3484a4a54dd5SStephen Warren 				   const char *propname)
3485a4a54dd5SStephen Warren {
34862bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
3487e3b1e6a1SMark Brown 	int num_routes;
3488a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
3489a4a54dd5SStephen Warren 	int i, ret;
3490a4a54dd5SStephen Warren 
3491a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
3492c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
349310e8aa9aSMichał Mirosław 		dev_err(card->dev,
349410e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
349510e8aa9aSMichał Mirosław 			propname);
3496a4a54dd5SStephen Warren 		return -EINVAL;
3497a4a54dd5SStephen Warren 	}
3498a4a54dd5SStephen Warren 	num_routes /= 2;
3499a4a54dd5SStephen Warren 	if (!num_routes) {
3500f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3501a4a54dd5SStephen Warren 			propname);
3502a4a54dd5SStephen Warren 		return -EINVAL;
3503a4a54dd5SStephen Warren 	}
3504a4a54dd5SStephen Warren 
3505a86854d0SKees Cook 	routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
3506a4a54dd5SStephen Warren 			      GFP_KERNEL);
3507a4a54dd5SStephen Warren 	if (!routes) {
3508a4a54dd5SStephen Warren 		dev_err(card->dev,
3509f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
3510a4a54dd5SStephen Warren 		return -EINVAL;
3511a4a54dd5SStephen Warren 	}
3512a4a54dd5SStephen Warren 
3513a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
3514a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3515e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
3516a4a54dd5SStephen Warren 		if (ret) {
3517c871bd0bSMark Brown 			dev_err(card->dev,
3518c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3519c871bd0bSMark Brown 				propname, 2 * i, ret);
3520a4a54dd5SStephen Warren 			return -EINVAL;
3521a4a54dd5SStephen Warren 		}
3522a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
3523e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
3524a4a54dd5SStephen Warren 		if (ret) {
3525a4a54dd5SStephen Warren 			dev_err(card->dev,
3526c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
3527c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
3528a4a54dd5SStephen Warren 			return -EINVAL;
3529a4a54dd5SStephen Warren 		}
3530a4a54dd5SStephen Warren 	}
3531a4a54dd5SStephen Warren 
3532f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
3533f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
3534a4a54dd5SStephen Warren 
3535a4a54dd5SStephen Warren 	return 0;
3536a4a54dd5SStephen Warren }
35372bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3538a4a54dd5SStephen Warren 
3539a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3540389cb834SJyri Sarha 				     const char *prefix,
3541389cb834SJyri Sarha 				     struct device_node **bitclkmaster,
3542389cb834SJyri Sarha 				     struct device_node **framemaster)
3543a7930ed4SKuninori Morimoto {
3544a7930ed4SKuninori Morimoto 	int ret, i;
3545a7930ed4SKuninori Morimoto 	char prop[128];
3546a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3547a7930ed4SKuninori Morimoto 	int bit, frame;
3548a7930ed4SKuninori Morimoto 	const char *str;
3549a7930ed4SKuninori Morimoto 	struct {
3550a7930ed4SKuninori Morimoto 		char *name;
3551a7930ed4SKuninori Morimoto 		unsigned int val;
3552a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3553a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3554a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3555a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3556a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3557a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3558a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3559a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3560a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3561a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3562a7930ed4SKuninori Morimoto 	};
3563a7930ed4SKuninori Morimoto 
3564a7930ed4SKuninori Morimoto 	if (!prefix)
3565a7930ed4SKuninori Morimoto 		prefix = "";
3566a7930ed4SKuninori Morimoto 
3567a7930ed4SKuninori Morimoto 	/*
35685711c979SKuninori Morimoto 	 * check "dai-format = xxx"
35695711c979SKuninori Morimoto 	 * or    "[prefix]format = xxx"
3570a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3571a7930ed4SKuninori Morimoto 	 */
35725711c979SKuninori Morimoto 	ret = of_property_read_string(np, "dai-format", &str);
35735711c979SKuninori Morimoto 	if (ret < 0) {
3574a7930ed4SKuninori Morimoto 		snprintf(prop, sizeof(prop), "%sformat", prefix);
3575a7930ed4SKuninori Morimoto 		ret = of_property_read_string(np, prop, &str);
35765711c979SKuninori Morimoto 	}
3577a7930ed4SKuninori Morimoto 	if (ret == 0) {
3578a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3579a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3580a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3581a7930ed4SKuninori Morimoto 				break;
3582a7930ed4SKuninori Morimoto 			}
3583a7930ed4SKuninori Morimoto 		}
3584a7930ed4SKuninori Morimoto 	}
3585a7930ed4SKuninori Morimoto 
3586a7930ed4SKuninori Morimoto 	/*
35878c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3588a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3589a7930ed4SKuninori Morimoto 	 */
35908c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
359151930295SJulia Lawall 	if (of_property_read_bool(np, prop))
35928c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
35938c2d6a9fSKuninori Morimoto 	else
35948c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3595a7930ed4SKuninori Morimoto 
3596a7930ed4SKuninori Morimoto 	/*
3597a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3598a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3599a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3600a7930ed4SKuninori Morimoto 	 */
3601a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3602a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3603a7930ed4SKuninori Morimoto 
3604a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3605a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3606a7930ed4SKuninori Morimoto 
3607a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3608a7930ed4SKuninori Morimoto 	case 0x11:
3609a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3610a7930ed4SKuninori Morimoto 		break;
3611a7930ed4SKuninori Morimoto 	case 0x10:
3612a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3613a7930ed4SKuninori Morimoto 		break;
3614a7930ed4SKuninori Morimoto 	case 0x01:
3615a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3616a7930ed4SKuninori Morimoto 		break;
3617a7930ed4SKuninori Morimoto 	default:
3618a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3619a7930ed4SKuninori Morimoto 		break;
3620a7930ed4SKuninori Morimoto 	}
3621a7930ed4SKuninori Morimoto 
3622a7930ed4SKuninori Morimoto 	/*
3623a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3624a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3625a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_MASTER_MASK area
3626a7930ed4SKuninori Morimoto 	 */
3627a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3628a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3629389cb834SJyri Sarha 	if (bit && bitclkmaster)
3630389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3631a7930ed4SKuninori Morimoto 
3632a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3633a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3634389cb834SJyri Sarha 	if (frame && framemaster)
3635389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3636a7930ed4SKuninori Morimoto 
3637a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3638a7930ed4SKuninori Morimoto 	case 0x11:
3639a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFM;
3640a7930ed4SKuninori Morimoto 		break;
3641a7930ed4SKuninori Morimoto 	case 0x10:
3642a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBM_CFS;
3643a7930ed4SKuninori Morimoto 		break;
3644a7930ed4SKuninori Morimoto 	case 0x01:
3645a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFM;
3646a7930ed4SKuninori Morimoto 		break;
3647a7930ed4SKuninori Morimoto 	default:
3648a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_CBS_CFS;
3649a7930ed4SKuninori Morimoto 		break;
3650a7930ed4SKuninori Morimoto 	}
3651a7930ed4SKuninori Morimoto 
3652a7930ed4SKuninori Morimoto 	return format;
3653a7930ed4SKuninori Morimoto }
3654a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3655a7930ed4SKuninori Morimoto 
3656a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
3657a180e8b9SKuninori Morimoto {
365809d4cc03SKuninori Morimoto 	struct snd_soc_component *component;
3659*c1e230f0SKuninori Morimoto 	struct snd_soc_dai_link_component dlc;
3660a180e8b9SKuninori Morimoto 	int ret;
3661a180e8b9SKuninori Morimoto 
3662*c1e230f0SKuninori Morimoto 	dlc.of_node	= of_graph_get_port_parent(ep);
3663*c1e230f0SKuninori Morimoto 	dlc.name	= NULL;
3664a180e8b9SKuninori Morimoto 	/*
3665a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
3666a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
3667a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
3668a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
3669a180e8b9SKuninori Morimoto 	 */
3670a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
3671a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
3672*c1e230f0SKuninori Morimoto 	component = soc_find_component(&dlc);
367309d4cc03SKuninori Morimoto 	if (component &&
367409d4cc03SKuninori Morimoto 	    component->driver->of_xlate_dai_id)
367509d4cc03SKuninori Morimoto 		ret = component->driver->of_xlate_dai_id(component, ep);
3676a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
3677a180e8b9SKuninori Morimoto 
3678*c1e230f0SKuninori Morimoto 	of_node_put(dlc.of_node);
3679c0a480d1STony Lindgren 
3680a180e8b9SKuninori Morimoto 	return ret;
3681a180e8b9SKuninori Morimoto }
3682a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3683a180e8b9SKuninori Morimoto 
36841ad8ec53SKuninori Morimoto int snd_soc_get_dai_name(struct of_phandle_args *args,
3685cb470087SKuninori Morimoto 				const char **dai_name)
3686cb470087SKuninori Morimoto {
3687cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
36883e0aa8d8SJyri Sarha 	struct device_node *component_of_node;
368993b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3690cb470087SKuninori Morimoto 
3691cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3692368dee94SKuninori Morimoto 	for_each_component(pos) {
3693c0834440SKuninori Morimoto 		component_of_node = soc_component_to_node(pos);
36943e0aa8d8SJyri Sarha 
36953e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
3696cb470087SKuninori Morimoto 			continue;
3697cb470087SKuninori Morimoto 
36986833c452SKuninori Morimoto 		if (pos->driver->of_xlate_dai_name) {
369993b0f3eeSJean-Francois Moine 			ret = pos->driver->of_xlate_dai_name(pos,
370093b0f3eeSJean-Francois Moine 							     args,
370193b0f3eeSJean-Francois Moine 							     dai_name);
37026833c452SKuninori Morimoto 		} else {
370358bf4179SKuninori Morimoto 			struct snd_soc_dai *dai;
37046833c452SKuninori Morimoto 			int id = -1;
37056833c452SKuninori Morimoto 
370693b0f3eeSJean-Francois Moine 			switch (args->args_count) {
37076833c452SKuninori Morimoto 			case 0:
37086833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
37096833c452SKuninori Morimoto 				break;
37106833c452SKuninori Morimoto 			case 1:
371193b0f3eeSJean-Francois Moine 				id = args->args[0];
37126833c452SKuninori Morimoto 				break;
37136833c452SKuninori Morimoto 			default:
37146833c452SKuninori Morimoto 				/* not supported */
3715cb470087SKuninori Morimoto 				break;
3716cb470087SKuninori Morimoto 			}
3717cb470087SKuninori Morimoto 
37186833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
37196833c452SKuninori Morimoto 				ret = -EINVAL;
37203dcba280SNicolin Chen 				continue;
37216833c452SKuninori Morimoto 			}
3722e41975edSXiubo Li 
3723e41975edSXiubo Li 			ret = 0;
3724e41975edSXiubo Li 
372558bf4179SKuninori Morimoto 			/* find target DAI */
372615a0c645SKuninori Morimoto 			for_each_component_dais(pos, dai) {
372758bf4179SKuninori Morimoto 				if (id == 0)
372858bf4179SKuninori Morimoto 					break;
372958bf4179SKuninori Morimoto 				id--;
373058bf4179SKuninori Morimoto 			}
373158bf4179SKuninori Morimoto 
373258bf4179SKuninori Morimoto 			*dai_name = dai->driver->name;
3733e41975edSXiubo Li 			if (!*dai_name)
3734e41975edSXiubo Li 				*dai_name = pos->name;
37356833c452SKuninori Morimoto 		}
37366833c452SKuninori Morimoto 
3737cb470087SKuninori Morimoto 		break;
3738cb470087SKuninori Morimoto 	}
3739cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
374093b0f3eeSJean-Francois Moine 	return ret;
374193b0f3eeSJean-Francois Moine }
37421ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
374393b0f3eeSJean-Francois Moine 
374493b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
374593b0f3eeSJean-Francois Moine 			    const char **dai_name)
374693b0f3eeSJean-Francois Moine {
374793b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
374893b0f3eeSJean-Francois Moine 	int ret;
374993b0f3eeSJean-Francois Moine 
375093b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
375193b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
375293b0f3eeSJean-Francois Moine 	if (ret)
375393b0f3eeSJean-Francois Moine 		return ret;
375493b0f3eeSJean-Francois Moine 
375593b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3756cb470087SKuninori Morimoto 
3757cb470087SKuninori Morimoto 	of_node_put(args.np);
3758cb470087SKuninori Morimoto 
3759cb470087SKuninori Morimoto 	return ret;
3760cb470087SKuninori Morimoto }
3761cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3762cb470087SKuninori Morimoto 
376393b0f3eeSJean-Francois Moine /*
376494685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
376594685763SSylwester Nawrocki  * @dai_link: DAI link
376694685763SSylwester Nawrocki  *
376794685763SSylwester Nawrocki  * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
376894685763SSylwester Nawrocki  */
376994685763SSylwester Nawrocki void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
377094685763SSylwester Nawrocki {
37713db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *component;
377294685763SSylwester Nawrocki 	int index;
377394685763SSylwester Nawrocki 
37743db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
377594685763SSylwester Nawrocki 		if (!component->of_node)
377694685763SSylwester Nawrocki 			break;
377794685763SSylwester Nawrocki 		of_node_put(component->of_node);
377894685763SSylwester Nawrocki 		component->of_node = NULL;
377994685763SSylwester Nawrocki 	}
378094685763SSylwester Nawrocki }
378194685763SSylwester Nawrocki EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
378294685763SSylwester Nawrocki 
378394685763SSylwester Nawrocki /*
378493b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
378593b0f3eeSJean-Francois Moine  * @dev: Card device
378693b0f3eeSJean-Francois Moine  * @of_node: Device node
378793b0f3eeSJean-Francois Moine  * @dai_link: DAI link
378893b0f3eeSJean-Francois Moine  *
378993b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
379093b0f3eeSJean-Francois Moine  * 'sound-dai'.
379193b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
379294685763SSylwester Nawrocki  * The device nodes in the array (of_node) must be dereferenced by calling
379394685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs() on @dai_link.
379493b0f3eeSJean-Francois Moine  *
379593b0f3eeSJean-Francois Moine  * Returns 0 for success
379693b0f3eeSJean-Francois Moine  */
379793b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
379893b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
379993b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
380093b0f3eeSJean-Francois Moine {
380193b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
380293b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
380393b0f3eeSJean-Francois Moine 	char *name;
380493b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
380593b0f3eeSJean-Francois Moine 
380693b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
380793b0f3eeSJean-Francois Moine 	name = "sound-dai";
380893b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
380993b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
381093b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
381193b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
381293b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
381393b0f3eeSJean-Francois Moine 		else
381493b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
381593b0f3eeSJean-Francois Moine 		return num_codecs;
381693b0f3eeSJean-Francois Moine 	}
3817a86854d0SKees Cook 	component = devm_kcalloc(dev,
3818a86854d0SKees Cook 				 num_codecs, sizeof(*component),
381993b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
382093b0f3eeSJean-Francois Moine 	if (!component)
382193b0f3eeSJean-Francois Moine 		return -ENOMEM;
382293b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
382393b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
382493b0f3eeSJean-Francois Moine 
382593b0f3eeSJean-Francois Moine 	/* Parse the list */
38263db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
382793b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
382893b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
382993b0f3eeSJean-Francois Moine 						 index, &args);
383093b0f3eeSJean-Francois Moine 		if (ret)
383193b0f3eeSJean-Francois Moine 			goto err;
383293b0f3eeSJean-Francois Moine 		component->of_node = args.np;
383393b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
383493b0f3eeSJean-Francois Moine 		if (ret < 0)
383593b0f3eeSJean-Francois Moine 			goto err;
383693b0f3eeSJean-Francois Moine 	}
383793b0f3eeSJean-Francois Moine 	return 0;
383893b0f3eeSJean-Francois Moine err:
383994685763SSylwester Nawrocki 	snd_soc_of_put_dai_link_codecs(dai_link);
384093b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
384193b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
384293b0f3eeSJean-Francois Moine 	return ret;
384393b0f3eeSJean-Francois Moine }
384493b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
384593b0f3eeSJean-Francois Moine 
3846c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3847db2a4165SFrank Mandarino {
38486553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
3849fb257897SMark Brown 	snd_soc_util_init();
3850fb257897SMark Brown 
3851db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3852db2a4165SFrank Mandarino }
38534abe8e16SMark Brown module_init(snd_soc_init);
3854db2a4165SFrank Mandarino 
38557d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3856db2a4165SFrank Mandarino {
3857fb257897SMark Brown 	snd_soc_util_exit();
38586553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3859fb257897SMark Brown 
3860db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3861db2a4165SFrank Mandarino }
3862db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3863db2a4165SFrank Mandarino 
3864db2a4165SFrank Mandarino /* Module information */
3865d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3866db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3867db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
3868db2a4165SFrank Mandarino MODULE_ALIAS("platform:soc-audio");
3869