xref: /openbmc/linux/sound/soc/soc-core.c (revision 99c68653a56528ce66edf3d87bc050eee2ce0aee)
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>
347de14d58SJon Hunter #include <linux/acpi.h>
35db2a4165SFrank Mandarino #include <sound/core.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>
4102e75636SKuninori Morimoto #include <sound/soc-link.h>
42db2a4165SFrank Mandarino #include <sound/initval.h>
43db2a4165SFrank Mandarino 
44a8b1d34fSMark Brown #define CREATE_TRACE_POINTS
45a8b1d34fSMark Brown #include <trace/events/asoc.h>
46a8b1d34fSMark Brown 
47c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex);
48030e79f6SKuninori Morimoto static LIST_HEAD(component_list);
49e894efefSSrinivas Kandagatla static LIST_HEAD(unbind_card_list);
50c5af3a2eSMark Brown 
51368dee94SKuninori Morimoto #define for_each_component(component)			\
52368dee94SKuninori Morimoto 	list_for_each_entry(component, &component_list, list)
53368dee94SKuninori Morimoto 
54db2a4165SFrank Mandarino /*
55587c9844SKuninori Morimoto  * This is used if driver don't need to have CPU/Codec/Platform
56587c9844SKuninori Morimoto  * dai_link. see soc.h
57587c9844SKuninori Morimoto  */
58587c9844SKuninori Morimoto struct snd_soc_dai_link_component null_dailink_component[0];
59587c9844SKuninori Morimoto EXPORT_SYMBOL_GPL(null_dailink_component);
60587c9844SKuninori Morimoto 
61587c9844SKuninori Morimoto /*
62db2a4165SFrank Mandarino  * This is a timeout to do a DAPM powerdown after a stream is closed().
63db2a4165SFrank Mandarino  * It can be used to eliminate pops between different playback streams, e.g.
64db2a4165SFrank Mandarino  * between two audio tracks.
65db2a4165SFrank Mandarino  */
66db2a4165SFrank Mandarino static int pmdown_time = 5000;
67db2a4165SFrank Mandarino module_param(pmdown_time, int, 0);
68db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
69db2a4165SFrank Mandarino 
70dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev,
71dbe21408SMark Brown 				struct device_attribute *attr, char *buf)
72dbe21408SMark Brown {
7336ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
74dbe21408SMark Brown 
75f0fba2adSLiam Girdwood 	return sprintf(buf, "%ld\n", rtd->pmdown_time);
76dbe21408SMark Brown }
77dbe21408SMark Brown 
785a3f869cSYueHaibing static ssize_t pmdown_time_store(struct device *dev,
79dbe21408SMark Brown 				 struct device_attribute *attr,
80dbe21408SMark Brown 				 const char *buf, size_t count)
81dbe21408SMark Brown {
8236ae1a96SMark Brown 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
83c593b520SMark Brown 	int ret;
84dbe21408SMark Brown 
85b785a492SJingoo Han 	ret = kstrtol(buf, 10, &rtd->pmdown_time);
86c593b520SMark Brown 	if (ret)
87c593b520SMark Brown 		return ret;
88dbe21408SMark Brown 
89dbe21408SMark Brown 	return count;
90dbe21408SMark Brown }
91dbe21408SMark Brown 
925a3f869cSYueHaibing static DEVICE_ATTR_RW(pmdown_time);
93dbe21408SMark Brown 
94d29697dcSTakashi Iwai static struct attribute *soc_dev_attrs[] = {
95d29697dcSTakashi Iwai 	&dev_attr_pmdown_time.attr,
96d29697dcSTakashi Iwai 	NULL
97d29697dcSTakashi Iwai };
98d29697dcSTakashi Iwai 
99d29697dcSTakashi Iwai static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
100d29697dcSTakashi Iwai 				       struct attribute *attr, int idx)
101d29697dcSTakashi Iwai {
102d29697dcSTakashi Iwai 	struct device *dev = kobj_to_dev(kobj);
103d29697dcSTakashi Iwai 	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
104d29697dcSTakashi Iwai 
105d918a376SKuninori Morimoto 	if (!rtd)
106d918a376SKuninori Morimoto 		return 0;
107d918a376SKuninori Morimoto 
108d29697dcSTakashi Iwai 	if (attr == &dev_attr_pmdown_time.attr)
109d29697dcSTakashi Iwai 		return attr->mode; /* always visible */
1103b6eed8dSKuninori Morimoto 	return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
111d29697dcSTakashi Iwai }
112d29697dcSTakashi Iwai 
113d29697dcSTakashi Iwai static const struct attribute_group soc_dapm_dev_group = {
114d29697dcSTakashi Iwai 	.attrs = soc_dapm_dev_attrs,
115d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
116d29697dcSTakashi Iwai };
117d29697dcSTakashi Iwai 
118f7e73b26SMark Brown static const struct attribute_group soc_dev_group = {
119d29697dcSTakashi Iwai 	.attrs = soc_dev_attrs,
120d29697dcSTakashi Iwai 	.is_visible = soc_dev_attr_is_visible,
121d29697dcSTakashi Iwai };
122d29697dcSTakashi Iwai 
123d29697dcSTakashi Iwai static const struct attribute_group *soc_dev_attr_groups[] = {
124d29697dcSTakashi Iwai 	&soc_dapm_dev_group,
125f7e73b26SMark Brown 	&soc_dev_group,
126d29697dcSTakashi Iwai 	NULL
127d29697dcSTakashi Iwai };
128d29697dcSTakashi Iwai 
1292624d5faSMark Brown #ifdef CONFIG_DEBUG_FS
130a4072cdfSKuninori Morimoto struct dentry *snd_soc_debugfs_root;
131a4072cdfSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
132a4072cdfSKuninori Morimoto 
13381c7cfd1SLars-Peter Clausen static void soc_init_component_debugfs(struct snd_soc_component *component)
134e73f3de5SRussell King {
1356553bf06SLars-Peter Clausen 	if (!component->card->debugfs_card_root)
1366553bf06SLars-Peter Clausen 		return;
1376553bf06SLars-Peter Clausen 
13881c7cfd1SLars-Peter Clausen 	if (component->debugfs_prefix) {
13981c7cfd1SLars-Peter Clausen 		char *name;
140e73f3de5SRussell King 
14181c7cfd1SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s:%s",
14281c7cfd1SLars-Peter Clausen 			component->debugfs_prefix, component->name);
14381c7cfd1SLars-Peter Clausen 		if (name) {
14481c7cfd1SLars-Peter Clausen 			component->debugfs_root = debugfs_create_dir(name,
14581c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
14681c7cfd1SLars-Peter Clausen 			kfree(name);
14781c7cfd1SLars-Peter Clausen 		}
14881c7cfd1SLars-Peter Clausen 	} else {
14981c7cfd1SLars-Peter Clausen 		component->debugfs_root = debugfs_create_dir(component->name,
15081c7cfd1SLars-Peter Clausen 				component->card->debugfs_card_root);
151e73f3de5SRussell King 	}
152e73f3de5SRussell King 
15381c7cfd1SLars-Peter Clausen 	snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
15481c7cfd1SLars-Peter Clausen 		component->debugfs_root);
15581c7cfd1SLars-Peter Clausen }
15681c7cfd1SLars-Peter Clausen 
15781c7cfd1SLars-Peter Clausen static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
15881c7cfd1SLars-Peter Clausen {
159ad64bfbdSKuninori Morimoto 	if (!component->debugfs_root)
160ad64bfbdSKuninori Morimoto 		return;
16181c7cfd1SLars-Peter Clausen 	debugfs_remove_recursive(component->debugfs_root);
162ad64bfbdSKuninori Morimoto 	component->debugfs_root = NULL;
16381c7cfd1SLars-Peter Clausen }
16481c7cfd1SLars-Peter Clausen 
165c15b2a1dSPeng Donglin static int dai_list_show(struct seq_file *m, void *v)
166f3208780SMark Brown {
1671438c2f6SLars-Peter Clausen 	struct snd_soc_component *component;
168f3208780SMark Brown 	struct snd_soc_dai *dai;
169f3208780SMark Brown 
17034e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
17134e81ab4SLars-Peter Clausen 
172368dee94SKuninori Morimoto 	for_each_component(component)
17315a0c645SKuninori Morimoto 		for_each_component_dais(component, dai)
174700c17caSDonglin Peng 			seq_printf(m, "%s\n", dai->name);
175f3208780SMark Brown 
17634e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
17734e81ab4SLars-Peter Clausen 
178700c17caSDonglin Peng 	return 0;
179700c17caSDonglin Peng }
180c15b2a1dSPeng Donglin DEFINE_SHOW_ATTRIBUTE(dai_list);
181f3208780SMark Brown 
182db795f9bSKuninori Morimoto static int component_list_show(struct seq_file *m, void *v)
183db795f9bSKuninori Morimoto {
184db795f9bSKuninori Morimoto 	struct snd_soc_component *component;
185db795f9bSKuninori Morimoto 
186db795f9bSKuninori Morimoto 	mutex_lock(&client_mutex);
187db795f9bSKuninori Morimoto 
188368dee94SKuninori Morimoto 	for_each_component(component)
189db795f9bSKuninori Morimoto 		seq_printf(m, "%s\n", component->name);
190db795f9bSKuninori Morimoto 
191db795f9bSKuninori Morimoto 	mutex_unlock(&client_mutex);
192db795f9bSKuninori Morimoto 
193db795f9bSKuninori Morimoto 	return 0;
194db795f9bSKuninori Morimoto }
195db795f9bSKuninori Morimoto DEFINE_SHOW_ATTRIBUTE(component_list);
196db795f9bSKuninori Morimoto 
197a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card)
198a6052154SJarkko Nikula {
199a6052154SJarkko Nikula 	card->debugfs_card_root = debugfs_create_dir(card->name,
2008a9dab1aSMark Brown 						     snd_soc_debugfs_root);
2013a45b867SJarkko Nikula 
202fee531d6SGreg Kroah-Hartman 	debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root,
2033a45b867SJarkko Nikula 			   &card->pop_time);
204d8ca7a0aSKuninori Morimoto 
205d8ca7a0aSKuninori Morimoto 	snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
206a6052154SJarkko Nikula }
207a6052154SJarkko Nikula 
208a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
209a6052154SJarkko Nikula {
210a6052154SJarkko Nikula 	debugfs_remove_recursive(card->debugfs_card_root);
21129040d1aSKuninori Morimoto 	card->debugfs_card_root = NULL;
212a6052154SJarkko Nikula }
213a6052154SJarkko Nikula 
2146553bf06SLars-Peter Clausen static void snd_soc_debugfs_init(void)
2156553bf06SLars-Peter Clausen {
2166553bf06SLars-Peter Clausen 	snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
2176553bf06SLars-Peter Clausen 
218fee531d6SGreg Kroah-Hartman 	debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
219fee531d6SGreg Kroah-Hartman 			    &dai_list_fops);
220db795f9bSKuninori Morimoto 
221fee531d6SGreg Kroah-Hartman 	debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
222fee531d6SGreg Kroah-Hartman 			    &component_list_fops);
2236553bf06SLars-Peter Clausen }
2246553bf06SLars-Peter Clausen 
2256553bf06SLars-Peter Clausen static void snd_soc_debugfs_exit(void)
2266553bf06SLars-Peter Clausen {
2276553bf06SLars-Peter Clausen 	debugfs_remove_recursive(snd_soc_debugfs_root);
2286553bf06SLars-Peter Clausen }
2296553bf06SLars-Peter Clausen 
2302624d5faSMark Brown #else
2312624d5faSMark Brown 
23281c7cfd1SLars-Peter Clausen static inline void soc_init_component_debugfs(
23381c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
2342624d5faSMark Brown {
2352624d5faSMark Brown }
2362624d5faSMark Brown 
23781c7cfd1SLars-Peter Clausen static inline void soc_cleanup_component_debugfs(
23881c7cfd1SLars-Peter Clausen 	struct snd_soc_component *component)
239731f1ab2SSebastien Guiriec {
240731f1ab2SSebastien Guiriec }
241731f1ab2SSebastien Guiriec 
242b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card)
243b95fccbcSAxel Lin {
244b95fccbcSAxel Lin }
245b95fccbcSAxel Lin 
246b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
247b95fccbcSAxel Lin {
248b95fccbcSAxel Lin }
2496553bf06SLars-Peter Clausen 
2506553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_init(void)
2516553bf06SLars-Peter Clausen {
2526553bf06SLars-Peter Clausen }
2536553bf06SLars-Peter Clausen 
2546553bf06SLars-Peter Clausen static inline void snd_soc_debugfs_exit(void)
2556553bf06SLars-Peter Clausen {
2566553bf06SLars-Peter Clausen }
2576553bf06SLars-Peter Clausen 
2582624d5faSMark Brown #endif
2592624d5faSMark Brown 
26012b05232SKuninori Morimoto static int snd_soc_rtd_add_component(struct snd_soc_pcm_runtime *rtd,
261a0ac4411SKuninori Morimoto 				     struct snd_soc_component *component)
262a0ac4411SKuninori Morimoto {
2632b544dd7SKuninori Morimoto 	struct snd_soc_component *comp;
264613fb500SKuninori Morimoto 	int i;
265a0ac4411SKuninori Morimoto 
266613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, comp) {
267a0ac4411SKuninori Morimoto 		/* already connected */
2682b544dd7SKuninori Morimoto 		if (comp == component)
269a0ac4411SKuninori Morimoto 			return 0;
270a0ac4411SKuninori Morimoto 	}
271a0ac4411SKuninori Morimoto 
272613fb500SKuninori Morimoto 	/* see for_each_rtd_components */
273613fb500SKuninori Morimoto 	rtd->components[rtd->num_components] = component;
274613fb500SKuninori Morimoto 	rtd->num_components++;
275a0ac4411SKuninori Morimoto 
276a0ac4411SKuninori Morimoto 	return 0;
277a0ac4411SKuninori Morimoto }
278a0ac4411SKuninori Morimoto 
279a0ac4411SKuninori Morimoto struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
280a0ac4411SKuninori Morimoto 						const char *driver_name)
281a0ac4411SKuninori Morimoto {
2822b544dd7SKuninori Morimoto 	struct snd_soc_component *component;
283613fb500SKuninori Morimoto 	int i;
284a0ac4411SKuninori Morimoto 
285971da24cSKuninori Morimoto 	if (!driver_name)
286971da24cSKuninori Morimoto 		return NULL;
287971da24cSKuninori Morimoto 
288a33c0d16SKuninori Morimoto 	/*
289a33c0d16SKuninori Morimoto 	 * NOTE
290a33c0d16SKuninori Morimoto 	 *
291a33c0d16SKuninori Morimoto 	 * snd_soc_rtdcom_lookup() will find component from rtd by using
292a33c0d16SKuninori Morimoto 	 * specified driver name.
293a33c0d16SKuninori Morimoto 	 * But, if many components which have same driver name are connected
294a33c0d16SKuninori Morimoto 	 * to 1 rtd, this function will return 1st found component.
295a33c0d16SKuninori Morimoto 	 */
296613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
2972b544dd7SKuninori Morimoto 		const char *component_name = component->driver->name;
298971da24cSKuninori Morimoto 
299971da24cSKuninori Morimoto 		if (!component_name)
300971da24cSKuninori Morimoto 			continue;
301971da24cSKuninori Morimoto 
302971da24cSKuninori Morimoto 		if ((component_name == driver_name) ||
303971da24cSKuninori Morimoto 		    strcmp(component_name, driver_name) == 0)
304613fb500SKuninori Morimoto 			return component;
305a0ac4411SKuninori Morimoto 	}
306a0ac4411SKuninori Morimoto 
307a0ac4411SKuninori Morimoto 	return NULL;
308a0ac4411SKuninori Morimoto }
309031734b7SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
310a0ac4411SKuninori Morimoto 
3116fbea6b6SShengjiu Wang struct snd_soc_component
31218dd66eaSKuninori Morimoto *snd_soc_lookup_component_nolocked(struct device *dev, const char *driver_name)
313b8132657SKuninori Morimoto {
314b8132657SKuninori Morimoto 	struct snd_soc_component *component;
3155bd7e08bSKuninori Morimoto 	struct snd_soc_component *found_component;
316b8132657SKuninori Morimoto 
3175bd7e08bSKuninori Morimoto 	found_component = NULL;
318b8132657SKuninori Morimoto 	for_each_component(component) {
3195bd7e08bSKuninori Morimoto 		if ((dev == component->dev) &&
3205bd7e08bSKuninori Morimoto 		    (!driver_name ||
3215bd7e08bSKuninori Morimoto 		     (driver_name == component->driver->name) ||
3225bd7e08bSKuninori Morimoto 		     (strcmp(component->driver->name, driver_name) == 0))) {
3235bd7e08bSKuninori Morimoto 			found_component = component;
324b8132657SKuninori Morimoto 			break;
325b8132657SKuninori Morimoto 		}
3265bd7e08bSKuninori Morimoto 	}
327b8132657SKuninori Morimoto 
3285bd7e08bSKuninori Morimoto 	return found_component;
329b8132657SKuninori Morimoto }
3306fbea6b6SShengjiu Wang EXPORT_SYMBOL_GPL(snd_soc_lookup_component_nolocked);
33118dd66eaSKuninori Morimoto 
33218dd66eaSKuninori Morimoto struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
33318dd66eaSKuninori Morimoto 						   const char *driver_name)
33418dd66eaSKuninori Morimoto {
33518dd66eaSKuninori Morimoto 	struct snd_soc_component *component;
33618dd66eaSKuninori Morimoto 
33718dd66eaSKuninori Morimoto 	mutex_lock(&client_mutex);
33818dd66eaSKuninori Morimoto 	component = snd_soc_lookup_component_nolocked(dev, driver_name);
33918dd66eaSKuninori Morimoto 	mutex_unlock(&client_mutex);
34018dd66eaSKuninori Morimoto 
34118dd66eaSKuninori Morimoto 	return component;
34218dd66eaSKuninori Morimoto }
343b8132657SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
344b8132657SKuninori Morimoto 
34594def8eaSKuninori Morimoto struct snd_soc_pcm_runtime
34694def8eaSKuninori Morimoto *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
3474468189fSKuninori Morimoto 			 struct snd_soc_dai_link *dai_link)
34894def8eaSKuninori Morimoto {
34994def8eaSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
35094def8eaSKuninori Morimoto 
35194def8eaSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
3524468189fSKuninori Morimoto 		if (rtd->dai_link == dai_link)
35394def8eaSKuninori Morimoto 			return rtd;
35494def8eaSKuninori Morimoto 	}
3554468189fSKuninori Morimoto 	dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link->name);
35694def8eaSKuninori Morimoto 	return NULL;
35794def8eaSKuninori Morimoto }
35894def8eaSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
35994def8eaSKuninori Morimoto 
36083f94a2eSKuninori Morimoto /*
36183f94a2eSKuninori Morimoto  * Power down the audio subsystem pmdown_time msecs after close is called.
36283f94a2eSKuninori Morimoto  * This is to ensure there are no pops or clicks in between any music tracks
36383f94a2eSKuninori Morimoto  * due to DAPM power cycling.
36483f94a2eSKuninori Morimoto  */
36583f94a2eSKuninori Morimoto void snd_soc_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
36683f94a2eSKuninori Morimoto {
367c2233a26SKuninori Morimoto 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
3680f6011fdSKuninori Morimoto 	int playback = SNDRV_PCM_STREAM_PLAYBACK;
36983f94a2eSKuninori Morimoto 
37083f94a2eSKuninori Morimoto 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
37183f94a2eSKuninori Morimoto 
37283f94a2eSKuninori Morimoto 	dev_dbg(rtd->dev,
37383f94a2eSKuninori Morimoto 		"ASoC: pop wq checking: %s status: %s waiting: %s\n",
37483f94a2eSKuninori Morimoto 		codec_dai->driver->playback.stream_name,
375b3dea624SKuninori Morimoto 		snd_soc_dai_stream_active(codec_dai, playback) ?
376b3dea624SKuninori Morimoto 		"active" : "inactive",
37783f94a2eSKuninori Morimoto 		rtd->pop_wait ? "yes" : "no");
37883f94a2eSKuninori Morimoto 
37983f94a2eSKuninori Morimoto 	/* are we waiting on this codec DAI stream */
38083f94a2eSKuninori Morimoto 	if (rtd->pop_wait == 1) {
38183f94a2eSKuninori Morimoto 		rtd->pop_wait = 0;
3820f6011fdSKuninori Morimoto 		snd_soc_dapm_stream_event(rtd, playback,
38383f94a2eSKuninori Morimoto 					  SND_SOC_DAPM_STREAM_STOP);
38483f94a2eSKuninori Morimoto 	}
38583f94a2eSKuninori Morimoto 
38683f94a2eSKuninori Morimoto 	mutex_unlock(&rtd->card->pcm_mutex);
38783f94a2eSKuninori Morimoto }
38883f94a2eSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_close_delayed_work);
38983f94a2eSKuninori Morimoto 
3906e864344SKuninori Morimoto static void soc_release_rtd_dev(struct device *dev)
3916e864344SKuninori Morimoto {
3926e864344SKuninori Morimoto 	/* "dev" means "rtd->dev" */
3936e864344SKuninori Morimoto 	kfree(dev);
3946e864344SKuninori Morimoto }
3956e864344SKuninori Morimoto 
3961c93a9e0SKuninori Morimoto static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
3971c93a9e0SKuninori Morimoto {
3986e864344SKuninori Morimoto 	if (!rtd)
3996e864344SKuninori Morimoto 		return;
4006e864344SKuninori Morimoto 
401753ace0aSKuninori Morimoto 	list_del(&rtd->list);
402b7c5bc45SKuninori Morimoto 
4039c9b6520SCurtis Malainey 	if (delayed_work_pending(&rtd->delayed_work))
4040ced7b05SKuninori Morimoto 		flush_delayed_work(&rtd->delayed_work);
4050ced7b05SKuninori Morimoto 	snd_soc_pcm_component_free(rtd);
4060ced7b05SKuninori Morimoto 
407b7c5bc45SKuninori Morimoto 	/*
408b7c5bc45SKuninori Morimoto 	 * we don't need to call kfree() for rtd->dev
409b7c5bc45SKuninori Morimoto 	 * see
410b7c5bc45SKuninori Morimoto 	 *	soc_release_rtd_dev()
411d918a376SKuninori Morimoto 	 *
412d918a376SKuninori Morimoto 	 * We don't need rtd->dev NULL check, because
413d918a376SKuninori Morimoto 	 * it is alloced *before* rtd.
414d918a376SKuninori Morimoto 	 * see
415d918a376SKuninori Morimoto 	 *	soc_new_pcm_runtime()
416f1b3ee78SKuninori Morimoto 	 *
417f1b3ee78SKuninori Morimoto 	 * We don't need to mind freeing for rtd,
418f1b3ee78SKuninori Morimoto 	 * because it was created from dev (= rtd->dev)
419f1b3ee78SKuninori Morimoto 	 * see
420f1b3ee78SKuninori Morimoto 	 *	soc_new_pcm_runtime()
421f1b3ee78SKuninori Morimoto 	 *
422f1b3ee78SKuninori Morimoto 	 *		rtd = devm_kzalloc(dev, ...);
423f1b3ee78SKuninori Morimoto 	 *		rtd->dev = dev
424b7c5bc45SKuninori Morimoto 	 */
425b7c5bc45SKuninori Morimoto 	device_unregister(rtd->dev);
4261c93a9e0SKuninori Morimoto }
4271c93a9e0SKuninori Morimoto 
4284bf2e385SCurtis Malainey static void close_delayed_work(struct work_struct *work) {
4294bf2e385SCurtis Malainey 	struct snd_soc_pcm_runtime *rtd =
4304bf2e385SCurtis Malainey 			container_of(work, struct snd_soc_pcm_runtime,
4314bf2e385SCurtis Malainey 				     delayed_work.work);
4324bf2e385SCurtis Malainey 
4334bf2e385SCurtis Malainey 	if (rtd->close_delayed_work_func)
4344bf2e385SCurtis Malainey 		rtd->close_delayed_work_func(rtd);
4354bf2e385SCurtis Malainey }
4364bf2e385SCurtis Malainey 
4371a497983SMengdong Lin static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
4381a497983SMengdong Lin 	struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
4391a497983SMengdong Lin {
4401a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
441613fb500SKuninori Morimoto 	struct snd_soc_component *component;
442d918a376SKuninori Morimoto 	struct device *dev;
4436e864344SKuninori Morimoto 	int ret;
444d74c2a15SKuninori Morimoto 	int stream;
4451a497983SMengdong Lin 
4466e864344SKuninori Morimoto 	/*
447d918a376SKuninori Morimoto 	 * for rtd->dev
448d918a376SKuninori Morimoto 	 */
449d918a376SKuninori Morimoto 	dev = kzalloc(sizeof(struct device), GFP_KERNEL);
450d918a376SKuninori Morimoto 	if (!dev)
451d918a376SKuninori Morimoto 		return NULL;
452d918a376SKuninori Morimoto 
453d918a376SKuninori Morimoto 	dev->parent	= card->dev;
454d918a376SKuninori Morimoto 	dev->release	= soc_release_rtd_dev;
455d918a376SKuninori Morimoto 
456d918a376SKuninori Morimoto 	dev_set_name(dev, "%s", dai_link->name);
457d918a376SKuninori Morimoto 
458d918a376SKuninori Morimoto 	ret = device_register(dev);
459d918a376SKuninori Morimoto 	if (ret < 0) {
460d918a376SKuninori Morimoto 		put_device(dev); /* soc_release_rtd_dev */
461d918a376SKuninori Morimoto 		return NULL;
462d918a376SKuninori Morimoto 	}
463d918a376SKuninori Morimoto 
464d918a376SKuninori Morimoto 	/*
4656e864344SKuninori Morimoto 	 * for rtd
4666e864344SKuninori Morimoto 	 */
467613fb500SKuninori Morimoto 	rtd = devm_kzalloc(dev,
468613fb500SKuninori Morimoto 			   sizeof(*rtd) +
469613fb500SKuninori Morimoto 			   sizeof(*component) * (dai_link->num_cpus +
470613fb500SKuninori Morimoto 						 dai_link->num_codecs +
471613fb500SKuninori Morimoto 						 dai_link->num_platforms),
472613fb500SKuninori Morimoto 			   GFP_KERNEL);
4735fa7553dSKuninori Morimoto 	if (!rtd) {
4745fa7553dSKuninori Morimoto 		device_unregister(dev);
4755fa7553dSKuninori Morimoto 		return NULL;
4765fa7553dSKuninori Morimoto 	}
4771a497983SMengdong Lin 
478d918a376SKuninori Morimoto 	rtd->dev = dev;
47907d22a9bSTakashi Iwai 	INIT_LIST_HEAD(&rtd->list);
480d74c2a15SKuninori Morimoto 	for_each_pcm_streams(stream) {
481d74c2a15SKuninori Morimoto 		INIT_LIST_HEAD(&rtd->dpcm[stream].be_clients);
482d74c2a15SKuninori Morimoto 		INIT_LIST_HEAD(&rtd->dpcm[stream].fe_clients);
483d74c2a15SKuninori Morimoto 	}
484d918a376SKuninori Morimoto 	dev_set_drvdata(dev, rtd);
4854bf2e385SCurtis Malainey 	INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
486d918a376SKuninori Morimoto 
4876e864344SKuninori Morimoto 	/*
48822a2fc81SKuninori Morimoto 	 * for rtd->dais
4896e864344SKuninori Morimoto 	 */
49022a2fc81SKuninori Morimoto 	rtd->dais = devm_kcalloc(dev, dai_link->num_cpus + dai_link->num_codecs,
4916396bb22SKees Cook 					sizeof(struct snd_soc_dai *),
4921a497983SMengdong Lin 					GFP_KERNEL);
49322a2fc81SKuninori Morimoto 	if (!rtd->dais)
4946e864344SKuninori Morimoto 		goto free_rtd;
4956e864344SKuninori Morimoto 
4966e864344SKuninori Morimoto 	/*
49722a2fc81SKuninori Morimoto 	 * dais = [][][][][][][][][][][][][][][][][][]
49822a2fc81SKuninori Morimoto 	 *	  ^cpu_dais         ^codec_dais
49922a2fc81SKuninori Morimoto 	 *	  |--- num_cpus ---|--- num_codecs --|
5001729025bSKuninori Morimoto 	 * see
5011729025bSKuninori Morimoto 	 *	asoc_rtd_to_cpu()
5021729025bSKuninori Morimoto 	 *	asoc_rtd_to_codec()
50376afa643SShreyas NC 	 */
50449648d02SKuninori Morimoto 	rtd->num_cpus	= dai_link->num_cpus;
50549648d02SKuninori Morimoto 	rtd->num_codecs	= dai_link->num_codecs;
506929deb84SKuninori Morimoto 	rtd->card	= card;
507929deb84SKuninori Morimoto 	rtd->dai_link	= dai_link;
50801faf501SKuninori Morimoto 	rtd->num	= card->num_rtd++;
509929deb84SKuninori Morimoto 
5106634e3d6SKuninori Morimoto 	/* see for_each_card_rtds */
5111a497983SMengdong Lin 	list_add_tail(&rtd->list, &card->rtd_list);
512a848125eSKuninori Morimoto 
5135c74c9d3SCharles Keepax 	ret = device_add_groups(dev, soc_dev_attr_groups);
5145c74c9d3SCharles Keepax 	if (ret < 0)
5155c74c9d3SCharles Keepax 		goto free_rtd;
5165c74c9d3SCharles Keepax 
517a848125eSKuninori Morimoto 	return rtd;
5186e864344SKuninori Morimoto 
5196e864344SKuninori Morimoto free_rtd:
5206e864344SKuninori Morimoto 	soc_free_pcm_runtime(rtd);
5216e864344SKuninori Morimoto 	return NULL;
5221a497983SMengdong Lin }
5231a497983SMengdong Lin 
52465462e44SKuninori Morimoto static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
52565462e44SKuninori Morimoto {
52665462e44SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
52765462e44SKuninori Morimoto 
52865462e44SKuninori Morimoto 	for_each_card_rtds(card, rtd)
52965462e44SKuninori Morimoto 		flush_delayed_work(&rtd->delayed_work);
53065462e44SKuninori Morimoto }
53165462e44SKuninori Morimoto 
5326f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP
533d4c1d9ebSKuninori Morimoto static void soc_playback_digital_mute(struct snd_soc_card *card, int mute)
534d4c1d9ebSKuninori Morimoto {
535d4c1d9ebSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
536d4c1d9ebSKuninori Morimoto 	struct snd_soc_dai *dai;
537d4c1d9ebSKuninori Morimoto 	int playback = SNDRV_PCM_STREAM_PLAYBACK;
538d4c1d9ebSKuninori Morimoto 	int i;
539d4c1d9ebSKuninori Morimoto 
540d4c1d9ebSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
541d4c1d9ebSKuninori Morimoto 
542d4c1d9ebSKuninori Morimoto 		if (rtd->dai_link->ignore_suspend)
543d4c1d9ebSKuninori Morimoto 			continue;
544d4c1d9ebSKuninori Morimoto 
545d4c1d9ebSKuninori Morimoto 		for_each_rtd_dais(rtd, i, dai) {
546d4c1d9ebSKuninori Morimoto 			if (snd_soc_dai_stream_active(dai, playback))
547d4c1d9ebSKuninori Morimoto 				snd_soc_dai_digital_mute(dai, mute, playback);
548d4c1d9ebSKuninori Morimoto 		}
549d4c1d9ebSKuninori Morimoto 	}
550d4c1d9ebSKuninori Morimoto }
551d4c1d9ebSKuninori Morimoto 
552baed393eSKuninori Morimoto static void soc_dapm_suspend_resume(struct snd_soc_card *card, int event)
553baed393eSKuninori Morimoto {
554baed393eSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
555baed393eSKuninori Morimoto 	int stream;
556baed393eSKuninori Morimoto 
557baed393eSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
558baed393eSKuninori Morimoto 
559baed393eSKuninori Morimoto 		if (rtd->dai_link->ignore_suspend)
560baed393eSKuninori Morimoto 			continue;
561baed393eSKuninori Morimoto 
562baed393eSKuninori Morimoto 		for_each_pcm_streams(stream)
563baed393eSKuninori Morimoto 			snd_soc_dapm_stream_event(rtd, stream, event);
564baed393eSKuninori Morimoto 	}
565baed393eSKuninori Morimoto }
566baed393eSKuninori Morimoto 
567db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */
5686f8ab4acSMark Brown int snd_soc_suspend(struct device *dev)
569db2a4165SFrank Mandarino {
5706f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
571d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
5721a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
5731a497983SMengdong Lin 	int i;
574db2a4165SFrank Mandarino 
575c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
576c5599b87SLars-Peter Clausen 	if (!card->instantiated)
577e3509ff0SDaniel Mack 		return 0;
578e3509ff0SDaniel Mack 
5792c7b696aSMarcel Ziswiler 	/*
5802c7b696aSMarcel Ziswiler 	 * Due to the resume being scheduled into a workqueue we could
5816ed25978SAndy Green 	 * suspend before that's finished - wait for it to complete.
5826ed25978SAndy Green 	 */
583b6cc78daSTakashi Iwai 	snd_power_wait(card->snd_card);
5846ed25978SAndy Green 
5856ed25978SAndy Green 	/* we're going to block userspace touching us until resume completes */
586f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
5876ed25978SAndy Green 
588a00f90f9SMark Brown 	/* mute any active DACs */
589d4c1d9ebSKuninori Morimoto 	soc_playback_digital_mute(card, 1);
590db2a4165SFrank Mandarino 
5914ccab3e7SLiam Girdwood 	/* suspend all pcms */
592bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
5931a497983SMengdong Lin 		if (rtd->dai_link->ignore_suspend)
5943efab7dcSMark Brown 			continue;
5953efab7dcSMark Brown 
5961a497983SMengdong Lin 		snd_pcm_suspend_all(rtd->pcm);
5973efab7dcSMark Brown 	}
5984ccab3e7SLiam Girdwood 
599130dc08cSKuninori Morimoto 	snd_soc_card_suspend_pre(card);
600db2a4165SFrank Mandarino 
60137660b6dSLars-Peter Clausen 	/* close any waiting streams */
60265462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
603db2a4165SFrank Mandarino 
604baed393eSKuninori Morimoto 	soc_dapm_suspend_resume(card, SND_SOC_DAPM_STREAM_SUSPEND);
605db2a4165SFrank Mandarino 
6068be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
6078be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
608e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
609e2d32ff6SMark Brown 
6109178feb4SKuninori Morimoto 	/* suspend all COMPONENTs */
6111272063aSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
6121272063aSKuninori Morimoto 
6131272063aSKuninori Morimoto 		if (rtd->dai_link->ignore_suspend)
6141272063aSKuninori Morimoto 			continue;
6151272063aSKuninori Morimoto 
6161272063aSKuninori Morimoto 		for_each_rtd_components(rtd, i, component) {
6172c7b696aSMarcel Ziswiler 			struct snd_soc_dapm_context *dapm =
6182c7b696aSMarcel Ziswiler 				snd_soc_component_get_dapm(component);
619d9fc4063SKuninori Morimoto 
6202c7b696aSMarcel Ziswiler 			/*
6211272063aSKuninori Morimoto 			 * ignore if component was already suspended
6222c7b696aSMarcel Ziswiler 			 */
6231272063aSKuninori Morimoto 			if (snd_soc_component_is_suspended(component))
6241272063aSKuninori Morimoto 				continue;
6251272063aSKuninori Morimoto 
6261272063aSKuninori Morimoto 			/*
6271272063aSKuninori Morimoto 			 * If there are paths active then the COMPONENT will be
6281272063aSKuninori Morimoto 			 * held with bias _ON and should not be suspended.
6291272063aSKuninori Morimoto 			 */
6304890140fSLars-Peter Clausen 			switch (snd_soc_dapm_get_bias_level(dapm)) {
6311547aba9SMark Brown 			case SND_SOC_BIAS_STANDBY:
632125a25daSMark Brown 				/*
6339178feb4SKuninori Morimoto 				 * If the COMPONENT is capable of idle
634125a25daSMark Brown 				 * bias off then being in STANDBY
635125a25daSMark Brown 				 * means it's doing something,
636125a25daSMark Brown 				 * otherwise fall through.
637125a25daSMark Brown 				 */
6384890140fSLars-Peter Clausen 				if (dapm->idle_bias_off) {
6399178feb4SKuninori Morimoto 					dev_dbg(component->dev,
64010e8aa9aSMichał Mirosław 						"ASoC: idle_bias_off CODEC on over suspend\n");
641125a25daSMark Brown 					break;
642125a25daSMark Brown 				}
643df561f66SGustavo A. R. Silva 				fallthrough;
644a8093297SLars-Peter Clausen 
6451547aba9SMark Brown 			case SND_SOC_BIAS_OFF:
64666c51573SKuninori Morimoto 				snd_soc_component_suspend(component);
6479178feb4SKuninori Morimoto 				if (component->regmap)
6489178feb4SKuninori Morimoto 					regcache_mark_dirty(component->regmap);
649988e8cc4SNicolin Chen 				/* deactivate pins to sleep state */
6509178feb4SKuninori Morimoto 				pinctrl_pm_select_sleep_state(component->dev);
6511547aba9SMark Brown 				break;
6521547aba9SMark Brown 			default:
6539178feb4SKuninori Morimoto 				dev_dbg(component->dev,
6549178feb4SKuninori Morimoto 					"ASoC: COMPONENT is on over suspend\n");
6551547aba9SMark Brown 				break;
6561547aba9SMark Brown 			}
6571547aba9SMark Brown 		}
658f0fba2adSLiam Girdwood 	}
659db2a4165SFrank Mandarino 
660d17b60b2SKuninori Morimoto 	snd_soc_card_suspend_post(card);
661db2a4165SFrank Mandarino 
662db2a4165SFrank Mandarino 	return 0;
663db2a4165SFrank Mandarino }
6646f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend);
665db2a4165SFrank Mandarino 
6662c7b696aSMarcel Ziswiler /*
6672c7b696aSMarcel Ziswiler  * deferred resume work, so resume can complete before we finished
6686ed25978SAndy Green  * setting our codec back up, which can be very slow on I2C
6696ed25978SAndy Green  */
6706ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work)
671db2a4165SFrank Mandarino {
672f0fba2adSLiam Girdwood 	struct snd_soc_card *card =
6732c7b696aSMarcel Ziswiler 			container_of(work, struct snd_soc_card,
6742c7b696aSMarcel Ziswiler 				     deferred_resume_work);
675d9fc4063SKuninori Morimoto 	struct snd_soc_component *component;
676db2a4165SFrank Mandarino 
6772c7b696aSMarcel Ziswiler 	/*
6782c7b696aSMarcel Ziswiler 	 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
6796ed25978SAndy Green 	 * so userspace apps are blocked from touching us
6806ed25978SAndy Green 	 */
6816ed25978SAndy Green 
682f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: starting resume work\n");
6836ed25978SAndy Green 
6849949788bSMark Brown 	/* Bring us up into D2 so that DAPM starts enabling things */
685f0fba2adSLiam Girdwood 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
6869949788bSMark Brown 
687934c752cSKuninori Morimoto 	snd_soc_card_resume_pre(card);
688db2a4165SFrank Mandarino 
689f70f18f7SKuninori Morimoto 	for_each_card_components(card, component) {
690e40fadbcSKuninori Morimoto 		if (snd_soc_component_is_suspended(component))
6919a840cbaSKuninori Morimoto 			snd_soc_component_resume(component);
6921547aba9SMark Brown 	}
693db2a4165SFrank Mandarino 
694baed393eSKuninori Morimoto 	soc_dapm_suspend_resume(card, SND_SOC_DAPM_STREAM_RESUME);
695db2a4165SFrank Mandarino 
6963ff3f64bSMark Brown 	/* unmute any active DACs */
697d4c1d9ebSKuninori Morimoto 	soc_playback_digital_mute(card, 0);
698db2a4165SFrank Mandarino 
699739443d1SKuninori Morimoto 	snd_soc_card_resume_post(card);
700db2a4165SFrank Mandarino 
701f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: resume work completed\n");
7026ed25978SAndy Green 
7038be4da29SLars-Peter Clausen 	/* Recheck all endpoints too, their state is affected by suspend */
7048be4da29SLars-Peter Clausen 	dapm_mark_endpoints_dirty(card);
705e2d32ff6SMark Brown 	snd_soc_dapm_sync(&card->dapm);
7061a7aaa58SJeeja KP 
7071a7aaa58SJeeja KP 	/* userspace can access us now we are back as we were before */
7081a7aaa58SJeeja KP 	snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
7096ed25978SAndy Green }
7106ed25978SAndy Green 
7116ed25978SAndy Green /* powers up audio subsystem after a suspend */
7126f8ab4acSMark Brown int snd_soc_resume(struct device *dev)
7136ed25978SAndy Green {
7146f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
71576c39e86SKuninori Morimoto 	struct snd_soc_component *component;
716b9dd94a8SPeter Ujfalusi 
717c5599b87SLars-Peter Clausen 	/* If the card is not initialized yet there is nothing to do */
718c5599b87SLars-Peter Clausen 	if (!card->instantiated)
7195ff1ddf2SEric Miao 		return 0;
7205ff1ddf2SEric Miao 
721988e8cc4SNicolin Chen 	/* activate pins from sleep state */
72276c39e86SKuninori Morimoto 	for_each_card_components(card, component)
723b3dea624SKuninori Morimoto 		if (snd_soc_component_active(component))
72476c39e86SKuninori Morimoto 			pinctrl_pm_select_default_state(component->dev);
725988e8cc4SNicolin Chen 
726f110bfc7SLiam Girdwood 	dev_dbg(dev, "ASoC: Scheduling resume work\n");
7276308419aSMark Brown 	if (!schedule_work(&card->deferred_resume_work))
728f110bfc7SLiam Girdwood 		dev_err(dev, "ASoC: resume work item may be lost\n");
7296ed25978SAndy Green 
730db2a4165SFrank Mandarino 	return 0;
731db2a4165SFrank Mandarino }
7326f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume);
733b3da4251SKuninori Morimoto 
734b3da4251SKuninori Morimoto static void soc_resume_init(struct snd_soc_card *card)
735b3da4251SKuninori Morimoto {
736b3da4251SKuninori Morimoto 	/* deferred resume work */
737b3da4251SKuninori Morimoto 	INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
738b3da4251SKuninori Morimoto }
739db2a4165SFrank Mandarino #else
7406f8ab4acSMark Brown #define snd_soc_suspend NULL
7416f8ab4acSMark Brown #define snd_soc_resume NULL
742b3da4251SKuninori Morimoto static inline void soc_resume_init(struct snd_soc_card *card)
743b3da4251SKuninori Morimoto {
744b3da4251SKuninori Morimoto }
745db2a4165SFrank Mandarino #endif
746db2a4165SFrank Mandarino 
747c0834440SKuninori Morimoto static struct device_node
748c0834440SKuninori Morimoto *soc_component_to_node(struct snd_soc_component *component)
74912023a9aSMisael Lopez Cruz {
750c0834440SKuninori Morimoto 	struct device_node *of_node;
75112023a9aSMisael Lopez Cruz 
752c0834440SKuninori Morimoto 	of_node = component->dev->of_node;
753c0834440SKuninori Morimoto 	if (!of_node && component->dev->parent)
754c0834440SKuninori Morimoto 		of_node = component->dev->parent->of_node;
75534e81ab4SLars-Peter Clausen 
756c0834440SKuninori Morimoto 	return of_node;
75712023a9aSMisael Lopez Cruz }
75812023a9aSMisael Lopez Cruz 
759be6ac0a9SKuninori Morimoto static int snd_soc_is_matching_component(
760be6ac0a9SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc,
761be6ac0a9SKuninori Morimoto 	struct snd_soc_component *component)
762be6ac0a9SKuninori Morimoto {
763be6ac0a9SKuninori Morimoto 	struct device_node *component_of_node;
764be6ac0a9SKuninori Morimoto 
7657d7db5d3SKuninori Morimoto 	if (!dlc)
7667d7db5d3SKuninori Morimoto 		return 0;
7677d7db5d3SKuninori Morimoto 
7687d7db5d3SKuninori Morimoto 	component_of_node = soc_component_to_node(component);
769be6ac0a9SKuninori Morimoto 
770be6ac0a9SKuninori Morimoto 	if (dlc->of_node && component_of_node != dlc->of_node)
771be6ac0a9SKuninori Morimoto 		return 0;
772be6ac0a9SKuninori Morimoto 	if (dlc->name && strcmp(component->name, dlc->name))
773be6ac0a9SKuninori Morimoto 		return 0;
774be6ac0a9SKuninori Morimoto 
775be6ac0a9SKuninori Morimoto 	return 1;
776be6ac0a9SKuninori Morimoto }
777be6ac0a9SKuninori Morimoto 
778db2a4165SFrank Mandarino static struct snd_soc_component *soc_find_component(
779c1e230f0SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc)
780db2a4165SFrank Mandarino {
781db2a4165SFrank Mandarino 	struct snd_soc_component *component;
782db2a4165SFrank Mandarino 
783db2a4165SFrank Mandarino 	lockdep_assert_held(&client_mutex);
784db2a4165SFrank Mandarino 
785e3303268SKuninori Morimoto 	/*
786e3303268SKuninori Morimoto 	 * NOTE
787e3303268SKuninori Morimoto 	 *
788e3303268SKuninori Morimoto 	 * It returns *1st* found component, but some driver
789e3303268SKuninori Morimoto 	 * has few components by same of_node/name
790e3303268SKuninori Morimoto 	 * ex)
791e3303268SKuninori Morimoto 	 *	CPU component and generic DMAEngine component
792e3303268SKuninori Morimoto 	 */
793c1e230f0SKuninori Morimoto 	for_each_component(component)
794c1e230f0SKuninori Morimoto 		if (snd_soc_is_matching_component(dlc, component))
795db2a4165SFrank Mandarino 			return component;
79602a06d30SBarry Song 
79702a06d30SBarry Song 	return NULL;
79812023a9aSMisael Lopez Cruz }
79912023a9aSMisael Lopez Cruz 
800fbb88b5cSMengdong Lin /**
801fbb88b5cSMengdong Lin  * snd_soc_find_dai - Find a registered DAI
802fbb88b5cSMengdong Lin  *
8034958471bSJeffy Chen  * @dlc: name of the DAI or the DAI driver and optional component info to match
804fbb88b5cSMengdong Lin  *
805ad61dd30SStephen Boyd  * This function will search all registered components and their DAIs to
806fbb88b5cSMengdong Lin  * find the DAI of the same name. The component's of_node and name
807fbb88b5cSMengdong Lin  * should also match if being specified.
808fbb88b5cSMengdong Lin  *
809fbb88b5cSMengdong Lin  * Return: pointer of DAI, or NULL if not found.
810fbb88b5cSMengdong Lin  */
811305e9020SMengdong Lin struct snd_soc_dai *snd_soc_find_dai(
81214621c7eSLars-Peter Clausen 	const struct snd_soc_dai_link_component *dlc)
81312023a9aSMisael Lopez Cruz {
81414621c7eSLars-Peter Clausen 	struct snd_soc_component *component;
81514621c7eSLars-Peter Clausen 	struct snd_soc_dai *dai;
81612023a9aSMisael Lopez Cruz 
81734e81ab4SLars-Peter Clausen 	lockdep_assert_held(&client_mutex);
81834e81ab4SLars-Peter Clausen 
81914621c7eSLars-Peter Clausen 	/* Find CPU DAI from registered DAIs */
820368dee94SKuninori Morimoto 	for_each_component(component) {
821be6ac0a9SKuninori Morimoto 		if (!snd_soc_is_matching_component(dlc, component))
82212023a9aSMisael Lopez Cruz 			continue;
82315a0c645SKuninori Morimoto 		for_each_component_dais(component, dai) {
8244958471bSJeffy Chen 			if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
8256a6dafdaSJeffy Chen 			    && (!dai->driver->name
8266a6dafdaSJeffy Chen 				|| strcmp(dai->driver->name, dlc->dai_name)))
82714621c7eSLars-Peter Clausen 				continue;
82812023a9aSMisael Lopez Cruz 
82914621c7eSLars-Peter Clausen 			return dai;
83012023a9aSMisael Lopez Cruz 		}
83112023a9aSMisael Lopez Cruz 	}
83212023a9aSMisael Lopez Cruz 
83312023a9aSMisael Lopez Cruz 	return NULL;
83412023a9aSMisael Lopez Cruz }
835305e9020SMengdong Lin EXPORT_SYMBOL_GPL(snd_soc_find_dai);
83612023a9aSMisael Lopez Cruz 
837c1c277b2SKuninori Morimoto struct snd_soc_dai *snd_soc_find_dai_with_mutex(
838c1c277b2SKuninori Morimoto 	const struct snd_soc_dai_link_component *dlc)
839c1c277b2SKuninori Morimoto {
840c1c277b2SKuninori Morimoto 	struct snd_soc_dai *dai;
841c1c277b2SKuninori Morimoto 
842c1c277b2SKuninori Morimoto 	mutex_lock(&client_mutex);
843c1c277b2SKuninori Morimoto 	dai = snd_soc_find_dai(dlc);
844c1c277b2SKuninori Morimoto 	mutex_unlock(&client_mutex);
845c1c277b2SKuninori Morimoto 
846c1c277b2SKuninori Morimoto 	return dai;
847c1c277b2SKuninori Morimoto }
848c1c277b2SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_find_dai_with_mutex);
849c1c277b2SKuninori Morimoto 
850bfce78a5SKuninori Morimoto static int soc_dai_link_sanity_check(struct snd_soc_card *card,
85136794902SKuninori Morimoto 				     struct snd_soc_dai_link *link)
85236794902SKuninori Morimoto {
85336794902SKuninori Morimoto 	int i;
85476afa643SShreyas NC 	struct snd_soc_dai_link_component *cpu, *codec, *platform;
85536794902SKuninori Morimoto 
85636794902SKuninori Morimoto 	for_each_link_codecs(link, i, codec) {
85736794902SKuninori Morimoto 		/*
85836794902SKuninori Morimoto 		 * Codec must be specified by 1 of name or OF node,
85936794902SKuninori Morimoto 		 * not both or neither.
86036794902SKuninori Morimoto 		 */
86136794902SKuninori Morimoto 		if (!!codec->name == !!codec->of_node) {
86236794902SKuninori Morimoto 			dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
86336794902SKuninori Morimoto 				link->name);
86436794902SKuninori Morimoto 			return -EINVAL;
86536794902SKuninori Morimoto 		}
86636794902SKuninori Morimoto 
86736794902SKuninori Morimoto 		/* Codec DAI name must be specified */
86836794902SKuninori Morimoto 		if (!codec->dai_name) {
86936794902SKuninori Morimoto 			dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
87036794902SKuninori Morimoto 				link->name);
87136794902SKuninori Morimoto 			return -EINVAL;
87236794902SKuninori Morimoto 		}
87336794902SKuninori Morimoto 
87436794902SKuninori Morimoto 		/*
87536794902SKuninori Morimoto 		 * Defer card registration if codec component is not added to
87636794902SKuninori Morimoto 		 * component list.
87736794902SKuninori Morimoto 		 */
878b2acc24cSRanjani Sridharan 		if (!soc_find_component(codec)) {
879b2acc24cSRanjani Sridharan 			dev_dbg(card->dev,
880b2acc24cSRanjani Sridharan 				"ASoC: codec component %s not found for link %s\n",
881b2acc24cSRanjani Sridharan 				codec->name, link->name);
88236794902SKuninori Morimoto 			return -EPROBE_DEFER;
88336794902SKuninori Morimoto 		}
884b2acc24cSRanjani Sridharan 	}
88536794902SKuninori Morimoto 
88636794902SKuninori Morimoto 	for_each_link_platforms(link, i, platform) {
88736794902SKuninori Morimoto 		/*
88836794902SKuninori Morimoto 		 * Platform may be specified by either name or OF node, but it
88936794902SKuninori Morimoto 		 * can be left unspecified, then no components will be inserted
89036794902SKuninori Morimoto 		 * in the rtdcom list
89136794902SKuninori Morimoto 		 */
89236794902SKuninori Morimoto 		if (!!platform->name == !!platform->of_node) {
89336794902SKuninori Morimoto 			dev_err(card->dev,
89436794902SKuninori Morimoto 				"ASoC: Neither/both platform name/of_node are set for %s\n",
89536794902SKuninori Morimoto 				link->name);
89636794902SKuninori Morimoto 			return -EINVAL;
89736794902SKuninori Morimoto 		}
89836794902SKuninori Morimoto 
89936794902SKuninori Morimoto 		/*
90036794902SKuninori Morimoto 		 * Defer card registration if platform component is not added to
90136794902SKuninori Morimoto 		 * component list.
90236794902SKuninori Morimoto 		 */
903b2acc24cSRanjani Sridharan 		if (!soc_find_component(platform)) {
904b2acc24cSRanjani Sridharan 			dev_dbg(card->dev,
905b2acc24cSRanjani Sridharan 				"ASoC: platform component %s not found for link %s\n",
906b2acc24cSRanjani Sridharan 				platform->name, link->name);
90736794902SKuninori Morimoto 			return -EPROBE_DEFER;
90836794902SKuninori Morimoto 		}
909b2acc24cSRanjani Sridharan 	}
91036794902SKuninori Morimoto 
91176afa643SShreyas NC 	for_each_link_cpus(link, i, cpu) {
91236794902SKuninori Morimoto 		/*
91336794902SKuninori Morimoto 		 * CPU device may be specified by either name or OF node, but
91436794902SKuninori Morimoto 		 * can be left unspecified, and will be matched based on DAI
91536794902SKuninori Morimoto 		 * name alone..
91636794902SKuninori Morimoto 		 */
91776afa643SShreyas NC 		if (cpu->name && cpu->of_node) {
91836794902SKuninori Morimoto 			dev_err(card->dev,
91936794902SKuninori Morimoto 				"ASoC: Neither/both cpu name/of_node are set for %s\n",
92036794902SKuninori Morimoto 				link->name);
92136794902SKuninori Morimoto 			return -EINVAL;
92236794902SKuninori Morimoto 		}
92336794902SKuninori Morimoto 
92436794902SKuninori Morimoto 		/*
925cd3c5ad7SKuninori Morimoto 		 * Defer card registration if cpu dai component is not added to
92636794902SKuninori Morimoto 		 * component list.
92736794902SKuninori Morimoto 		 */
92876afa643SShreyas NC 		if ((cpu->of_node || cpu->name) &&
929b2acc24cSRanjani Sridharan 		    !soc_find_component(cpu)) {
930b2acc24cSRanjani Sridharan 			dev_dbg(card->dev,
931b2acc24cSRanjani Sridharan 				"ASoC: cpu component %s not found for link %s\n",
932b2acc24cSRanjani Sridharan 				cpu->name, link->name);
93336794902SKuninori Morimoto 			return -EPROBE_DEFER;
934b2acc24cSRanjani Sridharan 		}
93536794902SKuninori Morimoto 
93636794902SKuninori Morimoto 		/*
93736794902SKuninori Morimoto 		 * At least one of CPU DAI name or CPU device name/node must be
93836794902SKuninori Morimoto 		 * specified
93936794902SKuninori Morimoto 		 */
94076afa643SShreyas NC 		if (!cpu->dai_name &&
94176afa643SShreyas NC 		    !(cpu->name || cpu->of_node)) {
94236794902SKuninori Morimoto 			dev_err(card->dev,
94336794902SKuninori Morimoto 				"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
94436794902SKuninori Morimoto 				link->name);
94536794902SKuninori Morimoto 			return -EINVAL;
94636794902SKuninori Morimoto 		}
94776afa643SShreyas NC 	}
94836794902SKuninori Morimoto 
94936794902SKuninori Morimoto 	return 0;
95036794902SKuninori Morimoto }
95136794902SKuninori Morimoto 
952da704f26SKuninori Morimoto /**
95350cd9b53SKuninori Morimoto  * snd_soc_remove_pcm_runtime - Remove a pcm_runtime from card
95450cd9b53SKuninori Morimoto  * @card: The ASoC card to which the pcm_runtime has
95550cd9b53SKuninori Morimoto  * @rtd: The pcm_runtime to remove
956da704f26SKuninori Morimoto  *
95750cd9b53SKuninori Morimoto  * This function removes a pcm_runtime from the ASoC card.
958da704f26SKuninori Morimoto  */
95950cd9b53SKuninori Morimoto void snd_soc_remove_pcm_runtime(struct snd_soc_card *card,
96050cd9b53SKuninori Morimoto 				struct snd_soc_pcm_runtime *rtd)
961bc7a9091SKuninori Morimoto {
962da704f26SKuninori Morimoto 	lockdep_assert_held(&client_mutex);
963da704f26SKuninori Morimoto 
96421a00fb3SPierre-Louis Bossart 	/* release machine specific resources */
96521a00fb3SPierre-Louis Bossart 	snd_soc_link_exit(rtd);
96621a00fb3SPierre-Louis Bossart 
967da704f26SKuninori Morimoto 	/*
968da704f26SKuninori Morimoto 	 * Notify the machine driver for extra destruction
969da704f26SKuninori Morimoto 	 */
970fcbbcc32SKuninori Morimoto 	snd_soc_card_remove_dai_link(card, rtd->dai_link);
971da704f26SKuninori Morimoto 
972bc7a9091SKuninori Morimoto 	soc_free_pcm_runtime(rtd);
973bc7a9091SKuninori Morimoto }
97450cd9b53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_remove_pcm_runtime);
975bc7a9091SKuninori Morimoto 
97663dc47daSKuninori Morimoto /**
9770c048004SKuninori Morimoto  * snd_soc_add_pcm_runtime - Add a pcm_runtime dynamically via dai_link
9780c048004SKuninori Morimoto  * @card: The ASoC card to which the pcm_runtime is added
9790c048004SKuninori Morimoto  * @dai_link: The DAI link to find pcm_runtime
98063dc47daSKuninori Morimoto  *
9810c048004SKuninori Morimoto  * This function adds a pcm_runtime ASoC card by using dai_link.
98263dc47daSKuninori Morimoto  *
9830c048004SKuninori Morimoto  * Note: Topology can use this API to add pcm_runtime when probing the
98463dc47daSKuninori Morimoto  * topology component. And machine drivers can still define static
98563dc47daSKuninori Morimoto  * DAI links in dai_link array.
98663dc47daSKuninori Morimoto  */
9870c048004SKuninori Morimoto int snd_soc_add_pcm_runtime(struct snd_soc_card *card,
9886f2f1ff0SMengdong Lin 			    struct snd_soc_dai_link *dai_link)
989db2a4165SFrank Mandarino {
9901a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
99176afa643SShreyas NC 	struct snd_soc_dai_link_component *codec, *platform, *cpu;
99290be711eSKuninori Morimoto 	struct snd_soc_component *component;
993bfce78a5SKuninori Morimoto 	int i, ret;
994db2a4165SFrank Mandarino 
99563dc47daSKuninori Morimoto 	lockdep_assert_held(&client_mutex);
99663dc47daSKuninori Morimoto 
99763dc47daSKuninori Morimoto 	/*
99863dc47daSKuninori Morimoto 	 * Notify the machine driver for extra initialization
99963dc47daSKuninori Morimoto 	 */
1000cbc7a6b5SKuninori Morimoto 	ret = snd_soc_card_add_dai_link(card, dai_link);
1001cbc7a6b5SKuninori Morimoto 	if (ret < 0)
1002cbc7a6b5SKuninori Morimoto 		return ret;
100363dc47daSKuninori Morimoto 
1004a655de80SLiam Girdwood 	if (dai_link->ignore)
1005a655de80SLiam Girdwood 		return 0;
1006a655de80SLiam Girdwood 
10076f2f1ff0SMengdong Lin 	dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
10086308419aSMark Brown 
1009bfce78a5SKuninori Morimoto 	ret = soc_dai_link_sanity_check(card, dai_link);
1010bfce78a5SKuninori Morimoto 	if (ret < 0)
1011bfce78a5SKuninori Morimoto 		return ret;
1012bfce78a5SKuninori Morimoto 
1013513cb311SSudip Mukherjee 	rtd = soc_new_pcm_runtime(card, dai_link);
1014513cb311SSudip Mukherjee 	if (!rtd)
1015513cb311SSudip Mukherjee 		return -ENOMEM;
1016513cb311SSudip Mukherjee 
101776afa643SShreyas NC 	for_each_link_cpus(dai_link, i, cpu) {
1018c2233a26SKuninori Morimoto 		asoc_rtd_to_cpu(rtd, i) = snd_soc_find_dai(cpu);
1019c2233a26SKuninori Morimoto 		if (!asoc_rtd_to_cpu(rtd, i)) {
10206b490879SMartin Hundebøll 			dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
102176afa643SShreyas NC 				 cpu->dai_name);
10221a497983SMengdong Lin 			goto _err_defer;
1023c5af3a2eSMark Brown 		}
1024c2233a26SKuninori Morimoto 		snd_soc_rtd_add_component(rtd, asoc_rtd_to_cpu(rtd, i)->component);
102576afa643SShreyas NC 	}
102676afa643SShreyas NC 
102788bd870fSBenoit Cousson 	/* Find CODEC from registered CODECs */
102834614739SJerome Brunet 	for_each_link_codecs(dai_link, i, codec) {
1029c2233a26SKuninori Morimoto 		asoc_rtd_to_codec(rtd, i) = snd_soc_find_dai(codec);
1030c2233a26SKuninori Morimoto 		if (!asoc_rtd_to_codec(rtd, i)) {
10317c7e2d6aSStefan Agner 			dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
103234614739SJerome Brunet 				 codec->dai_name);
10331a497983SMengdong Lin 			goto _err_defer;
103412023a9aSMisael Lopez Cruz 		}
103534614739SJerome Brunet 
1036c2233a26SKuninori Morimoto 		snd_soc_rtd_add_component(rtd, asoc_rtd_to_codec(rtd, i)->component);
103788bd870fSBenoit Cousson 	}
103888bd870fSBenoit Cousson 
1039e2b30edfSKuninori Morimoto 	/* Find PLATFORM from registered PLATFORMs */
104034614739SJerome Brunet 	for_each_link_platforms(dai_link, i, platform) {
1041368dee94SKuninori Morimoto 		for_each_component(component) {
104234614739SJerome Brunet 			if (!snd_soc_is_matching_component(platform, component))
104390be711eSKuninori Morimoto 				continue;
104490be711eSKuninori Morimoto 
104512b05232SKuninori Morimoto 			snd_soc_rtd_add_component(rtd, component);
104690be711eSKuninori Morimoto 		}
104734614739SJerome Brunet 	}
104890be711eSKuninori Morimoto 
1049b19e6e7bSMark Brown 	return 0;
10501a497983SMengdong Lin 
10511a497983SMengdong Lin _err_defer:
105250cd9b53SKuninori Morimoto 	snd_soc_remove_pcm_runtime(card, rtd);
10531a497983SMengdong Lin 	return -EPROBE_DEFER;
10546b05eda6SMark Brown }
10550c048004SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_pcm_runtime);
10566b05eda6SMark Brown 
1057ba9e82a1SKuninori Morimoto static void snd_soc_runtime_get_dai_fmt(struct snd_soc_pcm_runtime *rtd)
1058ba9e82a1SKuninori Morimoto {
1059ba9e82a1SKuninori Morimoto 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1060ba9e82a1SKuninori Morimoto 	struct snd_soc_dai *dai, *not_used;
1061ba9e82a1SKuninori Morimoto 	struct device *dev = rtd->dev;
1062ba9e82a1SKuninori Morimoto 	u64 pos, possible_fmt;
1063ba9e82a1SKuninori Morimoto 	unsigned int mask = 0, dai_fmt = 0;
1064ba9e82a1SKuninori Morimoto 	int i, j, priority, pri, until;
1065ba9e82a1SKuninori Morimoto 
1066ba9e82a1SKuninori Morimoto 	/*
1067ba9e82a1SKuninori Morimoto 	 * Get selectable format from each DAIs.
1068ba9e82a1SKuninori Morimoto 	 *
1069ba9e82a1SKuninori Morimoto 	 ****************************
1070ba9e82a1SKuninori Morimoto 	 *            NOTE
1071ba9e82a1SKuninori Morimoto 	 * Using .auto_selectable_formats is not mandatory,
1072ba9e82a1SKuninori Morimoto 	 * we can select format manually from Sound Card.
1073ba9e82a1SKuninori Morimoto 	 * When use it, driver should list well tested format only.
1074ba9e82a1SKuninori Morimoto 	 ****************************
1075ba9e82a1SKuninori Morimoto 	 *
1076ba9e82a1SKuninori Morimoto 	 * ex)
1077ba9e82a1SKuninori Morimoto 	 *	auto_selectable_formats (= SND_SOC_POSSIBLE_xxx)
1078ba9e82a1SKuninori Morimoto 	 *		 (A)	 (B)	 (C)
1079ba9e82a1SKuninori Morimoto 	 *	DAI0_: { 0x000F, 0x00F0, 0x0F00 };
1080ba9e82a1SKuninori Morimoto 	 *	DAI1 : { 0xF000, 0x0F00 };
1081ba9e82a1SKuninori Morimoto 	 *		 (X)	 (Y)
1082ba9e82a1SKuninori Morimoto 	 *
1083ba9e82a1SKuninori Morimoto 	 * "until" will be 3 in this case (MAX array size from DAI0 and DAI1)
1084ba9e82a1SKuninori Morimoto 	 * Here is dev_dbg() message and comments
1085ba9e82a1SKuninori Morimoto 	 *
1086ba9e82a1SKuninori Morimoto 	 * priority = 1
1087ba9e82a1SKuninori Morimoto 	 * DAI0: (pri, fmt) = (1, 000000000000000F) // 1st check (A) DAI1 is not selected
1088ba9e82a1SKuninori Morimoto 	 * DAI1: (pri, fmt) = (0, 0000000000000000) //               Necessary Waste
1089ba9e82a1SKuninori Morimoto 	 * DAI0: (pri, fmt) = (1, 000000000000000F) // 2nd check (A)
1090ba9e82a1SKuninori Morimoto 	 * DAI1: (pri, fmt) = (1, 000000000000F000) //           (X)
1091ba9e82a1SKuninori Morimoto 	 * priority = 2
1092ba9e82a1SKuninori Morimoto 	 * DAI0: (pri, fmt) = (2, 00000000000000FF) // 3rd check (A) + (B)
1093ba9e82a1SKuninori Morimoto 	 * DAI1: (pri, fmt) = (1, 000000000000F000) //           (X)
1094ba9e82a1SKuninori Morimoto 	 * DAI0: (pri, fmt) = (2, 00000000000000FF) // 4th check (A) + (B)
1095ba9e82a1SKuninori Morimoto 	 * DAI1: (pri, fmt) = (2, 000000000000FF00) //           (X) + (Y)
1096ba9e82a1SKuninori Morimoto 	 * priority = 3
1097ba9e82a1SKuninori Morimoto 	 * DAI0: (pri, fmt) = (3, 0000000000000FFF) // 5th check (A) + (B) + (C)
1098ba9e82a1SKuninori Morimoto 	 * DAI1: (pri, fmt) = (2, 000000000000FF00) //           (X) + (Y)
1099ba9e82a1SKuninori Morimoto 	 * found auto selected format: 0000000000000F00
1100ba9e82a1SKuninori Morimoto 	 */
1101ba9e82a1SKuninori Morimoto 	until = snd_soc_dai_get_fmt_max_priority(rtd);
1102ba9e82a1SKuninori Morimoto 	for (priority = 1; priority <= until; priority++) {
1103ba9e82a1SKuninori Morimoto 
1104ba9e82a1SKuninori Morimoto 		dev_dbg(dev, "priority = %d\n", priority);
1105ba9e82a1SKuninori Morimoto 		for_each_rtd_dais(rtd, j, not_used) {
1106ba9e82a1SKuninori Morimoto 
1107ba9e82a1SKuninori Morimoto 			possible_fmt = ULLONG_MAX;
1108ba9e82a1SKuninori Morimoto 			for_each_rtd_dais(rtd, i, dai) {
1109ba9e82a1SKuninori Morimoto 				u64 fmt = 0;
1110ba9e82a1SKuninori Morimoto 
1111ba9e82a1SKuninori Morimoto 				pri = (j >= i) ? priority : priority - 1;
1112ba9e82a1SKuninori Morimoto 				fmt = snd_soc_dai_get_fmt(dai, pri);
1113ba9e82a1SKuninori Morimoto 				dev_dbg(dev, "%s: (pri, fmt) = (%d, %016llX)\n", dai->name, pri, fmt);
1114ba9e82a1SKuninori Morimoto 				possible_fmt &= fmt;
1115ba9e82a1SKuninori Morimoto 			}
1116ba9e82a1SKuninori Morimoto 			if (possible_fmt)
1117ba9e82a1SKuninori Morimoto 				goto found;
1118ba9e82a1SKuninori Morimoto 		}
1119ba9e82a1SKuninori Morimoto 	}
1120ba9e82a1SKuninori Morimoto 	/* Not Found */
1121ba9e82a1SKuninori Morimoto 	return;
1122ba9e82a1SKuninori Morimoto found:
1123ba9e82a1SKuninori Morimoto 	dev_dbg(dev, "found auto selected format: %016llX\n", possible_fmt);
1124ba9e82a1SKuninori Morimoto 
1125ba9e82a1SKuninori Morimoto 	/*
1126ba9e82a1SKuninori Morimoto 	 * convert POSSIBLE_DAIFMT to DAIFMT
1127ba9e82a1SKuninori Morimoto 	 *
1128ba9e82a1SKuninori Morimoto 	 * Some basic/default settings on each is defined as 0.
1129ba9e82a1SKuninori Morimoto 	 * see
1130ba9e82a1SKuninori Morimoto 	 *	SND_SOC_DAIFMT_NB_NF
1131ba9e82a1SKuninori Morimoto 	 *	SND_SOC_DAIFMT_GATED
1132ba9e82a1SKuninori Morimoto 	 *
1133ba9e82a1SKuninori Morimoto 	 * SND_SOC_DAIFMT_xxx_MASK can't notice it if Sound Card specify
1134ba9e82a1SKuninori Morimoto 	 * these value, and will be overwrite to auto selected value.
1135ba9e82a1SKuninori Morimoto 	 *
1136ba9e82a1SKuninori Morimoto 	 * To avoid such issue, loop from 63 to 0 here.
1137ba9e82a1SKuninori Morimoto 	 * Small number of SND_SOC_POSSIBLE_xxx will be Hi priority.
1138ba9e82a1SKuninori Morimoto 	 * Basic/Default settings of each part and aboves are defined
1139ba9e82a1SKuninori Morimoto 	 * as Hi priority (= small number) of SND_SOC_POSSIBLE_xxx.
1140ba9e82a1SKuninori Morimoto 	 */
1141ba9e82a1SKuninori Morimoto 	for (i = 63; i >= 0; i--) {
1142ba9e82a1SKuninori Morimoto 		pos = 1ULL << i;
1143ba9e82a1SKuninori Morimoto 		switch (possible_fmt & pos) {
1144ba9e82a1SKuninori Morimoto 		/*
1145ba9e82a1SKuninori Morimoto 		 * for format
1146ba9e82a1SKuninori Morimoto 		 */
1147ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_I2S:
1148ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_RIGHT_J:
1149ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_LEFT_J:
1150ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_DSP_A:
1151ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_DSP_B:
1152ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_AC97:
1153ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_PDM:
1154ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_FORMAT_MASK) | i;
1155ba9e82a1SKuninori Morimoto 			break;
1156ba9e82a1SKuninori Morimoto 		/*
1157ba9e82a1SKuninori Morimoto 		 * for clock
1158ba9e82a1SKuninori Morimoto 		 */
1159ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_CONT:
1160ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_MASK) | SND_SOC_DAIFMT_CONT;
1161ba9e82a1SKuninori Morimoto 			break;
1162ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_GATED:
1163ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_MASK) | SND_SOC_DAIFMT_GATED;
1164ba9e82a1SKuninori Morimoto 			break;
1165ba9e82a1SKuninori Morimoto 		/*
1166ba9e82a1SKuninori Morimoto 		 * for clock invert
1167ba9e82a1SKuninori Morimoto 		 */
1168ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_NB_NF:
1169ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_NB_NF;
1170ba9e82a1SKuninori Morimoto 			break;
1171ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_NB_IF:
1172ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_NB_IF;
1173ba9e82a1SKuninori Morimoto 			break;
1174ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_IB_NF:
1175ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_IB_NF;
1176ba9e82a1SKuninori Morimoto 			break;
1177ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_IB_IF:
1178ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_IB_IF;
1179ba9e82a1SKuninori Morimoto 			break;
1180ba9e82a1SKuninori Morimoto 		/*
1181ba9e82a1SKuninori Morimoto 		 * for clock provider / consumer
1182ba9e82a1SKuninori Morimoto 		 */
1183ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_CBP_CFP:
1184ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBP_CFP;
1185ba9e82a1SKuninori Morimoto 			break;
1186ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_CBC_CFP:
1187ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBC_CFP;
1188ba9e82a1SKuninori Morimoto 			break;
1189ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_CBP_CFC:
1190ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBP_CFC;
1191ba9e82a1SKuninori Morimoto 			break;
1192ba9e82a1SKuninori Morimoto 		case SND_SOC_POSSIBLE_DAIFMT_CBC_CFC:
1193ba9e82a1SKuninori Morimoto 			dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBC_CFC;
1194ba9e82a1SKuninori Morimoto 			break;
1195ba9e82a1SKuninori Morimoto 		}
1196ba9e82a1SKuninori Morimoto 	}
1197ba9e82a1SKuninori Morimoto 
1198ba9e82a1SKuninori Morimoto 	/*
1199ba9e82a1SKuninori Morimoto 	 * Some driver might have very complex limitation.
1200ba9e82a1SKuninori Morimoto 	 * In such case, user want to auto-select non-limitation part,
1201ba9e82a1SKuninori Morimoto 	 * and want to manually specify complex part.
1202ba9e82a1SKuninori Morimoto 	 *
1203ba9e82a1SKuninori Morimoto 	 * Or for example, if both CPU and Codec can be clock provider,
1204ba9e82a1SKuninori Morimoto 	 * but because of its quality, user want to specify it manually.
1205ba9e82a1SKuninori Morimoto 	 *
1206ba9e82a1SKuninori Morimoto 	 * Use manually specified settings if sound card did.
1207ba9e82a1SKuninori Morimoto 	 */
1208ba9e82a1SKuninori Morimoto 	if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK))
1209ba9e82a1SKuninori Morimoto 		mask |= SND_SOC_DAIFMT_FORMAT_MASK;
1210ba9e82a1SKuninori Morimoto 	if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_CLOCK_MASK))
1211ba9e82a1SKuninori Morimoto 		mask |= SND_SOC_DAIFMT_CLOCK_MASK;
1212ba9e82a1SKuninori Morimoto 	if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_INV_MASK))
1213ba9e82a1SKuninori Morimoto 		mask |= SND_SOC_DAIFMT_INV_MASK;
1214640eac4cSKuninori Morimoto 	if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK))
1215640eac4cSKuninori Morimoto 		mask |= SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK;
1216ba9e82a1SKuninori Morimoto 
1217ba9e82a1SKuninori Morimoto 	dai_link->dai_fmt |= (dai_fmt & mask);
1218ba9e82a1SKuninori Morimoto }
1219ba9e82a1SKuninori Morimoto 
12204d1a98b5SKuninori Morimoto /**
12214d1a98b5SKuninori Morimoto  * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
12224d1a98b5SKuninori Morimoto  * @rtd: The runtime for which the DAI link format should be changed
12234d1a98b5SKuninori Morimoto  * @dai_fmt: The new DAI link format
12244d1a98b5SKuninori Morimoto  *
12254d1a98b5SKuninori Morimoto  * This function updates the DAI link format for all DAIs connected to the DAI
12264d1a98b5SKuninori Morimoto  * link for the specified runtime.
12274d1a98b5SKuninori Morimoto  *
12284d1a98b5SKuninori Morimoto  * Note: For setups with a static format set the dai_fmt field in the
12294d1a98b5SKuninori Morimoto  * corresponding snd_dai_link struct instead of using this function.
12304d1a98b5SKuninori Morimoto  *
12314d1a98b5SKuninori Morimoto  * Returns 0 on success, otherwise a negative error code.
12324d1a98b5SKuninori Morimoto  */
12334d1a98b5SKuninori Morimoto int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
12344d1a98b5SKuninori Morimoto 				unsigned int dai_fmt)
12354d1a98b5SKuninori Morimoto {
12364d1a98b5SKuninori Morimoto 	struct snd_soc_dai *cpu_dai;
12374d1a98b5SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
12384d1a98b5SKuninori Morimoto 	unsigned int inv_dai_fmt;
12394d1a98b5SKuninori Morimoto 	unsigned int i;
12404d1a98b5SKuninori Morimoto 	int ret;
12414d1a98b5SKuninori Morimoto 
12424d1a98b5SKuninori Morimoto 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
12434d1a98b5SKuninori Morimoto 		ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
12444d1a98b5SKuninori Morimoto 		if (ret != 0 && ret != -ENOTSUPP)
12454d1a98b5SKuninori Morimoto 			return ret;
12464d1a98b5SKuninori Morimoto 	}
12474d1a98b5SKuninori Morimoto 
12484d1a98b5SKuninori Morimoto 	/*
12494d1a98b5SKuninori Morimoto 	 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
12504d1a98b5SKuninori Morimoto 	 * the component which has non_legacy_dai_naming is Codec
12514d1a98b5SKuninori Morimoto 	 */
1252b44a67f8SKuninori Morimoto 	inv_dai_fmt = snd_soc_daifmt_clock_provider_fliped(dai_fmt);
1253b44a67f8SKuninori Morimoto 
12544d1a98b5SKuninori Morimoto 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
12554d1a98b5SKuninori Morimoto 		unsigned int fmt = dai_fmt;
12564d1a98b5SKuninori Morimoto 
12574d1a98b5SKuninori Morimoto 		if (cpu_dai->component->driver->non_legacy_dai_naming)
12584d1a98b5SKuninori Morimoto 			fmt = inv_dai_fmt;
12594d1a98b5SKuninori Morimoto 
12604d1a98b5SKuninori Morimoto 		ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
12614d1a98b5SKuninori Morimoto 		if (ret != 0 && ret != -ENOTSUPP)
12624d1a98b5SKuninori Morimoto 			return ret;
12634d1a98b5SKuninori Morimoto 	}
12644d1a98b5SKuninori Morimoto 
12654d1a98b5SKuninori Morimoto 	return 0;
12664d1a98b5SKuninori Morimoto }
12674d1a98b5SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
12684d1a98b5SKuninori Morimoto 
1269eaffeefbSKuninori Morimoto static int soc_init_pcm_runtime(struct snd_soc_card *card,
127046496acbSKuninori Morimoto 				struct snd_soc_pcm_runtime *rtd)
127146496acbSKuninori Morimoto {
127246496acbSKuninori Morimoto 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
1273c2233a26SKuninori Morimoto 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
127446496acbSKuninori Morimoto 	struct snd_soc_component *component;
1275613fb500SKuninori Morimoto 	int ret, num, i;
127646496acbSKuninori Morimoto 
127746496acbSKuninori Morimoto 	/* set default power off timeout */
127846496acbSKuninori Morimoto 	rtd->pmdown_time = pmdown_time;
127946496acbSKuninori Morimoto 
128046496acbSKuninori Morimoto 	/* do machine specific initialization */
128102e75636SKuninori Morimoto 	ret = snd_soc_link_init(rtd);
128202e75636SKuninori Morimoto 	if (ret < 0)
128346496acbSKuninori Morimoto 		return ret;
128446496acbSKuninori Morimoto 
1285ba9e82a1SKuninori Morimoto 	snd_soc_runtime_get_dai_fmt(rtd);
128646496acbSKuninori Morimoto 	if (dai_link->dai_fmt) {
128746496acbSKuninori Morimoto 		ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
128846496acbSKuninori Morimoto 		if (ret)
128946496acbSKuninori Morimoto 			return ret;
129046496acbSKuninori Morimoto 	}
129146496acbSKuninori Morimoto 
129246496acbSKuninori Morimoto 	/* add DPCM sysfs entries */
129346496acbSKuninori Morimoto 	soc_dpcm_debugfs_add(rtd);
129446496acbSKuninori Morimoto 
129546496acbSKuninori Morimoto 	num = rtd->num;
129646496acbSKuninori Morimoto 
129746496acbSKuninori Morimoto 	/*
129846496acbSKuninori Morimoto 	 * most drivers will register their PCMs using DAI link ordering but
129946496acbSKuninori Morimoto 	 * topology based drivers can use the DAI link id field to set PCM
130046496acbSKuninori Morimoto 	 * device number and then use rtd + a base offset of the BEs.
130146496acbSKuninori Morimoto 	 */
1302613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
130346496acbSKuninori Morimoto 		if (!component->driver->use_dai_pcm_id)
130446496acbSKuninori Morimoto 			continue;
130546496acbSKuninori Morimoto 
130646496acbSKuninori Morimoto 		if (rtd->dai_link->no_pcm)
130746496acbSKuninori Morimoto 			num += component->driver->be_pcm_base;
130846496acbSKuninori Morimoto 		else
130946496acbSKuninori Morimoto 			num = rtd->dai_link->id;
131046496acbSKuninori Morimoto 	}
131146496acbSKuninori Morimoto 
131246496acbSKuninori Morimoto 	/* create compress_device if possible */
131346496acbSKuninori Morimoto 	ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
131462462e01SKuninori Morimoto 	if (ret != -ENOTSUPP)
131546496acbSKuninori Morimoto 		return ret;
131646496acbSKuninori Morimoto 
131746496acbSKuninori Morimoto 	/* create the pcm */
131846496acbSKuninori Morimoto 	ret = soc_new_pcm(rtd, num);
131946496acbSKuninori Morimoto 	if (ret < 0) {
132046496acbSKuninori Morimoto 		dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
132146496acbSKuninori Morimoto 			dai_link->stream_name, ret);
132246496acbSKuninori Morimoto 		return ret;
132346496acbSKuninori Morimoto 	}
1324d1eb6d11SKuninori Morimoto 
13250b73ba55SKuninori Morimoto 	return snd_soc_pcm_dai_new(rtd);
132646496acbSKuninori Morimoto }
132746496acbSKuninori Morimoto 
1328ffd60fbaSKuninori Morimoto static void soc_set_name_prefix(struct snd_soc_card *card,
1329ffd60fbaSKuninori Morimoto 				struct snd_soc_component *component)
1330ffd60fbaSKuninori Morimoto {
1331aec3ff99SKuninori Morimoto 	struct device_node *of_node = soc_component_to_node(component);
133229d9fc7aSKuninori Morimoto 	const char *str;
133329d9fc7aSKuninori Morimoto 	int ret, i;
1334ffd60fbaSKuninori Morimoto 
13354702f991SKuninori Morimoto 	for (i = 0; i < card->num_configs; i++) {
1336ffd60fbaSKuninori Morimoto 		struct snd_soc_codec_conf *map = &card->codec_conf[i];
1337ffd60fbaSKuninori Morimoto 
13383256ef98SSameer Pujar 		if (snd_soc_is_matching_component(&map->dlc, component) &&
13393256ef98SSameer Pujar 		    map->name_prefix) {
1340ffd60fbaSKuninori Morimoto 			component->name_prefix = map->name_prefix;
1341ffd60fbaSKuninori Morimoto 			return;
1342ffd60fbaSKuninori Morimoto 		}
1343c13493a2SKuninori Morimoto 	}
1344ffd60fbaSKuninori Morimoto 
1345ffd60fbaSKuninori Morimoto 	/*
1346ffd60fbaSKuninori Morimoto 	 * If there is no configuration table or no match in the table,
1347ffd60fbaSKuninori Morimoto 	 * check if a prefix is provided in the node
1348ffd60fbaSKuninori Morimoto 	 */
134929d9fc7aSKuninori Morimoto 	ret = of_property_read_string(of_node, "sound-name-prefix", &str);
135029d9fc7aSKuninori Morimoto 	if (ret < 0)
135129d9fc7aSKuninori Morimoto 		return;
135229d9fc7aSKuninori Morimoto 
135329d9fc7aSKuninori Morimoto 	component->name_prefix = str;
1354ffd60fbaSKuninori Morimoto }
1355ffd60fbaSKuninori Morimoto 
1356c6619b72SKuninori Morimoto static void soc_remove_component(struct snd_soc_component *component,
1357c6619b72SKuninori Morimoto 				 int probed)
135822d14231SKuninori Morimoto {
1359c6619b72SKuninori Morimoto 
1360c6619b72SKuninori Morimoto 	if (!component->card)
1361c6619b72SKuninori Morimoto 		return;
1362c6619b72SKuninori Morimoto 
1363c6619b72SKuninori Morimoto 	if (probed)
1364c6619b72SKuninori Morimoto 		snd_soc_component_remove(component);
1365c6619b72SKuninori Morimoto 
136604f770d9SKuninori Morimoto 	/* For framework level robustness */
13673bb936f5SAmadeusz Sławiński 	snd_soc_component_set_jack(component, NULL, NULL);
136804f770d9SKuninori Morimoto 
13697a5d9815SBard liao 	list_del_init(&component->card_list);
137022d14231SKuninori Morimoto 	snd_soc_dapm_free(snd_soc_component_get_dapm(component));
137122d14231SKuninori Morimoto 	soc_cleanup_component_debugfs(component);
137222d14231SKuninori Morimoto 	component->card = NULL;
13730e36f36bSPierre-Louis Bossart 	snd_soc_component_module_put_when_remove(component);
137422d14231SKuninori Morimoto }
137522d14231SKuninori Morimoto 
1376ffd60fbaSKuninori Morimoto static int soc_probe_component(struct snd_soc_card *card,
1377ffd60fbaSKuninori Morimoto 			       struct snd_soc_component *component)
1378ffd60fbaSKuninori Morimoto {
1379ffd60fbaSKuninori Morimoto 	struct snd_soc_dapm_context *dapm =
1380ffd60fbaSKuninori Morimoto 		snd_soc_component_get_dapm(component);
1381ffd60fbaSKuninori Morimoto 	struct snd_soc_dai *dai;
1382c6619b72SKuninori Morimoto 	int probed = 0;
1383ffd60fbaSKuninori Morimoto 	int ret;
1384ffd60fbaSKuninori Morimoto 
13858f1a1681SKuninori Morimoto 	if (snd_soc_component_is_dummy(component))
1386ffd60fbaSKuninori Morimoto 		return 0;
1387ffd60fbaSKuninori Morimoto 
1388ffd60fbaSKuninori Morimoto 	if (component->card) {
1389ffd60fbaSKuninori Morimoto 		if (component->card != card) {
1390ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1391ffd60fbaSKuninori Morimoto 				"Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1392ffd60fbaSKuninori Morimoto 				card->name, component->card->name);
1393ffd60fbaSKuninori Morimoto 			return -ENODEV;
1394ffd60fbaSKuninori Morimoto 		}
1395ffd60fbaSKuninori Morimoto 		return 0;
1396ffd60fbaSKuninori Morimoto 	}
1397ffd60fbaSKuninori Morimoto 
1398ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_module_get_when_probe(component);
1399ffd60fbaSKuninori Morimoto 	if (ret < 0)
1400ffd60fbaSKuninori Morimoto 		return ret;
1401ffd60fbaSKuninori Morimoto 
1402ffd60fbaSKuninori Morimoto 	component->card = card;
1403ffd60fbaSKuninori Morimoto 	soc_set_name_prefix(card, component);
1404ffd60fbaSKuninori Morimoto 
1405ffd60fbaSKuninori Morimoto 	soc_init_component_debugfs(component);
1406ffd60fbaSKuninori Morimoto 
140795c267ddSKuninori Morimoto 	snd_soc_dapm_init(dapm, card, component);
1408b614beafSKuninori Morimoto 
1409ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_new_controls(dapm,
1410ffd60fbaSKuninori Morimoto 					component->driver->dapm_widgets,
1411ffd60fbaSKuninori Morimoto 					component->driver->num_dapm_widgets);
1412ffd60fbaSKuninori Morimoto 
1413ffd60fbaSKuninori Morimoto 	if (ret != 0) {
1414ffd60fbaSKuninori Morimoto 		dev_err(component->dev,
1415ffd60fbaSKuninori Morimoto 			"Failed to create new controls %d\n", ret);
1416ffd60fbaSKuninori Morimoto 		goto err_probe;
1417ffd60fbaSKuninori Morimoto 	}
1418ffd60fbaSKuninori Morimoto 
1419ffd60fbaSKuninori Morimoto 	for_each_component_dais(component, dai) {
1420ffd60fbaSKuninori Morimoto 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1421ffd60fbaSKuninori Morimoto 		if (ret != 0) {
1422ffd60fbaSKuninori Morimoto 			dev_err(component->dev,
1423ffd60fbaSKuninori Morimoto 				"Failed to create DAI widgets %d\n", ret);
1424ffd60fbaSKuninori Morimoto 			goto err_probe;
1425ffd60fbaSKuninori Morimoto 		}
1426ffd60fbaSKuninori Morimoto 	}
1427ffd60fbaSKuninori Morimoto 
1428ffd60fbaSKuninori Morimoto 	ret = snd_soc_component_probe(component);
142960adbd8fSKuninori Morimoto 	if (ret < 0)
1430ffd60fbaSKuninori Morimoto 		goto err_probe;
143160adbd8fSKuninori Morimoto 
1432ffd60fbaSKuninori Morimoto 	WARN(dapm->idle_bias_off &&
1433ffd60fbaSKuninori Morimoto 	     dapm->bias_level != SND_SOC_BIAS_OFF,
1434ffd60fbaSKuninori Morimoto 	     "codec %s can not start from non-off bias with idle_bias_off==1\n",
1435ffd60fbaSKuninori Morimoto 	     component->name);
1436c6619b72SKuninori Morimoto 	probed = 1;
1437ffd60fbaSKuninori Morimoto 
1438257c4dacSKuninori Morimoto 	/*
1439257c4dacSKuninori Morimoto 	 * machine specific init
1440257c4dacSKuninori Morimoto 	 * see
1441257c4dacSKuninori Morimoto 	 *	snd_soc_component_set_aux()
1442257c4dacSKuninori Morimoto 	 */
1443257c4dacSKuninori Morimoto 	ret = snd_soc_component_init(component);
1444257c4dacSKuninori Morimoto 	if (ret < 0)
1445ffd60fbaSKuninori Morimoto 		goto err_probe;
1446ffd60fbaSKuninori Morimoto 
1447ffd60fbaSKuninori Morimoto 	ret = snd_soc_add_component_controls(component,
1448ffd60fbaSKuninori Morimoto 					     component->driver->controls,
1449ffd60fbaSKuninori Morimoto 					     component->driver->num_controls);
1450ffd60fbaSKuninori Morimoto 	if (ret < 0)
1451ffd60fbaSKuninori Morimoto 		goto err_probe;
1452ffd60fbaSKuninori Morimoto 
1453ffd60fbaSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(dapm,
1454ffd60fbaSKuninori Morimoto 				      component->driver->dapm_routes,
1455ffd60fbaSKuninori Morimoto 				      component->driver->num_dapm_routes);
1456a22ae72bSPierre-Louis Bossart 	if (ret < 0) {
1457a22ae72bSPierre-Louis Bossart 		if (card->disable_route_checks) {
1458a22ae72bSPierre-Louis Bossart 			dev_info(card->dev,
1459a22ae72bSPierre-Louis Bossart 				 "%s: disable_route_checks set, ignoring errors on add_routes\n",
1460a22ae72bSPierre-Louis Bossart 				 __func__);
1461a22ae72bSPierre-Louis Bossart 		} else {
1462a22ae72bSPierre-Louis Bossart 			dev_err(card->dev,
1463a22ae72bSPierre-Louis Bossart 				"%s: snd_soc_dapm_add_routes failed: %d\n",
1464a22ae72bSPierre-Louis Bossart 				__func__, ret);
1465ffd60fbaSKuninori Morimoto 			goto err_probe;
1466a22ae72bSPierre-Louis Bossart 		}
1467a22ae72bSPierre-Louis Bossart 	}
1468ffd60fbaSKuninori Morimoto 
1469ffd60fbaSKuninori Morimoto 	/* see for_each_card_components */
1470ffd60fbaSKuninori Morimoto 	list_add(&component->card_list, &card->component_dev_list);
1471ffd60fbaSKuninori Morimoto 
1472ffd60fbaSKuninori Morimoto err_probe:
1473ffd60fbaSKuninori Morimoto 	if (ret < 0)
1474c6619b72SKuninori Morimoto 		soc_remove_component(component, probed);
1475ffd60fbaSKuninori Morimoto 
1476ffd60fbaSKuninori Morimoto 	return ret;
1477ffd60fbaSKuninori Morimoto }
1478ffd60fbaSKuninori Morimoto 
14794ca47d21SKuninori Morimoto static void soc_remove_link_dais(struct snd_soc_card *card)
1480f0fba2adSLiam Girdwood {
14814ca47d21SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
14824ca47d21SKuninori Morimoto 	int order;
14834ca47d21SKuninori Morimoto 
14844ca47d21SKuninori Morimoto 	for_each_comp_order(order) {
14854ca47d21SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
14867eaa313bSKuninori Morimoto 			/* remove all rtd connected DAIs in good order */
14877eaa313bSKuninori Morimoto 			snd_soc_pcm_dai_remove(rtd, order);
1488f0fba2adSLiam Girdwood 		}
14894ca47d21SKuninori Morimoto 	}
14904ca47d21SKuninori Morimoto }
1491f0fba2adSLiam Girdwood 
1492bc7c16c2SKuninori Morimoto static int soc_probe_link_dais(struct snd_soc_card *card)
1493bc7c16c2SKuninori Morimoto {
1494bc7c16c2SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
149551801aeaSKuninori Morimoto 	int order, ret;
1496bc7c16c2SKuninori Morimoto 
1497bc7c16c2SKuninori Morimoto 	for_each_comp_order(order) {
1498bc7c16c2SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1499bc7c16c2SKuninori Morimoto 
1500bc7c16c2SKuninori Morimoto 			dev_dbg(card->dev,
1501bc7c16c2SKuninori Morimoto 				"ASoC: probe %s dai link %d late %d\n",
1502bc7c16c2SKuninori Morimoto 				card->name, rtd->num, order);
1503bc7c16c2SKuninori Morimoto 
150451801aeaSKuninori Morimoto 			/* probe all rtd connected DAIs in good order */
150551801aeaSKuninori Morimoto 			ret = snd_soc_pcm_dai_probe(rtd, order);
1506bc7c16c2SKuninori Morimoto 			if (ret)
1507bc7c16c2SKuninori Morimoto 				return ret;
1508bc7c16c2SKuninori Morimoto 		}
1509bc7c16c2SKuninori Morimoto 	}
1510bc7c16c2SKuninori Morimoto 
1511bc7c16c2SKuninori Morimoto 	return 0;
1512bc7c16c2SKuninori Morimoto }
1513bc7c16c2SKuninori Morimoto 
1514b006c0c6SKuninori Morimoto static void soc_remove_link_components(struct snd_soc_card *card)
151562ae68faSStephen Warren {
151661aca564SLars-Peter Clausen 	struct snd_soc_component *component;
1517b006c0c6SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
1518613fb500SKuninori Morimoto 	int i, order;
151962ae68faSStephen Warren 
1520b006c0c6SKuninori Morimoto 	for_each_comp_order(order) {
1521b006c0c6SKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1522613fb500SKuninori Morimoto 			for_each_rtd_components(rtd, i, component) {
1523b006c0c6SKuninori Morimoto 				if (component->driver->remove_order != order)
1524b006c0c6SKuninori Morimoto 					continue;
1525b006c0c6SKuninori Morimoto 
1526c6619b72SKuninori Morimoto 				soc_remove_component(component, 1);
152762ae68faSStephen Warren 			}
152862ae68faSStephen Warren 		}
1529b006c0c6SKuninori Morimoto 	}
1530b006c0c6SKuninori Morimoto }
153162ae68faSStephen Warren 
153262f07a6bSKuninori Morimoto static int soc_probe_link_components(struct snd_soc_card *card)
15336fb03550SKuninori Morimoto {
15346fb03550SKuninori Morimoto 	struct snd_soc_component *component;
153562f07a6bSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
1536613fb500SKuninori Morimoto 	int i, ret, order;
15376fb03550SKuninori Morimoto 
153862f07a6bSKuninori Morimoto 	for_each_comp_order(order) {
153962f07a6bSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
1540613fb500SKuninori Morimoto 			for_each_rtd_components(rtd, i, component) {
154162f07a6bSKuninori Morimoto 				if (component->driver->probe_order != order)
154262f07a6bSKuninori Morimoto 					continue;
154362f07a6bSKuninori Morimoto 
15446fb03550SKuninori Morimoto 				ret = soc_probe_component(card, component);
15456fb03550SKuninori Morimoto 				if (ret < 0)
15466fb03550SKuninori Morimoto 					return ret;
15476fb03550SKuninori Morimoto 			}
15486fb03550SKuninori Morimoto 		}
154962f07a6bSKuninori Morimoto 	}
15506fb03550SKuninori Morimoto 
15516fb03550SKuninori Morimoto 	return 0;
15526fb03550SKuninori Morimoto }
15536fb03550SKuninori Morimoto 
1554e8fbd250SKuninori Morimoto static void soc_unbind_aux_dev(struct snd_soc_card *card)
15554893a2ebSKuninori Morimoto {
1556e8fbd250SKuninori Morimoto 	struct snd_soc_component *component, *_component;
1557e8fbd250SKuninori Morimoto 
1558e8fbd250SKuninori Morimoto 	for_each_card_auxs_safe(card, component, _component) {
1559257c4dacSKuninori Morimoto 		/* for snd_soc_component_init() */
1560257c4dacSKuninori Morimoto 		snd_soc_component_set_aux(component, NULL);
15614893a2ebSKuninori Morimoto 		list_del(&component->card_aux_list);
15624893a2ebSKuninori Morimoto 	}
1563e8fbd250SKuninori Morimoto }
15644893a2ebSKuninori Morimoto 
1565bee886f1SKuninori Morimoto static int soc_bind_aux_dev(struct snd_soc_card *card)
1566b19e6e7bSMark Brown {
1567f2ed6b07SMengdong Lin 	struct snd_soc_component *component;
1568bee886f1SKuninori Morimoto 	struct snd_soc_aux_dev *aux;
1569bee886f1SKuninori Morimoto 	int i;
15703ca041edSSebastian Reichel 
1571bee886f1SKuninori Morimoto 	for_each_card_pre_auxs(card, i, aux) {
1572f2ed6b07SMengdong Lin 		/* codecs, usually analog devices */
1573bee886f1SKuninori Morimoto 		component = soc_find_component(&aux->dlc);
1574f2ed6b07SMengdong Lin 		if (!component)
15753dc29b8bSKuninori Morimoto 			return -EPROBE_DEFER;
15763ca041edSSebastian Reichel 
1577257c4dacSKuninori Morimoto 		/* for snd_soc_component_init() */
1578257c4dacSKuninori Morimoto 		snd_soc_component_set_aux(component, aux);
1579c2b71c71SKuninori Morimoto 		/* see for_each_card_auxs */
1580d2e3a135SSylwester Nawrocki 		list_add(&component->card_aux_list, &card->aux_comp_list);
1581bee886f1SKuninori Morimoto 	}
1582f2ed6b07SMengdong Lin 	return 0;
1583b19e6e7bSMark Brown }
1584b19e6e7bSMark Brown 
1585f2ed6b07SMengdong Lin static int soc_probe_aux_devices(struct snd_soc_card *card)
1586f2ed6b07SMengdong Lin {
158774bd3f92SKuninori Morimoto 	struct snd_soc_component *component;
1588f2ed6b07SMengdong Lin 	int order;
1589f2ed6b07SMengdong Lin 	int ret;
1590f2ed6b07SMengdong Lin 
15911a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
159274bd3f92SKuninori Morimoto 		for_each_card_auxs(card, component) {
159374bd3f92SKuninori Morimoto 			if (component->driver->probe_order != order)
159474bd3f92SKuninori Morimoto 				continue;
159574bd3f92SKuninori Morimoto 
159674bd3f92SKuninori Morimoto 			ret = soc_probe_component(card,	component);
159774bd3f92SKuninori Morimoto 			if (ret < 0)
1598f2ed6b07SMengdong Lin 				return ret;
1599f2ed6b07SMengdong Lin 		}
1600f2ed6b07SMengdong Lin 	}
160165d9361fSLars-Peter Clausen 
160244c69bb1SLars-Peter Clausen 	return 0;
16033ca041edSSebastian Reichel }
16042eea392dSJarkko Nikula 
1605f2ed6b07SMengdong Lin static void soc_remove_aux_devices(struct snd_soc_card *card)
160644c69bb1SLars-Peter Clausen {
1607f2ed6b07SMengdong Lin 	struct snd_soc_component *comp, *_comp;
1608f2ed6b07SMengdong Lin 	int order;
160944c69bb1SLars-Peter Clausen 
16101a1035a9SKuninori Morimoto 	for_each_comp_order(order) {
1611c2b71c71SKuninori Morimoto 		for_each_card_auxs_safe(card, comp, _comp) {
1612e8fbd250SKuninori Morimoto 			if (comp->driver->remove_order == order)
1613c6619b72SKuninori Morimoto 				soc_remove_component(comp, 1);
16145f3484acSLars-Peter Clausen 		}
16152eea392dSJarkko Nikula 	}
16162eea392dSJarkko Nikula }
16172eea392dSJarkko Nikula 
16181f5a4535STakashi Iwai #ifdef CONFIG_DMI
16192c7b696aSMarcel Ziswiler /*
16208a6a6a38SKuninori Morimoto  * If a DMI filed contain strings in this blacklist (e.g.
16218a6a6a38SKuninori Morimoto  * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
16228a6a6a38SKuninori Morimoto  * as invalid and dropped when setting the card long name from DMI info.
16238a6a6a38SKuninori Morimoto  */
16248a6a6a38SKuninori Morimoto static const char * const dmi_blacklist[] = {
16258a6a6a38SKuninori Morimoto 	"To be filled by OEM",
16268a6a6a38SKuninori Morimoto 	"TBD by OEM",
16278a6a6a38SKuninori Morimoto 	"Default String",
16288a6a6a38SKuninori Morimoto 	"Board Manufacturer",
16298a6a6a38SKuninori Morimoto 	"Board Vendor Name",
16308a6a6a38SKuninori Morimoto 	"Board Product Name",
16318a6a6a38SKuninori Morimoto 	NULL,	/* terminator */
16328a6a6a38SKuninori Morimoto };
16338a6a6a38SKuninori Morimoto 
16348a6a6a38SKuninori Morimoto /*
16352c7b696aSMarcel Ziswiler  * Trim special characters, and replace '-' with '_' since '-' is used to
1636345233d7SLiam Girdwood  * separate different DMI fields in the card long name. Only number and
1637345233d7SLiam Girdwood  * alphabet characters and a few separator characters are kept.
1638345233d7SLiam Girdwood  */
1639345233d7SLiam Girdwood static void cleanup_dmi_name(char *name)
1640345233d7SLiam Girdwood {
1641345233d7SLiam Girdwood 	int i, j = 0;
1642345233d7SLiam Girdwood 
1643345233d7SLiam Girdwood 	for (i = 0; name[i]; i++) {
1644345233d7SLiam Girdwood 		if (isalnum(name[i]) || (name[i] == '.')
1645345233d7SLiam Girdwood 		    || (name[i] == '_'))
1646345233d7SLiam Girdwood 			name[j++] = name[i];
1647345233d7SLiam Girdwood 		else if (name[i] == '-')
1648345233d7SLiam Girdwood 			name[j++] = '_';
1649345233d7SLiam Girdwood 	}
1650345233d7SLiam Girdwood 
1651345233d7SLiam Girdwood 	name[j] = '\0';
1652345233d7SLiam Girdwood }
1653345233d7SLiam Girdwood 
16542c7b696aSMarcel Ziswiler /*
16552c7b696aSMarcel Ziswiler  * Check if a DMI field is valid, i.e. not containing any string
165698faf436SMengdong Lin  * in the black list.
165798faf436SMengdong Lin  */
165898faf436SMengdong Lin static int is_dmi_valid(const char *field)
165998faf436SMengdong Lin {
166098faf436SMengdong Lin 	int i = 0;
166198faf436SMengdong Lin 
166298faf436SMengdong Lin 	while (dmi_blacklist[i]) {
166398faf436SMengdong Lin 		if (strstr(field, dmi_blacklist[i]))
166498faf436SMengdong Lin 			return 0;
166598faf436SMengdong Lin 		i++;
166646b5a4d2SWu Fengguang 	}
166798faf436SMengdong Lin 
166898faf436SMengdong Lin 	return 1;
166998faf436SMengdong Lin }
167098faf436SMengdong Lin 
16714e01e5dbSJaroslav Kysela /*
16724e01e5dbSJaroslav Kysela  * Append a string to card->dmi_longname with character cleanups.
16734e01e5dbSJaroslav Kysela  */
16744e01e5dbSJaroslav Kysela static void append_dmi_string(struct snd_soc_card *card, const char *str)
16754e01e5dbSJaroslav Kysela {
16764e01e5dbSJaroslav Kysela 	char *dst = card->dmi_longname;
16774e01e5dbSJaroslav Kysela 	size_t dst_len = sizeof(card->dmi_longname);
16784e01e5dbSJaroslav Kysela 	size_t len;
16794e01e5dbSJaroslav Kysela 
16804e01e5dbSJaroslav Kysela 	len = strlen(dst);
16814e01e5dbSJaroslav Kysela 	snprintf(dst + len, dst_len - len, "-%s", str);
16824e01e5dbSJaroslav Kysela 
16834e01e5dbSJaroslav Kysela 	len++;	/* skip the separator "-" */
16844e01e5dbSJaroslav Kysela 	if (len < dst_len)
16854e01e5dbSJaroslav Kysela 		cleanup_dmi_name(dst + len);
16864e01e5dbSJaroslav Kysela }
16874e01e5dbSJaroslav Kysela 
1688345233d7SLiam Girdwood /**
1689345233d7SLiam Girdwood  * snd_soc_set_dmi_name() - Register DMI names to card
1690345233d7SLiam Girdwood  * @card: The card to register DMI names
1691345233d7SLiam Girdwood  * @flavour: The flavour "differentiator" for the card amongst its peers.
1692345233d7SLiam Girdwood  *
1693345233d7SLiam Girdwood  * An Intel machine driver may be used by many different devices but are
1694345233d7SLiam Girdwood  * difficult for userspace to differentiate, since machine drivers ususally
1695345233d7SLiam Girdwood  * use their own name as the card short name and leave the card long name
1696345233d7SLiam Girdwood  * blank. To differentiate such devices and fix bugs due to lack of
1697345233d7SLiam Girdwood  * device-specific configurations, this function allows DMI info to be used
1698345233d7SLiam Girdwood  * as the sound card long name, in the format of
1699345233d7SLiam Girdwood  * "vendor-product-version-board"
1700345233d7SLiam Girdwood  * (Character '-' is used to separate different DMI fields here).
1701345233d7SLiam Girdwood  * This will help the user space to load the device-specific Use Case Manager
1702345233d7SLiam Girdwood  * (UCM) configurations for the card.
1703345233d7SLiam Girdwood  *
1704345233d7SLiam Girdwood  * Possible card long names may be:
1705345233d7SLiam Girdwood  * DellInc.-XPS139343-01-0310JH
1706345233d7SLiam Girdwood  * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1707345233d7SLiam Girdwood  * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1708345233d7SLiam Girdwood  *
1709345233d7SLiam Girdwood  * This function also supports flavoring the card longname to provide
1710345233d7SLiam Girdwood  * the extra differentiation, like "vendor-product-version-board-flavor".
1711345233d7SLiam Girdwood  *
1712345233d7SLiam Girdwood  * We only keep number and alphabet characters and a few separator characters
1713345233d7SLiam Girdwood  * in the card long name since UCM in the user space uses the card long names
1714345233d7SLiam Girdwood  * as card configuration directory names and AudoConf cannot support special
1715345233d7SLiam Girdwood  * charactors like SPACE.
1716345233d7SLiam Girdwood  *
1717345233d7SLiam Girdwood  * Returns 0 on success, otherwise a negative error code.
1718345233d7SLiam Girdwood  */
1719345233d7SLiam Girdwood int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1720345233d7SLiam Girdwood {
1721cdb76568SKuninori Morimoto 	const char *vendor, *product, *board;
1722345233d7SLiam Girdwood 
1723345233d7SLiam Girdwood 	if (card->long_name)
1724345233d7SLiam Girdwood 		return 0; /* long name already set by driver or from DMI */
1725345233d7SLiam Girdwood 
1726c68fded7SPierre-Louis Bossart 	if (!dmi_available)
17277de14d58SJon Hunter 		return 0;
17287de14d58SJon Hunter 
17294e01e5dbSJaroslav Kysela 	/* make up dmi long name as: vendor-product-version-board */
1730345233d7SLiam Girdwood 	vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
173198faf436SMengdong Lin 	if (!vendor || !is_dmi_valid(vendor)) {
1732345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1733345233d7SLiam Girdwood 		return 0;
1734345233d7SLiam Girdwood 	}
1735345233d7SLiam Girdwood 
17364e01e5dbSJaroslav Kysela 	snprintf(card->dmi_longname, sizeof(card->dmi_longname), "%s", vendor);
1737345233d7SLiam Girdwood 	cleanup_dmi_name(card->dmi_longname);
1738345233d7SLiam Girdwood 
1739345233d7SLiam Girdwood 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
174098faf436SMengdong Lin 	if (product && is_dmi_valid(product)) {
1741cdb76568SKuninori Morimoto 		const char *product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
1742cdb76568SKuninori Morimoto 
17434e01e5dbSJaroslav Kysela 		append_dmi_string(card, product);
1744345233d7SLiam Girdwood 
17452c7b696aSMarcel Ziswiler 		/*
17462c7b696aSMarcel Ziswiler 		 * some vendors like Lenovo may only put a self-explanatory
1747345233d7SLiam Girdwood 		 * name in the product version field
1748345233d7SLiam Girdwood 		 */
17494e01e5dbSJaroslav Kysela 		if (product_version && is_dmi_valid(product_version))
17504e01e5dbSJaroslav Kysela 			append_dmi_string(card, product_version);
1751345233d7SLiam Girdwood 	}
1752345233d7SLiam Girdwood 
1753345233d7SLiam Girdwood 	board = dmi_get_system_info(DMI_BOARD_NAME);
175498faf436SMengdong Lin 	if (board && is_dmi_valid(board)) {
175539870b0dSJaroslav Kysela 		if (!product || strcasecmp(board, product))
17564e01e5dbSJaroslav Kysela 			append_dmi_string(card, board);
1757345233d7SLiam Girdwood 	} else if (!product) {
1758345233d7SLiam Girdwood 		/* fall back to using legacy name */
1759345233d7SLiam Girdwood 		dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1760345233d7SLiam Girdwood 		return 0;
1761345233d7SLiam Girdwood 	}
1762345233d7SLiam Girdwood 
1763345233d7SLiam Girdwood 	/* Add flavour to dmi long name */
17644e01e5dbSJaroslav Kysela 	if (flavour)
17654e01e5dbSJaroslav Kysela 		append_dmi_string(card, flavour);
1766345233d7SLiam Girdwood 
1767345233d7SLiam Girdwood 	/* set the card long name */
1768345233d7SLiam Girdwood 	card->long_name = card->dmi_longname;
1769345233d7SLiam Girdwood 
1770345233d7SLiam Girdwood 	return 0;
1771345233d7SLiam Girdwood }
1772345233d7SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
17731f5a4535STakashi Iwai #endif /* CONFIG_DMI */
1774345233d7SLiam Girdwood 
1775a655de80SLiam Girdwood static void soc_check_tplg_fes(struct snd_soc_card *card)
1776a655de80SLiam Girdwood {
1777a655de80SLiam Girdwood 	struct snd_soc_component *component;
1778a655de80SLiam Girdwood 	const struct snd_soc_component_driver *comp_drv;
1779a655de80SLiam Girdwood 	struct snd_soc_dai_link *dai_link;
1780a655de80SLiam Girdwood 	int i;
1781a655de80SLiam Girdwood 
1782368dee94SKuninori Morimoto 	for_each_component(component) {
1783a655de80SLiam Girdwood 
178449f9c4f2SDaniel Baluta 		/* does this component override BEs ? */
1785a655de80SLiam Girdwood 		if (!component->driver->ignore_machine)
1786a655de80SLiam Girdwood 			continue;
1787a655de80SLiam Girdwood 
1788a655de80SLiam Girdwood 		/* for this machine ? */
1789e194098bSPierre-Louis Bossart 		if (!strcmp(component->driver->ignore_machine,
1790a655de80SLiam Girdwood 			    card->dev->driver->name))
1791e194098bSPierre-Louis Bossart 			goto match;
1792e194098bSPierre-Louis Bossart 		if (strcmp(component->driver->ignore_machine,
1793e194098bSPierre-Louis Bossart 			   dev_name(card->dev)))
1794a655de80SLiam Girdwood 			continue;
1795e194098bSPierre-Louis Bossart match:
1796a655de80SLiam Girdwood 		/* machine matches, so override the rtd data */
17977fe072b4SKuninori Morimoto 		for_each_card_prelinks(card, i, dai_link) {
1798a655de80SLiam Girdwood 
1799a655de80SLiam Girdwood 			/* ignore this FE */
1800a655de80SLiam Girdwood 			if (dai_link->dynamic) {
1801a655de80SLiam Girdwood 				dai_link->ignore = true;
1802a655de80SLiam Girdwood 				continue;
1803a655de80SLiam Girdwood 			}
1804a655de80SLiam Girdwood 
18051328948fSPierre-Louis Bossart 			dev_dbg(card->dev, "info: override BE DAI link %s\n",
1806a655de80SLiam Girdwood 				card->dai_link[i].name);
1807a655de80SLiam Girdwood 
1808a655de80SLiam Girdwood 			/* override platform component */
1809adb76b5bSKuninori Morimoto 			if (!dai_link->platforms) {
1810daecf46eSKuninori Morimoto 				dev_err(card->dev, "init platform error");
1811daecf46eSKuninori Morimoto 				continue;
1812daecf46eSKuninori Morimoto 			}
1813d9714003SDaniel Baluta 
1814d9714003SDaniel Baluta 			if (component->dev->of_node)
1815d9714003SDaniel Baluta 				dai_link->platforms->of_node = component->dev->of_node;
1816d9714003SDaniel Baluta 			else
1817910fdcabSKuninori Morimoto 				dai_link->platforms->name = component->name;
1818a655de80SLiam Girdwood 
1819a655de80SLiam Girdwood 			/* convert non BE into BE */
1820607fa205SBard Liao 			if (!dai_link->no_pcm) {
1821a655de80SLiam Girdwood 				dai_link->no_pcm = 1;
1822607fa205SBard Liao 
1823607fa205SBard Liao 				if (dai_link->dpcm_playback)
1824607fa205SBard Liao 					dev_warn(card->dev,
1825607fa205SBard Liao 						 "invalid configuration, dailink %s has flags no_pcm=0 and dpcm_playback=1\n",
1826607fa205SBard Liao 						 dai_link->name);
1827607fa205SBard Liao 				if (dai_link->dpcm_capture)
1828607fa205SBard Liao 					dev_warn(card->dev,
1829607fa205SBard Liao 						 "invalid configuration, dailink %s has flags no_pcm=0 and dpcm_capture=1\n",
1830607fa205SBard Liao 						 dai_link->name);
1831607fa205SBard Liao 
1832607fa205SBard Liao 				/* convert normal link into DPCM one */
1833607fa205SBard Liao 				if (!(dai_link->dpcm_playback ||
1834607fa205SBard Liao 				      dai_link->dpcm_capture)) {
1835607fa205SBard Liao 					dai_link->dpcm_playback = !dai_link->capture_only;
1836607fa205SBard Liao 					dai_link->dpcm_capture = !dai_link->playback_only;
1837607fa205SBard Liao 				}
1838607fa205SBard Liao 			}
1839a655de80SLiam Girdwood 
18400cbbf8a0SKuninori Morimoto 			/*
18410cbbf8a0SKuninori Morimoto 			 * override any BE fixups
18420cbbf8a0SKuninori Morimoto 			 * see
18430cbbf8a0SKuninori Morimoto 			 *	snd_soc_link_be_hw_params_fixup()
18440cbbf8a0SKuninori Morimoto 			 */
1845a655de80SLiam Girdwood 			dai_link->be_hw_params_fixup =
1846a655de80SLiam Girdwood 				component->driver->be_hw_params_fixup;
1847a655de80SLiam Girdwood 
18482c7b696aSMarcel Ziswiler 			/*
18492c7b696aSMarcel Ziswiler 			 * most BE links don't set stream name, so set it to
1850a655de80SLiam Girdwood 			 * dai link name if it's NULL to help bind widgets.
1851a655de80SLiam Girdwood 			 */
1852a655de80SLiam Girdwood 			if (!dai_link->stream_name)
1853a655de80SLiam Girdwood 				dai_link->stream_name = dai_link->name;
1854a655de80SLiam Girdwood 		}
1855a655de80SLiam Girdwood 
1856a655de80SLiam Girdwood 		/* Inform userspace we are using alternate topology */
1857a655de80SLiam Girdwood 		if (component->driver->topology_name_prefix) {
1858a655de80SLiam Girdwood 
1859a655de80SLiam Girdwood 			/* topology shortname created? */
1860a655de80SLiam Girdwood 			if (!card->topology_shortname_created) {
1861a655de80SLiam Girdwood 				comp_drv = component->driver;
1862a655de80SLiam Girdwood 
1863a655de80SLiam Girdwood 				snprintf(card->topology_shortname, 32, "%s-%s",
1864a655de80SLiam Girdwood 					 comp_drv->topology_name_prefix,
1865a655de80SLiam Girdwood 					 card->name);
1866a655de80SLiam Girdwood 				card->topology_shortname_created = true;
1867a655de80SLiam Girdwood 			}
1868a655de80SLiam Girdwood 
1869a655de80SLiam Girdwood 			/* use topology shortname */
1870a655de80SLiam Girdwood 			card->name = card->topology_shortname;
1871a655de80SLiam Girdwood 		}
1872a655de80SLiam Girdwood 	}
1873a655de80SLiam Girdwood }
1874a655de80SLiam Girdwood 
18750f23f718SKuninori Morimoto #define soc_setup_card_name(name, name1, name2, norm)		\
18760f23f718SKuninori Morimoto 	__soc_setup_card_name(name, sizeof(name), name1, name2, norm)
18770f23f718SKuninori Morimoto static void __soc_setup_card_name(char *name, int len,
18780f23f718SKuninori Morimoto 				  const char *name1, const char *name2,
18790f23f718SKuninori Morimoto 				  int normalization)
18800f23f718SKuninori Morimoto {
18810f23f718SKuninori Morimoto 	int i;
18820f23f718SKuninori Morimoto 
18830f23f718SKuninori Morimoto 	snprintf(name, len, "%s", name1 ? name1 : name2);
18840f23f718SKuninori Morimoto 
18850f23f718SKuninori Morimoto 	if (!normalization)
18860f23f718SKuninori Morimoto 		return;
18870f23f718SKuninori Morimoto 
18880f23f718SKuninori Morimoto 	/*
18890f23f718SKuninori Morimoto 	 * Name normalization
18900f23f718SKuninori Morimoto 	 *
18910f23f718SKuninori Morimoto 	 * The driver name is somewhat special, as it's used as a key for
18920f23f718SKuninori Morimoto 	 * searches in the user-space.
18930f23f718SKuninori Morimoto 	 *
18940f23f718SKuninori Morimoto 	 * ex)
18950f23f718SKuninori Morimoto 	 *	"abcd??efg" -> "abcd__efg"
18960f23f718SKuninori Morimoto 	 */
18970f23f718SKuninori Morimoto 	for (i = 0; i < len; i++) {
18980f23f718SKuninori Morimoto 		switch (name[i]) {
18990f23f718SKuninori Morimoto 		case '_':
19000f23f718SKuninori Morimoto 		case '-':
19010f23f718SKuninori Morimoto 		case '\0':
19020f23f718SKuninori Morimoto 			break;
19030f23f718SKuninori Morimoto 		default:
19040f23f718SKuninori Morimoto 			if (!isalnum(name[i]))
19050f23f718SKuninori Morimoto 				name[i] = '_';
19060f23f718SKuninori Morimoto 			break;
19070f23f718SKuninori Morimoto 		}
19080f23f718SKuninori Morimoto 	}
19090f23f718SKuninori Morimoto }
19100f23f718SKuninori Morimoto 
191127f07cacSKuninori Morimoto static void soc_cleanup_card_resources(struct snd_soc_card *card)
191253e947a0SKuninori Morimoto {
1913cc733900SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd, *n;
19147ce6088fSKuninori Morimoto 
19150ced7b05SKuninori Morimoto 	if (card->snd_card)
19160ced7b05SKuninori Morimoto 		snd_card_disconnect_sync(card->snd_card);
191753e947a0SKuninori Morimoto 
19182a6f0892SKuninori Morimoto 	snd_soc_dapm_shutdown(card);
19192a6f0892SKuninori Morimoto 
192053e947a0SKuninori Morimoto 	/* remove and free each DAI */
19217ce6088fSKuninori Morimoto 	soc_remove_link_dais(card);
19220ced7b05SKuninori Morimoto 	soc_remove_link_components(card);
19237ce6088fSKuninori Morimoto 
1924cc733900SKuninori Morimoto 	for_each_card_rtds_safe(card, rtd, n)
192550cd9b53SKuninori Morimoto 		snd_soc_remove_pcm_runtime(card, rtd);
19267ce6088fSKuninori Morimoto 
192753e947a0SKuninori Morimoto 	/* remove auxiliary devices */
192853e947a0SKuninori Morimoto 	soc_remove_aux_devices(card);
1929e8fbd250SKuninori Morimoto 	soc_unbind_aux_dev(card);
193053e947a0SKuninori Morimoto 
193153e947a0SKuninori Morimoto 	snd_soc_dapm_free(&card->dapm);
193253e947a0SKuninori Morimoto 	soc_cleanup_card_debugfs(card);
193353e947a0SKuninori Morimoto 
193453e947a0SKuninori Morimoto 	/* remove the card */
1935b0275d95SKuninori Morimoto 	snd_soc_card_remove(card);
19360ced7b05SKuninori Morimoto 
19370ced7b05SKuninori Morimoto 	if (card->snd_card) {
19380ced7b05SKuninori Morimoto 		snd_card_free(card->snd_card);
19390ced7b05SKuninori Morimoto 		card->snd_card = NULL;
19400ced7b05SKuninori Morimoto 	}
194153e947a0SKuninori Morimoto }
194253e947a0SKuninori Morimoto 
19432cc1afcfSKuninori Morimoto static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
19442cc1afcfSKuninori Morimoto {
19452cc1afcfSKuninori Morimoto 	if (card->instantiated) {
19462cc1afcfSKuninori Morimoto 		card->instantiated = false;
19472cc1afcfSKuninori Morimoto 		snd_soc_flush_all_delayed_work(card);
19482cc1afcfSKuninori Morimoto 
194927f07cacSKuninori Morimoto 		soc_cleanup_card_resources(card);
19502cc1afcfSKuninori Morimoto 		if (!unregister)
19512cc1afcfSKuninori Morimoto 			list_add(&card->list, &unbind_card_list);
19522cc1afcfSKuninori Morimoto 	} else {
19532cc1afcfSKuninori Morimoto 		if (unregister)
19542cc1afcfSKuninori Morimoto 			list_del(&card->list);
19552cc1afcfSKuninori Morimoto 	}
19562cc1afcfSKuninori Morimoto }
19572cc1afcfSKuninori Morimoto 
1958ed90c013SKuninori Morimoto static int snd_soc_bind_card(struct snd_soc_card *card)
1959f0fba2adSLiam Girdwood {
19601a497983SMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
196176c39e86SKuninori Morimoto 	struct snd_soc_component *component;
196261b0088bSMengdong Lin 	struct snd_soc_dai_link *dai_link;
196327f07cacSKuninori Morimoto 	int ret, i;
196496dd3622SMark Brown 
196534e81ab4SLars-Peter Clausen 	mutex_lock(&client_mutex);
196601b9d99aSLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1967f0fba2adSLiam Girdwood 
196895c267ddSKuninori Morimoto 	snd_soc_dapm_init(&card->dapm, card, NULL);
196953e947a0SKuninori Morimoto 
1970a655de80SLiam Girdwood 	/* check whether any platform is ignore machine FE and using topology */
1971a655de80SLiam Girdwood 	soc_check_tplg_fes(card);
1972a655de80SLiam Girdwood 
197344c69bb1SLars-Peter Clausen 	/* bind aux_devs too */
1974bee886f1SKuninori Morimoto 	ret = soc_bind_aux_dev(card);
1975bee886f1SKuninori Morimoto 	if (ret < 0)
197653e947a0SKuninori Morimoto 		goto probe_end;
1977db2a4165SFrank Mandarino 
1978f8f80361SMengdong Lin 	/* add predefined DAI links to the list */
1979d8145989SKuninori Morimoto 	card->num_rtd = 0;
19805b99a0aaSKuninori Morimoto 	for_each_card_prelinks(card, i, dai_link) {
19810c048004SKuninori Morimoto 		ret = snd_soc_add_pcm_runtime(card, dai_link);
19825b99a0aaSKuninori Morimoto 		if (ret < 0)
19835b99a0aaSKuninori Morimoto 			goto probe_end;
19845b99a0aaSKuninori Morimoto 	}
1985f8f80361SMengdong Lin 
1986f0fba2adSLiam Girdwood 	/* card bind complete so register a sound card */
1987102b5a8dSTakashi Iwai 	ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1988f0fba2adSLiam Girdwood 			card->owner, 0, &card->snd_card);
1989f0fba2adSLiam Girdwood 	if (ret < 0) {
199010e8aa9aSMichał Mirosław 		dev_err(card->dev,
199110e8aa9aSMichał Mirosław 			"ASoC: can't create sound card for card %s: %d\n",
199210e8aa9aSMichał Mirosław 			card->name, ret);
199353e947a0SKuninori Morimoto 		goto probe_end;
1994db2a4165SFrank Mandarino 	}
1995db2a4165SFrank Mandarino 
19960757d834SLars-Peter Clausen 	soc_init_card_debugfs(card);
19970757d834SLars-Peter Clausen 
1998b3da4251SKuninori Morimoto 	soc_resume_init(card);
19996ed25978SAndy Green 
2000b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
20019a841ebbSMark Brown 					card->num_dapm_widgets);
2002b8ba3b57SKuninori Morimoto 	if (ret < 0)
2003b8ba3b57SKuninori Morimoto 		goto probe_end;
20049a841ebbSMark Brown 
2005b8ba3b57SKuninori Morimoto 	ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2006f23e860eSNicolin Chen 					card->num_of_dapm_widgets);
2007b8ba3b57SKuninori Morimoto 	if (ret < 0)
2008b8ba3b57SKuninori Morimoto 		goto probe_end;
2009f23e860eSNicolin Chen 
2010f0fba2adSLiam Girdwood 	/* initialise the sound card only once */
201173de4b02SKuninori Morimoto 	ret = snd_soc_card_probe(card);
2012f0fba2adSLiam Girdwood 	if (ret < 0)
201353e947a0SKuninori Morimoto 		goto probe_end;
2014f0fba2adSLiam Girdwood 
201562ae68faSStephen Warren 	/* probe all components used by DAI links on this card */
201662f07a6bSKuninori Morimoto 	ret = soc_probe_link_components(card);
201762ae68faSStephen Warren 	if (ret < 0) {
2018f110bfc7SLiam Girdwood 		dev_err(card->dev,
201962f07a6bSKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
202053e947a0SKuninori Morimoto 		goto probe_end;
202162ae68faSStephen Warren 	}
202262ae68faSStephen Warren 
2023f2ed6b07SMengdong Lin 	/* probe auxiliary components */
2024f2ed6b07SMengdong Lin 	ret = soc_probe_aux_devices(card);
202574bd3f92SKuninori Morimoto 	if (ret < 0) {
202674bd3f92SKuninori Morimoto 		dev_err(card->dev,
202774bd3f92SKuninori Morimoto 			"ASoC: failed to probe aux component %d\n", ret);
202853e947a0SKuninori Morimoto 		goto probe_end;
202974bd3f92SKuninori Morimoto 	}
2030f2ed6b07SMengdong Lin 
203162ae68faSStephen Warren 	/* probe all DAI links on this card */
2032c7e73774SKuninori Morimoto 	ret = soc_probe_link_dais(card);
2033fe3e78e0SMark Brown 	if (ret < 0) {
2034f110bfc7SLiam Girdwood 		dev_err(card->dev,
2035c7e73774SKuninori Morimoto 			"ASoC: failed to instantiate card %d\n", ret);
203653e947a0SKuninori Morimoto 		goto probe_end;
2037fe3e78e0SMark Brown 	}
2038fe3e78e0SMark Brown 
2039626c2e57SKuninori Morimoto 	for_each_card_rtds(card, rtd) {
2040eaffeefbSKuninori Morimoto 		ret = soc_init_pcm_runtime(card, rtd);
2041626c2e57SKuninori Morimoto 		if (ret < 0)
2042626c2e57SKuninori Morimoto 			goto probe_end;
2043626c2e57SKuninori Morimoto 	}
2044c4b46982SKuninori Morimoto 
2045888df395SMark Brown 	snd_soc_dapm_link_dai_widgets(card);
2046b893ea5fSLiam Girdwood 	snd_soc_dapm_connect_dai_link_widgets(card);
2047888df395SMark Brown 
20489b98c7c2SKuninori Morimoto 	ret = snd_soc_add_card_controls(card, card->controls,
20492c7b696aSMarcel Ziswiler 					card->num_controls);
20509b98c7c2SKuninori Morimoto 	if (ret < 0)
20519b98c7c2SKuninori Morimoto 		goto probe_end;
2052b7af1dafSMark Brown 
2053daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2054b8ad29deSMark Brown 				      card->num_dapm_routes);
2055a22ae72bSPierre-Louis Bossart 	if (ret < 0) {
2056a22ae72bSPierre-Louis Bossart 		if (card->disable_route_checks) {
2057a22ae72bSPierre-Louis Bossart 			dev_info(card->dev,
2058a22ae72bSPierre-Louis Bossart 				 "%s: disable_route_checks set, ignoring errors on add_routes\n",
2059a22ae72bSPierre-Louis Bossart 				 __func__);
2060a22ae72bSPierre-Louis Bossart 		} else {
2061a22ae72bSPierre-Louis Bossart 			dev_err(card->dev,
2062a22ae72bSPierre-Louis Bossart 				 "%s: snd_soc_dapm_add_routes failed: %d\n",
2063a22ae72bSPierre-Louis Bossart 				 __func__, ret);
2064daa480bdSKuninori Morimoto 			goto probe_end;
2065a22ae72bSPierre-Louis Bossart 		}
2066a22ae72bSPierre-Louis Bossart 	}
2067b8ad29deSMark Brown 
2068daa480bdSKuninori Morimoto 	ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2069f23e860eSNicolin Chen 				      card->num_of_dapm_routes);
2070daa480bdSKuninori Morimoto 	if (ret < 0)
2071daa480bdSKuninori Morimoto 		goto probe_end;
207275d9ac46SMark Brown 
2073861886d3STakashi Iwai 	/* try to set some sane longname if DMI is available */
2074861886d3STakashi Iwai 	snd_soc_set_dmi_name(card, NULL);
2075861886d3STakashi Iwai 
20760f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->shortname,
20770f23f718SKuninori Morimoto 			    card->name, NULL, 0);
20780f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->longname,
20790f23f718SKuninori Morimoto 			    card->long_name, card->name, 0);
20800f23f718SKuninori Morimoto 	soc_setup_card_name(card->snd_card->driver,
20810f23f718SKuninori Morimoto 			    card->driver_name, card->name, 1);
2082fe3e78e0SMark Brown 
2083dc73d73aSJaroslav Kysela 	if (card->components) {
2084dc73d73aSJaroslav Kysela 		/* the current implementation of snd_component_add() accepts */
2085dc73d73aSJaroslav Kysela 		/* multiple components in the string separated by space, */
2086dc73d73aSJaroslav Kysela 		/* but the string collision (identical string) check might */
2087dc73d73aSJaroslav Kysela 		/* not work correctly */
2088dc73d73aSJaroslav Kysela 		ret = snd_component_add(card->snd_card, card->components);
2089dc73d73aSJaroslav Kysela 		if (ret < 0) {
2090dc73d73aSJaroslav Kysela 			dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n",
2091dc73d73aSJaroslav Kysela 				card->name, ret);
2092dc73d73aSJaroslav Kysela 			goto probe_end;
2093dc73d73aSJaroslav Kysela 		}
2094dc73d73aSJaroslav Kysela 	}
2095dc73d73aSJaroslav Kysela 
20965c0eac03SKuninori Morimoto 	ret = snd_soc_card_late_probe(card);
20975c0eac03SKuninori Morimoto 	if (ret < 0)
209853e947a0SKuninori Morimoto 		goto probe_end;
209928e9ad92SMark Brown 
2100824ef826SLars-Peter Clausen 	snd_soc_dapm_new_widgets(card);
21018c193b8dSLars-Peter Clausen 
2102f0fba2adSLiam Girdwood 	ret = snd_card_register(card->snd_card);
2103fe3e78e0SMark Brown 	if (ret < 0) {
2104f110bfc7SLiam Girdwood 		dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2105f110bfc7SLiam Girdwood 				ret);
210653e947a0SKuninori Morimoto 		goto probe_end;
2107fe3e78e0SMark Brown 	}
2108fe3e78e0SMark Brown 
2109435c5e25SMark Brown 	card->instantiated = 1;
2110882eab6cSTzung-Bi Shih 	dapm_mark_endpoints_dirty(card);
21114f4c0072SMark Brown 	snd_soc_dapm_sync(&card->dapm);
2112b19e6e7bSMark Brown 
2113ed90c013SKuninori Morimoto 	/* deactivate pins to sleep state */
211476c39e86SKuninori Morimoto 	for_each_card_components(card, component)
2115b3dea624SKuninori Morimoto 		if (!snd_soc_component_active(component))
211676c39e86SKuninori Morimoto 			pinctrl_pm_select_sleep_state(component->dev);
2117ed90c013SKuninori Morimoto 
211853e947a0SKuninori Morimoto probe_end:
211953e947a0SKuninori Morimoto 	if (ret < 0)
212027f07cacSKuninori Morimoto 		soc_cleanup_card_resources(card);
2121db2a4165SFrank Mandarino 
2122f0fba2adSLiam Girdwood 	mutex_unlock(&card->mutex);
212334e81ab4SLars-Peter Clausen 	mutex_unlock(&client_mutex);
2124db2a4165SFrank Mandarino 
2125b19e6e7bSMark Brown 	return ret;
2126435c5e25SMark Brown }
2127435c5e25SMark Brown 
2128435c5e25SMark Brown /* probes a new socdev */
2129435c5e25SMark Brown static int soc_probe(struct platform_device *pdev)
2130435c5e25SMark Brown {
2131f0fba2adSLiam Girdwood 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2132435c5e25SMark Brown 
213370a7ca34SVinod Koul 	/*
213470a7ca34SVinod Koul 	 * no card, so machine driver should be registering card
213570a7ca34SVinod Koul 	 * we should not be here in that case so ret error
213670a7ca34SVinod Koul 	 */
213770a7ca34SVinod Koul 	if (!card)
213870a7ca34SVinod Koul 		return -EINVAL;
213970a7ca34SVinod Koul 
2140fe4085e8SMark Brown 	dev_warn(&pdev->dev,
2141f110bfc7SLiam Girdwood 		 "ASoC: machine %s should use snd_soc_register_card()\n",
2142fe4085e8SMark Brown 		 card->name);
2143fe4085e8SMark Brown 
2144435c5e25SMark Brown 	/* Bodge while we unpick instantiation */
2145435c5e25SMark Brown 	card->dev = &pdev->dev;
2146f0fba2adSLiam Girdwood 
2147ad61b78eSQinglang Miao 	return devm_snd_soc_register_card(&pdev->dev, card);
2148db2a4165SFrank Mandarino }
2149db2a4165SFrank Mandarino 
21506f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev)
215151737470SMark Brown {
21526f8ab4acSMark Brown 	struct snd_soc_card *card = dev_get_drvdata(dev);
215376c39e86SKuninori Morimoto 	struct snd_soc_component *component;
215451737470SMark Brown 
215551737470SMark Brown 	if (!card->instantiated)
2156416356fcSMark Brown 		return 0;
215751737470SMark Brown 
21582c7b696aSMarcel Ziswiler 	/*
21592c7b696aSMarcel Ziswiler 	 * Flush out pmdown_time work - we actually do want to run it
21602c7b696aSMarcel Ziswiler 	 * now, we're shutting down so no imminent restart.
21612c7b696aSMarcel Ziswiler 	 */
216265462e44SKuninori Morimoto 	snd_soc_flush_all_delayed_work(card);
216351737470SMark Brown 
2164f0fba2adSLiam Girdwood 	snd_soc_dapm_shutdown(card);
2165416356fcSMark Brown 
2166988e8cc4SNicolin Chen 	/* deactivate pins to sleep state */
216776c39e86SKuninori Morimoto 	for_each_card_components(card, component)
216876c39e86SKuninori Morimoto 		pinctrl_pm_select_sleep_state(component->dev);
2169988e8cc4SNicolin Chen 
2170416356fcSMark Brown 	return 0;
217151737470SMark Brown }
21726f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff);
217351737470SMark Brown 
21746f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = {
2175b1dd5897SViresh Kumar 	.suspend = snd_soc_suspend,
2176b1dd5897SViresh Kumar 	.resume = snd_soc_resume,
2177b1dd5897SViresh Kumar 	.freeze = snd_soc_suspend,
2178b1dd5897SViresh Kumar 	.thaw = snd_soc_resume,
21796f8ab4acSMark Brown 	.poweroff = snd_soc_poweroff,
2180b1dd5897SViresh Kumar 	.restore = snd_soc_resume,
2181416356fcSMark Brown };
2182deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2183416356fcSMark Brown 
2184db2a4165SFrank Mandarino /* ASoC platform driver */
2185db2a4165SFrank Mandarino static struct platform_driver soc_driver = {
2186db2a4165SFrank Mandarino 	.driver		= {
2187db2a4165SFrank Mandarino 		.name		= "soc-audio",
21886f8ab4acSMark Brown 		.pm		= &snd_soc_pm_ops,
2189db2a4165SFrank Mandarino 	},
2190db2a4165SFrank Mandarino 	.probe		= soc_probe,
2191db2a4165SFrank Mandarino };
2192db2a4165SFrank Mandarino 
2193096e49d5SMark Brown /**
2194db2a4165SFrank Mandarino  * snd_soc_cnew - create new control
2195db2a4165SFrank Mandarino  * @_template: control template
2196db2a4165SFrank Mandarino  * @data: control private data
2197ac11a2b3SMark Brown  * @long_name: control long name
2198efb7ac3fSMark Brown  * @prefix: control name prefix
2199db2a4165SFrank Mandarino  *
2200db2a4165SFrank Mandarino  * Create a new mixer control from a template control.
2201db2a4165SFrank Mandarino  *
2202db2a4165SFrank Mandarino  * Returns 0 for success, else error.
2203db2a4165SFrank Mandarino  */
2204db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
22053056557fSMark Brown 				  void *data, const char *long_name,
2206efb7ac3fSMark Brown 				  const char *prefix)
2207db2a4165SFrank Mandarino {
2208db2a4165SFrank Mandarino 	struct snd_kcontrol_new template;
2209efb7ac3fSMark Brown 	struct snd_kcontrol *kcontrol;
2210efb7ac3fSMark Brown 	char *name = NULL;
2211db2a4165SFrank Mandarino 
2212db2a4165SFrank Mandarino 	memcpy(&template, _template, sizeof(template));
2213db2a4165SFrank Mandarino 	template.index = 0;
2214db2a4165SFrank Mandarino 
2215efb7ac3fSMark Brown 	if (!long_name)
2216efb7ac3fSMark Brown 		long_name = template.name;
2217efb7ac3fSMark Brown 
2218efb7ac3fSMark Brown 	if (prefix) {
22192b581074SLars-Peter Clausen 		name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2220efb7ac3fSMark Brown 		if (!name)
2221efb7ac3fSMark Brown 			return NULL;
2222efb7ac3fSMark Brown 
2223efb7ac3fSMark Brown 		template.name = name;
2224efb7ac3fSMark Brown 	} else {
2225efb7ac3fSMark Brown 		template.name = long_name;
2226efb7ac3fSMark Brown 	}
2227efb7ac3fSMark Brown 
2228efb7ac3fSMark Brown 	kcontrol = snd_ctl_new1(&template, data);
2229efb7ac3fSMark Brown 
2230efb7ac3fSMark Brown 	kfree(name);
2231efb7ac3fSMark Brown 
2232efb7ac3fSMark Brown 	return kcontrol;
2233db2a4165SFrank Mandarino }
2234db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew);
2235db2a4165SFrank Mandarino 
2236022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2237022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls,
2238022658beSLiam Girdwood 	const char *prefix, void *data)
2239022658beSLiam Girdwood {
2240eaf2469cSKuninori Morimoto 	int i;
2241022658beSLiam Girdwood 
2242022658beSLiam Girdwood 	for (i = 0; i < num_controls; i++) {
2243022658beSLiam Girdwood 		const struct snd_kcontrol_new *control = &controls[i];
2244eaf2469cSKuninori Morimoto 		int err = snd_ctl_add(card, snd_soc_cnew(control, data,
2245022658beSLiam Girdwood 							 control->name, prefix));
2246022658beSLiam Girdwood 		if (err < 0) {
2247f110bfc7SLiam Girdwood 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
2248f110bfc7SLiam Girdwood 				control->name, err);
2249022658beSLiam Girdwood 			return err;
2250022658beSLiam Girdwood 		}
2251022658beSLiam Girdwood 	}
2252022658beSLiam Girdwood 
2253022658beSLiam Girdwood 	return 0;
2254022658beSLiam Girdwood }
2255022658beSLiam Girdwood 
2256db2a4165SFrank Mandarino /**
22570f2780adSLars-Peter Clausen  * snd_soc_add_component_controls - Add an array of controls to a component.
22580f2780adSLars-Peter Clausen  *
22590f2780adSLars-Peter Clausen  * @component: Component to add controls to
22600f2780adSLars-Peter Clausen  * @controls: Array of controls to add
22610f2780adSLars-Peter Clausen  * @num_controls: Number of elements in the array
22620f2780adSLars-Peter Clausen  *
22630f2780adSLars-Peter Clausen  * Return: 0 for success, else error.
22640f2780adSLars-Peter Clausen  */
22650f2780adSLars-Peter Clausen int snd_soc_add_component_controls(struct snd_soc_component *component,
22660f2780adSLars-Peter Clausen 	const struct snd_kcontrol_new *controls, unsigned int num_controls)
22670f2780adSLars-Peter Clausen {
22680f2780adSLars-Peter Clausen 	struct snd_card *card = component->card->snd_card;
22690f2780adSLars-Peter Clausen 
22700f2780adSLars-Peter Clausen 	return snd_soc_add_controls(card, component->dev, controls,
22710f2780adSLars-Peter Clausen 			num_controls, component->name_prefix, component);
22720f2780adSLars-Peter Clausen }
22730f2780adSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
22740f2780adSLars-Peter Clausen 
22750f2780adSLars-Peter Clausen /**
2276022658beSLiam Girdwood  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2277022658beSLiam Girdwood  * Convenience function to add a list of controls.
2278022658beSLiam Girdwood  *
2279022658beSLiam Girdwood  * @soc_card: SoC card to add controls to
2280022658beSLiam Girdwood  * @controls: array of controls to add
2281022658beSLiam Girdwood  * @num_controls: number of elements in the array
2282022658beSLiam Girdwood  *
2283022658beSLiam Girdwood  * Return 0 for success, else error.
2284022658beSLiam Girdwood  */
2285022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2286022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2287022658beSLiam Girdwood {
2288022658beSLiam Girdwood 	struct snd_card *card = soc_card->snd_card;
2289022658beSLiam Girdwood 
2290022658beSLiam Girdwood 	return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2291022658beSLiam Girdwood 			NULL, soc_card);
2292022658beSLiam Girdwood }
2293022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2294022658beSLiam Girdwood 
2295022658beSLiam Girdwood /**
2296022658beSLiam Girdwood  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2297022658beSLiam Girdwood  * Convienience function to add a list of controls.
2298022658beSLiam Girdwood  *
2299022658beSLiam Girdwood  * @dai: DAI to add controls to
2300022658beSLiam Girdwood  * @controls: array of controls to add
2301022658beSLiam Girdwood  * @num_controls: number of elements in the array
2302022658beSLiam Girdwood  *
2303022658beSLiam Girdwood  * Return 0 for success, else error.
2304022658beSLiam Girdwood  */
2305022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2306022658beSLiam Girdwood 	const struct snd_kcontrol_new *controls, int num_controls)
2307022658beSLiam Girdwood {
2308313665b9SLars-Peter Clausen 	struct snd_card *card = dai->component->card->snd_card;
2309022658beSLiam Girdwood 
2310022658beSLiam Girdwood 	return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2311022658beSLiam Girdwood 			NULL, dai);
2312022658beSLiam Girdwood }
2313022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2314022658beSLiam Girdwood 
2315c5af3a2eSMark Brown /**
2316c5af3a2eSMark Brown  * snd_soc_register_card - Register a card with the ASoC core
2317c5af3a2eSMark Brown  *
2318ac11a2b3SMark Brown  * @card: Card to register
2319c5af3a2eSMark Brown  *
2320c5af3a2eSMark Brown  */
232170a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card)
2322c5af3a2eSMark Brown {
2323c5af3a2eSMark Brown 	if (!card->name || !card->dev)
2324c5af3a2eSMark Brown 		return -EINVAL;
2325c5af3a2eSMark Brown 
2326ed77cc12SMark Brown 	dev_set_drvdata(card->dev, card);
2327ed77cc12SMark Brown 
2328b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->widgets);
2329b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->paths);
2330b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->dapm_list);
2331b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->aux_comp_list);
2332b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->component_dev_list);
2333b03bfaecSKuninori Morimoto 	INIT_LIST_HEAD(&card->list);
23341a497983SMengdong Lin 	INIT_LIST_HEAD(&card->rtd_list);
2335db432b41SMark Brown 	INIT_LIST_HEAD(&card->dapm_dirty);
23368a978234SLiam Girdwood 	INIT_LIST_HEAD(&card->dobj_list);
2337b03bfaecSKuninori Morimoto 
2338c5af3a2eSMark Brown 	card->instantiated = 0;
2339f0fba2adSLiam Girdwood 	mutex_init(&card->mutex);
2340a73fb2dfSLiam Girdwood 	mutex_init(&card->dapm_mutex);
234172b745e3SPeter Ujfalusi 	mutex_init(&card->pcm_mutex);
2342a9764869SKaiChieh Chuang 	spin_lock_init(&card->dpcm_lock);
2343c5af3a2eSMark Brown 
2344e894efefSSrinivas Kandagatla 	return snd_soc_bind_card(card);
2345c5af3a2eSMark Brown }
234670a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card);
2347c5af3a2eSMark Brown 
2348c5af3a2eSMark Brown /**
2349c5af3a2eSMark Brown  * snd_soc_unregister_card - Unregister a card with the ASoC core
2350c5af3a2eSMark Brown  *
2351ac11a2b3SMark Brown  * @card: Card to unregister
2352c5af3a2eSMark Brown  *
2353c5af3a2eSMark Brown  */
235470a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card)
2355c5af3a2eSMark Brown {
2356b545542aSKuninori Morimoto 	mutex_lock(&client_mutex);
2357e894efefSSrinivas Kandagatla 	snd_soc_unbind_card(card, true);
2358b545542aSKuninori Morimoto 	mutex_unlock(&client_mutex);
2359f110bfc7SLiam Girdwood 	dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2360c5af3a2eSMark Brown 
2361c5af3a2eSMark Brown 	return 0;
2362c5af3a2eSMark Brown }
236370a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2364c5af3a2eSMark Brown 
2365f0fba2adSLiam Girdwood /*
2366f0fba2adSLiam Girdwood  * Simplify DAI link configuration by removing ".-1" from device names
2367f0fba2adSLiam Girdwood  * and sanitizing names.
2368f0fba2adSLiam Girdwood  */
23690b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id)
2370f0fba2adSLiam Girdwood {
237145dd9943SDmitry Baryshkov 	const char *devname = dev_name(dev);
237245dd9943SDmitry Baryshkov 	char *found, *name;
2373d3b47a88SPierre-Louis Bossart 	unsigned int id1, id2;
2374f0fba2adSLiam Girdwood 
237545dd9943SDmitry Baryshkov 	if (devname == NULL)
2376f0fba2adSLiam Girdwood 		return NULL;
2377f0fba2adSLiam Girdwood 
237845dd9943SDmitry Baryshkov 	name = devm_kstrdup(dev, devname, GFP_KERNEL);
237941daf6baSKefeng Wang 	if (!name)
238041daf6baSKefeng Wang 		return NULL;
2381f0fba2adSLiam Girdwood 
2382f0fba2adSLiam Girdwood 	/* are we a "%s.%d" name (platform and SPI components) */
2383f0fba2adSLiam Girdwood 	found = strstr(name, dev->driver->name);
2384f0fba2adSLiam Girdwood 	if (found) {
2385f0fba2adSLiam Girdwood 		/* get ID */
2386f0fba2adSLiam Girdwood 		if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2387f0fba2adSLiam Girdwood 
2388f0fba2adSLiam Girdwood 			/* discard ID from name if ID == -1 */
2389f0fba2adSLiam Girdwood 			if (*id == -1)
2390f0fba2adSLiam Girdwood 				found[strlen(dev->driver->name)] = '\0';
2391f0fba2adSLiam Girdwood 		}
2392f0fba2adSLiam Girdwood 
2393f0fba2adSLiam Girdwood 	/* I2C component devices are named "bus-addr" */
239445dd9943SDmitry Baryshkov 	} else if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2395f0fba2adSLiam Girdwood 
2396f0fba2adSLiam Girdwood 		/* create unique ID number from I2C addr and bus */
239705899446SJarkko Nikula 		*id = ((id1 & 0xffff) << 16) + id2;
2398f0fba2adSLiam Girdwood 
239945dd9943SDmitry Baryshkov 		devm_kfree(dev, name);
240045dd9943SDmitry Baryshkov 
2401f0fba2adSLiam Girdwood 		/* sanitize component name for DAI link creation */
240245dd9943SDmitry Baryshkov 		name = devm_kasprintf(dev, GFP_KERNEL, "%s.%s", dev->driver->name, devname);
240345dd9943SDmitry Baryshkov 	} else {
2404f0fba2adSLiam Girdwood 		*id = 0;
2405f0fba2adSLiam Girdwood 	}
2406f0fba2adSLiam Girdwood 
240745dd9943SDmitry Baryshkov 	return name;
2408f0fba2adSLiam Girdwood }
2409f0fba2adSLiam Girdwood 
2410f0fba2adSLiam Girdwood /*
2411f0fba2adSLiam Girdwood  * Simplify DAI link naming for single devices with multiple DAIs by removing
2412f0fba2adSLiam Girdwood  * any ".-1" and using the DAI name (instead of device name).
2413f0fba2adSLiam Girdwood  */
2414f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev,
2415f0fba2adSLiam Girdwood 		struct snd_soc_dai_driver *dai_drv)
2416f0fba2adSLiam Girdwood {
2417f0fba2adSLiam Girdwood 	if (dai_drv->name == NULL) {
241810e8aa9aSMichał Mirosław 		dev_err(dev,
241910e8aa9aSMichał Mirosław 			"ASoC: error - multiple DAI %s registered with no name\n",
242010e8aa9aSMichał Mirosław 			dev_name(dev));
2421f0fba2adSLiam Girdwood 		return NULL;
2422f0fba2adSLiam Girdwood 	}
2423f0fba2adSLiam Girdwood 
242450014499SKuninori Morimoto 	return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2425f0fba2adSLiam Girdwood }
2426f0fba2adSLiam Girdwood 
2427ffdbca0bSKuninori Morimoto void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2428e11381f3SKuninori Morimoto {
2429e11381f3SKuninori Morimoto 	dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2430e11381f3SKuninori Morimoto 	list_del(&dai->list);
2431e11381f3SKuninori Morimoto }
2432ffdbca0bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2433e11381f3SKuninori Morimoto 
24347ca24386SKuninori Morimoto /**
24357ca24386SKuninori Morimoto  * snd_soc_register_dai - Register a DAI dynamically & create its widgets
24367ca24386SKuninori Morimoto  *
24377ca24386SKuninori Morimoto  * @component: The component the DAIs are registered for
24387ca24386SKuninori Morimoto  * @dai_drv: DAI driver to use for the DAI
2439bc9a6655SRandy Dunlap  * @legacy_dai_naming: if %true, use legacy single-name format;
2440bc9a6655SRandy Dunlap  * 	if %false, use multiple-name format;
24417ca24386SKuninori Morimoto  *
24427ca24386SKuninori Morimoto  * Topology can use this API to register DAIs when probing a component.
24437ca24386SKuninori Morimoto  * These DAIs's widgets will be freed in the card cleanup and the DAIs
24447ca24386SKuninori Morimoto  * will be freed in the component cleanup.
24457ca24386SKuninori Morimoto  */
24467ca24386SKuninori Morimoto struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
24475e4fb372SMengdong Lin 					 struct snd_soc_dai_driver *dai_drv,
24485e4fb372SMengdong Lin 					 bool legacy_dai_naming)
24495e4fb372SMengdong Lin {
24505e4fb372SMengdong Lin 	struct device *dev = component->dev;
24515e4fb372SMengdong Lin 	struct snd_soc_dai *dai;
24525e4fb372SMengdong Lin 
24535e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
24545e4fb372SMengdong Lin 
24557ca24386SKuninori Morimoto 	lockdep_assert_held(&client_mutex);
24567ca24386SKuninori Morimoto 
245750014499SKuninori Morimoto 	dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
24585e4fb372SMengdong Lin 	if (dai == NULL)
24595e4fb372SMengdong Lin 		return NULL;
24605e4fb372SMengdong Lin 
24615e4fb372SMengdong Lin 	/*
24625e4fb372SMengdong Lin 	 * Back in the old days when we still had component-less DAIs,
24635e4fb372SMengdong Lin 	 * instead of having a static name, component-less DAIs would
24645e4fb372SMengdong Lin 	 * inherit the name of the parent device so it is possible to
24655e4fb372SMengdong Lin 	 * register multiple instances of the DAI. We still need to keep
24665e4fb372SMengdong Lin 	 * the same naming style even though those DAIs are not
24675e4fb372SMengdong Lin 	 * component-less anymore.
24685e4fb372SMengdong Lin 	 */
24695e4fb372SMengdong Lin 	if (legacy_dai_naming &&
24705e4fb372SMengdong Lin 	    (dai_drv->id == 0 || dai_drv->name == NULL)) {
24715e4fb372SMengdong Lin 		dai->name = fmt_single_name(dev, &dai->id);
24725e4fb372SMengdong Lin 	} else {
24735e4fb372SMengdong Lin 		dai->name = fmt_multiple_name(dev, dai_drv);
24745e4fb372SMengdong Lin 		if (dai_drv->id)
24755e4fb372SMengdong Lin 			dai->id = dai_drv->id;
24765e4fb372SMengdong Lin 		else
24775e4fb372SMengdong Lin 			dai->id = component->num_dai;
24785e4fb372SMengdong Lin 	}
247950014499SKuninori Morimoto 	if (!dai->name)
24805e4fb372SMengdong Lin 		return NULL;
24815e4fb372SMengdong Lin 
24825e4fb372SMengdong Lin 	dai->component = component;
24835e4fb372SMengdong Lin 	dai->dev = dev;
24845e4fb372SMengdong Lin 	dai->driver = dai_drv;
24855e4fb372SMengdong Lin 
248615a0c645SKuninori Morimoto 	/* see for_each_component_dais */
248758bf4179SKuninori Morimoto 	list_add_tail(&dai->list, &component->dai_list);
24885e4fb372SMengdong Lin 	component->num_dai++;
24895e4fb372SMengdong Lin 
24905e4fb372SMengdong Lin 	dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
24915e4fb372SMengdong Lin 	return dai;
24925e4fb372SMengdong Lin }
2493e11381f3SKuninori Morimoto 
24949115171aSMark Brown /**
2495f7b6603cSMauro Carvalho Chehab  * snd_soc_unregister_dais - Unregister DAIs from the ASoC core
24963f6674aeSKuninori Morimoto  *
24973f6674aeSKuninori Morimoto  * @component: The component for which the DAIs should be unregistered
24983f6674aeSKuninori Morimoto  */
24993f6674aeSKuninori Morimoto static void snd_soc_unregister_dais(struct snd_soc_component *component)
25003f6674aeSKuninori Morimoto {
25013f6674aeSKuninori Morimoto 	struct snd_soc_dai *dai, *_dai;
25023f6674aeSKuninori Morimoto 
2503e11381f3SKuninori Morimoto 	for_each_component_dais_safe(component, dai, _dai)
2504e11381f3SKuninori Morimoto 		snd_soc_unregister_dai(dai);
25053f6674aeSKuninori Morimoto }
25063f6674aeSKuninori Morimoto 
25073f6674aeSKuninori Morimoto /**
2508daf77373SKuninori Morimoto  * snd_soc_register_dais - Register a DAI with the ASoC core
2509daf77373SKuninori Morimoto  *
2510daf77373SKuninori Morimoto  * @component: The component the DAIs are registered for
2511daf77373SKuninori Morimoto  * @dai_drv: DAI driver to use for the DAIs
2512daf77373SKuninori Morimoto  * @count: Number of DAIs
2513daf77373SKuninori Morimoto  */
2514daf77373SKuninori Morimoto static int snd_soc_register_dais(struct snd_soc_component *component,
2515daf77373SKuninori Morimoto 				 struct snd_soc_dai_driver *dai_drv,
2516daf77373SKuninori Morimoto 				 size_t count)
2517daf77373SKuninori Morimoto {
2518daf77373SKuninori Morimoto 	struct snd_soc_dai *dai;
2519daf77373SKuninori Morimoto 	unsigned int i;
2520daf77373SKuninori Morimoto 	int ret;
2521daf77373SKuninori Morimoto 
2522daf77373SKuninori Morimoto 	for (i = 0; i < count; i++) {
252371cb85f5SKuninori Morimoto 		dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
2524daf77373SKuninori Morimoto 				  !component->driver->non_legacy_dai_naming);
2525daf77373SKuninori Morimoto 		if (dai == NULL) {
2526daf77373SKuninori Morimoto 			ret = -ENOMEM;
2527daf77373SKuninori Morimoto 			goto err;
2528daf77373SKuninori Morimoto 		}
2529daf77373SKuninori Morimoto 	}
2530daf77373SKuninori Morimoto 
2531daf77373SKuninori Morimoto 	return 0;
2532daf77373SKuninori Morimoto 
2533daf77373SKuninori Morimoto err:
2534daf77373SKuninori Morimoto 	snd_soc_unregister_dais(component);
2535daf77373SKuninori Morimoto 
2536daf77373SKuninori Morimoto 	return ret;
2537daf77373SKuninori Morimoto }
2538daf77373SKuninori Morimoto 
2539273d778eSKuninori Morimoto #define ENDIANNESS_MAP(name) \
2540273d778eSKuninori Morimoto 	(SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2541273d778eSKuninori Morimoto static u64 endianness_format_map[] = {
2542273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S16_),
2543273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U16_),
2544273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_),
2545273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_),
2546273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S32_),
2547273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U32_),
2548273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S24_3),
2549273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U24_3),
2550273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S20_3),
2551273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U20_3),
2552273d778eSKuninori Morimoto 	ENDIANNESS_MAP(S18_3),
2553273d778eSKuninori Morimoto 	ENDIANNESS_MAP(U18_3),
2554273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT_),
2555273d778eSKuninori Morimoto 	ENDIANNESS_MAP(FLOAT64_),
2556273d778eSKuninori Morimoto 	ENDIANNESS_MAP(IEC958_SUBFRAME_),
2557273d778eSKuninori Morimoto };
2558273d778eSKuninori Morimoto 
2559273d778eSKuninori Morimoto /*
2560273d778eSKuninori Morimoto  * Fix up the DAI formats for endianness: codecs don't actually see
2561273d778eSKuninori Morimoto  * the endianness of the data but we're using the CPU format
2562273d778eSKuninori Morimoto  * definitions which do need to include endianness so we ensure that
2563273d778eSKuninori Morimoto  * codec DAIs always have both big and little endian variants set.
2564273d778eSKuninori Morimoto  */
2565273d778eSKuninori Morimoto static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2566273d778eSKuninori Morimoto {
2567273d778eSKuninori Morimoto 	int i;
2568273d778eSKuninori Morimoto 
2569273d778eSKuninori Morimoto 	for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2570273d778eSKuninori Morimoto 		if (stream->formats & endianness_format_map[i])
2571273d778eSKuninori Morimoto 			stream->formats |= endianness_format_map[i];
2572273d778eSKuninori Morimoto }
2573273d778eSKuninori Morimoto 
2574e894efefSSrinivas Kandagatla static void snd_soc_try_rebind_card(void)
2575e894efefSSrinivas Kandagatla {
2576e894efefSSrinivas Kandagatla 	struct snd_soc_card *card, *c;
2577e894efefSSrinivas Kandagatla 
2578b245d273SKuninori Morimoto 	list_for_each_entry_safe(card, c, &unbind_card_list, list)
2579e894efefSSrinivas Kandagatla 		if (!snd_soc_bind_card(card))
2580e894efefSSrinivas Kandagatla 			list_del(&card->list);
2581e894efefSSrinivas Kandagatla }
2582e894efefSSrinivas Kandagatla 
2583486c7978SKuninori Morimoto static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2584486c7978SKuninori Morimoto {
2585b18768f5SKuninori Morimoto 	struct snd_soc_card *card = component->card;
2586b18768f5SKuninori Morimoto 
2587486c7978SKuninori Morimoto 	snd_soc_unregister_dais(component);
2588b18768f5SKuninori Morimoto 
2589b18768f5SKuninori Morimoto 	if (card)
2590b18768f5SKuninori Morimoto 		snd_soc_unbind_card(card, false);
2591b18768f5SKuninori Morimoto 
2592b18768f5SKuninori Morimoto 	list_del(&component->list);
2593486c7978SKuninori Morimoto }
2594486c7978SKuninori Morimoto 
259508ff7209SCezary Rojewski int snd_soc_component_initialize(struct snd_soc_component *component,
259608ff7209SCezary Rojewski 				 const struct snd_soc_component_driver *driver,
25977274d4cdSCezary Rojewski 				 struct device *dev)
259808ff7209SCezary Rojewski {
259908ff7209SCezary Rojewski 	INIT_LIST_HEAD(&component->dai_list);
260008ff7209SCezary Rojewski 	INIT_LIST_HEAD(&component->dobj_list);
260108ff7209SCezary Rojewski 	INIT_LIST_HEAD(&component->card_list);
260208ff7209SCezary Rojewski 	mutex_init(&component->io_mutex);
260308ff7209SCezary Rojewski 
26047274d4cdSCezary Rojewski 	component->name = fmt_single_name(dev, &component->id);
26057274d4cdSCezary Rojewski 	if (!component->name) {
26067274d4cdSCezary Rojewski 		dev_err(dev, "ASoC: Failed to allocate name\n");
26077274d4cdSCezary Rojewski 		return -ENOMEM;
26087274d4cdSCezary Rojewski 	}
26097274d4cdSCezary Rojewski 
261008ff7209SCezary Rojewski 	component->dev		= dev;
261108ff7209SCezary Rojewski 	component->driver	= driver;
261208ff7209SCezary Rojewski 
261308ff7209SCezary Rojewski 	return 0;
261408ff7209SCezary Rojewski }
261508ff7209SCezary Rojewski EXPORT_SYMBOL_GPL(snd_soc_component_initialize);
261608ff7209SCezary Rojewski 
2617ea029dd8SCezary Rojewski int snd_soc_add_component(struct snd_soc_component *component,
2618d191bd8dSKuninori Morimoto 			  struct snd_soc_dai_driver *dai_drv,
2619d191bd8dSKuninori Morimoto 			  int num_dai)
2620d191bd8dSKuninori Morimoto {
2621bb13109dSLars-Peter Clausen 	int ret;
2622273d778eSKuninori Morimoto 	int i;
2623d191bd8dSKuninori Morimoto 
2624b18768f5SKuninori Morimoto 	mutex_lock(&client_mutex);
2625b18768f5SKuninori Morimoto 
2626ea029dd8SCezary Rojewski 	if (component->driver->endianness) {
2627273d778eSKuninori Morimoto 		for (i = 0; i < num_dai; i++) {
2628273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].playback);
2629273d778eSKuninori Morimoto 			convert_endianness_formats(&dai_drv[i].capture);
2630273d778eSKuninori Morimoto 		}
2631273d778eSKuninori Morimoto 	}
2632273d778eSKuninori Morimoto 
26330e7b25c6SKuninori Morimoto 	ret = snd_soc_register_dais(component, dai_drv, num_dai);
2634bb13109dSLars-Peter Clausen 	if (ret < 0) {
2635ea029dd8SCezary Rojewski 		dev_err(component->dev, "ASoC: Failed to register DAIs: %d\n",
2636ea029dd8SCezary Rojewski 			ret);
2637bb13109dSLars-Peter Clausen 		goto err_cleanup;
2638bb13109dSLars-Peter Clausen 	}
2639bb13109dSLars-Peter Clausen 
2640b18768f5SKuninori Morimoto 	if (!component->driver->write && !component->driver->read) {
2641b18768f5SKuninori Morimoto 		if (!component->regmap)
2642b18768f5SKuninori Morimoto 			component->regmap = dev_get_regmap(component->dev,
2643b18768f5SKuninori Morimoto 							   NULL);
2644b18768f5SKuninori Morimoto 		if (component->regmap)
2645b18768f5SKuninori Morimoto 			snd_soc_component_setup_regmap(component);
2646b18768f5SKuninori Morimoto 	}
2647bb13109dSLars-Peter Clausen 
2648b18768f5SKuninori Morimoto 	/* see for_each_component */
2649b18768f5SKuninori Morimoto 	list_add(&component->list, &component_list);
2650bb13109dSLars-Peter Clausen 
2651bb13109dSLars-Peter Clausen err_cleanup:
2652b18768f5SKuninori Morimoto 	if (ret < 0)
2653486c7978SKuninori Morimoto 		snd_soc_del_component_unlocked(component);
2654ea029dd8SCezary Rojewski 
2655b18768f5SKuninori Morimoto 	mutex_unlock(&client_mutex);
2656b18768f5SKuninori Morimoto 
2657b18768f5SKuninori Morimoto 	if (ret == 0)
2658b18768f5SKuninori Morimoto 		snd_soc_try_rebind_card();
2659b18768f5SKuninori Morimoto 
2660bb13109dSLars-Peter Clausen 	return ret;
2661d191bd8dSKuninori Morimoto }
2662e0dac41bSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_add_component);
2663e0dac41bSKuninori Morimoto 
2664e0dac41bSKuninori Morimoto int snd_soc_register_component(struct device *dev,
2665e0dac41bSKuninori Morimoto 			const struct snd_soc_component_driver *component_driver,
2666e0dac41bSKuninori Morimoto 			struct snd_soc_dai_driver *dai_drv,
2667e0dac41bSKuninori Morimoto 			int num_dai)
2668e0dac41bSKuninori Morimoto {
2669e0dac41bSKuninori Morimoto 	struct snd_soc_component *component;
2670ea029dd8SCezary Rojewski 	int ret;
2671e0dac41bSKuninori Morimoto 
26727ecbd6a9SKuninori Morimoto 	component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
267308e61d03SKuninori Morimoto 	if (!component)
2674e0dac41bSKuninori Morimoto 		return -ENOMEM;
2675e0dac41bSKuninori Morimoto 
2676ea029dd8SCezary Rojewski 	ret = snd_soc_component_initialize(component, component_driver, dev);
2677ea029dd8SCezary Rojewski 	if (ret < 0)
2678ea029dd8SCezary Rojewski 		return ret;
2679ea029dd8SCezary Rojewski 
2680ea029dd8SCezary Rojewski 	return snd_soc_add_component(component, dai_drv, num_dai);
2681e0dac41bSKuninori Morimoto }
2682d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component);
2683d191bd8dSKuninori Morimoto 
2684d191bd8dSKuninori Morimoto /**
268558f30150SMaxime Ripard  * snd_soc_unregister_component_by_driver - Unregister component using a given driver
268658f30150SMaxime Ripard  * from the ASoC core
268758f30150SMaxime Ripard  *
268858f30150SMaxime Ripard  * @dev: The device to unregister
268958f30150SMaxime Ripard  * @component_driver: The component driver to unregister
269058f30150SMaxime Ripard  */
269158f30150SMaxime Ripard void snd_soc_unregister_component_by_driver(struct device *dev,
269258f30150SMaxime Ripard 					    const struct snd_soc_component_driver *component_driver)
269358f30150SMaxime Ripard {
269458f30150SMaxime Ripard 	struct snd_soc_component *component;
269558f30150SMaxime Ripard 
269658f30150SMaxime Ripard 	if (!component_driver)
269758f30150SMaxime Ripard 		return;
269858f30150SMaxime Ripard 
269958f30150SMaxime Ripard 	mutex_lock(&client_mutex);
270058f30150SMaxime Ripard 	component = snd_soc_lookup_component_nolocked(dev, component_driver->name);
270158f30150SMaxime Ripard 	if (!component)
270258f30150SMaxime Ripard 		goto out;
270358f30150SMaxime Ripard 
270458f30150SMaxime Ripard 	snd_soc_del_component_unlocked(component);
270558f30150SMaxime Ripard 
270658f30150SMaxime Ripard out:
270758f30150SMaxime Ripard 	mutex_unlock(&client_mutex);
270858f30150SMaxime Ripard }
270958f30150SMaxime Ripard EXPORT_SYMBOL_GPL(snd_soc_unregister_component_by_driver);
271058f30150SMaxime Ripard 
271158f30150SMaxime Ripard /**
27122eccea8cSKuninori Morimoto  * snd_soc_unregister_component - Unregister all related component
27132eccea8cSKuninori Morimoto  * from the ASoC core
2714d191bd8dSKuninori Morimoto  *
2715628536eaSJonathan Corbet  * @dev: The device to unregister
2716d191bd8dSKuninori Morimoto  */
27172eccea8cSKuninori Morimoto void snd_soc_unregister_component(struct device *dev)
27182eccea8cSKuninori Morimoto {
2719ac6a4dd3SKuninori Morimoto 	mutex_lock(&client_mutex);
2720ac6a4dd3SKuninori Morimoto 	while (1) {
27215600f3d5SKuninori Morimoto 		struct snd_soc_component *component = snd_soc_lookup_component_nolocked(dev, NULL);
27225600f3d5SKuninori Morimoto 
2723ac6a4dd3SKuninori Morimoto 		if (!component)
2724ac6a4dd3SKuninori Morimoto 			break;
2725ac6a4dd3SKuninori Morimoto 
2726ac6a4dd3SKuninori Morimoto 		snd_soc_del_component_unlocked(component);
2727ac6a4dd3SKuninori Morimoto 	}
2728ac6a4dd3SKuninori Morimoto 	mutex_unlock(&client_mutex);
2729d191bd8dSKuninori Morimoto }
2730d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2731d191bd8dSKuninori Morimoto 
2732bec4fa05SStephen Warren /* Retrieve a card's name from device tree */
2733b07609ceSKuninori Morimoto int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2734bec4fa05SStephen Warren 			       const char *propname)
2735bec4fa05SStephen Warren {
2736b07609ceSKuninori Morimoto 	struct device_node *np;
2737bec4fa05SStephen Warren 	int ret;
2738bec4fa05SStephen Warren 
27397e07e7c0STushar Behera 	if (!card->dev) {
27407e07e7c0STushar Behera 		pr_err("card->dev is not set before calling %s\n", __func__);
27417e07e7c0STushar Behera 		return -EINVAL;
27427e07e7c0STushar Behera 	}
27437e07e7c0STushar Behera 
27447e07e7c0STushar Behera 	np = card->dev->of_node;
27457e07e7c0STushar Behera 
2746bec4fa05SStephen Warren 	ret = of_property_read_string_index(np, propname, 0, &card->name);
2747bec4fa05SStephen Warren 	/*
2748bec4fa05SStephen Warren 	 * EINVAL means the property does not exist. This is fine providing
2749bec4fa05SStephen Warren 	 * card->name was previously set, which is checked later in
2750bec4fa05SStephen Warren 	 * snd_soc_register_card.
2751bec4fa05SStephen Warren 	 */
2752bec4fa05SStephen Warren 	if (ret < 0 && ret != -EINVAL) {
2753bec4fa05SStephen Warren 		dev_err(card->dev,
2754f110bfc7SLiam Girdwood 			"ASoC: Property '%s' could not be read: %d\n",
2755bec4fa05SStephen Warren 			propname, ret);
2756bec4fa05SStephen Warren 		return ret;
2757bec4fa05SStephen Warren 	}
2758bec4fa05SStephen Warren 
2759bec4fa05SStephen Warren 	return 0;
2760bec4fa05SStephen Warren }
2761b07609ceSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2762bec4fa05SStephen Warren 
27639a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = {
27649a6d4860SXiubo Li 	SND_SOC_DAPM_MIC("Microphone", NULL),
27659a6d4860SXiubo Li 	SND_SOC_DAPM_LINE("Line", NULL),
27669a6d4860SXiubo Li 	SND_SOC_DAPM_HP("Headphone", NULL),
27679a6d4860SXiubo Li 	SND_SOC_DAPM_SPK("Speaker", NULL),
27689a6d4860SXiubo Li };
27699a6d4860SXiubo Li 
277021efde50SKuninori Morimoto int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
27719a6d4860SXiubo Li 					  const char *propname)
27729a6d4860SXiubo Li {
277321efde50SKuninori Morimoto 	struct device_node *np = card->dev->of_node;
27749a6d4860SXiubo Li 	struct snd_soc_dapm_widget *widgets;
27759a6d4860SXiubo Li 	const char *template, *wname;
2776*99c68653SKuninori Morimoto 	int i, j, num_widgets;
27779a6d4860SXiubo Li 
27789a6d4860SXiubo Li 	num_widgets = of_property_count_strings(np, propname);
27799a6d4860SXiubo Li 	if (num_widgets < 0) {
27809a6d4860SXiubo Li 		dev_err(card->dev,
27819a6d4860SXiubo Li 			"ASoC: Property '%s' does not exist\n",	propname);
27829a6d4860SXiubo Li 		return -EINVAL;
27839a6d4860SXiubo Li 	}
27849a6d4860SXiubo Li 	if (num_widgets & 1) {
27859a6d4860SXiubo Li 		dev_err(card->dev,
27869a6d4860SXiubo Li 			"ASoC: Property '%s' length is not even\n", propname);
27879a6d4860SXiubo Li 		return -EINVAL;
27889a6d4860SXiubo Li 	}
27899a6d4860SXiubo Li 
27909a6d4860SXiubo Li 	num_widgets /= 2;
27919a6d4860SXiubo Li 	if (!num_widgets) {
27929a6d4860SXiubo Li 		dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
27939a6d4860SXiubo Li 			propname);
27949a6d4860SXiubo Li 		return -EINVAL;
27959a6d4860SXiubo Li 	}
27969a6d4860SXiubo Li 
27979a6d4860SXiubo Li 	widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
27989a6d4860SXiubo Li 			       GFP_KERNEL);
27999a6d4860SXiubo Li 	if (!widgets) {
28009a6d4860SXiubo Li 		dev_err(card->dev,
28019a6d4860SXiubo Li 			"ASoC: Could not allocate memory for widgets\n");
28029a6d4860SXiubo Li 		return -ENOMEM;
28039a6d4860SXiubo Li 	}
28049a6d4860SXiubo Li 
28059a6d4860SXiubo Li 	for (i = 0; i < num_widgets; i++) {
2806*99c68653SKuninori Morimoto 		int ret = of_property_read_string_index(np, propname,
28079a6d4860SXiubo Li 							2 * i, &template);
28089a6d4860SXiubo Li 		if (ret) {
28099a6d4860SXiubo Li 			dev_err(card->dev,
28109a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
28119a6d4860SXiubo Li 				propname, 2 * i, ret);
28129a6d4860SXiubo Li 			return -EINVAL;
28139a6d4860SXiubo Li 		}
28149a6d4860SXiubo Li 
28159a6d4860SXiubo Li 		for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
28169a6d4860SXiubo Li 			if (!strncmp(template, simple_widgets[j].name,
28179a6d4860SXiubo Li 				     strlen(simple_widgets[j].name))) {
28189a6d4860SXiubo Li 				widgets[i] = simple_widgets[j];
28199a6d4860SXiubo Li 				break;
28209a6d4860SXiubo Li 			}
28219a6d4860SXiubo Li 		}
28229a6d4860SXiubo Li 
28239a6d4860SXiubo Li 		if (j >= ARRAY_SIZE(simple_widgets)) {
28249a6d4860SXiubo Li 			dev_err(card->dev,
28259a6d4860SXiubo Li 				"ASoC: DAPM widget '%s' is not supported\n",
28269a6d4860SXiubo Li 				template);
28279a6d4860SXiubo Li 			return -EINVAL;
28289a6d4860SXiubo Li 		}
28299a6d4860SXiubo Li 
28309a6d4860SXiubo Li 		ret = of_property_read_string_index(np, propname,
28319a6d4860SXiubo Li 						    (2 * i) + 1,
28329a6d4860SXiubo Li 						    &wname);
28339a6d4860SXiubo Li 		if (ret) {
28349a6d4860SXiubo Li 			dev_err(card->dev,
28359a6d4860SXiubo Li 				"ASoC: Property '%s' index %d read error:%d\n",
28369a6d4860SXiubo Li 				propname, (2 * i) + 1, ret);
28379a6d4860SXiubo Li 			return -EINVAL;
28389a6d4860SXiubo Li 		}
28399a6d4860SXiubo Li 
28409a6d4860SXiubo Li 		widgets[i].name = wname;
28419a6d4860SXiubo Li 	}
28429a6d4860SXiubo Li 
2843f23e860eSNicolin Chen 	card->of_dapm_widgets = widgets;
2844f23e860eSNicolin Chen 	card->num_of_dapm_widgets = num_widgets;
28459a6d4860SXiubo Li 
28469a6d4860SXiubo Li 	return 0;
28479a6d4860SXiubo Li }
284821efde50SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
28499a6d4860SXiubo Li 
2850cbdfab3bSJerome Brunet int snd_soc_of_get_slot_mask(struct device_node *np,
28516131084aSJyri Sarha 			     const char *prop_name,
28526131084aSJyri Sarha 			     unsigned int *mask)
28536131084aSJyri Sarha {
28546131084aSJyri Sarha 	u32 val;
28556c84e591SJyri Sarha 	const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
28566131084aSJyri Sarha 	int i;
28576131084aSJyri Sarha 
28586131084aSJyri Sarha 	if (!of_slot_mask)
28596131084aSJyri Sarha 		return 0;
28606131084aSJyri Sarha 	val /= sizeof(u32);
28616131084aSJyri Sarha 	for (i = 0; i < val; i++)
28626131084aSJyri Sarha 		if (be32_to_cpup(&of_slot_mask[i]))
28636131084aSJyri Sarha 			*mask |= (1 << i);
28646131084aSJyri Sarha 
28656131084aSJyri Sarha 	return val;
28666131084aSJyri Sarha }
2867cbdfab3bSJerome Brunet EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
28686131084aSJyri Sarha 
286989c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np,
28706131084aSJyri Sarha 			      unsigned int *tx_mask,
28716131084aSJyri Sarha 			      unsigned int *rx_mask,
287289c67857SXiubo Li 			      unsigned int *slots,
287389c67857SXiubo Li 			      unsigned int *slot_width)
287489c67857SXiubo Li {
287589c67857SXiubo Li 	u32 val;
287689c67857SXiubo Li 	int ret;
287789c67857SXiubo Li 
28786131084aSJyri Sarha 	if (tx_mask)
28796131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
28806131084aSJyri Sarha 	if (rx_mask)
28816131084aSJyri Sarha 		snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
28826131084aSJyri Sarha 
288389c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-num")) {
288489c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
288589c67857SXiubo Li 		if (ret)
288689c67857SXiubo Li 			return ret;
288789c67857SXiubo Li 
288889c67857SXiubo Li 		if (slots)
288989c67857SXiubo Li 			*slots = val;
289089c67857SXiubo Li 	}
289189c67857SXiubo Li 
289289c67857SXiubo Li 	if (of_property_read_bool(np, "dai-tdm-slot-width")) {
289389c67857SXiubo Li 		ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
289489c67857SXiubo Li 		if (ret)
289589c67857SXiubo Li 			return ret;
289689c67857SXiubo Li 
289789c67857SXiubo Li 		if (slot_width)
289889c67857SXiubo Li 			*slot_width = val;
289989c67857SXiubo Li 	}
290089c67857SXiubo Li 
290189c67857SXiubo Li 	return 0;
290289c67857SXiubo Li }
290389c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
290489c67857SXiubo Li 
29053b710356SKuninori Morimoto void snd_soc_of_parse_node_prefix(struct device_node *np,
29065e3cdaa2SKuninori Morimoto 				  struct snd_soc_codec_conf *codec_conf,
29075e3cdaa2SKuninori Morimoto 				  struct device_node *of_node,
29085e3cdaa2SKuninori Morimoto 				  const char *propname)
29095e3cdaa2SKuninori Morimoto {
29105e3cdaa2SKuninori Morimoto 	const char *str;
29115e3cdaa2SKuninori Morimoto 	int ret;
29125e3cdaa2SKuninori Morimoto 
29135e3cdaa2SKuninori Morimoto 	ret = of_property_read_string(np, propname, &str);
29145e3cdaa2SKuninori Morimoto 	if (ret < 0) {
29155e3cdaa2SKuninori Morimoto 		/* no prefix is not error */
29165e3cdaa2SKuninori Morimoto 		return;
29175e3cdaa2SKuninori Morimoto 	}
29185e3cdaa2SKuninori Morimoto 
2919c13493a2SKuninori Morimoto 	codec_conf->dlc.of_node	= of_node;
29205e3cdaa2SKuninori Morimoto 	codec_conf->name_prefix	= str;
29215e3cdaa2SKuninori Morimoto }
29223b710356SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
29235e3cdaa2SKuninori Morimoto 
29242bc644afSKuninori Morimoto int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
2925a4a54dd5SStephen Warren 				   const char *propname)
2926a4a54dd5SStephen Warren {
29272bc644afSKuninori Morimoto 	struct device_node *np = card->dev->of_node;
2928e3b1e6a1SMark Brown 	int num_routes;
2929a4a54dd5SStephen Warren 	struct snd_soc_dapm_route *routes;
2930a4a54dd5SStephen Warren 	int i, ret;
2931a4a54dd5SStephen Warren 
2932a4a54dd5SStephen Warren 	num_routes = of_property_count_strings(np, propname);
2933c34ce320SRichard Zhao 	if (num_routes < 0 || num_routes & 1) {
293410e8aa9aSMichał Mirosław 		dev_err(card->dev,
293510e8aa9aSMichał Mirosław 			"ASoC: Property '%s' does not exist or its length is not even\n",
293610e8aa9aSMichał Mirosław 			propname);
2937a4a54dd5SStephen Warren 		return -EINVAL;
2938a4a54dd5SStephen Warren 	}
2939a4a54dd5SStephen Warren 	num_routes /= 2;
2940a4a54dd5SStephen Warren 
2941a86854d0SKees Cook 	routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
2942a4a54dd5SStephen Warren 			      GFP_KERNEL);
2943a4a54dd5SStephen Warren 	if (!routes) {
2944a4a54dd5SStephen Warren 		dev_err(card->dev,
2945f110bfc7SLiam Girdwood 			"ASoC: Could not allocate DAPM route table\n");
29467d3865a1SZhen Lei 		return -ENOMEM;
2947a4a54dd5SStephen Warren 	}
2948a4a54dd5SStephen Warren 
2949a4a54dd5SStephen Warren 	for (i = 0; i < num_routes; i++) {
2950a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
2951e3b1e6a1SMark Brown 			2 * i, &routes[i].sink);
2952a4a54dd5SStephen Warren 		if (ret) {
2953c871bd0bSMark Brown 			dev_err(card->dev,
2954c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
2955c871bd0bSMark Brown 				propname, 2 * i, ret);
2956a4a54dd5SStephen Warren 			return -EINVAL;
2957a4a54dd5SStephen Warren 		}
2958a4a54dd5SStephen Warren 		ret = of_property_read_string_index(np, propname,
2959e3b1e6a1SMark Brown 			(2 * i) + 1, &routes[i].source);
2960a4a54dd5SStephen Warren 		if (ret) {
2961a4a54dd5SStephen Warren 			dev_err(card->dev,
2962c871bd0bSMark Brown 				"ASoC: Property '%s' index %d could not be read: %d\n",
2963c871bd0bSMark Brown 				propname, (2 * i) + 1, ret);
2964a4a54dd5SStephen Warren 			return -EINVAL;
2965a4a54dd5SStephen Warren 		}
2966a4a54dd5SStephen Warren 	}
2967a4a54dd5SStephen Warren 
2968f23e860eSNicolin Chen 	card->num_of_dapm_routes = num_routes;
2969f23e860eSNicolin Chen 	card->of_dapm_routes = routes;
2970a4a54dd5SStephen Warren 
2971a4a54dd5SStephen Warren 	return 0;
2972a4a54dd5SStephen Warren }
29732bc644afSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
2974a4a54dd5SStephen Warren 
29751ae0965dSStephan Gerhold int snd_soc_of_parse_aux_devs(struct snd_soc_card *card, const char *propname)
29761ae0965dSStephan Gerhold {
29771ae0965dSStephan Gerhold 	struct device_node *node = card->dev->of_node;
29781ae0965dSStephan Gerhold 	struct snd_soc_aux_dev *aux;
29791ae0965dSStephan Gerhold 	int num, i;
29801ae0965dSStephan Gerhold 
29811ae0965dSStephan Gerhold 	num = of_count_phandle_with_args(node, propname, NULL);
29821ae0965dSStephan Gerhold 	if (num == -ENOENT) {
29831ae0965dSStephan Gerhold 		return 0;
29841ae0965dSStephan Gerhold 	} else if (num < 0) {
29851ae0965dSStephan Gerhold 		dev_err(card->dev, "ASOC: Property '%s' could not be read: %d\n",
29861ae0965dSStephan Gerhold 			propname, num);
29871ae0965dSStephan Gerhold 		return num;
29881ae0965dSStephan Gerhold 	}
29891ae0965dSStephan Gerhold 
29901ae0965dSStephan Gerhold 	aux = devm_kcalloc(card->dev, num, sizeof(*aux), GFP_KERNEL);
29911ae0965dSStephan Gerhold 	if (!aux)
29921ae0965dSStephan Gerhold 		return -ENOMEM;
29931ae0965dSStephan Gerhold 	card->aux_dev = aux;
29941ae0965dSStephan Gerhold 	card->num_aux_devs = num;
29951ae0965dSStephan Gerhold 
29961ae0965dSStephan Gerhold 	for_each_card_pre_auxs(card, i, aux) {
29971ae0965dSStephan Gerhold 		aux->dlc.of_node = of_parse_phandle(node, propname, i);
29981ae0965dSStephan Gerhold 		if (!aux->dlc.of_node)
29991ae0965dSStephan Gerhold 			return -EINVAL;
30001ae0965dSStephan Gerhold 	}
30011ae0965dSStephan Gerhold 
30021ae0965dSStephan Gerhold 	return 0;
30031ae0965dSStephan Gerhold }
30041ae0965dSStephan Gerhold EXPORT_SYMBOL_GPL(snd_soc_of_parse_aux_devs);
30051ae0965dSStephan Gerhold 
3006b44a67f8SKuninori Morimoto unsigned int snd_soc_daifmt_clock_provider_fliped(unsigned int dai_fmt)
3007b44a67f8SKuninori Morimoto {
3008b44a67f8SKuninori Morimoto 	unsigned int inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK;
3009b44a67f8SKuninori Morimoto 
3010b44a67f8SKuninori Morimoto 	switch (dai_fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
3011b44a67f8SKuninori Morimoto 	case SND_SOC_DAIFMT_CBP_CFP:
3012b44a67f8SKuninori Morimoto 		inv_dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
3013b44a67f8SKuninori Morimoto 		break;
3014b44a67f8SKuninori Morimoto 	case SND_SOC_DAIFMT_CBP_CFC:
3015b44a67f8SKuninori Morimoto 		inv_dai_fmt |= SND_SOC_DAIFMT_CBC_CFP;
3016b44a67f8SKuninori Morimoto 		break;
3017b44a67f8SKuninori Morimoto 	case SND_SOC_DAIFMT_CBC_CFP:
3018b44a67f8SKuninori Morimoto 		inv_dai_fmt |= SND_SOC_DAIFMT_CBP_CFC;
3019b44a67f8SKuninori Morimoto 		break;
3020b44a67f8SKuninori Morimoto 	case SND_SOC_DAIFMT_CBC_CFC:
3021b44a67f8SKuninori Morimoto 		inv_dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
3022b44a67f8SKuninori Morimoto 		break;
3023b44a67f8SKuninori Morimoto 	}
3024b44a67f8SKuninori Morimoto 
3025b44a67f8SKuninori Morimoto 	return inv_dai_fmt;
3026b44a67f8SKuninori Morimoto }
3027b44a67f8SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_daifmt_clock_provider_fliped);
3028b44a67f8SKuninori Morimoto 
302991ae4477SKuninori Morimoto unsigned int snd_soc_daifmt_clock_provider_from_bitmap(unsigned int bit_frame)
303091ae4477SKuninori Morimoto {
30317766861dSKuninori Morimoto 	/*
30327766861dSKuninori Morimoto 	 * bit_frame is return value from
30337766861dSKuninori Morimoto 	 *	snd_soc_daifmt_parse_clock_provider_raw()
30347766861dSKuninori Morimoto 	 */
30357766861dSKuninori Morimoto 
303691ae4477SKuninori Morimoto 	/* Codec base */
303791ae4477SKuninori Morimoto 	switch (bit_frame) {
303891ae4477SKuninori Morimoto 	case 0x11:
303991ae4477SKuninori Morimoto 		return SND_SOC_DAIFMT_CBP_CFP;
304091ae4477SKuninori Morimoto 	case 0x10:
304191ae4477SKuninori Morimoto 		return SND_SOC_DAIFMT_CBP_CFC;
304291ae4477SKuninori Morimoto 	case 0x01:
304391ae4477SKuninori Morimoto 		return SND_SOC_DAIFMT_CBC_CFP;
304491ae4477SKuninori Morimoto 	default:
304591ae4477SKuninori Morimoto 		return SND_SOC_DAIFMT_CBC_CFC;
304691ae4477SKuninori Morimoto 	}
304791ae4477SKuninori Morimoto 
304891ae4477SKuninori Morimoto 	return 0;
304991ae4477SKuninori Morimoto }
305091ae4477SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_daifmt_clock_provider_from_bitmap);
305191ae4477SKuninori Morimoto 
30527766861dSKuninori Morimoto unsigned int snd_soc_daifmt_parse_format(struct device_node *np,
30537766861dSKuninori Morimoto 					 const char *prefix)
3054a7930ed4SKuninori Morimoto {
30555ad76775SKuninori Morimoto 	int ret;
3056a7930ed4SKuninori Morimoto 	char prop[128];
3057a7930ed4SKuninori Morimoto 	unsigned int format = 0;
3058a7930ed4SKuninori Morimoto 	int bit, frame;
3059a7930ed4SKuninori Morimoto 	const char *str;
3060a7930ed4SKuninori Morimoto 	struct {
3061a7930ed4SKuninori Morimoto 		char *name;
3062a7930ed4SKuninori Morimoto 		unsigned int val;
3063a7930ed4SKuninori Morimoto 	} of_fmt_table[] = {
3064a7930ed4SKuninori Morimoto 		{ "i2s",	SND_SOC_DAIFMT_I2S },
3065a7930ed4SKuninori Morimoto 		{ "right_j",	SND_SOC_DAIFMT_RIGHT_J },
3066a7930ed4SKuninori Morimoto 		{ "left_j",	SND_SOC_DAIFMT_LEFT_J },
3067a7930ed4SKuninori Morimoto 		{ "dsp_a",	SND_SOC_DAIFMT_DSP_A },
3068a7930ed4SKuninori Morimoto 		{ "dsp_b",	SND_SOC_DAIFMT_DSP_B },
3069a7930ed4SKuninori Morimoto 		{ "ac97",	SND_SOC_DAIFMT_AC97 },
3070a7930ed4SKuninori Morimoto 		{ "pdm",	SND_SOC_DAIFMT_PDM},
3071a7930ed4SKuninori Morimoto 		{ "msb",	SND_SOC_DAIFMT_MSB },
3072a7930ed4SKuninori Morimoto 		{ "lsb",	SND_SOC_DAIFMT_LSB },
3073a7930ed4SKuninori Morimoto 	};
3074a7930ed4SKuninori Morimoto 
3075a7930ed4SKuninori Morimoto 	if (!prefix)
3076a7930ed4SKuninori Morimoto 		prefix = "";
3077a7930ed4SKuninori Morimoto 
3078a7930ed4SKuninori Morimoto 	/*
30795711c979SKuninori Morimoto 	 * check "dai-format = xxx"
30805711c979SKuninori Morimoto 	 * or    "[prefix]format = xxx"
3081a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_FORMAT_MASK area
3082a7930ed4SKuninori Morimoto 	 */
30835711c979SKuninori Morimoto 	ret = of_property_read_string(np, "dai-format", &str);
30845711c979SKuninori Morimoto 	if (ret < 0) {
3085a7930ed4SKuninori Morimoto 		snprintf(prop, sizeof(prop), "%sformat", prefix);
3086a7930ed4SKuninori Morimoto 		ret = of_property_read_string(np, prop, &str);
30875711c979SKuninori Morimoto 	}
3088a7930ed4SKuninori Morimoto 	if (ret == 0) {
30895ad76775SKuninori Morimoto 		int i;
30905ad76775SKuninori Morimoto 
3091a7930ed4SKuninori Morimoto 		for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3092a7930ed4SKuninori Morimoto 			if (strcmp(str, of_fmt_table[i].name) == 0) {
3093a7930ed4SKuninori Morimoto 				format |= of_fmt_table[i].val;
3094a7930ed4SKuninori Morimoto 				break;
3095a7930ed4SKuninori Morimoto 			}
3096a7930ed4SKuninori Morimoto 		}
3097a7930ed4SKuninori Morimoto 	}
3098a7930ed4SKuninori Morimoto 
3099a7930ed4SKuninori Morimoto 	/*
31008c2d6a9fSKuninori Morimoto 	 * check "[prefix]continuous-clock"
3101a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_CLOCK_MASK area
3102a7930ed4SKuninori Morimoto 	 */
31038c2d6a9fSKuninori Morimoto 	snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
310451930295SJulia Lawall 	if (of_property_read_bool(np, prop))
31058c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_CONT;
31068c2d6a9fSKuninori Morimoto 	else
31078c2d6a9fSKuninori Morimoto 		format |= SND_SOC_DAIFMT_GATED;
3108a7930ed4SKuninori Morimoto 
3109a7930ed4SKuninori Morimoto 	/*
3110a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-inversion"
3111a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-inversion"
3112a7930ed4SKuninori Morimoto 	 * SND_SOC_DAIFMT_INV_MASK area
3113a7930ed4SKuninori Morimoto 	 */
3114a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3115a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3116a7930ed4SKuninori Morimoto 
3117a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3118a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3119a7930ed4SKuninori Morimoto 
3120a7930ed4SKuninori Morimoto 	switch ((bit << 4) + frame) {
3121a7930ed4SKuninori Morimoto 	case 0x11:
3122a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_IF;
3123a7930ed4SKuninori Morimoto 		break;
3124a7930ed4SKuninori Morimoto 	case 0x10:
3125a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_IB_NF;
3126a7930ed4SKuninori Morimoto 		break;
3127a7930ed4SKuninori Morimoto 	case 0x01:
3128a7930ed4SKuninori Morimoto 		format |= SND_SOC_DAIFMT_NB_IF;
3129a7930ed4SKuninori Morimoto 		break;
3130a7930ed4SKuninori Morimoto 	default:
3131a7930ed4SKuninori Morimoto 		/* SND_SOC_DAIFMT_NB_NF is default */
3132a7930ed4SKuninori Morimoto 		break;
3133a7930ed4SKuninori Morimoto 	}
3134a7930ed4SKuninori Morimoto 
31357766861dSKuninori Morimoto 	return format;
31367766861dSKuninori Morimoto }
31377766861dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_daifmt_parse_format);
31387766861dSKuninori Morimoto 
31397766861dSKuninori Morimoto unsigned int snd_soc_daifmt_parse_clock_provider_raw(struct device_node *np,
31407766861dSKuninori Morimoto 						     const char *prefix,
31417766861dSKuninori Morimoto 						     struct device_node **bitclkmaster,
31427766861dSKuninori Morimoto 						     struct device_node **framemaster)
31437766861dSKuninori Morimoto {
31447766861dSKuninori Morimoto 	char prop[128];
31457766861dSKuninori Morimoto 	unsigned int bit, frame;
31467766861dSKuninori Morimoto 
31477766861dSKuninori Morimoto 	if (!prefix)
31487766861dSKuninori Morimoto 		prefix = "";
31497766861dSKuninori Morimoto 
3150a7930ed4SKuninori Morimoto 	/*
3151a7930ed4SKuninori Morimoto 	 * check "[prefix]bitclock-master"
3152a7930ed4SKuninori Morimoto 	 * check "[prefix]frame-master"
3153a7930ed4SKuninori Morimoto 	 */
3154a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3155a7930ed4SKuninori Morimoto 	bit = !!of_get_property(np, prop, NULL);
3156389cb834SJyri Sarha 	if (bit && bitclkmaster)
3157389cb834SJyri Sarha 		*bitclkmaster = of_parse_phandle(np, prop, 0);
3158a7930ed4SKuninori Morimoto 
3159a7930ed4SKuninori Morimoto 	snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3160a7930ed4SKuninori Morimoto 	frame = !!of_get_property(np, prop, NULL);
3161389cb834SJyri Sarha 	if (frame && framemaster)
3162389cb834SJyri Sarha 		*framemaster = of_parse_phandle(np, prop, 0);
3163a7930ed4SKuninori Morimoto 
31647766861dSKuninori Morimoto 	/*
31657766861dSKuninori Morimoto 	 * return bitmap.
31667766861dSKuninori Morimoto 	 * It will be parameter of
31677766861dSKuninori Morimoto 	 *	snd_soc_daifmt_clock_provider_from_bitmap()
31687766861dSKuninori Morimoto 	 */
31697766861dSKuninori Morimoto 	return (bit << 4) + frame;
3170a7930ed4SKuninori Morimoto }
31717766861dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_daifmt_parse_clock_provider_raw);
3172a7930ed4SKuninori Morimoto 
3173a180e8b9SKuninori Morimoto int snd_soc_get_dai_id(struct device_node *ep)
3174a180e8b9SKuninori Morimoto {
317509d4cc03SKuninori Morimoto 	struct snd_soc_component *component;
3176c1e230f0SKuninori Morimoto 	struct snd_soc_dai_link_component dlc;
3177a180e8b9SKuninori Morimoto 	int ret;
3178a180e8b9SKuninori Morimoto 
3179c1e230f0SKuninori Morimoto 	dlc.of_node	= of_graph_get_port_parent(ep);
3180c1e230f0SKuninori Morimoto 	dlc.name	= NULL;
3181a180e8b9SKuninori Morimoto 	/*
3182a180e8b9SKuninori Morimoto 	 * For example HDMI case, HDMI has video/sound port,
3183a180e8b9SKuninori Morimoto 	 * but ALSA SoC needs sound port number only.
3184a180e8b9SKuninori Morimoto 	 * Thus counting HDMI DT port/endpoint doesn't work.
3185a180e8b9SKuninori Morimoto 	 * Then, it should have .of_xlate_dai_id
3186a180e8b9SKuninori Morimoto 	 */
3187a180e8b9SKuninori Morimoto 	ret = -ENOTSUPP;
3188a180e8b9SKuninori Morimoto 	mutex_lock(&client_mutex);
3189c1e230f0SKuninori Morimoto 	component = soc_find_component(&dlc);
31902c7b1704SKuninori Morimoto 	if (component)
31912c7b1704SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_id(component, ep);
3192a180e8b9SKuninori Morimoto 	mutex_unlock(&client_mutex);
3193a180e8b9SKuninori Morimoto 
3194c1e230f0SKuninori Morimoto 	of_node_put(dlc.of_node);
3195c0a480d1STony Lindgren 
3196a180e8b9SKuninori Morimoto 	return ret;
3197a180e8b9SKuninori Morimoto }
3198a180e8b9SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3199a180e8b9SKuninori Morimoto 
3200933f98beSKrzysztof Kozlowski int snd_soc_get_dai_name(const struct of_phandle_args *args,
3201cb470087SKuninori Morimoto 				const char **dai_name)
3202cb470087SKuninori Morimoto {
3203cb470087SKuninori Morimoto 	struct snd_soc_component *pos;
320493b0f3eeSJean-Francois Moine 	int ret = -EPROBE_DEFER;
3205cb470087SKuninori Morimoto 
3206cb470087SKuninori Morimoto 	mutex_lock(&client_mutex);
3207368dee94SKuninori Morimoto 	for_each_component(pos) {
3208bce00560SKuninori Morimoto 		struct device_node *component_of_node = soc_component_to_node(pos);
32093e0aa8d8SJyri Sarha 
32103e0aa8d8SJyri Sarha 		if (component_of_node != args->np)
3211cb470087SKuninori Morimoto 			continue;
3212cb470087SKuninori Morimoto 
3213a2a34175SKuninori Morimoto 		ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3214a2a34175SKuninori Morimoto 		if (ret == -ENOTSUPP) {
321558bf4179SKuninori Morimoto 			struct snd_soc_dai *dai;
32166833c452SKuninori Morimoto 			int id = -1;
32176833c452SKuninori Morimoto 
321893b0f3eeSJean-Francois Moine 			switch (args->args_count) {
32196833c452SKuninori Morimoto 			case 0:
32206833c452SKuninori Morimoto 				id = 0; /* same as dai_drv[0] */
32216833c452SKuninori Morimoto 				break;
32226833c452SKuninori Morimoto 			case 1:
322393b0f3eeSJean-Francois Moine 				id = args->args[0];
32246833c452SKuninori Morimoto 				break;
32256833c452SKuninori Morimoto 			default:
32266833c452SKuninori Morimoto 				/* not supported */
3227cb470087SKuninori Morimoto 				break;
3228cb470087SKuninori Morimoto 			}
3229cb470087SKuninori Morimoto 
32306833c452SKuninori Morimoto 			if (id < 0 || id >= pos->num_dai) {
32316833c452SKuninori Morimoto 				ret = -EINVAL;
32323dcba280SNicolin Chen 				continue;
32336833c452SKuninori Morimoto 			}
3234e41975edSXiubo Li 
3235e41975edSXiubo Li 			ret = 0;
3236e41975edSXiubo Li 
323758bf4179SKuninori Morimoto 			/* find target DAI */
323815a0c645SKuninori Morimoto 			for_each_component_dais(pos, dai) {
323958bf4179SKuninori Morimoto 				if (id == 0)
324058bf4179SKuninori Morimoto 					break;
324158bf4179SKuninori Morimoto 				id--;
324258bf4179SKuninori Morimoto 			}
324358bf4179SKuninori Morimoto 
324458bf4179SKuninori Morimoto 			*dai_name = dai->driver->name;
3245e41975edSXiubo Li 			if (!*dai_name)
3246e41975edSXiubo Li 				*dai_name = pos->name;
32471dfa5a5aSJerome Brunet 		} else if (ret) {
32481dfa5a5aSJerome Brunet 			/*
32491dfa5a5aSJerome Brunet 			 * if another error than ENOTSUPP is returned go on and
32501dfa5a5aSJerome Brunet 			 * check if another component is provided with the same
32511dfa5a5aSJerome Brunet 			 * node. This may happen if a device provides several
32521dfa5a5aSJerome Brunet 			 * components
32531dfa5a5aSJerome Brunet 			 */
32541dfa5a5aSJerome Brunet 			continue;
32556833c452SKuninori Morimoto 		}
32566833c452SKuninori Morimoto 
3257cb470087SKuninori Morimoto 		break;
3258cb470087SKuninori Morimoto 	}
3259cb470087SKuninori Morimoto 	mutex_unlock(&client_mutex);
326093b0f3eeSJean-Francois Moine 	return ret;
326193b0f3eeSJean-Francois Moine }
32621ad8ec53SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
326393b0f3eeSJean-Francois Moine 
326493b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_name(struct device_node *of_node,
326593b0f3eeSJean-Francois Moine 			    const char **dai_name)
326693b0f3eeSJean-Francois Moine {
326793b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
326893b0f3eeSJean-Francois Moine 	int ret;
326993b0f3eeSJean-Francois Moine 
327093b0f3eeSJean-Francois Moine 	ret = of_parse_phandle_with_args(of_node, "sound-dai",
327193b0f3eeSJean-Francois Moine 					 "#sound-dai-cells", 0, &args);
327293b0f3eeSJean-Francois Moine 	if (ret)
327393b0f3eeSJean-Francois Moine 		return ret;
327493b0f3eeSJean-Francois Moine 
327593b0f3eeSJean-Francois Moine 	ret = snd_soc_get_dai_name(&args, dai_name);
3276cb470087SKuninori Morimoto 
3277cb470087SKuninori Morimoto 	of_node_put(args.np);
3278cb470087SKuninori Morimoto 
3279cb470087SKuninori Morimoto 	return ret;
3280cb470087SKuninori Morimoto }
3281cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3282cb470087SKuninori Morimoto 
328393b0f3eeSJean-Francois Moine /*
328494685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
328594685763SSylwester Nawrocki  * @dai_link: DAI link
328694685763SSylwester Nawrocki  *
328794685763SSylwester Nawrocki  * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
328894685763SSylwester Nawrocki  */
328994685763SSylwester Nawrocki void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
329094685763SSylwester Nawrocki {
32913db769f1SKuninori Morimoto 	struct snd_soc_dai_link_component *component;
329294685763SSylwester Nawrocki 	int index;
329394685763SSylwester Nawrocki 
32943db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
329594685763SSylwester Nawrocki 		if (!component->of_node)
329694685763SSylwester Nawrocki 			break;
329794685763SSylwester Nawrocki 		of_node_put(component->of_node);
329894685763SSylwester Nawrocki 		component->of_node = NULL;
329994685763SSylwester Nawrocki 	}
330094685763SSylwester Nawrocki }
330194685763SSylwester Nawrocki EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
330294685763SSylwester Nawrocki 
330394685763SSylwester Nawrocki /*
330493b0f3eeSJean-Francois Moine  * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
330593b0f3eeSJean-Francois Moine  * @dev: Card device
330693b0f3eeSJean-Francois Moine  * @of_node: Device node
330793b0f3eeSJean-Francois Moine  * @dai_link: DAI link
330893b0f3eeSJean-Francois Moine  *
330993b0f3eeSJean-Francois Moine  * Builds an array of CODEC DAI components from the DAI link property
331093b0f3eeSJean-Francois Moine  * 'sound-dai'.
331193b0f3eeSJean-Francois Moine  * The array is set in the DAI link and the number of DAIs is set accordingly.
331294685763SSylwester Nawrocki  * The device nodes in the array (of_node) must be dereferenced by calling
331394685763SSylwester Nawrocki  * snd_soc_of_put_dai_link_codecs() on @dai_link.
331493b0f3eeSJean-Francois Moine  *
331593b0f3eeSJean-Francois Moine  * Returns 0 for success
331693b0f3eeSJean-Francois Moine  */
331793b0f3eeSJean-Francois Moine int snd_soc_of_get_dai_link_codecs(struct device *dev,
331893b0f3eeSJean-Francois Moine 				   struct device_node *of_node,
331993b0f3eeSJean-Francois Moine 				   struct snd_soc_dai_link *dai_link)
332093b0f3eeSJean-Francois Moine {
332193b0f3eeSJean-Francois Moine 	struct of_phandle_args args;
332293b0f3eeSJean-Francois Moine 	struct snd_soc_dai_link_component *component;
332393b0f3eeSJean-Francois Moine 	char *name;
332493b0f3eeSJean-Francois Moine 	int index, num_codecs, ret;
332593b0f3eeSJean-Francois Moine 
332693b0f3eeSJean-Francois Moine 	/* Count the number of CODECs */
332793b0f3eeSJean-Francois Moine 	name = "sound-dai";
332893b0f3eeSJean-Francois Moine 	num_codecs = of_count_phandle_with_args(of_node, name,
332993b0f3eeSJean-Francois Moine 						"#sound-dai-cells");
333093b0f3eeSJean-Francois Moine 	if (num_codecs <= 0) {
333193b0f3eeSJean-Francois Moine 		if (num_codecs == -ENOENT)
333293b0f3eeSJean-Francois Moine 			dev_err(dev, "No 'sound-dai' property\n");
333393b0f3eeSJean-Francois Moine 		else
333493b0f3eeSJean-Francois Moine 			dev_err(dev, "Bad phandle in 'sound-dai'\n");
333593b0f3eeSJean-Francois Moine 		return num_codecs;
333693b0f3eeSJean-Francois Moine 	}
3337a86854d0SKees Cook 	component = devm_kcalloc(dev,
3338a86854d0SKees Cook 				 num_codecs, sizeof(*component),
333993b0f3eeSJean-Francois Moine 				 GFP_KERNEL);
334093b0f3eeSJean-Francois Moine 	if (!component)
334193b0f3eeSJean-Francois Moine 		return -ENOMEM;
334293b0f3eeSJean-Francois Moine 	dai_link->codecs = component;
334393b0f3eeSJean-Francois Moine 	dai_link->num_codecs = num_codecs;
334493b0f3eeSJean-Francois Moine 
334593b0f3eeSJean-Francois Moine 	/* Parse the list */
33463db769f1SKuninori Morimoto 	for_each_link_codecs(dai_link, index, component) {
334793b0f3eeSJean-Francois Moine 		ret = of_parse_phandle_with_args(of_node, name,
334893b0f3eeSJean-Francois Moine 						 "#sound-dai-cells",
334993b0f3eeSJean-Francois Moine 						 index, &args);
335093b0f3eeSJean-Francois Moine 		if (ret)
335193b0f3eeSJean-Francois Moine 			goto err;
335293b0f3eeSJean-Francois Moine 		component->of_node = args.np;
335393b0f3eeSJean-Francois Moine 		ret = snd_soc_get_dai_name(&args, &component->dai_name);
335493b0f3eeSJean-Francois Moine 		if (ret < 0)
335593b0f3eeSJean-Francois Moine 			goto err;
335693b0f3eeSJean-Francois Moine 	}
335793b0f3eeSJean-Francois Moine 	return 0;
335893b0f3eeSJean-Francois Moine err:
335994685763SSylwester Nawrocki 	snd_soc_of_put_dai_link_codecs(dai_link);
336093b0f3eeSJean-Francois Moine 	dai_link->codecs = NULL;
336193b0f3eeSJean-Francois Moine 	dai_link->num_codecs = 0;
336293b0f3eeSJean-Francois Moine 	return ret;
336393b0f3eeSJean-Francois Moine }
336493b0f3eeSJean-Francois Moine EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
336593b0f3eeSJean-Francois Moine 
3366c9b3a40fSTakashi Iwai static int __init snd_soc_init(void)
3367db2a4165SFrank Mandarino {
33686553bf06SLars-Peter Clausen 	snd_soc_debugfs_init();
3369fb257897SMark Brown 	snd_soc_util_init();
3370fb257897SMark Brown 
3371db2a4165SFrank Mandarino 	return platform_driver_register(&soc_driver);
3372db2a4165SFrank Mandarino }
33734abe8e16SMark Brown module_init(snd_soc_init);
3374db2a4165SFrank Mandarino 
33757d8c16a6SMark Brown static void __exit snd_soc_exit(void)
3376db2a4165SFrank Mandarino {
3377fb257897SMark Brown 	snd_soc_util_exit();
33786553bf06SLars-Peter Clausen 	snd_soc_debugfs_exit();
3379fb257897SMark Brown 
3380db2a4165SFrank Mandarino 	platform_driver_unregister(&soc_driver);
3381db2a4165SFrank Mandarino }
3382db2a4165SFrank Mandarino module_exit(snd_soc_exit);
3383db2a4165SFrank Mandarino 
3384db2a4165SFrank Mandarino /* Module information */
3385d331124dSLiam Girdwood MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3386db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core");
3387db2a4165SFrank Mandarino MODULE_LICENSE("GPL");
33888b45a209SKay Sievers MODULE_ALIAS("platform:soc-audio");
3389